Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edge 장비에서 cnn, nlp 모델 추론 가능성 test #63

Open
jungae-park opened this issue Feb 16, 2023 · 6 comments · May be fixed by #66
Open

edge 장비에서 cnn, nlp 모델 추론 가능성 test #63

jungae-park opened this issue Feb 16, 2023 · 6 comments · May be fixed by #66
Assignees

Comments

@jungae-park
Copy link
Contributor

jungae-park commented Feb 16, 2023

edge 장비에서 여러 모델 추론 가능성을 확인합니다.

  1. image classification (mobilenetv1, mobilenetv2, inceptionv3 - raw image 1장 추론)
  2. coral-tpu 장비에서 mage classification (edgetpu-tflite 모델 {mobilenetv1, mobilenetv2, inceptionv3} - raw image 1장 추론)
  3. object detection (yolov5 - raw image 1장 추론)
  4. nlp (rnn, lstm, bert, distilbert - test data 하나만 추론)
@jungae-park jungae-park self-assigned this Feb 16, 2023
@jungae-park
Copy link
Contributor Author

jungae-park commented Feb 16, 2023

1번, 2번, 3번 사항에 대해 완료하여 코드 정리 후 PR 진행 예정이며,
4번 nlp 모델 추론의 경우 기존에 imdb 데이터셋의 테스트 데이터를 batch size 마다 추론하는 형태였고, batch size 1 기준으로 추론을 진행할 경우 1시간 이상의 시간이 걸렸습니다.
그래서 이것을 모델은 그대로 사용하고, 데이터셋만 IMDB 사이트에서 영화 어벤져스의 10점 긍정 리뷰와 영화 블랙팬서의 1점 부정 리뷰 문장으로 사용하는 것으로 변경하여 추론하도록 코드를 변경하였고, PR 진행하려고 합니다.

그리고 bert, distilbert 모델의 경우 모델을 로드하는 과정에서 아래와 같이 이슈가 발생하여 해결하고 있습니다.

2023-02-16 16:52:08.934103: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.10.2
Traceback (most recent call last):
  File "rnn-raw.py", line 7, in <module>
    model = tf.keras.models.load_model('./model/distilbert_sst2_model.h5')
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/save.py", line 202, in load_model
    compile)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/hdf5_format.py", line 181, in load_model_from_hdf5
    custom_objects=custom_objects)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/model_config.py", line 59, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/serialization.py", line 163, in deserialize
    printable_module_name='layer')
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py", line 672, in deserialize_keras_object
    list(custom_objects.items())))
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py", line 669, in from_config
    config, custom_objects)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py", line 1279, in reconstruct_from_config
    process_layer(layer_data)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py", line 1261, in process_layer
    layer = deserialize_layer(layer_data, custom_objects=custom_objects)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/serialization.py", line 163, in deserialize
    printable_module_name='layer')
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py", line 654, in deserialize_keras_object
    config, module_objects, custom_objects, printable_module_name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py", line 561, in class_and_config_for_serialized_keras_object
    .format(printable_module_name, class_name))
ValueError: Unknown layer: TFDistilBertModel. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

@kmu-leeky
Copy link
Member

"batch size 1 기준으로 추론을 진행할 경우 1시간 이상의 시간이 걸렸습니다." batch size 1 기준으로 한개를 처리하는데 그정도의 시간이 걸리는건 아니지 않아? 우리가 가정하는 시나리오가 batch size 1 에 처리하는 데이터 개수도 1이니 크게 상관이 없지 않을까 싶은데..

@jungae-park
Copy link
Contributor Author

네 테스트 데이터 모두를 추론할때가 3000초 이상 걸렸던 것이고, 하나만 추론 하는 경우에는 0.12초 정도 걸려서 데이터 셋을 바꿀 필요없이 기존코드에서 데이터 하나만 추론하도록 변경하겠습니다.

@jungae-park
Copy link
Contributor Author

  • rnn, lstm, bert, distrilbert 모델에 맞게 데이터 전처리 방식이 달라 하나씩 확인을 해보았고 rnn, lstm은 imdb 데이터셋을 tf.keras.datasets.imdb.load_data을 통해 다운받아와 사용하고 있으며, 전처리 방식이 간단하여 raw 데이터 1개에 대해 적용이 바로 가능성 확인하여 추론 작업 진행하였는데, 모델에 입력 데이터를 넣는 과정에서 아래와 같은 에러가 발생하였습니다.
ValueError: Input 0 of layer simple_rnn is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (130, 32)

모델에 맞게 전처리가 잘 된 것인지 다시 확인해보고있습니다.

  • bert의 경우 같은 imdb 데이터셋을 사용하지만 pkl(피클) 형태로 만들어 사용하고 모델에 맞는 전처리 방식이 달라 확인이 좀 더 필요할 것 같아 확인해보고 있습니다.
  • 그리고 distrilbert의 경우는 sst2 데이터셋을 사용하고 이것도 모델에 맞는 전처리 방식이 달라 추가적인 확인이 필요합니다.

@jungae-park
Copy link
Contributor Author

rnn, lstm 모델에서 하나의 imdb test dataset 추론을 하도록 구성하였습니다.
https://github.com/ddps-lab/edge-inference/blob/nlp/NLP/nlp_raw_inference.py

기존 처음에는 batch 단위로 데이터를 가져오는 전처리 방법에서 batch 단위 없이 하나의 데이터를 가져와서 모델에 맞게 tensor 형식으로 변환하여 전처리하는 과정에서 위의 댓글의 에러 이슈가 있었습니다.

그래서 batch 1로 데이터를 가져오면서 하나의 데이터만 사용할 수 있도록 다시 확인하여 추론이 가능하도록 하였습니다.

# rnn
accuracy 1.0
load_model_time 1.9536924362182617
load_dataset_time 0.02190232276916504
total_inference_time 0.861821174621582
raw_inference_time 0.860095739364624
ips 0.3524333490043051
ips(inf) 1.1603335232962813

# lstm
accuracy 1.0
load_model_time 2.121065855026245
load_dataset_time 0.019814729690551758
total_inference_time 1.2942748069763184
raw_inference_time 1.292433500289917
ips 0.29110764608151285
ips(inf) 0.7726334427664535

@kmu-leeky
Copy link
Member

어제 이야기한 내용과 잘 매치가 되지 않는것 같아. 해결은 어떻게 한거야? 지금 봐서는 https://github.com/ddps-lab/edge-inference/blob/nlp/NLP/nlp_raw_inference.py#L10 여기에 있는 load_test_batch 를 사용하지 않다가, 해당 함수의 인자를 1로 설정해서 데이터를 만들면서 추론이 진행되는건가?

뒤쪽의 정확도 측정하는 모듈을 봐서는 어떤 걸 예측하고, 정확도는 어떻게 측정하는지 잘 감이 안오는데 이건 따로 이야기해주면 좋겠다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants