python-3.x - ValueError : Input to `.fit()` should have rank 4. 得到形状为 : (10, 20, 50, 50, 1) 的数组

标签 python-3.x numpy image-processing keras conv-neural-network

我的模型

model.add(Conv3D(nb_filters[0], kernel_dim1=nb_conv[0], 
kernel_dim2=nb_conv[0], kernel_dim3=nb_conv[0],
input_shape=(20, 50, 50,1), activation='relu'))
model.add(MaxPooling3D(pool_size=(nb_pool[0], nb_pool[0], nb_pool[0])))
model.add(Conv3D(nb_filters[1], kernel_dim1=nb_conv[0], 
kernel_dim2=nb_conv[0], kernel_dim3=nb_conv[0],activation='relu'))
model.add(MaxPooling3D(pool_size=(nb_pool[0], nb_pool[0], nb_pool[0])))
model.add(Dropout(0.8))
model.add(Flatten())
model.add(Dense(1024, init='normal', activation='relu'))
model.add(Dropout(0.8))
model.add(Dense(nb_classes,init='normal'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adadelta',
metrics['accuracy'])

我的输入 numpy 数组
train_data=np.load('D:/muchdata-50-50-20.npy')
train=train_data[-10:]
test=train_data[-2:]
train1 = np.array([i[0] for i in train]).reshape(-1,20,50,50,1)
y_train =  [i[1] for i in train]
testx=np.array([i[0] for i in test]).reshape(-1,20,50,50,1)
testy=[i[1] for i in test]

这是预处理功能
datagen = ImageDataGenerator(featurewise_center=True,
featurewise_std_normalization=True,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)

这是合适的
datagen.fit(train1)

当我尝试运行时,我收到这样的错误 enter image description here

最佳答案

对于图像数据生成器,图像形状应设置为 4 级。因此,我认为您的 reshape 命令应该更改(我的建议)。用于整形 -1 ,这实际上意味着“将多维转换为一维”。

关于python-3.x - ValueError : Input to `.fit()` should have rank 4. 得到形状为 : (10, 20, 50, 50, 1) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43730635/

相关文章:

python - 如何在没有错误 :list index out of range 的情况下检查列表

Python:将 'one-line if' s 替换为三元 if,好主意与否

python - 使用python使用复制方法将json str插入postgresql时如何修复 "invalid input syntax for type json"

python - Numpy "inner join"带重复键

matlab - 旋转圆形图像

python - 干草堆:stats()和stats_results()不起作用

python - 摆脱天体中的单位

python - Cython 优化 numpy 数组求和的关键部分

python - 为什么PIL的Image.fromarray()会使我的图像颜色失真?

c++ - imwrite 不创建图像