Search code examples
pythontensorflowkeras

ValueError: Shapes (None, 1) and (None, 30, 30, 3, 1) are incompatible


I am doing an exercise to classify images using a convolutional neural network. The images must be read using OpenCV. load_data is already implemented, but I can't seem to implement get_model because of this error.

Whenever I attemp to run this code, I get an error ValueError: Shapes (None, 1) and (None, 30, 30, 1) are incompatible. I have tried searching but I can't understand why this error is occurring. If anyone could help me understand what this error is and why it is happening , I would be very grateful.

import cv2
import numpy as np
import os
import sys
import tensorflow as tf

from sklearn.model_selection import train_test_split

EPOCHS = 10
IMG_WIDTH = 30
IMG_HEIGHT = 30
NUM_CATEGORIES = 43
TEST_SIZE = 0.4


def main():

    # Check command-line arguments
    if len(sys.argv) is not in [2, 3]:
        sys.exit("Usage: python traffic.py data_directory [model.h5]")

    # Get image arrays and labels for all image files
    images, labels = load_data(sys.argv[1])

    # Split data into training and testing sets
    labels = tf.keras.utils.to_categorical(labels)
    x_train, x_test, y_train, y_test = train_test_split(
        np.array(images), np.array(labels), test_size=TEST_SIZE
    )

    # Get a compiled neural network
    model = get_model()

    # Fit model on training data
    model.fit(x_train, y_train, epochs=EPOCHS)

    # Evaluate neural network performance
    model.evaluate(x_test,  y_test, verbose=2)

    # Save model to file
    if len(sys.argv) == 3:
        filename = sys.argv[2]
        model.save(filename)
        print(f"Model saved to {filename}.")


    def load_data(data_dir):
    """
    Load image data from directory `data_dir`.

    Assume `data_dir` has one directory named after each category, numbered
    0 through NUM_CATEGORIES = 1. Inside each category directory will be some
    number of image files

    Return the tuple `(images, labels)`. `images` should be a list of all
    of the images in the data directory, where each image is formatted as a
    numpy ndarray with dimensions IMG_WIDTH x IMG_HEIGHT x 3. `labels` should
    be a list of integer labels, representing the categories for each of the
    corresponding 'images'.
    """
    images = []
    labels = []
    for i in range (NUM_CATEGORIES):
        path = f'{data_dir} {os.sep}{i}'

        for file in os.listdir(path):
            file_path = f'{path}{os.sep}{file}'

            print(f"Reading {file_path}...")
            image = cv2.imread(file_path)

            image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
            
            images.append(image)
            labels.append(i)

    return (images, labels)

    def get_model():
    """
    Returns a compiled convolutional neural network model. Assume that the
    `input_shape` of the first layer is `(IMG_WIDTH, IMG_HEIGHT, 3)`.
    The output layer should have `NUM_CATEGORIES` units, one for each category.
    """
    # Convolutional Neural Network
    model = tf.keras.Sequential([
        # input
        tf.keras.layers.Dense(1, activation="relu") ,

        # hidden layers

        # output
        tf.keras.layers.Dense(NUM_CATEGORIES)
    ])

    model.compile(
        optimizer="adam",
        loss=tf.keras.losses.CategoricalCrossentropy()
    )

    return model


if __name__ == "__main__":
    main()

Solution

  • Because you need a CNN, but you just have this tf.keras.layers.Dense(1, activation="relu"). This is not CNN. Here is an example of CNN https://towardsdatascience.com/coding-a-convolutional-neural-network-cnn-using-keras-sequential-api-ec5211126875