Search code examples
pythonimage-processingneural-networkpredictionloss-function

Error when checking target: expected dense_3 to have shape (256,) but got array with shape (1,)


I am working on training a VGG16-like model in Keras, trying to predict a continue / time-to-event value (regression) having as input images, and encountered the following error:

Error when checking target: expected dense_3 to have shape (256,) but got array with shape (1,)

This is the structure of the model:

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 256, 256, 64)      1792      
_________________________________________________________________
batch_normalization_1 (Batch (None, 256, 256, 64)      256       
_________________________________________________________________
.
.
.

_________________________________________________________________
conv2d_16 (Conv2D)           (None, 16, 16, 512)       2359808   
_________________________________________________________________
batch_normalization_16 (Batc (None, 16, 16, 512)       2048      
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 8, 8, 512)         0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 32768)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 1000)              32769000  
_________________________________________________________________
dense_2 (Dense)              (None, 1000)              1001000   
_________________________________________________________________
dense_3 (Dense)              (None, 256)               256256    

Total params: 54,072,656
Trainable params: 54,061,648
Non-trainable params: 11,008

I have tried adding another layer at the end with only one neuron and it seems to work that way, but i do not think that is the right way to do it. I have read about similar issue but i have not managed to find a solution.

The last layers of the model built by the code below

#Convolution Layer
#input: 64x64x128, image with 128 channels, appy 256 convolution filters
model.add(Conv2D(512, kernel_size=3, activation='relu',padding='same' ))
#the output of the layer above is 64x64x256

#Normalization layer
model.add(BatchNormalization())

#Convolution Layer
#input: 64x64x128, image with 128 channels, appy 256 convolution filters
model.add(Conv2D(512, kernel_size=3, activation='relu',padding='same' ))
#the output of the layer above is 64x64x256

#Normalization layer
model.add(BatchNormalization())

#Max-Pooling
#poolsize:(2,2), factors by which to downscale (vertical, horizontal)
model.add(MaxPooling2D(pool_size=(2,2), dim_ordering="tf"))

#Flatten layer
model.add(Flatten())

#Fully connected layer 
#number of neurons is chosen randomly
model.add(Dense(1000, activation='relu'))

#Fully connected layer 
model.add(Dense(1000, activation='relu'))


#Fully connected layer 
model.add(Dense(256, activation='softmax'))

model.summary()


#Compile model
model.compile(loss='categorical_crossentropy', optimizer='adagrad')

I am also not sure which loss function should i use in case of predicting time-to-event values.


Solution

  • model.add(Dense(256, activation='relu'))
    model.add(Dense(1, activation='linear'))
    model.compile(loss='mean_squared_error', optimizer='adagrad')
    

    Change your last but one layer to relu activation and finally add one linear activation layer. Use MSE loss as it is a regression problem.