반응형

 

안녕하세요. 

 

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

 

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
반응형

+ Recent posts