記錄Tensorflow 搭建過程,基於Docker
MNIST 模型計算
Tensorflow Serveing 搭建過程,基於Docker
模型測試
Tensorflow-Serveing gRPC 遠端預測呼叫
Tensorflow -Serveing RESTful 遠端預測呼叫
Tensorflow 基於Docker 搭建過程版本使用說明:
docker 19.03.4
Tensorflow:1.12.3-py3 使用映象:tensorflow/tensorflow:1.12.3-py3
映象包括:
- Jupyter Notebook- TensorFlow- scikit-learn- pandas- matplotlib- numpy- scipy- Pillow- Python3
安裝Docker環境啟動 啟動tensorflowdocker run --name notebooks -d -v /home/notebooks:/notebooks -v /home/logs:/logs -p 8888:8888 tensorflow/tensorflow:1.12.3-py3 /run_jupyter.sh --allow-root --NotebookApp.token='abc123'啟動TensorBoarddocker run --name board -d -v /home/logs:/logs -p 6006:6006 tensorflow/tensorflow:1.12.3-py3 tensorboard --logdir /logs 開啟:http://<ip>:8888 和 http://<ip>:6006 開啟jupyter 和 board
MNIST模型訓練並生成model上傳mnist 對應的程式碼。避免下載訓練資料失敗, 這裡一併上傳。進入notebook的容器, 執行訓練資料
python /notebooks/mnist/source/mnist_saved_model.py --training_iteration=5000 --model_version=1 --work_dir=/notebooks/mnist/data /notebooks/model
3. 訓練完後 , 資料被儲存在了/notebooks/model中對應宿主機的/home/notebooks/model中
啟動tensorflow-serveing docker 並指定model位置docker run --rm -p 8501:8501 -p 8500:8500 \-v "/home/notebooks/model:/models/mnist" \-e MODEL_NAME=mnist -t tensorflow/serving &
說明:8500 gRPC使用埠, 8501 RESTful使用埠
mnist 模型名稱
模型測試啟動後,使用mnist_client估算模型(因為需要tensorflow環境,這裡直接進入notebook的容器執行)
python ./mnist/source/mnist_client.py --server=172.17.0.3:8500 --work_dir=./mnist/data
執行中會提示缺少tensorflow_serving的模組。
因為我們使用的是1.12.3的tensorflow的版本,這邊需要使用pip下載該模組
pip install 'tensorflow-serving-api~=1.12.3'
到此tensorflow-serveing 已經啟動,相關內容可以參考:https://www.tensorflow.org/tfx/serving/api_rest
http://<ip>:8501/v1/models/mnist/metadata 檢視metadata定義
外部訪問tensorflow-serveing gRPC方式識別參考“TensorFlow 建立過程(單機版)” 可以建立單機版本的Tensorflow+notebook 的環境, 並透過該環境執行py檔案對圖片進行識別。 conda 啟動tensorflow12 的環境, 啟動jupyter
以下為呼叫的方法
import timeimport numpy as npimport grpcfrom tensorflow.contrib.util import make_tensor_protofrom tensorflow.contrib import util as contrib_utilfrom tensorflow_serving.apis import predict_pb2from tensorflow_serving.apis import prediction_service_pb2_grpcimport imageioimport matplotlib.pyplot as pltfrom PIL import Imagedef run(host, port, image, model, signature_name): channel = grpc.insecure_channel('{host}:{port}'.format(host=host, port=port)) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) # Read an image data = imageio.imread(image) data = data.astype(np.float32) #data = imageprepare(image) data1 = Image.open(image); plt.imshow(data1) plt.axis('off') # 不顯示座標軸 plt.show() #print(data) start = time.time() # Call classification model to make prediction on the image request = predict_pb2.PredictRequest() request.model_spec.name = model request.model_spec.signature_name = signature_name #mtp = make_tensor_proto(data, shape=[1, 784]) mtp = contrib_util.make_tensor_proto(data, shape=[1, 784]) request.inputs['images'].CopyFrom(mtp) #print(request) result = stub.Predict(request, 10.0) end = time.time() time_diff = end - start print('耗時: {}'.format(time_diff)) response = np.array(result.outputs['scores'].float_val) prediction = np.argmax(response) print('{}{}'.format('識別為:',prediction))
run("yun.mydomain.top", 8500, "./data/1.png", "mnist", "predict_images")
run("yun.mydomain.top", 8500, "./data/2.png", "mnist", "predict_images")
run("yun.mydomain.top", 8500, "./data/3.png", "mnist", "predict_images")
run("yun.mydomain.top", 8500, "./data/4.png", "mnist", "predict_images")
run("yun.mydomain.top", 8500, "./data/5.png", "mnist", "predict_images")
run("yun.mydomain.top", 8500, "./data/6.png", "mnist", "predict_images")
run("yun.mydomain.top", 8500, "./data/7.png", "mnist", "predict_images")
run("yun.mydomain.top", 8500, "./data/8.png", "mnist", "predict_images")
run("yun.mydomain.top", 8500, "./data/9.png", "mnist", "predict_images")
外部透過RESTful API的方式呼叫
以下透過python的方法來使用,因為各種語言均可使用RESTful的呼叫。 則Python透過其他也可以一樣透過。
import matplotlib.pyplot as pltimport imageioimport numpy as npfrom PIL import Imageimport requestsimport jsonimport timedef run(host, port, image_file, model, signature_name): image = Image.open(image_file) #resized_image = image.resize((28, 28), Image.ANTIALIAS) data = np.array(Image.open(image_file).convert('L').resize((28, 28))).astype(np.float).reshape(-1, 28, 28, 1) #data =resized_image# imageio.imread(resized_image) plt.imshow(image) plt.axis('off') # 不顯示座標軸 plt.show() data = data.astype(np.float32) data = data.flatten() #變成1維陣列 t= np.array2string(data, separator=',', formatter={'float':lambda x: "%f" % x}) json_request = '{{"signature_name": "{}","inputs":{{"images":[{}] }}}}'.format(signature_name,t) start = time.time() resp = requests.post('http://{}:{}/v1/models/{}:predict'.format(host,port,model), data=json_request) res = json.loads(resp.content) end = time.time() time_diff = end - start print('耗時: {}'.format(time_diff)) response = np.array(res['outputs']) prediction = np.argmax(response) print('{}{}'.format('識別為:',prediction))
run("yun.mydomain.top", 8501, "./data/1.png", "mnist", "predict_images")
run("yun.mydomain.top", 8501, "./data/2.png", "mnist", "predict_images")
run("yun.mydomain.top", 8501, "./data/3.png", "mnist", "predict_images")
run("yun.mydomain.top", 8501, "./data/4.png", "mnist", "predict_images")
run("yun.mydomain.top", 8501, "./data/5.png", "mnist", "predict_images")
run("yun.mydomain.top", 8501, "./data/6.png", "mnist", "predict_images")
run("yun.mydomain.top", 8501, "./data/7.png", "mnist", "predict_images")
run("yun.mydomain.top", 8501, "./data/8.png", "mnist", "predict_images")
run("yun.zhucl1006.top", 8501, "./data/9.png", "mnist", "predict_images")