2017-10-28 106 views
0

我正在使用tensorflow存储库中image_retraining文件夹中提供的再培训脚本。通过加载intermediate_output_graphs(.pb)继续培训(image_retraining/retrain.py)

之一解析器参数/标志让你存储中间图形每隔X步骤

parser.add_argument(
     '--intermediate_output_graphs_dir', 
     type=str, 
     default='tf_files2/tmp/intermediate_graph/', 
     help='Where to save the intermediate graphs.' 

然而,这似乎图表存储为冷冻图形与.pb扩展。 有关如何正确加载.pb文件以继续培训的信息非常少。 我发现的大多数信息都使用.meta图和.ckpts。 .pb将被弃用?

如果是这样,我应该重新从模型开始,并使用tf.Saver获得 .meta和ckpt图作为中间检查点?

昨天,我正在训练一个模型,由于某种原因训练冻结了,所以我想加载中间图形,并继续训练。

我使用以来模型进行再培训:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/image_retraining/retrain.py

如果任何人都可以点我或告诉我如何正确地加载了一个.pb中间曲线(循序渐进),从我离开的地方继续关 - 我真的很感激它。

谢谢。

编辑:

@Mingxing

所以我假设我应该让retrain.py首先创建默认的图形基于默认以来模型(下此功能),然后就用它覆盖加载图?

def create_model_graph(model_info): 
    """"Creates a graph from saved GraphDef file and returns a Graph object. 

    Args: 
    model_info: Dictionary containing information about the model architecture. 

    Returns: 
    Graph holding the trained Inception network, and various tensors we'll be 
    manipulating. 
    """ 
    with tf.Graph().as_default() as graph: 
    model_path = os.path.join(FLAGS.model_dir, model_info['model_file_name']) 
    with gfile.FastGFile(model_path, 'rb') as f: 
     graph_def = tf.GraphDef() 
     graph_def.ParseFromString(f.read()) 
     bottleneck_tensor, resized_input_tensor = (tf.import_graph_def(
      graph_def, 
      name='', 
      return_elements=[ 
       model_info['bottleneck_tensor_name'], 
       model_info['resized_input_tensor_name'], 
      ])) 
    return graph, bottleneck_tensor, resized_input_tensor 

EDIT_2:

我得到一个错误是:

ValueError: Tensor("second_to_final_fC_layer_ops/weights/final_weights_1:0", shape=(2048, 102 
4), dtype=float32_ref) must be from the same graph as Tensor("BottleneckInputPlaceholder:0", 
shape=(?, 2048), dtype=float32). 

我第一FC层后添加一个额外的FC层。 因此2048 - > 1024 - >变异前的分类数目。

当训练模型时,我没有问题,但现在加载图表我似乎遇到了上述错误。

这是所添加的层的外观:

layer_name = 'second_to_final_fC_layer_ops' 
    with tf.name_scope(layer_name): 
    with tf.name_scope('weights'): 
     initial_value = tf.truncated_normal(
      [bottleneck_tensor_size, 1024], stddev=0.001) 

     layer_weights = tf.Variable(initial_value, name='weights') 

     variable_summaries(layer_weights) 
    with tf.name_scope('biases'): 
     layer_biases = tf.Variable(tf.zeros([1024]), name='biases') 
     variable_summaries(layer_biases) 
    with tf.name_scope('Wx_plus_b'): 
     logits = tf.matmul(bottleneck_input, layer_weights) + layer_biases 
     tf.summary.histogram('pre_activations', logits) 
    with tf.name_scope('Relu_activation'): 
     relu_activated =tf.nn.relu(logits, name= 'Relu') 
     tf.summary.histogram('final_relu_activation', relu_activated) 

然后最后的层(其是原始最后的层,但现在的输入是输出来自最后一层,而不是瓶颈张量):

layer_name = 'final_training_ops' 
    with tf.name_scope(layer_name): 
    with tf.name_scope('weights'): 
     initial_value = tf.truncated_normal(
      [1024, class_count], stddev=0.001) 

     layer_weights = tf.Variable(initial_value, name='final_weights') 

     variable_summaries(layer_weights) 
    with tf.name_scope('biases'): 
     layer_biases = tf.Variable(tf.zeros([class_count]), name='final_biases') 
     variable_summaries(layer_biases) 
    with tf.name_scope('Wx_plus_b'): 
     logits = tf.matmul(relu_activated, layer_weights) + layer_biases 
     tf.summary.histogram('pre_activations', logits) 

    final_tensor = tf.nn.softmax(logits, name=final_tensor_name) 
    tf.summary.histogram('activations', final_tensor) 

编辑:还是不知道如何加载weights--加载图形结构似乎很容易,但我不知道如何加载已使用的培训,一旦盗梦空间的权重和投入再次使用转移学习。

使用来自image_retraining/retrain.py的权重和变量的清晰示例将非常有用。谢谢。

回答

1

您可以使用tf.import_graph_def导入您的冷冻.pb文件:

# Read the .pb file into graph_def. 
with tf.gfile.GFile(FLAGS.graph, "rb") as f: 
    graph_def = tf.GraphDef() 
    graph_def.ParseFromString(f.read()) 

# Restore the graph. 
with tf.Graph().as_default() as graph: 
    tf.import_graph_def(graph_def, name="") 

# After this, graph is the what you need. 

虽然没有什么错误直接使用冷冻.pb文件,我还是要指出的是,推荐的方法是遵循标准保存/恢复(official doc)。

+0

谢谢。我用解决方案的另一个问题更新了OP。当你有时间的时候,请看看。谢谢=) – Moondra

+0

我创建了另一个关于我得到的错误的更新。不急。只需登录更新。谢谢。 – Moondra