-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
446 additions
and
42 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import sys | ||
sys.path.append("..") | ||
import torch.nn as nn | ||
from modelR.backbones.mobilenetv2 import MobilenetV2 | ||
from modelR.necks.csa_drf_fpn import CSA_DRF_FPN | ||
from modelR.head.dsc_head import DSC_Head | ||
from modelR.head.dsc_head_hbb import Ordinary_Head | ||
from utils.utils_basic import * | ||
|
||
class LODet(nn.Module): | ||
""" | ||
Note : int the __init__(), to define the modules should be in order, because of the weight file is order | ||
""" | ||
def __init__(self, pre_weights=None): | ||
super(LODet, self).__init__() | ||
self.__anchors = torch.FloatTensor(cfg.MODEL["ANCHORS"]) | ||
self.__strides = torch.FloatTensor(cfg.MODEL["STRIDES"]) | ||
self.__nC = cfg.DATA["NUM"] | ||
self.__backnone = MobilenetV2(weight_path=pre_weights, extract_list=["6", "13", "conv"])#"17" | ||
self.__neck = CSA_DRF_FPN(fileters_in=[1280, 96, 32]) | ||
# small | ||
self.__head_s = DSC_Head(nC=self.__nC, anchors=self.__anchors[0], stride=self.__strides[0]) | ||
# medium | ||
self.__head_m = DSC_Head(nC=self.__nC, anchors=self.__anchors[1], stride=self.__strides[1]) | ||
# large | ||
self.__head_l = DSC_Head(nC=self.__nC, anchors=self.__anchors[2], stride=self.__strides[2]) | ||
|
||
def forward(self, x): | ||
out = [] | ||
x_s, x_m, x_l = self.__backnone(x) | ||
x_s, x_m, x_l = self.__neck(x_l, x_m, x_s) | ||
out.append(self.__head_s(x_s)) | ||
out.append(self.__head_m(x_m)) | ||
out.append(self.__head_l(x_l)) | ||
if self.training: | ||
p, p_d = list(zip(*out)) | ||
return p, p_d # smalll, medium, large | ||
else: | ||
p, p_d = list(zip(*out)) | ||
return p, torch.cat(p_d, 0) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import sys | ||
sys.path.append("../utils") | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
from utils import utils_basic | ||
import config.cfg_lodet as cfg | ||
|
||
class FocalLoss(nn.Module): | ||
def __init__(self, gamma=2.0, alpha=1.0, reduction="mean"): | ||
super(FocalLoss, self).__init__() | ||
self.__gamma = gamma | ||
self.__alpha = alpha | ||
self.__loss = nn.BCEWithLogitsLoss(reduction=reduction) | ||
def forward(self, input, target): | ||
loss = self.__loss(input=input, target=target) | ||
loss *= self.__alpha * torch.pow(torch.abs(target - torch.sigmoid(input)), self.__gamma) | ||
return loss | ||
|
||
class Loss(nn.Module): | ||
def __init__(self, anchors, strides, iou_threshold_loss=0.5): | ||
super(Loss, self).__init__() | ||
self.__iou_threshold_loss = iou_threshold_loss | ||
self.__strides = strides | ||
self.__scale_factor = cfg.SCALE_FACTOR | ||
self.__scale_factor_a = cfg.SCALE_FACTOR_A | ||
|
||
def forward(self, p, p_d, label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes): | ||
strides = self.__strides | ||
loss_s, loss_s_iou, loss_s_conf, loss_s_cls = self.__cal_loss_per_layer(p[0], p_d[0], label_sbbox, | ||
sbboxes, strides[0]) | ||
loss_m, loss_m_iou, loss_m_conf, loss_m_cls = self.__cal_loss_per_layer(p[1], p_d[1], label_mbbox, | ||
mbboxes, strides[1]) | ||
loss_l, loss_l_iou, loss_l_conf, loss_l_cls = self.__cal_loss_per_layer(p[2], p_d[2], label_lbbox, | ||
lbboxes, strides[2]) | ||
loss = loss_l + loss_m + loss_s | ||
loss_iou = loss_s_iou + loss_m_iou + loss_l_iou | ||
loss_conf = loss_s_conf + loss_m_conf + loss_l_conf | ||
loss_cls = loss_s_cls + loss_m_cls + loss_l_cls | ||
return loss, loss_iou, loss_conf, loss_cls | ||
|
||
def smooth_l1_loss(self, input, target, beta=1. / 9, size_average=True): | ||
n = torch.abs(input - target) | ||
cond = n < beta | ||
loss = torch.where(cond, 0.5 * n ** 2 / beta, n - 0.5 * beta) | ||
return loss | ||
|
||
def __cal_loss_per_layer(self, p, p_d, label, bboxes, stride): | ||
batch_size, grid = p.shape[:2] | ||
img_size = stride * grid | ||
p_d_xywh = p_d[..., :4] | ||
p_d_a = p_d[..., 4:8] | ||
p_d_r = p_d[..., 8:9] | ||
p_conf = p[..., 9:10] | ||
p_cls = p[..., 10:] | ||
|
||
label_xywh = label[..., :4] | ||
label_a = label[..., 4:8] | ||
label_r = label[...,8:9] | ||
label_s13 = label[...,9:10] | ||
label_s24 = label[..., 10:11] | ||
label_obj_mask = label[..., 11:12] | ||
label_mix = label[..., 12:13] | ||
label_cls = label[..., 13:] | ||
|
||
if cfg.TRAIN["IOU_TYPE"] == 'GIOU': | ||
xiou = utils_basic.GIOU_xywh_torch(p_d_xywh, label_xywh).unsqueeze(-1) | ||
elif cfg.TRAIN["IOU_TYPE"] == 'CIOU': | ||
xiou = utils_basic.CIOU_xywh_torch(p_d_xywh, label_xywh).unsqueeze(-1) | ||
bbox_loss_scale = self.__scale_factor - (self.__scale_factor-1.0) * label_xywh[..., 2:3] * label_xywh[..., 3:4] / (img_size ** 2) | ||
loss_iou = label_obj_mask * bbox_loss_scale * (1.0 - xiou) * label_mix | ||
|
||
#loss r | ||
loss_r = label_obj_mask * self.smooth_l1_loss (p_d_r, label_r) * label_mix * 16 | ||
a_sum = self.smooth_l1_loss(p_d_a, label_a) | ||
a_loss_scale = 1 + (self.__scale_factor_a -1)* (label_xywh[..., 2:3] * label_xywh[...,3:4] / (img_size ** 2)) | ||
loss_a = label_obj_mask * a_sum * label_mix * a_loss_scale | ||
onesa = torch.ones_like(p_d_r) | ||
d13 = p_d_xywh[..., 2:3] * torch.abs(onesa - p_d_a[..., 0:1] - p_d_a[..., 2:3]) | ||
s13 = p_d_xywh[..., 3:4] / torch.sqrt(torch.mul(d13, d13) + torch.mul(p_d_xywh[..., 3:4], p_d_xywh[..., 3:4])) | ||
d24 = p_d_xywh[..., 3:4] * torch.abs(onesa - p_d_a[..., 1:2] - p_d_a[..., 3:4]) | ||
s24 = p_d_xywh[..., 2:3] / torch.sqrt(torch.mul(d24, d24) + torch.mul(p_d_xywh[..., 2:3], p_d_xywh[..., 2:3])) | ||
s1234sum = self.smooth_l1_loss(s13, label_s13)*(1.0/(label_s13+1e-8)) + self.smooth_l1_loss(s24, label_s24)*(1.0/(label_s24+1e-8)) | ||
loss_s = label_obj_mask * s1234sum * label_mix | ||
|
||
FOCAL = FocalLoss(gamma=2, alpha=1.0, reduction="none") | ||
iou = utils_basic.iou_xywh_torch(p_d_xywh.unsqueeze(4), bboxes.unsqueeze(1).unsqueeze(1).unsqueeze(1)) | ||
iou_max = iou.max(-1, keepdim=True)[0] | ||
label_noobj_mask = (1.0 - label_obj_mask) * (iou_max < self.__iou_threshold_loss).float() | ||
|
||
loss_conf = (label_obj_mask * FOCAL(input=p_conf, target=label_obj_mask) + | ||
label_noobj_mask * FOCAL(input=p_conf, target=label_obj_mask)) * label_mix | ||
|
||
# loss classes | ||
BCE = nn.BCEWithLogitsLoss(reduction="none") | ||
loss_cls = label_obj_mask * BCE(input=p_cls, target=label_cls) * label_mix | ||
|
||
loss_iou = (torch.sum(loss_iou)) / batch_size | ||
loss_conf = (torch.sum(loss_conf)) / batch_size | ||
loss_cls = (torch.sum(loss_cls)) / batch_size | ||
loss_a = (torch.sum(loss_a)) / batch_size | ||
loss_r = (torch.sum(loss_r)) / batch_size | ||
loss_s = (torch.sum(loss_s)) / batch_size | ||
|
||
loss = loss_iou + (loss_a + loss_r) + loss_conf + loss_cls + loss_s | ||
|
||
return loss, loss_iou, loss_conf, loss_cls, loss_a, loss_r, loss_s |
Oops, something went wrong.