Skip to content

Analysis

core.AnalysisHandler

모델을 빌드하지 않고 저장된 모델 인퍼런스 결과를 이용해서 analysis를 수행하는 클래스입니다.

Usage

demo/analysis.py 참고

build(config) classmethod

AnalysisHandler class의 instance를 생성합니다.

Parameters:

Name Type Description Default
config Dict

AnalysisHandler를 build 하기 위한 config가 담겨 있는 dictionary 입니다.

{
    "inference_options": Optional[Dict], # 인퍼런스 옵션 config (default None). 구조는 `get_default_inference_option`의 Keys 참고.
}

required

Returns:

Name Type Description
AnalysisHandler AnalysisHandler

build가 완료된 core.AnalysisHandler class의 instance를 반환합니다.

get_default_inference_option(key)

Postprocess 옵션의 기본 설정 값을 반환합니다.

Parameters:

Name Type Description Default
key str

옵션 key

required

Returns:

Name Type Description
Any Any

옵션 value

Keys

사용 가능한 key와 value 목록:

{
    "outputs.scoremap": bool,  # (default True)
    "outputs.score_min": bool,  # (default True) scoremap에서 최소값.
    "outputs.score_max": bool,  # (default True) scoremap에서 최대값.
    "outputs.mask": bool,  # (default True)
    "outputs.is_ng": bool,  # (default True)
    "outputs.heatmap": bool,  # (default True)
    "outputs.objects": bool,  # (default True)
    "params.anomaly_score_threshold": float,    # is_ng를 판단할 때 사용됩니다.
                                                # True이면 NG, False이면 OK 이미지를 나타냅니다.
                                                # (-inf, inf) 범위의 소수. (default 0.0)
    # 다음 두 heatmap_color_range_x 파라미터를 사용해 min-max normalization을 수행합니다.
    "params.heatmap_color_range_max": float,    # scoremap을 이용해, heatmap을 만들 때 사용됩니다.
                                                # (default 0.0)
    "params.heatmap_color_range_min": float,    # scoremap을 이용해, heatmap을 만들 때 사용됩니다.
                                                # (default 0.0)
    "params.calc_object_area_and_apply_threshold": bool,  # True인 경우 object마다 area(=픽셀수)를 계산하고 object_area_threshold를 적용합니다.
                                                # outputs.objects가 True일때만 적용.
                                                # (default True)
    "params.calc_object_score_and_apply_threshold": bool,  # True인 경우 object마다 anomaly score를 계산하고 object_score_threshold를 적용합니다.
                                                # outputs.objects가 True일때만 적용.
                                                # (default True)
    "params.object_boundary_threshold": float,  # mask를 만들 때 사용됩니다.
                                                # 픽셀의 예측된 score가 threshold보다 작은 경우 0으로, 큰 경우 1로 치환 됩니다.
                                                # 픽셀값이 1이면 Anomaly part이고, 0이면 Normal part입니다.
                                                # (-inf, inf) 범위의 소수. (default 0.0)
    "params.object_scoring_method": str,        # object별로 score를 나타내기 위해 사용되는 method를 조절합니다.
                                                # "outputs.objects"가 True이고, "params.calc_object_score_and_apply_threshold"가 True일때 동작합니다.
                                                # option중에는 ["mean", "max"]이 있습니다.
                                                # (default "mean")
    "params.object_score_threshold": float,     # object의 score가 threshold보다 작은 경우 필터링 됩니다.
                                                # "outputs.objects"가 True이고, "params.calc_object_score_and_apply_threshold"가 True일때 동작합니다.
                                                # (-inf, inf) 범위의 소수. (default 0.0)
    "params.object_area_method": str,           # object area를 계산할 때 사용되는 method.
                                                # option중에는 ["fast_plus"]만 존재합니다.
                                                # (default "fast_plus")
    "params.object_area_threshold": int,        # object의 area(=픽셀수)가 threshold보다 작은 경우 필터링 됩니다.
                                                # "outputs.objects"가 True이고, "params.calc_object_area_and_apply_threshold"가 True일때 동작합니다.
                                                # 0 이상의 정수. (default 0)
}

