DL之RetinaNet:基于RetinaNet算法(keras框架)利用resnet50_coco数据集(.h5文件)实现目标检测


无心笑煎蛋
无心笑煎蛋 2022-09-19 14:05:18 51053
分类专栏: 资讯

DL之RetinaNet:基于RetinaNet算法(keras框架)利用resnet50_coco数据集(.h5文件)实现目标检测

相关文章
DL之RetinaNet:RetinaNet算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略 之6、ResNet50RetinaNet在程序中如何实现的?——结构框图详解

输出结果

设计思路

更新……

核心代码

更新……

  1. def __create_pyramid_features(C3, C4, C5, feature_size=256):
  2. """ Creates the FPN layers on top of the backbone features.
  3. 在ResNet基础上创建FPN金字塔特征:参照博客的框架图,输入[C3,C4,C5],返回5个特征级别[P3, P4, P5, P6, P7]
  4. 参考博客:https://yunyaniu.blog.csdn.net/article/details/100010853
  5. Args
  6. C3 : Feature stage C3 from the backbone.
  7. C4 : Feature stage C4 from the backbone.
  8. C5 : Feature stage C5 from the backbone.
  9. feature_size : The feature size to use for the resulting feature levels.
  10. Returns
  11. A list of feature levels [P3, P4, P5, P6, P7].
  12. """
  13. upsample C5 to get P5 from the FPN paper
  14. P5 = keras.layers.Conv2D(feature_size, kernel_size=1, strides=1, padding='same', name='C5_reduced')(C5)
  15. P5_upsampled = layers.UpsampleLike(name='P5_upsampled')([P5, C4])
  16. P5 = keras.layers.Conv2D(feature_size, kernel_size=3, strides=1, padding='same', name='P5')(P5)
  17. add P5 elementwise to C4
  18. P4 = keras.layers.Conv2D(feature_size, kernel_size=1, strides=1, padding='same', name='C4_reduced')(C4)
  19. P4 = keras.layers.Add(name='P4_merged')([P5_upsampled, P4])
  20. P4_upsampled = layers.UpsampleLike(name='P4_upsampled')([P4, C3])
  21. P4 = keras.layers.Conv2D(feature_size, kernel_size=3, strides=1, padding='same', name='P4')(P4)
  22. add P4 elementwise to C3
  23. P3 = keras.layers.Conv2D(feature_size, kernel_size=1, strides=1, padding='same', name='C3_reduced')(C3)
  24. P3 = keras.layers.Add(name='P3_merged')([P4_upsampled, P3])
  25. P3 = keras.layers.Conv2D(feature_size, kernel_size=3, strides=1, padding='same', name='P3')(P3)
  26. "P6 is obtained via a 3x3 stride-2 conv on C5"
  27. P6 = keras.layers.Conv2D(feature_size, kernel_size=3, strides=2, padding='same', name='P6')(C5)
  28. "P7 is computed by applying ReLU followed by a 3x3 stride-2 conv on P6"
  29. P7 = keras.layers.Activation('relu', name='C6_relu')(P6)
  30. P7 = keras.layers.Conv2D(feature_size, kernel_size=3, strides=2, padding='same', name='P7')(P7)
  31. return [P3, P4, P5, P6, P7]
  32. def default_submodels(num_classes, num_anchors):
  33. """ Create a list of default submodels used for object detection.
  34. 两个子模型:目标分类子模型default_classification_model、框回归子模型default_regression_model
  35. The default submodels contains a regression submodel and a classification submodel.
  36. Args
  37. num_classes : Number of classes to use.
  38. num_anchors : Number of base anchors.
  39. Returns
  40. A list of tuple, where the first element is the name of the submodel and the second element is the submodel itself.
  41. """
  42. return [
  43. ('regression', default_regression_model(4, num_anchors)),
  44. ('classification', default_classification_model(num_classes, num_anchors))
  45. ]
  46. def __build_model_pyramid(name, model, features):
  47. """ Applies a single submodel to each FPN level.
  48. 真正的构造金字塔模型
  49. Args
  50. name : Name of the submodel.
  51. model : The submodel to evaluate.
  52. features : The FPN features.
  53. Returns
  54. A tensor containing the response from the submodel on the FPN features.
  55. """
  56. return keras.layers.Concatenate(axis=1, name=name)([model(f) for f in features])
  57. """
  58. The default anchor parameters. 默认的anchors参数,组合以后有9个anchors
  59. """
  60. AnchorParameters.default = AnchorParameters(
  61. sizes = [32, 64, 128, 256, 512],
  62. strides = [8, 16, 32, 64, 128],
  63. ratios = np.array([0.5, 1, 2], keras.backend.floatx()),
  64. scales = np.array([2 ** 0, 2 ** (1.0 / 3.0), 2 ** (2.0 / 3.0)], keras.backend.floatx()),
  65. )
  66. def anchor_targets_bbox(
  67. anchors,
  68. image_group,
  69. annotations_group,
  70. num_classes,
  71. negative_overlap和positive_overlap,根据IOU区分
  72. negative_overlap=0.4,
  73. positive_overlap=0.5
  74. ):
  75. def focal(alpha=0.25, gamma=2.0):
  76. """ Create a functor for computing the focal loss.
  77. Args
  78. alpha: Scale the focal weight with alpha.
  79. gamma: Take the power of the focal weight with gamma.
  80. Returns
  81. A functor that computes the focal loss using the alpha and gamma.
  82. """
  83. def _focal(y_true, y_pred):
  84. """ Compute the focal loss given the target tensor and the predicted tensor.
  85. As defined in https://arxiv.org/abs/1708.02002
  86. Args
  87. y_true: Tensor of target data from the generator with shape (B, N, num_classes).
  88. y_pred: Tensor of predicted data from the network with shape (B, N, num_classes).
  89. Returns
  90. The focal loss of y_pred w.r.t. y_true.
  91. """
  92. labels = y_true[:, :, :-1]
  93. anchor_state = y_true[:, :, -1] -1 for ignore, 0 for background, 1 for object
  94. classification = y_pred
  95. filter out "ignore" anchors
  96. indices = backend.where(keras.backend.not_equal(anchor_state, -1))
  97. labels = backend.gather_nd(labels, indices)
  98. classification = backend.gather_nd(classification, indices)
  99. compute the focal loss
  100. alpha_factor = keras.backend.ones_like(labels) * alpha
  101. alpha_factor = backend.where(keras.backend.equal(labels, 1), alpha_factor, 1 - alpha_factor)
  102. focal_weight = backend.where(keras.backend.equal(labels, 1), 1 - classification, classification)
  103. focal_weight = alpha_factor * focal_weight ** gamma
  104. 定义分类损失: 权重*原来的交叉熵损失
  105. cls_loss = focal_weight * keras.backend.binary_crossentropy(labels, classification)
  106. compute the normalizer: the number of positive anchors
  107. normalizer = backend.where(keras.backend.equal(anchor_state, 1))
  108. normalizer = keras.backend.cast(keras.backend.shape(normalizer)[0], keras.backend.floatx())
  109. normalizer = keras.backend.maximum(keras.backend.cast_to_floatx(1.0), normalizer)
  110. return keras.backend.sum(cls_loss) / normalizer
  111. return _focal
  112. def smooth_l1(sigma=3.0): 框回归损失采用smooth_l1函数
  113. """ Create a smooth L1 loss functor.
  114. Args
  115. sigma: This argument defines the point where the loss changes from L2 to L1.
  116. Returns
  117. A functor for computing the smooth L1 loss given target data and predicted data.
  118. """
  119. sigma_squared = sigma ** 2
  120. def _smooth_l1(y_true, y_pred):
  121. """ Compute the smooth L1 loss of y_pred w.r.t. y_true.
  122. Args
  123. y_true: Tensor from the generator of shape (B, N, 5). The last value for each box is the state of the anchor (ignore, negative, positive).
  124. y_pred: Tensor from the network of shape (B, N, 4).
  125. Returns
  126. The smooth L1 loss of y_pred w.r.t. y_true.
  127. """
  128. separate target and state
  129. regression = y_pred
  130. regression_target = y_true[:, :, :-1]
  131. anchor_state = y_true[:, :, -1]
  132. filter out "ignore" anchors
  133. indices = backend.where(keras.backend.equal(anchor_state, 1))
  134. regression = backend.gather_nd(regression, indices)
  135. regression_target = backend.gather_nd(regression_target, indices)
  136. compute smooth L1 loss
  137. f(x) = 0.5 * (sigma * x)^2 if |x| < 1 / sigma / sigma
  138. |x| - 0.5 / sigma / sigma otherwise
  139. regression_diff = regression - regression_target
  140. regression_diff = keras.backend.abs(regression_diff)
  141. regression_loss = backend.where(
  142. keras.backend.less(regression_diff, 1.0 / sigma_squared),
  143. 0.5 * sigma_squared * keras.backend.pow(regression_diff, 2),
  144. regression_diff - 0.5 / sigma_squared
  145. )
  146. compute the normalizer: the number of positive anchors
  147. normalizer = keras.backend.maximum(1, keras.backend.shape(indices)[0])
  148. normalizer = keras.backend.cast(normalizer, dtype=keras.backend.floatx())
  149. return keras.backend.sum(regression_loss) / normalizer
  150. return _smooth_l1

