-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
293 lines (240 loc) · 9.25 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.autograd import Variable
from conf import settings
from dataset.dataset import CUB_200_2011_Train, CUB_200_2011_Test
from torch.utils.data.distributed import DistributedSampler
def get_network(args):
if args.net == 'vgg16':
from models.vgg import vgg16
model_ft = vgg16(args.num_classes,export_onnx=args.export_onnx)
elif args.net == 'alexnet':
from models.alexnet import alexnet
model_ft = alexnet(num_classes=args.num_classes,export_onnx=args.export_onnx)
elif args.net == 'mobilenet':
from models.mobilenet import mobilenet_v2
model_ft = mobilenet_v2(pretrained=True,export_onnx=args.export_onnx)
elif args.net == 'vgg19':
from models.vgg import vgg19
model_ft = vgg19(args.num_classes,export_onnx=args.export_onnx)
else:
if args.net == 'googlenet':
from models.googlenet import googlenet
model_ft = googlenet(pretrained=True)
elif args.net == 'inception':
from models.inception import inception_v3
model_ft = inception_v3(args,pretrained=True,export_onnx=args.export_onnx)
elif args.net == 'resnet18':
from models.resnet import resnet18
model_ft = resnet18(pretrained=True,export_onnx=args.export_onnx)
elif args.net == 'resnet34':
from models.resnet import resnet34
model_ft = resnet34(pretrained=True,export_onnx=args.export_onnx)
elif args.net == 'resnet101':
from models.resnet import resnet101
model_ft = resnet101(pretrained=True,export_onnx=args.export_onnx)
elif args.net == 'resnet50':
from models.resnet import resnet50
model_ft = resnet50(pretrained=True,export_onnx=args.export_onnx)
elif args.net == 'resnet152':
from models.resnet import resnet152
model_ft = resnet152(pretrained=True,export_onnx=args.export_onnx)
else:
print("The %s is not supported..." %(args.net))
return
if args.net == 'mobilenet':
num_ftrs = model_ft.classifier[1].in_features
model_ft.classifier[1] = nn.Linear(num_ftrs*4, args.num_classes)
else:
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, args.num_classes)
net = model_ft
return net
def get_train_dataloader(path,path_txt, transforms, batch_size, num_workers, target_transforms=None):
""" return training dataloader
Args:
path: path to CUB_200_2011 dataset
transforms: transforms of dataset
target_transforms: transforms for targets
batch_size: dataloader batchsize
num_workers: dataloader num_works
Returns: train_data_loader:torch dataloader object
"""
train_dataset = CUB_200_2011_Train(
path,
path_txt,
transform=transforms,
target_transform=target_transforms
)
## get sampler weight
class_ids = list(train_dataset.class_ids.values())
target = np.array(class_ids, dtype=np.int32)
class_sample_count = np.array(
[len(np.where(target == t)[0]) for t in np.unique(target)])
weight = 1. / class_sample_count
samples_weight = np.array([weight[t] for t in target])
samples_weight = torch.from_numpy(samples_weight)
samples_weight = samples_weight.double()
sampler = torch.utils.data.sampler.WeightedRandomSampler(samples_weight, len(samples_weight), replacement=True)
train_dataloader = DataLoader(
train_dataset,
batch_size=batch_size,
sampler=sampler,
num_workers=num_workers,
#shuffle=True,
)
return train_dataloader
'''
def get_train_dataloader(path,path_txt, transforms, batch_size, num_workers, target_transforms=None):
""" return training dataloader
Args:
path: path to CUB_200_2011 dataset
transforms: transforms of dataset
target_transforms: transforms for targets
batch_size: dataloader batchsize
num_workers: dataloader num_works
Returns: train_data_loader:torch dataloader object
"""
train_dataset = CUB_200_2011_Train(
path,
path_txt,
transform=transforms,
target_transform=target_transforms
)
train_dataloader = DataLoader(
train_dataset,
batch_size=batch_size,
num_workers=num_workers,
shuffle=False,
pin_memory=True
)
return train_dataloader
'''
def get_test_dataloader(path, path_txt, transforms, batch_size, num_workers, target_transforms=None):
""" return training dataloader
Args:
path: path to CUB_200_2011 dataset
transforms: transforms of dataset
target_transforms: transforms for targets
batch_size: dataloader batchsize
num_workers: dataloader num_works
Returns: train_data_loader:torch dataloader object
"""
test_dataset = CUB_200_2011_Test(
path,
path_txt,
transform=transforms,
target_transform=target_transforms
)
test_dataloader = DataLoader(
test_dataset,
batch_size=batch_size,
num_workers=num_workers,
shuffle=True
)
return test_dataloader
def get_lastlayer_params(net):
"""get last trainable layer of a net
Args:
network architectur
Returns:
last layer weights and last layer bias
"""
last_layer_weights = None
last_layer_bias = None
for name, para in net.named_parameters():
if 'weight' in name:
last_layer_weights = para
if 'bias' in name:
last_layer_bias = para
return last_layer_weights, last_layer_bias
def visualize_network(writer, net):
"""visualize network architecture"""
input_tensor = torch.Tensor(3, 3, settings.IMAGE_SIZE, settings.IMAGE_SIZE)
input_tensor = input_tensor.to(next(net.parameters()).device)
writer.add_graph(net, Variable(input_tensor, requires_grad=True))
def visualize_lastlayer(writer, net, n_iter):
"""visualize last layer grads"""
weights, bias = get_lastlayer_params(net)
writer.add_scalar('LastLayerGradients/grad_norm2_weights', weights.grad.norm(), n_iter)
writer.add_scalar('LastLayerGradients/grad_norm2_bias', bias.grad.norm(), n_iter)
def visualize_train_loss(writer, loss, n_iter):
"""visualize training loss"""
writer.add_scalar('Train/loss', loss, n_iter)
def visualize_param_hist(writer, net, epoch):
"""visualize histogram of params"""
for name, param in net.named_parameters():
layer, attr = os.path.splitext(name)
attr = attr[1:]
writer.add_histogram("{}/{}".format(layer, attr), param, epoch)
def visualize_test_loss(writer, loss, epoch):
"""visualize test loss"""
writer.add_scalar('Test/loss', loss, epoch)
def visualize_test_acc(writer, acc, epoch):
"""visualize test acc"""
writer.add_scalar('Test/Accuracy', acc, epoch)
def visualize_learning_rate(writer, lr, epoch):
"""visualize learning rate"""
writer.add_scalar('Train/LearningRate', lr, epoch)
def init_weights(net):
"""the weights of conv layer and fully connected layers
are both initilized with Xavier algorithm, In particular,
we set the parameters to random values uniformly drawn from [-a, a]
where a = sqrt(6 * (din + dout)), for batch normalization
layers, y=1, b=0, all bias initialized to 0.
"""
for m in net.modules():
if isinstance(m, nn.Conv2d):
nn.init.xavier_uniform_(m.weight)
#nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
return net
def split_weights(net):
"""split network weights into to categlories,
one are weights in conv layer and linear layer,
others are other learnable paramters(conv bias,
bn weights, bn bias, linear bias)
Args:
net: network architecture
Returns:
a dictionary of params splite into to categlories
"""
decay = []
no_decay = []
for m in net.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
decay.append(m.weight)
if m.bias is not None:
no_decay.append(m.bias)
else:
if hasattr(m, 'weight'):
no_decay.append(m.weight)
if hasattr(m, 'bias'):
no_decay.append(m.bias)
assert len(list(net.parameters())) == len(decay) + len(no_decay)
return [dict(params=decay), dict(params=no_decay, weight_decay=0)]
def mixup_data(x, y, alpha=0.2):
"""Returns mixed up inputs pairs of targets and lambda"""
if alpha > 0:
lam = np.random.beta(alpha, alpha)
else:
lam = 1
batch_size = x.size(0)
index = torch.randperm(batch_size)
index = index.to(x.device)
lam = max(lam, 1 - lam)
mixed_x = lam * x + (1 - lam) * x[index, :]
y_a = y
y_b = y[index, :]
return mixed_x, y_a, y_b, lam