Clone the official DETR GitHub repository and install the necessary dependencies:
git clone https://github.com/facebookresearch/detr.git
cd detr
conda install -c pytorch pytorch torchvision
conda install cython scipy
pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'Change the num_classes in detr/models/detr.py to 17 to match the number of custom object classes.
Download the required pre-trained model from the Model Zoo such as detr-r50-e632da11.pth.
Convert the YOLO formatted train data and valid data to COCO format.
Use the yolo_to_coco.py script to perform the conversion and save the files as instances_train2017.json & instances_val2017.json.
The folder structure should be as follows:
path/to/coco/
├── annotations/ # annotation json files (instances_train2017.json & instances_val2017.json)
├── train2017/ # train images
└── val2017/ # val imagesRun the following command in the DETR directory to start training the model:
python main.py --batch_size 8 --epochs 150 \
--coco_path /home/vv1150n/113-1_HW/CVDL_HW1/dataset \
--output_dir /home/vv1150n/113-1_HW/CVDL_HW1/DETR_output_150epochs \
--resume /home/vv1150n/113-1_HW/CVDL_HW1/checkpoint_file/detr-r50-e632da11.pth \
--num_workers 4python main.py --batch_size 1 --start_epoch 151 \
--epochs 201 --lr 1e-5 --num_workers 4 \
--coco_path /home/vv1150n/113-1_HW/CVDL_HW1/dataset \
--output_dir /home/vv1150n/113-1_HW/CVDL_HW1/DETR_output_150epoch_50epochs \
--resume /home/vv1150n/113-1_HW/CVDL_HW1/DETR_output_150epochs/checkpoint.pth \
Run the following command in the DETR directory to start evaluation the model:
python main.py --batch_size 8 --no_aux_loss --eval \
--resume /home/vv1150n/113-1_HW/CVDL_HW1/DETR_output_150epochs/checkpoint.pth \
--coco_path /home/vv1150n/113-1_HW/CVDL_HW1/dataset \
--num_workers 4Use inference.py to make predictions on the validation set, and save the output as valid_<student-id>.json.
You can adjust the prediction threshold as needed:
python inference.py --folder_path ./valid/images \
--model_path /home/vv1150n/113-1_HW/CVDL_HW1/DETR_output_150epochs/checkpoint.pth \
--output_file valid_r12945042.json \
--threshold 0.95 Use eval_new.py to evaluate the model's predictions on the validation set.
Provide the paths for val_submission.json and valid_target.json to get the mAP (50-95) evaluation results:
python eval_new.py valid_r12945042.json valid_target.json Use inference.py to make predictions on the test set and save the output as test_<student-id>.json:
python inference.py --folder_path ./test/images \
--model_path /home/vv1150n/113-1_HW/CVDL_HW1/DETR_output_150epoch_50epochs/checkpoint.pth \
--output_file test_r12945042.json \
--threshold 0.95 Run visualization.py, enter the image file name, and the final result will be output as visualization_result.png.
python visualization.py