I'm new to keras. I'm trying to train a model which focuses on batchnormalization. My code is
batchnorm_model = Sequential()
batchnorm_model.add(Dense(50, input_shape=(X_train.shape[1],), activation='relu', kernel_initializer='normal'))
batchnorm_model.add(BatchNormalization())
batchnorm_model.add(Dense(50, activation='relu', kernel_initializer='normal'))
batchnorm_model.add(BatchNormalization())
batchnorm_model.add(Dense(2))
# Compile your model with sgd
batchnorm_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
h2_callback = batchnorm_model.fit(X_train, train_labels, validation_data=(X_test, test_labels), epochs=10, verbose = 0)
And my X_train is
print(X_train.shape)
(7000, 28, 28, 5)
And my error is
ValueError: in user code:
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 889, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py", line 248, in assert_input_compatibility
raise ValueError(
ValueError: Exception encountered when calling layer "sequential_14" (type Sequential).
Input 0 of layer "dense_48" is incompatible with the layer: expected axis -1 of input shape to have value 28, but received input with shape (None, 28, 28, 5)
Call arguments received by layer "sequential_14" (type Sequential):
• inputs=tf.Tensor(shape=(None, 28, 28, 5), dtype=float32)
• training=True
• mask=None
Do I need to reshape each image in X_train to (-1,28,28,1)? The process of dealing with X_train is below:
width = 28
height = 28
dim = (width, height)
from google.colab.patches import cv2_imshow
from skimage.io import imread
from skimage.io import imshow
all_images = []
for id in new_id:
PIC = '/content/new/' id
im = cv2.imread(PIC)
resized = cv2.resize(im, dim, interpolation = cv2.INTER_AREA)/255
indices = np.dstack(np.indices(resized.shape[:2]))
data = np.concatenate((resized, indices), axis=-1)
all_images.append(data)
...processing labels data
X_train, X_test, y_train, y_test = train_test_split(all_images, all_labels,
test_size=0.3)
X_train = np.array(X_train,dtype="float32")
X_test = np.array(X_test,dtype="float32")
CodePudding user response:
try to match image channels with the data input layer. The image channels is important you may try to use 'grayscales' or 'rgb' in color_mode from the original image or simply feed it to the model ( 28, 28, 5 ) it is the same when you working with features extraction image that had more than one channels frequency responses.
Problem:
- From you question the line batchnorm_model.add(Dense(50, input_shape=(X_train.shape
CodePudding user response:
You should have a
Flatten
layer before your firstDense
layer. And if you don't use one-hot encoding, but provide the labels as integers, you should use SparseCategoricalCrossentropy as loss, withfrom_logits=True
because there is no activation in your lastDense
layer.batchnorm_model = Sequential() batchnorm_model.add(Flatten(input_shape=(X_train.shape[1],))) batchnorm_model.add(Dense(50, activation='relu', kernel_initializer='normal')) batchnorm_model.add(BatchNormalization()) batchnorm_model.add(Dense(50, activation='relu', kernel_initializer='normal')) batchnorm_model.add(BatchNormalization()) batchnorm_model.add(Dense(2)) # Compile your model with sgd batchnorm_model.compile(optimizer='sgd', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) h2_callback = batchnorm_model.fit(X_train, train_labels, validation_data=(X_test, test_labels), epochs=10, verbose = 0)