[英]ValueError: Error when checking target: expected dense_2 to have shape (1,) but got array with shape (50,)
[英]ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (64, 50) (Keras)
使用Keras以这种方式训练预训练模型时:
baseModel = keras.applications.resnet50.ResNet50(include_top=False, weights='imagenet')
t = baseModel.output
t = MaxPooling2D()(t)
t = Dense(1000, activation='relu', kernel_regularizer=regularizers.l2(0.01))(t)
predictions = Dense(NUMCLASSES, activation='softmax')(t)
model = Model(inputs=baseModel.input, outputs=predictions)
for layer in baseModel.layers:
layer.trainable = False
model.compile(loss=losses.categorical_crossentropy, optimizer=keras.optimizers.Adam())
# loading the data
files = np.array(list(train_gt.keys()))
np.random.shuffle(files)
pics = [resize(io.imread(join(trainImgDir, f)), INPUTSHAPE, mode='reflect') for f in files]
pics = np.array(pics)
classes = np.array([train_gt[f] for f in files])
classes = to_categorical(classes, NUMCLASSES)
train = pics[: int(pics.shape[0] * ((SPLITDATA - 1) / SPLITDATA))]
classesTr = classes[: int(classes.shape[0] * ((SPLITDATA - 1) / SPLITDATA))]
# training
fIn = open("Error", 'w')
batchSize = 64
for ep in range(1000):
# train data
trLosses = np.array([], dtype='Float64')
for s in range(train.shape[0] // batchSize + (train.shape[0] % batchSize != 0)):
batch = train[s * batchSize : (s + 1) * batchSize]
batchClasses = classesTr[s * batchSize : (s + 1) * batchSize]
trLosses = np.append(trLosses, model.train_on_batch(batch, batchClasses))
我有一个错误:
File "/home/mark/miniconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1636, in train_on_batch
check_batch_axis=True)
File "/home/mark/miniconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1315, in _standardize_user_data
exception_prefix='target')
File "/home/mark/miniconda3/lib/python3.6/site-packages/keras/engine/training.py", line 127, in _standardize_input_data
str(array.shape))
ValueError: Error when checking target: expected dense_2 to have 4 dimensions, but got array with shape (64, 50)
我尝试了其他损失,但这没有帮助。 batchClasses具有形状(batchSize,NUMCLASSES)=(64,50),我希望在Dense的输出中出现这种形状。
MaxPooling2D()
不会删除宽度和高度尺寸,因此t = MaxPooling2D()(t)
的输出将是形状的张量(batch_size, w, h, 2048)
。 这就是为什么下面的“ Dense
层为您提供4D张量的原因。
另外,由于没有为MaxPooling2D()
提供任何参数,默认参数pool_size=(2, 2)
,因此w
和h
都可能大于1。
因此,您基本上有两种选择,取决于您认为更适合自己的问题的方式:
在MaxPooling2D()
Flatten()
之后添加Flatten()
:我不确定这是否是您想要的,因为如果w
和h
较大,则将其展平将导致相当大的向量。
删除t = MaxPooling2D()(t)
并使用以下任一方法:
ResNet50(..., pooling='max')
(推荐),或 t = GlobalMaxPooling2D()(t)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.