Ermitteln, ob ein Bild anomal ist - Amazon Lookout für Vision

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

Ermitteln, ob ein Bild anomal ist

Sie können auf verschiedene Arten feststellen, ob ein Bild anomal ist. Die Methode, die Sie wählen, hängt von Ihrem Anwendungsfall und dem Typ Ihres Modells ab. Im Folgenden finden Sie mögliche Lösungen.

Klassifizierung

IsAnomalousklassifiziert ein Bild als anomal, verwende denConfidenceFeld, um zu entscheiden, ob das Bild tatsächlich anomal ist. Ein höherer Wert weist auf ein größeres Vertrauen hin. Sie könnten beispielsweise entscheiden, dass ein Produkt nur dann fehlerhaft ist, wenn die Zuverlässigkeit bei über 80% liegt. Sie können die analysierten Bilder anhand von Klassifizierungsmodellen oder anhand von Bildsegmentierungsmodellen klassifizieren.

Python

Das vollständige Codebeispiel finden Sie unterGitHub.

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; }

Segmentierung

Wenn es sich bei Ihrem Modell um ein Bildsegmentierungsmodell handelt, können Sie anhand der Segmentierungsinformationen feststellen, ob ein Bild Anomalien enthält. Sie können auch ein Bildsegmentierungsmodell verwenden, um Bilder zu klassifizieren. Code zum Abrufen und Anzeigen von Bildmasken finden Sie unterAnzeige von Klassifizierungs- und Segmentierungsinformationen

Bereich der Anomalie

Verwenden Sie die prozentuale Deckung (TotalPercentageArea) einer Anomalie auf dem Bild. Sie könnten beispielsweise entscheiden, dass ein Produkt defekt ist, wenn der Bereich einer Anomalie mehr als 1% des Bildes ausmacht.

Python

Das vollständige Codebeispiel finden Sie unterGitHub.

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; }

Anzahl der Anomaliearten

Verwenden Sie eine Anzahl verschiedener Anomalietypen (Name) auf dem Bild gefunden. Sie könnten beispielsweise entscheiden, dass ein Produkt defekt ist, wenn mehr als zwei Arten von Anomalien vorliegen.

Python

Das vollständige Codebeispiel finden Sie unterGitHub.

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; }