diff --git a/README.md b/README.md index bde85d7..d2f3705 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ A PyTorch implementation for [Adversarial Discriminative Domain Adaptation](https://arxiv.org/abs/1702.05464). ## Environment -- Python 3.6 -- PyTorch 0.2.0 +- Python >= 3.6 (Tested on Python 3.8) +- PyTorch >= 1.0.0 (Tested on PyTorch 1.11.0) ## Usage diff --git a/core/adapt.py b/core/adapt.py index 63d11c8..bc4e26d 100644 --- a/core/adapt.py +++ b/core/adapt.py @@ -107,9 +107,9 @@ def train_tgt(src_encoder, tgt_encoder, critic, params.num_epochs, step + 1, len_data_loader, - loss_critic.data[0], - loss_tgt.data[0], - acc.data[0])) + loss_critic.item(), + loss_tgt.item(), + acc.item())) ############################# # 2.4 save model parameters # diff --git a/core/pretrain.py b/core/pretrain.py index 4038c0f..40f6c37 100644 --- a/core/pretrain.py +++ b/core/pretrain.py @@ -52,7 +52,7 @@ def train_src(encoder, classifier, data_loader): params.num_epochs_pre, step + 1, len(data_loader), - loss.data[0])) + loss.item())) # eval model on test set if ((epoch + 1) % params.eval_step_pre == 0): @@ -78,8 +78,8 @@ def eval_src(encoder, classifier, data_loader): classifier.eval() # init loss and accuracy - loss = 0 - acc = 0 + loss = 0. + acc = 0. # set loss function criterion = nn.CrossEntropyLoss() @@ -90,7 +90,7 @@ def eval_src(encoder, classifier, data_loader): labels = make_variable(labels) preds = classifier(encoder(images)) - loss += criterion(preds, labels).data[0] + loss += criterion(preds, labels).item() pred_cls = preds.data.max(1)[1] acc += pred_cls.eq(labels.data).cpu().sum() diff --git a/core/test.py b/core/test.py index d414961..8c99960 100644 --- a/core/test.py +++ b/core/test.py @@ -13,8 +13,8 @@ def eval_tgt(encoder, classifier, data_loader): classifier.eval() # init loss and accuracy - loss = 0 - acc = 0 + loss = 0. + acc = 0. # set loss function criterion = nn.CrossEntropyLoss() @@ -25,7 +25,7 @@ def eval_tgt(encoder, classifier, data_loader): labels = make_variable(labels).squeeze_() preds = classifier(encoder(images)) - loss += criterion(preds, labels).data[0] + loss += criterion(preds, labels).item() pred_cls = preds.data.max(1)[1] acc += pred_cls.eq(labels.data).cpu().sum() diff --git a/datasets/usps.py b/datasets/usps.py index 62c4629..f6d906e 100644 --- a/datasets/usps.py +++ b/datasets/usps.py @@ -57,7 +57,7 @@ def __init__(self, root, train=True, transform=None, download=False): np.random.shuffle(indices) self.train_data = self.train_data[indices[0:self.dataset_size], ::] self.train_labels = self.train_labels[indices[0:self.dataset_size]] - self.train_data *= 255.0 + self.train_data = self.train_data.transpose( (0, 2, 3, 1)) # convert to HWC diff --git a/models/discriminator.py b/models/discriminator.py index 3e4a782..3740765 100644 --- a/models/discriminator.py +++ b/models/discriminator.py @@ -18,7 +18,6 @@ def __init__(self, input_dims, hidden_dims, output_dims): nn.Linear(hidden_dims, hidden_dims), nn.ReLU(), nn.Linear(hidden_dims, output_dims), - nn.LogSoftmax() ) def forward(self, input): diff --git a/params.py b/params.py index 707967d..14374b5 100644 --- a/params.py +++ b/params.py @@ -4,8 +4,8 @@ data_root = "data" 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) +dataset_mean = dataset_mean_value +dataset_std = dataset_std_value batch_size = 50 image_size = 64 diff --git a/utils.py b/utils.py index 7951c6d..944e4d1 100644 --- a/utils.py +++ b/utils.py @@ -15,7 +15,7 @@ def make_variable(tensor, volatile=False): """Convert Tensor to Variable.""" if torch.cuda.is_available(): tensor = tensor.cuda() - return Variable(tensor, volatile=volatile) + return tensor def make_cuda(tensor):