dogeplusplus commited on
Commit
daf7bc7
·
1 Parent(s): a1ded73

Streamlit web app with example images.

Browse files
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ serving
2
+ tensorflow
Makefile CHANGED
@@ -1,2 +1,5 @@
1
- start: style
2
  docker run --gpus all -p 8500:8500 --mount type=bind,source=/home/albert/github/neural-style/style,target=/models/style -e MODEL_NAME=style -t tensorflow/serving:latest-gpu
 
 
 
 
1
+ model-server: style
2
  docker run --gpus all -p 8500:8500 --mount type=bind,source=/home/albert/github/neural-style/style,target=/models/style -e MODEL_NAME=style -t tensorflow/serving:latest-gpu
3
+
4
+ app:
5
+ exec streamlit run streamlit_app.py
README.md CHANGED
@@ -7,4 +7,5 @@
7
  - Go
8
  - gocv
9
  - tensorflow and tensorflow/serving repositories (for golang protobufs)
10
- - golang protobuf library
 
 
7
  - Go
8
  - gocv
9
  - tensorflow and tensorflow/serving repositories (for golang protobufs)
10
+ - golang protobuf library
11
+ - protobuf compiler
client.go DELETED
@@ -1,19 +0,0 @@
1
- package main
2
-
3
- import (
4
- "flag"
5
- "fmt"
6
- "gocv.io/x/gocv"
7
- pb "tensorflow_serving/apis"
8
- )
9
-
10
- func main() {
11
- request := &pb.PredictRequest{}
12
- webcam, _ := gocv.VideoCaptureDevice(0)
13
- window := gocv.NewWindow("hello")
14
- for {
15
- webcam.Read(&img)
16
- window.IMShow(img)
17
- window.WaitKey(1)
18
- }
19
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
client.py CHANGED
@@ -1,12 +1,28 @@
1
- import os
2
  import cv2
3
  import grpc
4
  import tensorflow as tf
5
  import numpy as np
6
- import matplotlib.pyplot as plt
7
  from tensorflow_serving.apis import predict_pb2, prediction_service_pb2_grpc
8
- import time
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  if __name__ == "__main__":
12
  options = [
@@ -20,27 +36,7 @@ if __name__ == "__main__":
20
  file = tf.io.read_file('/home/albert/Downloads/pebbles.jpg')
21
  style = tf.io.decode_image(file)
22
 
23
- style_image = cv2.resize(np.array(style, dtype=np.float32), (64, 64))[np.newaxis, ...] / 255.
24
- style_proto = tf.make_tensor_proto(np.array(style, dtype=np.float32)[np.newaxis, ...] / 255.)
25
-
26
- def style_transfer(stub, image):
27
- request.model_spec.name = 'style'
28
- request.model_spec.signature_name = 'serving_default'
29
- image = cv2.resize(np.array(image, dtype=np.float32), (512, 512))
30
- image_proto = tf.make_tensor_proto(image[np.newaxis, ...] / 255.)
31
-
32
- request.inputs['placeholder'].CopyFrom(image_proto)
33
- request.inputs['placeholder_1'].CopyFrom(style_proto)
34
- resp = stub.Predict(request)
35
- stylized_image = tf.make_ndarray(resp.outputs['output_0'])[0]
36
- return stylized_image
37
-
38
- video = cv2.VideoCapture('/home/albert/Downloads/cat_yelling.mp4')
39
- while video.isOpened():
40
- ret, frame = video.read()
41
- styled_image = style_transfer(stub, frame)
42
- cv2.imshow('cheese', styled_image)
43
- if cv2.waitKey(1) & 0xFF == ord('q'):
44
- break
45
- video.release()
46
- cv2.destroyAllWindows()
 
 
1
  import cv2
2
  import grpc
3
  import tensorflow as tf
4
  import numpy as np
 
5
  from tensorflow_serving.apis import predict_pb2, prediction_service_pb2_grpc
 
6
 
7
+ def style_transfer_serving(stub, content, style, resize=None):
8
+ content = np.array(content, dtype=np.float32)
9
+ style = np.array(style, dtype=np.float32)
10
+
11
+ if resize:
12
+ content = cv2.resize(content, (512, 512))
13
+ style = cv2.resize(style, (512, 512))
14
+
15
+ image_proto = tf.make_tensor_proto(content[np.newaxis, ...] / 255.)
16
+ style_proto = tf.make_tensor_proto(style[np.newaxis, ...] / 255.)
17
+
18
+ request = predict_pb2.PredictRequest()
19
+ request.model_spec.name = 'style'
20
+ request.model_spec.signature_name = 'serving_default'
21
+ request.inputs['placeholder'].CopyFrom(image_proto)
22
+ request.inputs['placeholder_1'].CopyFrom(style_proto)
23
+ resp = stub.Predict(request)
24
+ stylized_image = tf.make_ndarray(resp.outputs['output_0'])[0]
25
+ return stylized_image
26
 
27
  if __name__ == "__main__":
28
  options = [
 
36
  file = tf.io.read_file('/home/albert/Downloads/pebbles.jpg')
37
  style = tf.io.decode_image(file)
38
 
39
+ file = tf.io.read_file('/home/albert/Downloads/sam_and_nyx/sam_faces/sam_kitchen.jpg')
40
+ content = tf.io.decode_image(file)
41
+
42
+ style_transfer_serving(stub, content, style)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
streamlit_app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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'])
22
+ style_options = st.sidebar.selectbox(label='Example Styles', options=os.listdir('template_styles'))
23
+ col1.subheader('Content Image')
24
+ col2.subheader('Style Image')
25
+ show_image = col1.empty()
26
+ show_style = col2.empty()
27
+
28
+ st.subheader('Style Transfer')
29
+ show_transfer = st.empty()
30
+
31
+ style = None
32
+ content = None
33
+
34
+ if content_file:
35
+ content = content_file.getvalue()
36
+ show_image.image(content, use_column_width=True)
37
+
38
+ if style_file:
39
+ style = style_file.getvalue()
40
+ show_style.image(style, use_column_width=True)
41
+ elif style_options is not None:
42
+ with open(os.path.join('template_styles', style_options), 'rb') as f:
43
+ style = f.read()
44
+ show_style.image(style, use_column_width=True)
45
+
46
+ if content is not None and style is not None:
47
+ content_image = tf.io.decode_image(content)
48
+ style_image = tf.image.resize(tf.io.decode_image(style), (256, 256))
49
+ with st.spinner('Generating style transfer...'):
50
+ style_transfer = style_transfer_serving(stub, content_image, style_image)
51
+ show_transfer.image(style_transfer, use_column_width=True)
52
+
53
+
54
+ if __name__ == "__main__":
55
+ main()
template_styles/cogs.jpg ADDED
template_styles/cow.jpg ADDED
template_styles/crystals.jpg ADDED
template_styles/eyess.jpg ADDED
template_styles/fire.jpg ADDED
template_styles/gems.jpg ADDED
template_styles/ice.jpg ADDED
template_styles/leaves.jpeg ADDED
template_styles/leaves.png ADDED
template_styles/oranges.jpg ADDED
template_styles/pattern.jpeg ADDED
template_styles/pebbles.jpg ADDED
template_styles/penrose.jpg ADDED
template_styles/polka.jpeg ADDED
template_styles/pumpkin.jpg ADDED
template_styles/rainbow.png ADDED
template_styles/rainbow_squares.jpg ADDED
template_styles/rocks.jpeg ADDED
template_styles/screm.jpg ADDED
template_styles/spaceship.jpg ADDED
template_styles/squares.png ADDED
template_styles/swirl.jpg ADDED
template_styles/technology.jpg ADDED
template_styles/tiles.jpg ADDED
template_styles/trees.jpg ADDED
template_styles/zigzag.jpg ADDED
webcam_stream.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from threading import Thread
5
+
6
+
7
+ class WebcamVideoStream:
8
+ def __init__(self, src=0):
9
+ self.stream = cv2.VideoCapture(src)
10
+ self.grabbed, self.frame = self.stream.read()
11
+ self.stopped = False
12
+
13
+ def start(self):
14
+ Thread(target=self.update, args=()).start()
15
+ return self
16
+
17
+ def update(self):
18
+ while True:
19
+ if self.stopped:
20
+ return
21
+
22
+ self.grabbed, self.frame = self.stream.read()
23
+
24
+ def read(self):
25
+ return self.frame
26
+
27
+ def stop(self):
28
+ self.stopped = True
29
+
30
+
31
+ def style_transfer_direct(image, model, resize=None):
32
+ if resize:
33
+ image = cv2.resize(np.array(image, dtype=np.float32), (512, 512))
34
+ else:
35
+ image = np.array(image, dtype=np.float32)
36
+ resp = model(tf.convert_to_tensor(image[np.newaxis, ...] / 255.), tf.convert_to_tensor(style_image))
37
+ stylized_image = resp[0]
38
+ return stylized_image.numpy()[0]
39
+
40
+
41
+ def main():
42
+ model = tf.saved_model.load('style/1')
43
+ video_capture = WebcamVideoStream(0)
44
+ video_capture.start()
45
+ while True:
46
+ content_image = video_capture.read()
47
+ transfer = style_transfer_direct(content_image, model)
48
+ cv2.imshow('style camera', transfer)
49
+ if cv2.waitKey(1) & 0xFF == ord('q'):
50
+ break
51
+ cv2.destroyAllWindows()
52
+ video_capture.stop()
53
+
54
+
55
+ if __name__ == "__main__":
56
+ main()