<jupyter_start><jupyter_text>Compile Tensorflow Object Detection model for Jetson Nano  Download Model<jupyter_code>!mkdir -p posenet
!wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/posenet_mobilenet_v1_100_257x257_multi_kpt_stripped.tflite -O posenet/posenet_mobile.tflite
model_filename = 'posenet/posenet_mobile.tflite'
model_name = 'posenet-mobile'

# Compress model into .tar.gz so SageMaker Neo can use it
model_tar = model_name + '.tar.gz'
!tar -czf {model_tar} {model_filename}<jupyter_output><empty_output><jupyter_text>Upload Model to S3<jupyter_code>import sagemaker
from sagemaker import get_execution_role
import boto3
import botocore
import json
role = get_execution_role()
sess = sagemaker.Session()
region = 'ap-northeast-1'
# S3 bucket and folders for saving model artifacts.
# Feel free to specify different bucket/folders here if you wish.
bucket = '<your bucket here>'
folder = 'edge-cv-datalake-demo/models/pose-estimator'
training_output_folder = folder + '/training-output'
compilation_output_sub_folder = folder + '/compilation-output'
s3_packaging_output_location = folder + '/packaging-output'

# S3 Location to save the model artifact after compilation
s3_training_output_location = 's3://{}/{}'.format(bucket, training_output_folder)
s3_compilation_output_location = 's3://{}/{}'.format(bucket, compilation_output_sub_folder)
s3_packaging_output_location = 's3://{}/{}'.format(bucket, s3_packaging_output_location)
print(s3_compilation_output_location)
print(s3_packaging_output_location)
sagemaker_client = boto3.client('sagemaker', region_name=region)
pose_estimator_s3_path = sess.upload_data(model_tar, bucket, training_output_folder)
## Update version
pose_estimator_model_version = '0.1.0'<jupyter_output><empty_output><jupyter_text>Start a Neo Compilation Job<jupyter_code>import time

def compile_model(model_type, model_path, model_framework, model_data_shape, target_device, compiler_options):
    compilation_job_name = 'edge-cv-datalake-demo-'+ model_type + '-' + pose_estimator_model_version.replace('.', '-') + '-' + target_device.replace('_', '-')
    print('Compilation job for %s started' % compilation_job_name)

    response = sagemaker_client.create_compilation_job(
            CompilationJobName=compilation_job_name,
            RoleArn=role,
            InputConfig={
                'S3Uri': model_path,
                'DataInputConfig': model_data_shape,
                'Framework': model_framework.upper()
            },
            OutputConfig={
                'S3OutputLocation': s3_compilation_output_location,
                'TargetDevice': target_device,
                'CompilerOptions': compiler_options
            },
            StoppingCondition={
                'MaxRuntimeInSeconds': 1800
            }
        )

    print(response)

    # Poll every 30 sec
    while True:
        response = sagemaker_client.describe_compilation_job(CompilationJobName=compilation_job_name)
        if response['CompilationJobStatus'] == 'COMPLETED':
            break
        elif response['CompilationJobStatus'] == 'FAILED':
            raise RuntimeError('Compilation failed')
        print('Compiling ...')
        time.sleep(30)
    print('Done!')
    return compilation_job_name
def package_model(compilation_job_name, packaged_model_name, model_version):
    model_packaging_job_name=compilation_job_name
    response = sagemaker_client.create_edge_packaging_job(
        RoleArn=role,
        OutputConfig={
            'S3OutputLocation': s3_packaging_output_location,
        },
        ModelName=packaged_model_name,
        ModelVersion=model_version,
        EdgePackagingJobName=model_packaging_job_name,
        CompilationJobName=compilation_job_name,
    )

    print(response)

    # Poll every 30 sec
    while True:
        job_status = sagemaker_client.describe_edge_packaging_job(EdgePackagingJobName=model_packaging_job_name)
        if job_status['EdgePackagingJobStatus'] == 'COMPLETED':
            break
        elif job_status['EdgePackagingJobStatus'] == 'FAILED':
            raise RuntimeError('Edge Packaging failed')
        print('Packaging ...')
        time.sleep(30)
    print('Done!')
    return model_packaging_job_name
pose_estimator_model_data_shape = '{"sub_2":[1,257,257,3]}'

model_framework = 'tflite'
target_device = 'jetson_nano'

# Compile for JETPACK 4.5.1 on Jetson Nano 
compiler_options = json.dumps({
    'NVIDIA': {
        'gpu_code': 'sm_53',
        'trt-ver': '7.1.3',
        'cuda-ver': '10.2'
    }
})
pose_estimator_compilation_job_name = compile_model('pose-estimator', pose_estimator_s3_path, model_framework, pose_estimator_model_data_shape, target_device, compiler_options)
pose_estimator_packaged_model_name = "pose-estimator-model-packaged-nano"
pose_estimator_model_package = '{}-{}.tar.gz'.format(pose_estimator_packaged_model_name, pose_estimator_model_version.replace('.', '-'))
print(pose_estimator_model_package)
pose_estimator_packaging_job_name = package_model('edge-cv-datalake-demo-pose-estimator-0-1-0-jetson-nano', pose_estimator_packaged_model_name, pose_estimator_model_version)
pose_estimator_packaging_job_name = package_model(pose_estimator_compilation_job_name, pose_estimator_packaged_model_name, pose_estimator_model_version)
target_device = 'rasp3b'
compiler_options = '{"":""}'
pose_estimator_compilation_job_name = compile_model('pose-estimator', pose_estimator_s3_path, model_framework, pose_estimator_model_data_shape, target_device, compiler_options)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09906508450197771, 'fraction_numerical': 0.01312477526069759, 'mean_word_length': 4.641987829614605, 'pattern_counts': {'":': 2, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '505113', 'license': 'MIT', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1788, 'n_words': 282, 'path': 'cloud/sagemaker/neo-tflite-pose.ipynb', 'repo_name': 'gai6948/edgecv-datalake-for-pose-estimation', 'size': '5562'}