Faceboxes pytorch代码解读(一) box_utils.py(上篇)

有幸读到Shifeng Zhang老师团队的人脸检测论文,感觉对自己的人脸学习论文十分有帮助。通过看别人的paper,学习别人的代码,能够使得我们对人脸检测算法的学习有更近一步的理解。
但是在学习的时候发现,自己看别人的代码是一个耗时而又头疼的事情。毕竟每个人的思路都不一样,跟着别人的思路走确实不容易。所以希望能够分享一下自己在学习代码时的理解,希望对刚刚入门的你们有所帮助。
我也是深度学习的小白一枚,如若出现错误希望大佬们能够指正,谢谢!

论文原文地址:http://www.cbsr.ia.ac.cn/users/sfzhang/Shifeng%20Zhang’s%20Homepage_files/FaceBoxes_NC.pdf

github代码地址:
https://github.com/zisianw/FaceBoxes.PyTorch

point_form()函数

功能:将坐标由(cx,cy,w,h)形式转换为(xmin, ymin, xmax, ymax)形式

def point_form(boxes):#将坐标由(cx,cy,w,h)形式转换为(xmin, ymin, xmax, ymax)形式""" Convert prior_boxes to (xmin, ymin, xmax, ymax)representation for comparison to point form ground truth data.Args:boxes: (tensor) center-size default boxes from priorbox layers.Return:boxes: (tensor) Converted xmin, ymin, xmax, ymax form of boxes."""return torch.cat((boxes[:, :2] - boxes[:, 2:]/2,     # xmin, yminboxes[:, :2] + boxes[:, 2:]/2), 1)  # xmax, ymax#torch.cat是将两个张量(tensor)拼接在一起 维度1表示进行横向拼接

center_size()函数

功能:将坐标由(xmin, ymin, xmax, ymax)形式转换为(cx,cy,w,h)形式

def center_size(boxes):#将坐标由(xmin, ymin, xmax, ymax)形式转换为(cx,cy,w,h)形式""" Convert prior_boxes to (cx, cy, w, h)representation for comparison to center-size form ground truth data.Args:boxes: (tensor) point_form boxesReturn:boxes: (tensor) Converted xmin, ymin, xmax, ymax form of boxes."""return torch.cat((boxes[:, 2:] + boxes[:, :2])/2,  # cx, cyboxes[:, 2:] - boxes[:, :2], 1)  # w, h

intersect()函数

功能:计算box_a和box_b的交集

def intersect(box_a, box_b):#计算交集""" We resize both tensors to [A,B,2] without new malloc:[A,2] -> [A,1,2] -> [A,B,2][B,2] -> [1,B,2] -> [A,B,2]Then we compute the area of intersect between box_a and box_b.Args:box_a: (tensor) bounding boxes, Shape: [A,4].  **A为真实框的数量**box_b: (tensor) bounding boxes, Shape: [B,4].  **B为prior_box的数量**Return:(tensor) intersection area, Shape: [A,B].    **返回值为A*B个IOU值**"""A = box_a.size(0)B = box_b.size(0)max_xy = torch.min(box_a[:, 2:].unsqueeze(1).expand(A, B, 2),box_b[:, 2:].unsqueeze(0).expand(A, B, 2))min_xy = torch.max(box_a[:, :2].unsqueeze(1).expand(A, B, 2),box_b[:, :2].unsqueeze(0).expand(A, B, 2))inter = torch.clamp((max_xy - min_xy), min=0)return inter[:, :, 0] * inter[:, :, 1]#返回intersection的面积

jaccard()函数

功能:计算box_a和box_b的IOU值

def jaccard(box_a, box_b):"""Compute the jaccard overlap of two sets of boxes.  The jaccard overlapis simply the intersection over union of two boxes.  Here we operate onground truth boxes and default boxes.E.g.:A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B) **IOU计算公式**Args:box_a: (tensor) Ground truth bounding boxes, Shape: [num_objects,4]box_b: (tensor) Prior boxes from priorbox layers, Shape: [num_priors,4]Return:jaccard overlap: (tensor) Shape: [box_a.size(0), box_b.size(0)]"""inter = intersect(box_a, box_b)#计算inter面积area_a = ((box_a[:, 2]-box_a[:, 0]) *(box_a[:, 3]-box_a[:, 1])).unsqueeze(1).expand_as(inter)  # [A,B]area_b = ((box_b[:, 2]-box_b[:, 0]) *(box_b[:, 3]-box_b[:, 1])).unsqueeze(0).expand_as(inter)  # [A,B]union = area_a + area_b - interreturn inter / union# [A,B]

matrix_iou()函数

功能:numpy版本的IOU计算

def matrix_iou(a, b):"""return iou of a and b, numpy version for data augenmentation"""lt = np.maximum(a[:, np.newaxis, :2], b[:, :2])rb = np.minimum(a[:, np.newaxis, 2:], b[:, 2:])area_i = np.prod(rb - lt, axis=2) * (lt < rb).all(axis=2)area_a = np.prod(a[:, 2:] - a[:, :2], axis=1)area_b = np.prod(b[:, 2:] - b[:, :2], axis=1)return area_i / (area_a[:, np.newaxis] + area_b - area_i)

matrix_iof()函数

功能:numpy版本的IOF计算

