Reputation: 3
I'm new to Keras and try to build a convolutional neural network. For the preprocessing I took 32 patches of each picture in my sample (batch size) with a size of 200 x 200 (patch size). I generated numpy arrays of it and saved them. I try to feed the model with these batches.
pg = PatchGenerator(image_folder, list_path, sheet_name, patch_size,
batch_size, split_distribution)
# Load batches and labels
X_train = []
Y_train = []
c = 0
for x in range(2):
X_train.append(np.load(train_batch_load_path + train_batch_name +
str(c+1) + '.npy'))
Y_train.append(pg.hd_train_list[c])
c = c + 1
X_train = np.array(X_train)
print(X_train.shape) # shape: (2, 32, 200, 200, 3)
# Model
model.add(Convolution2D(filters=16, kernel_size=3, strides=(1, 1),
bias_initializer='zeros', padding='same', data_format='channels_last',
activation=None, batch_size=32, input_shape=(patch_size, patch_size, 3)))
print(model.output_shape) # shape: (32, 200, 200, 16)
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same',
data_format='channels_last'))
print(model.output_shape) #shape: (32, 100, 100, 16)
model.add(Convolution2D(filters=32, kernel_size=3, strides=(2, 2),
bias_initializer='zeros', padding='same', data_format='channels_last',
activation='relu'))
print(model.output_shape) # shape: (32, 50, 50, 32)
model.add(AveragePooling2D(pool_size=(2, 2), strides=(5, 5),
padding='same', data_format='channels_last'))
print(model.output_shape) # shape: (32, 10, 10, 32)
model.add(Convolution2D(filters=16, kernel_size=5, strides=(1, 1),
bias_initializer='zeros', padding='valid', data_format='channels_last',
activation='relu'))
print(model.output_shape) # shape: (32, 6, 6, 16)
model.add(MaxPooling2D(pool_size=(4, 4), strides=(1, 1), padding='valid',
data_format='channels_last'))
print(model.output_shape) # shape: (32, 3, 3, 16)
model.add(Convolution2D(filters=1, kernel_size=3, strides=(1, 1),
bias_initializer='zeros', padding='valid', data_format='channels_last',
activation='relu'))
print(model.output_shape) # shape: (32, 1, 1, 1)
model.add(Flatten())
print(model.output_shape) # shape: (32, 1)
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=1)
When I try to run the code I got the following error:
ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (2, 32, 200, 200, 3)
I know their are more then 4 dimensons, but when I do the same with only one batch that has a shape with 4 dimensons I got a error that I have to too little dimensions. I think the output shapes are the right ones I need to run the model.
Hope you can help me.
Upvotes: 0
Views: 757
Reputation: 56417
No, your data should be 4D as the error mentions. You gave input shape equals to (200, 200, 3), meaning that the shape for input X should be (samples, 200, 200, 3). Samples can be any number, but the other two dimensions are fixed.
If you got a different error when using a 4D array, you should also include it in your question.
Upvotes: 0