tanh1c commited on
Commit
9c763c6
·
1 Parent(s): d13c106

Add Gradio image demo without binary calibration PNGs

Browse files
.gitattributes CHANGED
@@ -1,3 +1,2 @@
1
  *.pth filter=lfs diff=lfs merge=lfs -text
2
  *.tar.gz filter=lfs diff=lfs merge=lfs -text
3
- *.png filter=lfs diff=lfs merge=lfs -text
 
1
  *.pth filter=lfs diff=lfs merge=lfs -text
2
  *.tar.gz filter=lfs diff=lfs merge=lfs -text
 
assignments/assignment-1/app/shared/artifact_utils.py CHANGED
@@ -9,6 +9,9 @@ import os
9
  from pathlib import Path
10
  from typing import Any, Dict, Optional
11
 
 
 
 
12
  from .model_registry import CalibrationResult
13
 
14
 
@@ -18,6 +21,110 @@ ASSIGNMENT_ROOT = Path(
18
  ARTIFACTS_DIR = ASSIGNMENT_ROOT / "image" / "artifacts"
19
 
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def get_best_accuracy_from_history(history: Optional[Dict[str, Any]]) -> Optional[float]:
22
  """Return the best validation accuracy found in a checkpoint history."""
23
  if not history:
@@ -44,20 +151,24 @@ def load_precomputed_calibration_result(
44
  return None
45
 
46
  metrics_name = f"{model_tag}_calibration_metrics_{sample_tag}.json"
47
- image_name = f"{model_tag}_calibration_{sample_tag}.png"
48
-
49
  metrics_path = next(ARTIFACTS_DIR.rglob(metrics_name), None)
 
50
  image_path = next(ARTIFACTS_DIR.rglob(image_name), None)
51
 
52
- if metrics_path is None or image_path is None:
53
  return None
54
 
55
  metrics = json.loads(metrics_path.read_text(encoding="utf-8"))
 
 
 
 
 
56
  return CalibrationResult(
57
  ece=float(metrics["ece"]),
58
  bin_accuracies=[float(x) for x in metrics["bin_accuracies"]],
59
  bin_confidences=[float(x) for x in metrics["bin_confidences"]],
60
  bin_counts=[int(x) for x in metrics["bin_counts"]],
61
- reliability_diagram=str(image_path),
62
  source=f"Notebook artifact ({metrics_path.parent.name})",
63
  )
 
9
  from pathlib import Path
10
  from typing import Any, Dict, Optional
11
 
12
+ import numpy as np
13
+ from PIL import Image
14
+
15
  from .model_registry import CalibrationResult
16
 
17
 
 
21
  ARTIFACTS_DIR = ASSIGNMENT_ROOT / "image" / "artifacts"
22
 
23
 
24
+ def _render_reliability_diagram_from_metrics(metrics: Dict[str, Any]) -> np.ndarray:
25
+ """Render a reliability diagram directly from saved calibration metrics."""
26
+ import matplotlib
27
+
28
+ matplotlib.use("Agg")
29
+ import matplotlib.pyplot as plt
30
+
31
+ bin_accuracies = [float(x) for x in metrics["bin_accuracies"]]
32
+ bin_confidences = [float(x) for x in metrics["bin_confidences"]]
33
+ bin_counts = [int(x) for x in metrics["bin_counts"]]
34
+ ece = float(metrics["ece"])
35
+
36
+ n_bins = len(bin_accuracies)
37
+ bin_boundaries = np.linspace(0, 1, n_bins + 1)
38
+ bin_centers = [
39
+ (bin_boundaries[i] + bin_boundaries[i + 1]) / 2 for i in range(n_bins)
40
+ ]
41
+ total = max(sum(bin_counts), 1)
42
+
43
+ fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
44
+ fig.patch.set_facecolor("#0d1117")
45
+
46
+ ax1.set_facecolor("#161b22")
47
+ width = 0.08
48
+ ax1.bar(
49
+ [c - width / 2 for c in bin_centers],
50
+ bin_accuracies,
51
+ width,
52
+ label="Accuracy",
53
+ color="#58a6ff",
54
+ alpha=0.9,
55
+ edgecolor="#58a6ff",
56
+ )
57
+ ax1.bar(
58
+ [c + width / 2 for c in bin_centers],
59
+ bin_confidences,
60
+ width,
61
+ label="Avg Confidence",
62
+ color="#f97583",
63
+ alpha=0.9,
64
+ edgecolor="#f97583",
65
+ )
66
+ ax1.plot(
67
+ [0, 1],
68
+ [0, 1],
69
+ "--",
70
+ color="#8b949e",
71
+ linewidth=2,
72
+ label="Perfect Calibration",
73
+ )
74
+ ax1.set_xlim(0, 1)
75
+ ax1.set_ylim(0, 1)
76
+ ax1.set_xlabel("Confidence", color="white", fontsize=12)
77
+ ax1.set_ylabel("Accuracy / Confidence", color="white", fontsize=12)
78
+ ax1.set_title(
79
+ f"Reliability Diagram (ECE: {ece:.4f})",
80
+ color="white",
81
+ fontsize=14,
82
+ fontweight="bold",
83
+ pad=15,
84
+ )
85
+ ax1.legend(
86
+ facecolor="#161b22",
87
+ edgecolor="#30363d",
88
+ labelcolor="white",
89
+ fontsize=10,
90
+ )
91
+ ax1.tick_params(colors="white")
92
+ for spine in ax1.spines.values():
93
+ spine.set_edgecolor("#30363d")
94
+ ax1.grid(True, alpha=0.1, color="white")
95
+
96
+ ax2.set_facecolor("#161b22")
97
+ ax2.bar(
98
+ bin_centers,
99
+ [count / total for count in bin_counts],
100
+ 0.08,
101
+ color="#56d364",
102
+ alpha=0.9,
103
+ edgecolor="#56d364",
104
+ )
105
+ ax2.set_xlim(0, 1)
106
+ ax2.set_xlabel("Confidence", color="white", fontsize=12)
107
+ ax2.set_ylabel("Fraction of Samples", color="white", fontsize=12)
108
+ ax2.set_title(
109
+ "Confidence Distribution",
110
+ color="white",
111
+ fontsize=14,
112
+ fontweight="bold",
113
+ pad=15,
114
+ )
115
+ ax2.tick_params(colors="white")
116
+ for spine in ax2.spines.values():
117
+ spine.set_edgecolor("#30363d")
118
+ ax2.grid(True, alpha=0.1, color="white")
119
+
120
+ plt.tight_layout(pad=3)
121
+ fig.canvas.draw()
122
+ rgba_buffer = fig.canvas.buffer_rgba()
123
+ diagram = np.array(rgba_buffer)[:, :, :3]
124
+ plt.close(fig)
125
+ return diagram
126
+
127
+
128
  def get_best_accuracy_from_history(history: Optional[Dict[str, Any]]) -> Optional[float]:
129
  """Return the best validation accuracy found in a checkpoint history."""
130
  if not history:
 
151
  return None
152
 
153
  metrics_name = f"{model_tag}_calibration_metrics_{sample_tag}.json"
 
 
154
  metrics_path = next(ARTIFACTS_DIR.rglob(metrics_name), None)
155
+ image_name = f"{model_tag}_calibration_{sample_tag}.png"
156
  image_path = next(ARTIFACTS_DIR.rglob(image_name), None)
157
 
158
+ if metrics_path is None:
159
  return None
160
 
161
  metrics = json.loads(metrics_path.read_text(encoding="utf-8"))
162
+ if image_path is not None:
163
+ reliability_diagram = np.array(Image.open(image_path).convert("RGB"))
164
+ else:
165
+ reliability_diagram = _render_reliability_diagram_from_metrics(metrics)
166
+
167
  return CalibrationResult(
168
  ece=float(metrics["ece"]),
169
  bin_accuracies=[float(x) for x in metrics["bin_accuracies"]],
170
  bin_confidences=[float(x) for x in metrics["bin_confidences"]],
171
  bin_counts=[int(x) for x in metrics["bin_counts"]],
172
+ reliability_diagram=reliability_diagram,
173
  source=f"Notebook artifact ({metrics_path.parent.name})",
174
  )
assignments/assignment-1/image/artifacts/cnn/resnet18_calibration_full.png DELETED

Git LFS Details

  • SHA256: 1cb171cadaa51bc9800aeae468a823cb6e30799714c5d6c7cc39d6cbc32acc42
  • Pointer size: 131 Bytes
  • Size of remote file: 104 kB
assignments/assignment-1/image/artifacts/vit/vit_b16_calibration_full.png DELETED

Git LFS Details

  • SHA256: 9ec24a90630f7df2784bd3706eac25a4711021ed65c2dc58ec7a1ebccf6bd314
  • Pointer size: 131 Bytes
  • Size of remote file: 109 kB