-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.py
105 lines (81 loc) · 2.96 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
import os
import numpy as np
import torch
import torch.nn.functional as F
def ort_loss(x, y):
loss = torch.abs((x * y).sum(dim=1)).sum()
loss = loss / float(x.size(0))
return loss
def ang_loss(x, y):
loss = (x * y).sum(dim=1).sum()
loss = loss / float(x.size(0))
return loss
def rgb2gray(img):
r, g, b = torch.split(img, 1, dim=1)
return torch.mul(r, 0.299) + torch.mul(g, 0.587) + torch.mul(b, 0.114)
def save_checkpoint(model, epoch, name=""):
if not os.path.exists("model/"):
os.makedirs("model/")
model_path = "model/" + name + "_epoch_{}.pth.tar".format(epoch)
state = {"epoch": epoch, "state_dict": model.state_dict()}
torch.save(state, model_path)
print("checkpoint saved to {}".format(model_path))
def load_model(model, pretrained):
weights = torch.load(pretrained)
pretrained_dict = weights["state_dict"]
model_dict = model.state_dict()
# 1. filter out unnecessary keys
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
# 2. overwrite entries in the existing state dict
model_dict.update(pretrained_dict)
# 3. load the new state dict
model.load_state_dict(model_dict)
def set_requires_grad(nets, requires_grad=False):
if not isinstance(nets, list):
nets = [nets]
for net in nets:
if net is not None:
for param in net.parameters():
param.requires_grad = requires_grad
# assign adain_params to AdaIN layers
def assign_adain_params(adain_params, model):
for m in model.modules():
if m.__class__.__name__ == "AdaptiveInstanceNorm2d":
mean = adain_params[:, :m.num_features]
std = adain_params[:, m.num_features:2*m.num_features]
m.bias = mean.contiguous().view(-1)
m.weight = std.contiguous().view(-1)
if adain_params.size(1) > 2*m.num_features:
adain_params = adain_params[:, 2*m.num_features:]
def accuracy(output, target, topk=(1,)):
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def adjust_learning_rate(lr, step, optimizer, epoch):
scale = 0.457305051927326
lr = lr * (scale ** (epoch // step))
print('lr: {}'.format(lr))
if (epoch != 0) & (epoch % step == 0):
print('Change lr')
for param_group in optimizer.param_groups:
param_group['lr'] = param_group['lr'] * scale
class AverageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count