我是 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)