Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,41 @@
|
|
| 1 |
'''NEURAL STYLE TRANSFER '''
|
| 2 |
|
| 3 |
-
import
|
| 4 |
import tensorflow as tf
|
| 5 |
import tensorflow_hub as hub
|
| 6 |
-
import
|
| 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 |
-
|
| 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 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|