LoginSignup
1
2

More than 3 years have passed since last update.

Amazon Lookout for Visionで異常検出 その2(Python3.6)

Posted at

はじめに

Amazon AI by ナレコム アドベントカレンダー 2020の20日目の記事です。

前回のAmazon Lookout for Visionで異常検出 その1では、AWSコンソール上でAmazon Lookout for Visionの異常検出プロジェクトを作成し、学習とトライアル検出を行いました。
今回は、boto3を用いて作成したモデルをホスティングし、推論を行ってみましょう。

開発環境

  • AWS CLI 2.1.13
  • boto3 1.16.40
  • Python 3.6
  • Windows PC

導入

IAMユーザーの作成、AWS CLI & SDKs の設定、AmazonLookoutVisionFullAccessのアクセス権限を与えます。AmazonLookoutVisionFullAccessポリシーが見つからなかったので、作成しました。

image.png

AWS CLIとboto3を更新しておきましょう。

Windows での AWS CLI バージョン 2 のインストール、更新、アンインストール

pip install -U boto3

Start

作成したモデルを開始します。プロジェクト名(test)とモデルのバージョン(1)を指定します。

start_model.py
#Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

import boto3

def start_model(project_name, model_version, min_inference_units):

    client=boto3.client('lookoutvision')

    try:
        # Start the model
        print('Starting model version ' + model_version  + ' for project ' + project_name )
        response=client.start_model(ProjectName=project_name,
            ModelVersion=model_version,
            MinInferenceUnits=min_inference_units)
        print('Status: ' + response['Status'])


    except Exception as e:
        print(e)

    print('Done...')

def main():
    project='test'
    model_version='1'
    min_inference_units=1 
    start_model(project, model_version, min_inference_units)

if __name__ == "__main__":
    main()    

状態が「STARTING_HOSTING」となります。

$ python start_model.py
Starting model version 1 for project test
Status: STARTING_HOSTING
Done...

Describe

モデルを開始したら、状態を確認しましょう。

describe_model.py
#Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

import boto3
import json

def describe_models(project_name):

    client=boto3.client('lookoutvision')

    try:
        #list models
        response=client.list_models(ProjectName=project_name)
        print('Project: ' + project_name)
        for model in response['Models']:
            print('Version: ' + model['ModelVersion'])
            print('ARN: ' + model['ModelArn'])
            if 'Description' in model:
                print('Description: ' + model['Description'])
            print('Status: ' + model['Status'])
            print('Status Message: ' + model['StatusMessage'])

            # get model description
            model_description=client.describe_model(ProjectName=project_name,ModelVersion=model['ModelVersion'])
            print('Status: ' + model_description['ModelDescription']['Status'])
            print('Message: ' + model_description['ModelDescription']['StatusMessage'])

            if 'Performance' in model_description['ModelDescription']:
                print('Recall: ' + str(model_description['ModelDescription']['Performance']['Recall']))
                print('Precision: ' + str(model_description['ModelDescription']['Performance']['Precision']))

            if 'OutputConfig' in model_description['ModelDescription']:
                print('Output config: ' + str(model_description['ModelDescription']['OutputConfig']))
            print()

        print('Done...')

    except Exception as e:
        print(e)        

def main():
    project_name='test'
    describe_models(project_name)

if __name__ == "__main__":
    main()

状態が「STARTING_HOSTING」となっています。

$ python describe_model.py
Project: test
Version: 1
ARN: arn:aws:lookoutvision:us-east-1:xxxxxxxxxxxx:model/test/1
Status: STARTING_HOSTING
Status Message: Hosting starting.
Status: STARTING_HOSTING
Message: Hosting starting.
Recall: 0.9090909361839294
Precision: 0.9090909361839294
Output config: {'S3Location': {'Bucket': 'lookoutvision-us-east-1-xxxxxxxxxx', 'Prefix': 'project/test/model/'}}

Done...

「HOSTED」になれば使用可能です。

Project: test
Version: 1
ARN: arn:aws:lookoutvision:us-east-1:xxxxxxxxxxxx:model/test/1
Status: HOSTED
Status Message: The model is running
Status: HOSTED
Message: The model is running
Recall: 0.9090909361839294
Precision: 0.9090909361839294
Output config: {'S3Location': {'Bucket': 'lookoutvision-us-east-1-xxxxxxxxxx', 'Prefix': 'project/test/model/'}}

Done...

もしモデルが停止していた場合は、「TRAINED」となっています。

$ python describe_model.py
Project: test
Version: 1
ARN: arn:aws:lookoutvision:us-east-1:xxxxxxxxxxxx:model/test/1
Status: TRAINED
Status Message: The model is ready for hosting
Status: TRAINED
Message: The model is ready for hosting
Recall: 0.9090909361839294
Precision: 0.9090909361839294
Output config: {'S3Location': {'Bucket': 'lookoutvision-us-east-1-xxxxxxxxxx', 'Prefix': 'project/test/model/'}}

Done...

Detect

モデルの状態が「HOSTED」になったら、推論してみましょう。これらの画像をテストしました。

異常(capsule/test/squeeze/001.png) 正常(capsule/test/good/022.png)
000.png 022.png
detect.py
#Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

import boto3

def detect_anomalies(project_name,model_version,photo):


    client=boto3.client('lookoutvision')

    #Call DetectAnomalies 

    with open(photo, 'rb') as image:
        response = client.detect_anomalies(ProjectName=project_name, 
        # ContentType='image/jpeg',
        ContentType='image/png',
        Body=image.read(),
        ModelVersion=model_version)
    print ('Anomalous?: ' + str(response['DetectAnomalyResult']['IsAnomalous']))
    print ('Confidence: ' + str(response['DetectAnomalyResult']['Confidence']))


def main():
    project_name='test'
    photo="capsule/test/squeeze/000.png"
    model_version='1'

    anomalous=detect_anomalies(project_name,model_version,photo)


if __name__ == "__main__":
    main()

正しい予測結果が出ています。

$ python detect.py
Anomalous?: True
Confidence: 0.9995505213737488
$ python detect.py
Anomalous?: False
Confidence: 0.9181729555130005

モデルが「STARTING_HOSTING」の状態のときに推論した場合、以下のようなエラーが出ます。

botocore.errorfactory.ConflictException: An error occurred (ConflictException) when calling the DetectAnomalies operation: Detect cannot be performed when the resource is in STARTING_HOSTING. The resource must be in HOSTED to perform this action

また、AWSコンソールのLookout for Visionのダッシュボードで検出結果を見ることができます。

最初に異常画像を推論した結果です。

image.png

次に正常画像を推論した結果です。ダッシュボードはすぐに更新されます。

image.png

Stop

モデルを停止します。停止するまで料金かかるので注意しましょう。

stop_model.py
#Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

import boto3

def stop_model(project_name, model_version):

    client=boto3.client('lookoutvision')


    try:
        # Stop the model
        print('Stopping model version ' + model_version  + ' for project ' + project_name )
        response=client.stop_model(ProjectName=project_name,
            ModelVersion=model_version)
        print('Status: ' + response['Status'])


    except Exception as e:
        print(e)

    print('Done...')

def main():
    project='test'
    model_version='1'
    stop_model(project, model_version)

if __name__ == "__main__":
    main()
$ python stop_model.py
Stopping model version 1 for project test
Status: STOPPING_HOSTING
Done...

まとめ

前回作ったモデルをboto3を用いてホスティングし、推論してみました。これで運用できそうです。

参考

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2