-
Notifications
You must be signed in to change notification settings - Fork 5
/
configs.py
195 lines (155 loc) · 5.53 KB
/
configs.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
import os
import random
import numpy as np
import torch
from torch.optim import Adam, lr_scheduler
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms
from utils import datasets
default_workers = os.cpu_count()
def nclass(config):
r = {
'imagenet100': 100,
'cifar10': 10,
'nuswide': 21,
}[config['dataset']]
return r
def R(config):
r = {
'imagenet100': 1000,
'cifar10': 59000, # mAP@all
'cifar10_2': 50000,
'nuswide': 5000,
}[config['dataset'] + {2: '_2'}.get(config['dataset_kwargs']['evaluation_protocol'], '')]
return r
def scheduler(config, optimizer):
s_type = config['scheduler']
kwargs = config['scheduler_kwargs']
if s_type == 'step':
return lr_scheduler.StepLR(optimizer,
kwargs['step_size'],
kwargs['gamma'])
elif s_type == 'mstep':
return lr_scheduler.MultiStepLR(optimizer,
[int(float(m) * int(config['epochs'])) for m in
kwargs['milestones'].split(',')],
kwargs['gamma'])
else:
raise Exception('Scheduler not supported yet: ' + s_type)
def compose_transform(mode='train', resize=0, crop=0, norm=0,
augmentations=None):
"""
:param mode:
:param resize:
:param crop:
:param norm:
:param augmentations:
:return:
if train:
Resize (optional, usually done in Augmentations)
Augmentations
ToTensor
Normalize
if test:
Resize
CenterCrop
ToTensor
Normalize
"""
# norm = 0, 0 to 1
# norm = 1, -1 to 1
# norm = 2, standardization
mean, std = {
0: [[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]],
1: [[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]],
2: [[0.485, 0.456, 0.406], [0.229, 0.224, 0.225]]
}[norm]
compose = []
if resize != 0:
compose.append(transforms.Resize(resize))
if mode == 'train' and augmentations is not None:
compose += augmentations
if mode == 'test' and crop != 0 and resize != crop:
compose.append(transforms.CenterCrop(crop))
compose.append(transforms.ToTensor())
if norm != 0:
compose.append(transforms.Normalize(mean, std))
return transforms.Compose(compose)
def dataset(config, filename, transform_mode):
dataset_name = config['dataset']
nclass = config['arch_kwargs']['nclass']
resize = config['dataset_kwargs'].get('resize', 0)
crop = config['dataset_kwargs'].get('crop', 0)
norm = config['dataset_kwargs'].get('norm', 2)
reset = config['dataset_kwargs'].get('reset', False)
if dataset_name in ['imagenet100', 'nuswide']:
# resizec = 0 if resize == 256 else resize
# cropc = 224 if crop == 0 else crop
if transform_mode == 'train':
transform = compose_transform('train', 0, crop, 2, {
'imagenet100': [
transforms.RandomResizedCrop(crop),
# transforms.Resize(resize),
# transforms.RandomCrop(crop),
transforms.RandomHorizontalFlip()
],
'nuswide': [
transforms.Resize(resize),
transforms.RandomCrop(crop),
transforms.RandomHorizontalFlip()
]
}[dataset_name])
else:
transform = compose_transform('test', resize, crop, 2)
datafunc = {
'imagenet100': datasets.imagenet100,
'nuswide': datasets.nuswide,
}[dataset_name]
d = datafunc(transform=transform, filename=filename)
else: # cifar10/ cifar100
resizec = 0 if resize == 32 else resize
cropc = 0 if crop == 32 else crop
if transform_mode == 'train':
transform = compose_transform('train', resizec, 0, norm, [
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=0.05, contrast=0.05),
])
else:
transform = compose_transform('test', resizec, cropc, norm)
ep = config['dataset_kwargs'].get('evaluation_protocol', 1)
d = datasets.cifar(nclass, transform=transform, filename=filename, evaluation_protocol=ep, reset=reset)
return d
def dataloader(d, bs=256, shuffle=True, workers=-1, drop_last=True):
if workers < 0:
workers = default_workers
l = DataLoader(d,
bs,
shuffle,
drop_last=drop_last,
num_workers=workers)
return l
def seeding(seed):
seed = int(seed)
if seed != -1:
os.environ['PYTHONHASHSEED'] = str(seed)
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.deterministic = True
def tensor_to_dataset(tensor, transform=None):
class TransformTensorDataset(Dataset):
def __init__(self, tensor, ts=None):
super(TransformTensorDataset, self).__init__()
self.tensor = tensor
self.ts = ts
def __getitem__(self, index):
if self.ts is not None:
return self.ts(self.tensor[index])
return self.tensor[index]
def __len__(self):
return len(self.tensor)
ttd = TransformTensorDataset(tensor, transform)
return ttd