我想在CIFAR-100上从头开始训练MobileNetV2,并且得到以下结果,但一段时间后它停止学习。
这是我的代码。我希望看到至少60-70%的验证准确性,并且我想知道是否必须在imagenet上对其进行预培训,或者是否因为CIFAR100仅为32x32x3? 由于某些限制,我将Keras 2.2.4与tensorflow 1.12.0一起使用。
from keras.applications.mobilenet_v2 import MobileNetV2
[..]
(x_train, y_train), (x_test, y_test) = cifar100.load_data()
x_train = x_train / 255
x_test = x_test / 255
y_train = np_utils.to_categorical(y_train, 100)
y_test = np_utils.to_categorical(y_test, 100)
input_tensor = Input(shape=(32,32,3))
x = MobileNetV2(include_top=False,
weights=None,
classes=100)(input_tensor)
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
preds = Dense(100, activation='softmax')(x)
model = Model(inputs=[input_tensor], outputs=[preds])
optimizer = Adam(lr=1e-3)
model.compile(loss="categorical_crossentropy",
optimizer=optimizer,
metrics=['accuracy'])
epochs = 300
batch_size = 64
callbacks = [ReduceLROnPlateau(monitor='val_loss', factor=np.sqrt(0.1), cooldown=0, patience=10, min_lr=1e-6)]
generator = ImageDataGenerator(rotation_range=15,
width_shift_range=5. / 32,
height_shift_range=5. / 32,
horizontal_flip=True)
generator.fit(x_train)
model.fit_generator(generator.flow(x_train, y_train),
validation_data=(x_test, y_test),
steps_per_epoch=(len(x_train) // batch_size),
epochs=epochs, verbose=1,
callbacks=callbacks)
答案 0 :(得分:2)
好吧,MobileNets
和所有其他基于imagenet的模型将图像下采样5次(224-> 7),然后进行GlobalAveragePooling2D
,然后进行输出层。
我认为直接在这些模型上使用32 * 32图像不会给您带来良好的效果,因为张量形状甚至在GlobalAveragePooling2D
之前都是1 * 1。
也许您应该尝试调整图像的大小以使其适合96*96或删除第一个stride=2
。以NASNet paper作为参考,他们在Cifar版本和ImageNet版本中都使用4个池,而在第一卷积层中只有ImageNet版本具有stride=2
。