!git clone https://github.com/YoongiKim/CIFAR-10-images.git
Import Dataset CIFAR-10 Melalui Github
Lakukan clone repository github berikut:
Tambahkan kode berikut untuk load dataset
import tensorflow as tf
import numpy as np
import os
def load_data(image_root='CIFAR-10-images', img_size=(32, 32), batch_size=32, as_numpy=True):
= os.path.join(image_root, 'train')
train_dir = os.path.join(image_root, 'test')
test_dir
= tf.keras.utils.image_dataset_from_directory(
train_ds
train_dir,='inferred',
labels='int',
label_mode=img_size,
image_size=batch_size,
batch_size=True
shuffle
)
= tf.keras.utils.image_dataset_from_directory(
test_ds
test_dir,='inferred',
labels='int',
label_mode=img_size,
image_size=batch_size,
batch_size=False
shuffle
)
if as_numpy:
= [], []
x_train, y_train for imgs, labels in train_ds:
x_train.append(imgs.numpy())
y_train.append(labels.numpy())= np.concatenate(x_train, axis=0)
x_train if batch_size == None:
= np.stack(y_train, axis=0).reshape(-1, 1)
y_train else:
= np.concatenate(y_train, axis=0).reshape(-1, 1)
y_train
= [], []
x_test, y_test for imgs, labels in test_ds:
x_test.append(imgs.numpy())
y_test.append(labels.numpy())= np.concatenate(x_test, axis=0)
x_test if batch_size == None:
= np.stack(y_test, axis=0).reshape(-1, 1)
y_test else:
= np.concatenate(y_test, axis=0).reshape(-1, 1)
y_test
return (x_train.astype(np.uint8), y_train.astype(np.uint8)), (x_test.astype(np.uint8), y_test.astype(np.uint8))
return train_ds, test_ds
Train dan Test Dataset bisa langsung di load dengan kode berikut
= load_data() (X_train, y_train), (X_test, y_test)