keras 由于输入格式不匹配或无值(?)

btxsgosb  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(113)

我正在做一个Unet来进行分割,在这个文件https://github.com/erickfmm/ML-experiments/blob/master/test/butterfly.py
xtrain和ytrain的形状是x_train形状(557,100,100,3)y_train形状(557,100,100,3)
没有None值
我的模型的总结()是:

Layer (type)                Output Shape                 Param #   Connected to
==================================================================================================
 input_1 (InputLayer)        [(None, 100, 100, 3)]        0         []

 conv2d (Conv2D)             (None, 100, 100, 32)         2432      ['input_1[0][0]']

 dropout (Dropout)           (None, 100, 100, 32)         0         ['conv2d[0][0]']

 max_pooling2d (MaxPooling2  (None, 50, 50, 32)           0         ['dropout[0][0]']
 D)

 conv2d_1 (Conv2D)           (None, 50, 50, 32)           25632     ['max_pooling2d[0][0]']

 dropout_1 (Dropout)         (None, 50, 50, 32)           0         ['conv2d_1[0][0]']

 max_pooling2d_1 (MaxPoolin  (None, 25, 25, 32)           0         ['dropout_1[0][0]']
 g2D)

 conv2d_2 (Conv2D)           (None, 25, 25, 32)           9248      ['max_pooling2d_1[0][0]']

 dropout_2 (Dropout)         (None, 25, 25, 32)           0         ['conv2d_2[0][0]']

 max_pooling2d_2 (MaxPoolin  (None, 9, 9, 32)             0         ['dropout_2[0][0]']
 g2D)

 conv2d_3 (Conv2D)           (None, 9, 9, 256)            73984     ['max_pooling2d_2[0][0]']

 up_sampling2d (UpSampling2  (None, 27, 27, 256)          0         ['conv2d_3[0][0]']
 D)

 zero_padding2d (ZeroPaddin  (None, 27, 27, 32)           0         ['conv2d_2[0][0]']
 g2D)

 concatenate (Concatenate)   (None, 27, 27, 288)          0         ['up_sampling2d[0][0]',
                                                                     'zero_padding2d[0][0]']

 conv2d_4 (Conv2D)           (None, 25, 25, 32)           82976     ['concatenate[0][0]']

 dropout_3 (Dropout)         (None, 25, 25, 32)           0         ['conv2d_4[0][0]']

 up_sampling2d_1 (UpSamplin  (None, 50, 50, 32)           0         ['dropout_3[0][0]']
 g2D)

 concatenate_1 (Concatenate  (None, 50, 50, 64)           0         ['up_sampling2d_1[0][0]',
 )                                                                   'conv2d_1[0][0]']

 conv2d_5 (Conv2D)           (None, 46, 46, 32)           51232     ['concatenate_1[0][0]']

 dropout_4 (Dropout)         (None, 46, 46, 32)           0         ['conv2d_5[0][0]']

 up_sampling2d_2 (UpSamplin  (None, 92, 92, 32)           0         ['dropout_4[0][0]']
 g2D)

 zero_padding2d_1 (ZeroPadd  (None, 100, 100, 32)         0         ['up_sampling2d_2[0][0]']
 ing2D)

 concatenate_2 (Concatenate  (None, 100, 100, 64)         0         ['zero_padding2d_1[0][0]',
 )                                                                   'conv2d[0][0]']

 conv2d_6 (Conv2D)           (None, 100, 100, 3)          1731      ['concatenate_2[0][0]']

==================================================================================================

字符串
我的代码是

input = keras.Input(shape=(100,100,3))
x1 = layers.Conv2D(32, 5, activation="relu", padding="same")(input)
x = layers.Dropout(0.1)(x1)
x = layers.MaxPooling2D((2,2), padding="same")(x)
x2 = layers.Conv2D(32, 5, activation="relu", padding="same")(x)
x = layers.Dropout(0.1)(x2)
x = layers.MaxPooling2D((2,2), padding="same")(x)
x3 = layers.Conv2D(32, 3, activation="relu", padding="same")(x)
x = layers.Dropout(0.1)(x3)
x = layers.MaxPooling2D((3,3), padding="same")(x)

x = layers.Conv2D(256, 3, activation="relu", padding="same")(x)

x = layers.UpSampling2D(size=(3,3))(x)
x3 = layers.ZeroPadding2D(padding=(1, 1))(x3)
x = layers.concatenate([x, x3], axis=-1)
x = layers.Conv2D(32, 3, activation="relu", padding="valid")(x)
x = layers.Dropout(0.1)(x)

x = layers.UpSampling2D(size=(2,2))(x)
#x2 = layers.ZeroPadding2D(2)(x2)
x = layers.concatenate([x, x2], axis=-1)
x = layers.Conv2D(32, 5, activation="relu", padding="valid")(x)
x = layers.Dropout(0.1)(x)

x = layers.UpSampling2D(size=(2,2))(x)
x = layers.ZeroPadding2D(padding=(4, 4))(x)
x = layers.concatenate([x, x1], axis=-1)
output = layers.Conv2D(3, 3, activation="relu", padding="same")(x)

model = keras.Model(inputs=input, outputs=output)
model.summary() #only for printing purposes
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
model.fit(x, y, epochs=100, batch_size=16)


我修复了我的层的形状,我确保没有None值,所有的int。我不知道为什么输入形状输出有[],但我认为它是好的,不知道为什么它给我的错误
错误消息

WARNING:tensorflow:Keras is training/fitting/evaluating on array-like data. Keras may not be optimized for this format, so if your input data format is supported by TensorFlow I/O (https://github.com/tensorflow/io) we recommend using that to load a Dataset instead.
  Traceback (most recent call last):
    File "D:\<mifolder>\ML-experiments\test\butterfly.py", line 189, in <module>
      model = simple_segmentation(x_train, y_train)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "D:\<mifolder>\ML-experiments\test\butterfly.py", line 167, in simple_segmentation
      model.fit(x, y, epochs=100, batch_size=16)
    File "D:\<mifolder>\venv\Lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler
      raise e.with_traceback(filtered_tb) from None
    File "D:\<mifolder>\venv\Lib\site-packages\keras\src\engine\data_adapter.py", line 266, in <genexpr>
      int(i.shape[0]) for i in tf.nest.flatten(inputs)
      ^^^^^^^^^^^^^^^
  TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
    ```

vxbzzdmp

vxbzzdmp1#

解决了,LOL我用“x”变量层替换了函数参数中的“x”变量。我用def simple_segmentation(x_input,y_input)替换了def simple_segmentation(x,y)并解决了XD我现在也用binary_crossentropy作为损失函数

相关问题