简体   繁体   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)

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我的 X_train 是

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)?我是否需要将 X_train 中的每个图像重塑为 (-1,28,28,1)? The process of dealing with X_train is below:处理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)

...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.你应该在第一个Dense层之前有一个Flatten层。 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.如果你不使用单热编码,而是将标签作为整数提供,你应该使用 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)

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.图像通道很重要,您可以尝试在原始图像的 color_mode 中使用“灰度”或“rgb”,或者简单地将其提供给模型(28、28、5),当您使用具有以下特征的特征提取图像时,它是相同的多于一个通道的频率响应。

Problem:问题:

  1. From you question the line batchnorm_model.add(Dense(50, input_shape=(X_train.shape[1],), activation='relu', kernel_initializer='normal')) indicated mismatched input从你质疑行 batchnorm_model.add(Dense(50, input_shape=(X_train.shape[1],), activation='relu', kernel_initializer='normal')) 表示输入不匹配
  2. Error message: 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)错误消息:层“dense_48”的输入 0 与层不兼容:输入形状的预期轴 -1 的值为 28,但收到的输入形状为(无、28、28、5)

You should你应该

  1. Match the input shape to input_shape=(X_train.shape[1], X_train.shape[2], X_train.shape[3]) or将输入形状与 input_shape=(X_train.shape[1], X_train.shape[2], X_train.shape[3]) 或
  2. Convert them into 'rgb' or 'grayscale' format then use an image generator.将它们转换为“rgb”或“灰度”格式,然后使用图像生成器。
  3. The number of channels does not often indicate data type when displayed but the information is the frequency response for the image sees the image in example.通道数在显示时通常不指示数据类型,但信息是图像的频率响应,例如示例中的图像。

Sample: Image to a dataset, custom_image_preprocess function to convert to the target format.示例:图像到数据集,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)

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

暂无
暂无

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

相关问题 Tensorflow 在预测时给出错误:输入形状的预期轴 -1 的值为 784,但收到的输入形状为 [None, 28] - Tensorflow gives error when predicting: expected axis -1 of input shape to have value 784 but received input with shape [None, 28] | ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(None, 28, 28),找到的形状 =(None, 28, 3) - | ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 28, 28), found shape=(None, 28, 3) “连接”层需要具有匹配形状的输入,连接轴除外。 收到:input_shape=[(None, 28), (None, 28, 28)] - A `Concatenate` layer requires inputs with matching shapes except for the concatenation axis. Received: input_shape=[(None, 28), (None, 28, 28)] 将input_data(shape = [None,28,28,1])整形为input_data(shape = [None,28,28]) - Reshape input_data(shape=[None, 28, 28, 1]) to input_data(shape=[None, 28, 28]) ValueError:检查时出错:预期conv2d_1_input具有形状(28,28,1),但数组的形状为(58000,28,28) - ValueError: Error when checking : expected conv2d_1_input to have shape (28, 28, 1) but got array with shape (58000, 28, 28) ValueError: 层序的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。 收到的完整形状:(无、28、28) - ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: (None, 28, 28) 如何解决问题“检查输入时出错:期望flatten_7_input具有形状(28,28)但形状为(28,3)的数组” - How to fix problem 'Error when checking input: expected flatten_7_input to have shape (28, 28) but got array with shape (28, 3)' Tensorflow:Model 是用形状 (None, 28, 28) 构造的,但它是在形状不兼容的输入上调用的 (None, 28) - Tensorflow:Model was constructed with shape (None, 28, 28) , but it was called on an input with incompatible shape (None, 28) Keras 输入形状抛出预期 4d 的值错误,但得到了一个形状为 (60000, 28,28) 的数组 - Keras input shape throws value error expected 4d but got an array with shape (60000, 28,28) DeepFool无法将形状(28,28,28)的输入数组广播成形状(28,28,1) - DeepFool could not broadcast input array from shape (28,28,28) into shape (28,28,1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM