Spaces:
Sleeping
Sleeping
Fix OOM crash when training KNN with large k by chunking full-image prediction; make the labeling-accuracy button actually jump to the Resultats tab
Browse files55 unmodified lines56575859606130 unmodified lines92939495969724 unmodified lines12212312412512612712824 unmodified lines15315415515615715815923 unmodified lines183184185186187188189497 unmodified lines687688689690691692186 unmodified lines87988088188288388455 unmodified linesBASE_DIR = os.path.dirname(os.path.abspath(__file__))# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Data loading (once at startup)# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ30 unmodified lines# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Model training (from the student's own labels + real spectral bands)# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef train_and_predict(student_labels, k):"""Train a KNN classifier on the student's 23 labeled polygons (using thereal spectral bands), then evaluate it only at the Ground Truth pixel24 unmodified lines# with a known Ground Truth. We can't evaluate accuracy outside GT locations# (no known answer there) but we can still show what the model predicted.H, W, n_bands = bands.shapepred_full = clf.predict(scaler.transform(bands.reshape(-1, n_bands))).reshape(H, W)return model_oa, model_matrix, pred_gt, y_gt, pred_full24 unmodified linesmodel_oa = model_matrix.diagonal().sum() / model_matrix.sum()H, W, n_bands = bands.shapepred_full = clf.predict(scaler.transform(bands.reshape(-1, n_bands))).reshape(H, W)return model_oa, model_matrix, pred_gt, y_gt, pred_full23 unmodified linesmodel_oa = model_matrix.diagonal().sum() / model_matrix.sum()H, W, n_bands = bands.shapepred_full = clf.predict(bands.reshape(-1, n_bands)).reshape(H, W)return model_oa, model_matrix, pred_gt, y_gt, pred_full497 unmodified linesgr.update(value=table, visible=True),gr.update(visible=True),gr.update(value=student_fig),)def on_train(current_labels, k):186 unmodified linesresults_tbl,row_plots_student,student_matrix_plot,],)55 unmodified lines565758596061626364656667686930 unmodified lines10010110210310410510610710810911011111211311424 unmodified lines13914014114214314414524 unmodified lines17017117217317417517623 unmodified lines200201202203204205206497 unmodified lines704705706707708709710186 unmodified lines89789889990090190290355 unmodified linesBASE_DIR = os.path.dirname(os.path.abspath(__file__))# Chunk size for predicting the full ~4.6M-pixel image. KNN's predict() builds# a (n_queries, k) neighbor distance/index array β at k=100 over the full# image that's ~4.6M x 100 x 8 bytes x 2 β 7.4GB in one call, which (combined# with n_jobs=-1 worker overhead) exceeded the Space's 16GB memory limit and# crashed it. Predicting in bounded chunks keeps peak memory constant# regardless of k or image size.FULL_PREDICT_CHUNK = 200_000# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Data loading (once at startup)# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ30 unmodified lines# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# Model training (from the student's own labels + real spectral bands)# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef predict_full_image(clf, X_full, H, W):"""Predict on the whole image in bounded chunks (see FULL_PREDICT_CHUNK)instead of one call over all ~4.6M pixels at once."""preds = np.empty(X_full.shape[0], dtype=np.uint8)for start in range(0, X_full.shape[0], FULL_PREDICT_CHUNK):end = start + FULL_PREDICT_CHUNKpreds[start:end] = clf.predict(X_full[start:end])return preds.reshape(H, W)def train_and_predict(student_labels, k):"""Train a KNN classifier on the student's 23 labeled polygons (using thereal spectral bands), then evaluate it only at the Ground Truth pixel24 unmodified lines# with a known Ground Truth. We can't evaluate accuracy outside GT locations# (no known answer there) but we can still show what the model predicted.H, W, n_bands = bands.shapepred_full = predict_full_image(clf, scaler.transform(bands.reshape(-1, n_bands)), H, W)return model_oa, model_matrix, pred_gt, y_gt, pred_full24 unmodified linesmodel_oa = model_matrix.diagonal().sum() / model_matrix.sum()H, W, n_bands = bands.shapepred_full = predict_full_image(clf, scaler.transform(bands.reshape(-1, n_bands)), H, W)return model_oa, model_matrix, pred_gt, y_gt, pred_full23 unmodified linesmodel_oa = model_matrix.diagonal().sum() / model_matrix.sum()H, W, n_bands = bands.shapepred_full = predict_full_image(clf, bands.reshape(-1, n_bands), H, W)return model_oa, model_matrix, pred_gt, y_gt, pred_full497 unmodified linesgr.update(value=table, visible=True),gr.update(visible=True),gr.update(value=student_fig),gr.update(selected="tab_results"),)def on_train(current_labels, k):186 unmodified linesresults_tbl,row_plots_student,student_matrix_plot,tabs,],)
app.py+22-3