목표 : 내 데이터셋에 대해 DETR를 재학습시키기
이 글은 DETR에 대한 환경설정이 모두 완료되었다고 가정하고 작성되었습니다. 아직 환경 설정이 완료되지 않았다면 아래 글을 참고해주세요.
Anaconda 설치 및 가상환경 생성
DETR 맛보기
우선 임의의 폴더 상에 DETR 코드를 다운로드합니다. Git bash를 열고 아래 명령을 치면 다운로드가 완료됩니다.
git clone https://github.com/facebookresearch/detr.git .
저는 anaconda를 설치할 때 함께 설치되는 spyder(IDE)를 활용했습니다.
conda install spyder
프롬프트에 아래 명령을 수행하면 가상환경이 적용된 spyder 앱이 실행됩니다.
spyder
수정된 스크립트는 아래 github에 보실 수 있습니다.
import torch.nn as nn
parser.add_argument('--want_class', type=int,
help='number of class which want to finetuning')
# 변경 전
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])
# 변경 후
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu], find_unused_parameters=True)
# 변경 전
model_without_ddp.load_state_dict(checkpoint['model'])
# 변경 후
model_without_ddp.load_state_dict(checkpoint['model'], strict=False)
num_ftrs = model_without_ddp.class_embed.in_features
model_without_ddp.class_embed = nn.Linear(num_ftrs, args.want_class + 2).to(device)
num_classes = args.want_class + 1
데이터셋은 COCO format으로 구성되어 있다고 가정합니다.
이제 아래 명령을 통해 새로운 데이터셋을 학습시킬 수 있습니다.
# 단일 GPU를 사용할 경우
python main.py --coco_path /path/to/dataset --resume https://dl.fbaipublicfiles.com/detr/detr-r50-e632da11.pth --want_class number_of_classes_of_dataset --output_dir finetuning_detr
# 다수의 GPU를 사용할 경우
python -m torch.distributed.launch --nproc_per_node=number_of_gpu --use_env main.py --coco_path /path/to/dataset --resume https://dl.fbaipublicfiles.com/detr/detr-r50-e632da11.pth --want_class number_of_classes_of_dataset --output_dir finetuning_detr