dogeplusplus commited on
Commit
15c5016
·
1 Parent(s): cbb9694

Refactor application to just use the model locally and not need gRPC.

Browse files
Files changed (5) hide show
  1. Makefile +1 -1
  2. streamlit_app.py → app.py +23 -11
  3. client.py +0 -52
  4. requirements.txt +5 -0
  5. setup.py +13 -18
Makefile CHANGED
@@ -7,7 +7,7 @@ model-server-cpu: style
7
  docker run -p 8500:8500 --mount type=bind,source=${CURRENT_DIR}/style,target=/models/style -e MODEL_NAME=style -t tensorflow/serving:2.4.0
8
 
9
  app:
10
- exec streamlit run streamlit_app.py
11
 
12
  webcam:
13
  exec streamlit run webcam_stream.py
 
7
  docker run -p 8500:8500 --mount type=bind,source=${CURRENT_DIR}/style,target=/models/style -e MODEL_NAME=style -t tensorflow/serving:2.4.0
8
 
9
  app:
10
+ exec streamlit run app.py
11
 
12
  webcam:
13
  exec streamlit run webcam_stream.py
streamlit_app.py → app.py RENAMED
@@ -1,21 +1,34 @@
1
  import os
 
 
2
  import streamlit as st
3
  import tensorflow as tf
4
- from tensorflow_serving.apis import prediction_service_pb2_grpc
5
 
6
- from client import style_transfer_serving
7
- import grpc
 
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def main():
11
- options = [
12
- ('grpc.max_send_message_length', 200 * 1024 * 1024),
13
- ('grpc.max_receive_message_length', 200 * 1024 * 1024)
14
- ]
15
- channel = grpc.insecure_channel('localhost:8500', options=options)
16
- stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
17
 
18
  st.title("Neural Style-Transfer App")
 
19
  col1, col2 = st.beta_columns(2)
20
  content_file = st.sidebar.file_uploader('Upload Image', type=['jpg', 'jpeg', 'png'])
21
  style_file = st.sidebar.file_uploader('Upload Style', type=['jpg', 'jpeg', 'png'])
@@ -26,7 +39,6 @@ def main():
26
  show_image = col1.empty()
27
  show_style = col2.empty()
28
 
29
-
30
  style = None
31
  content = None
32
 
@@ -46,7 +58,7 @@ def main():
46
  content_image = tf.io.decode_image(content)
47
  style_image = tf.image.resize(tf.io.decode_image(style), (256, 256))
48
  with st.spinner('Generating style transfer...'):
49
- style_transfer = style_transfer_serving(stub, content_image, style_image)
50
  show_style.image(style_transfer, use_column_width=True)
51
 
52
 
 
1
  import os
2
+ import cv2
3
+ import numpy as np
4
  import streamlit as st
5
  import tensorflow as tf
 
6
 
7
+ @st.cache(suppress_st_warning=True)
8
+ def load_model():
9
+ model = tf.keras.models.load_model("style/1")
10
+ return model
11
 
12
+ @st.cache(suppress_st_warning=True)
13
+ def apply_style_transfer(model, content, style, resize=None):
14
+ content = np.array(content, dtype=np.float32) / 255.
15
+ style = np.array(style, dtype=np.float32) / 255.
16
+
17
+ if resize:
18
+ content = cv2.resize(content, (512, 512))
19
+ style = cv2.resize(style, (512, 512))
20
+
21
+ stylized_image = model(tf.constant(content[np.newaxis, ...]), tf.constant(style[np.newaxis, ...]))
22
+ stylized_image = stylized_image[0] * 255
23
+ stylized_image = np.array(stylized_image, dtype=np.uint8)
24
+ stylized_image = stylized_image
25
+ return stylized_image
26
 
27
  def main():
28
+ model = load_model()
 
 
 
 
 
29
 
30
  st.title("Neural Style-Transfer App")
31
+ st.write("`neural-style` is a pre-trained model from Tensorflow-Hub that allows you to apply styles to images and create pretty art. This app allows you to upload your own content or style images to create some funky effects. We provide some example styles which you can use.")
32
  col1, col2 = st.beta_columns(2)
33
  content_file = st.sidebar.file_uploader('Upload Image', type=['jpg', 'jpeg', 'png'])
