YOLOv5代码注释版更新啦,注释的是最近的2021.07.14的版本,且注释更全
github: https://github.com/Laughing-q/yolov5_annotations
YOLOV5 general.py注释与解析
暂时只做了build_targets和compute_loss函数的注释,主要是今天正好对yolov5的边框回归方式看了一下;
有时间再更新其它函数;
build_targets函数中有对yolov5边框回归的详细说明,毕竟现在也没有发paper,只能通过代码自己研究,要是有错误,欢迎指正。
def build_targets(p, targets, model):
"""
Args:
p: 网络输出,List[torch.tensor * 3], p[i].shape = (b, 3, h, w, nc+5), hw分别为特征图的长宽,b为batch-size
targets: targets.shape = (nt, 6) , 6=icxywh,i表示第一张图片,c为类别,然后为坐标xywh
model: 模型
Returns:
"""
# Build targets for compute_loss(), input targets(image,class,x,y,w,h)
# 获取每一个(3个)检测层
det = model.module.model[-1] if is_parallel(model) else model.model[-1] # Detect() module
# anchor数量和标签框数量
na, nt = det.na, targets.shape[0] # number of anchors, targets
tcls, tbox, indices, anch = [], [], [], []
gain = torch.ones(7, device=targets.device) # normalized to gridspace gain
# ai.shape = (na, nt) 生成anchor索引
ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt)
# targets.shape = (na, nt, 7)
targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices
# 设置偏移量
g = 0.5 # bias
# (5, 2)
off = torch.tensor([[0, 0],
[1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m
# [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm
], device=targets.device).float() * g # offsets
# 对每个检测层进行处理
for i in range(det.nl):
anchors = det.anchors[i]
# 得到特征图的坐标系数