Issue
I’m working on a tensorflow model for identifying different butterfies. I’m using Neural networks for this and I’m reading images from folders and all the data gets split in a train dataset and validation dataset, but I want split these like this:
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
instead of:
train_ds = utils.image_dataset_from_directory(data_dir, validation_split=0.2, subset="training", seed=123, image_size=(img_height, img_width), batch_size=BATCH_SIZE)
val_ds = utils.image_dataset_from_directory(data_dir, validation_split=0.2, subset="validation", seed=123, image_size=(img_height, img_width), batch_size=BATCH_SIZE)
I have tried doing this, but it makes the accuracy of my model real crap so I don’t think it’s correct:
train_images = np.concatenate([x for x, y in train_ds], axis=0)
train_labels = np.concatenate([y for x, y in train_ds], axis=0)
test_images = np.concatenate([x for x, y in val_ds], axis=0)
test_labels = np.concatenate([y for x, y in val_ds], axis=0)
I have tried a lot of methods from stackoverflow, but they also don’t work.
My model:
model = tf.keras.Sequential([
# Please reread this link for a better understanding of the data being entered:
#https://www.codespeedy.com/determine-input-shape-in-keras-tensorflow/
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(180, 180, 3)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2), strides=2),
layers.Flatten(),
layers.Dropout(0.2, input_shape=(180, 180, 3)),
layers.Dense(64, activation='relu'),
layers.Dense(5, activation='softmax') # there are 5 classes_names/folders or 5 kinds of butterflies
])
Solution
Fixed the problem:
train_images = np.array([]).reshape((0,180,180,3))
train_labels = np.array([]).reshape(0,)
for x, y in train_ds:
train_images = np.concatenate((train_images, x), axis=0)
train_labels = np.concatenate((train_labels, y), axis=0)
reshape to the input shape of the images, 0 as size is necessary otherwise, you cannot add x or y.
Answered By – IceFox_Programming
Answer Checked By – Mildred Charles (AngularFixing Admin)