constructelligence commited on
Commit
04c7da2
·
verified ·
1 Parent(s): fcbb510

Upload benchmark.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. benchmark.py +19 -10
benchmark.py CHANGED
@@ -28,6 +28,7 @@ from __future__ import annotations
28
 
29
  import argparse
30
  import json
 
31
  from pathlib import Path
32
 
33
  import numpy as np
@@ -36,6 +37,11 @@ from PIL import Image
36
  from label_schema import CLASSES, IGNORE
37
  from provenance import sha256_file
38
 
 
 
 
 
 
39
  BENCHMARK_VERSION = "1.0"
40
  TAXONOMY_VERSION = "1.0"
41
  REDUCED_CLASSES = ("other", "wall", "window")
@@ -140,13 +146,12 @@ def evaluate_painting_vision(checkpoint_path, data, split, size, limit, device):
140
  from torch.utils.data import DataLoader
141
 
142
  from label_schema import mask_to_labels
143
- from models import DEFAULT_ARCH, build_model
144
  from train import MEAN, STD, WallDataset, letterbox
145
 
146
  checkpoint = torch.load(checkpoint_path, map_location=device, weights_only=True)
147
  arch = str(checkpoint.get("arch", DEFAULT_ARCH))
148
- model = build_model(arch, len(CLASSES), pretrained_backbone=False)
149
- model.load_state_dict(checkpoint["model"])
150
  model.to(device).eval()
151
 
152
  dataset = WallDataset(data, split, size)
@@ -156,7 +161,7 @@ def evaluate_painting_vision(checkpoint_path, data, split, size, limit, device):
156
  coverage_errors = []
157
  count = 0
158
  with torch.inference_mode():
159
- for x, y, _ in loader:
160
  prediction = model(x.to(device))["semantic"].argmax(1).cpu().numpy()
161
  for p, t in zip(prediction, y.numpy()):
162
  full_cm += confusion(p, t, len(CLASSES))
@@ -187,8 +192,8 @@ def evaluate_baseline(name, data, split, limit, device):
187
 
188
  if name == "segformer-ade":
189
  from transformers import AutoImageProcessor, SegformerForSemanticSegmentation
190
- processor = AutoImageProcessor.from_pretrained(ADE_BASELINE)
191
- model = SegformerForSemanticSegmentation.from_pretrained(ADE_BASELINE).to(device).eval()
192
  lut = build_label_lut(model.config.id2label)
193
 
194
  def predict(image):
@@ -270,10 +275,14 @@ def main():
270
  print("no --checkpoint given: skipping the Painting Vision row (train one first)\n")
271
 
272
  if args.baseline != "none":
273
- import torch
274
- device = torch.device(args.device if torch.cuda.is_available() or args.device == "cpu" else "cpu")
275
- results[f"baseline:{args.baseline}"] = evaluate_baseline(args.baseline, args.data, args.split,
276
- args.limit, device)
 
 
 
 
277
 
278
  results["floor:all-other"] = evaluate_floor(args.data, args.split, args.limit)
279
 
 
28
 
29
  import argparse
30
  import json
31
+ import os
32
  from pathlib import Path
33
 
34
  import numpy as np
 
37
  from label_schema import CLASSES, IGNORE
38
  from provenance import sha256_file
39
 
40
+ # Public baselines must load without credentials. A stale/invalid HF token in the
41
+ # environment makes huggingface_hub return 401 for public repos, so disable the
42
+ # implicit token outright (and pass token=False at the call sites).
43
+ os.environ.setdefault("HF_HUB_DISABLE_IMPLICIT_TOKEN", "1")
44
+
45
  BENCHMARK_VERSION = "1.0"
46
  TAXONOMY_VERSION = "1.0"
47
  REDUCED_CLASSES = ("other", "wall", "window")
 
146
  from torch.utils.data import DataLoader
147
 
148
  from label_schema import mask_to_labels
149
+ from models import DEFAULT_ARCH, build_from_checkpoint
150
  from train import MEAN, STD, WallDataset, letterbox
151
 
152
  checkpoint = torch.load(checkpoint_path, map_location=device, weights_only=True)
153
  arch = str(checkpoint.get("arch", DEFAULT_ARCH))
154
+ model = build_from_checkpoint(checkpoint, len(CLASSES))
 
155
  model.to(device).eval()
156
 
157
  dataset = WallDataset(data, split, size)
 
161
  coverage_errors = []
162
  count = 0
163
  with torch.inference_mode():
164
+ for x, y, *_ in loader:
165
  prediction = model(x.to(device))["semantic"].argmax(1).cpu().numpy()
166
  for p, t in zip(prediction, y.numpy()):
167
  full_cm += confusion(p, t, len(CLASSES))
 
192
 
193
  if name == "segformer-ade":
194
  from transformers import AutoImageProcessor, SegformerForSemanticSegmentation
195
+ processor = AutoImageProcessor.from_pretrained(ADE_BASELINE, token=False)
196
+ model = SegformerForSemanticSegmentation.from_pretrained(ADE_BASELINE, token=False).to(device).eval()
197
  lut = build_label_lut(model.config.id2label)
198
 
199
  def predict(image):
 
275
  print("no --checkpoint given: skipping the Painting Vision row (train one first)\n")
276
 
277
  if args.baseline != "none":
278
+ try:
279
+ import torch
280
+ device = torch.device(args.device if torch.cuda.is_available() or args.device == "cpu" else "cpu")
281
+ results[f"baseline:{args.baseline}"] = evaluate_baseline(args.baseline, args.data, args.split,
282
+ args.limit, device)
283
+ except Exception as exc: # an optional comparison must not invalidate our own result
284
+ results[f"baseline:{args.baseline}"] = {"error": f"{type(exc).__name__}: {exc}"}
285
+ print(f"baseline {args.baseline!r} could not run and was skipped: {type(exc).__name__}: {exc}\n")
286
 
287
  results["floor:all-other"] = evaluate_floor(args.data, args.split, args.limit)
288