Skip to content

Instantly share code, notes, and snippets.

@funwarioisii
Last active October 4, 2018 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funwarioisii/68ed46d8ccfcbc31a456b7c4166b8d0e to your computer and use it in GitHub Desktop.
Save funwarioisii/68ed46d8ccfcbc31a456b7c4166b8d0e to your computer and use it in GitHub Desktop.
pbファイルの作成

Tensor Flowのpbファイルの保存でしょうもないエラーで悩み続けていたので知見まとめ

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"])

一度変数を定数に変換する作業が必要らしい

これで先程のグラフ再構築コードは動く

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment