-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpipeline_start_end_split.sh
executable file
·234 lines (193 loc) · 6.73 KB
/
pipeline_start_end_split.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
#!/bin/bash
# Created by argbash-init v2.10.0
# ARG_OPTIONAL_SINGLE([gpu],[g],[Specify which GPU to run on],[all])
# ARG_POSITIONAL_SINGLE([input],[Input directory containing images])
# ARG_POSITIONAL_SINGLE([output],[Output directory with images and pagexml])
# ARG_DEFAULTS_POS([])
# ARG_HELP([<Script to run all steps of the baseline/region pipeline, with start and end in seperate images>])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.10.0 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo "$1" >&2
exit "${_ret}"
}
begins_with_short_option()
{
local first_option all_short_options='gh'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
# THE DEFAULTS INITIALIZATION - POSITIONALS
_positionals=()
_arg_input=
_arg_output=
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_gpu="all"
print_help()
{
printf '%s\n' "<Script to run all steps of the baseline/region pipeline, with start and end in seperate images>"
printf 'Usage: %s [-g|--gpu <arg>] [-h|--help] <input> <output>\n' "$0"
printf '\t%s\n' "<input>: Input directory containing images"
printf '\t%s\n' "<output>: Output directory with images and pagexml"
printf '\t%s\n' "-g, --gpu: Specify which GPU to run on (default: 'all')"
printf '\t%s\n' "-h, --help: Prints help"
}
parse_commandline()
{
_positionals_count=0
while test $# -gt 0
do
_key="$1"
case "$_key" in
-g|--gpu)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_gpu="$2"
shift
;;
--gpu=*)
_arg_gpu="${_key##--gpu=}"
;;
-g*)
_arg_gpu="${_key##-g}"
;;
-h|--help)
print_help
exit 0
;;
-h*)
print_help
exit 0
;;
*)
_last_positional="$1"
_positionals+=("$_last_positional")
_positionals_count=$((_positionals_count + 1))
;;
esac
shift
done
}
handle_passed_args_count()
{
local _required_args_string="'input' and 'output'"
test "${_positionals_count}" -ge 2 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require exactly 2 (namely: $_required_args_string), but got only ${_positionals_count}." 1
test "${_positionals_count}" -le 2 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect exactly 2 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}')." 1
}
assign_positional_args()
{
local _positional_name _shift_for=$1
_positional_names="_arg_input _arg_output "
shift "$_shift_for"
for _positional_name in ${_positional_names}
do
test $# -gt 0 || break
eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
shift
done
}
parse_commandline "$@"
handle_passed_args_count
assign_positional_args 1 "${_positionals[@]}"
# OTHER STUFF GENERATED BY Argbash
### END OF CODE GENERATED BY Argbash (sortof) ### ])
# [ <-- needed because of Argbash
if !(docker -v &> /dev/null); then
echo "Docker is not installed please follow https://docs.docker.com/engine/install/"
exit 1
fi
if !(docker image inspect loghi/docker.loghi-tooling:latest &> /dev/null); then
echo "Loghi tooling is not installed please follow https://github.com/MMaas3/dockerize-images to install"
exit 1
fi
if !(docker image inspect loghi/docker.laypa:latest &> /dev/null); then
echo "Laypa is not installed please follow https://github.com/MMaas3/dockerize-images to install"
exit 1
fi
# tmpdir=$(mktemp -d)
input_dir=$(realpath $_arg_input)
tmp_dir=$(realpath $(mktemp -d))
baseline=$tmp_dir/baseline
start=$tmp_dir/start
end=$tmp_dir/end
output_dir=$(realpath $_arg_output)
# input_dir=/home/stefan/Documents/test
# output_dir=/home/stefan/Documents/test2
if [[ ! -d $input_dir ]]; then
echo "Specified input dir (${input_dir}) does not exist, stopping program"
exit 1
fi
if [[ ! -d $output_dir ]]; then
echo "Could not find output dir (${output_dir}), creating one at specified location"
mkdir -p $output_dir
fi
GPU=$_arg_gpu
DOCKERGPUPARAMS=""
if [[ $GPU -gt -1 ]]; then
DOCKERGPUPARAMS="--gpus device=${GPU}"
echo "using GPU ${GPU}"
fi
docker run $DOCKERGPUPARAMS --shm-size 8G --rm -it -m 32000m -v $input_dir:$input_dir -v $tmp_dir:$tmp_dir loghi/docker.laypa:latest \
python run.py \
-c configs/segmentation/baseline/baseline_dataset.yaml \
-i $input_dir \
-o $tmp_dir/baseline \
--opts MODEL.WEIGHTS "" TEST.WEIGHTS pretrained_models/baseline_model_best_mIoU.pth
# > /dev/null
if [[ $? -ne 0 ]]; then
echo "Baseline detection has errored, stopping program"
exit 1
fi
docker run $DOCKERGPUPARAMS --shm-size 8G --rm -it -m 32000m -v $input_dir:$input_dir -v $tmp_dir:$tmp_dir loghi/docker.laypa:latest \
python run.py \
-c configs/segmentation/start/start_dataset.yaml \
-i $input_dir \
-o $tmp_dir/start \
--opts MODEL.WEIGHTS "" TEST.WEIGHTS pretrained_models/start_model_best_mIoU.pth
# > /dev/null
if [[ $? -ne 0 ]]; then
echo "Start detection has errored, stopping program"
exit 1
fi
docker run $DOCKERGPUPARAMS --shm-size 8G --rm -it -m 32000m -v $input_dir:$input_dir -v $tmp_dir:$tmp_dir loghi/docker.laypa:latest \
python run.py \
-c configs/segmentation/end/end_dataset.yaml \
-i $input_dir \
-o $tmp_dir/end \
--opts MODEL.WEIGHTS "" TEST.WEIGHTS pretrained_models/end_model_best_mIoU.pth
# > /dev/null
if [[ $? -ne 0 ]]; then
echo "End detection has errored, stopping program"
exit 1
fi
cp -P $tmp_dir/baseline/* $output_dir
# Just used for debugging right now
docker run $DOCKERGPUPARAMS --shm-size 8G --rm -it -m 32000m -v $output_dir:$output_dir -v $tmp_dir:$tmp_dir loghi/docker.laypa:latest \
python utils/combine_start_end.py \
--baseline $tmp_dir/baseline/page/ \
--start $tmp_dir/start/page/ \
--end $tmp_dir/end/page/ \
--output $output_dir/page/
# > /dev/null
if [[ $? -ne 0 ]]; then
echo "Combining images has errored, stopping program"
exit 1
fi
docker run --rm -v $output_dir:$output_dir -v $tmp_dir:$tmp_dir loghi/docker.loghi-tooling:latest /src/loghi-tooling/minions/target/appassembler/bin/MinionExtractBaselinesStartEndNew \
-input_path_png $tmp_dir/baseline/page/ \
-input_path_png_start $tmp_dir/start/page/ \
-input_path_png_end $tmp_dir/end/page/ \
-input_path_pagexml $tmp_dir/baseline/page/ \
-output_path_pagexml $output_dir/page/ \
-as_single_region true
if [[ $? -ne 0 ]]; then
echo "Extract baselines has errored, stopping program"
exit 1
fi
# rm -r $tmp_dir
# ] <-- needed because of Argbash