soiz1 commited on
Commit
0783197
·
verified ·
1 Parent(s): 22ae380

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -20
app.py CHANGED
@@ -13,35 +13,47 @@ from PIL import Image
13
  import warnings
14
  warnings.filterwarnings('ignore')
15
 
16
- path_to_model = hf_hub_download(repo_id="opetrova/face-frontalization", filename="generator_v0.pt")
 
 
 
 
17
 
18
- # Download network.py into the current directory
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
- saved_model = torch.load(path_to_model, map_location=torch.device('cpu'))
 
24
 
25
  def frontalize(image):
26
-
27
- # Convert the test image to a [1, 3, 128, 128]-shaped torch tensor
28
- # (as required by the frontalization model)
29
- preprocess = transforms.Compose((transforms.ToPILImage(),
30
- transforms.Resize(size = (128, 128)),
31
- transforms.ToTensor()))
32
  input_tensor = torch.unsqueeze(preprocess(image), 0)
33
-
34
- # Use the saved model to generate an output (whose values go between -1 and 1,
35
- # and this will need to get fixed before the output is displayed)
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
- iface = gr.Interface(frontalize, gr.inputs.Image(type="numpy"), "image",
43
- title='Face Frontalization',
44
- description='PyTorch implementation of a supervised GAN (see <a href="https://blog.scaleway.com/gpu-instances-using-deep-learning-to-obtain-frontal-rendering-of-facial-images/">blog post</a>)',
45
- examples=["amos.png", "clarissa.png"],
 
 
 
 
 
 
 
46
  )
47
- iface.launch()
 
 
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()