-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_outlier_finder.sh
executable file
·422 lines (354 loc) · 12.4 KB
/
run_outlier_finder.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/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 "=== INPUT OPTIONS ==="
echo "--datalist-key=[KEY] - Dictionary key name to be read in input datalist. Default: data"
echo "--selcols=[COLS] - Data column ids to be selected from input data, separated by commas"
echo "=== IFOREST OPTIONS ==="
echo "--predict - Predict data encoding using input IsolationForest model. Default: False"
echo "--model=[MODEL] - IsolationForest model filename (.h5) when using predict mode"
echo "--nestimators=[NESTIMATORS] - Number of forest trees to fit. Default: 100"
echo "--max-features=[MAX_FEATURES] - Number of max features used in each forest tree. Default: 1"
echo "--max-samples=[MAX_SAMPLES] - Number of max samples used in each forest tree. -1 means auto options, e.g. 256 entries, otherwise it is the fraction of total available entries. Default: -1"
echo "--contamination=[CONTAMINATION] - Fraction of outliers expected [0,0.5]. If None set it to auto. Default: None"
echo "--anomaly-thr=[ANOMALY_THR] - Threshold in anomaly score above which observation is set as outlier. Default: 0.9"
echo "--run-scan - Run parameter optimization scan before run. Default: false"
echo "--scan-nestimators - Scan n_estimators parameter. Default: false"
echo "--scan-maxfeatures - Scan max_features parameter . Default: false"
echo "--scan-maxsamples - Scan max_samples parameter. Default: false"
echo "--scan-contamination - Scan contamination parameter. Default: false"
echo "--random-state=[RND] - Model random state . Default: None"
echo ""
echo "=== DATA PRE-PROCESSING OPTIONS ==="
echo "--normalize - Apply minmax normalization to images "
echo "--scalerfile=[SCALER_FILE] - Load and use data transform stored in this file (.sav)."
echo "--classid-label-map=[DICT] - Class ID label dictionary. Will take labels from input dictionary label field if left empty. Default: empty"
#echo "--objids-excluded-in-train=[OBJIDS] - Source ids not included for training as considered unknown classes. Default: -1,0"
echo ""
echo "=== SAVE OPTIONS ==="
echo "--no-save-ascii - Disable save output to ascii format "
echo "--no-save-json - Disable save output to json format "
echo "--no-save-model - Disable save model to file "
echo "--no-save-features - Disable save features to file "
echo "--outfile=[FILENAME] - Name of output file. Default: outlier_data.dat"
echo "--outfile-json=[FILENAME] - Name of output file in json format. Default: outlier_data.json"
echo "--save-labels-in-ascii - Save class labels to ascii. Default: False (save class ids) "
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
#######################################
# - Run options
JOB_DIR=""
JOB_OUTDIR=""
SCRIPT_DIR="/usr/bin"
RUN_SCRIPT=false
WAIT_COPY=false
COPY_WAIT_TIME=30
REDIRECT_LOGS=true
# - Input options
DATALIST=""
DATALIST_GIVEN=false
DATALIST_KEY="data"
SELCOLS=""
# - IsolationForest options
PREDICT=""
MODEL=""
NESTIMATORS=100
MAX_FEATURES=1
MAX_SAMPLES=-1
CONTAMINATION=""
ANOMALY_THR=0.9
RUN_SCAN=""
SCAN_NESTIMATORS=""
SCAN_MAXFEATURES=""
SCAN_MAXSAMPLES=""
SCAN_CONTAMINATION=""
RANDOM_STATE_OPT=""
# - Data pre-processing options
NORMALIZE=""
SCALERFILE=""
CLASSID_LABEL_MAP=""
#OBJS_EXCLUDED_IN_TRAIN="0,1"
# - Save options
OUTFILE="outlier_data.dat"
OUTFILE_JSON="outlier_data.json"
NO_SAVE_ASCII=""
NO_SAVE_JSON=""
NO_SAVE_MODEL=""
NO_SAVE_FEATURES=""
SAVE_LABELS=""
for item in "$@"
do
case $item in
# **************************
# ** MANDATORY
# **************************
# - INPUT OPTIONS
--inputfile=*)
DATALIST=`echo $item | /bin/sed 's/[-a-zA-Z0-9]*=//'`
if [ "$DATALIST" != "" ]; then
DATALIST_GIVEN=true
fi
;;
--datalist-key=*)
DATALIST_KEY=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--selcols=*)
SELCOLS=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
# **************************
# ** OPTIONAL OPTIONS
# **************************
# - IsolationForest options
--predict*)
PREDICT="--predict"
;;
--model=*)
MODEL=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--nestimators=*)
NESTIMATORS=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--max-features=*)
MAX_FEATURES=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--max-samples=*)
MAX_SAMPLES=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--contamination=*)
CONTAMINATION=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--anomaly-thr=*)
ANOMALY_THR=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--run-scan*)
RUN_SCAN="--run_scan"
;;
--scan-nestimators*)
SCAN_NESTIMATORS="--scan_nestimators"
;;
--scan-maxfeatures*)
SCAN_MAXFEATURES="--scan_maxfeatures"
;;
--scan-maxsamples*)
SCAN_MAXSAMPLES="--scan_maxsamples"
;;
--scan-contamination*)
SCAN_CONTAMINATION="--scan_contamination"
;;
--random-state=*)
RANDOM_STATE=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
RANDOM_STATE_OPT="--random_state=$RANDOM_STATE "
;;
# - PREPROC OPTIONS
--normalize*)
NORMALIZE="--normalize"
;;
--scalerfile=*)
SCALERFILE=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
--classid-label-map=*)
CLASSID_LABEL_MAP=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
;;
# --objids-excluded-in-train=*)
# OBJS_EXCLUDED_IN_TRAIN=`echo $item | sed 's/[-a-zA-Z0-9]*=//'`
# ;;
# - SAVE OPTIONS
--no-save-ascii*)
NO_SAVE_ASCII="--no_save_ascii"
;;
--no-save-json*)
NO_SAVE_JSON="--no_save_json"
;;
--no-save-model*)
NO_SAVE_MODEL="--no_save_model"
;;
--no-save-features*)
NO_SAVE_FEATURES="--no_save_features"
;;
--outfile=*)
OUTFILE=`echo $item | /bin/sed 's/[-a-zA-Z0-9]*=//'`
;;
--outfile-json=*)
OUTFILE_JSON=`echo $item | /bin/sed 's/[-a-zA-Z0-9]*=//'`
;;
--save-labels-in-ascii*)
SAVE_LABELS="--save_labels_in_ascii"
;;
# - RUN OPTIONS
--run*)
RUN_SCRIPT=true
;;
--scriptdir=*)
SCRIPT_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
;;
*)
# 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 OPTIONS
#######################################
INPUT_OPTS="--inputfile=$DATALIST --datalist_key=$DATALIST_KEY --selcols=$SELCOLS "
#PREPROC_OPTS="$NORMALIZE --scalerfile=$SCALERFILE --classid_label_map=$CLASSID_LABEL_MAP --objids_excluded_in_train=$OBJS_EXCLUDED_IN_TRAIN "
PREPROC_OPTS="$NORMALIZE --scalerfile=$SCALERFILE --classid_label_map=$CLASSID_LABEL_MAP "
IFOREST_OPTS="--modelfile=$MODEL $PREDICT --n_estimators=$NESTIMATORS --max_features=$MAX_FEATURES --max_samples=$MAX_SAMPLES --contamination=$CONTAMINATION --anomaly_thr=$ANOMALY_THR $RUN_SCAN $SCAN_NESTIMATORS $SCAN_MAXFEATURES $SCAN_MAXSAMPLES $SCAN_CONTAMINATION $RANDOM_STATE_OPT "
SAVE_OPTS="--outfile=$OUTFILE --outfile_json=$OUTFILE_JSON $SAVE_LABELS $NO_SAVE_ASCII $NO_SAVE_JSON $NO_SAVE_MODEL $NO_SAVE_FEATURES "
#######################################
## DEFINE GENERATE EXE SCRIPT FCN
#######################################
# - Set shfile
shfile="run_outlier_finder.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/find_outliers.py"
ARGS="$INPUT_OPTS $PREPROC_OPTS $IFOREST_OPTS $SAVE_OPTS "
CMD="$EXE $ARGS"
echo "date"
echo ""
echo "echo \"INFO: Running outlier search ...\""
if [ $REDIRECT_LOGS = true ]; then
echo "$CMD >> $logfile 2>&1"
else
echo "$CMD"
fi
echo " "
echo 'JOB_STATUS=$?'
echo 'echo "Outlier search run 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 'tab_count=`ls -1 *.json 2>/dev/null | wc -l`'
echo 'if [ $tab_count != 0 ] ; then'
echo " echo \"INFO: Copying output json file(s) to $JOB_OUTDIR ...\""
echo " cp *.json $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 OUTLIER FINDER
###############################
# - 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 ***"