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

Cách tải và làm việc với tập flower dataset bằng TensorFlow

Tập flower dataset chứa hàng nghìn ảnh hoa, 5 thư mục con (mỗi thư mục = 1 lớp).

Mã ví dụ

print("Tham số loader")
batch_size = 32
img_height = 180
img_width = 180

print("Tiền xử lý ảnh dataset bằng Keras")
print("Chia 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)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="validation",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)

print("Tên các lớp trong thư mục con")
class_names = train_ds.class_names
print(class_names)

Nguồn mã: TensorFlow Images Tutorial

Đầu ra

Tham số loader
Tiền xử lý ảnh dataset bằng Keras
Chia train/validation
Found 3670 files belonging to 5 classes. Using 2936 files for training.
Chia train/validation
Found 3670 files belonging to 5 classes. Using 734 files for validation.
Tên các lớp trong thư mục con
['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']

Giải thích

  • Định nghĩa tham số: batch_size, img_height, img_width.
  • Dataset chia train/validation (80/20).
  • In ra class_names từ tên thư mục con.