File size: 1,579 Bytes
040810d
 
 
46f4974
e3910d5
5342120
040810d
8b5a1e9
720f8b7
8e7e8df
 
 
 
 
 
 
720f8b7
46f4974
44a5020
 
5342120
8e7e8df
1f79383
8949e37
 
 
1f79383
 
a61ec70
 
1f79383
 
561e110
1f79383
 
 
d38a6c6
040810d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import gradio as gr
import tensorflow as tf
import tensorflow_hub as hub
import matplotlib.pyplot as plt
import numpy as np
import PIL.Image

hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2') 

def tensor_to_image(tensor):
    tensor = tensor*255
    tensor = np.array(tensor, dtype=np.uint8)
    if np.ndim(tensor)>3:
      assert tensor.shape[0] == 1
      tensor = tensor[0]
    return PIL.Image.fromarray(tensor)

def stylize(content_image, style_image):
    content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.
    style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.
    stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
    return tensor_to_image(stylized_image)
    
paris =[["example_paris.jpeg"], ["example_vangogh.jpeg"]]
aristotle = [["example_aristotle.jpeg"], ["example_dali.jpeg"]]
avatar = [["example_avatar.jpeg"], ["example_kandinsky.jpeg"]]
title = "Fast Neural Style Transfer using TF-Hub"
description = "Demo for neural style transfer using the pretrained Arbitrary Image Stylization model from TensorFlow Hub."
content_input = gr.inputs.Image(label="Content Image", source="upload")
style_input = gr.inputs.Image(label="Style Image", source="upload")

iface = gr.Interface(fn=stylize, 
                     inputs=[content_input, style_input], 
                     outputs="image",
                     title=title,
                     description=description,
                     examples=[content_examples, style_examples])
iface.launch()