Determinare se un'immagine è anomala - Amazon Lookout per Vision

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Determinare se un'immagine è anomala

È possibile determinare se un'immagine è anomala in diversi modi. Il metodo scelto dipende dal tuo caso d'uso e dal tipo di modello. Di seguito sono elencate le possibili soluzioni.

Classificazione

IsAnomalousclassifica un'immagine come anomala, usa ilConfidencecampo per aiutare a decidere se l'immagine è effettivamente anomala. Un valore più alto indica una maggiore sicurezza. Ad esempio, potresti decidere che un prodotto è difettoso solo se la fiducia è superiore all'80%. È possibile classificare le immagini analizzate mediante modelli di classificazione o modelli di segmentazione delle immagini.

Python

Per l'esempio di codice completo, vedereGitHub.

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

Segmentazione

Se il modello è un modello di segmentazione dell'immagine, puoi utilizzare le informazioni sulla segmentazione per determinare se un'immagine contiene anomalie. È inoltre possibile utilizzare un modello di segmentazione delle immagini per classificare le immagini. Ad esempio, codice che ottiene e visualizza maschere di immagini, vediVisualizzazione delle informazioni su classificazione e segmentazione

Area di anomalia

Usa la copertura percentuale (TotalPercentageArea) di un'anomalia sull'immagine. Ad esempio, potresti decidere che un prodotto è difettoso se l'area di un'anomalia è superiore all'1% dell'immagine.

Python

Per l'esempio di codice completo, vedereGitHub.

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

Numero di tipi di anomalie

Utilizza un conteggio di diversi tipi di anomalie (Name) trovato nell'immagine. Ad esempio, potresti decidere che un prodotto è difettoso se sono presenti più di due tipi di anomalie.

Python

Per l'esempio di codice completo, vedereGitHub.

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