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

วิธีดาวน์โหลดและเตรียมข้อมูล CIFAR-10 ด้วย TensorFlow และ Python

บทนำ

CIFAR-10 เป็น dataset ยอดนิยมสำหรับ image classification ประกอบด้วยรูปภาพสี 60,000 รูป ขนาด 32×32 พิกเซล แบ่ง 10 คลาส คลาสละ 6,000 รูป

TensorFlow/Keras มีฟังก์ชัน datasets.cifar10.load_data() สำหรับดาวน์โหลดและแยก train/test อัตโนมัติ

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

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt

print("The CIFAR dataset is being downloaded")
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

print("The pixel values are normalized to be between 0 and 1")
train_images, test_images = train_images / 255.0, test_images / 255.0

class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
               'dog', 'frog', 'horse', 'ship', 'truck']

ผลลัพธ์

The CIFAR dataset is being downloaded
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170500096/170498071 [==============================] - 11s 0us/step
The pixel values are normalized to be between 0 and 1

คำอธิบาย

  • load_data() คืนค่า tuple: ((train_images, train_labels), (test_images, test_labels))
  • ขนาด train: 50,000 รูป, test: 10,000 รูป
  • normalize ด้วยหาร 255.0 ให้ค่าพิกเซลอยู่ใน [0,1] ซึ่งช่วยให้โมเดล converge เร็วขึ้น
  • class_names map หมายเลขคลาส 0-9 เป็นชื่อคลาสอ่านง่าย

ข้อมูลเพิ่มเติม

CIFAR-10 มักใช้เป็น benchmark เริ่มต้นสำหรับ CNN เนื่องจากขนาดเล็ก โหลดเร็ว แต่ยังท้าทายพอที่จะทดสอบ architecture ต่างๆ ได้ดี