Spaces:
Running
on
Zero
Running
on
Zero
Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- README.md +2 -1
- app.py +40 -16
- content_images/Beach.jpg +0 -0
- content_images/CameraGirl.jpg +0 -0
- content_images/GoldenRetriever.jpg +0 -0
- content_images/NYCSkyline.jpg +0 -0
- content_images/PaintedLadies.jpg +0 -0
- content_images/Scream.jpg +3 -0
- content_images/SeaTurtle.jpg +0 -0
- content_images/StandingOnCliff.png +0 -0
- content_images/Surfer.jpg +0 -0
- content_images/TajMahal.jpg +0 -0
- requirements.txt +1 -0
- style_images/OilPainting.jpg +0 -0
.gitattributes
CHANGED
@@ -35,3 +35,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
style_images/Scream.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
style_images/StarryNight.jpg filter=lfs diff=lfs merge=lfs -text
|
|
|
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
style_images/Scream.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
style_images/StarryNight.jpg filter=lfs diff=lfs merge=lfs -text
|
38 |
+
content_images/Scream.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
---
|
2 |
title: style-transfer
|
|
|
3 |
app_file: app.py
|
4 |
sdk: gradio
|
5 |
sdk_version: 4.44.0
|
6 |
---
|
7 |
-
# style-transfer
|
|
|
1 |
---
|
2 |
title: style-transfer
|
3 |
+
emoji: 🖼️
|
4 |
app_file: app.py
|
5 |
sdk: gradio
|
6 |
sdk_version: 4.44.0
|
7 |
---
|
8 |
+
# style-transfer
|
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
from PIL import Image
|
2 |
|
3 |
import spaces
|
@@ -7,6 +8,7 @@ import torch.optim as optim
|
|
7 |
import torchvision.transforms as transforms
|
8 |
import torchvision.models as models
|
9 |
import gradio as gr
|
|
|
10 |
|
11 |
device = 'cpu'
|
12 |
if torch.backends.mps.is_available():
|
@@ -74,19 +76,30 @@ def save_img(img, original_size):
|
|
74 |
return img
|
75 |
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
@spaces.GPU
|
78 |
-
def
|
79 |
-
|
80 |
img_size = 512
|
81 |
content_img, original_size = load_img(content_image, img_size)
|
82 |
content_img = content_img.to(device)
|
83 |
-
style_img = load_img_from_path(
|
|
|
|
|
84 |
|
85 |
iters = 100
|
86 |
lr = 1e-1
|
87 |
alpha = 1
|
88 |
beta = 1
|
89 |
|
|
|
90 |
generated_img = content_img.clone().requires_grad_(True)
|
91 |
optimizer = optim.Adam([generated_img], lr=lr)
|
92 |
|
@@ -117,25 +130,36 @@ def transfer_style(content_image):
|
|
117 |
total_loss.backward()
|
118 |
optimizer.step()
|
119 |
|
120 |
-
if iter %
|
121 |
saved_image = save_img(generated_img, original_size)
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
yield save_img(generated_img, original_size), str(round(iter/iters*100))+'%'
|
127 |
-
|
|
|
|
|
|
|
128 |
|
129 |
interface = gr.Interface(
|
130 |
-
fn=
|
131 |
inputs=[
|
132 |
-
gr.Image(label='Content', type='pil', sources=['upload'])
|
|
|
133 |
],
|
134 |
outputs=[
|
135 |
-
|
136 |
-
gr.Label(label='Progress')
|
137 |
],
|
138 |
-
title="
|
139 |
api_name='style',
|
140 |
-
allow_flagging='
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
).launch(inbrowser=True)
|
|
|
1 |
+
import time
|
2 |
from PIL import Image
|
3 |
|
4 |
import spaces
|
|
|
8 |
import torchvision.transforms as transforms
|
9 |
import torchvision.models as models
|
10 |
import gradio as gr
|
11 |
+
from gradio_imageslider import ImageSlider
|
12 |
|
13 |
device = 'cpu'
|
14 |
if torch.backends.mps.is_available():
|
|
|
76 |
return img
|
77 |
|
78 |
|
79 |
+
style_options = {
|
80 |
+
'Starry Night': 'StarryNight.jpg',
|
81 |
+
'Great Wave': 'GreatWave.jpg',
|
82 |
+
'Lego Bricks': 'LegoBricks.jpg',
|
83 |
+
'Oil Painting': 'OilPainting.jpg',
|
84 |
+
}
|
85 |
+
style_options = {k: f'./style_images/{v}' for k, v in style_options.items()}
|
86 |
+
|
87 |
@spaces.GPU
|
88 |
+
def inference(content_image, style_image):
|
89 |
+
print('STYLE STYLE', style_image)
|
90 |
img_size = 512
|
91 |
content_img, original_size = load_img(content_image, img_size)
|
92 |
content_img = content_img.to(device)
|
93 |
+
style_img = load_img_from_path(style_options[style_image], img_size)[0].to(device)
|
94 |
+
|
95 |
+
print('CONTENT IMG SIZE:', original_size)
|
96 |
|
97 |
iters = 100
|
98 |
lr = 1e-1
|
99 |
alpha = 1
|
100 |
beta = 1
|
101 |
|
102 |
+
st = time.time()
|
103 |
generated_img = content_img.clone().requires_grad_(True)
|
104 |
optimizer = optim.Adam([generated_img], lr=lr)
|
105 |
|
|
|
130 |
total_loss.backward()
|
131 |
optimizer.step()
|
132 |
|
133 |
+
if iter % 15 == 0:
|
134 |
saved_image = save_img(generated_img, original_size)
|
135 |
+
yield (content_image, saved_image), str(round(iter/iters*100))+'%'
|
136 |
+
|
137 |
+
et = time.time()
|
138 |
+
print('TIME TAKEN:', et-st)
|
139 |
+
yield (content_image, save_img(generated_img, original_size)), str(round(iter/iters*100))+'%'
|
140 |
+
|
141 |
+
css = """
|
142 |
+
#style, #progress-label { height: 100px }
|
143 |
+
"""
|
144 |
|
145 |
interface = gr.Interface(
|
146 |
+
fn=inference,
|
147 |
inputs=[
|
148 |
+
gr.Image(label='Content', type='pil', sources=['upload'], elem_id='content'),
|
149 |
+
gr.Dropdown(choices=list(style_options.keys()), label='Style', value='Starry Night', type='value', elem_id='style'),
|
150 |
],
|
151 |
outputs=[
|
152 |
+
ImageSlider(position=0.15, label='Output', show_download_button=True, interactive=False, elem_id='output'),
|
153 |
+
gr.Label(label='Progress', elem_id='progress-label'),
|
154 |
],
|
155 |
+
title="🖼️ Neural Style Transfer",
|
156 |
api_name='style',
|
157 |
+
allow_flagging='manual',
|
158 |
+
examples=[
|
159 |
+
['./content_images/TajMahal.jpg', 'Starry Night'],
|
160 |
+
['./content_images/GoldenRetriever.jpg', 'Lego Bricks'],
|
161 |
+
['./content_images/Beach.jpg', 'Oil Painting'],
|
162 |
+
['./content_images/StandingOnCliff.png', 'Great Wave'],
|
163 |
+
],
|
164 |
+
css=css
|
165 |
).launch(inbrowser=True)
|
content_images/Beach.jpg
ADDED
content_images/CameraGirl.jpg
ADDED
content_images/GoldenRetriever.jpg
ADDED
content_images/NYCSkyline.jpg
ADDED
content_images/PaintedLadies.jpg
ADDED
content_images/Scream.jpg
ADDED
Git LFS Details
|
content_images/SeaTurtle.jpg
ADDED
content_images/StandingOnCliff.png
ADDED
content_images/Surfer.jpg
ADDED
content_images/TajMahal.jpg
ADDED
requirements.txt
CHANGED
@@ -3,4 +3,5 @@ torch
|
|
3 |
torchvision
|
4 |
pillow
|
5 |
gradio
|
|
|
6 |
spaces
|
|
|
3 |
torchvision
|
4 |
pillow
|
5 |
gradio
|
6 |
+
gradio_imageslider
|
7 |
spaces
|
style_images/OilPainting.jpg
ADDED