3

'pool_3:0''softmax:0'レイヤーの両方の出力を抽出したいと思います。モデルを 2 回実行して、実行ごとに 1 つのレイヤーの出力を抽出することもできますが、これは少し無駄です。モデルを一度だけ実行することは可能ですか?

によって提供された例を使用していclassify_image.pyます。関連するスニペットは次のとおりです。

def run_inference_on_image(image_data):
  create_graph()
  with tf.Session() as sess:
    # Some useful tensors:
    # 'softmax:0': A tensor containing the normalized prediction across
    #   1000 labels.
    # 'pool_3:0': A tensor containing the next-to-last layer containing 2048
    #   float description of the image.
    # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
    #   encoding of the image.
    # Runs the softmax tensor by feeding the image_data as input to the graph.
    softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
    predictions = sess.run(softmax_tensor,
                           {'DecodeJpeg:0': image_data})
    predictions = np.squeeze(predictions)

    # Creates node ID --> English string lookup.
    node_lookup = NodeLookup()

    top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
    for node_id in top_k:
      human_string = node_lookup.id_to_string(node_id)
      score = predictions[node_id]
      print('%s (score = %.5f)' % (human_string, score))

    return predictions
4

1 に答える 1