콘텐츠로 이동

Train VAD

SaigeVAD.VadTrainer

Bases: train_vad_engine.VadTrainer

VAD 모델을 학습시키기 위한 VadTrainer class입니다. - 참고: main_train_vad.py를 사용하여 python 환경에서 train pipeline을 실행할 수 있습니다.

Usage
from SaigeVAD.engine.saigevad_api import VadTrainer, VadTrainConfigMaker

checkpoint_path = "./sample.saigevad"
password = "saigevad_model" #None일 경우 암호화 X

config = {
    "train_data": {
        "clip_path": "/NFS/database_personal/bts/VAD/voc/221207/vad_training_sample/clips",
        "frame_dir": [
            "test1_00",
            "test1_01",
            "test1_04",
            "test1_06",
        ],
    },
    "config": {
        "cycle_list": [
            "test1_00-00000000",  # {clip name}-{start frame cycle 1}
            "test1_00-00000027",  # {clip name}-{end frame cycle 1}
            "test1_00-00000035",  # {clip name}-{start frame cycle 2}
            "test1_00-00000059",  # {clip name}-{end frame cycle 2}
            "test1_04-00000000",  # {clip name}-{start frame cycle 3}
            "test1_04-00000029",  # {clip name}-{end frame cycle 3}
        ],
        "iteration": 5000,
        "network_model": "shallow"
    },
    "metadata": {
        "common_settings": {
            "roi_xyxy": [1149, 557, 1278, 746],  # (left, top, right, bottom)
            "fps": 25,
            "mask_mode": None,  # "binary", "semi_binary
            "cycle_length": 29, # int
        },
    },
}

# config 변환
error, train_config_maker = VadTrainConfigMaker.build(config=config)
error, config_dict = train_config_maker.make_train_config()

# Trainer 빌드
error, trainer = VadTrainer.build(config=config_dict)

# 0번 GPU로 이동
error, _= trainer.to_device(0)

# 1000 스텝 학습
for _ in range(1000):
    error, train_result = trainer.train_one_step()

# weight sampling 수행
error, weight_sampling_result = trainer.process_weight_sampling()

# adaptive autoencoder 학습
error, _= trainer.train_adaptive_autoencoder()

# warm up 수행
error, _= trainer.warm_up()

# VAD 최종 체크포인트 저장
error, _= trainer.save_checkpoint(
            checkpoint_path=checkpoint_path,
            password=password,
            metadata=trainer.metadata,
        )

build(config, task='developer') classmethod

학습을 위해 VadTrainer class의 instance를 생성합니다.

Parameters:

Name Type Description Default
config Dict

학습 config가 담겨 있는 dictionary 입니다.

config = {
    "train_config": Dict, # 학습에 필요한 설정이 담겨있습니다.
        {
            "base_config": Dict, # 학습 기본 설정에 대한 정보가 담겨있습니다.
            "train_dataloader": Dict, # 학습 데이터로더에 대한 정보가 담겨있습니다.
            "motion_detector_dataloader": Dict, # 모션 디텍터에 대한 정보가 담겨있습니다.
        },
    "reference_images": Dict, # 학습, 검사에 참고하는 이미지들 입니다.
        {
            "template_image_tensors": List[torch.Tensor], # 템플릿 매칭에 사용됩니다.
            "color_count_map": List[torch.Tensor], # ROI 매칭에 사용됩니다.
        },
    "pretrained_model_path": os.path.join(ROOT_DIR, "../pretrained_weight/vad"),
}

required
task str

str. # 연구팀 디버그용 파라미터 입니다. Defaults to "developer".

'developer'

Returns:

Name Type Description
VadTrainer

학습 config가 담겨 있는 dictionary 입니다.


to_device(device)

VadTrainer가 train 하는 device를 변경합니다. (gpu only)

Parameters:

Name Type Description Default
device Union[int, str]

변경하고자 하는 device 입니다.

required

train_one_step()

VAD 학습을 1스텝 진행합니다.

Returns:

Name Type Description
Dict Dict

학습과 관련된 정보가 담겨 있는 dictionary 입니다.

output = {
    "step": int, # 현재까지 진행된 총 학습 step입니다.
    "epoch": int, # 현재까지 진행된 총 학습 epoch. 사용하지 않는 정보입니다.
    "epoch_step": int, # 현재 epoch에서 몇 번째 step인지를 나타냅니다.
    "loss/total": float, # 학습에 사용되는 모든 loss를 모두 더한 값입니다.
    "step_time(sec)": float,  # 학습을 1 step 진행하는데 걸린 총 시간입니다.
}


process_weight_sampling()

학습 데이터 전체에 대하여 weight sampling을 진행합니다.

Total train step 별 권장 weight sampling 마일스톤은 아래와 같습니다. - train_step 5000 : [1000, 3000] (default) - train_step 8000 : [1000, 3000, 6000] - train_step 10000 : [1000, 4000, 7000]

Returns:

Type Description
np.ndarray

np.ndarray: Weight sampling 관련 정보가 담겨있는 array입니다.


train_adaptive_autoencoder()

Adaptive autoencoder 학습과정 전체를 수행합니다.


warm_up()

3개의 clip을 이용하여 warm up을 수행합니다. VAD 학습의 가장 마지막 과정입니다.


save_checkpoint(checkpoint_path, metadata=None, password=None)

현재 VadTrainer의 상태를 dictionary로 저장합니다.

Parameters:

Name Type Description Default
checkpoint_path str

checkpoint를 저장할 path 입니다.

required
metadata dict

checkpoint에 저장할 추가 메타데이터입니다. Defaults to None.

None
password str

checkpoint 파일에서 중요한 정보를 암호화 하는데 사용되는 password 입니다. None이면 암호화하지 않습니다. Defaults to None.

None