-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_train.py
More file actions
1420 lines (1217 loc) · 59.8 KB
/
admin_train.py
File metadata and controls
1420 lines (1217 loc) · 59.8 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
admin_train.py — WARP Central Model Trainer
============================================
Trains two models from community-contributed data:
1. icon_classifier (EfficientNet-B0) — from confirmed icon crops
staging/<install_id>/crops/<sha>.png + annotations.jsonl
2. screen_classifier (MobileNetV3-Small) — from confirmed screen type screenshots
staging/<install_id>/screen_types/<TYPE>/<sha>.png
Democratic voting: 1 install_id = 1 vote per sha, majority label wins.
Both models uploaded to sets-sto/warp-knowledge/models/.
Requires torch, torchvision, cv2 — installed in the sets-warp venv, not here.
Run from the sets-warp directory:
.venv/bin/python ../sets-warp-backend/admin_train.py
.venv/bin/python ../sets-warp-backend/admin_train.py --train --min 1
Environment variables (.env in this directory):
HF_TOKEN — HF write token (write access to both repos)
HF_DATASET — training crops repo (default: sets-sto/sto-icon-dataset)
HF_REPO_ID — model output repo (default: sets-sto/warp-knowledge)
"""
from __future__ import annotations
import argparse
import hashlib
import json
import logging
import os
import sys
import tempfile
import time
from collections import Counter, defaultdict
from datetime import datetime, timezone
from pathlib import Path
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
log = logging.getLogger(__name__)
UTC = timezone.utc
# ── Load .env ─────────────────────────────────────────────────────────────────
def _load_env():
for candidate in [Path(__file__).parent / '.env', Path(__file__).parent.parent / '.env']:
if candidate.exists():
for line in candidate.read_text().splitlines():
line = line.strip()
if line and not line.startswith('#') and '=' in line:
k, v = line.split('=', 1)
os.environ.setdefault(k.strip(), v.strip())
break
_load_env()
HF_TOKEN = os.environ.get('HF_TOKEN', '')
HF_DATASET = os.environ.get('HF_DATASET', 'sets-sto/sto-icon-dataset')
HF_REPO_ID = os.environ.get('HF_REPO_ID', 'sets-sto/warp-knowledge')
# ── Training hyper-parameters (mirror local_trainer.py) ──────────────────────
IMG_SIZE = 64
MODEL_IMG_SIZE = 224
BATCH_SIZE = 16
MAX_EPOCHS = 30
LR = 3e-4
PATIENCE = 5
FOCAL_GAMMA = 2.0
MIN_SAMPLES = 5 # require at least 5 total crops to bother training
MIN_NEW_CROPS = 10 # minimum new crops since last training to bother retraining
# Screen classifier hyper-parameters (MobileNetV3-Small)
SC_IMG_SIZE = 224
SC_BATCH_SIZE = 8
SC_MAX_EPOCHS = 40
SC_LR = 3e-4
SC_PATIENCE = 8
SC_MIN_SAMPLES = 7 # at least 7 screenshots total to bother training
SC_MIN_CLASS_SAMPLES = 5 # drop a class from training if it has fewer than this many samples
SC_MIN_KEEP = 30 # per screen-type: below this count keep all samples
SC_MAX_KEEP = 150 # per screen-type: above SC_MIN_KEEP cap to this many
SCREEN_TYPES = [
'SPACE_EQ', 'GROUND_EQ', 'TRAITS',
'BOFFS', 'SPECIALIZATIONS', 'SPACE_MIXED', 'GROUND_MIXED',
]
# ── HF helpers ────────────────────────────────────────────────────────────────
def _require_hf():
try:
from huggingface_hub import HfApi, hf_hub_download # noqa
except ImportError:
print('ERROR: pip install huggingface-hub', file=sys.stderr)
sys.exit(1)
if not HF_TOKEN:
print('ERROR: HF_TOKEN not set', file=sys.stderr)
sys.exit(1)
def _list_staging_folders() -> list[str]:
"""Return list of install_id staging folder names."""
from huggingface_hub import HfApi
from huggingface_hub.hf_api import RepoFolder
api = HfApi(token=HF_TOKEN)
try:
# Optimization: list only the 'staging/' directory non-recursively
elements = api.list_repo_tree(HF_DATASET, path_in_repo='staging', repo_type='dataset', recursive=False)
folders = [e.path.split('/')[-1] for e in elements if isinstance(e, RepoFolder)]
if folders:
return sorted(folders)
except Exception as e:
log.warning(f"list_repo_tree('staging') failed: {e}. Falling back to full list.")
# Fallback to the old method (might timeout on large repos)
files = list(api.list_repo_files(HF_DATASET, repo_type='dataset'))
folders = {
f.split('/')[1]
for f in files
if f.startswith('staging/') and '/' in f[len('staging/'):]
}
return sorted(folders)
def _load_staging_annotations(install_id: str) -> list[dict]:
"""Download and parse staging/<install_id>/annotations.jsonl."""
from huggingface_hub import hf_hub_download
path_in_repo = f'staging/{install_id}/annotations.jsonl'
try:
local = hf_hub_download(
HF_DATASET, path_in_repo, repo_type='dataset', token=HF_TOKEN
)
entries = []
for line in Path(local).read_text(encoding='utf-8').splitlines():
line = line.strip()
if line:
try:
entries.append(json.loads(line))
except Exception:
pass
return entries
except Exception as e:
log.debug(f'No annotations for {install_id}: {e}')
return []
def _download_crop(install_id: str, sha: str, dest_dir: Path) -> Path | None:
"""Download staging/<install_id>/crops/<sha>.png to dest_dir. Returns local path."""
from huggingface_hub import hf_hub_download
dest = dest_dir / sha
if dest.exists():
return dest
try:
local = hf_hub_download(
HF_DATASET,
f'staging/{install_id}/crops/{sha}.png',
repo_type='dataset',
token=HF_TOKEN,
)
import shutil
shutil.copy2(local, dest)
return dest
except Exception as e:
log.debug(f'Crop {sha} from {install_id} missing: {e}')
return None
def _create_commit_with_retry(api, repo_id: str, repo_type: str,
operations: list, commit_message: str,
max_retries: int = 3) -> bool:
"""Call api.create_commit with retry on 429 rate-limit errors."""
import re
for attempt in range(max_retries):
try:
api.create_commit(
repo_id=repo_id,
repo_type=repo_type,
operations=operations,
commit_message=commit_message,
)
return True
except Exception as e:
msg = str(e)
if '429' in msg or 'rate limit' in msg.lower():
# Parse suggested wait time from HF error ("Retry after N seconds")
m = re.search(r'[Rr]etry after (\d+)', msg)
wait = int(m.group(1)) + 10 if m else 150
if attempt < max_retries - 1:
log.warning(f'HF rate limit hit — waiting {wait}s before retry '
f'({attempt + 1}/{max_retries - 1})…')
time.sleep(wait)
continue
log.error(f'Upload failed: {e}')
return False
return False
def _upload_model(models_dir: Path, n_classes: int, val_acc: float,
n_samples: int, n_users: int,
sc_val_acc: float | None = None,
sc_n_samples: int = 0) -> bool:
"""Upload icon + screen model files to sets-sto/warp-knowledge under models/."""
from huggingface_hub import HfApi, CommitOperationAdd
api = HfApi(token=HF_TOKEN)
pt_path = models_dir / 'icon_classifier.pt'
label_path = models_dir / 'label_map.json'
meta_path = models_dir / 'icon_classifier_meta.json'
if not pt_path.exists():
log.error('icon_classifier.pt not found — nothing to upload')
return False
# Compute version hash (sha256 of icon model file, first 16 hex chars)
sha = hashlib.sha256(pt_path.read_bytes()).hexdigest()[:16]
trained_at = datetime.now(UTC).isoformat() + 'Z'
version_data = {
'version': sha,
'trained_at': trained_at,
'n_classes': n_classes,
'val_acc': round(val_acc, 4),
'n_samples': n_samples,
'n_users': n_users,
}
if sc_val_acc is not None:
version_data['screen_trained_at'] = trained_at
version_data['screen_val_acc'] = round(sc_val_acc, 4)
version_data['screen_n_samples'] = sc_n_samples
version_path = models_dir / 'model_version.json'
version_path.write_text(json.dumps(version_data, indent=2), encoding='utf-8')
manifest_path = models_dir / 'training_manifest.json'
ops = [
CommitOperationAdd(path_in_repo='models/icon_classifier.pt', path_or_fileobj=str(pt_path)),
CommitOperationAdd(path_in_repo='models/label_map.json', path_or_fileobj=str(label_path)),
CommitOperationAdd(path_in_repo='models/icon_classifier_meta.json', path_or_fileobj=str(meta_path)),
CommitOperationAdd(path_in_repo='models/model_version.json', path_or_fileobj=str(version_path)),
]
if manifest_path.exists():
ops.append(CommitOperationAdd(
path_in_repo='models/training_manifest.json',
path_or_fileobj=str(manifest_path),
))
# Include screen classifier if trained
sc_pt = models_dir / 'screen_classifier.pt'
sc_labels = models_dir / 'screen_classifier_labels.json'
if sc_pt.exists():
ops.append(CommitOperationAdd(path_in_repo='models/screen_classifier.pt', path_or_fileobj=str(sc_pt)))
if sc_labels.exists():
ops.append(CommitOperationAdd(path_in_repo='models/screen_classifier_labels.json', path_or_fileobj=str(sc_labels)))
commit_msg = (f'admin_train: icon {n_classes}cls val={val_acc:.1%}'
+ (f', screen val={sc_val_acc:.1%}' if sc_val_acc is not None else '')
+ f' ({trained_at[:10]})')
ok = _create_commit_with_retry(api, HF_REPO_ID, 'dataset', ops, commit_msg)
if ok:
log.info(f'Model uploaded to {HF_REPO_ID}: version={sha}, val_acc={val_acc:.1%}')
return ok
# ── Community anchors (P11) ──────────────────────────────────────────────────
def _list_anchor_grid_files(folders: list[str]) -> list[tuple[str, str]]:
"""Return list of (install_id, filename) for all anchors_grid_*.json in staging."""
from huggingface_hub import HfApi
api = HfApi(token=HF_TOKEN)
result = []
for iid in folders:
try:
tree = api.list_repo_tree(
HF_DATASET, path_in_repo=f'staging/{iid}',
repo_type='dataset', recursive=False,
)
for item in tree:
p = getattr(item, 'path', '')
fname = p.split('/')[-1]
if fname.startswith('anchors_grid_') and fname.endswith('.json'):
result.append((iid, fname))
except Exception:
pass
return result
def _download_anchor_grid(install_id: str, filename: str) -> dict | None:
"""Download staging/<install_id>/<filename> and return parsed dict."""
try:
from huggingface_hub import hf_hub_download
local = hf_hub_download(
repo_id=HF_DATASET,
filename=f'staging/{install_id}/{filename}',
repo_type='dataset',
token=HF_TOKEN,
)
return json.loads(Path(local).read_text(encoding='utf-8'))
except Exception as e:
log.debug(f'anchor grid {install_id}/{filename} unavailable: {e}')
return None
def build_community_anchors(folders: list[str], min_contributors: int = 2) -> list[dict]:
"""
Aggregate anchor grids from all staging folders.
Returns list of community anchor entries (same format as anchors.json learned entries).
Accepts groups with exactly 1 contributor (no conflict) or >= min_contributors (consensus).
Skips groups with 2..min_contributors-1 (ambiguous — some data but not enough for consensus).
With min_contributors=2, all groups n>=1 are accepted (n>1 and n<2 is never true).
"""
from collections import defaultdict
import statistics
grid_files = _list_anchor_grid_files(folders)
print(f'Found {len(grid_files)} anchor grid file(s) across {len(folders)} user(s).')
if not grid_files:
return []
# {(build_type, aspect_bucket): {install_id: [grid_entry, ...]}}
groups: dict[tuple, dict[str, list]] = defaultdict(lambda: defaultdict(list))
for install_id, filename in grid_files:
entry = _download_anchor_grid(install_id, filename)
if not entry:
continue
build_type = entry.get('build_type', '')
aspect = entry.get('aspect')
if not build_type or aspect is None:
continue
# Bucket aspect to 2 decimal places for grouping
aspect_bucket = round(float(aspect), 2)
groups[(build_type, aspect_bucket)][install_id].append(entry)
results = []
for (build_type, aspect_bucket), contributors in groups.items():
n = len(contributors)
# Accept sole contributor (no conflict) or consensus (>= min_contributors).
# Skip only when 2..min_contributors-1: some data but not enough for reliable consensus.
if n > 1 and n < min_contributors:
print(f' Skipping {build_type} aspect={aspect_bucket}: '
f'{n} contributor(s) < {min_contributors} required')
continue
# Collect all slot data across all contributors
slot_vectors: dict[str, list[dict]] = defaultdict(list)
resolutions: list[str] = []
for iid, entries in contributors.items():
for e in entries:
slots = e.get('slots', {})
for slot_name, geo in slots.items():
if isinstance(geo, dict):
slot_vectors[slot_name].append(geo)
if e.get('resolution'):
resolutions.append(e['resolution'])
# Median per slot per component
aggregated_slots = {}
for slot_name, geos in slot_vectors.items():
if len(geos) < 1:
continue
def _med(key):
vals = [g[key] for g in geos if key in g]
return round(statistics.median(vals), 5) if vals else None
entry_out = {
'x0_rel': _med('x0_rel'),
'y_rel': _med('y_rel'),
'w_rel': _med('w_rel'),
'h_rel': _med('h_rel'),
'step_rel': _med('step_rel'),
'count': round(statistics.median(counts)) if (counts := [g['count'] for g in geos if 'count' in g]) else 1,
}
if None not in entry_out.values():
aggregated_slots[slot_name] = entry_out
if not aggregated_slots:
continue
# Pick most common resolution as representative
rep_res = max(set(resolutions), key=resolutions.count) if resolutions else ''
results.append({
'type': build_type,
'aspect': aspect_bucket,
'res': rep_res,
'slots': aggregated_slots,
'n_contributors': len(contributors),
'timestamp': int(__import__('time').time()),
})
print(f' Community anchor: {build_type} aspect={aspect_bucket} '
f'({len(contributors)} contributors, {len(aggregated_slots)} slots)')
return results
def upload_community_anchors(entries: list[dict], models_dir: Path) -> bool:
"""Write community_anchors.json and upload to HF knowledge repo."""
from huggingface_hub import HfApi, CommitOperationAdd
from datetime import datetime, timezone
import io
payload = {
'generated_at': datetime.now(timezone.utc).isoformat() + 'Z',
'n_contributors': max((e['n_contributors'] for e in entries), default=0),
'entries': entries,
}
payload_bytes = json.dumps(payload, indent=2, ensure_ascii=False).encode('utf-8')
# Save locally for reference
local_path = models_dir / 'community_anchors.json'
local_path.write_bytes(payload_bytes)
api = HfApi(token=HF_TOKEN)
ok = _create_commit_with_retry(
api, HF_REPO_ID, 'dataset',
[CommitOperationAdd(path_in_repo='models/community_anchors.json',
path_or_fileobj=io.BytesIO(payload_bytes))],
f'community anchors: {len(entries)} entries',
)
if ok:
log.info(f'community_anchors.json uploaded ({len(entries)} entries)')
return ok
# ── Ship Type / Tier OCR correction map ──────────────────────────────────────
def collect_text_corrections(staging_folders: list[str], models_dir: Path) -> None:
"""
Build and upload ship_type_corrections.json from Ship Type / Ship Tier annotations.
For each (ml_name, name) pair where ml_name != '' and ml_name != name:
- 1 vote per install_id
- majority winner per ml_name key → corrections dict
Uploads to HF as models/ship_type_corrections.json.
"""
from huggingface_hub import HfApi, CommitOperationAdd
_TEXT_LEARNING_SLOTS = {'Ship Type', 'Ship Tier'}
# ml_name -> {install_id -> corrected_name}
votes: dict[str, dict[str, str]] = defaultdict(dict)
for iid in staging_folders:
anns = _load_staging_annotations(iid)
for entry in anns:
if entry.get('slot') not in _TEXT_LEARNING_SLOTS:
continue
ml_name = entry.get('ml_name', '').strip()
name = entry.get('name', '').strip()
if ml_name and name and ml_name != name:
votes[ml_name][iid] = name # 1 install_id = 1 vote
if not votes:
print(' No OCR correction pairs found — ship_type_corrections.json not updated.')
return
corrections: dict[str, str] = {}
for ml_name, iid_votes in votes.items():
label_counts = Counter(iid_votes.values())
winner, _ = label_counts.most_common(1)[0]
corrections[ml_name] = winner
print(f' Built {len(corrections)} OCR correction(s).')
payload_bytes = json.dumps(corrections, indent=2, ensure_ascii=False).encode('utf-8')
# Save locally for reference
local_path = models_dir / 'ship_type_corrections.json'
local_path.write_bytes(payload_bytes)
api = HfApi(token=HF_TOKEN)
ok = _create_commit_with_retry(
api, HF_REPO_ID, 'dataset',
[CommitOperationAdd(path_in_repo='models/ship_type_corrections.json',
path_or_fileobj=local_path)],
f'ship_type_corrections: {len(corrections)} entries',
)
if ok:
log.info(f'ship_type_corrections.json uploaded ({len(corrections)} entries)')
# ── Screen classifier helpers ─────────────────────────────────────────────────
def _list_screen_type_files(folders: list[str]) -> list[tuple[str, str, str]]:
"""
Return list of (install_id, stype, sha) for all screen type PNGs in staging.
Path format: staging/<install_id>/screen_types/<stype>/<sha>.png
"""
from huggingface_hub import HfApi
api = HfApi(token=HF_TOKEN)
result = []
# Optimization: instead of listing the whole repo, list each staging/<id>/screen_types
for iid in folders:
try:
path = f'staging/{iid}/screen_types'
# recursive=True here is fine because we are limited to one user's screen_types
elements = api.list_repo_tree(HF_DATASET, path_in_repo=path, repo_type='dataset', recursive=True)
for e in elements:
# e.path: staging/<iid>/screen_types/<stype>/<sha>.png
parts = e.path.split('/')
if len(parts) == 5 and e.path.endswith('.png'):
stype = parts[3]
sha = parts[4][:-4] # strip .png
result.append((iid, stype, sha))
except Exception:
# Folder might not exist for this user
continue
if result:
return result
# Fallback (slow, might timeout on large repos)
files = list(api.list_repo_files(HF_DATASET, repo_type='dataset'))
for f in files:
# staging/<install_id>/screen_types/<stype>/<sha>.png
parts = f.split('/')
if len(parts) == 5 and parts[0] == 'staging' and parts[2] == 'screen_types' and f.endswith('.png'):
install_id = parts[1]
stype = parts[3]
sha = parts[4][:-4] # strip .png
result.append((install_id, stype, sha))
return result
def _download_screen_shot(install_id: str, stype: str, sha: str, dest_dir: Path) -> Path | None:
"""Download staging/<install_id>/screen_types/<stype>/<sha>.png to dest_dir."""
from huggingface_hub import hf_hub_download
dest = dest_dir / f'{sha}.png'
if dest.exists():
return dest
try:
local = hf_hub_download(
HF_DATASET,
f'staging/{install_id}/screen_types/{stype}/{sha}.png',
repo_type='dataset',
token=HF_TOKEN,
)
import shutil
shutil.copy2(local, dest)
return dest
except Exception as e:
log.debug(f'Screen shot {sha} from {install_id}/{stype} missing: {e}')
return None
def collect_screen_type_votes(all_files: list[tuple[str, str, str]]) -> tuple[dict[str, tuple[str, str]], int]:
"""
Apply democratic voting to screen type screenshots.
Returns:
winner_map: {sha -> (winning_stype, install_id_that_uploaded_it)}
n_users: number of install_ids that contributed at least one screenshot
"""
# sha -> {install_id -> stype} (each install_id casts 1 vote per sha)
sha_votes: dict[str, dict[str, str]] = defaultdict(dict)
sha_source: dict[str, str] = {}
for install_id, stype, sha in all_files:
if stype not in SCREEN_TYPES:
continue
sha_votes[sha][install_id] = stype
sha_source.setdefault(sha, install_id)
n_users = len({iid for iid, _, _ in all_files})
winner_map: dict[str, tuple[str, str]] = {}
for sha, votes in sha_votes.items():
label_counts = Counter(votes.values())
winner, _ = label_counts.most_common(1)[0]
winner_map[sha] = (winner, sha_source[sha])
# Per-class cap: if a screen type has >= SC_MIN_KEEP samples, keep only
# SC_MAX_KEEP (random selection — avoids bloat for stable UI screens).
import random as _random
by_class: dict[str, list[str]] = defaultdict(list)
for sha, (stype, _) in winner_map.items():
by_class[stype].append(sha)
capped: dict[str, tuple[str, str]] = {}
for stype, shas in by_class.items():
if len(shas) >= SC_MIN_KEEP and len(shas) > SC_MAX_KEEP:
_random.shuffle(shas)
shas = shas[:SC_MAX_KEEP]
print(f' Screen type {stype}: capped to {SC_MAX_KEEP} of {len(by_class[stype])} samples')
for sha in shas:
capped[sha] = winner_map[sha]
return capped, n_users
def train_screen_classifier(
winner_map: dict[str, tuple[str, str]],
models_dir: Path,
tmpdir: Path,
prev_model_pt: Path | None = None,
deadline: float | None = None,
) -> tuple[float, int]:
"""
Download winning screenshots, fine-tune MobileNetV3-Small, save to models_dir.
Returns (best_val_acc, n_samples_used).
"""
import cv2
import torch
import torchvision.models as tv_models
import torchvision.transforms as T
import torch.nn.functional as _F
import random
import logging as _log_sc
_log_sc.getLogger('httpx').setLevel(_log_sc.WARNING)
from collections import defaultdict as _dd_sc2
# ── Collect screenshots (bulk download per install_id) ────────────────────
# Group by install_id so we can snapshot-download entire screen_types folders
by_iid_sc: dict[str, list[tuple[str, str]]] = _dd_sc2(list)
for sha, (stype, iid) in winner_map.items():
by_iid_sc[iid].append((sha, stype))
snap_sc = tmpdir / 'snap_sc'
snap_sc.mkdir(exist_ok=True)
print(f'\nDownloading {len(winner_map)} screenshots from {len(by_iid_sc)} contributor(s)...')
import socket as _socket_sc
import urllib.request as _urllib_sc
from concurrent.futures import ThreadPoolExecutor as _TPE_sc
_socket_sc.setdefaulttimeout(120)
_hf_base_sc = f'https://huggingface.co/datasets/{HF_DATASET}/resolve/main'
_opener_sc = _urllib_sc.build_opener()
if HF_TOKEN:
_opener_sc.addheaders = [('Authorization', f'Bearer {HF_TOKEN}')]
def _fetch_screen(args: tuple[str, str, str]) -> bool:
iid, sha, stype = args
dest = snap_sc / 'staging' / iid / 'screen_types' / stype / f'{sha}.png'
if dest.exists():
return True
dest.parent.mkdir(parents=True, exist_ok=True)
try:
url = f'{_hf_base_sc}/staging/{iid}/screen_types/{stype}/{sha}.png'
with _opener_sc.open(url) as r:
dest.write_bytes(r.read())
return True
except Exception:
return False
_sc_tasks = [(iid, sha, stype) for sha, (stype, iid) in winner_map.items()]
_sc_ok = _sc_fail = 0
with _TPE_sc(max_workers=16) as _pool_sc:
for _r in _pool_sc.map(_fetch_screen, _sc_tasks):
if _r:
_sc_ok += 1
else:
_sc_fail += 1
print(f' {_sc_ok} downloaded, {_sc_fail} failed/skipped.')
images, labels = [], []
for iid, items in by_iid_sc.items():
for sha, stype in items:
p = snap_sc / 'staging' / iid / 'screen_types' / stype / f'{sha}.png'
if not p.exists():
continue
img = cv2.imread(str(p))
if img is None:
continue
images.append(cv2.resize(img, (SC_IMG_SIZE, SC_IMG_SIZE)))
labels.append(stype)
print(f'{len(images)}/{len(winner_map)} screenshots loaded.')
n = len(images)
print(f'{n} screenshots ready.')
if n < SC_MIN_SAMPLES:
raise RuntimeError(
f'Only {n} screen type screenshots (need {SC_MIN_SAMPLES}). '
'Contribute more confirmed screen type labels first.'
)
# ── Label map ─────────────────────────────────────────────────────────────
# Log per-class distribution and drop classes with too few samples.
raw_counts = Counter(labels)
print(' Per-class sample counts:')
for lbl in sorted(raw_counts):
flag = '' if raw_counts[lbl] >= SC_MIN_CLASS_SAMPLES else f' <-- DROP (< {SC_MIN_CLASS_SAMPLES})'
print(f' {lbl:<22}: {raw_counts[lbl]:>4}{flag}')
kept = {l for l, c in raw_counts.items() if c >= SC_MIN_CLASS_SAMPLES}
dropped = sorted(raw_counts.keys() - kept)
if dropped:
print(f' Dropping under-represented classes: {dropped}')
images = [img for img, lbl in zip(images, labels) if lbl in kept]
labels = [lbl for lbl in labels if lbl in kept]
n = len(images)
if n < SC_MIN_SAMPLES:
raise RuntimeError(f'Only {n} screenshots remain after class filtering (need {SC_MIN_SAMPLES}).')
unique_labels = sorted(set(labels))
label_to_idx = {l: i for i, l in enumerate(unique_labels)}
idx_to_label = {i: l for l, i in label_to_idx.items()}
n_classes = len(unique_labels)
y = [label_to_idx[l] for l in labels]
print(f' {n_classes} classes for training: {unique_labels}')
# ── Dataset ───────────────────────────────────────────────────────────────
transform_train = T.Compose([
T.ToPILImage(),
T.RandomResizedCrop(SC_IMG_SIZE, scale=(0.85, 1.0)),
T.ColorJitter(brightness=0.15, contrast=0.15),
T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
transform_val = T.Compose([
T.ToPILImage(),
T.Resize((SC_IMG_SIZE, SC_IMG_SIZE)),
T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
class ScreenDataset(torch.utils.data.Dataset):
def __init__(self, imgs, lbls, tf):
self.imgs, self.lbls, self.tf = imgs, lbls, tf
def __len__(self): return len(self.imgs)
def __getitem__(self, i):
return self.tf(cv2.cvtColor(self.imgs[i], cv2.COLOR_BGR2RGB)), self.lbls[i]
# Stratified split — 20% per class for validation (min 2 if enough samples).
from collections import defaultdict as _dd_sc
by_cls_sc: dict[int, list[int]] = _dd_sc(list)
for i, lbl in enumerate(y):
by_cls_sc[lbl].append(i)
train_idx_sc: list[int] = []
val_idx_sc: list[int] = []
for lbl, idxs in by_cls_sc.items():
random.shuffle(idxs)
n_val = max(2, len(idxs) // 5) if len(idxs) >= 5 else (1 if len(idxs) >= 2 else 0)
val_idx_sc.extend(idxs[:n_val])
train_idx_sc.extend(idxs[n_val:])
random.shuffle(train_idx_sc)
val_idx_sc = val_idx_sc or train_idx_sc[:1]
ds_train = ScreenDataset([images[i] for i in train_idx_sc], [y[i] for i in train_idx_sc], transform_train)
ds_val = ScreenDataset([images[i] for i in val_idx_sc], [y[i] for i in val_idx_sc], transform_val)
dl_train = torch.utils.data.DataLoader(ds_train, batch_size=SC_BATCH_SIZE, shuffle=True, num_workers=0)
dl_val = torch.utils.data.DataLoader(ds_val, batch_size=SC_BATCH_SIZE, shuffle=False, num_workers=0)
# ── Model ─────────────────────────────────────────────────────────────────
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f'\n── screen_classifier (MobileNetV3-Small) {"─" * 38}')
print(f' Dataset : {n} screenshots, {n_classes} classes')
print(f' Split : {len(train_idx_sc)} train / {len(val_idx_sc)} val')
print(f' Device : {device}')
print(f'{"─" * 64}')
model = tv_models.mobilenet_v3_small(weights=tv_models.MobileNet_V3_Small_Weights.IMAGENET1K_V1)
in_features = model.classifier[-1].in_features
model.classifier[-1] = torch.nn.Linear(in_features, n_classes)
model = model.to(device)
# Warm-start: load backbone from previous central screen_classifier if available.
# Strip classifier keys before loading — strict=False ignores missing/unexpected
# keys but still raises on size mismatch (same key, different n_classes shape).
sc_fine_tuning = False
if prev_model_pt and prev_model_pt.exists():
try:
state = torch.load(str(prev_model_pt), map_location=device)
backbone_state = {k: v for k, v in state.items()
if not k.startswith('classifier')}
missing, unexpected = model.load_state_dict(backbone_state, strict=False)
non_head = [k for k in (missing + unexpected) if 'classifier' not in k]
if not non_head:
print('Loaded backbone from previous central screen_classifier — fine-tuning')
sc_fine_tuning = True
else:
print(f'Previous screen model: {len(non_head)} unexpected backbone keys')
except Exception as e:
print(f'Previous screen model load failed ({e}) — using ImageNet weights')
else:
print('No previous central screen_classifier — training from ImageNet weights')
if n < 30:
for p in model.features.parameters():
p.requires_grad = False
effective_sc_lr = SC_LR * 0.3 if sc_fine_tuning else SC_LR
optimizer = torch.optim.AdamW(
filter(lambda p: p.requires_grad, model.parameters()), lr=effective_sc_lr)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=SC_MAX_EPOCHS)
counts = Counter(y)
_cw = torch.tensor(
[1.0 / max(counts[i], 1) for i in range(n_classes)],
dtype=torch.float32, device=device)
_cw = _cw / _cw.sum() * n_classes
# Plain cross-entropy with class weights — Focal Loss was miscalibrating
# softmax outputs (suppressing easy samples → low confidence on correct predictions).
criterion = torch.nn.CrossEntropyLoss(weight=_cw).to(device)
# ── Training loop ─────────────────────────────────────────────────────────
best_val_acc = 0.0
# Initialise with pre-training weights so we always have a valid fallback,
# even if val_acc never improves above 0 (e.g. tiny val set or bad warm-start).
best_state = {k: v.cpu().clone() for k, v in model.state_dict().items()}
patience_count = 0
for epoch in range(SC_MAX_EPOCHS):
if deadline is not None and time.monotonic() > deadline:
print(f' Time budget exceeded, stopping screen classifier at epoch {epoch+1}.')
break
if epoch == SC_MAX_EPOCHS // 2 and n < 30:
for p in model.features.parameters():
p.requires_grad = True
optimizer = torch.optim.AdamW(model.parameters(), lr=SC_LR * 0.1)
model.train()
for xb, yb in dl_train:
xb, yb = xb.to(device), yb.to(device)
optimizer.zero_grad()
criterion(model(xb), yb).backward()
optimizer.step()
scheduler.step()
model.eval()
correct = total = 0
with torch.no_grad():
for xb, yb in dl_val:
xb, yb = xb.to(device), yb.to(device)
preds = model(xb).argmax(dim=1)
correct += (preds == yb).sum().item()
total += yb.size(0)
val_acc = correct / total if total > 0 else 0.0
print(f' Epoch {epoch+1:2d}/{SC_MAX_EPOCHS} val_acc={val_acc:.1%} best={best_val_acc:.1%}')
if val_acc > best_val_acc:
best_val_acc = val_acc
best_state = {k: v.cpu().clone() for k, v in model.state_dict().items()}
patience_count = 0
else:
patience_count += 1
if patience_count >= SC_PATIENCE:
print(f' Early stop at epoch {epoch+1}.')
break
if best_state:
model.load_state_dict(best_state)
# ── Save ──────────────────────────────────────────────────────────────────
models_dir.mkdir(parents=True, exist_ok=True)
model.eval().cpu()
torch.save(model.state_dict(), str(models_dir / 'screen_classifier.pt'))
with open(models_dir / 'screen_classifier_labels.json', 'w', encoding='utf-8') as f:
json.dump(idx_to_label, f, ensure_ascii=False, indent=2)
print(f'\n✓ screen_classifier saved — {n_classes} classes, val_acc={best_val_acc:.1%}')
return best_val_acc, n
# ── Democratic voting ─────────────────────────────────────────────────────────
def collect_votes(staging_folders: list[str]) -> tuple[dict[str, str], dict[str, str], int]:
"""
Download all staging annotations and apply democratic label voting.
Returns:
winner_labels: {crop_sha256 -> winning_label}
winner_sources: {crop_sha256 -> install_id_that_uploaded_this_crop}
n_users: number of install_ids that contributed at least one crop
"""
# sha -> {install_id -> label} (last label wins per install_id)
sha_votes: dict[str, dict[str, str]] = defaultdict(dict)
# sha -> install_id (who uploaded this crop file)
sha_source: dict[str, str] = {}
# Slots whose annotations are NOT for icon training.
# Ship Type and Ship Tier crops feed ship_type_corrections.json instead.
_TEXT_LEARNING_SLOTS = {'Ship Type', 'Ship Tier'}
n_with_data = 0
for iid in staging_folders:
anns = _load_staging_annotations(iid)
if not anns:
continue
n_with_data += 1
for entry in anns:
if entry.get('slot') in _TEXT_LEARNING_SLOTS:
continue # handled by collect_text_corrections(), not icon training
sha = entry.get('crop_sha256', '').strip()
label = entry.get('name', '').strip()
if sha and label:
sha_votes[sha][iid] = label # 1 install_id = 1 vote
sha_source.setdefault(sha, iid) # record first uploader
winner_labels: dict[str, str] = {}
for sha, votes in sha_votes.items():
# Count votes per label, majority wins; ties go to first encountered
label_counts = Counter(votes.values())
winner, _ = label_counts.most_common(1)[0]
winner_labels[sha] = winner
return winner_labels, sha_source, n_with_data
# ── Training ──────────────────────────────────────────────────────────────────
def train(winner_labels: dict[str, str], sha_source: dict[str, str],
models_dir: Path, tmpdir: Path,
prev_model_pt: Path | None = None,
deadline: float | None = None) -> tuple[float, int]:
"""
Download winning crops, train EfficientNet-B0, save model to models_dir.
prev_model_pt: path to a previously-trained icon_classifier.pt — its
backbone weights are loaded (strict=False) for warm-start fine-tuning.
Returns (best_val_acc, n_samples_used).
"""
import cv2
import torch
import torchvision.models as tv_models
import torchvision.transforms as T
import torch.nn.functional as _F
import random
from collections import Counter as _Counter
# ── Collect crops (parallel urllib download with socket timeout) ─────────
# urllib.request uses blocking sockets → socket.setdefaulttimeout applies,
# killing stalled TCP reads that httpx/snapshot_download cannot time out.
import socket as _socket
import urllib.request as _urllib
from collections import defaultdict as _dd3
from concurrent.futures import ThreadPoolExecutor as _TPE
# Group by install_id
by_iid: dict[str, list[tuple[str, str]]] = _dd3(list)
for sha, label in winner_labels.items():
iid = sha_source.get(sha)
if iid:
by_iid[iid].append((sha, label))
snap_cache = tmpdir / 'snap'
snap_cache.mkdir(exist_ok=True)
_socket.setdefaulttimeout(120) # 2 min hard timeout per socket read
_hf_base = f'https://huggingface.co/datasets/{HF_DATASET}/resolve/main'
_auth_headers = [('Authorization', f'Bearer {HF_TOKEN}')] if HF_TOKEN else []
_opener = _urllib.build_opener()
_opener.addheaders = _auth_headers
def _fetch_crop(args: tuple[str, str]) -> bool:
iid, sha = args
dest = snap_cache / 'staging' / iid / 'crops' / f'{sha}.png'
if dest.exists():
return True
dest.parent.mkdir(parents=True, exist_ok=True)
try:
with _opener.open(f'{_hf_base}/staging/{iid}/crops/{sha}.png') as r:
dest.write_bytes(r.read())
return True
except Exception:
return False
all_crops = [(iid, sha) for iid, items in by_iid.items() for sha, _ in items]
print(f'\nDownloading {len(all_crops)} crops from {len(by_iid)} contributor(s)...')
_ok = _fail = 0
with _TPE(max_workers=16) as _pool:
for _result in _pool.map(_fetch_crop, all_crops):
if _result:
_ok += 1
else:
_fail += 1
print(f' {_ok} downloaded, {_fail} failed/skipped.')
crops, labels = [], []
for iid, items in by_iid.items():
crop_dir = snap_cache / 'staging' / iid / 'crops'
for sha, label in items:
p = crop_dir / f'{sha}.png'
if not p.exists():
continue
img = cv2.imread(str(p))
if img is None:
continue
crops.append(cv2.resize(img, (IMG_SIZE, IMG_SIZE)))
labels.append(label)
print(f'{len(crops)}/{sum(len(v) for v in by_iid.values())} crops loaded.')
n = len(crops)
print(f'{n} crops ready.')
if n < MIN_SAMPLES:
raise RuntimeError(
f'Only {n} crops available (need {MIN_SAMPLES}). '
'Contribute more confirmed annotations first.'
)
# ── Label map ────────────────────────────────────────────────────────────