Tensorflow.js (.json) 모델에서 Tensorflow (SavedModel) 또는 Tensorflow Lite (.tflite) 모델로 변환하는 방법은 무엇입니까?

벤 버터 워스

나는 한 다운로드 그것의, 그래서 구글에서 Tensorflow.js (tfjs)에 대한 사전 교육을받은 PoseNet 모델을 json으로 파일.

하지만 Android에서 사용하고 싶기 때문에 .tflite모델이 필요합니다 . 누군가가 tflite하는 tfjs에서 비슷한 모델을 '포팅'하고 있지만 여기에 , 나는 그들이 변환 (PoseNet의 많은 변종이 있습니다) 어떤 모델 모른다. 나는 단계를 직접하고 싶다. 또한 누군가가 stackOverflow의 파일에 업로드 한 임의의 코드를 실행하고 싶지 않습니다.

주의 : 신뢰할 수없는 코드에주의하십시오. TensorFlow 모델은 코드입니다. 자세한 내용은 안전하게 TensorFlow 사용을 참조하세요. Tensorflow 문서

이 작업을 수행하는 편리한 방법을 아는 사람이 있습니까?

벤 버터 워스

json 파일을 보면 어떤 tfjs 형식이 있는지 확인할 수 있습니다. 종종 "그래프 모델"이라고 말합니다. 그들 사이의 차이점은 여기에 있습니다 .

tfjs 그래프 모델에서 저장된 모델로 (더 일반적)

사용 tfjs - 투 - TF 에 의해 패트릭 레빈 .

import tfjs_graph_converter.api as tfjs
tfjs.graph_model_to_saved_model(
               "savedmodel/posenet/mobilenet/float/050/model-stride16.json",
               "realsavedmodel"
            )

# Code below taken from https://www.tensorflow.org/lite/convert/python_api
converter = tf.lite.TFLiteConverter.from_saved_model("realsavedmodel")
tflite_model = converter.convert()

# Save the TF Lite model.
with tf.io.gfile.GFile('model.tflite', 'wb') as f:
  f.write(tflite_model)

tfjs 레이어 모델에서 저장된 모델로

참고 : 이것은 질문에서와 같이 그래프 모델 형식이 아닌 레이어 모델 형식에서만 작동합니다. 나는 그들 사이의 차이점을 여기에 썼다 .


  1. tensorflowjs-convert를 설치 하고 사용하여 .json파일을 Keras HDF5 파일 (다른 SO 스레드에서 ) 로 변환합니다 .

Mac에서는 pyenv ( fix ) 실행시 문제가 발생 하고 Z-shell에서는 pyenv가 올바르게로드되지 않습니다 ( fix ). 또한 pyenv가 실행되면 pyenv가 나를 위해 pip에서 사용하는 파이썬을 변경하지 않았기 때문에 python -m pip install tensorflowjs대신 pip install tensorflowjs사용하십시오.

tensorflowjs_converter 가이드를 따랐 으면 실행 tensorflowjs_converter하여 오류없이 작동하는지 확인하고 Missing input_path argument. 그때:

tensorflowjs_converter --input_format=tfjs_layers_model --output_format=keras tfjs_model.json hdf5_keras_model.hdf5
  1. Keras HDF5 파일을 저장된 모델 (표준 Tensorflow 모델 파일)로 변환하거나 TFLiteConverter를.tflite 사용하여 파일 로 직접 변환 합니다 . 다음은 Python 파일에서 실행됩니다.
# Convert the model.
model = tf.keras.models.load_model('hdf5_keras_model.hdf5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert() 
    
# Save the TF Lite model.
with tf.io.gfile.GFile('model.tflite', 'wb') as f:
f.write(tflite_model)

또는 저장된 모델에 저장하려면 :

# Convert the model.
model = tf.keras.models.load_model('hdf5_keras_model.hdf5')
tf.keras.models.save_model(
    model, filepath, overwrite=True, include_optimizer=True, save_format=None,
    signatures=None, options=None
)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관