Skip to content

Instantly share code, notes, and snippets.

@solaris33
Last active January 7, 2019 01:48
Show Gist options
  • Save solaris33/bbc3255bc50272ae60de6d814ed23caa to your computer and use it in GitHub Desktop.
Save solaris33/bbc3255bc50272ae60de6d814ed23caa to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""Inception v3 architecture 모델을 retraining한 모델을 이용해서 이미지에 대한 추론(inference)을 진행하는 예제"""
import numpy as np
import tensorflow as tf
imagePath = '/tmp/test_chartreux.jpg' # 추론을 진행할 이미지 경로
modelFullPath = '/tmp/output_graph.pb' # 읽어들일 graph 파일 경로
labelsFullPath = '/tmp/output_labels.txt' # 읽어들일 labels 파일 경로
def create_graph():
"""저장된(saved) GraphDef 파일로부터 graph를 생성하고 saver를 반환한다."""
# 저장된(saved) graph_def.pb로부터 graph를 생성한다.
with tf.gfile.FastGFile(modelFullPath, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
def run_inference_on_image():
answer = None
if not tf.gfile.Exists(imagePath):
tf.logging.fatal('File does not exist %s', imagePath)
return answer
image_data = tf.gfile.FastGFile(imagePath, 'rb').read()
# 저장된(saved) GraphDef 파일로부터 graph를 생성한다.
create_graph()
with tf.Session() as sess:
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor,
{'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)
top_k = predictions.argsort()[-5:][::-1] # 가장 높은 확률을 가진 5개(top 5)의 예측값(predictions)을 얻는다.
f = open(labelsFullPath, 'rb')
lines = f.readlines()
labels = [str(w).replace("\n", "") for w in lines]
for node_id in top_k:
human_string = labels[node_id]
score = predictions[node_id]
print('%s (score = %.5f)' % (human_string, score))
answer = labels[top_k[0]]
return answer
if __name__ == '__main__':
run_inference_on_image()
@chanchanchan92
Copy link

안녕하세요 질문좀드리려고합니다. 이소스와 inceptionv3소스를 가지고 한프로세스안에서 여러번수행하려고하는데 한번 인식된 추론에 대한 정보들이 갱신이 되지않습니다. 이소소로 따지면 top_k부분이 갱신이안되서 새로운 이미지에 대한 추론값이 계쏙그대로인데요 어떻게 수정해야할까요 만약 프로세스를 껏다가 다시켜 추론하면 원하는결과가나오는데 한프로세스안에서는 변화가없습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment