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

วิธีตรวจสอบข้อมูล CIFAR ด้วย TensorFlow และ Python

บทนำ

หลังจากโหลดชุดข้อมูล CIFAR-10 แล้ว ขั้นตอนสำคัญคือการ ตรวจสอบข้อมูล (verify) โดยการพล็อตรูปภาพตัวอย่างเพื่อดูว่าโหลดถูกต้องและไลเบลตรงกัน

CIFAR labels เก็บเป็นอาร์เรย์ (array) จึงต้องใช้ดัชนีเพิ่มเติมเพื่อดึงชื่อคลาส

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

print("Verifying the data")
plt.figure(figsize=(10,10))
print("Plot the first 15 images")
print("An extra index is needed since CIFAR labels are arrays")
for i in range(15):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i][0]])
plt.show()

ผลลัพธ์

Verifying the data
Plot the first 15 images
An extra index is needed since CIFAR labels are arrays

(จะแสดงกรอบ 5×5 รูปภาพ 15 รูปแรกพร้อมชื่อคลาส)

คำอธิบาย

  • ใช้ matplotlib.pyplot.imshow แสดงรูปภาพที่ผ่านการ normalize แล้ว (ค่า 0-1)
  • train_labels[i][0] ใช้ดัชนี `[0]` เพราะ label เป็น array ขนาด (1,)
  • การ visualize ช่วยยืนยันว่า pipeline โหลดข้อมูลทำงานถูกต้องก่อนเข้า training

ข้อแนะนำ

ควรตรวจสอบข้อมูลทุกครั้งหลังโหลด dataset ใหม่ เพื่อป้องกัน bug เช่น label ผิด, รูปเสีย, หรือ preprocessing ผิดพลาด