-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_seg.sh
More file actions
200 lines (165 loc) · 5.89 KB
/
Copy pathtest_seg.sh
File metadata and controls
200 lines (165 loc) · 5.89 KB
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
#!/usr/bin/env bash
if [ -z "${BASH_VERSION:-}" ]; then
exec /usr/bin/env bash "$0" "$@"
fi
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "${SCRIPT_DIR}"
PYTHON_BIN="${PYTHON_BIN:-python}"
GPU_ID="${GPU_ID:-0}"
export CUDA_VISIBLE_DEVICES="${GPU_ID}"
STOP_ON_ERROR="${STOP_ON_ERROR:-1}"
MODE="${MODE:-test}"
MODEL="${MODEL:-SwinUMamba}"
CHECKPOINT="${CHECKPOINT:-}"
DATA_PATH="${DATA_PATH:-/path/to/segmentation_data}"
IMAGE_SIZE="${IMAGE_SIZE:-512}"
VAL_BATCH_SIZE="${VAL_BATCH_SIZE:-1}"
SEED="${SEED:-2026}"
NUM_WORKERS="${NUM_WORKERS:-4}"
PREFETCH_FACTOR="${PREFETCH_FACTOR:-2}"
PIN_MEMORY="${PIN_MEMORY:-0}"
PERSISTENT_WORKERS="${PERSISTENT_WORKERS:-0}"
USE_COORDS="${USE_COORDS:-1}"
SAVE_NPZ="${SAVE_NPZ:-${SAVE_NPY:-0}}"
SAVE_OVERLAY="${SAVE_OVERLAY:-0}"
SAVE_UNCERTAINTY_OVERLAY="${SAVE_UNCERTAINTY_OVERLAY:-0}"
SAVE_CSV="${SAVE_CSV:-1}"
SAVE_PRED="${SAVE_PRED:-0}"
SAVE_MASK="${SAVE_MASK:-0}"
if [[ "${MODE}" != "test" ]]; then
echo "MODE must be 'test', got: ${MODE}" >&2
exit 1
fi
if [[ "${USE_COORDS}" != "1" ]]; then
echo "main_seg.py currently defaults to use_coords=True and does not expose --no-use_coords." >&2
echo "Please keep USE_COORDS=1 for test_seg.sh." >&2
exit 1
fi
# Batch experiments run sequentially. Format:
# "DisplayName::--model SwinUMamba --checkpoint ./ckpts/your_experiment_dir"
# Leave empty to run a single test with the default variables above.
EXPERIMENTS=(
# "Baseline_BoneSeg_SwinUMamba::--model SwinUMamba --checkpoint ./ckpts/Baseline_BoneSeg_swinumamba_20260331000732 --save_csv"
# "Baseline_BoneSeg_SwinUNETR::--model SwinUNETR --checkpoint ./ckpts/Baseline_BoneSeg_swinunetr_202603291751 --save_csv"
# "Baseline_BoneSeg_TansUNet::--model TransUnet --checkpoint ./ckpts/Baseline_BoneSeg_transunet_20260401113613 --save_csv"
# "Baseline_BoneSeg_UMambaEnc::--model UMambaEnc --checkpoint ./ckpts/Baseline_BoneSeg_umambaenc_20260331224356 --save_csv"
# "Baseline_BoneSeg_Unet::--model Unet --checkpoint ./ckpts/Baseline_BoneSeg_unet_20260401195552 --save_csv"
# "Baseline_BoneSeg_Unet++::--model Unet++ --checkpoint ./ckpts/Baseline_BoneSeg_unet++_20260402182806 --save_csv --save_npz --save_overlay"
# "Baseline_BoneSeg_MambaVision::--model MambaVisionT --checkpoint ./ckpts/Baseline_BoneSeg_mambavisiont_20260407035837 --save_csv --save_npz --save_overlay"
"Baseline_BoneSeg_SegFormer::--model SegFormer --checkpoint ./ckpts/Baseline_BoneSeg_segformer_20260410031019 --save_csv --save_npz --save_overlay"
)
has_checkpoint_arg() {
local arg
for arg in "$@"; do
if [[ "${arg}" == "--checkpoint" ]]; then
return 0
fi
done
return 1
}
run_experiment() {
local exp_name="$1"
shift
if [[ -z "${CHECKPOINT}" ]] && ! has_checkpoint_arg "$@"; then
echo "Please set CHECKPOINT or add --checkpoint in the experiment args." >&2
return 1
fi
local cmd=(
"${PYTHON_BIN}"
main_seg.py
--launcher none
--mode "${MODE}"
--model "${MODEL}"
--checkpoint "${CHECKPOINT}"
--data_path "${DATA_PATH}"
--image_size "${IMAGE_SIZE}"
--val_batch_size "${VAL_BATCH_SIZE}"
--seed "${SEED}"
--num_workers "${NUM_WORKERS}"
--prefetch_factor "${PREFETCH_FACTOR}"
--use_coords
)
if [[ "${PIN_MEMORY}" == "1" ]]; then
cmd+=(--pin_memory)
else
cmd+=(--no-pin_memory)
fi
if [[ "${PERSISTENT_WORKERS}" == "1" ]]; then
cmd+=(--persistent_workers)
else
cmd+=(--no-persistent_workers)
fi
if [[ "${SAVE_NPZ}" == "1" ]]; then
cmd+=(--save_npz)
fi
if [[ "${SAVE_OVERLAY}" == "1" ]]; then
cmd+=(--save_overlay)
fi
if [[ "${SAVE_UNCERTAINTY_OVERLAY}" == "1" ]]; then
cmd+=(--save_uncertainty_overlay)
fi
if [[ "${SAVE_CSV}" == "1" ]]; then
cmd+=(--save_csv)
fi
if [[ "${SAVE_PRED}" == "1" ]]; then
cmd+=(--save_pred)
fi
if [[ "${SAVE_MASK}" == "1" ]]; then
cmd+=(--save_mask)
fi
cmd+=("$@")
echo "=================================================="
echo "Launching bone segmentation test: ${exp_name:-single}"
echo "GPU_ID=${GPU_ID}"
echo "MODEL=${MODEL}"
echo "DATA_PATH=${DATA_PATH}"
echo "Command: ${cmd[*]}"
echo "=================================================="
"${cmd[@]}"
}
if [[ "${#EXPERIMENTS[@]}" -eq 0 ]]; then
echo "Launching single bone segmentation test"
echo "GPU_ID=${GPU_ID}"
echo "MODEL=${MODEL}"
echo "CHECKPOINT=${CHECKPOINT}"
echo "DATA_PATH=${DATA_PATH}"
run_experiment "" "$@"
exit 0
fi
echo "Launching ${#EXPERIMENTS[@]} bone segmentation tests sequentially"
echo "GPU_ID=${GPU_ID}"
echo "Fallback MODEL=${MODEL}"
echo "Fallback CHECKPOINT=${CHECKPOINT}"
echo "DATA_PATH=${DATA_PATH}"
failed_experiments=()
for idx in "${!EXPERIMENTS[@]}"; do
spec="${EXPERIMENTS[idx]}"
exp_idx=$((idx + 1))
exp_name="exp$(printf '%02d' "${exp_idx}")"
exp_args_str="${spec}"
if [[ "${spec}" == *"::"* ]]; then
exp_name="${spec%%::*}"
exp_args_str="${spec#*::}"
fi
if [[ -z "${exp_name}" ]]; then
exp_name="exp$(printf '%02d' "${exp_idx}")"
fi
read -r -a exp_args <<< "${exp_args_str}"
echo "[${exp_idx}/${#EXPERIMENTS[@]}] Starting ${exp_name}"
if run_experiment "${exp_name}" "${exp_args[@]}" "$@"; then
echo "[${exp_idx}/${#EXPERIMENTS[@]}] Finished ${exp_name}"
else
echo "[${exp_idx}/${#EXPERIMENTS[@]}] Failed ${exp_name}"
failed_experiments+=("${exp_name}")
if [[ "${STOP_ON_ERROR}" == "1" ]]; then
echo "STOP_ON_ERROR=1, stopping batch run."
exit 1
fi
fi
done
if [[ "${#failed_experiments[@]}" -gt 0 ]]; then
echo "Batch run finished with failures: ${failed_experiments[*]}"
exit 1
fi
echo "All bone segmentation tests finished successfully."