TensorFlow 2.0學習筆記(2):預訓練影像分類模型使用

前言:

Google Cloud使用筆記(六):使用 Cloud AutoML Vision訓練影像辨識模型(Tensorflow Lite)當中,我和大家講解了如何透過AutoML Vision訓練Model。在本次的文章中,我們將學會如何在Google Colab上,透過Tensor Flow的.pb模型,結合OpenCV,進行影像分類

Image for post
Image for post

下載*.pb模型

首先,請各位讀者到上次訓練的AutoML Vision頁面,依照上圖點擊「測試及使用」、「Container」,並且匯出至Cloud Storage,將其下載回電腦中,並將Model檔名改成「saved_model.pb」。

上傳檔案到Google雲端硬碟

我們首先至Google雲端硬碟首頁

建立以下資料夾:

  1. AutoML_TensorFlow_Colab/image
也就是說在AutoML_TensorFlow_Colab資料夾當中,再建立一個image資料夾

上傳檔案:

  1. 將要預測的圖片上傳到AutoML_TensorFlow_Colab/image

使用Google Colab

開啟以下頁面,由上到下,依照步驟執行,即可完成影像分類

程式碼逐行解釋:

from google.colab import drive # 匯入連結至Google Drive的模組drive.mount('/content/gdrive') # 請點擊網址,選擇 Google 帳號登入,然後將授權碼貼回輸入框中

2.匯入OpenCV、Tensorflow

import cv2from google.colab.patches import cv2_imshowimport pathlib# 使用Tensorflow 1.0來進行推論import tensorflow.compat.v1 as tftf.disable_v2_behavior()

3.進行推論

# 模型的路徑位置(不須包含檔名,資料夾即可)model_path = "/content/gdrive/My Drive/AutoML_TensorFlow_Colab/"# 待預測的圖片路徑位置(不須包含檔名,資料夾即可)image_path = "/content/gdrive/My Drive/AutoML_TensorFlow_Colab/image/"# 建立Tensorflow的Sessionsession = tf.Session(graph=tf.Graph())# 載入*.pb的Modeltf.saved_model.loader.load(session, ['serve'], model_path)
for file in pathlib.Path(image_path).iterdir():
# 取得圖片資料夾路徑
current_image_path = r"{}".format(file.resolve())
# 用2進位方式讀取圖片(Tensorflow需要2進位格式)
img_bytes = open(current_image_path, 'rb').read()
# 將圖片丟給Model進行推論
result = session.run('scores:0', feed_dict={'Placeholder:0': [img_bytes]})
probabilities = result[0] # 透過OpenCV讀取圖片
img = cv2.imread(current_image_path)
if probabilities[0] > probabilities[1]: # 找出陣列當中,預測數值最高者(Python的順序(index)從0開始,所以0是最大的)
print("\n")
print("The Image {} has probability {}".format(file.stem, probabilities[0]))
print("Image {} is Oriolus chinensis".format(file.stem))
print("\n")
# 將圖片調整尺寸到指定大小
new_img = cv2.resize(img, (720, 405))
# 在Colab中顯示圖片
cv2_imshow(new_img)
print("\n")
print("====================================")
else: # 找出陣列當中,預測數值最高者,此為第二個Label數值比較大的情況下 print("\n")
print("The Image {} has probability {}".format(file.stem, probabilities[1]))
print("Image {} is Oriolus tenuirostris".format(file.stem))
print("\n")
# 將圖片調整尺寸到指定大小
new_img = cv2.resize(img, (720, 405))
# 在Colab中顯示圖片
cv2_imshow(new_img)
print("\n")
print("====================================")

輸出內容:

INFO:tensorflow:Saver not created because there are no variables in the graph to restore
INFO:tensorflow:The specified SavedModel has no variables; no checkpoints were restored.


The Image 3 has probability 0.5980759263038635
Image 3 is Oriolus tenuirostris
Image for post
Image for post
====================================================================
The Image 4 has probability 0.8305904269218445
Image 4 is Oriolus chinensis
Image for post
Image for post
====================================================================

The Image 1 has probability 0.9025376439094543
Image 1 is Oriolus chinensis
Image for post
Image for post
====================================================================

The Image 2 has probability 0.8414837121963501
Image 2 is Oriolus chinensis
Image for post
Image for post
====================================================================

The Image 5 has probability 0.927149772644043
Image 5 is Oriolus chinensis
Image for post
Image for post
====================================================================

The Image 6 has probability 0.8794543147087097
Image 6 is Oriolus chinensis
Image for post
Image for post
====================================================================

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store