查看原文
其他

【调参实战】BN和Dropout对小模型有什么影响?全局池化相比全连接有什么劣势?

言有三 有三AI 2020-09-07

大家好,欢迎来到专栏《调参实战》,虽然当前自动化调参研究越来越火,但那其实只是换了一些参数来调,对参数的理解和调试在机器学习相关任务中是最基本的素质,在这个专栏中我们会带领大家一步一步理解和学习调参。


本次主要讲述图像分类项目中的BN层和Drouout层的调参对比实践,以及全连接层和池化层的对比实践


作者&编辑 | 言有三

本文资源与结果展示

本文篇幅:3000字

背景要求:会使用Python和任一深度学习开源框架

附带资料:Caffe代码和数据集一份

同步平台:有三AI知识星球(一周内)

1 项目背景与准备工作

在卷积神经网络的设计中,早期出现的Dropout层可以降低模型过拟合的风险,增强模型的泛化性能。而随着Batch Normalization层的出现,Dropout逐渐被代替,Batch Normalization层不仅可以加速模型的训练,还在一定程度上缓解了模拟的过拟合风险。


与之类似,全连接层和全局池化层也是一对冤家,最早期的时候,对于分类任务来说网络最后层都是全连接层,但是因为它的参数量巨大,导致后来被全局池化层替代,那替换就一定是带来正向的结果吗?会不会有什么副作用?


这一期我们来对以上问题进行实践,本次项目开发需要以下环境:


(1) Linux系统,推荐ubuntu16.04或者ubuntu18.04。使用windows系统也可以完成,但是使用Linux效率更高。

(2) 最好拥有一块显存不低于6G的GPU显卡,如果没有使用CPU进行训练速度较慢。

(3) 安装好的Caffe开源框架。

2 Dropout和BN层实践

下面我们首先对Dropout和BN层进行实践,如果对这两者的理解不熟悉的,请查看往期文章:

【AI初识境】深度学习模型中的Normalization,你懂了多少?

【AI初识境】被Hinton,DeepMind和斯坦福嫌弃的池化,到底是什么?


本次的数据集和基准模型与上一期内容相同,大家如果不熟悉就去查看上一期的内容,链接如下:

【调参实战】如何开始你的第一个深度学习调参任务?不妨从图像分类中的学习率入手。

【调参实战】那些优化方法的性能究竟如何,各自的参数应该如何选择?


2.1 Dropout层


首先我们给基准模型添加Dropout层,它通常是被添加在网络靠后的位置,我们将其添加到conv5层后面,得到的模型结构如下:

完整的结构配置如下:

layer {

   name: "data"

   type: "ImageData"

   top: "data"

   top: "label"

   include {

     phase: TRAIN

   }

   transform_param {

     mirror: true

     crop_size: 224

     mean_value: 104.0

     mean_value: 117.0

     mean_value: 124.0

   }

   image_data_param {

     source: "list_train_shuffle.txt"

     batch_size: 64

     shuffle: true

     new_height: 256

     new_width: 256

   }

 }


layer {

   name: "data"

   type: "ImageData"

   top: "data"

   top: "label"

   include {

     phase: TEST

   }

   transform_param {

     mirror: false

     crop_size: 224

     mean_value: 104.0

     mean_value: 117.0

     mean_value: 124.0

   }

   image_data_param {

     source: "list_val_shuffle.txt"

     batch_size: 64

     shuffle: false

     new_height: 224

     new_width: 224

   }

 }


layer {

  bottom: "data"

  top: "conv1"

  name: "conv1"

  type: "Convolution"

  param {

    lr_mult: 1

    decay_mult: 1

  }

  param {

    lr_mult: 2

    decay_mult: 0

  }

  convolution_param {

    num_output: 64

    pad: 1

    kernel_size: 3    

    stride: 2

    weight_filler {

      type: "gaussian"

      std: 0.01

    }

    bias_filler {

      type: "constant"

      value: 0

    }

  }

}

layer {

  bottom: "conv1"

  top: "conv1"

  name: "relu1"

  type: "ReLU"

}

layer {

  bottom: "conv1"

  top: "conv2"

  name: "conv2"

  type: "Convolution"

  param {

    lr_mult: 1

    decay_mult: 1

  }

  param {

    lr_mult: 2

    decay_mult: 0

  }

  convolution_param {

    num_output: 64

    pad: 1

    kernel_size: 3

    stride: 2

    weight_filler {

      type: "gaussian"

      std: 0.01

    }

    bias_filler {

      type: "constant"

      value: 0

    }

  }

}

