Skip to content

Log io usage #5038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 21, 2021
2 changes: 2 additions & 0 deletions torchvision/csrc/io/image/cpu/decode_jpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ static void torch_jpeg_set_source_mgr(
} // namespace

torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) {
C10_LOG_API_USAGE_ONCE(
"torchvision.csrc.io.image.cpu.decode_jpeg.decode_jpeg");
// Check that the input tensor dtype is uint8
TORCH_CHECK(data.dtype() == torch::kU8, "Expected a torch.uint8 tensor");
// Check that the input tensor is 1-dimensional
Expand Down
1 change: 1 addition & 0 deletions torchvision/csrc/io/image/cpu/decode_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ torch::Tensor decode_png(
const torch::Tensor& data,
ImageReadMode mode,
bool allow_16_bits) {
C10_LOG_API_USAGE_ONCE("torchvision.csrc.io.image.cpu.decode_png.decode_png");
// Check that the input tensor dtype is uint8
TORCH_CHECK(data.dtype() == torch::kU8, "Expected a torch.uint8 tensor");
// Check that the input tensor is 1-dimensional
Expand Down
2 changes: 2 additions & 0 deletions torchvision/csrc/io/image/cpu/encode_jpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ using JpegSizeType = size_t;
using namespace detail;

torch::Tensor encode_jpeg(const torch::Tensor& data, int64_t quality) {
C10_LOG_API_USAGE_ONCE(
"torchvision.csrc.io.image.cpu.encode_jpeg.encode_jpeg");
// Define compression structures and error handling
struct jpeg_compress_struct cinfo {};
struct torch_jpeg_error_mgr jerr {};
Expand Down
1 change: 1 addition & 0 deletions torchvision/csrc/io/image/cpu/encode_png.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void torch_png_write_data(
} // namespace

torch::Tensor encode_png(const torch::Tensor& data, int64_t compression_level) {
C10_LOG_API_USAGE_ONCE("torchvision.csrc.io.image.cpu.encode_png.encode_png");
// Define compression structures and error handling
png_structp png_write;
png_infop info_ptr;
Expand Down
4 changes: 4 additions & 0 deletions torchvision/csrc/io/image/cpu/read_write_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ std::wstring utf8_decode(const std::string& str) {
#endif

torch::Tensor read_file(const std::string& filename) {
C10_LOG_API_USAGE_ONCE(
"torchvision.csrc.io.image.cpu.read_write_file.read_file");
#ifdef _WIN32
// According to
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions?view=vs-2019,
Expand Down Expand Up @@ -76,6 +78,8 @@ torch::Tensor read_file(const std::string& filename) {
}

void write_file(const std::string& filename, torch::Tensor& data) {
C10_LOG_API_USAGE_ONCE(
"torchvision.csrc.io.image.cpu.read_write_file.write_file");
// Check that the input tensor is on CPU
TORCH_CHECK(data.device() == torch::kCPU, "Input tensor should be on CPU");

Expand Down
2 changes: 2 additions & 0 deletions torchvision/csrc/io/image/cuda/decode_jpeg_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ torch::Tensor decode_jpeg_cuda(
const torch::Tensor& data,
ImageReadMode mode,
torch::Device device) {
C10_LOG_API_USAGE_ONCE(
"torchvision.csrc.io.image.cuda.decode_jpeg_cuda.decode_jpeg_cuda");
TORCH_CHECK(data.dtype() == torch::kU8, "Expected a torch.uint8 tensor");

TORCH_CHECK(
Expand Down
1 change: 1 addition & 0 deletions torchvision/csrc/io/video/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ void Video::_getDecoderParams(
} // _get decoder params

Video::Video(std::string videoPath, std::string stream, int64_t numThreads) {
C10_LOG_API_USAGE_ONCE("torchvision.csrc.io.video.video.Video");
// set number of threads global
numThreads_ = numThreads;
// parse stream information
Expand Down
8 changes: 8 additions & 0 deletions torchvision/csrc/io/video_reader/video_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ torch::List<torch::Tensor> read_video_from_memory(
int64_t audioEndPts,
int64_t audioTimeBaseNum,
int64_t audioTimeBaseDen) {
C10_LOG_API_USAGE_ONCE(
"torchvision.csrc.io.video_reader.video_reader.read_video_from_memory");
return readVideo(
false,
input_video,
Expand Down Expand Up @@ -627,6 +629,8 @@ torch::List<torch::Tensor> read_video_from_file(
int64_t audioEndPts,
int64_t audioTimeBaseNum,
int64_t audioTimeBaseDen) {
C10_LOG_API_USAGE_ONCE(
"torchvision.csrc.io.video_reader.video_reader.read_video_from_file");
torch::Tensor dummy_input_video = torch::ones({0});
return readVideo(
true,
Expand All @@ -653,10 +657,14 @@ torch::List<torch::Tensor> read_video_from_file(
}

torch::List<torch::Tensor> probe_video_from_memory(torch::Tensor input_video) {
C10_LOG_API_USAGE_ONCE(
"torchvision.csrc.io.video_reader.video_reader.probe_video_from_memory");
return probeVideo(false, input_video, "");
}

torch::List<torch::Tensor> probe_video_from_file(std::string videoPath) {
C10_LOG_API_USAGE_ONCE(
"torchvision.csrc.io.video_reader.video_reader.probe_video_from_file");
torch::Tensor dummy_input_video = torch::ones({0});
return probeVideo(true, dummy_input_video, videoPath);
}
Expand Down
2 changes: 2 additions & 0 deletions torchvision/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import torch

from ..utils import _log_api_usage_once
from ._video_opt import (
Timebase,
VideoMetaData,
Expand Down Expand Up @@ -106,6 +107,7 @@ class VideoReader:
"""

def __init__(self, path: str, stream: str = "video", num_threads: int = 0) -> None:
_log_api_usage_once(self)
if not _has_video_opt():
raise RuntimeError(
"Not compiled with video_reader support, "
Expand Down
21 changes: 21 additions & 0 deletions torchvision/io/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import torch

from ..extension import _load_library
from ..utils import _log_api_usage_once


try:
Expand Down Expand Up @@ -41,6 +42,8 @@ def read_file(path: str) -> torch.Tensor:
Returns:
data (Tensor)
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(read_file)
data = torch.ops.image.read_file(path)
return data

Expand All @@ -54,6 +57,8 @@ def write_file(filename: str, data: torch.Tensor) -> None:
filename (str): the path to the file to be written
data (Tensor): the contents to be written to the output file
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(write_file)
torch.ops.image.write_file(filename, data)


Expand All @@ -74,6 +79,8 @@ def decode_png(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHANGE
Returns:
output (Tensor[image_channels, image_height, image_width])
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(decode_png)
output = torch.ops.image.decode_png(input, mode.value, False)
return output

Expand All @@ -93,6 +100,8 @@ def encode_png(input: torch.Tensor, compression_level: int = 6) -> torch.Tensor:
Tensor[1]: A one dimensional int8 tensor that contains the raw bytes of the
PNG file.
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(encode_png)
output = torch.ops.image.encode_png(input, compression_level)
return output

Expand All @@ -109,6 +118,8 @@ def write_png(input: torch.Tensor, filename: str, compression_level: int = 6):
compression_level (int): Compression factor for the resulting file, it must be a number
between 0 and 9. Default: 6
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(write_png)
output = encode_png(input, compression_level)
write_file(filename, output)

Expand Down Expand Up @@ -137,6 +148,8 @@ def decode_jpeg(
Returns:
output (Tensor[image_channels, image_height, image_width])
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(decode_jpeg)
device = torch.device(device)
if device.type == "cuda":
output = torch.ops.image.decode_jpeg_cuda(input, mode.value, device)
Expand All @@ -160,6 +173,8 @@ def encode_jpeg(input: torch.Tensor, quality: int = 75) -> torch.Tensor:
output (Tensor[1]): A one dimensional int8 tensor that contains the raw bytes of the
JPEG file.
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(encode_jpeg)
if quality < 1 or quality > 100:
raise ValueError("Image quality should be a positive number between 1 and 100")

Expand All @@ -178,6 +193,8 @@ def write_jpeg(input: torch.Tensor, filename: str, quality: int = 75):
quality (int): Quality of the resulting JPEG file, it must be a number
between 1 and 100. Default: 75
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(write_jpeg)
output = encode_jpeg(input, quality)
write_file(filename, output)

Expand All @@ -201,6 +218,8 @@ def decode_image(input: torch.Tensor, mode: ImageReadMode = ImageReadMode.UNCHAN
Returns:
output (Tensor[image_channels, image_height, image_width])
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(decode_image)
output = torch.ops.image.decode_image(input, mode.value)
return output

Expand All @@ -221,6 +240,8 @@ def read_image(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torc
Returns:
output (Tensor[image_channels, image_height, image_width])
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(read_image)
data = read_file(path)
return decode_image(data, mode)

Expand Down
7 changes: 7 additions & 0 deletions torchvision/io/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np
import torch

from ..utils import _log_api_usage_once
from . import _video_opt


Expand Down Expand Up @@ -77,6 +78,8 @@ def write_video(
audio_codec (str): the name of the audio codec, i.e. "mp3", "aac", etc.
audio_options (Dict): dictionary containing options to be passed into the PyAV audio stream
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(write_video)
_check_av_available()
video_array = torch.as_tensor(video_array, dtype=torch.uint8).numpy()

Expand Down Expand Up @@ -256,6 +259,8 @@ def read_video(
aframes (Tensor[K, L]): the audio frames, where `K` is the number of channels and `L` is the number of points
info (Dict): metadata for the video and audio. Can contain the fields video_fps (float) and audio_fps (int)
"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(read_video)

from torchvision import get_video_backend

Expand Down Expand Up @@ -374,6 +379,8 @@ def read_video_timestamps(filename: str, pts_unit: str = "pts") -> Tuple[List[in
video_fps (float, optional): the frame rate for the video

"""
if not torch.jit.is_scripting() and not torch.jit.is_tracing():
_log_api_usage_once(read_video_timestamps)
from torchvision import get_video_backend

if get_video_backend() != "pyav":
Expand Down