我正在处理图像分类问题,因为我的数据集太大而无法一次处理。因此,我保存了ResNet50模型并再次加载它以训练更多数据。第一次尝试100次后,我的准确率达到了95%左右,但是当我保存并再次训练下一组数据时,它的准确率下降到25%。这是代码:
img_input = Input(shape=(224, 224, 3))
model = ResNet50(input_tensor=img_input, include_top=True)
last_layer = model.get_layer('avg_pool').output
x = Flatten(name='flatten')(last_layer)
out = Dense(num_classes, activation='softmax', name='output_layer')(x)
resnet_model = Model(inputs=img_input, outputs=out)
for layer in resnet_model.layers[:-1]:
layer.trainable = False
resnet_model.layers[-1].trainable
resnet_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
t = time()
hist = resnet_model.fit(x_t, y_t, batch_size=32, epochs=100, verbose=1, validation_data=(x_v, y_v))
print('training time %s' % (t- time()))
(loss, acc) = resnet_model.evaluate(x_v, y_v, batch_size=10, verbose=1)
print('loss={:.4f}, accuracy: {:.4f}%'.format(loss,acc * 100))
下面是我如何在新数据集上加载和运行模型。
resnet_model = load_model('./../data/resnet_model.h5')
resnet_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
t = time()
hist = resnet_model.fit(x_t, y_t, batch_size=32, epochs=100, verbose=1, validation_data=(x_v, y_v))
print('training time %s' % (t- time()))
(loss, acc) = resnet_model.evaluate(x_v, y_v, batch_size=10, verbose=1)
print('loss={:.4f}, accuracy: {:.4f}%'.format(loss,acc * 100))