wwwwwwwwwwwwwwww
立即下载
资源介绍:
wwwwwwwwwwwwwwwwwwww
# Copyright (c) Horizon Robotics. All rights reserved.
# Source code reference to mmdetection, gluoncv, gluonnas.
import copy
import inspect
import logging
import math
import random
from abc import abstractmethod
from typing import Dict, List, Optional, Sequence, Tuple, Union
import cv2
import numpy as np
import torch
import torchvision
from hat.core.box_utils import bbox_overlaps, is_center_of_bboxes_in_roi
from hat.data.transforms.functional_img import (
image_normalize,
image_pad,
imresize,
imresize_pad_to_keep_ratio,
random_flip,
)
from hat.registry import OBJECT_REGISTRY
from .affine import (
AffineAugMat,
AffineMat2DGenerator,
AffineMatFromROIBoxGenerator,
AlphaImagePyramid,
ImageAffineTransform,
LabelAffineTransform,
_pad_array,
get_affine_image_resize,
resize_affine_mat,
)
from .bbox import (
clip_bbox,
filter_bbox,
remap_bbox_label_by_area,
remap_bbox_label_by_clip_area_ratio,
)
from .classification import BgrToYuv444, BgrToYuv444V2
from .common import Cast
from .gaze import eye_ldmk_mirror
try:
import albumentations
except ImportError:
albumentations = None
logger = logging.getLogger(__name__)
__all__ = [
"Resize",
"Resize3D",
"RandomFlip",
"Pad",
"Normalize",
"RandomCrop",
"ToTensor",
"Batchify",
"FixedCrop",
"PresetCrop",
"ColorJitter",
"RandomExpand",
"MinIoURandomCrop",
"AugmentHSV",
"get_dynamic_roi_from_camera",
"IterableDetRoITransform",
"PadDetData",
"ToFasterRCNNData",
"ToMultiTaskFasterRCNNData",
"PadTensorListToBatch",
"PlainCopyPaste",
"HueSaturationValue",
"RGBShift",
"MeanBlur",
"MedianBlur",
"RandomBrightnessContrast",
"ShiftScaleRotate",
"RandomResizedCrop",
"AlbuImageOnlyTransform",
"BoxJitter",
"RandomSizeCrop",
"DetYOLOv5MixUp",
"DetYOLOXMixUp",
"DetMosaic",
"DetAffineAugTransformer",
"IterableDetRoIListTransform",
]
def _transform_bboxes(
gt_boxes,
ig_regions,
img_roi,
affine_aug_param,
clip=True,
min_valid_area=8,
min_valid_clip_area_ratio=0.5,
min_edge_size=2,
complete_boxes=False,
):
bbox_ts = LabelAffineTransform(label_type="box")
ts_gt_boxes = bbox_ts(
gt_boxes, affine_aug_param.mat, flip=affine_aug_param.flipped
)
if clip:
clip_gt_boxes = clip_bbox(ts_gt_boxes, img_roi, need_copy=True)
else:
clip_gt_boxes = ts_gt_boxes
clip_gt_boxes = remap_bbox_label_by_area(clip_gt_boxes, min_valid_area)
clip_gt_boxes = remap_bbox_label_by_clip_area_ratio(
ts_gt_boxes, clip_gt_boxes, min_valid_clip_area_ratio
)
if clip and complete_boxes:
mask = filter_bbox(
clip_gt_boxes,
img_roi,
allow_outside_center=True,
min_edge_size=min_edge_size,
return_mask=True,
)
to_be_hard_flag = np.logical_and(
clip_gt_boxes[:, 4] < 0, ts_gt_boxes[:, 4] > 0
)
ts_gt_boxes[to_be_hard_flag, 4] *= -1
clip_gt_boxes = ts_gt_boxes[mask]
else:
clip_gt_boxes = filter_bbox(
clip_gt_boxes,
img_roi,
allow_outside_center=True,
min_edge_size=min_edge_size,
)
if ig_regions is not None:
ts_ig_regions = bbox_ts(
ig_regions, affine_aug_param.mat, flip=affine_aug_param.flipped
)
if clip:
clip_ig_regions = clip_bbox(ts_ig_regions, img_roi)
else:
clip_ig_regions = ts_ig_regions
else:
clip_ig_regions = None
return clip_gt_boxes, clip_ig_regions
@OBJECT_REGISTRY.register
class Resize(object):
"""Resize image & bbox & mask & seg.
.. note::
Affected keys: 'img', 'ori_img', 'img_shape', 'pad_shape',
'resized_shape', 'pad_shape', 'scale_factor', 'gt_bboxes',
'gt_seg', 'gt_ldmk'.
Args:
img_scale: See above.
max_scale: The max size of image. If the image's shape > max_scale,
The image is resized to max_scale
multiscale_mode: Value must be one of "range" or "value".
This transform resizes the input image and bbox to same scale
factor.
There are 3 multiscale modes:
'ratio_range' is not None: randomly sample a ratio from the ratio
range and multiply with the image scale.
e.g. Resize(img_scale=(400, 500)), multiscale_mode='range',
ratio_range=(0.5, 2.0)
'ratio_range' is None and 'multiscale_mode' == "range": randomly
sample a scale from a range, the length of img_scale[tuple] must be
2, which represent small img_scale and large img_scale.
e.g. Resize(img_scale=((100, 200), (400,500)),
multiscale_mode='range')
'ratio_range' is None and 'multiscale_mode' == "value": randomly
sample a scale from multiple scales.
e.g. Resize(img_scale=((100, 200), (300, 400), (400, 500)),
multiscale_mode='value')))
ratio_range: Scale factor range like (min_ratio, max_ratio).
keep_ratio: Whether to keep the aspect ratio when resizing the image.
pad_to_keep_ratio: Whether to pad image to keep the same shape
and aspect ratio when resizing the image to target shape.
raw_scaler_enable: Whether to enable raw scaler when resize the image.
sample1c_enable: Whether to sample one channel after resize the image.
divisor: Width and height are rounded to multiples of `divisor`.
rm_neg_coords: Whether to rm negative coordinates.
"""
def __init__(
self,
img_scale: Union[Sequence[int], Sequence[Sequence[int]]] = None,
max_scale: Union[Sequence[int], Sequence[Sequence[int]]] = None,
multiscale_mode: str = "range",
ratio_range: Tuple[float, float] = None,
keep_ratio: bool = True,
pad_to_keep_ratio: bool = False,
raw_scaler_enable: bool = False,
sample1c_enable: bool = False,
divisor: int = 1,
rm_neg_coords: bool = True,
split_transform: bool = False,
split_trans_w: int = 256,
split_trans_h: int = 256,
):
if img_scale is None:
self.img_scale = img_scale
else:
if isinstance(img_scale, (list, tuple)):
if isinstance(img_scale[0], (tuple, list)):
self.img_scale = img_scale
else:
self.img_scale = [img_scale]
else:
self.img_scale = [img_scale]
for value in self.img_scale:
assert isinstance(value, (tuple, list)), (
"you should set img_scale like a tupe/list or a list of "
"tuple/list"
)
self.max_scale = max_scale
if ratio_range is not None:
# mode 1: given a scale and a range of image ratio
assert len(self.img_scale) == 1
else:
# mode 2: given multiple scales or a range of scales
assert multiscale_mode in ["value", "range", "max_size"]
self.multiscale_mode = multiscale_mode
self.ratio_range = ratio_range
self.keep_ratio = keep_ratio
self.pad_to_keep_ratio = pad_to_keep_ratio
self.raw_scaler_enable = raw_scaler_enable
self.sample1c_enable = sample1c_enable
self.divisor = divisor
self.rm_neg_coords = rm_neg_coords
self.split_transform = split_transform
self.split_trans_w = split_trans_w
self.split_trans_h = split_trans_h
@staticmethod
def random_select(img_scales):
scale_idx = np.random.randint(len(img_scales))
img_scale = img_scales[scale_idx]
return img_scale, scale_idx
@staticmethod
def random_sample(img_scales):
assert len(img_scales) == 2
资源文件列表:
hat.zip 大约有1141个文件