我是Keras的新手,我有一个包含多个文件夹的数据集,每个文件夹都指向一个特定的类。我使用ImageDataGenerator从子文件夹中读取数据。 我试图使用大小为80x100的16个连续帧,因此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)
答案 0 :(得分: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)
答案 1 :(得分:0)
该模型将您的输入解释为16个80x100灰度图像样本。您必须将输入重新整形为
(no_of_samples,16,80,100,1)
这里有16个你的时间步长
答案 2 :(得分:0)
我认为问题是cellForItemAt
。
它仅适用于图像,不适用于视频(我遇到了同样的错误,并且在https://github.com/keras-team/keras/issues/10150处他们还声称ImageDataGenerator
仅适用于图像形张量。在那里,他们还建议实现您的自己的数据生成(如https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly.html中所述,但我自己没有尝试过)