简体   繁体   中英

ValueError: Error when checking target: expected dense_2 to have shape (2,) but got array with shape (75,)

model = Sequential()
model.add(TimeDistributed(Conv2D(64, (3, 3), activation='relu'),input_shape=(10, 255, 255,1)))
model.add(TimeDistributed(Conv2D(64, (3, 3), activation='relu')))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(10, activation='relu', return_sequences=False))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='sigmoid'))
model.compile('adam', loss='categorical_crossentropy')

model.fit(train_dataset_new, train_labels,
                                    epochs=25,
                                    batch_size=10,
                                   validation_data=(validation_dataset_new,validation_labels))
                                    

Hello,

I'm struggling on the subject at hand. I will start with my goal:

I would like to train the following model with a sequence of images (video) - 10 to be precise.

My logic: In order to do so, I have created a video Database and extracted 10 frames from each video into an np.array object. Each frame has the size of (255,255) and on grayscale. Summing up: The input shape to (255,255,1).

I am certain I'm missing something about the TimeDistrebution layer\ LSTM layer and I couldn't find solution on any sources I have looked for (and I have tried)

I have debugged the code and verified the following:

train_dataset_new = (75,10,255,255,1) # (75 is the amount of train videos I have ) validation_dataset_new = (19,10,255,255,1) # (19 is the amount of train videos I have )

My understanding on the problem: As noted in the headline, my Dense layer is expecting an input of size but I feed into my network my entire sample domain (75) and I cannot find out if I'm fitting wrongly or my model Architecture is wrong (I would like to note that this is a basic Architecture I found online and using it just to explore this subject)

Would be glad for any help \ direction to a reading on the matter. If any more data is required due tell as Im new to this forum.

Cheers!

Take a look at the shape of your target labels. When running your code with (randomly generated) training data of shape (75,10,255,255,1) and target labels of shape (75,2) , it seemed to work for me, using TF2.2 and Keras 2.4.3. The error might just being saying the network's output and target label shapes are not matching, though I must say it is not a very clear error message if so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM