File size: 2,824 Bytes
ab7237b
 
 
 
 
 
 
 
 
bd56b3a
ab7237b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3004b1
a266672
ab7237b
 
 
612e92e
 
 
 
ab7237b
612e92e
 
 
 
ab7237b
612e92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab7237b
 
 
4999b07
961e8fe
4999b07
 
 
 
 
 
ab7237b
 
 
 
c7bda61
ab7237b
 
4999b07
 
ab7237b
 
4999b07
ab7237b
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import gradio as gr
import torch
import torch.nn as nn
import numpy as np

class Generator(nn.Module):
    def __init__(self):
        super(Generator, self).__init__()
        self.main = nn.Sequential(
            nn.ConvTranspose2d(128, 64 * 8, 4, 1, 0, bias=False),
            nn.BatchNorm2d(64 * 8),
            nn.ReLU(True),

            nn.ConvTranspose2d(64 * 8, 64 * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(64 * 4),
            nn.ReLU(True),

            nn.ConvTranspose2d(64 * 4, 64 * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(64 * 2),
            nn.ReLU(True),

            nn.ConvTranspose2d(64 * 2, 64, 4, 2, 1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(True),

            nn.ConvTranspose2d(64, 3, 4, 2, 1, bias=False),
            nn.Tanh()
        )

    def forward(self, input):
        return self.main(input)

netG = Generator()

device = "cuda" if torch.cuda.is_available() else "cpu"
model_file = "model.pth"
netG.load_state_dict(torch.load(model_file, map_location=device))
netG.eval()

def generate_image():
    try:
        noise = torch.randn(1, 128, 1, 1, device=device)
        with torch.no_grad():
            fake_image = netG(noise).cpu()

        img = fake_image.squeeze().cpu().numpy()
        img = np.transpose(img, (1, 2, 0))
        img = (img + 1) / 2.0 
        img = (img * 255).astype(np.uint8)

        return img
        
    except Exception as e:
        print(f"Error generating image: {e}")

def generate_images(seed, num_images, is_random):
    try:
        generated_images = []

        if is_random:
            seed = np.random.randint(0, 99999999)
        else:
            np.random.seed(seed)
        
        torch.manual_seed(seed)

        noise = torch.randn(num_images, 128, 1, 1).to(device)

        with torch.no_grad():
            fake_images = netG(noise)

        for img in fake_images:
            img = img.squeeze().cpu().numpy()
            img = np.transpose(img, (1, 2, 0))
            img = (img + 1) / 2.0 
            img = (img * 255).astype(np.uint8)

            generated_images.append(img)

        print("Seed:", seed)
        return generated_images
        
    except Exception as e:
        print(f"Error generating images: {e}")

title = "DCGAN Image Generator 🖌️🎨"
description = "Generate non-existing images using DCGAN."
content = """
## How to Use 🎨

To generate an image, follow these steps:

1. Click \"Generate\" button to generate a image!
2. Once the image is generated, you can save it or share it to the community!
"""
    
iface = gr.Interface(
    fn=generate_image,
    inputs=None,
    outputs=gr.Image(label="Image", type="pil"),
    title=title,
    description=description,
    article=content,
    api_name="generate"
)

iface.queue()
iface.launch()