带MobiletNetV2的cifar100

2024-04-26 05:40:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用keras.应用 这是我的密码:

(x_train,y_train),(x_test,y_test) = tf.keras.datasets.cifar100.load_data(label_mode='fine')

x_test = x_test.astype("float32")
x_train = x_train.astype("float32")

x_test /=255
x_train /=255

y_test = tf.keras.utils.to_categorical(y_test,100)
y_train = tf.keras.utils.to_categorical(y_train,100)
model = MobileNetV2(input_shape=(32,32,3),
                    alpha=1.0,
                    include_top=True,
                    weights=None,
                    classes=100)
epochs = 200
batch_size = 64
print('Using real-time data augmentation.')
    # This will do preprocessing and realtime data augmentation:
datagen = ImageDataGenerator(
        featurewise_center=False,  # set input mean to 0 over the dataset
        samplewise_center=False,  # set each sample mean to 0
        featurewise_std_normalization=False,  # divide inputs by std of the dataset
        samplewise_std_normalization=False,  # divide each input by its std
        zca_whitening=False,  # apply ZCA whitening
        zca_epsilon=1e-06,  # epsilon for ZCA whitening
        rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)
        # randomly shift images horizontally (fraction of total width)
        width_shift_range=0.1,
        # randomly shift images vertically (fraction of total height)
        height_shift_range=0.1,
        shear_range=0.,  # set range for random shear
        zoom_range=0.,  # set range for random zoom
        channel_shift_range=0.,  # set range for random channel shifts
        # set mode for filling points outside the input boundaries
        fill_mode='nearest',
        cval=0.,  # value used for fill_mode = "constant"
        horizontal_flip=True,  # randomly flip images
        vertical_flip=False,  # randomly flip images
        # set rescaling factor (applied before any other transformation)
        rescale=None,
        # set function that will be applied on each input
        preprocessing_function=None,
        # image data format, either "channels_first" or "channels_last"
        data_format=None,
        # fraction of images reserved for validation (strictly between 0 and 1)
        validation_split=0.0)
datagen.fit(x_train)

model.compile(optimizer='adam', loss=tf.keras.losses.categorical_crossentropy, metrics=['acc'])
history = model.fit_generator(datagen.flow(x_train, y_train,batch_size=batch_size),
                        epochs=epochs,
                        validation_data=(x_test, y_test)
                             )

问题在于验证的准确性,经过200个周期后,acc几乎达到了40%。 我试图微调优化器/损失参数,但仍然是一样的。我猜输入的尺寸对于模型来说太小了,因为默认值是224*224,但是根据文档,你可以使用任何你想要的东西!你知道吗

有什么建议吗?(我不想把cifar100的dim改成224*224,因为与这个实验有关的一些假设)!你知道吗


Tags: totestfalseforinputdatashiftmode
1条回答
网友
1楼 · 发布于 2024-04-26 05:40:48

我想到的一些事情。。。你知道吗

  • 检查数据扩充管道(datagen)。它可能是扭曲输入太多,因此模型可能是学习奇怪的东西,而不是学习分类的图像
  • 还要检查训练的准确性。。。比验证好吗?多少钱?你知道吗
  • 对我来说,32x32是小,但我认为你应该即使得到更高的精度。。。你知道吗

相关问题 更多 >