更多输出

  1. Using TensorFlow backend.
  2. 2019-08-27 21:56:31.376015:
  3. __________________________________________________________________________________________________
  4. Layer (type) Output Shape Param Connected to
  5. ==================================================================================================
  6. input_1 (InputLayer) (None, None, None, 3 0
  7. __________________________________________________________________________________________________
  8. padding_conv1 (ZeroPadding2D) (None, None, None, 3 0 input_1[0][0]
  9. __________________________________________________________________________________________________
  10. conv1 (Conv2D) (None, None, None, 6 9408 padding_conv1[0][0]
  11. __________________________________________________________________________________________________
  12. bn_conv1 (BatchNormalization) (None, None, None, 6 256 conv1[0][0]
  13. __________________________________________________________________________________________________
  14. conv1_relu (Activation) (None, None, None, 6 0 bn_conv1[0][0]
  15. __________________________________________________________________________________________________
  16. pool1 (MaxPooling2D) (None, None, None, 6 0 conv1_relu[0][0]
  17. __________________________________________________________________________________________________
  18. res2a_branch2a (Conv2D) (None, None, None, 6 4096 pool1[0][0]
  19. __________________________________________________________________________________________________
  20. bn2a_branch2a (BatchNormalizati (None, None, None, 6 256 res2a_branch2a[0][0]
  21. __________________________________________________________________________________________________
  22. res2a_branch2a_relu (Activation (None, None, None, 6 0 bn2a_branch2a[0][0]
  23. __________________________________________________________________________________________________
  24. padding2a_branch2b (ZeroPadding (None, None, None, 6 0 res2a_branch2a_relu[0][0]
  25. __________________________________________________________________________________________________
  26. res2a_branch2b (Conv2D) (None, None, None, 6 36864 padding2a_branch2b[0][0]
  27. __________________________________________________________________________________________________
  28. bn2a_branch2b (BatchNormalizati (None, None, None, 6 256 res2a_branch2b[0][0]
  29. __________________________________________________________________________________________________
  30. res2a_branch2b_relu (Activation (None, None, None, 6 0 bn2a_branch2b[0][0]
  31. __________________________________________________________________________________________________
  32. res2a_branch2c (Conv2D) (None, None, None, 2 16384 res2a_branch2b_relu[0][0]
  33. __________________________________________________________________________________________________
  34. res2a_branch1 (Conv

网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。

本文链接:https://www.xckfsq.com/news/show.html?id=2857
赞同 0
评论 0 条
无心笑煎蛋L0
粉丝 0 发表 4 + 关注 私信
上周热门
如何使用 StarRocks 管理和优化数据湖中的数据?  2935
【软件正版化】软件正版化工作要点  2854
统信UOS试玩黑神话:悟空  2811
信刻光盘安全隔离与信息交换系统  2702
镜舟科技与中启乘数科技达成战略合作,共筑数据服务新生态  1235
grub引导程序无法找到指定设备和分区  1205
点击报名 | 京东2025校招进校行程预告  162
华为全联接大会2024丨软通动力分论坛精彩议程抢先看!  160
2024海洋能源产业融合发展论坛暨博览会同期活动-海洋能源与数字化智能化论坛成功举办  157
华为纯血鸿蒙正式版9月底见!但Mate 70的内情还得接着挖...  154
本周热议
我的信创开放社区兼职赚钱历程 40
今天你签到了吗? 27
信创开放社区邀请他人注册的具体步骤如下 15
如何玩转信创开放社区—从小白进阶到专家 15
方德桌面操作系统 14
我有15积分有什么用? 13
用抖音玩法闯信创开放社区——用平台宣传企业产品服务 13
如何让你先人一步获得悬赏问题信息?(创作者必看) 12
2024中国信创产业发展大会暨中国信息科技创新与应用博览会 9
中央国家机关政府采购中心:应当将CPU、操作系统符合安全可靠测评要求纳入采购需求 8

加入交流群

请使用微信扫一扫!