While training pretrained model using Keras in such a way:
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))
I have an error:
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)
I've tryed other losses, but this didn't help. batchClasses has shape (batchSize, NUMCLASSES) = (64, 50) and I expect this shape in the output of Dense.
The
MaxPooling2D()
does not remove the width and height dimensions, so the output oft = MaxPooling2D()(t)
will be a tensor of shape(batch_size, w, h, 2048)
. That's why the followingDense
layer gives you a 4D tensor.Also, since you did not provide any argument to
MaxPooling2D()
, with the default argumentpool_size=(2, 2)
, it's likely that bothw
andh
are larger than 1.So you have basically two options, depends on what you consider to be more appropriate on your problem:
Add
Flatten()
afterMaxPooling2D()
: I'm not sure if this is what you want, since flatten it will result in a quite large vector ifw
andh
are large.Remove
t = MaxPooling2D()(t)
and use either:ResNet50(..., pooling='max')
(recommended), ort = GlobalMaxPooling2D()(t)