Inference
SaigeVAD.VadInferenceEngine
Bases: VadInferenceEngineHandler
학습한 VAD 모델을 사용하여 검사를 하기 위핸 VadInferenceEngine class입니다.
roi 보정 on/off를 위해서는 is_roi_calibration setter를 이용합니다.
domain shift detection on/off를 위해서는 is_domain_shift_detection setter를 이용합니다.
domain shift detection off 상태에서는 roi_calibration이 동작하지 않습니다.
camera shift detection on/off를 위해서는 is_camera_shift_detection setter를 이용합니다.
fixed roi가 설정되어 있지 않은 경우 camera shift detection은 동작하지 않습니다.
Usage
from SaigeVAD.SaigeToolkit.SaigeToolkit.util.config import read_yml
from SaigeVAD.tools.config_maker import create_train_config_and_reference_images
from SaigeVAD.engine.saigevad_api import VadInferenceEngine
config_path = "./config/sample_inference_config_database.yml"
model_path = "./sample.saigevad"
password = "password" #or None
save_dir = "./test_results"
config = read_yml(config_path)
#config을 사용하는 경우 engine 빌드
error, vad_inference_engine = VadInferenceEngine.build(
model_path=None,
config=config,
password=password
)
#model file을 사용하는 경우
error, vad_inference_engine = VadInferenceEngine.build(
model_path=model_path,
password=password
)
# gpu assign
vad_inference_engine.to_device(device="0")
# critical_threshold 적용 (10으로 변경)
vad_inference_engine.n_sigma = 10
# detect_mode: "moderate" 적용 ("moderate" mode로 변경)
vad_inference_engine.detect_mode = "moderate"
# ROI calibration 기능 Off (On은 True)
vad_inference_engine.is_roi_calibration = False
# Domain shift detection 기능 off (On은 True)
vad_inference_engine.is_domain_shift_detection = False
# Camera shift detection 기능 off (On은 True) - fixed_roi가 설정되어 있는 경우 default 값은 True.
vad_inference_engine.is_camera_shift_detection = False
# N 개의 frame에 대하여 inference 수행
error, results = vad_inference_engine.inference(input_data, frame_timestamp, force_cluster_result, cluster_result_threshold)
# inference 결과로부터 heatmap 저장
error, heatmap_path_list = vad_inference_engine.save_heatmap(results, save_dir)
# 업데이트 된 모델 저장
vad_inference_engine.save_checkpoint(checkpoint_path=model_path, password=password)
build(model_path, config=None, n_sigma=None, detect_mode='strict', password=None)
classmethod
검사를 위해 InferenceHandler class의 instance를 생성합니다. config 혹은 model_path 둘 중 하나는 필수입니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path |
str
|
InferenceHandler를 build 하기 위한 VAD model path입니다. |
required |
config |
Dict
|
InferenceHandler를 build 하기 위한 config가 담겨 있는 dictionary 입니다.. Defaults to None. |
None
|
n_sigma |
int
|
Threshold 계산을 위한 n_sigma 입니다. Defaults to None. |
None
|
detect_mode |
str
|
결함 감지의 세기를 조절하는 option ("strict" or "moderate")입니다. Defaults to "strict". |
'strict'
|
password |
str
|
VAD model 복호화를 위해 필요한 password입니다. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
VadInferenceEngine |
build가 완료된 saigevad_api.VadInferenceEngine class의 instance를 반환합니다. |
to_device(device)
Engine이 검사하는 device를 변경합니다. (gpu only)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device |
Union[int, str]
|
변경하고자 하는 device 입니다. |
required |
inference(input_data, frame_timestamp, profile_mode=False, force_cluster_result=False, cluster_result_threshold=2.0)
input image data에 대하여 검사를 수행합니다. 모델 파일에 clustering 관련 정보가 자동으로 clustering을 적용합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_data |
Union[ndarray, Tensor]
|
검사 대상 이미지입니다. |
required |
frame_timestamp |
List[str]
|
검사 이미지의 time stamp 입니다. |
required |
profile_mode |
bool
|
연구팀 디버깅을 위한 값입니다. Defaults to False. |
False
|
force_cluster_result |
bool
|
내부에서 anomaly로 판단하지 않아도 클러스터 결과를 반환합니다. |
False
|
cluster_result_threshold |
float
|
force_cluster_result가 True이고, final_score가 cluster_result_threshold 초과 인 경우, 클러스터 결과를 반환합니다. Defaults to 2.0. |
2.0
|
Returns:
| Name | Type | Description |
|---|---|---|
ResultContainer |
ResultContainer
|
input_data에 대한 검사 결과가 들어있습니다. |
save_heatmap(results, save_dir_path)
이상감지된 이미지에 대하여 heatmap을 저장합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results |
ResultContainer
|
검사 결과가 들어있습니다. |
required |
save_dir_path |
str
|
heatmap 이미지 저장을 위한 폴더 경로 입니다. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Dict |
Dict
|
save_checkpoint(checkpoint_path, password=None, **options)
검사를 진행하며 업데이트 된 모델을 저장합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint_path |
str
|
checkpoint를 저장할 path 입니다. |
required |
password |
Optional[str]
|
checkpoint 파일에서 중요한 정보를 암호화 하는데 사용되는 password 입니다. None이면 암호화하지 않습니다. Defaults to None. |
None
|