Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Chia dataset hoa thành train/validation TensorFlow Python

Tách dataset hoa thành tập huấn luyện (training) và tập kiểm định (validation) với tỷ lệ 80/20.

Mã ví dụ

batch_size = 32
img_height = 180
img_width = 180

print("Chia dữ liệu train/validation...")
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="training",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size
)

Kết quả

Chia dữ liệu train/validation
Found 3670 files belonging to 5 classes.
Using 2936 files for training.

Giải thích

  • Tổng 3670 ảnh, 5 class (daisy, dandelion, roses, sunflowers, tulips).
  • Training: 2936 ảnh (80%). Validation: 734 ảnh (20%).
  • image_dataset_from_directory tự động: đọc file, decode, resize, batch, shuffle (training).
  • Trả về tf.data.Dataset hiệu quả cho training.
  • Cùng seed đảm bảo train/val không overlap.