ddps-lab / edge-inference

Evaluation of inference model performance on edge devices
Apache License 2.0
2 stars 3 forks source link

CNN 모델 edge TPU (tflite) convert 에러 #8

Closed jungae-park closed 2 years ago

jungae-park commented 2 years ago

image

mobilnet_model = tf.keras.applications.MobileNet(weights='imagenet') mobilnetv2_model = tf.keras.applications.MobileNetV2(weights='imagenet') Inceptionv3_model = tf.keras.applications.InceptionV3(weights='imagenet')

converter = tf.lite.TFLiteConverter.from_keras_model(mobilnet_model) tflite_model = converter.convert()

with open('mobilenet_v1.tflite', 'wb') as f: f.write(tflite_model)

converter = tf.lite.TFLiteConverter.from_keras_model(mobilnetv2_model) tflite_model = converter.convert()

with open('mobilenet_v2.tflite', 'wb') as f: f.write(tflite_model)

converter = tf.lite.TFLiteConverter.from_keras_model(Inceptionv3_model) tflite_model = converter.convert()

with open('inception_v3.tflite', 'wb') as f: f.write(tflite_model)


* tflite 모델 edge tpu model convert 과정

curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list

sudo apt-get update

sudo apt-get install edgetpu-compiler

edgetpu_compiler 명령문을 사용하여 변환

edgetpu_compiler mobilenet_v1.tflite

edgetpu_compiler mobilenet_v2.tflite

edgetpu_compiler inception_v3.tflite

* edge tpu convert 성공시 메시지

Edge TPU Compiler version 16.0.384591198 Started a compilation timeout timer of 180 seconds.

Model compiled successfully in 554 ms.

Input model: inception_v3.tflite Input size: 90.91MiB Output model: inception_v3_edgetpu.tflite Output size: 90.90MiB On-chip memory used for caching model parameters: 0.00B On-chip memory remaining for caching model parameters: 0.00B Off-chip memory used for streaming uncached model parameters: 0.00B Number of Edge TPU subgraphs: 0 Total number of operations: 125 Operation log: inception_v3_edgetpu.log

Model successfully compiled but not all operations are supported by the Edge TPU. A percentage of the model will instead run on the CPU, which is slower. If possible, consider updating your model to use only operations supported by the Edge TPU. For details, visit g.co/coral/model-reqs. Number of operations that will run on Edge TPU: 0 Number of operations that will run on CPU: 125 See the operation log file for individual operation details. Compilation child process completed within timeout period. Compilation succeeded!

* edge tpu convert 실패시 메시지

Edge TPU Compiler version 16.0.384591198 Started a compilation timeout timer of 180 seconds. ERROR: Attempting to use a delegate that only supports static-sized tensors with a graph that has dynamic-sized tensors. Compilation failed: Model failed in Tflite interpreter. Please ensure model can be loaded/run in Tflite interpreter. Compilation child process completed within timeout period. Compilation failed!


convert 실패시 메시지를 아래와 같이 확인 하였을 때 모델 사이즈를 정적으로 지정하지 못해서 생기는 이슈로 판단되어 확인중 (FP32 or FP16)

ERROR: Attempting to use a delegate that only supports static-sized tensors with a graph that has dynamic-sized tensors.

jungae-park commented 2 years ago

error를 확인해보니, 모델 convert시 입력/출력 크기를 지정하고 모델 크기를 지정해서 변환해야하는 것으로 확인하였습니다. 그러나 이슈를 해결하기 위해 참고할만한 자료가 부족하였고, coral 공식 홈페이지에서 mobilnet v1 모델을 제공하는 것을 확인하였습니다. (imagenet dataset 사용, input :224,224,3, TFv2) https://coral.ai/models/all/

굳이 convert를 할 필요 없이, 제공해주는 edge tpu 모델을 사용하는 것이 좋을 것 같습니다. mobilenet v1 이외에도 mobilnet v2, inception v3 등 필요한 모델이 모두 제공되는 것 같습니다.

그래서 image classification model은 공식 홈페이지에서 제공해주는 모델을 사용하는 것이 맞을 것 같고 yolo v5 모델도 제가 convert 한 것이 제대로 잘 한 것인지 확인해보겠습니다. (모델 dtype 확인)

jungae-park commented 2 years ago

추가적으로 model의 dtype을 확인해보니, mobilenet v1,v2, inception v3는 FP32에서 edge tpu 용으로 convert 하기 위해 uint8로 변환하여 공식 홈페이지에서 제공하고 있는것 같고, yolo v5 모델은 FP16에서 edge tpu용 unit8로 변환한 모델임을 확인하였습니다.

