-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_classifier.sh
executable file
·375 lines (302 loc) · 10.8 KB
/
run_classifier.sh
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
#!/bin/bash -e
# NB: -e makes script to fail if internal script fails (for example when --run is enabled)
#######################################
## CHECK ARGS
#######################################
NARGS="$#"
echo "INFO: NARGS= $NARGS"
if [ "$NARGS" -lt 1 ]; then
echo "ERROR: Invalid number of arguments...see script usage!"
echo ""
echo "**************************"
echo "*** USAGE ***"
echo "**************************"
echo "$0 [ARGS]"
echo ""
echo "=========================="
echo "== ARGUMENT LIST =="
echo "=========================="
echo "*** MANDATORY ARGS ***"
echo "--inputfile=[FILENAME] - Input file name (.json) containing images to be processed."
echo ""
echo "*** OPTIONAL ARGS ***"
echo "=== MODEL OPTIONS ==="
echo "--model=[MODEL] - Classifier model to be used in prediction. Options are {smorphclass,sclass-radio_3.4um-4.6um-12um-22um}. Default: smorph"
echo ""
echo "=== PRE-PROCESSING OPTIONS ==="
echo "--imgsize=[IMGSIZE] - Size in pixels used for image resize"
echo "--normalize - Apply minmax normalization to images "
echo "--normmin=[NORM_MIN] - Normalization min value (default=0)"
echo "--normmax=[NORM_MAX] - Normalization max value (default=1)"
echo "--scale-absmax - Scale each image channel by absolute maximum value across all channels "
echo "--scale-max - Scale each image channel by its maximum value"
echo "--zscale - Apply zscale transform to each image channel"
echo "--zscale_contrasts=[CONTRASTS] - zscale transform contrast parameters (separated by commas) (default=0.25)"
echo "--sigmaclip - Apply sigma clipping to each image channel"
echo "--sigmaclip-low=[SIGMA_CLIP_LOW] - Min sigma clipping value (default=5)"
echo "--sigmaclip-up=[SIGMA_CLIP_UP] - Max sigma clipping value (default=30)"
echo "--sigmaclip-chid=[SIGMA_CHID] - Channel used to apply clipping (-1=all channels) (default=-1)"
echo "--standardize - Apply standardization to images"
echo "--means=[IMG_MEANS] - Image channel means (default=0)"
echo "--sigmas=[IMG_SIGMAS] - Image channel sigmas (default=1)"
echo ""
echo "=== RUN OPTIONS ==="
echo "--run - Run the generated run script on the local shell. If disabled only run script will be generated for later run."
echo "--scriptdir=[SCRIPT_DIR] - Job directory where to find scripts (default=/usr/bin)"
echo "--modeldir=[MODEL_DIR] - Job directory where to find model & weight files (default=/opt/models)"
echo "--jobdir=[JOB_DIR] - Job directory where to run (default=pwd)"
echo "--outdir=[OUTPUT_DIR] - Output directory where to put run output file (default=pwd)"
echo "--waitcopy - Wait a bit after copying output files to output dir (default=no)"
echo "--copywaittime=[COPY_WAIT_TIME] - Time to wait after copying output files (default=30)"
echo "--no-logredir - Do not redirect logs to output file in script "
echo "=========================="
exit 1
fi
#######################################
## PARSE ARGS
#######################################
JOB_DIR=""
JOB_OUTDIR=""
SCRIPT_DIR="/usr/bin"
MODEL_DIR="/opt/models"
DATALIST=""
DATALIST_GIVEN=false
RUN_SCRIPT=false
WAIT_COPY=false
COPY_WAIT_TIME=30
REDIRECT_LOGS=true
MODEL="smorphclass"
IMGSIZE=64
NORMALIZE_MINMAX=""
NORM_MIN=0
NORM_MAX=1
SCALE_TO_ABS_MAX=""
SCALE_TO_MAX=""
ZSCALE_STRETCH=""
ZSCALE_CONTRASTS="0.25"
CLIP_DATA=""
SIGMA_CLIP_LOW=5
SIGMA_CLIP_UP=30
CLIP_CHID=-1
STANDARDIZE=""
IMG_MEANS="0"
IMG_SIGMAS="1"
for item in "$@"
do
case $item in
## MANDATORY ##
--inputfile=*)
DATALIST=`echo $item | /bin/sed 's/[-a-zA-Z0-9]*=//'`
if [ "$DATALIST" != "" ]; then
DATALIST_GIVEN=true
fi
;;
## OPTIONAL ##
--run*)
RUN_SCRIPT=true
;;
--scriptdir=*)
SCRIPT_DIR=`echo $item | /bin/sed 's/[-a-zA-Z0-9]*=//'`
;;
--modeldir=*)
MODEL_DIR=`echo $item | /bin/sed 's/[-a-zA-Z0-9]*=//'`
;;
--outdir=*)
JOB_OUTDIR=`echo $item | /bin/sed 's/[-a-zA-Z0-9]*=//'`
;;
--waitcopy*)
WAIT_COPY=true
;;
--copywaittime=*)
COPY_WAIT_TIME=`echo $item | /bin/sed 's/[-a-zA-Z0-9]*=//'`
;;
--jobdir=*)
JOB_DIR=`echo $item | /bin/sed 's/[-a-zA-Z0-9]*=//'`
;;
--no-logredir*)
REDIRECT_LOGS=false
;;
--model=*)
MODEL=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--imgsize=*)
IMGSIZE=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--normalize*)
NORMALIZE_MINMAX="--normalize_minmax"
;;
--normmin=*)
NORM_MIN=`echo $item | /bin/sed 's/[-a-zA-Z0-9]*=//'`
;;
--normmax=*)
NORM_MAX=`echo $item | /bin/sed 's/[-a-zA-Z0-9]*=//'`
;;
--scale-absmax*)
SCALE_TO_ABS_MAX="--scale_to_abs_max"
;;
--scale-max*)
SCALE_TO_MAX="--scale_to_max"
;;
--zscale*)
ZSCALE_STRETCH="--zscale_stretch"
;;
--zscale-contrasts*)
ZSCALE_CONTRASTS=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--sigmaclip*)
CLIP_DATA="--clip_data"
;;
--sigmaclip-low*)
SIGMA_CLIP_LOW=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--sigmaclip-up*)
SIGMA_CLIP_UP=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--sigmaclip-chid*)
CLIP_CHID=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--standardize*)
STANDARDIZE="--standardize"
;;
--means*)
IMG_MEANS=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--sigmas*)
IMG_SIGMAS=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
*)
# Unknown option
echo "ERROR: Unknown option ($item)...exit!"
exit 1
;;
esac
done
## Check arguments parsed
if [ "$DATALIST_GIVEN" = false ]; then
echo "ERROR: Missing or empty DATALIST args (hint: you must specify it)!"
exit 1
fi
if [ "$JOB_DIR" = "" ]; then
echo "WARN: Empty JOB_DIR given, setting it to pwd ($PWD) ..."
JOB_DIR="$PWD"
fi
if [ "$JOB_OUTDIR" = "" ]; then
echo "WARN: Empty JOB_OUTDIR given, setting it to pwd ($PWD) ..."
JOB_OUTDIR="$PWD"
fi
#######################################
## SET CLASSIFIER OPTIONS
#######################################
PREPROC_OPTS="--resize_size=$IMGSIZE --upscale $NORMALIZE_MINMAX --norm_min=$NORM_MIN --norm_max=$NORM_MAX $SCALE_TO_ABS_MAX $SCALE_TO_MAX $ZSCALE_STRETCH --zscale_contrasts=$ZSCALE_CONTRASTS $CLIP_DATA --sigma_clip_low=$SIGMA_CLIP_LOW --sigma_clip_up=$SIGMA_CLIP_UP --clip_chid=$CLIP_CHID $STANDARDIZE --img_means=$IMG_MEANS --img_sigmas=$IMG_SIGMAS "
if [ "$MODEL" = "smorphclass" ]; then
CLASSID_REMAP='{0:-1,1:0,2:1,3:2,4:3,5:4,6:5}'
TARGET_LABEL_MAP='{0:"1C-1P",1:"1C-2P",2:"1C-3P",3:"2C-2P",4:"2C-3P",5:"3C-3P"}'
CLASSID_LABEL_MAP='{1:"1C-1P",2:"1C-2P",3:"1C-3P",4:"2C-2P",5:"2C-3P",6:"3C-3P"}'
TARGET_NAMES="1C-1P,1C-2P,1C-3P,2C-2P,2C-3P,3C-3P"
NCLASSES=6
MODELFILE="$MODEL_DIR/cnn-smorphclass_rgz.h5"
WEIGHTFILE="$MODEL_DIR/weights-smorphclass_rgz.h5"
elif [ "$MODEL" = "sclass-radio_3.4um-4.6um-12um-22um" ]; then
CLASSID_REMAP='{0:-1,1:4,2:5,3:0,6:1,23:2,24:3,6000:6}'
TARGET_LABEL_MAP='{-1:"UNKNOWN",0:"PN",1:"HII",2:"PULSAR",3:"YSO",4:"STAR",5:"GALAXY",6:"QSO"}'
CLASSID_LABEL_MAP='{0:"UNKNOWN",1:"STAR",2:"GALAXY",3:"PN",6:"HII",23:"PULSAR",24:"YSO",6000:"QSO"}'
TARGET_NAMES="PN","HII","PULSAR","YSO","STAR","GALAXY","QSO"
NCLASSES=7
MODELFILE="$MODEL_DIR/cnn-sclass_radio-3.4um-4.6um-12um-22um.h5"
WEIGHTFILE="$MODEL_DIR/weights-sclass_radio-3.4um-4.6um-12um-22um.h5"
else
echo "ERROR: Unknown/not supported MODEL argument $MODEL given!"
exit 1
fi
CLASS_OPTS="--classid_remap=$CLASSID_REMAP --target_label_map=$TARGET_LABEL_MAP --classid_label_map=$CLASSID_LABEL_MAP --target_names=$TARGET_NAMES --nclasses=$NCLASSES --objids_excluded_in_train=-1,0 "
#######################################
## DEFINE GENERATE EXE SCRIPT FCN
#######################################
# - Set shfile
shfile="run_predict.sh"
generate_exec_script(){
local shfile=$1
echo "INFO: Creating sh file $shfile ..."
(
echo "#!/bin/bash -e"
echo " "
echo " "
echo 'echo "*************************************************"'
echo 'echo "**** PREPARE JOB ****"'
echo 'echo "*************************************************"'
echo " "
echo "echo \"INFO: Entering job dir $JOB_DIR ...\""
echo "cd $JOB_DIR"
echo " "
echo 'echo "*************************************************"'
echo 'echo "**** RUN CLASSIFIER ****"'
echo 'echo "*************************************************"'
EXE="python $SCRIPT_DIR/run_classifier_nn.py"
ARGS="--predict --datalist=$DATALIST $PREPROC_OPTS --modelfile=$MODELFILE --weightfile=$WEIGHTFILE --objids_excluded_in_train=-1,0 --classid_remap=""'""$CLASSID_REMAP""'"" --target_label_map=""'""$TARGET_LABEL_MAP""'"" --classid_label_map=""'""$CLASSID_LABEL_MAP""'"" --target_names=""'""$TARGET_NAMES""'"" --nclasses=$NCLASSES"
CMD="$EXE $ARGS"
echo "date"
echo ""
echo "echo \"INFO: Running classifier ...\""
if [ $REDIRECT_LOGS = true ]; then
echo "$CMD >> $logfile 2>&1"
else
echo "$CMD"
fi
echo " "
echo 'JOB_STATUS=$?'
echo 'echo "Classifier terminated with status=$JOB_STATUS"'
echo "date"
echo " "
echo 'echo "*************************************************"'
echo 'echo "**** COPY DATA TO OUTDIR ****"'
echo 'echo "*************************************************"'
echo 'echo ""'
if [ "$JOB_DIR" != "$JOB_OUTDIR" ]; then
echo "echo \"INFO: Copying job outputs in $JOB_OUTDIR ...\""
echo "ls -ltr $JOB_DIR"
echo " "
echo "# - Copy output data"
echo 'tab_count=`ls -1 *.dat 2>/dev/null | wc -l`'
echo 'if [ $tab_count != 0 ] ; then'
echo " echo \"INFO: Copying output table file(s) to $JOB_OUTDIR ...\""
echo " cp *.dat $JOB_OUTDIR"
echo "fi"
echo " "
echo "# - Show output directory"
echo "echo \"INFO: Show files in $JOB_OUTDIR ...\""
echo "ls -ltr $JOB_OUTDIR"
echo " "
echo "# - Wait a bit after copying data"
echo "# NB: Needed if using rclone inside a container, otherwise nothing is copied"
if [ $WAIT_COPY = true ]; then
echo "sleep $COPY_WAIT_TIME"
fi
fi
echo " "
echo " "
echo 'echo "*** END RUN ***"'
echo 'exit $JOB_STATUS'
) > $shfile
chmod +x $shfile
}
## close function generate_exec_script()
###############################
## RUN CLASSIFIER
###############################
# - Check if job directory exists
if [ ! -d "$JOB_DIR" ] ; then
echo "INFO: Job dir $JOB_DIR not existing, creating it now ..."
mkdir -p "$JOB_DIR"
fi
# - Moving to job directory
echo "INFO: Moving to job directory $JOB_DIR ..."
cd $JOB_DIR
# - Generate run script
echo "INFO: Creating run script file $shfile ..."
generate_exec_script "$shfile"
# - Launch run script
if [ "$RUN_SCRIPT" = true ] ; then
echo "INFO: Running script $shfile to local shell system ..."
$JOB_DIR/$shfile
fi
echo "*** END SUBMISSION ***"