diff --git a/.gitignore b/.gitignore index 305281c..9b0c0ca 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ __pycache__/ *.py[cod] *$py.class _gsdata_ +*.ipynb # C extensions *.so @@ -105,3 +106,4 @@ ENV/ .idea .DS_Store main_legacy.py +test.ipynb diff --git a/core/dann.py b/core/dann.py index c9462f5..b96c7af 100644 --- a/core/dann.py +++ b/core/dann.py @@ -101,4 +101,4 @@ def train_dann(model, src_data_loader, tgt_data_loader, tgt_data_loader_eval): # save final model save_model(model, params.src_dataset + '-' + params.tgt_dataset + "-dann-final.pt") - return model + return model \ No newline at end of file diff --git a/datasets/mnist.py b/datasets/mnist.py index 30dbe56..667bae7 100644 --- a/datasets/mnist.py +++ b/datasets/mnist.py @@ -12,8 +12,9 @@ def get_mnist(train): # image pre-processing pre_process = transforms.Compose([transforms.ToTensor(), transforms.Normalize( - mean=params.dataset_mean, - std=params.dataset_std)]) + mean=(0.5, 0.5, 0.5), + std=(0.5, 0.5, 0.5) + )]) # datasets and data loader mnist_dataset = datasets.MNIST(root=os.path.join(params.dataset_root,'mnist'), diff --git a/datasets/mnistm.py b/datasets/mnistm.py index 272f079..5fe1fa4 100644 --- a/datasets/mnistm.py +++ b/datasets/mnistm.py @@ -41,11 +41,12 @@ class GetLoader(data.Dataset): def get_mnistm(train): """Get MNISTM datasets loader.""" # image pre-processing - pre_process = transforms.Compose([transforms.Resize(params.digit_image_size), + pre_process = transforms.Compose([transforms.Resize(28), transforms.ToTensor(), transforms.Normalize( - mean=params.dataset_mean, - std=params.dataset_std)]) + mean=(0.5, 0.5, 0.5), + std=(0.5, 0.5, 0.5) + )]) # datasets and data_loader if train: diff --git a/datasets/office.py b/datasets/office.py index 98c5998..e2f3289 100644 --- a/datasets/office.py +++ b/datasets/office.py @@ -13,8 +13,9 @@ def get_office(train, category): pre_process = transforms.Compose([transforms.Resize(params.office_image_size), transforms.ToTensor(), transforms.Normalize( - mean=params.imagenet_dataset_mean, - std=params.imagenet_dataset_mean)]) + mean=(0.485, 0.456, 0.406), + std=(0.229, 0.224, 0.225) + )]) # datasets and data_loader office_dataset = datasets.ImageFolder( @@ -25,6 +26,6 @@ def get_office(train, category): dataset=office_dataset, batch_size=params.batch_size, shuffle=True, - num_workers=8) + num_workers=4) return office_dataloader \ No newline at end of file diff --git a/datasets/officecaltech.py b/datasets/officecaltech.py index 70c7d5c..59eeda4 100644 --- a/datasets/officecaltech.py +++ b/datasets/officecaltech.py @@ -13,8 +13,9 @@ def get_officecaltech(train, category): pre_process = transforms.Compose([transforms.Resize(params.office_image_size), transforms.ToTensor(), transforms.Normalize( - mean=params.imagenet_dataset_mean, - std=params.imagenet_dataset_mean)]) + mean=(0.485, 0.456, 0.406), + std=(0.229, 0.224, 0.225) + )]) # datasets and data_loader officecaltech_dataset = datasets.ImageFolder( @@ -25,6 +26,6 @@ def get_officecaltech(train, category): dataset=officecaltech_dataset, batch_size=params.batch_size, shuffle=True, - num_workers=8) + num_workers=4) return officecaltech_dataloader \ No newline at end of file diff --git a/datasets/svhn.py b/datasets/svhn.py index f32ee3a..d9db05f 100644 --- a/datasets/svhn.py +++ b/datasets/svhn.py @@ -15,8 +15,9 @@ def get_svhn(train): transforms.Resize(params.digit_image_size), transforms.ToTensor(), transforms.Normalize( - mean=params.dataset_mean, - std=params.dataset_std)]) + mean=(0.5, 0.5, 0.5), + std=(0.5, 0.5, 0.5) + )]) # datasets and data loader if train: diff --git a/models/model.py b/models/model.py index 1b6c865..a32af29 100644 --- a/models/model.py +++ b/models/model.py @@ -3,7 +3,6 @@ import torch.nn as nn from .functions import ReverseLayerF from torchvision import models -import params class Classifier(nn.Module): @@ -43,6 +42,45 @@ class Classifier(nn.Module): return class_output +class MNISTmodel(nn.Module): + """ MNIST architecture""" + + def __init__(self): + super(MNISTmodel, self).__init__() + self.restored = False + + self.feature = nn.Sequential( + nn.Conv2d(in_channels=1, out_channels=32, kernel_size=(5, 5)), # 1 28 28, 32 24 24 + nn.ReLU(inplace=True), + nn.MaxPool2d(kernel_size=(2, 2)), # 32 12 12 + nn.Conv2d(in_channels=32, out_channels=48, kernel_size=(5, 5)), # 48 8 8 + nn.ReLU(inplace=True), + nn.MaxPool2d(kernel_size=(2, 2)), # 48 4 4 + ) + + self.classifier = nn.Sequential( + nn.Linear(48*4*4, 100), + nn.ReLU(inplace=True), + nn.Linear(100, 100), + nn.ReLU(inplace=True), + nn.Linear(100, 10), + ) + + self.discriminator = nn.Sequential( + nn.Linear(48*4*4, 100), + nn.ReLU(inplace=True), + nn.Linear(100, 2), + ) + + def forward(self, input_data, alpha): + input_data = input_data.expand(input_data.data.shape[0], 1, 28, 28) + feature = self.feature(input_data) + feature = feature.view(-1, 48 * 4 * 4) + reverse_feature = ReverseLayerF.apply(feature, alpha) + class_output = self.classifier(feature) + domain_output = self.discriminator(reverse_feature) + + return class_output, domain_output class SVHNmodel(nn.Module): """ SVHN architecture""" @@ -90,7 +128,6 @@ class SVHNmodel(nn.Module): return class_output, domain_output - class AlexModel(nn.Module): """ AlexNet pretrained on imagenet for Office dataset""" @@ -119,7 +156,7 @@ class AlexModel(nn.Module): nn.Dropout(0.5), nn.Linear(4096, 256), nn.ReLU(inplace=True), - nn.Linear(256, params.class_num_src), + nn.Linear(256, 31), ) self.discriminator = nn.Sequential( @@ -143,4 +180,4 @@ class AlexModel(nn.Module): domain_output = self.discriminator(reverse_feature) - return class_output, domain_output + return class_output, domain_output \ No newline at end of file diff --git a/params.py b/params.py index 2dd1011..8872e48 100644 --- a/params.py +++ b/params.py @@ -7,16 +7,9 @@ dataset_root = os.path.expanduser(os.path.join('~', 'Datasets')) model_root = os.path.expanduser(os.path.join('~', 'Models', 'pytorch-DANN')) # params for datasets and data loader -dataset_mean_value = 0.5 -dataset_std_value = 0.5 -dataset_mean = (dataset_mean_value, dataset_mean_value, dataset_mean_value) -dataset_std = (dataset_std_value, dataset_std_value, dataset_std_value) - -imagenet_dataset_mean = (0.485, 0.456, 0.406) -imagenet_dataset_std = (0.229, 0.224, 0.225) batch_size = 64 -digit_image_size = 28 + office_image_size = 227 # params for source dataset