如何使用 Tensorflow 和 Python 從磁碟載入花卉資料集和模型?
Tensorflow 可以使用 'image_dataset_from_directory' 方法從磁碟載入花卉資料集和模型。
閱讀更多: 什麼是 TensorFlow 以及 Keras 如何與 TensorFlow 協作建立神經網路?
包含至少一層卷積層的神經網路稱為卷積神經網路。我們可以使用卷積神經網路構建學習模型。
影像分類遷移學習背後的直覺是,如果一個模型在大型通用資料集上進行訓練,則該模型可以有效地用作視覺世界的通用模型。它將學習特徵對映,這意味著使用者無需從頭開始在大型資料集上訓練大型模型。
TensorFlow Hub 是一個包含預訓練 TensorFlow 模型的儲存庫。TensorFlow 可用於微調學習模型。
我們將瞭解如何使用來自 TensorFlow Hub 的模型與 tf.keras,使用來自 TensorFlow Hub 的影像分類模型。完成此操作後,可以執行遷移學習以微調模型以適應自定義影像類別。這是透過使用預訓練的分類器模型來獲取影像並預測它是哪個來完成的。這可以在無需任何訓練的情況下完成。
我們正在使用 Google Colaboratory 執行以下程式碼。Google Colab 或 Colaboratory 幫助透過瀏覽器執行 Python 程式碼,無需任何配置,並且可以免費訪問 GPU(圖形處理單元)。Colaboratory 是建立在 Jupyter Notebook 之上的。
示例
print("The flower dataset") data_root = tf.keras.utils.get_file( 'flower_photos','https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz', untar=True) print("Load data into the model using images off disk with image_dataset_from_directory") batch_size = 32 img_height = 224 img_width = 224 train_ds = tf.keras.preprocessing.image_dataset_from_directory( str(data_root), validation_split=0.2, subset="training", seed=123, image_size=(img_height, img_width), batch_size=batch_size)
程式碼來源 −https://www.tensorflow.org/tutorials/images/transfer_learning_with_hub
輸出
The flower dataset Downloading data from https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz 228818944/228813984 [==============================] - 4s 0us/step Load data into the model using images off disk with image_dataset_from_directory Found 3670 files belonging to 5 classes. Using 2936 files for training.
解釋
- 如果我們需要使用不同的類別訓練模型,則可以使用來自 TFHub 的模型。
- 這將有助於透過重新訓練模型的頂層來訓練自定義影像分類器。
- 這將有助於識別我們資料集中存在的類別。
- 我們將為此使用 iris 資料集。
- 該模型使用磁碟上的影像進行訓練,使用的是 image_dataset_from_directory。
廣告