Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在RNN模型中使用图像集_Python_Opencv_Tensorflow_Keras - Fatal编程技术网

Python 如何在RNN模型中使用图像集

Python 如何在RNN模型中使用图像集,python,opencv,tensorflow,keras,Python,Opencv,Tensorflow,Keras,您好,我正在尝试使用Keras和Tensorflow进行我的第一次RNN,但我遇到了一个问题,或者正在重塑我的图像以适应模型 我看了这篇文章,但没有弄清楚重塑的原因: 我拥有的是一组在视频中每一帧拍摄的图像。我保存了python之外的所有帧,因此我有一个非常大的图像文件夹。我将帧分为21帧,用于一个片段,因此每个运动我要捕获21个图像。我想把这21幅图像作为一个序列来读。我有从多个摄像机/角度拍摄的相同序列,我想在这个模型中向我们展示。我想尝试的是建立一个运动模型,看看一个人是否在做这个运动,

您好,我正在尝试使用Keras和Tensorflow进行我的第一次RNN,但我遇到了一个问题,或者正在重塑我的图像以适应模型

我看了这篇文章,但没有弄清楚重塑的原因:

我拥有的是一组在视频中每一帧拍摄的图像。我保存了python之外的所有帧,因此我有一个非常大的图像文件夹。我将帧分为21帧,用于一个片段,因此每个运动我要捕获21个图像。我想把这21幅图像作为一个序列来读。我有从多个摄像机/角度拍摄的相同序列,我想在这个模型中向我们展示。我想尝试的是建立一个运动模型,看看一个人是否在做这个运动,所以这是一个二元模型,基本上是或不是。不是最复杂的,但是使用这个模型和keras是一个学习过程

我需要帮助弄清楚如何在keras模型中使用这些图像。我看过一些关于MINST数据集的教程,但这并没有帮助我解决这个问题。 任何帮助都将不胜感激

这是当我尝试训练模型时给我的错误

ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (2026, 200, 200, 1)
我的代码是:

from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import LSTM
from tqdm import tqdm
import cv2
import os
import numpy as np

imageSize = 200

#create lables for each image
def labelImage(img):
    wordLabel = img.split('.')[-3]
    #Conversion to one hot array [lat,not]
    if wordLabel == "FWAC":
        return[1,0]
    else:
        return[0,1]

#Process images and add lables
#Convert data into an array and add its lable
def makeTrainingData():
    print("Creating Training Data")
    trainingData = []
    for img in tqdm(os.listdir(trainDir)):
        label = labelImage(img)
        path = os.path.join(trainDir,img)
        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        img = cv2.resize(img, (imageSize,imageSize))
        trainingData.append([np.array(img),np.array(label)])

    #Save the array file to load it into other models if needed
    np.save("trainingData.npy", trainingData)
    print("Training Data Saved")
    return trainingData

#process the testing data in the same manner
def processTestData():
    print("Creating Testing Data")
    testData = []
    for img in tqdm(os.listdir(testDri)):
        print("image", img)
        path = os.path.join(testDri, img)
        imgNum = img.split(".")[0]
        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
        img = cv2.resize(img, (imageSize, imageSize))
        testData.append([np.array(img), imgNum])

    np.save("testingData.npy", testData)
    print("Testing Data Saved")
    return testData



rnnSize = 512

model = Sequential()
model.add(LSTM(rnnSize, input_shape=(imageSize, imageSize)))
model.add(Dense(1024))
model.add(Activation('relu'))
model.add(Dense(50))
model.add(Activation('sigmoid'))
model.add(Dense(3))  
model.add(Activation('softmax'))


model.compile(loss='mean_squared_error', optimizer='adam',metrics=['accuracy'])

#Data
trainDir = "D:/TrainingDataSets/TrainingSet/"
testDri = "D:/TrainingDataSets/TestingSet/"

#trainData = makeTrainingData()
#testData = processTestData()
trainData = np.load('trainingData.npy')
testData = np.load("testingData.npy")
#resize the image to this See above
train = trainData[:-500]
test = trainData[-200:]

x = []
y = []
for xi in trainData:
    x.append(xi[0].reshape((-1, imageSize, imageSize)))
    y.append(xi[1])

x_train = np.array([i[0] for i in train]).reshape(-1,imageSize, imageSize,1)
y_train = [i[1] for i in train]



test_x = np.array([i[0] for i in test]).reshape(-1,imageSize , imageSize,1)
test_y = [i[1] for i in test]


epoch = 5
batchSize = 100

model.fit(x_train, y_train, epochs=epoch, batch_size= batchSize, verbose=1, shuffle=False)

对于密集层添加此行之前的错误:

model.add(Flatten())
以前,您应该导入:

from keras.layers import Flatten