pycoral.utils

pycoral.utils.dataset

Utilities to help process a dataset.

pycoral.utils.dataset.read_label_file(file_path)[source]

Reads labels from a text file and returns it as a dictionary.

This function supports label files with the following formats:

  • Each line contains id and description separated by colon or space. Example: 0:cat or 0 cat.

  • Each line contains a description only. The returned label id’s are based on the row number.

Parameters

file_path (str) – path to the label file.

Returns

Dict of (int, string) which maps label id to description.

pycoral.utils.edgetpu

Utilities for using the TensorFlow Lite Interpreter with Edge TPU.

pycoral.utils.edgetpu.get_runtime_version()

Returns the Edge TPU runtime (libedgetpu.so) version.

This runtime version is dynamically retrieved from the shared object.

Returns

A string for the version name.

pycoral.utils.edgetpu.invoke_with_bytes()

Invoke the given tf.lite.Interpreter with bytes as input.

Parameters
  • interpreter – The tf.lite:Interpreter to invoke.

  • input_data (bytes) – Raw bytes as input data.

pycoral.utils.edgetpu.invoke_with_dmabuffer()

Invoke the given tf.lite.Interpreter using a given Linux dma-buf file descriptor as an input tensor.

Works only for Edge TPU models running on PCIe-based Coral devices. You can verify device support with supports_dmabuf().

Parameters
  • interpreter – The tf.lite:Interpreter to invoke.

  • dma_fd (int) – DMA file descriptor.

  • size (size_t) – DMA buffer size.

pycoral.utils.edgetpu.invoke_with_membuffer()

Invoke the given tf.lite.Interpreter with a pointer to a native memory allocation.

Works only for Edge TPU models running on PCIe TPU devices.

Parameters
  • interpreter – The tf.lite:Interpreter to invoke.

  • buffer (intptr_t) – Pointer to memory buffer with input data.

  • size (size_t) – The buffer size.

pycoral.utils.edgetpu.list_edge_tpus()

Lists all available Edge TPU devices.

Returns

A list of dictionary items, each representing an Edge TPU in the system. Each dictionary includes a “type” (either “usb” or “pci”) and a “path” (the device location in the system). Note: The order of the Edge TPUs in this list are not guaranteed to be consistent across system reboots.

pycoral.utils.edgetpu.load_edgetpu_delegate(options=None)[source]

Loads the Edge TPU delegate with the given options.

Parameters

options (dict) – Options that are passed to the Edge TPU delegate, via tf.lite.load_delegate. The only option you should use is “device”, which defines the Edge TPU to use. Supported values are the same as device in make_interpreter().

Returns

The Edge TPU delegate object.

pycoral.utils.edgetpu.make_interpreter(model_path_or_content, device=None, delegate=None)[source]

Creates a new tf.lite.Interpreter instance using the given model.

Note: If you have multiple Edge TPUs, you should always specify the device argument.

Parameters
  • model_path_or_content (str or bytes) – str object is interpreted as model path, bytes object is interpreted as model content.

  • device (str) –

    The Edge TPU device you want:

    • None – use any Edge TPU (this is the default)

    • ”:<N>” – use N-th Edge TPU (this corresponds to the enumerated index position from list_edge_tpus())

    • ”usb” – use any USB Edge TPU

    • ”usb:<N>” – use N-th USB Edge TPU

    • ”pci” – use any PCIe Edge TPU

    • ”pci:<N>” – use N-th PCIe Edge TPU

    If left as None, you cannot reliably predict which device you’ll get. So if you have multiple Edge TPUs and want to run a specific model on each one, then you must specify the device.

  • delegate – A pre-loaded Edge TPU delegate object, as provided by load_edgetpu_delegate(). If provided, the device argument is ignored.

Returns

New tf.lite.Interpreter instance.

pycoral.utils.edgetpu.run_inference(interpreter, input_data)[source]

Performs interpreter invoke() with a raw input tensor.

Parameters
  • interpreter – The tf.lite.Interpreter to invoke.

  • input_data – A 1-D array as the input tensor. Input data must be uint8 format. Data may be Gst.Buffer or numpy.ndarray.

pycoral.utils.edgetpu.set_verbosity()

Sets the verbosity of operating logs related to each Edge TPU. 10 is the most verbose; 0 is the default.

Parameters

verbosity (int) – Desired verbosity 0-10.

Returns

A boolean indicating if verbosity was succesfully set.

pycoral.utils.edgetpu.supports_dmabuf()

Checks whether the device supports Linux dma-buf.

Parameters

interpreter – The tf.lite:Interpreter that’s bound to the Edge TPU you want to query.

Returns

True if the device supports DMA buffers.

API version 2.0