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

btxsgosb  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(140)

我正在做一个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值
我的模型的总结()是:

  1. Layer (type) Output Shape Param # Connected to
  2. ==================================================================================================
  3. input_1 (InputLayer) [(None, 100, 100, 3)] 0 []
  4. conv2d (Conv2D) (None, 100, 100, 32) 2432 ['input_1[0][0]']
  5. dropout (Dropout) (None, 100, 100, 32) 0 ['conv2d[0][0]']
  6. max_pooling2d (MaxPooling2 (None, 50, 50, 32) 0 ['dropout[0][0]']
  7. D)
  8. conv2d_1 (Conv2D) (None, 50, 50, 32) 25632 ['max_pooling2d[0][0]']
  9. dropout_1 (Dropout) (None, 50, 50, 32) 0 ['conv2d_1[0][0]']
  10. max_pooling2d_1 (MaxPoolin (None, 25, 25, 32) 0 ['dropout_1[0][0]']
  11. g2D)
  12. conv2d_2 (Conv2D) (None, 25, 25, 32) 9248 ['max_pooling2d_1[0][0]']
  13. dropout_2 (Dropout) (None, 25, 25, 32) 0 ['conv2d_2[0][0]']
  14. max_pooling2d_2 (MaxPoolin (None, 9, 9, 32) 0 ['dropout_2[0][0]']
  15. g2D)
  16. conv2d_3 (Conv2D) (None, 9, 9, 256) 73984 ['max_pooling2d_2[0][0]']
  17. up_sampling2d (UpSampling2 (None, 27, 27, 256) 0 ['conv2d_3[0][0]']
  18. D)
  19. zero_padding2d (ZeroPaddin (None, 27, 27, 32) 0 ['conv2d_2[0][0]']
  20. g2D)
  21. concatenate (Concatenate) (None, 27, 27, 288) 0 ['up_sampling2d[0][0]',
  22. 'zero_padding2d[0][0]']
  23. conv2d_4 (Conv2D) (None, 25, 25, 32) 82976 ['concatenate[0][0]']
  24. dropout_3 (Dropout) (None, 25, 25, 32) 0 ['conv2d_4[0][0]']
  25. up_sampling2d_1 (UpSamplin (None, 50, 50, 32) 0 ['dropout_3[0][0]']
  26. g2D)
  27. concatenate_1 (Concatenate (None, 50, 50, 64) 0 ['up_sampling2d_1[0][0]',
  28. ) 'conv2d_1[0][0]']
  29. conv2d_5 (Conv2D) (None, 46, 46, 32) 51232 ['concatenate_1[0][0]']
  30. dropout_4 (Dropout) (None, 46, 46, 32) 0 ['conv2d_5[0][0]']
  31. up_sampling2d_2 (UpSamplin (None, 92, 92, 32) 0 ['dropout_4[0][0]']
  32. g2D)
  33. zero_padding2d_1 (ZeroPadd (None, 100, 100, 32) 0 ['up_sampling2d_2[0][0]']
  34. ing2D)
  35. concatenate_2 (Concatenate (None, 100, 100, 64) 0 ['zero_padding2d_1[0][0]',
  36. ) 'conv2d[0][0]']
  37. conv2d_6 (Conv2D) (None, 100, 100, 3) 1731 ['concatenate_2[0][0]']
  38. ==================================================================================================

字符串
我的代码是

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。我不知道为什么输入形状输出有[],但我认为它是好的,不知道为什么它给我的错误
错误消息

  1. 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.
  2. Traceback (most recent call last):
  3. File "D:\<mifolder>\ML-experiments\test\butterfly.py", line 189, in <module>
  4. model = simple_segmentation(x_train, y_train)
  5. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  6. File "D:\<mifolder>\ML-experiments\test\butterfly.py", line 167, in simple_segmentation
  7. model.fit(x, y, epochs=100, batch_size=16)
  8. File "D:\<mifolder>\venv\Lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler
  9. raise e.with_traceback(filtered_tb) from None
  10. File "D:\<mifolder>\venv\Lib\site-packages\keras\src\engine\data_adapter.py", line 266, in <genexpr>
  11. int(i.shape[0]) for i in tf.nest.flatten(inputs)
  12. ^^^^^^^^^^^^^^^
  13. TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
  14. ```

vxbzzdmp

vxbzzdmp1#

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

相关问题