JacopoMangiavacchi
commited on
Commit
•
879376e
1
Parent(s):
9cd005a
test
Browse files- Example01.jpeg +0 -0
- Output01.png +0 -0
- app.py +118 -0
- requirements.txt +4 -0
Example01.jpeg
ADDED
Output01.png
ADDED
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from huggingface_hub import hf_hub_url, cached_download
|
4 |
+
import PIL
|
5 |
+
import onnx
|
6 |
+
import onnxruntime
|
7 |
+
|
8 |
+
config_file_url = hf_hub_url("Jacopo/ToonClip", filename="model.onnx")
|
9 |
+
model_file = cached_download(config_file_url)
|
10 |
+
|
11 |
+
onnx_model = onnx.load(model_file)
|
12 |
+
onnx.checker.check_model(onnx_model)
|
13 |
+
|
14 |
+
opts = onnxruntime.SessionOptions()
|
15 |
+
opts.intra_op_num_threads = 16
|
16 |
+
ort_session = onnxruntime.InferenceSession(model_file, sess_options=opts)
|
17 |
+
|
18 |
+
input_name = ort_session.get_inputs()[0].name
|
19 |
+
output_name = ort_session.get_outputs()[0].name
|
20 |
+
|
21 |
+
def normalize(x, mean=(0., 0., 0.), std=(1.0, 1.0, 1.0)):
|
22 |
+
# x = (x - mean) / std
|
23 |
+
x = np.asarray(x, dtype=np.float32)
|
24 |
+
if len(x.shape) == 4:
|
25 |
+
for dim in range(3):
|
26 |
+
x[:, dim, :, :] = (x[:, dim, :, :] - mean[dim]) / std[dim]
|
27 |
+
if len(x.shape) == 3:
|
28 |
+
for dim in range(3):
|
29 |
+
x[dim, :, :] = (x[dim, :, :] - mean[dim]) / std[dim]
|
30 |
+
|
31 |
+
return x
|
32 |
+
|
33 |
+
def denormalize(x, mean=(0., 0., 0.), std=(1.0, 1.0, 1.0)):
|
34 |
+
# x = (x * std) + mean
|
35 |
+
x = np.asarray(x, dtype=np.float32)
|
36 |
+
if len(x.shape) == 4:
|
37 |
+
for dim in range(3):
|
38 |
+
x[:, dim, :, :] = (x[:, dim, :, :] * std[dim]) + mean[dim]
|
39 |
+
if len(x.shape) == 3:
|
40 |
+
for dim in range(3):
|
41 |
+
x[dim, :, :] = (x[dim, :, :] * std[dim]) + mean[dim]
|
42 |
+
|
43 |
+
return x
|
44 |
+
|
45 |
+
def nogan(input_img):
|
46 |
+
i = np.asarray(input_img)
|
47 |
+
i = i.astype("float32")
|
48 |
+
i = np.transpose(i, (2, 0, 1))
|
49 |
+
i = np.expand_dims(i, 0)
|
50 |
+
i = i / 255.0
|
51 |
+
i = normalize(i, (0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
|
52 |
+
|
53 |
+
ort_outs = ort_session.run([output_name], {input_name: i})
|
54 |
+
output = ort_outs
|
55 |
+
output = output[0][0]
|
56 |
+
|
57 |
+
output = denormalize(output, (0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
|
58 |
+
output = output * 255.0
|
59 |
+
output = output.astype('uint8')
|
60 |
+
output = np.transpose(output, (1, 2, 0))
|
61 |
+
output_image = PIL.Image.fromarray(output, 'RGB')
|
62 |
+
|
63 |
+
return output_image
|
64 |
+
|
65 |
+
title = "ToonClip Comics Hero Demo"
|
66 |
+
description = """
|
67 |
+
Gradio demo for ToonClip, a UNet++ network with MobileNet v3 backbone optimized for mobile frameworks and trained with VGG Perceptual Feature Loss trained with PyTorch Lighting.
|
68 |
+
To use it, simply upload an image with a face or choose an example from the list below.
|
69 |
+
"""
|
70 |
+
article = """
|
71 |
+
<style>
|
72 |
+
.boxes{
|
73 |
+
width:50%;
|
74 |
+
float:left;
|
75 |
+
}
|
76 |
+
#mainDiv{
|
77 |
+
width:50%;
|
78 |
+
margin:auto;
|
79 |
+
}
|
80 |
+
img{
|
81 |
+
max-width:100%;
|
82 |
+
}
|
83 |
+
</style>
|
84 |
+
<p style='text-align: center'>The \"ToonClip\" model was trained by <a href='https://twitter.com/JacopoMangia' target='_blank'>Jacopo Mangiavacchi</a> and available at <a href='https://github.com/jacopomangiavacchi/ComicsHeroMobileUNet' target='_blank'>Github Repo ComicsHeroMobileUNet</a></p>
|
85 |
+
<p style='text-align: center'>The \"Comics Hero dataset\" used to train this model was produced by <a href='https://linktr.ee/Norod78' target='_blank'>Doron Adler</a> and available at <a href='https://github.com/Norod/U-2-Net-StyleTransfer' target='_blank'>Github Repo Comics hero U2Net</a></p>
|
86 |
+
<p style='text-align: center'>The \"ToonClip\" iOS mobile app using a CoreML version of this model is available on Apple App Store at <a href='https://apps.apple.com/us/app/toonclip/id1536285338' target='_blank'>ToonClip</a></p>
|
87 |
+
<p style='text-align: center'>samples: </p>
|
88 |
+
<p>
|
89 |
+
<div id='mainDiv'>
|
90 |
+
<div id='divOne' class='boxes'>
|
91 |
+
<img src='https://hf.space/gradioiframe/Jacopo/ComicsHeroMobileUNet/file/Example01.jpeg' alt='Example01'/>
|
92 |
+
</div>
|
93 |
+
<div id='divTwo' class='boxes'>
|
94 |
+
<img <img src='https://hf.space/gradioiframe/Jacopo/ComicsHeroMobileUNet/file/Output01.png' alt='Output01'/>
|
95 |
+
</div>
|
96 |
+
<div id='divOne' class='boxes'>
|
97 |
+
<img src='https://hf.space/gradioiframe/Jacopo/ComicsHeroMobileUNet/file/Example01.jpeg' alt='Example01'/>
|
98 |
+
</div>
|
99 |
+
<div id='divTwo' class='boxes'>
|
100 |
+
<img <img src='https://hf.space/gradioiframe/Jacopo/ComicsHeroMobileUNet/file/Output01.png' alt='Output01'/>
|
101 |
+
</div>
|
102 |
+
</div>
|
103 |
+
</p>
|
104 |
+
"""
|
105 |
+
examples=[['Example01.jpeg']]
|
106 |
+
|
107 |
+
iface = gr.Interface(
|
108 |
+
nogan,
|
109 |
+
gr.inputs.Image(type="pil", shape=(1024, 1024)),
|
110 |
+
gr.outputs.Image(type="pil"),
|
111 |
+
title=title,
|
112 |
+
description=description,
|
113 |
+
article=article,
|
114 |
+
examples=examples,
|
115 |
+
enable_queue=True,
|
116 |
+
live=True)
|
117 |
+
|
118 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pillow==9.0.0
|
2 |
+
numpy
|
3 |
+
onnx
|
4 |
+
onnxruntime
|