Spaces:
Runtime error
Runtime error
129fc5e32c631b5007cb2cd8d45d416002b42bab
commited on
Commit
Β·
d817532
1
Parent(s):
1914f7a
Initial commit
Browse files- README.md +3 -2
- app.py +52 -0
- example_aristotle.jpeg +0 -0
- example_avatar.jpeg +0 -0
- example_dali.jpeg +0 -0
- example_einstein.jpeg +0 -0
- example_joker.jpeg +0 -0
- example_paris.jpeg +0 -0
- example_polasticot1.jpeg +0 -0
- example_polasticot2.jpeg +0 -0
- example_polasticot3.jpeg +0 -0
- example_vangogh.jpeg +0 -0
- requirements.txt +4 -0
README.md
CHANGED
@@ -1,13 +1,14 @@
|
|
1 |
---
|
2 |
title: Neural Art
|
3 |
emoji: π
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.0.22
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: gpl-2.0
|
11 |
---
|
|
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: Neural Art
|
3 |
emoji: π
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: red
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.0.22
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: gpl-2.0
|
11 |
---
|
12 |
+
Original at https://huggingface.co/spaces/luca-martial/neural-style-transfer, maintained by me I guess since the original doesn't work any more
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import tensorflow_hub as hub
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import numpy as np
|
6 |
+
import PIL.Image
|
7 |
+
|
8 |
+
# Load model from TF-Hub
|
9 |
+
hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
|
10 |
+
|
11 |
+
|
12 |
+
# Function to convert tensor to image
|
13 |
+
def tensor_to_image(tensor):
|
14 |
+
tensor = tensor * 255
|
15 |
+
tensor = np.array(tensor, dtype=np.uint8)
|
16 |
+
if np.ndim(tensor) > 3:
|
17 |
+
assert tensor.shape[0] == 1
|
18 |
+
tensor = tensor[0]
|
19 |
+
return PIL.Image.fromarray(tensor)
|
20 |
+
|
21 |
+
|
22 |
+
# Stylize function
|
23 |
+
def stylize(content_image, style_image):
|
24 |
+
# Convert to float32 numpy array, add batch dimension, and normalize to range [0, 1]. Example using numpy:
|
25 |
+
content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.
|
26 |
+
style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.
|
27 |
+
# Stylize image
|
28 |
+
stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
|
29 |
+
return tensor_to_image(stylized_image)
|
30 |
+
|
31 |
+
|
32 |
+
# Add image examples for users
|
33 |
+
joker = ["example_joker.jpeg", "example_polasticot1.jpeg"]
|
34 |
+
paris = ["example_paris.jpeg", "example_vangogh.jpeg"]
|
35 |
+
einstein = ["example_einstein.jpeg", "example_polasticot2.jpeg"]
|
36 |
+
|
37 |
+
# Customize interface
|
38 |
+
title = "Fast Neural Style Transfer using TF-Hub"
|
39 |
+
description = "Demo for neural style transfer using the pretrained Arbitrary Image Stylization model from TensorFlow Hub."
|
40 |
+
article = "<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><p style='text-align: center'>Check out the <a href='https://huggingface.co/spaces/luca-martial/neural-style-transfer'>original space</a> although that one doesn't work</p>"
|
41 |
+
content_input = gr.inputs.Image(label="Content Image", source="upload")
|
42 |
+
style_input = gr.inputs.Image(label="Style Image", source="upload")
|
43 |
+
|
44 |
+
# Build and launch
|
45 |
+
iface = gr.Interface(fn=stylize,
|
46 |
+
inputs=[content_input, style_input],
|
47 |
+
outputs="image",
|
48 |
+
title=title,
|
49 |
+
description=description,
|
50 |
+
article=article,
|
51 |
+
examples=[joker, paris, einstein])
|
52 |
+
iface.launch()
|
example_aristotle.jpeg
ADDED
![]() |
example_avatar.jpeg
ADDED
![]() |
example_dali.jpeg
ADDED
![]() |
example_einstein.jpeg
ADDED
![]() |
example_joker.jpeg
ADDED
![]() |
example_paris.jpeg
ADDED
![]() |
example_polasticot1.jpeg
ADDED
![]() |
example_polasticot2.jpeg
ADDED
![]() |
example_polasticot3.jpeg
ADDED
![]() |
example_vangogh.jpeg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
matplotlib
|
3 |
+
tensorflow
|
4 |
+
tensorflow_hub
|