2017-08-07 19 views
0

を使用する私はtensorflowに新しいです、私はグラフどのようにすでに訓練を受けたinceptionV4モデル

import os, argparse 

import tensorflow as tf 
from tensorflow.python.framework import graph_util 

dir = os.path.dirname(os.path.realpath(__file__)) 

def freeze_graph(model_folder, output_node_names, output_graph): 
    # We retrieve our checkpoint fullpath 
    checkpoint = tf.train.get_checkpoint_state(model_folder) 
    input_checkpoint = checkpoint.model_checkpoint_path 

    # We precise the file fullname of our freezed graph 
    #absolute_model_folder = "/".join(input_checkpoint.split('/')[:-1]) 
    #output_graph = absolute_model_folder + "/frozen_model.pb" 

    # Before exporting our graph, we need to precise what is our output node 
    # This is how TF decides what part of the Graph he has to keep and what part it can dump 
    # NOTE: this variable is plural, because you can have multiple output nodes 
    output_node_names = output_node_names 

    # We clear devices to allow TensorFlow to control on which device it will load operations 
    clear_devices = True 

    # We import the meta graph and retrieve a Saver 
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices) 

    # We retrieve the protobuf graph definition 
    graph = tf.get_default_graph() 
    input_graph_def = graph.as_graph_def() 

    # We start a session and restore the graph weights 
    with tf.Session() as sess: 
     saver.restore(sess, input_checkpoint) 

     # We use a built-in TF helper to export variables to constants 
     output_graph_def = graph_util.convert_variables_to_constants(
      sess, # The session is used to retrieve the weights 
      input_graph_def, # The graph_def is used to retrieve the nodes 
      output_node_names.split(",") # The output node names are used to select the usefull nodes 
     ) 

     # Finally we serialize and dump the output graph to the filesystem 
     with tf.gfile.GFile(output_graph, "wb") as f: 
      f.write(output_graph_def.SerializeToString()) 
     print("%d ops in the final graph." % len(output_graph_def.node)) 


if __name__ == '__main__': 
    parser = argparse.ArgumentParser() 
    parser.add_argument("--model_folder", type=str, help="Model folder to export") 
    parser.add_argument("--output_node_names", type=str, default="frozen_model.pb", help="output_node_names") 
    parser.add_argument("--output_graph", type=str, help="output_graph name") 
    args = parser.parse_args() 

    freeze_graph(args.model_folder, args.output_node_names, args.output_graph) 
を凍結するコードの下に使用私自身のデータ http://download.tensorflow.org/models/inception_v4_2016_09_09.tar.gz

python train_image_classifier.py \ 
--train_dir=${TRAIN_DIR} \ 
--dataset_name=stamps \ 
--dataset_split_name=train \ 
--dataset_dir=${DATASET_DIR} \ 
--model_name=inception_v4 \ 
--clone_on_cpu=true \ 
--checkpoint_path=${PRETRAINED_CHECKPOINT_DIR}/inception_v4.ckpt \ 
--checkpoint_exclude_scopes=InceptionV4/Logits,InceptionV4/AuxLogits \ 
--trainable_scopes=InceptionV4/Logits,InceptionV4/AuxLogits \ 
--max_number_of_steps=50 \ 
--batch_size=32 \ 
--learning_rate=0.01 \ 
--learning_rate_decay_type=fixed \ 
--save_interval_secs=60 \ 
--save_summaries_secs=60 \ 
--log_every_n_steps=100 \ 
--optimizer=rmsprop \ 
--weight_decay=0.00004 

python eval_image_classifier.py \ 
--checkpoint_path=${TRAIN_DIR} \ 
--eval_dir=${TRAIN_DIR} \ 
--dataset_name=stamps \ 
--dataset_split_name=validation \ 
--dataset_dir=${DATASET_DIR} \ 
--model_name=inception_v4 \ 
--batch_size=32 

を訓練前に訓練inceptionV4モデルを使用

使用するoutput_node_nameがわかりません。使用するようになりました。InceptionV4/Logits/Predictions

python ${OUTPUT_DIR}/freeze.py --model_folder=${TRAIN_DIR} \ 
--output_node_names=InceptionV4/Logits/Predictions \ 
--output_graph=${OUTPUT_DIR}/frozen_inception_v4.pb 

画像ラベルの出力グラフfrozen_inception_v4.pbの使い方は?

+0

公式[例](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/label_image/label_image.py)はinput_layerとoutput_layerを必要とするが、私はのために使用する知りません開始V4 – Lee

答えて

1

フローズンイメージモデルの使用例についてlabel_imageのサンプルコードを見てみましょう:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/label_image/label_image.py

をフラグのほとんどは彼らのデフォルトで[OK]をする必要がありますが、あなたは--input_mean=-127--input_std=127が必要になります、--output_layer=InceptionV4/Logits/Prediction、および--graph=${OUTPUT_DIR}/frozen_inception_v4.pb

+0

これはdefaのようです入力入力が見つからない。 'python tensorflow/examples/label_image/label_image.py --image = 11323.jpg \ --input_mean = -127 --input_std = 127 --output_layer = InceptionV4/Logits /予測--graph = frozen_inception_v4.pb KeyError: "名前 'import/input'は、グラフにない操作を参照しています。" – Lee

関連する問題