Duplicate from FrankAst/image_mixer
Browse filesCo-authored-by: Franco Astegiano <FrankAst@users.noreply.huggingface.co>
- .github/workflows/main.yml +19 -0
- README.md +12 -0
- app.py +201 -0
- requirements.txt +3 -0
.github/workflows/main.yml
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: Sync to Hugging Face hub
|
2 |
+
on:
|
3 |
+
push:
|
4 |
+
branches: [main]
|
5 |
+
|
6 |
+
# to run this workflow manually from the Actions tab
|
7 |
+
workflow_dispatch:
|
8 |
+
|
9 |
+
jobs:
|
10 |
+
sync-to-hub:
|
11 |
+
runs-on: ubuntu-latest
|
12 |
+
steps:
|
13 |
+
- uses: actions/checkout@v2
|
14 |
+
with:
|
15 |
+
fetch-depth: 0
|
16 |
+
- name: Push to hub
|
17 |
+
env:
|
18 |
+
HF_TOKEN: ${{ secrets.HF_TOKEN}}
|
19 |
+
run: git push --force https://FrankAst:$HF_TOKEN@huggingface.co/spaces/FrankAst/image_mixer
|
README.md
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Image_mixer
|
3 |
+
emoji: ⚡
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: pink
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 2.9.4
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: FrankAst/image_mixer
|
11 |
+
---
|
12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
app.py
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""TF-Hub: Fast Style Transfer for Arbitrary Styles.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf2_arbitrary_image_stylization.ipynb
|
8 |
+
|
9 |
+
##### Copyright 2019 The TensorFlow Hub Authors.
|
10 |
+
|
11 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
12 |
+
"""
|
13 |
+
|
14 |
+
# Copyright 2019 The TensorFlow Hub Authors. All Rights Reserved.
|
15 |
+
#
|
16 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
17 |
+
# you may not use this file except in compliance with the License.
|
18 |
+
# You may obtain a copy of the License at
|
19 |
+
#
|
20 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
21 |
+
#
|
22 |
+
# Unless required by applicable law or agreed to in writing, software
|
23 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
24 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
25 |
+
# See the License for the specific language governing permissions and
|
26 |
+
# limitations under the License.
|
27 |
+
# ==============================================================================
|
28 |
+
|
29 |
+
"""# Fast Style Transfer for Arbitrary Styles
|
30 |
+
|
31 |
+
<table class="tfo-notebook-buttons" align="left">
|
32 |
+
<td>
|
33 |
+
<a target="_blank" href="https://www.tensorflow.org/hub/tutorials/tf2_arbitrary_image_stylization"><img src="https://www.tensorflow.org/images/tf_logo_32px.png" />View on TensorFlow.org</a>
|
34 |
+
</td>
|
35 |
+
<td>
|
36 |
+
<a target="_blank" href="https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf2_arbitrary_image_stylization.ipynb"><img src="https://www.tensorflow.org/images/colab_logo_32px.png" />Run in Google Colab</a>
|
37 |
+
</td>
|
38 |
+
<td>
|
39 |
+
<a target="_blank" href="https://github.com/tensorflow/hub/blob/master/examples/colab/tf2_arbitrary_image_stylization.ipynb"><img src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" />View on GitHub</a>
|
40 |
+
</td>
|
41 |
+
<td>
|
42 |
+
<a href="https://storage.googleapis.com/tensorflow_docs/hub/examples/colab/tf2_arbitrary_image_stylization.ipynb"><img src="https://www.tensorflow.org/images/download_logo_32px.png" />Download notebook</a>
|
43 |
+
</td>
|
44 |
+
<td>
|
45 |
+
<a href="https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2"><img src="https://www.tensorflow.org/images/hub_logo_32px.png" />See TF Hub model</a>
|
46 |
+
</td>
|
47 |
+
</table>
|
48 |
+
|
49 |
+
Based on the model code in [magenta](https://github.com/tensorflow/magenta/tree/master/magenta/models/arbitrary_image_stylization) and the publication:
|
50 |
+
|
51 |
+
[Exploring the structure of a real-time, arbitrary neural artistic stylization
|
52 |
+
network](https://arxiv.org/abs/1705.06830).
|
53 |
+
*Golnaz Ghiasi, Honglak Lee,
|
54 |
+
Manjunath Kudlur, Vincent Dumoulin, Jonathon Shlens*,
|
55 |
+
Proceedings of the British Machine Vision Conference (BMVC), 2017.
|
56 |
+
|
57 |
+
## Setup
|
58 |
+
|
59 |
+
Let's start with importing TF2 and all relevant dependencies.
|
60 |
+
"""
|
61 |
+
|
62 |
+
import functools
|
63 |
+
import os
|
64 |
+
from PIL import Image
|
65 |
+
from matplotlib import gridspec
|
66 |
+
import matplotlib.pylab as plt
|
67 |
+
import numpy as np
|
68 |
+
import tensorflow as tf
|
69 |
+
import tensorflow_hub as hub
|
70 |
+
import gradio as gr
|
71 |
+
|
72 |
+
# @title Define image loading and visualization functions { display-mode: "form" }
|
73 |
+
|
74 |
+
def crop_center(image):
|
75 |
+
"""Returns a cropped square image."""
|
76 |
+
shape = image.shape
|
77 |
+
new_shape = min(shape[1], shape[2])
|
78 |
+
offset_y = max(shape[1] - shape[2], 0) // 2
|
79 |
+
offset_x = max(shape[2] - shape[1], 0) // 2
|
80 |
+
image = tf.image.crop_to_bounding_box(
|
81 |
+
image, offset_y, offset_x, new_shape, new_shape)
|
82 |
+
return image
|
83 |
+
|
84 |
+
@functools.lru_cache(maxsize=None)
|
85 |
+
def load_image(image, image_size=(256, 256), preserve_aspect_ratio=True):
|
86 |
+
"""Loads and preprocesses images."""
|
87 |
+
# Cache image file locally.
|
88 |
+
#image_path = tf.keras.utils.get_file(os.path.basename(image_url)[-128:], image_url)
|
89 |
+
# Load and convert to float32 numpy array, add batch dimension, and normalize to range [0, 1].
|
90 |
+
#img = tf.io.decode_image(
|
91 |
+
# tf.io.read_file(image_path),
|
92 |
+
#channels=3, dtype=tf.float32)[tf.newaxis, ...]
|
93 |
+
#img = crop_center(image)
|
94 |
+
img = tf.image.resize(image, image_size, preserve_aspect_ratio=True)
|
95 |
+
return img
|
96 |
+
|
97 |
+
def show_n(images, titles=('',)):
|
98 |
+
n = len(images)
|
99 |
+
image_sizes = [image.shape[1] for image in images]
|
100 |
+
w = (image_sizes[0] * 6) // 320
|
101 |
+
plt.figure(figsize=(w * n, w))
|
102 |
+
gs = gridspec.GridSpec(1, n, width_ratios=image_sizes)
|
103 |
+
for i in range(n):
|
104 |
+
plt.subplot(gs[i])
|
105 |
+
plt.imshow(images[i][0], aspect='equal')
|
106 |
+
plt.axis('off')
|
107 |
+
plt.title(titles[i] if len(titles) > i else '')
|
108 |
+
plt.show()
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
"""Let's get as well some images to play with."""
|
114 |
+
|
115 |
+
# @title Load example images { display-mode: "form" }
|
116 |
+
|
117 |
+
#content_image_url = 'https://live.staticflickr.com/65535/52032998695_f57c61746c_c.jpg' # @param {type:"string"}
|
118 |
+
#style_image_url = 'https://live.staticflickr.com/65535/52032731604_a815a0b19f_c.jpg' # @param {type:"string"}
|
119 |
+
output_image_size = 384 # @param {type:"integer"}
|
120 |
+
|
121 |
+
# The content image size can be arbitrary.
|
122 |
+
content_img_size = (output_image_size, output_image_size)
|
123 |
+
# The style prediction model was trained with image size 256 and it's the
|
124 |
+
# recommended image size for the style image (though, other sizes work as
|
125 |
+
# well but will lead to different results).
|
126 |
+
style_img_size = (256, 256) # Recommended to keep it at 256.
|
127 |
+
|
128 |
+
|
129 |
+
|
130 |
+
# Load images from app
|
131 |
+
content_image_input = gr.inputs.Image(label="Content Image")
|
132 |
+
style_image_input = gr.inputs.Image(shape=(256, 256), label="Style Image")
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
#content_image = load_image(content_image_input, content_img_size)
|
137 |
+
#style_image = load_image(style_image_input, style_img_size)
|
138 |
+
#style_image = tf.nn.avg_pool(style_image, ksize=[3,3], strides=[1,1], padding='SAME')
|
139 |
+
#show_n([content_image, style_image], ['Content image', 'Style image'])
|
140 |
+
|
141 |
+
"""## Import TF Hub module"""
|
142 |
+
|
143 |
+
# Load TF Hub module.
|
144 |
+
|
145 |
+
hub_handle = 'https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2'
|
146 |
+
hub_module = hub.load(hub_handle)
|
147 |
+
|
148 |
+
"""The signature of this hub module for image stylization is:
|
149 |
+
```
|
150 |
+
outputs = hub_module(content_image, style_image)
|
151 |
+
stylized_image = outputs[0]
|
152 |
+
```
|
153 |
+
Where `content_image`, `style_image`, and `stylized_image` are expected to be 4-D Tensors with shapes `[batch_size, image_height, image_width, 3]`.
|
154 |
+
|
155 |
+
In the current example we provide only single images and therefore the batch dimension is 1, but one can use the same module to process more images at the same time.
|
156 |
+
|
157 |
+
The input and output values of the images should be in the range [0, 1].
|
158 |
+
|
159 |
+
The shapes of content and style image don't have to match. Output image shape
|
160 |
+
is the same as the content image shape.
|
161 |
+
|
162 |
+
## Demonstrate image stylization
|
163 |
+
"""
|
164 |
+
|
165 |
+
# Stylize content image with given style image.
|
166 |
+
# This is pretty fast within a few milliseconds on a GPU.
|
167 |
+
'''
|
168 |
+
def modify(imageinput,style_input):
|
169 |
+
content_image = load_image(imageinput, content_img_size)
|
170 |
+
style_image = load_image(style_input, style_img_size)
|
171 |
+
style_image = tf.nn.avg_pool(style_image, ksize=[3,3], strides=[1,1], padding='SAME')
|
172 |
+
#show_n([content_image, style_image], ['Content image', 'Style image'])
|
173 |
+
outputs = hub_module(tf.constant(imageinput), tf.constant(style_input))
|
174 |
+
return outputs[0]
|
175 |
+
'''
|
176 |
+
def perform_style_transfer(content_image, style_image):
|
177 |
+
|
178 |
+
content_image = tf.convert_to_tensor(content_image, np.float32)[tf.newaxis, ...] / 255.
|
179 |
+
style_image = tf.convert_to_tensor(style_image, np.float32)[tf.newaxis, ...] / 255.
|
180 |
+
|
181 |
+
output = hub_module(content_image, style_image)
|
182 |
+
stylized_image = output[0]
|
183 |
+
|
184 |
+
return Image.fromarray(np.uint8(stylized_image[0] * 255))
|
185 |
+
#stylized_image = outputs[0]
|
186 |
+
|
187 |
+
# Visualize input images and the generated stylized image.
|
188 |
+
|
189 |
+
#show_n([content_image, style_image, stylized_image], titles=['Original content image', 'Style image', 'Stylized image'])
|
190 |
+
|
191 |
+
# Gradio app
|
192 |
+
|
193 |
+
#label = gr.outputs.Image(modify(content_image_input, style_image_input))
|
194 |
+
app_interface = gr.Interface(perform_style_transfer,
|
195 |
+
inputs=[content_image_input, style_image_input],
|
196 |
+
outputs = gr.outputs.Image(),
|
197 |
+
title="Fast Neural Style Transfer",
|
198 |
+
description="Gradio demo for Fast Neural Style Transfer using a pretrained Image Stylization model from TensorFlow Hub. To use it, simply upload a content image and style image. To learn more about the project, please find the references listed below.",
|
199 |
+
)
|
200 |
+
app_interface.launch(debug= True)
|
201 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
numpy==1.21.2
|
2 |
+
tensorflow==2.2.0
|
3 |
+
tensorflow_hub==0.12.0
|