Incompatible dense layer error in keras

Ram

My input is an series of videos, 8500 in number. Each video is fed to LSTM as a series of 50 frames, each frame having 960 pixels. So the input dim is 8500,50,960 There are 487 possible output classes possible, so the output dimension is 8500,487.

But when I run the following code, I am getting these errors in keras.

Any help is greatly appreciated. Thanks!

(8500, 50, 960)

(8500, 487)

Creating model..

Adding first layer..

Adding second layer..

Adding output layer..

Traceback (most recent call last):

File "/Users/temp/PycharmProjects/detect_sport_video/build_model.py", line 68, in model.add(Dense(487, activation='softmax'))

File "/Users/temp/anaconda/lib/python2.7/site-packages/Keras-1.0.3-py2.7.egg/keras/models.py", line 146, in add output_tensor = layer(self.outputs[0])

File "/Users/temp/anaconda/lib/python2.7/site-packages/Keras-1.0.3-py2.7.egg/keras/engine/topology.py", line 441, in call self.assert_input_compatibility(x)

File "/Users/temp/anaconda/lib/python2.7/site-packages/Keras-1.0.3-py2.7.egg/keras/engine/topology.py", line 382, in assert_input_compatibility str(K.ndim(x)))

Exception: Input 0 is incompatible with layer dense_1: expected ndim=2, found ndim=3

from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as np
from PIL import Image
import os

def atoi(video):
    return int(video) if video.isdigit() else video

def natural_keys(video):
    return [ atoi(c) for c in os.path.splitext(video) ]


input_data =np.zeros((8500,50,960))

video_index = 0
data = 'train'
video_list = sorted(os.listdir('/Users/temp/PycharmProjects/detect_sport_video/' + data + '_frame_resize1/'))
video_list.sort(key=natural_keys)


for video in video_list:
    if video != '.DS_Store':
        frame_index = 0
        frame_list = sorted(os.listdir('/Users/temp/PycharmProjects/detect_sport_video/' + data + '_frame_resize1/' + video + '/'))
        frame_list.sort(key=natural_keys)
        for frame in frame_list:
            image = np.asarray(Image.open('/Users/temp/PycharmProjects/detect_sport_video/' + data + '_frame_resize1/' + video + '/' + frame))
            image = image.reshape(image.shape[0] * image.shape[1],3)
            image = (image[:,0] + image[:,1] + image[:,2]) / 3
            image = image.reshape(len(image),1)
            image = image[:960]
            image = image.T
            input_data[video_index][frame_index] = image
            frame_index += 1
        video_index += 1

print input_data.shape

cnt = 1
output_classes = []
with open('/Users/temp/PycharmProjects/detect_sport_video/sports-1m-dataset/' + data + '_correct_links.txt') as input_file:
 while cnt <= 8500:
        output_classes.append(int(input_file.readline().split()[2]))
        cnt += 1
output_data =np.zeros((8500,487))
output_index = 0
while(output_index < 8500):
    output_data[output_index,output_classes[output_index]] = 1
    output_index += 1

print output_data.shape

print("Creating model..")
model = Sequential()
print("Adding first layer..")
model.add(LSTM(100, return_sequences=True,
               input_shape=(50, 960)))

print("Adding second layer..")
model.add(LSTM(100, return_sequences=True))

print("Adding output layer..")
model.add(Dense(487, activation='softmax'))

print "Compiling model.."
model.compile(loss='categorical_crossentropy',
              optimizer='RMSprop',
              metrics=['accuracy'])

print "Fitting model.."
model.fit(input_data,output_data,
          batch_size=50, nb_epoch=100)

Also, If I try to print model.output_shape after adding every LSTM layer, the output I get is (None, 50, 200) but it should have been (None,200). Thats where the problem is. But I dont know why am getting (None,50,200). Any ideas?

林春艾

print("Adding second layer..") model.add(LSTM(100, return_sequences=False))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

merging recurrent layers with dense layer in Keras

From Dev

Keras Dense layer's input is not flattened

From Dev

Keras error in Dense layer, expected 4 dimensions got array with shape (1024,2)

From Dev

Error in keras - name 'Dense' is not defined

From Dev

Error in keras - name 'Dense' is not defined

From Dev

Python keras how to transform a dense layer into a convolutional layer

From Dev

How to convert a dense layer to an equivalent convolutional layer in Keras?

From Dev

Keras error with merge layer

From Dev

Keras Input Layer Shape On Input Layer Error

From Dev

Dense layer probably produces InvalidArgumentError: Incompatible shapes: [0,2] vs. [32,2]

From Java

what is the difference between using softmax as a sequential layer in tf.keras and softmax as an activation function for a dense layer?

From Dev

why is the first dense layer not "dense"?

From Dev

Keras output layer gives unexpected error

From Java

Keras Conv2D - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3

From Dev

merging inputs with embeddings and dense layer

From Dev

Tensorflow CNN - Dense layer as Softmax layer input

From Dev

Error when trying to output previous to last layer in Keras

From Dev

Keras ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5

From Dev

Specifying Dense using keras library

From Dev

Building a dense residual network with keras

From Java

Add dense layer on top of Huggingface BERT model

From Dev

Keras error Incompatible shapes: [32,168,24] vs. [32,24]

From Dev

Why is the final layer Dense layer when you construct a network for classification?

From Dev

Keras replacing input layer

From Dev

Keras custom masking layer

From Dev

Viewing layer activations with Keras

From Dev

Input incompatible with the layer for chatbot prediction model

From Dev

AlertDialog Incompatible types error

From Dev

Hashmap error: incompatible types

Related Related

  1. 1

    merging recurrent layers with dense layer in Keras

  2. 2

    Keras Dense layer's input is not flattened

  3. 3

    Keras error in Dense layer, expected 4 dimensions got array with shape (1024,2)

  4. 4

    Error in keras - name 'Dense' is not defined

  5. 5

    Error in keras - name 'Dense' is not defined

  6. 6

    Python keras how to transform a dense layer into a convolutional layer

  7. 7

    How to convert a dense layer to an equivalent convolutional layer in Keras?

  8. 8

    Keras error with merge layer

  9. 9

    Keras Input Layer Shape On Input Layer Error

  10. 10

    Dense layer probably produces InvalidArgumentError: Incompatible shapes: [0,2] vs. [32,2]

  11. 11

    what is the difference between using softmax as a sequential layer in tf.keras and softmax as an activation function for a dense layer?

  12. 12

    why is the first dense layer not "dense"?

  13. 13

    Keras output layer gives unexpected error

  14. 14

    Keras Conv2D - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3

  15. 15

    merging inputs with embeddings and dense layer

  16. 16

    Tensorflow CNN - Dense layer as Softmax layer input

  17. 17

    Error when trying to output previous to last layer in Keras

  18. 18

    Keras ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5

  19. 19

    Specifying Dense using keras library

  20. 20

    Building a dense residual network with keras

  21. 21

    Add dense layer on top of Huggingface BERT model

  22. 22

    Keras error Incompatible shapes: [32,168,24] vs. [32,24]

  23. 23

    Why is the final layer Dense layer when you construct a network for classification?

  24. 24

    Keras replacing input layer

  25. 25

    Keras custom masking layer

  26. 26

    Viewing layer activations with Keras

  27. 27

    Input incompatible with the layer for chatbot prediction model

  28. 28

    AlertDialog Incompatible types error

  29. 29

    Hashmap error: incompatible types

HotTag

Archive