-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjob_eval_swin.sh
More file actions
executable file
·172 lines (148 loc) · 5.16 KB
/
Copy pathjob_eval_swin.sh
File metadata and controls
executable file
·172 lines (148 loc) · 5.16 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
#!/bin/bash -l
#SBATCH --job-name=ThinkingViT-Swin-eval
#SBATCH --nodes=1
#SBATCH --tasks-per-node=1
#SBATCH --cpus-per-task=16
#SBATCH --mem=128000
#SBATCH --time=1-00:00:00
#SBATCH --output=eval_swin.out
#SBATCH --error=eval_swin.err
#SBATCH --partition=long
#SBATCH --gres=gpu:L40:1
set -euo pipefail
if ! command -v module &> /dev/null; then
set +u
source /etc/profile || true
set -u
fi
for MODULE_INIT in /etc/profile.d/modules.sh /etc/profile.d/lmod.sh /usr/share/Modules/init/bash /usr/share/lmod/lmod/init/bash; do
if ! command -v module &> /dev/null && [ -f "${MODULE_INIT}" ]; then
set +u
source "${MODULE_INIT}" || true
set -u
fi
done
if command -v module &> /dev/null; then
module load gpu-env
module load cuda
module load gcc12-env
module load python
else
echo "Warning: environment module command not found; continuing with the current environment."
fi
source /home/aho/envs/env/bin/activate
cd /home/aho/ThinkingViTCVPR/ThinkingViT
CHECKPOINT="/home/aho/ThinkingViTCVPR/ThinkingViT/ThinkingViTSwin.pth.tar"
DATA_DIR="/data22/datasets/ilsvrc2012/"
BATCH_SIZE=128
MODEL="swin_small_patch4_window7_224"
HEAD_ROUND_1=(3 3 6 12)
HEAD_ROUND_2=(3 6 12 24)
EMA_FLAG="--use-ema"
# Edit this list to choose the thresholds to evaluate.
THRESHOLDS=(0.0 0.1 0.2 0.3 0.5 0.8 1.0 1.2 1.4 1.6 2 5)
LOG_DIR="eval_logs_swin"
mkdir -p "${LOG_DIR}"
rm -f "${LOG_DIR}"/threshold_*.log "${LOG_DIR}/summary.md"
for THRESHOLD in "${THRESHOLDS[@]}"; do
SAFE_THRESHOLD="${THRESHOLD//./p}"
echo "Evaluating threshold ${THRESHOLD}"
srun python validate_swin.py \
--model "${MODEL}" \
--checkpoint "${CHECKPOINT}" \
--data-dir "${DATA_DIR}" \
--batch-size "${BATCH_SIZE}" \
${EMA_FLAG} \
--head-round-1 "${HEAD_ROUND_1[@]}" \
--head-round-2 "${HEAD_ROUND_2[@]}" \
--threshold "${THRESHOLD}" \
&> "${LOG_DIR}/threshold_${SAFE_THRESHOLD}.log"
done
python - "${LOG_DIR}" "${CHECKPOINT}" "${MODEL}" "${HEAD_ROUND_1[*]} | ${HEAD_ROUND_2[*]}" <<'PY'
import json
import re
import sys
from datetime import datetime
from pathlib import Path
log_dir = Path(sys.argv[1])
checkpoint = sys.argv[2]
model = sys.argv[3]
head_rounds = sys.argv[4]
rows = []
max_stages = 0
for log_path in sorted(log_dir.glob("threshold_*.log")):
text = log_path.read_text(errors="replace")
threshold_match = re.search(r"Entropy Threshold:\s*([0-9.]+)", text)
top1_match = re.search(r"\*\s+Acc@1\s+([0-9.]+)", text)
top5_match = re.search(r"Acc@5\s+([0-9.]+)", text)
flops_match = re.search(r"Average FLOPs per sample\s*:\s*([0-9.]+)\s+GFLOPs", text)
result_match = re.search(r"--result\s*(\{.*?\})\s*$", text, re.DOTALL)
top1 = top1_match.group(1) if top1_match else ""
top5 = top5_match.group(1) if top5_match else ""
if result_match:
try:
result = json.loads(result_match.group(1))
top1 = str(result.get("top1", top1))
top5 = str(result.get("top5", top5))
except json.JSONDecodeError:
pass
dispatches = re.findall(
r"Stage\s+\d+\s+\(([^)]+)\)\s+dispatched:\s*([0-9.]+)%\s+\((\d+)/(\d+)\)",
text,
)
stage_accs = re.findall(
r"Stage\s+\d+\s+accuracy:\s*([0-9.]+)%\s+\((\d+)/(\d+)\)",
text,
)
max_stages = max(max_stages, len(dispatches), len(stage_accs))
threshold = threshold_match.group(1) if threshold_match else log_path.stem.replace("threshold_", "").replace("p", ".")
rows.append({
"threshold": threshold,
"top1": top1,
"top5": top5,
"flops": flops_match.group(1) if flops_match else "",
"dispatches": dispatches,
"stage_accs": stage_accs,
"log": log_path.name,
})
rows.sort(key=lambda row: float(row["threshold"]))
headers = ["Threshold", "Top-1", "Top-5", "Avg GFLOPs"]
for idx in range(max_stages):
headers.append(f"Stage {idx + 1} Dispatch")
for idx in range(max_stages):
headers.append(f"Stage {idx + 1} Acc")
headers.append("Log")
lines = [
"# ThinkingViT-Swin Evaluation Summary",
"",
f"- Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
f"- Model: `{model}`",
f"- Head rounds: `{head_rounds}`",
f"- Checkpoint: `{checkpoint}`",
"",
"|" + "|".join(headers) + "|",
"|" + "|".join(["---"] * len(headers)) + "|",
]
for row in rows:
cells = [row["threshold"], row["top1"], row["top5"], row["flops"]]
for idx in range(max_stages):
if idx < len(row["dispatches"]):
label, pct, count, total = row["dispatches"][idx]
cells.append(f"{label}: {pct}% ({count}/{total})")
else:
cells.append("")
for idx in range(max_stages):
if idx < len(row["stage_accs"]):
pct, correct, total = row["stage_accs"][idx]
cells.append(f"{pct}% ({correct}/{total})")
else:
cells.append("")
cells.append(f"[{row['log']}](./{row['log']})")
lines.append("|" + "|".join(cells) + "|")
report_path = log_dir / "summary.md"
report_path.write_text("\n".join(lines) + "\n")
print(f"Wrote evaluation summary to {report_path}")
PY
if command -v jobinfo &> /dev/null; then
jobinfo
fi