我是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)
我认为问题是ImageDataGenerator
。
它只适用于图像,而不适用于视频(我得到了相同的错误,在https://github.com/keras-team/keras/issues/10150,他们也声称ImageDataGenerator
只适用于图像形状的张量。在那里他们还建议实现你自己的数据生成,如https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly.html中描述的,但我我自己没试过)
https://gist.github.com/Emadeldeen-24/736c33ac2af0c00cc48810ad62e1f54a
这是一个自定义imagedatagenerator,用于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)
该模型将您的输入解释为16个80x100灰度图像样本。您必须将输入重新整形为
(no_of_samples,16,80,100,1)
这里有16个是你的时间步