Doc AWS SDK 예제 GitHub 리포지토리에서 더 많은 SDK 예제를 사용할 수 있습니다. AWS
기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
SDK for Python (Boto3)을 사용한 Lookout for Vision 예제
다음 코드 예제에서는 Lookout for Vision과 AWS SDK for Python (Boto3) 함께를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.
작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 관련 시나리오의 컨텍스트에 따라 표시되며, 개별 서비스 함수를 직접적으로 호출하는 방법을 보여줍니다.
시나리오는 동일한 서비스 내에서 또는 다른 AWS 서비스와 결합된 상태에서 여러 함수를 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예제입니다.
각 예시에는 전체 소스 코드에 대한 링크가 포함되어 있으며, 여기에서 컨텍스트에 맞춰 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있습니다.
시작
다음 코드 예제에서는 Lookout for Vision를 사용하여 시작하는 방법을 보여 줍니다.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. """ This example shows how to list your Amazon Lookout for Vision projects. If you haven't previously created a project in the current AWS Region, the response is an empty list, however it confirms that you can call the Lookout for Vision API. """ from botocore.exceptions import ClientError import boto3 class Hello: """Hello class for Amazon Lookout for Vision""" @staticmethod def list_projects(lookoutvision_client): """ Lists information about the projects that are in your AWS account and in the current AWS Region. : param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() for project in response["Projects"]: print("Project: " + project["ProjectName"]) print("ARN: " + project["ProjectArn"]) print() print("Done!") except ClientError as err: print(f"Couldn't list projects. \n{err}") raise def main(): session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") Hello.list_projects(lookoutvision_client) if __name__ == "__main__": main()
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 ListProjects를 참조하십시오.
-
작업
다음 코드 예시에서는 CreateDataset
을 사용하는 방법을 보여 줍니다.
자세한 내용은 데이터 세트 생성을 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Datasets: @staticmethod def create_dataset(lookoutvision_client, project_name, manifest_file, dataset_type): """ Creates a new Lookout for Vision dataset :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project_name: The name of the project in which you want to create a dataset. :param bucket: The bucket that contains the manifest file. :param manifest_file: The path and name of the manifest file. :param dataset_type: The type of the dataset (train or test). """ try: bucket, key = manifest_file.replace("s3://", "").split("/", 1) logger.info("Creating %s dataset type...", dataset_type) dataset = { "GroundTruthManifest": {"S3Object": {"Bucket": bucket, "Key": key}} } response = lookoutvision_client.create_dataset( ProjectName=project_name, DatasetType=dataset_type, DatasetSource=dataset, ) logger.info("Dataset Status: %s", response["DatasetMetadata"]["Status"]) logger.info( "Dataset Status Message: %s", response["DatasetMetadata"]["StatusMessage"], ) logger.info("Dataset Type: %s", response["DatasetMetadata"]["DatasetType"]) # Wait until either created or failed. finished = False status = "" dataset_description = {} while finished is False: dataset_description = lookoutvision_client.describe_dataset( ProjectName=project_name, DatasetType=dataset_type ) status = dataset_description["DatasetDescription"]["Status"] if status == "CREATE_IN_PROGRESS": logger.info("Dataset creation in progress...") time.sleep(2) elif status == "CREATE_COMPLETE": logger.info("Dataset created.") finished = True else: logger.info( "Dataset creation failed: %s", dataset_description["DatasetDescription"]["StatusMessage"], ) finished = True if status != "CREATE_COMPLETE": message = dataset_description["DatasetDescription"]["StatusMessage"] logger.exception("Couldn't create dataset: %s", message) raise Exception(f"Couldn't create dataset: {message}") except ClientError: logger.exception("Service error: Couldn't create dataset.") raise
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 CreateDataset를 참조하십시오.
-
다음 코드 예시에서는 CreateModel
을 사용하는 방법을 보여 줍니다.
자세한 내용은 모델 훈련을 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Models: @staticmethod def create_model( lookoutvision_client, project_name, training_results, tag_key=None, tag_key_value=None, ): """ Creates a version of a Lookout for Vision model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project in which you want to create a model. :param training_results: The Amazon S3 location where training results are stored. :param tag_key: The key for a tag to add to the model. :param tag_key_value - A value associated with the tag_key. return: The model status and version. """ try: logger.info("Training model...") output_bucket, output_folder = training_results.replace("s3://", "").split( "/", 1 ) output_config = { "S3Location": {"Bucket": output_bucket, "Prefix": output_folder} } tags = [] if tag_key is not None: tags = [{"Key": tag_key, "Value": tag_key_value}] response = lookoutvision_client.create_model( ProjectName=project_name, OutputConfig=output_config, Tags=tags ) logger.info("ARN: %s", response["ModelMetadata"]["ModelArn"]) logger.info("Version: %s", response["ModelMetadata"]["ModelVersion"]) logger.info("Started training...") print("Training started. Training might take several hours to complete.") # Wait until training completes. finished = False status = "UNKNOWN" while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=response["ModelMetadata"]["ModelVersion"], ) status = model_description["ModelDescription"]["Status"] if status == "TRAINING": logger.info("Model training in progress...") time.sleep(600) continue if status == "TRAINED": logger.info("Model was successfully trained.") else: logger.info( "Model training failed: %s ", model_description["ModelDescription"]["StatusMessage"], ) finished = True except ClientError: logger.exception("Couldn't train model.") raise else: return status, response["ModelMetadata"]["ModelVersion"]
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 CreateModel를 참조하십시오.
-
다음 코드 예시에서는 CreateProject
을 사용하는 방법을 보여 줍니다.
자세한 내용은 프로젝트 생성을 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Projects: @staticmethod def create_project(lookoutvision_client, project_name): """ Creates a new Lookout for Vision project. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name for the new project. :return project_arn: The ARN of the new project. """ try: logger.info("Creating project: %s", project_name) response = lookoutvision_client.create_project(ProjectName=project_name) project_arn = response["ProjectMetadata"]["ProjectArn"] logger.info("project ARN: %s", project_arn) except ClientError: logger.exception("Couldn't create project %s.", project_name) raise else: return project_arn
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 CreateProject를 참조하십시오.
-
다음 코드 예시에서는 DeleteDataset
을 사용하는 방법을 보여 줍니다.
자세한 내용은 데이터 세트 삭제를 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Datasets: @staticmethod def delete_dataset(lookoutvision_client, project_name, dataset_type): """ Deletes a Lookout for Vision dataset :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the dataset that you want to delete. :param dataset_type: The type (train or test) of the dataset that you want to delete. """ try: logger.info( "Deleting the %s dataset for project %s.", dataset_type, project_name ) lookoutvision_client.delete_dataset( ProjectName=project_name, DatasetType=dataset_type ) logger.info("Dataset deleted.") except ClientError: logger.exception("Service error: Couldn't delete dataset.") raise
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 DeleteDataset를 참조하십시오.
-
다음 코드 예시에서는 DeleteModel
을 사용하는 방법을 보여 줍니다.
자세한 내용은 모델 삭제를 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Models: @staticmethod def delete_model(lookoutvision_client, project_name, model_version): """ Deletes a Lookout for Vision model. The model must first be stopped and can't be in training. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the desired model. :param model_version: The version of the model that you want to delete. """ try: logger.info("Deleting model: %s", model_version) lookoutvision_client.delete_model( ProjectName=project_name, ModelVersion=model_version ) model_exists = True while model_exists: response = lookoutvision_client.list_models(ProjectName=project_name) model_exists = False for model in response["Models"]: if model["ModelVersion"] == model_version: model_exists = True if model_exists is False: logger.info("Model deleted") else: logger.info("Model is being deleted...") time.sleep(2) logger.info("Deleted Model: %s", model_version) except ClientError: logger.exception("Couldn't delete model.") raise
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 DeleteModel를 참조하십시오.
-
다음 코드 예시에서는 DeleteProject
을 사용하는 방법을 보여 줍니다.
자세한 내용은 프로젝트 삭제를 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Projects: @staticmethod def delete_project(lookoutvision_client, project_name): """ Deletes a Lookout for Vision Model :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that you want to delete. """ try: logger.info("Deleting project: %s", project_name) response = lookoutvision_client.delete_project(ProjectName=project_name) logger.info("Deleted project ARN: %s ", response["ProjectArn"]) except ClientError as err: logger.exception("Couldn't delete project %s.", project_name) raise
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 DeleteProject를 참조하십시오.
-
다음 코드 예시에서는 DescribeDataset
을 사용하는 방법을 보여 줍니다.
자세한 내용은 데이터 세트 보기를 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Datasets: @staticmethod def describe_dataset(lookoutvision_client, project_name, dataset_type): """ Gets information about a Lookout for Vision dataset. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the dataset that you want to describe. :param dataset_type: The type (train or test) of the dataset that you want to describe. """ try: response = lookoutvision_client.describe_dataset( ProjectName=project_name, DatasetType=dataset_type ) print(f"Name: {response['DatasetDescription']['ProjectName']}") print(f"Type: {response['DatasetDescription']['DatasetType']}") print(f"Status: {response['DatasetDescription']['Status']}") print(f"Message: {response['DatasetDescription']['StatusMessage']}") print(f"Images: {response['DatasetDescription']['ImageStats']['Total']}") print(f"Labeled: {response['DatasetDescription']['ImageStats']['Labeled']}") print(f"Normal: {response['DatasetDescription']['ImageStats']['Normal']}") print(f"Anomaly: {response['DatasetDescription']['ImageStats']['Anomaly']}") except ClientError: logger.exception("Service error: problem listing datasets.") raise print("Done.")
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 DescribeDataset를 참조하십시오.
-
다음 코드 예시에서는 DescribeModel
을 사용하는 방법을 보여 줍니다.
자세한 내용은 모델 보기를 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Models: @staticmethod def describe_model(lookoutvision_client, project_name, model_version): """ Shows the performance metrics for a trained model. :param lookoutvision_client: A Boto3 Amazon Lookout for Vision client. :param project_name: The name of the project that contains the desired model. :param model_version: The version of the model. """ response = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=model_version ) model_description = response["ModelDescription"] print(f"\tModel version: {model_description['ModelVersion']}") print(f"\tARN: {model_description['ModelArn']}") if "Description" in model_description: print(f"\tDescription: {model_description['Description']}") print(f"\tStatus: {model_description['Status']}") print(f"\tMessage: {model_description['StatusMessage']}") print(f"\tCreated: {str(model_description['CreationTimestamp'])}") if model_description["Status"] in ("TRAINED", "HOSTED"): training_start = model_description["CreationTimestamp"] training_end = model_description["EvaluationEndTimestamp"] duration = training_end - training_start print(f"\tTraining duration: {duration}") print("\n\tPerformance metrics\n\t-------------------") print(f"\tRecall: {model_description['Performance']['Recall']}") print(f"\tPrecision: {model_description['Performance']['Precision']}") print(f"\tF1: {model_description['Performance']['F1Score']}") training_output_bucket = model_description["OutputConfig"]["S3Location"][ "Bucket" ] prefix = model_description["OutputConfig"]["S3Location"]["Prefix"] print(f"\tTraining output: s3://{training_output_bucket}/{prefix}")
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 DescribeModel를 참조하십시오.
-
다음 코드 예시에서는 DetectAnomalies
을 사용하는 방법을 보여 줍니다.
자세한 내용은 이미지에서 이상 감지를 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Inference: """ Shows how to detect anomalies in an image using a trained Lookout for Vision model. """ @staticmethod def detect_anomalies(lookoutvision_client, project_name, model_version, photo): """ Calls DetectAnomalies using the supplied project, model version, and image. :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project: The project that contains the model that you want to use. :param model_version: The version of the model that you want to use. :param photo: The photo that you want to analyze. :return: The DetectAnomalyResult object that contains the analysis results. """ image_type = imghdr.what(photo) if image_type == "jpeg": content_type = "image/jpeg" elif image_type == "png": content_type = "image/png" else: logger.info("Image type not valid for %s", photo) raise ValueError( f"File format not valid. Supply a jpeg or png format file: {photo}" ) # Get images bytes for call to detect_anomalies. with open(photo, "rb") as image: response = lookoutvision_client.detect_anomalies( ProjectName=project_name, ContentType=content_type, Body=image.read(), ModelVersion=model_version, ) return response["DetectAnomalyResult"] @staticmethod def download_from_s3(s3_resource, photo): """ Downloads an image from an S3 bucket. :param s3_resource: A Boto3 Amazon S3 resource. :param photo: The Amazon S3 path of a photo to download. return: The local path to the downloaded file. """ try: bucket, key = photo.replace("s3://", "").split("/", 1) local_file = os.path.basename(photo) except ValueError: logger.exception("Couldn't get S3 info for %s", photo) raise try: logger.info("Downloading %s", photo) s3_resource.Bucket(bucket).download_file(key, local_file) except ClientError: logger.exception("Couldn't download %s from S3.", photo) raise return local_file @staticmethod def reject_on_classification(image, prediction, confidence_limit): """ Returns True if the anomaly confidence is greater than or equal to the supplied confidence limit. :param image: The name of the image file that was analyzed. :param prediction: The DetectAnomalyResult object returned from DetectAnomalies. :param confidence_limit: The minimum acceptable confidence (float 0 - 1). :return: True if the error condition indicates an anomaly, otherwise False. """ reject = False logger.info("Checking classification for %s", image) if prediction["IsAnomalous"] and prediction["Confidence"] >= confidence_limit: reject = True reject_info = ( f"Rejected: Anomaly confidence ({prediction['Confidence']:.2%}) is greater" f" than limit ({confidence_limit:.2%})" ) logger.info("%s", reject_info) if not reject: logger.info("No anomalies found.") return reject @staticmethod def reject_on_anomaly_types( image, prediction, confidence_limit, anomaly_types_limit ): """ Checks if the number of anomaly types is greater than the anomaly types limit and if the prediction confidence is greater than the confidence limit. :param image: The name of the image file that was analyzed. :param prediction: The DetectAnomalyResult object returned from DetectAnomalies. :param confidence: The minimum acceptable confidence (float 0 - 1). :param anomaly_types_limit: The maximum number of allowable anomaly types (int). :return: True if the error condition indicates an anomaly, otherwise False. """ logger.info("Checking number of anomaly types for %s", image) reject = False if prediction["IsAnomalous"] and prediction["Confidence"] >= confidence_limit: anomaly_types = { anomaly["Name"] for anomaly in prediction["Anomalies"] if anomaly["Name"] != "background" } if len(anomaly_types) > anomaly_types_limit: reject = True reject_info = ( f"Rejected: Anomaly confidence ({prediction['Confidence']:.2%}) " f"is greater than limit ({confidence_limit:.2%}) and " f"the number of anomaly types ({len(anomaly_types)-1}) is " f"greater than the limit ({anomaly_types_limit})" ) logger.info("%s", reject_info) if not reject: logger.info("No anomalies found.") return reject @staticmethod def reject_on_coverage( image, prediction, confidence_limit, anomaly_label, coverage_limit ): """ Checks if the coverage area of an anomaly is greater than the coverage limit and if the prediction confidence is greater than the confidence limit. :param image: The name of the image file that was analyzed. :param prediction: The DetectAnomalyResult object returned from DetectAnomalies. :param confidence_limit: The minimum acceptable confidence (float 0-1). :anomaly_label: The anomaly label for the type of anomaly that you want to check. :coverage_limit: The maximum acceptable percentage coverage of an anomaly (float 0-1). :return: True if the error condition indicates an anomaly, otherwise False. """ reject = False logger.info("Checking coverage for %s", image) if prediction["IsAnomalous"] and prediction["Confidence"] >= confidence_limit: for anomaly in prediction["Anomalies"]: if anomaly["Name"] == anomaly_label and anomaly["PixelAnomaly"][ "TotalPercentageArea" ] > (coverage_limit): reject = True reject_info = ( f"Rejected: Anomaly confidence ({prediction['Confidence']:.2%}) " f"is greater than limit ({confidence_limit:.2%}) and {anomaly['Name']} " f"coverage ({anomaly['PixelAnomaly']['TotalPercentageArea']:.2%}) " f"is greater than limit ({coverage_limit:.2%})" ) logger.info("%s", reject_info) if not reject: logger.info("No anomalies found.") return reject @staticmethod def analyze_image(lookoutvision_client, image, config): """ Analyzes an image with an Amazon Lookout for Vision model. Also runs a series of checks to determine if the contents of an image should be rejected. :param lookoutvision_client: A Lookout for Vision Boto3 client. param image: A local image that you want to analyze. param config: Configuration information for the model and reject limits. """ project = config["project"] model_version = config["model_version"] confidence_limit = config["confidence_limit"] coverage_limit = config["coverage_limit"] anomaly_types_limit = config["anomaly_types_limit"] anomaly_label = config["anomaly_label"] # Get analysis results. print(f"Analyzing {image}.") prediction = Inference.detect_anomalies( lookoutvision_client, project, model_version, image ) anomalies = [] reject = Inference.reject_on_classification(image, prediction, confidence_limit) if reject: anomalies.append("Classification: An anomaly was found.") reject = Inference.reject_on_coverage( image, prediction, confidence_limit, anomaly_label, coverage_limit ) if reject: anomalies.append("Coverage: Anomaly coverage too high.") reject = Inference.reject_on_anomaly_types( image, prediction, confidence_limit, anomaly_types_limit ) if reject: anomalies.append("Anomaly type count: Too many anomaly types found.") print() if len(anomalies) > 0: print(f"Anomalies found in {image}") for anomaly in anomalies: print(f"{anomaly}") else: print(f"No anomalies found in {image}") def main(): """ Detects anomalies in an image file. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser( description="Find anomalies with Amazon Lookout for Vision." ) parser.add_argument( "image", help="The file that you want to analyze. Supply a local file path or a " "path to an S3 object.", ) parser.add_argument( "config", help=( "The configuration JSON file to use. " "See https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/" "python/example_code/lookoutvision/README.md" ), ) args = parser.parse_args() session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") s3_resource = session.resource("s3") # Get configuration information. with open(args.config, encoding="utf-8") as config_file: config = json.load(config_file) # Download image if located in S3 bucket. if args.image.startswith("s3://"): image = Inference.download_from_s3(s3_resource, args.image) else: image = args.image Inference.analyze_image(lookoutvision_client, image, config) # Delete image, if downloaded from S3 bucket. if args.image.startswith("s3://"): os.remove(image) except ClientError as err: print(f"Service error: {err.response['Error']['Message']}") except FileNotFoundError as err: print(f"The supplied file couldn't be found: {err.filename}.") except ValueError as err: print(f"A value error occurred: {err}.") else: print("\nSuccessfully completed analysis.") if __name__ == "__main__": main()
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 DetectAnomalies를 참조하십시오.
-
다음 코드 예시에서는 ListModels
을 사용하는 방법을 보여 줍니다.
자세한 내용은 모델 보기를 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Models: @staticmethod def describe_models(lookoutvision_client, project_name): """ Gets information about all models in a Lookout for Vision project. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that you want to use. """ try: response = lookoutvision_client.list_models(ProjectName=project_name) print("Project: " + project_name) for model in response["Models"]: Models.describe_model( lookoutvision_client, project_name, model["ModelVersion"] ) print() print("Done...") except ClientError: logger.exception("Couldn't list models.") raise
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 ListModels를 참조하십시오.
-
다음 코드 예시에서는 ListProjects
을 사용하는 방법을 보여 줍니다.
자세한 내용은 프로젝트 보기를 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Projects: @staticmethod def list_projects(lookoutvision_client): """ Lists information about the projects that are in in your AWS account and in the current AWS Region. :param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() for project in response["Projects"]: print("Project: " + project["ProjectName"]) print("\tARN: " + project["ProjectArn"]) print("\tCreated: " + str(["CreationTimestamp"])) print("Datasets") project_description = lookoutvision_client.describe_project( ProjectName=project["ProjectName"] ) if not project_description["ProjectDescription"]["Datasets"]: print("\tNo datasets") else: for dataset in project_description["ProjectDescription"][ "Datasets" ]: print(f"\ttype: {dataset['DatasetType']}") print(f"\tStatus: {dataset['StatusMessage']}") print("Models") response_models = lookoutvision_client.list_models( ProjectName=project["ProjectName"] ) if not response_models["Models"]: print("\tNo models") else: for model in response_models["Models"]: Models.describe_model( lookoutvision_client, project["ProjectName"], model["ModelVersion"], ) print("------------------------------------------------------------\n") print("Done!") except ClientError: logger.exception("Problem listing projects.") raise
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 ListProjects를 참조하십시오.
-
다음 코드 예시에서는 StartModel
을 사용하는 방법을 보여 줍니다.
자세한 내용은 모델 시작하기를 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Hosting: @staticmethod def start_model( lookoutvision_client, project_name, model_version, min_inference_units ): """ Starts the hosting of a Lookout for Vision model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the version of the model that you want to start hosting. :param model_version: The version of the model that you want to start hosting. :param min_inference_units: The number of inference units to use for hosting. """ try: logger.info( "Starting model version %s for project %s", model_version, project_name ) lookoutvision_client.start_model( ProjectName=project_name, ModelVersion=model_version, MinInferenceUnits=min_inference_units, ) print("Starting hosting...") status = "" finished = False # Wait until hosted or failed. while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=model_version ) status = model_description["ModelDescription"]["Status"] if status == "STARTING_HOSTING": logger.info("Host starting in progress...") time.sleep(10) continue if status == "HOSTED": logger.info("Model is hosted and ready for use.") finished = True continue logger.info("Model hosting failed and the model can't be used.") finished = True if status != "HOSTED": logger.error("Error hosting model: %s", status) raise Exception(f"Error hosting model: {status}") except ClientError: logger.exception("Couldn't host model.") raise
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 StartModel를 참조하십시오.
-
다음 코드 예시에서는 StopModel
을 사용하는 방법을 보여 줍니다.
자세한 내용은 모델 시작하기를 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Hosting: @staticmethod def stop_model(lookoutvision_client, project_name, model_version): """ Stops a running Lookout for Vision Model. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param project_name: The name of the project that contains the version of the model that you want to stop hosting. :param model_version: The version of the model that you want to stop hosting. """ try: logger.info("Stopping model version %s for %s", model_version, project_name) response = lookoutvision_client.stop_model( ProjectName=project_name, ModelVersion=model_version ) logger.info("Stopping hosting...") status = response["Status"] finished = False # Wait until stopped or failed. while finished is False: model_description = lookoutvision_client.describe_model( ProjectName=project_name, ModelVersion=model_version ) status = model_description["ModelDescription"]["Status"] if status == "STOPPING_HOSTING": logger.info("Host stopping in progress...") time.sleep(10) continue if status == "TRAINED": logger.info("Model is no longer hosted.") finished = True continue logger.info("Failed to stop model: %s ", status) finished = True if status != "TRAINED": logger.error("Error stopping model: %s", status) raise Exception(f"Error stopping model: {status}") except ClientError: logger.exception("Couldn't stop hosting model.") raise
-
API 세부 정보는 AWS SDK for Python (Boto3) API 참조의 StopModel를 참조하십시오.
-
시나리오
다음 코드 예제에서는 Lookout for Vision 매니페스트 파일을 생성하고 Amazon S3에 업로드하는 방법을 보여줍니다.
자세한 내용은 매니페스트 파일 생성을 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Datasets: @staticmethod def create_manifest_file_s3(s3_resource, image_s3_path, manifest_s3_path): """ Creates a manifest file and uploads to Amazon S3. :param s3_resource: A Boto3 Amazon S3 resource. :param image_s3_path: The Amazon S3 path to the images referenced by the manifest file. The images must be in an Amazon S3 bucket with the following folder structure. s3://amzn-s3-demo-bucket/<train or test>/ normal/ anomaly/ Place normal images in the normal folder and anomalous images in the anomaly folder. :param manifest_s3_path: The Amazon S3 location in which to store the created manifest file. """ output_manifest_file = "temp.manifest" try: # Current date and time in manifest file format. dttm = datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f") # Get bucket and folder from image and manifest file paths. bucket, prefix = image_s3_path.replace("s3://", "").split("/", 1) if prefix[-1] != "/": prefix += "/" manifest_bucket, manifest_prefix = manifest_s3_path.replace( "s3://", "" ).split("/", 1) with open(output_manifest_file, "w") as mfile: logger.info("Creating manifest file") src_bucket = s3_resource.Bucket(bucket) # Create JSON lines for anomalous images. for obj in src_bucket.objects.filter( Prefix=prefix + "anomaly/", Delimiter="/" ): image_path = f"s3://{src_bucket.name}/{obj.key}" manifest = Datasets.create_json_line(image_path, "anomaly", dttm) mfile.write(json.dumps(manifest) + "\n") # Create json lines for normal images. for obj in src_bucket.objects.filter( Prefix=prefix + "normal/", Delimiter="/" ): image_path = f"s3://{src_bucket.name}/{obj.key}" manifest = Datasets.create_json_line(image_path, "normal", dttm) mfile.write(json.dumps(manifest) + "\n") logger.info("Uploading manifest file to %s", manifest_s3_path) s3_resource.Bucket(manifest_bucket).upload_file( output_manifest_file, manifest_prefix ) except ClientError: logger.exception("Error uploading manifest.") raise except Exception: logger.exception("Error uploading manifest.") raise else: logger.info("Completed manifest file creation and upload.") finally: try: os.remove(output_manifest_file) except FileNotFoundError: pass @staticmethod def create_json_line(image, class_name, dttm): """ Creates a single JSON line for an image. :param image: The S3 location for the image. :param class_name: The class of the image (normal or anomaly) :param dttm: The date and time that the JSON is created. """ label = 0 if class_name == "normal": label = 0 elif class_name == "anomaly": label = 1 else: logger.error("Unexpected label value: %s for %s", label, image) raise Exception(f"Unexpected label value: {label} for {image}") manifest = { "source-ref": image, "anomaly-label": label, "anomaly-label-metadata": { "confidence": 1, "job-name": "labeling-job/anomaly-label", "class-name": class_name, "human-annotated": "yes", "creation-date": dttm, "type": "groundtruth/image-classification", }, } return manifest
다음 코드 예제에서는 Lookout for Vision 모델을 생성하고 학습시키고 생성하는 방법을 보여줍니다.
- SDK for Python (Boto3)
-
명령줄 인수를 사용하여 Amazon Lookout for Vision 모델을 생성하고 선택적으로 시작합니다. 예제 코드는 새 프로젝트, 교육 데이터 세트, 선택적 테스트 데이터 세트 및 모델을 생성합니다. 모델 학습이 완료되면 제공된 스크립트를 사용하여 이미지로 모델을 시험해 볼 수 있습니다.
이 예제에는 모델을 학습시키기 위한 이미지 세트가 필요합니다. GitHub에서 교육 및 테스트에 사용할 수 있는 예제 회로 기판 이미지를 찾을 수 있습니다. Amazon Simple Storage Service (Amazon S3) 버킷에 이러한 이미지를 복사하는 방법에 대한 자세한 내용은 예제 이미지 준비를 참조하십시오.
전체 소스 코드와 설정 및 실행 방법에 대한 지침은 GitHub
에서 전체 예제를 참조하십시오. 이 예시에서 사용되는 서비스
Lookout for Vision
다음 코드 예제에서는 Lookout for Vision 프로젝트에서 데이터 세트를 내보내는 방법을 보여줍니다.
자세한 내용은 프로젝트에서 데이터세트 내보내기 (SDK) 를 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. """ Purpose Shows how to export the datasets (manifest files and images) from an Amazon Lookout for Vision project to a new Amazon S3 location. """ import argparse import json import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def copy_file(s3_resource, source_file, destination_file): """ Copies a file from a source Amazon S3 folder to a destination Amazon S3 folder. The destination can be in a different S3 bucket. :param s3: An Amazon S3 Boto3 resource. :param source_file: The Amazon S3 path to the source file. :param destination_file: The destination Amazon S3 path for the copy operation. """ source_bucket, source_key = source_file.replace("s3://", "").split("/", 1) destination_bucket, destination_key = destination_file.replace("s3://", "").split( "/", 1 ) try: bucket = s3_resource.Bucket(destination_bucket) dest_object = bucket.Object(destination_key) dest_object.copy_from(CopySource={"Bucket": source_bucket, "Key": source_key}) dest_object.wait_until_exists() logger.info("Copied %s to %s", source_file, destination_file) except ClientError as error: if error.response["Error"]["Code"] == "404": error_message = ( f"Failed to copy {source_file} to " f"{destination_file}. : {error.response['Error']['Message']}" ) logger.warning(error_message) error.response["Error"]["Message"] = error_message raise def upload_manifest_file(s3_resource, manifest_file, destination): """ Uploads a manifest file to a destination Amazon S3 folder. :param s3: An Amazon S3 Boto3 resource. :param manifest_file: The manifest file that you want to upload. :destination: The Amazon S3 folder location to upload the manifest file to. """ destination_bucket, destination_key = destination.replace("s3://", "").split("/", 1) bucket = s3_resource.Bucket(destination_bucket) put_data = open(manifest_file, "rb") obj = bucket.Object(destination_key + manifest_file) try: obj.put(Body=put_data) obj.wait_until_exists() logger.info("Put manifest file '%s' to bucket '%s'.", obj.key, obj.bucket_name) except ClientError: logger.exception( "Couldn't put manifest file '%s' to bucket '%s'.", obj.key, obj.bucket_name ) raise finally: if getattr(put_data, "close", None): put_data.close() def get_dataset_types(lookoutvision_client, project): """ Determines the types of the datasets (train or test) in an Amazon Lookout for Vision project. :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project: The Lookout for Vision project that you want to check. :return: The dataset types in the project. """ try: response = lookoutvision_client.describe_project(ProjectName=project) datasets = [] for dataset in response["ProjectDescription"]["Datasets"]: if dataset["Status"] in ("CREATE_COMPLETE", "UPDATE_COMPLETE"): datasets.append(dataset["DatasetType"]) return datasets except lookoutvision_client.exceptions.ResourceNotFoundException: logger.exception("Project %s not found.", project) raise def process_json_line(s3_resource, entry, dataset_type, destination): """ Creates a JSON line for a new manifest file, copies image and mask to destination. :param s3_resource: An Amazon S3 Boto3 resource. :param entry: A JSON line from the manifest file. :param dataset_type: The type (train or test) of the dataset that you want to create the manifest file for. :param destination: The destination Amazon S3 folder for the manifest file and dataset images. :return: A JSON line with details for the destination location. """ entry_json = json.loads(entry) print(f"source: {entry_json['source-ref']}") # Use existing folder paths to ensure console added image names don't clash. bucket, key = entry_json["source-ref"].replace("s3://", "").split("/", 1) logger.info("Source location: %s/%s", bucket, key) destination_image_location = destination + dataset_type + "/images/" + key copy_file(s3_resource, entry_json["source-ref"], destination_image_location) # Update JSON for writing. entry_json["source-ref"] = destination_image_location if "anomaly-mask-ref" in entry_json: source_anomaly_ref = entry_json["anomaly-mask-ref"] mask_bucket, mask_key = source_anomaly_ref.replace("s3://", "").split("/", 1) destination_mask_location = destination + dataset_type + "/masks/" + mask_key entry_json["anomaly-mask-ref"] = destination_mask_location copy_file(s3_resource, source_anomaly_ref, entry_json["anomaly-mask-ref"]) return entry_json def write_manifest_file( lookoutvision_client, s3_resource, project, dataset_type, destination ): """ Creates a manifest file for a dataset. Copies the manifest file and dataset images (and masks, if present) to the specified Amazon S3 destination. :param lookoutvision_client: A Lookout for Vision Boto3 client. :param project: The Lookout for Vision project that you want to use. :param dataset_type: The type (train or test) of the dataset that you want to create the manifest file for. :param destination: The destination Amazon S3 folder for the manifest file and dataset images. """ try: # Create a reusable Paginator paginator = lookoutvision_client.get_paginator("list_dataset_entries") # Create a PageIterator from the Paginator page_iterator = paginator.paginate( ProjectName=project, DatasetType=dataset_type, PaginationConfig={"PageSize": 100}, ) output_manifest_file = dataset_type + ".manifest" # Create manifest file then upload to Amazon S3 with images. with open(output_manifest_file, "w", encoding="utf-8") as manifest_file: for page in page_iterator: for entry in page["DatasetEntries"]: try: entry_json = process_json_line( s3_resource, entry, dataset_type, destination ) manifest_file.write(json.dumps(entry_json) + "\n") except ClientError as error: if error.response["Error"]["Code"] == "404": print(error.response["Error"]["Message"]) print(f"Excluded JSON line: {entry}") else: raise upload_manifest_file( s3_resource, output_manifest_file, destination + "datasets/" ) except ClientError: logger.exception("Problem getting dataset_entries") raise def export_datasets(lookoutvision_client, s3_resource, project, destination): """ Exports the datasets from an Amazon Lookout for Vision project to a specified Amazon S3 destination. :param project: The Lookout for Vision project that you want to use. :param destination: The destination Amazon S3 folder for the exported datasets. """ # Add trailing backslash, if missing. destination = destination if destination[-1] == "/" else destination + "/" print(f"Exporting project {project} datasets to {destination}.") # Get each dataset and export to destination. dataset_types = get_dataset_types(lookoutvision_client, project) for dataset in dataset_types: logger.info("Copying %s dataset to %s.", dataset, destination) write_manifest_file( lookoutvision_client, s3_resource, project, dataset, destination ) print("Exported dataset locations") for dataset in dataset_types: print(f" {dataset}: {destination}datasets/{dataset}.manifest") print("Done.") def add_arguments(parser): """ Adds command line arguments to the parser. :param parser: The command line parser. """ parser.add_argument("project", help="The project that contains the dataset.") parser.add_argument("destination", help="The destination Amazon S3 folder.") def main(): """ Exports the datasets from an Amazon Lookout for Vision project to a destination Amazon S3 location. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser(usage=argparse.SUPPRESS) add_arguments(parser) args = parser.parse_args() try: session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") s3_resource = session.resource("s3") export_datasets( lookoutvision_client, s3_resource, args.project, args.destination ) except ClientError as err: logger.exception(err) print(f"Failed: {format(err)}") if __name__ == "__main__": main()
다음 코드 예제에서는 특정 태그가 지정된 Lookout for Vision 프로젝트를 찾는 방법을 보여줍니다.
자세한 내용은 모델 태그 지정을 참조하십시오.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import logging import argparse import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def find_tag(tags, key, value): """ Finds a tag in the supplied list of tags. :param tags: A list of tags associated with a Lookout for Vision model. :param key: The tag to search for. :param value: The tag key value to search for. :return: True if the tag value exists, otherwise False. """ found = False for tag in tags: if key == tag["Key"]: logger.info("\t\tMatch found for tag: %s value: %s.", key, value) found = True break return found def find_tag_in_projects(lookoutvision_client, key, value): """ Finds Lookout for Vision models tagged with the supplied key and value. :param lookoutvision_client: A Boto3 Lookout for Vision client. :param key: The tag key to find. :param value: The value of the tag that you want to find. return: A list of matching model versions (and model projects) that were found. """ try: found_tags = [] found = False projects = lookoutvision_client.list_projects() # Iterate through each project and models within a project. for project in projects["Projects"]: logger.info("Searching project: %s ...", project["ProjectName"]) response_models = lookoutvision_client.list_models( ProjectName=project["ProjectName"] ) for model in response_models["Models"]: model_description = lookoutvision_client.describe_model( ProjectName=project["ProjectName"], ModelVersion=model["ModelVersion"], ) tags = lookoutvision_client.list_tags_for_resource( ResourceArn=model_description["ModelDescription"]["ModelArn"] ) logger.info( "\tSearching model: %s for tag: %s value: %s.", model_description["ModelDescription"]["ModelArn"], key, value, ) if find_tag(tags["Tags"], key, value) is True: found = True logger.info( "\t\tMATCH: Project: %s: model version %s", project["ProjectName"], model_description["ModelDescription"]["ModelVersion"], ) found_tags.append( { "Project": project["ProjectName"], "ModelVersion": model_description["ModelDescription"][ "ModelVersion" ], } ) if found is False: logger.info("No match for tag %s with value %s.", key, value) except ClientError: logger.exception("Problem finding tags.") raise else: return found_tags def main(): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser(usage=argparse.SUPPRESS) parser.add_argument("tag", help="The tag that you want to find.") parser.add_argument("value", help="The tag value that you want to find.") args = parser.parse_args() key = args.tag value = args.value session = boto3.Session(profile_name="lookoutvision-access") lookoutvision_client = session.client("lookoutvision") print(f"Searching your models for tag: {key} with value: {value}.") tagged_models = find_tag_in_projects(lookoutvision_client, key, value) print("Matched models\n--------------") if len(tagged_models) > 0: for model in tagged_models: print(f"Project: {model['Project']}. model version:{model['ModelVersion']}") else: print("No matches found.") if __name__ == "__main__": main()
다음 코드 예제에서는 현재 호스팅되고 있는 Lookout for Vision 모델을 나열하는 방법을 보여줍니다.
- SDK for Python (Boto3)
-
참고
GitHub에 더 많은 내용이 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. class Hosting: @staticmethod def list_hosted(lookoutvision_client): """ Displays a list of models in your account that are currently hosted. :param lookoutvision_client: A Boto3 Lookout for Vision client. """ try: response = lookoutvision_client.list_projects() hosted = 0 print("Hosted models\n-------------") for project in response["Projects"]: response_models = lookoutvision_client.list_models( ProjectName=project["ProjectName"] ) for model in response_models["Models"]: model_description = lookoutvision_client.describe_model( ProjectName=project["ProjectName"], ModelVersion=model["ModelVersion"], ) if model_description["ModelDescription"]["Status"] == "HOSTED": print( f"Project: {project['ProjectName']} Model version: " f"{model['ModelVersion']}" ) hosted += 1 print(f"{hosted} model(s) hosted") except ClientError: logger.exception("Problem listing hosted models.") raise