def matrix_iof(a, b):"""return iof of a and b, numpy version for data augenmentation"""lt = np.maximum(a[:, np.newaxis, :2], b[:, :2])rb = np.minimum(a[:, np.newaxis, 2:], b[:, 2:])area_i = np.prod(rb - lt, axis=2) * (lt < rb).all(axis=2)area_a = np.prod(a[:, 2:] - a[:, :2], axis=1)return area_i / np.maximum(area_a[:, np.newaxis], 1)

这就是box_utils.py(上篇)的内容,下篇的内容会在随后更新。谢谢!

Faceboxes pytorch代码解读(一) box_utils.py(上篇)相关推荐

  1. MAML-RL Pytorch 代码解读 (6) -- maml_rl/envs/bandit.py

    MAML-RL Pytorch 代码解读 (6) – maml_rl/envs/bandit.py 文章目录 MAML-RL Pytorch 代码解读 (6) -- maml_rl/envs/band ...

  2. TSN算法的PyTorch代码解读(训练部分)

    这篇博客来读一读TSN算法的PyTorch代码,总体而言代码风格还是不错的,多读读优秀的代码对自身的提升还是有帮助的,另外因为代码内容较多,所以分训练和测试两篇介绍,这篇介绍训练代码,介绍顺序为代码运 ...

  3. 对抗自编码器AAE——pytorch代码解读试验

    AAE网络结构基本框架如论文中所示: 闲话不多说,直接来学习一下加了注释和微调的基本AAE的代码(初始代码链接github): aae_pytorch_basic.py #!/usr/bin/env ...

  4. yolov5 代码解读 损失函数 loss.py

    smooth_BCE def smooth_BCE(eps=0.1): # https://github.com/ultralytics/yolov3/issues/238#issuecomment- ...

  5. ResNet及其变种的结构梳理、有效性分析与代码解读(PyTorch)

    点击我爱计算机视觉标星,更快获取CVML新技术 本文来自知乎,作者费敬敬,现为同济大学计算机科学与技术硕士. https://zhuanlan.zhihu.com/p/54289848 温故而知新,理 ...

  6. Pytorch LSTM 代码解读及自定义双向 LSTM 算子

    Pytorch LSTM 代码解读及自定义双向 LSTM 算子 1. 理论 关于 LSTM 的理论部分可以参考 Paper Long Short-Term Memory Based Recurrent ...

  7. Resnet的pytorch官方实现代码解读

    Resnet的pytorch官方实现代码解读 目录 Resnet的pytorch官方实现代码解读 前言 概述 34层网络结构的"平原"网络与"残差"网络的结构图 ...

  8. mapbox 修改初始位置_一行代码教你如何随心所欲初始化Bert参数(附Pytorch代码详细解读)...

    微信公众号:NLP从入门到放弃 微信文章在这里(排版更漂亮,但是内置链接不太行,看大家喜欢哪个点哪个看吧): 一行代码带你随心所欲重新初始化bert的参数(附Pytorch代码详细解读)​mp.wei ...

  9. 说话人识别损失函数的PyTorch实现与代码解读

    概述 说话人识别中的损失函数分为基于多类别分类的损失函数,和端到端的损失函数(也叫基于度量学习的损失函数),关于这些损失函数的理论部分,可参考说话人识别中的损失函数 本文主要关注这些损失函数的实现,此 ...

最新文章

  1. linux下makefile
  2. NYOJ 1053 Alice and Bob (N)
  3. oracle表对比同步,Oracle表双向同步问题
  4. Notes on language modeling-COMS W4705: Natural Language Processing-学习笔记
  5. 【LeetCode】剑指 Offer 52. 两个链表的第一个公共节点
  6. javascript中事件
  7. git clone出现fatal: HTTP request failed --git版本问题
  8. JAVA 使用Dom4j 解析XML
  9. Autodesk 3DSMax 2012 安装说明
  10. 重磅!Science发表西湖大学周强实验室关于“新冠”的最新研究成果
  11. 哈利波特系列之伏地魔生平
  12. 缓解迷茫焦虑的最好方法:用自己的方式好好生活
  13. 微信客服消息跳转h5,回复关键字,跳转小程序方式全集
  14. 从零学习Vue - 02模板语法、el与data两种写法、mvvm模型、数据代理
  15. skynet框架应用 (一) skynet介绍
  16. Android artoolkitx渲染3D模型
  17. 计算机工程被退稿,一稿多投被拒稿,确实是你的错
  18. [Vue] 模板语法
  19. Salted Password Hashing - Doing it Right
  20. 关于maven pom (父项目)的maven子moudle/springboot子项目

热门文章

  1. linux samba yum,CentOS7下yum安装SAMBA全命令过程
  2. Django-(6)
  3. Spark性能优化之-shuffle调优
  4. System.setOut(ps)重定义了输出流后,如何重定向控制台输出
  5. abcd选项后的数据分析_引入新的数据abcs
  6. 不会做动画的程序猿不是好的动画师(如何用css3动画做动画)
  7. 计算机的管理员在那,win10系统administrator管理员在哪
  8. java计算机毕业设计钢材出入库管理系统(附源码、数据库)
  9. 计算机目录排版的文章,自动生成目录,论文排版看这一篇就够了
  10. 我的世界修改服务器视距,教程/测量距离 - Minecraft Wiki,最详细的官方我的世界百科...