luca-martial commited on
Commit
a96c0a1
·
1 Parent(s): b71afd7

new comments

Browse files
Files changed (1) hide show
  1. app.py +11 -2
app.py CHANGED
@@ -5,8 +5,10 @@ import matplotlib.pyplot as plt
5
  import numpy as np
6
  import PIL.Image
7
 
 
8
  hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
9
 
 
10
  def tensor_to_image(tensor):
11
  tensor = tensor*255
12
  tensor = np.array(tensor, dtype=np.uint8)
@@ -15,20 +17,27 @@ def tensor_to_image(tensor):
15
  tensor = tensor[0]
16
  return PIL.Image.fromarray(tensor)
17
 
 
18
  def stylize(content_image, style_image):
 
19
  content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.
20
  style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.
 
21
  stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
22
  return tensor_to_image(stylized_image)
23
-
 
24
  paris =[["example_paris.jpeg"], ["example_vangogh.jpeg"]]
25
  aristotle = [["example_aristotle.jpeg"], ["example_dali.jpeg"]]
26
  avatar = [["example_avatar.jpeg"], ["example_kandinsky.jpeg"]]
 
 
27
  title = "Fast Neural Style Transfer using TF-Hub"
28
- description = "Demo for neural style transfer using the pretrained Arbitrary Image Stylization model from TensorFlow Hub."
29
  content_input = gr.inputs.Image(label="Content Image", source="upload")
30
  style_input = gr.inputs.Image(label="Style Image", source="upload")
31
 
 
32
  iface = gr.Interface(fn=stylize,
33
  inputs=[content_input, style_input],
34
  outputs="image",
 
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
+ # Function to convert tensor to image
12
  def tensor_to_image(tensor):
13
  tensor = tensor*255
14
  tensor = np.array(tensor, dtype=np.uint8)
 
17
  tensor = tensor[0]
18
  return PIL.Image.fromarray(tensor)
19
 
20
+ # Stylize function
21
  def stylize(content_image, style_image):
22
+ # Convert to float32 numpy array, add batch dimension, and normalize to range [0, 1]. Example using numpy:
23
  content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.
24
  style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.
25
+ # Stylize image
26
  stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
27
  return tensor_to_image(stylized_image)
28
+
29
+ # Add image examples for users
30
  paris =[["example_paris.jpeg"], ["example_vangogh.jpeg"]]
31
  aristotle = [["example_aristotle.jpeg"], ["example_dali.jpeg"]]
32
  avatar = [["example_avatar.jpeg"], ["example_kandinsky.jpeg"]]
33
+
34
+ # Customize interface
35
  title = "Fast Neural Style Transfer using TF-Hub"
36
+ description = "\nDemo for neural style transfer using the pretrained Arbitrary Image Stylization model from TensorFlow Hub.\n"
37
  content_input = gr.inputs.Image(label="Content Image", source="upload")
38
  style_input = gr.inputs.Image(label="Style Image", source="upload")
39
 
40
+ # Build and launch
41
  iface = gr.Interface(fn=stylize,
42
  inputs=[content_input, style_input],
43
  outputs="image",