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.
Because it was cheap on Prime Day, spare PC
& mdash; Kenji Fujimoto (Gachimoto) @XRKaigi (@sotongshi) October 18, 2020
[Basic specifications]
OS: Windows 10 Home 64bit, CPU : Intel Core i7-9750H, GPU: NVIDIA GeForce GTX 1650 Ti Max-Q Design 4GB GDDR5, Memory: 16GB (8GB x 2), SSD: 512GB, LCD Panel: 15.6 inch, Full HD (1,920 x 1,080), Refresh Rate 120Hz pic.twitter.com/S8RIsayFPY
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.
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 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...
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...
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) |
---|---|
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.
Next is the result of inferring a normal image. The dashboard will be updated soon.
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...
I hosted the model I made last time using boto3 and tried to infer it. It seems that it can be operated with this.
Recommended Posts