Spaces:
Sleeping
Sleeping
Fix class-code mismatch in ground_truth/knn_result/teacher-answer rasters vs Excel key; add full-image (unmasked) classification maps
Browse files- app.py +53 -4
- data/polygon_teacher_classes.json +25 -25
app.py
CHANGED
|
@@ -38,6 +38,14 @@ BAND_FILES = [
|
|
| 38 |
'band_5_nir.tif', 'band_6_swir1.tif', 'band_7_swir2.tif',
|
| 39 |
]
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 42 |
|
| 43 |
# ─────────────────────────────────────────────────────────────────────────────
|
|
@@ -49,8 +57,8 @@ def load_data():
|
|
| 49 |
with rasterio.open(path) as src:
|
| 50 |
return src.read(1)
|
| 51 |
|
| 52 |
-
ground_truth = read_tif('ground_truth.tif')
|
| 53 |
-
knn_result = read_tif('knn_result.tif')
|
| 54 |
training_ids = read_tif('training_polygons.tif')
|
| 55 |
|
| 56 |
bands = np.stack(
|
|
@@ -116,7 +124,13 @@ def train_and_predict(student_labels, k):
|
|
| 116 |
np.add.at(model_matrix, (y_gt - 1, pred_gt - 1), 1)
|
| 117 |
model_oa = model_matrix.diagonal().sum() / model_matrix.sum()
|
| 118 |
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 122 |
# Visualization helpers
|
|
@@ -203,6 +217,27 @@ def fig_gt_maps(pred_gt, y_gt, knn_ref_gt):
|
|
| 203 |
plt.tight_layout()
|
| 204 |
return fig
|
| 205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
def fig_student_matrix(student_labels):
|
| 207 |
"""7×7 confusion matrix: student labels vs teacher labels."""
|
| 208 |
matrix = np.zeros((7, 7), dtype=int)
|
|
@@ -389,6 +424,16 @@ pixels de vérité terrain (indépendants des polygones d'entraînement).
|
|
| 389 |
label="Cartes aux emplacements de vérité terrain (seuls points où la vraie classe est connue)",
|
| 390 |
visible=False,
|
| 391 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
|
| 393 |
# ─────────────────────────────────────────────────────────────────────
|
| 394 |
# Event handlers
|
|
@@ -481,9 +526,10 @@ pixels de vérité terrain (indépendants des polygones d'entraînement).
|
|
| 481 |
gr.update(),
|
| 482 |
gr.update(),
|
| 483 |
gr.update(visible=False),
|
|
|
|
| 484 |
)
|
| 485 |
|
| 486 |
-
model_oa, model_matrix, pred_gt, y_gt, knn_ref_gt = train_and_predict(
|
| 487 |
current_labels, k
|
| 488 |
)
|
| 489 |
model_acc_text = (
|
|
@@ -495,6 +541,7 @@ pixels de vérité terrain (indépendants des polygones d'entraînement).
|
|
| 495 |
model_fig = fig_model_matrix(model_matrix, model_oa)
|
| 496 |
knn_fig = fig_knn_matrix()
|
| 497 |
map_fig = fig_gt_maps(pred_gt, y_gt, knn_ref_gt)
|
|
|
|
| 498 |
|
| 499 |
return (
|
| 500 |
gr.update(visible=False),
|
|
@@ -503,6 +550,7 @@ pixels de vérité terrain (indépendants des polygones d'entraînement).
|
|
| 503 |
gr.update(value=model_fig),
|
| 504 |
gr.update(value=knn_fig),
|
| 505 |
gr.update(value=map_fig, visible=True),
|
|
|
|
| 506 |
)
|
| 507 |
|
| 508 |
# Wire up navigation (Précédent / Suivant / direct jump-to-polygon dropdown —
|
|
@@ -563,6 +611,7 @@ pixels de vérité terrain (indépendants des polygones d'entraînement).
|
|
| 563 |
model_matrix_plot,
|
| 564 |
knn_plot,
|
| 565 |
three_panel_plot,
|
|
|
|
| 566 |
],
|
| 567 |
)
|
| 568 |
|
|
|
|
| 38 |
'band_5_nir.tif', 'band_6_swir1.tif', 'band_7_swir2.tif',
|
| 39 |
]
|
| 40 |
|
| 41 |
+
# ground_truth.tif and knn_result.tif were exported (QGIS) with a class-code order
|
| 42 |
+
# that does NOT match CLASSES above. Confirmed by cross-referencing the professor's
|
| 43 |
+
# Excel answer key (Copie de TD_Vallee du Nil_Complet.xlsx, sheet "Training (RSL)")
|
| 44 |
+
# against the raw codes at the 23 training-polygon locations (23/23 match this single
|
| 45 |
+
# permutation), and independently via NIR/SWIR spectral signature per code. Index i of
|
| 46 |
+
# this LUT is the raw code found in the tif, value is the true CLASSES code.
|
| 47 |
+
CLASS_REMAP_LUT = np.array([0, 1, 4, 7, 5, 6, 2, 3], dtype=np.uint8)
|
| 48 |
+
|
| 49 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 50 |
|
| 51 |
# ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
| 57 |
with rasterio.open(path) as src:
|
| 58 |
return src.read(1)
|
| 59 |
|
| 60 |
+
ground_truth = CLASS_REMAP_LUT[read_tif('ground_truth.tif')]
|
| 61 |
+
knn_result = CLASS_REMAP_LUT[read_tif('knn_result.tif')]
|
| 62 |
training_ids = read_tif('training_polygons.tif')
|
| 63 |
|
| 64 |
bands = np.stack(
|
|
|
|
| 124 |
np.add.at(model_matrix, (y_gt - 1, pred_gt - 1), 1)
|
| 125 |
model_oa = model_matrix.diagonal().sum() / model_matrix.sum()
|
| 126 |
|
| 127 |
+
# Full-image prediction: the model classifies EVERY pixel, not just the ones
|
| 128 |
+
# with a known Ground Truth. We can't evaluate accuracy outside GT locations
|
| 129 |
+
# (no known answer there) but we can still show what the model predicted.
|
| 130 |
+
H, W, n_bands = bands.shape
|
| 131 |
+
pred_full = clf.predict(scaler.transform(bands.reshape(-1, n_bands))).reshape(H, W)
|
| 132 |
+
|
| 133 |
+
return model_oa, model_matrix, pred_gt, y_gt, knn_ref_gt, pred_full
|
| 134 |
|
| 135 |
# ─────────────────────────────────────────────────────────────────────────────
|
| 136 |
# Visualization helpers
|
|
|
|
| 217 |
plt.tight_layout()
|
| 218 |
return fig
|
| 219 |
|
| 220 |
+
def fig_full_maps(pred_full, knn_full):
|
| 221 |
+
"""Full-image classification maps: EVERY pixel, not just the ~21k Ground
|
| 222 |
+
Truth locations. Both your model and the reference model classify the whole
|
| 223 |
+
image — but outside Ground Truth locations there is no known right answer,
|
| 224 |
+
so accuracy can't be computed here. This is purely illustrative."""
|
| 225 |
+
fig, axes = plt.subplots(1, 2, figsize=(14, 6.5))
|
| 226 |
+
panels = [
|
| 227 |
+
(pred_full, "Votre modèle KNN — carte complète"),
|
| 228 |
+
(knn_full, "Modèle de référence (enseignant) — carte complète"),
|
| 229 |
+
]
|
| 230 |
+
for ax, (classes, title) in zip(axes, panels):
|
| 231 |
+
ax.imshow(COLORS_RGB[classes])
|
| 232 |
+
ax.set_title(title, fontsize=11, fontweight='bold')
|
| 233 |
+
ax.axis('off')
|
| 234 |
+
fig.suptitle(
|
| 235 |
+
"Classification sur l'ensemble de l'image (précision non évaluable hors vérité terrain)",
|
| 236 |
+
fontsize=11,
|
| 237 |
+
)
|
| 238 |
+
plt.tight_layout()
|
| 239 |
+
return fig
|
| 240 |
+
|
| 241 |
def fig_student_matrix(student_labels):
|
| 242 |
"""7×7 confusion matrix: student labels vs teacher labels."""
|
| 243 |
matrix = np.zeros((7, 7), dtype=int)
|
|
|
|
| 424 |
label="Cartes aux emplacements de vérité terrain (seuls points où la vraie classe est connue)",
|
| 425 |
visible=False,
|
| 426 |
)
|
| 427 |
+
gr.Markdown(
|
| 428 |
+
"*(Ci-dessous : les deux modèles classifient en réalité l'image entière — "
|
| 429 |
+
"y compris les zones sans vérité terrain. On ne peut pas y vérifier si "
|
| 430 |
+
"c'est correct, mais on peut voir à quoi ressemble la classification complète.)*",
|
| 431 |
+
visible=True,
|
| 432 |
+
)
|
| 433 |
+
full_maps_plot = gr.Plot(
|
| 434 |
+
label="Cartes de classification complètes (tous les pixels, précision non évaluable)",
|
| 435 |
+
visible=False,
|
| 436 |
+
)
|
| 437 |
|
| 438 |
# ─────────────────────────────────────────────────────────────────────
|
| 439 |
# Event handlers
|
|
|
|
| 526 |
gr.update(),
|
| 527 |
gr.update(),
|
| 528 |
gr.update(visible=False),
|
| 529 |
+
gr.update(visible=False),
|
| 530 |
)
|
| 531 |
|
| 532 |
+
model_oa, model_matrix, pred_gt, y_gt, knn_ref_gt, pred_full = train_and_predict(
|
| 533 |
current_labels, k
|
| 534 |
)
|
| 535 |
model_acc_text = (
|
|
|
|
| 541 |
model_fig = fig_model_matrix(model_matrix, model_oa)
|
| 542 |
knn_fig = fig_knn_matrix()
|
| 543 |
map_fig = fig_gt_maps(pred_gt, y_gt, knn_ref_gt)
|
| 544 |
+
full_fig = fig_full_maps(pred_full, DATA['knn_result'])
|
| 545 |
|
| 546 |
return (
|
| 547 |
gr.update(visible=False),
|
|
|
|
| 550 |
gr.update(value=model_fig),
|
| 551 |
gr.update(value=knn_fig),
|
| 552 |
gr.update(value=map_fig, visible=True),
|
| 553 |
+
gr.update(value=full_fig, visible=True),
|
| 554 |
)
|
| 555 |
|
| 556 |
# Wire up navigation (Précédent / Suivant / direct jump-to-polygon dropdown —
|
|
|
|
| 611 |
model_matrix_plot,
|
| 612 |
knn_plot,
|
| 613 |
three_panel_plot,
|
| 614 |
+
full_maps_plot,
|
| 615 |
],
|
| 616 |
)
|
| 617 |
|
data/polygon_teacher_classes.json
CHANGED
|
@@ -1,25 +1,25 @@
|
|
| 1 |
-
{
|
| 2 |
-
"1": 1,
|
| 3 |
-
"2":
|
| 4 |
-
"3":
|
| 5 |
-
"4": 1,
|
| 6 |
-
"5": 1,
|
| 7 |
-
"6":
|
| 8 |
-
"7":
|
| 9 |
-
"8":
|
| 10 |
-
"9":
|
| 11 |
-
"10":
|
| 12 |
-
"11":
|
| 13 |
-
"12":
|
| 14 |
-
"13": 1,
|
| 15 |
-
"14":
|
| 16 |
-
"15": 1,
|
| 17 |
-
"16":
|
| 18 |
-
"17":
|
| 19 |
-
"18":
|
| 20 |
-
"19":
|
| 21 |
-
"20":
|
| 22 |
-
"21":
|
| 23 |
-
"22": 1,
|
| 24 |
-
"23": 1
|
| 25 |
-
}
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"1": 1,
|
| 3 |
+
"2": 4,
|
| 4 |
+
"3": 4,
|
| 5 |
+
"4": 1,
|
| 6 |
+
"5": 1,
|
| 7 |
+
"6": 7,
|
| 8 |
+
"7": 7,
|
| 9 |
+
"8": 7,
|
| 10 |
+
"9": 7,
|
| 11 |
+
"10": 7,
|
| 12 |
+
"11": 7,
|
| 13 |
+
"12": 7,
|
| 14 |
+
"13": 1,
|
| 15 |
+
"14": 5,
|
| 16 |
+
"15": 1,
|
| 17 |
+
"16": 6,
|
| 18 |
+
"17": 2,
|
| 19 |
+
"18": 3,
|
| 20 |
+
"19": 3,
|
| 21 |
+
"20": 3,
|
| 22 |
+
"21": 3,
|
| 23 |
+
"22": 1,
|
| 24 |
+
"23": 1
|
| 25 |
+
}
|