Esempio n. 1
0
def f_prod_pic4one(img_np,
                   data_transform,
                   model,
                   size_ts,
                   labels_lsit,
                   is_keeep=False,
                   cfg=None,
                   gboxes_ltrb=None,
                   target=None):
    p_boxes_ltrb, p_scores_float, plabels_text = model_out4one(
        model,
        img_np,
        data_transform,
        size_ts=size_ts,
        labels_lsit=labels_lsit,
        target=target,
        is_keeep=is_keeep)

    f_show_od_np4plt(
        img_np,
        gboxes_ltrb=gboxes_ltrb,
        pboxes_ltrb=p_boxes_ltrb,
        plabels_text=plabels_text,
        p_scores_float=p_scores_float,
    )
Esempio n. 2
0
def t_bbox(dataset, transform):
    for data in dataset:
        # coco dataset可视化
        # print(data)
        # 如果有 transform 则返回的基本是统一的 tensor3d torch.Size([3, 448, 448])
        img_np_tensor, target = data  # 没有transform 则是原图 np(1385, 1024, 3) whc bgr
        # 这个是用 coco api
        # print(target)

        # 各种 coco 模式统一用这个
        f_show_coco_pics(coco, path_img, ids_img=[target['image_id']])
        # 这里输出的是np或tensor
        if transform is None:
            f_show_od_np4plt(img_np_tensor, target['boxes'], is_recover_size=False)  # 显示原图
        else:
            f_show_od_ts4plt(img_np_tensor, target['boxes'], is_recover_size=True)  # 需要恢复box
        pass
