zoukankan      html  css  js  c++  java
  • .fit VS .fit_generator in Keras

    For small, simplistic datasets it’s perfectly acceptable to use Keras’ .fit function.

    These datasets are often not very challenging and do not require any data augmentation.

    However, real-world datasets are rarely that simple:

    • Real-world datasets are often too large to fit into memory.
    • They also tend to be challenging, requiring us to perform data augmentation to avoid overfitting and increase the ability of our model to generalize.

    In those situations we need to utilize Keras’ .fit_generator function:

    # initialize the number of epochs and batch size
    EPOCHS = 100
    BS = 32
    
    # construct the training image generator for data augmentation
    aug = ImageDataGenerator(rotation_range=20, zoom_range=0.15,
    	width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15,
    	horizontal_flip=True, fill_mode="nearest")
    
    # train the network
    H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS),
    	validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS,
    	epochs=EPOCHS)
    
    

    Here we start by first initializing the number of epochs we are going to train our network for along with the batch size.

    We then initialize aug , a Keras ImageDataGenerator object that is used to apply data augmentation, randomly translating, rotating, resizing, etc. images on the fly.

    Performing data augmentation is a form of regularization, enabling our model to generalize better.

    However, applying data augmentation implies that our training data is no longer “static” — the data is constantly changing.

    Each new batch of data is randomly adjusted according to the parameters supplied to ImageDataGenerator.

    Thus, we now need to utilize Keras’ .fit_generator function to train our model.

    As the name suggests, the .fit_generator function assumes there is an underlying function that is generating the data for it.

    The function itself is a Python generator.

    Internally, Keras is using the following process when training a model with .fit_generator:

    1. Keras calls the generator function supplied to .fit_generator (in this case, aug.flow ).
    2. The generator function yields a batch of size BS to the .fit_generator function.
    3. The .fit_generator function accepts the batch of data, performs backpropagation, and updates the weights in our model.
      This process is repeated until we have reached the desired number of epochs.
      You’ll notice we now need to supply a steps_per_epoch parameter when calling .fit_generator (the .fit method had no such parameter).

    Why do we need steps_per_epoch ?

    Keep in mind that a Keras data generator is meant to loop infinitely — it should never return or exit.

    Since the function is intended to loop infinitely, Keras has no ability to determine when one epoch starts and a new epoch begins.

    Therefore, we compute the steps_per_epoch value as the total number of training data points divided by the batch size. Once Keras hits this step count it knows that it’s a new epoch.

  • 相关阅读:
    js进阶 12-7 pageY和screenY以及clientY的区别是什么
    printf交替使用
    【iOS发展-81】setNeedsDisplay刷新显卡,并CADisplayLink它用来模拟计时器效果
    android tv 全屏幕垂直画
    uva 10305
    (札记)Java应用架构设计-模块化模式与OSGi
    《学习opencv》笔记——矩阵和图像处理——cvAnd、cvAndS、cvAvg and cvAvgSdv
    一旦专利
    SharePoint 2010 升级到2013时间 为了确保用户可以连接,但无法改变升级数据
    [TroubleShooting] The remote copy of database xx has not been rolled forward to a point in time
  • 原文地址:https://www.cnblogs.com/larkiisready/p/11681611.html
Copyright © 2011-2022 走看看