0
$\begingroup$

I've directory structure like this, for my dataset:

|--train
|--test
|--valid

In the train folder, these are pairs of images like xyz_sat and xyz_mask.

So I've loaded them with Pillow, and converted them to NumPy array to feed to TensorFlow:

train_sat = [np.array(Image.open(name),dtype="float32") for name in train_names_sat]
train_mask = [np.array(Image.open(name).convert('RGB'),dtype="float32") for name in train_names_mask]

Then normalized them and all other things.

I'm trying to feed images to my model like this:

history = model.fit(train_sat,
                    train_mask,
                    validation_split = 0.15,
                    epochs=EPOCHS,
                    batch_size = BATCH_SIZE,
                    #callbacks = [callbacks]
                       )

But I'm getting this error:

 ValueError: Layer model_4 expects 1 input(s), but it received 10 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 1024, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None, 1024, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:2' shape=(None, 1024, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:3' shape=(None, 1024, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:4' shape=(None, 1024, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:5' shape=(None, 1024, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:6' shape=(None, 1024, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:7' shape=(None, 1024, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:8' shape=(None, 1024, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:9' shape=(None, 1024, 3) dtype=float32>]

Please share how you debugged the error, that would be more helpful.

I can understand that it's because I've 10 images in my dataset training set, for testing purposes, they are causing the error.

My UNet model looks like this:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_5 (InputLayer)            [(None, 1024, 1024,  0                                            
__________________________________________________________________________________________________
conv2d_112 (Conv2D)             (None, 1024, 1024, 6 1792        input_5[0][0]                    
__________________________________________________________________________________________________
batch_normalization_124 (BatchN (None, 1024, 1024, 6 256         conv2d_112[0][0]                 
__________________________________________________________________________________________________
conv2d_113 (Conv2D)             (None, 1024, 1024, 6 36928       batch_normalization_124[0][0]    
__________________________________________________________________________________________________
batch_normalization_125 (BatchN (None, 1024, 1024, 6 256         conv2d_113[0][0]                 
__________________________________________________________________________________________________
conv2d_114 (Conv2D)             (None, 1024, 1024, 6 36928       batch_normalization_125[0][0]    
__________________________________________________________________________________________________
max_pooling2d_20 (MaxPooling2D) (None, 512, 512, 64) 0           conv2d_114[0][0]                 
__________________________________________________________________________________________________
batch_normalization_126 (BatchN (None, 512, 512, 64) 256         max_pooling2d_20[0][0]           
__________________________________________________________________________________________________
conv2d_115 (Conv2D)             (None, 512, 512, 64) 36928       batch_normalization_126[0][0]    
__________________________________________________________________________________________________
batch_normalization_127 (BatchN (None, 512, 512, 64) 256         conv2d_115[0][0]                 
__________________________________________________________________________________________________
conv2d_116 (Conv2D)             (None, 512, 512, 64) 36928       batch_normalization_127[0][0]    
__________________________________________________________________________________________________
batch_normalization_128 (BatchN (None, 512, 512, 64) 256         conv2d_116[0][0]                 
__________________________________________________________________________________________________
conv2d_117 (Conv2D)             (None, 512, 512, 64) 36928       batch_normalization_128[0][0]    
__________________________________________________________________________________________________
max_pooling2d_21 (MaxPooling2D) (None, 256, 256, 64) 0           conv2d_117[0][0]                 
__________________________________________________________________________________________________
batch_normalization_129 (BatchN (None, 256, 256, 64) 256         max_pooling2d_21[0][0]           
__________________________________________________________________________________________________
conv2d_118 (Conv2D)             (None, 256, 256, 64) 36928       batch_normalization_129[0][0]    
__________________________________________________________________________________________________
batch_normalization_130 (BatchN (None, 256, 256, 64) 256         conv2d_118[0][0]                 
__________________________________________________________________________________________________
conv2d_119 (Conv2D)             (None, 256, 256, 64) 36928       batch_normalization_130[0][0]    
__________________________________________________________________________________________________
batch_normalization_131 (BatchN (None, 256, 256, 64) 256         conv2d_119[0][0]                 
__________________________________________________________________________________________________
conv2d_120 (Conv2D)             (None, 256, 256, 64) 36928       batch_normalization_131[0][0]    
__________________________________________________________________________________________________
max_pooling2d_22 (MaxPooling2D) (None, 128, 128, 64) 0           conv2d_120[0][0]                 
__________________________________________________________________________________________________
batch_normalization_132 (BatchN (None, 128, 128, 64) 256         max_pooling2d_22[0][0]           
__________________________________________________________________________________________________
conv2d_121 (Conv2D)             (None, 128, 128, 64) 36928       batch_normalization_132[0][0]    
__________________________________________________________________________________________________
batch_normalization_133 (BatchN (None, 128, 128, 64) 256         conv2d_121[0][0]                 
__________________________________________________________________________________________________
conv2d_122 (Conv2D)             (None, 128, 128, 64) 36928       batch_normalization_133[0][0]    
__________________________________________________________________________________________________
batch_normalization_134 (BatchN (None, 128, 128, 64) 256         conv2d_122[0][0]                 
__________________________________________________________________________________________________
conv2d_123 (Conv2D)             (None, 128, 128, 64) 36928       batch_normalization_134[0][0]    
__________________________________________________________________________________________________
max_pooling2d_23 (MaxPooling2D) (None, 64, 64, 64)   0           conv2d_123[0][0]                 
__________________________________________________________________________________________________
batch_normalization_135 (BatchN (None, 64, 64, 64)   256         max_pooling2d_23[0][0]           
__________________________________________________________________________________________________
conv2d_124 (Conv2D)             (None, 64, 64, 64)   36928       batch_normalization_135[0][0]    
__________________________________________________________________________________________________
batch_normalization_136 (BatchN (None, 64, 64, 64)   256         conv2d_124[0][0]                 
__________________________________________________________________________________________________
conv2d_125 (Conv2D)             (None, 64, 64, 64)   36928       batch_normalization_136[0][0]    
__________________________________________________________________________________________________
batch_normalization_137 (BatchN (None, 64, 64, 64)   256         conv2d_125[0][0]                 
__________________________________________________________________________________________________
conv2d_126 (Conv2D)             (None, 64, 64, 64)   36928       batch_normalization_137[0][0]    
__________________________________________________________________________________________________
max_pooling2d_24 (MaxPooling2D) (None, 32, 32, 64)   0           conv2d_126[0][0]                 
__________________________________________________________________________________________________
batch_normalization_138 (BatchN (None, 32, 32, 64)   256         max_pooling2d_24[0][0]           
__________________________________________________________________________________________________
conv2d_127 (Conv2D)             (None, 32, 32, 64)   36928       batch_normalization_138[0][0]    
__________________________________________________________________________________________________
batch_normalization_139 (BatchN (None, 32, 32, 64)   256         conv2d_127[0][0]                 
__________________________________________________________________________________________________
conv2d_128 (Conv2D)             (None, 32, 32, 64)   36928       batch_normalization_139[0][0]    
__________________________________________________________________________________________________
batch_normalization_140 (BatchN (None, 32, 32, 64)   256         conv2d_128[0][0]                 
__________________________________________________________________________________________________
conv2d_transpose_20 (Conv2DTran (None, 64, 64, 64)   36928       batch_normalization_140[0][0]    
__________________________________________________________________________________________________
concatenate_20 (Concatenate)    (None, 64, 64, 128)  0           conv2d_transpose_20[0][0]        
                                                                 conv2d_125[0][0]                 
__________________________________________________________________________________________________
batch_normalization_141 (BatchN (None, 64, 64, 128)  512         concatenate_20[0][0]             
__________________________________________________________________________________________________
conv2d_129 (Conv2D)             (None, 64, 64, 96)   110688      batch_normalization_141[0][0]    
__________________________________________________________________________________________________
batch_normalization_142 (BatchN (None, 64, 64, 96)   384         conv2d_129[0][0]                 
__________________________________________________________________________________________________
conv2d_130 (Conv2D)             (None, 64, 64, 64)   55360       batch_normalization_142[0][0]    
__________________________________________________________________________________________________
batch_normalization_143 (BatchN (None, 64, 64, 64)   256         conv2d_130[0][0]                 
__________________________________________________________________________________________________
conv2d_transpose_21 (Conv2DTran (None, 128, 128, 64) 36928       batch_normalization_143[0][0]    
__________________________________________________________________________________________________
concatenate_21 (Concatenate)    (None, 128, 128, 128 0           conv2d_transpose_21[0][0]        
                                                                 conv2d_122[0][0]                 
__________________________________________________________________________________________________
batch_normalization_144 (BatchN (None, 128, 128, 128 512         concatenate_21[0][0]             
__________________________________________________________________________________________________
conv2d_131 (Conv2D)             (None, 128, 128, 96) 110688      batch_normalization_144[0][0]    
__________________________________________________________________________________________________
batch_normalization_145 (BatchN (None, 128, 128, 96) 384         conv2d_131[0][0]                 
__________________________________________________________________________________________________
conv2d_132 (Conv2D)             (None, 128, 128, 64) 55360       batch_normalization_145[0][0]    
__________________________________________________________________________________________________
batch_normalization_146 (BatchN (None, 128, 128, 64) 256         conv2d_132[0][0]                 
__________________________________________________________________________________________________
conv2d_transpose_22 (Conv2DTran (None, 256, 256, 64) 36928       batch_normalization_146[0][0]    
__________________________________________________________________________________________________
concatenate_22 (Concatenate)    (None, 256, 256, 128 0           conv2d_transpose_22[0][0]        
                                                                 conv2d_119[0][0]                 
__________________________________________________________________________________________________
batch_normalization_147 (BatchN (None, 256, 256, 128 512         concatenate_22[0][0]             
__________________________________________________________________________________________________
conv2d_133 (Conv2D)             (None, 256, 256, 96) 110688      batch_normalization_147[0][0]    
__________________________________________________________________________________________________
batch_normalization_148 (BatchN (None, 256, 256, 96) 384         conv2d_133[0][0]                 
__________________________________________________________________________________________________
conv2d_134 (Conv2D)             (None, 256, 256, 64) 55360       batch_normalization_148[0][0]    
__________________________________________________________________________________________________
batch_normalization_149 (BatchN (None, 256, 256, 64) 256         conv2d_134[0][0]                 
__________________________________________________________________________________________________
conv2d_transpose_23 (Conv2DTran (None, 512, 512, 64) 36928       batch_normalization_149[0][0]    
__________________________________________________________________________________________________
concatenate_23 (Concatenate)    (None, 512, 512, 128 0           conv2d_transpose_23[0][0]        
                                                                 conv2d_116[0][0]                 
__________________________________________________________________________________________________
batch_normalization_150 (BatchN (None, 512, 512, 128 512         concatenate_23[0][0]             
__________________________________________________________________________________________________
conv2d_135 (Conv2D)             (None, 512, 512, 96) 110688      batch_normalization_150[0][0]    
__________________________________________________________________________________________________
batch_normalization_151 (BatchN (None, 512, 512, 96) 384         conv2d_135[0][0]                 
__________________________________________________________________________________________________
conv2d_136 (Conv2D)             (None, 512, 512, 64) 55360       batch_normalization_151[0][0]    
__________________________________________________________________________________________________
batch_normalization_152 (BatchN (None, 512, 512, 64) 256         conv2d_136[0][0]                 
__________________________________________________________________________________________________
conv2d_transpose_24 (Conv2DTran (None, 1024, 1024, 6 36928       batch_normalization_152[0][0]    
__________________________________________________________________________________________________
concatenate_24 (Concatenate)    (None, 1024, 1024, 1 0           conv2d_transpose_24[0][0]        
                                                                 conv2d_113[0][0]                 
__________________________________________________________________________________________________
batch_normalization_153 (BatchN (None, 1024, 1024, 1 512         concatenate_24[0][0]             
__________________________________________________________________________________________________
conv2d_137 (Conv2D)             (None, 1024, 1024, 9 110688      batch_normalization_153[0][0]    
__________________________________________________________________________________________________
batch_normalization_154 (BatchN (None, 1024, 1024, 9 384         conv2d_137[0][0]                 
__________________________________________________________________________________________________
conv2d_138 (Conv2D)             (None, 1024, 1024, 6 55360       batch_normalization_154[0][0]    
__________________________________________________________________________________________________
conv2d_139 (Conv2D)             (None, 1024, 1024, 1 65          conv2d_138[0][0]                 
==================================================================================================
Total params: 1,617,441
Trainable params: 1,612,513
Non-trainable params: 4,928
$\endgroup$
1
$\begingroup$

This error is caused by the fact that you are passing a list of arrays with the image data to .fit() instead of a single array with the first dimension being the number of samples. Try using numpy.stack to convert the list of arrays to a single numpy array.

$\endgroup$
2
  • $\begingroup$ Awesome, this worked... But I have images of (1024, 1024, 3) shape... But it gives me: ValueError: logits and labels must have the same shape ((8, 1024, 1024, 1) vs (8, 1024, 1024, 3)) $\endgroup$ Oct 25 '21 at 7:31
  • $\begingroup$ This seems like an error that arises when calculating the loss between your masks and the outputs from your model. Your model summary seems to show the correct model output shape ((None, 1024, 1024, 1)), but have you checked what the shape is of the model output array? $\endgroup$
    – Oxbowerce
    Oct 25 '21 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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