python_code
stringlengths
0
992k
repo_name
stringlengths
8
46
file_path
stringlengths
5
162
from torch import nn from torch.autograd import Function from torch.autograd.function import once_differentiable from torch.nn.modules.utils import _pair # import alphaction._custom_cuda_ext as _C class _ROIPool3d(Function): @staticmethod def forward(ctx, input, roi, output_size, spatial_scale): ctx.output_size = _pair(output_size) ctx.spatial_scale = spatial_scale ctx.input_shape = input.size() output, argmax = _C.roi_pool_3d_forward( input, roi, spatial_scale, output_size[0], output_size[1] ) ctx.save_for_backward(input, roi, argmax) return output @staticmethod @once_differentiable def backward(ctx, grad_output): input, rois, argmax = ctx.saved_tensors output_size = ctx.output_size spatial_scale = ctx.spatial_scale bs, ch, l, h, w = ctx.input_shape grad_input = _C.roi_pool_3d_backward( grad_output, input, rois, argmax, spatial_scale, output_size[0], output_size[1], bs, ch, l, h, w, ) return grad_input, None, None, None roi_pool_3d = _ROIPool3d.apply class ROIPool3d(nn.Module): def __init__(self, output_size, spatial_scale): super(ROIPool3d, self).__init__() self.output_size = output_size self.spatial_scale = spatial_scale def forward(self, input, rois): return roi_pool_3d(input, rois, self.output_size, self.spatial_scale) def __repr__(self): tmpstr = self.__class__.__name__ + "(" tmpstr += "output_size=" + str(self.output_size) tmpstr += ", spatial_scale=" + str(self.spatial_scale) tmpstr += ")" return tmpstr
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/layers/roi_pool_3d.py
import torch from .roi_align_3d import ROIAlign3d # from .roi_align_3d import roi_align_3d from .roi_pool_3d import ROIPool3d from .roi_pool_3d import roi_pool_3d from .batch_norm import FrozenBatchNorm1d, FrozenBatchNorm2d, FrozenBatchNorm3d from .sigmoid_focal_loss import SigmoidFocalLoss from .softmax_focal_loss import SoftmaxFocalLoss # __all__ = ["roi_align_3d", "ROIAlign3d", "roi_pool_3d", "ROIPool3d", # "SigmoidFocalLoss", "SoftmaxFocalLoss", "FrozenBatchNorm1d", # "FrozenBatchNorm2d", "FrozenBatchNorm3d", # ] __all__ = ["ROIAlign3d", "roi_pool_3d", "ROIPool3d", "SigmoidFocalLoss", "SoftmaxFocalLoss", "FrozenBatchNorm1d", "FrozenBatchNorm2d", "FrozenBatchNorm3d", ]
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/layers/__init__.py
import torch from torch import nn from torch.autograd import Function from torch.autograd.function import once_differentiable # import alphaction._custom_cuda_ext as _C class _SigmoidFocalLoss(Function): @staticmethod def forward(ctx, logits, targets, gamma, alpha): ctx.save_for_backward(logits, targets) ctx.gamma = gamma ctx.alpha = alpha losses = _C.sigmoid_focalloss_forward( logits, targets, gamma, alpha ) return losses @staticmethod @once_differentiable def backward(ctx, d_loss): logits, targets = ctx.saved_tensors gamma = ctx.gamma alpha = ctx.alpha d_logits = _C.sigmoid_focalloss_backward( logits, targets, d_loss, gamma, alpha ) return d_logits, None, None, None def sigmoid_focal_loss(logits, targets, gamma, alpha, reduction='mean'): assert reduction in ["none", "mean", "sum"], "Unsupported reduction type \"{}\"".format(reduction) logits = logits.float() targets = targets.float() ret = _SigmoidFocalLoss.apply(logits, targets, gamma, alpha) if reduction != "none": ret = torch.mean(ret) if reduction == "mean" else torch.sum(ret) return ret class SigmoidFocalLoss(nn.Module): def __init__(self, gamma, alpha, reduction="mean"): super(SigmoidFocalLoss, self).__init__() self.gamma = gamma self.alpha = alpha self.reduction = reduction def forward(self, logits, targets): loss = sigmoid_focal_loss(logits, targets, self.gamma, self.alpha, self.reduction) return loss def __repr__(self): tmpstr = self.__class__.__name__ + "(" tmpstr += "gamma=" + str(self.gamma) tmpstr += ", alpha=" + str(self.alpha) tmpstr += ")" return tmpstr
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/layers/sigmoid_focal_loss.py
import torch from torch import nn from torch.autograd import Function from torch.autograd.function import once_differentiable from torch.nn.modules.utils import _pair # import alphaction._custom_cuda_ext as _C from torchvision.ops import roi_align # class _ROIAlign3d(Function): # @staticmethod # def forward(ctx, input, roi, output_size, spatial_scale, sampling_ratio): # ctx.save_for_backward(roi) # ctx.output_size = _pair(output_size) # ctx.spatial_scale = spatial_scale # ctx.sampling_ratio = sampling_ratio # ctx.input_shape = input.size() # output = _C.roi_align_3d_forward( # input, roi, spatial_scale, output_size[0], output_size[1], sampling_ratio # ) # return output # # @staticmethod # @once_differentiable # def backward(ctx, grad_output): # rois, = ctx.saved_tensors # output_size = ctx.output_size # spatial_scale = ctx.spatial_scale # sampling_ratio = ctx.sampling_ratio # bs, ch, l, h, w = ctx.input_shape # grad_input = _C.roi_align_3d_backward( # grad_output, # rois, # spatial_scale, # output_size[0], # output_size[1], # bs, # ch, # l, # h, # w, # sampling_ratio, # ) # return grad_input, None, None, None, None # # # roi_align_3d = _ROIAlign3d.apply class ROIAlign3d(nn.Module): def __init__(self, output_size, spatial_scale, sampling_ratio): super(ROIAlign3d, self).__init__() self.output_size = output_size self.spatial_scale = spatial_scale self.sampling_ratio = sampling_ratio def forward(self, input, rois): # return roi_align_3d( # input, rois, self.output_size, self.spatial_scale, self.sampling_ratio # ) # aia提供的algin不支持半精度,先使用torchvision return roi_align(input, rois, self.output_size, self.spatial_scale, self.sampling_ratio) def __repr__(self): tmpstr = self.__class__.__name__ + "(" tmpstr += "output_size=" + str(self.output_size) tmpstr += ", spatial_scale=" + str(self.spatial_scale) tmpstr += ", sampling_ratio=" + str(self.sampling_ratio) tmpstr += ")" return tmpstr
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/layers/roi_align_3d.py
import torch from torch import nn class _FrozenBatchNorm(nn.Module): def __init__(self, num_features, eps=1e-5, affine=True, track_running_stats=True): super(_FrozenBatchNorm, self).__init__() self.num_features = num_features self.eps = eps self.affine = affine self.track_running_stats = track_running_stats if self.affine: self.register_buffer("weight", torch.Tensor(num_features)) self.register_buffer("bias", torch.Tensor(num_features)) else: self.register_buffer("weight", None) self.register_buffer("bias", None) if self.track_running_stats: self.register_buffer('running_mean', torch.zeros(num_features)) self.register_buffer('running_var', torch.ones(num_features)) else: self.register_parameter('running_mean', None) self.register_parameter('running_var', None) self.reset_parameters() def reset_running_stats(self): if self.track_running_stats: self.running_mean.zero_() self.running_var.fill_(1) def reset_parameters(self): self.reset_running_stats() if self.affine: self.weight.data.uniform_() self.bias.data.zero_() def _check_input_dim(self, input): raise NotImplementedError def forward(self, input): self._check_input_dim(input) view_shape = (1, self.num_features) + (1,) * (input.dim() - 2) if self.track_running_stats: scale = self.weight / (self.running_var + self.eps).sqrt() bias = self.bias - self.running_mean * scale else: scale = self.weight bias = self.bias return scale.view(*view_shape) * input + bias.view(*view_shape) def extra_repr(self): return '{num_features}, eps={eps}, affine={affine}, ' \ 'track_running_stats={track_running_stats}'.format(**self.__dict__) def _load_from_state_dict(self, state_dict, prefix, metadata, strict, missing_keys, unexpected_keys, error_msgs): num_batches_tracked_key = prefix + 'num_batches_tracked' if num_batches_tracked_key in state_dict: del state_dict[num_batches_tracked_key] super(_FrozenBatchNorm, self)._load_from_state_dict( state_dict, prefix, metadata, strict, missing_keys, unexpected_keys, error_msgs) class FrozenBatchNorm1d(_FrozenBatchNorm): def _check_input_dim(self, input): if input.dim() != 2 and input.dim() != 3: raise ValueError('expected 2D or 3D input (got {}D input)' .format(input.dim())) class FrozenBatchNorm2d(_FrozenBatchNorm): def _check_input_dim(self, input): if input.dim() != 4: raise ValueError('expected 4D input (got {}D input)' .format(input.dim())) class FrozenBatchNorm3d(_FrozenBatchNorm): def _check_input_dim(self, input): if input.dim() != 5: raise ValueError('expected 5D input (got {}D input)' .format(input.dim()))
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/layers/batch_norm.py
from collections import defaultdict class MemoryPool(object): def __init__(self): self.cache = defaultdict(dict) def update(self, update_info): for movie_id, feature_per_movie in update_info.items(): self.cache[movie_id].update(feature_per_movie) def update_list(self, update_info_list): for update_info in update_info_list: self.update(update_info) def __getitem__(self, item): if isinstance(item, tuple) and len(item)==2: return self.cache[item[0]][item[1]] return self.cache[item] def __setitem__(self, key, value): if isinstance(key, tuple) and len(key)==2: self.cache[key[0]][key[1]] = value else: self.cache[key] = value def __delitem__(self, item): if isinstance(item, tuple) and len(item)==2: del self.cache[item[0]][item[1]] else: del self.cache[item] def __contains__(self, item): if isinstance(item, tuple) and len(item)==2: return (item[0] in self.cache and item[1] in self.cache[item[0]]) return (item in self.cache) def items(self): return self.cache.items()
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/structures/memory_pool.py
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/structures/__init__.py
# Modified from https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/structures/bounding_box.py import torch import pdb # transpose FLIP_LEFT_RIGHT = 0 FLIP_TOP_BOTTOM = 1 class BoxList(object): """ This class represents a set of bounding boxes. The bounding boxes are represented as a Nx4 Tensor. In order to uniquely determine the bounding boxes with respect to an image, we also store the corresponding image dimensions. They can contain extra information that is specific to each bounding box, such as labels. """ def __init__(self, bbox, image_size, mode="xyxy"): device = bbox.device if isinstance(bbox, torch.Tensor) else torch.device("cpu") bbox = torch.as_tensor(bbox, dtype=torch.float32, device=device) if bbox.ndimension() != 2: raise ValueError( "bbox should have 2 dimensions, got {}".format(bbox.ndimension()) ) if bbox.size(-1) != 4: raise ValueError( "last dimension of bbox should have a " "size of 4, got {}".format(bbox.size(-1)) ) if mode not in ("xyxy", "xywh"): raise ValueError("mode should be 'xyxy' or 'xywh'") self.bbox = bbox self.size = image_size # (image_width, image_height) self.mode = mode self.extra_fields = {} def add_field(self, field, field_data): self.extra_fields[field] = field_data def get_field(self, field): return self.extra_fields[field] def has_field(self, field): return field in self.extra_fields def delete_field(self, field): return self.extra_fields.pop(field, None) def fields(self): return list(self.extra_fields.keys()) def _copy_extra_fields(self, bbox): for k, v in bbox.extra_fields.items(): self.extra_fields[k] = v def convert(self, mode): if mode not in ("xyxy", "xywh"): raise ValueError("mode should be 'xyxy' or 'xywh'") if mode == self.mode: return self # we only have two modes, so don't need to check # self.mode xmin, ymin, xmax, ymax = self._split_into_xyxy() if mode == "xyxy": bbox = torch.cat((xmin, ymin, xmax, ymax), dim=-1) bbox = BoxList(bbox, self.size, mode=mode) else: TO_REMOVE = 1 bbox = torch.cat( (xmin, ymin, xmax - xmin + TO_REMOVE, ymax - ymin + TO_REMOVE), dim=-1 ) bbox = BoxList(bbox, self.size, mode=mode) bbox._copy_extra_fields(self) return bbox def _split_into_xyxy(self): if self.mode == "xyxy": xmin, ymin, xmax, ymax = self.bbox.split(1, dim=-1) return xmin, ymin, xmax, ymax elif self.mode == "xywh": TO_REMOVE = 1 xmin, ymin, w, h = self.bbox.split(1, dim=-1) return ( xmin, ymin, xmin + (w - TO_REMOVE).clamp(min=0), ymin + (h - TO_REMOVE).clamp(min=0), ) else: raise RuntimeError("Should not be here") def resize(self, size, *args, **kwargs): """ Returns a resized copy of this bounding box :param size: The requested size in pixels, as a 2-tuple: (width, height). """ ratios = tuple(float(s) / float(s_orig) for s, s_orig in zip(size, self.size)) if ratios[0] == ratios[1]: ratio = ratios[0] scaled_box = self.bbox * ratio bbox = BoxList(scaled_box, size, mode=self.mode) # bbox._copy_extra_fields(self) for k, v in self.extra_fields.items(): if not isinstance(v, torch.Tensor) and (hasattr(v, "resize")): v = v.resize(size, *args, **kwargs) bbox.add_field(k, v) return bbox ratio_width, ratio_height = ratios xmin, ymin, xmax, ymax = self._split_into_xyxy() scaled_xmin = xmin * ratio_width scaled_xmax = xmax * ratio_width scaled_ymin = ymin * ratio_height scaled_ymax = ymax * ratio_height scaled_box = torch.cat( (scaled_xmin, scaled_ymin, scaled_xmax, scaled_ymax), dim=-1 ) bbox = BoxList(scaled_box, size, mode="xyxy") # bbox._copy_extra_fields(self) for k, v in self.extra_fields.items(): if not isinstance(v, torch.Tensor) and hasattr(v, "resize"): v = v.resize(size, *args, **kwargs) bbox.add_field(k, v) return bbox.convert(self.mode) def transpose(self, method): """ Transpose bounding box (flip or rotate in 90 degree steps) :param method: One of :py:attr:`PIL.Image.FLIP_LEFT_RIGHT`, :py:attr:`PIL.Image.FLIP_TOP_BOTTOM`, :py:attr:`PIL.Image.ROTATE_90`, :py:attr:`PIL.Image.ROTATE_180`, :py:attr:`PIL.Image.ROTATE_270`, :py:attr:`PIL.Image.TRANSPOSE` or :py:attr:`PIL.Image.TRANSVERSE`. """ if method not in (FLIP_LEFT_RIGHT, FLIP_TOP_BOTTOM): raise NotImplementedError( "Only FLIP_LEFT_RIGHT and FLIP_TOP_BOTTOM implemented" ) image_width, image_height = self.size xmin, ymin, xmax, ymax = self._split_into_xyxy() if method == FLIP_LEFT_RIGHT: TO_REMOVE = 1 transposed_xmin = image_width - xmax - TO_REMOVE transposed_xmax = image_width - xmin - TO_REMOVE transposed_ymin = ymin transposed_ymax = ymax elif method == FLIP_TOP_BOTTOM: transposed_xmin = xmin transposed_xmax = xmax transposed_ymin = image_height - ymax transposed_ymax = image_height - ymin transposed_boxes = torch.cat( (transposed_xmin, transposed_ymin, transposed_xmax, transposed_ymax), dim=-1 ) bbox = BoxList(transposed_boxes, self.size, mode="xyxy") for k, v in self.extra_fields.items(): if not isinstance(v, torch.Tensor) and (hasattr(v, 'transpose')): v = v.transpose(method) bbox.add_field(k, v) return bbox.convert(self.mode) def crop(self, box): """ Cropss a rectangular region from this bounding box. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate. """ xmin, ymin, xmax, ymax = self._split_into_xyxy() w, h = box[2] - box[0], box[3] - box[1] cropped_xmin = (xmin - box[0]).clamp(min=0, max=w) cropped_ymin = (ymin - box[1]).clamp(min=0, max=h) cropped_xmax = (xmax - box[0]).clamp(min=0, max=w) cropped_ymax = (ymax - box[1]).clamp(min=0, max=h) # TODO should I filter empty boxes here? if False: is_empty = (cropped_xmin == cropped_xmax) | (cropped_ymin == cropped_ymax) cropped_box = torch.cat( (cropped_xmin, cropped_ymin, cropped_xmax, cropped_ymax), dim=-1 ) bbox = BoxList(cropped_box, (w, h), mode="xyxy") # bbox._copy_extra_fields(self) for k, v in self.extra_fields.items(): if not isinstance(v, torch.Tensor): v = v.crop(box) bbox.add_field(k, v) return bbox.convert(self.mode) def extend(self, scale): """ Return a extended bounding box copy of this bounding box. All other fields should be keep unchanged. :param scale: By what extent the bounding boxes will be extended. :return: A extended copy. """ if len(scale)<2: x_scale = y_scale = scale[0] else: x_scale = scale[0] y_scale = scale[1] TO_REMOVE = 1 xmin, ymin, xmax, ymax = self._split_into_xyxy() boxw, boxh = xmax - xmin + TO_REMOVE, ymax - ymin + TO_REMOVE padw, padh = float(x_scale) * boxw / 2, float(y_scale) * boxh / 2 extended_xmin = xmin - padw extended_ymin = ymin - padh extended_xmax = xmax + padw extended_ymax = ymax + padh extended_box = torch.cat( (extended_xmin, extended_ymin, extended_xmax, extended_ymax), dim=-1 ) bbox = BoxList(extended_box, self.size, mode='xyxy') bbox.clip_to_image() for k, v in self.extra_fields.items(): bbox.add_field(k, v) return bbox.convert(self.mode) def random_aug(self, jitter_x_out, jitter_x_in, jitter_y_out, jitter_y_in): TO_REMOVE = 1 xmin, ymin, xmax, ymax = self._split_into_xyxy() device = xmin.device def torch_uniform(rows, a=0.0, b=1.0): return torch.rand(rows, 1, dtype=torch.float32, device=device) * (b - a) + a boxw, boxh = xmax - xmin + TO_REMOVE, ymax - ymin + TO_REMOVE num_boxes = len(self) jitter_xmin = xmin + boxw * torch_uniform(num_boxes, -jitter_x_out, jitter_x_in) jitter_ymin = ymin + boxh * torch_uniform(num_boxes, -jitter_y_out, jitter_y_in) jitter_xmax = xmax + boxw * torch_uniform(num_boxes, -jitter_x_in, jitter_x_out) jitter_ymax = ymax + boxh * torch_uniform(num_boxes, -jitter_y_in, jitter_y_out) jitter_xmin.clamp_(min=0, max=self.size[0] - TO_REMOVE - 1) jitter_ymin.clamp_(min=0, max=self.size[1] - TO_REMOVE - 1) jitter_xmax = torch.max(torch.clamp(jitter_xmax, max=self.size[0] - TO_REMOVE), jitter_xmin + 1) jitter_ymax = torch.max(torch.clamp(jitter_ymax, max=self.size[1] - TO_REMOVE), jitter_ymin + 1) aug_box = torch.cat( (jitter_xmin, jitter_ymin, jitter_xmax, jitter_ymax), dim=-1 ) bbox = BoxList(aug_box, self.size, mode='xyxy') bbox.clip_to_image(remove_empty=False) for k, v in self.extra_fields.items(): bbox.add_field(k, v) return bbox.convert(self.mode) # Tensor-like methods def to(self, device): bbox = BoxList(self.bbox.to(device), self.size, self.mode) for k, v in self.extra_fields.items(): if hasattr(v, "to"): v = v.to(device) bbox.add_field(k, v) return bbox def top_k(self, k): # categories, bbox, scores if "scores" in self.extra_fields: scores = self.extra_fields["scores"] length = len(scores) start = max(length - k, 0) idx = torch.argsort(scores)[start:] bbox = BoxList(self.bbox[[idx]], self.size, self.mode) for k, v in self.extra_fields.items(): if isinstance(v, torch.Tensor): bbox.add_field(k, v[idx]) else: bbox.add_field(k, v) else: bbox = BoxList(self.bbox[:k], self.size, self.mode) for k, v in self.extra_fields.items(): if isinstance(v, torch.Tensor): bbox.add_field(k, v[:k]) else: bbox.add_field(k, v) return bbox def __getitem__(self, item): bbox = BoxList(self.bbox[item].reshape(-1,4), self.size, self.mode) for k, v in self.extra_fields.items(): if isinstance(v, torch.Tensor): bbox.add_field(k, v[item]) else: bbox.add_field(k, v) return bbox def update_box(self, box, mode): self.bbox = box self.convert(mode) def __len__(self): return self.bbox.shape[0] def clip_to_image(self, remove_empty=True): TO_REMOVE = 1 self.bbox[:, 0].clamp_(min=0, max=self.size[0] - TO_REMOVE) self.bbox[:, 1].clamp_(min=0, max=self.size[1] - TO_REMOVE) self.bbox[:, 2].clamp_(min=0, max=self.size[0] - TO_REMOVE) self.bbox[:, 3].clamp_(min=0, max=self.size[1] - TO_REMOVE) if remove_empty: box = self.bbox keep = (box[:, 3] > box[:, 1]) & (box[:, 2] > box[:, 0]) return self[keep] return self def area(self): box = self.bbox if self.mode == "xyxy": TO_REMOVE = 1 area = (box[:, 2] - box[:, 0] + TO_REMOVE) * (box[:, 3] - box[:, 1] + TO_REMOVE) elif self.mode == "xywh": area = box[:, 2] * box[:, 3] else: raise RuntimeError("Should not be here") return area def copy_with_fields(self, fields, skip_missing=False): bbox = BoxList(self.bbox, self.size, self.mode) if not isinstance(fields, (list, tuple)): fields = [fields] for field in fields: if self.has_field(field): bbox.add_field(field, self.get_field(field)) elif not skip_missing: raise KeyError("Field '{}' not found in {}".format(field, self)) return bbox def __repr__(self): s = self.__class__.__name__ + "(" s += "num_boxes={}, ".format(len(self)) s += "image_width={}, ".format(self.size[0]) s += "image_height={}, ".format(self.size[1]) s += "mode={})".format(self.mode) return s if __name__ == "__main__": bbox = BoxList([[0, 0, 10, 10], [0, 0, 5, 5]], (10, 10)) s_bbox = bbox.resize((5, 5)) print(s_bbox) print(s_bbox.bbox) t_bbox = bbox.transpose(0) print(t_bbox) print(t_bbox.bbox)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/structures/bounding_box.py
from .defaults import _C as cfg
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/config/__init__.py
"""Centralized catalog of paths.""" import os class DatasetCatalog(object): DATA_DIR = "" DATASETS = { "ava_video_train_v2.2": { "video_root": "/mnt/cache/xingsen/ava_dataset/AVA/clips/trainval/", "ann_file": "/mnt/lustre/share_data/xingsen/ava_dataset/AVA/annotations/ava_train_v2.2_min.json", "box_file": "", "eval_file_paths": { "csv_gt_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_train_v2.2.csv", "labelmap_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_action_list_v2.2_for_activitynet_2019.pbtxt", "exclusion_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_train_excluded_timestamps_v2.2.csv", }, }, "ava_video_val_v2.2": { "video_root": "/mnt/cache/xingsen/ava_dataset/AVA/clips/trainval", "ann_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_v2.2_min.json", "box_file": "/mnt/cache/xingsen/ava_dataset/AVA/boxes/ava_val_det_person_bbox.json", "eval_file_paths": { "csv_gt_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_v2.2.csv", "labelmap_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_action_list_v2.2_for_activitynet_2019.pbtxt", "exclusion_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_excluded_timestamps_v2.2.csv", }, }, "kinetics_val":{ 'kinetics_annfile':"/mnt/cache/xingsen/xingsen2/kinetics_val_v1.0.json", "box_file":"/mnt/cache/xingsen/xingsen2/kinetics_person_box.json", "eval_file_paths":{ "csv_gt_file" : '/mnt/cache/xingsen/xingsen2/kinetics_val_gt_v1.0.csv', "labelmap_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_action_list_v2.2_for_activitynet_2019.pbtxt", "exclusion_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_excluded_timestamps_v2.2.csv", } }, "ak_train":{ "video_root" : "/mnt/cache/xingsen/ava_dataset/AVA/clips/trainval/", "ava_annfile": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_train_v2.2_min.json", "kinetics_annfile":"/mnt/cache/xingsen/xingsen2/kinetics_train_v1.0.json", "ava_eval_file_path":{ "csv_gt_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_train_v2.2.csv", "labelmap_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_action_list_v2.2_for_activitynet_2019.pbtxt", "exclusion_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_train_excluded_timestamps_v2.2.csv" } }, "ak_val":{ "video_root":"/mnt/cache/xingsen/ava_dataset/AVA/clips/trainval/", "ava_annfile":"/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_v2.2_min.json", "kinetics_annfile":"/mnt/cache/xingsen/xingsen2/kinetics_val_v1.0.json", "kinetics_box_file":"/mnt/cache/xingsen/xingsen2/kinetics_person_box.json", "ava_box_file":"/mnt/cache/xingsen/ava_dataset/AVA/boxes/ava_val_det_person_bbox.json", "eval_file_paths":{ "csv_gt_file":'/mnt/cache/xingsen/xingsen2/ak_val_gt.csv', "labelmap_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_action_list_v2.2_for_activitynet_2019.pbtxt", "exclusion_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_excluded_timestamps_v2.2.csv", }, "ava_eval_file_path":{ "csv_gt_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_v2.2.csv", "labelmap_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_action_list_v2.2_for_activitynet_2019.pbtxt", "exclusion_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_excluded_timestamps_v2.2.csv", }, "kinetics_eval_file_path":{ "csv_gt_file" : '/mnt/cache/xingsen/xingsen2/kinetics_val_gt_v1.0.csv', "labelmap_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_action_list_v2.2_for_activitynet_2019.pbtxt", "exclusion_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_excluded_timestamps_v2.2.csv" } } } @staticmethod def get(name): if "ava_video" in name: data_dir = DatasetCatalog.DATA_DIR args = DatasetCatalog.DATASETS[name] return dict( factory="Dataset", args=args ) raise RuntimeError("Dataset not available: {}".format(name))
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/config/paths_catalog.py
from yacs.config import CfgNode as CN # ----------------------------------------------------------------------------- # Config definition # ----------------------------------------------------------------------------- _C = CN() _C.MODEL = CN() _C.MODEL.WEIGHT = "" # ----------------------------------------------------------------------------- # ROI action head config. # ----------------------------------------------------------------------------- _C.MODEL.ROI_ACTION_HEAD = CN() # Feature extractor config. _C.MODEL.ROI_ACTION_HEAD.FEATURE_EXTRACTOR = "MaxpoolFeatureExtractor" # Config for pooler in feature extractor. # Pooler type, can be 'pooling3d' or 'align3d' _C.MODEL.ROI_ACTION_HEAD.POOLER_TYPE = 'align3d' _C.MODEL.ROI_ACTION_HEAD.POOLER_RESOLUTION = 7 _C.MODEL.ROI_ACTION_HEAD.POOLER_SCALE = 1./16 # Only used for align3d _C.MODEL.ROI_ACTION_HEAD.POOLER_SAMPLING_RATIO = 0 _C.MODEL.ROI_ACTION_HEAD.MEAN_BEFORE_POOLER = False _C.MODEL.ROI_ACTION_HEAD.MLP_HEAD_DIM = 1024 # Action predictor config. _C.MODEL.ROI_ACTION_HEAD.PREDICTOR = "FCPredictor" _C.MODEL.ROI_ACTION_HEAD.DROPOUT_RATE = 0.0 _C.MODEL.ROI_ACTION_HEAD.NUM_CLASSES = 80 # Action loss evaluator config. _C.MODEL.ROI_ACTION_HEAD.PROPOSAL_PER_CLIP = 10 _C.MODEL.ROI_ACTION_HEAD.NUM_PERSON_MOVEMENT_CLASSES = 14 _C.MODEL.ROI_ACTION_HEAD.NUM_OBJECT_MANIPULATION_CLASSES = 49 _C.MODEL.ROI_ACTION_HEAD.NUM_PERSON_INTERACTION_CLASSES = 17 _C.MODEL.ROI_ACTION_HEAD.POSE_LOSS_WEIGHT = 1.2 _C.MODEL.ROI_ACTION_HEAD.OBJECT_LOSS_WEIGHT = float(_C.MODEL.ROI_ACTION_HEAD.NUM_OBJECT_MANIPULATION_CLASSES) _C.MODEL.ROI_ACTION_HEAD.PERSON_LOSS_WEIGHT = float(_C.MODEL.ROI_ACTION_HEAD.NUM_PERSON_INTERACTION_CLASSES) # Focal loss config. _C.MODEL.ROI_ACTION_HEAD.FOCAL_LOSS = CN() _C.MODEL.ROI_ACTION_HEAD.FOCAL_LOSS.GAMMA = 2.0 _C.MODEL.ROI_ACTION_HEAD.FOCAL_LOSS.ALPHA = -1. # ----------------------------------------------------------------------------- # INPUT # ----------------------------------------------------------------------------- _C.INPUT = CN() # Size of the smallest side of the image during training _C.INPUT.MIN_SIZE_TRAIN = 256 # Maximum size of the side of the image during training _C.INPUT.MAX_SIZE_TRAIN = 464 # Size of the smallest side of the image during testing _C.INPUT.MIN_SIZE_TEST = 256 # Maximum size of the side of the image during testing _C.INPUT.MAX_SIZE_TEST = 464 # Values to be used for image normalization, in rgb order _C.INPUT.PIXEL_MEAN = [122.7717, 115.9465, 102.9801] # Values to be used for image normalization _C.INPUT.PIXEL_STD = [57.375, 57.375, 57.375] # Convert image to BGR format (for Caffe2 models) _C.INPUT.TO_BGR = False _C.INPUT.FRAME_NUM = 32 _C.INPUT.FRAME_SAMPLE_RATE = 2 _C.INPUT.TAU = 8 _C.INPUT.ALPHA = 8 _C.INPUT.SLOW_JITTER = False _C.INPUT.COLOR_JITTER = False _C.INPUT.HUE_JITTER = 20.0 #in degree, hue is in 0~360 _C.INPUT.SAT_JITTER = 0.1 _C.INPUT.VAL_JITTER = 0.1 # ----------------------------------------------------------------------------- # Dataset # ----------------------------------------------------------------------------- _C.DATASETS = CN() # List of the dataset names for training, as present in paths_catalog.py _C.DATASETS.TRAIN = () # List of the dataset names for testing, as present in paths_catalog.py _C.DATASETS.TEST = () # ----------------------------------------------------------------------------- # DataLoader # ----------------------------------------------------------------------------- _C.DATALOADER = CN() # Number of dataset loading threads _C.DATALOADER.NUM_WORKERS = 4 # If > 0, this enforces that each collated batch should have a size divisible # by SIZE_DIVISIBILITY _C.DATALOADER.SIZE_DIVISIBILITY = 16 # If True, each batch should contain only images for which the aspect ratio # is compatible. This groups portrait images together, and landscape images # are not batched with portrait images. _C.DATALOADER.ASPECT_RATIO_GROUPING = True # ---------------------------------------------------------------------------- # # Backbone options # ---------------------------------------------------------------------------- # _C.MODEL.BACKBONE = CN() # The backbone conv body to use # Available backbone conv-body should be registered in modeling.backbone.backbone.py _C.MODEL.BACKBONE.CONV_BODY = "Slowfast-Resnet50" _C.MODEL.BACKBONE.FROZEN_BN = False _C.MODEL.BACKBONE.BN_MOMENTUM = 0.1 _C.MODEL.BACKBONE.BN_EPSILON = 1e-05 # Kaiming: # We may use 0 to initialize the residual branch of a residual block, # so the inital state of the block is exactly identiy. This helps optimizaiton. _C.MODEL.BACKBONE.BN_INIT_GAMMA = 0.0 _C.MODEL.BACKBONE.I3D = CN() _C.MODEL.BACKBONE.I3D.CONV3_NONLOCAL = True _C.MODEL.BACKBONE.I3D.CONV4_NONLOCAL = True _C.MODEL.BACKBONE.I3D.CONV3_GROUP_NL = False # ---------------------------------------------------------------------------- # # Slowfast options # ---------------------------------------------------------------------------- # _C.MODEL.BACKBONE.SLOWFAST = CN() _C.MODEL.BACKBONE.SLOWFAST.BETA = 1./8 _C.MODEL.BACKBONE.SLOWFAST.LATERAL = 'tconv' _C.MODEL.BACKBONE.SLOWFAST.SLOW = CN() _C.MODEL.BACKBONE.SLOWFAST.SLOW.ACTIVE = True _C.MODEL.BACKBONE.SLOWFAST.SLOW.CONV3_NONLOCAL = True _C.MODEL.BACKBONE.SLOWFAST.SLOW.CONV4_NONLOCAL = True _C.MODEL.BACKBONE.SLOWFAST.SLOW.CONV3_GROUP_NL = False _C.MODEL.BACKBONE.SLOWFAST.FAST = CN() _C.MODEL.BACKBONE.SLOWFAST.FAST.ACTIVE = True _C.MODEL.BACKBONE.SLOWFAST.FAST.CONV3_NONLOCAL = False _C.MODEL.BACKBONE.SLOWFAST.FAST.CONV4_NONLOCAL = False _C.MODEL.BACKBONE.SLOWFAST.FAST.CONV3_GROUP_NL = False # ---------------------------------------------------------------------------- # # Nonlocal options # ---------------------------------------------------------------------------- # _C.MODEL.NONLOCAL = CN() _C.MODEL.NONLOCAL.CONV_INIT_STD = 0.01 _C.MODEL.NONLOCAL.USE_ZERO_INIT_CONV = False _C.MODEL.NONLOCAL.NO_BIAS = False _C.MODEL.NONLOCAL.USE_MAXPOOL = True _C.MODEL.NONLOCAL.USE_SOFTMAX = True _C.MODEL.NONLOCAL.USE_SCALE = True _C.MODEL.NONLOCAL.USE_BN = True _C.MODEL.NONLOCAL.FROZEN_BN = False _C.MODEL.NONLOCAL.BN_MOMENTUM = 0.1 _C.MODEL.NONLOCAL.BN_EPSILON = 1e-05 _C.MODEL.NONLOCAL.BN_INIT_GAMMA = 0.0 _C.MODEL.IA_STRUCTURE = CN() _C.MODEL.IA_STRUCTURE.ACTIVE = False _C.MODEL.IA_STRUCTURE.STRUCTURE = "serial" _C.MODEL.IA_STRUCTURE.MAX_PER_SEC = 5 _C.MODEL.IA_STRUCTURE.MAX_PERSON = 25 _C.MODEL.IA_STRUCTURE.DIM_IN = 2304 _C.MODEL.IA_STRUCTURE.DIM_INNER = 512 _C.MODEL.IA_STRUCTURE.DIM_OUT = 512 _C.MODEL.IA_STRUCTURE.PENALTY = True _C.MODEL.IA_STRUCTURE.LENGTH = (30, 30) _C.MODEL.IA_STRUCTURE.MEMORY_RATE = 1 _C.MODEL.IA_STRUCTURE.FUSION = "concat" _C.MODEL.IA_STRUCTURE.CONV_INIT_STD = 0.01 _C.MODEL.IA_STRUCTURE.DROPOUT = 0. _C.MODEL.IA_STRUCTURE.NO_BIAS = False _C.MODEL.IA_STRUCTURE.I_BLOCK_LIST = ['P', 'O', 'M', 'P', 'O', 'M'] _C.MODEL.IA_STRUCTURE.LAYER_NORM = False _C.MODEL.IA_STRUCTURE.TEMPORAL_POSITION = True _C.MODEL.IA_STRUCTURE.ROI_DIM_REDUCE = True _C.MODEL.IA_STRUCTURE.USE_ZERO_INIT_CONV = True _C.MODEL.IA_STRUCTURE.MAX_OBJECT = 0 # ---------------------------------------------------------------------------- # # Solver # ---------------------------------------------------------------------------- # _C.SOLVER = CN() _C.SOLVER.MAX_ITER = 75000 _C.SOLVER.BASE_LR = 0.02 _C.SOLVER.BIAS_LR_FACTOR = 2 _C.SOLVER.IA_LR_FACTOR = 1.0 _C.SOLVER.MOMENTUM = 0.9 _C.SOLVER.WEIGHT_DECAY = 0.0001 _C.SOLVER.WEIGHT_DECAY_BIAS = 0.0 # Use for bn _C.SOLVER.WEIGHT_DECAY_BN = 0.0 _C.SOLVER.SCHEDULER = "warmup_multi_step" _C.SOLVER.GAMMA = 0.1 _C.SOLVER.STEPS = (33750, 67500) _C.SOLVER.WARMUP_ON = True _C.SOLVER.WARMUP_FACTOR = 1.0 / 3 _C.SOLVER.WARMUP_ITERS = 500 _C.SOLVER.WARMUP_METHOD = "linear" _C.SOLVER.CHECKPOINT_PERIOD = 5000 _C.SOLVER.EVAL_PERIOD = 5000 # Number of video clips per batch # This is global, so if we have 8 GPUs and VIDEOS_PER_BATCH = 16, each GPU will # see 2 clips per batch _C.SOLVER.VIDEOS_PER_BATCH = 32 # ---------------------------------------------------------------------------- # # Specific test options # ---------------------------------------------------------------------------- # _C.TEST = CN() # Number of video clips per batch # This is global, so if we have 8 GPUs and VIDEOS_PER_BATCH = 16, each GPU will # see 2 clips per batch _C.TEST.VIDEOS_PER_BATCH = 16 # Config used in inference. _C.TEST.EXTEND_SCALE = (0.1, 0.05) _C.TEST.BOX_THRESH = 0.8 _C.TEST.ACTION_THRESH = 0.05 # ---------------------------------------------------------------------------- # # Misc options # ---------------------------------------------------------------------------- # _C.OUTPUT_DIR = "."
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/config/defaults.py
import bisect import copy import torch.utils.data from alphaction.utils.comm import get_world_size from alphaction.utils.IA_helper import has_object import alphaction.config.paths_catalog as paths_catalog from . import datasets as D from . import samplers from .collate_batch import BatchCollator from .transforms import build_transforms, build_object_transforms def build_dataset(cfg, dataset_list, transforms, dataset_catalog, is_train=True, object_transforms=None): """ Arguments: cfg: config object for the experiment. dataset_list (list[str]): Contains the names of the datasets, i.e., ava_video_train_v2.2, ava_video_val_v2.2, etc.. transforms (callable): transforms to apply to each (clip, target) sample. dataset_catalog (DatasetCatalog): contains the information on how to construct a dataset. is_train (bool): whether to setup the dataset for training or testing. object_transforms: transforms to apply to object boxes. """ if not isinstance(dataset_list, (list, tuple)): raise RuntimeError( "dataset_list should be a list of strings, got {}".format(dataset_list) ) datasets = [] for dataset_name in dataset_list: data = dataset_catalog.get(dataset_name) factory = getattr(D, data["factory"]) args = data["args"] if data["factory"] == "AVAVideoDataset": # for AVA, we want to remove clips without annotations # during training args["remove_clips_without_annotations"] = is_train args["frame_span"] = cfg.INPUT.FRAME_NUM*cfg.INPUT.FRAME_SAMPLE_RATE if not is_train: args["box_thresh"] = cfg.TEST.BOX_THRESH args["action_thresh"] = cfg.TEST.ACTION_THRESH else: # disable box_file when train, use only gt to train args["box_file"] = None if has_object(cfg.MODEL.IA_STRUCTURE): args["object_transforms"] = object_transforms else: args["object_file"] = None args["transforms"] = transforms # make dataset from factory dataset = factory(**args) datasets.append(dataset) # for testing, return a list of datasets if not is_train: return datasets # for training, concatenate all datasets into a single one dataset = datasets[0] if len(datasets) > 1: dataset = D.ConcatDataset(datasets) return [dataset] def make_data_sampler(dataset, shuffle, distributed): if distributed: return samplers.DistributedSampler(dataset, shuffle=shuffle) if shuffle: sampler = torch.utils.data.sampler.RandomSampler(dataset) else: sampler = torch.utils.data.sampler.SequentialSampler(dataset) return sampler def _quantize(x, bins): bins = copy.copy(bins) bins = sorted(bins) quantized = list(map(lambda y: bisect.bisect_right(bins, y), x)) return quantized def _compute_aspect_ratios(dataset): aspect_ratios = [] for i in range(len(dataset)): video_info = dataset.get_video_info(i) aspect_ratio = float(video_info["height"]) / float(video_info["width"]) aspect_ratios.append(aspect_ratio) return aspect_ratios def make_batch_data_sampler( dataset, sampler, aspect_grouping, videos_per_batch, num_iters=None, start_iter=0, drop_last=False ): if aspect_grouping: if not isinstance(aspect_grouping, (list, tuple)): aspect_grouping = [aspect_grouping] aspect_ratios = _compute_aspect_ratios(dataset) group_ids = _quantize(aspect_ratios, aspect_grouping) batch_sampler = samplers.GroupedBatchSampler( sampler, group_ids, videos_per_batch, drop_uneven=drop_last ) else: batch_sampler = torch.utils.data.sampler.BatchSampler( sampler, videos_per_batch, drop_last=drop_last ) if num_iters is not None: batch_sampler = samplers.IterationBasedBatchSampler( batch_sampler, num_iters, start_iter ) return batch_sampler def make_data_loader(cfg, is_train=True, is_distributed=False, start_iter=0): num_gpus = get_world_size() if is_train: # for training videos_per_batch = cfg.SOLVER.VIDEOS_PER_BATCH assert ( videos_per_batch % num_gpus == 0 ), "SOLVER.VIDEOS_PER_BATCH ({}) must be divisible by the number " "of GPUs ({}) used.".format(videos_per_batch, num_gpus) videos_per_gpu = videos_per_batch // num_gpus shuffle = True drop_last = True num_iters = cfg.SOLVER.MAX_ITER else: # for testing videos_per_batch = cfg.TEST.VIDEOS_PER_BATCH assert ( videos_per_batch % num_gpus == 0 ), "TEST.VIDEOS_PER_BATCH ({}) must be divisible by the number " "of GPUs ({}) used.".format(videos_per_batch, num_gpus) videos_per_gpu = videos_per_batch // num_gpus shuffle = False if not is_distributed else True drop_last = False num_iters = None start_iter = 0 # group images which have similar aspect ratio. In this case, we only # group in two cases: those with width / height > 1, and the other way around, # but the code supports more general grouping strategy aspect_grouping = [1] if cfg.DATALOADER.ASPECT_RATIO_GROUPING else [] DatasetCatalog = paths_catalog.DatasetCatalog dataset_list = cfg.DATASETS.TRAIN if is_train else cfg.DATASETS.TEST # build dataset transforms = build_transforms(cfg, is_train) if has_object(cfg.MODEL.IA_STRUCTURE): object_transforms = build_object_transforms(cfg, is_train=is_train) else: object_transforms = None datasets = build_dataset(cfg, dataset_list, transforms, DatasetCatalog, is_train, object_transforms) # build sampler and dataloader data_loaders = [] for dataset in datasets: sampler = make_data_sampler(dataset, shuffle, is_distributed) batch_sampler = make_batch_data_sampler( dataset, sampler, aspect_grouping, videos_per_gpu, num_iters, start_iter, drop_last ) collator = BatchCollator(cfg.DATALOADER.SIZE_DIVISIBILITY) num_workers = cfg.DATALOADER.NUM_WORKERS data_loader = torch.utils.data.DataLoader( dataset, num_workers=num_workers, batch_sampler=batch_sampler, collate_fn=collator, ) data_loaders.append(data_loader) if is_train: # during training, a single (possibly concatenated) data_loader is returned assert len(data_loaders) == 1 return data_loaders[0] return data_loaders
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/build.py
from .build import make_data_loader
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/__init__.py
import math def batch_different_videos(videos, size_divisible=0): ''' :param videos: a list of video tensors :param size_divisible: output_size(width and height) should be divisble by this param :return: batched videos as a single tensor ''' assert isinstance(videos, (tuple, list)) max_size = tuple(max(s) for s in zip(*[clip.shape for clip in videos])) if size_divisible > 0: stride = size_divisible max_size = list(max_size) max_size[2] = int(math.ceil(max_size[2] / stride) * stride) max_size[3] = int(math.ceil(max_size[3] / stride) * stride) max_size = tuple(max_size) batch_shape = (len(videos),) + max_size batched_clips = videos[0].new(*batch_shape).zero_() for clip, pad_clip in zip(videos, batched_clips): pad_clip[:clip.shape[0], :clip.shape[1], :clip.shape[2], :clip.shape[3]].copy_(clip) return batched_clips class BatchCollator(object): """ From a list of samples from the dataset, returns the batched objectimages and targets. This should be passed to the DataLoader """ def __init__(self, size_divisible=0): self.divisible = size_divisible self.size_divisible = self.divisible def __call__(self, batch): transposed_batch = list(zip(*batch)) slow_clips = batch_different_videos(transposed_batch[0], self.size_divisible) fast_clips = batch_different_videos(transposed_batch[1], self.size_divisible) boxes = transposed_batch[2] objects = transposed_batch[3] extras = transposed_batch[4] clip_ids = transposed_batch[5] return slow_clips, fast_clips, boxes, objects, extras, clip_ids
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/collate_batch.py
import bisect from torch.utils.data.dataset import ConcatDataset as _ConcatDataset class ConcatDataset(_ConcatDataset): """ Same as torch.utils.dataset.dataset.ConcatDataset, but exposes an extra method for querying the sizes of the image """ def get_idxs(self, idx): dataset_idx = bisect.bisect_right(self.cumulative_sizes, idx) if dataset_idx == 0: sample_idx = idx else: sample_idx = idx - self.cumulative_sizes[dataset_idx - 1] return dataset_idx, sample_idx def get_video_info(self, idx): dataset_idx, sample_idx = self.get_idxs(idx) return self.datasets[dataset_idx].get_video_info(sample_idx)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/concat_dataset.py
import os import torch.utils.data as data import time import torch import numpy as np from alphaction.structures.bounding_box import BoxList from collections import defaultdict from alphaction.utils.video_decode import av_decode_video import json # This is used to avoid pytorch issuse #13246 class NpInfoDict(object): def __init__(self, info_dict, key_type=None, value_type=None): keys = sorted(list(info_dict.keys())) self.key_arr = np.array(keys, dtype=key_type) self.val_arr = np.array([info_dict[k] for k in keys], dtype=value_type) # should not be used in dataset __getitem__ self._key_idx_map = {k:i for i,k in enumerate(keys)} def __getitem__(self, idx): return self.key_arr[idx], self.val_arr[idx] def update_value(self, idx, value): self.key_arr[idx],self.val_arr[idx] = value def __len__(self): return len(self.key_arr) def convert_key(self, org_key): # convert relevant variable whose original value is in the key set. # should not be used in __getitem__ return self._key_idx_map[org_key] # This is used to avoid pytorch issuse #13246 class NpBoxDict(object): def __init__(self, id_to_box_dict, key_list=None, value_types=[]): value_fields, value_types = list(zip(*value_types)) assert "bbox" in value_fields if key_list is None: key_list = sorted(list(id_to_box_dict.keys())) self.length = len(key_list) pointer_list = [] value_lists = {field: [] for field in value_fields} cur = 0 pointer_list.append(cur) for k in key_list: box_infos = id_to_box_dict[k] cur += len(box_infos) pointer_list.append(cur) for box_info in box_infos: for field in value_fields: value_lists[field].append(box_info[field]) self.pointer_arr = np.array(pointer_list, dtype=np.int32) self.attr_names = np.array(["vfield_" + field for field in value_fields]) for field_name, value_type, attr_name in zip(value_fields, value_types, self.attr_names): setattr(self, attr_name, np.array(value_lists[field_name], dtype=value_type)) def __getitem__(self, idx): l_pointer = self.pointer_arr[idx] r_pointer = self.pointer_arr[idx + 1] ret_val = [getattr(self, attr_name)[l_pointer:r_pointer] for attr_name in self.attr_names] return ret_val def __len__(self): return self.length class AVAVideoDataset(data.Dataset): def __init__(self, video_root, ann_file, remove_clips_without_annotations, frame_span, box_file=None, eval_file_paths={}, box_thresh=0.0, action_thresh=0.0, transforms=None, object_file=None, object_transforms=None,): print('loading annotations into memory...') tic = time.time() json_dict = json.load(open(ann_file, 'r')) assert type(json_dict) == dict, 'annotation file format {} not supported'.format(type(json_dict)) print('Done (t={:0.2f}s)'.format(time.time() - tic)) self.video_root = video_root self.transforms = transforms self.frame_span = frame_span # These two attributes are used during ava evaluation... # Maybe there is a better implementation self.eval_file_paths = eval_file_paths self.action_thresh = action_thresh clip2ann = defaultdict(list) if "annotations" in json_dict: for ann in json_dict["annotations"]: action_ids = ann["action_ids"] one_hot = np.zeros(81, dtype=np.bool) one_hot[action_ids] = True packed_act = np.packbits(one_hot[1:]) clip2ann[ann["image_id"]].append(dict(bbox=ann["bbox"], packed_act=packed_act)) movies_size = {} clips_info = {} for img in json_dict["images"]: mov = img["movie"] if mov not in movies_size: movies_size[mov] = [img["width"], img["height"]] clips_info[img["id"]] = [mov, img["timestamp"]] self.movie_info = NpInfoDict(movies_size, value_type=np.int32) clip_ids = sorted(list(clips_info.keys())) if remove_clips_without_annotations: clip_ids = [clip_id for clip_id in clip_ids if clip_id in clip2ann] if box_file: # this is only for validation or testing # we use detected boxes, so remove clips without boxes detected. imgToBoxes = self.load_box_file(box_file, box_thresh) clip_ids = [ img_id for img_id in clip_ids if len(imgToBoxes[img_id]) > 0 ] self.det_persons = NpBoxDict(imgToBoxes, clip_ids, value_types=[("bbox", np.float32), ("score", np.float32)]) else: self.det_persons = None if object_file: imgToObjects = self.load_box_file(object_file) self.det_objects = NpBoxDict(imgToObjects, clip_ids, value_types=[("bbox", np.float32), ("score", np.float32)]) else: self.det_objects = None if object_transforms: self.object_transforms = object_transforms else: self.object_transforms = None self.anns = NpBoxDict(clip2ann, clip_ids, value_types=[("bbox", np.float32), ("packed_act", np.uint8)]) clips_info = { clip_id: [ self.movie_info.convert_key(clips_info[clip_id][0]), clips_info[clip_id][1] ] for clip_id in clip_ids } self.clips_info = NpInfoDict(clips_info, value_type=np.int32) def __getitem__(self, idx): _, clip_info = self.clips_info[idx] # mov_id is the id in self.movie_info mov_id, timestamp = clip_info # movie_id is the human-readable youtube id. movie_id, movie_size = self.movie_info[mov_id] video_data = self._decode_video_data(movie_id, timestamp) im_w, im_h = movie_size if self.det_persons is None: # Note: During training, we only use gt. Thus we should not provide box file, # otherwise we will use only box file instead. boxes, packed_act = self.anns[idx] boxes_tensor = torch.as_tensor(boxes, dtype=torch.float32).reshape(-1, 4) # guard against no boxes boxes = BoxList(boxes_tensor, (im_w, im_h), mode="xywh").convert("xyxy") # Decode the packed bits from uint8 to one hot, since AVA has 80 classes, # it can be exactly denoted with 10 bytes, otherwise we may need to discard some bits. one_hot_label = np.unpackbits(packed_act, axis=1) one_hot_label = torch.as_tensor(one_hot_label, dtype=torch.uint8) boxes.add_field("labels", one_hot_label) else: boxes, box_score = self.det_persons[idx] boxes_tensor = torch.as_tensor(boxes).reshape(-1, 4) boxes = BoxList(boxes_tensor, (im_w, im_h), mode="xywh").convert("xyxy") boxes = boxes.clip_to_image(remove_empty=True) # extra fields extras = {} if self.transforms is not None: video_data, boxes, transform_randoms = self.transforms(video_data, boxes) slow_video, fast_video = video_data objects = None if self.det_objects is not None: objects = self.get_objects(idx, im_w, im_h) if self.object_transforms is not None: objects = self.object_transforms(objects, transform_randoms) # add infos neccessary for memory feature extras["movie_id"] = movie_id extras["timestamp"] = timestamp return slow_video, fast_video, boxes, objects, extras, idx return video_data, boxes, idx, movie_id, timestamp def return_null_box(self, im_w, im_h): return BoxList(torch.zeros((0, 4)), (im_w, im_h), mode="xyxy") def get_objects(self, idx, im_w, im_h): obj_boxes = self.return_null_box(im_w, im_h) if hasattr(self, 'det_objects'): boxes, box_score = self.det_objects[idx] if len(box_score) == 0: return obj_boxes obj_boxes_tensor = torch.as_tensor(boxes).reshape(-1, 4) obj_boxes = BoxList(obj_boxes_tensor, (im_w, im_h), mode="xywh").convert("xyxy") scores = torch.as_tensor(box_score) obj_boxes.add_field("scores", scores) return obj_boxes def get_video_info(self, index): _, clip_info = self.clips_info[index] # mov_id is the id in self.movie_info mov_id, timestamp = clip_info # movie_id is the human-readable youtube id. movie_id, movie_size = self.movie_info[mov_id] w, h = movie_size return dict(width=w, height=h, movie=movie_id, timestamp=timestamp) def load_box_file(self, box_file, score_thresh=0.0): import json print('Loading box file into memory...') tic = time.time() with open(box_file, "r") as f: box_results = json.load(f) print('Done (t={:0.2f}s)'.format(time.time() - tic)) boxImgIds = [box['image_id'] for box in box_results] imgToBoxes = defaultdict(list) for img_id, box in zip(boxImgIds, box_results): if box['score'] >= score_thresh: imgToBoxes[img_id].append(box) return imgToBoxes def _decode_video_data(self, dirname, timestamp): # decode target video data from segment per second. video_folder = os.path.join(self.video_root, dirname) right_span = self.frame_span//2 left_span = self.frame_span - right_span #load right cur_t = timestamp right_frames = [] while len(right_frames)<right_span: video_path = os.path.join(video_folder, "{}.mp4".format(cur_t)) # frames = cv2_decode_video(video_path) frames = av_decode_video(video_path) if len(frames)==0: raise RuntimeError("Video {} cannot be decoded.".format(video_path)) right_frames = right_frames+frames cur_t += 1 #load left cur_t = timestamp-1 left_frames = [] while len(left_frames)<left_span: video_path = os.path.join(video_folder, "{}.mp4".format(cur_t)) # frames = cv2_decode_video(video_path) frames = av_decode_video(video_path) if len(frames)==0: raise RuntimeError("Video {} cannot be decoded.".format(video_path)) left_frames = frames+left_frames cur_t -= 1 #adjust key frame to center, usually no need min_frame_num = min(len(left_frames), len(right_frames)) frames = left_frames[-min_frame_num:] + right_frames[:min_frame_num] video_data = np.stack(frames) return video_data def __len__(self): return len(self.clips_info) def __repr__(self): fmt_str = 'Dataset ' + self.__class__.__name__ + '\n' fmt_str += ' Number of datapoints: {}\n'.format(self.__len__()) fmt_str += ' Video Root Location: {}\n'.format(self.video_root) tmp = ' Transforms (if any): ' fmt_str += '{0}{1}\n'.format(tmp, self.transforms.__repr__().replace('\n', '\n' + ' ' * len(tmp))) return fmt_str
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/ava.py
from .concat_dataset import ConcatDataset from .ava import AVAVideoDataset __all__ = ["ConcatDataset", "AVAVideoDataset"]
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/__init__.py
from alphaction.dataset import datasets from .ava import ava_evaluation def evaluate(dataset, predictions, output_folder, **kwargs): """evaluate dataset using different methods based on dataset type. Args: dataset: Dataset object predictions(list[BoxList]): each item in the list represents the prediction results for one image. output_folder: output folder, to save evaluation files or results. **kwargs: other args. Returns: evaluation result """ args = dict( dataset=dataset, predictions=predictions, output_folder=output_folder, **kwargs ) if isinstance(dataset, datasets.AVAVideoDataset): return ava_evaluation(**args) else: dataset_name = dataset.__class__.__name__ raise NotImplementedError("Unsupported dataset type {}.".format(dataset_name))
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/__init__.py
import numpy as np import tempfile import os from pprint import pformat import csv import time from collections import defaultdict from .pascal_evaluation import object_detection_evaluation, standard_fields def do_ava_evaluation(dataset, predictions, output_folder, logger): logger.info("Preparing results for AVA format") ava_results = prepare_for_ava_detection(predictions, dataset) logger.info("Evaluating predictions") with tempfile.NamedTemporaryFile() as f: file_path = f.name if output_folder: file_path = os.path.join(output_folder, "result.csv") if len(dataset.eval_file_paths) == 0: write_csv(ava_results, file_path, logger) return eval_res = evaluate_predictions_on_ava( dataset.eval_file_paths, ava_results, file_path, logger ) logger.info(pformat(eval_res, indent=2)) if output_folder: log_file_path = os.path.join(output_folder, "result.log") with open(log_file_path, "a+") as logf: logf.write(pformat(eval_res)) return eval_res, ava_results def make_image_key(video_id, timestamp): """Returns a unique identifier for a video id & timestamp.""" return "%s,%04d" % (video_id, int(timestamp)) def decode_image_key(image_key): return image_key[:-5], image_key[-4:] def prepare_for_ava_detection(predictions, dataset): ava_results = {} score_thresh = dataset.action_thresh TO_REMOVE = 1.0 for video_id, prediction in enumerate(predictions): video_info = dataset.get_video_info(video_id) if len(prediction) == 0: continue video_width = video_info["width"] video_height = video_info["height"] prediction = prediction.resize((video_width, video_height)) prediction = prediction.convert("xyxy") boxes = prediction.bbox.numpy() boxes[:, [2, 3]] += TO_REMOVE boxes[:, [0, 2]] /= video_width boxes[:, [1, 3]] /= video_height boxes = np.clip(boxes, 0.0, 1.0) # No background class. scores = prediction.get_field("scores").numpy() box_ids, action_ids = np.where(scores >= score_thresh) boxes = boxes[box_ids, :] scores = scores[box_ids, action_ids] action_ids = action_ids + 1 movie_name = video_info['movie'] timestamp = video_info['timestamp'] clip_key = make_image_key(movie_name, timestamp) ava_results[clip_key] = { "boxes": boxes, "scores": scores, "action_ids": action_ids } return ava_results def read_exclusions(exclusions_file): """Reads a CSV file of excluded timestamps. Args: exclusions_file: Path of file containing a csv of video-id,timestamp. Returns: A set of strings containing excluded image keys, e.g. "aaaaaaaaaaa,0904", or an empty set if exclusions file is None. """ excluded = set() exclusions_file = open(exclusions_file, 'r') if exclusions_file: reader = csv.reader(exclusions_file) for row in reader: assert len(row) == 2, "Expected only 2 columns, got: {}".format(row) excluded.add(make_image_key(row[0], row[1])) return excluded def read_labelmap(labelmap_file): """Reads a labelmap without the dependency on protocol buffers. Args: labelmap_file: Path of file containing a label map protocol buffer. Returns: labelmap: The label map in the form used by the object_detection_evaluation module - a list of {"id": integer, "name": classname } dicts. class_ids: A set containing all of the valid class id integers. """ labelmap = [] class_ids = set() name = "" class_id = "" labelmap_file = open(labelmap_file, 'r') for line in labelmap_file: if line.startswith(" name:"): name = line.split('"')[1] elif line.startswith(" id:") or line.startswith(" label_id:"): class_id = int(line.strip().split(" ")[-1]) labelmap.append({"id": class_id, "name": name}) class_ids.add(class_id) return labelmap, class_ids def read_csv(csv_file, logger, class_whitelist=None): """Loads boxes and class labels from a CSV file in the AVA format. CSV file format described at https://research.google.com/ava/download.html. Args: csv_file: Path of csv file. class_whitelist: If provided, boxes corresponding to (integer) class labels not in this set are skipped. Returns: boxes: A dictionary mapping each unique image key (string) to a list of boxes, given as coordinates [y1, x1, y2, x2]. labels: A dictionary mapping each unique image key (string) to a list of integer class lables, matching the corresponding box in `boxes`. scores: A dictionary mapping each unique image key (string) to a list of score values lables, matching the corresponding label in `labels`. If scores are not provided in the csv, then they will default to 1.0. """ start = time.time() boxes = defaultdict(list) labels = defaultdict(list) scores = defaultdict(list) csv_file = open(csv_file, 'r') reader = csv.reader(csv_file) for row in reader: assert len(row) in [7, 8], "Wrong number of columns: " + row image_key = make_image_key(row[0], row[1]) x1, y1, x2, y2 = [float(n) for n in row[2:6]] action_id = int(row[6]) if class_whitelist and action_id not in class_whitelist: continue score = 1.0 if len(row) == 8: score = float(row[7]) boxes[image_key].append([y1, x1, y2, x2]) labels[image_key].append(action_id) scores[image_key].append(score) print_time(logger, "read file " + csv_file.name, start) return boxes, labels, scores def write_csv(ava_results, csv_result_file, logger): start = time.time() with open(csv_result_file, 'w') as csv_file: spamwriter = csv.writer(csv_file, delimiter=',') for clip_key in ava_results: movie_name, timestamp = decode_image_key(clip_key) cur_result = ava_results[clip_key] boxes = cur_result["boxes"] scores = cur_result["scores"] action_ids = cur_result["action_ids"] assert boxes.shape[0] == scores.shape[0] == action_ids.shape[0] for box, score, action_id in zip(boxes, scores, action_ids): box_str = ['{:.5f}'.format(cord) for cord in box] score_str = '{:.5f}'.format(score) spamwriter.writerow([movie_name, timestamp, ] + box_str + [action_id, score_str]) print_time(logger, "write file " + csv_result_file, start) def print_time(logger, message, start): logger.info("==> %g seconds to %s", time.time() - start, message) def evaluate_predictions_on_ava(eval_file_paths, ava_results, csv_result_file, logger): write_csv(ava_results, csv_result_file, logger) groundtruth = eval_file_paths["csv_gt_file"] labelmap = eval_file_paths["labelmap_file"] exclusions = eval_file_paths["exclusion_file"] categories, class_whitelist = read_labelmap(labelmap) logger.info("CATEGORIES (%d):\n%s", len(categories), pformat(categories, indent=2)) excluded_keys = read_exclusions(exclusions) pascal_evaluator = object_detection_evaluation.PascalDetectionEvaluator( categories) # Reads the ground truth dataset. boxes, labels, _ = read_csv(groundtruth, logger, class_whitelist) start = time.time() for image_key in boxes: if image_key in excluded_keys: logger.info(("Found excluded timestamp in ground truth: %s. " "It will be ignored."), image_key) continue pascal_evaluator.add_single_ground_truth_image_info( image_key, { standard_fields.InputDataFields.groundtruth_boxes: np.array(boxes[image_key], dtype=float), standard_fields.InputDataFields.groundtruth_classes: np.array(labels[image_key], dtype=int), standard_fields.InputDataFields.groundtruth_difficult: np.zeros(len(boxes[image_key]), dtype=bool) }) print_time(logger, "convert groundtruth", start) # Reads detections dataset. boxes, labels, scores = read_csv(csv_result_file, logger, class_whitelist) start = time.time() for image_key in boxes: if image_key in excluded_keys: logger.info(("Found excluded timestamp in detections: %s. " "It will be ignored."), image_key) continue pascal_evaluator.add_single_detected_image_info( image_key, { standard_fields.DetectionResultFields.detection_boxes: np.array(boxes[image_key], dtype=float), standard_fields.DetectionResultFields.detection_classes: np.array(labels[image_key], dtype=int), standard_fields.DetectionResultFields.detection_scores: np.array(scores[image_key], dtype=float) }) print_time(logger, "convert detections", start) start = time.time() metrics = pascal_evaluator.evaluate() print_time(logger, "run_evaluator", start) return metrics
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/ava_eval.py
import logging from .ava_eval import do_ava_evaluation def ava_evaluation(dataset, predictions, output_folder, **_): logger = logging.getLogger("alphaction.inference") logger.info("performing ava evaluation.") return do_ava_evaluation( dataset=dataset, predictions=predictions, output_folder=output_folder, logger=logger, )
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/__init__.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Functions for computing metrics like precision, recall, CorLoc and etc.""" from __future__ import division import numpy as np def compute_precision_recall(scores, labels, num_gt): """Compute precision and recall. Args: scores: A float numpy array representing detection score labels: A boolean numpy array representing true/false positive labels num_gt: Number of ground truth instances Raises: ValueError: if the input is not of the correct format Returns: precision: Fraction of positive instances over detected ones. This value is None if no ground truth labels are present. recall: Fraction of detected positive instance over all positive instances. This value is None if no ground truth labels are present. """ if not isinstance( labels, np.ndarray) or labels.dtype != np.bool or len(labels.shape) != 1: raise ValueError("labels must be single dimension bool numpy array") if not isinstance( scores, np.ndarray) or len(scores.shape) != 1: raise ValueError("scores must be single dimension numpy array") if num_gt < np.sum(labels): raise ValueError("Number of true positives must be smaller than num_gt.") if len(scores) != len(labels): raise ValueError("scores and labels must be of the same size.") if num_gt == 0: return None, None sorted_indices = np.argsort(scores) sorted_indices = sorted_indices[::-1] labels = labels.astype(int) true_positive_labels = labels[sorted_indices] false_positive_labels = 1 - true_positive_labels cum_true_positives = np.cumsum(true_positive_labels) cum_false_positives = np.cumsum(false_positive_labels) precision = cum_true_positives.astype(float) / ( cum_true_positives + cum_false_positives) recall = cum_true_positives.astype(float) / num_gt return precision, recall def compute_average_precision(precision, recall): """Compute Average Precision according to the definition in VOCdevkit. Precision is modified to ensure that it does not decrease as recall decrease. Args: precision: A float [N, 1] numpy array of precisions recall: A float [N, 1] numpy array of recalls Raises: ValueError: if the input is not of the correct format Returns: average_precison: The area under the precision recall curve. NaN if precision and recall are None. """ if precision is None: if recall is not None: raise ValueError("If precision is None, recall must also be None") return np.NAN if not isinstance(precision, np.ndarray) or not isinstance(recall, np.ndarray): raise ValueError("precision and recall must be numpy array") if precision.dtype != np.float or recall.dtype != np.float: raise ValueError("input must be float numpy array.") if len(precision) != len(recall): raise ValueError("precision and recall must be of the same size.") if not precision.size: return 0.0 if np.amin(precision) < 0 or np.amax(precision) > 1: raise ValueError("Precision must be in the range of [0, 1].") if np.amin(recall) < 0 or np.amax(recall) > 1: raise ValueError("recall must be in the range of [0, 1].") if not all(recall[i] <= recall[i + 1] for i in range(len(recall) - 1)): raise ValueError("recall must be a non-decreasing array") recall = np.concatenate([[0], recall, [1]]) precision = np.concatenate([[0], precision, [0]]) # Preprocess precision to be a non-decreasing array for i in range(len(precision) - 2, -1, -1): precision[i] = np.maximum(precision[i], precision[i + 1]) indices = np.where(recall[1:] != recall[:-1])[0] + 1 average_precision = np.sum( (recall[indices] - recall[indices - 1]) * precision[indices]) return average_precision def compute_cor_loc(num_gt_imgs_per_class, num_images_correctly_detected_per_class): """Compute CorLoc according to the definition in the following paper. https://www.robots.ox.ac.uk/~vgg/rg/papers/deselaers-eccv10.pdf Returns nans if there are no ground truth images for a class. Args: num_gt_imgs_per_class: 1D array, representing number of images containing at least one object instance of a particular class num_images_correctly_detected_per_class: 1D array, representing number of images that are correctly detected at least one object instance of a particular class Returns: corloc_per_class: A float numpy array represents the corloc score of each class """ # Divide by zero expected for classes with no gt examples. with np.errstate(divide="ignore", invalid="ignore"): return np.where( num_gt_imgs_per_class == 0, np.nan, num_images_correctly_detected_per_class / num_gt_imgs_per_class)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/metrics.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Bounding Box List operations for Numpy BoxLists. Example box operations that are supported: * Areas: compute bounding box areas * IOU: pairwise intersection-over-union scores """ import numpy as np from . import np_box_list, np_box_ops class SortOrder(object): """Enum class for sort order. Attributes: ascend: ascend order. descend: descend order. """ ASCEND = 1 DESCEND = 2 def area(boxlist): """Computes area of boxes. Args: boxlist: BoxList holding N boxes Returns: a numpy array with shape [N*1] representing box areas """ y_min, x_min, y_max, x_max = boxlist.get_coordinates() return (y_max - y_min) * (x_max - x_min) def intersection(boxlist1, boxlist2): """Compute pairwise intersection areas between boxes. Args: boxlist1: BoxList holding N boxes boxlist2: BoxList holding M boxes Returns: a numpy array with shape [N*M] representing pairwise intersection area """ return np_box_ops.intersection(boxlist1.get(), boxlist2.get()) def iou(boxlist1, boxlist2): """Computes pairwise intersection-over-union between box collections. Args: boxlist1: BoxList holding N boxes boxlist2: BoxList holding M boxes Returns: a numpy array with shape [N, M] representing pairwise iou scores. """ return np_box_ops.iou(boxlist1.get(), boxlist2.get()) def ioa(boxlist1, boxlist2): """Computes pairwise intersection-over-area between box collections. Intersection-over-area (ioa) between two boxes box1 and box2 is defined as their intersection area over box2's area. Note that ioa is not symmetric, that is, IOA(box1, box2) != IOA(box2, box1). Args: boxlist1: BoxList holding N boxes boxlist2: BoxList holding M boxes Returns: a numpy array with shape [N, M] representing pairwise ioa scores. """ return np_box_ops.ioa(boxlist1.get(), boxlist2.get()) def gather(boxlist, indices, fields=None): """Gather boxes from BoxList according to indices and return new BoxList. By default, gather returns boxes corresponding to the input index list, as well as all additional fields stored in the boxlist (indexing into the first dimension). However one can optionally only gather from a subset of fields. Args: boxlist: BoxList holding N boxes indices: a 1-d numpy array of type int_ fields: (optional) list of fields to also gather from. If None (default), all fields are gathered from. Pass an empty fields list to only gather the box coordinates. Returns: subboxlist: a BoxList corresponding to the subset of the input BoxList specified by indices Raises: ValueError: if specified field is not contained in boxlist or if the indices are not of type int_ """ if indices.size: if np.amax(indices) >= boxlist.num_boxes() or np.amin(indices) < 0: raise ValueError('indices are out of valid range.') subboxlist = np_box_list.BoxList(boxlist.get()[indices, :]) if fields is None: fields = boxlist.get_extra_fields() for field in fields: extra_field_data = boxlist.get_field(field) subboxlist.add_field(field, extra_field_data[indices, ...]) return subboxlist def sort_by_field(boxlist, field, order=SortOrder.DESCEND): """Sort boxes and associated fields according to a scalar field. A common use case is reordering the boxes according to descending scores. Args: boxlist: BoxList holding N boxes. field: A BoxList field for sorting and reordering the BoxList. order: (Optional) 'descend' or 'ascend'. Default is descend. Returns: sorted_boxlist: A sorted BoxList with the field in the specified order. Raises: ValueError: if specified field does not exist or is not of single dimension. ValueError: if the order is not either descend or ascend. """ if not boxlist.has_field(field): raise ValueError('Field ' + field + ' does not exist') if len(boxlist.get_field(field).shape) != 1: raise ValueError('Field ' + field + 'should be single dimension.') if order != SortOrder.DESCEND and order != SortOrder.ASCEND: raise ValueError('Invalid sort order') field_to_sort = boxlist.get_field(field) sorted_indices = np.argsort(field_to_sort) if order == SortOrder.DESCEND: sorted_indices = sorted_indices[::-1] return gather(boxlist, sorted_indices) def non_max_suppression(boxlist, max_output_size=10000, iou_threshold=1.0, score_threshold=-10.0): """Non maximum suppression. This op greedily selects a subset of detection bounding boxes, pruning away boxes that have high IOU (intersection over union) overlap (> thresh) with already selected boxes. In each iteration, the detected bounding box with highest score in the available pool is selected. Args: boxlist: BoxList holding N boxes. Must contain a 'scores' field representing detection scores. All scores belong to the same class. max_output_size: maximum number of retained boxes iou_threshold: intersection over union threshold. score_threshold: minimum score threshold. Remove the boxes with scores less than this value. Default value is set to -10. A very low threshold to pass pretty much all the boxes, unless the user sets a different score threshold. Returns: a BoxList holding M boxes where M <= max_output_size Raises: ValueError: if 'scores' field does not exist ValueError: if threshold is not in [0, 1] ValueError: if max_output_size < 0 """ if not boxlist.has_field('scores'): raise ValueError('Field scores does not exist') if iou_threshold < 0. or iou_threshold > 1.0: raise ValueError('IOU threshold must be in [0, 1]') if max_output_size < 0: raise ValueError('max_output_size must be bigger than 0.') boxlist = filter_scores_greater_than(boxlist, score_threshold) if boxlist.num_boxes() == 0: return boxlist boxlist = sort_by_field(boxlist, 'scores') # Prevent further computation if NMS is disabled. if iou_threshold == 1.0: if boxlist.num_boxes() > max_output_size: selected_indices = np.arange(max_output_size) return gather(boxlist, selected_indices) else: return boxlist boxes = boxlist.get() num_boxes = boxlist.num_boxes() # is_index_valid is True only for all remaining valid boxes, is_index_valid = np.full(num_boxes, 1, dtype=bool) selected_indices = [] num_output = 0 for i in range(num_boxes): if num_output < max_output_size: if is_index_valid[i]: num_output += 1 selected_indices.append(i) is_index_valid[i] = False valid_indices = np.where(is_index_valid)[0] if valid_indices.size == 0: break intersect_over_union = np_box_ops.iou( np.expand_dims(boxes[i, :], axis=0), boxes[valid_indices, :]) intersect_over_union = np.squeeze(intersect_over_union, axis=0) is_index_valid[valid_indices] = np.logical_and( is_index_valid[valid_indices], intersect_over_union <= iou_threshold) return gather(boxlist, np.array(selected_indices)) def multi_class_non_max_suppression(boxlist, score_thresh, iou_thresh, max_output_size): """Multi-class version of non maximum suppression. This op greedily selects a subset of detection bounding boxes, pruning away boxes that have high IOU (intersection over union) overlap (> thresh) with already selected boxes. It operates independently for each class for which scores are provided (via the scores field of the input box_list), pruning boxes with score less than a provided threshold prior to applying NMS. Args: boxlist: BoxList holding N boxes. Must contain a 'scores' field representing detection scores. This scores field is a tensor that can be 1 dimensional (in the case of a single class) or 2-dimensional, which which case we assume that it takes the shape [num_boxes, num_classes]. We further assume that this rank is known statically and that scores.shape[1] is also known (i.e., the number of classes is fixed and known at graph construction time). score_thresh: scalar threshold for score (low scoring boxes are removed). iou_thresh: scalar threshold for IOU (boxes that that high IOU overlap with previously selected boxes are removed). max_output_size: maximum number of retained boxes per class. Returns: a BoxList holding M boxes with a rank-1 scores field representing corresponding scores for each box with scores sorted in decreasing order and a rank-1 classes field representing a class label for each box. Raises: ValueError: if iou_thresh is not in [0, 1] or if input boxlist does not have a valid scores field. """ if not 0 <= iou_thresh <= 1.0: raise ValueError('thresh must be between 0 and 1') if not isinstance(boxlist, np_box_list.BoxList): raise ValueError('boxlist must be a BoxList') if not boxlist.has_field('scores'): raise ValueError('input boxlist must have \'scores\' field') scores = boxlist.get_field('scores') if len(scores.shape) == 1: scores = np.reshape(scores, [-1, 1]) elif len(scores.shape) == 2: if scores.shape[1] is None: raise ValueError('scores field must have statically defined second ' 'dimension') else: raise ValueError('scores field must be of rank 1 or 2') num_boxes = boxlist.num_boxes() num_scores = scores.shape[0] num_classes = scores.shape[1] if num_boxes != num_scores: raise ValueError('Incorrect scores field length: actual vs expected.') selected_boxes_list = [] for class_idx in range(num_classes): boxlist_and_class_scores = np_box_list.BoxList(boxlist.get()) class_scores = np.reshape(scores[0:num_scores, class_idx], [-1]) boxlist_and_class_scores.add_field('scores', class_scores) boxlist_filt = filter_scores_greater_than(boxlist_and_class_scores, score_thresh) nms_result = non_max_suppression(boxlist_filt, max_output_size=max_output_size, iou_threshold=iou_thresh, score_threshold=score_thresh) nms_result.add_field( 'classes', np.zeros_like(nms_result.get_field('scores')) + class_idx) selected_boxes_list.append(nms_result) selected_boxes = concatenate(selected_boxes_list) sorted_boxes = sort_by_field(selected_boxes, 'scores') return sorted_boxes def scale(boxlist, y_scale, x_scale): """Scale box coordinates in x and y dimensions. Args: boxlist: BoxList holding N boxes y_scale: float x_scale: float Returns: boxlist: BoxList holding N boxes """ y_min, x_min, y_max, x_max = np.array_split(boxlist.get(), 4, axis=1) y_min = y_scale * y_min y_max = y_scale * y_max x_min = x_scale * x_min x_max = x_scale * x_max scaled_boxlist = np_box_list.BoxList(np.hstack([y_min, x_min, y_max, x_max])) fields = boxlist.get_extra_fields() for field in fields: extra_field_data = boxlist.get_field(field) scaled_boxlist.add_field(field, extra_field_data) return scaled_boxlist def clip_to_window(boxlist, window): """Clip bounding boxes to a window. This op clips input bounding boxes (represented by bounding box corners) to a window, optionally filtering out boxes that do not overlap at all with the window. Args: boxlist: BoxList holding M_in boxes window: a numpy array of shape [4] representing the [y_min, x_min, y_max, x_max] window to which the op should clip boxes. Returns: a BoxList holding M_out boxes where M_out <= M_in """ y_min, x_min, y_max, x_max = np.array_split(boxlist.get(), 4, axis=1) win_y_min = window[0] win_x_min = window[1] win_y_max = window[2] win_x_max = window[3] y_min_clipped = np.fmax(np.fmin(y_min, win_y_max), win_y_min) y_max_clipped = np.fmax(np.fmin(y_max, win_y_max), win_y_min) x_min_clipped = np.fmax(np.fmin(x_min, win_x_max), win_x_min) x_max_clipped = np.fmax(np.fmin(x_max, win_x_max), win_x_min) clipped = np_box_list.BoxList( np.hstack([y_min_clipped, x_min_clipped, y_max_clipped, x_max_clipped])) clipped = _copy_extra_fields(clipped, boxlist) areas = area(clipped) nonzero_area_indices = np.reshape(np.nonzero(np.greater(areas, 0.0)), [-1]).astype(np.int32) return gather(clipped, nonzero_area_indices) def prune_non_overlapping_boxes(boxlist1, boxlist2, minoverlap=0.0): """Prunes the boxes in boxlist1 that overlap less than thresh with boxlist2. For each box in boxlist1, we want its IOA to be more than minoverlap with at least one of the boxes in boxlist2. If it does not, we remove it. Args: boxlist1: BoxList holding N boxes. boxlist2: BoxList holding M boxes. minoverlap: Minimum required overlap between boxes, to count them as overlapping. Returns: A pruned boxlist with size [N', 4]. """ intersection_over_area = ioa(boxlist2, boxlist1) # [M, N] tensor intersection_over_area = np.amax(intersection_over_area, axis=0) # [N] tensor keep_bool = np.greater_equal(intersection_over_area, np.array(minoverlap)) keep_inds = np.nonzero(keep_bool)[0] new_boxlist1 = gather(boxlist1, keep_inds) return new_boxlist1 def prune_outside_window(boxlist, window): """Prunes bounding boxes that fall outside a given window. This function prunes bounding boxes that even partially fall outside the given window. See also ClipToWindow which only prunes bounding boxes that fall completely outside the window, and clips any bounding boxes that partially overflow. Args: boxlist: a BoxList holding M_in boxes. window: a numpy array of size 4, representing [ymin, xmin, ymax, xmax] of the window. Returns: pruned_corners: a tensor with shape [M_out, 4] where M_out <= M_in. valid_indices: a tensor with shape [M_out] indexing the valid bounding boxes in the input tensor. """ y_min, x_min, y_max, x_max = np.array_split(boxlist.get(), 4, axis=1) win_y_min = window[0] win_x_min = window[1] win_y_max = window[2] win_x_max = window[3] coordinate_violations = np.hstack([np.less(y_min, win_y_min), np.less(x_min, win_x_min), np.greater(y_max, win_y_max), np.greater(x_max, win_x_max)]) valid_indices = np.reshape( np.where(np.logical_not(np.max(coordinate_violations, axis=1))), [-1]) return gather(boxlist, valid_indices), valid_indices def concatenate(boxlists, fields=None): """Concatenate list of BoxLists. This op concatenates a list of input BoxLists into a larger BoxList. It also handles concatenation of BoxList fields as long as the field tensor shapes are equal except for the first dimension. Args: boxlists: list of BoxList objects fields: optional list of fields to also concatenate. By default, all fields from the first BoxList in the list are included in the concatenation. Returns: a BoxList with number of boxes equal to sum([boxlist.num_boxes() for boxlist in BoxList]) Raises: ValueError: if boxlists is invalid (i.e., is not a list, is empty, or contains non BoxList objects), or if requested fields are not contained in all boxlists """ if not isinstance(boxlists, list): raise ValueError('boxlists should be a list') if not boxlists: raise ValueError('boxlists should have nonzero length') for boxlist in boxlists: if not isinstance(boxlist, np_box_list.BoxList): raise ValueError('all elements of boxlists should be BoxList objects') concatenated = np_box_list.BoxList( np.vstack([boxlist.get() for boxlist in boxlists])) if fields is None: fields = boxlists[0].get_extra_fields() for field in fields: first_field_shape = boxlists[0].get_field(field).shape first_field_shape = first_field_shape[1:] for boxlist in boxlists: if not boxlist.has_field(field): raise ValueError('boxlist must contain all requested fields') field_shape = boxlist.get_field(field).shape field_shape = field_shape[1:] if field_shape != first_field_shape: raise ValueError('field %s must have same shape for all boxlists ' 'except for the 0th dimension.' % field) concatenated_field = np.concatenate( [boxlist.get_field(field) for boxlist in boxlists], axis=0) concatenated.add_field(field, concatenated_field) return concatenated def filter_scores_greater_than(boxlist, thresh): """Filter to keep only boxes with score exceeding a given threshold. This op keeps the collection of boxes whose corresponding scores are greater than the input threshold. Args: boxlist: BoxList holding N boxes. Must contain a 'scores' field representing detection scores. thresh: scalar threshold Returns: a BoxList holding M boxes where M <= N Raises: ValueError: if boxlist not a BoxList object or if it does not have a scores field """ if not isinstance(boxlist, np_box_list.BoxList): raise ValueError('boxlist must be a BoxList') if not boxlist.has_field('scores'): raise ValueError('input boxlist must have \'scores\' field') scores = boxlist.get_field('scores') if len(scores.shape) > 2: raise ValueError('Scores should have rank 1 or 2') if len(scores.shape) == 2 and scores.shape[1] != 1: raise ValueError('Scores should have rank 1 or have shape ' 'consistent with [None, 1]') high_score_indices = np.reshape(np.where(np.greater(scores, thresh)), [-1]).astype(np.int32) return gather(boxlist, high_score_indices) def change_coordinate_frame(boxlist, window): """Change coordinate frame of the boxlist to be relative to window's frame. Given a window of the form [ymin, xmin, ymax, xmax], changes bounding box coordinates from boxlist to be relative to this window (e.g., the min corner maps to (0,0) and the max corner maps to (1,1)). An example use case is dataset augmentation: where we are given groundtruth boxes (boxlist) and would like to randomly crop the image to some window (window). In this case we need to change the coordinate frame of each groundtruth box to be relative to this new window. Args: boxlist: A BoxList object holding N boxes. window: a size 4 1-D numpy array. Returns: Returns a BoxList object with N boxes. """ win_height = window[2] - window[0] win_width = window[3] - window[1] boxlist_new = scale( np_box_list.BoxList(boxlist.get() - [window[0], window[1], window[0], window[1]]), 1.0 / win_height, 1.0 / win_width) _copy_extra_fields(boxlist_new, boxlist) return boxlist_new def _copy_extra_fields(boxlist_to_copy_to, boxlist_to_copy_from): """Copies the extra fields of boxlist_to_copy_from to boxlist_to_copy_to. Args: boxlist_to_copy_to: BoxList to which extra fields are copied. boxlist_to_copy_from: BoxList from which fields are copied. Returns: boxlist_to_copy_to with extra fields. """ for field in boxlist_to_copy_from.get_extra_fields(): boxlist_to_copy_to.add_field(field, boxlist_to_copy_from.get_field(field)) return boxlist_to_copy_to def _update_valid_indices_by_removing_high_iou_boxes( selected_indices, is_index_valid, intersect_over_union, threshold): max_iou = np.max(intersect_over_union[:, selected_indices], axis=1) return np.logical_and(is_index_valid, max_iou <= threshold)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/np_box_list_ops.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Numpy BoxList classes and functions.""" import numpy as np class BoxList(object): """Box collection. BoxList represents a list of bounding boxes as numpy array, where each bounding box is represented as a row of 4 numbers, [y_min, x_min, y_max, x_max]. It is assumed that all bounding boxes within a given list correspond to a single image. Optionally, users can add additional related fields (such as objectness/classification scores). """ def __init__(self, data): """Constructs box collection. Args: data: a numpy array of shape [N, 4] representing box coordinates Raises: ValueError: if bbox dataset is not a numpy array ValueError: if invalid dimensions for bbox dataset """ if not isinstance(data, np.ndarray): raise ValueError('dataset must be a numpy array.') if len(data.shape) != 2 or data.shape[1] != 4: raise ValueError('Invalid dimensions for box dataset.') if data.dtype != np.float32 and data.dtype != np.float64: raise ValueError('Invalid dataset type for box dataset: float is required.') if not self._is_valid_boxes(data): raise ValueError('Invalid box dataset. dataset must be a numpy array of ' 'N*[y_min, x_min, y_max, x_max]') self.data = {'boxes': data} def num_boxes(self): """Return number of boxes held in collections.""" return self.data['boxes'].shape[0] def get_extra_fields(self): """Return all non-box fields.""" return [k for k in self.data.keys() if k != 'boxes'] def has_field(self, field): return field in self.data def add_field(self, field, field_data): """Add dataset to a specified field. Args: field: a string parameter used to speficy a related field to be accessed. field_data: a numpy array of [N, ...] representing the dataset associated with the field. Raises: ValueError: if the field is already exist or the dimension of the field dataset does not matches the number of boxes. """ if self.has_field(field): raise ValueError('Field ' + field + 'already exists') if len(field_data.shape) < 1 or field_data.shape[0] != self.num_boxes(): raise ValueError('Invalid dimensions for field dataset') self.data[field] = field_data def get(self): """Convenience function for accesssing box coordinates. Returns: a numpy array of shape [N, 4] representing box corners """ return self.get_field('boxes') def get_field(self, field): """Accesses dataset associated with the specified field in the box collection. Args: field: a string parameter used to speficy a related field to be accessed. Returns: a numpy 1-d array representing dataset of an associated field Raises: ValueError: if invalid field """ if not self.has_field(field): raise ValueError('field {} does not exist'.format(field)) return self.data[field] def get_coordinates(self): """Get corner coordinates of boxes. Returns: a list of 4 1-d numpy arrays [y_min, x_min, y_max, x_max] """ box_coordinates = self.get() y_min = box_coordinates[:, 0] x_min = box_coordinates[:, 1] y_max = box_coordinates[:, 2] x_max = box_coordinates[:, 3] return [y_min, x_min, y_max, x_max] def _is_valid_boxes(self, data): """Check whether dataset fullfills the format of N*[ymin, xmin, ymax, xmin]. Args: data: a numpy array of shape [N, 4] representing box coordinates Returns: a boolean indicating whether all ymax of boxes are equal or greater than ymin, and all xmax of boxes are equal or greater than xmin. """ if data.shape[0] > 0: for i in range(data.shape[0]): if data[i, 0] > data[i, 2] or data[i, 1] > data[i, 3]: return False return True
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/np_box_list.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Numpy BoxMaskList classes and functions.""" import numpy as np from . import np_box_list class BoxMaskList(np_box_list.BoxList): """Convenience wrapper for BoxList with masks. BoxMaskList extends the np_box_list.BoxList to contain masks as well. In particular, its constructor receives both boxes and masks. Note that the masks correspond to the full image. """ def __init__(self, box_data, mask_data): """Constructs box collection. Args: box_data: a numpy array of shape [N, 4] representing box coordinates mask_data: a numpy array of shape [N, height, width] representing masks with values are in {0,1}. The masks correspond to the full image. The height and the width will be equal to image height and width. Raises: ValueError: if bbox dataset is not a numpy array ValueError: if invalid dimensions for bbox dataset ValueError: if mask dataset is not a numpy array ValueError: if invalid dimension for mask dataset """ super(BoxMaskList, self).__init__(box_data) if not isinstance(mask_data, np.ndarray): raise ValueError('Mask dataset must be a numpy array.') if len(mask_data.shape) != 3: raise ValueError('Invalid dimensions for mask dataset.') if mask_data.dtype != np.uint8: raise ValueError('Invalid dataset type for mask dataset: uint8 is required.') if mask_data.shape[0] != box_data.shape[0]: raise ValueError('There should be the same number of boxes and masks.') self.data['masks'] = mask_data def get_masks(self): """Convenience function for accessing masks. Returns: a numpy array of shape [N, height, width] representing masks """ return self.get_field('masks')
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/np_box_mask_list.py
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/__init__.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Contains classes specifying naming conventions used for object detection. Specifies: InputDataFields: standard fields used by reader/preprocessor/batcher. DetectionResultFields: standard fields returned by object detector. BoxListFields: standard field used by BoxList TfExampleFields: standard fields for tf-example dataset format (go/tf-example). """ class InputDataFields(object): """Names for the input tensors. Holds the standard dataset field names to use for identifying input tensors. This should be used by the decoder to identify keys for the returned tensor_dict containing input tensors. And it should be used by the model to identify the tensors it needs. Attributes: image: image. original_image: image in the original input size. key: unique key corresponding to image. source_id: source of the original image. filename: original filename of the dataset (without common path). groundtruth_image_classes: image-level class labels. groundtruth_boxes: coordinates of the ground truth boxes in the image. groundtruth_classes: box-level class labels. groundtruth_label_types: box-level label types (e.g. explicit negative). groundtruth_is_crowd: [DEPRECATED, use groundtruth_group_of instead] is the groundtruth a single object or a crowd. groundtruth_area: area of a groundtruth segment. groundtruth_difficult: is a `difficult` object groundtruth_group_of: is a `group_of` objects, e.g. multiple objects of the same class, forming a connected group, where instances are heavily occluding each other. proposal_boxes: coordinates of object proposal boxes. proposal_objectness: objectness score of each proposal. groundtruth_instance_masks: ground truth instance masks. groundtruth_instance_boundaries: ground truth instance boundaries. groundtruth_instance_classes: instance mask-level class labels. groundtruth_keypoints: ground truth keypoints. groundtruth_keypoint_visibilities: ground truth keypoint visibilities. groundtruth_label_scores: groundtruth label scores. groundtruth_weights: groundtruth weight factor for bounding boxes. num_groundtruth_boxes: number of groundtruth boxes. true_image_shapes: true shapes of images in the resized images, as resized images can be padded with zeros. """ image = 'image' original_image = 'original_image' key = 'key' source_id = 'source_id' filename = 'filename' groundtruth_image_classes = 'groundtruth_image_classes' groundtruth_boxes = 'groundtruth_boxes' groundtruth_classes = 'groundtruth_classes' groundtruth_label_types = 'groundtruth_label_types' groundtruth_is_crowd = 'groundtruth_is_crowd' groundtruth_area = 'groundtruth_area' groundtruth_difficult = 'groundtruth_difficult' groundtruth_group_of = 'groundtruth_group_of' proposal_boxes = 'proposal_boxes' proposal_objectness = 'proposal_objectness' groundtruth_instance_masks = 'groundtruth_instance_masks' groundtruth_instance_boundaries = 'groundtruth_instance_boundaries' groundtruth_instance_classes = 'groundtruth_instance_classes' groundtruth_keypoints = 'groundtruth_keypoints' groundtruth_keypoint_visibilities = 'groundtruth_keypoint_visibilities' groundtruth_label_scores = 'groundtruth_label_scores' groundtruth_weights = 'groundtruth_weights' num_groundtruth_boxes = 'num_groundtruth_boxes' true_image_shape = 'true_image_shape' class DetectionResultFields(object): """Naming conventions for storing the output of the detector. Attributes: source_id: source of the original image. key: unique key corresponding to image. detection_boxes: coordinates of the detection boxes in the image. detection_scores: detection scores for the detection boxes in the image. detection_classes: detection-level class labels. detection_masks: contains a segmentation mask for each detection box. detection_boundaries: contains an object boundary for each detection box. detection_keypoints: contains detection keypoints for each detection box. num_detections: number of detections in the batch. """ source_id = 'source_id' key = 'key' detection_boxes = 'detection_boxes' detection_scores = 'detection_scores' detection_classes = 'detection_classes' detection_masks = 'detection_masks' detection_boundaries = 'detection_boundaries' detection_keypoints = 'detection_keypoints' num_detections = 'num_detections' class BoxListFields(object): """Naming conventions for BoxLists. Attributes: boxes: bounding box coordinates. classes: classes per bounding box. scores: scores per bounding box. weights: sample weights per bounding box. objectness: objectness score per bounding box. masks: masks per bounding box. boundaries: boundaries per bounding box. keypoints: keypoints per bounding box. keypoint_heatmaps: keypoint heatmaps per bounding box. """ boxes = 'boxes' classes = 'classes' scores = 'scores' weights = 'weights' objectness = 'objectness' masks = 'masks' boundaries = 'boundaries' keypoints = 'keypoints' keypoint_heatmaps = 'keypoint_heatmaps' class TfExampleFields(object): """TF-example proto feature names for object detection. Holds the standard feature names to load from an Example proto for object detection. Attributes: image_encoded: JPEG encoded string image_format: image format, e.g. "JPEG" filename: filename channels: number of channels of image colorspace: colorspace, e.g. "RGB" height: height of image in pixels, e.g. 462 width: width of image in pixels, e.g. 581 source_id: original source of the image object_class_text: labels in text format, e.g. ["person", "cat"] object_class_label: labels in numbers, e.g. [16, 8] object_bbox_xmin: xmin coordinates of groundtruth box, e.g. 10, 30 object_bbox_xmax: xmax coordinates of groundtruth box, e.g. 50, 40 object_bbox_ymin: ymin coordinates of groundtruth box, e.g. 40, 50 object_bbox_ymax: ymax coordinates of groundtruth box, e.g. 80, 70 object_view: viewpoint of object, e.g. ["frontal", "left"] object_truncated: is object truncated, e.g. [true, false] object_occluded: is object occluded, e.g. [true, false] object_difficult: is object difficult, e.g. [true, false] object_group_of: is object a single object or a group of objects object_depiction: is object a depiction object_is_crowd: [DEPRECATED, use object_group_of instead] is the object a single object or a crowd object_segment_area: the area of the segment. object_weight: a weight factor for the object's bounding box. instance_masks: instance segmentation masks. instance_boundaries: instance boundaries. instance_classes: Classes for each instance segmentation mask. detection_class_label: class label in numbers. detection_bbox_ymin: ymin coordinates of a detection box. detection_bbox_xmin: xmin coordinates of a detection box. detection_bbox_ymax: ymax coordinates of a detection box. detection_bbox_xmax: xmax coordinates of a detection box. detection_score: detection score for the class label and box. """ image_encoded = 'image/encoded' image_format = 'image/format' # format is reserved keyword filename = 'image/filename' channels = 'image/channels' colorspace = 'image/colorspace' height = 'image/height' width = 'image/width' source_id = 'image/source_id' object_class_text = 'image/object/class/text' object_class_label = 'image/object/class/label' object_bbox_ymin = 'image/object/bbox/ymin' object_bbox_xmin = 'image/object/bbox/xmin' object_bbox_ymax = 'image/object/bbox/ymax' object_bbox_xmax = 'image/object/bbox/xmax' object_view = 'image/object/view' object_truncated = 'image/object/truncated' object_occluded = 'image/object/occluded' object_difficult = 'image/object/difficult' object_group_of = 'image/object/group_of' object_depiction = 'image/object/depiction' object_is_crowd = 'image/object/is_crowd' object_segment_area = 'image/object/segment/area' object_weight = 'image/object/weight' instance_masks = 'image/segmentation/object' instance_boundaries = 'image/boundaries/object' instance_classes = 'image/segmentation/object/class' detection_class_label = 'image/detection/label' detection_bbox_ymin = 'image/detection/bbox/ymin' detection_bbox_xmin = 'image/detection/bbox/xmin' detection_bbox_ymax = 'image/detection/bbox/ymax' detection_bbox_xmax = 'image/detection/bbox/xmax' detection_score = 'image/detection/score'
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/standard_fields.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Evaluate Object Detection result on a single image. Annotate each detected result as true positives or false positive according to a predefined IOU ratio. Non Maximum Supression is used by default. Multi class detection is supported by default. Based on the settings, per image evaluation is either performed on boxes or on object masks. """ import numpy as np from . import np_box_list_ops, np_box_mask_list_ops, np_box_list, np_box_mask_list class PerImageEvaluation(object): """Evaluate detection result of a single image.""" def __init__(self, num_groundtruth_classes, matching_iou_threshold=0.5, nms_iou_threshold=0.3, nms_max_output_boxes=50): """Initialized PerImageEvaluation by evaluation parameters. Args: num_groundtruth_classes: Number of ground truth object classes matching_iou_threshold: A ratio of area intersection to union, which is the threshold to consider whether a detection is true positive or not nms_iou_threshold: IOU threshold used in Non Maximum Suppression. nms_max_output_boxes: Number of maximum output boxes in NMS. """ self.matching_iou_threshold = matching_iou_threshold self.nms_iou_threshold = nms_iou_threshold self.nms_max_output_boxes = nms_max_output_boxes self.num_groundtruth_classes = num_groundtruth_classes def compute_object_detection_metrics( self, detected_boxes, detected_scores, detected_class_labels, groundtruth_boxes, groundtruth_class_labels, groundtruth_is_difficult_list, groundtruth_is_group_of_list, detected_masks=None, groundtruth_masks=None): """Evaluates detections as being tp, fp or ignored from a single image. The evaluation is done in two stages: 1. All detections are matched to non group-of boxes; true positives are determined and detections matched to difficult boxes are ignored. 2. Detections that are determined as false positives are matched against group-of boxes and ignored if matched. Args: detected_boxes: A float numpy array of shape [N, 4], representing N regions of detected object regions. Each row is of the format [y_min, x_min, y_max, x_max] detected_scores: A float numpy array of shape [N, 1], representing the confidence scores of the detected N object instances. detected_class_labels: A integer numpy array of shape [N, 1], repreneting the class labels of the detected N object instances. groundtruth_boxes: A float numpy array of shape [M, 4], representing M regions of object instances in ground truth groundtruth_class_labels: An integer numpy array of shape [M, 1], representing M class labels of object instances in ground truth groundtruth_is_difficult_list: A boolean numpy array of length M denoting whether a ground truth box is a difficult instance or not groundtruth_is_group_of_list: A boolean numpy array of length M denoting whether a ground truth box has group-of tag detected_masks: (optional) A uint8 numpy array of shape [N, height, width]. If not None, the metrics will be computed based on masks. groundtruth_masks: (optional) A uint8 numpy array of shape [M, height, width]. Returns: scores: A list of C float numpy arrays. Each numpy array is of shape [K, 1], representing K scores detected with object class label c tp_fp_labels: A list of C boolean numpy arrays. Each numpy array is of shape [K, 1], representing K True/False positive label of object instances detected with class label c is_class_correctly_detected_in_image: a numpy integer array of shape [C, 1], indicating whether the correponding class has a least one instance being correctly detected in the image """ detected_boxes, detected_scores, detected_class_labels, detected_masks = ( self._remove_invalid_boxes(detected_boxes, detected_scores, detected_class_labels, detected_masks)) scores, tp_fp_labels = self._compute_tp_fp( detected_boxes=detected_boxes, detected_scores=detected_scores, detected_class_labels=detected_class_labels, groundtruth_boxes=groundtruth_boxes, groundtruth_class_labels=groundtruth_class_labels, groundtruth_is_difficult_list=groundtruth_is_difficult_list, groundtruth_is_group_of_list=groundtruth_is_group_of_list, detected_masks=detected_masks, groundtruth_masks=groundtruth_masks) is_class_correctly_detected_in_image = self._compute_cor_loc( detected_boxes=detected_boxes, detected_scores=detected_scores, detected_class_labels=detected_class_labels, groundtruth_boxes=groundtruth_boxes, groundtruth_class_labels=groundtruth_class_labels, detected_masks=detected_masks, groundtruth_masks=groundtruth_masks) return scores, tp_fp_labels, is_class_correctly_detected_in_image def _compute_cor_loc(self, detected_boxes, detected_scores, detected_class_labels, groundtruth_boxes, groundtruth_class_labels, detected_masks=None, groundtruth_masks=None): """Compute CorLoc score for object detection result. Args: detected_boxes: A float numpy array of shape [N, 4], representing N regions of detected object regions. Each row is of the format [y_min, x_min, y_max, x_max] detected_scores: A float numpy array of shape [N, 1], representing the confidence scores of the detected N object instances. detected_class_labels: A integer numpy array of shape [N, 1], repreneting the class labels of the detected N object instances. groundtruth_boxes: A float numpy array of shape [M, 4], representing M regions of object instances in ground truth groundtruth_class_labels: An integer numpy array of shape [M, 1], representing M class labels of object instances in ground truth detected_masks: (optional) A uint8 numpy array of shape [N, height, width]. If not None, the scores will be computed based on masks. groundtruth_masks: (optional) A uint8 numpy array of shape [M, height, width]. Returns: is_class_correctly_detected_in_image: a numpy integer array of shape [C, 1], indicating whether the correponding class has a least one instance being correctly detected in the image Raises: ValueError: If detected masks is not None but groundtruth masks are None, or the other way around. """ if (detected_masks is not None and groundtruth_masks is None) or (detected_masks is None and groundtruth_masks is not None): raise ValueError( 'If `detected_masks` is provided, then `groundtruth_masks` should ' 'also be provided.' ) is_class_correctly_detected_in_image = np.zeros( self.num_groundtruth_classes, dtype=int) for i in range(self.num_groundtruth_classes): (gt_boxes_at_ith_class, gt_masks_at_ith_class, detected_boxes_at_ith_class, detected_scores_at_ith_class, detected_masks_at_ith_class) = self._get_ith_class_arrays( detected_boxes, detected_scores, detected_masks, detected_class_labels, groundtruth_boxes, groundtruth_masks, groundtruth_class_labels, i) is_class_correctly_detected_in_image[i] = ( self._compute_is_class_correctly_detected_in_image( detected_boxes=detected_boxes_at_ith_class, detected_scores=detected_scores_at_ith_class, groundtruth_boxes=gt_boxes_at_ith_class, detected_masks=detected_masks_at_ith_class, groundtruth_masks=gt_masks_at_ith_class)) return is_class_correctly_detected_in_image def _compute_is_class_correctly_detected_in_image( self, detected_boxes, detected_scores, groundtruth_boxes, detected_masks=None, groundtruth_masks=None): """Compute CorLoc score for a single class. Args: detected_boxes: A numpy array of shape [N, 4] representing detected box coordinates detected_scores: A 1-d numpy array of length N representing classification score groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth box coordinates detected_masks: (optional) A np.uint8 numpy array of shape [N, height, width]. If not None, the scores will be computed based on masks. groundtruth_masks: (optional) A np.uint8 numpy array of shape [M, height, width]. Returns: is_class_correctly_detected_in_image: An integer 1 or 0 denoting whether a class is correctly detected in the image or not """ if detected_boxes.size > 0: if groundtruth_boxes.size > 0: max_score_id = np.argmax(detected_scores) mask_mode = False if detected_masks is not None and groundtruth_masks is not None: mask_mode = True if mask_mode: detected_boxlist = np_box_mask_list.BoxMaskList( box_data=np.expand_dims(detected_boxes[max_score_id], axis=0), mask_data=np.expand_dims(detected_masks[max_score_id], axis=0)) gt_boxlist = np_box_mask_list.BoxMaskList( box_data=groundtruth_boxes, mask_data=groundtruth_masks) iou = np_box_mask_list_ops.iou(detected_boxlist, gt_boxlist) else: detected_boxlist = np_box_list.BoxList( np.expand_dims(detected_boxes[max_score_id, :], axis=0)) gt_boxlist = np_box_list.BoxList(groundtruth_boxes) iou = np_box_list_ops.iou(detected_boxlist, gt_boxlist) if np.max(iou) >= self.matching_iou_threshold: return 1 return 0 def _compute_tp_fp(self, detected_boxes, detected_scores, detected_class_labels, groundtruth_boxes, groundtruth_class_labels, groundtruth_is_difficult_list, groundtruth_is_group_of_list, detected_masks=None, groundtruth_masks=None): """Labels true/false positives of detections of an image across all classes. Args: detected_boxes: A float numpy array of shape [N, 4], representing N regions of detected object regions. Each row is of the format [y_min, x_min, y_max, x_max] detected_scores: A float numpy array of shape [N, 1], representing the confidence scores of the detected N object instances. detected_class_labels: A integer numpy array of shape [N, 1], repreneting the class labels of the detected N object instances. groundtruth_boxes: A float numpy array of shape [M, 4], representing M regions of object instances in ground truth groundtruth_class_labels: An integer numpy array of shape [M, 1], representing M class labels of object instances in ground truth groundtruth_is_difficult_list: A boolean numpy array of length M denoting whether a ground truth box is a difficult instance or not groundtruth_is_group_of_list: A boolean numpy array of length M denoting whether a ground truth box has group-of tag detected_masks: (optional) A np.uint8 numpy array of shape [N, height, width]. If not None, the scores will be computed based on masks. groundtruth_masks: (optional) A np.uint8 numpy array of shape [M, height, width]. Returns: result_scores: A list of float numpy arrays. Each numpy array is of shape [K, 1], representing K scores detected with object class label c result_tp_fp_labels: A list of boolean numpy array. Each numpy array is of shape [K, 1], representing K True/False positive label of object instances detected with class label c Raises: ValueError: If detected masks is not None but groundtruth masks are None, or the other way around. """ if detected_masks is not None and groundtruth_masks is None: raise ValueError( 'Detected masks is available but groundtruth masks is not.') if detected_masks is None and groundtruth_masks is not None: raise ValueError( 'Groundtruth masks is available but detected masks is not.') result_scores = [] result_tp_fp_labels = [] for i in range(self.num_groundtruth_classes): groundtruth_is_difficult_list_at_ith_class = ( groundtruth_is_difficult_list[groundtruth_class_labels == i]) groundtruth_is_group_of_list_at_ith_class = ( groundtruth_is_group_of_list[groundtruth_class_labels == i]) (gt_boxes_at_ith_class, gt_masks_at_ith_class, detected_boxes_at_ith_class, detected_scores_at_ith_class, detected_masks_at_ith_class) = self._get_ith_class_arrays( detected_boxes, detected_scores, detected_masks, detected_class_labels, groundtruth_boxes, groundtruth_masks, groundtruth_class_labels, i) scores, tp_fp_labels = self._compute_tp_fp_for_single_class( detected_boxes=detected_boxes_at_ith_class, detected_scores=detected_scores_at_ith_class, groundtruth_boxes=gt_boxes_at_ith_class, groundtruth_is_difficult_list= groundtruth_is_difficult_list_at_ith_class, groundtruth_is_group_of_list= groundtruth_is_group_of_list_at_ith_class, detected_masks=detected_masks_at_ith_class, groundtruth_masks=gt_masks_at_ith_class) result_scores.append(scores) result_tp_fp_labels.append(tp_fp_labels) return result_scores, result_tp_fp_labels def _get_overlaps_and_scores_mask_mode( self, detected_boxes, detected_scores, detected_masks, groundtruth_boxes, groundtruth_masks, groundtruth_is_group_of_list): """Computes overlaps and scores between detected and groudntruth masks. Args: detected_boxes: A numpy array of shape [N, 4] representing detected box coordinates detected_scores: A 1-d numpy array of length N representing classification score detected_masks: A uint8 numpy array of shape [N, height, width]. If not None, the scores will be computed based on masks. groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth box coordinates groundtruth_masks: A uint8 numpy array of shape [M, height, width]. groundtruth_is_group_of_list: A boolean numpy array of length M denoting whether a ground truth box has group-of tag. If a groundtruth box is group-of box, every detection matching this box is ignored. Returns: iou: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If gt_non_group_of_boxlist.num_boxes() == 0 it will be None. ioa: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If gt_group_of_boxlist.num_boxes() == 0 it will be None. scores: The score of the detected boxlist. num_boxes: Number of non-maximum suppressed detected boxes. """ detected_boxlist = np_box_mask_list.BoxMaskList( box_data=detected_boxes, mask_data=detected_masks) detected_boxlist.add_field('scores', detected_scores) detected_boxlist = np_box_mask_list_ops.non_max_suppression( detected_boxlist, self.nms_max_output_boxes, self.nms_iou_threshold) gt_non_group_of_boxlist = np_box_mask_list.BoxMaskList( box_data=groundtruth_boxes[~groundtruth_is_group_of_list], mask_data=groundtruth_masks[~groundtruth_is_group_of_list]) gt_group_of_boxlist = np_box_mask_list.BoxMaskList( box_data=groundtruth_boxes[groundtruth_is_group_of_list], mask_data=groundtruth_masks[groundtruth_is_group_of_list]) iou = np_box_mask_list_ops.iou(detected_boxlist, gt_non_group_of_boxlist) ioa = np_box_mask_list_ops.ioa(gt_group_of_boxlist, detected_boxlist) scores = detected_boxlist.get_field('scores') num_boxes = detected_boxlist.num_boxes() return iou, ioa, scores, num_boxes def _get_overlaps_and_scores_box_mode( self, detected_boxes, detected_scores, groundtruth_boxes, groundtruth_is_group_of_list): """Computes overlaps and scores between detected and groudntruth boxes. Args: detected_boxes: A numpy array of shape [N, 4] representing detected box coordinates detected_scores: A 1-d numpy array of length N representing classification score groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth box coordinates groundtruth_is_group_of_list: A boolean numpy array of length M denoting whether a ground truth box has group-of tag. If a groundtruth box is group-of box, every detection matching this box is ignored. Returns: iou: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If gt_non_group_of_boxlist.num_boxes() == 0 it will be None. ioa: A float numpy array of size [num_detected_boxes, num_gt_boxes]. If gt_group_of_boxlist.num_boxes() == 0 it will be None. scores: The score of the detected boxlist. num_boxes: Number of non-maximum suppressed detected boxes. """ detected_boxlist = np_box_list.BoxList(detected_boxes) detected_boxlist.add_field('scores', detected_scores) detected_boxlist = np_box_list_ops.non_max_suppression( detected_boxlist, self.nms_max_output_boxes, self.nms_iou_threshold) gt_non_group_of_boxlist = np_box_list.BoxList( groundtruth_boxes[~groundtruth_is_group_of_list]) gt_group_of_boxlist = np_box_list.BoxList( groundtruth_boxes[groundtruth_is_group_of_list]) iou = np_box_list_ops.iou(detected_boxlist, gt_non_group_of_boxlist) ioa = np_box_list_ops.ioa(gt_group_of_boxlist, detected_boxlist) scores = detected_boxlist.get_field('scores') num_boxes = detected_boxlist.num_boxes() return iou, ioa, scores, num_boxes def _compute_tp_fp_for_single_class( self, detected_boxes, detected_scores, groundtruth_boxes, groundtruth_is_difficult_list, groundtruth_is_group_of_list, detected_masks=None, groundtruth_masks=None): """Labels boxes detected with the same class from the same image as tp/fp. Args: detected_boxes: A numpy array of shape [N, 4] representing detected box coordinates detected_scores: A 1-d numpy array of length N representing classification score groundtruth_boxes: A numpy array of shape [M, 4] representing ground truth box coordinates groundtruth_is_difficult_list: A boolean numpy array of length M denoting whether a ground truth box is a difficult instance or not. If a groundtruth box is difficult, every detection matching this box is ignored. groundtruth_is_group_of_list: A boolean numpy array of length M denoting whether a ground truth box has group-of tag. If a groundtruth box is group-of box, every detection matching this box is ignored. detected_masks: (optional) A uint8 numpy array of shape [N, height, width]. If not None, the scores will be computed based on masks. groundtruth_masks: (optional) A uint8 numpy array of shape [M, height, width]. Returns: Two arrays of the same size, containing all boxes that were evaluated as being true positives or false positives; if a box matched to a difficult box or to a group-of box, it is ignored. scores: A numpy array representing the detection scores. tp_fp_labels: a boolean numpy array indicating whether a detection is a true positive. """ if detected_boxes.size == 0: return np.array([], dtype=float), np.array([], dtype=bool) mask_mode = False if detected_masks is not None and groundtruth_masks is not None: mask_mode = True if mask_mode: (iou, ioa, scores, num_detected_boxes) = self._get_overlaps_and_scores_mask_mode( detected_boxes=detected_boxes, detected_scores=detected_scores, detected_masks=detected_masks, groundtruth_boxes=groundtruth_boxes, groundtruth_masks=groundtruth_masks, groundtruth_is_group_of_list=groundtruth_is_group_of_list) else: (iou, ioa, scores, num_detected_boxes) = self._get_overlaps_and_scores_box_mode( detected_boxes=detected_boxes, detected_scores=detected_scores, groundtruth_boxes=groundtruth_boxes, groundtruth_is_group_of_list=groundtruth_is_group_of_list) if groundtruth_boxes.size == 0: return scores, np.zeros(num_detected_boxes, dtype=bool) tp_fp_labels = np.zeros(num_detected_boxes, dtype=bool) is_matched_to_difficult_box = np.zeros(num_detected_boxes, dtype=bool) is_matched_to_group_of_box = np.zeros(num_detected_boxes, dtype=bool) # The evaluation is done in two stages: # 1. All detections are matched to non group-of boxes; true positives are # determined and detections matched to difficult boxes are ignored. # 2. Detections that are determined as false positives are matched against # group-of boxes and ignored if matched. # Tp-fp evaluation for non-group of boxes (if any). if iou.shape[1] > 0: groundtruth_nongroup_of_is_difficult_list = groundtruth_is_difficult_list[ ~groundtruth_is_group_of_list] max_overlap_gt_ids = np.argmax(iou, axis=1) is_gt_box_detected = np.zeros(iou.shape[1], dtype=bool) for i in range(num_detected_boxes): gt_id = max_overlap_gt_ids[i] if iou[i, gt_id] >= self.matching_iou_threshold: if not groundtruth_nongroup_of_is_difficult_list[gt_id]: if not is_gt_box_detected[gt_id]: tp_fp_labels[i] = True is_gt_box_detected[gt_id] = True else: is_matched_to_difficult_box[i] = True # Tp-fp evaluation for group of boxes. if ioa.shape[0] > 0: max_overlap_group_of_gt = np.max(ioa, axis=0) for i in range(num_detected_boxes): if (not tp_fp_labels[i] and not is_matched_to_difficult_box[i] and max_overlap_group_of_gt[i] >= self.matching_iou_threshold): is_matched_to_group_of_box[i] = True return scores[~is_matched_to_difficult_box & ~is_matched_to_group_of_box], tp_fp_labels[ ~is_matched_to_difficult_box & ~is_matched_to_group_of_box] def _get_ith_class_arrays(self, detected_boxes, detected_scores, detected_masks, detected_class_labels, groundtruth_boxes, groundtruth_masks, groundtruth_class_labels, class_index): """Returns numpy arrays belonging to class with index `class_index`. Args: detected_boxes: A numpy array containing detected boxes. detected_scores: A numpy array containing detected scores. detected_masks: A numpy array containing detected masks. detected_class_labels: A numpy array containing detected class labels. groundtruth_boxes: A numpy array containing groundtruth boxes. groundtruth_masks: A numpy array containing groundtruth masks. groundtruth_class_labels: A numpy array containing groundtruth class labels. class_index: An integer index. Returns: gt_boxes_at_ith_class: A numpy array containing groundtruth boxes labeled as ith class. gt_masks_at_ith_class: A numpy array containing groundtruth masks labeled as ith class. detected_boxes_at_ith_class: A numpy array containing detected boxes corresponding to the ith class. detected_scores_at_ith_class: A numpy array containing detected scores corresponding to the ith class. detected_masks_at_ith_class: A numpy array containing detected masks corresponding to the ith class. """ selected_groundtruth = (groundtruth_class_labels == class_index) gt_boxes_at_ith_class = groundtruth_boxes[selected_groundtruth] if groundtruth_masks is not None: gt_masks_at_ith_class = groundtruth_masks[selected_groundtruth] else: gt_masks_at_ith_class = None selected_detections = (detected_class_labels == class_index) detected_boxes_at_ith_class = detected_boxes[selected_detections] detected_scores_at_ith_class = detected_scores[selected_detections] if detected_masks is not None: detected_masks_at_ith_class = detected_masks[selected_detections] else: detected_masks_at_ith_class = None return (gt_boxes_at_ith_class, gt_masks_at_ith_class, detected_boxes_at_ith_class, detected_scores_at_ith_class, detected_masks_at_ith_class) def _remove_invalid_boxes(self, detected_boxes, detected_scores, detected_class_labels, detected_masks=None): """Removes entries with invalid boxes. A box is invalid if either its xmax is smaller than its xmin, or its ymax is smaller than its ymin. Args: detected_boxes: A float numpy array of size [num_boxes, 4] containing box coordinates in [ymin, xmin, ymax, xmax] format. detected_scores: A float numpy array of size [num_boxes]. detected_class_labels: A int32 numpy array of size [num_boxes]. detected_masks: A uint8 numpy array of size [num_boxes, height, width]. Returns: valid_detected_boxes: A float numpy array of size [num_valid_boxes, 4] containing box coordinates in [ymin, xmin, ymax, xmax] format. valid_detected_scores: A float numpy array of size [num_valid_boxes]. valid_detected_class_labels: A int32 numpy array of size [num_valid_boxes]. valid_detected_masks: A uint8 numpy array of size [num_valid_boxes, height, width]. """ valid_indices = np.logical_and(detected_boxes[:, 0] < detected_boxes[:, 2], detected_boxes[:, 1] < detected_boxes[:, 3]) detected_boxes = detected_boxes[valid_indices] detected_scores = detected_scores[valid_indices] detected_class_labels = detected_class_labels[valid_indices] if detected_masks is not None: detected_masks = detected_masks[valid_indices] return [ detected_boxes, detected_scores, detected_class_labels, detected_masks ]
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/per_image_evaluation.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Operations for [N, height, width] numpy arrays representing masks. Example mask operations that are supported: * Areas: compute mask areas * IOU: pairwise intersection-over-union scores """ import numpy as np EPSILON = 1e-7 def area(masks): """Computes area of masks. Args: masks: Numpy array with shape [N, height, width] holding N masks. Masks values are of type np.uint8 and values are in {0,1}. Returns: a numpy array with shape [N*1] representing mask areas. Raises: ValueError: If masks.dtype is not np.uint8 """ if masks.dtype != np.uint8: raise ValueError('Masks type should be np.uint8') return np.sum(masks, axis=(1, 2), dtype=np.float32) def intersection(masks1, masks2): """Compute pairwise intersection areas between masks. Args: masks1: a numpy array with shape [N, height, width] holding N masks. Masks values are of type np.uint8 and values are in {0,1}. masks2: a numpy array with shape [M, height, width] holding M masks. Masks values are of type np.uint8 and values are in {0,1}. Returns: a numpy array with shape [N*M] representing pairwise intersection area. Raises: ValueError: If masks1 and masks2 are not of type np.uint8. """ if masks1.dtype != np.uint8 or masks2.dtype != np.uint8: raise ValueError('masks1 and masks2 should be of type np.uint8') n = masks1.shape[0] m = masks2.shape[0] answer = np.zeros([n, m], dtype=np.float32) for i in np.arange(n): for j in np.arange(m): answer[i, j] = np.sum(np.minimum(masks1[i], masks2[j]), dtype=np.float32) return answer def iou(masks1, masks2): """Computes pairwise intersection-over-union between mask collections. Args: masks1: a numpy array with shape [N, height, width] holding N masks. Masks values are of type np.uint8 and values are in {0,1}. masks2: a numpy array with shape [M, height, width] holding N masks. Masks values are of type np.uint8 and values are in {0,1}. Returns: a numpy array with shape [N, M] representing pairwise iou scores. Raises: ValueError: If masks1 and masks2 are not of type np.uint8. """ if masks1.dtype != np.uint8 or masks2.dtype != np.uint8: raise ValueError('masks1 and masks2 should be of type np.uint8') intersect = intersection(masks1, masks2) area1 = area(masks1) area2 = area(masks2) union = np.expand_dims(area1, axis=1) + np.expand_dims( area2, axis=0) - intersect return intersect / np.maximum(union, EPSILON) def ioa(masks1, masks2): """Computes pairwise intersection-over-area between box collections. Intersection-over-area (ioa) between two masks, mask1 and mask2 is defined as their intersection area over mask2's area. Note that ioa is not symmetric, that is, IOA(mask1, mask2) != IOA(mask2, mask1). Args: masks1: a numpy array with shape [N, height, width] holding N masks. Masks values are of type np.uint8 and values are in {0,1}. masks2: a numpy array with shape [M, height, width] holding N masks. Masks values are of type np.uint8 and values are in {0,1}. Returns: a numpy array with shape [N, M] representing pairwise ioa scores. Raises: ValueError: If masks1 and masks2 are not of type np.uint8. """ if masks1.dtype != np.uint8 or masks2.dtype != np.uint8: raise ValueError('masks1 and masks2 should be of type np.uint8') intersect = intersection(masks1, masks2) areas = np.expand_dims(area(masks2), axis=0) return intersect / (areas + EPSILON)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/np_mask_ops.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Operations for [N, 4] numpy arrays representing bounding boxes. Example box operations that are supported: * Areas: compute bounding box areas * IOU: pairwise intersection-over-union scores """ import numpy as np def area(boxes): """Computes area of boxes. Args: boxes: Numpy array with shape [N, 4] holding N boxes Returns: a numpy array with shape [N*1] representing box areas """ return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) def intersection(boxes1, boxes2): """Compute pairwise intersection areas between boxes. Args: boxes1: a numpy array with shape [N, 4] holding N boxes boxes2: a numpy array with shape [M, 4] holding M boxes Returns: a numpy array with shape [N*M] representing pairwise intersection area """ [y_min1, x_min1, y_max1, x_max1] = np.split(boxes1, 4, axis=1) [y_min2, x_min2, y_max2, x_max2] = np.split(boxes2, 4, axis=1) all_pairs_min_ymax = np.minimum(y_max1, np.transpose(y_max2)) all_pairs_max_ymin = np.maximum(y_min1, np.transpose(y_min2)) intersect_heights = np.maximum( np.zeros(all_pairs_max_ymin.shape), all_pairs_min_ymax - all_pairs_max_ymin) all_pairs_min_xmax = np.minimum(x_max1, np.transpose(x_max2)) all_pairs_max_xmin = np.maximum(x_min1, np.transpose(x_min2)) intersect_widths = np.maximum( np.zeros(all_pairs_max_xmin.shape), all_pairs_min_xmax - all_pairs_max_xmin) return intersect_heights * intersect_widths def iou(boxes1, boxes2): """Computes pairwise intersection-over-union between box collections. Args: boxes1: a numpy array with shape [N, 4] holding N boxes. boxes2: a numpy array with shape [M, 4] holding N boxes. Returns: a numpy array with shape [N, M] representing pairwise iou scores. """ intersect = intersection(boxes1, boxes2) area1 = area(boxes1) area2 = area(boxes2) union = np.expand_dims(area1, axis=1) + np.expand_dims( area2, axis=0) - intersect return intersect / union def ioa(boxes1, boxes2): """Computes pairwise intersection-over-area between box collections. Intersection-over-area (ioa) between two boxes box1 and box2 is defined as their intersection area over box2's area. Note that ioa is not symmetric, that is, IOA(box1, box2) != IOA(box2, box1). Args: boxes1: a numpy array with shape [N, 4] holding N boxes. boxes2: a numpy array with shape [M, 4] holding N boxes. Returns: a numpy array with shape [N, M] representing pairwise ioa scores. """ intersect = intersection(boxes1, boxes2) areas = np.expand_dims(area(boxes2), axis=0) return intersect / areas
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/np_box_ops.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Label map utility functions.""" import logging logger = logging.getLogger("alphaction.inference") # from google.protobuf import text_format # from google3.third_party.tensorflow_models.object_detection.protos import string_int_label_map_pb2 def _validate_label_map(label_map): """Checks if a label map is valid. Args: label_map: StringIntLabelMap to validate. Raises: ValueError: if label map is invalid. """ for item in label_map.item: if item.id < 1: raise ValueError('Label map ids should be >= 1.') def create_category_index(categories): """Creates dictionary of COCO compatible categories keyed by category id. Args: categories: a list of dicts, each of which has the following keys: 'id': (required) an integer id uniquely identifying this category. 'name': (required) string representing category name e.g., 'cat', 'dog', 'pizza'. Returns: category_index: a dict containing the same entries as categories, but keyed by the 'id' field of each category. """ category_index = {} for cat in categories: category_index[cat['id']] = cat return category_index def get_max_label_map_index(label_map): """Get maximum index in label map. Args: label_map: a StringIntLabelMapProto Returns: an integer """ return max([item.id for item in label_map.item]) def convert_label_map_to_categories(label_map, max_num_classes, use_display_name=True): """Loads label map proto and returns categories list compatible with eval. This function loads a label map and returns a list of dicts, each of which has the following keys: 'id': (required) an integer id uniquely identifying this category. 'name': (required) string representing category name e.g., 'cat', 'dog', 'pizza'. We only allow class into the list if its id-label_id_offset is between 0 (inclusive) and max_num_classes (exclusive). If there are several items mapping to the same id in the label map, we will only keep the first one in the categories list. Args: label_map: a StringIntLabelMapProto or None. If None, a default categories list is created with max_num_classes categories. max_num_classes: maximum number of (consecutive) label indices to include. use_display_name: (boolean) choose whether to load 'display_name' field as category name. If False or if the display_name field does not exist, uses 'name' field as category names instead. Returns: categories: a list of dictionaries representing all possible categories. """ categories = [] list_of_ids_already_added = [] if not label_map: label_id_offset = 1 for class_id in range(max_num_classes): categories.append({ 'id': class_id + label_id_offset, 'name': 'category_{}'.format(class_id + label_id_offset) }) return categories for item in label_map.item: if not 0 < item.id <= max_num_classes: logger.info('Ignore item %d since it falls outside of requested ' 'label range.', item.id) continue if use_display_name and item.HasField('display_name'): name = item.display_name else: name = item.name if item.id not in list_of_ids_already_added: list_of_ids_already_added.append(item.id) categories.append({'id': item.id, 'name': name}) return categories def load_labelmap(path): """Loads label map proto. Args: path: path to StringIntLabelMap proto text file. Returns: a StringIntLabelMapProto """ with open(path, 'r') as fid: label_map_string = fid.read() label_map = string_int_label_map_pb2.StringIntLabelMap() try: text_format.Merge(label_map_string, label_map) except text_format.ParseError: label_map.ParseFromString(label_map_string) _validate_label_map(label_map) return label_map def get_label_map_dict(label_map_path, use_display_name=False): """Reads a label map and returns a dictionary of label names to id. Args: label_map_path: path to label_map. use_display_name: whether to use the label map items' display names as keys. Returns: A dictionary mapping label names to id. """ label_map = load_labelmap(label_map_path) label_map_dict = {} for item in label_map.item: if use_display_name: label_map_dict[item.display_name] = item.id else: label_map_dict[item.name] = item.id return label_map_dict def create_category_index_from_labelmap(label_map_path): """Reads a label map and returns a category index. Args: label_map_path: Path to `StringIntLabelMap` proto text file. Returns: A category index, which is a dictionary that maps integer ids to dicts containing categories, e.g. {1: {'id': 1, 'name': 'dog'}, 2: {'id': 2, 'name': 'cat'}, ...} """ label_map = load_labelmap(label_map_path) max_num_classes = max(item.id for item in label_map.item) categories = convert_label_map_to_categories(label_map, max_num_classes) return create_category_index(categories) def create_class_agnostic_category_index(): """Creates a category index with a single `object` class.""" return {1: {'id': 1, 'name': 'object'}}
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/label_map_util.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Operations for np_box_mask_list.BoxMaskList. Example box operations that are supported: * Areas: compute bounding box areas * IOU: pairwise intersection-over-union scores """ import numpy as np from . import np_box_list_ops, np_mask_ops, np_box_mask_list def box_list_to_box_mask_list(boxlist): """Converts a BoxList containing 'masks' into a BoxMaskList. Args: boxlist: An np_box_list.BoxList object. Returns: An np_box_mask_list.BoxMaskList object. Raises: ValueError: If boxlist does not contain `masks` as a field. """ if not boxlist.has_field('masks'): raise ValueError('boxlist does not contain mask field.') box_mask_list = np_box_mask_list.BoxMaskList( box_data=boxlist.get(), mask_data=boxlist.get_field('masks')) extra_fields = boxlist.get_extra_fields() for key in extra_fields: if key != 'masks': box_mask_list.data[key] = boxlist.get_field(key) return box_mask_list def area(box_mask_list): """Computes area of masks. Args: box_mask_list: np_box_mask_list.BoxMaskList holding N boxes and masks Returns: a numpy array with shape [N*1] representing mask areas """ return np_mask_ops.area(box_mask_list.get_masks()) def intersection(box_mask_list1, box_mask_list2): """Compute pairwise intersection areas between masks. Args: box_mask_list1: BoxMaskList holding N boxes and masks box_mask_list2: BoxMaskList holding M boxes and masks Returns: a numpy array with shape [N*M] representing pairwise intersection area """ return np_mask_ops.intersection(box_mask_list1.get_masks(), box_mask_list2.get_masks()) def iou(box_mask_list1, box_mask_list2): """Computes pairwise intersection-over-union between box and mask collections. Args: box_mask_list1: BoxMaskList holding N boxes and masks box_mask_list2: BoxMaskList holding M boxes and masks Returns: a numpy array with shape [N, M] representing pairwise iou scores. """ return np_mask_ops.iou(box_mask_list1.get_masks(), box_mask_list2.get_masks()) def ioa(box_mask_list1, box_mask_list2): """Computes pairwise intersection-over-area between box and mask collections. Intersection-over-area (ioa) between two masks mask1 and mask2 is defined as their intersection area over mask2's area. Note that ioa is not symmetric, that is, IOA(mask1, mask2) != IOA(mask2, mask1). Args: box_mask_list1: np_box_mask_list.BoxMaskList holding N boxes and masks box_mask_list2: np_box_mask_list.BoxMaskList holding M boxes and masks Returns: a numpy array with shape [N, M] representing pairwise ioa scores. """ return np_mask_ops.ioa(box_mask_list1.get_masks(), box_mask_list2.get_masks()) def gather(box_mask_list, indices, fields=None): """Gather boxes from np_box_mask_list.BoxMaskList according to indices. By default, gather returns boxes corresponding to the input index list, as well as all additional fields stored in the box_mask_list (indexing into the first dimension). However one can optionally only gather from a subset of fields. Args: box_mask_list: np_box_mask_list.BoxMaskList holding N boxes indices: a 1-d numpy array of type int_ fields: (optional) list of fields to also gather from. If None (default), all fields are gathered from. Pass an empty fields list to only gather the box coordinates. Returns: subbox_mask_list: a np_box_mask_list.BoxMaskList corresponding to the subset of the input box_mask_list specified by indices Raises: ValueError: if specified field is not contained in box_mask_list or if the indices are not of type int_ """ if fields is not None: if 'masks' not in fields: fields.append('masks') return box_list_to_box_mask_list( np_box_list_ops.gather( boxlist=box_mask_list, indices=indices, fields=fields)) def sort_by_field(box_mask_list, field, order=np_box_list_ops.SortOrder.DESCEND): """Sort boxes and associated fields according to a scalar field. A common use case is reordering the boxes according to descending scores. Args: box_mask_list: BoxMaskList holding N boxes. field: A BoxMaskList field for sorting and reordering the BoxMaskList. order: (Optional) 'descend' or 'ascend'. Default is descend. Returns: sorted_box_mask_list: A sorted BoxMaskList with the field in the specified order. """ return box_list_to_box_mask_list( np_box_list_ops.sort_by_field( boxlist=box_mask_list, field=field, order=order)) def non_max_suppression(box_mask_list, max_output_size=10000, iou_threshold=1.0, score_threshold=-10.0): """Non maximum suppression. This op greedily selects a subset of detection bounding boxes, pruning away boxes that have high IOU (intersection over union) overlap (> thresh) with already selected boxes. In each iteration, the detected bounding box with highest score in the available pool is selected. Args: box_mask_list: np_box_mask_list.BoxMaskList holding N boxes. Must contain a 'scores' field representing detection scores. All scores belong to the same class. max_output_size: maximum number of retained boxes iou_threshold: intersection over union threshold. score_threshold: minimum score threshold. Remove the boxes with scores less than this value. Default value is set to -10. A very low threshold to pass pretty much all the boxes, unless the user sets a different score threshold. Returns: an np_box_mask_list.BoxMaskList holding M boxes where M <= max_output_size Raises: ValueError: if 'scores' field does not exist ValueError: if threshold is not in [0, 1] ValueError: if max_output_size < 0 """ if not box_mask_list.has_field('scores'): raise ValueError('Field scores does not exist') if iou_threshold < 0. or iou_threshold > 1.0: raise ValueError('IOU threshold must be in [0, 1]') if max_output_size < 0: raise ValueError('max_output_size must be bigger than 0.') box_mask_list = filter_scores_greater_than(box_mask_list, score_threshold) if box_mask_list.num_boxes() == 0: return box_mask_list box_mask_list = sort_by_field(box_mask_list, 'scores') # Prevent further computation if NMS is disabled. if iou_threshold == 1.0: if box_mask_list.num_boxes() > max_output_size: selected_indices = np.arange(max_output_size) return gather(box_mask_list, selected_indices) else: return box_mask_list masks = box_mask_list.get_masks() num_masks = box_mask_list.num_boxes() # is_index_valid is True only for all remaining valid boxes, is_index_valid = np.full(num_masks, 1, dtype=bool) selected_indices = [] num_output = 0 for i in range(num_masks): if num_output < max_output_size: if is_index_valid[i]: num_output += 1 selected_indices.append(i) is_index_valid[i] = False valid_indices = np.where(is_index_valid)[0] if valid_indices.size == 0: break intersect_over_union = np_mask_ops.iou( np.expand_dims(masks[i], axis=0), masks[valid_indices]) intersect_over_union = np.squeeze(intersect_over_union, axis=0) is_index_valid[valid_indices] = np.logical_and( is_index_valid[valid_indices], intersect_over_union <= iou_threshold) return gather(box_mask_list, np.array(selected_indices)) def multi_class_non_max_suppression(box_mask_list, score_thresh, iou_thresh, max_output_size): """Multi-class version of non maximum suppression. This op greedily selects a subset of detection bounding boxes, pruning away boxes that have high IOU (intersection over union) overlap (> thresh) with already selected boxes. It operates independently for each class for which scores are provided (via the scores field of the input box_list), pruning boxes with score less than a provided threshold prior to applying NMS. Args: box_mask_list: np_box_mask_list.BoxMaskList holding N boxes. Must contain a 'scores' field representing detection scores. This scores field is a tensor that can be 1 dimensional (in the case of a single class) or 2-dimensional, in which case we assume that it takes the shape [num_boxes, num_classes]. We further assume that this rank is known statically and that scores.shape[1] is also known (i.e., the number of classes is fixed and known at graph construction time). score_thresh: scalar threshold for score (low scoring boxes are removed). iou_thresh: scalar threshold for IOU (boxes that that high IOU overlap with previously selected boxes are removed). max_output_size: maximum number of retained boxes per class. Returns: a box_mask_list holding M boxes with a rank-1 scores field representing corresponding scores for each box with scores sorted in decreasing order and a rank-1 classes field representing a class label for each box. Raises: ValueError: if iou_thresh is not in [0, 1] or if input box_mask_list does not have a valid scores field. """ if not 0 <= iou_thresh <= 1.0: raise ValueError('thresh must be between 0 and 1') if not isinstance(box_mask_list, np_box_mask_list.BoxMaskList): raise ValueError('box_mask_list must be a box_mask_list') if not box_mask_list.has_field('scores'): raise ValueError('input box_mask_list must have \'scores\' field') scores = box_mask_list.get_field('scores') if len(scores.shape) == 1: scores = np.reshape(scores, [-1, 1]) elif len(scores.shape) == 2: if scores.shape[1] is None: raise ValueError('scores field must have statically defined second ' 'dimension') else: raise ValueError('scores field must be of rank 1 or 2') num_boxes = box_mask_list.num_boxes() num_scores = scores.shape[0] num_classes = scores.shape[1] if num_boxes != num_scores: raise ValueError('Incorrect scores field length: actual vs expected.') selected_boxes_list = [] for class_idx in range(num_classes): box_mask_list_and_class_scores = np_box_mask_list.BoxMaskList( box_data=box_mask_list.get(), mask_data=box_mask_list.get_masks()) class_scores = np.reshape(scores[0:num_scores, class_idx], [-1]) box_mask_list_and_class_scores.add_field('scores', class_scores) box_mask_list_filt = filter_scores_greater_than( box_mask_list_and_class_scores, score_thresh) nms_result = non_max_suppression( box_mask_list_filt, max_output_size=max_output_size, iou_threshold=iou_thresh, score_threshold=score_thresh) nms_result.add_field( 'classes', np.zeros_like(nms_result.get_field('scores')) + class_idx) selected_boxes_list.append(nms_result) selected_boxes = np_box_list_ops.concatenate(selected_boxes_list) sorted_boxes = np_box_list_ops.sort_by_field(selected_boxes, 'scores') return box_list_to_box_mask_list(boxlist=sorted_boxes) def prune_non_overlapping_masks(box_mask_list1, box_mask_list2, minoverlap=0.0): """Prunes the boxes in list1 that overlap less than thresh with list2. For each mask in box_mask_list1, we want its IOA to be more than minoverlap with at least one of the masks in box_mask_list2. If it does not, we remove it. If the masks are not full size image, we do the pruning based on boxes. Args: box_mask_list1: np_box_mask_list.BoxMaskList holding N boxes and masks. box_mask_list2: np_box_mask_list.BoxMaskList holding M boxes and masks. minoverlap: Minimum required overlap between boxes, to count them as overlapping. Returns: A pruned box_mask_list with size [N', 4]. """ intersection_over_area = ioa(box_mask_list2, box_mask_list1) # [M, N] tensor intersection_over_area = np.amax(intersection_over_area, axis=0) # [N] tensor keep_bool = np.greater_equal(intersection_over_area, np.array(minoverlap)) keep_inds = np.nonzero(keep_bool)[0] new_box_mask_list1 = gather(box_mask_list1, keep_inds) return new_box_mask_list1 def concatenate(box_mask_lists, fields=None): """Concatenate list of box_mask_lists. This op concatenates a list of input box_mask_lists into a larger box_mask_list. It also handles concatenation of box_mask_list fields as long as the field tensor shapes are equal except for the first dimension. Args: box_mask_lists: list of np_box_mask_list.BoxMaskList objects fields: optional list of fields to also concatenate. By default, all fields from the first BoxMaskList in the list are included in the concatenation. Returns: a box_mask_list with number of boxes equal to sum([box_mask_list.num_boxes() for box_mask_list in box_mask_list]) Raises: ValueError: if box_mask_lists is invalid (i.e., is not a list, is empty, or contains non box_mask_list objects), or if requested fields are not contained in all box_mask_lists """ if fields is not None: if 'masks' not in fields: fields.append('masks') return box_list_to_box_mask_list( np_box_list_ops.concatenate(boxlists=box_mask_lists, fields=fields)) def filter_scores_greater_than(box_mask_list, thresh): """Filter to keep only boxes and masks with score exceeding a given threshold. This op keeps the collection of boxes and masks whose corresponding scores are greater than the input threshold. Args: box_mask_list: BoxMaskList holding N boxes and masks. Must contain a 'scores' field representing detection scores. thresh: scalar threshold Returns: a BoxMaskList holding M boxes and masks where M <= N Raises: ValueError: if box_mask_list not a np_box_mask_list.BoxMaskList object or if it does not have a scores field """ if not isinstance(box_mask_list, np_box_mask_list.BoxMaskList): raise ValueError('box_mask_list must be a BoxMaskList') if not box_mask_list.has_field('scores'): raise ValueError('input box_mask_list must have \'scores\' field') scores = box_mask_list.get_field('scores') if len(scores.shape) > 2: raise ValueError('Scores should have rank 1 or 2') if len(scores.shape) == 2 and scores.shape[1] != 1: raise ValueError('Scores should have rank 1 or have shape ' 'consistent with [None, 1]') high_score_indices = np.reshape(np.where(np.greater(scores, thresh)), [-1]).astype(np.int32) return gather(box_mask_list, high_score_indices)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/np_box_mask_list_ops.py
# Copyright 2017 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """object_detection_evaluation module. ObjectDetectionEvaluation is a class which manages ground truth information of a object detection dataset, and computes frequently used detection metrics such as Precision, Recall, CorLoc of the provided detection results. It supports the following operations: 1) Add ground truth information of images sequentially. 2) Add detection result of images sequentially. 3) Evaluate detection metrics on already inserted detection results. 4) Write evaluation result into a pickle file for future processing or visualization. Note: This module operates on numpy boxes and box lists. """ from abc import ABCMeta from abc import abstractmethod import collections import logging import numpy as np from . import metrics, standard_fields, label_map_util, per_image_evaluation logger = logging.getLogger("alphaction.inference") class DetectionEvaluator(object): """Interface for object detection evalution classes. Example usage of the Evaluator: ------------------------------ evaluator = DetectionEvaluator(categories) # Detections and groundtruth for image 1. evaluator.add_single_groundtruth_image_info(...) evaluator.add_single_detected_image_info(...) # Detections and groundtruth for image 2. evaluator.add_single_groundtruth_image_info(...) evaluator.add_single_detected_image_info(...) metrics_dict = evaluator.evaluate() """ __metaclass__ = ABCMeta def __init__(self, categories): """Constructor. Args: categories: A list of dicts, each of which has the following keys - 'id': (required) an integer id uniquely identifying this category. 'name': (required) string representing category name e.g., 'cat', 'dog'. """ self._categories = categories @abstractmethod def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): """Adds groundtruth for a single image to be used for evaluation. Args: image_id: A unique string/integer identifier for the image. groundtruth_dict: A dictionary of groundtruth numpy arrays required for evaluations. """ pass @abstractmethod def add_single_detected_image_info(self, image_id, detections_dict): """Adds detections for a single image to be used for evaluation. Args: image_id: A unique string/integer identifier for the image. detections_dict: A dictionary of detection numpy arrays required for evaluation. """ pass @abstractmethod def evaluate(self): """Evaluates detections and returns a dictionary of metrics.""" pass @abstractmethod def clear(self): """Clears the state to prepare for a fresh evaluation.""" pass class ObjectDetectionEvaluator(DetectionEvaluator): """A class to evaluate detections.""" def __init__(self, categories, matching_iou_threshold=0.5, evaluate_corlocs=False, metric_prefix=None, use_weighted_mean_ap=False, evaluate_masks=False): """Constructor. Args: categories: A list of dicts, each of which has the following keys - 'id': (required) an integer id uniquely identifying this category. 'name': (required) string representing category name e.g., 'cat', 'dog'. matching_iou_threshold: IOU threshold to use for matching groundtruth boxes to detection boxes. evaluate_corlocs: (optional) boolean which determines if corloc scores are to be returned or not. metric_prefix: (optional) string prefix for metric name; if None, no prefix is used. use_weighted_mean_ap: (optional) boolean which determines if the mean average precision is computed directly from the scores and tp_fp_labels of all classes. evaluate_masks: If False, evaluation will be performed based on boxes. If True, mask evaluation will be performed instead. Raises: ValueError: If the category ids are not 1-indexed. """ super(ObjectDetectionEvaluator, self).__init__(categories) self._num_classes = max([cat['id'] for cat in categories]) if min(cat['id'] for cat in categories) < 1: raise ValueError('Classes should be 1-indexed.') self._matching_iou_threshold = matching_iou_threshold self._use_weighted_mean_ap = use_weighted_mean_ap self._label_id_offset = 1 self._evaluate_masks = evaluate_masks self._evaluation = ObjectDetectionEvaluation( num_groundtruth_classes=self._num_classes, matching_iou_threshold=self._matching_iou_threshold, use_weighted_mean_ap=self._use_weighted_mean_ap, label_id_offset=self._label_id_offset) self._image_ids = set([]) self._evaluate_corlocs = evaluate_corlocs self._metric_prefix = (metric_prefix + '_') if metric_prefix else '' def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): """Adds groundtruth for a single image to be used for evaluation. Args: image_id: A unique string/integer identifier for the image. groundtruth_dict: A dictionary containing - standard_fields.InputDataFields.groundtruth_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. standard_fields.InputDataFields.groundtruth_classes: integer numpy array of shape [num_boxes] containing 1-indexed groundtruth classes for the boxes. standard_fields.InputDataFields.groundtruth_difficult: Optional length M numpy boolean array denoting whether a ground truth box is a difficult instance or not. This field is optional to support the case that no boxes are difficult. standard_fields.InputDataFields.groundtruth_instance_masks: Optional numpy array of shape [num_boxes, height, width] with values in {0, 1}. Raises: ValueError: On adding groundtruth for an image more than once. Will also raise error if instance masks are not in groundtruth dictionary. """ if image_id in self._image_ids: raise ValueError('Image with id {} already added.'.format(image_id)) groundtruth_classes = ( groundtruth_dict[standard_fields.InputDataFields.groundtruth_classes] - self._label_id_offset) # If the key is not present in the groundtruth_dict or the array is empty # (unless there are no annotations for the groundtruth on this image) # use values from the dictionary or insert None otherwise. if (standard_fields.InputDataFields.groundtruth_difficult in groundtruth_dict.keys() and (groundtruth_dict[standard_fields.InputDataFields.groundtruth_difficult] .size or not groundtruth_classes.size)): groundtruth_difficult = groundtruth_dict[ standard_fields.InputDataFields.groundtruth_difficult] else: groundtruth_difficult = None if not len(self._image_ids) % 1000: logger.warning( 'image %s does not have groundtruth difficult flag specified', image_id) groundtruth_masks = None if self._evaluate_masks: if (standard_fields.InputDataFields.groundtruth_instance_masks not in groundtruth_dict): raise ValueError('Instance masks not in groundtruth dictionary.') groundtruth_masks = groundtruth_dict[ standard_fields.InputDataFields.groundtruth_instance_masks] self._evaluation.add_single_ground_truth_image_info( image_key=image_id, groundtruth_boxes=groundtruth_dict[ standard_fields.InputDataFields.groundtruth_boxes], groundtruth_class_labels=groundtruth_classes, groundtruth_is_difficult_list=groundtruth_difficult, groundtruth_masks=groundtruth_masks) self._image_ids.update([image_id]) def add_single_detected_image_info(self, image_id, detections_dict): """Adds detections for a single image to be used for evaluation. Args: image_id: A unique string/integer identifier for the image. detections_dict: A dictionary containing - standard_fields.DetectionResultFields.detection_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` detection boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. standard_fields.DetectionResultFields.detection_scores: float32 numpy array of shape [num_boxes] containing detection scores for the boxes. standard_fields.DetectionResultFields.detection_classes: integer numpy array of shape [num_boxes] containing 1-indexed detection classes for the boxes. standard_fields.DetectionResultFields.detection_masks: uint8 numpy array of shape [num_boxes, height, width] containing `num_boxes` masks of values ranging between 0 and 1. Raises: ValueError: If detection masks are not in detections dictionary. """ detection_classes = ( detections_dict[standard_fields.DetectionResultFields.detection_classes] - self._label_id_offset) detection_masks = None if self._evaluate_masks: if (standard_fields.DetectionResultFields.detection_masks not in detections_dict): raise ValueError('Detection masks not in detections dictionary.') detection_masks = detections_dict[ standard_fields.DetectionResultFields.detection_masks] self._evaluation.add_single_detected_image_info( image_key=image_id, detected_boxes=detections_dict[ standard_fields.DetectionResultFields.detection_boxes], detected_scores=detections_dict[ standard_fields.DetectionResultFields.detection_scores], detected_class_labels=detection_classes, detected_masks=detection_masks) def evaluate(self): """Compute evaluation result. Returns: A dictionary of metrics with the following fields - 1. summary_metrics: 'Precision/mAP@<matching_iou_threshold>IOU': mean average precision at the specified IOU threshold. 2. per_category_ap: category specific results with keys of the form 'PerformanceByCategory/mAP@<matching_iou_threshold>IOU/category'. """ (per_class_ap, mean_ap, _, _, per_class_corloc, mean_corloc) = ( self._evaluation.evaluate()) pascal_metrics = { self._metric_prefix + 'Precision/mAP@{}IOU'.format(self._matching_iou_threshold): mean_ap } if self._evaluate_corlocs: pascal_metrics[self._metric_prefix + 'Precision/meanCorLoc@{}IOU'.format( self._matching_iou_threshold)] = mean_corloc category_index = label_map_util.create_category_index(self._categories) for idx in range(per_class_ap.size): if idx + self._label_id_offset in category_index: display_name = ( self._metric_prefix + 'PerformanceByCategory/AP@{}IOU/{}'.format( self._matching_iou_threshold, category_index[idx + self._label_id_offset]['name'])) pascal_metrics[display_name] = per_class_ap[idx] # Optionally add CorLoc metrics.classes if self._evaluate_corlocs: display_name = ( self._metric_prefix + 'PerformanceByCategory/CorLoc@{}IOU/{}' .format(self._matching_iou_threshold, category_index[idx + self._label_id_offset]['name'])) pascal_metrics[display_name] = per_class_corloc[idx] return pascal_metrics def clear(self): """Clears the state to prepare for a fresh evaluation.""" self._evaluation = ObjectDetectionEvaluation( num_groundtruth_classes=self._num_classes, matching_iou_threshold=self._matching_iou_threshold, use_weighted_mean_ap=self._use_weighted_mean_ap, label_id_offset=self._label_id_offset) self._image_ids.clear() class PascalDetectionEvaluator(ObjectDetectionEvaluator): """A class to evaluate detections using PASCAL metrics.""" def __init__(self, categories, matching_iou_threshold=0.5): super(PascalDetectionEvaluator, self).__init__( categories, matching_iou_threshold=matching_iou_threshold, evaluate_corlocs=False, metric_prefix='PascalBoxes', use_weighted_mean_ap=False) class WeightedPascalDetectionEvaluator(ObjectDetectionEvaluator): """A class to evaluate detections using weighted PASCAL metrics. Weighted PASCAL metrics computes the mean average precision as the average precision given the scores and tp_fp_labels of all classes. In comparison, PASCAL metrics computes the mean average precision as the mean of the per-class average precisions. This definition is very similar to the mean of the per-class average precisions weighted by class frequency. However, they are typically not the same as the average precision is not a linear function of the scores and tp_fp_labels. """ def __init__(self, categories, matching_iou_threshold=0.5): super(WeightedPascalDetectionEvaluator, self).__init__( categories, matching_iou_threshold=matching_iou_threshold, evaluate_corlocs=False, metric_prefix='WeightedPascalBoxes', use_weighted_mean_ap=True) class PascalInstanceSegmentationEvaluator(ObjectDetectionEvaluator): """A class to evaluate instance masks using PASCAL metrics.""" def __init__(self, categories, matching_iou_threshold=0.5): super(PascalInstanceSegmentationEvaluator, self).__init__( categories, matching_iou_threshold=matching_iou_threshold, evaluate_corlocs=False, metric_prefix='PascalMasks', use_weighted_mean_ap=False, evaluate_masks=True) class WeightedPascalInstanceSegmentationEvaluator(ObjectDetectionEvaluator): """A class to evaluate instance masks using weighted PASCAL metrics. Weighted PASCAL metrics computes the mean average precision as the average precision given the scores and tp_fp_labels of all classes. In comparison, PASCAL metrics computes the mean average precision as the mean of the per-class average precisions. This definition is very similar to the mean of the per-class average precisions weighted by class frequency. However, they are typically not the same as the average precision is not a linear function of the scores and tp_fp_labels. """ def __init__(self, categories, matching_iou_threshold=0.5): super(WeightedPascalInstanceSegmentationEvaluator, self).__init__( categories, matching_iou_threshold=matching_iou_threshold, evaluate_corlocs=False, metric_prefix='WeightedPascalMasks', use_weighted_mean_ap=True, evaluate_masks=True) class OpenImagesDetectionEvaluator(ObjectDetectionEvaluator): """A class to evaluate detections using Open Images V2 metrics. Open Images V2 introduce group_of type of bounding boxes and this metric handles those boxes appropriately. """ def __init__(self, categories, matching_iou_threshold=0.5, evaluate_corlocs=False): """Constructor. Args: categories: A list of dicts, each of which has the following keys - 'id': (required) an integer id uniquely identifying this category. 'name': (required) string representing category name e.g., 'cat', 'dog'. matching_iou_threshold: IOU threshold to use for matching groundtruth boxes to detection boxes. evaluate_corlocs: if True, additionally evaluates and returns CorLoc. """ super(OpenImagesDetectionEvaluator, self).__init__( categories, matching_iou_threshold, evaluate_corlocs, metric_prefix='OpenImagesV2') def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): """Adds groundtruth for a single image to be used for evaluation. Args: image_id: A unique string/integer identifier for the image. groundtruth_dict: A dictionary containing - standard_fields.InputDataFields.groundtruth_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. standard_fields.InputDataFields.groundtruth_classes: integer numpy array of shape [num_boxes] containing 1-indexed groundtruth classes for the boxes. standard_fields.InputDataFields.groundtruth_group_of: Optional length M numpy boolean array denoting whether a groundtruth box contains a group of instances. Raises: ValueError: On adding groundtruth for an image more than once. """ if image_id in self._image_ids: raise ValueError('Image with id {} already added.'.format(image_id)) groundtruth_classes = ( groundtruth_dict[standard_fields.InputDataFields.groundtruth_classes] - self._label_id_offset) # If the key is not present in the groundtruth_dict or the array is empty # (unless there are no annotations for the groundtruth on this image) # use values from the dictionary or insert None otherwise. if (standard_fields.InputDataFields.groundtruth_group_of in groundtruth_dict.keys() and (groundtruth_dict[standard_fields.InputDataFields.groundtruth_group_of] .size or not groundtruth_classes.size)): groundtruth_group_of = groundtruth_dict[ standard_fields.InputDataFields.groundtruth_group_of] else: groundtruth_group_of = None if not len(self._image_ids) % 1000: logger.warning( 'image %s does not have groundtruth group_of flag specified', image_id) self._evaluation.add_single_ground_truth_image_info( image_id, groundtruth_dict[standard_fields.InputDataFields.groundtruth_boxes], groundtruth_classes, groundtruth_is_difficult_list=None, groundtruth_is_group_of_list=groundtruth_group_of) self._image_ids.update([image_id]) ObjectDetectionEvalMetrics = collections.namedtuple( 'ObjectDetectionEvalMetrics', [ 'average_precisions', 'mean_ap', 'precisions', 'recalls', 'corlocs', 'mean_corloc' ]) class ObjectDetectionEvaluation(object): """Internal implementation of Pascal object detection metrics.""" def __init__(self, num_groundtruth_classes, matching_iou_threshold=0.5, nms_iou_threshold=1.0, nms_max_output_boxes=10000, use_weighted_mean_ap=False, label_id_offset=0): if num_groundtruth_classes < 1: raise ValueError('Need at least 1 groundtruth class for evaluation.') self.per_image_eval = per_image_evaluation.PerImageEvaluation( num_groundtruth_classes=num_groundtruth_classes, matching_iou_threshold=matching_iou_threshold, nms_iou_threshold=nms_iou_threshold, nms_max_output_boxes=nms_max_output_boxes) self.num_class = num_groundtruth_classes self.use_weighted_mean_ap = use_weighted_mean_ap self.label_id_offset = label_id_offset self.groundtruth_boxes = {} self.groundtruth_class_labels = {} self.groundtruth_masks = {} self.groundtruth_is_difficult_list = {} self.groundtruth_is_group_of_list = {} self.num_gt_instances_per_class = np.zeros(self.num_class, dtype=int) self.num_gt_imgs_per_class = np.zeros(self.num_class, dtype=int) self._initialize_detections() def _initialize_detections(self): self.detection_keys = set() self.scores_per_class = [[] for _ in range(self.num_class)] self.tp_fp_labels_per_class = [[] for _ in range(self.num_class)] self.num_images_correctly_detected_per_class = np.zeros(self.num_class) self.average_precision_per_class = np.empty(self.num_class, dtype=float) self.average_precision_per_class.fill(np.nan) self.precisions_per_class = [] self.recalls_per_class = [] self.corloc_per_class = np.ones(self.num_class, dtype=float) def clear_detections(self): self._initialize_detections() def add_single_ground_truth_image_info(self, image_key, groundtruth_boxes, groundtruth_class_labels, groundtruth_is_difficult_list=None, groundtruth_is_group_of_list=None, groundtruth_masks=None): """Adds groundtruth for a single image to be used for evaluation. Args: image_key: A unique string/integer identifier for the image. groundtruth_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. groundtruth_class_labels: integer numpy array of shape [num_boxes] containing 0-indexed groundtruth classes for the boxes. groundtruth_is_difficult_list: A length M numpy boolean array denoting whether a ground truth box is a difficult instance or not. To support the case that no boxes are difficult, it is by default set as None. groundtruth_is_group_of_list: A length M numpy boolean array denoting whether a ground truth box is a group-of box or not. To support the case that no boxes are groups-of, it is by default set as None. groundtruth_masks: uint8 numpy array of shape [num_boxes, height, width] containing `num_boxes` groundtruth masks. The mask values range from 0 to 1. """ if image_key in self.groundtruth_boxes: logger.warning( 'image %s has already been added to the ground truth database.', image_key) return self.groundtruth_boxes[image_key] = groundtruth_boxes self.groundtruth_class_labels[image_key] = groundtruth_class_labels self.groundtruth_masks[image_key] = groundtruth_masks if groundtruth_is_difficult_list is None: num_boxes = groundtruth_boxes.shape[0] groundtruth_is_difficult_list = np.zeros(num_boxes, dtype=bool) self.groundtruth_is_difficult_list[ image_key] = groundtruth_is_difficult_list.astype(dtype=bool) if groundtruth_is_group_of_list is None: num_boxes = groundtruth_boxes.shape[0] groundtruth_is_group_of_list = np.zeros(num_boxes, dtype=bool) self.groundtruth_is_group_of_list[ image_key] = groundtruth_is_group_of_list.astype(dtype=bool) self._update_ground_truth_statistics( groundtruth_class_labels, groundtruth_is_difficult_list.astype(dtype=bool), groundtruth_is_group_of_list.astype(dtype=bool)) def add_single_detected_image_info(self, image_key, detected_boxes, detected_scores, detected_class_labels, detected_masks=None): """Adds detections for a single image to be used for evaluation. Args: image_key: A unique string/integer identifier for the image. detected_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` detection boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. detected_scores: float32 numpy array of shape [num_boxes] containing detection scores for the boxes. detected_class_labels: integer numpy array of shape [num_boxes] containing 0-indexed detection classes for the boxes. detected_masks: np.uint8 numpy array of shape [num_boxes, height, width] containing `num_boxes` detection masks with values ranging between 0 and 1. Raises: ValueError: if the number of boxes, scores and class labels differ in length. """ if (len(detected_boxes) != len(detected_scores) or len(detected_boxes) != len(detected_class_labels)): raise ValueError('detected_boxes, detected_scores and ' 'detected_class_labels should all have same lengths. Got' '[%d, %d, %d]' % len(detected_boxes), len(detected_scores), len(detected_class_labels)) if image_key in self.detection_keys: logger.warning( 'image %s has already been added to the detection result database', image_key) return self.detection_keys.add(image_key) if image_key in self.groundtruth_boxes: groundtruth_boxes = self.groundtruth_boxes[image_key] groundtruth_class_labels = self.groundtruth_class_labels[image_key] # Masks are popped instead of look up. The reason is that we do not want # to keep all masks in memory which can cause memory overflow. groundtruth_masks = self.groundtruth_masks.pop(image_key) groundtruth_is_difficult_list = self.groundtruth_is_difficult_list[ image_key] groundtruth_is_group_of_list = self.groundtruth_is_group_of_list[ image_key] else: groundtruth_boxes = np.empty(shape=[0, 4], dtype=float) groundtruth_class_labels = np.array([], dtype=int) if detected_masks is None: groundtruth_masks = None else: groundtruth_masks = np.empty(shape=[0, 1, 1], dtype=float) groundtruth_is_difficult_list = np.array([], dtype=bool) groundtruth_is_group_of_list = np.array([], dtype=bool) scores, tp_fp_labels, is_class_correctly_detected_in_image = ( self.per_image_eval.compute_object_detection_metrics( detected_boxes=detected_boxes, detected_scores=detected_scores, detected_class_labels=detected_class_labels, groundtruth_boxes=groundtruth_boxes, groundtruth_class_labels=groundtruth_class_labels, groundtruth_is_difficult_list=groundtruth_is_difficult_list, groundtruth_is_group_of_list=groundtruth_is_group_of_list, detected_masks=detected_masks, groundtruth_masks=groundtruth_masks)) for i in range(self.num_class): if scores[i].shape[0] > 0: self.scores_per_class[i].append(scores[i]) self.tp_fp_labels_per_class[i].append(tp_fp_labels[i]) self.num_images_correctly_detected_per_class += is_class_correctly_detected_in_image def _update_ground_truth_statistics(self, groundtruth_class_labels, groundtruth_is_difficult_list, groundtruth_is_group_of_list): """Update grouth truth statitistics. 1. Difficult boxes are ignored when counting the number of ground truth instances as done in Pascal VOC devkit. 2. Difficult boxes are treated as normal boxes when computing CorLoc related statitistics. Args: groundtruth_class_labels: An integer numpy array of length M, representing M class labels of object instances in ground truth groundtruth_is_difficult_list: A boolean numpy array of length M denoting whether a ground truth box is a difficult instance or not groundtruth_is_group_of_list: A boolean numpy array of length M denoting whether a ground truth box is a group-of box or not """ for class_index in range(self.num_class): num_gt_instances = np.sum(groundtruth_class_labels[ ~groundtruth_is_difficult_list & ~groundtruth_is_group_of_list] == class_index) self.num_gt_instances_per_class[class_index] += num_gt_instances if np.any(groundtruth_class_labels == class_index): self.num_gt_imgs_per_class[class_index] += 1 def evaluate(self): """Compute evaluation result. Returns: A named tuple with the following fields - average_precision: float numpy array of average precision for each class. mean_ap: mean average precision of all classes, float scalar precisions: List of precisions, each precision is a float numpy array recalls: List of recalls, each recall is a float numpy array corloc: numpy float array mean_corloc: Mean CorLoc score for each class, float scalar """ if (self.num_gt_instances_per_class == 0).any(): logger.info( 'The following classes have no ground truth examples: %s', np.squeeze(np.argwhere(self.num_gt_instances_per_class == 0)) + self.label_id_offset) if self.use_weighted_mean_ap: all_scores = np.array([], dtype=float) all_tp_fp_labels = np.array([], dtype=bool) for class_index in range(self.num_class): if self.num_gt_instances_per_class[class_index] == 0: continue if not self.scores_per_class[class_index]: scores = np.array([], dtype=float) tp_fp_labels = np.array([], dtype=bool) else: scores = np.concatenate(self.scores_per_class[class_index]) tp_fp_labels = np.concatenate(self.tp_fp_labels_per_class[class_index]) if self.use_weighted_mean_ap: all_scores = np.append(all_scores, scores) all_tp_fp_labels = np.append(all_tp_fp_labels, tp_fp_labels) precision, recall = metrics.compute_precision_recall( scores, tp_fp_labels, self.num_gt_instances_per_class[class_index]) self.precisions_per_class.append(precision) self.recalls_per_class.append(recall) average_precision = metrics.compute_average_precision(precision, recall) self.average_precision_per_class[class_index] = average_precision self.corloc_per_class = metrics.compute_cor_loc( self.num_gt_imgs_per_class, self.num_images_correctly_detected_per_class) if self.use_weighted_mean_ap: num_gt_instances = np.sum(self.num_gt_instances_per_class) precision, recall = metrics.compute_precision_recall( all_scores, all_tp_fp_labels, num_gt_instances) mean_ap = metrics.compute_average_precision(precision, recall) else: mean_ap = np.nanmean(self.average_precision_per_class) mean_corloc = np.nanmean(self.corloc_per_class) return ObjectDetectionEvalMetrics( self.average_precision_per_class, mean_ap, self.precisions_per_class, self.recalls_per_class, self.corloc_per_class, mean_corloc)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/datasets/evaluation/ava/pascal_evaluation/object_detection_evaluation.py
from . import video_transforms as T from . import object_transforms as OT def build_transforms(cfg, is_train=True): # build transforms for training of testing if is_train: min_size = cfg.INPUT.MIN_SIZE_TRAIN max_size = cfg.INPUT.MAX_SIZE_TRAIN color_jitter = cfg.INPUT.COLOR_JITTER flip_prob = 0.5 slow_jitter = cfg.INPUT.SLOW_JITTER else: min_size = cfg.INPUT.MIN_SIZE_TEST max_size = cfg.INPUT.MAX_SIZE_TEST color_jitter = False flip_prob = 0 slow_jitter = False frame_num = cfg.INPUT.FRAME_NUM sample_rate = cfg.INPUT.FRAME_SAMPLE_RATE if color_jitter: color_transform = T.ColorJitter( cfg.INPUT.HUE_JITTER, cfg.INPUT.SAT_JITTER, cfg.INPUT.VAL_JITTER ) else: color_transform = T.Identity() to_bgr = cfg.INPUT.TO_BGR normalize_transform = T.Normalize( mean=cfg.INPUT.PIXEL_MEAN, std=cfg.INPUT.PIXEL_STD, to_bgr=to_bgr ) tau = cfg.INPUT.TAU alpha = cfg.INPUT.ALPHA transform = T.Compose( [ T.TemporalCrop(frame_num, sample_rate), T.Resize(min_size, max_size), color_transform, T.RandomHorizontalFlip(flip_prob), T.ToTensor(), normalize_transform, T.SlowFastCrop(tau, alpha, slow_jitter), ] ) return transform def build_object_transforms(cfg, is_train=True): # build transforms for object boxes, should be kept consistent with video transforms. if is_train: flip_prob = 0.5 else: flip_prob = 0 transform = OT.Compose([ OT.PickTop(cfg.MODEL.IA_STRUCTURE.MAX_OBJECT), OT.Resize(), OT.RandomHorizontalFlip(flip_prob) ]) return transform
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/transforms/build.py
from .build import build_transforms, build_object_transforms
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/transforms/__init__.py
import torch import random import numpy as np import cv2 cv2.setNumThreads(0) class Compose(object): # Compose different kinds of video transforms into one. def __init__(self, transforms): self.transforms = transforms def __call__(self, videos, target): transform_randoms = {} for t in self.transforms: videos, target, transform_randoms = t((videos, target, transform_randoms)) return videos, target, transform_randoms def __repr__(self): format_string = self.__class__.__name__ + "(" for t in self.transforms: format_string += "\n" format_string += " {0}".format(t) format_string += "\n)" return format_string class TemporalCrop(object): def __init__(self, frame_num, sample_rate, temporal_jitter=0): self.frame_num = frame_num self.sample_rate = sample_rate self.temporal_jitter = temporal_jitter def __call__(self, results): clip, target, transform_randoms = results # crop the input frames from raw clip raw_frame_num = clip.shape[0] # determine frame start based on frame_num, sample_rate and the jitter shift. frame_start = (raw_frame_num - self.frame_num * self.sample_rate) // 2 + ( self.sample_rate - 1) // 2 + self.temporal_jitter idx = np.arange(frame_start, frame_start + self.frame_num * self.sample_rate, self.sample_rate) idx = np.clip(idx, 0, raw_frame_num - 1) clip = clip[idx] return clip, target, transform_randoms class Resize(object): def __init__(self, min_size, max_size): self.min_size = min_size self.max_size = max_size def get_size(self, image_size): # Calculate output size according to min_size, max_size. h, w = image_size size = self.min_size # # max_size###### # max_size = self.max_size # if max_size is not None: # min_original_size = float(min((w, h))) # max_original_size = float(max((w, h))) # if max_original_size / min_original_size * size > max_size: # size = int(round(max_size * min_original_size / max_original_size)) # # max_size###### if (w <= h and w == size) or (h <= w and h == size): return (h, w) if w < h: ow = size oh = int(size * h / w) else: oh = size ow = int(size * w / h) return (ow, oh) def __call__(self, results): # Input clip should be [TxHxWxC](uint8) ndarray. clip, target, transform_randoms = results if isinstance(clip, list): clip = np.array(clip) size = self.get_size(clip.shape[1:3]) clip_new = np.zeros((clip.shape[0], size[1], size[0], clip.shape[3]), dtype=np.uint8) for i in range(clip.shape[0]): cv2.resize(clip[i], size, clip_new[i]) if target is not None: target = target.resize(size) # Store the size for object box transforms. transform_randoms["Resize"] = size return clip_new, target, transform_randoms class RandomClip(object): def __init__(self, is_train): self.is_train = is_train self.size = 224 # (w, h) def __call__(self, results): # Input clip should be [TxHxWxC](uint8) ndarray. # size = self.get_size(clip.shape[1:3]) clip, target, transform_randoms = results if self.is_train: size = self.size clip_new = np.zeros((clip.shape[0], size, size, clip.shape[3]), dtype=np.uint8) image_height, image_width = clip.shape[1], clip.shape[2] self.tl_x = random.random() self.tl_y = random.random() x1 = int(self.tl_x * (image_width - size)) y1 = int(self.tl_y * (image_height - size)) x2 = x1 + size y2 = y1 + size for i in range(clip.shape[0]): # cv2.resize(clip[i], size, clip_new[i]) assert clip_new[i].shape == clip[i, y1:y2, x1:x2, :].shape, \ print('x1={}, y1={}, x2={}, y2={}, ori_size={}'.format(x1, y1, x2, y2, clip.shape)) clip_new[i] = clip[i, y1:y2, x1:x2, :] if target is not None: # target = target.resize(size) target = target.crop([x1, y1, x2, y2]) else: clip_new = clip # Store the size for object box transforms. # transform_randoms["Resize"] = size return clip_new, target, transform_randoms class ColorJitter(object): def __init__(self, hue_shift, sat_shift, val_shift): # color jitter in hsv space. H: 0~360, circular S: 0.0~1.0 V: 0.0~1.0 self.hue_bound = int(round(hue_shift / 2)) self.sat_bound = int(round(sat_shift * 255)) self.val_bound = int(round(val_shift * 255)) def __call__(self, results): # Convert: RGB->HSV clip, target, transform_randoms = results clip_hsv = np.zeros_like(clip) for i in range(clip.shape[0]): cv2.cvtColor(clip[i], cv2.COLOR_RGB2HSV, clip_hsv[i]) clip_hsv = clip_hsv.astype(np.int32) # Jittering. hue_s = random.randint(-self.hue_bound, self.hue_bound) clip_hsv[..., 0] = (clip_hsv[..., 0] + hue_s + 180) % 180 sat_s = random.randint(-self.sat_bound, self.sat_bound) clip_hsv[..., 1] = np.clip(clip_hsv[..., 1] + sat_s, 0, 255) val_s = random.randint(-self.val_bound, self.val_bound) clip_hsv[..., 2] = np.clip(clip_hsv[..., 2] + val_s, 0, 255) clip_hsv = clip_hsv.astype(np.uint8) # Convert: HSV->RGB clip = np.zeros_like(clip) for i in range(clip.shape[0]): cv2.cvtColor(clip_hsv[i], cv2.COLOR_HSV2RGB, clip[i]) return clip, target, transform_randoms class RandomHorizontalFlip(object): def __init__(self, prob=0.5): self.prob = prob def __call__(self, results): # Input clip should be [TxHxWxC] ndarray.(uint8) clip, target, transform_randoms = results flip_random = random.random() if flip_random < self.prob: clip = np.flip(clip, 2) if target is not None: target = target.transpose(0) # Store the random variable for object box transforms transform_randoms["Flip"] = flip_random return clip, target, transform_randoms class ToTensor(object): def __call__(self, results): # Input clip should be [TxHxWxC] ndarray. # Convert to [CxTxHxW] tensor. clip, target, transform_randoms = results return torch.from_numpy(clip.transpose((3, 0, 1, 2)).astype(np.float32)), target, transform_randoms class Normalize(object): def __init__(self, mean, std, to_bgr=False): self.mean = mean self.std = std self.to_bgr = to_bgr def video_normalize(self, tensor, mean, std): # Copied from torchvision.transforms.functional.normalize but remove the type check of tensor. for t, m, s in zip(tensor, mean, std): t.sub_(m).div_(s) return tensor def __call__(self, results): clip, target, transform_randoms = results if self.to_bgr: clip = clip[[2, 1, 0]] # normalize: (x-mean)/std clip = self.video_normalize(clip, self.mean, self.std) return clip, target, transform_randoms class SlowFastCrop(object): # Class used to split frames for slow pathway and fast pathway. def __init__(self, tau, alpha, slow_jitter=False): self.tau = tau self.alpha = alpha self.slow_jitter = slow_jitter def __call__(self, results): clip, target, transform_randoms = results if self.slow_jitter: # if jitter, random choose a start slow_start = random.randint(0, self.tau - 1) else: # if no jitter, select the middle slow_start = (self.tau - 1) // 2 slow_clip = clip[:, slow_start::self.tau, :, :] fast_stride = self.tau // self.alpha fast_start = (fast_stride - 1) // 2 fast_clip = clip[:, fast_start::fast_stride, :, :] return [slow_clip, fast_clip], target, transform_randoms class Identity(object): # Return what is received. Do nothing. def __init__(self): pass def __call__(self, results): return results
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/transforms/video_transforms.py
class Compose(object): # Class used to compose different kinds of object transforms def __init__(self, transforms): self.transforms = transforms def __call__(self, object_boxes, transform_randoms): #should reuse the random varaible in video transforms for t in self.transforms: object_boxes = t(object_boxes, transform_randoms) return object_boxes class PickTop(object): # pick top scored object boxes. def __init__(self, top_k): self.top_k = top_k def __call__(self, objects, _): objects = objects.top_k(self.top_k) return objects class Resize(object): def __call__(self, object_boxes, transform_randoms): # resize according to video transforms size = transform_randoms["Resize"] if object_boxes is not None: object_boxes = object_boxes.resize(size) return object_boxes class RandomHorizontalFlip(object): def __init__(self, prob=0.5): self.prob = prob def __call__(self, object_boxes, transform_randoms): # flip according to video transforms flip_random = transform_randoms["Flip"] if flip_random < self.prob: object_boxes.transpose(0) return object_boxes
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/transforms/object_transforms.py
from .distributed import DistributedSampler from .grouped_batch_sampler import GroupedBatchSampler from .iteration_based_batch_sampler import IterationBasedBatchSampler __all__ = ["DistributedSampler", "GroupedBatchSampler", "IterationBasedBatchSampler"]
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/samplers/__init__.py
# Modified based on https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/data/samplers/grouped_batch_sampler.py import itertools import torch from torch.utils.data.sampler import BatchSampler from torch.utils.data.sampler import Sampler class GroupedBatchSampler(BatchSampler): """ Wraps another sampler to yield a mini-batch of indices. It enforces that elements from the same group should appear in groups of batch_size. It also tries to provide mini-batches which follows an ordering which is as close as possible to the ordering from the original sampler. Arguments: sampler (Sampler): Base sampler. batch_size (int): Size of mini-batch. drop_uneven (bool): If ``True``, the sampler will drop the batches whose size is less than ``batch_size`` """ def __init__(self, sampler, group_ids, batch_size, drop_uneven=False): if not isinstance(sampler, Sampler): raise ValueError( "sampler should be an instance of " "torch.utils.dataset.Sampler, but got sampler={}".format(sampler) ) self.sampler = sampler self.group_ids = torch.as_tensor(group_ids) assert self.group_ids.dim() == 1 self.batch_size = batch_size self.drop_uneven = drop_uneven self.groups = torch.unique(self.group_ids).sort(0)[0] def _prepare_batches(self): dataset_size = len(self.group_ids) # get the sampled indices from the sampler sampled_ids = torch.as_tensor(list(self.sampler)) # potentially not all elements of the dataset were sampled # by the sampler (e.g., DistributedSampler). # construct a tensor which contains -1 if the element was # not sampled, and a non-negative number indicating the # order where the element was sampled. # for example. if sampled_ids = [3, 1] and dataset_size = 5, # the order is [-1, 1, -1, 0, -1] order = torch.full((dataset_size,), -1, dtype=torch.int64) order[sampled_ids] = torch.arange(len(sampled_ids)) # get a mask with the elements that were sampled mask = order >= 0 # find the elements that belong to each individual cluster clusters = [(self.group_ids == i) & mask for i in self.groups] # get relative order of the elements inside each cluster # that follows the order from the sampler relative_order = [order[cluster] for cluster in clusters] # with the relative order, find the absolute order in the # sampled space permutation_ids = [s[s.sort()[1]] for s in relative_order] # permute each cluster so that they follow the order from # the sampler permuted_clusters = [sampled_ids[idx] for idx in permutation_ids] # splits each cluster in batch_size, and merge as a list of tensors splits = [c.split(self.batch_size) for c in permuted_clusters] merged = tuple(itertools.chain.from_iterable(splits)) # now each batch internally has the right order, but # they are grouped by clusters. Find the permutation between # different batches that brings them as close as possible to # the order that we have in the sampler. For that, we will consider the # ordering as coming from the first element of each batch, and sort # correspondingly first_element_of_batch = [t[0].item() for t in merged] # get and inverse mapping from sampled indices and the position where # they occur (as returned by the sampler) inv_sampled_ids_map = {v: k for k, v in enumerate(sampled_ids.tolist())} # from the first element in each batch, get a relative ordering first_index_of_batch = torch.as_tensor( [inv_sampled_ids_map[s] for s in first_element_of_batch] ) # permute the batches so that they approximately follow the order # from the sampler permutation_order = first_index_of_batch.sort(0)[1].tolist() # finally, permute the batches batches = [merged[i].tolist() for i in permutation_order] if self.drop_uneven: kept = [] for batch in batches: if len(batch) == self.batch_size: kept.append(batch) batches = kept return batches def __iter__(self): batches = self._prepare_batches() self._batches = batches return iter(batches) def __len__(self): if not hasattr(self, "_batches"): self._batches = self._prepare_batches() return len(self._batches)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/samplers/grouped_batch_sampler.py
# Code is copy-pasted exactly as in torch.utils.dataset.distributed. # FIXME remove this once c10d fixes the bug it has import math import torch import torch.distributed as dist from torch.utils.data.sampler import Sampler class DistributedSampler(Sampler): """Sampler that restricts dataset loading to a subset of the dataset. It is especially useful in conjunction with :class:`torch.nn.parallel.DistributedDataParallel`. In such case, each process can pass a DistributedSampler instance as a DataLoader sampler, and load a subset of the original dataset that is exclusive to it. .. note:: Dataset is assumed to be of constant size. Arguments: dataset: Dataset used for sampling. num_replicas (optional): Number of processes participating in distributed training. rank (optional): Rank of the current process within num_replicas. """ def __init__(self, dataset, num_replicas=None, rank=None, shuffle=True): if num_replicas is None: if not dist.is_available(): raise RuntimeError("Requires distributed package to be available") num_replicas = dist.get_world_size() if rank is None: if not dist.is_available(): raise RuntimeError("Requires distributed package to be available") rank = dist.get_rank() self.dataset = dataset self.num_replicas = num_replicas self.rank = rank self.epoch = 0 self.num_samples = int(math.ceil(len(self.dataset) * 1.0 / self.num_replicas)) self.total_size = self.num_samples * self.num_replicas self.shuffle = shuffle def __iter__(self): if self.shuffle: # deterministically shuffle based on epoch g = torch.Generator() g.manual_seed(self.epoch) indices = torch.randperm(len(self.dataset), generator=g).tolist() else: indices = torch.arange(len(self.dataset)).tolist() # add extra samples to make it evenly divisible indices += indices[: (self.total_size - len(indices))] assert len(indices) == self.total_size # subsample offset = self.num_samples * self.rank indices = indices[offset : offset + self.num_samples] assert len(indices) == self.num_samples return iter(indices) def __len__(self): return self.num_samples def set_epoch(self, epoch): self.epoch = epoch
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/samplers/distributed.py
# Adopted from https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/data/samplers/iteration_based_batch_sampler.py from torch.utils.data.sampler import BatchSampler class IterationBasedBatchSampler(BatchSampler): """ Wraps a BatchSampler, resampling from it until a specified number of iterations have been sampled """ def __init__(self, batch_sampler, num_iterations, start_iter=0): self.batch_sampler = batch_sampler self.num_iterations = num_iterations self.start_iter = start_iter def __iter__(self): iteration = self.start_iter while iteration <= self.num_iterations: # if the underlying sampler has a set_epoch method, like # DistributedSampler, used for making each process see # a different split of the dataset, then set it if hasattr(self.batch_sampler.sampler, "set_epoch"): self.batch_sampler.sampler.set_epoch(iteration) for batch in self.batch_sampler: iteration += 1 if iteration > self.num_iterations: break yield batch def __len__(self): return self.num_iterations
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/dataset/samplers/iteration_based_batch_sampler.py
# Adopted from https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/utils/metric_logger.py from collections import defaultdict from collections import deque import torch class SmoothedValue(object): """Track a series of values and provide access to smoothed values over a window or the global series average. """ def __init__(self, window_size=20): self.deque = deque(maxlen=window_size) self.series = [] self.total = 0.0 self.count = 0 def update(self, value): self.deque.append(value) self.series.append(value) self.count += 1 self.total += value @property def median(self): d = torch.tensor(list(self.deque)) return d.median().item() @property def avg(self): d = torch.tensor(list(self.deque)) return d.mean().item() @property def global_avg(self): return self.total / self.count class MetricLogger(object): def __init__(self, delimiter="\t"): self.meters = defaultdict(SmoothedValue) self.delimiter = delimiter def update(self, **kwargs): for k, v in kwargs.items(): if isinstance(v, torch.Tensor): v = v.item() assert isinstance(v, (float, int)) self.meters[k].update(v) def __getattr__(self, attr): if attr in self.meters: return self.meters[attr] if attr in self.__dict__: return self.__dict__[attr] raise AttributeError("'{}' object has no attribute '{}'".format( type(self).__name__, attr)) def __str__(self): loss_str = [] for name, meter in self.meters.items(): loss_str.append( "{}: {:.4f} ({:.4f})".format(name, meter.median, meter.global_avg) ) return self.delimiter.join(loss_str)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/utils/metric_logger.py
# Modified from https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/utils/checkpoint.py import logging import os import torch from alphaction.utils.model_serialization import load_state_dict from alphaction.utils.c2_model_loading import load_c2_format from alphaction.structures.memory_pool import MemoryPool class Checkpointer(object): def __init__( self, model, optimizer=None, scheduler=None, save_dir="", save_to_disk=None, logger=None, ): self.model = model self.optimizer = optimizer self.scheduler = scheduler self.save_dir = save_dir self.save_to_disk = save_to_disk if logger is None: logger = logging.getLogger(__name__) self.logger = logger def save(self, name, **kwargs): if not self.save_dir: return if not self.save_to_disk: return data = {} data["model"] = self.model.state_dict() if self.optimizer is not None: data["optimizer"] = self.optimizer.state_dict() if self.scheduler is not None: data["scheduler"] = self.scheduler.state_dict() data.update(kwargs) save_file = os.path.join(self.save_dir, "{}.pth".format(name)) self.logger.info("Saving checkpoint to {}".format(save_file)) torch.save(data, save_file) self.tag_last_checkpoint(save_file) def load(self, f=None, model_weight_only=False, adjust_scheduler=False, no_head=False): if self.has_checkpoint(): # override argument with existing checkpoint f = self.get_checkpoint_file() if not f: # no checkpoint could be found self.logger.info("No checkpoint found. Initializing model from scratch") return {} self.logger.info("Loading checkpoint from {}".format(f)) checkpoint = self._load_file(f) self._load_model(checkpoint, no_head) if "optimizer" in checkpoint and self.optimizer: if model_weight_only: del checkpoint['optimizer'] else: self.logger.info("Loading optimizer from {}".format(f)) self.optimizer.load_state_dict(checkpoint.pop("optimizer")) if "scheduler" in checkpoint and self.scheduler: if model_weight_only: del checkpoint['scheduler'] elif adjust_scheduler: last_epoch = checkpoint.pop("scheduler")['last_epoch'] self.logger.info("Adjust scheduler at iteration {}".format(last_epoch)) self.scheduler.step(last_epoch) else: self.logger.info("Loading scheduler from {}".format(f)) self.scheduler.load_state_dict(checkpoint.pop("scheduler")) if model_weight_only: checkpoint["iteration"] = 0 checkpoint["person_pool"] = MemoryPool() # return any further checkpoint dataset return checkpoint def has_checkpoint(self): save_file = os.path.join(self.save_dir, "last_checkpoint") return os.path.exists(save_file) def get_checkpoint_file(self): save_file = os.path.join(self.save_dir, "last_checkpoint") try: with open(save_file, "r") as f: last_saved = f.read() last_saved = last_saved.strip() except IOError: # if file doesn't exist, maybe because it has just been # deleted by a separate process last_saved = "" return last_saved def tag_last_checkpoint(self, last_filename): save_file = os.path.join(self.save_dir, "last_checkpoint") with open(save_file, "w") as f: f.write(last_filename) def _load_file(self, f): return torch.load(f, map_location=torch.device("cpu")) def _load_model(self, checkpoint, no_head): load_state_dict(self.model, checkpoint.pop("model"), no_head) class ActionCheckpointer(Checkpointer): def __init__( self, cfg, model, optimizer=None, scheduler=None, save_dir="", save_to_disk=None, logger=None, ): super(ActionCheckpointer, self).__init__( model, optimizer, scheduler, save_dir, save_to_disk, logger ) self.cfg = cfg.clone() def _load_file(self, f): if f.endswith(".pkl"): return load_c2_format(f, self._get_c2_weight_map()) loaded = super(ActionCheckpointer, self)._load_file(f) if "model" not in loaded: loaded = dict(model=loaded) return loaded def _get_c2_weight_map(self): if hasattr(self.model, "c2_weight_mapping"): return self.model.c2_weight_mapping() elif hasattr(self.model, "module") and hasattr(self.model.module, "c2_weight_mapping"): return self.model.module.c2_weight_mapping() else: raise RuntimeError("Cannot get C2 weight mapping from current model definition.")
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/utils/checkpoint.py
# Modified from https://github.com/facebookresearch/detectron2/blob/master/detectron2/utils/comm.py """ This file contains primitives for multi-gpu communication. This is useful when doing distributed training. """ import pickle import torch import torch.distributed as dist import functools def get_world_size(): if not dist.is_available(): return 1 if not dist.is_initialized(): return 1 return dist.get_world_size() def get_rank(): if not dist.is_available(): return 0 if not dist.is_initialized(): return 0 return dist.get_rank() def is_main_process(): return get_rank() == 0 def synchronize(group=None): """ Helper function to synchronize (barrier) among all processes when using distributed training """ if not dist.is_available(): return if not dist.is_initialized(): return if group is None: group = _get_global_gloo_group() world_size = dist.get_world_size(group=group) if world_size == 1: return dist.barrier(group=group) @functools.lru_cache() def _get_global_gloo_group(): """ Return a process group based on gloo backend, containing all the ranks The result is cached. """ if dist.get_backend() == "nccl": return dist.new_group(backend="gloo") else: return dist.group.WORLD def _serialize_to_tensor(data, group): backend = dist.get_backend(group) assert backend in ["gloo", "nccl"] device = torch.device("cpu" if backend == "gloo" else "cuda") buffer = pickle.dumps(data) storage = torch.ByteStorage.from_buffer(buffer) tensor = torch.ByteTensor(storage).to(device=device) return tensor def _pad_to_largest_tensor(tensor, group): """ Returns: list[int]: size of the tensor, on each rank Tensor: padded tensor that has the max size """ world_size = dist.get_world_size(group=group) assert ( world_size >= 1 ), "comm.all_gather must be called from ranks within the given group!" local_size = torch.tensor([tensor.numel()], dtype=torch.int64, device=tensor.device) size_list = [ torch.zeros([1], dtype=torch.int64, device=tensor.device) for _ in range(world_size) ] dist.all_gather(size_list, local_size, group=group) size_list = [int(size.item()) for size in size_list] max_size = max(size_list) # we pad the tensor because torch all_gather does not support # gathering tensors of different shapes if local_size != max_size: padding = torch.zeros((max_size - local_size,), dtype=torch.uint8, device=tensor.device) tensor = torch.cat((tensor, padding), dim=0) return size_list, tensor def all_gather(data, group=None): """ Run all_gather on arbitrary picklable data (not necessarily tensors). Args: data: any picklable object group: a torch process group. By default, will use a group which contains all ranks on gloo backend. Returns: list[data]: list of data gathered from each rank """ if get_world_size() == 1: return [data] if group is None: group = _get_global_gloo_group() if dist.get_world_size(group) == 1: return [data] tensor = _serialize_to_tensor(data, group) size_list, tensor = _pad_to_largest_tensor(tensor, group) max_size = max(size_list) # receiving Tensor from all ranks tensor_list = [ torch.empty((max_size,), dtype=torch.uint8, device=tensor.device) for _ in size_list ] dist.all_gather(tensor_list, tensor, group=group) data_list = [] for size, tensor in zip(size_list, tensor_list): buffer = tensor.cpu().numpy().tobytes()[:size] data_list.append(pickle.loads(buffer)) return data_list def gather(data, dst=0, group=None): """ Run gather on arbitrary picklable data (not necessarily tensors). Args: data: any picklable object dst (int): destination rank group: a torch process group. By default, will use a group which contains all ranks on gloo backend. Returns: list[data]: on dst, a list of data gathered from each rank. Otherwise, an empty list. """ if get_world_size() == 1: return [data] if group is None: group = _get_global_gloo_group() if dist.get_world_size(group=group) == 1: return [data] rank = dist.get_rank(group=group) tensor = _serialize_to_tensor(data, group) size_list, tensor = _pad_to_largest_tensor(tensor, group) # receiving Tensor from all ranks if rank == dst: max_size = max(size_list) tensor_list = [ torch.empty((max_size,), dtype=torch.uint8, device=tensor.device) for _ in size_list ] dist.gather(tensor, tensor_list, dst=dst, group=group) data_list = [] for size, tensor in zip(size_list, tensor_list): buffer = tensor.cpu().numpy().tobytes()[:size] data_list.append(pickle.loads(buffer)) return data_list else: dist.gather(tensor, [], dst=dst, group=group) return [] def reduce_dict(input_dict, average=True): """ Args: input_dict (dict): all the values will be reduced average (bool): whether to do average or sum Reduce the values in the dictionary from all processes so that process with rank 0 has the averaged results. Returns a dict with the same fields as input_dict, after reduction. """ world_size = get_world_size() if world_size < 2: return input_dict with torch.no_grad(): names = [] values = [] # sort the keys so that they are consistent across processes for k in sorted(input_dict.keys()): names.append(k) values.append(input_dict[k]) values = torch.stack(values, dim=0) dist.reduce(values, dst=0) if dist.get_rank() == 0 and average: # only main process gets accumulated, so only divide by # world_size in this case values /= world_size reduced_dict = {k: v for k, v in zip(names, values)} return reduced_dict def all_reduce(tensor, average=False): world_size = get_world_size() if world_size < 2: return dist.all_reduce(tensor) if average: tensor /= world_size
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/utils/comm.py
import logging import torch import pickle from collections import OrderedDict def _rename_weights(weights, weight_map): logger = logging.getLogger(__name__) logger.info("Remapping C2 weights") max_c2_key_size = max([len(k) for k in weight_map.values()]) new_weights = OrderedDict() for k in weight_map: c2_name = weight_map[k] logger.info("C2 name: {: <{}} mapped name: {}".format(c2_name, max_c2_key_size, k)) if c2_name not in weights: logger.info("{} not found in C2 weights file, skipped.".format(c2_name)) continue v = weights[c2_name] w = torch.from_numpy(v) new_weights[k] = w return new_weights def _load_c2_pickled_weights(file_path): with open(file_path, "rb") as f: if torch._six.PY3: data = pickle.load(f, encoding="latin1") else: data = pickle.load(f) if "blobs" in data: weights = data["blobs"] else: weights = data return weights def load_c2_format(f, weight_map): # We also support load from caffe2 weights. state_dict = _load_c2_pickled_weights(f) state_dict = _rename_weights(state_dict, weight_map) return dict(model=state_dict)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/utils/c2_model_loading.py
# Adopted from https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/utils/registry.py def _register_generic(module_dict, module_name, module): assert module_name not in module_dict module_dict[module_name] = module class Registry(dict): ''' A helper class for managing registering modules, it extends a dictionary and provides a register functions. Eg. creeting a registry: some_registry = Registry({"default": default_module}) There're two ways of registering new modules: 1): normal way is just calling register function: def foo(): ... some_registry.register("foo_module", foo) 2): used as decorator when declaring the module: @some_registry.register("foo_module") @some_registry.register("foo_modeul_nickname") def foo(): ... Access of module is just like using a dictionary, eg: f = some_registry["foo_modeul"] ''' def __init__(self, *args, **kwargs): super(Registry, self).__init__(*args, **kwargs) def register(self, module_name, module=None): # used as function call if module is not None: _register_generic(self, module_name, module) return # used as decorator def register_fn(fn): _register_generic(self, module_name, fn) return fn return register_fn
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/utils/registry.py
import av import io import decord def av_decode_video(video_path): with av.open(video_path) as container: frames = [] for frame in container.decode(video=0): frames.append(frame.to_rgb().to_ndarray()) return frames
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/utils/video_decode.py
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/utils/__init__.py
# Modified from https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/utils/logger.py import logging import os import sys import time def setup_logger(name, save_dir, distributed_rank, filename=None): logger = logging.getLogger(name) logger.setLevel(logging.DEBUG) logger.propagate = False # don't log results for the non-master process if distributed_rank > 0: return logger ch = logging.StreamHandler(stream=sys.stdout) ch.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s: %(message)s") ch.setFormatter(formatter) logger.addHandler(ch) if save_dir: if filename is None: filename = time.strftime("%Y-%m-%d_%H.%M.%S", time.localtime()) + ".log" fh = logging.FileHandler(os.path.join(save_dir, filename)) fh.setLevel(logging.DEBUG) fh.setFormatter(formatter) logger.addHandler(fh) return logger def setup_tblogger(save_dir, distributed_rank): if distributed_rank>0: return None from tensorboardX import SummaryWriter tbdir = os.path.join(save_dir,'tb') os.makedirs(tbdir,exist_ok=True) tblogger = SummaryWriter(tbdir) return tblogger
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/utils/logger.py
# Modified from https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/utils/model_serialization.py from collections import OrderedDict import logging import torch def align_and_update_state_dicts(model_state_dict, loaded_state_dict, no_head): """ Strategy: suppose that the models that we will create will have prefixes appended to each of its keys, for example due to an extra level of nesting that the original pre-trained weights from ImageNet won't contain. For example, model.state_dict() might return backbone[0].body.res2.conv1.weight, while the pre-trained model contains res2.conv1.weight. We thus want to match both parameters together. For that, we look for each model weight, look among all loaded keys if there is one that is a suffix of the current weight name, and use it if that's the case. If multiple matches exist, take the one with longest size of the corresponding name. For example, for the same model as before, the pretrained weight file can contain both res2.conv1.weight, as well as conv1.weight. In this case, we want to match backbone[0].body.conv1.weight to conv1.weight, and backbone[0].body.res2.conv1.weight to res2.conv1.weight. """ current_keys = sorted(list(model_state_dict.keys())) loaded_keys = sorted(list(loaded_state_dict.keys())) # get a matrix of string matches, where each (i, j) entry correspond to the size of the # loaded_key string, if it matches match_matrix = [ len(j) if i.endswith(j) else 0 for i in current_keys for j in loaded_keys ] match_matrix = torch.as_tensor(match_matrix).view( len(current_keys), len(loaded_keys) ) max_match_size, idxs = match_matrix.max(1) # remove indices that correspond to no-match idxs[max_match_size == 0] = -1 # used for logging max_size = max([len(key) for key in current_keys]) if current_keys else 1 max_size_loaded = max([len(key) for key in loaded_keys]) if loaded_keys else 1 log_str_template = "{: <{}} loaded from {: <{}} of shape {}" logger = logging.getLogger(__name__) for idx_new, idx_old in enumerate(idxs.tolist()): if idx_old == -1: continue key = current_keys[idx_new] key_old = loaded_keys[idx_old] if no_head and key_old.startswith("roi_heads."): logger.info("{} will not be loaded.".format(key)) continue model_state_dict[key] = loaded_state_dict[key_old] logger.info( log_str_template.format( key, max_size, key_old, max_size_loaded, tuple(loaded_state_dict[key_old].shape), ) ) def strip_prefix_if_present(state_dict, prefix): keys = sorted(state_dict.keys()) if not all(key.startswith(prefix) for key in keys): return state_dict stripped_state_dict = OrderedDict() for key, value in state_dict.items(): stripped_state_dict[key.replace(prefix, "")] = value return stripped_state_dict def load_state_dict(model, loaded_state_dict, no_head): model_state_dict = model.state_dict() # if the state_dict comes from a model that was wrapped in a # DataParallel or DistributedDataParallel during serialization, # remove the "module" prefix before performing the matching loaded_state_dict = strip_prefix_if_present(loaded_state_dict, prefix="module.") align_and_update_state_dicts(model_state_dict, loaded_state_dict, no_head) # use strict loading model.load_state_dict(model_state_dict)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/utils/model_serialization.py
import itertools def _block_set(ia_blocks): if len(ia_blocks) > 0 and isinstance(ia_blocks[0], list): ia_blocks = list(itertools.chain.from_iterable(ia_blocks)) return ia_blocks def has_person(ia_config): ia_blocks = _block_set(ia_config.I_BLOCK_LIST) return (ia_config.ACTIVE and 'P' in ia_blocks and ia_config.MAX_PERSON > 0) def has_object(ia_config): ia_blocks = _block_set(ia_config.I_BLOCK_LIST) return (ia_config.ACTIVE and 'O' in ia_blocks and ia_config.MAX_OBJECT > 0) def has_memory(ia_config): ia_blocks = _block_set(ia_config.I_BLOCK_LIST) return (ia_config.ACTIVE and 'M' in ia_blocks and ia_config.MAX_PER_SEC > 0)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/utils/IA_helper.py
import torch import random import numpy as np def set_seed(seed, rank, world_size): rng = random.Random(seed) seed_per_rank = [rng.randint(0, 2**32-1) for _ in range(world_size)] cur_seed = seed_per_rank[rank] random.seed(cur_seed) torch.manual_seed(cur_seed) torch.cuda.manual_seed(cur_seed) np.random.seed(cur_seed)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/utils/random_seed.py
import torch from .lr_scheduler import WarmupMultiStepLR, HalfPeriodCosStepLR import torch.nn as nn from alphaction.modeling.roi_heads.action_head.IA_structure import IAStructure def make_optimizer(cfg, model): params = [] bn_param_set = set() transformer_param_set = set() for name, module in model.named_modules(): if isinstance(module, (nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d)): bn_param_set.add(name + ".weight") bn_param_set.add(name + ".bias") elif isinstance(module, IAStructure): for param_name, _ in module.named_parameters(name): transformer_param_set.add(param_name) for key, value in model.named_parameters(): if not value.requires_grad: continue lr = cfg.SOLVER.BASE_LR weight_decay = cfg.SOLVER.WEIGHT_DECAY if key in bn_param_set: weight_decay = cfg.SOLVER.WEIGHT_DECAY_BN elif "bias" in key: lr = cfg.SOLVER.BASE_LR * cfg.SOLVER.BIAS_LR_FACTOR weight_decay = cfg.SOLVER.WEIGHT_DECAY_BIAS if key in transformer_param_set: lr = lr * cfg.SOLVER.IA_LR_FACTOR params += [{"params": [value], "lr": lr, "weight_decay": weight_decay}] optimizer = torch.optim.SGD(params, cfg.SOLVER.BASE_LR, momentum=cfg.SOLVER.MOMENTUM) return optimizer def make_lr_scheduler(cfg, optimizer): scheduler = cfg.SOLVER.SCHEDULER if scheduler not in ("half_period_cosine", "warmup_multi_step"): raise ValueError('Scheduler not available') if scheduler == 'warmup_multi_step': return WarmupMultiStepLR( optimizer, cfg.SOLVER.STEPS, cfg.SOLVER.GAMMA, warmup_factor=cfg.SOLVER.WARMUP_FACTOR, warmup_iters=cfg.SOLVER.WARMUP_ITERS if cfg.SOLVER.WARMUP_ON else 0, warmup_method=cfg.SOLVER.WARMUP_METHOD, ) elif scheduler == 'half_period_cosine': return HalfPeriodCosStepLR( optimizer, warmup_factor=cfg.SOLVER.WARMUP_FACTOR, warmup_iters=cfg.SOLVER.WARMUP_ITERS if cfg.SOLVER.WARMUP_ON else 0, max_iters=cfg.SOLVER.MAX_ITER, warmup_method=cfg.SOLVER.WARMUP_METHOD, )
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/solver/build.py
# Modified from https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/solver/lr_scheduler.py from bisect import bisect_right import torch import math class WarmupMultiStepLR(torch.optim.lr_scheduler._LRScheduler): def __init__( self, optimizer, milestones, gamma=0.1, warmup_factor=1.0 / 3, warmup_iters=500, warmup_method="linear", last_epoch=-1, ): if not list(milestones) == sorted(milestones): raise ValueError( "Milestones should be a list of" " increasing integers. Got {}", milestones, ) if warmup_method not in ("constant", "linear"): raise ValueError( "Only 'constant' or 'linear' warmup_method accepted" "got {}".format(warmup_method) ) self.milestones = milestones self.gamma = gamma self.warmup_factor = warmup_factor self.warmup_iters = warmup_iters self.warmup_method = warmup_method super(WarmupMultiStepLR, self).__init__(optimizer, last_epoch) def get_lr(self): warmup_factor = 1 if self.last_epoch < self.warmup_iters: if self.warmup_method == "constant": warmup_factor = self.warmup_factor elif self.warmup_method == "linear": alpha = float(self.last_epoch) / self.warmup_iters warmup_factor = self.warmup_factor * (1 - alpha) + alpha return [ base_lr * warmup_factor * self.gamma ** bisect_right(self.milestones, self.last_epoch) for base_lr in self.base_lrs ] class HalfPeriodCosStepLR(torch.optim.lr_scheduler._LRScheduler): def __init__( self, optimizer, warmup_factor=1.0 / 3, warmup_iters=8000, max_iters=60000, warmup_method="linear", last_epoch=-1, ): if warmup_method not in ("constant", "linear"): raise ValueError( "Only 'constant' or 'linear' warmup_method accepted" "got {}".format(warmup_method) ) self.warmup_factor = warmup_factor self.warmup_iters = warmup_iters self.max_iters = max_iters self.warmup_method = warmup_method super(HalfPeriodCosStepLR, self).__init__(optimizer, last_epoch) def get_lr(self): warmup_factor = 1 if self.last_epoch < self.warmup_iters: if self.warmup_method == "constant": warmup_factor = self.warmup_factor elif self.warmup_method == "linear": alpha = float(self.last_epoch) / self.warmup_iters warmup_factor = self.warmup_factor * (1 - alpha) + alpha else: warmup_factor = 0.5 * (math.cos(self.last_epoch / self.max_iters * math.pi) + 1) return [ base_lr * warmup_factor for base_lr in self.base_lrs ]
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/solver/lr_scheduler.py
from .build import make_optimizer from .build import make_lr_scheduler from .lr_scheduler import WarmupMultiStepLR
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/solver/__init__.py
from __future__ import (absolute_import, division, print_function, unicode_literals) import torch import torch.nn as nn from alphaction.layers import FrozenBatchNorm3d class NLBlock(nn.Module): def __init__(self, dim_in, dim_out, dim_inner, nl_cfg, group=False): super(NLBlock, self).__init__() self.nl_cfg = nl_cfg.clone() self.group = group self.group_size = 4 init_std = nl_cfg.CONV_INIT_STD bias = not nl_cfg.NO_BIAS pool_stride = 2 self.scale_value = dim_inner ** (-0.5) self.dim_inner = dim_inner self.theta = nn.Conv3d(dim_in, dim_inner, 1, bias=bias) nn.init.normal_(self.theta.weight, std=init_std) if bias: nn.init.constant_(self.theta.bias, 0) if nl_cfg.USE_MAXPOOL: self.maxpool = nn.MaxPool3d((1, pool_stride, pool_stride), stride=(1, pool_stride, pool_stride)) self.phi = nn.Conv3d(dim_in, dim_inner, 1, bias=bias) nn.init.normal_(self.phi.weight, std=init_std) if bias: nn.init.constant_(self.phi.bias, 0) self.g = nn.Conv3d(dim_in, dim_inner, 1, bias=bias) nn.init.normal_(self.g.weight, std=init_std) if bias: nn.init.constant_(self.g.bias, 0) if nl_cfg.USE_SOFTMAX: self.softmax = nn.Softmax(dim=2) self.out = nn.Conv3d(dim_inner, dim_out, 1, bias=bias) if nl_cfg.USE_ZERO_INIT_CONV: nn.init.constant_(self.out.weight, 0) else: nn.init.normal_(self.out.weight, std=init_std) if bias: nn.init.constant_(self.out.bias, 0) if nl_cfg.USE_BN: if nl_cfg.FROZEN_BN: self.bn = FrozenBatchNorm3d(dim_out, eps=nl_cfg.BN_EPSILON) else: self.bn = nn.BatchNorm3d(dim_out, eps=nl_cfg.BN_EPSILON, momentum=nl_cfg.BN_MOMENTUM) nn.init.constant_(self.bn.weight, nl_cfg.BN_INIT_GAMMA) def forward(self, x): if x.dim() != 5: raise ValueError('expected 4D or 5D input (got {}D input)' .format(x.dim())) if self.group: x = x.transpose(1, 2) sz_before_group = list(x.shape) sz_after_group = sz_before_group.copy() sz_after_group[0] = -1 sz_after_group[1] = self.group_size x = x.contiguous().view(*sz_after_group) x = x.transpose(1, 2) batch_size = x.shape[0] theta = self.theta(x) if self.nl_cfg.USE_MAXPOOL: max_pool = self.maxpool(x) else: max_pool = x phi = self.phi(max_pool) g = self.g(max_pool) org_size = theta.size() mat_size = [batch_size, self.dim_inner, -1] theta = theta.view(*mat_size) phi = phi.view(*mat_size) g = g.view(*mat_size) theta_phi = torch.bmm(theta.transpose(1, 2), phi) if self.nl_cfg.USE_SOFTMAX: if self.nl_cfg.USE_SCALE: theta_phi_sc = theta_phi * self.scale_value else: theta_phi_sc = theta_phi p = self.softmax(theta_phi_sc) else: p = theta_phi / theta_phi.shape[-1] t = torch.bmm(g, p.transpose(1, 2)) t = t.view(org_size) out = self.out(t) if self.nl_cfg.USE_BN: out = self.bn(out) out = out + x if self.group: out = out.transpose(1, 2) out = out.contiguous().view(*sz_before_group) out = out.transpose(1, 2) return out def c2_weight_mapping(self): weight_map = {} for name, m_child in self.named_children(): if m_child.state_dict(): if isinstance(m_child, (nn.BatchNorm3d, FrozenBatchNorm3d)): weight_map[name + '.weight'] = '{}_s'.format(name) weight_map[name + '.running_mean'] = '{}_rm'.format(name) weight_map[name + '.running_var'] = '{}_riv'.format(name) elif isinstance(m_child, nn.GroupNorm): weight_map[name + '.weight'] = '{}_s'.format(name) else: weight_map[name + '.weight'] = '{}_w'.format(name) weight_map[name + '.bias'] = '{}_b'.format(name) return weight_map
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/nonlocal_block.py
import torch.nn as nn from alphaction.modeling.nonlocal_block import NLBlock from alphaction.layers import FrozenBatchNorm3d class Conv3dBN(nn.Module): def __init__(self, cfg, dim_in, dim_out, kernels, stride, padding, dilation=1, init_weight=None): super(Conv3dBN, self).__init__() self.conv = nn.Conv3d(dim_in, dim_out, kernels, stride=stride, padding=padding, dilation=dilation, bias=False) nn.init.kaiming_normal_(self.conv.weight) if cfg.MODEL.BACKBONE.FROZEN_BN: self.bn = FrozenBatchNorm3d(dim_out, eps=cfg.MODEL.BACKBONE.BN_EPSILON) nn.init.constant_(self.bn.weight, 1.0) nn.init.constant_(self.bn.bias, 0.0) else: self.bn = nn.BatchNorm3d(dim_out, eps=cfg.MODEL.BACKBONE.BN_EPSILON, momentum=cfg.MODEL.BACKBONE.BN_MOMENTUM) if init_weight is not None: nn.init.constant_(self.bn.weight, init_weight) def forward(self, x): out = self.conv(x) out = self.bn(out) return out def c2_weight_mapping(self): return { 'conv.weight': 'w', 'bn.weight': 'bn_s', 'bn.bias': 'bn_b', 'bn.running_mean': 'bn_rm', 'bn.running_var': 'bn_riv' } class Bottleneck(nn.Module): def __init__(self, cfg, dim_in, dim_out, dim_inner, stride, dilation=1, use_temp_conv=1, temp_stride=1): super(Bottleneck, self).__init__() self.conv1 = Conv3dBN(cfg, dim_in, dim_inner, (1 + use_temp_conv * 2, 1, 1), stride=(temp_stride, 1, 1), padding=(use_temp_conv, 0, 0)) self.conv2 = Conv3dBN(cfg, dim_inner, dim_inner, (1, 3, 3), stride=(1, stride, stride), dilation=(1, dilation, dilation), padding=(0, dilation, dilation)) self.conv3 = Conv3dBN(cfg, dim_inner, dim_out, (1, 1, 1), stride=(1, 1, 1), padding=0, init_weight=cfg.MODEL.BACKBONE.BN_INIT_GAMMA) self.relu = nn.ReLU(inplace=True) def forward(self, x): out = self.conv1(x) out = self.relu(out) out = self.conv2(out) out = self.relu(out) out = self.conv3(out) return out def c2_weight_mapping(self): weight_map = {} for i in range(1, 4): name = 'conv{}'.format(i) child_map = getattr(self, name).c2_weight_mapping() for key, val in child_map.items(): new_key = name + '.' + key prefix = 'branch2{}_'.format(chr(ord('a') + i - 1)) weight_map[new_key] = prefix + val return weight_map class ResBlock(nn.Module): def __init__(self, cfg, dim_in, dim_out, dim_inner, stride, dilation=1, use_temp_conv=0, temp_stride=1, need_shortcut=False): super(ResBlock, self).__init__() self.btnk = Bottleneck(cfg, dim_in, dim_out, dim_inner=dim_inner, stride=stride, dilation=dilation, use_temp_conv=use_temp_conv, temp_stride=temp_stride) if not need_shortcut: self.shortcut = None else: self.shortcut = Conv3dBN(cfg, dim_in, dim_out, (1, 1, 1), stride=(temp_stride, stride, stride), padding=0) self.relu = nn.ReLU(inplace=True) def forward(self, x): tr = self.btnk(x) if self.shortcut is None: sc = x else: sc = self.shortcut(x) return self.relu(tr + sc) def c2_weight_mapping(self): weight_map = {} for name, m_child in self.named_children(): if m_child.state_dict(): child_map = m_child.c2_weight_mapping() for key, val in child_map.items(): new_key = name + '.' + key if isinstance(m_child, Conv3dBN): prefix = 'branch1_' else: prefix = '' weight_map[new_key] = prefix + val return weight_map class ResNLBlock(nn.Module): def __init__(self, cfg, dim_in, dim_out, stride, num_blocks, dim_inner, use_temp_convs, temp_strides, dilation=1, nonlocal_mod=1000, group_nonlocal=False, lateral=False): super(ResNLBlock, self).__init__() self.blocks = [] for idx in range(num_blocks): block_name = "res_{}".format(idx) block_stride = stride if idx == 0 else 1 block_dilation = dilation dim_in0 = dim_in + int(dim_in * cfg.MODEL.BACKBONE.SLOWFAST.BETA * 2) if lateral and (idx == 0) and ( cfg.MODEL.BACKBONE.SLOWFAST.LATERAL != 'ttoc_sum') else dim_in # To transfer weight from classification model, we change res5_0 from stride 2 to stride 1, # and all res5_x layers from dilation 1 to dilation 2. In pretrain, res5_0 with stride 2 need a shortcut conv. # idx==0 and dilation!=1 means that it need a short cut in pretrain stage, # so we should keep it since we load weight from a pretrained model. # if idx!=0, block_stride will not be larger than 1 in pretrain stage. need_shortcut = not (dim_in0==dim_out and temp_strides[idx]==1 and block_stride==1) or \ (idx==0 and dilation!=1) res_module = ResBlock(cfg, dim_in0, dim_out, dim_inner=dim_inner, stride=block_stride, dilation=block_dilation, use_temp_conv=use_temp_convs[idx], temp_stride=temp_strides[idx], need_shortcut=need_shortcut) self.add_module(block_name, res_module) self.blocks.append(block_name) dim_in = dim_out if idx % nonlocal_mod == nonlocal_mod - 1: nl_block_name = "nonlocal_{}".format(idx) nl_module = NLBlock(dim_in, dim_in, int(dim_in / 2), cfg.MODEL.NONLOCAL, group=group_nonlocal) self.add_module(nl_block_name, nl_module) self.blocks.append(nl_block_name) def forward(self, x): for layer_name in self.blocks: x = getattr(self, layer_name)(x) return x def c2_weight_mapping(self): weight_map = {} for name, m_child in self.named_children(): idx = name.split('_')[-1] if m_child.state_dict(): child_map = m_child.c2_weight_mapping() for key, val in child_map.items(): new_key = name + '.' + key if isinstance(m_child, NLBlock): prefix = 'nonlocal_conv{}_' + '{}_'.format(idx) else: prefix = 'res{}_' + '{}_'.format(idx) weight_map[new_key] = prefix + val return weight_map
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/common_blocks.py
import torch from torch import nn from alphaction.layers import ROIAlign3d, ROIPool3d class Pooler3d(nn.Module): def __init__(self, output_size, scale, sampling_ratio=None, pooler_type='align3d'): super(Pooler3d, self).__init__() if pooler_type == 'align3d': assert sampling_ratio is not None, 'Sampling ratio should be specified for 3d roi align.' self.pooler = ROIAlign3d( output_size, spatial_scale=scale, sampling_ratio=sampling_ratio ) elif pooler_type == 'pooling3d': self.pooler = ROIPool3d( output_size, spatial_scale=scale ) self.output_size = output_size def convert_to_roi_format(self, boxes, dtype, device): bbox_list = list() ids_list = list() for i, b in enumerate(boxes): if not b: bbox_list.append(torch.zeros((0, 4), dtype=dtype, device=device)) ids_list.append(torch.zeros((0, 1), dtype=dtype, device=device)) else: bbox_list.append(b.bbox.to(dtype)) ids_list.append(torch.full((len(b), 1), i, dtype=dtype, device=device)) concat_boxes = torch.cat(bbox_list, dim=0) ids = torch.cat(ids_list, dim=0) rois = torch.cat([ids, concat_boxes], dim=1) return rois def forward(self, x, boxes): rois = self.convert_to_roi_format(boxes, x.dtype, x.device) return self.pooler(x, rois) def make_3d_pooler(head_cfg): resolution = head_cfg.POOLER_RESOLUTION scale = head_cfg.POOLER_SCALE sampling_ratio = head_cfg.POOLER_SAMPLING_RATIO pooler_type = head_cfg.POOLER_TYPE pooler = Pooler3d( output_size=(resolution, resolution), scale=scale, sampling_ratio=sampling_ratio, pooler_type=pooler_type, ) return pooler
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/poolers.py
from alphaction.utils.registry import Registry BACKBONES = Registry() ROI_ACTION_FEATURE_EXTRACTORS = Registry() ROI_ACTION_PREDICTORS = Registry() INTERACTION_AGGREGATION_STRUCTURES = Registry()
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/registry.py
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/__init__.py
""" Miscellaneous utility functions """ import torch from alphaction.structures.bounding_box import BoxList def cat(tensors, dim=0): """ Efficient version of torch.cat that avoids a copy if there is only a single element in a list """ assert isinstance(tensors, (list, tuple)) if len(tensors) == 1: return tensors[0] return torch.cat(tensors, dim) def pad_sequence(sequence, targ_size, padding_value=0): tensor_size = sequence[0].size() trailing_dims = tensor_size[1:] out_dims = (len(sequence), targ_size) + trailing_dims out_tensor = sequence[0].new_full(out_dims, padding_value) for i, tensor in enumerate(sequence): length = tensor.size(0) out_tensor[i, :length, ...] = tensor return out_tensor def prepare_pooled_feature(x_pooled, boxes, detach=True): image_shapes = [box.size for box in boxes] boxes_per_image = [len(box) for box in boxes] box_tensors = [a.bbox for a in boxes] if detach: x_pooled = x_pooled.detach() pooled_feature = x_pooled.split(boxes_per_image, dim=0) boxes_result = [] for feature_per_image, boxes_per_image, image_shape in zip( pooled_feature, box_tensors, image_shapes ): boxlist = BoxList(boxes_per_image, image_shape, mode="xyxy") boxlist.add_field("pooled_feature", feature_per_image) boxes_result.append(boxlist) return boxes_result
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/utils.py
from .action_detector import build_detection_model
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/detector/__init__.py
from torch import nn from ..backbone import build_backbone from ..roi_heads.roi_heads_3d import build_3d_roi_heads class ActionDetector(nn.Module): def __init__(self, cfg): super(ActionDetector, self).__init__() self.backbone = build_backbone(cfg) self.roi_heads = build_3d_roi_heads(cfg, self.backbone.dim_out) def forward(self, slow_video, fast_video, boxes, objects=None, extras={}, part_forward=-1): # part_forward is used to split this model into two parts. # if part_forward<0, just use it as a single model # if part_forward=0, use this model to extract pooled feature(person and object, no memory features). # if part_forward=1, use the ia structure to aggregate interactions and give final result. # implemented in roi_heads if part_forward==1: slow_features = fast_features = None else: slow_features, fast_features = self.backbone(slow_video, fast_video) result, detector_losses, loss_weight, detector_metrics = self.roi_heads(slow_features, fast_features, boxes, objects, extras, part_forward) if self.training: return detector_losses, loss_weight, detector_metrics, result return result def c2_weight_mapping(self): if not hasattr(self, "c2_mapping"): weight_map = {} for name, m_child in self.named_children(): if m_child.state_dict() and hasattr(m_child, "c2_weight_mapping"): child_map = m_child.c2_weight_mapping() for key, val in child_map.items(): new_key = name + '.' + key weight_map[new_key] = val self.c2_mapping = weight_map return self.c2_mapping def build_detection_model(cfg): return ActionDetector(cfg)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/detector/action_detector.py
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/roi_heads/__init__.py
import torch from .action_head.action_head import build_roi_action_head class Combined3dROIHeads(torch.nn.ModuleDict): def __init__(self, cfg, heads): super(Combined3dROIHeads, self).__init__(heads) self.cfg = cfg.clone() def forward(self, slow_features, fast_features, boxes, objects=None, extras={}, part_forward=-1): result, loss_action, loss_weight, accuracy_action = self.action(slow_features, fast_features, boxes, objects, extras, part_forward) return result, loss_action, loss_weight, accuracy_action def c2_weight_mapping(self): weight_map = {} for name, m_child in self.named_children(): if m_child.state_dict() and hasattr(m_child,"c2_weight_mapping"): child_map = m_child.c2_weight_mapping() for key, val in child_map.items(): new_key = name + '.' + key weight_map[new_key] = val return weight_map def build_3d_roi_heads(cfg, dim_in): roi_heads = [] roi_heads.append(("action", build_roi_action_head(cfg, dim_in))) if roi_heads: roi_heads = Combined3dROIHeads(cfg, roi_heads) return roi_heads
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/roi_heads/roi_heads_3d.py
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/roi_heads/action_head/__init__.py
import torch from torch import nn from torch.nn import functional as F from alphaction.modeling import registry from alphaction.modeling.poolers import make_3d_pooler from alphaction.modeling.roi_heads.action_head.IA_structure import make_ia_structure from alphaction.modeling.utils import cat, pad_sequence, prepare_pooled_feature from alphaction.utils.IA_helper import has_object @registry.ROI_ACTION_FEATURE_EXTRACTORS.register("2MLPFeatureExtractor") class MLPFeatureExtractor(nn.Module): def __init__(self, config, dim_in): super(MLPFeatureExtractor, self).__init__() self.config = config head_cfg = config.MODEL.ROI_ACTION_HEAD self.pooler = make_3d_pooler(head_cfg) resolution = head_cfg.POOLER_RESOLUTION self.max_pooler = nn.MaxPool3d((1, resolution, resolution)) if config.MODEL.IA_STRUCTURE.ACTIVE: self.max_feature_len_per_sec = config.MODEL.IA_STRUCTURE.MAX_PER_SEC self.ia_structure = make_ia_structure(config, dim_in) representation_size = head_cfg.MLP_HEAD_DIM fc1_dim_in = dim_in if config.MODEL.IA_STRUCTURE.ACTIVE and (config.MODEL.IA_STRUCTURE.FUSION == "concat"): fc1_dim_in += config.MODEL.IA_STRUCTURE.DIM_OUT self.fc1 = nn.Linear(fc1_dim_in, representation_size) self.fc2 = nn.Linear(representation_size, representation_size) for l in [self.fc1, self.fc2]: nn.init.kaiming_uniform_(l.weight, a=1) nn.init.constant_(l.bias, 0) self.dim_out = representation_size def roi_pooling(self, slow_features, fast_features, proposals): if slow_features is not None: if self.config.MODEL.ROI_ACTION_HEAD.MEAN_BEFORE_POOLER: slow_features = slow_features.mean(dim=2, keepdim=True) slow_x = self.pooler(slow_features, proposals) if not self.config.MODEL.ROI_ACTION_HEAD.MEAN_BEFORE_POOLER: slow_x = slow_x.mean(dim=2, keepdim=True) x = slow_x if fast_features is not None: if self.config.MODEL.ROI_ACTION_HEAD.MEAN_BEFORE_POOLER: fast_features = fast_features.mean(dim=2, keepdim=True) fast_x = self.pooler(fast_features, proposals) if not self.config.MODEL.ROI_ACTION_HEAD.MEAN_BEFORE_POOLER: fast_x = fast_x.mean(dim=2, keepdim=True) x = fast_x if slow_features is not None and fast_features is not None: x = torch.cat([slow_x, fast_x], dim=1) return x def max_pooling_zero_safe(self, x): if x.size(0) == 0: _, c, t, h, w = x.size() res = self.config.MODEL.ROI_ACTION_HEAD.POOLER_RESOLUTION x = torch.zeros((0, c, 1, h - res + 1, w - res + 1), device=x.device) else: x = self.max_pooler(x) return x def forward(self, slow_features, fast_features, proposals, objects=None, extras={}, part_forward=-1): ia_active = hasattr(self, "ia_structure") if part_forward == 1: person_pooled = cat([box.get_field("pooled_feature") for box in proposals]) if objects is None: object_pooled = None else: object_pooled = cat([box.get_field("pooled_feature") for box in objects]) else: x = self.roi_pooling(slow_features, fast_features, proposals) person_pooled = self.max_pooler(x) if has_object(self.config.MODEL.IA_STRUCTURE): object_pooled = self.roi_pooling(slow_features, fast_features, objects) object_pooled = self.max_pooling_zero_safe(object_pooled) else: object_pooled = None if part_forward == 0: return None, person_pooled, object_pooled x_after = person_pooled if ia_active: tsfmr = self.ia_structure mem_len = self.config.MODEL.IA_STRUCTURE.LENGTH mem_rate = self.config.MODEL.IA_STRUCTURE.MEMORY_RATE use_penalty = self.config.MODEL.IA_STRUCTURE.PENALTY memory_person, memory_person_boxes = self.get_memory_feature(extras["person_pool"], extras, mem_len, mem_rate, self.max_feature_len_per_sec, tsfmr.dim_others, person_pooled, proposals, use_penalty) ia_feature = self.ia_structure(person_pooled, proposals, object_pooled, objects, memory_person, ) x_after = self.fusion(x_after, ia_feature, self.config.MODEL.IA_STRUCTURE.FUSION) x_after = x_after.view(x_after.size(0), -1) x_after = F.relu(self.fc1(x_after)) x_after = F.relu(self.fc2(x_after)) return x_after, person_pooled, object_pooled def get_memory_feature(self, feature_pool, extras, mem_len, mem_rate, max_boxes, fixed_dim, current_x, current_box, use_penalty): before, after = mem_len mem_feature_list = [] mem_pos_list = [] device = current_x.device if use_penalty and self.training: cur_loss = extras["cur_loss"] else: cur_loss = 0.0 current_feat = prepare_pooled_feature(current_x, current_box, detach=True) for movie_id, timestamp, new_feat in zip(extras["movie_ids"], extras["timestamps"], current_feat): before_inds = range(timestamp - before * mem_rate, timestamp, mem_rate) after_inds = range(timestamp + mem_rate, timestamp + (after + 1) * mem_rate, mem_rate) cache_cur_mov = feature_pool[movie_id] mem_box_list_before = [self.check_fetch_mem_feature(cache_cur_mov, mem_ind, max_boxes, cur_loss, use_penalty) for mem_ind in before_inds] mem_box_list_after = [self.check_fetch_mem_feature(cache_cur_mov, mem_ind, max_boxes, cur_loss, use_penalty) for mem_ind in after_inds] mem_box_current = [self.sample_mem_feature(new_feat, max_boxes), ] mem_box_list = mem_box_list_before + mem_box_current + mem_box_list_after mem_feature_list += [box_list.get_field("pooled_feature") if box_list is not None else torch.zeros(0, fixed_dim, 1, 1, 1, dtype=torch.float32, device="cuda") for box_list in mem_box_list] mem_pos_list += [box_list.bbox if box_list is not None else torch.zeros(0, 4, dtype=torch.float32, device="cuda") for box_list in mem_box_list] seq_length = sum(mem_len) + 1 person_per_seq = seq_length * max_boxes mem_feature = pad_sequence(mem_feature_list, max_boxes) mem_feature = mem_feature.view(-1, person_per_seq, fixed_dim, 1, 1, 1) mem_feature = mem_feature.to(device) mem_pos = pad_sequence(mem_pos_list, max_boxes) mem_pos = mem_pos.view(-1, person_per_seq, 4) mem_pos = mem_pos.to(device) return mem_feature, mem_pos def check_fetch_mem_feature(self, movie_cache, mem_ind, max_num, cur_loss, use_penalty): if mem_ind not in movie_cache: return None box_list = movie_cache[mem_ind] box_list = self.sample_mem_feature(box_list, max_num) if use_penalty and self.training: loss_tag = box_list.delete_field("loss_tag") penalty = loss_tag / cur_loss if loss_tag < cur_loss else cur_loss / loss_tag features = box_list.get_field("pooled_feature") * penalty box_list.add_field("pooled_feature", features) return box_list def sample_mem_feature(self, box_list, max_num): if len(box_list) > max_num: idx = torch.randperm(len(box_list))[:max_num] return box_list[idx].to("cuda") else: return box_list.to("cuda") def fusion(self, x, out, type="add"): if type == "add": return x + out elif type == "concat": return torch.cat([x, out], dim=1) else: raise NotImplementedError def make_roi_action_feature_extractor(cfg, dim_in): func = registry.ROI_ACTION_FEATURE_EXTRACTORS[ cfg.MODEL.ROI_ACTION_HEAD.FEATURE_EXTRACTOR ] return func(cfg, dim_in)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/roi_heads/action_head/roi_action_feature_extractor.py
import torch from .roi_action_feature_extractor import make_roi_action_feature_extractor from .roi_action_predictors import make_roi_action_predictor from .inference import make_roi_action_post_processor from .loss import make_roi_action_loss_evaluator from .metric import make_roi_action_accuracy_evaluator from alphaction.modeling.utils import prepare_pooled_feature from alphaction.utils.comm import all_reduce class ROIActionHead(torch.nn.Module): """ Generic Action Head class. """ def __init__(self, cfg, dim_in): super(ROIActionHead, self).__init__() self.feature_extractor = make_roi_action_feature_extractor(cfg, dim_in) self.predictor = make_roi_action_predictor(cfg, self.feature_extractor.dim_out) self.post_processor = make_roi_action_post_processor(cfg) self.loss_evaluator = make_roi_action_loss_evaluator(cfg) self.accuracy_evaluator = make_roi_action_accuracy_evaluator(cfg) self.test_ext = cfg.TEST.EXTEND_SCALE def forward(self, slow_features, fast_features, boxes, objects=None, extras={}, part_forward=-1): # In training stage, boxes are from gt. # In testing stage, boxes are detected by human detector and proposals should be # enlarged boxes. assert not (self.training and part_forward >= 0) if part_forward == 1: boxes = extras["current_feat_p"] objects = extras["current_feat_o"] if self.training: proposals = self.loss_evaluator.sample_box(boxes) else: proposals = [box.extend(self.test_ext) for box in boxes] x, x_pooled, x_objects = self.feature_extractor(slow_features, fast_features, proposals, objects, extras, part_forward) if part_forward == 0: pooled_feature = prepare_pooled_feature(x_pooled, boxes) if x_objects is None: object_pooled_feature = None else: object_pooled_feature = prepare_pooled_feature(x_objects, objects) return [pooled_feature, object_pooled_feature], {}, {}, {} action_logits = self.predictor(x) if not self.training: result = self.post_processor((action_logits,), boxes) return result, {}, {}, {} box_num = action_logits.size(0) box_num = torch.as_tensor([box_num], dtype=torch.float32, device=action_logits.device) all_reduce(box_num, average=True) loss_dict, loss_weight = self.loss_evaluator( [action_logits], box_num.item(), ) metric_dict = self.accuracy_evaluator( [action_logits], proposals, box_num.item(), ) pooled_feature = prepare_pooled_feature(x_pooled, proposals) if x_objects is None: object_pooled_feature = [] else: object_pooled_feature = prepare_pooled_feature(x_objects, objects) return ( [pooled_feature, object_pooled_feature], loss_dict, loss_weight, metric_dict, ) def c2_weight_mapping(self): weight_map = {} for name, m_child in self.named_children(): if m_child.state_dict() and hasattr(m_child, "c2_weight_mapping"): child_map = m_child.c2_weight_mapping() for key, val in child_map.items(): new_key = name + '.' + key weight_map[new_key] = val return weight_map def build_roi_action_head(cfg, dim_in): return ROIActionHead(cfg, dim_in)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/roi_heads/action_head/action_head.py
import torch from alphaction.modeling.utils import cat class ActionAccuracyComputation(object): def __init__(self, num_pose, num_object, num_person): self.num_pose = num_pose self.num_object = num_object self.num_person = num_person def logic_iou(self, pred, label): device = pred.device version = torch.__version__ if eval('.'.join(version.split('.')[:2]))>=1.3: pred = pred.bool() label = label.bool() label_union = (pred | label).float().sum(dim=1) label_inter = (pred & label).float().sum(dim=1) replacer = torch.ones_like(label_union, device=device) zero_mask = label_union == 0 label_inter = torch.where(zero_mask, replacer, label_inter) label_union = torch.where(zero_mask, replacer, label_union) return label_inter / label_union def __call__(self, class_logits, proposals, avg_box_num): class_logits = [logits.detach() for logits in class_logits] class_logits = cat(class_logits, dim=0) assert class_logits.shape[1] == (self.num_pose + self.num_object + self.num_person), \ "The shape of tensor class logits doesn't match total number of action classes." labels = cat([proposal.get_field("labels") for proposal in proposals], dim=0) metric_dict = {} if self.num_pose>0: pose_label = labels[:, :self.num_pose].argmax(dim=1) pose_pred = class_logits[:, :self.num_pose].argmax(dim=1) accuracy_pose_action = pose_label.eq(pose_pred).float().sum() metric_dict["accuracy_pose_action"] = accuracy_pose_action / avg_box_num interaction_label = labels[:, self.num_pose:] interaction_logits = class_logits[:, self.num_pose:] interaction_pred = interaction_logits.sigmoid() > 0.5 if self.num_object>0: object_label = interaction_label[:, :self.num_object] object_pred = interaction_pred[:, :self.num_object] accuracy_object_interaction = self.logic_iou(object_pred, object_label) metric_dict["accuracy_object_interaction"] = accuracy_object_interaction.sum() / avg_box_num if self.num_person>0: person_label = interaction_label[:, self.num_object:] person_pred = interaction_pred[:, self.num_object:] accuracy_person_interaction = self.logic_iou(person_pred, person_label) metric_dict["accuracy_person_interaction"] = accuracy_person_interaction.sum() / avg_box_num return metric_dict def make_roi_action_accuracy_evaluator(cfg): num_pose = cfg.MODEL.ROI_ACTION_HEAD.NUM_PERSON_MOVEMENT_CLASSES num_object = cfg.MODEL.ROI_ACTION_HEAD.NUM_OBJECT_MANIPULATION_CLASSES num_person = cfg.MODEL.ROI_ACTION_HEAD.NUM_PERSON_INTERACTION_CLASSES return ActionAccuracyComputation(num_pose, num_object, num_person)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/roi_heads/action_head/metric.py
import torch from alphaction.layers import SigmoidFocalLoss, SoftmaxFocalLoss from alphaction.modeling.utils import cat class ActionLossComputation(object): def __init__(self, cfg): self.proposal_per_clip = cfg.MODEL.ROI_ACTION_HEAD.PROPOSAL_PER_CLIP self.num_pose = cfg.MODEL.ROI_ACTION_HEAD.NUM_PERSON_MOVEMENT_CLASSES self.num_object = cfg.MODEL.ROI_ACTION_HEAD.NUM_OBJECT_MANIPULATION_CLASSES self.num_person = cfg.MODEL.ROI_ACTION_HEAD.NUM_PERSON_INTERACTION_CLASSES self.weight_dict = dict( loss_pose_action = cfg.MODEL.ROI_ACTION_HEAD.POSE_LOSS_WEIGHT, loss_object_interaction = cfg.MODEL.ROI_ACTION_HEAD.OBJECT_LOSS_WEIGHT, loss_person_interaction = cfg.MODEL.ROI_ACTION_HEAD.PERSON_LOSS_WEIGHT, ) gamma = cfg.MODEL.ROI_ACTION_HEAD.FOCAL_LOSS.GAMMA alpha = cfg.MODEL.ROI_ACTION_HEAD.FOCAL_LOSS.ALPHA self.sigmoid_focal_loss = SigmoidFocalLoss(gamma, alpha, reduction="none") self.softmax_focal_loss = SoftmaxFocalLoss(gamma, alpha, reduction="sum") def sample_box(self, boxes): proposals = [] num_proposals = self.proposal_per_clip for boxes_per_image in boxes: num_boxes = len(boxes_per_image) if num_boxes > num_proposals: choice_inds = torch.randperm(num_boxes)[:num_proposals] proposals_per_image = boxes_per_image[choice_inds] else: proposals_per_image = boxes_per_image proposals_per_image = proposals_per_image.random_aug(0.2, 0.1, 0.1, 0.05) proposals.append(proposals_per_image) self._proposals = proposals return proposals def __call__(self, class_logits, avg_box_num): class_logits = cat(class_logits, dim=0) assert class_logits.shape[1] == (self.num_pose + self.num_object + self.num_person), \ "The shape of tensor class logits doesn't match total number of action classes." if not hasattr(self, "_proposals"): raise RuntimeError("sample_box needs to be called before") proposals = self._proposals labels = cat([proposal.get_field("labels") for proposal in proposals], dim=0) assert class_logits.shape[1] == labels.shape[1], \ "The shape of tensor class logits doesn't match the label tensor." loss_dict = {} if self.num_pose > 0: pose_label = labels[:, :self.num_pose].argmax(dim=1) pose_logits = class_logits[:, :self.num_pose] pose_loss = self.softmax_focal_loss(pose_logits, pose_label) / avg_box_num loss_dict["loss_pose_action"] = pose_loss interaction_label = labels[:, self.num_pose:].to(dtype=torch.float32) object_label = interaction_label[:, :self.num_object] person_label = interaction_label[:, self.num_object:] interaction_logits = class_logits[:, self.num_pose:] object_logits = interaction_logits[:, :self.num_object] person_logits = interaction_logits[:, self.num_object:] if self.num_object > 0: object_loss = self.sigmoid_focal_loss(object_logits, object_label).mean(dim=1).sum() / avg_box_num loss_dict["loss_object_interaction"] = object_loss if self.num_person > 0: person_loss = self.sigmoid_focal_loss(person_logits, person_label).mean(dim=1).sum() / avg_box_num loss_dict["loss_person_interaction"] = person_loss return loss_dict, self.weight_dict def make_roi_action_loss_evaluator(cfg): loss_evaluator = ActionLossComputation(cfg) return loss_evaluator
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/roi_heads/action_head/loss.py
from torch import nn from alphaction.modeling import registry @registry.ROI_ACTION_PREDICTORS.register("FCPredictor") class FCPredictor(nn.Module): def __init__(self, config, dim_in): super(FCPredictor, self).__init__() num_classes = config.MODEL.ROI_ACTION_HEAD.NUM_CLASSES dropout_rate = config.MODEL.ROI_ACTION_HEAD.DROPOUT_RATE if dropout_rate > 0: self.dropout = nn.Dropout(p=dropout_rate, inplace=True) self.cls_score = nn.Linear(dim_in, num_classes) nn.init.normal_(self.cls_score.weight, std=0.01) nn.init.constant_(self.cls_score.bias, 0) def forward(self, x): x = x.view(x.size(0), -1) if hasattr(self, "dropout"): x = self.dropout(x) scores = self.cls_score(x) return scores def c2_weight_mapping(self): return {"cls_score.weight": "pred_w", "cls_score.bias": "pred_b"} def make_roi_action_predictor(cfg, dim_in): func = registry.ROI_ACTION_PREDICTORS[cfg.MODEL.ROI_ACTION_HEAD.PREDICTOR] return func(cfg, dim_in)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/roi_heads/action_head/roi_action_predictors.py
import torch from torch import nn import torch.nn.functional as F from alphaction.structures.bounding_box import BoxList class PostProcessor(nn.Module): def __init__(self, pose_action_num): super(PostProcessor, self).__init__() self.pose_action_num = pose_action_num def forward(self, x, boxes): # boxes should be (#detections,4) # prob should be calculated in different way. class_logits, = x pose_action_prob = F.softmax(class_logits[:,:self.pose_action_num],-1) interaction_action_prob = torch.sigmoid(class_logits[:,self.pose_action_num:]) action_prob = torch.cat((pose_action_prob,interaction_action_prob),1) image_shapes = [box.size for box in boxes] boxes_per_image = [len(box) for box in boxes] box_tensors = [a.bbox for a in boxes] action_prob = action_prob.split(boxes_per_image, dim=0) results = [] for prob, boxes_per_image, image_shape in zip( action_prob, box_tensors, image_shapes ): boxlist = self.prepare_boxlist(boxes_per_image, prob, image_shape) results.append(boxlist) return results def prepare_boxlist(self, boxes, scores, image_shape): boxlist = BoxList(boxes, image_shape, mode="xyxy") boxlist.add_field("scores", scores) return boxlist def make_roi_action_post_processor(cfg): softmax_num = cfg.MODEL.ROI_ACTION_HEAD.NUM_PERSON_MOVEMENT_CLASSES postprocessor = PostProcessor(softmax_num) return postprocessor
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/roi_heads/action_head/inference.py
from __future__ import (absolute_import, division, print_function, unicode_literals) import math import torch import torch.nn as nn from alphaction.modeling import registry from alphaction.utils.IA_helper import has_memory, has_person, has_object class InteractionBlock(nn.Module): def __init__(self, dim_person, dim_other, dim_out, dim_inner, structure_config, max_others=20, temp_pos_len=-1, # configs for memory feature dropout=0.): super(InteractionBlock, self).__init__() self.dim_person = dim_person self.dim_other = dim_other self.dim_out = dim_out self.dim_inner = dim_inner self.max_others = max_others self.scale_value = dim_inner ** (-0.5) # config for temporal position, only used for memory feature, self.temp_pos_len = temp_pos_len bias = not structure_config.NO_BIAS init_std = structure_config.CONV_INIT_STD self.query = nn.Conv3d(dim_person, dim_inner, 1, bias) init_layer(self.query, init_std, bias) self.key = nn.Conv3d(dim_other, dim_inner, 1, bias) init_layer(self.key, init_std, bias) self.value = nn.Conv3d(dim_other, dim_inner, 1, bias) init_layer(self.value, init_std, bias) self.out = nn.Conv3d(dim_inner, dim_out, 1, bias) if structure_config.USE_ZERO_INIT_CONV: out_init = 0 else: out_init = init_std init_layer(self.out, out_init, bias) self.softmax = nn.Softmax(dim=2) self.relu = nn.ReLU() self.dropout = nn.Dropout(dropout) self.use_ln = structure_config.LAYER_NORM if dim_person != dim_out: self.shortcut = nn.Conv3d(dim_person, dim_out, 1, bias) init_layer(self.shortcut, init_std, bias) else: self.shortcut = None if self.temp_pos_len > 0: self.temporal_position_k = nn.Parameter(torch.zeros(temp_pos_len, 1, self.dim_inner, 1, 1, 1)) self.temporal_position_v = nn.Parameter(torch.zeros(temp_pos_len, 1, self.dim_inner, 1, 1, 1)) def forward(self, person, others): """ :param person: [n, channels, t, h, w] :param others: [n, num_other, channels, t, h, w] """ device = person.device n, dim_person, t, h, w = person.size() _, max_others, dim_others, t_others, h_others, w_others = others.size() query_batch = person key = fuse_batch_num(others) # [n*num_other, channels, t, h, w] query_batch = self.query(query_batch) key_batch = self.key(key).contiguous().view(n, self.max_others, self.dim_inner, t_others, h_others, w_others) value_batch = self.value(key).contiguous().view(n, self.max_others, self.dim_inner, t_others, h_others, w_others) if self.temp_pos_len > 0: max_person_per_sec = max_others // self.temp_pos_len key_batch = key_batch.contiguous().view(n, self.temp_pos_len, max_person_per_sec, self.dim_inner, t_others, h_others, w_others) key_batch = key_batch + self.temporal_position_k key_batch = key_batch.contiguous().view(n, self.max_others, self.dim_inner, t_others, h_others, w_others) value_batch = value_batch.contiguous().view(n, self.temp_pos_len, max_person_per_sec, self.dim_inner, t_others, h_others, w_others) value_batch = value_batch + self.temporal_position_v value_batch = value_batch.contiguous().view(n, self.max_others, self.dim_inner, t_others, h_others, w_others) query_batch = query_batch.contiguous().view(n, self.dim_inner, -1).transpose(1, 2) # [n, thw, dim_inner] key_batch = key_batch.contiguous().view(n, self.max_others, self.dim_inner, -1).transpose(2, 3) key_batch = key_batch.contiguous().view(n, self.max_others * t_others * h_others * w_others, -1).transpose( 1, 2) qk = torch.bmm(query_batch, key_batch) # n, thw, max_other * thw qk_sc = qk * self.scale_value weight = self.softmax(qk_sc) value_batch = value_batch.contiguous().view(n, self.max_others, self.dim_inner, -1).transpose(2, 3) value_batch = value_batch.contiguous().view(n, self.max_others * t_others * h_others * w_others, -1) out = torch.bmm(weight, value_batch) # n, thw, dim_inner out = out.contiguous().view(n, t * h * w, -1) out = out.transpose(1, 2) out = out.contiguous().view(n, self.dim_inner, t, h, w) if self.use_ln: if not hasattr(self, "layer_norm"): self.layer_norm = nn.LayerNorm([self.dim_inner, t, h, w], elementwise_affine=False).to( device) out = self.layer_norm(out) out = self.relu(out) out = self.out(out) out = self.dropout(out) if self.shortcut: person = self.shortcut(person) out = out + person return out class IAStructure(nn.Module): def __init__(self, dim_person, dim_mem, dim_out, structure_cfg): super(IAStructure, self).__init__() self.dim_person = dim_person self.dim_others = dim_mem self.dim_inner = structure_cfg.DIM_INNER self.dim_out = dim_out self.max_person = structure_cfg.MAX_PERSON self.max_object = structure_cfg.MAX_OBJECT self.mem_len = structure_cfg.LENGTH[0] + structure_cfg.LENGTH[1] + 1 self.mem_feature_len = self.mem_len * structure_cfg.MAX_PER_SEC self.I_block_list = structure_cfg.I_BLOCK_LIST bias = not structure_cfg.NO_BIAS conv_init_std = structure_cfg.CONV_INIT_STD self.has_P = has_person(structure_cfg) self.has_O = has_object(structure_cfg) self.has_M = has_memory(structure_cfg) self.person_dim_reduce = nn.Conv3d(dim_person, self.dim_inner, 1, bias) # reduce person query init_layer(self.person_dim_reduce, conv_init_std, bias) self.reduce_dropout = nn.Dropout(structure_cfg.DROPOUT) if self.has_M: self.mem_dim_reduce = nn.Conv3d(dim_mem, self.dim_inner, 1, bias) init_layer(self.mem_dim_reduce, conv_init_std, bias) if self.has_P: self.person_key_dim_reduce = nn.Conv3d(dim_person, self.dim_inner, 1, bias) # reduce person key init_layer(self.person_key_dim_reduce, conv_init_std, bias) if self.has_O: self.object_dim_reduce = nn.Conv3d(dim_person, self.dim_inner, 1, bias) init_layer(self.object_dim_reduce, conv_init_std, bias) def forward(self, person, person_boxes, obj_feature, object_boxes, mem_feature, ): query, person_key, object_key, mem_key = self._reduce_dim(person, person_boxes, obj_feature, object_boxes, mem_feature) return self._aggregate(person_boxes, query, person_key, object_key, mem_key) def _reduce_dim(self, person, person_boxes, obj_feature, object_boxes, mem_feature): query = self.person_dim_reduce(person) query = self.reduce_dropout(query) n = query.size(0) if self.has_P: person_key = self.person_key_dim_reduce(person) person_key = self.reduce_dropout(person_key) else: person_key = None if self.has_O: object_key = separate_roi_per_person(person_boxes, obj_feature, object_boxes, self.max_object) object_key = fuse_batch_num(object_key) object_key = self.object_dim_reduce(object_key) object_key = unfuse_batch_num(object_key, n, self.max_object) object_key = self.reduce_dropout(object_key) else: object_key = None if self.has_M: mem_key = separate_batch_per_person(person_boxes, mem_feature) mem_key = fuse_batch_num(mem_key) mem_key = self.mem_dim_reduce(mem_key) mem_key = unfuse_batch_num(mem_key, n, self.mem_feature_len) mem_key = self.reduce_dropout(mem_key) else: mem_key = None return query, person_key, object_key, mem_key def _aggregate(self, proposals, query, person_key, object_key, mem_key): raise NotImplementedError def _make_interaction_block(self, block_type, block_name, dim_person, dim_other, dim_out, dim_inner, structure_cfg): dropout = structure_cfg.DROPOUT temp_pos_len = -1 if block_type == "P": max_others = self.max_person elif block_type == "O": max_others = self.max_object elif block_type == "M": max_others = self.mem_feature_len if structure_cfg.TEMPORAL_POSITION: temp_pos_len = self.mem_len else: raise KeyError("Unrecognized interaction block type '{}'!".format(block_type)) I_block = InteractionBlock(dim_person, dim_other, dim_out, dim_inner, structure_cfg, max_others, temp_pos_len=temp_pos_len, dropout=dropout) self.add_module(block_name, I_block) @registry.INTERACTION_AGGREGATION_STRUCTURES.register("serial") class SerialIAStructure(IAStructure): def __init__(self, dim_person, dim_mem, dim_out, structure_cfg): super(SerialIAStructure, self).__init__(dim_person, dim_mem, dim_out, structure_cfg) block_count = dict() for idx, block_type in enumerate(self.I_block_list): block_count[block_type] = block_count.get(block_type, 0) + 1 name = block_type + "_block_{}".format(block_count[block_type]) dim_out_trans = dim_out if (idx == len(self.I_block_list) - 1) else self.dim_inner self._make_interaction_block(block_type, name, self.dim_inner, self.dim_inner, dim_out_trans, self.dim_inner, structure_cfg) def _aggregate(self, person_boxes, query, person_key, object_key, mem_key): block_count = dict() for idx, block_type in enumerate(self.I_block_list): block_count[block_type] = block_count.get(block_type, 0) + 1 name = block_type + "_block_{}".format(block_count[block_type]) I_block = getattr(self, name) if block_type == "P": person_key = separate_roi_per_person(person_boxes, person_key, person_boxes, self.max_person, ) query = I_block(query, person_key) elif block_type == "O": query = I_block(query, object_key) elif block_type == "M": query = I_block(query, mem_key) person_key = query return query @registry.INTERACTION_AGGREGATION_STRUCTURES.register("dense") class DenseSerialIAStructure(SerialIAStructure): def __init__(self, dim_person, dim_mem, dim_out, structure_cfg): super(DenseSerialIAStructure, self).__init__(dim_person, dim_mem, dim_out, structure_cfg) self.dense_params = nn.ParameterDict() for idx in range(2, len(self.I_block_list)): init_tensor = torch.full((idx, self.dim_inner, 1, 1, 1), -5 * math.log(10), dtype=torch.float32) init_tensor[idx - 1, ...] = 0 self.dense_params[str(idx)] = nn.Parameter(init_tensor) self.softmax = nn.Softmax(dim=0) def _aggregate(self, person_boxes, query, person_key, object_key, mem_key): block_count = dict() history_query = [] for idx, block_type in enumerate(self.I_block_list): block_count[block_type] = block_count.get(block_type, 0) + 1 name = block_type + "_block_{}".format(block_count[block_type]) I_block = getattr(self, name) if idx >= 2: query = torch.stack(history_query, dim=1) # [n, prev_idx, c, T, H, W] query = torch.sum(query * self.softmax(self.dense_params[str(idx)]), dim=1) person_key = query if block_type == "P": person_key = separate_roi_per_person(person_boxes, person_key, person_boxes, self.max_person, ) query = I_block(query, person_key) elif block_type == "O": query = I_block(query, object_key) elif block_type == "M": query = I_block(query, mem_key) person_key = query history_query.append(query) return query @registry.INTERACTION_AGGREGATION_STRUCTURES.register("parallel") class ParallelIAStructure(IAStructure): def __init__(self, dim_person, dim_others, dim_out, structure_cfg): super(ParallelIAStructure, self).__init__(dim_person, dim_others, dim_out, structure_cfg) block_count = dict() for layer_idx, layer in enumerate(self.I_block_list): for block_type in layer: block_count[block_type] = block_count.get(block_type, 0) + 1 name = block_type + "_block_{}".format(block_count[block_type]) dim_out_trans = dim_out if (layer_idx == len(self.I_block_list) - 1) else self.dim_inner self._make_interaction_block(block_type, name, self.dim_inner, self.dim_inner, dim_out_trans, self.dim_inner, structure_cfg) def _aggregate(self, person_boxes, query, person_key, object_key, mem_key): block_count = dict() last_query = [query, ] * len(self.I_block_list[0]) for layer_idx, layer in enumerate(self.I_block_list): new_query = [] for idx, block_type in enumerate(layer): block_count[block_type] = block_count.get(block_type, 0) + 1 name = block_type + "_block_{}".format(block_count[block_type]) I_block = getattr(self, name) query = last_query[idx] if block_type == "P": person_key = separate_roi_per_person(person_boxes, person_key, person_boxes, self.max_person) query = I_block(query, person_key) person_key = query elif block_type == "O": query = I_block(query, object_key) elif block_type == "M": query = I_block(query, mem_key) new_query.append(query) last_query = new_query query = torch.sum(torch.stack(last_query), 0) return query def separate_roi_per_person(proposals, things, other_proposals, max_things): """ :param things: [n2, c, t, h, w] :param proposals: :param max_things: :return [n, max_other, c, t, h, w] """ res = [] _, c, t, h, w = things.size() device = things.device index = 0 for i, (person_box, other_box) in enumerate(zip(proposals, other_proposals)): person_num = len(person_box) other_num = len(other_box) tmp = torch.zeros((person_num, max_things, c, t, h, w), device=device) if other_num > max_things: idx = torch.randperm(other_num)[:max_things] tmp[:, :max_things] = things[index:index + other_num][idx] else: tmp[:, :other_num] = things[index:index + other_num] res.append(tmp) index += other_num features = torch.cat(res, dim=0) return features def separate_batch_per_person(proposals, things): """ :param things: [b, max_others, c, t, h, w] :return [n, max_others, c, t, h, w] """ res = [] b, max_others, c, t, h, w = things.size() device = things.device for i, person_box in enumerate(proposals): person_num = len(person_box) tmp = torch.zeros((person_num, max_others, c, t, h, w), device=device) tmp[:] = things[i] res.append(tmp) return torch.cat(res, dim=0) def fuse_batch_num(things): n, number, c, t, h, w = things.size() return things.contiguous().view(-1, c, t, h, w) def unfuse_batch_num(things, batch_size, num): assert things.size(0) == batch_size * num, "dimension should matches" _, c, t, h, w = things.size() return things.contiguous().view(batch_size, num, c, t, h, w) def init_layer(layer, init_std, bias): if init_std == 0: nn.init.constant_(layer.weight, 0) else: nn.init.normal_(layer.weight, std=init_std) if bias: nn.init.constant_(layer.bias, 0) def make_ia_structure(cfg, dim_in): func = registry.INTERACTION_AGGREGATION_STRUCTURES[ cfg.MODEL.IA_STRUCTURE.STRUCTURE ] return func(dim_in, cfg.MODEL.IA_STRUCTURE.DIM_IN, cfg.MODEL.IA_STRUCTURE.DIM_OUT, cfg.MODEL.IA_STRUCTURE)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/roi_heads/action_head/IA_structure.py
from alphaction.modeling import registry from . import slowfast, i3d @registry.BACKBONES.register("Slowfast-Resnet50") @registry.BACKBONES.register("Slowfast-Resnet101") def build_slowfast_resnet_backbone(cfg): model = slowfast.SlowFast(cfg) return model @registry.BACKBONES.register("I3D-Resnet50") @registry.BACKBONES.register("I3D-Resnet101") @registry.BACKBONES.register("I3D-Resnet50-Sparse") @registry.BACKBONES.register("I3D-Resnet101-Sparse") def build_i3d_resnet_backbone(cfg): model = i3d.I3D(cfg) return model def build_backbone(cfg): assert cfg.MODEL.BACKBONE.CONV_BODY in registry.BACKBONES, \ "cfg.MODEL.BACKBONE.CONV_BODY: {} are not registered in registry".format( cfg.MODEL.BACKBONE.CONV_BODY ) return registry.BACKBONES[cfg.MODEL.BACKBONE.CONV_BODY](cfg)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/backbone/backbone.py
from .backbone import build_backbone
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/backbone/__init__.py
from __future__ import (absolute_import, division, print_function, unicode_literals) import torch import torch.nn as nn from alphaction.modeling.common_blocks import ResNLBlock from alphaction.layers import FrozenBatchNorm3d def get_slow_model_cfg(cfg): backbone_strs = cfg.MODEL.BACKBONE.CONV_BODY.split('-')[1:] error_msg = 'Model backbone {} is not supported.'.format(cfg.MODEL.BACKBONE.CONV_BODY) use_temp_convs_1 = [0] temp_strides_1 = [1] max_pool_stride_1 = 1 use_temp_convs_2 = [0, 0, 0] temp_strides_2 = [1, 1, 1] use_temp_convs_3 = [0, 0, 0, 0] temp_strides_3 = [1, 1, 1, 1] use_temp_convs_5 = [1, 1, 1] temp_strides_5 = [1, 1, 1] slow_stride = cfg.INPUT.TAU avg_pool_stride = int(cfg.INPUT.FRAME_NUM / slow_stride) if backbone_strs[0] == 'Resnet50': block_config = (3, 4, 6, 3) use_temp_convs_4 = [1, 1, 1, 1, 1, 1] temp_strides_4 = [1, 1, 1, 1, 1, 1] elif backbone_strs[0] == 'Resnet101': block_config = (3, 4, 23, 3) use_temp_convs_4 = [1, ] * 23 temp_strides_4 = [1, ] * 23 else: raise KeyError(error_msg) if len(backbone_strs) > 1: raise KeyError(error_msg) use_temp_convs_set = [use_temp_convs_1, use_temp_convs_2, use_temp_convs_3, use_temp_convs_4, use_temp_convs_5] temp_strides_set = [temp_strides_1, temp_strides_2, temp_strides_3, temp_strides_4, temp_strides_5] pool_strides_set = [max_pool_stride_1, avg_pool_stride] return block_config, use_temp_convs_set, temp_strides_set, pool_strides_set def get_fast_model_cfg(cfg): backbone_strs = cfg.MODEL.BACKBONE.CONV_BODY.split('-')[1:] error_msg = 'Model backbone {} is not supported.'.format(cfg.MODEL.BACKBONE.CONV_BODY) use_temp_convs_1 = [2] temp_strides_1 = [1] max_pool_stride_1 = 1 use_temp_convs_2 = [1, 1, 1] temp_strides_2 = [1, 1, 1] use_temp_convs_3 = [1, 1, 1, 1] temp_strides_3 = [1, 1, 1, 1] use_temp_convs_5 = [1, 1, 1] temp_strides_5 = [1, 1, 1] fast_stride = cfg.INPUT.TAU // cfg.INPUT.ALPHA avg_pool_stride = int(cfg.INPUT.FRAME_NUM / fast_stride) if backbone_strs[0] == 'Resnet50': block_config = (3, 4, 6, 3) use_temp_convs_4 = [1, 1, 1, 1, 1, 1] temp_strides_4 = [1, 1, 1, 1, 1, 1] elif backbone_strs[0] == 'Resnet101': block_config = (3, 4, 23, 3) use_temp_convs_4 = [1, ] * 23 temp_strides_4 = [1, ] * 23 else: raise KeyError(error_msg) if len(backbone_strs) > 1: raise KeyError(error_msg) use_temp_convs_set = [use_temp_convs_1, use_temp_convs_2, use_temp_convs_3, use_temp_convs_4, use_temp_convs_5] temp_strides_set = [temp_strides_1, temp_strides_2, temp_strides_3, temp_strides_4, temp_strides_5] pool_strides_set = [max_pool_stride_1, avg_pool_stride] return block_config, use_temp_convs_set, temp_strides_set, pool_strides_set class LateralBlock(nn.Module): def __init__(self, conv_dim, alpha): super(LateralBlock, self).__init__() self.conv = nn.Conv3d(conv_dim, conv_dim * 2, kernel_size=(5, 1, 1), stride=(alpha, 1, 1), padding=(2, 0, 0), bias=True) nn.init.kaiming_normal_(self.conv.weight) nn.init.constant_(self.conv.bias, 0.0) def forward(self, x): out = self.conv(x) return out class FastPath(nn.Module): def __init__(self, cfg): super(FastPath, self).__init__() self.cfg = cfg.clone() block_config, use_temp_convs_set, temp_strides_set, pool_strides_set = get_fast_model_cfg(cfg) conv3_nonlocal = cfg.MODEL.BACKBONE.SLOWFAST.FAST.CONV3_NONLOCAL conv4_nonlocal = cfg.MODEL.BACKBONE.SLOWFAST.FAST.CONV4_NONLOCAL dim_inner = 8 conv_dims = [8, 32, 64, 128, 256] self.dim_out = conv_dims[-1] n1, n2, n3, n4 = block_config layer_mod = 2 conv3_nl_mod = layer_mod conv4_nl_mod = layer_mod if not conv3_nonlocal: conv3_nl_mod = 1000 if not conv4_nonlocal: conv4_nl_mod = 1000 self.c2_mapping = None self.conv1 = nn.Conv3d(3, conv_dims[0], (1 + use_temp_convs_set[0][0] * 2, 7, 7), stride=(temp_strides_set[0][0], 2, 2), padding=(use_temp_convs_set[0][0], 3, 3), bias=False) nn.init.kaiming_normal_(self.conv1.weight) if cfg.MODEL.BACKBONE.FROZEN_BN: self.bn1 = FrozenBatchNorm3d(conv_dims[0], eps=cfg.MODEL.BACKBONE.BN_EPSILON) nn.init.constant_(self.bn1.weight, 1.0) nn.init.constant_(self.bn1.bias, 0.0) else: self.bn1 = nn.BatchNorm3d(conv_dims[0], eps=cfg.MODEL.BACKBONE.BN_EPSILON, momentum=cfg.MODEL.BACKBONE.BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.maxpool1 = nn.MaxPool3d((pool_strides_set[0], 3, 3), stride=(pool_strides_set[0], 2, 2)) self.res_nl1 = ResNLBlock(cfg, conv_dims[0], conv_dims[1], stride=1, num_blocks=n1, dim_inner=dim_inner, use_temp_convs=use_temp_convs_set[1], temp_strides=temp_strides_set[1]) self.res_nl2 = ResNLBlock(cfg, conv_dims[1], conv_dims[2], stride=2, num_blocks=n2, dim_inner=dim_inner * 2, use_temp_convs=use_temp_convs_set[2], temp_strides=temp_strides_set[2], nonlocal_mod=conv3_nl_mod, group_nonlocal=cfg.MODEL.BACKBONE.SLOWFAST.FAST.CONV3_GROUP_NL) self.res_nl3 = ResNLBlock(cfg, conv_dims[2], conv_dims[3], stride=2, num_blocks=n3, dim_inner=dim_inner * 4, use_temp_convs=use_temp_convs_set[3], temp_strides=temp_strides_set[3], nonlocal_mod=conv4_nl_mod) self.res_nl4 = ResNLBlock(cfg, conv_dims[3], conv_dims[4], stride=1, num_blocks=n4, dim_inner=dim_inner * 8, use_temp_convs=use_temp_convs_set[4], temp_strides=temp_strides_set[4], dilation=2) if cfg.MODEL.BACKBONE.SLOWFAST.LATERAL == 'tconv': self._tconv(conv_dims) def _tconv(self, conv_dims): alpha = self.cfg.INPUT.ALPHA self.Tconv1 = LateralBlock(conv_dims[0], alpha) self.Tconv2 = LateralBlock(conv_dims[1], alpha) self.Tconv3 = LateralBlock(conv_dims[2], alpha) self.Tconv4 = LateralBlock(conv_dims[3], alpha) def forward(self, x): out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.maxpool1(out) tconv1 = self.Tconv1(out) out = self.res_nl1(out) tconv2 = self.Tconv2(out) out = self.res_nl2(out) tconv3 = self.Tconv3(out) out = self.res_nl3(out) tconv4 = self.Tconv4(out) out = self.res_nl4(out) return out, [tconv1, tconv2, tconv3, tconv4] class SlowPath(nn.Module): def __init__(self, cfg): super(SlowPath, self).__init__() self.cfg = cfg.clone() block_config, use_temp_convs_set, temp_strides_set, pool_strides_set = get_slow_model_cfg(cfg) conv3_nonlocal = cfg.MODEL.BACKBONE.SLOWFAST.SLOW.CONV3_NONLOCAL conv4_nonlocal = cfg.MODEL.BACKBONE.SLOWFAST.SLOW.CONV4_NONLOCAL dim_inner = 64 conv_dims = [64, 256, 512, 1024, 2048] self.dim_out = conv_dims[-1] n1, n2, n3, n4 = block_config layer_mod = 2 conv3_nl_mod = layer_mod conv4_nl_mod = layer_mod if not conv3_nonlocal: conv3_nl_mod = 1000 if not conv4_nonlocal: conv4_nl_mod = 1000 self.c2_mapping = None self.conv1 = nn.Conv3d(3, conv_dims[0], (1 + use_temp_convs_set[0][0] * 2, 7, 7), stride=(temp_strides_set[0][0], 2, 2), padding=(use_temp_convs_set[0][0], 3, 3), bias=False) nn.init.kaiming_normal_(self.conv1.weight) if cfg.MODEL.BACKBONE.FROZEN_BN: self.bn1 = FrozenBatchNorm3d(conv_dims[0], eps=cfg.MODEL.BACKBONE.BN_EPSILON) nn.init.constant_(self.bn1.weight, 1.0) nn.init.constant_(self.bn1.bias, 0.0) else: self.bn1 = nn.BatchNorm3d(conv_dims[0], eps=cfg.MODEL.BACKBONE.BN_EPSILON, momentum=cfg.MODEL.BACKBONE.BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.maxpool1 = nn.MaxPool3d((pool_strides_set[0], 3, 3), stride=(pool_strides_set[0], 2, 2)) self.res_nl1 = ResNLBlock(cfg, conv_dims[0], conv_dims[1], stride=1, num_blocks=n1, dim_inner=dim_inner, use_temp_convs=use_temp_convs_set[1], temp_strides=temp_strides_set[1], lateral=cfg.MODEL.BACKBONE.SLOWFAST.FAST.ACTIVE) self.res_nl2 = ResNLBlock(cfg, conv_dims[1], conv_dims[2], stride=2, num_blocks=n2, dim_inner=dim_inner * 2, use_temp_convs=use_temp_convs_set[2], temp_strides=temp_strides_set[2], nonlocal_mod=conv3_nl_mod, group_nonlocal=cfg.MODEL.BACKBONE.SLOWFAST.SLOW.CONV3_GROUP_NL, lateral=cfg.MODEL.BACKBONE.SLOWFAST.FAST.ACTIVE) self.res_nl3 = ResNLBlock(cfg, conv_dims[2], conv_dims[3], stride=2, num_blocks=n3, dim_inner=dim_inner * 4, use_temp_convs=use_temp_convs_set[3], temp_strides=temp_strides_set[3], nonlocal_mod=conv4_nl_mod, lateral=cfg.MODEL.BACKBONE.SLOWFAST.FAST.ACTIVE) self.res_nl4 = ResNLBlock(cfg, conv_dims[3], conv_dims[4], stride=1, num_blocks=n4, dim_inner=dim_inner * 8, use_temp_convs=use_temp_convs_set[4], temp_strides=temp_strides_set[4], lateral=cfg.MODEL.BACKBONE.SLOWFAST.FAST.ACTIVE, dilation=2) def forward(self, x, lateral_connection=None): out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.maxpool1(out) if lateral_connection: out = torch.cat([out, lateral_connection[0]], dim=1) out = self.res_nl1(out) if lateral_connection: out = torch.cat([out, lateral_connection[1]], dim=1) out = self.res_nl2(out) if lateral_connection: out = torch.cat([out, lateral_connection[2]], dim=1) out = self.res_nl3(out) if lateral_connection: out = torch.cat([out, lateral_connection[3]], dim=1) out = self.res_nl4(out) return out class SlowFast(nn.Module): def __init__(self, cfg): super(SlowFast, self).__init__() self.cfg = cfg.clone() if cfg.MODEL.BACKBONE.SLOWFAST.SLOW.ACTIVE: self.slow = SlowPath(cfg) if cfg.MODEL.BACKBONE.SLOWFAST.FAST.ACTIVE: self.fast = FastPath(cfg) if cfg.MODEL.BACKBONE.SLOWFAST.SLOW.ACTIVE and cfg.MODEL.BACKBONE.SLOWFAST.FAST.ACTIVE: self.dim_out = self.slow.dim_out + self.fast.dim_out elif cfg.MODEL.BACKBONE.SLOWFAST.SLOW.ACTIVE: self.dim_out = self.slow.dim_out elif cfg.MODEL.BACKBONE.SLOWFAST.FAST.ACTIVE: self.dim_out = self.fast.dim_out def forward(self, slow_x, fast_x): tconv = None cfg = self.cfg slowout = None fastout = None if cfg.MODEL.BACKBONE.SLOWFAST.FAST.ACTIVE: fastout, tconv = self.fast(fast_x) if cfg.MODEL.BACKBONE.SLOWFAST.SLOW.ACTIVE: slowout = self.slow(slow_x, tconv) return slowout, fastout
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/backbone/slowfast.py
from __future__ import (absolute_import, division, print_function, unicode_literals) import torch.nn as nn from alphaction.layers import FrozenBatchNorm3d from alphaction.modeling.common_blocks import ResNLBlock def get_model_cfg(cfg): backbone_strs = cfg.MODEL.BACKBONE.CONV_BODY.split('-')[1:] error_msg = 'Model backbone {} is not supported.'.format(cfg.MODEL.BACKBONE.CONV_BODY) use_temp_convs_1 = [2] temp_strides_1 = [2] max_pool_stride_1 = 2 use_temp_convs_2 = [1, 1, 1] temp_strides_2 = [1, 1, 1] max_pool_stride_2 = 2 use_temp_convs_3 = [1, 0, 1, 0] temp_strides_3 = [1, 1, 1, 1] use_temp_convs_5 = [0, 1, 0] temp_strides_5 = [1, 1, 1] avg_pool_stride = int(cfg.INPUT.FRAME_NUM / 8) if backbone_strs[0] == 'Resnet50': block_config = (3, 4, 6, 3) use_temp_convs_4 = [1, 0, 1, 0, 1, 0] temp_strides_4 = [1, 1, 1, 1, 1, 1] elif backbone_strs[0] == 'Resnet101': block_config = (3, 4, 23, 3) use_temp_convs_4 = [] for i in range(23): if i % 2 == 0: use_temp_convs_4.append(1) else: use_temp_convs_4.append(0) temp_strides_4 = [1, ] * 23 else: raise KeyError(error_msg) if len(backbone_strs) > 1: if len(backbone_strs) == 2 and backbone_strs[1] == 'Sparse': temp_strides_1 = [1] max_pool_stride_1 = 1 avg_pool_stride = int(cfg.INPUT.FRAME_NUM / 2) else: raise KeyError(error_msg) use_temp_convs_set = [use_temp_convs_1, use_temp_convs_2, use_temp_convs_3, use_temp_convs_4, use_temp_convs_5] temp_strides_set = [temp_strides_1, temp_strides_2, temp_strides_3, temp_strides_4, temp_strides_5] pool_strides_set = [max_pool_stride_1, max_pool_stride_2, avg_pool_stride] return block_config, use_temp_convs_set, temp_strides_set, pool_strides_set class I3D(nn.Module): def __init__(self, cfg): super(I3D, self).__init__() self.cfg = cfg.clone() block_config, use_temp_convs_set, temp_strides_set, pool_strides_set = get_model_cfg(cfg) conv3_nonlocal = cfg.MODEL.BACKBONE.I3D.CONV3_NONLOCAL conv4_nonlocal = cfg.MODEL.BACKBONE.I3D.CONV4_NONLOCAL dim_inner = 64 conv_dims = [64, 256, 512, 1024, 2048] self.dim_out = conv_dims[-1] n1, n2, n3, n4 = block_config layer_mod = 2 conv3_nl_mod = layer_mod conv4_nl_mod = layer_mod if not conv3_nonlocal: conv3_nl_mod = 1000 if not conv4_nonlocal: conv4_nl_mod = 1000 self.c2_mapping = None data_dim = 3 self.conv1 = nn.Conv3d(data_dim, conv_dims[0], (1 + use_temp_convs_set[0][0] * 2, 7, 7), stride=(temp_strides_set[0][0], 2, 2), padding=(use_temp_convs_set[0][0], 3, 3), bias=False) nn.init.kaiming_normal_(self.conv1.weight) if cfg.MODEL.BACKBONE.FROZEN_BN: self.bn1 = FrozenBatchNorm3d(conv_dims[0], eps=cfg.MODEL.BACKBONE.BN_EPSILON) nn.init.constant_(self.bn1.weight, 1.0) nn.init.constant_(self.bn1.bias, 0.0) else: self.bn1 = nn.BatchNorm3d(conv_dims[0], eps=cfg.MODEL.BACKBONE.BN_EPSILON, momentum=cfg.MODEL.BACKBONE.BN_MOMENTUM) self.relu = nn.ReLU(inplace=True) self.maxpool1 = nn.MaxPool3d((pool_strides_set[0], 3, 3), stride=(pool_strides_set[0], 2, 2)) self.res_nl1 = ResNLBlock(cfg, conv_dims[0], conv_dims[1], stride=1, num_blocks=n1, dim_inner=dim_inner, use_temp_convs=use_temp_convs_set[1], temp_strides=temp_strides_set[1]) self.maxpool2 = nn.MaxPool3d((pool_strides_set[1], 1, 1), stride=(pool_strides_set[1], 1, 1)) self.res_nl2 = ResNLBlock(cfg, conv_dims[1], conv_dims[2], stride=2, num_blocks=n2, dim_inner=dim_inner * 2, use_temp_convs=use_temp_convs_set[2], temp_strides=temp_strides_set[2], nonlocal_mod=conv3_nl_mod, group_nonlocal=cfg.MODEL.BACKBONE.I3D.CONV3_GROUP_NL) self.res_nl3 = ResNLBlock(cfg, conv_dims[2], conv_dims[3], stride=2, num_blocks=n3, dim_inner=dim_inner * 4, use_temp_convs=use_temp_convs_set[3], temp_strides=temp_strides_set[3], nonlocal_mod=conv4_nl_mod) self.res_nl4 = ResNLBlock(cfg, conv_dims[3], conv_dims[4], stride=1, num_blocks=n4, dim_inner=dim_inner * 8, use_temp_convs=use_temp_convs_set[4], temp_strides=temp_strides_set[4], dilation=2) def forward(self, _, x): # We only use fast videos, which is the second input. out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.maxpool1(out) out = self.res_nl1(out) out = self.maxpool2(out) out = self.res_nl2(out) out = self.res_nl3(out) out = self.res_nl4(out) return None, out def c2_weight_mapping(self): if self.c2_mapping is None: weight_map = {'conv1.weight': 'conv1_w', 'bn1.weight': 'res_conv1_bn_s', 'bn1.bias': 'res_conv1_bn_b', 'bn1.running_mean': 'res_conv1_bn_rm', 'bn1.running_var': 'res_conv1_bn_riv'} for i in range(1, 5): name = 'res_nl{}'.format(i) child_map = getattr(self, name).c2_weight_mapping() for key, val in child_map.items(): new_key = name + '.' + key weight_map[new_key] = val.format(i + 1) self.c2_mapping = weight_map return self.c2_mapping
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/modeling/backbone/i3d.py
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/engine/__init__.py
# modified from https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/engine/inference.py import logging import os import torch from tqdm import tqdm import time import datetime from alphaction.dataset.datasets.evaluation import evaluate from alphaction.utils.comm import get_rank, is_main_process, all_gather, gather, synchronize, get_world_size from alphaction.structures.memory_pool import MemoryPool def compute_on_dataset_1stage(model, data_loader, device): # single stage inference, for model without memory features cpu_device = torch.device("cpu") results_dict = {} if get_world_size() == 1: extra_args = {} else: rank = get_rank() extra_args = dict(desc="rank {}".format(rank)) for batch in tqdm(data_loader, **extra_args): slow_clips, fast_clips, boxes, objects, extras, video_ids = batch slow_clips = slow_clips.to(device) fast_clips = fast_clips.to(device) boxes = [box.to(device) for box in boxes] objects = [None if (box is None) else box.to(device) for box in objects] with torch.no_grad(): output = model(slow_clips, fast_clips, boxes, objects, extras) output = [o.to(cpu_device) for o in output] results_dict.update( {video_id: result for video_id, result in zip(video_ids, output)} ) return results_dict def compute_on_dataset_2stage(model, data_loader, device, logger): # two stage inference, for model with memory features. # first extract features and then do the inference cpu_device = torch.device("cpu") num_devices = get_world_size() dataset = data_loader.dataset if num_devices == 1: extra_args = {} else: rank = get_rank() extra_args = dict(desc="rank {}".format(rank)) loader_len = len(data_loader) person_feature_pool = MemoryPool() batch_info_list = [None]*loader_len logger.info("Stage 1: extracting clip features.") start_time = time.time() for i, batch in enumerate(tqdm(data_loader, **extra_args)): slow_clips, fast_clips, boxes, objects, extras, video_ids = batch slow_clips = slow_clips.to(device) fast_clips = fast_clips.to(device) boxes = [box.to(device) for box in boxes] objects = [None if (box is None) else box.to(device) for box in objects] movie_ids = [e["movie_id"] for e in extras] timestamps = [e["timestamp"] for e in extras] with torch.no_grad(): feature = model(slow_clips, fast_clips, boxes, objects, part_forward=0) person_feature = [ft.to(cpu_device) for ft in feature[0]] object_feature = [ft.to(cpu_device) for ft in feature[1]] # store person features into memory pool for movie_id, timestamp, p_ft, o_ft in zip(movie_ids, timestamps, person_feature, object_feature): person_feature_pool[movie_id, timestamp] = p_ft # store other information in list, for further inference batch_info_list[i] = (movie_ids, timestamps, video_ids, object_feature) # gather feature pools from different ranks synchronize() total_time = time.time() - start_time total_time_str = str(datetime.timedelta(seconds=total_time)) logger.info( "Stage 1 time: {} ({} s / video per device, on {} devices)".format( total_time_str, total_time * num_devices / len(dataset), num_devices ) ) feature_pool = all_gather(person_feature_pool) all_feature_pool_p = MemoryPool() all_feature_pool_p.update_list(feature_pool) del feature_pool, person_feature_pool # do the inference results_dict = {} logger.info("Stage 2: predicting with extracted feature.") start_time = time.time() for movie_ids, timestamps, video_ids, object_feature in tqdm(batch_info_list, **extra_args): current_feat_p = [all_feature_pool_p[movie_id, timestamp].to(device) for movie_id, timestamp in zip(movie_ids, timestamps)] current_feat_o = [ft_o.to(device) for ft_o in object_feature] extras = dict( person_pool=all_feature_pool_p, movie_ids=movie_ids, timestamps=timestamps, current_feat_p=current_feat_p, current_feat_o=current_feat_o, ) with torch.no_grad(): output = model(None, None, None, None, extras=extras, part_forward=1) output = [o.to(cpu_device) for o in output] results_dict.update( {video_id: result for video_id, result in zip(video_ids, output)} ) synchronize() total_time = time.time() - start_time total_time_str = str(datetime.timedelta(seconds=total_time)) logger.info( "Stage 2 time: {} ({} s / video per device, on {} devices)".format( total_time_str, total_time * num_devices / len(dataset), num_devices ) ) return results_dict def compute_on_dataset(model, data_loader, device, logger, mem_active): model.eval() if mem_active: results_dict = compute_on_dataset_2stage(model, data_loader, device, logger) else: results_dict = compute_on_dataset_1stage(model, data_loader, device) return results_dict def _accumulate_predictions_from_multiple_gpus(predictions_per_gpu): all_predictions = gather(predictions_per_gpu) if not is_main_process(): return # merge the list of dicts predictions = {} for p in all_predictions: predictions.update(p) # convert a dict where the key is the index in a list video_ids = list(sorted(predictions.keys())) if len(video_ids) != video_ids[-1] + 1: logger = logging.getLogger("alphaction.inference") logger.warning( "Number of videos that were gathered from multiple processes is not " "a contiguous set. Some images might be missing from the evaluation" ) # convert to a list predictions = [predictions[i] for i in video_ids] return predictions def inference( model, data_loader, dataset_name, mem_active=False, output_folder=None, ): # convert to a torch.device for efficiency device = torch.device("cuda") num_devices = get_world_size() logger = logging.getLogger("alphaction.inference") dataset = data_loader.dataset logger.info("Start evaluation on {} dataset({} videos).".format(dataset_name, len(dataset))) start_time = time.time() predictions = compute_on_dataset(model, data_loader, device, logger, mem_active) # wait for all processes to complete before measuring the time synchronize() total_time = time.time() - start_time total_time_str = str(datetime.timedelta(seconds=total_time)) logger.info( "Total inference time: {} ({} s / video per device, on {} devices)".format( total_time_str, total_time * num_devices / len(dataset), num_devices ) ) predictions = _accumulate_predictions_from_multiple_gpus(predictions) if not is_main_process(): return if output_folder: torch.save(predictions, os.path.join(output_folder, "predictions.pth")) return evaluate( dataset=dataset, predictions=predictions, output_folder=output_folder, )
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/engine/inference.py
import datetime import logging import time import torch from alphaction.utils.metric_logger import MetricLogger from alphaction.engine.inference import inference from alphaction.utils.comm import synchronize, reduce_dict, all_gather from alphaction.structures.memory_pool import MemoryPool def do_train( model, data_loader, optimizer, scheduler, checkpointer, device, checkpoint_period, arguments, tblogger, val_period, dataset_names_val, data_loaders_val, distributed, mem_active, ): logger = logging.getLogger("alphaction.trainer") logger.info("Start training") meters = MetricLogger(delimiter=" ") max_iter = len(data_loader) start_iter = arguments["iteration"] person_pool = arguments["person_pool"] model.train() start_training_time = time.time() end = time.time() losses_reduced = torch.tensor(0.0) for iteration, (slow_video, fast_video, boxes, objects, extras, _) in enumerate(data_loader, start_iter): data_time = time.time() - end iteration = iteration + 1 arguments["iteration"] = iteration slow_video = slow_video.to(device) fast_video = fast_video.to(device) boxes = [box.to(device) for box in boxes] objects = [None if (box is None) else box.to(device) for box in objects] mem_extras = {} if mem_active: movie_ids = [e["movie_id"] for e in extras] timestamps = [e["timestamp"] for e in extras] mem_extras["person_pool"] = person_pool mem_extras["movie_ids"] = movie_ids mem_extras["timestamps"] = timestamps mem_extras["cur_loss"] = losses_reduced.item() loss_dict, weight_dict, metric_dict, pooled_feature = model(slow_video, fast_video, boxes, objects, mem_extras) losses = sum([loss_dict[k]*weight_dict[k] for k in loss_dict]) # reduce losses over all GPUs for logging purposes loss_dict["total_loss"] = losses.detach().clone() loss_dict_reduced = reduce_dict(loss_dict) metric_dict_reduced = reduce_dict(metric_dict) meters.update(**loss_dict_reduced, **metric_dict_reduced) losses_reduced = loss_dict_reduced.pop("total_loss") optimizer.zero_grad() losses.backward() optimizer.step() # update mem pool if mem_active: person_feature = [ft.to("cpu") for ft in pooled_feature[0]] feature_update = MemoryPool() for movie_id, timestamp, new_feature in zip(movie_ids, timestamps, person_feature): new_feature.add_field("loss_tag", losses_reduced.item()) feature_update[movie_id, timestamp] = new_feature feature_update_list = all_gather(feature_update) person_pool.update_list(feature_update_list) batch_time = time.time() - end end = time.time() meters.update(time=batch_time, data=data_time) eta_seconds = meters.time.global_avg * (max_iter - iteration) eta_string = str(datetime.timedelta(seconds=int(eta_seconds))) if iteration % 20 == 0 or iteration == max_iter: logger.info( meters.delimiter.join( [ "eta: {eta}", "iter: {iter}", "{meters}", "lr: {lr:.6f}", "max mem: {memory:.0f}", ] ).format( eta=eta_string, iter=iteration, meters=str(meters), lr=optimizer.param_groups[0]["lr"], memory=torch.cuda.max_memory_allocated() / 1024.0 / 1024.0, ) ) if tblogger is not None: for name, meter in meters.meters.items(): tblogger.add_scalar(name, meter.median, iteration) tblogger.add_scalar("lr", optimizer.param_groups[0]["lr"], iteration) scheduler.step() if iteration % checkpoint_period == 0: checkpointer.save("model_{:07d}".format(iteration), **arguments) if iteration == max_iter: arguments.pop("person_pool", None) checkpointer.save("model_final", **arguments) if dataset_names_val and iteration % val_period == 0: # do validation val_in_train( model, dataset_names_val, data_loaders_val, tblogger, iteration, distributed, mem_active ) end = time.time() total_training_time = time.time() - start_training_time total_time_str = str(datetime.timedelta(seconds=total_training_time)) logger.info( "Total training time: {} ({:.4f} s / it)".format( total_time_str, total_training_time / (max_iter) ) ) def val_in_train( model, dataset_names_val, data_loaders_val, tblogger, iteration, distributed, mem_active, ): if distributed: model_val = model.module else: model_val = model for dataset_name, data_loader_val in zip(dataset_names_val, data_loaders_val): eval_res = inference( model_val, data_loader_val, dataset_name, mem_active, ) synchronize() if tblogger is not None: eval_res, _ = eval_res total_mAP = eval_res['PascalBoxes_Precision/[email protected]'] tblogger.add_scalar(dataset_name + '_mAP_0.5IOU', total_mAP, iteration) model.train()
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/alphaction/engine/trainer.py
from operator import is_ from alphaction.dataset.transforms import video_transforms as T # from rand_aug import * from dataclasses import dataclass import numpy as np from data.RandomAugmentBBox import * import cv2 cv2.setNumThreads(0) @dataclass class TransformsCfg: MIN_SIZE_TRAIN: int = 256 MAX_SIZE_TRAIN: int = 464 MIN_SIZE_TEST: int = 256 MAX_SIZE_TEST: int = 464 PIXEL_MEAN = [122.7717, 115.9465, 102.9801] PIXEL_STD = [57.375, 57.375, 57.375] TO_BGR: bool = False FRAME_NUM: int = 16 # 16 FRAME_SAMPLE_RATE: int = 4 # 4 COLOR_JITTER: bool = True HUE_JITTER: float = 20.0 SAT_JITTER: float = 0.1 VAL_JITTER: float = 0.1 def build_transforms(cfg=TransformsCfg(), is_train=True, args=None): # build transforms for training of testing if is_train: min_size = cfg.MIN_SIZE_TRAIN max_size = cfg.MAX_SIZE_TRAIN color_jitter = cfg.COLOR_JITTER flip_prob = 0.5 else: min_size = cfg.MIN_SIZE_TEST max_size = cfg.MAX_SIZE_TEST color_jitter = False flip_prob = 0 frame_num = cfg.FRAME_NUM sample_rate = cfg.FRAME_SAMPLE_RATE if color_jitter: color_transform = T.ColorJitter( cfg.HUE_JITTER, cfg.SAT_JITTER, cfg.VAL_JITTER ) else: color_transform = T.Identity() to_bgr = cfg.TO_BGR normalize_transform = T.Normalize( mean=cfg.PIXEL_MEAN, std=cfg.PIXEL_STD, to_bgr=to_bgr ) transform = T.Compose( [ T.TemporalCrop(frame_num, sample_rate), T.Resize(min_size, max_size), T.RandomClip(is_train), color_transform, T.RandomHorizontalFlip(flip_prob), T.ToTensor(), normalize_transform, ] ) return transform
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/data/transforms.py
""" Modified from https://github.com/google-research/ssl_detection/blob/master/detection/utils/augmentation.py. """ import copy from unittest import result import cv2 import mmcv import numpy as np from PIL import Image, ImageEnhance, ImageOps from mmcv.image.colorspace import bgr2rgb, rgb2bgr from mmdet.core.mask import BitmapMasks, PolygonMasks from mmdet.datasets import PIPELINES from mmdet.datasets.pipelines import Compose as BaseCompose from mmdet.datasets.pipelines import transforms import numpy as np import pdb class GTrans(object): @classmethod def inverse(cls, results): # compute the inverse return results["transform_matrix"].I # 3x3 @classmethod def apply(self, results, operator, **kwargs): trans_matrix = getattr(self, f"_get_{operator}_matrix")(**kwargs) if "transform_matrix" not in results: results["transform_matrix"] = trans_matrix else: base_transformation = results["transform_matrix"] results["transform_matrix"] = np.dot(trans_matrix, base_transformation) @classmethod def apply_cv2_matrix(self, results, cv2_matrix): if cv2_matrix.shape[0] == 2: mat = np.concatenate( [cv2_matrix, np.array([0, 0, 1]).reshape((1, 3))], axis=0 ) else: mat = cv2_matrix base_transformation = results["transform_matrix"] results["transform_matrix"] = np.dot(mat, base_transformation) return results @classmethod def _get_rotate_matrix(cls, degree=None, cv2_rotation_matrix=None, inverse=False): # TODO: this is rotated by zero point if degree is None and cv2_rotation_matrix is None: raise ValueError( "At least one of degree or rotation matrix should be provided" ) if degree: if inverse: degree = -degree rad = degree * np.pi / 180 sin_a = np.sin(rad) cos_a = np.cos(rad) return np.array([[cos_a, sin_a, 0], [-sin_a, cos_a, 0], [0, 0, 1]]) # 2x3 else: mat = np.concatenate( [cv2_rotation_matrix, np.array([0, 0, 1]).reshape((1, 3))], axis=0 ) if inverse: mat = mat * np.array([[1, -1, -1], [-1, 1, -1], [1, 1, 1]]) return mat @classmethod def _get_shift_matrix(cls, dx=0, dy=0, inverse=False): if inverse: dx = -dx dy = -dy return np.array([[1, 0, dx], [0, 1, dy], [0, 0, 1]]) @classmethod def _get_shear_matrix( cls, degree=None, magnitude=None, direction="horizontal", inverse=False ): if magnitude is None: assert degree is not None rad = degree * np.pi / 180 magnitude = np.tan(rad) if inverse: magnitude = -magnitude if direction == "horizontal": shear_matrix = np.float32([[1, magnitude, 0], [0, 1, 0], [0, 0, 1]]) else: shear_matrix = np.float32([[1, 0, 0], [magnitude, 1, 0], [0, 0, 1]]) return shear_matrix @classmethod def _get_flip_matrix(cls, shape, direction="horizontal", inverse=False): h, w = shape if direction == "horizontal": flip_matrix = np.float32([[-1, 0, w], [0, 1, 0], [0, 0, 1]]) else: flip_matrix = np.float32([[1, 0, 0], [0, h - 1, 0], [0, 0, 1]]) return flip_matrix @classmethod def _get_scale_matrix(cls, sx, sy, inverse=False): if inverse: sx = 1 / sx sy = 1 / sy return np.float32([[sx, 0, 0], [0, sy, 0], [0, 0, 1]]) PARAMETER_MAX = 10 def int_parameter(level, maxval, max_level=None): if max_level is None: max_level = PARAMETER_MAX return int(level * maxval / max_level) def float_parameter(level, maxval, max_level=None): if max_level is None: max_level = PARAMETER_MAX return float(level) * maxval / max_level class SplitClip(object): def __init__(self,datatype=None): self.datatype = datatype def __call__(self,results): clips, box, transform_randoms = results clip_list = np.split(clips,clips.shape[0],axis=0) for i in range(len(clip_list)): clip_list[i] = clip_list[i].squeeze() return clip_list,box,transform_randoms def enable_record(self, mode: bool = True): self.record = mode class Composeclip(object): def __init__(self,datatype=None): self.datatype = datatype def __call__(self,results): clip_list, box,transform_randoms = results for i in range(len(clip_list)): clip_list[i] = np.expand_dims(clip_list[i], axis=0) clip = np.concatenate(clip_list,axis=0) return clip,box,transform_randoms def enable_record(self, mode: bool = True): self.record = mode class RandAug(object): """refer to https://github.com/google-research/ssl_detection/blob/00d52272f61b56eade8d5ace18213cba6c74f6d8/detection/utils/augmentation.py#L240.""" def __init__( self, prob: float = 1.0, magnitude: int = 10, random_magnitude: bool = True, record: bool = False, magnitude_limit: int = 10, ): assert 0 <= prob <= 1, f"probability should be in (0,1) but get {prob}" assert ( magnitude <= PARAMETER_MAX ), f"magnitude should be small than max value {PARAMETER_MAX} but get {magnitude}" self.prob = prob self.magnitude = magnitude self.magnitude_limit = magnitude_limit self.random_magnitude = random_magnitude self.record = record self.buffer = None def __call__(self, results): if np.random.random() < self.prob: magnitude = self.magnitude if self.random_magnitude: magnitude = np.random.randint(1, magnitude) if self.record: if "aug_info" not in results: results["aug_info"] = [] results["aug_info"].append(self.get_aug_info(magnitude=magnitude)) results = self.apply(results, magnitude) # clear buffer return results def apply(self, clip_list, box, magnitude: int = None): raise NotImplementedError() def __repr__(self): return f"{self.__class__.__name__}(prob={self.prob},magnitude={self.magnitude},max_magnitude={self.magnitude_limit},random_magnitude={self.random_magnitude})" def get_aug_info(self, **kwargs): aug_info = dict(type=self.__class__.__name__) aug_info.update( dict( prob=1.0, random_magnitude=False, record=False, magnitude=self.magnitude, ) ) aug_info.update(kwargs) return aug_info def enable_record(self, mode: bool = True): self.record = mode @PIPELINES.register_module() class Identity(RandAug): def apply(self, results, magnitude: int = None): return results @PIPELINES.register_module() class AutoContrast(RandAug): ###将图像对比度最大化 def apply(self, results, magnitude=None): clip_list, box, transform_randoms = results new_clip_list = [] for clip in clip_list: new_clip_list.append(np.asarray(ImageOps.autocontrast(Image.fromarray(clip)), dtype=clip.dtype)) return new_clip_list,box,transform_randoms @PIPELINES.register_module() class RandEqualize(RandAug): ###均衡图像的直方图。该函数使用一个非线性映射到输入图像,为了产生灰色值均匀分布的输出图像 def apply(self, results, magnitude=None): clip_list, box, transform_randoms = results new_clip_list = [] for clip in clip_list: new_clip_list.append(np.asarray(ImageOps.equalize(Image.fromarray(clip)), dtype=clip.dtype)) return new_clip_list,box,transform_randoms @PIPELINES.register_module() class RandSolarize(RandAug): ###曝光图像,反转所有高于阈值的像素值 def apply(self, results, magnitude=None): clip_list, box, transform_randoms = results new_clip_list = [] for clip in clip_list: new_clip_list.append(mmcv.solarize( clip, min(int_parameter(magnitude, 256, self.magnitude_limit), 255) )) return new_clip_list,box,transform_randoms def _enhancer_impl(enhancer): """Sets level to be between 0.1 and 1.8 for ImageEnhance transforms of PIL.""" def impl(pil_img, level, max_level=None): v = float_parameter(level, 1.8, max_level) + 0.1 # going to 0 just destroys it return enhancer(pil_img).enhance(v) return impl class RandEnhance(RandAug): op = None def apply(self,results, magnitude=None): clip_list, box = results new_clip_list = [] for clip in clip_list: new_clip_list.append( np.asarray( _enhancer_impl(self.op)( Image.fromarray(clip), magnitude, self.magnitude_limit ), dtype=clip.dtype, ) ) return new_clip_list,box @PIPELINES.register_module() class RandColor(RandEnhance): ##颜色增强 op = ImageEnhance.Color @PIPELINES.register_module() class RandContrast(RandEnhance): ## 对比度增强类用于调整图像的对比度。类似于调整彩色电视机的对比度 op = ImageEnhance.Contrast @PIPELINES.register_module() class RandBrightness(RandEnhance): ##亮度增强 op = ImageEnhance.Brightness @PIPELINES.register_module() class RandSharpness(RandEnhance): ##锐度增强 op = ImageEnhance.Sharpness @PIPELINES.register_module() class RandPosterize(RandAug): ###将每个颜色通道上变量bits对应的低(8-bits)个bit置0。变量bits的取值范围为[0,8] def apply(self, results, magnitude=None): clip_list, box, transform_randoms = results new_clip_list = [] for clip in clip_list: new_clip_list.append(np.asarray(ImageOps.posterize(Image.fromarray(clip), 4-magnitude), dtype=clip.dtype)) return new_clip_list,box,transform_randoms @PIPELINES.register_module() class Sequential(BaseCompose): def __init__(self, transforms, record: bool = False): super().__init__(transforms) self.record = record self.enable_record(record) def enable_record(self, mode: bool = True): # enable children to record self.record = mode for transform in self.transforms: transform.enable_record(mode) @PIPELINES.register_module() class OneOf(Sequential): def __init__(self, transforms, record: bool = False): self.transforms = [] for trans in transforms: if isinstance(trans, list): self.transforms.append(Sequential(trans)) else: assert isinstance(trans, dict) self.transforms.append(Sequential([trans])) self.enable_record(record) def __call__(self, results): transform = np.random.choice(self.transforms) return transform(results) @PIPELINES.register_module() class ShuffledSequential(Sequential): def __call__(self, data): order = np.random.permutation(len(self.transforms)) for idx in order: t = self.transforms[idx] data = t(data) if data is None: return None return data """ Geometric Augmentation. Modified from thirdparty/mmdetection/mmdet/datasets/pipelines/auto_augment.py """ def bbox2fields(): """The key correspondence from bboxes to labels, masks and segmentations.""" bbox2label = {"gt_bboxes": "gt_labels", "gt_bboxes_ignore": "gt_labels_ignore"} bbox2mask = {"gt_bboxes": "gt_masks", "gt_bboxes_ignore": "gt_masks_ignore"} bbox2seg = { "gt_bboxes": "gt_semantic_seg", } return bbox2label, bbox2mask, bbox2seg class GeometricAugmentation(object): def __init__( self, img_fill_val=125, seg_ignore_label=255, min_size=0, prob: float = 1.0, random_magnitude: bool = True, record: bool = False, ): if isinstance(img_fill_val, (float, int)): img_fill_val = tuple([float(img_fill_val)] * 3) elif isinstance(img_fill_val, tuple): assert len(img_fill_val) == 3, "img_fill_val as tuple must have 3 elements." img_fill_val = tuple([float(val) for val in img_fill_val]) assert np.all( [0 <= val <= 255 for val in img_fill_val] ), "all elements of img_fill_val should between range [0,255]." self.img_fill_val = img_fill_val self.seg_ignore_label = seg_ignore_label self.min_size = min_size self.prob = prob self.random_magnitude = random_magnitude self.record = record def __call__(self, results): if np.random.random() < self.prob: magnitude: dict = self.get_magnitude(results) if self.record: if "aug_info" not in results: results["aug_info"] = [] results["aug_info"].append(self.get_aug_info(**magnitude)) results = self.apply(results, **magnitude) self._filter_invalid(results, min_size=self.min_size) return results def get_magnitude(self, results) -> dict: raise NotImplementedError() def apply(self, results, **kwargs): raise NotImplementedError() def enable_record(self, mode: bool = True): self.record = mode def get_aug_info(self, **kwargs): aug_info = dict(type=self.__class__.__name__) aug_info.update( dict( # make op deterministic prob=1.0, random_magnitude=False, record=False, img_fill_val=self.img_fill_val, seg_ignore_label=self.seg_ignore_label, min_size=self.min_size, ) ) aug_info.update(kwargs) return aug_info def _filter_invalid(self, results, min_size=0): """Filter bboxes and masks too small or translated out of image.""" if min_size is None: return results bbox2label, bbox2mask, _ = bbox2fields() for key in results.get("bbox_fields", []): bbox_w = results[key][:, 2] - results[key][:, 0] bbox_h = results[key][:, 3] - results[key][:, 1] valid_inds = (bbox_w > min_size) & (bbox_h > min_size) valid_inds = np.nonzero(valid_inds)[0] results[key] = results[key][valid_inds] # label fields. e.g. gt_labels and gt_labels_ignore label_key = bbox2label.get(key) if label_key in results: results[label_key] = results[label_key][valid_inds] # mask fields, e.g. gt_masks and gt_masks_ignore mask_key = bbox2mask.get(key) if mask_key in results: results[mask_key] = results[mask_key][valid_inds] return results def __repr__(self): return f"""{self.__class__.__name__}( img_fill_val={self.img_fill_val}, seg_ignore_label={self.seg_ignore_label}, min_size={self.magnitude}, prob: float = {self.prob}, random_magnitude: bool = {self.random_magnitude}, )""" @PIPELINES.register_module() class RandTranslate(GeometricAugmentation): def __init__(self, x=None, y=None, **kwargs): super().__init__(**kwargs) self.x = x self.y = y if self.x is None and self.y is None: self.prob = 0.0 def get_magnitude(self, results): magnitude = {} if self.random_magnitude: if isinstance(self.x, (list, tuple)): assert len(self.x) == 2 x = np.random.random() * (self.x[1] - self.x[0]) + self.x[0] magnitude["x"] = x if isinstance(self.y, (list, tuple)): assert len(self.y) == 2 y = np.random.random() * (self.y[1] - self.y[0]) + self.y[0] magnitude["y"] = y else: if self.x is not None: assert isinstance(self.x, (int, float)) magnitude["x"] = self.x if self.y is not None: assert isinstance(self.y, (int, float)) magnitude["y"] = self.y return magnitude def apply(self, results, x=None, y=None): # ratio to pixel h, w, c = results["img_shape"] if x is not None: x = w * x if y is not None: y = h * y if x is not None: # translate horizontally self._translate(results, x) if y is not None: # translate veritically self._translate(results, y, direction="vertical") return results def _translate(self, results, offset, direction="horizontal"): if self.record: GTrans.apply( results, "shift", dx=offset if direction == "horizontal" else 0, dy=offset if direction == "vertical" else 0, ) self._translate_img(results, offset, direction=direction) self._translate_bboxes(results, offset, direction=direction) # fill_val defaultly 0 for BitmapMasks and None for PolygonMasks. self._translate_masks(results, offset, direction=direction) self._translate_seg( results, offset, fill_val=self.seg_ignore_label, direction=direction ) def _translate_img(self, results, offset, direction="horizontal"): for key in results.get("img_fields", ["img"]): img = results[key].copy() results[key] = mmcv.imtranslate( img, offset, direction, self.img_fill_val ).astype(img.dtype) def _translate_bboxes(self, results, offset, direction="horizontal"): """Shift bboxes horizontally or vertically, according to offset.""" h, w, c = results["img_shape"] for key in results.get("bbox_fields", []): min_x, min_y, max_x, max_y = np.split( results[key], results[key].shape[-1], axis=-1 ) if direction == "horizontal": min_x = np.maximum(0, min_x + offset) max_x = np.minimum(w, max_x + offset) elif direction == "vertical": min_y = np.maximum(0, min_y + offset) max_y = np.minimum(h, max_y + offset) # the boxes translated outside of image will be filtered along with # the corresponding masks, by invoking ``_filter_invalid``. results[key] = np.concatenate([min_x, min_y, max_x, max_y], axis=-1) def _translate_masks(self, results, offset, direction="horizontal", fill_val=0): """Translate masks horizontally or vertically.""" h, w, c = results["img_shape"] for key in results.get("mask_fields", []): masks = results[key] results[key] = masks.translate((h, w), offset, direction, fill_val) def _translate_seg(self, results, offset, direction="horizontal", fill_val=255): """Translate segmentation maps horizontally or vertically.""" for key in results.get("seg_fields", []): seg = results[key].copy() results[key] = mmcv.imtranslate(seg, offset, direction, fill_val).astype( seg.dtype ) def __repr__(self): repr_str = super().__repr__() return ("\n").join( repr_str.split("\n")[:-1] + [f"x={self.x}", f"y={self.y}"] + repr_str.split("\n")[-1:] ) @PIPELINES.register_module() class RandRotate(GeometricAugmentation): def __init__(self, angle=None, center=None, scale=1, **kwargs): super().__init__(**kwargs) self.angle = angle self.center = center self.scale = scale if self.angle is None: self.prob = 0.0 def get_magnitude(self, results): magnitude = {} if self.random_magnitude: if isinstance(self.angle, (list, tuple)): assert len(self.angle) == 2 angle = ( np.random.random() * (self.angle[1] - self.angle[0]) + self.angle[0] ) magnitude["angle"] = angle else: if self.angle is not None: assert isinstance(self.angle, (int, float)) magnitude["angle"] = self.angle return magnitude def apply(self, results, angle: float = None): h, w = results["img"].shape[:2] center = self.center if center is None: center = ((w - 1) * 0.5, (h - 1) * 0.5) self._rotate_img(results, angle, center, self.scale) rotate_matrix = cv2.getRotationMatrix2D(center, -angle, self.scale) if self.record: GTrans.apply(results, "rotate", cv2_rotation_matrix=rotate_matrix) self._rotate_bboxes(results, rotate_matrix) self._rotate_masks(results, angle, center, self.scale, fill_val=0) self._rotate_seg( results, angle, center, self.scale, fill_val=self.seg_ignore_label ) return results def _rotate_img(self, results, angle, center=None, scale=1.0): """Rotate the image. Args: results (dict): Result dict from loading pipeline. angle (float): Rotation angle in degrees, positive values mean clockwise rotation. Same in ``mmcv.imrotate``. center (tuple[float], optional): Center point (w, h) of the rotation. Same in ``mmcv.imrotate``. scale (int | float): Isotropic scale factor. Same in ``mmcv.imrotate``. """ for key in results.get("img_fields", ["img"]): img = results[key].copy() img_rotated = mmcv.imrotate( img, angle, center, scale, border_value=self.img_fill_val ) results[key] = img_rotated.astype(img.dtype) def _rotate_bboxes(self, results, rotate_matrix): """Rotate the bboxes.""" h, w, c = results["img_shape"] for key in results.get("bbox_fields", []): min_x, min_y, max_x, max_y = np.split( results[key], results[key].shape[-1], axis=-1 ) coordinates = np.stack( [[min_x, min_y], [max_x, min_y], [min_x, max_y], [max_x, max_y]] ) # [4, 2, nb_bbox, 1] # pad 1 to convert from format [x, y] to homogeneous # coordinates format [x, y, 1] coordinates = np.concatenate( ( coordinates, np.ones((4, 1, coordinates.shape[2], 1), coordinates.dtype), ), axis=1, ) # [4, 3, nb_bbox, 1] coordinates = coordinates.transpose((2, 0, 1, 3)) # [nb_bbox, 4, 3, 1] rotated_coords = np.matmul(rotate_matrix, coordinates) # [nb_bbox, 4, 2, 1] rotated_coords = rotated_coords[..., 0] # [nb_bbox, 4, 2] min_x, min_y = ( np.min(rotated_coords[:, :, 0], axis=1), np.min(rotated_coords[:, :, 1], axis=1), ) max_x, max_y = ( np.max(rotated_coords[:, :, 0], axis=1), np.max(rotated_coords[:, :, 1], axis=1), ) min_x, min_y = ( np.clip(min_x, a_min=0, a_max=w), np.clip(min_y, a_min=0, a_max=h), ) max_x, max_y = ( np.clip(max_x, a_min=min_x, a_max=w), np.clip(max_y, a_min=min_y, a_max=h), ) results[key] = np.stack([min_x, min_y, max_x, max_y], axis=-1).astype( results[key].dtype ) def _rotate_masks(self, results, angle, center=None, scale=1.0, fill_val=0): """Rotate the masks.""" h, w, c = results["img_shape"] for key in results.get("mask_fields", []): masks = results[key] results[key] = masks.rotate((h, w), angle, center, scale, fill_val) def _rotate_seg(self, results, angle, center=None, scale=1.0, fill_val=255): """Rotate the segmentation map.""" for key in results.get("seg_fields", []): seg = results[key].copy() results[key] = mmcv.imrotate( seg, angle, center, scale, border_value=fill_val ).astype(seg.dtype) def __repr__(self): repr_str = super().__repr__() return ("\n").join( repr_str.split("\n")[:-1] + [f"angle={self.angle}", f"center={self.center}", f"scale={self.scale}"] + repr_str.split("\n")[-1:] ) @PIPELINES.register_module() class RandShear(GeometricAugmentation): def __init__(self, x=None, y=None, interpolation="bilinear", **kwargs): super().__init__(**kwargs) self.x = x self.y = y self.interpolation = interpolation if self.x is None and self.y is None: self.prob = 0.0 def get_magnitude(self, results): magnitude = {} if self.random_magnitude: if isinstance(self.x, (list, tuple)): assert len(self.x) == 2 x = np.random.random() * (self.x[1] - self.x[0]) + self.x[0] magnitude["x"] = x if isinstance(self.y, (list, tuple)): assert len(self.y) == 2 y = np.random.random() * (self.y[1] - self.y[0]) + self.y[0] magnitude["y"] = y else: if self.x is not None: assert isinstance(self.x, (int, float)) magnitude["x"] = self.x if self.y is not None: assert isinstance(self.y, (int, float)) magnitude["y"] = self.y return magnitude def apply(self, results, x=None, y=None): if x is not None: # translate horizontally self._shear(results, np.tanh(-x * np.pi / 180)) if y is not None: # translate veritically self._shear(results, np.tanh(y * np.pi / 180), direction="vertical") return results def _shear(self, results, magnitude, direction="horizontal"): if self.record: GTrans.apply(results, "shear", magnitude=magnitude, direction=direction) self._shear_img(results, magnitude, direction, interpolation=self.interpolation) self._shear_bboxes(results, magnitude, direction=direction) # fill_val defaultly 0 for BitmapMasks and None for PolygonMasks. self._shear_masks( results, magnitude, direction=direction, interpolation=self.interpolation ) self._shear_seg( results, magnitude, direction=direction, interpolation=self.interpolation, fill_val=self.seg_ignore_label, ) def _shear_img( self, results, magnitude, direction="horizontal", interpolation="bilinear" ): """Shear the image. Args: results (dict): Result dict from loading pipeline. magnitude (int | float): The magnitude used for shear. direction (str): The direction for shear, either "horizontal" or "vertical". interpolation (str): Same as in :func:`mmcv.imshear`. """ for key in results.get("img_fields", ["img"]): img = results[key] img_sheared = mmcv.imshear( img, magnitude, direction, border_value=self.img_fill_val, interpolation=interpolation, ) results[key] = img_sheared.astype(img.dtype) def _shear_bboxes(self, results, magnitude, direction="horizontal"): """Shear the bboxes.""" h, w, c = results["img_shape"] if direction == "horizontal": shear_matrix = np.stack([[1, magnitude], [0, 1]]).astype( np.float32 ) # [2, 2] else: shear_matrix = np.stack([[1, 0], [magnitude, 1]]).astype(np.float32) for key in results.get("bbox_fields", []): min_x, min_y, max_x, max_y = np.split( results[key], results[key].shape[-1], axis=-1 ) coordinates = np.stack( [[min_x, min_y], [max_x, min_y], [min_x, max_y], [max_x, max_y]] ) # [4, 2, nb_box, 1] coordinates = ( coordinates[..., 0].transpose((2, 1, 0)).astype(np.float32) ) # [nb_box, 2, 4] new_coords = np.matmul( shear_matrix[None, :, :], coordinates ) # [nb_box, 2, 4] min_x = np.min(new_coords[:, 0, :], axis=-1) min_y = np.min(new_coords[:, 1, :], axis=-1) max_x = np.max(new_coords[:, 0, :], axis=-1) max_y = np.max(new_coords[:, 1, :], axis=-1) min_x = np.clip(min_x, a_min=0, a_max=w) min_y = np.clip(min_y, a_min=0, a_max=h) max_x = np.clip(max_x, a_min=min_x, a_max=w) max_y = np.clip(max_y, a_min=min_y, a_max=h) results[key] = np.stack([min_x, min_y, max_x, max_y], axis=-1).astype( results[key].dtype ) def _shear_masks( self, results, magnitude, direction="horizontal", fill_val=0, interpolation="bilinear", ): """Shear the masks.""" h, w, c = results["img_shape"] for key in results.get("mask_fields", []): masks = results[key] results[key] = masks.shear( (h, w), magnitude, direction, border_value=fill_val, interpolation=interpolation, ) def _shear_seg( self, results, magnitude, direction="horizontal", fill_val=255, interpolation="bilinear", ): """Shear the segmentation maps.""" for key in results.get("seg_fields", []): seg = results[key] results[key] = mmcv.imshear( seg, magnitude, direction, border_value=fill_val, interpolation=interpolation, ).astype(seg.dtype) def __repr__(self): repr_str = super().__repr__() return ("\n").join( repr_str.split("\n")[:-1] + [f"x_magnitude={self.x}", f"y_magnitude={self.y}"] + repr_str.split("\n")[-1:] ) @PIPELINES.register_module() class RandErase(GeometricAugmentation): def __init__( self, n_iterations=None, size=None, squared: bool = True, patches=None, **kwargs, ): kwargs.update(min_size=None) super().__init__(**kwargs) self.n_iterations = n_iterations self.size = size self.squared = squared self.patches = patches def get_magnitude(self, results): magnitude = {} if self.random_magnitude: n_iterations = self._get_erase_cycle() patches = [] h, w, c = results["img_shape"] for i in range(n_iterations): # random sample patch size in the image ph, pw = self._get_patch_size(h, w) # random sample patch left top in the image px, py = np.random.randint(0, w - pw), np.random.randint(0, h - ph) patches.append([px, py, px + pw, py + ph]) magnitude["patches"] = patches else: assert self.patches is not None magnitude["patches"] = self.patches return magnitude def _get_erase_cycle(self): if isinstance(self.n_iterations, int): n_iterations = self.n_iterations else: assert ( isinstance(self.n_iterations, (tuple, list)) and len(self.n_iterations) == 2 ) n_iterations = np.random.randint(*self.n_iterations) return n_iterations def _get_patch_size(self, h, w): if isinstance(self.size, float): assert 0 < self.size < 1 return int(self.size * h), int(self.size * w) else: assert isinstance(self.size, (tuple, list)) assert len(self.size) == 2 assert 0 <= self.size[0] < 1 and 0 <= self.size[1] < 1 w_ratio = np.random.random() * (self.size[1] - self.size[0]) + self.size[0] h_ratio = w_ratio if not self.squared: h_ratio = ( np.random.random() * (self.size[1] - self.size[0]) + self.size[0] ) return int(h_ratio * h), int(w_ratio * w) def apply(self, results, patches: list): for patch in patches: self._erase_image(results, patch, fill_val=self.img_fill_val) self._erase_mask(results, patch) self._erase_seg(results, patch, fill_val=self.seg_ignore_label) return results def _erase_image(self, results, patch, fill_val=128): for key in results.get("img_fields", ["img"]): tmp = results[key].copy() x1, y1, x2, y2 = patch tmp[y1:y2, x1:x2, :] = fill_val results[key] = tmp def _erase_mask(self, results, patch, fill_val=0): for key in results.get("mask_fields", []): masks = results[key] if isinstance(masks, PolygonMasks): # convert mask to bitmask masks = masks.to_bitmap() x1, y1, x2, y2 = patch tmp = masks.masks.copy() tmp[:, y1:y2, x1:x2] = fill_val masks = BitmapMasks(tmp, masks.height, masks.width) results[key] = masks def _erase_seg(self, results, patch, fill_val=0): for key in results.get("seg_fields", []): seg = results[key].copy() x1, y1, x2, y2 = patch seg[y1:y2, x1:x2] = fill_val results[key] = seg @PIPELINES.register_module() class RecomputeBox(object): def __init__(self, record=False): self.record = record def __call__(self, results): if self.record: if "aug_info" not in results: results["aug_info"] = [] results["aug_info"].append(dict(type="RecomputeBox")) _, bbox2mask, _ = bbox2fields() for key in results.get("bbox_fields", []): mask_key = bbox2mask.get(key) if mask_key in results: masks = results[mask_key] results[key] = self._recompute_bbox(masks) return results def enable_record(self, mode: bool = True): self.record = mode def _recompute_bbox(self, masks): boxes = np.zeros(masks.masks.shape[0], 4, dtype=np.float32) x_any = np.any(masks.masks, axis=1) y_any = np.any(masks.masks, axis=2) for idx in range(masks.masks.shape[0]): x = np.where(x_any[idx, :])[0] y = np.where(y_any[idx, :])[0] if len(x) > 0 and len(y) > 0: boxes[idx, :] = np.array( [x[0], y[0], x[-1] + 1, y[-1] + 1], dtype=np.float32 ) return boxes # TODO: Implement Augmentation Inside Box @PIPELINES.register_module() class RandResize(transforms.Resize): def __init__(self, record=False, **kwargs): super().__init__(**kwargs) self.record = record def __call__(self, results): results = super().__call__(results) if self.record: scale_factor = results["scale_factor"] GTrans.apply(results, "scale", sx=scale_factor[0], sy=scale_factor[1]) if "aug_info" not in results: results["aug_info"] = [] new_h, new_w = results["img"].shape[:2] results["aug_info"].append( dict( type=self.__class__.__name__, record=False, img_scale=(new_w, new_h), keep_ratio=False, bbox_clip_border=self.bbox_clip_border, backend=self.backend, ) ) return results def enable_record(self, mode: bool = True): self.record = mode @PIPELINES.register_module() class RandFlip(transforms.RandomFlip): def __init__(self, record=False, **kwargs): super().__init__(**kwargs) self.record = record def __call__(self, results): results = super().__call__(results) if self.record: if "aug_info" not in results: results["aug_info"] = [] if results["flip"]: GTrans.apply( results, "flip", direction=results["flip_direction"], shape=results["img_shape"][:2], ) results["aug_info"].append( dict( type=self.__class__.__name__, record=False, flip_ratio=1.0, direction=results["flip_direction"], ) ) else: results["aug_info"].append( dict( type=self.__class__.__name__, record=False, flip_ratio=0.0, direction="vertical", ) ) return results def enable_record(self, mode: bool = True): self.record = mode @PIPELINES.register_module() class MultiBranch(object): def __init__(self, **transform_group): self.transform_group = {k: BaseCompose(v) for k, v in transform_group.items()} def __call__(self, results): multi_results = [] for k, v in self.transform_group.items(): res = v(copy.deepcopy(results)) if res is None: return None # res["img_metas"]["tag"] = k multi_results.append(res) return multi_results if __name__ == '__main__': # RandResize(img_scale=[(1333, 400), (1333, 1200)], # multiscale_mode="range", # keep_ratio=True), # RandFlip(flip_ratio=0.5), img_trans = Sequential( transforms=[ SplitClip(), OneOf( transforms=[ dict(type=k) for k in [ "Identity", "AutoContrast", "RandEqualize", "RandSolarize", "RandColor", "RandContrast", "RandBrightness", "RandSharpness", "RandPosterize", ] ] ), Composeclip() ] ) img = np.random.randint(0,255,size=[4,2,2,3]) img = np.uint8(img) box = None # pdb.set_trace() new_img, _ = img_trans((img,box)) print(new_img.shape)
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/data/rand_aug.py
# Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Object detection strong augmentation utilities.""" # pylint: disable=g-explicit-length-test import data.augmentations as augmentations # from FasterRCNN.utils import np_box_ops import imgaug as ia import imgaug.augmenters as iaa from imgaug.augmenters.geometric import Affine import numpy as np import torch from PIL import Image import cv2 # from tensorpack.utils import logger RANDOM_COLOR_POLICY_OPS = ( 'Identity', 'AutoContrast', 'Equalize', 'Solarize', 'Color', 'Contrast', 'Brightness', 'Sharpness', 'Posterize', ) # for image size == 800, 0.1 is 80. CUTOUT = iaa.Cutout(nb_iterations=(1, 5), size=[0, 0.2], squared=True) DEGREE = 30 AFFINE_TRANSFORM = iaa.Sequential( [ iaa.OneOf([ Affine( # TranslateX translate_percent={'x': (-0.1, 0.1)}, order=[0, 1], cval=125, ), Affine( # TranslateY translate_percent={'y': (-0.1, 0.1)}, order=[0, 1], cval=125, ), Affine( # Rotate rotate=(-DEGREE, DEGREE), order=[0, 1], cval=125, ), Affine( # ShearX and ShareY shear=(-DEGREE, DEGREE), order=[0, 1], cval=125, ), ]), ], # do all of the above augmentations in random order random_order=True) # AFFINE_TRANSFORM = iaa.Sequential( # [ # iaa.OneOf([ # Affine( # TranslateX # translate_percent={'x': (-0.1, 0.1)}, # order=[0, 1], # cval=125, # ) # ]) # ], # # do all of the above augmentations in random order # random_order=True) AFFINE_TRANSFORM_WEAK = iaa.Sequential( [ iaa.OneOf([ Affine( translate_percent={'x': (-0.05, 0.05)}, order=[0, 1], cval=125, ), Affine( translate_percent={'y': (-0.05, 0.05)}, order=[0, 1], cval=125, ), Affine( rotate=(-10, 10), order=[0, 1], cval=125, ), Affine( shear=(-10, 10), order=[0, 1], cval=125, ), ]), ], # do all of the above augmentations in random order random_order=True) COLOR = iaa.Sequential( [ iaa.OneOf( # apply one color transformation [ iaa.Add((0, 0)), # identity transform iaa.OneOf([ iaa.GaussianBlur((0, 3.0)), iaa.AverageBlur(k=(2, 7)), ]), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), iaa.AdditiveGaussianNoise( loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), iaa.Invert(0.05, per_channel=True), # invert color channels # Add a value of -10 to 10 to each pixel. iaa.Add((-10, 10), per_channel=0.5), # Change brightness of images (50-150% of original value). iaa.Multiply((0.5, 1.5), per_channel=0.5), # Improve or worsen the contrast of images. iaa.contrast.LinearContrast((0.5, 2.0), per_channel=0.5), ]) ], random_order=True) def bb_to_array(bbs): coords = [] for bb in range(len(bbs)): coord = list(bbs[bb]._split_into_xyxy()) coords.append([i.squeeze().numpy() for i in coord]) coords = np.array(coords) return coords def siglebb_to_array(bbs): coords = [] for bb in bbs: coords.append([bb.x1, bb.y1, bb.x2, bb.y2]) # coords = np.array(coords) return coords def allbb_to_array(bbs): coords = [] for bb in bbs: coords.append(array_to_bb(bb_to_array(bb))) return coords def array_to_bb(coords): # convert to bbs bbs = [] for b in coords: bbs.append(ia.BoundingBox(x1=b[0], y1=b[1], x2=b[2], y2=b[3])) return bbs class RandomAugmentBBox(object): """Augmentation class.""" def __init__(self, is_train = True, aug_type='strong', magnitude=10, weighted_inbox_selection=False): self.affine_aug_op = AFFINE_TRANSFORM # for inbox affine, we use small degree self.inbox_affine_aug_op = AFFINE_TRANSFORM_WEAK self.jitter_aug_op = COLOR self.cutout_op = CUTOUT self.magnitude = magnitude self.aug_type = aug_type self.is_train = is_train self.weighted_inbox_selection = weighted_inbox_selection # self.augment_fn is a list of list (i.g. [[],...]), each item is a list of # augmentation will be randomly picked up. Th total items determine the # number of layers of augmentation to apply # Note: put cutout_augment at last if used. if aug_type == 'strong': # followd by cutout self.augment_fn = [[self.color_augment], [self.bbox_affine_transform, self.affine_transform], [self.cutout_augment]] # self.augment_fn = [[self.bbox_affine_transform]] elif aug_type == 'default': self.augment_fn = [[self.color_augment], [self.affine_transform]] elif aug_type == 'default_w_cutout': self.augment_fn = [[self.color_augment], [self.affine_transform], [self.cutout_augment]] elif aug_type == 'default_wo_affine': self.augment_fn = [[self.color_augment], [self.affine_transform], [self.cutout_augment]] else: raise NotImplementedError('aug_type {} does not exist'.format(aug_type)) self.randomize_parameters() # logger.info('-' * 100) # logger.info('Augmentation type {}: {}'.format(aug_type, self.augment_fn)) # logger.info('-' * 100) def normaize(self, x): x = x / 255.0 x = x / 0.5 - 1.0 return x def unnormaize(self, x): x = (x + 1.0) * 0.5 * 255.0 return x def numpy_apply_policies(self, arglist): x, policies = arglist re = [] for y, policy in zip(x, policies): # apply_policy has input to have images [-1, 1] y_a = augmentations.apply_policy(policy, self.normaize(y)) y_a = np.reshape(y_a, y.shape) y_a = self.unnormaize(y_a) re.append(y_a) return np.stack(re).astype('f') def bbox_affine_transform(self, results, **kwargs): """In-box affine transformation.,这里是将图片中的一些box部分图片裁剪下来,仅仅对裁剪下来的部分进行变换""" images, bounding_boxes,transform_randoms = results real_box_n = [len(i) for i in bounding_boxes] if isinstance(images, np.ndarray): images = np.split(images, len(images), axis=0) images = [i.squeeze() for i in images] shape = images[0].shape copybounding_boxes = [bounding_boxes for _ in range(len(images))] for im, boxes in zip(images, copybounding_boxes): boxes = bb_to_array(boxes) # large area has better probability to be sampled if self.weighted_inbox_selection: area = np_box_ops.area(boxes[:real_box_n]) k = np.random.choice([i for i in range(real_box_n)], 1, p=area / area.sum())[0] else: k = self.k_det if len(boxes) > 0: # import pdb # pdb.set_trace() box = boxes[k] im_crop = im[int(box[1]):int(box[3]), int(box[0]):int(box[2])].copy() im_paste = self.inbox_affine_aug_op_det(images=[im_crop])[0] # in-memory operation im[int(box[1]):int(box[3]), int(box[0]):int(box[2])] = im_paste assert shape == images[0].shape images = np.array(images) return images, bounding_boxes, transform_randoms def affine_transform(self, results): """Global affine transformation.""" images, bounding_boxes,transform_randoms = results if isinstance(images, np.ndarray): images = np.split(images, len(images), axis=0) images = [i.squeeze() for i in images] shape = images[0].shape bounding_boxes = [bounding_boxes for _ in range(len(images))] ori_bbx = allbb_to_array(bounding_boxes) images_aug, bbs_aug = self.affine_aug_op_det( images=images, bounding_boxes=ori_bbx) for i in range(len(bbs_aug)): new_array = siglebb_to_array(bbs_aug[i]) new_array = torch.as_tensor(new_array, dtype=torch.float32).reshape(-1, 4) assert new_array.shape == bounding_boxes[i].bbox.shape bounding_boxes[i].update_box(new_array,"xyxy") break assert shape == images_aug[0].shape images_aug = np.array(images_aug) return images_aug, bounding_boxes[0], transform_randoms def jitter_augment(self, results): """Color jitters.""" images, bounding_boxes,transform_randoms = results images_aug = self.jitter_aug_op(images=images) return images_aug, bounding_boxes, transform_randoms def cutout_augment(self, results): """Cutout augmentation.""" images, bounding_boxes,transform_randoms = results images_aug = self.cutout_op_det(images=images) return images_aug, bounding_boxes, transform_randoms def color_augment(self, results, p=1.0, **kwargs): """RandAug color augmentation.""" """颜色增强,不会改变box,images:List[np.ndarray]""" del kwargs images, bounding_boxes,transform_randoms = results policy = lambda: [(op, p, self.magnitude_det) # pylint: disable=g-long-lambda for op in self.color_policies_det] images_aug = [] shape = images[0].shape for x in range(len(images)): images_aug.append( self.numpy_apply_policies((images[x:x + 1], [policy()]))[0]) assert shape == images_aug[0].shape if bounding_boxes is None: return images_aug images_aug = np.array(images_aug) return images_aug, bounding_boxes, transform_randoms def set_k_for_bbox_affine_transform(self, **kwargs): real_box_n = kwargs['n_real_box'] self.k_det = np.random.choice([i for i in range(real_box_n)], 1)[0] def randomize_parameters(self, **kwargs): self.cutout_op_det = self.cutout_op.to_deterministic() self.affine_aug_op_det = self.affine_aug_op.to_deterministic() self.inbox_affine_aug_op_det = self.inbox_affine_aug_op.to_deterministic() self.magnitude_det = np.random.randint(1, self.magnitude) self.color_policies_det = np.random.choice(RANDOM_COLOR_POLICY_OPS, 1) # if 'n_real_box' in kwargs: real_box_n = kwargs['n_real_box'] if 'n_real_box' in kwargs else 1 self.k_det = np.random.choice([i for i in range(real_box_n)], 1)[0] if len(self.augment_fn) > 0 and self.augment_fn[-1][0].__name__ == 'cutout_augment': naug = len(self.augment_fn) self.oder1_det = np.random.permutation(np.arange(naug - 1)) else: self.order2_det = np.random.permutation(np.arange(len(self.augment_fn))) self.tmp = np.random.randint(0, 2) def __call__(self, results, **kwargs): if self.is_train: images, bounding_boxes,transform_randoms = results # random order if len(self.augment_fn) > 0 and self.augment_fn[-1][0].__name__ == 'cutout_augment': # put cutout in the last always naug = len(self.augment_fn) order = self.oder1_det order = np.concatenate([order, [naug - 1]], 0) else: order = self.order2_det # pylint: disable=invalid-name T = None for i in order: fns = self.augment_fn[i] # fn = fns[np.random.randint(0, len(fns))] if len(fns) > 1: fn = fns[self.tmp] else: fn = fns[0] images, bounding_boxes, _T = fn( results=(images, bounding_boxes, transform_randoms), **kwargs) if _T is not None: T = _T return images, bounding_boxes, T else: return results if __name__ == '__main__': import sys sys.path.append('/mnt/cache/xingsen/VideoMAE_ava_ft3/') img = np.random.randint(0, 255, (2, 100, 100, 3), dtype=np.uint8) img = np.split(img,img.shape[0],axis=0) img = [np.array(Image.open('/mnt/cache/xingsen/VideoMAE_ava_ft3/data/1.jpg'))] for i in range(len(img)): img[i] = img[i].squeeze() print(type(img[0])) from alphaction.structures.bounding_box import BoxList im_w, im_h = 100, 100 n = 1 xy = np.array([16,11]).reshape(1,2)#np.zeros([n,2])#np.uint8(np.random.random([n, 2]) * 50 + 30) w = np.uint8(np.ones([n, 1]) * 53) h = np.uint8(np.ones([n, 1]) * 62) # cv2.rectangle(img[0], (16,11), (69,73), (0,255,0), 4) # cv2.imwrite('test.jpg',img[0]) boxes = np.concatenate([xy, w, h], axis=1) boxes_tensor = torch.as_tensor(boxes, dtype=torch.float32).reshape(-1, 4) # guard against no boxes boxes = BoxList(boxes_tensor, (im_w, im_h), mode="xywh").convert("xyxy") xmin, ymin, xmax, ymax = boxes[0]._split_into_xyxy() aug = RandomAugmentBBox() aug.randomize_parameters() images_aug, bbs_aug, _ = aug((img, [boxes], None)) print(images_aug,bbs_aug) # x1,y1,x2,y2 = bbs_aug[0][0].x1,bbs_aug[0][0].y1,bbs_aug[0][0].x2,bbs_aug[0][0].y2 # if x1 < 0: # x1 = 0 # if y1 < 0: # y1 = 0 # cv2.rectangle(images_aug[0], (x1,y1), (x2,y2), (0,255,0), 4) # cv2.imwrite('test2.jpg',images_aug[0])
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/data/RandomAugmentBBox.py
import numpy as np import logging import tempfile import os from pprint import pformat import csv import time from collections import defaultdict from alphaction.dataset.datasets.evaluation.ava.pascal_evaluation import object_detection_evaluation, standard_fields def do_ava_evaluation(dataset, predictions, output_folder): logging.info("Preparing results for AVA format") ava_results = prepare_for_ava_detection(predictions, dataset) logging.info("Evaluating predictions") # import pdb # pdb.set_trace() with tempfile.NamedTemporaryFile() as f: file_path = f.name if output_folder: file_path = os.path.join(output_folder, "result.csv") if len(dataset.eval_file_paths) == 0: write_csv(ava_results, file_path) return eval_res = evaluate_predictions_on_ava( dataset.eval_file_paths, ava_results, file_path ) logging.info(pformat(eval_res, indent=2)) if output_folder: log_file_path = os.path.join(output_folder, "result.log") with open(log_file_path, "a+") as logf: logf.write(pformat(eval_res)) return eval_res, ava_results def make_image_key(video_id, timestamp): """Returns a unique identifier for a video id & timestamp.""" return "%s,%04d" % (video_id, int(float(timestamp))) def decode_image_key(image_key): return image_key[:-5], image_key[-4:] def prepare_for_ava_detection(predictions, dataset): # import pdb # pdb.set_trace() ava_results = {} score_thresh = dataset.action_thresh TO_REMOVE = 1.0 for video_id, prediction in enumerate(predictions): # import pdb # pdb.set_trace() video_info = dataset.get_video_info(video_id) if len(prediction) == 0: continue video_width = video_info["width"] video_height = video_info["height"] prediction = prediction.resize((video_width, video_height)) prediction = prediction.convert("xyxy") boxes = prediction.bbox.numpy() boxes[:, [2, 3]] += TO_REMOVE boxes[:, [0, 2]] /= video_width boxes[:, [1, 3]] /= video_height boxes = np.clip(boxes, 0.0, 1.0) # No background class. scores = prediction.get_field("scores").numpy() box_ids, action_ids = np.where(scores >= score_thresh) boxes = boxes[box_ids, :] scores = scores[box_ids, action_ids] action_ids = action_ids + 1 movie_name = video_info['movie'] timestamp = video_info['timestamp'] clip_key = make_image_key(movie_name, timestamp) ava_results[clip_key] = { "boxes": boxes, "scores": scores, "action_ids": action_ids } return ava_results def read_exclusions(exclusions_file): """Reads a CSV file of excluded timestamps. Args: exclusions_file: Path of file containing a csv of video-id,timestamp. Returns: A set of strings containing excluded image keys, e.g. "aaaaaaaaaaa,0904", or an empty set if exclusions file is None. """ excluded = set() exclusions_file = open(exclusions_file, 'r') if exclusions_file: reader = csv.reader(exclusions_file) for row in reader: assert len(row) == 2, "Expected only 2 columns, got: {}".format(row) excluded.add(make_image_key(row[0], row[1])) return excluded def read_labelmap(labelmap_file): """Reads a labelmap without the dependency on protocol buffers. Args: labelmap_file: Path of file containing a label map protocol buffer. Returns: labelmap: The label map in the form used by the object_detection_evaluation module - a list of {"id": integer, "name": classname } dicts. class_ids: A set containing all of the valid class id integers. """ labelmap = [] class_ids = set() name = "" class_id = "" labelmap_file = open(labelmap_file, 'r') for line in labelmap_file: if line.startswith(" name:"): name = line.split('"')[1] elif line.startswith(" id:") or line.startswith(" label_id:"): class_id = int(line.strip().split(" ")[-1]) labelmap.append({"id": class_id, "name": name}) class_ids.add(class_id) return labelmap, class_ids def read_csv(csv_file, class_whitelist=None): """Loads boxes and class labels from a CSV file in the AVA format. CSV file format described at https://research.google.com/ava/download.html. Args: csv_file: Path of csv file. class_whitelist: If provided, boxes corresponding to (integer) class labels not in this set are skipped. Returns: boxes: A dictionary mapping each unique image key (string) to a list of boxes, given as coordinates [y1, x1, y2, x2]. labels: A dictionary mapping each unique image key (string) to a list of integer class lables, matching the corresponding box in `boxes`. scores: A dictionary mapping each unique image key (string) to a list of score values lables, matching the corresponding label in `labels`. If scores are not provided in the csv, then they will default to 1.0. """ start = time.time() boxes = defaultdict(list) labels = defaultdict(list) scores = defaultdict(list) csv_file = open(csv_file, 'r') reader = csv.reader(csv_file) for row in reader: assert len(row) in [7, 8], "Wrong number of columns: " + row image_key = make_image_key(row[0], row[1]) x1, y1, x2, y2 = [float(n) for n in row[2:6]] action_id = int(row[6]) if class_whitelist and action_id not in class_whitelist: continue score = 1.0 if len(row) == 8: score = float(row[7]) boxes[image_key].append([y1, x1, y2, x2]) labels[image_key].append(action_id) scores[image_key].append(score) print_time("read file " + csv_file.name, start) return boxes, labels, scores def write_csv(ava_results, csv_result_file): start = time.time() with open(csv_result_file, 'w') as csv_file: spamwriter = csv.writer(csv_file, delimiter=',') for clip_key in ava_results: movie_name, timestamp = decode_image_key(clip_key) cur_result = ava_results[clip_key] boxes = cur_result["boxes"] scores = cur_result["scores"] action_ids = cur_result["action_ids"] assert boxes.shape[0] == scores.shape[0] == action_ids.shape[0] for box, score, action_id in zip(boxes, scores, action_ids): box_str = ['{:.5f}'.format(cord) for cord in box] score_str = '{:.5f}'.format(score) spamwriter.writerow([movie_name, timestamp, ] + box_str + [action_id, score_str]) print_time("write file " + csv_result_file, start) def print_time(message, start): logging.info("==> %g seconds to %s", time.time() - start, message) def evaluate_predictions_on_ava(eval_file_paths, ava_results, csv_result_file): write_csv(ava_results, csv_result_file) groundtruth = eval_file_paths["csv_gt_file"] labelmap = eval_file_paths["labelmap_file"] exclusions = eval_file_paths["exclusion_file"] categories, class_whitelist = read_labelmap(labelmap) logging.info("CATEGORIES (%d):\n%s", len(categories), pformat(categories, indent=2)) excluded_keys = read_exclusions(exclusions) pascal_evaluator = object_detection_evaluation.PascalDetectionEvaluator( categories) # Reads the ground truth dataset. boxes, labels, _ = read_csv(groundtruth, class_whitelist) start = time.time() for image_key in boxes: if image_key in excluded_keys: logging.info(("Found excluded timestamp in ground truth: %s. " "It will be ignored."), image_key) continue pascal_evaluator.add_single_ground_truth_image_info( image_key, { standard_fields.InputDataFields.groundtruth_boxes: np.array(boxes[image_key], dtype=float), standard_fields.InputDataFields.groundtruth_classes: np.array(labels[image_key], dtype=int), standard_fields.InputDataFields.groundtruth_difficult: np.zeros(len(boxes[image_key]), dtype=bool) }) print_time("convert groundtruth", start) # Reads detections dataset. boxes, labels, scores = read_csv(csv_result_file, class_whitelist) start = time.time() for image_key in boxes: if image_key in excluded_keys: logging.info(("Found excluded timestamp in detections: %s. " "It will be ignored."), image_key) continue pascal_evaluator.add_single_detected_image_info( image_key, { standard_fields.DetectionResultFields.detection_boxes: np.array(boxes[image_key], dtype=float), standard_fields.DetectionResultFields.detection_classes: np.array(labels[image_key], dtype=int), standard_fields.DetectionResultFields.detection_scores: np.array(scores[image_key], dtype=float) }) print_time("convert detections", start) start = time.time() metrics = pascal_evaluator.evaluate() print_time("run_evaluator", start) return metrics
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/data/ava_eval.py
import os from venv import main import torch.utils.data as data import time import torch import numpy as np import sys import torch.nn.functional as F sys.path.append('/mnt/cache/xingsen/xingsen2/VideoMAE_ava') from alphaction.structures.bounding_box import BoxList # from transforms import build_transforms from collections import defaultdict from alphaction.utils.video_decode import av_decode_video from alphaction.dataset.datasets.ava import NpInfoDict, NpBoxDict import pdb import json import decord import logging from tqdm import tqdm class AKDataset(data.Dataset): def __init__(self,kinetics_annfile,ava_annfile,video_root,frame_span,remove_clips_without_annotations,kinetics_box_file=None,ava_box_file=None, ava_eval_file_path={},kinetics_eval_file_path={},eval_file_paths={},box_thresh=0.0,action_thresh=0.0,transforms=None): self.action_thresh = action_thresh self.box_thresh = box_thresh self.eval_file_paths = eval_file_paths self.remove_clips_without_annotations = remove_clips_without_annotations self.kinetics_dataset = KineticsDataset(kinetics_annfile=kinetics_annfile, frame_span=frame_span, remove_clips_without_annotations=True, box_file=kinetics_box_file, eval_file_paths=kinetics_eval_file_path, box_thresh=box_thresh, action_thresh=action_thresh, transforms=transforms) self.ava_dataset = AVAVideoDataset( video_root=video_root, ann_file=ava_annfile, frame_span=frame_span, remove_clips_without_annotations=remove_clips_without_annotations, box_file=ava_box_file, eval_file_paths=ava_eval_file_path, box_thresh=box_thresh, action_thresh=action_thresh, transforms=transforms) def __len__(self): return len(self.kinetics_dataset) + len(self.ava_dataset) def __getitem__(self, index): if self.remove_clips_without_annotations: missing_id = [17261,35964,52484,97042] if index in missing_id: return self.kinetics_dataset[-1] if index < len(self.kinetics_dataset): return self.kinetics_dataset[index] else: return self.ava_dataset[index - len(self.kinetics_dataset)] def get_video_info(self, index): if index < len(self.kinetics_dataset): return self.kinetics_dataset.get_video_info(index) else: return self.ava_dataset.get_video_info(index - len(self.kinetics_dataset)) class KineticsDataset(data.Dataset): def __init__(self,kinetics_annfile,frame_span,remove_clips_without_annotations, box_file=None, eval_file_paths={},box_thresh=0.0,action_thresh=0.0,transforms=None): print('loading annotations into memory...') tic = time.time() self.kinetics_annfile = kinetics_annfile kinetics_dict = json.load(open(kinetics_annfile,'r')) assert type(kinetics_dict) == dict, 'annotation file format {} not supported'.format(type(kinetics_dict)) self.transforms = transforms self.frame_span = frame_span self.video_map = np.load('../annotations/video_path_k700.npy',allow_pickle=True).item() self.video_size_map = np.load('../annotations/movie_size.npy',allow_pickle=True).item() # These two attributes are used during ava evaluation... # Maybe there is a better implementation self.eval_file_paths = eval_file_paths self.action_thresh = action_thresh self.clip2ann_kinetics = self.load_ann_file(kinetics_dict) self.movie_info_kinetics, self.clip_ids_kinetics, self.clips_info_kinetics = self.get_videos_info(kinetics_dict) if remove_clips_without_annotations: self.clip_ids_kinetics = [clip_id for clip_id in self.clip_ids_kinetics if clip_id in self.clip2ann_kinetics] # pdb.set_trace() valid_id = self.del_invalid_id() #23315 self.clip_ids_kinetics = valid_id if box_file: imgToBoxes = self.load_box_file(box_file,box_thresh) # val len 23224 clip_ids = [ img_id for img_id in self.clip_ids_kinetics # if len(imgToBoxes[img_id]) > 0 ] #val len 21738 self.clip_ids_kinetics = clip_ids self.det_persons = NpBoxDict(imgToBoxes,clip_ids, value_types=[("bbox", np.float32), ("score", np.float32)]) #21738 else: self.det_persons = None self.anns = NpBoxDict(self.clip2ann_kinetics,self.clip_ids_kinetics,value_types=[("bbox", np.float32), ("packed_act", np.uint8)]) clips_info = { clip_id: [ self.movie_info_kinetics.convert_key(self.clips_info_kinetics[clip_id][0]), self.clips_info_kinetics[clip_id][1] ] for clip_id in self.clip_ids_kinetics } self.clips_info = NpInfoDict(clips_info,value_type=np.int32) # pdb.set_trace() super().__init__() def __getitem__(self, idx): _, clip_info = self.clips_info[idx] mov_id, timestamp = clip_info movie_id, _ = self.movie_info_kinetics[mov_id] video_data = self._decode_video_data(movie_id, timestamp) _,im_h,im_w,_ = video_data.shape if self.det_persons is None: # Note: During training, we only use gt. Thus we should not provide box file, # otherwise we will use only box file instead. boxes, packed_act = self.anns[idx] boxes = boxes * np.array([im_w, im_h, im_w, im_h]) boxes_tensor = torch.as_tensor(boxes, dtype=torch.float32).reshape(-1, 4) # guard against no boxes boxes = BoxList(boxes_tensor, (im_w, im_h), mode="xywh").convert("xyxy") # Decode the packed bits from uint8 to one hot, since AVA has 80 classes, # it can be exactly denoted with 10 bytes, otherwise we may need to discard some bits. one_hot_label = np.unpackbits(packed_act, axis=1) one_hot_label = torch.as_tensor(one_hot_label, dtype=torch.uint8) boxes.add_field("labels", one_hot_label) # 80 else: boxes, box_score = self.det_persons[idx] logging.info("box is {},h:{},w:{}".format(boxes,im_h,im_w)) boxes = boxes * np.array([im_w, im_h, im_w, im_h]) boxes_tensor = torch.as_tensor(boxes, dtype=torch.float32).reshape(-1, 4) boxes = BoxList(boxes_tensor, (im_w, im_h), mode="xyxy") box_score_tensor = torch.as_tensor(box_score, dtype=torch.float32).reshape(-1, 1) boxes.add_field("scores", box_score_tensor) boxes = boxes.clip_to_image(remove_empty=True) if self.transforms is not None: video_data, boxes, transform_randoms = self.transforms(video_data, boxes) return video_data, boxes, idx def __len__(self): return len(self.clips_info) def _decode_video_data(self, dirname, timestamp): # decode target video data from segment per second. video_path = self.video_map[dirname] time_list = video_path[:-4].split('_')[-2:] start_time = int(time_list[0]) end_time = int(time_list[1]) time_offset = (timestamp - start_time)/(end_time-start_time) frames = av_decode_video(video_path) mid_frame = int(time_offset * len(frames)) right_span = self.frame_span//2 + mid_frame left_span = right_span - self.frame_span if left_span < 0 and right_span <= len(frames): frames_left = self.numpy2tensorinterpolate(frames[0:mid_frame],self.frame_span-(self.frame_span//2)) frames_right = frames[mid_frame:right_span] frames = np.concatenate([frames_left,np.stack(frames_right)],axis=0) elif left_span >= 0 and right_span > len(frames): frames_left = frames[left_span:mid_frame] try: frames_right = self.numpy2tensorinterpolate(frames[mid_frame:],self.frame_span//2) frames = np.concatenate([np.stack(frames_left),frames_right],axis=0) except ValueError: print(mid_frame,len(frames),time_offset,timestamp,start_time,end_time,dirname) exit(0) else: frames = np.stack(frames[left_span:right_span]) return frames def numpy2tensorinterpolate(self,aArray,time_len): aArray = torch.from_numpy(np.stack(aArray)).unsqueeze(0).float() aArray = aArray.permute(0,4,1,2,3) # 1 * channel * time * h * w _, c, t, h, w = aArray.shape aArray = F.interpolate(aArray,size=[time_len,h,w],mode='trilinear',align_corners=False) aArray = aArray.squeeze() aArray = np.uint8(aArray.permute(1,2,3,0).numpy()) return aArray def del_invalid_id(self): valid_clip_ids = [] for clip_id in self.clip_ids_kinetics: timestamp = int(clip_id.split('_')[-1]) assert timestamp == self.clips_info_kinetics[clip_id][1] mov_id = self.movie_info_kinetics.convert_key(self.clips_info_kinetics[clip_id][0]) movie_id,_ = self.movie_info_kinetics[mov_id] video_path = self.video_map[movie_id] try: time_list = video_path[:-4].split('_')[-2:] except TypeError: continue start_time = int(time_list[0]) end_time = int(time_list[1]) if timestamp > start_time and timestamp < end_time: valid_clip_ids.append(clip_id) return valid_clip_ids def load_ann_file(self,json_dict): clip2ann = defaultdict(list) if "annotations" in json_dict: for ann in json_dict["annotations"]: try: action_ids = np.array(ann["action_ids"],dtype=np.uint8) except ValueError: continue one_hot = np.zeros(81, dtype=np.bool) one_hot[action_ids] = True packed_act = np.packbits(one_hot[1:]) clip2ann[ann["image_id"]].append(dict(bbox=ann["bbox"], packed_act=packed_act)) return clip2ann def get_videos_info(self,json_dict): movies_size = {} clips_info = {} for img in json_dict["images"]: mov = img["movie"] if mov not in movies_size: movies_size[mov] = [img["width"],img["height"]] clips_info[img["id"]] = [mov, img["timestamp"]] movie_info = NpInfoDict(movies_size, value_type=np.int32) clip_ids = sorted(list(clips_info.keys())) return movie_info, clip_ids, clips_info def get_video_info(self, index): _, clip_info = self.clips_info[index] # mov_id is the id in self.movie_info mov_id, timestamp = clip_info # movie_id is the human-readable youtube id. movie_id, movie_size = self.movie_info_kinetics[mov_id] h, w = self.video_size_map[movie_id] return dict(width=w, height=h, movie=movie_id, timestamp=timestamp) def load_box_file(self, box_file, score_thresh=0.0): import json print('Loading box file into memory...') tic = time.time() with open(box_file, "r") as f: box_results = json.load(f) print('Done (t={:0.2f}s)'.format(time.time() - tic)) boxImgIds = [box['image_id'] for box in box_results] imgToBoxes = defaultdict(list) for img_id, box in zip(boxImgIds, box_results): if box['score'] >= score_thresh: imgToBoxes[img_id].append(box) return imgToBoxes def getitem(self,idx): return self.__getitem__(idx) class AVAVideoDataset(data.Dataset): def __init__(self, video_root, ann_file, remove_clips_without_annotations, frame_span, box_file=None, eval_file_paths={}, box_thresh=0.0, action_thresh=0.0, transforms=None): print('loading annotations into memory...') tic = time.time() json_dict = json.load(open(ann_file, 'r')) assert type(json_dict) == dict, 'annotation file format {} not supported'.format(type(json_dict)) print('Done (t={:0.2f}s)'.format(time.time() - tic)) self.video_root = video_root self.transforms = transforms self.frame_span = frame_span # These two attributes are used during ava evaluation... # Maybe there is a better implementation self.eval_file_paths = eval_file_paths self.action_thresh = action_thresh clip2ann = defaultdict(list) if "annotations" in json_dict: for ann in json_dict["annotations"]: action_ids = ann["action_ids"] one_hot = np.zeros(81, dtype=np.bool) one_hot[action_ids] = True packed_act = np.packbits(one_hot[1:]) clip2ann[ann["image_id"]].append(dict(bbox=ann["bbox"], packed_act=packed_act)) movies_size = {} clips_info = {} for img in json_dict["images"]: mov = img["movie"] if mov not in movies_size: movies_size[mov] = [img["width"], img["height"]] clips_info[img["id"]] = [mov, img["timestamp"]] self.movie_info = NpInfoDict(movies_size, value_type=np.int32) clip_ids = sorted(list(clips_info.keys())) if remove_clips_without_annotations: clip_ids = [clip_id for clip_id in clip_ids if clip_id in clip2ann] if box_file: # this is only for validation or testing # we use detected boxes, so remove clips without boxes detected. imgToBoxes = self.load_box_file(box_file, box_thresh) clip_ids = [ img_id for img_id in clip_ids if len(imgToBoxes[img_id]) > 0 ] self.det_persons = NpBoxDict(imgToBoxes, clip_ids, value_types=[("bbox", np.float32), ("score", np.float32)]) else: self.det_persons = None self.anns = NpBoxDict(clip2ann, clip_ids, value_types=[("bbox", np.float32), ("packed_act", np.uint8)]) clips_info = { # key clip_id: [ self.movie_info.convert_key(clips_info[clip_id][0]), clips_info[clip_id][1] ] for clip_id in clip_ids } self.clips_info = NpInfoDict(clips_info, value_type=np.int32) def __getitem__(self, idx): _, clip_info = self.clips_info[idx] # mov_id is the id in self.movie_info mov_id, timestamp = clip_info # movie_id is the human-readable youtube id. movie_id, movie_size = self.movie_info[mov_id] video_data = self._decode_video_data(movie_id, timestamp) im_w, im_h = movie_size if self.det_persons is None: # Note: During training, we only use gt. Thus we should not provide box file, # otherwise we will use only box file instead. boxes, packed_act = self.anns[idx] boxes_tensor = torch.as_tensor(boxes, dtype=torch.float32).reshape(-1, 4) # guard against no boxes boxes = BoxList(boxes_tensor, (im_w, im_h), mode="xywh").convert("xyxy") # Decode the packed bits from uint8 to one hot, since AVA has 80 classes, # it can be exactly denoted with 10 bytes, otherwise we may need to discard some bits. one_hot_label = np.unpackbits(packed_act, axis=1) one_hot_label = torch.as_tensor(one_hot_label, dtype=torch.uint8) boxes.add_field("labels", one_hot_label) # 80 else: boxes, box_score = self.det_persons[idx] boxes_tensor = torch.as_tensor(boxes).reshape(-1, 4) boxes = BoxList(boxes_tensor, (im_w, im_h), mode="xywh").convert("xyxy") box_score_tensor = torch.as_tensor(box_score, dtype=torch.float32).reshape(-1, 1) boxes.add_field("scores", box_score_tensor) boxes = boxes.clip_to_image(remove_empty=True) if self.transforms is not None: video_data, boxes, transform_randoms = self.transforms(video_data, boxes) return video_data, boxes, idx def get_video_info(self, index): _, clip_info = self.clips_info[index] # mov_id is the id in self.movie_info mov_id, timestamp = clip_info # movie_id is the human-readable youtube id. movie_id, movie_size = self.movie_info[mov_id] w, h = movie_size return dict(width=w, height=h, movie=movie_id, timestamp=timestamp) def load_box_file(self, box_file, score_thresh=0.0): import json print('Loading box file into memory...') tic = time.time() with open(box_file, "r") as f: box_results = json.load(f) print('Done (t={:0.2f}s)'.format(time.time() - tic)) boxImgIds = [box['image_id'] for box in box_results] imgToBoxes = defaultdict(list) for img_id, box in zip(boxImgIds, box_results): if box['score'] >= score_thresh: imgToBoxes[img_id].append(box) return imgToBoxes def _decode_video_data(self, dirname, timestamp): # decode target video data from segment per second. video_folder = os.path.join(self.video_root, dirname) right_span = self.frame_span//2 left_span = self.frame_span - right_span #load right cur_t = timestamp right_frames = [] while len(right_frames)<right_span: video_path = os.path.join(video_folder, "{}.mp4".format(cur_t)) # frames = cv2_decode_video(video_path) frames = av_decode_video(video_path) if len(frames)==0: raise RuntimeError("Video {} cannot be decoded.".format(video_path)) right_frames = right_frames+frames cur_t += 1 #load left cur_t = timestamp-1 left_frames = [] while len(left_frames)<left_span: video_path = os.path.join(video_folder, "{}.mp4".format(cur_t)) # frames = cv2_decode_video(video_path) frames = av_decode_video(video_path) if len(frames)==0: raise RuntimeError("Video {} cannot be decoded.".format(video_path)) left_frames = frames+left_frames cur_t -= 1 #adjust key frame to center, usually no need min_frame_num = min(len(left_frames), len(right_frames)) frames = left_frames[-min_frame_num:] + right_frames[:min_frame_num] video_data = np.stack(frames) return video_data def __len__(self): return len(self.clips_info) def __repr__(self): fmt_str = 'Dataset ' + self.__class__.__name__ + '\n' fmt_str += ' Number of datapoints: {}\n'.format(self.__len__()) fmt_str += ' Video Root Location: {}\n'.format(self.video_root) tmp = ' Transforms (if any): ' fmt_str += '{0}{1}\n'.format(tmp, self.transforms.__repr__().replace('\n', '\n' + ' ' * len(tmp))) return fmt_str def build_ak_dataset(is_train,transforms): input_filename = "ak_train" if is_train else "ak_val" assert input_filename data_args = {} if is_train: data_args['video_root'] = '/mnt/cache/xingsen/ava_dataset/AVA/clips/trainval/' data_args['ava_annfile'] = "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_train_v2.2_min.json" data_args['kinetics_annfile'] = "/mnt/cache/xingsen/xingsen2/kinetics_train_v1.0.json" data_args['transforms'] = transforms data_args['remove_clips_without_annotations'] = True data_args['frame_span'] = 64 data_args["ava_eval_file_path"] = { "csv_gt_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_train_v2.2.csv", "labelmap_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_action_list_v2.2_for_activitynet_2019.pbtxt", "exclusion_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_train_excluded_timestamps_v2.2.csv", } else: data_args['video_root'] = '/mnt/cache/xingsen/ava_dataset/AVA/clips/trainval/' data_args['ava_annfile'] = "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_v2.2_min.json" data_args['kinetics_annfile'] = "/mnt/cache/xingsen/xingsen2/kinetics_val_v1.0.json" data_args['box_thresh'] = 0.8 data_args['action_thresh'] = 0. data_args['transforms'] = transforms data_args['remove_clips_without_annotations'] = True data_args['kinetics_box_file'] = '/mnt/cache/xingsen/xingsen2/kinetics_person_box.json' data_args['ava_box_file'] = '/mnt/cache/xingsen/ava_dataset/AVA/boxes/ava_val_det_person_bbox.json' data_args['frame_span'] = 64 # 64 data_args['eval_file_paths'] = { "csv_gt_file":'/mnt/cache/xingsen/xingsen2/ak_val_gt.csv', "labelmap_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_action_list_v2.2_for_activitynet_2019.pbtxt", "exclusion_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_excluded_timestamps_v2.2.csv", } data_args["ava_eval_file_path"] = { "csv_gt_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_v2.2.csv", "labelmap_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_action_list_v2.2_for_activitynet_2019.pbtxt", "exclusion_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_excluded_timestamps_v2.2.csv", } data_args['kinetics_eval_file_path'] = { "csv_gt_file" : '/mnt/cache/xingsen/xingsen2/kinetics_val_gt_v1.0.csv', "labelmap_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_action_list_v2.2_for_activitynet_2019.pbtxt", "exclusion_file": "/mnt/cache/xingsen/ava_dataset/AVA/annotations/ava_val_excluded_timestamps_v2.2.csv", } dataset = AKDataset(**data_args) return dataset movie_size = {} def check_valid(path,movie): try: reader = decord.VideoReader(path) frame = reader.get_batch([0]).asnumpy() except Exception: print(path) return False return True if __name__ == '__main__': transform_val = None dataset = build_ak_dataset(True,transform_val) for i in tqdm(range(len(dataset))): dataset[i]
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/data/ava.py
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/data/__init__.py
""" Adapted code from: @inproceedings{hara3dcnns, author={Kensho Hara and Hirokatsu Kataoka and Yutaka Satoh}, title={Can Spatiotemporal 3D CNNs Retrace the History of 2D CNNs and ImageNet?}, booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR)}, pages={6546--6555}, year={2018}, }. """ import random import numbers import collections import numpy as np import torch from PIL import Image try: import accimage except ImportError: accimage = None import imgaug.augmenters as iaa import math from .RandomAugmentBBox import RandomAugmentBBox class Compose(object): """Compose several transforms together. Args: transforms (list of ``Transform`` objects): List of transforms to compose. Example: >>> Compose([ >>> CenterCrop(10), >>> ToTensor(), >>> ]) """ def __init__(self, transforms): self.transforms = transforms def __call__(self, image, bounding_boxes=None, **kwargs): """ Args: img (PIL.Image): Image to be transformed. Returns: PIL.Image: Transformed image. """ if bounding_boxes is not None: for t in self.transforms: image, bounding_boxes = t(image, bounding_boxes, **kwargs) return image, bounding_boxes else: for t in self.transforms: image = t(image) return image def randomize_parameters(self, **kwargs): for t in self.transforms: t.randomize_parameters(**kwargs) # def set_k_for_bbox_affine_transform(self, **kwargs): # self.transforms[] def __repr__(self): return self.__class__.__name__ + '(' + repr(self.transforms) + ')' class RandomAugment(RandomAugmentBBox): def __repr__(self): return '{self.__class__.__name__}(aug_type={self.aug_type}, magnitude={self.magnitude})'.format(self=self) class ToTensor(object): """Convert a ``PIL.Image`` or ``numpy.ndarray`` to tensor. Convert a PIL.Image or numpy.ndarray (H x W x C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 255.0 / norm_value]. Args: norm_value (float, optional): Normalization constant. """ def __init__(self, norm_value=255.): self.norm_value = norm_value def __call__(self, pic, bboxes=None, **kwargs): """ Args: pic (PIL.Image or numpy.ndarray): Image to be converted to tensor. Returns: Tensor: Converted image. """ if isinstance(pic, np.ndarray): # handle numpy array img = torch.from_numpy(pic.transpose((2, 0, 1))) # backward compatibility if bboxes is not None: return img.float().div(self.norm_value), bboxes else: return img.float().div(self.norm_value) if accimage is not None and isinstance(pic, accimage.Image): nppic = np.zeros( [pic.channels, pic.height, pic.width], dtype=np.float32) pic.copyto(nppic) if bboxes is not None: return torch.from_numpy(nppic), bboxes else: return torch.from_numpy(nppic) # handle PIL Image if pic.mode == 'I': img = torch.from_numpy(np.array(pic, np.int32, copy=False)) elif pic.mode == 'I;16': img = torch.from_numpy(np.array(pic, np.int16, copy=False)) else: img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes())) # PIL image mode: 1, L, P, I, F, RGB, YCbCr, RGBA, CMYK if pic.mode == 'YCbCr': nchannel = 3 elif pic.mode == 'I;16': nchannel = 1 else: nchannel = len(pic.mode) img = img.view(pic.size[1], pic.size[0], nchannel) # put it from HWC to CHW format # yikes, this transpose takes 80% of the loading time/CPU img = img.transpose(0, 1).transpose(0, 2).contiguous() if isinstance(img, torch.ByteTensor): if bboxes is not None: return img.float().div(self.norm_value), bboxes else: return img.float().div(self.norm_value) else: if bboxes is not None: return img, bboxes else: return img def randomize_parameters(self, **kwargs): pass def __repr__(self): return '{self.__class__.__name__}(norm_value={self.norm_value})'.format(self=self) class Normalize(object): """Normalize an tensor image with mean and standard deviation. Given mean: (R, G, B) and std: (R, G, B), will normalize each channel of the torch.*Tensor, i.e. channel = (channel - mean) / std. Args: mean (sequence): Sequence of means for R, G, B channels respecitvely. std (sequence): Sequence of standard deviations for R, G, B channels respecitvely. """ def __init__(self, mean, std): self.mean = mean self.std = std def __call__(self, tensor, bboxes=None, **kwargs): """ Args: tensor (Tensor): Tensor image of size (C, H, W) to be normalized. Returns: Tensor: Normalized image. """ for t, m, s in zip(tensor, self.mean, self.std): t.sub_(m).div_(s) if bboxes is not None: return tensor, bboxes else: return tensor def randomize_parameters(self, **kwargs): pass def __repr__(self): return '{self.__class__.__name__}(mean={self.mean}, std={self.std})'.format(self=self) class Scale(object): """Rescale the input PIL.Image to the given size. Args: size (sequence or int): Desired output size. If size is a sequence like (w, h), output size will be matched to this. If size is an int, smaller edge of the image will be matched to this number. i.e, if height > width, then image will be rescaled to (size * height / width, size). interpolation (int, optional): Desired interpolation. Default is ``PIL.Image.BILINEAR``. max_ratio (float, optional): If not None, denotes maximum allowed aspect ratio after rescaling the input. """ # TO-DO: implement max_ratio with imgaug? def __init__(self, resize, interpolation='linear', max_ratio=2.42, fixed_size=False): assert isinstance(resize, int) or (isinstance(resize, collections.Iterable) and len(resize) == 2) self.resize = resize self.interpolation = interpolation self.max_size = int(max_ratio*resize) if fixed_size: self._resize = iaa.Resize(self.resize, interpolation=interpolation) else: self._resize = iaa.Resize({"shorter-side": self.resize, "longer-side": "keep-aspect-ratio"}, interpolation=interpolation) self._resize_max = iaa.Resize({"shorter-side": "keep-aspect-ratio", "longer-side": self.max_size}, interpolation=interpolation) def __call__(self, image, bounding_boxes=None, **kwargs): """ Args: img (PIL.Image): Image to be scaled. Returns: PIL.Image: Rescaled image. """ img_aug, boudingbox_aug = self._resize_det(image=image, bounding_boxes=bounding_boxes) newh, neww, _ = img_aug.shape if max(newh, neww) > self.max_size: img_aug, boudingbox_aug = self._resize_det_max(image=image, bounding_boxes=bounding_boxes) if bounding_boxes is not None: return img_aug, boudingbox_aug else: return img_aug def randomize_parameters(self, **kwargs): self._resize_det = self._resize.to_deterministic() self._resize_det_max = self._resize_max.to_deterministic() def __repr__(self): return '{self.__class__.__name__}(resize={self.resize}, interpolation={self.interpolation}, max_size={self.max_size})'.format(self=self) # TO-DO: re-implement with imgaug class CenterCrop(object): """Crop the given PIL.Image at the center. Args: size (sequence or int): Desired output size of the crop. If size is an int instead of sequence like (h, w), a square crop (size, size) is made. """ def __init__(self, size): if isinstance(size, numbers.Number): self.size = (int(size), int(size)) else: self.size = size def __call__(self, img): """ Args: img (PIL.Image): Image to be cropped. Returns: PIL.Image: Cropped image. """ w, h = img.size th, tw = self.size x1 = int(round((w - tw) / 2.)) y1 = int(round((h - th) / 2.)) return img.crop((x1, y1, x1 + tw, y1 + th)) def randomize_parameters(self): return [{'transform': 'CenterCrop', 'size': self.size}] def __repr__(self): return '{self.__class__.__name__}(size={self.size})'.format(self=self) # TO-DO: re-implement with imgaug class CornerCrop(object): """Crop the given PIL.Image at some corner or the center. Args: size (int): Desired output size of the square crop. crop_position (str, optional): Designate the position to be cropped. If is None, a random position will be selected from five choices. """ def __init__(self, size, crop_position=None): """ Args: img (PIL.Image): Image to be cropped. Returns: PIL.Image: Cropped image. """ self.size = size if crop_position is None: self.randomize = True else: self.randomize = False self.crop_position = crop_position self.crop_positions = ['c', 'tl', 'tr', 'bl', 'br'] def __call__(self, img): image_width = img.size[0] image_height = img.size[1] if self.crop_position == 'c': th, tw = (self.size, self.size) x1 = int(round((image_width - tw) / 2.)) y1 = int(round((image_height - th) / 2.)) x2 = x1 + tw y2 = y1 + th elif self.crop_position == 'tl': x1 = 0 y1 = 0 x2 = self.size y2 = self.size elif self.crop_position == 'tr': x1 = image_width - self.size y1 = 0 x2 = image_width y2 = self.size elif self.crop_position == 'bl': x1 = 0 y1 = image_height - self.size x2 = self.size y2 = image_height elif self.crop_position == 'br': x1 = image_width - self.size y1 = image_height - self.size x2 = image_width y2 = image_height img = img.crop((x1, y1, x2, y2)) return img def randomize_parameters(self, param=None): if self.randomize: self.crop_position = self.crop_positions[random.randint( 0, len(self.crop_positions) - 1)] return [{'transform': 'CornerCrop', 'crop_position': self.crop_position, 'size': self.size}] def __repr__(self): return '{self.__class__.__name__}(size={self.size}, crop_position={self.crop_position})'.format(self=self) class RandomHorizontalFlip(object): """Horizontally flip the given PIL.Image randomly with a given probability. Args: p (float, optional): Probability of flipping. """ def __init__(self, p=0.5): self.prob = p self._flip = iaa.Fliplr(self.prob) def __call__(self, image, bounding_boxes=None, **kwargs): """ Args: img (PIL.Image): Image to be flipped. Returns: PIL.Image: Randomly flipped image. """ img_aug, bboxes_aug = self._flip_det(image=image, bounding_boxes=bounding_boxes) if bounding_boxes is not None: return img_aug, bboxes_aug else: return img_aug def randomize_parameters(self, **kwargs): self._flip_det = self._flip.to_deterministic() def __repr__(self): return '{self.__class__.__name__}(prob={self.prob})'.format(self=self) # TO-DO: re-implement with imgaug class ScaleJitteringRandomCrop(object): """Randomly rescale the given PIL.Image and then take a random crop. Args: min_scale (int): Minimum scale for random rescaling. max_scale (int): Maximum scale for random rescaling. size (int): Desired output size of the square crop. interpolation (int, optional): Desired interpolation. Default is ``PIL.Image.BILINEAR``. """ def __init__(self, min_size, max_size, size, interpolation='linear'): self.min_size = min_size self.max_size = max_size self.size = size self.interpolation = interpolation self._resize = iaa.Resize({"shorter-side": (self.min_size, self.max_size), "longer-side": "keep-aspect-ratio"}, interpolation=interpolation) self._crop = iaa.CropToFixedSize(width=self.size, height=self.size) def __call__(self, image, bounding_boxes=None, **kwargs): """ Args: img (PIL.Image): Image to be scaled. Returns: PIL.Image: Rescaled image. """ img_aug, boudingbox_aug = self._resize_det(image=image, bounding_boxes=bounding_boxes) img_aug, boudingbox_aug = self._crop_det(image=img_aug, bounding_boxes=boudingbox_aug) if bounding_boxes is not None: return img_aug, boudingbox_aug else: return img_aug def randomize_parameters(self, **kwargs): self._resize_det = self._resize.to_deterministic() self._crop_det = self._crop.to_deterministic() def __repr__(self): return '{self.__class__.__name__}(min_size={self.min_size}, max_size={self.max_size}, size={self.size}, interpolation={self.interpolation})'.format(self=self) class CropBBoxRescale(object): """Crop region within the bbox and resize to fixed size. Args: min_scale (int): Minimum scale for random rescaling. max_scale (int): Maximum scale for random rescaling. size (int): Desired output size of the square crop. interpolation (int, optional): Desired interpolation. Default is ``PIL.Image.BILINEAR``. """ def __init__(self, resize, bbox_scales=None, interpolation='linear'): if bbox_scales is None: bbox_scales = [1, 1.5] self.size = resize self.interpolation = interpolation self._resize = iaa.Resize(resize, interpolation=interpolation) self.scale_list = bbox_scales def __call__(self, image, bounding_boxes=None, **kwargs): """ Args: img (PIL.Image): Image to be scaled. Returns: PIL.Image: Rescaled image. """ h, w, _ = image.shape bbox = bounding_boxes[0] bbox = scale_box(bbox, self.scale) bbox = clip_box_to_image(bbox, h, w) x1, y1, x2, y2 = round_down(bbox[0]), round_down(bbox[1]), round_down(bbox[2]), round_down(bbox[3]) # Crop region inside the bbox img_cropped = image[y1:y2, x1:x2,:] # Resize cropped image to fixed size img_aug = self._resize_det(image=img_cropped) # if bounding_boxes is not None: # return img_aug, boudingbox_aug # else: return img_aug, bounding_boxes def randomize_parameters(self, **kwargs): self.scale = random.uniform(self.scale_list[0], self.scale_list[1]) self._resize_det = self._resize.to_deterministic() def __repr__(self): return '{self.__class__.__name__}(size={self.size}, interpolation={self.interpolation})'.format(self=self) def round_down(x): return int(math.floor(x)) def clip_box_to_image(box, height, width): """Clip an box to an image with the given height and width.""" box[[0, 2]] = np.minimum(width - 1., np.maximum(0., box[[0, 2]])) box[[1, 3]] = np.minimum(height - 1., np.maximum(0., box[[1, 3]])) return box def scale_box(box, scale): """ box: (4,) [w1, h1, w2, h2] """ w1, h1, w2, h2 = box.x1, box.y1, box.x2, box.y2 center_w = (w1 + w2) / 2 center_h = (h1 + h2) / 2 half_w = center_w - w1 half_h = center_h - h1 half_w = scale * half_w half_h = scale * half_h new_box = np.array([center_w-half_w, center_h-half_h, center_w+half_w, center_h+half_h]) return new_box
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/data/spatial_transforms.py
# Copyright 2018 The TensorFlow Authors All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """Transforms used in the Augmentation Policies.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import random import numpy as np # pylint:disable=g-multiple-import from PIL import ImageOps, ImageEnhance, ImageFilter, Image # pylint:enable=g-multiple-import # IMAGE_SIZE = 32 # What is the dataset mean and std of the images on the training set # MEANS = [0.49139968, 0.48215841, 0.44653091] # STDS = [0.24703223, 0.24348513, 0.26158784] PARAMETER_MAX = 10 # What is the max 'level' a transform could be predicted def random_flip(x): """Flip the input x horizontally with 50% probability.""" if np.random.rand(1)[0] > 0.5: return np.fliplr(x) return x def zero_pad_and_crop(img, amount=4): """Zero pad by `amount` zero pixels on each side then take a random crop. Args: img: numpy image that will be zero padded and cropped. amount: amount of zeros to pad `img` with horizontally and verically. Returns: The cropped zero padded img. The returned numpy array will be of the same shape as `img`. """ padded_img = np.zeros( (img.shape[0] + amount * 2, img.shape[1] + amount * 2, img.shape[2])) padded_img[amount:img.shape[0] + amount, amount:img.shape[1] + amount, :] = img top = np.random.randint(low=0, high=2 * amount) left = np.random.randint(low=0, high=2 * amount) new_img = padded_img[top:top + img.shape[0], left:left + img.shape[1], :] return new_img def create_cutout_mask(img_height, img_width, num_channels, size): """Creates a zero mask used for cutout of shape `img_height` x `img_width`. Args: img_height: Height of image cutout mask will be applied to. img_width: Width of image cutout mask will be applied to. num_channels: Number of channels in the image. size: Size of the zeros mask. Returns: A mask of shape `img_height` x `img_width` with all ones except for a square of zeros of shape `size` x `size`. This mask is meant to be elementwise multiplied with the original image. Additionally returns the `upper_coord` and `lower_coord` which specify where the cutout mask will be applied. """ assert img_height == img_width # Sample center where cutout mask will be applied height_loc = np.random.randint(low=0, high=img_height) width_loc = np.random.randint(low=0, high=img_width) # Determine upper right and lower left corners of patch upper_coord = (max(0, height_loc - size // 2), max(0, width_loc - size // 2)) lower_coord = (min(img_height, height_loc + size // 2), min(img_width, width_loc + size // 2)) mask_height = lower_coord[0] - upper_coord[0] mask_width = lower_coord[1] - upper_coord[1] assert mask_height > 0 assert mask_width > 0 mask = np.ones((img_height, img_width, num_channels)) mask[upper_coord[0]:lower_coord[0], upper_coord[1]:lower_coord[1], :] = 0 return mask, upper_coord, lower_coord def cutout_numpy(img, size=16): """Apply cutout with mask of shape `size` x `size` to `img`. The cutout operation is from the paper https://arxiv.org/abs/1708.04552. This operation applies a `size`x`size` mask of zeros to a random location within `img`. Args: img: Numpy image that cutout will be applied to. size: Height/width of the cutout mask that will be Returns: A numpy tensor that is the result of applying the cutout mask to `img`. """ if size <= 0: return img assert len(img.shape) == 3 img_height, img_width, num_channels = img.shape mask = create_cutout_mask(img_height, img_width, num_channels, size)[0] return img * mask def float_parameter(level, maxval): """Helper function to scale `val` between 0 and maxval . Args: level: Level of the operation that will be between [0, `PARAMETER_MAX`]. maxval: Maximum value that the operation can have. This will be scaled to level/PARAMETER_MAX. Returns: A float that results from scaling `maxval` according to `level`. """ return float(level) * maxval / PARAMETER_MAX def int_parameter(level, maxval): """Helper function to scale `val` between 0 and maxval . Args: level: Level of the operation that will be between [0, `PARAMETER_MAX`]. maxval: Maximum value that the operation can have. This will be scaled to level/PARAMETER_MAX. Returns: An int that results from scaling `maxval` according to `level`. """ return int(level * maxval / PARAMETER_MAX) def pil_wrap(img, mean=0.5, std=0.5): """Convert the `img` numpy tensor to a PIL Image.""" return Image.fromarray(np.uint8((img * std + mean) * 255.0)).convert('RGBA') def pil_unwrap(pil_img, mean=0.5, std=0.5): """Converts the PIL img to a numpy array.""" s = pil_img.size pic_array = (np.array(pil_img.getdata()).reshape((s[0], s[1], 4)) / 255.0) i1, i2 = np.where(pic_array[:, :, 3] == 0) pic_array = (pic_array[:, :, :3] - mean) / std pic_array[i1, i2] = [0, 0, 0] return pic_array def apply_policy(policy, img): """Apply the `policy` to the numpy `img`. Args: policy: A list of tuples with the form (name, probability, level) where `name` is the name of the augmentation operation to apply, `probability` is the probability of applying the operation and `level` is what strength the operation to apply. img: Numpy image that will have `policy` applied to it. Returns: The result of applying `policy` to `img`. """ pil_img = pil_wrap(img) for xform in policy: assert len(xform) == 3 name, probability, level = xform xform_fn = NAME_TO_TRANSFORM[name].pil_transformer(probability, level) pil_img = xform_fn(pil_img) return pil_unwrap(pil_img) class TransformFunction(object): """Wraps the Transform function for pretty printing options.""" def __init__(self, func, name): self.f = func self.name = name def __repr__(self): return '<' + self.name + '>' def __call__(self, pil_img): return self.f(pil_img) class TransformT(object): """Each instance of this class represents a specific transform.""" def __init__(self, name, xform_fn): self.name = name self.xform = xform_fn def pil_transformer(self, probability, level): def return_function(im): if random.random() < probability: im = self.xform(im, level) return im name = self.name + '({:.1f},{})'.format(probability, level) return TransformFunction(return_function, name) def do_transform(self, image, level): f = self.pil_transformer(PARAMETER_MAX, level) return pil_unwrap(f(pil_wrap(image))) ################## Transform Functions ################## identity = TransformT('Identity', lambda pil_img, level: pil_img) flip_lr = TransformT( 'FlipLR', lambda pil_img, level: pil_img.transpose(Image.FLIP_LEFT_RIGHT)) flip_ud = TransformT( 'FlipUD', lambda pil_img, level: pil_img.transpose(Image.FLIP_TOP_BOTTOM)) # pylint:disable=g-long-lambda auto_contrast = TransformT( 'AutoContrast', lambda pil_img, level: ImageOps.autocontrast( pil_img.convert('RGB')).convert('RGBA')) equalize = TransformT( 'Equalize', lambda pil_img, level: ImageOps.equalize( pil_img.convert('RGB')).convert('RGBA')) invert = TransformT( 'Invert', lambda pil_img, level: ImageOps.invert(pil_img.convert('RGB')). convert('RGBA')) # pylint:enable=g-long-lambda blur = TransformT('Blur', lambda pil_img, level: pil_img.filter(ImageFilter.BLUR)) smooth = TransformT('Smooth', lambda pil_img, level: pil_img.filter(ImageFilter.SMOOTH)) def _rotate_impl(pil_img, level): """Rotates `pil_img` from -30 to 30 degrees depending on `level`.""" degrees = int_parameter(level, 30) if random.random() > 0.5: degrees = -degrees return pil_img.rotate(degrees) rotate = TransformT('Rotate', _rotate_impl) def _posterize_impl(pil_img, level): """Applies PIL Posterize to `pil_img`.""" level = int_parameter(level, 4) return ImageOps.posterize(pil_img.convert('RGB'), 4 - level).convert('RGBA') posterize = TransformT('Posterize', _posterize_impl) def _shear_x_impl(pil_img, level): """Applies PIL ShearX to `pil_img`. The ShearX operation shears the image along the horizontal axis with `level` magnitude. Args: pil_img: Image in PIL object. level: Strength of the operation specified as an Integer from [0, `PARAMETER_MAX`]. Returns: A PIL Image that has had ShearX applied to it. """ level = float_parameter(level, 0.3) if random.random() > 0.5: level = -level return pil_img.transform(pil_img.size, Image.AFFINE, (1, level, 0, 0, 1, 0)) shear_x = TransformT('ShearX', _shear_x_impl) def _shear_y_impl(pil_img, level): """Applies PIL ShearY to `pil_img`. The ShearY operation shears the image along the vertical axis with `level` magnitude. Args: pil_img: Image in PIL object. level: Strength of the operation specified as an Integer from [0, `PARAMETER_MAX`]. Returns: A PIL Image that has had ShearX applied to it. """ level = float_parameter(level, 0.3) if random.random() > 0.5: level = -level return pil_img.transform(pil_img.size, Image.AFFINE, (1, 0, 0, level, 1, 0)) shear_y = TransformT('ShearY', _shear_y_impl) def _translate_x_impl(pil_img, level): """Applies PIL TranslateX to `pil_img`. Translate the image in the horizontal direction by `level` number of pixels. Args: pil_img: Image in PIL object. level: Strength of the operation specified as an Integer from [0, `PARAMETER_MAX`]. Returns: A PIL Image that has had TranslateX applied to it. """ level = int_parameter(level, 10) if random.random() > 0.5: level = -level return pil_img.transform(pil_img.size, Image.AFFINE, (1, 0, level, 0, 1, 0)) translate_x = TransformT('TranslateX', _translate_x_impl) def _translate_y_impl(pil_img, level): """Applies PIL TranslateY to `pil_img`. Translate the image in the vertical direction by `level` number of pixels. Args: pil_img: Image in PIL object. level: Strength of the operation specified as an Integer from [0, `PARAMETER_MAX`]. Returns: A PIL Image that has had TranslateY applied to it. """ level = int_parameter(level, 10) if random.random() > 0.5: level = -level return pil_img.transform(pil_img.size, Image.AFFINE, (1, 0, 0, 0, 1, level)) translate_y = TransformT('TranslateY', _translate_y_impl) def _crop_impl(pil_img, level, interpolation=Image.BILINEAR): """Applies a crop to `pil_img` with the size depending on the `level`.""" cropped = pil_img.crop( (level, level, pil_img.size[0] - level, pil_img.size[1] - level)) resized = cropped.resize(pil_img.size, interpolation) return resized crop_bilinear = TransformT('CropBilinear', _crop_impl) def _solarize_impl(pil_img, level): """Applies PIL Solarize to `pil_img`. Translate the image in the vertical direction by `level` number of pixels. Args: pil_img: Image in PIL object. level: Strength of the operation specified as an Integer from [0, `PARAMETER_MAX`]. Returns: A PIL Image that has had Solarize applied to it. """ level = int_parameter(level, 256) return ImageOps.solarize(pil_img.convert('RGB'), 256 - level).convert('RGBA') solarize = TransformT('Solarize', _solarize_impl) def _cutout_pil_impl(pil_img, level): """Apply cutout to pil_img at the specified level.""" size = int_parameter(level, 20) if size <= 0: return pil_img img_width, img_height = pil_img.size num_channels = 3 _, upper_coord, lower_coord = ( create_cutout_mask(img_height, img_width, num_channels, size)) pixels = pil_img.load() # create the pixel map for i in range(upper_coord[0], lower_coord[0]): # for every col: for j in range(upper_coord[1], lower_coord[1]): # For every row pixels[i, j] = (127, 127, 127, 0) # set the colour accordingly return pil_img cutout = TransformT('Cutout', _cutout_pil_impl) def _enhancer_impl(enhancer): """Sets level to be between 0.1 and 1.8 for ImageEnhance transforms of PIL.""" def impl(pil_img, level): v = float_parameter(level, 1.8) + .1 # going to 0 just destroys it return enhancer(pil_img).enhance(v) return impl color = TransformT('Color', _enhancer_impl(ImageEnhance.Color)) contrast = TransformT('Contrast', _enhancer_impl(ImageEnhance.Contrast)) brightness = TransformT('Brightness', _enhancer_impl(ImageEnhance.Brightness)) sharpness = TransformT('Sharpness', _enhancer_impl(ImageEnhance.Sharpness)) ALL_TRANSFORMS = [ identity, flip_lr, flip_ud, auto_contrast, equalize, invert, rotate, posterize, crop_bilinear, solarize, color, contrast, brightness, sharpness, shear_x, shear_y, translate_x, translate_y, cutout, blur, smooth ] NAME_TO_TRANSFORM = {t.name: t for t in ALL_TRANSFORMS} TRANSFORM_NAMES = NAME_TO_TRANSFORM.keys()
InternVideo-main
Downstream/Spatial-Temporal-Action-Localization/data/augmentations.py
# python imports import argparse import os import time import datetime from pprint import pprint # torch imports import torch import torch.nn as nn import torch.utils.data # for visualization from torch.utils.tensorboard import SummaryWriter # our code from libs.core import load_config from libs.datasets import make_dataset, make_data_loader from libs.modeling import make_meta_arch from libs.utils import (train_one_epoch, valid_one_epoch, ANETdetection, save_checkpoint, make_optimizer, make_scheduler, fix_random_seed, ModelEma) ################################################################################ def main(args): """main function that handles training / inference""" """1. setup parameters / folders""" # parse args args.start_epoch = 0 if os.path.isfile(args.config): cfg = load_config(args.config) else: raise ValueError("Config file does not exist.") pprint(cfg) # prep for output folder (based on time stamp) if not os.path.exists(cfg['output_folder']): os.makedirs(cfg['output_folder']) cfg_filename = os.path.basename(args.config).replace('.yaml', '') if len(args.output) == 0: ts = datetime.datetime.fromtimestamp(int(time.time())) ckpt_folder = os.path.join( cfg['output_folder'], cfg_filename + '_' + str(ts)) else: ckpt_folder = os.path.join( cfg['output_folder'], cfg_filename + '_' + str(args.output)+'_'+str(cfg['loader']['batch_size'])+'_'+str(cfg['opt']['learning_rate'])) if not os.path.exists(ckpt_folder): os.mkdir(ckpt_folder) # tensorboard writer tb_writer = SummaryWriter(os.path.join(ckpt_folder, 'logs')) # fix the random seeds (this will fix everything) rng_generator = fix_random_seed(cfg['init_rand_seed'], include_cuda=True) # re-scale learning rate / # workers based on number of GPUs cfg['opt']["learning_rate"] *= len(cfg['devices']) cfg['loader']['num_workers'] *= len(cfg['devices']) """2. create dataset / dataloader""" train_dataset = make_dataset( cfg['dataset_name'], True, cfg['train_split'], **cfg['dataset'] ) # update cfg based on dataset attributes (fix to epic-kitchens) train_db_vars = train_dataset.get_attributes() cfg['model']['train_cfg']['head_empty_cls'] = train_db_vars['empty_label_ids'] # data loaders train_loader = make_data_loader( train_dataset, True, rng_generator, **cfg['loader']) """3. create model, optimizer, and scheduler""" # model model = make_meta_arch(cfg['model_name'], **cfg['model']) # not ideal for multi GPU training, ok for now model = nn.DataParallel(model, device_ids=cfg['devices']) # optimizer optimizer = make_optimizer(model, cfg['opt']) # schedule num_iters_per_epoch = len(train_loader) scheduler = make_scheduler(optimizer, cfg['opt'], num_iters_per_epoch) # enable model EMA print("Using model EMA ...") model_ema = ModelEma(model) """4. Resume from model / Misc""" # resume from a checkpoint? if args.resume: if os.path.isfile(args.resume): # load ckpt, reset epoch / best rmse checkpoint = torch.load(args.resume, map_location = lambda storage, loc: storage.cuda( cfg['devices'][0])) args.start_epoch = checkpoint['epoch'] + 1 model.load_state_dict(checkpoint['state_dict']) model_ema.module.load_state_dict(checkpoint['state_dict_ema']) # also load the optimizer / scheduler if necessary optimizer.load_state_dict(checkpoint['optimizer']) scheduler.load_state_dict(checkpoint['scheduler']) print("=> loaded checkpoint '{:s}' (epoch {:d}".format( args.resume, checkpoint['epoch'] )) del checkpoint else: print("=> no checkpoint found at '{}'".format(args.resume)) return # save the current config with open(os.path.join(ckpt_folder, 'config.txt'), 'w') as fid: pprint(cfg, stream=fid) fid.flush() """4. training / validation loop""" print("\nStart training model {:s} ...".format(cfg['model_name'])) # start training max_epochs = cfg['opt'].get( 'early_stop_epochs', cfg['opt']['epochs'] + cfg['opt']['warmup_epochs'] ) """2. create dataset / dataloader""" val_dataset = make_dataset( cfg['dataset_name'], False, cfg['val_split'], **cfg['dataset'] ) # set bs = 1, and disable shuffle val_loader = make_data_loader( val_dataset, False, None, 1, cfg['loader']['num_workers'] ) for epoch in range(args.start_epoch, max_epochs): # train for one epoch train_one_epoch( train_loader, model, optimizer, scheduler, epoch, model_ema = model_ema, clip_grad_l2norm = cfg['train_cfg']['clip_grad_l2norm'], tb_writer=tb_writer, print_freq=args.print_freq ) if epoch>=5:#(max_epochs//4): # if epoch>1:#(max_epochs//3): # model model_eval = make_meta_arch(cfg['model_name'], **cfg['model']) # not ideal for multi GPU training, ok for now model_eval = nn.DataParallel(model_eval, device_ids=cfg['devices']) model_eval.load_state_dict(model_ema.module.state_dict()) # set up evaluator det_eval, output_file = None, None # if not args.saveonly: val_db_vars = val_dataset.get_attributes() det_eval = ANETdetection( val_dataset.json_file, val_dataset.split[0], tiou_thresholds = val_db_vars['tiou_thresholds'] ) # else: # output_file = os.path.join('eval_results.pkl') """5. Test the model""" print("\nStart testing model {:s} ...".format(cfg['model_name'])) start = time.time() mAP = valid_one_epoch( val_loader, model_eval, -1, evaluator=det_eval, output_file=output_file, ext_score_file=cfg['test_cfg']['ext_score_file'], tb_writer=None, print_freq=999999 #args.print_freq ) end = time.time() # print("All done! Total time: {:0.2f} sec".format(end - start)) print(epoch,mAP) save_states = { 'epoch': epoch, 'state_dict': model.state_dict(), 'scheduler': scheduler.state_dict(), 'optimizer': optimizer.state_dict(), } save_states['state_dict_ema'] = model_ema.module.state_dict() save_checkpoint( save_states, False, file_folder=ckpt_folder, file_name='epoch_{:03d}_{:.5f}.pth.tar'.format(epoch,mAP) ) # wrap up tb_writer.close() print("All done!") return ################################################################################ if __name__ == '__main__': """Entry Point""" # the arg parser parser = argparse.ArgumentParser( description='Train a point-based transformer for action localization') parser.add_argument('config', metavar='DIR', help='path to a config file') parser.add_argument('-p', '--print-freq', default=10, type=int, help='print frequency (default: 10 iterations)') parser.add_argument('-c', '--ckpt-freq', default=5, type=int, help='checkpoint frequency (default: every 5 epochs)') parser.add_argument('--output', default='', type=str, help='name of exp folder (default: none)') parser.add_argument('--resume', default='', type=str, metavar='PATH', help='path to a checkpoint (default: none)') args = parser.parse_args() main(args)
InternVideo-main
Downstream/Temporal-Action-Localization/train_eval.py
import yaml DEFAULTS = { # random seed for reproducibility, a large number is preferred "init_rand_seed": 1234567891, # dataset loader, specify the dataset here "dataset_name": "epic", "devices": ['cuda:0'], # default: single gpu "train_split": ('training', ), "val_split": ('validation', ), "model_name": "LocPointTransformer", "dataset": { # temporal stride of the feats "feat_stride": 16, # number of frames for each feat "num_frames": 32, # default fps, may vary across datasets; Set to none for read from json file "default_fps": None, # input feat dim "input_dim": 2304, # number of classes "num_classes": 97, # downsampling rate of features, 1 to use original resolution "downsample_rate": 1, # max sequence length during training "max_seq_len": 2304, # threshold for truncating an action "trunc_thresh": 0.5, # set to a tuple (e.g., (0.9, 1.0)) to enable random feature cropping # might not be implemented by the dataloader "crop_ratio": None, # if true, force upsampling of the input features into a fixed size # only used for ActivityNet "force_upsampling": False, }, "loader": { "batch_size": 8, "num_workers": 4, }, # network architecture "model": { # type of backbone (convTransformer | conv) "backbone_type": 'convTransformer', # type of FPN (fpn | identity) "fpn_type": "identity", "backbone_arch": (2, 2, 5), # scale factor between pyramid levels "scale_factor": 2, # regression range for pyramid levels "regression_range": [(0, 4), (4, 8), (8, 16), (16, 32), (32, 64), (64, 10000)], # number of heads in self-attention "n_head": 4, # window size for self attention; <=1 to use full seq (ie global attention) "n_mha_win_size": -1, # kernel size for embedding network "embd_kernel_size": 3, # (output) feature dim for embedding network "embd_dim": 512, # if attach group norm to embedding network "embd_with_ln": True, # feat dim for FPN "fpn_dim": 512, # if add ln at the end of fpn outputs "fpn_with_ln": True, # feat dim for head "head_dim": 512, # kernel size for reg/cls/center heads "head_kernel_size": 3, # number of layers in the head (including the final one) "head_num_layers": 3, # if attach group norm to heads "head_with_ln": True, # defines the max length of the buffered points "max_buffer_len_factor": 6.0, # disable abs position encoding (added to input embedding) "use_abs_pe": False, # use rel position encoding (added to self-attention) "use_rel_pe": False, }, "train_cfg": { # radius | none (if to use center sampling) "center_sample": "radius", "center_sample_radius": 1.5, "loss_weight": 1.0, # on reg_loss, use -1 to enable auto balancing "cls_prior_prob": 0.01, "init_loss_norm": 2000, # gradient cliping, not needed for pre-LN transformer "clip_grad_l2norm": -1, # cls head without data (a fix to epic-kitchens / thumos) "head_empty_cls": [], # dropout ratios for tranformers "dropout": 0.0, # ratio for drop path "droppath": 0.1, # if to use label smoothing (>0.0) "label_smoothing": 0.0, }, "test_cfg": { "pre_nms_thresh": 0.001, "pre_nms_topk": 5000, "iou_threshold": 0.1, "min_score": 0.01, "max_seg_num": 1000, "nms_method": 'soft', # soft | hard | none "nms_sigma" : 0.5, "duration_thresh": 0.05, "multiclass_nms": True, "ext_score_file": None, "voting_thresh" : 0.75, }, # optimizer (for training) "opt": { # solver "type": "AdamW", # SGD or AdamW # solver params "momentum": 0.9, "weight_decay": 0.0, "learning_rate": 1e-3, # excluding the warmup epochs "epochs": 30, # lr scheduler: cosine / multistep "warmup": True, "warmup_epochs": 5, "schedule_type": "cosine", # in #epochs excluding warmup "schedule_steps": [], "schedule_gamma": 0.1, } } def _merge(src, dst): for k, v in src.items(): if k in dst: if isinstance(v, dict): _merge(src[k], dst[k]) else: dst[k] = v def load_default_config(): config = DEFAULTS return config def _update_config(config): # fill in derived fields config["model"]["input_dim"] = config["dataset"]["input_dim"] config["model"]["num_classes"] = config["dataset"]["num_classes"] config["model"]["max_seq_len"] = config["dataset"]["max_seq_len"] config["model"]["train_cfg"] = config["train_cfg"] config["model"]["test_cfg"] = config["test_cfg"] return config def load_config(config_file, defaults=DEFAULTS): with open(config_file, "r") as fd: config = yaml.load(fd, Loader=yaml.FullLoader) _merge(defaults, config) config = _update_config(config) return config
InternVideo-main
Downstream/Temporal-Action-Localization/libs/core/config.py
from .config import load_default_config, load_config __all__ = ['load_default_config', 'load_config']
InternVideo-main
Downstream/Temporal-Action-Localization/libs/core/__init__.py
import os import json import numpy as np import torch from torch.utils.data import Dataset from torch.nn import functional as F from .datasets import register_dataset from .data_utils import truncate_feats import pickle import io from petrel_client.client import Client client = Client() @register_dataset("thumos") class THUMOS14Dataset(Dataset): def __init__( self, is_training, # if in training mode split, # split, a tuple/list allowing concat of subsets feat_folder, # folder for features json_file, # json file for annotations feat_stride, # temporal stride of the feats num_frames, # number of frames for each feat default_fps, # default fps downsample_rate, # downsample rate for feats max_seq_len, # maximum sequence length during training trunc_thresh, # threshold for truncate an action segment crop_ratio, # a tuple (e.g., (0.9, 1.0)) for random cropping input_dim, # input feat dim num_classes, # number of action categories file_prefix, # feature file prefix if any file_ext, # feature file extension if any force_upsampling # force to upsample to max_seq_len ): # file path # # assert os.path.exists(feat_folder) and os.path.exists(json_file) # assert client.isdir(feat_folder) and os.path.exists(json_file) assert isinstance(split, tuple) or isinstance(split, list) assert crop_ratio == None or len(crop_ratio) == 2 self.feat_folder = feat_folder if file_prefix is not None: self.file_prefix = file_prefix else: self.file_prefix = '' self.file_ext = file_ext self.json_file = json_file # split / training mode self.split = split self.is_training = is_training # features meta info self.feat_stride = feat_stride self.num_frames = num_frames self.input_dim = input_dim self.default_fps = default_fps self.downsample_rate = downsample_rate self.max_seq_len = max_seq_len self.trunc_thresh = trunc_thresh self.num_classes = num_classes self.label_dict = None self.crop_ratio = crop_ratio # load database and select the subset dict_db, label_dict = self._load_json_db(self.json_file) assert len(label_dict) == num_classes self.data_list = dict_db self.label_dict = label_dict # dataset specific attributes self.db_attributes = { 'dataset_name': 'thumos-14', 'tiou_thresholds': np.linspace(0.3, 0.7, 5), # we will mask out cliff diving 'empty_label_ids': [], } def get_attributes(self): return self.db_attributes def _load_json_db(self, json_file): # load database and select the subset with open(json_file, 'r') as fid: json_data = json.load(fid) json_db = json_data['database'] # if label_dict is not available if self.label_dict is None: label_dict = {} for key, value in json_db.items(): for act in value['annotations']: label_dict[act['label']] = act['label_id'] # fill in the db (immutable afterwards) dict_db = tuple() for key, value in json_db.items(): # skip the video if not in the split if value['subset'].lower() not in self.split: continue # or does not have the feature file feat_file = os.path.join(self.feat_folder, self.file_prefix + key + self.file_ext) if not os.path.exists(feat_file): # if not client.contains(feat_file): continue # if not os.path.exists(os.path.join('/mnt/petrelfs/liuyi/tsp_th14_mae_h',self.file_prefix + key+'.pkl')): # continue # get fps if available if self.default_fps is not None: fps = self.default_fps elif 'fps' in value: fps = value['fps'] else: assert False, "Unknown video FPS." # get video duration if available if 'duration' in value: duration = value['duration'] else: duration = 1e8 # get annotations if available if ('annotations' in value) and (len(value['annotations']) > 0): # a fun fact of THUMOS: cliffdiving (4) is a subset of diving (7) # our code can now handle this corner case segments, labels = [], [] for act in value['annotations']: segments.append(act['segment']) labels.append([label_dict[act['label']]]) segments = np.asarray(segments, dtype=np.float32) labels = np.squeeze(np.asarray(labels, dtype=np.int64), axis=1) else: segments = None labels = None dict_db += ({'id': key, 'fps' : fps, 'duration' : duration, 'segments' : segments, 'labels' : labels }, ) return dict_db, label_dict def __len__(self): return len(self.data_list) def __getitem__(self, idx): # directly return a (truncated) data point (so it is very fast!) # auto batching will be disabled in the subsequent dataloader # instead the model will need to decide how to batch / preporcess the data video_item = self.data_list[idx] # load features filename = os.path.join(self.feat_folder, self.file_prefix + video_item['id'] + self.file_ext) feats_i3d = np.load(filename).astype(np.float32) # feats_mae=pickle.load(io.BytesIO(client.get(os.path.join('s3://video_pub/thumos_feature/stride_8',video_item['id']+'.pkl'))) ) # feats_mae=np.load(io.BytesIO(client.get(os.path.join('s3://video_pub/thumos_feature/clip_16_4/',video_item['id']+'.npy'))) ).astype(np.float32) # feats_mae=pickle.load(io.BytesIO(client.get(os.path.join('s3://video_pub/thumos_feature/',video_item['id']+'.pkl'))) ) feats_mae=np.load(os.path.join('/mnt/petrelfs/liuyi/data_feature/data_th14/th14_mae_g_16_4',video_item['id']+'.npy')).astype(np.float32) # feats_mae=pickle.load(io.BytesIO(client.get(os.path.join('/mnt/petrelfs/liuyi/tsp_th14_mae_h',video_item['id']+'.pkl'))) ) feats_mae = F.interpolate(torch.from_numpy(feats_mae).permute(1,0).unsqueeze(0), size=feats_i3d.shape[0], mode='linear',align_corners=False)[0,...].permute(1,0) feats_mae = feats_mae.numpy() # feats_clip=np.load(io.BytesIO(client.get(os.path.join('/mnt/petrelfs/liuyi/th14_clipk700_16_4',video_item['id']+'.npy'))) ).astype(np.float32) # feats_clip = F.interpolate(torch.from_numpy(feats_clip).permute(1,0).unsqueeze(0), size=feats_i3d.shape[0], mode='linear',align_corners=False)[0,...].permute(1,0) # feats_clip = feats_clip.numpy() # feats = np.concatenate([feats_clip, feats_mae], axis=1) feats=feats_mae # feats=feats_clip # feats = feats_i3d[:,0:1024] # feats=feats_i3d[:,1024:] # deal with downsampling (= increased feat stride) feats = feats[::self.downsample_rate, :] feat_stride = self.feat_stride * self.downsample_rate # T x C -> C x T feats = torch.from_numpy(np.ascontiguousarray(feats.transpose())) # convert time stamp (in second) into temporal feature grids # ok to have small negative values here if video_item['segments'] is not None: segments = torch.from_numpy( (video_item['segments'] * video_item['fps'] - 0.5 * self.num_frames) / feat_stride ) labels = torch.from_numpy(video_item['labels']) else: segments, labels = None, None # return a data dict data_dict = {'video_id' : video_item['id'], 'feats' : feats, # C x T 'segments' : segments, # N x 2 'labels' : labels, # N 'fps' : video_item['fps'], 'duration' : video_item['duration'], 'feat_stride' : feat_stride, 'feat_num_frames' : self.num_frames} # truncate the features during training if self.is_training and (segments is not None): data_dict = truncate_feats( data_dict, self.max_seq_len, self.trunc_thresh, self.crop_ratio ) return data_dict
InternVideo-main
Downstream/Temporal-Action-Localization/libs/datasets/thumos14.py
import os import torch from .data_utils import trivial_batch_collator, worker_init_reset_seed datasets = {} def register_dataset(name): def decorator(cls): datasets[name] = cls return cls return decorator def make_dataset(name, is_training, split, **kwargs): """ A simple dataset builder """ dataset = datasets[name](is_training, split, **kwargs) return dataset def make_data_loader(dataset, is_training, generator, batch_size, num_workers): """ A simple dataloder builder """ loader = torch.utils.data.DataLoader( dataset, batch_size=batch_size, num_workers=num_workers, collate_fn=trivial_batch_collator, worker_init_fn=(worker_init_reset_seed if is_training else None), shuffle=is_training, drop_last=is_training, generator=generator, persistent_workers=True ) return loader
InternVideo-main
Downstream/Temporal-Action-Localization/libs/datasets/datasets.py
from .data_utils import worker_init_reset_seed, truncate_feats from .datasets import make_dataset, make_data_loader from . import thumos14, anet # other datasets go here __all__ = ['worker_init_reset_seed', 'truncate_feats', 'make_dataset', 'make_data_loader']
InternVideo-main
Downstream/Temporal-Action-Localization/libs/datasets/__init__.py
import os import copy import random import numpy as np import random import torch def trivial_batch_collator(batch): """ A batch collator that does nothing """ return batch def worker_init_reset_seed(worker_id): """ Reset random seed for each worker """ seed = torch.initial_seed() % 2 ** 31 np.random.seed(seed) random.seed(seed) os.environ["PYTHONHASHSEED"] = str(seed) def truncate_feats( data_dict, max_seq_len, trunc_thresh, crop_ratio=None, max_num_trials=200, has_action=True, no_trunc=False ): """ Truncate feats and time stamps in a dict item data_dict = {'video_id' : str 'feats' : Tensor C x T 'segments' : Tensor N x 2 (in feature grid) 'labels' : Tensor N 'fps' : float 'feat_stride' : int 'feat_num_frames' : in """ # get the meta info feat_len = data_dict['feats'].shape[1] num_segs = data_dict['segments'].shape[0] # seq_len < max_seq_len if feat_len <= max_seq_len: # do nothing if crop_ratio == None: return data_dict # randomly crop the seq by setting max_seq_len to a value in [l, r] else: max_seq_len = random.randint( max(round(crop_ratio[0] * feat_len), 1), min(round(crop_ratio[1] * feat_len), feat_len), ) # # corner case if feat_len == max_seq_len: return data_dict # otherwise, deep copy the dict data_dict = copy.deepcopy(data_dict) # try a few times till a valid truncation with at least one action for _ in range(max_num_trials): # sample a random truncation of the video feats st = random.randint(0, feat_len - max_seq_len) ed = st + max_seq_len window = torch.as_tensor([st, ed], dtype=torch.float32) # compute the intersection between the sampled window and all segments window = window[None].repeat(num_segs, 1) left = torch.maximum(window[:, 0], data_dict['segments'][:, 0]) right = torch.minimum(window[:, 1], data_dict['segments'][:, 1]) inter = (right - left).clamp(min=0) area_segs = torch.abs( data_dict['segments'][:, 1] - data_dict['segments'][:, 0]) inter_ratio = inter / area_segs # only select those segments over the thresh seg_idx = (inter_ratio >= trunc_thresh) if no_trunc: # with at least one action and not truncating any actions seg_trunc_idx = torch.logical_and( (inter_ratio > 0.0), (inter_ratio < 1.0) ) if (seg_idx.sum().item() > 0) and (seg_trunc_idx.sum().item() == 0): break elif has_action: # with at least one action if seg_idx.sum().item() > 0: break else: # without any constraints break # feats: C x T data_dict['feats'] = data_dict['feats'][:, st:ed].clone() # segments: N x 2 in feature grids data_dict['segments'] = torch.stack((left[seg_idx], right[seg_idx]), dim=1) # shift the time stamps due to truncation data_dict['segments'] = data_dict['segments'] - st # labels: N data_dict['labels'] = data_dict['labels'][seg_idx].clone() return data_dict
InternVideo-main
Downstream/Temporal-Action-Localization/libs/datasets/data_utils.py
from linecache import updatecache import os import json import h5py import numpy as np import torch from torch.utils.data import Dataset from torch.nn import functional as F from .datasets import register_dataset from .data_utils import truncate_feats from ..utils import remove_duplicate_annotations import pickle import io from petrel_client.client import Client client = Client() @register_dataset("anet") class ActivityNetDataset(Dataset): def __init__( self, is_training, # if in training mode split, # split, a tuple/list allowing concat of subsets feat_folder, # folder for features mae_feat_folder, json_file, # json file for annotations feat_stride, # temporal stride of the feats num_frames, # number of frames for each feat default_fps, # default fps downsample_rate, # downsample rate for feats max_seq_len, # maximum sequence length during training trunc_thresh, # threshold for truncate an action segment crop_ratio, # a tuple (e.g., (0.9, 1.0)) for random cropping input_dim, # input feat dim num_classes, # number of action categories file_prefix, # feature file prefix if any file_ext, # feature file extension if any force_upsampling # force to upsample to max_seq_len ): # file path # assert os.path.exists(feat_folder) and os.path.exists(json_file) # assert client.isdir(feat_folder) and os.path.exists(json_file) # assert isinstance(split, tuple) or isinstance(split, list) # assert crop_ratio == None or len(crop_ratio) == 2 self.feat_folder = feat_folder self.mae_feat_folder = mae_feat_folder self.use_hdf5 = '.hdf5' in feat_folder if file_prefix is not None: self.file_prefix = file_prefix else: self.file_prefix = '' self.file_ext = file_ext self.json_file = json_file # anet uses fixed length features, make sure there is no downsampling self.force_upsampling = force_upsampling # split / training mode self.split = split self.is_training = is_training # self.i3d_feature_list=list(self.i3d_feature.keys()) # features meta info self.feat_stride = feat_stride self.num_frames = num_frames self.input_dim = input_dim self.default_fps = default_fps self.downsample_rate = downsample_rate self.max_seq_len = max_seq_len self.trunc_thresh = trunc_thresh self.num_classes = num_classes self.label_dict = None self.crop_ratio = crop_ratio # load database and select the subset dict_db, label_dict = self._load_json_db(self.json_file) # proposal vs action categories assert (num_classes == 1) or (len(label_dict) == num_classes) self.data_list = dict_db self.label_dict = label_dict # dataset specific attributes self.db_attributes = { 'dataset_name': 'ActivityNet 1.3', 'tiou_thresholds': np.linspace(0.5, 0.95, 10), 'empty_label_ids': [] } def get_attributes(self): return self.db_attributes def _load_json_db(self, json_file): # load database and select the subset with open(json_file, 'r') as fid: json_data = json.load(fid) json_db = json_data['database'] # if label_dict is not available if self.label_dict is None: label_dict = {} for key, value in json_db.items(): for act in value['annotations']: label_dict[act['label']] = act['label_id'] # json.dump(label_dict,open('label_dict.json','w')) # fill in the db (immutable afterwards) dict_db = tuple() for key, value in json_db.items(): # skip the video if not in the split if value['subset'].lower() not in self.split: continue # if 'v_'+key not in self.i3d_feature_list: # continue # get fps if available if self.default_fps is not None: fps = self.default_fps elif 'fps' in value: fps = value['fps'] else: assert False, "Unknown video FPS." duration = value['duration'] # get annotations if available if ('annotations' in value) and (len(value['annotations']) > 0): valid_acts = remove_duplicate_annotations(value['annotations']) num_acts = len(valid_acts) segments = np.zeros([num_acts, 2], dtype=np.float32) labels = np.zeros([num_acts, ], dtype=np.int64) for idx, act in enumerate(valid_acts): segments[idx][0] = act['segment'][0] segments[idx][1] = act['segment'][1] if self.num_classes == 1: labels[idx] = 0 else: labels[idx] = label_dict[act['label']] else: segments = None labels = None dict_db += ({'id': key, 'fps' : fps, 'duration' : duration, 'segments' : segments, 'labels' : labels }, ) return dict_db, label_dict def __len__(self): return len(self.data_list) def __getitem__(self, idx): # directly return a (truncated) data point (so it is very fast!) # auto batching will be disabled in the subsequent dataloader # instead the model will need to decide how to batch / preporcess the data video_item = self.data_list[idx] filename = os.path.join(self.feat_folder, self.file_prefix + video_item['id'] + self.file_ext) feats_tsp = np.load(filename).astype(np.float32) # feats_mae=pickle.load(open(os.path.join(self.mae_feat_folder,self.file_prefix + video_item['id']+'.pkl'),'rb')) feats_mae=np.load(os.path.join(self.mae_feat_folder,self.file_prefix + video_item['id']+'.npy')).astype(np.float32) feats_mae = F.interpolate(torch.from_numpy(feats_mae).permute(1,0).unsqueeze(0), size=feats_tsp.shape[0], mode='linear',align_corners=False)[0,...].permute(1,0) feats_mae = feats_mae.numpy() # feats_clip=np.load(os.path.join('/mnt/petrelfs/liuyi/anet_clipk700/',self.file_prefix + video_item['id']+'.npy')).astype(np.float32) # feats_clip = F.interpolate(torch.from_numpy(feats_clip).permute(1,0).unsqueeze(0), size=feats_tsp.shape[0], mode='linear',align_corners=False)[0,...].permute(1,0) # feats_clip = feats_clip.numpy() # feats = np.concatenate([feats_clip, feats_mae], axis=1) feats=feats_mae # we support both fixed length features / variable length features if self.feat_stride > 0: # var length features feat_stride, num_frames = self.feat_stride, self.num_frames # only apply down sampling here if self.downsample_rate > 1: feats = feats[::self.downsample_rate, :] feat_stride = self.feat_stride * self.downsample_rate else: # deal with fixed length feature, recompute feat_stride, num_frames seq_len = feats.shape[0] assert seq_len <= self.max_seq_len if self.force_upsampling: # reset to max_seq_len seq_len = self.max_seq_len feat_stride = video_item['duration'] * video_item['fps'] / seq_len # center the features num_frames = feat_stride * self.num_frames # T x C -> C x T feats = torch.from_numpy(np.ascontiguousarray(feats.transpose())) # resize the features if needed if (self.feat_stride <= 0) and (feats.shape[-1] != self.max_seq_len) and self.force_upsampling: resize_feats = F.interpolate( feats.unsqueeze(0), size=self.max_seq_len, mode='linear', align_corners=False ) feats = resize_feats.squeeze(0) # convert time stamp (in second) into temporal feature grids # ok to have small negative values here if video_item['segments'] is not None: segments = torch.from_numpy( (video_item['segments'] * video_item['fps'] - 0.5 * num_frames) / feat_stride ) labels = torch.from_numpy(video_item['labels']) # for activity net, we have a few videos with a bunch of missing frames # here is a quick fix for training if self.is_training: feat_len = feats.shape[1] valid_seg_list, valid_label_list = [], [] for seg, label in zip(segments, labels): if seg[0] >= feat_len: # skip an action outside of the feature map continue # truncate an action boundary valid_seg_list.append(seg.clamp(max=feat_len)) # some weird bug here if not converting to size 1 tensor valid_label_list.append(label.view(1)) segments = torch.stack(valid_seg_list, dim=0) labels = torch.cat(valid_label_list) else: segments, labels = None, None # return a data dict data_dict = {'video_id' : video_item['id'], 'feats' : feats, # C x T 'segments' : segments, # N x 2 'labels' : labels, # N 'fps' : video_item['fps'], 'duration' : video_item['duration'], 'feat_stride' : feat_stride, 'feat_num_frames' : num_frames} # no truncation is needed # truncate the features during training if self.is_training and (segments is not None) and (self.feat_stride > 0): data_dict = truncate_feats( data_dict, self.max_seq_len, self.trunc_thresh, self.crop_ratio ) return data_dict
InternVideo-main
Downstream/Temporal-Action-Localization/libs/datasets/anet.py
# Modified from official EPIC-Kitchens action detection evaluation code # see https://github.com/epic-kitchens/C2-Action-Detection/blob/master/EvaluationCode/evaluate_detection_json_ek100.py import os import json import pandas as pd import numpy as np from joblib import Parallel, delayed from typing import List from typing import Tuple from typing import Dict def remove_duplicate_annotations(ants, tol=1e-3): # remove duplicate annotations (same category and starting/ending time) valid_events = [] for event in ants: s, e, l = event['segment'][0], event['segment'][1], event['label_id'] valid = True for p_event in valid_events: if ((abs(s-p_event['segment'][0]) <= tol) and (abs(e-p_event['segment'][1]) <= tol) and (l == p_event['label_id']) ): valid = False break if valid: valid_events.append(event) return valid_events def load_gt_seg_from_json(json_file, split=None, label='label_id', label_offset=0): # load json file with open(json_file, "r", encoding="utf8") as f: json_db = json.load(f) json_db = json_db['database'] vids, starts, stops, labels = [], [], [], [] for k, v in json_db.items(): # filter based on split if (split is not None) and v['subset'].lower() != split: continue # remove duplicated instances ants = remove_duplicate_annotations(v['annotations']) # video id vids += [k] * len(ants) # for each event, grab the start/end time and label for event in ants: starts += [float(event['segment'][0])] stops += [float(event['segment'][1])] if isinstance(event[label], (Tuple, List)): # offset the labels by label_offset label_id = 0 for i, x in enumerate(event[label][::-1]): label_id += label_offset**i + int(x) else: # load label_id directly label_id = int(event[label]) labels += [label_id] # move to pd dataframe gt_base = pd.DataFrame({ 'video-id' : vids, 't-start' : starts, 't-end': stops, 'label': labels }) return gt_base def load_pred_seg_from_json(json_file, label='label_id', label_offset=0): # load json file with open(json_file, "r", encoding="utf8") as f: json_db = json.load(f) json_db = json_db['database'] vids, starts, stops, labels, scores = [], [], [], [], [] for k, v, in json_db.items(): # video id vids += [k] * len(v) # for each event for event in v: starts += [float(event['segment'][0])] stops += [float(event['segment'][1])] if isinstance(event[label], (Tuple, List)): # offset the labels by label_offset label_id = 0 for i, x in enumerate(event[label][::-1]): label_id += label_offset**i + int(x) else: # load label_id directly label_id = int(event[label]) labels += [label_id] scores += [float(event['scores'])] # move to pd dataframe pred_base = pd.DataFrame({ 'video-id' : vids, 't-start' : starts, 't-end': stops, 'label': labels, 'score': scores }) return pred_base class ANETdetection(object): """Adapted from https://github.com/activitynet/ActivityNet/blob/master/Evaluation/eval_detection.py""" def __init__( self, ant_file, split=None, tiou_thresholds=np.linspace(0.1, 0.5, 5), label='label_id', label_offset=0, num_workers=8, dataset_name=None, ): self.tiou_thresholds = tiou_thresholds self.ap = None self.num_workers = num_workers if dataset_name is not None: self.dataset_name = dataset_name else: self.dataset_name = os.path.basename(ant_file).replace('.json', '') # Import ground truth and predictions self.split = split self.ground_truth = load_gt_seg_from_json( ant_file, split=self.split, label=label, label_offset=label_offset) # remove labels that does not exists in gt self.activity_index = {j: i for i, j in enumerate(sorted(self.ground_truth['label'].unique()))} self.ground_truth['label']=self.ground_truth['label'].replace(self.activity_index) def _get_predictions_with_label(self, prediction_by_label, label_name, cidx): """Get all predicitons of the given label. Return empty DataFrame if there is no predcitions with the given label. """ try: res = prediction_by_label.get_group(cidx).reset_index(drop=True) return res except: print('Warning: No predictions of label \'%s\' were provdied.' % label_name) return pd.DataFrame() def wrapper_compute_average_precision(self, preds): """Computes average precision for each class in the subset. """ ap = np.zeros((len(self.tiou_thresholds), len(self.activity_index))) # Adaptation to query faster ground_truth_by_label = self.ground_truth.groupby('label') prediction_by_label = preds.groupby('label') results = Parallel(n_jobs=self.num_workers)( delayed(compute_average_precision_detection)( ground_truth=ground_truth_by_label.get_group(cidx).reset_index(drop=True), prediction=self._get_predictions_with_label(prediction_by_label, label_name, cidx), tiou_thresholds=self.tiou_thresholds, ) for label_name, cidx in self.activity_index.items()) for i, cidx in enumerate(self.activity_index.values()): ap[:,cidx] = results[i] return ap def evaluate(self, preds, verbose=True): """Evaluates a prediction file. For the detection task we measure the interpolated mean average precision to measure the performance of a method. preds can be (1) a pd.DataFrame; or (2) a json file where the data will be loaded; or (3) a python dict item with numpy arrays as the values """ if isinstance(preds, pd.DataFrame): assert 'label' in preds elif isinstance(preds, str) and os.path.isfile(preds): preds = load_pred_seg_from_json(preds) elif isinstance(preds, Dict): # move to pd dataframe # did not check dtype here, can accept both numpy / pytorch tensors preds = pd.DataFrame({ 'video-id' : preds['video-id'], 't-start' : preds['t-start'].tolist(), 't-end': preds['t-end'].tolist(), 'label': preds['label'].tolist(), 'score': preds['score'].tolist() }) # always reset ap self.ap = None # make the label ids consistent preds['label'] = preds['label'].replace(self.activity_index) # compute mAP self.ap = self.wrapper_compute_average_precision(preds) mAP = self.ap.mean(axis=1) average_mAP = mAP.mean() # print results if verbose: # print the results print('[RESULTS] Action detection results on {:s}.'.format( self.dataset_name) ) block = '' for tiou, tiou_mAP in zip(self.tiou_thresholds, mAP): block += '\n|tIoU = {:.2f}: mAP = {:.2f} (%)'.format(tiou, tiou_mAP*100) print(block) print('Avearge mAP: {:.2f} (%)'.format(average_mAP*100)) # return the results return mAP, average_mAP def compute_average_precision_detection( ground_truth, prediction, tiou_thresholds=np.linspace(0.1, 0.5, 5) ): """Compute average precision (detection task) between ground truth and predictions data frames. If multiple predictions occurs for the same predicted segment, only the one with highest score is matches as true positive. This code is greatly inspired by Pascal VOC devkit. Parameters ---------- ground_truth : df Data frame containing the ground truth instances. Required fields: ['video-id', 't-start', 't-end'] prediction : df Data frame containing the prediction instances. Required fields: ['video-id, 't-start', 't-end', 'score'] tiou_thresholds : 1darray, optional Temporal intersection over union threshold. Outputs ------- ap : float Average precision score. """ ap = np.zeros(len(tiou_thresholds)) if prediction.empty: return ap npos = float(len(ground_truth)) lock_gt = np.ones((len(tiou_thresholds),len(ground_truth))) * -1 # Sort predictions by decreasing score order. sort_idx = prediction['score'].values.argsort()[::-1] prediction = prediction.loc[sort_idx].reset_index(drop=True) # Initialize true positive and false positive vectors. tp = np.zeros((len(tiou_thresholds), len(prediction))) fp = np.zeros((len(tiou_thresholds), len(prediction))) # Adaptation to query faster ground_truth_gbvn = ground_truth.groupby('video-id') # Assigning true positive to truly ground truth instances. for idx, this_pred in prediction.iterrows(): try: # Check if there is at least one ground truth in the video associated. ground_truth_videoid = ground_truth_gbvn.get_group(this_pred['video-id']) except Exception as e: fp[:, idx] = 1 continue this_gt = ground_truth_videoid.reset_index() tiou_arr = segment_iou(this_pred[['t-start', 't-end']].values, this_gt[['t-start', 't-end']].values) # We would like to retrieve the predictions with highest tiou score. tiou_sorted_idx = tiou_arr.argsort()[::-1] for tidx, tiou_thr in enumerate(tiou_thresholds): for jdx in tiou_sorted_idx: if tiou_arr[jdx] < tiou_thr: fp[tidx, idx] = 1 break if lock_gt[tidx, this_gt.loc[jdx]['index']] >= 0: continue # Assign as true positive after the filters above. tp[tidx, idx] = 1 lock_gt[tidx, this_gt.loc[jdx]['index']] = idx break if fp[tidx, idx] == 0 and tp[tidx, idx] == 0: fp[tidx, idx] = 1 tp_cumsum = np.cumsum(tp, axis=1).astype(np.float) fp_cumsum = np.cumsum(fp, axis=1).astype(np.float) recall_cumsum = tp_cumsum / npos precision_cumsum = tp_cumsum / (tp_cumsum + fp_cumsum) for tidx in range(len(tiou_thresholds)): ap[tidx] = interpolated_prec_rec(precision_cumsum[tidx,:], recall_cumsum[tidx,:]) return ap def segment_iou(target_segment, candidate_segments): """Compute the temporal intersection over union between a target segment and all the test segments. Parameters ---------- target_segment : 1d array Temporal target segment containing [starting, ending] times. candidate_segments : 2d array Temporal candidate segments containing N x [starting, ending] times. Outputs ------- tiou : 1d array Temporal intersection over union score of the N's candidate segments. """ tt1 = np.maximum(target_segment[0], candidate_segments[:, 0]) tt2 = np.minimum(target_segment[1], candidate_segments[:, 1]) # Intersection including Non-negative overlap score. segments_intersection = (tt2 - tt1).clip(0) # Segment union. segments_union = (candidate_segments[:, 1] - candidate_segments[:, 0]) \ + (target_segment[1] - target_segment[0]) - segments_intersection # Compute overlap as the ratio of the intersection # over union of two segments. tIoU = segments_intersection.astype(float) / segments_union return tIoU def interpolated_prec_rec(prec, rec): """Interpolated AP - VOCdevkit from VOC 2011. """ mprec = np.hstack([[0], prec, [0]]) mrec = np.hstack([[0], rec, [1]]) for i in range(len(mprec) - 1)[::-1]: mprec[i] = max(mprec[i], mprec[i + 1]) idx = np.where(mrec[1::] != mrec[0:-1])[0] + 1 ap = np.sum((mrec[idx] - mrec[idx - 1]) * mprec[idx]) return ap
InternVideo-main
Downstream/Temporal-Action-Localization/libs/utils/metrics.py
import os import shutil import time import pickle import numpy as np import random from copy import deepcopy import torch import torch.optim as optim import torch.backends.cudnn as cudnn from .lr_schedulers import LinearWarmupMultiStepLR, LinearWarmupCosineAnnealingLR from .postprocessing import postprocess_results from ..modeling import MaskedConv1D, Scale, AffineDropPath, LayerNorm from tqdm import tqdm import json ################################################################################ def fix_random_seed(seed, include_cuda=True): rng_generator = torch.manual_seed(seed) np.random.seed(seed) random.seed(seed) os.environ["PYTHONHASHSEED"] = str(seed) if include_cuda: # training: disable cudnn benchmark to ensure the reproducibility cudnn.enabled = True cudnn.benchmark = False cudnn.deterministic = True torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) # this is needed for CUDA >= 10.2 os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8" # torch.use_deterministic_algorithms(True, warn_only=True) torch.use_deterministic_algorithms(True) else: cudnn.enabled = True cudnn.benchmark = True return rng_generator def save_checkpoint(state, is_best, file_folder, file_name='checkpoint.pth.tar'): """save checkpoint to file""" if not os.path.exists(file_folder): os.mkdir(file_folder) torch.save(state, os.path.join(file_folder, file_name)) if is_best: # skip the optimization / scheduler state state.pop('optimizer', None) state.pop('scheduler', None) torch.save(state, os.path.join(file_folder, 'model_best.pth.tar')) def print_model_params(model): for name, param in model.named_parameters(): print(name, param.min().item(), param.max().item(), param.mean().item()) return def make_optimizer(model, optimizer_config): """create optimizer return a supported optimizer """ # separate out all parameters that with / without weight decay # see https://github.com/karpathy/minGPT/blob/master/mingpt/model.py#L134 decay = set() no_decay = set() whitelist_weight_modules = (torch.nn.Linear, torch.nn.Conv1d, MaskedConv1D) blacklist_weight_modules = (LayerNorm, torch.nn.GroupNorm) # loop over all modules / params for mn, m in model.named_modules(): for pn, p in m.named_parameters(): fpn = '%s.%s' % (mn, pn) if mn else pn # full param name if pn.endswith('bias'): # all biases will not be decayed no_decay.add(fpn) elif pn.endswith('weight') and isinstance(m, whitelist_weight_modules): # weights of whitelist modules will be weight decayed decay.add(fpn) elif pn.endswith('weight') and isinstance(m, blacklist_weight_modules): # weights of blacklist modules will NOT be weight decayed no_decay.add(fpn) elif pn.endswith('scale') and isinstance(m, (Scale, AffineDropPath)): # corner case of our scale layer no_decay.add(fpn) elif pn.endswith('rel_pe'): # corner case for relative position encoding no_decay.add(fpn) # validate that we considered every parameter param_dict = {pn: p for pn, p in model.named_parameters()} inter_params = decay & no_decay union_params = decay | no_decay assert len(inter_params) == 0, "parameters %s made it into both decay/no_decay sets!" % (str(inter_params), ) assert len(param_dict.keys() - union_params) == 0, \ "parameters %s were not separated into either decay/no_decay set!" \ % (str(param_dict.keys() - union_params), ) # create the pytorch optimizer object optim_groups = [ {"params": [param_dict[pn] for pn in sorted(list(decay))], "weight_decay": optimizer_config['weight_decay']}, {"params": [param_dict[pn] for pn in sorted(list(no_decay))], "weight_decay": 0.0}, ] if optimizer_config["type"] == "SGD": optimizer = optim.SGD( optim_groups, lr=optimizer_config["learning_rate"], momentum=optimizer_config["momentum"] ) elif optimizer_config["type"] == "AdamW": optimizer = optim.AdamW( optim_groups, lr=optimizer_config["learning_rate"] ) else: raise TypeError("Unsupported optimizer!") return optimizer def make_scheduler( optimizer, optimizer_config, num_iters_per_epoch, last_epoch=-1 ): """create scheduler return a supported scheduler All scheduler returned by this function should step every iteration """ if optimizer_config["warmup"]: max_epochs = optimizer_config["epochs"] + optimizer_config["warmup_epochs"] max_steps = max_epochs * num_iters_per_epoch # get warmup params warmup_epochs = optimizer_config["warmup_epochs"] warmup_steps = warmup_epochs * num_iters_per_epoch # with linear warmup: call our custom schedulers if optimizer_config["schedule_type"] == "cosine": # Cosine scheduler = LinearWarmupCosineAnnealingLR( optimizer, warmup_steps, max_steps, last_epoch=last_epoch ) elif optimizer_config["schedule_type"] == "multistep": # Multi step steps = [num_iters_per_epoch * step for step in optimizer_config["schedule_steps"]] scheduler = LinearWarmupMultiStepLR( optimizer, warmup_steps, steps, gamma=optimizer_config["schedule_gamma"], last_epoch=last_epoch ) else: raise TypeError("Unsupported scheduler!") else: max_epochs = optimizer_config["epochs"] max_steps = max_epochs * num_iters_per_epoch # without warmup: call default schedulers if optimizer_config["schedule_type"] == "cosine": # step per iteration scheduler = optim.lr_scheduler.CosineAnnealingLR( optimizer, max_steps, last_epoch=last_epoch ) elif optimizer_config["schedule_type"] == "multistep": # step every some epochs steps = [num_iters_per_epoch * step for step in optimizer_config["schedule_steps"]] scheduler = optim.lr_scheduler.MultiStepLR( optimizer, steps, gamma=schedule_config["gamma"], last_epoch=last_epoch ) else: raise TypeError("Unsupported scheduler!") return scheduler class AverageMeter(object): """Computes and stores the average and current value. Used to compute dataset stats from mini-batches """ def __init__(self): self.initialized = False self.val = None self.avg = None self.sum = None self.count = 0.0 def initialize(self, val, n): self.val = val self.avg = val self.sum = val * n self.count = n self.initialized = True def update(self, val, n=1): if not self.initialized: self.initialize(val, n) else: self.add(val, n) def add(self, val, n): self.val = val self.sum += val * n self.count += n self.avg = self.sum / self.count class ModelEma(torch.nn.Module): def __init__(self, model, decay=0.999, device=None): super().__init__() # make a copy of the model for accumulating moving average of weights self.module = deepcopy(model) self.module.eval() self.decay = decay self.device = device # perform ema on different device from model if set if self.device is not None: self.module.to(device=device) def _update(self, model, update_fn): with torch.no_grad(): for ema_v, model_v in zip(self.module.state_dict().values(), model.state_dict().values()): if self.device is not None: model_v = model_v.to(device=self.device) ema_v.copy_(update_fn(ema_v, model_v)) def update(self, model): self._update(model, update_fn=lambda e, m: self.decay * e + (1. - self.decay) * m) def set(self, model): self._update(model, update_fn=lambda e, m: m) ################################################################################ def train_one_epoch( train_loader, model, optimizer, scheduler, curr_epoch, model_ema = None, clip_grad_l2norm = -1, tb_writer = None, print_freq = 20 ): """Training the model for one epoch""" # set up meters batch_time = AverageMeter() losses_tracker = {} # number of iterations per epoch num_iters = len(train_loader) # switch to train mode model.train() # main training loop print("\n[Train]: Epoch {:d} started".format(curr_epoch)) start = time.time() for iter_idx, video_list in (enumerate(train_loader, 0)): # zero out optim optimizer.zero_grad(set_to_none=True) # forward / backward the model losses = model(video_list) losses['final_loss'].backward() # gradient cliping (to stabilize training if necessary) if clip_grad_l2norm > 0.0: torch.nn.utils.clip_grad_norm_( model.parameters(), clip_grad_l2norm ) # step optimizer / scheduler optimizer.step() scheduler.step() if model_ema is not None: model_ema.update(model) # printing (only check the stats when necessary to avoid extra cost) if (iter_idx != 0) and (iter_idx % print_freq) == 0: # measure elapsed time (sync all kernels) torch.cuda.synchronize() batch_time.update((time.time() - start) / print_freq) start = time.time() # track all losses for key, value in losses.items(): # init meter if necessary if key not in losses_tracker: losses_tracker[key] = AverageMeter() # update losses_tracker[key].update(value.item()) # log to tensor board lr = scheduler.get_last_lr()[0] global_step = curr_epoch * num_iters + iter_idx if tb_writer is not None: # learning rate (after stepping) tb_writer.add_scalar( 'train/learning_rate', lr, global_step ) # all losses tag_dict = {} for key, value in losses_tracker.items(): if key != "final_loss": tag_dict[key] = value.val tb_writer.add_scalars( 'train/all_losses', tag_dict, global_step ) # final loss tb_writer.add_scalar( 'train/final_loss', losses_tracker['final_loss'].val, global_step ) # print to terminal block1 = 'Epoch: [{:03d}][{:05d}/{:05d}]'.format( curr_epoch, iter_idx, num_iters ) block2 = 'Time {:.2f} ({:.2f})'.format( batch_time.val, batch_time.avg ) block3 = 'Loss {:.2f} ({:.2f})\n'.format( losses_tracker['final_loss'].val, losses_tracker['final_loss'].avg ) block4 = '' for key, value in losses_tracker.items(): if key != "final_loss": block4 += '\t{:s} {:.2f} ({:.2f})'.format( key, value.val, value.avg ) # print('\t'.join([block1, block2, block3, block4])) # finish up and print lr = scheduler.get_last_lr()[0] # print("[Train]: Epoch {:d} finished with lr={:.8f}\n".format(curr_epoch, lr)) return def valid_one_epoch( val_loader, model, curr_epoch, ext_score_file = None, evaluator = None, output_file = None, tb_writer = None, print_freq = 20 ): """Test the model on the validation set""" # either evaluate the results or save the results # assert (evaluator is not None) or (output_file is not None) # set up meters batch_time = AverageMeter() # switch to evaluate mode model.eval() # dict for results (for our evaluation code) results = { 'video-id': [], 't-start' : [], 't-end': [], 'label': [], 'score': [] } # loop over validation set start = time.time() for iter_idx, video_list in enumerate(val_loader, 0): # forward the model (wo. grad) with torch.no_grad(): output = model(video_list) # upack the results into ANet format num_vids = len(output) for vid_idx in range(num_vids): if output[vid_idx]['segments'].shape[0] > 0: results['video-id'].extend( [output[vid_idx]['video_id']] * output[vid_idx]['segments'].shape[0] ) results['t-start'].append(output[vid_idx]['segments'][:, 0]) results['t-end'].append(output[vid_idx]['segments'][:, 1]) results['label'].append(output[vid_idx]['labels']) results['score'].append(output[vid_idx]['scores']) # printing # if (iter_idx != 0) and iter_idx % (print_freq) == 0: # # measure elapsed time (sync all kernels) # torch.cuda.synchronize() # batch_time.update((time.time() - start) / print_freq) # start = time.time() # # print timing # print('Test: [{0:05d}/{1:05d}]\t' # 'Time {batch_time.val:.2f} ({batch_time.avg:.2f})'.format( # iter_idx, len(val_loader), batch_time=batch_time)) # gather all stats and evaluate results['t-start'] = torch.cat(results['t-start']).numpy() results['t-end'] = torch.cat(results['t-end']).numpy() results['label'] = torch.cat(results['label']).numpy() results['score'] = torch.cat(results['score']).numpy() if evaluator is not None: if (ext_score_file is not None) and isinstance(ext_score_file, str): results = postprocess_results(results, ext_score_file) # call the evaluator _, mAP = evaluator.evaluate(results, verbose=True) # log mAP to tb_writer if tb_writer is not None: tb_writer.add_scalar('validation/mAP', mAP, curr_epoch) return mAP
InternVideo-main
Downstream/Temporal-Action-Localization/libs/utils/train_utils.py
import math import warnings from collections import Counter from bisect import bisect_right import torch from torch.optim.lr_scheduler import _LRScheduler class LinearWarmupCosineAnnealingLR(_LRScheduler): """ Sets the learning rate of each parameter group to follow a linear warmup schedule between warmup_start_lr and base_lr followed by a cosine annealing schedule between base_lr and eta_min. .. warning:: It is recommended to call :func:`.step()` for :class:`LinearWarmupCosineAnnealingLR` after each iteration as calling it after each epoch will keep the starting lr at warmup_start_lr for the first epoch which is 0 in most cases. .. warning:: passing epoch to :func:`.step()` is being deprecated and comes with an EPOCH_DEPRECATION_WARNING. It calls the :func:`_get_closed_form_lr()` method for this scheduler instead of :func:`get_lr()`. Though this does not change the behavior of the scheduler, when passing epoch param to :func:`.step()`, the user should call the :func:`.step()` function before calling train and validation methods. Example: >>> layer = nn.Linear(10, 1) >>> optimizer = Adam(layer.parameters(), lr=0.02) >>> scheduler = LinearWarmupCosineAnnealingLR(optimizer, warmup_epochs=10, max_epochs=40) >>> # >>> # the default case >>> for epoch in range(40): ... # train(...) ... # validate(...) ... scheduler.step() >>> # >>> # passing epoch param case >>> for epoch in range(40): ... scheduler.step(epoch) ... # train(...) ... # validate(...) """ def __init__( self, optimizer, warmup_epochs, max_epochs, warmup_start_lr = 0.0, eta_min = 1e-8, last_epoch = -1, ): """ Args: optimizer (Optimizer): Wrapped optimizer. warmup_epochs (int): Maximum number of iterations for linear warmup max_epochs (int): Maximum number of iterations warmup_start_lr (float): Learning rate to start the linear warmup. Default: 0. eta_min (float): Minimum learning rate. Default: 0. last_epoch (int): The index of last epoch. Default: -1. """ self.warmup_epochs = warmup_epochs self.max_epochs = max_epochs self.warmup_start_lr = warmup_start_lr self.eta_min = eta_min super(LinearWarmupCosineAnnealingLR, self).__init__(optimizer, last_epoch) def get_lr(self): """ Compute learning rate using chainable form of the scheduler """ if not self._get_lr_called_within_step: warnings.warn( "To get the last learning rate computed by the scheduler, " "please use `get_last_lr()`.", UserWarning, ) if self.last_epoch == 0: return [self.warmup_start_lr] * len(self.base_lrs) elif self.last_epoch < self.warmup_epochs: return [ group["lr"] + (base_lr - self.warmup_start_lr) / (self.warmup_epochs - 1) for base_lr, group in zip(self.base_lrs, self.optimizer.param_groups) ] elif self.last_epoch == self.warmup_epochs: return self.base_lrs elif (self.last_epoch - 1 - self.max_epochs) % (2 * (self.max_epochs - self.warmup_epochs)) == 0: return [ group["lr"] + (base_lr - self.eta_min) * (1 - math.cos(math.pi / (self.max_epochs - self.warmup_epochs))) / 2 for base_lr, group in zip(self.base_lrs, self.optimizer.param_groups) ] return [ (1 + math.cos(math.pi * (self.last_epoch - self.warmup_epochs) / (self.max_epochs - self.warmup_epochs))) / ( 1 + math.cos(math.pi * (self.last_epoch - self.warmup_epochs - 1) / (self.max_epochs - self.warmup_epochs)) ) * (group["lr"] - self.eta_min) + self.eta_min for group in self.optimizer.param_groups ] def _get_closed_form_lr(self): """ Called when epoch is passed as a param to the `step` function of the scheduler. """ if self.last_epoch < self.warmup_epochs: return [ self.warmup_start_lr + self.last_epoch * (base_lr - self.warmup_start_lr) / (self.warmup_epochs - 1) for base_lr in self.base_lrs ] return [ self.eta_min + 0.5 * (base_lr - self.eta_min) * (1 + math.cos(math.pi * (self.last_epoch - self.warmup_epochs) / (self.max_epochs - self.warmup_epochs))) for base_lr in self.base_lrs ] class LinearWarmupMultiStepLR(_LRScheduler): """ Sets the learning rate of each parameter group to follow a linear warmup schedule between warmup_start_lr and base_lr followed by a multi-step schedule that decays the learning rate of each parameter group by gamma once the number of epoch reaches one of the milestones. .. warning:: It is recommended to call :func:`.step()` for :class:`LinearWarmupCosineAnnealingLR` after each iteration as calling it after each epoch will keep the starting lr at warmup_start_lr for the first epoch which is 0 in most cases. .. warning:: passing epoch to :func:`.step()` is being deprecated and comes with an EPOCH_DEPRECATION_WARNING. It calls the :func:`_get_closed_form_lr()` method for this scheduler instead of :func:`get_lr()`. Though this does not change the behavior of the scheduler, when passing epoch param to :func:`.step()`, the user should call the :func:`.step()` function before calling train and validation methods. """ def __init__( self, optimizer, warmup_epochs, milestones, warmup_start_lr = 0.0, gamma = 0.1, last_epoch = -1, ): """ Args: optimizer (Optimizer): Wrapped optimizer. warmup_epochs (int): Maximum number of iterations for linear warmup max_epochs (int): Maximum number of iterations milestones (list): List of epoch indices. Must be increasing. warmup_start_lr (float): Learning rate to start the linear warmup. Default: 0. gamma (float): Multiplicative factor of learning rate decay. Default: 0.1. last_epoch (int): The index of last epoch. Default: -1. """ self.warmup_epochs = warmup_epochs self.warmup_start_lr = warmup_start_lr self.milestones = Counter(milestones) self.gamma = gamma super(LinearWarmupMultiStepLR, self).__init__(optimizer, last_epoch) def get_lr(self): """ Compute learning rate using chainable form of the scheduler """ if not self._get_lr_called_within_step: warnings.warn("To get the last learning rate computed by the scheduler, " "please use `get_last_lr()`.", UserWarning) if self.last_epoch == 0: # starting warm up return [self.warmup_start_lr] * len(self.base_lrs) elif self.last_epoch < self.warmup_epochs: # linear warm up (0 ~ self.warmup_epochs -1) return [ group["lr"] + (base_lr - self.warmup_start_lr) / (self.warmup_epochs - 1) for base_lr, group in zip(self.base_lrs, self.optimizer.param_groups) ] elif self.last_epoch == self.warmup_epochs: # end of warm up (reset to base lrs) return self.base_lrs elif (self.last_epoch - self.warmup_epochs) not in self.milestones: # in between the steps return [group['lr'] for group in self.optimizer.param_groups] return [ group['lr'] * self.gamma ** self.milestones[self.last_epoch - self.warmup_epochs] for group in self.optimizer.param_groups ] def _get_closed_form_lr(self): """ Called when epoch is passed as a param to the `step` function of the scheduler. """ if self.last_epoch < self.warmup_epochs: return [ self.warmup_start_lr + self.last_epoch * (base_lr - self.warmup_start_lr) / (self.warmup_epochs - 1) for base_lr in self.base_lrs ] milestones = list(sorted(self.milestones.elements())) return [base_lr * self.gamma ** bisect_right(milestones, self.last_epoch - self.warmup_epochs) for base_lr in self.base_lrs]
InternVideo-main
Downstream/Temporal-Action-Localization/libs/utils/lr_schedulers.py
# Functions for 1D NMS, modified from: # https://github.com/open-mmlab/mmcv/blob/master/mmcv/ops/nms.py import torch import nms_1d_cpu class NMSop(torch.autograd.Function): @staticmethod def forward( ctx, segs, scores, cls_idxs, iou_threshold, min_score, max_num ): # vanilla nms will not change the score, so we can filter segs first is_filtering_by_score = (min_score > 0) if is_filtering_by_score: valid_mask = scores > min_score segs, scores = segs[valid_mask], scores[valid_mask] cls_idxs = cls_idxs[valid_mask] valid_inds = torch.nonzero( valid_mask, as_tuple=False).squeeze(dim=1) # nms op; return inds that is sorted by descending order inds = nms_1d_cpu.nms( segs.contiguous().cpu(), scores.contiguous().cpu(), iou_threshold=float(iou_threshold)) # cap by max number if max_num > 0: inds = inds[:min(max_num, len(inds))] # return the sorted segs / scores sorted_segs = segs[inds] sorted_scores = scores[inds] sorted_cls_idxs = cls_idxs[inds] return sorted_segs.clone(), sorted_scores.clone(), sorted_cls_idxs.clone() class SoftNMSop(torch.autograd.Function): @staticmethod def forward( ctx, segs, scores, cls_idxs, iou_threshold, sigma, min_score, method, max_num ): # pre allocate memory for sorted results dets = segs.new_empty((segs.size(0), 3), device='cpu') # softnms op, return dets that stores the sorted segs / scores inds = nms_1d_cpu.softnms( segs.cpu(), scores.cpu(), dets.cpu(), iou_threshold=float(iou_threshold), sigma=float(sigma), min_score=float(min_score), method=int(method)) # cap by max number if max_num > 0: n_segs = min(len(inds), max_num) else: n_segs = len(inds) sorted_segs = dets[:n_segs, :2] sorted_scores = dets[:n_segs, 2] sorted_cls_idxs = cls_idxs[inds] sorted_cls_idxs = sorted_cls_idxs[:n_segs] return sorted_segs.clone(), sorted_scores.clone(), sorted_cls_idxs.clone() def seg_voting(nms_segs, all_segs, all_scores, iou_threshold, score_offset=1.5): """ blur localization results by incorporating side segs. this is known as bounding box voting in object detection literature. slightly boost the performance around iou_threshold """ # *_segs : N_i x 2, all_scores: N, # apply offset offset_scores = all_scores + score_offset # computer overlap between nms and all segs # construct the distance matrix of # N_nms x # N_all num_nms_segs, num_all_segs = nms_segs.shape[0], all_segs.shape[0] ex_nms_segs = nms_segs[:, None].expand(num_nms_segs, num_all_segs, 2) ex_all_segs = all_segs[None, :].expand(num_nms_segs, num_all_segs, 2) # compute intersection left = torch.maximum(ex_nms_segs[:, :, 0], ex_all_segs[:, :, 0]) right = torch.minimum(ex_nms_segs[:, :, 1], ex_all_segs[:, :, 1]) inter = (right-left).clamp(min=0) # lens of all segments nms_seg_lens = ex_nms_segs[:, :, 1] - ex_nms_segs[:, :, 0] all_seg_lens = ex_all_segs[:, :, 1] - ex_all_segs[:, :, 0] # iou iou = inter / (nms_seg_lens + all_seg_lens - inter) # get neighbors (# N_nms x # N_all) / weights seg_weights = (iou >= iou_threshold).to(all_scores.dtype) * all_scores[None, :] seg_weights /= torch.sum(seg_weights, dim=1, keepdim=True) refined_segs = seg_weights @ all_segs return refined_segs def batched_nms( segs, scores, cls_idxs, iou_threshold, min_score, max_seg_num, use_soft_nms=True, multiclass=True, sigma=0.5, voting_thresh=0.75, ): # Based on Detectron2 implementation, num_segs = segs.shape[0] # corner case, no prediction outputs if num_segs == 0: return torch.zeros([0, 2]),\ torch.zeros([0,]),\ torch.zeros([0,], dtype=cls_idxs.dtype) if multiclass: # multiclass nms: apply nms on each class independently new_segs, new_scores, new_cls_idxs = [], [], [] for class_id in torch.unique(cls_idxs): curr_indices = torch.where(cls_idxs == class_id)[0] # soft_nms vs nms if use_soft_nms: sorted_segs, sorted_scores, sorted_cls_idxs = SoftNMSop.apply( segs[curr_indices], scores[curr_indices], cls_idxs[curr_indices], iou_threshold, sigma, min_score, 2, max_seg_num ) else: sorted_segs, sorted_scores, sorted_cls_idxs = NMSop.apply( segs[curr_indices], scores[curr_indices], cls_idxs[curr_indices], iou_threshold, min_score, max_seg_num ) # disable seg voting for multiclass nms, no sufficient segs # fill in the class index new_segs.append(sorted_segs) new_scores.append(sorted_scores) new_cls_idxs.append(sorted_cls_idxs) # cat the results new_segs = torch.cat(new_segs) new_scores = torch.cat(new_scores) new_cls_idxs = torch.cat(new_cls_idxs) else: # class agnostic if use_soft_nms: new_segs, new_scores, new_cls_idxs = SoftNMSop.apply( segs, scores, cls_idxs, iou_threshold, sigma, min_score, 2, max_seg_num ) else: new_segs, new_scores, new_cls_idxs = NMSop.apply( segs, scores, cls_idxs, iou_threshold, min_score, max_seg_num ) # seg voting if voting_thresh > 0: new_segs = seg_voting( new_segs, segs, scores, voting_thresh ) # sort based on scores and return # truncate the results based on max_seg_num _, idxs = new_scores.sort(descending=True) max_seg_num = min(max_seg_num, new_segs.shape[0]) # needed for multiclass NMS new_segs = new_segs[idxs[:max_seg_num]] new_scores = new_scores[idxs[:max_seg_num]] new_cls_idxs = new_cls_idxs[idxs[:max_seg_num]] return new_segs, new_scores, new_cls_idxs
InternVideo-main
Downstream/Temporal-Action-Localization/libs/utils/nms.py
from .nms import batched_nms from .metrics import ANETdetection, remove_duplicate_annotations from .train_utils import (make_optimizer, make_scheduler, save_checkpoint, AverageMeter, train_one_epoch, valid_one_epoch, fix_random_seed, ModelEma) from .postprocessing import postprocess_results __all__ = ['batched_nms', 'make_optimizer', 'make_scheduler', 'save_checkpoint', 'AverageMeter', 'train_one_epoch', 'valid_one_epoch', 'ANETdetection', 'postprocess_results', 'fix_random_seed', 'ModelEma', 'remove_duplicate_annotations']
InternVideo-main
Downstream/Temporal-Action-Localization/libs/utils/__init__.py