본문 바로가기
archive.tar

[TensorFlow] inception resnet v2 모델을 사용하여 이미지 추론하기

by 냉동만두 2017. 2. 8.

inception-resnet-v2 image classification


# inception-resnet-v2 설명

https://tensorflow.blog/2016/09/01/new-convnet-model-inception-resnet-v2/

https://research.googleblog.com/2016/08/improving-inception-and-image.html



# tensorflow  'models-slim' 소스코드 다운로드

https://github.com/tensorflow/models


# 다운로드 받은 'slim' 소스코드 폴더로 이동     ex) $HOME/models/slim

앞으로 사용할 .ckpt 파일과 label_image.py는 이 폴더 아래 있어야 한다


# inception-resnet-v2 체크포인트 파일 다운로드 후 models/slim/ 아래에 압축 해제

ex) $HOME/models/slim/inception_resnet_v2_2016_08_30.ckpt


# 추론 소스코드 작성 ($HOME/models/slim/label_image.py)

import os
import tensorflow as tf
from datasets import imagenet
from nets import inception_resnet_v2
from preprocessing import inception_preprocessing

# set your .ckpt file
checkpoints_dir = '/home/username/models/slim'

slim = tf.contrib.slim

batch_size = 3
image_size = 299

with tf.Graph().as_default():
    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
        # set your image
        imgPath = 'panda.jpg'
        testImage_string = tf.gfile.FastGFile(imgPath, 'rb').read()
        testImage = tf.image.decode_jpeg(testImage_string, channels=3)
        processed_image = inception_preprocessing.preprocess_image(testImage, image_size, image_size, is_training=False)
        processed_images = tf.expand_dims(processed_image, 0)

        logits, _ = inception_resnet_v2.inception_resnet_v2(processed_images, num_classes=1001, is_training=False)
        probabilities = tf.nn.softmax(logits)

        init_fn = slim.assign_from_checkpoint_fn(
        os.path.join(checkpoints_dir, 'inception_resnet_v2_2016_08_30.ckpt'), slim.get_model_variables('InceptionResnetV2'))

        with tf.Session() as sess:
            init_fn(sess)

            np_image, probabilities = sess.run([processed_images, probabilities])
            probabilities = probabilities[0, 0:]
            sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])]

            names = imagenet.create_readable_names_for_imagenet_labels()
            for i in range(15):
                index = sorted_inds[i]
                print((probabilities[index], names[index]))


# label_image.py 터미널에서 실행 , 추론

추론에 사용된 샘플 사진 panda.jpg


$ python label_image.py

I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcurand.so locally
I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties:
name: GeForce GTX 960
major: 5 minor: 2 memoryClockRate (GHz) 1.266
pciBusID 0000:01:00.0
Total memory: 1.94GiB
Free memory: 1.62GiB
I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0
I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0:   Y
I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 960, pci bus id: 0000:01:00.0)
W tensorflow/core/common_runtime/bfc_allocator.cc:217] Ran out of memory trying to allocate 1.91GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.
W tensorflow/core/common_runtime/bfc_allocator.cc:217] Ran out of memory trying to allocate 2.07GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.
(0.92441648, 'giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca')
(0.0019214519, 'lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens')
(0.00042356219, 'soccer ball')
(0.00038290277, 'capuchin, ringtail, Cebus capucinus')
(0.00032850041, 'sloth bear, Melursus ursinus, Ursus ursinus')
(0.00031071997, 'earthstar')
(0.00030963434, 'bullet train, bullet')
(0.00030530145, 'brown bear, bruin, Ursus arctos')
(0.00027325188, 'indri, indris, Indri indri, Indri brevicaudatus')
(0.00027179901, 'Lakeland terrier')
(0.00025744142, 'cheetah, chetah, Acinonyx jubatus')
(0.00024388272, 'colobus, colobus monkey')
(0.00024178435, 'African elephant, Loxodonta africana')
(0.00024043048, 'Walker hound, Walker foxhound')
(0.00023779736, 'gorilla, Gorilla gorilla')


터미널 창 내용을 확인하면 높은 점수로 추론에 성공한다. 기존에 .pb 파일을 사용하여 추론 한 것보다 시간은 조금 더 걸린다.

그렇지만 .ckpt 파일을 바로 사용하여 추론할 수 있다. 이 소스를 활용하면 inception-v4 등 기타 모델을 사용 할 수 있겠다

다음에는 inception-resnet-v2 모델을 사용하여 원하는 이미지로 학습 시켜보고 이 소스를 활용하여 추론 해 봐야지.



* 참고

http://stackoverflow.com/questions/39750572/tensorflow-inception-resnet-v2-input-tensor




# 자신의 데이터로 재학습 후 추론 하기

http://gusrb.tistory.com/54