簡體   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