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

Adding checklist of dependencies, and slimmed down code to download style transfer model.

Browse files
Files changed (5) hide show
  1. README.md +10 -0
  2. client.go +19 -0
  3. client.py +0 -15
  4. download_model.py +7 -0
  5. main.py +0 -68
README.md ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Dependencies
2
+ - Tensorflow serving apis
3
+ - Python 3.6+
4
+ - Docker
5
+ - Nvidia-docker
6
+ - tensorflow/serving:latest-gpu image
7
+ - Go
8
+ - gocv
9
+ - tensorflow and tensorflow/serving repositories (for golang protobufs)
10
+ - golang protobuf library
client.go ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
@@ -44,18 +44,3 @@ if __name__ == "__main__":
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')
 
44
  break
45
  video.release()
46
  cv2.destroyAllWindows()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
download_model.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tensorflow as tf
3
+ import tensorflow_hub as hub
4
+ os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
5
+
6
+ hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
7
+ tf.saved_model.save(hub_model, 'style/1')
main.py DELETED
@@ -1,68 +0,0 @@
1
- import os
2
- import tensorflow as tf
3
-
4
- os.environ['TFHUB_MODEL_LOAD_FORMAT'] = 'COMPRESSED'
5
- import matplotlib.pyplot as plt
6
- import numpy as np
7
- import time
8
- import functools
9
- import PIL
10
-
11
-
12
- def tensor_to_image(tensor):
13
- tensor *= 255
14
- tensor = np.array(tensor, dtype=np.uint8)
15
- if np.ndim(tensor) > 3:
16
- assert tensor.shape[0] == 1
17
- tensor = tensor[0]
18
- return PIL.Image.fromarray(tensor)
19
-
20
-
21
- content_path = tf.keras.utils.get_file('YellowLabradorLooking_new.jpg',
22
- 'https://storage.googleapis.com/download.tensorflow.org/example_images/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
-
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)
35
- scale = max_dim / long_dim
36
-
37
- new_shape = tf.cast(shape * scale, tf.int32)
38
-
39
- img = tf.image.resize(img, new_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)
47
-
48
- plt.imshow(image)
49
- if title:
50
- plt.title(title)
51
-
52
-
53
- content_image = load_img(content_path)
54
- style_image = load_img(style_path)
55
-
56
- plt.subplot(1, 2, 1)
57
- imshow(content_image, 'Content Image')
58
- plt.subplot(1, 2, 2)
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')