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")
You should have a Flatten
layer before your first Dense
layer. And if you don't use one-hot encoding, but provide the labels as integers, you should use SparseCategoricalCrossentropy as loss, with from_logits=True
because there is no activation in your last Dense
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)
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:
You should
Sample: Image to a dataset, custom_image_preprocess function to convert to the target format.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Class / Function
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def custom_image_preprocess( image ):
random_lotation_layer = tf.keras.layers.RandomRotation(
factor=(-0.2, 0.3),
fill_mode='nearest',
interpolation='nearest',
seed=None,
fill_value=0.0,
)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Image conversion function / sample ( you can applied feature extraction example MFCC as in the example
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
image = tf.experimental.numpy.dstack( [image, tf.zeros([IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS])] )
image = image[:,:,0:IMG_CHANNELS]
return image
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: DataSet
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
train_image_generator = ImageDataGenerator(rescale=1. / 255, vertical_flip=True, horizontal_flip=True, preprocessing_function=custom_image_preprocess,)
train_data_gen = train_image_generator.flow_from_directory(batch_size=BATCH_SIZE,
directory=train_dir,
shuffle=True,
target_size=(IMG_WIDTH, IMG_HEIGHT),
class_mode='binary',
color_mode='rgb',
seed=seed_1,)
test_image_generator = ImageDataGenerator(rescale=1. / 255, vertical_flip=True, horizontal_flip=True, preprocessing_function=custom_image_preprocess,)
test_data_gen = test_image_generator.flow_from_directory(batch_size=BATCH_SIZE,
directory=test_dir,
shuffle=False,
target_size=(IMG_WIDTH, IMG_HEIGHT),
class_mode='binary',
color_mode='rgb',
seed=seed_2,)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
base_model = tf.keras.applications.Xception( weights='imagenet', input_shape=(IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS), include_top=False)
base_model.trainable = False
inputs = tf.keras.Input(shape=(IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS))
x = tf.keras.applications.xception.preprocess_input(inputs)
x = base_model(x, training=False)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dropout(0.2)(x)
outputs = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inputs, outputs)
Output: Complie with the environments.
Epoch 1/10
2022-12-09 01:07:34.157717: I tensorflow/stream_executor/cuda/cuda_dnn.cc:368] Loaded cuDNN version 8100
44/321 [===>..........................] - ETA: 1:16 - loss: 0.4925 - binary_accuracy: 0.1321
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.