Python tflite_runtime.interpreter.load_delegate() Examples

The following are 13 code examples for showing how to use tflite_runtime.interpreter.load_delegate(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module tflite_runtime.interpreter , or try the search function .

Example 1
Project: frigate   Author: blakeblackshear   File: edgetpu.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self):
        edge_tpu_delegate = None
        try:
            edge_tpu_delegate = load_delegate('libedgetpu.so.1.0')
        except ValueError:
            print("No EdgeTPU detected. Falling back to CPU.")
        
        if edge_tpu_delegate is None:
            self.interpreter = tflite.Interpreter(
                model_path='/cpu_model.tflite')
        else:
            self.interpreter = tflite.Interpreter(
                model_path='/edgetpu_model.tflite',
                experimental_delegates=[edge_tpu_delegate])
        
        self.interpreter.allocate_tensors()

        self.tensor_input_details = self.interpreter.get_input_details()
        self.tensor_output_details = self.interpreter.get_output_details() 
Example 2
Project: ambianic-edge   Author: ambianic   File: inference.py    License: Apache License 2.0 6 votes vote down vote up
def _get_edgetpu_interpreter(model=None):  # pragma: no cover
    # Note: Looking for ideas how to test Coral EdgeTPU dependent code
    # in a cloud CI environment such as Travis CI and Github
    tf_interpreter = None
    if model:
        try:
            edgetpu_delegate = load_delegate('libedgetpu.so.1.0')
            assert edgetpu_delegate
            tf_interpreter = Interpreter(
                model_path=model,
                experimental_delegates=[edgetpu_delegate]
                )
            log.debug('EdgeTPU available. Will use EdgeTPU model.')
        except Exception as e:
            log.debug('EdgeTPU init error: %r', e)
            # log.debug(stacktrace())
    return tf_interpreter 
Example 3
Project: SpectralMachine   Author: feranick   File: libSpectraKeras.py    License: GNU General Public License v3.0 5 votes vote down vote up
def loadModel(dP):
    if dP.TFliteRuntime:
        import tflite_runtime.interpreter as tflite
        # model here is intended as interpreter
        if dP.runCoralEdge:
            print(" Running on Coral Edge TPU")
            try:
                model = tflite.Interpreter(model_path=os.path.splitext(dP.model_name)[0]+'_edgetpu.tflite',
                    experimental_delegates=[tflite.load_delegate(dP.edgeTPUSharedLib,{})])
            except:
                print(" Coral Edge TPU not found. Please make sure it's connected. ")
        else:
            model = tflite.Interpreter(model_path=os.path.splitext(dP.model_name)[0]+'.tflite')
        model.allocate_tensors()
    else:
        getTFVersion(dP)
        import tensorflow as tf
        if dP.useTFlitePred:
            # model here is intended as interpreter
            model = tf.lite.Interpreter(model_path=os.path.splitext(dP.model_name)[0]+'.tflite')
            model.allocate_tensors()
        else:
            model = tf.keras.models.load_model(dP.model_name)
    return model

#************************************
# Make prediction based on framework
#************************************ 
Example 4
Project: smart-zoneminder   Author: goruck   File: detect_servers_tpu.py    License: MIT License 5 votes vote down vote up
def __init__(self):
        # Load TFLite model and allocate tensors.
        self.interpreter = tflite.Interpreter(model_path=PERSON_CLASS_MODEL,
            experimental_delegates=[tflite.load_delegate('libedgetpu.so.1')])
        self.interpreter.allocate_tensors()

        # Get input and output tensors.
        self.input_details = self.interpreter.get_input_details()
        self.output_details = self.interpreter.get_output_details() 
Example 5
Project: examples-camera   Author: google-coral   File: common.py    License: Apache License 2.0 5 votes vote down vote up
def make_interpreter(model_file):
    model_file, *device = model_file.split('@')
    return tflite.Interpreter(
      model_path=model_file,
      experimental_delegates=[
          tflite.load_delegate(EDGETPU_SHARED_LIB,
                               {'device': device[0]} if device else {})
      ]) 
Example 6
Project: examples-camera   Author: google-coral   File: common.py    License: Apache License 2.0 5 votes vote down vote up
def make_interpreter(model_file):
    model_file, *device = model_file.split('@')
    return tflite.Interpreter(
      model_path=model_file,
      experimental_delegates=[
          tflite.load_delegate(EDGETPU_SHARED_LIB,
                               {'device': device[0]} if device else {})
      ]) 
Example 7
Project: examples-camera   Author: google-coral   File: common.py    License: Apache License 2.0 5 votes vote down vote up
def make_interpreter(model_file):
    model_file, *device = model_file.split('@')
    return tflite.Interpreter(
      model_path=model_file,
      experimental_delegates=[
          tflite.load_delegate(EDGETPU_SHARED_LIB,
                               {'device': device[0]} if device else {})
      ]) 
