DL之SSD:基于tensorflow利用SSD算法实现目标检测(21类)


大象2
大象2 2022-09-19 13:38:22 50927
分类专栏: 资讯

DL之SSD:基于tensorflow利用SSD算法实现目标检测(21类)

目录

输出结果

SSD代码


输出结果

  1. VOC_LABELS = {
  2. 'none': (0, 'Background'),
  3. 'aeroplane': (1, 'Vehicle'),
  4. 'bicycle': (2, 'Vehicle'),
  5. 'bird': (3, 'Animal'),
  6. 'boat': (4, 'Vehicle'),
  7. 'bottle': (5, 'Indoor'),
  8. 'bus': (6, 'Vehicle'),
  9. 'car': (7, 'Vehicle'),
  10. 'cat': (8, 'Animal'),
  11. 'chair': (9, 'Indoor'),
  12. 'cow': (10, 'Animal'),
  13. 'diningtable': (11, 'Indoor'),
  14. 'dog': (12, 'Animal'),
  15. 'horse': (13, 'Animal'),
  16. 'motorbike': (14, 'Vehicle'),
  17. 'person': (15, 'Person'),
  18. 'pottedplant': (16, 'Indoor'),
  19. 'sheep': (17, 'Animal'),
  20. 'sofa': (18, 'Indoor'),
  21. 'train': (19, 'Vehicle'),
  22. 'tvmonitor': (20, 'Indoor'),
  23. }

