Spaces:
Running
on
Zero
Running
on
Zero
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -12,7 +12,8 @@ import torchvision.models as models
|
|
12 |
import gradio as gr
|
13 |
from gradio_imageslider import ImageSlider
|
14 |
|
15 |
-
device = '
|
|
|
16 |
print('DEVICE:', device)
|
17 |
|
18 |
class VGG_19(nn.Module):
|
@@ -83,7 +84,7 @@ style_options = {
|
|
83 |
style_options = {k: f'./style_images/{v}' for k, v in style_options.items()}
|
84 |
|
85 |
@spaces.GPU
|
86 |
-
def inference(content_image, style_image, progress=gr.Progress(track_tqdm=True)):
|
87 |
yield None
|
88 |
print('-'*15)
|
89 |
print('STYLE:', style_image)
|
@@ -94,7 +95,7 @@ def inference(content_image, style_image, progress=gr.Progress(track_tqdm=True))
|
|
94 |
|
95 |
print('CONTENT IMG SIZE:', original_size)
|
96 |
|
97 |
-
iters =
|
98 |
lr = 1e-1
|
99 |
alpha = 1
|
100 |
beta = 1
|
@@ -102,8 +103,8 @@ def inference(content_image, style_image, progress=gr.Progress(track_tqdm=True))
|
|
102 |
st = time.time()
|
103 |
generated_img = content_img.clone().requires_grad_(True)
|
104 |
optimizer = optim.Adam([generated_img], lr=lr)
|
105 |
-
|
106 |
-
for iter in tqdm(range(iters
|
107 |
generated_features = model(generated_img)
|
108 |
content_features = model(content_img)
|
109 |
style_features = model(style_img)
|
@@ -134,31 +135,40 @@ def inference(content_image, style_image, progress=gr.Progress(track_tqdm=True))
|
|
134 |
yield content_image, save_img(generated_img, original_size)
|
135 |
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
],
|
143 |
-
|
144 |
-
|
145 |
-
],
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
import gradio as gr
|
13 |
from gradio_imageslider import ImageSlider
|
14 |
|
15 |
+
device = 'mps'
|
16 |
+
# device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
17 |
print('DEVICE:', device)
|
18 |
|
19 |
class VGG_19(nn.Module):
|
|
|
84 |
style_options = {k: f'./style_images/{v}' for k, v in style_options.items()}
|
85 |
|
86 |
@spaces.GPU
|
87 |
+
def inference(content_image, style_image, style_strength, progress=gr.Progress(track_tqdm=True)):
|
88 |
yield None
|
89 |
print('-'*15)
|
90 |
print('STYLE:', style_image)
|
|
|
95 |
|
96 |
print('CONTENT IMG SIZE:', original_size)
|
97 |
|
98 |
+
iters = style_strength
|
99 |
lr = 1e-1
|
100 |
alpha = 1
|
101 |
beta = 1
|
|
|
103 |
st = time.time()
|
104 |
generated_img = content_img.clone().requires_grad_(True)
|
105 |
optimizer = optim.Adam([generated_img], lr=lr)
|
106 |
+
|
107 |
+
for iter in tqdm(range(iters)):
|
108 |
generated_features = model(generated_img)
|
109 |
content_features = model(content_img)
|
110 |
style_features = model(style_img)
|
|
|
135 |
yield content_image, save_img(generated_img, original_size)
|
136 |
|
137 |
|
138 |
+
examples = [
|
139 |
+
# page 1
|
140 |
+
['./content_images/TajMahal.jpg', 'Starry Night'],
|
141 |
+
['./content_images/GoldenRetriever.jpg', 'Lego Bricks'],
|
142 |
+
['./content_images/Beach.jpg', 'Oil Painting'],
|
143 |
+
['./content_images/StandingOnCliff.png', 'Great Wave'],
|
144 |
+
# page 2
|
145 |
+
['./content_images/Surfer.jpg', 'Starry Night'],
|
146 |
+
['./content_images/CameraGirl.jpg', 'Lego Bricks'],
|
147 |
+
['./content_images/NYCSkyline.jpg', 'Oil Painting'],
|
148 |
+
['./content_images/GoldenRetriever.jpg', 'Great Wave'],
|
149 |
+
]
|
150 |
+
|
151 |
+
with gr.Blocks(title='🖼️ Neural Style Transfer') as demo:
|
152 |
+
gr.HTML("<h1 style='text-align: center'>🖼️ Neural Style Transfer</h1>")
|
153 |
+
with gr.Row():
|
154 |
+
with gr.Column():
|
155 |
+
content_image = gr.Image(label='Content', type='pil', sources=['upload'])
|
156 |
+
style_dropdown = gr.Dropdown(choices=list(style_options.keys()), label='Style', value='Starry Night', type='value')
|
157 |
+
with gr.Accordion('Advanced Settings', open=False):
|
158 |
+
style_strength = gr.Slider(label='Style Strength', minimum=1, maximum=200, step=1, value=100)
|
159 |
+
submit_button = gr.Button('Submit')
|
160 |
+
with gr.Column():
|
161 |
+
output_image = ImageSlider(position=0.25, label='Output', show_download_button=True, interactive=False)
|
162 |
+
|
163 |
+
submit_button.click(fn=inference, inputs=[content_image, style_dropdown, style_strength], outputs=[output_image])
|
164 |
+
|
165 |
+
gr.Examples(
|
166 |
+
fn=inference,
|
167 |
+
examples=examples,
|
168 |
+
inputs=[content_image, style_dropdown],
|
169 |
+
outputs=[output_image],
|
170 |
+
examples_per_page=len(style_options),
|
171 |
+
cache_examples='lazy',
|
172 |
+
)
|
173 |
+
|
174 |
+
demo.launch(inbrowser=True, show_api=True)
|