Example 8
Project: examples-camera   Author: google-coral   File: common.py    License: Apache License 2.0 5 votes vote down vote up
def make_interpreter(model_file):
    model_file, *device = model_file.split('@')
    return tflite.Interpreter(
      model_path=model_file,
      experimental_delegates=[
          tflite.load_delegate(EDGETPU_SHARED_LIB,
                               {'device': device[0]} if device else {})
      ]) 
Example 9
Project: tflite   Author: google-coral   File: detect_image.py    License: Apache License 2.0 5 votes vote down vote up
def make_interpreter(model_file):
  model_file, *device = model_file.split('@')
  return tflite.Interpreter(
      model_path=model_file,
      experimental_delegates=[
          tflite.load_delegate(EDGETPU_SHARED_LIB,
                               {'device': device[0]} if device else {})
      ]) 
Example 10
Project: tflite   Author: google-coral   File: classify_image.py    License: Apache License 2.0 5 votes vote down vote up
def make_interpreter(model_file):
  model_file, *device = model_file.split('@')
  return tflite.Interpreter(
      model_path=model_file,
      experimental_delegates=[
          tflite.load_delegate(EDGETPU_SHARED_LIB,
                               {'device': device[0]} if device else {})
      ]) 
Example 11
Project: project-keyword-spotter   Author: google-coral   File: model.py    License: Apache License 2.0 5 votes vote down vote up
def make_interpreter(model_file):
    model_file, *device = model_file.split('@')
    return tflite.Interpreter(
      model_path=model_file,
      experimental_delegates=[
          tflite.load_delegate(EDGETPU_SHARED_LIB,
                               {'device': device[0]} if device else {})
      ]) 
Example 12
Project: automl-video-ondevice   Author: google   File: tflite_object_detection.py    License: Apache License 2.0 4 votes vote down vote up
def _load_tflite(self, tflite_path):
    experimental_delegates = []
    try:
      experimental_delegates.append(
          tflite.load_delegate(
              EDGETPU_SHARED_LIB,
              {'device': self._config.device} if self._config.device else {}))
    except AttributeError as e:
      if '\'Delegate\' object has no attribute \'_library\'' in str(e):
        print(
            'Warning: EdgeTPU library not found. You can still run CPU models, '
            'but if you have a Coral device make sure you set it up: '
            'https://coral.ai/docs/setup/.')
    except ValueError as e:
      if 'Failed to load delegate from ' in str(e):
        print(
            'Warning: EdgeTPU library not found. You can still run CPU models, '
            'but if you have a Coral device make sure you set it up: '
            'https://coral.ai/docs/setup/.')

    try:
      self._interpreter = tflite.Interpreter(
          model_path=tflite_path, experimental_delegates=experimental_delegates)
    except TypeError as e:
      if 'got an unexpected keyword argument \'experimental_delegates\'' in str(
          e):
        self._interpreter = tflite.Interpreter(model_path=tflite_path)
    try:
      self._interpreter.allocate_tensors()
    except RuntimeError as e:
      if 'edgetpu-custom-op' in str(e) or 'EdgeTpuDelegateForCustomOp' in str(
          e):
        raise RuntimeError('Loaded an EdgeTPU model without the EdgeTPU '
                           'library loaded. If you have a Coral device make '
                           'sure you set it up: https://coral.ai/docs/setup/.')
      else:
        raise e
    self._is_lstm = self._check_lstm()
    if self._is_lstm:
      print('Loading an LSTM model.')
      self._lstm_c = np.copy(self.input_tensor(1))
      self._lstm_h = np.copy(self.input_tensor(2)) 
Example 13
Project: smart-zoneminder   Author: goruck   File: evaluate_model.py    License: MIT License 4 votes vote down vote up
def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('--model',
        type=str,
        default=None,
        help='tflite model to evaluate')
    ap.add_argument('--dataset',
        default='/mnt/dataset/',
        help='location of evaluation dataset')
    args = vars(ap.parse_args())

    logging.basicConfig(format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
        level=logging.DEBUG)

    # Let model on command line override default from config.
    if args['model'] is not None:
        model = args['model']
    else:
        model = DEFAULT_MODEL

    logger.info('Evaluating tflite model: {} on dataset: {}'
        .format(model, args['dataset']))

    # Grab test images paths.
    imagePaths = glob(args['dataset'] + '/**/*.*', recursive=True)

    # Create a test image generator comprehension.
    # Generates (image path, image label) tuples.
    test_gen = ((path.abspath(imagePath), imagePath.split(path.sep)[-2])
        for imagePath in imagePaths)

    # Start the tflite interpreter on the tpu and allocate tensors.
    interpreter = tflite.Interpreter(model_path=model,
        experimental_delegates=[tflite.load_delegate('libedgetpu.so.1')])
    interpreter.allocate_tensors()

    logger.info(interpreter.get_input_details())
    logger.info(interpreter.get_output_details())

    # Compute accuracy on the test image set.
    accuracy, inference_time = evaluate_model(interpreter=interpreter,
        test_gen=test_gen)

    num_images = len(imagePaths)

    logger.info('accuracy: {:.4f}, num test images: {}, inferences / sec: {:.4f}'
        .format(accuracy, num_images, num_images / inference_time))