이미지가 변칙적인지 여부 확인 - Amazon Lookout for Vision

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

이미지가 변칙적인지 여부 확인

다양한 방법으로 이미지가 이상인지 보기 선택하는 방법은 사용 사례와 모델 유형에 따라 다릅니다. 잠재적 해결 방법은 다음과 같습니다.

분류

IsAnomalous은 이미지를 변칙으로 분류하고 Confidence 필드를 사용하여 이미지가 실제로 변칙적인지 판단할 수 있습니다. 값이 높을수록 신뢰도가 높아집니다. 예를 들어 신뢰도가 80% 를 초과하는 경우에만 제품에 결함이 있다고 판단할 수 있습니다. 분류 모델 또는 이미지 분할 모델로 분석된 이미지를 분류할 수 있습니다.

Python

전체 코드 예제는 GitHub를 참조하십시오.

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 value between 0 and 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
Java V2
public static boolean rejectOnClassification(String image, DetectAnomalyResult prediction, float minConfidence) { /** * Rejects an image based on its anomaly classification and prediction * confidence * * @param image The file name of the analyzed image. * @param prediction The prediction for an image analyzed with * DetectAnomalies. * @param minConfidence The minimum acceptable confidence for the prediction * (0-1). * * @return boolean True if the image is anomalous, otherwise False. */ Boolean reject = false; logger.log(Level.INFO, "Checking classification for {0}", image); String[] logParameters = { prediction.confidence().toString(), String.valueOf(minConfidence) }; if (Boolean.TRUE.equals(prediction.isAnomalous()) && prediction.confidence() >= minConfidence) { logger.log(Level.INFO, "Rejected: Anomaly confidence {0} is greater than confidence limit {1}", logParameters); reject = true; } if (Boolean.FALSE.equals(reject)) logger.log(Level.INFO, ": No anomalies found."); return reject; }

세분화

모델이 이미지 세분화 모델인 경우 세분화 정보를 사용하여 이미지에 이상이 있는지 확인할 수 있습니다. 이미지 세분화 모델을 사용하여 이미지를 분류할 수도 있습니다. 이미지 마스크를 가져와서 표시하는 예제 코드는 분류 및 세분화 정보 표시을 참조하십시오.

이상 영역

이미지에 있는 예외 항목의 백분율 범위 (TotalPercentageArea) 를 사용하십시오. 예를 들어, 이상 영역이 이미지의 1% 보다 크면 제품에 결함이 있다고 판단할 수 있습니다.

Python

전체 코드 예제는 GitHub를 참조하십시오.

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
Java V2
public static Boolean rejectOnCoverage(String image, DetectAnomalyResult prediction, float minConfidence, String anomalyType, float maxCoverage) { /** * Rejects an image based on a maximum allowable coverage area for an anomaly * type. * * @param image The file name of the analyzed image. * @param prediction The prediction for an image analyzed with * DetectAnomalies. * @param minConfidence The minimum acceptable confidence for the prediction * (0-1). * @param anomalyTypes The anomaly type to check. * @param maxCoverage The maximum allowable coverage area of the anomaly type. * (0-1). * * @return boolean True if the coverage area of the anomaly type exceeds the * maximum allowed, otherwise False. */ Boolean reject = false; logger.log(Level.INFO, "Checking coverage for {0}", image); if (Boolean.TRUE.equals(prediction.isAnomalous()) && prediction.confidence() >= minConfidence) { for (Anomaly anomaly : prediction.anomalies()) { if (Objects.equals(anomaly.name(), anomalyType) && anomaly.pixelAnomaly().totalPercentageArea() >= maxCoverage) { String[] logParameters = { prediction.confidence().toString(), String.valueOf(minConfidence), String.valueOf(anomaly.pixelAnomaly().totalPercentageArea()), String.valueOf(maxCoverage) }; logger.log(Level.INFO, "Rejected: Anomaly confidence {0} is greater than confidence limit {1} and " + "{2} anomaly type coverage is higher than coverage limit {3}\n", logParameters); reject = true; } } } if (Boolean.FALSE.equals(reject)) logger.log(Level.INFO, ": No anomalies found."); return reject; }

예외 유형 수

이미지에 있는 다양한 예외 유형 수 (Name)를 사용하십시오. 예를 들어, 두 가지 이상의 이상 유형이 있는 경우 제품에 결함이 있다고 판단할 수 있습니다.

Python

전체 코드 예제는 GitHub를 참조하십시오.

def reject_on_anomaly_types(image, prediction, confidence_limit, anomaly_types_limit): """ Checks if the number of anomaly types is greater than 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 value between 0 and 1. :param anomaly_types_limit: The maximum number of allowable anomaly types (Integer). :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
Java V2
public static Boolean rejectOnAnomalyTypeCount(String image, DetectAnomalyResult prediction, float minConfidence, Integer maxAnomalyTypes) { /** * Rejects an image based on a maximum allowable number of anomaly types. * * @param image The file name of the analyzed image. * @param prediction The prediction for an image analyzed with * DetectAnomalies. * @param minConfidence The minimum acceptable confidence for the predictio * (0-1). * @param maxAnomalyTypes The maximum allowable number of anomaly types. * * @return boolean True if the image contains more than the maximum allowed * anomaly types, otherwise False. */ Boolean reject = false; logger.log(Level.INFO, "Checking coverage for {0}", image); Set<String> defectTypes = new HashSet<>(); if (Boolean.TRUE.equals(prediction.isAnomalous()) && prediction.confidence() >= minConfidence) { for (Anomaly anomaly : prediction.anomalies()) { defectTypes.add(anomaly.name()); } // Reduce defect types by one to account for 'background' anomaly type. if ((defectTypes.size() - 1) > maxAnomalyTypes) { String[] logParameters = { prediction.confidence().toString(), String.valueOf(minConfidence), String.valueOf(defectTypes.size()), String.valueOf(maxAnomalyTypes) }; logger.log(Level.INFO, "Rejected: Anomaly confidence {0} is >= minimum confidence {1} and " + "the number of anomaly types {2} > the allowable number of anomaly types {3}\n", logParameters); reject = true; } } if (Boolean.FALSE.equals(reject)) logger.log(Level.INFO, ": No anomalies found."); return reject; }