34
  style_file = st.sidebar.file_uploader('Upload Style', type=['jpg', 'jpeg', 'png'])
 
39
  show_image = col1.empty()
40
  show_style = col2.empty()
41
 
 
42
  style = None
43
  content = None
44
 
 
58
  content_image = tf.io.decode_image(content)
59
  style_image = tf.image.resize(tf.io.decode_image(style), (256, 256))
60
  with st.spinner('Generating style transfer...'):
61
+ style_transfer = apply_style_transfer(model, content_image, style_image)
62
  show_style.image(style_transfer, use_column_width=True)
63
 
64
 
client.py DELETED
@@ -1,52 +0,0 @@
1
- import cv2
2
- import grpc
3
- import tensorflow as tf
4
- import tensorflow_hub as hub
5
- import numpy as np
6
- from tensorflow_serving.apis import predict_pb2, prediction_service_pb2_grpc
7
-
8
- hub_module = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
9
-
10
- def style_transfer_serving(stub, content, style, resize=None):
11
- content = np.array(content, dtype=np.float32) / 255.
12
- style = np.array(style, dtype=np.float32) / 255.
13
-
14
- if resize:
15
- content = cv2.resize(content, (512, 512))
16
- style = cv2.resize(style, (512, 512))
17
-
18
-
19
- image_proto = tf.make_tensor_proto(content[np.newaxis, ...] / 255.)
20
- style_proto = tf.make_tensor_proto(style[np.newaxis, ...] / 255.)
21
-
22
- stylized_image = hub_module(tf.constant(content[np.newaxis, ...]), tf.constant(style[np.newaxis, ...]))
23
- # request = predict_pb2.PredictRequest()
24
- # request.model_spec.name = 'style'
25
- # request.inputs['placeholder'].CopyFrom(image_proto)
26
- # request.inputs['placeholder_1'].CopyFrom(style_proto)
27
- # resp = stub.Predict(request)
28
- # stylized_image = tf.make_ndarray(resp.outputs['output_0'])[0]
29
- stylized_image = stylized_image[0] * 255
30
- stylized_image = np.array(stylized_image, dtype=np.uint8)
31
- stylized_image = stylized_image
32
- return stylized_image
33
-
34
- if __name__ == "__main__":
35
- options = [
36
- ('grpc.max_send_message_length', 200 * 1024 * 1024),
37
- ('grpc.max_receive_message_length', 200 * 1024 * 1024)
38
- ]
39
- # channel = grpc.insecure_channel('localhost:8500', options=options)
40
- # stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
41
-
42
- file = tf.io.read_file('/home/albert/github/neural-style/assets/template_styles/pebbles.jpg')
43
- style = tf.io.decode_image(file)
44
-
45
- file = tf.io.read_file('/home/albert/Downloads/sam_and_nyx/sam_stairs.jpeg')
46
- content = tf.io.decode_image(file)
47
-
48
- stub = None
49
- result = style_transfer_serving(stub, content, style)
50
- import matplotlib.pyplot as plt
51
- plt.imshow(result[0])
52
- plt.show()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ opencv-python==4.4.0.46
2
+ tensorflow==2.4.1
3
+ streamlit==0.70.0
4
+ numpy==1.19.5
5
+ grpcio==1.32.0
setup.py CHANGED
@@ -1,21 +1,16 @@
1
- from setuptools import setup
2
 
3
- setup(
4
- name='neural-style',
5
- version='',
6
- packages=[],
7
- install_requires=[
8
- 'opencv-python==4.4.0.46',
9
- 'tensorflow==2.4.1',
10
- 'streamlit==0.70.0',
11
- 'numpy==1.19.5',
12
- 'grpcio==1.32.0',
13
- 'tensorflow-serving-api==2.4.1',
14
 
15
- ],
16
- url='',
17
- license='',
18
- author='albert',
19
- author_email='',
20
- description=''
 
 
 
 
21
  )
 
1
+ from setuptools import setup, find_packages
2
 
3
+ with open("requirements.txt") as f:
4
+ requirements = f.readlines()
 
 
 
 
 
 
 
 
 
5
 
6
+ setup(
7
+ name="neural-style",
8
+ version="",
9
+ packages=find_packages(),
10
+ install_requires=requirements,
11
+ url="",
12
+ license="",
13
+ author="albert",
14
+ author_email="",
15
+ description=""
16
  )