JeanCGuerrero commited on
Commit
de04d07
verified
1 Parent(s): 4bd3486

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -9,25 +9,31 @@ import torchvision.transforms as transforms
9
  class Autoencoder(nn.Module):
10
  def __init__(self):
11
  super(Autoencoder, self).__init__()
12
- self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=2, padding=1)
13
- self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1)
14
- self.fc1 = nn.Linear(64 * 16 * 16, 16)
15
- self.fc2 = nn.Linear(16, 64 * 16 * 16)
16
- self.conv3 = nn.ConvTranspose2d(64, 32, kernel_size=3, stride=2, padding=1, output_padding=1)
17
- self.conv4 = nn.ConvTranspose2d(32, 1, kernel_size=3, stride=2, padding=1, output_padding=1)
 
 
 
 
18
 
19
  def encode(self, x):
20
  z = torch.tanh(self.conv1(x))
21
  z = torch.tanh(self.conv2(z))
 
22
  z = z.view(z.size(0), -1)
23
  z = torch.tanh(self.fc1(z))
24
  return z
25
 
26
  def decode(self, x):
27
  z = torch.tanh(self.fc2(x))
28
- z = z.view(z.size(0), 64, 16, 16)
29
- z = torch.tanh(self.conv3(z))
30
- z = torch.sigmoid(self.conv4(z))
 
31
  return z
32
 
33
  def forward(self, x):
@@ -50,3 +56,4 @@ def detectar_anomalia(imagen):
50
  with torch.no_grad():
51
  img_tensor = transform(imagen).unsqueeze(0) # A帽adir batch
52
  reconstruida = model(img_tensor).squeeze(0).squeeze(0)
 
 
9
  class Autoencoder(nn.Module):
10
  def __init__(self):
11
  super(Autoencoder, self).__init__()
12
+ # Encoder
13
+ self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=2, padding=1) # 64x64 -> 32x32
14
+ self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1) # 32x32 -> 16x16
15
+ self.conv3 = nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1) # 16x16 -> 8x8
16
+ self.fc1 = nn.Linear(128 * 8 * 8, 32) # Espacio latente
17
+ # Decoder
18
+ self.fc2 = nn.Linear(32, 128 * 8 * 8)
19
+ self.conv4 = nn.ConvTranspose2d(128, 64, kernel_size=3, stride=2, padding=1, output_padding=1) # 8x8 -> 16x16
20
+ self.conv5 = nn.ConvTranspose2d(64, 32, kernel_size=3, stride=2, padding=1, output_padding=1) # 16x16 -> 32x32
21
+ self.conv6 = nn.ConvTranspose2d(32, 1, kernel_size=3, stride=2, padding=1, output_padding=1) # 32x32 -> 64x64
22
 
23
  def encode(self, x):
24
  z = torch.tanh(self.conv1(x))
25
  z = torch.tanh(self.conv2(z))
26
+ z = torch.tanh(self.conv3(z))
27
  z = z.view(z.size(0), -1)
28
  z = torch.tanh(self.fc1(z))
29
  return z
30
 
31
  def decode(self, x):
32
  z = torch.tanh(self.fc2(x))
33
+ z = z.view(z.size(0), 128, 8, 8)
34
+ z = torch.tanh(self.conv4(z))
35
+ z = torch.tanh(self.conv5(z))
36
+ z = torch.sigmoid(self.conv6(z))
37
  return z
38
 
39
  def forward(self, x):
 
56
  with torch.no_grad():
57
  img_tensor = transform(imagen).unsqueeze(0) # A帽adir batch
58
  reconstruida = model(img_tensor).squeeze(0).squeeze(0)
59
+ return reconstruida.numpy() # Convertir a numpy para visualizaci贸n