Unverified Commit d716c426 authored by Kai Zhang's avatar Kai Zhang Committed by GitHub
Browse files

revamp log api usage method (#5072)

* revamp log api usage method
parent e0c5cc41
......@@ -37,7 +37,7 @@ class VGG(nn.Module):
self, features: nn.Module, num_classes: int = 1000, init_weights: bool = True, dropout: float = 0.5
) -> None:
super().__init__()
_log_api_usage_once(self)
_log_api_usage_once("models", self.__class__.__name__)
self.features = features
self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
self.classifier = nn.Sequential(
......
......@@ -209,7 +209,7 @@ class VideoResNet(nn.Module):
zero_init_residual (bool, optional): Zero init bottleneck residual BN. Defaults to False.
"""
super().__init__()
_log_api_usage_once(self)
_log_api_usage_once("models", self.__class__.__name__)
self.inplanes = 64
self.stem = stem()
......
......@@ -34,7 +34,7 @@ def nms(boxes: Tensor, scores: Tensor, iou_threshold: float) -> Tensor:
Tensor: int64 tensor with the indices of the elements that have been kept
by NMS, sorted in decreasing order of scores
"""
_log_api_usage_once("torchvision.ops.nms")
_log_api_usage_once("ops", "nms")
_assert_has_ops()
return torch.ops.torchvision.nms(boxes, scores, iou_threshold)
......@@ -63,7 +63,7 @@ def batched_nms(
Tensor: int64 tensor with the indices of the elements that have been kept by NMS, sorted
in decreasing order of scores
"""
_log_api_usage_once("torchvision.ops.batched_nms")
_log_api_usage_once("ops", "batched_nms")
# Benchmarks that drove the following thresholds are at
# https://github.com/pytorch/vision/issues/1311#issuecomment-781329339
if boxes.numel() > (4000 if boxes.device.type == "cpu" else 20000) and not torchvision._is_tracing():
......@@ -122,7 +122,7 @@ def remove_small_boxes(boxes: Tensor, min_size: float) -> Tensor:
Tensor[K]: indices of the boxes that have both sides
larger than min_size
"""
_log_api_usage_once("torchvision.ops.remove_small_boxes")
_log_api_usage_once("ops", "remove_small_boxes")
ws, hs = boxes[:, 2] - boxes[:, 0], boxes[:, 3] - boxes[:, 1]
keep = (ws >= min_size) & (hs >= min_size)
keep = torch.where(keep)[0]
......@@ -141,7 +141,7 @@ def clip_boxes_to_image(boxes: Tensor, size: Tuple[int, int]) -> Tensor:
Returns:
Tensor[N, 4]: clipped boxes
"""
_log_api_usage_once("torchvision.ops.clip_boxes_to_image")
_log_api_usage_once("ops", "clip_boxes_to_image")
dim = boxes.dim()
boxes_x = boxes[..., 0::2]
boxes_y = boxes[..., 1::2]
......@@ -182,7 +182,7 @@ def box_convert(boxes: Tensor, in_fmt: str, out_fmt: str) -> Tensor:
Tensor[N, 4]: Boxes into converted format.
"""
_log_api_usage_once("torchvision.ops.box_convert")
_log_api_usage_once("ops", "box_convert")
allowed_fmts = ("xyxy", "xywh", "cxcywh")
if in_fmt not in allowed_fmts or out_fmt not in allowed_fmts:
raise ValueError("Unsupported Bounding Box Conversions for given in_fmt and out_fmt")
......@@ -232,7 +232,7 @@ def box_area(boxes: Tensor) -> Tensor:
Returns:
Tensor[N]: the area for each box
"""
_log_api_usage_once("torchvision.ops.box_area")
_log_api_usage_once("ops", "box_area")
boxes = _upcast(boxes)
return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
......@@ -268,7 +268,7 @@ def box_iou(boxes1: Tensor, boxes2: Tensor) -> Tensor:
Returns:
Tensor[N, M]: the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2
"""
_log_api_usage_once("torchvision.ops.box_iou")
_log_api_usage_once("ops", "box_iou")
inter, union = _box_inter_union(boxes1, boxes2)
iou = inter / union
return iou
......@@ -291,7 +291,7 @@ def generalized_box_iou(boxes1: Tensor, boxes2: Tensor) -> Tensor:
for every element in boxes1 and boxes2
"""
_log_api_usage_once("torchvision.ops.generalized_box_iou")
_log_api_usage_once("ops", "generalized_box_iou")
# degenerate boxes gives inf / nan results
# so do an early check
assert (boxes1[:, 2:] >= boxes1[:, :2]).all()
......@@ -323,7 +323,7 @@ def masks_to_boxes(masks: torch.Tensor) -> torch.Tensor:
Returns:
Tensor[N, 4]: bounding boxes
"""
_log_api_usage_once("torchvision.ops.masks_to_boxes")
_log_api_usage_once("ops", "masks_to_boxes")
if masks.numel() == 0:
return torch.zeros((0, 4), device=masks.device, dtype=torch.float)
......
......@@ -61,7 +61,7 @@ def deform_conv2d(
>>> torch.Size([4, 5, 8, 8])
"""
_log_api_usage_once("torchvision.ops.deform_conv2d")
_log_api_usage_once("ops", "deform_conv2d")
_assert_has_ops()
out_channels = weight.shape[0]
......
......@@ -77,7 +77,7 @@ class FeaturePyramidNetwork(nn.Module):
extra_blocks: Optional[ExtraFPNBlock] = None,
):
super().__init__()
_log_api_usage_once(self)
_log_api_usage_once("ops", self.__class__.__name__)
self.inner_blocks = nn.ModuleList()
self.layer_blocks = nn.ModuleList()
for in_channels in in_channels_list:
......
......@@ -32,7 +32,7 @@ def sigmoid_focal_loss(
Returns:
Loss tensor with the reduction option applied.
"""
_log_api_usage_once("torchvision.ops.sigmoid_focal_loss")
_log_api_usage_once("ops", "sigmoid_focal_loss")
p = torch.sigmoid(inputs)
ce_loss = F.binary_cross_entropy_with_logits(inputs, targets, reduction="none")
p_t = p * targets + (1 - p) * (1 - targets)
......
......@@ -61,7 +61,7 @@ class FrozenBatchNorm2d(torch.nn.Module):
warnings.warn("`n` argument is deprecated and has been renamed `num_features`", DeprecationWarning)
num_features = n
super().__init__()
_log_api_usage_once(self)
_log_api_usage_once("ops", self.__class__.__name__)
self.eps = eps
self.register_buffer("weight", torch.ones(num_features))
self.register_buffer("bias", torch.zeros(num_features))
......@@ -155,7 +155,7 @@ class ConvNormActivation(torch.nn.Sequential):
if activation_layer is not None:
layers.append(activation_layer(inplace=inplace))
super().__init__(*layers)
_log_api_usage_once(self)
_log_api_usage_once("ops", self.__class__.__name__)
self.out_channels = out_channels
......@@ -179,7 +179,7 @@ class SqueezeExcitation(torch.nn.Module):
scale_activation: Callable[..., torch.nn.Module] = torch.nn.Sigmoid,
) -> None:
super().__init__()
_log_api_usage_once(self)
_log_api_usage_once("ops", self.__class__.__name__)
self.avgpool = torch.nn.AdaptiveAvgPool2d(1)
self.fc1 = torch.nn.Conv2d(input_channels, squeeze_channels, 1)
self.fc2 = torch.nn.Conv2d(squeeze_channels, input_channels, 1)
......
......@@ -276,7 +276,7 @@ class MultiScaleRoIAlign(nn.Module):
canonical_level: int = 4,
):
super().__init__()
_log_api_usage_once(self)
_log_api_usage_once("ops", self.__class__.__name__)
if isinstance(output_size, int):
output_size = (output_size, output_size)
self.featmap_names = featmap_names
......
......@@ -43,7 +43,7 @@ def ps_roi_align(
Returns:
Tensor[K, C / (output_size[0] * output_size[1]), output_size[0], output_size[1]]: The pooled RoIs
"""
_log_api_usage_once("torchvision.ops.ps_roi_align")
_log_api_usage_once("ops", "ps_roi_align")
_assert_has_ops()
check_roi_boxes_shape(boxes)
rois = boxes
......
......@@ -37,7 +37,7 @@ def ps_roi_pool(
Returns:
Tensor[K, C / (output_size[0] * output_size[1]), output_size[0], output_size[1]]: The pooled RoIs.
"""
_log_api_usage_once("torchvision.ops.ps_roi_pool")
_log_api_usage_once("ops", "ps_roi_pool")
_assert_has_ops()
check_roi_boxes_shape(boxes)
rois = boxes
......
......@@ -50,7 +50,7 @@ def roi_align(
Returns:
Tensor[K, C, output_size[0], output_size[1]]: The pooled RoIs.
"""
_log_api_usage_once("torchvision.ops.roi_align")
_log_api_usage_once("ops", "roi_align")
_assert_has_ops()
check_roi_boxes_shape(boxes)
rois = boxes
......
......@@ -39,7 +39,7 @@ def roi_pool(
Returns:
Tensor[K, C, output_size[0], output_size[1]]: The pooled RoIs.
"""
_log_api_usage_once("torchvision.ops.roi_pool")
_log_api_usage_once("ops", "roi_pool")
_assert_has_ops()
check_roi_boxes_shape(boxes)
rois = boxes
......
......@@ -23,7 +23,7 @@ def stochastic_depth(input: Tensor, p: float, mode: str, training: bool = True)
Returns:
Tensor[N, ...]: The randomly zeroed tensor.
"""
_log_api_usage_once("torchvision.ops.stochastic_depth")
_log_api_usage_once("ops", "stochastic_depth")
if p < 0.0 or p > 1.0:
raise ValueError(f"drop probability has to be between 0 and 1, but got {p}")
if mode not in ["batch", "row"]:
......
......@@ -140,7 +140,7 @@ class VisionTransformer(nn.Module):
norm_layer: Callable[..., torch.nn.Module] = partial(nn.LayerNorm, eps=1e-6),
):
super().__init__()
_log_api_usage_once(self)
_log_api_usage_once("models", self.__class__.__name__)
torch._assert(image_size % patch_size == 0, "Input shape indivisible by patch size!")
self.image_size = image_size
self.patch_size = patch_size
......
import math
import pathlib
import warnings
from typing import Union, Optional, List, Tuple, BinaryIO, no_type_check
from typing import Union, Optional, List, Tuple, BinaryIO
import numpy as np
import torch
......@@ -375,13 +375,7 @@ def _generate_color_palette(num_masks: int):
return [tuple((i * palette) % 255) for i in range(num_masks)]
@no_type_check
def _log_api_usage_once(obj: str) -> None: # type: ignore
def _log_api_usage_once(module: str, name: str) -> None:
if torch.jit.is_scripting() or torch.jit.is_tracing():
return
# NOTE: obj can be an object as well, but mocking it here to be
# only a string to appease torchscript
if isinstance(obj, str):
torch._C._log_api_usage_once(obj)
else:
torch._C._log_api_usage_once(f"{obj.__module__}.{obj.__class__.__name__}")
torch._C._log_api_usage_once(f"torchvision.{module}.{name}")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment