Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
| 4 |
-
import
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load pretrained StyleGAN2 model
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Load gender direction vector
|
| 12 |
-
|
| 13 |
-
gender_direction =
|
| 14 |
-
|
| 15 |
-
def edit_gender(seed, strength):
|
| 16 |
-
rnd = np.random.RandomState(seed)
|
| 17 |
-
z = torch.from_numpy(rnd.randn(1, G.z_dim)).to(torch.float32).to(torch.device("cpu"))
|
| 18 |
-
w = G.mapping(z, None, truncation_psi=0.5)
|
| 19 |
-
w = w + gender_direction * strength
|
| 20 |
-
img = G.synthesis(w, noise_mode="const")
|
| 21 |
-
img = (img.clamp(-1, 1) + 1) * 127.5
|
| 22 |
-
img = img.permute(0, 2, 3, 1)[0].cpu().numpy().astype(np.uint8)
|
| 23 |
return img
|
| 24 |
|
|
|
|
|
|
|
| 25 |
gr.Interface(
|
| 26 |
fn=edit_gender,
|
| 27 |
inputs=[
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
],
|
| 31 |
-
outputs=gr.Image(label
|
|
|
|
|
|
|
|
|
|
| 32 |
title="StyleGAN2 Gender Editor",
|
| 33 |
-
description="
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
| 4 |
+
import pickle #0.7) # Using a truncation_psi < 1 often gives better faces
|
| 5 |
+
|
| 6 |
+
# Apply the gender transformation
|
| 7 |
+
w = w + gender_direction * strength
|
| 8 |
+
|
| 9 |
+
# Synthesize the image from the modified w <--- CHANGE 1: Import pickle instead of legacy
|
| 10 |
|
| 11 |
# Load pretrained StyleGAN2 model
|
| 12 |
+
print("Loading StyleGAN2 model...")
|
| 13 |
+
device = torch.device("cpu")
|
| 14 |
+
with open("stylegan2-ffhq-config-
|
| 15 |
+
img = G.synthesis(w, noise_mode="const")
|
| 16 |
+
|
| 17 |
+
# Post-process the imagef.pkl", "rb") as f:
|
| 18 |
+
# CHANGE 2: Use pickle.load() instead of legacy. for display
|
| 19 |
+
img = (img.clamp(-1, 1) + 1) / 2.load_network_pkl()
|
| 20 |
+
G = pickle.load(f)["G_ema"].to(device)0
|
| 21 |
+
img = (img * 255).to(torch.uint8)
|
| 22 |
+
img = img.perm
|
| 23 |
+
print("Model loaded successfully.")
|
| 24 |
|
| 25 |
# Load gender direction vector
|
| 26 |
+
print("Loading gender direction vector...")
|
| 27 |
+
gender_direction = npute(0, 2, 3, 1)[0].cpu().numpy()
|
| 28 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return img
|
| 30 |
|
| 31 |
+
#.load("stylegan2directions/gender.npy")
|
| 32 |
+
gender_direction = torch.from_numpy(gender Create and launch the Gradio interface
|
| 33 |
gr.Interface(
|
| 34 |
fn=edit_gender,
|
| 35 |
inputs=[
|
| 36 |
+
_direction).to(torch.float32).to(device)
|
| 37 |
+
print("Vector loaded successfully.")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def edit_gendergr.Slider(0, 10000, step=1, value=1234, label="Random Seed"),
|
| 41 |
+
gr.Slider(-5, 5, step=0.1, value=0, label(seed, strength):
|
| 42 |
+
# Ensure seed is an integer for the random state
|
| 43 |
+
seed = int(seed)
|
| 44 |
+
="Gender Strength (β Feminine | Masculine β)")
|
| 45 |
],
|
| 46 |
+
outputs=gr.Image(label
|
| 47 |
+
# Generate latent code from the seed
|
| 48 |
+
rnd = np.random.RandomState(seed)
|
| 49 |
+
z = torch.from_numpy(rnd.randn(1, G.z_dim)).to(torch="Generated Face"),
|
| 50 |
title="StyleGAN2 Gender Editor",
|
| 51 |
+
description="Move the slider to change the gender expression.float32).to(device)
|
| 52 |
+
|
| 53 |
+
# Map the latent code to the intermediate space (W)
|
| 54 |
+
w = G.mapping(z, None, truncation_psi=0.7)
|
| 55 |
+
|
| 56 |
+
# Apply the gender direction transformation
|
| 57 |
+
w = w + gender_direction * strength
|
| 58 |
+
|
| 59 |
+
# Synt of the generated face. Change the seed to get a new face.",
|
| 60 |
+
allow_flagging="never"
|
| 61 |
+
).launch()
|