Anomaly detection with Amazon Lookout for Vision Part 2 (Python3.6)

Introduction

This is the article on the 20th day of Amazon AI by Narecom Advent Calendar 2020.

In the previous Amazon Lookout for Vision Abnormality Detection Part 1, we created an Amazon Lookout for Vision anomaly detection project on the AWS console, and performed learning and trial detection. This time, let's host a model created using boto3 and make inferences.

Development environment

Introduction

Create an IAM user, grant AWS CLI & SDKs settings (https://docs.aws.amazon.com/lookout-for-vision/latest/developer-guide/su-awscli-sdk.html), and AmazonLookoutVisionFullAccess permissions. The AmazonLookoutVisionFullAccess policy was not found, so I created it.

image.png

Let's update the AWS CLI and boto3.

Install, update, and uninstall AWS CLI version 2 on Windows (https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/install-cliv2-windows.html#cliv2-windows-install)

pip install -U boto3

Start

Start the model you created. Specify the project name (test) and model version (1).

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()    

The status will be "STARTING_HOSTING".

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

Describe

After starting the model, let's check the status.

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()

The status is "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...

It can be used when it becomes "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...

If the model is stopped, it will be "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

When the model status is "HOSTED", let's infer. I tested these images.

Abnormal (capsule)/test/squeeze/001.png) Normal (capsule/test/good/022.png)
000.png 022.png
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()

The correct prediction result is coming out.

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

If you infer when the model is in the "STARTING_HOSTING" state, you will get the following error:

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

You can also see the detection results in the Lookout for Vision dashboard in the AWS console.

This is the result of inferring the abnormal image first.

image.png

Next is the result of inferring a normal image. The dashboard will be updated soon.

image.png

Stop

Stop the model. Please note that it will cost you until it stops.

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...

Summary

I hosted the model I made last time using boto3 and tried to infer it. It seems that it can be operated with this.

reference

Recommended Posts

Anomaly detection with Amazon Lookout for Vision Part 2 (Python3.6)
Image processing with Python (Part 2)
Studying Python with freeCodeCamp part1
Bordering images with python Part 1
Face detection with Python + dlib
Studying Python with freeCodeCamp part2
Image processing with Python (Part 1)
Image processing with Python (Part 3)
Face detection with Python + OpenCV
Scraping with Selenium + Python Part 2
Blockchain tampering detection with Python
Playing handwritten numbers with python Part 1
[Automation with python! ] Part 1: Setting file
Face detection with Lambda (Python) + Rekognition
[Python] Using OpenCV with Python (Edge Detection)
Automate simple tasks with Python Part0
[Automation with python! ] Part 2: File operation
Getting Started with Python for PHPer-Functions
Excel aggregation with Python pandas Part 1
Google Cloud Vision API sample for python
Face detection with Python + OpenCV (rotation invariant)
Homebrew Python Part 3-Amazon Product Search Program
INSERT into MySQL with Python [For beginners]
Building a Python3 environment with Amazon Linux2
WEB scraping with Python (for personal notes)
Manually ssh registration for coreserver with python
Play handwritten numbers with python Part 2 (identify)
FM modulation and demodulation with Python Part 3
Memo to ask for KPI with python
Amplify images for machine learning with python
Retrieving food data with Amazon API (Python)
Automate simple tasks with Python Part1 Scraping
Use Amazon Simple Notification Service with Python
Tips for dealing with binaries in Python
100 Language Processing Knock with Python (Chapter 2, Part 2)
Working with Azure CosmosDB from Python Part.2
Tips for using python + caffe with TSUBAME
Excel aggregation with Python pandas Part 2 Variadic
[Shakyo] Encounter with Python for machine learning
Process multiple lists with for in Python
Time series data anomaly detection for beginners
100 Language Processing Knock with Python (Chapter 2, Part 1)
Getting Started with Python for PHPer-Super Basics
Debug for mysql connection with python mysql.connector
FM modulation and demodulation with Python Part 2
[Python] Read images with OpenCV (for beginners)
[Part1] Scraping with Python → Organize to csv!
WebApi creation with Python (CRUD creation) For beginners
Send an email with Amazon SES + Python
Preparation for scraping with python [Chocolate flavor]
[For beginners] Try web scraping with Python
[Python] Real-time object detection with iPad camera
Data acquisition from analytics API with Google API Client for python Part 2 Web application
Analyze Amazon Gift Certificate Low Price Information with Python for Web Scraping & R
Hello World and face detection with OpenCV 4.3 + Python
Machine learning starting with Python Personal memorandum Part2
Create test data like that with Python (Part 1)
Causal reasoning and causal search with Python (for beginners)
Get a ticket for a theme park with python
Text extraction with GCP Cloud Vision API (Python3.6)
Machine learning starting with Python Personal memorandum Part1