Spaces:
Sleeping
Sleeping
Fix radio navigation bug (use .input() instead of .change()) and split KNN training into its own tab
Browse files
app.py
CHANGED
|
@@ -324,30 +324,18 @@ puis soumettez vos réponses pour générer la carte et la matrice de confusion.
|
|
| 324 |
)
|
| 325 |
gr.Markdown("---")
|
| 326 |
progress_bar = gr.Markdown("**Progression : 0 / 23**")
|
| 327 |
-
gr.Markdown("---")
|
| 328 |
-
gr.Markdown("### Entraînement du modèle KNN")
|
| 329 |
-
gr.Markdown(
|
| 330 |
-
"*(Une fois les 23 polygones étiquetés, ce modèle sera "
|
| 331 |
-
"entraîné sur VOS réponses + les vraies valeurs spectrales "
|
| 332 |
-
"des 7 bandes satellite, puis évalué sur la vérité terrain)*"
|
| 333 |
-
)
|
| 334 |
-
k_slider = gr.Slider(
|
| 335 |
-
minimum=K_MIN, maximum=K_MAX, value=K_DEFAULT, step=1,
|
| 336 |
-
label="k (nombre de voisins)",
|
| 337 |
-
)
|
| 338 |
btn_submit = gr.Button(
|
| 339 |
-
"🚀
|
| 340 |
variant="primary",
|
| 341 |
visible=False,
|
| 342 |
size="lg",
|
| 343 |
)
|
| 344 |
|
| 345 |
-
# ── Tab 3: Résultats ──────────────────────────
|
| 346 |
with gr.Tab("📊 Résultats", id="tab_results") as tab_results:
|
| 347 |
results_placeholder = gr.Markdown(
|
| 348 |
"*(Les résultats apparaîtront ici après soumission)*"
|
| 349 |
)
|
| 350 |
-
gr.Markdown("## 1️⃣ Qualité de votre interprétation visuelle")
|
| 351 |
accuracy_md = gr.Markdown(visible=False)
|
| 352 |
results_tbl = gr.Dataframe(
|
| 353 |
headers=["Polygone", "Votre réponse", "Réponse enseignant", "Résultat"],
|
|
@@ -357,7 +345,25 @@ puis soumettez vos réponses pour générer la carte et la matrice de confusion.
|
|
| 357 |
with gr.Row(visible=False) as row_plots_student:
|
| 358 |
student_matrix_plot = gr.Plot(label="Votre interprétation vs Enseignant")
|
| 359 |
|
| 360 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
model_accuracy_md = gr.Markdown(visible=False)
|
| 362 |
with gr.Row(visible=False) as row_plots_model:
|
| 363 |
model_matrix_plot = gr.Plot(label="Votre modèle KNN vs Vérité terrain")
|
|
@@ -408,11 +414,8 @@ puis soumettez vos réponses pour générer la carte et la matrice de confusion.
|
|
| 408 |
gr.update(value=overall),
|
| 409 |
gr.update(visible=show_submit))
|
| 410 |
|
| 411 |
-
def
|
| 412 |
-
"""
|
| 413 |
-
train a real KNN model on the student's labels + spectral bands and
|
| 414 |
-
evaluate it against Ground Truth."""
|
| 415 |
-
# 1) Accuracy of the visual interpretation vs teacher's answer key
|
| 416 |
correct = sum(
|
| 417 |
1 for pid in range(1, 24)
|
| 418 |
if current_labels[pid-1] is not None
|
|
@@ -429,8 +432,33 @@ puis soumettez vos réponses pour générer la carte et la matrice de confusion.
|
|
| 429 |
table = build_results_table(current_labels)
|
| 430 |
student_fig = fig_student_matrix(current_labels)
|
| 431 |
|
| 432 |
-
|
| 433 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
model_oa, model_matrix, student_map, knn_map, gt_map = train_and_predict(
|
| 435 |
current_labels, k
|
| 436 |
)
|
|
@@ -445,16 +473,12 @@ puis soumettez vos réponses pour générer la carte et la matrice de confusion.
|
|
| 445 |
map_fig = fig_three_panel_map(student_map, knn_map, gt_map)
|
| 446 |
|
| 447 |
return (
|
| 448 |
-
gr.update(
|
| 449 |
-
gr.update(value=acc_text, visible=True),
|
| 450 |
-
gr.update(value=table, visible=True),
|
| 451 |
-
gr.update(visible=True),
|
| 452 |
-
gr.update(value=student_fig),
|
| 453 |
gr.update(value=model_acc_text, visible=True),
|
| 454 |
gr.update(visible=True),
|
| 455 |
gr.update(value=model_fig),
|
| 456 |
gr.update(value=knn_fig),
|
| 457 |
-
gr.update(value=map_fig,
|
| 458 |
)
|
| 459 |
|
| 460 |
# Wire up navigation
|
|
@@ -469,23 +493,37 @@ puis soumettez vos réponses pour générer la carte et la matrice de confusion.
|
|
| 469 |
outputs=[cur_idx, polygon_img, polygon_title, class_radio, progress_bar],
|
| 470 |
)
|
| 471 |
|
| 472 |
-
# Wire up class selection
|
| 473 |
-
class_radio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
on_class_select,
|
| 475 |
inputs=[class_radio, cur_idx, labels],
|
| 476 |
outputs=[labels, progress_bar, progress_md, btn_submit],
|
| 477 |
)
|
| 478 |
|
| 479 |
-
# Wire up submit (labeling accuracy
|
| 480 |
btn_submit.click(
|
| 481 |
-
|
| 482 |
-
inputs=[labels
|
| 483 |
outputs=[
|
| 484 |
results_placeholder,
|
| 485 |
accuracy_md,
|
| 486 |
results_tbl,
|
| 487 |
row_plots_student,
|
| 488 |
student_matrix_plot,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
model_accuracy_md,
|
| 490 |
row_plots_model,
|
| 491 |
model_matrix_plot,
|
|
|
|
| 324 |
)
|
| 325 |
gr.Markdown("---")
|
| 326 |
progress_bar = gr.Markdown("**Progression : 0 / 23**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
btn_submit = gr.Button(
|
| 328 |
+
"🚀 Voir ma précision d'interprétation",
|
| 329 |
variant="primary",
|
| 330 |
visible=False,
|
| 331 |
size="lg",
|
| 332 |
)
|
| 333 |
|
| 334 |
+
# ── Tab 3: Résultats de l'interprétation ──────────────────────────
|
| 335 |
with gr.Tab("📊 Résultats", id="tab_results") as tab_results:
|
| 336 |
results_placeholder = gr.Markdown(
|
| 337 |
"*(Les résultats apparaîtront ici après soumission)*"
|
| 338 |
)
|
|
|
|
| 339 |
accuracy_md = gr.Markdown(visible=False)
|
| 340 |
results_tbl = gr.Dataframe(
|
| 341 |
headers=["Polygone", "Votre réponse", "Réponse enseignant", "Résultat"],
|
|
|
|
| 345 |
with gr.Row(visible=False) as row_plots_student:
|
| 346 |
student_matrix_plot = gr.Plot(label="Votre interprétation vs Enseignant")
|
| 347 |
|
| 348 |
+
# ── Tab 4: Entraînement du modèle KNN ──────────────────────────────
|
| 349 |
+
with gr.Tab("🧠 Entraînement du modèle KNN"):
|
| 350 |
+
gr.Markdown("""
|
| 351 |
+
## Entraînez votre propre modèle KNN
|
| 352 |
+
|
| 353 |
+
Ce modèle est entraîné sur **vos** 23 étiquettes (onglet précédent) combinées
|
| 354 |
+
aux vraies valeurs spectrales des 7 bandes satellite, puis évalué sur les
|
| 355 |
+
pixels de vérité terrain (indépendants des polygones d'entraînement).
|
| 356 |
+
""")
|
| 357 |
+
train_warning_md = gr.Markdown(visible=False)
|
| 358 |
+
k_slider = gr.Slider(
|
| 359 |
+
minimum=K_MIN, maximum=K_MAX, value=K_DEFAULT, step=1,
|
| 360 |
+
label="k (nombre de voisins)",
|
| 361 |
+
)
|
| 362 |
+
btn_train = gr.Button(
|
| 363 |
+
"🚀 Entraîner mon modèle KNN",
|
| 364 |
+
variant="primary",
|
| 365 |
+
size="lg",
|
| 366 |
+
)
|
| 367 |
model_accuracy_md = gr.Markdown(visible=False)
|
| 368 |
with gr.Row(visible=False) as row_plots_model:
|
| 369 |
model_matrix_plot = gr.Plot(label="Votre modèle KNN vs Vérité terrain")
|
|
|
|
| 414 |
gr.update(value=overall),
|
| 415 |
gr.update(visible=show_submit))
|
| 416 |
|
| 417 |
+
def on_submit_labels(current_labels):
|
| 418 |
+
"""Show the accuracy of the visual interpretation vs the teacher's answer key."""
|
|
|
|
|
|
|
|
|
|
| 419 |
correct = sum(
|
| 420 |
1 for pid in range(1, 24)
|
| 421 |
if current_labels[pid-1] is not None
|
|
|
|
| 432 |
table = build_results_table(current_labels)
|
| 433 |
student_fig = fig_student_matrix(current_labels)
|
| 434 |
|
| 435 |
+
return (
|
| 436 |
+
gr.update(value=""),
|
| 437 |
+
gr.update(value=acc_text, visible=True),
|
| 438 |
+
gr.update(value=table, visible=True),
|
| 439 |
+
gr.update(visible=True),
|
| 440 |
+
gr.update(value=student_fig),
|
| 441 |
+
)
|
| 442 |
+
|
| 443 |
+
def on_train(current_labels, k):
|
| 444 |
+
"""Train a KNN model on the student's own labels + real spectral bands,
|
| 445 |
+
then evaluate it against Ground Truth."""
|
| 446 |
+
total = count_labeled(current_labels)
|
| 447 |
+
if total < N_POLYGONS:
|
| 448 |
+
warning = (
|
| 449 |
+
f"⚠️ Vous n'avez étiqueté que {total}/{N_POLYGONS} polygones. "
|
| 450 |
+
f"Terminez l'étiquetage dans l'onglet 🖊️ *Interprétation des polygones* "
|
| 451 |
+
f"avant d'entraîner un modèle."
|
| 452 |
+
)
|
| 453 |
+
return (
|
| 454 |
+
gr.update(value=warning, visible=True),
|
| 455 |
+
gr.update(visible=False),
|
| 456 |
+
gr.update(visible=False),
|
| 457 |
+
gr.update(),
|
| 458 |
+
gr.update(),
|
| 459 |
+
gr.update(visible=False),
|
| 460 |
+
)
|
| 461 |
+
|
| 462 |
model_oa, model_matrix, student_map, knn_map, gt_map = train_and_predict(
|
| 463 |
current_labels, k
|
| 464 |
)
|
|
|
|
| 473 |
map_fig = fig_three_panel_map(student_map, knn_map, gt_map)
|
| 474 |
|
| 475 |
return (
|
| 476 |
+
gr.update(visible=False),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
gr.update(value=model_acc_text, visible=True),
|
| 478 |
gr.update(visible=True),
|
| 479 |
gr.update(value=model_fig),
|
| 480 |
gr.update(value=knn_fig),
|
| 481 |
+
gr.update(value=map_fig, visible=True),
|
| 482 |
)
|
| 483 |
|
| 484 |
# Wire up navigation
|
|
|
|
| 493 |
outputs=[cur_idx, polygon_img, polygon_title, class_radio, progress_bar],
|
| 494 |
)
|
| 495 |
|
| 496 |
+
# Wire up class selection. IMPORTANT: use .input(), not .change() — on_prev/on_next
|
| 497 |
+
# also set class_radio's displayed value when navigating (to reflect any previously
|
| 498 |
+
# saved label), and .change() fires on *any* value update including that
|
| 499 |
+
# programmatic one, which was overwriting/re-saving the wrong polygon's label and
|
| 500 |
+
# made the last polygon's selection appear to never "stick". .input() only fires
|
| 501 |
+
# on genuine user interaction.
|
| 502 |
+
class_radio.input(
|
| 503 |
on_class_select,
|
| 504 |
inputs=[class_radio, cur_idx, labels],
|
| 505 |
outputs=[labels, progress_bar, progress_md, btn_submit],
|
| 506 |
)
|
| 507 |
|
| 508 |
+
# Wire up submit (labeling accuracy only)
|
| 509 |
btn_submit.click(
|
| 510 |
+
on_submit_labels,
|
| 511 |
+
inputs=[labels],
|
| 512 |
outputs=[
|
| 513 |
results_placeholder,
|
| 514 |
accuracy_md,
|
| 515 |
results_tbl,
|
| 516 |
row_plots_student,
|
| 517 |
student_matrix_plot,
|
| 518 |
+
],
|
| 519 |
+
)
|
| 520 |
+
|
| 521 |
+
# Wire up model training (separate tab, independent of the labeling submit)
|
| 522 |
+
btn_train.click(
|
| 523 |
+
on_train,
|
| 524 |
+
inputs=[labels, k_slider],
|
| 525 |
+
outputs=[
|
| 526 |
+
train_warning_md,
|
| 527 |
model_accuracy_md,
|
| 528 |
row_plots_model,
|
| 529 |
model_matrix_plot,
|