Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .gitattributes +2 -0
- .github/workflows/update_space.yml +28 -0
- .gitignore +165 -0
- LICENSE +21 -0
- README.md +3 -8
- app.py +129 -0
- gatys2015.ipynb +0 -0
- generated_images/TajMahal-StarryNight.jpg +0 -0
- input_images/GoldenRetriever.jpg +0 -0
- input_images/NYCSkyline.jpg +0 -0
- input_images/PaintedLadies.jpg +0 -0
- input_images/SeaTurtle.jpg +0 -0
- input_images/Surfer.jpg +0 -0
- input_images/TajMahal.jpg +0 -0
- style_images/GreatWave.jpg +0 -0
- style_images/LegoBricks.jpg +0 -0
- style_images/Scream.jpg +3 -0
- style_images/StarryNight.jpg +3 -0
- style_images/WaterColor2.jpg +0 -0
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst 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
|
.github/workflows/update_space.yml
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: Run Python script
|
2 |
+
|
3 |
+
on:
|
4 |
+
push:
|
5 |
+
branches:
|
6 |
+
- main
|
7 |
+
|
8 |
+
jobs:
|
9 |
+
build:
|
10 |
+
runs-on: ubuntu-latest
|
11 |
+
|
12 |
+
steps:
|
13 |
+
- name: Checkout
|
14 |
+
uses: actions/checkout@v2
|
15 |
+
|
16 |
+
- name: Set up Python
|
17 |
+
uses: actions/setup-python@v2
|
18 |
+
with:
|
19 |
+
python-version: '3.9'
|
20 |
+
|
21 |
+
- name: Install Gradio
|
22 |
+
run: python -m pip install gradio
|
23 |
+
|
24 |
+
- name: Log in to Hugging Face
|
25 |
+
run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
|
26 |
+
|
27 |
+
- name: Deploy to Spaces
|
28 |
+
run: gradio deploy
|
.gitignore
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Mac-specific
|
2 |
+
.DS_Store
|
3 |
+
|
4 |
+
# Byte-compiled / optimized / DLL files
|
5 |
+
__pycache__/
|
6 |
+
*.py[cod]
|
7 |
+
*$py.class
|
8 |
+
|
9 |
+
# C extensions
|
10 |
+
*.so
|
11 |
+
|
12 |
+
# Distribution / packaging
|
13 |
+
.Python
|
14 |
+
build/
|
15 |
+
develop-eggs/
|
16 |
+
dist/
|
17 |
+
downloads/
|
18 |
+
eggs/
|
19 |
+
.eggs/
|
20 |
+
lib/
|
21 |
+
lib64/
|
22 |
+
parts/
|
23 |
+
sdist/
|
24 |
+
var/
|
25 |
+
wheels/
|
26 |
+
share/python-wheels/
|
27 |
+
*.egg-info/
|
28 |
+
.installed.cfg
|
29 |
+
*.egg
|
30 |
+
MANIFEST
|
31 |
+
|
32 |
+
# PyInstaller
|
33 |
+
# Usually these files are written by a python script from a template
|
34 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
35 |
+
*.manifest
|
36 |
+
*.spec
|
37 |
+
|
38 |
+
# Installer logs
|
39 |
+
pip-log.txt
|
40 |
+
pip-delete-this-directory.txt
|
41 |
+
|
42 |
+
# Unit test / coverage reports
|
43 |
+
htmlcov/
|
44 |
+
.tox/
|
45 |
+
.nox/
|
46 |
+
.coverage
|
47 |
+
.coverage.*
|
48 |
+
.cache
|
49 |
+
nosetests.xml
|
50 |
+
coverage.xml
|
51 |
+
*.cover
|
52 |
+
*.py,cover
|
53 |
+
.hypothesis/
|
54 |
+
.pytest_cache/
|
55 |
+
cover/
|
56 |
+
|
57 |
+
# Translations
|
58 |
+
*.mo
|
59 |
+
*.pot
|
60 |
+
|
61 |
+
# Django stuff:
|
62 |
+
*.log
|
63 |
+
local_settings.py
|
64 |
+
db.sqlite3
|
65 |
+
db.sqlite3-journal
|
66 |
+
|
67 |
+
# Flask stuff:
|
68 |
+
instance/
|
69 |
+
.webassets-cache
|
70 |
+
|
71 |
+
# Scrapy stuff:
|
72 |
+
.scrapy
|
73 |
+
|
74 |
+
# Sphinx documentation
|
75 |
+
docs/_build/
|
76 |
+
|
77 |
+
# PyBuilder
|
78 |
+
.pybuilder/
|
79 |
+
target/
|
80 |
+
|
81 |
+
# Jupyter Notebook
|
82 |
+
.ipynb_checkpoints
|
83 |
+
|
84 |
+
# IPython
|
85 |
+
profile_default/
|
86 |
+
ipython_config.py
|
87 |
+
|
88 |
+
# pyenv
|
89 |
+
# For a library or package, you might want to ignore these files since the code is
|
90 |
+
# intended to run in multiple environments; otherwise, check them in:
|
91 |
+
# .python-version
|
92 |
+
|
93 |
+
# pipenv
|
94 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
95 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
96 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
97 |
+
# install all needed dependencies.
|
98 |
+
#Pipfile.lock
|
99 |
+
|
100 |
+
# poetry
|
101 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
102 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
103 |
+
# commonly ignored for libraries.
|
104 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
105 |
+
#poetry.lock
|
106 |
+
|
107 |
+
# pdm
|
108 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
109 |
+
#pdm.lock
|
110 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
111 |
+
# in version control.
|
112 |
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
113 |
+
.pdm.toml
|
114 |
+
.pdm-python
|
115 |
+
.pdm-build/
|
116 |
+
|
117 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
118 |
+
__pypackages__/
|
119 |
+
|
120 |
+
# Celery stuff
|
121 |
+
celerybeat-schedule
|
122 |
+
celerybeat.pid
|
123 |
+
|
124 |
+
# SageMath parsed files
|
125 |
+
*.sage.py
|
126 |
+
|
127 |
+
# Environments
|
128 |
+
.env
|
129 |
+
.venv
|
130 |
+
env/
|
131 |
+
venv/
|
132 |
+
ENV/
|
133 |
+
env.bak/
|
134 |
+
venv.bak/
|
135 |
+
|
136 |
+
# Spyder project settings
|
137 |
+
.spyderproject
|
138 |
+
.spyproject
|
139 |
+
|
140 |
+
# Rope project settings
|
141 |
+
.ropeproject
|
142 |
+
|
143 |
+
# mkdocs documentation
|
144 |
+
/site
|
145 |
+
|
146 |
+
# mypy
|
147 |
+
.mypy_cache/
|
148 |
+
.dmypy.json
|
149 |
+
dmypy.json
|
150 |
+
|
151 |
+
# Pyre type checker
|
152 |
+
.pyre/
|
153 |
+
|
154 |
+
# pytype static type analyzer
|
155 |
+
.pytype/
|
156 |
+
|
157 |
+
# Cython debug symbols
|
158 |
+
cython_debug/
|
159 |
+
|
160 |
+
# PyCharm
|
161 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
162 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
163 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
164 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
165 |
+
#.idea/
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2024 Jai Amin
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,12 +1,7 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
colorFrom: purple
|
5 |
-
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.44.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: style-transfer
|
3 |
+
app_file: app.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
sdk_version: 4.44.0
|
|
|
|
|
6 |
---
|
7 |
+
# style-transfer
|
|
app.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
import torch.optim as optim
|
5 |
+
import torchvision.transforms as transforms
|
6 |
+
import torchvision.models as models
|
7 |
+
import torchvision.utils as utils
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
device = 'cpu'
|
11 |
+
if torch.backends.mps.is_available():
|
12 |
+
device = 'mps'
|
13 |
+
if torch.cuda.is_available():
|
14 |
+
device = 'cuda'
|
15 |
+
print('DEVICE:', device)
|
16 |
+
|
17 |
+
class VGG_19(nn.Module):
|
18 |
+
def __init__(self):
|
19 |
+
super(VGG_19, self).__init__()
|
20 |
+
self.model = models.vgg19(pretrained=True).features[:30]
|
21 |
+
|
22 |
+
for i, _ in enumerate(self.model):
|
23 |
+
if i in [4, 9, 18, 27]:
|
24 |
+
self.model[i] = nn.AvgPool2d(kernel_size=2, stride=2, padding=0)
|
25 |
+
|
26 |
+
def forward(self, x):
|
27 |
+
features = []
|
28 |
+
|
29 |
+
for i, layer in enumerate(self.model):
|
30 |
+
x = layer(x)
|
31 |
+
if i in [0, 5, 10, 19, 28]:
|
32 |
+
features.append(x)
|
33 |
+
return features
|
34 |
+
|
35 |
+
model = VGG_19().to(device)
|
36 |
+
for param in model.parameters():
|
37 |
+
param.requires_grad = False
|
38 |
+
|
39 |
+
def load_img(img: Image, img_size):
|
40 |
+
original_size = img.size
|
41 |
+
|
42 |
+
transform = transforms.Compose([
|
43 |
+
transforms.Resize((img_size, img_size)),
|
44 |
+
transforms.ToTensor()
|
45 |
+
])
|
46 |
+
img = transform(img).unsqueeze(0)
|
47 |
+
return img, original_size
|
48 |
+
|
49 |
+
def load_img_from_path(path_to_image, img_size):
|
50 |
+
img = Image.open(path_to_image)
|
51 |
+
original_size = img.size
|
52 |
+
|
53 |
+
transform = transforms.Compose([
|
54 |
+
transforms.Resize((img_size, img_size)),
|
55 |
+
transforms.ToTensor()
|
56 |
+
])
|
57 |
+
img = transform(img).unsqueeze(0)
|
58 |
+
return img, original_size
|
59 |
+
|
60 |
+
def save_img(img, original_size):
|
61 |
+
img = img.cpu().clone()
|
62 |
+
img = img.squeeze(0)
|
63 |
+
|
64 |
+
# address tensor value scaling and quantization
|
65 |
+
img = torch.clamp(img, 0, 1)
|
66 |
+
img = img.mul(255).byte()
|
67 |
+
|
68 |
+
unloader = transforms.ToPILImage()
|
69 |
+
img = unloader(img)
|
70 |
+
|
71 |
+
img = img.resize(original_size, Image.Resampling.LANCZOS)
|
72 |
+
|
73 |
+
return img
|
74 |
+
|
75 |
+
|
76 |
+
def transfer_style(content_image):
|
77 |
+
style_img_filename = 'StarryNight.jpg'
|
78 |
+
img_size = 512
|
79 |
+
content_img, original_size = load_img(content_image, img_size)
|
80 |
+
content_img = content_img.to(device)
|
81 |
+
style_img = load_img_from_path(f'./style_images/{style_img_filename}', img_size)[0].to(device)
|
82 |
+
|
83 |
+
iters = 100
|
84 |
+
lr = 1e-1
|
85 |
+
alpha = 1
|
86 |
+
beta = 1
|
87 |
+
|
88 |
+
generated_img = content_img.clone().requires_grad_(True)
|
89 |
+
optimizer = optim.Adam([generated_img], lr=lr)
|
90 |
+
|
91 |
+
for iter in range(iters+1):
|
92 |
+
generated_features = model(generated_img)
|
93 |
+
content_features = model(content_img)
|
94 |
+
style_features = model(style_img)
|
95 |
+
|
96 |
+
content_loss = 0
|
97 |
+
style_loss = 0
|
98 |
+
|
99 |
+
for generated_feature, content_feature, style_feature in zip(generated_features, content_features, style_features):
|
100 |
+
|
101 |
+
batch_size, n_feature_maps, height, width = generated_feature.size()
|
102 |
+
|
103 |
+
content_loss += (torch.mean((generated_feature - content_feature) ** 2))
|
104 |
+
|
105 |
+
G = torch.mm((generated_feature.view(batch_size * n_feature_maps, height * width)), (generated_feature.view(batch_size * n_feature_maps, height * width)).t())
|
106 |
+
A = torch.mm((style_feature.view(batch_size * n_feature_maps, height * width)), (style_feature.view(batch_size * n_feature_maps, height * width)).t())
|
107 |
+
|
108 |
+
E_l = ((G - A) ** 2)
|
109 |
+
w_l = 1/5
|
110 |
+
style_loss += torch.mean(w_l * E_l)
|
111 |
+
|
112 |
+
total_loss = alpha * content_loss + beta * style_loss
|
113 |
+
optimizer.zero_grad()
|
114 |
+
total_loss.backward()
|
115 |
+
optimizer.step()
|
116 |
+
|
117 |
+
yield save_img(generated_img, original_size)
|
118 |
+
|
119 |
+
yield save_img(generated_img, original_size)
|
120 |
+
|
121 |
+
|
122 |
+
interface = gr.Interface(
|
123 |
+
fn=transfer_style,
|
124 |
+
inputs=[gr.Image(type='pil')],
|
125 |
+
outputs='image',
|
126 |
+
title="Starry Night Style Transfer",
|
127 |
+
api_name='style',
|
128 |
+
allow_flagging='never'
|
129 |
+
).launch()
|
gatys2015.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
generated_images/TajMahal-StarryNight.jpg
ADDED
input_images/GoldenRetriever.jpg
ADDED
input_images/NYCSkyline.jpg
ADDED
input_images/PaintedLadies.jpg
ADDED
input_images/SeaTurtle.jpg
ADDED
input_images/Surfer.jpg
ADDED
input_images/TajMahal.jpg
ADDED
style_images/GreatWave.jpg
ADDED
style_images/LegoBricks.jpg
ADDED
style_images/Scream.jpg
ADDED
Git LFS Details
|
style_images/StarryNight.jpg
ADDED
Git LFS Details
|
style_images/WaterColor2.jpg
ADDED