사용 예시:

handler.get_default_inference_option(key="outputs.mask")
handler.set_inference_option(key="outputs.mask", value=False)

Note

InferenceHandler.get_default_inference_optionoutputs.time, params.batch_size를 제외하면 동일합니다.

get_inference_option(key)

현재 설정된 postprocess 옵션 값을 읽습니다.

Parameters:

Name Type Description Default
key str

옵션 key

required

Returns:

Name Type Description
Any Any

옵션 value

Keys

get_default_inference_option과 동일합니다.

set_inference_option(key, value)

postprocess 옵션을 설정합니다. Note: heatmap_color_range_max과 heatmap_color_range_min을 설정할 때는, 현재 어떤 값으로 설정되어있는지 확인하고, heatmap_color_range_max >= heatmap_color_range_min의 조건을 만족하도록 설정해야 합니다.

Parameters:

Name Type Description Default
key str

설정하고자 하는 옵션 key 입니다.

required
value Any

설정하고자 하는 옵션 value 입니다.

required

Returns:

Name Type Description
None None

None

Keys

get_default_inference_option과 동일합니다.

analyze(images, labeled)

저장된 network output으로 부터 images들에 대한 analysis 결과 계산

Args & Returns: InferenceHandler.analyze()와 동일하며, 결과 predictions에서 time은 제외됩니다.

Note1

analyze() 함수를 사용하기 위해서는 inference_options 중 output.mask=True & outputs.objects=True 여야 합니다.

Note2

모델 인퍼런스까지 재수행하려면 InferenceHandler.analyze()를 사용하세요.

core.SummaryHandler

모델을 빌드하지 않고 저장된 analysis 결과를 이용해서 analysis summary를 수행하는 클래스입니다.

Usage

demo/inference.py & demo/analysis.py 참고

build() classmethod

SummaryHandler class의 instance를 생성합니다.

Returns:

Name Type Description
SummaryHandler SummaryHandler

build가 완료된 core.SummaryHandler class의 instance를 반환합니다.

summarize_analysis(images, labeled)

Analysis를 마무리하고 결과를 리턴합니다.

Parameters:

Name Type Description Default
images Dict[str, Dict]

analysis에 사용할 데이터 리스트.

{
    "{image_id}": {
        "save_dir": str,  # 해당 이미지의 analysis 결과 저장 directory
    },
    ...,  # times number of images
}

required
labeled bool

label 존재 여부. label 존재 여부에 따라 analysis 결과 Dict 구성 요소가 달라집니다.

required

Returns:

Name Type Description
Dict Dict

Analysis summary

{
    # InferenceHandler.summarize_analysis 호출시에 labeled=True였다면 아래 Key들을 return합니다.
    ## 아래 AUROC, AUPR, Best threshold, Best F1-score는 NG와 OK이미지가 모두 적어도 1개 이상 있을때 출력합니다.
    "AUROC": float,  # Area under ROC
    "AUPR": float,  # Area under PR curve
    "Best threshold": float,  # Best threshold
    "Best F1-Score": float,  # F1 score with labels
    ## 아래 이미지 개수에 대한 summary는 anomaly_score_threshold로 계산되었습니다.
    "number_of_images": int,  # number of analyzed images
    "number_of_correct_images": int,  # number of correct images
    "number_of_wrong_images": int,  # number of wrong images
    "number_of_overkill": int,  # number of overkill images(label: OK, prediction: NG)
    "number_of_not_overkill": int,  # number of images(label: OK, prediction: OK)
    "number_of_underkill": int,  # number of images(label: NG, prediction: OK)
    "number_of_not_underkill": int  # number of images(label: NG, prediction: NG)
}