Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,35 +13,47 @@ from PIL import Image
|
|
| 13 |
import warnings
|
| 14 |
warnings.filterwarnings('ignore')
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
network_url = hf_hub_url(repo_id="opetrova/face-frontalization", filename="network.py")
|
| 20 |
r = requests.get(network_url, allow_redirects=True)
|
| 21 |
open('network.py', 'wb').write(r.content)
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
def frontalize(image):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
input_tensor = torch.unsqueeze(preprocess(image), 0)
|
| 33 |
-
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
generated_image = saved_model(Variable(input_tensor.type('torch.FloatTensor')))
|
| 37 |
generated_image = generated_image.detach().squeeze().permute(1, 2, 0).numpy()
|
| 38 |
-
generated_image = (generated_image + 1.0) / 2.0
|
| 39 |
-
|
| 40 |
return generated_image
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
-
|
|
|
|
|
|
| 13 |
import warnings
|
| 14 |
warnings.filterwarnings('ignore')
|
| 15 |
|
| 16 |
+
# モデルのダウンロード
|
| 17 |
+
path_to_model = hf_hub_download(
|
| 18 |
+
repo_id="opetrova/face-frontalization",
|
| 19 |
+
filename="generator_v0.pt"
|
| 20 |
+
)
|
| 21 |
|
| 22 |
+
# network.py をカレントディレクトリにダウンロード
|
| 23 |
network_url = hf_hub_url(repo_id="opetrova/face-frontalization", filename="network.py")
|
| 24 |
r = requests.get(network_url, allow_redirects=True)
|
| 25 |
open('network.py', 'wb').write(r.content)
|
| 26 |
|
| 27 |
+
# PyTorch 2.6 以降は weights_only=False を指定しないとエラーになる
|
| 28 |
+
saved_model = torch.load(path_to_model, map_location=torch.device("cpu"), weights_only=False)
|
| 29 |
|
| 30 |
def frontalize(image):
|
| 31 |
+
# 画像を [1, 3, 128, 128] tensor に変換
|
| 32 |
+
preprocess = transforms.Compose((
|
| 33 |
+
transforms.ToPILImage(),
|
| 34 |
+
transforms.Resize(size=(128, 128)),
|
| 35 |
+
transforms.ToTensor(),
|
| 36 |
+
))
|
| 37 |
input_tensor = torch.unsqueeze(preprocess(image), 0)
|
| 38 |
+
|
| 39 |
+
# 推論
|
| 40 |
+
generated_image = saved_model(Variable(input_tensor.type(torch.FloatTensor)))
|
|
|
|
| 41 |
generated_image = generated_image.detach().squeeze().permute(1, 2, 0).numpy()
|
| 42 |
+
generated_image = (generated_image + 1.0) / 2.0 # [-1,1] → [0,1]
|
| 43 |
+
|
| 44 |
return generated_image
|
| 45 |
|
| 46 |
+
# Gradio インターフェース
|
| 47 |
+
iface = gr.Interface(
|
| 48 |
+
fn=frontalize,
|
| 49 |
+
inputs=gr.Image(type="numpy"),
|
| 50 |
+
outputs="image",
|
| 51 |
+
title="Face Frontalization",
|
| 52 |
+
description=(
|
| 53 |
+
'PyTorch implementation of a supervised GAN '
|
| 54 |
+
'(see <a href="https://blog.scaleway.com/gpu-instances-using-deep-learning-to-obtain-frontal-rendering-of-facial-images/">blog post</a>)'
|
| 55 |
+
),
|
| 56 |
+
examples=["amos.png", "clarissa.png"],
|
| 57 |
)
|
| 58 |
+
|
| 59 |
+
iface.launch()
|