在Keras中,如果我改变增广的话,模型精度又从0开始

2024-06-16 11:34:56 发布

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

我正在用googlecolab中的Keras训练一个DL模型。在训练模型时,如果我更改图像增强参数(或)如果我更改图像增强,则精度将再次从0开始,尽管之前的精度为任何值(20%或40%)。所以,我被迫使用相同的增强技术,并得到了

model.fit(X_train_new, Y_train,
          batch_size=128,
          nb_epoch=20,
          validation_data=(X_test_new, Y_test),
          shuffle=True,
          callbacks=callbacks)

输出:

Epoch 20/20
100000/100000 [==============================] - 231s 2ms/step - loss: 3.5002 - acc: 0.2198 - val_loss: 3.9516 - val_acc: 0.1587

Epoch 00020: val_acc improved from 0.15840 to 0.15870, saving model to /content/gdrive/My Drive/Colab Notebooks/Ultimate/weights-improvement-20-0.16.h5
<keras.callbacks.History at 0x7f2ec2f1a1d0>

现在,当我保存并加载模型时(或者如果我继续,甚至不加载模型):

from keras.models import load_model
model.save('/content/gdrive/My Drive/Colab Notebooks/Ultimate/model-cell-1-final.h5')

#del model
!ls '/content/gdrive/My Drive/Colab Notebooks/Ultimate/'
from keras.models import load_model
model = load_model('/content/gdrive/My Drive/Colab Notebooks/Ultimate/model-cell-1-final1.h5')

from imgaug import augmenters as iaa

#seq = iaa.Sequential([...])  # list of desired augmentors

seq = iaa.Sequential([
    iaa.Fliplr(0.5), # horizontal flips
    iaa.Crop(percent=(0, 0.1)), # random crops
    # Small gaussian blur with random sigma between 0 and 0.5.
    # But we only blur about 50% of all images.
    iaa.Sometimes(0.5,
        iaa.GaussianBlur(sigma=(0, 0.5))
    ),
    # Strengthen or weaken the contrast in each image.
    iaa.ContrastNormalization((0.75, 1.5)),
    # Add gaussian noise.
    # For 50% of all images, we sample the noise once per pixel.
    # For the other 50% of all images, we sample the noise per pixel AND
    # channel. This can change the color (not only brightness) of the
    # pixels.
    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
    # Make some images brighter and some darker.
    # In 20% of all cases, we sample the multiplier once per channel,
    # which can end up changing the color of the images.
    iaa.Multiply((0.8, 1.2), per_channel=0.2),
    # Apply affine transformations to each image.
    # Scale/zoom them, translate/move them, rotate them and shear them.
    iaa.Affine(
        scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
        translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
        rotate=(-25, 25),
        shear=(-8, 8)
    )
], random_order=True) # apply augmenters in random order


ig = ImageDataGenerator(preprocessing_function=seq.augment_image)  # pass this as the preprocessing function

#gen = ig.flow_from_directory(data_dir)  # nothing else changes with the generator

ig.fit(X_train_new)

#Train our model using ImgAug Augmentation
model.fit_generator(ig.flow(X_train_new, Y_train, batch_size=128),
                        steps_per_epoch=200,
                        epochs=20,
                        verbose=1,     
                        validation_data=(X_test_new, Y_test),
                        callbacks=callbacks)

它又从0%的准确度开始

Epoch 1/20
200/200 [==============================] - 97s 487ms/step - loss: 5.6513 - acc: 0.0152 - val_loss: 5.2551 - val_acc: 0.0213

Epoch 00001: val_acc improved from -inf to 0.02130, saving model to /content/gdrive/My Drive/Colab Notebooks/Ultimate/weights-improvement-01-0.02.h5
WARNING:tensorflow:From <ipython-input-13-69a262e916e5>:21: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
 - LR: 0.033921

即使我使用本机ImageDatagenerator的增强技术,并且不知道如何解决这个问题,也会发生这种情况


Tags: ofthetofromnewmodelmytrain