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

标签 python opencv tensorflow keras

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

我看过这篇文章,但无法弄清楚 reshape :

Keras - Input a 3 channel image into LSTM

我拥有的是在视频中的每一帧拍摄的一堆图像。我将所有帧都保存在 python 之外,所以我有一个非常大的图像文件夹。我将帧分成 21 个帧作为一个片段,因此每个 Action 我想要捕获 21 个图像。我想将这 21 张图像作为一个序列读取。在这个模型中,我从多个相机/角度捕捉到了相同的序列。我想尝试的是对一个 Action 进行建模,看看一个人是否在做这个 Action ,所以它基本上是一个二元模型是或否。不是最复杂的,但它是使用这个模型和 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

关于python - 如何在 RNN 模型中使用图像集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52749816/

相关文章:

python - 在 tensorflow 双射器中使用和修改变量

python - 从二进制数据中识别没有扩展名的文件类型

python - 如何识别某个图像何时消失

c++ - 均衡 HSV 图像的直方图

python - Tensorflow:如何对矩阵的每个元素执行操作

C++ 等效于 Tensorflow 中的 python : tf. Graph.get_tensor_by_name()?

python beautiful-soap json - 抓取一页但不抓取其他类似的页面

python - 使用 BeautifulSoup 访问 javascript 变量

python - 额外学分 3,艰难地学习 Python 练习 17

python - 如何在 OpenCV 中计算光流幅度