trtutils.image.preprocessors package¶
Module contents¶
Preprocessors for images.
Classes¶
CPUPreprocessorCPU-based preprocessor.
CUDAPreprocessorCUDA-based preprocessor.
TRTPreprocessorTensorRT-based preprocessor.
ImagePreprocessorAbstract base class for image preprocessors.
GPUImagePreprocessorAbstract base class for GPU-based image preprocessors.
Functions¶
preprocess()Preprocess an image for a model.
- class trtutils.image.preprocessors.CPUPreprocessor(output_shape: tuple[int, int], output_range: tuple[float, float], dtype: np.dtype, resize: str = 'letterbox', mean: tuple[float, float, float] | None = None, std: tuple[float, float, float] | None = None, tag: str | None = None)[source]¶
Bases:
ImagePreprocessorCPU-based preprocessor for image processing models.
- preprocess(images: ndarray, resize: str | None = None, *, no_copy: bool | None = None, verbose: bool | None = None) tuple[ndarray, list[tuple[float, float]], list[tuple[float, float]]][source]¶
- preprocess(images: list[ndarray], resize: str | None = None, *, no_copy: bool | None = None, verbose: bool | None = None) tuple[ndarray, list[tuple[float, float]], list[tuple[float, float]]]
Preprocess images for the model.
- Parameters:
images (np.ndarray | list[np.ndarray]) – A single image (HWC format) or list of images to preprocess.
resize (str) – The method to resize the image with. By default letterbox, options are [letterbox, linear]
no_copy (bool, optional) – Compatibility parameter for CUDA parity.
verbose (bool, optional) – Whether or not to output additional information to stdout. If not provided, will default to overall engines verbose setting.
- Returns:
The preprocessed batch tensor, list of ratios, and list of padding per image.
- Return type:
tuple[np.ndarray, list[tuple[float, float]], list[tuple[float, float]]]
- class trtutils.image.preprocessors.CUDAPreprocessor(output_shape: tuple[int, int], output_range: tuple[float, float], dtype: np.dtype[Any], resize: str = 'letterbox', mean: tuple[float, float, float] | None = None, std: tuple[float, float, float] | None = None, stream: cudart.cudaStream_t | None = None, threads: tuple[int, int, int] | None = None, tag: str | None = None, *, pagelocked_mem: bool | None = None, unified_mem: bool | None = None, orig_size_dtype: np.dtype[Any] | None = None)[source]¶
Bases:
GPUImagePreprocessorCUDA-based preprocessor for image processing models.
- direct_preproc(images: list[np.ndarray], resize: str | None = None, *, no_warn: bool | None = None, verbose: bool | None = None) tuple[int, list[tuple[float, float]], list[tuple[float, float]]][source]¶
Preprocess images for the model.
- Parameters:
images (list[np.ndarray]) – The images to preprocess.
resize (str) – The method to resize the image with. By default letterbox, options are [letterbox, linear]
no_warn (bool, optional) – If True, do not warn about usage.
verbose (bool, optional) – Whether or not to output additional information to stdout. If not provided, will default to overall engines verbose setting.
- Returns:
The GPU pointer to preprocessed data, list of ratios, and list of padding per image.
- Return type:
tuple[int, list[tuple[float, float]], list[tuple[float, float]]]
- class trtutils.image.preprocessors.GPUImagePreprocessor(output_shape: tuple[int, int], output_range: tuple[float, float], dtype: np.dtype[Any], resize: str = 'letterbox', mean: tuple[float, float, float] | None = None, std: tuple[float, float, float] | None = None, stream: cudart.cudaStream_t | None = None, threads: tuple[int, int, int] | None = None, tag: str | None = None, *, pagelocked_mem: bool | None = None, unified_mem: bool | None = None, orig_size_dtype: np.dtype[Any] | None = None)[source]¶
Bases:
ImagePreprocessorGPU-based image preprocessor.
- warmup() None[source]¶
Warmup the CUDA preprocessor.
Allocates all CUDA memory and enables future passes to be significantly faster.
- abstractmethod direct_preproc(images: list[np.ndarray], resize: str | None = None, *, no_warn: bool | None = None, verbose: bool | None = None) tuple[int, list[tuple[float, float]], list[tuple[float, float]]][source]¶
Preprocess images for the model with H2D copies and GPU kernels.
This method performs the complete preprocessing pipeline: 1. Host-to-device copy of input images 2. Resize kernels (letterbox or linear) 3. Normalization (SST) kernel
- Parameters:
images (list[np.ndarray]) – The images to preprocess (HWC format, uint8).
resize (str, optional) – The resize method. Options are [‘letterbox’, ‘linear’]. If None, uses the configured default.
no_warn (bool, optional) – If True, suppress warnings about usage.
verbose (bool, optional) – Enable verbose logging.
- Returns:
GPU pointer to preprocessed output, list of ratios (scale_x, scale_y), and list of padding (pad_x, pad_y) per image.
- Return type:
tuple[int, list[tuple[float, float]], list[tuple[float, float]]]
- abstract property output_binding: Binding¶
Get the output binding for the preprocessor.
Subclasses must implement this to return their specific output binding.
- Returns:
The output binding containing the preprocessed data.
- Return type:
- preprocess(images: ndarray, resize: str | None = None, *, no_copy: bool | None = None, verbose: bool | None = None) tuple[ndarray, list[tuple[float, float]], list[tuple[float, float]]][source]¶
- preprocess(images: list[ndarray], resize: str | None = None, *, no_copy: bool | None = None, verbose: bool | None = None) tuple[ndarray, list[tuple[float, float]], list[tuple[float, float]]]
Preprocess images for the model.
- Parameters:
images (np.ndarray | list[np.ndarray]) – A single image (HWC format) or list of images to preprocess.
resize (str, optional) – The method to resize the image with. Options are [letterbox, linear], will use method provided in constructor by default.
no_copy (bool, optional) – If True, the outputs will not be copied out from the cuda allocated host memory. Instead, the host memory will be returned directly. This memory WILL BE OVERWRITTEN INPLACE by future preprocessing calls.
verbose (bool, optional) – Whether or not to output additional information to stdout. If not provided, will default to overall engines verbose setting.
- Returns:
The preprocessed batch tensor, list of ratios, and list of padding per image.
- Return type:
tuple[np.ndarray, list[tuple[float, float]], list[tuple[float, float]]]
- class trtutils.image.preprocessors.ImagePreprocessor(output_shape: tuple[int, int], output_range: tuple[float, float], dtype: np.dtype[Any], resize: str = 'letterbox', mean: tuple[float, float, float] | None = None, std: tuple[float, float, float] | None = None, tag: str | None = None)[source]¶
Bases:
ABCAbstract base class for image preprocessors.
- update_output_range(output_range: tuple[float, float]) None[source]¶
Update the output range of the preprocessor.
- update_mean_std(mean: tuple[float, float, float], std: tuple[float, float, float]) None[source]¶
Update the mean and standard deviation of the preprocessor.
- abstractmethod preprocess(images: ndarray, resize: str | None = None, *, no_copy: bool | None = None, verbose: bool | None = None) tuple[ndarray, list[tuple[float, float]], list[tuple[float, float]]][source]¶
- abstractmethod preprocess(images: list[ndarray], resize: str | None = None, *, no_copy: bool | None = None, verbose: bool | None = None) tuple[ndarray, list[tuple[float, float]], list[tuple[float, float]]]
- class trtutils.image.preprocessors.TRTPreprocessor(output_shape: tuple[int, int], output_range: tuple[float, float], dtype: np.dtype[Any], batch_size: int = 1, resize: str = 'letterbox', mean: tuple[float, float, float] | None = None, std: tuple[float, float, float] | None = None, stream: cudart.cudaStream_t | None = None, threads: tuple[int, int, int] | None = None, tag: str | None = None, *, pagelocked_mem: bool | None = None, unified_mem: bool | None = None, orig_size_dtype: np.dtype[Any] | None = None)[source]¶
Bases:
GPUImagePreprocessorTRT-based preprocessor for image processing models.
- direct_preproc(images: list[np.ndarray], resize: str | None = None, *, no_warn: bool | None = None, verbose: bool | None = None) tuple[int, list[tuple[float, float]], list[tuple[float, float]]][source]¶
Preprocess images for the model.
- Parameters:
images (list[np.ndarray]) – The images to preprocess.
resize (str) – The method to resize the image with. By default letterbox, options are [letterbox, linear]
no_warn (bool, optional) – If True, do not warn about usage.
verbose (bool, optional) – Whether or not to output additional information to stdout. If not provided, will default to overall engines verbose setting.
- Returns:
The GPU pointer to preprocessed data, list of ratios, and list of padding per image.
- Return type:
tuple[int, list[tuple[float, float]], list[tuple[float, float]]]
- Raises:
ValueError – If batch size exceeds the configured batch size for the TRT engine.
- trtutils.image.preprocessors.preprocess(images: list[ndarray], input_shape: tuple[int, int], dtype: dtype, input_range: tuple[float, float] = (0.0, 1.0), method: str = 'letterbox', mean: tuple[float, float, float] | None = None, std: tuple[float, float, float] | None = None, *, verbose: bool | None = None) tuple[ndarray, list[tuple[float, float]], list[tuple[float, float]]][source]¶
Preprocess inputs for a YOLO network.
- Parameters:
images (list[np.ndarray]) – The images to be preprocessed.
input_shape (tuple[int, int]) – The shape to resize the inputs.
dtype (np.dtype) – The datatype of the inputs to the network.
input_range (tuple[float, float]) – The range of the model expects for inputs. By default, [0.0, 1.0] (divide input by 255.0)
method (str) – The method by which to resize the image. By default letterbox will be used. Options are [letterbox, linear]
mean (tuple[float, float, float], optional) – The mean to subtract from the image. By default, None, which will not subtract any mean.
std (tuple[float, float, float], optional) – The standard deviation to divide the image by. By default, None, which will not divide by any standard deviation.
verbose (bool, optional) – Whether or not to log additional information.
- Returns:
The preprocessed batch tensor, list of ratios, and list of padding per image.
- Return type:
tuple[np.ndarray, list[tuple[float, float]], list[tuple[float, float]]]
- Raises:
ValueError – If the method for resizing is not ‘letterbox’ or ‘linear’
ValueError – If only one of mean or std is provided