forked from osmr/imgclsmob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
383 lines (333 loc) · 10.6 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""
Main routines shared between training and evaluation scripts.
"""
__all__ = ['prepare_mx_context', 'get_initializer', 'prepare_model', 'calc_net_weight_count', 'validate',
'report_accuracy', 'get_composite_metric', 'get_metric_name', 'get_loss']
import os
import re
import logging
import numpy as np
import mxnet as mx
from mxnet.gluon.loss import SoftmaxCrossEntropyLoss
from .gluoncv2.model_provider import get_model
from .metrics.cls_metrics import Top1Error, TopKError
from .metrics.seg_metrics import PixelAccuracyMetric, MeanIoUMetric
from .metrics.det_metrics import CocoDetMApMetric, VOC07MApMetric, WiderfaceDetMetric
from .metrics.hpe_metrics import CocoHpeOksApMetric
from .losses import SegSoftmaxCrossEntropyLoss, MixSoftmaxCrossEntropyLoss
def prepare_mx_context(num_gpus,
batch_size):
"""
Prepare MXNet context and correct batch size.
Parameters
----------
num_gpus : int
Number of GPU.
batch_size : int
Batch size for each GPU.
Returns
-------
Context
MXNet context.
int
Batch size for all GPUs.
"""
ctx = [mx.gpu(i) for i in range(num_gpus)] if num_gpus > 0 else [mx.cpu()]
batch_size *= max(1, num_gpus)
return ctx, batch_size
def get_initializer(initializer_name):
"""
Get initializer by name.
Parameters
----------
initializer_name : str
Initializer name.
Returns
-------
Initializer
Initializer.
"""
if initializer_name == "MSRAPrelu":
return mx.init.MSRAPrelu()
elif initializer_name == "Xavier":
return mx.init.Xavier()
elif initializer_name == "Xavier-gaussian-out-2":
return mx.init.Xavier(
rnd_type="gaussian",
factor_type="out",
magnitude=2)
else:
return None
def prepare_model(model_name,
use_pretrained,
pretrained_model_file_path,
dtype,
net_extra_kwargs=None,
load_ignore_extra=False,
tune_layers=None,
classes=None,
in_channels=None,
do_hybridize=True,
initializer=mx.init.MSRAPrelu(),
ctx=mx.cpu()):
"""
Create and initialize model by name.
Parameters
----------
model_name : str
Model name.
use_pretrained : bool
Whether to use pretrained weights.
pretrained_model_file_path : str
Path to file with pretrained weights.
dtype : str
Base data type for tensors.
net_extra_kwargs : dict, default None
Extra parameters for model.
load_ignore_extra : bool, default False
Whether to ignore extra layers in pretrained model.
tune_layers : dict, default False
Layers for tuning (all other will be frozen).
classes : int, default None
Number of classes.
in_channels : int, default None
Number of input channels.
do_hybridize : bool, default True
Whether to hybridize model.
initializer : Initializer
Initializer.
ctx : Context, default CPU
MXNet context.
Returns
-------
HybridBlock
Model.
"""
kwargs = {"ctx": ctx,
"pretrained": use_pretrained}
if classes is not None:
kwargs["classes"] = classes
if in_channels is not None:
kwargs["in_channels"] = in_channels
if net_extra_kwargs is not None:
kwargs.update(net_extra_kwargs)
net = get_model(model_name, **kwargs)
if pretrained_model_file_path:
assert (os.path.isfile(pretrained_model_file_path))
logging.info("Loading model: {}".format(pretrained_model_file_path))
net.load_parameters(
filename=pretrained_model_file_path,
ctx=ctx,
ignore_extra=load_ignore_extra)
net.cast(dtype)
if do_hybridize:
net.hybridize(
static_alloc=True,
static_shape=True)
if pretrained_model_file_path or use_pretrained:
for param in net.collect_params().values():
if param._data is not None:
continue
param.initialize(initializer, ctx=ctx)
else:
net.initialize(initializer, ctx=ctx)
if (tune_layers is not None) and tune_layers:
tune_layers_pattern = re.compile(tune_layers)
for k, v in net._collect_params_with_prefix().items():
if tune_layers_pattern.match(k):
logging.info("Fine-tune parameter: {}".format(k))
else:
v.grad_req = "null"
for param in net.collect_params().values():
if param._data is not None:
continue
param.initialize(initializer, ctx=ctx)
return net
def calc_net_weight_count(net):
"""
Calculate number of model trainable parameters.
Parameters
----------
net : HybridBlock
Model.
Returns
-------
int
Number of parameters.
"""
net_params = net.collect_params()
weight_count = 0
for param in net_params.values():
if (param.shape is None) or (not param._differentiable):
continue
weight_count += np.prod(param.shape)
return weight_count
def validate(metric,
net,
val_data,
batch_fn,
data_source_needs_reset,
dtype,
ctx):
"""
Core validation/testing routine.
Parameters:
----------
metric : EvalMetric
Metric object instance.
net : HybridBlock
Model.
val_data : DataLoader or ImageRecordIter
Data loader or ImRec-iterator.
batch_fn : func
Function for splitting data after extraction from data loader.
data_source_needs_reset : bool
Whether to reset data (if test_data is ImageRecordIter).
dtype : str
Base data type for tensors.
ctx : Context
MXNet context.
Returns
-------
EvalMetric
Metric object instance.
"""
if data_source_needs_reset:
val_data.reset()
metric.reset()
for batch in val_data:
data_list, labels_list = batch_fn(batch, ctx)
outputs_list = [net(X.astype(dtype, copy=False)) for X in data_list]
metric.update(labels_list, outputs_list)
return metric
def report_accuracy(metric,
extended_log=False):
"""
Make report string for composite metric.
Parameters:
----------
metric : EvalMetric
Metric object instance.
extended_log : bool, default False
Whether to log more precise accuracy values.
Returns
-------
str
Report string.
"""
def create_msg(name, value):
if type(value) in [list, tuple]:
if extended_log:
return "{}={} ({})".format("{}", "/".join(["{:.4f}"] * len(value)), "/".join(["{}"] * len(value))).\
format(name, *(value + value))
else:
return "{}={}".format("{}", "/".join(["{:.4f}"] * len(value))).format(name, *value)
else:
if extended_log:
return "{name}={value:.4f} ({value})".format(name=name, value=value)
else:
return "{name}={value:.4f}".format(name=name, value=value)
metric_info = metric.get()
if isinstance(metric, mx.metric.CompositeEvalMetric):
msg = ", ".join([create_msg(name=m[0], value=m[1]) for m in zip(*metric_info)])
elif isinstance(metric, mx.metric.EvalMetric):
msg = create_msg(name=metric_info[0], value=metric_info[1])
else:
raise Exception("Wrong metric type: {}".format(type(metric)))
return msg
def get_metric(metric_name, metric_extra_kwargs):
"""
Get metric by name.
Parameters:
----------
metric_name : str
Metric name.
metric_extra_kwargs : dict
Metric extra parameters.
Returns
-------
EvalMetric
Metric object instance.
"""
if metric_name == "Top1Error":
return Top1Error(**metric_extra_kwargs)
elif metric_name == "TopKError":
return TopKError(**metric_extra_kwargs)
elif metric_name == "PixelAccuracyMetric":
return PixelAccuracyMetric(**metric_extra_kwargs)
elif metric_name == "MeanIoUMetric":
return MeanIoUMetric(**metric_extra_kwargs)
elif metric_name == "CocoDetMApMetric":
return CocoDetMApMetric(**metric_extra_kwargs)
elif metric_name == "VOC07MApMetric":
return VOC07MApMetric(**metric_extra_kwargs)
elif metric_name == "WiderfaceDetMetric":
return WiderfaceDetMetric(**metric_extra_kwargs)
elif metric_name == "CocoHpeOksApMetric":
return CocoHpeOksApMetric(**metric_extra_kwargs)
else:
raise Exception("Wrong metric name: {}".format(metric_name))
def get_composite_metric(metric_names, metric_extra_kwargs):
"""
Get composite metric by list of metric names.
Parameters:
----------
metric_names : list of str
Metric name list.
metric_extra_kwargs : list of dict
Metric extra parameters list.
Returns
-------
CompositeEvalMetric
Metric object instance.
"""
if len(metric_names) == 1:
metric = get_metric(metric_names[0], metric_extra_kwargs[0])
else:
metric = mx.metric.CompositeEvalMetric()
for name, extra_kwargs in zip(metric_names, metric_extra_kwargs):
metric.add(get_metric(name, extra_kwargs))
return metric
def get_metric_name(metric, index):
"""
Get metric name by index in the composite metric.
Parameters:
----------
metric : CompositeEvalMetric or EvalMetric
Metric object instance.
index : int
Index.
Returns
-------
str
Metric name.
"""
if isinstance(metric, mx.metric.CompositeEvalMetric):
return metric.metrics[index].name
elif isinstance(metric, mx.metric.EvalMetric):
assert (index == 0)
return metric.name
else:
raise Exception("Wrong metric type: {}".format(type(metric)))
def get_loss(loss_name, loss_extra_kwargs):
"""
Get loss by name.
Parameters:
----------
loss_name : str
Loss name.
loss_extra_kwargs : dict
Loss extra parameters.
Returns
-------
Loss
Loss object instance.
"""
if loss_name == "SoftmaxCrossEntropy":
return SoftmaxCrossEntropyLoss(**loss_extra_kwargs)
if loss_name == "SegSoftmaxCrossEntropy":
return SegSoftmaxCrossEntropyLoss(**loss_extra_kwargs)
if loss_name == "MixSoftmaxCrossEntropy":
return MixSoftmaxCrossEntropyLoss(**loss_extra_kwargs)
else:
raise Exception("Wrong loss name: {}".format(loss_name))