Bmo411 commited on
Commit
19e9e99
verified
1 Parent(s): 799b0d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -78
app.py CHANGED
@@ -6,92 +6,36 @@ from huggingface_hub import hf_hub_download
6
  import time
7
  import os
8
 
9
- # Configuraci贸n de GPU para TensorFlow
10
- physical_devices = tf.config.list_physical_devices('GPU')
11
- if physical_devices:
12
- print("GPU disponible. Configurando...")
13
- try:
14
- for gpu in physical_devices:
15
- tf.config.experimental.set_memory_growth(gpu, True)
16
- print("Configuraci贸n de GPU completada")
17
- except Exception as e:
18
- print(f"Error en configuraci贸n de GPU: {e}")
19
- else:
20
- print("No se detect贸 GPU. El procesamiento ser谩 m谩s lento.")
21
 
22
- # Definir funciones personalizadas para las transformadas de Fourier
23
  def fourier_transform(x):
24
  fourier = tf.signal.fft2d(tf.cast(x, tf.complex64))
25
  fourier = tf.complex(tf.math.real(fourier), tf.math.imag(fourier))
26
  fourier = tf.abs(fourier)
27
  return tf.concat([tf.math.real(fourier), tf.math.imag(fourier)], axis=-1)
28
 
 
29
  def inverse_fourier_transform(x):
30
  real_part, imag_part = tf.split(x, num_or_size_splits=2, axis=-1)
31
  complex_fourier = tf.complex(real_part, imag_part)
32
  return tf.abs(tf.signal.ifft2d(complex_fourier))
33
-
34
- # Construir modelo manualmente
35
- def build_autoencoder(input_shape=(256, 256, 3)):
36
- """Reconstruir el modelo autoencoder manualmente"""
37
-
38
- # Definir entradas
39
- inputs = tf.keras.layers.Input(shape=input_shape)
40
-
41
- # Aplicar transformada de Fourier (opcional)
42
- # x = tf.keras.layers.Lambda(fourier_transform)(inputs)
43
- x = inputs # Skip Fourier transform for now
44
-
45
- # Encoder
46
- x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
47
- x = tf.keras.layers.MaxPooling2D((2, 2), padding='same')(x)
48
- x = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
49
- x = tf.keras.layers.MaxPooling2D((2, 2), padding='same')(x)
50
- x = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
51
- encoded = tf.keras.layers.MaxPooling2D((2, 2), padding='same')(x)
52
-
53
- # Decoder
54
- x = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same')(encoded)
55
- x = tf.keras.layers.UpSampling2D((2, 2))(x)
56
- x = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
57
- x = tf.keras.layers.UpSampling2D((2, 2))(x)
58
- x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
59
- x = tf.keras.layers.UpSampling2D((2, 2))(x)
60
-
61
- # Output
62
- # x = tf.keras.layers.Lambda(inverse_fourier_transform)(x) # Skip inverse transform
63
- outputs = tf.keras.layers.Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
64
-
65
- # Crear modelo
66
- model = tf.keras.models.Model(inputs, outputs)
67
 
68
- return model
69
-
70
- # Opci贸n alternativa: Crear un modelo simplificado
71
- def build_simple_autoencoder(input_shape=(256, 256, 3)):
72
- """Crear un autoencoder simple (sin Fourier)"""
73
-
74
- inputs = tf.keras.layers.Input(shape=input_shape)
75
-
76
- # Encoder
77
- x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
78
- x = tf.keras.layers.MaxPooling2D((2, 2), padding='same')(x)
79
- x = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
80
- x = tf.keras.layers.MaxPooling2D((2, 2), padding='same')(x)
81
-
82
- # Decoder
83
- x = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
84
- x = tf.keras.layers.UpSampling2D((2, 2))(x)
85
- x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
86
- x = tf.keras.layers.UpSampling2D((2, 2))(x)
87
-
88
- # Output
89
- outputs = tf.keras.layers.Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
90
-
91
- # Crear modelo
92
- model = tf.keras.models.Model(inputs, outputs)
93
-
94
- return model
95
 
96
  # Funciones de preprocesamiento optimizadas
97
  def degrade_image(image, downscale_factor=4):
@@ -138,10 +82,6 @@ def preprocess_image(image, std_dev=0.1, downscale_factor=4, target_size=(256, 2
138
 
139
  return noisy_img
140
 
141
- print("Crear modelo simplificado...")
142
- model = build_simple_autoencoder()
143
- print("Modelo creado correctamente")
144
-
145
  # Funci贸n de denoising
146
  def Denoiser(imagen):
147
  """Aplica el modelo autoencoder para eliminar ruido de la imagen."""
 
6
  import time
7
  import os
8
 
9
+ model_path = hf_hub_download(repo_id="Bmo411/DenoisingAutoencoder", filename="autoencoder_complete_model_Fourier.keras")
10
+ model = tf.keras.models.load_model(model_path)
11
+
12
+ from tensorflow.keras.saving import register_keras_serializable
 
 
 
 
 
 
 
 
13
 
14
+ @register_keras_serializable()
15
  def fourier_transform(x):
16
  fourier = tf.signal.fft2d(tf.cast(x, tf.complex64))
17
  fourier = tf.complex(tf.math.real(fourier), tf.math.imag(fourier))
18
  fourier = tf.abs(fourier)
19
  return tf.concat([tf.math.real(fourier), tf.math.imag(fourier)], axis=-1)
20
 
21
+ @register_keras_serializable()
22
  def inverse_fourier_transform(x):
23
  real_part, imag_part = tf.split(x, num_or_size_splits=2, axis=-1)
24
  complex_fourier = tf.complex(real_part, imag_part)
25
  return tf.abs(tf.signal.ifft2d(complex_fourier))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ # Configuraci贸n de GPU para TensorFlow
28
+ physical_devices = tf.config.list_physical_devices('GPU')
29
+ if physical_devices:
30
+ print("GPU disponible. Configurando...")
31
+ try:
32
+ for gpu in physical_devices:
33
+ tf.config.experimental.set_memory_growth(gpu, True)
34
+ print("Configuraci贸n de GPU completada")
35
+ except Exception as e:
36
+ print(f"Error en configuraci贸n de GPU: {e}")
37
+ else:
38
+ print("No se detect贸 GPU. El procesamiento ser谩 m谩s lento.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  # Funciones de preprocesamiento optimizadas
41
  def degrade_image(image, downscale_factor=4):
 
82
 
83
  return noisy_img
84
 
 
 
 
 
85
  # Funci贸n de denoising
86
  def Denoiser(imagen):
87
  """Aplica el modelo autoencoder para eliminar ruido de la imagen."""