-
Notifications
You must be signed in to change notification settings - Fork 557
/
utils.py
264 lines (225 loc) · 9.48 KB
/
utils.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
"""
@author: Junguang Jiang, Baixu Chen
@contact: [email protected], [email protected]
"""
import sys
import os.path as osp
import time
from PIL import Image
import timm
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as T
from timm.data.auto_augment import auto_augment_transform, rand_augment_transform
sys.path.append('../../..')
import tllib.vision.datasets as datasets
import tllib.vision.models as models
from tllib.vision.transforms import ResizeImage
from tllib.utils.metric import accuracy, ConfusionMatrix
from tllib.utils.meter import AverageMeter, ProgressMeter
from tllib.vision.datasets.imagelist import MultipleDomainsDataset
def get_model_names():
return sorted(
name for name in models.__dict__
if name.islower() and not name.startswith("__")
and callable(models.__dict__[name])
) + timm.list_models()
def get_model(model_name, pretrain=True):
if model_name in models.__dict__:
# load models from tllib.vision.models
backbone = models.__dict__[model_name](pretrained=pretrain)
else:
# load models from pytorch-image-models
backbone = timm.create_model(model_name, pretrained=pretrain)
try:
backbone.out_features = backbone.get_classifier().in_features
backbone.reset_classifier(0, '')
except:
backbone.out_features = backbone.head.in_features
backbone.head = nn.Identity()
return backbone
def get_dataset_names():
return sorted(
name for name in datasets.__dict__
if not name.startswith("__") and callable(datasets.__dict__[name])
) + ['Digits']
def get_dataset(dataset_name, root, source, target, train_source_transform, val_transform, train_target_transform=None):
if train_target_transform is None:
train_target_transform = train_source_transform
if dataset_name == "Digits":
train_source_dataset = datasets.__dict__[source[0]](osp.join(root, source[0]), download=True,
transform=train_source_transform)
train_target_dataset = datasets.__dict__[target[0]](osp.join(root, target[0]), download=True,
transform=train_target_transform)
val_dataset = test_dataset = datasets.__dict__[target[0]](osp.join(root, target[0]), split='test',
download=True, transform=val_transform)
class_names = datasets.MNIST.get_classes()
num_classes = len(class_names)
elif dataset_name in datasets.__dict__:
# load datasets from tllib.vision.datasets
dataset = datasets.__dict__[dataset_name]
def concat_dataset(tasks, start_idx, **kwargs):
# return ConcatDataset([dataset(task=task, **kwargs) for task in tasks])
return MultipleDomainsDataset([dataset(task=task, **kwargs) for task in tasks], tasks,
domain_ids=list(range(start_idx, start_idx + len(tasks))))
train_source_dataset = concat_dataset(root=root, tasks=source, download=True, transform=train_source_transform,
start_idx=0)
train_target_dataset = concat_dataset(root=root, tasks=target, download=True, transform=train_target_transform,
start_idx=len(source))
val_dataset = concat_dataset(root=root, tasks=target, download=True, transform=val_transform,
start_idx=len(source))
if dataset_name == 'DomainNet':
test_dataset = concat_dataset(root=root, tasks=target, split='test', download=True, transform=val_transform,
start_idx=len(source))
else:
test_dataset = val_dataset
class_names = train_source_dataset.datasets[0].classes
num_classes = len(class_names)
else:
raise NotImplementedError(dataset_name)
return train_source_dataset, train_target_dataset, val_dataset, test_dataset, num_classes, class_names
def validate(val_loader, model, args, device) -> float:
batch_time = AverageMeter('Time', ':6.3f')
losses = AverageMeter('Loss', ':.4e')
top1 = AverageMeter('Acc@1', ':6.2f')
progress = ProgressMeter(
len(val_loader),
[batch_time, losses, top1],
prefix='Test: ')
# switch to evaluate mode
model.eval()
if args.per_class_eval:
confmat = ConfusionMatrix(len(args.class_names))
else:
confmat = None
with torch.no_grad():
end = time.time()
for i, data in enumerate(val_loader):
images, target = data[:2]
images = images.to(device)
target = target.to(device)
# compute output
output = model(images)
loss = F.cross_entropy(output, target)
# measure accuracy and record loss
acc1, = accuracy(output, target, topk=(1,))
if confmat:
confmat.update(target, output.argmax(1))
losses.update(loss.item(), images.size(0))
top1.update(acc1.item(), images.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
progress.display(i)
print(' * Acc@1 {top1.avg:.3f}'.format(top1=top1))
if confmat:
print(confmat.format(args.class_names))
return top1.avg
def get_train_transform(resizing='default', scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), random_horizontal_flip=True,
random_color_jitter=False, resize_size=224, norm_mean=(0.485, 0.456, 0.406),
norm_std=(0.229, 0.224, 0.225), auto_augment=None):
"""
resizing mode:
- default: resize the image to 256 and take a random resized crop of size 224;
- cen.crop: resize the image to 256 and take the center crop of size 224;
- res: resize the image to 224;
"""
transformed_img_size = 224
if resizing == 'default':
transform = T.Compose([
ResizeImage(256),
T.RandomResizedCrop(224, scale=scale, ratio=ratio)
])
elif resizing == 'cen.crop':
transform = T.Compose([
ResizeImage(256),
T.CenterCrop(224)
])
elif resizing == 'ran.crop':
transform = T.Compose([
ResizeImage(256),
T.RandomCrop(224)
])
elif resizing == 'res.':
transform = ResizeImage(resize_size)
transformed_img_size = resize_size
else:
raise NotImplementedError(resizing)
transforms = [transform]
if random_horizontal_flip:
transforms.append(T.RandomHorizontalFlip())
if auto_augment:
aa_params = dict(
translate_const=int(transformed_img_size * 0.45),
img_mean=tuple([min(255, round(255 * x)) for x in norm_mean]),
interpolation=Image.BILINEAR
)
if auto_augment.startswith('rand'):
transforms.append(rand_augment_transform(auto_augment, aa_params))
else:
transforms.append(auto_augment_transform(auto_augment, aa_params))
elif random_color_jitter:
transforms.append(T.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5))
transforms.extend([
T.ToTensor(),
T.Normalize(mean=norm_mean, std=norm_std)
])
return T.Compose(transforms)
def get_val_transform(resizing='default', resize_size=224,
norm_mean=(0.485, 0.456, 0.406), norm_std=(0.229, 0.224, 0.225)):
"""
resizing mode:
- default: resize the image to 256 and take the center crop of size 224;
– res.: resize the image to 224
"""
if resizing == 'default':
transform = T.Compose([
ResizeImage(256),
T.CenterCrop(224),
])
elif resizing == 'res.':
transform = ResizeImage(resize_size)
else:
raise NotImplementedError(resizing)
return T.Compose([
transform,
T.ToTensor(),
T.Normalize(mean=norm_mean, std=norm_std)
])
def empirical_risk_minimization(train_source_iter, model, optimizer, lr_scheduler, epoch, args, device):
batch_time = AverageMeter('Time', ':3.1f')
data_time = AverageMeter('Data', ':3.1f')
losses = AverageMeter('Loss', ':3.2f')
cls_accs = AverageMeter('Cls Acc', ':3.1f')
progress = ProgressMeter(
args.iters_per_epoch,
[batch_time, data_time, losses, cls_accs],
prefix="Epoch: [{}]".format(epoch))
# switch to train mode
model.train()
end = time.time()
for i in range(args.iters_per_epoch):
x_s, labels_s = next(train_source_iter)[:2]
x_s = x_s.to(device)
labels_s = labels_s.to(device)
# measure data loading time
data_time.update(time.time() - end)
# compute output
y_s, f_s = model(x_s)
cls_loss = F.cross_entropy(y_s, labels_s)
loss = cls_loss
cls_acc = accuracy(y_s, labels_s)[0]
losses.update(loss.item(), x_s.size(0))
cls_accs.update(cls_acc.item(), x_s.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
lr_scheduler.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
progress.display(i)