Esempio n. 3
0
    def forward(self, pyolos, targets, imgs_ts=None):
        '''

        :param pyolos: torch.Size([3, 40, 13, 13]) [conf-1,class-3,box4] 5*8=40
            d19 torch.Size([2, 81, 13, 13])  9* conf +type4+ box4
        :param targets:
            target['boxes'] = target['boxes'].to(device)
            target['labels'] = target['labels'].to(device)
            target['size'] = target['size']
            target['image_id'] = int
        :param imgs_ts:
        :return:
        '''
        cfg = self.cfg
        device = pyolos.device
        batch, c, h, w = pyolos.shape
        s_ = 1 + cfg.NUM_CLASSES
        # [3, 40, 13, 13] -> [3, 8, 5, 13*13] -> [3, 169, 5, 8]
        pyolos = pyolos.view(batch, s_ + 4, cfg.NUM_ANC, - 1).permute(0, 3, 2, 1).contiguous()
        # [3, 169, 5, 8] -> [3, 169*5, 8]
        pyolos = pyolos.view(batch, -1, s_ + 4)
        preg_pos = pyolos[..., s_:s_ + 4]

        '''--------------gt匹配---------------'''
        # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
        if cfg.MODE_TRAIN == 4:
            gdim = 1 + cfg.NUM_CLASSES + 4 + 1 + 4 + 1  # torch.Size([3, 13, 13, 5, 13])
        else:
            gdim = 1 + cfg.NUM_CLASSES + 4 + 1 + 4  # torch.Size([3, 13, 13, 5, 13])

        gyolos = torch.empty((batch, h, w, cfg.NUM_ANC, gdim), device=device)

        # 匹配GT
        for i, target in enumerate(targets):  # batch遍历
            gboxes_ltrb_b = target['boxes']  # ltrb
            glabels_b = target['labels']

            # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
            if cfg.MODE_TRAIN == 4:
                gyolos[i] = fmatch4yolov2_99(
                    gboxes_ltrb_b=gboxes_ltrb_b,
                    glabels_b=glabels_b,
                    grid=h,  # 7 只有一层
                    gdim=gdim,
                    device=device,
                    cfg=cfg,
                    preg_b=preg_pos[i],
                    img_ts=imgs_ts[i],
                )
            else:
                gyolos[i] = fmatch4yolov2(
                    gboxes_ltrb_b=gboxes_ltrb_b,
                    glabels_b=glabels_b,
                    grid=h,  # 7 只有一层
                    gdim=gdim,
                    device=device,
                    cfg=cfg,
                    img_ts=imgs_ts[i],
                )

            '''可视化验证'''
            if cfg.IS_VISUAL:
                # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
                gyolo_test = gyolos[i].clone()  # torch.Size([32, 13, 13, 9])
                gyolo_test = gyolo_test.view(-1, gdim)
                gconf_one = gyolo_test[:, 0]
                # mask_pos = torch.logical_or(gconf_one == 1, gconf_one == -1)
                mask_pos_2d = gconf_one == 1

                gtxywh = gyolo_test[:, 1 + cfg.NUM_CLASSES:1 + cfg.NUM_CLASSES + 4]
                # 这里是修复是 xy
                _xy_grid = gtxywh[:, :2] + f_mershgrid(h, w, is_rowcol=False, num_repeat=cfg.NUM_ANC).to(device)
                hw_ts = torch.tensor((h, w), device=device)
                gtxywh[:, :2] = torch.true_divide(_xy_grid, hw_ts)
                gtxywh = gtxywh[mask_pos_2d]

                gtxywh[:, 2:4] = torch.exp(gtxywh[:, 2:]) / h  # 原图归一化

                from f_tools.pic.enhance.f_data_pretreatment4pil import f_recover_normalization4ts
                img_ts = f_recover_normalization4ts(imgs_ts[i])
                from torchvision.transforms import functional as transformsF
                img_pil = transformsF.to_pil_image(img_ts).convert('RGB')
                import numpy as np
                img_np = np.array(img_pil)
                f_show_od_np4plt(img_np, gboxes_ltrb=gboxes_ltrb_b.cpu()
                                 , pboxes_ltrb=xywh2ltrb(gtxywh.cpu()), is_recover_size=True,
                                 grids=(h, w))

        # torch.Size([32, 13, 13, 5, 13]) -> [32, 13*13*5, 13]
        gyolos = gyolos.view(batch, -1, gdim)
        gconf = gyolos[:, :, 0]  # 正例使用1  torch.Size([32, 910])
        mask_pos_2d = gconf > 0
        mask_neg_2d = gconf == 0  # 忽略-1 不管
        nums_pos = (mask_pos_2d.sum(-1).to(torch.float)).clamp(min=torch.finfo(torch.float16).eps)
        nums_neg = (mask_neg_2d.sum(-1).to(torch.float)).clamp(min=torch.finfo(torch.float16).eps)
        pyolos_pos = pyolos[mask_pos_2d]  # torch.Size([32, 845, 8]) -> torch.Size([40, 8])
        gyolos_pos = gyolos[mask_pos_2d]  # torch.Size([32, 845, 13]) -> torch.Size([40, 8])

        ''' ----------------cls损失---------------- '''
        pcls_sigmoid_pos = pyolos_pos[:, 1:s_].sigmoid()
        gcls_pos = gyolos_pos[:, 1:s_]
        _loss_val = x_bce(pcls_sigmoid_pos, gcls_pos, reduction="none")  # torch.Size([46, 3])
        # torch.Size([46, 3]) -> val
        l_cls = _loss_val.sum(-1).mean() * cfg.LOSS_WEIGHT[2]

        ''' ----------------conf损失 ---------------- '''
        pconf_sigmoid = pyolos[:, :, 0].sigmoid()  # 这个需要归一化 torch.Size([3, 845])

        # ------------conf-mse ------------
        # _loss_val = F.mse_loss(pconf_sigmoid, gconf, reduction="none")
        # l_conf_pos = ((_loss_val * mask_pos_2d).sum(-1) / nums_pos).mean() * 10
        # l_conf_neg = ((_loss_val * mask_neg_2d).sum(-1) / nums_neg).mean() * 30

        # ------------ focalloss   ------------
        mash_ignore_2d = torch.logical_not(torch.logical_or(mask_pos_2d, mask_neg_2d))
        l_pos, l_neg = focalloss(pconf_sigmoid, gconf, mask_pos=mask_pos_2d,
                                 mash_ignore=mash_ignore_2d, is_debug=True, alpha=0.5)
        l_conf_pos = (l_pos.sum(-1).sum(-1) / nums_pos).mean()
        l_conf_neg = (l_neg.sum(-1).sum(-1) / nums_neg).mean() * 3

        ''' ---------------- box损失 ----------------- '''
        log_dict = {}
        if cfg.MODE_TRAIN == 4:
            # ------------ iou损失   ------------
            # 解码pxywh 计算预测与 GT 的 iou 作为 gconf
            preg_pos = pyolos_pos[:, s_:s_ + 4]
            gltrb_pos_tx = gyolos_pos[:, s_ + 4 + 1:s_ + 4 + 1 + 4]
            match_anc_ids = gyolos_pos[:, s_ + 4 + 1 + 4]

            # 解码yolo2 特图尺寸
            pxy_pos_sigmoid = preg_pos[..., :2].sigmoid()

            # 这里与yolo1不一样
            match_ancs = torch.tensor(cfg.ANCS_SCALE, device=device)[match_anc_ids.long()]
            pwh_pos_scale = torch.exp(preg_pos[..., 2:4]) * match_ancs * h  # 恢复到特图
            pzxywh = torch.cat([pxy_pos_sigmoid, pwh_pos_scale], -1)

            iou_zg = bbox_iou4one_2d(xywh2ltrb(pzxywh), gltrb_pos_tx, is_giou=True)
            # iou_zg = bbox_iou4y(xywh2ltrb4ts(pzxywh), gltrb_pos_tx, GIoU=True)
            # print(iou_zg)
            l_reg = (1 - iou_zg).mean() * 2

            ''' ---------------- loss完成 ----------------- '''
            l_total = l_conf_pos + l_conf_neg + l_cls + l_reg
            log_dict['l_reg'] = l_reg.item()
        else:
            # ------------ mse+bce   ------------ 666666
            # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
            pxy_pos_sigmoid = pyolos_pos[:, s_:s_ + 2].sigmoid()  # 这个需要归一化
            pwh_pos_scale = pyolos_pos[:, s_ + 2:s_ + 4]
            weight_pos = gyolos_pos[:, s_ + 4 + 1]  # torch.Size([32, 845])
            gtxy_pos = gyolos_pos[:, s_:s_ + 2]  # [nn]
            gtwh_pos = gyolos_pos[:, s_ + 2:s_ + 4]

            _loss_val = x_bce(pxy_pos_sigmoid, gtxy_pos, reduction="none")
            l_txty = (_loss_val.sum(-1) * weight_pos).mean()
            _loss_val = F.mse_loss(pwh_pos_scale, gtwh_pos, reduction="none")
            l_twth = (_loss_val.sum(-1) * weight_pos).mean()

            ''' ---------------- loss完成 ----------------- '''
            l_total = l_conf_pos + l_conf_neg + l_cls + l_txty + l_twth
            log_dict['l_xy'] = l_txty.item()
            log_dict['l_wh'] = l_twth.item()

        log_dict['l_total'] = l_total.item()
        log_dict['l_conf_pos'] = l_conf_pos.item()
        log_dict['l_conf_neg'] = l_conf_neg.item()
        log_dict['l_cls'] = l_cls.item()

        log_dict['p_max'] = pconf_sigmoid.max().item()
        log_dict['p_min'] = pconf_sigmoid.min().item()
        log_dict['p_mean'] = pconf_sigmoid.mean().item()
        return l_total, log_dict
