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

วิธี Visualize Flower Dataset ด้วย TensorFlow และ Matplotlib

บทนำ

Visualize ข้อมูลภาพช่วยตรวจสอบว่า loading/preprocessing ถูกต้อง ใช้ matplotlib.pyplot.imshow

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

import matplotlib.pyplot as plt
print("Visualizing the flower dataset")
plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
    for i in range(6):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(images[i].numpy().astype("uint8"))
        plt.title(class_names[labels[i]])
        plt.axis("off")

print("Iterating over dataset")
print("Retrieving batches of images")
for image_batch, labels_batch in train_ds:
    print(image_batch.shape)
    print(labels_batch.shape)
    break

ผลลัพธ์

Visualizing the flower dataset
Iterating over dataset
Retrieving batches of images
(32, 180, 180, 3)
(32,)

(พร้อมรูปภาพ 6 รูปแรกพร้อม label)

คำอธิบาย

  • train_ds.take(1): ดึง batch แรก (32 images)
  • images[i].numpy().astype("uint8"): tensor → numpy array → uint8 สำหรับ imshow
  • class_names[labels[i]]: แปลง class index → ชื่อคลาส
  • Batch shape: (32, 180, 180, 3) = (batch, height, width, channels)
  • Labels shape: (32,) = class indices

ข้อสังเกต

ควร visualize ทุกครั้งหลังสร้าง dataset pipeline เพื่อ catch errors เช่น: label ผิด, resize ผิด, color channel order ผิด, normalization ผิด