-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathfunctions
350 lines (318 loc) · 9.88 KB
/
functions
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
#!/usr/bin/env bash
usage() {
cat <<- EOS
Usage: docker-compose run finetuner <command>
Commands: (default is finetune)
finetune: Do fine-tuning
gen_train <image_size>: Generate train and validation image recordIO files.
gen_test <image_size>: Generate test image recordIO files.
test: test with specified model.
ensemble <valid or test>: averaging ensemble test with validation or test dataset.
export: Generate MXNet models for mxnet-model-server.
num_layers <model_name>: Print the number of layers before the last fullc layer.
jupyter: Launch jupyter notebook. Add --service-port option
when executing command.
docker-commpose run --service-port finetuner jupyter
version: Show the mxnet-finetuner version information.
EOS
}
version() {
echo 'mxnet-finetuner version 0.0.11'
}
generate_compose() {
local cur_dir="$1"
local docker_compose_file="$2"
local nvidia_docker_version="$3"
if [[ $nvidia_docker_version = "2" ]]; then
mo < "$cur_dir/util/compose-template-nvidia-docker2.mo" > "$cur_dir/$docker_compose_file" \
&& echo "Generate $dockr_compose_file"
else
mo < "$cur_dir/util/compose-template.mo" > "$cur_dir/$docker_compose_file" \
&& echo "Generate $dockr_compose_file"
fi
}
update_compose() {
local cur_dir="$1"
local DEVICES="$2"
local docker_compose_file="$3"
local nvidia_docker_version="$4"
if [[ $nvidia_docker_version != "2" ]]; then
if [[ "$DEVICES" = "" ]]; then
sed -i -e 's/knjcode\/mxnet-finetuner/knjcode\/mxnet-finetuner:cpu/' "$cur_dir/$docker_compose_file" \
&& echo "Set use cpu docker image (knjcode/mxnet-finetuner:cpu)"
sed -i -e 's/awsdeeplearningteam\/mms_gpu/awsdeeplearningteam\/mms_cpu/' "$cur_dir/$docker_compose_file" \
&& echo "Set use cpu docker image (awsdeeplearningteam/mms_cpu)"
sed -i -e 's/mms_app_gpu.conf/mms_app_cpu.conf/' "$cur_dir/$docker_compose_file" \
&& echo "Set use cpu config file for mxnet-model-server (model/mms_app_cpu.conf)"
else
echo "Set use gpu docker image (knjcode/mxnet-finetuner)"
fi
sed -i -e 's/\/usr\/local\/nvidia:roFIX_VOLUME_NAME$//' "$cur_dir/$docker_compose_file"
fi
}
generate_config() {
local cur_dir="$1"
local config_file="$2"
cp "$cur_dir/util/sample_config.yml" "$cur_dir/$config_file" \
&& echo "Generate $config_file"
}
update_config() {
local cur_dir="$1"
local DEVICES="$2"
local config_file="$3"
if [[ ! "$DEVICES" = "" ]]; then
sed -i -e 's/# gpus/gpus/g' "$cur_dir/$config_file" \
&& echo "Detect GPUs. Activate common.gpus option in $config_file"
fi
}
generate_export_model_signature() {
local cur_dir="$1"
local MODEL_IMAGE_SIZE="$2"
local RGB_MEAN="$3"
local NUM_CLASSES="$4"
local EXPORT_TMP_DIR="$5"
mo < "$cur_dir/export_tmpl/signature.mo" > "$EXPORT_TMP_DIR/signature.json" \
&& echo "Generate signature.json"
}
generate_export_model_service() {
local cur_dir="$1"
local CENTER_CROP="$2"
local TOP_K="$3"
local SERVICE_TMP_DIR="$4"
if [[ "$CENTER_CROP" = '1' ]]; then
mo < "$cur_dir/export_tmpl/mxnet_vision_service_center_crop.mo" > "$SERVICE_TMP_DIR/mxnet_finetuner_service.py" \
&& echo "Generate mxnet_finetuner_service.py"
else
mo < "$cur_dir/export_tmpl/mxnet_vision_service.mo" > "$SERVICE_TMP_DIR/mxnet_finetuner_service.py" \
&& echo "Generate mxnet_finetuner_service.py"
fi
}
generate_export_model_conf() {
local cur_dir="$1"
local MODEL_NAME="$2"
local MODEL_FILE="$3"
mo < "$cur_dir/export_tmpl/mms_app_cpu.conf.mo" > model/mms_app_cpu.conf \
&& echo "Saved mms config for cpu \"model/mms_app_cpu.conf\""
mo < "$cur_dir/export_tmpl/mms_app_gpu.conf.mo" > model/mms_app_gpu.conf \
&& echo "Saved mms config for gpu \"model/mms_app_gpu.conf\""
}
check_from_scratch() {
local model="$1"
if [[ "$model" = scratch-* ]]; then
echo 0
else
echo 1
fi
}
trim_scratch() {
local model="$1"
if [[ $(check_from_scratch "$model") -eq 0 ]]; then
echo "$model" | sed -e 's/^scratch-//'
else
echo "$momdel"
fi
}
check_has_num_layers() {
local model="$1"
if [[ "$model" = *resnet-v1-* ]]; then
echo 0
elif [[ "$model" = *resnet-* ]]; then
echo 0
elif [[ "$model" = *resnext-* ]]; then
echo 0
elif [[ "$model" = *vgg-* ]]; then
echo 0
else
echo 1
fi
}
check_resnet_num_layers() {
# Specify the number of layers for N in scratch-resnet-v1, scratch-resnet and scratch-resnext.
# N can be set to 18, 34, 50, 101, 152, 200 and 269.
local model="$1"
if [[ $(check_has_num_layers "$model") -eq 0 ]]; then
# num-layers
if [[ "$model" =~ 18|34|50|101|152|200|269 ]]; then
echo 0
else
echo 1
fi
else
# do not have num-layers
echo 1
fi
}
check_vgg_num_layers() {
# Specify the number of layers for N in scratch-vgg.
# N can be set to 11, 13, 16 and 19.
local model="$1"
if [[ $(check_has_num_layers "$model") -eq 0 ]]; then
# num-layers
if [[ "$model" =~ 11|13|16|19 ]]; then
echo 0
else
echo 1
fi
else
# do not have num-layers
echo 1
fi
}
get_resnet_num_layers() {
local model="$1"
if [[ $(check_resnet_num_layers "$model") -eq 0 ]]; then
echo "$model" | awk -F - '{ print $NF }'
else
echo 'null'
fi
}
get_vgg_num_layers() {
local model="$1"
if [[ $(check_vgg_num_layers "$model") -eq 0 ]]; then
echo "$model" | awk -F - '{ print $NF }'
else
echo 'null'
fi
}
get_num_layers() {
local model="$1"
if [[ "$model" = *vgg-* ]]; then
get_vgg_num_layers "$model"
elif [[ "$model" = *resnet-v1-* ]]; then
get_resnet_num_layers "$model"
elif [[ "$model" = *resnet-* ]]; then
get_resnet_num_layers "$model"
elif [[ "$model" = *resnext-* ]]; then
get_resnet_num_layers "$model"
else
echo 'null'
fi
}
trim_num_layers() {
local model="$1"
echo "$model" | sed -e 's/-[0-9]*$//'
}
get_conf() {
local config="$1"
local param="$2"
local default="$3"
local value
value=$(echo "$config" | jq -r "$param")
if [[ "$value" = 'null' ]]; then
value="$default"
fi
echo "$value"
}
get_conf_array() {
local config="$1"
local param="$2"
local default="$3"
local value
value=$(echo "$config" | jq -r "$param")
if [[ "$value" = 'null' ]]; then
value="$default"
else
value=$(echo "$config" | jq -r "$param | .[]")
fi
echo "$value"
}
get_image_size() {
local MODEL="$1"
if [[ "$MODEL" = *caffenet* ]]; then
IMAGE_SIZE=227
elif [[ "$MODEL" = *squeezenet* ]]; then
IMAGE_SIZE=227
elif [[ "$MODEL" = *alexnet* ]]; then
IMAGE_SIZE=227
elif [[ "$MODEL" = *googlenet* ]]; then
IMAGE_SIZE=299
elif [[ "$MODEL" = *inception-v3* ]]; then
IMAGE_SIZE=299
elif [[ "$MODEL" = *inception-v4* ]]; then
IMAGE_SIZE=299
elif [[ "$MODEL" = *inception-resnet-v2* ]]; then
IMAGE_SIZE=299
else
IMAGE_SIZE=224
fi
echo "$IMAGE_SIZE"
}
download_inception_v3_model() {
if [ ! -e inception-v3.tar.gz ]; then
wget http://data.dmlc.ml/models/imagenet/inception-v3.tar.gz
fi
tar xf inception-v3.tar.gz
mv model/Inception-7-0001.params model/imagenet1k-inception-v3-0000.params
mv model/Inception-7-symbol.json model/imagenet1k-inception-v3-symbol.json
}
check_inception_v3_model() {
if [ ! -e "/mxnet/example/image-classification/model/imagenet1k-inception-v3-0000.params" ]; then
download_inception_v3_model
fi
}
gdrive_download () {
local FILEID="$1"
local FILENAME="$2"
CONFIRM=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate \
"https://docs.google.com/uc?export=download&id=$1" -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')
wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$CONFIRM&id=$1" -O $2
rm -rf /tmp/cookies.txt
}
download_se_resnext_50_model() {
gdrive_download "0B_M7XF_l0CzXOHNybXVWLWZteEE" "model/imagenet1k-se-resnext-50-0000.params"
wget "https://raw.githubusercontent.com/bruinxiong/SENet.mxnet/master/se-resnext-imagenet-50-0-symbol.json" \
-O "model/imagenet1k-se-resnext-50-symbol.json"
}
check_se_resnext_50_model() {
if [ ! -e "/mxnet/example/image-classification/model/imagenet1k-se-resnext-50-0000.params" ]; then
download_se_resnext_50_model
fi
}
download_densenet_169_model() {
gdrive_download "0B_M7XF_l0CzXX3V3WXJoUnNKZFE" "model/imagenet1k-densenet-169-0000.params"
wget "https://raw.githubusercontent.com/bruinxiong/densenet.mxnet/master/densenet-imagenet-169-0-symbol.json" \
-O "model/imagenet1k-densenet-169-symbol.json"
}
check_densenet_169_model() {
if [ ! -e "/mxnet/example/image-classification/model/imagenet1k-densenet-169-0000.params" ]; then
download_densenet_169_model
fi
}
get_layer_before_fullc() {
local MODEL="$1"
if [[ $MODEL = *caffenet* ]]; then
LAYER_BEFORE_FULLC="flatten_0"
elif [[ $MODEL = *vgg* ]]; then
LAYER_BEFORE_FULLC="flatten_0"
elif [[ $MODEL = *nin* ]]; then
LAYER_BEFORE_FULLC="flatten"
elif [[ $MODEL = *squeezenet* ]]; then
LAYER_BEFORE_FULLC="flatten"
elif [[ $MODEL = *inception-v3* ]]; then
LAYER_BEFORE_FULLC="flatten"
check_inception_v3_model
elif [[ $MODEL = *inception* ]]; then
LAYER_BEFORE_FULLC="flatten"
elif [[ $MODEL = *resnet* ]]; then
LAYER_BEFORE_FULLC="flatten0"
elif [[ $MODEL = *se-resnext-50* ]]; then
LAYER_BEFORE_FULLC="flatten0"
check_se_resnext_50_model
elif [[ $MODEL = *resnext* ]]; then
LAYER_BEFORE_FULLC="flatten0"
elif [[ $MODEL = *densenet* ]]; then
LAYER_BEFORE_FULLC="flatten0"
check_densenet_169_model
else
LAYER_BEFORE_FULLC="flatten_0"
fi
echo "$LAYER_BEFORE_FULLC"
}
print_classification_report() {
local report="$1"
local body
body=$(cat "$report" | tail -n +4 | \
sed -e 's/precision/a precision/' -e 's/avg \/ total/avg\/total/' | \
column -t | sed -e 's/^a / /' | sed -e '2i \ ' | sed -e '$ i \ ')
echo "$body"
}