如何使用Python中的Tensorflow新增批處理維度並將影像傳遞給模型?
Tensorflow可以透過將影像轉換為Numpy陣列來新增批處理維度並將影像傳遞給模型。
閱讀更多: 什麼是TensorFlow以及Keras如何與TensorFlow一起建立神經網路?
包含至少一層卷積層的神經網路被稱為卷積神經網路。我們可以使用卷積神經網路來構建學習模型。
我們使用Google Colaboratory執行以下程式碼。Google Colab或Colaboratory幫助在瀏覽器上執行Python程式碼,無需任何配置,並且可以免費訪問GPU(圖形處理單元)。Colaboratory構建在Jupyter Notebook之上。
影像分類遷移學習背後的直覺是,如果一個模型在一個大型且通用的資料集上進行了訓練,那麼這個模型可以有效地作為一個通用的視覺世界模型。它已經學習了特徵對映,這意味著使用者不必從頭開始在一個大型資料集上訓練一個大型模型。
TensorFlow Hub是一個包含預訓練TensorFlow模型的儲存庫。TensorFlow可用於微調學習模型。
我們將瞭解如何使用TensorFlow Hub中的模型與tf.keras,使用TensorFlow Hub中的影像分類模型。完成後,可以執行遷移學習來微調針對自定義影像類別的模型。這是透過使用預訓練的分類器模型來獲取影像並預測它是做什麼的。這可以在無需任何訓練的情況下完成。
示例
grace_hopper = np.array(grace_hopper)/255.0
print("The dimensions of the image are")
print(grace_hopper.shape)
result = classifier.predict(grace_hopper[np.newaxis, ...])
print("The dimensions of the resultant image are")
print(result.shape)
predicted_class = np.argmax(result[0], axis=-1)
print("The predicted class is")
print(predicted_class)程式碼來源 −https://www.tensorflow.org/tutorials/images/transfer_learning_with_hub
輸出
The dimensions of the image are (224, 224, 3) The dimensions of the resultant image are (1, 1001) The predicted class is 819
解釋
- 添加了批處理維度。
- 影像被傳遞給模型。
- 結果是一個包含1001個元素的logits向量。
- 這將對影像的每個類別的機率進行評分。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP