0

I have the following code that works fine.

num_classes = 3
input_shape = (120, 120, 3)

(x_train, y_train), (x_test, y_test) = keras.datasets.cifar100.load_data()

print(f"x_train shape: {x_train.shape} - y_train shape: {y_train.shape}")
print(f"x_test shape: {x_test.shape} - y_test shape: {y_test.shape}")

print(f"X train :  {x_train}")
print(f"Y train :  {y_train}")

Output :

x_train shape: (50000, 32, 32, 3) - y_train shape: (50000, 1)
x_test shape: (10000, 32, 32, 3) - y_test shape: (10000, 1)

Now I want to load my own data set in the same format so that the rest of the code won't be impacted. My image folder is at '/keras_data/train' and '/keras_data/test' in the current folder. There are three classes ['cars','flowers','planes'] in each train and test folder.

This is what I did to get a similar format to 'cifar100' but it's not quite right.

from tensorflow.keras.preprocessing.image import ImageDataGenerator

num_classes = 3
input_shape = (120, 120, 3)

# Define the directories for the training and testing data
train_dir = 'keras_data/train'
test_dir = 'keras_data/test'

# Define the size of the input image and the batch size
img_size = (120, 120)
batch_size = 64

# Define data generators for train and test sets
train_datagen = ImageDataGenerator()#rescale=1./255)
train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=img_size,
        batch_size=batch_size,
        class_mode='input')

test_datagen = ImageDataGenerator()#rescale=1./255)
test_generator = test_datagen.flow_from_directory(
        test_dir,
        target_size=img_size,
        batch_size=batch_size,
        class_mode='input')

# Load the dataset into variables
x_train, y_train = train_generator.next()
x_test, y_test = test_generator.next()

#print(f"X train :  {x_train}")
#print(f"Y train :  {y_train}")

print(f"x_train shape: {x_train.shape} - y_train shape: {y_train.shape}")
print(f"x_test shape: {x_test.shape} - y_test shape: {y_test.shape}")

Output :

x_train shape: (64, 120, 120, 3) - y_train shape: (64, 120, 120, 3)
x_test shape: (43, 120, 120, 3) - y_test shape: (43, 120, 120, 3)

y_train and y_test dimensions are not right.

Any help is appreciated to get an output similar to 'cifar100' output.

PCG
  • 2,049
  • 5
  • 24
  • 42

0 Answers0