-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtools.py
293 lines (261 loc) · 11.6 KB
/
tools.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
import os, sys, glob, argparse
import logging
import types
from collections import OrderedDict
import torch
import torch.nn.functional as F
import utils
import models
import main as entry
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="0"
def export_onnx(args):
model_name = args.model
if model_name in models.model_zoo:
model, args = models.get_model(args)
else:
print("model(%s) not support, available models: %r" % (model_name, models.model_zoo))
return
if utils.check_file(args.old):
print("load pretrained from %s" % args.old)
if torch.cuda.is_available():
checkpoint = torch.load(args.old)
else: # force cpu mode
checkpoint = torch.load(args.old, map_location='cpu')
print("load pretrained ==> last epoch: %d" % checkpoint.get('epoch', 0))
print("load pretrained ==> last best_acc: %f" % checkpoint.get('best_acc', 0))
print("load pretrained ==> last learning_rate: %f" % checkpoint.get('learning_rate', 0))
try:
utils.load_state_dict(model, checkpoint.get('state_dict', checkpoint))
except RuntimeError:
print("Loading pretrained model failed")
else:
print("no pretrained file exists({}), init model with default initlizer".
format(args.old))
onnx_model = torch.nn.Sequential(OrderedDict([
('network', model),
#('softmax', torch.nn.Softmax()),
]))
onnx_path = "onnx/" + model_name
if not os.path.exists(onnx_path):
os.makedirs(onnx_path)
onnx_save = onnx_path + "/" + model_name + '.onnx'
input_names = ["input"]
dummy_input = torch.zeros((1, 3, args.input_size, args.input_size))
output_names = ['prob']
torch.onnx.export(
onnx_model,
dummy_input,
onnx_save,
verbose=True,
input_names=input_names,
output_names=output_names,
opset_version=7,
keep_initializers_as_inputs=True
)
def inference(args):
from models.quant import custom_conv
def init(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=False,
args=None, force_fp=False, feature_stride=1):
super(custom_conv, self).__init__(in_channels, out_channels, kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias)
self.args = args
self.force_fp = True
custom_conv.__init__ = init
model_name = args.model
if model_name in models.model_zoo:
model, args = models.get_model(args)
else:
print("model(%s) not support, available models: %r" % (model_name, models.model_zoo))
return
def forward(self, x):
print(x.shape, self.weight.shape, self.kernel_size, self.stride, self.padding, self.dilation, self.groups)
output = F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
return output
for m in model.modules():
if isinstance(m, torch.nn.Conv2d):
m.forward = types.MethodType(forward, m)
input = torch.rand(1, 3, args.input_size, args.input_size)
model.forward(input)
def get_parameter():
parser = entry.get_parser()
parser.add_argument('--old', type=str, default='')
parser.add_argument('--new', type=str, default='')
parser.add_argument('--mapping_from', '--mf', type=str, default='')
parser.add_argument('--mapping_to', '--mt', type=str, default='')
parser.add_argument('--verbose_list', default='ratio,sep', type=str)
args = parser.parse_args()
if isinstance(args.verbose_list, str):
args.verbose_list = [x.strip() for x in args.verbose_list.split(',')]
if isinstance(args.keyword, str):
args.keyword = [x.strip() for x in args.keyword.split(',')]
return args
def main():
args = get_parameter()
args.weights_dir = os.path.join(args.weights_dir, args.model)
utils.check_folder(args.weights_dir)
if os.path.exists(args.log_dir):
utils.setup_logging(os.path.join(args.log_dir, 'tools.txt'), resume=True)
config = dict()
for i in args.keyword:
config[i] = True
if 'export_onnx' in config.keys():
export_onnx(args)
if 'inference' in config.keys():
inference(args)
if 'verbose' in config.keys():
if torch.cuda.is_available():
checkpoint = torch.load(args.old)
else: # force cpu mode
checkpoint = torch.load(args.old, map_location='cpu')
if 'state_dict' in checkpoint:
checkpoint = checkpoint['state_dict']
if 'model' in checkpoint:
checkpoint = checkpoint['model']
for name, value in checkpoint.items():
if ('quant_activation' in name or 'quant_weight' in name) and name.split('.')[-1] in args.verbose_list:
print(name, value.shape, value.requires_grad)
print(value.data)
elif "all" in args.verbose_list:
if 'num_batches_tracked' not in name:
if isinstance(value, torch.Tensor):
print(name, value.shape, value.requires_grad)
elif isinstance(value, int) or isinstance(value, float) or isinstance(value, str):
print(name, value, type(value))
else:
print(name, type(value))
if 'load' in config.keys() or 'save' in config.keys():
model_name = args.model
if model_name in models.model_zoo:
model, args = models.get_model(args)
else:
print("model(%s) not support, available models: %r" % (model_name, models.model_zoo))
return
if utils.check_file(args.old):
raw = 'raw' in config.keys()
if torch.cuda.is_available():
checkpoint = torch.load(args.old)
else: # force cpu mode
checkpoint = torch.load(args.old, map_location='cpu')
try:
utils.load_state_dict(model, checkpoint.get('state_dict', None) if not raw else checkpoint, verbose=False)
except RuntimeError:
print("Loading pretrained model failed")
print("Loading pretrained model OK")
if 'save' in config.keys() and args.new != '':
torch.save(model.state_dict(), args.new)
print("Save pretrained model into %s" % args.new)
else:
print("file not exist %s" % args.old)
if 'update' in config.keys():
mapping_from = []
mapping_to = []
if os.path.isfile(args.mapping_from):
with open(args.mapping_from) as f:
mapping_from = f.readlines()
f.close()
if os.path.isfile(args.mapping_to):
with open(args.mapping_to) as f:
mapping_to = f.readlines()
f.close()
mapping_from = [ i.strip().strip('\n').strip('"').strip("'") for i in mapping_from]
mapping_from = [ i for i in mapping_from if len(i) > 0 and i[0] != '#']
mapping_to = [ i.strip().strip('\n').strip('"').strip("'") for i in mapping_to]
mapping_to = [ i for i in mapping_to if len(i) > 0 and i[0] != '#']
if len(mapping_to) != len(mapping_from) or len(mapping_to) == 0 or len(mapping_from) == 0:
mapping = None
logging.info('no valid mapping')
else:
mapping = dict()
for i, k in enumerate(mapping_from):
if '{' in k and '}' in k and '{' in mapping_to[i] and '}' in mapping_to[i]:
item = k.split('{')
for v in item[1].strip('}').split(","):
v = v.strip()
mapping[item[0] + v] = mapping_to[i].split('{')[0] + v
else:
mapping[k] = mapping_to[i]
raw = 'raw' in config.keys()
if not os.path.isfile(args.old):
args.old = args.pretrained
utils.import_state_dict(args.old, args.new, mapping, raw, raw_prefix=args.case)
if 'det-load' in config.keys():
from third_party.checkpoint import DetectionCheckpointer
model_name = args.model
if model_name in models.model_zoo:
model, args = models.get_model(args)
else:
print("model(%s) not support, available models: %r" % (model_name, models.model_zoo))
return
split = os.path.split(args.old)
checkpointer = DetectionCheckpointer(model, split[0], save_to_disk=True)
checkpointer.resume_or_load(args.old, resume=True)
checkpointer.save(split[1])
if 'swap' in config.keys():
mapping_from = []
if os.path.isfile(args.mapping_from):
with open(args.mapping_from) as f:
mapping_from = f.readlines()
f.close()
mapping_from = [ i.strip().strip('\n').strip('"').strip("'") for i in mapping_from]
mapping_from = [ i for i in mapping_from if len(i) > 0 and i[0] != '#']
lists = args.verbose_list
for i in lists:
item = i.split('/')
interval = (int)(item[0])
index = item[1].split('-')
index = [(int)(x) for x in index]
if len(mapping_from) % interval == 0 and len(index) <= interval:
mapping_to = mapping_from.copy()
for j, k in enumerate(index):
k = k % interval
mapping_to[j::interval] = mapping_from[k::interval]
mapping_to= [ i + '\n' for i in mapping_to]
with open(args.mapping_from + "-swap", 'w') as f:
f.writelines(mapping_to)
f.close()
if 'sort' in config.keys():
mapping_from = []
if os.path.isfile(args.mapping_from):
with open(args.mapping_from) as f:
mapping_from = f.readlines()
f.close()
mapping_from.sort()
with open(args.mapping_from + "-sort", 'w') as f:
f.writelines(mapping_from)
f.close()
if 'verify-data' in config.keys() or 'verify-image' in config.keys():
if 'verify-image' in config.keys():
lists = args.verbose_list
else:
with open(os.path.join(args.root, 'train.txt')) as f:
lists = f.readlines()
f.close()
from PIL import Image
from threading import Thread
print("going to check %d files" % len(lists))
def check(lists, start, end, index):
for i, item in enumerate(lists[start:end]):
try:
items = item.split()
if len(items) >= 1:
path = items[0].strip().strip('\n')
else:
print("skip line %s" % i)
continue
path = os.path.join(args.root, os.path.join("train", path))
imgs = Image.open(path)
imgs.resize((256,256))
if index == 0:
print(i, end ="\r", file=sys.stderr)
except (RuntimeError, IOError):
print("\nError when read image %s" % path)
print("\nFinish checking", index)
#lists = lists[45000:]
num = min(len(lists), 20)
for i in range(num):
start = len(lists) // num * i
end = min(start + len(lists) // num, len(lists))
th = Thread(target=check, args=(lists, start, end, i))
th.start()
if __name__ == '__main__':
main()