2

スリム ウォークスルー ノートブックからすべての例を正常に実行した後、グラフをフリーズしたいと考えました。それを行うために、私は以下を実行しました(元のノートブックからのコピー):

import os

from datasets import flowers
from nets import inception
from preprocessing import inception_preprocessing

slim = tf.contrib.slim
image_size = inception.inception_v1.default_image_size


def get_init_fn():
    """Returns a function run by the chief worker to warm-start the training."""
    checkpoint_exclude_scopes=["InceptionV1/Logits", "InceptionV1/AuxLogits"]

    exclusions = [scope.strip() for scope in checkpoint_exclude_scopes]

    variables_to_restore = []
    for var in slim.get_model_variables():
        excluded = False
        for exclusion in exclusions:
            if var.op.name.startswith(exclusion):
                excluded = True
                break
        if not excluded:
            variables_to_restore.append(var)

    return slim.assign_from_checkpoint_fn(
      os.path.join(checkpoints_dir, 'inception_v1.ckpt'),
      variables_to_restore)


train_dir = '/tmp/inception_finetuned/'

with tf.Graph().as_default():
    tf.logging.set_verbosity(tf.logging.INFO)

    dataset = flowers.get_split('train', flowers_data_dir)
    images, _, labels = load_batch(dataset, height=image_size, width=image_size)

    # Create the model, use the default arg scope to configure the batch norm parameters.
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits, _ = inception.inception_v1(images, num_classes=dataset.num_classes, is_training=True)

    # Specify the loss function:
    one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes)
    slim.losses.softmax_cross_entropy(logits, one_hot_labels)
    total_loss = slim.losses.get_total_loss()

    # Create some summaries to visualize the training process:
    tf.scalar_summary('losses/Total Loss', total_loss)

    # Specify the optimizer and create the train op:
    optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
    train_op = slim.learning.create_train_op(total_loss, optimizer)

    # Run the training:
    final_loss = slim.learning.train(
        train_op,
        logdir=train_dir,
        init_fn=get_init_fn(),
        number_of_steps=2)


print('Finished training. Last batch loss %f' % final_loss)

上記のコードにより、/tmp/inception_finetuned フォルダーに次のファイルが作成されました。

  • チェックポイント
  • model.ckpt-0.meta
  • events.out.tfevents.1478081437.Nikos-MacBook-Pro.local
  • model.ckpt-2 グラフ.pbtxt
  • model.ckpt-2.meta モデル.ckpt-0

次に、グラフをフリーズするために、次のコマンドを実行しました。

bazel-bin/tensorflow/python/tools/freeze_graph --input_graph=/tmp/inception_finetuned/graph.pbtxt --input_checkpoint=/tmp/inception_finetuned/model.ckpt-2 --output_graph=/tmp/freeze.pb --output_node_names=InceptionV1/Logits/Predictions/Softmax

ただし、このコマンドは次のエラーを生成しました。

W tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1
     [[Node: _send_InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1_0 = _Send[T=DT_FLOAT, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=6007788667487390928, tensor_name="InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1:0", _device="/job:localhost/replica:0/task:0/cpu:0"](InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1)]]
...
W tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1
     [[Node: _send_InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1_0 = _Send[T=DT_FLOAT, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=6007788667487390928, tensor_name="InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1:0", _device="/job:localhost/replica:0/task:0/cpu:0"](InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1)]]
Traceback (most recent call last):
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 135, in <module>
    tf.app.run()
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/platform/app.py", line 32, in run
    sys.exit(main(sys.argv[:1] + flags_passthrough))
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 132, in main
    FLAGS.output_graph, FLAGS.clear_devices, FLAGS.initializer_nodes)
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 121, in freeze_graph
    sess, input_graph_def, output_node_names.split(","))
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/framework/graph_util.py", line 226, in convert_variables_to_constants
    returned_variables = sess.run(variable_names)
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 915, in _run
    feed_dict_string, options, run_metadata)
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 965, in _do_run
    target_list, options, run_metadata)
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 985, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1
     [[Node: _send_InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1_0 = _Send[T=DT_FLOAT, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=6007788667487390928, tensor_name="InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1:0", _device="/job:localhost/replica:0/task:0/cpu:0"](InceptionV1/Logits/Conv2d_0c_1x1/biases/Adam_1)]]

次に、別のオプティマイザを使用しようとしました:

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)

次のエラーが発生しました:

W tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value global_step
     [[Node: _send_global_step_0 = _Send[T=DT_INT64, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=8900174487477528080, tensor_name="global_step:0", _device="/job:localhost/replica:0/task:0/cpu:0"](global_step)]]
...
     [[Node: _send_global_step_0 = _Send[T=DT_INT64, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=8900174487477528080, tensor_name="global_step:0", _device="/job:localhost/replica:0/task:0/cpu:0"](global_step)]]
W tensorflow/core/framework/op_kernel.cc:968] Failed precondition: Attempting to use uninitialized value global_step
     [[Node: _send_global_step_0 = _Send[T=DT_INT64, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=8900174487477528080, tensor_name="global_step:0", _device="/job:localhost/replica:0/task:0/cpu:0"](global_step)]]
Traceback (most recent call last):
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 135, in <module>
    tf.app.run()
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/platform/app.py", line 32, in run
    sys.exit(main(sys.argv[:1] + flags_passthrough))
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 132, in main
    FLAGS.output_graph, FLAGS.clear_devices, FLAGS.initializer_nodes)
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/tools/freeze_graph.py", line 121, in freeze_graph
    sess, input_graph_def, output_node_names.split(","))
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/framework/graph_util.py", line 226, in convert_variables_to_constants
    returned_variables = sess.run(variable_names)
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 915, in _run
    feed_dict_string, options, run_metadata)
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 965, in _do_run
    target_list, options, run_metadata)
  File "/Users/nikogamulin/workspace/tensorflow/bazel-bin/tensorflow/python/tools/freeze_graph.runfiles/org_tensorflow/tensorflow/python/client/session.py", line 985, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value global_step
     [[Node: _send_global_step_0 = _Send[T=DT_INT64, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=8900174487477528080, tensor_name="global_step:0", _device="/job:localhost/replica:0/task:0/cpu:0"](global_step)]]

同様に、次のコマンドを実行してモデルを再トレーニングする場合:

python train_image_classifier.py \        
    --train_dir=${TRAIN_DIR} \
    --dataset_dir=${DATASET_DIR} \
    --dataset_name=flowers \
    --dataset_split_name=train \
    --model_name=inception_v1 \
    --checkpoint_path=${CHECKPOINT_PATH} \
    --checkpoint_exclude_scopes=InceptionV1/Logits,InceptionV1/AuxLogits/Logits \
    --trainable_scopes=InceptionV1/Logits,InceptionV1/AuxLogits/Logits

グラフをフリーズしようとすると、関連するエラーが表示されます

global_step

上記のエラーが発生する理由と解決方法を知っている人はいますか? 誰かが inception v1 (tf-slim) グラフをフリーズできた場合、問題を解決する可能性のある提案に感謝します。

4

0 に答える 0