dogeplusplus commited on
Commit
c71df2a
·
1 Parent(s): df7fd14

Converting to tensorflow serving gpu container.

Browse files
Files changed (3) hide show
  1. Makefile +1 -1
  2. client.py +61 -25
  3. main.py +5 -2
Makefile CHANGED
@@ -1,2 +1,2 @@
1
  start: style
2
- docker run -p 8500:8500 --mount type=bind,source=C:/Users/doge/github/neural-style/style,target=/models/style -e MODEL_NAME=style -t tensorflow/serving
 
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
client.py CHANGED
@@ -1,25 +1,61 @@
1
- import grpc
2
- import tensorflow as tf
3
- import numpy as np
4
- from tensorflow_serving.apis import predict_pb2, prediction_service_pb2_grpc
5
-
6
- if __name__ == "__main__":
7
- options = [('grpc.max_message_length', 100 * 1024 * 1024)]
8
- channel = grpc.insecure_channel('localhost:8500', options=options)
9
- stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
10
- request = predict_pb2.PredictRequest()
11
-
12
- file = tf.io.read_file('C:\\Users\\doge\\Downloads\\sam.jpg')
13
- image = tf.io.decode_image(file)
14
-
15
- request.model_spec.name = 'style'
16
- request.model_spec.signature_name = 'serving_default'
17
- image_proto = tf.make_tensor_proto(np.array(image, dtype=np.float32)[np.newaxis, ...])
18
- request.inputs['placeholder'].CopyFrom(image_proto)
19
- request.inputs['placeholder_1'].CopyFrom(image_proto)
20
- resp = stub.Predict(request)
21
- print(resp)
22
-
23
-
24
-
25
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = [
13
+ ('grpc.max_send_message_length', 200 * 1024 * 1024),
14
+ ('grpc.max_receive_message_length', 200 * 1024 * 1024)
15
+ ]
16
+ channel = grpc.insecure_channel('localhost:8500', options=options)
17
+ stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
18
+ request = predict_pb2.PredictRequest()
19
+
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()
47
+
48
+ # styled_plot = plt.imshow(styled_image)
49
+ # plt.axis('off')
50
+ # plt.show()
51
+ #
52
+ # IMAGES_PATH = '/home/albert/Downloads/sam'
53
+ # for path in os.listdir(IMAGES_PATH):
54
+ # start = time.time()
55
+ # file = tf.io.read_file(os.path.join(IMAGES_PATH, path))
56
+ # image = tf.io.decode_image(file)
57
+ # output = style_transfer(stub, image)
58
+ # styled_plot.set_data(output)
59
+ # plt.draw()
60
+ # end = time.time()
61
+ # print(f'Time taken to predict: {end - start}s')
main.py CHANGED
@@ -23,11 +23,12 @@ content_path = tf.keras.utils.get_file('YellowLabradorLooking_new.jpg',
23
  style_path = tf.keras.utils.get_file('kandinsky5.jpg',
24
  'https://storage.googleapis.com/download.tensorflow.org/example_images/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg')
25
 
 
26
  def load_img(path_to_img):
27
  max_dim = 512
28
  img = tf.io.read_file(path_to_img)
29
  img = tf.image.decode_image(img, channels=3)
30
- img = tf.image.convert_image_dtype(img ,tf.float32)
31
 
32
  shape = tf.cast(tf.shape(img)[:-1], tf.float32)
33
  long_dim = max(shape)
@@ -39,6 +40,7 @@ def load_img(path_to_img):
39
  img = img[tf.newaxis, ...]
40
  return img
41
 
 
42
  def imshow(image, title=None):
43
  if np.ndim(image) > 3:
44
  image = tf.squeeze(image, axis=0)
@@ -47,6 +49,7 @@ def imshow(image, title=None):
47
  if title:
48
  plt.title(title)
49
 
 
50
  content_image = load_img(content_path)
51
  style_image = load_img(style_path)
52
 
@@ -56,10 +59,10 @@ plt.subplot(1, 2, 2)
56
  imshow(style_image, 'Style Image')
57
 
58
  import tensorflow_hub as hub
 
59
  hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
60
  stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
61
  tensor_to_image(stylized_image)
62
  print(type(hub_model))
63
 
64
-
65
  tf.saved_model.save(hub_model, 'style')
 
23
  style_path = tf.keras.utils.get_file('kandinsky5.jpg',
24
  'https://storage.googleapis.com/download.tensorflow.org/example_images/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg')
25
 
26
+
27
  def load_img(path_to_img):
28
  max_dim = 512
29
  img = tf.io.read_file(path_to_img)
30
  img = tf.image.decode_image(img, channels=3)
31
+ img = tf.image.convert_image_dtype(img, tf.float32)
32
 
33
  shape = tf.cast(tf.shape(img)[:-1], tf.float32)
34
  long_dim = max(shape)
 
40
  img = img[tf.newaxis, ...]
41
  return img
42
 
43
+
44
  def imshow(image, title=None):
45
  if np.ndim(image) > 3:
46
  image = tf.squeeze(image, axis=0)
 
49
  if title:
50
  plt.title(title)
51
 
52
+
53
  content_image = load_img(content_path)
54
  style_image = load_img(style_path)
55
 
 
59
  imshow(style_image, 'Style Image')
60
 
61
  import tensorflow_hub as hub
62
+
63
  hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
64
  stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
65
  tensor_to_image(stylized_image)
66
  print(type(hub_model))
67
 
 
68
  tf.saved_model.save(hub_model, 'style')