6月3日のブログ、Google Edge TPUの offline compilerから約2か月
July 2019 Updates
・New on-device transfer learning APIs ・post-training quant support ・new TF Lite delegate
この中で、new TF Lite delegate が気になったので。。。
TensorFlow Liteの例題、load_image.pyの
interpreter = Interpreter(model_path=args.model_file) を interpreter = Interpreter(model_path=args.model_file, experimental_delegates=[load_delegate('libedgetpu.so.1.0')]) に変更するだけ、experimental_delegatesに、load_delegateにてライブラリを呼び出すだけ。。。 from tensorflow.lite.python.interpreter import load_delegate で、load_delegate を取り込めばいい。
おっと、なんだ、experimental_delegates なんか、無かったんだけどね。
interpreter.pyを見たら、追加されていたわ。。。
def __init__(self, model_path=None, model_content=None, experimental_delegates=None): """Constructor. Args: model_path: Path to TF-Lite Flatbuffer file. model_content: Content of model. experimental_delegates: Experimental. Subject to change. List of [TfLiteDelegate](https://www.tensorflow.org/lite/performance/delegates) objects returned by lite.load_delegate().
@_tf_export('lite.experimental.load_delegate') def load_delegate(library, options=None): """Returns loaded Delegate object. Args: library: Name of shared library containing the [TfLiteDelegate](https://www.tensorflow.org/lite/performance/delegates). options: Dictionary of options that are required to load the delegate. All keys and values in the dictionary should be convertible to str. Consult the documentation of the specific delegate for required and legal options. (default None) Returns: Delegate object. """ # TODO(b/137299813): Fix darwin support for delegates. if sys.platform == 'darwin': raise RuntimeError('Dynamic loading of delegates on Darwin not supported.') try: delegate = Delegate(library, options) except ValueError as e: raise ValueError('Failed to load delegate from {}\n{}'.format( library, str(e))) return delegate
def __init__(self, library, options=None): """Loads delegate from the shared library. Args: library: Shared library name. options: Dictionary of options that are required to load the delegate. All keys and values in the dictionary should be serializable. Consult the documentation of the specific delegate for required and legal options. (default None) Raises: RuntimeError:
のように定義されていて、library が 共有ライブラリ名、optionsはオプション。デフォルト値は、None。
self._library = ctypes.pydll.LoadLibrary(library)
self._delegate_ptr = self._library.tflite_plugin_create_delegate( options_keys, options_values, len(options), error_capturer_cb)
}}}
にあって、解放は
にあって、解放は
def __del__(self): # __del__ can be called multiple times, so if the delegate is destroyed. # don't try to destroy it twice. if self._library is not None: self._library.tflite_plugin_destroy_delegate.argtypes = [ctypes.c_void_p] self._library.tflite_plugin_destroy_delegate(self._delegate_ptr) self._library = None
にあるように、tflite_plugin_destroy_delegateを呼び出せばいいと。。
があれば、Delegate作れるんだね。