SSD代码

  1. class SSDNet(-title class_ inherited__">object):
  2. """Implementation of the SSD VGG-based 300 network.
  3. The default features layers with 300x300 image input are:
  4. conv4 ==> 38 x 38
  5. conv7 ==> 19 x 19
  6. conv8 ==> 10 x 10
  7. conv9 ==> 5 x 5
  8. conv10 ==> 3 x 3
  9. conv11 ==> 1 x 1
  10. The default image size used to train this network is 300x300.
  11. """
  12. default_params = SSDParams(
  13. img_shape=(300, 300),
  14. num_classes=21,
  15. no_annotation_label=21,
  16. feat_layers=['block4', 'block7', 'block8', 'block9', 'block10', 'block11'],
  17. feat_shapes=[(38, 38), (19, 19), (10, 10), (5, 5), (3, 3), (1, 1)],
  18. anchor_size_bounds=[0.15, 0.90],
  19. anchor_size_bounds=[0.20, 0.90],
  20. anchor_sizes=[(21., 45.),
  21. (45., 99.),
  22. (99., 153.),
  23. (153., 207.),
  24. (207., 261.),
  25. (261., 315.)],
  26. anchor_sizes=[(30., 60.),
  27. (60., 111.),
  28. (111., 162.),
  29. (162., 213.),
  30. (213., 264.),
  31. (264., 315.)],
  32. anchor_ratios=[[2, .5],
  33. [2, .5, 3, 1./3],
  34. [2, .5, 3, 1./3],
  35. [2, .5, 3, 1./3],
  36. [2, .5],
  37. [2, .5]],
  38. anchor_steps=[8, 16, 32, 64, 100, 300],
  39. anchor_offset=0.5,
  40. normalizations=[20, -1, -1, -1, -1, -1],
  41. prior_scaling=[0.1, 0.1, 0.2, 0.2]
  42. )
  43. def __init__(self, params=None):
  44. """Init the SSD net with some parameters. Use the default ones
  45. if none provided.
  46. """
  47. if isinstance(params, SSDParams):
  48. self.params = params
  49. else:
  50. self.params = SSDNet.default_params
  51. =======================================================================
  52. def net(self, inputs,
  53. is_training=True,
  54. update_feat_shapes=True,
  55. dropout_keep_prob=0.5,
  56. prediction_fn=slim.softmax,
  57. reuse=None,
  58. scope='ssd_300_vgg'):
  59. """SSD network definition.
  60. """
  61. r = ssd_net(inputs,
  62. num_classes=self.params.num_classes,
  63. feat_layers=self.params.feat_layers,
  64. anchor_sizes=self.params.anchor_sizes,
  65. anchor_ratios=self.params.anchor_ratios,
  66. normalizations=self.params.normalizations,
  67. is_training=is_training,
  68. dropout_keep_prob=dropout_keep_prob,
  69. prediction_fn=prediction_fn,
  70. reuse=reuse,
  71. scope=scope)
  72. Update feature shapes (try at least!)
  73. if update_feat_shapes:
  74. shapes = ssd_feat_shapes_from_net(r[0], self.params.feat_shapes)
  75. self.params = self.params._replace(feat_shapes=shapes)
  76. return r
  77. def arg_scope(self, weight_decay=0.0005, data_format='NHWC'):
  78. """Network arg_scope.
  79. """
  80. return ssd_arg_scope(weight_decay, data_format=data_format)
  81. def arg_scope_caffe(self, caffe_scope):
  82. """Caffe arg_scope used for weights importing.
  83. """
  84. return ssd_arg_scope_caffe(caffe_scope)
  85. =======================================================================
  86. def update_feature_shapes(self, predictions):
  87. """Update feature shapes from predictions collection (Tensor or Numpy
  88. array).
  89. """
  90. shapes = ssd_feat_shapes_from_net(predictions, self.params.feat_shapes)
  91. self.params = self.params._replace(feat_shapes=shapes)
  92. def anchors(self, img_shape, dtype=np.float32):
  93. """Compute the default anchor boxes, given an image shape.
  94. """
  95. return ssd_anchors_all_layers(img_shape,
  96. self.params.feat_shapes,
  97. self.params.anchor_sizes,
  98. self.params.anchor_ratios,
  99. self.params.anchor_steps,
  100. self.params.anchor_offset,
  101. dtype)
  102. def bboxes_encode(self, labels, bboxes, anchors,
  103. scope=None):
  104. """Encode labels and bounding boxes.
  105. """
  106. return ssd_common.tf_ssd_bboxes_encode(
  107. labels, bboxes, anchors,
  108. self.params.num_classes,
  109. self.params.no_annotation_label,
  110. ignore_threshold=0.5,
  111. prior_scaling=self.params.prior_scaling,
  112. scope=scope)
  113. def bboxes_decode(self, feat_localizations, anchors,
  114. scope='ssd_bboxes_decode'):
  115. """Encode labels and bounding boxes.
  116. """
  117. return ssd_common.tf_ssd_bboxes_decode(
  118. feat_localizations, anchors,
  119. prior_scaling=self.params.prior_scaling,
  120. scope=scope)
  121. def detected_bboxes(self, predictions, localisations,
  122. select_threshold=None, nms_threshold=0.5,
  123. clipping_bbox=None, top_k=400, keep_top_k=200):
  124. """Get the detected bounding boxes from the SSD network output.
  125. """
  126. Select top_k bboxes from predictions, and clip
  127. rscores, rbboxes = \
  128. ssd_common.tf_ssd_bboxes_select(predictions, localisations,
  129. select_threshold=select_threshold,
  130. num_classes=self.params.num_classes)
  131. rscores, rbboxes = \
  132. tfe.bboxes_sort(rscores, rbboxes, top_k=top_k)
  133. Apply NMS algorithm.
  134. rscores, rbboxes = \
  135. tfe.bboxes_nms_batch(rscores, rbboxes,
  136. nms_threshold=nms_threshold,
  137. keep_top_k=keep_top_k)
  138. if clipping_bbox is not None:
  139. rbboxes = tfe.bboxes_clip(clipping_bbox, rbboxes)
  140. return rscores, rbboxes
  141. def losses(self, logits, localisations,
  142. gclasses, glocalisations, gscores,
  143. match_threshold=0.5,
  144. negative_ratio=3.,
  145. alpha=1.,
  146. label_smoothing=0.,
  147. scope='ssd_losses'):
  148. """Define the SSD network losses.
  149. """
  150. return ssd_losses(logits, localisations,
  151. gclasses, glocalisations, gscores,
  152. match_threshold=match_threshold,
  153. negative_ratio=negative_ratio,
  154. alpha=alpha,
  155. label_smoothing=label_smoothing,
  156. scope=scope)

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

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

加入交流群

请使用微信扫一扫!