forked from gyhui14/spottune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
143 lines (119 loc) · 4.61 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
import os
import re
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision.datasets as torchdata
import agent_net
from spottune_models import *
class AverageMeter(object):
"""Computes and stores the average and current value"""
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
def adjust_learning_rate_net(optimizer, epoch, args):
"""Sets the learning rate to the initial LR decayed by 10 every X epochs"""
if epoch >= args.step3:
lr = args.lr * 0.001
if epoch >= args.step2:
lr = args.lr * 0.01
if epoch >= args.step1:
lr = args.lr * 0.1
else:
lr = args.lr
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def adjust_learning_rate_agent(optimizer, epoch, args):
"""Sets the learning rate to the initial LR decayed by 10 every X epochs"""
if epoch >= args.step3:
lr = args.lr_agent * 0.001
if epoch >= args.step2:
lr = args.lr_agent * 0.01
if epoch >= args.step1:
lr = args.lr_agent * 0.1
else:
lr = args.lr_agent
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def load_weights_to_flatresnet(source, net, agent, dataset):
checkpoint = torch.load(source)
net_old = checkpoint['net']
store_data = []
for name, m in net_old.named_modules():
if isinstance(m, nn.Conv2d):
store_data.append(m.weight.data)
element = 0
for name, m in net.named_modules():
if isinstance(m, nn.Conv2d):
m.weight.data = torch.nn.Parameter(store_data[element].clone())
element += 1
store_data = []
store_data_bias = []
store_data_rm = []
store_data_rv = []
for name, m in net_old.named_modules():
if isinstance(m, nn.BatchNorm2d):
store_data.append(m.weight.data)
store_data_bias.append(m.bias.data)
store_data_rm.append(m.running_mean)
store_data_rv.append(m.running_var)
element = 0
for name, m in net.named_modules():
if isinstance(m, nn.BatchNorm2d):
m.weight.data = torch.nn.Parameter(store_data[element].clone())
m.bias.data = torch.nn.Parameter(store_data_bias[element].clone())
m.running_var = store_data_rv[element].clone()
m.running_mean = store_data_rm[element].clone()
element += 1
agent_old = checkpoint['agent']
store_data = []
for name, m in agent_old.named_modules():
if isinstance(m, nn.Conv2d):
store_data.append(m.weight.data)
element = 0
for name, m in agent.named_modules():
if isinstance(m, nn.Conv2d):
m.weight.data = torch.nn.Parameter(store_data[element].clone())
element += 1
store_data = []
store_data_bias = []
store_data_rm = []
store_data_rv = []
for name, m in agent_old.named_modules():
if isinstance(m, nn.BatchNorm2d):
store_data.append(m.weight.data)
store_data_bias.append(m.bias.data)
store_data_rm.append(m.running_mean)
store_data_rv.append(m.running_var)
element = 0
for name, m in agent.named_modules():
if isinstance(m, nn.BatchNorm2d):
m.weight.data = torch.nn.Parameter(store_data[element].clone())
m.bias.data = torch.nn.Parameter(store_data_bias[element].clone())
m.running_var = store_data_rv[element].clone()
m.running_mean = store_data_rm[element].clone()
element += 1
agent.linear.weight.data = torch.nn.Parameter(agent_old.module.linear.weight.data.clone())
agent.linear.bias.data = torch.nn.Parameter(agent_old.module.linear.bias.data.clone())
net.linear.weight.data = torch.nn.Parameter(net_old.module.linear.weight.data.clone())
net.linear.bias.data = torch.nn.Parameter(net_old.module.linear.bias.data.clone())
del net_old
del agent_old
return net, agent
def get_net_and_agent(model, num_class, dataset = None):
if model == 'resnet26':
if dataset is not None:
source = '../cv/' + dataset + '/' + dataset + '.t7'
rnet = resnet26(num_class)
agent = agent_net.resnet(sum(rnet.layer_config))
rnet, agent = load_weights_to_flatresnet(source, rnet, agent, dataset)
return rnet, agent