keras中不兼容的密集层错误
Posted
技术标签:
【中文标题】keras中不兼容的密集层错误【英文标题】:Incompatible dense layer error in keras 【发布时间】:2016-09-29 10:02:47 【问题描述】:我的输入是一系列视频,数量为 8500。每个视频作为一系列 50 帧输入 LSTM,每帧有 960 个像素。 所以输入dim是8500,50,960 有 487 个可能的输出类别,因此输出维度为 8500,487。
但是当我运行以下代码时,我在 keras 中遇到了这些错误。
非常感谢任何帮助。谢谢!
(8500, 50, 960)
(8500, 487)
创建模型..
添加第一层..
添加第二层..
添加输出层..
Traceback(最近一次调用最后一次):
文件“/Users/temp/PycharmProjects/detect_sport_video/build_model.py”,第 68 行,在 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 与 layer_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)。这就是问题所在。但我不知道为什么会得到 (None,50,200)。有什么想法吗?
【问题讨论】:
【参考方案1】:print("添加第二层..") model.add(LSTM(100, return_sequences=False))
【讨论】:
是的,你应该把 return_sequences=False 放在第二个 LSTM 层中。 或者将输出层设为 TimeDistributedLayerprint("Adding second layer...") model.add(LSTM(100, return_sequences=True)) print("Adding output layer...") model.add(TimeDistributed(Dense(487, activation="softmax")))
以上是关于keras中不兼容的密集层错误的主要内容,如果未能解决你的问题,请参考以下文章
Keras 功能 api 输入形状错误,lstm 层收到 2d 而不是 3d 形状