简体   繁体   English

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

[英]How to use a image set in RNN model

Hello I am trying to do my first RNN using Keras and Tensorflow, but I am getting stuck on an issue or reshaping my images to fit into the model.您好,我正在尝试使用 Keras 和 Tensorflow 进行我的第一个 RNN,但是我遇到了一个问题,或者正在重新调整我的图像以适应模型。

I have looked at this post but could not figure out about the reshaping:我看过这篇文章,但无法弄清楚重塑:

Keras - Input a 3 channel image into LSTM Keras - 将 3 通道图像输入 LSTM

What I have is a bunch of images that are taken at every frame in a video.我拥有的是在视频中的每一帧拍摄的一堆图像。 I saved all the frames outside of python so I have a very large folder of images.I separated the frames into 21 frames for a segment so 21 images per motion that I want to capture.我保存了 python 之外的所有帧,所以我有一个非常大的图像文件夹。我将帧分成 21 帧作为一个片段,所以我想捕获每个运动的 21 个图像。 I want to read in these 21 images as one sequence.我想将这 21 张图像作为一个序列读取。 I have the same sequence captured from multiple cameras/angles which I want to us in this model.我有从多个摄像机/角度捕获的相同序列,我想在这个模型中给我们。 What I want to try is to model a movement and see if a person is doing this movement or not, so it is a binary model yes or no basically.我想尝试的是模拟一个运动,看看一个人是否在做这个运动,所以它基本上是一个二元模型是或否。 Not the most sophisticated but its a learning process to use this model and keras.不是最复杂的,但它是使用此模型和 keras 的学习过程。

I need help figuring out how to use these images inside the keras model.我需要帮助弄清楚如何在 keras 模型中使用这些图像。 I have looked at a few tutorials on MINST data set but that didnt help me figure this out.我看过一些关于 MINST 数据集的教程,但这并没有帮助我弄清楚这一点。 Any help will be appreciated.任何帮助将不胜感激。

This is the error that is given to me when I try to train the model这是我尝试训练模型时给我的错误

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

My code is this:我的代码是这样的:

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)

For the error before dense layers add this line:对于密集层之前的错误添加以下行:

model.add(Flatten())

Previously, you should import:以前,您应该导入:

from keras.layers import Flatten

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM