我想在keras中使用 LSTM进行视频处理。但视频数据中LSTM的输入形状有问题。请告诉我,我的代码是否正确???而且我知道LSTM的视频时长没有限制,但是当我使用**
在我的代码中为True时:
**才能获取所有我出错的帧,并且每个视频必须获取 40帧。请指导我解决我的问题。我有 297个视频,我的数据形状为(297,40,100,100),其中297个为样本总数,40个为每个视频的帧数,100 * 100为像素。如何获得每个视频的所有帧没有长度限制的LSTM 模型?
listdata_frame = []
num_channel=1
w=100
h=100
opt=[]
cap = cv2.VideoCapture(vid)
batch_size = 2
num_classes = 3
epochs = 10
ret, frame1 = cap.read()
frame1 = cv2.resize(frame1, (w,h))
prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY)
#while(1):
for k in range(40):
ret, frame2 = cap.read()
if not ret:
break
else:
frame2 = cv2.resize(frame2, (w,h))
next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY)
#next = img_to_array(next)
#next = next.shape[0]
#next= next.transpose(Image.FLIP_LEFT_RIGHT)
opt.append(next)
prvs = next
cap.release()
cv2.destroyAllWindows()
input = np.array(opt)
input = input.astype('float32')
listdata_frame.append(input)
#opt=[]
img_data1= np.array(listdata_frame)
num_samples1 = len(img_data1)
#Assign Label to each class
label1=np.ones((num_samples1),dtype = int)
label1[0:2]= 0
label1[2:4] = 1
label1[4:] = 2
Y=np_utils.to_categorical(label1,num_classes)
x,y=shuffle(img_data1,Y,random_state=2)
x_train,x_test,y_train,y_test=train_test_split
(x,y,test_size=0.2,random_state=4)
timesteps = 40
x_train_reshape = [x.reshape((timesteps, 100,-1)) for x in x_train]
x_train = x_train.reshape(x_train.shape[0],
x_train.shape[1]*x_train.shape[2], x_train.shape[3])
x_train = np.array(x_train)
n_classes= 3
n_units=128
n_epochs=5
batch_size = 2
model = Sequential()
model.add(LSTM(n_units, input_shape=(x_train.shape[1],
x_train.shape[2])))
model.add(Dense(n_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size= batch_size, epochs=n_epochs, shuffle=False)