Knowledge
Model
- Tensorflowのモデルはmodel-ckpt.meta, model-ckpt.data-0000-of-00001, model-ckpt.index, checkpointの4つの変数を持つ*1
- ウェブサイトなどでデプロイするときに一つにまとめた.pbファイルにする
- kerasで保存する対象とそのコード。くわしくは下記サイトを参照*2
保存の関数 | 読み込みの関数 | 対象(拡張子) |
---|---|---|
model.save(‘hoge.h5’) | model = load_model(‘hoge.h5’) | アーキテクチャ + 重み + オプティマイザの状態(.hdf5 or .h) |
json_string = model.to_json() | model = model_from_json(json_string) | モデルのアーキテクチャ(weightパラメータや学習時の設定は含まない)(.json or .yml) |
model.save_weights(‘hoge.h5’) | model.load_weights(‘hoge.h5’) | モデルの重みのみ(.hdf5 or .h) |
References
TF2.0
Model save and load
.jsonと.h(.hdf5)で読み込み.pbで保存
import logging import tensorflow as tf from tensorflow.compat.v1 import graph_util from tensorflow.python.keras import backend as K from tensorflow import keras # necessary !!! tf.compat.v1.disable_eager_execution() # read the model json_string = open('./model/octClsf_1912280210_model.json').read() model = tf.keras.models.model_from_json(json_string) model.summary() # read the weights model.load_weights('./model/octClsf_1912280210_weights.hdf5') # save pb with K.get_session() as sess: output_names = [out.op.name for out in model.outputs] input_graph_def = sess.graph.as_graph_def() for node in input_graph_def.node: node.device = "" graph = graph_util.remove_training_nodes(input_graph_def) graph_frozen = graph_util.convert_variables_to_constants(sess, graph, output_names) tf.io.write_graph(graph_frozen, './model', 'model.pb') logging.info("save pb successfully!")
TF1.0
Model save and load
.jsonと.h(.hdf5)で読み込み.pbで保存。読み込みできないのでウソの可能性がある*1
import tensorflow as tf from keras import backend as K from keras.models import model_from_json # This line must be executed before loading Keras model. K.set_learning_phase(0) from keras.models import load_model # model = load_model('./model/keras_model.h5') # read the model # json_string = open('./model/octClsf_1912280210_model.json').read() # model = keras.model.model_from_json(json_string) # model = model_from_json(json_string) # read the weights # model.load_weights('./model/octClsf_1912280210_weights.hdf5') #model.load_model('./model/octClsf_1912280210_weights.hdf5') # model = load_model('./model/octClsf_1912280210_weights.hdf5') model = load_model('./model/unet_3class.hdf5') # model.summary() def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True): """ Freezes the state of a session into a pruned computation graph. Creates a new computation graph where variable nodes are replaced by constants taking their current value in the session. The new graph will be pruned so subgraphs that are not necessary to compute the requested outputs are removed. @param session The TensorFlow session to be frozen. @param keep_var_names A list of variable names that should not be frozen, or None to freeze all the variables in the graph. @param output_names Names of the relevant graph outputs. @param clear_devices Remove the device directives from the graph for better portability. @return The frozen graph definition. """ from tensorflow.python.framework.graph_util import convert_variables_to_constants graph = session.graph with graph.as_default(): freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or [])) output_names = output_names or [] output_names += [v.op.name for v in tf.global_variables()] # Graph -> GraphDef ProtoBuf input_graph_def = graph.as_graph_def() if clear_devices: for node in input_graph_def.node: node.device = "" frozen_graph = convert_variables_to_constants(session, input_graph_def, output_names, freeze_var_names) return frozen_graph frozen_graph = freeze_session(K.get_session(), output_names=[out.op.name for out in model.outputs]) tf.train.write_graph(frozen_graph, "model", "tf1_model.pb", as_text=False) logging.info("save pb successfully!")
References
- *1:kerasモデルh5ファイルをテンソルフロー保存モデル(pb)に変換する方法
- *2:How to freeze a graph in Tensorflow
- *3:How to restore Tensorflow model from .pb file in python?
- *4:How to convert trained Keras model to a single TensorFlow .pb file and make prediction
Keras
Model save and load
重みからmodel読み込むとエラーになる。JSON→HDF5の順なら問題ない
model.save_weights('model.hdf5')
load_modelでパスを指定して読み込むことでエラーを解消*1
from keras.models import load_model model = load_model('model.h5')
カスタムされたモデルは引数をつけないとロードできない*2
References
- *1:Save and load weights in keras
- *2:kerasのモデルのload_modelでエラー
- *3:[TF]KerasでModelとParameterをLoad/Saveする方法
- *4:Kerasで可視化いろいろ
- よさそうなのでやってみる