-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmodel.py
169 lines (134 loc) · 6.56 KB
/
model.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
import random, sys, os
import torch
import torch.nn as nn
import torch.utils.data as data
import ops
from dataset import DatasetForDASH
from option import opt
import template
#M(ulti-resolution)Nas_Net
class MultiNetwork(nn.Module):
def __init__(self, config, act=nn.ReLU(True)):
super(MultiNetwork, self).__init__()
self.networks = nn.ModuleList()
self.scale_dict = {}
#set model parameter (e.g., layer, channel)
for iteration, scale in enumerate(config):
self.networks.append(SingleNetwork(num_block=config[scale]['block'], num_feature=config[scale]['feature'], num_channel=3, scale=scale, output_filter=config[scale]['output_filter'], bias=True, act=act))
self.scale_dict[scale] = iteration
self.target_scale = None
def getOutputNodes(self):
assert self.target_scale != None
return self.networks[self.scale_dict[self.target_scale]].getOutputNodes()
def setTargetScale(self, scale):
assert scale in self.scale_dict.keys()
self.target_scale= scale
def forward(self, x, idx=None):
assert self.target_scale != None
x = self.networks[self.scale_dict[self.target_scale]].forward(x, idx)
return x
#save a scalable DNN by dividing into chunks
def save_chunk(self, save_dir):
total_dict = {}
model_out_path = os.path.join(save_dir, 'DNN_chunk_1.pth')
state_dict = self.state_dict()
for index in range(4):
dict01 = {k:v for (k,v) in state_dict.items() if '{}.head'.format(index) in k}
dict02 = {k:v for (k,v) in state_dict.items() if '{}.tail'.format(index) in k}
dict03 = {k:v for (k,v) in state_dict.items() if '{}.upscale'.format(index) in k}
dict04 = {k:v for (k,v) in state_dict.items() if '{}.body_end.'.format(index) in k}
total_dict = {**total_dict, **dict01, **dict02, **dict03, **dict04}
torch.save(total_dict, model_out_path)
total_dict ={}
model_out_path = os.path.join(save_dir, 'DNN_chunk_2.pth')
total_dict = {k:v for (k,v) in state_dict.items() if '{}.body.0.0'.format(0) in k}
for index in range(4):
partial_dict = {k:v for (k,v) in state_dict.items() if '{}.body.0.0'.format(index) in k}
total_dict = {**total_dict, **partial_dict}
torch.save(total_dict, model_out_path)
total_dict ={}
model_out_path = os.path.join(save_dir, 'DNN_chunk_3.pth')
total_dict = {k:v for (k,v) in state_dict.items() if '{}.body.1.0'.format(0) in k}
for index in range(4):
partial_dict = {k:v for (k,v) in state_dict.items() if '{}.body.1.0'.format(index) in k}
total_dict = {**total_dict, **partial_dict}
torch.save(total_dict, model_out_path)
total_dict ={}
model_out_path = os.path.join(save_dir, 'DNN_chunk_4.pth')
total_dict = {k:v for (k,v) in state_dict.items() if '{}.body.2.0'.format(0) in k}
for index in range(4):
partial_dict = {k:v for (k,v) in state_dict.items() if '{}.body.2.0'.format(index) in k}
total_dict = {**total_dict, **partial_dict}
torch.save(total_dict, model_out_path)
total_dict ={}
model_out_path = os.path.join(save_dir, 'DNN_chunk_5.pth')
total_dict = {k:v for (k,v) in state_dict.items() if '{}.body.3.0'.format(0) in k}
for index in range(4):
partial_dict = {k:v for (k,v) in state_dict.items() if '{}.body.3.0'.format(index) in k}
total_dict = {**total_dict, **partial_dict}
torch.save(total_dict, model_out_path)
#S(ingle-resolution)NAS_Net
class SingleNetwork(nn.Module):
def __init__(self, num_block, num_feature, num_channel, scale, output_filter, bias=True, act=nn.ReLU(True)):
super(SingleNetwork, self).__init__()
self.num_block = num_block
self.num_feature = num_feature
self.num_channel = num_channel
self.scale = scale
assert self.scale in [1,2,3,4]
#output_filter is used for selecting intermediate layers that are used as early-exit
self.outputNode = []
for i in range(self.num_block // output_filter + 1):
self.outputNode.append(self.num_block - output_filter * i)
assert self.num_block - (output_filter * i) >= 0
if self.num_block not in self.outputNode:
self.outputNode.append(self.num_block)
self.outputNode = sorted(self.outputNode)
self.outputList = ops.random_gradual_03(self.outputNode)
self.head = nn.Sequential(*[nn.Conv2d(in_channels=self.num_channel, out_channels=self.num_feature, kernel_size=3, stride=1, padding=1, bias=bias)])
self.body = nn.ModuleList()
for _ in range(self.num_block):
modules_body = [ops.ResBlock(self.num_feature, bias=bias, act=act)]
self.body.append(nn.Sequential(*modules_body))
body_end = []
body_end.append(nn.Conv2d(in_channels=self.num_feature, out_channels=self.num_feature, kernel_size=3, stride=1, padding=1, bias=bias))
self.body_end = nn.Sequential(*body_end)
if self.scale > 1:
self.upscale= nn.Sequential(*ops.Upsampler(self.scale, self.num_feature, bias=bias))
self.tail = nn.Sequential(*[nn.Conv2d(in_channels=self.num_feature, out_channels=self.num_channel, kernel_size=3, stride=1, padding=1, bias=bias)])
def getOutputNodes(self):
return self.outputNode
def forward(self, x, idx=None):
#choose a random index for training
if idx is None:
idx = random.choice(self.outputList)
else:
assert idx <= self.num_block and idx >= 0
#feed-forward part
x = self.head(x)
res = x
for i in range(idx):
res = self.body[i](res)
res = self.body_end(res)
res += x
if self.scale > 1:
x = self.upscale(res)
else:
x = res
x = self.tail(x)
return x
if __name__ == "__main__":
"""Simple test code for model"""
model = MultiNetwork(template.get_nas_config('low')).to('cuda:0')
model.setTargetScale(4)
dataset = DatasetForDASH(opt)
dataset.setTargetLR(240)
dataset.setDatasetType('train')
#print(len(dataset))
#print(model.getOutputNodes(4))
dataloader = data.DataLoader(dataset=dataset, num_workers=opt.num_thread, batch_size=opt.num_batch, pin_memory=True, shuffle=True)
for iteration, batch in enumerate(dataloader):
batch[0] = batch[0].cuda()
output = model(batch[0])
print(batch[0].size(), output.size())
break