-
Notifications
You must be signed in to change notification settings - Fork 0
/
images_multi_source_iter.py
280 lines (253 loc) · 8.88 KB
/
images_multi_source_iter.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import os
import torch
import pickle
import argparse
import torchvision
from tqdm.auto import tqdm
from sklearn.metrics import accuracy_score
from da_baselines.data import all_domains
from da_baselines.data import ImagesDataset
from da_baselines.architectures import get_vit
from da_baselines.architectures import get_resnet
parser = argparse.ArgumentParser(
description='Train baseline on Office-Like benchmarks')
parser.add_argument('--root',
type=str,
help='Root for dataset')
parser.add_argument('--benchmark',
type=str,
help='Name of benchmark')
parser.add_argument('--backbone',
type=str,
help='Type of backbone (resnet or vit)')
parser.add_argument('--size',
type=int,
help='Number of layers')
parser.add_argument('--vit_type',
type=str,
help='Type of ViT')
parser.add_argument('--batch_size',
type=int,
default=64)
parser.add_argument('--learning_rate',
type=float,
default=5e-5)
parser.add_argument('--n_iter',
type=int,
default=15)
parser.add_argument('--eval_every',
type=int,
default=1000)
parser.add_argument('--tgt',
type=str,
default='amazon',
help="Target domain")
parser.add_argument('--extract_features',
type=str,
default='True',
help="Whether or not extract features")
parser.add_argument('--out_path',
type=str,
default='./features',
help="path to save features")
parser.add_argument('--optimizer_name',
type=str,
default="adam",
help="optimizer name (either SGD or Adam)")
args = parser.parse_args()
n_iter = args.n_iter
batch_size = args.batch_size
learning_rate = args.learning_rate
train_transforms = torchvision.transforms.Compose([
torchvision.transforms.RandomResizedCrop(224, scale=(0.75, 1)),
torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.ToTensor()
])
test_transforms = torchvision.transforms.Compose([
torchvision.transforms.Resize((224, 224)),
torchvision.transforms.ToTensor()
])
domains = all_domains[args.benchmark]
sources = [d for d in domains if d != args.tgt]
train_dataset = ImagesDataset(
root=args.root,
dataset_name=args.benchmark,
domains=sources,
transform=train_transforms,
train=True,
test=True,
multi_source=False
)
n_classes = len(train_dataset.name2cat)
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=True)
train_iter = iter(train_loader)
test_dataset = ImagesDataset(
root=args.root,
dataset_name=args.benchmark,
domains=[args.tgt,],
transform=test_transforms,
train=True,
test=True,
multi_source=False
)
test_loader = torch.utils.data.DataLoader(
test_dataset, batch_size=batch_size, shuffle=False)
if args.backbone.lower() == 'vit':
model = get_vit(
n_layers=args.size,
name=args.vit_type,
n_classes=n_classes,
return_T=False).to('cuda')
elif args.backbone.lower() == 'resnet':
model = get_resnet(
resnet_size=args.size,
n_classes=n_classes,
return_T=False
).to('cuda')
else:
raise ValueError("Expected backbone to be either vit or "
f"resnet, but got {args.backbone.lower()}")
if args.optimizer_name.lower() == 'adam':
optimizer = torch.optim.Adam(model.parameters(),
lr=learning_rate,
weight_decay=5e-4)
else:
optimizer = torch.optim.SGD(model.parameters(),
lr=learning_rate,
momentum=0.9,
weight_decay=5e-4)
criterion = torch.nn.CrossEntropyLoss()
pbar = tqdm(range(n_iter))
for it in pbar:
try:
x, y = next(train_iter)
except StopIteration:
train_iter = iter(train_loader)
x, y = next(train_iter)
optimizer.zero_grad()
yhat = model(x.to('cuda'))
loss = criterion(yhat, target=y.to('cuda'))
loss.backward()
optimizer.step()
pbar.set_description(f"Iteration {it} complete. Loss: {loss.item()}")
try:
with open(f"./logs/{args.benchmark}/{args.tgt}_train.csv", 'a') as f:
f.write(f"{it},{loss.item()}\n")
except FileNotFoundError:
print(f"Error, path './logs/{args.benchmark}/{args.tgt}"
"_train.csv' does not exist")
print("Continuing...")
if (it + 1) % args.eval_every == 0:
model.eval()
with torch.no_grad():
predictions = []
ground_truth = []
for x, y in tqdm(test_loader):
predictions.append(model(x.to('cuda')).cpu())
ground_truth.append(y)
predictions = torch.cat(predictions, dim=0)
ground_truth = torch.cat(ground_truth, dim=0)
ts_loss = criterion(predictions, target=ground_truth)
ts_acc = accuracy_score(predictions.argmax(dim=1), ground_truth)
print(f"Evaluation (it {it})\n")
print(f"Loss: {ts_loss}, Accuracy: {ts_acc}\n")
try:
with open(
f"./logs/{args.benchmark}/{args.tgt}_test.csv", 'a') as f:
f.write(f"{it},{ts_loss},{ts_acc}\n")
except FileNotFoundError:
print(f"Error, path './logs/{args.benchmark}/{args.tgt}_test.csv'"
" does not exist")
print("Continuing...")
model.train()
if args.extract_features.lower() == 'true':
print("Extracting features")
model.eval()
dataset = {'sources': {}, 'target': None}
for domain in sources:
tr_loader = torch.utils.data.DataLoader(
ImagesDataset(
root=args.root,
dataset_name=args.benchmark,
domains=[domain, ],
transform=test_transforms,
train=True,
test=False,
multi_source=False
),
batch_size=batch_size,
shuffle=False)
ts_loader = torch.utils.data.DataLoader(
ImagesDataset(
root=args.root,
dataset_name=args.benchmark,
domains=[domain, ],
transform=test_transforms,
train=False,
test=True,
multi_source=False
),
batch_size=batch_size,
shuffle=False)
_xtr, _xts, _ytr, _yts = [], [], [], []
for x, y in tqdm(tr_loader, total=len(tr_loader)):
with torch.no_grad():
_xtr.append(
model.encode(x.to('cuda')).cpu())
_ytr.append(y)
_xtr = torch.cat(_xtr, dim=0)
_ytr = torch.cat(_ytr, dim=0)
for x, y in tqdm(ts_loader, total=len(ts_loader)):
with torch.no_grad():
_xts.append(
model.encode(x.to('cuda')).cpu())
_yts.append(y)
_xts = torch.cat(_xts, dim=0)
_yts = torch.cat(_yts, dim=0)
dataset['sources'][domain] = (_xtr, _ytr, _xts, _yts)
tr_loader = torch.utils.data.DataLoader(
ImagesDataset(
root=args.root,
dataset_name=args.benchmark,
domains=[args.tgt, ],
transform=test_transforms,
train=True,
test=False,
multi_source=False
),
batch_size=batch_size,
shuffle=False)
ts_loader = torch.utils.data.DataLoader(
ImagesDataset(
root=args.root,
dataset_name=args.benchmark,
domains=[args.tgt, ],
transform=test_transforms,
train=False,
test=True,
multi_source=False
),
batch_size=batch_size,
shuffle=False)
_xtr, _xts, _ytr, _yts = [], [], [], []
for x, y in tqdm(tr_loader, total=len(tr_loader)):
with torch.no_grad():
_xtr.append(
model.encode(x.to('cuda')).cpu())
_ytr.append(y)
_xtr = torch.cat(_xtr, dim=0)
_ytr = torch.cat(_ytr, dim=0)
for x, y in tqdm(ts_loader, total=len(ts_loader)):
with torch.no_grad():
_xts.append(
model.encode(x.to('cuda')).cpu())
_yts.append(y)
_xts = torch.cat(_xts, dim=0)
_yts = torch.cat(_yts, dim=0)
dataset['target'] = (_xtr, _ytr, _xts, _yts)
with open(
os.path.join(args.out_path, f"{args.benchmark}_"
f"{args.tgt}_{args.backbone}_{args.size}.pkl"),
"wb") as f:
f.write(pickle.dumps(dataset))