如何在 TensorFlow 中使用 Dataset.map 建立影像和標籤對的資料集?
透過轉換路徑元件列表,然後將標籤編碼為整數格式來建立 (影像,標籤) 對。‘map’ 方法有助於建立對應於 (影像,標籤) 對的資料集。
閱讀更多: 什麼是 TensorFlow 以及 Keras 如何與 TensorFlow 協作建立神經網路?
我們將使用花卉資料集,其中包含數千朵花的影像。它包含 5 個子目錄,每個類別都有一個子目錄。
我們正在使用 Google Colaboratory 來執行以下程式碼。Google Colab 或 Colaboratory 幫助在瀏覽器上執行 Python 程式碼,無需任何配置即可免費訪問 GPU(圖形處理單元)。Colaboratory 建立在 Jupyter Notebook 之上。
print("The 'num_parallel_calls' is set so that multiple images are loaded and processed in parallel") train_ds = train_ds.map(process_path, num_parallel_calls=AUTOTUNE) val_ds = val_ds.map(process_path, num_parallel_calls=AUTOTUNE) for image, label in train_ds.take(1): print("The shape of image is : ", image.numpy().shape) print("The label is : ", label.numpy())
輸出
The 'num_parallel_calls' is set so that multiple images are loaded and processed in parallel The shape of image is : (180, 180, 3) The label is : 0
程式碼來源:https://www.tensorflow.org/tutorials/load_data/images
解釋
- 多個影像被同時載入和處理。
- ‘map’ 方法用於建立包含 (影像,標籤) 對的資料集。
- 它被迭代,並且形狀的維度和標籤在控制檯上顯示。
廣告