Views
No views yet
huggan/pokemon dataset using Optuna for hyperparameter optimization.| Component | Details |
|---|---|
| Discriminator | A convolutional neural network that processes 64x64x3 RGB images down to a 1x1 scalar. It applies Spectral Normalization to every Conv2d layer to enforce Lipschitz continuity and stabilize training, paired with LeakyReLU activations. |
| Generator | Takes a latent noise vector (1x1) and projects it using a ConvTranspose2d layer. To prevent checkerboard artifacts, the upsampling path uses Upsampling followed by standard Conv2d layers, BatchNorm2d, and ReLU. The final output uses a Tanh activation to scale pixels to [-1, 1]. |
ReLU(1.0 - D(real)) + ReLU(1.0 + D(fake))-D(fake)1import torch
2from torch import nn
3import matplotlib.pyplot as plt
4from huggingface_hub import hf_hub_download
5
6class PokemonGenerator(nn.Module):
7 def __init__(self, noise_dim=100, features_g=64, channels=3):
8 super().__init__()
9
10 self.initial_block = nn.Sequential(
11 nn.ConvTranspose2d(noise_dim, features_g * 8, kernel_size=4, stride=1, padding=0),
12 nn.BatchNorm2d(features_g * 8),
13 nn.ReLU(True)
14 )
15
16 self.upsample_blocks = nn.Sequential(
17 # 4x4 -> 8x8
18 nn.Upsample(scale_factor=2, mode='nearest'),
19 nn.Conv2d(features_g * 8, features_g * 4, kernel_size=3, stride=1, padding=1),
20 nn.BatchNorm2d(features_g * 4),
21 nn.ReLU(True),
22
23 # 8x8 -> 16x16
24 nn.Upsample(scale_factor=2, mode='nearest'),
25 nn.Conv2d(features_g * 4, features_g * 2, kernel_size=3, stride=1, padding=1),
26 nn.BatchNorm2d(features_g * 2),
27 nn.ReLU(True),
28
29 # 16x16 -> 32x32
30 nn.Upsample(scale_factor=2, mode='nearest'),
31 nn.Conv2d(features_g * 2, features_g, kernel_size=3, stride=1, padding=1),
32 nn.BatchNorm2d(features_g),
33 nn.ReLU(True),
34
35 # 32x32 -> 64x64 RGB
36 nn.Upsample(scale_factor=2, mode='nearest'),
37 nn.Conv2d(features_g, channels, kernel_size=3, stride=1, padding=1),
38 nn.Tanh()
39 )
40
41 def forward(self, x):
42 out = self.initial_block(x)
43 return self.upsample_blocks(out)
44
45model = PokemonGenerator(noise_dim=128)
46
47weights_path = hf_hub_download(repo_id="VioletaR/pokemon-gan", filename="pokemon_generator.pth")
48model.load_state_dict(torch.load(weights_path))
49model.eval()
50
51# Note: The generator expects a 4D tensor (Batch, Channels, Height, Width)
52eval_noise = torch.randn(1, noise_dim, 1, 1)
53
54with torch.no_grad():
55 generated_img = model(eval_noise)
56
57generated_img = (generated_img + 1) / 2.0 # Scale from [-1, 1] to [0, 1]
58img_numpy = generated_img.squeeze().permute(1, 2, 0).cpu().numpy()
59
60plt.imshow(img_numpy)
61plt.title("Generated Pokemon")
62plt.axis('off')
63plt.show()