Esempio n. 4
0
    def forward(self, pyolos, targets, imgs_ts=None):
        '''

        :param pyolos: torch.Size([2, 45, 13, 13])
        :param targets:
        :param imgs_ts:
        :return:
        '''
        cfg = self.cfg
        device = pyolos.device
        batch, c, h, w = pyolos.shape  # torch.Size([2, 45, 13, 13])

        # [3, 40, 13, 13] -> [3, 8, 5, 13*13] -> [3, 169, 5, 8]
        pyolos = pyolos.view(batch, 1 + cfg.NUM_CLASSES + 4, cfg.NUM_ANC, - 1).permute(0, 3, 2, 1).contiguous()

        # [3, 169, 5, 8] -> [3, 169*5, 8]
        pyolos = pyolos.view(batch, h * w * cfg.NUM_ANC, -1)
        # pyolos = pyolos.view(batch, -1, s_ + 4)

        preg = pyolos[..., 1 + cfg.NUM_CLASSES:1 + cfg.NUM_CLASSES + 4]  # torch.Size([2, 169, 5, 4])
        pltrb = boxes_decode4yolo2_v2(preg, h, w, cfg)  # 输出原图归一化 用于更新conf [2, 845, 4]

        '''--------------gt匹配---------------'''
        # conf-1, cls-1, txywh-4, weight-1, gltrb-4
        if cfg.MODE_TRAIN == 99 or cfg.MODE_TRAIN == 98:
            gdim = 1 + 1 + 4 + 1 + 4

        gyolos = torch.empty((batch, h, w, cfg.NUM_ANC, gdim), device=device)

        # 匹配GT
        for i, target in enumerate(targets):  # batch遍历
            gboxes_ltrb_b = target['boxes']  # ltrb
            glabels_b = target['labels']

            # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
            if cfg.MODE_TRAIN == 99 or cfg.MODE_TRAIN == 98:
                gyolos[i] = fmatch4yolov2_99(
                    gboxes_ltrb_b=gboxes_ltrb_b,
                    glabels_b=glabels_b,
                    grid=h,  # 7 只有一层
                    gdim=gdim,
                    device=device,
                    cfg=cfg,
                    preg_b=preg[i],
                    img_ts=imgs_ts[i],
                )

            '''可视化验证'''
            if cfg.IS_VISUAL:
                # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
                gyolo_test = gyolos[i].clone()  # torch.Size([32, 13, 13, 9])
                gyolo_test = gyolo_test.view(-1, gdim)
                gconf_one = gyolo_test[:, 0]
                # mask_pos = torch.logical_or(gconf_one == 1, gconf_one == -1)
                mask_pos_2d = gconf_one == 1

                gtxywh = gyolo_test[:, 1 + cfg.NUM_CLASSES:1 + cfg.NUM_CLASSES + 4]
                # 这里是修复是 xy
                _xy_grid = gtxywh[:, :2] + f_mershgrid(h, w, is_rowcol=False, num_repeat=cfg.NUM_ANC).to(device)
                hw_ts = torch.tensor((h, w), device=device)
                gtxywh[:, :2] = torch.true_divide(_xy_grid, hw_ts)
                gtxywh = gtxywh[mask_pos_2d]

                gtxywh[:, 2:4] = torch.exp(gtxywh[:, 2:]) / h  # 原图归一化

                from f_tools.pic.enhance.f_data_pretreatment4pil import f_recover_normalization4ts
                img_ts = f_recover_normalization4ts(imgs_ts[i])
                from torchvision.transforms import functional as transformsF
                img_pil = transformsF.to_pil_image(img_ts).convert('RGB')
                import numpy as np
                img_np = np.array(img_pil)
                f_show_od_np4plt(img_np, gboxes_ltrb=gboxes_ltrb_b.cpu()
                                 , pboxes_ltrb=xywh2ltrb(gtxywh.cpu()), is_recover_size=True,
                                 grids=(h, w))

        # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
        # torch.Size([32, 13, 13, 5, 11]) -> [32, 13*13*5, 11] ->[2, 845, 11]
        gyolos = gyolos.view(batch, -1, gdim)
        gconf = gyolos[:, :, 0]  # 正例使用1  torch.Size([32, 910])
        # mask_pos_3d = gyolos[:, :, :1] > 0
        # mask_neg_3d = gyolos[:, :, :1] == 0

        mask_pos_2d = gconf > 0
        mask_neg_2d = gconf == 0  # 忽略-1 不管
        nums_pos = (mask_pos_2d.sum(-1).to(torch.float)).clamp(min=torch.finfo(torch.float16).eps)
        nums_neg = (mask_neg_2d.sum(-1).to(torch.float)).clamp(min=torch.finfo(torch.float16).eps)
        pyolos_pos = pyolos[mask_pos_2d]  # torch.Size([32, 845, 8]) -> torch.Size([40, 8])
        gyolos_pos = gyolos[mask_pos_2d]  # torch.Size([32, 845, 13]) -> torch.Size([40, 8])

        # [2, 845, 4] ->
        # iou_zg = bbox_iou4one(pltrb, gyolos[..., 1 + 1 + 4 + 1:1 + 1 + 4 + 1 + 4], is_giou=True)
        iou_zg = bbox_iou4one(pltrb, gyolos[..., 1 + 1 + 4 + 1:1 + 1 + 4 + 1 + 4], is_ciou=True)

        ''' ----------------cls损失---------------- '''
        # pcls_sigmoid_pos = pyolos_pos[:, 1:1 + cfg.NUM_CLASSES].sigmoid()
        pcls_pos = pyolos_pos[:, 1:1 + cfg.NUM_CLASSES]
        gcls_pos = gyolos_pos[:, 1].long()
        # torch.Size([3, 4]) ^^ tensor([2., 2., 3.])
        _loss_val = F.cross_entropy(pcls_pos, gcls_pos, reduction="none")
        # _loss_val = x_bce(pcls_sigmoid_pos, gcls_pos, reduction="none")  # torch.Size([46, 3])
        # torch.Size([46, 3]) -> val
        l_cls = _loss_val.sum(-1).mean()

        ''' ----------------conf损失 ---------------- '''
        pconf_sigmoid = pyolos[:, :, 0].sigmoid()  # 这个需要归一化 torch.Size([3, 845])
        # ------------conf-mse ------------
        _loss_val = F.mse_loss(pconf_sigmoid, iou_zg, reduction="none")  # 这理用的IOU
        l_conf_pos = ((_loss_val * mask_pos_2d).sum(-1) / nums_pos).mean() * 5.
        l_conf_neg = ((_loss_val * mask_neg_2d).sum(-1) / nums_neg).mean() * 1.

        ''' ---------------- box损失 ----------------- '''
        pxy_pos_sigmoid = pyolos_pos[:, 1 + cfg.NUM_CLASSES:1 + cfg.NUM_CLASSES + 2].sigmoid()  # 这个需要归一化
        pwh_pos_scale = pyolos_pos[:, 1 + cfg.NUM_CLASSES + 2:1 + cfg.NUM_CLASSES + 4]
        weight_pos = gyolos_pos[:, 1 + 1 + 4 + 1]  # torch.Size([32, 845])
        gtxy_pos = gyolos_pos[:, 1 + 1:1 + 1 + 2]  # [nn]
        gtwh_pos = gyolos_pos[:, 1 + 1 + 2:1 + 1 + 4]

        _loss_val = x_bce(pxy_pos_sigmoid, gtxy_pos, reduction="none")
        l_txty = (_loss_val.sum(-1) * weight_pos).mean()
        _loss_val = F.mse_loss(pwh_pos_scale, gtwh_pos, reduction="none")
        l_twth = (_loss_val.sum(-1) * weight_pos).mean()

        ''' ---------------- loss完成 ----------------- '''
        log_dict = {}
        l_total = l_conf_pos + l_conf_neg + l_cls + l_txty + l_twth
        log_dict['l_total'] = l_total.item()
        log_dict['l_xy'] = l_txty.item()
        log_dict['l_wh'] = l_twth.item()
        log_dict['l_conf_pos'] = l_conf_pos.item()
        log_dict['l_conf_neg'] = l_conf_neg.item()
        log_dict['l_cls'] = l_cls.item()

        return l_total, log_dict
