gpt4 book ai didi

python - Keras ImageDataGenerator 流目录与 3D CNN 数据格式错误?

转载 作者:行者123 更新时间:2023-11-28 17:07:18 27 4
gpt4 key购买 nike

我是 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 中所述,但我自己还没有尝试过)

关于python - Keras ImageDataGenerator 流目录与 3D CNN 数据格式错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50232685/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com