如何使用TensorFlow和預訓練模型來串聯資料增強、重新縮放和基礎模型?
TensorFlow和預訓練模型可以透過首先定義輸入大小來串聯資料增強、重新縮放和基礎模型。完成此操作後,透過傳遞相關的輸出呼叫先前定義的函式,即“data_augmentation”、“base_model”。
閱讀更多: 什麼是TensorFlow以及Keras如何與TensorFlow一起建立神經網路?
包含至少一層卷積層的神經網路被稱為卷積神經網路。我們可以使用卷積神經網路來構建學習模型。
我們將瞭解如何藉助來自預訓練網路的遷移學習來對貓和狗的影像進行分類。影像分類遷移學習背後的直覺是,如果一個模型在一個大型且通用的資料集上進行訓練,則該模型可以有效地用作視覺世界的通用模型。它將學習特徵對映,這意味著使用者無需從頭開始訓練大型資料集上的大型模型。
閱讀更多: 如何預訓練定製模型?
我們使用Google Colaboratory執行以下程式碼。Google Colab或Colaboratory幫助在瀏覽器上執行Python程式碼,無需任何配置即可免費訪問GPU(圖形處理單元)。Colaboratory構建在Jupyter Notebook之上。
示例
print("Chaining together data augmentation, rescaling, base_model and feature extractor layers") inputs = tf.keras.Input(shape=(160, 160, 3)) x = data_augmentation(inputs) x = preprocess_input(x) x = base_model(x, training=False) x = global_average_layer(x) x = tf.keras.layers.Dropout(0.2)(x) outputs = prediction_layer(x) model = tf.keras.Model(inputs, outputs)
輸出
Chaining together data augmentation, rescaling, base_model and feature extractor layers
解釋
透過串聯資料增強、重新縮放、base_model和特徵提取層來構建模型。
這是藉助Keras函式式API完成的。
使用training=False是因為模型包含BatchNormalization層。
廣告