Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การโหลด Flower Dataset และโมเดลจากดิสก์ด้วย TensorFlow


แนวคิด

ใช้ image_dataset_from_directory โหลดข้อมูลภาพจากโฟลเดอร์

แนวคิด Transfer Learning, TensorFlow Hub, Google Colab เหมือนเดิม

ตัวอย่างโค้ด

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)

Credit: TensorFlow Tutorials

ผลลัพธ์

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 retrain เฉพาะเลเยอร์บนสุด
  • ช่วยจดจำคลาสใน dataset ของเรา
  • ใช้ Iris dataset (จริงควรเป็น Flower dataset)
  • โมเดลเทรนด้วยภาพจากดิสก์ผ่าน image_dataset_from_directory