Spaces:
Sleeping
Sleeping
Remove KNN classification map from results
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import numpy as np
|
|
| 3 |
import matplotlib
|
| 4 |
matplotlib.use('Agg')
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
-
import matplotlib.patches as mpatches
|
| 7 |
import rasterio
|
| 8 |
import json
|
| 9 |
import os
|
|
@@ -25,18 +24,6 @@ CLASSES = {
|
|
| 25 |
|
| 26 |
CLASS_CHOICES = [f"{k} - {v}" for k, v in CLASSES.items()]
|
| 27 |
|
| 28 |
-
# Distinctive color palette per class (index 0 = background)
|
| 29 |
-
COLORS_RGB = np.array([
|
| 30 |
-
[20, 20, 20 ], # 0 background
|
| 31 |
-
[0, 100, 220], # 1 eau
|
| 32 |
-
[0, 160, 60], # 2 vergers
|
| 33 |
-
[120, 220, 100], # 3 cultures delta
|
| 34 |
-
[220, 50, 50], # 4 zones bรขties
|
| 35 |
-
[255, 165, 0], # 5 cultures irriguรฉes dรฉsert
|
| 36 |
-
[160, 90, 30], # 6 cultures non irriguรฉes
|
| 37 |
-
[240, 230, 140], # 7 zones sableuses
|
| 38 |
-
], dtype=np.uint8)
|
| 39 |
-
|
| 40 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 41 |
|
| 42 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
@@ -48,10 +35,8 @@ def load_data():
|
|
| 48 |
with rasterio.open(path) as src:
|
| 49 |
return src.read(1)
|
| 50 |
|
| 51 |
-
training_ids = read_tif('training_polygons.tif')
|
| 52 |
ground_truth = read_tif('ground_truth.tif')
|
| 53 |
knn_result = read_tif('knn_result.tif')
|
| 54 |
-
teacher_lbl = read_tif('teacher_labels.tif')
|
| 55 |
|
| 56 |
with open(os.path.join(BASE_DIR, 'data', 'polygon_teacher_classes.json')) as f:
|
| 57 |
polygon_teacher = {int(k): v for k, v in json.load(f).items()}
|
|
@@ -64,25 +49,10 @@ def load_data():
|
|
| 64 |
np.add.at(knn_matrix, (gt_flat[valid] - 1, knn_flat[valid] - 1), 1)
|
| 65 |
knn_oa = knn_matrix.diagonal().sum() / knn_matrix.sum()
|
| 66 |
|
| 67 |
-
# Downsample rasters to ~500 px height for display
|
| 68 |
-
from PIL import Image as PILImage
|
| 69 |
-
h, w = training_ids.shape
|
| 70 |
-
new_h, new_w = 500, int(w * 500 / h)
|
| 71 |
-
|
| 72 |
-
def small(arr):
|
| 73 |
-
return np.array(PILImage.fromarray(arr).resize((new_w, new_h), PILImage.NEAREST))
|
| 74 |
-
|
| 75 |
return dict(
|
| 76 |
-
training_ids = training_ids,
|
| 77 |
-
ground_truth = ground_truth,
|
| 78 |
-
knn_result = knn_result,
|
| 79 |
-
teacher_lbl = teacher_lbl,
|
| 80 |
polygon_teacher= polygon_teacher,
|
| 81 |
knn_matrix = knn_matrix,
|
| 82 |
knn_oa = knn_oa,
|
| 83 |
-
tr_small = small(training_ids),
|
| 84 |
-
gt_small = small(ground_truth),
|
| 85 |
-
knn_small = small(knn_result),
|
| 86 |
)
|
| 87 |
|
| 88 |
DATA = load_data()
|
|
@@ -90,38 +60,6 @@ DATA = load_data()
|
|
| 90 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 91 |
# Visualization helpers
|
| 92 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 93 |
-
def raster_to_rgb(arr):
|
| 94 |
-
rgb = COLORS_RGB[arr]
|
| 95 |
-
return rgb
|
| 96 |
-
|
| 97 |
-
def legend_patches():
|
| 98 |
-
return [mpatches.Patch(color=COLORS_RGB[i]/255, label=f"{i} โ {CLASSES[i]}")
|
| 99 |
-
for i in range(1, 8)]
|
| 100 |
-
|
| 101 |
-
def fig_maps(student_labels):
|
| 102 |
-
fig, axes = plt.subplots(1, 3, figsize=(18, 6))
|
| 103 |
-
fig.patch.set_facecolor('#f5f5f5')
|
| 104 |
-
|
| 105 |
-
student_map = np.zeros_like(DATA['tr_small'])
|
| 106 |
-
for pid, label in enumerate(student_labels, 1):
|
| 107 |
-
if label is not None:
|
| 108 |
-
student_map[DATA['tr_small'] == pid] = label
|
| 109 |
-
|
| 110 |
-
titles = [
|
| 111 |
-
"Votre interprรฉtation\n(polygones d'entraรฎnement)",
|
| 112 |
-
"Classification KNN\n(carte produite par l'IA)",
|
| 113 |
-
"Vรฉritรฉ terrain\n(zones de validation)",
|
| 114 |
-
]
|
| 115 |
-
rasters = [student_map, DATA['knn_small'], DATA['gt_small']]
|
| 116 |
-
for ax, rast, title in zip(axes, rasters, titles):
|
| 117 |
-
ax.imshow(raster_to_rgb(rast), interpolation='nearest')
|
| 118 |
-
ax.set_title(title, fontsize=11, fontweight='bold', pad=8)
|
| 119 |
-
ax.axis('off')
|
| 120 |
-
|
| 121 |
-
fig.legend(handles=legend_patches(), loc='lower center', ncol=4,
|
| 122 |
-
bbox_to_anchor=(0.5, -0.06), fontsize=9, framealpha=0.9)
|
| 123 |
-
plt.tight_layout()
|
| 124 |
-
return fig
|
| 125 |
|
| 126 |
def fig_knn_matrix():
|
| 127 |
matrix = DATA['knn_matrix']
|
|
@@ -305,7 +243,6 @@ puis soumettez vos rรฉponses pour gรฉnรฉrer la carte et la matrice de confusion.
|
|
| 305 |
with gr.Row(visible=False) as row_plots_student:
|
| 306 |
student_matrix_plot = gr.Plot(label="Votre interprรฉtation vs Enseignant")
|
| 307 |
|
| 308 |
-
maps_plot = gr.Plot(label="Cartes", visible=False)
|
| 309 |
knn_plot = gr.Plot(label="Matrice de confusion KNN vs Vรฉritรฉ terrain", visible=False)
|
| 310 |
|
| 311 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
@@ -369,17 +306,15 @@ puis soumettez vos rรฉponses pour gรฉnรฉrer la carte et la matrice de confusion.
|
|
| 369 |
|
| 370 |
table = build_results_table(current_labels)
|
| 371 |
|
| 372 |
-
maps_fig = fig_maps(current_labels)
|
| 373 |
knn_fig = fig_knn_matrix()
|
| 374 |
student_fig = fig_student_matrix(current_labels)
|
| 375 |
|
| 376 |
return (
|
| 377 |
-
gr.update(value=""),
|
| 378 |
gr.update(value=acc_text, visible=True),
|
| 379 |
gr.update(value=table, visible=True),
|
| 380 |
gr.update(visible=True),
|
| 381 |
gr.update(value=student_fig),
|
| 382 |
-
gr.update(value=maps_fig, visible=True),
|
| 383 |
gr.update(value=knn_fig, visible=True),
|
| 384 |
)
|
| 385 |
|
|
@@ -412,7 +347,6 @@ puis soumettez vos rรฉponses pour gรฉnรฉrer la carte et la matrice de confusion.
|
|
| 412 |
results_tbl,
|
| 413 |
row_plots_student,
|
| 414 |
student_matrix_plot,
|
| 415 |
-
maps_plot,
|
| 416 |
knn_plot,
|
| 417 |
],
|
| 418 |
)
|
|
|
|
| 3 |
import matplotlib
|
| 4 |
matplotlib.use('Agg')
|
| 5 |
import matplotlib.pyplot as plt
|
|
|
|
| 6 |
import rasterio
|
| 7 |
import json
|
| 8 |
import os
|
|
|
|
| 24 |
|
| 25 |
CLASS_CHOICES = [f"{k} - {v}" for k, v in CLASSES.items()]
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 28 |
|
| 29 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
| 35 |
with rasterio.open(path) as src:
|
| 36 |
return src.read(1)
|
| 37 |
|
|
|
|
| 38 |
ground_truth = read_tif('ground_truth.tif')
|
| 39 |
knn_result = read_tif('knn_result.tif')
|
|
|
|
| 40 |
|
| 41 |
with open(os.path.join(BASE_DIR, 'data', 'polygon_teacher_classes.json')) as f:
|
| 42 |
polygon_teacher = {int(k): v for k, v in json.load(f).items()}
|
|
|
|
| 49 |
np.add.at(knn_matrix, (gt_flat[valid] - 1, knn_flat[valid] - 1), 1)
|
| 50 |
knn_oa = knn_matrix.diagonal().sum() / knn_matrix.sum()
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return dict(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
polygon_teacher= polygon_teacher,
|
| 54 |
knn_matrix = knn_matrix,
|
| 55 |
knn_oa = knn_oa,
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
DATA = load_data()
|
|
|
|
| 60 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 61 |
# Visualization helpers
|
| 62 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
def fig_knn_matrix():
|
| 65 |
matrix = DATA['knn_matrix']
|
|
|
|
| 243 |
with gr.Row(visible=False) as row_plots_student:
|
| 244 |
student_matrix_plot = gr.Plot(label="Votre interprรฉtation vs Enseignant")
|
| 245 |
|
|
|
|
| 246 |
knn_plot = gr.Plot(label="Matrice de confusion KNN vs Vรฉritรฉ terrain", visible=False)
|
| 247 |
|
| 248 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
| 306 |
|
| 307 |
table = build_results_table(current_labels)
|
| 308 |
|
|
|
|
| 309 |
knn_fig = fig_knn_matrix()
|
| 310 |
student_fig = fig_student_matrix(current_labels)
|
| 311 |
|
| 312 |
return (
|
| 313 |
+
gr.update(value=""),
|
| 314 |
gr.update(value=acc_text, visible=True),
|
| 315 |
gr.update(value=table, visible=True),
|
| 316 |
gr.update(visible=True),
|
| 317 |
gr.update(value=student_fig),
|
|
|
|
| 318 |
gr.update(value=knn_fig, visible=True),
|
| 319 |
)
|
| 320 |
|
|
|
|
| 347 |
results_tbl,
|
| 348 |
row_plots_student,
|
| 349 |
student_matrix_plot,
|
|
|
|
| 350 |
knn_plot,
|
| 351 |
],
|
| 352 |
)
|