Commit ·
14544c7
1
Parent(s): 1160c58
Fix marimo variable uniqueness (prefix private vars with _)
Browse files- train-image-classifier.py +42 -43
train-image-classifier.py
CHANGED
|
@@ -228,29 +228,29 @@ def _(dataset_name, mo):
|
|
| 228 |
@app.cell
|
| 229 |
def _(dataset, labels, mo):
|
| 230 |
# Show sample images (notebook mode only)
|
| 231 |
-
import base64
|
| 232 |
-
from io import BytesIO
|
| 233 |
|
| 234 |
-
def
|
| 235 |
"""Convert PIL image to base64 for HTML display."""
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
return
|
| 241 |
|
| 242 |
# Get 6 sample images with different labels
|
| 243 |
-
|
| 244 |
|
| 245 |
-
|
| 246 |
-
for
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
f"""
|
| 251 |
<div style="text-align: center; margin: 5px;">
|
| 252 |
-
<img src="data:image/png;base64,{
|
| 253 |
-
<br/><small>{
|
| 254 |
</div>
|
| 255 |
"""
|
| 256 |
)
|
|
@@ -258,7 +258,7 @@ def _(dataset, labels, mo):
|
|
| 258 |
mo.md(f"""
|
| 259 |
### Sample Images
|
| 260 |
<div style="display: flex; flex-wrap: wrap; gap: 10px;">
|
| 261 |
-
{"".join(
|
| 262 |
</div>
|
| 263 |
""")
|
| 264 |
return
|
|
@@ -420,41 +420,40 @@ def _(trainer):
|
|
| 420 |
@app.cell
|
| 421 |
def _(dataset, id2label, image_processor, mo, model):
|
| 422 |
import torch
|
|
|
|
|
|
|
| 423 |
|
| 424 |
# Show some predictions (notebook mode)
|
| 425 |
model.eval()
|
| 426 |
-
|
| 427 |
|
| 428 |
-
|
| 429 |
-
for
|
| 430 |
-
|
| 431 |
-
|
| 432 |
|
| 433 |
with torch.no_grad():
|
| 434 |
-
|
| 435 |
-
|
| 436 |
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
|
| 441 |
# Convert image for display
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
border_color = "#4ade80" if correct == "correct" else "#f87171"
|
| 452 |
-
prediction_html.append(
|
| 453 |
f"""
|
| 454 |
-
<div style="text-align: center; margin: 5px; padding: 10px; border: 2px solid {
|
| 455 |
-
<img src="data:image/png;base64,{
|
| 456 |
-
<br/><small>True: <b>{
|
| 457 |
-
<br/><small>Pred: <b>{
|
| 458 |
</div>
|
| 459 |
"""
|
| 460 |
)
|
|
@@ -462,7 +461,7 @@ def _(dataset, id2label, image_processor, mo, model):
|
|
| 462 |
mo.md(f"""
|
| 463 |
### Sample Predictions
|
| 464 |
<div style="display: flex; flex-wrap: wrap; gap: 10px;">
|
| 465 |
-
{"".join(
|
| 466 |
</div>
|
| 467 |
<small>Green border = correct, Red border = wrong</small>
|
| 468 |
""")
|
|
|
|
| 228 |
@app.cell
|
| 229 |
def _(dataset, labels, mo):
|
| 230 |
# Show sample images (notebook mode only)
|
| 231 |
+
import base64 as _base64
|
| 232 |
+
from io import BytesIO as _BytesIO
|
| 233 |
|
| 234 |
+
def _image_to_base64(img, max_size=150):
|
| 235 |
"""Convert PIL image to base64 for HTML display."""
|
| 236 |
+
_img_copy = img.copy()
|
| 237 |
+
_img_copy.thumbnail((max_size, max_size))
|
| 238 |
+
_buffered = _BytesIO()
|
| 239 |
+
_img_copy.save(_buffered, format="PNG")
|
| 240 |
+
return _base64.b64encode(_buffered.getvalue()).decode()
|
| 241 |
|
| 242 |
# Get 6 sample images with different labels
|
| 243 |
+
_samples = dataset["train"].shuffle(seed=42).select(range(6))
|
| 244 |
|
| 245 |
+
_images_html = []
|
| 246 |
+
for _sample in _samples:
|
| 247 |
+
_img_b64 = _image_to_base64(_sample["image"])
|
| 248 |
+
_label_name = labels[_sample["label"]] if labels else _sample["label"]
|
| 249 |
+
_images_html.append(
|
| 250 |
f"""
|
| 251 |
<div style="text-align: center; margin: 5px;">
|
| 252 |
+
<img src="data:image/png;base64,{_img_b64}" style="border-radius: 8px;"/>
|
| 253 |
+
<br/><small>{_label_name}</small>
|
| 254 |
</div>
|
| 255 |
"""
|
| 256 |
)
|
|
|
|
| 258 |
mo.md(f"""
|
| 259 |
### Sample Images
|
| 260 |
<div style="display: flex; flex-wrap: wrap; gap: 10px;">
|
| 261 |
+
{"".join(_images_html)}
|
| 262 |
</div>
|
| 263 |
""")
|
| 264 |
return
|
|
|
|
| 420 |
@app.cell
|
| 421 |
def _(dataset, id2label, image_processor, mo, model):
|
| 422 |
import torch
|
| 423 |
+
import base64 as _b64
|
| 424 |
+
from io import BytesIO as _BIO
|
| 425 |
|
| 426 |
# Show some predictions (notebook mode)
|
| 427 |
model.eval()
|
| 428 |
+
_test_samples = dataset["test"].shuffle(seed=42).select(range(4))
|
| 429 |
|
| 430 |
+
_prediction_html = []
|
| 431 |
+
for _sample in _test_samples:
|
| 432 |
+
_img = _sample["image"].convert("RGB")
|
| 433 |
+
_inputs = image_processor(_img, return_tensors="pt")
|
| 434 |
|
| 435 |
with torch.no_grad():
|
| 436 |
+
_outputs = model(**_inputs)
|
| 437 |
+
_pred_idx = _outputs.logits.argmax(-1).item()
|
| 438 |
|
| 439 |
+
_true_label = id2label[_sample["label"]] if id2label else _sample["label"]
|
| 440 |
+
_pred_label = id2label[_pred_idx] if id2label else _pred_idx
|
| 441 |
+
_correct = "correct" if _pred_idx == _sample["label"] else "wrong"
|
| 442 |
|
| 443 |
# Convert image for display
|
| 444 |
+
_img_copy = _img.copy()
|
| 445 |
+
_img_copy.thumbnail((120, 120))
|
| 446 |
+
_buffered = _BIO()
|
| 447 |
+
_img_copy.save(_buffered, format="PNG")
|
| 448 |
+
_img_b64 = _b64.b64encode(_buffered.getvalue()).decode()
|
| 449 |
+
|
| 450 |
+
_border_color = "#4ade80" if _correct == "correct" else "#f87171"
|
| 451 |
+
_prediction_html.append(
|
|
|
|
|
|
|
|
|
|
| 452 |
f"""
|
| 453 |
+
<div style="text-align: center; margin: 5px; padding: 10px; border: 2px solid {_border_color}; border-radius: 8px;">
|
| 454 |
+
<img src="data:image/png;base64,{_img_b64}" style="border-radius: 4px;"/>
|
| 455 |
+
<br/><small>True: <b>{_true_label}</b></small>
|
| 456 |
+
<br/><small>Pred: <b>{_pred_label}</b></small>
|
| 457 |
</div>
|
| 458 |
"""
|
| 459 |
)
|
|
|
|
| 461 |
mo.md(f"""
|
| 462 |
### Sample Predictions
|
| 463 |
<div style="display: flex; flex-wrap: wrap; gap: 10px;">
|
| 464 |
+
{"".join(_prediction_html)}
|
| 465 |
</div>
|
| 466 |
<small>Green border = correct, Red border = wrong</small>
|
| 467 |
""")
|