따라서 제가 convert 한 yolov5 모델도 공식 홈페이지에서 제공해주는 image classification 모델과 같이 사용해도 괜찮을 것 같습니다.

kmu-leeky commented 2 years ago

아 정애야 잘찾았다 ㅎㅎ 변환된게 제공이 되고 있구나 ㅎㅎ 그것들을 사용하면 좋겠다.

마지막 코멘트에 있는 "mobilenet v1,v2, inception v3는 FP32에서 edge tpu 용으로 convert 하기 위해 uint8로 변환하여 공식 홈페이지에서 제공하고 있는것 같고" 여기서 공식 홈페이지가 위에서 언급한 https://coral.ai/models/all/ 사이트인거야? 해당 사이트에서는 그냥 단순히 비트수만 변화된게 아니라 edgetpu_compiler 까지 진행될걸 공유해주는거지?

"yolo v5 모델은 FP16에서 edge tpu용 unit8로 변환한 모델임을 확인하였습니다." 이 내용은 어디에서 제공이 되는거야?

그나저나 세부 문제별로 이슈를 정리하니 follow 하기가 좋다!

jungae-park commented 2 years ago

네 맞습니다. https://coral.ai/models/all/ 에서 제공하는 모델이 edge tpu의 model dtype에 맞게 변환하는 것을 확인하였고, yolo v5는 기존에 제가 참고하던 github(https://github.com/ultralytics/yolov5/blob/master/export.py의 edgetpu부분) 에서 edge tpu로 convert 하는 것을 참고하였는데 이것도 edge tpu model의 dtype으로 변환할 수 있게 제공하는 것을 확인하여 변환된 모델을 사용하는 것이 좋을 것 같습니다.

kmu-leeky commented 2 years ago

오케이. 정애야 조금 만 더 디테일하게 확인을 하면. 이전에 코멘트를 보면 "제공하는 모델이 edge tpu의 model dtype에 맞게 변환하는 것을 확인하였고" 제공하는 모델이 ~~ 변환하는 것을 이라는게 말이 안맞지.. 아마도 제공하는 모델은 ~~ 변환되어 있는것을 이라고 표현을 하려고 했던것 같은데. 혹시나 싶어서 확인하면,

내가 이해하기로는 tpu 의 구동을 위해서 일반 모델을 uint8 로 변환 하고 그걸 edgetpu_compiler 로 한번 더 변환해야 하는 것으로 알고 있어. 그렇다면 yolo5 를 제외한 다른 모델의 경우 edgetpu_compiler 를 사용해서 컴파일 하지 않아도 바로 사용가능한 상태오 coral 페이지에서 제공해주는거지?

다음으로 yolo5 의 경우 "edge tpu model의 dtype으로 변환할 수 있게 제공하는 것을 확인하여 변환된 모델을 사용하는 것이 좋을 것 같습니다." 이건 어디에서 제공이 되는거야? 이전에 이야기했던 coral 웹페이지에서는 해당 내용을 확인할 수가 없어서.

jungae-park commented 2 years ago

네 교수님. edge tpu 환경에서 사용하는 모델은 tflite convert - edgetpu_compiler convert (uint8) 되어진 모델을 사용하는 것이 맞으며, coral 홈페이지 download 탭의 Edge TPU model ( https://raw.githubusercontent.com/google-coral/test_data/master/tf2_mobilenet_v1_1.0_224_ptq_edgetpu.tflite )로 제공해주고 있습니다.

yolov5 모델의 경우도 위에서 제가 전달드린 https://github.com/ultralytics/yolov5/blob/master/export.py 이 코드의 edgetpu 부분을 보면 tflite로 convert - edgetpu_compiler로 convert 되어 모델을 사용할 수 있도록 하였습니다. 그래서 coral에서 제공하고 있는 edge tpu용 모델과 같은 dtype으로 convert 된 것을 확인하여 사용이 가능할 것 같습니다.

jungae-park commented 2 years ago

이슈는 해결하여 close하고, convert 된 모델을 repo에 업로드 하겠습니다.

kmu-leeky commented 2 years ago

정애야. 이슈를 먼저 close 하지는 말고 pr 과 이슈를 연계시키면 pr 을 통해서 main 으로 Merge 가 되면 자동으로 issue 를 close 하게 할수 있어. 다음에는 그렇게 진행을 해보자. 참조 : https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue

jungae-park commented 2 years ago

넵 참고해서 다음부터 진행해보겠습니다.