Hailo,地平线RDK X3使用 yolov5 v2.0版本需要修改的部分

1.

models/yolo.py  line 130 ,_initialize_biases方法里添加  with torch.no_grad():


    def _initialize_biases(self, cf=None):  # initialize biases into Detect(), cf is class frequency
        # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
        m = self.model[-1]  # Detect() module
       
        for mi, s in zip(m.m, m.stride):  #  from
            b = mi.bias.view(m.na, -1)  # conv.bias(255) to (3,85)
            #amend start
            # b[:, 4] += math.log(8 / (640 / s) ** 2)  # obj (8 objects per 640 image)
            # b[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum())  # cls
            
            
            with torch.no_grad():
                b[:, 4] += math.log(8 / (640 / s) ** 2)  # obj (8 objects per 640 image)
                b[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum())  # cls
            #amend end   
            mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
--------------------------------------------------------------------------------------------------------------------   

2.

#err msg 取自不同的device   

utils/utils.py的533行在 a, t = at[j], t.repeat(na, 1, 1)[j]  # filter      的上方添加

            at = at.to(j.device)
            t = t.to(j.device)
            a, t = at[j], t.repeat(na, 1, 1)[j]  # filter
            
--------------------------------------------------------------------------------------------------------------------   

3.

#err msg    ( 这个不是bug,是自己修改forward的代码后可能会产生的问题)     
pxy = ps[:, :2].sigmoid() * 2 - 0.5
IndexError: too many indices for tensor of dimension 1           

如果自己修改了yolo.py的forward方法,可能会报这个错。把自己修改的部分改回去。

--------------------------------------------------------------------------------------------------------------------

4.

utils/utils.py
can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

line 922的output_to_target方法的最后返回值
# return np.array(targets)
return torch.tensor(targets).cpu().numpy()


--------------------------------------------------------------------------------------------------------------------

5.

errmsg:找不到models模块( 这个不是bug,是文件夹路径的问题)     

models/export.py
假设文件夹名称是yolov5,在export.py的最开头添加
path = os.path.dirname(__file__)
project_path=path.split('/yolov5/')[0]+'/yolov5'
sys.path.append(project_path)
print('project_path',project_path)


--------------------------------------------------------------------------------------------------------------------

6.

models/yolo.py (注意,只有使用地平线RTK X3导出onnx的时候才需要做这个修改,导出后需要将其改回。Hailo不用修改)


def forward(self, x):
    bs, _, ny, nx = x[i].shape  # x(bs,255,20,20) to x(bs,3,20,20,85)的下一行


    # https://developer.horizon.cc/forumDetail/163807123501918330
    # todo RDK_X3 不知道爲什麼
    # x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
    x[i] = x[i].permute(0, 2, 3, 1).contiguous()


--------------------------------------------------------------------------------------------------------------------

7.

train.py

errmsg:
Traceback (most recent call last):
  File "train.py", line 469, in <module>
    train(hyp, tb_writer, opt, device)
  File "train.py", line 371, in train
    with open(results_file, 'r') as f:  # create checkpoint
FileNotFoundError: [Errno 2] No such file or directory: 'runs/exp28/results.txt'


将Save model的代码整体后退一个tab,也就是只有if not opt.notest or final_epoch: 的时候才会save model
# Save model
save = (not opt.nosave) or (final_epoch and not opt.evolve)
if save:
    with open(results_file, 'r') as f:  # create checkpoint
        ckpt = {'epoch': epoch,
                'best_fitness': best_fitness,
                'training_results': f.read(),
                'model': ema.ema.module if hasattr(ema, 'module') else ema.ema,
                'optimizer': None if final_epoch else optimizer.state_dict()}

    # Save last, best and delete
    torch.save(ckpt, last)
    if (best_fitness == fi) and not final_epoch:
        torch.save(ckpt, best)
    del ckpt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值