Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
'''NEURAL STYLE TRANSFER '''
|
2 |
|
3 |
import numpy as np
|
@@ -5,34 +46,75 @@ 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 |
|
12 |
def tensor_to_image(tensor):
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
tensor
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def transform_my_model(content_image, style_image):
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
demo = gr.Interface(
|
26 |
fn=transform_my_model,
|
27 |
inputs=[gr.Image(label="Content Image"), gr.Image(label="Style Image")],
|
28 |
outputs=gr.Image(label="Result"),
|
29 |
-
title="Style Transfer",
|
30 |
-
|
31 |
-
|
32 |
-
["Content_Images/contnt2.jpg", "Content_Images/styl9.jpg"],
|
33 |
-
["Content_Images/contnt.jpg", "Content_Images/styl22.jpg"]
|
34 |
-
],
|
35 |
article="References-\n\nExploring the structure of a real-time, arbitrary neural artistic stylization network. Golnaz Ghiasi, Honglak Lee, Manjunath Kudlur, Vincent Dumoulin."
|
36 |
)
|
37 |
|
38 |
-
|
|
|
|
|
|
|
|
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 |
+
# model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
|
11 |
+
|
12 |
+
# def tensor_to_image(tensor):
|
13 |
+
# tensor *= 255
|
14 |
+
# tensor = np.array(tensor, dtype=np.uint8)
|
15 |
+
# if tensor.ndim > 3:
|
16 |
+
# tensor = tensor[0]
|
17 |
+
# return Image.fromarray(tensor)
|
18 |
+
|
19 |
+
# def transform_my_model(content_image, style_image):
|
20 |
+
# content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
21 |
+
# style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
22 |
+
# stylized_image = model(tf.constant(content_image), tf.constant(style_image))[0]
|
23 |
+
# return tensor_to_image(stylized_image)
|
24 |
+
|
25 |
+
# demo = gr.Interface(
|
26 |
+
# fn=transform_my_model,
|
27 |
+
# inputs=[gr.Image(label="Content Image"), gr.Image(label="Style Image")],
|
28 |
+
# outputs=gr.Image(label="Result"),
|
29 |
+
# title="Style Transfer",
|
30 |
+
# examples=[
|
31 |
+
# ["Content_Images/contnt12.jpg", "VG516.jpg"],
|
32 |
+
# ["Content_Images/contnt2.jpg", "Content_Images/styl9.jpg"],
|
33 |
+
# ["Content_Images/contnt.jpg", "Content_Images/styl22.jpg"]
|
34 |
+
# ],
|
35 |
+
# article="References-\n\nExploring the structure of a real-time, arbitrary neural artistic stylization network. Golnaz Ghiasi, Honglak Lee, Manjunath Kudlur, Vincent Dumoulin."
|
36 |
+
# )
|
37 |
+
|
38 |
+
# demo.launch(share=True)
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
'''NEURAL STYLE TRANSFER '''
|
43 |
|
44 |
import numpy as np
|
|
|
46 |
import tensorflow_hub as hub
|
47 |
import gradio as gr
|
48 |
from PIL import Image
|
49 |
+
import os
|
50 |
+
import logging
|
51 |
+
|
52 |
+
# Set up logging
|
53 |
+
logging.basicConfig(level=logging.INFO)
|
54 |
+
logger = logging.getLogger(__name__)
|
55 |
|
56 |
np.set_printoptions(suppress=True)
|
57 |
+
|
58 |
+
# Load model with error handling
|
59 |
+
try:
|
60 |
+
logger.info("Loading TensorFlow Hub model...")
|
61 |
+
model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
|
62 |
+
logger.info("Model loaded successfully!")
|
63 |
+
except Exception as e:
|
64 |
+
logger.error(f"Error loading model: {str(e)}")
|
65 |
+
raise
|
66 |
|
67 |
def tensor_to_image(tensor):
|
68 |
+
try:
|
69 |
+
tensor *= 255
|
70 |
+
tensor = np.array(tensor, dtype=np.uint8)
|
71 |
+
if tensor.ndim > 3:
|
72 |
+
tensor = tensor[0]
|
73 |
+
return Image.fromarray(tensor)
|
74 |
+
except Exception as e:
|
75 |
+
logger.error(f"Error in tensor_to_image: {str(e)}")
|
76 |
+
raise
|
77 |
|
78 |
def transform_my_model(content_image, style_image):
|
79 |
+
try:
|
80 |
+
if content_image is None or style_image is None:
|
81 |
+
raise ValueError("Both content and style images are required")
|
82 |
+
|
83 |
+
logger.info("Processing images...")
|
84 |
+
content_image = content_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
85 |
+
style_image = style_image.astype(np.float32)[np.newaxis, ...] / 255.0
|
86 |
+
stylized_image = model(tf.constant(content_image), tf.constant(style_image))[0]
|
87 |
+
logger.info("Style transfer completed successfully!")
|
88 |
+
return tensor_to_image(stylized_image)
|
89 |
+
except Exception as e:
|
90 |
+
logger.error(f"Error in transform_my_model: {str(e)}")
|
91 |
+
raise
|
92 |
+
|
93 |
+
# Verify example images exist
|
94 |
+
example_images = [
|
95 |
+
["Content_Images/contnt12.jpg", "VG516.jpg"],
|
96 |
+
["Content_Images/contnt2.jpg", "Content_Images/styl9.jpg"],
|
97 |
+
["Content_Images/contnt.jpg", "Content_Images/styl22.jpg"]
|
98 |
+
]
|
99 |
+
|
100 |
+
valid_examples = []
|
101 |
+
for content_path, style_path in example_images:
|
102 |
+
if os.path.exists(content_path) and os.path.exists(style_path):
|
103 |
+
valid_examples.append([content_path, style_path])
|
104 |
+
else:
|
105 |
+
logger.warning(f"Example image not found: {content_path} or {style_path}")
|
106 |
|
107 |
demo = gr.Interface(
|
108 |
fn=transform_my_model,
|
109 |
inputs=[gr.Image(label="Content Image"), gr.Image(label="Style Image")],
|
110 |
outputs=gr.Image(label="Result"),
|
111 |
+
title="Neural Style Transfer",
|
112 |
+
description="Upload a content image and a style image to create a stylized version of your content image.",
|
113 |
+
examples=valid_examples if valid_examples else None,
|
|
|
|
|
|
|
114 |
article="References-\n\nExploring the structure of a real-time, arbitrary neural artistic stylization network. Golnaz Ghiasi, Honglak Lee, Manjunath Kudlur, Vincent Dumoulin."
|
115 |
)
|
116 |
|
117 |
+
if __name__ == "__main__":
|
118 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
119 |
+
|
120 |
+
|