Common Generalization Modules¶
Classifier¶
-
class
dglib.modules.classifier.
ImageClassifier
(backbone, num_classes, freeze_bn=False, dropout_p=0.1, **kwargs)[source]¶ ImageClassifier specific for reproducing results of DomainBed. You are free to freeze all BatchNorm2d layers and insert one additional Dropout layer, this can achieve better results for some datasets like PACS but may be worse for others.
- Parameters
backbone (torch.nn.Module) – Any backbone to extract features from data
num_classes (int) – Number of classes
freeze_bn (bool, optional) – whether to freeze all BatchNorm2d layers. Default: False
dropout_p (float, optional) – dropout ratio for additional Dropout layer, this layer is only used when freeze_bn is True. Default: 0.1
Sampler¶
-
class
dglib.modules.sampler.
DefaultSampler
(data_source, batch_size)[source]¶ Traverse all \(N\) domains, randomly select \(K\) samples in each domain to form a mini-batch of size \(N\times K\).
- Parameters
data_source (ConcatDataset) – dataset that contains data from multiple domains
batch_size (int) – mini-batch size (\(N\times K\) here)
-
class
dglib.modules.sampler.
RandomDomainSampler
(data_source, batch_size, n_domains_per_batch)[source]¶ Randomly sample \(N\) domains, then randomly select \(K\) samples in each domain to form a mini-batch of size \(N\times K\).
-
class
dglib.generalization.mixstyle.sampler.
RandomDomainMultiInstanceSampler
(dataset, batch_size, n_domains_per_batch, num_instances)[source]¶ Randomly sample \(N\) domains, then randomly select \(P\) instances in each domain, for each instance, randomly select \(K\) images to form a mini-batch of size \(N\times P\times K\).
- Parameters
dataset (ConcatDataset) – dataset that contains data from multiple domains
batch_size (int) – mini-batch size (\(N\times P\times K\) here)
n_domains_per_batch (int) – number of domains to select in a single mini-batch (\(N\) here)
num_instances (int) – number of instances to select in each domain (\(K\) here)