-
Notifications
You must be signed in to change notification settings - Fork 14
/
infer.sh
executable file
·113 lines (100 loc) · 2.16 KB
/
infer.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
#!/usr/bin/env bash
command="$0 $@"
function base_usage {
echo "Usage: $0 [-c checkpoint_dir] [-g gpu] [-o output_dir] [-h|--help] [-v] img1 [img2 ...]"
}
function usage {
base_usage
python3 main.py -h
}
if [[ $# -eq 0 ]]; then
base_usage
exit 0
fi
params=
GPUID=0
cptd=
output_dir=./
inputs=()
while [[ $# -gt 0 ]]; do
case "$1" in
-c)
params="$params --checkpoint_dir $2"
cptd="$2"
shift
;;
-g)
GPUID="$2"
shift
;;
-h|--help)
usage
exit 0
;;
-o)
output_dir="$2"
if [[ ! -d "$output_dir" ]]; then
mkdir "$output_dir"
fi
shift
;;
-v)
params="$params --params show_confidence=1"
;;
*)
if [[ -f "$1" ]]; then
inputs+=( "$1" )
else
params="$params $1"
fi
;;
esac
shift
done
if [[ ${#inputs[@]} -lt 1 ]]; then
echo "Needs image inputs"
exit 1
fi
# requires a checkpoint
if [[ -z "$cptd" ]] || [[ ! -d "$cptd" ]]; then
echo "Either missing or invalid checkpoint directory"
exit 1
fi
# save command in case we crash
if [[ ! -d checkpoint ]]; then
mkdir checkpoint
fi
echo "$command" >> checkpoint/infer.log
# output info
echo "Using gpu $GPUID"
echo "Parameters: $params"
echo "Files(${#inputs[@]}): ${inputs[@]}"
# create evaluation directories
tmpdir=$(mktemp -d)
realdir="$tmpdir/real/160x160/gray"
mkdir -p "$realdir"
instdir="$tmpdir/instruction"
mkdir "$instdir"
list="$tmpdir/test_real.txt"
i=0
for img in "${inputs[@]}"; do
name="input-$(basename "${img%.jpg}")"
convert "$img" -fx '(r+g+b)/3' -colorspace Gray "$realdir/${name}.jpg"
convert -size 20x20 xc:black -alpha off "$instdir/${name}.png"
echo "$name" >> "$list"
((++i))
done
# copy evaluation to output directory
src_dir="$cptd/eval"
# run actual script
# @see https://github.com/tensorflow/tensorflow/issues/379
CUDA_VISIBLE_DEVICES="$GPUID" python3 main.py $params --notraining --batch_size=1 --dataset="$tmpdir"
# copy output
echo "Tempdir $tmpdir"
i=0
for img in "${inputs[@]}"; do
name=$(basename "${img%.jpg}")
input="input-$(basename "${img%.jpg}")"
cp "$src_dir/${input}.png" "$output_dir/${name}.png"
((++i))
done