当前位置:   article > 正文

ResNet 各个版本的区别_resnet50 resnet101 区别

resnet50 resnet101 区别

最近在研究分类问题,提到分类那就是必须要提到ResNet 这个经典的模型了。这个模型也分成了很多个版本。每个版本区别如下

这就是每一个版本ResNet的区别。我们可以看到主要的区别就是每一个卷积的多少的区别。这里又一个keras 的简单实现。

  1. def ResNet50(include_top=True, weights='imagenet',
  2. input_tensor=None, input_shape=None,
  3. pooling=None,
  4. classes=1000):
  5. x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
  6. x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
  7. x = Activation('relu')(x)
  8. x = MaxPooling2D((3, 3), strides=(2, 2))(x)
  9. x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
  10. x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
  11. x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
  12. x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
  13. x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
  14. x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
  15. x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
  16. x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
  17. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
  18. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
  19. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
  20. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
  21. x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
  22. x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
  23. x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
  24. x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

我么可以看到这是一个ResNet50的网络。里面的每一个模块数是(3,4,6,3)。我们可以在代码中具体看到。里面每一个小的模块的代码如下

  1. def identity_block(input_tensor, kernel_size, filters, stage, block):
  2. """The identity block is the block that has no conv layer at shortcut.
  3. # Arguments
  4. input_tensor: input tensor
  5. kernel_size: defualt 3, the kernel size of middle conv layer at main path
  6. filters: list of integers, the filterss of 3 conv layer at main path
  7. stage: integer, current stage label, used for generating layer names
  8. block: 'a','b'..., current block label, used for generating layer names
  9. # Returns
  10. Output tensor for the block.
  11. """
  12. filters1, filters2, filters3 = filters
  13. if K.image_data_format() == 'channels_last':
  14. bn_axis = 3
  15. else:
  16. bn_axis = 1
  17. conv_name_base = 'res' + str(stage) + block + '_branch'
  18. bn_name_base = 'bn' + str(stage) + block + '_branch'
  19. x = Conv2D(filters1, (1, 1), name=conv_name_base + '2a')(input_tensor)
  20. x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)
  21. x = Activation('relu')(x)
  22. x = Conv2D(filters2, kernel_size,
  23. padding='same', name=conv_name_base + '2b')(x)
  24. x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)
  25. x = Activation('relu')(x)
  26. x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)
  27. x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)
  28. x = layers.add([x, input_tensor])
  29. x = Activation('relu')(x)
  30. return x
  31. def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):
  32. """conv_block is the block that has a conv layer at shortcut
  33. # Arguments
  34. input_tensor: input tensor
  35. kernel_size: defualt 3, the kernel size of middle conv layer at main path
  36. filters: list of integers, the filterss of 3 conv layer at main path
  37. stage: integer, current stage label, used for generating layer names
  38. block: 'a','b'..., current block label, used for generating layer names
  39. # Returns
  40. Output tensor for the block.
  41. Note that from stage 3, the first conv layer at main path is with strides=(2,2)
  42. And the shortcut should have strides=(2,2) as well
  43. """
  44. filters1, filters2, filters3 = filters
  45. if K.image_data_format() == 'channels_last':
  46. bn_axis = 3
  47. else:
  48. bn_axis = 1
  49. conv_name_base = 'res' + str(stage) + block + '_branch'
  50. bn_name_base = 'bn' + str(stage) + block + '_branch'
  51. x = Conv2D(filters1, (1, 1), strides=strides,
  52. name=conv_name_base + '2a')(input_tensor)
  53. x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)
  54. x = Activation('relu')(x)
  55. x = Conv2D(filters2, kernel_size, padding='same',
  56. name=conv_name_base + '2b')(x)
  57. x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)
  58. x = Activation('relu')(x)
  59. x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)
  60. x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)
  61. shortcut = Conv2D(filters3, (1, 1), strides=strides,
  62. name=conv_name_base + '1')(input_tensor)
  63. shortcut = BatchNormalization(axis=bn_axis, name=bn_name_base + '1')(shortcut)
  64. x = layers.add([x, shortcut])
  65. x = Activation('relu')(x)
  66. return x

有了这个基本模块,大家就可以多次组合在一起成不同的版本了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/598187
推荐阅读
相关标签
  

闽ICP备14008679号