Error in Unet due to mismatch in input format or None values (?)

40 views Asked by At

I am doing a Unet to make segmentation, in this file https://github.com/erickfmm/ML-experiments/blob/master/test/butterfly.py

The shape of xtrain and ytrain are x_train shape (557, 100, 100, 3) y_train shape (557, 100, 100, 3)

And no None values

The summary() of my model is:


 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]']

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

And my code is

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

I fixed the shapes of my layers to be ok, i ensured no None values and all int. I don't know why the input shape output has [] but i suppose its good, don't know why its giving me that error

The error message

  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'
    ```
1

There are 1 answers

0
vincegeratorix On BEST ANSWER

Solved, LOL I replaced "x" variable from function parameter with "x" variable layer. I replaced def simple_segmentation(x, y) with def simple_segmentation(x_input, y_input) And solved XD Also I use now binary_crossentropy as loss function