Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import tensorflow as tf
|
5 |
+
import tensorflow_hub as hub
|
6 |
+
|
7 |
+
# Load model from TF-Hub
|
8 |
+
style_transfer_model = hub.load("https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2")
|
9 |
+
|
10 |
+
# function to Stylize the Image or to perform a style transfer
|
11 |
+
def do_style_transfer(content_image, style_image):
|
12 |
+
# Convert to float32 numpy array, add batch dimension, and normalize to range [0, 1]. Example using numpy:
|
13 |
+
content_image = tf.convert_to_tensor(content_image, np.float32)[tf.newaxis, ...] / 255.
|
14 |
+
style_image = tf.convert_to_tensor(style_image, np.float32)[tf.newaxis, ...] / 255.
|
15 |
+
# Stylize image
|
16 |
+
output = style_transfer_model(content_image, style_image)
|
17 |
+
stylized_image = output[0]
|
18 |
+
|
19 |
+
return Image.fromarray(np.uint8(stylized_image[0] * 255))
|
20 |
+
|
21 |
+
|
22 |
+
content_image_input = gr.inputs.Image(label="Content Image")
|
23 |
+
style_image_input = gr.inputs.Image(shape=(256, 256), label="Style Image")
|
24 |
+
|
25 |
+
# Add image examples for users
|
26 |
+
golden_gate = ["golden_gate_bridge.jpeg", "the_great_wave.jpeg"]
|
27 |
+
joshua_tree = ["joshua_tree.jpeg", "starry_night.jpeg"]
|
28 |
+
glacier = ["glacier_national_park.jpeg", "the_scream.jpg"]
|
29 |
+
|
30 |
+
# Customize interface
|
31 |
+
title = "Fast Neural Style Transfer using TF-Hub"
|
32 |
+
description = "Demo for Neural Style Transfer using a pretrained Arbitrary Image Stylization model from TensorFlow Hub. To use it, simply upload a content image and style image, or click one of the examples to load them. To learn more about the project, please find the references listed below."
|
33 |
+
article = "**References**\n\n"
|
34 |
+
"<a href='https://www.tensorflow.org/hub/tutorials/tf2_arbitrary_image_stylization' target='_blank'> Tutorial to implement Fast Neural Style Transfer using the pretrained model from TensorFlow Hub</a> \n"
|
35 |
+
"<p style='text-align: center'><a href='https://arxiv.org/abs/1705.06830'>Exploring the structure of a real-time, arbitrary neural artistic stylization network</a></p>"
|
36 |
+
""
|
37 |
+
content_input = gr.inputs.Image(label="Content Image", source="upload")
|
38 |
+
style_input = gr.inputs.Image(label="Style Image", source="upload")
|
39 |
+
|
40 |
+
app_interface = gr.Interface(fn=do_style_transfer,
|
41 |
+
inputs=[content_image_input, style_image_input],
|
42 |
+
outputs="image",
|
43 |
+
title=title,
|
44 |
+
description=description,
|
45 |
+
examples=[glacier, golden_gate, joshua_tree],
|
46 |
+
article=article
|
47 |
+
)
|
48 |
+
app_interface.launch()
|