Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import numpy as np
|
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
# Define your Generator architecture - with ngf=128 to match your training parameters
|
| 9 |
class Generator(nn.Module):
|
|
@@ -64,32 +65,54 @@ except Exception as e:
|
|
| 64 |
model.eval()
|
| 65 |
print(f"Model initialized: {model is not None}")
|
| 66 |
|
| 67 |
-
def
|
| 68 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Set seed for reproducibility
|
| 70 |
torch.manual_seed(random_seed)
|
| 71 |
|
| 72 |
-
# Generate
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
fake_img = fake_images * 0.5 + 0.5 # unnormalize
|
| 81 |
-
fake_img = fake_img.squeeze(0).permute(1, 2, 0).numpy()
|
| 82 |
-
fake_img = np.clip(fake_img * 255, 0, 255).astype(np.uint8)
|
| 83 |
-
return Image.fromarray(fake_img)
|
| 84 |
|
| 85 |
# Create Gradio interface
|
| 86 |
demo = gr.Interface(
|
| 87 |
-
fn=
|
| 88 |
-
inputs=
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
title="DCGAN Computer Mouse Generator",
|
| 91 |
-
description="Generate unique computer mouse designs using a DCGAN model
|
| 92 |
-
examples=[[42], [23], [7
|
| 93 |
)
|
| 94 |
|
| 95 |
# Launch the app
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
import os
|
| 7 |
+
import math
|
| 8 |
|
| 9 |
# Define your Generator architecture - with ngf=128 to match your training parameters
|
| 10 |
class Generator(nn.Module):
|
|
|
|
| 65 |
model.eval()
|
| 66 |
print(f"Model initialized: {model is not None}")
|
| 67 |
|
| 68 |
+
def create_image_grid(images, rows, cols):
|
| 69 |
+
"""Create a grid of images"""
|
| 70 |
+
w, h = images[0].size
|
| 71 |
+
grid = Image.new('RGB', size=(cols*w, rows*h))
|
| 72 |
+
|
| 73 |
+
for i, image in enumerate(images):
|
| 74 |
+
grid.paste(image, box=(i%cols*w, i//cols*h))
|
| 75 |
+
return grid
|
| 76 |
+
|
| 77 |
+
def generate_multiple_images(random_seed=42, num_images=4):
|
| 78 |
+
"""Generate multiple images using the DCGAN model"""
|
| 79 |
# Set seed for reproducibility
|
| 80 |
torch.manual_seed(random_seed)
|
| 81 |
|
| 82 |
+
# Generate multiple images
|
| 83 |
+
images = []
|
| 84 |
+
for i in range(num_images):
|
| 85 |
+
# Generate random noise with different seeds
|
| 86 |
+
noise = torch.randn(1, 100, 1, 1, device=device)
|
| 87 |
+
|
| 88 |
+
# Generate fake image
|
| 89 |
+
with torch.no_grad():
|
| 90 |
+
fake_image = model(noise).detach().cpu()
|
| 91 |
+
|
| 92 |
+
# Convert tensor to image
|
| 93 |
+
fake_img = fake_image * 0.5 + 0.5 # unnormalize
|
| 94 |
+
fake_img = fake_img.squeeze(0).permute(1, 2, 0).numpy()
|
| 95 |
+
fake_img = np.clip(fake_img * 255, 0, 255).astype(np.uint8)
|
| 96 |
+
images.append(Image.fromarray(fake_img))
|
| 97 |
|
| 98 |
+
# Create a grid of images
|
| 99 |
+
rows = int(math.sqrt(num_images))
|
| 100 |
+
cols = int(math.ceil(num_images / rows))
|
| 101 |
+
grid = create_image_grid(images, rows, cols)
|
| 102 |
|
| 103 |
+
return grid
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
# Create Gradio interface
|
| 106 |
demo = gr.Interface(
|
| 107 |
+
fn=generate_multiple_images,
|
| 108 |
+
inputs=[
|
| 109 |
+
gr.Slider(minimum=1, maximum=100, step=1, default=42, label="Random Seed"),
|
| 110 |
+
gr.Slider(minimum=1, maximum=16, step=1, default=4, label="Number of Images")
|
| 111 |
+
],
|
| 112 |
+
outputs=gr.Image(type="pil", label="Generated Computer Mice"),
|
| 113 |
title="DCGAN Computer Mouse Generator",
|
| 114 |
+
description="Generate multiple unique computer mouse designs using a DCGAN model.",
|
| 115 |
+
examples=[[42, 4], [23, 9], [7, 16]]
|
| 116 |
)
|
| 117 |
|
| 118 |
# Launch the app
|