Esempio n. 5
0
    def forward(self, pssd, targets, imgs_ts=None):
        '''

        :param pssd: preg, pcls = class+1 [2, 4, 8732]
        :param targets:
        :param imgs_ts:
        :return:
        '''
        cfg = self.cfg
        # pcls  classes+1 [b, 4, 8732]
        preg, pcls = pssd
        preg = preg.permute(0, 2, 1)  # [b, 4, 8732] -> [b, 8732, 4]
        device = preg.device
        batch, hw, c = preg.shape

        # cls_val-1, gltrb-4
        gdim = 1 + 4
        gssd = torch.empty((batch, hw, gdim), device=device)  # 每批会整体更新这里不需要赋0

        for i, target in enumerate(targets):  # batch遍历
            gboxes_ltrb_b = target['boxes']  # ltrb
            glabels_b = target['labels']

            boxes_index, mask_pos_b, mask_neg_b, mash_ignore_b = matchs_gt_b(
                cfg,
                gboxes_ltrb_b=gboxes_ltrb_b,
                glabels_b=glabels_b,
                anc_obj=self.anc_obj,
                mode='iou',
                ptxywh_b=preg[i],
                img_ts=imgs_ts[i],
                num_atss_topk=9)
            '''正反例设置 正例才匹配'''
            gssd[i][:, 0] = 0  # 是背景 这个要计算正反例 故需全fill 0
            gssd[i][mask_pos_b, 0] = glabels_b[boxes_index][mask_pos_b]
            gssd[i][mask_pos_b, 1:1 +
                    4] = gboxes_ltrb_b[boxes_index][mask_pos_b]  # gltrb-4
            '''可视化验证'''
            if cfg.IS_VISUAL:
                gssd_test = gssd[i].clone()
                # gssd_test = gssd_test.view(-1, gdim)
                gconf_one = gssd_test[:, 0]
                mask_pos_2d = gconf_one > 0
                flog.debug('mask_pos_2d 个数%s', mask_pos_2d.sum())
                # torch.Size([169, 4])
                anc_ltrb_pos = xywh2ltrb(self.anc_obj.ancs_xywh[mask_pos_2d])

                from f_tools.pic.enhance.f_data_pretreatment4pil import f_recover_normalization4ts
                img_ts = f_recover_normalization4ts(imgs_ts[i])
                from torchvision.transforms import functional as transformsF
                img_pil = transformsF.to_pil_image(img_ts).convert('RGB')
                import numpy as np
                img_np = np.array(img_pil)
                f_show_od_np4plt(
                    img_np,
                    gboxes_ltrb=gboxes_ltrb_b.cpu(),
                    pboxes_ltrb=anc_ltrb_pos.cpu(),  # ltrb
                    # other_ltrb=xywh2ltrb(self.anc_obj.ancs_xywh)[:100],
                    is_recover_size=True,
                    # grids=(h, w)
                )

        # cls_val-1, gltrb-4
        glabel = gssd[:, :, 0]  # 0为背景
        mask_pos_2d = glabel > 0
        nums_pos = (mask_pos_2d.sum(-1).to(
            torch.float)).clamp(min=torch.finfo(torch.float16).eps)
        # mask_neg_2d = glabel == 0 反例没用上
        # nums_neg = (mask_neg.sum(-1).to(torch.float)).clamp(min=torch.finfo(torch.float16).eps)
        ''' ---------------- 回归损失   ----------------- '''
        gboxes_ltrb_m_pos = gssd[:, :, 1:1 + 4][mask_pos_2d]
        ancs_xywh_m_pos = self.anc_obj.ancs_xywh.unsqueeze(0).repeat(
            batch, 1, 1)[mask_pos_2d]
        gtxywh_pos = boxes_encode4ssd(cfg, ancs_xywh_m_pos,
                                      ltrb2xywh(gboxes_ltrb_m_pos))
        _loss_val = F.smooth_l1_loss(preg[mask_pos_2d],
                                     gtxywh_pos,
                                     reduction="none")
        l_box = _loss_val.sum(-1).mean()
        ''' ---------------- 类别-cls损失 ---------------- '''
        # 自带softmax
        _loss_val = F.cross_entropy(pcls, glabel.long(), reduction="none")
        mask_neg_hard = f_ohem(_loss_val, nums_pos * 3, mask_pos=mask_pos_2d)
        l_conf_pos = ((_loss_val * mask_pos_2d).sum(-1) /
                      nums_pos).mean()  # 正例越多反例越多
        l_conf_neg = ((_loss_val * mask_neg_hard).sum(-1) / nums_pos).mean()

        log_dict = {}
        ''' ---------------- loss完成 ----------------- '''
        l_total = l_box + l_conf_pos + l_conf_neg

        log_dict['l_total'] = l_total.item()
        log_dict['l_conf_pos'] = l_conf_pos.item()
        log_dict['l_conf_neg'] = l_conf_neg.item()
        log_dict['l_box'] = l_box.item()

        # log_dict['p_max'] = pcls.max().item()
        # log_dict['p_min'] = pcls.min().item()
        # log_dict['p_mean'] = pcls.mean().item()
        return l_total, log_dict
Esempio n. 6
0
    def forward(self, pyolos, targets, imgs_ts=None):
        '''

        :param pyolos: torch.Size([32, 6, 14, 14]) [conf-1,class-20,box4]
        :param targets:
        :param imgs_ts:
        :return:
        '''
        cfg = self.cfg
        device = pyolos.device
        batch, c, h, w = pyolos.shape  # torch.Size([32, 13,13, 8])
        # b,c,h,w -> b,c,hw -> b,hw,c  torch.Size([32, 169, 8])
        pyolos = pyolos.view(batch, c, -1).permute(0, 2, 1)
        s_ = 1 + cfg.NUM_CLASSES
        ptxywh = pyolos[..., s_:s_ + 4]  # torch.Size([32, 169, 4])

        # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
        gdim = 1 + cfg.NUM_CLASSES + 4 + 1 + 4
        gyolos = torch.empty((batch, h, w, gdim),
                             device=device)  # 每批会整体更新这里不需要赋0

        for i, target in enumerate(targets):  # batch遍历
            gboxes_ltrb_b = target['boxes']  # ltrb
            glabels_b = target['labels']
            '''
            yolo4
            1. 每层选一个匹配一个 anc与GT的IOU最大的一个
                技巧gt的xy可调整成 格子偏移与pxy匹配
            2. 其它的IOU>0.4忽略,除正例
            3. reg损失: 解码预测 pxy.sigmoid exp(pwh*anc) -> 进行IOU loss
                正例损失进行平均, 权重0.05
            4. cls损失: 
                label_smooth 标签平滑正则化, onehot* (1-0.01) + 0.01 /num_class
                pos_weight=0.5
                loss_weight=0.5 * num_classes / 80 = 0.01875
            5. conf损失:
                整体权重0.4  忽略的
            6. 每一层的损失全加起来
            '''
            gyolos[i] = fmatch4yolov1(
                gboxes_ltrb_b=gboxes_ltrb_b,
                glabels_b=glabels_b,
                grid=h,  # 7
                gdim=gdim,
                device=device,
                img_ts=imgs_ts[i],
                cfg=cfg,
                use_conf=True)
            '''可视化验证'''
            if cfg.IS_VISUAL:
                # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
                gyolo_test = gyolos[i].clone()  # torch.Size([32, 13, 13, 9])
                gyolo_test = gyolo_test.view(-1, gdim)
                gconf_one = gyolo_test[:, 0]
                mask_pos = gconf_one == 1  # [169]

                # torch.Size([169, 4])
                txywh_t = gyolo_test[:, 1 + cfg.NUM_CLASSES:1 +
                                     cfg.NUM_CLASSES + 4]

                # 这里是修复所有的xy
                zpxy_t = txywh_t[:, :2] + f_mershgrid(
                    h, w, is_rowcol=False).to(device)
                hw_ts = torch.tensor((h, w), device=device)
                zpxy = torch.true_divide(zpxy_t, hw_ts)
                zpwh = torch.exp(txywh_t[:, 2:]) / hw_ts
                zpxywh_pos = torch.cat([zpxy, zpwh], dim=-1)[mask_pos]

                from f_tools.pic.enhance.f_data_pretreatment4pil import f_recover_normalization4ts
                img_ts = f_recover_normalization4ts(imgs_ts[i])
                from torchvision.transforms import functional as transformsF
                img_pil = transformsF.to_pil_image(img_ts).convert('RGB')
                import numpy as np
                img_np = np.array(img_pil)
                f_show_od_np4plt(img_np,
                                 gboxes_ltrb=gboxes_ltrb_b.cpu(),
                                 pboxes_ltrb=xywh2ltrb(zpxywh_pos.cpu()),
                                 is_recover_size=True,
                                 grids=(h, w))

        gyolos = gyolos.view(batch, -1, gdim)  # b,hw,7
        gconf = gyolos[:, :, 0]  # torch.Size([5, 169])
        mask_pos = gconf > 0  # torch.Size([32, 169])
        # mask_pos = gconf == 1  # yolo1 gt 写死是1
        mask_neg = gconf == 0
        nums_pos = (mask_pos.sum(-1).to(
            torch.float)).clamp(min=torch.finfo(torch.float16).eps)
        nums_neg = (mask_neg.sum(-1).to(
            torch.float)).clamp(min=torch.finfo(torch.float16).eps)
        pyolos_pos = pyolos[mask_pos]  # torch.Size([32, 169, 13]) -> [nn, 13]
        gyolos_pos = gyolos[mask_pos]  # torch.Size([32, 169, 13]) -> [nn, 13]
        ''' ---------------- 类别-cls损失 ---------------- '''
        # # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
        pcls_sigmoid = pyolos[:, :, 1:s_].sigmoid()  # torch.Size([32, 169, 8])
        gcls = gyolos[:, :, 1:s_]  # torch.Size([32, 169, 13])
        _loss_val = x_bce(pcls_sigmoid, gcls, reduction="none")
        l_cls = ((_loss_val.sum(-1) * mask_pos).sum(-1) / nums_pos).mean()

        # pcls_sigmoid_pos = pyolos_pos[:, 1:s_].sigmoid()
        # gcls_pos = gyolos_pos[:, 1:s_]
        # _loss_val = x_bce(pcls_sigmoid_pos, gcls_pos, reduction="none")  # torch.Size([46, 3])
        # torch.Size([46, 3]) -> val
        # l_cls = _loss_val.sum(-1).mean()
        ''' ---------------- 类别-conf损失 ---------------- '''
        # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
        pconf_sigmoid = pyolos[:, :, 0].sigmoid()

        # ------------ conf-mse ------------''' 666666
        _loss_val = F.mse_loss(pconf_sigmoid, gconf,
                               reduction="none")  # 用MSE效果更好
        l_conf_pos = ((_loss_val * mask_pos).sum(-1) / nums_pos).mean() * 5.
        l_conf_neg = ((_loss_val * mask_neg).sum(-1) / nums_pos).mean() * 1.

        # 效果一样 169:1
        # pos_ = _loss_val[mask_pos]
        # l_conf_pos = pos_.mean() * 1
        # l_conf_neg = _loss_val[mask_neg].mean() * 3

        # ------------ conf_ohem  ap26_26 ------------'''
        # _loss_val = x_bce(pconf_sigmoid, gconf)
        # mask_ignore = torch.logical_not(torch.logical_or(mask_pos, mask_neg))
        # mask_neg_hard = f_ohem(_loss_val, nums_pos * 3, mask_pos=mask_pos, mash_ignore=mask_ignore)
        # l_conf_pos = ((_loss_val * mask_pos).sum(-1) / nums_pos).mean() * 3  # 正例越多反例越多
        # l_conf_neg = ((_loss_val * mask_neg_hard).sum(-1) / nums_pos).mean() * 3

        # ------------ focalloss   ------------
        # l_pos, l_neg = focalloss(pconf_sigmoid, gconf, mask_pos=mask_pos, is_debug=True, alpha=0.5)
        # l_conf_pos = (l_pos.sum(-1).sum(-1) / nums_pos).mean()
        # l_conf_neg = (l_neg.sum(-1).sum(-1) / nums_neg).mean() * 3

        log_dict = {}
        ''' ----------------回归损失   xy采用bce wh采用mes----------------- '''
        if cfg.MODE_TRAIN == 4:
            # ------------ iou损失   ------------
            # 解码pxywh 计算预测与 GT 的 iou 作为 gconf
            # preg_pos = pyolos_pos[:, s_:s_ + 4]
            # # 解码yolo1
            # pxy_pos_toff = preg_pos[..., :2].sigmoid()
            # pwh_pos = torch.exp(preg_pos[..., 2:])
            # pzxywh = torch.cat([pxy_pos_toff, pwh_pos], -1)

            # 这里是归一化的 gt
            gltrb_pos = gyolos_pos[:, s_ + 4 + 1:s_ + 4 + 1 + 4]

            ptxywh = pyolos[..., s_:s_ + 4]
            pltrb_pos = boxes_decode4yolo1(ptxywh, h, w, cfg)[mask_pos]

            iou_zg = bbox_iou4one(pltrb_pos, gltrb_pos, is_giou=True)
            # iou_zg = bbox_iou4y(xywh2ltrb4ts(pzxywh), gltrb_pos_tx, GIoU=True)
            # print(iou_zg)
            l_reg = (1 - iou_zg).mean() * 5
            ''' ---------------- loss完成 ----------------- '''
            l_total = l_conf_pos + l_conf_neg + l_cls + l_reg
            log_dict['l_reg'] = l_reg.item()
        else:
            # ------------ mse+bce   ------------ 666666
            # conf-1, cls-num_class, txywh-4, weight-1, gltrb-4
            # torch.Size([32, 169, 13])  9->实际是8
            ptxty_sigmoid = pyolos[:, :, s_:s_ + 2].sigmoid()  # 4:6
            ptwth = pyolos[:, :, s_ + 2:s_ + 4]  # 这里不需要归一

            weight = gyolos[:, :, s_ + 4]  # 这个是大小目标缩放比例
            gtxty = gyolos[:, :, s_:s_ + 2]  # torch.Size([5, 169, 2])
            gtwth = gyolos[:, :, s_ + 2:s_ + 4]

            # _loss_val = x_bce(ptxty_sigmoid, gtxty, reduction="none")
            _loss_val = F.mse_loss(ptxty_sigmoid, gtxty, reduction="none")
            l_txty = ((_loss_val.sum(-1) * mask_pos * weight).sum(-1) /
                      nums_pos).mean()
            _loss_val = F.mse_loss(ptwth, gtwth, reduction="none")
            l_twth = ((_loss_val.sum(-1) * mask_pos * weight).sum(-1) /
                      nums_pos).mean()
            ''' ---------------- loss完成 ----------------- '''
            l_total = l_conf_pos + l_conf_neg + l_cls + l_txty + l_twth
            log_dict['l_xy'] = l_txty.item()
            log_dict['l_wh'] = l_twth.item()

        log_dict['l_total'] = l_total.item()
        log_dict['l_conf_pos'] = l_conf_pos.item()
        log_dict['l_conf_neg'] = l_conf_neg.item()
        log_dict['l_cls'] = l_cls.item()

        log_dict['p_max'] = pconf_sigmoid.max().item()
        log_dict['p_min'] = pconf_sigmoid.min().item()
        log_dict['p_mean'] = pconf_sigmoid.mean().item()
        return l_total, log_dict