-
Notifications
You must be signed in to change notification settings - Fork 27
/
train.py
128 lines (94 loc) · 5.11 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import torch
import numpy as np
import utils
import torch.optim as optim
import torch.nn as nn
import test
import mnist
import mnistm
from utils import save_model
from utils import visualize
from utils import set_model_mode
import params
# Source : 0, Target :1
source_test_loader = mnist.mnist_test_loader
target_test_loader = mnistm.mnistm_test_loader
def source_only(encoder, classifier, source_train_loader, target_train_loader):
print("Training with only the source dataset")
classifier_criterion = nn.CrossEntropyLoss().cuda()
optimizer = optim.SGD(
list(encoder.parameters()) +
list(classifier.parameters()),
lr=0.01, momentum=0.9)
for epoch in range(params.epochs):
print(f"Epoch: {epoch}")
set_model_mode('train', [encoder, classifier])
start_steps = epoch * len(source_train_loader)
total_steps = params.epochs * len(target_train_loader)
for batch_idx, (source_data, target_data) in enumerate(zip(source_train_loader, target_train_loader)):
source_image, source_label = source_data
p = float(batch_idx + start_steps) / total_steps
source_image = torch.cat((source_image, source_image, source_image), 1) # MNIST convert to 3 channel
source_image, source_label = source_image.cuda(), source_label.cuda() # 32
optimizer = utils.optimizer_scheduler(optimizer=optimizer, p=p)
optimizer.zero_grad()
source_feature = encoder(source_image)
# Classification loss
class_pred = classifier(source_feature)
class_loss = classifier_criterion(class_pred, source_label)
class_loss.backward()
optimizer.step()
if (batch_idx + 1) % 100 == 0:
total_processed = batch_idx * len(source_image)
total_dataset = len(source_train_loader.dataset)
percentage_completed = 100. * batch_idx / len(source_train_loader)
print(f'[{total_processed}/{total_dataset} ({percentage_completed:.0f}%)]\tClassification Loss: {class_loss.item():.4f}')
test.tester(encoder, classifier, None, source_test_loader, target_test_loader, training_mode='Source_only')
save_model(encoder, classifier, None, 'Source-only')
visualize(encoder, 'Source-only')
def dann(encoder, classifier, discriminator, source_train_loader, target_train_loader):
print("Training with the DANN adaptation method")
classifier_criterion = nn.CrossEntropyLoss().cuda()
discriminator_criterion = nn.CrossEntropyLoss().cuda()
optimizer = optim.SGD(
list(encoder.parameters()) +
list(classifier.parameters()) +
list(discriminator.parameters()),
lr=0.01,
momentum=0.9)
for epoch in range(params.epochs):
print(f"Epoch: {epoch}")
set_model_mode('train', [encoder, classifier, discriminator])
start_steps = epoch * len(source_train_loader)
total_steps = params.epochs * len(target_train_loader)
for batch_idx, (source_data, target_data) in enumerate(zip(source_train_loader, target_train_loader)):
source_image, source_label = source_data
target_image, target_label = target_data
p = float(batch_idx + start_steps) / total_steps
alpha = 2. / (1. + np.exp(-10 * p)) - 1
source_image = torch.cat((source_image, source_image, source_image), 1)
source_image, source_label = source_image.cuda(), source_label.cuda()
target_image, target_label = target_image.cuda(), target_label.cuda()
combined_image = torch.cat((source_image, target_image), 0)
optimizer = utils.optimizer_scheduler(optimizer=optimizer, p=p)
optimizer.zero_grad()
combined_feature = encoder(combined_image)
source_feature = encoder(source_image)
# 1.Classification loss
class_pred = classifier(source_feature)
class_loss = classifier_criterion(class_pred, source_label)
# 2. Domain loss
domain_pred = discriminator(combined_feature, alpha)
domain_source_labels = torch.zeros(source_label.shape[0]).type(torch.LongTensor)
domain_target_labels = torch.ones(target_label.shape[0]).type(torch.LongTensor)
domain_combined_label = torch.cat((domain_source_labels, domain_target_labels), 0).cuda()
domain_loss = discriminator_criterion(domain_pred, domain_combined_label)
total_loss = class_loss + domain_loss
total_loss.backward()
optimizer.step()
if (batch_idx + 1) % 100 == 0:
print('[{}/{} ({:.0f}%)]\tTotal Loss: {:.4f}\tClassification Loss: {:.4f}\tDomain Loss: {:.4f}'.format(
batch_idx * len(target_image), len(target_train_loader.dataset), 100. * batch_idx / len(target_train_loader), total_loss.item(), class_loss.item(), domain_loss.item()))
test.tester(encoder, classifier, discriminator, source_test_loader, target_test_loader, training_mode='DANN')
save_model(encoder, classifier, discriminator, 'DANN')
visualize(encoder, 'DANN')