layer {

  bottom: "conv2"

  top: "conv2"

  name: "relu2"

  type: "ReLU"

}


layer {

  bottom: "conv2"

  top: "conv3"

  name: "conv3"

  type: "Convolution"

  param {

    lr_mult: 1

    decay_mult: 1

  }

  param {

    lr_mult: 2

    decay_mult: 0

  }

  convolution_param {

    num_output: 128

    pad: 1

    kernel_size: 3

    stride: 2

    weight_filler {

      type: "gaussian"

      std: 0.01

    }

    bias_filler {

      type: "constant"

      value: 0

    }

  }

}

layer {

  bottom: "conv3"

  top: "conv3"

  name: "relu3"

  type: "ReLU"

}

layer {

  bottom: "conv3"

  top: "conv4"

  name: "conv4"

  type: "Convolution"

  param {

    lr_mult: 1

    decay_mult: 1

  }

  param {

    lr_mult: 2

    decay_mult: 0

  }

  convolution_param {

    num_output: 128

    pad: 1

    stride: 2

    kernel_size: 3

    weight_filler {

      type: "gaussian"

      std: 0.01

    }

    bias_filler {

      type: "constant"

      value: 0

    }

  }

}

layer {

  bottom: "conv4"

  top: "conv4"

  name: "relu4"

  type: "ReLU"

}

layer {

  bottom: "conv4"

  top: "conv5"

  name: "conv5"

  type: "Convolution"

  param {

    lr_mult: 1

    decay_mult: 1

  }

  param {

    lr_mult: 2

    decay_mult: 0

  }

  convolution_param {

    num_output: 256

    pad: 1

    stride: 2

    kernel_size: 3

    weight_filler {

      type: "gaussian"

      std: 0.01

    }

    bias_filler {

      type: "constant"

      value: 0

    }

  }

}

layer {

  bottom: "conv5"

  top: "conv5"

  name: "relu5"

  type: "ReLU"

}


layer {

  name: "drop"

  type: "Dropout"

  bottom: "conv5"

  top: "conv5"

  dropout_param {

    dropout_ratio: 0.5

  }

}


layer {

    bottom: "conv5"

    top: "pool5"

    name: "pool5"

    type: "Pooling"

    pooling_param {

        kernel_size: 7

        stride: 1

        pool: AVE

    }

}


layer {

  bottom: "pool5"

  top: "fc"

  name: "fc"

  type: "InnerProduct"

    inner_product_param {

        num_output: 20

        weight_filler {

            type: "xavier"

        }

        bias_filler {

            type: "constant"

            value: 0

        }

    }

}



layer {

  name: "accuracy_at_1"

  type: "Accuracy"

  bottom: "fc"

  bottom: "label"

  top: "accuracy_at_1"

  accuracy_param {

    top_k: 1

  }

}

layer {

  name: "accuracy_at_5"

  type: "Accuracy"

  bottom: "fc"

  bottom: "label"

  top: "accuracy_at_5"

  accuracy_param {

    top_k: 5

  }

}

layer {

  bottom: "fc"

  bottom: "label"

  top: "loss"

  name: "loss"

  type: "SoftmaxWithLoss"

}


我们试验了两个不同比率,即Dropout=0.5和Dropout=0.9,优化参数配置如下:

net: "allconv6.prototxt"

test_interval:100

test_iter:15

base_lr: 0.01

lr_policy: "step"

stepsize: 10000

gamma: 0.1

momentum: 0.9

weight_decay: 0.005

display: 100

max_iter: 100000

snapshot: 10000

snapshot_prefix: "models/allconv6_"

solver_mode: GPU


其与基准模型试验结果对比如下:

可以看出,添加Dropout之后,模型明显要稳定很多,但是其性能稍微有所下降,这是因为基准模型本身就比较小,Dropout会降低模型的容量。Dropout=0.5和Dropout=0.9时性能差不多,这都是比较常用的配置,更小的比率预期会进一步降低模型的性能,大家可以进行尝试。


2.2 BN层

微信扫一扫付费阅读全部

    Preview the first 66% of the content for free.

    微信扫一扫付费阅读全部

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存