### Install All Sample Dependencies Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/getting_started.rst Installs dependencies for all CV-CUDA samples using the provided installation script. Navigate to the samples directory first. ```bash cd samples ./install_samples_dependencies.sh ``` -------------------------------- ### Install Ubuntu Packages Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Installs essential development tools like git, git-lfs, pre-commit, and shellcheck. ```shell sudo apt install -y git git-lfs pre-commit shellcheck npm ``` -------------------------------- ### Install CV-CUDA from PyPI (CUDA 12 Example) Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/wsl2.rst Install the CV-CUDA library for CUDA 12 using pip. Replace 'cu12' with 'cu13' for CUDA 13. ```shell python3 -m pip install cvcuda-cu12 ``` -------------------------------- ### Configure Virtual Environment and Install Wheel for Docs Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/CMakeLists.txt This custom command sets up a Python virtual environment with system site packages enabled and installs the project's wheel and documentation dependencies. It relies on a generated CMake script for the actual wheel installation. ```cmake add_custom_command( OUTPUT ${WHEEL_INSTALL_MARKER} # Create virtual environment with system site packages COMMAND ${PYTHON_EXECUTABLE} -m venv --system-site-packages ${DOC_VENV_DIR} # Install documentation dependencies (Sphinx, breathe, etc.) # this will skip packages installed via the system-site-packages flag # in existing CI setup this will do nothing, on local it may install packages if venv creator # does not properly support system-site-packages flag COMMAND ${DOC_VENV_PYTHON} -m pip install -r ${CMAKE_SOURCE_DIR}/docker/requirements.sys_python.txt # Find the wheel file dynamically and install it using CMake script COMMAND ${CMAKE_COMMAND} -DWHEEL_DIR=${CMAKE_BINARY_DIR}/python3/repaired_wheels -DVENV_PYTHON=${DOC_VENV_PYTHON} -DMARKER_FILE=${WHEEL_INSTALL_MARKER} -P ${INSTALL_WHEEL_SCRIPT} DEPENDS wheel COMMENT "Creating venv and installing cvcuda wheel for documentation (requires: -DCMAKE_BUILD_TYPE=Release -DBUILD_PYTHON=1)" ) ``` -------------------------------- ### Install Python Test Files Source: https://github.com/cvcuda/cv-cuda/blob/main/tests/cvcuda/python/CMakeLists.txt Installs the Python source files to the designated test installation directory. This ensures that tests can be run against the installed module. ```cmake install(FILES ${SOURCES} DESTINATION "${PYTHON_TEST_INSTDIR}" COMPONENT tests) ``` -------------------------------- ### Install Python Dependencies using uv Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Sets up a Python virtual environment using uv with seeding enabled and installs project-specific Python dependencies. ```shell uv venv env --seed source env/bin/activate uv pip install -r docker/requirements.sys_python.txt ``` -------------------------------- ### Install Local CV-CUDA Wheel Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples.rst Installs a local CV-CUDA wheel, overwriting any existing PyPI version. Ensure you are in the correct virtual environment. ```bash source venv_samples/bin/activate python3 -m pip install --force-reinstall ../build-rel/python3/repaired_wheels/cvcuda-*.whl ``` -------------------------------- ### Install System Dependencies and Python Packages Source: https://github.com/cvcuda/cv-cuda/blob/main/bench/python/README.md Run this script to install system dependencies, NVIDIA Nsight Systems, and Python packages. It detects CUDA versions and sets up a Python virtual environment. ```shell cd bench/python ./install_dependencies.sh ``` -------------------------------- ### Quick Test (Hello World) for CUDA 12 Source: https://github.com/cvcuda/cv-cuda/blob/main/samples/README.md Installs CV-CUDA, NumPy, cuda-python, and nvImageCodec for the hello_world.py sample using CUDA 12. ```shell python3 -m venv venv_samples source venv_samples/bin/activate python3 -m pip install -r requirements_hello_world_cu12.txt python3 applications/hello_world.py ``` -------------------------------- ### Install CV-CUDA Dependencies (CUDA 12) Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/getting_started.rst Installs minimal dependencies for CV-CUDA and the hello_world sample for CUDA 12. Ensure you are in a Python virtual environment. ```bash python3 -m venv venv_samples source venv_samples/bin/activate python3 -m pip install -r samples/requirements_hello_world_cu12.txt ``` -------------------------------- ### Install CV-CUDA Dependencies (CUDA 13) Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/getting_started.rst Installs minimal dependencies for CV-CUDA and the hello_world sample for CUDA 13. Ensure you are in a Python virtual environment. ```bash python3 -m venv venv_samples source venv_samples/bin/activate python3 -m pip install -r samples/requirements_hello_world_cu13.txt ``` -------------------------------- ### Quick Test (Hello World) for CUDA 13 Source: https://github.com/cvcuda/cv-cuda/blob/main/samples/README.md Installs CV-CUDA, NumPy, cuda-python, and nvImageCodec for the hello_world.py sample using CUDA 13. ```shell python3 -m venv venv_samples source venv_samples/bin/activate python3 -m pip install -r requirements_hello_world_cu13.txt python3 applications/hello_world.py ``` -------------------------------- ### Run All Tests from Installed Packages Directory Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Executes all tests from the installation directory when CV-CUDA is installed from packages. The path may vary based on the installed version. ```shell /opt/nvidia/cvcuda*/bin/run_tests.sh [filter1,filter2,...] ``` -------------------------------- ### Install Python Dependencies using venv Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Sets up a Python virtual environment using venv and installs project-specific Python dependencies from a requirements file. ```shell python3 -m venv env source env/bin/activate python3 -m pip install -r docker/requirements.sys_python.txt ``` -------------------------------- ### Install CV-CUDA Wheel using uv Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst This command installs a CV-CUDA Python wheel using the 'uv' package installer. This is an alternative to pip for managing Python packages. Replace '' with the correct version. ```shell uv pip install ./cvcuda_cu12--cp310.cp311-cp310.cp311-linux_x86_64.whl ``` -------------------------------- ### Interoperability Dependencies Installation Script Source: https://github.com/cvcuda/cv-cuda/blob/main/samples/README.md Installs a minimal set of dependencies for CV-CUDA interoperability samples, including PyTorch, CuPy, PyCUDA, and PyNvVideoCodec. ```shell ./install_interop_dependencies.sh ``` -------------------------------- ### Example: Load and Run TensorRT Inference Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/common.rst Demonstrates loading a TensorRT engine and performing inference with CV-CUDA tensors. ```python # Load TensorRT engine from common import get_cache_dir model = TRT(get_cache_dir() / "resnet50.trtmodel") # Run inference input_tensors = [preprocessed_image] output_tensors = model(input_tensors) # Access results logits = output_tensors[0] ``` -------------------------------- ### Activate Virtual Environment Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/interoperability.rst Activates the virtual environment created by the installation script, making all installed dependencies available for use. ```bash source venv_samples/bin/activate ``` -------------------------------- ### Example: Build TensorRT Engine from ONNX Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/common.rst Shows how to convert an ONNX model into a TensorRT engine file. ```python engine_from_onnx( Path("model.onnx"), Path("model.trtmodel"), use_fp16=True ) ``` -------------------------------- ### Define Custom Target for Wheel Installation Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/CMakeLists.txt This custom target ensures that the wheel installation process is executed when needed. It depends on the custom command that creates the wheel installation marker file. ```cmake add_custom_target(install_wheel_for_docs DEPENDS ${WHEEL_INSTALL_MARKER}) ``` -------------------------------- ### Install CV-CUDA Libraries and Development Headers via Debian Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Install the C++/CUDA libraries and development headers using Debian package manager. Replace placeholders with actual version and architecture. ```shell sudo apt install -y \ ./cvcuda-lib----linux.deb \ ./cvcuda-dev----linux.deb ``` -------------------------------- ### Generate Installers with CPack Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Generates Debian and tarball installers for the project using CPack. This command should be run from the build directory. ```shell cd build-rel cpack . ``` -------------------------------- ### Model Setup for Classification Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/applications/classification.rst This section covers the initial setup for the classification model, including exporting a PyTorch ResNet50 model to ONNX format and building an optimized TensorRT engine. These components are cached for subsequent runs. ```python model_setup( model_path=args.model_path, onnx_path=args.onnx_path, engine_path=args.engine_path, input_shape=args.input_shape, batch_size=args.batch_size, fp16=args.fp16 ) ``` -------------------------------- ### Install Debian Build and Test Dependencies Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Installs all necessary Debian packages for building and testing CV-CUDA, including C++/CUDA libraries, Python development headers, testing frameworks, and documentation tools. ```shell sudo apt install -y \ g++-11 cmake ninja-build cuda-12-9 \ python3-dev python3-venv python3-pip \ libgtest-dev libgmock-dev libssl-dev zlib1g-dev \ fonts-dejavu doxygen graphviz ``` -------------------------------- ### Install CV-CUDA for CUDA 13 Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Use this command to install the Python wheel for CV-CUDA with CUDA 13 support from PyPI. ```shell pip install cvcuda-cu13 ``` -------------------------------- ### Install CV-CUDA for CUDA 12 Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Use this command to install the Python wheel for CV-CUDA with CUDA 12 support from PyPI. ```shell pip install cvcuda-cu12 ``` -------------------------------- ### Example: Export PyTorch Segmentation Model to ONNX Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/common.rst Demonstrates exporting a FCN ResNet101 segmentation model to ONNX format. ```python import torchvision fcn = torchvision.models.segmentation.fcn_resnet101(weights='DEFAULT') export_segmentation_onnx( fcn, Path("fcn.onnx"), (3, 224, 224) ) ``` -------------------------------- ### Install Interoperability Dependencies Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/interoperability.rst This bash script installs all necessary dependencies for CV-CUDA interoperability samples, including PyTorch, CuPy, and PyCUDA. It automatically detects your CUDA version and sets up a virtual environment. ```bash cd samples ./install_interop_dependencies.sh ``` -------------------------------- ### Model Setup for Semantic Segmentation Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/applications/segmentation.rst This section covers the setup of the FCN-ResNet101 model, including exporting it to ONNX and building a TensorRT engine. The model is pretrained on COCO+VOC datasets and supports 21 classes. ```python import os import sys import cvcuda import tensorrt as trt from model_loader import ModelLoader def begin_model_setup(): pass def end_model_setup(): pass if __name__ == "__main__": begin_model_setup() model_loader = ModelLoader() model_loader.load_model("fcn-resnet101", "cvcuda/.cache") end_model_setup() ``` -------------------------------- ### Install Python Dependencies using uv for NumPy 1.x Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Installs project dependencies using the 'uv' package manager, including NumPy 1.x and Torch. This is an alternative to 'pip' for faster dependency resolution. ```shell uv pip install -r docker/requirements.no_torch_no_numpy.txt -r docker/requirements.numpy1.txt torch==2.8.* ``` -------------------------------- ### Example: Export PyTorch Classifier to ONNX Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/common.rst Demonstrates exporting a ResNet50 model to ONNX format. ```python import torchvision model = torchvision.models.resnet50(weights='DEFAULT') export_classifier_onnx( model, Path("resnet50.onnx"), (3, 224, 224) ) ``` -------------------------------- ### Install Python Dependencies using uv for NumPy 2.x Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Installs project dependencies using the 'uv' package manager, including NumPy 2.x and Torch. This is an alternative to 'pip' for faster dependency resolution. ```shell uv pip install -r docker/requirements.no_torch_no_numpy.txt -r docker/requirements.numpy2.txt torch==2.8.* ``` -------------------------------- ### Activate Python Virtual Environment Source: https://github.com/cvcuda/cv-cuda/blob/main/bench/python/README.md Activate the Python virtual environment created by the installation script before running benchmarks. ```shell source venv_bench/bin/activate ``` -------------------------------- ### Handling Unbounded Cache Growth Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/advanced/object_cache.rst Shows how to manage potential unbounded cache growth by creating many non-wrapped objects with varying shapes. This example highlights the need for cache control. ```python import cvcuda # Create numerous non-wrapped objects with different shapes for i in range(100): shape = [i * 10 + 10, i * 10 + 10] img = cvcuda.Image.create(shape[0], shape[1], cvcuda.Type.U8) # In a real application, these images would be used and then go out of scope # At this point, the cache might have grown significantly. # Subsequent code would typically involve setting a cache limit or clearing the cache. ``` -------------------------------- ### Object Detection Model Setup and Export Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/applications/object_detection.rst This Python code configures the object detection model by loading a pretrained RetinaNet with a ResNet50-FPN backbone, adding the TensorRT EfficientNMS plugin, exporting the pipeline to ONNX, and building an optimized TensorRT engine. EfficientNMS performs Non-Maximum Suppression on the GPU. ```python import cvcuda import tensorrt as trt from polygraphy.comparator import compare from polygraphy.func import identity from polygraphy.json import save_json from polygraphy.logger import LOGGER from polygraphy.util import save_revision from torch import nn from torch.nn import functional as F from torchvision.models.detection import RetinaNet from torchvision.models.detection.backbone_utils import resnet_fpn_backbone class RetinaNetEfficientNMS(nn.Module): def __init__(self, model, max_detections=100, score_threshold=0.05, iou_threshold=0.5): super().__init__() self.model = model self.max_detections = max_detections self.score_threshold = score_threshold self.iou_threshold = iou_threshold def forward(self, x, targets=None): # RetinaNet outputs a list of dictionaries, one for each image in the batch # Each dictionary contains 'boxes', 'labels', and 'scores' outputs = self.model(x) # If targets are provided (during training), return them along with outputs if targets is not None: return outputs, targets # For inference, we need to process the outputs to be compatible with EfficientNMS # EfficientNMS expects inputs in the format: num_detections, boxes, scores, classes # We need to convert the list of dictionaries into tensors # Concatenate boxes, scores, and labels across all images in the batch boxes = [] scores = [] classes = [] for output in outputs: boxes.append(output["boxes"]) scores.append(output["scores"]) # EfficientNMS expects class indices, RetinaNet provides labels (which are class indices) # We add 1 to labels because EfficientNMS uses 1-based indexing for classes, and 0 is background classes.append(output["labels"]) # Stack the tensors to create batch dimensions boxes = torch.stack(boxes) scores = torch.stack(scores) classes = torch.stack(classes) # Pad tensors to max_detections if necessary # This is a simplification; a more robust implementation would handle variable lengths more carefully max_len = max(len(b) for b in boxes) padded_boxes = F.pad(boxes, (0, 0, 0, max_len - boxes.shape[1])) padded_scores = F.pad(scores, (0, max_len - scores.shape[1])) padded_classes = F.pad(classes, (0, max_len - classes.shape[1])) # EfficientNMS expects num_detections as the first output num_detections = torch.full((boxes.shape[0], 1), max_len, dtype=torch.int32) return num_detections, padded_boxes, padded_scores, padded_classes def export_model(onnx_path, trt_engine_path, width=224, height=224): LOGGER.info(f"Exporting model to ONNX: {onnx_path}") # Load pretrained RetinaNet model with ResNet50-FPN backbone backbone = resnet_fpn_backbone("resnet50", pretrained=True) model = RetinaNet(backbone, numclasses=91) # COCO dataset has 90 classes + background model.eval() # Wrap the model with EfficientNMS logic # Note: This is a conceptual representation. Actual integration might require modifying the model or using a custom layer. # For this example, we assume a wrapper class `RetinaNetEfficientNMS` exists. # In a real scenario, you'd integrate EfficientNMS directly into the model graph or use a TensorRT plugin. model_with_nms = RetinaNetEfficientNMS(model) # Create a dummy input tensor dummy_input = torch.randn(1, 3, height, width, device="cuda") # Export to ONNX torch.onnx.export( model_with_nms, dummy_input, onnx_path, opset_version=14, input_names=["input"], output_names=["num_detections", "boxes", "scores", "classes"], dynamic_axes={ "input": {"0": "batch_size", "2": "height", "3": "width"}, "num_detections": {"0": "batch_size"}, "boxes": {"0": "batch_size"}, "scores": {"0": "batch_size"}, "classes": {"0": "batch_size"}, }, ) LOGGER.success(f"Model exported to {onnx_path}") LOGGER.info(f"Building TensorRT engine: {trt_engine_path}") # Build TensorRT engine from ONNX model # This part requires Polygraphy or TensorRT's Python API to load and optimize the ONNX model # Example using Polygraphy: compare( onnx_path, inputs=dummy_input, outputs=["num_detections", "boxes", "scores", "classes"], # Specify the target TensorRT engine path trt_engine_outputs=trt_engine_path, # Specify optimization profiles for dynamic axes trt_optimization_profiles={ "input": [("batch_size", 1, 4, 16), ("height", 224, 256, 512), ("width", 224, 256, 512)], "num_detections": ["batch_size"], "boxes": ["batch_size"], "scores": ["batch_size"], "classes": ["batch_size"], }, # Specify TensorRT builder flags if needed trt_builder_flags=[ trt.BuilderFlag.FP16, # Use FP16 precision for faster inference trt.BuilderFlag.STRICT_TYPES, # Ensure output types match ONNX ], # Save the engine after comparison save_engine=True, ) LOGGER.success(f"TensorRT engine built at {trt_engine_path}") def setup_model(width, height): # Define paths for ONNX model and TensorRT engine onnx_path = "./retinanet_efficientnms.onnx" trt_engine_path = "./retinanet_efficientnms.trt" # Check if TensorRT engine already exists, if not, export and build it if not os.path.exists(trt_engine_path): export_model(onnx_path, trt_engine_path, width, height) else: LOGGER.info(f"TensorRT engine found at {trt_engine_path}. Skipping export and build.") return trt_engine_path ``` -------------------------------- ### Install Python Dependencies using venv for NumPy 1.x Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Installs project dependencies, including pytest and typing-extensions, along with NumPy 1.x and Torch, into a virtual environment. Use this for projects targeting older NumPy versions. ```shell python3 -m pip install -r docker/requirements.no_torch_no_numpy.txt -r docker/requirements.numpy1.txt torch==2.8.* ``` -------------------------------- ### C Header Compatibility Test Setup Source: https://github.com/cvcuda/cv-cuda/blob/main/tests/cvcuda/system/CMakeLists.txt Configures a C header compatibility test using the C11 standard, depending on the cvcuda library and specified C headers. ```cmake get_target_property(CVCUDA_SOURCE_DIR cvcuda SOURCE_DIR) # Gather C headers file(GLOB_RECURSE CAPI_HEADERS RELATIVE "${CVCUDA_SOURCE_DIR}/include" CONFIGURE_DEPENDS "${CVCUDA_SOURCE_DIR}/include/*.h") add_header_compat_test(TARGET cvcuda_test_capi_header_compat SOURCE TestAPI.c STANDARD c11 DEPENDS cvcuda HEADERS ${CAPI_HEADERS}) ``` -------------------------------- ### Run CV-CUDA Hello World Sample Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/getting_started.rst Executes the 'hello_world.py' sample application to demonstrate basic CV-CUDA functionality. ```bash python3 samples/applications/hello_world.py ``` -------------------------------- ### Run Hello World with Custom Image Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples.rst Runs the 'Hello World' sample using a custom input image and specifying an output file. This allows testing the pipeline with your own data. ```bash python3 samples/applications/hello_world.py -i your_image.jpg -o output.jpg ``` -------------------------------- ### Run Hello World with Custom Dimensions Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/applications/hello_world.rst Processes a single input image ('input.jpg') and saves the output ('output.jpg') after resizing to custom dimensions (512x512). ```bash python3 hello_world.py -i input.jpg -o output.jpg --width 512 --height 512 ``` -------------------------------- ### Configure Installed Python Test Script Source: https://github.com/cvcuda/cv-cuda/blob/main/tests/cvcuda/python/CMakeLists.txt Generates the Python test script that will be installed. It correctly sets the Python module directory, ensuring that Python can locate the 'cvcuda' and 'nvcv' modules after installation, especially when installed in custom paths. ```cmake # CRITICAL: Python modules are installed to ${CMAKE_INSTALL_LIBDIR}/python/, not just ${CMAKE_INSTALL_LIBDIR}/ # The /python suffix is required for the test script to correctly set PYTHONPATH and find cvcuda/nvcv modules. # Without this suffix, Python will fail with "ModuleNotFoundError: No module named 'cvcuda'" # # Why this matters: # - DEB packages install to system paths (/usr/lib/python3.X/dist-packages/) which are in Python's default search path # - TAR archives install to custom paths (/opt/nvidia/cvcuda*/lib/python/) which require PYTHONPATH to be set # - Removing /python will cause TAR tests to fail while DEB tests still pass, making the bug subtle to detect set(PYTHON_MODULE_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/python) configure_file(cvcuda_test_python.in cvcuda_test_python @ONLY) ``` -------------------------------- ### Run Stack Sample Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/operators/stack.rst Execute the stack sample script from the command line, specifying an input image. ```bash python3 stack.py -i input.jpg ``` -------------------------------- ### Initialize Repository Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Runs the initialization script for the cloned repository. This should be done only once. ```shell cd ~/cvcuda ./init_repo.sh ``` -------------------------------- ### Generate CMake Script for Wheel Installation Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/CMakeLists.txt This script is generated by CMake to find and install the project's wheel file into the virtual environment. It handles locating the wheel, installing it using pip, and creating a marker file upon successful installation. ```cmake file(WRITE ${INSTALL_WHEEL_SCRIPT} " # Script to find and install the cvcuda wheel at build time file(GLOB WHEEL_FILES "${WHEEL_DIR}/*.whl") if(NOT WHEEL_FILES) message(FATAL_ERROR "No wheel files found in ${WHEEL_DIR}") endif() list(GET WHEEL_FILES 0 WHEEL_FILE) message(STATUS "Installing wheel: ${WHEEL_FILE}") execute_process( COMMAND "${VENV_PYTHON}" -m pip install --force-reinstall "${WHEEL_FILE}" RESULT_VARIABLE INSTALL_RESULT ) if(NOT INSTALL_RESULT EQUAL 0) message(FATAL_ERROR "Failed to install wheel: ${WHEEL_FILE}") endif() # Create marker file to indicate successful installation file(TOUCH "${MARKER_FILE}") ") ``` -------------------------------- ### Run Hello World with Default Parameters Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/applications/hello_world.rst Executes the hello_world.py script with default settings, processing the 'tabby_tiger_cat.jpg' image. ```bash python3 hello_world.py ``` -------------------------------- ### Run Reformat Sample Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/operators/reformat.rst Execute the Reformat sample script with a specified input image file. ```bash python3 reformat.py -i input.jpg ``` -------------------------------- ### Install CV-CUDA Wheel using pip and venv Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Use this command to install a CV-CUDA Python wheel using the standard pip package installer within a virtual environment. Ensure you replace '' with the actual version number. ```shell python3 -m pip install ./cvcuda_cu12--cp310.cp311-cp310.cp311-linux_x86_64.whl ``` -------------------------------- ### Run Hello World with Multiple Images Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/applications/hello_world.rst Processes multiple input images in a single batch and saves corresponding output images. ```bash python3 hello_world.py -i img1.jpg img2.jpg img3.jpg -o out1.jpg out2.jpg out3.jpg ``` -------------------------------- ### Run Labeling Sample Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/operators/label.rst Execute the label sample using Python 3, specifying input and output image paths. ```bash python3 label.py -i input.jpg -o labeled.jpg ``` -------------------------------- ### Initialize NvImgCodec Session Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/interop/nvimgcodec.rst Set up the NvImgCodec library by creating a session. This involves configuring the codec with desired parameters. ```python # docs_tag: begin_init_nvimgcodec # Initialize NvImgCodec codec = NvImgCodec() # Create a session config = NvImgCodecConfig() config.device_id = 0 # Specify the GPU device ID config.mem_type = NvImgCodecConfig.NvImgCodecMemType.NVIMGCODEC_MEM_TYPE_CUDA session = NvImgCodecSession(codec, config) # docs_tag: end_init_nvimgcodec ``` -------------------------------- ### Run Development Container Source: https://github.com/cvcuda/cv-cuda/blob/main/docker/README.md Execute a development container, automatically selecting the correct architecture. Mount your source code for development. ```bash # Run development container (automatically selects correct architecture) docker run -it --gpus all devel_u22.04_cu12.5.0_num1_torch2.8.0:v2 # Mount source code for development docker run -it --gpus all \ -v /path/to/cvcuda:/workspace \ devel_u22.04_cu12.5.0_num1_torch2.8.0:v2 ``` -------------------------------- ### Create a Tensor Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/datatypes.rst Demonstrates how to create a Tensor object. Ensure necessary imports are present. ```python import cvcuda # Create a tensor with shape (1, 3, 224, 224) and NHWC layout tensor = cvcuda.Tensor.create_from_shape(cvcuda.TensorShape(1, 3, 224, 224), cvcuda.Type.U8, cvcuda.TensorLayout.NHWC) # Print tensor information print(f"Tensor shape: {tensor.shape}") print(f"Tensor type: {tensor.dtype}") print(f"Tensor layout: {tensor.layout}") ``` -------------------------------- ### Run Object Detection Sample Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/applications/object_detection.rst Execute the object detection sample from the command line. Specify input and output image paths as needed. The sample automatically handles model download, export, and TensorRT engine building on the first run. ```bash python3 object_detection.py -i image.jpg ``` ```bash python3 object_detection.py -i street.jpg -o detections.jpg ``` -------------------------------- ### Install CV-CUDA Python Bindings via Debian Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Install the Python bindings for CV-CUDA using the Debian package manager. Replace placeholders with actual versions and architecture. ```shell sudo apt install -y \ ./cvcuda-python----linux.deb ``` -------------------------------- ### Build CV-CUDA with Documentation and Python Support Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Builds the project in release mode, enabling documentation generation and Python bindings. Use this to include documentation and Python features in your build. ```shell ci/build.sh release build-rel -DBUILD_DOCS=1 -DBUILD_PYTHON=1 ``` -------------------------------- ### Generate Specific Installers with CPack Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Generates specific types of installers (Debian or tarballs) using CPack with the -G flag. Use 'DEB' for Debian packages and 'TXZ' for tarballs. ```shell cpack . -G [DEB|TXZ] ``` -------------------------------- ### Install CV-CUDA Tests via Debian Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Install the CV-CUDA test packages using the Debian package manager. Replace placeholders with actual version, CUDA version, and architecture. ```shell sudo apt install -y \ ./cvcuda-tests----linux.deb ``` -------------------------------- ### Check for manylinux Python Installation Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/CMakeLists.txt Checks if a manylinux Python installation exists at the constructed path. If found, it sets the Python root directory to this path, prioritizing the manylinux version. ```cmake if(EXISTS ${MANYLINUX_PYTHON_PATH}) message(STATUS "Found manylinux Python path: ${MANYLINUX_PYTHON_PATH}") set(Python_ROOT_DIR ${MANYLINUX_PYTHON_PATH}) endif() ``` -------------------------------- ### Apply Gaussian blur with default parameters Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/operators/gaussian.rst Run the Gaussian blur sample with a default input image and observe the output with default kernel size and sigma. ```bash python3 gaussian.py -i input.jpg ``` -------------------------------- ### Create an Image Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/datatypes.rst Shows how to allocate and initialize an Image object with specified dimensions, format, and layout. Requires cvcuda import. ```python import cvcuda # Create an 8-bit RGB interleaved image with dimensions 1920x1080 image = cvcuda.Image.create(1920, 1080, cvcuda.Format.RGB8, cvcuda.Layout.PITCH_LINEAR) # Print image information print(f"Image width: {image.width}") print(f"Image height: {image.height}") print(f"Image format: {image.format}") print(f"Image layout: {image.layout}") ``` -------------------------------- ### Run All CV-CUDA Samples Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples.rst Executes all available CV-CUDA samples using default parameters. This is useful for a comprehensive test of the library's functionalities. ```bash ./samples/run_samples.sh ``` -------------------------------- ### Labeling Sample Preprocessing Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/operators/label.rst This snippet handles the preprocessing steps for the labeling sample, including converting the image to grayscale, applying histogram equalization for contrast enhancement, and thresholding to create a binary image. ```python image_rgb = cvcuda.read_image(args.input) image_gray = cvcuda.cvtcolor(image_rgb, cvcuda.COLOR_RGB2GRAY) image_eq = cvcuda.histogrameq(image_gray) # Threshold to create binary image # The threshold value is uploaded to the device threshold_val = 128 threshold_val_d = cuda_memcpy_h2d(np.array([threshold_val], dtype=np.uint8)) image_binary = cvcuda.threshold(image_eq, threshold_val_d) ``` -------------------------------- ### Stack Sample Implementation Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/operators/stack.rst This is the core implementation of the stack sample, demonstrating how to use the cvcuda.stack operator. ```python import cvcuda import numpy as np def main(): # Create dummy input images (e.g., 3 images of shape 480x640x3) # In a real scenario, these would be loaded from files or other sources. dummy_image1 = np.random.randint(0, 256, size=(480, 640, 3), dtype=np.uint8) dummy_image2 = np.random.randint(0, 256, size=(480, 640, 3), dtype=np.uint8) dummy_image3 = np.random.randint(0, 256, size=(480, 640, 3), dtype=np.uint8) images = [dummy_image1, dummy_image2, dummy_image3] # Stack the images into a batch # The result will be a tensor of shape (3, 480, 640, 3) batch = cvcuda.stack(images) print(f"Stacked batch shape: {batch.shape}") print(f"Stacked batch dtype: {batch.dtype}") # Optionally, split the batch back into individual tensors # This demonstrates the inverse operation, useful for processing individual items later. individual_images = cvcuda.zero_copy_split(batch) print(f"Split back to {len(individual_images)} individual images.") for i, img in enumerate(individual_images): print(f" Image {i} shape: {img.shape}") if __name__ == "__main__": main() ``` -------------------------------- ### Install Python Dependencies using venv for NumPy 2.x Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Installs project dependencies, including pytest and typing-extensions, along with NumPy 2.x and Torch, into a virtual environment. Use this for projects targeting newer NumPy versions. ```shell python3 -m pip install -r docker/requirements.no_torch_no_numpy.txt -r docker/requirements.numpy2.txt torch==2.8.* ``` -------------------------------- ### Run Semantic Segmentation Sample Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/applications/segmentation.rst Execute the semantic segmentation sample from the command line. Specify input and output image paths. The sample will automatically download the model, build the TensorRT engine, perform segmentation, apply post-processing, and save the result. ```bash python3 segmentation.py -i image.jpg ``` ```bash python3 segmentation.py -i portrait.jpg -o segmented_portrait.jpg ``` -------------------------------- ### Required Imports for NumPy Interoperability Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/interop/numpy.rst These imports are necessary to utilize the NumPy interoperability features demonstrated in the example. ```python # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 # # 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. import numpy as np import torch import cupy as cp from pycuda import gpuarray from cuda import cuda import threading import time import os from cuda import cudart from cuda.cudart import cudaStream_t # Define a simple CUDA kernel def cuda_kernel(input_array, output_array): threads_per_block = 128 blocks_per_grid = (input_array.size + threads_per_block - 1) // threads_per_block # Launch the kernel # This is a placeholder and would typically involve actual CUDA kernel code # For demonstration, we simulate a GPU operation output_array[:] = input_array * 2 # In a real scenario, you would use CUDA C/C++ kernels compiled and launched via CUDA Python # Example: kernel_function[blocks_per_grid, threads_per_block](input_array, output_array) pass ``` -------------------------------- ### Check NVCV Directory Source: https://github.com/cvcuda/cv-cuda/blob/main/tests/nvcv_types/standalone/CMakeLists.txt Checks if the NVCV_DIR environment variable is set. This variable is crucial for locating the NVCV installation. ```cmake if(NOT NVCV_DIR) message(FATAL_ERROR "NVCV_DIR is empty! Path to NVCV directory must be given.") endif() ``` -------------------------------- ### Build CV-CUDA with Default Release Configuration Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/installation.rst Executes the build script with default settings for a release build. This is the most basic way to build the project. ```shell ci/build.sh ``` -------------------------------- ### Sphinx Build Configuration Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/CMakeLists.txt Sets up source and build directories for Sphinx documentation, along with the expected index file path. Also defines paths for C and C++ API reStructuredText files. ```cmake set(SPHINX_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/sphinx) set(SPHINX_BUILD ${CMAKE_CURRENT_BINARY_DIR}/sphinx) set(SPHINX_INDEX_FILE ${SPHINX_BUILD}/index.html) set(C_API_RST ${SPHINX_SOURCE}/_c_api) set(CPP_API_RST ${SPHINX_SOURCE}/_cpp_api) ``` -------------------------------- ### Required Imports for PyTorch Interoperability Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/interop/pytorch.rst Import necessary libraries for PyTorch and CV-CUDA integration. Ensure PyTorch is installed and accessible. ```python import torch import cvcuda ``` -------------------------------- ### Run All CV-CUDA Samples Source: https://github.com/cvcuda/cv-cuda/blob/main/samples/README.md Executes all CV-CUDA samples, categorized into operators/applications and interoperability scripts. ```shell ./run_samples.sh # Operators and applications ./run_interop.sh # Interoperability samples ``` -------------------------------- ### Find cvcuda and CUDAToolkit Packages Source: https://github.com/cvcuda/cv-cuda/blob/main/python/common/CMakeLists.txt Locates the cvcuda and CUDAToolkit packages required for the project. Ensure these packages are installed and discoverable by CMake. ```cmake find_package(cvcuda REQUIRED) find_package(CUDAToolkit REQUIRED) ``` -------------------------------- ### Login to Docker Registry Source: https://github.com/cvcuda/cv-cuda/blob/main/docker/README.md Authenticate with a Docker registry before pushing or pulling images. This is necessary for private registries or when required by your CI/CD setup. ```bash docker login ``` -------------------------------- ### Initialize PyNvVideoCodec Decoder and Encoder Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/interop/pynvvideocodec.rst Initialize the PyNvVideoCodec decoder for input and the encoder for output. The decoder is set to output RGB frames to device memory, while the encoder expects NV12 format at 640x480. ```python # docs_tag: begin_init_pynvvideocodec # Initialize decoder decoder = pynvvcodec.PyNvDecoder(input_color_fmt=pynvvcodec.ColorFormat.RGB, output_memory_type=pynvvcodec.MemoryType.DEVICE) # Initialize encoder encoder = pynvvcodec.PyNvEncoder(input_width=640, input_height=480, input_color_fmt=pynvvcodec.ColorFormat.NV12) # docs_tag: end_init_pynvvideocodec ``` -------------------------------- ### Cache Reuse Example Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/advanced/object_cache.rst Illustrates how CV-CUDA reuses cached memory for new objects with identical specifications. This avoids new memory allocations. ```python import cvcuda # Create the first tensor tensor1 = cvcuda.Tensor.create([10, 10], cvcuda.Type.F32) # Create a second tensor with identical specifications # This will reuse the memory allocated for tensor1 tensor2 = cvcuda.Tensor.create([10, 10], cvcuda.Type.F32) # Verify that tensor2 reuses tensor1's memory (implementation specific check) # In a real scenario, you'd observe performance benefits or check memory addresses print(f"Tensor 1 memory address: {tensor1.buffer_ptr()}") print(f"Tensor 2 memory address: {tensor2.buffer_ptr()}") ``` -------------------------------- ### Import necessary libraries for CuPy interoperability Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/interop/cupy.rst Import CuPy and CV-CUDA for GPU array operations and tensor conversions. Ensure CuPy is installed and accessible. ```python # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 # 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. import cupy import cvcuda ``` -------------------------------- ### Build and Push Multi-Arch Docker Images Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/docker_images.rst Builds multi-architecture Docker images (x86_64 and aarch64) and pushes them to a specified registry. This requires registry authentication. ```shell ./build_dockers.sh $REGISTRY_PREFIX multiarch ``` -------------------------------- ### Process Image with CV-CUDA Operations Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/interop/nvimgcodec.rst Apply CV-CUDA operations, such as resizing, to the image tensor. This example resizes the image to 224x224 using cubic interpolation. ```python # docs_tag: begin_cvcuda_resize # Resize the image to 224x224 using cubic interpolation resized_tensor = cvcuda.resize( src=cv_cuda_tensor, dsize=(224, 224), interpolation=cvcuda.Interp.CUBIC, ) # docs_tag: end_cvcuda_resize ``` -------------------------------- ### Set manylinux Python Path Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/CMakeLists.txt Constructs the path for a manylinux Python installation based on the Python version. This path is used to locate the Python executable in manylinux environments. ```cmake string(REPLACE "." "" PYTHON_VER_NODOT ${PYTHON_VER}) set(MANYLINUX_PYTHON_PATH "/opt/python/cp${PYTHON_VER_NODOT}-cp${PYTHON_VER_NODOT}") ``` -------------------------------- ### Run Individual CV-CUDA Samples Source: https://github.com/cvcuda/cv-cuda/blob/main/samples/README.md Executes specific CV-CUDA sample scripts for operators, applications, and interoperability. ```shell python3 operators/label.py python3 applications/classification.py python3 interoperability/pytorch_interop.py ``` -------------------------------- ### Configure Python Test Script Source: https://github.com/cvcuda/cv-cuda/blob/main/tests/cvcuda/python/CMakeLists.txt Generates the Python test script by configuring a template file. This script is crucial for setting up the Python environment to find installed CV-CUDA modules. ```cmake configure_file(cvcuda_test_python.in ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/cvcuda_test_python @ONLY) ``` -------------------------------- ### Specify custom output path for Gaussian blur Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/samples/operators/gaussian.rst Run the Gaussian blur sample and specify a custom file path for the blurred output image. ```bash python3 gaussian.py -i image.jpg -o blurred.jpg ``` -------------------------------- ### Add CUDA Environment Variables to .bashrc Source: https://github.com/cvcuda/cv-cuda/blob/main/docs/sphinx/wsl2.rst Append commands to your ~/.bashrc file to automatically set CUDA environment variables every time you start a new shell session in WSL2. ```shell echo 'export CUDA_PATH=/usr/local/cuda' >> ~/.bashrc echo 'export PATH=$CUDA_PATH/bin:$PATH' >> ~/.bashrc echo 'export LIBRARY_PATH=$CUDA_PATH/lib64/stubs:$CUDA_PATH/lib64:$LIBRARY_PATH' >> ~/.bashrc echo 'export LD_LIBRARY_PATH=$CUDA_PATH/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc ``` -------------------------------- ### Reinstall Python Packages for CUDA 13 Source: https://github.com/cvcuda/cv-cuda/blob/main/bench/python/README.md Manually reinstall Python packages using the CUDA 13 requirements file. Ensure system dependencies are already installed and the virtual environment is activated. ```shell cd bench/python source venv_bench/bin/activate python3 -m pip install -r requirements_cu13.txt ``` -------------------------------- ### Summarize a Single Benchmark Run Source: https://github.com/cvcuda/cv-cuda/blob/main/bench/python/README.md Use bench_utils.py to summarize a single benchmark run into a CSV file. Provide the output directory, the benchmark JSON path, and a baseline name. ```bash python3 bench/python/bench_utils.py -o -b -bn baseline ``` -------------------------------- ### Reinstall Python Packages for CUDA 12 Source: https://github.com/cvcuda/cv-cuda/blob/main/bench/python/README.md Manually reinstall Python packages using the CUDA 12 requirements file. Ensure system dependencies are already installed and the virtual environment is activated. ```shell cd bench/python source venv_bench/bin/activate python3 -m pip install -r requirements_cu12.txt ``` -------------------------------- ### C++ Header Compatibility Test Setup Source: https://github.com/cvcuda/cv-cuda/blob/main/tests/cvcuda/system/CMakeLists.txt Configures a C++ header compatibility test using the C++11 standard, excluding optools files, depending on the cvcuda library and specified C++ headers. ```cmake # Gather C++ headers file(GLOB_RECURSE CXXAPI_HEADERS RELATIVE "${CVCUDA_SOURCE_DIR}/include" CONFIGURE_DEPENDS "${CVCUDA_SOURCE_DIR}/include/*.hpp") # remove optools files, they are c++17 list(FILTER CXXAPI_HEADERS EXCLUDE REGEX "cuda_tools/") add_header_compat_test(TARGET cvcuda_test_cxxapi_header_compat SOURCE TestAPI.cpp STANDARD c++11 DEPENDS cvcuda HEADERS ${CXXAPI_HEADERS}) ``` -------------------------------- ### Use Builder Image for Package Creation Source: https://github.com/cvcuda/cv-cuda/blob/main/docker/README.md Utilize the builder image for creating wheels. This command automatically selects the correct architecture. ```bash # Use builder for creating wheels (automatically selects correct architecture) docker run -it --gpus all \ -v /path/to/cvcuda:/workspace \ builder_cu12.5.0_gcc10:v1 ```