Tensor Flowのpbファイルの保存でしょうもないエラーで悩み続けていたので知見まとめ
Tensor Flowではグラフを構築し,パラメータを更新する
このパラメータの値を保存し,構築したグラフをTensor Flow Mobile/Lite/js ...など
別のフレームワークなどで使うために,一度pbファイルにする
import tensorflow_hub as hub
import tensorflow as tf
with tf.Graph().as_default() as g:
im_plh = tf.placeholder("float", shape=[None, 224, 224, 3])
lb_plh = tf.placeholder("float", shape=[None, 10])
module_spec = hub.load_module_spec("https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/2")
module = hub.Module(module_spec)
features = module(im_plh)
weight = tf.get_variable("example", shape=[1792, 10], initializer=tf.contrib.layers.variance_scaling_initializer())
prob = tf.matmul(features, weight, name='result')
loss_op = -tf.reduce_mean(lb_plh*tf.log(tf.clip_by_value(prob,1e-10,1.0)))
train_op = tf.train.AdamOptimizer(0.001).minimize(loss_op)
with tf.Session() as sess:
(sess.run(tf.global_variables_initializer()))
(sess.run(tf.tables_initializer()))
image = np.empty([1, 224, 224, 3])
label = np.empty([1, 10])
sess.run(train_op, {
im_plh: image,
lb_plh: label
})
graph_def = graph_util.convert_variables_to_constants(sess, g.as_graph_def(), ["result"])
tf.train.write_graph(graph_def,".","example.pb",as_text=False)
とするとexample.pb
というファイル名で保存される
このグラフが保存されたファイルを読み出して,再利用する
call_graph = tf.Graph()
call_graph_def = tf.GraphDef()
with open('./example.pb', 'rb') as f:
call_graph_def.ParseFromString(f.read())
with call_graph.as_default():
tf.import_graph_def(call_graph_def)
input_op = call_graph.get_operation_by_name('import/Placeholder').outputs[0]
output_op = call_graph.get_operation_by_name('import/result').outputs[0]
with tf.Session(graph = call_graph) as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.tables_initializer())
image = np.empty([1, 224, 224, 3], dtype=np.float32)
print(sess.run(output_op, {
input_op: image
}))
これは以下のようなエラーが発生する
FailedPreconditionError: Error while reading resource variable module/MobilenetV2/expanded_conv_15/expand/BatchNorm/moving_variance from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/module/MobilenetV2/expanded_conv_15/expand/BatchNorm/moving_variance)
[[Node: import/module_apply_default/MobilenetV2/expanded_conv_15/expand/BatchNorm/FusedBatchNorm/ReadVariableOp_1 = ReadVariableOp[dtype=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](import/module/MobilenetV2/expanded_conv_15/expand/BatchNorm/moving_variance)]]
何らかの変数が初期化されていないことを示唆しているが,これは問題解決に役立たない
モデルの作成と呼び出しは以下
from tensorflow.python.framework import graph_util
with tf.Graph().as_default() as g:
im_plh = tf.placeholder("float", shape=[None, 224, 224, 3])
lb_plh = tf.placeholder("float", shape=[None, 10])
module_spec = hub.load_module_spec("https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/feature_vector/2")
module = hub.Module(module_spec)
features = module(im_plh)
weight = tf.get_variable("example", shape=[1792, 10], initializer=tf.contrib.layers.variance_scaling_initializer())
prob = tf.matmul(features, weight, name='result')
loss_op = -tf.reduce_mean(lb_plh*tf.log(tf.clip_by_value(prob,1e-10,1.0)))
train_op = tf.train.AdamOptimizer(0.001).minimize(loss_op)
with tf.Session() as sess:
(sess.run(tf.global_variables_initializer()))
(sess.run(tf.tables_initializer()))
image = np.empty([1, 224, 224, 3])
label = np.empty([1, 10])
sess.run(train_op, {
im_plh: image,
lb_plh: label
})
graph_def = graph_util.convert_variables_to_constants(sess, g.as_graph_def(), ["result"])
tf.train.write_graph(graph_def,".","example.pb",as_text=False)
注意すべき変更点は
from tensorflow.python.framework import graph_util
と
graph_def = graph_util.convert_variables_to_constants(sess, g.as_graph_def(), ["result"])
で
一度変数を定数に変換する作業が必要らしい
これで先程のグラフ再構築コードは動く