繁体   English   中英

输入形状的预期轴 -1 的值为 28,但收到的输入形状为 (None, 28, 28, 5)

[英]expected axis -1 of input shape to have value 28, but received input with shape (None, 28, 28, 5)

我是凯拉斯的新手。 我正在尝试训练一个专注于批归一化的模型。 我的代码是

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)

我的 X_train 是

print(X_train.shape)
(7000, 28, 28, 5)

我的错误是

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

我是否需要将 X_train 中的每个图像重塑为 (-1,28,28,1)? 处理X_train的过程如下:

 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)

...处理标签数据

 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")

你应该在第一个Dense层之前有一个Flatten层。 如果你不使用单热编码,而是将标签作为整数提供,你应该使用 SparseCategoricalCrossentropy 作为损失,使用from_logits=True因为在你的最后一个Dense层中没有激活。

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)

尝试将图像通道与数据输入层相匹配。 图像通道很重要,您可以尝试在原始图像的 color_mode 中使用“灰度”或“rgb”,或者简单地将其提供给模型(28、28、5),当您使用具有以下特征的特征提取图像时,它是相同的多于一个通道的频率响应。

问题:

  1. 从你质疑行 batchnorm_model.add(Dense(50, input_shape=(X_train.shape[1],), activation='relu', kernel_initializer='normal')) 表示输入不匹配
  2. 错误消息:层“dense_48”的输入 0 与层不兼容:输入形状的预期轴 -1 的值为 28,但收到的输入形状为(无、28、28、5)

你应该

  1. 将输入形状与 input_shape=(X_train.shape[1], X_train.shape[2], X_train.shape[3]) 或
  2. 将它们转换为“rgb”或“灰度”格式,然后使用图像生成器。
  3. 通道数在显示时通常不指示数据类型,但信息是图像的频率响应,例如示例中的图像。

示例:图像到数据集,custom_image_preprocess 函数转换为目标格式。

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: 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)

输出:符合环境。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM