Bmo411 commited on
Commit
9d2d3c1
verified
1 Parent(s): a45d2d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -13
app.py CHANGED
@@ -36,6 +36,21 @@ if physical_devices:
36
  else:
37
  print("No se detect贸 GPU. El procesamiento ser谩 m谩s lento.")
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  # Descargar modelo desde Hugging Face (con cach茅)
40
  cache_dir = os.path.join(os.path.expanduser("~"), ".cache", "huggingface")
41
  model_path = hf_hub_download(
@@ -45,11 +60,41 @@ model_path = hf_hub_download(
45
  )
46
  print(f"Modelo cargado desde: {model_path}")
47
 
48
- # Cargar el modelo una sola vez
49
- model = tf.keras.models.load_model(model_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  # Crear versi贸n optimizada para inferencia
52
- @tf.function(jit_compile=True)
53
  def predict_optimized(input_tensor):
54
  return model(input_tensor, training=False)
55
 
@@ -126,8 +171,9 @@ def Denoiser(imagen):
126
  # Medir el tiempo de la predicci贸n
127
  start_time = time.time()
128
 
129
- # Predecir con el autoencoder utilizando la funci贸n optimizada
130
- reconstructed = predict_optimized(noisy_image_input).numpy()[0]
 
131
 
132
  prediction_time = time.time() - start_time
133
 
@@ -147,14 +193,6 @@ def Denoiser(imagen):
147
  print(f"Error en el procesamiento: {e}")
148
  return None, None
149
 
150
- # Ejemplo para precalentamiento (warm-up) del modelo
151
- try:
152
- dummy_input = np.zeros((1, 256, 256, 3), dtype=np.float32)
153
- _ = predict_optimized(dummy_input)
154
- print("Modelo precalentado con 茅xito")
155
- except Exception as e:
156
- print(f"Error en precalentamiento: {e}")
157
-
158
  # Crear interfaz en Gradio
159
  demo = gr.Interface(
160
  fn=Denoiser,
 
36
  else:
37
  print("No se detect贸 GPU. El procesamiento ser谩 m谩s lento.")
38
 
39
+ # IMPORTANTE: Habilitar deserializaci贸n insegura para capas Lambda
40
+ try:
41
+ # Para TensorFlow 2.11+
42
+ if hasattr(tf.keras.config, 'enable_unsafe_deserialization'):
43
+ tf.keras.config.enable_unsafe_deserialization()
44
+ print("Deserializaci贸n insegura habilitada mediante tf.keras.config")
45
+ # Para versiones antiguas, intentar Keras directamente
46
+ elif hasattr(tf.keras.utils, 'enable_unsafe_deserialization'):
47
+ tf.keras.utils.enable_unsafe_deserialization()
48
+ print("Deserializaci贸n insegura habilitada mediante tf.keras.utils")
49
+ else:
50
+ print("No se pudo habilitar la deserializaci贸n insegura autom谩ticamente")
51
+ except Exception as e:
52
+ print(f"Error al configurar deserializaci贸n: {e}")
53
+
54
  # Descargar modelo desde Hugging Face (con cach茅)
55
  cache_dir = os.path.join(os.path.expanduser("~"), ".cache", "huggingface")
56
  model_path = hf_hub_download(
 
60
  )
61
  print(f"Modelo cargado desde: {model_path}")
62
 
63
+ # Cargar el modelo con desactivaci贸n de safety
64
+ try:
65
+ # Intentar cargar con custom_objects para las funciones personalizadas
66
+ model = tf.keras.models.load_model(
67
+ model_path,
68
+ custom_objects={
69
+ 'fourier_transform': fourier_transform,
70
+ 'inverse_fourier_transform': inverse_fourier_transform
71
+ },
72
+ compile=False # No compilar el modelo para inferencia m谩s r谩pida
73
+ )
74
+ print("Modelo cargado correctamente")
75
+ except Exception as e:
76
+ print(f"Error al cargar el modelo: {e}")
77
+ # Alternativa fallback para versiones m谩s recientes de TF
78
+ try:
79
+ options = tf.saved_model.LoadOptions(
80
+ experimental_io_device='/job:localhost'
81
+ )
82
+ model = tf.keras.models.load_model(
83
+ model_path,
84
+ custom_objects={
85
+ 'fourier_transform': fourier_transform,
86
+ 'inverse_fourier_transform': inverse_fourier_transform
87
+ },
88
+ compile=False,
89
+ options=options
90
+ )
91
+ print("Modelo cargado con opciones alternativas")
92
+ except Exception as e2:
93
+ print(f"Error al cargar el modelo con opciones alternativas: {e2}")
94
+ raise Exception("No se pudo cargar el modelo")
95
 
96
  # Crear versi贸n optimizada para inferencia
97
+ @tf.function
98
  def predict_optimized(input_tensor):
99
  return model(input_tensor, training=False)
100
 
 
171
  # Medir el tiempo de la predicci贸n
172
  start_time = time.time()
173
 
174
+ # Predecir con el autoencoder
175
+ # Usar llamada directa en lugar de predict() para m谩s velocidad
176
+ reconstructed = model(noisy_image_input, training=False).numpy()[0]
177
 
178
  prediction_time = time.time() - start_time
179
 
 
193
  print(f"Error en el procesamiento: {e}")
194
  return None, None
195
 
 
 
 
 
 
 
 
 
196
  # Crear interfaz en Gradio
197
  demo = gr.Interface(
198
  fn=Denoiser,