keras中

时间:2016-05-31 11:46:56

标签: python python-2.7 neural-network deep-learning keras

我的输入是一系列视频,数量为8500。每个视频作为一系列50帧馈送到LSTM,每帧具有960个像素。 所以输入的暗淡是8500,50,960 可能有487种可能的输出类,因此输出维度为8500,487。

但是当我运行以下代码时,我在keras中遇到这些错误。

非常感谢任何帮助。谢谢!

(8500,50,960)

(8500,487)

创建模型..

添加第一层..

添加第二层..

添加输出图层..

追踪(最近一次呼叫最后一次):

文件“/Users/temp/PycharmProjects/detect_sport_video/build_model.py”,第68行,in     model.add(Dense(487,activation ='softmax'))

文件“/Users/temp/anaconda/lib/python2.7/site-packages/Keras-1.0.3-py2.7.egg/keras/models.py”,第146行,另外     output_tensor = layer(self.outputs [0])

文件“/Users/temp/anaconda/lib/python2.7/site-packages/Keras-1.0.3-py2.7.egg/keras/engine/topology.py”,第441行,调用     self.assert_input_compatibility(x)的

文件“/Users/temp/anaconda/lib/python2.7/site-packages/Keras-1.0.3-py2.7.egg/keras/engine/topology.py”,第382行,在assert_input_compatibility中     STR(K.ndim(X)))

例外:输入0与图层dense_1不兼容:预期ndim = 2,发现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)

另外,如果我在添加每个LSTM图层后尝试打印model.output_shape,我得到的输出是(None,50,200)但它应该是(None,200)。那就是问题所在。但我不知道为什么会得到(无,50,200)。有什么想法吗?

1 个答案:

答案 0 :(得分:4)

打印(“添加第二层......”) model.add(LSTM(100,return_sequences = False))