Sa-m commited on
Commit
97f3728
·
verified ·
1 Parent(s): dd953da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -72
app.py CHANGED
@@ -1,82 +1,41 @@
1
  '''NEURAL STYLE TRANSFER '''
2
 
3
- import gradio as gr
4
  import tensorflow as tf
5
  import tensorflow_hub as hub
6
- import PIL
7
  from PIL import Image
8
- import numpy as np
9
- # import time
10
- # import requests
11
- #import cv2
12
-
13
- # !mkdir nstmodel
14
- # !wget -c https://storage.googleapis.com/tfhub-modules/google/magenta/arbitrary-image-stylization-v1-256/2.tar.gz -O - | tar -xz -C /nstmodel
15
- # import tensorflow.keras
16
 
17
- # from PIL import Image, ImageOps
18
- #import requests
19
- #import tarfile
20
-
21
- #MODEL_PATH='Nst_model'
22
-
23
- # Disable scientific notation for clarity
24
  np.set_printoptions(suppress=True)
25
 
26
- # Load model from TF-Hub
27
- model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
28
-
29
- # Load the model
30
- #model = tf.keras.models.load_model(MODEL_PATH)
31
 
32
  def tensor_to_image(tensor):
33
- tensor = tensor*255
34
- tensor = np.array(tensor, dtype=np.uint8)
35
- if np.ndim(tensor)>3:
36
- assert tensor.shape[0] == 1
37
- tensor = tensor[0]
38
- return PIL.Image.fromarray(tensor)
39
-
40
-
41
-
42
- """## Grayscaling image for testing purpose to check if we could get better results.
43
- def gray_scaled(inp_img):
44
- gray = cv2.cvtColor(inp_img, cv2.COLOR_BGR2GRAY)
45
- gray_img = np.zeros_like(inp_img)
46
- gray_img[:,:,0] = gray
47
- gray_img[:,:,1] = gray
48
- gray_img[:,:,2] = gray
49
- return gray_img
50
- """
51
-
52
- ##Transformation
53
- def transform_my_model(content_image,style_image):
54
- # Convert to float32 numpy array, add batch dimension, and normalize to range [0, 1]
55
- #content_image=gray_scaled(content_image)
56
- content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.
57
- style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.
58
-
59
- #Resizing image
60
- #style_image = tf.image.resize(style_image, (256, 256))
61
-
62
- # Stylize image
63
- outputs = model(tf.constant(content_image), tf.constant(style_image))
64
- stylized_image = outputs[0]
65
-
66
- # stylized = tf.image.resize(stylized_image, (356, 356))
67
- stylized_image =tensor_to_image(stylized_image)
68
- return stylized_image
69
-
70
-
71
- image1 = gr.Image(label="Content Image") #CONTENT IMAGE
72
- image2 = gr.Image(label="Style Image") #STYLE IMAGE
73
- stylizedimg=gr.Image(label="Result")
74
- gr.Interface(fn=transform_my_model, inputs= [image1,image2] ,
75
- outputs= stylizedimg,title='Style Transfer',theme='seafoam',
76
- examples=[['Content_Images/contnt12.jpg','VG516.jpg'],['Content_Images/contnt2.jpg','Content_Images/styl9.jpg'],['Content_Images/contnt.jpg','Content_Images/styl22.jpg']],
77
- article="References-\n\nExploring the structure of a real-time, arbitrary neural artistic stylization network. Golnaz Ghiasi, Honglak Lee, Manjunath Kudlur, Vincent Dumoulin.",
78
- share=True
79
- ).launch()
80
-
81
-
82
-
 
1
  '''NEURAL STYLE TRANSFER '''
2
 
3
+ import numpy as np
4
  import tensorflow as tf
5
  import tensorflow_hub as hub
6
+ import gradio as gr
7
  from PIL import Image
 
 
 
 
 
 
 
 
8
 
 
 
 
 
 
 
 
9
  np.set_printoptions(suppress=True)
10
 
11
+ model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
 
 
 
 
12
 
13
  def tensor_to_image(tensor):
14
+ tensor *= 255
15
+ tensor = np.array(tensor, dtype=np.uint8)
16
+ if tensor.ndim > 3:
17
+ tensor = tensor[0]
18
+ return Image.fromarray(tensor)
19
+
20
+ def transform_my_model(content_image, style_image):
21
+ content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.0
22
+ style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.0
23
+ stylized_image = model(tf.constant(content_image), tf.constant(style_image))[0]
24
+ return tensor_to_image(stylized_image)
25
+
26
+ demo = gr.Interface(
27
+ fn=transform_my_model,
28
+ inputs=[gr.Image(label="Content Image"), gr.Image(label="Style Image")],
29
+ outputs=[gr.Image(label="Result")],
30
+ title="Style Transfer",
31
+ theme="seafoam",
32
+ examples=[
33
+ ["Content_Images/contnt12.jpg", "VG516.jpg"],
34
+ ["Content_Images/contnt2.jpg", "Content_Images/styl9.jpg"],
35
+ ["Content_Images/contnt.jpg", "Content_Images/styl22.jpg"]
36
+ ],
37
+ article="References-\n\nExploring the structure of a real-time, arbitrary neural artistic stylization network. Golnaz Ghiasi, Honglak Lee, Manjunath Kudlur, Vincent Dumoulin.",
38
+ share=True
39
+ )
40
+
41
+ demo.launch()