Google Coral USB Acceleratorのインストールメモ
Ubuntu/RaspberryPiでGoogle Coral USB Acceleratorを使用できるようにしてみる。
基本的にともに同じ手順で大丈夫(なハズ…)
参考サイト: RaspberryPi 4でCoral USB TPU Accelerator(EdgeTPU)をとりあえず使う
下記コマンドを実行して、EdgeTPU用ライブラリ(ドライバ類)をインストールする。
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 update
# 通常版をインストール
sudo apt install libedgetpu1-std
# または、最大クロック版
# sudo apt install libedgetpu1-max
pyenv + virtualenvを使用しているものとして記載する。
pyenv virtualenv 3.7.7 coral
mkdir -p /work/coral
cd /work/coral
pyenv local coral
pip install --upgrade pip setuptools
numpyのinstallで失敗するので、以下を実行しておく。
sudo apt install libatlas-base-dev
pip install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_x86_64.whl
[!NOTE] どれをインストールするかはPython quickstartで確認
========= 例えば、ubuntu/raspbianのときはpythonのバージョンに応じて以下のどれかを指定 ==============
Linux (x86-64) 3.5 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-linux_x86_64.whl 3.6 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_x86_64.whl 3.7 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_x86_64.whl
Linux (ARM 32) 3.5 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp35-cp35m-linux_armv7l.whl 3.6 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_armv7l.whl 3.7 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl
USBポートに Coral USB Accelerator を接続する
認識されたことを確認する
lsusb
以下の表示があったら正常に認識されている。
«省略»
Bus XXX Device YYY: ID 1a6e:089a Global Unichip Corp.
«省略»
Githubからソースをダウンロードする
cd /work/coral/
git** clone https://github.com/google-coral/tflite.git
cd /work/coral/tflite/python/examples/classification/
./install_requirements.sh
python classify_image.py
--model models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \
--labels models/inat_bird_labels.txt \
--input images/parrot.jpg
結果の例は以下。
結果は「Ara macao(コンゴウインコ)」と表示されている。
----INFERENCE TIME----
Note: The first inference on Edge TPU is slow because it includes loading the model into Edge TPU memory.
12.4ms
4.2ms
4.1ms
4.2ms
4.2ms
-------RESULTS--------
Ara macao (Scarlet Macaw): 0.77734
python classify_image.py \
--model models/mobilenet_v2_1.0_224_inat_bird_quant.tflite \
--labels models/inat_bird_labels.txt \
--input images/parrot.jpg
[!NOTE] CPUのみで実行する場合もCoral USB Acceleratorを接続しておかないとエラーになる。
接続しなくても実行できるようにするには以下のパッチを適用し、
実行時に-cpu
オプションを指定する。diff --git a/python/examples/classification/classify_image.py b/python/examples/classification/ > classify_image.py index 75f1d76..44fb798 100644 --- a/python/examples/classification/classify_image.py +++ b/python/examples/classification/classify_image.py @@ -12,7 +12,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -r"""Example using TF Lite to classify a given image using an Edge TPU. +"""Example using TF Lite to classify a given image using an Edge TPU. To run this code, you must attach an Edge TPU attached to the host and install the Edge TPU runtime (`libedgetpu.so`) and `tflite_runtime`. For @@ -64,9 +64,12 @@ def load_labels(path, encoding='utf-8'): return {index: line.strip() for index, line in enumerate(lines)} -def make_interpreter(model_file): +def make_interpreter(model_file, cpu=False): model_file, *device = model_file.split('@') - return tflite.Interpreter( + if cpu : + return tflite.Interpreter(model_path=model_file) + else : + return tflite.Interpreter( model_path=model_file, experimental_delegates=[ tflite.load_delegate(EDGETPU_SHARED_LIB, @@ -92,11 +95,19 @@ def main(): parser.add_argument( '-c', '--count', type=int, default=5, help='Number of times to run inference') + parser.add_argument( + '--cpu', action='store_true', + help='use cpu only(default:use with TPU)') args = parser.parse_args() + if args.cpu : + print('**** USE CPU ONLY!! ****') + else : + print('**** USE WITH TPU ****') + labels = load_labels(args.labels) if args.labels else {} - interpreter = make_interpreter(args.model) + interpreter = make_interpreter(args.model, args.cpu) interpreter.allocate_tensors() size = classify.input_size(interpreter)
cd /work/coral/tflite/python/examples/detection
./install_requirements.sh
python detect_image.py \
--model models/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite \
--input images/grace_hopper.bmp \
--labels models/coco_labels.txt \
--output result.jpg
結果の例は以下。
以下の表示と同時に認識結果の画像が別ウィンドウで表示される。
----INFERENCE TIME----
Note: The first inference is slow because it includes loading the model into Edge TPU memory.
27.90 ms
11.59 ms
11.36 ms
11.89 ms
11.10 ms
-------RESULTS--------
tie
id: 31
score: 0.83984375
bbox: BBox(xmin=226, ymin=417, xmax=290, ymax=539)
person
id: 0
score: 0.83984375
bbox: BBox(xmin=2, ymin=5, xmax=507, ymax=590)
python detect_image.py \
--model models/mobilenet_ssd_v2_coco_quant_postprocess.tflite \
--input images/grace_hopper.bmp \
--labels models/coco_labels.txt \
--output result.jpg
[!NOTE]
--output
オプションを指定すると、結果画像を保存するとともに、表示を行う。
指定しなければ認識だけ行う(結果の画像表示もしない)。
[!NOTE]
--output
オプションによる出力ファイルの形式は指定した拡張子で決定される。jpgやbmpなど。
rawデータで出力した場合(拡張子rgb)は、以下のコマンドでjpgやbmpに変換できる。convert result.rgb result.jpg
または
convert result.rgb result.bmp
[!NOTE] CPUのみで実行する場合でCoral USB Acceleratorを接続していなくてエラーになる場合は
こちらを参照