티스토리 뷰



Here is an example of using google inception v3 model with tf.slim


TF-Slim 기존의 복잡한 모델을 조금 더 쉽게 정의하고 학습 하기위해 새롭게 나온 API 라고 합니다.

아래 TF-Slim 에 포함되어 있는 CNN 중에 Inception V4 에 대해서 어떻게 사용하는지 설명을 드리고자 합니다.


 목표는 V4  모델이었지만 컴퓨팅 자원의 한계로 V3 로 수정하였습니다.


TensorFlow-Slim image classification model library


* https://github.com/tensorflow/models/tree/master/slim


Model

Top 1
Accuracy (%)

Top 5
Accuracy (%)

 Inception V1

 69.8

 89.6

 Inception V2

 73.9

 91.8

 Inception V3

 78

 93.9

 Inception V4

 80.2

 95.2

 Inception-ResNet-v2

 80.4

 95.3

 ResNet V1 50

 75.2

 92.2

 ResNet V1 101

 76.4

 92.9

 ResNet V1 152

 76.8

 93.2

 ResNet V2 50^

 75.6

 92.8

 ResNet V2 101^

 77

 93.7

 ResNet V2 152^

 77.8

 94.1



설치부터 dataset 을 만들고training 와 최종 evaluation 까지 전체 방향에 대해서 수행해 볼 수 있도록 

설명드리겠습니다. 

Linux 환경에 익숙하지 않으신 분들 위해서 windows 에서 해당 프로젝트를 진행 해보겠습니다.

물론 python 3.6 + tensorflow 1.0▲ 버전은 필수로 설치되어야 있어야 합니다.



1.Installing the TF-slim image models library

 github 에서 필요한 model 및 tf-slim 라이브러리를 clone 하도록 하겠습니다.

git clone https://github.com/tensorflow/models/



2.Preparing the datasets

기본적으로 데이터 set은 cifar10, flowers, mnist , imagenet 등을 자동으로 다운로드 받을 수 있지만 ,

자신만의 데이터를 활용하는 방법을 알려드리기 구글에서 새 이미지를 모아봤습니다.


download_and_convert_data.py # 데이터셋 (병아리, 매, 비둘기, 참새 4종 )





 3.Converting to TFRecord format


dataset 이 로컬에 저장되어 있다보니 slim 에 있는 예제대로 수행이 어려워 아래 몇가지 수정 사항이 생기네요


* datasets/download_and_convert_birds.py  #추가

* download_and_convert_data.py                      # 수정



<수정 내용 >



         < 추가 되어야 할 파일 > 




python download_and_convert_data.py ^
  --dataset_name=birds ^
  --dataset_dir=D:\tmp\birds




Training a model from scratch.


컴퓨터 자원의 한계로 이부분은 넘어가고 기존 만들어진 모델을 fine-tuning 해 보도록 하겠습니다,

4.Fine-tuning a model from an existing checkpoint


python train_image_classifier.py \ --train_dir=${TRAIN_DIR} \ --dataset_dir=${DATASET_DIR} \ --dataset_name=flowers \ --dataset_split_name=train \ --model_name=inception_v3 \ --checkpoint_path=${CHECKPOINT_PATH} \ --checkpoint_exclude_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \ --trainable_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \ --max_number_of_steps=2100 \ --save_summaries_secs=600 \ --save_interval_secs=300 \ --batch_size=32 #resource exhausted error 시 size 를 줄여주세요







5.Evaluating performance of a model


python eval_image_classifier.py \ --alsologtostderr \ --checkpoint_path=${CHECKPOINT_FILE} \ --dataset_dir=${DATASET_DIR} \ --dataset_name=imagenet \ --dataset_split_name=validation \ --model_name=inception_v3 \ --batch_size=16 ## GPU 가 한참 지난 750 Ti 라서 batch size 를 키우면 할당이 안되네요







6.Test sample images from trained model

label_image.py sciprt # 특정 폴더 이미지를 해당 모델로 테스트



import glob
import os,re,sys
import argparse
import importlib
import cv2

import tensorflow as tf
from preprocessing import inception_preprocessing

slim = tf.contrib.slim

# prefix image size
image_size = 299

def run(args):
	data_path = args.data_path
	label_path = args.label_path

	model_path = args.model_path
	model_name = args.model_name
	model_scope = model_name +'_arg_scope'
	
	inception = importlib.import_module('nets.'+model_name)


	with tf.Graph().as_default():
		with slim.arg_scope(getattr(inception,model_scope)()):


			files = glob.glob(data_path+os.path.sep+"*.jpg")
			file_list = list()

			for idx,f in enumerate(files):
				f_string = tf.gfile.FastGFile(f, 'rb').read()
				test_img = tf.image.decode_jpeg(f_string, channels=3)
				processed_image = inception_preprocessing.preprocess_image(test_img, image_size, image_size, is_training=False)
				#processed_images = tf.expand_dims(processed_image, 0)
				file_list.append(os.path.basename(f))
				if(idx == 0):
					processed_images = [processed_image]
				else:
					processed_images.append(processed_image)

			processed_images = tf.stack(processed_images,axis=0)

			with open(label_path,'r') as rdata:
				names = dict()
				for row in rdata:
					strip_row = row.strip()
					split_row = strip_row.split(":")
					if(len(split_row) == 2):
						names[int(split_row[0])]=split_row[1]


			logits, _ = getattr(inception,model_name)(processed_images, num_classes=4, is_training=False)
			probabilities = tf.nn.softmax(logits)

			init_fn = slim.assign_from_checkpoint_fn(model_path, slim.get_model_variables('InceptionV3'))

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

				np_image, probabilities = sess.run([processed_images, probabilities])

				print("\n========  DATA RESULT  =======\n")
				print("filename\t"+"\t".join(names.values()))

				for idx,iter in enumerate(probabilities):
					print(file_list[idx]+'\t' +'\t'.join([str(round(i,2)) for i in iter]))
				

if __name__ == '__main__':

	parser = argparse.ArgumentParser()
	parser.add_argument("--data_path",help="the path to test images")
	parser.add_argument("--model_path")
	parser.add_argument("--model_name")
	parser.add_argument("--label_path")
	if(len(sys.argv) != 5):
		parser.print_help()
		parser.exit()
	args = parser.parse_args()
	run(args)




label 코드를 활용하여 아래 최종 confusion matrix를 구해봤습니다.

전체 학습 보다는 --trainable_scopes=InceptionV3/Logits,InceptionV3/AuxLogits  에서 설정했듯이

Logits 와 AuxLogits layter 만 학습을 진행했는데 생각보다 꽤 괜찮은 결과가 나온 것 같습니다.



'프로그래밍 > tensorflow' 카테고리의 다른 글

Tensorflow Serving vs Flask REST API  (0) 2021.12.28
[ slim ] model-inception  (0) 2017.10.09
[ intro ] google cloud platform  (0) 2017.09.25
[ MLP ] neural network regression  (3) 2017.09.04
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함