Spaces:
Runtime error
Runtime error
Unifited latent np-torch conversion
Browse files- app.py +17 -16
- generate_videos.py +3 -3
app.py
CHANGED
|
@@ -183,30 +183,32 @@ class ImageEditor(object):
|
|
| 183 |
|
| 184 |
def get_target_latents(self, source_latent, edit_choices, generators):
|
| 185 |
|
| 186 |
-
np_source_latent = source_latent.squeeze(0).cpu().detach().numpy()
|
| 187 |
-
|
| 188 |
target_latents = []
|
| 189 |
|
| 190 |
if edit_choices["edit_type"] == "InterFaceGAN":
|
|
|
|
|
|
|
| 191 |
for attribute_name in ["pose", "smile", "gender", "age", "hair_length"]:
|
| 192 |
strength = edit_choices[attribute_name]
|
| 193 |
if strength != 0.0:
|
| 194 |
-
|
|
|
|
| 195 |
|
| 196 |
elif edit_choices["edit_type"] == "StyleCLIP":
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
| 206 |
|
| 207 |
-
# if edit type is none or if all
|
| 208 |
if not target_latents:
|
| 209 |
-
target_latents = [
|
| 210 |
|
| 211 |
return target_latents
|
| 212 |
|
|
@@ -249,8 +251,7 @@ class ImageEditor(object):
|
|
| 249 |
latent_for_gen = style_tensor_to_style_dict(latent_for_gen, g_ema)
|
| 250 |
img, _ = g_ema(latent_for_gen, input_is_s_code=True, input_is_latent=True, truncation=1, randomize_noise=False)
|
| 251 |
else:
|
| 252 |
-
|
| 253 |
-
img, _ = g_ema(latent_for_gen, input_is_latent=True, truncation=1, randomize_noise=False)
|
| 254 |
|
| 255 |
output_path = os.path.join(out_dir, f"out_{len(output_paths)}.jpg")
|
| 256 |
utils.save_image(img, output_path, nrow=1, normalize=True, range=(-1, 1))
|
|
|
|
| 183 |
|
| 184 |
def get_target_latents(self, source_latent, edit_choices, generators):
|
| 185 |
|
|
|
|
|
|
|
| 186 |
target_latents = []
|
| 187 |
|
| 188 |
if edit_choices["edit_type"] == "InterFaceGAN":
|
| 189 |
+
np_source_latent = source_latent.squeeze(0).cpu().detach().numpy()
|
| 190 |
+
|
| 191 |
for attribute_name in ["pose", "smile", "gender", "age", "hair_length"]:
|
| 192 |
strength = edit_choices[attribute_name]
|
| 193 |
if strength != 0.0:
|
| 194 |
+
projected_code_np = project_code_by_edit_name(np_source_latent, attribute_name, strength)
|
| 195 |
+
target_latents.append(torch.from_numpy(projected_code_np).float().to(self.device))
|
| 196 |
|
| 197 |
elif edit_choices["edit_type"] == "StyleCLIP":
|
| 198 |
+
if edit_choices["alpha"] != 0.0:
|
| 199 |
+
source_s_dict = generators[0].get_s_code(source_latent, input_is_latent=True)[0]
|
| 200 |
+
target_latents.append(project_code_with_styleclip(source_s_dict,
|
| 201 |
+
edit_choices["src_text"],
|
| 202 |
+
edit_choices["tar_text"],
|
| 203 |
+
edit_choices["alpha"],
|
| 204 |
+
edit_choices["beta"],
|
| 205 |
+
generators[0],
|
| 206 |
+
self.styleclip_fs3,
|
| 207 |
+
self.clip_model))
|
| 208 |
|
| 209 |
+
# if edit type is none or if all sliders were set to 0
|
| 210 |
if not target_latents:
|
| 211 |
+
target_latents = [source_latent, ] * max((len(generators) - 1), 1)
|
| 212 |
|
| 213 |
return target_latents
|
| 214 |
|
|
|
|
| 251 |
latent_for_gen = style_tensor_to_style_dict(latent_for_gen, g_ema)
|
| 252 |
img, _ = g_ema(latent_for_gen, input_is_s_code=True, input_is_latent=True, truncation=1, randomize_noise=False)
|
| 253 |
else:
|
| 254 |
+
img, _ = g_ema([latent_for_gen], input_is_latent=True, truncation=1, randomize_noise=False)
|
|
|
|
| 255 |
|
| 256 |
output_path = os.path.join(out_dir, f"out_{len(output_paths)}.jpg")
|
| 257 |
utils.save_image(img, output_path, nrow=1, normalize=True, range=(-1, 1))
|
generate_videos.py
CHANGED
|
@@ -64,21 +64,21 @@ def generate_frames(source_latent, target_latents, g_ema_list, output_dir):
|
|
| 64 |
|
| 65 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 66 |
|
| 67 |
-
code_is_s = target_latents.size()[1] == 9088
|
| 68 |
|
| 69 |
if code_is_s:
|
| 70 |
source_s_dict = g_ema_list[0].get_s_code(source_latent, input_is_latent=True)[0]
|
| 71 |
np_latent = style_dict_to_style_tensor(source_s_dict, g_ema_list[0]).cpu().detach().numpy()
|
| 72 |
-
target_latents = target_latents.cpu().detach().numpy()
|
| 73 |
else:
|
| 74 |
np_latent = source_latent.squeeze(0).cpu().detach().numpy()
|
| 75 |
|
|
|
|
| 76 |
|
| 77 |
num_alphas = min(10, 30 // len(target_latents))
|
| 78 |
|
| 79 |
alphas = np.linspace(0, 1, num=num_alphas)
|
| 80 |
|
| 81 |
-
latents = interpolate_with_target_latents(np_latent,
|
| 82 |
|
| 83 |
segments = len(g_ema_list) - 1
|
| 84 |
|
|
|
|
| 64 |
|
| 65 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 66 |
|
| 67 |
+
code_is_s = target_latents[0].size()[1] == 9088
|
| 68 |
|
| 69 |
if code_is_s:
|
| 70 |
source_s_dict = g_ema_list[0].get_s_code(source_latent, input_is_latent=True)[0]
|
| 71 |
np_latent = style_dict_to_style_tensor(source_s_dict, g_ema_list[0]).cpu().detach().numpy()
|
|
|
|
| 72 |
else:
|
| 73 |
np_latent = source_latent.squeeze(0).cpu().detach().numpy()
|
| 74 |
|
| 75 |
+
np_target_latents = [target_latent.cpu().detach().numpy() for target_latent in target_latents]
|
| 76 |
|
| 77 |
num_alphas = min(10, 30 // len(target_latents))
|
| 78 |
|
| 79 |
alphas = np.linspace(0, 1, num=num_alphas)
|
| 80 |
|
| 81 |
+
latents = interpolate_with_target_latents(np_latent, np_target_latents, alphas)
|
| 82 |
|
| 83 |
segments = len(g_ema_list) - 1
|
| 84 |
|