Hi @ptrblck . Thanks for your reply. I am trying to run this code. This is code for unsupervised domain adaptation using GRL.
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class DACNN(nn.Module):
def __init__(self):
super().__init__()
self.feature_extractor = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=5),
nn.BatchNorm2d(64), nn.MaxPool2d(2),
nn.ReLU(True),
nn.Conv2d(64, 50, kernel_size=5),
nn.BatchNorm2d(50), nn.Dropout2d(), nn.MaxPool2d(2),
nn.ReLU(True),
)
self.class_classifier = nn.Sequential(
nn.Linear(50*72*72, 100), nn.BatchNorm1d(100), nn.Dropout2d(),
nn.ReLU(True),
nn.Linear(100, 100), nn.BatchNorm1d(100),
nn.ReLU(True),
nn.Linear(100, 2),
nn.LogSoftmax(dim=1),
)
self.domain_classifier = nn.Sequential(
nn.Linear(50*72*72, 100), nn.BatchNorm1d(100),
nn.ReLU(True),
nn.Linear(100, 2),
nn.LogSoftmax(dim=1),
)
def forward(self, x, grl_lambda=1.0):
x = x.expand(x.data.shape[0], 3, 300, 300)
features = self.feature_extractor(x)
features = features.view(x.size(0), -1)
reverse_features = GradientReversalFn.apply(features, grl_lambda)
class_pred = self.class_classifier(features)
domain_pred = self.domain_classifier(reverse_features)
return class_pred, domain_pred
model = DACNN()
model.to(device)
And I am sending inputs to GPU using this code:
for batch_idx in range(max_batches):
optimizer.zero_grad()
p = float(batch_idx+epoch_idx * max_batches) / (n_epochs * max_batches)
grl_lambda = 2. / (1. + np.exp(-10 * p)) - 1
X_s, y_s = next(dl_source_iter)
X_s, y_s = X_s.to(device), y_s.to(device)
y_s_domain = torch.zeros(BATCH_SIZE, dtype=torch.long)
X_t, _ = next(dl_target_iter)
X_t = X_t.to(device)
y_t_domain = torch.ones(len(X_t), dtype=torch.long)