4

我是 Keras 的新手,我有一个包含多个文件夹的数据集,每个文件夹都指向一个特定的类。我使用 ImageDataGenerator 从子文件夹中读取数据。我正在尝试使用 16 个大小为 80x100 的连续帧,因此 input_shape 为 (16, 80, 100, 1)。当我进行训练时,网络输入出现错误,我知道输入应该是 3D CNN 的 5d 张量,但我不确定我是否正确执行此操作。

我正在使用 spyder 编写和实现代码:

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, Conv3D, MaxPooling3D
from keras.layers.advanced_activations import LeakyReLU
from keras.optimizers import SGD, RMSprop
from keras.utils import np_utils, generic_utils
from keras.losses import categorical_crossentropy
from keras.optimizers import Adam
import os
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import cv2
from sklearn.cross_validation import train_test_split
from sklearn import cross_validation
from sklearn import preprocessing

datagen = ImageDataGenerator()
train_data=datagen.flow_from_directory('C:\\Users\\AA\\Data\\Training', target_size=(80, 100), color_mode='grayscale', classes=None, class_mode='categorical', batch_size=32, interpolation='nearest')
test_data=datagen.flow_from_directory('C:\\Users\\AA\\Data\\Testing', target_size=(80, 100), color_mode='grayscale', classes=None, class_mode='categorical', batch_size=32, interpolation='nearest')



    ins = (16, 80, 100, 1)
    model = Sequential()
    model.add(Conv3D(32, kernel_size=(3, 3, 3), input_shape=ins, border_mode='same'))
    model.add(Activation('relu'))
    model.add(Conv3D(32, kernel_size=(3, 3, 3), border_mode='same'))
    model.add(Activation('softmax'))
    model.add(MaxPooling3D(pool_size=(3, 3, 3), border_mode='same'))
    model.add(Dropout(0.25))

    model.add(Conv3D(64, kernel_size=(3, 3, 3), border_mode='same'))
    model.add(Activation('relu'))
    model.add(Conv3D(64, kernel_size=(3, 3, 3), border_mode='same'))
    model.add(Activation('softmax'))
    model.add(MaxPooling3D(pool_size=(3, 3, 3), border_mode='same'))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(512, activation='sigmoid'))
    model.add(Dropout(0.5))
    model.add(Dense(8, activation='softmax'))


 model.compile(loss=categorical_crossentropy, optimizer=Adam(), metrics=['accuracy'])    


   model.fit_generator(train_data,
        steps_per_epoch=2000,
        epochs=50,
        validation_data=test_data,
        validation_steps=800)

错误说:

File "C:\Users\AA\AppData\Local\Continuum\Anaconda3\lib\site-packages\keras\engine\training.py", line 113, in _standardize_input_data
    'with shape ' + str(data_shape))

ValueError: Error when checking input: expected conv3d_24_input to have 5 dimensions, but got array with shape (32, 80, 100, 1)
4

3 回答 3

1

我认为问题出在ImageDataGenerator.

它仅适用于图像,不适用于视频(我遇到了同样的错误,在https://github.com/keras-team/keras/issues/10150,他们还声称ImageDataGenerator仅适用于图像形张量。他们也建议实现您自己的数据生成,如https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly.html中所述,但我自己没有尝试过)

于 2018-12-02T19:04:18.977 回答
1

https://gist.github.com/Emadeldeen-24/736c33ac2af0c00cc48810ad62e1f54a

这是用于 Conv3D 网络的 5D 输入的自定义图像数据生成器。希望能帮助到你。

from tweaked_ImageGenerator_v2 import ImageDataGenerator
datagen = ImageDataGenerator()
train_data=datagen.flow_from_directory('path/to/data', target_size=(x, y), batch_size=32, frames_per_step=4)
于 2019-03-05T19:01:05.850 回答
0

该模型将您的输入解释为 16 个 80x100 灰度图像样本。您必须将输入重塑为

(no_of_samples,16,80,100,1)

这里 16 是你的时间步长

于 2018-05-08T16:53:57.520 回答