1

I am using input images with (64, 64, 3) shape based on the following script. I am not sure why it returns error about data dimension. I also tried trainX = tf.expand_dims(trainX, axis=-1) based on this post, but I could not solve it. Can anyone help me about that?

inputShape = (64, 64, 3)
chanDim = -1
# define the model input
inputs = Input(shape=inputShape)
# CONV => RELU => BN => POOL
x = Conv2D(16, (3, 3), padding="same")(inputs)
x = Activation("relu")(x)
x = BatchNormalization(axis=chanDim)(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
# CONV => RELU => BN => POOL
x = Conv2D(32, (3, 3), padding="same")(x)
x = Activation("relu")(x)
x = BatchNormalization(axis=chanDim)(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
# CONV => RELU => BN => POOL
x = Conv2D(64, (3, 3), padding="same")(x)
x = Activation("relu")(x)
x = BatchNormalization(axis=chanDim)(x)
x = MaxPooling2D(pool_size=(2, 2))(x)

# flatten the volume, then FC => RELU => BN => DROPOUT
x = Flatten()(x)
x = Dense(16)(x)
x = Activation("relu")(x)
x = BatchNormalization(axis=chanDim)(x)
x = Dropout(0.5)(x)

# apply another FC layer, this one to match the number of nodes
# coming out of the MLP
x = Dense(4)(x)
x = Activation("relu")(x)
x = Dense(1, activation="linear")(x)

# construct the CNN
model = Model(inputs, x)

model.summary()
fileToSaveModelPlot='model.png'
plot_model(model, to_file='model.png')
print("[INFO] Model plot saved to {}".format(fileToSaveModelPlot) )

opt = Adam(lr=1e-3, decay=1e-3 / 200)
model.compile(loss="mean_absolute_percentage_error", optimizer=opt)

history=model.fit(trainX, trainY, validation_data=(testX, testY),epochs=EPOCHS_NUM, batch_size=2)

error:

ValueError: Input 0 of layer conv2d_46 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 64, 64)

-update

#load csv file
labelPath =  "/content/drive/MyDrive/Notebook/tepm.csv"
cols = ["temperature"]
df = pd.read_csv(labelPath, sep=" ", header=None, names=cols)

inputPath='/content/drive/MyDrive/Notebook/test_png_64'
images = []

# Load in the images
for filepath in os.listdir(inputPath):
    images.append(cv2.imread(inputPath+'/{0}'.format(filepath),0))

images_scaled = np.array(images, dtype="float") / 255.0

here is the script to define trainY, testY, trainX, and testX

(trainY, testY, trainX, testX) = train_test_split(df, images_scaled, test_size=0.25, random_state=42)

these are the code and result for their shape:

print (trainY.shape,testY.shape,trainX.shape, testX.shape)

(224, 1) (75, 1) (224, 64, 64) (75, 64, 64)
rayan
  • 332
  • 1
  • 12

1 Answers1

0

Simply change the following line:

images.append(cv2.imread(inputPath+'/{0}'.format(filepath)))

By default, OpenCV expects and reads a colored image. Using the 0 flag as an argument to cv2.imread instead loads the image as a grayscale image, which results in it having a single color channel rather than 3 color channels.

DerekG
  • 3,555
  • 1
  • 11
  • 21