I'm new with machine learning and Keras and I'm messing around with some code. I am trying to make a image classifier that can determine if a picture is a cat or not a cat. My problem is when I pass test_set_y and train_set_y into model.fit(), somehow the array shapes don't match up.

I've searched in stack overflow for the same problem and many of the solutions include one-hot encoding the labels. However the problem still continues after I've one-hot encoded the labels.

def load_dataset():
    train_dataset = h5py.File('cat/train_catvnoncat.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels

    test_dataset = h5py.File('cat/test_catvnoncat.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels

    classes = np.array(test_dataset["list_classes"][:]) # the list of classes

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes

train_dataset = h5py.File('cat/train_catvnoncat.h5', "r")
test_dataset = h5py.File('cat/test_catvnoncat.h5', "r")
# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()

# Example of a picture
index = 78
example = train_set_x_orig[index]
plt.imshow(train_set_x_orig[index])
plt.show()
print("y = " + str(train_set_y[:, index]) + ", it's a '" + classes[np.squeeze(train_set_y[:, index])].decode("utf-8") + "' picture.")

print(train_set_x_orig.shape, test_set_x_orig.shape, train_set_y.shape, test_set_y.shape)

# One hot encode the labels------
train_set_y = to_categorical(train_set_y, num_classes=2)
test_set_y = to_categorical(test_set_y, num_classes=2)
print(train_set_y.shape, test_set_y.shape)

train_set_y = np.reshape(train_set_y, (209, 2))
test_set_y = np.reshape(test_set_y, (50, 2))

print(train_set_y.shape, test_set_y.shape)



# CNN ---------
# Forming model
model = Sequential()

# Adding layers
model.add(Conv2D(64, kernel_size=5, strides=1, padding="Same", activation="relu", input_shape=(64, 64, 3)))
model.add(MaxPooling2D(padding="same"))

model.add(Conv2D(128, kernel_size=5, strides=1, padding="same", activation="relu"))
model.add(MaxPooling2D(padding="same"))
model.add(Dropout(0.3))

model.add(Flatten())

model.add(Dense(256, activation="relu"))
model.add(Dropout(0.3))

model.add(Dense(512, activation="relu"))
model.add(Dropout(0.3))

model.add(Dense(10, activation="softmax"))

# Compiling the model 
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])

# Training the model
model.fit(train_set_x_orig, train_set_y, batch_size=50, epochs=30, validation_data=(test_set_x_orig, test_set_y))

# Evaluate
train_loss_score = model.evaluate(train_set_x_orig, train_set_y)
test_loss_score = model.evaluate(test_set_x_orig, test_set_y)
print(train_loss_score)
print(test_loss_score)

I expect the model to train and at the end give me the loss and scores, but I get "ValueError: Error when checking target: expected dense_3 to have shape (10,) but got array with shape (2,)"

Related posts

Recent Viewed