반응형

 

안녕하세요. 

 

만약, 처음 이 글을 보신다면 아래 글을 먼저 읽어보시는 것을 추천드립니다.

 

1편 : Coral Board 대신 라즈베리파이를 사용하는 이유 : https://coding-yoon.tistory.com/85?category=866905

2편 : USB Accelerator(Edge TPU) 환경설정 : https://coding-yoon.tistory.com/87?category=866905

3편 : Coral Edge TPU 예제 돌리기 : https://coding-yoon.tistory.com/88

 

 

이 그림을 이해하기만 하면 끝!

Edge TPU는 CNN과 같은 Deep feed-forward 신경망을 실행할 수 있습니다.

 

8비트 Edge TPU 용으로 컴파일 된 TensorFlow Lite 모델만 지원하며, 32비트 TensorFlow는 지원하지 않습니다. 

 

Tensor Lite로 직접 모델을 학습시킬 수 없고, Transfer Learning(전이 학습)을 통해 모델을 향샹시킬 수 있습니다.

 

TensorFlow(.pb)나 Keras(.h5)(32bit)를  통해 학습된 모델을 TensorFlow Lite Converter를 사용하여 TensorFlow 파일에서 TensorFlow Lite (.tflite)로 모델을 변환해야합니다. 

 

 

<참고> 

저는 Coral로 개발을 할 때 개인적으로 Keras를 추천드리고 싶습니다. 

 

Edge TPU가 모든 연산을 하는 것은 아닙니다. 공식 홈페이지에서 Edge TPU가 지원하는 연산들을 소개합니다. 

 

모바일넷과 같은 경량 모델에서 사용하는 연산들을 지원하며, 예제들 또한 Keras모델을 불러와 학습시켰습니다.

 

예를 들면 Depthwise Seperable Convolution을 찾을 수 있고, 경량 모델 : MobileNet,  Xception 등을 볼 수 있습니다.

https://coding-yoon.tistory.com/77?category=825914

 

