2
$\begingroup$

I'm trying to create an auto-encoder based model for segmentation, which looks something like this: https://i.stack.imgur.com/4F3Z0.png

I haven't added a single step, nor missed one as far as I remember. Then how come, when I try to fit data to it, it throws me an error saying: ValueError: Input 0 of layer conv2d is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape (None, 128, 128, 3)

My code looks something like this:

img_input = Input(shape=input_shape)
x = img_input

# Encoder
# Block 1
# Block 2
# Block 3

# Decoder
# Deconv 1
# Deconv 2

x = Reshape((input_shape[0],input_shape[1], classes))(x)
x = Activation("softmax")(x)
    
model = Model(img_input, x)
    
return model
//Not posting all the code, I might hear I'm dumping code and asking help

Fitted values, something like:

Auto_Encoder.fit(
      np.array(x), # `x` is a python array 
      np.array(y), # `y` is a python array
      ...

Here is the model summary:

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 128, 128, 1)]     0         
_________________________________________________________________
conv2d (Conv2D)              (None, 128, 128, 64)      640       
_________________________________________________________________
batch_normalization (BatchNo (None, 128, 128, 64)      256       
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 128, 128, 64)      36928     
_________________________________________________________________
batch_normalization_1 (Batch (None, 128, 128, 64)      256       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 64, 64, 64)        0         
_________________________________________________________________
dropout (Dropout)            (None, 64, 64, 64)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 64, 64, 128)       73856     
_________________________________________________________________
batch_normalization_2 (Batch (None, 64, 64, 128)       512       
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 64, 64, 128)       147584    
_________________________________________________________________
batch_normalization_3 (Batch (None, 64, 64, 128)       512       
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 64, 64, 128)       147584    
_________________________________________________________________
batch_normalization_4 (Batch (None, 64, 64, 128)       512       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 32, 32, 128)       0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 32, 32, 128)       0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 32, 32, 256)       295168    
_________________________________________________________________
batch_normalization_5 (Batch (None, 32, 32, 256)       1024      
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 32, 32, 256)       590080    
_________________________________________________________________
batch_normalization_6 (Batch (None, 32, 32, 256)       1024      
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 32, 32, 256)       590080    
_________________________________________________________________
batch_normalization_7 (Batch (None, 32, 32, 256)       1024      
_________________________________________________________________
dropout_2 (Dropout)          (None, 32, 32, 256)       0         
_________________________________________________________________
up_sampling2d (UpSampling2D) (None, 64, 64, 256)       0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 64, 64, 128)       295040    
_________________________________________________________________
batch_normalization_8 (Batch (None, 64, 64, 128)       512       
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 64, 64, 128)       147584    
_________________________________________________________________
batch_normalization_9 (Batch (None, 64, 64, 128)       512       
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 64, 64, 128)       147584    
_________________________________________________________________
batch_normalization_10 (Batc (None, 64, 64, 128)       512       
_________________________________________________________________
dropout_3 (Dropout)          (None, 64, 64, 128)       0         
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 128, 128, 128)     0         
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 128, 128, 64)      73792     
_________________________________________________________________
batch_normalization_11 (Batc (None, 128, 128, 64)      256       
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 128, 128, 64)      36928     
_________________________________________________________________
batch_normalization_12 (Batc (None, 128, 128, 64)      256       
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 128, 128, 4)       2308      
_________________________________________________________________
dropout_4 (Dropout)          (None, 128, 128, 4)       0         
_________________________________________________________________
reshape (Reshape)            (None, 128, 128, 4)       0         
_________________________________________________________________
activation (Activation)      (None, 128, 128, 4)       0         
=================================================================
Total params: 2,592,324
Trainable params: 2,588,740
Non-trainable params: 3,584
$\endgroup$

1 Answer 1

4
$\begingroup$

The error message means that the input shape of Conv2D layer should be (128,128,1) which is consistent with your model summary. However, in the actual input the shape it finds is (128,128,3), hence the error. It would seem that you are using a 3 channel image when you have defined only one channel in the input shape.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.