keras模型错误:预期density_53具有2维,但数组的形状为(1、1、28、28)

时间:2018-11-15 15:13:42

标签: python keras

我有一个模型,尽管我有一个平坦的层,但它总是给我错误:

ValueError: Error when checking target: expected dense_53 to have 2 dimensions, but got array with shape (1, 1, 28, 28)

此外,如果删除密集和平整的图层,则会给maxPooling和卷积带来相同的错误。这是我的整个代码:

import os 
import numpy as np 
from keras.preprocessing import image
from keras import backend as K

from keras.callbacks import EarlyStopping
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D
from keras.optimizers import Adam
from keras.layers.pooling import MaxPooling2D
from keras.utils import to_categorical

PATH = os.getcwd()

train_path = PATH+'/Downloads/KerasStuff/train_data/rice/'
train_batch = os.listdir(train_path)
x_train = np.empty((0,32,32,3))

# if data are in form of images
for sample in train_batch:
 img_path = train_path+sample
 if img_path == '/Users/23athreyad/Downloads/KerasStuff/train_data/rice/.DS_Store':
  print("INVALID")
 else:
  x = image.load_img(img_path)
  np.append(x_train,x)

test_path = PATH+'/Downloads/KerasStuff/test_data/rice/'
test_batch = os.listdir(test_path)
x_test = []

for sample in test_batch:
 img_path = test_path+sample
 x = image.load_img(img_path)
 # preprocessing if required
 x_test.append(x)

# Create the model

K.set_image_dim_ordering('tf')

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size = (2,2),data_format="channels_last"))
print(model.output_shape)
model.add(Dropout(0.25))
model.add(Flatten())
print(model.output_shape)
model.add(Dense(7200,activation = 'relu'))

# Compile the model
model.compile(loss='categorical_crossentropy',
              optimizer=Adam(lr=0.0001, decay=1e-6),
              metrics=['accuracy'])
model.fit(x_train,y_train,batch_size = 10, epochs = 20,callbacks=[EarlyStopping(min_delta=0.001, patience=3)])

有人可以帮我吗?我是Keras的新手,已经进行了查找,但未找到任何解决方案。

0 个答案:

没有答案