Update build_merged_submission.py to support compare-mode 3 (best=first/second/optimized)
Browse files
medal-solvers/build_merged_submission.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
build_merged_submission.py —
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
Usage:
|
| 7 |
python build_merged_submission.py \
|
| 8 |
-
--best-json
|
| 9 |
--first-zip ../submission-base.zip \
|
| 10 |
-
--second-zip ../submission-
|
| 11 |
--optimized-dir optimized \
|
| 12 |
--output /kaggle/working/submission.zip
|
| 13 |
"""
|
|
@@ -19,7 +20,7 @@ import argparse
|
|
| 19 |
|
| 20 |
def main():
|
| 21 |
parser = argparse.ArgumentParser()
|
| 22 |
-
parser.add_argument('--best-json', required=True, help='Output from
|
| 23 |
parser.add_argument('--first-zip', required=True, help='First submission zip')
|
| 24 |
parser.add_argument('--second-zip', required=True, help='Second submission zip')
|
| 25 |
parser.add_argument('--optimized-dir', required=True, help='optimized/ dir with models')
|
|
@@ -40,16 +41,27 @@ def main():
|
|
| 40 |
|
| 41 |
from_first = 0
|
| 42 |
from_second = 0
|
|
|
|
| 43 |
|
| 44 |
with zipfile.ZipFile(args.output, 'w', zipfile.ZIP_DEFLATED) as zf_out:
|
| 45 |
for task_num in range(1, 401):
|
| 46 |
fname = f'task{task_num:03d}.onnx'
|
| 47 |
task_key = str(task_num)
|
| 48 |
-
|
| 49 |
best = results.get(task_key, {}).get('best', 'first')
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
if best == 'second':
|
| 52 |
-
# Use second model
|
| 53 |
try:
|
| 54 |
data = zf_second.read(fname)
|
| 55 |
zf_out.writestr(fname, data)
|
|
@@ -58,31 +70,28 @@ def main():
|
|
| 58 |
except:
|
| 59 |
pass # fallthrough to first
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
if task_num in optimized_tasks:
|
| 63 |
-
opt_path = os.path.join(args.optimized_dir, fname)
|
| 64 |
-
if os.path.exists(opt_path):
|
| 65 |
-
zf_out.write(opt_path, fname)
|
| 66 |
-
from_first += 1
|
| 67 |
-
continue
|
| 68 |
-
|
| 69 |
try:
|
| 70 |
data = zf_first.read(fname)
|
| 71 |
zf_out.writestr(fname, data)
|
| 72 |
from_first += 1
|
| 73 |
except:
|
| 74 |
# Last resort: second
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
zf_first.close()
|
| 80 |
zf_second.close()
|
| 81 |
|
| 82 |
print(f"Created {args.output}")
|
| 83 |
-
print(f" From first:
|
| 84 |
-
print(f" From second:
|
| 85 |
-
print(f"
|
|
|
|
| 86 |
|
| 87 |
|
| 88 |
if __name__ == '__main__':
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
build_merged_submission.py — Build merged submission zip from profile_best_models.py output.
|
| 4 |
+
|
| 5 |
+
Supports compare-mode 2 (best=first|second) and compare-mode 3 (best=first|second|optimized).
|
| 6 |
|
| 7 |
Usage:
|
| 8 |
python build_merged_submission.py \
|
| 9 |
+
--best-json best_per_task.json \
|
| 10 |
--first-zip ../submission-base.zip \
|
| 11 |
+
--second-zip ../submission-v88.zip \
|
| 12 |
--optimized-dir optimized \
|
| 13 |
--output /kaggle/working/submission.zip
|
| 14 |
"""
|
|
|
|
| 20 |
|
| 21 |
def main():
|
| 22 |
parser = argparse.ArgumentParser()
|
| 23 |
+
parser.add_argument('--best-json', required=True, help='Output from profile_best_models.py')
|
| 24 |
parser.add_argument('--first-zip', required=True, help='First submission zip')
|
| 25 |
parser.add_argument('--second-zip', required=True, help='Second submission zip')
|
| 26 |
parser.add_argument('--optimized-dir', required=True, help='optimized/ dir with models')
|
|
|
|
| 41 |
|
| 42 |
from_first = 0
|
| 43 |
from_second = 0
|
| 44 |
+
from_optimized = 0
|
| 45 |
|
| 46 |
with zipfile.ZipFile(args.output, 'w', zipfile.ZIP_DEFLATED) as zf_out:
|
| 47 |
for task_num in range(1, 401):
|
| 48 |
fname = f'task{task_num:03d}.onnx'
|
| 49 |
task_key = str(task_num)
|
| 50 |
+
|
| 51 |
best = results.get(task_key, {}).get('best', 'first')
|
| 52 |
|
| 53 |
+
# --- best = optimized: take from optimized dir ---
|
| 54 |
+
if best == 'optimized':
|
| 55 |
+
opt_path = os.path.join(args.optimized_dir, fname)
|
| 56 |
+
if os.path.exists(opt_path):
|
| 57 |
+
zf_out.write(opt_path, fname)
|
| 58 |
+
from_optimized += 1
|
| 59 |
+
continue
|
| 60 |
+
# fallthrough to first if file missing
|
| 61 |
+
best = 'first'
|
| 62 |
+
|
| 63 |
+
# --- best = second: take from second zip ---
|
| 64 |
if best == 'second':
|
|
|
|
| 65 |
try:
|
| 66 |
data = zf_second.read(fname)
|
| 67 |
zf_out.writestr(fname, data)
|
|
|
|
| 70 |
except:
|
| 71 |
pass # fallthrough to first
|
| 72 |
|
| 73 |
+
# --- best = first: take from first zip ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
try:
|
| 75 |
data = zf_first.read(fname)
|
| 76 |
zf_out.writestr(fname, data)
|
| 77 |
from_first += 1
|
| 78 |
except:
|
| 79 |
# Last resort: second
|
| 80 |
+
try:
|
| 81 |
+
data = zf_second.read(fname)
|
| 82 |
+
zf_out.writestr(fname, data)
|
| 83 |
+
from_second += 1
|
| 84 |
+
except:
|
| 85 |
+
print(f" WARNING: no model found for {fname}")
|
| 86 |
|
| 87 |
zf_first.close()
|
| 88 |
zf_second.close()
|
| 89 |
|
| 90 |
print(f"Created {args.output}")
|
| 91 |
+
print(f" From first: {from_first} models")
|
| 92 |
+
print(f" From second: {from_second} models")
|
| 93 |
+
print(f" From optimized: {from_optimized} models")
|
| 94 |
+
print(f" Total: {from_first + from_second + from_optimized}")
|
| 95 |
|
| 96 |
|
| 97 |
if __name__ == '__main__':
|