2017-10-28 1 views
0

나는 tensorflow 저장소에서 image_retraining 폴더에 제공된 retrain 스크립트를 사용하고 있습니다. 파서 인수의 intermediate_output_graphs (.pb)로드하여 계속 교육 (image_retraining/retrain.py)

는/플래그는 중간 그래프를 저장할 때마다 X가이 .pb 또 확장자를 가진 냉동 그래프로 그래프를 저장하는 것, 그러나

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

단계를 보자. 교육을 계속하기 위해 .pb 파일을 올바르게로드하는 방법에 대한 정보는 거의 없습니다. 내가 찾은 정보의 대부분은 .meta 그래프와 .ckpts를 사용합니다. .pb는 더 이상 사용되지 않을 예정입니까?

그렇다면 모델을 처음부터 다시 테스트하고 tf.Saver를 사용하여 .meta 및 ckpt 그래프를 중간 체크 포인트로 사용해야합니까?

어제는 모델을 교육 중이었고 어떤 이유로 교육이 중단되어 중간 그래프를로드하고 교육을 계속하고 싶습니다.

나는 재교육을 위해 설립 모델을 사용하고 있습니다 :
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/image_retraining/retrain.py

사람이 날 지점 또는 나를 보여줄 수 있다면 얼마나 제대로 .pb 또 중간 그래프 (단계적으로)로드 나는 부분부터 계속 나는 정말로 감사 할 것입니다.

감사합니다.

편집 :

그래서 난 그냥 첫 번째 (아래에이 기능) 기본 개시 모델을 기반으로 기본 그래프를 만들 retrain.py하도록해야 겠지

을 @Mingxing 후 바로으로 덮어 로드 된 그래프?

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 :

내가지고있어 오류가 있습니다 : 내가 처음 FC 층 후 추가 FC 층을 추가했다

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). 

. 그래서 2048 -> 1024 -> 수업 전 tranining.

모델을 교육 할 때 문제가 없었지만 그래프를로드하면 위의 오류가 발생하는 것 같습니다.

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

당신은 당신의 냉동 .pb 또 파일을 가져 tf.import_graph_def를 사용할 수 있습니다

# 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