Spaces:
Sleeping
Sleeping
Update app.py
Browse filesresize operation
app.py
CHANGED
|
@@ -8,6 +8,14 @@ from PIL import Image
|
|
| 8 |
|
| 9 |
# --- 1. CONFIGURACIÓN ---
|
| 10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Transformación inversa (Desnormalizar para mostrar la imagen final)
|
| 13 |
unloader = transforms.Compose([
|
|
@@ -61,28 +69,10 @@ def run_style_transfer(content_img, style_img, content_weight, style_weight, tv_
|
|
| 61 |
if content_img is None or style_img is None:
|
| 62 |
return None
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
# Transformación del contenido: SIN redimensionar, mantiene su tamaño original
|
| 68 |
-
content_transform = transforms.Compose([
|
| 69 |
-
transforms.ToTensor(),
|
| 70 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 71 |
-
])
|
| 72 |
-
|
| 73 |
-
# Transformación del estilo: lo redimensionamos para que coincida con el contenido
|
| 74 |
-
# Nota: transforms.Resize espera (Alto, Ancho)
|
| 75 |
-
style_transform = transforms.Compose([
|
| 76 |
-
transforms.Resize((original_height, original_width)),
|
| 77 |
-
transforms.ToTensor(),
|
| 78 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 79 |
-
])
|
| 80 |
-
|
| 81 |
-
# Aplicamos las transformaciones
|
| 82 |
-
content_tensor = content_transform(content_img).unsqueeze(0).to(device, torch.float)
|
| 83 |
-
style_tensor = style_transform(style_img).unsqueeze(0).to(device, torch.float)
|
| 84 |
|
| 85 |
-
# El resto del código se mantiene igual...
|
| 86 |
gen_img = content_tensor.clone().requires_grad_(True)
|
| 87 |
extractor = VGGFeatureExtractor().to(device)
|
| 88 |
|
|
@@ -115,18 +105,29 @@ def run_style_transfer(content_img, style_img, content_weight, style_weight, tv_
|
|
| 115 |
|
| 116 |
gen_img.data.clamp_(-2.1, 2.6)
|
| 117 |
|
| 118 |
-
# Convertimos de vuelta a imagen PIL (Saldrá sin ejes y en su tamaño original)
|
| 119 |
final_image = unloader(gen_img.cpu().squeeze(0))
|
| 120 |
return final_image
|
| 121 |
|
| 122 |
# --- 5. INTERFAZ DE USUARIO (GRADIO) ---
|
| 123 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
with gr.Row():
|
| 128 |
with gr.Column():
|
| 129 |
-
content_in = gr.Image(type="pil", label="Imagen Base (A)
|
| 130 |
style_in = gr.Image(type="pil", label="Imagen de Estilo (B)")
|
| 131 |
with gr.Column():
|
| 132 |
output_image = gr.Image(type="pil", label="Imagen Resultante (C)")
|
|
@@ -137,7 +138,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 137 |
c_weight = gr.Slider(minimum=0.1, maximum=10.0, value=1.0, step=0.1, label="Peso del Contenido (Estructura)")
|
| 138 |
s_weight = gr.Slider(minimum=1000, maximum=1000000, value=100000, step=1000, label="Peso del Estilo (Arte)")
|
| 139 |
tv_weight = gr.Slider(minimum=0, maximum=0.001, value=0.000001, step=0.000001, label="Suavizado (Variación Total)")
|
| 140 |
-
iters = gr.Slider(minimum=5, maximum=30, value=10, step=1, label="Iteraciones
|
| 141 |
|
| 142 |
run_btn = gr.Button("¡Mezclar Imágenes!", variant="primary")
|
| 143 |
|
|
|
|
| 8 |
|
| 9 |
# --- 1. CONFIGURACIÓN ---
|
| 10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
imsize = 384 # Balance perfecto entre velocidad (CPU) y calidad
|
| 12 |
+
|
| 13 |
+
# Transformación de entrada (Ahora redimensiona todo a imsize x imsize)
|
| 14 |
+
loader = transforms.Compose([
|
| 15 |
+
transforms.Resize((imsize, imsize)),
|
| 16 |
+
transforms.ToTensor(),
|
| 17 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 18 |
+
])
|
| 19 |
|
| 20 |
# Transformación inversa (Desnormalizar para mostrar la imagen final)
|
| 21 |
unloader = transforms.Compose([
|
|
|
|
| 69 |
if content_img is None or style_img is None:
|
| 70 |
return None
|
| 71 |
|
| 72 |
+
# Aplicamos las transformaciones (incluyendo el resize a 384x384)
|
| 73 |
+
content_tensor = loader(content_img).unsqueeze(0).to(device, torch.float)
|
| 74 |
+
style_tensor = loader(style_img).unsqueeze(0).to(device, torch.float)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
|
|
|
| 76 |
gen_img = content_tensor.clone().requires_grad_(True)
|
| 77 |
extractor = VGGFeatureExtractor().to(device)
|
| 78 |
|
|
|
|
| 105 |
|
| 106 |
gen_img.data.clamp_(-2.1, 2.6)
|
| 107 |
|
|
|
|
| 108 |
final_image = unloader(gen_img.cpu().squeeze(0))
|
| 109 |
return final_image
|
| 110 |
|
| 111 |
# --- 5. INTERFAZ DE USUARIO (GRADIO) ---
|
| 112 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 113 |
+
|
| 114 |
+
# ENCABEZADO Y ENLACES
|
| 115 |
+
gr.Markdown(
|
| 116 |
+
"""
|
| 117 |
+
<div style="text-align: center;">
|
| 118 |
+
<h1>🎨 Transferencia de Estilo Neuronal</h1>
|
| 119 |
+
<p>Sube una imagen base y una imagen de estilo para combinarlas. <i>Nota: Las imágenes se redimensionan automáticamente para procesamiento rápido.</i></p>
|
| 120 |
+
<p>
|
| 121 |
+
<a href="https://github.com/MGranados64" target="_blank" style="text-decoration: none;">🐙 <b>Mi GitHub</b></a> |
|
| 122 |
+
<a href="https://huggingface.co/MGC1991MF" target="_blank" style="text-decoration: none;">🤗 <b>Mi perfil en Hugging Face</b></a>
|
| 123 |
+
</p>
|
| 124 |
+
</div>
|
| 125 |
+
"""
|
| 126 |
+
)
|
| 127 |
|
| 128 |
with gr.Row():
|
| 129 |
with gr.Column():
|
| 130 |
+
content_in = gr.Image(type="pil", label="Imagen Base (A)")
|
| 131 |
style_in = gr.Image(type="pil", label="Imagen de Estilo (B)")
|
| 132 |
with gr.Column():
|
| 133 |
output_image = gr.Image(type="pil", label="Imagen Resultante (C)")
|
|
|
|
| 138 |
c_weight = gr.Slider(minimum=0.1, maximum=10.0, value=1.0, step=0.1, label="Peso del Contenido (Estructura)")
|
| 139 |
s_weight = gr.Slider(minimum=1000, maximum=1000000, value=100000, step=1000, label="Peso del Estilo (Arte)")
|
| 140 |
tv_weight = gr.Slider(minimum=0, maximum=0.001, value=0.000001, step=0.000001, label="Suavizado (Variación Total)")
|
| 141 |
+
iters = gr.Slider(minimum=5, maximum=30, value=10, step=1, label="Iteraciones")
|
| 142 |
|
| 143 |
run_btn = gr.Button("¡Mezclar Imágenes!", variant="primary")
|
| 144 |
|