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

TensorFlow: แสดงภาพข้อมูล (Visualize) ด้วย Matplotlib

แสดงภาพจาก flower dataset ที่โหลดผ่าน image_dataset_from_directory ด้วย matplotlib

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

print("แสดงภาพข้อมูล...")
import matplotlib.pyplot as plt

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")

for image_batch, labels_batch in train_ds:
    print("Image batch shape:", image_batch.shape)
    print("Labels batch shape:", labels_batch.shape)
    break

ผลลัพธ์

แสดงภาพข้อมูล...
(32, 180, 180, 3)
(32,)

TensorFlow: แสดงภาพข้อมูล (Visualize) ด้วย Matplotlib

คำอธิบาย

  • train_ds.take(1): ดึง batch แรกเท่านั้น
  • images[i].numpy().astype("uint8"): แปลง tensor เป็น numpy array dtype uint8 สำหรับ imshow
  • class_names[labels[i]]: แสดงชื่อคลาสจาก label index
  • Image batch shape: (32, 180, 180, 3) = 32 รูป, 180x180 pixels, 3 channels (RGB)
  • Labels batch shape: (32,) = 32 labels (integer class indices)