簡體   English   中英

無法使用 Python 在 GPU(Jetson Nano)上運行 tflite model

[英]Cannot run tflite model on GPU (Jetson Nano) using Python

我有一個量化的 tflite model,我想對其進行基准測試以在 Nvidia Jetson Nano 上進行推理。 我使用 tf.lite.Interpreter() 方法進行推理。 該進程似乎沒有在 GPU 上運行,因為 CPU 和 GPU 上的推理時間相同。

有沒有辦法使用 Python 在 GPU 上運行 tflite model?

我試圖通過設置 tf.device() 方法強制使用 GPU,但仍然不起作用。 官方文檔有一個名為 delegates for GPU acceleration 的東西,但我似乎找不到 Python 的任何東西。

with tf.device('/device:GPU:0'):

    interpreter = tf.lite.Interpreter(model_path="model.tflite")

    interpreter.allocate_tensors()

    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    input_shape = input_details[0]['shape']
    input_data = np.array(np.random.random_sample(input_shape), dtype=np.uint8)
    interpreter.set_tensor(input_details[0]['index'], input_data)

    start_time = time.time()

    interpreter.invoke()

    elapsed_time = time.time() - start_time
    print(elapsed_time)

    output_data = interpreter.get_tensor(output_details[0]['index'])

根據此鏈接, TFLite 不支持 Nvidia GPU

這似乎根據可用的傑森一家納米這個最近的線程。 但它看起來像一個自定義構建,試試它而不是 tensorflow lite。

如果您已經安裝了它,如果該版本應該支持 GPU,則可能會要求 nvidia 開發人員。

或者您可以通過這種方式安裝 nvidia 自定義 tensorflow。

Python 3.6+JetPack4.4

sudo apt-get install libhdf5-serial-dev hdf5-tools libhdf5-dev zlib1g-dev zip libjpeg8-dev liblapack-dev libblas-dev gfortran
sudo apt-get install python3-pip
sudo pip3 install -U pip
sudo pip3 install -U pip testresources setuptools numpy==1.16.1 future==0.17.1 mock==3.0.5 h5py==2.9.0 keras_preprocessing==1.0.5 keras_applications==1.0.8 gast==0.2.2 futures protobuf pybind11
# TF-2.x
$ sudo pip3 install --pre --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v44 tensorflow==2.2.0+nv20.8
# TF-1.15
$ sudo pip3 install --pre --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v44 ‘tensorflow<2’

Python 3.6+JetPack4.3

$ sudo apt-get install libhdf5-serial-dev hdf5-tools libhdf5-dev zlib1g-dev zip libjpeg8-dev
$ sudo apt-get install python3-pip
$ sudo pip3 install -U pip
$ sudo pip3 install -U numpy grpcio absl-py py-cpuinfo psutil portpicker six mock requests gast h5py astor termcolor protobuf keras-applications keras-preprocessing wrapt google-pasta
# TF-2.x
$ sudo pip3 install --pre --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v43 tensorflow==2.1.0+nv20.3
# TF-1.15
$ sudo pip3 install --pre --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v43 tensorflow==1.15.2+nv20.3

您的 Jetson Nano 是否支持 OpenCL? 如果是,您可以將 OpenCL 委托與 TFLite 結合使用。 https://www.tensorflow.org/lite/guide/build_cmake#opencl_gpu_delegate

這是因為你是一些如何使用 tf. 看這里

interpreter = tf.lite.Interpreter(model_path="model.tflite").

你能做的就是安裝

python3 -m pip install tflite-runtime

並使用

import tflite_runtime.interpreter as tflite
interpreter = tflite.Interpreter(model_path=args.model_file)

你應該能夠正常運行。

我希望它有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM