PyCoral API Documentation

repository·master·Indexed 19 days ago

https://github.com/google-coral/pycoral

A Python API for running inferences and performing on-device transfer learning using TensorFlow Lite models on Coral Edge TPU hardware. The library includes modules for image classification (pycoral.adapters.classify), object detection (pycoral.adapters.detect), dataset management, and model fine-tuning.

Tokens
2.5K
Snippets
7
Records
11
Agent score
64%

What's inside PyCoral

  1. Overview of the PyCoral API modules

    master

    The PyCoral library is organized into several functional modules designed for working with Coral Edge TPU models. Key functional areas include:

    • Adapters: High-level interfaces for common tasks like image classification (pycoral.adapters.classify) and object detection (pycoral.adapters.detect).
    • Utils: Utility modules for dataset management (pycoral.utils.dataset) and Edge TPU hardware interaction (pycoral.utils.edgetpu).
    • Pipeline: Advanced execution patterns, such as the pycoral.pipeline.pipelined_model_runner for optimized model inference.
    • Learning: Modules for model fine-tuning and adaptation, including backpropagation-based methods (pycoral.learn.backprop.softmax_regression) and imprinting engines (pycoral.learn.imprinting.engine).
  2. Access PyCoral documentation and examples

    master

    PyCoral provides an API for running inferences and performing on-device transfer learning with TensorFlow Lite models on Coral devices.

  3. Understand Class and Object abstractions in pycoral.adapters

    master

    PyCoral uses specific data classes to represent model outputs:

    • pycoral.adapters.classify.Class: Represents a single classification result, typically containing the label/name of the category.
    • pycoral.adapters.detect.Object: Represents a single detected entity in an image.
    • pycoral.adapters.detect.BBox: Represents the bounding box of a detected object, providing the spatial coordinates of the detection.
  4. Export PyCoral API documentation formats

    master

    When building the documentation with makedocs.sh, you can choose different output formats:

    • Local Preview: Run bash makedocs.sh -p. The files are output to _build/preview/. You can view them by opening the index.html file in a browser.
    • Web Publishing: Run bash makedocs.sh -w. The files are output to _build/web/ and are designed for publishing on the official Coral website.
    # For local viewing
    bash makedocs.sh -p
    
    # For web publishing
    bash makedocs.sh -w
  5. Download requirements for PyCoral examples

    master

    The examples/install_requirements.sh script automates the downloading of models and assets required for the examples.

    • To download requirements for all examples, run the script without arguments.
    • To download requirements for a specific example, pass the filename of that example to the script.
    # Download requirements for all examples
    bash examples/install_requirements.sh
    
    # Download requirements for a specific example (replace <filename> with the actual file)
    bash examples/install_requirements.sh <filename>
  6. Run PyCoral API examples

    master

    To run the provided examples for inference or on-device transfer learning, follow these steps:

    1. Set up your Coral device: Ensure your Coral hardware is configured and the PyCoral library is installed according to the official Coral setup guide.
    2. Clone the repository: Clone the pycoral repository onto your Coral board or the host system if using a Coral accelerator.
    3. Install example requirements: Use the install_requirements.sh script to download the specific models and files needed for the examples. You can download requirements for all examples or specify a single filename to download only what is needed for that specific example.
    4. Execute the example: Run the command specified at the top of each .py file. Note that some examples may require additional manual downloads as noted in their code comments.
    # Clone the repository
    git clone https://github.com/google-coral/pycoral
    
    # Navigate to the repo and download requirements for all examples
    cd pycoral
    bash examples/install_requirements.sh
  7. Build PyCoral from source

    master

    If you need to build the library yourself, ensure you have version-matching builds of libcoral and libedgetpu. These are submodules of the pycoral repository and must share the same TENSORFLOW_COMMIT value.

    Prerequisites

    • Docker must be installed (the build process is Docker-based).
    • Submodules (libcoral and libedgetpu) must be initialized.

    Build Steps

    1. Clone the repository with submodules:
      git clone --recurse-submodules https://github.com/google-coral/pycoral
      Or, if already cloned:
      cd pycoral
      git submodule init && git submodule update
    2. Build the pybind11-based native layer using the provided script:
      scripts/build.sh
    3. Generate and install the Python wheel:
      make wheel
      pip3 install $(ls dist/*.whl)
    git clone --recurse-submodules https://github.com/google-coral/pycoral
    cd pycoral
    scripts/build.sh
    make wheel
    pip3 install $(ls dist/*.whl)
  8. Install the PyCoral library

    master

    To install the prebuilt PyCoral library, follow the instructions at coral.ai/software/.

    Important for Debian users: If you are using a Debian-based system, you must install the library using apt-get rather than pip. Using pip install may cause compatibility issues with other Coral libraries that are required to be installed via apt-get.

  9. Build the PyCoral API reference documentation

    master

    To build the PyCoral API reference documentation using Sphinx, you must first synchronize submodules and build the pywrap library. Because of GLIBC dependencies, it is recommended to perform the build inside a Docker container that matches the environment used for the library build.

    After setting up the environment and installing libedgetpu, install the Python requirements and run the documentation build script. The output is located in _build/.

    # 1. Sync submodules in the repo root
    git submodule init && git submodule update
    
    # 2. Build the pywrap lib (example for Python 3.9)
    DOCKER_CPUS=k8 scripts/build.sh --python_versions 39
    
    # 3. Open a matching Docker shell (example for Ubuntu 21.04)
    DOCKER_IMAGE=ubuntu:21.04 make docker-shell
    
    # 4. Inside the Docker shell, install libedgetpu
    echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list
    curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
    sudo apt-get update
    sudo apt-get install libedgetpu1-std
    
    # 5. Install Python requirements
    cd docs
    sudo apt install python3-pip -y
    python3 -m pip install -r requirements.txt
    
    # If libraries are installed outside PATH, add them (example):
    export PATH=$PATH:/home/yourname/.local/bin
    
    # 6. Build the docs
    bash makedocs.sh -p
  10. Use pycoral.adapters.classify for image classification

    master

    The pycoral.adapters.classify module provides utilities for extracting classification results from a model's output.

    Key functions:

    • get_scores(model, image): Returns the raw scores for each class.
    • get_classes(model, image): Returns the class labels (as Class objects) for the top predictions.
    • get_classes_from_scores(model, scores, threshold): Returns class labels based on a provided list of scores and a minimum threshold.
    • num_classes(model): Returns the total number of classes the model is trained to recognize.
    import pycoral.adapters.classify as classify
    
    # Example usage (conceptual)
    scores = classify.get_scores(model, image)
    classes = classify.get_classes(model, image)
    count = classify.num_classes(model)
  11. Use pycoral.adapters.detect for object detection

    master

    The pycoral.adapters.detect module provides utilities for extracting detected objects from a model's output.

    Key functions:

    • get_objects(model, image): Returns a list of Object instances found in the image.

    Each detected Object contains information about the detection, including its bounding box (BBox). The BBox class provides methods to access the coordinates of the detected area.

    import pycoral.adapters.detect as detect
    
    # Example usage (conceptual)
    objects = detect.get_objects(model, image)
    for obj in objects:
        bbox = obj.bbox
        # Access bbox coordinates...