บทนำ
tf.data.Dataset.map() ใช้แปลงแต่ละ element ของ dataset (เช่น file path → (image tensor, label integer)) แบบ parallel
โค้ดตัวอย่าง
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
คำอธิบาย
process_path: function ที่รับ file path คืน (image_tensor, label_int) (ดูบทความก่อนหน้า)num_parallel_calls=AUTOTUNE: ให้ TF 決定จำนวน parallel calls อัตโนมัติ (ใช้ CPU cores ทั้งหมด)take(1): ดูตัวอย่าง 1 batch เพื่อตรวจสอบ shape/label- Image shape: (180, 180, 3) = resize จากโค้ดก่อนหน้า | Label: 0 = class index (daisy)
ประโยชน์ของ map + parallel
- ใช้ CPU cores ทั้งหมด decode/resize images พร้อมกัน
- ไม่ block GPU รอ data
- ส่วนสำคัญของ high-performance input pipeline