[딥러닝] Depthwise Separable Covolution with Pytorch( feat. Convolution parameters VS Depthwise Separable Covolution paramet

안녕하세요. Google Coral에서 학습된 모델을 통해 추론을 할 수 있는 Coral Board & USB Accelator 가 있습니다. 저는 Coral Board를 사용하지 않고, 라즈베리파이4에 USB Accelator를 연결하여 사용할 생각입니..

coding-yoon.tistory.com

Coral에서 만든 예제들

 

 

우선, TensorFlow나 Keras로 학습된 모델을 저장해야 합니다. 

 

TensorFlow로 학습했다면, (.pb)파일,

Keras로 학습했다면 (.h5)파일

 

 

학습된 모델(32bit)을 TensorFlow Lite Converter(8bit)로 변환해야합니다.

 

변환한 모델을 저장합니다.

 

이제 모델이 준비가 끝났습니다. 최소한의 코드로 작성했습니다.

불러오기

Operation name

Runtime version*

Known limitations

Add

All

 

AveragePool2d

All

No fused activation function.

Concatenation

All

No fused activation function.
If any input is a compile-time constant tensor, there must be only 2 inputs, and this constant tensor must be all zeros (effectively, a zero-padding op).

Conv2d

All

Must use the same dilation in x and y dimensions.

DepthwiseConv2d

12

Dilated conv kernels are not supported.

13

Must use the same dilation in x and y dimensions.

ExpandDims

13

 

FullyConnected

All

Only the default format is supported for fully-connected weights. Output tensor is one-dimensional.

L2Normalization

All

 

Logistic

All

 

Maximum

All

 

MaxPool2d

All

No fused activation function.

Mean

12

No reduction in batch dimension. Supports reduction along x- and/or y-dimensions only.

13

No reduction in batch dimension. If a z-reduction, the z-dimension must be multiple of 4.

 

지원하는 연산입니다. 지원하는 연산을 더 보고 싶으시면 아래 주소를 달아놓을테니 보시면 좋을 것 같습니다.

 

 

파란색 Input_data가 추론하고자 하는 실제 데이터입니다. 

 

input쪽이 코드가 길어져 input_tensor로 따로 함수화를 시켰습니다.

Coral 공식 GitHub예제를 따라했습니다.

 

output쪽은 그렇게 길지 않기 때문에 tensorflow lite 공식 홈페이지를 따라했습니다.

 

추론을 시작하는 코드는 Interpreter.invoke()입니다.

 

추론을 완료하면 output_data에 추론 값이 담겨 있습니다.

 

 

다음 글은 간단한 모델을 하나 불러와 실제로 추론을 한 번 해보겠습니다. 

 

https://coral.ai/docs/edgetpu/models-intro/#compatibility-overview

https://www.tensorflow.org/tutorials/keras/save_and_load?hl=ko

https://www.tensorflow.org/lite/convert/python_api

https://coral.ai/docs/edgetpu/tflite-python/#load-tensorflow-lite-and-run-an-inference

https://www.tensorflow.org/api_docs/python/tf/lite/Interpreter

728x90
반응형
반응형

안녕하세요. 

 

한 달만에 USB Accelator 에 대해 드디어 글을 작성합니다. 

 

https://www.devicemart.co.kr/goods/view?no=12379075

 

Google Coral USB Accelerator

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

www.devicemart.co.kr

99,000원에 디바이스마트에서 구매할 수 있습니다. 

오... 
개봉!
장착
Coral-USB-Accelerator-datasheet.pdf
1.46MB

자세한 데이터 시트를 자세히 보고 싶으시면 위 pdf를 참고하시길 바랍니다. 

 

간단하게 요약하면

 

제가 공부자료로 만들어 놓은 PPT의 일부분입니다. 

 

C타입 포트 TPU 이며, USB 3.0 Port(파란색), Python은 3.5, 3.6. 3.7만 지원합니다. 

(Python 3.8은 지원하지 않습니다.)

 

바로 시작해보겠습니다.

 

 

 

USB Accelator(Edge TPU)를 사용하기 위해서는 The Edge TPU runtime을 설치해야합니다. 

 

(윈도우로 따지면 드라이버같은 거라고 생각하시면 편할 것 같습니다. )

 

라즈비안이나 우분투는 Linux 계통이기 때문에 Linux 단계로 따라가겠습니다.

(Mac이나 Window에서 원하시면 옆 주소를 확인하시기 바랍니다. https://coral.ai/docs/accelerator/get-started/

 

1. Repository에 추가 

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

 

2. Edge TPU 설치

sudo apt-get install libedgetpu1-std

 

3. 옵션(속도 최대화 : 대신 기기가 뜨거워질 수 있으니 주의)

sudo apt-get install libedgetpu1-max

USB Accelator(Edge TPU)는 직접 모델을 학습시킬 수 없습니다. Inference용입니다. 

 

대신, TensorFlow나 Keras로 학습된 모델(32bit)TensorFlow lite(8bit)로 변환 후 .tflite로 변경시키고 좀 더 성능을 향샹시키려면 Transfer Learning(전이학습)을 하면 됩니다. 

 

오늘은 인공지능을 할 수 있는 셋팅까지만 설명하겠습니다. 

 

이제 TensorFlow Lite 인터프리터를 설치하겠습니다.

 

 

 

 

 

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

 

Python 빠른 시작  |  TensorFlow Lite

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trade

www.tensorflow.org

위를 참고하시기 바랍니다.

 

참고로 라즈베리파이는 ARM Architecture 32bit 입니다. 

 

자신의 파이썬 버젼에 맞게 깔아주시면 됩니다. (python3 -V) 

 

pip3 install <해당 URL 복사/붙여넣기>
플랫폼 파이썬 URL
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
3.8 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp38-cp38-linux_armv7l.whl

 

이렇게 라즈베리파이에서 인공지능 추론을 할 수 있는 환경을 다 갖췄습니다.

 

다음 글은 간단한 예제 하나를 돌려보도록 하겠습니다.

728x90
반응형

+ Recent posts