Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from matplotlib import pyplot as plt
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
class Generator(nn.Module):
|
7 |
+
def __init__(self):
|
8 |
+
super(Generator, self).__init__()
|
9 |
+
self.model = nn.Sequential(
|
10 |
+
nn.Linear(z_dim, 256),
|
11 |
+
nn.LeakyReLU(0.2),
|
12 |
+
nn.Linear(256, 512),
|
13 |
+
nn.LeakyReLU(0.2),
|
14 |
+
nn.Linear(512, 1024),
|
15 |
+
nn.LeakyReLU(0.2),
|
16 |
+
nn.Linear(1024, 2304)
|
17 |
+
)
|
18 |
+
|
19 |
+
def forward(self, x):
|
20 |
+
return self.model(x)
|
21 |
+
device = "cpu"
|
22 |
+
generator = Generator()
|
23 |
+
generator.load_state_dict(torch.load('generator_model.pt'))
|
24 |
+
num_faces_to_generate = 10
|
25 |
+
z_dim = 13
|
26 |
+
ethnicity_map = {'White': 0, 'Black': 1, 'Asian': 2, 'Indian': 3, 'Other': 4}
|
27 |
+
gender_map = {'Male': 0, 'Female': 1}
|
28 |
+
|
29 |
+
def generate_faces(age, ethnicity, gender):
|
30 |
+
|
31 |
+
random_z = torch.randn(num_faces_to_generate, z_dim).to(device)
|
32 |
+
|
33 |
+
|
34 |
+
random_z[:, 0] = age
|
35 |
+
random_z[:, 1] = ethnicity_map[ethnicity]
|
36 |
+
random_z[:, 2] = gender_map[gender]
|
37 |
+
|
38 |
+
|
39 |
+
random_z[:, 3:] = torch.randn(num_faces_to_generate, z_dim - 3).to(device)
|
40 |
+
with torch.no_grad():
|
41 |
+
generated_faces = generator(random_z)
|
42 |
+
|
43 |
+
|
44 |
+
generated_faces_np = generated_faces.cpu().detach().numpy()
|
45 |
+
|
46 |
+
generated_faces_np = generated_faces_np.reshape(-1, 48, 48)
|
47 |
+
|
48 |
+
img = plot_images(generated_faces_np)
|
49 |
+
return img
|
50 |
+
|
51 |
+
def plot_images(images):
|
52 |
+
num_cols = 5
|
53 |
+
num_rows = (len(images) - 1) // num_cols + 1
|
54 |
+
fig, axs = plt.subplots(num_rows, num_cols, figsize=(num_cols * 2, num_rows * 2))
|
55 |
+
for i, image in enumerate(images):
|
56 |
+
axs[i // num_cols][i % num_cols].imshow(image, cmap='gray')
|
57 |
+
axs[i // num_cols][i % num_cols].axis('off')
|
58 |
+
plt.tight_layout()
|
59 |
+
buf = io.BytesIO()
|
60 |
+
fig.savefig(buf)
|
61 |
+
buf.seek(0)
|
62 |
+
img = Image.open(buf)
|
63 |
+
|
64 |
+
return img
|
65 |
+
|
66 |
+
title = "FaceGAN by Q-bert"
|
67 |
+
description = f"""
|
68 |
+
## FaceGAN - Human Face Generation with GANs
|
69 |
+
|
70 |
+
![FaceGAN](https://example.com/facegan_image.jpg)
|
71 |
+
|
72 |
+
### Description
|
73 |
+
|
74 |
+
FaceGAN is a powerful Generative Adversarial Network (GAN) model designed to generate realistic human faces with varying attributes, including age, ethnicity, and gender. This model has been extensively trained on a diverse dataset of real human faces, enabling it to produce high-quality synthetic images.
|
75 |
+
|
76 |
+
The purpose of this project is to create an interactive web interface for FaceGAN. This interface allows users to explore the capabilities of the model by generating custom human faces with specific attributes. Users can adjust various parameters to influence the output, such as age range, ethnicity, gender, image resolution, noise levels, and latent space values.
|
77 |
+
|
78 |
+
### How It Works
|
79 |
+
|
80 |
+
FaceGAN consists of two main components: a **Generator** and a **Discriminator**. The Generator generates synthetic face images based on random noise and user-defined attributes. The Discriminator evaluates these generated images and real human face images to distinguish between real and fake. The two components are trained in an adversarial manner, where the Generator tries to improve its ability to deceive the Discriminator, and the Discriminator tries to improve its ability to distinguish real from fake.
|
81 |
+
|
82 |
+
### Features
|
83 |
+
|
84 |
+
- Generate realistic human faces with different attributes (age, ethnicity, gender).
|
85 |
+
- Adjust age range to control the apparent age of the generated faces.
|
86 |
+
- Choose ethnicity to influence the racial appearance of the generated faces.
|
87 |
+
- Select gender to determine the gender representation of the generated faces.
|
88 |
+
- Fine-tune image resolution and noise levels for more precise results.
|
89 |
+
- Explore the latent space by adjusting latent space values for unique face variations.
|
90 |
+
|
91 |
+
### Installation
|
92 |
+
|
93 |
+
1. Clone this repository to your local machine.
|
94 |
+
2. Install the required dependencies listed in `requirements.txt`.
|
95 |
+
|
96 |
+
### Usage
|
97 |
+
|
98 |
+
1. Run the application using `python app.py`.
|
99 |
+
2. Access the web interface through your browser at `http://localhost:5000`.
|
100 |
+
3. Customize the face attributes using the provided controls.
|
101 |
+
4. Observe the generated faces based on your selected attributes.
|
102 |
+
|
103 |
+
### Contributing
|
104 |
+
|
105 |
+
Contributions to this project are welcome! If you find any issues or want to add new features, feel free to open an issue or submit a pull request.
|
106 |
+
|
107 |
+
### License
|
108 |
+
|
109 |
+
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).
|
110 |
+
|
111 |
+
### Credits
|
112 |
+
|
113 |
+
The FaceGAN model used in this project is based on the work by [Talha Rüzgar Akkuş](https://www.linkedin.com/in/talha-r%C3%BCzgar-akku%C5%9F-1b5457264/).
|
114 |
+
|
115 |
+
### Disclaimer
|
116 |
+
|
117 |
+
The generated faces in this application are entirely synthetic and do not represent real individuals. The application is for educational and entertainment purposes only. The creators of this application are not responsible for any misuse or misrepresentation of the generated content.
|
118 |
+
"""
|
119 |
+
iface = gr.Interface(fn=generate_faces,
|
120 |
+
inputs=[gr.inputs.Slider(minimum=0, maximum=100, label='Age'),
|
121 |
+
gr.inputs.Dropdown(choices=['White', 'Black', 'Asian', 'Indian', 'Other'], label='Ethnicity'),
|
122 |
+
gr.inputs.Radio(choices=['Male', 'Female'], label='Gender')],
|
123 |
+
description=description,
|
124 |
+
title=title,
|
125 |
+
outputs=gr.outputs.Image(type='pil'))
|
126 |
+
iface.launch()
|