File size: 2,489 Bytes
4d5be2f
 
 
 
 
 
 
 
 
 
 
de04d07
 
 
 
 
 
 
 
 
 
4d5be2f
 
 
 
de04d07
4d5be2f
 
 
 
 
 
de04d07
 
 
 
4d5be2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152bd9c
de04d07
f2c9943
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
import torch.nn as nn
import numpy as np
from PIL import Image
import torchvision.transforms as transforms

# Modelo autoencoder
class Autoencoder(nn.Module):
    def __init__(self):
        super(Autoencoder, self).__init__()
        # Encoder
        self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=2, padding=1)  # 64x64 -> 32x32
        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1)  # 32x32 -> 16x16
        self.conv3 = nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1)  # 16x16 -> 8x8
        self.fc1 = nn.Linear(128 * 8 * 8, 32)  # Espacio latente
        # Decoder
        self.fc2 = nn.Linear(32, 128 * 8 * 8)
        self.conv4 = nn.ConvTranspose2d(128, 64, kernel_size=3, stride=2, padding=1, output_padding=1)  # 8x8 -> 16x16
        self.conv5 = nn.ConvTranspose2d(64, 32, kernel_size=3, stride=2, padding=1, output_padding=1)  # 16x16 -> 32x32
        self.conv6 = nn.ConvTranspose2d(32, 1, kernel_size=3, stride=2, padding=1, output_padding=1)  # 32x32 -> 64x64

    def encode(self, x):
        z = torch.tanh(self.conv1(x))
        z = torch.tanh(self.conv2(z))
        z = torch.tanh(self.conv3(z))
        z = z.view(z.size(0), -1)
        z = torch.tanh(self.fc1(z))
        return z

    def decode(self, x):
        z = torch.tanh(self.fc2(x))
        z = z.view(z.size(0), 128, 8, 8)
        z = torch.tanh(self.conv4(z))
        z = torch.tanh(self.conv5(z))
        z = torch.sigmoid(self.conv6(z))
        return z

    def forward(self, x):
        return self.decode(self.encode(x))

# Cargar el modelo
model = Autoencoder()
model.load_state_dict(torch.load("autoencoder.pth", map_location=torch.device("cpu")))
model.eval()

# Transformaci贸n de entrada
transform = transforms.Compose([
    transforms.Grayscale(),
    transforms.Resize((64, 64)),
    transforms.ToTensor()
])

# Funci贸n de inferencia
def detectar_anomalia(imagen):
    with torch.no_grad():
        img_tensor = transform(imagen).unsqueeze(0)  # A帽adir batch
        reconstruida = model(img_tensor).squeeze(0).squeeze(0)
        return reconstruida.numpy()  # Convertir a numpy para visualizaci贸n


# Interfaz de Gradio
interface = gr.Interface(
    fn=detectar_anomalia,
    inputs=gr.Image(type="pil"),
    outputs=[gr.Image(type="numpy"), gr.Text()],
    title="Detecci贸n de Anomal铆as con Autoencoder",
    description="Sube una imagen para detectar anomal铆as usando un autoencoder entrenado."
)

interface.launch()