| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- import torch
- import torch.nn as nn
- import torch.nn.functional as F
- from torch.utils.data import DataLoader, DistributedSampler
- import torchvision
- import cv2
- import math
- import numpy as np
- from copy import deepcopy
- from thop import profile
- # ---------------------------- For Dataset ----------------------------
- ## build dataloader
- def build_dataloader(args, dataset, batch_size, collate_fn=None):
- # distributed
- if args.distributed:
- sampler = DistributedSampler(dataset)
- else:
- sampler = torch.utils.data.RandomSampler(dataset)
- batch_sampler_train = torch.utils.data.BatchSampler(sampler, batch_size, drop_last=True)
- dataloader = DataLoader(dataset, batch_sampler=batch_sampler_train,
- collate_fn=collate_fn, num_workers=args.num_workers, pin_memory=True)
-
- return dataloader
-
- ## collate_fn for dataloader
- class CollateFunc(object):
- def __call__(self, batch):
- targets = []
- images = []
- for sample in batch:
- image = sample[0]
- target = sample[1]
- images.append(image)
- targets.append(target)
- images = torch.stack(images, 0) # [B, C, H, W]
- return images, targets
- # ---------------------------- For Loss ----------------------------
- ## FocalLoss
- def sigmoid_focal_loss(inputs, targets, num_boxes, alpha: float = 0.25, gamma: float = 2):
- """
- Loss used in RetinaNet for dense detection: https://arxiv.org/abs/1708.02002.
- Args:
- inputs: A float tensor of arbitrary shape.
- The predictions for each example.
- targets: A float tensor with the same shape as inputs. Stores the binary
- classification label for each element in inputs
- (0 for the negative class and 1 for the positive class).
- alpha: (optional) Weighting factor in range (0,1) to balance
- positive vs negative examples. Default = -1 (no weighting).
- gamma: Exponent of the modulating factor (1 - p_t) to
- balance easy vs hard examples.
- Returns:
- Loss tensor
- """
- prob = inputs.sigmoid()
- ce_loss = F.binary_cross_entropy_with_logits(inputs, targets, reduction="none")
- p_t = prob * targets + (1 - prob) * (1 - targets)
- loss = ce_loss * ((1 - p_t) ** gamma)
- if alpha >= 0:
- alpha_t = alpha * targets + (1 - alpha) * (1 - targets)
- loss = alpha_t * loss
- return loss.mean(1).sum() / num_boxes
- ## InverseSigmoid
- def inverse_sigmoid(x, eps=1e-5):
- x = x.clamp(min=0, max=1)
- x1 = x.clamp(min=eps)
- x2 = (1 - x).clamp(min=eps)
- return torch.log(x1/x2)
- # ---------------------------- For Model ----------------------------
- ## fuse Conv & BN layer
- def fuse_conv_bn(module):
- """Recursively fuse conv and bn in a module.
- During inference, the functionary of batch norm layers is turned off
- but only the mean and var alone channels are used, which exposes the
- chance to fuse it with the preceding conv layers to save computations and
- simplify network structures.
- Args:
- module (nn.Module): Module to be fused.
- Returns:
- nn.Module: Fused module.
- """
- last_conv = None
- last_conv_name = None
-
- def _fuse_conv_bn(conv, bn):
- """Fuse conv and bn into one module.
- Args:
- conv (nn.Module): Conv to be fused.
- bn (nn.Module): BN to be fused.
- Returns:
- nn.Module: Fused module.
- """
- conv_w = conv.weight
- conv_b = conv.bias if conv.bias is not None else torch.zeros_like(
- bn.running_mean)
- factor = bn.weight / torch.sqrt(bn.running_var + bn.eps)
- conv.weight = nn.Parameter(conv_w *
- factor.reshape([conv.out_channels, 1, 1, 1]))
- conv.bias = nn.Parameter((conv_b - bn.running_mean) * factor + bn.bias)
- return conv
- for name, child in module.named_children():
- if isinstance(child,
- (nn.modules.batchnorm._BatchNorm, nn.SyncBatchNorm)):
- if last_conv is None: # only fuse BN that is after Conv
- continue
- fused_conv = _fuse_conv_bn(last_conv, child)
- module._modules[last_conv_name] = fused_conv
- # To reduce changes, set BN as Identity instead of deleting it.
- module._modules[name] = nn.Identity()
- last_conv = None
- elif isinstance(child, nn.Conv2d):
- last_conv = child
- last_conv_name = name
- else:
- fuse_conv_bn(child)
- return module
- ## replace module
- def replace_module(module, replaced_module_type, new_module_type, replace_func=None) -> nn.Module:
- """
- Replace given type in module to a new type. mostly used in deploy.
- Args:
- module (nn.Module): model to apply replace operation.
- replaced_module_type (Type): module type to be replaced.
- new_module_type (Type)
- replace_func (function): python function to describe replace logic. Defalut value None.
- Returns:
- model (nn.Module): module that already been replaced.
- """
- def default_replace_func(replaced_module_type, new_module_type):
- return new_module_type()
- if replace_func is None:
- replace_func = default_replace_func
- model = module
- if isinstance(module, replaced_module_type):
- model = replace_func(replaced_module_type, new_module_type)
- else: # recurrsively replace
- for name, child in module.named_children():
- new_child = replace_module(child, replaced_module_type, new_module_type)
- if new_child is not child: # child is already replaced
- model.add_module(name, new_child)
- return model
- ## compute FLOPs & Parameters
- def compute_flops(model, img_size, device):
- x = torch.randn(1, 3, img_size, img_size).to(device)
- print('==============================')
- flops, params = profile(model, inputs=(x, ), verbose=False)
- print('GFLOPs : {:.2f}'.format(flops / 1e9 * 2))
- print('Params : {:.2f} M'.format(params / 1e6))
- ## load trained weight
- def load_weight(model, path_to_ckpt, fuse_cbn=False):
- # check ckpt file
- if path_to_ckpt is None:
- print('no weight file ...')
- else:
- checkpoint = torch.load(path_to_ckpt, map_location='cpu')
- print('--------------------------------------')
- print('Best model infor:')
- print('Epoch: {}'.format(checkpoint["epoch"]))
- print('mAP: {}'.format(checkpoint["mAP"]))
- print('--------------------------------------')
- checkpoint_state_dict = checkpoint["model"]
- model.load_state_dict(checkpoint_state_dict)
- print('Finished loading model!')
- # fuse conv & bn
- if fuse_cbn:
- print('Fusing Conv & BN ...')
- model = fuse_conv_bn(model)
- return model
- ## Model EMA
- class ModelEMA(object):
- """ Updated Exponential Moving Average (EMA) from https://github.com/rwightman/pytorch-image-models
- Keeps a moving average of everything in the model state_dict (parameters and buffers)
- For EMA details see https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage
- """
- def __init__(self, cfg, model, updates=0):
- # Create EMA
- self.ema = deepcopy(self.de_parallel(model)).eval() # FP32 EMA
- self.updates = updates # number of EMA updates
- self.decay = lambda x: cfg['ema_decay'] * (1 - math.exp(-x / cfg['ema_tau'])) # decay exponential ramp (to help early epochs)
- for p in self.ema.parameters():
- p.requires_grad_(False)
- def is_parallel(self, model):
- # Returns True if model is of type DP or DDP
- return type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel)
- def de_parallel(self, model):
- # De-parallelize a model: returns single-GPU model if model is of type DP or DDP
- return model.module if self.is_parallel(model) else model
- def copy_attr(self, a, b, include=(), exclude=()):
- # Copy attributes from b to a, options to only include [...] and to exclude [...]
- for k, v in b.__dict__.items():
- if (len(include) and k not in include) or k.startswith('_') or k in exclude:
- continue
- else:
- setattr(a, k, v)
- def update(self, model):
- # Update EMA parameters
- self.updates += 1
- d = self.decay(self.updates)
- msd = self.de_parallel(model).state_dict() # model state_dict
- for k, v in self.ema.state_dict().items():
- if v.dtype.is_floating_point: # true for FP16 and FP32
- v *= d
- v += (1 - d) * msd[k].detach()
- # assert v.dtype == msd[k].dtype == torch.float32, f'{k}: EMA {v.dtype} and model {msd[k].dtype} must be FP32'
- def update_attr(self, model, include=(), exclude=('process_group', 'reducer')):
- # Update EMA attributes
- self.copy_attr(self.ema, model, include, exclude)
- ## SiLU
- class SiLU(nn.Module):
- """export-friendly version of nn.SiLU()"""
- @staticmethod
- def forward(x):
- return x * torch.sigmoid(x)
- # ---------------------------- NMS ----------------------------
- ## basic NMS
- def nms(bboxes, scores, nms_thresh):
- """"Pure Python NMS."""
- x1 = bboxes[:, 0] #xmin
- y1 = bboxes[:, 1] #ymin
- x2 = bboxes[:, 2] #xmax
- y2 = bboxes[:, 3] #ymax
- areas = (x2 - x1) * (y2 - y1)
- order = scores.argsort()[::-1]
- keep = []
- while order.size > 0:
- i = order[0]
- keep.append(i)
- # compute iou
- xx1 = np.maximum(x1[i], x1[order[1:]])
- yy1 = np.maximum(y1[i], y1[order[1:]])
- xx2 = np.minimum(x2[i], x2[order[1:]])
- yy2 = np.minimum(y2[i], y2[order[1:]])
- w = np.maximum(1e-10, xx2 - xx1)
- h = np.maximum(1e-10, yy2 - yy1)
- inter = w * h
- iou = inter / (areas[i] + areas[order[1:]] - inter + 1e-14)
- #reserve all the boundingbox whose ovr less than thresh
- inds = np.where(iou <= nms_thresh)[0]
- order = order[inds + 1]
- return keep
- ## class-agnostic NMS
- def multiclass_nms_class_agnostic(scores, labels, bboxes, nms_thresh):
- # nms
- keep = nms(bboxes, scores, nms_thresh)
- scores = scores[keep]
- labels = labels[keep]
- bboxes = bboxes[keep]
- return scores, labels, bboxes
- ## class-aware NMS
- def multiclass_nms_class_aware(scores, labels, bboxes, nms_thresh, num_classes):
- # nms
- keep = np.zeros(len(bboxes), dtype=np.int32)
- for i in range(num_classes):
- inds = np.where(labels == i)[0]
- if len(inds) == 0:
- continue
- c_bboxes = bboxes[inds]
- c_scores = scores[inds]
- c_keep = nms(c_bboxes, c_scores, nms_thresh)
- keep[inds[c_keep]] = 1
- keep = np.where(keep > 0)
- scores = scores[keep]
- labels = labels[keep]
- bboxes = bboxes[keep]
- return scores, labels, bboxes
- ## multi-class NMS
- def multiclass_nms(scores, labels, bboxes, nms_thresh, num_classes, class_agnostic=False):
- if class_agnostic:
- return multiclass_nms_class_agnostic(scores, labels, bboxes, nms_thresh)
- else:
- return multiclass_nms_class_aware(scores, labels, bboxes, nms_thresh, num_classes)
- def non_max_suppression(
- prediction,
- conf_thres=0.25,
- iou_thres=0.45,
- classes=None,
- agnostic=False,
- multi_label=False,
- max_det=300,
- nc=0, # number of classes (optional)
- max_nms=30000,
- max_wh=7680,
- ):
- """
- Perform non-maximum suppression (NMS) on a set of boxes, with support for masks and multiple labels per box.
- Args:
- prediction (torch.Tensor): A tensor of shape (batch_size, num_classes + 4 + num_masks, num_boxes)
- containing the predicted boxes, classes, and masks. The tensor should be in the format
- output by a model, such as YOLO.
- conf_thres (float): The confidence threshold below which boxes will be filtered out.
- Valid values are between 0.0 and 1.0.
- iou_thres (float): The IoU threshold below which boxes will be filtered out during NMS.
- Valid values are between 0.0 and 1.0.
- classes (List[int]): A list of class indices to consider. If None, all classes will be considered.
- agnostic (bool): If True, the model is agnostic to the number of classes, and all
- classes will be considered as one.
- multi_label (bool): If True, each box may have multiple labels.
- labels (List[List[Union[int, float, torch.Tensor]]]): A list of lists, where each inner
- list contains the apriori labels for a given image. The list should be in the format
- output by a dataloader, with each label being a tuple of (class_index, x1, y1, x2, y2).
- max_det (int): The maximum number of boxes to keep after NMS.
- nc (int, optional): The number of classes output by the model. Any indices after this will be considered masks.
- max_time_img (float): The maximum time (seconds) for processing one image.
- max_nms (int): The maximum number of boxes into torchvision.ops.nms().
- max_wh (int): The maximum box width and height in pixels
- Returns:
- (List[torch.Tensor]): A list of length batch_size, where each element is a tensor of
- shape (num_boxes, 6 + num_masks) containing the kept boxes, with columns
- (x1, y1, x2, y2, confidence, class, mask1, mask2, ...).
- """
- # Checks
- assert 0 <= conf_thres <= 1, f'Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0'
- assert 0 <= iou_thres <= 1, f'Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0'
- device = prediction.device # [N, C+4]
- nc = nc or (prediction.shape[1] - 4) # number of classes
- xc = prediction[:, 4:].amax(1) > conf_thres # candidates
- # Settings
- multi_label &= nc > 1 # multiple labels per box (adds 0.5ms/img)
- output = torch.zeros((0, 6), device=device)
- # Apply constraints
- prediction = prediction[xc] # confidence
- # If none remain process next image
- if not prediction.shape[0]:
- pass
- # Detections matrix nx6 (xyxy, conf, cls)
- box, cls = prediction.split((4, nc), 1)
- if multi_label:
- i, j = torch.where(cls > conf_thres)
- prediction = torch.cat((box[i], prediction[i, 4 + j, None], j[:, None].float()), 1)
- else: # best class only
- conf, j = cls.max(1, keepdim=True)
- prediction = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres]
- # Filter by class
- if classes is not None:
- prediction = prediction[(prediction[:, 5:6] == torch.tensor(classes, device=device)).any(1)]
- # Check shape
- n = prediction.shape[0] # number of boxes
- if n > max_nms: # excess boxes
- prediction = prediction[prediction[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence and remove excess boxes
- # Batched NMS
- c = prediction[:, 5:6] * (0 if agnostic else max_wh) # classes
- boxes, scores = prediction[:, :4] + c, prediction[:, 4] # boxes (offset by class), scores
- i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS
- i = i[:max_det] # limit detections
- output = prediction[i]
- return output
- # ---------------------------- Processor for Deployment ----------------------------
- ## Pre-processer
- class PreProcessor(object):
- def __init__(self, img_size):
- self.img_size = img_size
- self.input_size = [img_size, img_size]
-
- def __call__(self, image, swap=(2, 0, 1)):
- """
- Input:
- image: (ndarray) [H, W, 3] or [H, W]
- formar: color format
- """
- if len(image.shape) == 3:
- padded_img = np.ones((self.input_size[0], self.input_size[1], 3), np.float32) * 114.
- else:
- padded_img = np.ones(self.input_size, np.float32) * 114.
- # resize
- orig_h, orig_w = image.shape[:2]
- r = min(self.input_size[0] / orig_h, self.input_size[1] / orig_w)
- resize_size = (int(orig_w * r), int(orig_h * r))
- if r != 1:
- resized_img = cv2.resize(image, resize_size, interpolation=cv2.INTER_LINEAR)
- else:
- resized_img = image
- # padding
- padded_img[:resized_img.shape[0], :resized_img.shape[1]] = resized_img
-
- # [H, W, C] -> [C, H, W]
- padded_img = padded_img.transpose(swap)
- padded_img = np.ascontiguousarray(padded_img, dtype=np.float32) / 255.
- return padded_img, r
- ## Post-processer
- class PostProcessor(object):
- def __init__(self, num_classes, conf_thresh=0.15, nms_thresh=0.5):
- self.num_classes = num_classes
- self.conf_thresh = conf_thresh
- self.nms_thresh = nms_thresh
- def __call__(self, predictions):
- """
- Input:
- predictions: (ndarray) [n_anchors_all, 4+1+C]
- """
- bboxes = predictions[..., :4]
- scores = predictions[..., 4:]
- # scores & labels
- labels = np.argmax(scores, axis=1) # [M,]
- scores = scores[(np.arange(scores.shape[0]), labels)] # [M,]
- # thresh
- keep = np.where(scores > self.conf_thresh)
- scores = scores[keep]
- labels = labels[keep]
- bboxes = bboxes[keep]
- # nms
- scores, labels, bboxes = multiclass_nms(
- scores, labels, bboxes, self.nms_thresh, self.num_classes, True)
- return bboxes, scores, labels
|