NanoOWL Documentation

repository·main·Indexed 19 days ago

https://github.com/nvidia-ai-iot/nanoowl

NanoOWL optimizes the OWL-ViT model using NVIDIA TensorRT for real-time open-vocabulary object detection on Jetson Orin platforms. It features a 'tree prediction' pipeline for hierarchical detection and classification, combining OWL-ViT and CLIP. The library includes the OwlPredictor class for basic detection and a tree prediction system supporting nested detection and mutually exclusive classification via specific prompt syntax.

Tokens
1.5K
Snippets
5
Records
5
Agent score
16%

What's inside NanoOWL

  1. Install NanoOWL and build the image encoder engine

    main

    To use NanoOWL on NVIDIA Jetson Orin platforms, follow these steps to install dependencies, the package itself, and build the required TensorRT engine for the OWL-ViT vision encoder.

    1. Install Dependencies

    Ensure you have installed:

    • PyTorch
    • torch2trt
    • NVIDIA TensorRT
    • transformers library via python3 -m pip install transformers

    2. Install NanoOWL

    Clone the repository and install the package in development mode:

    git clone https://github.com/NVIDIA-AI-IOT/nanoowl
    cd nanoowl
    python3 setup.py develop --user

    3. Build the TensorRT Engine

    You must build the engine for the vision encoder before running predictions:

    mkdir -p data
    python3 -m nanoowl.build_image_encoder_engine data/owl_image_encoder_patch32.engine
    # Install dependencies
    python3 -m pip install transformers
    
    # Install NanoOWL
    git clone https://github.com/NVIDIA-AI-IOT/nanoowl
    cd nanoowl
    python3 setup.py develop --user
    
    # Build the engine
    mkdir -p data
    python3 -m nanoowl.build_image_encoder_engine data/owl_image_encoder_patch32.engine
  2. Run Tree Prediction Live Demo with Camera

    main

    You can run a live interactive demo of the tree predictor using a connected camera and a web interface.

    1. Ensure a camera is connected.
    2. Navigate to the demo directory and launch the script, passing the path to your engine file:
      cd examples/tree_demo
      python3 tree_demo.py ../../data/owl_image_encoder_patch32.engine
    3. Open a browser and navigate to http://<ip address>:7860.
    4. Enter prompts in the web UI to see real-time results (e.g., [a face [a nose, an eye, a mouth]]).
    cd examples/tree_demo
    python3 tree_demo.py ../../data/owl_image_encoder_patch32.engine
  3. Use Tree Prediction for nested detection and classification

    main

    NanoOWL's "tree prediction" pipeline combines OWL-ViT and CLIP to enable nested detection and classification. You can define hierarchical relationships or simple classifications using a specific prompt syntax.

    Prompt Syntax Patterns:

    • Nested Detection: Use square brackets [] to define sub-objects within a detected object.
      • Example: "[an owl [a wing, an eye]]" (Detect an owl, then detect wings and eyes inside the owl regions).
    • Classification: Use parentheses () to provide mutually exclusive classes.
      • Example: "(indoors, outdoors)".
    • Combined Logic: Combine classification and nested detection.
      • Example: "(indoors, outdoors [an owl])" (Classify as indoors/outdoors; if outdoors, detect owls).

    CLI Usage:

    Run tree_predict.py from the examples directory:

    python3 tree_predict.py \
        --prompt="[an owl [a wing, an eye]]" \
        --threshold=0.15 \
        --image_encoder_engine=../data/owl_image_encoder_patch32.engine
    # Nested detection example
    python3 tree_predict.py \
        --prompt="[an owl [a wing, an eye]]" \
        --threshold=0.15 \
        --image_encoder_engine=../data/owl_image_encoder_patch32.engine
    
    # Classification example
    python3 tree_predict.py \
        --prompt="(indoors, outdoors)" \
        --threshold=0.15 \
        --image_encoder_engine=../data/owl_image_encoder_patch32.engine
  4. Use OwlPredictor for basic object detection

    main

    The OwlPredictor class allows you to perform open-vocabulary object detection using a TensorRT optimized OWL-ViT model. You provide a PIL Image and a list of text labels to detect.

    Parameters:

    • model_name: The HuggingFace model path (e.g., "google/owlvit-base-patch32").
    • image_encoder_engine: Path to the built .engine file.
    • predict(image, text, threshold):
      • image: A PIL Image object.
      • text: A list of strings representing the objects to detect.
      • threshold: Confidence threshold for detection.
    from nanoowl.owl_predictor import OwlPredictor
    import PIL.Image
    
    predictor = OwlPredictor(
        "google/owlvit-base-patch32",
        image_encoder_engine="data/owlvit-base-patch32-image-encoder.engine"
    )
    
    image = PIL.Image.open("assets/owl_glove_small.jpg")
    output = predictor.predict(image=image, text=["an owl", "a glove"], threshold=0.1)
    
    print(output)
  5. Run basic prediction via CLI

    main

    You can run a basic prediction from the command line using the owl_predict.py script. This is useful for testing your setup or profiling inference performance.

    CLI Flags:

    • --prompt: A list of labels in a string format, e.g., "[an owl, a glove]".
    • --threshold: Confidence threshold (e.g., 0.1).
    • --image_encoder_engine: Path to the .engine file.
    • --profile: Set this flag to profile inference performance.

    By default, the output visualization is saved to data/owl_predict_out.jpg.

    cd examples
    python3 owl_predict.py \
        --prompt="[an owl, a glove]" \
        --threshold=0.1 \
        --image_encoder_engine=../data/owl_image_encoder_patch32.engine