LSTM Keras混乱

2024-04-26 19:08:07 发布

您现在位置:Python中文网/ 问答频道 /正文

@enumaris谢谢你的回答。我将试着解释一下我的方法:

我通过resnet模型推送视频帧,得到了(k, 2048)的疲劳形状。我将数据放入训练/验证和测试文件夹。然后我在写这个剧本:

from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Activation, Dropout, Dense

import tensorflow as tf
import matplotlib

import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import cv2
import os


dataTrain  = []
labelsTrain = []


# Prepare the Training Data. The .txt files contain name of the name of the 
#file and the label which is 0,1,or 2 based on which class the video belongs
#to (nameVideo.npy 0)
with open('D:...\Data\/train_files.txt') as f:
    trainingList = f.readlines()
    for line in trainingList:
        npyFiles = line.split( )
        loadTrainingData = np.load(npyFiles[0])
        dataTrain.append(loadTrainingData)
        labelsTrain.append(npyFiles[1])
        dataNp = np.array(dataTrain, dtype=object)
        labelsNp = np.array(labelsTrain, dtype=object)
        f.close()

dataVal  = []
labelsVal = []

# Prepare the Validation Data
with open('D:\...\Data\/val_files.txt') as f:
    valList = f.readlines()
    for line in valList:
        npyValFiles = line.split( )
        loadValData = np.load(npyValFiles[0])
        dataVal.append(loadValData)
        labelsVal.append(npyValFiles[1])
        f.close()

print(len(dataVal))

model = Sequential()
model.add(LSTM(32,
               batch_input_shape=(None, None, 1),
               return_sequences=True))
model.add(LSTM(32, return_sequences=True))
model.add(LSTM(32))
model.add(Dense(10, activation='softmax'))
model.compile(loss='mean_absolute_error',
              optimizer='adam',
              metrics=['accuracy'])
model.summary()

history = model.fit(dataTrain, labelsTrain,
                    epochs=10,
                    validation_data=(dataVal, labelsVal))

从而导致以下错误:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 3521 arrays.


Tags: ofthefromimportadddatamodelas