File size: 19,075 Bytes
4790918 54b9d20 6045f26 4790918 6be1f83 4790918 6be1f83 4790918 7711f86 4790918 6be1f83 4790918 6be1f83 4790918 6be1f83 4790918 6be1f83 4790918 6be1f83 4790918 6be1f83 4790918 6be1f83 4790918 6be1f83 4790918 6be1f83 4790918 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 4790918 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 4790918 6be1f83 54b9d20 6be1f83 4790918 6be1f83 4790918 6be1f83 4790918 6be1f83 4790918 54b9d20 6be1f83 54b9d20 6be1f83 4790918 6be1f83 4790918 6be1f83 54b9d20 6be1f83 4790918 6be1f83 54b9d20 6be1f83 4790918 6be1f83 4790918 6be1f83 54b9d20 6be1f83 4790918 6be1f83 6045f26 6be1f83 6045f26 6be1f83 6045f26 4790918 6be1f83 4790918 6be1f83 4790918 6be1f83 6045f26 6be1f83 6045f26 6be1f83 4790918 6be1f83 4790918 6be1f83 4790918 6be1f83 6045f26 6be1f83 6045f26 6be1f83 6045f26 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 6045f26 6be1f83 6045f26 6be1f83 6045f26 6be1f83 6045f26 6be1f83 6045f26 6be1f83 6045f26 4790918 6045f26 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 54b9d20 6be1f83 6045f26 6be1f83 6045f26 6be1f83 4790918 6045f26 6be1f83 6045f26 6be1f83 4790918 6be1f83 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
import gradio as gr
import os
import io
import png
import tensorflow as tf
import tensorflow_text as tf_text
import tensorflow_hub as tf_hub
import numpy as np
from PIL import Image
from huggingface_hub import snapshot_download, HfFolder
from sklearn.metrics.pairwise import cosine_similarity
import traceback
import time
import pandas as pd # Para formatear la salida en tabla
# --- Configuración ---
MODEL_REPO_ID = "google/cxr-foundation"
MODEL_DOWNLOAD_DIR = './hf_cxr_foundation_space' # Directorio dentro del contenedor del Space
SIMILARITY_DIFFERENCE_THRESHOLD = 0.1
POSITIVE_SIMILARITY_THRESHOLD = 0.1
print(f"Usando umbrales: Comp Δ={SIMILARITY_DIFFERENCE_THRESHOLD}, Simp τ={POSITIVE_SIMILARITY_THRESHOLD}")
# --- Prompts ---
criteria_list_positive = [
"optimal centering", "optimal inspiration", "optimal penetration",
"complete field of view", "scapulae retracted", "sharp image", "artifact free"
]
criteria_list_negative = [
"poorly centered", "poor inspiration", "non-diagnostic exposure",
"cropped image", "scapulae overlying lungs", "blurred image", "obscuring artifact"
]
# --- Funciones Auxiliares ---
def bert_tokenize(text, preprocessor):
if preprocessor is None: raise ValueError("BERT preprocessor no está cargado.")
if not isinstance(text, str): text = str(text)
out = preprocessor(tf.constant([text.lower()]))
ids = out['input_word_ids'].numpy().astype(np.int32)
masks = out['input_mask'].numpy().astype(np.float32)
paddings = 1.0 - masks
end_token_idx = (ids == 102)
ids[end_token_idx] = 0
paddings[end_token_idx] = 1.0
if ids.ndim == 2: ids = np.expand_dims(ids, axis=1)
if paddings.ndim == 2: paddings = np.expand_dims(paddings, axis=1)
expected_shape = (1, 1, 128)
if ids.shape != expected_shape:
if ids.shape == (1,128): ids = np.expand_dims(ids, axis=1)
else: raise ValueError(f"Shape incorrecta para ids: {ids.shape}, esperado {expected_shape}")
if paddings.shape != expected_shape:
if paddings.shape == (1,128): paddings = np.expand_dims(paddings, axis=1)
else: raise ValueError(f"Shape incorrecta para paddings: {paddings.shape}, esperado {expected_shape}")
return ids, paddings
def png_to_tfexample(image_array: np.ndarray) -> tf.train.Example:
if image_array.ndim == 3 and image_array.shape[2] == 1:
image_array = np.squeeze(image_array, axis=2)
elif image_array.ndim != 2:
raise ValueError(f'Array debe ser 2-D. Dimensiones: {image_array.ndim}')
image = image_array.astype(np.float32)
min_val, max_val = image.min(), image.max()
if max_val <= min_val:
if image_array.dtype == np.uint8 or (min_val >= 0 and max_val <= 255):
pixel_array = image.astype(np.uint8); bitdepth = 8
else:
pixel_array = np.zeros_like(image, dtype=np.uint16); bitdepth = 16
else:
image -= min_val
current_max = max_val - min_val
if image_array.dtype != np.uint8:
image *= 65535 / current_max
pixel_array = image.astype(np.uint16); bitdepth = 16
else:
image *= 255 / current_max
pixel_array = image.astype(np.uint8); bitdepth = 8
output = io.BytesIO()
png.Writer(width=pixel_array.shape[1], height=pixel_array.shape[0], greyscale=True, bitdepth=bitdepth).write(output, pixel_array.tolist())
example = tf.train.Example()
features = example.features.feature
features['image/encoded'].bytes_list.value.append(output.getvalue())
features['image/format'].bytes_list.value.append(b'png')
return example
def generate_image_embedding(img_np, elixrc_infer, qformer_infer):
if elixrc_infer is None or qformer_infer is None: raise ValueError("Modelos ELIXR-C o QFormer no cargados.")
try:
serialized_img_tf_example = png_to_tfexample(img_np).SerializeToString()
elixrc_output = elixrc_infer(input_example=tf.constant([serialized_img_tf_example]))
elixrc_embedding = elixrc_output['feature_maps_0'].numpy()
qformer_input_img = {
'image_feature': elixrc_embedding.tolist(),
'ids': np.zeros((1, 1, 128), dtype=np.int32).tolist(),
'paddings': np.ones((1, 1, 128), dtype=np.float32).tolist(),
}
qformer_output_img = qformer_infer(**qformer_input_img)
image_embedding = qformer_output_img['all_contrastive_img_emb'].numpy()
if image_embedding.ndim > 2:
image_embedding = np.mean(image_embedding, axis=tuple(range(1, image_embedding.ndim - 1)))
if image_embedding.ndim == 1: image_embedding = np.expand_dims(image_embedding, axis=0)
if image_embedding.ndim != 2: raise ValueError(f"Embedding final no tiene 2 dims: {image_embedding.shape}")
return image_embedding
except Exception as e:
print(f"Error generando embedding imagen: {e}"); traceback.print_exc(); raise
def calculate_similarities_and_classify(image_embedding, bert_preprocessor, qformer_infer):
if image_embedding is None: raise ValueError("Embedding imagen es None.")
if bert_preprocessor is None: raise ValueError("Preprocesador BERT es None.")
if qformer_infer is None: raise ValueError("QFormer es None.")
detailed_results = {}
print("\n--- Calculando similitudes ---")
for i in range(len(criteria_list_positive)):
positive_text, negative_text = criteria_list_positive[i], criteria_list_negative[i]
criterion_name = positive_text
print(f"Procesando: \"{criterion_name}\"")
similarity_positive, similarity_negative, difference = None, None, None
classification_comp, classification_simp = "ERROR", "ERROR"
try:
tokens_pos, paddings_pos = bert_tokenize(positive_text, bert_preprocessor)
qformer_input_pos = {'image_feature': np.zeros([1, 8, 8, 1376], dtype=np.float32).tolist(), 'ids': tokens_pos.tolist(), 'paddings': paddings_pos.tolist()}
text_embedding_pos = qformer_infer(**qformer_input_pos)['contrastive_txt_emb'].numpy()
if text_embedding_pos.ndim == 1: text_embedding_pos = np.expand_dims(text_embedding_pos, axis=0)
tokens_neg, paddings_neg = bert_tokenize(negative_text, bert_preprocessor)
qformer_input_neg = {'image_feature': np.zeros([1, 8, 8, 1376], dtype=np.float32).tolist(), 'ids': tokens_neg.tolist(), 'paddings': paddings_neg.tolist()}
text_embedding_neg = qformer_infer(**qformer_input_neg)['contrastive_txt_emb'].numpy()
if text_embedding_neg.ndim == 1: text_embedding_neg = np.expand_dims(text_embedding_neg, axis=0)
if image_embedding.shape[1] != text_embedding_pos.shape[1]: raise ValueError(f"Dim mismatch: Img ({image_embedding.shape[1]}) vs Pos ({text_embedding_pos.shape[1]})")
if image_embedding.shape[1] != text_embedding_neg.shape[1]: raise ValueError(f"Dim mismatch: Img ({image_embedding.shape[1]}) vs Neg ({text_embedding_neg.shape[1]})")
similarity_positive = cosine_similarity(image_embedding, text_embedding_pos)[0][0]
similarity_negative = cosine_similarity(image_embedding, text_embedding_neg)[0][0]
difference = similarity_positive - similarity_negative
classification_comp = "PASS" if difference > SIMILARITY_DIFFERENCE_THRESHOLD else "FAIL"
classification_simp = "PASS" if similarity_positive > POSITIVE_SIMILARITY_THRESHOLD else "FAIL"
print(f" Sim(+)={similarity_positive:.4f}, Sim(-)={similarity_negative:.4f}, Diff={difference:.4f} -> Comp:{classification_comp}, Simp:{classification_simp}")
except Exception as e:
print(f" ERROR criterio '{criterion_name}': {e}"); traceback.print_exc()
detailed_results[criterion_name] = {
'positive_prompt': positive_text, 'negative_prompt': negative_text,
'similarity_positive': float(similarity_positive) if similarity_positive is not None else None,
'similarity_negative': float(similarity_negative) if similarity_negative is not None else None,
'difference': float(difference) if difference is not None else None,
'classification_comparative': classification_comp, 'classification_simplified': classification_simp
}
return detailed_results
# --- Carga Global de Modelos ---
print("--- Iniciando carga global de modelos ---")
start_time = time.time()
models_loaded = False
bert_preprocessor_global = None
elixrc_infer_global = None
qformer_infer_global = None
try:
hf_token = os.environ.get("HF_TOKEN") # Leer token desde secretos del Space
if hf_token: print("Usando HF_TOKEN para autenticación.")
os.makedirs(MODEL_DOWNLOAD_DIR, exist_ok=True)
print(f"Descargando/verificando modelos en: {MODEL_DOWNLOAD_DIR}")
snapshot_download(repo_id=MODEL_REPO_ID, local_dir=MODEL_DOWNLOAD_DIR,
allow_patterns=['elixr-c-v2-pooled/*', 'pax-elixr-b-text/*'],
local_dir_use_symlinks=False, token=hf_token) # Pasar token
print("Modelos descargados/verificados.")
print("Cargando Preprocesador BERT...")
bert_preprocess_handle = "https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3"
bert_preprocessor_global = tf_hub.KerasLayer(bert_preprocess_handle)
print("Preprocesador BERT cargado.")
print("Cargando ELIXR-C...")
elixrc_model_path = os.path.join(MODEL_DOWNLOAD_DIR, 'elixr-c-v2-pooled')
elixrc_model = tf.saved_model.load(elixrc_model_path)
elixrc_infer_global = elixrc_model.signatures['serving_default']
print("Modelo ELIXR-C cargado.")
print("Cargando QFormer (ELIXR-B Text)...")
qformer_model_path = os.path.join(MODEL_DOWNLOAD_DIR, 'pax-elixr-b-text')
qformer_model = tf.saved_model.load(qformer_model_path)
qformer_infer_global = qformer_model.signatures['serving_default']
print("Modelo QFormer cargado.")
models_loaded = True
end_time = time.time()
print(f"--- Modelos cargados globalmente con éxito en {end_time - start_time:.2f} segundos ---")
except Exception as e:
models_loaded = False
print(f"--- ERROR CRÍTICO DURANTE LA CARGA GLOBAL DE MODELOS ---"); print(e); traceback.print_exc()
# --- Función Principal de Procesamiento para Gradio ---
def assess_quality_and_update_ui(image_pil):
"""Procesa la imagen y devuelve actualizaciones para la UI."""
if not models_loaded:
raise gr.Error("Error: Los modelos no se pudieron cargar. La aplicación no puede procesar imágenes.")
if image_pil is None:
return (
gr.update(visible=True), # Muestra bienvenida
gr.update(visible=False), # Oculta resultados
None, # Borra imagen de salida
gr.update(value="N/A"), # Borra etiqueta
pd.DataFrame(), # Borra dataframe
None # Borra JSON
)
print("\n--- Iniciando evaluación para nueva imagen ---")
start_process_time = time.time()
try:
img_np = np.array(image_pil.convert('L'))
image_embedding = generate_image_embedding(img_np, elixrc_infer_global, qformer_infer_global)
detailed_results = calculate_similarities_and_classify(image_embedding, bert_preprocessor_global, qformer_infer_global)
output_data, passed_count, total_count = [], 0, 0
for criterion, details in detailed_results.items():
total_count += 1
sim_pos = details['similarity_positive']
sim_neg = details['similarity_negative']
diff = details['difference']
comp = details['classification_comparative']
simp = details['classification_simplified']
output_data.append([ criterion, f"{sim_pos:.4f}" if sim_pos else "N/A",
f"{sim_neg:.4f}" if sim_neg else "N/A", f"{diff:.4f}" if diff else "N/A", comp, simp ])
if comp == "PASS": passed_count += 1
df_results = pd.DataFrame(output_data, columns=[ "Criterion", "Sim (+)", "Sim (-)", "Difference", "Assessment (Comp)", "Assessment (Simp)" ])
overall_quality = "Error"; pass_rate = 0
if total_count > 0:
pass_rate = passed_count / total_count
if pass_rate >= 0.85: overall_quality = "Excellent"
elif pass_rate >= 0.70: overall_quality = "Good"
elif pass_rate >= 0.50: overall_quality = "Fair"
else: overall_quality = "Poor"
quality_label = f"{overall_quality} ({passed_count}/{total_count} passed)"
end_process_time = time.time()
print(f"--- Evaluación completada en {end_process_time - start_process_time:.2f} seg ---")
return (
gr.update(visible=False), # Oculta bienvenida
gr.update(visible=True), # Muestra resultados
image_pil, # Muestra imagen procesada
gr.update(value=quality_label), # Actualiza etiqueta
df_results, # Actualiza dataframe
detailed_results # Actualiza JSON
)
except Exception as e:
print(f"Error durante procesamiento Gradio: {e}"); traceback.print_exc()
raise gr.Error(f"Error procesando imagen: {str(e)}")
# --- Función para Resetear la UI ---
def reset_ui():
print("Reseteando UI...")
return (
gr.update(visible=True), # Muestra bienvenida
gr.update(visible=False), # Oculta resultados
None, # Borra imagen de entrada
None, # Borra imagen de salida
gr.update(value="N/A"), # Borra etiqueta
pd.DataFrame(), # Borra dataframe
None # Borra JSON
)
# --- Definir Tema Oscuro Personalizado (CORREGIDO) ---
dark_theme = gr.themes.Default(
primary_hue=gr.themes.colors.blue,
secondary_hue=gr.themes.colors.blue,
neutral_hue=gr.themes.colors.gray,
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
font_mono=[gr.themes.GoogleFont("JetBrains Mono"), "ui-monospace", "Consolas", "monospace"],
).set(
# Fondos
body_background_fill="#111827",
background_fill_primary="#1f2937",
background_fill_secondary="#374151",
block_background_fill="#1f2937",
# Texto
body_text_color="#d1d5db",
# text_color_subdued="#9ca3af", # <-- Línea eliminada que causaba el error
block_label_text_color="#d1d5db",
block_title_text_color="#ffffff",
# Bordes
border_color_accent="#374151",
border_color_primary="#4b5563",
# Botones y Elementos Interactivos
button_primary_background_fill="*primary_600",
button_primary_text_color="#ffffff",
button_secondary_background_fill="*neutral_700",
button_secondary_text_color="#ffffff",
input_background_fill="#374151",
input_border_color="#4b5563",
input_text_color="#ffffff",
# Sombras y Radios
shadow_drop="rgba(0,0,0,0.2) 0px 2px 4px",
block_shadow="rgba(0,0,0,0.2) 0px 2px 5px",
radius_size="*radius_lg",
)
# --- Definir la Interfaz Gradio con Bloques y Tema ---
with gr.Blocks(theme=dark_theme, title="CXR Quality Assessment") as demo:
# --- Cabecera ---
with gr.Row():
gr.Markdown(
"""
# <span style="color: #e5e7eb;">CXR Quality Assessment</span>
<p style="color: #9ca3af;">Evaluate chest X-ray technical quality using AI (ELIXR family)</p>
""",
elem_id="app-header"
)
# --- Contenido Principal (Dos Columnas) ---
with gr.Row(equal_height=False):
# --- Columna Izquierda (Carga) ---
with gr.Column(scale=1, min_width=350):
gr.Markdown("### 1. Upload Image", elem_id="upload-title")
input_image = gr.Image(type="pil", label="Upload Chest X-ray", height=300)
with gr.Row():
analyze_btn = gr.Button("Analyze Image", variant="primary", scale=2)
reset_btn = gr.Button("Reset", variant="secondary", scale=1)
# gr.Examples( examples=[os.path.join("examples", "sample_cxr.png")], inputs=input_image, label="Example CXR" )
gr.Markdown( "<p style='color:#9ca3af; font-size:0.9em;'>Model loading on startup takes ~1 min. Analysis takes ~15-40 sec.</p>" )
# --- Columna Derecha (Bienvenida / Resultados) ---
with gr.Column(scale=2):
with gr.Column(visible=True, elem_id="welcome-section") as welcome_block:
gr.Markdown( """ ### Welcome! Upload a chest X-ray image (PNG, JPG, etc.) on the left panel and click "Analyze Image". The system will evaluate its technical quality based on 7 standard criteria using the ELIXR model family. The results will appear here once the analysis is complete. """, elem_id="welcome-text" )
with gr.Column(visible=False, elem_id="results-section") as results_block:
gr.Markdown("### 2. Quality Assessment Results", elem_id="results-title")
with gr.Row():
with gr.Column(scale=1): output_image = gr.Image(type="pil", label="Analyzed Image", interactive=False)
with gr.Column(scale=1):
gr.Markdown("#### Summary", elem_id="summary-title")
output_label = gr.Label(value="N/A", label="Overall Quality Estimate", elem_id="quality-label")
gr.Markdown("#### Detailed Criteria Evaluation", elem_id="detailed-title")
output_dataframe = gr.DataFrame( headers=["Criterion", "Sim (+)", "Sim (-)", "Difference", "Assessment (Comp)", "Assessment (Simp)"], label=None, wrap=True, max_rows=10, overflow_row_behaviour="show_ends", interactive=False, elem_id="results-dataframe" )
with gr.Accordion("Raw JSON Output (for debugging)", open=False): output_json = gr.JSON(label=None)
gr.Markdown( f""" #### Technical Notes * **Criterion:** Quality aspect evaluated. * **Sim (+/-):** Cosine similarity with positive/negative prompt. * **Difference:** Sim (+) - Sim (-). * **Assessment (Comp):** PASS if Difference > {SIMILARITY_DIFFERENCE_THRESHOLD}. (Main Result) * **Assessment (Simp):** PASS if Sim (+) > {POSITIVE_SIMILARITY_THRESHOLD}. """, elem_id="notes-text" )
# --- Pie de página ---
gr.Markdown( """ ---- <p style='text-align:center; color:#9ca3af; font-size:0.8em;'> CXR Quality Assessment Tool | Model: google/cxr-foundation | Interface: Gradio </p> """, elem_id="app-footer" )
# --- Conexiones de Eventos ---
analyze_btn.click(
fn=assess_quality_and_update_ui,
inputs=[input_image],
outputs=[ welcome_block, results_block, output_image, output_label, output_dataframe, output_json ]
)
reset_btn.click(
fn=reset_ui,
inputs=None,
outputs=[ welcome_block, results_block, input_image, output_image, output_label, output_dataframe, output_json ]
)
# --- Iniciar la Aplicación Gradio ---
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860) |