Spaces:
Sleeping
Sleeping
FagerholmEmil
commited on
Commit
·
e4f1bae
1
Parent(s):
0bad298
No changes
Browse files- app.py +70 -0
- generator.pth +3 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Check for GPU
|
| 8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
+
|
| 10 |
+
# Define the Generator architecture
|
| 11 |
+
class Generator(nn.Module):
|
| 12 |
+
def __init__(self, latent_dim=100, img_channels=3, feature_dim=64):
|
| 13 |
+
super(Generator, self).__init__()
|
| 14 |
+
self.latent_dim = latent_dim
|
| 15 |
+
self.model = nn.Sequential(
|
| 16 |
+
nn.ConvTranspose2d(latent_dim, feature_dim * 8, 4, 1, 0, bias=False),
|
| 17 |
+
nn.BatchNorm2d(feature_dim * 8),
|
| 18 |
+
nn.ReLU(True),
|
| 19 |
+
nn.ConvTranspose2d(feature_dim * 8, feature_dim * 4, 4, 2, 1, bias=False),
|
| 20 |
+
nn.BatchNorm2d(feature_dim * 4),
|
| 21 |
+
nn.ReLU(True),
|
| 22 |
+
nn.ConvTranspose2d(feature_dim * 4, feature_dim * 2, 4, 2, 1, bias=False),
|
| 23 |
+
nn.BatchNorm2d(feature_dim * 2),
|
| 24 |
+
nn.ReLU(True),
|
| 25 |
+
nn.ConvTranspose2d(feature_dim * 2, feature_dim, 4, 2, 1, bias=False),
|
| 26 |
+
nn.BatchNorm2d(feature_dim),
|
| 27 |
+
nn.ReLU(True),
|
| 28 |
+
nn.ConvTranspose2d(feature_dim, img_channels, 4, 2, 1, bias=False),
|
| 29 |
+
nn.Tanh()
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
def forward(self, z):
|
| 33 |
+
return self.model(z)
|
| 34 |
+
|
| 35 |
+
def generate_latent_space(self, batch_size):
|
| 36 |
+
return torch.randn(batch_size, self.latent_dim, 1, 1, device=device)
|
| 37 |
+
|
| 38 |
+
# Instantiate the generator and load pre-trained weights
|
| 39 |
+
latent_dim = 100
|
| 40 |
+
generator = Generator(latent_dim=latent_dim)
|
| 41 |
+
# Make sure you have uploaded your pre-trained model file "generator.pth" to your Space
|
| 42 |
+
generator.load_state_dict(torch.load("generator.pth", map_location=device))
|
| 43 |
+
generator.to(device)
|
| 44 |
+
generator.eval()
|
| 45 |
+
|
| 46 |
+
# Function to generate a face image
|
| 47 |
+
def generate_face():
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
# Generate a random latent vector and produce an image
|
| 50 |
+
z = generator.generate_latent_space(1)
|
| 51 |
+
generated_image = generator(z)
|
| 52 |
+
generated_image = generated_image.cpu().squeeze(0)
|
| 53 |
+
# Denormalize the image (from [-1, 1] to [0, 1])
|
| 54 |
+
generated_image = generated_image * 0.5 + 0.5
|
| 55 |
+
# Convert the tensor to a PIL Image
|
| 56 |
+
to_pil = transforms.ToPILImage()
|
| 57 |
+
image = to_pil(generated_image)
|
| 58 |
+
return image
|
| 59 |
+
|
| 60 |
+
# Set up the Gradio interface
|
| 61 |
+
demo = gr.Interface(
|
| 62 |
+
fn=generate_face,
|
| 63 |
+
inputs=[], # No inputs – each button press generates a new image
|
| 64 |
+
outputs="image",
|
| 65 |
+
title="CelebA GAN Face Generator",
|
| 66 |
+
description="Generates a face image using a pre-trained GAN on the CelebA dataset.",
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch()
|
generator.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b3476e418d46809c315cca43789616e1a45ddef3351916fc5fa3f15cea259ead
|
| 3 |
+
size 14322821
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
gradio
|