如何使用Tensorflow和預訓練模型以及Python進行資料評估和預測?


Tensorflow 和預訓練模型可以使用“evaluate”和“predict”方法進行資料評估和預測。首先將輸入影像批次展平。對模型應用 sigmoid 函式,以便它返回 logit 值。

閱讀更多: 什麼是 TensorFlow 以及 Keras 如何與 TensorFlow 一起建立神經網路?

包含至少一層卷積層的神經網路稱為卷積神經網路。我們可以使用卷積神經網路構建學習模型。

我們將瞭解如何藉助來自預訓練網路的遷移學習對貓和狗的影像進行分類。影像分類中遷移學習背後的直覺是,如果一個模型在大型通用資料集上進行訓練,則此模型可以有效地用作視覺世界的通用模型。它將學習特徵圖,這意味著使用者不必從頭開始在大型資料集上訓練大型模型。

閱讀更多: 如何預訓練自定義模型?

我們正在使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助透過瀏覽器執行 Python 程式碼,無需任何配置,並且可以免費訪問 GPU(圖形處理單元)。Colaboratory 是在 Jupyter Notebook 之上構建的。

示例

print("Evaluation and prediction")
loss, accuracy = model.evaluate(test_dataset)
print('Test accuracy is :', accuracy)
print("The batch of image from test set is retrieved")
image_batch, label_batch = test_dataset.as_numpy_iterator().next()
predictions = model.predict_on_batch(image_batch).flatten()
print("The sigmoid function is applied on the model, it returns logits")
predictions = tf.nn.sigmoid(predictions)
predictions = tf.where(predictions < 0.5, 0, 1)
print('Predictions are:\n', predictions.numpy())
print('Labels are:\n', label_batch)

程式碼來源 −https://www.tensorflow.org/tutorials/images/transfer_learning

輸出

Evaluation and prediction
6/6 [==============================] - 3s 516ms/step - loss: 0.0276 - accuracy: 0.9844
Test accuracy is : 0.984375
The batch of image from test set is retrieved
The sigmoid function is applied on the model, it returns logits
Predictions are:
[1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1 1 0 1 0 1]
Labels are:
[1 1 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1 1 0 1 0 1]

解釋

  • 現在可以使用該模型來預測和評估資料。
  • 當影像作為輸入傳遞時,會進行預測。
  • 預測必須是影像是否為狗或貓。

更新於: 2021年2月25日

322 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告