Search code examples
pythontensorflowkerasdeep-learning3d-reconstruction

Can not squeeze dim[4], expected a dimension of 1, got 3


I am working with project where model take 3D cuboid as input, and reconstruct 3D cuboid. I am getting error:

ValueError: Can not squeeze dim[4], expected a dimension of 1, got 3 for '{{node Squeeze}} = 
Squeeze[T=DT_FLOAT, squeeze_dims=[-1]](remove_squeezable_dimensions/Squeeze)' with input  
shapes: [1,227,227,10,3].

I am using below model. Input is 10 consucative frames with height and width 227*227 and three channel(R, G, B)

model=Sequential()
model.add(Conv3D(filters=128,kernel_size=(11,11,1),strides=(4,4,1),padding='valid',input_shape=(227,227,10,3),activation='tanh'))
model.add(Conv3D(filters=64,kernel_size=(5,5,1),strides=(2,2,1),padding='valid',activation='tanh'))
model.add(ConvLSTM2D(filters=64,kernel_size=(3,3),strides=1,padding='same',dropout=0.4,return_sequences=True,recurrent_dropout=0.3))
model.add(ConvLSTM2D(filters=32,kernel_size=(3,3),strides=1,padding='same',dropout=0.3,return_sequences=True))
model.add(ConvLSTM2D(filters=64,kernel_size=(3,3),strides=1,padding='same',dropout=0.5,return_sequences=True))
model.add(Conv3DTranspose(filters=128,kernel_size=(5,5,1),strides=(2,2,1),padding='valid',activation='tanh'))
model.add(Conv3DTranspose(filters=3,kernel_size=(11,11,1),strides=(4,4,1),padding='valid',activation='tanh'))
model.compile(optimizer='adam',loss='mean_squared_error',metrics=['accuracy'])



Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv3d (Conv3D)             (None, 55, 55, 10, 128)   46592     
                                                                 
 conv3d_1 (Conv3D)           (None, 26, 26, 10, 64)    204864    
                                                                 
 conv_lstm2d (ConvLSTM2D)    (None, 26, 26, 10, 64)    295168    
                                                                 
 conv_lstm2d_1 (ConvLSTM2D)  (None, 26, 26, 10, 32)    110720    
                                                                 
 conv_lstm2d_2 (ConvLSTM2D)  (None, 26, 26, 10, 64)    221440    
                                                                 
 conv3d_transpose (Conv3DTra  (None, 55, 55, 10, 128)  204928    
 nspose)                                                         
                                                                 
 conv3d_transpose_1 (Conv3DT  (None, 227, 227, 10, 3)  46467     
 ranspose)                                                       
                                                                 
=================================================================
Total params: 1,130,179
Trainable params: 1,130,179
Non-trainable params: 0
_________________________________________________________________

And I am using this code to create dataset and train the model

training_data = []
target_data = []
training_data=np.load('/content/training.npy')
print(training_data.shape)  #(227, 227, 3, 14943)
frames=training_data.shape[3]
frames=frames-frames%10

training_data=training_data[:,:,:,:frames]
training_data=training_data.reshape(-1,227,227,10,3)
training_data=np.expand_dims(training_data,axis=5)
target_data=training_data.copy()

epochs=15
batch_size=1

callback_save = ModelCheckpoint("saved_model.h5", monitor="mean_squared_error", save_best_only=True)

callback_early_stopping = EarlyStopping(monitor='loss', patience=3)

history = model.fit(training_data,target_data, batch_size=batch_size, epochs=epochs, callbacks = [callback_save,callback_early_stopping])
model.save("saved_model.h5")

Solution

  • Is training_data=np.expand_dims(training_data,axis=5) needed? I think it makes the training data shape incompatible with the input shape.