Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,21 +24,24 @@ model_tf = tf.saved_model.load(extract_dir)
|
|
| 24 |
|
| 25 |
# Función helper para inferencia TensorFlow
|
| 26 |
def predict_tf(img: Image.Image):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# 🔹 Cargar modelo ViT desde Hugging Face
|
| 44 |
MODEL_NAME = "ahishamm/vit-base-HAM-10000-sharpened-patch-32"
|
|
@@ -67,34 +70,51 @@ RISK_LEVELS = {
|
|
| 67 |
}
|
| 68 |
|
| 69 |
def analizar_lesion_combined(img):
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# Gráfico ViT
|
| 100 |
colors_bars = [RISK_LEVELS[i]['color'] for i in range(7)]
|
|
|
|
| 24 |
|
| 25 |
# Función helper para inferencia TensorFlow
|
| 26 |
def predict_tf(img: Image.Image):
|
| 27 |
+
try:
|
| 28 |
+
# Preprocesar imagen para TF: convertir a tensor float32, normalizar, añadir batch
|
| 29 |
+
img_resized = img.resize((224,224)) # ajusta según modelo
|
| 30 |
+
img_np = np.array(img_resized) / 255.0
|
| 31 |
+
if img_np.shape[-1] == 4: # eliminar canal alfa si existe
|
| 32 |
+
img_np = img_np[..., :3]
|
| 33 |
+
img_tf = tf.convert_to_tensor(img_np, dtype=tf.float32)
|
| 34 |
+
img_tf = tf.expand_dims(img_tf, axis=0) # batch dimension
|
| 35 |
+
|
| 36 |
+
# Ejecutar modelo (suponiendo firma default)
|
| 37 |
+
infer = model_tf.signatures["serving_default"]
|
| 38 |
+
output = infer(img_tf)
|
| 39 |
+
pred = list(output.values())[0].numpy()[0]
|
| 40 |
+
probs = tf.nn.softmax(pred).numpy()
|
| 41 |
+
return probs
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Error en predict_tf: {e}")
|
| 44 |
+
return np.zeros(len(CLASSES))
|
| 45 |
|
| 46 |
# 🔹 Cargar modelo ViT desde Hugging Face
|
| 47 |
MODEL_NAME = "ahishamm/vit-base-HAM-10000-sharpened-patch-32"
|
|
|
|
| 70 |
}
|
| 71 |
|
| 72 |
def analizar_lesion_combined(img):
|
| 73 |
+
try:
|
| 74 |
+
# Convertir imagen para Fastai
|
| 75 |
+
img_fastai = PILImage.create(img)
|
| 76 |
+
|
| 77 |
+
# ViT prediction
|
| 78 |
+
inputs = feature_extractor(img, return_tensors="pt")
|
| 79 |
+
with torch.no_grad():
|
| 80 |
+
outputs = model_vit(**inputs)
|
| 81 |
+
probs_vit = outputs.logits.softmax(dim=-1).cpu().numpy()[0]
|
| 82 |
+
pred_idx_vit = int(np.argmax(probs_vit))
|
| 83 |
+
pred_class_vit = CLASSES[pred_idx_vit]
|
| 84 |
+
confidence_vit = probs_vit[pred_idx_vit]
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Error en ViT prediction: {e}")
|
| 87 |
+
pred_class_vit = "Error"
|
| 88 |
+
confidence_vit = 0.0
|
| 89 |
+
probs_vit = np.zeros(len(CLASSES))
|
| 90 |
+
|
| 91 |
+
try:
|
| 92 |
+
# Fast.ai models
|
| 93 |
+
pred_fast_malignant, _, probs_fast_mal = model_malignancy.predict(img_fastai)
|
| 94 |
+
prob_malignant = float(probs_fast_mal[1]) # 1 = maligno
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f"Error en Fast.ai malignancy: {e}")
|
| 97 |
+
prob_malignant = 0.0
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
pred_fast_type, _, probs_fast_type = model_norm2000.predict(img_fastai)
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(f"Error en Fast.ai tipo: {e}")
|
| 103 |
+
pred_fast_type = "Error"
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
# TensorFlow model prediction
|
| 107 |
+
probs_tf = predict_tf(img)
|
| 108 |
+
pred_idx_tf = int(np.argmax(probs_tf))
|
| 109 |
+
confidence_tf = probs_tf[pred_idx_tf]
|
| 110 |
+
if pred_idx_tf < len(CLASSES):
|
| 111 |
+
pred_class_tf = CLASSES[pred_idx_tf]
|
| 112 |
+
else:
|
| 113 |
+
pred_class_tf = f"Clase desconocida (índice {pred_idx_tf})"
|
| 114 |
+
except Exception as e:
|
| 115 |
+
print(f"Error en TensorFlow prediction: {e}")
|
| 116 |
+
pred_class_tf = "Error"
|
| 117 |
+
confidence_tf = 0.0
|
| 118 |
|
| 119 |
# Gráfico ViT
|
| 120 |
colors_bars = [RISK_LEVELS[i]['color'] for i in range(7)]
|