2020年12月30日 星期三

[Docker]Tensorflow-serving

  1. # CPU
  2. # Download the TensorFlow Serving Docker image and repo
  3. docker pull tensorflow/serving:latest-gpu
  1. # GPU
  1. # 建立tf資料夾
  2. mkdir -p $(pwd)/tf
  3. TESTDATA="$(pwd)/tf"
  4.  
  5. ### CPU ###
  6. docker run -t --rm -p 8501:8501 \
  7. --name tf_serving \
  8. -v "$TESTDATA:/models/fashion_model" \
  9. -e MODEL_NAME=fashion_model \
  10. tensorflow/serving &
  11.  
  12. ### GPU ###
  13. docker run -t --runtime=nvidia -p 8501:8501 \
  14. --name tf_serving_gpu \
  15. -v "$TESTDATA:/models/fashion_model" \
  16. -e MODEL_NAME=fashion_model \
  17. tensorflow/serving:latest-gpu &
  1. # Query the model using the predict API
  2. curl -d '{"instances": [1.0, 2.0, 5.0]}' \
  3. -X POST http://localhost:8501/v1/models/half_plus_two:predict
  1. import tensorflow as tf
  2. from tensorflow import keras
  3.  
  4. # Helper libraries
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. import json
  8.  
  9. import requests
  10.  
  11. fashion_mnist = keras.datasets.fashion_mnist
  12. (_, _), (test_images, test_labels) = fashion_mnist.load_data()
  13.  
  14. test_images = test_images.reshape(test_images.shape[0], 28, 28, 1)
  15.  
  16. data = json.dumps({"signature_name": "serving_default", "instances": test_images[0:3].tolist()})
  17.  
  18. headers = {"content-type": "application/json"}
  19. json_response = requests.post('http://10.15.11.75:8501/v1/models/fashion_model:predict', data=data, headers=headers)
  20. print(json_response)
  21.  
  22. predictions = json.loads(json_response.text)['predictions']
  23. print(np.argmax(predictions[0]))

沒有留言:

張貼留言