-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP3_DANN_SVHN_training.py
167 lines (137 loc) · 5.17 KB
/
P3_DANN_SVHN_training.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from itertools import chain
from pathlib import Path
import numpy as np
import torch
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard.writer import SummaryWriter
from torchvision import transforms
from tqdm import tqdm
from digit_dataloader import digit_dataset
from P3_SVHN_model import DomainClassifier, FeatureExtractor, LabelPredictor
# https://github.com/NaJaeMin92/pytorch_DANN
def rm_tree(pth: Path):
if pth.is_dir():
for child in pth.iterdir():
if child.is_file():
child.unlink()
else:
rm_tree(child)
pth.rmdir()
def cycle(iterable):
while True:
for x in iterable:
yield x
source_train_set = digit_dataset(
# [0.4631, 0.4666, 0.4195], [0.1979, 0.1845, 0.2083]
root='hw2_data/digits/mnistm/data',
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.4631, 0.4666, 0.4195],
[0.1979, 0.1845, 0.2083])
]),
label_csv='hw2_data/digits/mnistm/train.csv'
)
target_train_set = digit_dataset(
# [0.2570, 0.2570, 0.2570], [0.3372, 0.3372, 0.3372]
root='hw2_data/digits/svhn/data',
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.4413, 0.4458, 0.4715],
[0.1169, 0.1206, 0.1042])
]),
label_csv='hw2_data/digits/svhn/train.csv'
)
target_val_set = digit_dataset(
# [0.2570, 0.2570, 0.2570], [0.3372, 0.3372, 0.3372]
root='hw2_data/digits/svhn/data',
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.4413, 0.4458, 0.4715],
[0.1169, 0.1206, 0.1042])
]),
label_csv='hw2_data/digits/svhn/val.csv'
)
batch_size = 1024
source_train_loader = DataLoader(
source_train_set, batch_size, shuffle=True, num_workers=6)
target_train_loader = DataLoader(
target_train_set, batch_size, shuffle=True, num_workers=6)
target_val_loader = DataLoader(
target_val_set, 2 * batch_size, shuffle=False, num_workers=6)
target_train_loader = iter(cycle(target_train_loader))
device = 'cuda' if torch.cuda.is_available() else 'cpu'
ckpt_path = Path('./P3_SVHN_ckpt')
tb_path = Path('./P3_SVHN_tb')
rm_tree(ckpt_path)
rm_tree(tb_path)
ckpt_path.mkdir(exist_ok=True)
tb_path.mkdir(exist_ok=True)
writer = SummaryWriter(tb_path)
num_epochs = 200
lr = 0.003
gamma = 10
F = FeatureExtractor().to(device)
L = LabelPredictor().to(device)
D = DomainClassifier().to(device)
label_loss_fn = nn.CrossEntropyLoss()
domain_loss_fn = nn.BCEWithLogitsLoss()
optim = torch.optim.SGD(
chain(F.parameters(), L.parameters(), D.parameters()), lr=lr, momentum=0.9)
current_step = 0
total_steps = num_epochs * len(source_train_loader)
best_target_acc = 0.3
for epoch in range(num_epochs):
for (src_x, src_y), (tgt_x, _) in tqdm(zip(source_train_loader, target_train_loader), total=len(source_train_loader)):
src_x = src_x.to(device, non_blocking=True)
src_y = src_y.to(device, non_blocking=True)
tgt_x = tgt_x.to(device, non_blocking=True)
# scheduling
p = current_step / total_steps
lambda_ = 2.0 / (1.0 + np.exp(-gamma * p)) - 1
optim.param_groups[0]['lr'] = lr / (1.0 + gamma * p) ** 0.75
# feature extraction
source_feature = F(src_x)
target_feature = F(tgt_x)
# label classification loss
source_logits = L(source_feature)
label_loss = label_loss_fn(source_logits, src_y)
# domain discriminator loss
# source=1
source_domain_logit = D(source_feature, lambda_).squeeze()
source_domain_loss = domain_loss_fn(source_domain_logit, torch.zeros(
src_x.shape[0], dtype=torch.float, device=device))
# target=1
target_domain_logit = D(target_feature, lambda_).squeeze()
target_domain_loss = domain_loss_fn(target_domain_logit, torch.ones(
tgt_x.shape[0], dtype=torch.float, device=device))
domain_loss = source_domain_loss + target_domain_loss
writer.add_scalars('training', {
'label_loss': label_loss, 'domain_loss': domain_loss}, global_step=current_step)
loss = label_loss + domain_loss
loss.backward()
optim.step()
optim.zero_grad()
current_step += 1
# validation
for model in [F, L, D]:
model.eval()
va_acc = 0
for tgt_x, tgt_y in tqdm(target_val_loader):
tgt_x = tgt_x.to(device)
tgt_y = tgt_y.cpu().numpy()
with torch.no_grad():
logits = L(F(tgt_x))
pred = logits.argmax(-1).cpu().numpy()
va_acc += np.mean((pred == tgt_y).astype(int))
for model in [F, L, D]:
model.train()
va_acc /= len(target_val_loader)
writer.add_scalar('accuracy/validation', va_acc, global_step=current_step)
print(f"epoch: {epoch}, va_acc: {va_acc}")
if va_acc >= best_target_acc:
best_target_acc = va_acc
torch.save(F.state_dict(), ckpt_path / f'best_F.pth')
torch.save(L.state_dict(), ckpt_path / f'best_L.pth')
torch.save(D.state_dict(), ckpt_path / f'best_D.pth')
print(f"[new model saved]")