TensorFlow BERT モデルの変換¶
危険
ここで説明されているコードは非推奨になりました。従来のソリューションの適用を避けるため使用しないでください。下位互換性を確保するためにしばらく保持されますが、最新のアプリケーションでは使用してはなりません。
このガイドでは、非推奨となった変換方法について説明します。新しい推奨方法に関するガイドは、Python チュートリアルに記載されています。
BERT (トランスフォーマーからの双方向エンコーダー表現) の事前トレーニング済みモデルは公開されています。
サポートされるモデル¶
事前トレーニングされた BERT モデルリストの次のモデルが現在サポートされています。
BERT-Base, Cased
BERT-Base, Uncased
BERT-Base, Multilingual Cased
BERT-Base, Multilingual Uncased
BERT-Base, Chinese
BERT-Large, Cased
BERT-Large, Uncased
事前トレーニング済み BERT モデルをダウンロード¶
BERT ベースの多言語ケースなしモデルを使用してアーカイブをダウンロードして解凍します。
アーカイブが解凍されると、次のファイルが含まれるディレクトリー uncased_L-12_H-768_A-12
が作成されます。
bert_config.json
bert_model.ckpt.data-00000-of-00001
bert_model.ckpt.index
bert_model.ckpt.meta
vocab.txt
事前トレーニングされたモデルのメタグラフファイルは、bert_model.ckpt.*
です。
TensorFlow BERT モデルを IR に変換¶
モデルの BERT 中間表現 (IR) を生成するには、次のパラメーターを使用してモデル変換を実行します。
mo \
--input_meta_graph uncased_L-12_H-768_A-12/bert_model.ckpt.meta \
--output bert/pooler/dense/Tanh \
--input Placeholder{i32},Placeholder_1{i32},Placeholder_2{i32}
モデル内に複数の形状がハードコードされているため、事前トレーニング済みモデルは、すぐに使用できるバッチ再形状には適していません。
再構成可能な TensorFlow BERT モデルを OpenVINO IR に変換¶
事前トレーニングされた TensorFlow BERT モデルをバッチ次元で再形成可能にするには、次の手順に従います。
使用する事前トレーニング済み BERT モデルをサポートされるモデルのリストからダウンロードします。
-
google-research/bert git リポジトリーのクローンを作成します。
https://github.com/google-research/bert.git
-
クローン作成されたリポジトリーのルート・ディレクトリーに移動します。
cd bert
-
(オプション) 変換がテストされたコミットをチェックアウトします。
git checkout eedf5716c
-
GLUE データをロードするスクリプトをダウンロードします。
-
UNIX のようなシステムでは、次のコマンドを実行します。
wget https://gist.githubusercontent.com/W4ngatang/60c2bdb54d156a41194446737ce03e2e/raw/17b8dd0d724281ed7c3b2aeeda662b92809aadd5/download_glue_data.py
-
Windows* システム:
Python スクリプトを現在の作業ディレクトリーにダウンロードします。
-
-
以下を実行して GLUE データをダウンロードします。
python3 download_glue_data.py --tasks MRPC
-
ファイル
modeling.py
をテキストエディターで開き、行 923~924 を削除します。次のようになります。if not non_static_indexes: return shape
-
ファイル
run_classifier.py
を開き、行 645 の後に次のコードを挿入します。import os, sys import tensorflow as tf from tensorflow.python.framework import graph_io with tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph()) as sess: (assignment_map, initialized_variable_names) = \ modeling.get_assignment_map_from_checkpoint(tf.compat.v1.trainable_variables(), init_checkpoint) tf.compat.v1.train.init_from_checkpoint(init_checkpoint, assignment_map) sess.run(tf.compat.v1.global_variables_initializer()) frozen = tf.compat.v1.graph_util.convert_variables_to_constants(sess, sess.graph_def, ["bert/pooler/dense/Tanh"]) graph_io.write_graph(frozen, './', 'inference_graph.pb', as_text=False) print('BERT frozen model path {}'.format(os.path.join(os.path.dirname(__file__), 'inference_graph.pb'))) sys.exit(0)
挿入されたコードの前の行は次のようになります。
(total_loss, per_example_loss, logits, probabilities) = create_model( bert_config, is_training, input_ids, input_mask, segment_ids, label_ids, num_labels, use_one_hot_embeddings)
-
環境変数
BERT_BASE_DIR
、BERT_REPO_DIR
を設定し、スクリプトrun_classifier.py
を実行して、クローンされた BERT リポジトリーのルートにinference_graph.pb
ファイルを作成します。export BERT_BASE_DIR=/path/to/bert/uncased_L-12_H-768_A-12 export BERT_REPO_DIR=/current/working/directory python3 run_classifier.py \ --task_name=MRPC \ --do_eval=true \ --data_dir=$BERT_REPO_DIR/glue_data/MRPC \ --vocab_file=$BERT_BASE_DIR/vocab.txt \ --bert_config_file=$BERT_BASE_DIR/bert_config.json \ --init_checkpoint=$BERT_BASE_DIR/bert_model.ckpt \ --output_dir=./
次のコマンドライン・パラメーターを使用してモデル変換を実行し、再形成可能な BERT 中間表現 (IR) を生成します。
mo \ --input_model inference_graph.pb \ --input "IteratorGetNext:0{i32}[1,128],IteratorGetNext:1{i32}[1,128],IteratorGetNext:4{i32}[1,128]"
パラメーターの詳細については、TensorFlow からのモデル変換ガイドを参照してください。
モデル再形成の詳細については、形状推論の使用ガイドを参照してください。