블로그 이미지
devtang
Instagram : @taebr0 devtaehyeong@gmail.com

calendar

1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Notice

2020. 2. 25. 20:54 AI/TensorFlow Lite

*참고한 사이트는 게시글 하단에 남겨놓겠습니다.

 

 

Tensor Processing Unit

구글에서 Coral Edge TPU 라는 제품을 냈습니다.

 

Google Coral USB Accelerator

구글 코랄 USB 액셀레이터 / 컴퓨터에 꽂아 Google Edge TPU 보조프로세서로 기능하는 제품 / USB 3.0 C타입 / Google 클라우드와 호환 / 로컬 인공지능

www.devicemart.co.kr

 

USB 형식으로 컴퓨터에 꽂아 뉴럴 네트워크의 연산속도를 가속화 해주는 제품입니다.

컴퓨터 제품에 간단하게 꽂아서 쓸수 있어서 좋은점도 있지만, USB형식이라 라즈베리파이와 같은

싱글보드 컴퓨터에 호환이되는것이 흥미롭습니다.

 

되게 아담합니다. ( 소프트 점보 지우개 수준 )

Tensorflow Lite 버전밖에 지원이 되지 않지만 그래도 라즈베리파이에서 실시간 사물 인식을

꽤 높은 프레임으로 실행할 수 있어서 괜찮은 제품인 것 같습니다.

라즈베리파이에 USB를 꽂으면 기계에서 LED가 켜집니다.

 

라즈베리파이에 이용하려면 아래 3가지가 설치 되어있어야합니다.

(라즈베리파이가 지원되지만 Coral 공식 홈페이지에서는 3B+ 모델과 4 모델에만 테스트 했다고 합니다.)

 

1. Python 3.5 이상 설치

2. Edge TPU Runtime 설치

3. TensorFlow Lite 설치

 

1. Python 3.5 이상 설치

라즈베리파이 스트레치 버전(제 기준)에는 파이썬이 2.7.x 버전대로 설치되어 있으므로 아래와 같은 명령어로 파이썬을 설치해줍니다. 

sudo apt-get update
sudo apt-get install python3

 

2. Edge TPU Runtime 설치

coral에서 제공하는 클라우드 패키지를 라즈베리에 저장합니다.

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-get update

이제 TPU runtime을 설치합니다. 

표준 클럭으로 설치하려면 아래의 명령어를 입력하세요.

sudo apt-get install libedgetpu1-std

클럭을 최대로 높이고 설치하려면 아래의 명령어를 입력하세요. (라즈베리파이에 쿨러가 없다면 왠만하면 비추입니다.)

sudo apt-get install libedgetpu1-max

설치가  완료되면 Coral USB Accelerator를 라즈베리파이에 꼽고 Tensorflow Lite 설치를 합니다.

 

3. TensorFlow Lite 설치

 

라즈베리파이3 파이썬3.7.x 버전 이므로 아래와 같은 명령어를 입력하여 다운로드 합니다.

wget https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl

다운로드 받은다음 pip 명령어로 설치합니다.

pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl

 

다른 버전의 TensorFlow Lite를 원하시면 아래 사이트를 참조하세요.

https://www.tensorflow.org/lite/guide/python

 

Python quickstart  |  TensorFlow Lite

Using TensorFlow Lite with Python is great for embedded devices based on Linux, such as Raspberry Pi and Coral devices with Edge TPU, among many others. This page shows how you can start running TensorFlow Lite models with Python in just a few minutes. All

www.tensorflow.org

 

이제 모든 설치가 완료되었으면, 정상적으로 구동이 되는지 확인해봅시다!

 

coral이라는 폴더를 생성하고 github에서 예제 코드를 받아옵니다.

mkdir coral &&  cd coral git clone https://github.com/google-coral/tflite.git

새(bird)를 분류하는 텐서플로우 기본 예제를 다운 받아옵니다.

cd tflite / python / examples / classification bash install_requirements.sh

parrot.jpg

파이썬으로 예제 그림을 추론할 수 있는지 돌려봅니다.

python3 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

아래와 같이 나오면 TensorFlow Lite 가 정상적으로 설치됬습니다.

INFO: Initialized TensorFlow Lite runtime.
----INFERENCE TIME----
Note: The first inference on Edge TPU is slow because it includes loading the model into Edge TPU memory.
15.3ms
3.0ms
2.8ms
2.9ms
2.9ms
-------RESULTS--------
Ara macao (Scarlet Macaw): 0.76562

Ara macao가 무엇인고 찾아봤더니

금강앵무새라고 하네요. 사진보니 위의 예제 사진과 동일한 새입니다.

 

올바르게 설치된 것 같으니 이제는 사물인식 코드를 이용해서 실시간 웹캠으로 사물인식을 해보겠습니다.

 

https://github.com/google-coral/edgetpu/blob/master/examples/object_detection.py

 

google-coral/edgetpu

Source code for the Edge TPU API library. Contribute to google-coral/edgetpu development by creating an account on GitHub.

github.com

 

Lite버전임에도 불구하고 은근 정확도가 높습니다.

 

0  person
1  bicycle
2  car
3  motorcycle
4  airplane
5  bus
6  train
7  truck
8  boat
9  traffic light
10  fire hydrant
12  stop sign
13  parking meter
14  bench
15  bird
16  cat
17  dog
18  horse
19  sheep
20  cow
21  elephant
22  bear
23  zebra
24  giraffe
26  backpack
27  umbrella
30  handbag
31  tie
32  suitcase
33  frisbee
34  skis
35  snowboard
36  sports ball
37  kite
38  baseball bat
39  baseball glove
40  skateboard
41  surfboard
42  tennis racket
43  bottle
45  wine glass
46  cup
47  fork
48  knife
49  spoon
50  bowl
51  banana
52  apple
53  sandwich
54  orange
55  broccoli
56  carrot
57  hot dog
58  pizza
59  donut
60  cake
61  chair
62  couch
63  potted plant
64  bed
66  dining table
69  toilet
71  tv
72  laptop
73  mouse
74  remote
75  keyboard
76  cell phone
77  microwave
78  oven
79  toaster
80  sink
81  refrigerator
83  book
84  clock
85  vase
86  scissors
87  teddy bear
88  hair drier
89  toothbrush

89개의 객체가 학습되어있습니다. 가지고 놀다보면 꽤 재밌어요 ㅎㅎ

아래는 실시간 웹캠 영상입니다.

TensorFlow Lite를 이용한 사물 인식 영상으로 마무리 짓겠습니다.

 

서투른 글 읽어주셔서 감사합니다.

 

참고사이트

https://coral.ai/docs/edgetpu/multiple-edgetpu/

https://coral.ai/docs/accelerator/get-started#2-install-the-tensorflow-lite-library

https://www.tensorflow.org/lite/guide/python

 

Python quickstart  |  TensorFlow Lite

Using TensorFlow Lite with Python is great for embedded devices based on Linux, such as Raspberry Pi and Coral devices with Edge TPU, among many others. This page shows how you can start running TensorFlow Lite models with Python in just a few minutes. All

www.tensorflow.org

 

Get started with the USB Accelerator | Coral

Learn how to set up the Coral USB Accelerator and run some demo code.

coral.ai

 

Run multiple models with multiple Edge TPUs | Coral

 

coral.ai

 

posted by devtang