BLOG

TensorFlow 훈련을 받은 이미지 분류 모델을 AWS DeepLens에 배포하기
작성일: 2018-08-30

TensorFlow(버전 1.4)를 사용하여 훈련을 받은 컴퓨터 비전 모델을 AWS DeepLens에 배포할 수 있음을 알려드리게 되어 기쁘게 생각합니다. 머리 자세 탐지는 AWS DeepLens 샘플 프로젝트의 일부입니다. 이 글에서는 Amazon SageMaker의 P2 교육 인스턴스를 사용하여 모델을 처음부터 교육하는 방법을 보여 드리겠습니다. 저희는 ResNet-50 모델을 사용하고 훈련된 모델을 ” frozen” protobuff 형식으로 저장할 것입니다. TensorFlow는 모델 그래프(예: 체크포인트, .ckpt-XXX.meta, .ckpt_XXX.index, .ckpt_XXX.data-00000-of-00001, .pbtxt, optimized protobuff, frozen protobuff 등)를 저장할 수 있는 다양한 데이터 형식을 제공하지만 AWS DeepLens 모델 optimizer는 frozen protobuff 형식을 지원합니다.

 

 

앞에서 간단한 Python API를 사용하여 AWS DeepLens에 Gluon 모델 배포에 대해서 설명한 동일한 Prima 머리 자세 데이터셋을 사용합니다.

 

Amazon S3 버킷 생성하기

지난번 글에서 했던 것처럼, 저희는 먼저 Amazon S3 콘솔을 사용하여 Amazon S3 버킷을 만들 것입니다. 이 예에서는 북 버지니아(US East 1) AWS 리전에 호스팅된 S3 버킷을 “deeplens-sagemaker-0001″라고 이름 붙이겠습니다. (교육받은 모델 아티팩트를 AWS DeepLens에 직접 배포하려면 리전은 북 버지니아(US East 1)여야 합니다.)

버킷 안에는 “headpose”라는 폴더가 있습니다. “headpose” 폴더에는 “TFartifacts,” “customTFcodes,” “datasets,” and “testIMs” 라는 4개의 하위 폴더가 있습니다.

 


데이터셋 폴더의 이전(HeadPoseData_trn_test_x15_py2.pkl)에 연결된 이전 블로그 게시물의 머리 자세 데이터셋을 호스트하려고 합니다.

 

 

이는 준비를 위한 것입니다.

 

Amazon SageMaker 노트북

이제 Amazon SageMaker를 런칭하겠습니다. Amazon SageMaker 노트북을 연 후 샘플 노트북, TensorFlow ResNet 스크립트 및 진입점 Python 스크립트(각각 tensorflow_resnet_headpose_for_deeplens.ipynbresnet_model_headpose.py, 및 resnet_headpose)를 업로드합니다. 이러한 스크립트는 Amazon SageMaker 샘플 스크립트에서 수정됩니다.

 

 

노트북과 Python 스크립트를 추가한 후에는 교육을 세 단계만 실행할 수 있습니다.

먼저 샘플 Amazon SageMaker 노트북(tensorflow_resnet_headpose_for_deeplens.ipynb)에 S3 버킷 이름을 지정합니다. 이 파트에서는 “headpose” 폴더와 “TFartifacts” 및 그 아래에 있는 “customTFcodes”과 같은 S3 버킷 내의 다른 폴더도 지정합니다.

import os

import sagemaker

from sagemaker import get_execution_role

 

sagemaker_session = sagemaker.Session()

 

s3_bucket = ‘deeplens-sagemaker-0001’

headpose_folder = ‘headpose’

 

#Bucket location to save your custom code in tar.gz format.

custom_code_folder = ‘customTFcodes’

custom_code_upload_location = ‘s3://{}/{}/{}’.format(s3_bucket, headpose_folder, custom_code_folder)

 

#Bucket location where results of model training are saved.

model_artifacts_folder = ‘TFartifacts’

model_artifacts_location = ‘s3://{}/{}/{}’.format(s3_bucket, headpose_folder, model_artifacts_folder)

 

#IAM execution role that gives SageMaker access to resources in your AWS account.

#We can use the SageMaker Python SDK to get the role from our notebook environment.

 

role = get_execution_role()

 

둘째, TensorFlow 개체에서 교육 인스턴스 및 기타 파라미터를 지정합니다. 이 예에서는 교육에 ml.p2.xlarge 인스턴스를 사용합니다.

from sagemaker.tensorflow import TensorFlow

 

source_dir = os.path.join(os.getcwd())

 

estimator = TensorFlow(entry_point=’resnet_headpose.py’,

framework_version = 1.4,

source_dir=source_dir,

role=role,

training_steps=20000, evaluation_steps=500,

train_instance_count=1,

base_job_name=’deeplens-TF-headpose’,

output_path=model_artifacts_location,

code_location=custom_code_upload_location,

train_instance_type=’ml.p2.xlarge’,

train_max_run = 432000,

train_volume_size=100)

 

 

# Head-pose dataset “HeadPoseData_trn_test_x15_py2.pkl” is in the following S3 folder.

dataset_location = ‘s3://{}/{}/datasets’.format(s3_bucket, headpose_folder)

 

AWS DeepLens는 현재 TensorFlow 버전 1.4(2018년 8월 9일 기준)를 지원합니다. Amazon SageMaker는 framework_version = 1.4를 간단히 표시하여 버전 제어를 지원합니다.

마지막으로, “.fit” 방법을 통해 교육을 실시합니다.

estimator.fit(dataset_location)

 

교육이 끝나면 S3 버킷의 TFartifacts 폴더에 있는 출력 폴더 안에 일련의 훈련된 TensorFlow 모델 아티팩트(model.tar.gz)를 찾을 수 있습니다.

 

 

AWS DeepLens용 frozen protobuff 파일 만들기

저희는 방금 만든 model.tar.gz에서 frozen protobuff 파일을 만들 것입니다. 이 튜토리얼에서는 동일한 Amazon SageMaker 노트북에서 TensorFlow Python API를 사용합니다.

먼저 모델 아티팩트가 포함된 압축 파일을 Amazon SageMaker 노트북 로컬 디렉토리에 다운로드합니다.

import boto3

s3 = boto3.resource(‘s3’)

key = ‘{}/{}/{}/output/model.tar.gz’.format(headpose_folder, model_artifacts_folder,estimator.latest_training_job.name)

print(key)

s3.Bucket(s3_bucket).download_file(key,’model.tar.gz’)

 

파일의 압축을 푼 후 export/Servo/{Assigned by Amazon SageMaker} 디렉토리에 있는 saved_model.pb, variables/variables.index, 및 variables/variables.data-00000-of-00001 이라는 세 개의 별도 파일을 찾습니다.

import glob

model_dir = glob.glob(‘export/*/*’)

print(model_dir)

 

다음은 그래프를 동결하고 frozen protobuff 형식으로 저장하는 코드입니다.

import tensorflow as tf

from tensorflow.python.tools import optimize_for_inference_lib

def freeze_graph(model_dir, output_node_names):

“””Extract the sub graph defined by the output nodes and convert

all its variables into constant

Args:

model_dir: the root folder containing the checkpoint state file

output_node_names: a string, containing all the output node’s names,

comma separated

“””

 

# We start a session using a temporary fresh Graph

with tf.Session(graph=tf.Graph()) as sess:

# We import the meta graph in the current default Graph

tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], model_dir)

 

# We use a built-in TF helper to export variables to constants

input_graph_def = tf.graph_util.convert_variables_to_constants(

sess, # The session is used to retrieve the weights

tf.get_default_graph().as_graph_def(), # The graph_def is used to retrieve the nodes

output_node_names.split(“,”) # The output node names are used to select the usefull nodes

)

 

# We generate the inference graph_def

output_graph_def = optimize_for_inference_lib.optimize_for_inference(tf.graph_util.remove_training_nodes(input_graph_def),

[‘Const_1’], # an array of the input node(s)

output_node_names.split(“,”), # an array of output nodes

tf.float32.as_datatype_enum)

# Finally we serialize and dump the output graph_def to the filesystem

with tf.gfile.GFile(‘frozen_model.pb’, “wb”) as f:

f.write(output_graph_def.SerializeToString())

print(“tf magic!”)

 

저희는 tf.graph_util.convert_variables_to_constants API를 사용하여 그래프를 고정한 다음 tf.graph_util.remove_training_nodes를 사용하여 불필요한 노드를 제거합니다. 그런 다음 optimize_for_inference_lib.optimize_for_inference를 사용하여 추론 그래프의_def를 생성합니다. 마지막으로, 파일을 직렬화하고 protobuff로 파일을 저장합니다.

freeze_graph(model_dir[-1], ‘softmax_tensor’)

 

저희는 입력 및 출력 노드의 이름(‘Const_1’ 및 ‘softmax_tensor’)을 미리 알고 있었습니다. TensorBoard를 사용하여 그래프를 보는 것이 중요합니다. 또한 그래프 내의 예기치 않은 노드/네임스페이스를 방지하기 위해 모델 스크립트(resnet_model_headpose.py)의 모든 계층에 이름을 지정하는 것도 좋습니다.

frozen protobuff 파일인 frozen_mode.pb가 생성된 후 S3 버킷의 TFartifacts 폴더 내의 출력 폴더에 다시 넣습니다.

data = open(‘frozen_model.pb’, “rb”)

key = ‘{}/{}/{}/output/frozen_model.pb’.format(headpose_folder, model_artifacts_folder,estimator.latest_training_job.name)

s3.Bucket(s3_bucket).put_object(Key=key, Body=data)

 

S3 버킷에 frozen_model.pb가 있습니다.

 

 

모델을 AWS DeepLens 장치에 즉시 배포할 수 있습니다.

이제 모델이 준비되었습니다. AWS DeepLens를 사용하려면 AWS Lamda 함수가 필요합니다. Lamda 함수는 AWS DeepLens의 샘플 프로젝트 중 하나와 함께 제공됩니다.

AWS DeepLens에 대한 Lambda 함수를 직접 작성하는 방법을 알아보려면 이 블로그를 살펴보십시오.

 

결론

이 글에서는 Amazon SageMaker에서 TensorFlow에서 머리-자세 추정기 ResNet-50 모델을 학습했습니다. 그런 다음 AWS DeepLens 장치에 배포할 수 있도록 교육을 받은 모델 아티팩트 파일을 처리했습니다. 이제 Amazon SageMaker에서 TensorFlow를 사용하여 고유한 AWS DeepLens 모델을 개발할 수 있습니다.

 

원문 URL: https://aws.amazon.com/ko/blogs/machine-learning/deploy-a-tensorflow-trained-image-classification-model-to-aws-deeplens/

** 메가존 TechBlog는 AWS BLOG 영문 게재글중에서 한국 사용자들에게 유용한 정보 및 콘텐츠를 우선적으로 번역하여 내부 엔지니어 검수를 받아서, 정기적으로 게재하고 있습니다. 추가로 번역및 게재를 희망하는 글에 대해서 관리자에게 메일 또는 SNS페이지에 댓글을 남겨주시면, 우선적으로 번역해서 전달해드리도록 하겠습니다.