Inference
vims.InferenceHandler
학습한 vims 모델을 사용하여 검사를 하기 위한 InferenceHandler class 입니다.
Usage
demo/inference.py 참고
build(config)
classmethod
InferenceHandler class의 instance를 생성합니다.
Parameters:
Returns:
| Name | Type | Description |
|---|---|---|
InferenceHandler |
InferenceHandler
|
build가 완료된 detection.InferenceHandler class의 instance를 반환합니다. |
infer(data, frame_timestamps=None)
Inference를 수행하고, 결과를 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
Union[RawImages, PreprocessedSequence]
|
inference에 사용할 데이터입니다. |
required |
frame_timestamps |
Optional[List[int]]
|
frame timestamp입니다. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ResultContainer |
ResultContainer
|
inference 결과를 반환합니다. |
Usage
# ResultContainer는 model_dump 메소드를 사용하여 dict로 변환할 수 있습니다.
error, message, result = inference_handler.infer(data)
result_dict = result.model_dump()
result_dict = {
"anomaly_score_final": List[float], # 각 프레임의 anomaly score입니다.
"threshold_final": float, # 최종 threshold 값입니다.
"state_index": List[int], # 각 프레임의 state index입니다.
"frame_timestamps": List[int], # 각 프레임의 timestamp입니다.
"roi_coordinates": List[List[int]], # roi 좌표입니다.
"is_roi_shift": List[bool], # roi shift 발생 여부이고 True일 경우 roi calibration이 수행되었음을 의미합니다.
"is_anomaly": List[bool], # 각 프레임의 anomaly 여부입니다.
"is_domain_shift": List[bool], # domain shift 발생 여부입니다. True일 경우 roi calibration state로 변경됩니다.
"error_code": int, # error code입니다. 0이면 정상입니다.
"clip_path": List[int], # 각 프레임의 clip path입니다.
"frame_index": List[int], # 각 프레임의 index입니다.
"roi_matching_scores": List[float], # roi matching score입니다.
"camera_shift_roi_matching_scores": List[float], # camera shift roi matching score입니다.
}
# 더 많은 정보를 얻기 위해서는 아래와 같이 사용합니다. 연구소 모듈 내부에서 사용하는 정보 및 이미지가 포함됩니다.
result_dict = result.model_dump(return_original=True)
process_manual_roi_calibration(data)
수동 roi calibration을 수행합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
Union[RawImages, PreprocessedSequence]
|
inference에 사용할 데이터입니다. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ResultContainer |
수동 roi calibration 결과를 반환합니다. 구조는 |
get_default_inference_option(key)
inference 옵션의 기본 설정 값을 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key |
str
|
옵션 key |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
옵션 value |
Keys
사용 가능한 key와 value 목록:
{
"apply_roi_calibration": bool, # inference 시 roi calibration을 작동할지 여부입니다. 기본값은 모델 학습 시 설정된 값입니다.
"apply_domain_shift_detection": bool, # inference 시 domain shift detection을 작동할지 여부입니다. 기본값은 모델 학습 시 설정된 값입니다.
"apply_motion_detection": bool, # inference 시 motion detection을 작동할지 여부입니다. 기본값은 모델 학습 시 설정된 값입니다.
"apply_adaptation": bool, # inference 시 adaptation을 작동할지 여부입니다. 기본값은 모델 True입니다.
"score_mode": Literal["topk", "mean"], # score를 계산하는 방식입니다. "topk" 또는 "mean" 중 하나여야 합니다. 기본값은 "mean" 입니다.
"final_threshold": float, # Anomaly 판단 threshold 값입니다. 기본값은 5 입니다.
"camera_shift_matching_threshold": float, # camera shift 판단 threshold 값입니다. 기본값은 0.6 입니다.
"camera_shift_frame_interval": float, # camera shift detection을 수행하는 frame interval 값입니다.기본값은 300 입니다.
}
Usage
get_default_inference_options()
get_inference_option(key)
현재 설정된 inference 옵션 값을 읽습니다.
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)
Inference 옵션을 설정합니다.
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과 동일합니다.
save_heatmap(result, save_dir_path)
이상감지된 이미지에 대하여 heatmap을 저장합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result |
ResultContainer
|
검사 결과가 들어있습니다. |
required |
save_dir_path |
str
|
heatmap 이미지 저장을 위한 폴더 경로 입니다. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict[str, List[str]]
|
save_adaptation_layer(checkpoint_path, metadata=None, password=None, **options)
Adaptation layer를 저장합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint_path |
str
|
checkpoint를 저장할 path 입니다. |
required |
metadata |
Optional[Dict]
|
checkpoint에 저장할 추가 metadata. Defaults to None. |
None
|
password |
Optional[str]
|
checkpoint 파일에서 중요한 정보를 암호화 하는데 사용되는 password 입니다. None이면 암호화하지 않습니다. Defaults to None. |
None
|
**options |
state_dict 저장 시 추가적인 옵션을 설정합니다. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None |
get_template_images()
ROI calibration에서 사용하는 template 이미지를 반환합니다.
Returns:
| Type | Description |
|---|---|
Dict[str, ndarray]
|
Dict[str, np.ndarray]: template 이미지 dictionary 입니다. |
Dict[str, ndarray]
|
```python |
Dict[str, ndarray]
|
output = { "roi_images": np.ndarray, # roi template 이미지들 입니다. "camera_shift_roi_images" : Optional[np.ndarray], # camera shift roi template 이미지들 입니다. |
Dict[str, ndarray]
|
} |