Repository: augmentedstartups/AS-One Branch: main Commit: 1a6eb0952fdc Files: 501 Total size: 3.3 MB Directory structure: gitextract_lrbvjsnv/ ├── .dockerignore ├── .gitignore ├── Dockerfile ├── LICENCE ├── README.md ├── asone/ │ ├── __init__.py │ ├── asone.py │ ├── demo_detector.py │ ├── demo_ocr.py │ ├── demo_pose_estimator.py │ ├── demo_segmentor.py │ ├── demo_tracker.py │ ├── detectors/ │ │ ├── __init__.py │ │ ├── detector.py │ │ ├── easyocr_detector/ │ │ │ ├── __init__.py │ │ │ └── text_detector.py │ │ ├── utils/ │ │ │ ├── __init__.py │ │ │ ├── cfg_path.py │ │ │ ├── coreml_utils.py │ │ │ ├── exp_name.py │ │ │ └── weights_path.py │ │ ├── yolonas/ │ │ │ ├── __init__.py │ │ │ └── yolonas.py │ │ ├── yolor/ │ │ │ ├── __init__.py │ │ │ ├── cfg/ │ │ │ │ ├── yolor_csp.cfg │ │ │ │ ├── yolor_csp_x.cfg │ │ │ │ └── yolor_p6.cfg │ │ │ ├── models/ │ │ │ │ ├── __init__.py │ │ │ │ ├── common.py │ │ │ │ ├── export.py │ │ │ │ └── models.py │ │ │ ├── utils/ │ │ │ │ ├── __init__.py │ │ │ │ ├── activations.py │ │ │ │ ├── autoanchor.py │ │ │ │ ├── datasets.py │ │ │ │ ├── export.py │ │ │ │ ├── general.py │ │ │ │ ├── google_utils.py │ │ │ │ ├── layers.py │ │ │ │ ├── loss.py │ │ │ │ ├── metrics.py │ │ │ │ ├── parse_config.py │ │ │ │ ├── plots.py │ │ │ │ ├── torch_utils.py │ │ │ │ └── yolor_utils.py │ │ │ └── yolor_detector.py │ │ ├── yolov5/ │ │ │ ├── __init__.py │ │ │ ├── yolov5/ │ │ │ │ ├── __init__.py │ │ │ │ ├── export.py │ │ │ │ ├── models/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── common.py │ │ │ │ │ ├── experimental.py │ │ │ │ │ ├── general.py │ │ │ │ │ ├── tf.py │ │ │ │ │ └── yolo.py │ │ │ │ └── utils/ │ │ │ │ ├── __init__.py │ │ │ │ ├── activations.py │ │ │ │ ├── augmentations.py │ │ │ │ ├── dataloaders.py │ │ │ │ ├── downloads.py.py │ │ │ │ ├── general.py │ │ │ │ ├── metrics.py │ │ │ │ ├── torch_utils.py │ │ │ │ └── yolov5_utils.py │ │ │ └── yolov5_detector.py │ │ ├── yolov6/ │ │ │ ├── __init__.py │ │ │ ├── yolov6/ │ │ │ │ ├── __init__.py │ │ │ │ ├── assigners/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── anchor_generator.py │ │ │ │ │ ├── assigner_utils.py │ │ │ │ │ ├── atss_assigner.py │ │ │ │ │ ├── iou2d_calculator.py │ │ │ │ │ └── tal_assigner.py │ │ │ │ ├── layers/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── common.py │ │ │ │ │ └── dbb_transforms.py │ │ │ │ ├── models/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── efficientrep.py │ │ │ │ │ ├── effidehead.py │ │ │ │ │ ├── end2end.py │ │ │ │ │ ├── loss.py │ │ │ │ │ ├── loss_distill.py │ │ │ │ │ ├── reppan.py │ │ │ │ │ └── yolo.py │ │ │ │ └── utils/ │ │ │ │ ├── __init__.py │ │ │ │ ├── checkpoint.py │ │ │ │ ├── events.py │ │ │ │ ├── figure_iou.py │ │ │ │ ├── general.py │ │ │ │ ├── torch_utils.py │ │ │ │ └── yolov6_utils.py │ │ │ └── yolov6_detector.py │ │ ├── yolov7/ │ │ │ ├── __init__.py │ │ │ ├── yolov7/ │ │ │ │ ├── __init__.py │ │ │ │ ├── models/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── common.py │ │ │ │ │ ├── experimental.py │ │ │ │ │ └── yolo.py │ │ │ │ └── utils/ │ │ │ │ ├── __init__.py │ │ │ │ ├── torch_utils.py │ │ │ │ └── yolov7_utils.py │ │ │ └── yolov7_detector.py │ │ ├── yolov8/ │ │ │ ├── __init__.py │ │ │ ├── utils/ │ │ │ │ ├── __init__.py │ │ │ │ └── yolov8_utils.py │ │ │ └── yolov8_detector.py │ │ ├── yolov9/ │ │ │ ├── __init__.py │ │ │ ├── export.py │ │ │ ├── yolov9/ │ │ │ │ ├── __init__.py │ │ │ │ ├── models/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── common.py │ │ │ │ │ ├── experimental.py │ │ │ │ │ ├── tf.py │ │ │ │ │ └── yolo.py │ │ │ │ └── utils/ │ │ │ │ ├── __init__.py │ │ │ │ ├── activations.py │ │ │ │ ├── augmentations.py │ │ │ │ ├── autoanchor.py │ │ │ │ ├── autobatch.py │ │ │ │ ├── callbacks.py │ │ │ │ ├── coco_utils.py │ │ │ │ ├── dataloaders.py │ │ │ │ ├── downloads.py │ │ │ │ ├── general.py │ │ │ │ ├── lion.py │ │ │ │ ├── loss.py │ │ │ │ ├── loss_tal.py │ │ │ │ ├── loss_tal_dual.py │ │ │ │ ├── loss_tal_triple.py │ │ │ │ ├── metrics.py │ │ │ │ ├── plots.py │ │ │ │ ├── segment/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── augmentations.py │ │ │ │ │ ├── dataloaders.py │ │ │ │ │ ├── general.py │ │ │ │ │ ├── loss.py │ │ │ │ │ ├── loss_tal.py │ │ │ │ │ ├── loss_tal_dual.py │ │ │ │ │ ├── metrics.py │ │ │ │ │ ├── plots.py │ │ │ │ │ └── tal/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── anchor_generator.py │ │ │ │ │ └── assigner.py │ │ │ │ ├── tal/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── anchor_generator.py │ │ │ │ │ └── assigner.py │ │ │ │ ├── torch_utils.py │ │ │ │ ├── triton.py │ │ │ │ └── yolov9_utils.py │ │ │ └── yolov9_detector.py │ │ └── yolox/ │ │ ├── __init__.py │ │ ├── exps/ │ │ │ ├── __init__.py │ │ │ ├── yolov3.py │ │ │ ├── yolox_l.py │ │ │ ├── yolox_m.py │ │ │ ├── yolox_nano.py │ │ │ ├── yolox_s.py │ │ │ ├── yolox_tiny.py │ │ │ └── yolox_x.py │ │ ├── yolox/ │ │ │ ├── __init__.py │ │ │ ├── core/ │ │ │ │ ├── __init__.py │ │ │ │ ├── launch.py │ │ │ │ └── trainer.py │ │ │ ├── data/ │ │ │ │ ├── __init__.py │ │ │ │ ├── data_augment.py │ │ │ │ ├── data_prefetcher.py │ │ │ │ ├── dataloading.py │ │ │ │ ├── datasets/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── coco.py │ │ │ │ │ ├── coco_classes.py │ │ │ │ │ ├── datasets_wrapper.py │ │ │ │ │ ├── mosaicdetection.py │ │ │ │ │ ├── voc.py │ │ │ │ │ └── voc_classes.py │ │ │ │ └── samplers.py │ │ │ ├── evaluators/ │ │ │ │ ├── __init__.py │ │ │ │ ├── coco_evaluator.py │ │ │ │ ├── voc_eval.py │ │ │ │ └── voc_evaluator.py │ │ │ ├── exp/ │ │ │ │ ├── __init__.py │ │ │ │ ├── base_exp.py │ │ │ │ ├── build.py │ │ │ │ ├── default/ │ │ │ │ │ └── __init__.py │ │ │ │ └── yolox_base.py │ │ │ ├── models/ │ │ │ │ ├── __init__.py │ │ │ │ ├── build.py │ │ │ │ ├── darknet.py │ │ │ │ ├── losses.py │ │ │ │ ├── network_blocks.py │ │ │ │ ├── yolo_fpn.py │ │ │ │ ├── yolo_head.py │ │ │ │ ├── yolo_pafpn.py │ │ │ │ └── yolox.py │ │ │ └── utils/ │ │ │ ├── __init__.py │ │ │ ├── allreduce_norm.py │ │ │ ├── boxes.py │ │ │ ├── checkpoint.py │ │ │ ├── compat.py │ │ │ ├── demo_utils.py │ │ │ ├── dist.py │ │ │ ├── ema.py │ │ │ ├── logger.py │ │ │ ├── lr_scheduler.py │ │ │ ├── metric.py │ │ │ ├── model_utils.py │ │ │ ├── setup_env.py │ │ │ └── visualize.py │ │ ├── yolox_detector.py │ │ └── yolox_utils.py │ ├── linux/ │ │ ├── Instructions/ │ │ │ ├── Benchmarking.md │ │ │ ├── Demo-Detectron2.md │ │ │ ├── Docker-Setup.md │ │ │ ├── Driver-Installations.md │ │ │ ├── Manual-Build.md │ │ │ └── Manual-Installation.md │ │ ├── README.md │ │ ├── docker-installation.sh │ │ └── main.py │ ├── pose_estimator.py │ ├── pose_estimators/ │ │ ├── __init__.py │ │ ├── yolov7_pose/ │ │ │ ├── __init__.py │ │ │ ├── main.py │ │ │ ├── models/ │ │ │ │ ├── __init__.py │ │ │ │ ├── common.py │ │ │ │ ├── experimental.py │ │ │ │ └── yolo.py │ │ │ ├── requirements.txt │ │ │ ├── utils/ │ │ │ │ ├── __init__.py │ │ │ │ ├── activations.py │ │ │ │ ├── add_nms.py │ │ │ │ ├── autoanchor.py │ │ │ │ ├── datasets.py │ │ │ │ ├── general.py │ │ │ │ ├── google_utils.py │ │ │ │ ├── loss.py │ │ │ │ ├── metrics.py │ │ │ │ ├── plots.py │ │ │ │ ├── torch_utils.py │ │ │ │ ├── wandb_logging/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── log_dataset.py │ │ │ │ │ └── wandb_utils.py │ │ │ │ └── yolov7_pose_utils.py │ │ │ └── yolov7.py │ │ └── yolov8_pose/ │ │ ├── __init__.py │ │ ├── plots.py │ │ └── yolov8.py │ ├── recognizers/ │ │ ├── __init__.py │ │ ├── easyocr_recognizer/ │ │ │ ├── __init__.py │ │ │ └── easyocr_recognizer.py │ │ ├── recognizer.py │ │ └── utils/ │ │ ├── __init__.py │ │ └── recognizer_name.py │ ├── schemas/ │ │ └── output_schemas.py │ ├── segmentors/ │ │ ├── __init__.py │ │ ├── segment_anything/ │ │ │ ├── __init__.py │ │ │ └── sam.py │ │ ├── segmentor.py │ │ └── utils/ │ │ ├── __init__.py │ │ └── weights_path.py │ ├── trackers/ │ │ ├── __init__.py │ │ ├── byte_track/ │ │ │ ├── __init__.py │ │ │ ├── bytetracker.py │ │ │ └── tracker/ │ │ │ ├── __init__.py │ │ │ ├── basetrack.py │ │ │ ├── byte_tracker.py │ │ │ ├── kalman_filter.py │ │ │ └── matching.py │ │ ├── deep_sort/ │ │ │ ├── __init__.py │ │ │ ├── deepsort.py │ │ │ └── tracker/ │ │ │ ├── .gitignore │ │ │ ├── README.md │ │ │ ├── __init__.py │ │ │ ├── deep/ │ │ │ │ ├── __init__.py │ │ │ │ ├── evaluate.py │ │ │ │ ├── feature_extractor.py │ │ │ │ ├── model.py │ │ │ │ ├── original_model.py │ │ │ │ ├── test.py │ │ │ │ └── train.py │ │ │ ├── deep_sort.py │ │ │ ├── parser.py │ │ │ └── sort/ │ │ │ ├── __init__.py │ │ │ ├── detection.py │ │ │ ├── iou_matching.py │ │ │ ├── kalman_filter.py │ │ │ ├── linear_assignment.py │ │ │ ├── nn_matching.py │ │ │ ├── preprocessing.py │ │ │ ├── track.py │ │ │ └── tracker.py │ │ ├── motpy/ │ │ │ ├── __init__.py │ │ │ └── motpy.py │ │ ├── nor_fair/ │ │ │ ├── __init__.py │ │ │ └── norfair.py │ │ ├── oc_sort/ │ │ │ ├── __init__.py │ │ │ ├── ocsort.py │ │ │ └── tracker/ │ │ │ ├── __init__.py │ │ │ ├── association.py │ │ │ ├── kalmanfilter.py │ │ │ └── ocsort.py │ │ ├── strong_sort/ │ │ │ ├── __init__.py │ │ │ ├── strongsort.py │ │ │ └── tracker/ │ │ │ ├── __init__.py │ │ │ ├── configs/ │ │ │ │ └── strong_sort.yaml │ │ │ ├── deep/ │ │ │ │ ├── __init__.py │ │ │ │ ├── checkpoint/ │ │ │ │ │ └── .gitkeep │ │ │ │ ├── reid/ │ │ │ │ │ ├── .flake8 │ │ │ │ │ ├── .gitignore │ │ │ │ │ ├── .isort.cfg │ │ │ │ │ ├── .style.yapf │ │ │ │ │ ├── LICENSE │ │ │ │ │ ├── README.rst │ │ │ │ │ ├── configs/ │ │ │ │ │ │ ├── im_osnet_ain_x1_0_softmax_256x128_amsgrad_cosine.yaml │ │ │ │ │ │ ├── im_osnet_ibn_x1_0_softmax_256x128_amsgrad.yaml │ │ │ │ │ │ ├── im_osnet_x0_25_softmax_256x128_amsgrad.yaml │ │ │ │ │ │ ├── im_osnet_x0_5_softmax_256x128_amsgrad.yaml │ │ │ │ │ │ ├── im_osnet_x0_75_softmax_256x128_amsgrad.yaml │ │ │ │ │ │ ├── im_osnet_x1_0_softmax_256x128_amsgrad.yaml │ │ │ │ │ │ ├── im_osnet_x1_0_softmax_256x128_amsgrad_cosine.yaml │ │ │ │ │ │ ├── im_r50_softmax_256x128_amsgrad.yaml │ │ │ │ │ │ └── im_r50fc512_softmax_256x128_amsgrad.yaml │ │ │ │ │ ├── docs/ │ │ │ │ │ │ ├── AWESOME_REID.md │ │ │ │ │ │ ├── MODEL_ZOO.md │ │ │ │ │ │ ├── Makefile │ │ │ │ │ │ ├── conf.py │ │ │ │ │ │ ├── datasets.rst │ │ │ │ │ │ ├── evaluation.rst │ │ │ │ │ │ ├── index.rst │ │ │ │ │ │ ├── pkg/ │ │ │ │ │ │ │ ├── data.rst │ │ │ │ │ │ │ ├── engine.rst │ │ │ │ │ │ │ ├── losses.rst │ │ │ │ │ │ │ ├── metrics.rst │ │ │ │ │ │ │ ├── models.rst │ │ │ │ │ │ │ ├── optim.rst │ │ │ │ │ │ │ └── utils.rst │ │ │ │ │ │ └── user_guide.rst │ │ │ │ │ ├── linter.sh │ │ │ │ │ ├── projects/ │ │ │ │ │ │ ├── DML/ │ │ │ │ │ │ │ ├── README.md │ │ │ │ │ │ │ ├── default_config.py │ │ │ │ │ │ │ ├── dml.py │ │ │ │ │ │ │ ├── im_osnet_x1_0_dml_256x128_amsgrad_cosine.yaml │ │ │ │ │ │ │ └── main.py │ │ │ │ │ │ ├── OSNet_AIN/ │ │ │ │ │ │ │ ├── README.md │ │ │ │ │ │ │ ├── default_config.py │ │ │ │ │ │ │ ├── main.py │ │ │ │ │ │ │ ├── nas.yaml │ │ │ │ │ │ │ ├── osnet_child.py │ │ │ │ │ │ │ ├── osnet_search.py │ │ │ │ │ │ │ └── softmax_nas.py │ │ │ │ │ │ ├── README.md │ │ │ │ │ │ └── attribute_recognition/ │ │ │ │ │ │ ├── README.md │ │ │ │ │ │ ├── datasets/ │ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ │ ├── dataset.py │ │ │ │ │ │ │ └── pa100k.py │ │ │ │ │ │ ├── default_parser.py │ │ │ │ │ │ ├── main.py │ │ │ │ │ │ ├── models/ │ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ │ └── osnet.py │ │ │ │ │ │ └── train.sh │ │ │ │ │ ├── scripts/ │ │ │ │ │ │ ├── default_config.py │ │ │ │ │ │ └── main.py │ │ │ │ │ ├── setup.py │ │ │ │ │ ├── tools/ │ │ │ │ │ │ ├── compute_mean_std.py │ │ │ │ │ │ ├── parse_test_res.py │ │ │ │ │ │ └── visualize_actmap.py │ │ │ │ │ └── torchreid/ │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── data/ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ ├── datamanager.py │ │ │ │ │ │ ├── datasets/ │ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ │ ├── dataset.py │ │ │ │ │ │ │ ├── image/ │ │ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ │ │ ├── cuhk01.py │ │ │ │ │ │ │ │ ├── cuhk02.py │ │ │ │ │ │ │ │ ├── cuhk03.py │ │ │ │ │ │ │ │ ├── cuhksysu.py │ │ │ │ │ │ │ │ ├── dukemtmcreid.py │ │ │ │ │ │ │ │ ├── grid.py │ │ │ │ │ │ │ │ ├── ilids.py │ │ │ │ │ │ │ │ ├── market1501.py │ │ │ │ │ │ │ │ ├── msmt17.py │ │ │ │ │ │ │ │ ├── prid.py │ │ │ │ │ │ │ │ ├── sensereid.py │ │ │ │ │ │ │ │ ├── university1652.py │ │ │ │ │ │ │ │ └── viper.py │ │ │ │ │ │ │ └── video/ │ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ │ ├── dukemtmcvidreid.py │ │ │ │ │ │ │ ├── ilidsvid.py │ │ │ │ │ │ │ ├── mars.py │ │ │ │ │ │ │ └── prid2011.py │ │ │ │ │ │ ├── sampler.py │ │ │ │ │ │ └── transforms.py │ │ │ │ │ ├── engine/ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ ├── engine.py │ │ │ │ │ │ ├── image/ │ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ │ ├── softmax.py │ │ │ │ │ │ │ └── triplet.py │ │ │ │ │ │ └── video/ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ ├── softmax.py │ │ │ │ │ │ └── triplet.py │ │ │ │ │ ├── losses/ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ ├── cross_entropy_loss.py │ │ │ │ │ │ └── hard_mine_triplet_loss.py │ │ │ │ │ ├── metrics/ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ ├── accuracy.py │ │ │ │ │ │ ├── distance.py │ │ │ │ │ │ ├── rank.py │ │ │ │ │ │ └── rank_cylib/ │ │ │ │ │ │ ├── Makefile │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ ├── rank_cy.pyx │ │ │ │ │ │ ├── setup.py │ │ │ │ │ │ └── test_cython.py │ │ │ │ │ ├── models/ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ ├── densenet.py │ │ │ │ │ │ ├── hacnn.py │ │ │ │ │ │ ├── inceptionresnetv2.py │ │ │ │ │ │ ├── inceptionv4.py │ │ │ │ │ │ ├── mlfn.py │ │ │ │ │ │ ├── mobilenetv2.py │ │ │ │ │ │ ├── mudeep.py │ │ │ │ │ │ ├── nasnet.py │ │ │ │ │ │ ├── osnet.py │ │ │ │ │ │ ├── osnet_ain.py │ │ │ │ │ │ ├── pcb.py │ │ │ │ │ │ ├── resnet.py │ │ │ │ │ │ ├── resnet_ibn_a.py │ │ │ │ │ │ ├── resnet_ibn_b.py │ │ │ │ │ │ ├── resnetmid.py │ │ │ │ │ │ ├── senet.py │ │ │ │ │ │ ├── shufflenet.py │ │ │ │ │ │ ├── shufflenetv2.py │ │ │ │ │ │ ├── squeezenet.py │ │ │ │ │ │ └── xception.py │ │ │ │ │ ├── optim/ │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ ├── lr_scheduler.py │ │ │ │ │ │ ├── optimizer.py │ │ │ │ │ │ └── radam.py │ │ │ │ │ └── utils/ │ │ │ │ │ ├── GPU-Re-Ranking/ │ │ │ │ │ │ ├── README.md │ │ │ │ │ │ ├── extension/ │ │ │ │ │ │ │ ├── adjacency_matrix/ │ │ │ │ │ │ │ │ ├── build_adjacency_matrix.cpp │ │ │ │ │ │ │ │ ├── build_adjacency_matrix_kernel.cu │ │ │ │ │ │ │ │ └── setup.py │ │ │ │ │ │ │ ├── make.sh │ │ │ │ │ │ │ └── propagation/ │ │ │ │ │ │ │ ├── gnn_propagate.cpp │ │ │ │ │ │ │ ├── gnn_propagate_kernel.cu │ │ │ │ │ │ │ └── setup.py │ │ │ │ │ │ ├── gnn_reranking.py │ │ │ │ │ │ ├── main.py │ │ │ │ │ │ └── utils.py │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── avgmeter.py │ │ │ │ │ ├── feature_extractor.py │ │ │ │ │ ├── loggers.py │ │ │ │ │ ├── model_complexity.py │ │ │ │ │ ├── reidtools.py │ │ │ │ │ ├── rerank.py │ │ │ │ │ ├── tools.py │ │ │ │ │ └── torchtools.py │ │ │ │ └── reid_model_factory.py │ │ │ ├── sort/ │ │ │ │ ├── __init__.py │ │ │ │ ├── detection.py │ │ │ │ ├── iou_matching.py │ │ │ │ ├── kalman_filter.py │ │ │ │ ├── linear_assignment.py │ │ │ │ ├── nn_matching.py │ │ │ │ ├── preprocessing.py │ │ │ │ ├── track.py │ │ │ │ └── tracker.py │ │ │ ├── strong_sort.py │ │ │ └── utils/ │ │ │ ├── __init__.py │ │ │ ├── asserts.py │ │ │ ├── draw.py │ │ │ ├── evaluation.py │ │ │ ├── io.py │ │ │ ├── json_logger.py │ │ │ ├── log.py │ │ │ ├── parser.py │ │ │ └── tools.py │ │ └── tracker.py │ ├── utils/ │ │ ├── __init__.py │ │ ├── classes.py │ │ ├── colors.py │ │ ├── counting.py │ │ ├── default_cfg.py │ │ ├── download.py │ │ ├── draw.py │ │ ├── ponits_conversion.py │ │ ├── pose_estimators_weights.py │ │ ├── temp_loader.py │ │ ├── utils.py │ │ ├── video_reader.py │ │ └── weights.json │ └── windows/ │ ├── README.md │ ├── cam2ip-1.6-64bit-cv/ │ │ ├── AUTHORS │ │ └── COPYING │ ├── enable_feature.bat │ ├── installation.bat │ ├── test-display.py │ └── test-webcam.py ├── docker-compose.yml ├── main.py ├── requirements.txt └── setup.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .dockerignore ================================================ .env/ results/ **__pycache__** *.onnx *.pt *.mlmodel **byte_track_results** **deep_sort_results** **nor_fair_results** test_env/ ================================================ FILE: .gitignore ================================================ .env/ **__pycache__** *.onnx *.pt *.pth *.mlmodel **byte_track_results** **deep_sort_results** **nor_fair_results** build/ dist/ asone.egg-info/ test_custom_tracker.py custom_weights.py data/results/ temp/ ================================================ FILE: Dockerfile ================================================ FROM pytorch/pytorch:latest # Set Time Zone to prevent issues for installing some apt packages ENV TZ=Europe/Minsk RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # install apt packages RUN apt-get update -y RUN apt-get install git gcc \ g++ python3-opencv \ vim -y RUN mkdir /app WORKDIR /app ADD asone asone ADD sample_videos sample_videos ADD main.py main.py # ADD demo.py demo.py ADD setup.py setup.py ADD requirements.txt requirements.txt RUN pip3 install Cython numpy RUN pip3 install cython-bbox ADD pypi_README.md pypi_README.md RUN pip3 install torch torchvision --extra-index-url https://download.pytorch.org/whl/cu113 RUN pip3 install . WORKDIR /workspace # Entry Point CMD /bin/bash ================================================ FILE: LICENCE ================================================ GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . ================================================ FILE: README.md ================================================ # AS-One v2 : A Modular Library for YOLO Object Detection, Segmentation, Tracking & Pose

Watch Video



[![PyPI version](https://badge.fury.io/py/asone.svg)](https://badge.fury.io/py/asone) [![python-version](https://img.shields.io/pypi/pyversions/supervision)](https://badge.fury.io/py/asone) [![colab](https://colab.research.google.com/assets/colab-badge.svg)](https://drive.google.com/file/d/1xy5P9WGI19-PzRH3ceOmoCgp63K6J_Ls/view?usp=sharing) [![start with why](https://img.shields.io/badge/version-2.0.0-green)](https://github.com/augmentedstartups/AS-One) [![GPLv3 License](https://img.shields.io/badge/License-GPL%20v3-yellow.svg)](https://opensource.org/licenses/)
## 👋 Hello ==UPDATE: ASOne v2 is now out! We've updated with YOLOV9 and SAM== AS-One is a python wrapper for multiple detection and tracking algorithms all at one place. Different trackers such as `ByteTrack`, `DeepSORT` or `NorFair` can be integrated with different versions of `YOLO` with minimum lines of code. This python wrapper provides YOLO models in `ONNX`, `PyTorch` & `CoreML` flavors. We plan to offer support for future versions of YOLO when they get released. This is One Library for most of your computer vision needs. If you would like to dive deeper into YOLO Object Detection and Tracking, then check out our [courses](https://www.augmentedstartups.com/store) and [projects](https://store.augmentedstartups.com) [](https://www.youtube.com/watch?v=K-VcpPwcM8k) Watch the step-by-step tutorial 🤝 ## 💻 Install
🔥 Prerequisites - Make sure to install `GPU` drivers in your system if you want to use `GPU` . Follow [driver installation](asone/linux/Instructions/Driver-Installations.md) for further instructions. - Make sure you have [MS Build tools](https://aka.ms/vs/17/release/vs_BuildTools.exe) installed in system if using windows. - [Download git for windows](https://git-scm.com/download/win) if not installed.
```bash pip install asone ``` For windows machine, you will need to install from source to run `asone` library. Check out instructions in `👉 Install from Source` section below to install on windows.
👉 Install from Source ### 💾 Clone the Repository Navigate to an empty folder of your choice. `git clone https://github.com/augmentedstartups/AS-One.git` Change Directory to AS-One `cd AS-One`
👉 For Linux ```shell python3 -m venv .env source .env/bin/activate pip install -r requirements.txt # for CPU pip install torch torchvision # for GPU pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cu113 ```
👉 For Windows 10/11 ```shell python -m venv .env .env\Scripts\activate pip install numpy Cython pip install lap pip install -e git+https://github.com/samson-wang/cython_bbox.git#egg=cython-bbox pip install asone onnxruntime-gpu==1.12.1 pip install typing_extensions==4.7.1 pip install super-gradients==3.1.3 # for CPU pip install torch torchvision # for GPU pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cu113 or pip install torch==1.10.1+cu113 torchvision==0.11.2+cu113 torchaudio===0.10.1+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html ```
👉 For MacOS ```shell python3 -m venv .env source .env/bin/activate pip install -r requirements.txt # for CPU pip install torch torchvision ```
## Quick Start 🏃‍♂️ Use tracker on sample video. ```python import asone from asone import ASOne model = ASOne(tracker=asone.BYTETRACK, detector=asone.YOLOV9_C, use_cuda=True) tracks = model.video_tracker('data/sample_videos/test.mp4', filter_classes=['car']) for model_output in tracks: annotations = ASOne.draw(model_output, display=False) ``` ### Run in `Google Colab` 💻 Open In Colab ## Sample Code Snippets 📃
6.1 👉 Object Detection ```python import asone from asone import ASOne model = ASOne(detector=asone.YOLOV9_C, use_cuda=True) # Set use_cuda to False for cpu vid = model.read_video('data/sample_videos/test.mp4') for img in vid: detection = model.detecter(img) annotations = ASOne.draw(detection, img=img, display=True) ``` Run the `asone/demo_detector.py` to test detector. ```shell # run on gpu python -m asone.demo_detector data/sample_videos/test.mp4 # run on cpu python -m asone.demo_detector data/sample_videos/test.mp4 --cpu ```
6.1.1 👉 Use Custom Trained Weights for Detector Use your custom weights of a detector model trained on custom data by simply providing path of the weights file. ```python import asone from asone import ASOne model = ASOne(detector=asone.YOLOV9_C, weights='data/custom_weights/yolov7_custom.pt', use_cuda=True) # Set use_cuda to False for cpu vid = model.read_video('data/sample_videos/license_video.mp4') for img in vid: detection = model.detecter(img) annotations = ASOne.draw(detection, img=img, display=True, class_names=['license_plate']) ```
6.1.2 👉 Changing Detector Models Change detector by simply changing detector flag. The flags are provided in [benchmark](asone/linux/Instructions/Benchmarking.md) tables. - Our library now supports YOLOv5, YOLOv7, and YOLOv8 on macOS. ```python # Change detector model = ASOne(detector=asone.YOLOX_S_PYTORCH, use_cuda=True) # For macOs # YOLO5 model = ASOne(detector=asone.YOLOV5X_MLMODEL) # YOLO7 model = ASOne(detector=asone.YOLOV7_MLMODEL) # YOLO8 model = ASOne(detector=asone.YOLOV8L_MLMODEL) ```
6.2 👉 Object Tracking Use tracker on sample video. ```python import asone from asone import ASOne # Instantiate Asone object model = ASOne(tracker=asone.BYTETRACK, detector=asone.YOLOV9_C, use_cuda=True) #set use_cuda=False to use cpu tracks = model.video_tracker('data/sample_videos/test.mp4', filter_classes=['car']) # Loop over track to retrieve outputs of each frame for model_output in tracks: annotations = ASOne.draw(model_output, display=True) # Do anything with bboxes here ``` [Note] Use can use custom weights for a detector model by simply providing path of the weights file. in `ASOne` class.
6.2.1 👉 Changing Detector and Tracking Models Change Tracker by simply changing the tracker flag. The flags are provided in [benchmark](asone/linux/Instructions/Benchmarking.md) tables. ```python model = ASOne(tracker=asone.BYTETRACK, detector=asone.YOLOV9_C, use_cuda=True) # Change tracker model = ASOne(tracker=asone.DEEPSORT, detector=asone.YOLOV9_C, use_cuda=True) ``` ```python # Change Detector model = ASOne(tracker=asone.DEEPSORT, detector=asone.YOLOX_S_PYTORCH, use_cuda=True) ```
Run the `asone/demo_tracker.py` to test detector. ```shell # run on gpu python -m asone.demo_tracker data/sample_videos/test.mp4 # run on cpu python -m asone.demo_tracker data/sample_videos/test.mp4 --cpu ```
6.3 👉 Segmentation ```python import asone from asone import ASOne model = ASOne(detector=asone.YOLOV9_C, segmentor=asone.SAM, use_cuda=True) #set use_cuda=False to use cpu tracks = model.video_detecter('data/sample_videos/test.mp4', filter_classes=['car']) for model_output in tracks: annotations = ASOne.draw_masks(model_output, display=True) # Draw masks ```
6.4 👉 Text Detection Sample code to detect text on an image ```python # Detect and recognize text import asone from asone import ASOne, utils import cv2 model = ASOne(detector=asone.CRAFT, recognizer=asone.EASYOCR, use_cuda=True) # Set use_cuda to False for cpu img = cv2.imread('data/sample_imgs/sample_text.jpeg') results = model.detect_text(img) annotations = utils.draw_text(img, results, display=True) ``` Use Tracker on Text ```python import asone from asone import ASOne # Instantiate Asone object model = ASOne(tracker=asone.DEEPSORT, detector=asone.CRAFT, recognizer=asone.EASYOCR, use_cuda=True) #set use_cuda=False to use cpu tracks = model.video_tracker('data/sample_videos/GTA_5-Unique_License_Plate.mp4') # Loop over track to retrieve outputs of each frame for model_output in tracks: annotations = ASOne.draw(model_output, display=True) # Do anything with bboxes here ``` Run the `asone/demo_ocr.py` to test ocr. ```shell # run on gpu python -m asone.demo_ocr data/sample_videos/GTA_5-Unique_License_Plate.mp4 # run on cpu python -m asone.demo_ocr data/sample_videos/GTA_5-Unique_License_Plate.mp4 --cpu ```
6.5 👉 Pose Estimation Sample code to estimate pose on an image ```python # Pose Estimation import asone from asone import PoseEstimator, utils import cv2 model = PoseEstimator(estimator_flag=asone.YOLOV8M_POSE, use_cuda=True) #set use_cuda=False to use cpu img = cv2.imread('data/sample_imgs/test2.jpg') kpts = model.estimate_image(img) annotations = utils.draw_kpts(kpts, image=img, display=True) ``` - Now you can use Yolov8 and Yolov7-w6 for pose estimation. The flags are provided in [benchmark](asone/linux/Instructions/Benchmarking.md) tables. ```python # Pose Estimation on video import asone from asone import PoseEstimator, utils model = PoseEstimator(estimator_flag=asone.YOLOV7_W6_POSE, use_cuda=True) #set use_cuda=False to use cpu estimator = model.video_estimator('data/sample_videos/football1.mp4') for model_output in estimator: annotations = utils.draw_kpts(model_output) # Do anything with kpts here ``` Run the `asone/demo_pose_estimator.py` to test Pose estimation. ```shell # run on gpu python -m asone.demo_pose_estimator data/sample_videos/football1.mp4 # run on cpu python -m asone.demo_pose_estimator data/sample_videos/football1.mp4 --cpu ```
To setup ASOne using Docker follow instructions given in [docker setup](asone/linux/Instructions/Docker-Setup.md)🐳 ### ToDo 📝 - [x] First Release - [x] Import trained models - [x] Simplify code even further - [x] Updated for YOLOv8 - [x] OCR and Counting - [x] OCSORT, StrongSORT, MoTPy - [x] M1/2 Apple Silicon Compatibility - [x] Pose Estimation YOLOv7/v8 - [x] YOLO-NAS - [x] Updated for YOLOv8.1 - [x] YOLOV9 - [x] SAM Integration | Offered By 💼 : | Maintained By 👨‍💻 : | | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------- | | [![AugmentedStarups](https://user-images.githubusercontent.com/107035454/195115263-d3271ef3-973b-40a4-83c8-0ade8727dd40.png)](https://augmentedstartups.com) | [![AxcelerateAI](https://user-images.githubusercontent.com/107035454/195114870-691c8a52-fcf0-462e-9e02-a720fc83b93f.png)](https://axcelerate.ai/) | ================================================ FILE: asone/__init__.py ================================================ from .asone import ASOne import asone.detectors import asone.trackers import asone.recognizers import asone.segmentors from .pose_estimator import PoseEstimator from asone.utils.video_reader import VideoReader BYTETRACK = 0 DEEPSORT = 1 NORFAIR = 2 MOTPY = 3 OCSORT = 4 STRONGSORT = 5 YOLOV5X6_PYTORCH = 0 YOLOV5S_PYTORCH = 2 YOLOV5N_PYTORCH = 4 YOLOV5M_PYTORCH = 6 YOLOV5L_PYTORCH = 8 YOLOV5X_PYTORCH = 10 YOLOV5N6_PYTORCH = 12 YOLOV5S6_PYTORCH = 14 YOLOV5M6_PYTORCH = 16 YOLOV5L6_PYTORCH = 18 YOLOV6N_PYTORCH = 20 YOLOV6T_PYTORCH = 22 YOLOV6S_PYTORCH = 24 YOLOV6M_PYTORCH = 26 YOLOV6L_PYTORCH = 28 YOLOV6L_RELU_PYTORCH = 30 YOLOV6S_REPOPT_PYTORCH = 32 YOLOV7_TINY_PYTORCH = 34 YOLOV7_PYTORCH = 36 YOLOV7_X_PYTORCH = 38 YOLOV7_W6_PYTORCH = 40 YOLOV7_E6_PYTORCH = 42 YOLOV7_D6_PYTORCH = 44 YOLOV7_E6E_PYTORCH = 46 YOLOR_CSP_X_PYTORCH = 48 YOLOR_CSP_X_STAR_PYTORCH = 50 YOLOR_CSP_STAR_PYTORCH = 52 YOLOR_CSP_PYTORCH = 54 YOLOR_P6_PYTORCH = 56 YOLOX_L_PYTORCH = 58 YOLOX_NANO_PYTORCH = 60 YOLOX_TINY_PYTORCH = 62 YOLOX_DARKNET_PYTORCH = 64 YOLOX_S_PYTORCH = 66 YOLOX_M_PYTORCH = 68 YOLOX_X_PYTORCH = 70 #ONNX YOLOV5X6_ONNX = 1 YOLOV5S_ONNX = 3 YOLOV5N_ONNX = 5 YOLOV5M_ONNX = 7 YOLOV5L_ONNX = 9 YOLOV5X_ONNX = 11 YOLOV5N6_ONNX = 13 YOLOV5S6_ONNX = 15 YOLOV5M6_ONNX = 17 YOLOV5L6_ONNX = 19 YOLOV6N_ONNX = 21 YOLOV6T_ONNX = 23 YOLOV6S_ONNX = 25 YOLOV6M_ONNX = 27 YOLOV6L_ONNX = 29 YOLOV6L_RELU_ONNX = 31 YOLOV6S_REPOPT_ONNX = 33 YOLOV7_TINY_ONNX = 35 YOLOV7_ONNX = 37 YOLOV7_X_ONNX = 39 YOLOV7_W6_ONNX = 41 YOLOV7_E6_ONNX = 43 YOLOV7_D6_ONNX = 45 YOLOV7_E6E_ONNX = 47 YOLOR_CSP_X_ONNX = 49 YOLOR_CSP_X_STAR_ONNX = 51 YOLOR_CSP_STAR_ONNX = 53 YOLOR_CSP_ONNX = 55 YOLOR_P6_ONNX = 57 YOLOX_L_ONNX = 59 YOLOX_NANO_ONNX = 61 YOLOX_TINY_ONNX = 63 YOLOX_DARKNET_ONNX = 65 YOLOX_S_ONNX = 67 YOLOX_M_ONNX = 69 YOLOX_X_ONNX = 71 # YOLOv8 YOLOV8N_PYTORCH = 72 YOLOV8N_ONNX = 73 YOLOV8S_PYTORCH = 74 YOLOV8S_ONNX = 75 YOLOV8M_PYTORCH = 76 YOLOV8M_ONNX = 77 YOLOV8L_PYTORCH = 78 YOLOV8L_ONNX = 79 YOLOV8X_PYTORCH = 80 YOLOV8X_ONNX = 81 # coreml YOLOV5N_MLMODEL = 120 YOLOV5S_MLMODEL = 121 YOLOV5X6_MLMODEL = 122 YOLOV5M_MLMODEL = 123 YOLOV5L_MLMODEL = 124 YOLOV5X_MLMODEL = 125 YOLOV5N6_MLMODEL = 126 YOLOV5S6_MLMODEL = 127 YOLOV5M6_MLMODEL = 128 YOLOV5L6_MLMODEL = 129 YOLOV7_TINY_MLMODEL = 130 YOLOV7_MLMODEL = 131 YOLOV7_X_MLMODEL = 132 YOLOV7_W6_MLMODEL = 133 YOLOV7_E6_MLMODEL = 134 YOLOV7_D6_MLMODEL = 135 YOLOV7_E6E_MLMODEL = 136 YOLOV8N_MLMODEL = 139 YOLOV8S_MLMODEL = 140 YOLOV8M_MLMODEL = 141 YOLOV8L_MLMODEL = 142 YOLOV8X_MLMODEL = 143 YOLOV8N_POSE = 144 YOLOV8S_POSE = 145 YOLOV8M_POSE = 146 YOLOV8L_POSE = 147 YOLOV8X_POSE = 148 YOLOV7_W6_POSE = 149 YOLONAS_S_PYTORCH = 160 YOLONAS_M_PYTORCH = 161 YOLONAS_L_PYTORCH = 162 # YOLOv9 YOLOV9_C_CONVERTED = 164 YOLOV9_E_CONVERTED = 165 YOLOV9_C = 166 YOLOV9_E = 167 GELAN_C = 168 GELAN_E = 169 # Segmentors SAM = 171 # Text Detectors # easyocr CRAFT = 82 DBNET18 = 83 # Text Recognizers EASYOCR = 200 __all__ = ['ASOne', 'detectors', 'trackers', 'recognizers', 'segmentors', 'PoseEstimator'] ================================================ FILE: asone/asone.py ================================================ import copy import warnings import cv2 from loguru import logger import os import time import asone.utils as utils from asone.trackers import Tracker from asone.detectors import Detector from asone.recognizers import TextRecognizer from asone.segmentors import Segmentor from asone.utils.default_cfg import config from asone.utils.video_reader import VideoReader from asone.utils import compute_color_for_labels from asone.schemas.output_schemas import ModelOutput import numpy as np class ASOne: def __init__(self, detector: int = 0, tracker: int = -1, segmentor: int = -1, weights: str = None, segmentor_weights: str = None, use_cuda: bool = True, recognizer: int = None, languages: list = ['en'], num_classes=80 ) -> None: self.use_cuda = use_cuda self.use_segmentation = False self.model_output = ModelOutput() # Check if user want to use segmentor if segmentor != -1: self.use_segmentation = True # Load Segmentation model self.segmentor = self.get_segmentor(segmentor, segmentor_weights) # get detector object self.detector = self.get_detector(detector, weights, recognizer, num_classes) self.recognizer = self.get_recognizer(recognizer, languages=languages) if tracker == -1: self.tracker = None return self.tracker = self.get_tracker(tracker) def get_detector(self, detector: int, weights: str, recognizer, num_classes): detector = Detector(detector, weights=weights, use_cuda=self.use_cuda, recognizer=recognizer, num_classes=num_classes).get_detector() return detector def get_recognizer(self, recognizer: int, languages): if recognizer == None: return None recognizer = TextRecognizer(recognizer, use_cuda=self.use_cuda, languages=languages).get_recognizer() return recognizer def get_tracker(self, tracker: int): tracker = Tracker(tracker, self.detector, use_cuda=self.use_cuda) return tracker def get_segmentor(self, segmentor, segmentor_weights): segmentor = Segmentor(segmentor, segmentor_weights, self.use_cuda) return segmentor def _update_args(self, kwargs): for key, value in kwargs.items(): if key in config.keys(): config[key] = value else: print(f'"{key}" argument not found! valid args: {list(config.keys())}') exit() return config def track_stream(self, stream_url, **kwargs ): # Emit the warning for DeprecationWarning with warnings.catch_warnings(): warnings.simplefilter("always", DeprecationWarning) warnings.warn("track_stream function is deprecated. Kindly use stream_tracker instead", DeprecationWarning) output_filename = 'result.mp4' kwargs['filename'] = output_filename config = self._update_args(kwargs) for (bbox_details, frame_details) in self._start_tracking(stream_url, config): # yeild bbox_details, frame_details to main script yield bbox_details, frame_details def stream_tracker(self, stream_url, **kwargs ): output_filename = 'result.mp4' kwargs['filename'] = output_filename config = self._update_args(kwargs) for (bbox_details, frame_details) in self._start_tracking(stream_url, config): # yeild bbox_details, frame_details to main script yield self.format_output(bbox_details, frame_details) def track_video(self, video_path, **kwargs ): # Emit the warning for DeprecationWarning with warnings.catch_warnings(): warnings.simplefilter("always", DeprecationWarning) warnings.warn("track_video function is deprecated. Kindly use video_tracker instead", DeprecationWarning) output_filename = os.path.basename(video_path) kwargs['filename'] = output_filename config = self._update_args(kwargs) for (bbox_details, frame_details) in self._start_tracking(video_path, config): # yeild bbox_details, frame_details to main script yield bbox_details, frame_details def video_tracker(self, video_path, **kwargs ): output_filename = os.path.basename(video_path) kwargs['filename'] = output_filename config = self._update_args(kwargs) for (bbox_details, frame_details) in self._start_tracking(video_path, config): # yeild bbox_details, frame_details to main script yield self.format_output(bbox_details, frame_details) def detect_video(self, video_path, **kwargs ): # Emit the warning for DeprecationWarning with warnings.catch_warnings(): warnings.simplefilter("always", DeprecationWarning) warnings.warn("detect_video function is deprecated. Kindly use video_detecter instead", DeprecationWarning) output_filename = os.path.basename(video_path) kwargs['filename'] = output_filename config = self._update_args(kwargs) for (bbox_details, frame_details) in self._start_tracking(video_path, config): # yeild bbox_details, frame_details to main script yield bbox_details, frame_details def video_detecter(self, video_path, **kwargs ): output_filename = os.path.basename(video_path) kwargs['filename'] = output_filename config = self._update_args(kwargs) for (bbox_details, frame_details) in self._start_tracking(video_path, config): # yeild bbox_details, frame_details to main script yield self.format_output(bbox_details, frame_details) def detect(self, source, **kwargs)->np.ndarray: """ Function to perform detection on an img Args: source (_type_): if str read the image. if nd.array pass it directly to detect Returns: _type_: ndarray of detection """ # Emit the warning for DeprecationWarning with warnings.catch_warnings(): warnings.simplefilter("always", DeprecationWarning) warnings.warn("detect function is deprecated. Kindly use detecter instead", DeprecationWarning) if isinstance(source, str): source = cv2.imread(source) return self.detector.detect(source, **kwargs) def detecter(self, source, **kwargs): """ Function to perform detection on an img Args: source (_type_): if str read the image. if nd.array pass it directly to detect Returns: _type_: ndarray of detection """ if isinstance(source, str): source = cv2.imread(source) dets, _ = self.detector.detect(source, **kwargs) bboxes_xyxy = dets[:, :4] scores = dets[:, 4] class_ids = dets[:, 5] ids = None info = None return self.format_output((bboxes_xyxy, ids, scores, class_ids), info) def detect_and_track(self, frame, **kwargs): if self.tracker: bboxes_xyxy, ids, scores, class_ids = self.tracker.detect_and_track( frame, kwargs) info = None else: dets, info = self.detect(source=frame, **kwargs) bboxes_xyxy = dets[:, :4] scores = dets[:, 4] class_ids = dets[:, 5] ids = None return (bboxes_xyxy, ids, scores, class_ids), info def detect_track_manager(self, frame, **kwargs): if self.tracker: bboxes_xyxy, ids, scores, class_ids = self.tracker.detect_and_track( frame, kwargs) info = None else: model_output = self.detecter(source=frame, **kwargs) info = model_output.info bboxes_xyxy = model_output.dets.bbox scores = model_output.dets.score class_ids = model_output.dets.class_ids ids = None return (bboxes_xyxy, ids, scores, class_ids), info def detect_text(self, image): horizontal_list, _ = self.detector.detect(image) if self.recognizer is None: raise TypeError("Recognizer can not be None") return self.recognizer.recognize(image, horizontal_list=horizontal_list, free_list=[]) def track_webcam(self, cam_id=0, **kwargs): # Emit the warning for DeprecationWarning with warnings.catch_warnings(): warnings.simplefilter("always", DeprecationWarning) warnings.warn("track_webcam function is deprecated. Kindly use webcam_tracker instead", DeprecationWarning) output_filename = 'results.mp4' kwargs['filename'] = output_filename kwargs['fps'] = 29 config = self._update_args(kwargs) for (bbox_details, frame_details) in self._start_tracking(cam_id, config): # yeild bbox_details, frame_details to main script yield bbox_details, frame_details def webcam_tracker(self, cam_id=0, **kwargs): output_filename = 'results.mp4' kwargs['filename'] = output_filename kwargs['fps'] = 29 config = self._update_args(kwargs) for (bbox_details, frame_details) in self._start_tracking(cam_id, config): # yeild bbox_details, frame_details to main script yield self.format_output(bbox_details, frame_details) def _start_tracking(self, stream_path: str, config: dict) -> tuple: if not self.tracker: warnings.warn(f'No tracker has been selected. Only the detector is operational.') fps = config.pop('fps') output_dir = config.pop('output_dir') filename = config.pop('filename') save_result = config.pop('save_result') display = config.pop('display') draw_trails = config.pop('draw_trails') class_names = config.pop('class_names') cap = self.read_video(stream_path) width, height = cap.frame_size frame_count = cap.frame_counts if fps is None: fps = cap.fps if save_result: os.makedirs(output_dir, exist_ok=True) save_path = os.path.join(output_dir, filename) logger.info(f"video save path is {save_path}") video_writer = cv2.VideoWriter( save_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (int(width), int(height)), ) frame_id = 1 tic = time.time() prevTime = 0 for frame in cap: start_time = time.time() im0 = copy.deepcopy(frame) try: (bboxes_xyxy, ids, scores, class_ids), _ = self.detect_track_manager(frame, **config) except: (bboxes_xyxy, ids, scores, class_ids), _ = self.detect_and_track(frame, **config) elapsed_time = time.time() - start_time logger.info( 'frame {}/{} ({:.2f} ms)'.format(frame_id, int(frame_count), elapsed_time * 1000)) if self.recognizer: res = self.recognizer.recognize(frame, horizontal_list=bboxes_xyxy, free_list=[]) im0 = utils.draw_text(im0, res) else: im0 = self.draw((bboxes_xyxy, ids, scores, class_ids), img=im0, draw_trails=draw_trails, class_names=class_names, display=display) currTime = time.time() fps = 1 / (currTime - prevTime) prevTime = currTime cv2.line(im0, (20, 25), (127, 25), [85, 45, 255], 30) cv2.putText(im0, f'FPS: {int(fps)}', (11, 35), 0, 1, [ 225, 255, 255], thickness=2, lineType=cv2.LINE_AA) if self.use_segmentation: if len(bboxes_xyxy) > 0: # Check if bounding box is present or not # Will generate mask using SAM masks = self.segmentor.create_mask(np.array(bboxes_xyxy), frame) im0 = self.draw_masks(masks, img=im0, display=display) bboxes_xyxy = (bboxes_xyxy, masks) if save_result: video_writer.write(im0) frame_id += 1 if cv2.waitKey(25) & 0xFF == ord('q'): break # yeild required values in form of (bbox_details, frames_details) yield (bboxes_xyxy, ids, scores, class_ids), (im0 if display else frame, frame_id-1, fps) tac = time.time() print(f'Total Time Taken: {tac - tic:.2f}') @staticmethod def draw(dets, display=False, img=None, **kwargs): draw_trails = kwargs.get('draw_trails', False) class_names = kwargs.get('class_names', None) if isinstance(dets, tuple): bboxes_xyxy, ids, scores, class_ids = dets if isinstance(bboxes_xyxy, tuple): bboxes_xyxy, _ = bboxes_xyxy elif isinstance(dets, np.ndarray): bboxes_xyxy = dets[:, :4] scores = dets[:, 4] class_ids = dets[:, 5] ids = None elif isinstance(dets, ModelOutput): bboxes_xyxy = dets.dets.bbox ids = dets.dets.ids score = dets.dets.score class_ids = dets.dets.class_ids img = dets.info.image if dets.info.image is not None else img frame_no = dets.info.frame_no fps = dets.info.fps img = utils.draw_boxes(img, bbox_xyxy=bboxes_xyxy, class_ids=class_ids, identities=ids, draw_trails=draw_trails, class_names=class_names) if display: cv2.imshow(' Sample', img) return img @staticmethod def draw_masks(dets, display, img=None, **kwargs): # Check if bounding box are present if isinstance(dets, tuple) and len(dets) > 0 and len(dets[0]) == 0: return img elif isinstance(dets, ModelOutput): masks = dets.dets.bbox ids = dets.dets.ids score = dets.dets.score class_ids = dets.dets.class_ids img = dets.info.image if dets.info.image is not None else img frame_no = dets.info.frame_no fps = dets.info.fps if isinstance(masks, tuple): bboxes_xyxy, masks = masks if isinstance(masks, np.ndarray): return img elif isinstance(dets, tuple): bboxes_xyxy, ids, scores, class_ids = dets if isinstance(bboxes_xyxy, tuple): bboxes_xyxy, masks = bboxes_xyxy else: masks = dets class_ids = None color = [0, 255, 0] masked_image = img.copy() for idx in range(len(masks)): mask = masks[idx].squeeze() # Squeeze to remove singleton dimension if class_ids is not None: color = compute_color_for_labels(int(class_ids[idx])) color = np.asarray(color, dtype='uint8') mask_color = np.expand_dims(mask, axis=-1) * color # Apply color to the mask # Apply the mask to the image masked_image = np.where(mask_color > 0, mask_color, masked_image) masked_image = masked_image.astype(np.uint8) img = cv2.addWeighted(img, 0.5, masked_image, 0.5, 0) if display: cv2.imshow(' Sample', img) return img def read_video(self, video_path): vid = VideoReader(video_path) return vid def format_output(self, bbox_details, frame_details): # Set detections self.model_output.dets.bbox = bbox_details[0] self.model_output.dets.ids = bbox_details[1] self.model_output.dets.score = bbox_details[2] self.model_output.dets.class_ids = bbox_details[3] if frame_details: # Set image info self.model_output.info.image = frame_details[0] self.model_output.info.frame_no = frame_details[1] self.model_output.info.fps = frame_details[2] return self.model_output if __name__ == '__main__': # asone = ASOne(tracker='norfair') asone = ASOne() asone.start_tracking('data/sample_videos/video2.mp4', save_result=True, display=False) ================================================ FILE: asone/demo_detector.py ================================================ import sys import argparse import asone from asone import ASOne import torch def main(args): filter_classes = args.filter_classes if filter_classes: filter_classes = ['person'] # Check if cuda available if args.use_cuda and torch.cuda.is_available(): args.use_cuda = True else: args.use_cuda = False if sys.platform.startswith('darwin'): detector = asone.YOLOV7_MLMODEL else: detector = asone.YOLOV7_PYTORCH detect = ASOne( detector=detector, weights=args.weights, use_cuda=args.use_cuda ) # Get tracking function track = detect.detect_video(args.video_path, output_dir=args.output_dir, conf_thres=args.conf_thres, iou_thres=args.iou_thres, display=args.display, filter_classes=filter_classes, class_names=None) # class_names=['License Plate'] for custom weights # Loop over track_fn to retrieve outputs of each frame for bbox_details, frame_details in track: bbox_xyxy, ids, scores, class_ids = bbox_details frame, frame_num, fps = frame_details print(frame_num) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('video_path', help='Path to input video') parser.add_argument('--cpu', default=True, action='store_false', dest='use_cuda', help='run on cpu if not provided the program will run on gpu.') parser.add_argument('--no_save', default=True, action='store_false', dest='save_result', help='whether or not save results') parser.add_argument('--no_display', default=True, action='store_false', dest='display', help='whether or not display results on screen') parser.add_argument('--output_dir', default='data/results', help='Path to output directory') parser.add_argument('--draw_trails', action='store_true', default=False, help='if provided object motion trails will be drawn.') parser.add_argument('--filter_classes', default=None, help='Filter class name') parser.add_argument('-w', '--weights', default=None, help='Path of trained weights') parser.add_argument('-ct', '--conf_thres', default=0.25, type=float, help='confidence score threshold') parser.add_argument('-it', '--iou_thres', default=0.45, type=float, help='iou score threshold') args = parser.parse_args() main(args) ================================================ FILE: asone/demo_ocr.py ================================================ import argparse import asone from asone import ASOne def main(args): detect = ASOne( tracker=asone.DEEPSORT, detector=asone.CRAFT, weights=args.weights, recognizer=asone.EASYOCR, use_cuda=args.use_cuda ) # Get tracking function track = detect.track_video(args.video_path, output_dir=args.output_dir, conf_thres=args.conf_thres, iou_thres=args.iou_thres, display=args.display, draw_trails=args.draw_trails) # class_names=['License Plate'] for custom weights # Loop over track to retrieve outputs of each frame for bbox_details, frame_details in track: bbox_xyxy, ids, scores, class_ids = bbox_details frame, frame_num, fps = frame_details print(frame_num) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('video_path', help='Path to input video') parser.add_argument('--cpu', default=True, action='store_false', dest='use_cuda', help='run on cpu if not provided the program will run on gpu.') parser.add_argument('--no_save', default=True, action='store_false', dest='save_result', help='whether or not save results') parser.add_argument('--no_display', default=True, action='store_false', dest='display', help='whether or not display results on screen') parser.add_argument('--output_dir', default='data/results', help='Path to output directory') parser.add_argument('--draw_trails', action='store_true', default=False, help='if provided object motion trails will be drawn.') parser.add_argument('-w', '--weights', default=None, help='Path of trained weights') parser.add_argument('-ct', '--conf_thres', default=0.25, type=float, help='confidence score threshold') parser.add_argument('-it', '--iou_thres', default=0.45, type=float, help='iou score threshold') args = parser.parse_args() main(args) ================================================ FILE: asone/demo_pose_estimator.py ================================================ import asone from asone import PoseEstimator from .utils import draw_kpts import cv2 import argparse import time import os def main(args): video_path = args.video os.makedirs(args.output_path, exist_ok=True) estimator = PoseEstimator(asone.YOLOV7_W6_POSE, weights=args.weights, use_cuda=args.use_cuda) cap = cv2.VideoCapture(video_path) width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) FPS = cap.get(cv2.CAP_PROP_FPS) if args.save: video_writer = cv2.VideoWriter( os.path.join(args.output_path,os.path.basename(video_path)), cv2.VideoWriter_fourcc(*"mp4v"), FPS, (int(width), int(height)), ) frame_no = 1 tic = time.time() prevTime = 0 fframe_num = 0 while True: start_time = time.time() ret, img = cap.read() if not ret: break frame = img.copy() kpts = estimator.estimate_image(img) currTime = time.time() fps = 1 / (currTime - prevTime) prevTime = currTime if kpts is not None: img = draw_kpts(img, kpts) cv2.line(img, (20, 25), (127, 25), [85, 45, 255], 30) cv2.putText(img, f'FPS: {int(fps)}', (11, 35), 0, 1, [ 225, 255, 255], thickness=2, lineType=cv2.LINE_AA) frame_no+=1 if args.display: cv2.imshow('Window', img) if args.save: video_writer.write(img) # frame_no += 1 print(frame_no) if cv2.waitKey(25) & 0xFF == ord('q'): break if __name__=='__main__': parser = argparse.ArgumentParser() parser.add_argument("video", help="Path of video") parser.add_argument('--cpu', default=True, action='store_false', dest='use_cuda', help='If provided the model will run on cpu otherwise it will run on gpu') parser.add_argument('-w', '--weights', default=None, help='Path of trained weights') parser.add_argument('-o', '--output_path', default='data/results', help='path of output file') parser.add_argument('--no_display', action='store_false', default=True, dest='display', help='if provided video will not be displayed') parser.add_argument('--no_save', action='store_false', default=True, dest='save', help='if provided video will not be saved') args = parser.parse_args() main(args) ================================================ FILE: asone/demo_segmentor.py ================================================ import sys import argparse import asone from asone import ASOne import torch def main(args): filter_classes = args.filter_classes if filter_classes: filter_classes = ['person'] # Check if cuda available if args.use_cuda and torch.cuda.is_available(): args.use_cuda = True else: args.use_cuda = False if sys.platform.startswith('darwin'): detector = asone.YOLOV7_MLMODEL else: detector = asone.YOLOV7_PYTORCH detect = ASOne( detector=detector, segmentor=asone.SAM, weights=args.weights, segmentor_weights=args.segmentor_weights, use_cuda=args.use_cuda ) # Get tracking function track = detect.detect_video(args.video_path, output_dir=args.output_dir, conf_thres=args.conf_thres, iou_thres=args.iou_thres, display=args.display, draw_trails=args.draw_trails, filter_classes=filter_classes, class_names=None) # class_names=['License Plate'] for custom weights # Loop over track_fn to retrieve outputs of each frame for bbox_details, frame_details in track: bbox_xyxy, ids, scores, class_ids = bbox_details frame, frame_num, fps = frame_details print(frame_num) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('video_path', help='Path to input video') parser.add_argument('--cpu', default=True, action='store_false', dest='use_cuda', help='run on cpu if not provided the program will run on gpu.') parser.add_argument('--no_save', default=True, action='store_false', dest='save_result', help='whether or not save results') parser.add_argument('--no_display', default=True, action='store_false', dest='display', help='whether or not display results on screen') parser.add_argument('--output_dir', default='data/results', help='Path to output directory') parser.add_argument('--draw_trails', action='store_true', default=False, help='if provided object motion trails will be drawn.') parser.add_argument('--filter_classes', default=None, help='Filter class name') parser.add_argument('-w', '--weights', default=None, help='Path of trained weights') parser.add_argument('--segmentor_weights', default=None, help='Path of Segmentor weights') parser.add_argument('-ct', '--conf_thres', default=0.25, type=float, help='confidence score threshold') parser.add_argument('-it', '--iou_thres', default=0.45, type=float, help='iou score threshold') args = parser.parse_args() main(args) ================================================ FILE: asone/demo_tracker.py ================================================ import asone from asone import ASOne from .utils import draw_boxes import cv2 import argparse import time import os def main(args): filter_classes = args.filter_classes video_path = args.video os.makedirs(args.output_path, exist_ok=True) if filter_classes: filter_classes = filter_classes.split(',') detect = ASOne(tracker=asone.BYTETRACK, detector=asone.YOLOV7_PYTORCH, use_cuda=args.use_cuda) track = detect.track_video(video_path, output_dir=args.output_path, save_result=args.save, display=args.display, filter_classes=filter_classes) for bbox_details, frame_details in track: bbox_xyxy, ids, scores, class_ids = bbox_details frame, frame_num, fps = frame_details if __name__=='__main__': parser = argparse.ArgumentParser() parser.add_argument("video", help="Path of video") parser.add_argument('--cpu', default=True, action='store_false', dest='use_cuda', help='If provided the model will run on cpu otherwise it will run on gpu') parser.add_argument('--filter_classes', default=None, help='Class names seperated by comma (,). e.g. person,car ') parser.add_argument('-w', '--weights', default=None, help='Path of trained weights') parser.add_argument('-o', '--output_path', default='data/results', help='path of output file') parser.add_argument('--no_display', action='store_false', default=True, dest='display', help='if provided video will not be displayed') parser.add_argument('--no_save', action='store_false', default=True, dest='save', help='if provided video will not be saved') args = parser.parse_args() main(args) ================================================ FILE: asone/detectors/__init__.py ================================================ from asone.detectors.yolov5 import YOLOv5Detector from asone.detectors.yolov6 import YOLOv6Detector from asone.detectors.yolov7 import YOLOv7Detector from asone.detectors.yolov9 import YOLOv9Detector from asone.detectors.yolor import YOLOrDetector from asone.detectors.yolox import YOLOxDetector from asone.detectors.easyocr_detector import TextDetector from asone.detectors.detector import Detector __all__ = ['Detector' 'YOLOv5Detector', 'YOLOv6Detector', 'YOLOv7Detector', 'YOLOv9Detector', 'YOLOrDetector', 'YOLOxDetector', 'TextDetector', 'YOLOnasDetector'] ================================================ FILE: asone/detectors/detector.py ================================================ import cv2 from asone.detectors.utils.weights_path import get_weight_path from asone.detectors.utils.cfg_path import get_cfg_path from asone.detectors.utils.exp_name import get_exp__name class Detector: def __init__(self, model_flag: int, weights: str = None, use_cuda: bool = True, recognizer:int = None, num_classes=80): self.model = self._select_detector(model_flag, weights, use_cuda, recognizer, num_classes) def _select_detector(self, model_flag, weights, cuda, recognizer, num_classes): # Get required weight using model_flag mlmodel = False if weights and weights.split('.')[-1] == 'onnx': onnx = True weight = weights elif weights and weights.split('.')[-1] == 'mlmodel': onnx = False weight = weights mlmodel = True elif weights: onnx = False weight = weights else: mlmodel, onnx, weight = get_weight_path(model_flag) if model_flag in range(0, 20) or model_flag in range(120, 131): from asone.detectors.yolov5 import YOLOv5Detector _detector = YOLOv5Detector(weights=weight, use_onnx=onnx, mlmodel=mlmodel, use_cuda=cuda) elif model_flag in range(20, 34): from asone.detectors.yolov6 import YOLOv6Detector _detector = YOLOv6Detector(weights=weight, use_onnx=onnx, use_cuda=cuda) elif model_flag in range(34, 48) or model_flag in range(131, 139): from asone.detectors.yolov7 import YOLOv7Detector # Get exp file and corresponding model for coreml only _detector = YOLOv7Detector(weights=weight, use_onnx=onnx, mlmodel=mlmodel, use_cuda=cuda) elif model_flag in range(48, 58): from asone.detectors.yolor import YOLOrDetector # Get Configuration file for Yolor if model_flag in range(48, 57, 2): cfg = get_cfg_path(model_flag) else: cfg = None _detector = YOLOrDetector(weights=weight, cfg=cfg, use_onnx=onnx, use_cuda=cuda) elif model_flag in range(58, 72): from asone.detectors.yolox import YOLOxDetector # Get exp file and corresponding model for pytorch only if model_flag in range(58, 71, 2): exp, model_name = get_exp__name(model_flag) else: exp = model_name = None _detector = YOLOxDetector(model_name=model_name, exp_file=exp, weights=weight, use_onnx=onnx, use_cuda=cuda) elif model_flag in range(72, 82) or model_flag in range(139, 144): from asone.detectors.yolov8 import YOLOv8Detector # Get exp file and corresponding model for pytorch only _detector = YOLOv8Detector(weights=weight, use_onnx=onnx, mlmodel=mlmodel, use_cuda=cuda) # Get TextDetector model elif model_flag in range(82, 85): from asone.detectors.easyocr_detector import TextDetector _detector = TextDetector(detect_network=weight, use_cuda=cuda) elif model_flag in range(160, 163): from asone.detectors.yolonas import YOLOnasDetector # Get exp file and corresponding model for coreml only _detector = YOLOnasDetector( model_flag, weights=weight, use_onnx=onnx, use_cuda=cuda, num_classes=num_classes) elif model_flag in range(164, 170): from asone.detectors.yolov9 import YOLOv9Detector # Get exp file and corresponding model for pytorch only _detector = YOLOv9Detector( weights=weight, use_onnx=onnx, use_cuda=cuda) return _detector def get_detector(self): return self.model def detect(self, image: list, return_image=False, **kwargs: dict): return self.model.detect(image,return_image,**kwargs) if __name__ == '__main__': # Initialize YOLOv6 object detector model_type = 56 result = Detector(model_flag=model_type, use_cuda=True) img = cv2.imread('asone/asone-linux/test.jpeg') pred = result.get_detector(img) print(pred) ================================================ FILE: asone/detectors/easyocr_detector/__init__.py ================================================ from .text_detector import TextDetector __all__ = ['TextDetector'] ================================================ FILE: asone/detectors/easyocr_detector/text_detector.py ================================================ import easyocr import numpy as np class TextDetector: def __init__(self, detect_network, languages: list = ['en'], use_cuda=True): self.use_cuda = use_cuda self.detect_network = detect_network self.reader = easyocr.Reader(languages, detect_network=self.detect_network ,gpu=self.use_cuda) def detect(self, image: list, freelist: bool=False, return_image=False, **config) -> list: """_summary_ Args: image : Image languages (list, optional): List of languages. Defaults to ['en']. Returns: list: numpy array of extracted text and img info(heigh, width) """ h, w = image.shape[0:2] horizontal_list, free_list = self.reader.detect(image) if horizontal_list[0] == [] and free_list[0] == []: if return_image: return horizontal_list, image return np.empty((0, 6)), {'width': w, 'height': h} if freelist: return horizontal_list, free_list, {'width': w, 'height': h} x_list = [] y_list = [] new_points = [] if free_list[0] != []: bbox_list = np.array(free_list[0]).astype(int) xmin= bbox_list[:, :, 0].min(axis=1, keepdims=True) xmax= bbox_list[:, :, 0].max(axis=1, keepdims=True) ymin= bbox_list[:, :, 1].min(axis=1, keepdims=True) ymax= bbox_list[:, :, 1].max(axis=1, keepdims=True) new_points = np.hstack((xmin, xmax, ymin, ymax)).tolist() if len(horizontal_list[0]) < 1: horizontal_list = [new_points] else: horizontal_list = [horizontal_list[0] + new_points] horizontal_list = np.array(horizontal_list[0]) horizontal_list[:, [1, 2]] = horizontal_list[:, [2, 1]] horizontal_list = np.hstack((horizontal_list, np.array([[0.7, 80]]*len(horizontal_list)))) if return_image: return horizontal_list, image return horizontal_list, {'width': w, 'height': h} ================================================ FILE: asone/detectors/utils/__init__.py ================================================ ================================================ FILE: asone/detectors/utils/cfg_path.py ================================================ import os cfg_dir = os.path.dirname(os.path.dirname(__file__)) configuration = {'0': os.path.join(cfg_dir, 'yolor','cfg','yolor_csp_x.cfg'), '1': os.path.join(cfg_dir, 'yolor','cfg','yolor_csp.cfg'), '2': os.path.join(cfg_dir, 'yolor','cfg','yolor_p6.cfg')} def get_cfg_path(model_flag): if model_flag in [48,50]: cfg = configuration['0'] if model_flag in [52,54]: cfg = configuration['1'] if model_flag == 56: cfg = configuration['2'] return cfg ================================================ FILE: asone/detectors/utils/coreml_utils.py ================================================ import numpy as np def yolo_to_xyxy(bboxes, img_size): w, h = img_size bboxes = bboxes[:, 0:] bboxes[:, 0] = bboxes[:, 0]*w bboxes[:, 1] = bboxes[:, 1]*h bboxes[:, 2] = bboxes[:, 2]*w bboxes[:, 3] = bboxes[:, 3]*h bboxes[:, 0] = bboxes[:, 0] - bboxes[:, 2]/2 bboxes[:, 1] = bboxes[:, 1] - bboxes[:, 3]/2 bboxes[:, 2] = bboxes[:, 0] + bboxes[:, 2] bboxes[:, 3] = bboxes[:, 1] + bboxes[:, 3] return bboxes.astype(int) def generalize_output_format(bboxes, confidence_list, conf_thres): """_summary_ Args: bboxes : Bounding boxes in xyxy format confidence_list : List containing confidence score of each class conf_thres : confidence_threshold Returns: np.array: Array of format [Xmin, Ymin, Xmax, Ymax, confidence, class_id] """ class_ids = np.argmax(confidence_list, axis=1) conf_scr = [] output = [] for i, confidence in enumerate(confidence_list): if conf_thres < confidence[class_ids[i]]: conf_scr = confidence[class_ids[i]] res = np.append(np.append(bboxes[i], conf_scr), class_ids[i]) output.append(res) return np.array(output) def scale_bboxes(bboxes, org_img_shape, resized_img_shape): # Rescaling Bounding Boxes bboxes[:, :4] /= np.array([resized_img_shape[1], resized_img_shape[0], resized_img_shape[1], resized_img_shape[0]]) bboxes[:, :4] *= np.array([org_img_shape[1], org_img_shape[0], org_img_shape[1], org_img_shape[0]]) return bboxes ================================================ FILE: asone/detectors/utils/exp_name.py ================================================ import os exp_dir = os.path.dirname(os.path.dirname(__file__)) exp_file_name = {'58': (os.path.join(exp_dir, 'yolox','exps','yolox_l.py'),'yolox-l'), '60': (os.path.join(exp_dir, 'yolox','exps','yolox_nano.py'),'yolox-nano'), '62': (os.path.join(exp_dir, 'yolox','exps','yolox_tiny'),'yolox-tiny'), '64': (os.path.join(exp_dir, 'yolox','exps','yolov3.py'),'yolox-darknet'), '66': (os.path.join(exp_dir, 'yolox','exps','yolox_s.py'),'yolox-s'), '68': (os.path.join(exp_dir, 'yolox','exps','yolox_m.py'),'yolox-m'), '70': (os.path.join(exp_dir, 'yolox','exps','yolox_x.py'),'yolox-x') } def get_exp__name(model_flag): if model_flag == 58: exp, model_name = exp_file_name['58'][0], exp_file_name['58'][1] elif model_flag == 60: exp, model_name = exp_file_name['60'][0], exp_file_name['60'][1] elif model_flag == 62: exp, model_name = exp_file_name['62'][0], exp_file_name['62'][1] elif model_flag == 64: exp, model_name = exp_file_name['64'][0], exp_file_name['64'][1] elif model_flag == 66: exp, model_name = exp_file_name['66'][0], exp_file_name['66'][1] elif model_flag == 68: exp, model_name = exp_file_name['68'][0], exp_file_name['68'][1] elif model_flag == 70: exp, model_name = exp_file_name['70'][0], exp_file_name['70'][1] return exp, model_name ================================================ FILE: asone/detectors/utils/weights_path.py ================================================ import os weights = { '0': os.path.join('yolov5','weights','yolov5x6.pt'), '1': os.path.join('yolov5','weights','yolov5x6.onnx'), '2': os.path.join('yolov5','weights','yolov5s.pt'), '3': os.path.join('yolov5','weights','yolov5s.onnx'), '4': os.path.join('yolov5','weights','yolov5n.pt'), '5': os.path.join('yolov5','weights','yolov5n.onnx'), '6': os.path.join('yolov5','weights','yolov5m.pt'), '7': os.path.join('yolov5','weights','yolov5m.onnx'), '8': os.path.join('yolov5','weights','yolov5l.pt'), '9': os.path.join('yolov5','weights','yolov5l.onnx'), '10': os.path.join('yolov5','weights','yolov5x.pt'), '11': os.path.join('yolov5','weights','yolov5x.onnx'), '12': os.path.join('yolov5','weights','yolov5n6.pt'), '13': os.path.join('yolov5','weights','yolov5n6.onnx'), '14': os.path.join('yolov5','weights','yolov5s6.pt'), '15': os.path.join('yolov5','weights','yolov5s6.onnx'), '16': os.path.join('yolov5','weights','yolov5m6.pt'), '17': os.path.join('yolov5','weights','yolov5m6.onnx'), '18': os.path.join('yolov5','weights','yolov5l6.pt'), '19': os.path.join('yolov5','weights','yolov5l6.onnx'), '120': os.path.join('yolov5','weights','yolov5n.mlmodel'), '121': os.path.join('yolov5','weights','yolov5s.mlmodel'), '122': os.path.join('yolov5','weights','yolov5x6.mlmodel'), '123': os.path.join('yolov5','weights','yolov5m.mlmodel'), '124': os.path.join('yolov5','weights','yolov5l.mlmodel'), '125': os.path.join('yolov5','weights','yolov5x.mlmodel'), '126': os.path.join('yolov5','weights','yolov5n6.mlmodel'), '127': os.path.join('yolov5','weights','yolov5s6.mlmodel'), '128': os.path.join('yolov5','weights','yolov5m6.mlmodel'), '129': os.path.join('yolov5','weights','yolov5l6.mlmodel'), # YOLOv6 '20': os.path.join('yolov6','weights','yolov6n.pt'), '21': os.path.join('yolov6','weights','yolov6n.onnx'), '22': os.path.join('yolov6','weights','yolov6t.pt'), '23': os.path.join('yolov6','weights','yolov6t.onnx'), '24': os.path.join('yolov6','weights','yolov6s.pt'), '25': os.path.join('yolov6','weights','yolov6s.onnx'), '26': os.path.join('yolov6','weights','yolov6m.pt'), '27': os.path.join('yolov6','weights','yolov6m.onnx'), '28': os.path.join('yolov6','weights','yolov6l.pt'), '29': os.path.join('yolov6','weights','yolov6l.onnx'), '30': os.path.join('yolov6','weights','yolov6l_relu.pt'), '31': os.path.join('yolov6','weights','yolov6l_relu.onnx'), '32': os.path.join('yolov6','weights','yolov6s_repopt.pt'), '33': os.path.join('yolov6','weights','yolov6s_repopt.onnx'), # YOLOv7 '34': os.path.join('yolov7','weights','yolov7-tiny.pt'), '35': os.path.join('yolov7','weights','yolov7-tiny.onnx'), '36': os.path.join('yolov7','weights','yolov7.pt'), '37': os.path.join('yolov7','weights','yolov7.onnx'), '38': os.path.join('yolov7','weights','yolov7x.pt'), '39': os.path.join('yolov7','weights','yolov7x.onnx'), '40': os.path.join('yolov7','weights','yolov7-w6.pt'), '41': os.path.join('yolov7','weights','yolov7-w6.onnx'), '42': os.path.join('yolov7','weights','yolov7-e6.pt'), '43': os.path.join('yolov7','weights','yolov7-e6.onnx'), '44': os.path.join('yolov7','weights','yolov7-d6.pt'), '45': os.path.join('yolov7','weights','yolov7-d6.onnx'), '46': os.path.join('yolov7','weights','yolov7-e6e.pt'), '47': os.path.join('yolov7','weights','yolov7-e6e.onnx'), '130': os.path.join('yolov7','weights','yolov7-tiny.mlmodel'), '131': os.path.join('yolov7','weights','yolov7.mlmodel'), '132': os.path.join('yolov7','weights','yolov7x.mlmodel'), '133': os.path.join('yolov7','weights','yolov7-w6.mlmodel'), '134': os.path.join('yolov7','weights','yolov7-e6.mlmodel'), '135': os.path.join('yolov7','weights','yolov7-d6.mlmodel'), '136': os.path.join('yolov7','weights','yolov7-e6e.mlmodel'), # YOLOR '48': os.path.join('yolor','weights','yolor_csp_x.pt'), '49': os.path.join('yolor','weights','yolor_csp_x.onnx'), '50': os.path.join('yolor','weights','yolor_csp_x_star.pt'), '51': os.path.join('yolor','weights','yolor_csp_x_star.onnx'), '52': os.path.join('yolor','weights','yolor_csp_star.pt'), '53': os.path.join('yolor','weights','yolor_csp_star.onnx'), '54': os.path.join('yolor','weights','yolor_csp.pt'), '55': os.path.join('yolor','weights','yolor_csp.onnx'), '56': os.path.join('yolor','weights','yolor_p6.pt'), '57': os.path.join('yolor','weights','yolor_p6.onnx'), # YOLOX '58': os.path.join('yolox','weights','yolox_l.pth'), '59': os.path.join('yolox','weights','yolox_l.onnx'), '60': os.path.join('yolox','weights','yolox_nano.pth'), '61': os.path.join('yolox','weights','yolox_nano.onnx'), '62': os.path.join('yolox','weights','yolox_tiny.pth'), '63': os.path.join('yolox','weights','yolox_tiny.onnx'), '64': os.path.join('yolox','weights','yolox_darknet.pth'), '65': os.path.join('yolox','weights','yolox_darknet.onnx'), '66': os.path.join('yolox','weights','yolox_s.pth'), '67': os.path.join('yolox','weights','yolox_s.onnx'), '68': os.path.join('yolox','weights','yolox_m.pth'), '69': os.path.join('yolox','weights','yolox_m.onnx'), '70': os.path.join('yolox','weights','yolox_x.pth'), '71': os.path.join('yolox','weights','yolox_x.onnx'), # YOLOv8 '72': os.path.join('yolov8','weights','yolov8n.pt'), '73': os.path.join('yolov8','weights','yolov8n.onnx'), '74': os.path.join('yolov8','weights','yolov8s.pt'), '75': os.path.join('yolov8','weights','yolov8s.onnx'), '76': os.path.join('yolov8','weights','yolov8m.pt'), '77': os.path.join('yolov8','weights','yolov8m.onnx'), '78': os.path.join('yolov8','weights','yolov8l.pt'), '79': os.path.join('yolov8','weights','yolov8l.onnx'), '80': os.path.join('yolov8','weights','yolov8x.pt'), '81': os.path.join('yolov8','weights','yolov8x.onnx'), '139': os.path.join('yolov8','weights','yolov8n.mlmodel'), '140': os.path.join('yolov8','weights','yolov8s.mlmodel'), '141': os.path.join('yolov8','weights','yolov8m.mlmodel'), '142': os.path.join('yolov8','weights','yolov8l.mlmodel'), '143': os.path.join('yolov8','weights','yolov8x.mlmodel'), # Text Detectors '82': 'craft', '83': 'dbnet18', # YOLONAS_S_PYTORCH # YOLO NAS '160':os.path.join('yolonas','weights','yolo_nas_s.pth'), '161':os.path.join('yolonas','weights','yolo_nas_m.pth'), '162':os.path.join('yolonas','weights','yolo_nas_l.pth'), # YOLOv9 '164':os.path.join('yolov9','weights','yolov9-c-converted.pt'), '165':os.path.join('yolov9','weights','yolov9-e-converted.pt'), '166':os.path.join('yolov9','weights','yolov9-c.pt'), '167':os.path.join('yolov9','weights','yolov9-e.pt'), '168':os.path.join('yolov9','weights','gelan-c.pt'), '169':os.path.join('yolov9','weights','gelan-e.pt'), # Segmentor '171':os.path.join('sam','weights','sam_vit_h_4b8939.pth'), } def get_weight_path(model_flag): coreml= False if model_flag in range(0, 20): onnx = False if (model_flag % 2 == 0) else True weight = weights[str(model_flag)] elif model_flag in range(20, 34): onnx = False if (model_flag % 2 == 0) else True weight = weights[str(model_flag)] elif model_flag in range(34, 48): onnx = False if (model_flag % 2 == 0) else True weight = weights[str(model_flag)] elif model_flag in range(48, 58): onnx = False if (model_flag % 2 == 0) else True weight = weights[str(model_flag)] elif model_flag in range(58, 72): onnx = False if (model_flag % 2 == 0) else True weight = weights[str(model_flag)] elif model_flag in range(72, 82): onnx = False if (model_flag % 2 == 0) else True weight = weights[str(model_flag)] elif model_flag in range(82, 85): onnx = False weight = weights[str(model_flag)] elif model_flag in range(120, 130): weight = weights[str(model_flag)] onnx=False coreml = True elif model_flag in range(130, 137): weight = weights[str(model_flag)] onnx=False coreml = True elif model_flag in range(139, 145): weight = weights[str(model_flag)] onnx=False coreml = True elif model_flag in range(160, 163): weight = weights[str(model_flag)] onnx=False coreml = True elif model_flag in range(164, 170): onnx = False weight = weights[str(model_flag)] return coreml, onnx, weight ================================================ FILE: asone/detectors/yolonas/__init__.py ================================================ from .yolonas import YOLOnasDetector __all__ = ['YOLOnasDetector'] ================================================ FILE: asone/detectors/yolonas/yolonas.py ================================================ import os from asone.utils import get_names import numpy as np import warnings import torch import onnxruntime from asone import utils import super_gradients import numpy as np from super_gradients.training.processing import DetectionCenterPadding, StandardizeImage, NormalizeImage, ImagePermute, ComposeProcessing, DetectionLongestMaxSizeRescale from super_gradients.training import models from super_gradients.common.object_names import Models from asone.utils.utils import PathResolver class_names = [""] class YOLOnasDetector: def __init__(self, model_flag, weights=None, cfg=None, use_onnx=True, use_cuda=True, # checkpoint_num_classes=80, num_classes=80 ): self.model_flag = model_flag # self.checkpoint_num_classes = checkpoint_num_classes if not os.path.exists(weights): utils.download_weights(weights) self.num_classes = num_classes self.device = 'cuda' if use_cuda and torch.cuda.is_available() else 'cpu' self.use_onnx = use_onnx with PathResolver(): # Load Model self.model = self.load_model(weights=weights) def load_model(self, weights): # model = super_gradients.training.models.get(name, # checkpoint_path=weights, # checkpoint_num_classes=self.checkpoint_num_classes, # num_classes=self.num_classes).to(self.device) if self.model_flag == 160: model = models.get(Models.YOLO_NAS_S, checkpoint_path=weights, num_classes=self.num_classes).to(self.device) elif self.model_flag == 161: model = models.get(Models.YOLO_NAS_M, checkpoint_path=weights, num_classes=self.num_classes).to(self.device) elif self.model_flag == 162: model = models.get(Models.YOLO_NAS_L, checkpoint_path=weights, num_classes=self.num_classes).to(self.device) return model def detect(self, image: list, input_shape: tuple = (640, 640), conf_thres: float = 0.25, iou_thres: float = 0.45, max_det: int = 1000, filter_classes: bool = None, agnostic_nms: bool = True, with_p6: bool = False, return_image=False) -> list: if self.num_classes==80: self.model.set_dataset_processing_params(class_names=class_names, image_processor=ComposeProcessing( [ DetectionLongestMaxSizeRescale(output_shape=(636, 636)), DetectionCenterPadding(output_shape=(640, 640), pad_value=114), StandardizeImage(max_value=255.0), ImagePermute(permutation=(2, 0, 1)), ] ), iou=iou_thres,conf=conf_thres, ) original_image = image # Inference if self.use_onnx: pass else: detections = self.model.predict(image) image_info = { 'width': original_image.shape[1], 'height': original_image.shape[0], } detections = list(detections) pred = detections[0].prediction bboxes_xyxy = pred.bboxes_xyxy confidence = pred.confidence labels = pred.labels confidence = confidence.reshape(-1,1) labels = labels.reshape(-1,1) arr = np.append(bboxes_xyxy, confidence, axis=1) predictions = np.append(arr, labels, axis=1) if return_image: return predictions, original_image else: return predictions, image_info ================================================ FILE: asone/detectors/yolor/__init__.py ================================================ from .yolor_detector import YOLOrDetector __all__ = ['YOLOrDetector'] ================================================ FILE: asone/detectors/yolor/cfg/yolor_csp.cfg ================================================ [net] # Testing #batch=1 #subdivisions=1 # Training batch=64 subdivisions=8 width=512 height=512 channels=3 momentum=0.949 decay=0.0005 angle=0 saturation = 1.5 exposure = 1.5 hue=.1 learning_rate=0.00261 burn_in=1000 max_batches = 500500 policy=steps steps=400000,450000 scales=.1,.1 #cutmix=1 mosaic=1 # ============ Backbone ============ # # Stem # 0 [convolutional] batch_normalize=1 filters=32 size=3 stride=1 pad=1 activation=silu # P1 # Downsample [convolutional] batch_normalize=1 filters=64 size=3 stride=2 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=32 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=64 size=3 stride=1 pad=1 activation=silu # 4 (previous+1+3k) [shortcut] from=-3 activation=linear # P2 # Downsample [convolutional] batch_normalize=1 filters=128 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=64 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=64 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first [convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=silu # Merge [-1, -(3k+4)] [route] layers = -1,-10 # Transition last # 17 (previous+7+3k) [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu # P3 # Downsample [convolutional] batch_normalize=1 filters=256 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu # Merge [-1 -(4+3k)] [route] layers = -1,-28 # Transition last # 48 (previous+7+3k) [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # P4 # Downsample [convolutional] batch_normalize=1 filters=512 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # Merge [-1 -(3k+4)] [route] layers = -1,-28 # Transition last # 79 (previous+7+3k) [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu # P5 # Downsample [convolutional] batch_normalize=1 filters=1024 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu # Merge [-1 -(3k+4)] [route] layers = -1,-16 # Transition last # 98 (previous+7+3k) [convolutional] batch_normalize=1 filters=1024 size=1 stride=1 pad=1 activation=silu # ============ End of Backbone ============ # # ============ Neck ============ # # CSPSPP [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=silu [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu ### SPP ### [maxpool] stride=1 size=5 [route] layers=-2 [maxpool] stride=1 size=9 [route] layers=-4 [maxpool] stride=1 size=13 [route] layers=-1,-3,-5,-6 ### End SPP ### [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=silu [route] layers = -1, -13 # 113 (previous+6+5+2k) [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu # End of CSPSPP # FPN-4 [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [upsample] stride=2 [route] layers = 79 [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [route] layers = -1, -3 [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu # Merge [-1, -(2k+2)] [route] layers = -1, -6 # Transition last # 127 (previous+6+4+2k) [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # FPN-3 [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [upsample] stride=2 [route] layers = 48 [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [route] layers = -1, -3 [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=128 activation=silu [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=128 activation=silu # Merge [-1, -(2k+2)] [route] layers = -1, -6 # Transition last # 141 (previous+6+4+2k) [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu # PAN-4 [convolutional] batch_normalize=1 size=3 stride=2 pad=1 filters=256 activation=silu [route] layers = -1, 127 [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu [route] layers = -1,-6 # Transition last # 152 (previous+3+4+2k) [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # PAN-5 [convolutional] batch_normalize=1 size=3 stride=2 pad=1 filters=512 activation=silu [route] layers = -1, 113 [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=silu [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=silu [route] layers = -1,-6 # Transition last # 163 (previous+3+4+2k) [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu # ============ End of Neck ============ # # 164 [implicit_add] filters=256 # 165 [implicit_add] filters=512 # 166 [implicit_add] filters=1024 # 167 [implicit_mul] filters=255 # 168 [implicit_mul] filters=255 # 169 [implicit_mul] filters=255 # ============ Head ============ # # YOLO-3 [route] layers = 141 [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu [shift_channels] from=164 [convolutional] size=1 stride=1 pad=1 filters=255 activation=linear [control_channels] from=167 [yolo] mask = 0,1,2 anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401 classes=80 num=9 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1 scale_x_y = 1.05 iou_thresh=0.213 cls_normalizer=1.0 iou_normalizer=0.07 iou_loss=ciou nms_kind=greedynms beta_nms=0.6 # YOLO-4 [route] layers = 152 [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=silu [shift_channels] from=165 [convolutional] size=1 stride=1 pad=1 filters=255 activation=linear [control_channels] from=168 [yolo] mask = 3,4,5 anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401 classes=80 num=9 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1 scale_x_y = 1.05 iou_thresh=0.213 cls_normalizer=1.0 iou_normalizer=0.07 iou_loss=ciou nms_kind=greedynms beta_nms=0.6 # YOLO-5 [route] layers = 163 [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=1024 activation=silu [shift_channels] from=166 [convolutional] size=1 stride=1 pad=1 filters=255 activation=linear [control_channels] from=169 [yolo] mask = 6,7,8 anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401 classes=80 num=9 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1 scale_x_y = 1.05 iou_thresh=0.213 cls_normalizer=1.0 iou_normalizer=0.07 iou_loss=ciou nms_kind=greedynms beta_nms=0.6 ================================================ FILE: asone/detectors/yolor/cfg/yolor_csp_x.cfg ================================================ [net] # Testing #batch=1 #subdivisions=1 # Training batch=64 subdivisions=8 width=512 height=512 channels=3 momentum=0.949 decay=0.0005 angle=0 saturation = 1.5 exposure = 1.5 hue=.1 learning_rate=0.00261 burn_in=1000 max_batches = 500500 policy=steps steps=400000,450000 scales=.1,.1 #cutmix=1 mosaic=1 # ============ Backbone ============ # # Stem # 0 [convolutional] batch_normalize=1 filters=32 size=3 stride=1 pad=1 activation=silu # P1 # Downsample [convolutional] batch_normalize=1 filters=80 size=3 stride=2 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=40 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=80 size=3 stride=1 pad=1 activation=silu # 4 (previous+1+3k) [shortcut] from=-3 activation=linear # P2 # Downsample [convolutional] batch_normalize=1 filters=160 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=80 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=80 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=80 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=80 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=80 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=80 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=80 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=80 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first [convolutional] batch_normalize=1 filters=80 size=1 stride=1 pad=1 activation=silu # Merge [-1, -(3k+4)] [route] layers = -1,-13 # Transition last # 20 (previous+7+3k) [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu # P3 # Downsample [convolutional] batch_normalize=1 filters=320 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=160 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=160 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=160 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=160 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=160 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=160 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=160 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=160 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=160 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=160 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu # Merge [-1 -(4+3k)] [route] layers = -1,-34 # Transition last # 57 (previous+7+3k) [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu # P4 # Downsample [convolutional] batch_normalize=1 filters=640 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu # Merge [-1 -(3k+4)] [route] layers = -1,-34 # Transition last # 94 (previous+7+3k) [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu # P5 # Downsample [convolutional] batch_normalize=1 filters=1280 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=640 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=640 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=640 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=640 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=640 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu # Merge [-1 -(3k+4)] [route] layers = -1,-19 # Transition last # 116 (previous+7+3k) [convolutional] batch_normalize=1 filters=1280 size=1 stride=1 pad=1 activation=silu # ============ End of Backbone ============ # # ============ Neck ============ # # CSPSPP [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=640 activation=silu [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu ### SPP ### [maxpool] stride=1 size=5 [route] layers=-2 [maxpool] stride=1 size=9 [route] layers=-4 [maxpool] stride=1 size=13 [route] layers=-1,-3,-5,-6 ### End SPP ### [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=640 activation=silu [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=640 activation=silu [route] layers = -1, -15 # 133 (previous+6+5+2k) [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu # End of CSPSPP # FPN-4 [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [upsample] stride=2 [route] layers = 94 [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [route] layers = -1, -3 [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu # Merge [-1, -(2k+2)] [route] layers = -1, -8 # Transition last # 149 (previous+6+4+2k) [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu # FPN-3 [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [upsample] stride=2 [route] layers = 57 [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [route] layers = -1, -3 [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=160 activation=silu [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=160 activation=silu [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=160 activation=silu # Merge [-1, -(2k+2)] [route] layers = -1, -8 # Transition last # 165 (previous+6+4+2k) [convolutional] batch_normalize=1 filters=160 size=1 stride=1 pad=1 activation=silu # PAN-4 [convolutional] batch_normalize=1 size=3 stride=2 pad=1 filters=320 activation=silu [route] layers = -1, 149 [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu [route] layers = -1,-8 # Transition last # 178 (previous+3+4+2k) [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu # PAN-5 [convolutional] batch_normalize=1 size=3 stride=2 pad=1 filters=640 activation=silu [route] layers = -1, 133 [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=640 activation=silu [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=640 activation=silu [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=640 activation=silu [route] layers = -1,-8 # Transition last # 191 (previous+3+4+2k) [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu # ============ End of Neck ============ # # 192 [implicit_add] filters=320 # 193 [implicit_add] filters=640 # 194 [implicit_add] filters=1280 # 195 [implicit_mul] filters=255 # 196 [implicit_mul] filters=255 # 197 [implicit_mul] filters=255 # ============ Head ============ # # YOLO-3 [route] layers = 165 [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu [shift_channels] from=192 [convolutional] size=1 stride=1 pad=1 filters=255 activation=linear [control_channels] from=195 [yolo] mask = 0,1,2 anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401 classes=80 num=9 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1 scale_x_y = 1.05 iou_thresh=0.213 cls_normalizer=1.0 iou_normalizer=0.07 iou_loss=ciou nms_kind=greedynms beta_nms=0.6 # YOLO-4 [route] layers = 178 [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=640 activation=silu [shift_channels] from=193 [convolutional] size=1 stride=1 pad=1 filters=255 activation=linear [control_channels] from=196 [yolo] mask = 3,4,5 anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401 classes=80 num=9 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1 scale_x_y = 1.05 iou_thresh=0.213 cls_normalizer=1.0 iou_normalizer=0.07 iou_loss=ciou nms_kind=greedynms beta_nms=0.6 # YOLO-5 [route] layers = 191 [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=1280 activation=silu [shift_channels] from=194 [convolutional] size=1 stride=1 pad=1 filters=255 activation=linear [control_channels] from=197 [yolo] mask = 6,7,8 anchors = 12, 16, 19, 36, 40, 28, 36, 75, 76, 55, 72, 146, 142, 110, 192, 243, 459, 401 classes=80 num=9 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1 scale_x_y = 1.05 iou_thresh=0.213 cls_normalizer=1.0 iou_normalizer=0.07 iou_loss=ciou nms_kind=greedynms beta_nms=0.6 ================================================ FILE: asone/detectors/yolor/cfg/yolor_p6.cfg ================================================ [net] batch=64 subdivisions=8 width=1280 height=1280 channels=3 momentum=0.949 decay=0.0005 angle=0 saturation = 1.5 exposure = 1.5 hue=.1 learning_rate=0.00261 burn_in=1000 max_batches = 500500 policy=steps steps=400000,450000 scales=.1,.1 mosaic=1 # ============ Backbone ============ # # Stem # P1 # Downsample # 0 [reorg] [convolutional] batch_normalize=1 filters=64 size=3 stride=1 pad=1 activation=silu # P2 # Downsample [convolutional] batch_normalize=1 filters=128 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=64 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=64 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=64 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first # #[convolutional] #batch_normalize=1 #filters=64 #size=1 #stride=1 #pad=1 #activation=silu # Merge [-1, -(3k+3)] [route] layers = -1,-12 # Transition last # 16 (previous+6+3k) [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu # P3 # Downsample [convolutional] batch_normalize=1 filters=256 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first # #[convolutional] #batch_normalize=1 #filters=128 #size=1 #stride=1 #pad=1 #activation=silu # Merge [-1, -(3k+3)] [route] layers = -1,-24 # Transition last # 43 (previous+6+3k) [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # P4 # Downsample [convolutional] batch_normalize=1 filters=384 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=192 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=192 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=192 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=192 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=192 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=192 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=192 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first # #[convolutional] #batch_normalize=1 #filters=192 #size=1 #stride=1 #pad=1 #activation=silu # Merge [-1, -(3k+3)] [route] layers = -1,-24 # Transition last # 70 (previous+6+3k) [convolutional] batch_normalize=1 filters=384 size=1 stride=1 pad=1 activation=silu # P5 # Downsample [convolutional] batch_normalize=1 filters=512 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first # #[convolutional] #batch_normalize=1 #filters=256 #size=1 #stride=1 #pad=1 #activation=silu # Merge [-1, -(3k+3)] [route] layers = -1,-12 # Transition last # 85 (previous+6+3k) [convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=silu # P6 # Downsample [convolutional] batch_normalize=1 filters=640 size=3 stride=2 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu # Residual Block [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 filters=320 size=3 stride=1 pad=1 activation=silu [shortcut] from=-3 activation=linear # Transition first # #[convolutional] #batch_normalize=1 #filters=320 #size=1 #stride=1 #pad=1 #activation=silu # Merge [-1, -(3k+3)] [route] layers = -1,-12 # Transition last # 100 (previous+6+3k) [convolutional] batch_normalize=1 filters=640 size=1 stride=1 pad=1 activation=silu # ============ End of Backbone ============ # # ============ Neck ============ # # CSPSPP [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [route] layers = -2 [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu ### SPP ### [maxpool] stride=1 size=5 [route] layers=-2 [maxpool] stride=1 size=9 [route] layers=-4 [maxpool] stride=1 size=13 [route] layers=-1,-3,-5,-6 ### End SPP ### [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu [route] layers = -1, -13 # 115 (previous+6+5+2k) [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu # End of CSPSPP # FPN-5 [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [upsample] stride=2 [route] layers = 85 [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [route] layers = -1, -3 [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu # Merge [-1, -(2k+2)] [route] layers = -1, -8 # Transition last # 131 (previous+6+4+2k) [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # FPN-4 [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [upsample] stride=2 [route] layers = 70 [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [route] layers = -1, -3 [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=192 activation=silu [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=192 activation=silu [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=192 activation=silu # Merge [-1, -(2k+2)] [route] layers = -1, -8 # Transition last # 147 (previous+6+4+2k) [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu # FPN-3 [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [upsample] stride=2 [route] layers = 43 [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [route] layers = -1, -3 [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=128 activation=silu [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=128 activation=silu [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=128 activation=silu # Merge [-1, -(2k+2)] [route] layers = -1, -8 # Transition last # 163 (previous+6+4+2k) [convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=silu # PAN-4 [convolutional] batch_normalize=1 size=3 stride=2 pad=1 filters=192 activation=silu [route] layers = -1, 147 [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=192 activation=silu [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=192 activation=silu [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=192 activation=silu [route] layers = -1,-8 # Transition last # 176 (previous+3+4+2k) [convolutional] batch_normalize=1 filters=192 size=1 stride=1 pad=1 activation=silu # PAN-5 [convolutional] batch_normalize=1 size=3 stride=2 pad=1 filters=256 activation=silu [route] layers = -1, 131 [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu [route] layers = -1,-8 # Transition last # 189 (previous+3+4+2k) [convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=silu # PAN-6 [convolutional] batch_normalize=1 size=3 stride=2 pad=1 filters=320 activation=silu [route] layers = -1, 115 [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu # Split [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [route] layers = -2 # Plain Block [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=320 activation=silu [route] layers = -1,-8 # Transition last # 202 (previous+3+4+2k) [convolutional] batch_normalize=1 filters=320 size=1 stride=1 pad=1 activation=silu # ============ End of Neck ============ # # 203 [implicit_add] filters=256 # 204 [implicit_add] filters=384 # 205 [implicit_add] filters=512 # 206 [implicit_add] filters=640 # 207 [implicit_mul] filters=255 # 208 [implicit_mul] filters=255 # 209 [implicit_mul] filters=255 # 210 [implicit_mul] filters=255 # ============ Head ============ # # YOLO-3 [route] layers = 163 [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=silu [shift_channels] from=203 [convolutional] size=1 stride=1 pad=1 filters=255 activation=linear [control_channels] from=207 [yolo] mask = 0,1,2 anchors = 19,27, 44,40, 38,94, 96,68, 86,152, 180,137, 140,301, 303,264, 238,542, 436,615, 739,380, 925,792 classes=80 num=12 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1 scale_x_y = 1.05 iou_thresh=0.213 cls_normalizer=1.0 iou_normalizer=0.07 iou_loss=ciou nms_kind=greedynms beta_nms=0.6 # YOLO-4 [route] layers = 176 [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=384 activation=silu [shift_channels] from=204 [convolutional] size=1 stride=1 pad=1 filters=255 activation=linear [control_channels] from=208 [yolo] mask = 3,4,5 anchors = 19,27, 44,40, 38,94, 96,68, 86,152, 180,137, 140,301, 303,264, 238,542, 436,615, 739,380, 925,792 classes=80 num=12 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1 scale_x_y = 1.05 iou_thresh=0.213 cls_normalizer=1.0 iou_normalizer=0.07 iou_loss=ciou nms_kind=greedynms beta_nms=0.6 # YOLO-5 [route] layers = 189 [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=silu [shift_channels] from=205 [convolutional] size=1 stride=1 pad=1 filters=255 activation=linear [control_channels] from=209 [yolo] mask = 6,7,8 anchors = 19,27, 44,40, 38,94, 96,68, 86,152, 180,137, 140,301, 303,264, 238,542, 436,615, 739,380, 925,792 classes=80 num=12 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1 scale_x_y = 1.05 iou_thresh=0.213 cls_normalizer=1.0 iou_normalizer=0.07 iou_loss=ciou nms_kind=greedynms beta_nms=0.6 # YOLO-6 [route] layers = 202 [convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=640 activation=silu [shift_channels] from=206 [convolutional] size=1 stride=1 pad=1 filters=255 activation=linear [control_channels] from=210 [yolo] mask = 9,10,11 anchors = 19,27, 44,40, 38,94, 96,68, 86,152, 180,137, 140,301, 303,264, 238,542, 436,615, 739,380, 925,792 classes=80 num=12 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1 scale_x_y = 1.05 iou_thresh=0.213 cls_normalizer=1.0 iou_normalizer=0.07 iou_loss=ciou nms_kind=greedynms beta_nms=0.6 # ============ End of Head ============ # ================================================ FILE: asone/detectors/yolor/models/__init__.py ================================================ ================================================ FILE: asone/detectors/yolor/models/common.py ================================================ # This file contains modules common to various models import math import numpy as np import torch import torch.nn as nn from PIL import Image, ImageDraw from asone.detectors.yolor.utils.datasets import letterbox from asone.detectors.yolor.utils.general import non_max_suppression, make_divisible, scale_coords, xyxy2xywh from asone.detectors.yolor.utils.plots import color_list try: from pytorch_wavelets import DWTForward, DWTInverse class DWT(nn.Module): def __init__(self): super(DWT, self).__init__() self.xfm = DWTForward(J=1, wave='db1', mode='zero') def forward(self, x): b,c,w,h = x.shape yl, yh = self.xfm(x) return torch.cat([yl/2., yh[0].view(b,-1,w//2,h//2)/2.+.5], 1) except: class DWT(nn.Module): # use ReOrg instead def __init__(self): super(DWT, self).__init__() def forward(self, x): return torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1) class ImplicitA(nn.Module): def __init__(self, channel): super(ImplicitA, self).__init__() self.channel = channel self.implicit = nn.Parameter(torch.zeros(1, channel, 1, 1)) nn.init.normal_(self.implicit, std=.02) def forward(self, x): return self.implicit.expand_as(x) + x class ImplicitM(nn.Module): def __init__(self, channel): super(ImplicitM, self).__init__() self.channel = channel self.implicit = nn.Parameter(torch.ones(1, channel, 1, 1)) nn.init.normal_(self.implicit, mean=1., std=.02) def forward(self, x): return self.implicit.expand_as(x) * x class ReOrg(nn.Module): def __init__(self): super(ReOrg, self).__init__() def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2) return torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1) def autopad(k, p=None): # kernel, padding # Pad to 'same' if p is None: p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad return p def DWConv(c1, c2, k=1, s=1, act=True): # Depthwise convolution return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act) class Conv(nn.Module): # Standard convolution def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super(Conv, self).__init__() self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) self.bn = nn.BatchNorm2d(c2) self.act = nn.SiLU() if act else nn.Identity() def forward(self, x): return self.act(self.bn(self.conv(x))) def fuseforward(self, x): return self.act(self.conv(x)) class ConvSig(nn.Module): # Standard convolution def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super(ConvSig, self).__init__() self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) self.act = nn.Sigmoid() if act else nn.Identity() def forward(self, x): return self.act(self.conv(x)) def fuseforward(self, x): return self.act(self.conv(x)) class ConvSqu(nn.Module): # Standard convolution def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super(ConvSqu, self).__init__() self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) self.act = nn.SiLU() if act else nn.Identity() def forward(self, x): return self.act(self.conv(x)) def fuseforward(self, x): return self.act(self.conv(x)) class Bottleneck(nn.Module): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super(Bottleneck, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c2, 3, 1, g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class BottleneckG(nn.Module): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super(BottleneckG, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1, g=g) self.cv2 = Conv(c_, c2, 3, 1, g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class BottleneckCSP(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSP, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class BottleneckCSPF(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPF, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) #self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.m(self.cv1(x)) y2 = self.cv2(x) return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class BottleneckCSPL(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPL, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) #self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.act(self.bn(torch.cat((y1, y2), dim=1))) class BottleneckCSPLG(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=3, e=0.25): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPLG, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, g*c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(g*c_, g*c_, 1, 1, groups=g, bias=False) #self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d((1+g) * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*[BottleneckG(g*c_, g*c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.act(self.bn(torch.cat((y1, y2), dim=1))) class BottleneckCSPSE(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPSE, self).__init__() c_ = int(c2 * e) # hidden channels self.avg_pool = nn.AdaptiveAvgPool2d(1) self.cs = ConvSqu(c1, c1//8, 1, 1) self.cvsig = ConvSig(c1//8, c1, 1, 1) self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x = x * self.cvsig(self.cs(self.avg_pool(x))).expand_as(x) y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class BottleneckCSPSEA(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPSEA, self).__init__() c_ = int(c2 * e) # hidden channels self.avg_pool = nn.AdaptiveAvgPool2d(1) self.cs = ConvSqu(c1, c1//8, 1, 1) self.cvsig = ConvSig(c1//8, c1, 1, 1) self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x = x + x * self.cvsig(self.cs(self.avg_pool(x))).expand_as(x) y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class BottleneckCSPSAM(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPSAM, self).__init__() c_ = int(c2 * e) # hidden channels self.cvsig = ConvSig(c1, c1, 1, 1) self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x = x * self.cvsig(x) y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class BottleneckCSPSAMA(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPSAMA, self).__init__() c_ = int(c2 * e) # hidden channels self.cvsig = ConvSig(c1, c1, 1, 1) self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x = x + x * self.cvsig(x) y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class BottleneckCSPSAMB(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPSAMB, self).__init__() c_ = int(c2 * e) # hidden channels self.cvsig = ConvSig(c2, c2, 1, 1) self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) y = self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) return y * self.cvsig(y) class BottleneckCSPGC(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPGC, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) self.channel_add_conv = nn.Sequential( nn.Conv2d(c2, c2, kernel_size=1), nn.LayerNorm([c2, 1, 1]), nn.ReLU(inplace=True), # yapf: disable nn.Conv2d(c2, c2, kernel_size=1)) self.conv_mask = nn.Conv2d(c2, 1, kernel_size=1) self.softmax = nn.Softmax(dim=2) def spatial_pool(self, x): batch, channel, height, width = x.size() input_x = x # [N, C, H * W] input_x = input_x.view(batch, channel, height * width) # [N, 1, C, H * W] input_x = input_x.unsqueeze(1) # [N, 1, H, W] context_mask = self.conv_mask(x) # [N, 1, H * W] context_mask = context_mask.view(batch, 1, height * width) # [N, 1, H * W] context_mask = self.softmax(context_mask) # [N, 1, H * W, 1] context_mask = context_mask.unsqueeze(-1) # [N, 1, C, 1] context = torch.matmul(input_x, context_mask) # [N, C, 1, 1] context = context.view(batch, channel, 1, 1) return context def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) y = self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) return y + self.channel_add_conv(self.spatial_pool(y)) class BottleneckCSPDNL(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPDNL, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) self.conv_query = nn.Conv2d(c2, c2, kernel_size=1) self.conv_key = nn.Conv2d(c2, c2, kernel_size=1) self.conv_value = nn.Conv2d(c2, c2, kernel_size=1, bias=False) self.conv_out = None self.scale = math.sqrt(c2) self.temperature = 0.05 self.softmax = nn.Softmax(dim=2) self.gamma = nn.Parameter(torch.zeros(1)) self.conv_mask = nn.Conv2d(c2, 1, kernel_size=1) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) y = self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) # [N, C, T, H, W] residual = y # [N, C, T, H', W'] input_x = y # [N, C', T, H, W] query = self.conv_query(y) # [N, C', T, H', W'] key = self.conv_key(input_x) value = self.conv_value(input_x) # [N, C', H x W] query = query.view(query.size(0), query.size(1), -1) # [N, C', H' x W'] key = key.view(key.size(0), key.size(1), -1) value = value.view(value.size(0), value.size(1), -1) # channel whitening key_mean = key.mean(2).unsqueeze(2) query_mean = query.mean(2).unsqueeze(2) key -= key_mean query -= query_mean # [N, T x H x W, T x H' x W'] sim_map = torch.bmm(query.transpose(1, 2), key) sim_map = sim_map/self.scale sim_map = sim_map/self.temperature sim_map = self.softmax(sim_map) # [N, T x H x W, C'] out_sim = torch.bmm(sim_map, value.transpose(1, 2)) # [N, C', T x H x W] out_sim = out_sim.transpose(1, 2) # [N, C', T, H, W] out_sim = out_sim.view(out_sim.size(0), out_sim.size(1), *y.size()[2:]).contiguous() out_sim = self.gamma * out_sim # [N, 1, H', W'] mask = self.conv_mask(input_x) # [N, 1, H'x W'] mask = mask.view(mask.size(0), mask.size(1), -1) mask = self.softmax(mask) # [N, C, 1, 1] out_gc = torch.bmm(value, mask.permute(0,2,1)).unsqueeze(-1).contiguous() return out_sim + out_gc + residual class BottleneckCSP2(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSP2, self).__init__() c_ = int(c2) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv3 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x1 = self.cv1(x) y1 = self.m(x1) y2 = self.cv2(x1) return self.cv3(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class BottleneckCSP2SAM(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSP2SAM, self).__init__() c_ = int(c2) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cvsig = ConvSig(c_, c_, 1, 1) self.cv2 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv3 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) self.act = nn.SiLU() self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x1 = self.cv1(x) x1 = x1 * self.cvsig(x1).contiguous() y1 = self.m(x1) y2 = self.cv2(x1) return self.cv3(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class VoVCSP(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(VoVCSP, self).__init__() c_ = int(c2) # hidden channels self.cv1 = Conv(c1//2, c_//2, 3, 1) self.cv2 = Conv(c_//2, c_//2, 3, 1) self.cv3 = Conv(c_, c2, 1, 1) def forward(self, x): _, x1 = x.chunk(2, dim=1) x1 = self.cv1(x1) x2 = self.cv2(x1) return self.cv3(torch.cat((x1,x2), dim=1)) class SPP(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, k=(5, 9, 13)): super(SPP, self).__init__() c_ = c1 // 2 # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1) self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) def forward(self, x): x = self.cv1(x) return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1)) class SPPCSP(nn.Module): # CSP SPP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)): super(SPPCSP, self).__init__() c_ = int(2 * c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = Conv(c_, c_, 3, 1) self.cv4 = Conv(c_, c_, 1, 1) self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) self.cv5 = Conv(4 * c_, c_, 1, 1) self.cv6 = Conv(c_, c_, 3, 1) self.bn = nn.BatchNorm2d(2 * c_) self.act = nn.SiLU() self.cv7 = Conv(2 * c_, c2, 1, 1) def forward(self, x): x1 = self.cv4(self.cv3(self.cv1(x))) y1 = self.cv6(self.cv5(torch.cat([x1] + [m(x1) for m in self.m], 1))) y2 = self.cv2(x) return self.cv7(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class Focus(nn.Module): # Focus wh information into c-space def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super(Focus, self).__init__() self.conv = Conv(c1 * 4, c2, k, s, p, g, act) def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2) return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)) class MP(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, k=2): super(MP, self).__init__() self.m = nn.MaxPool2d(kernel_size=k, stride=k) def forward(self, x): return self.m(x) class DownD(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, n=1, k=2): super(DownD, self).__init__() c_ = int(c1) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c_, 3, k) self.cv3 = Conv(c_, c2, 1, 1) self.cv4 = Conv(c1, c2, 1, 1) self.ap = nn.AvgPool2d(kernel_size=k, stride=k) def forward(self, x): return self.cv3(self.cv2(self.cv1(x))) + self.cv4(self.ap(x)) class DownC(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, n=1, k=2): super(DownC, self).__init__() c_ = int(c1) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c2//2, 3, k) self.cv3 = Conv(c1, c2//2, 1, 1) self.mp = nn.MaxPool2d(kernel_size=k, stride=k) def forward(self, x): return torch.cat((self.cv2(self.cv1(x)), self.cv3(self.mp(x))), dim=1) class DNL(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, k=3, s=1): super(DNL, self).__init__() c_ = int(c1) # hidden channels # self.conv_query = nn.Conv2d(c1, c_, kernel_size=1) self.conv_key = nn.Conv2d(c1, c_, kernel_size=1) self.conv_value = nn.Conv2d(c1, c1, kernel_size=1, bias=False) self.conv_out = None self.scale = math.sqrt(c_) self.temperature = 0.05 self.softmax = nn.Softmax(dim=2) self.gamma = nn.Parameter(torch.zeros(1)) self.conv_mask = nn.Conv2d(c1, 1, kernel_size=1) self.cv = Conv(c1, c2, k, s) def forward(self, x): # [N, C, T, H, W] residual = x # [N, C, T, H', W'] input_x = x # [N, C', T, H, W] query = self.conv_query(x) # [N, C', T, H', W'] key = self.conv_key(input_x) value = self.conv_value(input_x) # [N, C', H x W] query = query.view(query.size(0), query.size(1), -1) # [N, C', H' x W'] key = key.view(key.size(0), key.size(1), -1) value = value.view(value.size(0), value.size(1), -1) # channel whitening key_mean = key.mean(2).unsqueeze(2) query_mean = query.mean(2).unsqueeze(2) key -= key_mean query -= query_mean # [N, T x H x W, T x H' x W'] sim_map = torch.bmm(query.transpose(1, 2), key) sim_map = sim_map/self.scale sim_map = sim_map/self.temperature sim_map = self.softmax(sim_map) # [N, T x H x W, C'] out_sim = torch.bmm(sim_map, value.transpose(1, 2)) # [N, C', T x H x W] out_sim = out_sim.transpose(1, 2) # [N, C', T, H, W] out_sim = out_sim.view(out_sim.size(0), out_sim.size(1), *x.size()[2:]) out_sim = self.gamma * out_sim # [N, 1, H', W'] mask = self.conv_mask(input_x) # [N, 1, H'x W'] mask = mask.view(mask.size(0), mask.size(1), -1) mask = self.softmax(mask) # [N, C, 1, 1] out_gc = torch.bmm(value, mask.permute(0,2,1)).unsqueeze(-1) out_sim = out_sim+out_gc return self.cv(out_sim + residual) class GC(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, k=3, s=1): super(GC, self).__init__() c_ = int(c1) # hidden channels # self.channel_add_conv = nn.Sequential( nn.Conv2d(c1, c_, kernel_size=1), nn.LayerNorm([c_, 1, 1]), nn.ReLU(inplace=True), # yapf: disable nn.Conv2d(c_, c1, kernel_size=1)) self.conv_mask = nn.Conv2d(c_, 1, kernel_size=1) self.softmax = nn.Softmax(dim=2) self.cv = Conv(c1, c2, k, s) def spatial_pool(self, x): batch, channel, height, width = x.size() input_x = x # [N, C, H * W] input_x = input_x.view(batch, channel, height * width) # [N, 1, C, H * W] input_x = input_x.unsqueeze(1) # [N, 1, H, W] context_mask = self.conv_mask(x) # [N, 1, H * W] context_mask = context_mask.view(batch, 1, height * width) # [N, 1, H * W] context_mask = self.softmax(context_mask) # [N, 1, H * W, 1] context_mask = context_mask.unsqueeze(-1) # [N, 1, C, 1] context = torch.matmul(input_x, context_mask) # [N, C, 1, 1] context = context.view(batch, channel, 1, 1) return context def forward(self, x): return self.cv(x + self.channel_add_conv(self.spatial_pool(x))) class SAM(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, k=3, s=1): super(SAM, self).__init__() c_ = int(c1) # hidden channels self.cvsig = ConvSig(c1, c1, 1, 1) self.cv = Conv(c1, c2, k, s) def forward(self, x): return self.cv(x * self.cvsig(x)) class SAMA(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, k=3, s=1): super(SAMA, self).__init__() c_ = int(c1) # hidden channels self.cvsig = ConvSig(c1, c1, 1, 1) self.cv = Conv(c1, c2, k, s) def forward(self, x): return self.cv(x + x * self.cvsig(x)) class SAMB(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, k=3, s=1): super(SAMB, self).__init__() c_ = int(c1) # hidden channels self.cv = Conv(c1, c2, k, s) self.cvsig = ConvSig(c2, c2, 1, 1) def forward(self, x): x = self.cv(x) return x * self.cvsig(x) class Concat(nn.Module): # Concatenate a list of tensors along dimension def __init__(self, dimension=1): super(Concat, self).__init__() self.d = dimension def forward(self, x): return torch.cat(x, self.d) class NMS(nn.Module): # Non-Maximum Suppression (NMS) module conf = 0.25 # confidence threshold iou = 0.45 # IoU threshold classes = None # (optional list) filter by class def __init__(self): super(NMS, self).__init__() def forward(self, x): return non_max_suppression(x[0], conf_thres=self.conf, iou_thres=self.iou, classes=self.classes) class autoShape(nn.Module): # input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS img_size = 640 # inference size (pixels) conf = 0.25 # NMS confidence threshold iou = 0.45 # NMS IoU threshold classes = None # (optional list) filter by class def __init__(self, model): super(autoShape, self).__init__() self.model = model.eval() def forward(self, imgs, size=640, augment=False, profile=False): # supports inference from various sources. For height=720, width=1280, RGB images example inputs are: # opencv: imgs = cv2.imread('image.jpg')[:,:,::-1] # HWC BGR to RGB x(720,1280,3) # PIL: imgs = Image.open('image.jpg') # HWC x(720,1280,3) # numpy: imgs = np.zeros((720,1280,3)) # HWC # torch: imgs = torch.zeros(16,3,720,1280) # BCHW # multiple: imgs = [Image.open('image1.jpg'), Image.open('image2.jpg'), ...] # list of images p = next(self.model.parameters()) # for device and type if isinstance(imgs, torch.Tensor): # torch return self.model(imgs.to(p.device).type_as(p), augment, profile) # inference # Pre-process if not isinstance(imgs, list): imgs = [imgs] shape0, shape1 = [], [] # image and inference shapes batch = range(len(imgs)) # batch size for i in batch: imgs[i] = np.array(imgs[i]) # to numpy imgs[i] = imgs[i][:, :, :3] if imgs[i].ndim == 3 else np.tile(imgs[i][:, :, None], 3) # enforce 3ch input s = imgs[i].shape[:2] # HWC shape0.append(s) # image shape g = (size / max(s)) # gain shape1.append([y * g for y in s]) shape1 = [make_divisible(x, int(self.stride.max())) for x in np.stack(shape1, 0).max(0)] # inference shape x = [letterbox(imgs[i], new_shape=shape1, auto=False)[0] for i in batch] # pad x = np.stack(x, 0) if batch[-1] else x[0][None] # stack x = np.ascontiguousarray(x.transpose((0, 3, 1, 2))) # BHWC to BCHW x = torch.from_numpy(x).to(p.device).type_as(p) / 255. # uint8 to fp16/32 # Inference with torch.no_grad(): y = self.model(x, augment, profile)[0] # forward y = non_max_suppression(y, conf_thres=self.conf, iou_thres=self.iou, classes=self.classes) # NMS # Post-process for i in batch: if y[i] is not None: y[i][:, :4] = scale_coords(shape1, y[i][:, :4], shape0[i]) return Detections(imgs, y, self.names) class Detections: # detections class for YOLOv5 inference results def __init__(self, imgs, pred, names=None): super(Detections, self).__init__() self.imgs = imgs # list of images as numpy arrays self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls) self.names = names # class names self.xyxy = pred # xyxy pixels self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels gn = [torch.Tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1., 1.]) for im in imgs] # normalization gains self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized def display(self, pprint=False, show=False, save=False): colors = color_list() for i, (img, pred) in enumerate(zip(self.imgs, self.pred)): str = f'Image {i + 1}/{len(self.pred)}: {img.shape[0]}x{img.shape[1]} ' if pred is not None: for c in pred[:, -1].unique(): n = (pred[:, -1] == c).sum() # detections per class str += f'{n} {self.names[int(c)]}s, ' # add to string if show or save: img = Image.fromarray(img.astype(np.uint8)) if isinstance(img, np.ndarray) else img # from np for *box, conf, cls in pred: # xyxy, confidence, class # str += '%s %.2f, ' % (names[int(cls)], conf) # label ImageDraw.Draw(img).rectangle(box, width=4, outline=colors[int(cls) % 10]) # plot if save: f = f'results{i}.jpg' str += f"saved to '{f}'" img.save(f) # save if show: img.show(f'Image {i}') # show if pprint: print(str) def print(self): self.display(pprint=True) # print results def show(self): self.display(show=True) # show results def save(self): self.display(save=True) # save results class Flatten(nn.Module): # Use after nn.AdaptiveAvgPool2d(1) to remove last 2 dimensions @staticmethod def forward(x): return x.view(x.size(0), -1) class Classify(nn.Module): # Classification head, i.e. x(b,c1,20,20) to x(b,c2) def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups super(Classify, self).__init__() self.aap = nn.AdaptiveAvgPool2d(1) # to x(b,c1,1,1) self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) # to x(b,c2,1,1) self.flat = Flatten() def forward(self, x): z = torch.cat([self.aap(y) for y in (x if isinstance(x, list) else [x])], 1) # cat if list return self.flat(self.conv(z)) # flatten to x(b,c2) class TransformerLayer(nn.Module): def __init__(self, c, num_heads): super().__init__() self.ln1 = nn.LayerNorm(c) self.q = nn.Linear(c, c, bias=False) self.k = nn.Linear(c, c, bias=False) self.v = nn.Linear(c, c, bias=False) self.ma = nn.MultiheadAttention(embed_dim=c, num_heads=num_heads) self.ln2 = nn.LayerNorm(c) self.fc1 = nn.Linear(c, c, bias=False) self.fc2 = nn.Linear(c, c, bias=False) def forward(self, x): x_ = self.ln1(x) x = self.ma(self.q(x_), self.k(x_), self.v(x_))[0] + x x = self.ln2(x) x = self.fc2(self.fc1(x)) + x return x class TransformerBlock(nn.Module): def __init__(self, c1, c2, num_heads, num_layers): super().__init__() self.conv = None if c1 != c2: self.conv = Conv(c1, c2) self.linear = nn.Linear(c2, c2) self.tr = nn.Sequential(*[TransformerLayer(c2, num_heads) for _ in range(num_layers)]) self.c2 = c2 def forward(self, x): if self.conv is not None: x = self.conv(x) b, _, w, h = x.shape p = x.flatten(2) p = p.unsqueeze(0) p = p.transpose(0, 3) p = p.squeeze(3) e = self.linear(p) x = p + e x = self.tr(x) x = x.unsqueeze(3) x = x.transpose(0, 3) x = x.reshape(b, self.c2, w, h) return x class BottleneckCSPTR(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPTR, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = TransformerBlock(c_, c_, 4, n) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class BottleneckCSP2TR(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSP2TR, self).__init__() c_ = int(c2) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv3 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) self.act = nn.SiLU() self.m = TransformerBlock(c_, c_, 4, n) def forward(self, x): x1 = self.cv1(x) y1 = self.m(x1) y2 = self.cv2(x1) return self.cv3(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class SPPCSPTR(nn.Module): # CSP SPP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)): super(SPPCSPTR, self).__init__() c_ = int(2 * c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = Conv(c_, c_, 3, 1) self.cv4 = Conv(c_, c_, 1, 1) self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) self.cv5 = Conv(4 * c_, c_, 1, 1) self.cv6 = TransformerBlock(c_, c_, 4, 1) self.bn = nn.BatchNorm2d(2 * c_) self.act = nn.SiLU() self.cv7 = Conv(2 * c_, c2, 1, 1) def forward(self, x): x1 = self.cv4(self.cv3(self.cv1(x))) y1 = self.cv6(self.cv5(torch.cat([x1] + [m(x1) for m in self.m], 1))) y2 = self.cv2(x) return self.cv7(self.act(self.bn(torch.cat((y1, y2), dim=1)))) class TR(BottleneckCSPTR): def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) self.m = TransformerBlock(c_, c_, 4, n) ================================================ FILE: asone/detectors/yolor/models/export.py ================================================ import argparse import torch from asone.detectors.yolor.utils.google_utils import attempt_download if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--weights', type=str, default='./yolov4.pt', help='weights path') parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size') parser.add_argument('--batch-size', type=int, default=1, help='batch size') opt = parser.parse_args() opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand print(opt) # Input img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size(1,3,320,192) iDetection # Load PyTorch model attempt_download(opt.weights) model = torch.load(opt.weights, map_location=torch.device('cpu'))['model'].float() model.eval() model.model[-1].export = True # set Detect() layer export=True y = model(img) # dry run # TorchScript export try: print('\nStarting TorchScript export with torch %s...' % torch.__version__) f = opt.weights.replace('.pt', '.torchscript.pt') # filename ts = torch.jit.trace(model, img) ts.save(f) print('TorchScript export success, saved as %s' % f) except Exception as e: print('TorchScript export failure: %s' % e) # ONNX export try: import onnx print('\nStarting ONNX export with onnx %s...' % onnx.__version__) f = opt.weights.replace('.pt', '.onnx') # filename model.fuse() # only for ONNX torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'], output_names=['classes', 'boxes'] if y is None else ['output']) # Checks onnx_model = onnx.load(f) # load onnx model onnx.checker.check_model(onnx_model) # check onnx model print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable model print('ONNX export success, saved as %s' % f) except Exception as e: print('ONNX export failure: %s' % e) # CoreML export try: import coremltools as ct print('\nStarting CoreML export with coremltools %s...' % ct.__version__) # convert model from torchscript and apply pixel scaling as per detect.py model = ct.convert(ts, inputs=[ct.ImageType(name='images', shape=img.shape, scale=1 / 255.0, bias=[0, 0, 0])]) f = opt.weights.replace('.pt', '.mlmodel') # filename model.save(f) print('CoreML export success, saved as %s' % f) except Exception as e: print('CoreML export failure: %s' % e) # Finish print('\nExport complete. Visualize with https://github.com/lutzroeder/netron.') ================================================ FILE: asone/detectors/yolor/models/models.py ================================================ from asone.detectors.yolor.utils.google_utils import * from asone.detectors.yolor.utils.layers import * from asone.detectors.yolor.utils.parse_config import * from asone.detectors.yolor.utils import torch_utils ONNX_EXPORT = False def create_modules(module_defs, img_size, cfg): # Constructs module list of layer blocks from module configuration in module_defs img_size = [img_size] * 2 if isinstance(img_size, int) else img_size # expand if necessary _ = module_defs.pop(0) # cfg training hyperparams (unused) output_filters = [3] # input channels module_list = nn.ModuleList() routs = [] # list of layers which rout to deeper layers yolo_index = -1 for i, mdef in enumerate(module_defs): modules = nn.Sequential() if mdef['type'] == 'convolutional': bn = mdef['batch_normalize'] filters = mdef['filters'] k = mdef['size'] # kernel size stride = mdef['stride'] if 'stride' in mdef else (mdef['stride_y'], mdef['stride_x']) if isinstance(k, int): # single-size conv modules.add_module('Conv2d', nn.Conv2d(in_channels=output_filters[-1], out_channels=filters, kernel_size=k, stride=stride, padding=k // 2 if mdef['pad'] else 0, groups=mdef['groups'] if 'groups' in mdef else 1, bias=not bn)) else: # multiple-size conv modules.add_module('MixConv2d', MixConv2d(in_ch=output_filters[-1], out_ch=filters, k=k, stride=stride, bias=not bn)) if bn: modules.add_module('BatchNorm2d', nn.BatchNorm2d(filters, momentum=0.03, eps=1E-4)) else: routs.append(i) # detection output (goes into yolo layer) if mdef['activation'] == 'leaky': # activation study https://github.com/ultralytics/yolov3/issues/441 modules.add_module('activation', nn.LeakyReLU(0.1, inplace=True)) elif mdef['activation'] == 'swish': modules.add_module('activation', Swish()) elif mdef['activation'] == 'mish': modules.add_module('activation', Mish()) elif mdef['activation'] == 'emb': modules.add_module('activation', F.normalize()) elif mdef['activation'] == 'logistic': modules.add_module('activation', nn.Sigmoid()) elif mdef['activation'] == 'silu': modules.add_module('activation', nn.SiLU()) elif mdef['type'] == 'deformableconvolutional': bn = mdef['batch_normalize'] filters = mdef['filters'] k = mdef['size'] # kernel size stride = mdef['stride'] if 'stride' in mdef else (mdef['stride_y'], mdef['stride_x']) if isinstance(k, int): # single-size conv modules.add_module('DeformConv2d', DeformConv2d(output_filters[-1], filters, kernel_size=k, padding=k // 2 if mdef['pad'] else 0, stride=stride, bias=not bn, modulation=True)) else: # multiple-size conv modules.add_module('MixConv2d', MixConv2d(in_ch=output_filters[-1], out_ch=filters, k=k, stride=stride, bias=not bn)) if bn: modules.add_module('BatchNorm2d', nn.BatchNorm2d(filters, momentum=0.03, eps=1E-4)) else: routs.append(i) # detection output (goes into yolo layer) if mdef['activation'] == 'leaky': # activation study https://github.com/ultralytics/yolov3/issues/441 modules.add_module('activation', nn.LeakyReLU(0.1, inplace=True)) elif mdef['activation'] == 'swish': modules.add_module('activation', Swish()) elif mdef['activation'] == 'mish': modules.add_module('activation', Mish()) elif mdef['activation'] == 'silu': modules.add_module('activation', nn.SiLU()) elif mdef['type'] == 'dropout': p = mdef['probability'] modules = nn.Dropout(p) elif mdef['type'] == 'avgpool': modules = GAP() elif mdef['type'] == 'silence': filters = output_filters[-1] modules = Silence() elif mdef['type'] == 'scale_channels': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] routs.extend([i + l if l < 0 else l for l in layers]) modules = ScaleChannel(layers=layers) elif mdef['type'] == 'shift_channels': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] routs.extend([i + l if l < 0 else l for l in layers]) modules = ShiftChannel(layers=layers) elif mdef['type'] == 'shift_channels_2d': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] routs.extend([i + l if l < 0 else l for l in layers]) modules = ShiftChannel2D(layers=layers) elif mdef['type'] == 'control_channels': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] routs.extend([i + l if l < 0 else l for l in layers]) modules = ControlChannel(layers=layers) elif mdef['type'] == 'control_channels_2d': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] routs.extend([i + l if l < 0 else l for l in layers]) modules = ControlChannel2D(layers=layers) elif mdef['type'] == 'alternate_channels': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] * 2 routs.extend([i + l if l < 0 else l for l in layers]) modules = AlternateChannel(layers=layers) elif mdef['type'] == 'alternate_channels_2d': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] * 2 routs.extend([i + l if l < 0 else l for l in layers]) modules = AlternateChannel2D(layers=layers) elif mdef['type'] == 'select_channels': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] routs.extend([i + l if l < 0 else l for l in layers]) modules = SelectChannel(layers=layers) elif mdef['type'] == 'select_channels_2d': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] routs.extend([i + l if l < 0 else l for l in layers]) modules = SelectChannel2D(layers=layers) elif mdef['type'] == 'sam': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] routs.extend([i + l if l < 0 else l for l in layers]) modules = ScaleSpatial(layers=layers) elif mdef['type'] == 'BatchNorm2d': filters = output_filters[-1] modules = nn.BatchNorm2d(filters, momentum=0.03, eps=1E-4) if i == 0 and filters == 3: # normalize RGB image # imagenet mean and var https://pytorch.org/docs/stable/torchvision/models.html#classification modules.running_mean = torch.tensor([0.485, 0.456, 0.406]) modules.running_var = torch.tensor([0.0524, 0.0502, 0.0506]) elif mdef['type'] == 'maxpool': k = mdef['size'] # kernel size stride = mdef['stride'] maxpool = nn.MaxPool2d(kernel_size=k, stride=stride, padding=(k - 1) // 2) if k == 2 and stride == 1: # yolov3-tiny modules.add_module('ZeroPad2d', nn.ZeroPad2d((0, 1, 0, 1))) modules.add_module('MaxPool2d', maxpool) else: modules = maxpool elif mdef['type'] == 'local_avgpool': k = mdef['size'] # kernel size stride = mdef['stride'] avgpool = nn.AvgPool2d(kernel_size=k, stride=stride, padding=(k - 1) // 2) if k == 2 and stride == 1: # yolov3-tiny modules.add_module('ZeroPad2d', nn.ZeroPad2d((0, 1, 0, 1))) modules.add_module('AvgPool2d', avgpool) else: modules = avgpool elif mdef['type'] == 'upsample': if ONNX_EXPORT: # explicitly state size, avoid scale_factor g = (yolo_index + 1) * 2 / 32 # gain modules = nn.Upsample(size=tuple(int(x * g) for x in img_size)) # img_size = (320, 192) else: modules = nn.Upsample(scale_factor=mdef['stride']) elif mdef['type'] == 'route': # nn.Sequential() placeholder for 'route' layer layers = mdef['layers'] filters = sum([output_filters[l + 1 if l > 0 else l] for l in layers]) routs.extend([i + l if l < 0 else l for l in layers]) modules = FeatureConcat(layers=layers) elif mdef['type'] == 'route2': # nn.Sequential() placeholder for 'route' layer layers = mdef['layers'] filters = sum([output_filters[l + 1 if l > 0 else l] for l in layers]) routs.extend([i + l if l < 0 else l for l in layers]) modules = FeatureConcat2(layers=layers) elif mdef['type'] == 'route3': # nn.Sequential() placeholder for 'route' layer layers = mdef['layers'] filters = sum([output_filters[l + 1 if l > 0 else l] for l in layers]) routs.extend([i + l if l < 0 else l for l in layers]) modules = FeatureConcat3(layers=layers) elif mdef['type'] == 'route_lhalf': # nn.Sequential() placeholder for 'route' layer layers = mdef['layers'] filters = sum([output_filters[l + 1 if l > 0 else l] for l in layers])//2 routs.extend([i + l if l < 0 else l for l in layers]) modules = FeatureConcat_l(layers=layers) elif mdef['type'] == 'shortcut': # nn.Sequential() placeholder for 'shortcut' layer layers = mdef['from'] filters = output_filters[-1] routs.extend([i + l if l < 0 else l for l in layers]) modules = WeightedFeatureFusion(layers=layers, weight='weights_type' in mdef) elif mdef['type'] == 'reorg3d': # yolov3-spp-pan-scale pass elif mdef['type'] == 'reorg': # yolov3-spp-pan-scale filters = 4 * output_filters[-1] modules.add_module('Reorg', Reorg()) elif mdef['type'] == 'dwt': # yolov3-spp-pan-scale filters = 4 * output_filters[-1] modules.add_module('DWT', DWT()) elif mdef['type'] == 'implicit_add': # yolov3-spp-pan-scale filters = mdef['filters'] modules = ImplicitA(channel=filters) elif mdef['type'] == 'implicit_mul': # yolov3-spp-pan-scale filters = mdef['filters'] modules = ImplicitM(channel=filters) elif mdef['type'] == 'implicit_cat': # yolov3-spp-pan-scale filters = mdef['filters'] modules = ImplicitC(channel=filters) elif mdef['type'] == 'implicit_add_2d': # yolov3-spp-pan-scale channels = mdef['filters'] filters = mdef['atoms'] modules = Implicit2DA(atom=filters, channel=channels) elif mdef['type'] == 'implicit_mul_2d': # yolov3-spp-pan-scale channels = mdef['filters'] filters = mdef['atoms'] modules = Implicit2DM(atom=filters, channel=channels) elif mdef['type'] == 'implicit_cat_2d': # yolov3-spp-pan-scale channels = mdef['filters'] filters = mdef['atoms'] modules = Implicit2DC(atom=filters, channel=channels) elif mdef['type'] == 'yolo': yolo_index += 1 stride = [8, 16, 32, 64, 128] # P3, P4, P5, P6, P7 strides if any(x in cfg for x in ['yolov4-tiny', 'fpn', 'yolov3']): # P5, P4, P3 strides stride = [32, 16, 8] layers = mdef['from'] if 'from' in mdef else [] modules = YOLOLayer(anchors=mdef['anchors'][mdef['mask']], # anchor list nc=mdef['classes'], # number of classes img_size=img_size, # (416, 416) yolo_index=yolo_index, # 0, 1, 2... layers=layers, # output layers stride=stride[yolo_index]) # Initialize preceding Conv2d() bias (https://arxiv.org/pdf/1708.02002.pdf section 3.3) try: j = layers[yolo_index] if 'from' in mdef else -2 bias_ = module_list[j][0].bias # shape(255,) bias = bias_[:modules.no * modules.na].view(modules.na, -1) # shape(3,85) #bias[:, 4] += -4.5 # obj bias.data[:, 4] += math.log(8 / (640 / stride[yolo_index]) ** 2) # obj (8 objects per 640 image) bias.data[:, 5:] += math.log(0.6 / (modules.nc - 0.99)) # cls (sigmoid(p) = 1/nc) module_list[j][0].bias = torch.nn.Parameter(bias_, requires_grad=bias_.requires_grad) #j = [-2, -5, -8] #for sj in j: # bias_ = module_list[sj][0].bias # bias = bias_[:modules.no * 1].view(1, -1) # bias.data[:, 4] += math.log(8 / (640 / stride[yolo_index]) ** 2) # bias.data[:, 5:] += math.log(0.6 / (modules.nc - 0.99)) # module_list[sj][0].bias = torch.nn.Parameter(bias_, requires_grad=bias_.requires_grad) except: print('WARNING: smart bias initialization failure.') elif mdef['type'] == 'jde': yolo_index += 1 stride = [8, 16, 32, 64, 128] # P3, P4, P5, P6, P7 strides if any(x in cfg for x in ['yolov4-tiny', 'fpn', 'yolov3']): # P5, P4, P3 strides stride = [32, 16, 8] layers = mdef['from'] if 'from' in mdef else [] modules = JDELayer(anchors=mdef['anchors'][mdef['mask']], # anchor list nc=mdef['classes'], # number of classes img_size=img_size, # (416, 416) yolo_index=yolo_index, # 0, 1, 2... layers=layers, # output layers stride=stride[yolo_index]) # Initialize preceding Conv2d() bias (https://arxiv.org/pdf/1708.02002.pdf section 3.3) try: j = layers[yolo_index] if 'from' in mdef else -1 bias_ = module_list[j][0].bias # shape(255,) bias = bias_[:modules.no * modules.na].view(modules.na, -1) # shape(3,85) #bias[:, 4] += -4.5 # obj bias.data[:, 4] += math.log(8 / (640 / stride[yolo_index]) ** 2) # obj (8 objects per 640 image) bias.data[:, 5:] += math.log(0.6 / (modules.nc - 0.99)) # cls (sigmoid(p) = 1/nc) module_list[j][0].bias = torch.nn.Parameter(bias_, requires_grad=bias_.requires_grad) except: print('WARNING: smart bias initialization failure.') else: print('Warning: Unrecognized Layer Type: ' + mdef['type']) # Register module list and number of output filters module_list.append(modules) output_filters.append(filters) routs_binary = [False] * (i + 1) for i in routs: routs_binary[i] = True return module_list, routs_binary class YOLOLayer(nn.Module): def __init__(self, anchors, nc, img_size, yolo_index, layers, stride): super(YOLOLayer, self).__init__() self.anchors = torch.Tensor(anchors) self.index = yolo_index # index of this layer in layers self.layers = layers # model output layer indices self.stride = stride # layer stride self.nl = len(layers) # number of output layers (3) self.na = len(anchors) # number of anchors (3) self.nc = nc # number of classes (80) self.no = nc + 5 # number of outputs (85) self.nx, self.ny, self.ng = 0, 0, 0 # initialize number of x, y gridpoints self.anchor_vec = self.anchors / self.stride self.anchor_wh = self.anchor_vec.view(1, self.na, 1, 1, 2) if ONNX_EXPORT: self.training = False self.create_grids((img_size[1] // stride, img_size[0] // stride)) # number x, y grid points def create_grids(self, ng=(13, 13), device='cpu'): self.nx, self.ny = ng # x and y grid size self.ng = torch.tensor(ng, dtype=torch.float) # build xy offsets if not self.training: yv, xv = torch.meshgrid([torch.arange(self.ny, device=device), torch.arange(self.nx, device=device)]) self.grid = torch.stack((xv, yv), 2).view((1, 1, self.ny, self.nx, 2)).float() if self.anchor_vec.device != device: self.anchor_vec = self.anchor_vec.to(device) self.anchor_wh = self.anchor_wh.to(device) def forward(self, p, out): ASFF = False # https://arxiv.org/abs/1911.09516 if ASFF: i, n = self.index, self.nl # index in layers, number of layers p = out[self.layers[i]] bs, _, ny, nx = p.shape # bs, 255, 13, 13 if (self.nx, self.ny) != (nx, ny): self.create_grids((nx, ny), p.device) # outputs and weights # w = F.softmax(p[:, -n:], 1) # normalized weights w = torch.sigmoid(p[:, -n:]) * (2 / n) # sigmoid weights (faster) # w = w / w.sum(1).unsqueeze(1) # normalize across layer dimension # weighted ASFF sum p = out[self.layers[i]][:, :-n] * w[:, i:i + 1] for j in range(n): if j != i: p += w[:, j:j + 1] * \ F.interpolate(out[self.layers[j]][:, :-n], size=[ny, nx], mode='bilinear', align_corners=False) elif ONNX_EXPORT: bs = 1 # batch size else: bs, _, ny, nx = p.shape # bs, 255, 13, 13 if (self.nx, self.ny) != (nx, ny): self.create_grids((nx, ny), p.device) # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh) p = p.view(bs, self.na, self.no, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction if self.training: return p elif ONNX_EXPORT: # Avoid broadcasting for ANE operations m = self.na * self.nx * self.ny ng = 1. / self.ng.repeat(m, 1) grid = self.grid.repeat(1, self.na, 1, 1, 1).view(m, 2) anchor_wh = self.anchor_wh.repeat(1, 1, self.nx, self.ny, 1).view(m, 2) * ng p = p.view(m, self.no) xy = torch.sigmoid(p[:, 0:2]) + grid # x, y wh = torch.exp(p[:, 2:4]) * anchor_wh # width, height p_cls = torch.sigmoid(p[:, 4:5]) if self.nc == 1 else \ torch.sigmoid(p[:, 5:self.no]) * torch.sigmoid(p[:, 4:5]) # conf return p_cls, xy * ng, wh else: # inference io = p.sigmoid() io[..., :2] = (io[..., :2] * 2. - 0.5 + self.grid) io[..., 2:4] = (io[..., 2:4] * 2) ** 2 * self.anchor_wh io[..., :4] *= self.stride #io = p.clone() # inference output #io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid # xy #io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh # wh yolo method #io[..., :4] *= self.stride #torch.sigmoid_(io[..., 4:]) return io.view(bs, -1, self.no), p # view [1, 3, 13, 13, 85] as [1, 507, 85] class JDELayer(nn.Module): def __init__(self, anchors, nc, img_size, yolo_index, layers, stride): super(JDELayer, self).__init__() self.anchors = torch.Tensor(anchors) self.index = yolo_index # index of this layer in layers self.layers = layers # model output layer indices self.stride = stride # layer stride self.nl = len(layers) # number of output layers (3) self.na = len(anchors) # number of anchors (3) self.nc = nc # number of classes (80) self.no = nc + 5 # number of outputs (85) self.nx, self.ny, self.ng = 0, 0, 0 # initialize number of x, y gridpoints self.anchor_vec = self.anchors / self.stride self.anchor_wh = self.anchor_vec.view(1, self.na, 1, 1, 2) if ONNX_EXPORT: self.training = False self.create_grids((img_size[1] // stride, img_size[0] // stride)) # number x, y grid points def create_grids(self, ng=(13, 13), device='cpu'): self.nx, self.ny = ng # x and y grid size self.ng = torch.tensor(ng, dtype=torch.float) # build xy offsets if not self.training: yv, xv = torch.meshgrid([torch.arange(self.ny, device=device), torch.arange(self.nx, device=device)]) self.grid = torch.stack((xv, yv), 2).view((1, 1, self.ny, self.nx, 2)).float() if self.anchor_vec.device != device: self.anchor_vec = self.anchor_vec.to(device) self.anchor_wh = self.anchor_wh.to(device) def forward(self, p, out): ASFF = False # https://arxiv.org/abs/1911.09516 if ASFF: i, n = self.index, self.nl # index in layers, number of layers p = out[self.layers[i]] bs, _, ny, nx = p.shape # bs, 255, 13, 13 if (self.nx, self.ny) != (nx, ny): self.create_grids((nx, ny), p.device) # outputs and weights # w = F.softmax(p[:, -n:], 1) # normalized weights w = torch.sigmoid(p[:, -n:]) * (2 / n) # sigmoid weights (faster) # w = w / w.sum(1).unsqueeze(1) # normalize across layer dimension # weighted ASFF sum p = out[self.layers[i]][:, :-n] * w[:, i:i + 1] for j in range(n): if j != i: p += w[:, j:j + 1] * \ F.interpolate(out[self.layers[j]][:, :-n], size=[ny, nx], mode='bilinear', align_corners=False) elif ONNX_EXPORT: bs = 1 # batch size else: bs, _, ny, nx = p.shape # bs, 255, 13, 13 if (self.nx, self.ny) != (nx, ny): self.create_grids((nx, ny), p.device) # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh) p = p.view(bs, self.na, self.no, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction if self.training: return p elif ONNX_EXPORT: # Avoid broadcasting for ANE operations m = self.na * self.nx * self.ny ng = 1. / self.ng.repeat(m, 1) grid = self.grid.repeat(1, self.na, 1, 1, 1).view(m, 2) anchor_wh = self.anchor_wh.repeat(1, 1, self.nx, self.ny, 1).view(m, 2) * ng p = p.view(m, self.no) xy = torch.sigmoid(p[:, 0:2]) + grid # x, y wh = torch.exp(p[:, 2:4]) * anchor_wh # width, height p_cls = torch.sigmoid(p[:, 4:5]) if self.nc == 1 else \ torch.sigmoid(p[:, 5:self.no]) * torch.sigmoid(p[:, 4:5]) # conf return p_cls, xy * ng, wh else: # inference #io = p.sigmoid() #io[..., :2] = (io[..., :2] * 2. - 0.5 + self.grid) #io[..., 2:4] = (io[..., 2:4] * 2) ** 2 * self.anchor_wh #io[..., :4] *= self.stride io = p.clone() # inference output io[..., :2] = torch.sigmoid(io[..., :2]) * 2. - 0.5 + self.grid # xy io[..., 2:4] = (torch.sigmoid(io[..., 2:4]) * 2) ** 2 * self.anchor_wh # wh yolo method io[..., :4] *= self.stride io[..., 4:] = F.softmax(io[..., 4:]) return io.view(bs, -1, self.no), p # view [1, 3, 13, 13, 85] as [1, 507, 85] class Darknet(nn.Module): # YOLOv3 object detection model def __init__(self, cfg, img_size=(416, 416), verbose=False): super(Darknet, self).__init__() self.module_defs = parse_model_cfg(cfg) self.module_list, self.routs = create_modules(self.module_defs, img_size, cfg) self.yolo_layers = get_yolo_layers(self) # torch_utils.initialize_weights(self) # Darknet Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346 self.version = np.array([0, 2, 5], dtype=np.int32) # (int32) version info: major, minor, revision self.seen = np.array([0], dtype=np.int64) # (int64) number of images seen during training self.info(verbose) if not ONNX_EXPORT else None # print model description def forward(self, x, augment=False, verbose=False): if not augment: return self.forward_once(x) else: # Augment images (inference and test only) https://github.com/ultralytics/yolov3/issues/931 img_size = x.shape[-2:] # height, width s = [0.83, 0.67] # scales y = [] for i, xi in enumerate((x, torch_utils.scale_img(x.flip(3), s[0], same_shape=False), # flip-lr and scale torch_utils.scale_img(x, s[1], same_shape=False), # scale )): # cv2.imwrite('img%g.jpg' % i, 255 * xi[0].numpy().transpose((1, 2, 0))[:, :, ::-1]) y.append(self.forward_once(xi)[0]) y[1][..., :4] /= s[0] # scale y[1][..., 0] = img_size[1] - y[1][..., 0] # flip lr y[2][..., :4] /= s[1] # scale # for i, yi in enumerate(y): # coco small, medium, large = < 32**2 < 96**2 < # area = yi[..., 2:4].prod(2)[:, :, None] # if i == 1: # yi *= (area < 96. ** 2).float() # elif i == 2: # yi *= (area > 32. ** 2).float() # y[i] = yi y = torch.cat(y, 1) return y, None def forward_once(self, x, augment=False, verbose=False): img_size = x.shape[-2:] # height, width yolo_out, out = [], [] if verbose: print('0', x.shape) str = '' # Augment images (inference and test only) if augment: # https://github.com/ultralytics/yolov3/issues/931 nb = x.shape[0] # batch size s = [0.83, 0.67] # scales x = torch.cat((x, torch_utils.scale_img(x.flip(3), s[0]), # flip-lr and scale torch_utils.scale_img(x, s[1]), # scale ), 0) for i, module in enumerate(self.module_list): name = module.__class__.__name__ #print(name) if name in ['WeightedFeatureFusion', 'FeatureConcat', 'FeatureConcat2', 'FeatureConcat3', 'FeatureConcat_l', 'ScaleChannel', 'ShiftChannel', 'ShiftChannel2D', 'ControlChannel', 'ControlChannel2D', 'AlternateChannel', 'AlternateChannel2D', 'SelectChannel', 'SelectChannel2D', 'ScaleSpatial']: # sum, concat if verbose: l = [i - 1] + module.layers # layers sh = [list(x.shape)] + [list(out[i].shape) for i in module.layers] # shapes str = ' >> ' + ' + '.join(['layer %g %s' % x for x in zip(l, sh)]) x = module(x, out) # WeightedFeatureFusion(), FeatureConcat() elif name in ['ImplicitA', 'ImplicitM', 'ImplicitC', 'Implicit2DA', 'Implicit2DM', 'Implicit2DC']: x = module() elif name == 'YOLOLayer': yolo_out.append(module(x, out)) elif name == 'JDELayer': yolo_out.append(module(x, out)) else: # run module directly, i.e. mtype = 'convolutional', 'upsample', 'maxpool', 'batchnorm2d' etc. #print(module) #print(x.shape) x = module(x) out.append(x if self.routs[i] else []) if verbose: print('%g/%g %s -' % (i, len(self.module_list), name), list(x.shape), str) str = '' if self.training: # train return yolo_out elif ONNX_EXPORT: # export x = [torch.cat(x, 0) for x in zip(*yolo_out)] return x[0], torch.cat(x[1:3], 1) # scores, boxes: 3780x80, 3780x4 else: # inference or test x, p = zip(*yolo_out) # inference output, training output x = torch.cat(x, 1) # cat yolo outputs if augment: # de-augment results x = torch.split(x, nb, dim=0) x[1][..., :4] /= s[0] # scale x[1][..., 0] = img_size[1] - x[1][..., 0] # flip lr x[2][..., :4] /= s[1] # scale x = torch.cat(x, 1) return x, p def fuse(self): # Fuse Conv2d + BatchNorm2d layers throughout model print('Fusing layers...') fused_list = nn.ModuleList() for a in list(self.children())[0]: if isinstance(a, nn.Sequential): for i, b in enumerate(a): if isinstance(b, nn.modules.batchnorm.BatchNorm2d): # fuse this bn layer with the previous conv2d layer conv = a[i - 1] fused = torch_utils.fuse_conv_and_bn(conv, b) a = nn.Sequential(fused, *list(a.children())[i + 1:]) break fused_list.append(a) self.module_list = fused_list self.info() if not ONNX_EXPORT else None # yolov3-spp reduced from 225 to 152 layers def info(self, verbose=False): torch_utils.model_info(self, verbose) def get_yolo_layers(model): return [i for i, m in enumerate(model.module_list) if m.__class__.__name__ in ['YOLOLayer', 'JDELayer']] # [89, 101, 113] def load_darknet_weights(self, weights, cutoff=-1): # Parses and loads the weights stored in 'weights' # Establish cutoffs (load layers between 0 and cutoff. if cutoff = -1 all are loaded) file = Path(weights).name if file == 'darknet53.conv.74': cutoff = 75 elif file == 'yolov3-tiny.conv.15': cutoff = 15 # Read weights file with open(weights, 'rb') as f: # Read Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346 self.version = np.fromfile(f, dtype=np.int32, count=3) # (int32) version info: major, minor, revision self.seen = np.fromfile(f, dtype=np.int64, count=1) # (int64) number of images seen during training weights = np.fromfile(f, dtype=np.float32) # the rest are weights ptr = 0 for i, (mdef, module) in enumerate(zip(self.module_defs[:cutoff], self.module_list[:cutoff])): if mdef['type'] == 'convolutional': conv = module[0] if mdef['batch_normalize']: # Load BN bias, weights, running mean and running variance bn = module[1] nb = bn.bias.numel() # number of biases # Bias bn.bias.data.copy_(torch.from_numpy(weights[ptr:ptr + nb]).view_as(bn.bias)) ptr += nb # Weight bn.weight.data.copy_(torch.from_numpy(weights[ptr:ptr + nb]).view_as(bn.weight)) ptr += nb # Running Mean bn.running_mean.data.copy_(torch.from_numpy(weights[ptr:ptr + nb]).view_as(bn.running_mean)) ptr += nb # Running Var bn.running_var.data.copy_(torch.from_numpy(weights[ptr:ptr + nb]).view_as(bn.running_var)) ptr += nb else: # Load conv. bias nb = conv.bias.numel() conv_b = torch.from_numpy(weights[ptr:ptr + nb]).view_as(conv.bias) conv.bias.data.copy_(conv_b) ptr += nb # Load conv. weights nw = conv.weight.numel() # number of weights conv.weight.data.copy_(torch.from_numpy(weights[ptr:ptr + nw]).view_as(conv.weight)) ptr += nw def save_weights(self, path='model.weights', cutoff=-1): # Converts a PyTorch model to Darket format (*.pt to *.weights) # Note: Does not work if model.fuse() is applied with open(path, 'wb') as f: # Write Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346 self.version.tofile(f) # (int32) version info: major, minor, revision self.seen.tofile(f) # (int64) number of images seen during training # Iterate through layers for i, (mdef, module) in enumerate(zip(self.module_defs[:cutoff], self.module_list[:cutoff])): if mdef['type'] == 'convolutional': conv_layer = module[0] # If batch norm, load bn first if mdef['batch_normalize']: bn_layer = module[1] bn_layer.bias.data.cpu().numpy().tofile(f) bn_layer.weight.data.cpu().numpy().tofile(f) bn_layer.running_mean.data.cpu().numpy().tofile(f) bn_layer.running_var.data.cpu().numpy().tofile(f) # Load conv bias else: conv_layer.bias.data.cpu().numpy().tofile(f) # Load conv weights conv_layer.weight.data.cpu().numpy().tofile(f) def convert(cfg='cfg/yolov3-spp.cfg', weights='weights/yolov3-spp.weights', saveto='converted.weights'): # Converts between PyTorch and Darknet format per extension (i.e. *.weights convert to *.pt and vice versa) # from models import *; convert('cfg/yolov3-spp.cfg', 'weights/yolov3-spp.weights') # Initialize model model = Darknet(cfg) ckpt = torch.load(weights) # load checkpoint try: ckpt['model'] = {k: v for k, v in ckpt['model'].items() if model.state_dict()[k].numel() == v.numel()} model.load_state_dict(ckpt['model'], strict=False) save_weights(model, path=saveto, cutoff=-1) except KeyError as e: print(e) def attempt_download(weights): # Attempt to download pretrained weights if not found locally weights = weights.strip() msg = weights + ' missing, try downloading from https://drive.google.com/open?id=1LezFG5g3BCW6iYaV89B2i64cqEUZD7e0' if len(weights) > 0 and not os.path.isfile(weights): d = {''} file = Path(weights).name if file in d: r = gdrive_download(id=d[file], name=weights) else: # download from pjreddie.com url = 'https://pjreddie.com/media/files/' + file print('Downloading ' + url) r = os.system('curl -f ' + url + ' -o ' + weights) # Error check if not (r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6): # weights exist and > 1MB os.system('rm ' + weights) # remove partial downloads raise Exception(msg) ================================================ FILE: asone/detectors/yolor/utils/__init__.py ================================================ ================================================ FILE: asone/detectors/yolor/utils/activations.py ================================================ # Activation functions import torch import torch.nn as nn import torch.nn.functional as F # Swish https://arxiv.org/pdf/1905.02244.pdf --------------------------------------------------------------------------- class Swish(nn.Module): # @staticmethod def forward(x): return x * torch.sigmoid(x) class Hardswish(nn.Module): # export-friendly version of nn.Hardswish() @staticmethod def forward(x): # return x * F.hardsigmoid(x) # for torchscript and CoreML return x * F.hardtanh(x + 3, 0., 6.) / 6. # for torchscript, CoreML and ONNX class MemoryEfficientSwish(nn.Module): class F(torch.autograd.Function): @staticmethod def forward(ctx, x): ctx.save_for_backward(x) return x * torch.sigmoid(x) @staticmethod def backward(ctx, grad_output): x = ctx.saved_tensors[0] sx = torch.sigmoid(x) return grad_output * (sx * (1 + x * (1 - sx))) def forward(self, x): return self.F.apply(x) # Mish https://github.com/digantamisra98/Mish -------------------------------------------------------------------------- class Mish(nn.Module): @staticmethod def forward(x): return x * F.softplus(x).tanh() class MemoryEfficientMish(nn.Module): class F(torch.autograd.Function): @staticmethod def forward(ctx, x): ctx.save_for_backward(x) return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x))) @staticmethod def backward(ctx, grad_output): x = ctx.saved_tensors[0] sx = torch.sigmoid(x) fx = F.softplus(x).tanh() return grad_output * (fx + x * sx * (1 - fx * fx)) def forward(self, x): return self.F.apply(x) # FReLU https://arxiv.org/abs/2007.11824 ------------------------------------------------------------------------------- class FReLU(nn.Module): def __init__(self, c1, k=3): # ch_in, kernel super().__init__() self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1) self.bn = nn.BatchNorm2d(c1) def forward(self, x): return torch.max(x, self.bn(self.conv(x))) ================================================ FILE: asone/detectors/yolor/utils/autoanchor.py ================================================ # Auto-anchor utils import numpy as np import torch import yaml from scipy.cluster.vq import kmeans from tqdm import tqdm def check_anchor_order(m): # Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary a = m.anchor_grid.prod(-1).view(-1) # anchor area da = a[-1] - a[0] # delta a ds = m.stride[-1] - m.stride[0] # delta s if da.sign() != ds.sign(): # same order print('Reversing anchor order') m.anchors[:] = m.anchors.flip(0) m.anchor_grid[:] = m.anchor_grid.flip(0) def check_anchors(dataset, model, thr=4.0, imgsz=640): # Check anchor fit to data, recompute if necessary print('\nAnalyzing anchors... ', end='') m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1] # Detect() shapes = imgsz * dataset.shapes / dataset.shapes.max(1, keepdims=True) scale = np.random.uniform(0.9, 1.1, size=(shapes.shape[0], 1)) # augment scale wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes * scale, dataset.labels)])).float() # wh def metric(k): # compute metric r = wh[:, None] / k[None] x = torch.min(r, 1. / r).min(2)[0] # ratio metric best = x.max(1)[0] # best_x aat = (x > 1. / thr).float().sum(1).mean() # anchors above threshold bpr = (best > 1. / thr).float().mean() # best possible recall return bpr, aat bpr, aat = metric(m.anchor_grid.clone().cpu().view(-1, 2)) print('anchors/target = %.2f, Best Possible Recall (BPR) = %.4f' % (aat, bpr), end='') if bpr < 0.98: # threshold to recompute print('. Attempting to improve anchors, please wait...') na = m.anchor_grid.numel() // 2 # number of anchors new_anchors = kmean_anchors(dataset, n=na, img_size=imgsz, thr=thr, gen=1000, verbose=False) new_bpr = metric(new_anchors.reshape(-1, 2))[0] if new_bpr > bpr: # replace anchors new_anchors = torch.tensor(new_anchors, device=m.anchors.device).type_as(m.anchors) m.anchor_grid[:] = new_anchors.clone().view_as(m.anchor_grid) # for inference m.anchors[:] = new_anchors.clone().view_as(m.anchors) / m.stride.to(m.anchors.device).view(-1, 1, 1) # loss check_anchor_order(m) print('New anchors saved to model. Update model *.yaml to use these anchors in the future.') else: print('Original anchors better than new anchors. Proceeding with original anchors.') print('') # newline def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen=1000, verbose=True): """ Creates kmeans-evolved anchors from training dataset Arguments: path: path to dataset *.yaml, or a loaded dataset n: number of anchors img_size: image size used for training thr: anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0 gen: generations to evolve anchors using genetic algorithm verbose: print all results Return: k: kmeans evolved anchors Usage: from utils.general import *; _ = kmean_anchors() """ thr = 1. / thr def metric(k, wh): # compute metrics r = wh[:, None] / k[None] x = torch.min(r, 1. / r).min(2)[0] # ratio metric # x = wh_iou(wh, torch.tensor(k)) # iou metric return x, x.max(1)[0] # x, best_x def anchor_fitness(k): # mutation fitness _, best = metric(torch.tensor(k, dtype=torch.float32), wh) return (best * (best > thr).float()).mean() # fitness def print_results(k): k = k[np.argsort(k.prod(1))] # sort small to large x, best = metric(k, wh0) bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n # best possible recall, anch > thr print('thr=%.2f: %.4f best possible recall, %.2f anchors past thr' % (thr, bpr, aat)) print('n=%g, img_size=%s, metric_all=%.3f/%.3f-mean/best, past_thr=%.3f-mean: ' % (n, img_size, x.mean(), best.mean(), x[x > thr].mean()), end='') for i, x in enumerate(k): print('%i,%i' % (round(x[0]), round(x[1])), end=', ' if i < len(k) - 1 else '\n') # use in *.cfg return k if isinstance(path, str): # *.yaml file with open(path) as f: data_dict = yaml.load(f, Loader=yaml.FullLoader) # model dict from asone.detectors.yolor.utils.datasets import LoadImagesAndLabels dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True) else: dataset = path # dataset # Get label wh shapes = img_size * dataset.shapes / dataset.shapes.max(1, keepdims=True) wh0 = np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)]) # wh # Filter i = (wh0 < 3.0).any(1).sum() if i: print('WARNING: Extremely small objects found. ' '%g of %g labels are < 3 pixels in width or height.' % (i, len(wh0))) wh = wh0[(wh0 >= 2.0).any(1)] # filter > 2 pixels # Kmeans calculation print('Running kmeans for %g anchors on %g points...' % (n, len(wh))) s = wh.std(0) # sigmas for whitening k, dist = kmeans(wh / s, n, iter=30) # points, mean distance k *= s wh = torch.tensor(wh, dtype=torch.float32) # filtered wh0 = torch.tensor(wh0, dtype=torch.float32) # unfiltered k = print_results(k) # Plot # k, d = [None] * 20, [None] * 20 # for i in tqdm(range(1, 21)): # k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance # fig, ax = plt.subplots(1, 2, figsize=(14, 7)) # ax = ax.ravel() # ax[0].plot(np.arange(1, 21), np.array(d) ** 2, marker='.') # fig, ax = plt.subplots(1, 2, figsize=(14, 7)) # plot wh # ax[0].hist(wh[wh[:, 0]<100, 0],400) # ax[1].hist(wh[wh[:, 1]<100, 1],400) # fig.tight_layout() # fig.savefig('wh.png', dpi=200) # Evolve npr = np.random f, sh, mp, s = anchor_fitness(k), k.shape, 0.9, 0.1 # fitness, generations, mutation prob, sigma pbar = tqdm(range(gen), desc='Evolving anchors with Genetic Algorithm') # progress bar for _ in pbar: v = np.ones(sh) while (v == 1).all(): # mutate until a change occurs (prevent duplicates) v = ((npr.random(sh) < mp) * npr.random() * npr.randn(*sh) * s + 1).clip(0.3, 3.0) kg = (k.copy() * v).clip(min=2.0) fg = anchor_fitness(kg) if fg > f: f, k = fg, kg.copy() pbar.desc = 'Evolving anchors with Genetic Algorithm: fitness = %.4f' % f if verbose: print_results(k) return print_results(k) ================================================ FILE: asone/detectors/yolor/utils/datasets.py ================================================ # Dataset utils and dataloaders import glob import math import os import random import shutil import time from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from threading import Thread import cv2 import numpy as np import torch from PIL import Image, ExifTags from torch.utils.data import Dataset from tqdm import tqdm import pickle from copy import deepcopy from pycocotools import mask as maskUtils from torchvision.utils import save_image from asone.detectors.yolor.utils.general import xyxy2xywh, xywh2xyxy from asone.detectors.yolor.utils.torch_utils import torch_distributed_zero_first # Parameters help_url = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data' img_formats = ['bmp', 'jpg', 'jpeg', 'png', 'tif', 'tiff', 'dng'] # acceptable image suffixes vid_formats = ['mov', 'avi', 'mp4', 'mpg', 'mpeg', 'm4v', 'wmv', 'mkv'] # acceptable video suffixes # Get orientation exif tag for orientation in ExifTags.TAGS.keys(): if ExifTags.TAGS[orientation] == 'Orientation': break def get_hash(files): # Returns a single hash value of a list of files return sum(os.path.getsize(f) for f in files if os.path.isfile(f)) def exif_size(img): # Returns exif-corrected PIL size s = img.size # (width, height) try: rotation = dict(img._getexif().items())[orientation] if rotation == 6: # rotation 270 s = (s[1], s[0]) elif rotation == 8: # rotation 90 s = (s[1], s[0]) except: pass return s def create_dataloader(path, imgsz, batch_size, stride, opt, hyp=None, augment=False, cache=False, pad=0.0, rect=False, rank=-1, world_size=1, workers=8): # Make sure only the first process in DDP process the dataset first, and the following others can use the cache with torch_distributed_zero_first(rank): dataset = LoadImagesAndLabels(path, imgsz, batch_size, augment=augment, # augment images hyp=hyp, # augmentation hyperparameters rect=rect, # rectangular training cache_images=cache, single_cls=opt.single_cls, stride=int(stride), pad=pad, rank=rank) batch_size = min(batch_size, len(dataset)) nw = min([os.cpu_count() // world_size, batch_size if batch_size > 1 else 0, workers]) # number of workers sampler = torch.utils.data.distributed.DistributedSampler(dataset) if rank != -1 else None dataloader = InfiniteDataLoader(dataset, batch_size=batch_size, num_workers=nw, sampler=sampler, pin_memory=True, collate_fn=LoadImagesAndLabels.collate_fn) # torch.utils.data.DataLoader() return dataloader, dataset def create_dataloader9(path, imgsz, batch_size, stride, opt, hyp=None, augment=False, cache=False, pad=0.0, rect=False, rank=-1, world_size=1, workers=8): # Make sure only the first process in DDP process the dataset first, and the following others can use the cache with torch_distributed_zero_first(rank): dataset = LoadImagesAndLabels9(path, imgsz, batch_size, augment=augment, # augment images hyp=hyp, # augmentation hyperparameters rect=rect, # rectangular training cache_images=cache, single_cls=opt.single_cls, stride=int(stride), pad=pad, rank=rank) batch_size = min(batch_size, len(dataset)) nw = min([os.cpu_count() // world_size, batch_size if batch_size > 1 else 0, workers]) # number of workers sampler = torch.utils.data.distributed.DistributedSampler(dataset) if rank != -1 else None dataloader = InfiniteDataLoader(dataset, batch_size=batch_size, num_workers=nw, sampler=sampler, pin_memory=True, collate_fn=LoadImagesAndLabels9.collate_fn) # torch.utils.data.DataLoader() return dataloader, dataset class InfiniteDataLoader(torch.utils.data.dataloader.DataLoader): """ Dataloader that reuses workers Uses same syntax as vanilla DataLoader """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) object.__setattr__(self, 'batch_sampler', _RepeatSampler(self.batch_sampler)) self.iterator = super().__iter__() def __len__(self): return len(self.batch_sampler.sampler) def __iter__(self): for i in range(len(self)): yield next(self.iterator) class _RepeatSampler(object): """ Sampler that repeats forever Args: sampler (Sampler) """ def __init__(self, sampler): self.sampler = sampler def __iter__(self): while True: yield from iter(self.sampler) class LoadImages: # for inference def __init__(self, path, img_size=640, auto_size=32): p = str(Path(path)) # os-agnostic p = os.path.abspath(p) # absolute path if '*' in p: files = sorted(glob.glob(p, recursive=True)) # glob elif os.path.isdir(p): files = sorted(glob.glob(os.path.join(p, '*.*'))) # dir elif os.path.isfile(p): files = [p] # files else: raise Exception('ERROR: %s does not exist' % p) images = [x for x in files if x.split('.')[-1].lower() in img_formats] videos = [x for x in files if x.split('.')[-1].lower() in vid_formats] ni, nv = len(images), len(videos) self.img_size = img_size self.auto_size = auto_size self.files = images + videos self.nf = ni + nv # number of files self.video_flag = [False] * ni + [True] * nv self.mode = 'images' if any(videos): self.new_video(videos[0]) # new video else: self.cap = None assert self.nf > 0, 'No images or videos found in %s. Supported formats are:\nimages: %s\nvideos: %s' % \ (p, img_formats, vid_formats) def __iter__(self): self.count = 0 return self def __next__(self): if self.count == self.nf: raise StopIteration path = self.files[self.count] if self.video_flag[self.count]: # Read video self.mode = 'video' ret_val, img0 = self.cap.read() if not ret_val: self.count += 1 self.cap.release() if self.count == self.nf: # last video raise StopIteration else: path = self.files[self.count] self.new_video(path) ret_val, img0 = self.cap.read() self.frame += 1 print('video %g/%g (%g/%g) %s: ' % (self.count + 1, self.nf, self.frame, self.nframes, path), end='') else: # Read image self.count += 1 img0 = cv2.imread(path) # BGR assert img0 is not None, 'Image Not Found ' + path print('image %g/%g %s: ' % (self.count, self.nf, path), end='') # Padded resize img = letterbox(img0, new_shape=self.img_size, auto_size=self.auto_size)[0] # Convert img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 img = np.ascontiguousarray(img) return path, img, img0, self.cap def new_video(self, path): self.frame = 0 self.cap = cv2.VideoCapture(path) self.nframes = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT)) def __len__(self): return self.nf # number of files class LoadWebcam: # for inference def __init__(self, pipe='0', img_size=640): self.img_size = img_size if pipe.isnumeric(): pipe = eval(pipe) # local camera # pipe = 'rtsp://192.168.1.64/1' # IP camera # pipe = 'rtsp://username:password@192.168.1.64/1' # IP camera with login # pipe = 'http://wmccpinetop.axiscam.net/mjpg/video.mjpg' # IP golf camera self.pipe = pipe self.cap = cv2.VideoCapture(pipe) # video capture object self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # set buffer size def __iter__(self): self.count = -1 return self def __next__(self): self.count += 1 if cv2.waitKey(1) == ord('q'): # q to quit self.cap.release() cv2.destroyAllWindows() raise StopIteration # Read frame if self.pipe == 0: # local camera ret_val, img0 = self.cap.read() img0 = cv2.flip(img0, 1) # flip left-right else: # IP camera n = 0 while True: n += 1 self.cap.grab() if n % 30 == 0: # skip frames ret_val, img0 = self.cap.retrieve() if ret_val: break # Print assert ret_val, 'Camera Error %s' % self.pipe img_path = 'webcam.jpg' print('webcam %g: ' % self.count, end='') # Padded resize img = letterbox(img0, new_shape=self.img_size)[0] # Convert img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 img = np.ascontiguousarray(img) return img_path, img, img0, None def __len__(self): return 0 class LoadStreams: # multiple IP or RTSP cameras def __init__(self, sources='streams.txt', img_size=640): self.mode = 'images' self.img_size = img_size if os.path.isfile(sources): with open(sources, 'r') as f: sources = [x.strip() for x in f.read().splitlines() if len(x.strip())] else: sources = [sources] n = len(sources) self.imgs = [None] * n self.sources = sources for i, s in enumerate(sources): # Start the thread to read frames from the video stream print('%g/%g: %s... ' % (i + 1, n, s), end='') cap = cv2.VideoCapture(eval(s) if s.isnumeric() else s) assert cap.isOpened(), 'Failed to open %s' % s w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = cap.get(cv2.CAP_PROP_FPS) % 100 _, self.imgs[i] = cap.read() # guarantee first frame thread = Thread(target=self.update, args=([i, cap]), daemon=True) print(' success (%gx%g at %.2f FPS).' % (w, h, fps)) thread.start() print('') # newline # check for common shapes s = np.stack([letterbox(x, new_shape=self.img_size)[0].shape for x in self.imgs], 0) # inference shapes self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal if not self.rect: print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.') def update(self, index, cap): # Read next stream frame in a daemon thread n = 0 while cap.isOpened(): n += 1 # _, self.imgs[index] = cap.read() cap.grab() if n == 4: # read every 4th frame _, self.imgs[index] = cap.retrieve() n = 0 time.sleep(0.01) # wait time def __iter__(self): self.count = -1 return self def __next__(self): self.count += 1 img0 = self.imgs.copy() if cv2.waitKey(1) == ord('q'): # q to quit cv2.destroyAllWindows() raise StopIteration # Letterbox img = [letterbox(x, new_shape=self.img_size, auto=self.rect)[0] for x in img0] # Stack img = np.stack(img, 0) # Convert img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416 img = np.ascontiguousarray(img) return self.sources, img, img0, None def __len__(self): return 0 # 1E12 frames = 32 streams at 30 FPS for 30 years class LoadImagesAndLabels(Dataset): # for training/testing def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, cache_images=False, single_cls=False, stride=32, pad=0.0, rank=-1): self.img_size = img_size self.augment = augment self.hyp = hyp self.image_weights = image_weights self.rect = False if image_weights else rect self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training) self.mosaic_border = [-img_size // 2, -img_size // 2] self.stride = stride def img2label_paths(img_paths): # Define label paths as a function of image paths sa, sb = os.sep + 'images' + os.sep, os.sep + 'labels' + os.sep # /images/, /labels/ substrings return [x.replace(sa, sb, 1).replace(x.split('.')[-1], 'txt') for x in img_paths] try: f = [] # image files for p in path if isinstance(path, list) else [path]: p = Path(p) # os-agnostic if p.is_dir(): # dir f += glob.glob(str(p / '**' / '*.*'), recursive=True) elif p.is_file(): # file with open(p, 'r') as t: t = t.read().splitlines() parent = str(p.parent) + os.sep f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path else: raise Exception('%s does not exist' % p) self.img_files = sorted([x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in img_formats]) assert self.img_files, 'No images found' except Exception as e: raise Exception('Error loading data from %s: %s\nSee %s' % (path, e, help_url)) # Check cache self.label_files = img2label_paths(self.img_files) # labels cache_path = str(Path(self.label_files[0]).parent) + '.cache3' # cached labels if os.path.isfile(cache_path): cache = torch.load(cache_path) # load if cache['hash'] != get_hash(self.label_files + self.img_files): # dataset changed cache = self.cache_labels(cache_path) # re-cache else: cache = self.cache_labels(cache_path) # cache # Read cache cache.pop('hash') # remove hash labels, shapes = zip(*cache.values()) self.labels = list(labels) self.shapes = np.array(shapes, dtype=np.float64) self.img_files = list(cache.keys()) # update self.label_files = img2label_paths(cache.keys()) # update n = len(shapes) # number of images bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index nb = bi[-1] + 1 # number of batches self.batch = bi # batch index of image self.n = n # Rectangular Training if self.rect: # Sort by aspect ratio s = self.shapes # wh ar = s[:, 1] / s[:, 0] # aspect ratio irect = ar.argsort() self.img_files = [self.img_files[i] for i in irect] self.label_files = [self.label_files[i] for i in irect] self.labels = [self.labels[i] for i in irect] self.shapes = s[irect] # wh ar = ar[irect] # Set training image shapes shapes = [[1, 1]] * nb for i in range(nb): ari = ar[bi == i] mini, maxi = ari.min(), ari.max() if maxi < 1: shapes[i] = [maxi, 1] elif mini > 1: shapes[i] = [1, 1 / mini] self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(np.int) * stride # Check labels create_datasubset, extract_bounding_boxes, labels_loaded = False, False, False nm, nf, ne, ns, nd = 0, 0, 0, 0, 0 # number missing, found, empty, datasubset, duplicate pbar = enumerate(self.label_files) if rank in [-1, 0]: pbar = tqdm(pbar) for i, file in pbar: l = self.labels[i] # label if l is not None and l.shape[0]: assert l.shape[1] == 5, '> 5 label columns: %s' % file assert (l >= 0).all(), 'negative labels: %s' % file assert (l[:, 1:] <= 1).all(), 'non-normalized or out of bounds coordinate labels: %s' % file if np.unique(l, axis=0).shape[0] < l.shape[0]: # duplicate rows nd += 1 # print('WARNING: duplicate rows in %s' % self.label_files[i]) # duplicate rows if single_cls: l[:, 0] = 0 # force dataset into single-class mode self.labels[i] = l nf += 1 # file found # Create subdataset (a smaller dataset) if create_datasubset and ns < 1E4: if ns == 0: create_folder(path='./datasubset') os.makedirs('./datasubset/images') exclude_classes = 43 if exclude_classes not in l[:, 0]: ns += 1 # shutil.copy(src=self.img_files[i], dst='./datasubset/images/') # copy image with open('./datasubset/images.txt', 'a') as f: f.write(self.img_files[i] + '\n') # Extract object detection boxes for a second stage classifier if extract_bounding_boxes: p = Path(self.img_files[i]) img = cv2.imread(str(p)) h, w = img.shape[:2] for j, x in enumerate(l): f = '%s%sclassifier%s%g_%g_%s' % (p.parent.parent, os.sep, os.sep, x[0], j, p.name) if not os.path.exists(Path(f).parent): os.makedirs(Path(f).parent) # make new output folder b = x[1:] * [w, h, w, h] # box b[2:] = b[2:].max() # rectangle to square b[2:] = b[2:] * 1.3 + 30 # pad b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(np.int) b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image b[[1, 3]] = np.clip(b[[1, 3]], 0, h) assert cv2.imwrite(f, img[b[1]:b[3], b[0]:b[2]]), 'Failure extracting classifier boxes' else: ne += 1 # print('empty labels for image %s' % self.img_files[i]) # file empty # os.system("rm '%s' '%s'" % (self.img_files[i], self.label_files[i])) # remove if rank in [-1, 0]: pbar.desc = 'Scanning labels %s (%g found, %g missing, %g empty, %g duplicate, for %g images)' % ( cache_path, nf, nm, ne, nd, n) if nf == 0: s = 'WARNING: No labels found in %s. See %s' % (os.path.dirname(file) + os.sep, help_url) print(s) assert not augment, '%s. Can not train without labels.' % s # Cache images into memory for faster training (WARNING: large datasets may exceed system RAM) self.imgs = [None] * n if cache_images: gb = 0 # Gigabytes of cached images self.img_hw0, self.img_hw = [None] * n, [None] * n results = ThreadPool(8).imap(lambda x: load_image(*x), zip(repeat(self), range(n))) # 8 threads pbar = tqdm(enumerate(results), total=n) for i, x in pbar: self.imgs[i], self.img_hw0[i], self.img_hw[i] = x # img, hw_original, hw_resized = load_image(self, i) gb += self.imgs[i].nbytes pbar.desc = 'Caching images (%.1fGB)' % (gb / 1E9) def cache_labels(self, path='labels.cache3'): # Cache dataset labels, check images and read shapes x = {} # dict pbar = tqdm(zip(self.img_files, self.label_files), desc='Scanning images', total=len(self.img_files)) for (img, label) in pbar: try: l = [] im = Image.open(img) im.verify() # PIL verify shape = exif_size(im) # image size assert (shape[0] > 9) & (shape[1] > 9), 'image size <10 pixels' if os.path.isfile(label): with open(label, 'r') as f: l = np.array([x.split() for x in f.read().splitlines()], dtype=np.float32) # labels if len(l) == 0: l = np.zeros((0, 5), dtype=np.float32) x[img] = [l, shape] except Exception as e: print('WARNING: Ignoring corrupted image and/or label %s: %s' % (img, e)) x['hash'] = get_hash(self.label_files + self.img_files) torch.save(x, path) # save for next time return x def __len__(self): return len(self.img_files) # def __iter__(self): # self.count = -1 # print('ran dataset iter') # #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF) # return self def __getitem__(self, index): if self.image_weights: index = self.indices[index] hyp = self.hyp mosaic = self.mosaic and random.random() < hyp['mosaic'] if mosaic: # Load mosaic img, labels = load_mosaic(self, index) #img, labels = load_mosaic9(self, index) shapes = None # MixUp https://arxiv.org/pdf/1710.09412.pdf if random.random() < hyp['mixup']: img2, labels2 = load_mosaic(self, random.randint(0, len(self.labels) - 1)) #img2, labels2 = load_mosaic9(self, random.randint(0, len(self.labels) - 1)) r = np.random.beta(8.0, 8.0) # mixup ratio, alpha=beta=8.0 img = (img * r + img2 * (1 - r)).astype(np.uint8) labels = np.concatenate((labels, labels2), 0) else: # Load image img, (h0, w0), (h, w) = load_image(self, index) # Letterbox shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment) shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling # Load labels labels = [] x = self.labels[index] if x.size > 0: # Normalized xywh to pixel xyxy format labels = x.copy() labels[:, 1] = ratio[0] * w * (x[:, 1] - x[:, 3] / 2) + pad[0] # pad width labels[:, 2] = ratio[1] * h * (x[:, 2] - x[:, 4] / 2) + pad[1] # pad height labels[:, 3] = ratio[0] * w * (x[:, 1] + x[:, 3] / 2) + pad[0] labels[:, 4] = ratio[1] * h * (x[:, 2] + x[:, 4] / 2) + pad[1] if self.augment: # Augment imagespace if not mosaic: img, labels = random_perspective(img, labels, degrees=hyp['degrees'], translate=hyp['translate'], scale=hyp['scale'], shear=hyp['shear'], perspective=hyp['perspective']) # Augment colorspace augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v']) # Apply cutouts # if random.random() < 0.9: # labels = cutout(img, labels) nL = len(labels) # number of labels if nL: labels[:, 1:5] = xyxy2xywh(labels[:, 1:5]) # convert xyxy to xywh labels[:, [2, 4]] /= img.shape[0] # normalized height 0-1 labels[:, [1, 3]] /= img.shape[1] # normalized width 0-1 if self.augment: # flip up-down if random.random() < hyp['flipud']: img = np.flipud(img) if nL: labels[:, 2] = 1 - labels[:, 2] # flip left-right if random.random() < hyp['fliplr']: img = np.fliplr(img) if nL: labels[:, 1] = 1 - labels[:, 1] labels_out = torch.zeros((nL, 6)) if nL: labels_out[:, 1:] = torch.from_numpy(labels) # Convert img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 img = np.ascontiguousarray(img) return torch.from_numpy(img), labels_out, self.img_files[index], shapes @staticmethod def collate_fn(batch): img, label, path, shapes = zip(*batch) # transposed for i, l in enumerate(label): l[:, 0] = i # add target image index for build_targets() return torch.stack(img, 0), torch.cat(label, 0), path, shapes class LoadImagesAndLabels9(Dataset): # for training/testing def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, cache_images=False, single_cls=False, stride=32, pad=0.0, rank=-1): self.img_size = img_size self.augment = augment self.hyp = hyp self.image_weights = image_weights self.rect = False if image_weights else rect self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training) self.mosaic_border = [-img_size // 2, -img_size // 2] self.stride = stride def img2label_paths(img_paths): # Define label paths as a function of image paths sa, sb = os.sep + 'images' + os.sep, os.sep + 'labels' + os.sep # /images/, /labels/ substrings return [x.replace(sa, sb, 1).replace(x.split('.')[-1], 'txt') for x in img_paths] try: f = [] # image files for p in path if isinstance(path, list) else [path]: p = Path(p) # os-agnostic if p.is_dir(): # dir f += glob.glob(str(p / '**' / '*.*'), recursive=True) elif p.is_file(): # file with open(p, 'r') as t: t = t.read().splitlines() parent = str(p.parent) + os.sep f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path else: raise Exception('%s does not exist' % p) self.img_files = sorted([x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in img_formats]) assert self.img_files, 'No images found' except Exception as e: raise Exception('Error loading data from %s: %s\nSee %s' % (path, e, help_url)) # Check cache self.label_files = img2label_paths(self.img_files) # labels cache_path = str(Path(self.label_files[0]).parent) + '.cache3' # cached labels if os.path.isfile(cache_path): cache = torch.load(cache_path) # load if cache['hash'] != get_hash(self.label_files + self.img_files): # dataset changed cache = self.cache_labels(cache_path) # re-cache else: cache = self.cache_labels(cache_path) # cache # Read cache cache.pop('hash') # remove hash labels, shapes = zip(*cache.values()) self.labels = list(labels) self.shapes = np.array(shapes, dtype=np.float64) self.img_files = list(cache.keys()) # update self.label_files = img2label_paths(cache.keys()) # update n = len(shapes) # number of images bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index nb = bi[-1] + 1 # number of batches self.batch = bi # batch index of image self.n = n # Rectangular Training if self.rect: # Sort by aspect ratio s = self.shapes # wh ar = s[:, 1] / s[:, 0] # aspect ratio irect = ar.argsort() self.img_files = [self.img_files[i] for i in irect] self.label_files = [self.label_files[i] for i in irect] self.labels = [self.labels[i] for i in irect] self.shapes = s[irect] # wh ar = ar[irect] # Set training image shapes shapes = [[1, 1]] * nb for i in range(nb): ari = ar[bi == i] mini, maxi = ari.min(), ari.max() if maxi < 1: shapes[i] = [maxi, 1] elif mini > 1: shapes[i] = [1, 1 / mini] self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(np.int) * stride # Check labels create_datasubset, extract_bounding_boxes, labels_loaded = False, False, False nm, nf, ne, ns, nd = 0, 0, 0, 0, 0 # number missing, found, empty, datasubset, duplicate pbar = enumerate(self.label_files) if rank in [-1, 0]: pbar = tqdm(pbar) for i, file in pbar: l = self.labels[i] # label if l is not None and l.shape[0]: assert l.shape[1] == 5, '> 5 label columns: %s' % file assert (l >= 0).all(), 'negative labels: %s' % file assert (l[:, 1:] <= 1).all(), 'non-normalized or out of bounds coordinate labels: %s' % file if np.unique(l, axis=0).shape[0] < l.shape[0]: # duplicate rows nd += 1 # print('WARNING: duplicate rows in %s' % self.label_files[i]) # duplicate rows if single_cls: l[:, 0] = 0 # force dataset into single-class mode self.labels[i] = l nf += 1 # file found # Create subdataset (a smaller dataset) if create_datasubset and ns < 1E4: if ns == 0: create_folder(path='./datasubset') os.makedirs('./datasubset/images') exclude_classes = 43 if exclude_classes not in l[:, 0]: ns += 1 # shutil.copy(src=self.img_files[i], dst='./datasubset/images/') # copy image with open('./datasubset/images.txt', 'a') as f: f.write(self.img_files[i] + '\n') # Extract object detection boxes for a second stage classifier if extract_bounding_boxes: p = Path(self.img_files[i]) img = cv2.imread(str(p)) h, w = img.shape[:2] for j, x in enumerate(l): f = '%s%sclassifier%s%g_%g_%s' % (p.parent.parent, os.sep, os.sep, x[0], j, p.name) if not os.path.exists(Path(f).parent): os.makedirs(Path(f).parent) # make new output folder b = x[1:] * [w, h, w, h] # box b[2:] = b[2:].max() # rectangle to square b[2:] = b[2:] * 1.3 + 30 # pad b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(np.int) b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image b[[1, 3]] = np.clip(b[[1, 3]], 0, h) assert cv2.imwrite(f, img[b[1]:b[3], b[0]:b[2]]), 'Failure extracting classifier boxes' else: ne += 1 # print('empty labels for image %s' % self.img_files[i]) # file empty # os.system("rm '%s' '%s'" % (self.img_files[i], self.label_files[i])) # remove if rank in [-1, 0]: pbar.desc = 'Scanning labels %s (%g found, %g missing, %g empty, %g duplicate, for %g images)' % ( cache_path, nf, nm, ne, nd, n) if nf == 0: s = 'WARNING: No labels found in %s. See %s' % (os.path.dirname(file) + os.sep, help_url) print(s) assert not augment, '%s. Can not train without labels.' % s # Cache images into memory for faster training (WARNING: large datasets may exceed system RAM) self.imgs = [None] * n if cache_images: gb = 0 # Gigabytes of cached images self.img_hw0, self.img_hw = [None] * n, [None] * n results = ThreadPool(8).imap(lambda x: load_image(*x), zip(repeat(self), range(n))) # 8 threads pbar = tqdm(enumerate(results), total=n) for i, x in pbar: self.imgs[i], self.img_hw0[i], self.img_hw[i] = x # img, hw_original, hw_resized = load_image(self, i) gb += self.imgs[i].nbytes pbar.desc = 'Caching images (%.1fGB)' % (gb / 1E9) def cache_labels(self, path='labels.cache3'): # Cache dataset labels, check images and read shapes x = {} # dict pbar = tqdm(zip(self.img_files, self.label_files), desc='Scanning images', total=len(self.img_files)) for (img, label) in pbar: try: l = [] im = Image.open(img) im.verify() # PIL verify shape = exif_size(im) # image size assert (shape[0] > 9) & (shape[1] > 9), 'image size <10 pixels' if os.path.isfile(label): with open(label, 'r') as f: l = np.array([x.split() for x in f.read().splitlines()], dtype=np.float32) # labels if len(l) == 0: l = np.zeros((0, 5), dtype=np.float32) x[img] = [l, shape] except Exception as e: print('WARNING: Ignoring corrupted image and/or label %s: %s' % (img, e)) x['hash'] = get_hash(self.label_files + self.img_files) torch.save(x, path) # save for next time return x def __len__(self): return len(self.img_files) # def __iter__(self): # self.count = -1 # print('ran dataset iter') # #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF) # return self def __getitem__(self, index): if self.image_weights: index = self.indices[index] hyp = self.hyp mosaic = self.mosaic and random.random() < hyp['mosaic'] if mosaic: # Load mosaic #img, labels = load_mosaic(self, index) img, labels = load_mosaic9(self, index) shapes = None # MixUp https://arxiv.org/pdf/1710.09412.pdf if random.random() < hyp['mixup']: #img2, labels2 = load_mosaic(self, random.randint(0, len(self.labels) - 1)) img2, labels2 = load_mosaic9(self, random.randint(0, len(self.labels) - 1)) r = np.random.beta(8.0, 8.0) # mixup ratio, alpha=beta=8.0 img = (img * r + img2 * (1 - r)).astype(np.uint8) labels = np.concatenate((labels, labels2), 0) else: # Load image img, (h0, w0), (h, w) = load_image(self, index) # Letterbox shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment) shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling # Load labels labels = [] x = self.labels[index] if x.size > 0: # Normalized xywh to pixel xyxy format labels = x.copy() labels[:, 1] = ratio[0] * w * (x[:, 1] - x[:, 3] / 2) + pad[0] # pad width labels[:, 2] = ratio[1] * h * (x[:, 2] - x[:, 4] / 2) + pad[1] # pad height labels[:, 3] = ratio[0] * w * (x[:, 1] + x[:, 3] / 2) + pad[0] labels[:, 4] = ratio[1] * h * (x[:, 2] + x[:, 4] / 2) + pad[1] if self.augment: # Augment imagespace if not mosaic: img, labels = random_perspective(img, labels, degrees=hyp['degrees'], translate=hyp['translate'], scale=hyp['scale'], shear=hyp['shear'], perspective=hyp['perspective']) # Augment colorspace augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v']) # Apply cutouts # if random.random() < 0.9: # labels = cutout(img, labels) nL = len(labels) # number of labels if nL: labels[:, 1:5] = xyxy2xywh(labels[:, 1:5]) # convert xyxy to xywh labels[:, [2, 4]] /= img.shape[0] # normalized height 0-1 labels[:, [1, 3]] /= img.shape[1] # normalized width 0-1 if self.augment: # flip up-down if random.random() < hyp['flipud']: img = np.flipud(img) if nL: labels[:, 2] = 1 - labels[:, 2] # flip left-right if random.random() < hyp['fliplr']: img = np.fliplr(img) if nL: labels[:, 1] = 1 - labels[:, 1] labels_out = torch.zeros((nL, 6)) if nL: labels_out[:, 1:] = torch.from_numpy(labels) # Convert img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 img = np.ascontiguousarray(img) return torch.from_numpy(img), labels_out, self.img_files[index], shapes @staticmethod def collate_fn(batch): img, label, path, shapes = zip(*batch) # transposed for i, l in enumerate(label): l[:, 0] = i # add target image index for build_targets() return torch.stack(img, 0), torch.cat(label, 0), path, shapes # Ancillary functions -------------------------------------------------------------------------------------------------- def load_image(self, index): # loads 1 image from dataset, returns img, original hw, resized hw img = self.imgs[index] if img is None: # not cached path = self.img_files[index] img = cv2.imread(path) # BGR assert img is not None, 'Image Not Found ' + path h0, w0 = img.shape[:2] # orig hw r = self.img_size / max(h0, w0) # resize image to img_size if r != 1: # always resize down, only resize up if training with augmentation interp = cv2.INTER_AREA if r < 1 and not self.augment else cv2.INTER_LINEAR img = cv2.resize(img, (int(w0 * r), int(h0 * r)), interpolation=interp) return img, (h0, w0), img.shape[:2] # img, hw_original, hw_resized else: return self.imgs[index], self.img_hw0[index], self.img_hw[index] # img, hw_original, hw_resized def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5): r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) dtype = img.dtype # uint8 x = np.arange(0, 256, dtype=np.int16) lut_hue = ((x * r[0]) % 180).astype(dtype) lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) lut_val = np.clip(x * r[2], 0, 255).astype(dtype) img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype) cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed # Histogram equalization # if random.random() < 0.2: # for i in range(3): # img[:, :, i] = cv2.equalizeHist(img[:, :, i]) def load_mosaic(self, index): # loads images in a mosaic labels4 = [] s = self.img_size yc, xc = [int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border] # mosaic center x, y indices = [index] + [random.randint(0, len(self.labels) - 1) for _ in range(3)] # 3 additional image indices for i, index in enumerate(indices): # Load image img, _, (h, w) = load_image(self, index) # place img in img4 if i == 0: # top left img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image) x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image) elif i == 1: # top right x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h elif i == 2: # bottom left x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h) x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h) elif i == 3: # bottom right x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h) x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h) img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax] padw = x1a - x1b padh = y1a - y1b # Labels x = self.labels[index] labels = x.copy() if x.size > 0: # Normalized xywh to pixel xyxy format labels[:, 1] = w * (x[:, 1] - x[:, 3] / 2) + padw labels[:, 2] = h * (x[:, 2] - x[:, 4] / 2) + padh labels[:, 3] = w * (x[:, 1] + x[:, 3] / 2) + padw labels[:, 4] = h * (x[:, 2] + x[:, 4] / 2) + padh labels4.append(labels) # Concat/clip labels if len(labels4): labels4 = np.concatenate(labels4, 0) np.clip(labels4[:, 1:], 0, 2 * s, out=labels4[:, 1:]) # use with random_perspective # img4, labels4 = replicate(img4, labels4) # replicate # Augment img4, labels4 = random_perspective(img4, labels4, degrees=self.hyp['degrees'], translate=self.hyp['translate'], scale=self.hyp['scale'], shear=self.hyp['shear'], perspective=self.hyp['perspective'], border=self.mosaic_border) # border to remove return img4, labels4 def load_mosaic9(self, index): # loads images in a 9-mosaic labels9 = [] s = self.img_size indices = [index] + [random.randint(0, len(self.labels) - 1) for _ in range(8)] # 8 additional image indices for i, index in enumerate(indices): # Load image img, _, (h, w) = load_image(self, index) # place img in img9 if i == 0: # center img9 = np.full((s * 3, s * 3, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles h0, w0 = h, w c = s, s, s + w, s + h # xmin, ymin, xmax, ymax (base) coordinates elif i == 1: # top c = s, s - h, s + w, s elif i == 2: # top right c = s + wp, s - h, s + wp + w, s elif i == 3: # right c = s + w0, s, s + w0 + w, s + h elif i == 4: # bottom right c = s + w0, s + hp, s + w0 + w, s + hp + h elif i == 5: # bottom c = s + w0 - w, s + h0, s + w0, s + h0 + h elif i == 6: # bottom left c = s + w0 - wp - w, s + h0, s + w0 - wp, s + h0 + h elif i == 7: # left c = s - w, s + h0 - h, s, s + h0 elif i == 8: # top left c = s - w, s + h0 - hp - h, s, s + h0 - hp padx, pady = c[:2] x1, y1, x2, y2 = [max(x, 0) for x in c] # allocate coords # Labels x = self.labels[index] labels = x.copy() if x.size > 0: # Normalized xywh to pixel xyxy format labels[:, 1] = w * (x[:, 1] - x[:, 3] / 2) + padx labels[:, 2] = h * (x[:, 2] - x[:, 4] / 2) + pady labels[:, 3] = w * (x[:, 1] + x[:, 3] / 2) + padx labels[:, 4] = h * (x[:, 2] + x[:, 4] / 2) + pady labels9.append(labels) # Image img9[y1:y2, x1:x2] = img[y1 - pady:, x1 - padx:] # img9[ymin:ymax, xmin:xmax] hp, wp = h, w # height, width previous # Offset yc, xc = [int(random.uniform(0, s)) for x in self.mosaic_border] # mosaic center x, y img9 = img9[yc:yc + 2 * s, xc:xc + 2 * s] # Concat/clip labels if len(labels9): labels9 = np.concatenate(labels9, 0) labels9[:, [1, 3]] -= xc labels9[:, [2, 4]] -= yc np.clip(labels9[:, 1:], 0, 2 * s, out=labels9[:, 1:]) # use with random_perspective # img9, labels9 = replicate(img9, labels9) # replicate # Augment img9, labels9 = random_perspective(img9, labels9, degrees=self.hyp['degrees'], translate=self.hyp['translate'], scale=self.hyp['scale'], shear=self.hyp['shear'], perspective=self.hyp['perspective'], border=self.mosaic_border) # border to remove return img9, labels9 def replicate(img, labels): # Replicate labels h, w = img.shape[:2] boxes = labels[:, 1:].astype(int) x1, y1, x2, y2 = boxes.T s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels) for i in s.argsort()[:round(s.size * 0.5)]: # smallest indices x1b, y1b, x2b, y2b = boxes[i] bh, bw = y2b - y1b, x2b - x1b yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh] img[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax] labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0) return img, labels def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, auto_size=32): # Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232 shape = img.shape[:2] # current shape [height, width] if isinstance(new_shape, int): new_shape = (new_shape, new_shape) # Scale ratio (new / old) r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) if not scaleup: # only scale down, do not scale up (for better test mAP) r = min(r, 1.0) # Compute padding ratio = r, r # width, height ratios new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding if auto: # minimum rectangle dw, dh = np.mod(dw, auto_size), np.mod(dh, auto_size) # wh padding elif scaleFill: # stretch dw, dh = 0.0, 0.0 new_unpad = (new_shape[1], new_shape[0]) ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios dw /= 2 # divide padding into 2 sides dh /= 2 if shape[::-1] != new_unpad: # resize img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border return img, ratio, (dw, dh) def random_perspective(img, targets=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0, border=(0, 0)): # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10)) # targets = [cls, xyxy] height = img.shape[0] + border[0] * 2 # shape(h,w,c) width = img.shape[1] + border[1] * 2 # Center C = np.eye(3) C[0, 2] = -img.shape[1] / 2 # x translation (pixels) C[1, 2] = -img.shape[0] / 2 # y translation (pixels) # Perspective P = np.eye(3) P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y) P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x) # Rotation and Scale R = np.eye(3) a = random.uniform(-degrees, degrees) # a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations s = random.uniform(1 - scale, 1 + scale) # s = 2 ** random.uniform(-scale, scale) R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s) # Shear S = np.eye(3) S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg) S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg) # Translation T = np.eye(3) T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels) T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels) # Combined rotation matrix M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed if perspective: img = cv2.warpPerspective(img, M, dsize=(width, height), borderValue=(114, 114, 114)) else: # affine img = cv2.warpAffine(img, M[:2], dsize=(width, height), borderValue=(114, 114, 114)) # Visualize # import matplotlib.pyplot as plt # ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel() # ax[0].imshow(img[:, :, ::-1]) # base # ax[1].imshow(img2[:, :, ::-1]) # warped # Transform label coordinates n = len(targets) if n: # warp points xy = np.ones((n * 4, 3)) xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1 xy = xy @ M.T # transform if perspective: xy = (xy[:, :2] / xy[:, 2:3]).reshape(n, 8) # rescale else: # affine xy = xy[:, :2].reshape(n, 8) # create new boxes x = xy[:, [0, 2, 4, 6]] y = xy[:, [1, 3, 5, 7]] xy = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T # # apply angle-based reduction of bounding boxes # radians = a * math.pi / 180 # reduction = max(abs(math.sin(radians)), abs(math.cos(radians))) ** 0.5 # x = (xy[:, 2] + xy[:, 0]) / 2 # y = (xy[:, 3] + xy[:, 1]) / 2 # w = (xy[:, 2] - xy[:, 0]) * reduction # h = (xy[:, 3] - xy[:, 1]) * reduction # xy = np.concatenate((x - w / 2, y - h / 2, x + w / 2, y + h / 2)).reshape(4, n).T # clip boxes xy[:, [0, 2]] = xy[:, [0, 2]].clip(0, width) xy[:, [1, 3]] = xy[:, [1, 3]].clip(0, height) # filter candidates i = box_candidates(box1=targets[:, 1:5].T * s, box2=xy.T) targets = targets[i] targets[:, 1:5] = xy[i] return img, targets def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1): # box1(4,n), box2(4,n) # Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio w1, h1 = box1[2] - box1[0], box1[3] - box1[1] w2, h2 = box2[2] - box2[0], box2[3] - box2[1] ar = np.maximum(w2 / (h2 + 1e-16), h2 / (w2 + 1e-16)) # aspect ratio return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + 1e-16) > area_thr) & (ar < ar_thr) # candidates def cutout(image, labels): # Applies image cutout augmentation https://arxiv.org/abs/1708.04552 h, w = image.shape[:2] def bbox_ioa(box1, box2): # Returns the intersection over box2 area given box1, box2. box1 is 4, box2 is nx4. boxes are x1y1x2y2 box2 = box2.transpose() # Get the coordinates of bounding boxes b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3] b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3] # Intersection area inter_area = (np.minimum(b1_x2, b2_x2) - np.maximum(b1_x1, b2_x1)).clip(0) * \ (np.minimum(b1_y2, b2_y2) - np.maximum(b1_y1, b2_y1)).clip(0) # box2 area box2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + 1e-16 # Intersection over box2 area return inter_area / box2_area # create random masks scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16 # image size fraction for s in scales: mask_h = random.randint(1, int(h * s)) mask_w = random.randint(1, int(w * s)) # box xmin = max(0, random.randint(0, w) - mask_w // 2) ymin = max(0, random.randint(0, h) - mask_h // 2) xmax = min(w, xmin + mask_w) ymax = min(h, ymin + mask_h) # apply random color mask image[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)] # return unobscured labels if len(labels) and s > 0.03: box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32) ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area labels = labels[ioa < 0.60] # remove >60% obscured labels return labels def create_folder(path='./new'): # Create folder if os.path.exists(path): shutil.rmtree(path) # delete output folder os.makedirs(path) # make new output folder def flatten_recursive(path='../coco128'): # Flatten a recursive directory by bringing all files to top level new_path = Path(path + '_flat') create_folder(new_path) for file in tqdm(glob.glob(str(Path(path)) + '/**/*.*', recursive=True)): shutil.copyfile(file, new_path / Path(file).name) ================================================ FILE: asone/detectors/yolor/utils/export.py ================================================ import argparse import torch from asone.detectors.yolor.models.models import * from asone.detectors.yolor.utils.google_utils import attempt_download if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--weights', type=str, default='./yolov4.pt', help='weights path') parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size') parser.add_argument('--batch-size', type=int, default=1, help='batch size') parser.add_argument('--cfg', type=str, default='cfg/yolor_p6.cfg', help='*.cfg path') opt = parser.parse_args() opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand # print(opt) # Input img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size(1,3,320,192) iDetection # Load PyTorch model attempt_download(opt.weights) # print(ad) # model = Darknet(cfg, ).cuda() model.load_state_dict(torch.load(opt.weights, map_location=device)['model']) print(type(model)) print("*"*50) exit() model.eval() model.model[-1].export = True # set Detect() layer export=True y = model(img) # dry run # print("-------------------") # model = Darknet(cfg, imgsz).cuda() # model.load_state_dict(torch.load(weights[0], map_location=device)['model']) #model = attempt_load(weights, map_location=device) # load FP32 model #imgsz = check_img_size(imgsz, s=model.stride.max()) # check img_size # model.to(device).eval() # TorchScript export try: print('\nStarting TorchScript export with torch %s...' % torch.__version__) f = opt.weights.replace('.pt', '.torchscript.pt') # filename ts = torch.jit.trace(model, img) ts.save(f) print('TorchScript export success, saved as %s' % f) except Exception as e: print('TorchScript export failure: %s' % e) # ONNX export try: import onnx print('\nStarting ONNX export with onnx %s...' % onnx.__version__) f = opt.weights.replace('.pt', '.onnx') # filename model.fuse() # only for ONNX torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'], output_names=['classes', 'boxes'] if y is None else ['output']) # Checks onnx_model = onnx.load(f) # load onnx model onnx.checker.check_model(onnx_model) # check onnx model print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable model print('ONNX export success, saved as %s' % f) except Exception as e: print('ONNX export failure: %s' % e) # CoreML export try: import coremltools as ct print('\nStarting CoreML export with coremltools %s...' % ct.__version__) # convert model from torchscript and apply pixel scaling as per detect.py model = ct.convert(ts, inputs=[ct.ImageType(name='images', shape=img.shape, scale=1 / 255.0, bias=[0, 0, 0])]) f = opt.weights.replace('.pt', '.mlmodel') # filename model.save(f) print('CoreML export success, saved as %s' % f) except Exception as e: print('CoreML export failure: %s' % e) # Finish print('\nExport complete. Visualize with https://github.com/lutzroeder/netron.') ================================================ FILE: asone/detectors/yolor/utils/general.py ================================================ # General utils import glob import logging import math import os import platform import random import re import subprocess import time from pathlib import Path import cv2 import matplotlib import numpy as np import torch import yaml from asone.detectors.yolor.utils.google_utils import gsutil_getsize from asone.detectors.yolor.utils.metrics import fitness from asone.detectors.yolor.utils.torch_utils import init_torch_seeds # Set printoptions torch.set_printoptions(linewidth=320, precision=5, profile='long') np.set_printoptions(linewidth=320, formatter={'float_kind': '{:11.5g}'.format}) # format short g, %precision=5 matplotlib.rc('font', **{'size': 11}) # Prevent OpenCV from multithreading (to use PyTorch DataLoader) cv2.setNumThreads(0) def set_logging(rank=-1): logging.basicConfig( format="%(message)s", level=logging.INFO if rank in [-1, 0] else logging.WARN) def init_seeds(seed=0): random.seed(seed) np.random.seed(seed) init_torch_seeds(seed) def get_latest_run(search_dir='.'): # Return path to most recent 'last.pt' in /runs (i.e. to --resume from) last_list = glob.glob(f'{search_dir}/**/last*.pt', recursive=True) return max(last_list, key=os.path.getctime) if last_list else '' def check_git_status(): # Suggest 'git pull' if repo is out of date if platform.system() in ['Linux', 'Darwin'] and not os.path.isfile('/.dockerenv'): s = subprocess.check_output('if [ -d .git ]; then git fetch && git status -uno; fi', shell=True).decode('utf-8') if 'Your branch is behind' in s: print(s[s.find('Your branch is behind'):s.find('\n\n')] + '\n') def check_img_size(img_size, s=32): # Verify img_size is a multiple of stride s new_size = make_divisible(img_size, int(s)) # ceil gs-multiple if new_size != img_size: print('WARNING: --img-size %g must be multiple of max stride %g, updating to %g' % (img_size, s, new_size)) return new_size def check_file(file): # Search for file if not found if os.path.isfile(file) or file == '': return file else: files = glob.glob('./**/' + file, recursive=True) # find file assert len(files), 'File Not Found: %s' % file # assert file was found assert len(files) == 1, "Multiple files match '%s', specify exact path: %s" % (file, files) # assert unique return files[0] # return file def check_dataset(dict): # Download dataset if not found locally val, s = dict.get('val'), dict.get('download') if val and len(val): val = [Path(x).resolve() for x in (val if isinstance(val, list) else [val])] # val path if not all(x.exists() for x in val): print('\nWARNING: Dataset not found, nonexistent paths: %s' % [str(x) for x in val if not x.exists()]) if s and len(s): # download script print('Downloading %s ...' % s) if s.startswith('http') and s.endswith('.zip'): # URL f = Path(s).name # filename torch.hub.download_url_to_file(s, f) r = os.system('unzip -q %s -d ../ && rm %s' % (f, f)) # unzip else: # bash script r = os.system(s) print('Dataset autodownload %s\n' % ('success' if r == 0 else 'failure')) # analyze return value else: raise Exception('Dataset not found.') def make_divisible(x, divisor): # Returns x evenly divisible by divisor return math.ceil(x / divisor) * divisor def labels_to_class_weights(labels, nc=80): # Get class weights (inverse frequency) from training labels if labels[0] is None: # no labels loaded return torch.Tensor() labels = np.concatenate(labels, 0) # labels.shape = (866643, 5) for COCO classes = labels[:, 0].astype(np.int) # labels = [class xywh] weights = np.bincount(classes, minlength=nc) # occurrences per class # Prepend gridpoint count (for uCE training) # gpi = ((320 / 32 * np.array([1, 2, 4])) ** 2 * 3).sum() # gridpoints per image # weights = np.hstack([gpi * len(labels) - weights.sum() * 9, weights * 9]) ** 0.5 # prepend gridpoints to start weights[weights == 0] = 1 # replace empty bins with 1 weights = 1 / weights # number of targets per class weights /= weights.sum() # normalize return torch.from_numpy(weights) def labels_to_image_weights(labels, nc=80, class_weights=np.ones(80)): # Produces image weights based on class mAPs n = len(labels) class_counts = np.array([np.bincount(labels[i][:, 0].astype(np.int), minlength=nc) for i in range(n)]) image_weights = (class_weights.reshape(1, nc) * class_counts).sum(1) # index = random.choices(range(n), weights=image_weights, k=1) # weight image sample return image_weights def coco80_to_coco91_class(): # converts 80-index (val2014) to 91-index (paper) # https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/ # a = np.loadtxt('data/coco.names', dtype='str', delimiter='\n') # b = np.loadtxt('data/coco_paper.names', dtype='str', delimiter='\n') # x1 = [list(a[i] == b).index(True) + 1 for i in range(80)] # darknet to coco # x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)] # coco to darknet x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90] return x def xyxy2xywh(x): # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = (x[:, 0] + x[:, 2]) / 2 # x center y[:, 1] = (x[:, 1] + x[:, 3]) / 2 # y center y[:, 2] = x[:, 2] - x[:, 0] # width y[:, 3] = x[:, 3] - x[:, 1] # height return y def xywh2xyxy(x): # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y return y def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None): # Rescale coords (xyxy) from img1_shape to img0_shape if ratio_pad is None: # calculate from img0_shape gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding else: gain = ratio_pad[0][0] pad = ratio_pad[1] coords[:, [0, 2]] -= pad[0] # x padding coords[:, [1, 3]] -= pad[1] # y padding coords[:, :4] /= gain clip_coords(coords, img0_shape) return coords def clip_coords(boxes, img_shape): # Clip bounding xyxy bounding boxes to image shape (height, width) boxes[:, 0].clamp_(0, img_shape[1]) # x1 boxes[:, 1].clamp_(0, img_shape[0]) # y1 boxes[:, 2].clamp_(0, img_shape[1]) # x2 boxes[:, 3].clamp_(0, img_shape[0]) # y2 def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, EIoU=False, ECIoU=False, eps=1e-9): # Returns the IoU of box1 to box2. box1 is 4, box2 is nx4 box2 = box2.T # Get the coordinates of bounding boxes if x1y1x2y2: # x1, y1, x2, y2 = box1 b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3] b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3] else: # transform from xywh to xyxy b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2 b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2 b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2 b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2 # Intersection area inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \ (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0) # Union Area w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps union = w1 * h1 + w2 * h2 - inter + eps iou = inter / union if GIoU or DIoU or CIoU or EIoU or ECIoU: cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height if CIoU or DIoU or EIoU or ECIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1 c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared if DIoU: return iou - rho2 / c2 # DIoU elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47 v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2) with torch.no_grad(): alpha = v / ((1 + eps) - iou + v) return iou - (rho2 / c2 + v * alpha) # CIoU elif EIoU: # Efficient IoU https://arxiv.org/abs/2101.08158 rho3 = (w1-w2) **2 c3 = cw ** 2 + eps rho4 = (h1-h2) **2 c4 = ch ** 2 + eps return iou - rho2 / c2 - rho3 / c3 - rho4 / c4 # EIoU elif ECIoU: v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2) with torch.no_grad(): alpha = v / ((1 + eps) - iou + v) rho3 = (w1-w2) **2 c3 = cw ** 2 + eps rho4 = (h1-h2) **2 c4 = ch ** 2 + eps return iou - v * alpha - rho2 / c2 - rho3 / c3 - rho4 / c4 # ECIoU else: # GIoU https://arxiv.org/pdf/1902.09630.pdf c_area = cw * ch + eps # convex area return iou - (c_area - union) / c_area # GIoU else: return iou # IoU def box_iou(box1, box2): # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Arguments: box1 (Tensor[N, 4]) box2 (Tensor[M, 4]) Returns: iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 """ def box_area(box): # box = 4xn return (box[2] - box[0]) * (box[3] - box[1]) area1 = box_area(box1.T) area2 = box_area(box2.T) # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2) inter = (torch.min(box1[:, None, 2:], box2[:, 2:]) - torch.max(box1[:, None, :2], box2[:, :2])).clamp(0).prod(2) return inter / (area1[:, None] + area2 - inter) # iou = inter / (area1 + area2 - inter) def wh_iou(wh1, wh2): # Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2 wh1 = wh1[:, None] # [N,1,2] wh2 = wh2[None] # [1,M,2] inter = torch.min(wh1, wh2).prod(2) # [N,M] return inter / (wh1.prod(2) + wh2.prod(2) - inter) # iou = inter / (area1 + area2 - inter) def non_max_suppression(prediction, conf_thres=0.1, iou_thres=0.6, merge=False, classes=None, agnostic=False): """Performs Non-Maximum Suppression (NMS) on inference results Returns: detections with shape: nx6 (x1, y1, x2, y2, conf, cls) """ nc = prediction[0].shape[1] - 5 # number of classes xc = prediction[..., 4] > conf_thres # candidates # Settings min_wh, max_wh = 2, 4096 # (pixels) minimum and maximum box width and height max_det = 300 # maximum number of detections per image time_limit = 10.0 # seconds to quit after redundant = True # require redundant detections multi_label = nc > 1 # multiple labels per box (adds 0.5ms/img) t = time.time() output = [torch.zeros(0, 6)] * prediction.shape[0] for xi, x in enumerate(prediction): # image index, image inference # Apply constraints # x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height x = x[xc[xi]] # confidence # If none remain process next image if not x.shape[0]: continue # Compute conf x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf # Box (center x, center y, width, height) to (x1, y1, x2, y2) box = xywh2xyxy(x[:, :4]) # Detections matrix nx6 (xyxy, conf, cls) if multi_label: i, j = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T x = torch.cat((box[i], x[i, j + 5, None], j[:, None].float()), 1) else: # best class only conf, j = x[:, 5:].max(1, keepdim=True) x = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres] # Filter by class if classes: x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)] # Apply finite constraint # if not torch.isfinite(x).all(): # x = x[torch.isfinite(x).all(1)] # If none remain process next image n = x.shape[0] # number of boxes if not n: continue # Sort by confidence # x = x[x[:, 4].argsort(descending=True)] # Batched NMS c = x[:, 5:6] * (0 if agnostic else max_wh) # classes boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores i = torch.ops.torchvision.nms(boxes, scores, iou_thres) if i.shape[0] > max_det: # limit detections i = i[:max_det] if merge and (1 < n < 3E3): # Merge NMS (boxes merged using weighted mean) # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4) iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix weights = iou * scores[None] # box weights x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes if redundant: i = i[iou.sum(1) > 1] # require redundancy output[xi] = x[i] if (time.time() - t) > time_limit: break # time limit exceeded return output def strip_optimizer(f='weights/best.pt', s=''): # from utils.general import *; strip_optimizer() # Strip optimizer from 'f' to finalize training, optionally save as 's' x = torch.load(f, map_location=torch.device('cpu')) x['optimizer'] = None x['training_results'] = None x['epoch'] = -1 #x['model'].half() # to FP16 #for p in x['model'].parameters(): # p.requires_grad = False torch.save(x, s or f) mb = os.path.getsize(s or f) / 1E6 # filesize print('Optimizer stripped from %s,%s %.1fMB' % (f, (' saved as %s,' % s) if s else '', mb)) def print_mutation(hyp, results, yaml_file='hyp_evolved.yaml', bucket=''): # Print mutation results to evolve.txt (for use with train.py --evolve) a = '%10s' * len(hyp) % tuple(hyp.keys()) # hyperparam keys b = '%10.3g' * len(hyp) % tuple(hyp.values()) # hyperparam values c = '%10.4g' * len(results) % results # results (P, R, mAP@0.5, mAP@0.5:0.95, val_losses x 3) print('\n%s\n%s\nEvolved fitness: %s\n' % (a, b, c)) if bucket: url = 'gs://%s/evolve.txt' % bucket if gsutil_getsize(url) > (os.path.getsize('evolve.txt') if os.path.exists('evolve.txt') else 0): os.system('gsutil cp %s .' % url) # download evolve.txt if larger than local with open('evolve.txt', 'a') as f: # append result f.write(c + b + '\n') x = np.unique(np.loadtxt('evolve.txt', ndmin=2), axis=0) # load unique rows x = x[np.argsort(-fitness(x))] # sort np.savetxt('evolve.txt', x, '%10.3g') # save sort by fitness # Save yaml for i, k in enumerate(hyp.keys()): hyp[k] = float(x[0, i + 7]) with open(yaml_file, 'w') as f: results = tuple(x[0, :7]) c = '%10.4g' * len(results) % results # results (P, R, mAP@0.5, mAP@0.5:0.95, val_losses x 3) f.write('# Hyperparameter Evolution Results\n# Generations: %g\n# Metrics: ' % len(x) + c + '\n\n') yaml.dump(hyp, f, sort_keys=False) if bucket: os.system('gsutil cp evolve.txt %s gs://%s' % (yaml_file, bucket)) # upload def apply_classifier(x, model, img, im0): # applies a second stage classifier to yolo outputs im0 = [im0] if isinstance(im0, np.ndarray) else im0 for i, d in enumerate(x): # per image if d is not None and len(d): d = d.clone() # Reshape and pad cutouts b = xyxy2xywh(d[:, :4]) # boxes b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1) # rectangle to square b[:, 2:] = b[:, 2:] * 1.3 + 30 # pad d[:, :4] = xywh2xyxy(b).long() # Rescale boxes from img_size to im0 size scale_coords(img.shape[2:], d[:, :4], im0[i].shape) # Classes pred_cls1 = d[:, 5].long() ims = [] for j, a in enumerate(d): # per item cutout = im0[i][int(a[1]):int(a[3]), int(a[0]):int(a[2])] im = cv2.resize(cutout, (224, 224)) # BGR # cv2.imwrite('test%i.jpg' % j, cutout) im = im[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 im = np.ascontiguousarray(im, dtype=np.float32) # uint8 to float32 im /= 255.0 # 0 - 255 to 0.0 - 1.0 ims.append(im) pred_cls2 = model(torch.Tensor(ims).to(d.device)).argmax(1) # classifier prediction x[i] = x[i][pred_cls1 == pred_cls2] # retain matching class detections return x def increment_path(path, exist_ok=True, sep=''): # Increment path, i.e. runs/exp --> runs/exp{sep}0, runs/exp{sep}1 etc. path = Path(path) # os-agnostic if (path.exists() and exist_ok) or (not path.exists()): return str(path) else: dirs = glob.glob(f"{path}{sep}*") # similar paths matches = [re.search(rf"%s{sep}(\d+)" % path.stem, d) for d in dirs] i = [int(m.groups()[0]) for m in matches if m] # indices n = max(i) + 1 if i else 2 # increment number return f"{path}{sep}{n}" # update path ================================================ FILE: asone/detectors/yolor/utils/google_utils.py ================================================ # Google utils: https://cloud.google.com/storage/docs/reference/libraries import os import platform import subprocess import time from pathlib import Path import torch import torch.nn as nn def gsutil_getsize(url=''): # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du s = subprocess.check_output('gsutil du %s' % url, shell=True).decode('utf-8') return eval(s.split(' ')[0]) if len(s) else 0 # bytes def attempt_download(weights): # Attempt to download pretrained weights if not found locally weights = weights.strip().replace("'", '') file = Path(weights).name msg = weights + ' missing, try downloading from https://github.com/WongKinYiu/yolor/releases/' models = ['yolor_p6.pt', 'yolor_w6.pt'] # available models if file in models and not os.path.isfile(weights): try: # GitHub url = 'https://github.com/WongKinYiu/yolor/releases/download/v1.0/' + file print('Downloading %s to %s...' % (url, weights)) torch.hub.download_url_to_file(url, weights) assert os.path.exists(weights) and os.path.getsize(weights) > 1E6 # check except Exception as e: # GCP print('ERROR: Download failure.') print('') def attempt_load(weights, map_location=None): # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a model = Ensemble() for w in weights if isinstance(weights, list) else [weights]: attempt_download(w) model.append(torch.load(w, map_location=map_location)['model'].float().fuse().eval()) # load FP32 model if len(model) == 1: return model[-1] # return model else: print('Ensemble created with %s\n' % weights) for k in ['names', 'stride']: setattr(model, k, getattr(model[-1], k)) return model # return ensemble def gdrive_download(id='1n_oKgR81BJtqk75b00eAjdv03qVCQn2f', name='coco128.zip'): # Downloads a file from Google Drive. from utils.google_utils import *; gdrive_download() t = time.time() print('Downloading https://drive.google.com/uc?export=download&id=%s as %s... ' % (id, name), end='') os.remove(name) if os.path.exists(name) else None # remove existing os.remove('cookie') if os.path.exists('cookie') else None # Attempt file download out = "NUL" if platform.system() == "Windows" else "/dev/null" os.system('curl -c ./cookie -s -L "drive.google.com/uc?export=download&id=%s" > %s ' % (id, out)) if os.path.exists('cookie'): # large file s = 'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm=%s&id=%s" -o %s' % (get_token(), id, name) else: # small file s = 'curl -s -L -o %s "drive.google.com/uc?export=download&id=%s"' % (name, id) r = os.system(s) # execute, capture return os.remove('cookie') if os.path.exists('cookie') else None # Error check if r != 0: os.remove(name) if os.path.exists(name) else None # remove partial print('Download error ') # raise Exception('Download error') return r # Unzip if archive if name.endswith('.zip'): print('unzipping... ', end='') os.system('unzip -q %s' % name) # unzip os.remove(name) # remove zip to free space print('Done (%.1fs)' % (time.time() - t)) return r def get_token(cookie="./cookie"): with open(cookie) as f: for line in f: if "download" in line: return line.split()[-1] return "" class Ensemble(nn.ModuleList): # Ensemble of models def __init__(self): super().__init__() def forward(self, x, augment=False, profile=False, visualize=False): y = [module(x, augment, profile, visualize)[0] for module in self] # y = torch.stack(y).max(0)[0] # max ensemble # y = torch.stack(y).mean(0) # mean ensemble y = torch.cat(y, 1) # nms ensemble return y, None # inference, train output # def upload_blob(bucket_name, source_file_name, destination_blob_name): # # Uploads a file to a bucket # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python # # storage_client = storage.Client() # bucket = storage_client.get_bucket(bucket_name) # blob = bucket.blob(destination_blob_name) # # blob.upload_from_filename(source_file_name) # # print('File {} uploaded to {}.'.format( # source_file_name, # destination_blob_name)) # # # def download_blob(bucket_name, source_blob_name, destination_file_name): # # Uploads a blob from a bucket # storage_client = storage.Client() # bucket = storage_client.get_bucket(bucket_name) # blob = bucket.blob(source_blob_name) # # blob.download_to_filename(destination_file_name) # # print('Blob {} downloaded to {}.'.format( # source_blob_name, # destination_file_name)) ================================================ FILE: asone/detectors/yolor/utils/layers.py ================================================ from asone.detectors.yolor.utils.general import * import torch from torch import nn import torch.nn.functional as F try: from mish_cuda import MishCuda as Mish except: class Mish(nn.Module): # https://github.com/digantamisra98/Mish def forward(self, x): return x * F.softplus(x).tanh() try: from pytorch_wavelets import DWTForward, DWTInverse class DWT(nn.Module): def __init__(self): super(DWT, self).__init__() self.xfm = DWTForward(J=1, wave='db1', mode='zero') def forward(self, x): b,c,w,h = x.shape yl, yh = self.xfm(x) return torch.cat([yl/2., yh[0].view(b,-1,w//2,h//2)/2.+.5], 1) except: # using Reorg instead class DWT(nn.Module): def forward(self, x): return torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1) class Reorg(nn.Module): def forward(self, x): return torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1) def make_divisible(v, divisor): # Function ensures all layers have a channel number that is divisible by 8 # https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py return math.ceil(v / divisor) * divisor class Flatten(nn.Module): # Use after nn.AdaptiveAvgPool2d(1) to remove last 2 dimensions def forward(self, x): return x.view(x.size(0), -1) class Concat(nn.Module): # Concatenate a list of tensors along dimension def __init__(self, dimension=1): super(Concat, self).__init__() self.d = dimension def forward(self, x): return torch.cat(x, self.d) class FeatureConcat(nn.Module): def __init__(self, layers): super(FeatureConcat, self).__init__() self.layers = layers # layer indices self.multiple = len(layers) > 1 # multiple layers flag def forward(self, x, outputs): return torch.cat([outputs[i] for i in self.layers], 1) if self.multiple else outputs[self.layers[0]] class FeatureConcat2(nn.Module): def __init__(self, layers): super(FeatureConcat2, self).__init__() self.layers = layers # layer indices self.multiple = len(layers) > 1 # multiple layers flag def forward(self, x, outputs): return torch.cat([outputs[self.layers[0]], outputs[self.layers[1]].detach()], 1) class FeatureConcat3(nn.Module): def __init__(self, layers): super(FeatureConcat3, self).__init__() self.layers = layers # layer indices self.multiple = len(layers) > 1 # multiple layers flag def forward(self, x, outputs): return torch.cat([outputs[self.layers[0]], outputs[self.layers[1]].detach(), outputs[self.layers[2]].detach()], 1) class FeatureConcat_l(nn.Module): def __init__(self, layers): super(FeatureConcat_l, self).__init__() self.layers = layers # layer indices self.multiple = len(layers) > 1 # multiple layers flag def forward(self, x, outputs): return torch.cat([outputs[i][:,:outputs[i].shape[1]//2,:,:] for i in self.layers], 1) if self.multiple else outputs[self.layers[0]][:,:outputs[self.layers[0]].shape[1]//2,:,:] class WeightedFeatureFusion(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, layers, weight=False): super(WeightedFeatureFusion, self).__init__() self.layers = layers # layer indices self.weight = weight # apply weights boolean self.n = len(layers) + 1 # number of layers if weight: self.w = nn.Parameter(torch.zeros(self.n), requires_grad=True) # layer weights def forward(self, x, outputs): # Weights if self.weight: w = torch.sigmoid(self.w) * (2 / self.n) # sigmoid weights (0-1) x = x * w[0] # Fusion nx = x.shape[1] # input channels for i in range(self.n - 1): a = outputs[self.layers[i]] * w[i + 1] if self.weight else outputs[self.layers[i]] # feature to add na = a.shape[1] # feature channels # Adjust channels if nx == na: # same shape x = x + a elif nx > na: # slice input x[:, :na] = x[:, :na] + a # or a = nn.ZeroPad2d((0, 0, 0, 0, 0, dc))(a); x = x + a else: # slice feature x = x + a[:, :nx] return x class MixConv2d(nn.Module): # MixConv: Mixed Depthwise Convolutional Kernels https://arxiv.org/abs/1907.09595 def __init__(self, in_ch, out_ch, k=(3, 5, 7), stride=1, dilation=1, bias=True, method='equal_params'): super(MixConv2d, self).__init__() groups = len(k) if method == 'equal_ch': # equal channels per group i = torch.linspace(0, groups - 1E-6, out_ch).floor() # out_ch indices ch = [(i == g).sum() for g in range(groups)] else: # 'equal_params': equal parameter count per group b = [out_ch] + [0] * groups a = np.eye(groups + 1, groups, k=-1) a -= np.roll(a, 1, axis=1) a *= np.array(k) ** 2 a[0] = 1 ch = np.linalg.lstsq(a, b, rcond=None)[0].round().astype(int) # solve for equal weight indices, ax = b self.m = nn.ModuleList([nn.Conv2d(in_channels=in_ch, out_channels=ch[g], kernel_size=k[g], stride=stride, padding=k[g] // 2, # 'same' pad dilation=dilation, bias=bias) for g in range(groups)]) def forward(self, x): return torch.cat([m(x) for m in self.m], 1) # Activation functions below ------------------------------------------------------------------------------------------- class SwishImplementation(torch.autograd.Function): @staticmethod def forward(ctx, x): ctx.save_for_backward(x) return x * torch.sigmoid(x) @staticmethod def backward(ctx, grad_output): x = ctx.saved_tensors[0] sx = torch.sigmoid(x) # sigmoid(ctx) return grad_output * (sx * (1 + x * (1 - sx))) class MishImplementation(torch.autograd.Function): @staticmethod def forward(ctx, x): ctx.save_for_backward(x) return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x))) @staticmethod def backward(ctx, grad_output): x = ctx.saved_tensors[0] sx = torch.sigmoid(x) fx = F.softplus(x).tanh() return grad_output * (fx + x * sx * (1 - fx * fx)) class MemoryEfficientSwish(nn.Module): def forward(self, x): return SwishImplementation.apply(x) class MemoryEfficientMish(nn.Module): def forward(self, x): return MishImplementation.apply(x) class Swish(nn.Module): def forward(self, x): return x * torch.sigmoid(x) class HardSwish(nn.Module): # https://arxiv.org/pdf/1905.02244.pdf def forward(self, x): return x * F.hardtanh(x + 3, 0., 6., True) / 6. class DeformConv2d(nn.Module): def __init__(self, inc, outc, kernel_size=3, padding=1, stride=1, bias=None, modulation=False): """ Args: modulation (bool, optional): If True, Modulated Defomable Convolution (Deformable ConvNets v2). """ super(DeformConv2d, self).__init__() self.kernel_size = kernel_size self.padding = padding self.stride = stride self.zero_padding = nn.ZeroPad2d(padding) self.conv = nn.Conv2d(inc, outc, kernel_size=kernel_size, stride=kernel_size, bias=bias) self.p_conv = nn.Conv2d(inc, 2*kernel_size*kernel_size, kernel_size=3, padding=1, stride=stride) nn.init.constant_(self.p_conv.weight, 0) self.p_conv.register_backward_hook(self._set_lr) self.modulation = modulation if modulation: self.m_conv = nn.Conv2d(inc, kernel_size*kernel_size, kernel_size=3, padding=1, stride=stride) nn.init.constant_(self.m_conv.weight, 0) self.m_conv.register_backward_hook(self._set_lr) @staticmethod def _set_lr(module, grad_input, grad_output): grad_input = (grad_input[i] * 0.1 for i in range(len(grad_input))) grad_output = (grad_output[i] * 0.1 for i in range(len(grad_output))) def forward(self, x): offset = self.p_conv(x) if self.modulation: m = torch.sigmoid(self.m_conv(x)) dtype = offset.data.type() ks = self.kernel_size N = offset.size(1) // 2 if self.padding: x = self.zero_padding(x) # (b, 2N, h, w) p = self._get_p(offset, dtype) # (b, h, w, 2N) p = p.contiguous().permute(0, 2, 3, 1) q_lt = p.detach().floor() q_rb = q_lt + 1 q_lt = torch.cat([torch.clamp(q_lt[..., :N], 0, x.size(2)-1), torch.clamp(q_lt[..., N:], 0, x.size(3)-1)], dim=-1).long() q_rb = torch.cat([torch.clamp(q_rb[..., :N], 0, x.size(2)-1), torch.clamp(q_rb[..., N:], 0, x.size(3)-1)], dim=-1).long() q_lb = torch.cat([q_lt[..., :N], q_rb[..., N:]], dim=-1) q_rt = torch.cat([q_rb[..., :N], q_lt[..., N:]], dim=-1) # clip p p = torch.cat([torch.clamp(p[..., :N], 0, x.size(2)-1), torch.clamp(p[..., N:], 0, x.size(3)-1)], dim=-1) # bilinear kernel (b, h, w, N) g_lt = (1 + (q_lt[..., :N].type_as(p) - p[..., :N])) * (1 + (q_lt[..., N:].type_as(p) - p[..., N:])) g_rb = (1 - (q_rb[..., :N].type_as(p) - p[..., :N])) * (1 - (q_rb[..., N:].type_as(p) - p[..., N:])) g_lb = (1 + (q_lb[..., :N].type_as(p) - p[..., :N])) * (1 - (q_lb[..., N:].type_as(p) - p[..., N:])) g_rt = (1 - (q_rt[..., :N].type_as(p) - p[..., :N])) * (1 + (q_rt[..., N:].type_as(p) - p[..., N:])) # (b, c, h, w, N) x_q_lt = self._get_x_q(x, q_lt, N) x_q_rb = self._get_x_q(x, q_rb, N) x_q_lb = self._get_x_q(x, q_lb, N) x_q_rt = self._get_x_q(x, q_rt, N) # (b, c, h, w, N) x_offset = g_lt.unsqueeze(dim=1) * x_q_lt + \ g_rb.unsqueeze(dim=1) * x_q_rb + \ g_lb.unsqueeze(dim=1) * x_q_lb + \ g_rt.unsqueeze(dim=1) * x_q_rt # modulation if self.modulation: m = m.contiguous().permute(0, 2, 3, 1) m = m.unsqueeze(dim=1) m = torch.cat([m for _ in range(x_offset.size(1))], dim=1) x_offset *= m x_offset = self._reshape_x_offset(x_offset, ks) out = self.conv(x_offset) return out def _get_p_n(self, N, dtype): p_n_x, p_n_y = torch.meshgrid( torch.arange(-(self.kernel_size-1)//2, (self.kernel_size-1)//2+1), torch.arange(-(self.kernel_size-1)//2, (self.kernel_size-1)//2+1)) # (2N, 1) p_n = torch.cat([torch.flatten(p_n_x), torch.flatten(p_n_y)], 0) p_n = p_n.view(1, 2*N, 1, 1).type(dtype) return p_n def _get_p_0(self, h, w, N, dtype): p_0_x, p_0_y = torch.meshgrid( torch.arange(1, h*self.stride+1, self.stride), torch.arange(1, w*self.stride+1, self.stride)) p_0_x = torch.flatten(p_0_x).view(1, 1, h, w).repeat(1, N, 1, 1) p_0_y = torch.flatten(p_0_y).view(1, 1, h, w).repeat(1, N, 1, 1) p_0 = torch.cat([p_0_x, p_0_y], 1).type(dtype) return p_0 def _get_p(self, offset, dtype): N, h, w = offset.size(1)//2, offset.size(2), offset.size(3) # (1, 2N, 1, 1) p_n = self._get_p_n(N, dtype) # (1, 2N, h, w) p_0 = self._get_p_0(h, w, N, dtype) p = p_0 + p_n + offset return p def _get_x_q(self, x, q, N): b, h, w, _ = q.size() padded_w = x.size(3) c = x.size(1) # (b, c, h*w) x = x.contiguous().view(b, c, -1) # (b, h, w, N) index = q[..., :N]*padded_w + q[..., N:] # offset_x*w + offset_y # (b, c, h*w*N) index = index.contiguous().unsqueeze(dim=1).expand(-1, c, -1, -1, -1).contiguous().view(b, c, -1) x_offset = x.gather(dim=-1, index=index).contiguous().view(b, c, h, w, N) return x_offset @staticmethod def _reshape_x_offset(x_offset, ks): b, c, h, w, N = x_offset.size() x_offset = torch.cat([x_offset[..., s:s+ks].contiguous().view(b, c, h, w*ks) for s in range(0, N, ks)], dim=-1) x_offset = x_offset.contiguous().view(b, c, h*ks, w*ks) return x_offset class GAP(nn.Module): def __init__(self): super(GAP, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) def forward(self, x): #b, c, _, _ = x.size() return self.avg_pool(x)#.view(b, c) class Silence(nn.Module): def __init__(self): super(Silence, self).__init__() def forward(self, x): return x class ScaleChannel(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, layers): super(ScaleChannel, self).__init__() self.layers = layers # layer indices def forward(self, x, outputs): a = outputs[self.layers[0]] return x.expand_as(a) * a class ShiftChannel(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, layers): super(ShiftChannel, self).__init__() self.layers = layers # layer indices def forward(self, x, outputs): a = outputs[self.layers[0]] return a.expand_as(x) + x class ShiftChannel2D(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, layers): super(ShiftChannel2D, self).__init__() self.layers = layers # layer indices def forward(self, x, outputs): a = outputs[self.layers[0]].view(1,-1,1,1) return a.expand_as(x) + x class ControlChannel(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, layers): super(ControlChannel, self).__init__() self.layers = layers # layer indices def forward(self, x, outputs): a = outputs[self.layers[0]] return a.expand_as(x) * x class ControlChannel2D(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, layers): super(ControlChannel2D, self).__init__() self.layers = layers # layer indices def forward(self, x, outputs): a = outputs[self.layers[0]].view(1,-1,1,1) return a.expand_as(x) * x class AlternateChannel(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, layers): super(AlternateChannel, self).__init__() self.layers = layers # layer indices def forward(self, x, outputs): a = outputs[self.layers[0]] return torch.cat([a.expand_as(x), x], dim=1) class AlternateChannel2D(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, layers): super(AlternateChannel2D, self).__init__() self.layers = layers # layer indices def forward(self, x, outputs): a = outputs[self.layers[0]].view(1,-1,1,1) return torch.cat([a.expand_as(x), x], dim=1) class SelectChannel(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, layers): super(SelectChannel, self).__init__() self.layers = layers # layer indices def forward(self, x, outputs): a = outputs[self.layers[0]] return a.sigmoid().expand_as(x) * x class SelectChannel2D(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, layers): super(SelectChannel2D, self).__init__() self.layers = layers # layer indices def forward(self, x, outputs): a = outputs[self.layers[0]].view(1,-1,1,1) return a.sigmoid().expand_as(x) * x class ScaleSpatial(nn.Module): # weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, layers): super(ScaleSpatial, self).__init__() self.layers = layers # layer indices def forward(self, x, outputs): a = outputs[self.layers[0]] return x * a class ImplicitA(nn.Module): def __init__(self, channel): super(ImplicitA, self).__init__() self.channel = channel self.implicit = nn.Parameter(torch.zeros(1, channel, 1, 1)) nn.init.normal_(self.implicit, std=.02) def forward(self): return self.implicit class ImplicitC(nn.Module): def __init__(self, channel): super(ImplicitC, self).__init__() self.channel = channel self.implicit = nn.Parameter(torch.zeros(1, channel, 1, 1)) nn.init.normal_(self.implicit, std=.02) def forward(self): return self.implicit class ImplicitM(nn.Module): def __init__(self, channel): super(ImplicitM, self).__init__() self.channel = channel self.implicit = nn.Parameter(torch.ones(1, channel, 1, 1)) nn.init.normal_(self.implicit, mean=1., std=.02) def forward(self): return self.implicit class Implicit2DA(nn.Module): def __init__(self, atom, channel): super(Implicit2DA, self).__init__() self.channel = channel self.implicit = nn.Parameter(torch.zeros(1, atom, channel, 1)) nn.init.normal_(self.implicit, std=.02) def forward(self): return self.implicit class Implicit2DC(nn.Module): def __init__(self, atom, channel): super(Implicit2DC, self).__init__() self.channel = channel self.implicit = nn.Parameter(torch.zeros(1, atom, channel, 1)) nn.init.normal_(self.implicit, std=.02) def forward(self): return self.implicit class Implicit2DM(nn.Module): def __init__(self, atom, channel): super(Implicit2DM, self).__init__() self.channel = channel self.implicit = nn.Parameter(torch.ones(1, atom, channel, 1)) nn.init.normal_(self.implicit, mean=1., std=.02) def forward(self): return self.implicit ================================================ FILE: asone/detectors/yolor/utils/loss.py ================================================ # Loss functions import torch import torch.nn as nn from asone.detectors.yolor.utils.general import bbox_iou from asone.detectors.yolor.utils.torch_utils import is_parallel def smooth_BCE(eps=0.1): # https://github.com/ultralytics/yolov3/issues/238#issuecomment-598028441 # return positive, negative label smoothing BCE targets return 1.0 - 0.5 * eps, 0.5 * eps class BCEBlurWithLogitsLoss(nn.Module): # BCEwithLogitLoss() with reduced missing label effects. def __init__(self, alpha=0.05): super(BCEBlurWithLogitsLoss, self).__init__() self.loss_fcn = nn.BCEWithLogitsLoss(reduction='none') # must be nn.BCEWithLogitsLoss() self.alpha = alpha def forward(self, pred, true): loss = self.loss_fcn(pred, true) pred = torch.sigmoid(pred) # prob from logits dx = pred - true # reduce only missing label effects # dx = (pred - true).abs() # reduce missing label and false label effects alpha_factor = 1 - torch.exp((dx - 1) / (self.alpha + 1e-4)) loss *= alpha_factor return loss.mean() class FocalLoss(nn.Module): # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5) def __init__(self, loss_fcn, gamma=1.5, alpha=0.25): super(FocalLoss, self).__init__() self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss() self.gamma = gamma self.alpha = alpha self.reduction = loss_fcn.reduction self.loss_fcn.reduction = 'none' # required to apply FL to each element def forward(self, pred, true): loss = self.loss_fcn(pred, true) # p_t = torch.exp(-loss) # loss *= self.alpha * (1.000001 - p_t) ** self.gamma # non-zero power for gradient stability # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py pred_prob = torch.sigmoid(pred) # prob from logits p_t = true * pred_prob + (1 - true) * (1 - pred_prob) alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha) modulating_factor = (1.0 - p_t) ** self.gamma loss *= alpha_factor * modulating_factor if self.reduction == 'mean': return loss.mean() elif self.reduction == 'sum': return loss.sum() else: # 'none' return loss def compute_loss(p, targets, model): # predictions, targets, model device = targets.device #print(device) lcls, lbox, lobj = torch.zeros(1, device=device), torch.zeros(1, device=device), torch.zeros(1, device=device) tcls, tbox, indices, anchors = build_targets(p, targets, model) # targets h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.Tensor([h['cls_pw']])).to(device) BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.Tensor([h['obj_pw']])).to(device) # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 cp, cn = smooth_BCE(eps=0.0) # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g) # Losses nt = 0 # number of targets no = len(p) # number of outputs balance = [4.0, 1.0, 0.4] if no == 3 else [4.0, 1.0, 0.4, 0.1] # P3-5 or P3-6 balance = [4.0, 1.0, 0.5, 0.4, 0.1] if no == 5 else balance for i, pi in enumerate(p): # layer index, layer predictions b, a, gj, gi = indices[i] # image, anchor, gridy, gridx tobj = torch.zeros_like(pi[..., 0], device=device) # target obj n = b.shape[0] # number of targets if n: nt += n # cumulative targets ps = pi[b, a, gj, gi] # prediction subset corresponding to targets # Regression pxy = ps[:, :2].sigmoid() * 2. - 0.5 pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i] pbox = torch.cat((pxy, pwh), 1).to(device) # predicted box iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True) # iou(prediction, target) lbox += (1.0 - iou).mean() # iou loss # Objectness tobj[b, a, gj, gi] = (1.0 - model.gr) + model.gr * iou.detach().clamp(0).type(tobj.dtype) # iou ratio # Classification if model.nc > 1: # cls loss (only if multiple classes) t = torch.full_like(ps[:, 5:], cn, device=device) # targets t[range(n), tcls[i]] = cp lcls += BCEcls(ps[:, 5:], t) # BCE # Append targets to text file # with open('targets.txt', 'a') as file: # [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)] lobj += BCEobj(pi[..., 4], tobj) * balance[i] # obj loss s = 3 / no # output count scaling lbox *= h['box'] * s lobj *= h['obj'] * s * (1.4 if no >= 4 else 1.) lcls *= h['cls'] * s bs = tobj.shape[0] # batch size loss = lbox + lobj + lcls return loss * bs, torch.cat((lbox, lobj, lcls, loss)).detach() def build_targets(p, targets, model): nt = targets.shape[0] # number of anchors, targets tcls, tbox, indices, anch = [], [], [], [] gain = torch.ones(6, device=targets.device) # normalized to gridspace gain off = torch.tensor([[1, 0], [0, 1], [-1, 0], [0, -1]], device=targets.device).float() # overlap offsets g = 0.5 # offset multi_gpu = is_parallel(model) for i, jj in enumerate(model.module.yolo_layers if multi_gpu else model.yolo_layers): # get number of grid points and anchor vec for this yolo layer anchors = model.module.module_list[jj].anchor_vec if multi_gpu else model.module_list[jj].anchor_vec gain[2:] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain # Match targets to anchors a, t, offsets = [], targets * gain, 0 if nt: na = anchors.shape[0] # number of anchors at = torch.arange(na).view(na, 1).repeat(1, nt) # anchor tensor, same as .repeat_interleave(nt) r = t[None, :, 4:6] / anchors[:, None] # wh ratio j = torch.max(r, 1. / r).max(2)[0] < model.hyp['anchor_t'] # compare # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n) = wh_iou(anchors(3,2), gwh(n,2)) a, t = at[j], t.repeat(na, 1, 1)[j] # filter # overlaps gxy = t[:, 2:4] # grid xy z = torch.zeros_like(gxy) j, k = ((gxy % 1. < g) & (gxy > 1.)).T l, m = ((gxy % 1. > (1 - g)) & (gxy < (gain[[2, 3]] - 1.))).T a, t = torch.cat((a, a[j], a[k], a[l], a[m]), 0), torch.cat((t, t[j], t[k], t[l], t[m]), 0) offsets = torch.cat((z, z[j] + off[0], z[k] + off[1], z[l] + off[2], z[m] + off[3]), 0) * g # Define b, c = t[:, :2].long().T # image, class gxy = t[:, 2:4] # grid xy gwh = t[:, 4:6] # grid wh gij = (gxy - offsets).long() gi, gj = gij.T # grid xy indices # Append #indices.append((b, a, gj, gi)) # image, anchor, grid indices indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices tbox.append(torch.cat((gxy - gij, gwh), 1)) # box anch.append(anchors[a]) # anchors tcls.append(c) # class return tcls, tbox, indices, anch ================================================ FILE: asone/detectors/yolor/utils/metrics.py ================================================ # Model validation metrics import matplotlib.pyplot as plt import numpy as np def fitness(x): # Model fitness as a weighted combination of metrics w = [0.0, 0.0, 0.1, 0.9] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return (x[:, :4] * w).sum(1) def fitness_p(x): # Model fitness as a weighted combination of metrics w = [1.0, 0.0, 0.0, 0.0] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return (x[:, :4] * w).sum(1) def fitness_r(x): # Model fitness as a weighted combination of metrics w = [0.0, 1.0, 0.0, 0.0] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return (x[:, :4] * w).sum(1) def fitness_ap50(x): # Model fitness as a weighted combination of metrics w = [0.0, 0.0, 1.0, 0.0] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return (x[:, :4] * w).sum(1) def fitness_ap(x): # Model fitness as a weighted combination of metrics w = [0.0, 0.0, 0.0, 1.0] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return (x[:, :4] * w).sum(1) def fitness_f(x): # Model fitness as a weighted combination of metrics #w = [0.0, 0.0, 0.0, 1.0] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return ((x[:, 0]*x[:, 1])/(x[:, 0]+x[:, 1])) def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, fname='precision-recall_curve.png'): """ Compute the average precision, given the recall and precision curves. Source: https://github.com/rafaelpadilla/Object-Detection-Metrics. # Arguments tp: True positives (nparray, nx1 or nx10). conf: Objectness value from 0-1 (nparray). pred_cls: Predicted object classes (nparray). target_cls: True object classes (nparray). plot: Plot precision-recall curve at mAP@0.5 fname: Plot filename # Returns The average precision as computed in py-faster-rcnn. """ # Sort by objectness i = np.argsort(-conf) tp, conf, pred_cls = tp[i], conf[i], pred_cls[i] # Find unique classes unique_classes = np.unique(target_cls) # Create Precision-Recall curve and compute AP for each class px, py = np.linspace(0, 1, 1000), [] # for plotting pr_score = 0.1 # score to evaluate P and R https://github.com/ultralytics/yolov3/issues/898 s = [unique_classes.shape[0], tp.shape[1]] # number class, number iou thresholds (i.e. 10 for mAP0.5...0.95) ap, p, r = np.zeros(s), np.zeros(s), np.zeros(s) for ci, c in enumerate(unique_classes): i = pred_cls == c n_l = (target_cls == c).sum() # number of labels n_p = i.sum() # number of predictions if n_p == 0 or n_l == 0: continue else: # Accumulate FPs and TPs fpc = (1 - tp[i]).cumsum(0) tpc = tp[i].cumsum(0) # Recall recall = tpc / (n_l + 1e-16) # recall curve r[ci] = np.interp(-pr_score, -conf[i], recall[:, 0]) # r at pr_score, negative x, xp because xp decreases # Precision precision = tpc / (tpc + fpc) # precision curve p[ci] = np.interp(-pr_score, -conf[i], precision[:, 0]) # p at pr_score # AP from recall-precision curve for j in range(tp.shape[1]): ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j]) if j == 0: py.append(np.interp(px, mrec, mpre)) # precision at mAP@0.5 # Compute F1 score (harmonic mean of precision and recall) f1 = 2 * p * r / (p + r + 1e-16) if plot: py = np.stack(py, axis=1) fig, ax = plt.subplots(1, 1, figsize=(5, 5)) ax.plot(px, py, linewidth=0.5, color='grey') # plot(recall, precision) ax.plot(px, py.mean(1), linewidth=2, color='blue', label='all classes %.3f mAP@0.5' % ap[:, 0].mean()) ax.set_xlabel('Recall') ax.set_ylabel('Precision') ax.set_xlim(0, 1) ax.set_ylim(0, 1) plt.legend() fig.tight_layout() fig.savefig(fname, dpi=200) return p, r, ap, f1, unique_classes.astype('int32') def compute_ap(recall, precision): """ Compute the average precision, given the recall and precision curves. Source: https://github.com/rbgirshick/py-faster-rcnn. # Arguments recall: The recall curve (list). precision: The precision curve (list). # Returns The average precision as computed in py-faster-rcnn. """ # Append sentinel values to beginning and end mrec = np.concatenate(([0.0], recall, [1.0])) mpre = np.concatenate(([1.0], precision, [0.0])) # Compute the precision envelope mpre = np.flip(np.maximum.accumulate(np.flip(mpre))) # Integrate area under curve method = 'interp' # methods: 'continuous', 'interp' if method == 'interp': x = np.linspace(0, 1, 101) # 101-point interp (COCO) ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate else: # 'continuous' i = np.where(mrec[1:] != mrec[:-1])[0] # points where x axis (recall) changes ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve return ap, mpre, mrec ================================================ FILE: asone/detectors/yolor/utils/parse_config.py ================================================ import os import numpy as np def parse_model_cfg(path): # Parse the yolo *.cfg file and return module definitions path may be 'cfg/yolov3.cfg', 'yolov3.cfg', or 'yolov3' if not path.endswith('.cfg'): # add .cfg suffix if omitted path += '.cfg' if not os.path.exists(path) and os.path.exists('cfg' + os.sep + path): # add cfg/ prefix if omitted path = 'cfg' + os.sep + path with open(path, 'r') as f: lines = f.read().split('\n') lines = [x for x in lines if x and not x.startswith('#')] lines = [x.rstrip().lstrip() for x in lines] # get rid of fringe whitespaces mdefs = [] # module definitions for line in lines: if line.startswith('['): # This marks the start of a new block mdefs.append({}) mdefs[-1]['type'] = line[1:-1].rstrip() if mdefs[-1]['type'] == 'convolutional': mdefs[-1]['batch_normalize'] = 0 # pre-populate with zeros (may be overwritten later) else: key, val = line.split("=") key = key.rstrip() if key == 'anchors': # return nparray mdefs[-1][key] = np.array([float(x) for x in val.split(',')]).reshape((-1, 2)) # np anchors elif (key in ['from', 'layers', 'mask']) or (key == 'size' and ',' in val): # return array mdefs[-1][key] = [int(x) for x in val.split(',')] else: val = val.strip() if val.isnumeric(): # return int or float mdefs[-1][key] = int(val) if (int(val) - float(val)) == 0 else float(val) else: mdefs[-1][key] = val # return string # Check all fields are supported supported = ['type', 'batch_normalize', 'filters', 'size', 'stride', 'pad', 'activation', 'layers', 'groups', 'from', 'mask', 'anchors', 'classes', 'num', 'jitter', 'ignore_thresh', 'truth_thresh', 'random', 'stride_x', 'stride_y', 'weights_type', 'weights_normalization', 'scale_x_y', 'beta_nms', 'nms_kind', 'iou_loss', 'iou_normalizer', 'cls_normalizer', 'iou_thresh', 'atoms', 'na', 'nc'] f = [] # fields for x in mdefs[1:]: [f.append(k) for k in x if k not in f] u = [x for x in f if x not in supported] # unsupported fields assert not any(u), "Unsupported fields %s in %s. See https://github.com/ultralytics/yolov3/issues/631" % (u, path) return mdefs def parse_data_cfg(path): # Parses the data configuration file if not os.path.exists(path) and os.path.exists('data' + os.sep + path): # add data/ prefix if omitted path = 'data' + os.sep + path with open(path, 'r') as f: lines = f.readlines() options = dict() for line in lines: line = line.strip() if line == '' or line.startswith('#'): continue key, val = line.split('=') options[key.strip()] = val.strip() return options ================================================ FILE: asone/detectors/yolor/utils/plots.py ================================================ # Plotting utils import glob import math import os import random from copy import copy from pathlib import Path import cv2 import matplotlib import matplotlib.pyplot as plt import numpy as np import torch import yaml from PIL import Image from scipy.signal import butter, filtfilt from asone.detectors.yolor.utils.general import xywh2xyxy, xyxy2xywh from asone.detectors.yolor.utils.metrics import fitness # Settings matplotlib.use('Agg') # for writing to files only def color_list(): # Return first 10 plt colors as (r,g,b) https://stackoverflow.com/questions/51350872/python-from-color-name-to-rgb def hex2rgb(h): return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4)) return [hex2rgb(h) for h in plt.rcParams['axes.prop_cycle'].by_key()['color']] def hist2d(x, y, n=100): # 2d histogram used in labels.png and evolve.png xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n) hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges)) xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1) yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1) return np.log(hist[xidx, yidx]) def butter_lowpass_filtfilt(data, cutoff=1500, fs=50000, order=5): # https://stackoverflow.com/questions/28536191/how-to-filter-smooth-with-scipy-numpy def butter_lowpass(cutoff, fs, order): nyq = 0.5 * fs normal_cutoff = cutoff / nyq return butter(order, normal_cutoff, btype='low', analog=False) b, a = butter_lowpass(cutoff, fs, order=order) return filtfilt(b, a, data) # forward-backward filter def plot_one_box(x, img, color=None, label=None, line_thickness=None): # Plots one bounding box on image img tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness color = color or [random.randint(0, 255) for _ in range(3)] c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) if label: tf = max(tl - 1, 1) # font thickness t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) def plot_wh_methods(): # from utils.general import *; plot_wh_methods() # Compares the two methods for width-height anchor multiplication # https://github.com/ultralytics/yolov3/issues/168 x = np.arange(-4.0, 4.0, .1) ya = np.exp(x) yb = torch.sigmoid(torch.from_numpy(x)).numpy() * 2 fig = plt.figure(figsize=(6, 3), dpi=150) plt.plot(x, ya, '.-', label='YOLO') plt.plot(x, yb ** 2, '.-', label='YOLO ^2') plt.plot(x, yb ** 1.6, '.-', label='YOLO ^1.6') plt.xlim(left=-4, right=4) plt.ylim(bottom=0, top=6) plt.xlabel('input') plt.ylabel('output') plt.grid() plt.legend() fig.tight_layout() fig.savefig('comparison.png', dpi=200) def output_to_target(output, width, height): # Convert model output to target format [batch_id, class_id, x, y, w, h, conf] if isinstance(output, torch.Tensor): output = output.cpu().numpy() targets = [] for i, o in enumerate(output): if o is not None: for pred in o: box = pred[:4] w = (box[2] - box[0]) / width h = (box[3] - box[1]) / height x = box[0] / width + w / 2 y = box[1] / height + h / 2 conf = pred[4] cls = int(pred[5]) targets.append([i, cls, x, y, w, h, conf]) return np.array(targets) def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max_size=640, max_subplots=16): # Plot image grid with labels if isinstance(images, torch.Tensor): images = images.cpu().float().numpy() if isinstance(targets, torch.Tensor): targets = targets.cpu().numpy() # un-normalise if np.max(images[0]) <= 1: images *= 255 tl = 3 # line thickness tf = max(tl - 1, 1) # font thickness bs, _, h, w = images.shape # batch size, _, height, width bs = min(bs, max_subplots) # limit plot images ns = np.ceil(bs ** 0.5) # number of subplots (square) # Check if we should resize scale_factor = max_size / max(h, w) if scale_factor < 1: h = math.ceil(scale_factor * h) w = math.ceil(scale_factor * w) colors = color_list() # list of colors mosaic = np.full((int(ns * h), int(ns * w), 3), 255, dtype=np.uint8) # init for i, img in enumerate(images): if i == max_subplots: # if last batch has fewer images than we expect break block_x = int(w * (i // ns)) block_y = int(h * (i % ns)) img = img.transpose(1, 2, 0) if scale_factor < 1: img = cv2.resize(img, (w, h)) mosaic[block_y:block_y + h, block_x:block_x + w, :] = img if len(targets) > 0: image_targets = targets[targets[:, 0] == i] boxes = xywh2xyxy(image_targets[:, 2:6]).T classes = image_targets[:, 1].astype('int') labels = image_targets.shape[1] == 6 # labels if no conf column conf = None if labels else image_targets[:, 6] # check for confidence presence (label vs pred) boxes[[0, 2]] *= w boxes[[0, 2]] += block_x boxes[[1, 3]] *= h boxes[[1, 3]] += block_y for j, box in enumerate(boxes.T): cls = int(classes[j]) color = colors[cls % len(colors)] cls = names[cls] if names else cls if labels or conf[j] > 0.25: # 0.25 conf thresh label = '%s' % cls if labels else '%s %.1f' % (cls, conf[j]) plot_one_box(box, mosaic, label=label, color=color, line_thickness=tl) # Draw image filename labels if paths: label = Path(paths[i]).name[:40] # trim to 40 char t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] cv2.putText(mosaic, label, (block_x + 5, block_y + t_size[1] + 5), 0, tl / 3, [220, 220, 220], thickness=tf, lineType=cv2.LINE_AA) # Image border cv2.rectangle(mosaic, (block_x, block_y), (block_x + w, block_y + h), (255, 255, 255), thickness=3) if fname: r = min(1280. / max(h, w) / ns, 1.0) # ratio to limit image size mosaic = cv2.resize(mosaic, (int(ns * w * r), int(ns * h * r)), interpolation=cv2.INTER_AREA) # cv2.imwrite(fname, cv2.cvtColor(mosaic, cv2.COLOR_BGR2RGB)) # cv2 save Image.fromarray(mosaic).save(fname) # PIL save return mosaic def plot_lr_scheduler(optimizer, scheduler, epochs=300, save_dir=''): # Plot LR simulating training for full epochs optimizer, scheduler = copy(optimizer), copy(scheduler) # do not modify originals y = [] for _ in range(epochs): scheduler.step() y.append(optimizer.param_groups[0]['lr']) plt.plot(y, '.-', label='LR') plt.xlabel('epoch') plt.ylabel('LR') plt.grid() plt.xlim(0, epochs) plt.ylim(0) plt.tight_layout() plt.savefig(Path(save_dir) / 'LR.png', dpi=200) def plot_test_txt(): # from utils.general import *; plot_test() # Plot test.txt histograms x = np.loadtxt('test.txt', dtype=np.float32) box = xyxy2xywh(x[:, :4]) cx, cy = box[:, 0], box[:, 1] fig, ax = plt.subplots(1, 1, figsize=(6, 6), tight_layout=True) ax.hist2d(cx, cy, bins=600, cmax=10, cmin=0) ax.set_aspect('equal') plt.savefig('hist2d.png', dpi=300) fig, ax = plt.subplots(1, 2, figsize=(12, 6), tight_layout=True) ax[0].hist(cx, bins=600) ax[1].hist(cy, bins=600) plt.savefig('hist1d.png', dpi=200) def plot_targets_txt(): # from utils.general import *; plot_targets_txt() # Plot targets.txt histograms x = np.loadtxt('targets.txt', dtype=np.float32).T s = ['x targets', 'y targets', 'width targets', 'height targets'] fig, ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True) ax = ax.ravel() for i in range(4): ax[i].hist(x[i], bins=100, label='%.3g +/- %.3g' % (x[i].mean(), x[i].std())) ax[i].legend() ax[i].set_title(s[i]) plt.savefig('targets.jpg', dpi=200) def plot_study_txt(f='study.txt', x=None): # from utils.general import *; plot_study_txt() # Plot study.txt generated by test.py fig, ax = plt.subplots(2, 4, figsize=(10, 6), tight_layout=True) ax = ax.ravel() fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4), tight_layout=True) for f in ['study/study_coco_yolo%s.txt' % x for x in ['s', 'm', 'l', 'x']]: y = np.loadtxt(f, dtype=np.float32, usecols=[0, 1, 2, 3, 7, 8, 9], ndmin=2).T x = np.arange(y.shape[1]) if x is None else np.array(x) s = ['P', 'R', 'mAP@.5', 'mAP@.5:.95', 't_inference (ms/img)', 't_NMS (ms/img)', 't_total (ms/img)'] for i in range(7): ax[i].plot(x, y[i], '.-', linewidth=2, markersize=8) ax[i].set_title(s[i]) j = y[3].argmax() + 1 ax2.plot(y[6, :j], y[3, :j] * 1E2, '.-', linewidth=2, markersize=8, label=Path(f).stem.replace('study_coco_', '').replace('yolo', 'YOLO')) ax2.plot(1E3 / np.array([209, 140, 97, 58, 35, 18]), [34.6, 40.5, 43.0, 47.5, 49.7, 51.5], 'k.-', linewidth=2, markersize=8, alpha=.25, label='EfficientDet') ax2.grid() ax2.set_xlim(0, 30) ax2.set_ylim(28, 50) ax2.set_yticks(np.arange(30, 55, 5)) ax2.set_xlabel('GPU Speed (ms/img)') ax2.set_ylabel('COCO AP val') ax2.legend(loc='lower right') plt.savefig('study_mAP_latency.png', dpi=300) plt.savefig(f.replace('.txt', '.png'), dpi=300) def plot_labels(labels, save_dir=''): # plot dataset labels c, b = labels[:, 0], labels[:, 1:].transpose() # classes, boxes nc = int(c.max() + 1) # number of classes fig, ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True) ax = ax.ravel() ax[0].hist(c, bins=np.linspace(0, nc, nc + 1) - 0.5, rwidth=0.8) ax[0].set_xlabel('classes') ax[1].scatter(b[0], b[1], c=hist2d(b[0], b[1], 90), cmap='jet') ax[1].set_xlabel('x') ax[1].set_ylabel('y') ax[2].scatter(b[2], b[3], c=hist2d(b[2], b[3], 90), cmap='jet') ax[2].set_xlabel('width') ax[2].set_ylabel('height') plt.savefig(Path(save_dir) / 'labels.png', dpi=200) plt.close() # seaborn correlogram try: import seaborn as sns import pandas as pd x = pd.DataFrame(b.transpose(), columns=['x', 'y', 'width', 'height']) sns.pairplot(x, corner=True, diag_kind='hist', kind='scatter', markers='o', plot_kws=dict(s=3, edgecolor=None, linewidth=1, alpha=0.02), diag_kws=dict(bins=50)) plt.savefig(Path(save_dir) / 'labels_correlogram.png', dpi=200) plt.close() except Exception as e: pass def plot_evolution(yaml_file='data/hyp.finetune.yaml'): # from utils.general import *; plot_evolution() # Plot hyperparameter evolution results in evolve.txt with open(yaml_file) as f: hyp = yaml.load(f, Loader=yaml.FullLoader) x = np.loadtxt('evolve.txt', ndmin=2) f = fitness(x) # weights = (f - f.min()) ** 2 # for weighted results plt.figure(figsize=(10, 12), tight_layout=True) matplotlib.rc('font', **{'size': 8}) for i, (k, v) in enumerate(hyp.items()): y = x[:, i + 7] # mu = (y * weights).sum() / weights.sum() # best weighted result mu = y[f.argmax()] # best single result plt.subplot(6, 5, i + 1) plt.scatter(y, f, c=hist2d(y, f, 20), cmap='viridis', alpha=.8, edgecolors='none') plt.plot(mu, f.max(), 'k+', markersize=15) plt.title('%s = %.3g' % (k, mu), fontdict={'size': 9}) # limit to 40 characters if i % 5 != 0: plt.yticks([]) print('%15s: %.3g' % (k, mu)) plt.savefig('evolve.png', dpi=200) print('\nPlot saved as evolve.png') def plot_results_overlay(start=0, stop=0): # from utils.general import *; plot_results_overlay() # Plot training 'results*.txt', overlaying train and val losses s = ['train', 'train', 'train', 'Precision', 'mAP@0.5', 'val', 'val', 'val', 'Recall', 'mAP@0.5:0.95'] # legends t = ['Box', 'Objectness', 'Classification', 'P-R', 'mAP-F1'] # titles for f in sorted(glob.glob('results*.txt') + glob.glob('../../Downloads/results*.txt')): results = np.loadtxt(f, usecols=[2, 3, 4, 8, 9, 12, 13, 14, 10, 11], ndmin=2).T n = results.shape[1] # number of rows x = range(start, min(stop, n) if stop else n) fig, ax = plt.subplots(1, 5, figsize=(14, 3.5), tight_layout=True) ax = ax.ravel() for i in range(5): for j in [i, i + 5]: y = results[j, x] ax[i].plot(x, y, marker='.', label=s[j]) # y_smooth = butter_lowpass_filtfilt(y) # ax[i].plot(x, np.gradient(y_smooth), marker='.', label=s[j]) ax[i].set_title(t[i]) ax[i].legend() ax[i].set_ylabel(f) if i == 0 else None # add filename fig.savefig(f.replace('.txt', '.png'), dpi=200) def plot_results(start=0, stop=0, bucket='', id=(), labels=(), save_dir=''): # from utils.general import *; plot_results(save_dir='runs/train/exp0') # Plot training 'results*.txt' fig, ax = plt.subplots(2, 5, figsize=(12, 6)) ax = ax.ravel() s = ['Box', 'Objectness', 'Classification', 'Precision', 'Recall', 'val Box', 'val Objectness', 'val Classification', 'mAP@0.5', 'mAP@0.5:0.95'] if bucket: # os.system('rm -rf storage.googleapis.com') # files = ['https://storage.googleapis.com/%s/results%g.txt' % (bucket, x) for x in id] files = ['%g.txt' % x for x in id] c = ('gsutil cp ' + '%s ' * len(files) + '.') % tuple('gs://%s/%g.txt' % (bucket, x) for x in id) os.system(c) else: files = glob.glob(str(Path(save_dir) / '*.txt')) + glob.glob('../../Downloads/results*.txt') assert len(files), 'No results.txt files found in %s, nothing to plot.' % os.path.abspath(save_dir) for fi, f in enumerate(files): try: results = np.loadtxt(f, usecols=[2, 3, 4, 8, 9, 12, 13, 14, 10, 11], ndmin=2).T n = results.shape[1] # number of rows x = range(start, min(stop, n) if stop else n) for i in range(10): y = results[i, x] if i in [0, 1, 2, 5, 6, 7]: y[y == 0] = np.nan # don't show zero loss values # y /= y[0] # normalize label = labels[fi] if len(labels) else Path(f).stem ax[i].plot(x, y, marker='.', label=label, linewidth=1, markersize=6) ax[i].set_title(s[i]) # if i in [5, 6, 7]: # share train and val loss y axes # ax[i].get_shared_y_axes().join(ax[i], ax[i - 5]) except Exception as e: print('Warning: Plotting error for %s; %s' % (f, e)) fig.tight_layout() ax[1].legend() fig.savefig(Path(save_dir) / 'results.png', dpi=200) ================================================ FILE: asone/detectors/yolor/utils/torch_utils.py ================================================ # PyTorch utils import logging import math import os import time from contextlib import contextmanager from copy import deepcopy import torch import torch.backends.cudnn as cudnn import torch.nn as nn import torch.nn.functional as F import torchvision logger = logging.getLogger(__name__) @contextmanager def torch_distributed_zero_first(local_rank: int): """ Decorator to make all processes in distributed training wait for each local_master to do something. """ if local_rank not in [-1, 0]: torch.distributed.barrier() yield if local_rank == 0: torch.distributed.barrier() def init_torch_seeds(seed=0): # Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html torch.manual_seed(seed) if seed == 0: # slower, more reproducible cudnn.deterministic = True cudnn.benchmark = False else: # faster, less reproducible cudnn.deterministic = False cudnn.benchmark = True def select_device(device='', batch_size=None): # device = 'cpu' or '0' or '0,1,2,3' cpu_request = device.lower() == 'cpu' if device and not cpu_request: # if device requested other than 'cpu' os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable assert torch.cuda.is_available(), 'CUDA unavailable, invalid device %s requested' % device # check availablity cuda = False if cpu_request else torch.cuda.is_available() if cuda: c = 1024 ** 2 # bytes to MB ng = torch.cuda.device_count() if ng > 1 and batch_size: # check that batch_size is compatible with device_count assert batch_size % ng == 0, 'batch-size %g not multiple of GPU count %g' % (batch_size, ng) x = [torch.cuda.get_device_properties(i) for i in range(ng)] s = f'Using torch {torch.__version__} ' for i in range(0, ng): if i == 1: s = ' ' * len(s) logger.info("%sCUDA:%g (%s, %dMB)" % (s, i, x[i].name, x[i].total_memory / c)) else: logger.info(f'Using torch {torch.__version__} CPU') logger.info('') # skip a line return torch.device('cuda:0' if cuda else 'cpu') def time_synchronized(): torch.cuda.synchronize() if torch.cuda.is_available() else None return time.time() def is_parallel(model): return type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel) def intersect_dicts(da, db, exclude=()): # Dictionary intersection of matching keys and shapes, omitting 'exclude' keys, using da values return {k: v for k, v in da.items() if k in db and not any(x in k for x in exclude) and v.shape == db[k].shape} def initialize_weights(model): for m in model.modules(): t = type(m) if t is nn.Conv2d: pass # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif t is nn.BatchNorm2d: m.eps = 1e-3 m.momentum = 0.03 elif t in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6]: m.inplace = True def find_modules(model, mclass=nn.Conv2d): # Finds layer indices matching module class 'mclass' return [i for i, m in enumerate(model.module_list) if isinstance(m, mclass)] def sparsity(model): # Return global model sparsity a, b = 0., 0. for p in model.parameters(): a += p.numel() b += (p == 0).sum() return b / a def prune(model, amount=0.3): # Prune model to requested global sparsity import torch.nn.utils.prune as prune print('Pruning model... ', end='') for name, m in model.named_modules(): if isinstance(m, nn.Conv2d): prune.l1_unstructured(m, name='weight', amount=amount) # prune prune.remove(m, 'weight') # make permanent print(' %.3g global sparsity' % sparsity(model)) def fuse_conv_and_bn(conv, bn): # Fuse convolution and batchnorm layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/ fusedconv = nn.Conv2d(conv.in_channels, conv.out_channels, kernel_size=conv.kernel_size, stride=conv.stride, padding=conv.padding, groups=conv.groups, bias=True).requires_grad_(False).to(conv.weight.device) # prepare filters w_conv = conv.weight.clone().view(conv.out_channels, -1) w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.size())) # prepare spatial bias b_conv = torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps)) fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) return fusedconv def model_info(model, verbose=False, img_size=640): # Model information. img_size may be int or list, i.e. img_size=640 or img_size=[640, 320] n_p = sum(x.numel() for x in model.parameters()) # number parameters n_g = sum(x.numel() for x in model.parameters() if x.requires_grad) # number gradients if verbose: print('%5s %40s %9s %12s %20s %10s %10s' % ('layer', 'name', 'gradient', 'parameters', 'shape', 'mu', 'sigma')) for i, (name, p) in enumerate(model.named_parameters()): name = name.replace('module_list.', '') print('%5g %40s %9s %12g %20s %10.3g %10.3g' % (i, name, p.requires_grad, p.numel(), list(p.shape), p.mean(), p.std())) try: # FLOPS from thop import profile flops = profile(deepcopy(model), inputs=(torch.zeros(1, 3, img_size, img_size),), verbose=False)[0] / 1E9 * 2 img_size = img_size if isinstance(img_size, list) else [img_size, img_size] # expand if int/float fs = ', %.9f GFLOPS' % (flops) # 640x640 FLOPS except (ImportError, Exception): fs = '' logger.info(f"Model Summary: {len(list(model.modules()))} layers, {n_p} parameters, {n_g} gradients{fs}") def load_classifier(name='resnet101', n=2): # Loads a pretrained model reshaped to n-class output model = torchvision.models.__dict__[name](pretrained=True) # ResNet model properties # input_size = [3, 224, 224] # input_space = 'RGB' # input_range = [0, 1] # mean = [0.485, 0.456, 0.406] # std = [0.229, 0.224, 0.225] # Reshape output to n classes filters = model.fc.weight.shape[1] model.fc.bias = nn.Parameter(torch.zeros(n), requires_grad=True) model.fc.weight = nn.Parameter(torch.zeros(n, filters), requires_grad=True) model.fc.out_features = n return model def scale_img(img, ratio=1.0, same_shape=False): # img(16,3,256,416), r=ratio # scales img(bs,3,y,x) by ratio if ratio == 1.0: return img else: h, w = img.shape[2:] s = (int(h * ratio), int(w * ratio)) # new size img = F.interpolate(img, size=s, mode='bilinear', align_corners=False) # resize if not same_shape: # pad/crop img gs = 32 # (pixels) grid size h, w = [math.ceil(x * ratio / gs) * gs for x in (h, w)] return F.pad(img, [0, w - s[1], 0, h - s[0]], value=0.447) # value = imagenet mean def copy_attr(a, b, include=(), exclude=()): # Copy attributes from b to a, options to only include [...] and to exclude [...] for k, v in b.__dict__.items(): if (len(include) and k not in include) or k.startswith('_') or k in exclude: continue else: setattr(a, k, v) class ModelEMA: """ Model Exponential Moving Average from https://github.com/rwightman/pytorch-image-models Keep a moving average of everything in the model state_dict (parameters and buffers). This is intended to allow functionality like https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage A smoothed version of the weights is necessary for some training schemes to perform well. This class is sensitive where it is initialized in the sequence of model init, GPU assignment and distributed training wrappers. """ def __init__(self, model, decay=0.9999, updates=0): # Create EMA self.ema = deepcopy(model.module if is_parallel(model) else model).eval() # FP32 EMA # if next(model.parameters()).device.type != 'cpu': # self.ema.half() # FP16 EMA self.updates = updates # number of EMA updates self.decay = lambda x: decay * (1 - math.exp(-x / 2000)) # decay exponential ramp (to help early epochs) for p in self.ema.parameters(): p.requires_grad_(False) def update(self, model): # Update EMA parameters with torch.no_grad(): self.updates += 1 d = self.decay(self.updates) msd = model.module.state_dict() if is_parallel(model) else model.state_dict() # model state_dict for k, v in self.ema.state_dict().items(): if v.dtype.is_floating_point: v *= d v += (1. - d) * msd[k].detach() def update_attr(self, model, include=(), exclude=('process_group', 'reducer')): # Update EMA attributes copy_attr(self.ema, model, include, exclude) ================================================ FILE: asone/detectors/yolor/utils/yolor_utils.py ================================================ import torch import torchvision import time import numpy as np import cv2 class_names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'] # Create a list of colors for each class where each color is a tuple of 3 integer values rng = np.random.default_rng(3) colors = rng.uniform(0, 255, size=(len(class_names), 3)) def box_area(box): # box = xyxy(4,n) return (box[2] - box[0]) * (box[3] - box[1]) def box_iou(box1, box2, eps=1e-7): # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Arguments: box1 (Tensor[N, 4]) box2 (Tensor[M, 4]) Returns: iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 """ # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2) (a1, a2), (b1, b2) = box1[:, None].chunk(2, 2), box2.chunk(2, 1) inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp(0).prod(2) # IoU = inter / (area1 + area2 - inter) return inter / (box_area(box1.T)[:, None] + box_area(box2.T) - inter + eps) def xywh2xyxy(x): # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y return y def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False, labels=(), max_det=300): """Non-Maximum Suppression (NMS) on inference results to reject overlapping bounding boxes Returns: list of detections, on (n,6) tensor per image [xyxy, conf, cls] """ # prediction = torch.Tensor(prediction) bs = prediction.shape[0] # batch size nc = prediction.shape[2] - 5 # number of classes xc = prediction[..., 4] > conf_thres # candidates # Checks assert 0 <= conf_thres <= 1, f'Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0' assert 0 <= iou_thres <= 1, f'Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0' # Settings # min_wh = 2 # (pixels) minimum box width and height max_wh = 7680 # (pixels) maximum box width and height max_nms = 30000 # maximum number of boxes into torchvision.ops.nms() time_limit = 0.3 + 0.03 * bs # seconds to quit after redundant = True # require redundant detections multi_label &= nc > 1 # multiple labels per box (adds 0.5ms/img) merge = False # use merge-NMS t = time.time() output = [torch.zeros((0, 6), device=prediction.device)] * bs for xi, x in enumerate(prediction): # image index, image inference # Apply constraints # x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height x = x[xc[xi]] # confidence # Cat apriori labels if autolabelling if labels and len(labels[xi]): lb = labels[xi] v = torch.zeros((len(lb), nc + 5), device=x.device) v[:, :4] = lb[:, 1:5] # box v[:, 4] = 1.0 # conf v[range(len(lb)), lb[:, 0].long() + 5] = 1.0 # cls x = torch.cat((x, v), 0) # If none remain process next image if not x.shape[0]: continue # Compute conf x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf # Box (center x, center y, width, height) to (x1, y1, x2, y2) # print(type(x)) box = xywh2xyxy(x[:, :4]) # Detections matrix nx6 (xyxy, conf, cls) if multi_label: i, j = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T x = torch.cat((box[i], x[i, j + 5, None], j[:, None].float()), 1) else: # best class only conf, j = x[:, 5:].max(1, keepdim=True) x = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres] # Filter by class if classes is not None: x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)] # Apply finite constraint # if not torch.isfinite(x).all(): # x = x[torch.isfinite(x).all(1)] # Check shape n = x.shape[0] # number of boxes if not n: # no boxes continue elif n > max_nms: # excess boxes x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence # Batched NMS c = x[:, 5:6] * (0 if agnostic else max_wh) # classes boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS if i.shape[0] > max_det: # limit detections i = i[:max_det] if merge and (1 < n < 3E3): # Merge NMS (boxes merged using weighted mean) # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4) iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix weights = iou * scores[None] # box weights x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes if redundant: i = i[iou.sum(1) > 1] # require redundancy output[xi] = x[i] if (time.time() - t) > time_limit: # LOGGER.warning(f'WARNING: NMS time limit {time_limit:.3f}s exceeded') break # time limit exceeded return output def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32): # Resize and pad image while meeting stride-multiple constraints shape = im.shape[:2] # current shape [height, width] if isinstance(new_shape, int): new_shape = (new_shape, new_shape) # Scale ratio (new / old) r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) if not scaleup: # only scale down, do not scale up (for better val mAP) r = min(r, 1.0) # Compute padding ratio = r, r # width, height ratios new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding if auto: # minimum rectangle dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding elif scaleFill: # stretch dw, dh = 0.0, 0.0 new_unpad = (new_shape[1], new_shape[0]) ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios dw /= 2 # divide padding into 2 sides dh /= 2 if shape[::-1] != new_unpad: # resize im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border return im, ratio, (dw, dh) def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None): # Rescale coords (xyxy) from img1_shape to img0_shape if ratio_pad is None: # calculate from img0_shape gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding else: gain = ratio_pad[0][0] pad = ratio_pad[1] coords[:, [0, 2]] -= pad[0] # x padding coords[:, [1, 3]] -= pad[1] # y padding coords[:, :4] /= gain clip_coords(coords, img0_shape) return coords def clip_coords(boxes, shape): # Clip bounding xyxy bounding boxes to image shape (height, width) if isinstance(boxes, torch.Tensor): # faster individually boxes[:, 0].clamp_(0, shape[1]) # x1 boxes[:, 1].clamp_(0, shape[0]) # y1 boxes[:, 2].clamp_(0, shape[1]) # x2 boxes[:, 3].clamp_(0, shape[0]) # y2 else: # np.array (faster grouped) boxes[:, [0, 2]] = boxes[:, [0, 2]].clip(0, shape[1]) # x1, x2 boxes[:, [1, 3]] = boxes[:, [1, 3]].clip(0, shape[0]) # y1, y2 ================================================ FILE: asone/detectors/yolor/yolor_detector.py ================================================ import os from asone.utils import get_names import numpy as np import warnings import torch import onnxruntime from asone.detectors.yolor.models.models import * from asone import utils from asone.detectors.yolor.utils.yolor_utils import (non_max_suppression, scale_coords, letterbox) from asone.utils.utils import PathResolver class YOLOrDetector: def __init__(self, weights=None, cfg=None, use_onnx=True, use_cuda=True, ): self.use_onnx = use_onnx self.device = 'cuda' if use_cuda else 'cpu' if not os.path.exists(weights): utils.download_weights(weights) if cfg == None: cfg = os.path.join("cfg", "yolor_p6.cfg") # If incase weighst is a list of paths then select path at first index weights = str(weights[0] if isinstance(weights, list) else weights) with PathResolver(): # Load Model self.model = self.load_model(use_cuda, weights, cfg=cfg, img_size=640) def load_model(self, use_cuda, weights, cfg, img_size, fp16=False): # Device: CUDA and if fp16=True only then half precision floating point works self.fp16 = fp16 & ( (not self.use_onnx or self.use_onnx) and self.device != 'cpu') # Load onnx if self.use_onnx: if use_cuda: providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] else: providers = ['CPUExecutionProvider'] model = onnxruntime.InferenceSession(weights, providers=providers) # Load Pytorch else: model = Darknet(cfg, img_size).to(self.device) model.load_state_dict(torch.load( weights, map_location=self.device)['model']) model.to(self.device).eval() model.half() if self.fp16 else model.float() return model def image_preprocessing(self, image: list, input_shape=(640, 640)) -> list: original_image = image.copy() image = letterbox(image, input_shape, stride=32, auto=False)[0] image = image.transpose((2, 0, 1))[::-1] image = np.ascontiguousarray(image, dtype=np.float32) image /= 255 # 0 - 255 to 0.0 - 1.0 if len(image.shape) == 3: image = image[None] # expand for batch dim return original_image, image def detect(self, image: list, input_shape: tuple = (640, 640), conf_thres: float = 0.25, iou_thres: float = 0.45, max_det: int = 1000, filter_classes: bool = None, agnostic_nms: bool = True, with_p6: bool = False, return_image=False) -> list: # Image Preprocessing original_image, processed_image = self.image_preprocessing( image, input_shape) # Inference if self.use_onnx: # Input names of ONNX model on which it is exported input_name = self.model.get_inputs()[0].name # Run onnx model pred = self.model.run([self.model.get_outputs()[0].name], { input_name: processed_image})[0] # Run Pytorch model else: processed_image = torch.from_numpy(processed_image).to(self.device) # Change image floating point precision if fp16 set to true processed_image = processed_image.half() if self.fp16 else processed_image.float() pred = self.model(processed_image, augment=False)[0] pred = pred.detach().cpu().numpy() if isinstance(pred, np.ndarray): pred = torch.tensor(pred, device=self.device) predictions = non_max_suppression( pred, conf_thres, iou_thres, agnostic=agnostic_nms, max_det=max_det) for i, prediction in enumerate(predictions): # per image if len(prediction): prediction[:, :4] = scale_coords( processed_image.shape[2:], prediction[:, :4], original_image.shape).round() predictions[i] = prediction predictions = predictions[0].cpu().numpy() image_info = { 'width': original_image.shape[1], 'height': original_image.shape[0], } self.boxes = predictions[:, :4] self.scores = predictions[:, 4:5] self.class_ids = predictions[:, 5:6] if filter_classes: class_names = get_names() filter_class_idx = [] if filter_classes: for _class in filter_classes: if _class.lower() in class_names: filter_class_idx.append( class_names.index(_class.lower())) else: warnings.warn( f"class {_class} not found in model classes list.") # detection = detection[np.in1d( # detection[:, 5].astype(int), filter_class_idx)] if return_image: return predictions, original_image else: return predictions, image_info ================================================ FILE: asone/detectors/yolov5/__init__.py ================================================ from .yolov5_detector import YOLOv5Detector __all__ = ['YOLOv5Detector'] ================================================ FILE: asone/detectors/yolov5/yolov5/__init__.py ================================================ ================================================ FILE: asone/detectors/yolov5/yolov5/export.py ================================================ # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license """ Export a YOLOv5 PyTorch model to other formats. TensorFlow exports authored by https://github.com/zldrobit Format | `export.py --include` | Model --- | --- | --- PyTorch | - | yolov5s.pt TorchScript | `torchscript` | yolov5s.torchscript ONNX | `onnx` | yolov5s.onnx OpenVINO | `openvino` | yolov5s_openvino_model/ TensorRT | `engine` | yolov5s.engine CoreML | `coreml` | yolov5s.mlmodel TensorFlow SavedModel | `saved_model` | yolov5s_saved_model/ TensorFlow GraphDef | `pb` | yolov5s.pb TensorFlow Lite | `tflite` | yolov5s.tflite TensorFlow Edge TPU | `edgetpu` | yolov5s_edgetpu.tflite TensorFlow.js | `tfjs` | yolov5s_web_model/ PaddlePaddle | `paddle` | yolov5s_paddle_model/ Requirements: $ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime openvino-dev tensorflow-cpu # CPU $ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime-gpu openvino-dev tensorflow # GPU Usage: $ python export.py --weights yolov5s.pt --include torchscript onnx openvino engine coreml tflite ... Inference: $ python detect.py --weights yolov5s.pt # PyTorch yolov5s.torchscript # TorchScript yolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnn yolov5s_openvino_model # OpenVINO yolov5s.engine # TensorRT yolov5s.mlmodel # CoreML (macOS-only) yolov5s_saved_model # TensorFlow SavedModel yolov5s.pb # TensorFlow GraphDef yolov5s.tflite # TensorFlow Lite yolov5s_edgetpu.tflite # TensorFlow Edge TPU yolov5s_paddle_model # PaddlePaddle TensorFlow.js: $ cd .. && git clone https://github.com/zldrobit/tfjs-yolov5-example.git && cd tfjs-yolov5-example $ npm install $ ln -s ../../yolov5/yolov5s_web_model public/yolov5s_web_model $ npm start """ import argparse import contextlib import json import os import platform import re import subprocess import sys import time import warnings from pathlib import Path import pandas as pd import torch from torch.utils.mobile_optimizer import optimize_for_mobile FILE = Path(__file__).resolve() ROOT = FILE.parents[0] # YOLOv5 root directory if str(ROOT) not in sys.path: sys.path.append(str(ROOT)) # add ROOT to PATH if platform.system() != "Windows": ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative from asone.detectors.yolov5.yolov5.models.experimental import attempt_load from asone.detectors.yolov5.yolov5.models.yolo import ClassificationModel, Detect, DetectionModel, SegmentationModel from asone.detectors.yolov5.yolov5.utils.dataloaders import LoadImages from asone.detectors.yolov5.yolov5.utils.general import ( LOGGER, Profile, check_dataset, check_img_size, check_requirements, check_version, check_yaml, colorstr, file_size, get_default_args, print_args, url2file, yaml_save, ) from asone.detectors.yolov5.yolov5.utils.torch_utils import select_device, smart_inference_mode MACOS = platform.system() == "Darwin" # macOS environment class iOSModel(torch.nn.Module): def __init__(self, model, im): """Initializes an iOS compatible model with normalization based on image dimensions.""" super().__init__() b, c, h, w = im.shape # batch, channel, height, width self.model = model self.nc = model.nc # number of classes if w == h: self.normalize = 1.0 / w else: self.normalize = torch.tensor([1.0 / w, 1.0 / h, 1.0 / w, 1.0 / h]) # broadcast (slower, smaller) # np = model(im)[0].shape[1] # number of points # self.normalize = torch.tensor([1. / w, 1. / h, 1. / w, 1. / h]).expand(np, 4) # explicit (faster, larger) def forward(self, x): """Runs forward pass on the input tensor, returning class confidences and normalized coordinates.""" xywh, conf, cls = self.model(x)[0].squeeze().split((4, 1, self.nc), 1) return cls * conf, xywh * self.normalize # confidence (3780, 80), coordinates (3780, 4) def export_formats(): """Returns a DataFrame of supported YOLOv5 model export formats and their properties.""" x = [ ["PyTorch", "-", ".pt", True, True], ["TorchScript", "torchscript", ".torchscript", True, True], ["ONNX", "onnx", ".onnx", True, True], ["OpenVINO", "openvino", "_openvino_model", True, False], ["TensorRT", "engine", ".engine", False, True], ["CoreML", "coreml", ".mlmodel", True, False], ["TensorFlow SavedModel", "saved_model", "_saved_model", True, True], ["TensorFlow GraphDef", "pb", ".pb", True, True], ["TensorFlow Lite", "tflite", ".tflite", True, False], ["TensorFlow Edge TPU", "edgetpu", "_edgetpu.tflite", False, False], ["TensorFlow.js", "tfjs", "_web_model", False, False], ["PaddlePaddle", "paddle", "_paddle_model", True, True], ] return pd.DataFrame(x, columns=["Format", "Argument", "Suffix", "CPU", "GPU"]) def try_export(inner_func): """Decorator @try_export for YOLOv5 model export functions that logs success/failure, time taken, and file size.""" inner_args = get_default_args(inner_func) def outer_func(*args, **kwargs): prefix = inner_args["prefix"] try: with Profile() as dt: f, model = inner_func(*args, **kwargs) LOGGER.info(f"{prefix} export success ✅ {dt.t:.1f}s, saved as {f} ({file_size(f):.1f} MB)") return f, model except Exception as e: LOGGER.info(f"{prefix} export failure ❌ {dt.t:.1f}s: {e}") return None, None return outer_func @try_export def export_torchscript(model, im, file, optimize, prefix=colorstr("TorchScript:")): """Exports YOLOv5 model to TorchScript format, optionally optimized for mobile, with image shape and stride metadata. """ LOGGER.info(f"\n{prefix} starting export with torch {torch.__version__}...") f = file.with_suffix(".torchscript") ts = torch.jit.trace(model, im, strict=False) d = {"shape": im.shape, "stride": int(max(model.stride)), "names": model.names} extra_files = {"config.txt": json.dumps(d)} # torch._C.ExtraFilesMap() if optimize: # https://pytorch.org/tutorials/recipes/mobile_interpreter.html optimize_for_mobile(ts)._save_for_lite_interpreter(str(f), _extra_files=extra_files) else: ts.save(str(f), _extra_files=extra_files) return f, None @try_export def export_onnx(model, im, file, opset, dynamic, simplify, prefix=colorstr("ONNX:")): """Exports a YOLOv5 model to ONNX format with dynamic axes and optional simplification.""" check_requirements("onnx>=1.12.0") import onnx LOGGER.info(f"\n{prefix} starting export with onnx {onnx.__version__}...") f = str(file.with_suffix(".onnx")) output_names = ["output0", "output1"] if isinstance(model, SegmentationModel) else ["output0"] if dynamic: dynamic = {"images": {0: "batch", 2: "height", 3: "width"}} # shape(1,3,640,640) if isinstance(model, SegmentationModel): dynamic["output0"] = {0: "batch", 1: "anchors"} # shape(1,25200,85) dynamic["output1"] = {0: "batch", 2: "mask_height", 3: "mask_width"} # shape(1,32,160,160) elif isinstance(model, DetectionModel): dynamic["output0"] = {0: "batch", 1: "anchors"} # shape(1,25200,85) torch.onnx.export( model.cpu() if dynamic else model, # --dynamic only compatible with cpu im.cpu() if dynamic else im, f, verbose=False, opset_version=opset, do_constant_folding=True, # WARNING: DNN inference with torch>=1.12 may require do_constant_folding=False input_names=["images"], output_names=output_names, dynamic_axes=dynamic or None, ) # Checks model_onnx = onnx.load(f) # load onnx model onnx.checker.check_model(model_onnx) # check onnx model # Metadata d = {"stride": int(max(model.stride)), "names": model.names} for k, v in d.items(): meta = model_onnx.metadata_props.add() meta.key, meta.value = k, str(v) onnx.save(model_onnx, f) # Simplify if simplify: try: cuda = torch.cuda.is_available() check_requirements(("onnxruntime-gpu" if cuda else "onnxruntime", "onnx-simplifier>=0.4.1")) import onnxsim LOGGER.info(f"{prefix} simplifying with onnx-simplifier {onnxsim.__version__}...") model_onnx, check = onnxsim.simplify(model_onnx) assert check, "assert check failed" onnx.save(model_onnx, f) except Exception as e: LOGGER.info(f"{prefix} simplifier failure: {e}") return f, model_onnx @try_export def export_openvino(file, metadata, half, int8, data, prefix=colorstr("OpenVINO:")): # YOLOv5 OpenVINO export check_requirements("openvino-dev>=2023.0") # requires openvino-dev: https://pypi.org/project/openvino-dev/ import openvino.runtime as ov # noqa from openvino.tools import mo # noqa LOGGER.info(f"\n{prefix} starting export with openvino {ov.__version__}...") f = str(file).replace(file.suffix, f"_{'int8_' if int8 else ''}openvino_model{os.sep}") f_onnx = file.with_suffix(".onnx") f_ov = str(Path(f) / file.with_suffix(".xml").name) ov_model = mo.convert_model(f_onnx, model_name=file.stem, framework="onnx", compress_to_fp16=half) # export if int8: check_requirements("nncf>=2.5.0") # requires at least version 2.5.0 to use the post-training quantization import nncf import numpy as np from asone.detectors.yolov5.yolov5.utils.dataloaders import create_dataloader def gen_dataloader(yaml_path, task="train", imgsz=640, workers=4): data_yaml = check_yaml(yaml_path) data = check_dataset(data_yaml) dataloader = create_dataloader( data[task], imgsz=imgsz, batch_size=1, stride=32, pad=0.5, single_cls=False, rect=False, workers=workers )[0] return dataloader # noqa: F811 def transform_fn(data_item): """ Quantization transform function. Extracts and preprocess input data from dataloader item for quantization. Parameters: data_item: Tuple with data item produced by DataLoader during iteration Returns: input_tensor: Input data for quantization """ assert data_item[0].dtype == torch.uint8, "input image must be uint8 for the quantization preprocessing" img = data_item[0].numpy().astype(np.float32) # uint8 to fp16/32 img /= 255.0 # 0 - 255 to 0.0 - 1.0 return np.expand_dims(img, 0) if img.ndim == 3 else img ds = gen_dataloader(data) quantization_dataset = nncf.Dataset(ds, transform_fn) ov_model = nncf.quantize(ov_model, quantization_dataset, preset=nncf.QuantizationPreset.MIXED) ov.serialize(ov_model, f_ov) # save yaml_save(Path(f) / file.with_suffix(".yaml").name, metadata) # add metadata.yaml return f, None @try_export def export_paddle(model, im, file, metadata, prefix=colorstr("PaddlePaddle:")): """Exports a YOLOv5 model to PaddlePaddle format using X2Paddle, saving to `save_dir` and adding a metadata.yaml file. """ check_requirements(("paddlepaddle", "x2paddle")) import x2paddle from x2paddle.convert import pytorch2paddle LOGGER.info(f"\n{prefix} starting export with X2Paddle {x2paddle.__version__}...") f = str(file).replace(".pt", f"_paddle_model{os.sep}") pytorch2paddle(module=model, save_dir=f, jit_type="trace", input_examples=[im]) # export yaml_save(Path(f) / file.with_suffix(".yaml").name, metadata) # add metadata.yaml return f, None @try_export def export_coreml(model, im, file, int8, half, nms, prefix=colorstr("CoreML:")): """Exports YOLOv5 model to CoreML format with optional NMS, INT8, and FP16 support; requires coremltools.""" check_requirements("coremltools") import coremltools as ct LOGGER.info(f"\n{prefix} starting export with coremltools {ct.__version__}...") f = file.with_suffix(".mlmodel") if nms: model = iOSModel(model, im) ts = torch.jit.trace(model, im, strict=False) # TorchScript model ct_model = ct.convert(ts, inputs=[ct.ImageType("image", shape=im.shape, scale=1 / 255, bias=[0, 0, 0])]) bits, mode = (8, "kmeans_lut") if int8 else (16, "linear") if half else (32, None) if bits < 32: if MACOS: # quantization only supported on macOS with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) # suppress numpy==1.20 float warning ct_model = ct.models.neural_network.quantization_utils.quantize_weights(ct_model, bits, mode) else: print(f"{prefix} quantization only supported on macOS, skipping...") ct_model.save(f) return f, ct_model @try_export def export_engine(model, im, file, half, dynamic, simplify, workspace=4, verbose=False, prefix=colorstr("TensorRT:")): """ Exports a YOLOv5 model to TensorRT engine format, requiring GPU and TensorRT>=7.0.0. https://developer.nvidia.com/tensorrt """ assert im.device.type != "cpu", "export running on CPU but must be on GPU, i.e. `python export.py --device 0`" try: import tensorrt as trt except Exception: if platform.system() == "Linux": check_requirements("nvidia-tensorrt", cmds="-U --index-url https://pypi.ngc.nvidia.com") import tensorrt as trt if trt.__version__[0] == "7": # TensorRT 7 handling https://github.com/ultralytics/yolov5/issues/6012 grid = model.model[-1].anchor_grid model.model[-1].anchor_grid = [a[..., :1, :1, :] for a in grid] export_onnx(model, im, file, 12, dynamic, simplify) # opset 12 model.model[-1].anchor_grid = grid else: # TensorRT >= 8 check_version(trt.__version__, "8.0.0", hard=True) # require tensorrt>=8.0.0 export_onnx(model, im, file, 12, dynamic, simplify) # opset 12 onnx = file.with_suffix(".onnx") LOGGER.info(f"\n{prefix} starting export with TensorRT {trt.__version__}...") assert onnx.exists(), f"failed to export ONNX file: {onnx}" f = file.with_suffix(".engine") # TensorRT engine file logger = trt.Logger(trt.Logger.INFO) if verbose: logger.min_severity = trt.Logger.Severity.VERBOSE builder = trt.Builder(logger) config = builder.create_builder_config() config.max_workspace_size = workspace * 1 << 30 # config.set_memory_pool_limit(trt.MemoryPoolType.WORKSPACE, workspace << 30) # fix TRT 8.4 deprecation notice flag = 1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH) network = builder.create_network(flag) parser = trt.OnnxParser(network, logger) if not parser.parse_from_file(str(onnx)): raise RuntimeError(f"failed to load ONNX file: {onnx}") inputs = [network.get_input(i) for i in range(network.num_inputs)] outputs = [network.get_output(i) for i in range(network.num_outputs)] for inp in inputs: LOGGER.info(f'{prefix} input "{inp.name}" with shape{inp.shape} {inp.dtype}') for out in outputs: LOGGER.info(f'{prefix} output "{out.name}" with shape{out.shape} {out.dtype}') if dynamic: if im.shape[0] <= 1: LOGGER.warning(f"{prefix} WARNING ⚠️ --dynamic model requires maximum --batch-size argument") profile = builder.create_optimization_profile() for inp in inputs: profile.set_shape(inp.name, (1, *im.shape[1:]), (max(1, im.shape[0] // 2), *im.shape[1:]), im.shape) config.add_optimization_profile(profile) LOGGER.info(f"{prefix} building FP{16 if builder.platform_has_fast_fp16 and half else 32} engine as {f}") if builder.platform_has_fast_fp16 and half: config.set_flag(trt.BuilderFlag.FP16) with builder.build_engine(network, config) as engine, open(f, "wb") as t: t.write(engine.serialize()) return f, None @try_export def export_saved_model( model, im, file, dynamic, tf_nms=False, agnostic_nms=False, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, keras=False, prefix=colorstr("TensorFlow SavedModel:"), ): # YOLOv5 TensorFlow SavedModel export try: import tensorflow as tf except Exception: check_requirements(f"tensorflow{'' if torch.cuda.is_available() else '-macos' if MACOS else '-cpu'}<=2.15.1") import tensorflow as tf from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2 from asone.detectors.yolov5.yolov5.models.tf import TFModel LOGGER.info(f"\n{prefix} starting export with tensorflow {tf.__version__}...") if tf.__version__ > "2.13.1": helper_url = "https://github.com/ultralytics/yolov5/issues/12489" LOGGER.info( f"WARNING ⚠️ using Tensorflow {tf.__version__} > 2.13.1 might cause issue when exporting the model to tflite {helper_url}" ) # handling issue https://github.com/ultralytics/yolov5/issues/12489 f = str(file).replace(".pt", "_saved_model") batch_size, ch, *imgsz = list(im.shape) # BCHW tf_model = TFModel(cfg=model.yaml, model=model, nc=model.nc, imgsz=imgsz) im = tf.zeros((batch_size, *imgsz, ch)) # BHWC order for TensorFlow _ = tf_model.predict(im, tf_nms, agnostic_nms, topk_per_class, topk_all, iou_thres, conf_thres) inputs = tf.keras.Input(shape=(*imgsz, ch), batch_size=None if dynamic else batch_size) outputs = tf_model.predict(inputs, tf_nms, agnostic_nms, topk_per_class, topk_all, iou_thres, conf_thres) keras_model = tf.keras.Model(inputs=inputs, outputs=outputs) keras_model.trainable = False keras_model.summary() if keras: keras_model.save(f, save_format="tf") else: spec = tf.TensorSpec(keras_model.inputs[0].shape, keras_model.inputs[0].dtype) m = tf.function(lambda x: keras_model(x)) # full model m = m.get_concrete_function(spec) frozen_func = convert_variables_to_constants_v2(m) tfm = tf.Module() tfm.__call__ = tf.function(lambda x: frozen_func(x)[:4] if tf_nms else frozen_func(x), [spec]) tfm.__call__(im) tf.saved_model.save( tfm, f, options=tf.saved_model.SaveOptions(experimental_custom_gradients=False) if check_version(tf.__version__, "2.6") else tf.saved_model.SaveOptions(), ) return f, keras_model @try_export def export_pb(keras_model, file, prefix=colorstr("TensorFlow GraphDef:")): """Exports YOLOv5 model to TensorFlow GraphDef *.pb format; see https://github.com/leimao/Frozen_Graph_TensorFlow for details.""" import tensorflow as tf from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2 LOGGER.info(f"\n{prefix} starting export with tensorflow {tf.__version__}...") f = file.with_suffix(".pb") m = tf.function(lambda x: keras_model(x)) # full model m = m.get_concrete_function(tf.TensorSpec(keras_model.inputs[0].shape, keras_model.inputs[0].dtype)) frozen_func = convert_variables_to_constants_v2(m) frozen_func.graph.as_graph_def() tf.io.write_graph(graph_or_graph_def=frozen_func.graph, logdir=str(f.parent), name=f.name, as_text=False) return f, None @try_export def export_tflite( keras_model, im, file, int8, per_tensor, data, nms, agnostic_nms, prefix=colorstr("TensorFlow Lite:") ): # YOLOv5 TensorFlow Lite export import tensorflow as tf LOGGER.info(f"\n{prefix} starting export with tensorflow {tf.__version__}...") batch_size, ch, *imgsz = list(im.shape) # BCHW f = str(file).replace(".pt", "-fp16.tflite") converter = tf.lite.TFLiteConverter.from_keras_model(keras_model) converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS] converter.target_spec.supported_types = [tf.float16] converter.optimizations = [tf.lite.Optimize.DEFAULT] if int8: from models.tf import representative_dataset_gen dataset = LoadImages(check_dataset(check_yaml(data))["train"], img_size=imgsz, auto=False) converter.representative_dataset = lambda: representative_dataset_gen(dataset, ncalib=100) converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8] converter.target_spec.supported_types = [] converter.inference_input_type = tf.uint8 # or tf.int8 converter.inference_output_type = tf.uint8 # or tf.int8 converter.experimental_new_quantizer = True if per_tensor: converter._experimental_disable_per_channel = True f = str(file).replace(".pt", "-int8.tflite") if nms or agnostic_nms: converter.target_spec.supported_ops.append(tf.lite.OpsSet.SELECT_TF_OPS) tflite_model = converter.convert() open(f, "wb").write(tflite_model) return f, None @try_export def export_edgetpu(file, prefix=colorstr("Edge TPU:")): """ Exports a YOLOv5 model to Edge TPU compatible TFLite format; requires Linux and Edge TPU compiler. https://coral.ai/docs/edgetpu/models-intro/ """ cmd = "edgetpu_compiler --version" help_url = "https://coral.ai/docs/edgetpu/compiler/" assert platform.system() == "Linux", f"export only supported on Linux. See {help_url}" if subprocess.run(f"{cmd} > /dev/null 2>&1", shell=True).returncode != 0: LOGGER.info(f"\n{prefix} export requires Edge TPU compiler. Attempting install from {help_url}") sudo = subprocess.run("sudo --version >/dev/null", shell=True).returncode == 0 # sudo installed on system for c in ( "curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -", 'echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list', "sudo apt-get update", "sudo apt-get install edgetpu-compiler", ): subprocess.run(c if sudo else c.replace("sudo ", ""), shell=True, check=True) ver = subprocess.run(cmd, shell=True, capture_output=True, check=True).stdout.decode().split()[-1] LOGGER.info(f"\n{prefix} starting export with Edge TPU compiler {ver}...") f = str(file).replace(".pt", "-int8_edgetpu.tflite") # Edge TPU model f_tfl = str(file).replace(".pt", "-int8.tflite") # TFLite model subprocess.run( [ "edgetpu_compiler", "-s", "-d", "-k", "10", "--out_dir", str(file.parent), f_tfl, ], check=True, ) return f, None @try_export def export_tfjs(file, int8, prefix=colorstr("TensorFlow.js:")): """Exports a YOLOv5 model to TensorFlow.js format, optionally with uint8 quantization.""" check_requirements("tensorflowjs") import tensorflowjs as tfjs LOGGER.info(f"\n{prefix} starting export with tensorflowjs {tfjs.__version__}...") f = str(file).replace(".pt", "_web_model") # js dir f_pb = file.with_suffix(".pb") # *.pb path f_json = f"{f}/model.json" # *.json path args = [ "tensorflowjs_converter", "--input_format=tf_frozen_model", "--quantize_uint8" if int8 else "", "--output_node_names=Identity,Identity_1,Identity_2,Identity_3", str(f_pb), f, ] subprocess.run([arg for arg in args if arg], check=True) json = Path(f_json).read_text() with open(f_json, "w") as j: # sort JSON Identity_* in ascending order subst = re.sub( r'{"outputs": {"Identity.?.?": {"name": "Identity.?.?"}, ' r'"Identity.?.?": {"name": "Identity.?.?"}, ' r'"Identity.?.?": {"name": "Identity.?.?"}, ' r'"Identity.?.?": {"name": "Identity.?.?"}}}', r'{"outputs": {"Identity": {"name": "Identity"}, ' r'"Identity_1": {"name": "Identity_1"}, ' r'"Identity_2": {"name": "Identity_2"}, ' r'"Identity_3": {"name": "Identity_3"}}}', json, ) j.write(subst) return f, None def add_tflite_metadata(file, metadata, num_outputs): """ Adds TFLite metadata to a model file, supporting multiple outputs, as specified by TensorFlow guidelines. https://www.tensorflow.org/lite/models/convert/metadata """ with contextlib.suppress(ImportError): # check_requirements('tflite_support') from tflite_support import flatbuffers from tflite_support import metadata as _metadata from tflite_support import metadata_schema_py_generated as _metadata_fb tmp_file = Path("/tmp/meta.txt") with open(tmp_file, "w") as meta_f: meta_f.write(str(metadata)) model_meta = _metadata_fb.ModelMetadataT() label_file = _metadata_fb.AssociatedFileT() label_file.name = tmp_file.name model_meta.associatedFiles = [label_file] subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [_metadata_fb.TensorMetadataT()] subgraph.outputTensorMetadata = [_metadata_fb.TensorMetadataT()] * num_outputs model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish(model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) metadata_buf = b.Output() populator = _metadata.MetadataPopulator.with_model_file(file) populator.load_metadata_buffer(metadata_buf) populator.load_associated_files([str(tmp_file)]) populator.populate() tmp_file.unlink() def pipeline_coreml(model, im, file, names, y, prefix=colorstr("CoreML Pipeline:")): """Converts a PyTorch YOLOv5 model to CoreML format with NMS, handling different input/output shapes and saving the model. """ import coremltools as ct from PIL import Image print(f"{prefix} starting pipeline with coremltools {ct.__version__}...") batch_size, ch, h, w = list(im.shape) # BCHW t = time.time() # YOLOv5 Output shapes spec = model.get_spec() out0, out1 = iter(spec.description.output) if platform.system() == "Darwin": img = Image.new("RGB", (w, h)) # img(192 width, 320 height) # img = torch.zeros((*opt.img_size, 3)).numpy() # img size(320,192,3) iDetection out = model.predict({"image": img}) out0_shape, out1_shape = out[out0.name].shape, out[out1.name].shape else: # linux and windows can not run model.predict(), get sizes from pytorch output y s = tuple(y[0].shape) out0_shape, out1_shape = (s[1], s[2] - 5), (s[1], 4) # (3780, 80), (3780, 4) # Checks nx, ny = spec.description.input[0].type.imageType.width, spec.description.input[0].type.imageType.height na, nc = out0_shape # na, nc = out0.type.multiArrayType.shape # number anchors, classes assert len(names) == nc, f"{len(names)} names found for nc={nc}" # check # Define output shapes (missing) out0.type.multiArrayType.shape[:] = out0_shape # (3780, 80) out1.type.multiArrayType.shape[:] = out1_shape # (3780, 4) # spec.neuralNetwork.preprocessing[0].featureName = '0' # Flexible input shapes # from coremltools.models.neural_network import flexible_shape_utils # s = [] # shapes # s.append(flexible_shape_utils.NeuralNetworkImageSize(320, 192)) # s.append(flexible_shape_utils.NeuralNetworkImageSize(640, 384)) # (height, width) # flexible_shape_utils.add_enumerated_image_sizes(spec, feature_name='image', sizes=s) # r = flexible_shape_utils.NeuralNetworkImageSizeRange() # shape ranges # r.add_height_range((192, 640)) # r.add_width_range((192, 640)) # flexible_shape_utils.update_image_size_range(spec, feature_name='image', size_range=r) # Print print(spec.description) # Model from spec model = ct.models.MLModel(spec) # 3. Create NMS protobuf nms_spec = ct.proto.Model_pb2.Model() nms_spec.specificationVersion = 5 for i in range(2): decoder_output = model._spec.description.output[i].SerializeToString() nms_spec.description.input.add() nms_spec.description.input[i].ParseFromString(decoder_output) nms_spec.description.output.add() nms_spec.description.output[i].ParseFromString(decoder_output) nms_spec.description.output[0].name = "confidence" nms_spec.description.output[1].name = "coordinates" output_sizes = [nc, 4] for i in range(2): ma_type = nms_spec.description.output[i].type.multiArrayType ma_type.shapeRange.sizeRanges.add() ma_type.shapeRange.sizeRanges[0].lowerBound = 0 ma_type.shapeRange.sizeRanges[0].upperBound = -1 ma_type.shapeRange.sizeRanges.add() ma_type.shapeRange.sizeRanges[1].lowerBound = output_sizes[i] ma_type.shapeRange.sizeRanges[1].upperBound = output_sizes[i] del ma_type.shape[:] nms = nms_spec.nonMaximumSuppression nms.confidenceInputFeatureName = out0.name # 1x507x80 nms.coordinatesInputFeatureName = out1.name # 1x507x4 nms.confidenceOutputFeatureName = "confidence" nms.coordinatesOutputFeatureName = "coordinates" nms.iouThresholdInputFeatureName = "iouThreshold" nms.confidenceThresholdInputFeatureName = "confidenceThreshold" nms.iouThreshold = 0.45 nms.confidenceThreshold = 0.25 nms.pickTop.perClass = True nms.stringClassLabels.vector.extend(names.values()) nms_model = ct.models.MLModel(nms_spec) # 4. Pipeline models together pipeline = ct.models.pipeline.Pipeline( input_features=[ ("image", ct.models.datatypes.Array(3, ny, nx)), ("iouThreshold", ct.models.datatypes.Double()), ("confidenceThreshold", ct.models.datatypes.Double()), ], output_features=["confidence", "coordinates"], ) pipeline.add_model(model) pipeline.add_model(nms_model) # Correct datatypes pipeline.spec.description.input[0].ParseFromString(model._spec.description.input[0].SerializeToString()) pipeline.spec.description.output[0].ParseFromString(nms_model._spec.description.output[0].SerializeToString()) pipeline.spec.description.output[1].ParseFromString(nms_model._spec.description.output[1].SerializeToString()) # Update metadata pipeline.spec.specificationVersion = 5 pipeline.spec.description.metadata.versionString = "https://github.com/ultralytics/yolov5" pipeline.spec.description.metadata.shortDescription = "https://github.com/ultralytics/yolov5" pipeline.spec.description.metadata.author = "glenn.jocher@ultralytics.com" pipeline.spec.description.metadata.license = "https://github.com/ultralytics/yolov5/blob/master/LICENSE" pipeline.spec.description.metadata.userDefined.update( { "classes": ",".join(names.values()), "iou_threshold": str(nms.iouThreshold), "confidence_threshold": str(nms.confidenceThreshold), } ) # Save the model f = file.with_suffix(".mlmodel") # filename model = ct.models.MLModel(pipeline.spec) model.input_description["image"] = "Input image" model.input_description["iouThreshold"] = f"(optional) IOU Threshold override (default: {nms.iouThreshold})" model.input_description["confidenceThreshold"] = ( f"(optional) Confidence Threshold override (default: {nms.confidenceThreshold})" ) model.output_description["confidence"] = 'Boxes × Class confidence (see user-defined metadata "classes")' model.output_description["coordinates"] = "Boxes × [x, y, width, height] (relative to image size)" model.save(f) # pipelined print(f"{prefix} pipeline success ({time.time() - t:.2f}s), saved as {f} ({file_size(f):.1f} MB)") @smart_inference_mode() def run( data=ROOT / "data/coco128.yaml", # 'dataset.yaml path' weights=ROOT / "yolov5s.pt", # weights path imgsz=(640, 640), # image (height, width) batch_size=1, # batch size device="cpu", # cuda device, i.e. 0 or 0,1,2,3 or cpu include=("torchscript", "onnx"), # include formats half=False, # FP16 half-precision export inplace=False, # set YOLOv5 Detect() inplace=True keras=False, # use Keras optimize=False, # TorchScript: optimize for mobile int8=False, # CoreML/TF INT8 quantization per_tensor=False, # TF per tensor quantization dynamic=False, # ONNX/TF/TensorRT: dynamic axes simplify=False, # ONNX: simplify model opset=12, # ONNX: opset version verbose=False, # TensorRT: verbose log workspace=4, # TensorRT: workspace size (GB) nms=False, # TF: add NMS to model agnostic_nms=False, # TF: add agnostic NMS to model topk_per_class=100, # TF.js NMS: topk per class to keep topk_all=100, # TF.js NMS: topk for all classes to keep iou_thres=0.45, # TF.js NMS: IoU threshold conf_thres=0.25, # TF.js NMS: confidence threshold ): t = time.time() include = [x.lower() for x in include] # to lowercase fmts = tuple(export_formats()["Argument"][1:]) # --include arguments flags = [x in include for x in fmts] assert sum(flags) == len(include), f"ERROR: Invalid --include {include}, valid --include arguments are {fmts}" jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle = flags # export booleans file = Path(url2file(weights) if str(weights).startswith(("http:/", "https:/")) else weights) # PyTorch weights # Load PyTorch model device = select_device(device) if half: assert device.type != "cpu" or coreml, "--half only compatible with GPU export, i.e. use --device 0" assert not dynamic, "--half not compatible with --dynamic, i.e. use either --half or --dynamic but not both" model = attempt_load(weights, device=device, inplace=True, fuse=True) # load FP32 model # Checks imgsz *= 2 if len(imgsz) == 1 else 1 # expand if optimize: assert device.type == "cpu", "--optimize not compatible with cuda devices, i.e. use --device cpu" # Input gs = int(max(model.stride)) # grid size (max stride) imgsz = [check_img_size(x, gs) for x in imgsz] # verify img_size are gs-multiples im = torch.zeros(batch_size, 3, *imgsz).to(device) # image size(1,3,320,192) BCHW iDetection # Update model model.eval() for k, m in model.named_modules(): if isinstance(m, Detect): m.inplace = inplace m.dynamic = dynamic m.export = True for _ in range(2): y = model(im) # dry runs if half and not coreml: im, model = im.half(), model.half() # to FP16 shape = tuple((y[0] if isinstance(y, tuple) else y).shape) # model output shape metadata = {"stride": int(max(model.stride)), "names": model.names} # model metadata LOGGER.info(f"\n{colorstr('PyTorch:')} starting from {file} with output shape {shape} ({file_size(file):.1f} MB)") # Exports f = [""] * len(fmts) # exported filenames warnings.filterwarnings(action="ignore", category=torch.jit.TracerWarning) # suppress TracerWarning if jit: # TorchScript f[0], _ = export_torchscript(model, im, file, optimize) if engine: # TensorRT required before ONNX f[1], _ = export_engine(model, im, file, half, dynamic, simplify, workspace, verbose) if onnx or xml: # OpenVINO requires ONNX f[2], _ = export_onnx(model, im, file, opset, dynamic, simplify) if xml: # OpenVINO f[3], _ = export_openvino(file, metadata, half, int8, data) if coreml: # CoreML f[4], ct_model = export_coreml(model, im, file, int8, half, nms) if nms: pipeline_coreml(ct_model, im, file, model.names, y) if any((saved_model, pb, tflite, edgetpu, tfjs)): # TensorFlow formats assert not tflite or not tfjs, "TFLite and TF.js models must be exported separately, please pass only one type." assert not isinstance(model, ClassificationModel), "ClassificationModel export to TF formats not yet supported." f[5], s_model = export_saved_model( model.cpu(), im, file, dynamic, tf_nms=nms or agnostic_nms or tfjs, agnostic_nms=agnostic_nms or tfjs, topk_per_class=topk_per_class, topk_all=topk_all, iou_thres=iou_thres, conf_thres=conf_thres, keras=keras, ) if pb or tfjs: # pb prerequisite to tfjs f[6], _ = export_pb(s_model, file) if tflite or edgetpu: f[7], _ = export_tflite( s_model, im, file, int8 or edgetpu, per_tensor, data=data, nms=nms, agnostic_nms=agnostic_nms ) if edgetpu: f[8], _ = export_edgetpu(file) add_tflite_metadata(f[8] or f[7], metadata, num_outputs=len(s_model.outputs)) if tfjs: f[9], _ = export_tfjs(file, int8) if paddle: # PaddlePaddle f[10], _ = export_paddle(model, im, file, metadata) # Finish f = [str(x) for x in f if x] # filter out '' and None if any(f): cls, det, seg = (isinstance(model, x) for x in (ClassificationModel, DetectionModel, SegmentationModel)) # type det &= not seg # segmentation models inherit from SegmentationModel(DetectionModel) dir = Path("segment" if seg else "classify" if cls else "") h = "--half" if half else "" # --half FP16 inference arg s = ( "# WARNING ⚠️ ClassificationModel not yet supported for PyTorch Hub AutoShape inference" if cls else "# WARNING ⚠️ SegmentationModel not yet supported for PyTorch Hub AutoShape inference" if seg else "" ) LOGGER.info( f'\nExport complete ({time.time() - t:.1f}s)' f"\nResults saved to {colorstr('bold', file.parent.resolve())}" f"\nDetect: python {dir / ('detect.py' if det else 'predict.py')} --weights {f[-1]} {h}" f"\nValidate: python {dir / 'val.py'} --weights {f[-1]} {h}" f"\nPyTorch Hub: model = torch.hub.load('ultralytics/yolov5', 'custom', '{f[-1]}') {s}" f'\nVisualize: https://netron.app' ) return f # return list of exported files/dirs def parse_opt(known=False): """Parses command-line arguments for YOLOv5 model export configurations, returning the parsed options.""" parser = argparse.ArgumentParser() parser.add_argument("--data", type=str, default=ROOT / "data/coco128.yaml", help="dataset.yaml path") parser.add_argument("--weights", nargs="+", type=str, default=ROOT / "yolov5s.pt", help="model.pt path(s)") parser.add_argument("--imgsz", "--img", "--img-size", nargs="+", type=int, default=[640, 640], help="image (h, w)") parser.add_argument("--batch-size", type=int, default=1, help="batch size") parser.add_argument("--device", default="cpu", help="cuda device, i.e. 0 or 0,1,2,3 or cpu") parser.add_argument("--half", action="store_true", help="FP16 half-precision export") parser.add_argument("--inplace", action="store_true", help="set YOLOv5 Detect() inplace=True") parser.add_argument("--keras", action="store_true", help="TF: use Keras") parser.add_argument("--optimize", action="store_true", help="TorchScript: optimize for mobile") parser.add_argument("--int8", action="store_true", help="CoreML/TF/OpenVINO INT8 quantization") parser.add_argument("--per-tensor", action="store_true", help="TF per-tensor quantization") parser.add_argument("--dynamic", action="store_true", help="ONNX/TF/TensorRT: dynamic axes") parser.add_argument("--simplify", action="store_true", help="ONNX: simplify model") parser.add_argument("--opset", type=int, default=17, help="ONNX: opset version") parser.add_argument("--verbose", action="store_true", help="TensorRT: verbose log") parser.add_argument("--workspace", type=int, default=4, help="TensorRT: workspace size (GB)") parser.add_argument("--nms", action="store_true", help="TF: add NMS to model") parser.add_argument("--agnostic-nms", action="store_true", help="TF: add agnostic NMS to model") parser.add_argument("--topk-per-class", type=int, default=100, help="TF.js NMS: topk per class to keep") parser.add_argument("--topk-all", type=int, default=100, help="TF.js NMS: topk for all classes to keep") parser.add_argument("--iou-thres", type=float, default=0.45, help="TF.js NMS: IoU threshold") parser.add_argument("--conf-thres", type=float, default=0.25, help="TF.js NMS: confidence threshold") parser.add_argument( "--include", nargs="+", default=["torchscript"], help="torchscript, onnx, openvino, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle", ) opt = parser.parse_known_args()[0] if known else parser.parse_args() print_args(vars(opt)) return opt def main(opt): """Executes the YOLOv5 model inference or export with specified weights and options.""" for opt.weights in opt.weights if isinstance(opt.weights, list) else [opt.weights]: run(**vars(opt)) if __name__ == "__main__": opt = parse_opt() main(opt) ================================================ FILE: asone/detectors/yolov5/yolov5/models/__init__.py ================================================ # import os # import sys # sys.path.append(os.path.dirname(__file__)) ================================================ FILE: asone/detectors/yolov5/yolov5/models/common.py ================================================ # YOLOv5 🚀 by Ultralytics, GPL-3.0 license """ Common modules """ import json import math import platform import warnings from collections import OrderedDict, namedtuple from copy import copy from pathlib import Path import cv2 import numpy as np import pandas as pd import requests import torch import torch.nn as nn import yaml from PIL import Image from torch.cuda import amp from asone.detectors.yolov5.yolov5.models.general import (LOGGER, check_requirements, check_suffix, check_version, colorstr, increment_path) def autopad(k, p=None): # kernel, padding # Pad to 'same' if p is None: p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad return p class Conv(nn.Module): # Standard convolution def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super().__init__() self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) self.bn = nn.BatchNorm2d(c2) self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) def forward(self, x): return self.act(self.bn(self.conv(x))) def forward_fuse(self, x): return self.act(self.conv(x)) class DWConv(Conv): # Depth-wise convolution class def __init__(self, c1, c2, k=1, s=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), act=act) class DWConvTranspose2d(nn.ConvTranspose2d): # Depth-wise transpose convolution class def __init__(self, c1, c2, k=1, s=1, p1=0, p2=0): # ch_in, ch_out, kernel, stride, padding, padding_out super().__init__(c1, c2, k, s, p1, p2, groups=math.gcd(c1, c2)) class TransformerLayer(nn.Module): # Transformer layer https://arxiv.org/abs/2010.11929 (LayerNorm layers removed for better performance) def __init__(self, c, num_heads): super().__init__() self.q = nn.Linear(c, c, bias=False) self.k = nn.Linear(c, c, bias=False) self.v = nn.Linear(c, c, bias=False) self.ma = nn.MultiheadAttention(embed_dim=c, num_heads=num_heads) self.fc1 = nn.Linear(c, c, bias=False) self.fc2 = nn.Linear(c, c, bias=False) def forward(self, x): x = self.ma(self.q(x), self.k(x), self.v(x))[0] + x x = self.fc2(self.fc1(x)) + x return x class TransformerBlock(nn.Module): # Vision Transformer https://arxiv.org/abs/2010.11929 def __init__(self, c1, c2, num_heads, num_layers): super().__init__() self.conv = None if c1 != c2: self.conv = Conv(c1, c2) self.linear = nn.Linear(c2, c2) # learnable position embedding self.tr = nn.Sequential(*(TransformerLayer(c2, num_heads) for _ in range(num_layers))) self.c2 = c2 def forward(self, x): if self.conv is not None: x = self.conv(x) b, _, w, h = x.shape p = x.flatten(2).permute(2, 0, 1) return self.tr(p + self.linear(p)).permute(1, 2, 0).reshape(b, self.c2, w, h) class Bottleneck(nn.Module): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c2, 3, 1, g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class BottleneckCSP(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n))) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(self.act(self.bn(torch.cat((y1, y2), 1)))) class CrossConv(nn.Module): # Cross Convolution Downsample def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False): # ch_in, ch_out, kernel, stride, groups, expansion, shortcut super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, (1, k), (1, s)) self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class C3(nn.Module): # CSP Bottleneck with 3 convolutions def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2) self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n))) def forward(self, x): return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1)) class C3x(C3): # C3 module with cross-convolutions def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) self.m = nn.Sequential(*(CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n))) class C3TR(C3): # C3 module with TransformerBlock() def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) self.m = TransformerBlock(c_, c_, 4, n) class C3SPP(C3): # C3 module with SPP() def __init__(self, c1, c2, k=(5, 9, 13), n=1, shortcut=True, g=1, e=0.5): super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) self.m = SPP(c_, c_, k) class C3Ghost(C3): # C3 module with GhostBottleneck() def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*(GhostBottleneck(c_, c_) for _ in range(n))) class SPP(nn.Module): # Spatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729 def __init__(self, c1, c2, k=(5, 9, 13)): super().__init__() c_ = c1 // 2 # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1) self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) def forward(self, x): x = self.cv1(x) with warnings.catch_warnings(): warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1)) class SPPF(nn.Module): # Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13)) super().__init__() c_ = c1 // 2 # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_ * 4, c2, 1, 1) self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2) def forward(self, x): x = self.cv1(x) with warnings.catch_warnings(): warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning y1 = self.m(x) y2 = self.m(y1) return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1)) class Focus(nn.Module): # Focus wh information into c-space def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super().__init__() self.conv = Conv(c1 * 4, c2, k, s, p, g, act) # self.contract = Contract(gain=2) def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2) return self.conv(torch.cat((x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]), 1)) # return self.conv(self.contract(x)) class GhostConv(nn.Module): # Ghost Convolution https://github.com/huawei-noah/ghostnet def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups super().__init__() c_ = c2 // 2 # hidden channels self.cv1 = Conv(c1, c_, k, s, None, g, act) self.cv2 = Conv(c_, c_, 5, 1, None, c_, act) def forward(self, x): y = self.cv1(x) return torch.cat((y, self.cv2(y)), 1) class GhostBottleneck(nn.Module): # Ghost Bottleneck https://github.com/huawei-noah/ghostnet def __init__(self, c1, c2, k=3, s=1): # ch_in, ch_out, kernel, stride super().__init__() c_ = c2 // 2 self.conv = nn.Sequential( GhostConv(c1, c_, 1, 1), # pw DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw GhostConv(c_, c2, 1, 1, act=False)) # pw-linear self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False), Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity() def forward(self, x): return self.conv(x) + self.shortcut(x) class Contract(nn.Module): # Contract width-height into channels, i.e. x(1,64,80,80) to x(1,256,40,40) def __init__(self, gain=2): super().__init__() self.gain = gain def forward(self, x): b, c, h, w = x.size() # assert (h / s == 0) and (W / s == 0), 'Indivisible gain' s = self.gain x = x.view(b, c, h // s, s, w // s, s) # x(1,64,40,2,40,2) x = x.permute(0, 3, 5, 1, 2, 4).contiguous() # x(1,2,2,64,40,40) return x.view(b, c * s * s, h // s, w // s) # x(1,256,40,40) class Expand(nn.Module): # Expand channels into width-height, i.e. x(1,64,80,80) to x(1,16,160,160) def __init__(self, gain=2): super().__init__() self.gain = gain def forward(self, x): b, c, h, w = x.size() # assert C / s ** 2 == 0, 'Indivisible gain' s = self.gain x = x.view(b, s, s, c // s ** 2, h, w) # x(1,2,2,16,80,80) x = x.permute(0, 3, 4, 1, 5, 2).contiguous() # x(1,16,80,2,80,2) return x.view(b, c // s ** 2, h * s, w * s) # x(1,16,160,160) class Concat(nn.Module): # Concatenate a list of tensors along dimension def __init__(self, dimension=1): super().__init__() self.d = dimension def forward(self, x): return torch.cat(x, self.d) class DetectMultiBackend(nn.Module): # YOLOv5 MultiBackend class for python inference on various backends def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False, data=None, fp16=False, fuse=True): # Usage: # PyTorch: weights = *.pt # TorchScript: *.torchscript # ONNX Runtime: *.onnx # ONNX OpenCV DNN: *.onnx with --dnn # OpenVINO: *.xml # CoreML: *.mlmodel # TensorRT: *.engine # TensorFlow SavedModel: *_saved_model # TensorFlow GraphDef: *.pb # TensorFlow Lite: *.tflite # TensorFlow Edge TPU: *_edgetpu.tflite from asone.detectors.yolov5.yolov5.models.experimental import attempt_download, attempt_load # scoped to avoid circular import super().__init__() w = str(weights[0] if isinstance(weights, list) else weights) pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs = self.model_type(w) # get backend w = attempt_download(w) # download if not local fp16 &= (pt or jit or onnx or engine) and device.type != 'cpu' # FP16 stride, names = 32, [f'class{i}' for i in range(1000)] # assign defaults if data: # assign class names (optional) with open(data, errors='ignore') as f: names = yaml.safe_load(f)['names'] if pt: # PyTorch model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse) stride = max(int(model.stride.max()), 32) # model stride names = model.module.names if hasattr(model, 'module') else model.names # get class names model.half() if fp16 else model.float() self.model = model # explicitly assign for to(), cpu(), cuda(), half() elif jit: # TorchScript LOGGER.info(f'Loading {w} for TorchScript inference...') extra_files = {'config.txt': ''} # model metadata model = torch.jit.load(w, _extra_files=extra_files) model.half() if fp16 else model.float() if extra_files['config.txt']: d = json.loads(extra_files['config.txt']) # extra_files dict stride, names = int(d['stride']), d['names'] elif dnn: # ONNX OpenCV DNN LOGGER.info(f'Loading {w} for ONNX OpenCV DNN inference...') check_requirements(('opencv-python>=4.5.4',)) net = cv2.dnn.readNetFromONNX(w) elif onnx: # ONNX Runtime LOGGER.info(f'Loading {w} for ONNX Runtime inference...') cuda = torch.cuda.is_available() check_requirements(('onnx', 'onnxruntime-gpu' if cuda else 'onnxruntime')) import onnxruntime providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider'] session = onnxruntime.InferenceSession(w, providers=providers) meta = session.get_modelmeta().custom_metadata_map # metadata if 'stride' in meta: stride, names = int(meta['stride']), eval(meta['names']) elif xml: # OpenVINO LOGGER.info(f'Loading {w} for OpenVINO inference...') check_requirements(('openvino',)) # requires openvino-dev: https://pypi.org/project/openvino-dev/ import openvino from openvino.runtime import Core, Layout, get_batch ie = Core() if not Path(w).is_file(): # if not *.xml w = next(Path(w).glob('*.xml')) # get *.xml file from *_openvino_model dir network = ie.read_model(model=w, weights=Path(w).with_suffix('.bin')) if network.get_parameters()[0].get_layout().empty: network.get_parameters()[0].set_layout(Layout("NCHW")) batch_dim = get_batch(network) if batch_dim.is_static: batch_size = batch_dim.get_length() executable_network = ie.compile_model(network, device_name="CPU") # device_name="MYRIAD" for Intel NCS2 output_layer = next(iter(executable_network.outputs)) meta = Path(w).with_suffix('.yaml') if meta.exists(): stride, names = self._load_metadata(meta) # load metadata elif engine: # TensorRT LOGGER.info(f'Loading {w} for TensorRT inference...') import tensorrt as trt # https://developer.nvidia.com/nvidia-tensorrt-download check_version(trt.__version__, '7.0.0', hard=True) # require tensorrt>=7.0.0 Binding = namedtuple('Binding', ('name', 'dtype', 'shape', 'data', 'ptr')) logger = trt.Logger(trt.Logger.INFO) with open(w, 'rb') as f, trt.Runtime(logger) as runtime: model = runtime.deserialize_cuda_engine(f.read()) context = model.create_execution_context() bindings = OrderedDict() fp16 = False # default updated below dynamic_input = False for index in range(model.num_bindings): name = model.get_binding_name(index) dtype = trt.nptype(model.get_binding_dtype(index)) if model.binding_is_input(index): if -1 in tuple(model.get_binding_shape(index)): # dynamic dynamic_input = True context.set_binding_shape(index, tuple(model.get_profile_shape(0, index)[2])) if dtype == np.float16: fp16 = True shape = tuple(context.get_binding_shape(index)) data = torch.from_numpy(np.empty(shape, dtype=np.dtype(dtype))).to(device) bindings[name] = Binding(name, dtype, shape, data, int(data.data_ptr())) binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items()) batch_size = bindings['images'].shape[0] # if dynamic, this is instead max batch size elif coreml: # CoreML LOGGER.info(f'Loading {w} for CoreML inference...') import coremltools as ct model = ct.models.MLModel(w) else: # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU) if saved_model: # SavedModel LOGGER.info(f'Loading {w} for TensorFlow SavedModel inference...') import tensorflow as tf keras = False # assume TF1 saved_model model = tf.keras.models.load_model(w) if keras else tf.saved_model.load(w) elif pb: # GraphDef https://www.tensorflow.org/guide/migrate#a_graphpb_or_graphpbtxt LOGGER.info(f'Loading {w} for TensorFlow GraphDef inference...') import tensorflow as tf def wrap_frozen_graph(gd, inputs, outputs): x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) # wrapped ge = x.graph.as_graph_element return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs)) gd = tf.Graph().as_graph_def() # graph_def with open(w, 'rb') as f: gd.ParseFromString(f.read()) frozen_func = wrap_frozen_graph(gd, inputs="x:0", outputs="Identity:0") elif tflite or edgetpu: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python try: # https://coral.ai/docs/edgetpu/tflite-python/#update-existing-tf-lite-code-for-the-edge-tpu from tflite_runtime.interpreter import Interpreter, load_delegate except ImportError: import tensorflow as tf Interpreter, load_delegate = tf.lite.Interpreter, tf.lite.experimental.load_delegate, if edgetpu: # Edge TPU https://coral.ai/software/#edgetpu-runtime LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...') delegate = { 'Linux': 'libedgetpu.so.1', 'Darwin': 'libedgetpu.1.dylib', 'Windows': 'edgetpu.dll'}[platform.system()] interpreter = Interpreter(model_path=w, experimental_delegates=[load_delegate(delegate)]) else: # Lite LOGGER.info(f'Loading {w} for TensorFlow Lite inference...') interpreter = Interpreter(model_path=w) # load TFLite model interpreter.allocate_tensors() # allocate input_details = interpreter.get_input_details() # inputs output_details = interpreter.get_output_details() # outputs elif tfjs: raise Exception('ERROR: YOLOv5 TF.js inference is not supported') else: raise Exception(f'ERROR: {w} is not a supported format') self.__dict__.update(locals()) # assign all variables to self def forward(self, im, augment=False, visualize=False, val=False): # YOLOv5 MultiBackend inference b, ch, h, w = im.shape # batch, channel, height, width if self.fp16 and im.dtype != torch.float16: im = im.half() # to FP16 if self.pt: # PyTorch y = self.model(im, augment=augment, visualize=visualize)[0] elif self.jit: # TorchScript y = self.model(im)[0] elif self.dnn: # ONNX OpenCV DNN im = im.cpu().numpy() # torch to numpy self.net.setInput(im) y = self.net.forward() elif self.onnx: # ONNX Runtime im = im.cpu().numpy() # torch to numpy y = self.session.run([self.session.get_outputs()[0].name], {self.session.get_inputs()[0].name: im})[0] elif self.xml: # OpenVINO im = im.cpu().numpy() # FP32 y = self.executable_network([im])[self.output_layer] elif self.engine: # TensorRT if im.shape != self.bindings['images'].shape and self.dynamic_input: self.context.set_binding_shape(self.model.get_binding_index('images'), im.shape) # reshape if dynamic self.bindings['images'] = self.bindings['images']._replace(shape=im.shape) assert im.shape == self.bindings['images'].shape, ( f"image shape {im.shape} exceeds model max shape {self.bindings['images'].shape}" if self.dynamic_input else f"image shape {im.shape} does not match model shape {self.bindings['images'].shape}") self.binding_addrs['images'] = int(im.data_ptr()) self.context.execute_v2(list(self.binding_addrs.values())) y = self.bindings['output'].data elif self.coreml: # CoreML im = im.permute(0, 2, 3, 1).cpu().numpy() # torch BCHW to numpy BHWC shape(1,320,192,3) im = Image.fromarray((im[0] * 255).astype('uint8')) # im = im.resize((192, 320), Image.ANTIALIAS) y = self.model.predict({'image': im}) # coordinates are xywh normalized if 'confidence' in y: box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) # xyxy pixels conf, cls = y['confidence'].max(1), y['confidence'].argmax(1).astype(np.float) y = np.concatenate((box, conf.reshape(-1, 1), cls.reshape(-1, 1)), 1) else: k = 'var_' + str(sorted(int(k.replace('var_', '')) for k in y)[-1]) # output key y = y[k] # output else: # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU) im = im.permute(0, 2, 3, 1).cpu().numpy() # torch BCHW to numpy BHWC shape(1,320,192,3) if self.saved_model: # SavedModel y = (self.model(im, training=False) if self.keras else self.model(im)).numpy() elif self.pb: # GraphDef y = self.frozen_func(x=self.tf.constant(im)).numpy() else: # Lite or Edge TPU input, output = self.input_details[0], self.output_details[0] int8 = input['dtype'] == np.uint8 # is TFLite quantized uint8 model if int8: scale, zero_point = input['quantization'] im = (im / scale + zero_point).astype(np.uint8) # de-scale self.interpreter.set_tensor(input['index'], im) self.interpreter.invoke() y = self.interpreter.get_tensor(output['index']) if int8: scale, zero_point = output['quantization'] y = (y.astype(np.float32) - zero_point) * scale # re-scale y[..., :4] *= [w, h, w, h] # xywh normalized to pixels if isinstance(y, np.ndarray): y = torch.tensor(y, device=self.device) return (y, []) if val else y def warmup(self, imgsz=(1, 3, 640, 640)): # Warmup model by running inference once warmup_types = self.pt, self.jit, self.onnx, self.engine, self.saved_model, self.pb if any(warmup_types) and self.device.type != 'cpu': im = torch.zeros(*imgsz, dtype=torch.half if self.fp16 else torch.float, device=self.device) # input for _ in range(2 if self.jit else 1): # self.forward(im) # warmup @staticmethod def model_type(p='path/to/model.pt'): # Return model type from model path, i.e. path='path/to/model.onnx' -> type=onnx from asone.detectors.yolov5.yolov5.export import export_formats suffixes = list(export_formats().Suffix) + ['.xml'] # export suffixes check_suffix(p, suffixes) # checks p = Path(p).name # eliminate trailing separators pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, xml2 = (s in p for s in suffixes) xml |= xml2 # *_openvino_model or *.xml tflite &= not edgetpu # *.tflite return pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs @staticmethod def _load_metadata(f='path/to/meta.yaml'): # Load metadata from meta.yaml if it exists with open(f, errors='ignore') as f: d = yaml.safe_load(f) return d['stride'], d['names'] # assign stride, names class AutoShape(nn.Module): # YOLOv5 input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS conf = 0.25 # NMS confidence threshold iou = 0.45 # NMS IoU threshold agnostic = False # NMS class-agnostic multi_label = False # NMS multiple labels per box classes = None # (optional list) filter by class, i.e. = [0, 15, 16] for COCO persons, cats and dogs max_det = 1000 # maximum number of detections per image amp = False # Automatic Mixed Precision (AMP) inference def __init__(self, model, verbose=True): super().__init__() if verbose: LOGGER.info('Adding AutoShape... ') copy_attr(self, model, include=('yaml', 'nc', 'hyp', 'names', 'stride', 'abc'), exclude=()) # copy attributes self.dmb = isinstance(model, DetectMultiBackend) # DetectMultiBackend() instance self.pt = not self.dmb or model.pt # PyTorch model self.model = model.eval() def _apply(self, fn): # Apply to(), cpu(), cuda(), half() to model tensors that are not parameters or registered buffers self = super()._apply(fn) if self.pt: m = self.model.model.model[-1] if self.dmb else self.model.model[-1] # Detect() m.stride = fn(m.stride) m.grid = list(map(fn, m.grid)) if isinstance(m.anchor_grid, list): m.anchor_grid = list(map(fn, m.anchor_grid)) return self @torch.no_grad() def forward(self, imgs, size=640, augment=False, profile=False): # Inference from various sources. For height=640, width=1280, RGB images example inputs are: # file: imgs = 'data/images/zidane.jpg' # str or PosixPath # URI: = 'https://ultralytics.com/images/zidane.jpg' # OpenCV: = cv2.imread('image.jpg')[:,:,::-1] # HWC BGR to RGB x(640,1280,3) # PIL: = Image.open('image.jpg') or ImageGrab.grab() # HWC x(640,1280,3) # numpy: = np.zeros((640,1280,3)) # HWC # torch: = torch.zeros(16,3,320,640) # BCHW (scaled to size=640, 0-1 values) # multiple: = [Image.open('image1.jpg'), Image.open('image2.jpg'), ...] # list of images t = [time_sync()] p = next(self.model.parameters()) if self.pt else torch.zeros(1, device=self.model.device) # for device, type autocast = self.amp and (p.device.type != 'cpu') # Automatic Mixed Precision (AMP) inference if isinstance(imgs, torch.Tensor): # torch with amp.autocast(autocast): return self.model(imgs.to(p.device).type_as(p), augment, profile) # inference # Pre-process n, imgs = (len(imgs), list(imgs)) if isinstance(imgs, (list, tuple)) else (1, [imgs]) # number, list of images shape0, shape1, files = [], [], [] # image and inference shapes, filenames for i, im in enumerate(imgs): f = f'image{i}' # filename if isinstance(im, (str, Path)): # filename or uri im, f = Image.open(requests.get(im, stream=True).raw if str(im).startswith('http') else im), im im = np.asarray(exif_transpose(im)) elif isinstance(im, Image.Image): # PIL Image im, f = np.asarray(exif_transpose(im)), getattr(im, 'filename', f) or f files.append(Path(f).with_suffix('.jpg').name) if im.shape[0] < 5: # image in CHW im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1) im = im[..., :3] if im.ndim == 3 else np.tile(im[..., None], 3) # enforce 3ch input s = im.shape[:2] # HWC shape0.append(s) # image shape g = (size / max(s)) # gain shape1.append([y * g for y in s]) imgs[i] = im if im.data.contiguous else np.ascontiguousarray(im) # update shape1 = [make_divisible(x, self.stride) if self.pt else size for x in np.array(shape1).max(0)] # inf shape x = [letterbox(im, shape1, auto=False)[0] for im in imgs] # pad x = np.ascontiguousarray(np.array(x).transpose((0, 3, 1, 2))) # stack and BHWC to BCHW x = torch.from_numpy(x).to(p.device).type_as(p) / 255 # uint8 to fp16/32 t.append(time_sync()) with amp.autocast(autocast): # Inference y = self.model(x, augment, profile) # forward t.append(time_sync()) # Post-process y = non_max_suppression(y if self.dmb else y[0], self.conf, self.iou, self.classes, self.agnostic, self.multi_label, max_det=self.max_det) # NMS for i in range(n): scale_coords(shape1, y[i][:, :4], shape0[i]) t.append(time_sync()) return Detections(imgs, y, files, t, self.names, x.shape) class Detections: # YOLOv5 detections class for inference results def __init__(self, imgs, pred, files, times=(0, 0, 0, 0), names=None, shape=None): super().__init__() d = pred[0].device # device gn = [torch.tensor([*(im.shape[i] for i in [1, 0, 1, 0]), 1, 1], device=d) for im in imgs] # normalizations self.imgs = imgs # list of images as numpy arrays self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls) self.names = names # class names self.files = files # image filenames self.times = times # profiling times self.xyxy = pred # xyxy pixels self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized self.n = len(self.pred) # number of images (batch size) self.t = tuple((times[i + 1] - times[i]) * 1000 / self.n for i in range(3)) # timestamps (ms) self.s = shape # inference BCHW shape def display(self, pprint=False, show=False, save=False, crop=False, render=False, labels=True, save_dir=Path('')): crops = [] for i, (im, pred) in enumerate(zip(self.imgs, self.pred)): s = f'image {i + 1}/{len(self.pred)}: {im.shape[0]}x{im.shape[1]} ' # string if pred.shape[0]: for c in pred[:, -1].unique(): n = (pred[:, -1] == c).sum() # detections per class s += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string if show or save or render or crop: annotator = Annotator(im, example=str(self.names)) for *box, conf, cls in reversed(pred): # xyxy, confidence, class label = f'{self.names[int(cls)]} {conf:.2f}' if crop: file = save_dir / 'crops' / self.names[int(cls)] / self.files[i] if save else None crops.append({ 'box': box, 'conf': conf, 'cls': cls, 'label': label, 'im': save_one_box(box, im, file=file, save=save)}) else: # all others annotator.box_label(box, label if labels else '', color=colors(cls)) im = annotator.im else: s += '(no detections)' im = Image.fromarray(im.astype(np.uint8)) if isinstance(im, np.ndarray) else im # from np if pprint: print(s.rstrip(', ')) if show: im.show(self.files[i]) # show if save: f = self.files[i] im.save(save_dir / f) # save if i == self.n - 1: LOGGER.info(f"Saved {self.n} image{'s' * (self.n > 1)} to {colorstr('bold', save_dir)}") if render: self.imgs[i] = np.asarray(im) if crop: if save: LOGGER.info(f'Saved results to {save_dir}\n') return crops def print(self): self.display(pprint=True) # print results print(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {tuple(self.s)}' % self.t) def show(self, labels=True): self.display(show=True, labels=labels) # show results def save(self, labels=True, save_dir='runs/detect/exp'): save_dir = increment_path(save_dir, exist_ok=save_dir != 'runs/detect/exp', mkdir=True) # increment save_dir self.display(save=True, labels=labels, save_dir=save_dir) # save results def crop(self, save=True, save_dir='runs/detect/exp'): save_dir = increment_path(save_dir, exist_ok=save_dir != 'runs/detect/exp', mkdir=True) if save else None return self.display(crop=True, save=save, save_dir=save_dir) # crop results def render(self, labels=True): self.display(render=True, labels=labels) # render results return self.imgs def pandas(self): # return detections as pandas DataFrames, i.e. print(results.pandas().xyxy[0]) new = copy(self) # return copy ca = 'xmin', 'ymin', 'xmax', 'ymax', 'confidence', 'class', 'name' # xyxy columns cb = 'xcenter', 'ycenter', 'width', 'height', 'confidence', 'class', 'name' # xywh columns for k, c in zip(['xyxy', 'xyxyn', 'xywh', 'xywhn'], [ca, ca, cb, cb]): a = [[x[:5] + [int(x[5]), self.names[int(x[5])]] for x in x.tolist()] for x in getattr(self, k)] # update setattr(new, k, [pd.DataFrame(x, columns=c) for x in a]) return new def tolist(self): # return a list of Detections objects, i.e. 'for result in results.tolist():' r = range(self.n) # iterable x = [Detections([self.imgs[i]], [self.pred[i]], [self.files[i]], self.times, self.names, self.s) for i in r] # for d in x: # for k in ['imgs', 'pred', 'xyxy', 'xyxyn', 'xywh', 'xywhn']: # setattr(d, k, getattr(d, k)[0]) # pop out of list return x def __len__(self): return self.n # override len(results) def __str__(self): self.print() # override print(results) return '' class Classify(nn.Module): # Classification head, i.e. x(b,c1,20,20) to x(b,c2) def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups super().__init__() self.aap = nn.AdaptiveAvgPool2d(1) # to x(b,c1,1,1) self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g) # to x(b,c2,1,1) self.flat = nn.Flatten() def forward(self, x): z = torch.cat([self.aap(y) for y in (x if isinstance(x, list) else [x])], 1) # cat if list return self.flat(self.conv(z)) # flatten to x(b,c2) ================================================ FILE: asone/detectors/yolov5/yolov5/models/experimental.py ================================================ # YOLOv5 🚀 by Ultralytics, GPL-3.0 license """ Experimental modules """ import math import numpy as np import torch import torch.nn as nn from asone.detectors.yolov5.yolov5.utils.yolov5_utils import yolov5_in_syspath class Ensemble(nn.ModuleList): # Ensemble of models def __init__(self): super().__init__() def forward(self, x, augment=False, profile=False, visualize=False): y = [module(x, augment, profile, visualize)[0] for module in self] # y = torch.stack(y).max(0)[0] # max ensemble # y = torch.stack(y).mean(0) # mean ensemble y = torch.cat(y, 1) # nms ensemble return y, None # inference, train output def attempt_load(weights, device=None, inplace=True, fuse=True): # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a with yolov5_in_syspath(): from asone.detectors.yolov5.yolov5.models.yolo import Detect, Model model = Ensemble() for w in weights if isinstance(weights, list) else [weights]: with yolov5_in_syspath(): ckpt = torch.load(w, map_location='cpu') # load ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float() # FP32 model model.append(ckpt.fuse().eval() if fuse else ckpt.eval()) # fused or un-fused model in eval mode # Compatibility updates for m in model.modules(): t = type(m) if t in (nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, Detect, Model): m.inplace = inplace # torch 1.7.0 compatibility if t is Detect and not isinstance(m.anchor_grid, list): delattr(m, 'anchor_grid') setattr(m, 'anchor_grid', [torch.zeros(1)] * m.nl) elif t is nn.Upsample and not hasattr(m, 'recompute_scale_factor'): m.recompute_scale_factor = None # torch 1.11.0 compatibility if len(model) == 1: return model[-1] # return model print(f'Ensemble created with {weights}\n') for k in 'names', 'nc', 'yaml': setattr(model, k, getattr(model[0], k)) model.stride = model[torch.argmax(torch.tensor([m.stride.max() for m in model])).int()].stride # max stride assert all(model[0].nc == m.nc for m in model), f'Models have different class counts: {[m.nc for m in model]}' return model # return ensemble ================================================ FILE: asone/detectors/yolov5/yolov5/models/general.py ================================================ # YOLOv5 🚀 by Ultralytics, GPL-3.0 license """ General utils """ import contextlib import glob import inspect import logging import math import os import platform import random import re import shutil import signal import threading import time import urllib from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from typing import Optional from zipfile import ZipFile import cv2 import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml FILE = Path(__file__).resolve() ROOT = FILE.parents[1] # YOLOv5 root directory RANK = int(os.getenv('RANK', -1)) # Settings DATASETS_DIR = ROOT.parent / 'datasets' # YOLOv5 datasets directory NUM_THREADS = min(8, max(1, os.cpu_count() - 1)) # number of YOLOv5 multiprocessing threads AUTOINSTALL = str(os.getenv('YOLOv5_AUTOINSTALL', True)).lower() == 'true' # global auto-install mode VERBOSE = str(os.getenv('YOLOv5_VERBOSE', True)).lower() == 'true' # global verbose mode FONT = 'Arial.ttf' # https://ultralytics.com/assets/Arial.ttf torch.set_printoptions(linewidth=320, precision=5, profile='long') np.set_printoptions(linewidth=320, formatter={'float_kind': '{:11.5g}'.format}) # format short g, %precision=5 pd.options.display.max_columns = 10 cv2.setNumThreads(0) # prevent OpenCV from multithreading (incompatible with PyTorch DataLoader) os.environ['NUMEXPR_MAX_THREADS'] = str(NUM_THREADS) # NumExpr max threads os.environ['OMP_NUM_THREADS'] = '1' if platform.system() == 'darwin' else str(NUM_THREADS) # OpenMP (PyTorch and SciPy) def is_kaggle(): # Is environment a Kaggle Notebook? try: assert os.environ.get('PWD') == '/kaggle/working' assert os.environ.get('KAGGLE_URL_BASE') == 'https://www.kaggle.com' return True except AssertionError: return False def is_writeable(dir, test=False): # Return True if directory has write permissions, test opening a file with write permissions if test=True if not test: return os.access(dir, os.R_OK) # possible issues on Windows file = Path(dir) / 'tmp.txt' try: with open(file, 'w'): # open file with write permissions pass file.unlink() # remove file return True except OSError: return False def set_logging(name=None, verbose=VERBOSE): # Sets level and returns logger if is_kaggle(): for h in logging.root.handlers: logging.root.removeHandler(h) # remove all handlers associated with the root logger object rank = int(os.getenv('RANK', -1)) # rank in world for Multi-GPU trainings level = logging.INFO if verbose and rank in {-1, 0} else logging.ERROR log = logging.getLogger(name) log.setLevel(level) handler = logging.StreamHandler() handler.setFormatter(logging.Formatter("%(message)s")) handler.setLevel(level) log.addHandler(handler) set_logging() # run before defining LOGGER LOGGER = logging.getLogger("yolov5") # define globally (used in train.py, val.py, detect.py, etc.) def user_config_dir(dir='Ultralytics', env_var='YOLOV5_CONFIG_DIR'): # Return path of user configuration directory. Prefer environment variable if exists. Make dir if required. env = os.getenv(env_var) if env: path = Path(env) # use environment variable else: cfg = {'Windows': 'AppData/Roaming', 'Linux': '.config', 'Darwin': 'Library/Application Support'} # 3 OS dirs path = Path.home() / cfg.get(platform.system(), '') # OS-specific config dir path = (path if is_writeable(path) else Path('/tmp')) / dir # GCP and AWS lambda fix, only /tmp is writeable path.mkdir(exist_ok=True) # make if required return path CONFIG_DIR = user_config_dir() # Ultralytics settings dir class Profile(contextlib.ContextDecorator): # Usage: @Profile() decorator or 'with Profile():' context manager def __enter__(self): self.start = time.time() def __exit__(self, type, value, traceback): print(f'Profile results: {time.time() - self.start:.5f}s') class Timeout(contextlib.ContextDecorator): # Usage: @Timeout(seconds) decorator or 'with Timeout(seconds):' context manager def __init__(self, seconds, *, timeout_msg='', suppress_timeout_errors=True): self.seconds = int(seconds) self.timeout_message = timeout_msg self.suppress = bool(suppress_timeout_errors) def _timeout_handler(self, signum, frame): raise TimeoutError(self.timeout_message) def __enter__(self): if platform.system() != 'Windows': # not supported on Windows signal.signal(signal.SIGALRM, self._timeout_handler) # Set handler for SIGALRM signal.alarm(self.seconds) # start countdown for SIGALRM to be raised def __exit__(self, exc_type, exc_val, exc_tb): if platform.system() != 'Windows': signal.alarm(0) # Cancel SIGALRM if it's scheduled if self.suppress and exc_type is TimeoutError: # Suppress TimeoutError return True class WorkingDirectory(contextlib.ContextDecorator): # Usage: @WorkingDirectory(dir) decorator or 'with WorkingDirectory(dir):' context manager def __init__(self, new_dir): self.dir = new_dir # new dir self.cwd = Path.cwd().resolve() # current dir def __enter__(self): os.chdir(self.dir) def __exit__(self, exc_type, exc_val, exc_tb): os.chdir(self.cwd) def try_except(func): # try-except function. Usage: @try_except decorator def handler(*args, **kwargs): try: func(*args, **kwargs) except Exception as e: print(e) return handler def threaded(func): # Multi-threads a target function and returns thread. Usage: @threaded decorator def wrapper(*args, **kwargs): thread = threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True) thread.start() return thread return wrapper def methods(instance): # Get class/instance methods return [f for f in dir(instance) if callable(getattr(instance, f)) and not f.startswith("__")] def print_args(args: Optional[dict] = None, show_file=True, show_fcn=False): # Print function arguments (optional args dict) x = inspect.currentframe().f_back # previous frame file, _, fcn, _, _ = inspect.getframeinfo(x) if args is None: # get args automatically args, _, _, frm = inspect.getargvalues(x) args = {k: v for k, v in frm.items() if k in args} s = (f'{Path(file).stem}: ' if show_file else '') + (f'{fcn}: ' if show_fcn else '') LOGGER.info(colorstr(s) + ', '.join(f'{k}={v}' for k, v in args.items())) def init_seeds(seed=0, deterministic=False): # Initialize random number generator (RNG) seeds https://pytorch.org/docs/stable/notes/randomness.html # cudnn seed 0 settings are slower and more reproducible, else faster and less reproducible import torch.backends.cudnn as cudnn if deterministic and check_version(torch.__version__, '1.12.0'): # https://github.com/ultralytics/yolov5/pull/8213 torch.use_deterministic_algorithms(True) os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8' os.environ['PYTHONHASHSEED'] = str(seed) random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) cudnn.benchmark, cudnn.deterministic = (False, True) if seed == 0 else (True, False) torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) # for Multi-GPU, exception safe def intersect_dicts(da, db, exclude=()): # Dictionary intersection of matching keys and shapes, omitting 'exclude' keys, using da values return {k: v for k, v in da.items() if k in db and not any(x in k for x in exclude) and v.shape == db[k].shape} def get_latest_run(search_dir='.'): # Return path to most recent 'last.pt' in /runs (i.e. to --resume from) last_list = glob.glob(f'{search_dir}/**/last*.pt', recursive=True) return max(last_list, key=os.path.getctime) if last_list else '' def is_docker() -> bool: """Check if the process runs inside a docker container.""" if Path("/.dockerenv").exists(): return True try: # check if docker is in control groups with open("/proc/self/cgroup") as file: return any("docker" in line for line in file) except OSError: return False def is_colab(): # Is environment a Google Colab instance? try: import google.colab return True except ImportError: return False def is_pip(): # Is file in a pip package? return 'site-packages' in Path(__file__).resolve().parts def is_ascii(s=''): # Is string composed of all ASCII (no UTF) characters? (note str().isascii() introduced in python 3.7) s = str(s) # convert list, tuple, None, etc. to str return len(s.encode().decode('ascii', 'ignore')) == len(s) def is_chinese(s='人工智能'): # Is string composed of any Chinese characters? return bool(re.search('[\u4e00-\u9fff]', str(s))) def emojis(str=''): # Return platform-dependent emoji-safe version of string return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str def file_age(path=__file__): # Return days since last file update dt = (datetime.now() - datetime.fromtimestamp(Path(path).stat().st_mtime)) # delta return dt.days # + dt.seconds / 86400 # fractional days def file_date(path=__file__): # Return human-readable file modification date, i.e. '2021-3-26' t = datetime.fromtimestamp(Path(path).stat().st_mtime) return f'{t.year}-{t.month}-{t.day}' def file_size(path): # Return file/dir size (MB) mb = 1 << 20 # bytes to MiB (1024 ** 2) path = Path(path) if path.is_file(): return path.stat().st_size / mb elif path.is_dir(): return sum(f.stat().st_size for f in path.glob('**/*') if f.is_file()) / mb else: return 0.0 def check_online(): # Check internet connectivity import socket try: socket.create_connection(("1.1.1.1", 443), 5) # check host accessibility return True except OSError: return False def git_describe(path=ROOT): # path must be a directory # Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe try: assert (Path(path) / '.git').is_dir() return check_output(f'git -C {path} describe --tags --long --always', shell=True).decode()[:-1] except Exception: return '' @try_except @WorkingDirectory(ROOT) def check_git_status(repo='ultralytics/yolov5'): # YOLOv5 status check, recommend 'git pull' if code is out of date url = f'https://github.com/{repo}' msg = f', for updates see {url}' s = colorstr('github: ') # string assert Path('.git').exists(), s + 'skipping check (not a git repository)' + msg assert check_online(), s + 'skipping check (offline)' + msg splits = re.split(pattern=r'\s', string=check_output('git remote -v', shell=True).decode()) matches = [repo in s for s in splits] if any(matches): remote = splits[matches.index(True) - 1] else: remote = 'ultralytics' check_output(f'git remote add {remote} {url}', shell=True) check_output(f'git fetch {remote}', shell=True, timeout=5) # git fetch branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out n = int(check_output(f'git rev-list {branch}..{remote}/master --count', shell=True)) # commits behind if n > 0: pull = 'git pull' if remote == 'origin' else f'git pull {remote} master' s += f"⚠️ YOLOv5 is out of date by {n} commit{'s' * (n > 1)}. Use `{pull}` or `git clone {url}` to update." else: s += f'up to date with {url} ✅' LOGGER.info(emojis(s)) # emoji-safe def check_python(minimum='3.7.0'): # Check current python version vs. required python version check_version(platform.python_version(), minimum, name='Python ', hard=True) def check_version(current='0.0.0', minimum='0.0.0', name='version ', pinned=False, hard=False, verbose=False): # Check version vs. required version current, minimum = (pkg.parse_version(x) for x in (current, minimum)) result = (current == minimum) if pinned else (current >= minimum) # bool s = f'{name}{minimum} required by YOLOv5, but {name}{current} is currently installed' # string if hard: assert result, s # assert min requirements met if verbose and not result: LOGGER.warning(s) return result @try_except def check_requirements(requirements=ROOT / 'requirements.txt', exclude=(), install=True, cmds=()): # Check installed dependencies meet requirements (pass *.txt file or list of packages) prefix = colorstr('red', 'bold', 'requirements:') check_python() # check python version if isinstance(requirements, (str, Path)): # requirements.txt file file = Path(requirements) assert file.exists(), f"{prefix} {file.resolve()} not found, check failed." with file.open() as f: requirements = [f'{x.name}{x.specifier}' for x in pkg.parse_requirements(f) if x.name not in exclude] else: # list or tuple of packages requirements = [x for x in requirements if x not in exclude] n = 0 # number of packages updates for i, r in enumerate(requirements): try: pkg.require(r) except Exception: # DistributionNotFound or VersionConflict if requirements not met s = f"{prefix} {r} not found and is required by YOLOv5" if install and AUTOINSTALL: # check environment variable LOGGER.info(f"{s}, attempting auto-update...") try: assert check_online(), f"'pip install {r}' skipped (offline)" LOGGER.info(check_output(f'pip install "{r}" {cmds[i] if cmds else ""}', shell=True).decode()) n += 1 except Exception as e: LOGGER.warning(f'{prefix} {e}') else: LOGGER.info(f'{s}. Please install and rerun your command.') if n: # if packages updated source = file.resolve() if 'file' in locals() else requirements s = f"{prefix} {n} package{'s' * (n > 1)} updated per {source}\n" \ f"{prefix} ⚠️ {colorstr('bold', 'Restart runtime or rerun command for updates to take effect')}\n" LOGGER.info(emojis(s)) def check_img_size(imgsz, s=32, floor=0): # Verify image size is a multiple of stride s in each dimension if isinstance(imgsz, int): # integer i.e. img_size=640 new_size = max(make_divisible(imgsz, int(s)), floor) else: # list i.e. img_size=[640, 480] imgsz = list(imgsz) # convert to list if tuple new_size = [max(make_divisible(x, int(s)), floor) for x in imgsz] if new_size != imgsz: LOGGER.warning(f'WARNING: --img-size {imgsz} must be multiple of max stride {s}, updating to {new_size}') return new_size def check_imshow(): # Check if environment supports image displays try: assert not is_docker(), 'cv2.imshow() is disabled in Docker environments' assert not is_colab(), 'cv2.imshow() is disabled in Google Colab environments' cv2.imshow('test', np.zeros((1, 1, 3))) cv2.waitKey(1) cv2.destroyAllWindows() cv2.waitKey(1) return True except Exception as e: LOGGER.warning(f'WARNING: Environment does not support cv2.imshow() or PIL Image.show() image displays\n{e}') return False def check_suffix(file='yolov5s.pt', suffix=('.pt',), msg=''): # Check file(s) for acceptable suffix if file and suffix: if isinstance(suffix, str): suffix = [suffix] for f in file if isinstance(file, (list, tuple)) else [file]: s = Path(f).suffix.lower() # file suffix if len(s): assert s in suffix, f"{msg}{f} acceptable suffix is {suffix}" def check_yaml(file, suffix=('.yaml', '.yml')): # Search/download YAML file (if necessary) and return path, checking suffix return check_file(file, suffix) def check_file(file, suffix=''): # Search/download file (if necessary) and return path check_suffix(file, suffix) # optional file = str(file) # convert to str() if Path(file).is_file() or not file: # exists return file elif file.startswith(('http:/', 'https:/')): # download url = file # warning: Pathlib turns :// -> :/ file = Path(urllib.parse.unquote(file).split('?')[0]).name # '%2F' to '/', split https://url.com/file.txt?auth if Path(file).is_file(): LOGGER.info(f'Found {url} locally at {file}') # file already exists else: LOGGER.info(f'Downloading {url} to {file}...') torch.hub.download_url_to_file(url, file) assert Path(file).exists() and Path(file).stat().st_size > 0, f'File download failed: {url}' # check return file else: # search files = [] for d in 'data', 'models', 'utils': # search directories files.extend(glob.glob(str(ROOT / d / '**' / file), recursive=True)) # find file assert len(files), f'File not found: {file}' # assert file was found assert len(files) == 1, f"Multiple files match '{file}', specify exact path: {files}" # assert unique return files[0] # return file def check_font(font=FONT, progress=False): # Download font to CONFIG_DIR if necessary font = Path(font) file = CONFIG_DIR / font.name if not font.exists() and not file.exists(): url = "https://ultralytics.com/assets/" + font.name LOGGER.info(f'Downloading {url} to {file}...') torch.hub.download_url_to_file(url, str(file), progress=progress) def check_dataset(data, autodownload=True): # Download, check and/or unzip dataset if not found locally # Download (optional) extract_dir = '' if isinstance(data, (str, Path)) and str(data).endswith('.zip'): # i.e. gs://bucket/dir/coco128.zip download(data, dir=DATASETS_DIR, unzip=True, delete=False, curl=False, threads=1) data = next((DATASETS_DIR / Path(data).stem).rglob('*.yaml')) extract_dir, autodownload = data.parent, False # Read yaml (optional) if isinstance(data, (str, Path)): with open(data, errors='ignore') as f: data = yaml.safe_load(f) # dictionary # Checks for k in 'train', 'val', 'nc': assert k in data, emojis(f"data.yaml '{k}:' field missing ❌") if 'names' not in data: LOGGER.warning(emojis("data.yaml 'names:' field missing ⚠️, assigning default names 'class0', 'class1', etc.")) data['names'] = [f'class{i}' for i in range(data['nc'])] # default names # Resolve paths path = Path(extract_dir or data.get('path') or '') # optional 'path' default to '.' if not path.is_absolute(): path = (ROOT / path).resolve() for k in 'train', 'val', 'test': if data.get(k): # prepend path data[k] = str(path / data[k]) if isinstance(data[k], str) else [str(path / x) for x in data[k]] # Parse yaml train, val, test, s = (data.get(x) for x in ('train', 'val', 'test', 'download')) if val: val = [Path(x).resolve() for x in (val if isinstance(val, list) else [val])] # val path if not all(x.exists() for x in val): LOGGER.info(emojis('\nDataset not found ⚠️, missing paths %s' % [str(x) for x in val if not x.exists()])) if not s or not autodownload: raise Exception(emojis('Dataset not found ❌')) t = time.time() root = path.parent if 'path' in data else '..' # unzip directory i.e. '../' if s.startswith('http') and s.endswith('.zip'): # URL f = Path(s).name # filename LOGGER.info(f'Downloading {s} to {f}...') torch.hub.download_url_to_file(s, f) Path(root).mkdir(parents=True, exist_ok=True) # create root ZipFile(f).extractall(path=root) # unzip Path(f).unlink() # remove zip r = None # success elif s.startswith('bash '): # bash script LOGGER.info(f'Running {s} ...') r = os.system(s) else: # python script r = exec(s, {'yaml': data}) # return None dt = f'({round(time.time() - t, 1)}s)' s = f"success ✅ {dt}, saved to {colorstr('bold', root)}" if r in (0, None) else f"failure {dt} ❌" LOGGER.info(emojis(f"Dataset download {s}")) check_font('Arial.ttf' if is_ascii(data['names']) else 'Arial.Unicode.ttf', progress=True) # download fonts return data # dictionary def check_amp(model): # Check PyTorch Automatic Mixed Precision (AMP) functionality. Return True on correct operation from asone.detectors.yolov5.yolov5.models.common import AutoShape, DetectMultiBackend def amp_allclose(model, im): # All close FP32 vs AMP results m = AutoShape(model, verbose=False) # model a = m(im).xywhn[0] # FP32 inference m.amp = True b = m(im).xywhn[0] # AMP inference return a.shape == b.shape and torch.allclose(a, b, atol=0.1) # close to 10% absolute tolerance prefix = colorstr('AMP: ') device = next(model.parameters()).device # get model device if device.type == 'cpu': return False # AMP disabled on CPU f = ROOT / 'data' / 'images' / 'bus.jpg' # image to check im = f if f.exists() else 'https://ultralytics.com/images/bus.jpg' if check_online() else np.ones((640, 640, 3)) try: assert amp_allclose(model, im) or amp_allclose(DetectMultiBackend('yolov5n.pt', device), im) LOGGER.info(emojis(f'{prefix}checks passed ✅')) return True except Exception: help_url = 'https://github.com/ultralytics/yolov5/issues/7908' LOGGER.warning(emojis(f'{prefix}checks failed ❌, disabling Automatic Mixed Precision. See {help_url}')) return False def url2file(url): # Convert URL to filename, i.e. https://url.com/file.txt?auth -> file.txt url = str(Path(url)).replace(':/', '://') # Pathlib turns :// -> :/ return Path(urllib.parse.unquote(url)).name.split('?')[0] # '%2F' to '/', split https://url.com/file.txt?auth def download(url, dir='.', unzip=True, delete=True, curl=False, threads=1, retry=3): # Multi-threaded file download and unzip function, used in data.yaml for autodownload def download_one(url, dir): # Download 1 file success = True f = dir / Path(url).name # filename if Path(url).is_file(): # exists in current path Path(url).rename(f) # move to dir elif not f.exists(): LOGGER.info(f'Downloading {url} to {f}...') for i in range(retry + 1): if curl: s = 'sS' if threads > 1 else '' # silent r = os.system(f'curl -{s}L "{url}" -o "{f}" --retry 9 -C -') # curl download with retry, continue success = r == 0 else: torch.hub.download_url_to_file(url, f, progress=threads == 1) # torch download success = f.is_file() if success: break elif i < retry: LOGGER.warning(f'Download failure, retrying {i + 1}/{retry} {url}...') else: LOGGER.warning(f'Failed to download {url}...') if unzip and success and f.suffix in ('.zip', '.gz'): LOGGER.info(f'Unzipping {f}...') if f.suffix == '.zip': ZipFile(f).extractall(path=dir) # unzip elif f.suffix == '.gz': os.system(f'tar xfz {f} --directory {f.parent}') # unzip if delete: f.unlink() # remove zip dir = Path(dir) dir.mkdir(parents=True, exist_ok=True) # make directory if threads > 1: pool = ThreadPool(threads) pool.imap(lambda x: download_one(*x), zip(url, repeat(dir))) # multi-threaded pool.close() pool.join() else: for u in [url] if isinstance(url, (str, Path)) else url: download_one(u, dir) def make_divisible(x, divisor): # Returns nearest x divisible by divisor if isinstance(divisor, torch.Tensor): divisor = int(divisor.max()) # to int return math.ceil(x / divisor) * divisor def clean_str(s): # Cleans a string by replacing special characters with underscore _ return re.sub(pattern="[|@#!¡·$€%&()=?¿^*;:,¨´><+]", repl="_", string=s) def one_cycle(y1=0.0, y2=1.0, steps=100): # lambda function for sinusoidal ramp from y1 to y2 https://arxiv.org/pdf/1812.01187.pdf return lambda x: ((1 - math.cos(x * math.pi / steps)) / 2) * (y2 - y1) + y1 def colorstr(*input): # Colors a string https://en.wikipedia.org/wiki/ANSI_escape_code, i.e. colorstr('blue', 'hello world') *args, string = input if len(input) > 1 else ('blue', 'bold', input[0]) # color arguments, string colors = { 'black': '\033[30m', # basic colors 'red': '\033[31m', 'green': '\033[32m', 'yellow': '\033[33m', 'blue': '\033[34m', 'magenta': '\033[35m', 'cyan': '\033[36m', 'white': '\033[37m', 'bright_black': '\033[90m', # bright colors 'bright_red': '\033[91m', 'bright_green': '\033[92m', 'bright_yellow': '\033[93m', 'bright_blue': '\033[94m', 'bright_magenta': '\033[95m', 'bright_cyan': '\033[96m', 'bright_white': '\033[97m', 'end': '\033[0m', # misc 'bold': '\033[1m', 'underline': '\033[4m'} return ''.join(colors[x] for x in args) + f'{string}' + colors['end'] def labels_to_class_weights(labels, nc=80): # Get class weights (inverse frequency) from training labels if labels[0] is None: # no labels loaded return torch.Tensor() labels = np.concatenate(labels, 0) # labels.shape = (866643, 5) for COCO classes = labels[:, 0].astype(int) # labels = [class xywh] weights = np.bincount(classes, minlength=nc) # occurrences per class # Prepend gridpoint count (for uCE training) # gpi = ((320 / 32 * np.array([1, 2, 4])) ** 2 * 3).sum() # gridpoints per image # weights = np.hstack([gpi * len(labels) - weights.sum() * 9, weights * 9]) ** 0.5 # prepend gridpoints to start weights[weights == 0] = 1 # replace empty bins with 1 weights = 1 / weights # number of targets per class weights /= weights.sum() # normalize return torch.from_numpy(weights).float() def labels_to_image_weights(labels, nc=80, class_weights=np.ones(80)): # Produces image weights based on class_weights and image contents # Usage: index = random.choices(range(n), weights=image_weights, k=1) # weighted image sample class_counts = np.array([np.bincount(x[:, 0].astype(int), minlength=nc) for x in labels]) return (class_weights.reshape(1, nc) * class_counts).sum(1) def coco80_to_coco91_class(): # converts 80-index (val2014) to 91-index (paper) # https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/ # a = np.loadtxt('data/coco.names', dtype='str', delimiter='\n') # b = np.loadtxt('data/coco_paper.names', dtype='str', delimiter='\n') # x1 = [list(a[i] == b).index(True) + 1 for i in range(80)] # darknet to coco # x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)] # coco to darknet return [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90] def xyxy2xywh(x): # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = (x[:, 0] + x[:, 2]) / 2 # x center y[:, 1] = (x[:, 1] + x[:, 3]) / 2 # y center y[:, 2] = x[:, 2] - x[:, 0] # width y[:, 3] = x[:, 3] - x[:, 1] # height return y def xywh2xyxy(x): # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y return y def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0): # Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = w * (x[:, 0] - x[:, 2] / 2) + padw # top left x y[:, 1] = h * (x[:, 1] - x[:, 3] / 2) + padh # top left y y[:, 2] = w * (x[:, 0] + x[:, 2] / 2) + padw # bottom right x y[:, 3] = h * (x[:, 1] + x[:, 3] / 2) + padh # bottom right y return y def xyxy2xywhn(x, w=640, h=640, clip=False, eps=0.0): # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] normalized where xy1=top-left, xy2=bottom-right if clip: clip_coords(x, (h - eps, w - eps)) # warning: inplace clip y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = ((x[:, 0] + x[:, 2]) / 2) / w # x center y[:, 1] = ((x[:, 1] + x[:, 3]) / 2) / h # y center y[:, 2] = (x[:, 2] - x[:, 0]) / w # width y[:, 3] = (x[:, 3] - x[:, 1]) / h # height return y def xyn2xy(x, w=640, h=640, padw=0, padh=0): # Convert normalized segments into pixel segments, shape (n,2) y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = w * x[:, 0] + padw # top left x y[:, 1] = h * x[:, 1] + padh # top left y return y def segment2box(segment, width=640, height=640): # Convert 1 segment label to 1 box label, applying inside-image constraint, i.e. (xy1, xy2, ...) to (xyxy) x, y = segment.T # segment xy inside = (x >= 0) & (y >= 0) & (x <= width) & (y <= height) x, y, = x[inside], y[inside] return np.array([x.min(), y.min(), x.max(), y.max()]) if any(x) else np.zeros((1, 4)) # xyxy def segments2boxes(segments): # Convert segment labels to box labels, i.e. (cls, xy1, xy2, ...) to (cls, xywh) boxes = [] for s in segments: x, y = s.T # segment xy boxes.append([x.min(), y.min(), x.max(), y.max()]) # cls, xyxy return xyxy2xywh(np.array(boxes)) # cls, xywh def resample_segments(segments, n=1000): # Up-sample an (n,2) segment for i, s in enumerate(segments): s = np.concatenate((s, s[0:1, :]), axis=0) x = np.linspace(0, len(s) - 1, n) xp = np.arange(len(s)) segments[i] = np.concatenate([np.interp(x, xp, s[:, i]) for i in range(2)]).reshape(2, -1).T # segment xy return segments def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None): # Rescale coords (xyxy) from img1_shape to img0_shape if ratio_pad is None: # calculate from img0_shape gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding else: gain = ratio_pad[0][0] pad = ratio_pad[1] coords[:, [0, 2]] -= pad[0] # x padding coords[:, [1, 3]] -= pad[1] # y padding coords[:, :4] /= gain clip_coords(coords, img0_shape) return coords def clip_coords(boxes, shape): # Clip bounding xyxy bounding boxes to image shape (height, width) if isinstance(boxes, torch.Tensor): # faster individually boxes[:, 0].clamp_(0, shape[1]) # x1 boxes[:, 1].clamp_(0, shape[0]) # y1 boxes[:, 2].clamp_(0, shape[1]) # x2 boxes[:, 3].clamp_(0, shape[0]) # y2 else: # np.array (faster grouped) boxes[:, [0, 2]] = boxes[:, [0, 2]].clip(0, shape[1]) # x1, x2 boxes[:, [1, 3]] = boxes[:, [1, 3]].clip(0, shape[0]) # y1, y2 def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False, labels=(), max_det=300): """Non-Maximum Suppression (NMS) on inference results to reject overlapping bounding boxes Returns: list of detections, on (n,6) tensor per image [xyxy, conf, cls] """ bs = prediction.shape[0] # batch size nc = prediction.shape[2] - 5 # number of classes xc = prediction[..., 4] > conf_thres # candidates # Checks assert 0 <= conf_thres <= 1, f'Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0' assert 0 <= iou_thres <= 1, f'Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0' # Settings # min_wh = 2 # (pixels) minimum box width and height max_wh = 7680 # (pixels) maximum box width and height max_nms = 30000 # maximum number of boxes into torchvision.ops.nms() time_limit = 0.3 + 0.03 * bs # seconds to quit after redundant = True # require redundant detections multi_label &= nc > 1 # multiple labels per box (adds 0.5ms/img) merge = False # use merge-NMS t = time.time() output = [torch.zeros((0, 6), device=prediction.device)] * bs for xi, x in enumerate(prediction): # image index, image inference # Apply constraints # x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height x = x[xc[xi]] # confidence # Cat apriori labels if autolabelling if labels and len(labels[xi]): lb = labels[xi] v = torch.zeros((len(lb), nc + 5), device=x.device) v[:, :4] = lb[:, 1:5] # box v[:, 4] = 1.0 # conf v[range(len(lb)), lb[:, 0].long() + 5] = 1.0 # cls x = torch.cat((x, v), 0) # If none remain process next image if not x.shape[0]: continue # Compute conf x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf # Box (center x, center y, width, height) to (x1, y1, x2, y2) box = xywh2xyxy(x[:, :4]) # Detections matrix nx6 (xyxy, conf, cls) if multi_label: i, j = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T x = torch.cat((box[i], x[i, j + 5, None], j[:, None].float()), 1) else: # best class only conf, j = x[:, 5:].max(1, keepdim=True) x = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres] # Filter by class if classes is not None: x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)] # Apply finite constraint # if not torch.isfinite(x).all(): # x = x[torch.isfinite(x).all(1)] # Check shape n = x.shape[0] # number of boxes if not n: # no boxes continue elif n > max_nms: # excess boxes x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence # Batched NMS c = x[:, 5:6] * (0 if agnostic else max_wh) # classes boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS if i.shape[0] > max_det: # limit detections i = i[:max_det] if merge and (1 < n < 3E3): # Merge NMS (boxes merged using weighted mean) # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4) iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix weights = iou * scores[None] # box weights x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes if redundant: i = i[iou.sum(1) > 1] # require redundancy output[xi] = x[i] if (time.time() - t) > time_limit: LOGGER.warning(f'WARNING: NMS time limit {time_limit:.3f}s exceeded') break # time limit exceeded return output def strip_optimizer(f='best.pt', s=''): # from utils.general import *; strip_optimizer() # Strip optimizer from 'f' to finalize training, optionally save as 's' x = torch.load(f, map_location=torch.device('cpu')) if x.get('ema'): x['model'] = x['ema'] # replace model with ema for k in 'optimizer', 'best_fitness', 'wandb_id', 'ema', 'updates': # keys x[k] = None x['epoch'] = -1 x['model'].half() # to FP16 for p in x['model'].parameters(): p.requires_grad = False torch.save(x, s or f) mb = os.path.getsize(s or f) / 1E6 # filesize LOGGER.info(f"Optimizer stripped from {f},{f' saved as {s},' if s else ''} {mb:.1f}MB") def print_mutation(results, hyp, save_dir, bucket, prefix=colorstr('evolve: ')): evolve_csv = save_dir / 'evolve.csv' evolve_yaml = save_dir / 'hyp_evolve.yaml' keys = ('metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95', 'val/box_loss', 'val/obj_loss', 'val/cls_loss') + tuple(hyp.keys()) # [results + hyps] keys = tuple(x.strip() for x in keys) vals = results + tuple(hyp.values()) n = len(keys) # Download (optional) if bucket: url = f'gs://{bucket}/evolve.csv' if gsutil_getsize(url) > (evolve_csv.stat().st_size if evolve_csv.exists() else 0): os.system(f'gsutil cp {url} {save_dir}') # download evolve.csv if larger than local # Log to evolve.csv s = '' if evolve_csv.exists() else (('%20s,' * n % keys).rstrip(',') + '\n') # add header with open(evolve_csv, 'a') as f: f.write(s + ('%20.5g,' * n % vals).rstrip(',') + '\n') # Save yaml with open(evolve_yaml, 'w') as f: data = pd.read_csv(evolve_csv) data = data.rename(columns=lambda x: x.strip()) # strip keys i = np.argmax(fitness(data.values[:, :4])) # generations = len(data) f.write('# YOLOv5 Hyperparameter Evolution Results\n' + f'# Best generation: {i}\n' + f'# Last generation: {generations - 1}\n' + '# ' + ', '.join(f'{x.strip():>20s}' for x in keys[:7]) + '\n' + '# ' + ', '.join(f'{x:>20.5g}' for x in data.values[i, :7]) + '\n\n') yaml.safe_dump(data.loc[i][7:].to_dict(), f, sort_keys=False) # Print to screen LOGGER.info(prefix + f'{generations} generations finished, current result:\n' + prefix + ', '.join(f'{x.strip():>20s}' for x in keys) + '\n' + prefix + ', '.join(f'{x:20.5g}' for x in vals) + '\n\n') if bucket: os.system(f'gsutil cp {evolve_csv} {evolve_yaml} gs://{bucket}') # upload def apply_classifier(x, model, img, im0): # Apply a second stage classifier to YOLO outputs # Example model = torchvision.models.__dict__['efficientnet_b0'](pretrained=True).to(device).eval() im0 = [im0] if isinstance(im0, np.ndarray) else im0 for i, d in enumerate(x): # per image if d is not None and len(d): d = d.clone() # Reshape and pad cutouts b = xyxy2xywh(d[:, :4]) # boxes b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1) # rectangle to square b[:, 2:] = b[:, 2:] * 1.3 + 30 # pad d[:, :4] = xywh2xyxy(b).long() # Rescale boxes from img_size to im0 size scale_coords(img.shape[2:], d[:, :4], im0[i].shape) # Classes pred_cls1 = d[:, 5].long() ims = [] for a in d: cutout = im0[i][int(a[1]):int(a[3]), int(a[0]):int(a[2])] im = cv2.resize(cutout, (224, 224)) # BGR im = im[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 im = np.ascontiguousarray(im, dtype=np.float32) # uint8 to float32 im /= 255 # 0 - 255 to 0.0 - 1.0 ims.append(im) pred_cls2 = model(torch.Tensor(ims).to(d.device)).argmax(1) # classifier prediction x[i] = x[i][pred_cls1 == pred_cls2] # retain matching class detections return x def increment_path(path, exist_ok=False, sep='', mkdir=False): # Increment file or directory path, i.e. runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc. path = Path(path) # os-agnostic if path.exists() and not exist_ok: path, suffix = (path.with_suffix(''), path.suffix) if path.is_file() else (path, '') # Method 1 for n in range(2, 9999): p = f'{path}{sep}{n}{suffix}' # increment path if not os.path.exists(p): # break path = Path(p) # Method 2 (deprecated) # dirs = glob.glob(f"{path}{sep}*") # similar paths # matches = [re.search(rf"{path.stem}{sep}(\d+)", d) for d in dirs] # i = [int(m.groups()[0]) for m in matches if m] # indices # n = max(i) + 1 if i else 2 # increment number # path = Path(f"{path}{sep}{n}{suffix}") # increment path if mkdir: path.mkdir(parents=True, exist_ok=True) # make directory return path # OpenCV Chinese-friendly functions ------------------------------------------------------------------------------------ imshow_ = cv2.imshow # copy to avoid recursion errors def imread(path, flags=cv2.IMREAD_COLOR): return cv2.imdecode(np.fromfile(path, np.uint8), flags) def imwrite(path, im): try: cv2.imencode(Path(path).suffix, im)[1].tofile(path) return True except Exception: return False def imshow(path, im): imshow_(path.encode('unicode_escape').decode(), im) cv2.imread, cv2.imwrite, cv2.imshow = imread, imwrite, imshow # redefine # Variables ------------------------------------------------------------------------------------------------------------ NCOLS = 0 if is_docker() else shutil.get_terminal_size().columns # terminal window size for tqdm ================================================ FILE: asone/detectors/yolov5/yolov5/models/tf.py ================================================ # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license """ TensorFlow, Keras and TFLite versions of YOLOv5 Authored by https://github.com/zldrobit in PR https://github.com/ultralytics/yolov5/pull/1127 Usage: $ python models/tf.py --weights yolov5s.pt Export: $ python export.py --weights yolov5s.pt --include saved_model pb tflite tfjs """ import argparse import sys from copy import deepcopy from pathlib import Path FILE = Path(__file__).resolve() ROOT = FILE.parents[1] # YOLOv5 root directory if str(ROOT) not in sys.path: sys.path.append(str(ROOT)) # add ROOT to PATH # ROOT = ROOT.relative_to(Path.cwd()) # relative import numpy as np import tensorflow as tf import torch import torch.nn as nn from tensorflow import keras from asone.detectors.yolov5.yolov5.models.common import ( C3, SPP, SPPF, Bottleneck, BottleneckCSP, C3x, Concat, Conv, CrossConv, DWConv, DWConvTranspose2d, Focus, autopad, ) from asone.detectors.yolov5.yolov5.models.experimental import MixConv2d, attempt_load from asone.detectors.yolov5.yolov5.models.yolo import Detect, Segment from asone.detectors.yolov5.yolov5.utils.activations import SiLU from asone.detectors.yolov5.yolov5.utils.general import LOGGER, make_divisible, print_args class TFBN(keras.layers.Layer): # TensorFlow BatchNormalization wrapper def __init__(self, w=None): """Initializes a TensorFlow BatchNormalization layer with optional pretrained weights.""" super().__init__() self.bn = keras.layers.BatchNormalization( beta_initializer=keras.initializers.Constant(w.bias.numpy()), gamma_initializer=keras.initializers.Constant(w.weight.numpy()), moving_mean_initializer=keras.initializers.Constant(w.running_mean.numpy()), moving_variance_initializer=keras.initializers.Constant(w.running_var.numpy()), epsilon=w.eps, ) def call(self, inputs): """Applies batch normalization to the inputs.""" return self.bn(inputs) class TFPad(keras.layers.Layer): # Pad inputs in spatial dimensions 1 and 2 def __init__(self, pad): """ Initializes a padding layer for spatial dimensions 1 and 2 with specified padding, supporting both int and tuple inputs. Inputs are """ super().__init__() if isinstance(pad, int): self.pad = tf.constant([[0, 0], [pad, pad], [pad, pad], [0, 0]]) else: # tuple/list self.pad = tf.constant([[0, 0], [pad[0], pad[0]], [pad[1], pad[1]], [0, 0]]) def call(self, inputs): """Pads input tensor with zeros using specified padding, suitable for int and tuple pad dimensions.""" return tf.pad(inputs, self.pad, mode="constant", constant_values=0) class TFConv(keras.layers.Layer): # Standard convolution def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True, w=None): """ Initializes a standard convolution layer with optional batch normalization and activation; supports only group=1. Inputs are ch_in, ch_out, weights, kernel, stride, padding, groups. """ super().__init__() assert g == 1, "TF v2.2 Conv2D does not support 'groups' argument" # TensorFlow convolution padding is inconsistent with PyTorch (e.g. k=3 s=2 'SAME' padding) # see https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch conv = keras.layers.Conv2D( filters=c2, kernel_size=k, strides=s, padding="SAME" if s == 1 else "VALID", use_bias=not hasattr(w, "bn"), kernel_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()), bias_initializer="zeros" if hasattr(w, "bn") else keras.initializers.Constant(w.conv.bias.numpy()), ) self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv]) self.bn = TFBN(w.bn) if hasattr(w, "bn") else tf.identity self.act = activations(w.act) if act else tf.identity def call(self, inputs): """Applies convolution, batch normalization, and activation function to input tensors.""" return self.act(self.bn(self.conv(inputs))) class TFDWConv(keras.layers.Layer): # Depthwise convolution def __init__(self, c1, c2, k=1, s=1, p=None, act=True, w=None): """ Initializes a depthwise convolution layer with optional batch normalization and activation for TensorFlow models. Input are ch_in, ch_out, weights, kernel, stride, padding, groups. """ super().__init__() assert c2 % c1 == 0, f"TFDWConv() output={c2} must be a multiple of input={c1} channels" conv = keras.layers.DepthwiseConv2D( kernel_size=k, depth_multiplier=c2 // c1, strides=s, padding="SAME" if s == 1 else "VALID", use_bias=not hasattr(w, "bn"), depthwise_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()), bias_initializer="zeros" if hasattr(w, "bn") else keras.initializers.Constant(w.conv.bias.numpy()), ) self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv]) self.bn = TFBN(w.bn) if hasattr(w, "bn") else tf.identity self.act = activations(w.act) if act else tf.identity def call(self, inputs): """Applies convolution, batch normalization, and activation function to input tensors.""" return self.act(self.bn(self.conv(inputs))) class TFDWConvTranspose2d(keras.layers.Layer): # Depthwise ConvTranspose2d def __init__(self, c1, c2, k=1, s=1, p1=0, p2=0, w=None): """ Initializes depthwise ConvTranspose2D layer with specific channel, kernel, stride, and padding settings. Inputs are ch_in, ch_out, weights, kernel, stride, padding, groups. """ super().__init__() assert c1 == c2, f"TFDWConv() output={c2} must be equal to input={c1} channels" assert k == 4 and p1 == 1, "TFDWConv() only valid for k=4 and p1=1" weight, bias = w.weight.permute(2, 3, 1, 0).numpy(), w.bias.numpy() self.c1 = c1 self.conv = [ keras.layers.Conv2DTranspose( filters=1, kernel_size=k, strides=s, padding="VALID", output_padding=p2, use_bias=True, kernel_initializer=keras.initializers.Constant(weight[..., i : i + 1]), bias_initializer=keras.initializers.Constant(bias[i]), ) for i in range(c1) ] def call(self, inputs): """Processes input through parallel convolutions and concatenates results, trimming border pixels.""" return tf.concat([m(x) for m, x in zip(self.conv, tf.split(inputs, self.c1, 3))], 3)[:, 1:-1, 1:-1] class TFFocus(keras.layers.Layer): # Focus wh information into c-space def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True, w=None): """ Initializes TFFocus layer to focus width and height information into channel space with custom convolution parameters. Inputs are ch_in, ch_out, kernel, stride, padding, groups. """ super().__init__() self.conv = TFConv(c1 * 4, c2, k, s, p, g, act, w.conv) def call(self, inputs): """ Performs pixel shuffling and convolution on input tensor, downsampling by 2 and expanding channels by 4. Example x(b,w,h,c) -> y(b,w/2,h/2,4c). """ inputs = [inputs[:, ::2, ::2, :], inputs[:, 1::2, ::2, :], inputs[:, ::2, 1::2, :], inputs[:, 1::2, 1::2, :]] return self.conv(tf.concat(inputs, 3)) class TFBottleneck(keras.layers.Layer): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5, w=None): """ Initializes a standard bottleneck layer for TensorFlow models, expanding and contracting channels with optional shortcut. Arguments are ch_in, ch_out, shortcut, groups, expansion. """ super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv(c_, c2, 3, 1, g=g, w=w.cv2) self.add = shortcut and c1 == c2 def call(self, inputs): """Performs forward pass; if shortcut is True & input/output channels match, adds input to the convolution result. """ return inputs + self.cv2(self.cv1(inputs)) if self.add else self.cv2(self.cv1(inputs)) class TFCrossConv(keras.layers.Layer): # Cross Convolution def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False, w=None): """Initializes cross convolution layer with optional expansion, grouping, and shortcut addition capabilities.""" super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = TFConv(c1, c_, (1, k), (1, s), w=w.cv1) self.cv2 = TFConv(c_, c2, (k, 1), (s, 1), g=g, w=w.cv2) self.add = shortcut and c1 == c2 def call(self, inputs): """Passes input through two convolutions optionally adding the input if channel dimensions match.""" return inputs + self.cv2(self.cv1(inputs)) if self.add else self.cv2(self.cv1(inputs)) class TFConv2d(keras.layers.Layer): # Substitution for PyTorch nn.Conv2D def __init__(self, c1, c2, k, s=1, g=1, bias=True, w=None): """Initializes a TensorFlow 2D convolution layer, mimicking PyTorch's nn.Conv2D functionality for given filter sizes and stride. """ super().__init__() assert g == 1, "TF v2.2 Conv2D does not support 'groups' argument" self.conv = keras.layers.Conv2D( filters=c2, kernel_size=k, strides=s, padding="VALID", use_bias=bias, kernel_initializer=keras.initializers.Constant(w.weight.permute(2, 3, 1, 0).numpy()), bias_initializer=keras.initializers.Constant(w.bias.numpy()) if bias else None, ) def call(self, inputs): """Applies a convolution operation to the inputs and returns the result.""" return self.conv(inputs) class TFBottleneckCSP(keras.layers.Layer): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None): """ Initializes CSP bottleneck layer with specified channel sizes, count, shortcut option, groups, and expansion ratio. Inputs are ch_in, ch_out, number, shortcut, groups, expansion. """ super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv2d(c1, c_, 1, 1, bias=False, w=w.cv2) self.cv3 = TFConv2d(c_, c_, 1, 1, bias=False, w=w.cv3) self.cv4 = TFConv(2 * c_, c2, 1, 1, w=w.cv4) self.bn = TFBN(w.bn) self.act = lambda x: keras.activations.swish(x) self.m = keras.Sequential([TFBottleneck(c_, c_, shortcut, g, e=1.0, w=w.m[j]) for j in range(n)]) def call(self, inputs): """Processes input through the model layers, concatenates, normalizes, activates, and reduces the output dimensions. """ y1 = self.cv3(self.m(self.cv1(inputs))) y2 = self.cv2(inputs) return self.cv4(self.act(self.bn(tf.concat((y1, y2), axis=3)))) class TFC3(keras.layers.Layer): # CSP Bottleneck with 3 convolutions def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None): """ Initializes CSP Bottleneck with 3 convolutions, supporting optional shortcuts and group convolutions. Inputs are ch_in, ch_out, number, shortcut, groups, expansion. """ super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv(c1, c_, 1, 1, w=w.cv2) self.cv3 = TFConv(2 * c_, c2, 1, 1, w=w.cv3) self.m = keras.Sequential([TFBottleneck(c_, c_, shortcut, g, e=1.0, w=w.m[j]) for j in range(n)]) def call(self, inputs): """ Processes input through a sequence of transformations for object detection (YOLOv5). See https://github.com/ultralytics/yolov5. """ return self.cv3(tf.concat((self.m(self.cv1(inputs)), self.cv2(inputs)), axis=3)) class TFC3x(keras.layers.Layer): # 3 module with cross-convolutions def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None): """ Initializes layer with cross-convolutions for enhanced feature extraction in object detection models. Inputs are ch_in, ch_out, number, shortcut, groups, expansion. """ super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv(c1, c_, 1, 1, w=w.cv2) self.cv3 = TFConv(2 * c_, c2, 1, 1, w=w.cv3) self.m = keras.Sequential( [TFCrossConv(c_, c_, k=3, s=1, g=g, e=1.0, shortcut=shortcut, w=w.m[j]) for j in range(n)] ) def call(self, inputs): """Processes input through cascaded convolutions and merges features, returning the final tensor output.""" return self.cv3(tf.concat((self.m(self.cv1(inputs)), self.cv2(inputs)), axis=3)) class TFSPP(keras.layers.Layer): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, k=(5, 9, 13), w=None): """Initializes a YOLOv3-SPP layer with specific input/output channels and kernel sizes for pooling.""" super().__init__() c_ = c1 // 2 # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv(c_ * (len(k) + 1), c2, 1, 1, w=w.cv2) self.m = [keras.layers.MaxPool2D(pool_size=x, strides=1, padding="SAME") for x in k] def call(self, inputs): """Processes input through two TFConv layers and concatenates with max-pooled outputs at intermediate stage.""" x = self.cv1(inputs) return self.cv2(tf.concat([x] + [m(x) for m in self.m], 3)) class TFSPPF(keras.layers.Layer): # Spatial pyramid pooling-Fast layer def __init__(self, c1, c2, k=5, w=None): """Initializes a fast spatial pyramid pooling layer with customizable in/out channels, kernel size, and weights. """ super().__init__() c_ = c1 // 2 # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv(c_ * 4, c2, 1, 1, w=w.cv2) self.m = keras.layers.MaxPool2D(pool_size=k, strides=1, padding="SAME") def call(self, inputs): """Executes the model's forward pass, concatenating input features with three max-pooled versions before final convolution. """ x = self.cv1(inputs) y1 = self.m(x) y2 = self.m(y1) return self.cv2(tf.concat([x, y1, y2, self.m(y2)], 3)) class TFDetect(keras.layers.Layer): # TF YOLOv5 Detect layer def __init__(self, nc=80, anchors=(), ch=(), imgsz=(640, 640), w=None): """Initializes YOLOv5 detection layer for TensorFlow with configurable classes, anchors, channels, and image size. """ super().__init__() self.stride = tf.convert_to_tensor(w.stride.numpy(), dtype=tf.float32) self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [tf.zeros(1)] * self.nl # init grid self.anchors = tf.convert_to_tensor(w.anchors.numpy(), dtype=tf.float32) self.anchor_grid = tf.reshape(self.anchors * tf.reshape(self.stride, [self.nl, 1, 1]), [self.nl, 1, -1, 1, 2]) self.m = [TFConv2d(x, self.no * self.na, 1, w=w.m[i]) for i, x in enumerate(ch)] self.training = False # set to False after building model self.imgsz = imgsz for i in range(self.nl): ny, nx = self.imgsz[0] // self.stride[i], self.imgsz[1] // self.stride[i] self.grid[i] = self._make_grid(nx, ny) def call(self, inputs): """Performs forward pass through the model layers to predict object bounding boxes and classifications.""" z = [] # inference output x = [] for i in range(self.nl): x.append(self.m[i](inputs[i])) # x(bs,20,20,255) to x(bs,3,20,20,85) ny, nx = self.imgsz[0] // self.stride[i], self.imgsz[1] // self.stride[i] x[i] = tf.reshape(x[i], [-1, ny * nx, self.na, self.no]) if not self.training: # inference y = x[i] grid = tf.transpose(self.grid[i], [0, 2, 1, 3]) - 0.5 anchor_grid = tf.transpose(self.anchor_grid[i], [0, 2, 1, 3]) * 4 xy = (tf.sigmoid(y[..., 0:2]) * 2 + grid) * self.stride[i] # xy wh = tf.sigmoid(y[..., 2:4]) ** 2 * anchor_grid # Normalize xywh to 0-1 to reduce calibration error xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32) wh /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32) y = tf.concat([xy, wh, tf.sigmoid(y[..., 4 : 5 + self.nc]), y[..., 5 + self.nc :]], -1) z.append(tf.reshape(y, [-1, self.na * ny * nx, self.no])) return tf.transpose(x, [0, 2, 1, 3]) if self.training else (tf.concat(z, 1),) @staticmethod def _make_grid(nx=20, ny=20): """Generates a 2D grid of coordinates in (x, y) format with shape [1, 1, ny*nx, 2].""" # return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() xv, yv = tf.meshgrid(tf.range(nx), tf.range(ny)) return tf.cast(tf.reshape(tf.stack([xv, yv], 2), [1, 1, ny * nx, 2]), dtype=tf.float32) class TFSegment(TFDetect): # YOLOv5 Segment head for segmentation models def __init__(self, nc=80, anchors=(), nm=32, npr=256, ch=(), imgsz=(640, 640), w=None): """Initializes YOLOv5 Segment head with specified channel depths, anchors, and input size for segmentation models. """ super().__init__(nc, anchors, ch, imgsz, w) self.nm = nm # number of masks self.npr = npr # number of protos self.no = 5 + nc + self.nm # number of outputs per anchor self.m = [TFConv2d(x, self.no * self.na, 1, w=w.m[i]) for i, x in enumerate(ch)] # output conv self.proto = TFProto(ch[0], self.npr, self.nm, w=w.proto) # protos self.detect = TFDetect.call def call(self, x): """Applies detection and proto layers on input, returning detections and optionally protos if training.""" p = self.proto(x[0]) # p = TFUpsample(None, scale_factor=4, mode='nearest')(self.proto(x[0])) # (optional) full-size protos p = tf.transpose(p, [0, 3, 1, 2]) # from shape(1,160,160,32) to shape(1,32,160,160) x = self.detect(self, x) return (x, p) if self.training else (x[0], p) class TFProto(keras.layers.Layer): def __init__(self, c1, c_=256, c2=32, w=None): """Initializes TFProto layer with convolutional and upsampling layers for feature extraction and transformation. """ super().__init__() self.cv1 = TFConv(c1, c_, k=3, w=w.cv1) self.upsample = TFUpsample(None, scale_factor=2, mode="nearest") self.cv2 = TFConv(c_, c_, k=3, w=w.cv2) self.cv3 = TFConv(c_, c2, w=w.cv3) def call(self, inputs): """Performs forward pass through the model, applying convolutions and upscaling on input tensor.""" return self.cv3(self.cv2(self.upsample(self.cv1(inputs)))) class TFUpsample(keras.layers.Layer): # TF version of torch.nn.Upsample() def __init__(self, size, scale_factor, mode, w=None): """ Initializes a TensorFlow upsampling layer with specified size, scale_factor, and mode, ensuring scale_factor is even. Warning: all arguments needed including 'w' """ super().__init__() assert scale_factor % 2 == 0, "scale_factor must be multiple of 2" self.upsample = lambda x: tf.image.resize(x, (x.shape[1] * scale_factor, x.shape[2] * scale_factor), mode) # self.upsample = keras.layers.UpSampling2D(size=scale_factor, interpolation=mode) # with default arguments: align_corners=False, half_pixel_centers=False # self.upsample = lambda x: tf.raw_ops.ResizeNearestNeighbor(images=x, # size=(x.shape[1] * 2, x.shape[2] * 2)) def call(self, inputs): """Applies upsample operation to inputs using nearest neighbor interpolation.""" return self.upsample(inputs) class TFConcat(keras.layers.Layer): # TF version of torch.concat() def __init__(self, dimension=1, w=None): """Initializes a TensorFlow layer for NCHW to NHWC concatenation, requiring dimension=1.""" super().__init__() assert dimension == 1, "convert only NCHW to NHWC concat" self.d = 3 def call(self, inputs): """Concatenates a list of tensors along the last dimension, used for NCHW to NHWC conversion.""" return tf.concat(inputs, self.d) def parse_model(d, ch, model, imgsz): """Parses a model definition dict `d` to create YOLOv5 model layers, including dynamic channel adjustments.""" LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}") anchors, nc, gd, gw, ch_mul = ( d["anchors"], d["nc"], d["depth_multiple"], d["width_multiple"], d.get("channel_multiple"), ) na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors no = na * (nc + 5) # number of outputs = anchors * (classes + 5) if not ch_mul: ch_mul = 8 layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out for i, (f, n, m, args) in enumerate(d["backbone"] + d["head"]): # from, number, module, args m_str = m m = eval(m) if isinstance(m, str) else m # eval strings for j, a in enumerate(args): try: args[j] = eval(a) if isinstance(a, str) else a # eval strings except NameError: pass n = max(round(n * gd), 1) if n > 1 else n # depth gain if m in [ nn.Conv2d, Conv, DWConv, DWConvTranspose2d, Bottleneck, SPP, SPPF, MixConv2d, Focus, CrossConv, BottleneckCSP, C3, C3x, ]: c1, c2 = ch[f], args[0] c2 = make_divisible(c2 * gw, ch_mul) if c2 != no else c2 args = [c1, c2, *args[1:]] if m in [BottleneckCSP, C3, C3x]: args.insert(2, n) n = 1 elif m is nn.BatchNorm2d: args = [ch[f]] elif m is Concat: c2 = sum(ch[-1 if x == -1 else x + 1] for x in f) elif m in [Detect, Segment]: args.append([ch[x + 1] for x in f]) if isinstance(args[1], int): # number of anchors args[1] = [list(range(args[1] * 2))] * len(f) if m is Segment: args[3] = make_divisible(args[3] * gw, ch_mul) args.append(imgsz) else: c2 = ch[f] tf_m = eval("TF" + m_str.replace("nn.", "")) m_ = ( keras.Sequential([tf_m(*args, w=model.model[i][j]) for j in range(n)]) if n > 1 else tf_m(*args, w=model.model[i]) ) # module torch_m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module t = str(m)[8:-2].replace("__main__.", "") # module type np = sum(x.numel() for x in torch_m_.parameters()) # number params m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params LOGGER.info(f"{i:>3}{str(f):>18}{str(n):>3}{np:>10} {t:<40}{str(args):<30}") # print save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist layers.append(m_) ch.append(c2) return keras.Sequential(layers), sorted(save) class TFModel: # TF YOLOv5 model def __init__(self, cfg="yolov5s.yaml", ch=3, nc=None, model=None, imgsz=(640, 640)): """Initializes TF YOLOv5 model with specified configuration, channels, classes, model instance, and input size. """ super().__init__() if isinstance(cfg, dict): self.yaml = cfg # model dict else: # is *.yaml import yaml # for torch hub self.yaml_file = Path(cfg).name with open(cfg) as f: self.yaml = yaml.load(f, Loader=yaml.FullLoader) # model dict # Define model if nc and nc != self.yaml["nc"]: LOGGER.info(f"Overriding {cfg} nc={self.yaml['nc']} with nc={nc}") self.yaml["nc"] = nc # override yaml value self.model, self.savelist = parse_model(deepcopy(self.yaml), ch=[ch], model=model, imgsz=imgsz) def predict( self, inputs, tf_nms=False, agnostic_nms=False, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, ): y = [] # outputs x = inputs for m in self.model.layers: if m.f != -1: # if not from previous layer x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers x = m(x) # run y.append(x if m.i in self.savelist else None) # save output # Add TensorFlow NMS if tf_nms: boxes = self._xywh2xyxy(x[0][..., :4]) probs = x[0][:, :, 4:5] classes = x[0][:, :, 5:] scores = probs * classes if agnostic_nms: nms = AgnosticNMS()((boxes, classes, scores), topk_all, iou_thres, conf_thres) else: boxes = tf.expand_dims(boxes, 2) nms = tf.image.combined_non_max_suppression( boxes, scores, topk_per_class, topk_all, iou_thres, conf_thres, clip_boxes=False ) return (nms,) return x # output [1,6300,85] = [xywh, conf, class0, class1, ...] # x = x[0] # [x(1,6300,85), ...] to x(6300,85) # xywh = x[..., :4] # x(6300,4) boxes # conf = x[..., 4:5] # x(6300,1) confidences # cls = tf.reshape(tf.cast(tf.argmax(x[..., 5:], axis=1), tf.float32), (-1, 1)) # x(6300,1) classes # return tf.concat([conf, cls, xywh], 1) @staticmethod def _xywh2xyxy(xywh): """Converts bounding box format from [x, y, w, h] to [x1, y1, x2, y2], where xy1=top-left and xy2=bottom- right. """ x, y, w, h = tf.split(xywh, num_or_size_splits=4, axis=-1) return tf.concat([x - w / 2, y - h / 2, x + w / 2, y + h / 2], axis=-1) class AgnosticNMS(keras.layers.Layer): # TF Agnostic NMS def call(self, input, topk_all, iou_thres, conf_thres): """Performs agnostic NMS on input tensors using given thresholds and top-K selection.""" return tf.map_fn( lambda x: self._nms(x, topk_all, iou_thres, conf_thres), input, fn_output_signature=(tf.float32, tf.float32, tf.float32, tf.int32), name="agnostic_nms", ) @staticmethod def _nms(x, topk_all=100, iou_thres=0.45, conf_thres=0.25): """Performs agnostic non-maximum suppression (NMS) on detected objects, filtering based on IoU and confidence thresholds. """ boxes, classes, scores = x class_inds = tf.cast(tf.argmax(classes, axis=-1), tf.float32) scores_inp = tf.reduce_max(scores, -1) selected_inds = tf.image.non_max_suppression( boxes, scores_inp, max_output_size=topk_all, iou_threshold=iou_thres, score_threshold=conf_thres ) selected_boxes = tf.gather(boxes, selected_inds) padded_boxes = tf.pad( selected_boxes, paddings=[[0, topk_all - tf.shape(selected_boxes)[0]], [0, 0]], mode="CONSTANT", constant_values=0.0, ) selected_scores = tf.gather(scores_inp, selected_inds) padded_scores = tf.pad( selected_scores, paddings=[[0, topk_all - tf.shape(selected_boxes)[0]]], mode="CONSTANT", constant_values=-1.0, ) selected_classes = tf.gather(class_inds, selected_inds) padded_classes = tf.pad( selected_classes, paddings=[[0, topk_all - tf.shape(selected_boxes)[0]]], mode="CONSTANT", constant_values=-1.0, ) valid_detections = tf.shape(selected_inds)[0] return padded_boxes, padded_scores, padded_classes, valid_detections def activations(act=nn.SiLU): """Converts PyTorch activations to TensorFlow equivalents, supporting LeakyReLU, Hardswish, and SiLU/Swish.""" if isinstance(act, nn.LeakyReLU): return lambda x: keras.activations.relu(x, alpha=0.1) elif isinstance(act, nn.Hardswish): return lambda x: x * tf.nn.relu6(x + 3) * 0.166666667 elif isinstance(act, (nn.SiLU, SiLU)): return lambda x: keras.activations.swish(x) else: raise Exception(f"no matching TensorFlow activation found for PyTorch activation {act}") def representative_dataset_gen(dataset, ncalib=100): """Generates a representative dataset for calibration by yielding transformed numpy arrays from the input dataset. """ for n, (path, img, im0s, vid_cap, string) in enumerate(dataset): im = np.transpose(img, [1, 2, 0]) im = np.expand_dims(im, axis=0).astype(np.float32) im /= 255 yield [im] if n >= ncalib: break def run( weights=ROOT / "yolov5s.pt", # weights path imgsz=(640, 640), # inference size h,w batch_size=1, # batch size dynamic=False, # dynamic batch size ): # PyTorch model im = torch.zeros((batch_size, 3, *imgsz)) # BCHW image model = attempt_load(weights, device=torch.device("cpu"), inplace=True, fuse=False) _ = model(im) # inference model.info() # TensorFlow model im = tf.zeros((batch_size, *imgsz, 3)) # BHWC image tf_model = TFModel(cfg=model.yaml, model=model, nc=model.nc, imgsz=imgsz) _ = tf_model.predict(im) # inference # Keras model im = keras.Input(shape=(*imgsz, 3), batch_size=None if dynamic else batch_size) keras_model = keras.Model(inputs=im, outputs=tf_model.predict(im)) keras_model.summary() LOGGER.info("PyTorch, TensorFlow and Keras models successfully verified.\nUse export.py for TF model export.") def parse_opt(): """Parses and returns command-line options for model inference, including weights path, image size, batch size, and dynamic batching. """ parser = argparse.ArgumentParser() parser.add_argument("--weights", type=str, default=ROOT / "yolov5s.pt", help="weights path") parser.add_argument("--imgsz", "--img", "--img-size", nargs="+", type=int, default=[640], help="inference size h,w") parser.add_argument("--batch-size", type=int, default=1, help="batch size") parser.add_argument("--dynamic", action="store_true", help="dynamic batch size") opt = parser.parse_args() opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand print_args(vars(opt)) return opt def main(opt): """Executes the YOLOv5 model run function with parsed command line options.""" run(**vars(opt)) if __name__ == "__main__": opt = parse_opt() main(opt) ================================================ FILE: asone/detectors/yolov5/yolov5/models/yolo.py ================================================ # YOLOv5 🚀 by Ultralytics, GPL-3.0 license """ YOLO-specific modules Usage: $ python path/to/models/yolo.py --cfg yolov5s.yaml """ import argparse import contextlib import os import platform import sys from copy import deepcopy from pathlib import Path FILE = Path(__file__).resolve() ROOT = FILE.parents[1] # YOLOv5 root directory # if str(ROOT) not in sys.path: # sys.path.append(str(ROOT)) # add ROOT to PATH if platform.system() != 'Windows': ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative from asone.detectors.yolov5.yolov5.models.common import * from asone.detectors.yolov5.yolov5.models.experimental import * from asone.detectors.yolov5.yolov5.models.general import (LOGGER, check_version, check_yaml, make_divisible, print_args) from asone.detectors.yolov5.yolov5.utils.torch_utils import ( fuse_conv_and_bn, initialize_weights, model_info, profile, scale_img, select_device, time_sync) try: import thop # for FLOPs computation except ImportError: thop = None class Detect(nn.Module): stride = None # strides computed during build onnx_dynamic = False # ONNX export parameter export = False # export mode def __init__(self, nc=80, anchors=(), ch=(), inplace=True): # detection layer super().__init__() self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid self.anchor_grid = [torch.zeros(1)] * self.nl # init anchor grid self.register_buffer('anchors', torch.tensor(anchors).float().view(self.nl, -1, 2)) # shape(nl,na,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv self.inplace = inplace # use inplace ops (e.g. slice assignment) def forward(self, x): z = [] # inference output for i in range(self.nl): x[i] = self.m[i](x[i]) # conv bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.onnx_dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i) y = x[i].sigmoid() if self.inplace: y[..., 0:2] = (y[..., 0:2] * 2 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953 xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # y.tensor_split((2, 4, 5), 4) # torch 1.8.0 xy = (xy * 2 + self.grid[i]) * self.stride[i] # xy wh = (wh * 2) ** 2 * self.anchor_grid[i] # wh y = torch.cat((xy, wh, conf), 4) z.append(y.view(bs, -1, self.no)) return x if self.training else (torch.cat(z, 1),) if self.export else (torch.cat(z, 1), x) def _make_grid(self, nx=20, ny=20, i=0): d = self.anchors[i].device t = self.anchors[i].dtype shape = 1, self.na, ny, nx, 2 # grid shape y, x = torch.arange(ny, device=d, dtype=t), torch.arange(nx, device=d, dtype=t) if check_version(torch.__version__, '1.10.0'): # torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility yv, xv = torch.meshgrid(y, x, indexing='ij') else: yv, xv = torch.meshgrid(y, x) grid = torch.stack((xv, yv), 2).expand(shape) - 0.5 # add grid offset, i.e. y = 2.0 * x - 0.5 anchor_grid = (self.anchors[i] * self.stride[i]).view((1, self.na, 1, 1, 2)).expand(shape) return grid, anchor_grid class DetectionModel(nn.Module): # YOLOv5 model def __init__(self, cfg='yolov5s.yaml', ch=3, nc=None, anchors=None): # model, input channels, number of classes super().__init__() if isinstance(cfg, dict): self.yaml = cfg # model dict else: # is *.yaml import yaml # for torch hub self.yaml_file = Path(cfg).name with open(cfg, encoding='ascii', errors='ignore') as f: self.yaml = yaml.safe_load(f) # model dict # Define model ch = self.yaml['ch'] = self.yaml.get('ch', ch) # input channels if nc and nc != self.yaml['nc']: LOGGER.info(f"Overriding model.yaml nc={self.yaml['nc']} with nc={nc}") self.yaml['nc'] = nc # override yaml value if anchors: LOGGER.info(f'Overriding model.yaml anchors with anchors={anchors}') self.yaml['anchors'] = round(anchors) # override yaml value self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist self.names = [str(i) for i in range(self.yaml['nc'])] # default names self.inplace = self.yaml.get('inplace', True) # Build strides, anchors m = self.model[-1] # Detect() if isinstance(m, Detect): s = 256 # 2x min stride m.inplace = self.inplace m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward check_anchor_order(m) # must be in pixel-space (not grid-space) m.anchors /= m.stride.view(-1, 1, 1) self.stride = m.stride self._initialize_biases() # only run once # Init weights, biases initialize_weights(self) self.info() LOGGER.info('') def forward(self, x, augment=False, profile=False, visualize=False): if augment: return self._forward_augment(x) # augmented inference, None return self._forward_once(x, profile, visualize) # single-scale inference, train def _forward_augment(self, x): img_size = x.shape[-2:] # height, width s = [1, 0.83, 0.67] # scales f = [None, 3, None] # flips (2-ud, 3-lr) y = [] # outputs for si, fi in zip(s, f): xi = scale_img(x.flip(fi) if fi else x, si, gs=int(self.stride.max())) yi = self._forward_once(xi)[0] # forward # cv2.imwrite(f'img_{si}.jpg', 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1]) # save yi = self._descale_pred(yi, fi, si, img_size) y.append(yi) y = self._clip_augmented(y) # clip augmented tails return torch.cat(y, 1), None # augmented inference, train def _forward_once(self, x, profile=False, visualize=False): y, dt = [], [] # outputs for m in self.model: if m.f != -1: # if not from previous layer x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers if profile: self._profile_one_layer(m, x, dt) x = m(x) # run y.append(x if m.i in self.save else None) # save output if visualize: feature_visualization(x, m.type, m.i, save_dir=visualize) return x def _descale_pred(self, p, flips, scale, img_size): # de-scale predictions following augmented inference (inverse operation) if self.inplace: p[..., :4] /= scale # de-scale if flips == 2: p[..., 1] = img_size[0] - p[..., 1] # de-flip ud elif flips == 3: p[..., 0] = img_size[1] - p[..., 0] # de-flip lr else: x, y, wh = p[..., 0:1] / scale, p[..., 1:2] / scale, p[..., 2:4] / scale # de-scale if flips == 2: y = img_size[0] - y # de-flip ud elif flips == 3: x = img_size[1] - x # de-flip lr p = torch.cat((x, y, wh, p[..., 4:]), -1) return p def _clip_augmented(self, y): # Clip YOLOv5 augmented inference tails nl = self.model[-1].nl # number of detection layers (P3-P5) g = sum(4 ** x for x in range(nl)) # grid points e = 1 # exclude layer count i = (y[0].shape[1] // g) * sum(4 ** x for x in range(e)) # indices y[0] = y[0][:, :-i] # large i = (y[-1].shape[1] // g) * sum(4 ** (nl - 1 - x) for x in range(e)) # indices y[-1] = y[-1][:, i:] # small return y def _profile_one_layer(self, m, x, dt): c = isinstance(m, Detect) # is final layer, copy input as inplace fix o = thop.profile(m, inputs=(x.copy() if c else x,), verbose=False)[0] / 1E9 * 2 if thop else 0 # FLOPs t = time_sync() for _ in range(10): m(x.copy() if c else x) dt.append((time_sync() - t) * 100) if m == self.model[0]: LOGGER.info(f"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s} module") LOGGER.info(f'{dt[-1]:10.2f} {o:10.2f} {m.np:10.0f} {m.type}') if c: LOGGER.info(f"{sum(dt):10.2f} {'-':>10s} {'-':>10s} Total") def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency # https://arxiv.org/abs/1708.02002 section 3.3 # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. m = self.model[-1] # Detect() module for mi, s in zip(m.m, m.stride): # from b = mi.bias.view(m.na, -1).detach() # conv.bias(255) to (3,85) b[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image) b[:, 5:] += math.log(0.6 / (m.nc - 0.999999)) if cf is None else torch.log(cf / cf.sum()) # cls mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) def _print_biases(self): m = self.model[-1] # Detect() module for mi in m.m: # from b = mi.bias.detach().view(m.na, -1).T # conv.bias(255) to (3,85) LOGGER.info( ('%6g Conv2d.bias:' + '%10.3g' * 6) % (mi.weight.shape[1], *b[:5].mean(1).tolist(), b[5:].mean())) def _print_weights(self): for m in self.model.modules(): if type(m) is Bottleneck: LOGGER.info('%10.3g' % (m.w.detach().sigmoid() * 2)) # shortcut weights def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers # LOGGER.info('Fusing layers... ') for m in self.model.modules(): if isinstance(m, (Conv, DWConv)) and hasattr(m, 'bn'): m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv delattr(m, 'bn') # remove batchnorm m.forward = m.forward_fuse # update forward # self.info() return self def info(self, verbose=False, img_size=640): # print model information model_info(self, verbose, img_size) def _apply(self, fn): # Apply to(), cpu(), cuda(), half() to model tensors that are not parameters or registered buffers self = super()._apply(fn) m = self.model[-1] # Detect() if isinstance(m, Detect): m.stride = fn(m.stride) m.grid = list(map(fn, m.grid)) if isinstance(m.anchor_grid, list): m.anchor_grid = list(map(fn, m.anchor_grid)) return self Model = DetectionModel # retain YOLOv5 'Model' class for backwards compatibility def parse_model(d, ch): # model_dict, input_channels(3) LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}") anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple'] na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors no = na * (nc + 5) # number of outputs = anchors * (classes + 5) layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number, module, args m = eval(m) if isinstance(m, str) else m # eval strings for j, a in enumerate(args): with contextlib.suppress(NameError): args[j] = eval(a) if isinstance(a, str) else a # eval strings n = n_ = max(round(n * gd), 1) if n > 1 else n # depth gain if m in (Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv, BottleneckCSP, C3, C3TR, C3SPP, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x): c1, c2 = ch[f], args[0] if c2 != no: # if not output c2 = make_divisible(c2 * gw, 8) args = [c1, c2, *args[1:]] if m in [BottleneckCSP, C3, C3TR, C3Ghost, C3x]: args.insert(2, n) # number of repeats n = 1 elif m is nn.BatchNorm2d: args = [ch[f]] elif m is Concat: c2 = sum(ch[x] for x in f) elif m is Detect: args.append([ch[x] for x in f]) if isinstance(args[1], int): # number of anchors args[1] = [list(range(args[1] * 2))] * len(f) elif m is Contract: c2 = ch[f] * args[0] ** 2 elif m is Expand: c2 = ch[f] // args[0] ** 2 else: c2 = ch[f] m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module t = str(m)[8:-2].replace('__main__.', '') # module type np = sum(x.numel() for x in m_.parameters()) # number params m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params LOGGER.info(f'{i:>3}{str(f):>18}{n_:>3}{np:10.0f} {t:<40}{str(args):<30}') # print save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist layers.append(m_) if i == 0: ch = [] ch.append(c2) return nn.Sequential(*layers), sorted(save) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--cfg', type=str, default='yolov5s.yaml', help='model.yaml') parser.add_argument('--batch-size', type=int, default=1, help='total batch size for all GPUs') parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') parser.add_argument('--profile', action='store_true', help='profile model speed') parser.add_argument('--line-profile', action='store_true', help='profile model speed layer by layer') parser.add_argument('--test', action='store_true', help='test all yolo*.yaml') opt = parser.parse_args() opt.cfg = check_yaml(opt.cfg) # check YAML print_args(vars(opt)) device = select_device(opt.device) # Create model im = torch.rand(opt.batch_size, 3, 640, 640).to(device) model = Model(opt.cfg).to(device) # Options if opt.line_profile: # profile layer by layer _ = model(im, profile=True) elif opt.profile: # profile forward-backward results = profile(input=im, ops=[model], n=3) elif opt.test: # test all models for cfg in Path(ROOT / 'models').rglob('yolo*.yaml'): try: _ = Model(cfg) except Exception as e: print(f'Error in {cfg}: {e}') else: # report fused model summary model.fuse() ================================================ FILE: asone/detectors/yolov5/yolov5/utils/__init__.py ================================================ ================================================ FILE: asone/detectors/yolov5/yolov5/utils/activations.py ================================================ # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license """Activation functions.""" import torch import torch.nn as nn import torch.nn.functional as F class SiLU(nn.Module): @staticmethod def forward(x): """ Applies the Sigmoid-weighted Linear Unit (SiLU) activation function. https://arxiv.org/pdf/1606.08415.pdf. """ return x * torch.sigmoid(x) class Hardswish(nn.Module): @staticmethod def forward(x): """ Applies the Hardswish activation function, compatible with TorchScript, CoreML, and ONNX. Equivalent to x * F.hardsigmoid(x) """ return x * F.hardtanh(x + 3, 0.0, 6.0) / 6.0 # for TorchScript, CoreML and ONNX class Mish(nn.Module): """Mish activation https://github.com/digantamisra98/Mish.""" @staticmethod def forward(x): """Applies the Mish activation function, a smooth alternative to ReLU.""" return x * F.softplus(x).tanh() class MemoryEfficientMish(nn.Module): class F(torch.autograd.Function): @staticmethod def forward(ctx, x): """Applies the Mish activation function, a smooth ReLU alternative, to the input tensor `x`.""" ctx.save_for_backward(x) return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x))) @staticmethod def backward(ctx, grad_output): """Computes the gradient of the Mish activation function with respect to input `x`.""" x = ctx.saved_tensors[0] sx = torch.sigmoid(x) fx = F.softplus(x).tanh() return grad_output * (fx + x * sx * (1 - fx * fx)) def forward(self, x): """Applies the Mish activation function to the input tensor `x`.""" return self.F.apply(x) class FReLU(nn.Module): """FReLU activation https://arxiv.org/abs/2007.11824.""" def __init__(self, c1, k=3): # ch_in, kernel """Initializes FReLU activation with channel `c1` and kernel size `k`.""" super().__init__() self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False) self.bn = nn.BatchNorm2d(c1) def forward(self, x): """ Applies FReLU activation with max operation between input and BN-convolved input. https://arxiv.org/abs/2007.11824 """ return torch.max(x, self.bn(self.conv(x))) class AconC(nn.Module): """ ACON activation (activate or not) function. AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter See "Activate or Not: Learning Customized Activation" https://arxiv.org/pdf/2009.04759.pdf. """ def __init__(self, c1): """Initializes AconC with learnable parameters p1, p2, and beta for channel-wise activation control.""" super().__init__() self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1)) self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1)) self.beta = nn.Parameter(torch.ones(1, c1, 1, 1)) def forward(self, x): """Applies AconC activation function with learnable parameters for channel-wise control on input tensor x.""" dpx = (self.p1 - self.p2) * x return dpx * torch.sigmoid(self.beta * dpx) + self.p2 * x class MetaAconC(nn.Module): """ ACON activation (activate or not) function. AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter See "Activate or Not: Learning Customized Activation" https://arxiv.org/pdf/2009.04759.pdf. """ def __init__(self, c1, k=1, s=1, r=16): """Initializes MetaAconC with params: channel_in (c1), kernel size (k=1), stride (s=1), reduction (r=16).""" super().__init__() c2 = max(r, c1 // r) self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1)) self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1)) self.fc1 = nn.Conv2d(c1, c2, k, s, bias=True) self.fc2 = nn.Conv2d(c2, c1, k, s, bias=True) # self.bn1 = nn.BatchNorm2d(c2) # self.bn2 = nn.BatchNorm2d(c1) def forward(self, x): """Applies a forward pass transforming input `x` using learnable parameters and sigmoid activation.""" y = x.mean(dim=2, keepdims=True).mean(dim=3, keepdims=True) # batch-size 1 bug/instabilities https://github.com/ultralytics/yolov5/issues/2891 # beta = torch.sigmoid(self.bn2(self.fc2(self.bn1(self.fc1(y))))) # bug/unstable beta = torch.sigmoid(self.fc2(self.fc1(y))) # bug patch BN layers removed dpx = (self.p1 - self.p2) * x return dpx * torch.sigmoid(beta * dpx) + self.p2 * x ================================================ FILE: asone/detectors/yolov5/yolov5/utils/augmentations.py ================================================ # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license """Image augmentation functions.""" import math import random import cv2 import numpy as np import torch import torchvision.transforms as T import torchvision.transforms.functional as TF from asone.detectors.yolov5.yolov5.utils.general import LOGGER, check_version, colorstr, resample_segments, segment2box, xywhn2xyxy from asone.detectors.yolov5.yolov5.utils.metrics import bbox_ioa IMAGENET_MEAN = 0.485, 0.456, 0.406 # RGB mean IMAGENET_STD = 0.229, 0.224, 0.225 # RGB standard deviation class Albumentations: # YOLOv5 Albumentations class (optional, only used if package is installed) def __init__(self, size=640): """Initializes Albumentations class for optional data augmentation in YOLOv5 with specified input size.""" self.transform = None prefix = colorstr("albumentations: ") try: import albumentations as A check_version(A.__version__, "1.0.3", hard=True) # version requirement T = [ A.RandomResizedCrop(height=size, width=size, scale=(0.8, 1.0), ratio=(0.9, 1.11), p=0.0), A.Blur(p=0.01), A.MedianBlur(p=0.01), A.ToGray(p=0.01), A.CLAHE(p=0.01), A.RandomBrightnessContrast(p=0.0), A.RandomGamma(p=0.0), A.ImageCompression(quality_lower=75, p=0.0), ] # transforms self.transform = A.Compose(T, bbox_params=A.BboxParams(format="yolo", label_fields=["class_labels"])) LOGGER.info(prefix + ", ".join(f"{x}".replace("always_apply=False, ", "") for x in T if x.p)) except ImportError: # package not installed, skip pass except Exception as e: LOGGER.info(f"{prefix}{e}") def __call__(self, im, labels, p=1.0): """Applies transformations to an image and labels with probability `p`, returning updated image and labels.""" if self.transform and random.random() < p: new = self.transform(image=im, bboxes=labels[:, 1:], class_labels=labels[:, 0]) # transformed im, labels = new["image"], np.array([[c, *b] for c, b in zip(new["class_labels"], new["bboxes"])]) return im, labels def normalize(x, mean=IMAGENET_MEAN, std=IMAGENET_STD, inplace=False): """ Applies ImageNet normalization to RGB images in BCHW format, modifying them in-place if specified. Example: y = (x - mean) / std """ return TF.normalize(x, mean, std, inplace=inplace) def denormalize(x, mean=IMAGENET_MEAN, std=IMAGENET_STD): """Reverses ImageNet normalization for BCHW format RGB images by applying `x = x * std + mean`.""" for i in range(3): x[:, i] = x[:, i] * std[i] + mean[i] return x def augment_hsv(im, hgain=0.5, sgain=0.5, vgain=0.5): """Applies HSV color-space augmentation to an image with random gains for hue, saturation, and value.""" if hgain or sgain or vgain: r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains hue, sat, val = cv2.split(cv2.cvtColor(im, cv2.COLOR_BGR2HSV)) dtype = im.dtype # uint8 x = np.arange(0, 256, dtype=r.dtype) lut_hue = ((x * r[0]) % 180).astype(dtype) lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) lut_val = np.clip(x * r[2], 0, 255).astype(dtype) im_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))) cv2.cvtColor(im_hsv, cv2.COLOR_HSV2BGR, dst=im) # no return needed def hist_equalize(im, clahe=True, bgr=False): """Equalizes image histogram, with optional CLAHE, for BGR or RGB image with shape (n,m,3) and range 0-255.""" yuv = cv2.cvtColor(im, cv2.COLOR_BGR2YUV if bgr else cv2.COLOR_RGB2YUV) if clahe: c = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) yuv[:, :, 0] = c.apply(yuv[:, :, 0]) else: yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0]) # equalize Y channel histogram return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR if bgr else cv2.COLOR_YUV2RGB) # convert YUV image to RGB def replicate(im, labels): """ Replicates half of the smallest object labels in an image for data augmentation. Returns augmented image and labels. """ h, w = im.shape[:2] boxes = labels[:, 1:].astype(int) x1, y1, x2, y2 = boxes.T s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels) for i in s.argsort()[: round(s.size * 0.5)]: # smallest indices x1b, y1b, x2b, y2b = boxes[i] bh, bw = y2b - y1b, x2b - x1b yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh] im[y1a:y2a, x1a:x2a] = im[y1b:y2b, x1b:x2b] # im4[ymin:ymax, xmin:xmax] labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0) return im, labels def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32): """Resizes and pads image to new_shape with stride-multiple constraints, returns resized image, ratio, padding.""" shape = im.shape[:2] # current shape [height, width] if isinstance(new_shape, int): new_shape = (new_shape, new_shape) # Scale ratio (new / old) r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) if not scaleup: # only scale down, do not scale up (for better val mAP) r = min(r, 1.0) # Compute padding ratio = r, r # width, height ratios new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding if auto: # minimum rectangle dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding elif scaleFill: # stretch dw, dh = 0.0, 0.0 new_unpad = (new_shape[1], new_shape[0]) ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios dw /= 2 # divide padding into 2 sides dh /= 2 if shape[::-1] != new_unpad: # resize im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border return im, ratio, (dw, dh) def random_perspective( im, targets=(), segments=(), degrees=10, translate=0.1, scale=0.1, shear=10, perspective=0.0, border=(0, 0) ): # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(0.1, 0.1), scale=(0.9, 1.1), shear=(-10, 10)) # targets = [cls, xyxy] height = im.shape[0] + border[0] * 2 # shape(h,w,c) width = im.shape[1] + border[1] * 2 # Center C = np.eye(3) C[0, 2] = -im.shape[1] / 2 # x translation (pixels) C[1, 2] = -im.shape[0] / 2 # y translation (pixels) # Perspective P = np.eye(3) P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y) P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x) # Rotation and Scale R = np.eye(3) a = random.uniform(-degrees, degrees) # a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations s = random.uniform(1 - scale, 1 + scale) # s = 2 ** random.uniform(-scale, scale) R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s) # Shear S = np.eye(3) S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg) S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg) # Translation T = np.eye(3) T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels) T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels) # Combined rotation matrix M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed if perspective: im = cv2.warpPerspective(im, M, dsize=(width, height), borderValue=(114, 114, 114)) else: # affine im = cv2.warpAffine(im, M[:2], dsize=(width, height), borderValue=(114, 114, 114)) # Visualize # import matplotlib.pyplot as plt # ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel() # ax[0].imshow(im[:, :, ::-1]) # base # ax[1].imshow(im2[:, :, ::-1]) # warped # Transform label coordinates n = len(targets) if n: use_segments = any(x.any() for x in segments) and len(segments) == n new = np.zeros((n, 4)) if use_segments: # warp segments segments = resample_segments(segments) # upsample for i, segment in enumerate(segments): xy = np.ones((len(segment), 3)) xy[:, :2] = segment xy = xy @ M.T # transform xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2] # perspective rescale or affine # clip new[i] = segment2box(xy, width, height) else: # warp boxes xy = np.ones((n * 4, 3)) xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1 xy = xy @ M.T # transform xy = (xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]).reshape(n, 8) # perspective rescale or affine # create new boxes x = xy[:, [0, 2, 4, 6]] y = xy[:, [1, 3, 5, 7]] new = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T # clip new[:, [0, 2]] = new[:, [0, 2]].clip(0, width) new[:, [1, 3]] = new[:, [1, 3]].clip(0, height) # filter candidates i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01 if use_segments else 0.10) targets = targets[i] targets[:, 1:5] = new[i] return im, targets def copy_paste(im, labels, segments, p=0.5): """ Applies Copy-Paste augmentation by flipping and merging segments and labels on an image. Details at https://arxiv.org/abs/2012.07177. """ n = len(segments) if p and n: h, w, c = im.shape # height, width, channels im_new = np.zeros(im.shape, np.uint8) for j in random.sample(range(n), k=round(p * n)): l, s = labels[j], segments[j] box = w - l[3], l[2], w - l[1], l[4] ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area if (ioa < 0.30).all(): # allow 30% obscuration of existing labels labels = np.concatenate((labels, [[l[0], *box]]), 0) segments.append(np.concatenate((w - s[:, 0:1], s[:, 1:2]), 1)) cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (1, 1, 1), cv2.FILLED) result = cv2.flip(im, 1) # augment segments (flip left-right) i = cv2.flip(im_new, 1).astype(bool) im[i] = result[i] # cv2.imwrite('debug.jpg', im) # debug return im, labels, segments def cutout(im, labels, p=0.5): """ Applies cutout augmentation to an image with optional label adjustment, using random masks of varying sizes. Details at https://arxiv.org/abs/1708.04552. """ if random.random() < p: h, w = im.shape[:2] scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16 # image size fraction for s in scales: mask_h = random.randint(1, int(h * s)) # create random masks mask_w = random.randint(1, int(w * s)) # box xmin = max(0, random.randint(0, w) - mask_w // 2) ymin = max(0, random.randint(0, h) - mask_h // 2) xmax = min(w, xmin + mask_w) ymax = min(h, ymin + mask_h) # apply random color mask im[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)] # return unobscured labels if len(labels) and s > 0.03: box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32) ioa = bbox_ioa(box, xywhn2xyxy(labels[:, 1:5], w, h)) # intersection over area labels = labels[ioa < 0.60] # remove >60% obscured labels return labels def mixup(im, labels, im2, labels2): """ Applies MixUp augmentation by blending images and labels. See https://arxiv.org/pdf/1710.09412.pdf for details. """ r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0 im = (im * r + im2 * (1 - r)).astype(np.uint8) labels = np.concatenate((labels, labels2), 0) return im, labels def box_candidates(box1, box2, wh_thr=2, ar_thr=100, area_thr=0.1, eps=1e-16): """ Filters bounding box candidates by minimum width-height threshold `wh_thr` (pixels), aspect ratio threshold `ar_thr`, and area ratio threshold `area_thr`. box1(4,n) is before augmentation, box2(4,n) is after augmentation. """ w1, h1 = box1[2] - box1[0], box1[3] - box1[1] w2, h2 = box2[2] - box2[0], box2[3] - box2[1] ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps)) # aspect ratio return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr) # candidates def classify_albumentations( augment=True, size=224, scale=(0.08, 1.0), ratio=(0.75, 1.0 / 0.75), # 0.75, 1.33 hflip=0.5, vflip=0.0, jitter=0.4, mean=IMAGENET_MEAN, std=IMAGENET_STD, auto_aug=False, ): # YOLOv5 classification Albumentations (optional, only used if package is installed) prefix = colorstr("albumentations: ") try: import albumentations as A from albumentations.pytorch import ToTensorV2 check_version(A.__version__, "1.0.3", hard=True) # version requirement if augment: # Resize and crop T = [A.RandomResizedCrop(height=size, width=size, scale=scale, ratio=ratio)] if auto_aug: # TODO: implement AugMix, AutoAug & RandAug in albumentation LOGGER.info(f"{prefix}auto augmentations are currently not supported") else: if hflip > 0: T += [A.HorizontalFlip(p=hflip)] if vflip > 0: T += [A.VerticalFlip(p=vflip)] if jitter > 0: color_jitter = (float(jitter),) * 3 # repeat value for brightness, contrast, satuaration, 0 hue T += [A.ColorJitter(*color_jitter, 0)] else: # Use fixed crop for eval set (reproducibility) T = [A.SmallestMaxSize(max_size=size), A.CenterCrop(height=size, width=size)] T += [A.Normalize(mean=mean, std=std), ToTensorV2()] # Normalize and convert to Tensor LOGGER.info(prefix + ", ".join(f"{x}".replace("always_apply=False, ", "") for x in T if x.p)) return A.Compose(T) except ImportError: # package not installed, skip LOGGER.warning(f"{prefix}⚠️ not found, install with `pip install albumentations` (recommended)") except Exception as e: LOGGER.info(f"{prefix}{e}") def classify_transforms(size=224): """Applies a series of transformations including center crop, ToTensor, and normalization for classification.""" assert isinstance(size, int), f"ERROR: classify_transforms size {size} must be integer, not (list, tuple)" # T.Compose([T.ToTensor(), T.Resize(size), T.CenterCrop(size), T.Normalize(IMAGENET_MEAN, IMAGENET_STD)]) return T.Compose([CenterCrop(size), ToTensor(), T.Normalize(IMAGENET_MEAN, IMAGENET_STD)]) class LetterBox: # YOLOv5 LetterBox class for image preprocessing, i.e. T.Compose([LetterBox(size), ToTensor()]) def __init__(self, size=(640, 640), auto=False, stride=32): """Initializes a LetterBox object for YOLOv5 image preprocessing with optional auto sizing and stride adjustment. """ super().__init__() self.h, self.w = (size, size) if isinstance(size, int) else size self.auto = auto # pass max size integer, automatically solve for short side using stride self.stride = stride # used with auto def __call__(self, im): """ Resizes and pads input image `im` (HWC format) to specified dimensions, maintaining aspect ratio. im = np.array HWC """ imh, imw = im.shape[:2] r = min(self.h / imh, self.w / imw) # ratio of new/old h, w = round(imh * r), round(imw * r) # resized image hs, ws = (math.ceil(x / self.stride) * self.stride for x in (h, w)) if self.auto else self.h, self.w top, left = round((hs - h) / 2 - 0.1), round((ws - w) / 2 - 0.1) im_out = np.full((self.h, self.w, 3), 114, dtype=im.dtype) im_out[top : top + h, left : left + w] = cv2.resize(im, (w, h), interpolation=cv2.INTER_LINEAR) return im_out class CenterCrop: # YOLOv5 CenterCrop class for image preprocessing, i.e. T.Compose([CenterCrop(size), ToTensor()]) def __init__(self, size=640): """Initializes CenterCrop for image preprocessing, accepting single int or tuple for size, defaults to 640.""" super().__init__() self.h, self.w = (size, size) if isinstance(size, int) else size def __call__(self, im): """ Applies center crop to the input image and resizes it to a specified size, maintaining aspect ratio. im = np.array HWC """ imh, imw = im.shape[:2] m = min(imh, imw) # min dimension top, left = (imh - m) // 2, (imw - m) // 2 return cv2.resize(im[top : top + m, left : left + m], (self.w, self.h), interpolation=cv2.INTER_LINEAR) class ToTensor: # YOLOv5 ToTensor class for image preprocessing, i.e. T.Compose([LetterBox(size), ToTensor()]) def __init__(self, half=False): """Initializes ToTensor for YOLOv5 image preprocessing, with optional half precision (half=True for FP16).""" super().__init__() self.half = half def __call__(self, im): """ Converts BGR np.array image from HWC to RGB CHW format, and normalizes to [0, 1], with support for FP16 if `half=True`. im = np.array HWC in BGR order """ im = np.ascontiguousarray(im.transpose((2, 0, 1))[::-1]) # HWC to CHW -> BGR to RGB -> contiguous im = torch.from_numpy(im) # to torch im = im.half() if self.half else im.float() # uint8 to fp16/32 im /= 255.0 # 0-255 to 0.0-1.0 return im ================================================ FILE: asone/detectors/yolov5/yolov5/utils/dataloaders.py ================================================ # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license """Dataloaders and dataset utils.""" import contextlib import glob import hashlib import json import math import os import random import shutil import time from itertools import repeat from multiprocessing.pool import Pool, ThreadPool from pathlib import Path from threading import Thread from urllib.parse import urlparse import numpy as np import psutil import torch import torch.nn.functional as F import torchvision import yaml from PIL import ExifTags, Image, ImageOps from torch.utils.data import DataLoader, Dataset, dataloader, distributed from tqdm import tqdm from asone.detectors.yolov5.yolov5.utils.augmentations import ( Albumentations, augment_hsv, classify_albumentations, classify_transforms, copy_paste, letterbox, mixup, random_perspective, ) from asone.detectors.yolov5.yolov5.utils.general import ( DATASETS_DIR, LOGGER, NUM_THREADS, TQDM_BAR_FORMAT, check_dataset, check_requirements, check_yaml, clean_str, cv2, is_colab, is_kaggle, segments2boxes, unzip_file, xyn2xy, xywh2xyxy, xywhn2xyxy, xyxy2xywhn, ) from asone.detectors.yolov5.yolov5.utils.torch_utils import torch_distributed_zero_first # Parameters HELP_URL = "See https://docs.ultralytics.com/yolov5/tutorials/train_custom_data" IMG_FORMATS = "bmp", "dng", "jpeg", "jpg", "mpo", "png", "tif", "tiff", "webp", "pfm" # include image suffixes VID_FORMATS = "asf", "avi", "gif", "m4v", "mkv", "mov", "mp4", "mpeg", "mpg", "ts", "wmv" # include video suffixes LOCAL_RANK = int(os.getenv("LOCAL_RANK", -1)) # https://pytorch.org/docs/stable/elastic/run.html RANK = int(os.getenv("RANK", -1)) WORLD_SIZE = int(os.getenv("WORLD_SIZE", 1)) PIN_MEMORY = str(os.getenv("PIN_MEMORY", True)).lower() == "true" # global pin_memory for dataloaders # Get orientation exif tag for orientation in ExifTags.TAGS.keys(): if ExifTags.TAGS[orientation] == "Orientation": break def get_hash(paths): """Generates a single SHA256 hash for a list of file or directory paths by combining their sizes and paths.""" size = sum(os.path.getsize(p) for p in paths if os.path.exists(p)) # sizes h = hashlib.sha256(str(size).encode()) # hash sizes h.update("".join(paths).encode()) # hash paths return h.hexdigest() # return hash def exif_size(img): """Returns corrected PIL image size (width, height) considering EXIF orientation.""" s = img.size # (width, height) with contextlib.suppress(Exception): rotation = dict(img._getexif().items())[orientation] if rotation in [6, 8]: # rotation 270 or 90 s = (s[1], s[0]) return s def exif_transpose(image): """ Transpose a PIL image accordingly if it has an EXIF Orientation tag. Inplace version of https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageOps.py exif_transpose() :param image: The image to transpose. :return: An image. """ exif = image.getexif() orientation = exif.get(0x0112, 1) # default 1 if orientation > 1: method = { 2: Image.FLIP_LEFT_RIGHT, 3: Image.ROTATE_180, 4: Image.FLIP_TOP_BOTTOM, 5: Image.TRANSPOSE, 6: Image.ROTATE_270, 7: Image.TRANSVERSE, 8: Image.ROTATE_90, }.get(orientation) if method is not None: image = image.transpose(method) del exif[0x0112] image.info["exif"] = exif.tobytes() return image def seed_worker(worker_id): """ Sets the seed for a dataloader worker to ensure reproducibility, based on PyTorch's randomness notes. See https://pytorch.org/docs/stable/notes/randomness.html#dataloader. """ worker_seed = torch.initial_seed() % 2**32 np.random.seed(worker_seed) random.seed(worker_seed) # Inherit from DistributedSampler and override iterator # https://github.com/pytorch/pytorch/blob/master/torch/utils/data/distributed.py class SmartDistributedSampler(distributed.DistributedSampler): def __iter__(self): """Yields indices for distributed data sampling, shuffled deterministically based on epoch and seed.""" g = torch.Generator() g.manual_seed(self.seed + self.epoch) # determine the the eventual size (n) of self.indices (DDP indices) n = int((len(self.dataset) - self.rank - 1) / self.num_replicas) + 1 # num_replicas == WORLD_SIZE idx = torch.randperm(n, generator=g) if not self.shuffle: idx = idx.sort()[0] idx = idx.tolist() if self.drop_last: idx = idx[: self.num_samples] else: padding_size = self.num_samples - len(idx) if padding_size <= len(idx): idx += idx[:padding_size] else: idx += (idx * math.ceil(padding_size / len(idx)))[:padding_size] return iter(idx) def create_dataloader( path, imgsz, batch_size, stride, single_cls=False, hyp=None, augment=False, cache=False, pad=0.0, rect=False, rank=-1, workers=8, image_weights=False, quad=False, prefix="", shuffle=False, seed=0, ): if rect and shuffle: LOGGER.warning("WARNING ⚠️ --rect is incompatible with DataLoader shuffle, setting shuffle=False") shuffle = False with torch_distributed_zero_first(rank): # init dataset *.cache only once if DDP dataset = LoadImagesAndLabels( path, imgsz, batch_size, augment=augment, # augmentation hyp=hyp, # hyperparameters rect=rect, # rectangular batches cache_images=cache, single_cls=single_cls, stride=int(stride), pad=pad, image_weights=image_weights, prefix=prefix, rank=rank, ) batch_size = min(batch_size, len(dataset)) nd = torch.cuda.device_count() # number of CUDA devices nw = min([os.cpu_count() // max(nd, 1), batch_size if batch_size > 1 else 0, workers]) # number of workers sampler = None if rank == -1 else SmartDistributedSampler(dataset, shuffle=shuffle) loader = DataLoader if image_weights else InfiniteDataLoader # only DataLoader allows for attribute updates generator = torch.Generator() generator.manual_seed(6148914691236517205 + seed + RANK) return loader( dataset, batch_size=batch_size, shuffle=shuffle and sampler is None, num_workers=nw, sampler=sampler, pin_memory=PIN_MEMORY, collate_fn=LoadImagesAndLabels.collate_fn4 if quad else LoadImagesAndLabels.collate_fn, worker_init_fn=seed_worker, generator=generator, ), dataset class InfiniteDataLoader(dataloader.DataLoader): """ Dataloader that reuses workers. Uses same syntax as vanilla DataLoader """ def __init__(self, *args, **kwargs): """Initializes an InfiniteDataLoader that reuses workers with standard DataLoader syntax, augmenting with a repeating sampler. """ super().__init__(*args, **kwargs) object.__setattr__(self, "batch_sampler", _RepeatSampler(self.batch_sampler)) self.iterator = super().__iter__() def __len__(self): """Returns the length of the batch sampler's sampler in the InfiniteDataLoader.""" return len(self.batch_sampler.sampler) def __iter__(self): """Yields batches of data indefinitely in a loop by resetting the sampler when exhausted.""" for _ in range(len(self)): yield next(self.iterator) class _RepeatSampler: """ Sampler that repeats forever. Args: sampler (Sampler) """ def __init__(self, sampler): """Initializes a perpetual sampler wrapping a provided `Sampler` instance for endless data iteration.""" self.sampler = sampler def __iter__(self): """Returns an infinite iterator over the dataset by repeatedly yielding from the given sampler.""" while True: yield from iter(self.sampler) class LoadScreenshots: # YOLOv5 screenshot dataloader, i.e. `python detect.py --source "screen 0 100 100 512 256"` def __init__(self, source, img_size=640, stride=32, auto=True, transforms=None): """ Initializes a screenshot dataloader for YOLOv5 with specified source region, image size, stride, auto, and transforms. Source = [screen_number left top width height] (pixels) """ check_requirements("mss") import mss source, *params = source.split() self.screen, left, top, width, height = 0, None, None, None, None # default to full screen 0 if len(params) == 1: self.screen = int(params[0]) elif len(params) == 4: left, top, width, height = (int(x) for x in params) elif len(params) == 5: self.screen, left, top, width, height = (int(x) for x in params) self.img_size = img_size self.stride = stride self.transforms = transforms self.auto = auto self.mode = "stream" self.frame = 0 self.sct = mss.mss() # Parse monitor shape monitor = self.sct.monitors[self.screen] self.top = monitor["top"] if top is None else (monitor["top"] + top) self.left = monitor["left"] if left is None else (monitor["left"] + left) self.width = width or monitor["width"] self.height = height or monitor["height"] self.monitor = {"left": self.left, "top": self.top, "width": self.width, "height": self.height} def __iter__(self): """Iterates over itself, enabling use in loops and iterable contexts.""" return self def __next__(self): """Captures and returns the next screen frame as a BGR numpy array, cropping to only the first three channels from BGRA. """ im0 = np.array(self.sct.grab(self.monitor))[:, :, :3] # [:, :, :3] BGRA to BGR s = f"screen {self.screen} (LTWH): {self.left},{self.top},{self.width},{self.height}: " if self.transforms: im = self.transforms(im0) # transforms else: im = letterbox(im0, self.img_size, stride=self.stride, auto=self.auto)[0] # padded resize im = im.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB im = np.ascontiguousarray(im) # contiguous self.frame += 1 return str(self.screen), im, im0, None, s # screen, img, original img, im0s, s class LoadImages: """YOLOv5 image/video dataloader, i.e. `python detect.py --source image.jpg/vid.mp4`""" def __init__(self, path, img_size=640, stride=32, auto=True, transforms=None, vid_stride=1): """Initializes YOLOv5 loader for images/videos, supporting glob patterns, directories, and lists of paths.""" if isinstance(path, str) and Path(path).suffix == ".txt": # *.txt file with img/vid/dir on each line path = Path(path).read_text().rsplit() files = [] for p in sorted(path) if isinstance(path, (list, tuple)) else [path]: p = str(Path(p).resolve()) if "*" in p: files.extend(sorted(glob.glob(p, recursive=True))) # glob elif os.path.isdir(p): files.extend(sorted(glob.glob(os.path.join(p, "*.*")))) # dir elif os.path.isfile(p): files.append(p) # files else: raise FileNotFoundError(f"{p} does not exist") images = [x for x in files if x.split(".")[-1].lower() in IMG_FORMATS] videos = [x for x in files if x.split(".")[-1].lower() in VID_FORMATS] ni, nv = len(images), len(videos) self.img_size = img_size self.stride = stride self.files = images + videos self.nf = ni + nv # number of files self.video_flag = [False] * ni + [True] * nv self.mode = "image" self.auto = auto self.transforms = transforms # optional self.vid_stride = vid_stride # video frame-rate stride if any(videos): self._new_video(videos[0]) # new video else: self.cap = None assert self.nf > 0, ( f"No images or videos found in {p}. " f"Supported formats are:\nimages: {IMG_FORMATS}\nvideos: {VID_FORMATS}" ) def __iter__(self): """Initializes iterator by resetting count and returns the iterator object itself.""" self.count = 0 return self def __next__(self): """Advances to the next file in the dataset, raising StopIteration if at the end.""" if self.count == self.nf: raise StopIteration path = self.files[self.count] if self.video_flag[self.count]: # Read video self.mode = "video" for _ in range(self.vid_stride): self.cap.grab() ret_val, im0 = self.cap.retrieve() while not ret_val: self.count += 1 self.cap.release() if self.count == self.nf: # last video raise StopIteration path = self.files[self.count] self._new_video(path) ret_val, im0 = self.cap.read() self.frame += 1 # im0 = self._cv2_rotate(im0) # for use if cv2 autorotation is False s = f"video {self.count + 1}/{self.nf} ({self.frame}/{self.frames}) {path}: " else: # Read image self.count += 1 im0 = cv2.imread(path) # BGR assert im0 is not None, f"Image Not Found {path}" s = f"image {self.count}/{self.nf} {path}: " if self.transforms: im = self.transforms(im0) # transforms else: im = letterbox(im0, self.img_size, stride=self.stride, auto=self.auto)[0] # padded resize im = im.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB im = np.ascontiguousarray(im) # contiguous return path, im, im0, self.cap, s def _new_video(self, path): """Initializes a new video capture object with path, frame count adjusted by stride, and orientation metadata. """ self.frame = 0 self.cap = cv2.VideoCapture(path) self.frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT) / self.vid_stride) self.orientation = int(self.cap.get(cv2.CAP_PROP_ORIENTATION_META)) # rotation degrees # self.cap.set(cv2.CAP_PROP_ORIENTATION_AUTO, 0) # disable https://github.com/ultralytics/yolov5/issues/8493 def _cv2_rotate(self, im): """Rotates a cv2 image based on its orientation; supports 0, 90, and 180 degrees rotations.""" if self.orientation == 0: return cv2.rotate(im, cv2.ROTATE_90_CLOCKWISE) elif self.orientation == 180: return cv2.rotate(im, cv2.ROTATE_90_COUNTERCLOCKWISE) elif self.orientation == 90: return cv2.rotate(im, cv2.ROTATE_180) return im def __len__(self): """Returns the number of files in the dataset.""" return self.nf # number of files class LoadStreams: # YOLOv5 streamloader, i.e. `python detect.py --source 'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP streams` def __init__(self, sources="file.streams", img_size=640, stride=32, auto=True, transforms=None, vid_stride=1): """Initializes a stream loader for processing video streams with YOLOv5, supporting various sources including YouTube. """ torch.backends.cudnn.benchmark = True # faster for fixed-size inference self.mode = "stream" self.img_size = img_size self.stride = stride self.vid_stride = vid_stride # video frame-rate stride sources = Path(sources).read_text().rsplit() if os.path.isfile(sources) else [sources] n = len(sources) self.sources = [clean_str(x) for x in sources] # clean source names for later self.imgs, self.fps, self.frames, self.threads = [None] * n, [0] * n, [0] * n, [None] * n for i, s in enumerate(sources): # index, source # Start thread to read frames from video stream st = f"{i + 1}/{n}: {s}... " if urlparse(s).hostname in ("www.youtube.com", "youtube.com", "youtu.be"): # if source is YouTube video # YouTube format i.e. 'https://www.youtube.com/watch?v=Zgi9g1ksQHc' or 'https://youtu.be/LNwODJXcvt4' check_requirements(("pafy", "youtube_dl==2020.12.2")) import pafy s = pafy.new(s).getbest(preftype="mp4").url # YouTube URL s = eval(s) if s.isnumeric() else s # i.e. s = '0' local webcam if s == 0: assert not is_colab(), "--source 0 webcam unsupported on Colab. Rerun command in a local environment." assert not is_kaggle(), "--source 0 webcam unsupported on Kaggle. Rerun command in a local environment." cap = cv2.VideoCapture(s) assert cap.isOpened(), f"{st}Failed to open {s}" w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = cap.get(cv2.CAP_PROP_FPS) # warning: may return 0 or nan self.frames[i] = max(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 0) or float("inf") # infinite stream fallback self.fps[i] = max((fps if math.isfinite(fps) else 0) % 100, 0) or 30 # 30 FPS fallback _, self.imgs[i] = cap.read() # guarantee first frame self.threads[i] = Thread(target=self.update, args=([i, cap, s]), daemon=True) LOGGER.info(f"{st} Success ({self.frames[i]} frames {w}x{h} at {self.fps[i]:.2f} FPS)") self.threads[i].start() LOGGER.info("") # newline # check for common shapes s = np.stack([letterbox(x, img_size, stride=stride, auto=auto)[0].shape for x in self.imgs]) self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal self.auto = auto and self.rect self.transforms = transforms # optional if not self.rect: LOGGER.warning("WARNING ⚠️ Stream shapes differ. For optimal performance supply similarly-shaped streams.") def update(self, i, cap, stream): """Reads frames from stream `i`, updating imgs array; handles stream reopening on signal loss.""" n, f = 0, self.frames[i] # frame number, frame array while cap.isOpened() and n < f: n += 1 cap.grab() # .read() = .grab() followed by .retrieve() if n % self.vid_stride == 0: success, im = cap.retrieve() if success: self.imgs[i] = im else: LOGGER.warning("WARNING ⚠️ Video stream unresponsive, please check your IP camera connection.") self.imgs[i] = np.zeros_like(self.imgs[i]) cap.open(stream) # re-open stream if signal was lost time.sleep(0.0) # wait time def __iter__(self): """Resets and returns the iterator for iterating over video frames or images in a dataset.""" self.count = -1 return self def __next__(self): """Iterates over video frames or images, halting on thread stop or 'q' key press, raising `StopIteration` when done. """ self.count += 1 if not all(x.is_alive() for x in self.threads) or cv2.waitKey(1) == ord("q"): # q to quit cv2.destroyAllWindows() raise StopIteration im0 = self.imgs.copy() if self.transforms: im = np.stack([self.transforms(x) for x in im0]) # transforms else: im = np.stack([letterbox(x, self.img_size, stride=self.stride, auto=self.auto)[0] for x in im0]) # resize im = im[..., ::-1].transpose((0, 3, 1, 2)) # BGR to RGB, BHWC to BCHW im = np.ascontiguousarray(im) # contiguous return self.sources, im, im0, None, "" def __len__(self): """Returns the number of sources in the dataset, supporting up to 32 streams at 30 FPS over 30 years.""" return len(self.sources) # 1E12 frames = 32 streams at 30 FPS for 30 years def img2label_paths(img_paths): """Generates label file paths from corresponding image file paths by replacing `/images/` with `/labels/` and extension with `.txt`. """ sa, sb = f"{os.sep}images{os.sep}", f"{os.sep}labels{os.sep}" # /images/, /labels/ substrings return [sb.join(x.rsplit(sa, 1)).rsplit(".", 1)[0] + ".txt" for x in img_paths] class LoadImagesAndLabels(Dataset): # YOLOv5 train_loader/val_loader, loads images and labels for training and validation cache_version = 0.6 # dataset labels *.cache version rand_interp_methods = [cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4] def __init__( self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, cache_images=False, single_cls=False, stride=32, pad=0.0, min_items=0, prefix="", rank=-1, seed=0, ): self.img_size = img_size self.augment = augment self.hyp = hyp self.image_weights = image_weights self.rect = False if image_weights else rect self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training) self.mosaic_border = [-img_size // 2, -img_size // 2] self.stride = stride self.path = path self.albumentations = Albumentations(size=img_size) if augment else None try: f = [] # image files for p in path if isinstance(path, list) else [path]: p = Path(p) # os-agnostic if p.is_dir(): # dir f += glob.glob(str(p / "**" / "*.*"), recursive=True) # f = list(p.rglob('*.*')) # pathlib elif p.is_file(): # file with open(p) as t: t = t.read().strip().splitlines() parent = str(p.parent) + os.sep f += [x.replace("./", parent, 1) if x.startswith("./") else x for x in t] # to global path # f += [p.parent / x.lstrip(os.sep) for x in t] # to global path (pathlib) else: raise FileNotFoundError(f"{prefix}{p} does not exist") self.im_files = sorted(x.replace("/", os.sep) for x in f if x.split(".")[-1].lower() in IMG_FORMATS) # self.img_files = sorted([x for x in f if x.suffix[1:].lower() in IMG_FORMATS]) # pathlib assert self.im_files, f"{prefix}No images found" except Exception as e: raise Exception(f"{prefix}Error loading data from {path}: {e}\n{HELP_URL}") from e # Check cache self.label_files = img2label_paths(self.im_files) # labels cache_path = (p if p.is_file() else Path(self.label_files[0]).parent).with_suffix(".cache") try: cache, exists = np.load(cache_path, allow_pickle=True).item(), True # load dict assert cache["version"] == self.cache_version # matches current version assert cache["hash"] == get_hash(self.label_files + self.im_files) # identical hash except Exception: cache, exists = self.cache_labels(cache_path, prefix), False # run cache ops # Display cache nf, nm, ne, nc, n = cache.pop("results") # found, missing, empty, corrupt, total if exists and LOCAL_RANK in {-1, 0}: d = f"Scanning {cache_path}... {nf} images, {nm + ne} backgrounds, {nc} corrupt" tqdm(None, desc=prefix + d, total=n, initial=n, bar_format=TQDM_BAR_FORMAT) # display cache results if cache["msgs"]: LOGGER.info("\n".join(cache["msgs"])) # display warnings assert nf > 0 or not augment, f"{prefix}No labels found in {cache_path}, can not start training. {HELP_URL}" # Read cache [cache.pop(k) for k in ("hash", "version", "msgs")] # remove items labels, shapes, self.segments = zip(*cache.values()) nl = len(np.concatenate(labels, 0)) # number of labels assert nl > 0 or not augment, f"{prefix}All labels empty in {cache_path}, can not start training. {HELP_URL}" self.labels = list(labels) self.shapes = np.array(shapes) self.im_files = list(cache.keys()) # update self.label_files = img2label_paths(cache.keys()) # update # Filter images if min_items: include = np.array([len(x) >= min_items for x in self.labels]).nonzero()[0].astype(int) LOGGER.info(f"{prefix}{n - len(include)}/{n} images filtered from dataset") self.im_files = [self.im_files[i] for i in include] self.label_files = [self.label_files[i] for i in include] self.labels = [self.labels[i] for i in include] self.segments = [self.segments[i] for i in include] self.shapes = self.shapes[include] # wh # Create indices n = len(self.shapes) # number of images bi = np.floor(np.arange(n) / batch_size).astype(int) # batch index nb = bi[-1] + 1 # number of batches self.batch = bi # batch index of image self.n = n self.indices = np.arange(n) if rank > -1: # DDP indices (see: SmartDistributedSampler) # force each rank (i.e. GPU process) to sample the same subset of data on every epoch self.indices = self.indices[np.random.RandomState(seed=seed).permutation(n) % WORLD_SIZE == RANK] # Update labels include_class = [] # filter labels to include only these classes (optional) self.segments = list(self.segments) include_class_array = np.array(include_class).reshape(1, -1) for i, (label, segment) in enumerate(zip(self.labels, self.segments)): if include_class: j = (label[:, 0:1] == include_class_array).any(1) self.labels[i] = label[j] if segment: self.segments[i] = [segment[idx] for idx, elem in enumerate(j) if elem] if single_cls: # single-class training, merge all classes into 0 self.labels[i][:, 0] = 0 # Rectangular Training if self.rect: # Sort by aspect ratio s = self.shapes # wh ar = s[:, 1] / s[:, 0] # aspect ratio irect = ar.argsort() self.im_files = [self.im_files[i] for i in irect] self.label_files = [self.label_files[i] for i in irect] self.labels = [self.labels[i] for i in irect] self.segments = [self.segments[i] for i in irect] self.shapes = s[irect] # wh ar = ar[irect] # Set training image shapes shapes = [[1, 1]] * nb for i in range(nb): ari = ar[bi == i] mini, maxi = ari.min(), ari.max() if maxi < 1: shapes[i] = [maxi, 1] elif mini > 1: shapes[i] = [1, 1 / mini] self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(int) * stride # Cache images into RAM/disk for faster training if cache_images == "ram" and not self.check_cache_ram(prefix=prefix): cache_images = False self.ims = [None] * n self.npy_files = [Path(f).with_suffix(".npy") for f in self.im_files] if cache_images: b, gb = 0, 1 << 30 # bytes of cached images, bytes per gigabytes self.im_hw0, self.im_hw = [None] * n, [None] * n fcn = self.cache_images_to_disk if cache_images == "disk" else self.load_image results = ThreadPool(NUM_THREADS).imap(lambda i: (i, fcn(i)), self.indices) pbar = tqdm(results, total=len(self.indices), bar_format=TQDM_BAR_FORMAT, disable=LOCAL_RANK > 0) for i, x in pbar: if cache_images == "disk": b += self.npy_files[i].stat().st_size else: # 'ram' self.ims[i], self.im_hw0[i], self.im_hw[i] = x # im, hw_orig, hw_resized = load_image(self, i) b += self.ims[i].nbytes * WORLD_SIZE pbar.desc = f"{prefix}Caching images ({b / gb:.1f}GB {cache_images})" pbar.close() def check_cache_ram(self, safety_margin=0.1, prefix=""): """Checks if available RAM is sufficient for caching images, adjusting for a safety margin.""" b, gb = 0, 1 << 30 # bytes of cached images, bytes per gigabytes n = min(self.n, 30) # extrapolate from 30 random images for _ in range(n): im = cv2.imread(random.choice(self.im_files)) # sample image ratio = self.img_size / max(im.shape[0], im.shape[1]) # max(h, w) # ratio b += im.nbytes * ratio**2 mem_required = b * self.n / n # GB required to cache dataset into RAM mem = psutil.virtual_memory() cache = mem_required * (1 + safety_margin) < mem.available # to cache or not to cache, that is the question if not cache: LOGGER.info( f'{prefix}{mem_required / gb:.1f}GB RAM required, ' f'{mem.available / gb:.1f}/{mem.total / gb:.1f}GB available, ' f"{'caching images ✅' if cache else 'not caching images ⚠️'}" ) return cache def cache_labels(self, path=Path("./labels.cache"), prefix=""): """Caches dataset labels, verifies images, reads shapes, and tracks dataset integrity.""" x = {} # dict nm, nf, ne, nc, msgs = 0, 0, 0, 0, [] # number missing, found, empty, corrupt, messages desc = f"{prefix}Scanning {path.parent / path.stem}..." with Pool(NUM_THREADS) as pool: pbar = tqdm( pool.imap(verify_image_label, zip(self.im_files, self.label_files, repeat(prefix))), desc=desc, total=len(self.im_files), bar_format=TQDM_BAR_FORMAT, ) for im_file, lb, shape, segments, nm_f, nf_f, ne_f, nc_f, msg in pbar: nm += nm_f nf += nf_f ne += ne_f nc += nc_f if im_file: x[im_file] = [lb, shape, segments] if msg: msgs.append(msg) pbar.desc = f"{desc} {nf} images, {nm + ne} backgrounds, {nc} corrupt" pbar.close() if msgs: LOGGER.info("\n".join(msgs)) if nf == 0: LOGGER.warning(f"{prefix}WARNING ⚠️ No labels found in {path}. {HELP_URL}") x["hash"] = get_hash(self.label_files + self.im_files) x["results"] = nf, nm, ne, nc, len(self.im_files) x["msgs"] = msgs # warnings x["version"] = self.cache_version # cache version try: np.save(path, x) # save cache for next time path.with_suffix(".cache.npy").rename(path) # remove .npy suffix LOGGER.info(f"{prefix}New cache created: {path}") except Exception as e: LOGGER.warning(f"{prefix}WARNING ⚠️ Cache directory {path.parent} is not writeable: {e}") # not writeable return x def __len__(self): """Returns the number of images in the dataset.""" return len(self.im_files) # def __iter__(self): # self.count = -1 # print('ran dataset iter') # #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF) # return self def __getitem__(self, index): """Fetches the dataset item at the given index, considering linear, shuffled, or weighted sampling.""" index = self.indices[index] # linear, shuffled, or image_weights hyp = self.hyp mosaic = self.mosaic and random.random() < hyp["mosaic"] if mosaic: # Load mosaic img, labels = self.load_mosaic(index) shapes = None # MixUp augmentation if random.random() < hyp["mixup"]: img, labels = mixup(img, labels, *self.load_mosaic(random.choice(self.indices))) else: # Load image img, (h0, w0), (h, w) = self.load_image(index) # Letterbox shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment) shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling labels = self.labels[index].copy() if labels.size: # normalized xywh to pixel xyxy format labels[:, 1:] = xywhn2xyxy(labels[:, 1:], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1]) if self.augment: img, labels = random_perspective( img, labels, degrees=hyp["degrees"], translate=hyp["translate"], scale=hyp["scale"], shear=hyp["shear"], perspective=hyp["perspective"], ) nl = len(labels) # number of labels if nl: labels[:, 1:5] = xyxy2xywhn(labels[:, 1:5], w=img.shape[1], h=img.shape[0], clip=True, eps=1e-3) if self.augment: # Albumentations img, labels = self.albumentations(img, labels) nl = len(labels) # update after albumentations # HSV color-space augment_hsv(img, hgain=hyp["hsv_h"], sgain=hyp["hsv_s"], vgain=hyp["hsv_v"]) # Flip up-down if random.random() < hyp["flipud"]: img = np.flipud(img) if nl: labels[:, 2] = 1 - labels[:, 2] # Flip left-right if random.random() < hyp["fliplr"]: img = np.fliplr(img) if nl: labels[:, 1] = 1 - labels[:, 1] # Cutouts # labels = cutout(img, labels, p=0.5) # nl = len(labels) # update after cutout labels_out = torch.zeros((nl, 6)) if nl: labels_out[:, 1:] = torch.from_numpy(labels) # Convert img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB img = np.ascontiguousarray(img) return torch.from_numpy(img), labels_out, self.im_files[index], shapes def load_image(self, i): """ Loads an image by index, returning the image, its original dimensions, and resized dimensions. Returns (im, original hw, resized hw) """ im, f, fn = ( self.ims[i], self.im_files[i], self.npy_files[i], ) if im is None: # not cached in RAM if fn.exists(): # load npy im = np.load(fn) else: # read image im = cv2.imread(f) # BGR assert im is not None, f"Image Not Found {f}" h0, w0 = im.shape[:2] # orig hw r = self.img_size / max(h0, w0) # ratio if r != 1: # if sizes are not equal interp = cv2.INTER_LINEAR if (self.augment or r > 1) else cv2.INTER_AREA im = cv2.resize(im, (math.ceil(w0 * r), math.ceil(h0 * r)), interpolation=interp) return im, (h0, w0), im.shape[:2] # im, hw_original, hw_resized return self.ims[i], self.im_hw0[i], self.im_hw[i] # im, hw_original, hw_resized def cache_images_to_disk(self, i): """Saves an image to disk as an *.npy file for quicker loading, identified by index `i`.""" f = self.npy_files[i] if not f.exists(): np.save(f.as_posix(), cv2.imread(self.im_files[i])) def load_mosaic(self, index): """Loads a 4-image mosaic for YOLOv5, combining 1 selected and 3 random images, with labels and segments.""" labels4, segments4 = [], [] s = self.img_size yc, xc = (int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border) # mosaic center x, y indices = [index] + random.choices(self.indices, k=3) # 3 additional image indices random.shuffle(indices) for i, index in enumerate(indices): # Load image img, _, (h, w) = self.load_image(index) # place img in img4 if i == 0: # top left img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image) x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image) elif i == 1: # top right x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h elif i == 2: # bottom left x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h) x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h) elif i == 3: # bottom right x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h) x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h) img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax] padw = x1a - x1b padh = y1a - y1b # Labels labels, segments = self.labels[index].copy(), self.segments[index].copy() if labels.size: labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format segments = [xyn2xy(x, w, h, padw, padh) for x in segments] labels4.append(labels) segments4.extend(segments) # Concat/clip labels labels4 = np.concatenate(labels4, 0) for x in (labels4[:, 1:], *segments4): np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective() # img4, labels4 = replicate(img4, labels4) # replicate # Augment img4, labels4, segments4 = copy_paste(img4, labels4, segments4, p=self.hyp["copy_paste"]) img4, labels4 = random_perspective( img4, labels4, segments4, degrees=self.hyp["degrees"], translate=self.hyp["translate"], scale=self.hyp["scale"], shear=self.hyp["shear"], perspective=self.hyp["perspective"], border=self.mosaic_border, ) # border to remove return img4, labels4 def load_mosaic9(self, index): """Loads 1 image + 8 random images into a 9-image mosaic for augmented YOLOv5 training, returning labels and segments. """ labels9, segments9 = [], [] s = self.img_size indices = [index] + random.choices(self.indices, k=8) # 8 additional image indices random.shuffle(indices) hp, wp = -1, -1 # height, width previous for i, index in enumerate(indices): # Load image img, _, (h, w) = self.load_image(index) # place img in img9 if i == 0: # center img9 = np.full((s * 3, s * 3, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles h0, w0 = h, w c = s, s, s + w, s + h # xmin, ymin, xmax, ymax (base) coordinates elif i == 1: # top c = s, s - h, s + w, s elif i == 2: # top right c = s + wp, s - h, s + wp + w, s elif i == 3: # right c = s + w0, s, s + w0 + w, s + h elif i == 4: # bottom right c = s + w0, s + hp, s + w0 + w, s + hp + h elif i == 5: # bottom c = s + w0 - w, s + h0, s + w0, s + h0 + h elif i == 6: # bottom left c = s + w0 - wp - w, s + h0, s + w0 - wp, s + h0 + h elif i == 7: # left c = s - w, s + h0 - h, s, s + h0 elif i == 8: # top left c = s - w, s + h0 - hp - h, s, s + h0 - hp padx, pady = c[:2] x1, y1, x2, y2 = (max(x, 0) for x in c) # allocate coords # Labels labels, segments = self.labels[index].copy(), self.segments[index].copy() if labels.size: labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padx, pady) # normalized xywh to pixel xyxy format segments = [xyn2xy(x, w, h, padx, pady) for x in segments] labels9.append(labels) segments9.extend(segments) # Image img9[y1:y2, x1:x2] = img[y1 - pady :, x1 - padx :] # img9[ymin:ymax, xmin:xmax] hp, wp = h, w # height, width previous # Offset yc, xc = (int(random.uniform(0, s)) for _ in self.mosaic_border) # mosaic center x, y img9 = img9[yc : yc + 2 * s, xc : xc + 2 * s] # Concat/clip labels labels9 = np.concatenate(labels9, 0) labels9[:, [1, 3]] -= xc labels9[:, [2, 4]] -= yc c = np.array([xc, yc]) # centers segments9 = [x - c for x in segments9] for x in (labels9[:, 1:], *segments9): np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective() # img9, labels9 = replicate(img9, labels9) # replicate # Augment img9, labels9, segments9 = copy_paste(img9, labels9, segments9, p=self.hyp["copy_paste"]) img9, labels9 = random_perspective( img9, labels9, segments9, degrees=self.hyp["degrees"], translate=self.hyp["translate"], scale=self.hyp["scale"], shear=self.hyp["shear"], perspective=self.hyp["perspective"], border=self.mosaic_border, ) # border to remove return img9, labels9 @staticmethod def collate_fn(batch): """Batches images, labels, paths, and shapes, assigning unique indices to targets in merged label tensor.""" im, label, path, shapes = zip(*batch) # transposed for i, lb in enumerate(label): lb[:, 0] = i # add target image index for build_targets() return torch.stack(im, 0), torch.cat(label, 0), path, shapes @staticmethod def collate_fn4(batch): """Bundles a batch's data by quartering the number of shapes and paths, preparing it for model input.""" im, label, path, shapes = zip(*batch) # transposed n = len(shapes) // 4 im4, label4, path4, shapes4 = [], [], path[:n], shapes[:n] ho = torch.tensor([[0.0, 0, 0, 1, 0, 0]]) wo = torch.tensor([[0.0, 0, 1, 0, 0, 0]]) s = torch.tensor([[1, 1, 0.5, 0.5, 0.5, 0.5]]) # scale for i in range(n): # zidane torch.zeros(16,3,720,1280) # BCHW i *= 4 if random.random() < 0.5: im1 = F.interpolate(im[i].unsqueeze(0).float(), scale_factor=2.0, mode="bilinear", align_corners=False)[ 0 ].type(im[i].type()) lb = label[i] else: im1 = torch.cat((torch.cat((im[i], im[i + 1]), 1), torch.cat((im[i + 2], im[i + 3]), 1)), 2) lb = torch.cat((label[i], label[i + 1] + ho, label[i + 2] + wo, label[i + 3] + ho + wo), 0) * s im4.append(im1) label4.append(lb) for i, lb in enumerate(label4): lb[:, 0] = i # add target image index for build_targets() return torch.stack(im4, 0), torch.cat(label4, 0), path4, shapes4 # Ancillary functions -------------------------------------------------------------------------------------------------- def flatten_recursive(path=DATASETS_DIR / "coco128"): """Flattens a directory by copying all files from subdirectories to a new top-level directory, preserving filenames. """ new_path = Path(f"{str(path)}_flat") if os.path.exists(new_path): shutil.rmtree(new_path) # delete output folder os.makedirs(new_path) # make new output folder for file in tqdm(glob.glob(f"{str(Path(path))}/**/*.*", recursive=True)): shutil.copyfile(file, new_path / Path(file).name) def extract_boxes(path=DATASETS_DIR / "coco128"): """ Converts a detection dataset to a classification dataset, creating a directory for each class and extracting bounding boxes. Example: from utils.dataloaders import *; extract_boxes() """ path = Path(path) # images dir shutil.rmtree(path / "classification") if (path / "classification").is_dir() else None # remove existing files = list(path.rglob("*.*")) n = len(files) # number of files for im_file in tqdm(files, total=n): if im_file.suffix[1:] in IMG_FORMATS: # image im = cv2.imread(str(im_file))[..., ::-1] # BGR to RGB h, w = im.shape[:2] # labels lb_file = Path(img2label_paths([str(im_file)])[0]) if Path(lb_file).exists(): with open(lb_file) as f: lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32) # labels for j, x in enumerate(lb): c = int(x[0]) # class f = (path / "classification") / f"{c}" / f"{path.stem}_{im_file.stem}_{j}.jpg" # new filename if not f.parent.is_dir(): f.parent.mkdir(parents=True) b = x[1:] * [w, h, w, h] # box # b[2:] = b[2:].max() # rectangle to square b[2:] = b[2:] * 1.2 + 3 # pad b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(int) b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image b[[1, 3]] = np.clip(b[[1, 3]], 0, h) assert cv2.imwrite(str(f), im[b[1] : b[3], b[0] : b[2]]), f"box failure in {f}" def autosplit(path=DATASETS_DIR / "coco128/images", weights=(0.9, 0.1, 0.0), annotated_only=False): """Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files Usage: from utils.dataloaders import *; autosplit() Arguments path: Path to images directory weights: Train, val, test weights (list, tuple) annotated_only: Only use images with an annotated txt file """ path = Path(path) # images dir files = sorted(x for x in path.rglob("*.*") if x.suffix[1:].lower() in IMG_FORMATS) # image files only n = len(files) # number of files random.seed(0) # for reproducibility indices = random.choices([0, 1, 2], weights=weights, k=n) # assign each image to a split txt = ["autosplit_train.txt", "autosplit_val.txt", "autosplit_test.txt"] # 3 txt files for x in txt: if (path.parent / x).exists(): (path.parent / x).unlink() # remove existing print(f"Autosplitting images from {path}" + ", using *.txt labeled images only" * annotated_only) for i, img in tqdm(zip(indices, files), total=n): if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label with open(path.parent / txt[i], "a") as f: f.write(f"./{img.relative_to(path.parent).as_posix()}" + "\n") # add image to txt file def verify_image_label(args): """Verifies a single image-label pair, ensuring image format, size, and legal label values.""" im_file, lb_file, prefix = args nm, nf, ne, nc, msg, segments = 0, 0, 0, 0, "", [] # number (missing, found, empty, corrupt), message, segments try: # verify images im = Image.open(im_file) im.verify() # PIL verify shape = exif_size(im) # image size assert (shape[0] > 9) & (shape[1] > 9), f"image size {shape} <10 pixels" assert im.format.lower() in IMG_FORMATS, f"invalid image format {im.format}" if im.format.lower() in ("jpg", "jpeg"): with open(im_file, "rb") as f: f.seek(-2, 2) if f.read() != b"\xff\xd9": # corrupt JPEG ImageOps.exif_transpose(Image.open(im_file)).save(im_file, "JPEG", subsampling=0, quality=100) msg = f"{prefix}WARNING ⚠️ {im_file}: corrupt JPEG restored and saved" # verify labels if os.path.isfile(lb_file): nf = 1 # label found with open(lb_file) as f: lb = [x.split() for x in f.read().strip().splitlines() if len(x)] if any(len(x) > 6 for x in lb): # is segment classes = np.array([x[0] for x in lb], dtype=np.float32) segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in lb] # (cls, xy1...) lb = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1) # (cls, xywh) lb = np.array(lb, dtype=np.float32) nl = len(lb) if nl: assert lb.shape[1] == 5, f"labels require 5 columns, {lb.shape[1]} columns detected" assert (lb >= 0).all(), f"negative label values {lb[lb < 0]}" assert (lb[:, 1:] <= 1).all(), f"non-normalized or out of bounds coordinates {lb[:, 1:][lb[:, 1:] > 1]}" _, i = np.unique(lb, axis=0, return_index=True) if len(i) < nl: # duplicate row check lb = lb[i] # remove duplicates if segments: segments = [segments[x] for x in i] msg = f"{prefix}WARNING ⚠️ {im_file}: {nl - len(i)} duplicate labels removed" else: ne = 1 # label empty lb = np.zeros((0, 5), dtype=np.float32) else: nm = 1 # label missing lb = np.zeros((0, 5), dtype=np.float32) return im_file, lb, shape, segments, nm, nf, ne, nc, msg except Exception as e: nc = 1 msg = f"{prefix}WARNING ⚠️ {im_file}: ignoring corrupt image/label: {e}" return [None, None, None, None, nm, nf, ne, nc, msg] class HUBDatasetStats: """ Class for generating HUB dataset JSON and `-hub` dataset directory. Arguments path: Path to data.yaml or data.zip (with data.yaml inside data.zip) autodownload: Attempt to download dataset if not found locally Usage from utils.dataloaders import HUBDatasetStats stats = HUBDatasetStats('coco128.yaml', autodownload=True) # usage 1 stats = HUBDatasetStats('path/to/coco128.zip') # usage 2 stats.get_json(save=False) stats.process_images() """ def __init__(self, path="coco128.yaml", autodownload=False): """Initializes HUBDatasetStats with optional auto-download for datasets, given a path to dataset YAML or ZIP file. """ zipped, data_dir, yaml_path = self._unzip(Path(path)) try: with open(check_yaml(yaml_path), errors="ignore") as f: data = yaml.safe_load(f) # data dict if zipped: data["path"] = data_dir except Exception as e: raise Exception("error/HUB/dataset_stats/yaml_load") from e check_dataset(data, autodownload) # download dataset if missing self.hub_dir = Path(data["path"] + "-hub") self.im_dir = self.hub_dir / "images" self.im_dir.mkdir(parents=True, exist_ok=True) # makes /images self.stats = {"nc": data["nc"], "names": list(data["names"].values())} # statistics dictionary self.data = data @staticmethod def _find_yaml(dir): """Finds and returns the path to a single '.yaml' file in the specified directory, preferring files that match the directory name. """ files = list(dir.glob("*.yaml")) or list(dir.rglob("*.yaml")) # try root level first and then recursive assert files, f"No *.yaml file found in {dir}" if len(files) > 1: files = [f for f in files if f.stem == dir.stem] # prefer *.yaml files that match dir name assert files, f"Multiple *.yaml files found in {dir}, only 1 *.yaml file allowed" assert len(files) == 1, f"Multiple *.yaml files found: {files}, only 1 *.yaml file allowed in {dir}" return files[0] def _unzip(self, path): """Unzips a .zip file at 'path', returning success status, unzipped directory, and path to YAML file within.""" if not str(path).endswith(".zip"): # path is data.yaml return False, None, path assert Path(path).is_file(), f"Error unzipping {path}, file not found" unzip_file(path, path=path.parent) dir = path.with_suffix("") # dataset directory == zip name assert dir.is_dir(), f"Error unzipping {path}, {dir} not found. path/to/abc.zip MUST unzip to path/to/abc/" return True, str(dir), self._find_yaml(dir) # zipped, data_dir, yaml_path def _hub_ops(self, f, max_dim=1920): """Resizes and saves an image at reduced quality for web/app viewing, supporting both PIL and OpenCV.""" f_new = self.im_dir / Path(f).name # dataset-hub image filename try: # use PIL im = Image.open(f) r = max_dim / max(im.height, im.width) # ratio if r < 1.0: # image too large im = im.resize((int(im.width * r), int(im.height * r))) im.save(f_new, "JPEG", quality=50, optimize=True) # save except Exception as e: # use OpenCV LOGGER.info(f"WARNING ⚠️ HUB ops PIL failure {f}: {e}") im = cv2.imread(f) im_height, im_width = im.shape[:2] r = max_dim / max(im_height, im_width) # ratio if r < 1.0: # image too large im = cv2.resize(im, (int(im_width * r), int(im_height * r)), interpolation=cv2.INTER_AREA) cv2.imwrite(str(f_new), im) def get_json(self, save=False, verbose=False): """Generates dataset JSON for Ultralytics HUB, optionally saves or prints it; save=bool, verbose=bool.""" def _round(labels): """Rounds class labels to integers and coordinates to 4 decimal places for improved label accuracy.""" return [[int(c), *(round(x, 4) for x in points)] for c, *points in labels] for split in "train", "val", "test": if self.data.get(split) is None: self.stats[split] = None # i.e. no test set continue dataset = LoadImagesAndLabels(self.data[split]) # load dataset x = np.array( [ np.bincount(label[:, 0].astype(int), minlength=self.data["nc"]) for label in tqdm(dataset.labels, total=dataset.n, desc="Statistics") ] ) # shape(128x80) self.stats[split] = { "instance_stats": {"total": int(x.sum()), "per_class": x.sum(0).tolist()}, "image_stats": { "total": dataset.n, "unlabelled": int(np.all(x == 0, 1).sum()), "per_class": (x > 0).sum(0).tolist(), }, "labels": [{str(Path(k).name): _round(v.tolist())} for k, v in zip(dataset.im_files, dataset.labels)], } # Save, print and return if save: stats_path = self.hub_dir / "stats.json" print(f"Saving {stats_path.resolve()}...") with open(stats_path, "w") as f: json.dump(self.stats, f) # save stats.json if verbose: print(json.dumps(self.stats, indent=2, sort_keys=False)) return self.stats def process_images(self): """Compresses images for Ultralytics HUB across 'train', 'val', 'test' splits and saves to specified directory. """ for split in "train", "val", "test": if self.data.get(split) is None: continue dataset = LoadImagesAndLabels(self.data[split]) # load dataset desc = f"{split} images" for _ in tqdm(ThreadPool(NUM_THREADS).imap(self._hub_ops, dataset.im_files), total=dataset.n, desc=desc): pass print(f"Done. All images saved to {self.im_dir}") return self.im_dir # Classification dataloaders ------------------------------------------------------------------------------------------- class ClassificationDataset(torchvision.datasets.ImageFolder): """ YOLOv5 Classification Dataset. Arguments root: Dataset path transform: torchvision transforms, used by default album_transform: Albumentations transforms, used if installed """ def __init__(self, root, augment, imgsz, cache=False): """Initializes YOLOv5 Classification Dataset with optional caching, augmentations, and transforms for image classification. """ super().__init__(root=root) self.torch_transforms = classify_transforms(imgsz) self.album_transforms = classify_albumentations(augment, imgsz) if augment else None self.cache_ram = cache is True or cache == "ram" self.cache_disk = cache == "disk" self.samples = [list(x) + [Path(x[0]).with_suffix(".npy"), None] for x in self.samples] # file, index, npy, im def __getitem__(self, i): """Fetches and transforms an image sample by index, supporting RAM/disk caching and Augmentations.""" f, j, fn, im = self.samples[i] # filename, index, filename.with_suffix('.npy'), image if self.cache_ram and im is None: im = self.samples[i][3] = cv2.imread(f) elif self.cache_disk: if not fn.exists(): # load npy np.save(fn.as_posix(), cv2.imread(f)) im = np.load(fn) else: # read image im = cv2.imread(f) # BGR if self.album_transforms: sample = self.album_transforms(image=cv2.cvtColor(im, cv2.COLOR_BGR2RGB))["image"] else: sample = self.torch_transforms(im) return sample, j def create_classification_dataloader( path, imgsz=224, batch_size=16, augment=True, cache=False, rank=-1, workers=8, shuffle=True ): # Returns Dataloader object to be used with YOLOv5 Classifier with torch_distributed_zero_first(rank): # init dataset *.cache only once if DDP dataset = ClassificationDataset(root=path, imgsz=imgsz, augment=augment, cache=cache) batch_size = min(batch_size, len(dataset)) nd = torch.cuda.device_count() nw = min([os.cpu_count() // max(nd, 1), batch_size if batch_size > 1 else 0, workers]) sampler = None if rank == -1 else distributed.DistributedSampler(dataset, shuffle=shuffle) generator = torch.Generator() generator.manual_seed(6148914691236517205 + RANK) return InfiniteDataLoader( dataset, batch_size=batch_size, shuffle=shuffle and sampler is None, num_workers=nw, sampler=sampler, pin_memory=PIN_MEMORY, worker_init_fn=seed_worker, generator=generator, ) # or DataLoader(persistent_workers=True) ================================================ FILE: asone/detectors/yolov5/yolov5/utils/downloads.py.py ================================================ # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license """Download utils.""" import logging import subprocess import urllib from pathlib import Path import requests import torch def is_url(url, check=True): """Determines if a string is a URL and optionally checks its existence online, returning a boolean.""" try: url = str(url) result = urllib.parse.urlparse(url) assert all([result.scheme, result.netloc]) # check if is url return (urllib.request.urlopen(url).getcode() == 200) if check else True # check if exists online except (AssertionError, urllib.request.HTTPError): return False def gsutil_getsize(url=""): """ Returns the size in bytes of a file at a Google Cloud Storage URL using `gsutil du`. Returns 0 if the command fails or output is empty. """ output = subprocess.check_output(["gsutil", "du", url], shell=True, encoding="utf-8") return int(output.split()[0]) if output else 0 def url_getsize(url="https://ultralytics.com/images/bus.jpg"): """Returns the size in bytes of a downloadable file at a given URL; defaults to -1 if not found.""" response = requests.head(url, allow_redirects=True) return int(response.headers.get("content-length", -1)) def curl_download(url, filename, *, silent: bool = False) -> bool: """Download a file from a url to a filename using curl.""" silent_option = "sS" if silent else "" # silent proc = subprocess.run( [ "curl", "-#", f"-{silent_option}L", url, "--output", filename, "--retry", "9", "-C", "-", ] ) return proc.returncode == 0 def safe_download(file, url, url2=None, min_bytes=1e0, error_msg=""): """ Downloads a file from a URL (or alternate URL) to a specified path if file is above a minimum size. Removes incomplete downloads. """ from asone.detectors.yolov5.yolov5.utils.general import LOGGER file = Path(file) assert_msg = f"Downloaded file '{file}' does not exist or size is < min_bytes={min_bytes}" try: # url1 LOGGER.info(f"Downloading {url} to {file}...") torch.hub.download_url_to_file(url, str(file), progress=LOGGER.level <= logging.INFO) assert file.exists() and file.stat().st_size > min_bytes, assert_msg # check except Exception as e: # url2 if file.exists(): file.unlink() # remove partial downloads LOGGER.info(f"ERROR: {e}\nRe-attempting {url2 or url} to {file}...") # curl download, retry and resume on fail curl_download(url2 or url, file) finally: if not file.exists() or file.stat().st_size < min_bytes: # check if file.exists(): file.unlink() # remove partial downloads LOGGER.info(f"ERROR: {assert_msg}\n{error_msg}") LOGGER.info("") def attempt_download(file, repo="ultralytics/yolov5", release="v7.0"): """Downloads a file from GitHub release assets or via direct URL if not found locally, supporting backup versions. """ from asone.detectors.yolov5.yolov5.utils.general import LOGGER def github_assets(repository, version="latest"): # Return GitHub repo tag (i.e. 'v7.0') and assets (i.e. ['yolov5s.pt', 'yolov5m.pt', ...]) if version != "latest": version = f"tags/{version}" # i.e. tags/v7.0 response = requests.get(f"https://api.github.com/repos/{repository}/releases/{version}").json() # github api return response["tag_name"], [x["name"] for x in response["assets"]] # tag, assets file = Path(str(file).strip().replace("'", "")) if not file.exists(): # URL specified name = Path(urllib.parse.unquote(str(file))).name # decode '%2F' to '/' etc. if str(file).startswith(("http:/", "https:/")): # download url = str(file).replace(":/", "://") # Pathlib turns :// -> :/ file = name.split("?")[0] # parse authentication https://url.com/file.txt?auth... if Path(file).is_file(): LOGGER.info(f"Found {url} locally at {file}") # file already exists else: safe_download(file=file, url=url, min_bytes=1e5) return file # GitHub assets assets = [f"yolov5{size}{suffix}.pt" for size in "nsmlx" for suffix in ("", "6", "-cls", "-seg")] # default try: tag, assets = github_assets(repo, release) except Exception: try: tag, assets = github_assets(repo) # latest release except Exception: try: tag = subprocess.check_output("git tag", shell=True, stderr=subprocess.STDOUT).decode().split()[-1] except Exception: tag = release if name in assets: file.parent.mkdir(parents=True, exist_ok=True) # make parent dir (if required) safe_download( file, url=f"https://github.com/{repo}/releases/download/{tag}/{name}", min_bytes=1e5, error_msg=f"{file} missing, try downloading from https://github.com/{repo}/releases/{tag}", ) return str(file) ================================================ FILE: asone/detectors/yolov5/yolov5/utils/general.py ================================================ # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license """General utils.""" import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import subprocess import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml # Import 'ultralytics' package or install if missing try: import ultralytics assert hasattr(ultralytics, "__version__") # verify package is not directory except (ImportError, AssertionError): os.system("pip install -U ultralytics") import ultralytics from ultralytics.utils.checks import check_requirements from asone.detectors.yolov5.yolov5.utils import TryExcept, emojis from asone.detectors.yolov5.yolov5.utils.downloads import curl_download, gsutil_getsize from asone.detectors.yolov5.yolov5.utils.metrics import box_iou, fitness FILE = Path(__file__).resolve() ROOT = FILE.parents[1] # YOLOv5 root directory RANK = int(os.getenv("RANK", -1)) # Settings NUM_THREADS = min(8, max(1, os.cpu_count() - 1)) # number of YOLOv5 multiprocessing threads DATASETS_DIR = Path(os.getenv("YOLOv5_DATASETS_DIR", ROOT.parent / "datasets")) # global datasets directory AUTOINSTALL = str(os.getenv("YOLOv5_AUTOINSTALL", True)).lower() == "true" # global auto-install mode VERBOSE = str(os.getenv("YOLOv5_VERBOSE", True)).lower() == "true" # global verbose mode TQDM_BAR_FORMAT = "{l_bar}{bar:10}{r_bar}" # tqdm bar format FONT = "Arial.ttf" # https://ultralytics.com/assets/Arial.ttf torch.set_printoptions(linewidth=320, precision=5, profile="long") np.set_printoptions(linewidth=320, formatter={"float_kind": "{:11.5g}".format}) # format short g, %precision=5 pd.options.display.max_columns = 10 cv2.setNumThreads(0) # prevent OpenCV from multithreading (incompatible with PyTorch DataLoader) os.environ["NUMEXPR_MAX_THREADS"] = str(NUM_THREADS) # NumExpr max threads os.environ["OMP_NUM_THREADS"] = "1" if platform.system() == "darwin" else str(NUM_THREADS) # OpenMP (PyTorch and SciPy) os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" # suppress verbose TF compiler warnings in Colab def is_ascii(s=""): """Checks if input string `s` contains only ASCII characters; returns `True` if so, otherwise `False`.""" s = str(s) # convert list, tuple, None, etc. to str return len(s.encode().decode("ascii", "ignore")) == len(s) def is_chinese(s="人工智能"): """Determines if a string `s` contains any Chinese characters; returns `True` if so, otherwise `False`.""" return bool(re.search("[\u4e00-\u9fff]", str(s))) def is_colab(): """Checks if the current environment is a Google Colab instance; returns `True` for Colab, otherwise `False`.""" return "google.colab" in sys.modules def is_jupyter(): """ Check if the current script is running inside a Jupyter Notebook. Verified on Colab, Jupyterlab, Kaggle, Paperspace. Returns: bool: True if running inside a Jupyter Notebook, False otherwise. """ with contextlib.suppress(Exception): from IPython import get_ipython return get_ipython() is not None return False def is_kaggle(): """Checks if the current environment is a Kaggle Notebook by validating environment variables.""" return os.environ.get("PWD") == "/kaggle/working" and os.environ.get("KAGGLE_URL_BASE") == "https://www.kaggle.com" def is_docker() -> bool: """Check if the process runs inside a docker container.""" if Path("/.dockerenv").exists(): return True try: # check if docker is in control groups with open("/proc/self/cgroup") as file: return any("docker" in line for line in file) except OSError: return False def is_writeable(dir, test=False): """Checks if a directory is writable, optionally testing by creating a temporary file if `test=True`.""" if not test: return os.access(dir, os.W_OK) # possible issues on Windows file = Path(dir) / "tmp.txt" try: with open(file, "w"): # open file with write permissions pass file.unlink() # remove file return True except OSError: return False LOGGING_NAME = "yolov5" def set_logging(name=LOGGING_NAME, verbose=True): """Configures logging with specified verbosity; `name` sets the logger's name, `verbose` controls logging level.""" rank = int(os.getenv("RANK", -1)) # rank in world for Multi-GPU trainings level = logging.INFO if verbose and rank in {-1, 0} else logging.ERROR logging.config.dictConfig( { "version": 1, "disable_existing_loggers": False, "formatters": {name: {"format": "%(message)s"}}, "handlers": { name: { "class": "logging.StreamHandler", "formatter": name, "level": level, } }, "loggers": { name: { "level": level, "handlers": [name], "propagate": False, } }, } ) set_logging(LOGGING_NAME) # run before defining LOGGER LOGGER = logging.getLogger(LOGGING_NAME) # define globally (used in train.py, val.py, detect.py, etc.) if platform.system() == "Windows": for fn in LOGGER.info, LOGGER.warning: setattr(LOGGER, fn.__name__, lambda x: fn(emojis(x))) # emoji safe logging def user_config_dir(dir="Ultralytics", env_var="YOLOV5_CONFIG_DIR"): """Returns user configuration directory path, preferring environment variable `YOLOV5_CONFIG_DIR` if set, else OS- specific. """ env = os.getenv(env_var) if env: path = Path(env) # use environment variable else: cfg = {"Windows": "AppData/Roaming", "Linux": ".config", "Darwin": "Library/Application Support"} # 3 OS dirs path = Path.home() / cfg.get(platform.system(), "") # OS-specific config dir path = (path if is_writeable(path) else Path("/tmp")) / dir # GCP and AWS lambda fix, only /tmp is writeable path.mkdir(exist_ok=True) # make if required return path CONFIG_DIR = user_config_dir() # Ultralytics settings dir class Profile(contextlib.ContextDecorator): # YOLOv5 Profile class. Usage: @Profile() decorator or 'with Profile():' context manager def __init__(self, t=0.0, device: torch.device = None): """Initializes a profiling context for YOLOv5 with optional timing threshold and device specification.""" self.t = t self.device = device self.cuda = bool(device and str(device).startswith("cuda")) def __enter__(self): """Initializes timing at the start of a profiling context block for performance measurement.""" self.start = self.time() return self def __exit__(self, type, value, traceback): """Concludes timing, updating duration for profiling upon exiting a context block.""" self.dt = self.time() - self.start # delta-time self.t += self.dt # accumulate dt def time(self): """Measures and returns the current time, synchronizing CUDA operations if `cuda` is True.""" if self.cuda: torch.cuda.synchronize(self.device) return time.time() class Timeout(contextlib.ContextDecorator): # YOLOv5 Timeout class. Usage: @Timeout(seconds) decorator or 'with Timeout(seconds):' context manager def __init__(self, seconds, *, timeout_msg="", suppress_timeout_errors=True): """Initializes a timeout context/decorator with defined seconds, optional message, and error suppression.""" self.seconds = int(seconds) self.timeout_message = timeout_msg self.suppress = bool(suppress_timeout_errors) def _timeout_handler(self, signum, frame): """Raises a TimeoutError with a custom message when a timeout event occurs.""" raise TimeoutError(self.timeout_message) def __enter__(self): """Initializes timeout mechanism on non-Windows platforms, starting a countdown to raise TimeoutError.""" if platform.system() != "Windows": # not supported on Windows signal.signal(signal.SIGALRM, self._timeout_handler) # Set handler for SIGALRM signal.alarm(self.seconds) # start countdown for SIGALRM to be raised def __exit__(self, exc_type, exc_val, exc_tb): """Disables active alarm on non-Windows systems and optionally suppresses TimeoutError if set.""" if platform.system() != "Windows": signal.alarm(0) # Cancel SIGALRM if it's scheduled if self.suppress and exc_type is TimeoutError: # Suppress TimeoutError return True class WorkingDirectory(contextlib.ContextDecorator): # Usage: @WorkingDirectory(dir) decorator or 'with WorkingDirectory(dir):' context manager def __init__(self, new_dir): """Initializes a context manager/decorator to temporarily change the working directory.""" self.dir = new_dir # new dir self.cwd = Path.cwd().resolve() # current dir def __enter__(self): """Temporarily changes the working directory within a 'with' statement context.""" os.chdir(self.dir) def __exit__(self, exc_type, exc_val, exc_tb): """Restores the original working directory upon exiting a 'with' statement context.""" os.chdir(self.cwd) def methods(instance): """Returns list of method names for a class/instance excluding dunder methods.""" return [f for f in dir(instance) if callable(getattr(instance, f)) and not f.startswith("__")] def print_args(args: Optional[dict] = None, show_file=True, show_func=False): """Logs the arguments of the calling function, with options to include the filename and function name.""" x = inspect.currentframe().f_back # previous frame file, _, func, _, _ = inspect.getframeinfo(x) if args is None: # get args automatically args, _, _, frm = inspect.getargvalues(x) args = {k: v for k, v in frm.items() if k in args} try: file = Path(file).resolve().relative_to(ROOT).with_suffix("") except ValueError: file = Path(file).stem s = (f"{file}: " if show_file else "") + (f"{func}: " if show_func else "") LOGGER.info(colorstr(s) + ", ".join(f"{k}={v}" for k, v in args.items())) def init_seeds(seed=0, deterministic=False): """ Initializes RNG seeds and sets deterministic options if specified. See https://pytorch.org/docs/stable/notes/randomness.html """ random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) # for Multi-GPU, exception safe # torch.backends.cudnn.benchmark = True # AutoBatch problem https://github.com/ultralytics/yolov5/issues/9287 if deterministic and check_version(torch.__version__, "1.12.0"): # https://github.com/ultralytics/yolov5/pull/8213 torch.use_deterministic_algorithms(True) torch.backends.cudnn.deterministic = True os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8" os.environ["PYTHONHASHSEED"] = str(seed) def intersect_dicts(da, db, exclude=()): """Returns intersection of `da` and `db` dicts with matching keys and shapes, excluding `exclude` keys; uses `da` values. """ return {k: v for k, v in da.items() if k in db and all(x not in k for x in exclude) and v.shape == db[k].shape} def get_default_args(func): """Returns a dict of `func` default arguments by inspecting its signature.""" signature = inspect.signature(func) return {k: v.default for k, v in signature.parameters.items() if v.default is not inspect.Parameter.empty} def get_latest_run(search_dir="."): """Returns the path to the most recent 'last.pt' file in /runs to resume from, searches in `search_dir`.""" last_list = glob.glob(f"{search_dir}/**/last*.pt", recursive=True) return max(last_list, key=os.path.getctime) if last_list else "" def file_age(path=__file__): """Calculates and returns the age of a file in days based on its last modification time.""" dt = datetime.now() - datetime.fromtimestamp(Path(path).stat().st_mtime) # delta return dt.days # + dt.seconds / 86400 # fractional days def file_date(path=__file__): """Returns a human-readable file modification date in 'YYYY-M-D' format, given a file path.""" t = datetime.fromtimestamp(Path(path).stat().st_mtime) return f"{t.year}-{t.month}-{t.day}" def file_size(path): """Returns file or directory size in megabytes (MB) for a given path, where directories are recursively summed.""" mb = 1 << 20 # bytes to MiB (1024 ** 2) path = Path(path) if path.is_file(): return path.stat().st_size / mb elif path.is_dir(): return sum(f.stat().st_size for f in path.glob("**/*") if f.is_file()) / mb else: return 0.0 def check_online(): """Checks internet connectivity by attempting to create a connection to "1.1.1.1" on port 443, retries once if the first attempt fails. """ import socket def run_once(): # Check once try: socket.create_connection(("1.1.1.1", 443), 5) # check host accessibility return True except OSError: return False return run_once() or run_once() # check twice to increase robustness to intermittent connectivity issues def git_describe(path=ROOT): """ Returns a human-readable git description of the repository at `path`, or an empty string on failure. Example output is 'fv5.0-5-g3e25f1e'. See https://git-scm.com/docs/git-describe. """ try: assert (Path(path) / ".git").is_dir() return check_output(f"git -C {path} describe --tags --long --always", shell=True).decode()[:-1] except Exception: return "" @TryExcept() @WorkingDirectory(ROOT) def check_git_status(repo="ultralytics/yolov5", branch="master"): """Checks if YOLOv5 code is up-to-date with the repository, advising 'git pull' if behind; errors return informative messages. """ url = f"https://github.com/{repo}" msg = f", for updates see {url}" s = colorstr("github: ") # string assert Path(".git").exists(), s + "skipping check (not a git repository)" + msg assert check_online(), s + "skipping check (offline)" + msg splits = re.split(pattern=r"\s", string=check_output("git remote -v", shell=True).decode()) matches = [repo in s for s in splits] if any(matches): remote = splits[matches.index(True) - 1] else: remote = "ultralytics" check_output(f"git remote add {remote} {url}", shell=True) check_output(f"git fetch {remote}", shell=True, timeout=5) # git fetch local_branch = check_output("git rev-parse --abbrev-ref HEAD", shell=True).decode().strip() # checked out n = int(check_output(f"git rev-list {local_branch}..{remote}/{branch} --count", shell=True)) # commits behind if n > 0: pull = "git pull" if remote == "origin" else f"git pull {remote} {branch}" s += f"⚠️ YOLOv5 is out of date by {n} commit{'s' * (n > 1)}. Use '{pull}' or 'git clone {url}' to update." else: s += f"up to date with {url} ✅" LOGGER.info(s) @WorkingDirectory(ROOT) def check_git_info(path="."): """Checks YOLOv5 git info, returning a dict with remote URL, branch name, and commit hash.""" check_requirements("gitpython") import git try: repo = git.Repo(path) remote = repo.remotes.origin.url.replace(".git", "") # i.e. 'https://github.com/ultralytics/yolov5' commit = repo.head.commit.hexsha # i.e. '3134699c73af83aac2a481435550b968d5792c0d' try: branch = repo.active_branch.name # i.e. 'main' except TypeError: # not on any branch branch = None # i.e. 'detached HEAD' state return {"remote": remote, "branch": branch, "commit": commit} except git.exc.InvalidGitRepositoryError: # path is not a git dir return {"remote": None, "branch": None, "commit": None} def check_python(minimum="3.8.0"): """Checks if current Python version meets the minimum required version, exits if not.""" check_version(platform.python_version(), minimum, name="Python ", hard=True) def check_version(current="0.0.0", minimum="0.0.0", name="version ", pinned=False, hard=False, verbose=False): """Checks if the current version meets the minimum required version, exits or warns based on parameters.""" current, minimum = (pkg.parse_version(x) for x in (current, minimum)) result = (current == minimum) if pinned else (current >= minimum) # bool s = f"WARNING ⚠️ {name}{minimum} is required by YOLOv5, but {name}{current} is currently installed" # string if hard: assert result, emojis(s) # assert min requirements met if verbose and not result: LOGGER.warning(s) return result def check_img_size(imgsz, s=32, floor=0): """Adjusts image size to be divisible by stride `s`, supports int or list/tuple input, returns adjusted size.""" if isinstance(imgsz, int): # integer i.e. img_size=640 new_size = max(make_divisible(imgsz, int(s)), floor) else: # list i.e. img_size=[640, 480] imgsz = list(imgsz) # convert to list if tuple new_size = [max(make_divisible(x, int(s)), floor) for x in imgsz] if new_size != imgsz: LOGGER.warning(f"WARNING ⚠️ --img-size {imgsz} must be multiple of max stride {s}, updating to {new_size}") return new_size def check_imshow(warn=False): """Checks environment support for image display; warns on failure if `warn=True`.""" try: assert not is_jupyter() assert not is_docker() cv2.imshow("test", np.zeros((1, 1, 3))) cv2.waitKey(1) cv2.destroyAllWindows() cv2.waitKey(1) return True except Exception as e: if warn: LOGGER.warning(f"WARNING ⚠️ Environment does not support cv2.imshow() or PIL Image.show()\n{e}") return False def check_suffix(file="yolov5s.pt", suffix=(".pt",), msg=""): """Validates if a file or files have an acceptable suffix, raising an error if not.""" if file and suffix: if isinstance(suffix, str): suffix = [suffix] for f in file if isinstance(file, (list, tuple)) else [file]: s = Path(f).suffix.lower() # file suffix if len(s): assert s in suffix, f"{msg}{f} acceptable suffix is {suffix}" def check_yaml(file, suffix=(".yaml", ".yml")): """Searches/downloads a YAML file, verifies its suffix (.yaml or .yml), and returns the file path.""" return check_file(file, suffix) def check_file(file, suffix=""): """Searches/downloads a file, checks its suffix (if provided), and returns the file path.""" check_suffix(file, suffix) # optional file = str(file) # convert to str() if os.path.isfile(file) or not file: # exists return file elif file.startswith(("http:/", "https:/")): # download url = file # warning: Pathlib turns :// -> :/ file = Path(urllib.parse.unquote(file).split("?")[0]).name # '%2F' to '/', split https://url.com/file.txt?auth if os.path.isfile(file): LOGGER.info(f"Found {url} locally at {file}") # file already exists else: LOGGER.info(f"Downloading {url} to {file}...") torch.hub.download_url_to_file(url, file) assert Path(file).exists() and Path(file).stat().st_size > 0, f"File download failed: {url}" # check return file elif file.startswith("clearml://"): # ClearML Dataset ID assert ( "clearml" in sys.modules ), "ClearML is not installed, so cannot use ClearML dataset. Try running 'pip install clearml'." return file else: # search files = [] for d in "data", "models", "utils": # search directories files.extend(glob.glob(str(ROOT / d / "**" / file), recursive=True)) # find file assert len(files), f"File not found: {file}" # assert file was found assert len(files) == 1, f"Multiple files match '{file}', specify exact path: {files}" # assert unique return files[0] # return file def check_font(font=FONT, progress=False): """Ensures specified font exists or downloads it from Ultralytics assets, optionally displaying progress.""" font = Path(font) file = CONFIG_DIR / font.name if not font.exists() and not file.exists(): url = f"https://ultralytics.com/assets/{font.name}" LOGGER.info(f"Downloading {url} to {file}...") torch.hub.download_url_to_file(url, str(file), progress=progress) def check_dataset(data, autodownload=True): """Validates and/or auto-downloads a dataset, returning its configuration as a dictionary.""" # Download (optional) extract_dir = "" if isinstance(data, (str, Path)) and (is_zipfile(data) or is_tarfile(data)): download(data, dir=f"{DATASETS_DIR}/{Path(data).stem}", unzip=True, delete=False, curl=False, threads=1) data = next((DATASETS_DIR / Path(data).stem).rglob("*.yaml")) extract_dir, autodownload = data.parent, False # Read yaml (optional) if isinstance(data, (str, Path)): data = yaml_load(data) # dictionary # Checks for k in "train", "val", "names": assert k in data, emojis(f"data.yaml '{k}:' field missing ❌") if isinstance(data["names"], (list, tuple)): # old array format data["names"] = dict(enumerate(data["names"])) # convert to dict assert all(isinstance(k, int) for k in data["names"].keys()), "data.yaml names keys must be integers, i.e. 2: car" data["nc"] = len(data["names"]) # Resolve paths path = Path(extract_dir or data.get("path") or "") # optional 'path' default to '.' if not path.is_absolute(): path = (ROOT / path).resolve() data["path"] = path # download scripts for k in "train", "val", "test": if data.get(k): # prepend path if isinstance(data[k], str): x = (path / data[k]).resolve() if not x.exists() and data[k].startswith("../"): x = (path / data[k][3:]).resolve() data[k] = str(x) else: data[k] = [str((path / x).resolve()) for x in data[k]] # Parse yaml train, val, test, s = (data.get(x) for x in ("train", "val", "test", "download")) if val: val = [Path(x).resolve() for x in (val if isinstance(val, list) else [val])] # val path if not all(x.exists() for x in val): LOGGER.info("\nDataset not found ⚠️, missing paths %s" % [str(x) for x in val if not x.exists()]) if not s or not autodownload: raise Exception("Dataset not found ❌") t = time.time() if s.startswith("http") and s.endswith(".zip"): # URL f = Path(s).name # filename LOGGER.info(f"Downloading {s} to {f}...") torch.hub.download_url_to_file(s, f) Path(DATASETS_DIR).mkdir(parents=True, exist_ok=True) # create root unzip_file(f, path=DATASETS_DIR) # unzip Path(f).unlink() # remove zip r = None # success elif s.startswith("bash "): # bash script LOGGER.info(f"Running {s} ...") r = subprocess.run(s, shell=True) else: # python script r = exec(s, {"yaml": data}) # return None dt = f"({round(time.time() - t, 1)}s)" s = f"success ✅ {dt}, saved to {colorstr('bold', DATASETS_DIR)}" if r in (0, None) else f"failure {dt} ❌" LOGGER.info(f"Dataset download {s}") check_font("Arial.ttf" if is_ascii(data["names"]) else "Arial.Unicode.ttf", progress=True) # download fonts return data # dictionary def check_amp(model): """Checks PyTorch AMP functionality for a model, returns True if AMP operates correctly, otherwise False.""" from asone.detectors.yolov5.yolov5.models.common import AutoShape, DetectMultiBackend def amp_allclose(model, im): # All close FP32 vs AMP results m = AutoShape(model, verbose=False) # model a = m(im).xywhn[0] # FP32 inference m.amp = True b = m(im).xywhn[0] # AMP inference return a.shape == b.shape and torch.allclose(a, b, atol=0.1) # close to 10% absolute tolerance prefix = colorstr("AMP: ") device = next(model.parameters()).device # get model device if device.type in ("cpu", "mps"): return False # AMP only used on CUDA devices f = ROOT / "data" / "images" / "bus.jpg" # image to check im = f if f.exists() else "https://ultralytics.com/images/bus.jpg" if check_online() else np.ones((640, 640, 3)) try: assert amp_allclose(deepcopy(model), im) or amp_allclose(DetectMultiBackend("yolov5n.pt", device), im) LOGGER.info(f"{prefix}checks passed ✅") return True except Exception: help_url = "https://github.com/ultralytics/yolov5/issues/7908" LOGGER.warning(f"{prefix}checks failed ❌, disabling Automatic Mixed Precision. See {help_url}") return False def yaml_load(file="data.yaml"): """Safely loads and returns the contents of a YAML file specified by `file` argument.""" with open(file, errors="ignore") as f: return yaml.safe_load(f) def yaml_save(file="data.yaml", data={}): """Safely saves `data` to a YAML file specified by `file`, converting `Path` objects to strings; `data` is a dictionary. """ with open(file, "w") as f: yaml.safe_dump({k: str(v) if isinstance(v, Path) else v for k, v in data.items()}, f, sort_keys=False) def unzip_file(file, path=None, exclude=(".DS_Store", "__MACOSX")): """Unzips `file` to `path` (default: file's parent), excluding filenames containing any in `exclude` (`.DS_Store`, `__MACOSX`). """ if path is None: path = Path(file).parent # default path with ZipFile(file) as zipObj: for f in zipObj.namelist(): # list all archived filenames in the zip if all(x not in f for x in exclude): zipObj.extract(f, path=path) def url2file(url): """ Converts a URL string to a valid filename by stripping protocol, domain, and any query parameters. Example https://url.com/file.txt?auth -> file.txt """ url = str(Path(url)).replace(":/", "://") # Pathlib turns :// -> :/ return Path(urllib.parse.unquote(url)).name.split("?")[0] # '%2F' to '/', split https://url.com/file.txt?auth def download(url, dir=".", unzip=True, delete=True, curl=False, threads=1, retry=3): """Downloads and optionally unzips files concurrently, supporting retries and curl fallback.""" def download_one(url, dir): # Download 1 file success = True if os.path.isfile(url): f = Path(url) # filename else: # does not exist f = dir / Path(url).name LOGGER.info(f"Downloading {url} to {f}...") for i in range(retry + 1): if curl: success = curl_download(url, f, silent=(threads > 1)) else: torch.hub.download_url_to_file(url, f, progress=threads == 1) # torch download success = f.is_file() if success: break elif i < retry: LOGGER.warning(f"⚠️ Download failure, retrying {i + 1}/{retry} {url}...") else: LOGGER.warning(f"❌ Failed to download {url}...") if unzip and success and (f.suffix == ".gz" or is_zipfile(f) or is_tarfile(f)): LOGGER.info(f"Unzipping {f}...") if is_zipfile(f): unzip_file(f, dir) # unzip elif is_tarfile(f): subprocess.run(["tar", "xf", f, "--directory", f.parent], check=True) # unzip elif f.suffix == ".gz": subprocess.run(["tar", "xfz", f, "--directory", f.parent], check=True) # unzip if delete: f.unlink() # remove zip dir = Path(dir) dir.mkdir(parents=True, exist_ok=True) # make directory if threads > 1: pool = ThreadPool(threads) pool.imap(lambda x: download_one(*x), zip(url, repeat(dir))) # multithreaded pool.close() pool.join() else: for u in [url] if isinstance(url, (str, Path)) else url: download_one(u, dir) def make_divisible(x, divisor): """Adjusts `x` to be divisible by `divisor`, returning the nearest greater or equal value.""" if isinstance(divisor, torch.Tensor): divisor = int(divisor.max()) # to int return math.ceil(x / divisor) * divisor def clean_str(s): """Cleans a string by replacing special characters with underscore, e.g., `clean_str('#example!')` returns '_example_'. """ return re.sub(pattern="[|@#!¡·$€%&()=?¿^*;:,¨´><+]", repl="_", string=s) def one_cycle(y1=0.0, y2=1.0, steps=100): """ Generates a lambda for a sinusoidal ramp from y1 to y2 over 'steps'. See https://arxiv.org/pdf/1812.01187.pdf for details. """ return lambda x: ((1 - math.cos(x * math.pi / steps)) / 2) * (y2 - y1) + y1 def colorstr(*input): """ Colors a string using ANSI escape codes, e.g., colorstr('blue', 'hello world'). See https://en.wikipedia.org/wiki/ANSI_escape_code. """ *args, string = input if len(input) > 1 else ("blue", "bold", input[0]) # color arguments, string colors = { "black": "\033[30m", # basic colors "red": "\033[31m", "green": "\033[32m", "yellow": "\033[33m", "blue": "\033[34m", "magenta": "\033[35m", "cyan": "\033[36m", "white": "\033[37m", "bright_black": "\033[90m", # bright colors "bright_red": "\033[91m", "bright_green": "\033[92m", "bright_yellow": "\033[93m", "bright_blue": "\033[94m", "bright_magenta": "\033[95m", "bright_cyan": "\033[96m", "bright_white": "\033[97m", "end": "\033[0m", # misc "bold": "\033[1m", "underline": "\033[4m", } return "".join(colors[x] for x in args) + f"{string}" + colors["end"] def labels_to_class_weights(labels, nc=80): """Calculates class weights from labels to handle class imbalance in training; input shape: (n, 5).""" if labels[0] is None: # no labels loaded return torch.Tensor() labels = np.concatenate(labels, 0) # labels.shape = (866643, 5) for COCO classes = labels[:, 0].astype(int) # labels = [class xywh] weights = np.bincount(classes, minlength=nc) # occurrences per class # Prepend gridpoint count (for uCE training) # gpi = ((320 / 32 * np.array([1, 2, 4])) ** 2 * 3).sum() # gridpoints per image # weights = np.hstack([gpi * len(labels) - weights.sum() * 9, weights * 9]) ** 0.5 # prepend gridpoints to start weights[weights == 0] = 1 # replace empty bins with 1 weights = 1 / weights # number of targets per class weights /= weights.sum() # normalize return torch.from_numpy(weights).float() def labels_to_image_weights(labels, nc=80, class_weights=np.ones(80)): """Calculates image weights from labels using class weights for weighted sampling.""" # Usage: index = random.choices(range(n), weights=image_weights, k=1) # weighted image sample class_counts = np.array([np.bincount(x[:, 0].astype(int), minlength=nc) for x in labels]) return (class_weights.reshape(1, nc) * class_counts).sum(1) def coco80_to_coco91_class(): """ Converts COCO 80-class index to COCO 91-class index used in the paper. Reference: https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/ """ # a = np.loadtxt('data/coco.names', dtype='str', delimiter='\n') # b = np.loadtxt('data/coco_paper.names', dtype='str', delimiter='\n') # x1 = [list(a[i] == b).index(True) + 1 for i in range(80)] # darknet to coco # x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)] # coco to darknet return [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90, ] def xyxy2xywh(x): """Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right.""" y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = (x[..., 0] + x[..., 2]) / 2 # x center y[..., 1] = (x[..., 1] + x[..., 3]) / 2 # y center y[..., 2] = x[..., 2] - x[..., 0] # width y[..., 3] = x[..., 3] - x[..., 1] # height return y def xywh2xyxy(x): """Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right.""" y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = x[..., 0] - x[..., 2] / 2 # top left x y[..., 1] = x[..., 1] - x[..., 3] / 2 # top left y y[..., 2] = x[..., 0] + x[..., 2] / 2 # bottom right x y[..., 3] = x[..., 1] + x[..., 3] / 2 # bottom right y return y def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0): """Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right.""" y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = w * (x[..., 0] - x[..., 2] / 2) + padw # top left x y[..., 1] = h * (x[..., 1] - x[..., 3] / 2) + padh # top left y y[..., 2] = w * (x[..., 0] + x[..., 2] / 2) + padw # bottom right x y[..., 3] = h * (x[..., 1] + x[..., 3] / 2) + padh # bottom right y return y def xyxy2xywhn(x, w=640, h=640, clip=False, eps=0.0): """Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] normalized where xy1=top-left, xy2=bottom-right.""" if clip: clip_boxes(x, (h - eps, w - eps)) # warning: inplace clip y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = ((x[..., 0] + x[..., 2]) / 2) / w # x center y[..., 1] = ((x[..., 1] + x[..., 3]) / 2) / h # y center y[..., 2] = (x[..., 2] - x[..., 0]) / w # width y[..., 3] = (x[..., 3] - x[..., 1]) / h # height return y def xyn2xy(x, w=640, h=640, padw=0, padh=0): """Convert normalized segments into pixel segments, shape (n,2).""" y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = w * x[..., 0] + padw # top left x y[..., 1] = h * x[..., 1] + padh # top left y return y def segment2box(segment, width=640, height=640): """Convert 1 segment label to 1 box label, applying inside-image constraint, i.e. (xy1, xy2, ...) to (xyxy).""" x, y = segment.T # segment xy inside = (x >= 0) & (y >= 0) & (x <= width) & (y <= height) ( x, y, ) = x[inside], y[inside] return np.array([x.min(), y.min(), x.max(), y.max()]) if any(x) else np.zeros((1, 4)) # xyxy def segments2boxes(segments): """Convert segment labels to box labels, i.e. (cls, xy1, xy2, ...) to (cls, xywh).""" boxes = [] for s in segments: x, y = s.T # segment xy boxes.append([x.min(), y.min(), x.max(), y.max()]) # cls, xyxy return xyxy2xywh(np.array(boxes)) # cls, xywh def resample_segments(segments, n=1000): """Resamples an (n,2) segment to a fixed number of points for consistent representation.""" for i, s in enumerate(segments): s = np.concatenate((s, s[0:1, :]), axis=0) x = np.linspace(0, len(s) - 1, n) xp = np.arange(len(s)) segments[i] = np.concatenate([np.interp(x, xp, s[:, i]) for i in range(2)]).reshape(2, -1).T # segment xy return segments def scale_boxes(img1_shape, boxes, img0_shape, ratio_pad=None): """Rescales (xyxy) bounding boxes from img1_shape to img0_shape, optionally using provided `ratio_pad`.""" if ratio_pad is None: # calculate from img0_shape gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding else: gain = ratio_pad[0][0] pad = ratio_pad[1] boxes[..., [0, 2]] -= pad[0] # x padding boxes[..., [1, 3]] -= pad[1] # y padding boxes[..., :4] /= gain clip_boxes(boxes, img0_shape) return boxes def scale_segments(img1_shape, segments, img0_shape, ratio_pad=None, normalize=False): """Rescales segment coordinates from img1_shape to img0_shape, optionally normalizing them with custom padding.""" if ratio_pad is None: # calculate from img0_shape gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding else: gain = ratio_pad[0][0] pad = ratio_pad[1] segments[:, 0] -= pad[0] # x padding segments[:, 1] -= pad[1] # y padding segments /= gain clip_segments(segments, img0_shape) if normalize: segments[:, 0] /= img0_shape[1] # width segments[:, 1] /= img0_shape[0] # height return segments def clip_boxes(boxes, shape): """Clips bounding box coordinates (xyxy) to fit within the specified image shape (height, width).""" if isinstance(boxes, torch.Tensor): # faster individually boxes[..., 0].clamp_(0, shape[1]) # x1 boxes[..., 1].clamp_(0, shape[0]) # y1 boxes[..., 2].clamp_(0, shape[1]) # x2 boxes[..., 3].clamp_(0, shape[0]) # y2 else: # np.array (faster grouped) boxes[..., [0, 2]] = boxes[..., [0, 2]].clip(0, shape[1]) # x1, x2 boxes[..., [1, 3]] = boxes[..., [1, 3]].clip(0, shape[0]) # y1, y2 def clip_segments(segments, shape): """Clips segment coordinates (xy1, xy2, ...) to an image's boundaries given its shape (height, width).""" if isinstance(segments, torch.Tensor): # faster individually segments[:, 0].clamp_(0, shape[1]) # x segments[:, 1].clamp_(0, shape[0]) # y else: # np.array (faster grouped) segments[:, 0] = segments[:, 0].clip(0, shape[1]) # x segments[:, 1] = segments[:, 1].clip(0, shape[0]) # y def non_max_suppression( prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False, labels=(), max_det=300, nm=0, # number of masks ): """ Non-Maximum Suppression (NMS) on inference results to reject overlapping detections. Returns: list of detections, on (n,6) tensor per image [xyxy, conf, cls] """ # Checks assert 0 <= conf_thres <= 1, f"Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0" assert 0 <= iou_thres <= 1, f"Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0" if isinstance(prediction, (list, tuple)): # YOLOv5 model in validation model, output = (inference_out, loss_out) prediction = prediction[0] # select only inference output device = prediction.device mps = "mps" in device.type # Apple MPS if mps: # MPS not fully supported yet, convert tensors to CPU before NMS prediction = prediction.cpu() bs = prediction.shape[0] # batch size nc = prediction.shape[2] - nm - 5 # number of classes xc = prediction[..., 4] > conf_thres # candidates # Settings # min_wh = 2 # (pixels) minimum box width and height max_wh = 7680 # (pixels) maximum box width and height max_nms = 30000 # maximum number of boxes into torchvision.ops.nms() time_limit = 0.5 + 0.05 * bs # seconds to quit after redundant = True # require redundant detections multi_label &= nc > 1 # multiple labels per box (adds 0.5ms/img) merge = False # use merge-NMS t = time.time() mi = 5 + nc # mask start index output = [torch.zeros((0, 6 + nm), device=prediction.device)] * bs for xi, x in enumerate(prediction): # image index, image inference # Apply constraints # x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height x = x[xc[xi]] # confidence # Cat apriori labels if autolabelling if labels and len(labels[xi]): lb = labels[xi] v = torch.zeros((len(lb), nc + nm + 5), device=x.device) v[:, :4] = lb[:, 1:5] # box v[:, 4] = 1.0 # conf v[range(len(lb)), lb[:, 0].long() + 5] = 1.0 # cls x = torch.cat((x, v), 0) # If none remain process next image if not x.shape[0]: continue # Compute conf x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf # Box/Mask box = xywh2xyxy(x[:, :4]) # center_x, center_y, width, height) to (x1, y1, x2, y2) mask = x[:, mi:] # zero columns if no masks # Detections matrix nx6 (xyxy, conf, cls) if multi_label: i, j = (x[:, 5:mi] > conf_thres).nonzero(as_tuple=False).T x = torch.cat((box[i], x[i, 5 + j, None], j[:, None].float(), mask[i]), 1) else: # best class only conf, j = x[:, 5:mi].max(1, keepdim=True) x = torch.cat((box, conf, j.float(), mask), 1)[conf.view(-1) > conf_thres] # Filter by class if classes is not None: x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)] # Apply finite constraint # if not torch.isfinite(x).all(): # x = x[torch.isfinite(x).all(1)] # Check shape n = x.shape[0] # number of boxes if not n: # no boxes continue x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence and remove excess boxes # Batched NMS c = x[:, 5:6] * (0 if agnostic else max_wh) # classes boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS i = i[:max_det] # limit detections if merge and (1 < n < 3e3): # Merge NMS (boxes merged using weighted mean) # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4) iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix weights = iou * scores[None] # box weights x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes if redundant: i = i[iou.sum(1) > 1] # require redundancy output[xi] = x[i] if mps: output[xi] = output[xi].to(device) if (time.time() - t) > time_limit: LOGGER.warning(f"WARNING ⚠️ NMS time limit {time_limit:.3f}s exceeded") break # time limit exceeded return output def strip_optimizer(f="best.pt", s=""): """ Strips optimizer and optionally saves checkpoint to finalize training; arguments are file path 'f' and save path 's'. Example: from utils.general import *; strip_optimizer() """ x = torch.load(f, map_location=torch.device("cpu")) if x.get("ema"): x["model"] = x["ema"] # replace model with ema for k in "optimizer", "best_fitness", "ema", "updates": # keys x[k] = None x["epoch"] = -1 x["model"].half() # to FP16 for p in x["model"].parameters(): p.requires_grad = False torch.save(x, s or f) mb = os.path.getsize(s or f) / 1e6 # filesize LOGGER.info(f"Optimizer stripped from {f},{f' saved as {s},' if s else ''} {mb:.1f}MB") def print_mutation(keys, results, hyp, save_dir, bucket, prefix=colorstr("evolve: ")): """Logs evolution results and saves to CSV and YAML in `save_dir`, optionally syncs with `bucket`.""" evolve_csv = save_dir / "evolve.csv" evolve_yaml = save_dir / "hyp_evolve.yaml" keys = tuple(keys) + tuple(hyp.keys()) # [results + hyps] keys = tuple(x.strip() for x in keys) vals = results + tuple(hyp.values()) n = len(keys) # Download (optional) if bucket: url = f"gs://{bucket}/evolve.csv" if gsutil_getsize(url) > (evolve_csv.stat().st_size if evolve_csv.exists() else 0): subprocess.run(["gsutil", "cp", f"{url}", f"{save_dir}"]) # download evolve.csv if larger than local # Log to evolve.csv s = "" if evolve_csv.exists() else (("%20s," * n % keys).rstrip(",") + "\n") # add header with open(evolve_csv, "a") as f: f.write(s + ("%20.5g," * n % vals).rstrip(",") + "\n") # Save yaml with open(evolve_yaml, "w") as f: data = pd.read_csv(evolve_csv, skipinitialspace=True) data = data.rename(columns=lambda x: x.strip()) # strip keys i = np.argmax(fitness(data.values[:, :4])) # generations = len(data) f.write( "# YOLOv5 Hyperparameter Evolution Results\n" + f"# Best generation: {i}\n" + f"# Last generation: {generations - 1}\n" + "# " + ", ".join(f"{x.strip():>20s}" for x in keys[:7]) + "\n" + "# " + ", ".join(f"{x:>20.5g}" for x in data.values[i, :7]) + "\n\n" ) yaml.safe_dump(data.loc[i][7:].to_dict(), f, sort_keys=False) # Print to screen LOGGER.info( prefix + f"{generations} generations finished, current result:\n" + prefix + ", ".join(f"{x.strip():>20s}" for x in keys) + "\n" + prefix + ", ".join(f"{x:20.5g}" for x in vals) + "\n\n" ) if bucket: subprocess.run(["gsutil", "cp", f"{evolve_csv}", f"{evolve_yaml}", f"gs://{bucket}"]) # upload def apply_classifier(x, model, img, im0): """Applies second-stage classifier to YOLO outputs, filtering detections by class match.""" # Example model = torchvision.models.__dict__['efficientnet_b0'](pretrained=True).to(device).eval() im0 = [im0] if isinstance(im0, np.ndarray) else im0 for i, d in enumerate(x): # per image if d is not None and len(d): d = d.clone() # Reshape and pad cutouts b = xyxy2xywh(d[:, :4]) # boxes b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1) # rectangle to square b[:, 2:] = b[:, 2:] * 1.3 + 30 # pad d[:, :4] = xywh2xyxy(b).long() # Rescale boxes from img_size to im0 size scale_boxes(img.shape[2:], d[:, :4], im0[i].shape) # Classes pred_cls1 = d[:, 5].long() ims = [] for a in d: cutout = im0[i][int(a[1]) : int(a[3]), int(a[0]) : int(a[2])] im = cv2.resize(cutout, (224, 224)) # BGR im = im[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 im = np.ascontiguousarray(im, dtype=np.float32) # uint8 to float32 im /= 255 # 0 - 255 to 0.0 - 1.0 ims.append(im) pred_cls2 = model(torch.Tensor(ims).to(d.device)).argmax(1) # classifier prediction x[i] = x[i][pred_cls1 == pred_cls2] # retain matching class detections return x def increment_path(path, exist_ok=False, sep="", mkdir=False): """ Generates an incremented file or directory path if it exists, with optional mkdir; args: path, exist_ok=False, sep="", mkdir=False. Example: runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc """ path = Path(path) # os-agnostic if path.exists() and not exist_ok: path, suffix = (path.with_suffix(""), path.suffix) if path.is_file() else (path, "") # Method 1 for n in range(2, 9999): p = f"{path}{sep}{n}{suffix}" # increment path if not os.path.exists(p): # break path = Path(p) # Method 2 (deprecated) # dirs = glob.glob(f"{path}{sep}*") # similar paths # matches = [re.search(rf"{path.stem}{sep}(\d+)", d) for d in dirs] # i = [int(m.groups()[0]) for m in matches if m] # indices # n = max(i) + 1 if i else 2 # increment number # path = Path(f"{path}{sep}{n}{suffix}") # increment path if mkdir: path.mkdir(parents=True, exist_ok=True) # make directory return path # OpenCV Multilanguage-friendly functions ------------------------------------------------------------------------------------ imshow_ = cv2.imshow # copy to avoid recursion errors def imread(filename, flags=cv2.IMREAD_COLOR): """Reads an image from a file and returns it as a numpy array, using OpenCV's imdecode to support multilanguage paths. """ return cv2.imdecode(np.fromfile(filename, np.uint8), flags) def imwrite(filename, img): """Writes an image to a file, returns True on success and False on failure, supports multilanguage paths.""" try: cv2.imencode(Path(filename).suffix, img)[1].tofile(filename) return True except Exception: return False def imshow(path, im): """Displays an image using Unicode path, requires encoded path and image matrix as input.""" imshow_(path.encode("unicode_escape").decode(), im) if Path(inspect.stack()[0].filename).parent.parent.as_posix() in inspect.stack()[-1].filename: cv2.imread, cv2.imwrite, cv2.imshow = imread, imwrite, imshow # redefine # Variables ------------------------------------------------------------------------------------------------------------ ================================================ FILE: asone/detectors/yolov5/yolov5/utils/metrics.py ================================================ # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license """Model validation metrics.""" import math import warnings from pathlib import Path import matplotlib.pyplot as plt import numpy as np import torch from asone.detectors.yolov5.yolov5.utils import TryExcept, threaded def fitness(x): """Calculates fitness of a model using weighted sum of metrics P, R, mAP@0.5, mAP@0.5:0.95.""" w = [0.0, 0.0, 0.1, 0.9] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return (x[:, :4] * w).sum(1) def smooth(y, f=0.05): """Applies box filter smoothing to array `y` with fraction `f`, yielding a smoothed array.""" nf = round(len(y) * f * 2) // 2 + 1 # number of filter elements (must be odd) p = np.ones(nf // 2) # ones padding yp = np.concatenate((p * y[0], y, p * y[-1]), 0) # y padded return np.convolve(yp, np.ones(nf) / nf, mode="valid") # y-smoothed def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir=".", names=(), eps=1e-16, prefix=""): """ Compute the average precision, given the recall and precision curves. Source: https://github.com/rafaelpadilla/Object-Detection-Metrics. # Arguments tp: True positives (nparray, nx1 or nx10). conf: Objectness value from 0-1 (nparray). pred_cls: Predicted object classes (nparray). target_cls: True object classes (nparray). plot: Plot precision-recall curve at mAP@0.5 save_dir: Plot save directory # Returns The average precision as computed in py-faster-rcnn. """ # Sort by objectness i = np.argsort(-conf) tp, conf, pred_cls = tp[i], conf[i], pred_cls[i] # Find unique classes unique_classes, nt = np.unique(target_cls, return_counts=True) nc = unique_classes.shape[0] # number of classes, number of detections # Create Precision-Recall curve and compute AP for each class px, py = np.linspace(0, 1, 1000), [] # for plotting ap, p, r = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000)) for ci, c in enumerate(unique_classes): i = pred_cls == c n_l = nt[ci] # number of labels n_p = i.sum() # number of predictions if n_p == 0 or n_l == 0: continue # Accumulate FPs and TPs fpc = (1 - tp[i]).cumsum(0) tpc = tp[i].cumsum(0) # Recall recall = tpc / (n_l + eps) # recall curve r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # negative x, xp because xp decreases # Precision precision = tpc / (tpc + fpc) # precision curve p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1) # p at pr_score # AP from recall-precision curve for j in range(tp.shape[1]): ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j]) if plot and j == 0: py.append(np.interp(px, mrec, mpre)) # precision at mAP@0.5 # Compute F1 (harmonic mean of precision and recall) f1 = 2 * p * r / (p + r + eps) names = [v for k, v in names.items() if k in unique_classes] # list: only classes that have data names = dict(enumerate(names)) # to dict if plot: plot_pr_curve(px, py, ap, Path(save_dir) / f"{prefix}PR_curve.png", names) plot_mc_curve(px, f1, Path(save_dir) / f"{prefix}F1_curve.png", names, ylabel="F1") plot_mc_curve(px, p, Path(save_dir) / f"{prefix}P_curve.png", names, ylabel="Precision") plot_mc_curve(px, r, Path(save_dir) / f"{prefix}R_curve.png", names, ylabel="Recall") i = smooth(f1.mean(0), 0.1).argmax() # max F1 index p, r, f1 = p[:, i], r[:, i], f1[:, i] tp = (r * nt).round() # true positives fp = (tp / (p + eps) - tp).round() # false positives return tp, fp, p, r, f1, ap, unique_classes.astype(int) def compute_ap(recall, precision): """Compute the average precision, given the recall and precision curves # Arguments recall: The recall curve (list) precision: The precision curve (list) # Returns Average precision, precision curve, recall curve """ # Append sentinel values to beginning and end mrec = np.concatenate(([0.0], recall, [1.0])) mpre = np.concatenate(([1.0], precision, [0.0])) # Compute the precision envelope mpre = np.flip(np.maximum.accumulate(np.flip(mpre))) # Integrate area under curve method = "interp" # methods: 'continuous', 'interp' if method == "interp": x = np.linspace(0, 1, 101) # 101-point interp (COCO) ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate else: # 'continuous' i = np.where(mrec[1:] != mrec[:-1])[0] # points where x axis (recall) changes ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve return ap, mpre, mrec class ConfusionMatrix: # Updated version of https://github.com/kaanakan/object_detection_confusion_matrix def __init__(self, nc, conf=0.25, iou_thres=0.45): """Initializes ConfusionMatrix with given number of classes, confidence, and IoU threshold.""" self.matrix = np.zeros((nc + 1, nc + 1)) self.nc = nc # number of classes self.conf = conf self.iou_thres = iou_thres def process_batch(self, detections, labels): """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Arguments: detections (Array[N, 6]), x1, y1, x2, y2, conf, class labels (Array[M, 5]), class, x1, y1, x2, y2 Returns: None, updates confusion matrix accordingly """ if detections is None: gt_classes = labels.int() for gc in gt_classes: self.matrix[self.nc, gc] += 1 # background FN return detections = detections[detections[:, 4] > self.conf] gt_classes = labels[:, 0].int() detection_classes = detections[:, 5].int() iou = box_iou(labels[:, 1:], detections[:, :4]) x = torch.where(iou > self.iou_thres) if x[0].shape[0]: matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy() if x[0].shape[0] > 1: matches = matches[matches[:, 2].argsort()[::-1]] matches = matches[np.unique(matches[:, 1], return_index=True)[1]] matches = matches[matches[:, 2].argsort()[::-1]] matches = matches[np.unique(matches[:, 0], return_index=True)[1]] else: matches = np.zeros((0, 3)) n = matches.shape[0] > 0 m0, m1, _ = matches.transpose().astype(int) for i, gc in enumerate(gt_classes): j = m0 == i if n and sum(j) == 1: self.matrix[detection_classes[m1[j]], gc] += 1 # correct else: self.matrix[self.nc, gc] += 1 # true background if n: for i, dc in enumerate(detection_classes): if not any(m1 == i): self.matrix[dc, self.nc] += 1 # predicted background def tp_fp(self): """Calculates true positives (tp) and false positives (fp) excluding the background class from the confusion matrix. """ tp = self.matrix.diagonal() # true positives fp = self.matrix.sum(1) - tp # false positives # fn = self.matrix.sum(0) - tp # false negatives (missed detections) return tp[:-1], fp[:-1] # remove background class @TryExcept("WARNING ⚠️ ConfusionMatrix plot failure") def plot(self, normalize=True, save_dir="", names=()): """Plots confusion matrix using seaborn, optional normalization; can save plot to specified directory.""" import seaborn as sn array = self.matrix / ((self.matrix.sum(0).reshape(1, -1) + 1e-9) if normalize else 1) # normalize columns array[array < 0.005] = np.nan # don't annotate (would appear as 0.00) fig, ax = plt.subplots(1, 1, figsize=(12, 9), tight_layout=True) nc, nn = self.nc, len(names) # number of classes, names sn.set(font_scale=1.0 if nc < 50 else 0.8) # for label size labels = (0 < nn < 99) and (nn == nc) # apply names to ticklabels ticklabels = (names + ["background"]) if labels else "auto" with warnings.catch_warnings(): warnings.simplefilter("ignore") # suppress empty matrix RuntimeWarning: All-NaN slice encountered sn.heatmap( array, ax=ax, annot=nc < 30, annot_kws={"size": 8}, cmap="Blues", fmt=".2f", square=True, vmin=0.0, xticklabels=ticklabels, yticklabels=ticklabels, ).set_facecolor((1, 1, 1)) ax.set_xlabel("True") ax.set_ylabel("Predicted") ax.set_title("Confusion Matrix") fig.savefig(Path(save_dir) / "confusion_matrix.png", dpi=250) plt.close(fig) def print(self): """Prints the confusion matrix row-wise, with each class and its predictions separated by spaces.""" for i in range(self.nc + 1): print(" ".join(map(str, self.matrix[i]))) def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7): """ Calculates IoU, GIoU, DIoU, or CIoU between two boxes, supporting xywh/xyxy formats. Input shapes are box1(1,4) to box2(n,4). """ # Get the coordinates of bounding boxes if xywh: # transform from xywh to xyxy (x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1) w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2 b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_ b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_ else: # x1, y1, x2, y2 = box1 b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1) b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1) w1, h1 = b1_x2 - b1_x1, (b1_y2 - b1_y1).clamp(eps) w2, h2 = b2_x2 - b2_x1, (b2_y2 - b2_y1).clamp(eps) # Intersection area inter = (b1_x2.minimum(b2_x2) - b1_x1.maximum(b2_x1)).clamp(0) * ( b1_y2.minimum(b2_y2) - b1_y1.maximum(b2_y1) ).clamp(0) # Union Area union = w1 * h1 + w2 * h2 - inter + eps # IoU iou = inter / union if CIoU or DIoU or GIoU: cw = b1_x2.maximum(b2_x2) - b1_x1.minimum(b2_x1) # convex (smallest enclosing box) width ch = b1_y2.maximum(b2_y2) - b1_y1.minimum(b2_y1) # convex height if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1 c2 = cw**2 + ch**2 + eps # convex diagonal squared rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center dist ** 2 if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47 v = (4 / math.pi**2) * (torch.atan(w2 / h2) - torch.atan(w1 / h1)).pow(2) with torch.no_grad(): alpha = v / (v - iou + (1 + eps)) return iou - (rho2 / c2 + v * alpha) # CIoU return iou - rho2 / c2 # DIoU c_area = cw * ch + eps # convex area return iou - (c_area - union) / c_area # GIoU https://arxiv.org/pdf/1902.09630.pdf return iou # IoU def box_iou(box1, box2, eps=1e-7): # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Arguments: box1 (Tensor[N, 4]) box2 (Tensor[M, 4]) Returns: iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 """ # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2) (a1, a2), (b1, b2) = box1.unsqueeze(1).chunk(2, 2), box2.unsqueeze(0).chunk(2, 2) inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp(0).prod(2) # IoU = inter / (area1 + area2 - inter) return inter / ((a2 - a1).prod(2) + (b2 - b1).prod(2) - inter + eps) def bbox_ioa(box1, box2, eps=1e-7): """ Returns the intersection over box2 area given box1, box2. Boxes are x1y1x2y2 box1: np.array of shape(4) box2: np.array of shape(nx4) returns: np.array of shape(n) """ # Get the coordinates of bounding boxes b1_x1, b1_y1, b1_x2, b1_y2 = box1 b2_x1, b2_y1, b2_x2, b2_y2 = box2.T # Intersection area inter_area = (np.minimum(b1_x2, b2_x2) - np.maximum(b1_x1, b2_x1)).clip(0) * ( np.minimum(b1_y2, b2_y2) - np.maximum(b1_y1, b2_y1) ).clip(0) # box2 area box2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + eps # Intersection over box2 area return inter_area / box2_area def wh_iou(wh1, wh2, eps=1e-7): """Calculates the Intersection over Union (IoU) for two sets of widths and heights; `wh1` and `wh2` should be nx2 and mx2 tensors. """ wh1 = wh1[:, None] # [N,1,2] wh2 = wh2[None] # [1,M,2] inter = torch.min(wh1, wh2).prod(2) # [N,M] return inter / (wh1.prod(2) + wh2.prod(2) - inter + eps) # iou = inter / (area1 + area2 - inter) # Plots ---------------------------------------------------------------------------------------------------------------- @threaded def plot_pr_curve(px, py, ap, save_dir=Path("pr_curve.png"), names=()): """Plots precision-recall curve, optionally per class, saving to `save_dir`; `px`, `py` are lists, `ap` is Nx2 array, `names` optional. """ fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) py = np.stack(py, axis=1) if 0 < len(names) < 21: # display per-class legend if < 21 classes for i, y in enumerate(py.T): ax.plot(px, y, linewidth=1, label=f"{names[i]} {ap[i, 0]:.3f}") # plot(recall, precision) else: ax.plot(px, py, linewidth=1, color="grey") # plot(recall, precision) ax.plot(px, py.mean(1), linewidth=3, color="blue", label="all classes %.3f mAP@0.5" % ap[:, 0].mean()) ax.set_xlabel("Recall") ax.set_ylabel("Precision") ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.legend(bbox_to_anchor=(1.04, 1), loc="upper left") ax.set_title("Precision-Recall Curve") fig.savefig(save_dir, dpi=250) plt.close(fig) @threaded def plot_mc_curve(px, py, save_dir=Path("mc_curve.png"), names=(), xlabel="Confidence", ylabel="Metric"): """Plots a metric-confidence curve for model predictions, supporting per-class visualization and smoothing.""" fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) if 0 < len(names) < 21: # display per-class legend if < 21 classes for i, y in enumerate(py): ax.plot(px, y, linewidth=1, label=f"{names[i]}") # plot(confidence, metric) else: ax.plot(px, py.T, linewidth=1, color="grey") # plot(confidence, metric) y = smooth(py.mean(0), 0.05) ax.plot(px, y, linewidth=3, color="blue", label=f"all classes {y.max():.2f} at {px[y.argmax()]:.3f}") ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.legend(bbox_to_anchor=(1.04, 1), loc="upper left") ax.set_title(f"{ylabel}-Confidence Curve") fig.savefig(save_dir, dpi=250) plt.close(fig) ================================================ FILE: asone/detectors/yolov5/yolov5/utils/torch_utils.py ================================================ # YOLOv5 🚀 by Ultralytics, GPL-3.0 license """ PyTorch utils """ import math import os import platform import subprocess import time import warnings from contextlib import contextmanager from copy import deepcopy from pathlib import Path import torch import torch.distributed as dist import torch.nn as nn import torch.nn.functional as F from torch.nn.parallel import DistributedDataParallel as DDP # from utils.general import LOGGER, check_version, colorstr, file_date, git_describe LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html RANK = int(os.getenv('RANK', -1)) WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1)) try: import thop # for FLOPs computation except ImportError: thop = None # Suppress PyTorch warnings warnings.filterwarnings('ignore', message='User provided device_type of \'cuda\', but CUDA is not available. Disabling') def smart_DDP(model): # Model DDP creation with checks assert not check_version(torch.__version__, '1.12.0', pinned=True), \ 'torch==1.12.0 torchvision==0.13.0 DDP training is not supported due to a known issue. ' \ 'Please upgrade or downgrade torch to use DDP. See https://github.com/ultralytics/yolov5/issues/8395' if check_version(torch.__version__, '1.11.0'): return DDP(model, device_ids=[LOCAL_RANK], output_device=LOCAL_RANK, static_graph=True) else: return DDP(model, device_ids=[LOCAL_RANK], output_device=LOCAL_RANK) @contextmanager def torch_distributed_zero_first(local_rank: int): # Decorator to make all processes in distributed training wait for each local_master to do something if local_rank not in [-1, 0]: dist.barrier(device_ids=[local_rank]) yield if local_rank == 0: dist.barrier(device_ids=[0]) def device_count(): # Returns number of CUDA devices available. Safe version of torch.cuda.device_count(). Supports Linux and Windows assert platform.system() in ('Linux', 'Windows'), 'device_count() only supported on Linux or Windows' try: cmd = 'nvidia-smi -L | wc -l' if platform.system() == 'Linux' else 'nvidia-smi -L | find /c /v ""' # Windows return int(subprocess.run(cmd, shell=True, capture_output=True, check=True).stdout.decode().split()[-1]) except Exception: return 0 def select_device(device='', batch_size=0, newline=True): # device = None or 'cpu' or 0 or '0' or '0,1,2,3' s = f'YOLOv5 🚀 {git_describe() or file_date()} Python-{platform.python_version()} torch-{torch.__version__} ' device = str(device).strip().lower().replace('cuda:', '').replace('none', '') # to string, 'cuda:0' to '0' cpu = device == 'cpu' mps = device == 'mps' # Apple Metal Performance Shaders (MPS) if cpu or mps: os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False elif device: # non-cpu device requested os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable - must be before assert is_available() assert torch.cuda.is_available() and torch.cuda.device_count() >= len(device.replace(',', '')), \ f"Invalid CUDA '--device {device}' requested, use '--device cpu' or pass valid CUDA device(s)" if not (cpu or mps) and torch.cuda.is_available(): # prefer GPU if available devices = device.split(',') if device else '0' # range(torch.cuda.device_count()) # i.e. 0,1,6,7 n = len(devices) # device count if n > 1 and batch_size > 0: # check batch_size is divisible by device_count assert batch_size % n == 0, f'batch-size {batch_size} not multiple of GPU count {n}' space = ' ' * (len(s) + 1) for i, d in enumerate(devices): p = torch.cuda.get_device_properties(i) s += f"{'' if i == 0 else space}CUDA:{d} ({p.name}, {p.total_memory / (1 << 20):.0f}MiB)\n" # bytes to MB arg = 'cuda:0' elif mps and getattr(torch, 'has_mps', False) and torch.backends.mps.is_available(): # prefer MPS if available s += 'MPS\n' arg = 'mps' else: # revert to CPU s += 'CPU\n' arg = 'cpu' if not newline: s = s.rstrip() LOGGER.info(s.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else s) # emoji-safe return torch.device(arg) def time_sync(): # PyTorch-accurate time if torch.cuda.is_available(): torch.cuda.synchronize() return time.time() def profile(input, ops, n=10, device=None): results = [] if not isinstance(device, torch.device): device = select_device(device) print(f"{'Params':>12s}{'GFLOPs':>12s}{'GPU_mem (GB)':>14s}{'forward (ms)':>14s}{'backward (ms)':>14s}" f"{'input':>24s}{'output':>24s}") for x in input if isinstance(input, list) else [input]: x = x.to(device) x.requires_grad = True for m in ops if isinstance(ops, list) else [ops]: m = m.to(device) if hasattr(m, 'to') else m # device m = m.half() if hasattr(m, 'half') and isinstance(x, torch.Tensor) and x.dtype is torch.float16 else m tf, tb, t = 0, 0, [0, 0, 0] # dt forward, backward try: flops = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 # GFLOPs except Exception: flops = 0 try: for _ in range(n): t[0] = time_sync() y = m(x) t[1] = time_sync() try: _ = (sum(yi.sum() for yi in y) if isinstance(y, list) else y).sum().backward() t[2] = time_sync() except Exception: # no backward method # print(e) # for debug t[2] = float('nan') tf += (t[1] - t[0]) * 1000 / n # ms per op forward tb += (t[2] - t[1]) * 1000 / n # ms per op backward mem = torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0 # (GB) s_in, s_out = (tuple(x.shape) if isinstance(x, torch.Tensor) else 'list' for x in (x, y)) # shapes p = sum(x.numel() for x in m.parameters()) if isinstance(m, nn.Module) else 0 # parameters print(f'{p:12}{flops:12.4g}{mem:>14.3f}{tf:14.4g}{tb:14.4g}{str(s_in):>24s}{str(s_out):>24s}') results.append([p, flops, mem, tf, tb, s_in, s_out]) except Exception as e: print(e) results.append(None) torch.cuda.empty_cache() return results def is_parallel(model): # Returns True if model is of type DP or DDP return type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel) def de_parallel(model): # De-parallelize a model: returns single-GPU model if model is of type DP or DDP return model.module if is_parallel(model) else model def initialize_weights(model): for m in model.modules(): t = type(m) if t is nn.Conv2d: pass # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif t is nn.BatchNorm2d: m.eps = 1e-3 m.momentum = 0.03 elif t in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU]: m.inplace = True def find_modules(model, mclass=nn.Conv2d): # Finds layer indices matching module class 'mclass' return [i for i, m in enumerate(model.module_list) if isinstance(m, mclass)] def sparsity(model): # Return global model sparsity a, b = 0, 0 for p in model.parameters(): a += p.numel() b += (p == 0).sum() return b / a def prune(model, amount=0.3): # Prune model to requested global sparsity import torch.nn.utils.prune as prune print('Pruning model... ', end='') for name, m in model.named_modules(): if isinstance(m, nn.Conv2d): prune.l1_unstructured(m, name='weight', amount=amount) # prune prune.remove(m, 'weight') # make permanent print(' %.3g global sparsity' % sparsity(model)) def fuse_conv_and_bn(conv, bn): # Fuse Conv2d() and BatchNorm2d() layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/ fusedconv = nn.Conv2d(conv.in_channels, conv.out_channels, kernel_size=conv.kernel_size, stride=conv.stride, padding=conv.padding, groups=conv.groups, bias=True).requires_grad_(False).to(conv.weight.device) # Prepare filters w_conv = conv.weight.clone().view(conv.out_channels, -1) w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.shape)) # Prepare spatial bias b_conv = torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps)) fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) return fusedconv def model_info(model, verbose=False, img_size=640): # Model information. img_size may be int or list, i.e. img_size=640 or img_size=[640, 320] n_p = sum(x.numel() for x in model.parameters()) # number parameters n_g = sum(x.numel() for x in model.parameters() if x.requires_grad) # number gradients if verbose: print(f"{'layer':>5} {'name':>40} {'gradient':>9} {'parameters':>12} {'shape':>20} {'mu':>10} {'sigma':>10}") for i, (name, p) in enumerate(model.named_parameters()): name = name.replace('module_list.', '') print('%5g %40s %9s %12g %20s %10.3g %10.3g' % (i, name, p.requires_grad, p.numel(), list(p.shape), p.mean(), p.std())) try: # FLOPs from thop import profile stride = max(int(model.stride.max()), 32) if hasattr(model, 'stride') else 32 img = torch.zeros((1, model.yaml.get('ch', 3), stride, stride), device=next(model.parameters()).device) # input flops = profile(deepcopy(model), inputs=(img,), verbose=False)[0] / 1E9 * 2 # stride GFLOPs img_size = img_size if isinstance(img_size, list) else [img_size, img_size] # expand if int/float fs = ', %.1f GFLOPs' % (flops * img_size[0] / stride * img_size[1] / stride) # 640x640 GFLOPs except Exception: fs = '' name = Path(model.yaml_file).stem.replace('yolov5', 'YOLOv5') if hasattr(model, 'yaml_file') else 'Model' # LOGGER.info(f"{name} summary: {len(list(model.modules()))} layers, {n_p} parameters, {n_g} gradients{fs}") def scale_img(img, ratio=1.0, same_shape=False, gs=32): # img(16,3,256,416) # Scales img(bs,3,y,x) by ratio constrained to gs-multiple if ratio == 1.0: return img h, w = img.shape[2:] s = (int(h * ratio), int(w * ratio)) # new size img = F.interpolate(img, size=s, mode='bilinear', align_corners=False) # resize if not same_shape: # pad/crop img h, w = (math.ceil(x * ratio / gs) * gs for x in (h, w)) return F.pad(img, [0, w - s[1], 0, h - s[0]], value=0.447) # value = imagenet mean def copy_attr(a, b, include=(), exclude=()): # Copy attributes from b to a, options to only include [...] and to exclude [...] for k, v in b.__dict__.items(): if (len(include) and k not in include) or k.startswith('_') or k in exclude: continue else: setattr(a, k, v) def smart_optimizer(model, name='Adam', lr=0.001, momentum=0.9, weight_decay=1e-5): # YOLOv5 3-param group optimizer: 0) weights with decay, 1) weights no decay, 2) biases no decay g = [], [], [] # optimizer parameter groups bn = tuple(v for k, v in nn.__dict__.items() if 'Norm' in k) # normalization layers, i.e. BatchNorm2d() for v in model.modules(): if hasattr(v, 'bias') and isinstance(v.bias, nn.Parameter): # bias (no decay) g[2].append(v.bias) if isinstance(v, bn): # weight (no decay) g[1].append(v.weight) elif hasattr(v, 'weight') and isinstance(v.weight, nn.Parameter): # weight (with decay) g[0].append(v.weight) if name == 'Adam': optimizer = torch.optim.Adam(g[2], lr=lr, betas=(momentum, 0.999)) # adjust beta1 to momentum elif name == 'AdamW': optimizer = torch.optim.AdamW(g[2], lr=lr, betas=(momentum, 0.999), weight_decay=0.0) elif name == 'RMSProp': optimizer = torch.optim.RMSprop(g[2], lr=lr, momentum=momentum) elif name == 'SGD': optimizer = torch.optim.SGD(g[2], lr=lr, momentum=momentum, nesterov=True) else: raise NotImplementedError(f'Optimizer {name} not implemented.') optimizer.add_param_group({'params': g[0], 'weight_decay': weight_decay}) # add g0 with weight_decay optimizer.add_param_group({'params': g[1], 'weight_decay': 0.0}) # add g1 (BatchNorm2d weights) LOGGER.info(f"{colorstr('optimizer:')} {type(optimizer).__name__} with parameter groups " f"{len(g[1])} weight (no decay), {len(g[0])} weight, {len(g[2])} bias") return optimizer class EarlyStopping: # YOLOv5 simple early stopper def __init__(self, patience=30): self.best_fitness = 0.0 # i.e. mAP self.best_epoch = 0 self.patience = patience or float('inf') # epochs to wait after fitness stops improving to stop self.possible_stop = False # possible stop may occur next epoch def __call__(self, epoch, fitness): if fitness >= self.best_fitness: # >= 0 to allow for early zero-fitness stage of training self.best_epoch = epoch self.best_fitness = fitness delta = epoch - self.best_epoch # epochs without improvement self.possible_stop = delta >= (self.patience - 1) # possible stop may occur next epoch stop = delta >= self.patience # stop training if patience exceeded if stop: LOGGER.info(f'Stopping training early as no improvement observed in last {self.patience} epochs. ' f'Best results observed at epoch {self.best_epoch}, best model saved as best.pt.\n' f'To update EarlyStopping(patience={self.patience}) pass a new patience value, ' f'i.e. `python train.py --patience 300` or use `--patience 0` to disable EarlyStopping.') return stop class ModelEMA: """ Updated Exponential Moving Average (EMA) from https://github.com/rwightman/pytorch-image-models Keeps a moving average of everything in the model state_dict (parameters and buffers) For EMA details see https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage """ def __init__(self, model, decay=0.9999, tau=2000, updates=0): # Create EMA self.ema = deepcopy(de_parallel(model)).eval() # FP32 EMA # if next(model.parameters()).device.type != 'cpu': # self.ema.half() # FP16 EMA self.updates = updates # number of EMA updates self.decay = lambda x: decay * (1 - math.exp(-x / tau)) # decay exponential ramp (to help early epochs) for p in self.ema.parameters(): p.requires_grad_(False) def update(self, model): # Update EMA parameters with torch.no_grad(): self.updates += 1 d = self.decay(self.updates) msd = de_parallel(model).state_dict() # model state_dict for k, v in self.ema.state_dict().items(): if v.dtype.is_floating_point: v *= d v += (1 - d) * msd[k].detach() def update_attr(self, model, include=(), exclude=('process_group', 'reducer')): # Update EMA attributes copy_attr(self.ema, model, include, exclude) ================================================ FILE: asone/detectors/yolov5/yolov5/utils/yolov5_utils.py ================================================ import contextlib import time import numpy as np import torch import torchvision import cv2 import sys from pathlib import Path def box_area(box): # box = xyxy(4,n) return (box[2] - box[0]) * (box[3] - box[1]) def box_iou(box1, box2, eps=1e-7): # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Arguments: box1 (Tensor[N, 4]) box2 (Tensor[M, 4]) Returns: iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 """ # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2) (a1, a2), (b1, b2) = box1[:, None].chunk(2, 2), box2.chunk(2, 1) inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp(0).prod(2) # IoU = inter / (area1 + area2 - inter) return inter / (box_area(box1.T)[:, None] + box_area(box2.T) - inter + eps) def xywh2xyxy(x): # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y return y def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False, labels=(), max_det=300): """Non-Maximum Suppression (NMS) on inference results to reject overlapping bounding boxes Returns: list of detections, on (n,6) tensor per image [xyxy, conf, cls] """ # prediction = torch.Tensor(prediction) bs = prediction.shape[0] # batch size nc = prediction.shape[2] - 5 # number of classes xc = prediction[..., 4] > conf_thres # candidates # Checks assert 0 <= conf_thres <= 1, f'Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0' assert 0 <= iou_thres <= 1, f'Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0' # Settings # min_wh = 2 # (pixels) minimum box width and height max_wh = 7680 # (pixels) maximum box width and height max_nms = 30000 # maximum number of boxes into torchvision.ops.nms() time_limit = 0.3 + 0.03 * bs # seconds to quit after redundant = True # require redundant detections multi_label &= nc > 1 # multiple labels per box (adds 0.5ms/img) merge = False # use merge-NMS t = time.time() output = [torch.zeros((0, 6), device=prediction.device)] * bs for xi, x in enumerate(prediction): # image index, image inference # Apply constraints # x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height x = x[xc[xi]] # confidence # Cat apriori labels if autolabelling if labels and len(labels[xi]): lb = labels[xi] v = torch.zeros((len(lb), nc + 5), device=x.device) v[:, :4] = lb[:, 1:5] # box v[:, 4] = 1.0 # conf v[range(len(lb)), lb[:, 0].long() + 5] = 1.0 # cls x = torch.cat((x, v), 0) # If none remain process next image if not x.shape[0]: continue # Compute conf x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf # Box (center x, center y, width, height) to (x1, y1, x2, y2) # print(type(x)) box = xywh2xyxy(x[:, :4]) # Detections matrix nx6 (xyxy, conf, cls) if multi_label: i, j = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T x = torch.cat((box[i], x[i, j + 5, None], j[:, None].float()), 1) else: # best class only conf, j = x[:, 5:].max(1, keepdim=True) x = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres] # Filter by class if classes is not None: x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)] # Apply finite constraint # if not torch.isfinite(x).all(): # x = x[torch.isfinite(x).all(1)] # Check shape n = x.shape[0] # number of boxes if not n: # no boxes continue elif n > max_nms: # excess boxes x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence # Batched NMS c = x[:, 5:6] * (0 if agnostic else max_wh) # classes boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS if i.shape[0] > max_det: # limit detections i = i[:max_det] if merge and (1 < n < 3E3): # Merge NMS (boxes merged using weighted mean) # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4) iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix weights = iou * scores[None] # box weights x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes if redundant: i = i[iou.sum(1) > 1] # require redundancy output[xi] = x[i] if (time.time() - t) > time_limit: # LOGGER.warning(f'WARNING: NMS time limit {time_limit:.3f}s exceeded') break # time limit exceeded return output def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32): # Resize and pad image while meeting stride-multiple constraints shape = im.shape[:2] # current shape [height, width] if isinstance(new_shape, int): new_shape = (new_shape, new_shape) # Scale ratio (new / old) r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) if not scaleup: # only scale down, do not scale up (for better val mAP) r = min(r, 1.0) # Compute padding ratio = r, r # width, height ratios new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding if auto: # minimum rectangle dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding elif scaleFill: # stretch dw, dh = 0.0, 0.0 new_unpad = (new_shape[1], new_shape[0]) ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios dw /= 2 # divide padding into 2 sides dh /= 2 if shape[::-1] != new_unpad: # resize im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border return im, ratio, (dw, dh) def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None): # Rescale coords (xyxy) from img1_shape to img0_shape if ratio_pad is None: # calculate from img0_shape gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding else: gain = ratio_pad[0][0] pad = ratio_pad[1] coords[:, [0, 2]] -= pad[0] # x padding coords[:, [1, 3]] -= pad[1] # y padding coords[:, :4] /= gain clip_coords(coords, img0_shape) return coords def clip_coords(boxes, shape): # Clip bounding xyxy bounding boxes to image shape (height, width) if isinstance(boxes, torch.Tensor): # faster individually boxes[:, 0].clamp_(0, shape[1]) # x1 boxes[:, 1].clamp_(0, shape[0]) # y1 boxes[:, 2].clamp_(0, shape[1]) # x2 boxes[:, 3].clamp_(0, shape[0]) # y2 else: # np.array (faster grouped) boxes[:, [0, 2]] = boxes[:, [0, 2]].clip(0, shape[1]) # x1, x2 boxes[:, [1, 3]] = boxes[:, [1, 3]].clip(0, shape[0]) # y1, y2 @contextlib.contextmanager def yolov5_in_syspath(): """ Temporarily add yolov5 folder to `sys.path`. torch.hub handles it in the same way: https://github.com/pytorch/pytorch/blob/75024e228ca441290b6a1c2e564300ad507d7af6/torch/hub.py#L387 Proper fix for: #22, #134, #353, #1155, #1389, #1680, #2531, #3071 No need for such workarounds: #869, #1052, #2949 """ yolov5_folder_dir = str(Path(__file__).parents[1].absolute()) try: sys.path.insert(0, yolov5_folder_dir) yield finally: sys.path.remove(yolov5_folder_dir) ================================================ FILE: asone/detectors/yolov5/yolov5_detector.py ================================================ import os from asone.utils import get_names import numpy as np import warnings import torch from PIL import Image import onnxruntime import coremltools as ct from asone.detectors.yolov5.yolov5.utils.yolov5_utils import (non_max_suppression, scale_coords, letterbox) from asone.detectors.yolov5.yolov5.models.experimental import attempt_load from asone import utils from asone.detectors.utils.coreml_utils import yolo_to_xyxy, generalize_output_format, scale_bboxes from asone.utils.utils import PathResolver class YOLOv5Detector: def __init__(self, weights=None, use_onnx=False, mlmodel=False, use_cuda=True): self.use_onnx = use_onnx self.mlmodel = mlmodel self.device = 'cuda' if use_cuda else 'cpu' if not os.path.exists(weights): utils.download_weights(weights) with PathResolver(): # Load Model self.model = self.load_model(use_cuda, weights) def load_model(self, use_cuda, weights, fp16=False): # Device: CUDA and if fp16=True only then half precision floating point works self.fp16 = fp16 & ((not self.use_onnx or self.use_onnx) and self.device != 'cpu') # Load onnx if self.use_onnx: if use_cuda: providers = ['CUDAExecutionProvider','CPUExecutionProvider'] else: providers = ['CPUExecutionProvider'] model = onnxruntime.InferenceSession(weights, providers=providers) #Load coreml elif self.mlmodel: model = ct.models.MLModel(weights) #Load Pytorch else: model = attempt_load(weights, device=self.device, inplace=True, fuse=True) model.half() if self.fp16 else model.float() return model def image_preprocessing(self, image: list, input_shape=(640, 640))-> list: original_image = image.copy() image = letterbox(image, input_shape, stride=32, auto=False)[0] image = image.transpose((2, 0, 1))[::-1] image = np.ascontiguousarray(image, dtype=np.float32) image /= 255 # 0 - 255 to 0.0 - 1.0 if len(image.shape) == 3: image = image[None] # expand for batch dim return original_image, image def detect(self, image: list, input_shape: tuple = (640, 640), conf_thres: float = 0.25, iou_thres: float = 0.45, max_det: int = 1000, filter_classes: bool = None, agnostic_nms: bool = True, with_p6: bool = False, return_image=False) -> list: # Image Preprocessing original_image, processed_image = self.image_preprocessing(image, input_shape) # Inference if self.use_onnx: # Input names of ONNX model on which it is exported input_name = self.model.get_inputs()[0].name # Run onnx model pred = self.model.run([self.model.get_outputs()[0].name], {input_name: processed_image})[0] # Run mlmodel elif self.mlmodel: h ,w = image.shape[:2] pred = self.model.predict({"image":Image.fromarray(image).resize(input_shape)}) xyxy = yolo_to_xyxy(pred['coordinates'], input_shape) out = generalize_output_format(xyxy, pred['confidence'], conf_thres) if out != []: detections = scale_bboxes(out, image.shape[:2], input_shape) else: detections = np.empty((0, 6)) if filter_classes: class_names = get_names() filter_class_idx = [] if filter_classes: for _class in filter_classes: if _class.lower() in class_names: filter_class_idx.append(class_names.index(_class.lower())) else: warnings.warn(f"class {_class} not found in model classes list.") detections = detections[np.in1d(detections[:,5].astype(int), filter_class_idx)] return detections, {'width':w, 'height':h} # Run Pytorch model else: processed_image = torch.from_numpy(processed_image).to(self.device) # Change image floating point precision if fp16 set to true processed_image = processed_image.half() if self.fp16 else processed_image.float() pred = self.model(processed_image, augment=False, visualize=False)[0] # Post Processing if isinstance(pred, np.ndarray): pred = torch.tensor(pred, device=self.device) # print(pred) predictions = non_max_suppression(pred, conf_thres, iou_thres, agnostic=agnostic_nms, max_det=max_det) for i, prediction in enumerate(predictions): # per image if len(prediction): prediction[:, :4] = scale_coords( processed_image.shape[2:], prediction[:, :4], original_image.shape).round() predictions[i] = prediction detections = predictions[0].cpu().numpy() image_info = { 'width': original_image.shape[1], 'height': original_image.shape[0], } self.boxes = detections[:, :4] self.scores = detections[:, 4:5] self.class_ids = detections[:, 5:6] if filter_classes: class_names = get_names() filter_class_idx = [] if filter_classes: for _class in filter_classes: if _class.lower() in class_names: filter_class_idx.append(class_names.index(_class.lower())) else: warnings.warn(f"class {_class} not found in model classes list.") detections = detections[np.in1d(detections[:,5].astype(int), filter_class_idx)] if return_image: return detections, original_image else: return detections, image_info ================================================ FILE: asone/detectors/yolov6/__init__.py ================================================ from .yolov6_detector import YOLOv6Detector __all__ = ['YOLOv6Detector'] ================================================ FILE: asone/detectors/yolov6/yolov6/__init__.py ================================================ ================================================ FILE: asone/detectors/yolov6/yolov6/assigners/__init__.py ================================================ from .atss_assigner import ATSSAssigner from .tal_assigner import TaskAlignedAssigner ================================================ FILE: asone/detectors/yolov6/yolov6/assigners/anchor_generator.py ================================================ import torch def generate_anchors(feats, fpn_strides, grid_cell_size=5.0, grid_cell_offset=0.5, device='cpu', is_eval=False): '''Generate anchors from features.''' anchors = [] anchor_points = [] stride_tensor = [] num_anchors_list = [] assert feats is not None if is_eval: for i, stride in enumerate(fpn_strides): _, _, h, w = feats[i].shape shift_x = torch.arange(end=w, device=device) + grid_cell_offset shift_y = torch.arange(end=h, device=device) + grid_cell_offset shift_y, shift_x = torch.meshgrid(shift_y, shift_x) anchor_point = torch.stack( [shift_x, shift_y], axis=-1).to(torch.float) anchor_points.append(anchor_point.reshape([-1, 2])) stride_tensor.append( torch.full( (h * w, 1), stride, dtype=torch.float, device=device)) anchor_points = torch.cat(anchor_points) stride_tensor = torch.cat(stride_tensor) return anchor_points, stride_tensor else: for i, stride in enumerate(fpn_strides): _, _, h, w = feats[i].shape cell_half_size = grid_cell_size * stride * 0.5 shift_x = (torch.arange(end=w, device=device) + grid_cell_offset) * stride shift_y = (torch.arange(end=h, device=device) + grid_cell_offset) * stride shift_y, shift_x = torch.meshgrid(shift_y, shift_x) anchor = torch.stack( [ shift_x - cell_half_size, shift_y - cell_half_size, shift_x + cell_half_size, shift_y + cell_half_size ], axis=-1).clone().to(feats[0].dtype) anchor_point = torch.stack( [shift_x, shift_y], axis=-1).clone().to(feats[0].dtype) anchors.append(anchor.reshape([-1, 4])) anchor_points.append(anchor_point.reshape([-1, 2])) num_anchors_list.append(len(anchors[-1])) stride_tensor.append( torch.full( [num_anchors_list[-1], 1], stride, dtype=feats[0].dtype)) anchors = torch.cat(anchors) anchor_points = torch.cat(anchor_points).to(device) stride_tensor = torch.cat(stride_tensor).to(device) return anchors, anchor_points, num_anchors_list, stride_tensor ================================================ FILE: asone/detectors/yolov6/yolov6/assigners/assigner_utils.py ================================================ import torch import torch.nn.functional as F def dist_calculator(gt_bboxes, anchor_bboxes): """compute center distance between all bbox and gt Args: gt_bboxes (Tensor): shape(bs*n_max_boxes, 4) anchor_bboxes (Tensor): shape(num_total_anchors, 4) Return: distances (Tensor): shape(bs*n_max_boxes, num_total_anchors) ac_points (Tensor): shape(num_total_anchors, 2) """ gt_cx = (gt_bboxes[:, 0] + gt_bboxes[:, 2]) / 2.0 gt_cy = (gt_bboxes[:, 1] + gt_bboxes[:, 3]) / 2.0 gt_points = torch.stack([gt_cx, gt_cy], dim=1) ac_cx = (anchor_bboxes[:, 0] + anchor_bboxes[:, 2]) / 2.0 ac_cy = (anchor_bboxes[:, 1] + anchor_bboxes[:, 3]) / 2.0 ac_points = torch.stack([ac_cx, ac_cy], dim=1) distances = (gt_points[:, None, :] - ac_points[None, :, :]).pow(2).sum(-1).sqrt() return distances, ac_points def select_candidates_in_gts(xy_centers, gt_bboxes, eps=1e-9): """select the positive anchors's center in gt Args: xy_centers (Tensor): shape(bs*n_max_boxes, num_total_anchors, 4) gt_bboxes (Tensor): shape(bs, n_max_boxes, 4) Return: (Tensor): shape(bs, n_max_boxes, num_total_anchors) """ n_anchors = xy_centers.size(0) bs, n_max_boxes, _ = gt_bboxes.size() _gt_bboxes = gt_bboxes.reshape([-1, 4]) xy_centers = xy_centers.unsqueeze(0).repeat(bs * n_max_boxes, 1, 1) gt_bboxes_lt = _gt_bboxes[:, 0:2].unsqueeze(1).repeat(1, n_anchors, 1) gt_bboxes_rb = _gt_bboxes[:, 2:4].unsqueeze(1).repeat(1, n_anchors, 1) b_lt = xy_centers - gt_bboxes_lt b_rb = gt_bboxes_rb - xy_centers bbox_deltas = torch.cat([b_lt, b_rb], dim=-1) bbox_deltas = bbox_deltas.reshape([bs, n_max_boxes, n_anchors, -1]) return (bbox_deltas.min(axis=-1)[0] > eps).to(gt_bboxes.dtype) def select_highest_overlaps(mask_pos, overlaps, n_max_boxes): """if an anchor box is assigned to multiple gts, the one with the highest iou will be selected. Args: mask_pos (Tensor): shape(bs, n_max_boxes, num_total_anchors) overlaps (Tensor): shape(bs, n_max_boxes, num_total_anchors) Return: target_gt_idx (Tensor): shape(bs, num_total_anchors) fg_mask (Tensor): shape(bs, num_total_anchors) mask_pos (Tensor): shape(bs, n_max_boxes, num_total_anchors) """ fg_mask = mask_pos.sum(axis=-2) if fg_mask.max() > 1: mask_multi_gts = (fg_mask.unsqueeze(1) > 1).repeat([1, n_max_boxes, 1]) max_overlaps_idx = overlaps.argmax(axis=1) is_max_overlaps = F.one_hot(max_overlaps_idx, n_max_boxes) is_max_overlaps = is_max_overlaps.permute(0, 2, 1).to(overlaps.dtype) mask_pos = torch.where(mask_multi_gts, is_max_overlaps, mask_pos) fg_mask = mask_pos.sum(axis=-2) target_gt_idx = mask_pos.argmax(axis=-2) return target_gt_idx, fg_mask , mask_pos def iou_calculator(box1, box2, eps=1e-9): """Calculate iou for batch Args: box1 (Tensor): shape(bs, n_max_boxes, 1, 4) box2 (Tensor): shape(bs, 1, num_total_anchors, 4) Return: (Tensor): shape(bs, n_max_boxes, num_total_anchors) """ box1 = box1.unsqueeze(2) # [N, M1, 4] -> [N, M1, 1, 4] box2 = box2.unsqueeze(1) # [N, M2, 4] -> [N, 1, M2, 4] px1y1, px2y2 = box1[:, :, :, 0:2], box1[:, :, :, 2:4] gx1y1, gx2y2 = box2[:, :, :, 0:2], box2[:, :, :, 2:4] x1y1 = torch.maximum(px1y1, gx1y1) x2y2 = torch.minimum(px2y2, gx2y2) overlap = (x2y2 - x1y1).clip(0).prod(-1) area1 = (px2y2 - px1y1).clip(0).prod(-1) area2 = (gx2y2 - gx1y1).clip(0).prod(-1) union = area1 + area2 - overlap + eps return overlap / union ================================================ FILE: asone/detectors/yolov6/yolov6/assigners/atss_assigner.py ================================================ import torch import torch.nn as nn import torch.nn.functional as F from asone.detectors.yolov6.yolov6.assigners.iou2d_calculator import iou2d_calculator from asone.detectors.yolov6.yolov6.assigners.assigner_utils import dist_calculator, select_candidates_in_gts, select_highest_overlaps, iou_calculator class ATSSAssigner(nn.Module): '''Adaptive Training Sample Selection Assigner''' def __init__(self, topk=9, num_classes=80): super(ATSSAssigner, self).__init__() self.topk = topk self.num_classes = num_classes self.bg_idx = num_classes @torch.no_grad() def forward(self, anc_bboxes, n_level_bboxes, gt_labels, gt_bboxes, mask_gt, pd_bboxes): r"""This code is based on https://github.com/fcjian/TOOD/blob/master/mmdet/core/bbox/assigners/atss_assigner.py Args: anc_bboxes (Tensor): shape(num_total_anchors, 4) n_level_bboxes (List):len(3) gt_labels (Tensor): shape(bs, n_max_boxes, 1) gt_bboxes (Tensor): shape(bs, n_max_boxes, 4) mask_gt (Tensor): shape(bs, n_max_boxes, 1) pd_bboxes (Tensor): shape(bs, n_max_boxes, 4) Returns: target_labels (Tensor): shape(bs, num_total_anchors) target_bboxes (Tensor): shape(bs, num_total_anchors, 4) target_scores (Tensor): shape(bs, num_total_anchors, num_classes) fg_mask (Tensor): shape(bs, num_total_anchors) """ self.n_anchors = anc_bboxes.size(0) self.bs = gt_bboxes.size(0) self.n_max_boxes = gt_bboxes.size(1) if self.n_max_boxes == 0: device = gt_bboxes.device return torch.full( [self.bs, self.n_anchors], self.bg_idx).to(device), \ torch.zeros([self.bs, self.n_anchors, 4]).to(device), \ torch.zeros([self.bs, self.n_anchors, self.num_classes]).to(device), \ torch.zeros([self.bs, self.n_anchors]).to(device) overlaps = iou2d_calculator(gt_bboxes.reshape([-1, 4]), anc_bboxes) overlaps = overlaps.reshape([self.bs, -1, self.n_anchors]) distances, ac_points = dist_calculator(gt_bboxes.reshape([-1, 4]), anc_bboxes) distances = distances.reshape([self.bs, -1, self.n_anchors]) is_in_candidate, candidate_idxs = self.select_topk_candidates( distances, n_level_bboxes, mask_gt) overlaps_thr_per_gt, iou_candidates = self.thres_calculator( is_in_candidate, candidate_idxs, overlaps) # select candidates iou >= threshold as positive is_pos = torch.where( iou_candidates > overlaps_thr_per_gt.repeat([1, 1, self.n_anchors]), is_in_candidate, torch.zeros_like(is_in_candidate)) is_in_gts = select_candidates_in_gts(ac_points, gt_bboxes) mask_pos = is_pos * is_in_gts * mask_gt target_gt_idx, fg_mask, mask_pos = select_highest_overlaps( mask_pos, overlaps, self.n_max_boxes) # assigned target target_labels, target_bboxes, target_scores = self.get_targets( gt_labels, gt_bboxes, target_gt_idx, fg_mask) # soft label with iou if pd_bboxes is not None: ious = iou_calculator(gt_bboxes, pd_bboxes) * mask_pos ious = ious.max(axis=-2)[0].unsqueeze(-1) target_scores *= ious return target_labels.long(), target_bboxes, target_scores, fg_mask.bool() def select_topk_candidates(self, distances, n_level_bboxes, mask_gt): mask_gt = mask_gt.repeat(1, 1, self.topk).bool() level_distances = torch.split(distances, n_level_bboxes, dim=-1) is_in_candidate_list = [] candidate_idxs = [] start_idx = 0 for per_level_distances, per_level_boxes in zip(level_distances, n_level_bboxes): end_idx = start_idx + per_level_boxes selected_k = min(self.topk, per_level_boxes) _, per_level_topk_idxs = per_level_distances.topk(selected_k, dim=-1, largest=False) candidate_idxs.append(per_level_topk_idxs + start_idx) per_level_topk_idxs = torch.where(mask_gt, per_level_topk_idxs, torch.zeros_like(per_level_topk_idxs)) is_in_candidate = F.one_hot(per_level_topk_idxs, per_level_boxes).sum(dim=-2) is_in_candidate = torch.where(is_in_candidate > 1, torch.zeros_like(is_in_candidate), is_in_candidate) is_in_candidate_list.append(is_in_candidate.to(distances.dtype)) start_idx = end_idx is_in_candidate_list = torch.cat(is_in_candidate_list, dim=-1) candidate_idxs = torch.cat(candidate_idxs, dim=-1) return is_in_candidate_list, candidate_idxs def thres_calculator(self, is_in_candidate, candidate_idxs, overlaps): n_bs_max_boxes = self.bs * self.n_max_boxes _candidate_overlaps = torch.where(is_in_candidate > 0, overlaps, torch.zeros_like(overlaps)) candidate_idxs = candidate_idxs.reshape([n_bs_max_boxes, -1]) assist_idxs = self.n_anchors * torch.arange(n_bs_max_boxes, device=candidate_idxs.device) assist_idxs = assist_idxs[:,None] faltten_idxs = candidate_idxs + assist_idxs candidate_overlaps = _candidate_overlaps.reshape(-1)[faltten_idxs] candidate_overlaps = candidate_overlaps.reshape([self.bs, self.n_max_boxes, -1]) overlaps_mean_per_gt = candidate_overlaps.mean(axis=-1, keepdim=True) overlaps_std_per_gt = candidate_overlaps.std(axis=-1, keepdim=True) overlaps_thr_per_gt = overlaps_mean_per_gt + overlaps_std_per_gt return overlaps_thr_per_gt, _candidate_overlaps def get_targets(self, gt_labels, gt_bboxes, target_gt_idx, fg_mask): # assigned target labels batch_idx = torch.arange(self.bs, dtype=gt_labels.dtype, device=gt_labels.device) batch_idx = batch_idx[...,None] target_gt_idx = (target_gt_idx + batch_idx * self.n_max_boxes).long() target_labels = gt_labels.flatten()[target_gt_idx.flatten()] target_labels = target_labels.reshape([self.bs, self.n_anchors]) target_labels = torch.where(fg_mask > 0, target_labels, torch.full_like(target_labels, self.bg_idx)) # assigned target boxes target_bboxes = gt_bboxes.reshape([-1, 4])[target_gt_idx.flatten()] target_bboxes = target_bboxes.reshape([self.bs, self.n_anchors, 4]) # assigned target scores target_scores = F.one_hot(target_labels.long(), self.num_classes + 1).float() target_scores = target_scores[:, :, :self.num_classes] return target_labels, target_bboxes, target_scores ================================================ FILE: asone/detectors/yolov6/yolov6/assigners/iou2d_calculator.py ================================================ #This code is based on #https://github.com/fcjian/TOOD/blob/master/mmdet/core/bbox/iou_calculators/iou2d_calculator.py import torch def cast_tensor_type(x, scale=1., dtype=None): if dtype == 'fp16': # scale is for preventing overflows x = (x / scale).half() return x def fp16_clamp(x, min=None, max=None): if not x.is_cuda and x.dtype == torch.float16: # clamp for cpu float16, tensor fp16 has no clamp implementation return x.float().clamp(min, max).half() return x.clamp(min, max) def iou2d_calculator(bboxes1, bboxes2, mode='iou', is_aligned=False, scale=1., dtype=None): """2D Overlaps (e.g. IoUs, GIoUs) Calculator.""" """Calculate IoU between 2D bboxes. Args: bboxes1 (Tensor): bboxes have shape (m, 4) in format, or shape (m, 5) in format. bboxes2 (Tensor): bboxes have shape (m, 4) in format, shape (m, 5) in format, or be empty. If ``is_aligned `` is ``True``, then m and n must be equal. mode (str): "iou" (intersection over union), "iof" (intersection over foreground), or "giou" (generalized intersection over union). is_aligned (bool, optional): If True, then m and n must be equal. Default False. Returns: Tensor: shape (m, n) if ``is_aligned `` is False else shape (m,) """ assert bboxes1.size(-1) in [0, 4, 5] assert bboxes2.size(-1) in [0, 4, 5] if bboxes2.size(-1) == 5: bboxes2 = bboxes2[..., :4] if bboxes1.size(-1) == 5: bboxes1 = bboxes1[..., :4] if dtype == 'fp16': # change tensor type to save cpu and cuda memory and keep speed bboxes1 = cast_tensor_type(bboxes1, scale, dtype) bboxes2 = cast_tensor_type(bboxes2, scale, dtype) overlaps = bbox_overlaps(bboxes1, bboxes2, mode, is_aligned) if not overlaps.is_cuda and overlaps.dtype == torch.float16: # resume cpu float32 overlaps = overlaps.float() return overlaps return bbox_overlaps(bboxes1, bboxes2, mode, is_aligned) def bbox_overlaps(bboxes1, bboxes2, mode='iou', is_aligned=False, eps=1e-6): """Calculate overlap between two set of bboxes. FP16 Contributed by https://github.com/open-mmlab/mmdetection/pull/4889 Note: Assume bboxes1 is M x 4, bboxes2 is N x 4, when mode is 'iou', there are some new generated variable when calculating IOU using bbox_overlaps function: 1) is_aligned is False area1: M x 1 area2: N x 1 lt: M x N x 2 rb: M x N x 2 wh: M x N x 2 overlap: M x N x 1 union: M x N x 1 ious: M x N x 1 Total memory: S = (9 x N x M + N + M) * 4 Byte, When using FP16, we can reduce: R = (9 x N x M + N + M) * 4 / 2 Byte R large than (N + M) * 4 * 2 is always true when N and M >= 1. Obviously, N + M <= N * M < 3 * N * M, when N >=2 and M >=2, N + 1 < 3 * N, when N or M is 1. Given M = 40 (ground truth), N = 400000 (three anchor boxes in per grid, FPN, R-CNNs), R = 275 MB (one times) A special case (dense detection), M = 512 (ground truth), R = 3516 MB = 3.43 GB When the batch size is B, reduce: B x R Therefore, CUDA memory runs out frequently. Experiments on GeForce RTX 2080Ti (11019 MiB): | dtype | M | N | Use | Real | Ideal | |:----:|:----:|:----:|:----:|:----:|:----:| | FP32 | 512 | 400000 | 8020 MiB | -- | -- | | FP16 | 512 | 400000 | 4504 MiB | 3516 MiB | 3516 MiB | | FP32 | 40 | 400000 | 1540 MiB | -- | -- | | FP16 | 40 | 400000 | 1264 MiB | 276MiB | 275 MiB | 2) is_aligned is True area1: N x 1 area2: N x 1 lt: N x 2 rb: N x 2 wh: N x 2 overlap: N x 1 union: N x 1 ious: N x 1 Total memory: S = 11 x N * 4 Byte When using FP16, we can reduce: R = 11 x N * 4 / 2 Byte So do the 'giou' (large than 'iou'). Time-wise, FP16 is generally faster than FP32. When gpu_assign_thr is not -1, it takes more time on cpu but not reduce memory. There, we can reduce half the memory and keep the speed. If ``is_aligned`` is ``False``, then calculate the overlaps between each bbox of bboxes1 and bboxes2, otherwise the overlaps between each aligned pair of bboxes1 and bboxes2. Args: bboxes1 (Tensor): shape (B, m, 4) in format or empty. bboxes2 (Tensor): shape (B, n, 4) in format or empty. B indicates the batch dim, in shape (B1, B2, ..., Bn). If ``is_aligned`` is ``True``, then m and n must be equal. mode (str): "iou" (intersection over union), "iof" (intersection over foreground) or "giou" (generalized intersection over union). Default "iou". is_aligned (bool, optional): If True, then m and n must be equal. Default False. eps (float, optional): A value added to the denominator for numerical stability. Default 1e-6. Returns: Tensor: shape (m, n) if ``is_aligned`` is False else shape (m,) Example: >>> bboxes1 = torch.FloatTensor([ >>> [0, 0, 10, 10], >>> [10, 10, 20, 20], >>> [32, 32, 38, 42], >>> ]) >>> bboxes2 = torch.FloatTensor([ >>> [0, 0, 10, 20], >>> [0, 10, 10, 19], >>> [10, 10, 20, 20], >>> ]) >>> overlaps = bbox_overlaps(bboxes1, bboxes2) >>> assert overlaps.shape == (3, 3) >>> overlaps = bbox_overlaps(bboxes1, bboxes2, is_aligned=True) >>> assert overlaps.shape == (3, ) Example: >>> empty = torch.empty(0, 4) >>> nonempty = torch.FloatTensor([[0, 0, 10, 9]]) >>> assert tuple(bbox_overlaps(empty, nonempty).shape) == (0, 1) >>> assert tuple(bbox_overlaps(nonempty, empty).shape) == (1, 0) >>> assert tuple(bbox_overlaps(empty, empty).shape) == (0, 0) """ assert mode in ['iou', 'iof', 'giou'], f'Unsupported mode {mode}' # Either the boxes are empty or the length of boxes' last dimension is 4 assert (bboxes1.size(-1) == 4 or bboxes1.size(0) == 0) assert (bboxes2.size(-1) == 4 or bboxes2.size(0) == 0) # Batch dim must be the same # Batch dim: (B1, B2, ... Bn) assert bboxes1.shape[:-2] == bboxes2.shape[:-2] batch_shape = bboxes1.shape[:-2] rows = bboxes1.size(-2) cols = bboxes2.size(-2) if is_aligned: assert rows == cols if rows * cols == 0: if is_aligned: return bboxes1.new(batch_shape + (rows, )) else: return bboxes1.new(batch_shape + (rows, cols)) area1 = (bboxes1[..., 2] - bboxes1[..., 0]) * ( bboxes1[..., 3] - bboxes1[..., 1]) area2 = (bboxes2[..., 2] - bboxes2[..., 0]) * ( bboxes2[..., 3] - bboxes2[..., 1]) if is_aligned: lt = torch.max(bboxes1[..., :2], bboxes2[..., :2]) # [B, rows, 2] rb = torch.min(bboxes1[..., 2:], bboxes2[..., 2:]) # [B, rows, 2] wh = fp16_clamp(rb - lt, min=0) overlap = wh[..., 0] * wh[..., 1] if mode in ['iou', 'giou']: union = area1 + area2 - overlap else: union = area1 if mode == 'giou': enclosed_lt = torch.min(bboxes1[..., :2], bboxes2[..., :2]) enclosed_rb = torch.max(bboxes1[..., 2:], bboxes2[..., 2:]) else: lt = torch.max(bboxes1[..., :, None, :2], bboxes2[..., None, :, :2]) # [B, rows, cols, 2] rb = torch.min(bboxes1[..., :, None, 2:], bboxes2[..., None, :, 2:]) # [B, rows, cols, 2] wh = fp16_clamp(rb - lt, min=0) overlap = wh[..., 0] * wh[..., 1] if mode in ['iou', 'giou']: union = area1[..., None] + area2[..., None, :] - overlap else: union = area1[..., None] if mode == 'giou': enclosed_lt = torch.min(bboxes1[..., :, None, :2], bboxes2[..., None, :, :2]) enclosed_rb = torch.max(bboxes1[..., :, None, 2:], bboxes2[..., None, :, 2:]) eps = union.new_tensor([eps]) union = torch.max(union, eps) ious = overlap / union if mode in ['iou', 'iof']: return ious # calculate gious enclose_wh = fp16_clamp(enclosed_rb - enclosed_lt, min=0) enclose_area = enclose_wh[..., 0] * enclose_wh[..., 1] enclose_area = torch.max(enclose_area, eps) gious = ious - (enclose_area - union) / enclose_area return gious ================================================ FILE: asone/detectors/yolov6/yolov6/assigners/tal_assigner.py ================================================ import torch import torch.nn as nn import torch.nn.functional as F from asone.detectors.yolov6.yolov6.assigners.assigner_utils import select_candidates_in_gts, select_highest_overlaps, iou_calculator class TaskAlignedAssigner(nn.Module): def __init__(self, topk=13, num_classes=80, alpha=1.0, beta=6.0, eps=1e-9): super(TaskAlignedAssigner, self).__init__() self.topk = topk self.num_classes = num_classes self.bg_idx = num_classes self.alpha = alpha self.beta = beta self.eps = eps @torch.no_grad() def forward(self, pd_scores, pd_bboxes, anc_points, gt_labels, gt_bboxes, mask_gt): """This code referenced to https://github.com/Nioolek/PPYOLOE_pytorch/blob/master/ppyoloe/assigner/tal_assigner.py Args: pd_scores (Tensor): shape(bs, num_total_anchors, num_classes) pd_bboxes (Tensor): shape(bs, num_total_anchors, 4) anc_points (Tensor): shape(num_total_anchors, 2) gt_labels (Tensor): shape(bs, n_max_boxes, 1) gt_bboxes (Tensor): shape(bs, n_max_boxes, 4) mask_gt (Tensor): shape(bs, n_max_boxes, 1) Returns: target_labels (Tensor): shape(bs, num_total_anchors) target_bboxes (Tensor): shape(bs, num_total_anchors, 4) target_scores (Tensor): shape(bs, num_total_anchors, num_classes) fg_mask (Tensor): shape(bs, num_total_anchors) """ self.bs = pd_scores.size(0) self.n_max_boxes = gt_bboxes.size(1) if self.n_max_boxes == 0: device = gt_bboxes.device return torch.full_like(pd_scores[..., 0], self.bg_idx).to(device), \ torch.zeros_like(pd_bboxes).to(device), \ torch.zeros_like(pd_scores).to(device), \ torch.zeros_like(pd_scores[..., 0]).to(device) mask_pos, align_metric, overlaps = self.get_pos_mask( pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt) target_gt_idx, fg_mask, mask_pos = select_highest_overlaps( mask_pos, overlaps, self.n_max_boxes) # assigned target target_labels, target_bboxes, target_scores = self.get_targets( gt_labels, gt_bboxes, target_gt_idx, fg_mask) # normalize align_metric *= mask_pos pos_align_metrics = align_metric.max(axis=-1, keepdim=True)[0] pos_overlaps = (overlaps * mask_pos).max(axis=-1, keepdim=True)[0] norm_align_metric = (align_metric * pos_overlaps / (pos_align_metrics + self.eps)).max(-2)[0].unsqueeze(-1) target_scores = target_scores * norm_align_metric return target_labels, target_bboxes, target_scores, fg_mask.bool() def get_pos_mask(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt): # get anchor_align metric align_metric, overlaps = self.get_box_metrics(pd_scores, pd_bboxes, gt_labels, gt_bboxes) # get in_gts mask mask_in_gts = select_candidates_in_gts(anc_points, gt_bboxes) # get topk_metric mask mask_topk = self.select_topk_candidates( align_metric * mask_in_gts, topk_mask=mask_gt.repeat([1, 1, self.topk]).bool()) # merge all mask to a final mask mask_pos = mask_topk * mask_in_gts * mask_gt return mask_pos, align_metric, overlaps def get_box_metrics(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes): pd_scores = pd_scores.permute(0, 2, 1) gt_labels = gt_labels.to(torch.long) ind = torch.zeros([2, self.bs, self.n_max_boxes], dtype=torch.long) ind[0] = torch.arange(end=self.bs).view(-1, 1).repeat(1, self.n_max_boxes) ind[1] = gt_labels.squeeze(-1) bbox_scores = pd_scores[ind[0], ind[1]] overlaps = iou_calculator(gt_bboxes, pd_bboxes) align_metric = bbox_scores.pow(self.alpha) * overlaps.pow(self.beta) return align_metric, overlaps def select_topk_candidates(self, metrics, largest=True, topk_mask=None): num_anchors = metrics.shape[-1] topk_metrics, topk_idxs = torch.topk( metrics, self.topk, axis=-1, largest=largest) if topk_mask is None: topk_mask = (topk_metrics.max(axis=-1, keepdim=True) > self.eps).tile( [1, 1, self.topk]) topk_idxs = torch.where(topk_mask, topk_idxs, torch.zeros_like(topk_idxs)) is_in_topk = F.one_hot(topk_idxs, num_anchors).sum(axis=-2) is_in_topk = torch.where(is_in_topk > 1, torch.zeros_like(is_in_topk), is_in_topk) return is_in_topk.to(metrics.dtype) def get_targets(self, gt_labels, gt_bboxes, target_gt_idx, fg_mask): # assigned target labels batch_ind = torch.arange(end=self.bs, dtype=torch.int64, device=gt_labels.device)[...,None] target_gt_idx = target_gt_idx + batch_ind * self.n_max_boxes target_labels = gt_labels.long().flatten()[target_gt_idx] # assigned target boxes target_bboxes = gt_bboxes.reshape([-1, 4])[target_gt_idx] # assigned target scores target_labels[target_labels<0] = 0 target_scores = F.one_hot(target_labels, self.num_classes) fg_scores_mask = fg_mask[:, :, None].repeat(1, 1, self.num_classes) target_scores = torch.where(fg_scores_mask > 0, target_scores, torch.full_like(target_scores, 0)) return target_labels, target_bboxes, target_scores ================================================ FILE: asone/detectors/yolov6/yolov6/layers/__init__.py ================================================ ================================================ FILE: asone/detectors/yolov6/yolov6/layers/common.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- import warnings from pathlib import Path import numpy as np import torch import torch.nn as nn from torch.nn.parameter import Parameter import torch.nn.init as init class SiLU(nn.Module): '''Activation of SiLU''' @staticmethod def forward(x): return x * torch.sigmoid(x) class Conv(nn.Module): '''Normal Conv with SiLU activation''' def __init__(self, in_channels, out_channels, kernel_size, stride, groups=1, bias=False): super().__init__() padding = kernel_size // 2 self.conv = nn.Conv2d( in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, groups=groups, bias=bias, ) self.bn = nn.BatchNorm2d(out_channels) self.act = nn.SiLU() def forward(self, x): return self.act(self.bn(self.conv(x))) def forward_fuse(self, x): return self.act(self.conv(x)) class SimConv(nn.Module): '''Normal Conv with ReLU activation''' def __init__(self, in_channels, out_channels, kernel_size, stride, groups=1, bias=False): super().__init__() padding = kernel_size // 2 self.conv = nn.Conv2d( in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, groups=groups, bias=bias, ) self.bn = nn.BatchNorm2d(out_channels) self.act = nn.ReLU() def forward(self, x): return self.act(self.bn(self.conv(x))) def forward_fuse(self, x): return self.act(self.conv(x)) class ConvWrapper(nn.Module): '''Wrapper for normal Conv with SiLU activation''' def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, groups=1, bias=True): super().__init__() self.block = Conv(in_channels, out_channels, kernel_size, stride, groups, bias) def forward(self, x): return self.block(x) class SimConvWrapper(nn.Module): '''Wrapper for normal Conv with ReLU activation''' def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, groups=1, bias=True): super().__init__() self.block = SimConv(in_channels, out_channels, kernel_size, stride, groups, bias) def forward(self, x): return self.block(x) class SimSPPF(nn.Module): '''Simplified SPPF with ReLU activation''' def __init__(self, in_channels, out_channels, kernel_size=5): super().__init__() c_ = in_channels // 2 # hidden channels self.cv1 = SimConv(in_channels, c_, 1, 1) self.cv2 = SimConv(c_ * 4, out_channels, 1, 1) self.m = nn.MaxPool2d(kernel_size=kernel_size, stride=1, padding=kernel_size // 2) def forward(self, x): x = self.cv1(x) with warnings.catch_warnings(): warnings.simplefilter('ignore') y1 = self.m(x) y2 = self.m(y1) return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1)) class SPPF(nn.Module): '''Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher''' def __init__(self, in_channels, out_channels, kernel_size=5): # equivalent to SPP(k=(5, 9, 13)) super().__init__() c_ = in_channels // 2 # hidden channels self.cv1 = Conv(in_channels, c_, 1, 1) self.cv2 = Conv(c_ * 4, out_channels, 1, 1) self.m = nn.MaxPool2d(kernel_size=kernel_size, stride=1, padding=kernel_size // 2) def forward(self, x): x = self.cv1(x) with warnings.catch_warnings(): warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning y1 = self.m(x) y2 = self.m(y1) return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1)) class Transpose(nn.Module): '''Normal Transpose, default for upsampling''' def __init__(self, in_channels, out_channels, kernel_size=2, stride=2): super().__init__() self.upsample_transpose = torch.nn.ConvTranspose2d( in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, bias=True ) def forward(self, x): return self.upsample_transpose(x) class Concat(nn.Module): def __init__(self, dimension=1): super().__init__() self.d = dimension def forward(self, x): return torch.cat(x, self.d) def conv_bn(in_channels, out_channels, kernel_size, stride, padding, groups=1): '''Basic cell for rep-style block, including conv and bn''' result = nn.Sequential() result.add_module('conv', nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, groups=groups, bias=False)) result.add_module('bn', nn.BatchNorm2d(num_features=out_channels)) return result class RepVGGBlock(nn.Module): '''RepVGGBlock is a basic rep-style block, including training and deploy status This code is based on https://github.com/DingXiaoH/RepVGG/blob/main/repvgg.py ''' def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, padding_mode='zeros', deploy=False, use_se=False): super(RepVGGBlock, self).__init__() """ Initialization of the class. Args: in_channels (int): Number of channels in the input image out_channels (int): Number of channels produced by the convolution kernel_size (int or tuple): Size of the convolving kernel stride (int or tuple, optional): Stride of the convolution. Default: 1 padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 1 dilation (int or tuple, optional): Spacing between kernel elements. Default: 1 groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1 padding_mode (string, optional): Default: 'zeros' deploy: Whether to be deploy status or training status. Default: False use_se: Whether to use se. Default: False """ self.deploy = deploy self.groups = groups self.in_channels = in_channels self.out_channels = out_channels assert kernel_size == 3 assert padding == 1 padding_11 = padding - kernel_size // 2 self.nonlinearity = nn.ReLU() if use_se: raise NotImplementedError("se block not supported yet") else: self.se = nn.Identity() if deploy: self.rbr_reparam = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=True, padding_mode=padding_mode) else: self.rbr_identity = nn.BatchNorm2d(num_features=in_channels) if out_channels == in_channels and stride == 1 else None self.rbr_dense = conv_bn(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, groups=groups) self.rbr_1x1 = conv_bn(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=stride, padding=padding_11, groups=groups) def forward(self, inputs): '''Forward process''' if hasattr(self, 'rbr_reparam'): return self.nonlinearity(self.se(self.rbr_reparam(inputs))) if self.rbr_identity is None: id_out = 0 else: id_out = self.rbr_identity(inputs) return self.nonlinearity(self.se(self.rbr_dense(inputs) + self.rbr_1x1(inputs) + id_out)) def get_equivalent_kernel_bias(self): kernel3x3, bias3x3 = self._fuse_bn_tensor(self.rbr_dense) kernel1x1, bias1x1 = self._fuse_bn_tensor(self.rbr_1x1) kernelid, biasid = self._fuse_bn_tensor(self.rbr_identity) return kernel3x3 + self._pad_1x1_to_3x3_tensor(kernel1x1) + kernelid, bias3x3 + bias1x1 + biasid def _pad_1x1_to_3x3_tensor(self, kernel1x1): if kernel1x1 is None: return 0 else: return torch.nn.functional.pad(kernel1x1, [1, 1, 1, 1]) def _fuse_bn_tensor(self, branch): if branch is None: return 0, 0 if isinstance(branch, nn.Sequential): kernel = branch.conv.weight running_mean = branch.bn.running_mean running_var = branch.bn.running_var gamma = branch.bn.weight beta = branch.bn.bias eps = branch.bn.eps else: assert isinstance(branch, nn.BatchNorm2d) if not hasattr(self, 'id_tensor'): input_dim = self.in_channels // self.groups kernel_value = np.zeros((self.in_channels, input_dim, 3, 3), dtype=np.float32) for i in range(self.in_channels): kernel_value[i, i % input_dim, 1, 1] = 1 self.id_tensor = torch.from_numpy(kernel_value).to(branch.weight.device) kernel = self.id_tensor running_mean = branch.running_mean running_var = branch.running_var gamma = branch.weight beta = branch.bias eps = branch.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta - running_mean * gamma / std def switch_to_deploy(self): if hasattr(self, 'rbr_reparam'): return kernel, bias = self.get_equivalent_kernel_bias() self.rbr_reparam = nn.Conv2d(in_channels=self.rbr_dense.conv.in_channels, out_channels=self.rbr_dense.conv.out_channels, kernel_size=self.rbr_dense.conv.kernel_size, stride=self.rbr_dense.conv.stride, padding=self.rbr_dense.conv.padding, dilation=self.rbr_dense.conv.dilation, groups=self.rbr_dense.conv.groups, bias=True) self.rbr_reparam.weight.data = kernel self.rbr_reparam.bias.data = bias for para in self.parameters(): para.detach_() self.__delattr__('rbr_dense') self.__delattr__('rbr_1x1') if hasattr(self, 'rbr_identity'): self.__delattr__('rbr_identity') if hasattr(self, 'id_tensor'): self.__delattr__('id_tensor') self.deploy = True class RealVGGBlock(nn.Module): def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, padding_mode='zeros', use_se=False, ): super(RealVGGBlock, self).__init__() self.relu = nn.ReLU() self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=False) self.bn = nn.BatchNorm2d(out_channels) if use_se: raise NotImplementedError("se block not supported yet") else: self.se = nn.Identity() def forward(self, inputs): out = self.relu(self.se(self.bn(self.conv(inputs)))) return out class ScaleLayer(torch.nn.Module): def __init__(self, num_features, use_bias=True, scale_init=1.0): super(ScaleLayer, self).__init__() self.weight = Parameter(torch.Tensor(num_features)) init.constant_(self.weight, scale_init) self.num_features = num_features if use_bias: self.bias = Parameter(torch.Tensor(num_features)) init.zeros_(self.bias) else: self.bias = None def forward(self, inputs): if self.bias is None: return inputs * self.weight.view(1, self.num_features, 1, 1) else: return inputs * self.weight.view(1, self.num_features, 1, 1) + self.bias.view(1, self.num_features, 1, 1) # A CSLA block is a LinearAddBlock with is_csla=True class LinearAddBlock(nn.Module): def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, groups=1, padding_mode='zeros', use_se=False, is_csla=False, conv_scale_init=1.0): super(LinearAddBlock, self).__init__() self.in_channels = in_channels self.relu = nn.ReLU() self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=False) self.scale_conv = ScaleLayer(num_features=out_channels, use_bias=False, scale_init=conv_scale_init) self.conv_1x1 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=stride, padding=0, bias=False) self.scale_1x1 = ScaleLayer(num_features=out_channels, use_bias=False, scale_init=conv_scale_init) if in_channels == out_channels and stride == 1: self.scale_identity = ScaleLayer(num_features=out_channels, use_bias=False, scale_init=1.0) self.bn = nn.BatchNorm2d(out_channels) if is_csla: # Make them constant self.scale_1x1.requires_grad_(False) self.scale_conv.requires_grad_(False) if use_se: raise NotImplementedError("se block not supported yet") else: self.se = nn.Identity() def forward(self, inputs): out = self.scale_conv(self.conv(inputs)) + self.scale_1x1(self.conv_1x1(inputs)) if hasattr(self, 'scale_identity'): out += self.scale_identity(inputs) out = self.relu(self.se(self.bn(out))) return out class DetectBackend(nn.Module): def __init__(self, weights='yolov6s.pt', device=None, dnn=True): super().__init__() assert isinstance(weights, str) and Path(weights).suffix == '.pt', f'{Path(weights).suffix} format is not supported.' from asone.detectors.yolov6.yolov6.utils.checkpoint import load_checkpoint model = load_checkpoint(weights, map_location=device) stride = int(model.stride.max()) self.__dict__.update(locals()) # assign all variables to self def forward(self, im, val=False): y, _ = self.model(im) if isinstance(y, np.ndarray): y = torch.tensor(y, device=self.device) return y class RepBlock(nn.Module): ''' RepBlock is a stage block with rep-style basic block ''' def __init__(self, in_channels, out_channels, n=1, block=RepVGGBlock, basic_block=RepVGGBlock): super().__init__() self.conv1 = block(in_channels, out_channels) self.block = nn.Sequential(*(block(out_channels, out_channels) for _ in range(n - 1))) if n > 1 else None if block == BottleRep: self.conv1 = BottleRep(in_channels, out_channels, basic_block=basic_block, weight=True) n = n // 2 self.block = nn.Sequential(*(BottleRep(out_channels, out_channels, basic_block=basic_block, weight=True) for _ in range(n - 1))) if n > 1 else None def forward(self, x): x = self.conv1(x) if self.block is not None: x = self.block(x) return x class BottleRep(nn.Module): def __init__(self, in_channels, out_channels, basic_block=RepVGGBlock, weight=False): super().__init__() self.conv1 = basic_block(in_channels, out_channels) self.conv2 = basic_block(out_channels, out_channels) if in_channels != out_channels: self.shortcut = False else: self.shortcut = True if weight: self.alpha = Parameter(torch.ones(1)) else: self.alpha = 1.0 def forward(self, x): outputs = self.conv1(x) outputs = self.conv2(outputs) return outputs + self.alpha * x if self.shortcut else outputs def autopad(k, p=None): # kernel, padding # Pad to 'same' if p is None: p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad return p class Conv_C3(nn.Module): '''Standard convolution in BepC3-Block''' def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super().__init__() self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) self.bn = nn.BatchNorm2d(c2) self.act = nn.ReLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) def forward(self, x): return self.act(self.bn(self.conv(x))) def forward_fuse(self, x): return self.act(self.conv(x)) class BepC3(nn.Module): '''Beer-mug RepC3 Block''' def __init__(self, in_channels, out_channels, n=1, e=0.5, concat=True, block=RepVGGBlock): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() c_ = int(out_channels * e) # hidden channels self.cv1 = Conv_C3(in_channels, c_, 1, 1) self.cv2 = Conv_C3(in_channels, c_, 1, 1) self.cv3 = Conv_C3(2 * c_, out_channels, 1, 1) if block == ConvWrapper: self.cv1 = Conv_C3(in_channels, c_, 1, 1, act=nn.SiLU()) self.cv2 = Conv_C3(in_channels, c_, 1, 1, act=nn.SiLU()) self.cv3 = Conv_C3(2 * c_, out_channels, 1, 1, act=nn.SiLU()) self.m = RepBlock(in_channels=c_, out_channels=c_, n=n, block=BottleRep, basic_block=block) self.concat = concat if not concat: self.cv3 = Conv_C3(c_, out_channels, 1, 1) def forward(self, x): if self.concat is True: return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), dim=1)) else: return self.cv3(self.m(self.cv1(x))) def get_block(mode): if mode == 'repvgg': return RepVGGBlock elif mode == 'hyper_search': return LinearAddBlock elif mode == 'repopt': return RealVGGBlock elif mode == 'conv_relu': return SimConvWrapper elif mode == 'conv_silu': return ConvWrapper else: raise NotImplementedError("Undefied Repblock choice for mode {}".format(mode)) ================================================ FILE: asone/detectors/yolov6/yolov6/layers/dbb_transforms.py ================================================ import torch import numpy as np import torch.nn.functional as F def transI_fusebn(kernel, bn): gamma = bn.weight std = (bn.running_var + bn.eps).sqrt() return kernel * ((gamma / std).reshape(-1, 1, 1, 1)), bn.bias - bn.running_mean * gamma / std def transII_addbranch(kernels, biases): return sum(kernels), sum(biases) def transIII_1x1_kxk(k1, b1, k2, b2, groups): if groups == 1: k = F.conv2d(k2, k1.permute(1, 0, 2, 3)) # b_hat = (k2 * b1.reshape(1, -1, 1, 1)).sum((1, 2, 3)) else: k_slices = [] b_slices = [] k1_T = k1.permute(1, 0, 2, 3) k1_group_width = k1.size(0) // groups k2_group_width = k2.size(0) // groups for g in range(groups): k1_T_slice = k1_T[:, g*k1_group_width:(g+1)*k1_group_width, :, :] k2_slice = k2[g*k2_group_width:(g+1)*k2_group_width, :, :, :] k_slices.append(F.conv2d(k2_slice, k1_T_slice)) b_slices.append((k2_slice * b1[g * k1_group_width:(g+1) * k1_group_width].reshape(1, -1, 1, 1)).sum((1, 2, 3))) k, b_hat = transIV_depthconcat(k_slices, b_slices) return k, b_hat + b2 def transIV_depthconcat(kernels, biases): return torch.cat(kernels, dim=0), torch.cat(biases) def transV_avg(channels, kernel_size, groups): input_dim = channels // groups k = torch.zeros((channels, input_dim, kernel_size, kernel_size)) k[np.arange(channels), np.tile(np.arange(input_dim), groups), :, :] = 1.0 / kernel_size ** 2 return k # This has not been tested with non-square kernels (kernel.size(2) != kernel.size(3)) nor even-size kernels def transVI_multiscale(kernel, target_kernel_size): H_pixels_to_pad = (target_kernel_size - kernel.size(2)) // 2 W_pixels_to_pad = (target_kernel_size - kernel.size(3)) // 2 return F.pad(kernel, [H_pixels_to_pad, H_pixels_to_pad, W_pixels_to_pad, W_pixels_to_pad]) ================================================ FILE: asone/detectors/yolov6/yolov6/models/__init__.py ================================================ ================================================ FILE: asone/detectors/yolov6/yolov6/models/efficientrep.py ================================================ from torch import nn from asone.detectors.yolov6.yolov6.layers.common import BottleRep, RepVGGBlock, RepBlock, BepC3, SimSPPF, SPPF, ConvWrapper class EfficientRep(nn.Module): '''EfficientRep Backbone EfficientRep is handcrafted by hardware-aware neural network design. With rep-style struct, EfficientRep is friendly to high-computation hardware(e.g. GPU). ''' def __init__( self, in_channels=3, channels_list=None, num_repeats=None, block=RepVGGBlock ): super().__init__() assert channels_list is not None assert num_repeats is not None self.stem = block( in_channels=in_channels, out_channels=channels_list[0], kernel_size=3, stride=2 ) self.ERBlock_2 = nn.Sequential( block( in_channels=channels_list[0], out_channels=channels_list[1], kernel_size=3, stride=2 ), RepBlock( in_channels=channels_list[1], out_channels=channels_list[1], n=num_repeats[1], block=block, ) ) self.ERBlock_3 = nn.Sequential( block( in_channels=channels_list[1], out_channels=channels_list[2], kernel_size=3, stride=2 ), RepBlock( in_channels=channels_list[2], out_channels=channels_list[2], n=num_repeats[2], block=block, ) ) self.ERBlock_4 = nn.Sequential( block( in_channels=channels_list[2], out_channels=channels_list[3], kernel_size=3, stride=2 ), RepBlock( in_channels=channels_list[3], out_channels=channels_list[3], n=num_repeats[3], block=block, ) ) self.ERBlock_5 = nn.Sequential( block( in_channels=channels_list[3], out_channels=channels_list[4], kernel_size=3, stride=2, ), RepBlock( in_channels=channels_list[4], out_channels=channels_list[4], n=num_repeats[4], block=block, ), SimSPPF( in_channels=channels_list[4], out_channels=channels_list[4], kernel_size=5 ) ) def forward(self, x): outputs = [] x = self.stem(x) x = self.ERBlock_2(x) x = self.ERBlock_3(x) outputs.append(x) x = self.ERBlock_4(x) outputs.append(x) x = self.ERBlock_5(x) outputs.append(x) return tuple(outputs) class CSPBepBackbone(nn.Module): """ CSPBepBackbone module. """ def __init__( self, in_channels=3, channels_list=None, num_repeats=None, block=RepVGGBlock, csp_e=float(1)/2, ): super().__init__() assert channels_list is not None assert num_repeats is not None self.stem = block( in_channels=in_channels, out_channels=channels_list[0], kernel_size=3, stride=2 ) self.ERBlock_2 = nn.Sequential( block( in_channels=channels_list[0], out_channels=channels_list[1], kernel_size=3, stride=2 ), BepC3( in_channels=channels_list[1], out_channels=channels_list[1], n=num_repeats[1], e=csp_e, block=block, ) ) self.ERBlock_3 = nn.Sequential( block( in_channels=channels_list[1], out_channels=channels_list[2], kernel_size=3, stride=2 ), BepC3( in_channels=channels_list[2], out_channels=channels_list[2], n=num_repeats[2], e=csp_e, block=block, ) ) self.ERBlock_4 = nn.Sequential( block( in_channels=channels_list[2], out_channels=channels_list[3], kernel_size=3, stride=2 ), BepC3( in_channels=channels_list[3], out_channels=channels_list[3], n=num_repeats[3], e=csp_e, block=block, ) ) channel_merge_layer = SimSPPF if block == ConvWrapper: channel_merge_layer = SPPF self.ERBlock_5 = nn.Sequential( block( in_channels=channels_list[3], out_channels=channels_list[4], kernel_size=3, stride=2, ), BepC3( in_channels=channels_list[4], out_channels=channels_list[4], n=num_repeats[4], e=csp_e, block=block, ), channel_merge_layer( in_channels=channels_list[4], out_channels=channels_list[4], kernel_size=5 ) ) def forward(self, x): outputs = [] x = self.stem(x) x = self.ERBlock_2(x) x = self.ERBlock_3(x) outputs.append(x) x = self.ERBlock_4(x) outputs.append(x) x = self.ERBlock_5(x) outputs.append(x) return tuple(outputs) ================================================ FILE: asone/detectors/yolov6/yolov6/models/effidehead.py ================================================ import torch import torch.nn as nn import torch.nn.functional as F import math from asone.detectors.yolov6.yolov6.layers.common import * from asone.detectors.yolov6.yolov6.assigners.anchor_generator import generate_anchors from asone.detectors.yolov6.yolov6.utils.general import dist2bbox class Detect(nn.Module): '''Efficient Decoupled Head With hardware-aware degisn, the decoupled head is optimized with hybridchannels methods. ''' def __init__(self, num_classes=80, anchors=1, num_layers=3, inplace=True, head_layers=None, use_dfl=True, reg_max=16): # detection layer super().__init__() assert head_layers is not None self.nc = num_classes # number of classes self.no = num_classes + 5 # number of outputs per anchor self.nl = num_layers # number of detection layers if isinstance(anchors, (list, tuple)): self.na = len(anchors[0]) // 2 else: self.na = anchors self.anchors = anchors self.grid = [torch.zeros(1)] * num_layers self.prior_prob = 1e-2 self.inplace = inplace stride = [8, 16, 32] # strides computed during build self.stride = torch.tensor(stride) self.use_dfl = use_dfl self.reg_max = reg_max self.proj_conv = nn.Conv2d(self.reg_max + 1, 1, 1, bias=False) self.grid_cell_offset = 0.5 self.grid_cell_size = 5.0 # Init decouple head self.stems = nn.ModuleList() self.cls_convs = nn.ModuleList() self.reg_convs = nn.ModuleList() self.cls_preds = nn.ModuleList() self.reg_preds = nn.ModuleList() # Efficient decoupled head layers for i in range(num_layers): idx = i*5 self.stems.append(head_layers[idx]) self.cls_convs.append(head_layers[idx+1]) self.reg_convs.append(head_layers[idx+2]) self.cls_preds.append(head_layers[idx+3]) self.reg_preds.append(head_layers[idx+4]) def initialize_biases(self): for conv in self.cls_preds: b = conv.bias.view(-1, ) b.data.fill_(-math.log((1 - self.prior_prob) / self.prior_prob)) conv.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) w = conv.weight w.data.fill_(0.) conv.weight = torch.nn.Parameter(w, requires_grad=True) for conv in self.reg_preds: b = conv.bias.view(-1, ) b.data.fill_(1.0) conv.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) w = conv.weight w.data.fill_(0.) conv.weight = torch.nn.Parameter(w, requires_grad=True) self.proj = nn.Parameter(torch.linspace(0, self.reg_max, self.reg_max + 1), requires_grad=False) self.proj_conv.weight = nn.Parameter(self.proj.view([1, self.reg_max + 1, 1, 1]).clone().detach(), requires_grad=False) def forward(self, x): if self.training: cls_score_list = [] reg_distri_list = [] for i in range(self.nl): x[i] = self.stems[i](x[i]) cls_x = x[i] reg_x = x[i] cls_feat = self.cls_convs[i](cls_x) cls_output = self.cls_preds[i](cls_feat) reg_feat = self.reg_convs[i](reg_x) reg_output = self.reg_preds[i](reg_feat) cls_output = torch.sigmoid(cls_output) cls_score_list.append(cls_output.flatten(2).permute((0, 2, 1))) reg_distri_list.append(reg_output.flatten(2).permute((0, 2, 1))) cls_score_list = torch.cat(cls_score_list, axis=1) reg_distri_list = torch.cat(reg_distri_list, axis=1) return x, cls_score_list, reg_distri_list else: cls_score_list = [] reg_dist_list = [] anchor_points, stride_tensor = generate_anchors( x, self.stride, self.grid_cell_size, self.grid_cell_offset, device=x[0].device, is_eval=True) for i in range(self.nl): b, _, h, w = x[i].shape l = h * w x[i] = self.stems[i](x[i]) cls_x = x[i] reg_x = x[i] cls_feat = self.cls_convs[i](cls_x) cls_output = self.cls_preds[i](cls_feat) reg_feat = self.reg_convs[i](reg_x) reg_output = self.reg_preds[i](reg_feat) if self.use_dfl: reg_output = reg_output.reshape([-1, 4, self.reg_max + 1, l]).permute(0, 2, 1, 3) reg_output = self.proj_conv(F.softmax(reg_output, dim=1)) cls_output = torch.sigmoid(cls_output) cls_score_list.append(cls_output.reshape([b, self.nc, l])) reg_dist_list.append(reg_output.reshape([b, 4, l])) cls_score_list = torch.cat(cls_score_list, axis=-1).permute(0, 2, 1) reg_dist_list = torch.cat(reg_dist_list, axis=-1).permute(0, 2, 1) pred_bboxes = dist2bbox(reg_dist_list, anchor_points, box_format='xywh') pred_bboxes *= stride_tensor return torch.cat( [ pred_bboxes, torch.ones((b, pred_bboxes.shape[1], 1), device=pred_bboxes.device, dtype=pred_bboxes.dtype), cls_score_list ], axis=-1) def build_effidehead_layer(channels_list, num_anchors, num_classes, reg_max=16): head_layers = nn.Sequential( # stem0 Conv( in_channels=channels_list[6], out_channels=channels_list[6], kernel_size=1, stride=1 ), # cls_conv0 Conv( in_channels=channels_list[6], out_channels=channels_list[6], kernel_size=3, stride=1 ), # reg_conv0 Conv( in_channels=channels_list[6], out_channels=channels_list[6], kernel_size=3, stride=1 ), # cls_pred0 nn.Conv2d( in_channels=channels_list[6], out_channels=num_classes * num_anchors, kernel_size=1 ), # reg_pred0 nn.Conv2d( in_channels=channels_list[6], out_channels=4 * (reg_max + num_anchors), kernel_size=1 ), # stem1 Conv( in_channels=channels_list[8], out_channels=channels_list[8], kernel_size=1, stride=1 ), # cls_conv1 Conv( in_channels=channels_list[8], out_channels=channels_list[8], kernel_size=3, stride=1 ), # reg_conv1 Conv( in_channels=channels_list[8], out_channels=channels_list[8], kernel_size=3, stride=1 ), # cls_pred1 nn.Conv2d( in_channels=channels_list[8], out_channels=num_classes * num_anchors, kernel_size=1 ), # reg_pred1 nn.Conv2d( in_channels=channels_list[8], out_channels=4 * (reg_max + num_anchors), kernel_size=1 ), # stem2 Conv( in_channels=channels_list[10], out_channels=channels_list[10], kernel_size=1, stride=1 ), # cls_conv2 Conv( in_channels=channels_list[10], out_channels=channels_list[10], kernel_size=3, stride=1 ), # reg_conv2 Conv( in_channels=channels_list[10], out_channels=channels_list[10], kernel_size=3, stride=1 ), # cls_pred2 nn.Conv2d( in_channels=channels_list[10], out_channels=num_classes * num_anchors, kernel_size=1 ), # reg_pred2 nn.Conv2d( in_channels=channels_list[10], out_channels=4 * (reg_max + num_anchors), kernel_size=1 ) ) return head_layers ================================================ FILE: asone/detectors/yolov6/yolov6/models/end2end.py ================================================ import torch import torch.nn as nn import random class ORT_NMS(torch.autograd.Function): '''ONNX-Runtime NMS operation''' @staticmethod def forward(ctx, boxes, scores, max_output_boxes_per_class=torch.tensor([100]), iou_threshold=torch.tensor([0.45]), score_threshold=torch.tensor([0.25])): device = boxes.device batch = scores.shape[0] num_det = random.randint(0, 100) batches = torch.randint(0, batch, (num_det,)).sort()[0].to(device) idxs = torch.arange(100, 100 + num_det).to(device) zeros = torch.zeros((num_det,), dtype=torch.int64).to(device) selected_indices = torch.cat([batches[None], zeros[None], idxs[None]], 0).T.contiguous() selected_indices = selected_indices.to(torch.int64) return selected_indices @staticmethod def symbolic(g, boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold): return g.op("NonMaxSuppression", boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold) class TRT8_NMS(torch.autograd.Function): '''TensorRT NMS operation''' @staticmethod def forward( ctx, boxes, scores, background_class=-1, box_coding=1, iou_threshold=0.45, max_output_boxes=100, plugin_version="1", score_activation=0, score_threshold=0.25, ): batch_size, num_boxes, num_classes = scores.shape num_det = torch.randint(0, max_output_boxes, (batch_size, 1), dtype=torch.int32) det_boxes = torch.randn(batch_size, max_output_boxes, 4) det_scores = torch.randn(batch_size, max_output_boxes) det_classes = torch.randint(0, num_classes, (batch_size, max_output_boxes), dtype=torch.int32) return num_det, det_boxes, det_scores, det_classes @staticmethod def symbolic(g, boxes, scores, background_class=-1, box_coding=1, iou_threshold=0.45, max_output_boxes=100, plugin_version="1", score_activation=0, score_threshold=0.25): out = g.op("TRT::EfficientNMS_TRT", boxes, scores, background_class_i=background_class, box_coding_i=box_coding, iou_threshold_f=iou_threshold, max_output_boxes_i=max_output_boxes, plugin_version_s=plugin_version, score_activation_i=score_activation, score_threshold_f=score_threshold, outputs=4) nums, boxes, scores, classes = out return nums, boxes, scores, classes class TRT7_NMS(torch.autograd.Function): '''TensorRT NMS operation''' @staticmethod def forward( ctx, boxes, scores, plugin_version="1", shareLocation=1, backgroundLabelId=-1, numClasses=80, topK=1000, keepTopK=100, scoreThreshold=0.25, iouThreshold=0.45, isNormalized=0, clipBoxes=0, scoreBits=16, caffeSemantics=1, ): batch_size, num_boxes, numClasses = scores.shape num_det = torch.randint(0, keepTopK, (batch_size, 1), dtype=torch.int32) det_boxes = torch.randn(batch_size, keepTopK, 4) det_scores = torch.randn(batch_size, keepTopK) det_classes = torch.randint(0, numClasses, (batch_size, keepTopK)).float() return num_det, det_boxes, det_scores, det_classes @staticmethod def symbolic(g, boxes, scores, plugin_version='1', shareLocation=1, backgroundLabelId=-1, numClasses=80, topK=1000, keepTopK=100, scoreThreshold=0.25, iouThreshold=0.45, isNormalized=0, clipBoxes=0, scoreBits=16, caffeSemantics=1, ): out = g.op("TRT::BatchedNMSDynamic_TRT", # BatchedNMS_TRT BatchedNMSDynamic_TRT boxes, scores, shareLocation_i=shareLocation, plugin_version_s=plugin_version, backgroundLabelId_i=backgroundLabelId, numClasses_i=numClasses, topK_i=topK, keepTopK_i=keepTopK, scoreThreshold_f=scoreThreshold, iouThreshold_f=iouThreshold, isNormalized_i=isNormalized, clipBoxes_i=clipBoxes, scoreBits_i=scoreBits, caffeSemantics_i=caffeSemantics, outputs=4) nums, boxes, scores, classes = out return nums, boxes, scores, classes class ONNX_ORT(nn.Module): '''onnx module with ONNX-Runtime NMS operation.''' def __init__(self, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=640, device=None): super().__init__() self.device = device if device else torch.device("cpu") self.max_obj = torch.tensor([max_obj]).to(device) self.iou_threshold = torch.tensor([iou_thres]).to(device) self.score_threshold = torch.tensor([score_thres]).to(device) self.max_wh = max_wh self.convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]], dtype=torch.float32, device=self.device) def forward(self, x): box = x[:, :, :4] conf = x[:, :, 4:5] score = x[:, :, 5:] score *= conf box @= self.convert_matrix objScore, objCls = score.max(2, keepdim=True) dis = objCls.float() * self.max_wh nmsbox = box + dis objScore1 = objScore.transpose(1, 2).contiguous() selected_indices = ORT_NMS.apply(nmsbox, objScore1, self.max_obj, self.iou_threshold, self.score_threshold) X, Y = selected_indices[:, 0], selected_indices[:, 2] resBoxes = box[X, Y, :] resClasses = objCls[X, Y, :].float() resScores = objScore[X, Y, :] X = X.unsqueeze(1).float() return torch.cat([X, resBoxes, resClasses, resScores], 1) class ONNX_TRT7(nn.Module): '''onnx module with TensorRT NMS operation.''' def __init__(self, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=None ,device=None): super().__init__() assert max_wh is None self.device = device if device else torch.device('cpu') self.shareLocation = 1 self.backgroundLabelId = -1 self.numClasses = 80 self.topK = 1000 self.keepTopK = max_obj self.scoreThreshold = score_thres self.iouThreshold = iou_thres self.isNormalized = 0 self.clipBoxes = 0 self.scoreBits = 16 self.caffeSemantics = 1 self.plugin_version = '1' self.convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]], dtype=torch.float32, device=self.device) def forward(self, x): box = x[:, :, :4] conf = x[:, :, 4:5] score = x[:, :, 5:] score *= conf box @= self.convert_matrix box = box.unsqueeze(2) self.numClasses = int(score.shape[2]) num_det, det_boxes, det_scores, det_classes = TRT7_NMS.apply(box, score, self.plugin_version, self.shareLocation, self.backgroundLabelId, self.numClasses, self.topK, self.keepTopK, self.scoreThreshold, self.iouThreshold, self.isNormalized, self.clipBoxes, self.scoreBits, self.caffeSemantics, ) return num_det, det_boxes, det_scores, det_classes.int() class ONNX_TRT8(nn.Module): '''onnx module with TensorRT NMS operation.''' def __init__(self, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=None ,device=None): super().__init__() assert max_wh is None self.device = device if device else torch.device('cpu') self.background_class = -1, self.box_coding = 1, self.iou_threshold = iou_thres self.max_obj = max_obj self.plugin_version = '1' self.score_activation = 0 self.score_threshold = score_thres def forward(self, x): box = x[:, :, :4] conf = x[:, :, 4:5] score = x[:, :, 5:] score *= conf num_det, det_boxes, det_scores, det_classes = TRT8_NMS.apply(box, score, self.background_class, self.box_coding, self.iou_threshold, self.max_obj, self.plugin_version, self.score_activation, self.score_threshold) return num_det, det_boxes, det_scores, det_classes class End2End(nn.Module): '''export onnx or tensorrt model with NMS operation.''' def __init__(self, model, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=None, device=None, trt_version=8, with_preprocess=False): super().__init__() device = device if device else torch.device('cpu') self.with_preprocess = with_preprocess self.model = model.to(device) TRT = ONNX_TRT8 if trt_version >= 8 else ONNX_TRT7 self.patch_model = TRT if max_wh is None else ONNX_ORT self.end2end = self.patch_model(max_obj, iou_thres, score_thres, max_wh, device) self.end2end.eval() def forward(self, x): if self.with_preprocess: x = x[:,[2,1,0],...] x = x * (1/255) x = self.model(x) x = self.end2end(x) return x ================================================ FILE: asone/detectors/yolov6/yolov6/models/loss.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- import torch import torch.nn as nn import numpy as np import torch.nn.functional as F from asone.detectors.yolov6.yolov6.assigners.anchor_generator import generate_anchors from asone.detectors.yolov6.yolov6.utils.general import dist2bbox, bbox2dist, xywh2xyxy from asone.detectors.yolov6.yolov6.utils.figure_iou import IOUloss from asone.detectors.yolov6.yolov6.assigners.atss_assigner import ATSSAssigner from asone.detectors.yolov6.yolov6.assigners.tal_assigner import TaskAlignedAssigner class ComputeLoss: '''Loss computation func.''' def __init__(self, fpn_strides=[8, 16, 32], grid_cell_size=5.0, grid_cell_offset=0.5, num_classes=80, ori_img_size=640, warmup_epoch=4, use_dfl=True, reg_max=16, iou_type='giou', loss_weight={ 'class': 1.0, 'iou': 2.5, 'dfl': 0.5} ): self.fpn_strides = fpn_strides self.grid_cell_size = grid_cell_size self.grid_cell_offset = grid_cell_offset self.num_classes = num_classes self.ori_img_size = ori_img_size self.warmup_epoch = warmup_epoch self.warmup_assigner = ATSSAssigner(9, num_classes=self.num_classes) self.formal_assigner = TaskAlignedAssigner(topk=13, num_classes=self.num_classes, alpha=1.0, beta=6.0) self.use_dfl = use_dfl self.reg_max = reg_max self.proj = nn.Parameter(torch.linspace(0, self.reg_max, self.reg_max + 1), requires_grad=False) self.iou_type = iou_type self.varifocal_loss = VarifocalLoss().cuda() self.bbox_loss = BboxLoss(self.num_classes, self.reg_max, self.use_dfl, self.iou_type).cuda() self.loss_weight = loss_weight def __call__( self, outputs, targets, epoch_num ): feats, pred_scores, pred_distri = outputs anchors, anchor_points, n_anchors_list, stride_tensor = \ generate_anchors(feats, self.fpn_strides, self.grid_cell_size, self.grid_cell_offset, device=feats[0].device) assert pred_scores.type() == pred_distri.type() gt_bboxes_scale = torch.full((1,4), self.ori_img_size).type_as(pred_scores) batch_size = pred_scores.shape[0] # targets targets =self.preprocess(targets, batch_size, gt_bboxes_scale) gt_labels = targets[:, :, :1] gt_bboxes = targets[:, :, 1:] #xyxy mask_gt = (gt_bboxes.sum(-1, keepdim=True) > 0).float() # pboxes anchor_points_s = anchor_points / stride_tensor pred_bboxes = self.bbox_decode(anchor_points_s, pred_distri) #xyxy if epoch_num < self.warmup_epoch: target_labels, target_bboxes, target_scores, fg_mask = \ self.warmup_assigner( anchors, n_anchors_list, gt_labels, gt_bboxes, mask_gt, pred_bboxes.detach() * stride_tensor) else: target_labels, target_bboxes, target_scores, fg_mask = \ self.formal_assigner( pred_scores.detach(), pred_bboxes.detach() * stride_tensor, anchor_points, gt_labels, gt_bboxes, mask_gt) # rescale bbox target_bboxes /= stride_tensor # cls loss target_labels = torch.where(fg_mask > 0, target_labels, torch.full_like(target_labels, self.num_classes)) one_hot_label = F.one_hot(target_labels.long(), self.num_classes + 1)[..., :-1] loss_cls = self.varifocal_loss(pred_scores, target_scores, one_hot_label) target_scores_sum = target_scores.sum() loss_cls /= target_scores_sum # bbox loss loss_iou, loss_dfl = self.bbox_loss(pred_distri, pred_bboxes, anchor_points_s, target_bboxes, target_scores, target_scores_sum, fg_mask) loss = self.loss_weight['class'] * loss_cls + \ self.loss_weight['iou'] * loss_iou + \ self.loss_weight['dfl'] * loss_dfl return loss, \ torch.cat(((self.loss_weight['iou'] * loss_iou).unsqueeze(0), (self.loss_weight['dfl'] * loss_dfl).unsqueeze(0), (self.loss_weight['class'] * loss_cls).unsqueeze(0))).detach() def preprocess(self, targets, batch_size, scale_tensor): targets_list = np.zeros((batch_size, 1, 5)).tolist() for i, item in enumerate(targets.cpu().numpy().tolist()): targets_list[int(item[0])].append(item[1:]) max_len = max((len(l) for l in targets_list)) targets = torch.from_numpy(np.array(list(map(lambda l:l + [[-1,0,0,0,0]]*(max_len - len(l)), targets_list)))[:,1:,:]).to(targets.device) batch_target = targets[:, :, 1:5].mul_(scale_tensor) targets[..., 1:] = xywh2xyxy(batch_target) return targets def bbox_decode(self, anchor_points, pred_dist): if self.use_dfl: batch_size, n_anchors, _ = pred_dist.shape pred_dist = F.softmax(pred_dist.view(batch_size, n_anchors, 4, self.reg_max + 1), dim=-1).matmul(self.proj.to(pred_dist.device)) return dist2bbox(pred_dist, anchor_points) class VarifocalLoss(nn.Module): def __init__(self): super(VarifocalLoss, self).__init__() def forward(self, pred_score,gt_score, label, alpha=0.75, gamma=2.0): weight = alpha * pred_score.pow(gamma) * (1 - label) + gt_score * label with torch.cuda.amp.autocast(enabled=False): loss = (F.binary_cross_entropy(pred_score.float(), gt_score.float(), reduction='none') * weight).sum() return loss class BboxLoss(nn.Module): def __init__(self, num_classes, reg_max, use_dfl=False, iou_type='giou'): super(BboxLoss, self).__init__() self.num_classes = num_classes self.iou_loss = IOUloss(box_format='xyxy', iou_type=iou_type, eps=1e-10) self.reg_max = reg_max self.use_dfl = use_dfl def forward(self, pred_dist, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask): # select positive samples mask num_pos = fg_mask.sum() if num_pos > 0: # iou loss bbox_mask = fg_mask.unsqueeze(-1).repeat([1, 1, 4]) pred_bboxes_pos = torch.masked_select(pred_bboxes, bbox_mask).reshape([-1, 4]) target_bboxes_pos = torch.masked_select( target_bboxes, bbox_mask).reshape([-1, 4]) bbox_weight = torch.masked_select( target_scores.sum(-1), fg_mask).unsqueeze(-1) loss_iou = self.iou_loss(pred_bboxes_pos, target_bboxes_pos) * bbox_weight loss_iou = loss_iou.sum() / target_scores_sum # dfl loss if self.use_dfl: dist_mask = fg_mask.unsqueeze(-1).repeat( [1, 1, (self.reg_max + 1) * 4]) pred_dist_pos = torch.masked_select( pred_dist, dist_mask).reshape([-1, 4, self.reg_max + 1]) target_ltrb = bbox2dist(anchor_points, target_bboxes, self.reg_max) target_ltrb_pos = torch.masked_select( target_ltrb, bbox_mask).reshape([-1, 4]) loss_dfl = self._df_loss(pred_dist_pos, target_ltrb_pos) * bbox_weight loss_dfl = loss_dfl.sum() / target_scores_sum else: loss_dfl = torch.tensor(0.).to(pred_dist.device) else: loss_iou = torch.tensor(0.).to(pred_dist.device) loss_dfl = torch.tensor(0.).to(pred_dist.device) return loss_iou, loss_dfl def _df_loss(self, pred_dist, target): target_left = target.to(torch.long) target_right = target_left + 1 weight_left = target_right.to(torch.float) - target weight_right = 1 - weight_left loss_left = F.cross_entropy( pred_dist.view(-1, self.reg_max + 1), target_left.view(-1), reduction='none').view( target_left.shape) * weight_left loss_right = F.cross_entropy( pred_dist.view(-1, self.reg_max + 1), target_right.view(-1), reduction='none').view( target_left.shape) * weight_right return (loss_left + loss_right).mean(-1, keepdim=True) ================================================ FILE: asone/detectors/yolov6/yolov6/models/loss_distill.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- import torch import torch.nn as nn import numpy as np import torch.nn.functional as F from asone.detectors.yolov6.yolov6.assigners.anchor_generator import generate_anchors from asone.detectors.yolov6.yolov6.utils.general import dist2bbox, bbox2dist, xywh2xyxy from asone.detectors.yolov6.yolov6.utils.figure_iou import IOUloss from asone.detectors.yolov6.yolov6.assigners.atss_assigner import ATSSAssigner from asone.detectors.yolov6.yolov6.assigners.tal_assigner import TaskAlignedAssigner class ComputeLoss: '''Loss computation func.''' def __init__(self, fpn_strides=[8, 16, 32], grid_cell_size=5.0, grid_cell_offset=0.5, num_classes=80, ori_img_size=640, warmup_epoch=4, use_dfl=True, reg_max=16, iou_type='giou', loss_weight={ 'class': 1.0, 'iou': 2.5, 'dfl': 0.5, 'cwd': 10.0}, distill_feat = False, distill_weight={ 'class': 1.0, 'dfl': 1.0, } ): self.fpn_strides = fpn_strides self.grid_cell_size = grid_cell_size self.grid_cell_offset = grid_cell_offset self.num_classes = num_classes self.ori_img_size = ori_img_size self.warmup_epoch = warmup_epoch self.warmup_assigner = ATSSAssigner(9, num_classes=self.num_classes) self.formal_assigner = TaskAlignedAssigner(topk=13, num_classes=self.num_classes, alpha=1.0, beta=6.0) self.use_dfl = use_dfl self.reg_max = reg_max self.proj = nn.Parameter(torch.linspace(0, self.reg_max, self.reg_max + 1), requires_grad=False) self.iou_type = iou_type self.varifocal_loss = VarifocalLoss().cuda() self.bbox_loss = BboxLoss(self.num_classes, self.reg_max, self.use_dfl, self.iou_type).cuda() self.loss_weight = loss_weight self.distill_feat = distill_feat self.distill_weight = distill_weight def __call__( self, outputs, t_outputs, s_featmaps, t_featmaps, targets, epoch_num, max_epoch, temperature ): feats, pred_scores, pred_distri = outputs t_feats, t_pred_scores, t_pred_distri = t_outputs anchors, anchor_points, n_anchors_list, stride_tensor = \ generate_anchors(feats, self.fpn_strides, self.grid_cell_size, self.grid_cell_offset, device=feats[0].device) t_anchors, t_anchor_points, t_n_anchors_list, t_stride_tensor = \ generate_anchors(t_feats, self.fpn_strides, self.grid_cell_size, self.grid_cell_offset, device=feats[0].device) assert pred_scores.type() == pred_distri.type() gt_bboxes_scale = torch.full((1,4), self.ori_img_size).type_as(pred_scores) batch_size = pred_scores.shape[0] # targets targets =self.preprocess(targets, batch_size, gt_bboxes_scale) gt_labels = targets[:, :, :1] gt_bboxes = targets[:, :, 1:] #xyxy mask_gt = (gt_bboxes.sum(-1, keepdim=True) > 0).float() # pboxes anchor_points_s = anchor_points / stride_tensor pred_bboxes = self.bbox_decode(anchor_points_s, pred_distri) #xyxy t_anchor_points_s = t_anchor_points / t_stride_tensor t_pred_bboxes = self.bbox_decode(t_anchor_points_s, t_pred_distri) #xyxy if epoch_num < self.warmup_epoch: target_labels, target_bboxes, target_scores, fg_mask = \ self.warmup_assigner( anchors, n_anchors_list, gt_labels, gt_bboxes, mask_gt, pred_bboxes.detach() * stride_tensor) else: target_labels, target_bboxes, target_scores, fg_mask = \ self.formal_assigner( pred_scores.detach(), pred_bboxes.detach() * stride_tensor, anchor_points, gt_labels, gt_bboxes, mask_gt) # rescale bbox target_bboxes /= stride_tensor # cls loss target_labels = torch.where(fg_mask > 0, target_labels, torch.full_like(target_labels, self.num_classes)) one_hot_label = F.one_hot(target_labels, self.num_classes + 1)[..., :-1] loss_cls = self.varifocal_loss(pred_scores, target_scores, one_hot_label) target_scores_sum = target_scores.sum() loss_cls /= target_scores_sum # bbox loss loss_iou, loss_dfl, d_loss_dfl = self.bbox_loss(pred_distri, pred_bboxes, t_pred_distri, t_pred_bboxes, temperature, anchor_points_s, target_bboxes, target_scores, target_scores_sum, fg_mask) logits_student = pred_scores logits_teacher = t_pred_scores distill_num_classes = self.num_classes d_loss_cls = self.distill_loss_cls(logits_student, logits_teacher, distill_num_classes, temperature) if self.distill_feat: d_loss_cw = self.distill_loss_cw(s_featmaps, t_featmaps) else: d_loss_cw = torch.tensor(0.).to(feats[0].device) import math distill_weightdecay = ((1 - math.cos(epoch_num * math.pi / max_epoch)) / 2) * (0.01- 1) + 1 d_loss_dfl *= distill_weightdecay d_loss_cls *= distill_weightdecay d_loss_cw *= distill_weightdecay loss_cls_all = loss_cls + d_loss_cls * self.distill_weight['class'] loss_dfl_all = loss_dfl + d_loss_dfl * self.distill_weight['dfl'] loss = self.loss_weight['class'] * loss_cls_all + \ self.loss_weight['iou'] * loss_iou + \ self.loss_weight['dfl'] * loss_dfl_all + \ self.loss_weight['cwd'] * d_loss_cw return loss, \ torch.cat(((self.loss_weight['iou'] * loss_iou).unsqueeze(0), (self.loss_weight['dfl'] * loss_dfl_all).unsqueeze(0), (self.loss_weight['class'] * loss_cls_all).unsqueeze(0), (self.loss_weight['cwd'] * d_loss_cw).unsqueeze(0))).detach() def distill_loss_cls(self, logits_student, logits_teacher, num_classes, temperature=20): logits_student = logits_student.view(-1, num_classes) logits_teacher = logits_teacher.view(-1, num_classes) pred_student = F.softmax(logits_student / temperature, dim=1) pred_teacher = F.softmax(logits_teacher / temperature, dim=1) log_pred_student = torch.log(pred_student) d_loss_cls = F.kl_div(log_pred_student, pred_teacher, reduction="sum") d_loss_cls *= temperature**2 return d_loss_cls def distill_loss_cw(self, s_feats, t_feats, temperature=1): N,C,H,W = s_feats[0].shape # print(N,C,H,W) loss_cw = F.kl_div(F.log_softmax(s_feats[0].view(N,C,H*W)/temperature, dim=2), F.log_softmax(t_feats[0].view(N,C,H*W).detach()/temperature, dim=2), reduction='sum', log_target=True) * (temperature * temperature)/ (N*C) N,C,H,W = s_feats[1].shape # print(N,C,H,W) loss_cw += F.kl_div(F.log_softmax(s_feats[1].view(N,C,H*W)/temperature, dim=2), F.log_softmax(t_feats[1].view(N,C,H*W).detach()/temperature, dim=2), reduction='sum', log_target=True) * (temperature * temperature)/ (N*C) N,C,H,W = s_feats[2].shape # print(N,C,H,W) loss_cw += F.kl_div(F.log_softmax(s_feats[2].view(N,C,H*W)/temperature, dim=2), F.log_softmax(t_feats[2].view(N,C,H*W).detach()/temperature, dim=2), reduction='sum', log_target=True) * (temperature * temperature)/ (N*C) # print(loss_cw) return loss_cw def preprocess(self, targets, batch_size, scale_tensor): targets_list = np.zeros((batch_size, 1, 5)).tolist() for i, item in enumerate(targets.cpu().numpy().tolist()): targets_list[int(item[0])].append(item[1:]) max_len = max((len(l) for l in targets_list)) targets = torch.from_numpy(np.array(list(map(lambda l:l + [[-1,0,0,0,0]]*(max_len - len(l)), targets_list)))[:,1:,:]).to(targets.device) batch_target = targets[:, :, 1:5].mul_(scale_tensor) targets[..., 1:] = xywh2xyxy(batch_target) return targets def bbox_decode(self, anchor_points, pred_dist): if self.use_dfl: batch_size, n_anchors, _ = pred_dist.shape pred_dist = F.softmax(pred_dist.view(batch_size, n_anchors, 4, self.reg_max + 1), dim=-1).matmul(self.proj.to(pred_dist.device)) return dist2bbox(pred_dist, anchor_points) class VarifocalLoss(nn.Module): def __init__(self): super(VarifocalLoss, self).__init__() def forward(self, pred_score,gt_score, label, alpha=0.75, gamma=2.0): weight = alpha * pred_score.pow(gamma) * (1 - label) + gt_score * label with torch.cuda.amp.autocast(enabled=False): loss = (F.binary_cross_entropy(pred_score.float(), gt_score.float(), reduction='none') * weight).sum() return loss class BboxLoss(nn.Module): def __init__(self, num_classes, reg_max, use_dfl=False, iou_type='giou'): super(BboxLoss, self).__init__() self.num_classes = num_classes self.iou_loss = IOUloss(box_format='xyxy', iou_type=iou_type, eps=1e-10) self.reg_max = reg_max self.use_dfl = use_dfl def forward(self, pred_dist, pred_bboxes, t_pred_dist, t_pred_bboxes, temperature, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask): # select positive samples mask num_pos = fg_mask.sum() if num_pos > 0: # iou loss bbox_mask = fg_mask.unsqueeze(-1).repeat([1, 1, 4]) pred_bboxes_pos = torch.masked_select(pred_bboxes, bbox_mask).reshape([-1, 4]) t_pred_bboxes_pos = torch.masked_select(t_pred_bboxes, bbox_mask).reshape([-1, 4]) target_bboxes_pos = torch.masked_select( target_bboxes, bbox_mask).reshape([-1, 4]) bbox_weight = torch.masked_select( target_scores.sum(-1), fg_mask).unsqueeze(-1) loss_iou = self.iou_loss(pred_bboxes_pos, target_bboxes_pos) * bbox_weight loss_iou = loss_iou.sum() / target_scores_sum # dfl loss if self.use_dfl: dist_mask = fg_mask.unsqueeze(-1).repeat( [1, 1, (self.reg_max + 1) * 4]) pred_dist_pos = torch.masked_select( pred_dist, dist_mask).reshape([-1, 4, self.reg_max + 1]) t_pred_dist_pos = torch.masked_select( t_pred_dist, dist_mask).reshape([-1, 4, self.reg_max + 1]) target_ltrb = bbox2dist(anchor_points, target_bboxes, self.reg_max) target_ltrb_pos = torch.masked_select( target_ltrb, bbox_mask).reshape([-1, 4]) loss_dfl = self._df_loss(pred_dist_pos, target_ltrb_pos) * bbox_weight d_loss_dfl = self.distill_loss_dfl(pred_dist_pos, t_pred_dist_pos, temperature) * bbox_weight loss_dfl = loss_dfl.sum() / target_scores_sum d_loss_dfl = d_loss_dfl.sum() / target_scores_sum else: loss_dfl = torch.tensor(0.).to(pred_dist.device) d_loss_dfl = torch.tensor(0.).to(pred_dist.device) else: loss_iou = torch.tensor(0.).to(pred_dist.device) loss_dfl = torch.tensor(0.).to(pred_dist.device) d_loss_dfl = torch.tensor(0.).to(pred_dist.device) return loss_iou, loss_dfl, d_loss_dfl def _df_loss(self, pred_dist, target): target_left = target.to(torch.long) target_right = target_left + 1 weight_left = target_right.to(torch.float) - target weight_right = 1 - weight_left loss_left = F.cross_entropy( pred_dist.view(-1, self.reg_max + 1), target_left.view(-1), reduction='none').view( target_left.shape) * weight_left loss_right = F.cross_entropy( pred_dist.view(-1, self.reg_max + 1), target_right.view(-1), reduction='none').view( target_left.shape) * weight_right return (loss_left + loss_right).mean(-1, keepdim=True) def distill_loss_dfl(self, logits_student, logits_teacher, temperature=20): logits_student = logits_student.view(-1,17) logits_teacher = logits_teacher.view(-1,17) pred_student = F.softmax(logits_student / temperature, dim=1) pred_teacher = F.softmax(logits_teacher / temperature, dim=1) log_pred_student = torch.log(pred_student) d_loss_dfl = F.kl_div(log_pred_student, pred_teacher, reduction="none").sum(1).mean() d_loss_dfl *= temperature**2 return d_loss_dfl ================================================ FILE: asone/detectors/yolov6/yolov6/models/reppan.py ================================================ import torch from torch import nn from asone.detectors.yolov6.yolov6.layers.common import RepBlock, RepVGGBlock, BottleRep, BepC3, SimConv, Transpose _QUANT=False class RepPANNeck(nn.Module): """RepPANNeck Module EfficientRep is the default backbone of this model. RepPANNeck has the balance of feature fusion ability and hardware efficiency. """ def __init__( self, channels_list=None, num_repeats=None, block=RepVGGBlock ): super().__init__() assert channels_list is not None assert num_repeats is not None self.Rep_p4 = RepBlock( in_channels=channels_list[3] + channels_list[5], out_channels=channels_list[5], n=num_repeats[5], block=block ) self.Rep_p3 = RepBlock( in_channels=channels_list[2] + channels_list[6], out_channels=channels_list[6], n=num_repeats[6], block=block ) self.Rep_n3 = RepBlock( in_channels=channels_list[6] + channels_list[7], out_channels=channels_list[8], n=num_repeats[7], block=block ) self.Rep_n4 = RepBlock( in_channels=channels_list[5] + channels_list[9], out_channels=channels_list[10], n=num_repeats[8], block=block ) self.reduce_layer0 = SimConv( in_channels=channels_list[4], out_channels=channels_list[5], kernel_size=1, stride=1 ) self.upsample0 = Transpose( in_channels=channels_list[5], out_channels=channels_list[5], ) self.reduce_layer1 = SimConv( in_channels=channels_list[5], out_channels=channels_list[6], kernel_size=1, stride=1 ) self.upsample1 = Transpose( in_channels=channels_list[6], out_channels=channels_list[6] ) self.downsample2 = SimConv( in_channels=channels_list[6], out_channels=channels_list[7], kernel_size=3, stride=2 ) self.downsample1 = SimConv( in_channels=channels_list[8], out_channels=channels_list[9], kernel_size=3, stride=2 ) def upsample_enable_quant(self): print("Insert fakequant after upsample") # Insert fakequant after upsample op to build TensorRT engine from pytorch_quantization import nn as quant_nn from pytorch_quantization.tensor_quant import QuantDescriptor conv2d_input_default_desc = QuantDescriptor(num_bits=8, calib_method='histogram') self.upsample_feat0_quant = quant_nn.TensorQuantizer(conv2d_input_default_desc) self.upsample_feat1_quant = quant_nn.TensorQuantizer(conv2d_input_default_desc) global _QUANT _QUANT = True def forward(self, input): (x2, x1, x0) = input fpn_out0 = self.reduce_layer0(x0) upsample_feat0 = self.upsample0(fpn_out0) if _QUANT: upsample_feat0 = self.upsample_feat0_quant(upsample_feat0) f_concat_layer0 = torch.cat([upsample_feat0, x1], 1) f_out0 = self.Rep_p4(f_concat_layer0) fpn_out1 = self.reduce_layer1(f_out0) upsample_feat1 = self.upsample1(fpn_out1) if _QUANT: upsample_feat1 = self.upsample_feat1_quant(upsample_feat1) f_concat_layer1 = torch.cat([upsample_feat1, x2], 1) pan_out2 = self.Rep_p3(f_concat_layer1) down_feat1 = self.downsample2(pan_out2) p_concat_layer1 = torch.cat([down_feat1, fpn_out1], 1) pan_out1 = self.Rep_n3(p_concat_layer1) down_feat0 = self.downsample1(pan_out1) p_concat_layer2 = torch.cat([down_feat0, fpn_out0], 1) pan_out0 = self.Rep_n4(p_concat_layer2) outputs = [pan_out2, pan_out1, pan_out0] return outputs class CSPRepPANNeck(nn.Module): """ CSPRepPANNeck module. """ def __init__( self, channels_list=None, num_repeats=None, block=BottleRep, csp_e=float(1)/2 ): super().__init__() assert channels_list is not None assert num_repeats is not None self.Rep_p4 = BepC3( in_channels=channels_list[3] + channels_list[5], # 512 + 256 out_channels=channels_list[5], # 256 n=num_repeats[5], e=csp_e, block=block ) self.Rep_p3 = BepC3( in_channels=channels_list[2] + channels_list[6], # 256 + 128 out_channels=channels_list[6], # 128 n=num_repeats[6], e=csp_e, block=block ) self.Rep_n3 = BepC3( in_channels=channels_list[6] + channels_list[7], # 128 + 128 out_channels=channels_list[8], # 256 n=num_repeats[7], e=csp_e, block=block ) self.Rep_n4 = BepC3( in_channels=channels_list[5] + channels_list[9], # 256 + 256 out_channels=channels_list[10], # 512 n=num_repeats[8], e=csp_e, block=block ) self.reduce_layer0 = SimConv( in_channels=channels_list[4], # 1024 out_channels=channels_list[5], # 256 kernel_size=1, stride=1 ) self.upsample0 = Transpose( in_channels=channels_list[5], # 256 out_channels=channels_list[5], # 256 ) self.reduce_layer1 = SimConv( in_channels=channels_list[5], # 256 out_channels=channels_list[6], # 128 kernel_size=1, stride=1 ) self.upsample1 = Transpose( in_channels=channels_list[6], # 128 out_channels=channels_list[6] # 128 ) self.downsample2 = SimConv( in_channels=channels_list[6], # 128 out_channels=channels_list[7], # 128 kernel_size=3, stride=2 ) self.downsample1 = SimConv( in_channels=channels_list[8], # 256 out_channels=channels_list[9], # 256 kernel_size=3, stride=2 ) def forward(self, input): (x2, x1, x0) = input fpn_out0 = self.reduce_layer0(x0) upsample_feat0 = self.upsample0(fpn_out0) f_concat_layer0 = torch.cat([upsample_feat0, x1], 1) f_out0 = self.Rep_p4(f_concat_layer0) fpn_out1 = self.reduce_layer1(f_out0) upsample_feat1 = self.upsample1(fpn_out1) f_concat_layer1 = torch.cat([upsample_feat1, x2], 1) pan_out2 = self.Rep_p3(f_concat_layer1) down_feat1 = self.downsample2(pan_out2) p_concat_layer1 = torch.cat([down_feat1, fpn_out1], 1) pan_out1 = self.Rep_n3(p_concat_layer1) down_feat0 = self.downsample1(pan_out1) p_concat_layer2 = torch.cat([down_feat0, fpn_out0], 1) pan_out0 = self.Rep_n4(p_concat_layer2) outputs = [pan_out2, pan_out1, pan_out0] return outputs ================================================ FILE: asone/detectors/yolov6/yolov6/models/yolo.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- import math import torch import torch.nn as nn import torch.nn.functional as F from asone.detectors.yolov6.yolov6.layers.common import * from asone.detectors.yolov6.yolov6.utils.torch_utils import initialize_weights from asone.detectors.yolov6.yolov6.models.efficientrep import * from asone.detectors.yolov6.yolov6.models.reppan import * from asone.detectors.yolov6.yolov6.models.effidehead import Detect, build_effidehead_layer class Model(nn.Module): '''YOLOv6 model with backbone, neck and head. The default parts are EfficientRep Backbone, Rep-PAN and Efficient Decoupled Head. ''' def __init__(self, config, channels=3, num_classes=None, anchors=None): # model, input channels, number of classes super().__init__() # Build network num_layers = config.model.head.num_layers #self.mode = config.training_mode self.backbone, self.neck, self.detect = build_network(config, channels, num_classes, anchors, num_layers) # Init Detect head begin_indices = config.model.head.begin_indices out_indices_head = config.model.head.out_indices self.stride = self.detect.stride self.detect.i = begin_indices self.detect.f = out_indices_head self.detect.initialize_biases() # Init weights initialize_weights(self) def forward(self, x): export_mode = torch.onnx.is_in_onnx_export() x = self.backbone(x) x = self.neck(x) if export_mode == False: featmaps = [] featmaps.extend(x) x = self.detect(x) return x if export_mode is True else [x, featmaps] def _apply(self, fn): self = super()._apply(fn) self.detect.stride = fn(self.detect.stride) self.detect.grid = list(map(fn, self.detect.grid)) return self def make_divisible(x, divisor): # Upward revision the value x to make it evenly divisible by the divisor. return math.ceil(x / divisor) * divisor def build_network(config, channels, num_classes, anchors, num_layers): depth_mul = config.model.depth_multiple width_mul = config.model.width_multiple num_repeat_backbone = config.model.backbone.num_repeats channels_list_backbone = config.model.backbone.out_channels num_repeat_neck = config.model.neck.num_repeats channels_list_neck = config.model.neck.out_channels num_anchors = config.model.head.anchors use_dfl = config.model.head.use_dfl reg_max = config.model.head.reg_max num_repeat = [(max(round(i * depth_mul), 1) if i > 1 else i) for i in (num_repeat_backbone + num_repeat_neck)] channels_list = [make_divisible(i * width_mul, 8) for i in (channels_list_backbone + channels_list_neck)] block = get_block(config.training_mode) BACKBONE = eval(config.model.backbone.type) NECK = eval(config.model.neck.type) if 'CSP' in config.model.backbone.type: backbone = BACKBONE( in_channels=channels, channels_list=channels_list, num_repeats=num_repeat, block=block, csp_e=config.model.backbone.csp_e ) neck = NECK( channels_list=channels_list, num_repeats=num_repeat, block=block, csp_e=config.model.neck.csp_e ) else: backbone = BACKBONE( in_channels=channels, channels_list=channels_list, num_repeats=num_repeat, block=block ) neck = NECK( channels_list=channels_list, num_repeats=num_repeat, block=block ) head_layers = build_effidehead_layer(channels_list, num_anchors, num_classes, reg_max) head = Detect(num_classes, anchors, num_layers, head_layers=head_layers, use_dfl=use_dfl) return backbone, neck, head def build_model(cfg, num_classes, device): model = Model(cfg, channels=3, num_classes=num_classes, anchors=cfg.model.head.anchors).to(device) return model ================================================ FILE: asone/detectors/yolov6/yolov6/utils/__init__.py ================================================ ================================================ FILE: asone/detectors/yolov6/yolov6/utils/checkpoint.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- import os import shutil import torch import os.path as osp from asone.detectors.yolov6.yolov6.utils.events import LOGGER from asone.detectors.yolov6.yolov6.utils.torch_utils import fuse_model def load_state_dict(weights, model, map_location=None): """Load weights from checkpoint file, only assign weights those layers' name and shape are match.""" ckpt = torch.load(weights, map_location=map_location) state_dict = ckpt['model'].float().state_dict() model_state_dict = model.state_dict() state_dict = {k: v for k, v in state_dict.items() if k in model_state_dict and v.shape == model_state_dict[k].shape} model.load_state_dict(state_dict, strict=False) del ckpt, state_dict, model_state_dict return model def load_checkpoint(weights, map_location=None, inplace=True, fuse=True): """Load model from checkpoint file.""" LOGGER.info("Loading checkpoint from {}".format(weights)) ckpt = torch.load(weights, map_location=map_location) # load model = ckpt['ema' if ckpt.get('ema') else 'model'].float() if fuse: LOGGER.info("\nFusing model...") model = fuse_model(model).eval() else: model = model.eval() return model def save_checkpoint(ckpt, is_best, save_dir, model_name=""): """ Save checkpoint to the disk.""" if not osp.exists(save_dir): os.makedirs(save_dir) filename = osp.join(save_dir, model_name + '.pt') torch.save(ckpt, filename) if is_best: best_filename = osp.join(save_dir, 'best_ckpt.pt') shutil.copyfile(filename, best_filename) def strip_optimizer(ckpt_dir, epoch): """Delete optimizer from saved checkpoint file""" for s in ['best', 'last']: ckpt_path = osp.join(ckpt_dir, '{}_ckpt.pt'.format(s)) if not osp.exists(ckpt_path): continue ckpt = torch.load(ckpt_path, map_location=torch.device('cpu')) if ckpt.get('ema'): ckpt['model'] = ckpt['ema'] # replace model with ema for k in ['optimizer', 'ema', 'updates']: # keys ckpt[k] = None ckpt['epoch'] = epoch ckpt['model'].half() # to FP16 for p in ckpt['model'].parameters(): p.requires_grad = False torch.save(ckpt, ckpt_path) ================================================ FILE: asone/detectors/yolov6/yolov6/utils/events.py ================================================ #!/usr/bin/env python # -*- coding: utf-8 -*- import os import yaml import logging import shutil def set_logging(name=None): rank = int(os.getenv('RANK', -1)) logging.basicConfig(format="%(message)s", level=logging.INFO if (rank in (-1, 0)) else logging.WARNING) return logging.getLogger(name) LOGGER = set_logging(__name__) NCOLS = shutil.get_terminal_size().columns def load_yaml(file_path): """Load data from yaml file.""" if isinstance(file_path, str): with open(file_path, errors='ignore') as f: data_dict = yaml.safe_load(f) return data_dict def save_yaml(data_dict, save_path): """Save data to yaml file""" with open(save_path, 'w') as f: yaml.safe_dump(data_dict, f, sort_keys=False) def write_tblog(tblogger, epoch, results, losses): """Display mAP and loss information to log.""" tblogger.add_scalar("val/mAP@0.5", results[0], epoch + 1) tblogger.add_scalar("val/mAP@0.50:0.95", results[1], epoch + 1) tblogger.add_scalar("train/iou_loss", losses[0], epoch + 1) tblogger.add_scalar("train/l1_loss", losses[1], epoch + 1) tblogger.add_scalar("train/obj_loss", losses[2], epoch + 1) tblogger.add_scalar("train/cls_loss", losses[3], epoch + 1) tblogger.add_scalar("x/lr0", results[2], epoch + 1) tblogger.add_scalar("x/lr1", results[3], epoch + 1) tblogger.add_scalar("x/lr2", results[4], epoch + 1) def write_tbimg(tblogger, imgs, step, type='train'): """Display train_batch and validation predictions to tensorboard.""" if type == 'train': tblogger.add_image(f'train_batch', imgs, step + 1, dataformats='HWC') elif type == 'val': for idx, img in enumerate(imgs): tblogger.add_image(f'val_img_{idx + 1}', img, step + 1, dataformats='HWC') else: LOGGER.warning('WARNING: Unknown image type to visualize.\n') ================================================ FILE: asone/detectors/yolov6/yolov6/utils/figure_iou.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- import math import torch class IOUloss: """ Calculate IoU loss. """ def __init__(self, box_format='xywh', iou_type='ciou', reduction='none', eps=1e-7): """ Setting of the class. Args: box_format: (string), must be one of 'xywh' or 'xyxy'. iou_type: (string), can be one of 'ciou', 'diou', 'giou' or 'siou' reduction: (string), specifies the reduction to apply to the output, must be one of 'none', 'mean','sum'. eps: (float), a value to avoid divide by zero error. """ self.box_format = box_format self.iou_type = iou_type.lower() self.reduction = reduction self.eps = eps def __call__(self, box1, box2): """ calculate iou. box1 and box2 are torch tensor with shape [M, 4] and [Nm 4]. """ if box1.shape[0] != box2.shape[0]: box2 = box2.T if self.box_format == 'xyxy': b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3] b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3] elif self.box_format == 'xywh': b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2 b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2 b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2 b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2 else: if self.box_format == 'xyxy': b1_x1, b1_y1, b1_x2, b1_y2 = torch.split(box1, 1, dim=-1) b2_x1, b2_y1, b2_x2, b2_y2 = torch.split(box2, 1, dim=-1) elif self.box_format == 'xywh': b1_x1, b1_y1, b1_w, b1_h = torch.split(box1, 1, dim=-1) b2_x1, b2_y1, b2_w, b2_h = torch.split(box2, 1, dim=-1) b1_x1, b1_x2 = b1_x1 - b1_w / 2, b1_x1 + b1_w / 2 b1_y1, b1_y2 = b1_y1 - b1_h / 2, b1_y1 + b1_h / 2 b2_x1, b2_x2 = b2_x1 - b2_w / 2, b2_x1 + b2_w / 2 b2_y1, b2_y2 = b2_y1 - b2_h / 2, b2_y1 + b2_h / 2 # Intersection area inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \ (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0) # Union Area w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + self.eps w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + self.eps union = w1 * h1 + w2 * h2 - inter + self.eps iou = inter / union cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex width ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height if self.iou_type == 'giou': c_area = cw * ch + self.eps # convex area iou = iou - (c_area - union) / c_area elif self.iou_type in ['diou', 'ciou']: c2 = cw ** 2 + ch ** 2 + self.eps # convex diagonal squared rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared if self.iou_type == 'diou': iou = iou - rho2 / c2 elif self.iou_type == 'ciou': v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2) with torch.no_grad(): alpha = v / (v - iou + (1 + self.eps)) iou = iou - (rho2 / c2 + v * alpha) elif self.iou_type == 'siou': # SIoU Loss https://arxiv.org/pdf/2205.12740.pdf s_cw = (b2_x1 + b2_x2 - b1_x1 - b1_x2) * 0.5 + self.eps s_ch = (b2_y1 + b2_y2 - b1_y1 - b1_y2) * 0.5 + self.eps sigma = torch.pow(s_cw ** 2 + s_ch ** 2, 0.5) sin_alpha_1 = torch.abs(s_cw) / sigma sin_alpha_2 = torch.abs(s_ch) / sigma threshold = pow(2, 0.5) / 2 sin_alpha = torch.where(sin_alpha_1 > threshold, sin_alpha_2, sin_alpha_1) angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - math.pi / 2) rho_x = (s_cw / cw) ** 2 rho_y = (s_ch / ch) ** 2 gamma = angle_cost - 2 distance_cost = 2 - torch.exp(gamma * rho_x) - torch.exp(gamma * rho_y) omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2) omiga_h = torch.abs(h1 - h2) / torch.max(h1, h2) shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w), 4) + torch.pow(1 - torch.exp(-1 * omiga_h), 4) iou = iou - 0.5 * (distance_cost + shape_cost) loss = 1.0 - iou if self.reduction == 'sum': loss = loss.sum() elif self.reduction == 'mean': loss = loss.mean() return loss def pairwise_bbox_iou(box1, box2, box_format='xywh'): """Calculate iou. This code is based on https://github.com/Megvii-BaseDetection/YOLOX/blob/main/yolox/utils/boxes.py """ if box_format == 'xyxy': lt = torch.max(box1[:, None, :2], box2[:, :2]) rb = torch.min(box1[:, None, 2:], box2[:, 2:]) area_1 = torch.prod(box1[:, 2:] - box1[:, :2], 1) area_2 = torch.prod(box2[:, 2:] - box2[:, :2], 1) elif box_format == 'xywh': lt = torch.max( (box1[:, None, :2] - box1[:, None, 2:] / 2), (box2[:, :2] - box2[:, 2:] / 2), ) rb = torch.min( (box1[:, None, :2] + box1[:, None, 2:] / 2), (box2[:, :2] + box2[:, 2:] / 2), ) area_1 = torch.prod(box1[:, 2:], 1) area_2 = torch.prod(box2[:, 2:], 1) valid = (lt < rb).type(lt.type()).prod(dim=2) inter = torch.prod(rb - lt, 2) * valid return inter / (area_1[:, None] + area_2 - inter) ================================================ FILE: asone/detectors/yolov6/yolov6/utils/general.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- import os import glob import torch from pathlib import Path def increment_name(path): '''increase save directory's id''' path = Path(path) sep = '' if path.exists(): path, suffix = (path.with_suffix(''), path.suffix) if path.is_file() else (path, '') for n in range(1, 9999): p = f'{path}{sep}{n}{suffix}' if not os.path.exists(p): break path = Path(p) return path def find_latest_checkpoint(search_dir='.'): '''Find the most recent saved checkpoint in search_dir.''' checkpoint_list = glob.glob(f'{search_dir}/**/last*.pt', recursive=True) return max(checkpoint_list, key=os.path.getctime) if checkpoint_list else '' def dist2bbox(distance, anchor_points, box_format='xyxy'): '''Transform distance(ltrb) to box(xywh or xyxy).''' lt, rb = torch.split(distance, 2, -1) x1y1 = anchor_points - lt x2y2 = anchor_points + rb if box_format == 'xyxy': bbox = torch.cat([x1y1, x2y2], -1) elif box_format == 'xywh': c_xy = (x1y1 + x2y2) / 2 wh = x2y2 - x1y1 bbox = torch.cat([c_xy, wh], -1) return bbox def bbox2dist(anchor_points, bbox, reg_max): '''Transform bbox(xyxy) to dist(ltrb).''' x1y1, x2y2 = torch.split(bbox, 2, -1) lt = anchor_points - x1y1 rb = x2y2 - anchor_points dist = torch.cat([lt, rb], -1).clip(0, reg_max - 0.01) return dist def xywh2xyxy(bboxes): '''Transform bbox(xywh) to box(xyxy).''' bboxes[..., 0] = bboxes[..., 0] - bboxes[..., 2] * 0.5 bboxes[..., 1] = bboxes[..., 1] - bboxes[..., 3] * 0.5 bboxes[..., 2] = bboxes[..., 0] + bboxes[..., 2] bboxes[..., 3] = bboxes[..., 1] + bboxes[..., 3] return bboxes def box_iou(box1, box2): # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Arguments: box1 (Tensor[N, 4]) box2 (Tensor[M, 4]) Returns: iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 """ def box_area(box): # box = 4xn return (box[2] - box[0]) * (box[3] - box[1]) area1 = box_area(box1.T) area2 = box_area(box2.T) # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2) inter = (torch.min(box1[:, None, 2:], box2[:, 2:]) - torch.max(box1[:, None, :2], box2[:, :2])).clamp(0).prod(2) return inter / (area1[:, None] + area2 - inter) # iou = inter / (area1 + area2 - inter) ================================================ FILE: asone/detectors/yolov6/yolov6/utils/torch_utils.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- import time from contextlib import contextmanager from copy import deepcopy import torch import torch.distributed as dist import torch.nn as nn import torch.nn.functional as F from asone.detectors.yolov6.yolov6.utils.events import LOGGER try: import thop # for FLOPs computation except ImportError: thop = None @contextmanager def torch_distributed_zero_first(local_rank: int): """ Decorator to make all processes in distributed training wait for each local_master to do something. """ if local_rank not in [-1, 0]: dist.barrier(device_ids=[local_rank]) yield if local_rank == 0: dist.barrier(device_ids=[0]) def time_sync(): # Waits for all kernels in all streams on a CUDA device to complete if cuda is available. if torch.cuda.is_available(): torch.cuda.synchronize() return time.time() def initialize_weights(model): for m in model.modules(): t = type(m) if t is nn.Conv2d: pass # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif t is nn.BatchNorm2d: m.eps = 1e-3 m.momentum = 0.03 elif t in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU]: m.inplace = True def fuse_conv_and_bn(conv, bn): # Fuse convolution and batchnorm layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/ fusedconv = ( nn.Conv2d( conv.in_channels, conv.out_channels, kernel_size=conv.kernel_size, stride=conv.stride, padding=conv.padding, groups=conv.groups, bias=True, ) .requires_grad_(False) .to(conv.weight.device) ) # prepare filters w_conv = conv.weight.clone().view(conv.out_channels, -1) w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.shape)) # prepare spatial bias b_conv = ( torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias ) b_bn = bn.bias - bn.weight.mul(bn.running_mean).div( torch.sqrt(bn.running_var + bn.eps) ) fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) return fusedconv def fuse_model(model): from asone.detectors.yolov6.yolov6.layers.common import Conv for m in model.modules(): if type(m) is Conv and hasattr(m, "bn"): m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv delattr(m, "bn") # remove batchnorm m.forward = m.forward_fuse # update forward return model def get_model_info(model, img_size=640): """Get model Params and GFlops. Code base on https://github.com/Megvii-BaseDetection/YOLOX/blob/main/yolox/utils/model_utils.py """ from thop import profile stride = 32 img = torch.zeros((1, 3, stride, stride), device=next(model.parameters()).device) flops, params = profile(deepcopy(model), inputs=(img,), verbose=False) params /= 1e6 flops /= 1e9 img_size = img_size if isinstance(img_size, list) else [img_size, img_size] flops *= img_size[0] * img_size[1] / stride / stride * 2 # Gflops info = "Params: {:.2f}M, Gflops: {:.2f}".format(params, flops) return info ================================================ FILE: asone/detectors/yolov6/yolov6/utils/yolov6_utils.py ================================================ import time import sys import os import numpy as np import cv2 import torch.nn as nn import torch import torchvision from asone.detectors.yolov6.yolov6.layers.common import Conv def nms(boxes, scores, iou_threshold): # Sort by score sorted_indices = np.argsort(scores)[::-1] keep_boxes = [] while sorted_indices.size > 0: # Pick the last box box_id = sorted_indices[0] keep_boxes.append(box_id) # Compute IoU of the picked box with the rest ious = compute_iou(boxes[box_id, :], boxes[sorted_indices[1:], :]) # Remove boxes with IoU over the threshold keep_indices = np.where(ious < iou_threshold)[0] # print(keep_indices.shape, sorted_indices.shape) sorted_indices = sorted_indices[keep_indices + 1] return keep_boxes def process_output(output, img_height, img_width, input_width, input_height, conf_thres, iou_thres): predictions = np.squeeze(output) # Filter out object confidence scores below threshold obj_conf = predictions[:, 4] predictions = predictions[obj_conf > conf_thres] obj_conf = obj_conf[obj_conf > conf_thres] # Multiply class confidence with bounding box confidence predictions[:, 5:] *= obj_conf[:, np.newaxis] # Get the scores scores = np.max(predictions[:, 5:], axis=1) # Filter out the objects with a low score predictions = predictions[obj_conf > conf_thres] scores = scores[scores > conf_thres] # Get the class with the highest confidence class_ids = np.argmax(predictions[:, 5:], axis=1) # Get bounding boxes for each object boxes = process_and_scale_boxes(predictions, img_height, img_width, input_width, input_height) # Apply non-maxima suppression to suppress weak, overlapping bounding boxes indices = nms(boxes, scores, iou_thres) return boxes[indices], scores[indices], class_ids[indices] def compute_iou(box, boxes): # Compute xmin, ymin, xmax, ymax for both boxes xmin = np.maximum(box[0], boxes[:, 0]) ymin = np.maximum(box[1], boxes[:, 1]) xmax = np.minimum(box[2], boxes[:, 2]) ymax = np.minimum(box[3], boxes[:, 3]) # Compute intersection area intersection_area = np.maximum(0, xmax - xmin) * np.maximum(0, ymax - ymin) # Compute union area box_area = (box[2] - box[0]) * (box[3] - box[1]) boxes_area = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) union_area = box_area + boxes_area - intersection_area # Compute IoU iou = intersection_area / union_area return iou def xywh2xyxy(x): # Convert bounding box (x, y, w, h) to bounding box (x1, y1, x2, y2) y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = x[..., 0] - x[..., 2] / 2 y[..., 1] = x[..., 1] - x[..., 3] / 2 y[..., 2] = x[..., 0] + x[..., 2] / 2 y[..., 3] = x[..., 1] + x[..., 3] / 2 return y def prepare_input(image, input_width, input_height): input_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Resize input image input_img = cv2.resize(input_img, (input_width, input_height)) # Scale input pixel values to 0 to 1 input_img = input_img / 255.0 input_img = input_img.transpose(2, 0, 1) input_tensor = input_img[np.newaxis, :, :, :].astype(np.float32) return input_tensor def process_and_scale_boxes(predictions, img_height, img_width, input_width, input_height): predictions = np.delete(predictions, 0, axis=1) # Extract boxes from predictions boxes = predictions[:, :4] # Scale boxes to original image dimensions boxes /= np.array([input_width, input_height, input_width, input_height]) boxes *= np.array([img_width, img_height, img_width, img_height]) # Convert boxes to xyxy format # boxes = xywh2xyxy(boxes) boxes = boxes[:,:4] class_ids = predictions[:,4:5] scores = predictions[:,5:] return boxes, scores, class_ids def load_pytorch(weights, map_location=None, inplace=True, fuse=False): """Load model from checkpoint file.""" ckpt = torch.load(weights, map_location=map_location) # load model = ckpt['ema' if ckpt.get('ema') else 'model'].float() if fuse: model = fuse_model(model).eval() else: model = model.eval() return model def fuse_model(model): for m in model.modules(): if type(m) is Conv and hasattr(m, "bn"): m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv delattr(m, "bn") # remove batchnorm m.forward = m.forward_fuse # update forward return model def fuse_conv_and_bn(conv, bn): # Fuse convolution and batchnorm layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/ fusedconv = ( nn.Conv2d( conv.in_channels, conv.out_channels, kernel_size=conv.kernel_size, stride=conv.stride, padding=conv.padding, groups=conv.groups, bias=True, ) .requires_grad_(False) .to(conv.weight.device) ) # prepare filters w_conv = conv.weight.clone().view(conv.out_channels, -1) w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.shape)) # prepare spatial bias b_conv = ( torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias ) b_bn = bn.bias - bn.weight.mul(bn.running_mean).div( torch.sqrt(bn.running_var + bn.eps) ) fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) return fusedconv def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False, max_det=300): """Runs Non-Maximum Suppression (NMS) on inference results. This code is borrowed from: https://github.com/ultralytics/yolov5/blob/47233e1698b89fc437a4fb9463c815e9171be955/utils/general.py#L775 Args: prediction: (tensor), with shape [N, 5 + num_classes], N is the number of bboxes. conf_thres: (float) confidence threshold. iou_thres: (float) iou threshold. classes: (None or list[int]), if a list is provided, nms only keep the classes you provide. agnostic: (bool), when it is set to True, we do class-independent nms, otherwise, different class would do nms respectively. multi_label: (bool), when it is set to True, one box can have multi labels, otherwise, one box only huave one label. max_det:(int), max number of output bboxes. Returns: list of detections, echo item is one tensor with shape (num_boxes, 6), 6 is for [xyxy, conf, cls]. """ num_classes = prediction.shape[2] - 5 # number of classes pred_candidates = prediction[..., 4] > conf_thres # candidates # Check the parameters. assert 0 <= conf_thres <= 1, f'conf_thresh must be in 0.0 to 1.0, however {conf_thres} is provided.' assert 0 <= iou_thres <= 1, f'iou_thres must be in 0.0 to 1.0, however {iou_thres} is provided.' # Function settings. max_wh = 4096 # maximum box width and height max_nms = 30000 # maximum number of boxes put into torchvision.ops.nms() time_limit = 10.0 # quit the function when nms cost time exceed the limit time. multi_label &= num_classes > 1 # multiple labels per box tik = time.time() output = [torch.zeros((0, 6), device=prediction.device)] * prediction.shape[0] for img_idx, x in enumerate(prediction): # image index, image inference x = x[pred_candidates[img_idx]] # confidence # If no box remains, skip the next process. if not x.shape[0]: continue # confidence multiply the objectness x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf # (center x, center y, width, height) to (x1, y1, x2, y2) box = xywh2xyxy(x[:, :4]) # Detections matrix's shape is (n,6), each row represents (xyxy, conf, cls) if multi_label: box_idx, class_idx = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T x = torch.cat((box[box_idx], x[box_idx, class_idx + 5, None], class_idx[:, None].float()), 1) else: # Only keep the class with highest scores. conf, class_idx = x[:, 5:].max(1, keepdim=True) x = torch.cat((box, conf, class_idx.float()), 1)[conf.view(-1) > conf_thres] # Filter by class, only keep boxes whose category is in classes. if classes is not None: x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)] # Check shape num_box = x.shape[0] # number of boxes if not num_box: # no boxes kept. continue elif num_box > max_nms: # excess max boxes' number. x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence # Batched NMS class_offset = x[:, 5:6] * (0 if agnostic else max_wh) # classes boxes, scores = x[:, :4] + class_offset, x[:, 4] # boxes (offset by class), scores keep_box_idx = torchvision.ops.nms(boxes, scores, iou_thres) # NMS if keep_box_idx.shape[0] > max_det: # limit detections keep_box_idx = keep_box_idx[:max_det] output[img_idx] = x[keep_box_idx] if (time.time() - tik) > time_limit: print(f'WARNING: NMS cost time exceed the limited {time_limit}s.') break # time limit exceeded return output ================================================ FILE: asone/detectors/yolov6/yolov6_detector.py ================================================ import os import sys from asone.utils import get_names import numpy as np import warnings import torch import onnxruntime from asone import utils from asone.detectors.yolov6.yolov6.utils.yolov6_utils import (prepare_input, load_pytorch, non_max_suppression, process_and_scale_boxes) # sys.path.append(os.path.dirname(__file__)) from asone.utils.utils import PathResolver class YOLOv6Detector: def __init__(self, weights=None, use_onnx=False, use_cuda=True): self.use_onnx = use_onnx self.device = 'cuda' if use_cuda else 'cpu' if not os.path.exists(weights): utils.download_weights(weights) #If incase weighst is a list of paths then select path at first index weights = str(weights[0] if isinstance(weights, list) else weights) with PathResolver(): # Load Model self.model = self.load_model(use_cuda, weights) if use_onnx: # Get Some ONNX model details self.input_shape, self.input_height, self.input_width = self.ONNXModel_detail(self.model) self.input_names, self.output_names = self.ONNXModel_names(self.model) def load_model(self, use_cuda, weights, fp16=False): # Device: CUDA and if fp16=True only then half precision floating point works self.fp16 = fp16 & ((not self.use_onnx or self.use_onnx) and self.device != 'cpu') # Load onnx if self.use_onnx: if use_cuda: providers = ['CUDAExecutionProvider','CPUExecutionProvider'] else: providers = ['CPUExecutionProvider'] model = onnxruntime.InferenceSession(weights, providers=providers) #Load Pytorch else: model = load_pytorch(weights, map_location=self.device) model.half() if self.fp16 else model.float() return model def ONNXModel_detail(self, model): # Get Model Input model_inputs = model.get_inputs() # Input shape input_shape = model_inputs[0].shape input_height = input_shape[2] input_width = input_shape[3] return input_shape, input_height, input_width def ONNXModel_names(self, model): # Get Model Input model_inputs = model.get_inputs() input_names = [model_inputs[i].name for i in range(len(model_inputs))] # Get Model Output model_outputs = model.get_outputs() output_names = [model_outputs[i].name for i in range(len(model_outputs))] return input_names, output_names def detect(self, image: list, input_shape: tuple = (640, 640), conf_thres: float = 0.25, iou_thres: float = 0.45, max_det: int = 1000, filter_classes: bool = None, agnostic_nms: bool = True, with_p6: bool = False, return_image=False) -> list: # Prepare Input img_height, img_width = image.shape[:2] processed_image = prepare_input(image, input_shape[0], input_shape[1]) # Perform Inference on the Image if self.use_onnx: # Run ONNX model prediction = self.model.run(self.output_names, {self.input_names[0]: processed_image})[0] # Run Pytorch model else: processed_image = torch.from_numpy(processed_image).to(self.device) # Change image floating point precision if fp16 set to true processed_image = processed_image.half() if self.fp16 else processed_image.float() prediction = self.model(processed_image)[0] # Post Procesing, non-max-suppression and rescaling if self.use_onnx: # Process ONNX Output boxes, scores, class_ids = process_and_scale_boxes(prediction, img_height, img_width, input_shape[1], input_shape[0]) detection = [] for box in range(len(boxes)): pred = np.append(boxes[box], scores[box]) pred = np.append(pred, class_ids[box]) detection.append(pred) detection = np.array(detection) else: detection = non_max_suppression(prediction, conf_thres, iou_thres, agnostic=agnostic_nms, max_det=max_det)[0] detection = detection.detach().cpu().numpy() detection[:, :4] /= np.array([input_shape[1], input_shape[0], input_shape[1], input_shape[0]]) detection[:, :4] *= np.array([img_width, img_height, img_width, img_height]) if filter_classes: class_names = get_names() filter_class_idx = [] if filter_classes: for _class in filter_classes: if _class.lower() in class_names: filter_class_idx.append(class_names.index(_class.lower())) else: warnings.warn(f"class {_class} not found in model classes list.") detection = detection[np.in1d(detection[:,5].astype(int), filter_class_idx)] image_info = { 'width': image.shape[1], 'height': image.shape[0], } if return_image: return detection, image else: return detection, image_info ================================================ FILE: asone/detectors/yolov7/__init__.py ================================================ from .yolov7_detector import YOLOv7Detector __all__ = ['YOLOv7Detector'] ================================================ FILE: asone/detectors/yolov7/yolov7/__init__.py ================================================ ================================================ FILE: asone/detectors/yolov7/yolov7/models/__init__.py ================================================ ================================================ FILE: asone/detectors/yolov7/yolov7/models/common.py ================================================ import math from copy import copy from pathlib import Path import numpy as np import pandas as pd import requests import torch import torch.nn as nn import torch.nn.functional as F from PIL import Image from torch.cuda import amp from asone.detectors.yolov7.yolov7.utils.torch_utils import time_synchronized ##### basic #### def autopad(k, p=None): # kernel, padding # Pad to 'same' if p is None: p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad return p class MP(nn.Module): def __init__(self, k=2): super(MP, self).__init__() self.m = nn.MaxPool2d(kernel_size=k, stride=k) def forward(self, x): return self.m(x) class SP(nn.Module): def __init__(self, k=3, s=1): super(SP, self).__init__() self.m = nn.MaxPool2d(kernel_size=k, stride=s, padding=k // 2) def forward(self, x): return self.m(x) class ReOrg(nn.Module): def __init__(self): super(ReOrg, self).__init__() def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2) return torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1) class Concat(nn.Module): def __init__(self, dimension=1): super(Concat, self).__init__() self.d = dimension def forward(self, x): return torch.cat(x, self.d) class Chuncat(nn.Module): def __init__(self, dimension=1): super(Chuncat, self).__init__() self.d = dimension def forward(self, x): x1 = [] x2 = [] for xi in x: xi1, xi2 = xi.chunk(2, self.d) x1.append(xi1) x2.append(xi2) return torch.cat(x1+x2, self.d) class Shortcut(nn.Module): def __init__(self, dimension=0): super(Shortcut, self).__init__() self.d = dimension def forward(self, x): return x[0]+x[1] class Foldcut(nn.Module): def __init__(self, dimension=0): super(Foldcut, self).__init__() self.d = dimension def forward(self, x): x1, x2 = x.chunk(2, self.d) return x1+x2 class Conv(nn.Module): # Standard convolution def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super(Conv, self).__init__() self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) self.bn = nn.BatchNorm2d(c2) self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) def forward(self, x): return self.act(self.bn(self.conv(x))) def fuseforward(self, x): return self.act(self.conv(x)) class RobustConv(nn.Module): # Robust convolution (use high kernel size 7-11 for: downsampling and other layers). Train for 300 - 450 epochs. def __init__(self, c1, c2, k=7, s=1, p=None, g=1, act=True, layer_scale_init_value=1e-6): # ch_in, ch_out, kernel, stride, padding, groups super(RobustConv, self).__init__() self.conv_dw = Conv(c1, c1, k=k, s=s, p=p, g=c1, act=act) self.conv1x1 = nn.Conv2d(c1, c2, 1, 1, 0, groups=1, bias=True) self.gamma = nn.Parameter(layer_scale_init_value * torch.ones(c2)) if layer_scale_init_value > 0 else None def forward(self, x): x = x.to(memory_format=torch.channels_last) x = self.conv1x1(self.conv_dw(x)) if self.gamma is not None: x = x.mul(self.gamma.reshape(1, -1, 1, 1)) return x class RobustConv2(nn.Module): # Robust convolution 2 (use [32, 5, 2] or [32, 7, 4] or [32, 11, 8] for one of the paths in CSP). def __init__(self, c1, c2, k=7, s=4, p=None, g=1, act=True, layer_scale_init_value=1e-6): # ch_in, ch_out, kernel, stride, padding, groups super(RobustConv2, self).__init__() self.conv_strided = Conv(c1, c1, k=k, s=s, p=p, g=c1, act=act) self.conv_deconv = nn.ConvTranspose2d(in_channels=c1, out_channels=c2, kernel_size=s, stride=s, padding=0, bias=True, dilation=1, groups=1 ) self.gamma = nn.Parameter(layer_scale_init_value * torch.ones(c2)) if layer_scale_init_value > 0 else None def forward(self, x): x = self.conv_deconv(self.conv_strided(x)) if self.gamma is not None: x = x.mul(self.gamma.reshape(1, -1, 1, 1)) return x def DWConv(c1, c2, k=1, s=1, act=True): # Depthwise convolution return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act) class GhostConv(nn.Module): # Ghost Convolution https://github.com/huawei-noah/ghostnet def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups super(GhostConv, self).__init__() c_ = c2 // 2 # hidden channels self.cv1 = Conv(c1, c_, k, s, None, g, act) self.cv2 = Conv(c_, c_, 5, 1, None, c_, act) def forward(self, x): y = self.cv1(x) return torch.cat([y, self.cv2(y)], 1) class Stem(nn.Module): # Stem def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super(Stem, self).__init__() c_ = int(c2/2) # hidden channels self.cv1 = Conv(c1, c_, 3, 2) self.cv2 = Conv(c_, c_, 1, 1) self.cv3 = Conv(c_, c_, 3, 2) self.pool = torch.nn.MaxPool2d(2, stride=2) self.cv4 = Conv(2 * c_, c2, 1, 1) def forward(self, x): x = self.cv1(x) return self.cv4(torch.cat((self.cv3(self.cv2(x)), self.pool(x)), dim=1)) class DownC(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, n=1, k=2): super(DownC, self).__init__() c_ = int(c1) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c2//2, 3, k) self.cv3 = Conv(c1, c2//2, 1, 1) self.mp = nn.MaxPool2d(kernel_size=k, stride=k) def forward(self, x): return torch.cat((self.cv2(self.cv1(x)), self.cv3(self.mp(x))), dim=1) class SPP(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, k=(5, 9, 13)): super(SPP, self).__init__() c_ = c1 // 2 # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1) self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) def forward(self, x): x = self.cv1(x) return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1)) class Bottleneck(nn.Module): # Darknet bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super(Bottleneck, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c2, 3, 1, g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class Res(nn.Module): # ResNet bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super(Res, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c_, 3, 1, g=g) self.cv3 = Conv(c_, c2, 1, 1) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv3(self.cv2(self.cv1(x))) if self.add else self.cv3(self.cv2(self.cv1(x))) class ResX(Res): # ResNet bottleneck def __init__(self, c1, c2, shortcut=True, g=32, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super().__init__(c1, c2, shortcut, g, e) c_ = int(c2 * e) # hidden channels class Ghost(nn.Module): # Ghost Bottleneck https://github.com/huawei-noah/ghostnet def __init__(self, c1, c2, k=3, s=1): # ch_in, ch_out, kernel, stride super(Ghost, self).__init__() c_ = c2 // 2 self.conv = nn.Sequential(GhostConv(c1, c_, 1, 1), # pw DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw GhostConv(c_, c2, 1, 1, act=False)) # pw-linear self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False), Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity() def forward(self, x): return self.conv(x) + self.shortcut(x) ##### end of basic ##### ##### cspnet ##### class SPPCSPC(nn.Module): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)): super(SPPCSPC, self).__init__() c_ = int(2 * c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(c_, c_, 3, 1) self.cv4 = Conv(c_, c_, 1, 1) self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) self.cv5 = Conv(4 * c_, c_, 1, 1) self.cv6 = Conv(c_, c_, 3, 1) self.cv7 = Conv(2 * c_, c2, 1, 1) def forward(self, x): x1 = self.cv4(self.cv3(self.cv1(x))) y1 = self.cv6(self.cv5(torch.cat([x1] + [m(x1) for m in self.m], 1))) y2 = self.cv2(x) return self.cv7(torch.cat((y1, y2), dim=1)) class GhostSPPCSPC(SPPCSPC): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)): super().__init__(c1, c2, n, shortcut, g, e, k) c_ = int(2 * c2 * e) # hidden channels self.cv1 = GhostConv(c1, c_, 1, 1) self.cv2 = GhostConv(c1, c_, 1, 1) self.cv3 = GhostConv(c_, c_, 3, 1) self.cv4 = GhostConv(c_, c_, 1, 1) self.cv5 = GhostConv(4 * c_, c_, 1, 1) self.cv6 = GhostConv(c_, c_, 3, 1) self.cv7 = GhostConv(2 * c_, c2, 1, 1) class GhostStem(Stem): # Stem def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super().__init__(c1, c2, k, s, p, g, act) c_ = int(c2/2) # hidden channels self.cv1 = GhostConv(c1, c_, 3, 2) self.cv2 = GhostConv(c_, c_, 1, 1) self.cv3 = GhostConv(c_, c_, 3, 2) self.cv4 = GhostConv(2 * c_, c2, 1, 1) class BottleneckCSPA(nn.Module): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPA, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.m(self.cv1(x)) y2 = self.cv2(x) return self.cv3(torch.cat((y1, y2), dim=1)) class BottleneckCSPB(nn.Module): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPB, self).__init__() c_ = int(c2) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x1 = self.cv1(x) y1 = self.m(x1) y2 = self.cv2(x1) return self.cv3(torch.cat((y1, y2), dim=1)) class BottleneckCSPC(nn.Module): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPC, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(c_, c_, 1, 1) self.cv4 = Conv(2 * c_, c2, 1, 1) self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(torch.cat((y1, y2), dim=1)) class ResCSPA(BottleneckCSPA): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class ResCSPB(BottleneckCSPB): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class ResCSPC(BottleneckCSPC): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class ResXCSPA(ResCSPA): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class ResXCSPB(ResCSPB): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class ResXCSPC(ResCSPC): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class GhostCSPA(BottleneckCSPA): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Ghost(c_, c_) for _ in range(n)]) class GhostCSPB(BottleneckCSPB): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[Ghost(c_, c_) for _ in range(n)]) class GhostCSPC(BottleneckCSPC): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Ghost(c_, c_) for _ in range(n)]) ##### end of cspnet ##### ##### yolor ##### class ImplicitA(nn.Module): def __init__(self, channel, mean=0., std=.02): super(ImplicitA, self).__init__() self.channel = channel self.mean = mean self.std = std self.implicit = nn.Parameter(torch.zeros(1, channel, 1, 1)) nn.init.normal_(self.implicit, mean=self.mean, std=self.std) def forward(self, x): return self.implicit + x class ImplicitM(nn.Module): def __init__(self, channel, mean=0., std=.02): super(ImplicitM, self).__init__() self.channel = channel self.mean = mean self.std = std self.implicit = nn.Parameter(torch.ones(1, channel, 1, 1)) nn.init.normal_(self.implicit, mean=self.mean, std=self.std) def forward(self, x): return self.implicit * x ##### end of yolor ##### ##### repvgg ##### class RepConv(nn.Module): # Represented convolution # https://arxiv.org/abs/2101.03697 def __init__(self, c1, c2, k=3, s=1, p=None, g=1, act=True, deploy=False): super(RepConv, self).__init__() self.deploy = deploy self.groups = g self.in_channels = c1 self.out_channels = c2 assert k == 3 assert autopad(k, p) == 1 padding_11 = autopad(k, p) - k // 2 self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) if deploy: self.rbr_reparam = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=True) else: self.rbr_identity = (nn.BatchNorm2d(num_features=c1) if c2 == c1 and s == 1 else None) self.rbr_dense = nn.Sequential( nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False), nn.BatchNorm2d(num_features=c2), ) self.rbr_1x1 = nn.Sequential( nn.Conv2d( c1, c2, 1, s, padding_11, groups=g, bias=False), nn.BatchNorm2d(num_features=c2), ) def forward(self, inputs): if hasattr(self, "rbr_reparam"): return self.act(self.rbr_reparam(inputs)) if self.rbr_identity is None: id_out = 0 else: id_out = self.rbr_identity(inputs) return self.act(self.rbr_dense(inputs) + self.rbr_1x1(inputs) + id_out) def get_equivalent_kernel_bias(self): kernel3x3, bias3x3 = self._fuse_bn_tensor(self.rbr_dense) kernel1x1, bias1x1 = self._fuse_bn_tensor(self.rbr_1x1) kernelid, biasid = self._fuse_bn_tensor(self.rbr_identity) return ( kernel3x3 + self._pad_1x1_to_3x3_tensor(kernel1x1) + kernelid, bias3x3 + bias1x1 + biasid, ) def _pad_1x1_to_3x3_tensor(self, kernel1x1): if kernel1x1 is None: return 0 else: return nn.functional.pad(kernel1x1, [1, 1, 1, 1]) def _fuse_bn_tensor(self, branch): if branch is None: return 0, 0 if isinstance(branch, nn.Sequential): kernel = branch[0].weight running_mean = branch[1].running_mean running_var = branch[1].running_var gamma = branch[1].weight beta = branch[1].bias eps = branch[1].eps else: assert isinstance(branch, nn.BatchNorm2d) if not hasattr(self, "id_tensor"): input_dim = self.in_channels // self.groups kernel_value = np.zeros( (self.in_channels, input_dim, 3, 3), dtype=np.float32 ) for i in range(self.in_channels): kernel_value[i, i % input_dim, 1, 1] = 1 self.id_tensor = torch.from_numpy(kernel_value).to(branch.weight.device) kernel = self.id_tensor running_mean = branch.running_mean running_var = branch.running_var gamma = branch.weight beta = branch.bias eps = branch.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta - running_mean * gamma / std def repvgg_convert(self): kernel, bias = self.get_equivalent_kernel_bias() return ( kernel.detach().cpu().numpy(), bias.detach().cpu().numpy(), ) def fuse_conv_bn(self, conv, bn): std = (bn.running_var + bn.eps).sqrt() bias = bn.bias - bn.running_mean * bn.weight / std t = (bn.weight / std).reshape(-1, 1, 1, 1) weights = conv.weight * t bn = nn.Identity() conv = nn.Conv2d(in_channels = conv.in_channels, out_channels = conv.out_channels, kernel_size = conv.kernel_size, stride=conv.stride, padding = conv.padding, dilation = conv.dilation, groups = conv.groups, bias = True, padding_mode = conv.padding_mode) conv.weight = torch.nn.Parameter(weights) conv.bias = torch.nn.Parameter(bias) return conv def fuse_repvgg_block(self): if self.deploy: return print(f"RepConv.fuse_repvgg_block") self.rbr_dense = self.fuse_conv_bn(self.rbr_dense[0], self.rbr_dense[1]) self.rbr_1x1 = self.fuse_conv_bn(self.rbr_1x1[0], self.rbr_1x1[1]) rbr_1x1_bias = self.rbr_1x1.bias weight_1x1_expanded = torch.nn.functional.pad(self.rbr_1x1.weight, [1, 1, 1, 1]) # Fuse self.rbr_identity if (isinstance(self.rbr_identity, nn.BatchNorm2d) or isinstance(self.rbr_identity, nn.modules.batchnorm.SyncBatchNorm)): # print(f"fuse: rbr_identity == BatchNorm2d or SyncBatchNorm") identity_conv_1x1 = nn.Conv2d( in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=1, stride=1, padding=0, groups=self.groups, bias=False) identity_conv_1x1.weight.data = identity_conv_1x1.weight.data.to(self.rbr_1x1.weight.data.device) identity_conv_1x1.weight.data = identity_conv_1x1.weight.data.squeeze().squeeze() # print(f" identity_conv_1x1.weight = {identity_conv_1x1.weight.shape}") identity_conv_1x1.weight.data.fill_(0.0) identity_conv_1x1.weight.data.fill_diagonal_(1.0) identity_conv_1x1.weight.data = identity_conv_1x1.weight.data.unsqueeze(2).unsqueeze(3) # print(f" identity_conv_1x1.weight = {identity_conv_1x1.weight.shape}") identity_conv_1x1 = self.fuse_conv_bn(identity_conv_1x1, self.rbr_identity) bias_identity_expanded = identity_conv_1x1.bias weight_identity_expanded = torch.nn.functional.pad(identity_conv_1x1.weight, [1, 1, 1, 1]) else: # print(f"fuse: rbr_identity != BatchNorm2d, rbr_identity = {self.rbr_identity}") bias_identity_expanded = torch.nn.Parameter( torch.zeros_like(rbr_1x1_bias) ) weight_identity_expanded = torch.nn.Parameter( torch.zeros_like(weight_1x1_expanded) ) #print(f"self.rbr_1x1.weight = {self.rbr_1x1.weight.shape}, ") #print(f"weight_1x1_expanded = {weight_1x1_expanded.shape}, ") #print(f"self.rbr_dense.weight = {self.rbr_dense.weight.shape}, ") self.rbr_dense.weight = torch.nn.Parameter(self.rbr_dense.weight + weight_1x1_expanded + weight_identity_expanded) self.rbr_dense.bias = torch.nn.Parameter(self.rbr_dense.bias + rbr_1x1_bias + bias_identity_expanded) self.rbr_reparam = self.rbr_dense self.deploy = True if self.rbr_identity is not None: del self.rbr_identity self.rbr_identity = None if self.rbr_1x1 is not None: del self.rbr_1x1 self.rbr_1x1 = None if self.rbr_dense is not None: del self.rbr_dense self.rbr_dense = None class RepBottleneck(Bottleneck): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super().__init__(c1, c2, shortcut=True, g=1, e=0.5) c_ = int(c2 * e) # hidden channels self.cv2 = RepConv(c_, c2, 3, 1, g=g) class RepBottleneckCSPA(BottleneckCSPA): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepBottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class RepBottleneckCSPB(BottleneckCSPB): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[RepBottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class RepBottleneckCSPC(BottleneckCSPC): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepBottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class RepRes(Res): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super().__init__(c1, c2, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.cv2 = RepConv(c_, c_, 3, 1, g=g) class RepResCSPA(ResCSPA): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepRes(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class RepResCSPB(ResCSPB): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[RepRes(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class RepResCSPC(ResCSPC): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepRes(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class RepResX(ResX): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=32, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super().__init__(c1, c2, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.cv2 = RepConv(c_, c_, 3, 1, g=g) class RepResXCSPA(ResXCSPA): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepResX(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class RepResXCSPB(ResXCSPB): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[RepResX(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class RepResXCSPC(ResXCSPC): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepResX(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) ##### end of repvgg ##### ##### transformer ##### class TransformerLayer(nn.Module): # Transformer layer https://arxiv.org/abs/2010.11929 (LayerNorm layers removed for better performance) def __init__(self, c, num_heads): super().__init__() self.q = nn.Linear(c, c, bias=False) self.k = nn.Linear(c, c, bias=False) self.v = nn.Linear(c, c, bias=False) self.ma = nn.MultiheadAttention(embed_dim=c, num_heads=num_heads) self.fc1 = nn.Linear(c, c, bias=False) self.fc2 = nn.Linear(c, c, bias=False) def forward(self, x): x = self.ma(self.q(x), self.k(x), self.v(x))[0] + x x = self.fc2(self.fc1(x)) + x return x class TransformerBlock(nn.Module): # Vision Transformer https://arxiv.org/abs/2010.11929 def __init__(self, c1, c2, num_heads, num_layers): super().__init__() self.conv = None if c1 != c2: self.conv = Conv(c1, c2) self.linear = nn.Linear(c2, c2) # learnable position embedding self.tr = nn.Sequential(*[TransformerLayer(c2, num_heads) for _ in range(num_layers)]) self.c2 = c2 def forward(self, x): if self.conv is not None: x = self.conv(x) b, _, w, h = x.shape p = x.flatten(2) p = p.unsqueeze(0) p = p.transpose(0, 3) p = p.squeeze(3) e = self.linear(p) x = p + e x = self.tr(x) x = x.unsqueeze(3) x = x.transpose(0, 3) x = x.reshape(b, self.c2, w, h) return x ##### end of transformer ##### ##### yolov5 ##### class Focus(nn.Module): # Focus wh information into c-space def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super(Focus, self).__init__() self.conv = Conv(c1 * 4, c2, k, s, p, g, act) # self.contract = Contract(gain=2) def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2) return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)) # return self.conv(self.contract(x)) class SPPF(nn.Module): # Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13)) super().__init__() c_ = c1 // 2 # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_ * 4, c2, 1, 1) self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2) def forward(self, x): x = self.cv1(x) y1 = self.m(x) y2 = self.m(y1) return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1)) class Contract(nn.Module): # Contract width-height into channels, i.e. x(1,64,80,80) to x(1,256,40,40) def __init__(self, gain=2): super().__init__() self.gain = gain def forward(self, x): N, C, H, W = x.size() # assert (H / s == 0) and (W / s == 0), 'Indivisible gain' s = self.gain x = x.view(N, C, H // s, s, W // s, s) # x(1,64,40,2,40,2) x = x.permute(0, 3, 5, 1, 2, 4).contiguous() # x(1,2,2,64,40,40) return x.view(N, C * s * s, H // s, W // s) # x(1,256,40,40) class Expand(nn.Module): # Expand channels into width-height, i.e. x(1,64,80,80) to x(1,16,160,160) def __init__(self, gain=2): super().__init__() self.gain = gain def forward(self, x): N, C, H, W = x.size() # assert C / s ** 2 == 0, 'Indivisible gain' s = self.gain x = x.view(N, s, s, C // s ** 2, H, W) # x(1,2,2,16,80,80) x = x.permute(0, 3, 4, 1, 5, 2).contiguous() # x(1,16,80,2,80,2) return x.view(N, C // s ** 2, H * s, W * s) # x(1,16,160,160) class NMS(nn.Module): # Non-Maximum Suppression (NMS) module conf = 0.25 # confidence threshold iou = 0.45 # IoU threshold classes = None # (optional list) filter by class def __init__(self): super(NMS, self).__init__() def forward(self, x): return non_max_suppression(x[0], conf_thres=self.conf, iou_thres=self.iou, classes=self.classes) class autoShape(nn.Module): # input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS conf = 0.25 # NMS confidence threshold iou = 0.45 # NMS IoU threshold classes = None # (optional list) filter by class def __init__(self, model): super(autoShape, self).__init__() self.model = model.eval() def autoshape(self): print('autoShape already enabled, skipping... ') # model already converted to model.autoshape() return self @torch.no_grad() def forward(self, imgs, size=640, augment=False, profile=False): # Inference from various sources. For height=640, width=1280, RGB images example inputs are: # filename: imgs = 'data/samples/zidane.jpg' # URI: = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/zidane.jpg' # OpenCV: = cv2.imread('image.jpg')[:,:,::-1] # HWC BGR to RGB x(640,1280,3) # PIL: = Image.open('image.jpg') # HWC x(640,1280,3) # numpy: = np.zeros((640,1280,3)) # HWC # torch: = torch.zeros(16,3,320,640) # BCHW (scaled to size=640, 0-1 values) # multiple: = [Image.open('image1.jpg'), Image.open('image2.jpg'), ...] # list of images t = [time_synchronized()] p = next(self.model.parameters()) # for device and type if isinstance(imgs, torch.Tensor): # torch with amp.autocast(enabled=p.device.type != 'cpu'): return self.model(imgs.to(p.device).type_as(p), augment, profile) # inference # Pre-process n, imgs = (len(imgs), imgs) if isinstance(imgs, list) else (1, [imgs]) # number of images, list of images shape0, shape1, files = [], [], [] # image and inference shapes, filenames for i, im in enumerate(imgs): f = f'image{i}' # filename if isinstance(im, str): # filename or uri im, f = np.asarray(Image.open(requests.get(im, stream=True).raw if im.startswith('http') else im)), im elif isinstance(im, Image.Image): # PIL Image im, f = np.asarray(im), getattr(im, 'filename', f) or f files.append(Path(f).with_suffix('.jpg').name) if im.shape[0] < 5: # image in CHW im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1) im = im[:, :, :3] if im.ndim == 3 else np.tile(im[:, :, None], 3) # enforce 3ch input s = im.shape[:2] # HWC shape0.append(s) # image shape g = (size / max(s)) # gain shape1.append([y * g for y in s]) imgs[i] = im # update shape1 = [make_divisible(x, int(self.stride.max())) for x in np.stack(shape1, 0).max(0)] # inference shape x = [letterbox(im, new_shape=shape1, auto=False)[0] for im in imgs] # pad x = np.stack(x, 0) if n > 1 else x[0][None] # stack x = np.ascontiguousarray(x.transpose((0, 3, 1, 2))) # BHWC to BCHW x = torch.from_numpy(x).to(p.device).type_as(p) / 255. # uint8 to fp16/32 t.append(time_synchronized()) with amp.autocast(enabled=p.device.type != 'cpu'): # Inference y = self.model(x, augment, profile)[0] # forward t.append(time_synchronized()) # Post-process y = non_max_suppression(y, conf_thres=self.conf, iou_thres=self.iou, classes=self.classes) # NMS for i in range(n): scale_coords(shape1, y[i][:, :4], shape0[i]) t.append(time_synchronized()) return Detections(imgs, y, files, t, self.names, x.shape) class Detections: # detections class for YOLOv5 inference results def __init__(self, imgs, pred, files, times=None, names=None, shape=None): super(Detections, self).__init__() d = pred[0].device # device gn = [torch.tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1., 1.], device=d) for im in imgs] # normalizations self.imgs = imgs # list of images as numpy arrays self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls) self.names = names # class names self.files = files # image filenames self.xyxy = pred # xyxy pixels self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized self.n = len(self.pred) # number of images (batch size) self.t = tuple((times[i + 1] - times[i]) * 1000 / self.n for i in range(3)) # timestamps (ms) self.s = shape # inference BCHW shape def display(self, pprint=False, show=False, save=False, render=False, save_dir=''): colors = color_list() for i, (img, pred) in enumerate(zip(self.imgs, self.pred)): str = f'image {i + 1}/{len(self.pred)}: {img.shape[0]}x{img.shape[1]} ' if pred is not None: for c in pred[:, -1].unique(): n = (pred[:, -1] == c).sum() # detections per class str += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string if show or save or render: for *box, conf, cls in pred: # xyxy, confidence, class label = f'{self.names[int(cls)]} {conf:.2f}' plot_one_box(box, img, label=label, color=colors[int(cls) % 10]) img = Image.fromarray(img.astype(np.uint8)) if isinstance(img, np.ndarray) else img # from np if pprint: print(str.rstrip(', ')) if show: img.show(self.files[i]) # show if save: f = self.files[i] img.save(Path(save_dir) / f) # save print(f"{'Saved' * (i == 0)} {f}", end=',' if i < self.n - 1 else f' to {save_dir}\n') if render: self.imgs[i] = np.asarray(img) def print(self): self.display(pprint=True) # print results print(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {tuple(self.s)}' % self.t) def show(self): self.display(show=True) # show results def save(self, save_dir='runs/hub/exp'): save_dir = increment_path(save_dir, exist_ok=save_dir != 'runs/hub/exp') # increment save_dir Path(save_dir).mkdir(parents=True, exist_ok=True) self.display(save=True, save_dir=save_dir) # save results def render(self): self.display(render=True) # render results return self.imgs def pandas(self): # return detections as pandas DataFrames, i.e. print(results.pandas().xyxy[0]) new = copy(self) # return copy ca = 'xmin', 'ymin', 'xmax', 'ymax', 'confidence', 'class', 'name' # xyxy columns cb = 'xcenter', 'ycenter', 'width', 'height', 'confidence', 'class', 'name' # xywh columns for k, c in zip(['xyxy', 'xyxyn', 'xywh', 'xywhn'], [ca, ca, cb, cb]): a = [[x[:5] + [int(x[5]), self.names[int(x[5])]] for x in x.tolist()] for x in getattr(self, k)] # update setattr(new, k, [pd.DataFrame(x, columns=c) for x in a]) return new def tolist(self): # return a list of Detections objects, i.e. 'for result in results.tolist():' x = [Detections([self.imgs[i]], [self.pred[i]], self.names, self.s) for i in range(self.n)] for d in x: for k in ['imgs', 'pred', 'xyxy', 'xyxyn', 'xywh', 'xywhn']: setattr(d, k, getattr(d, k)[0]) # pop out of list return x def __len__(self): return self.n class Classify(nn.Module): # Classification head, i.e. x(b,c1,20,20) to x(b,c2) def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups super(Classify, self).__init__() self.aap = nn.AdaptiveAvgPool2d(1) # to x(b,c1,1,1) self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g) # to x(b,c2,1,1) self.flat = nn.Flatten() def forward(self, x): z = torch.cat([self.aap(y) for y in (x if isinstance(x, list) else [x])], 1) # cat if list return self.flat(self.conv(z)) # flatten to x(b,c2) ##### end of yolov5 ###### ##### orepa ##### def transI_fusebn(kernel, bn): gamma = bn.weight std = (bn.running_var + bn.eps).sqrt() return kernel * ((gamma / std).reshape(-1, 1, 1, 1)), bn.bias - bn.running_mean * gamma / std class ConvBN(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, deploy=False, nonlinear=None): super().__init__() if nonlinear is None: self.nonlinear = nn.Identity() else: self.nonlinear = nonlinear if deploy: self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=True) else: self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=False) self.bn = nn.BatchNorm2d(num_features=out_channels) def forward(self, x): if hasattr(self, 'bn'): return self.nonlinear(self.bn(self.conv(x))) else: return self.nonlinear(self.conv(x)) def switch_to_deploy(self): kernel, bias = transI_fusebn(self.conv.weight, self.bn) conv = nn.Conv2d(in_channels=self.conv.in_channels, out_channels=self.conv.out_channels, kernel_size=self.conv.kernel_size, stride=self.conv.stride, padding=self.conv.padding, dilation=self.conv.dilation, groups=self.conv.groups, bias=True) conv.weight.data = kernel conv.bias.data = bias for para in self.parameters(): para.detach_() self.__delattr__('conv') self.__delattr__('bn') self.conv = conv class OREPA_3x3_RepConv(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, internal_channels_1x1_3x3=None, deploy=False, nonlinear=None, single_init=False): super(OREPA_3x3_RepConv, self).__init__() self.deploy = deploy if nonlinear is None: self.nonlinear = nn.Identity() else: self.nonlinear = nonlinear self.kernel_size = kernel_size self.in_channels = in_channels self.out_channels = out_channels self.groups = groups assert padding == kernel_size // 2 self.stride = stride self.padding = padding self.dilation = dilation self.branch_counter = 0 self.weight_rbr_origin = nn.Parameter(torch.Tensor(out_channels, int(in_channels/self.groups), kernel_size, kernel_size)) nn.init.kaiming_uniform_(self.weight_rbr_origin, a=math.sqrt(1.0)) self.branch_counter += 1 if groups < out_channels: self.weight_rbr_avg_conv = nn.Parameter(torch.Tensor(out_channels, int(in_channels/self.groups), 1, 1)) self.weight_rbr_pfir_conv = nn.Parameter(torch.Tensor(out_channels, int(in_channels/self.groups), 1, 1)) nn.init.kaiming_uniform_(self.weight_rbr_avg_conv, a=1.0) nn.init.kaiming_uniform_(self.weight_rbr_pfir_conv, a=1.0) self.weight_rbr_avg_conv.data self.weight_rbr_pfir_conv.data self.register_buffer('weight_rbr_avg_avg', torch.ones(kernel_size, kernel_size).mul(1.0/kernel_size/kernel_size)) self.branch_counter += 1 else: raise NotImplementedError self.branch_counter += 1 if internal_channels_1x1_3x3 is None: internal_channels_1x1_3x3 = in_channels if groups < out_channels else 2 * in_channels # For mobilenet, it is better to have 2X internal channels if internal_channels_1x1_3x3 == in_channels: self.weight_rbr_1x1_kxk_idconv1 = nn.Parameter(torch.zeros(in_channels, int(in_channels/self.groups), 1, 1)) id_value = np.zeros((in_channels, int(in_channels/self.groups), 1, 1)) for i in range(in_channels): id_value[i, i % int(in_channels/self.groups), 0, 0] = 1 id_tensor = torch.from_numpy(id_value).type_as(self.weight_rbr_1x1_kxk_idconv1) self.register_buffer('id_tensor', id_tensor) else: self.weight_rbr_1x1_kxk_conv1 = nn.Parameter(torch.Tensor(internal_channels_1x1_3x3, int(in_channels/self.groups), 1, 1)) nn.init.kaiming_uniform_(self.weight_rbr_1x1_kxk_conv1, a=math.sqrt(1.0)) self.weight_rbr_1x1_kxk_conv2 = nn.Parameter(torch.Tensor(out_channels, int(internal_channels_1x1_3x3/self.groups), kernel_size, kernel_size)) nn.init.kaiming_uniform_(self.weight_rbr_1x1_kxk_conv2, a=math.sqrt(1.0)) self.branch_counter += 1 expand_ratio = 8 self.weight_rbr_gconv_dw = nn.Parameter(torch.Tensor(in_channels*expand_ratio, 1, kernel_size, kernel_size)) self.weight_rbr_gconv_pw = nn.Parameter(torch.Tensor(out_channels, in_channels*expand_ratio, 1, 1)) nn.init.kaiming_uniform_(self.weight_rbr_gconv_dw, a=math.sqrt(1.0)) nn.init.kaiming_uniform_(self.weight_rbr_gconv_pw, a=math.sqrt(1.0)) self.branch_counter += 1 if out_channels == in_channels and stride == 1: self.branch_counter += 1 self.vector = nn.Parameter(torch.Tensor(self.branch_counter, self.out_channels)) self.bn = nn.BatchNorm2d(out_channels) self.fre_init() nn.init.constant_(self.vector[0, :], 0.25) #origin nn.init.constant_(self.vector[1, :], 0.25) #avg nn.init.constant_(self.vector[2, :], 0.0) #prior nn.init.constant_(self.vector[3, :], 0.5) #1x1_kxk nn.init.constant_(self.vector[4, :], 0.5) #dws_conv def fre_init(self): prior_tensor = torch.Tensor(self.out_channels, self.kernel_size, self.kernel_size) half_fg = self.out_channels/2 for i in range(self.out_channels): for h in range(3): for w in range(3): if i < half_fg: prior_tensor[i, h, w] = math.cos(math.pi*(h+0.5)*(i+1)/3) else: prior_tensor[i, h, w] = math.cos(math.pi*(w+0.5)*(i+1-half_fg)/3) self.register_buffer('weight_rbr_prior', prior_tensor) def weight_gen(self): weight_rbr_origin = torch.einsum('oihw,o->oihw', self.weight_rbr_origin, self.vector[0, :]) weight_rbr_avg = torch.einsum('oihw,o->oihw', torch.einsum('oihw,hw->oihw', self.weight_rbr_avg_conv, self.weight_rbr_avg_avg), self.vector[1, :]) weight_rbr_pfir = torch.einsum('oihw,o->oihw', torch.einsum('oihw,ohw->oihw', self.weight_rbr_pfir_conv, self.weight_rbr_prior), self.vector[2, :]) weight_rbr_1x1_kxk_conv1 = None if hasattr(self, 'weight_rbr_1x1_kxk_idconv1'): weight_rbr_1x1_kxk_conv1 = (self.weight_rbr_1x1_kxk_idconv1 + self.id_tensor).squeeze() elif hasattr(self, 'weight_rbr_1x1_kxk_conv1'): weight_rbr_1x1_kxk_conv1 = self.weight_rbr_1x1_kxk_conv1.squeeze() else: raise NotImplementedError weight_rbr_1x1_kxk_conv2 = self.weight_rbr_1x1_kxk_conv2 if self.groups > 1: g = self.groups t, ig = weight_rbr_1x1_kxk_conv1.size() o, tg, h, w = weight_rbr_1x1_kxk_conv2.size() weight_rbr_1x1_kxk_conv1 = weight_rbr_1x1_kxk_conv1.view(g, int(t/g), ig) weight_rbr_1x1_kxk_conv2 = weight_rbr_1x1_kxk_conv2.view(g, int(o/g), tg, h, w) weight_rbr_1x1_kxk = torch.einsum('gti,gothw->goihw', weight_rbr_1x1_kxk_conv1, weight_rbr_1x1_kxk_conv2).view(o, ig, h, w) else: weight_rbr_1x1_kxk = torch.einsum('ti,othw->oihw', weight_rbr_1x1_kxk_conv1, weight_rbr_1x1_kxk_conv2) weight_rbr_1x1_kxk = torch.einsum('oihw,o->oihw', weight_rbr_1x1_kxk, self.vector[3, :]) weight_rbr_gconv = self.dwsc2full(self.weight_rbr_gconv_dw, self.weight_rbr_gconv_pw, self.in_channels) weight_rbr_gconv = torch.einsum('oihw,o->oihw', weight_rbr_gconv, self.vector[4, :]) weight = weight_rbr_origin + weight_rbr_avg + weight_rbr_1x1_kxk + weight_rbr_pfir + weight_rbr_gconv return weight def dwsc2full(self, weight_dw, weight_pw, groups): t, ig, h, w = weight_dw.size() o, _, _, _ = weight_pw.size() tg = int(t/groups) i = int(ig*groups) weight_dw = weight_dw.view(groups, tg, ig, h, w) weight_pw = weight_pw.squeeze().view(o, groups, tg) weight_dsc = torch.einsum('gtihw,ogt->ogihw', weight_dw, weight_pw) return weight_dsc.view(o, i, h, w) def forward(self, inputs): weight = self.weight_gen() out = F.conv2d(inputs, weight, bias=None, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups) return self.nonlinear(self.bn(out)) class RepConv_OREPA(nn.Module): def __init__(self, c1, c2, k=3, s=1, padding=1, dilation=1, groups=1, padding_mode='zeros', deploy=False, use_se=False, nonlinear=nn.SiLU()): super(RepConv_OREPA, self).__init__() self.deploy = deploy self.groups = groups self.in_channels = c1 self.out_channels = c2 self.padding = padding self.dilation = dilation self.groups = groups assert k == 3 assert padding == 1 padding_11 = padding - k // 2 if nonlinear is None: self.nonlinearity = nn.Identity() else: self.nonlinearity = nonlinear if use_se: self.se = SEBlock(self.out_channels, internal_neurons=self.out_channels // 16) else: self.se = nn.Identity() if deploy: self.rbr_reparam = nn.Conv2d(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=k, stride=s, padding=padding, dilation=dilation, groups=groups, bias=True, padding_mode=padding_mode) else: self.rbr_identity = nn.BatchNorm2d(num_features=self.in_channels) if self.out_channels == self.in_channels and s == 1 else None self.rbr_dense = OREPA_3x3_RepConv(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=k, stride=s, padding=padding, groups=groups, dilation=1) self.rbr_1x1 = ConvBN(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=1, stride=s, padding=padding_11, groups=groups, dilation=1) print('RepVGG Block, identity = ', self.rbr_identity) def forward(self, inputs): if hasattr(self, 'rbr_reparam'): return self.nonlinearity(self.se(self.rbr_reparam(inputs))) if self.rbr_identity is None: id_out = 0 else: id_out = self.rbr_identity(inputs) out1 = self.rbr_dense(inputs) out2 = self.rbr_1x1(inputs) out3 = id_out out = out1 + out2 + out3 return self.nonlinearity(self.se(out)) # Optional. This improves the accuracy and facilitates quantization. # 1. Cancel the original weight decay on rbr_dense.conv.weight and rbr_1x1.conv.weight. # 2. Use like this. # loss = criterion(....) # for every RepVGGBlock blk: # loss += weight_decay_coefficient * 0.5 * blk.get_cust_L2() # optimizer.zero_grad() # loss.backward() # Not used for OREPA def get_custom_L2(self): K3 = self.rbr_dense.weight_gen() K1 = self.rbr_1x1.conv.weight t3 = (self.rbr_dense.bn.weight / ((self.rbr_dense.bn.running_var + self.rbr_dense.bn.eps).sqrt())).reshape(-1, 1, 1, 1).detach() t1 = (self.rbr_1x1.bn.weight / ((self.rbr_1x1.bn.running_var + self.rbr_1x1.bn.eps).sqrt())).reshape(-1, 1, 1, 1).detach() l2_loss_circle = (K3 ** 2).sum() - (K3[:, :, 1:2, 1:2] ** 2).sum() # The L2 loss of the "circle" of weights in 3x3 kernel. Use regular L2 on them. eq_kernel = K3[:, :, 1:2, 1:2] * t3 + K1 * t1 # The equivalent resultant central point of 3x3 kernel. l2_loss_eq_kernel = (eq_kernel ** 2 / (t3 ** 2 + t1 ** 2)).sum() # Normalize for an L2 coefficient comparable to regular L2. return l2_loss_eq_kernel + l2_loss_circle def get_equivalent_kernel_bias(self): kernel3x3, bias3x3 = self._fuse_bn_tensor(self.rbr_dense) kernel1x1, bias1x1 = self._fuse_bn_tensor(self.rbr_1x1) kernelid, biasid = self._fuse_bn_tensor(self.rbr_identity) return kernel3x3 + self._pad_1x1_to_3x3_tensor(kernel1x1) + kernelid, bias3x3 + bias1x1 + biasid def _pad_1x1_to_3x3_tensor(self, kernel1x1): if kernel1x1 is None: return 0 else: return torch.nn.functional.pad(kernel1x1, [1,1,1,1]) def _fuse_bn_tensor(self, branch): if branch is None: return 0, 0 if not isinstance(branch, nn.BatchNorm2d): if isinstance(branch, OREPA_3x3_RepConv): kernel = branch.weight_gen() elif isinstance(branch, ConvBN): kernel = branch.conv.weight else: raise NotImplementedError running_mean = branch.bn.running_mean running_var = branch.bn.running_var gamma = branch.bn.weight beta = branch.bn.bias eps = branch.bn.eps else: if not hasattr(self, 'id_tensor'): input_dim = self.in_channels // self.groups kernel_value = np.zeros((self.in_channels, input_dim, 3, 3), dtype=np.float32) for i in range(self.in_channels): kernel_value[i, i % input_dim, 1, 1] = 1 self.id_tensor = torch.from_numpy(kernel_value).to(branch.weight.device) kernel = self.id_tensor running_mean = branch.running_mean running_var = branch.running_var gamma = branch.weight beta = branch.bias eps = branch.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta - running_mean * gamma / std def switch_to_deploy(self): if hasattr(self, 'rbr_reparam'): return print(f"RepConv_OREPA.switch_to_deploy") kernel, bias = self.get_equivalent_kernel_bias() self.rbr_reparam = nn.Conv2d(in_channels=self.rbr_dense.in_channels, out_channels=self.rbr_dense.out_channels, kernel_size=self.rbr_dense.kernel_size, stride=self.rbr_dense.stride, padding=self.rbr_dense.padding, dilation=self.rbr_dense.dilation, groups=self.rbr_dense.groups, bias=True) self.rbr_reparam.weight.data = kernel self.rbr_reparam.bias.data = bias for para in self.parameters(): para.detach_() self.__delattr__('rbr_dense') self.__delattr__('rbr_1x1') if hasattr(self, 'rbr_identity'): self.__delattr__('rbr_identity') ##### end of orepa ##### ##### swin transformer ##### class WindowAttention(nn.Module): def __init__(self, dim, window_size, num_heads, qkv_bias=True, qk_scale=None, attn_drop=0., proj_drop=0.): super().__init__() self.dim = dim self.window_size = window_size # Wh, Ww self.num_heads = num_heads head_dim = dim // num_heads self.scale = qk_scale or head_dim ** -0.5 # define a parameter table of relative position bias self.relative_position_bias_table = nn.Parameter( torch.zeros((2 * window_size[0] - 1) * (2 * window_size[1] - 1), num_heads)) # 2*Wh-1 * 2*Ww-1, nH # get pair-wise relative position index for each token inside the window coords_h = torch.arange(self.window_size[0]) coords_w = torch.arange(self.window_size[1]) coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2 relative_coords[:, :, 0] += self.window_size[0] - 1 # shift to start from 0 relative_coords[:, :, 1] += self.window_size[1] - 1 relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1 relative_position_index = relative_coords.sum(-1) # Wh*Ww, Wh*Ww self.register_buffer("relative_position_index", relative_position_index) self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim) self.proj_drop = nn.Dropout(proj_drop) nn.init.normal_(self.relative_position_bias_table, std=.02) self.softmax = nn.Softmax(dim=-1) def forward(self, x, mask=None): B_, N, C = x.shape qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple) q = q * self.scale attn = (q @ k.transpose(-2, -1)) relative_position_bias = self.relative_position_bias_table[self.relative_position_index.view(-1)].view( self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], -1) # Wh*Ww,Wh*Ww,nH relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww attn = attn + relative_position_bias.unsqueeze(0) if mask is not None: nW = mask.shape[0] attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0) attn = attn.view(-1, self.num_heads, N, N) attn = self.softmax(attn) else: attn = self.softmax(attn) attn = self.attn_drop(attn) # print(attn.dtype, v.dtype) try: x = (attn @ v).transpose(1, 2).reshape(B_, N, C) except: #print(attn.dtype, v.dtype) x = (attn.half() @ v).transpose(1, 2).reshape(B_, N, C) x = self.proj(x) x = self.proj_drop(x) return x class Mlp(nn.Module): def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.SiLU, drop=0.): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features self.fc1 = nn.Linear(in_features, hidden_features) self.act = act_layer() self.fc2 = nn.Linear(hidden_features, out_features) self.drop = nn.Dropout(drop) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) x = self.drop(x) return x def window_partition(x, window_size): B, H, W, C = x.shape assert H % window_size == 0, 'feature map h and w can not divide by window size' x = x.view(B, H // window_size, window_size, W // window_size, window_size, C) windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C) return windows def window_reverse(windows, window_size, H, W): B = int(windows.shape[0] / (H * W / window_size / window_size)) x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1) x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1) return x class SwinTransformerLayer(nn.Module): def __init__(self, dim, num_heads, window_size=8, shift_size=0, mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0., drop_path=0., act_layer=nn.SiLU, norm_layer=nn.LayerNorm): super().__init__() self.dim = dim self.num_heads = num_heads self.window_size = window_size self.shift_size = shift_size self.mlp_ratio = mlp_ratio # if min(self.input_resolution) <= self.window_size: # # if window size is larger than input resolution, we don't partition windows # self.shift_size = 0 # self.window_size = min(self.input_resolution) assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size" self.norm1 = norm_layer(dim) self.attn = WindowAttention( dim, window_size=(self.window_size, self.window_size), num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop) self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() self.norm2 = norm_layer(dim) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop) def create_mask(self, H, W): # calculate attention mask for SW-MSA img_mask = torch.zeros((1, H, W, 1)) # 1 H W 1 h_slices = (slice(0, -self.window_size), slice(-self.window_size, -self.shift_size), slice(-self.shift_size, None)) w_slices = (slice(0, -self.window_size), slice(-self.window_size, -self.shift_size), slice(-self.shift_size, None)) cnt = 0 for h in h_slices: for w in w_slices: img_mask[:, h, w, :] = cnt cnt += 1 mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1 mask_windows = mask_windows.view(-1, self.window_size * self.window_size) attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2) attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0)) return attn_mask def forward(self, x): # reshape x[b c h w] to x[b l c] _, _, H_, W_ = x.shape Padding = False if min(H_, W_) < self.window_size or H_ % self.window_size!=0 or W_ % self.window_size!=0: Padding = True # print(f'img_size {min(H_, W_)} is less than (or not divided by) window_size {self.window_size}, Padding.') pad_r = (self.window_size - W_ % self.window_size) % self.window_size pad_b = (self.window_size - H_ % self.window_size) % self.window_size x = F.pad(x, (0, pad_r, 0, pad_b)) # print('2', x.shape) B, C, H, W = x.shape L = H * W x = x.permute(0, 2, 3, 1).contiguous().view(B, L, C) # b, L, c # create mask from init to forward if self.shift_size > 0: attn_mask = self.create_mask(H, W).to(x.device) else: attn_mask = None shortcut = x x = self.norm1(x) x = x.view(B, H, W, C) # cyclic shift if self.shift_size > 0: shifted_x = torch.roll(x, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2)) else: shifted_x = x # partition windows x_windows = window_partition(shifted_x, self.window_size) # nW*B, window_size, window_size, C x_windows = x_windows.view(-1, self.window_size * self.window_size, C) # nW*B, window_size*window_size, C # W-MSA/SW-MSA attn_windows = self.attn(x_windows, mask=attn_mask) # nW*B, window_size*window_size, C # merge windows attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C) shifted_x = window_reverse(attn_windows, self.window_size, H, W) # B H' W' C # reverse cyclic shift if self.shift_size > 0: x = torch.roll(shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2)) else: x = shifted_x x = x.view(B, H * W, C) # FFN x = shortcut + self.drop_path(x) x = x + self.drop_path(self.mlp(self.norm2(x))) x = x.permute(0, 2, 1).contiguous().view(-1, C, H, W) # b c h w if Padding: x = x[:, :, :H_, :W_] # reverse padding return x class SwinTransformerBlock(nn.Module): def __init__(self, c1, c2, num_heads, num_layers, window_size=8): super().__init__() self.conv = None if c1 != c2: self.conv = Conv(c1, c2) # remove input_resolution self.blocks = nn.Sequential(*[SwinTransformerLayer(dim=c2, num_heads=num_heads, window_size=window_size, shift_size=0 if (i % 2 == 0) else window_size // 2) for i in range(num_layers)]) def forward(self, x): if self.conv is not None: x = self.conv(x) x = self.blocks(x) return x class STCSPA(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(STCSPA, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformerBlock(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.m(self.cv1(x)) y2 = self.cv2(x) return self.cv3(torch.cat((y1, y2), dim=1)) class STCSPB(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(STCSPB, self).__init__() c_ = int(c2) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformerBlock(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x1 = self.cv1(x) y1 = self.m(x1) y2 = self.cv2(x1) return self.cv3(torch.cat((y1, y2), dim=1)) class STCSPC(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(STCSPC, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(c_, c_, 1, 1) self.cv4 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformerBlock(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(torch.cat((y1, y2), dim=1)) ##### end of swin transformer ##### ##### swin transformer v2 ##### class WindowAttention_v2(nn.Module): def __init__(self, dim, window_size, num_heads, qkv_bias=True, attn_drop=0., proj_drop=0., pretrained_window_size=[0, 0]): super().__init__() self.dim = dim self.window_size = window_size # Wh, Ww self.pretrained_window_size = pretrained_window_size self.num_heads = num_heads self.logit_scale = nn.Parameter(torch.log(10 * torch.ones((num_heads, 1, 1))), requires_grad=True) # mlp to generate continuous relative position bias self.cpb_mlp = nn.Sequential(nn.Linear(2, 512, bias=True), nn.ReLU(inplace=True), nn.Linear(512, num_heads, bias=False)) # get relative_coords_table relative_coords_h = torch.arange(-(self.window_size[0] - 1), self.window_size[0], dtype=torch.float32) relative_coords_w = torch.arange(-(self.window_size[1] - 1), self.window_size[1], dtype=torch.float32) relative_coords_table = torch.stack( torch.meshgrid([relative_coords_h, relative_coords_w])).permute(1, 2, 0).contiguous().unsqueeze(0) # 1, 2*Wh-1, 2*Ww-1, 2 if pretrained_window_size[0] > 0: relative_coords_table[:, :, :, 0] /= (pretrained_window_size[0] - 1) relative_coords_table[:, :, :, 1] /= (pretrained_window_size[1] - 1) else: relative_coords_table[:, :, :, 0] /= (self.window_size[0] - 1) relative_coords_table[:, :, :, 1] /= (self.window_size[1] - 1) relative_coords_table *= 8 # normalize to -8, 8 relative_coords_table = torch.sign(relative_coords_table) * torch.log2( torch.abs(relative_coords_table) + 1.0) / np.log2(8) self.register_buffer("relative_coords_table", relative_coords_table) # get pair-wise relative position index for each token inside the window coords_h = torch.arange(self.window_size[0]) coords_w = torch.arange(self.window_size[1]) coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2 relative_coords[:, :, 0] += self.window_size[0] - 1 # shift to start from 0 relative_coords[:, :, 1] += self.window_size[1] - 1 relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1 relative_position_index = relative_coords.sum(-1) # Wh*Ww, Wh*Ww self.register_buffer("relative_position_index", relative_position_index) self.qkv = nn.Linear(dim, dim * 3, bias=False) if qkv_bias: self.q_bias = nn.Parameter(torch.zeros(dim)) self.v_bias = nn.Parameter(torch.zeros(dim)) else: self.q_bias = None self.v_bias = None self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim) self.proj_drop = nn.Dropout(proj_drop) self.softmax = nn.Softmax(dim=-1) def forward(self, x, mask=None): B_, N, C = x.shape qkv_bias = None if self.q_bias is not None: qkv_bias = torch.cat((self.q_bias, torch.zeros_like(self.v_bias, requires_grad=False), self.v_bias)) qkv = F.linear(input=x, weight=self.qkv.weight, bias=qkv_bias) qkv = qkv.reshape(B_, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4) q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple) # cosine attention attn = (F.normalize(q, dim=-1) @ F.normalize(k, dim=-1).transpose(-2, -1)) logit_scale = torch.clamp(self.logit_scale, max=torch.log(torch.tensor(1. / 0.01))).exp() attn = attn * logit_scale relative_position_bias_table = self.cpb_mlp(self.relative_coords_table).view(-1, self.num_heads) relative_position_bias = relative_position_bias_table[self.relative_position_index.view(-1)].view( self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], -1) # Wh*Ww,Wh*Ww,nH relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww relative_position_bias = 16 * torch.sigmoid(relative_position_bias) attn = attn + relative_position_bias.unsqueeze(0) if mask is not None: nW = mask.shape[0] attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0) attn = attn.view(-1, self.num_heads, N, N) attn = self.softmax(attn) else: attn = self.softmax(attn) attn = self.attn_drop(attn) try: x = (attn @ v).transpose(1, 2).reshape(B_, N, C) except: x = (attn.half() @ v).transpose(1, 2).reshape(B_, N, C) x = self.proj(x) x = self.proj_drop(x) return x def extra_repr(self) -> str: return f'dim={self.dim}, window_size={self.window_size}, ' \ f'pretrained_window_size={self.pretrained_window_size}, num_heads={self.num_heads}' def flops(self, N): # calculate flops for 1 window with token length of N flops = 0 # qkv = self.qkv(x) flops += N * self.dim * 3 * self.dim # attn = (q @ k.transpose(-2, -1)) flops += self.num_heads * N * (self.dim // self.num_heads) * N # x = (attn @ v) flops += self.num_heads * N * N * (self.dim // self.num_heads) # x = self.proj(x) flops += N * self.dim * self.dim return flops class Mlp_v2(nn.Module): def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.SiLU, drop=0.): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features self.fc1 = nn.Linear(in_features, hidden_features) self.act = act_layer() self.fc2 = nn.Linear(hidden_features, out_features) self.drop = nn.Dropout(drop) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) x = self.drop(x) return x def window_partition_v2(x, window_size): B, H, W, C = x.shape x = x.view(B, H // window_size, window_size, W // window_size, window_size, C) windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C) return windows def window_reverse_v2(windows, window_size, H, W): B = int(windows.shape[0] / (H * W / window_size / window_size)) x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1) x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1) return x class SwinTransformerLayer_v2(nn.Module): def __init__(self, dim, num_heads, window_size=7, shift_size=0, mlp_ratio=4., qkv_bias=True, drop=0., attn_drop=0., drop_path=0., act_layer=nn.SiLU, norm_layer=nn.LayerNorm, pretrained_window_size=0): super().__init__() self.dim = dim #self.input_resolution = input_resolution self.num_heads = num_heads self.window_size = window_size self.shift_size = shift_size self.mlp_ratio = mlp_ratio #if min(self.input_resolution) <= self.window_size: # # if window size is larger than input resolution, we don't partition windows # self.shift_size = 0 # self.window_size = min(self.input_resolution) assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size" self.norm1 = norm_layer(dim) self.attn = WindowAttention_v2( dim, window_size=(self.window_size, self.window_size), num_heads=num_heads, qkv_bias=qkv_bias, attn_drop=attn_drop, proj_drop=drop, pretrained_window_size=(pretrained_window_size, pretrained_window_size)) self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() self.norm2 = norm_layer(dim) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = Mlp_v2(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop) def create_mask(self, H, W): # calculate attention mask for SW-MSA img_mask = torch.zeros((1, H, W, 1)) # 1 H W 1 h_slices = (slice(0, -self.window_size), slice(-self.window_size, -self.shift_size), slice(-self.shift_size, None)) w_slices = (slice(0, -self.window_size), slice(-self.window_size, -self.shift_size), slice(-self.shift_size, None)) cnt = 0 for h in h_slices: for w in w_slices: img_mask[:, h, w, :] = cnt cnt += 1 mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1 mask_windows = mask_windows.view(-1, self.window_size * self.window_size) attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2) attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0)) return attn_mask def forward(self, x): # reshape x[b c h w] to x[b l c] _, _, H_, W_ = x.shape Padding = False if min(H_, W_) < self.window_size or H_ % self.window_size!=0 or W_ % self.window_size!=0: Padding = True # print(f'img_size {min(H_, W_)} is less than (or not divided by) window_size {self.window_size}, Padding.') pad_r = (self.window_size - W_ % self.window_size) % self.window_size pad_b = (self.window_size - H_ % self.window_size) % self.window_size x = F.pad(x, (0, pad_r, 0, pad_b)) # print('2', x.shape) B, C, H, W = x.shape L = H * W x = x.permute(0, 2, 3, 1).contiguous().view(B, L, C) # b, L, c # create mask from init to forward if self.shift_size > 0: attn_mask = self.create_mask(H, W).to(x.device) else: attn_mask = None shortcut = x x = x.view(B, H, W, C) # cyclic shift if self.shift_size > 0: shifted_x = torch.roll(x, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2)) else: shifted_x = x # partition windows x_windows = window_partition_v2(shifted_x, self.window_size) # nW*B, window_size, window_size, C x_windows = x_windows.view(-1, self.window_size * self.window_size, C) # nW*B, window_size*window_size, C # W-MSA/SW-MSA attn_windows = self.attn(x_windows, mask=attn_mask) # nW*B, window_size*window_size, C # merge windows attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C) shifted_x = window_reverse_v2(attn_windows, self.window_size, H, W) # B H' W' C # reverse cyclic shift if self.shift_size > 0: x = torch.roll(shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2)) else: x = shifted_x x = x.view(B, H * W, C) x = shortcut + self.drop_path(self.norm1(x)) # FFN x = x + self.drop_path(self.norm2(self.mlp(x))) x = x.permute(0, 2, 1).contiguous().view(-1, C, H, W) # b c h w if Padding: x = x[:, :, :H_, :W_] # reverse padding return x def extra_repr(self) -> str: return f"dim={self.dim}, input_resolution={self.input_resolution}, num_heads={self.num_heads}, " \ f"window_size={self.window_size}, shift_size={self.shift_size}, mlp_ratio={self.mlp_ratio}" def flops(self): flops = 0 H, W = self.input_resolution # norm1 flops += self.dim * H * W # W-MSA/SW-MSA nW = H * W / self.window_size / self.window_size flops += nW * self.attn.flops(self.window_size * self.window_size) # mlp flops += 2 * H * W * self.dim * self.dim * self.mlp_ratio # norm2 flops += self.dim * H * W return flops class SwinTransformer2Block(nn.Module): def __init__(self, c1, c2, num_heads, num_layers, window_size=7): super().__init__() self.conv = None if c1 != c2: self.conv = Conv(c1, c2) # remove input_resolution self.blocks = nn.Sequential(*[SwinTransformerLayer_v2(dim=c2, num_heads=num_heads, window_size=window_size, shift_size=0 if (i % 2 == 0) else window_size // 2) for i in range(num_layers)]) def forward(self, x): if self.conv is not None: x = self.conv(x) x = self.blocks(x) return x class ST2CSPA(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(ST2CSPA, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformer2Block(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.m(self.cv1(x)) y2 = self.cv2(x) return self.cv3(torch.cat((y1, y2), dim=1)) class ST2CSPB(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(ST2CSPB, self).__init__() c_ = int(c2) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformer2Block(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x1 = self.cv1(x) y1 = self.m(x1) y2 = self.cv2(x1) return self.cv3(torch.cat((y1, y2), dim=1)) class ST2CSPC(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(ST2CSPC, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(c_, c_, 1, 1) self.cv4 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformer2Block(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(torch.cat((y1, y2), dim=1)) ##### end of swin transformer v2 ##### ================================================ FILE: asone/detectors/yolov7/yolov7/models/experimental.py ================================================ import torch import torch.nn as nn from asone.detectors.yolov7.yolov7.models.common import Conv class Ensemble(nn.ModuleList): # Ensemble of models def __init__(self): super(Ensemble, self).__init__() def forward(self, x, augment=False): y = [] for module in self: y.append(module(x, augment)[0]) # y = torch.stack(y).max(0)[0] # max ensemble # y = torch.stack(y).mean(0) # mean ensemble y = torch.cat(y, 1) # nms ensemble return y, None # inference, train output def attempt_load(weights, map_location=None): # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a model = Ensemble() for w in weights if isinstance(weights, list) else [weights]: ckpt = torch.load(w, map_location=map_location) # load model.append(ckpt['ema' if ckpt.get('ema') else 'model'].float().fuse().eval()) # FP32 model # Compatibility updates for m in model.modules(): if type(m) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU]: m.inplace = True # pytorch 1.7.0 compatibility elif type(m) is nn.Upsample: m.recompute_scale_factor = None # torch 1.11.0 compatibility elif type(m) is Conv: m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility if len(model) == 1: return model[-1] # return model else: print('Ensemble created with %s\n' % weights) for k in ['names', 'stride']: setattr(model, k, getattr(model[-1], k)) return model # return ensemble ================================================ FILE: asone/detectors/yolov7/yolov7/models/yolo.py ================================================ from asone.detectors.yolov7.yolov7.utils.torch_utils import time_synchronized, fuse_conv_and_bn, model_info, scale_img, \ initialize_weights, select_device, copy_attr from asone.detectors.yolov7.yolov7.models.experimental import * from asone.detectors.yolov7.yolov7.models.common import * import torch import argparse import logging import sys from copy import deepcopy # sys.path.append('./') # to run '$ python *.py' files in subdirectories logger = logging.getLogger(__name__) try: import thop # for FLOPS computation except ImportError: thop = None class Detect(nn.Module): stride = None # strides computed during build export = False # onnx export end2end = False include_nms = False concat = False def __init__(self, nc=80, anchors=(), ch=()): # detection layer super(Detect, self).__init__() self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer('anchors', a) # shape(nl,na,2) self.register_buffer('anchor_grid', a.clone().view( self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv def forward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](x[i]) # conv bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute( 0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() if not torch.onnx.is_in_onnx_export(): y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * \ self.anchor_grid[i] # wh else: # y.tensor_split((2, 4, 5), 4) # torch 1.8.0 xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # new xy xy = xy * (2. * self.stride[i]) + \ (self.stride[i] * (self.grid[i] - 0.5)) wh = wh ** 2 * (4 * self.anchor_grid[i].data) # new wh y = torch.cat((xy, wh, conf), 4) z.append(y.view(bs, -1, self.no)) if self.training: out = x elif self.end2end: out = torch.cat(z, 1) elif self.include_nms: z = self.convert(z) out = (z, ) elif self.concat: out = torch.cat(z, 1) else: out = (torch.cat(z, 1), x) return out @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() def convert(self, z): z = torch.cat(z, 1) box = z[:, :, :4] conf = z[:, :, 4:5] score = z[:, :, 5:] score *= conf convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]], dtype=torch.float32, device=z.device) box @= convert_matrix return (box, score) class IDetect(nn.Module): stride = None # strides computed during build export = False # onnx export end2end = False include_nms = False concat = False def __init__(self, nc=80, anchors=(), ch=()): # detection layer super(IDetect, self).__init__() self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer('anchors', a) # shape(nl,na,2) self.register_buffer('anchor_grid', a.clone().view( self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv self.ia = nn.ModuleList(ImplicitA(x) for x in ch) self.im = nn.ModuleList(ImplicitM(self.no * self.na) for _ in ch) def forward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](self.ia[i](x[i])) # conv x[i] = self.im[i](x[i]) bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute( 0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * \ self.anchor_grid[i] # wh z.append(y.view(bs, -1, self.no)) return x if self.training else (torch.cat(z, 1), x) def fuseforward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](x[i]) # conv bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute( 0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() if not torch.onnx.is_in_onnx_export(): y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * \ self.anchor_grid[i] # wh else: # y.tensor_split((2, 4, 5), 4) # torch 1.8.0 xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # new xy xy = xy * (2. * self.stride[i]) + \ (self.stride[i] * (self.grid[i] - 0.5)) wh = wh ** 2 * (4 * self.anchor_grid[i].data) # new wh y = torch.cat((xy, wh, conf), 4) z.append(y.view(bs, -1, self.no)) if self.training: out = x elif self.end2end: out = torch.cat(z, 1) elif self.include_nms: z = self.convert(z) out = (z, ) elif self.concat: out = torch.cat(z, 1) else: out = (torch.cat(z, 1), x) return out def fuse(self): print("IDetect.fuse") # fuse ImplicitA and Convolution for i in range(len(self.m)): c1, c2, _, _ = self.m[i].weight.shape c1_, c2_, _, _ = self.ia[i].implicit.shape self.m[i].bias += torch.matmul(self.m[i].weight.reshape( c1, c2), self.ia[i].implicit.reshape(c2_, c1_)).squeeze(1) # fuse ImplicitM and Convolution for i in range(len(self.m)): c1, c2, _, _ = self.im[i].implicit.shape self.m[i].bias *= self.im[i].implicit.reshape(c2) self.m[i].weight *= self.im[i].implicit.transpose(0, 1) @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() def convert(self, z): z = torch.cat(z, 1) box = z[:, :, :4] conf = z[:, :, 4:5] score = z[:, :, 5:] score *= conf convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]], dtype=torch.float32, device=z.device) box @= convert_matrix return (box, score) class IKeypoint(nn.Module): stride = None # strides computed during build export = False # onnx export def __init__(self, nc=80, anchors=(), nkpt=17, ch=(), inplace=True, dw_conv_kpt=False): # detection layer super(IKeypoint, self).__init__() self.nc = nc # number of classes self.nkpt = nkpt self.dw_conv_kpt = dw_conv_kpt # number of outputs per anchor for box and class self.no_det = (nc + 5) self.no_kpt = 3*self.nkpt # number of outputs per anchor for keypoints self.no = self.no_det+self.no_kpt self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid self.flip_test = False a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer('anchors', a) # shape(nl,na,2) self.register_buffer('anchor_grid', a.clone().view( self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no_det * self.na, 1) for x in ch) # output conv self.ia = nn.ModuleList(ImplicitA(x) for x in ch) self.im = nn.ModuleList(ImplicitM(self.no_det * self.na) for _ in ch) if self.nkpt is not None: if self.dw_conv_kpt: # keypoint head is slightly more complex self.m_kpt = nn.ModuleList( nn.Sequential(DWConv(x, x, k=3), Conv(x, x), DWConv(x, x, k=3), Conv(x, x), DWConv(x, x, k=3), Conv(x, x), DWConv(x, x, k=3), Conv(x, x), DWConv(x, x, k=3), Conv(x, x), DWConv(x, x, k=3), nn.Conv2d(x, self.no_kpt * self.na, 1)) for x in ch) else: # keypoint head is a single convolution self.m_kpt = nn.ModuleList( nn.Conv2d(x, self.no_kpt * self.na, 1) for x in ch) self.inplace = inplace # use in-place ops (e.g. slice assignment) def forward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): if self.nkpt is None or self.nkpt == 0: x[i] = self.im[i](self.m[i](self.ia[i](x[i]))) # conv else: x[i] = torch.cat( (self.im[i](self.m[i](self.ia[i](x[i]))), self.m_kpt[i](x[i])), axis=1) bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute( 0, 1, 3, 4, 2).contiguous() x_det = x[i][..., :6] x_kpt = x[i][..., 6:] if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) kpt_grid_x = self.grid[i][..., 0:1] kpt_grid_y = self.grid[i][..., 1:2] if self.nkpt == 0: y = x[i].sigmoid() else: y = x_det.sigmoid() if self.inplace: xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * \ self.stride[i] # xy wh = (y[..., 2:4] * 2) ** 2 * \ self.anchor_grid[i].view(1, self.na, 1, 1, 2) # wh if self.nkpt != 0: x_kpt[..., 0::3] = ( x_kpt[..., ::3] * 2. - 0.5 + kpt_grid_x.repeat(1, 1, 1, 1, 17)) * self.stride[i] # xy x_kpt[..., 1::3] = ( x_kpt[..., 1::3] * 2. - 0.5 + kpt_grid_y.repeat(1, 1, 1, 1, 17)) * self.stride[i] # xy # x_kpt[..., 0::3] = (x_kpt[..., ::3] + kpt_grid_x.repeat(1,1,1,1,17)) * self.stride[i] # xy # x_kpt[..., 1::3] = (x_kpt[..., 1::3] + kpt_grid_y.repeat(1,1,1,1,17)) * self.stride[i] # xy # print('=============') # print(self.anchor_grid[i].shape) # print(self.anchor_grid[i][...,0].unsqueeze(4).shape) #print(x_kpt[..., 0::3].shape) # x_kpt[..., 0::3] = ((x_kpt[..., 0::3].tanh() * 2.) ** 3 * self.anchor_grid[i][...,0].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_x.repeat(1,1,1,1,17) * self.stride[i] # xy # x_kpt[..., 1::3] = ((x_kpt[..., 1::3].tanh() * 2.) ** 3 * self.anchor_grid[i][...,1].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_y.repeat(1,1,1,1,17) * self.stride[i] # xy # x_kpt[..., 0::3] = (((x_kpt[..., 0::3].sigmoid() * 4.) ** 2 - 8.) * self.anchor_grid[i][...,0].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_x.repeat(1,1,1,1,17) * self.stride[i] # xy # x_kpt[..., 1::3] = (((x_kpt[..., 1::3].sigmoid() * 4.) ** 2 - 8.) * self.anchor_grid[i][...,1].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_y.repeat(1,1,1,1,17) * self.stride[i] # xy x_kpt[..., 2::3] = x_kpt[..., 2::3].sigmoid() y = torch.cat((xy, wh, y[..., 4:], x_kpt), dim=-1) else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953 xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * \ self.stride[i] # xy wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh if self.nkpt != 0: y[..., 6:] = (y[..., 6:] * 2. - 0.5 + self.grid[i].repeat( (1, 1, 1, 1, self.nkpt))) * self.stride[i] # xy y = torch.cat((xy, wh, y[..., 4:]), -1) z.append(y.view(bs, -1, self.no)) return x if self.training else (torch.cat(z, 1), x) @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() class IAuxDetect(nn.Module): stride = None # strides computed during build export = False # onnx export end2end = False include_nms = False concat = False def __init__(self, nc=80, anchors=(), ch=()): # detection layer super(IAuxDetect, self).__init__() self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer('anchors', a) # shape(nl,na,2) self.register_buffer('anchor_grid', a.clone().view( self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch[:self.nl]) # output conv self.m2 = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch[self.nl:]) # output conv self.ia = nn.ModuleList(ImplicitA(x) for x in ch[:self.nl]) self.im = nn.ModuleList(ImplicitM(self.no * self.na) for _ in ch[:self.nl]) def forward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](self.ia[i](x[i])) # conv x[i] = self.im[i](x[i]) bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute( 0, 1, 3, 4, 2).contiguous() x[i+self.nl] = self.m2[i](x[i+self.nl]) x[i+self.nl] = x[i+self.nl].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() if not torch.onnx.is_in_onnx_export(): y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * \ self.anchor_grid[i] # wh else: # y.tensor_split((2, 4, 5), 4) # torch 1.8.0 xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # new xy xy = xy * (2. * self.stride[i]) + \ (self.stride[i] * (self.grid[i] - 0.5)) wh = wh ** 2 * (4 * self.anchor_grid[i].data) # new wh y = torch.cat((xy, wh, conf), 4) z.append(y.view(bs, -1, self.no)) return x if self.training else (torch.cat(z, 1), x[:self.nl]) def fuseforward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](x[i]) # conv bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute( 0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() if not torch.onnx.is_in_onnx_export(): y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * \ self.anchor_grid[i] # wh else: xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * \ self.stride[i] # xy wh = (y[..., 2:4] * 2) ** 2 * \ self.anchor_grid[i].data # wh y = torch.cat((xy, wh, y[..., 4:]), -1) z.append(y.view(bs, -1, self.no)) if self.training: out = x elif self.end2end: out = torch.cat(z, 1) elif self.include_nms: z = self.convert(z) out = (z, ) elif self.concat: out = torch.cat(z, 1) else: out = (torch.cat(z, 1), x) return out def fuse(self): print("IAuxDetect.fuse") # fuse ImplicitA and Convolution for i in range(len(self.m)): c1, c2, _, _ = self.m[i].weight.shape c1_, c2_, _, _ = self.ia[i].implicit.shape self.m[i].bias += torch.matmul(self.m[i].weight.reshape( c1, c2), self.ia[i].implicit.reshape(c2_, c1_)).squeeze(1) # fuse ImplicitM and Convolution for i in range(len(self.m)): c1, c2, _, _ = self.im[i].implicit.shape self.m[i].bias *= self.im[i].implicit.reshape(c2) self.m[i].weight *= self.im[i].implicit.transpose(0, 1) @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() def convert(self, z): z = torch.cat(z, 1) box = z[:, :, :4] conf = z[:, :, 4:5] score = z[:, :, 5:] score *= conf convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]], dtype=torch.float32, device=z.device) box @= convert_matrix return (box, score) class IBin(nn.Module): stride = None # strides computed during build export = False # onnx export def __init__(self, nc=80, anchors=(), ch=(), bin_count=21): # detection layer super(IBin, self).__init__() self.nc = nc # number of classes self.bin_count = bin_count self.w_bin_sigmoid = SigmoidBin( bin_count=self.bin_count, min=0.0, max=4.0) self.h_bin_sigmoid = SigmoidBin( bin_count=self.bin_count, min=0.0, max=4.0) # classes, x,y,obj self.no = nc + 3 + \ self.w_bin_sigmoid.get_length() + self.h_bin_sigmoid.get_length() # w-bce, h-bce # + self.x_bin_sigmoid.get_length() + self.y_bin_sigmoid.get_length() self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer('anchors', a) # shape(nl,na,2) self.register_buffer('anchor_grid', a.clone().view( self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv self.ia = nn.ModuleList(ImplicitA(x) for x in ch) self.im = nn.ModuleList(ImplicitM(self.no * self.na) for _ in ch) def forward(self, x): #self.x_bin_sigmoid.use_fw_regression = True #self.y_bin_sigmoid.use_fw_regression = True self.w_bin_sigmoid.use_fw_regression = True self.h_bin_sigmoid.use_fw_regression = True # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](self.ia[i](x[i])) # conv x[i] = self.im[i](x[i]) bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute( 0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy # y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh #px = (self.x_bin_sigmoid.forward(y[..., 0:12]) + self.grid[i][..., 0]) * self.stride[i] #py = (self.y_bin_sigmoid.forward(y[..., 12:24]) + self.grid[i][..., 1]) * self.stride[i] pw = self.w_bin_sigmoid.forward( y[..., 2:24]) * self.anchor_grid[i][..., 0] ph = self.h_bin_sigmoid.forward( y[..., 24:46]) * self.anchor_grid[i][..., 1] #y[..., 0] = px #y[..., 1] = py y[..., 2] = pw y[..., 3] = ph y = torch.cat((y[..., 0:4], y[..., 46:]), dim=-1) z.append(y.view(bs, -1, y.shape[-1])) return x if self.training else (torch.cat(z, 1), x) @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() class Model(nn.Module): # model, input channels, number of classes def __init__(self, cfg='yolor-csp-c.yaml', ch=3, nc=None, anchors=None): super(Model, self).__init__() self.traced = False if isinstance(cfg, dict): self.yaml = cfg # model dict else: # is *.yaml import yaml # for torch hub self.yaml_file = Path(cfg).name with open(cfg) as f: self.yaml = yaml.load(f, Loader=yaml.SafeLoader) # model dict # Define model ch = self.yaml['ch'] = self.yaml.get('ch', ch) # input channels if nc and nc != self.yaml['nc']: logger.info( f"Overriding model.yaml nc={self.yaml['nc']} with nc={nc}") self.yaml['nc'] = nc # override yaml value if anchors: logger.info( f'Overriding model.yaml anchors with anchors={anchors}') self.yaml['anchors'] = round(anchors) # override yaml value self.model, self.save = parse_model( deepcopy(self.yaml), ch=[ch]) # model, savelist self.names = [str(i) for i in range(self.yaml['nc'])] # default names # print([x.shape for x in self.forward(torch.zeros(1, ch, 64, 64))]) # Build strides, anchors m = self.model[-1] # Detect() if isinstance(m, Detect): s = 256 # 2x min stride m.stride = torch.tensor( [s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward check_anchor_order(m) m.anchors /= m.stride.view(-1, 1, 1) self.stride = m.stride self._initialize_biases() # only run once # print('Strides: %s' % m.stride.tolist()) if isinstance(m, IDetect): s = 256 # 2x min stride m.stride = torch.tensor( [s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward check_anchor_order(m) m.anchors /= m.stride.view(-1, 1, 1) self.stride = m.stride self._initialize_biases() # only run once # print('Strides: %s' % m.stride.tolist()) if isinstance(m, IAuxDetect): s = 256 # 2x min stride m.stride = torch.tensor( [s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))[:4]]) # forward # print(m.stride) check_anchor_order(m) m.anchors /= m.stride.view(-1, 1, 1) self.stride = m.stride self._initialize_aux_biases() # only run once # print('Strides: %s' % m.stride.tolist()) if isinstance(m, IBin): s = 256 # 2x min stride m.stride = torch.tensor( [s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward check_anchor_order(m) m.anchors /= m.stride.view(-1, 1, 1) self.stride = m.stride self._initialize_biases_bin() # only run once # print('Strides: %s' % m.stride.tolist()) if isinstance(m, IKeypoint): s = 256 # 2x min stride m.stride = torch.tensor( [s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward check_anchor_order(m) m.anchors /= m.stride.view(-1, 1, 1) self.stride = m.stride self._initialize_biases_kpt() # only run once # print('Strides: %s' % m.stride.tolist()) # Init weights, biases initialize_weights(self) self.info() logger.info('') def forward(self, x, augment=False, profile=False): if augment: img_size = x.shape[-2:] # height, width s = [1, 0.83, 0.67] # scales f = [None, 3, None] # flips (2-ud, 3-lr) y = [] # outputs for si, fi in zip(s, f): xi = scale_img(x.flip(fi) if fi else x, si, gs=int(self.stride.max())) yi = self.forward_once(xi)[0] # forward # cv2.imwrite(f'img_{si}.jpg', 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1]) # save yi[..., :4] /= si # de-scale if fi == 2: yi[..., 1] = img_size[0] - yi[..., 1] # de-flip ud elif fi == 3: yi[..., 0] = img_size[1] - yi[..., 0] # de-flip lr y.append(yi) return torch.cat(y, 1), None # augmented inference, train else: # single-scale inference, train return self.forward_once(x, profile) def forward_once(self, x, profile=False): y, dt = [], [] # outputs for m in self.model: if m.f != -1: # if not from previous layer x = y[m.f] if isinstance(m.f, int) else [ x if j == -1 else y[j] for j in m.f] # from earlier layers if not hasattr(self, 'traced'): self.traced = False if self.traced: if isinstance(m, Detect) or isinstance(m, IDetect) or isinstance(m, IAuxDetect) or isinstance(m, IKeypoint): break if profile: c = isinstance(m, (Detect, IDetect, IAuxDetect, IBin)) o = thop.profile(m, inputs=(x.copy() if c else x,), verbose=False)[ 0] / 1E9 * 2 if thop else 0 # FLOPS for _ in range(10): m(x.copy() if c else x) t = time_synchronized() for _ in range(10): m(x.copy() if c else x) dt.append((time_synchronized() - t) * 100) print('%10.1f%10.0f%10.1fms %-40s' % (o, m.np, dt[-1], m.type)) x = m(x) # run y.append(x if m.i in self.save else None) # save output if profile: print('%.1fms total' % sum(dt)) return x # initialize biases into Detect(), cf is class frequency def _initialize_biases(self, cf=None): # https://arxiv.org/abs/1708.02002 section 3.3 # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. m = self.model[-1] # Detect() module for mi, s in zip(m.m, m.stride): # from b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) # obj (8 objects per 640 image) b.data[:, 4] += math.log(8 / (640 / s) ** 2) b.data[:, 5:] += math.log(0.6 / (m.nc - 0.99) ) if cf is None else torch.log(cf / cf.sum()) # cls mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) # initialize biases into Detect(), cf is class frequency def _initialize_aux_biases(self, cf=None): # https://arxiv.org/abs/1708.02002 section 3.3 # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. m = self.model[-1] # Detect() module for mi, mi2, s in zip(m.m, m.m2, m.stride): # from b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) # obj (8 objects per 640 image) b.data[:, 4] += math.log(8 / (640 / s) ** 2) b.data[:, 5:] += math.log(0.6 / (m.nc - 0.99) ) if cf is None else torch.log(cf / cf.sum()) # cls mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) b2 = mi2.bias.view(m.na, -1) # conv.bias(255) to (3,85) # obj (8 objects per 640 image) b2.data[:, 4] += math.log(8 / (640 / s) ** 2) b2.data[:, 5:] += math.log(0.6 / (m.nc - 0.99) ) if cf is None else torch.log(cf / cf.sum()) # cls mi2.bias = torch.nn.Parameter(b2.view(-1), requires_grad=True) # initialize biases into Detect(), cf is class frequency def _initialize_biases_bin(self, cf=None): # https://arxiv.org/abs/1708.02002 section 3.3 # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. m = self.model[-1] # Bin() module bc = m.bin_count for mi, s in zip(m.m, m.stride): # from b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) old = b[:, (0, 1, 2, bc+3)].data obj_idx = 2*bc+4 b[:, :obj_idx].data += math.log(0.6 / (bc + 1 - 0.99)) # obj (8 objects per 640 image) b[:, obj_idx].data += math.log(8 / (640 / s) ** 2) b[:, (obj_idx+1):].data += math.log(0.6 / (m.nc - 0.99) ) if cf is None else torch.log(cf / cf.sum()) # cls b[:, (0, 1, 2, bc+3)].data = old mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) # initialize biases into Detect(), cf is class frequency def _initialize_biases_kpt(self, cf=None): # https://arxiv.org/abs/1708.02002 section 3.3 # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. m = self.model[-1] # Detect() module for mi, s in zip(m.m, m.stride): # from b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) # obj (8 objects per 640 image) b.data[:, 4] += math.log(8 / (640 / s) ** 2) b.data[:, 5:] += math.log(0.6 / (m.nc - 0.99) ) if cf is None else torch.log(cf / cf.sum()) # cls mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) def _print_biases(self): m = self.model[-1] # Detect() module for mi in m.m: # from b = mi.bias.detach().view(m.na, -1).T # conv.bias(255) to (3,85) print(('%6g Conv2d.bias:' + '%10.3g' * 6) % (mi.weight.shape[1], *b[:5].mean(1).tolist(), b[5:].mean())) # def _print_weights(self): # for m in self.model.modules(): # if type(m) is Bottleneck: # print('%10.3g' % (m.w.detach().sigmoid() * 2)) # shortcut weights def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers print('Fusing layers... ') for m in self.model.modules(): if isinstance(m, RepConv): #print(f" fuse_repvgg_block") m.fuse_repvgg_block() elif isinstance(m, RepConv_OREPA): #print(f" switch_to_deploy") m.switch_to_deploy() elif type(m) is Conv and hasattr(m, 'bn'): m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv delattr(m, 'bn') # remove batchnorm m.forward = m.fuseforward # update forward elif isinstance(m, (IDetect, IAuxDetect)): m.fuse() m.forward = m.fuseforward self.info() return self def nms(self, mode=True): # add or remove NMS module present = type(self.model[-1]) is NMS # last layer is NMS if mode and not present: print('Adding NMS... ') m = NMS() # module m.f = -1 # from m.i = self.model[-1].i + 1 # index self.model.add_module(name='%s' % m.i, module=m) # add self.eval() elif not mode and present: print('Removing NMS... ') self.model = self.model[:-1] # remove return self def autoshape(self): # add autoShape module print('Adding autoShape... ') m = autoShape(self) # wrap model copy_attr(m, self, include=('yaml', 'nc', 'hyp', 'names', 'stride'), exclude=()) # copy attributes return m def info(self, verbose=False, img_size=640): # print model information model_info(self, verbose, img_size) def parse_model(d, ch): # model_dict, input_channels(3) logger.info('\n%3s%18s%3s%10s %-40s%-30s' % ('', 'from', 'n', 'params', 'module', 'arguments')) anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple'] na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors no = na * (nc + 5) # number of outputs = anchors * (classes + 5) layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out # from, number, module, args for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): m = eval(m) if isinstance(m, str) else m # eval strings for j, a in enumerate(args): try: args[j] = eval(a) if isinstance(a, str) else a # eval strings except: pass n = max(round(n * gd), 1) if n > 1 else n # depth gain if m in [nn.Conv2d, Conv, RobustConv, RobustConv2, DWConv, GhostConv, RepConv, RepConv_OREPA, DownC, SPP, SPPF, SPPCSPC, GhostSPPCSPC, MixConv2d, Focus, Stem, GhostStem, CrossConv, Bottleneck, BottleneckCSPA, BottleneckCSPB, BottleneckCSPC, RepBottleneck, RepBottleneckCSPA, RepBottleneckCSPB, RepBottleneckCSPC, Res, ResCSPA, ResCSPB, ResCSPC, RepRes, RepResCSPA, RepResCSPB, RepResCSPC, ResX, ResXCSPA, ResXCSPB, ResXCSPC, RepResX, RepResXCSPA, RepResXCSPB, RepResXCSPC, Ghost, GhostCSPA, GhostCSPB, GhostCSPC, SwinTransformerBlock, STCSPA, STCSPB, STCSPC, SwinTransformer2Block, ST2CSPA, ST2CSPB, ST2CSPC]: c1, c2 = ch[f], args[0] if c2 != no: # if not output c2 = make_divisible(c2 * gw, 8) args = [c1, c2, *args[1:]] if m in [DownC, SPPCSPC, GhostSPPCSPC, BottleneckCSPA, BottleneckCSPB, BottleneckCSPC, RepBottleneckCSPA, RepBottleneckCSPB, RepBottleneckCSPC, ResCSPA, ResCSPB, ResCSPC, RepResCSPA, RepResCSPB, RepResCSPC, ResXCSPA, ResXCSPB, ResXCSPC, RepResXCSPA, RepResXCSPB, RepResXCSPC, GhostCSPA, GhostCSPB, GhostCSPC, STCSPA, STCSPB, STCSPC, ST2CSPA, ST2CSPB, ST2CSPC]: args.insert(2, n) # number of repeats n = 1 elif m is nn.BatchNorm2d: args = [ch[f]] elif m is Concat: c2 = sum([ch[x] for x in f]) elif m is Chuncat: c2 = sum([ch[x] for x in f]) elif m is Shortcut: c2 = ch[f[0]] elif m is Foldcut: c2 = ch[f] // 2 elif m in [Detect, IDetect, IAuxDetect, IBin, IKeypoint]: args.append([ch[x] for x in f]) if isinstance(args[1], int): # number of anchors args[1] = [list(range(args[1] * 2))] * len(f) elif m is ReOrg: c2 = ch[f] * 4 elif m is Contract: c2 = ch[f] * args[0] ** 2 elif m is Expand: c2 = ch[f] // args[0] ** 2 else: c2 = ch[f] m_ = nn.Sequential(*[m(*args) for _ in range(n)] ) if n > 1 else m(*args) # module t = str(m)[8:-2].replace('__main__.', '') # module type np = sum([x.numel() for x in m_.parameters()]) # number params # attach index, 'from' index, type, number params m_.i, m_.f, m_.type, m_.np = i, f, t, np logger.info('%3s%18s%3s%10.0f %-40s%-30s' % (i, f, n, np, t, args)) # print save.extend(x % i for x in ([f] if isinstance( f, int) else f) if x != -1) # append to savelist layers.append(m_) if i == 0: ch = [] ch.append(c2) return nn.Sequential(*layers), sorted(save) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--cfg', type=str, default='yolor-csp-c.yaml', help='model.yaml') parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') parser.add_argument('--profile', action='store_true', help='profile model speed') opt = parser.parse_args() opt.cfg = check_file(opt.cfg) # check file set_logging() device = select_device(opt.device) # Create model model = Model(opt.cfg).to(device) model.train() if opt.profile: img = torch.rand(1, 3, 640, 640).to(device) y = model(img, profile=True) # Profile # img = torch.rand(8 if torch.cuda.is_available() else 1, 3, 640, 640).to(device) # y = model(img, profile=True) # Tensorboard # from torch.utils.tensorboard import SummaryWriter # tb_writer = SummaryWriter() # print("Run 'tensorboard --logdir=models/runs' to view tensorboard at http://localhost:6006/") # tb_writer.add_graph(model.model, img) # add model to tensorboard # tb_writer.add_image('test', img[0], dataformats='CWH') # add model to tensorboard ================================================ FILE: asone/detectors/yolov7/yolov7/utils/__init__.py ================================================ ================================================ FILE: asone/detectors/yolov7/yolov7/utils/torch_utils.py ================================================ # YOLOR PyTorch utils import datetime import logging import math import os import platform import subprocess import time from contextlib import contextmanager from copy import deepcopy from pathlib import Path import torch import torch.backends.cudnn as cudnn import torch.nn as nn import torch.nn.functional as F import torchvision try: import thop # for FLOPS computation except ImportError: thop = None logger = logging.getLogger(__name__) @contextmanager def torch_distributed_zero_first(local_rank: int): """ Decorator to make all processes in distributed training wait for each local_master to do something. """ if local_rank not in [-1, 0]: torch.distributed.barrier() yield if local_rank == 0: torch.distributed.barrier() def init_torch_seeds(seed=0): # Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html torch.manual_seed(seed) if seed == 0: # slower, more reproducible cudnn.benchmark, cudnn.deterministic = False, True else: # faster, less reproducible cudnn.benchmark, cudnn.deterministic = True, False def date_modified(path=__file__): # return human-readable file modification date, i.e. '2021-3-26' t = datetime.datetime.fromtimestamp(Path(path).stat().st_mtime) return f'{t.year}-{t.month}-{t.day}' def git_describe(path=Path(__file__).parent): # path must be a directory # return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe s = f'git -C {path} describe --tags --long --always' try: return subprocess.check_output(s, shell=True, stderr=subprocess.STDOUT).decode()[:-1] except subprocess.CalledProcessError as e: return '' # not a git repository def select_device(device='', batch_size=None): # device = 'cpu' or '0' or '0,1,2,3' s = f'YOLOR 🚀 {git_describe() or date_modified()} torch {torch.__version__} ' # string cpu = device.lower() == 'cpu' if cpu: os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False elif device: # non-cpu device requested os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested' # check availability cuda = not cpu and torch.cuda.is_available() if cuda: n = torch.cuda.device_count() if n > 1 and batch_size: # check that batch_size is compatible with device_count assert batch_size % n == 0, f'batch-size {batch_size} not multiple of GPU count {n}' space = ' ' * len(s) for i, d in enumerate(device.split(',') if device else range(n)): p = torch.cuda.get_device_properties(i) s += f"{'' if i == 0 else space}CUDA:{d} ({p.name}, {p.total_memory / 1024 ** 2}MB)\n" # bytes to MB else: s += 'CPU\n' logger.info(s.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else s) # emoji-safe return torch.device('cuda:0' if cuda else 'cpu') def time_synchronized(): # pytorch-accurate time if torch.cuda.is_available(): torch.cuda.synchronize() return time.time() def profile(x, ops, n=100, device=None): # profile a pytorch module or list of modules. Example usage: # x = torch.randn(16, 3, 640, 640) # input # m1 = lambda x: x * torch.sigmoid(x) # m2 = nn.SiLU() # profile(x, [m1, m2], n=100) # profile speed over 100 iterations device = device or torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') x = x.to(device) x.requires_grad = True print(torch.__version__, device.type, torch.cuda.get_device_properties(0) if device.type == 'cuda' else '') print(f"\n{'Params':>12s}{'GFLOPS':>12s}{'forward (ms)':>16s}{'backward (ms)':>16s}{'input':>24s}{'output':>24s}") for m in ops if isinstance(ops, list) else [ops]: m = m.to(device) if hasattr(m, 'to') else m # device m = m.half() if hasattr(m, 'half') and isinstance(x, torch.Tensor) and x.dtype is torch.float16 else m # type dtf, dtb, t = 0., 0., [0., 0., 0.] # dt forward, backward try: flops = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 # GFLOPS except: flops = 0 for _ in range(n): t[0] = time_synchronized() y = m(x) t[1] = time_synchronized() try: _ = y.sum().backward() t[2] = time_synchronized() except: # no backward method t[2] = float('nan') dtf += (t[1] - t[0]) * 1000 / n # ms per op forward dtb += (t[2] - t[1]) * 1000 / n # ms per op backward s_in = tuple(x.shape) if isinstance(x, torch.Tensor) else 'list' s_out = tuple(y.shape) if isinstance(y, torch.Tensor) else 'list' p = sum(list(x.numel() for x in m.parameters())) if isinstance(m, nn.Module) else 0 # parameters print(f'{p:12}{flops:12.4g}{dtf:16.4g}{dtb:16.4g}{str(s_in):>24s}{str(s_out):>24s}') def is_parallel(model): return type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel) def intersect_dicts(da, db, exclude=()): # Dictionary intersection of matching keys and shapes, omitting 'exclude' keys, using da values return {k: v for k, v in da.items() if k in db and not any(x in k for x in exclude) and v.shape == db[k].shape} def initialize_weights(model): for m in model.modules(): t = type(m) if t is nn.Conv2d: pass # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif t is nn.BatchNorm2d: m.eps = 1e-3 m.momentum = 0.03 elif t in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6]: m.inplace = True def find_modules(model, mclass=nn.Conv2d): # Finds layer indices matching module class 'mclass' return [i for i, m in enumerate(model.module_list) if isinstance(m, mclass)] def sparsity(model): # Return global model sparsity a, b = 0., 0. for p in model.parameters(): a += p.numel() b += (p == 0).sum() return b / a def prune(model, amount=0.3): # Prune model to requested global sparsity import torch.nn.utils.prune as prune print('Pruning model... ', end='') for name, m in model.named_modules(): if isinstance(m, nn.Conv2d): prune.l1_unstructured(m, name='weight', amount=amount) # prune prune.remove(m, 'weight') # make permanent print(' %.3g global sparsity' % sparsity(model)) def fuse_conv_and_bn(conv, bn): # Fuse convolution and batchnorm layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/ fusedconv = nn.Conv2d(conv.in_channels, conv.out_channels, kernel_size=conv.kernel_size, stride=conv.stride, padding=conv.padding, groups=conv.groups, bias=True).requires_grad_(False).to(conv.weight.device) # prepare filters w_conv = conv.weight.clone().view(conv.out_channels, -1) w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.shape)) # prepare spatial bias b_conv = torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps)) fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) return fusedconv def model_info(model, verbose=False, img_size=640): # Model information. img_size may be int or list, i.e. img_size=640 or img_size=[640, 320] n_p = sum(x.numel() for x in model.parameters()) # number parameters n_g = sum(x.numel() for x in model.parameters() if x.requires_grad) # number gradients if verbose: print('%5s %40s %9s %12s %20s %10s %10s' % ('layer', 'name', 'gradient', 'parameters', 'shape', 'mu', 'sigma')) for i, (name, p) in enumerate(model.named_parameters()): name = name.replace('module_list.', '') print('%5g %40s %9s %12g %20s %10.3g %10.3g' % (i, name, p.requires_grad, p.numel(), list(p.shape), p.mean(), p.std())) try: # FLOPS from thop import profile stride = max(int(model.stride.max()), 32) if hasattr(model, 'stride') else 32 img = torch.zeros((1, model.yaml.get('ch', 3), stride, stride), device=next(model.parameters()).device) # input flops = profile(deepcopy(model), inputs=(img,), verbose=False)[0] / 1E9 * 2 # stride GFLOPS img_size = img_size if isinstance(img_size, list) else [img_size, img_size] # expand if int/float fs = ', %.1f GFLOPS' % (flops * img_size[0] / stride * img_size[1] / stride) # 640x640 GFLOPS except (ImportError, Exception): fs = '' logger.info(f"Model Summary: {len(list(model.modules()))} layers, {n_p} parameters, {n_g} gradients{fs}") def load_classifier(name='resnet101', n=2): # Loads a pretrained model reshaped to n-class output model = torchvision.models.__dict__[name](pretrained=True) # ResNet model properties # input_size = [3, 224, 224] # input_space = 'RGB' # input_range = [0, 1] # mean = [0.485, 0.456, 0.406] # std = [0.229, 0.224, 0.225] # Reshape output to n classes filters = model.fc.weight.shape[1] model.fc.bias = nn.Parameter(torch.zeros(n), requires_grad=True) model.fc.weight = nn.Parameter(torch.zeros(n, filters), requires_grad=True) model.fc.out_features = n return model def scale_img(img, ratio=1.0, same_shape=False, gs=32): # img(16,3,256,416) # scales img(bs,3,y,x) by ratio constrained to gs-multiple if ratio == 1.0: return img else: h, w = img.shape[2:] s = (int(h * ratio), int(w * ratio)) # new size img = F.interpolate(img, size=s, mode='bilinear', align_corners=False) # resize if not same_shape: # pad/crop img h, w = [math.ceil(x * ratio / gs) * gs for x in (h, w)] return F.pad(img, [0, w - s[1], 0, h - s[0]], value=0.447) # value = imagenet mean def copy_attr(a, b, include=(), exclude=()): # Copy attributes from b to a, options to only include [...] and to exclude [...] for k, v in b.__dict__.items(): if (len(include) and k not in include) or k.startswith('_') or k in exclude: continue else: setattr(a, k, v) class ModelEMA: """ Model Exponential Moving Average from https://github.com/rwightman/pytorch-image-models Keep a moving average of everything in the model state_dict (parameters and buffers). This is intended to allow functionality like https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage A smoothed version of the weights is necessary for some training schemes to perform well. This class is sensitive where it is initialized in the sequence of model init, GPU assignment and distributed training wrappers. """ def __init__(self, model, decay=0.9999, updates=0): # Create EMA self.ema = deepcopy(model.module if is_parallel(model) else model).eval() # FP32 EMA # if next(model.parameters()).device.type != 'cpu': # self.ema.half() # FP16 EMA self.updates = updates # number of EMA updates self.decay = lambda x: decay * (1 - math.exp(-x / 2000)) # decay exponential ramp (to help early epochs) for p in self.ema.parameters(): p.requires_grad_(False) def update(self, model): # Update EMA parameters with torch.no_grad(): self.updates += 1 d = self.decay(self.updates) msd = model.module.state_dict() if is_parallel(model) else model.state_dict() # model state_dict for k, v in self.ema.state_dict().items(): if v.dtype.is_floating_point: v *= d v += (1. - d) * msd[k].detach() def update_attr(self, model, include=(), exclude=('process_group', 'reducer')): # Update EMA attributes copy_attr(self.ema, model, include, exclude) class BatchNormXd(torch.nn.modules.batchnorm._BatchNorm): def _check_input_dim(self, input): # The only difference between BatchNorm1d, BatchNorm2d, BatchNorm3d, etc # is this method that is overwritten by the sub-class # This original goal of this method was for tensor sanity checks # If you're ok bypassing those sanity checks (eg. if you trust your inference # to provide the right dimensional inputs), then you can just use this method # for easy conversion from SyncBatchNorm # (unfortunately, SyncBatchNorm does not store the original class - if it did # we could return the one that was originally created) return def revert_sync_batchnorm(module): # this is very similar to the function that it is trying to revert: # https://github.com/pytorch/pytorch/blob/c8b3686a3e4ba63dc59e5dcfe5db3430df256833/torch/nn/modules/batchnorm.py#L679 module_output = module if isinstance(module, torch.nn.modules.batchnorm.SyncBatchNorm): new_cls = BatchNormXd module_output = BatchNormXd(module.num_features, module.eps, module.momentum, module.affine, module.track_running_stats) if module.affine: with torch.no_grad(): module_output.weight = module.weight module_output.bias = module.bias module_output.running_mean = module.running_mean module_output.running_var = module.running_var module_output.num_batches_tracked = module.num_batches_tracked if hasattr(module, "qconfig"): module_output.qconfig = module.qconfig for name, child in module.named_children(): module_output.add_module(name, revert_sync_batchnorm(child)) del module return module_output class TracedModel(nn.Module): def __init__(self, model=None, device=None, img_size=(640,640)): super(TracedModel, self).__init__() print(" Convert model to Traced-model... ") self.stride = model.stride self.names = model.names self.model = model self.model = revert_sync_batchnorm(self.model) self.model.to('cpu') self.model.eval() self.detect_layer = self.model.model[-1] self.model.traced = True rand_example = torch.rand(1, 3, img_size, img_size) traced_script_module = torch.jit.trace(self.model, rand_example, strict=False) #traced_script_module = torch.jit.script(self.model) traced_script_module.save("traced_model.pt") print(" traced_script_module saved! ") self.model = traced_script_module self.model.to(device) self.detect_layer.to(device) print(" model is traced! \n") def forward(self, x, augment=False, profile=False): out = self.model(x) out = self.detect_layer(out) return out ================================================ FILE: asone/detectors/yolov7/yolov7/utils/yolov7_utils.py ================================================ import cv2 import numpy as np import torch import torchvision import time def prepare_input(image, input_shape): input_height, input_width = input_shape input_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Resize input image input_img = cv2.resize(input_img, (input_width, input_height)) # Scale input pixel values to 0 to 1 input_img = input_img / 255.0 input_img = input_img.transpose(2, 0, 1) input_tensor = input_img[np.newaxis, :, :, :].astype(np.float32) return input_tensor def process_output(output, ori_shape, input_shape, conf_threshold, iou_threshold): predictions = output[0] # predictions = np.squeeze(output[0]) # Filter out object confidence scores below threshold # obj_conf = predictions[:, 4] obj_conf = predictions[:, 6] # predictions = predictions[obj_conf > conf_threshold] # obj_conf = obj_conf[obj_conf > conf_threshold] # print(obj_conf[0]) # Multiply class confidence with bounding box confidence # predictions[:, 5] *= obj_conf[:, np.newaxis] # predictions[:, 6] *= obj_conf # Get the scores # scores = np.max(predictions[:, 5:], axis=1) scores = predictions[:, 6] # Filter out the objects with a low score predictions = predictions[obj_conf > conf_threshold] scores = scores[scores > conf_threshold] if len(scores) == 0: return [] # Get the class with the highest confidence # class_ids = np.argmax(predictions[:, 5:], axis=1) class_ids = predictions[:, 5].astype(np.uint16) # Extract boxes from predictions boxes = predictions[:, 1:5] # Scale boxes to original image dimensions boxes = rescale_boxes(boxes, ori_shape, input_shape) # Convert boxes to xyxy format # boxes = xywh2xyxy(boxes) # Apply non-maxima suppression to suppress weak, overlapping bounding boxes indices = nms(boxes, scores, iou_threshold) dets = [] for i in indices: dets.append([*boxes[i], scores[i], class_ids[i]]) # return boxes[indices], scores[indices], class_ids[indices] return np.array(dets) def rescale_boxes(boxes, ori_shape, input_shape): input_height, input_width = input_shape img_height, img_width = ori_shape # Rescale boxes to original image dimensions input_shape = np.array([input_width, input_height, input_width, input_height]) boxes = np.divide(boxes, input_shape, dtype=np.float32) boxes *= np.array([img_width, img_height, img_width, img_height]) return boxes def nms(boxes, scores, iou_threshold): # Sort by score sorted_indices = np.argsort(scores)[::-1] keep_boxes = [] while sorted_indices.size > 0: # Pick the last box box_id = sorted_indices[0] keep_boxes.append(box_id) # Compute IoU of the picked box with the rest ious = compute_iou(boxes[box_id, :], boxes[sorted_indices[1:], :]) # Remove boxes with IoU over the threshold keep_indices = np.where(ious < iou_threshold)[0] # print(keep_indices.shape, sorted_indices.shape) sorted_indices = sorted_indices[keep_indices + 1] return keep_boxes def compute_iou(box, boxes): # Compute xmin, ymin, xmax, ymax for both boxes xmin = np.maximum(box[0], boxes[:, 0]) ymin = np.maximum(box[1], boxes[:, 1]) xmax = np.minimum(box[2], boxes[:, 2]) ymax = np.minimum(box[3], boxes[:, 3]) # Compute intersection area intersection_area = np.maximum(0, xmax - xmin) * np.maximum(0, ymax - ymin) # Compute union area box_area = (box[2] - box[0]) * (box[3] - box[1]) boxes_area = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) union_area = box_area + boxes_area - intersection_area # Compute IoU iou = intersection_area / union_area return iou def xywh2xyxy(x): # Convert bounding box (x, y, w, h) to bounding box (x1, y1, x2, y2) y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = x[..., 0] - x[..., 2] / 2 y[..., 1] = x[..., 1] - x[..., 3] / 2 y[..., 2] = x[..., 0] + x[..., 2] / 2 y[..., 3] = x[..., 1] + x[..., 3] / 2 return y def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False, labels=()): """Runs Non-Maximum Suppression (NMS) on inference results Returns: list of detections, on (n,6) tensor per image [xyxy, conf, cls] """ nc = prediction.shape[2] - 5 # number of classes xc = prediction[..., 4] > conf_thres # candidates # Settings min_wh, max_wh = 2, 4096 # (pixels) minimum and maximum box width and height max_det = 300 # maximum number of detections per image max_nms = 30000 # maximum number of boxes into torchvision.ops.nms() time_limit = 10.0 # seconds to quit after redundant = True # require redundant detections multi_label &= nc > 1 # multiple labels per box (adds 0.5ms/img) merge = False # use merge-NMS t = time.time() output = [torch.zeros((0, 6), device=prediction.device)] * prediction.shape[0] for xi, x in enumerate(prediction): # image index, image inference # Apply constraints # x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height x = x[xc[xi]] # confidence # Cat apriori labels if autolabelling if labels and len(labels[xi]): l = labels[xi] v = torch.zeros((len(l), nc + 5), device=x.device) v[:, :4] = l[:, 1:5] # box v[:, 4] = 1.0 # conf v[range(len(l)), l[:, 0].long() + 5] = 1.0 # cls x = torch.cat((x, v), 0) # If none remain process next image if not x.shape[0]: continue # Compute conf if nc == 1: x[:, 5:] = x[:, 4:5] # for models with one class, cls_loss is 0 and cls_conf is always 0.5, # so there is no need to multiplicate. else: x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf # Box (center x, center y, width, height) to (x1, y1, x2, y2) box = xywh2xyxy(x[:, :4]) # Detections matrix nx6 (xyxy, conf, cls) if multi_label: i, j = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T x = torch.cat((box[i], x[i, j + 5, None], j[:, None].float()), 1) else: # best class only conf, j = x[:, 5:].max(1, keepdim=True) x = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres] # Filter by class if classes is not None: x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)] # Apply finite constraint # if not torch.isfinite(x).all(): # x = x[torch.isfinite(x).all(1)] # Check shape n = x.shape[0] # number of boxes if not n: # no boxes continue elif n > max_nms: # excess boxes x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence # Batched NMS c = x[:, 5:6] * (0 if agnostic else max_wh) # classes boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS if i.shape[0] > max_det: # limit detections i = i[:max_det] if merge and (1 < n < 3E3): # Merge NMS (boxes merged using weighted mean) # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4) iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix weights = iou * scores[None] # box weights x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes if redundant: i = i[iou.sum(1) > 1] # require redundancy output[xi] = x[i] if (time.time() - t) > time_limit: print(f'WARNING: NMS time limit {time_limit}s exceeded') break # time limit exceeded return output ================================================ FILE: asone/detectors/yolov7/yolov7_detector.py ================================================ import os import sys import onnxruntime import torch import coremltools as ct from asone.utils import get_names import numpy as np import warnings from asone.detectors.yolov7.yolov7.utils.yolov7_utils import (prepare_input, process_output, non_max_suppression) from asone.detectors.yolov7.yolov7.models.experimental import attempt_load from asone import utils from PIL import Image from asone.detectors.utils.coreml_utils import yolo_to_xyxy, generalize_output_format, scale_bboxes from asone.utils.utils import PathResolver def xywh2xyxy(x): # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y return y class YOLOv7Detector: def __init__(self, weights=None, use_onnx=False, mlmodel=False, use_cuda=True): self.use_onnx = use_onnx self.mlmodel = mlmodel self.device = 'cuda' if use_cuda else 'cpu' #If incase weighst is a list of paths then select path at first index weights = str(weights[0] if isinstance(weights, list) else weights) if not os.path.exists(weights): utils.download_weights(weights) with PathResolver(): # Load Model self.model = self.load_model(use_cuda, weights) def load_model(self, use_cuda, weights, fp16=False): # Device: CUDA and if fp16=True only then half precision floating point works self.fp16 = fp16 & ((not self.use_onnx or self.use_onnx) and self.device != 'cpu') # Load onnx if self.use_onnx: if use_cuda: providers = ['CUDAExecutionProvider','CPUExecutionProvider'] else: providers = ['CPUExecutionProvider'] model = onnxruntime.InferenceSession(weights, providers=providers) # Load coreml elif self.mlmodel: model = ct.models.MLModel(weights) #Load Pytorch else: model = attempt_load(weights, map_location=self.device) model.half() if self.fp16 else model.float() return model def detect(self, image: list, input_shape: tuple = (640, 640), conf_thres: float = 0.25, iou_thres: float = 0.45, max_det: int = 1000, filter_classes: bool = None, agnostic_nms: bool = True, with_p6: bool = False, return_image=False) -> list: # Preprocess input image and also copying original image for later use original_image = image.copy() img_height, img_width = original_image.shape[:2] processed_image = prepare_input(image, input_shape) # Perform Inference on the Image if self.use_onnx: # Run ONNX model input_name = self.model.get_inputs()[0].name prediction = self.model.run([self.model.get_outputs()[0].name], { input_name: processed_image}) # Run Coreml model elif self.mlmodel: h ,w = image.shape[:2] pred = self.model.predict({"image":Image.fromarray(image).resize(input_shape)}) xyxy = yolo_to_xyxy(pred['coordinates'], input_shape) out = generalize_output_format(xyxy, pred['confidence'], conf_thres) if out != []: detections = scale_bboxes(out, image.shape[:2], input_shape) else: detections = np.empty((0, 6)) if filter_classes: class_names = get_names() filter_class_idx = [] if filter_classes: for _class in filter_classes: if _class.lower() in class_names: filter_class_idx.append(class_names.index(_class.lower())) else: warnings.warn(f"class {_class} not found in model classes list.") detections = detections[np.in1d(detections[:,5].astype(int), filter_class_idx)] return detections, {'width':w, 'height':h} # Run Pytorch model else: processed_image = torch.from_numpy(processed_image).to(self.device) # Change image floating point precision if fp16 set to true processed_image = processed_image.half() if self.fp16 else processed_image.float() with torch.no_grad(): prediction = self.model(processed_image, augment=False)[0] detection = [] # Postprocess prediction if self.use_onnx: detection = process_output(prediction, original_image.shape[:2], input_shape, conf_thres, iou_thres) else: detection = non_max_suppression(prediction, conf_thres, iou_thres, agnostic=agnostic_nms)[0] detection = detection.detach().cpu().numpy() # detection = yolo_to_xyxy(detection, input_shape) # print(detection) # Rescaling Bounding Boxes detection[:, :4] /= np.array([input_shape[1], input_shape[0], input_shape[1], input_shape[0]]) detection[:, :4] *= np.array([img_width, img_height, img_width, img_height]) image_info = { 'width': original_image.shape[1], 'height': original_image.shape[0], } if len(detection) > 0: self.boxes = detection[:, :4] self.scores = detection[:, 4:5] self.class_ids = detection[:, 5:6] if filter_classes: class_names = get_names() filter_class_idx = [] if filter_classes: for _class in filter_classes: if _class.lower() in class_names: filter_class_idx.append(class_names.index(_class.lower())) else: warnings.warn(f"class {_class} not found in model classes list.") detection = detection[np.in1d(detection[:,5].astype(int), filter_class_idx)] if return_image: return detection, original_image else: return detection, image_info ================================================ FILE: asone/detectors/yolov8/__init__.py ================================================ from .yolov8_detector import YOLOv8Detector __all__ = ['YOLOv8Detector'] ================================================ FILE: asone/detectors/yolov8/utils/__init__.py ================================================ ================================================ FILE: asone/detectors/yolov8/utils/yolov8_utils.py ================================================ import cv2 import numpy as np from ultralytics.utils import ops import torch from ultralytics.data.augment import LetterBox def prepare_input(image, input_shape, stride, pt): input_tensor = LetterBox(input_shape, auto=pt, stride=stride)(image=image) input_tensor = input_tensor.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB input_tensor = np.ascontiguousarray(input_tensor).astype(np.float32) # contiguous input_tensor /= 255.0 # 0 - 255 to 0.0 - 1.0 input_tensor = input_tensor[None].astype(np.float32) return input_tensor def process_output(detections, ori_shape, input_shape, conf_threshold, iou_threshold, classes=None, mlmodel=False, agnostic=False, max_det=300, ): detections = ops.non_max_suppression(detections, conf_thres=conf_threshold, iou_thres=iou_threshold, classes=classes, agnostic=agnostic, max_det=max_det, ) if mlmodel: detection = detections[0].cpu().numpy() return detection for i in range(len(detections)): # Extract boxes from predictions detections[i][:, :4] = ops.scale_boxes(input_shape, detections[i][:, :4], ori_shape).round() return detections[0].cpu().numpy() def rescale_boxes(boxes, ori_shape, input_shape): input_height, input_width = input_shape img_height, img_width = ori_shape # Rescale boxes to original image dimensions input_shape = np.array( [input_width, input_height, input_width, input_height]) boxes = np.divide(boxes, input_shape, dtype=np.float32) boxes *= np.array([img_width, img_height, img_width, img_height]) return boxes ================================================ FILE: asone/detectors/yolov8/yolov8_detector.py ================================================ import os from asone import utils from asone.utils import get_names import onnxruntime import torch from asone.detectors.yolov8.utils.yolov8_utils import prepare_input, process_output import numpy as np import warnings from ultralytics.nn.autobackend import AutoBackend from ultralytics.nn.tasks import DetectionModel, attempt_load_one_weight import coremltools as ct from PIL import Image from asone.detectors.utils.coreml_utils import yolo_to_xyxy, generalize_output_format, scale_bboxes from asone.utils.utils import PathResolver class YOLOv8Detector: def __init__(self, weights=None, use_onnx=False, mlmodel=False, use_cuda=True): self.mlmodel = mlmodel self.use_onnx = use_onnx self.device = 'cuda' if use_cuda else 'cpu' # If incase weighst is a list of paths then select path at first index weights = str(weights[0] if isinstance(weights, list) else weights) if not os.path.exists(weights): utils.download_weights(weights) with PathResolver(): # Load Model self.model = self.load_model(use_cuda, weights) def load_model(self, use_cuda, weights, fp16=False): # Device: CUDA and if fp16=True only then half precision floating point works self.fp16 = fp16 & ( (not self.use_onnx or self.use_onnx) and self.device != 'cpu') # Load onnx if self.use_onnx: if use_cuda: providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] else: providers = ['CPUExecutionProvider'] model = onnxruntime.InferenceSession(weights, providers=providers) # Load coreml elif self.mlmodel: model = ct.models.MLModel(weights) # Load Pytorch else: model, ckpt = attempt_load_one_weight(weights) model = AutoBackend(model, fp16=False, dnn=False).to(self.device) model.half() if self.fp16 else model.float() return model def detect(self, image: list, input_shape: tuple = (640, 640), conf_thres: float = 0.25, iou_thres: float = 0.45, max_det: int = 1000, filter_classes: bool = None, agnostic_nms: bool = True, with_p6: bool = False, return_image=False ) -> list: # Preprocess input image and also copying original image for later use original_image = image.copy() processed_image = prepare_input( image, input_shape, 32, False if self.use_onnx else True) # Perform Inference on the Image if self.use_onnx: # Run ONNX model input_name = self.model.get_inputs()[0].name prediction = self.model.run([self.model.get_outputs()[0].name], { input_name: processed_image})[0] prediction = torch.from_numpy(prediction) # Run Coreml model elif self.mlmodel: im = Image.fromarray(image).resize(input_shape) y = self.model.predict({"image":im}) if 'confidence' in y: box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) # xyxy pixels conf, cls = y['confidence'].max(1), y['confidence'].argmax(1).astype(np.float) y = np.concatenate((box, conf.reshape(-1, 1), cls.reshape(-1, 1)), 1) else: k = 'var_' + str(sorted(int(k.replace('var_', '')) for k in y)[-1]) # output key y = y[k] # output width, height = im.size prediction = torch.from_numpy(y) # Run Pytorch model else: processed_image = torch.from_numpy(processed_image).to(self.device) # Change image floating point precision if fp16 set to true processed_image = processed_image.half() if self.fp16 else processed_image.float() with torch.no_grad(): prediction = self.model(processed_image, augment=False) detection = [] # Postprocess prediction if self.mlmodel: detection = process_output(prediction, original_image.shape[:2], [640, 640], conf_thres, iou_thres, mlmodel=True, agnostic=agnostic_nms, max_det=max_det) detection = scale_bboxes(detection, original_image.shape[:2], input_shape) else: detection = process_output(prediction, original_image.shape[:2], processed_image.shape[2:], conf_thres, iou_thres, agnostic=agnostic_nms, max_det=max_det) image_info = { 'width': original_image.shape[1], 'height': original_image.shape[0], } if filter_classes: class_names = get_names() filter_class_idx = [] if filter_classes: for _class in filter_classes: if _class.lower() in class_names: filter_class_idx.append( class_names.index(_class.lower())) else: warnings.warn( f"class {_class} not found in model classes list.") detection = detection[np.in1d( detection[:, 5].astype(int), filter_class_idx)] if return_image: return detection, original_image else: return detection, image_info ================================================ FILE: asone/detectors/yolov9/__init__.py ================================================ from .yolov9_detector import YOLOv9Detector __all__ = ['YOLOv9Detector'] ================================================ FILE: asone/detectors/yolov9/export.py ================================================ import argparse import contextlib import json import os import platform import re import subprocess import sys import time import warnings from pathlib import Path import pandas as pd import torch from torch.utils.mobile_optimizer import optimize_for_mobile FILE = Path(__file__).resolve() ROOT = FILE.parents[0] # YOLO root directory # if str(ROOT) not in sys.path: # sys.path.append(str(ROOT)) # add ROOT to PATH if platform.system() != 'Windows': ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative from asone.detectors.yolov9.yolov9.models.experimental import attempt_load, End2End from asone.detectors.yolov9.yolov9.models.yolo import ClassificationModel, Detect, DDetect, DualDetect, DualDDetect, DetectionModel, SegmentationModel from asone.detectors.yolov9.yolov9.utils.dataloaders import LoadImages from asone.detectors.yolov9.yolov9.utils.general import (LOGGER, Profile, check_dataset, check_img_size, check_requirements, check_version, check_yaml, colorstr, file_size, get_default_args, print_args, url2file, yaml_save) from asone.detectors.yolov9.yolov9.utils.torch_utils import select_device, smart_inference_mode MACOS = platform.system() == 'Darwin' # macOS environment def export_formats(): # YOLO export formats x = [ ['PyTorch', '-', '.pt', True, True], ['TorchScript', 'torchscript', '.torchscript', True, True], ['ONNX', 'onnx', '.onnx', True, True], ['ONNX END2END', 'onnx_end2end', '_end2end.onnx', True, True], ['OpenVINO', 'openvino', '_openvino_model', True, False], ['TensorRT', 'engine', '.engine', False, True], ['CoreML', 'coreml', '.mlmodel', True, False], ['TensorFlow SavedModel', 'saved_model', '_saved_model', True, True], ['TensorFlow GraphDef', 'pb', '.pb', True, True], ['TensorFlow Lite', 'tflite', '.tflite', True, False], ['TensorFlow Edge TPU', 'edgetpu', '_edgetpu.tflite', False, False], ['TensorFlow.js', 'tfjs', '_web_model', False, False], ['PaddlePaddle', 'paddle', '_paddle_model', True, True],] return pd.DataFrame(x, columns=['Format', 'Argument', 'Suffix', 'CPU', 'GPU']) def try_export(inner_func): # YOLO export decorator, i..e @try_export inner_args = get_default_args(inner_func) def outer_func(*args, **kwargs): prefix = inner_args['prefix'] try: with Profile() as dt: f, model = inner_func(*args, **kwargs) LOGGER.info(f'{prefix} export success ✅ {dt.t:.1f}s, saved as {f} ({file_size(f):.1f} MB)') return f, model except Exception as e: LOGGER.info(f'{prefix} export failure ❌ {dt.t:.1f}s: {e}') return None, None return outer_func @try_export def export_torchscript(model, im, file, optimize, prefix=colorstr('TorchScript:')): # YOLO TorchScript model export LOGGER.info(f'\n{prefix} starting export with torch {torch.__version__}...') f = file.with_suffix('.torchscript') ts = torch.jit.trace(model, im, strict=False) d = {"shape": im.shape, "stride": int(max(model.stride)), "names": model.names} extra_files = {'config.txt': json.dumps(d)} # torch._C.ExtraFilesMap() if optimize: # https://pytorch.org/tutorials/recipes/mobile_interpreter.html optimize_for_mobile(ts)._save_for_lite_interpreter(str(f), _extra_files=extra_files) else: ts.save(str(f), _extra_files=extra_files) return f, None @try_export def export_onnx(model, im, file, opset, dynamic, simplify, prefix=colorstr('ONNX:')): # YOLO ONNX export check_requirements('onnx') import onnx LOGGER.info(f'\n{prefix} starting export with onnx {onnx.__version__}...') f = file.with_suffix('.onnx') output_names = ['output0', 'output1'] if isinstance(model, SegmentationModel) else ['output0'] if dynamic: dynamic = {'images': {0: 'batch', 2: 'height', 3: 'width'}} # shape(1,3,640,640) if isinstance(model, SegmentationModel): dynamic['output0'] = {0: 'batch', 1: 'anchors'} # shape(1,25200,85) dynamic['output1'] = {0: 'batch', 2: 'mask_height', 3: 'mask_width'} # shape(1,32,160,160) elif isinstance(model, DetectionModel): dynamic['output0'] = {0: 'batch', 1: 'anchors'} # shape(1,25200,85) torch.onnx.export( model.cpu() if dynamic else model, # --dynamic only compatible with cpu im.cpu() if dynamic else im, f, verbose=False, opset_version=opset, do_constant_folding=True, input_names=['images'], output_names=output_names, dynamic_axes=dynamic or None) # Checks model_onnx = onnx.load(f) # load onnx model onnx.checker.check_model(model_onnx) # check onnx model # Metadata d = {'stride': int(max(model.stride)), 'names': model.names} for k, v in d.items(): meta = model_onnx.metadata_props.add() meta.key, meta.value = k, str(v) onnx.save(model_onnx, f) # Simplify if simplify: try: cuda = torch.cuda.is_available() check_requirements(('onnxruntime-gpu' if cuda else 'onnxruntime', 'onnx-simplifier>=0.4.1')) import onnxsim LOGGER.info(f'{prefix} simplifying with onnx-simplifier {onnxsim.__version__}...') model_onnx, check = onnxsim.simplify(model_onnx) assert check, 'assert check failed' onnx.save(model_onnx, f) except Exception as e: LOGGER.info(f'{prefix} simplifier failure: {e}') return f, model_onnx @try_export def export_onnx_end2end(model, im, file, simplify, topk_all, iou_thres, conf_thres, device, labels, prefix=colorstr('ONNX END2END:')): # YOLO ONNX export check_requirements('onnx') import onnx LOGGER.info(f'\n{prefix} starting export with onnx {onnx.__version__}...') f = os.path.splitext(file)[0] + "-end2end.onnx" batch_size = 'batch' dynamic_axes = {'images': {0 : 'batch', 2: 'height', 3:'width'}, } # variable length axes output_axes = { 'num_dets': {0: 'batch'}, 'det_boxes': {0: 'batch'}, 'det_scores': {0: 'batch'}, 'det_classes': {0: 'batch'}, } dynamic_axes.update(output_axes) model = End2End(model, topk_all, iou_thres, conf_thres, None ,device, labels) output_names = ['num_dets', 'det_boxes', 'det_scores', 'det_classes'] shapes = [ batch_size, 1, batch_size, topk_all, 4, batch_size, topk_all, batch_size, topk_all] torch.onnx.export(model, im, f, verbose=False, export_params=True, # store the trained parameter weights inside the model file opset_version=12, do_constant_folding=True, # whether to execute constant folding for optimization input_names=['images'], output_names=output_names, dynamic_axes=dynamic_axes) # Checks model_onnx = onnx.load(f) # load onnx model onnx.checker.check_model(model_onnx) # check onnx model for i in model_onnx.graph.output: for j in i.type.tensor_type.shape.dim: j.dim_param = str(shapes.pop(0)) if simplify: try: import onnxsim print('\nStarting to simplify ONNX...') model_onnx, check = onnxsim.simplify(model_onnx) assert check, 'assert check failed' except Exception as e: print(f'Simplifier failure: {e}') # print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable model onnx.save(model_onnx,f) print('ONNX export success, saved as %s' % f) return f, model_onnx @try_export def export_openvino(file, metadata, half, prefix=colorstr('OpenVINO:')): # YOLO OpenVINO export check_requirements('openvino-dev') # requires openvino-dev: https://pypi.org/project/openvino-dev/ import openvino.inference_engine as ie LOGGER.info(f'\n{prefix} starting export with openvino {ie.__version__}...') f = str(file).replace('.pt', f'_openvino_model{os.sep}') #cmd = f"mo --input_model {file.with_suffix('.onnx')} --output_dir {f} --data_type {'FP16' if half else 'FP32'}" #cmd = f"mo --input_model {file.with_suffix('.onnx')} --output_dir {f} {"--compress_to_fp16" if half else ""}" half_arg = "--compress_to_fp16" if half else "" cmd = f"mo --input_model {file.with_suffix('.onnx')} --output_dir {f} {half_arg}" subprocess.run(cmd.split(), check=True, env=os.environ) # export yaml_save(Path(f) / file.with_suffix('.yaml').name, metadata) # add metadata.yaml return f, None @try_export def export_paddle(model, im, file, metadata, prefix=colorstr('PaddlePaddle:')): # YOLO Paddle export check_requirements(('paddlepaddle', 'x2paddle')) import x2paddle from x2paddle.convert import pytorch2paddle LOGGER.info(f'\n{prefix} starting export with X2Paddle {x2paddle.__version__}...') f = str(file).replace('.pt', f'_paddle_model{os.sep}') pytorch2paddle(module=model, save_dir=f, jit_type='trace', input_examples=[im]) # export yaml_save(Path(f) / file.with_suffix('.yaml').name, metadata) # add metadata.yaml return f, None @try_export def export_coreml(model, im, file, int8, half, prefix=colorstr('CoreML:')): # YOLO CoreML export check_requirements('coremltools') import coremltools as ct LOGGER.info(f'\n{prefix} starting export with coremltools {ct.__version__}...') f = file.with_suffix('.mlmodel') ts = torch.jit.trace(model, im, strict=False) # TorchScript model ct_model = ct.convert(ts, inputs=[ct.ImageType('image', shape=im.shape, scale=1 / 255, bias=[0, 0, 0])]) bits, mode = (8, 'kmeans_lut') if int8 else (16, 'linear') if half else (32, None) if bits < 32: if MACOS: # quantization only supported on macOS with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) # suppress numpy==1.20 float warning ct_model = ct.models.neural_network.quantization_utils.quantize_weights(ct_model, bits, mode) else: print(f'{prefix} quantization only supported on macOS, skipping...') ct_model.save(f) return f, ct_model @try_export def export_engine(model, im, file, half, dynamic, simplify, workspace=4, verbose=False, prefix=colorstr('TensorRT:')): # YOLO TensorRT export https://developer.nvidia.com/tensorrt assert im.device.type != 'cpu', 'export running on CPU but must be on GPU, i.e. `python export.py --device 0`' try: import tensorrt as trt except Exception: if platform.system() == 'Linux': check_requirements('nvidia-tensorrt', cmds='-U --index-url https://pypi.ngc.nvidia.com') import tensorrt as trt if trt.__version__[0] == '7': # TensorRT 7 handling https://github.com/ultralytics/yolov5/issues/6012 grid = model.model[-1].anchor_grid model.model[-1].anchor_grid = [a[..., :1, :1, :] for a in grid] export_onnx(model, im, file, 12, dynamic, simplify) # opset 12 model.model[-1].anchor_grid = grid else: # TensorRT >= 8 check_version(trt.__version__, '8.0.0', hard=True) # require tensorrt>=8.0.0 export_onnx(model, im, file, 12, dynamic, simplify) # opset 12 onnx = file.with_suffix('.onnx') LOGGER.info(f'\n{prefix} starting export with TensorRT {trt.__version__}...') assert onnx.exists(), f'failed to export ONNX file: {onnx}' f = file.with_suffix('.engine') # TensorRT engine file logger = trt.Logger(trt.Logger.INFO) if verbose: logger.min_severity = trt.Logger.Severity.VERBOSE builder = trt.Builder(logger) config = builder.create_builder_config() config.max_workspace_size = workspace * 1 << 30 # config.set_memory_pool_limit(trt.MemoryPoolType.WORKSPACE, workspace << 30) # fix TRT 8.4 deprecation notice flag = (1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)) network = builder.create_network(flag) parser = trt.OnnxParser(network, logger) if not parser.parse_from_file(str(onnx)): raise RuntimeError(f'failed to load ONNX file: {onnx}') inputs = [network.get_input(i) for i in range(network.num_inputs)] outputs = [network.get_output(i) for i in range(network.num_outputs)] for inp in inputs: LOGGER.info(f'{prefix} input "{inp.name}" with shape{inp.shape} {inp.dtype}') for out in outputs: LOGGER.info(f'{prefix} output "{out.name}" with shape{out.shape} {out.dtype}') if dynamic: if im.shape[0] <= 1: LOGGER.warning(f"{prefix} WARNING ⚠️ --dynamic model requires maximum --batch-size argument") profile = builder.create_optimization_profile() for inp in inputs: profile.set_shape(inp.name, (1, *im.shape[1:]), (max(1, im.shape[0] // 2), *im.shape[1:]), im.shape) config.add_optimization_profile(profile) LOGGER.info(f'{prefix} building FP{16 if builder.platform_has_fast_fp16 and half else 32} engine as {f}') if builder.platform_has_fast_fp16 and half: config.set_flag(trt.BuilderFlag.FP16) with builder.build_engine(network, config) as engine, open(f, 'wb') as t: t.write(engine.serialize()) return f, None @try_export def export_saved_model(model, im, file, dynamic, tf_nms=False, agnostic_nms=False, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, keras=False, prefix=colorstr('TensorFlow SavedModel:')): # YOLO TensorFlow SavedModel export try: import tensorflow as tf except Exception: check_requirements(f"tensorflow{'' if torch.cuda.is_available() else '-macos' if MACOS else '-cpu'}") import tensorflow as tf from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2 from asone.detectors.yolov9.yolov9.models.tf import TFModel LOGGER.info(f'\n{prefix} starting export with tensorflow {tf.__version__}...') f = str(file).replace('.pt', '_saved_model') batch_size, ch, *imgsz = list(im.shape) # BCHW tf_model = TFModel(cfg=model.yaml, model=model, nc=model.nc, imgsz=imgsz) im = tf.zeros((batch_size, *imgsz, ch)) # BHWC order for TensorFlow _ = tf_model.predict(im, tf_nms, agnostic_nms, topk_per_class, topk_all, iou_thres, conf_thres) inputs = tf.keras.Input(shape=(*imgsz, ch), batch_size=None if dynamic else batch_size) outputs = tf_model.predict(inputs, tf_nms, agnostic_nms, topk_per_class, topk_all, iou_thres, conf_thres) keras_model = tf.keras.Model(inputs=inputs, outputs=outputs) keras_model.trainable = False keras_model.summary() if keras: keras_model.save(f, save_format='tf') else: spec = tf.TensorSpec(keras_model.inputs[0].shape, keras_model.inputs[0].dtype) m = tf.function(lambda x: keras_model(x)) # full model m = m.get_concrete_function(spec) frozen_func = convert_variables_to_constants_v2(m) tfm = tf.Module() tfm.__call__ = tf.function(lambda x: frozen_func(x)[:4] if tf_nms else frozen_func(x), [spec]) tfm.__call__(im) tf.saved_model.save(tfm, f, options=tf.saved_model.SaveOptions(experimental_custom_gradients=False) if check_version( tf.__version__, '2.6') else tf.saved_model.SaveOptions()) return f, keras_model @try_export def export_pb(keras_model, file, prefix=colorstr('TensorFlow GraphDef:')): # YOLO TensorFlow GraphDef *.pb export https://github.com/leimao/Frozen_Graph_TensorFlow import tensorflow as tf from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2 LOGGER.info(f'\n{prefix} starting export with tensorflow {tf.__version__}...') f = file.with_suffix('.pb') m = tf.function(lambda x: keras_model(x)) # full model m = m.get_concrete_function(tf.TensorSpec(keras_model.inputs[0].shape, keras_model.inputs[0].dtype)) frozen_func = convert_variables_to_constants_v2(m) frozen_func.graph.as_graph_def() tf.io.write_graph(graph_or_graph_def=frozen_func.graph, logdir=str(f.parent), name=f.name, as_text=False) return f, None @try_export def export_tflite(keras_model, im, file, int8, data, nms, agnostic_nms, prefix=colorstr('TensorFlow Lite:')): # YOLOv5 TensorFlow Lite export import tensorflow as tf LOGGER.info(f'\n{prefix} starting export with tensorflow {tf.__version__}...') batch_size, ch, *imgsz = list(im.shape) # BCHW f = str(file).replace('.pt', '-fp16.tflite') converter = tf.lite.TFLiteConverter.from_keras_model(keras_model) converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS] converter.target_spec.supported_types = [tf.float16] converter.optimizations = [tf.lite.Optimize.DEFAULT] if int8: from asone.detectors.yolov9.yolov9.models.tf import representative_dataset_gen dataset = LoadImages(check_dataset(check_yaml(data))['train'], img_size=imgsz, auto=False) converter.representative_dataset = lambda: representative_dataset_gen(dataset, ncalib=100) converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8] converter.target_spec.supported_types = [] converter.inference_input_type = tf.uint8 # or tf.int8 converter.inference_output_type = tf.uint8 # or tf.int8 converter.experimental_new_quantizer = True f = str(file).replace('.pt', '-int8.tflite') if nms or agnostic_nms: converter.target_spec.supported_ops.append(tf.lite.OpsSet.SELECT_TF_OPS) tflite_model = converter.convert() open(f, "wb").write(tflite_model) return f, None @try_export def export_edgetpu(file, prefix=colorstr('Edge TPU:')): # YOLO Edge TPU export https://coral.ai/docs/edgetpu/models-intro/ cmd = 'edgetpu_compiler --version' help_url = 'https://coral.ai/docs/edgetpu/compiler/' assert platform.system() == 'Linux', f'export only supported on Linux. See {help_url}' if subprocess.run(f'{cmd} >/dev/null', shell=True).returncode != 0: LOGGER.info(f'\n{prefix} export requires Edge TPU compiler. Attempting install from {help_url}') sudo = subprocess.run('sudo --version >/dev/null', shell=True).returncode == 0 # sudo installed on system for c in ( 'curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -', 'echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list', 'sudo apt-get update', 'sudo apt-get install edgetpu-compiler'): subprocess.run(c if sudo else c.replace('sudo ', ''), shell=True, check=True) ver = subprocess.run(cmd, shell=True, capture_output=True, check=True).stdout.decode().split()[-1] LOGGER.info(f'\n{prefix} starting export with Edge TPU compiler {ver}...') f = str(file).replace('.pt', '-int8_edgetpu.tflite') # Edge TPU model f_tfl = str(file).replace('.pt', '-int8.tflite') # TFLite model cmd = f"edgetpu_compiler -s -d -k 10 --out_dir {file.parent} {f_tfl}" subprocess.run(cmd.split(), check=True) return f, None @try_export def export_tfjs(file, prefix=colorstr('TensorFlow.js:')): # YOLO TensorFlow.js export check_requirements('tensorflowjs') import tensorflowjs as tfjs LOGGER.info(f'\n{prefix} starting export with tensorflowjs {tfjs.__version__}...') f = str(file).replace('.pt', '_web_model') # js dir f_pb = file.with_suffix('.pb') # *.pb path f_json = f'{f}/model.json' # *.json path cmd = f'tensorflowjs_converter --input_format=tf_frozen_model ' \ f'--output_node_names=Identity,Identity_1,Identity_2,Identity_3 {f_pb} {f}' subprocess.run(cmd.split()) json = Path(f_json).read_text() with open(f_json, 'w') as j: # sort JSON Identity_* in ascending order subst = re.sub( r'{"outputs": {"Identity.?.?": {"name": "Identity.?.?"}, ' r'"Identity.?.?": {"name": "Identity.?.?"}, ' r'"Identity.?.?": {"name": "Identity.?.?"}, ' r'"Identity.?.?": {"name": "Identity.?.?"}}}', r'{"outputs": {"Identity": {"name": "Identity"}, ' r'"Identity_1": {"name": "Identity_1"}, ' r'"Identity_2": {"name": "Identity_2"}, ' r'"Identity_3": {"name": "Identity_3"}}}', json) j.write(subst) return f, None def add_tflite_metadata(file, metadata, num_outputs): # Add metadata to *.tflite models per https://www.tensorflow.org/lite/models/convert/metadata with contextlib.suppress(ImportError): # check_requirements('tflite_support') from tflite_support import flatbuffers from tflite_support import metadata as _metadata from tflite_support import metadata_schema_py_generated as _metadata_fb tmp_file = Path('/tmp/meta.txt') with open(tmp_file, 'w') as meta_f: meta_f.write(str(metadata)) model_meta = _metadata_fb.ModelMetadataT() label_file = _metadata_fb.AssociatedFileT() label_file.name = tmp_file.name model_meta.associatedFiles = [label_file] subgraph = _metadata_fb.SubGraphMetadataT() subgraph.inputTensorMetadata = [_metadata_fb.TensorMetadataT()] subgraph.outputTensorMetadata = [_metadata_fb.TensorMetadataT()] * num_outputs model_meta.subgraphMetadata = [subgraph] b = flatbuffers.Builder(0) b.Finish(model_meta.Pack(b), _metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER) metadata_buf = b.Output() populator = _metadata.MetadataPopulator.with_model_file(file) populator.load_metadata_buffer(metadata_buf) populator.load_associated_files([str(tmp_file)]) populator.populate() tmp_file.unlink() @smart_inference_mode() def run( data=ROOT / 'data/coco.yaml', # 'dataset.yaml path' weights=ROOT / 'yolo.pt', # weights path imgsz=(640, 640), # image (height, width) batch_size=1, # batch size device='cpu', # cuda device, i.e. 0 or 0,1,2,3 or cpu include=('torchscript', 'onnx'), # include formats half=False, # FP16 half-precision export inplace=False, # set YOLO Detect() inplace=True keras=False, # use Keras optimize=False, # TorchScript: optimize for mobile int8=False, # CoreML/TF INT8 quantization dynamic=False, # ONNX/TF/TensorRT: dynamic axes simplify=False, # ONNX: simplify model opset=12, # ONNX: opset version verbose=False, # TensorRT: verbose log workspace=4, # TensorRT: workspace size (GB) nms=False, # TF: add NMS to model agnostic_nms=False, # TF: add agnostic NMS to model topk_per_class=100, # TF.js NMS: topk per class to keep topk_all=100, # TF.js NMS: topk for all classes to keep iou_thres=0.45, # TF.js NMS: IoU threshold conf_thres=0.25, # TF.js NMS: confidence threshold ): t = time.time() include = [x.lower() for x in include] # to lowercase fmts = tuple(export_formats()['Argument'][1:]) # --include arguments flags = [x in include for x in fmts] assert sum(flags) == len(include), f'ERROR: Invalid --include {include}, valid --include arguments are {fmts}' jit, onnx, onnx_end2end, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle = flags # export booleans file = Path(url2file(weights) if str(weights).startswith(('http:/', 'https:/')) else weights) # PyTorch weights # Load PyTorch model device = select_device(device) if half: assert device.type != 'cpu' or coreml, '--half only compatible with GPU export, i.e. use --device 0' assert not dynamic, '--half not compatible with --dynamic, i.e. use either --half or --dynamic but not both' model = attempt_load(weights, device=device, inplace=True, fuse=True) # load FP32 model # Checks imgsz *= 2 if len(imgsz) == 1 else 1 # expand if optimize: assert device.type == 'cpu', '--optimize not compatible with cuda devices, i.e. use --device cpu' # Input gs = int(max(model.stride)) # grid size (max stride) imgsz = [check_img_size(x, gs) for x in imgsz] # verify img_size are gs-multiples im = torch.zeros(batch_size, 3, *imgsz).to(device) # image size(1,3,320,192) BCHW iDetection # Update model model.eval() for k, m in model.named_modules(): if isinstance(m, (Detect, DDetect, DualDetect, DualDDetect)): m.inplace = inplace m.dynamic = dynamic m.export = True for _ in range(2): y = model(im) # dry runs if half and not coreml: im, model = im.half(), model.half() # to FP16 shape = tuple((y[0] if isinstance(y, (tuple, list)) else y).shape) # model output shape metadata = {'stride': int(max(model.stride)), 'names': model.names} # model metadata LOGGER.info(f"\n{colorstr('PyTorch:')} starting from {file} with output shape {shape} ({file_size(file):.1f} MB)") # Exports f = [''] * len(fmts) # exported filenames warnings.filterwarnings(action='ignore', category=torch.jit.TracerWarning) # suppress TracerWarning if jit: # TorchScript f[0], _ = export_torchscript(model, im, file, optimize) if engine: # TensorRT required before ONNX f[1], _ = export_engine(model, im, file, half, dynamic, simplify, workspace, verbose) if onnx or xml: # OpenVINO requires ONNX f[2], _ = export_onnx(model, im, file, opset, dynamic, simplify) if onnx_end2end: if isinstance(model, DetectionModel): labels = model.names f[2], _ = export_onnx_end2end(model, im, file, simplify, topk_all, iou_thres, conf_thres, device, len(labels)) else: raise RuntimeError("The model is not a DetectionModel.") if xml: # OpenVINO f[3], _ = export_openvino(file, metadata, half) if coreml: # CoreML f[4], _ = export_coreml(model, im, file, int8, half) if any((saved_model, pb, tflite, edgetpu, tfjs)): # TensorFlow formats assert not tflite or not tfjs, 'TFLite and TF.js models must be exported separately, please pass only one type.' assert not isinstance(model, ClassificationModel), 'ClassificationModel export to TF formats not yet supported.' f[5], s_model = export_saved_model(model.cpu(), im, file, dynamic, tf_nms=nms or agnostic_nms or tfjs, agnostic_nms=agnostic_nms or tfjs, topk_per_class=topk_per_class, topk_all=topk_all, iou_thres=iou_thres, conf_thres=conf_thres, keras=keras) if pb or tfjs: # pb prerequisite to tfjs f[6], _ = export_pb(s_model, file) if tflite or edgetpu: f[7], _ = export_tflite(s_model, im, file, int8 or edgetpu, data=data, nms=nms, agnostic_nms=agnostic_nms) if edgetpu: f[8], _ = export_edgetpu(file) add_tflite_metadata(f[8] or f[7], metadata, num_outputs=len(s_model.outputs)) if tfjs: f[9], _ = export_tfjs(file) if paddle: # PaddlePaddle f[10], _ = export_paddle(model, im, file, metadata) # Finish f = [str(x) for x in f if x] # filter out '' and None if any(f): cls, det, seg = (isinstance(model, x) for x in (ClassificationModel, DetectionModel, SegmentationModel)) # type dir = Path('segment' if seg else 'classify' if cls else '') h = '--half' if half else '' # --half FP16 inference arg s = "# WARNING ⚠️ ClassificationModel not yet supported for PyTorch Hub AutoShape inference" if cls else \ "# WARNING ⚠️ SegmentationModel not yet supported for PyTorch Hub AutoShape inference" if seg else '' if onnx_end2end: LOGGER.info(f'\nExport complete ({time.time() - t:.1f}s)' f"\nResults saved to {colorstr('bold', file.parent.resolve())}" f"\nVisualize: https://netron.app") else: LOGGER.info(f'\nExport complete ({time.time() - t:.1f}s)' f"\nResults saved to {colorstr('bold', file.parent.resolve())}" f"\nDetect: python {dir / ('detect.py' if det else 'predict.py')} --weights {f[-1]} {h}" f"\nValidate: python {dir / 'val.py'} --weights {f[-1]} {h}" f"\nPyTorch Hub: model = torch.hub.load('ultralytics/yolov5', 'custom', '{f[-1]}') {s}" f"\nVisualize: https://netron.app") return f # return list of exported files/dirs def parse_opt(): parser = argparse.ArgumentParser() parser.add_argument('--data', type=str, default=ROOT / 'data/coco.yaml', help='dataset.yaml path') parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolo.pt', help='model.pt path(s)') parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640, 640], help='image (h, w)') parser.add_argument('--batch-size', type=int, default=1, help='batch size') parser.add_argument('--device', default='cpu', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') parser.add_argument('--half', action='store_true', help='FP16 half-precision export') parser.add_argument('--inplace', action='store_true', help='set YOLO Detect() inplace=True') parser.add_argument('--keras', action='store_true', help='TF: use Keras') parser.add_argument('--optimize', action='store_true', help='TorchScript: optimize for mobile') parser.add_argument('--int8', action='store_true', help='CoreML/TF INT8 quantization') parser.add_argument('--dynamic', action='store_true', help='ONNX/TF/TensorRT: dynamic axes') parser.add_argument('--simplify', action='store_true', help='ONNX: simplify model') parser.add_argument('--opset', type=int, default=12, help='ONNX: opset version') parser.add_argument('--verbose', action='store_true', help='TensorRT: verbose log') parser.add_argument('--workspace', type=int, default=4, help='TensorRT: workspace size (GB)') parser.add_argument('--nms', action='store_true', help='TF: add NMS to model') parser.add_argument('--agnostic-nms', action='store_true', help='TF: add agnostic NMS to model') parser.add_argument('--topk-per-class', type=int, default=100, help='TF.js NMS: topk per class to keep') parser.add_argument('--topk-all', type=int, default=100, help='ONNX END2END/TF.js NMS: topk for all classes to keep') parser.add_argument('--iou-thres', type=float, default=0.45, help='ONNX END2END/TF.js NMS: IoU threshold') parser.add_argument('--conf-thres', type=float, default=0.25, help='ONNX END2END/TF.js NMS: confidence threshold') parser.add_argument( '--include', nargs='+', default=['torchscript'], help='torchscript, onnx, onnx_end2end, openvino, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle') opt = parser.parse_args() if 'onnx_end2end' in opt.include: opt.simplify = True opt.dynamic = True opt.inplace = True opt.half = False print_args(vars(opt)) return opt def main(opt): for opt.weights in (opt.weights if isinstance(opt.weights, list) else [opt.weights]): run(**vars(opt)) if __name__ == "__main__": opt = parse_opt() main(opt) ================================================ FILE: asone/detectors/yolov9/yolov9/__init__.py ================================================ ================================================ FILE: asone/detectors/yolov9/yolov9/models/__init__.py ================================================ # init ================================================ FILE: asone/detectors/yolov9/yolov9/models/common.py ================================================ import ast import contextlib import json import math import platform import warnings import zipfile from collections import OrderedDict, namedtuple from copy import copy from pathlib import Path from urllib.parse import urlparse from typing import Optional import cv2 import numpy as np import pandas as pd import requests import torch import torch.nn as nn from IPython.display import display from PIL import Image from torch.cuda import amp from asone.detectors.yolov9.yolov9.utils import TryExcept from asone.detectors.yolov9.yolov9.utils.dataloaders import exif_transpose, letterbox from asone.detectors.yolov9.yolov9.utils.general import (LOGGER, ROOT, Profile, check_requirements, check_suffix, check_version, colorstr, increment_path, is_notebook, make_divisible, non_max_suppression, scale_boxes, xywh2xyxy, xyxy2xywh, yaml_load) from asone.detectors.yolov9.yolov9.utils.plots import Annotator, colors, save_one_box from asone.detectors.yolov9.yolov9.utils.torch_utils import copy_attr, smart_inference_mode def autopad(k, p=None, d=1): # kernel, padding, dilation # Pad to 'same' shape outputs if d > 1: k = d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k] # actual kernel-size if p is None: p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad return p class Conv(nn.Module): # Standard convolution with args(ch_in, ch_out, kernel, stride, padding, groups, dilation, activation) default_act = nn.SiLU() # default activation def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True): super().__init__() self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False) self.bn = nn.BatchNorm2d(c2) self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity() def forward(self, x): return self.act(self.bn(self.conv(x))) def forward_fuse(self, x): return self.act(self.conv(x)) class AConv(nn.Module): def __init__(self, c1, c2): # ch_in, ch_out, shortcut, kernels, groups, expand super().__init__() self.cv1 = Conv(c1, c2, 3, 2, 1) def forward(self, x): x = torch.nn.functional.avg_pool2d(x, 2, 1, 0, False, True) return self.cv1(x) class ADown(nn.Module): def __init__(self, c1, c2): # ch_in, ch_out, shortcut, kernels, groups, expand super().__init__() self.c = c2 // 2 self.cv1 = Conv(c1 // 2, self.c, 3, 2, 1) self.cv2 = Conv(c1 // 2, self.c, 1, 1, 0) def forward(self, x): x = torch.nn.functional.avg_pool2d(x, 2, 1, 0, False, True) x1,x2 = x.chunk(2, 1) x1 = self.cv1(x1) x2 = torch.nn.functional.max_pool2d(x2, 3, 2, 1) x2 = self.cv2(x2) return torch.cat((x1, x2), 1) class RepConvN(nn.Module): """RepConv is a basic rep-style block, including training and deploy status This code is based on https://github.com/DingXiaoH/RepVGG/blob/main/repvgg.py """ default_act = nn.SiLU() # default activation def __init__(self, c1, c2, k=3, s=1, p=1, g=1, d=1, act=True, bn=False, deploy=False): super().__init__() assert k == 3 and p == 1 self.g = g self.c1 = c1 self.c2 = c2 self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity() self.bn = None self.conv1 = Conv(c1, c2, k, s, p=p, g=g, act=False) self.conv2 = Conv(c1, c2, 1, s, p=(p - k // 2), g=g, act=False) def forward_fuse(self, x): """Forward process""" return self.act(self.conv(x)) def forward(self, x): """Forward process""" id_out = 0 if self.bn is None else self.bn(x) return self.act(self.conv1(x) + self.conv2(x) + id_out) def get_equivalent_kernel_bias(self): kernel3x3, bias3x3 = self._fuse_bn_tensor(self.conv1) kernel1x1, bias1x1 = self._fuse_bn_tensor(self.conv2) kernelid, biasid = self._fuse_bn_tensor(self.bn) return kernel3x3 + self._pad_1x1_to_3x3_tensor(kernel1x1) + kernelid, bias3x3 + bias1x1 + biasid def _avg_to_3x3_tensor(self, avgp): channels = self.c1 groups = self.g kernel_size = avgp.kernel_size input_dim = channels // groups k = torch.zeros((channels, input_dim, kernel_size, kernel_size)) k[np.arange(channels), np.tile(np.arange(input_dim), groups), :, :] = 1.0 / kernel_size ** 2 return k def _pad_1x1_to_3x3_tensor(self, kernel1x1): if kernel1x1 is None: return 0 else: return torch.nn.functional.pad(kernel1x1, [1, 1, 1, 1]) def _fuse_bn_tensor(self, branch): if branch is None: return 0, 0 if isinstance(branch, Conv): kernel = branch.conv.weight running_mean = branch.bn.running_mean running_var = branch.bn.running_var gamma = branch.bn.weight beta = branch.bn.bias eps = branch.bn.eps elif isinstance(branch, nn.BatchNorm2d): if not hasattr(self, 'id_tensor'): input_dim = self.c1 // self.g kernel_value = np.zeros((self.c1, input_dim, 3, 3), dtype=np.float32) for i in range(self.c1): kernel_value[i, i % input_dim, 1, 1] = 1 self.id_tensor = torch.from_numpy(kernel_value).to(branch.weight.device) kernel = self.id_tensor running_mean = branch.running_mean running_var = branch.running_var gamma = branch.weight beta = branch.bias eps = branch.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta - running_mean * gamma / std def fuse_convs(self): if hasattr(self, 'conv'): return kernel, bias = self.get_equivalent_kernel_bias() self.conv = nn.Conv2d(in_channels=self.conv1.conv.in_channels, out_channels=self.conv1.conv.out_channels, kernel_size=self.conv1.conv.kernel_size, stride=self.conv1.conv.stride, padding=self.conv1.conv.padding, dilation=self.conv1.conv.dilation, groups=self.conv1.conv.groups, bias=True).requires_grad_(False) self.conv.weight.data = kernel self.conv.bias.data = bias for para in self.parameters(): para.detach_() self.__delattr__('conv1') self.__delattr__('conv2') if hasattr(self, 'nm'): self.__delattr__('nm') if hasattr(self, 'bn'): self.__delattr__('bn') if hasattr(self, 'id_tensor'): self.__delattr__('id_tensor') class SP(nn.Module): def __init__(self, k=3, s=1): super(SP, self).__init__() self.m = nn.MaxPool2d(kernel_size=k, stride=s, padding=k // 2) def forward(self, x): return self.m(x) class MP(nn.Module): # Max pooling def __init__(self, k=2): super(MP, self).__init__() self.m = nn.MaxPool2d(kernel_size=k, stride=k) def forward(self, x): return self.m(x) class ConvTranspose(nn.Module): # Convolution transpose 2d layer default_act = nn.SiLU() # default activation def __init__(self, c1, c2, k=2, s=2, p=0, bn=True, act=True): super().__init__() self.conv_transpose = nn.ConvTranspose2d(c1, c2, k, s, p, bias=not bn) self.bn = nn.BatchNorm2d(c2) if bn else nn.Identity() self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity() def forward(self, x): return self.act(self.bn(self.conv_transpose(x))) class DWConv(Conv): # Depth-wise convolution def __init__(self, c1, c2, k=1, s=1, d=1, act=True): # ch_in, ch_out, kernel, stride, dilation, activation super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), d=d, act=act) class DWConvTranspose2d(nn.ConvTranspose2d): # Depth-wise transpose convolution def __init__(self, c1, c2, k=1, s=1, p1=0, p2=0): # ch_in, ch_out, kernel, stride, padding, padding_out super().__init__(c1, c2, k, s, p1, p2, groups=math.gcd(c1, c2)) class DFL(nn.Module): # DFL module def __init__(self, c1=17): super().__init__() self.conv = nn.Conv2d(c1, 1, 1, bias=False).requires_grad_(False) self.conv.weight.data[:] = nn.Parameter(torch.arange(c1, dtype=torch.float).view(1, c1, 1, 1)) # / 120.0 self.c1 = c1 # self.bn = nn.BatchNorm2d(4) def forward(self, x): b, c, a = x.shape # batch, channels, anchors return self.conv(x.view(b, 4, self.c1, a).transpose(2, 1).softmax(1)).view(b, 4, a) # return self.conv(x.view(b, self.c1, 4, a).softmax(1)).view(b, 4, a) class BottleneckBase(nn.Module): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, k=(1, 3), e=0.5): # ch_in, ch_out, shortcut, kernels, groups, expand super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, k[0], 1) self.cv2 = Conv(c_, c2, k[1], 1, g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class RBottleneckBase(nn.Module): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 1), e=0.5): # ch_in, ch_out, shortcut, kernels, groups, expand super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, k[0], 1) self.cv2 = Conv(c_, c2, k[1], 1, g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class RepNRBottleneckBase(nn.Module): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 1), e=0.5): # ch_in, ch_out, shortcut, kernels, groups, expand super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = RepConvN(c1, c_, k[0], 1) self.cv2 = Conv(c_, c2, k[1], 1, g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class Bottleneck(nn.Module): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 3), e=0.5): # ch_in, ch_out, shortcut, kernels, groups, expand super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, k[0], 1) self.cv2 = Conv(c_, c2, k[1], 1, g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class RepNBottleneck(nn.Module): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 3), e=0.5): # ch_in, ch_out, shortcut, kernels, groups, expand super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = RepConvN(c1, c_, k[0], 1) self.cv2 = Conv(c_, c2, k[1], 1, g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class Res(nn.Module): # ResNet bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super(Res, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c_, 3, 1, g=g) self.cv3 = Conv(c_, c2, 1, 1) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv3(self.cv2(self.cv1(x))) if self.add else self.cv3(self.cv2(self.cv1(x))) class RepNRes(nn.Module): # ResNet bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super(RepNRes, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = RepConvN(c_, c_, 3, 1, g=g) self.cv3 = Conv(c_, c2, 1, 1) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv3(self.cv2(self.cv1(x))) if self.add else self.cv3(self.cv2(self.cv1(x))) class BottleneckCSP(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) self.cv4 = Conv(2 * c_, c2, 1, 1) self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) self.act = nn.SiLU() self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n))) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(self.act(self.bn(torch.cat((y1, y2), 1)))) class CSP(nn.Module): # CSP Bottleneck with 3 convolutions def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2) self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n))) def forward(self, x): return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1)) class RepNCSP(nn.Module): # CSP Bottleneck with 3 convolutions def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2) self.m = nn.Sequential(*(RepNBottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n))) def forward(self, x): return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1)) class CSPBase(nn.Module): # CSP Bottleneck with 3 convolutions def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2) self.m = nn.Sequential(*(BottleneckBase(c_, c_, shortcut, g, e=1.0) for _ in range(n))) def forward(self, x): return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1)) class SPP(nn.Module): # Spatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729 def __init__(self, c1, c2, k=(5, 9, 13)): super().__init__() c_ = c1 // 2 # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1) self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) def forward(self, x): x = self.cv1(x) with warnings.catch_warnings(): warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1)) class ASPP(torch.nn.Module): def __init__(self, in_channels, out_channels): super().__init__() kernel_sizes = [1, 3, 3, 1] dilations = [1, 3, 6, 1] paddings = [0, 3, 6, 0] self.aspp = torch.nn.ModuleList() for aspp_idx in range(len(kernel_sizes)): conv = torch.nn.Conv2d( in_channels, out_channels, kernel_size=kernel_sizes[aspp_idx], stride=1, dilation=dilations[aspp_idx], padding=paddings[aspp_idx], bias=True) self.aspp.append(conv) self.gap = torch.nn.AdaptiveAvgPool2d(1) self.aspp_num = len(kernel_sizes) for m in self.modules(): if isinstance(m, torch.nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) m.bias.data.fill_(0) def forward(self, x): avg_x = self.gap(x) out = [] for aspp_idx in range(self.aspp_num): inp = avg_x if (aspp_idx == self.aspp_num - 1) else x out.append(F.relu_(self.aspp[aspp_idx](inp))) out[-1] = out[-1].expand_as(out[-2]) out = torch.cat(out, dim=1) return out class SPPCSPC(nn.Module): # CSP SPP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)): super(SPPCSPC, self).__init__() c_ = int(2 * c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(c_, c_, 3, 1) self.cv4 = Conv(c_, c_, 1, 1) self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) self.cv5 = Conv(4 * c_, c_, 1, 1) self.cv6 = Conv(c_, c_, 3, 1) self.cv7 = Conv(2 * c_, c2, 1, 1) def forward(self, x): x1 = self.cv4(self.cv3(self.cv1(x))) y1 = self.cv6(self.cv5(torch.cat([x1] + [m(x1) for m in self.m], 1))) y2 = self.cv2(x) return self.cv7(torch.cat((y1, y2), dim=1)) class SPPF(nn.Module): # Spatial Pyramid Pooling - Fast (SPPF) layer by Glenn Jocher def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13)) super().__init__() c_ = c1 // 2 # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_ * 4, c2, 1, 1) self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2) # self.m = SoftPool2d(kernel_size=k, stride=1, padding=k // 2) def forward(self, x): x = self.cv1(x) with warnings.catch_warnings(): warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning y1 = self.m(x) y2 = self.m(y1) return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1)) import torch.nn.functional as F from torch.nn.modules.utils import _pair class ReOrg(nn.Module): # yolo def __init__(self): super(ReOrg, self).__init__() def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2) return torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1) class Contract(nn.Module): # Contract width-height into channels, i.e. x(1,64,80,80) to x(1,256,40,40) def __init__(self, gain=2): super().__init__() self.gain = gain def forward(self, x): b, c, h, w = x.size() # assert (h / s == 0) and (W / s == 0), 'Indivisible gain' s = self.gain x = x.view(b, c, h // s, s, w // s, s) # x(1,64,40,2,40,2) x = x.permute(0, 3, 5, 1, 2, 4).contiguous() # x(1,2,2,64,40,40) return x.view(b, c * s * s, h // s, w // s) # x(1,256,40,40) class Expand(nn.Module): # Expand channels into width-height, i.e. x(1,64,80,80) to x(1,16,160,160) def __init__(self, gain=2): super().__init__() self.gain = gain def forward(self, x): b, c, h, w = x.size() # assert C / s ** 2 == 0, 'Indivisible gain' s = self.gain x = x.view(b, s, s, c // s ** 2, h, w) # x(1,2,2,16,80,80) x = x.permute(0, 3, 4, 1, 5, 2).contiguous() # x(1,16,80,2,80,2) return x.view(b, c // s ** 2, h * s, w * s) # x(1,16,160,160) class Concat(nn.Module): # Concatenate a list of tensors along dimension def __init__(self, dimension=1): super().__init__() self.d = dimension def forward(self, x): return torch.cat(x, self.d) class Shortcut(nn.Module): def __init__(self, dimension=0): super(Shortcut, self).__init__() self.d = dimension def forward(self, x): return x[0]+x[1] class Silence(nn.Module): def __init__(self): super(Silence, self).__init__() def forward(self, x): return x ##### GELAN ##### class SPPELAN(nn.Module): # spp-elan def __init__(self, c1, c2, c3): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() self.c = c3 self.cv1 = Conv(c1, c3, 1, 1) self.cv2 = SP(5) self.cv3 = SP(5) self.cv4 = SP(5) self.cv5 = Conv(4*c3, c2, 1, 1) def forward(self, x): y = [self.cv1(x)] y.extend(m(y[-1]) for m in [self.cv2, self.cv3, self.cv4]) return self.cv5(torch.cat(y, 1)) class RepNCSPELAN4(nn.Module): # csp-elan def __init__(self, c1, c2, c3, c4, c5=1): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() self.c = c3//2 self.cv1 = Conv(c1, c3, 1, 1) self.cv2 = nn.Sequential(RepNCSP(c3//2, c4, c5), Conv(c4, c4, 3, 1)) self.cv3 = nn.Sequential(RepNCSP(c4, c4, c5), Conv(c4, c4, 3, 1)) self.cv4 = Conv(c3+(2*c4), c2, 1, 1) def forward(self, x): y = list(self.cv1(x).chunk(2, 1)) y.extend((m(y[-1])) for m in [self.cv2, self.cv3]) return self.cv4(torch.cat(y, 1)) def forward_split(self, x): y = list(self.cv1(x).split((self.c, self.c), 1)) y.extend(m(y[-1]) for m in [self.cv2, self.cv3]) return self.cv4(torch.cat(y, 1)) ################# ##### YOLOR ##### class ImplicitA(nn.Module): def __init__(self, channel): super(ImplicitA, self).__init__() self.channel = channel self.implicit = nn.Parameter(torch.zeros(1, channel, 1, 1)) nn.init.normal_(self.implicit, std=.02) def forward(self, x): return self.implicit + x class ImplicitM(nn.Module): def __init__(self, channel): super(ImplicitM, self).__init__() self.channel = channel self.implicit = nn.Parameter(torch.ones(1, channel, 1, 1)) nn.init.normal_(self.implicit, mean=1., std=.02) def forward(self, x): return self.implicit * x ################# ##### CBNet ##### class CBLinear(nn.Module): def __init__(self, c1, c2s, k=1, s=1, p=None, g=1): # ch_in, ch_outs, kernel, stride, padding, groups super(CBLinear, self).__init__() self.c2s = c2s self.conv = nn.Conv2d(c1, sum(c2s), k, s, autopad(k, p), groups=g, bias=True) def forward(self, x): outs = self.conv(x).split(self.c2s, dim=1) return outs class CBFuse(nn.Module): def __init__(self, idx): super(CBFuse, self).__init__() self.idx = idx def forward(self, xs): target_size = xs[-1].shape[2:] res = [F.interpolate(x[self.idx[i]], size=target_size, mode='nearest') for i, x in enumerate(xs[:-1])] out = torch.sum(torch.stack(res + xs[-1:]), dim=0) return out ################# class DetectMultiBackend(nn.Module): # YOLO MultiBackend class for python inference on various backends def __init__(self, weights='yolo.pt', device=torch.device('cpu'), dnn=False, data=None, fp16=False, fuse=True): # Usage: # PyTorch: weights = *.pt # TorchScript: *.torchscript # ONNX Runtime: *.onnx # ONNX OpenCV DNN: *.onnx --dnn # OpenVINO: *_openvino_model # CoreML: *.mlmodel # TensorRT: *.engine # TensorFlow SavedModel: *_saved_model # TensorFlow GraphDef: *.pb # TensorFlow Lite: *.tflite # TensorFlow Edge TPU: *_edgetpu.tflite # PaddlePaddle: *_paddle_model from asone.detectors.yolov9.yolov9.models.experimental import attempt_download, attempt_load # scoped to avoid circular import super().__init__() w = str(weights[0] if isinstance(weights, list) else weights) pt, jit, onnx, onnx_end2end, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle, triton = self._model_type(w) fp16 &= pt or jit or onnx or engine # FP16 nhwc = coreml or saved_model or pb or tflite or edgetpu # BHWC formats (vs torch BCWH) stride = 32 # default stride cuda = torch.cuda.is_available() and device.type != 'cpu' # use CUDA if not (pt or triton): w = attempt_download(w) # download if not local if pt: # PyTorch model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse) stride = max(int(model.stride.max()), 32) # model stride names = model.module.names if hasattr(model, 'module') else model.names # get class names model.half() if fp16 else model.float() self.model = model # explicitly assign for to(), cpu(), cuda(), half() elif jit: # TorchScript LOGGER.info(f'Loading {w} for TorchScript inference...') extra_files = {'config.txt': ''} # model metadata model = torch.jit.load(w, _extra_files=extra_files, map_location=device) model.half() if fp16 else model.float() if extra_files['config.txt']: # load metadata dict d = json.loads(extra_files['config.txt'], object_hook=lambda d: {int(k) if k.isdigit() else k: v for k, v in d.items()}) stride, names = int(d['stride']), d['names'] elif dnn: # ONNX OpenCV DNN LOGGER.info(f'Loading {w} for ONNX OpenCV DNN inference...') check_requirements('opencv-python>=4.5.4') net = cv2.dnn.readNetFromONNX(w) elif onnx: # ONNX Runtime LOGGER.info(f'Loading {w} for ONNX Runtime inference...') check_requirements(('onnx', 'onnxruntime-gpu' if cuda else 'onnxruntime')) import onnxruntime providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider'] session = onnxruntime.InferenceSession(w, providers=providers) output_names = [x.name for x in session.get_outputs()] meta = session.get_modelmeta().custom_metadata_map # metadata if 'stride' in meta: stride, names = int(meta['stride']), eval(meta['names']) elif xml: # OpenVINO LOGGER.info(f'Loading {w} for OpenVINO inference...') check_requirements('openvino') # requires openvino-dev: https://pypi.org/project/openvino-dev/ from openvino.runtime import Core, Layout, get_batch ie = Core() if not Path(w).is_file(): # if not *.xml w = next(Path(w).glob('*.xml')) # get *.xml file from *_openvino_model dir network = ie.read_model(model=w, weights=Path(w).with_suffix('.bin')) if network.get_parameters()[0].get_layout().empty: network.get_parameters()[0].set_layout(Layout("NCHW")) batch_dim = get_batch(network) if batch_dim.is_static: batch_size = batch_dim.get_length() executable_network = ie.compile_model(network, device_name="CPU") # device_name="MYRIAD" for Intel NCS2 stride, names = self._load_metadata(Path(w).with_suffix('.yaml')) # load metadata elif engine: # TensorRT LOGGER.info(f'Loading {w} for TensorRT inference...') import tensorrt as trt # https://developer.nvidia.com/nvidia-tensorrt-download check_version(trt.__version__, '7.0.0', hard=True) # require tensorrt>=7.0.0 if device.type == 'cpu': device = torch.device('cuda:0') Binding = namedtuple('Binding', ('name', 'dtype', 'shape', 'data', 'ptr')) logger = trt.Logger(trt.Logger.INFO) with open(w, 'rb') as f, trt.Runtime(logger) as runtime: model = runtime.deserialize_cuda_engine(f.read()) context = model.create_execution_context() bindings = OrderedDict() output_names = [] fp16 = False # default updated below dynamic = False for i in range(model.num_bindings): name = model.get_binding_name(i) dtype = trt.nptype(model.get_binding_dtype(i)) if model.binding_is_input(i): if -1 in tuple(model.get_binding_shape(i)): # dynamic dynamic = True context.set_binding_shape(i, tuple(model.get_profile_shape(0, i)[2])) if dtype == np.float16: fp16 = True else: # output output_names.append(name) shape = tuple(context.get_binding_shape(i)) im = torch.from_numpy(np.empty(shape, dtype=dtype)).to(device) bindings[name] = Binding(name, dtype, shape, im, int(im.data_ptr())) binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items()) batch_size = bindings['images'].shape[0] # if dynamic, this is instead max batch size elif coreml: # CoreML LOGGER.info(f'Loading {w} for CoreML inference...') import coremltools as ct model = ct.models.MLModel(w) elif saved_model: # TF SavedModel LOGGER.info(f'Loading {w} for TensorFlow SavedModel inference...') import tensorflow as tf keras = False # assume TF1 saved_model model = tf.keras.models.load_model(w) if keras else tf.saved_model.load(w) elif pb: # GraphDef https://www.tensorflow.org/guide/migrate#a_graphpb_or_graphpbtxt LOGGER.info(f'Loading {w} for TensorFlow GraphDef inference...') import tensorflow as tf def wrap_frozen_graph(gd, inputs, outputs): x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) # wrapped ge = x.graph.as_graph_element return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs)) def gd_outputs(gd): name_list, input_list = [], [] for node in gd.node: # tensorflow.core.framework.node_def_pb2.NodeDef name_list.append(node.name) input_list.extend(node.input) return sorted(f'{x}:0' for x in list(set(name_list) - set(input_list)) if not x.startswith('NoOp')) gd = tf.Graph().as_graph_def() # TF GraphDef with open(w, 'rb') as f: gd.ParseFromString(f.read()) frozen_func = wrap_frozen_graph(gd, inputs="x:0", outputs=gd_outputs(gd)) elif tflite or edgetpu: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python try: # https://coral.ai/docs/edgetpu/tflite-python/#update-existing-tf-lite-code-for-the-edge-tpu from tflite_runtime.interpreter import Interpreter, load_delegate except ImportError: import tensorflow as tf Interpreter, load_delegate = tf.lite.Interpreter, tf.lite.experimental.load_delegate, if edgetpu: # TF Edge TPU https://coral.ai/software/#edgetpu-runtime LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...') delegate = { 'Linux': 'libedgetpu.so.1', 'Darwin': 'libedgetpu.1.dylib', 'Windows': 'edgetpu.dll'}[platform.system()] interpreter = Interpreter(model_path=w, experimental_delegates=[load_delegate(delegate)]) else: # TFLite LOGGER.info(f'Loading {w} for TensorFlow Lite inference...') interpreter = Interpreter(model_path=w) # load TFLite model interpreter.allocate_tensors() # allocate input_details = interpreter.get_input_details() # inputs output_details = interpreter.get_output_details() # outputs # load metadata with contextlib.suppress(zipfile.BadZipFile): with zipfile.ZipFile(w, "r") as model: meta_file = model.namelist()[0] meta = ast.literal_eval(model.read(meta_file).decode("utf-8")) stride, names = int(meta['stride']), meta['names'] elif tfjs: # TF.js raise NotImplementedError('ERROR: YOLO TF.js inference is not supported') elif paddle: # PaddlePaddle LOGGER.info(f'Loading {w} for PaddlePaddle inference...') check_requirements('paddlepaddle-gpu' if cuda else 'paddlepaddle') import paddle.inference as pdi if not Path(w).is_file(): # if not *.pdmodel w = next(Path(w).rglob('*.pdmodel')) # get *.pdmodel file from *_paddle_model dir weights = Path(w).with_suffix('.pdiparams') config = pdi.Config(str(w), str(weights)) if cuda: config.enable_use_gpu(memory_pool_init_size_mb=2048, device_id=0) predictor = pdi.create_predictor(config) input_handle = predictor.get_input_handle(predictor.get_input_names()[0]) output_names = predictor.get_output_names() elif triton: # NVIDIA Triton Inference Server LOGGER.info(f'Using {w} as Triton Inference Server...') check_requirements('tritonclient[all]') from asone.detectors.yolov9.yolov9.utils.triton import TritonRemoteModel model = TritonRemoteModel(url=w) nhwc = model.runtime.startswith("tensorflow") else: raise NotImplementedError(f'ERROR: {w} is not a supported format') # class names if 'names' not in locals(): names = yaml_load(data)['names'] if data else {i: f'class{i}' for i in range(999)} if names[0] == 'n01440764' and len(names) == 1000: # ImageNet names = yaml_load(ROOT / 'data/ImageNet.yaml')['names'] # human-readable names self.__dict__.update(locals()) # assign all variables to self def forward(self, im, augment=False, visualize=False): # YOLO MultiBackend inference b, ch, h, w = im.shape # batch, channel, height, width if self.fp16 and im.dtype != torch.float16: im = im.half() # to FP16 if self.nhwc: im = im.permute(0, 2, 3, 1) # torch BCHW to numpy BHWC shape(1,320,192,3) if self.pt: # PyTorch y = self.model(im, augment=augment, visualize=visualize) if augment or visualize else self.model(im) elif self.jit: # TorchScript y = self.model(im) elif self.dnn: # ONNX OpenCV DNN im = im.cpu().numpy() # torch to numpy self.net.setInput(im) y = self.net.forward() elif self.onnx: # ONNX Runtime im = im.cpu().numpy() # torch to numpy y = self.session.run(self.output_names, {self.session.get_inputs()[0].name: im}) elif self.xml: # OpenVINO im = im.cpu().numpy() # FP32 y = list(self.executable_network([im]).values()) elif self.engine: # TensorRT if self.dynamic and im.shape != self.bindings['images'].shape: i = self.model.get_binding_index('images') self.context.set_binding_shape(i, im.shape) # reshape if dynamic self.bindings['images'] = self.bindings['images']._replace(shape=im.shape) for name in self.output_names: i = self.model.get_binding_index(name) self.bindings[name].data.resize_(tuple(self.context.get_binding_shape(i))) s = self.bindings['images'].shape assert im.shape == s, f"input size {im.shape} {'>' if self.dynamic else 'not equal to'} max model size {s}" self.binding_addrs['images'] = int(im.data_ptr()) self.context.execute_v2(list(self.binding_addrs.values())) y = [self.bindings[x].data for x in sorted(self.output_names)] elif self.coreml: # CoreML im = im.cpu().numpy() im = Image.fromarray((im[0] * 255).astype('uint8')) # im = im.resize((192, 320), Image.ANTIALIAS) y = self.model.predict({'image': im}) # coordinates are xywh normalized if 'confidence' in y: box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) # xyxy pixels conf, cls = y['confidence'].max(1), y['confidence'].argmax(1).astype(np.float) y = np.concatenate((box, conf.reshape(-1, 1), cls.reshape(-1, 1)), 1) else: y = list(reversed(y.values())) # reversed for segmentation models (pred, proto) elif self.paddle: # PaddlePaddle im = im.cpu().numpy().astype(np.float32) self.input_handle.copy_from_cpu(im) self.predictor.run() y = [self.predictor.get_output_handle(x).copy_to_cpu() for x in self.output_names] elif self.triton: # NVIDIA Triton Inference Server y = self.model(im) else: # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU) im = im.cpu().numpy() if self.saved_model: # SavedModel y = self.model(im, training=False) if self.keras else self.model(im) elif self.pb: # GraphDef y = self.frozen_func(x=self.tf.constant(im)) else: # Lite or Edge TPU input = self.input_details[0] int8 = input['dtype'] == np.uint8 # is TFLite quantized uint8 model if int8: scale, zero_point = input['quantization'] im = (im / scale + zero_point).astype(np.uint8) # de-scale self.interpreter.set_tensor(input['index'], im) self.interpreter.invoke() y = [] for output in self.output_details: x = self.interpreter.get_tensor(output['index']) if int8: scale, zero_point = output['quantization'] x = (x.astype(np.float32) - zero_point) * scale # re-scale y.append(x) y = [x if isinstance(x, np.ndarray) else x.numpy() for x in y] y[0][..., :4] *= [w, h, w, h] # xywh normalized to pixels if isinstance(y, (list, tuple)): return self.from_numpy(y[0]) if len(y) == 1 else [self.from_numpy(x) for x in y] else: return self.from_numpy(y) def from_numpy(self, x): return torch.from_numpy(x).to(self.device) if isinstance(x, np.ndarray) else x def warmup(self, imgsz=(1, 3, 640, 640)): # Warmup model by running inference once warmup_types = self.pt, self.jit, self.onnx, self.engine, self.saved_model, self.pb, self.triton if any(warmup_types) and (self.device.type != 'cpu' or self.triton): im = torch.empty(*imgsz, dtype=torch.half if self.fp16 else torch.float, device=self.device) # input for _ in range(2 if self.jit else 1): # self.forward(im) # warmup @staticmethod def _model_type(p='path/to/model.pt'): # Return model type from model path, i.e. path='path/to/model.onnx' -> type=onnx # types = [pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle] from asone.detectors.yolov9.export import export_formats from asone.detectors.yolov9.yolov9.utils.downloads import is_url sf = list(export_formats().Suffix) # export suffixes if not is_url(p, check=False): check_suffix(p, sf) # checks url = urlparse(p) # if url may be Triton inference server types = [s in Path(p).name for s in sf] types[8] &= not types[9] # tflite &= not edgetpu triton = not any(types) and all([any(s in url.scheme for s in ["http", "grpc"]), url.netloc]) return types + [triton] @staticmethod def _load_metadata(f=Path('path/to/meta.yaml')): # Load metadata from meta.yaml if it exists if f.exists(): d = yaml_load(f) return d['stride'], d['names'] # assign stride, names return None, None class AutoShape(nn.Module): # YOLO input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS conf = 0.25 # NMS confidence threshold iou = 0.45 # NMS IoU threshold agnostic = False # NMS class-agnostic multi_label = False # NMS multiple labels per box classes = None # (optional list) filter by class, i.e. = [0, 15, 16] for COCO persons, cats and dogs max_det = 1000 # maximum number of detections per image amp = False # Automatic Mixed Precision (AMP) inference def __init__(self, model, verbose=True): super().__init__() if verbose: LOGGER.info('Adding AutoShape... ') copy_attr(self, model, include=('yaml', 'nc', 'hyp', 'names', 'stride', 'abc'), exclude=()) # copy attributes self.dmb = isinstance(model, DetectMultiBackend) # DetectMultiBackend() instance self.pt = not self.dmb or model.pt # PyTorch model self.model = model.eval() if self.pt: m = self.model.model.model[-1] if self.dmb else self.model.model[-1] # Detect() m.inplace = False # Detect.inplace=False for safe multithread inference m.export = True # do not output loss values def _apply(self, fn): # Apply to(), cpu(), cuda(), half() to model tensors that are not parameters or registered buffers self = super()._apply(fn) from asone.detectors.yolov9.yolov9.models.yolo import Detect, Segment if self.pt: m = self.model.model.model[-1] if self.dmb else self.model.model[-1] # Detect() if isinstance(m, (Detect, Segment)): for k in 'stride', 'anchor_grid', 'stride_grid', 'grid': x = getattr(m, k) setattr(m, k, list(map(fn, x))) if isinstance(x, (list, tuple)) else setattr(m, k, fn(x)) return self @smart_inference_mode() def forward(self, ims, size=640, augment=False, profile=False): # Inference from various sources. For size(height=640, width=1280), RGB images example inputs are: # file: ims = 'data/images/zidane.jpg' # str or PosixPath # URI: = 'https://ultralytics.com/images/zidane.jpg' # OpenCV: = cv2.imread('image.jpg')[:,:,::-1] # HWC BGR to RGB x(640,1280,3) # PIL: = Image.open('image.jpg') or ImageGrab.grab() # HWC x(640,1280,3) # numpy: = np.zeros((640,1280,3)) # HWC # torch: = torch.zeros(16,3,320,640) # BCHW (scaled to size=640, 0-1 values) # multiple: = [Image.open('image1.jpg'), Image.open('image2.jpg'), ...] # list of images dt = (Profile(), Profile(), Profile()) with dt[0]: if isinstance(size, int): # expand size = (size, size) p = next(self.model.parameters()) if self.pt else torch.empty(1, device=self.model.device) # param autocast = self.amp and (p.device.type != 'cpu') # Automatic Mixed Precision (AMP) inference if isinstance(ims, torch.Tensor): # torch with amp.autocast(autocast): return self.model(ims.to(p.device).type_as(p), augment=augment) # inference # Pre-process n, ims = (len(ims), list(ims)) if isinstance(ims, (list, tuple)) else (1, [ims]) # number, list of images shape0, shape1, files = [], [], [] # image and inference shapes, filenames for i, im in enumerate(ims): f = f'image{i}' # filename if isinstance(im, (str, Path)): # filename or uri im, f = Image.open(requests.get(im, stream=True).raw if str(im).startswith('http') else im), im im = np.asarray(exif_transpose(im)) elif isinstance(im, Image.Image): # PIL Image im, f = np.asarray(exif_transpose(im)), getattr(im, 'filename', f) or f files.append(Path(f).with_suffix('.jpg').name) if im.shape[0] < 5: # image in CHW im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1) im = im[..., :3] if im.ndim == 3 else cv2.cvtColor(im, cv2.COLOR_GRAY2BGR) # enforce 3ch input s = im.shape[:2] # HWC shape0.append(s) # image shape g = max(size) / max(s) # gain shape1.append([int(y * g) for y in s]) ims[i] = im if im.data.contiguous else np.ascontiguousarray(im) # update shape1 = [make_divisible(x, self.stride) for x in np.array(shape1).max(0)] # inf shape x = [letterbox(im, shape1, auto=False)[0] for im in ims] # pad x = np.ascontiguousarray(np.array(x).transpose((0, 3, 1, 2))) # stack and BHWC to BCHW x = torch.from_numpy(x).to(p.device).type_as(p) / 255 # uint8 to fp16/32 with amp.autocast(autocast): # Inference with dt[1]: y = self.model(x, augment=augment) # forward # Post-process with dt[2]: y = non_max_suppression(y if self.dmb else y[0], self.conf, self.iou, self.classes, self.agnostic, self.multi_label, max_det=self.max_det) # NMS for i in range(n): scale_boxes(shape1, y[i][:, :4], shape0[i]) return Detections(ims, y, files, dt, self.names, x.shape) class Detections: # YOLO detections class for inference results def __init__(self, ims, pred, files, times=(0, 0, 0), names=None, shape=None): super().__init__() d = pred[0].device # device gn = [torch.tensor([*(im.shape[i] for i in [1, 0, 1, 0]), 1, 1], device=d) for im in ims] # normalizations self.ims = ims # list of images as numpy arrays self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls) self.names = names # class names self.files = files # image filenames self.times = times # profiling times self.xyxy = pred # xyxy pixels self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized self.n = len(self.pred) # number of images (batch size) self.t = tuple(x.t / self.n * 1E3 for x in times) # timestamps (ms) self.s = tuple(shape) # inference BCHW shape def _run(self, pprint=False, show=False, save=False, crop=False, render=False, labels=True, save_dir=Path('')): s, crops = '', [] for i, (im, pred) in enumerate(zip(self.ims, self.pred)): s += f'\nimage {i + 1}/{len(self.pred)}: {im.shape[0]}x{im.shape[1]} ' # string if pred.shape[0]: for c in pred[:, -1].unique(): n = (pred[:, -1] == c).sum() # detections per class s += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string s = s.rstrip(', ') if show or save or render or crop: annotator = Annotator(im, example=str(self.names)) for *box, conf, cls in reversed(pred): # xyxy, confidence, class label = f'{self.names[int(cls)]} {conf:.2f}' if crop: file = save_dir / 'crops' / self.names[int(cls)] / self.files[i] if save else None crops.append({ 'box': box, 'conf': conf, 'cls': cls, 'label': label, 'im': save_one_box(box, im, file=file, save=save)}) else: # all others annotator.box_label(box, label if labels else '', color=colors(cls)) im = annotator.im else: s += '(no detections)' im = Image.fromarray(im.astype(np.uint8)) if isinstance(im, np.ndarray) else im # from np if show: display(im) if is_notebook() else im.show(self.files[i]) if save: f = self.files[i] im.save(save_dir / f) # save if i == self.n - 1: LOGGER.info(f"Saved {self.n} image{'s' * (self.n > 1)} to {colorstr('bold', save_dir)}") if render: self.ims[i] = np.asarray(im) if pprint: s = s.lstrip('\n') return f'{s}\nSpeed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {self.s}' % self.t if crop: if save: LOGGER.info(f'Saved results to {save_dir}\n') return crops @TryExcept('Showing images is not supported in this environment') def show(self, labels=True): self._run(show=True, labels=labels) # show results def save(self, labels=True, save_dir='runs/detect/exp', exist_ok=False): save_dir = increment_path(save_dir, exist_ok, mkdir=True) # increment save_dir self._run(save=True, labels=labels, save_dir=save_dir) # save results def crop(self, save=True, save_dir='runs/detect/exp', exist_ok=False): save_dir = increment_path(save_dir, exist_ok, mkdir=True) if save else None return self._run(crop=True, save=save, save_dir=save_dir) # crop results def render(self, labels=True): self._run(render=True, labels=labels) # render results return self.ims def pandas(self): # return detections as pandas DataFrames, i.e. print(results.pandas().xyxy[0]) new = copy(self) # return copy ca = 'xmin', 'ymin', 'xmax', 'ymax', 'confidence', 'class', 'name' # xyxy columns cb = 'xcenter', 'ycenter', 'width', 'height', 'confidence', 'class', 'name' # xywh columns for k, c in zip(['xyxy', 'xyxyn', 'xywh', 'xywhn'], [ca, ca, cb, cb]): a = [[x[:5] + [int(x[5]), self.names[int(x[5])]] for x in x.tolist()] for x in getattr(self, k)] # update setattr(new, k, [pd.DataFrame(x, columns=c) for x in a]) return new def tolist(self): # return a list of Detections objects, i.e. 'for result in results.tolist():' r = range(self.n) # iterable x = [Detections([self.ims[i]], [self.pred[i]], [self.files[i]], self.times, self.names, self.s) for i in r] # for d in x: # for k in ['ims', 'pred', 'xyxy', 'xyxyn', 'xywh', 'xywhn']: # setattr(d, k, getattr(d, k)[0]) # pop out of list return x def print(self): LOGGER.info(self.__str__()) def __len__(self): # override len(results) return self.n def __str__(self): # override print(results) return self._run(pprint=True) # print results def __repr__(self): return f'YOLO {self.__class__} instance\n' + self.__str__() class Proto(nn.Module): # YOLO mask Proto module for segmentation models def __init__(self, c1, c_=256, c2=32): # ch_in, number of protos, number of masks super().__init__() self.cv1 = Conv(c1, c_, k=3) self.upsample = nn.Upsample(scale_factor=2, mode='nearest') self.cv2 = Conv(c_, c_, k=3) self.cv3 = Conv(c_, c2) def forward(self, x): return self.cv3(self.cv2(self.upsample(self.cv1(x)))) class UConv(nn.Module): def __init__(self, c1, c_=256, c2=256): # ch_in, number of protos, number of masks super().__init__() self.cv1 = Conv(c1, c_, k=3) self.cv2 = nn.Conv2d(c_, c2, 1, 1) self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True) def forward(self, x): return self.up(self.cv2(self.cv1(x))) class Classify(nn.Module): # YOLO classification head, i.e. x(b,c1,20,20) to x(b,c2) def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups super().__init__() c_ = 1280 # efficientnet_b0 size self.conv = Conv(c1, c_, k, s, autopad(k, p), g) self.pool = nn.AdaptiveAvgPool2d(1) # to x(b,c_,1,1) self.drop = nn.Dropout(p=0.0, inplace=True) self.linear = nn.Linear(c_, c2) # to x(b,c2) def forward(self, x): if isinstance(x, list): x = torch.cat(x, 1) return self.linear(self.drop(self.pool(self.conv(x)).flatten(1))) ================================================ FILE: asone/detectors/yolov9/yolov9/models/experimental.py ================================================ import math import random import numpy as np import torch import torch.nn as nn from asone.detectors.yolov9.yolov9.utils.downloads import attempt_download class Sum(nn.Module): # Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, n, weight=False): # n: number of inputs super().__init__() self.weight = weight # apply weights boolean self.iter = range(n - 1) # iter object if weight: self.w = nn.Parameter(-torch.arange(1.0, n) / 2, requires_grad=True) # layer weights def forward(self, x): y = x[0] # no weight if self.weight: w = torch.sigmoid(self.w) * 2 for i in self.iter: y = y + x[i + 1] * w[i] else: for i in self.iter: y = y + x[i + 1] return y class MixConv2d(nn.Module): # Mixed Depth-wise Conv https://arxiv.org/abs/1907.09595 def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): # ch_in, ch_out, kernel, stride, ch_strategy super().__init__() n = len(k) # number of convolutions if equal_ch: # equal c_ per group i = torch.linspace(0, n - 1E-6, c2).floor() # c2 indices c_ = [(i == g).sum() for g in range(n)] # intermediate channels else: # equal weight.numel() per group b = [c2] + [0] * n a = np.eye(n + 1, n, k=-1) a -= np.roll(a, 1, axis=1) a *= np.array(k) ** 2 a[0] = 1 c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b self.m = nn.ModuleList([ nn.Conv2d(c1, int(c_), k, s, k // 2, groups=math.gcd(c1, int(c_)), bias=False) for k, c_ in zip(k, c_)]) self.bn = nn.BatchNorm2d(c2) self.act = nn.SiLU() def forward(self, x): return self.act(self.bn(torch.cat([m(x) for m in self.m], 1))) class Ensemble(nn.ModuleList): # Ensemble of models def __init__(self): super().__init__() def forward(self, x, augment=False, profile=False, visualize=False): y = [module(x, augment, profile, visualize)[0] for module in self] # y = torch.stack(y).max(0)[0] # max ensemble # y = torch.stack(y).mean(0) # mean ensemble y = torch.cat(y, 1) # nms ensemble return y, None # inference, train output class ORT_NMS(torch.autograd.Function): '''ONNX-Runtime NMS operation''' @staticmethod def forward(ctx, boxes, scores, max_output_boxes_per_class=torch.tensor([100]), iou_threshold=torch.tensor([0.45]), score_threshold=torch.tensor([0.25])): device = boxes.device batch = scores.shape[0] num_det = random.randint(0, 100) batches = torch.randint(0, batch, (num_det,)).sort()[0].to(device) idxs = torch.arange(100, 100 + num_det).to(device) zeros = torch.zeros((num_det,), dtype=torch.int64).to(device) selected_indices = torch.cat([batches[None], zeros[None], idxs[None]], 0).T.contiguous() selected_indices = selected_indices.to(torch.int64) return selected_indices @staticmethod def symbolic(g, boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold): return g.op("NonMaxSuppression", boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold) class TRT_NMS(torch.autograd.Function): '''TensorRT NMS operation''' @staticmethod def forward( ctx, boxes, scores, background_class=-1, box_coding=1, iou_threshold=0.45, max_output_boxes=100, plugin_version="1", score_activation=0, score_threshold=0.25, ): batch_size, num_boxes, num_classes = scores.shape num_det = torch.randint(0, max_output_boxes, (batch_size, 1), dtype=torch.int32) det_boxes = torch.randn(batch_size, max_output_boxes, 4) det_scores = torch.randn(batch_size, max_output_boxes) det_classes = torch.randint(0, num_classes, (batch_size, max_output_boxes), dtype=torch.int32) return num_det, det_boxes, det_scores, det_classes @staticmethod def symbolic(g, boxes, scores, background_class=-1, box_coding=1, iou_threshold=0.45, max_output_boxes=100, plugin_version="1", score_activation=0, score_threshold=0.25): out = g.op("TRT::EfficientNMS_TRT", boxes, scores, background_class_i=background_class, box_coding_i=box_coding, iou_threshold_f=iou_threshold, max_output_boxes_i=max_output_boxes, plugin_version_s=plugin_version, score_activation_i=score_activation, score_threshold_f=score_threshold, outputs=4) nums, boxes, scores, classes = out return nums, boxes, scores, classes class ONNX_ORT(nn.Module): '''onnx module with ONNX-Runtime NMS operation.''' def __init__(self, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=640, device=None, n_classes=80): super().__init__() self.device = device if device else torch.device("cpu") self.max_obj = torch.tensor([max_obj]).to(device) self.iou_threshold = torch.tensor([iou_thres]).to(device) self.score_threshold = torch.tensor([score_thres]).to(device) self.max_wh = max_wh # if max_wh != 0 : non-agnostic else : agnostic self.convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]], dtype=torch.float32, device=self.device) self.n_classes=n_classes def forward(self, x): ## https://github.com/thaitc-hust/yolov9-tensorrt/blob/main/torch2onnx.py ## thanks https://github.com/thaitc-hust if isinstance(x, list): ## yolov9-c.pt and yolov9-e.pt return list x = x[1] x = x.permute(0, 2, 1) bboxes_x = x[..., 0:1] bboxes_y = x[..., 1:2] bboxes_w = x[..., 2:3] bboxes_h = x[..., 3:4] bboxes = torch.cat([bboxes_x, bboxes_y, bboxes_w, bboxes_h], dim = -1) bboxes = bboxes.unsqueeze(2) # [n_batch, n_bboxes, 4] -> [n_batch, n_bboxes, 1, 4] obj_conf = x[..., 4:] scores = obj_conf bboxes @= self.convert_matrix max_score, category_id = scores.max(2, keepdim=True) dis = category_id.float() * self.max_wh nmsbox = bboxes + dis max_score_tp = max_score.transpose(1, 2).contiguous() selected_indices = ORT_NMS.apply(nmsbox, max_score_tp, self.max_obj, self.iou_threshold, self.score_threshold) X, Y = selected_indices[:, 0], selected_indices[:, 2] selected_boxes = bboxes[X, Y, :] selected_categories = category_id[X, Y, :].float() selected_scores = max_score[X, Y, :] X = X.unsqueeze(1).float() return torch.cat([X, selected_boxes, selected_categories, selected_scores], 1) class ONNX_TRT(nn.Module): '''onnx module with TensorRT NMS operation.''' def __init__(self, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=None ,device=None, n_classes=80): super().__init__() assert max_wh is None self.device = device if device else torch.device('cpu') self.background_class = -1, self.box_coding = 1, self.iou_threshold = iou_thres self.max_obj = max_obj self.plugin_version = '1' self.score_activation = 0 self.score_threshold = score_thres self.n_classes=n_classes def forward(self, x): ## https://github.com/thaitc-hust/yolov9-tensorrt/blob/main/torch2onnx.py ## thanks https://github.com/thaitc-hust if isinstance(x, list): ## yolov9-c.pt and yolov9-e.pt return list x = x[1] x = x.permute(0, 2, 1) bboxes_x = x[..., 0:1] bboxes_y = x[..., 1:2] bboxes_w = x[..., 2:3] bboxes_h = x[..., 3:4] bboxes = torch.cat([bboxes_x, bboxes_y, bboxes_w, bboxes_h], dim = -1) bboxes = bboxes.unsqueeze(2) # [n_batch, n_bboxes, 4] -> [n_batch, n_bboxes, 1, 4] obj_conf = x[..., 4:] scores = obj_conf num_det, det_boxes, det_scores, det_classes = TRT_NMS.apply(bboxes, scores, self.background_class, self.box_coding, self.iou_threshold, self.max_obj, self.plugin_version, self.score_activation, self.score_threshold) return num_det, det_boxes, det_scores, det_classes class End2End(nn.Module): '''export onnx or tensorrt model with NMS operation.''' def __init__(self, model, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=None, device=None, n_classes=80): super().__init__() device = device if device else torch.device('cpu') assert isinstance(max_wh,(int)) or max_wh is None self.model = model.to(device) self.model.model[-1].end2end = True self.patch_model = ONNX_TRT if max_wh is None else ONNX_ORT self.end2end = self.patch_model(max_obj, iou_thres, score_thres, max_wh, device, n_classes) self.end2end.eval() def forward(self, x): x = self.model(x) x = self.end2end(x) return x def attempt_load(weights, device=None, inplace=True, fuse=True): # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a from asone.detectors.yolov9.yolov9.models.yolo import Detect, Model model = Ensemble() for w in weights if isinstance(weights, list) else [weights]: ckpt = torch.load(attempt_download(w), map_location='cpu') # load ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float() # FP32 model # Model compatibility updates if not hasattr(ckpt, 'stride'): ckpt.stride = torch.tensor([32.]) if hasattr(ckpt, 'names') and isinstance(ckpt.names, (list, tuple)): ckpt.names = dict(enumerate(ckpt.names)) # convert to dict model.append(ckpt.fuse().eval() if fuse and hasattr(ckpt, 'fuse') else ckpt.eval()) # model in eval mode # Module compatibility updates for m in model.modules(): t = type(m) if t in (nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, Detect, Model): m.inplace = inplace # torch 1.7.0 compatibility # if t is Detect and not isinstance(m.anchor_grid, list): # delattr(m, 'anchor_grid') # setattr(m, 'anchor_grid', [torch.zeros(1)] * m.nl) elif t is nn.Upsample and not hasattr(m, 'recompute_scale_factor'): m.recompute_scale_factor = None # torch 1.11.0 compatibility # Return model if len(model) == 1: return model[-1] # Return detection ensemble print(f'Ensemble created with {weights}\n') for k in 'names', 'nc', 'yaml': setattr(model, k, getattr(model[0], k)) model.stride = model[torch.argmax(torch.tensor([m.stride.max() for m in model])).int()].stride # max stride assert all(model[0].nc == m.nc for m in model), f'Models have different class counts: {[m.nc for m in model]}' return model ================================================ FILE: asone/detectors/yolov9/yolov9/models/tf.py ================================================ import argparse import sys from copy import deepcopy from pathlib import Path FILE = Path(__file__).resolve() ROOT = FILE.parents[1] # YOLO root directory # if str(ROOT) not in sys.path: # sys.path.append(str(ROOT)) # add ROOT to PATH # ROOT = ROOT.relative_to(Path.cwd()) # relative import numpy as np import tensorflow as tf import torch import torch.nn as nn from tensorflow import keras from asone.detectors.yolov9.yolov9.models.common import (C3, SPP, SPPF, Bottleneck, BottleneckCSP, C3x, Concat, Conv, CrossConv, DWConv, DWConvTranspose2d, Focus, autopad) from asone.detectors.yolov9.yolov9.models.experimental import MixConv2d, attempt_load from asone.detectors.yolov9.yolov9.models.yolo import Detect, Segment from asone.detectors.yolov9.yolov9.utils.activations import SiLU from asone.detectors.yolov9.yolov9.utils.general import LOGGER, make_divisible, print_args class TFBN(keras.layers.Layer): # TensorFlow BatchNormalization wrapper def __init__(self, w=None): super().__init__() self.bn = keras.layers.BatchNormalization( beta_initializer=keras.initializers.Constant(w.bias.numpy()), gamma_initializer=keras.initializers.Constant(w.weight.numpy()), moving_mean_initializer=keras.initializers.Constant(w.running_mean.numpy()), moving_variance_initializer=keras.initializers.Constant(w.running_var.numpy()), epsilon=w.eps) def call(self, inputs): return self.bn(inputs) class TFPad(keras.layers.Layer): # Pad inputs in spatial dimensions 1 and 2 def __init__(self, pad): super().__init__() if isinstance(pad, int): self.pad = tf.constant([[0, 0], [pad, pad], [pad, pad], [0, 0]]) else: # tuple/list self.pad = tf.constant([[0, 0], [pad[0], pad[0]], [pad[1], pad[1]], [0, 0]]) def call(self, inputs): return tf.pad(inputs, self.pad, mode='constant', constant_values=0) class TFConv(keras.layers.Layer): # Standard convolution def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True, w=None): # ch_in, ch_out, weights, kernel, stride, padding, groups super().__init__() assert g == 1, "TF v2.2 Conv2D does not support 'groups' argument" # TensorFlow convolution padding is inconsistent with PyTorch (e.g. k=3 s=2 'SAME' padding) # see https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch conv = keras.layers.Conv2D( filters=c2, kernel_size=k, strides=s, padding='SAME' if s == 1 else 'VALID', use_bias=not hasattr(w, 'bn'), kernel_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()), bias_initializer='zeros' if hasattr(w, 'bn') else keras.initializers.Constant(w.conv.bias.numpy())) self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv]) self.bn = TFBN(w.bn) if hasattr(w, 'bn') else tf.identity self.act = activations(w.act) if act else tf.identity def call(self, inputs): return self.act(self.bn(self.conv(inputs))) class TFDWConv(keras.layers.Layer): # Depthwise convolution def __init__(self, c1, c2, k=1, s=1, p=None, act=True, w=None): # ch_in, ch_out, weights, kernel, stride, padding, groups super().__init__() assert c2 % c1 == 0, f'TFDWConv() output={c2} must be a multiple of input={c1} channels' conv = keras.layers.DepthwiseConv2D( kernel_size=k, depth_multiplier=c2 // c1, strides=s, padding='SAME' if s == 1 else 'VALID', use_bias=not hasattr(w, 'bn'), depthwise_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()), bias_initializer='zeros' if hasattr(w, 'bn') else keras.initializers.Constant(w.conv.bias.numpy())) self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv]) self.bn = TFBN(w.bn) if hasattr(w, 'bn') else tf.identity self.act = activations(w.act) if act else tf.identity def call(self, inputs): return self.act(self.bn(self.conv(inputs))) class TFDWConvTranspose2d(keras.layers.Layer): # Depthwise ConvTranspose2d def __init__(self, c1, c2, k=1, s=1, p1=0, p2=0, w=None): # ch_in, ch_out, weights, kernel, stride, padding, groups super().__init__() assert c1 == c2, f'TFDWConv() output={c2} must be equal to input={c1} channels' assert k == 4 and p1 == 1, 'TFDWConv() only valid for k=4 and p1=1' weight, bias = w.weight.permute(2, 3, 1, 0).numpy(), w.bias.numpy() self.c1 = c1 self.conv = [ keras.layers.Conv2DTranspose(filters=1, kernel_size=k, strides=s, padding='VALID', output_padding=p2, use_bias=True, kernel_initializer=keras.initializers.Constant(weight[..., i:i + 1]), bias_initializer=keras.initializers.Constant(bias[i])) for i in range(c1)] def call(self, inputs): return tf.concat([m(x) for m, x in zip(self.conv, tf.split(inputs, self.c1, 3))], 3)[:, 1:-1, 1:-1] class TFFocus(keras.layers.Layer): # Focus wh information into c-space def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True, w=None): # ch_in, ch_out, kernel, stride, padding, groups super().__init__() self.conv = TFConv(c1 * 4, c2, k, s, p, g, act, w.conv) def call(self, inputs): # x(b,w,h,c) -> y(b,w/2,h/2,4c) # inputs = inputs / 255 # normalize 0-255 to 0-1 inputs = [inputs[:, ::2, ::2, :], inputs[:, 1::2, ::2, :], inputs[:, ::2, 1::2, :], inputs[:, 1::2, 1::2, :]] return self.conv(tf.concat(inputs, 3)) class TFBottleneck(keras.layers.Layer): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5, w=None): # ch_in, ch_out, shortcut, groups, expansion super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv(c_, c2, 3, 1, g=g, w=w.cv2) self.add = shortcut and c1 == c2 def call(self, inputs): return inputs + self.cv2(self.cv1(inputs)) if self.add else self.cv2(self.cv1(inputs)) class TFCrossConv(keras.layers.Layer): # Cross Convolution def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False, w=None): super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = TFConv(c1, c_, (1, k), (1, s), w=w.cv1) self.cv2 = TFConv(c_, c2, (k, 1), (s, 1), g=g, w=w.cv2) self.add = shortcut and c1 == c2 def call(self, inputs): return inputs + self.cv2(self.cv1(inputs)) if self.add else self.cv2(self.cv1(inputs)) class TFConv2d(keras.layers.Layer): # Substitution for PyTorch nn.Conv2D def __init__(self, c1, c2, k, s=1, g=1, bias=True, w=None): super().__init__() assert g == 1, "TF v2.2 Conv2D does not support 'groups' argument" self.conv = keras.layers.Conv2D(filters=c2, kernel_size=k, strides=s, padding='VALID', use_bias=bias, kernel_initializer=keras.initializers.Constant( w.weight.permute(2, 3, 1, 0).numpy()), bias_initializer=keras.initializers.Constant(w.bias.numpy()) if bias else None) def call(self, inputs): return self.conv(inputs) class TFBottleneckCSP(keras.layers.Layer): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv2d(c1, c_, 1, 1, bias=False, w=w.cv2) self.cv3 = TFConv2d(c_, c_, 1, 1, bias=False, w=w.cv3) self.cv4 = TFConv(2 * c_, c2, 1, 1, w=w.cv4) self.bn = TFBN(w.bn) self.act = lambda x: keras.activations.swish(x) self.m = keras.Sequential([TFBottleneck(c_, c_, shortcut, g, e=1.0, w=w.m[j]) for j in range(n)]) def call(self, inputs): y1 = self.cv3(self.m(self.cv1(inputs))) y2 = self.cv2(inputs) return self.cv4(self.act(self.bn(tf.concat((y1, y2), axis=3)))) class TFC3(keras.layers.Layer): # CSP Bottleneck with 3 convolutions def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv(c1, c_, 1, 1, w=w.cv2) self.cv3 = TFConv(2 * c_, c2, 1, 1, w=w.cv3) self.m = keras.Sequential([TFBottleneck(c_, c_, shortcut, g, e=1.0, w=w.m[j]) for j in range(n)]) def call(self, inputs): return self.cv3(tf.concat((self.m(self.cv1(inputs)), self.cv2(inputs)), axis=3)) class TFC3x(keras.layers.Layer): # 3 module with cross-convolutions def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() c_ = int(c2 * e) # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv(c1, c_, 1, 1, w=w.cv2) self.cv3 = TFConv(2 * c_, c2, 1, 1, w=w.cv3) self.m = keras.Sequential([ TFCrossConv(c_, c_, k=3, s=1, g=g, e=1.0, shortcut=shortcut, w=w.m[j]) for j in range(n)]) def call(self, inputs): return self.cv3(tf.concat((self.m(self.cv1(inputs)), self.cv2(inputs)), axis=3)) class TFSPP(keras.layers.Layer): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, k=(5, 9, 13), w=None): super().__init__() c_ = c1 // 2 # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv(c_ * (len(k) + 1), c2, 1, 1, w=w.cv2) self.m = [keras.layers.MaxPool2D(pool_size=x, strides=1, padding='SAME') for x in k] def call(self, inputs): x = self.cv1(inputs) return self.cv2(tf.concat([x] + [m(x) for m in self.m], 3)) class TFSPPF(keras.layers.Layer): # Spatial pyramid pooling-Fast layer def __init__(self, c1, c2, k=5, w=None): super().__init__() c_ = c1 // 2 # hidden channels self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1) self.cv2 = TFConv(c_ * 4, c2, 1, 1, w=w.cv2) self.m = keras.layers.MaxPool2D(pool_size=k, strides=1, padding='SAME') def call(self, inputs): x = self.cv1(inputs) y1 = self.m(x) y2 = self.m(y1) return self.cv2(tf.concat([x, y1, y2, self.m(y2)], 3)) class TFDetect(keras.layers.Layer): # TF YOLO Detect layer def __init__(self, nc=80, anchors=(), ch=(), imgsz=(640, 640), w=None): # detection layer super().__init__() self.stride = tf.convert_to_tensor(w.stride.numpy(), dtype=tf.float32) self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [tf.zeros(1)] * self.nl # init grid self.anchors = tf.convert_to_tensor(w.anchors.numpy(), dtype=tf.float32) self.anchor_grid = tf.reshape(self.anchors * tf.reshape(self.stride, [self.nl, 1, 1]), [self.nl, 1, -1, 1, 2]) self.m = [TFConv2d(x, self.no * self.na, 1, w=w.m[i]) for i, x in enumerate(ch)] self.training = False # set to False after building model self.imgsz = imgsz for i in range(self.nl): ny, nx = self.imgsz[0] // self.stride[i], self.imgsz[1] // self.stride[i] self.grid[i] = self._make_grid(nx, ny) def call(self, inputs): z = [] # inference output x = [] for i in range(self.nl): x.append(self.m[i](inputs[i])) # x(bs,20,20,255) to x(bs,3,20,20,85) ny, nx = self.imgsz[0] // self.stride[i], self.imgsz[1] // self.stride[i] x[i] = tf.reshape(x[i], [-1, ny * nx, self.na, self.no]) if not self.training: # inference y = x[i] grid = tf.transpose(self.grid[i], [0, 2, 1, 3]) - 0.5 anchor_grid = tf.transpose(self.anchor_grid[i], [0, 2, 1, 3]) * 4 xy = (tf.sigmoid(y[..., 0:2]) * 2 + grid) * self.stride[i] # xy wh = tf.sigmoid(y[..., 2:4]) ** 2 * anchor_grid # Normalize xywh to 0-1 to reduce calibration error xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32) wh /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32) y = tf.concat([xy, wh, tf.sigmoid(y[..., 4:5 + self.nc]), y[..., 5 + self.nc:]], -1) z.append(tf.reshape(y, [-1, self.na * ny * nx, self.no])) return tf.transpose(x, [0, 2, 1, 3]) if self.training else (tf.concat(z, 1),) @staticmethod def _make_grid(nx=20, ny=20): # yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) # return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() xv, yv = tf.meshgrid(tf.range(nx), tf.range(ny)) return tf.cast(tf.reshape(tf.stack([xv, yv], 2), [1, 1, ny * nx, 2]), dtype=tf.float32) class TFSegment(TFDetect): # YOLO Segment head for segmentation models def __init__(self, nc=80, anchors=(), nm=32, npr=256, ch=(), imgsz=(640, 640), w=None): super().__init__(nc, anchors, ch, imgsz, w) self.nm = nm # number of masks self.npr = npr # number of protos self.no = 5 + nc + self.nm # number of outputs per anchor self.m = [TFConv2d(x, self.no * self.na, 1, w=w.m[i]) for i, x in enumerate(ch)] # output conv self.proto = TFProto(ch[0], self.npr, self.nm, w=w.proto) # protos self.detect = TFDetect.call def call(self, x): p = self.proto(x[0]) # p = TFUpsample(None, scale_factor=4, mode='nearest')(self.proto(x[0])) # (optional) full-size protos p = tf.transpose(p, [0, 3, 1, 2]) # from shape(1,160,160,32) to shape(1,32,160,160) x = self.detect(self, x) return (x, p) if self.training else (x[0], p) class TFProto(keras.layers.Layer): def __init__(self, c1, c_=256, c2=32, w=None): super().__init__() self.cv1 = TFConv(c1, c_, k=3, w=w.cv1) self.upsample = TFUpsample(None, scale_factor=2, mode='nearest') self.cv2 = TFConv(c_, c_, k=3, w=w.cv2) self.cv3 = TFConv(c_, c2, w=w.cv3) def call(self, inputs): return self.cv3(self.cv2(self.upsample(self.cv1(inputs)))) class TFUpsample(keras.layers.Layer): # TF version of torch.nn.Upsample() def __init__(self, size, scale_factor, mode, w=None): # warning: all arguments needed including 'w' super().__init__() assert scale_factor % 2 == 0, "scale_factor must be multiple of 2" self.upsample = lambda x: tf.image.resize(x, (x.shape[1] * scale_factor, x.shape[2] * scale_factor), mode) # self.upsample = keras.layers.UpSampling2D(size=scale_factor, interpolation=mode) # with default arguments: align_corners=False, half_pixel_centers=False # self.upsample = lambda x: tf.raw_ops.ResizeNearestNeighbor(images=x, # size=(x.shape[1] * 2, x.shape[2] * 2)) def call(self, inputs): return self.upsample(inputs) class TFConcat(keras.layers.Layer): # TF version of torch.concat() def __init__(self, dimension=1, w=None): super().__init__() assert dimension == 1, "convert only NCHW to NHWC concat" self.d = 3 def call(self, inputs): return tf.concat(inputs, self.d) def parse_model(d, ch, model, imgsz): # model_dict, input_channels(3) LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}") anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple'] na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors no = na * (nc + 5) # number of outputs = anchors * (classes + 5) layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number, module, args m_str = m m = eval(m) if isinstance(m, str) else m # eval strings for j, a in enumerate(args): try: args[j] = eval(a) if isinstance(a, str) else a # eval strings except NameError: pass n = max(round(n * gd), 1) if n > 1 else n # depth gain if m in [ nn.Conv2d, Conv, DWConv, DWConvTranspose2d, Bottleneck, SPP, SPPF, MixConv2d, Focus, CrossConv, BottleneckCSP, C3, C3x]: c1, c2 = ch[f], args[0] c2 = make_divisible(c2 * gw, 8) if c2 != no else c2 args = [c1, c2, *args[1:]] if m in [BottleneckCSP, C3, C3x]: args.insert(2, n) n = 1 elif m is nn.BatchNorm2d: args = [ch[f]] elif m is Concat: c2 = sum(ch[-1 if x == -1 else x + 1] for x in f) elif m in [Detect, Segment]: args.append([ch[x + 1] for x in f]) if isinstance(args[1], int): # number of anchors args[1] = [list(range(args[1] * 2))] * len(f) if m is Segment: args[3] = make_divisible(args[3] * gw, 8) args.append(imgsz) else: c2 = ch[f] tf_m = eval('TF' + m_str.replace('nn.', '')) m_ = keras.Sequential([tf_m(*args, w=model.model[i][j]) for j in range(n)]) if n > 1 \ else tf_m(*args, w=model.model[i]) # module torch_m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module t = str(m)[8:-2].replace('__main__.', '') # module type np = sum(x.numel() for x in torch_m_.parameters()) # number params m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params LOGGER.info(f'{i:>3}{str(f):>18}{str(n):>3}{np:>10} {t:<40}{str(args):<30}') # print save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist layers.append(m_) ch.append(c2) return keras.Sequential(layers), sorted(save) class TFModel: # TF YOLO model def __init__(self, cfg='yolo.yaml', ch=3, nc=None, model=None, imgsz=(640, 640)): # model, channels, classes super().__init__() if isinstance(cfg, dict): self.yaml = cfg # model dict else: # is *.yaml import yaml # for torch hub self.yaml_file = Path(cfg).name with open(cfg) as f: self.yaml = yaml.load(f, Loader=yaml.FullLoader) # model dict # Define model if nc and nc != self.yaml['nc']: LOGGER.info(f"Overriding {cfg} nc={self.yaml['nc']} with nc={nc}") self.yaml['nc'] = nc # override yaml value self.model, self.savelist = parse_model(deepcopy(self.yaml), ch=[ch], model=model, imgsz=imgsz) def predict(self, inputs, tf_nms=False, agnostic_nms=False, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25): y = [] # outputs x = inputs for m in self.model.layers: if m.f != -1: # if not from previous layer x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers x = m(x) # run y.append(x if m.i in self.savelist else None) # save output # Add TensorFlow NMS if tf_nms: boxes = self._xywh2xyxy(x[0][..., :4]) probs = x[0][:, :, 4:5] classes = x[0][:, :, 5:] scores = probs * classes if agnostic_nms: nms = AgnosticNMS()((boxes, classes, scores), topk_all, iou_thres, conf_thres) else: boxes = tf.expand_dims(boxes, 2) nms = tf.image.combined_non_max_suppression(boxes, scores, topk_per_class, topk_all, iou_thres, conf_thres, clip_boxes=False) return (nms,) return x # output [1,6300,85] = [xywh, conf, class0, class1, ...] # x = x[0] # [x(1,6300,85), ...] to x(6300,85) # xywh = x[..., :4] # x(6300,4) boxes # conf = x[..., 4:5] # x(6300,1) confidences # cls = tf.reshape(tf.cast(tf.argmax(x[..., 5:], axis=1), tf.float32), (-1, 1)) # x(6300,1) classes # return tf.concat([conf, cls, xywh], 1) @staticmethod def _xywh2xyxy(xywh): # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right x, y, w, h = tf.split(xywh, num_or_size_splits=4, axis=-1) return tf.concat([x - w / 2, y - h / 2, x + w / 2, y + h / 2], axis=-1) class AgnosticNMS(keras.layers.Layer): # TF Agnostic NMS def call(self, input, topk_all, iou_thres, conf_thres): # wrap map_fn to avoid TypeSpec related error https://stackoverflow.com/a/65809989/3036450 return tf.map_fn(lambda x: self._nms(x, topk_all, iou_thres, conf_thres), input, fn_output_signature=(tf.float32, tf.float32, tf.float32, tf.int32), name='agnostic_nms') @staticmethod def _nms(x, topk_all=100, iou_thres=0.45, conf_thres=0.25): # agnostic NMS boxes, classes, scores = x class_inds = tf.cast(tf.argmax(classes, axis=-1), tf.float32) scores_inp = tf.reduce_max(scores, -1) selected_inds = tf.image.non_max_suppression(boxes, scores_inp, max_output_size=topk_all, iou_threshold=iou_thres, score_threshold=conf_thres) selected_boxes = tf.gather(boxes, selected_inds) padded_boxes = tf.pad(selected_boxes, paddings=[[0, topk_all - tf.shape(selected_boxes)[0]], [0, 0]], mode="CONSTANT", constant_values=0.0) selected_scores = tf.gather(scores_inp, selected_inds) padded_scores = tf.pad(selected_scores, paddings=[[0, topk_all - tf.shape(selected_boxes)[0]]], mode="CONSTANT", constant_values=-1.0) selected_classes = tf.gather(class_inds, selected_inds) padded_classes = tf.pad(selected_classes, paddings=[[0, topk_all - tf.shape(selected_boxes)[0]]], mode="CONSTANT", constant_values=-1.0) valid_detections = tf.shape(selected_inds)[0] return padded_boxes, padded_scores, padded_classes, valid_detections def activations(act=nn.SiLU): # Returns TF activation from input PyTorch activation if isinstance(act, nn.LeakyReLU): return lambda x: keras.activations.relu(x, alpha=0.1) elif isinstance(act, nn.Hardswish): return lambda x: x * tf.nn.relu6(x + 3) * 0.166666667 elif isinstance(act, (nn.SiLU, SiLU)): return lambda x: keras.activations.swish(x) else: raise Exception(f'no matching TensorFlow activation found for PyTorch activation {act}') def representative_dataset_gen(dataset, ncalib=100): # Representative dataset generator for use with converter.representative_dataset, returns a generator of np arrays for n, (path, img, im0s, vid_cap, string) in enumerate(dataset): im = np.transpose(img, [1, 2, 0]) im = np.expand_dims(im, axis=0).astype(np.float32) im /= 255 yield [im] if n >= ncalib: break def run( weights=ROOT / 'yolo.pt', # weights path imgsz=(640, 640), # inference size h,w batch_size=1, # batch size dynamic=False, # dynamic batch size ): # PyTorch model im = torch.zeros((batch_size, 3, *imgsz)) # BCHW image model = attempt_load(weights, device=torch.device('cpu'), inplace=True, fuse=False) _ = model(im) # inference model.info() # TensorFlow model im = tf.zeros((batch_size, *imgsz, 3)) # BHWC image tf_model = TFModel(cfg=model.yaml, model=model, nc=model.nc, imgsz=imgsz) _ = tf_model.predict(im) # inference # Keras model im = keras.Input(shape=(*imgsz, 3), batch_size=None if dynamic else batch_size) keras_model = keras.Model(inputs=im, outputs=tf_model.predict(im)) keras_model.summary() LOGGER.info('PyTorch, TensorFlow and Keras models successfully verified.\nUse export.py for TF model export.') def parse_opt(): parser = argparse.ArgumentParser() parser.add_argument('--weights', type=str, default=ROOT / 'yolo.pt', help='weights path') parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w') parser.add_argument('--batch-size', type=int, default=1, help='batch size') parser.add_argument('--dynamic', action='store_true', help='dynamic batch size') opt = parser.parse_args() opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand print_args(vars(opt)) return opt def main(opt): run(**vars(opt)) if __name__ == "__main__": opt = parse_opt() main(opt) ================================================ FILE: asone/detectors/yolov9/yolov9/models/yolo.py ================================================ import argparse import os import platform import sys from copy import deepcopy from pathlib import Path FILE = Path(__file__).resolve() ROOT = FILE.parents[1] # YOLO root directory # if str(ROOT) not in sys.path: # sys.path.append(str(ROOT)) # add ROOT to PATH if platform.system() != 'Windows': ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative from asone.detectors.yolov9.yolov9.models.common import * from asone.detectors.yolov9.yolov9.models.experimental import * from asone.detectors.yolov9.yolov9.utils.general import LOGGER, check_version, check_yaml, make_divisible, print_args from asone.detectors.yolov9.yolov9.utils.plots import feature_visualization from asone.detectors.yolov9.yolov9.utils.torch_utils import (fuse_conv_and_bn, initialize_weights, model_info, profile, scale_img, select_device, time_sync) from asone.detectors.yolov9.yolov9.utils.tal.anchor_generator import make_anchors, dist2bbox try: import thop # for FLOPs computation except ImportError: thop = None class Detect(nn.Module): # YOLO Detect head for detection models dynamic = False # force grid reconstruction export = False # export mode shape = None anchors = torch.empty(0) # init strides = torch.empty(0) # init def __init__(self, nc=80, ch=(), inplace=True): # detection layer super().__init__() self.nc = nc # number of classes self.nl = len(ch) # number of detection layers self.reg_max = 16 self.no = nc + self.reg_max * 4 # number of outputs per anchor self.inplace = inplace # use inplace ops (e.g. slice assignment) self.stride = torch.zeros(self.nl) # strides computed during build c2, c3 = max((ch[0] // 4, self.reg_max * 4, 16)), max((ch[0], min((self.nc * 2, 128)))) # channels self.cv2 = nn.ModuleList( nn.Sequential(Conv(x, c2, 3), Conv(c2, c2, 3), nn.Conv2d(c2, 4 * self.reg_max, 1)) for x in ch) self.cv3 = nn.ModuleList( nn.Sequential(Conv(x, c3, 3), Conv(c3, c3, 3), nn.Conv2d(c3, self.nc, 1)) for x in ch) self.dfl = DFL(self.reg_max) if self.reg_max > 1 else nn.Identity() def forward(self, x): shape = x[0].shape # BCHW for i in range(self.nl): x[i] = torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1) if self.training: return x elif self.dynamic or self.shape != shape: self.anchors, self.strides = (x.transpose(0, 1) for x in make_anchors(x, self.stride, 0.5)) self.shape = shape box, cls = torch.cat([xi.view(shape[0], self.no, -1) for xi in x], 2).split((self.reg_max * 4, self.nc), 1) dbox = dist2bbox(self.dfl(box), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides y = torch.cat((dbox, cls.sigmoid()), 1) return y if self.export else (y, x) def bias_init(self): # Initialize Detect() biases, WARNING: requires stride availability m = self # self.model[-1] # Detect() module # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1 # ncf = math.log(0.6 / (m.nc - 0.999999)) if cf is None else torch.log(cf / cf.sum()) # nominal class frequency for a, b, s in zip(m.cv2, m.cv3, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) class DDetect(nn.Module): # YOLO Detect head for detection models dynamic = False # force grid reconstruction export = False # export mode shape = None anchors = torch.empty(0) # init strides = torch.empty(0) # init def __init__(self, nc=80, ch=(), inplace=True): # detection layer super().__init__() self.nc = nc # number of classes self.nl = len(ch) # number of detection layers self.reg_max = 16 self.no = nc + self.reg_max * 4 # number of outputs per anchor self.inplace = inplace # use inplace ops (e.g. slice assignment) self.stride = torch.zeros(self.nl) # strides computed during build c2, c3 = make_divisible(max((ch[0] // 4, self.reg_max * 4, 16)), 4), max((ch[0], min((self.nc * 2, 128)))) # channels self.cv2 = nn.ModuleList( nn.Sequential(Conv(x, c2, 3), Conv(c2, c2, 3, g=4), nn.Conv2d(c2, 4 * self.reg_max, 1, groups=4)) for x in ch) self.cv3 = nn.ModuleList( nn.Sequential(Conv(x, c3, 3), Conv(c3, c3, 3), nn.Conv2d(c3, self.nc, 1)) for x in ch) self.dfl = DFL(self.reg_max) if self.reg_max > 1 else nn.Identity() def forward(self, x): shape = x[0].shape # BCHW for i in range(self.nl): x[i] = torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1) if self.training: return x elif self.dynamic or self.shape != shape: self.anchors, self.strides = (x.transpose(0, 1) for x in make_anchors(x, self.stride, 0.5)) self.shape = shape box, cls = torch.cat([xi.view(shape[0], self.no, -1) for xi in x], 2).split((self.reg_max * 4, self.nc), 1) dbox = dist2bbox(self.dfl(box), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides y = torch.cat((dbox, cls.sigmoid()), 1) return y if self.export else (y, x) def bias_init(self): # Initialize Detect() biases, WARNING: requires stride availability m = self # self.model[-1] # Detect() module # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1 # ncf = math.log(0.6 / (m.nc - 0.999999)) if cf is None else torch.log(cf / cf.sum()) # nominal class frequency for a, b, s in zip(m.cv2, m.cv3, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) class DualDetect(nn.Module): # YOLO Detect head for detection models dynamic = False # force grid reconstruction export = False # export mode shape = None anchors = torch.empty(0) # init strides = torch.empty(0) # init def __init__(self, nc=80, ch=(), inplace=True): # detection layer super().__init__() self.nc = nc # number of classes self.nl = len(ch) // 2 # number of detection layers self.reg_max = 16 self.no = nc + self.reg_max * 4 # number of outputs per anchor self.inplace = inplace # use inplace ops (e.g. slice assignment) self.stride = torch.zeros(self.nl) # strides computed during build c2, c3 = max((ch[0] // 4, self.reg_max * 4, 16)), max((ch[0], min((self.nc * 2, 128)))) # channels c4, c5 = max((ch[self.nl] // 4, self.reg_max * 4, 16)), max((ch[self.nl], min((self.nc * 2, 128)))) # channels self.cv2 = nn.ModuleList( nn.Sequential(Conv(x, c2, 3), Conv(c2, c2, 3), nn.Conv2d(c2, 4 * self.reg_max, 1)) for x in ch[:self.nl]) self.cv3 = nn.ModuleList( nn.Sequential(Conv(x, c3, 3), Conv(c3, c3, 3), nn.Conv2d(c3, self.nc, 1)) for x in ch[:self.nl]) self.cv4 = nn.ModuleList( nn.Sequential(Conv(x, c4, 3), Conv(c4, c4, 3), nn.Conv2d(c4, 4 * self.reg_max, 1)) for x in ch[self.nl:]) self.cv5 = nn.ModuleList( nn.Sequential(Conv(x, c5, 3), Conv(c5, c5, 3), nn.Conv2d(c5, self.nc, 1)) for x in ch[self.nl:]) self.dfl = DFL(self.reg_max) self.dfl2 = DFL(self.reg_max) def forward(self, x): shape = x[0].shape # BCHW d1 = [] d2 = [] for i in range(self.nl): d1.append(torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1)) d2.append(torch.cat((self.cv4[i](x[self.nl+i]), self.cv5[i](x[self.nl+i])), 1)) if self.training: return [d1, d2] elif self.dynamic or self.shape != shape: self.anchors, self.strides = (d1.transpose(0, 1) for d1 in make_anchors(d1, self.stride, 0.5)) self.shape = shape box, cls = torch.cat([di.view(shape[0], self.no, -1) for di in d1], 2).split((self.reg_max * 4, self.nc), 1) dbox = dist2bbox(self.dfl(box), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides box2, cls2 = torch.cat([di.view(shape[0], self.no, -1) for di in d2], 2).split((self.reg_max * 4, self.nc), 1) dbox2 = dist2bbox(self.dfl2(box2), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides y = [torch.cat((dbox, cls.sigmoid()), 1), torch.cat((dbox2, cls2.sigmoid()), 1)] return y if self.export else (y, [d1, d2]) def bias_init(self): # Initialize Detect() biases, WARNING: requires stride availability m = self # self.model[-1] # Detect() module # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1 # ncf = math.log(0.6 / (m.nc - 0.999999)) if cf is None else torch.log(cf / cf.sum()) # nominal class frequency for a, b, s in zip(m.cv2, m.cv3, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) for a, b, s in zip(m.cv4, m.cv5, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) class DualDDetect(nn.Module): # YOLO Detect head for detection models dynamic = False # force grid reconstruction export = False # export mode shape = None anchors = torch.empty(0) # init strides = torch.empty(0) # init def __init__(self, nc=80, ch=(), inplace=True): # detection layer super().__init__() self.nc = nc # number of classes self.nl = len(ch) // 2 # number of detection layers self.reg_max = 16 self.no = nc + self.reg_max * 4 # number of outputs per anchor self.inplace = inplace # use inplace ops (e.g. slice assignment) self.stride = torch.zeros(self.nl) # strides computed during build c2, c3 = make_divisible(max((ch[0] // 4, self.reg_max * 4, 16)), 4), max((ch[0], min((self.nc * 2, 128)))) # channels c4, c5 = make_divisible(max((ch[self.nl] // 4, self.reg_max * 4, 16)), 4), max((ch[self.nl], min((self.nc * 2, 128)))) # channels self.cv2 = nn.ModuleList( nn.Sequential(Conv(x, c2, 3), Conv(c2, c2, 3, g=4), nn.Conv2d(c2, 4 * self.reg_max, 1, groups=4)) for x in ch[:self.nl]) self.cv3 = nn.ModuleList( nn.Sequential(Conv(x, c3, 3), Conv(c3, c3, 3), nn.Conv2d(c3, self.nc, 1)) for x in ch[:self.nl]) self.cv4 = nn.ModuleList( nn.Sequential(Conv(x, c4, 3), Conv(c4, c4, 3, g=4), nn.Conv2d(c4, 4 * self.reg_max, 1, groups=4)) for x in ch[self.nl:]) self.cv5 = nn.ModuleList( nn.Sequential(Conv(x, c5, 3), Conv(c5, c5, 3), nn.Conv2d(c5, self.nc, 1)) for x in ch[self.nl:]) self.dfl = DFL(self.reg_max) self.dfl2 = DFL(self.reg_max) def forward(self, x): shape = x[0].shape # BCHW d1 = [] d2 = [] for i in range(self.nl): d1.append(torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1)) d2.append(torch.cat((self.cv4[i](x[self.nl+i]), self.cv5[i](x[self.nl+i])), 1)) if self.training: return [d1, d2] elif self.dynamic or self.shape != shape: self.anchors, self.strides = (d1.transpose(0, 1) for d1 in make_anchors(d1, self.stride, 0.5)) self.shape = shape box, cls = torch.cat([di.view(shape[0], self.no, -1) for di in d1], 2).split((self.reg_max * 4, self.nc), 1) dbox = dist2bbox(self.dfl(box), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides box2, cls2 = torch.cat([di.view(shape[0], self.no, -1) for di in d2], 2).split((self.reg_max * 4, self.nc), 1) dbox2 = dist2bbox(self.dfl2(box2), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides y = [torch.cat((dbox, cls.sigmoid()), 1), torch.cat((dbox2, cls2.sigmoid()), 1)] return y if self.export else (y, [d1, d2]) #y = torch.cat((dbox2, cls2.sigmoid()), 1) #return y if self.export else (y, d2) #y1 = torch.cat((dbox, cls.sigmoid()), 1) #y2 = torch.cat((dbox2, cls2.sigmoid()), 1) #return [y1, y2] if self.export else [(y1, d1), (y2, d2)] #return [y1, y2] if self.export else [(y1, y2), (d1, d2)] def bias_init(self): # Initialize Detect() biases, WARNING: requires stride availability m = self # self.model[-1] # Detect() module # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1 # ncf = math.log(0.6 / (m.nc - 0.999999)) if cf is None else torch.log(cf / cf.sum()) # nominal class frequency for a, b, s in zip(m.cv2, m.cv3, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) for a, b, s in zip(m.cv4, m.cv5, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) class TripleDetect(nn.Module): # YOLO Detect head for detection models dynamic = False # force grid reconstruction export = False # export mode shape = None anchors = torch.empty(0) # init strides = torch.empty(0) # init def __init__(self, nc=80, ch=(), inplace=True): # detection layer super().__init__() self.nc = nc # number of classes self.nl = len(ch) // 3 # number of detection layers self.reg_max = 16 self.no = nc + self.reg_max * 4 # number of outputs per anchor self.inplace = inplace # use inplace ops (e.g. slice assignment) self.stride = torch.zeros(self.nl) # strides computed during build c2, c3 = max((ch[0] // 4, self.reg_max * 4, 16)), max((ch[0], min((self.nc * 2, 128)))) # channels c4, c5 = max((ch[self.nl] // 4, self.reg_max * 4, 16)), max((ch[self.nl], min((self.nc * 2, 128)))) # channels c6, c7 = max((ch[self.nl * 2] // 4, self.reg_max * 4, 16)), max((ch[self.nl * 2], min((self.nc * 2, 128)))) # channels self.cv2 = nn.ModuleList( nn.Sequential(Conv(x, c2, 3), Conv(c2, c2, 3), nn.Conv2d(c2, 4 * self.reg_max, 1)) for x in ch[:self.nl]) self.cv3 = nn.ModuleList( nn.Sequential(Conv(x, c3, 3), Conv(c3, c3, 3), nn.Conv2d(c3, self.nc, 1)) for x in ch[:self.nl]) self.cv4 = nn.ModuleList( nn.Sequential(Conv(x, c4, 3), Conv(c4, c4, 3), nn.Conv2d(c4, 4 * self.reg_max, 1)) for x in ch[self.nl:self.nl*2]) self.cv5 = nn.ModuleList( nn.Sequential(Conv(x, c5, 3), Conv(c5, c5, 3), nn.Conv2d(c5, self.nc, 1)) for x in ch[self.nl:self.nl*2]) self.cv6 = nn.ModuleList( nn.Sequential(Conv(x, c6, 3), Conv(c6, c6, 3), nn.Conv2d(c6, 4 * self.reg_max, 1)) for x in ch[self.nl*2:self.nl*3]) self.cv7 = nn.ModuleList( nn.Sequential(Conv(x, c7, 3), Conv(c7, c7, 3), nn.Conv2d(c7, self.nc, 1)) for x in ch[self.nl*2:self.nl*3]) self.dfl = DFL(self.reg_max) self.dfl2 = DFL(self.reg_max) self.dfl3 = DFL(self.reg_max) def forward(self, x): shape = x[0].shape # BCHW d1 = [] d2 = [] d3 = [] for i in range(self.nl): d1.append(torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1)) d2.append(torch.cat((self.cv4[i](x[self.nl+i]), self.cv5[i](x[self.nl+i])), 1)) d3.append(torch.cat((self.cv6[i](x[self.nl*2+i]), self.cv7[i](x[self.nl*2+i])), 1)) if self.training: return [d1, d2, d3] elif self.dynamic or self.shape != shape: self.anchors, self.strides = (d1.transpose(0, 1) for d1 in make_anchors(d1, self.stride, 0.5)) self.shape = shape box, cls = torch.cat([di.view(shape[0], self.no, -1) for di in d1], 2).split((self.reg_max * 4, self.nc), 1) dbox = dist2bbox(self.dfl(box), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides box2, cls2 = torch.cat([di.view(shape[0], self.no, -1) for di in d2], 2).split((self.reg_max * 4, self.nc), 1) dbox2 = dist2bbox(self.dfl2(box2), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides box3, cls3 = torch.cat([di.view(shape[0], self.no, -1) for di in d3], 2).split((self.reg_max * 4, self.nc), 1) dbox3 = dist2bbox(self.dfl3(box3), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides y = [torch.cat((dbox, cls.sigmoid()), 1), torch.cat((dbox2, cls2.sigmoid()), 1), torch.cat((dbox3, cls3.sigmoid()), 1)] return y if self.export else (y, [d1, d2, d3]) def bias_init(self): # Initialize Detect() biases, WARNING: requires stride availability m = self # self.model[-1] # Detect() module # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1 # ncf = math.log(0.6 / (m.nc - 0.999999)) if cf is None else torch.log(cf / cf.sum()) # nominal class frequency for a, b, s in zip(m.cv2, m.cv3, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) for a, b, s in zip(m.cv4, m.cv5, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) for a, b, s in zip(m.cv6, m.cv7, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) class TripleDDetect(nn.Module): # YOLO Detect head for detection models dynamic = False # force grid reconstruction export = False # export mode shape = None anchors = torch.empty(0) # init strides = torch.empty(0) # init def __init__(self, nc=80, ch=(), inplace=True): # detection layer super().__init__() self.nc = nc # number of classes self.nl = len(ch) // 3 # number of detection layers self.reg_max = 16 self.no = nc + self.reg_max * 4 # number of outputs per anchor self.inplace = inplace # use inplace ops (e.g. slice assignment) self.stride = torch.zeros(self.nl) # strides computed during build c2, c3 = make_divisible(max((ch[0] // 4, self.reg_max * 4, 16)), 4), \ max((ch[0], min((self.nc * 2, 128)))) # channels c4, c5 = make_divisible(max((ch[self.nl] // 4, self.reg_max * 4, 16)), 4), \ max((ch[self.nl], min((self.nc * 2, 128)))) # channels c6, c7 = make_divisible(max((ch[self.nl * 2] // 4, self.reg_max * 4, 16)), 4), \ max((ch[self.nl * 2], min((self.nc * 2, 128)))) # channels self.cv2 = nn.ModuleList( nn.Sequential(Conv(x, c2, 3), Conv(c2, c2, 3, g=4), nn.Conv2d(c2, 4 * self.reg_max, 1, groups=4)) for x in ch[:self.nl]) self.cv3 = nn.ModuleList( nn.Sequential(Conv(x, c3, 3), Conv(c3, c3, 3), nn.Conv2d(c3, self.nc, 1)) for x in ch[:self.nl]) self.cv4 = nn.ModuleList( nn.Sequential(Conv(x, c4, 3), Conv(c4, c4, 3, g=4), nn.Conv2d(c4, 4 * self.reg_max, 1, groups=4)) for x in ch[self.nl:self.nl*2]) self.cv5 = nn.ModuleList( nn.Sequential(Conv(x, c5, 3), Conv(c5, c5, 3), nn.Conv2d(c5, self.nc, 1)) for x in ch[self.nl:self.nl*2]) self.cv6 = nn.ModuleList( nn.Sequential(Conv(x, c6, 3), Conv(c6, c6, 3, g=4), nn.Conv2d(c6, 4 * self.reg_max, 1, groups=4)) for x in ch[self.nl*2:self.nl*3]) self.cv7 = nn.ModuleList( nn.Sequential(Conv(x, c7, 3), Conv(c7, c7, 3), nn.Conv2d(c7, self.nc, 1)) for x in ch[self.nl*2:self.nl*3]) self.dfl = DFL(self.reg_max) self.dfl2 = DFL(self.reg_max) self.dfl3 = DFL(self.reg_max) def forward(self, x): shape = x[0].shape # BCHW d1 = [] d2 = [] d3 = [] for i in range(self.nl): d1.append(torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1)) d2.append(torch.cat((self.cv4[i](x[self.nl+i]), self.cv5[i](x[self.nl+i])), 1)) d3.append(torch.cat((self.cv6[i](x[self.nl*2+i]), self.cv7[i](x[self.nl*2+i])), 1)) if self.training: return [d1, d2, d3] elif self.dynamic or self.shape != shape: self.anchors, self.strides = (d1.transpose(0, 1) for d1 in make_anchors(d1, self.stride, 0.5)) self.shape = shape box, cls = torch.cat([di.view(shape[0], self.no, -1) for di in d1], 2).split((self.reg_max * 4, self.nc), 1) dbox = dist2bbox(self.dfl(box), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides box2, cls2 = torch.cat([di.view(shape[0], self.no, -1) for di in d2], 2).split((self.reg_max * 4, self.nc), 1) dbox2 = dist2bbox(self.dfl2(box2), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides box3, cls3 = torch.cat([di.view(shape[0], self.no, -1) for di in d3], 2).split((self.reg_max * 4, self.nc), 1) dbox3 = dist2bbox(self.dfl3(box3), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.strides #y = [torch.cat((dbox, cls.sigmoid()), 1), torch.cat((dbox2, cls2.sigmoid()), 1), torch.cat((dbox3, cls3.sigmoid()), 1)] #return y if self.export else (y, [d1, d2, d3]) y = torch.cat((dbox3, cls3.sigmoid()), 1) return y if self.export else (y, d3) def bias_init(self): # Initialize Detect() biases, WARNING: requires stride availability m = self # self.model[-1] # Detect() module # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1 # ncf = math.log(0.6 / (m.nc - 0.999999)) if cf is None else torch.log(cf / cf.sum()) # nominal class frequency for a, b, s in zip(m.cv2, m.cv3, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) for a, b, s in zip(m.cv4, m.cv5, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) for a, b, s in zip(m.cv6, m.cv7, m.stride): # from a[-1].bias.data[:] = 1.0 # box b[-1].bias.data[:m.nc] = math.log(5 / m.nc / (640 / s) ** 2) # cls (5 objects and 80 classes per 640 image) class Segment(Detect): # YOLO Segment head for segmentation models def __init__(self, nc=80, nm=32, npr=256, ch=(), inplace=True): super().__init__(nc, ch, inplace) self.nm = nm # number of masks self.npr = npr # number of protos self.proto = Proto(ch[0], self.npr, self.nm) # protos self.detect = Detect.forward c4 = max(ch[0] // 4, self.nm) self.cv4 = nn.ModuleList(nn.Sequential(Conv(x, c4, 3), Conv(c4, c4, 3), nn.Conv2d(c4, self.nm, 1)) for x in ch) def forward(self, x): p = self.proto(x[0]) bs = p.shape[0] mc = torch.cat([self.cv4[i](x[i]).view(bs, self.nm, -1) for i in range(self.nl)], 2) # mask coefficients x = self.detect(self, x) if self.training: return x, mc, p return (torch.cat([x, mc], 1), p) if self.export else (torch.cat([x[0], mc], 1), (x[1], mc, p)) class Panoptic(Detect): # YOLO Panoptic head for panoptic segmentation models def __init__(self, nc=80, sem_nc=93, nm=32, npr=256, ch=(), inplace=True): super().__init__(nc, ch, inplace) self.sem_nc = sem_nc self.nm = nm # number of masks self.npr = npr # number of protos self.proto = Proto(ch[0], self.npr, self.nm) # protos self.uconv = UConv(ch[0], ch[0]//4, self.sem_nc+self.nc) self.detect = Detect.forward c4 = max(ch[0] // 4, self.nm) self.cv4 = nn.ModuleList(nn.Sequential(Conv(x, c4, 3), Conv(c4, c4, 3), nn.Conv2d(c4, self.nm, 1)) for x in ch) def forward(self, x): p = self.proto(x[0]) s = self.uconv(x[0]) bs = p.shape[0] mc = torch.cat([self.cv4[i](x[i]).view(bs, self.nm, -1) for i in range(self.nl)], 2) # mask coefficients x = self.detect(self, x) if self.training: return x, mc, p, s return (torch.cat([x, mc], 1), p, s) if self.export else (torch.cat([x[0], mc], 1), (x[1], mc, p, s)) class BaseModel(nn.Module): # YOLO base model def forward(self, x, profile=False, visualize=False): return self._forward_once(x, profile, visualize) # single-scale inference, train def _forward_once(self, x, profile=False, visualize=False): y, dt = [], [] # outputs for m in self.model: if m.f != -1: # if not from previous layer x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers if profile: self._profile_one_layer(m, x, dt) x = m(x) # run y.append(x if m.i in self.save else None) # save output if visualize: feature_visualization(x, m.type, m.i, save_dir=visualize) return x def _profile_one_layer(self, m, x, dt): c = m == self.model[-1] # is final layer, copy input as inplace fix o = thop.profile(m, inputs=(x.copy() if c else x,), verbose=False)[0] / 1E9 * 2 if thop else 0 # FLOPs t = time_sync() for _ in range(10): m(x.copy() if c else x) dt.append((time_sync() - t) * 100) if m == self.model[0]: LOGGER.info(f"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s} module") LOGGER.info(f'{dt[-1]:10.2f} {o:10.2f} {m.np:10.0f} {m.type}') if c: LOGGER.info(f"{sum(dt):10.2f} {'-':>10s} {'-':>10s} Total") def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers LOGGER.info('Fusing layers... ') for m in self.model.modules(): if isinstance(m, (RepConvN)) and hasattr(m, 'fuse_convs'): m.fuse_convs() m.forward = m.forward_fuse # update forward if isinstance(m, (Conv, DWConv)) and hasattr(m, 'bn'): m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv delattr(m, 'bn') # remove batchnorm m.forward = m.forward_fuse # update forward self.info() return self def info(self, verbose=False, img_size=640): # print model information model_info(self, verbose, img_size) def _apply(self, fn): # Apply to(), cpu(), cuda(), half() to model tensors that are not parameters or registered buffers self = super()._apply(fn) m = self.model[-1] # Detect() if isinstance(m, (Detect, DualDetect, TripleDetect, DDetect, DualDDetect, TripleDDetect, Segment, Panoptic)): m.stride = fn(m.stride) m.anchors = fn(m.anchors) m.strides = fn(m.strides) # m.grid = list(map(fn, m.grid)) return self class DetectionModel(BaseModel): # YOLO detection model def __init__(self, cfg='yolo.yaml', ch=3, nc=None, anchors=None): # model, input channels, number of classes super().__init__() if isinstance(cfg, dict): self.yaml = cfg # model dict else: # is *.yaml import yaml # for torch hub self.yaml_file = Path(cfg).name with open(cfg, encoding='ascii', errors='ignore') as f: self.yaml = yaml.safe_load(f) # model dict # Define model ch = self.yaml['ch'] = self.yaml.get('ch', ch) # input channels if nc and nc != self.yaml['nc']: LOGGER.info(f"Overriding model.yaml nc={self.yaml['nc']} with nc={nc}") self.yaml['nc'] = nc # override yaml value if anchors: LOGGER.info(f'Overriding model.yaml anchors with anchors={anchors}') self.yaml['anchors'] = round(anchors) # override yaml value self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist self.names = [str(i) for i in range(self.yaml['nc'])] # default names self.inplace = self.yaml.get('inplace', True) # Build strides, anchors m = self.model[-1] # Detect() if isinstance(m, (Detect, DDetect, Segment, Panoptic)): s = 256 # 2x min stride m.inplace = self.inplace forward = lambda x: self.forward(x)[0] if isinstance(m, (Segment, Panoptic)) else self.forward(x) m.stride = torch.tensor([s / x.shape[-2] for x in forward(torch.zeros(1, ch, s, s))]) # forward # check_anchor_order(m) # m.anchors /= m.stride.view(-1, 1, 1) self.stride = m.stride m.bias_init() # only run once if isinstance(m, (DualDetect, TripleDetect, DualDDetect, TripleDDetect)): s = 256 # 2x min stride m.inplace = self.inplace #forward = lambda x: self.forward(x)[0][0] if isinstance(m, (DualSegment, DualPanoptic)) else self.forward(x)[0] forward = lambda x: self.forward(x)[0] m.stride = torch.tensor([s / x.shape[-2] for x in forward(torch.zeros(1, ch, s, s))]) # forward # check_anchor_order(m) # m.anchors /= m.stride.view(-1, 1, 1) self.stride = m.stride m.bias_init() # only run once # Init weights, biases initialize_weights(self) self.info() LOGGER.info('') def forward(self, x, augment=False, profile=False, visualize=False): if augment: return self._forward_augment(x) # augmented inference, None return self._forward_once(x, profile, visualize) # single-scale inference, train def _forward_augment(self, x): img_size = x.shape[-2:] # height, width s = [1, 0.83, 0.67] # scales f = [None, 3, None] # flips (2-ud, 3-lr) y = [] # outputs for si, fi in zip(s, f): xi = scale_img(x.flip(fi) if fi else x, si, gs=int(self.stride.max())) yi = self._forward_once(xi)[0] # forward # cv2.imwrite(f'img_{si}.jpg', 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1]) # save yi = self._descale_pred(yi, fi, si, img_size) y.append(yi) y = self._clip_augmented(y) # clip augmented tails return torch.cat(y, 1), None # augmented inference, train def _descale_pred(self, p, flips, scale, img_size): # de-scale predictions following augmented inference (inverse operation) if self.inplace: p[..., :4] /= scale # de-scale if flips == 2: p[..., 1] = img_size[0] - p[..., 1] # de-flip ud elif flips == 3: p[..., 0] = img_size[1] - p[..., 0] # de-flip lr else: x, y, wh = p[..., 0:1] / scale, p[..., 1:2] / scale, p[..., 2:4] / scale # de-scale if flips == 2: y = img_size[0] - y # de-flip ud elif flips == 3: x = img_size[1] - x # de-flip lr p = torch.cat((x, y, wh, p[..., 4:]), -1) return p def _clip_augmented(self, y): # Clip YOLO augmented inference tails nl = self.model[-1].nl # number of detection layers (P3-P5) g = sum(4 ** x for x in range(nl)) # grid points e = 1 # exclude layer count i = (y[0].shape[1] // g) * sum(4 ** x for x in range(e)) # indices y[0] = y[0][:, :-i] # large i = (y[-1].shape[1] // g) * sum(4 ** (nl - 1 - x) for x in range(e)) # indices y[-1] = y[-1][:, i:] # small return y Model = DetectionModel # retain YOLO 'Model' class for backwards compatibility class SegmentationModel(DetectionModel): # YOLO segmentation model def __init__(self, cfg='yolo-seg.yaml', ch=3, nc=None, anchors=None): super().__init__(cfg, ch, nc, anchors) class ClassificationModel(BaseModel): # YOLO classification model def __init__(self, cfg=None, model=None, nc=1000, cutoff=10): # yaml, model, number of classes, cutoff index super().__init__() self._from_detection_model(model, nc, cutoff) if model is not None else self._from_yaml(cfg) def _from_detection_model(self, model, nc=1000, cutoff=10): # Create a YOLO classification model from a YOLO detection model if isinstance(model, DetectMultiBackend): model = model.model # unwrap DetectMultiBackend model.model = model.model[:cutoff] # backbone m = model.model[-1] # last layer ch = m.conv.in_channels if hasattr(m, 'conv') else m.cv1.conv.in_channels # ch into module c = Classify(ch, nc) # Classify() c.i, c.f, c.type = m.i, m.f, 'models.common.Classify' # index, from, type model.model[-1] = c # replace self.model = model.model self.stride = model.stride self.save = [] self.nc = nc def _from_yaml(self, cfg): # Create a YOLO classification model from a *.yaml file self.model = None def parse_model(d, ch): # model_dict, input_channels(3) # Parse a YOLO model.yaml dictionary LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}") anchors, nc, gd, gw, act = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple'], d.get('activation') if act: Conv.default_act = eval(act) # redefine default activation, i.e. Conv.default_act = nn.SiLU() RepConvN.default_act = eval(act) # redefine default activation, i.e. Conv.default_act = nn.SiLU() LOGGER.info(f"{colorstr('activation:')} {act}") # print na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors no = na * (nc + 5) # number of outputs = anchors * (classes + 5) layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number, module, args m = eval(m) if isinstance(m, str) else m # eval strings for j, a in enumerate(args): with contextlib.suppress(NameError): args[j] = eval(a) if isinstance(a, str) else a # eval strings n = n_ = max(round(n * gd), 1) if n > 1 else n # depth gain if m in { Conv, AConv, ConvTranspose, Bottleneck, SPP, SPPF, DWConv, BottleneckCSP, nn.ConvTranspose2d, DWConvTranspose2d, SPPCSPC, ADown, RepNCSPELAN4, SPPELAN}: c1, c2 = ch[f], args[0] if c2 != no: # if not output c2 = make_divisible(c2 * gw, 8) args = [c1, c2, *args[1:]] if m in {BottleneckCSP, SPPCSPC}: args.insert(2, n) # number of repeats n = 1 elif m is nn.BatchNorm2d: args = [ch[f]] elif m is Concat: c2 = sum(ch[x] for x in f) elif m is Shortcut: c2 = ch[f[0]] elif m is ReOrg: c2 = ch[f] * 4 elif m is CBLinear: c2 = args[0] c1 = ch[f] args = [c1, c2, *args[1:]] elif m is CBFuse: c2 = ch[f[-1]] # TODO: channel, gw, gd elif m in {Detect, DualDetect, TripleDetect, DDetect, DualDDetect, TripleDDetect, Segment, Panoptic}: args.append([ch[x] for x in f]) # if isinstance(args[1], int): # number of anchors # args[1] = [list(range(args[1] * 2))] * len(f) if m in {Segment, Panoptic}: args[2] = make_divisible(args[2] * gw, 8) elif m is Contract: c2 = ch[f] * args[0] ** 2 elif m is Expand: c2 = ch[f] // args[0] ** 2 else: c2 = ch[f] m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module t = str(m)[8:-2].replace('__main__.', '') # module type np = sum(x.numel() for x in m_.parameters()) # number params m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params LOGGER.info(f'{i:>3}{str(f):>18}{n_:>3}{np:10.0f} {t:<40}{str(args):<30}') # print save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist layers.append(m_) if i == 0: ch = [] ch.append(c2) return nn.Sequential(*layers), sorted(save) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--cfg', type=str, default='yolo.yaml', help='model.yaml') parser.add_argument('--batch-size', type=int, default=1, help='total batch size for all GPUs') parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') parser.add_argument('--profile', action='store_true', help='profile model speed') parser.add_argument('--line-profile', action='store_true', help='profile model speed layer by layer') parser.add_argument('--test', action='store_true', help='test all yolo*.yaml') opt = parser.parse_args() opt.cfg = check_yaml(opt.cfg) # check YAML print_args(vars(opt)) device = select_device(opt.device) # Create model im = torch.rand(opt.batch_size, 3, 640, 640).to(device) model = Model(opt.cfg).to(device) model.eval() # Options if opt.line_profile: # profile layer by layer model(im, profile=True) elif opt.profile: # profile forward-backward results = profile(input=im, ops=[model], n=3) elif opt.test: # test all models for cfg in Path(ROOT / 'models').rglob('yolo*.yaml'): try: _ = Model(cfg) except Exception as e: print(f'Error in {cfg}: {e}') else: # report fused model summary model.fuse() ================================================ FILE: asone/detectors/yolov9/yolov9/utils/__init__.py ================================================ import contextlib import platform import threading def emojis(str=''): # Return platform-dependent emoji-safe version of string return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str class TryExcept(contextlib.ContextDecorator): # YOLOv5 TryExcept class. Usage: @TryExcept() decorator or 'with TryExcept():' context manager def __init__(self, msg=''): self.msg = msg def __enter__(self): pass def __exit__(self, exc_type, value, traceback): if value: print(emojis(f"{self.msg}{': ' if self.msg else ''}{value}")) return True def threaded(func): # Multi-threads a target function and returns thread. Usage: @threaded decorator def wrapper(*args, **kwargs): thread = threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True) thread.start() return thread return wrapper def join_threads(verbose=False): # Join all daemon threads, i.e. atexit.register(lambda: join_threads()) main_thread = threading.current_thread() for t in threading.enumerate(): if t is not main_thread: if verbose: print(f'Joining thread {t.name}') t.join() def notebook_init(verbose=True): # Check system software and hardware print('Checking setup...') import os import shutil from utils.general import check_font, check_requirements, is_colab from utils.torch_utils import select_device # imports check_font() import psutil from IPython import display # to display images and clear console output if is_colab(): shutil.rmtree('/content/sample_data', ignore_errors=True) # remove colab /sample_data directory # System info if verbose: gb = 1 << 30 # bytes to GiB (1024 ** 3) ram = psutil.virtual_memory().total total, used, free = shutil.disk_usage("/") display.clear_output() s = f'({os.cpu_count()} CPUs, {ram / gb:.1f} GB RAM, {(total - free) / gb:.1f}/{total / gb:.1f} GB disk)' else: s = '' select_device(newline=False) print(emojis(f'Setup complete ✅ {s}')) return display ================================================ FILE: asone/detectors/yolov9/yolov9/utils/activations.py ================================================ import torch import torch.nn as nn import torch.nn.functional as F class SiLU(nn.Module): # SiLU activation https://arxiv.org/pdf/1606.08415.pdf @staticmethod def forward(x): return x * torch.sigmoid(x) class Hardswish(nn.Module): # Hard-SiLU activation @staticmethod def forward(x): # return x * F.hardsigmoid(x) # for TorchScript and CoreML return x * F.hardtanh(x + 3, 0.0, 6.0) / 6.0 # for TorchScript, CoreML and ONNX class Mish(nn.Module): # Mish activation https://github.com/digantamisra98/Mish @staticmethod def forward(x): return x * F.softplus(x).tanh() class MemoryEfficientMish(nn.Module): # Mish activation memory-efficient class F(torch.autograd.Function): @staticmethod def forward(ctx, x): ctx.save_for_backward(x) return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x))) @staticmethod def backward(ctx, grad_output): x = ctx.saved_tensors[0] sx = torch.sigmoid(x) fx = F.softplus(x).tanh() return grad_output * (fx + x * sx * (1 - fx * fx)) def forward(self, x): return self.F.apply(x) class FReLU(nn.Module): # FReLU activation https://arxiv.org/abs/2007.11824 def __init__(self, c1, k=3): # ch_in, kernel super().__init__() self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False) self.bn = nn.BatchNorm2d(c1) def forward(self, x): return torch.max(x, self.bn(self.conv(x))) class AconC(nn.Module): r""" ACON activation (activate or not) AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter according to "Activate or Not: Learning Customized Activation" . """ def __init__(self, c1): super().__init__() self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1)) self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1)) self.beta = nn.Parameter(torch.ones(1, c1, 1, 1)) def forward(self, x): dpx = (self.p1 - self.p2) * x return dpx * torch.sigmoid(self.beta * dpx) + self.p2 * x class MetaAconC(nn.Module): r""" ACON activation (activate or not) MetaAconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is generated by a small network according to "Activate or Not: Learning Customized Activation" . """ def __init__(self, c1, k=1, s=1, r=16): # ch_in, kernel, stride, r super().__init__() c2 = max(r, c1 // r) self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1)) self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1)) self.fc1 = nn.Conv2d(c1, c2, k, s, bias=True) self.fc2 = nn.Conv2d(c2, c1, k, s, bias=True) # self.bn1 = nn.BatchNorm2d(c2) # self.bn2 = nn.BatchNorm2d(c1) def forward(self, x): y = x.mean(dim=2, keepdims=True).mean(dim=3, keepdims=True) # batch-size 1 bug/instabilities https://github.com/ultralytics/yolov5/issues/2891 # beta = torch.sigmoid(self.bn2(self.fc2(self.bn1(self.fc1(y))))) # bug/unstable beta = torch.sigmoid(self.fc2(self.fc1(y))) # bug patch BN layers removed dpx = (self.p1 - self.p2) * x return dpx * torch.sigmoid(beta * dpx) + self.p2 * x ================================================ FILE: asone/detectors/yolov9/yolov9/utils/augmentations.py ================================================ import math import random import cv2 import numpy as np import torch import torchvision.transforms as T import torchvision.transforms.functional as TF from asone.detectors.yolov9.yolov9.utils.general import LOGGER, check_version, colorstr, resample_segments, segment2box, xywhn2xyxy from asone.detectors.yolov9.yolov9.utils.metrics import bbox_ioa IMAGENET_MEAN = 0.485, 0.456, 0.406 # RGB mean IMAGENET_STD = 0.229, 0.224, 0.225 # RGB standard deviation class Albumentations: # YOLOv5 Albumentations class (optional, only used if package is installed) def __init__(self, size=640): self.transform = None prefix = colorstr('albumentations: ') try: import albumentations as A check_version(A.__version__, '1.0.3', hard=True) # version requirement T = [ A.RandomResizedCrop(height=size, width=size, scale=(0.8, 1.0), ratio=(0.9, 1.11), p=0.0), A.Blur(p=0.01), A.MedianBlur(p=0.01), A.ToGray(p=0.01), A.CLAHE(p=0.01), A.RandomBrightnessContrast(p=0.0), A.RandomGamma(p=0.0), A.ImageCompression(quality_lower=75, p=0.0)] # transforms self.transform = A.Compose(T, bbox_params=A.BboxParams(format='yolo', label_fields=['class_labels'])) LOGGER.info(prefix + ', '.join(f'{x}'.replace('always_apply=False, ', '') for x in T if x.p)) except ImportError: # package not installed, skip pass except Exception as e: LOGGER.info(f'{prefix}{e}') def __call__(self, im, labels, p=1.0): if self.transform and random.random() < p: new = self.transform(image=im, bboxes=labels[:, 1:], class_labels=labels[:, 0]) # transformed im, labels = new['image'], np.array([[c, *b] for c, b in zip(new['class_labels'], new['bboxes'])]) return im, labels def normalize(x, mean=IMAGENET_MEAN, std=IMAGENET_STD, inplace=False): # Denormalize RGB images x per ImageNet stats in BCHW format, i.e. = (x - mean) / std return TF.normalize(x, mean, std, inplace=inplace) def denormalize(x, mean=IMAGENET_MEAN, std=IMAGENET_STD): # Denormalize RGB images x per ImageNet stats in BCHW format, i.e. = x * std + mean for i in range(3): x[:, i] = x[:, i] * std[i] + mean[i] return x def augment_hsv(im, hgain=0.5, sgain=0.5, vgain=0.5): # HSV color-space augmentation if hgain or sgain or vgain: r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains hue, sat, val = cv2.split(cv2.cvtColor(im, cv2.COLOR_BGR2HSV)) dtype = im.dtype # uint8 x = np.arange(0, 256, dtype=r.dtype) lut_hue = ((x * r[0]) % 180).astype(dtype) lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) lut_val = np.clip(x * r[2], 0, 255).astype(dtype) im_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))) cv2.cvtColor(im_hsv, cv2.COLOR_HSV2BGR, dst=im) # no return needed def hist_equalize(im, clahe=True, bgr=False): # Equalize histogram on BGR image 'im' with im.shape(n,m,3) and range 0-255 yuv = cv2.cvtColor(im, cv2.COLOR_BGR2YUV if bgr else cv2.COLOR_RGB2YUV) if clahe: c = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) yuv[:, :, 0] = c.apply(yuv[:, :, 0]) else: yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0]) # equalize Y channel histogram return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR if bgr else cv2.COLOR_YUV2RGB) # convert YUV image to RGB def replicate(im, labels): # Replicate labels h, w = im.shape[:2] boxes = labels[:, 1:].astype(int) x1, y1, x2, y2 = boxes.T s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels) for i in s.argsort()[:round(s.size * 0.5)]: # smallest indices x1b, y1b, x2b, y2b = boxes[i] bh, bw = y2b - y1b, x2b - x1b yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh] im[y1a:y2a, x1a:x2a] = im[y1b:y2b, x1b:x2b] # im4[ymin:ymax, xmin:xmax] labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0) return im, labels def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32): # Resize and pad image while meeting stride-multiple constraints shape = im.shape[:2] # current shape [height, width] if isinstance(new_shape, int): new_shape = (new_shape, new_shape) # Scale ratio (new / old) r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) if not scaleup: # only scale down, do not scale up (for better val mAP) r = min(r, 1.0) # Compute padding ratio = r, r # width, height ratios new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding if auto: # minimum rectangle dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding elif scaleFill: # stretch dw, dh = 0.0, 0.0 new_unpad = (new_shape[1], new_shape[0]) ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios dw /= 2 # divide padding into 2 sides dh /= 2 if shape[::-1] != new_unpad: # resize im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border return im, ratio, (dw, dh) def random_perspective(im, targets=(), segments=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0, border=(0, 0)): # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(0.1, 0.1), scale=(0.9, 1.1), shear=(-10, 10)) # targets = [cls, xyxy] height = im.shape[0] + border[0] * 2 # shape(h,w,c) width = im.shape[1] + border[1] * 2 # Center C = np.eye(3) C[0, 2] = -im.shape[1] / 2 # x translation (pixels) C[1, 2] = -im.shape[0] / 2 # y translation (pixels) # Perspective P = np.eye(3) P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y) P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x) # Rotation and Scale R = np.eye(3) a = random.uniform(-degrees, degrees) # a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations s = random.uniform(1 - scale, 1 + scale) # s = 2 ** random.uniform(-scale, scale) R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s) # Shear S = np.eye(3) S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg) S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg) # Translation T = np.eye(3) T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels) T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels) # Combined rotation matrix M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed if perspective: im = cv2.warpPerspective(im, M, dsize=(width, height), borderValue=(114, 114, 114)) else: # affine im = cv2.warpAffine(im, M[:2], dsize=(width, height), borderValue=(114, 114, 114)) # Visualize # import matplotlib.pyplot as plt # ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel() # ax[0].imshow(im[:, :, ::-1]) # base # ax[1].imshow(im2[:, :, ::-1]) # warped # Transform label coordinates n = len(targets) if n: use_segments = any(x.any() for x in segments) new = np.zeros((n, 4)) if use_segments: # warp segments segments = resample_segments(segments) # upsample for i, segment in enumerate(segments): xy = np.ones((len(segment), 3)) xy[:, :2] = segment xy = xy @ M.T # transform xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2] # perspective rescale or affine # clip new[i] = segment2box(xy, width, height) else: # warp boxes xy = np.ones((n * 4, 3)) xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1 xy = xy @ M.T # transform xy = (xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]).reshape(n, 8) # perspective rescale or affine # create new boxes x = xy[:, [0, 2, 4, 6]] y = xy[:, [1, 3, 5, 7]] new = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T # clip new[:, [0, 2]] = new[:, [0, 2]].clip(0, width) new[:, [1, 3]] = new[:, [1, 3]].clip(0, height) # filter candidates i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01 if use_segments else 0.10) targets = targets[i] targets[:, 1:5] = new[i] return im, targets def copy_paste(im, labels, segments, p=0.5): # Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy) n = len(segments) if p and n: h, w, c = im.shape # height, width, channels im_new = np.zeros(im.shape, np.uint8) # calculate ioa first then select indexes randomly boxes = np.stack([w - labels[:, 3], labels[:, 2], w - labels[:, 1], labels[:, 4]], axis=-1) # (n, 4) ioa = bbox_ioa(boxes, labels[:, 1:5]) # intersection over area indexes = np.nonzero((ioa < 0.30).all(1))[0] # (N, ) n = len(indexes) for j in random.sample(list(indexes), k=round(p * n)): l, box, s = labels[j], boxes[j], segments[j] labels = np.concatenate((labels, [[l[0], *box]]), 0) segments.append(np.concatenate((w - s[:, 0:1], s[:, 1:2]), 1)) cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (1, 1, 1), cv2.FILLED) result = cv2.flip(im, 1) # augment segments (flip left-right) i = cv2.flip(im_new, 1).astype(bool) im[i] = result[i] # cv2.imwrite('debug.jpg', im) # debug return im, labels, segments def cutout(im, labels, p=0.5): # Applies image cutout augmentation https://arxiv.org/abs/1708.04552 if random.random() < p: h, w = im.shape[:2] scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16 # image size fraction for s in scales: mask_h = random.randint(1, int(h * s)) # create random masks mask_w = random.randint(1, int(w * s)) # box xmin = max(0, random.randint(0, w) - mask_w // 2) ymin = max(0, random.randint(0, h) - mask_h // 2) xmax = min(w, xmin + mask_w) ymax = min(h, ymin + mask_h) # apply random color mask im[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)] # return unobscured labels if len(labels) and s > 0.03: box = np.array([[xmin, ymin, xmax, ymax]], dtype=np.float32) ioa = bbox_ioa(box, xywhn2xyxy(labels[:, 1:5], w, h))[0] # intersection over area labels = labels[ioa < 0.60] # remove >60% obscured labels return labels def mixup(im, labels, im2, labels2): # Applies MixUp augmentation https://arxiv.org/pdf/1710.09412.pdf r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0 im = (im * r + im2 * (1 - r)).astype(np.uint8) labels = np.concatenate((labels, labels2), 0) return im, labels def box_candidates(box1, box2, wh_thr=2, ar_thr=100, area_thr=0.1, eps=1e-16): # box1(4,n), box2(4,n) # Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio w1, h1 = box1[2] - box1[0], box1[3] - box1[1] w2, h2 = box2[2] - box2[0], box2[3] - box2[1] ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps)) # aspect ratio return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr) # candidates def classify_albumentations( augment=True, size=224, scale=(0.08, 1.0), ratio=(0.75, 1.0 / 0.75), # 0.75, 1.33 hflip=0.5, vflip=0.0, jitter=0.4, mean=IMAGENET_MEAN, std=IMAGENET_STD, auto_aug=False): # YOLOv5 classification Albumentations (optional, only used if package is installed) prefix = colorstr('albumentations: ') try: import albumentations as A from albumentations.pytorch import ToTensorV2 check_version(A.__version__, '1.0.3', hard=True) # version requirement if augment: # Resize and crop T = [A.RandomResizedCrop(height=size, width=size, scale=scale, ratio=ratio)] if auto_aug: # TODO: implement AugMix, AutoAug & RandAug in albumentation LOGGER.info(f'{prefix}auto augmentations are currently not supported') else: if hflip > 0: T += [A.HorizontalFlip(p=hflip)] if vflip > 0: T += [A.VerticalFlip(p=vflip)] if jitter > 0: color_jitter = (float(jitter),) * 3 # repeat value for brightness, contrast, satuaration, 0 hue T += [A.ColorJitter(*color_jitter, 0)] else: # Use fixed crop for eval set (reproducibility) T = [A.SmallestMaxSize(max_size=size), A.CenterCrop(height=size, width=size)] T += [A.Normalize(mean=mean, std=std), ToTensorV2()] # Normalize and convert to Tensor LOGGER.info(prefix + ', '.join(f'{x}'.replace('always_apply=False, ', '') for x in T if x.p)) return A.Compose(T) except ImportError: # package not installed, skip LOGGER.warning(f'{prefix}⚠️ not found, install with `pip install albumentations` (recommended)') except Exception as e: LOGGER.info(f'{prefix}{e}') def classify_transforms(size=224): # Transforms to apply if albumentations not installed assert isinstance(size, int), f'ERROR: classify_transforms size {size} must be integer, not (list, tuple)' # T.Compose([T.ToTensor(), T.Resize(size), T.CenterCrop(size), T.Normalize(IMAGENET_MEAN, IMAGENET_STD)]) return T.Compose([CenterCrop(size), ToTensor(), T.Normalize(IMAGENET_MEAN, IMAGENET_STD)]) class LetterBox: # YOLOv5 LetterBox class for image preprocessing, i.e. T.Compose([LetterBox(size), ToTensor()]) def __init__(self, size=(640, 640), auto=False, stride=32): super().__init__() self.h, self.w = (size, size) if isinstance(size, int) else size self.auto = auto # pass max size integer, automatically solve for short side using stride self.stride = stride # used with auto def __call__(self, im): # im = np.array HWC imh, imw = im.shape[:2] r = min(self.h / imh, self.w / imw) # ratio of new/old h, w = round(imh * r), round(imw * r) # resized image hs, ws = (math.ceil(x / self.stride) * self.stride for x in (h, w)) if self.auto else self.h, self.w top, left = round((hs - h) / 2 - 0.1), round((ws - w) / 2 - 0.1) im_out = np.full((self.h, self.w, 3), 114, dtype=im.dtype) im_out[top:top + h, left:left + w] = cv2.resize(im, (w, h), interpolation=cv2.INTER_LINEAR) return im_out class CenterCrop: # YOLOv5 CenterCrop class for image preprocessing, i.e. T.Compose([CenterCrop(size), ToTensor()]) def __init__(self, size=640): super().__init__() self.h, self.w = (size, size) if isinstance(size, int) else size def __call__(self, im): # im = np.array HWC imh, imw = im.shape[:2] m = min(imh, imw) # min dimension top, left = (imh - m) // 2, (imw - m) // 2 return cv2.resize(im[top:top + m, left:left + m], (self.w, self.h), interpolation=cv2.INTER_LINEAR) class ToTensor: # YOLOv5 ToTensor class for image preprocessing, i.e. T.Compose([LetterBox(size), ToTensor()]) def __init__(self, half=False): super().__init__() self.half = half def __call__(self, im): # im = np.array HWC in BGR order im = np.ascontiguousarray(im.transpose((2, 0, 1))[::-1]) # HWC to CHW -> BGR to RGB -> contiguous im = torch.from_numpy(im) # to torch im = im.half() if self.half else im.float() # uint8 to fp16/32 im /= 255.0 # 0-255 to 0.0-1.0 return im ================================================ FILE: asone/detectors/yolov9/yolov9/utils/autoanchor.py ================================================ import random import numpy as np import torch import yaml from tqdm import tqdm from asone.detectors.yolov9.yolov9.utils import TryExcept from asone.detectors.yolov9.yolov9.utils.general import LOGGER, TQDM_BAR_FORMAT, colorstr PREFIX = colorstr('AutoAnchor: ') def check_anchor_order(m): # Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary a = m.anchors.prod(-1).mean(-1).view(-1) # mean anchor area per output layer da = a[-1] - a[0] # delta a ds = m.stride[-1] - m.stride[0] # delta s if da and (da.sign() != ds.sign()): # same order LOGGER.info(f'{PREFIX}Reversing anchor order') m.anchors[:] = m.anchors.flip(0) @TryExcept(f'{PREFIX}ERROR') def check_anchors(dataset, model, thr=4.0, imgsz=640): # Check anchor fit to data, recompute if necessary m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1] # Detect() shapes = imgsz * dataset.shapes / dataset.shapes.max(1, keepdims=True) scale = np.random.uniform(0.9, 1.1, size=(shapes.shape[0], 1)) # augment scale wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes * scale, dataset.labels)])).float() # wh def metric(k): # compute metric r = wh[:, None] / k[None] x = torch.min(r, 1 / r).min(2)[0] # ratio metric best = x.max(1)[0] # best_x aat = (x > 1 / thr).float().sum(1).mean() # anchors above threshold bpr = (best > 1 / thr).float().mean() # best possible recall return bpr, aat stride = m.stride.to(m.anchors.device).view(-1, 1, 1) # model strides anchors = m.anchors.clone() * stride # current anchors bpr, aat = metric(anchors.cpu().view(-1, 2)) s = f'\n{PREFIX}{aat:.2f} anchors/target, {bpr:.3f} Best Possible Recall (BPR). ' if bpr > 0.98: # threshold to recompute LOGGER.info(f'{s}Current anchors are a good fit to dataset ✅') else: LOGGER.info(f'{s}Anchors are a poor fit to dataset ⚠️, attempting to improve...') na = m.anchors.numel() // 2 # number of anchors anchors = kmean_anchors(dataset, n=na, img_size=imgsz, thr=thr, gen=1000, verbose=False) new_bpr = metric(anchors)[0] if new_bpr > bpr: # replace anchors anchors = torch.tensor(anchors, device=m.anchors.device).type_as(m.anchors) m.anchors[:] = anchors.clone().view_as(m.anchors) check_anchor_order(m) # must be in pixel-space (not grid-space) m.anchors /= stride s = f'{PREFIX}Done ✅ (optional: update model *.yaml to use these anchors in the future)' else: s = f'{PREFIX}Done ⚠️ (original anchors better than new anchors, proceeding with original anchors)' LOGGER.info(s) def kmean_anchors(dataset='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen=1000, verbose=True): """ Creates kmeans-evolved anchors from training dataset Arguments: dataset: path to data.yaml, or a loaded dataset n: number of anchors img_size: image size used for training thr: anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0 gen: generations to evolve anchors using genetic algorithm verbose: print all results Return: k: kmeans evolved anchors Usage: from utils.autoanchor import *; _ = kmean_anchors() """ from scipy.cluster.vq import kmeans npr = np.random thr = 1 / thr def metric(k, wh): # compute metrics r = wh[:, None] / k[None] x = torch.min(r, 1 / r).min(2)[0] # ratio metric # x = wh_iou(wh, torch.tensor(k)) # iou metric return x, x.max(1)[0] # x, best_x def anchor_fitness(k): # mutation fitness _, best = metric(torch.tensor(k, dtype=torch.float32), wh) return (best * (best > thr).float()).mean() # fitness def print_results(k, verbose=True): k = k[np.argsort(k.prod(1))] # sort small to large x, best = metric(k, wh0) bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n # best possible recall, anch > thr s = f'{PREFIX}thr={thr:.2f}: {bpr:.4f} best possible recall, {aat:.2f} anchors past thr\n' \ f'{PREFIX}n={n}, img_size={img_size}, metric_all={x.mean():.3f}/{best.mean():.3f}-mean/best, ' \ f'past_thr={x[x > thr].mean():.3f}-mean: ' for x in k: s += '%i,%i, ' % (round(x[0]), round(x[1])) if verbose: LOGGER.info(s[:-2]) return k if isinstance(dataset, str): # *.yaml file with open(dataset, errors='ignore') as f: data_dict = yaml.safe_load(f) # model dict from asone.detectors.yolov9.yolov9.utils.dataloaders import LoadImagesAndLabels dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True) # Get label wh shapes = img_size * dataset.shapes / dataset.shapes.max(1, keepdims=True) wh0 = np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)]) # wh # Filter i = (wh0 < 3.0).any(1).sum() if i: LOGGER.info(f'{PREFIX}WARNING ⚠️ Extremely small objects found: {i} of {len(wh0)} labels are <3 pixels in size') wh = wh0[(wh0 >= 2.0).any(1)].astype(np.float32) # filter > 2 pixels # wh = wh * (npr.rand(wh.shape[0], 1) * 0.9 + 0.1) # multiply by random scale 0-1 # Kmeans init try: LOGGER.info(f'{PREFIX}Running kmeans for {n} anchors on {len(wh)} points...') assert n <= len(wh) # apply overdetermined constraint s = wh.std(0) # sigmas for whitening k = kmeans(wh / s, n, iter=30)[0] * s # points assert n == len(k) # kmeans may return fewer points than requested if wh is insufficient or too similar except Exception: LOGGER.warning(f'{PREFIX}WARNING ⚠️ switching strategies from kmeans to random init') k = np.sort(npr.rand(n * 2)).reshape(n, 2) * img_size # random init wh, wh0 = (torch.tensor(x, dtype=torch.float32) for x in (wh, wh0)) k = print_results(k, verbose=False) # Plot # k, d = [None] * 20, [None] * 20 # for i in tqdm(range(1, 21)): # k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance # fig, ax = plt.subplots(1, 2, figsize=(14, 7), tight_layout=True) # ax = ax.ravel() # ax[0].plot(np.arange(1, 21), np.array(d) ** 2, marker='.') # fig, ax = plt.subplots(1, 2, figsize=(14, 7)) # plot wh # ax[0].hist(wh[wh[:, 0]<100, 0],400) # ax[1].hist(wh[wh[:, 1]<100, 1],400) # fig.savefig('wh.png', dpi=200) # Evolve f, sh, mp, s = anchor_fitness(k), k.shape, 0.9, 0.1 # fitness, generations, mutation prob, sigma pbar = tqdm(range(gen), bar_format=TQDM_BAR_FORMAT) # progress bar for _ in pbar: v = np.ones(sh) while (v == 1).all(): # mutate until a change occurs (prevent duplicates) v = ((npr.random(sh) < mp) * random.random() * npr.randn(*sh) * s + 1).clip(0.3, 3.0) kg = (k.copy() * v).clip(min=2.0) fg = anchor_fitness(kg) if fg > f: f, k = fg, kg.copy() pbar.desc = f'{PREFIX}Evolving anchors with Genetic Algorithm: fitness = {f:.4f}' if verbose: print_results(k, verbose) return print_results(k).astype(np.float32) ================================================ FILE: asone/detectors/yolov9/yolov9/utils/autobatch.py ================================================ from copy import deepcopy import numpy as np import torch from asone.detectors.yolov9.yolov9.utils.general import LOGGER, colorstr from asone.detectors.yolov9.yolov9.utils.torch_utils import profile def check_train_batch_size(model, imgsz=640, amp=True): # Check YOLOv5 training batch size with torch.cuda.amp.autocast(amp): return autobatch(deepcopy(model).train(), imgsz) # compute optimal batch size def autobatch(model, imgsz=640, fraction=0.8, batch_size=16): # Automatically estimate best YOLOv5 batch size to use `fraction` of available CUDA memory # Usage: # import torch # from utils.autobatch import autobatch # model = torch.hub.load('ultralytics/yolov5', 'yolov5s', autoshape=False) # print(autobatch(model)) # Check device prefix = colorstr('AutoBatch: ') LOGGER.info(f'{prefix}Computing optimal batch size for --imgsz {imgsz}') device = next(model.parameters()).device # get model device if device.type == 'cpu': LOGGER.info(f'{prefix}CUDA not detected, using default CPU batch-size {batch_size}') return batch_size if torch.backends.cudnn.benchmark: LOGGER.info(f'{prefix} ⚠️ Requires torch.backends.cudnn.benchmark=False, using default batch-size {batch_size}') return batch_size # Inspect CUDA memory gb = 1 << 30 # bytes to GiB (1024 ** 3) d = str(device).upper() # 'CUDA:0' properties = torch.cuda.get_device_properties(device) # device properties t = properties.total_memory / gb # GiB total r = torch.cuda.memory_reserved(device) / gb # GiB reserved a = torch.cuda.memory_allocated(device) / gb # GiB allocated f = t - (r + a) # GiB free LOGGER.info(f'{prefix}{d} ({properties.name}) {t:.2f}G total, {r:.2f}G reserved, {a:.2f}G allocated, {f:.2f}G free') # Profile batch sizes batch_sizes = [1, 2, 4, 8, 16] try: img = [torch.empty(b, 3, imgsz, imgsz) for b in batch_sizes] results = profile(img, model, n=3, device=device) except Exception as e: LOGGER.warning(f'{prefix}{e}') # Fit a solution y = [x[2] for x in results if x] # memory [2] p = np.polyfit(batch_sizes[:len(y)], y, deg=1) # first degree polynomial fit b = int((f * fraction - p[1]) / p[0]) # y intercept (optimal batch size) if None in results: # some sizes failed i = results.index(None) # first fail index if b >= batch_sizes[i]: # y intercept above failure point b = batch_sizes[max(i - 1, 0)] # select prior safe point if b < 1 or b > 1024: # b outside of safe range b = batch_size LOGGER.warning(f'{prefix}WARNING ⚠️ CUDA anomaly detected, recommend restart environment and retry command.') fraction = (np.polyval(p, b) + r + a) / t # actual fraction predicted LOGGER.info(f'{prefix}Using batch-size {b} for {d} {t * fraction:.2f}G/{t:.2f}G ({fraction * 100:.0f}%) ✅') return b ================================================ FILE: asone/detectors/yolov9/yolov9/utils/callbacks.py ================================================ import threading class Callbacks: """" Handles all registered callbacks for YOLOv5 Hooks """ def __init__(self): # Define the available callbacks self._callbacks = { 'on_pretrain_routine_start': [], 'on_pretrain_routine_end': [], 'on_train_start': [], 'on_train_epoch_start': [], 'on_train_batch_start': [], 'optimizer_step': [], 'on_before_zero_grad': [], 'on_train_batch_end': [], 'on_train_epoch_end': [], 'on_val_start': [], 'on_val_batch_start': [], 'on_val_image_end': [], 'on_val_batch_end': [], 'on_val_end': [], 'on_fit_epoch_end': [], # fit = train + val 'on_model_save': [], 'on_train_end': [], 'on_params_update': [], 'teardown': [],} self.stop_training = False # set True to interrupt training def register_action(self, hook, name='', callback=None): """ Register a new action to a callback hook Args: hook: The callback hook name to register the action to name: The name of the action for later reference callback: The callback to fire """ assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}" assert callable(callback), f"callback '{callback}' is not callable" self._callbacks[hook].append({'name': name, 'callback': callback}) def get_registered_actions(self, hook=None): """" Returns all the registered actions by callback hook Args: hook: The name of the hook to check, defaults to all """ return self._callbacks[hook] if hook else self._callbacks def run(self, hook, *args, thread=False, **kwargs): """ Loop through the registered actions and fire all callbacks on main thread Args: hook: The name of the hook to check, defaults to all args: Arguments to receive from YOLOv5 thread: (boolean) Run callbacks in daemon thread kwargs: Keyword Arguments to receive from YOLOv5 """ assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}" for logger in self._callbacks[hook]: if thread: threading.Thread(target=logger['callback'], args=args, kwargs=kwargs, daemon=True).start() else: logger['callback'](*args, **kwargs) ================================================ FILE: asone/detectors/yolov9/yolov9/utils/coco_utils.py ================================================ import cv2 from pycocotools.coco import COCO from pycocotools import mask as maskUtils # coco id: https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/ all_instances_ids = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90, ] all_stuff_ids = [ 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, # other 183, # unlabeled 0, ] # panoptic id: https://github.com/cocodataset/panopticapi/blob/master/panoptic_coco_categories.json panoptic_stuff_ids = [ 92, 93, 95, 100, 107, 109, 112, 118, 119, 122, 125, 128, 130, 133, 138, 141, 144, 145, 147, 148, 149, 151, 154, 155, 156, 159, 161, 166, 168, 171, 175, 176, 177, 178, 180, 181, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, # unlabeled 0, ] def getCocoIds(name = 'semantic'): if 'instances' == name: return all_instances_ids elif 'stuff' == name: return all_stuff_ids elif 'panoptic' == name: return all_instances_ids + panoptic_stuff_ids else: # semantic return all_instances_ids + all_stuff_ids def getMappingId(index, name = 'semantic'): ids = getCocoIds(name = name) return ids[index] def getMappingIndex(id, name = 'semantic'): ids = getCocoIds(name = name) return ids.index(id) # convert ann to rle encoded string def annToRLE(ann, img_size): h, w = img_size segm = ann['segmentation'] if list == type(segm): # polygon -- a single object might consist of multiple parts # we merge all parts into one mask rle code rles = maskUtils.frPyObjects(segm, h, w) rle = maskUtils.merge(rles) elif list == type(segm['counts']): # uncompressed RLE rle = maskUtils.frPyObjects(segm, h, w) else: # rle rle = ann['segmentation'] return rle # decode ann to mask martix def annToMask(ann, img_size): rle = annToRLE(ann, img_size) m = maskUtils.decode(rle) return m # convert mask to polygans def convert_to_polys(mask): # opencv 3.2 contours, hierarchy = cv2.findContours((mask).astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # before opencv 3.2 # contours, hierarchy = cv2.findContours((mask).astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) segmentation = [] for contour in contours: contour = contour.flatten().tolist() if 4 < len(contour): segmentation.append(contour) return segmentation ================================================ FILE: asone/detectors/yolov9/yolov9/utils/dataloaders.py ================================================ import contextlib import glob import hashlib import json import math import os import random import shutil import time from itertools import repeat from multiprocessing.pool import Pool, ThreadPool from pathlib import Path from threading import Thread from urllib.parse import urlparse import numpy as np import psutil import torch import torch.nn.functional as F import torchvision import yaml from PIL import ExifTags, Image, ImageOps from torch.utils.data import DataLoader, Dataset, dataloader, distributed from tqdm import tqdm from asone.detectors.yolov9.yolov9.utils.augmentations import (Albumentations, augment_hsv, classify_albumentations, classify_transforms, copy_paste, letterbox, mixup, random_perspective) from asone.detectors.yolov9.yolov9.utils.general import (DATASETS_DIR, LOGGER, NUM_THREADS, TQDM_BAR_FORMAT, check_dataset, check_requirements, check_yaml, clean_str, cv2, is_colab, is_kaggle, segments2boxes, unzip_file, xyn2xy, xywh2xyxy, xywhn2xyxy, xyxy2xywhn) from asone.detectors.yolov9.yolov9.utils.torch_utils import torch_distributed_zero_first # Parameters HELP_URL = 'See https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data' IMG_FORMATS = 'bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp', 'pfm' # include image suffixes VID_FORMATS = 'asf', 'avi', 'gif', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'ts', 'wmv' # include video suffixes LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html RANK = int(os.getenv('RANK', -1)) PIN_MEMORY = str(os.getenv('PIN_MEMORY', True)).lower() == 'true' # global pin_memory for dataloaders # Get orientation exif tag for orientation in ExifTags.TAGS.keys(): if ExifTags.TAGS[orientation] == 'Orientation': break def get_hash(paths): # Returns a single hash value of a list of paths (files or dirs) size = sum(os.path.getsize(p) for p in paths if os.path.exists(p)) # sizes h = hashlib.md5(str(size).encode()) # hash sizes h.update(''.join(paths).encode()) # hash paths return h.hexdigest() # return hash def exif_size(img): # Returns exif-corrected PIL size s = img.size # (width, height) with contextlib.suppress(Exception): rotation = dict(img._getexif().items())[orientation] if rotation in [6, 8]: # rotation 270 or 90 s = (s[1], s[0]) return s def exif_transpose(image): """ Transpose a PIL image accordingly if it has an EXIF Orientation tag. Inplace version of https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageOps.py exif_transpose() :param image: The image to transpose. :return: An image. """ exif = image.getexif() orientation = exif.get(0x0112, 1) # default 1 if orientation > 1: method = { 2: Image.FLIP_LEFT_RIGHT, 3: Image.ROTATE_180, 4: Image.FLIP_TOP_BOTTOM, 5: Image.TRANSPOSE, 6: Image.ROTATE_270, 7: Image.TRANSVERSE, 8: Image.ROTATE_90}.get(orientation) if method is not None: image = image.transpose(method) del exif[0x0112] image.info["exif"] = exif.tobytes() return image def seed_worker(worker_id): # Set dataloader worker seed https://pytorch.org/docs/stable/notes/randomness.html#dataloader worker_seed = torch.initial_seed() % 2 ** 32 np.random.seed(worker_seed) random.seed(worker_seed) def create_dataloader(path, imgsz, batch_size, stride, single_cls=False, hyp=None, augment=False, cache=False, pad=0.0, rect=False, rank=-1, workers=8, image_weights=False, close_mosaic=False, quad=False, min_items=0, prefix='', shuffle=False): if rect and shuffle: LOGGER.warning('WARNING ⚠️ --rect is incompatible with DataLoader shuffle, setting shuffle=False') shuffle = False with torch_distributed_zero_first(rank): # init dataset *.cache only once if DDP dataset = LoadImagesAndLabels( path, imgsz, batch_size, augment=augment, # augmentation hyp=hyp, # hyperparameters rect=rect, # rectangular batches cache_images=cache, single_cls=single_cls, stride=int(stride), pad=pad, image_weights=image_weights, min_items=min_items, prefix=prefix) batch_size = min(batch_size, len(dataset)) nd = torch.cuda.device_count() # number of CUDA devices nw = min([os.cpu_count() // max(nd, 1), batch_size if batch_size > 1 else 0, workers]) # number of workers sampler = None if rank == -1 else distributed.DistributedSampler(dataset, shuffle=shuffle) #loader = DataLoader if image_weights else InfiniteDataLoader # only DataLoader allows for attribute updates loader = DataLoader if image_weights or close_mosaic else InfiniteDataLoader generator = torch.Generator() generator.manual_seed(6148914691236517205 + RANK) return loader(dataset, batch_size=batch_size, shuffle=shuffle and sampler is None, num_workers=nw, sampler=sampler, pin_memory=PIN_MEMORY, collate_fn=LoadImagesAndLabels.collate_fn4 if quad else LoadImagesAndLabels.collate_fn, worker_init_fn=seed_worker, generator=generator), dataset class InfiniteDataLoader(dataloader.DataLoader): """ Dataloader that reuses workers Uses same syntax as vanilla DataLoader """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) object.__setattr__(self, 'batch_sampler', _RepeatSampler(self.batch_sampler)) self.iterator = super().__iter__() def __len__(self): return len(self.batch_sampler.sampler) def __iter__(self): for _ in range(len(self)): yield next(self.iterator) class _RepeatSampler: """ Sampler that repeats forever Args: sampler (Sampler) """ def __init__(self, sampler): self.sampler = sampler def __iter__(self): while True: yield from iter(self.sampler) class LoadScreenshots: # YOLOv5 screenshot dataloader, i.e. `python detect.py --source "screen 0 100 100 512 256"` def __init__(self, source, img_size=640, stride=32, auto=True, transforms=None): # source = [screen_number left top width height] (pixels) check_requirements('mss') import mss source, *params = source.split() self.screen, left, top, width, height = 0, None, None, None, None # default to full screen 0 if len(params) == 1: self.screen = int(params[0]) elif len(params) == 4: left, top, width, height = (int(x) for x in params) elif len(params) == 5: self.screen, left, top, width, height = (int(x) for x in params) self.img_size = img_size self.stride = stride self.transforms = transforms self.auto = auto self.mode = 'stream' self.frame = 0 self.sct = mss.mss() # Parse monitor shape monitor = self.sct.monitors[self.screen] self.top = monitor["top"] if top is None else (monitor["top"] + top) self.left = monitor["left"] if left is None else (monitor["left"] + left) self.width = width or monitor["width"] self.height = height or monitor["height"] self.monitor = {"left": self.left, "top": self.top, "width": self.width, "height": self.height} def __iter__(self): return self def __next__(self): # mss screen capture: get raw pixels from the screen as np array im0 = np.array(self.sct.grab(self.monitor))[:, :, :3] # [:, :, :3] BGRA to BGR s = f"screen {self.screen} (LTWH): {self.left},{self.top},{self.width},{self.height}: " if self.transforms: im = self.transforms(im0) # transforms else: im = letterbox(im0, self.img_size, stride=self.stride, auto=self.auto)[0] # padded resize im = im.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB im = np.ascontiguousarray(im) # contiguous self.frame += 1 return str(self.screen), im, im0, None, s # screen, img, original img, im0s, s class LoadImages: # YOLOv5 image/video dataloader, i.e. `python detect.py --source image.jpg/vid.mp4` def __init__(self, path, img_size=640, stride=32, auto=True, transforms=None, vid_stride=1): files = [] for p in sorted(path) if isinstance(path, (list, tuple)) else [path]: p = str(Path(p).resolve()) if '*' in p: files.extend(sorted(glob.glob(p, recursive=True))) # glob elif os.path.isdir(p): files.extend(sorted(glob.glob(os.path.join(p, '*.*')))) # dir elif os.path.isfile(p): files.append(p) # files else: raise FileNotFoundError(f'{p} does not exist') images = [x for x in files if x.split('.')[-1].lower() in IMG_FORMATS] videos = [x for x in files if x.split('.')[-1].lower() in VID_FORMATS] ni, nv = len(images), len(videos) self.img_size = img_size self.stride = stride self.files = images + videos self.nf = ni + nv # number of files self.video_flag = [False] * ni + [True] * nv self.mode = 'image' self.auto = auto self.transforms = transforms # optional self.vid_stride = vid_stride # video frame-rate stride if any(videos): self._new_video(videos[0]) # new video else: self.cap = None assert self.nf > 0, f'No images or videos found in {p}. ' \ f'Supported formats are:\nimages: {IMG_FORMATS}\nvideos: {VID_FORMATS}' def __iter__(self): self.count = 0 return self def __next__(self): if self.count == self.nf: raise StopIteration path = self.files[self.count] if self.video_flag[self.count]: # Read video self.mode = 'video' for _ in range(self.vid_stride): self.cap.grab() ret_val, im0 = self.cap.retrieve() while not ret_val: self.count += 1 self.cap.release() if self.count == self.nf: # last video raise StopIteration path = self.files[self.count] self._new_video(path) ret_val, im0 = self.cap.read() self.frame += 1 # im0 = self._cv2_rotate(im0) # for use if cv2 autorotation is False s = f'video {self.count + 1}/{self.nf} ({self.frame}/{self.frames}) {path}: ' else: # Read image self.count += 1 im0 = cv2.imread(path) # BGR assert im0 is not None, f'Image Not Found {path}' s = f'image {self.count}/{self.nf} {path}: ' if self.transforms: im = self.transforms(im0) # transforms else: im = letterbox(im0, self.img_size, stride=self.stride, auto=self.auto)[0] # padded resize im = im.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB im = np.ascontiguousarray(im) # contiguous return path, im, im0, self.cap, s def _new_video(self, path): # Create a new video capture object self.frame = 0 self.cap = cv2.VideoCapture(path) self.frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT) / self.vid_stride) self.orientation = int(self.cap.get(cv2.CAP_PROP_ORIENTATION_META)) # rotation degrees # self.cap.set(cv2.CAP_PROP_ORIENTATION_AUTO, 0) # disable https://github.com/ultralytics/yolov5/issues/8493 def _cv2_rotate(self, im): # Rotate a cv2 video manually if self.orientation == 0: return cv2.rotate(im, cv2.ROTATE_90_CLOCKWISE) elif self.orientation == 180: return cv2.rotate(im, cv2.ROTATE_90_COUNTERCLOCKWISE) elif self.orientation == 90: return cv2.rotate(im, cv2.ROTATE_180) return im def __len__(self): return self.nf # number of files class LoadStreams: # YOLOv5 streamloader, i.e. `python detect.py --source 'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP streams` def __init__(self, sources='streams.txt', img_size=640, stride=32, auto=True, transforms=None, vid_stride=1): torch.backends.cudnn.benchmark = True # faster for fixed-size inference self.mode = 'stream' self.img_size = img_size self.stride = stride self.vid_stride = vid_stride # video frame-rate stride sources = Path(sources).read_text().rsplit() if os.path.isfile(sources) else [sources] n = len(sources) self.sources = [clean_str(x) for x in sources] # clean source names for later self.imgs, self.fps, self.frames, self.threads = [None] * n, [0] * n, [0] * n, [None] * n for i, s in enumerate(sources): # index, source # Start thread to read frames from video stream st = f'{i + 1}/{n}: {s}... ' if urlparse(s).hostname in ('www.youtube.com', 'youtube.com', 'youtu.be'): # if source is YouTube video # YouTube format i.e. 'https://www.youtube.com/watch?v=Zgi9g1ksQHc' or 'https://youtu.be/Zgi9g1ksQHc' check_requirements(('pafy', 'youtube_dl==2020.12.2')) import pafy s = pafy.new(s).getbest(preftype="mp4").url # YouTube URL s = eval(s) if s.isnumeric() else s # i.e. s = '0' local webcam if s == 0: assert not is_colab(), '--source 0 webcam unsupported on Colab. Rerun command in a local environment.' assert not is_kaggle(), '--source 0 webcam unsupported on Kaggle. Rerun command in a local environment.' cap = cv2.VideoCapture(s) assert cap.isOpened(), f'{st}Failed to open {s}' w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = cap.get(cv2.CAP_PROP_FPS) # warning: may return 0 or nan self.frames[i] = max(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 0) or float('inf') # infinite stream fallback self.fps[i] = max((fps if math.isfinite(fps) else 0) % 100, 0) or 30 # 30 FPS fallback _, self.imgs[i] = cap.read() # guarantee first frame self.threads[i] = Thread(target=self.update, args=([i, cap, s]), daemon=True) LOGGER.info(f"{st} Success ({self.frames[i]} frames {w}x{h} at {self.fps[i]:.2f} FPS)") self.threads[i].start() LOGGER.info('') # newline # check for common shapes s = np.stack([letterbox(x, img_size, stride=stride, auto=auto)[0].shape for x in self.imgs]) self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal self.auto = auto and self.rect self.transforms = transforms # optional if not self.rect: LOGGER.warning('WARNING ⚠️ Stream shapes differ. For optimal performance supply similarly-shaped streams.') def update(self, i, cap, stream): # Read stream `i` frames in daemon thread n, f = 0, self.frames[i] # frame number, frame array while cap.isOpened() and n < f: n += 1 cap.grab() # .read() = .grab() followed by .retrieve() if n % self.vid_stride == 0: success, im = cap.retrieve() if success: self.imgs[i] = im else: LOGGER.warning('WARNING ⚠️ Video stream unresponsive, please check your IP camera connection.') self.imgs[i] = np.zeros_like(self.imgs[i]) cap.open(stream) # re-open stream if signal was lost time.sleep(0.0) # wait time def __iter__(self): self.count = -1 return self def __next__(self): self.count += 1 if not all(x.is_alive() for x in self.threads) or cv2.waitKey(1) == ord('q'): # q to quit cv2.destroyAllWindows() raise StopIteration im0 = self.imgs.copy() if self.transforms: im = np.stack([self.transforms(x) for x in im0]) # transforms else: im = np.stack([letterbox(x, self.img_size, stride=self.stride, auto=self.auto)[0] for x in im0]) # resize im = im[..., ::-1].transpose((0, 3, 1, 2)) # BGR to RGB, BHWC to BCHW im = np.ascontiguousarray(im) # contiguous return self.sources, im, im0, None, '' def __len__(self): return len(self.sources) # 1E12 frames = 32 streams at 30 FPS for 30 years def img2label_paths(img_paths): # Define label paths as a function of image paths sa, sb = f'{os.sep}images{os.sep}', f'{os.sep}labels{os.sep}' # /images/, /labels/ substrings return [sb.join(x.rsplit(sa, 1)).rsplit('.', 1)[0] + '.txt' for x in img_paths] class LoadImagesAndLabels(Dataset): # YOLOv5 train_loader/val_loader, loads images and labels for training and validation cache_version = 0.6 # dataset labels *.cache version rand_interp_methods = [cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4] def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, cache_images=False, single_cls=False, stride=32, pad=0.0, min_items=0, prefix=''): self.img_size = img_size self.augment = augment self.hyp = hyp self.image_weights = image_weights self.rect = False if image_weights else rect self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training) self.mosaic_border = [-img_size // 2, -img_size // 2] self.stride = stride self.path = path self.albumentations = Albumentations(size=img_size) if augment else None try: f = [] # image files for p in path if isinstance(path, list) else [path]: p = Path(p) # os-agnostic if p.is_dir(): # dir f += glob.glob(str(p / '**' / '*.*'), recursive=True) # f = list(p.rglob('*.*')) # pathlib elif p.is_file(): # file with open(p) as t: t = t.read().strip().splitlines() parent = str(p.parent) + os.sep f += [x.replace('./', parent, 1) if x.startswith('./') else x for x in t] # to global path # f += [p.parent / x.lstrip(os.sep) for x in t] # to global path (pathlib) else: raise FileNotFoundError(f'{prefix}{p} does not exist') self.im_files = sorted(x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in IMG_FORMATS) # self.img_files = sorted([x for x in f if x.suffix[1:].lower() in IMG_FORMATS]) # pathlib assert self.im_files, f'{prefix}No images found' except Exception as e: raise Exception(f'{prefix}Error loading data from {path}: {e}\n{HELP_URL}') from e # Check cache self.label_files = img2label_paths(self.im_files) # labels cache_path = (p if p.is_file() else Path(self.label_files[0]).parent).with_suffix('.cache') try: cache, exists = np.load(cache_path, allow_pickle=True).item(), True # load dict assert cache['version'] == self.cache_version # matches current version assert cache['hash'] == get_hash(self.label_files + self.im_files) # identical hash except Exception: cache, exists = self.cache_labels(cache_path, prefix), False # run cache ops # Display cache nf, nm, ne, nc, n = cache.pop('results') # found, missing, empty, corrupt, total if exists and LOCAL_RANK in {-1, 0}: d = f"Scanning {cache_path}... {nf} images, {nm + ne} backgrounds, {nc} corrupt" tqdm(None, desc=prefix + d, total=n, initial=n, bar_format=TQDM_BAR_FORMAT) # display cache results if cache['msgs']: LOGGER.info('\n'.join(cache['msgs'])) # display warnings assert nf > 0 or not augment, f'{prefix}No labels found in {cache_path}, can not start training. {HELP_URL}' # Read cache [cache.pop(k) for k in ('hash', 'version', 'msgs')] # remove items labels, shapes, self.segments = zip(*cache.values()) nl = len(np.concatenate(labels, 0)) # number of labels assert nl > 0 or not augment, f'{prefix}All labels empty in {cache_path}, can not start training. {HELP_URL}' self.labels = list(labels) self.shapes = np.array(shapes) self.im_files = list(cache.keys()) # update self.label_files = img2label_paths(cache.keys()) # update # Filter images if min_items: include = np.array([len(x) >= min_items for x in self.labels]).nonzero()[0].astype(int) LOGGER.info(f'{prefix}{n - len(include)}/{n} images filtered from dataset') self.im_files = [self.im_files[i] for i in include] self.label_files = [self.label_files[i] for i in include] self.labels = [self.labels[i] for i in include] self.segments = [self.segments[i] for i in include] self.shapes = self.shapes[include] # wh # Create indices n = len(self.shapes) # number of images bi = np.floor(np.arange(n) / batch_size).astype(int) # batch index nb = bi[-1] + 1 # number of batches self.batch = bi # batch index of image self.n = n self.indices = range(n) # Update labels include_class = [] # filter labels to include only these classes (optional) include_class_array = np.array(include_class).reshape(1, -1) for i, (label, segment) in enumerate(zip(self.labels, self.segments)): if include_class: j = (label[:, 0:1] == include_class_array).any(1) self.labels[i] = label[j] if segment: self.segments[i] = segment[j] if single_cls: # single-class training, merge all classes into 0 self.labels[i][:, 0] = 0 # Rectangular Training if self.rect: # Sort by aspect ratio s = self.shapes # wh ar = s[:, 1] / s[:, 0] # aspect ratio irect = ar.argsort() self.im_files = [self.im_files[i] for i in irect] self.label_files = [self.label_files[i] for i in irect] self.labels = [self.labels[i] for i in irect] self.segments = [self.segments[i] for i in irect] self.shapes = s[irect] # wh ar = ar[irect] # Set training image shapes shapes = [[1, 1]] * nb for i in range(nb): ari = ar[bi == i] mini, maxi = ari.min(), ari.max() if maxi < 1: shapes[i] = [maxi, 1] elif mini > 1: shapes[i] = [1, 1 / mini] self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(int) * stride # Cache images into RAM/disk for faster training if cache_images == 'ram' and not self.check_cache_ram(prefix=prefix): cache_images = False self.ims = [None] * n self.npy_files = [Path(f).with_suffix('.npy') for f in self.im_files] if cache_images: b, gb = 0, 1 << 30 # bytes of cached images, bytes per gigabytes self.im_hw0, self.im_hw = [None] * n, [None] * n fcn = self.cache_images_to_disk if cache_images == 'disk' else self.load_image results = ThreadPool(NUM_THREADS).imap(fcn, range(n)) pbar = tqdm(enumerate(results), total=n, bar_format=TQDM_BAR_FORMAT, disable=LOCAL_RANK > 0) for i, x in pbar: if cache_images == 'disk': b += self.npy_files[i].stat().st_size else: # 'ram' self.ims[i], self.im_hw0[i], self.im_hw[i] = x # im, hw_orig, hw_resized = load_image(self, i) b += self.ims[i].nbytes pbar.desc = f'{prefix}Caching images ({b / gb:.1f}GB {cache_images})' pbar.close() def check_cache_ram(self, safety_margin=0.1, prefix=''): # Check image caching requirements vs available memory b, gb = 0, 1 << 30 # bytes of cached images, bytes per gigabytes n = min(self.n, 30) # extrapolate from 30 random images for _ in range(n): im = cv2.imread(random.choice(self.im_files)) # sample image ratio = self.img_size / max(im.shape[0], im.shape[1]) # max(h, w) # ratio b += im.nbytes * ratio ** 2 mem_required = b * self.n / n # GB required to cache dataset into RAM mem = psutil.virtual_memory() cache = mem_required * (1 + safety_margin) < mem.available # to cache or not to cache, that is the question if not cache: LOGGER.info(f"{prefix}{mem_required / gb:.1f}GB RAM required, " f"{mem.available / gb:.1f}/{mem.total / gb:.1f}GB available, " f"{'caching images ✅' if cache else 'not caching images ⚠️'}") return cache def cache_labels(self, path=Path('./labels.cache'), prefix=''): # Cache dataset labels, check images and read shapes x = {} # dict nm, nf, ne, nc, msgs = 0, 0, 0, 0, [] # number missing, found, empty, corrupt, messages desc = f"{prefix}Scanning {path.parent / path.stem}..." with Pool(NUM_THREADS) as pool: pbar = tqdm(pool.imap(verify_image_label, zip(self.im_files, self.label_files, repeat(prefix))), desc=desc, total=len(self.im_files), bar_format=TQDM_BAR_FORMAT) for im_file, lb, shape, segments, nm_f, nf_f, ne_f, nc_f, msg in pbar: nm += nm_f nf += nf_f ne += ne_f nc += nc_f if im_file: x[im_file] = [lb, shape, segments] if msg: msgs.append(msg) pbar.desc = f"{desc} {nf} images, {nm + ne} backgrounds, {nc} corrupt" pbar.close() if msgs: LOGGER.info('\n'.join(msgs)) if nf == 0: LOGGER.warning(f'{prefix}WARNING ⚠️ No labels found in {path}. {HELP_URL}') x['hash'] = get_hash(self.label_files + self.im_files) x['results'] = nf, nm, ne, nc, len(self.im_files) x['msgs'] = msgs # warnings x['version'] = self.cache_version # cache version try: np.save(path, x) # save cache for next time path.with_suffix('.cache.npy').rename(path) # remove .npy suffix LOGGER.info(f'{prefix}New cache created: {path}') except Exception as e: LOGGER.warning(f'{prefix}WARNING ⚠️ Cache directory {path.parent} is not writeable: {e}') # not writeable return x def __len__(self): return len(self.im_files) # def __iter__(self): # self.count = -1 # print('ran dataset iter') # #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF) # return self def __getitem__(self, index): index = self.indices[index] # linear, shuffled, or image_weights hyp = self.hyp mosaic = self.mosaic and random.random() < hyp['mosaic'] if mosaic: # Load mosaic img, labels = self.load_mosaic(index) shapes = None # MixUp augmentation if random.random() < hyp['mixup']: img, labels = mixup(img, labels, *self.load_mosaic(random.randint(0, self.n - 1))) else: # Load image img, (h0, w0), (h, w) = self.load_image(index) # Letterbox shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment) shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling labels = self.labels[index].copy() if labels.size: # normalized xywh to pixel xyxy format labels[:, 1:] = xywhn2xyxy(labels[:, 1:], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1]) if self.augment: img, labels = random_perspective(img, labels, degrees=hyp['degrees'], translate=hyp['translate'], scale=hyp['scale'], shear=hyp['shear'], perspective=hyp['perspective']) nl = len(labels) # number of labels if nl: labels[:, 1:5] = xyxy2xywhn(labels[:, 1:5], w=img.shape[1], h=img.shape[0], clip=True, eps=1E-3) if self.augment: # Albumentations img, labels = self.albumentations(img, labels) nl = len(labels) # update after albumentations # HSV color-space augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v']) # Flip up-down if random.random() < hyp['flipud']: img = np.flipud(img) if nl: labels[:, 2] = 1 - labels[:, 2] # Flip left-right if random.random() < hyp['fliplr']: img = np.fliplr(img) if nl: labels[:, 1] = 1 - labels[:, 1] # Cutouts # labels = cutout(img, labels, p=0.5) # nl = len(labels) # update after cutout labels_out = torch.zeros((nl, 6)) if nl: labels_out[:, 1:] = torch.from_numpy(labels) # Convert img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB img = np.ascontiguousarray(img) return torch.from_numpy(img), labels_out, self.im_files[index], shapes def load_image(self, i): # Loads 1 image from dataset index 'i', returns (im, original hw, resized hw) im, f, fn = self.ims[i], self.im_files[i], self.npy_files[i], if im is None: # not cached in RAM if fn.exists(): # load npy im = np.load(fn) else: # read image im = cv2.imread(f) # BGR assert im is not None, f'Image Not Found {f}' h0, w0 = im.shape[:2] # orig hw r = self.img_size / max(h0, w0) # ratio if r != 1: # if sizes are not equal interp = cv2.INTER_LINEAR if (self.augment or r > 1) else cv2.INTER_AREA im = cv2.resize(im, (int(w0 * r), int(h0 * r)), interpolation=interp) return im, (h0, w0), im.shape[:2] # im, hw_original, hw_resized return self.ims[i], self.im_hw0[i], self.im_hw[i] # im, hw_original, hw_resized def cache_images_to_disk(self, i): # Saves an image as an *.npy file for faster loading f = self.npy_files[i] if not f.exists(): np.save(f.as_posix(), cv2.imread(self.im_files[i])) def load_mosaic(self, index): # YOLOv5 4-mosaic loader. Loads 1 image + 3 random images into a 4-image mosaic labels4, segments4 = [], [] s = self.img_size yc, xc = (int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border) # mosaic center x, y indices = [index] + random.choices(self.indices, k=3) # 3 additional image indices random.shuffle(indices) for i, index in enumerate(indices): # Load image img, _, (h, w) = self.load_image(index) # place img in img4 if i == 0: # top left img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image) x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image) elif i == 1: # top right x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h elif i == 2: # bottom left x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h) x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h) elif i == 3: # bottom right x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h) x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h) img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax] padw = x1a - x1b padh = y1a - y1b # Labels labels, segments = self.labels[index].copy(), self.segments[index].copy() if labels.size: labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format segments = [xyn2xy(x, w, h, padw, padh) for x in segments] labels4.append(labels) segments4.extend(segments) # Concat/clip labels labels4 = np.concatenate(labels4, 0) for x in (labels4[:, 1:], *segments4): np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective() # img4, labels4 = replicate(img4, labels4) # replicate # Augment img4, labels4, segments4 = copy_paste(img4, labels4, segments4, p=self.hyp['copy_paste']) img4, labels4 = random_perspective(img4, labels4, segments4, degrees=self.hyp['degrees'], translate=self.hyp['translate'], scale=self.hyp['scale'], shear=self.hyp['shear'], perspective=self.hyp['perspective'], border=self.mosaic_border) # border to remove return img4, labels4 def load_mosaic9(self, index): # YOLOv5 9-mosaic loader. Loads 1 image + 8 random images into a 9-image mosaic labels9, segments9 = [], [] s = self.img_size indices = [index] + random.choices(self.indices, k=8) # 8 additional image indices random.shuffle(indices) hp, wp = -1, -1 # height, width previous for i, index in enumerate(indices): # Load image img, _, (h, w) = self.load_image(index) # place img in img9 if i == 0: # center img9 = np.full((s * 3, s * 3, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles h0, w0 = h, w c = s, s, s + w, s + h # xmin, ymin, xmax, ymax (base) coordinates elif i == 1: # top c = s, s - h, s + w, s elif i == 2: # top right c = s + wp, s - h, s + wp + w, s elif i == 3: # right c = s + w0, s, s + w0 + w, s + h elif i == 4: # bottom right c = s + w0, s + hp, s + w0 + w, s + hp + h elif i == 5: # bottom c = s + w0 - w, s + h0, s + w0, s + h0 + h elif i == 6: # bottom left c = s + w0 - wp - w, s + h0, s + w0 - wp, s + h0 + h elif i == 7: # left c = s - w, s + h0 - h, s, s + h0 elif i == 8: # top left c = s - w, s + h0 - hp - h, s, s + h0 - hp padx, pady = c[:2] x1, y1, x2, y2 = (max(x, 0) for x in c) # allocate coords # Labels labels, segments = self.labels[index].copy(), self.segments[index].copy() if labels.size: labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padx, pady) # normalized xywh to pixel xyxy format segments = [xyn2xy(x, w, h, padx, pady) for x in segments] labels9.append(labels) segments9.extend(segments) # Image img9[y1:y2, x1:x2] = img[y1 - pady:, x1 - padx:] # img9[ymin:ymax, xmin:xmax] hp, wp = h, w # height, width previous # Offset yc, xc = (int(random.uniform(0, s)) for _ in self.mosaic_border) # mosaic center x, y img9 = img9[yc:yc + 2 * s, xc:xc + 2 * s] # Concat/clip labels labels9 = np.concatenate(labels9, 0) labels9[:, [1, 3]] -= xc labels9[:, [2, 4]] -= yc c = np.array([xc, yc]) # centers segments9 = [x - c for x in segments9] for x in (labels9[:, 1:], *segments9): np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective() # img9, labels9 = replicate(img9, labels9) # replicate # Augment img9, labels9, segments9 = copy_paste(img9, labels9, segments9, p=self.hyp['copy_paste']) img9, labels9 = random_perspective(img9, labels9, segments9, degrees=self.hyp['degrees'], translate=self.hyp['translate'], scale=self.hyp['scale'], shear=self.hyp['shear'], perspective=self.hyp['perspective'], border=self.mosaic_border) # border to remove return img9, labels9 @staticmethod def collate_fn(batch): im, label, path, shapes = zip(*batch) # transposed for i, lb in enumerate(label): lb[:, 0] = i # add target image index for build_targets() return torch.stack(im, 0), torch.cat(label, 0), path, shapes @staticmethod def collate_fn4(batch): im, label, path, shapes = zip(*batch) # transposed n = len(shapes) // 4 im4, label4, path4, shapes4 = [], [], path[:n], shapes[:n] ho = torch.tensor([[0.0, 0, 0, 1, 0, 0]]) wo = torch.tensor([[0.0, 0, 1, 0, 0, 0]]) s = torch.tensor([[1, 1, 0.5, 0.5, 0.5, 0.5]]) # scale for i in range(n): # zidane torch.zeros(16,3,720,1280) # BCHW i *= 4 if random.random() < 0.5: im1 = F.interpolate(im[i].unsqueeze(0).float(), scale_factor=2.0, mode='bilinear', align_corners=False)[0].type(im[i].type()) lb = label[i] else: im1 = torch.cat((torch.cat((im[i], im[i + 1]), 1), torch.cat((im[i + 2], im[i + 3]), 1)), 2) lb = torch.cat((label[i], label[i + 1] + ho, label[i + 2] + wo, label[i + 3] + ho + wo), 0) * s im4.append(im1) label4.append(lb) for i, lb in enumerate(label4): lb[:, 0] = i # add target image index for build_targets() return torch.stack(im4, 0), torch.cat(label4, 0), path4, shapes4 # Ancillary functions -------------------------------------------------------------------------------------------------- def flatten_recursive(path=DATASETS_DIR / 'coco128'): # Flatten a recursive directory by bringing all files to top level new_path = Path(f'{str(path)}_flat') if os.path.exists(new_path): shutil.rmtree(new_path) # delete output folder os.makedirs(new_path) # make new output folder for file in tqdm(glob.glob(f'{str(Path(path))}/**/*.*', recursive=True)): shutil.copyfile(file, new_path / Path(file).name) def extract_boxes(path=DATASETS_DIR / 'coco128'): # from utils.dataloaders import *; extract_boxes() # Convert detection dataset into classification dataset, with one directory per class path = Path(path) # images dir shutil.rmtree(path / 'classification') if (path / 'classification').is_dir() else None # remove existing files = list(path.rglob('*.*')) n = len(files) # number of files for im_file in tqdm(files, total=n): if im_file.suffix[1:] in IMG_FORMATS: # image im = cv2.imread(str(im_file))[..., ::-1] # BGR to RGB h, w = im.shape[:2] # labels lb_file = Path(img2label_paths([str(im_file)])[0]) if Path(lb_file).exists(): with open(lb_file) as f: lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32) # labels for j, x in enumerate(lb): c = int(x[0]) # class f = (path / 'classifier') / f'{c}' / f'{path.stem}_{im_file.stem}_{j}.jpg' # new filename if not f.parent.is_dir(): f.parent.mkdir(parents=True) b = x[1:] * [w, h, w, h] # box # b[2:] = b[2:].max() # rectangle to square b[2:] = b[2:] * 1.2 + 3 # pad b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(int) b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image b[[1, 3]] = np.clip(b[[1, 3]], 0, h) assert cv2.imwrite(str(f), im[b[1]:b[3], b[0]:b[2]]), f'box failure in {f}' def autosplit(path=DATASETS_DIR / 'coco128/images', weights=(0.9, 0.1, 0.0), annotated_only=False): """ Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files Usage: from utils.dataloaders import *; autosplit() Arguments path: Path to images directory weights: Train, val, test weights (list, tuple) annotated_only: Only use images with an annotated txt file """ path = Path(path) # images dir files = sorted(x for x in path.rglob('*.*') if x.suffix[1:].lower() in IMG_FORMATS) # image files only n = len(files) # number of files random.seed(0) # for reproducibility indices = random.choices([0, 1, 2], weights=weights, k=n) # assign each image to a split txt = ['autosplit_train.txt', 'autosplit_val.txt', 'autosplit_test.txt'] # 3 txt files for x in txt: if (path.parent / x).exists(): (path.parent / x).unlink() # remove existing print(f'Autosplitting images from {path}' + ', using *.txt labeled images only' * annotated_only) for i, img in tqdm(zip(indices, files), total=n): if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label with open(path.parent / txt[i], 'a') as f: f.write(f'./{img.relative_to(path.parent).as_posix()}' + '\n') # add image to txt file def verify_image_label(args): # Verify one image-label pair im_file, lb_file, prefix = args nm, nf, ne, nc, msg, segments = 0, 0, 0, 0, '', [] # number (missing, found, empty, corrupt), message, segments try: # verify images im = Image.open(im_file) im.verify() # PIL verify shape = exif_size(im) # image size assert (shape[0] > 9) & (shape[1] > 9), f'image size {shape} <10 pixels' assert im.format.lower() in IMG_FORMATS, f'invalid image format {im.format}' if im.format.lower() in ('jpg', 'jpeg'): with open(im_file, 'rb') as f: f.seek(-2, 2) if f.read() != b'\xff\xd9': # corrupt JPEG ImageOps.exif_transpose(Image.open(im_file)).save(im_file, 'JPEG', subsampling=0, quality=100) msg = f'{prefix}WARNING ⚠️ {im_file}: corrupt JPEG restored and saved' # verify labels if os.path.isfile(lb_file): nf = 1 # label found with open(lb_file) as f: lb = [x.split() for x in f.read().strip().splitlines() if len(x)] if any(len(x) > 6 for x in lb): # is segment classes = np.array([x[0] for x in lb], dtype=np.float32) segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in lb] # (cls, xy1...) lb = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1) # (cls, xywh) lb = np.array(lb, dtype=np.float32) nl = len(lb) if nl: assert lb.shape[1] == 5, f'labels require 5 columns, {lb.shape[1]} columns detected' assert (lb >= 0).all(), f'negative label values {lb[lb < 0]}' assert (lb[:, 1:] <= 1).all(), f'non-normalized or out of bounds coordinates {lb[:, 1:][lb[:, 1:] > 1]}' _, i = np.unique(lb, axis=0, return_index=True) if len(i) < nl: # duplicate row check lb = lb[i] # remove duplicates if segments: segments = [segments[x] for x in i] msg = f'{prefix}WARNING ⚠️ {im_file}: {nl - len(i)} duplicate labels removed' else: ne = 1 # label empty lb = np.zeros((0, 5), dtype=np.float32) else: nm = 1 # label missing lb = np.zeros((0, 5), dtype=np.float32) return im_file, lb, shape, segments, nm, nf, ne, nc, msg except Exception as e: nc = 1 msg = f'{prefix}WARNING ⚠️ {im_file}: ignoring corrupt image/label: {e}' return [None, None, None, None, nm, nf, ne, nc, msg] class HUBDatasetStats(): """ Class for generating HUB dataset JSON and `-hub` dataset directory Arguments path: Path to data.yaml or data.zip (with data.yaml inside data.zip) autodownload: Attempt to download dataset if not found locally Usage from utils.dataloaders import HUBDatasetStats stats = HUBDatasetStats('coco128.yaml', autodownload=True) # usage 1 stats = HUBDatasetStats('path/to/coco128.zip') # usage 2 stats.get_json(save=False) stats.process_images() """ def __init__(self, path='coco128.yaml', autodownload=False): # Initialize class zipped, data_dir, yaml_path = self._unzip(Path(path)) try: with open(check_yaml(yaml_path), errors='ignore') as f: data = yaml.safe_load(f) # data dict if zipped: data['path'] = data_dir except Exception as e: raise Exception("error/HUB/dataset_stats/yaml_load") from e check_dataset(data, autodownload) # download dataset if missing self.hub_dir = Path(data['path'] + '-hub') self.im_dir = self.hub_dir / 'images' self.im_dir.mkdir(parents=True, exist_ok=True) # makes /images self.stats = {'nc': data['nc'], 'names': list(data['names'].values())} # statistics dictionary self.data = data @staticmethod def _find_yaml(dir): # Return data.yaml file files = list(dir.glob('*.yaml')) or list(dir.rglob('*.yaml')) # try root level first and then recursive assert files, f'No *.yaml file found in {dir}' if len(files) > 1: files = [f for f in files if f.stem == dir.stem] # prefer *.yaml files that match dir name assert files, f'Multiple *.yaml files found in {dir}, only 1 *.yaml file allowed' assert len(files) == 1, f'Multiple *.yaml files found: {files}, only 1 *.yaml file allowed in {dir}' return files[0] def _unzip(self, path): # Unzip data.zip if not str(path).endswith('.zip'): # path is data.yaml return False, None, path assert Path(path).is_file(), f'Error unzipping {path}, file not found' unzip_file(path, path=path.parent) dir = path.with_suffix('') # dataset directory == zip name assert dir.is_dir(), f'Error unzipping {path}, {dir} not found. path/to/abc.zip MUST unzip to path/to/abc/' return True, str(dir), self._find_yaml(dir) # zipped, data_dir, yaml_path def _hub_ops(self, f, max_dim=1920): # HUB ops for 1 image 'f': resize and save at reduced quality in /dataset-hub for web/app viewing f_new = self.im_dir / Path(f).name # dataset-hub image filename try: # use PIL im = Image.open(f) r = max_dim / max(im.height, im.width) # ratio if r < 1.0: # image too large im = im.resize((int(im.width * r), int(im.height * r))) im.save(f_new, 'JPEG', quality=50, optimize=True) # save except Exception as e: # use OpenCV LOGGER.info(f'WARNING ⚠️ HUB ops PIL failure {f}: {e}') im = cv2.imread(f) im_height, im_width = im.shape[:2] r = max_dim / max(im_height, im_width) # ratio if r < 1.0: # image too large im = cv2.resize(im, (int(im_width * r), int(im_height * r)), interpolation=cv2.INTER_AREA) cv2.imwrite(str(f_new), im) def get_json(self, save=False, verbose=False): # Return dataset JSON for Ultralytics HUB def _round(labels): # Update labels to integer class and 6 decimal place floats return [[int(c), *(round(x, 4) for x in points)] for c, *points in labels] for split in 'train', 'val', 'test': if self.data.get(split) is None: self.stats[split] = None # i.e. no test set continue dataset = LoadImagesAndLabels(self.data[split]) # load dataset x = np.array([ np.bincount(label[:, 0].astype(int), minlength=self.data['nc']) for label in tqdm(dataset.labels, total=dataset.n, desc='Statistics')]) # shape(128x80) self.stats[split] = { 'instance_stats': { 'total': int(x.sum()), 'per_class': x.sum(0).tolist()}, 'image_stats': { 'total': dataset.n, 'unlabelled': int(np.all(x == 0, 1).sum()), 'per_class': (x > 0).sum(0).tolist()}, 'labels': [{ str(Path(k).name): _round(v.tolist())} for k, v in zip(dataset.im_files, dataset.labels)]} # Save, print and return if save: stats_path = self.hub_dir / 'stats.json' print(f'Saving {stats_path.resolve()}...') with open(stats_path, 'w') as f: json.dump(self.stats, f) # save stats.json if verbose: print(json.dumps(self.stats, indent=2, sort_keys=False)) return self.stats def process_images(self): # Compress images for Ultralytics HUB for split in 'train', 'val', 'test': if self.data.get(split) is None: continue dataset = LoadImagesAndLabels(self.data[split]) # load dataset desc = f'{split} images' for _ in tqdm(ThreadPool(NUM_THREADS).imap(self._hub_ops, dataset.im_files), total=dataset.n, desc=desc): pass print(f'Done. All images saved to {self.im_dir}') return self.im_dir # Classification dataloaders ------------------------------------------------------------------------------------------- class ClassificationDataset(torchvision.datasets.ImageFolder): """ YOLOv5 Classification Dataset. Arguments root: Dataset path transform: torchvision transforms, used by default album_transform: Albumentations transforms, used if installed """ def __init__(self, root, augment, imgsz, cache=False): super().__init__(root=root) self.torch_transforms = classify_transforms(imgsz) self.album_transforms = classify_albumentations(augment, imgsz) if augment else None self.cache_ram = cache is True or cache == 'ram' self.cache_disk = cache == 'disk' self.samples = [list(x) + [Path(x[0]).with_suffix('.npy'), None] for x in self.samples] # file, index, npy, im def __getitem__(self, i): f, j, fn, im = self.samples[i] # filename, index, filename.with_suffix('.npy'), image if self.cache_ram and im is None: im = self.samples[i][3] = cv2.imread(f) elif self.cache_disk: if not fn.exists(): # load npy np.save(fn.as_posix(), cv2.imread(f)) im = np.load(fn) else: # read image im = cv2.imread(f) # BGR if self.album_transforms: sample = self.album_transforms(image=cv2.cvtColor(im, cv2.COLOR_BGR2RGB))["image"] else: sample = self.torch_transforms(im) return sample, j def create_classification_dataloader(path, imgsz=224, batch_size=16, augment=True, cache=False, rank=-1, workers=8, shuffle=True): # Returns Dataloader object to be used with YOLOv5 Classifier with torch_distributed_zero_first(rank): # init dataset *.cache only once if DDP dataset = ClassificationDataset(root=path, imgsz=imgsz, augment=augment, cache=cache) batch_size = min(batch_size, len(dataset)) nd = torch.cuda.device_count() nw = min([os.cpu_count() // max(nd, 1), batch_size if batch_size > 1 else 0, workers]) sampler = None if rank == -1 else distributed.DistributedSampler(dataset, shuffle=shuffle) generator = torch.Generator() generator.manual_seed(6148914691236517205 + RANK) return InfiniteDataLoader(dataset, batch_size=batch_size, shuffle=shuffle and sampler is None, num_workers=nw, sampler=sampler, pin_memory=PIN_MEMORY, worker_init_fn=seed_worker, generator=generator) # or DataLoader(persistent_workers=True) ================================================ FILE: asone/detectors/yolov9/yolov9/utils/downloads.py ================================================ import logging import os import subprocess import urllib from pathlib import Path import requests import torch def is_url(url, check=True): # Check if string is URL and check if URL exists try: url = str(url) result = urllib.parse.urlparse(url) assert all([result.scheme, result.netloc]) # check if is url return (urllib.request.urlopen(url).getcode() == 200) if check else True # check if exists online except (AssertionError, urllib.request.HTTPError): return False def gsutil_getsize(url=''): # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du s = subprocess.check_output(f'gsutil du {url}', shell=True).decode('utf-8') return eval(s.split(' ')[0]) if len(s) else 0 # bytes def url_getsize(url='https://ultralytics.com/images/bus.jpg'): # Return downloadable file size in bytes response = requests.head(url, allow_redirects=True) return int(response.headers.get('content-length', -1)) def safe_download(file, url, url2=None, min_bytes=1E0, error_msg=''): # Attempts to download file from url or url2, checks and removes incomplete downloads < min_bytes from asone.detectors.yolov9.yolov9.utils.general import LOGGER file = Path(file) assert_msg = f"Downloaded file '{file}' does not exist or size is < min_bytes={min_bytes}" try: # url1 LOGGER.info(f'Downloading {url} to {file}...') torch.hub.download_url_to_file(url, str(file), progress=LOGGER.level <= logging.INFO) assert file.exists() and file.stat().st_size > min_bytes, assert_msg # check except Exception as e: # url2 if file.exists(): file.unlink() # remove partial downloads LOGGER.info(f'ERROR: {e}\nRe-attempting {url2 or url} to {file}...') os.system(f"curl -# -L '{url2 or url}' -o '{file}' --retry 3 -C -") # curl download, retry and resume on fail finally: if not file.exists() or file.stat().st_size < min_bytes: # check if file.exists(): file.unlink() # remove partial downloads LOGGER.info(f"ERROR: {assert_msg}\n{error_msg}") LOGGER.info('') def attempt_download(file, repo='ultralytics/yolov5', release='v7.0'): # Attempt file download from GitHub release assets if not found locally. release = 'latest', 'v7.0', etc. from asone.detectors.yolov9.yolov9.utils.general import LOGGER def github_assets(repository, version='latest'): # Return GitHub repo tag (i.e. 'v7.0') and assets (i.e. ['yolov5s.pt', 'yolov5m.pt', ...]) if version != 'latest': version = f'tags/{version}' # i.e. tags/v7.0 response = requests.get(f'https://api.github.com/repos/{repository}/releases/{version}').json() # github api return response['tag_name'], [x['name'] for x in response['assets']] # tag, assets file = Path(str(file).strip().replace("'", '')) if not file.exists(): # URL specified name = Path(urllib.parse.unquote(str(file))).name # decode '%2F' to '/' etc. if str(file).startswith(('http:/', 'https:/')): # download url = str(file).replace(':/', '://') # Pathlib turns :// -> :/ file = name.split('?')[0] # parse authentication https://url.com/file.txt?auth... if Path(file).is_file(): LOGGER.info(f'Found {url} locally at {file}') # file already exists else: safe_download(file=file, url=url, min_bytes=1E5) return file # GitHub assets assets = [f'yolov5{size}{suffix}.pt' for size in 'nsmlx' for suffix in ('', '6', '-cls', '-seg')] # default try: tag, assets = github_assets(repo, release) except Exception: try: tag, assets = github_assets(repo) # latest release except Exception: try: tag = subprocess.check_output('git tag', shell=True, stderr=subprocess.STDOUT).decode().split()[-1] except Exception: tag = release file.parent.mkdir(parents=True, exist_ok=True) # make parent dir (if required) if name in assets: url3 = 'https://drive.google.com/drive/folders/1EFQTEUeXWSFww0luse2jB9M1QNZQGwNl' # backup gdrive mirror safe_download( file, url=f'https://github.com/{repo}/releases/download/{tag}/{name}', min_bytes=1E5, error_msg=f'{file} missing, try downloading from https://github.com/{repo}/releases/{tag} or {url3}') return str(file) ================================================ FILE: asone/detectors/yolov9/yolov9/utils/general.py ================================================ import contextlib import glob import inspect import logging import logging.config import math import os import platform import random import re import signal import sys import time import urllib from copy import deepcopy from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from subprocess import check_output from tarfile import is_tarfile from typing import Optional from zipfile import ZipFile, is_zipfile import cv2 import IPython import numpy as np import pandas as pd import pkg_resources as pkg import torch import torchvision import yaml from asone.detectors.yolov9.yolov9.utils import TryExcept, emojis from asone.detectors.yolov9.yolov9.utils.downloads import gsutil_getsize from asone.detectors.yolov9.yolov9.utils.metrics import box_iou, fitness FILE = Path(__file__).resolve() ROOT = FILE.parents[1] # YOLO root directory RANK = int(os.getenv('RANK', -1)) # Settings NUM_THREADS = min(8, max(1, os.cpu_count() - 1)) # number of YOLOv5 multiprocessing threads DATASETS_DIR = Path(os.getenv('YOLOv5_DATASETS_DIR', ROOT.parent / 'datasets')) # global datasets directory AUTOINSTALL = str(os.getenv('YOLOv5_AUTOINSTALL', True)).lower() == 'true' # global auto-install mode VERBOSE = str(os.getenv('YOLOv5_VERBOSE', True)).lower() == 'true' # global verbose mode TQDM_BAR_FORMAT = '{l_bar}{bar:10}| {n_fmt}/{total_fmt} {elapsed}' # tqdm bar format FONT = 'Arial.ttf' # https://ultralytics.com/assets/Arial.ttf torch.set_printoptions(linewidth=320, precision=5, profile='long') np.set_printoptions(linewidth=320, formatter={'float_kind': '{:11.5g}'.format}) # format short g, %precision=5 pd.options.display.max_columns = 10 cv2.setNumThreads(0) # prevent OpenCV from multithreading (incompatible with PyTorch DataLoader) os.environ['NUMEXPR_MAX_THREADS'] = str(NUM_THREADS) # NumExpr max threads os.environ['OMP_NUM_THREADS'] = '1' if platform.system() == 'darwin' else str(NUM_THREADS) # OpenMP (PyTorch and SciPy) def is_ascii(s=''): # Is string composed of all ASCII (no UTF) characters? (note str().isascii() introduced in python 3.7) s = str(s) # convert list, tuple, None, etc. to str return len(s.encode().decode('ascii', 'ignore')) == len(s) def is_chinese(s='人工智能'): # Is string composed of any Chinese characters? return bool(re.search('[\u4e00-\u9fff]', str(s))) def is_colab(): # Is environment a Google Colab instance? return 'google.colab' in sys.modules def is_notebook(): # Is environment a Jupyter notebook? Verified on Colab, Jupyterlab, Kaggle, Paperspace ipython_type = str(type(IPython.get_ipython())) return 'colab' in ipython_type or 'zmqshell' in ipython_type def is_kaggle(): # Is environment a Kaggle Notebook? return os.environ.get('PWD') == '/kaggle/working' and os.environ.get('KAGGLE_URL_BASE') == 'https://www.kaggle.com' def is_docker() -> bool: """Check if the process runs inside a docker container.""" if Path("/.dockerenv").exists(): return True try: # check if docker is in control groups with open("/proc/self/cgroup") as file: return any("docker" in line for line in file) except OSError: return False def is_writeable(dir, test=False): # Return True if directory has write permissions, test opening a file with write permissions if test=True if not test: return os.access(dir, os.W_OK) # possible issues on Windows file = Path(dir) / 'tmp.txt' try: with open(file, 'w'): # open file with write permissions pass file.unlink() # remove file return True except OSError: return False LOGGING_NAME = "yolov5" def set_logging(name=LOGGING_NAME, verbose=True): # sets up logging for the given name rank = int(os.getenv('RANK', -1)) # rank in world for Multi-GPU trainings level = logging.INFO if verbose and rank in {-1, 0} else logging.ERROR logging.config.dictConfig({ "version": 1, "disable_existing_loggers": False, "formatters": { name: { "format": "%(message)s"}}, "handlers": { name: { "class": "logging.StreamHandler", "formatter": name, "level": level,}}, "loggers": { name: { "level": level, "handlers": [name], "propagate": False,}}}) set_logging(LOGGING_NAME) # run before defining LOGGER LOGGER = logging.getLogger(LOGGING_NAME) # define globally (used in train.py, val.py, detect.py, etc.) if platform.system() == 'Windows': for fn in LOGGER.info, LOGGER.warning: setattr(LOGGER, fn.__name__, lambda x: fn(emojis(x))) # emoji safe logging def user_config_dir(dir='Ultralytics', env_var='YOLOV5_CONFIG_DIR'): # Return path of user configuration directory. Prefer environment variable if exists. Make dir if required. env = os.getenv(env_var) if env: path = Path(env) # use environment variable else: cfg = {'Windows': 'AppData/Roaming', 'Linux': '.config', 'Darwin': 'Library/Application Support'} # 3 OS dirs path = Path.home() / cfg.get(platform.system(), '') # OS-specific config dir path = (path if is_writeable(path) else Path('/tmp')) / dir # GCP and AWS lambda fix, only /tmp is writeable path.mkdir(exist_ok=True) # make if required return path CONFIG_DIR = user_config_dir() # Ultralytics settings dir class Profile(contextlib.ContextDecorator): # YOLO Profile class. Usage: @Profile() decorator or 'with Profile():' context manager def __init__(self, t=0.0): self.t = t self.cuda = torch.cuda.is_available() def __enter__(self): self.start = self.time() return self def __exit__(self, type, value, traceback): self.dt = self.time() - self.start # delta-time self.t += self.dt # accumulate dt def time(self): if self.cuda: torch.cuda.synchronize() return time.time() class Timeout(contextlib.ContextDecorator): # YOLO Timeout class. Usage: @Timeout(seconds) decorator or 'with Timeout(seconds):' context manager def __init__(self, seconds, *, timeout_msg='', suppress_timeout_errors=True): self.seconds = int(seconds) self.timeout_message = timeout_msg self.suppress = bool(suppress_timeout_errors) def _timeout_handler(self, signum, frame): raise TimeoutError(self.timeout_message) def __enter__(self): if platform.system() != 'Windows': # not supported on Windows signal.signal(signal.SIGALRM, self._timeout_handler) # Set handler for SIGALRM signal.alarm(self.seconds) # start countdown for SIGALRM to be raised def __exit__(self, exc_type, exc_val, exc_tb): if platform.system() != 'Windows': signal.alarm(0) # Cancel SIGALRM if it's scheduled if self.suppress and exc_type is TimeoutError: # Suppress TimeoutError return True class WorkingDirectory(contextlib.ContextDecorator): # Usage: @WorkingDirectory(dir) decorator or 'with WorkingDirectory(dir):' context manager def __init__(self, new_dir): self.dir = new_dir # new dir self.cwd = Path.cwd().resolve() # current dir def __enter__(self): os.chdir(self.dir) def __exit__(self, exc_type, exc_val, exc_tb): os.chdir(self.cwd) def methods(instance): # Get class/instance methods return [f for f in dir(instance) if callable(getattr(instance, f)) and not f.startswith("__")] def print_args(args: Optional[dict] = None, show_file=True, show_func=False): # Print function arguments (optional args dict) x = inspect.currentframe().f_back # previous frame file, _, func, _, _ = inspect.getframeinfo(x) if args is None: # get args automatically args, _, _, frm = inspect.getargvalues(x) args = {k: v for k, v in frm.items() if k in args} try: file = Path(file).resolve().relative_to(ROOT).with_suffix('') except ValueError: file = Path(file).stem s = (f'{file}: ' if show_file else '') + (f'{func}: ' if show_func else '') LOGGER.info(colorstr(s) + ', '.join(f'{k}={v}' for k, v in args.items())) def init_seeds(seed=0, deterministic=False): # Initialize random number generator (RNG) seeds https://pytorch.org/docs/stable/notes/randomness.html random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) # for Multi-GPU, exception safe # torch.backends.cudnn.benchmark = True # AutoBatch problem https://github.com/ultralytics/yolov5/issues/9287 if deterministic and check_version(torch.__version__, '1.12.0'): # https://github.com/ultralytics/yolov5/pull/8213 torch.use_deterministic_algorithms(True) torch.backends.cudnn.deterministic = True os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8' os.environ['PYTHONHASHSEED'] = str(seed) def intersect_dicts(da, db, exclude=()): # Dictionary intersection of matching keys and shapes, omitting 'exclude' keys, using da values return {k: v for k, v in da.items() if k in db and all(x not in k for x in exclude) and v.shape == db[k].shape} def get_default_args(func): # Get func() default arguments signature = inspect.signature(func) return {k: v.default for k, v in signature.parameters.items() if v.default is not inspect.Parameter.empty} def get_latest_run(search_dir='.'): # Return path to most recent 'last.pt' in /runs (i.e. to --resume from) last_list = glob.glob(f'{search_dir}/**/last*.pt', recursive=True) return max(last_list, key=os.path.getctime) if last_list else '' def file_age(path=__file__): # Return days since last file update dt = (datetime.now() - datetime.fromtimestamp(Path(path).stat().st_mtime)) # delta return dt.days # + dt.seconds / 86400 # fractional days def file_date(path=__file__): # Return human-readable file modification date, i.e. '2021-3-26' t = datetime.fromtimestamp(Path(path).stat().st_mtime) return f'{t.year}-{t.month}-{t.day}' def file_size(path): # Return file/dir size (MB) mb = 1 << 20 # bytes to MiB (1024 ** 2) path = Path(path) if path.is_file(): return path.stat().st_size / mb elif path.is_dir(): return sum(f.stat().st_size for f in path.glob('**/*') if f.is_file()) / mb else: return 0.0 def check_online(): # Check internet connectivity import socket def run_once(): # Check once try: socket.create_connection(("1.1.1.1", 443), 5) # check host accessibility return True except OSError: return False return run_once() or run_once() # check twice to increase robustness to intermittent connectivity issues def git_describe(path=ROOT): # path must be a directory # Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe try: assert (Path(path) / '.git').is_dir() return check_output(f'git -C {path} describe --tags --long --always', shell=True).decode()[:-1] except Exception: return '' @TryExcept() @WorkingDirectory(ROOT) def check_git_status(repo='WongKinYiu/yolov9', branch='main'): # YOLO status check, recommend 'git pull' if code is out of date url = f'https://github.com/{repo}' msg = f', for updates see {url}' s = colorstr('github: ') # string assert Path('.git').exists(), s + 'skipping check (not a git repository)' + msg assert check_online(), s + 'skipping check (offline)' + msg splits = re.split(pattern=r'\s', string=check_output('git remote -v', shell=True).decode()) matches = [repo in s for s in splits] if any(matches): remote = splits[matches.index(True) - 1] else: remote = 'ultralytics' check_output(f'git remote add {remote} {url}', shell=True) check_output(f'git fetch {remote}', shell=True, timeout=5) # git fetch local_branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out n = int(check_output(f'git rev-list {local_branch}..{remote}/{branch} --count', shell=True)) # commits behind if n > 0: pull = 'git pull' if remote == 'origin' else f'git pull {remote} {branch}' s += f"⚠️ YOLO is out of date by {n} commit{'s' * (n > 1)}. Use `{pull}` or `git clone {url}` to update." else: s += f'up to date with {url} ✅' LOGGER.info(s) @WorkingDirectory(ROOT) def check_git_info(path='.'): # YOLO git info check, return {remote, branch, commit} check_requirements('gitpython') import git try: repo = git.Repo(path) remote = repo.remotes.origin.url.replace('.git', '') # i.e. 'https://github.com/WongKinYiu/yolov9' commit = repo.head.commit.hexsha # i.e. '3134699c73af83aac2a481435550b968d5792c0d' try: branch = repo.active_branch.name # i.e. 'main' except TypeError: # not on any branch branch = None # i.e. 'detached HEAD' state return {'remote': remote, 'branch': branch, 'commit': commit} except git.exc.InvalidGitRepositoryError: # path is not a git dir return {'remote': None, 'branch': None, 'commit': None} def check_python(minimum='3.7.0'): # Check current python version vs. required python version check_version(platform.python_version(), minimum, name='Python ', hard=True) def check_version(current='0.0.0', minimum='0.0.0', name='version ', pinned=False, hard=False, verbose=False): # Check version vs. required version current, minimum = (pkg.parse_version(x) for x in (current, minimum)) result = (current == minimum) if pinned else (current >= minimum) # bool s = f'WARNING ⚠️ {name}{minimum} is required by YOLO, but {name}{current} is currently installed' # string if hard: assert result, emojis(s) # assert min requirements met if verbose and not result: LOGGER.warning(s) return result @TryExcept() def check_requirements(requirements=ROOT / 'requirements.txt', exclude=(), install=True, cmds=''): # Check installed dependencies meet YOLO requirements (pass *.txt file or list of packages or single package str) prefix = colorstr('red', 'bold', 'requirements:') check_python() # check python version if isinstance(requirements, Path): # requirements.txt file file = requirements.resolve() assert file.exists(), f"{prefix} {file} not found, check failed." with file.open() as f: requirements = [f'{x.name}{x.specifier}' for x in pkg.parse_requirements(f) if x.name not in exclude] elif isinstance(requirements, str): requirements = [requirements] s = '' n = 0 for r in requirements: try: pkg.require(r) except (pkg.VersionConflict, pkg.DistributionNotFound): # exception if requirements not met s += f'"{r}" ' n += 1 if s and install and AUTOINSTALL: # check environment variable LOGGER.info(f"{prefix} YOLO requirement{'s' * (n > 1)} {s}not found, attempting AutoUpdate...") try: # assert check_online(), "AutoUpdate skipped (offline)" LOGGER.info(check_output(f'pip install {s} {cmds}', shell=True).decode()) source = file if 'file' in locals() else requirements s = f"{prefix} {n} package{'s' * (n > 1)} updated per {source}\n" \ f"{prefix} ⚠️ {colorstr('bold', 'Restart runtime or rerun command for updates to take effect')}\n" LOGGER.info(s) except Exception as e: LOGGER.warning(f'{prefix} ❌ {e}') def check_img_size(imgsz, s=32, floor=0): # Verify image size is a multiple of stride s in each dimension if isinstance(imgsz, int): # integer i.e. img_size=640 new_size = max(make_divisible(imgsz, int(s)), floor) else: # list i.e. img_size=[640, 480] imgsz = list(imgsz) # convert to list if tuple new_size = [max(make_divisible(x, int(s)), floor) for x in imgsz] if new_size != imgsz: LOGGER.warning(f'WARNING ⚠️ --img-size {imgsz} must be multiple of max stride {s}, updating to {new_size}') return new_size def check_imshow(warn=False): # Check if environment supports image displays try: assert not is_notebook() assert not is_docker() cv2.imshow('test', np.zeros((1, 1, 3))) cv2.waitKey(1) cv2.destroyAllWindows() cv2.waitKey(1) return True except Exception as e: if warn: LOGGER.warning(f'WARNING ⚠️ Environment does not support cv2.imshow() or PIL Image.show()\n{e}') return False def check_suffix(file='yolo.pt', suffix=('.pt',), msg=''): # Check file(s) for acceptable suffix if file and suffix: if isinstance(suffix, str): suffix = [suffix] for f in file if isinstance(file, (list, tuple)) else [file]: s = Path(f).suffix.lower() # file suffix if len(s): assert s in suffix, f"{msg}{f} acceptable suffix is {suffix}" def check_yaml(file, suffix=('.yaml', '.yml')): # Search/download YAML file (if necessary) and return path, checking suffix return check_file(file, suffix) def check_file(file, suffix=''): # Search/download file (if necessary) and return path check_suffix(file, suffix) # optional file = str(file) # convert to str() if os.path.isfile(file) or not file: # exists return file elif file.startswith(('http:/', 'https:/')): # download url = file # warning: Pathlib turns :// -> :/ file = Path(urllib.parse.unquote(file).split('?')[0]).name # '%2F' to '/', split https://url.com/file.txt?auth if os.path.isfile(file): LOGGER.info(f'Found {url} locally at {file}') # file already exists else: LOGGER.info(f'Downloading {url} to {file}...') torch.hub.download_url_to_file(url, file) assert Path(file).exists() and Path(file).stat().st_size > 0, f'File download failed: {url}' # check return file elif file.startswith('clearml://'): # ClearML Dataset ID assert 'clearml' in sys.modules, "ClearML is not installed, so cannot use ClearML dataset. Try running 'pip install clearml'." return file else: # search files = [] for d in 'data', 'models', 'utils': # search directories files.extend(glob.glob(str(ROOT / d / '**' / file), recursive=True)) # find file assert len(files), f'File not found: {file}' # assert file was found assert len(files) == 1, f"Multiple files match '{file}', specify exact path: {files}" # assert unique return files[0] # return file def check_font(font=FONT, progress=False): # Download font to CONFIG_DIR if necessary font = Path(font) file = CONFIG_DIR / font.name if not font.exists() and not file.exists(): url = f'https://ultralytics.com/assets/{font.name}' LOGGER.info(f'Downloading {url} to {file}...') torch.hub.download_url_to_file(url, str(file), progress=progress) def check_dataset(data, autodownload=True): # Download, check and/or unzip dataset if not found locally # Download (optional) extract_dir = '' if isinstance(data, (str, Path)) and (is_zipfile(data) or is_tarfile(data)): download(data, dir=f'{DATASETS_DIR}/{Path(data).stem}', unzip=True, delete=False, curl=False, threads=1) data = next((DATASETS_DIR / Path(data).stem).rglob('*.yaml')) extract_dir, autodownload = data.parent, False # Read yaml (optional) if isinstance(data, (str, Path)): data = yaml_load(data) # dictionary # Checks for k in 'train', 'val', 'names': assert k in data, emojis(f"data.yaml '{k}:' field missing ❌") if isinstance(data['names'], (list, tuple)): # old array format data['names'] = dict(enumerate(data['names'])) # convert to dict assert all(isinstance(k, int) for k in data['names'].keys()), 'data.yaml names keys must be integers, i.e. 2: car' data['nc'] = len(data['names']) # Resolve paths path = Path(extract_dir or data.get('path') or '') # optional 'path' default to '.' if not path.is_absolute(): path = (ROOT / path).resolve() data['path'] = path # download scripts for k in 'train', 'val', 'test': if data.get(k): # prepend path if isinstance(data[k], str): x = (path / data[k]).resolve() if not x.exists() and data[k].startswith('../'): x = (path / data[k][3:]).resolve() data[k] = str(x) else: data[k] = [str((path / x).resolve()) for x in data[k]] # Parse yaml train, val, test, s = (data.get(x) for x in ('train', 'val', 'test', 'download')) if val: val = [Path(x).resolve() for x in (val if isinstance(val, list) else [val])] # val path if not all(x.exists() for x in val): LOGGER.info('\nDataset not found ⚠️, missing paths %s' % [str(x) for x in val if not x.exists()]) if not s or not autodownload: raise Exception('Dataset not found ❌') t = time.time() if s.startswith('http') and s.endswith('.zip'): # URL f = Path(s).name # filename LOGGER.info(f'Downloading {s} to {f}...') torch.hub.download_url_to_file(s, f) Path(DATASETS_DIR).mkdir(parents=True, exist_ok=True) # create root unzip_file(f, path=DATASETS_DIR) # unzip Path(f).unlink() # remove zip r = None # success elif s.startswith('bash '): # bash script LOGGER.info(f'Running {s} ...') r = os.system(s) else: # python script r = exec(s, {'yaml': data}) # return None dt = f'({round(time.time() - t, 1)}s)' s = f"success ✅ {dt}, saved to {colorstr('bold', DATASETS_DIR)}" if r in (0, None) else f"failure {dt} ❌" LOGGER.info(f"Dataset download {s}") check_font('Arial.ttf' if is_ascii(data['names']) else 'Arial.Unicode.ttf', progress=True) # download fonts return data # dictionary def check_amp(model): # Check PyTorch Automatic Mixed Precision (AMP) functionality. Return True on correct operation from asone.detectors.yolov9.yolov9.models.common import AutoShape, DetectMultiBackend def amp_allclose(model, im): # All close FP32 vs AMP results m = AutoShape(model, verbose=False) # model a = m(im).xywhn[0] # FP32 inference m.amp = True b = m(im).xywhn[0] # AMP inference return a.shape == b.shape and torch.allclose(a, b, atol=0.1) # close to 10% absolute tolerance prefix = colorstr('AMP: ') device = next(model.parameters()).device # get model device if device.type in ('cpu', 'mps'): return False # AMP only used on CUDA devices f = ROOT / 'data' / 'images' / 'bus.jpg' # image to check im = f if f.exists() else 'https://ultralytics.com/images/bus.jpg' if check_online() else np.ones((640, 640, 3)) try: #assert amp_allclose(deepcopy(model), im) or amp_allclose(DetectMultiBackend('yolo.pt', device), im) LOGGER.info(f'{prefix}checks passed ✅') return True except Exception: help_url = 'https://github.com/ultralytics/yolov5/issues/7908' LOGGER.warning(f'{prefix}checks failed ❌, disabling Automatic Mixed Precision. See {help_url}') return False def yaml_load(file='data.yaml'): # Single-line safe yaml loading with open(file, errors='ignore') as f: return yaml.safe_load(f) def yaml_save(file='data.yaml', data={}): # Single-line safe yaml saving with open(file, 'w') as f: yaml.safe_dump({k: str(v) if isinstance(v, Path) else v for k, v in data.items()}, f, sort_keys=False) def unzip_file(file, path=None, exclude=('.DS_Store', '__MACOSX')): # Unzip a *.zip file to path/, excluding files containing strings in exclude list if path is None: path = Path(file).parent # default path with ZipFile(file) as zipObj: for f in zipObj.namelist(): # list all archived filenames in the zip if all(x not in f for x in exclude): zipObj.extract(f, path=path) def url2file(url): # Convert URL to filename, i.e. https://url.com/file.txt?auth -> file.txt url = str(Path(url)).replace(':/', '://') # Pathlib turns :// -> :/ return Path(urllib.parse.unquote(url)).name.split('?')[0] # '%2F' to '/', split https://url.com/file.txt?auth def download(url, dir='.', unzip=True, delete=True, curl=False, threads=1, retry=3): # Multithreaded file download and unzip function, used in data.yaml for autodownload def download_one(url, dir): # Download 1 file success = True if os.path.isfile(url): f = Path(url) # filename else: # does not exist f = dir / Path(url).name LOGGER.info(f'Downloading {url} to {f}...') for i in range(retry + 1): if curl: s = 'sS' if threads > 1 else '' # silent r = os.system( f'curl -# -{s}L "{url}" -o "{f}" --retry 9 -C -') # curl download with retry, continue success = r == 0 else: torch.hub.download_url_to_file(url, f, progress=threads == 1) # torch download success = f.is_file() if success: break elif i < retry: LOGGER.warning(f'⚠️ Download failure, retrying {i + 1}/{retry} {url}...') else: LOGGER.warning(f'❌ Failed to download {url}...') if unzip and success and (f.suffix == '.gz' or is_zipfile(f) or is_tarfile(f)): LOGGER.info(f'Unzipping {f}...') if is_zipfile(f): unzip_file(f, dir) # unzip elif is_tarfile(f): os.system(f'tar xf {f} --directory {f.parent}') # unzip elif f.suffix == '.gz': os.system(f'tar xfz {f} --directory {f.parent}') # unzip if delete: f.unlink() # remove zip dir = Path(dir) dir.mkdir(parents=True, exist_ok=True) # make directory if threads > 1: pool = ThreadPool(threads) pool.imap(lambda x: download_one(*x), zip(url, repeat(dir))) # multithreaded pool.close() pool.join() else: for u in [url] if isinstance(url, (str, Path)) else url: download_one(u, dir) def make_divisible(x, divisor): # Returns nearest x divisible by divisor if isinstance(divisor, torch.Tensor): divisor = int(divisor.max()) # to int return math.ceil(x / divisor) * divisor def clean_str(s): # Cleans a string by replacing special characters with underscore _ return re.sub(pattern="[|@#!¡·$€%&()=?¿^*;:,¨´><+]", repl="_", string=s) def one_cycle(y1=0.0, y2=1.0, steps=100): # lambda function for sinusoidal ramp from y1 to y2 https://arxiv.org/pdf/1812.01187.pdf return lambda x: ((1 - math.cos(x * math.pi / steps)) / 2) * (y2 - y1) + y1 def one_flat_cycle(y1=0.0, y2=1.0, steps=100): # lambda function for sinusoidal ramp from y1 to y2 https://arxiv.org/pdf/1812.01187.pdf #return lambda x: ((1 - math.cos(x * math.pi / steps)) / 2) * (y2 - y1) + y1 return lambda x: ((1 - math.cos((x - (steps // 2)) * math.pi / (steps // 2))) / 2) * (y2 - y1) + y1 if (x > (steps // 2)) else y1 def colorstr(*input): # Colors a string https://en.wikipedia.org/wiki/ANSI_escape_code, i.e. colorstr('blue', 'hello world') *args, string = input if len(input) > 1 else ('blue', 'bold', input[0]) # color arguments, string colors = { 'black': '\033[30m', # basic colors 'red': '\033[31m', 'green': '\033[32m', 'yellow': '\033[33m', 'blue': '\033[34m', 'magenta': '\033[35m', 'cyan': '\033[36m', 'white': '\033[37m', 'bright_black': '\033[90m', # bright colors 'bright_red': '\033[91m', 'bright_green': '\033[92m', 'bright_yellow': '\033[93m', 'bright_blue': '\033[94m', 'bright_magenta': '\033[95m', 'bright_cyan': '\033[96m', 'bright_white': '\033[97m', 'end': '\033[0m', # misc 'bold': '\033[1m', 'underline': '\033[4m'} return ''.join(colors[x] for x in args) + f'{string}' + colors['end'] def labels_to_class_weights(labels, nc=80): # Get class weights (inverse frequency) from training labels if labels[0] is None: # no labels loaded return torch.Tensor() labels = np.concatenate(labels, 0) # labels.shape = (866643, 5) for COCO classes = labels[:, 0].astype(int) # labels = [class xywh] weights = np.bincount(classes, minlength=nc) # occurrences per class # Prepend gridpoint count (for uCE training) # gpi = ((320 / 32 * np.array([1, 2, 4])) ** 2 * 3).sum() # gridpoints per image # weights = np.hstack([gpi * len(labels) - weights.sum() * 9, weights * 9]) ** 0.5 # prepend gridpoints to start weights[weights == 0] = 1 # replace empty bins with 1 weights = 1 / weights # number of targets per class weights /= weights.sum() # normalize return torch.from_numpy(weights).float() def labels_to_image_weights(labels, nc=80, class_weights=np.ones(80)): # Produces image weights based on class_weights and image contents # Usage: index = random.choices(range(n), weights=image_weights, k=1) # weighted image sample class_counts = np.array([np.bincount(x[:, 0].astype(int), minlength=nc) for x in labels]) return (class_weights.reshape(1, nc) * class_counts).sum(1) def coco80_to_coco91_class(): # converts 80-index (val2014) to 91-index (paper) # https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/ # a = np.loadtxt('data/coco.names', dtype='str', delimiter='\n') # b = np.loadtxt('data/coco_paper.names', dtype='str', delimiter='\n') # x1 = [list(a[i] == b).index(True) + 1 for i in range(80)] # darknet to coco # x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)] # coco to darknet return [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90] def xyxy2xywh(x): # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = (x[..., 0] + x[..., 2]) / 2 # x center y[..., 1] = (x[..., 1] + x[..., 3]) / 2 # y center y[..., 2] = x[..., 2] - x[..., 0] # width y[..., 3] = x[..., 3] - x[..., 1] # height return y def xywh2xyxy(x): # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = x[..., 0] - x[..., 2] / 2 # top left x y[..., 1] = x[..., 1] - x[..., 3] / 2 # top left y y[..., 2] = x[..., 0] + x[..., 2] / 2 # bottom right x y[..., 3] = x[..., 1] + x[..., 3] / 2 # bottom right y return y def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0): # Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = w * (x[..., 0] - x[..., 2] / 2) + padw # top left x y[..., 1] = h * (x[..., 1] - x[..., 3] / 2) + padh # top left y y[..., 2] = w * (x[..., 0] + x[..., 2] / 2) + padw # bottom right x y[..., 3] = h * (x[..., 1] + x[..., 3] / 2) + padh # bottom right y return y def xyxy2xywhn(x, w=640, h=640, clip=False, eps=0.0): # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] normalized where xy1=top-left, xy2=bottom-right if clip: clip_boxes(x, (h - eps, w - eps)) # warning: inplace clip y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = ((x[..., 0] + x[..., 2]) / 2) / w # x center y[..., 1] = ((x[..., 1] + x[..., 3]) / 2) / h # y center y[..., 2] = (x[..., 2] - x[..., 0]) / w # width y[..., 3] = (x[..., 3] - x[..., 1]) / h # height return y def xyn2xy(x, w=640, h=640, padw=0, padh=0): # Convert normalized segments into pixel segments, shape (n,2) y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = w * x[..., 0] + padw # top left x y[..., 1] = h * x[..., 1] + padh # top left y return y def segment2box(segment, width=640, height=640): # Convert 1 segment label to 1 box label, applying inside-image constraint, i.e. (xy1, xy2, ...) to (xyxy) x, y = segment.T # segment xy inside = (x >= 0) & (y >= 0) & (x <= width) & (y <= height) x, y, = x[inside], y[inside] return np.array([x.min(), y.min(), x.max(), y.max()]) if any(x) else np.zeros((1, 4)) # xyxy def segments2boxes(segments): # Convert segment labels to box labels, i.e. (cls, xy1, xy2, ...) to (cls, xywh) boxes = [] for s in segments: x, y = s.T # segment xy boxes.append([x.min(), y.min(), x.max(), y.max()]) # cls, xyxy return xyxy2xywh(np.array(boxes)) # cls, xywh def resample_segments(segments, n=1000): # Up-sample an (n,2) segment for i, s in enumerate(segments): s = np.concatenate((s, s[0:1, :]), axis=0) x = np.linspace(0, len(s) - 1, n) xp = np.arange(len(s)) segments[i] = np.concatenate([np.interp(x, xp, s[:, i]) for i in range(2)]).reshape(2, -1).T # segment xy return segments def scale_boxes(img1_shape, boxes, img0_shape, ratio_pad=None): # Rescale boxes (xyxy) from img1_shape to img0_shape if ratio_pad is None: # calculate from img0_shape gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding else: gain = ratio_pad[0][0] pad = ratio_pad[1] boxes[:, [0, 2]] -= pad[0] # x padding boxes[:, [1, 3]] -= pad[1] # y padding boxes[:, :4] /= gain clip_boxes(boxes, img0_shape) return boxes def scale_segments(img1_shape, segments, img0_shape, ratio_pad=None, normalize=False): # Rescale coords (xyxy) from img1_shape to img0_shape if ratio_pad is None: # calculate from img0_shape gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding else: gain = ratio_pad[0][0] pad = ratio_pad[1] segments[:, 0] -= pad[0] # x padding segments[:, 1] -= pad[1] # y padding segments /= gain clip_segments(segments, img0_shape) if normalize: segments[:, 0] /= img0_shape[1] # width segments[:, 1] /= img0_shape[0] # height return segments def clip_boxes(boxes, shape): # Clip boxes (xyxy) to image shape (height, width) if isinstance(boxes, torch.Tensor): # faster individually boxes[:, 0].clamp_(0, shape[1]) # x1 boxes[:, 1].clamp_(0, shape[0]) # y1 boxes[:, 2].clamp_(0, shape[1]) # x2 boxes[:, 3].clamp_(0, shape[0]) # y2 else: # np.array (faster grouped) boxes[:, [0, 2]] = boxes[:, [0, 2]].clip(0, shape[1]) # x1, x2 boxes[:, [1, 3]] = boxes[:, [1, 3]].clip(0, shape[0]) # y1, y2 def clip_segments(segments, shape): # Clip segments (xy1,xy2,...) to image shape (height, width) if isinstance(segments, torch.Tensor): # faster individually segments[:, 0].clamp_(0, shape[1]) # x segments[:, 1].clamp_(0, shape[0]) # y else: # np.array (faster grouped) segments[:, 0] = segments[:, 0].clip(0, shape[1]) # x segments[:, 1] = segments[:, 1].clip(0, shape[0]) # y def non_max_suppression( prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False, labels=(), max_det=300, nm=0, # number of masks ): """Non-Maximum Suppression (NMS) on inference results to reject overlapping detections Returns: list of detections, on (n,6) tensor per image [xyxy, conf, cls] """ if isinstance(prediction, (list, tuple)): # YOLO model in validation model, output = (inference_out, loss_out) prediction = prediction[0] # select only inference output device = prediction.device mps = 'mps' in device.type # Apple MPS if mps: # MPS not fully supported yet, convert tensors to CPU before NMS prediction = prediction.cpu() bs = prediction.shape[0] # batch size nc = prediction.shape[1] - nm - 4 # number of classes mi = 4 + nc # mask start index xc = prediction[:, 4:mi].amax(1) > conf_thres # candidates # Checks assert 0 <= conf_thres <= 1, f'Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0' assert 0 <= iou_thres <= 1, f'Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0' # Settings # min_wh = 2 # (pixels) minimum box width and height max_wh = 7680 # (pixels) maximum box width and height max_nms = 30000 # maximum number of boxes into torchvision.ops.nms() time_limit = 2.5 + 0.05 * bs # seconds to quit after redundant = True # require redundant detections multi_label &= nc > 1 # multiple labels per box (adds 0.5ms/img) merge = False # use merge-NMS t = time.time() output = [torch.zeros((0, 6 + nm), device=prediction.device)] * bs for xi, x in enumerate(prediction): # image index, image inference # Apply constraints # x[((x[:, 2:4] < min_wh) | (x[:, 2:4] > max_wh)).any(1), 4] = 0 # width-height x = x.T[xc[xi]] # confidence # Cat apriori labels if autolabelling if labels and len(labels[xi]): lb = labels[xi] v = torch.zeros((len(lb), nc + nm + 5), device=x.device) v[:, :4] = lb[:, 1:5] # box v[range(len(lb)), lb[:, 0].long() + 4] = 1.0 # cls x = torch.cat((x, v), 0) # If none remain process next image if not x.shape[0]: continue # Detections matrix nx6 (xyxy, conf, cls) box, cls, mask = x.split((4, nc, nm), 1) box = xywh2xyxy(box) # center_x, center_y, width, height) to (x1, y1, x2, y2) if multi_label: i, j = (cls > conf_thres).nonzero(as_tuple=False).T x = torch.cat((box[i], x[i, 4 + j, None], j[:, None].float(), mask[i]), 1) else: # best class only conf, j = cls.max(1, keepdim=True) x = torch.cat((box, conf, j.float(), mask), 1)[conf.view(-1) > conf_thres] # Filter by class if classes is not None: x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)] # Apply finite constraint # if not torch.isfinite(x).all(): # x = x[torch.isfinite(x).all(1)] # Check shape n = x.shape[0] # number of boxes if not n: # no boxes continue elif n > max_nms: # excess boxes x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence else: x = x[x[:, 4].argsort(descending=True)] # sort by confidence # Batched NMS c = x[:, 5:6] * (0 if agnostic else max_wh) # classes boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS if i.shape[0] > max_det: # limit detections i = i[:max_det] if merge and (1 < n < 3E3): # Merge NMS (boxes merged using weighted mean) # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4) iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix weights = iou * scores[None] # box weights x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes if redundant: i = i[iou.sum(1) > 1] # require redundancy output[xi] = x[i] if mps: output[xi] = output[xi].to(device) if (time.time() - t) > time_limit: LOGGER.warning(f'WARNING ⚠️ NMS time limit {time_limit:.3f}s exceeded') break # time limit exceeded return output def strip_optimizer(f='best.pt', s=''): # from utils.general import *; strip_optimizer() # Strip optimizer from 'f' to finalize training, optionally save as 's' x = torch.load(f, map_location=torch.device('cpu')) if x.get('ema'): x['model'] = x['ema'] # replace model with ema for k in 'optimizer', 'best_fitness', 'ema', 'updates': # keys x[k] = None x['epoch'] = -1 x['model'].half() # to FP16 for p in x['model'].parameters(): p.requires_grad = False torch.save(x, s or f) mb = os.path.getsize(s or f) / 1E6 # filesize LOGGER.info(f"Optimizer stripped from {f},{f' saved as {s},' if s else ''} {mb:.1f}MB") def print_mutation(keys, results, hyp, save_dir, bucket, prefix=colorstr('evolve: ')): evolve_csv = save_dir / 'evolve.csv' evolve_yaml = save_dir / 'hyp_evolve.yaml' keys = tuple(keys) + tuple(hyp.keys()) # [results + hyps] keys = tuple(x.strip() for x in keys) vals = results + tuple(hyp.values()) n = len(keys) # Download (optional) if bucket: url = f'gs://{bucket}/evolve.csv' if gsutil_getsize(url) > (evolve_csv.stat().st_size if evolve_csv.exists() else 0): os.system(f'gsutil cp {url} {save_dir}') # download evolve.csv if larger than local # Log to evolve.csv s = '' if evolve_csv.exists() else (('%20s,' * n % keys).rstrip(',') + '\n') # add header with open(evolve_csv, 'a') as f: f.write(s + ('%20.5g,' * n % vals).rstrip(',') + '\n') # Save yaml with open(evolve_yaml, 'w') as f: data = pd.read_csv(evolve_csv) data = data.rename(columns=lambda x: x.strip()) # strip keys i = np.argmax(fitness(data.values[:, :4])) # generations = len(data) f.write('# YOLO Hyperparameter Evolution Results\n' + f'# Best generation: {i}\n' + f'# Last generation: {generations - 1}\n' + '# ' + ', '.join(f'{x.strip():>20s}' for x in keys[:7]) + '\n' + '# ' + ', '.join(f'{x:>20.5g}' for x in data.values[i, :7]) + '\n\n') yaml.safe_dump(data.loc[i][7:].to_dict(), f, sort_keys=False) # Print to screen LOGGER.info(prefix + f'{generations} generations finished, current result:\n' + prefix + ', '.join(f'{x.strip():>20s}' for x in keys) + '\n' + prefix + ', '.join(f'{x:20.5g}' for x in vals) + '\n\n') if bucket: os.system(f'gsutil cp {evolve_csv} {evolve_yaml} gs://{bucket}') # upload def apply_classifier(x, model, img, im0): # Apply a second stage classifier to YOLO outputs # Example model = torchvision.models.__dict__['efficientnet_b0'](pretrained=True).to(device).eval() im0 = [im0] if isinstance(im0, np.ndarray) else im0 for i, d in enumerate(x): # per image if d is not None and len(d): d = d.clone() # Reshape and pad cutouts b = xyxy2xywh(d[:, :4]) # boxes b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1) # rectangle to square b[:, 2:] = b[:, 2:] * 1.3 + 30 # pad d[:, :4] = xywh2xyxy(b).long() # Rescale boxes from img_size to im0 size scale_boxes(img.shape[2:], d[:, :4], im0[i].shape) # Classes pred_cls1 = d[:, 5].long() ims = [] for a in d: cutout = im0[i][int(a[1]):int(a[3]), int(a[0]):int(a[2])] im = cv2.resize(cutout, (224, 224)) # BGR im = im[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 im = np.ascontiguousarray(im, dtype=np.float32) # uint8 to float32 im /= 255 # 0 - 255 to 0.0 - 1.0 ims.append(im) pred_cls2 = model(torch.Tensor(ims).to(d.device)).argmax(1) # classifier prediction x[i] = x[i][pred_cls1 == pred_cls2] # retain matching class detections return x def increment_path(path, exist_ok=False, sep='', mkdir=False): # Increment file or directory path, i.e. runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc. path = Path(path) # os-agnostic if path.exists() and not exist_ok: path, suffix = (path.with_suffix(''), path.suffix) if path.is_file() else (path, '') # Method 1 for n in range(2, 9999): p = f'{path}{sep}{n}{suffix}' # increment path if not os.path.exists(p): # break path = Path(p) # Method 2 (deprecated) # dirs = glob.glob(f"{path}{sep}*") # similar paths # matches = [re.search(rf"{path.stem}{sep}(\d+)", d) for d in dirs] # i = [int(m.groups()[0]) for m in matches if m] # indices # n = max(i) + 1 if i else 2 # increment number # path = Path(f"{path}{sep}{n}{suffix}") # increment path if mkdir: path.mkdir(parents=True, exist_ok=True) # make directory return path # OpenCV Chinese-friendly functions ------------------------------------------------------------------------------------ imshow_ = cv2.imshow # copy to avoid recursion errors def imread(path, flags=cv2.IMREAD_COLOR): return cv2.imdecode(np.fromfile(path, np.uint8), flags) def imwrite(path, im): try: cv2.imencode(Path(path).suffix, im)[1].tofile(path) return True except Exception: return False def imshow(path, im): imshow_(path.encode('unicode_escape').decode(), im) cv2.imread, cv2.imwrite, cv2.imshow = imread, imwrite, imshow # redefine # Variables ------------------------------------------------------------------------------------------------------------ ================================================ FILE: asone/detectors/yolov9/yolov9/utils/lion.py ================================================ """PyTorch implementation of the Lion optimizer.""" import torch from torch.optim.optimizer import Optimizer class Lion(Optimizer): r"""Implements Lion algorithm.""" def __init__(self, params, lr=1e-4, betas=(0.9, 0.99), weight_decay=0.0): """Initialize the hyperparameters. Args: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-4) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square (default: (0.9, 0.99)) weight_decay (float, optional): weight decay coefficient (default: 0) """ if not 0.0 <= lr: raise ValueError('Invalid learning rate: {}'.format(lr)) if not 0.0 <= betas[0] < 1.0: raise ValueError('Invalid beta parameter at index 0: {}'.format(betas[0])) if not 0.0 <= betas[1] < 1.0: raise ValueError('Invalid beta parameter at index 1: {}'.format(betas[1])) defaults = dict(lr=lr, betas=betas, weight_decay=weight_decay) super().__init__(params, defaults) @torch.no_grad() def step(self, closure=None): """Performs a single optimization step. Args: closure (callable, optional): A closure that reevaluates the model and returns the loss. Returns: the loss. """ loss = None if closure is not None: with torch.enable_grad(): loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue # Perform stepweight decay p.data.mul_(1 - group['lr'] * group['weight_decay']) grad = p.grad state = self.state[p] # State initialization if len(state) == 0: # Exponential moving average of gradient values state['exp_avg'] = torch.zeros_like(p) exp_avg = state['exp_avg'] beta1, beta2 = group['betas'] # Weight update update = exp_avg * beta1 + grad * (1 - beta1) p.add_(torch.sign(update), alpha=-group['lr']) # Decay the momentum running average coefficient exp_avg.mul_(beta2).add_(grad, alpha=1 - beta2) return loss ================================================ FILE: asone/detectors/yolov9/yolov9/utils/loss.py ================================================ import torch import torch.nn as nn import torch.nn.functional as F from asone.detectors.yolov9.yolov9.utils.metrics import bbox_iou from asone.detectors.yolov9.yolov9.utils.torch_utils import de_parallel def smooth_BCE(eps=0.1): # https://github.com/ultralytics/yolov3/issues/238#issuecomment-598028441 # return positive, negative label smoothing BCE targets return 1.0 - 0.5 * eps, 0.5 * eps class BCEBlurWithLogitsLoss(nn.Module): # BCEwithLogitLoss() with reduced missing label effects. def __init__(self, alpha=0.05): super().__init__() self.loss_fcn = nn.BCEWithLogitsLoss(reduction='none') # must be nn.BCEWithLogitsLoss() self.alpha = alpha def forward(self, pred, true): loss = self.loss_fcn(pred, true) pred = torch.sigmoid(pred) # prob from logits dx = pred - true # reduce only missing label effects # dx = (pred - true).abs() # reduce missing label and false label effects alpha_factor = 1 - torch.exp((dx - 1) / (self.alpha + 1e-4)) loss *= alpha_factor return loss.mean() class FocalLoss(nn.Module): # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5) def __init__(self, loss_fcn, gamma=1.5, alpha=0.25): super().__init__() self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss() self.gamma = gamma self.alpha = alpha self.reduction = loss_fcn.reduction self.loss_fcn.reduction = 'none' # required to apply FL to each element def forward(self, pred, true): loss = self.loss_fcn(pred, true) # p_t = torch.exp(-loss) # loss *= self.alpha * (1.000001 - p_t) ** self.gamma # non-zero power for gradient stability # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py pred_prob = torch.sigmoid(pred) # prob from logits p_t = true * pred_prob + (1 - true) * (1 - pred_prob) alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha) modulating_factor = (1.0 - p_t) ** self.gamma loss *= alpha_factor * modulating_factor if self.reduction == 'mean': return loss.mean() elif self.reduction == 'sum': return loss.sum() else: # 'none' return loss class QFocalLoss(nn.Module): # Wraps Quality focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5) def __init__(self, loss_fcn, gamma=1.5, alpha=0.25): super().__init__() self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss() self.gamma = gamma self.alpha = alpha self.reduction = loss_fcn.reduction self.loss_fcn.reduction = 'none' # required to apply FL to each element def forward(self, pred, true): loss = self.loss_fcn(pred, true) pred_prob = torch.sigmoid(pred) # prob from logits alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha) modulating_factor = torch.abs(true - pred_prob) ** self.gamma loss *= alpha_factor * modulating_factor if self.reduction == 'mean': return loss.mean() elif self.reduction == 'sum': return loss.sum() else: # 'none' return loss class ComputeLoss: sort_obj_iou = False # Compute losses def __init__(self, model, autobalance=False): device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['cls_pw']], device=device)) BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device)) # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) # positive, negative BCE targets # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g) m = de_parallel(model).model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7 self.ssi = list(m.stride).index(16) if autobalance else 0 # stride 16 index self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, 1.0, h, autobalance self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.anchors = m.anchors self.device = device def __call__(self, p, targets): # predictions, targets bs = p[0].shape[0] # batch size loss = torch.zeros(3, device=self.device) # [box, obj, cls] losses tcls, tbox, indices = self.build_targets(p, targets) # targets # Losses for i, pi in enumerate(p): # layer index, layer predictions b, gj, gi = indices[i] # image, anchor, gridy, gridx tobj = torch.zeros((pi.shape[0], pi.shape[2], pi.shape[3]), dtype=pi.dtype, device=self.device) # tgt obj n_labels = b.shape[0] # number of labels if n_labels: # pxy, pwh, _, pcls = pi[b, a, gj, gi].tensor_split((2, 4, 5), dim=1) # faster, requires torch 1.8.0 pxy, pwh, _, pcls = pi[b, :, gj, gi].split((2, 2, 1, self.nc), 1) # target-subset of predictions # Regression # pwh = (pwh.sigmoid() * 2) ** 2 * anchors[i] # pwh = (0.0 + (pwh - 1.09861).sigmoid() * 4) * anchors[i] # pwh = (0.33333 + (pwh - 1.09861).sigmoid() * 2.66667) * anchors[i] # pwh = (0.25 + (pwh - 1.38629).sigmoid() * 3.75) * anchors[i] # pwh = (0.20 + (pwh - 1.60944).sigmoid() * 4.8) * anchors[i] # pwh = (0.16667 + (pwh - 1.79175).sigmoid() * 5.83333) * anchors[i] pxy = pxy.sigmoid() * 1.6 - 0.3 pwh = (0.2 + pwh.sigmoid() * 4.8) * self.anchors[i] pbox = torch.cat((pxy, pwh), 1) # predicted box iou = bbox_iou(pbox, tbox[i], CIoU=True).squeeze() # iou(prediction, target) loss[0] += (1.0 - iou).mean() # box loss # Objectness iou = iou.detach().clamp(0).type(tobj.dtype) if self.sort_obj_iou: j = iou.argsort() b, gj, gi, iou = b[j], gj[j], gi[j], iou[j] if self.gr < 1: iou = (1.0 - self.gr) + self.gr * iou tobj[b, gj, gi] = iou # iou ratio # Classification if self.nc > 1: # cls loss (only if multiple classes) t = torch.full_like(pcls, self.cn, device=self.device) # targets t[range(n_labels), tcls[i]] = self.cp loss[2] += self.BCEcls(pcls, t) # cls loss obji = self.BCEobj(pi[:, 4], tobj) loss[1] += obji * self.balance[i] # obj loss if self.autobalance: self.balance[i] = self.balance[i] * 0.9999 + 0.0001 / obji.detach().item() if self.autobalance: self.balance = [x / self.balance[self.ssi] for x in self.balance] loss[0] *= self.hyp['box'] loss[1] *= self.hyp['obj'] loss[2] *= self.hyp['cls'] return loss.sum() * bs, loss.detach() # [box, obj, cls] losses def build_targets(self, p, targets): # Build targets for compute_loss(), input targets(image,class,x,y,w,h) nt = targets.shape[0] # number of anchors, targets tcls, tbox, indices = [], [], [] gain = torch.ones(6, device=self.device) # normalized to gridspace gain g = 0.3 # bias off = torch.tensor( [ [0, 0], [1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m # [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm ], device=self.device).float() * g # offsets for i in range(self.nl): shape = p[i].shape gain[2:6] = torch.tensor(shape)[[3, 2, 3, 2]] # xyxy gain # Match targets to anchors t = targets * gain # shape(3,n,7) if nt: # Matches r = t[..., 4:6] / self.anchors[i] # wh ratio j = torch.max(r, 1 / r).max(1)[0] < self.hyp['anchor_t'] # compare # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2)) t = t[j] # filter # Offsets gxy = t[:, 2:4] # grid xy gxi = gain[[2, 3]] - gxy # inverse j, k = ((gxy % 1 < g) & (gxy > 1)).T l, m = ((gxi % 1 < g) & (gxi > 1)).T j = torch.stack((torch.ones_like(j), j, k, l, m)) t = t.repeat((5, 1, 1))[j] offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j] else: t = targets[0] offsets = 0 # Define bc, gxy, gwh = t.chunk(3, 1) # (image, class), grid xy, grid wh b, c = bc.long().T # image, class gij = (gxy - offsets).long() gi, gj = gij.T # grid indices # Append indices.append((b, gj.clamp_(0, shape[2] - 1), gi.clamp_(0, shape[3] - 1))) # image, grid_y, grid_x indices tbox.append(torch.cat((gxy - gij, gwh), 1)) # box tcls.append(c) # class return tcls, tbox, indices class ComputeLoss_NEW: sort_obj_iou = False # Compute losses def __init__(self, model, autobalance=False): device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['cls_pw']], device=device)) BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device)) # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) # positive, negative BCE targets # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g) m = de_parallel(model).model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7 self.ssi = list(m.stride).index(16) if autobalance else 0 # stride 16 index self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, 1.0, h, autobalance self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.anchors = m.anchors self.device = device self.BCE_base = nn.BCEWithLogitsLoss(reduction='none') def __call__(self, p, targets): # predictions, targets tcls, tbox, indices = self.build_targets(p, targets) # targets bs = p[0].shape[0] # batch size n_labels = targets.shape[0] # number of labels loss = torch.zeros(3, device=self.device) # [box, obj, cls] losses # Compute all losses all_loss = [] for i, pi in enumerate(p): # layer index, layer predictions b, gj, gi = indices[i] # image, anchor, gridy, gridx if n_labels: pxy, pwh, pobj, pcls = pi[b, :, gj, gi].split((2, 2, 1, self.nc), 2) # target-subset of predictions # Regression pbox = torch.cat((pxy.sigmoid() * 1.6 - 0.3, (0.2 + pwh.sigmoid() * 4.8) * self.anchors[i]), 2) iou = bbox_iou(pbox, tbox[i], CIoU=True).squeeze() # iou(predicted_box, target_box) obj_target = iou.detach().clamp(0).type(pi.dtype) # objectness targets all_loss.append([(1.0 - iou) * self.hyp['box'], self.BCE_base(pobj.squeeze(), torch.ones_like(obj_target)) * self.hyp['obj'], self.BCE_base(pcls, F.one_hot(tcls[i], self.nc).float()).mean(2) * self.hyp['cls'], obj_target, tbox[i][..., 2] > 0.0]) # valid # Lowest 3 losses per label n_assign = 4 # top n matches cat_loss = [torch.cat(x, 1) for x in zip(*all_loss)] ij = torch.zeros_like(cat_loss[0]).bool() # top 3 mask sum_loss = cat_loss[0] + cat_loss[2] for col in torch.argsort(sum_loss, dim=1).T[:n_assign]: # ij[range(n_labels), col] = True ij[range(n_labels), col] = cat_loss[4][range(n_labels), col] loss[0] = cat_loss[0][ij].mean() * self.nl # box loss loss[2] = cat_loss[2][ij].mean() * self.nl # cls loss # Obj loss for i, (h, pi) in enumerate(zip(ij.chunk(self.nl, 1), p)): # layer index, layer predictions b, gj, gi = indices[i] # image, anchor, gridy, gridx tobj = torch.zeros((pi.shape[0], pi.shape[2], pi.shape[3]), dtype=pi.dtype, device=self.device) # obj if n_labels: # if any labels tobj[b[h], gj[h], gi[h]] = all_loss[i][3][h] loss[1] += self.BCEobj(pi[:, 4], tobj) * (self.balance[i] * self.hyp['obj']) return loss.sum() * bs, loss.detach() # [box, obj, cls] losses def build_targets(self, p, targets): # Build targets for compute_loss(), input targets(image,class,x,y,w,h) nt = targets.shape[0] # number of anchors, targets tcls, tbox, indices = [], [], [] gain = torch.ones(6, device=self.device) # normalized to gridspace gain g = 0.3 # bias off = torch.tensor( [ [0, 0], [1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m # [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm ], device=self.device).float() # offsets for i in range(self.nl): shape = p[i].shape gain[2:6] = torch.tensor(shape)[[3, 2, 3, 2]] # xyxy gain # Match targets to anchors t = targets * gain # shape(3,n,7) if nt: # # Matches r = t[..., 4:6] / self.anchors[i] # wh ratio a = torch.max(r, 1 / r).max(1)[0] < self.hyp['anchor_t'] # compare # a = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2)) # t = t[a] # filter # # Offsets gxy = t[:, 2:4] # grid xy gxi = gain[[2, 3]] - gxy # inverse j, k = ((gxy % 1 < g) & (gxy > 1)).T l, m = ((gxi % 1 < g) & (gxi > 1)).T j = torch.stack((torch.ones_like(j), j, k, l, m)) & a t = t.repeat((5, 1, 1)) offsets = torch.zeros_like(gxy)[None] + off[:, None] t[..., 4:6][~j] = 0.0 # move unsuitable targets far away else: t = targets[0] offsets = 0 # Define bc, gxy, gwh = t.chunk(3, 2) # (image, class), grid xy, grid wh b, c = bc.long().transpose(0, 2).contiguous() # image, class gij = (gxy - offsets).long() gi, gj = gij.transpose(0, 2).contiguous() # grid indices # Append indices.append((b, gj.clamp_(0, shape[2] - 1), gi.clamp_(0, shape[3] - 1))) # image, grid_y, grid_x indices tbox.append(torch.cat((gxy - gij, gwh), 2).permute(1, 0, 2).contiguous()) # box tcls.append(c) # class # # Unique # n1 = torch.cat((b.view(-1, 1), tbox[i].view(-1, 4)), 1).shape[0] # n2 = tbox[i].view(-1, 4).unique(dim=0).shape[0] # print(f'targets-unique {n1}-{n2} diff={n1-n2}') return tcls, tbox, indices ================================================ FILE: asone/detectors/yolov9/yolov9/utils/loss_tal.py ================================================ import os import torch import torch.nn as nn import torch.nn.functional as F from asone.detectors.yolov9.yolov9.utils.general import xywh2xyxy from asone.detectors.yolov9.yolov9.utils.metrics import bbox_iou from asone.detectors.yolov9.yolov9.utils.tal.anchor_generator import dist2bbox, make_anchors, bbox2dist from asone.detectors.yolov9.yolov9.utils.tal.assigner import TaskAlignedAssigner from asone.detectors.yolov9.yolov9.utils.torch_utils import de_parallel def smooth_BCE(eps=0.1): # https://github.com/ultralytics/yolov3/issues/238#issuecomment-598028441 # return positive, negative label smoothing BCE targets return 1.0 - 0.5 * eps, 0.5 * eps class VarifocalLoss(nn.Module): # Varifocal loss by Zhang et al. https://arxiv.org/abs/2008.13367 def __init__(self): super().__init__() def forward(self, pred_score, gt_score, label, alpha=0.75, gamma=2.0): weight = alpha * pred_score.sigmoid().pow(gamma) * (1 - label) + gt_score * label with torch.cuda.amp.autocast(enabled=False): loss = (F.binary_cross_entropy_with_logits(pred_score.float(), gt_score.float(), reduction="none") * weight).sum() return loss class FocalLoss(nn.Module): # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5) def __init__(self, loss_fcn, gamma=1.5, alpha=0.25): super().__init__() self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss() self.gamma = gamma self.alpha = alpha self.reduction = loss_fcn.reduction self.loss_fcn.reduction = "none" # required to apply FL to each element def forward(self, pred, true): loss = self.loss_fcn(pred, true) # p_t = torch.exp(-loss) # loss *= self.alpha * (1.000001 - p_t) ** self.gamma # non-zero power for gradient stability # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py pred_prob = torch.sigmoid(pred) # prob from logits p_t = true * pred_prob + (1 - true) * (1 - pred_prob) alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha) modulating_factor = (1.0 - p_t) ** self.gamma loss *= alpha_factor * modulating_factor if self.reduction == "mean": return loss.mean() elif self.reduction == "sum": return loss.sum() else: # 'none' return loss class BboxLoss(nn.Module): def __init__(self, reg_max, use_dfl=False): super().__init__() self.reg_max = reg_max self.use_dfl = use_dfl def forward(self, pred_dist, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask): # iou loss bbox_mask = fg_mask.unsqueeze(-1).repeat([1, 1, 4]) # (b, h*w, 4) pred_bboxes_pos = torch.masked_select(pred_bboxes, bbox_mask).view(-1, 4) target_bboxes_pos = torch.masked_select(target_bboxes, bbox_mask).view(-1, 4) bbox_weight = torch.masked_select(target_scores.sum(-1), fg_mask).unsqueeze(-1) iou = bbox_iou(pred_bboxes_pos, target_bboxes_pos, xywh=False, CIoU=True) loss_iou = 1.0 - iou loss_iou *= bbox_weight loss_iou = loss_iou.sum() / target_scores_sum # dfl loss if self.use_dfl: dist_mask = fg_mask.unsqueeze(-1).repeat([1, 1, (self.reg_max + 1) * 4]) pred_dist_pos = torch.masked_select(pred_dist, dist_mask).view(-1, 4, self.reg_max + 1) target_ltrb = bbox2dist(anchor_points, target_bboxes, self.reg_max) target_ltrb_pos = torch.masked_select(target_ltrb, bbox_mask).view(-1, 4) loss_dfl = self._df_loss(pred_dist_pos, target_ltrb_pos) * bbox_weight loss_dfl = loss_dfl.sum() / target_scores_sum else: loss_dfl = torch.tensor(0.0).to(pred_dist.device) return loss_iou, loss_dfl, iou def _df_loss(self, pred_dist, target): target_left = target.to(torch.long) target_right = target_left + 1 weight_left = target_right.to(torch.float) - target weight_right = 1 - weight_left loss_left = F.cross_entropy(pred_dist.view(-1, self.reg_max + 1), target_left.view(-1), reduction="none").view( target_left.shape) * weight_left loss_right = F.cross_entropy(pred_dist.view(-1, self.reg_max + 1), target_right.view(-1), reduction="none").view(target_left.shape) * weight_right return (loss_left + loss_right).mean(-1, keepdim=True) class ComputeLoss: # Compute losses def __init__(self, model, use_dfl=True): device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h["cls_pw"]], device=device), reduction='none') # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get("label_smoothing", 0.0)) # positive, negative BCE targets # Focal loss g = h["fl_gamma"] # focal loss gamma if g > 0: BCEcls = FocalLoss(BCEcls, g) m = de_parallel(model).model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7 self.BCEcls = BCEcls self.hyp = h self.stride = m.stride # model strides self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.no = m.no self.reg_max = m.reg_max self.device = device self.assigner = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.proj = torch.arange(m.reg_max).float().to(device) # / 120.0 self.use_dfl = use_dfl def preprocess(self, targets, batch_size, scale_tensor): if targets.shape[0] == 0: out = torch.zeros(batch_size, 0, 5, device=self.device) else: i = targets[:, 0] # image index _, counts = i.unique(return_counts=True) out = torch.zeros(batch_size, counts.max(), 5, device=self.device) for j in range(batch_size): matches = i == j n = matches.sum() if n: out[j, :n] = targets[matches, 1:] out[..., 1:5] = xywh2xyxy(out[..., 1:5].mul_(scale_tensor)) return out def bbox_decode(self, anchor_points, pred_dist): if self.use_dfl: b, a, c = pred_dist.shape # batch, anchors, channels pred_dist = pred_dist.view(b, a, 4, c // 4).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = pred_dist.view(b, a, c // 4, 4).transpose(2,3).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = (pred_dist.view(b, a, c // 4, 4).softmax(2) * self.proj.type(pred_dist.dtype).view(1, 1, -1, 1)).sum(2) return dist2bbox(pred_dist, anchor_points, xywh=False) def __call__(self, p, targets, img=None, epoch=0): loss = torch.zeros(3, device=self.device) # box, cls, dfl feats = p[1] if isinstance(p, tuple) else p pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores = pred_scores.permute(0, 2, 1).contiguous() pred_distri = pred_distri.permute(0, 2, 1).contiguous() dtype = pred_scores.dtype batch_size, grid_size = pred_scores.shape[:2] imgsz = torch.tensor(feats[0].shape[2:], device=self.device, dtype=dtype) * self.stride[0] # image size (h,w) anchor_points, stride_tensor = make_anchors(feats, self.stride, 0.5) # targets targets = self.preprocess(targets, batch_size, scale_tensor=imgsz[[1, 0, 1, 0]]) gt_labels, gt_bboxes = targets.split((1, 4), 2) # cls, xyxy mask_gt = gt_bboxes.sum(2, keepdim=True).gt_(0) # pboxes pred_bboxes = self.bbox_decode(anchor_points, pred_distri) # xyxy, (b, h*w, 4) target_labels, target_bboxes, target_scores, fg_mask = self.assigner( pred_scores.detach().sigmoid(), (pred_bboxes.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_bboxes /= stride_tensor target_scores_sum = max(target_scores.sum(), 1) # cls loss # loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum # VFL way loss[1] = self.BCEcls(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum # BCE # bbox loss if fg_mask.sum(): loss[0], loss[2], iou = self.bbox_loss(pred_distri, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask) loss[0] *= 7.5 # box gain loss[1] *= 0.5 # cls gain loss[2] *= 1.5 # dfl gain return loss.sum() * batch_size, loss.detach() # loss(box, cls, dfl) ================================================ FILE: asone/detectors/yolov9/yolov9/utils/loss_tal_dual.py ================================================ import os import torch import torch.nn as nn import torch.nn.functional as F from asone.detectors.yolov9.yolov9.utils.general import xywh2xyxy from asone.detectors.yolov9.yolov9.utils.metrics import bbox_iou from asone.detectors.yolov9.yolov9.utils.tal.anchor_generator import dist2bbox, make_anchors, bbox2dist from asone.detectors.yolov9.yolov9.utils.tal.assigner import TaskAlignedAssigner from asone.detectors.yolov9.yolov9.utils.torch_utils import de_parallel def smooth_BCE(eps=0.1): # https://github.com/ultralytics/yolov3/issues/238#issuecomment-598028441 # return positive, negative label smoothing BCE targets return 1.0 - 0.5 * eps, 0.5 * eps class VarifocalLoss(nn.Module): # Varifocal loss by Zhang et al. https://arxiv.org/abs/2008.13367 def __init__(self): super().__init__() def forward(self, pred_score, gt_score, label, alpha=0.75, gamma=2.0): weight = alpha * pred_score.sigmoid().pow(gamma) * (1 - label) + gt_score * label with torch.cuda.amp.autocast(enabled=False): loss = (F.binary_cross_entropy_with_logits(pred_score.float(), gt_score.float(), reduction="none") * weight).sum() return loss class FocalLoss(nn.Module): # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5) def __init__(self, loss_fcn, gamma=1.5, alpha=0.25): super().__init__() self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss() self.gamma = gamma self.alpha = alpha self.reduction = loss_fcn.reduction self.loss_fcn.reduction = "none" # required to apply FL to each element def forward(self, pred, true): loss = self.loss_fcn(pred, true) # p_t = torch.exp(-loss) # loss *= self.alpha * (1.000001 - p_t) ** self.gamma # non-zero power for gradient stability # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py pred_prob = torch.sigmoid(pred) # prob from logits p_t = true * pred_prob + (1 - true) * (1 - pred_prob) alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha) modulating_factor = (1.0 - p_t) ** self.gamma loss *= alpha_factor * modulating_factor if self.reduction == "mean": return loss.mean() elif self.reduction == "sum": return loss.sum() else: # 'none' return loss class BboxLoss(nn.Module): def __init__(self, reg_max, use_dfl=False): super().__init__() self.reg_max = reg_max self.use_dfl = use_dfl def forward(self, pred_dist, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask): # iou loss bbox_mask = fg_mask.unsqueeze(-1).repeat([1, 1, 4]) # (b, h*w, 4) pred_bboxes_pos = torch.masked_select(pred_bboxes, bbox_mask).view(-1, 4) target_bboxes_pos = torch.masked_select(target_bboxes, bbox_mask).view(-1, 4) bbox_weight = torch.masked_select(target_scores.sum(-1), fg_mask).unsqueeze(-1) iou = bbox_iou(pred_bboxes_pos, target_bboxes_pos, xywh=False, CIoU=True) loss_iou = 1.0 - iou loss_iou *= bbox_weight loss_iou = loss_iou.sum() / target_scores_sum # dfl loss if self.use_dfl: dist_mask = fg_mask.unsqueeze(-1).repeat([1, 1, (self.reg_max + 1) * 4]) pred_dist_pos = torch.masked_select(pred_dist, dist_mask).view(-1, 4, self.reg_max + 1) target_ltrb = bbox2dist(anchor_points, target_bboxes, self.reg_max) target_ltrb_pos = torch.masked_select(target_ltrb, bbox_mask).view(-1, 4) loss_dfl = self._df_loss(pred_dist_pos, target_ltrb_pos) * bbox_weight loss_dfl = loss_dfl.sum() / target_scores_sum else: loss_dfl = torch.tensor(0.0).to(pred_dist.device) return loss_iou, loss_dfl, iou def _df_loss(self, pred_dist, target): target_left = target.to(torch.long) target_right = target_left + 1 weight_left = target_right.to(torch.float) - target weight_right = 1 - weight_left loss_left = F.cross_entropy(pred_dist.view(-1, self.reg_max + 1), target_left.view(-1), reduction="none").view( target_left.shape) * weight_left loss_right = F.cross_entropy(pred_dist.view(-1, self.reg_max + 1), target_right.view(-1), reduction="none").view(target_left.shape) * weight_right return (loss_left + loss_right).mean(-1, keepdim=True) class ComputeLoss: # Compute losses def __init__(self, model, use_dfl=True): device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h["cls_pw"]], device=device), reduction='none') # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get("label_smoothing", 0.0)) # positive, negative BCE targets # Focal loss g = h["fl_gamma"] # focal loss gamma if g > 0: BCEcls = FocalLoss(BCEcls, g) m = de_parallel(model).model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7 self.BCEcls = BCEcls self.hyp = h self.stride = m.stride # model strides self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.no = m.no self.reg_max = m.reg_max self.device = device self.assigner = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.assigner2 = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.bbox_loss2 = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.proj = torch.arange(m.reg_max).float().to(device) # / 120.0 self.use_dfl = use_dfl def preprocess(self, targets, batch_size, scale_tensor): if targets.shape[0] == 0: out = torch.zeros(batch_size, 0, 5, device=self.device) else: i = targets[:, 0] # image index _, counts = i.unique(return_counts=True) out = torch.zeros(batch_size, counts.max(), 5, device=self.device) for j in range(batch_size): matches = i == j n = matches.sum() if n: out[j, :n] = targets[matches, 1:] out[..., 1:5] = xywh2xyxy(out[..., 1:5].mul_(scale_tensor)) return out def bbox_decode(self, anchor_points, pred_dist): if self.use_dfl: b, a, c = pred_dist.shape # batch, anchors, channels pred_dist = pred_dist.view(b, a, 4, c // 4).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = pred_dist.view(b, a, c // 4, 4).transpose(2,3).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = (pred_dist.view(b, a, c // 4, 4).softmax(2) * self.proj.type(pred_dist.dtype).view(1, 1, -1, 1)).sum(2) return dist2bbox(pred_dist, anchor_points, xywh=False) def __call__(self, p, targets, img=None, epoch=0): loss = torch.zeros(3, device=self.device) # box, cls, dfl feats = p[1][0] if isinstance(p, tuple) else p[0] feats2 = p[1][1] if isinstance(p, tuple) else p[1] pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores = pred_scores.permute(0, 2, 1).contiguous() pred_distri = pred_distri.permute(0, 2, 1).contiguous() pred_distri2, pred_scores2 = torch.cat([xi.view(feats2[0].shape[0], self.no, -1) for xi in feats2], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores2 = pred_scores2.permute(0, 2, 1).contiguous() pred_distri2 = pred_distri2.permute(0, 2, 1).contiguous() dtype = pred_scores.dtype batch_size, grid_size = pred_scores.shape[:2] imgsz = torch.tensor(feats[0].shape[2:], device=self.device, dtype=dtype) * self.stride[0] # image size (h,w) anchor_points, stride_tensor = make_anchors(feats, self.stride, 0.5) # targets targets = self.preprocess(targets, batch_size, scale_tensor=imgsz[[1, 0, 1, 0]]) gt_labels, gt_bboxes = targets.split((1, 4), 2) # cls, xyxy mask_gt = gt_bboxes.sum(2, keepdim=True).gt_(0) # pboxes pred_bboxes = self.bbox_decode(anchor_points, pred_distri) # xyxy, (b, h*w, 4) pred_bboxes2 = self.bbox_decode(anchor_points, pred_distri2) # xyxy, (b, h*w, 4) target_labels, target_bboxes, target_scores, fg_mask = self.assigner( pred_scores.detach().sigmoid(), (pred_bboxes.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_labels2, target_bboxes2, target_scores2, fg_mask2 = self.assigner2( pred_scores2.detach().sigmoid(), (pred_bboxes2.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_bboxes /= stride_tensor target_scores_sum = max(target_scores.sum(), 1) target_bboxes2 /= stride_tensor target_scores_sum2 = max(target_scores2.sum(), 1) # cls loss # loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum # VFL way loss[1] = self.BCEcls(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum # BCE loss[1] *= 0.25 loss[1] += self.BCEcls(pred_scores2, target_scores2.to(dtype)).sum() / target_scores_sum2 # BCE # bbox loss if fg_mask.sum(): loss[0], loss[2], iou = self.bbox_loss(pred_distri, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask) loss[0] *= 0.25 loss[2] *= 0.25 if fg_mask2.sum(): loss0_, loss2_, iou2 = self.bbox_loss2(pred_distri2, pred_bboxes2, anchor_points, target_bboxes2, target_scores2, target_scores_sum2, fg_mask2) loss[0] += loss0_ loss[2] += loss2_ loss[0] *= 7.5 # box gain loss[1] *= 0.5 # cls gain loss[2] *= 1.5 # dfl gain return loss.sum() * batch_size, loss.detach() # loss(box, cls, dfl) class ComputeLossLH: # Compute losses def __init__(self, model, use_dfl=True): device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h["cls_pw"]], device=device), reduction='none') # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get("label_smoothing", 0.0)) # positive, negative BCE targets # Focal loss g = h["fl_gamma"] # focal loss gamma if g > 0: BCEcls = FocalLoss(BCEcls, g) m = de_parallel(model).model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7 self.BCEcls = BCEcls self.hyp = h self.stride = m.stride # model strides self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.no = m.no self.reg_max = m.reg_max self.device = device self.assigner = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.proj = torch.arange(m.reg_max).float().to(device) # / 120.0 self.use_dfl = use_dfl def preprocess(self, targets, batch_size, scale_tensor): if targets.shape[0] == 0: out = torch.zeros(batch_size, 0, 5, device=self.device) else: i = targets[:, 0] # image index _, counts = i.unique(return_counts=True) out = torch.zeros(batch_size, counts.max(), 5, device=self.device) for j in range(batch_size): matches = i == j n = matches.sum() if n: out[j, :n] = targets[matches, 1:] out[..., 1:5] = xywh2xyxy(out[..., 1:5].mul_(scale_tensor)) return out def bbox_decode(self, anchor_points, pred_dist): if self.use_dfl: b, a, c = pred_dist.shape # batch, anchors, channels pred_dist = pred_dist.view(b, a, 4, c // 4).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = pred_dist.view(b, a, c // 4, 4).transpose(2,3).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = (pred_dist.view(b, a, c // 4, 4).softmax(2) * self.proj.type(pred_dist.dtype).view(1, 1, -1, 1)).sum(2) return dist2bbox(pred_dist, anchor_points, xywh=False) def __call__(self, p, targets, img=None, epoch=0): loss = torch.zeros(3, device=self.device) # box, cls, dfl feats = p[1][0] if isinstance(p, tuple) else p[0] feats2 = p[1][1] if isinstance(p, tuple) else p[1] pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores = pred_scores.permute(0, 2, 1).contiguous() pred_distri = pred_distri.permute(0, 2, 1).contiguous() pred_distri2, pred_scores2 = torch.cat([xi.view(feats2[0].shape[0], self.no, -1) for xi in feats2], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores2 = pred_scores2.permute(0, 2, 1).contiguous() pred_distri2 = pred_distri2.permute(0, 2, 1).contiguous() dtype = pred_scores.dtype batch_size, grid_size = pred_scores.shape[:2] imgsz = torch.tensor(feats[0].shape[2:], device=self.device, dtype=dtype) * self.stride[0] # image size (h,w) anchor_points, stride_tensor = make_anchors(feats, self.stride, 0.5) # targets targets = self.preprocess(targets, batch_size, scale_tensor=imgsz[[1, 0, 1, 0]]) gt_labels, gt_bboxes = targets.split((1, 4), 2) # cls, xyxy mask_gt = gt_bboxes.sum(2, keepdim=True).gt_(0) # pboxes pred_bboxes = self.bbox_decode(anchor_points, pred_distri) # xyxy, (b, h*w, 4) pred_bboxes2 = self.bbox_decode(anchor_points, pred_distri2) # xyxy, (b, h*w, 4) target_labels, target_bboxes, target_scores, fg_mask = self.assigner( pred_scores2.detach().sigmoid(), (pred_bboxes2.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_bboxes /= stride_tensor target_scores_sum = target_scores.sum() # cls loss # loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum # VFL way loss[1] = self.BCEcls(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum # BCE loss[1] *= 0.25 loss[1] += self.BCEcls(pred_scores2, target_scores.to(dtype)).sum() / target_scores_sum # BCE # bbox loss if fg_mask.sum(): loss[0], loss[2], iou = self.bbox_loss(pred_distri, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask) loss[0] *= 0.25 loss[2] *= 0.25 if fg_mask.sum(): loss0_, loss2_, iou2 = self.bbox_loss(pred_distri2, pred_bboxes2, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask) loss[0] += loss0_ loss[2] += loss2_ loss[0] *= 7.5 # box gain loss[1] *= 0.5 # cls gain loss[2] *= 1.5 # dfl gain return loss.sum() * batch_size, loss.detach() # loss(box, cls, dfl) ================================================ FILE: asone/detectors/yolov9/yolov9/utils/loss_tal_triple.py ================================================ import os import torch import torch.nn as nn import torch.nn.functional as F from asone.detectors.yolov9.yolov9.utils.general import xywh2xyxy from asone.detectors.yolov9.yolov9.utils.metrics import bbox_iou from asone.detectors.yolov9.yolov9.utils.tal.anchor_generator import dist2bbox, make_anchors, bbox2dist from asone.detectors.yolov9.yolov9.utils.tal.assigner import TaskAlignedAssigner from asone.detectors.yolov9.yolov9.utils.torch_utils import de_parallel def smooth_BCE(eps=0.1): # https://github.com/ultralytics/yolov3/issues/238#issuecomment-598028441 # return positive, negative label smoothing BCE targets return 1.0 - 0.5 * eps, 0.5 * eps class VarifocalLoss(nn.Module): # Varifocal loss by Zhang et al. https://arxiv.org/abs/2008.13367 def __init__(self): super().__init__() def forward(self, pred_score, gt_score, label, alpha=0.75, gamma=2.0): weight = alpha * pred_score.sigmoid().pow(gamma) * (1 - label) + gt_score * label with torch.cuda.amp.autocast(enabled=False): loss = (F.binary_cross_entropy_with_logits(pred_score.float(), gt_score.float(), reduction="none") * weight).sum() return loss class FocalLoss(nn.Module): # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5) def __init__(self, loss_fcn, gamma=1.5, alpha=0.25): super().__init__() self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss() self.gamma = gamma self.alpha = alpha self.reduction = loss_fcn.reduction self.loss_fcn.reduction = "none" # required to apply FL to each element def forward(self, pred, true): loss = self.loss_fcn(pred, true) # p_t = torch.exp(-loss) # loss *= self.alpha * (1.000001 - p_t) ** self.gamma # non-zero power for gradient stability # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py pred_prob = torch.sigmoid(pred) # prob from logits p_t = true * pred_prob + (1 - true) * (1 - pred_prob) alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha) modulating_factor = (1.0 - p_t) ** self.gamma loss *= alpha_factor * modulating_factor if self.reduction == "mean": return loss.mean() elif self.reduction == "sum": return loss.sum() else: # 'none' return loss class BboxLoss(nn.Module): def __init__(self, reg_max, use_dfl=False): super().__init__() self.reg_max = reg_max self.use_dfl = use_dfl def forward(self, pred_dist, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask): # iou loss bbox_mask = fg_mask.unsqueeze(-1).repeat([1, 1, 4]) # (b, h*w, 4) pred_bboxes_pos = torch.masked_select(pred_bboxes, bbox_mask).view(-1, 4) target_bboxes_pos = torch.masked_select(target_bboxes, bbox_mask).view(-1, 4) bbox_weight = torch.masked_select(target_scores.sum(-1), fg_mask).unsqueeze(-1) iou = bbox_iou(pred_bboxes_pos, target_bboxes_pos, xywh=False, CIoU=True) loss_iou = 1.0 - iou loss_iou *= bbox_weight loss_iou = loss_iou.sum() / target_scores_sum # dfl loss if self.use_dfl: dist_mask = fg_mask.unsqueeze(-1).repeat([1, 1, (self.reg_max + 1) * 4]) pred_dist_pos = torch.masked_select(pred_dist, dist_mask).view(-1, 4, self.reg_max + 1) target_ltrb = bbox2dist(anchor_points, target_bboxes, self.reg_max) target_ltrb_pos = torch.masked_select(target_ltrb, bbox_mask).view(-1, 4) loss_dfl = self._df_loss(pred_dist_pos, target_ltrb_pos) * bbox_weight loss_dfl = loss_dfl.sum() / target_scores_sum else: loss_dfl = torch.tensor(0.0).to(pred_dist.device) return loss_iou, loss_dfl, iou def _df_loss(self, pred_dist, target): target_left = target.to(torch.long) target_right = target_left + 1 weight_left = target_right.to(torch.float) - target weight_right = 1 - weight_left loss_left = F.cross_entropy(pred_dist.view(-1, self.reg_max + 1), target_left.view(-1), reduction="none").view( target_left.shape) * weight_left loss_right = F.cross_entropy(pred_dist.view(-1, self.reg_max + 1), target_right.view(-1), reduction="none").view(target_left.shape) * weight_right return (loss_left + loss_right).mean(-1, keepdim=True) class ComputeLoss: # Compute losses def __init__(self, model, use_dfl=True): device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h["cls_pw"]], device=device), reduction='none') # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get("label_smoothing", 0.0)) # positive, negative BCE targets # Focal loss g = h["fl_gamma"] # focal loss gamma if g > 0: BCEcls = FocalLoss(BCEcls, g) m = de_parallel(model).model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7 self.BCEcls = BCEcls self.hyp = h self.stride = m.stride # model strides self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.no = m.no self.reg_max = m.reg_max self.device = device self.assigner = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.assigner2 = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.assigner3 = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.bbox_loss2 = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.bbox_loss3 = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.proj = torch.arange(m.reg_max).float().to(device) # / 120.0 self.use_dfl = use_dfl def preprocess(self, targets, batch_size, scale_tensor): if targets.shape[0] == 0: out = torch.zeros(batch_size, 0, 5, device=self.device) else: i = targets[:, 0] # image index _, counts = i.unique(return_counts=True) out = torch.zeros(batch_size, counts.max(), 5, device=self.device) for j in range(batch_size): matches = i == j n = matches.sum() if n: out[j, :n] = targets[matches, 1:] out[..., 1:5] = xywh2xyxy(out[..., 1:5].mul_(scale_tensor)) return out def bbox_decode(self, anchor_points, pred_dist): if self.use_dfl: b, a, c = pred_dist.shape # batch, anchors, channels pred_dist = pred_dist.view(b, a, 4, c // 4).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = pred_dist.view(b, a, c // 4, 4).transpose(2,3).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = (pred_dist.view(b, a, c // 4, 4).softmax(2) * self.proj.type(pred_dist.dtype).view(1, 1, -1, 1)).sum(2) return dist2bbox(pred_dist, anchor_points, xywh=False) def __call__(self, p, targets, img=None, epoch=0): loss = torch.zeros(3, device=self.device) # box, cls, dfl feats = p[1][0] if isinstance(p, tuple) else p[0] feats2 = p[1][1] if isinstance(p, tuple) else p[1] feats3 = p[1][2] if isinstance(p, tuple) else p[2] pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores = pred_scores.permute(0, 2, 1).contiguous() pred_distri = pred_distri.permute(0, 2, 1).contiguous() pred_distri2, pred_scores2 = torch.cat([xi.view(feats2[0].shape[0], self.no, -1) for xi in feats2], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores2 = pred_scores2.permute(0, 2, 1).contiguous() pred_distri2 = pred_distri2.permute(0, 2, 1).contiguous() pred_distri3, pred_scores3 = torch.cat([xi.view(feats3[0].shape[0], self.no, -1) for xi in feats3], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores3 = pred_scores3.permute(0, 2, 1).contiguous() pred_distri3 = pred_distri3.permute(0, 2, 1).contiguous() dtype = pred_scores.dtype batch_size, grid_size = pred_scores.shape[:2] imgsz = torch.tensor(feats[0].shape[2:], device=self.device, dtype=dtype) * self.stride[0] # image size (h,w) anchor_points, stride_tensor = make_anchors(feats, self.stride, 0.5) # targets targets = self.preprocess(targets, batch_size, scale_tensor=imgsz[[1, 0, 1, 0]]) gt_labels, gt_bboxes = targets.split((1, 4), 2) # cls, xyxy mask_gt = gt_bboxes.sum(2, keepdim=True).gt_(0) # pboxes pred_bboxes = self.bbox_decode(anchor_points, pred_distri) # xyxy, (b, h*w, 4) pred_bboxes2 = self.bbox_decode(anchor_points, pred_distri2) # xyxy, (b, h*w, 4) pred_bboxes3 = self.bbox_decode(anchor_points, pred_distri3) # xyxy, (b, h*w, 4) target_labels, target_bboxes, target_scores, fg_mask = self.assigner( pred_scores.detach().sigmoid(), (pred_bboxes.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_labels2, target_bboxes2, target_scores2, fg_mask2 = self.assigner2( pred_scores2.detach().sigmoid(), (pred_bboxes2.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_labels3, target_bboxes3, target_scores3, fg_mask3 = self.assigner3( pred_scores3.detach().sigmoid(), (pred_bboxes3.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_bboxes /= stride_tensor target_scores_sum = max(target_scores.sum(), 1) target_bboxes2 /= stride_tensor target_scores_sum2 = max(target_scores2.sum(), 1) target_bboxes3 /= stride_tensor target_scores_sum3 = max(target_scores3.sum(), 1) # cls loss # loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum # VFL way loss[1] = 0.25 * self.BCEcls(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum # BCE loss[1] += 0.25 * self.BCEcls(pred_scores2, target_scores2.to(dtype)).sum() / target_scores_sum2 # BCE loss[1] += self.BCEcls(pred_scores3, target_scores3.to(dtype)).sum() / target_scores_sum3 # BCE # bbox loss if fg_mask.sum(): loss[0], loss[2], iou = self.bbox_loss(pred_distri, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask) loss[0] *= 0.25 loss[2] *= 0.25 if fg_mask2.sum(): loss0_, loss2_, iou2 = self.bbox_loss2(pred_distri2, pred_bboxes2, anchor_points, target_bboxes2, target_scores2, target_scores_sum2, fg_mask2) loss[0] += 0.25 * loss0_ loss[2] += 0.25 * loss2_ if fg_mask3.sum(): loss0__, loss2__, iou3 = self.bbox_loss3(pred_distri3, pred_bboxes3, anchor_points, target_bboxes3, target_scores3, target_scores_sum3, fg_mask3) loss[0] += loss0__ loss[2] += loss2__ loss[0] *= 7.5 # box gain loss[1] *= 0.5 # cls gain loss[2] *= 1.5 # dfl gain return loss.sum() * batch_size, loss.detach() # loss(box, cls, dfl) ================================================ FILE: asone/detectors/yolov9/yolov9/utils/metrics.py ================================================ import math import warnings from pathlib import Path import matplotlib.pyplot as plt import numpy as np import torch from asone.detectors.yolov9.yolov9.utils import TryExcept, threaded def fitness(x): # Model fitness as a weighted combination of metrics w = [0.0, 0.0, 0.1, 0.9] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return (x[:, :4] * w).sum(1) def smooth(y, f=0.05): # Box filter of fraction f nf = round(len(y) * f * 2) // 2 + 1 # number of filter elements (must be odd) p = np.ones(nf // 2) # ones padding yp = np.concatenate((p * y[0], y, p * y[-1]), 0) # y padded return np.convolve(yp, np.ones(nf) / nf, mode='valid') # y-smoothed def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names=(), eps=1e-16, prefix=""): """ Compute the average precision, given the recall and precision curves. Source: https://github.com/rafaelpadilla/Object-Detection-Metrics. # Arguments tp: True positives (nparray, nx1 or nx10). conf: Objectness value from 0-1 (nparray). pred_cls: Predicted object classes (nparray). target_cls: True object classes (nparray). plot: Plot precision-recall curve at mAP@0.5 save_dir: Plot save directory # Returns The average precision as computed in py-faster-rcnn. """ # Sort by objectness i = np.argsort(-conf) tp, conf, pred_cls = tp[i], conf[i], pred_cls[i] # Find unique classes unique_classes, nt = np.unique(target_cls, return_counts=True) nc = unique_classes.shape[0] # number of classes, number of detections # Create Precision-Recall curve and compute AP for each class px, py = np.linspace(0, 1, 1000), [] # for plotting ap, p, r = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000)) for ci, c in enumerate(unique_classes): i = pred_cls == c n_l = nt[ci] # number of labels n_p = i.sum() # number of predictions if n_p == 0 or n_l == 0: continue # Accumulate FPs and TPs fpc = (1 - tp[i]).cumsum(0) tpc = tp[i].cumsum(0) # Recall recall = tpc / (n_l + eps) # recall curve r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # negative x, xp because xp decreases # Precision precision = tpc / (tpc + fpc) # precision curve p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1) # p at pr_score # AP from recall-precision curve for j in range(tp.shape[1]): ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j]) if plot and j == 0: py.append(np.interp(px, mrec, mpre)) # precision at mAP@0.5 # Compute F1 (harmonic mean of precision and recall) f1 = 2 * p * r / (p + r + eps) names = [v for k, v in names.items() if k in unique_classes] # list: only classes that have data names = dict(enumerate(names)) # to dict if plot: plot_pr_curve(px, py, ap, Path(save_dir) / f'{prefix}PR_curve.png', names) plot_mc_curve(px, f1, Path(save_dir) / f'{prefix}F1_curve.png', names, ylabel='F1') plot_mc_curve(px, p, Path(save_dir) / f'{prefix}P_curve.png', names, ylabel='Precision') plot_mc_curve(px, r, Path(save_dir) / f'{prefix}R_curve.png', names, ylabel='Recall') i = smooth(f1.mean(0), 0.1).argmax() # max F1 index p, r, f1 = p[:, i], r[:, i], f1[:, i] tp = (r * nt).round() # true positives fp = (tp / (p + eps) - tp).round() # false positives return tp, fp, p, r, f1, ap, unique_classes.astype(int) def compute_ap(recall, precision): """ Compute the average precision, given the recall and precision curves # Arguments recall: The recall curve (list) precision: The precision curve (list) # Returns Average precision, precision curve, recall curve """ # Append sentinel values to beginning and end mrec = np.concatenate(([0.0], recall, [1.0])) mpre = np.concatenate(([1.0], precision, [0.0])) # Compute the precision envelope mpre = np.flip(np.maximum.accumulate(np.flip(mpre))) # Integrate area under curve method = 'interp' # methods: 'continuous', 'interp' if method == 'interp': x = np.linspace(0, 1, 101) # 101-point interp (COCO) ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate else: # 'continuous' i = np.where(mrec[1:] != mrec[:-1])[0] # points where x axis (recall) changes ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve return ap, mpre, mrec class ConfusionMatrix: # Updated version of https://github.com/kaanakan/object_detection_confusion_matrix def __init__(self, nc, conf=0.25, iou_thres=0.45): self.matrix = np.zeros((nc + 1, nc + 1)) self.nc = nc # number of classes self.conf = conf self.iou_thres = iou_thres def process_batch(self, detections, labels): """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Arguments: detections (Array[N, 6]), x1, y1, x2, y2, conf, class labels (Array[M, 5]), class, x1, y1, x2, y2 Returns: None, updates confusion matrix accordingly """ if detections is None: gt_classes = labels.int() for gc in gt_classes: self.matrix[self.nc, gc] += 1 # background FN return detections = detections[detections[:, 4] > self.conf] gt_classes = labels[:, 0].int() detection_classes = detections[:, 5].int() iou = box_iou(labels[:, 1:], detections[:, :4]) x = torch.where(iou > self.iou_thres) if x[0].shape[0]: matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy() if x[0].shape[0] > 1: matches = matches[matches[:, 2].argsort()[::-1]] matches = matches[np.unique(matches[:, 1], return_index=True)[1]] matches = matches[matches[:, 2].argsort()[::-1]] matches = matches[np.unique(matches[:, 0], return_index=True)[1]] else: matches = np.zeros((0, 3)) n = matches.shape[0] > 0 m0, m1, _ = matches.transpose().astype(int) for i, gc in enumerate(gt_classes): j = m0 == i if n and sum(j) == 1: self.matrix[detection_classes[m1[j]], gc] += 1 # correct else: self.matrix[self.nc, gc] += 1 # true background if n: for i, dc in enumerate(detection_classes): if not any(m1 == i): self.matrix[dc, self.nc] += 1 # predicted background def matrix(self): return self.matrix def tp_fp(self): tp = self.matrix.diagonal() # true positives fp = self.matrix.sum(1) - tp # false positives # fn = self.matrix.sum(0) - tp # false negatives (missed detections) return tp[:-1], fp[:-1] # remove background class @TryExcept('WARNING ⚠️ ConfusionMatrix plot failure') def plot(self, normalize=True, save_dir='', names=()): import seaborn as sn array = self.matrix / ((self.matrix.sum(0).reshape(1, -1) + 1E-9) if normalize else 1) # normalize columns array[array < 0.005] = np.nan # don't annotate (would appear as 0.00) fig, ax = plt.subplots(1, 1, figsize=(12, 9), tight_layout=True) nc, nn = self.nc, len(names) # number of classes, names sn.set(font_scale=1.0 if nc < 50 else 0.8) # for label size labels = (0 < nn < 99) and (nn == nc) # apply names to ticklabels ticklabels = (names + ['background']) if labels else "auto" with warnings.catch_warnings(): warnings.simplefilter('ignore') # suppress empty matrix RuntimeWarning: All-NaN slice encountered sn.heatmap(array, ax=ax, annot=nc < 30, annot_kws={ "size": 8}, cmap='Blues', fmt='.2f', square=True, vmin=0.0, xticklabels=ticklabels, yticklabels=ticklabels).set_facecolor((1, 1, 1)) ax.set_ylabel('True') ax.set_ylabel('Predicted') ax.set_title('Confusion Matrix') fig.savefig(Path(save_dir) / 'confusion_matrix.png', dpi=250) plt.close(fig) def print(self): for i in range(self.nc + 1): print(' '.join(map(str, self.matrix[i]))) class WIoU_Scale: ''' monotonous: { None: origin v1 True: monotonic FM v2 False: non-monotonic FM v3 } momentum: The momentum of running mean''' iou_mean = 1. monotonous = False _momentum = 1 - 0.5 ** (1 / 7000) _is_train = True def __init__(self, iou): self.iou = iou self._update(self) @classmethod def _update(cls, self): if cls._is_train: cls.iou_mean = (1 - cls._momentum) * cls.iou_mean + \ cls._momentum * self.iou.detach().mean().item() @classmethod def _scaled_loss(cls, self, gamma=1.9, delta=3): if isinstance(self.monotonous, bool): if self.monotonous: return (self.iou.detach() / self.iou_mean).sqrt() else: beta = self.iou.detach() / self.iou_mean alpha = delta * torch.pow(gamma, beta - delta) return beta / alpha return 1 def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, MDPIoU=False, feat_h=640, feat_w=640, eps=1e-7): # Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4) # Get the coordinates of bounding boxes if xywh: # transform from xywh to xyxy (x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1) w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2 b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_ b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_ else: # x1, y1, x2, y2 = box1 b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, -1) b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, -1) w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps # Intersection area inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \ (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0) # Union Area union = w1 * h1 + w2 * h2 - inter + eps # IoU iou = inter / union if CIoU or DIoU or GIoU: cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1 c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center dist ** 2 if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47 v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2) with torch.no_grad(): alpha = v / (v - iou + (1 + eps)) return iou - (rho2 / c2 + v * alpha) # CIoU return iou - rho2 / c2 # DIoU c_area = cw * ch + eps # convex area return iou - (c_area - union) / c_area # GIoU https://arxiv.org/pdf/1902.09630.pdf elif MDPIoU: d1 = (b2_x1 - b1_x1) ** 2 + (b2_y1 - b1_y1) ** 2 d2 = (b2_x2 - b1_x2) ** 2 + (b2_y2 - b1_y2) ** 2 mpdiou_hw_pow = feat_h ** 2 + feat_w ** 2 return iou - d1 / mpdiou_hw_pow - d2 / mpdiou_hw_pow # MPDIoU return iou # IoU def box_iou(box1, box2, eps=1e-7): # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Arguments: box1 (Tensor[N, 4]) box2 (Tensor[M, 4]) Returns: iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 """ # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2) (a1, a2), (b1, b2) = box1.unsqueeze(1).chunk(2, 2), box2.unsqueeze(0).chunk(2, 2) inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp(0).prod(2) # IoU = inter / (area1 + area2 - inter) return inter / ((a2 - a1).prod(2) + (b2 - b1).prod(2) - inter + eps) def bbox_ioa(box1, box2, eps=1e-7): """Returns the intersection over box2 area given box1, box2. Boxes are x1y1x2y2 box1: np.array of shape(nx4) box2: np.array of shape(mx4) returns: np.array of shape(nxm) """ # Get the coordinates of bounding boxes b1_x1, b1_y1, b1_x2, b1_y2 = box1.T b2_x1, b2_y1, b2_x2, b2_y2 = box2.T # Intersection area inter_area = (np.minimum(b1_x2[:, None], b2_x2) - np.maximum(b1_x1[:, None], b2_x1)).clip(0) * \ (np.minimum(b1_y2[:, None], b2_y2) - np.maximum(b1_y1[:, None], b2_y1)).clip(0) # box2 area box2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + eps # Intersection over box2 area return inter_area / box2_area def wh_iou(wh1, wh2, eps=1e-7): # Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2 wh1 = wh1[:, None] # [N,1,2] wh2 = wh2[None] # [1,M,2] inter = torch.min(wh1, wh2).prod(2) # [N,M] return inter / (wh1.prod(2) + wh2.prod(2) - inter + eps) # iou = inter / (area1 + area2 - inter) # Plots ---------------------------------------------------------------------------------------------------------------- @threaded def plot_pr_curve(px, py, ap, save_dir=Path('pr_curve.png'), names=()): # Precision-recall curve fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) py = np.stack(py, axis=1) if 0 < len(names) < 21: # display per-class legend if < 21 classes for i, y in enumerate(py.T): ax.plot(px, y, linewidth=1, label=f'{names[i]} {ap[i, 0]:.3f}') # plot(recall, precision) else: ax.plot(px, py, linewidth=1, color='grey') # plot(recall, precision) ax.plot(px, py.mean(1), linewidth=3, color='blue', label='all classes %.3f mAP@0.5' % ap[:, 0].mean()) ax.set_xlabel('Recall') ax.set_ylabel('Precision') ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.legend(bbox_to_anchor=(1.04, 1), loc="upper left") ax.set_title('Precision-Recall Curve') fig.savefig(save_dir, dpi=250) plt.close(fig) @threaded def plot_mc_curve(px, py, save_dir=Path('mc_curve.png'), names=(), xlabel='Confidence', ylabel='Metric'): # Metric-confidence curve fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) if 0 < len(names) < 21: # display per-class legend if < 21 classes for i, y in enumerate(py): ax.plot(px, y, linewidth=1, label=f'{names[i]}') # plot(confidence, metric) else: ax.plot(px, py.T, linewidth=1, color='grey') # plot(confidence, metric) y = smooth(py.mean(0), 0.05) ax.plot(px, y, linewidth=3, color='blue', label=f'all classes {y.max():.2f} at {px[y.argmax()]:.3f}') ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.legend(bbox_to_anchor=(1.04, 1), loc="upper left") ax.set_title(f'{ylabel}-Confidence Curve') fig.savefig(save_dir, dpi=250) plt.close(fig) ================================================ FILE: asone/detectors/yolov9/yolov9/utils/plots.py ================================================ import contextlib import math import os from copy import copy from pathlib import Path from urllib.error import URLError import cv2 import matplotlib import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sn import torch from PIL import Image, ImageDraw, ImageFont from asone.detectors.yolov9.yolov9.utils import TryExcept, threaded from asone.detectors.yolov9.yolov9.utils.general import (CONFIG_DIR, FONT, LOGGER, check_font, check_requirements, clip_boxes, increment_path, is_ascii, xywh2xyxy, xyxy2xywh) from asone.detectors.yolov9.yolov9.utils.metrics import fitness from asone.detectors.yolov9.yolov9.utils.segment.general import scale_image # Settings RANK = int(os.getenv('RANK', -1)) matplotlib.rc('font', **{'size': 11}) matplotlib.use('Agg') # for writing to files only class Colors: # Ultralytics color palette https://ultralytics.com/ def __init__(self): # hex = matplotlib.colors.TABLEAU_COLORS.values() hexs = ('FF3838', 'FF9D97', 'FF701F', 'FFB21D', 'CFD231', '48F90A', '92CC17', '3DDB86', '1A9334', '00D4BB', '2C99A8', '00C2FF', '344593', '6473FF', '0018EC', '8438FF', '520085', 'CB38FF', 'FF95C8', 'FF37C7') self.palette = [self.hex2rgb(f'#{c}') for c in hexs] self.n = len(self.palette) def __call__(self, i, bgr=False): c = self.palette[int(i) % self.n] return (c[2], c[1], c[0]) if bgr else c @staticmethod def hex2rgb(h): # rgb order (PIL) return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4)) colors = Colors() # create instance for 'from utils.plots import colors' def check_pil_font(font=FONT, size=10): # Return a PIL TrueType Font, downloading to CONFIG_DIR if necessary font = Path(font) font = font if font.exists() else (CONFIG_DIR / font.name) try: return ImageFont.truetype(str(font) if font.exists() else font.name, size) except Exception: # download if missing try: check_font(font) return ImageFont.truetype(str(font), size) except TypeError: check_requirements('Pillow>=8.4.0') # known issue https://github.com/ultralytics/yolov5/issues/5374 except URLError: # not online return ImageFont.load_default() class Annotator: # YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc'): assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.' non_ascii = not is_ascii(example) # non-latin labels, i.e. asian, arabic, cyrillic self.pil = pil or non_ascii if self.pil: # use PIL self.im = im if isinstance(im, Image.Image) else Image.fromarray(im) self.draw = ImageDraw.Draw(self.im) self.font = check_pil_font(font='Arial.Unicode.ttf' if non_ascii else font, size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12)) else: # use cv2 self.im = im self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2) # line width def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)): # Add one xyxy box to image with label if self.pil or not is_ascii(label): self.draw.rectangle(box, width=self.lw, outline=color) # box if label: w, h = self.font.getsize(label) # text width, height outside = box[1] - h >= 0 # label fits outside box self.draw.rectangle( (box[0], box[1] - h if outside else box[1], box[0] + w + 1, box[1] + 1 if outside else box[1] + h + 1), fill=color, ) # self.draw.text((box[0], box[1]), label, fill=txt_color, font=self.font, anchor='ls') # for PIL>8.0 self.draw.text((box[0], box[1] - h if outside else box[1]), label, fill=txt_color, font=self.font) else: # cv2 p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3])) cv2.rectangle(self.im, p1, p2, color, thickness=self.lw, lineType=cv2.LINE_AA) if label: tf = max(self.lw - 1, 1) # font thickness w, h = cv2.getTextSize(label, 0, fontScale=self.lw / 3, thickness=tf)[0] # text width, height outside = p1[1] - h >= 3 p2 = p1[0] + w, p1[1] - h - 3 if outside else p1[1] + h + 3 cv2.rectangle(self.im, p1, p2, color, -1, cv2.LINE_AA) # filled cv2.putText(self.im, label, (p1[0], p1[1] - 2 if outside else p1[1] + h + 2), 0, self.lw / 3, txt_color, thickness=tf, lineType=cv2.LINE_AA) def masks(self, masks, colors, im_gpu=None, alpha=0.5): """Plot masks at once. Args: masks (tensor): predicted masks on cuda, shape: [n, h, w] colors (List[List[Int]]): colors for predicted masks, [[r, g, b] * n] im_gpu (tensor): img is in cuda, shape: [3, h, w], range: [0, 1] alpha (float): mask transparency: 0.0 fully transparent, 1.0 opaque """ if self.pil: # convert to numpy first self.im = np.asarray(self.im).copy() if im_gpu is None: # Add multiple masks of shape(h,w,n) with colors list([r,g,b], [r,g,b], ...) if len(masks) == 0: return if isinstance(masks, torch.Tensor): masks = torch.as_tensor(masks, dtype=torch.uint8) masks = masks.permute(1, 2, 0).contiguous() masks = masks.cpu().numpy() # masks = np.ascontiguousarray(masks.transpose(1, 2, 0)) masks = scale_image(masks.shape[:2], masks, self.im.shape) masks = np.asarray(masks, dtype=np.float32) colors = np.asarray(colors, dtype=np.float32) # shape(n,3) s = masks.sum(2, keepdims=True).clip(0, 1) # add all masks together masks = (masks @ colors).clip(0, 255) # (h,w,n) @ (n,3) = (h,w,3) self.im[:] = masks * alpha + self.im * (1 - s * alpha) else: if len(masks) == 0: self.im[:] = im_gpu.permute(1, 2, 0).contiguous().cpu().numpy() * 255 colors = torch.tensor(colors, device=im_gpu.device, dtype=torch.float32) / 255.0 colors = colors[:, None, None] # shape(n,1,1,3) masks = masks.unsqueeze(3) # shape(n,h,w,1) masks_color = masks * (colors * alpha) # shape(n,h,w,3) inv_alph_masks = (1 - masks * alpha).cumprod(0) # shape(n,h,w,1) mcs = (masks_color * inv_alph_masks).sum(0) * 2 # mask color summand shape(n,h,w,3) im_gpu = im_gpu.flip(dims=[0]) # flip channel im_gpu = im_gpu.permute(1, 2, 0).contiguous() # shape(h,w,3) im_gpu = im_gpu * inv_alph_masks[-1] + mcs im_mask = (im_gpu * 255).byte().cpu().numpy() self.im[:] = scale_image(im_gpu.shape, im_mask, self.im.shape) if self.pil: # convert im back to PIL and update draw self.fromarray(self.im) def rectangle(self, xy, fill=None, outline=None, width=1): # Add rectangle to image (PIL-only) self.draw.rectangle(xy, fill, outline, width) def text(self, xy, text, txt_color=(255, 255, 255), anchor='top'): # Add text to image (PIL-only) if anchor == 'bottom': # start y from font bottom w, h = self.font.getsize(text) # text width, height xy[1] += 1 - h self.draw.text(xy, text, fill=txt_color, font=self.font) def fromarray(self, im): # Update self.im from a numpy array self.im = im if isinstance(im, Image.Image) else Image.fromarray(im) self.draw = ImageDraw.Draw(self.im) def result(self): # Return annotated image as array return np.asarray(self.im) def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detect/exp')): """ x: Features to be visualized module_type: Module type stage: Module stage within model n: Maximum number of feature maps to plot save_dir: Directory to save results """ if 'Detect' not in module_type: batch, channels, height, width = x.shape # batch, channels, height, width if height > 1 and width > 1: f = save_dir / f"stage{stage}_{module_type.split('.')[-1]}_features.png" # filename blocks = torch.chunk(x[0].cpu(), channels, dim=0) # select batch index 0, block by channels n = min(n, channels) # number of plots fig, ax = plt.subplots(math.ceil(n / 8), 8, tight_layout=True) # 8 rows x n/8 cols ax = ax.ravel() plt.subplots_adjust(wspace=0.05, hspace=0.05) for i in range(n): ax[i].imshow(blocks[i].squeeze()) # cmap='gray' ax[i].axis('off') LOGGER.info(f'Saving {f}... ({n}/{channels})') plt.savefig(f, dpi=300, bbox_inches='tight') plt.close() np.save(str(f.with_suffix('.npy')), x[0].cpu().numpy()) # npy save def hist2d(x, y, n=100): # 2d histogram used in labels.png and evolve.png xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n) hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges)) xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1) yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1) return np.log(hist[xidx, yidx]) def butter_lowpass_filtfilt(data, cutoff=1500, fs=50000, order=5): from scipy.signal import butter, filtfilt # https://stackoverflow.com/questions/28536191/how-to-filter-smooth-with-scipy-numpy def butter_lowpass(cutoff, fs, order): nyq = 0.5 * fs normal_cutoff = cutoff / nyq return butter(order, normal_cutoff, btype='low', analog=False) b, a = butter_lowpass(cutoff, fs, order=order) return filtfilt(b, a, data) # forward-backward filter def output_to_target(output, max_det=300): # Convert model output to target format [batch_id, class_id, x, y, w, h, conf] for plotting targets = [] for i, o in enumerate(output): box, conf, cls = o[:max_det, :6].cpu().split((4, 1, 1), 1) j = torch.full((conf.shape[0], 1), i) targets.append(torch.cat((j, cls, xyxy2xywh(box), conf), 1)) return torch.cat(targets, 0).numpy() @threaded def plot_images(images, targets, paths=None, fname='images.jpg', names=None): # Plot image grid with labels if isinstance(images, torch.Tensor): images = images.cpu().float().numpy() if isinstance(targets, torch.Tensor): targets = targets.cpu().numpy() max_size = 1920 # max image size max_subplots = 16 # max image subplots, i.e. 4x4 bs, _, h, w = images.shape # batch size, _, height, width bs = min(bs, max_subplots) # limit plot images ns = np.ceil(bs ** 0.5) # number of subplots (square) if np.max(images[0]) <= 1: images *= 255 # de-normalise (optional) # Build Image mosaic = np.full((int(ns * h), int(ns * w), 3), 255, dtype=np.uint8) # init for i, im in enumerate(images): if i == max_subplots: # if last batch has fewer images than we expect break x, y = int(w * (i // ns)), int(h * (i % ns)) # block origin im = im.transpose(1, 2, 0) mosaic[y:y + h, x:x + w, :] = im # Resize (optional) scale = max_size / ns / max(h, w) if scale < 1: h = math.ceil(scale * h) w = math.ceil(scale * w) mosaic = cv2.resize(mosaic, tuple(int(x * ns) for x in (w, h))) # Annotate fs = int((h + w) * ns * 0.01) # font size annotator = Annotator(mosaic, line_width=round(fs / 10), font_size=fs, pil=True, example=names) for i in range(i + 1): x, y = int(w * (i // ns)), int(h * (i % ns)) # block origin annotator.rectangle([x, y, x + w, y + h], None, (255, 255, 255), width=2) # borders if paths: annotator.text((x + 5, y + 5), text=Path(paths[i]).name[:40], txt_color=(220, 220, 220)) # filenames if len(targets) > 0: ti = targets[targets[:, 0] == i] # image targets boxes = xywh2xyxy(ti[:, 2:6]).T classes = ti[:, 1].astype('int') labels = ti.shape[1] == 6 # labels if no conf column conf = None if labels else ti[:, 6] # check for confidence presence (label vs pred) if boxes.shape[1]: if boxes.max() <= 1.01: # if normalized with tolerance 0.01 boxes[[0, 2]] *= w # scale to pixels boxes[[1, 3]] *= h elif scale < 1: # absolute coords need scale if image scales boxes *= scale boxes[[0, 2]] += x boxes[[1, 3]] += y for j, box in enumerate(boxes.T.tolist()): cls = classes[j] color = colors(cls) cls = names[cls] if names else cls if labels or conf[j] > 0.25: # 0.25 conf thresh label = f'{cls}' if labels else f'{cls} {conf[j]:.1f}' annotator.box_label(box, label, color=color) annotator.im.save(fname) # save def plot_lr_scheduler(optimizer, scheduler, epochs=300, save_dir=''): # Plot LR simulating training for full epochs optimizer, scheduler = copy(optimizer), copy(scheduler) # do not modify originals y = [] for _ in range(epochs): scheduler.step() y.append(optimizer.param_groups[0]['lr']) plt.plot(y, '.-', label='LR') plt.xlabel('epoch') plt.ylabel('LR') plt.grid() plt.xlim(0, epochs) plt.ylim(0) plt.savefig(Path(save_dir) / 'LR.png', dpi=200) plt.close() def plot_val_txt(): # from utils.plots import *; plot_val() # Plot val.txt histograms x = np.loadtxt('val.txt', dtype=np.float32) box = xyxy2xywh(x[:, :4]) cx, cy = box[:, 0], box[:, 1] fig, ax = plt.subplots(1, 1, figsize=(6, 6), tight_layout=True) ax.hist2d(cx, cy, bins=600, cmax=10, cmin=0) ax.set_aspect('equal') plt.savefig('hist2d.png', dpi=300) fig, ax = plt.subplots(1, 2, figsize=(12, 6), tight_layout=True) ax[0].hist(cx, bins=600) ax[1].hist(cy, bins=600) plt.savefig('hist1d.png', dpi=200) def plot_targets_txt(): # from utils.plots import *; plot_targets_txt() # Plot targets.txt histograms x = np.loadtxt('targets.txt', dtype=np.float32).T s = ['x targets', 'y targets', 'width targets', 'height targets'] fig, ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True) ax = ax.ravel() for i in range(4): ax[i].hist(x[i], bins=100, label=f'{x[i].mean():.3g} +/- {x[i].std():.3g}') ax[i].legend() ax[i].set_title(s[i]) plt.savefig('targets.jpg', dpi=200) def plot_val_study(file='', dir='', x=None): # from utils.plots import *; plot_val_study() # Plot file=study.txt generated by val.py (or plot all study*.txt in dir) save_dir = Path(file).parent if file else Path(dir) plot2 = False # plot additional results if plot2: ax = plt.subplots(2, 4, figsize=(10, 6), tight_layout=True)[1].ravel() fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4), tight_layout=True) # for f in [save_dir / f'study_coco_{x}.txt' for x in ['yolov5n6', 'yolov5s6', 'yolov5m6', 'yolov5l6', 'yolov5x6']]: for f in sorted(save_dir.glob('study*.txt')): y = np.loadtxt(f, dtype=np.float32, usecols=[0, 1, 2, 3, 7, 8, 9], ndmin=2).T x = np.arange(y.shape[1]) if x is None else np.array(x) if plot2: s = ['P', 'R', 'mAP@.5', 'mAP@.5:.95', 't_preprocess (ms/img)', 't_inference (ms/img)', 't_NMS (ms/img)'] for i in range(7): ax[i].plot(x, y[i], '.-', linewidth=2, markersize=8) ax[i].set_title(s[i]) j = y[3].argmax() + 1 ax2.plot(y[5, 1:j], y[3, 1:j] * 1E2, '.-', linewidth=2, markersize=8, label=f.stem.replace('study_coco_', '').replace('yolo', 'YOLO')) ax2.plot(1E3 / np.array([209, 140, 97, 58, 35, 18]), [34.6, 40.5, 43.0, 47.5, 49.7, 51.5], 'k.-', linewidth=2, markersize=8, alpha=.25, label='EfficientDet') ax2.grid(alpha=0.2) ax2.set_yticks(np.arange(20, 60, 5)) ax2.set_xlim(0, 57) ax2.set_ylim(25, 55) ax2.set_xlabel('GPU Speed (ms/img)') ax2.set_ylabel('COCO AP val') ax2.legend(loc='lower right') f = save_dir / 'study.png' print(f'Saving {f}...') plt.savefig(f, dpi=300) @TryExcept() # known issue https://github.com/ultralytics/yolov5/issues/5395 def plot_labels(labels, names=(), save_dir=Path('')): # plot dataset labels LOGGER.info(f"Plotting labels to {save_dir / 'labels.jpg'}... ") c, b = labels[:, 0], labels[:, 1:].transpose() # classes, boxes nc = int(c.max() + 1) # number of classes x = pd.DataFrame(b.transpose(), columns=['x', 'y', 'width', 'height']) # seaborn correlogram sn.pairplot(x, corner=True, diag_kind='auto', kind='hist', diag_kws=dict(bins=50), plot_kws=dict(pmax=0.9)) plt.savefig(save_dir / 'labels_correlogram.jpg', dpi=200) plt.close() # matplotlib labels matplotlib.use('svg') # faster ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True)[1].ravel() y = ax[0].hist(c, bins=np.linspace(0, nc, nc + 1) - 0.5, rwidth=0.8) with contextlib.suppress(Exception): # color histogram bars by class [y[2].patches[i].set_color([x / 255 for x in colors(i)]) for i in range(nc)] # known issue #3195 ax[0].set_ylabel('instances') if 0 < len(names) < 30: ax[0].set_xticks(range(len(names))) ax[0].set_xticklabels(list(names.values()), rotation=90, fontsize=10) else: ax[0].set_xlabel('classes') sn.histplot(x, x='x', y='y', ax=ax[2], bins=50, pmax=0.9) sn.histplot(x, x='width', y='height', ax=ax[3], bins=50, pmax=0.9) # rectangles labels[:, 1:3] = 0.5 # center labels[:, 1:] = xywh2xyxy(labels[:, 1:]) * 2000 img = Image.fromarray(np.ones((2000, 2000, 3), dtype=np.uint8) * 255) for cls, *box in labels[:1000]: ImageDraw.Draw(img).rectangle(box, width=1, outline=colors(cls)) # plot ax[1].imshow(img) ax[1].axis('off') for a in [0, 1, 2, 3]: for s in ['top', 'right', 'left', 'bottom']: ax[a].spines[s].set_visible(False) plt.savefig(save_dir / 'labels.jpg', dpi=200) matplotlib.use('Agg') plt.close() def imshow_cls(im, labels=None, pred=None, names=None, nmax=25, verbose=False, f=Path('images.jpg')): # Show classification image grid with labels (optional) and predictions (optional) from asone.detectors.yolov9.yolov9.utils.augmentations import denormalize names = names or [f'class{i}' for i in range(1000)] blocks = torch.chunk(denormalize(im.clone()).cpu().float(), len(im), dim=0) # select batch index 0, block by channels n = min(len(blocks), nmax) # number of plots m = min(8, round(n ** 0.5)) # 8 x 8 default fig, ax = plt.subplots(math.ceil(n / m), m) # 8 rows x n/8 cols ax = ax.ravel() if m > 1 else [ax] # plt.subplots_adjust(wspace=0.05, hspace=0.05) for i in range(n): ax[i].imshow(blocks[i].squeeze().permute((1, 2, 0)).numpy().clip(0.0, 1.0)) ax[i].axis('off') if labels is not None: s = names[labels[i]] + (f'—{names[pred[i]]}' if pred is not None else '') ax[i].set_title(s, fontsize=8, verticalalignment='top') plt.savefig(f, dpi=300, bbox_inches='tight') plt.close() if verbose: LOGGER.info(f"Saving {f}") if labels is not None: LOGGER.info('True: ' + ' '.join(f'{names[i]:3s}' for i in labels[:nmax])) if pred is not None: LOGGER.info('Predicted:' + ' '.join(f'{names[i]:3s}' for i in pred[:nmax])) return f def plot_evolve(evolve_csv='path/to/evolve.csv'): # from utils.plots import *; plot_evolve() # Plot evolve.csv hyp evolution results evolve_csv = Path(evolve_csv) data = pd.read_csv(evolve_csv) keys = [x.strip() for x in data.columns] x = data.values f = fitness(x) j = np.argmax(f) # max fitness index plt.figure(figsize=(10, 12), tight_layout=True) matplotlib.rc('font', **{'size': 8}) print(f'Best results from row {j} of {evolve_csv}:') for i, k in enumerate(keys[7:]): v = x[:, 7 + i] mu = v[j] # best single result plt.subplot(6, 5, i + 1) plt.scatter(v, f, c=hist2d(v, f, 20), cmap='viridis', alpha=.8, edgecolors='none') plt.plot(mu, f.max(), 'k+', markersize=15) plt.title(f'{k} = {mu:.3g}', fontdict={'size': 9}) # limit to 40 characters if i % 5 != 0: plt.yticks([]) print(f'{k:>15}: {mu:.3g}') f = evolve_csv.with_suffix('.png') # filename plt.savefig(f, dpi=200) plt.close() print(f'Saved {f}') def plot_results(file='path/to/results.csv', dir=''): # Plot training results.csv. Usage: from utils.plots import *; plot_results('path/to/results.csv') save_dir = Path(file).parent if file else Path(dir) fig, ax = plt.subplots(2, 5, figsize=(12, 6), tight_layout=True) ax = ax.ravel() files = list(save_dir.glob('results*.csv')) assert len(files), f'No results.csv files found in {save_dir.resolve()}, nothing to plot.' for f in files: try: data = pd.read_csv(f) s = [x.strip() for x in data.columns] x = data.values[:, 0] for i, j in enumerate([1, 2, 3, 4, 5, 8, 9, 10, 6, 7]): y = data.values[:, j].astype('float') # y[y == 0] = np.nan # don't show zero values ax[i].plot(x, y, marker='.', label=f.stem, linewidth=2, markersize=8) ax[i].set_title(s[j], fontsize=12) # if j in [8, 9, 10]: # share train and val loss y axes # ax[i].get_shared_y_axes().join(ax[i], ax[i - 5]) except Exception as e: LOGGER.info(f'Warning: Plotting error for {f}: {e}') ax[1].legend() fig.savefig(save_dir / 'results.png', dpi=200) plt.close() def profile_idetection(start=0, stop=0, labels=(), save_dir=''): # Plot iDetection '*.txt' per-image logs. from utils.plots import *; profile_idetection() ax = plt.subplots(2, 4, figsize=(12, 6), tight_layout=True)[1].ravel() s = ['Images', 'Free Storage (GB)', 'RAM Usage (GB)', 'Battery', 'dt_raw (ms)', 'dt_smooth (ms)', 'real-world FPS'] files = list(Path(save_dir).glob('frames*.txt')) for fi, f in enumerate(files): try: results = np.loadtxt(f, ndmin=2).T[:, 90:-30] # clip first and last rows n = results.shape[1] # number of rows x = np.arange(start, min(stop, n) if stop else n) results = results[:, x] t = (results[0] - results[0].min()) # set t0=0s results[0] = x for i, a in enumerate(ax): if i < len(results): label = labels[fi] if len(labels) else f.stem.replace('frames_', '') a.plot(t, results[i], marker='.', label=label, linewidth=1, markersize=5) a.set_title(s[i]) a.set_xlabel('time (s)') # if fi == len(files) - 1: # a.set_ylim(bottom=0) for side in ['top', 'right']: a.spines[side].set_visible(False) else: a.remove() except Exception as e: print(f'Warning: Plotting error for {f}; {e}') ax[1].legend() plt.savefig(Path(save_dir) / 'idetection_profile.png', dpi=200) def save_one_box(xyxy, im, file=Path('im.jpg'), gain=1.02, pad=10, square=False, BGR=False, save=True): # Save image crop as {file} with crop size multiple {gain} and {pad} pixels. Save and/or return crop xyxy = torch.tensor(xyxy).view(-1, 4) b = xyxy2xywh(xyxy) # boxes if square: b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1) # attempt rectangle to square b[:, 2:] = b[:, 2:] * gain + pad # box wh * gain + pad xyxy = xywh2xyxy(b).long() clip_boxes(xyxy, im.shape) crop = im[int(xyxy[0, 1]):int(xyxy[0, 3]), int(xyxy[0, 0]):int(xyxy[0, 2]), ::(1 if BGR else -1)] if save: file.parent.mkdir(parents=True, exist_ok=True) # make directory f = str(increment_path(file).with_suffix('.jpg')) # cv2.imwrite(f, crop) # save BGR, https://github.com/ultralytics/yolov5/issues/7007 chroma subsampling issue Image.fromarray(crop[..., ::-1]).save(f, quality=95, subsampling=0) # save RGB return crop ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/__init__.py ================================================ # init ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/augmentations.py ================================================ import math import random import cv2 import numpy as np from ..augmentations import box_candidates from ..general import resample_segments, segment2box def mixup(im, labels, segments, im2, labels2, segments2): # Applies MixUp augmentation https://arxiv.org/pdf/1710.09412.pdf r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0 im = (im * r + im2 * (1 - r)).astype(np.uint8) labels = np.concatenate((labels, labels2), 0) segments = np.concatenate((segments, segments2), 0) return im, labels, segments def random_perspective(im, targets=(), segments=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0, border=(0, 0)): # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10)) # targets = [cls, xyxy] height = im.shape[0] + border[0] * 2 # shape(h,w,c) width = im.shape[1] + border[1] * 2 # Center C = np.eye(3) C[0, 2] = -im.shape[1] / 2 # x translation (pixels) C[1, 2] = -im.shape[0] / 2 # y translation (pixels) # Perspective P = np.eye(3) P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y) P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x) # Rotation and Scale R = np.eye(3) a = random.uniform(-degrees, degrees) # a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations s = random.uniform(1 - scale, 1 + scale) # s = 2 ** random.uniform(-scale, scale) R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s) # Shear S = np.eye(3) S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg) S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg) # Translation T = np.eye(3) T[0, 2] = (random.uniform(0.5 - translate, 0.5 + translate) * width) # x translation (pixels) T[1, 2] = (random.uniform(0.5 - translate, 0.5 + translate) * height) # y translation (pixels) # Combined rotation matrix M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed if perspective: im = cv2.warpPerspective(im, M, dsize=(width, height), borderValue=(114, 114, 114)) else: # affine im = cv2.warpAffine(im, M[:2], dsize=(width, height), borderValue=(114, 114, 114)) # Visualize # import matplotlib.pyplot as plt # ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel() # ax[0].imshow(im[:, :, ::-1]) # base # ax[1].imshow(im2[:, :, ::-1]) # warped # Transform label coordinates n = len(targets) new_segments = [] if n: new = np.zeros((n, 4)) segments = resample_segments(segments) # upsample for i, segment in enumerate(segments): xy = np.ones((len(segment), 3)) xy[:, :2] = segment xy = xy @ M.T # transform xy = (xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]) # perspective rescale or affine # clip new[i] = segment2box(xy, width, height) new_segments.append(xy) # filter candidates i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01) targets = targets[i] targets[:, 1:5] = new[i] new_segments = np.array(new_segments)[i] return im, targets, new_segments ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/dataloaders.py ================================================ import os import random import cv2 import numpy as np import torch from torch.utils.data import DataLoader, distributed from ..augmentations import augment_hsv, copy_paste, letterbox from ..dataloaders import InfiniteDataLoader, LoadImagesAndLabels, seed_worker from ..general import LOGGER, xyn2xy, xywhn2xyxy, xyxy2xywhn from ..torch_utils import torch_distributed_zero_first from .augmentations import mixup, random_perspective RANK = int(os.getenv('RANK', -1)) def create_dataloader(path, imgsz, batch_size, stride, single_cls=False, hyp=None, augment=False, cache=False, pad=0.0, rect=False, rank=-1, workers=8, image_weights=False, close_mosaic=False, quad=False, prefix='', shuffle=False, mask_downsample_ratio=1, overlap_mask=False): if rect and shuffle: LOGGER.warning('WARNING ⚠️ --rect is incompatible with DataLoader shuffle, setting shuffle=False') shuffle = False with torch_distributed_zero_first(rank): # init dataset *.cache only once if DDP dataset = LoadImagesAndLabelsAndMasks( path, imgsz, batch_size, augment=augment, # augmentation hyp=hyp, # hyperparameters rect=rect, # rectangular batches cache_images=cache, single_cls=single_cls, stride=int(stride), pad=pad, image_weights=image_weights, prefix=prefix, downsample_ratio=mask_downsample_ratio, overlap=overlap_mask) batch_size = min(batch_size, len(dataset)) nd = torch.cuda.device_count() # number of CUDA devices nw = min([os.cpu_count() // max(nd, 1), batch_size if batch_size > 1 else 0, workers]) # number of workers sampler = None if rank == -1 else distributed.DistributedSampler(dataset, shuffle=shuffle) #loader = DataLoader if image_weights else InfiniteDataLoader # only DataLoader allows for attribute updates loader = DataLoader if image_weights or close_mosaic else InfiniteDataLoader generator = torch.Generator() generator.manual_seed(6148914691236517205 + RANK) return loader( dataset, batch_size=batch_size, shuffle=shuffle and sampler is None, num_workers=nw, sampler=sampler, pin_memory=True, collate_fn=LoadImagesAndLabelsAndMasks.collate_fn4 if quad else LoadImagesAndLabelsAndMasks.collate_fn, worker_init_fn=seed_worker, generator=generator, ), dataset class LoadImagesAndLabelsAndMasks(LoadImagesAndLabels): # for training/testing def __init__( self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, cache_images=False, single_cls=False, stride=32, pad=0, min_items=0, prefix="", downsample_ratio=1, overlap=False, ): super().__init__(path, img_size, batch_size, augment, hyp, rect, image_weights, cache_images, single_cls, stride, pad, min_items, prefix) self.downsample_ratio = downsample_ratio self.overlap = overlap def __getitem__(self, index): index = self.indices[index] # linear, shuffled, or image_weights hyp = self.hyp mosaic = self.mosaic and random.random() < hyp['mosaic'] masks = [] if mosaic: # Load mosaic img, labels, segments = self.load_mosaic(index) shapes = None # MixUp augmentation if random.random() < hyp["mixup"]: img, labels, segments = mixup(img, labels, segments, *self.load_mosaic(random.randint(0, self.n - 1))) else: # Load image img, (h0, w0), (h, w) = self.load_image(index) # Letterbox shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment) shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling labels = self.labels[index].copy() # [array, array, ....], array.shape=(num_points, 2), xyxyxyxy segments = self.segments[index].copy() if len(segments): for i_s in range(len(segments)): segments[i_s] = xyn2xy( segments[i_s], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1], ) if labels.size: # normalized xywh to pixel xyxy format labels[:, 1:] = xywhn2xyxy(labels[:, 1:], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1]) if self.augment: img, labels, segments = random_perspective(img, labels, segments=segments, degrees=hyp["degrees"], translate=hyp["translate"], scale=hyp["scale"], shear=hyp["shear"], perspective=hyp["perspective"]) nl = len(labels) # number of labels if nl: labels[:, 1:5] = xyxy2xywhn(labels[:, 1:5], w=img.shape[1], h=img.shape[0], clip=True, eps=1e-3) if self.overlap: masks, sorted_idx = polygons2masks_overlap(img.shape[:2], segments, downsample_ratio=self.downsample_ratio) masks = masks[None] # (640, 640) -> (1, 640, 640) labels = labels[sorted_idx] else: masks = polygons2masks(img.shape[:2], segments, color=1, downsample_ratio=self.downsample_ratio) masks = (torch.from_numpy(masks) if len(masks) else torch.zeros(1 if self.overlap else nl, img.shape[0] // self.downsample_ratio, img.shape[1] // self.downsample_ratio)) # TODO: albumentations support if self.augment: # Albumentations # there are some augmentation that won't change boxes and masks, # so just be it for now. img, labels = self.albumentations(img, labels) nl = len(labels) # update after albumentations # HSV color-space augment_hsv(img, hgain=hyp["hsv_h"], sgain=hyp["hsv_s"], vgain=hyp["hsv_v"]) # Flip up-down if random.random() < hyp["flipud"]: img = np.flipud(img) if nl: labels[:, 2] = 1 - labels[:, 2] masks = torch.flip(masks, dims=[1]) # Flip left-right if random.random() < hyp["fliplr"]: img = np.fliplr(img) if nl: labels[:, 1] = 1 - labels[:, 1] masks = torch.flip(masks, dims=[2]) # Cutouts # labels = cutout(img, labels, p=0.5) labels_out = torch.zeros((nl, 6)) if nl: labels_out[:, 1:] = torch.from_numpy(labels) # Convert img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB img = np.ascontiguousarray(img) return (torch.from_numpy(img), labels_out, self.im_files[index], shapes, masks) def load_mosaic(self, index): # YOLOv5 4-mosaic loader. Loads 1 image + 3 random images into a 4-image mosaic labels4, segments4 = [], [] s = self.img_size yc, xc = (int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border) # mosaic center x, y # 3 additional image indices indices = [index] + random.choices(self.indices, k=3) # 3 additional image indices for i, index in enumerate(indices): # Load image img, _, (h, w) = self.load_image(index) # place img in img4 if i == 0: # top left img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image) x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image) elif i == 1: # top right x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h elif i == 2: # bottom left x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h) x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h) elif i == 3: # bottom right x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h) x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h) img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax] padw = x1a - x1b padh = y1a - y1b labels, segments = self.labels[index].copy(), self.segments[index].copy() if labels.size: labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format segments = [xyn2xy(x, w, h, padw, padh) for x in segments] labels4.append(labels) segments4.extend(segments) # Concat/clip labels labels4 = np.concatenate(labels4, 0) for x in (labels4[:, 1:], *segments4): np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective() # img4, labels4 = replicate(img4, labels4) # replicate # Augment img4, labels4, segments4 = copy_paste(img4, labels4, segments4, p=self.hyp["copy_paste"]) img4, labels4, segments4 = random_perspective(img4, labels4, segments4, degrees=self.hyp["degrees"], translate=self.hyp["translate"], scale=self.hyp["scale"], shear=self.hyp["shear"], perspective=self.hyp["perspective"], border=self.mosaic_border) # border to remove return img4, labels4, segments4 @staticmethod def collate_fn(batch): img, label, path, shapes, masks = zip(*batch) # transposed batched_masks = torch.cat(masks, 0) for i, l in enumerate(label): l[:, 0] = i # add target image index for build_targets() return torch.stack(img, 0), torch.cat(label, 0), path, shapes, batched_masks def polygon2mask(img_size, polygons, color=1, downsample_ratio=1): """ Args: img_size (tuple): The image size. polygons (np.ndarray): [N, M], N is the number of polygons, M is the number of points(Be divided by 2). """ mask = np.zeros(img_size, dtype=np.uint8) polygons = np.asarray(polygons) polygons = polygons.astype(np.int32) shape = polygons.shape polygons = polygons.reshape(shape[0], -1, 2) cv2.fillPoly(mask, polygons, color=color) nh, nw = (img_size[0] // downsample_ratio, img_size[1] // downsample_ratio) # NOTE: fillPoly firstly then resize is trying the keep the same way # of loss calculation when mask-ratio=1. mask = cv2.resize(mask, (nw, nh)) return mask def polygons2masks(img_size, polygons, color, downsample_ratio=1): """ Args: img_size (tuple): The image size. polygons (list[np.ndarray]): each polygon is [N, M], N is the number of polygons, M is the number of points(Be divided by 2). """ masks = [] for si in range(len(polygons)): mask = polygon2mask(img_size, [polygons[si].reshape(-1)], color, downsample_ratio) masks.append(mask) return np.array(masks) def polygons2masks_overlap(img_size, segments, downsample_ratio=1): """Return a (640, 640) overlap mask.""" masks = np.zeros((img_size[0] // downsample_ratio, img_size[1] // downsample_ratio), dtype=np.int32 if len(segments) > 255 else np.uint8) areas = [] ms = [] for si in range(len(segments)): mask = polygon2mask( img_size, [segments[si].reshape(-1)], downsample_ratio=downsample_ratio, color=1, ) ms.append(mask) areas.append(mask.sum()) areas = np.asarray(areas) index = np.argsort(-areas) ms = np.array(ms)[index] for i in range(len(segments)): mask = ms[i] * (i + 1) masks = masks + mask masks = np.clip(masks, a_min=0, a_max=i + 1) return masks, index ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/general.py ================================================ import cv2 import numpy as np import torch import torch.nn.functional as F def crop_mask(masks, boxes): """ "Crop" predicted masks by zeroing out everything not in the predicted bbox. Vectorized by Chong (thanks Chong). Args: - masks should be a size [h, w, n] tensor of masks - boxes should be a size [n, 4] tensor of bbox coords in relative point form """ n, h, w = masks.shape x1, y1, x2, y2 = torch.chunk(boxes[:, :, None], 4, 1) # x1 shape(1,1,n) r = torch.arange(w, device=masks.device, dtype=x1.dtype)[None, None, :] # rows shape(1,w,1) c = torch.arange(h, device=masks.device, dtype=x1.dtype)[None, :, None] # cols shape(h,1,1) return masks * ((r >= x1) * (r < x2) * (c >= y1) * (c < y2)) def process_mask_upsample(protos, masks_in, bboxes, shape): """ Crop after upsample. proto_out: [mask_dim, mask_h, mask_w] out_masks: [n, mask_dim], n is number of masks after nms bboxes: [n, 4], n is number of masks after nms shape:input_image_size, (h, w) return: h, w, n """ c, mh, mw = protos.shape # CHW masks = (masks_in @ protos.float().view(c, -1)).sigmoid().view(-1, mh, mw) masks = F.interpolate(masks[None], shape, mode='bilinear', align_corners=False)[0] # CHW masks = crop_mask(masks, bboxes) # CHW return masks.gt_(0.5) def process_mask(protos, masks_in, bboxes, shape, upsample=False): """ Crop before upsample. proto_out: [mask_dim, mask_h, mask_w] out_masks: [n, mask_dim], n is number of masks after nms bboxes: [n, 4], n is number of masks after nms shape:input_image_size, (h, w) return: h, w, n """ c, mh, mw = protos.shape # CHW ih, iw = shape masks = (masks_in @ protos.float().view(c, -1)).sigmoid().view(-1, mh, mw) # CHW downsampled_bboxes = bboxes.clone() downsampled_bboxes[:, 0] *= mw / iw downsampled_bboxes[:, 2] *= mw / iw downsampled_bboxes[:, 3] *= mh / ih downsampled_bboxes[:, 1] *= mh / ih masks = crop_mask(masks, downsampled_bboxes) # CHW if upsample: masks = F.interpolate(masks[None], shape, mode='bilinear', align_corners=False)[0] # CHW return masks.gt_(0.5) def scale_image(im1_shape, masks, im0_shape, ratio_pad=None): """ img1_shape: model input shape, [h, w] img0_shape: origin pic shape, [h, w, 3] masks: [h, w, num] """ # Rescale coordinates (xyxy) from im1_shape to im0_shape if ratio_pad is None: # calculate from im0_shape gain = min(im1_shape[0] / im0_shape[0], im1_shape[1] / im0_shape[1]) # gain = old / new pad = (im1_shape[1] - im0_shape[1] * gain) / 2, (im1_shape[0] - im0_shape[0] * gain) / 2 # wh padding else: pad = ratio_pad[1] top, left = int(pad[1]), int(pad[0]) # y, x bottom, right = int(im1_shape[0] - pad[1]), int(im1_shape[1] - pad[0]) if len(masks.shape) < 2: raise ValueError(f'"len of masks shape" should be 2 or 3, but got {len(masks.shape)}') masks = masks[top:bottom, left:right] # masks = masks.permute(2, 0, 1).contiguous() # masks = F.interpolate(masks[None], im0_shape[:2], mode='bilinear', align_corners=False)[0] # masks = masks.permute(1, 2, 0).contiguous() masks = cv2.resize(masks, (im0_shape[1], im0_shape[0])) if len(masks.shape) == 2: masks = masks[:, :, None] return masks def mask_iou(mask1, mask2, eps=1e-7): """ mask1: [N, n] m1 means number of predicted objects mask2: [M, n] m2 means number of gt objects Note: n means image_w x image_h return: masks iou, [N, M] """ intersection = torch.matmul(mask1, mask2.t()).clamp(0) union = (mask1.sum(1)[:, None] + mask2.sum(1)[None]) - intersection # (area1 + area2) - intersection return intersection / (union + eps) def masks_iou(mask1, mask2, eps=1e-7): """ mask1: [N, n] m1 means number of predicted objects mask2: [N, n] m2 means number of gt objects Note: n means image_w x image_h return: masks iou, (N, ) """ intersection = (mask1 * mask2).sum(1).clamp(0) # (N, ) union = (mask1.sum(1) + mask2.sum(1))[None] - intersection # (area1 + area2) - intersection return intersection / (union + eps) def masks2segments(masks, strategy='largest'): # Convert masks(n,160,160) into segments(n,xy) segments = [] for x in masks.int().cpu().numpy().astype('uint8'): c = cv2.findContours(x, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0] if c: if strategy == 'concat': # concatenate all segments c = np.concatenate([x.reshape(-1, 2) for x in c]) elif strategy == 'largest': # select largest segment c = np.array(c[np.array([len(x) for x in c]).argmax()]).reshape(-1, 2) else: c = np.zeros((0, 2)) # no segments found segments.append(c.astype('float32')) return segments ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/loss.py ================================================ import torch import torch.nn as nn import torch.nn.functional as F from ..general import xywh2xyxy from ..loss import FocalLoss, smooth_BCE from ..metrics import bbox_iou from ..torch_utils import de_parallel from .general import crop_mask class ComputeLoss: # Compute losses def __init__(self, model, autobalance=False, overlap=False): self.sort_obj_iou = False self.overlap = overlap device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters self.device = device # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['cls_pw']], device=device)) BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device)) # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) # positive, negative BCE targets # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g) m = de_parallel(model).model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7 self.ssi = list(m.stride).index(16) if autobalance else 0 # stride 16 index self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, 1.0, h, autobalance self.na = m.na # number of anchors self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.nm = m.nm # number of masks self.anchors = m.anchors self.device = device def __call__(self, preds, targets, masks): # predictions, targets, model p, proto = preds bs, nm, mask_h, mask_w = proto.shape # batch size, number of masks, mask height, mask width lcls = torch.zeros(1, device=self.device) lbox = torch.zeros(1, device=self.device) lobj = torch.zeros(1, device=self.device) lseg = torch.zeros(1, device=self.device) tcls, tbox, indices, anchors, tidxs, xywhn = self.build_targets(p, targets) # targets # Losses for i, pi in enumerate(p): # layer index, layer predictions b, a, gj, gi = indices[i] # image, anchor, gridy, gridx tobj = torch.zeros(pi.shape[:4], dtype=pi.dtype, device=self.device) # target obj n = b.shape[0] # number of targets if n: pxy, pwh, _, pcls, pmask = pi[b, a, gj, gi].split((2, 2, 1, self.nc, nm), 1) # subset of predictions # Box regression pxy = pxy.sigmoid() * 2 - 0.5 pwh = (pwh.sigmoid() * 2) ** 2 * anchors[i] pbox = torch.cat((pxy, pwh), 1) # predicted box iou = bbox_iou(pbox, tbox[i], CIoU=True).squeeze() # iou(prediction, target) lbox += (1.0 - iou).mean() # iou loss # Objectness iou = iou.detach().clamp(0).type(tobj.dtype) if self.sort_obj_iou: j = iou.argsort() b, a, gj, gi, iou = b[j], a[j], gj[j], gi[j], iou[j] if self.gr < 1: iou = (1.0 - self.gr) + self.gr * iou tobj[b, a, gj, gi] = iou # iou ratio # Classification if self.nc > 1: # cls loss (only if multiple classes) t = torch.full_like(pcls, self.cn, device=self.device) # targets t[range(n), tcls[i]] = self.cp lcls += self.BCEcls(pcls, t) # BCE # Mask regression if tuple(masks.shape[-2:]) != (mask_h, mask_w): # downsample masks = F.interpolate(masks[None], (mask_h, mask_w), mode="nearest")[0] marea = xywhn[i][:, 2:].prod(1) # mask width, height normalized mxyxy = xywh2xyxy(xywhn[i] * torch.tensor([mask_w, mask_h, mask_w, mask_h], device=self.device)) for bi in b.unique(): j = b == bi # matching index if self.overlap: mask_gti = torch.where(masks[bi][None] == tidxs[i][j].view(-1, 1, 1), 1.0, 0.0) else: mask_gti = masks[tidxs[i]][j] lseg += self.single_mask_loss(mask_gti, pmask[j], proto[bi], mxyxy[j], marea[j]) obji = self.BCEobj(pi[..., 4], tobj) lobj += obji * self.balance[i] # obj loss if self.autobalance: self.balance[i] = self.balance[i] * 0.9999 + 0.0001 / obji.detach().item() if self.autobalance: self.balance = [x / self.balance[self.ssi] for x in self.balance] lbox *= self.hyp["box"] lobj *= self.hyp["obj"] lcls *= self.hyp["cls"] lseg *= self.hyp["box"] / bs loss = lbox + lobj + lcls + lseg return loss * bs, torch.cat((lbox, lseg, lobj, lcls)).detach() def single_mask_loss(self, gt_mask, pred, proto, xyxy, area): # Mask loss for one image pred_mask = (pred @ proto.view(self.nm, -1)).view(-1, *proto.shape[1:]) # (n,32) @ (32,80,80) -> (n,80,80) loss = F.binary_cross_entropy_with_logits(pred_mask, gt_mask, reduction="none") return (crop_mask(loss, xyxy).mean(dim=(1, 2)) / area).mean() def build_targets(self, p, targets): # Build targets for compute_loss(), input targets(image,class,x,y,w,h) na, nt = self.na, targets.shape[0] # number of anchors, targets tcls, tbox, indices, anch, tidxs, xywhn = [], [], [], [], [], [] gain = torch.ones(8, device=self.device) # normalized to gridspace gain ai = torch.arange(na, device=self.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt) if self.overlap: batch = p[0].shape[0] ti = [] for i in range(batch): num = (targets[:, 0] == i).sum() # find number of targets of each image ti.append(torch.arange(num, device=self.device).float().view(1, num).repeat(na, 1) + 1) # (na, num) ti = torch.cat(ti, 1) # (na, nt) else: ti = torch.arange(nt, device=self.device).float().view(1, nt).repeat(na, 1) targets = torch.cat((targets.repeat(na, 1, 1), ai[..., None], ti[..., None]), 2) # append anchor indices g = 0.5 # bias off = torch.tensor( [ [0, 0], [1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m # [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm ], device=self.device).float() * g # offsets for i in range(self.nl): anchors, shape = self.anchors[i], p[i].shape gain[2:6] = torch.tensor(shape)[[3, 2, 3, 2]] # xyxy gain # Match targets to anchors t = targets * gain # shape(3,n,7) if nt: # Matches r = t[..., 4:6] / anchors[:, None] # wh ratio j = torch.max(r, 1 / r).max(2)[0] < self.hyp['anchor_t'] # compare # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2)) t = t[j] # filter # Offsets gxy = t[:, 2:4] # grid xy gxi = gain[[2, 3]] - gxy # inverse j, k = ((gxy % 1 < g) & (gxy > 1)).T l, m = ((gxi % 1 < g) & (gxi > 1)).T j = torch.stack((torch.ones_like(j), j, k, l, m)) t = t.repeat((5, 1, 1))[j] offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j] else: t = targets[0] offsets = 0 # Define bc, gxy, gwh, at = t.chunk(4, 1) # (image, class), grid xy, grid wh, anchors (a, tidx), (b, c) = at.long().T, bc.long().T # anchors, image, class gij = (gxy - offsets).long() gi, gj = gij.T # grid indices # Append indices.append((b, a, gj.clamp_(0, shape[2] - 1), gi.clamp_(0, shape[3] - 1))) # image, anchor, grid tbox.append(torch.cat((gxy - gij, gwh), 1)) # box anch.append(anchors[a]) # anchors tcls.append(c) # class tidxs.append(tidx) xywhn.append(torch.cat((gxy, gwh), 1) / gain[2:6]) # xywh normalized return tcls, tbox, indices, anch, tidxs, xywhn ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/loss_tal.py ================================================ import os import torch import torch.nn as nn import torch.nn.functional as F from torchvision.ops import sigmoid_focal_loss from utils.general import xywh2xyxy, xyxy2xywh from utils.metrics import bbox_iou from utils.segment.tal.anchor_generator import dist2bbox, make_anchors, bbox2dist from utils.segment.tal.assigner import TaskAlignedAssigner from utils.torch_utils import de_parallel from utils.segment.general import crop_mask def smooth_BCE(eps=0.1): # https://github.com/ultralytics/yolov3/issues/238#issuecomment-598028441 # return positive, negative label smoothing BCE targets return 1.0 - 0.5 * eps, 0.5 * eps class VarifocalLoss(nn.Module): # Varifocal loss by Zhang et al. https://arxiv.org/abs/2008.13367 def __init__(self): super().__init__() def forward(self, pred_score, gt_score, label, alpha=0.75, gamma=2.0): weight = alpha * pred_score.sigmoid().pow(gamma) * (1 - label) + gt_score * label with torch.cuda.amp.autocast(enabled=False): loss = (F.binary_cross_entropy_with_logits(pred_score.float(), gt_score.float(), reduction="none") * weight).sum() return loss class FocalLoss(nn.Module): # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5) def __init__(self, loss_fcn, gamma=1.5, alpha=0.25): super().__init__() self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss() self.gamma = gamma self.alpha = alpha self.reduction = loss_fcn.reduction self.loss_fcn.reduction = "none" # required to apply FL to each element def forward(self, pred, true): loss = self.loss_fcn(pred, true) # p_t = torch.exp(-loss) # loss *= self.alpha * (1.000001 - p_t) ** self.gamma # non-zero power for gradient stability # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py pred_prob = torch.sigmoid(pred) # prob from logits p_t = true * pred_prob + (1 - true) * (1 - pred_prob) alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha) modulating_factor = (1.0 - p_t) ** self.gamma loss *= alpha_factor * modulating_factor if self.reduction == "mean": return loss.mean() elif self.reduction == "sum": return loss.sum() else: # 'none' return loss class BboxLoss(nn.Module): def __init__(self, reg_max, use_dfl=False): super().__init__() self.reg_max = reg_max self.use_dfl = use_dfl def forward(self, pred_dist, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask): # iou loss bbox_mask = fg_mask.unsqueeze(-1).repeat([1, 1, 4]) # (b, h*w, 4) pred_bboxes_pos = torch.masked_select(pred_bboxes, bbox_mask).view(-1, 4) target_bboxes_pos = torch.masked_select(target_bboxes, bbox_mask).view(-1, 4) bbox_weight = torch.masked_select(target_scores.sum(-1), fg_mask).unsqueeze(-1) iou = bbox_iou(pred_bboxes_pos, target_bboxes_pos, xywh=False, CIoU=True) loss_iou = 1.0 - iou loss_iou *= bbox_weight loss_iou = loss_iou.sum() / target_scores_sum # dfl loss if self.use_dfl: dist_mask = fg_mask.unsqueeze(-1).repeat([1, 1, (self.reg_max + 1) * 4]) pred_dist_pos = torch.masked_select(pred_dist, dist_mask).view(-1, 4, self.reg_max + 1) target_ltrb = bbox2dist(anchor_points, target_bboxes, self.reg_max) target_ltrb_pos = torch.masked_select(target_ltrb, bbox_mask).view(-1, 4) loss_dfl = self._df_loss(pred_dist_pos, target_ltrb_pos) * bbox_weight loss_dfl = loss_dfl.sum() / target_scores_sum else: loss_dfl = torch.tensor(0.0).to(pred_dist.device) return loss_iou, loss_dfl, iou def _df_loss(self, pred_dist, target): target_left = target.to(torch.long) target_right = target_left + 1 weight_left = target_right.to(torch.float) - target weight_right = 1 - weight_left loss_left = F.cross_entropy(pred_dist.view(-1, self.reg_max + 1), target_left.view(-1), reduction="none").view( target_left.shape) * weight_left loss_right = F.cross_entropy(pred_dist.view(-1, self.reg_max + 1), target_right.view(-1), reduction="none").view(target_left.shape) * weight_right return (loss_left + loss_right).mean(-1, keepdim=True) class ComputeLoss: # Compute losses def __init__(self, model, use_dfl=True, overlap=True): device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h["cls_pw"]], device=device), reduction='none') # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get("label_smoothing", 0.0)) # positive, negative BCE targets # Focal loss g = h["fl_gamma"] # focal loss gamma if g > 0: BCEcls = FocalLoss(BCEcls, g) m = de_parallel(model).model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7 self.BCEcls = BCEcls self.hyp = h self.stride = m.stride # model strides self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.no = m.no self.nm = m.nm self.overlap = overlap self.reg_max = m.reg_max self.device = device self.assigner = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.proj = torch.arange(m.reg_max).float().to(device) # / 120.0 self.use_dfl = use_dfl def preprocess(self, targets, batch_size, scale_tensor): if targets.shape[0] == 0: out = torch.zeros(batch_size, 0, 5, device=self.device) else: i = targets[:, 0] # image index _, counts = i.unique(return_counts=True) out = torch.zeros(batch_size, counts.max(), 5, device=self.device) for j in range(batch_size): matches = i == j n = matches.sum() if n: out[j, :n] = targets[matches, 1:] out[..., 1:5] = xywh2xyxy(out[..., 1:5].mul_(scale_tensor)) return out def bbox_decode(self, anchor_points, pred_dist): if self.use_dfl: b, a, c = pred_dist.shape # batch, anchors, channels pred_dist = pred_dist.view(b, a, 4, c // 4).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = pred_dist.view(b, a, c // 4, 4).transpose(2,3).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = (pred_dist.view(b, a, c // 4, 4).softmax(2) * self.proj.type(pred_dist.dtype).view(1, 1, -1, 1)).sum(2) return dist2bbox(pred_dist, anchor_points, xywh=False) def __call__(self, p, targets, masks, img=None, epoch=0): loss = torch.zeros(4, device=self.device) # box, cls, dfl feats, pred_masks, proto = p if len(p) == 3 else p[1] batch_size, _, mask_h, mask_w = proto.shape pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores = pred_scores.permute(0, 2, 1).contiguous() pred_distri = pred_distri.permute(0, 2, 1).contiguous() pred_masks = pred_masks.permute(0, 2, 1).contiguous() dtype = pred_scores.dtype batch_size, grid_size = pred_scores.shape[:2] imgsz = torch.tensor(feats[0].shape[2:], device=self.device, dtype=dtype) * self.stride[0] # image size (h,w) anchor_points, stride_tensor = make_anchors(feats, self.stride, 0.5) # targets try: batch_idx = targets[:, 0].view(-1, 1) targets = self.preprocess(targets.to(self.device), batch_size, scale_tensor=imgsz[[1, 0, 1, 0]]) gt_labels, gt_bboxes = targets.split((1, 4), 2) # cls, xyxy mask_gt = gt_bboxes.sum(2, keepdim=True).gt_(0) except RuntimeError as e: raise TypeError('ERROR.') from e # pboxes pred_bboxes = self.bbox_decode(anchor_points, pred_distri) # xyxy, (b, h*w, 4) target_labels, target_bboxes, target_scores, fg_mask, target_gt_idx = self.assigner( pred_scores.detach().sigmoid(), (pred_bboxes.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_scores_sum = target_scores.sum() # cls loss # loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum # VFL way loss[2] = self.BCEcls(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum # BCE # bbox loss if fg_mask.sum(): loss[0], loss[3], _ = self.bbox_loss(pred_distri, pred_bboxes, anchor_points, target_bboxes / stride_tensor, target_scores, target_scores_sum, fg_mask) # masks loss if tuple(masks.shape[-2:]) != (mask_h, mask_w): # downsample masks = F.interpolate(masks[None], (mask_h, mask_w), mode='nearest')[0] for i in range(batch_size): if fg_mask[i].sum(): mask_idx = target_gt_idx[i][fg_mask[i]] if self.overlap: gt_mask = torch.where(masks[[i]] == (mask_idx + 1).view(-1, 1, 1), 1.0, 0.0) else: gt_mask = masks[batch_idx.view(-1) == i][mask_idx] xyxyn = target_bboxes[i][fg_mask[i]] / imgsz[[1, 0, 1, 0]] marea = xyxy2xywh(xyxyn)[:, 2:].prod(1) mxyxy = xyxyn * torch.tensor([mask_w, mask_h, mask_w, mask_h], device=self.device) loss[1] += self.single_mask_loss(gt_mask, pred_masks[i][fg_mask[i]], proto[i], mxyxy, marea) # seg loss loss[0] *= 7.5 # box gain loss[1] *= 2.5 / batch_size loss[2] *= 0.5 # cls gain loss[3] *= 1.5 # dfl gain return loss.sum() * batch_size, loss.detach() # loss(box, cls, dfl) def single_mask_loss(self, gt_mask, pred, proto, xyxy, area): # Mask loss for one image pred_mask = (pred @ proto.view(self.nm, -1)).view(-1, *proto.shape[1:]) # (n, 32) @ (32,80,80) -> (n,80,80) loss = F.binary_cross_entropy_with_logits(pred_mask, gt_mask, reduction='none') #loss = sigmoid_focal_loss(pred_mask, gt_mask, alpha = .25, gamma = 2., reduction = 'none') return (crop_mask(loss, xyxy).mean(dim=(1, 2)) / area).mean() #p_m = torch.flatten(pred_mask.sigmoid()) #p_m = torch.flatten(pred_mask.softmax(dim = 1)) #g_m = torch.flatten(gt_mask) #i_m = torch.sum(torch.mul(p_m, g_m)) #u_m = torch.sum(torch.add(p_m, g_m)) #d_c = (2. * i_m + 1.) / (u_m + 1.) #d_l = (1. - d_c) #return d_l ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/loss_tal_dual.py ================================================ import os import torch import torch.nn as nn import torch.nn.functional as F from torchvision.ops import sigmoid_focal_loss from utils.general import xywh2xyxy, xyxy2xywh from utils.metrics import bbox_iou from utils.segment.tal.anchor_generator import dist2bbox, make_anchors, bbox2dist from utils.segment.tal.assigner import TaskAlignedAssigner from utils.torch_utils import de_parallel from utils.segment.general import crop_mask def smooth_BCE(eps=0.1): # https://github.com/ultralytics/yolov3/issues/238#issuecomment-598028441 # return positive, negative label smoothing BCE targets return 1.0 - 0.5 * eps, 0.5 * eps class VarifocalLoss(nn.Module): # Varifocal loss by Zhang et al. https://arxiv.org/abs/2008.13367 def __init__(self): super().__init__() def forward(self, pred_score, gt_score, label, alpha=0.75, gamma=2.0): weight = alpha * pred_score.sigmoid().pow(gamma) * (1 - label) + gt_score * label with torch.cuda.amp.autocast(enabled=False): loss = (F.binary_cross_entropy_with_logits(pred_score.float(), gt_score.float(), reduction="none") * weight).sum() return loss class FocalLoss(nn.Module): # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5) def __init__(self, loss_fcn, gamma=1.5, alpha=0.25): super().__init__() self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss() self.gamma = gamma self.alpha = alpha self.reduction = loss_fcn.reduction self.loss_fcn.reduction = "none" # required to apply FL to each element def forward(self, pred, true): loss = self.loss_fcn(pred, true) # p_t = torch.exp(-loss) # loss *= self.alpha * (1.000001 - p_t) ** self.gamma # non-zero power for gradient stability # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py pred_prob = torch.sigmoid(pred) # prob from logits p_t = true * pred_prob + (1 - true) * (1 - pred_prob) alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha) modulating_factor = (1.0 - p_t) ** self.gamma loss *= alpha_factor * modulating_factor if self.reduction == "mean": return loss.mean() elif self.reduction == "sum": return loss.sum() else: # 'none' return loss class BboxLoss(nn.Module): def __init__(self, reg_max, use_dfl=False): super().__init__() self.reg_max = reg_max self.use_dfl = use_dfl def forward(self, pred_dist, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask): # iou loss bbox_mask = fg_mask.unsqueeze(-1).repeat([1, 1, 4]) # (b, h*w, 4) pred_bboxes_pos = torch.masked_select(pred_bboxes, bbox_mask).view(-1, 4) target_bboxes_pos = torch.masked_select(target_bboxes, bbox_mask).view(-1, 4) bbox_weight = torch.masked_select(target_scores.sum(-1), fg_mask).unsqueeze(-1) iou = bbox_iou(pred_bboxes_pos, target_bboxes_pos, xywh=False, CIoU=True) loss_iou = 1.0 - iou loss_iou *= bbox_weight loss_iou = loss_iou.sum() / target_scores_sum # dfl loss if self.use_dfl: dist_mask = fg_mask.unsqueeze(-1).repeat([1, 1, (self.reg_max + 1) * 4]) pred_dist_pos = torch.masked_select(pred_dist, dist_mask).view(-1, 4, self.reg_max + 1) target_ltrb = bbox2dist(anchor_points, target_bboxes, self.reg_max) target_ltrb_pos = torch.masked_select(target_ltrb, bbox_mask).view(-1, 4) loss_dfl = self._df_loss(pred_dist_pos, target_ltrb_pos) * bbox_weight loss_dfl = loss_dfl.sum() / target_scores_sum else: loss_dfl = torch.tensor(0.0).to(pred_dist.device) return loss_iou, loss_dfl, iou def _df_loss(self, pred_dist, target): target_left = target.to(torch.long) target_right = target_left + 1 weight_left = target_right.to(torch.float) - target weight_right = 1 - weight_left loss_left = F.cross_entropy(pred_dist.view(-1, self.reg_max + 1), target_left.view(-1), reduction="none").view( target_left.shape) * weight_left loss_right = F.cross_entropy(pred_dist.view(-1, self.reg_max + 1), target_right.view(-1), reduction="none").view(target_left.shape) * weight_right return (loss_left + loss_right).mean(-1, keepdim=True) class ComputeLoss: # Compute losses def __init__(self, model, use_dfl=True, overlap=True): device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h["cls_pw"]], device=device), reduction='none') # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get("label_smoothing", 0.0)) # positive, negative BCE targets # Focal loss g = h["fl_gamma"] # focal loss gamma if g > 0: BCEcls = FocalLoss(BCEcls, g) m = de_parallel(model).model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7 self.BCEcls = BCEcls self.hyp = h self.stride = m.stride # model strides self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.no = m.no self.nm = m.nm self.overlap = overlap self.reg_max = m.reg_max self.device = device self.assigner = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.assigner2 = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.bbox_loss2 = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.proj = torch.arange(m.reg_max).float().to(device) # / 120.0 self.use_dfl = use_dfl def preprocess(self, targets, batch_size, scale_tensor): if targets.shape[0] == 0: out = torch.zeros(batch_size, 0, 5, device=self.device) else: i = targets[:, 0] # image index _, counts = i.unique(return_counts=True) out = torch.zeros(batch_size, counts.max(), 5, device=self.device) for j in range(batch_size): matches = i == j n = matches.sum() if n: out[j, :n] = targets[matches, 1:] out[..., 1:5] = xywh2xyxy(out[..., 1:5].mul_(scale_tensor)) return out def bbox_decode(self, anchor_points, pred_dist): if self.use_dfl: b, a, c = pred_dist.shape # batch, anchors, channels pred_dist = pred_dist.view(b, a, 4, c // 4).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = pred_dist.view(b, a, c // 4, 4).transpose(2,3).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = (pred_dist.view(b, a, c // 4, 4).softmax(2) * self.proj.type(pred_dist.dtype).view(1, 1, -1, 1)).sum(2) return dist2bbox(pred_dist, anchor_points, xywh=False) def __call__(self, p, targets, masks, img=None, epoch=0): loss = torch.zeros(4, device=self.device) # box, cls, dfl feats_, pred_masks_, proto_ = p if len(p) == 3 else p[1] feats, pred_masks, proto = feats_[0], pred_masks_[0], proto_[0] feats2, pred_masks2, proto2 = feats_[1], pred_masks_[1], proto_[1] batch_size, _, mask_h, mask_w = proto.shape pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores = pred_scores.permute(0, 2, 1).contiguous() pred_distri = pred_distri.permute(0, 2, 1).contiguous() pred_masks = pred_masks.permute(0, 2, 1).contiguous() pred_distri2, pred_scores2 = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats2], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores2 = pred_scores2.permute(0, 2, 1).contiguous() pred_distri2 = pred_distri2.permute(0, 2, 1).contiguous() pred_masks2 = pred_masks2.permute(0, 2, 1).contiguous() dtype = pred_scores.dtype batch_size, grid_size = pred_scores.shape[:2] imgsz = torch.tensor(feats[0].shape[2:], device=self.device, dtype=dtype) * self.stride[0] # image size (h,w) anchor_points, stride_tensor = make_anchors(feats, self.stride, 0.5) # targets try: batch_idx = targets[:, 0].view(-1, 1) targets = self.preprocess(targets.to(self.device), batch_size, scale_tensor=imgsz[[1, 0, 1, 0]]) gt_labels, gt_bboxes = targets.split((1, 4), 2) # cls, xyxy mask_gt = gt_bboxes.sum(2, keepdim=True).gt_(0) except RuntimeError as e: raise TypeError('ERROR.') from e # pboxes pred_bboxes = self.bbox_decode(anchor_points, pred_distri) # xyxy, (b, h*w, 4) pred_bboxes2 = self.bbox_decode(anchor_points, pred_distri2) # xyxy, (b, h*w, 4) target_labels, target_bboxes, target_scores, fg_mask, target_gt_idx = self.assigner( pred_scores.detach().sigmoid(), (pred_bboxes.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_labels2, target_bboxes2, target_scores2, fg_mask2, target_gt_idx2 = self.assigner2( pred_scores2.detach().sigmoid(), (pred_bboxes2.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_scores_sum = target_scores.sum() target_scores_sum2 = target_scores2.sum() # cls loss # loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum # VFL way loss[2] = self.BCEcls(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum # BCE loss[2] *= 0.25 loss[2] += self.BCEcls(pred_scores2, target_scores2.to(dtype)).sum() / target_scores_sum2 # BCE # bbox loss if fg_mask.sum(): loss[0], loss[3], _ = self.bbox_loss(pred_distri, pred_bboxes, anchor_points, target_bboxes / stride_tensor, target_scores, target_scores_sum, fg_mask) # masks loss if tuple(masks.shape[-2:]) != (mask_h, mask_w): # downsample masks = F.interpolate(masks[None], (mask_h, mask_w), mode='nearest')[0] for i in range(batch_size): if fg_mask[i].sum(): mask_idx = target_gt_idx[i][fg_mask[i]] if self.overlap: gt_mask = torch.where(masks[[i]] == (mask_idx + 1).view(-1, 1, 1), 1.0, 0.0) else: gt_mask = masks[batch_idx.view(-1) == i][mask_idx] xyxyn = target_bboxes[i][fg_mask[i]] / imgsz[[1, 0, 1, 0]] marea = xyxy2xywh(xyxyn)[:, 2:].prod(1) mxyxy = xyxyn * torch.tensor([mask_w, mask_h, mask_w, mask_h], device=self.device) loss[1] += self.single_mask_loss(gt_mask, pred_masks[i][fg_mask[i]], proto[i], mxyxy, marea) # seg loss loss[0] *= 0.25 loss[3] *= 0.25 loss[1] *= 0.25 # bbox loss if fg_mask2.sum(): loss0_, loss3_, _ = self.bbox_loss2(pred_distri2, pred_bboxes2, anchor_points, target_bboxes2 / stride_tensor, target_scores2, target_scores_sum2, fg_mask2) # masks loss if tuple(masks.shape[-2:]) != (mask_h, mask_w): # downsample masks = F.interpolate(masks[None], (mask_h, mask_w), mode='nearest')[0] for i in range(batch_size): if fg_mask2[i].sum(): mask_idx = target_gt_idx2[i][fg_mask2[i]] if self.overlap: gt_mask = torch.where(masks[[i]] == (mask_idx + 1).view(-1, 1, 1), 1.0, 0.0) else: gt_mask = masks[batch_idx.view(-1) == i][mask_idx] xyxyn = target_bboxes2[i][fg_mask2[i]] / imgsz[[1, 0, 1, 0]] marea = xyxy2xywh(xyxyn)[:, 2:].prod(1) mxyxy = xyxyn * torch.tensor([mask_w, mask_h, mask_w, mask_h], device=self.device) loss[1] += self.single_mask_loss(gt_mask, pred_masks2[i][fg_mask2[i]], proto2[i], mxyxy, marea) # seg loss loss[0] += loss0_ loss[3] += loss3_ loss[0] *= 7.5 # box gain loss[1] *= 2.5 / batch_size loss[2] *= 0.5 # cls gain loss[3] *= 1.5 # dfl gain return loss.sum() * batch_size, loss.detach() # loss(box, cls, dfl) def single_mask_loss(self, gt_mask, pred, proto, xyxy, area): # Mask loss for one image pred_mask = (pred @ proto.view(self.nm, -1)).view(-1, *proto.shape[1:]) # (n, 32) @ (32,80,80) -> (n,80,80) loss = F.binary_cross_entropy_with_logits(pred_mask, gt_mask, reduction='none') #loss = sigmoid_focal_loss(pred_mask, gt_mask, alpha = .25, gamma = 2., reduction = 'none') #p_m = torch.flatten(pred_mask.softmax(dim = 1)) #g_m = torch.flatten(gt_mask) #i_m = torch.sum(torch.mul(p_m, g_m)) #u_m = torch.sum(torch.add(p_m, g_m)) #dice_coef = (2. * i_m + 1.) / (u_m + 1.) #dice_loss = (1. - dice_coef) return (crop_mask(loss, xyxy).mean(dim=(1, 2)) / area).mean() class ComputeLossLH: # Compute losses def __init__(self, model, use_dfl=True, overlap=True): device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h["cls_pw"]], device=device), reduction='none') # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get("label_smoothing", 0.0)) # positive, negative BCE targets # Focal loss g = h["fl_gamma"] # focal loss gamma if g > 0: BCEcls = FocalLoss(BCEcls, g) m = de_parallel(model).model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7 self.BCEcls = BCEcls self.hyp = h self.stride = m.stride # model strides self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.no = m.no self.nm = m.nm self.overlap = overlap self.reg_max = m.reg_max self.device = device self.assigner = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.proj = torch.arange(m.reg_max).float().to(device) # / 120.0 self.use_dfl = use_dfl def preprocess(self, targets, batch_size, scale_tensor): if targets.shape[0] == 0: out = torch.zeros(batch_size, 0, 5, device=self.device) else: i = targets[:, 0] # image index _, counts = i.unique(return_counts=True) out = torch.zeros(batch_size, counts.max(), 5, device=self.device) for j in range(batch_size): matches = i == j n = matches.sum() if n: out[j, :n] = targets[matches, 1:] out[..., 1:5] = xywh2xyxy(out[..., 1:5].mul_(scale_tensor)) return out def bbox_decode(self, anchor_points, pred_dist): if self.use_dfl: b, a, c = pred_dist.shape # batch, anchors, channels pred_dist = pred_dist.view(b, a, 4, c // 4).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = pred_dist.view(b, a, c // 4, 4).transpose(2,3).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = (pred_dist.view(b, a, c // 4, 4).softmax(2) * self.proj.type(pred_dist.dtype).view(1, 1, -1, 1)).sum(2) return dist2bbox(pred_dist, anchor_points, xywh=False) def __call__(self, p, targets, masks, img=None, epoch=0): loss = torch.zeros(4, device=self.device) # box, cls, dfl feats_, pred_masks_, proto_ = p if len(p) == 3 else p[1] feats, pred_masks, proto = feats_[0], pred_masks_[0], proto_[0] feats2, pred_masks2, proto2 = feats_[1], pred_masks_[1], proto_[1] batch_size, _, mask_h, mask_w = proto.shape pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores = pred_scores.permute(0, 2, 1).contiguous() pred_distri = pred_distri.permute(0, 2, 1).contiguous() pred_masks = pred_masks.permute(0, 2, 1).contiguous() pred_distri2, pred_scores2 = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats2], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores2 = pred_scores2.permute(0, 2, 1).contiguous() pred_distri2 = pred_distri2.permute(0, 2, 1).contiguous() pred_masks2 = pred_masks2.permute(0, 2, 1).contiguous() dtype = pred_scores.dtype batch_size, grid_size = pred_scores.shape[:2] imgsz = torch.tensor(feats[0].shape[2:], device=self.device, dtype=dtype) * self.stride[0] # image size (h,w) anchor_points, stride_tensor = make_anchors(feats, self.stride, 0.5) # targets try: batch_idx = targets[:, 0].view(-1, 1) targets = self.preprocess(targets.to(self.device), batch_size, scale_tensor=imgsz[[1, 0, 1, 0]]) gt_labels, gt_bboxes = targets.split((1, 4), 2) # cls, xyxy mask_gt = gt_bboxes.sum(2, keepdim=True).gt_(0) except RuntimeError as e: raise TypeError('ERROR.') from e # pboxes pred_bboxes = self.bbox_decode(anchor_points, pred_distri) # xyxy, (b, h*w, 4) pred_bboxes2 = self.bbox_decode(anchor_points, pred_distri2) # xyxy, (b, h*w, 4) target_labels, target_bboxes, target_scores, fg_mask, target_gt_idx = self.assigner( pred_scores2.detach().sigmoid(), (pred_bboxes2.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_scores_sum = target_scores.sum() # cls loss # loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum # VFL way loss[2] = self.BCEcls(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum # BCE loss[2] *= 0.25 loss[2] += self.BCEcls(pred_scores2, target_scores.to(dtype)).sum() / target_scores_sum # BCE # bbox loss if fg_mask.sum(): loss[0], loss[3], _ = self.bbox_loss(pred_distri, pred_bboxes, anchor_points, target_bboxes / stride_tensor, target_scores, target_scores_sum, fg_mask) # masks loss if tuple(masks.shape[-2:]) != (mask_h, mask_w): # downsample masks = F.interpolate(masks[None], (mask_h, mask_w), mode='nearest')[0] for i in range(batch_size): if fg_mask[i].sum(): mask_idx = target_gt_idx[i][fg_mask[i]] if self.overlap: gt_mask = torch.where(masks[[i]] == (mask_idx + 1).view(-1, 1, 1), 1.0, 0.0) else: gt_mask = masks[batch_idx.view(-1) == i][mask_idx] xyxyn = target_bboxes[i][fg_mask[i]] / imgsz[[1, 0, 1, 0]] marea = xyxy2xywh(xyxyn)[:, 2:].prod(1) mxyxy = xyxyn * torch.tensor([mask_w, mask_h, mask_w, mask_h], device=self.device) loss[1] += self.single_mask_loss(gt_mask, pred_masks[i][fg_mask[i]], proto[i], mxyxy, marea) # seg loss loss[0] *= 0.25 loss[3] *= 0.25 loss[1] *= 0.25 # bbox loss if fg_mask.sum(): loss0_, loss3_, _ = self.bbox_loss(pred_distri2, pred_bboxes2, anchor_points, target_bboxes / stride_tensor, target_scores, target_scores_sum, fg_mask) # masks loss if tuple(masks.shape[-2:]) != (mask_h, mask_w): # downsample masks = F.interpolate(masks[None], (mask_h, mask_w), mode='nearest')[0] for i in range(batch_size): if fg_mask[i].sum(): mask_idx = target_gt_idx[i][fg_mask[i]] if self.overlap: gt_mask = torch.where(masks[[i]] == (mask_idx + 1).view(-1, 1, 1), 1.0, 0.0) else: gt_mask = masks[batch_idx.view(-1) == i][mask_idx] xyxyn = target_bboxes[i][fg_mask[i]] / imgsz[[1, 0, 1, 0]] marea = xyxy2xywh(xyxyn)[:, 2:].prod(1) mxyxy = xyxyn * torch.tensor([mask_w, mask_h, mask_w, mask_h], device=self.device) loss[1] += self.single_mask_loss(gt_mask, pred_masks2[i][fg_mask[i]], proto2[i], mxyxy, marea) # seg loss loss[0] += loss0_ loss[3] += loss3_ loss[0] *= 7.5 # box gain loss[1] *= 2.5 / batch_size loss[2] *= 0.5 # cls gain loss[3] *= 1.5 # dfl gain return loss.sum() * batch_size, loss.detach() # loss(box, cls, dfl) def single_mask_loss(self, gt_mask, pred, proto, xyxy, area): # Mask loss for one image pred_mask = (pred @ proto.view(self.nm, -1)).view(-1, *proto.shape[1:]) # (n, 32) @ (32,80,80) -> (n,80,80) loss = F.binary_cross_entropy_with_logits(pred_mask, gt_mask, reduction='none') #loss = sigmoid_focal_loss(pred_mask, gt_mask, alpha = .25, gamma = 2., reduction = 'none') #p_m = torch.flatten(pred_mask.softmax(dim = 1)) #g_m = torch.flatten(gt_mask) #i_m = torch.sum(torch.mul(p_m, g_m)) #u_m = torch.sum(torch.add(p_m, g_m)) #dice_coef = (2. * i_m + 1.) / (u_m + 1.) #dice_loss = (1. - dice_coef) return (crop_mask(loss, xyxy).mean(dim=(1, 2)) / area).mean() class ComputeLossLH0: # Compute losses def __init__(self, model, use_dfl=True, overlap=True): device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h["cls_pw"]], device=device), reduction='none') # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get("label_smoothing", 0.0)) # positive, negative BCE targets # Focal loss g = h["fl_gamma"] # focal loss gamma if g > 0: BCEcls = FocalLoss(BCEcls, g) m = de_parallel(model).model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(m.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7 self.BCEcls = BCEcls self.hyp = h self.stride = m.stride # model strides self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.no = m.no self.nm = m.nm self.overlap = overlap self.reg_max = m.reg_max self.device = device self.assigner = TaskAlignedAssigner(topk=int(os.getenv('YOLOM', 10)), num_classes=self.nc, alpha=float(os.getenv('YOLOA', 0.5)), beta=float(os.getenv('YOLOB', 6.0))) self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=use_dfl).to(device) self.proj = torch.arange(m.reg_max).float().to(device) # / 120.0 self.use_dfl = use_dfl def preprocess(self, targets, batch_size, scale_tensor): if targets.shape[0] == 0: out = torch.zeros(batch_size, 0, 5, device=self.device) else: i = targets[:, 0] # image index _, counts = i.unique(return_counts=True) out = torch.zeros(batch_size, counts.max(), 5, device=self.device) for j in range(batch_size): matches = i == j n = matches.sum() if n: out[j, :n] = targets[matches, 1:] out[..., 1:5] = xywh2xyxy(out[..., 1:5].mul_(scale_tensor)) return out def bbox_decode(self, anchor_points, pred_dist): if self.use_dfl: b, a, c = pred_dist.shape # batch, anchors, channels pred_dist = pred_dist.view(b, a, 4, c // 4).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = pred_dist.view(b, a, c // 4, 4).transpose(2,3).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = (pred_dist.view(b, a, c // 4, 4).softmax(2) * self.proj.type(pred_dist.dtype).view(1, 1, -1, 1)).sum(2) return dist2bbox(pred_dist, anchor_points, xywh=False) def __call__(self, p, targets, masks, img=None, epoch=0): loss = torch.zeros(4, device=self.device) # box, cls, dfl feats_, pred_masks_, proto_ = p if len(p) == 3 else p[1] feats, pred_masks, proto = feats_[0], pred_masks_[0], proto_[0] feats2, pred_masks2, proto2 = feats_[1], pred_masks_[1], proto_[1] batch_size, _, mask_h, mask_w = proto.shape pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores = pred_scores.permute(0, 2, 1).contiguous() pred_distri = pred_distri.permute(0, 2, 1).contiguous() pred_masks = pred_masks.permute(0, 2, 1).contiguous() pred_distri2, pred_scores2 = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats2], 2).split( (self.reg_max * 4, self.nc), 1) pred_scores2 = pred_scores2.permute(0, 2, 1).contiguous() pred_distri2 = pred_distri2.permute(0, 2, 1).contiguous() pred_masks2 = pred_masks2.permute(0, 2, 1).contiguous() dtype = pred_scores.dtype batch_size, grid_size = pred_scores.shape[:2] imgsz = torch.tensor(feats[0].shape[2:], device=self.device, dtype=dtype) * self.stride[0] # image size (h,w) anchor_points, stride_tensor = make_anchors(feats, self.stride, 0.5) # targets try: batch_idx = targets[:, 0].view(-1, 1) targets = self.preprocess(targets.to(self.device), batch_size, scale_tensor=imgsz[[1, 0, 1, 0]]) gt_labels, gt_bboxes = targets.split((1, 4), 2) # cls, xyxy mask_gt = gt_bboxes.sum(2, keepdim=True).gt_(0) except RuntimeError as e: raise TypeError('ERROR.') from e # pboxes pred_bboxes = self.bbox_decode(anchor_points, pred_distri) # xyxy, (b, h*w, 4) pred_bboxes2 = self.bbox_decode(anchor_points, pred_distri2) # xyxy, (b, h*w, 4) target_labels, target_bboxes, target_scores, fg_mask, target_gt_idx = self.assigner( pred_scores2.detach().sigmoid(), (pred_bboxes2.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_scores_sum = target_scores.sum() # cls loss # loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum # VFL way loss[2] = self.BCEcls(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum # BCE loss[2] *= 0.25 loss[2] += self.BCEcls(pred_scores2, target_scores.to(dtype)).sum() / target_scores_sum # BCE # bbox loss if fg_mask.sum(): loss[0], loss[3], _ = self.bbox_loss(pred_distri, pred_bboxes, anchor_points, target_bboxes / stride_tensor, target_scores, target_scores_sum, fg_mask) # masks loss if tuple(masks.shape[-2:]) != (mask_h, mask_w): # downsample masks = F.interpolate(masks[None], (mask_h, mask_w), mode='nearest')[0] for i in range(batch_size): if fg_mask[i].sum(): mask_idx = target_gt_idx[i][fg_mask[i]] if self.overlap: gt_mask = torch.where(masks[[i]] == (mask_idx + 1).view(-1, 1, 1), 1.0, 0.0) else: gt_mask = masks[batch_idx.view(-1) == i][mask_idx] xyxyn = target_bboxes[i][fg_mask[i]] / imgsz[[1, 0, 1, 0]] marea = xyxy2xywh(xyxyn)[:, 2:].prod(1) mxyxy = xyxyn * torch.tensor([mask_w, mask_h, mask_w, mask_h], device=self.device) loss[1] += self.single_mask_loss(gt_mask, pred_masks[i][fg_mask[i]], proto[i], mxyxy, marea) # seg loss loss[0] *= 0.25 loss[3] *= 0.25 loss[1] *= 0.25 # bbox loss if fg_mask.sum(): loss0_, loss3_, _ = self.bbox_loss(pred_distri2, pred_bboxes2, anchor_points, target_bboxes / stride_tensor, target_scores, target_scores_sum, fg_mask) # masks loss if tuple(masks.shape[-2:]) != (mask_h, mask_w): # downsample masks = F.interpolate(masks[None], (mask_h, mask_w), mode='nearest')[0] for i in range(batch_size): if fg_mask[i].sum(): mask_idx = target_gt_idx[i][fg_mask[i]] if self.overlap: gt_mask = torch.where(masks[[i]] == (mask_idx + 1).view(-1, 1, 1), 1.0, 0.0) else: gt_mask = masks[batch_idx.view(-1) == i][mask_idx] xyxyn = target_bboxes[i][fg_mask[i]] / imgsz[[1, 0, 1, 0]] marea = xyxy2xywh(xyxyn)[:, 2:].prod(1) mxyxy = xyxyn * torch.tensor([mask_w, mask_h, mask_w, mask_h], device=self.device) loss[1] += 0. * self.single_mask_loss(gt_mask, pred_masks2[i][fg_mask[i]], proto2[i], mxyxy, marea) # seg loss loss[0] += loss0_ loss[3] += loss3_ loss[0] *= 7.5 # box gain loss[1] *= 2.5 / batch_size loss[2] *= 0.5 # cls gain loss[3] *= 1.5 # dfl gain return loss.sum() * batch_size, loss.detach() # loss(box, cls, dfl) def single_mask_loss(self, gt_mask, pred, proto, xyxy, area): # Mask loss for one image pred_mask = (pred @ proto.view(self.nm, -1)).view(-1, *proto.shape[1:]) # (n, 32) @ (32,80,80) -> (n,80,80) loss = F.binary_cross_entropy_with_logits(pred_mask, gt_mask, reduction='none') #loss = sigmoid_focal_loss(pred_mask, gt_mask, alpha = .25, gamma = 2., reduction = 'none') #p_m = torch.flatten(pred_mask.softmax(dim = 1)) #g_m = torch.flatten(gt_mask) #i_m = torch.sum(torch.mul(p_m, g_m)) #u_m = torch.sum(torch.add(p_m, g_m)) #dice_coef = (2. * i_m + 1.) / (u_m + 1.) #dice_loss = (1. - dice_coef) return (crop_mask(loss, xyxy).mean(dim=(1, 2)) / area).mean() ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/metrics.py ================================================ import numpy as np from ..metrics import ap_per_class def fitness(x): # Model fitness as a weighted combination of metrics w = [0.0, 0.0, 0.1, 0.9, 0.0, 0.0, 0.1, 0.9] return (x[:, :8] * w).sum(1) def ap_per_class_box_and_mask( tp_m, tp_b, conf, pred_cls, target_cls, plot=False, save_dir=".", names=(), ): """ Args: tp_b: tp of boxes. tp_m: tp of masks. other arguments see `func: ap_per_class`. """ results_boxes = ap_per_class(tp_b, conf, pred_cls, target_cls, plot=plot, save_dir=save_dir, names=names, prefix="Box")[2:] results_masks = ap_per_class(tp_m, conf, pred_cls, target_cls, plot=plot, save_dir=save_dir, names=names, prefix="Mask")[2:] results = { "boxes": { "p": results_boxes[0], "r": results_boxes[1], "ap": results_boxes[3], "f1": results_boxes[2], "ap_class": results_boxes[4]}, "masks": { "p": results_masks[0], "r": results_masks[1], "ap": results_masks[3], "f1": results_masks[2], "ap_class": results_masks[4]}} return results class Metric: def __init__(self) -> None: self.p = [] # (nc, ) self.r = [] # (nc, ) self.f1 = [] # (nc, ) self.all_ap = [] # (nc, 10) self.ap_class_index = [] # (nc, ) @property def ap50(self): """AP@0.5 of all classes. Return: (nc, ) or []. """ return self.all_ap[:, 0] if len(self.all_ap) else [] @property def ap(self): """AP@0.5:0.95 Return: (nc, ) or []. """ return self.all_ap.mean(1) if len(self.all_ap) else [] @property def mp(self): """mean precision of all classes. Return: float. """ return self.p.mean() if len(self.p) else 0.0 @property def mr(self): """mean recall of all classes. Return: float. """ return self.r.mean() if len(self.r) else 0.0 @property def map50(self): """Mean AP@0.5 of all classes. Return: float. """ return self.all_ap[:, 0].mean() if len(self.all_ap) else 0.0 @property def map(self): """Mean AP@0.5:0.95 of all classes. Return: float. """ return self.all_ap.mean() if len(self.all_ap) else 0.0 def mean_results(self): """Mean of results, return mp, mr, map50, map""" return (self.mp, self.mr, self.map50, self.map) def class_result(self, i): """class-aware result, return p[i], r[i], ap50[i], ap[i]""" return (self.p[i], self.r[i], self.ap50[i], self.ap[i]) def get_maps(self, nc): maps = np.zeros(nc) + self.map for i, c in enumerate(self.ap_class_index): maps[c] = self.ap[i] return maps def update(self, results): """ Args: results: tuple(p, r, ap, f1, ap_class) """ p, r, all_ap, f1, ap_class_index = results self.p = p self.r = r self.all_ap = all_ap self.f1 = f1 self.ap_class_index = ap_class_index class Metrics: """Metric for boxes and masks.""" def __init__(self) -> None: self.metric_box = Metric() self.metric_mask = Metric() def update(self, results): """ Args: results: Dict{'boxes': Dict{}, 'masks': Dict{}} """ self.metric_box.update(list(results["boxes"].values())) self.metric_mask.update(list(results["masks"].values())) def mean_results(self): return self.metric_box.mean_results() + self.metric_mask.mean_results() def class_result(self, i): return self.metric_box.class_result(i) + self.metric_mask.class_result(i) def get_maps(self, nc): return self.metric_box.get_maps(nc) + self.metric_mask.get_maps(nc) @property def ap_class_index(self): # boxes and masks have the same ap_class_index return self.metric_box.ap_class_index KEYS = [ "train/box_loss", "train/seg_loss", # train loss "train/obj_loss", "train/cls_loss", "metrics/precision(B)", "metrics/recall(B)", "metrics/mAP_0.5(B)", "metrics/mAP_0.5:0.95(B)", # metrics "metrics/precision(M)", "metrics/recall(M)", "metrics/mAP_0.5(M)", "metrics/mAP_0.5:0.95(M)", # metrics "val/box_loss", "val/seg_loss", # val loss "val/obj_loss", "val/cls_loss", "x/lr0", "x/lr1", "x/lr2",] BEST_KEYS = [ "best/epoch", "best/precision(B)", "best/recall(B)", "best/mAP_0.5(B)", "best/mAP_0.5:0.95(B)", "best/precision(M)", "best/recall(M)", "best/mAP_0.5(M)", "best/mAP_0.5:0.95(M)",] ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/plots.py ================================================ import contextlib import math from pathlib import Path import cv2 import matplotlib.pyplot as plt import numpy as np import pandas as pd import torch from .. import threaded from ..general import xywh2xyxy from ..plots import Annotator, colors @threaded def plot_images_and_masks(images, targets, masks, paths=None, fname='images.jpg', names=None): # Plot image grid with labels if isinstance(images, torch.Tensor): images = images.cpu().float().numpy() if isinstance(targets, torch.Tensor): targets = targets.cpu().numpy() if isinstance(masks, torch.Tensor): masks = masks.cpu().numpy().astype(int) max_size = 1920 # max image size max_subplots = 16 # max image subplots, i.e. 4x4 bs, _, h, w = images.shape # batch size, _, height, width bs = min(bs, max_subplots) # limit plot images ns = np.ceil(bs ** 0.5) # number of subplots (square) if np.max(images[0]) <= 1: images *= 255 # de-normalise (optional) # Build Image mosaic = np.full((int(ns * h), int(ns * w), 3), 255, dtype=np.uint8) # init for i, im in enumerate(images): if i == max_subplots: # if last batch has fewer images than we expect break x, y = int(w * (i // ns)), int(h * (i % ns)) # block origin im = im.transpose(1, 2, 0) mosaic[y:y + h, x:x + w, :] = im # Resize (optional) scale = max_size / ns / max(h, w) if scale < 1: h = math.ceil(scale * h) w = math.ceil(scale * w) mosaic = cv2.resize(mosaic, tuple(int(x * ns) for x in (w, h))) # Annotate fs = int((h + w) * ns * 0.01) # font size annotator = Annotator(mosaic, line_width=round(fs / 10), font_size=fs, pil=True, example=names) for i in range(i + 1): x, y = int(w * (i // ns)), int(h * (i % ns)) # block origin annotator.rectangle([x, y, x + w, y + h], None, (255, 255, 255), width=2) # borders if paths: annotator.text((x + 5, y + 5 + h), text=Path(paths[i]).name[:40], txt_color=(220, 220, 220)) # filenames if len(targets) > 0: idx = targets[:, 0] == i ti = targets[idx] # image targets boxes = xywh2xyxy(ti[:, 2:6]).T classes = ti[:, 1].astype('int') labels = ti.shape[1] == 6 # labels if no conf column conf = None if labels else ti[:, 6] # check for confidence presence (label vs pred) if boxes.shape[1]: if boxes.max() <= 1.01: # if normalized with tolerance 0.01 boxes[[0, 2]] *= w # scale to pixels boxes[[1, 3]] *= h elif scale < 1: # absolute coords need scale if image scales boxes *= scale boxes[[0, 2]] += x boxes[[1, 3]] += y for j, box in enumerate(boxes.T.tolist()): cls = classes[j] color = colors(cls) cls = names[cls] if names else cls if labels or conf[j] > 0.25: # 0.25 conf thresh label = f'{cls}' if labels else f'{cls} {conf[j]:.1f}' annotator.box_label(box, label, color=color) # Plot masks if len(masks): if masks.max() > 1.0: # mean that masks are overlap image_masks = masks[[i]] # (1, 640, 640) nl = len(ti) index = np.arange(nl).reshape(nl, 1, 1) + 1 image_masks = np.repeat(image_masks, nl, axis=0) image_masks = np.where(image_masks == index, 1.0, 0.0) else: image_masks = masks[idx] im = np.asarray(annotator.im).copy() for j, box in enumerate(boxes.T.tolist()): if labels or conf[j] > 0.25: # 0.25 conf thresh color = colors(classes[j]) mh, mw = image_masks[j].shape if mh != h or mw != w: mask = image_masks[j].astype(np.uint8) mask = cv2.resize(mask, (w, h)) mask = mask.astype(bool) else: mask = image_masks[j].astype(bool) with contextlib.suppress(Exception): im[y:y + h, x:x + w, :][mask] = im[y:y + h, x:x + w, :][mask] * 0.4 + np.array(color) * 0.6 annotator.fromarray(im) annotator.im.save(fname) # save def plot_results_with_masks(file="path/to/results.csv", dir="", best=True): # Plot training results.csv. Usage: from utils.plots import *; plot_results('path/to/results.csv') save_dir = Path(file).parent if file else Path(dir) fig, ax = plt.subplots(2, 8, figsize=(18, 6), tight_layout=True) ax = ax.ravel() files = list(save_dir.glob("results*.csv")) assert len(files), f"No results.csv files found in {save_dir.resolve()}, nothing to plot." for f in files: try: data = pd.read_csv(f) index = np.argmax(0.9 * data.values[:, 8] + 0.1 * data.values[:, 7] + 0.9 * data.values[:, 12] + 0.1 * data.values[:, 11]) s = [x.strip() for x in data.columns] x = data.values[:, 0] for i, j in enumerate([1, 2, 3, 4, 5, 6, 9, 10, 13, 14, 15, 16, 7, 8, 11, 12]): y = data.values[:, j] # y[y == 0] = np.nan # don't show zero values ax[i].plot(x, y, marker=".", label=f.stem, linewidth=2, markersize=2) if best: # best ax[i].scatter(index, y[index], color="r", label=f"best:{index}", marker="*", linewidth=3) ax[i].set_title(s[j] + f"\n{round(y[index], 5)}") else: # last ax[i].scatter(x[-1], y[-1], color="r", label="last", marker="*", linewidth=3) ax[i].set_title(s[j] + f"\n{round(y[-1], 5)}") # if j in [8, 9, 10]: # share train and val loss y axes # ax[i].get_shared_y_axes().join(ax[i], ax[i - 5]) except Exception as e: print(f"Warning: Plotting error for {f}: {e}") ax[1].legend() fig.savefig(save_dir / "results.png", dpi=200) plt.close() ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/tal/__init__.py ================================================ # init ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/tal/anchor_generator.py ================================================ import torch from utils.general import check_version TORCH_1_10 = check_version(torch.__version__, '1.10.0') def make_anchors(feats, strides, grid_cell_offset=0.5): """Generate anchors from features.""" anchor_points, stride_tensor = [], [] assert feats is not None dtype, device = feats[0].dtype, feats[0].device for i, stride in enumerate(strides): _, _, h, w = feats[i].shape sx = torch.arange(end=w, device=device, dtype=dtype) + grid_cell_offset # shift x sy = torch.arange(end=h, device=device, dtype=dtype) + grid_cell_offset # shift y sy, sx = torch.meshgrid(sy, sx, indexing='ij') if TORCH_1_10 else torch.meshgrid(sy, sx) anchor_points.append(torch.stack((sx, sy), -1).view(-1, 2)) stride_tensor.append(torch.full((h * w, 1), stride, dtype=dtype, device=device)) return torch.cat(anchor_points), torch.cat(stride_tensor) def dist2bbox(distance, anchor_points, xywh=True, dim=-1): """Transform distance(ltrb) to box(xywh or xyxy).""" lt, rb = torch.split(distance, 2, dim) x1y1 = anchor_points - lt x2y2 = anchor_points + rb if xywh: c_xy = (x1y1 + x2y2) / 2 wh = x2y2 - x1y1 return torch.cat((c_xy, wh), dim) # xywh bbox return torch.cat((x1y1, x2y2), dim) # xyxy bbox def bbox2dist(anchor_points, bbox, reg_max): """Transform bbox(xyxy) to dist(ltrb).""" x1y1, x2y2 = torch.split(bbox, 2, -1) return torch.cat((anchor_points - x1y1, x2y2 - anchor_points), -1).clamp(0, reg_max - 0.01) # dist (lt, rb) ================================================ FILE: asone/detectors/yolov9/yolov9/utils/segment/tal/assigner.py ================================================ import torch import torch.nn as nn import torch.nn.functional as F from utils.metrics import bbox_iou def select_candidates_in_gts(xy_centers, gt_bboxes, eps=1e-9): """select the positive anchor center in gt Args: xy_centers (Tensor): shape(h*w, 4) gt_bboxes (Tensor): shape(b, n_boxes, 4) Return: (Tensor): shape(b, n_boxes, h*w) """ n_anchors = xy_centers.shape[0] bs, n_boxes, _ = gt_bboxes.shape lt, rb = gt_bboxes.view(-1, 1, 4).chunk(2, 2) # left-top, right-bottom bbox_deltas = torch.cat((xy_centers[None] - lt, rb - xy_centers[None]), dim=2).view(bs, n_boxes, n_anchors, -1) # return (bbox_deltas.min(3)[0] > eps).to(gt_bboxes.dtype) return bbox_deltas.amin(3).gt_(eps) def select_highest_overlaps(mask_pos, overlaps, n_max_boxes): """if an anchor box is assigned to multiple gts, the one with the highest iou will be selected. Args: mask_pos (Tensor): shape(b, n_max_boxes, h*w) overlaps (Tensor): shape(b, n_max_boxes, h*w) Return: target_gt_idx (Tensor): shape(b, h*w) fg_mask (Tensor): shape(b, h*w) mask_pos (Tensor): shape(b, n_max_boxes, h*w) """ # (b, n_max_boxes, h*w) -> (b, h*w) fg_mask = mask_pos.sum(-2) if fg_mask.max() > 1: # one anchor is assigned to multiple gt_bboxes mask_multi_gts = (fg_mask.unsqueeze(1) > 1).repeat([1, n_max_boxes, 1]) # (b, n_max_boxes, h*w) max_overlaps_idx = overlaps.argmax(1) # (b, h*w) is_max_overlaps = F.one_hot(max_overlaps_idx, n_max_boxes) # (b, h*w, n_max_boxes) is_max_overlaps = is_max_overlaps.permute(0, 2, 1).to(overlaps.dtype) # (b, n_max_boxes, h*w) mask_pos = torch.where(mask_multi_gts, is_max_overlaps, mask_pos) # (b, n_max_boxes, h*w) fg_mask = mask_pos.sum(-2) # find each grid serve which gt(index) target_gt_idx = mask_pos.argmax(-2) # (b, h*w) return target_gt_idx, fg_mask, mask_pos class TaskAlignedAssigner(nn.Module): def __init__(self, topk=13, num_classes=80, alpha=1.0, beta=6.0, eps=1e-9): super().__init__() self.topk = topk self.num_classes = num_classes self.bg_idx = num_classes self.alpha = alpha self.beta = beta self.eps = eps @torch.no_grad() def forward(self, pd_scores, pd_bboxes, anc_points, gt_labels, gt_bboxes, mask_gt): """This code referenced to https://github.com/Nioolek/PPYOLOE_pytorch/blob/master/ppyoloe/assigner/tal_assigner.py Args: pd_scores (Tensor): shape(bs, num_total_anchors, num_classes) pd_bboxes (Tensor): shape(bs, num_total_anchors, 4) anc_points (Tensor): shape(num_total_anchors, 2) gt_labels (Tensor): shape(bs, n_max_boxes, 1) gt_bboxes (Tensor): shape(bs, n_max_boxes, 4) mask_gt (Tensor): shape(bs, n_max_boxes, 1) Returns: target_labels (Tensor): shape(bs, num_total_anchors) target_bboxes (Tensor): shape(bs, num_total_anchors, 4) target_scores (Tensor): shape(bs, num_total_anchors, num_classes) fg_mask (Tensor): shape(bs, num_total_anchors) """ self.bs = pd_scores.size(0) self.n_max_boxes = gt_bboxes.size(1) if self.n_max_boxes == 0: device = gt_bboxes.device return (torch.full_like(pd_scores[..., 0], self.bg_idx).to(device), torch.zeros_like(pd_bboxes).to(device), torch.zeros_like(pd_scores).to(device), torch.zeros_like(pd_scores[..., 0]).to(device), torch.zeros_like(pd_scores[..., 0]).to(device)) mask_pos, align_metric, overlaps = self.get_pos_mask(pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt) target_gt_idx, fg_mask, mask_pos = select_highest_overlaps(mask_pos, overlaps, self.n_max_boxes) # assigned target target_labels, target_bboxes, target_scores = self.get_targets(gt_labels, gt_bboxes, target_gt_idx, fg_mask) # normalize align_metric *= mask_pos pos_align_metrics = align_metric.amax(axis=-1, keepdim=True) # b, max_num_obj pos_overlaps = (overlaps * mask_pos).amax(axis=-1, keepdim=True) # b, max_num_obj norm_align_metric = (align_metric * pos_overlaps / (pos_align_metrics + self.eps)).amax(-2).unsqueeze(-1) target_scores = target_scores * norm_align_metric return target_labels, target_bboxes, target_scores, fg_mask.bool(), target_gt_idx def get_pos_mask(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt): # get anchor_align metric, (b, max_num_obj, h*w) align_metric, overlaps = self.get_box_metrics(pd_scores, pd_bboxes, gt_labels, gt_bboxes) # get in_gts mask, (b, max_num_obj, h*w) mask_in_gts = select_candidates_in_gts(anc_points, gt_bboxes) # get topk_metric mask, (b, max_num_obj, h*w) mask_topk = self.select_topk_candidates(align_metric * mask_in_gts, topk_mask=mask_gt.repeat([1, 1, self.topk]).bool()) # merge all mask to a final mask, (b, max_num_obj, h*w) mask_pos = mask_topk * mask_in_gts * mask_gt return mask_pos, align_metric, overlaps def get_box_metrics(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes): gt_labels = gt_labels.to(torch.long) # b, max_num_obj, 1 ind = torch.zeros([2, self.bs, self.n_max_boxes], dtype=torch.long) # 2, b, max_num_obj ind[0] = torch.arange(end=self.bs).view(-1, 1).repeat(1, self.n_max_boxes) # b, max_num_obj ind[1] = gt_labels.squeeze(-1) # b, max_num_obj # get the scores of each grid for each gt cls bbox_scores = pd_scores[ind[0], :, ind[1]] # b, max_num_obj, h*w overlaps = bbox_iou(gt_bboxes.unsqueeze(2), pd_bboxes.unsqueeze(1), xywh=False, CIoU=True).squeeze(3).clamp(0) align_metric = bbox_scores.pow(self.alpha) * (overlaps).pow(self.beta) return align_metric, overlaps def select_topk_candidates(self, metrics, largest=True, topk_mask=None): """ Args: metrics: (b, max_num_obj, h*w). topk_mask: (b, max_num_obj, topk) or None """ num_anchors = metrics.shape[-1] # h*w # (b, max_num_obj, topk) topk_metrics, topk_idxs = torch.topk(metrics, self.topk, dim=-1, largest=largest) if topk_mask is None: topk_mask = (topk_metrics.max(-1, keepdim=True) > self.eps).tile([1, 1, self.topk]) # (b, max_num_obj, topk) topk_idxs = torch.where(topk_mask, topk_idxs, 0) # (b, max_num_obj, topk, h*w) -> (b, max_num_obj, h*w) is_in_topk = F.one_hot(topk_idxs, num_anchors).sum(-2) # filter invalid bboxes # assigned topk should be unique, this is for dealing with empty labels # since empty labels will generate index `0` through `F.one_hot` # NOTE: but what if the topk_idxs include `0`? is_in_topk = torch.where(is_in_topk > 1, 0, is_in_topk) return is_in_topk.to(metrics.dtype) def get_targets(self, gt_labels, gt_bboxes, target_gt_idx, fg_mask): """ Args: gt_labels: (b, max_num_obj, 1) gt_bboxes: (b, max_num_obj, 4) target_gt_idx: (b, h*w) fg_mask: (b, h*w) """ # assigned target labels, (b, 1) batch_ind = torch.arange(end=self.bs, dtype=torch.int64, device=gt_labels.device)[..., None] target_gt_idx = target_gt_idx + batch_ind * self.n_max_boxes # (b, h*w) target_labels = gt_labels.long().flatten()[target_gt_idx] # (b, h*w) # assigned target boxes, (b, max_num_obj, 4) -> (b, h*w) target_bboxes = gt_bboxes.view(-1, 4)[target_gt_idx] # assigned target scores target_labels.clamp(0) target_scores = F.one_hot(target_labels, self.num_classes) # (b, h*w, 80) fg_scores_mask = fg_mask[:, :, None].repeat(1, 1, self.num_classes) # (b, h*w, 80) target_scores = torch.where(fg_scores_mask > 0, target_scores, 0) return target_labels, target_bboxes, target_scores ================================================ FILE: asone/detectors/yolov9/yolov9/utils/tal/__init__.py ================================================ # init ================================================ FILE: asone/detectors/yolov9/yolov9/utils/tal/anchor_generator.py ================================================ import torch from asone.detectors.yolov9.yolov9.utils.general import check_version TORCH_1_10 = check_version(torch.__version__, '1.10.0') def make_anchors(feats, strides, grid_cell_offset=0.5): """Generate anchors from features.""" anchor_points, stride_tensor = [], [] assert feats is not None dtype, device = feats[0].dtype, feats[0].device for i, stride in enumerate(strides): _, _, h, w = feats[i].shape sx = torch.arange(end=w, device=device, dtype=dtype) + grid_cell_offset # shift x sy = torch.arange(end=h, device=device, dtype=dtype) + grid_cell_offset # shift y sy, sx = torch.meshgrid(sy, sx, indexing='ij') if TORCH_1_10 else torch.meshgrid(sy, sx) anchor_points.append(torch.stack((sx, sy), -1).view(-1, 2)) stride_tensor.append(torch.full((h * w, 1), stride, dtype=dtype, device=device)) return torch.cat(anchor_points), torch.cat(stride_tensor) def dist2bbox(distance, anchor_points, xywh=True, dim=-1): """Transform distance(ltrb) to box(xywh or xyxy).""" lt, rb = torch.split(distance, 2, dim) x1y1 = anchor_points - lt x2y2 = anchor_points + rb if xywh: c_xy = (x1y1 + x2y2) / 2 wh = x2y2 - x1y1 return torch.cat((c_xy, wh), dim) # xywh bbox return torch.cat((x1y1, x2y2), dim) # xyxy bbox def bbox2dist(anchor_points, bbox, reg_max): """Transform bbox(xyxy) to dist(ltrb).""" x1y1, x2y2 = torch.split(bbox, 2, -1) return torch.cat((anchor_points - x1y1, x2y2 - anchor_points), -1).clamp(0, reg_max - 0.01) # dist (lt, rb) ================================================ FILE: asone/detectors/yolov9/yolov9/utils/tal/assigner.py ================================================ import torch import torch.nn as nn import torch.nn.functional as F from utils.metrics import bbox_iou def select_candidates_in_gts(xy_centers, gt_bboxes, eps=1e-9): """select the positive anchor center in gt Args: xy_centers (Tensor): shape(h*w, 4) gt_bboxes (Tensor): shape(b, n_boxes, 4) Return: (Tensor): shape(b, n_boxes, h*w) """ n_anchors = xy_centers.shape[0] bs, n_boxes, _ = gt_bboxes.shape lt, rb = gt_bboxes.view(-1, 1, 4).chunk(2, 2) # left-top, right-bottom bbox_deltas = torch.cat((xy_centers[None] - lt, rb - xy_centers[None]), dim=2).view(bs, n_boxes, n_anchors, -1) # return (bbox_deltas.min(3)[0] > eps).to(gt_bboxes.dtype) return bbox_deltas.amin(3).gt_(eps) def select_highest_overlaps(mask_pos, overlaps, n_max_boxes): """if an anchor box is assigned to multiple gts, the one with the highest iou will be selected. Args: mask_pos (Tensor): shape(b, n_max_boxes, h*w) overlaps (Tensor): shape(b, n_max_boxes, h*w) Return: target_gt_idx (Tensor): shape(b, h*w) fg_mask (Tensor): shape(b, h*w) mask_pos (Tensor): shape(b, n_max_boxes, h*w) """ # (b, n_max_boxes, h*w) -> (b, h*w) fg_mask = mask_pos.sum(-2) if fg_mask.max() > 1: # one anchor is assigned to multiple gt_bboxes mask_multi_gts = (fg_mask.unsqueeze(1) > 1).repeat([1, n_max_boxes, 1]) # (b, n_max_boxes, h*w) max_overlaps_idx = overlaps.argmax(1) # (b, h*w) is_max_overlaps = F.one_hot(max_overlaps_idx, n_max_boxes) # (b, h*w, n_max_boxes) is_max_overlaps = is_max_overlaps.permute(0, 2, 1).to(overlaps.dtype) # (b, n_max_boxes, h*w) mask_pos = torch.where(mask_multi_gts, is_max_overlaps, mask_pos) # (b, n_max_boxes, h*w) fg_mask = mask_pos.sum(-2) # find each grid serve which gt(index) target_gt_idx = mask_pos.argmax(-2) # (b, h*w) return target_gt_idx, fg_mask, mask_pos class TaskAlignedAssigner(nn.Module): def __init__(self, topk=13, num_classes=80, alpha=1.0, beta=6.0, eps=1e-9): super().__init__() self.topk = topk self.num_classes = num_classes self.bg_idx = num_classes self.alpha = alpha self.beta = beta self.eps = eps @torch.no_grad() def forward(self, pd_scores, pd_bboxes, anc_points, gt_labels, gt_bboxes, mask_gt): """This code referenced to https://github.com/Nioolek/PPYOLOE_pytorch/blob/master/ppyoloe/assigner/tal_assigner.py Args: pd_scores (Tensor): shape(bs, num_total_anchors, num_classes) pd_bboxes (Tensor): shape(bs, num_total_anchors, 4) anc_points (Tensor): shape(num_total_anchors, 2) gt_labels (Tensor): shape(bs, n_max_boxes, 1) gt_bboxes (Tensor): shape(bs, n_max_boxes, 4) mask_gt (Tensor): shape(bs, n_max_boxes, 1) Returns: target_labels (Tensor): shape(bs, num_total_anchors) target_bboxes (Tensor): shape(bs, num_total_anchors, 4) target_scores (Tensor): shape(bs, num_total_anchors, num_classes) fg_mask (Tensor): shape(bs, num_total_anchors) """ self.bs = pd_scores.size(0) self.n_max_boxes = gt_bboxes.size(1) if self.n_max_boxes == 0: device = gt_bboxes.device return (torch.full_like(pd_scores[..., 0], self.bg_idx).to(device), torch.zeros_like(pd_bboxes).to(device), torch.zeros_like(pd_scores).to(device), torch.zeros_like(pd_scores[..., 0]).to(device)) mask_pos, align_metric, overlaps = self.get_pos_mask(pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt) target_gt_idx, fg_mask, mask_pos = select_highest_overlaps(mask_pos, overlaps, self.n_max_boxes) # assigned target target_labels, target_bboxes, target_scores = self.get_targets(gt_labels, gt_bboxes, target_gt_idx, fg_mask) # normalize align_metric *= mask_pos pos_align_metrics = align_metric.amax(axis=-1, keepdim=True) # b, max_num_obj pos_overlaps = (overlaps * mask_pos).amax(axis=-1, keepdim=True) # b, max_num_obj norm_align_metric = (align_metric * pos_overlaps / (pos_align_metrics + self.eps)).amax(-2).unsqueeze(-1) target_scores = target_scores * norm_align_metric return target_labels, target_bboxes, target_scores, fg_mask.bool() def get_pos_mask(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes, anc_points, mask_gt): # get anchor_align metric, (b, max_num_obj, h*w) align_metric, overlaps = self.get_box_metrics(pd_scores, pd_bboxes, gt_labels, gt_bboxes) # get in_gts mask, (b, max_num_obj, h*w) mask_in_gts = select_candidates_in_gts(anc_points, gt_bboxes) # get topk_metric mask, (b, max_num_obj, h*w) mask_topk = self.select_topk_candidates(align_metric * mask_in_gts, topk_mask=mask_gt.repeat([1, 1, self.topk]).bool()) # merge all mask to a final mask, (b, max_num_obj, h*w) mask_pos = mask_topk * mask_in_gts * mask_gt return mask_pos, align_metric, overlaps def get_box_metrics(self, pd_scores, pd_bboxes, gt_labels, gt_bboxes): gt_labels = gt_labels.to(torch.long) # b, max_num_obj, 1 ind = torch.zeros([2, self.bs, self.n_max_boxes], dtype=torch.long) # 2, b, max_num_obj ind[0] = torch.arange(end=self.bs).view(-1, 1).repeat(1, self.n_max_boxes) # b, max_num_obj ind[1] = gt_labels.squeeze(-1) # b, max_num_obj # get the scores of each grid for each gt cls bbox_scores = pd_scores[ind[0], :, ind[1]] # b, max_num_obj, h*w overlaps = bbox_iou(gt_bboxes.unsqueeze(2), pd_bboxes.unsqueeze(1), xywh=False, CIoU=True).squeeze(3).clamp(0) align_metric = bbox_scores.pow(self.alpha) * overlaps.pow(self.beta) return align_metric, overlaps def select_topk_candidates(self, metrics, largest=True, topk_mask=None): """ Args: metrics: (b, max_num_obj, h*w). topk_mask: (b, max_num_obj, topk) or None """ num_anchors = metrics.shape[-1] # h*w # (b, max_num_obj, topk) topk_metrics, topk_idxs = torch.topk(metrics, self.topk, dim=-1, largest=largest) if topk_mask is None: topk_mask = (topk_metrics.max(-1, keepdim=True) > self.eps).tile([1, 1, self.topk]) # (b, max_num_obj, topk) topk_idxs = torch.where(topk_mask, topk_idxs, 0) # (b, max_num_obj, topk, h*w) -> (b, max_num_obj, h*w) is_in_topk = F.one_hot(topk_idxs, num_anchors).sum(-2) # filter invalid bboxes # assigned topk should be unique, this is for dealing with empty labels # since empty labels will generate index `0` through `F.one_hot` # NOTE: but what if the topk_idxs include `0`? is_in_topk = torch.where(is_in_topk > 1, 0, is_in_topk) return is_in_topk.to(metrics.dtype) def get_targets(self, gt_labels, gt_bboxes, target_gt_idx, fg_mask): """ Args: gt_labels: (b, max_num_obj, 1) gt_bboxes: (b, max_num_obj, 4) target_gt_idx: (b, h*w) fg_mask: (b, h*w) """ # assigned target labels, (b, 1) batch_ind = torch.arange(end=self.bs, dtype=torch.int64, device=gt_labels.device)[..., None] target_gt_idx = target_gt_idx + batch_ind * self.n_max_boxes # (b, h*w) target_labels = gt_labels.long().flatten()[target_gt_idx] # (b, h*w) # assigned target boxes, (b, max_num_obj, 4) -> (b, h*w) target_bboxes = gt_bboxes.view(-1, 4)[target_gt_idx] # assigned target scores target_labels.clamp(0) target_scores = F.one_hot(target_labels, self.num_classes) # (b, h*w, 80) fg_scores_mask = fg_mask[:, :, None].repeat(1, 1, self.num_classes) # (b, h*w, 80) target_scores = torch.where(fg_scores_mask > 0, target_scores, 0) return target_labels, target_bboxes, target_scores ================================================ FILE: asone/detectors/yolov9/yolov9/utils/torch_utils.py ================================================ import math import os import platform import subprocess import time import warnings from contextlib import contextmanager from copy import deepcopy from pathlib import Path import torch import torch.distributed as dist import torch.nn as nn import torch.nn.functional as F from torch.nn.parallel import DistributedDataParallel as DDP from asone.detectors.yolov9.yolov9.utils.general import LOGGER, check_version, colorstr, file_date, git_describe from asone.detectors.yolov9.yolov9.utils.lion import Lion LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html RANK = int(os.getenv('RANK', -1)) WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1)) try: import thop # for FLOPs computation except ImportError: thop = None # Suppress PyTorch warnings warnings.filterwarnings('ignore', message='User provided device_type of \'cuda\', but CUDA is not available. Disabling') warnings.filterwarnings('ignore', category=UserWarning) def smart_inference_mode(torch_1_9=check_version(torch.__version__, '1.9.0')): # Applies torch.inference_mode() decorator if torch>=1.9.0 else torch.no_grad() decorator def decorate(fn): return (torch.inference_mode if torch_1_9 else torch.no_grad)()(fn) return decorate def smartCrossEntropyLoss(label_smoothing=0.0): # Returns nn.CrossEntropyLoss with label smoothing enabled for torch>=1.10.0 if check_version(torch.__version__, '1.10.0'): return nn.CrossEntropyLoss(label_smoothing=label_smoothing) if label_smoothing > 0: LOGGER.warning(f'WARNING ⚠️ label smoothing {label_smoothing} requires torch>=1.10.0') return nn.CrossEntropyLoss() def smart_DDP(model): # Model DDP creation with checks assert not check_version(torch.__version__, '1.12.0', pinned=True), \ 'torch==1.12.0 torchvision==0.13.0 DDP training is not supported due to a known issue. ' \ 'Please upgrade or downgrade torch to use DDP. See https://github.com/ultralytics/yolov5/issues/8395' if check_version(torch.__version__, '1.11.0'): return DDP(model, device_ids=[LOCAL_RANK], output_device=LOCAL_RANK, static_graph=True) else: return DDP(model, device_ids=[LOCAL_RANK], output_device=LOCAL_RANK) def reshape_classifier_output(model, n=1000): # Update a TorchVision classification model to class count 'n' if required from asone.detectors.yolov9.yolov9.models.common import Classify name, m = list((model.model if hasattr(model, 'model') else model).named_children())[-1] # last module if isinstance(m, Classify): # YOLOv5 Classify() head if m.linear.out_features != n: m.linear = nn.Linear(m.linear.in_features, n) elif isinstance(m, nn.Linear): # ResNet, EfficientNet if m.out_features != n: setattr(model, name, nn.Linear(m.in_features, n)) elif isinstance(m, nn.Sequential): types = [type(x) for x in m] if nn.Linear in types: i = types.index(nn.Linear) # nn.Linear index if m[i].out_features != n: m[i] = nn.Linear(m[i].in_features, n) elif nn.Conv2d in types: i = types.index(nn.Conv2d) # nn.Conv2d index if m[i].out_channels != n: m[i] = nn.Conv2d(m[i].in_channels, n, m[i].kernel_size, m[i].stride, bias=m[i].bias is not None) @contextmanager def torch_distributed_zero_first(local_rank: int): # Decorator to make all processes in distributed training wait for each local_master to do something if local_rank not in [-1, 0]: dist.barrier(device_ids=[local_rank]) yield if local_rank == 0: dist.barrier(device_ids=[0]) def device_count(): # Returns number of CUDA devices available. Safe version of torch.cuda.device_count(). Supports Linux and Windows assert platform.system() in ('Linux', 'Windows'), 'device_count() only supported on Linux or Windows' try: cmd = 'nvidia-smi -L | wc -l' if platform.system() == 'Linux' else 'nvidia-smi -L | find /c /v ""' # Windows return int(subprocess.run(cmd, shell=True, capture_output=True, check=True).stdout.decode().split()[-1]) except Exception: return 0 def select_device(device='', batch_size=0, newline=True): # device = None or 'cpu' or 0 or '0' or '0,1,2,3' s = f'YOLO 🚀 {git_describe() or file_date()} Python-{platform.python_version()} torch-{torch.__version__} ' device = str(device).strip().lower().replace('cuda:', '').replace('none', '') # to string, 'cuda:0' to '0' cpu = device == 'cpu' mps = device == 'mps' # Apple Metal Performance Shaders (MPS) if cpu or mps: os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False elif device: # non-cpu device requested os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable - must be before assert is_available() assert torch.cuda.is_available() and torch.cuda.device_count() >= len(device.replace(',', '')), \ f"Invalid CUDA '--device {device}' requested, use '--device cpu' or pass valid CUDA device(s)" if not cpu and not mps and torch.cuda.is_available(): # prefer GPU if available devices = device.split(',') if device else '0' # range(torch.cuda.device_count()) # i.e. 0,1,6,7 n = len(devices) # device count if n > 1 and batch_size > 0: # check batch_size is divisible by device_count assert batch_size % n == 0, f'batch-size {batch_size} not multiple of GPU count {n}' space = ' ' * (len(s) + 1) for i, d in enumerate(devices): p = torch.cuda.get_device_properties(i) s += f"{'' if i == 0 else space}CUDA:{d} ({p.name}, {p.total_memory / (1 << 20):.0f}MiB)\n" # bytes to MB arg = 'cuda:0' elif mps and getattr(torch, 'has_mps', False) and torch.backends.mps.is_available(): # prefer MPS if available s += 'MPS\n' arg = 'mps' else: # revert to CPU s += 'CPU\n' arg = 'cpu' if not newline: s = s.rstrip() LOGGER.info(s) return torch.device(arg) def time_sync(): # PyTorch-accurate time if torch.cuda.is_available(): torch.cuda.synchronize() return time.time() def profile(input, ops, n=10, device=None): """ YOLOv5 speed/memory/FLOPs profiler Usage: input = torch.randn(16, 3, 640, 640) m1 = lambda x: x * torch.sigmoid(x) m2 = nn.SiLU() profile(input, [m1, m2], n=100) # profile over 100 iterations """ results = [] if not isinstance(device, torch.device): device = select_device(device) print(f"{'Params':>12s}{'GFLOPs':>12s}{'GPU_mem (GB)':>14s}{'forward (ms)':>14s}{'backward (ms)':>14s}" f"{'input':>24s}{'output':>24s}") for x in input if isinstance(input, list) else [input]: x = x.to(device) x.requires_grad = True for m in ops if isinstance(ops, list) else [ops]: m = m.to(device) if hasattr(m, 'to') else m # device m = m.half() if hasattr(m, 'half') and isinstance(x, torch.Tensor) and x.dtype is torch.float16 else m tf, tb, t = 0, 0, [0, 0, 0] # dt forward, backward try: flops = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 # GFLOPs except Exception: flops = 0 try: for _ in range(n): t[0] = time_sync() y = m(x) t[1] = time_sync() try: _ = (sum(yi.sum() for yi in y) if isinstance(y, list) else y).sum().backward() t[2] = time_sync() except Exception: # no backward method # print(e) # for debug t[2] = float('nan') tf += (t[1] - t[0]) * 1000 / n # ms per op forward tb += (t[2] - t[1]) * 1000 / n # ms per op backward mem = torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0 # (GB) s_in, s_out = (tuple(x.shape) if isinstance(x, torch.Tensor) else 'list' for x in (x, y)) # shapes p = sum(x.numel() for x in m.parameters()) if isinstance(m, nn.Module) else 0 # parameters print(f'{p:12}{flops:12.4g}{mem:>14.3f}{tf:14.4g}{tb:14.4g}{str(s_in):>24s}{str(s_out):>24s}') results.append([p, flops, mem, tf, tb, s_in, s_out]) except Exception as e: print(e) results.append(None) torch.cuda.empty_cache() return results def is_parallel(model): # Returns True if model is of type DP or DDP return type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel) def de_parallel(model): # De-parallelize a model: returns single-GPU model if model is of type DP or DDP return model.module if is_parallel(model) else model def initialize_weights(model): for m in model.modules(): t = type(m) if t is nn.Conv2d: pass # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif t is nn.BatchNorm2d: m.eps = 1e-3 m.momentum = 0.03 elif t in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU]: m.inplace = True def find_modules(model, mclass=nn.Conv2d): # Finds layer indices matching module class 'mclass' return [i for i, m in enumerate(model.module_list) if isinstance(m, mclass)] def sparsity(model): # Return global model sparsity a, b = 0, 0 for p in model.parameters(): a += p.numel() b += (p == 0).sum() return b / a def prune(model, amount=0.3): # Prune model to requested global sparsity import torch.nn.utils.prune as prune for name, m in model.named_modules(): if isinstance(m, nn.Conv2d): prune.l1_unstructured(m, name='weight', amount=amount) # prune prune.remove(m, 'weight') # make permanent LOGGER.info(f'Model pruned to {sparsity(model):.3g} global sparsity') def fuse_conv_and_bn(conv, bn): # Fuse Conv2d() and BatchNorm2d() layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/ fusedconv = nn.Conv2d(conv.in_channels, conv.out_channels, kernel_size=conv.kernel_size, stride=conv.stride, padding=conv.padding, dilation=conv.dilation, groups=conv.groups, bias=True).requires_grad_(False).to(conv.weight.device) # Prepare filters w_conv = conv.weight.clone().view(conv.out_channels, -1) w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.shape)) # Prepare spatial bias b_conv = torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps)) fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) return fusedconv def model_info(model, verbose=False, imgsz=640): # Model information. img_size may be int or list, i.e. img_size=640 or img_size=[640, 320] n_p = sum(x.numel() for x in model.parameters()) # number parameters n_g = sum(x.numel() for x in model.parameters() if x.requires_grad) # number gradients if verbose: print(f"{'layer':>5} {'name':>40} {'gradient':>9} {'parameters':>12} {'shape':>20} {'mu':>10} {'sigma':>10}") for i, (name, p) in enumerate(model.named_parameters()): name = name.replace('module_list.', '') print('%5g %40s %9s %12g %20s %10.3g %10.3g' % (i, name, p.requires_grad, p.numel(), list(p.shape), p.mean(), p.std())) try: # FLOPs p = next(model.parameters()) stride = max(int(model.stride.max()), 32) if hasattr(model, 'stride') else 32 # max stride im = torch.empty((1, p.shape[1], stride, stride), device=p.device) # input image in BCHW format flops = thop.profile(deepcopy(model), inputs=(im,), verbose=False)[0] / 1E9 * 2 # stride GFLOPs imgsz = imgsz if isinstance(imgsz, list) else [imgsz, imgsz] # expand if int/float fs = f', {flops * imgsz[0] / stride * imgsz[1] / stride:.1f} GFLOPs' # 640x640 GFLOPs except Exception: fs = '' name = Path(model.yaml_file).stem.replace('yolov5', 'YOLOv5') if hasattr(model, 'yaml_file') else 'Model' LOGGER.info(f"{name} summary: {len(list(model.modules()))} layers, {n_p} parameters, {n_g} gradients{fs}") def scale_img(img, ratio=1.0, same_shape=False, gs=32): # img(16,3,256,416) # Scales img(bs,3,y,x) by ratio constrained to gs-multiple if ratio == 1.0: return img h, w = img.shape[2:] s = (int(h * ratio), int(w * ratio)) # new size img = F.interpolate(img, size=s, mode='bilinear', align_corners=False) # resize if not same_shape: # pad/crop img h, w = (math.ceil(x * ratio / gs) * gs for x in (h, w)) return F.pad(img, [0, w - s[1], 0, h - s[0]], value=0.447) # value = imagenet mean def copy_attr(a, b, include=(), exclude=()): # Copy attributes from b to a, options to only include [...] and to exclude [...] for k, v in b.__dict__.items(): if (len(include) and k not in include) or k.startswith('_') or k in exclude: continue else: setattr(a, k, v) def smart_optimizer(model, name='Adam', lr=0.001, momentum=0.9, decay=1e-5): # YOLOv5 3-param group optimizer: 0) weights with decay, 1) weights no decay, 2) biases no decay g = [], [], [] # optimizer parameter groups bn = tuple(v for k, v in nn.__dict__.items() if 'Norm' in k) # normalization layers, i.e. BatchNorm2d() #for v in model.modules(): # for p_name, p in v.named_parameters(recurse=0): # if p_name == 'bias': # bias (no decay) # g[2].append(p) # elif p_name == 'weight' and isinstance(v, bn): # weight (no decay) # g[1].append(p) # else: # g[0].append(p) # weight (with decay) for v in model.modules(): if hasattr(v, 'bias') and isinstance(v.bias, nn.Parameter): # bias (no decay) g[2].append(v.bias) if isinstance(v, bn): # weight (no decay) g[1].append(v.weight) elif hasattr(v, 'weight') and isinstance(v.weight, nn.Parameter): # weight (with decay) g[0].append(v.weight) if hasattr(v, 'im'): if hasattr(v.im, 'implicit'): g[1].append(v.im.implicit) else: for iv in v.im: g[1].append(iv.implicit) if hasattr(v, 'ia'): if hasattr(v.ia, 'implicit'): g[1].append(v.ia.implicit) else: for iv in v.ia: g[1].append(iv.implicit) if hasattr(v, 'im2'): if hasattr(v.im2, 'implicit'): g[1].append(v.im2.implicit) else: for iv in v.im2: g[1].append(iv.implicit) if hasattr(v, 'ia2'): if hasattr(v.ia2, 'implicit'): g[1].append(v.ia2.implicit) else: for iv in v.ia2: g[1].append(iv.implicit) if hasattr(v, 'im3'): if hasattr(v.im3, 'implicit'): g[1].append(v.im3.implicit) else: for iv in v.im3: g[1].append(iv.implicit) if hasattr(v, 'ia3'): if hasattr(v.ia3, 'implicit'): g[1].append(v.ia3.implicit) else: for iv in v.ia3: g[1].append(iv.implicit) if hasattr(v, 'im4'): if hasattr(v.im4, 'implicit'): g[1].append(v.im4.implicit) else: for iv in v.im4: g[1].append(iv.implicit) if hasattr(v, 'ia4'): if hasattr(v.ia4, 'implicit'): g[1].append(v.ia4.implicit) else: for iv in v.ia4: g[1].append(iv.implicit) if hasattr(v, 'im5'): if hasattr(v.im5, 'implicit'): g[1].append(v.im5.implicit) else: for iv in v.im5: g[1].append(iv.implicit) if hasattr(v, 'ia5'): if hasattr(v.ia5, 'implicit'): g[1].append(v.ia5.implicit) else: for iv in v.ia5: g[1].append(iv.implicit) if hasattr(v, 'im6'): if hasattr(v.im6, 'implicit'): g[1].append(v.im6.implicit) else: for iv in v.im6: g[1].append(iv.implicit) if hasattr(v, 'ia6'): if hasattr(v.ia6, 'implicit'): g[1].append(v.ia6.implicit) else: for iv in v.ia6: g[1].append(iv.implicit) if hasattr(v, 'im7'): if hasattr(v.im7, 'implicit'): g[1].append(v.im7.implicit) else: for iv in v.im7: g[1].append(iv.implicit) if hasattr(v, 'ia7'): if hasattr(v.ia7, 'implicit'): g[1].append(v.ia7.implicit) else: for iv in v.ia7: g[1].append(iv.implicit) if name == 'Adam': optimizer = torch.optim.Adam(g[2], lr=lr, betas=(momentum, 0.999)) # adjust beta1 to momentum elif name == 'AdamW': optimizer = torch.optim.AdamW(g[2], lr=lr, betas=(momentum, 0.999), weight_decay=0.0, amsgrad=True) elif name == 'RMSProp': optimizer = torch.optim.RMSprop(g[2], lr=lr, momentum=momentum) elif name == 'SGD': optimizer = torch.optim.SGD(g[2], lr=lr, momentum=momentum, nesterov=True) elif name == 'LION': optimizer = Lion(g[2], lr=lr, betas=(momentum, 0.99), weight_decay=0.0) else: raise NotImplementedError(f'Optimizer {name} not implemented.') optimizer.add_param_group({'params': g[0], 'weight_decay': decay}) # add g0 with weight_decay optimizer.add_param_group({'params': g[1], 'weight_decay': 0.0}) # add g1 (BatchNorm2d weights) LOGGER.info(f"{colorstr('optimizer:')} {type(optimizer).__name__}(lr={lr}) with parameter groups " f"{len(g[1])} weight(decay=0.0), {len(g[0])} weight(decay={decay}), {len(g[2])} bias") return optimizer def smart_hub_load(repo='ultralytics/yolov5', model='yolov5s', **kwargs): # YOLOv5 torch.hub.load() wrapper with smart error/issue handling if check_version(torch.__version__, '1.9.1'): kwargs['skip_validation'] = True # validation causes GitHub API rate limit errors if check_version(torch.__version__, '1.12.0'): kwargs['trust_repo'] = True # argument required starting in torch 0.12 try: return torch.hub.load(repo, model, **kwargs) except Exception: return torch.hub.load(repo, model, force_reload=True, **kwargs) def smart_resume(ckpt, optimizer, ema=None, weights='yolov5s.pt', epochs=300, resume=True): # Resume training from a partially trained checkpoint best_fitness = 0.0 start_epoch = ckpt['epoch'] + 1 if ckpt['optimizer'] is not None: optimizer.load_state_dict(ckpt['optimizer']) # optimizer best_fitness = ckpt['best_fitness'] if ema and ckpt.get('ema'): ema.ema.load_state_dict(ckpt['ema'].float().state_dict()) # EMA ema.updates = ckpt['updates'] if resume: assert start_epoch > 0, f'{weights} training to {epochs} epochs is finished, nothing to resume.\n' \ f"Start a new training without --resume, i.e. 'python train.py --weights {weights}'" LOGGER.info(f'Resuming training from {weights} from epoch {start_epoch} to {epochs} total epochs') if epochs < start_epoch: LOGGER.info(f"{weights} has been trained for {ckpt['epoch']} epochs. Fine-tuning for {epochs} more epochs.") epochs += ckpt['epoch'] # finetune additional epochs return best_fitness, start_epoch, epochs class EarlyStopping: # YOLOv5 simple early stopper def __init__(self, patience=30): self.best_fitness = 0.0 # i.e. mAP self.best_epoch = 0 self.patience = patience or float('inf') # epochs to wait after fitness stops improving to stop self.possible_stop = False # possible stop may occur next epoch def __call__(self, epoch, fitness): if fitness >= self.best_fitness: # >= 0 to allow for early zero-fitness stage of training self.best_epoch = epoch self.best_fitness = fitness delta = epoch - self.best_epoch # epochs without improvement self.possible_stop = delta >= (self.patience - 1) # possible stop may occur next epoch stop = delta >= self.patience # stop training if patience exceeded if stop: LOGGER.info(f'Stopping training early as no improvement observed in last {self.patience} epochs. ' f'Best results observed at epoch {self.best_epoch}, best model saved as best.pt.\n' f'To update EarlyStopping(patience={self.patience}) pass a new patience value, ' f'i.e. `python train.py --patience 300` or use `--patience 0` to disable EarlyStopping.') return stop class ModelEMA: """ Updated Exponential Moving Average (EMA) from https://github.com/rwightman/pytorch-image-models Keeps a moving average of everything in the model state_dict (parameters and buffers) For EMA details see https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage """ def __init__(self, model, decay=0.9999, tau=2000, updates=0): # Create EMA self.ema = deepcopy(de_parallel(model)).eval() # FP32 EMA self.updates = updates # number of EMA updates self.decay = lambda x: decay * (1 - math.exp(-x / tau)) # decay exponential ramp (to help early epochs) for p in self.ema.parameters(): p.requires_grad_(False) def update(self, model): # Update EMA parameters self.updates += 1 d = self.decay(self.updates) msd = de_parallel(model).state_dict() # model state_dict for k, v in self.ema.state_dict().items(): if v.dtype.is_floating_point: # true for FP16 and FP32 v *= d v += (1 - d) * msd[k].detach() # assert v.dtype == msd[k].dtype == torch.float32, f'{k}: EMA {v.dtype} and model {msd[k].dtype} must be FP32' def update_attr(self, model, include=(), exclude=('process_group', 'reducer')): # Update EMA attributes copy_attr(self.ema, model, include, exclude) ================================================ FILE: asone/detectors/yolov9/yolov9/utils/triton.py ================================================ import typing from urllib.parse import urlparse import torch class TritonRemoteModel: """ A wrapper over a model served by the Triton Inference Server. It can be configured to communicate over GRPC or HTTP. It accepts Torch Tensors as input and returns them as outputs. """ def __init__(self, url: str): """ Keyword arguments: url: Fully qualified address of the Triton server - for e.g. grpc://localhost:8000 """ parsed_url = urlparse(url) if parsed_url.scheme == "grpc": from tritonclient.grpc import InferenceServerClient, InferInput self.client = InferenceServerClient(parsed_url.netloc) # Triton GRPC client model_repository = self.client.get_model_repository_index() self.model_name = model_repository.models[0].name self.metadata = self.client.get_model_metadata(self.model_name, as_json=True) def create_input_placeholders() -> typing.List[InferInput]: return [ InferInput(i['name'], [int(s) for s in i["shape"]], i['datatype']) for i in self.metadata['inputs']] else: from tritonclient.http import InferenceServerClient, InferInput self.client = InferenceServerClient(parsed_url.netloc) # Triton HTTP client model_repository = self.client.get_model_repository_index() self.model_name = model_repository[0]['name'] self.metadata = self.client.get_model_metadata(self.model_name) def create_input_placeholders() -> typing.List[InferInput]: return [ InferInput(i['name'], [int(s) for s in i["shape"]], i['datatype']) for i in self.metadata['inputs']] self._create_input_placeholders_fn = create_input_placeholders @property def runtime(self): """Returns the model runtime""" return self.metadata.get("backend", self.metadata.get("platform")) def __call__(self, *args, **kwargs) -> typing.Union[torch.Tensor, typing.Tuple[torch.Tensor, ...]]: """ Invokes the model. Parameters can be provided via args or kwargs. args, if provided, are assumed to match the order of inputs of the model. kwargs are matched with the model input names. """ inputs = self._create_inputs(*args, **kwargs) response = self.client.infer(model_name=self.model_name, inputs=inputs) result = [] for output in self.metadata['outputs']: tensor = torch.as_tensor(response.as_numpy(output['name'])) result.append(tensor) return result[0] if len(result) == 1 else result def _create_inputs(self, *args, **kwargs): args_len, kwargs_len = len(args), len(kwargs) if not args_len and not kwargs_len: raise RuntimeError("No inputs provided.") if args_len and kwargs_len: raise RuntimeError("Cannot specify args and kwargs at the same time") placeholders = self._create_input_placeholders_fn() if args_len: if args_len != len(placeholders): raise RuntimeError(f"Expected {len(placeholders)} inputs, got {args_len}.") for input, value in zip(placeholders, args): input.set_data_from_numpy(value.cpu().numpy()) else: for input in placeholders: value = kwargs[input.name] input.set_data_from_numpy(value.cpu().numpy()) return placeholders ================================================ FILE: asone/detectors/yolov9/yolov9/utils/yolov9_utils.py ================================================ import cv2 import numpy as np import torch import torchvision import time from asone.detectors.yolov9.yolov9.utils.metrics import box_iou def prepare_input(image, input_shape): input_height, input_width = input_shape input_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Resize input image input_img = cv2.resize(input_img, (input_width, input_height)) # Scale input pixel values to 0 to 1 input_img = input_img / 255.0 input_img = input_img.transpose(2, 0, 1) input_tensor = input_img[np.newaxis, :, :, :].astype(np.float32) return input_tensor def process_output(output, ori_shape, input_shape, conf_threshold, iou_threshold): predictions = output[0] # predictions = np.squeeze(output[0]) # Filter out object confidence scores below threshold # obj_conf = predictions[:, 4] obj_conf = predictions[:, 6] # predictions = predictions[obj_conf > conf_threshold] # obj_conf = obj_conf[obj_conf > conf_threshold] # print(obj_conf[0]) # Multiply class confidence with bounding box confidence # predictions[:, 5] *= obj_conf[:, np.newaxis] # predictions[:, 6] *= obj_conf # Get the scores # scores = np.max(predictions[:, 5:], axis=1) scores = predictions[:, 6] # Filter out the objects with a low score predictions = predictions[obj_conf > conf_threshold] scores = scores[scores > conf_threshold] if len(scores) == 0: return [] # Get the class with the highest confidence # class_ids = np.argmax(predictions[:, 5:], axis=1) class_ids = predictions[:, 5].astype(np.uint16) # Extract boxes from predictions boxes = predictions[:, 1:5] # Scale boxes to original image dimensions boxes = rescale_boxes(boxes, ori_shape, input_shape) # Convert boxes to xyxy format # boxes = xywh2xyxy(boxes) # Apply non-maxima suppression to suppress weak, overlapping bounding boxes indices = nms(boxes, scores, iou_threshold) dets = [] for i in indices: dets.append([*boxes[i], scores[i], class_ids[i]]) # return boxes[indices], scores[indices], class_ids[indices] return np.array(dets) def rescale_boxes(boxes, ori_shape, input_shape): input_height, input_width = input_shape img_height, img_width = ori_shape # Rescale boxes to original image dimensions input_shape = np.array([input_width, input_height, input_width, input_height]) boxes = np.divide(boxes, input_shape, dtype=np.float32) boxes *= np.array([img_width, img_height, img_width, img_height]) return boxes def nms(boxes, scores, iou_threshold): # Sort by score sorted_indices = np.argsort(scores)[::-1] keep_boxes = [] while sorted_indices.size > 0: # Pick the last box box_id = sorted_indices[0] keep_boxes.append(box_id) # Compute IoU of the picked box with the rest ious = compute_iou(boxes[box_id, :], boxes[sorted_indices[1:], :]) # Remove boxes with IoU over the threshold keep_indices = np.where(ious < iou_threshold)[0] # print(keep_indices.shape, sorted_indices.shape) sorted_indices = sorted_indices[keep_indices + 1] return keep_boxes def compute_iou(box, boxes): # Compute xmin, ymin, xmax, ymax for both boxes xmin = np.maximum(box[0], boxes[:, 0]) ymin = np.maximum(box[1], boxes[:, 1]) xmax = np.minimum(box[2], boxes[:, 2]) ymax = np.minimum(box[3], boxes[:, 3]) # Compute intersection area intersection_area = np.maximum(0, xmax - xmin) * np.maximum(0, ymax - ymin) # Compute union area box_area = (box[2] - box[0]) * (box[3] - box[1]) boxes_area = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) union_area = box_area + boxes_area - intersection_area # Compute IoU iou = intersection_area / union_area return iou def xywh2xyxy(x): # Convert bounding box (x, y, w, h) to bounding box (x1, y1, x2, y2) y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[..., 0] = x[..., 0] - x[..., 2] / 2 y[..., 1] = x[..., 1] - x[..., 3] / 2 y[..., 2] = x[..., 0] + x[..., 2] / 2 y[..., 3] = x[..., 1] + x[..., 3] / 2 return y def non_max_suppression( prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False, labels=(), max_det=300, nm=0, # number of masks ): """Non-Maximum Suppression (NMS) on inference results to reject overlapping detections Returns: list of detections, on (n,6) tensor per image [xyxy, conf, cls] """ if isinstance(prediction, (list, tuple)): # YOLO model in validation model, output = (inference_out, loss_out) prediction = prediction[0] # select only inference output device = prediction.device mps = 'mps' in device.type # Apple MPS if mps: # MPS not fully supported yet, convert tensors to CPU before NMS prediction = prediction.cpu() bs = prediction.shape[0] # batch size nc = prediction.shape[1] - nm - 4 # number of classes mi = 4 + nc # mask start index xc = prediction[:, 4:mi].amax(1) > conf_thres # candidates # Checks assert 0 <= conf_thres <= 1, f'Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0' assert 0 <= iou_thres <= 1, f'Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0' # Settings # min_wh = 2 # (pixels) minimum box width and height max_wh = 7680 # (pixels) maximum box width and height max_nms = 30000 # maximum number of boxes into torchvision.ops.nms() time_limit = 2.5 + 0.05 * bs # seconds to quit after redundant = True # require redundant detections multi_label &= nc > 1 # multiple labels per box (adds 0.5ms/img) merge = False # use merge-NMS t = time.time() output = [torch.zeros((0, 6 + nm), device=prediction.device)] * bs for xi, x in enumerate(prediction): # image index, image inference # Apply constraints # x[((x[:, 2:4] < min_wh) | (x[:, 2:4] > max_wh)).any(1), 4] = 0 # width-height x = x.T[xc[xi]] # confidence # Cat apriori labels if autolabelling if labels and len(labels[xi]): lb = labels[xi] v = torch.zeros((len(lb), nc + nm + 5), device=x.device) v[:, :4] = lb[:, 1:5] # box v[range(len(lb)), lb[:, 0].long() + 4] = 1.0 # cls x = torch.cat((x, v), 0) # If none remain process next image if not x.shape[0]: continue # Detections matrix nx6 (xyxy, conf, cls) box, cls, mask = x.split((4, nc, nm), 1) box = xywh2xyxy(box) # center_x, center_y, width, height) to (x1, y1, x2, y2) if multi_label: i, j = (cls > conf_thres).nonzero(as_tuple=False).T x = torch.cat((box[i], x[i, 4 + j, None], j[:, None].float(), mask[i]), 1) else: # best class only conf, j = cls.max(1, keepdim=True) x = torch.cat((box, conf, j.float(), mask), 1)[conf.view(-1) > conf_thres] # Filter by class if classes is not None: x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)] # Apply finite constraint # if not torch.isfinite(x).all(): # x = x[torch.isfinite(x).all(1)] # Check shape n = x.shape[0] # number of boxes if not n: # no boxes continue elif n > max_nms: # excess boxes x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence else: x = x[x[:, 4].argsort(descending=True)] # sort by confidence # Batched NMS c = x[:, 5:6] * (0 if agnostic else max_wh) # classes boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS if i.shape[0] > max_det: # limit detections i = i[:max_det] if merge and (1 < n < 3E3): # Merge NMS (boxes merged using weighted mean) # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4) iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix weights = iou * scores[None] # box weights x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes if redundant: i = i[iou.sum(1) > 1] # require redundancy output[xi] = x[i] if mps: output[xi] = output[xi].to(device) return output ================================================ FILE: asone/detectors/yolov9/yolov9_detector.py ================================================ import os import sys import onnxruntime import torch import coremltools as ct from asone.utils import get_names import numpy as np import warnings from asone.detectors.yolov9.yolov9.utils.yolov9_utils import (prepare_input, process_output, non_max_suppression) from asone.detectors.yolov9.yolov9.models.experimental import attempt_load from asone import utils from PIL import Image from asone.detectors.utils.coreml_utils import yolo_to_xyxy, generalize_output_format, scale_bboxes from asone.utils.utils import PathResolver def xywh2xyxy(x): # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y return y # sys.path.append(os.path.join(os.path.dirname(__file__), 'yolov9')) class YOLOv9Detector: def __init__(self, weights=None, use_onnx=False, mlmodel=False, use_cuda=True): self.use_onnx = use_onnx self.mlmodel = mlmodel self.device = 'cuda' if use_cuda else 'cpu' #If incase weighst is a list of paths then select path at first index weights = str(weights[0] if isinstance(weights, list) else weights) if not os.path.exists(weights): utils.download_weights(weights) with PathResolver(): # Load Model self.model = self.load_model(use_cuda, weights) def load_model(self, use_cuda, weights, fp16=False): # Device: CUDA and if fp16=True only then half precision floating point works self.fp16 = fp16 & ((not self.use_onnx or self.use_onnx) and self.device != 'cpu') # Load onnx if self.use_onnx: if use_cuda: providers = ['CUDAExecutionProvider','CPUExecutionProvider'] else: providers = ['CPUExecutionProvider'] model = onnxruntime.InferenceSession(weights, providers=providers) # Load coreml elif self.mlmodel: model = ct.models.MLModel(weights) #Load Pytorch else: model = attempt_load(weights, device=self.device) model.half() if self.fp16 else model.float() return model def detect(self, image: list, input_shape: tuple = (640, 640), conf_thres: float = 0.25, iou_thres: float = 0.45, max_det: int = 1000, filter_classes: bool = None, agnostic_nms: bool = True, with_p6: bool = False, return_image=False) -> list: # Preprocess input image and also copying original image for later use original_image = image.copy() img_height, img_width = original_image.shape[:2] processed_image = prepare_input(image, input_shape) # Perform Inference on the Image if self.use_onnx: # Run ONNX model input_name = self.model.get_inputs()[0].name prediction = self.model.run([self.model.get_outputs()[0].name], { input_name: processed_image}) # Run Coreml model elif self.mlmodel: h ,w = image.shape[:2] pred = self.model.predict({"image":Image.fromarray(image).resize(input_shape)}) xyxy = yolo_to_xyxy(pred['coordinates'], input_shape) out = generalize_output_format(xyxy, pred['confidence'], conf_thres) if out != []: detections = scale_bboxes(out, image.shape[:2], input_shape) else: detections = np.empty((0, 6)) if filter_classes: class_names = get_names() filter_class_idx = [] if filter_classes: for _class in filter_classes: if _class.lower() in class_names: filter_class_idx.append(class_names.index(_class.lower())) else: warnings.warn(f"class {_class} not found in model classes list.") detections = detections[np.in1d(detections[:,5].astype(int), filter_class_idx)] return detections, {'width':w, 'height':h} # Run Pytorch model else: processed_image = torch.from_numpy(processed_image).to(self.device) # Change image floating point precision if fp16 set to true processed_image = processed_image.half() if self.fp16 else processed_image.float() with torch.no_grad(): prediction = self.model(processed_image, augment=False)[0] detection = [] # Postprocess prediction if self.use_onnx: detection = process_output(prediction, original_image.shape[:2], input_shape, conf_thres, iou_thres) else: detection = non_max_suppression(prediction, conf_thres, iou_thres, agnostic=agnostic_nms)[0] detection = detection.detach().cpu().numpy() # detection = yolo_to_xyxy(detection, input_shape) # print(detection) # Rescaling Bounding Boxes detection[:, :4] /= np.array([input_shape[1], input_shape[0], input_shape[1], input_shape[0]]) detection[:, :4] *= np.array([img_width, img_height, img_width, img_height]) image_info = { 'width': original_image.shape[1], 'height': original_image.shape[0], } if len(detection) > 0: self.boxes = detection[:, :4] self.scores = detection[:, 4:5] self.class_ids = detection[:, 5:6] if filter_classes: class_names = get_names() filter_class_idx = [] if filter_classes: for _class in filter_classes: if _class.lower() in class_names: filter_class_idx.append(class_names.index(_class.lower())) else: warnings.warn(f"class {_class} not found in model classes list.") detection = detection[np.in1d(detection[:,5].astype(int), filter_class_idx)] if return_image: return detection, original_image else: return detection, image_info ================================================ FILE: asone/detectors/yolox/__init__.py ================================================ from .yolox_detector import YOLOxDetector __all__ = ['YOLOxDetector'] ================================================ FILE: asone/detectors/yolox/exps/__init__.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. ================================================ FILE: asone/detectors/yolox/exps/yolov3.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import os import torch.nn as nn from asone.detectors.yolox.yolox.exp import Exp as MyExp class Exp(MyExp): def __init__(self): super(Exp, self).__init__() self.depth = 1.0 self.width = 1.0 self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0] def get_model(self, sublinear=False): def init_yolo(M): for m in M.modules(): if isinstance(m, nn.BatchNorm2d): m.eps = 1e-3 m.momentum = 0.03 if "model" not in self.__dict__: from asone.detectors.yolox.yolox.models import YOLOX, YOLOFPN, YOLOXHead backbone = YOLOFPN() head = YOLOXHead(self.num_classes, self.width, in_channels=[128, 256, 512], act="lrelu") self.model = YOLOX(backbone, head) self.model.apply(init_yolo) self.model.head.initialize_biases(1e-2) return self.model ================================================ FILE: asone/detectors/yolox/exps/yolox_l.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import os from asone.detectors.yolox.yolox.exp import Exp as MyExp class Exp(MyExp): def __init__(self): super(Exp, self).__init__() self.depth = 1.0 self.width = 1.0 self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0] ================================================ FILE: asone/detectors/yolox/exps/yolox_m.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import os from asone.detectors.yolox.yolox.exp import Exp as MyExp class Exp(MyExp): def __init__(self): super(Exp, self).__init__() self.depth = 0.67 self.width = 0.75 self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0] ================================================ FILE: asone/detectors/yolox/exps/yolox_nano.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import os import torch.nn as nn from asone.detectors.yolox.yolox.exp import Exp as MyExp class Exp(MyExp): def __init__(self): super(Exp, self).__init__() self.depth = 0.33 self.width = 0.25 self.input_size = (416, 416) self.random_size = (10, 20) self.mosaic_scale = (0.5, 1.5) self.test_size = (416, 416) self.mosaic_prob = 0.5 self.enable_mixup = False self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0] def get_model(self, sublinear=False): def init_yolo(M): for m in M.modules(): if isinstance(m, nn.BatchNorm2d): m.eps = 1e-3 m.momentum = 0.03 if "model" not in self.__dict__: from asone.detectors.yolox.yolox.models import YOLOX, YOLOPAFPN, YOLOXHead in_channels = [256, 512, 1024] # NANO model use depthwise = True, which is main difference. backbone = YOLOPAFPN( self.depth, self.width, in_channels=in_channels, act=self.act, depthwise=True, ) head = YOLOXHead( self.num_classes, self.width, in_channels=in_channels, act=self.act, depthwise=True ) self.model = YOLOX(backbone, head) self.model.apply(init_yolo) self.model.head.initialize_biases(1e-2) return self.model ================================================ FILE: asone/detectors/yolox/exps/yolox_s.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import os from asone.detectors.yolox.yolox.exp import Exp as MyExp class Exp(MyExp): def __init__(self): super(Exp, self).__init__() self.depth = 0.33 self.width = 0.50 self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0] ================================================ FILE: asone/detectors/yolox/exps/yolox_tiny.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import os from asone.detectors.yolox.yolox.exp import Exp as MyExp class Exp(MyExp): def __init__(self): super(Exp, self).__init__() self.depth = 0.33 self.width = 0.375 self.input_size = (416, 416) self.mosaic_scale = (0.5, 1.5) self.random_size = (10, 20) self.test_size = (416, 416) self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0] self.enable_mixup = False ================================================ FILE: asone/detectors/yolox/exps/yolox_x.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import os from asone.detectors.yolox.yolox.exp import Exp as MyExp class Exp(MyExp): def __init__(self): super(Exp, self).__init__() self.depth = 1.33 self.width = 1.25 self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0] ================================================ FILE: asone/detectors/yolox/yolox/__init__.py ================================================ ================================================ FILE: asone/detectors/yolox/yolox/core/__init__.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. from .launch import launch from .trainer import Trainer ================================================ FILE: asone/detectors/yolox/yolox/core/launch.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Code are based on # https://github.com/facebookresearch/detectron2/blob/master/detectron2/engine/launch.py # Copyright (c) Facebook, Inc. and its affiliates. # Copyright (c) Megvii, Inc. and its affiliates. import sys from datetime import timedelta from loguru import logger import torch import torch.distributed as dist import torch.multiprocessing as mp import yolox.utils.dist as comm __all__ = ["launch"] DEFAULT_TIMEOUT = timedelta(minutes=30) def _find_free_port(): """ Find an available port of current machine / node. """ import socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Binding to port 0 will cause the OS to find an available port for us sock.bind(("", 0)) port = sock.getsockname()[1] sock.close() # NOTE: there is still a chance the port could be taken by other processes. return port def launch( main_func, num_gpus_per_machine, num_machines=1, machine_rank=0, backend="nccl", dist_url=None, args=(), timeout=DEFAULT_TIMEOUT, ): """ Args: main_func: a function that will be called by `main_func(*args)` num_machines (int): the total number of machines machine_rank (int): the rank of this machine (one per machine) dist_url (str): url to connect to for distributed training, including protocol e.g. "tcp://127.0.0.1:8686". Can be set to auto to automatically select a free port on localhost args (tuple): arguments passed to main_func """ world_size = num_machines * num_gpus_per_machine if world_size > 1: # https://github.com/pytorch/pytorch/pull/14391 # TODO prctl in spawned processes if dist_url == "auto": assert ( num_machines == 1 ), "dist_url=auto cannot work with distributed training." port = _find_free_port() dist_url = f"tcp://127.0.0.1:{port}" start_method = "spawn" cache = vars(args[1]).get("cache", False) # To use numpy memmap for caching image into RAM, we have to use fork method if cache: assert sys.platform != "win32", ( "As Windows platform doesn't support fork method, " "do not add --cache in your training command." ) start_method = "fork" mp.start_processes( _distributed_worker, nprocs=num_gpus_per_machine, args=( main_func, world_size, num_gpus_per_machine, machine_rank, backend, dist_url, args, ), daemon=False, start_method=start_method, ) else: main_func(*args) def _distributed_worker( local_rank, main_func, world_size, num_gpus_per_machine, machine_rank, backend, dist_url, args, timeout=DEFAULT_TIMEOUT, ): assert ( torch.cuda.is_available() ), "cuda is not available. Please check your installation." global_rank = machine_rank * num_gpus_per_machine + local_rank logger.info("Rank {} initialization finished.".format(global_rank)) try: dist.init_process_group( backend=backend, init_method=dist_url, world_size=world_size, rank=global_rank, timeout=timeout, ) except Exception: logger.error("Process group URL: {}".format(dist_url)) raise # Setup the local process group (which contains ranks within the same machine) assert comm._LOCAL_PROCESS_GROUP is None num_machines = world_size // num_gpus_per_machine for i in range(num_machines): ranks_on_i = list( range(i * num_gpus_per_machine, (i + 1) * num_gpus_per_machine) ) pg = dist.new_group(ranks_on_i) if i == machine_rank: comm._LOCAL_PROCESS_GROUP = pg # synchronize is needed here to prevent a possible timeout after calling init_process_group # See: https://github.com/facebookresearch/maskrcnn-benchmark/issues/172 comm.synchronize() assert num_gpus_per_machine <= torch.cuda.device_count() torch.cuda.set_device(local_rank) main_func(*args) ================================================ FILE: asone/detectors/yolox/yolox/core/trainer.py ================================================ #!/usr/bin/env python3 # Copyright (c) Megvii, Inc. and its affiliates. import datetime import os import time from loguru import logger import torch from torch.nn.parallel import DistributedDataParallel as DDP from torch.utils.tensorboard import SummaryWriter from yolox.data import DataPrefetcher from yolox.exp import Exp from yolox.utils import ( MeterBuffer, ModelEMA, WandbLogger, adjust_status, all_reduce_norm, get_local_rank, get_model_info, get_rank, get_world_size, gpu_mem_usage, is_parallel, load_ckpt, mem_usage, occupy_mem, save_checkpoint, setup_logger, synchronize ) class Trainer: def __init__(self, exp: Exp, args): # init function only defines some basic attr, other attrs like model, optimizer are built in # before_train methods. self.exp = exp self.args = args # training related attr self.max_epoch = exp.max_epoch self.amp_training = args.fp16 self.scaler = torch.cuda.amp.GradScaler(enabled=args.fp16) self.is_distributed = get_world_size() > 1 self.rank = get_rank() self.local_rank = get_local_rank() self.device = "cuda:{}".format(self.local_rank) self.use_model_ema = exp.ema self.save_history_ckpt = exp.save_history_ckpt # data/dataloader related attr self.data_type = torch.float16 if args.fp16 else torch.float32 self.input_size = exp.input_size self.best_ap = 0 # metric record self.meter = MeterBuffer(window_size=exp.print_interval) self.file_name = os.path.join(exp.output_dir, args.experiment_name) if self.rank == 0: os.makedirs(self.file_name, exist_ok=True) setup_logger( self.file_name, distributed_rank=self.rank, filename="train_log.txt", mode="a", ) def train(self): self.before_train() try: self.train_in_epoch() except Exception: raise finally: self.after_train() def train_in_epoch(self): for self.epoch in range(self.start_epoch, self.max_epoch): self.before_epoch() self.train_in_iter() self.after_epoch() def train_in_iter(self): for self.iter in range(self.max_iter): self.before_iter() self.train_one_iter() self.after_iter() def train_one_iter(self): iter_start_time = time.time() inps, targets = self.prefetcher.next() inps = inps.to(self.data_type) targets = targets.to(self.data_type) targets.requires_grad = False inps, targets = self.exp.preprocess(inps, targets, self.input_size) data_end_time = time.time() with torch.cuda.amp.autocast(enabled=self.amp_training): outputs = self.model(inps, targets) loss = outputs["total_loss"] self.optimizer.zero_grad() self.scaler.scale(loss).backward() self.scaler.step(self.optimizer) self.scaler.update() if self.use_model_ema: self.ema_model.update(self.model) lr = self.lr_scheduler.update_lr(self.progress_in_iter + 1) for param_group in self.optimizer.param_groups: param_group["lr"] = lr iter_end_time = time.time() self.meter.update( iter_time=iter_end_time - iter_start_time, data_time=data_end_time - iter_start_time, lr=lr, **outputs, ) def before_train(self): logger.info("args: {}".format(self.args)) logger.info("exp value:\n{}".format(self.exp)) # model related init torch.cuda.set_device(self.local_rank) model = self.exp.get_model() logger.info( "Model Summary: {}".format(get_model_info(model, self.exp.test_size)) ) model.to(self.device) # solver related init self.optimizer = self.exp.get_optimizer(self.args.batch_size) # value of epoch will be set in `resume_train` model = self.resume_train(model) # data related init self.no_aug = self.start_epoch >= self.max_epoch - self.exp.no_aug_epochs self.train_loader = self.exp.get_data_loader( batch_size=self.args.batch_size, is_distributed=self.is_distributed, no_aug=self.no_aug, cache_img=self.args.cache, ) logger.info("init prefetcher, this might take one minute or less...") self.prefetcher = DataPrefetcher(self.train_loader) # max_iter means iters per epoch self.max_iter = len(self.train_loader) self.lr_scheduler = self.exp.get_lr_scheduler( self.exp.basic_lr_per_img * self.args.batch_size, self.max_iter ) if self.args.occupy: occupy_mem(self.local_rank) if self.is_distributed: model = DDP(model, device_ids=[self.local_rank], broadcast_buffers=False) if self.use_model_ema: self.ema_model = ModelEMA(model, 0.9998) self.ema_model.updates = self.max_iter * self.start_epoch self.model = model self.evaluator = self.exp.get_evaluator( batch_size=self.args.batch_size, is_distributed=self.is_distributed ) # Tensorboard and Wandb loggers if self.rank == 0: if self.args.logger == "tensorboard": self.tblogger = SummaryWriter(os.path.join(self.file_name, "tensorboard")) elif self.args.logger == "wandb": self.wandb_logger = WandbLogger.initialize_wandb_logger( self.args, self.exp, self.evaluator.dataloader.dataset ) else: raise ValueError("logger must be either 'tensorboard' or 'wandb'") logger.info("Training start...") logger.info("\n{}".format(model)) def after_train(self): logger.info( "Training of experiment is done and the best AP is {:.2f}".format(self.best_ap * 100) ) if self.rank == 0: if self.args.logger == "wandb": self.wandb_logger.finish() def before_epoch(self): logger.info("---> start train epoch{}".format(self.epoch + 1)) if self.epoch + 1 == self.max_epoch - self.exp.no_aug_epochs or self.no_aug: logger.info("--->No mosaic aug now!") self.train_loader.close_mosaic() logger.info("--->Add additional L1 loss now!") if self.is_distributed: self.model.module.head.use_l1 = True else: self.model.head.use_l1 = True self.exp.eval_interval = 1 if not self.no_aug: self.save_ckpt(ckpt_name="last_mosaic_epoch") def after_epoch(self): self.save_ckpt(ckpt_name="latest") if (self.epoch + 1) % self.exp.eval_interval == 0: all_reduce_norm(self.model) self.evaluate_and_save_model() def before_iter(self): pass def after_iter(self): """ `after_iter` contains two parts of logic: * log information * reset setting of resize """ # log needed information if (self.iter + 1) % self.exp.print_interval == 0: # TODO check ETA logic left_iters = self.max_iter * self.max_epoch - (self.progress_in_iter + 1) eta_seconds = self.meter["iter_time"].global_avg * left_iters eta_str = "ETA: {}".format(datetime.timedelta(seconds=int(eta_seconds))) progress_str = "epoch: {}/{}, iter: {}/{}".format( self.epoch + 1, self.max_epoch, self.iter + 1, self.max_iter ) loss_meter = self.meter.get_filtered_meter("loss") loss_str = ", ".join( ["{}: {:.1f}".format(k, v.latest) for k, v in loss_meter.items()] ) time_meter = self.meter.get_filtered_meter("time") time_str = ", ".join( ["{}: {:.3f}s".format(k, v.avg) for k, v in time_meter.items()] ) mem_str = "gpu mem: {:.0f}Mb, mem: {:.1f}Gb".format(gpu_mem_usage(), mem_usage()) logger.info( "{}, {}, {}, {}, lr: {:.3e}".format( progress_str, mem_str, time_str, loss_str, self.meter["lr"].latest, ) + (", size: {:d}, {}".format(self.input_size[0], eta_str)) ) if self.rank == 0: if self.args.logger == "tensorboard": self.tblogger.add_scalar( "train/lr", self.meter["lr"].latest, self.progress_in_iter) for k, v in loss_meter.items(): self.tblogger.add_scalar( f"train/{k}", v.latest, self.progress_in_iter) if self.args.logger == "wandb": metrics = {"train/" + k: v.latest for k, v in loss_meter.items()} metrics.update({ "train/lr": self.meter["lr"].latest }) self.wandb_logger.log_metrics(metrics, step=self.progress_in_iter) self.meter.clear_meters() # random resizing if (self.progress_in_iter + 1) % 10 == 0: self.input_size = self.exp.random_resize( self.train_loader, self.epoch, self.rank, self.is_distributed ) @property def progress_in_iter(self): return self.epoch * self.max_iter + self.iter def resume_train(self, model): if self.args.resume: logger.info("resume training") if self.args.ckpt is None: ckpt_file = os.path.join(self.file_name, "latest" + "_ckpt.pth") else: ckpt_file = self.args.ckpt ckpt = torch.load(ckpt_file, map_location=self.device) # resume the model/optimizer state dict model.load_state_dict(ckpt["model"]) self.optimizer.load_state_dict(ckpt["optimizer"]) self.best_ap = ckpt.pop("best_ap", 0) # resume the training states variables start_epoch = ( self.args.start_epoch - 1 if self.args.start_epoch is not None else ckpt["start_epoch"] ) self.start_epoch = start_epoch logger.info( "loaded checkpoint '{}' (epoch {})".format( self.args.resume, self.start_epoch ) ) # noqa else: if self.args.ckpt is not None: logger.info("loading checkpoint for fine tuning") ckpt_file = self.args.ckpt ckpt = torch.load(ckpt_file, map_location=self.device)["model"] model = load_ckpt(model, ckpt) self.start_epoch = 0 return model def evaluate_and_save_model(self): if self.use_model_ema: evalmodel = self.ema_model.ema else: evalmodel = self.model if is_parallel(evalmodel): evalmodel = evalmodel.module with adjust_status(evalmodel, training=False): (ap50_95, ap50, summary), predictions = self.exp.eval( evalmodel, self.evaluator, self.is_distributed, return_outputs=True ) update_best_ckpt = ap50_95 > self.best_ap self.best_ap = max(self.best_ap, ap50_95) if self.rank == 0: if self.args.logger == "tensorboard": self.tblogger.add_scalar("val/COCOAP50", ap50, self.epoch + 1) self.tblogger.add_scalar("val/COCOAP50_95", ap50_95, self.epoch + 1) if self.args.logger == "wandb": self.wandb_logger.log_metrics({ "val/COCOAP50": ap50, "val/COCOAP50_95": ap50_95, "train/epoch": self.epoch + 1, }) self.wandb_logger.log_images(predictions) logger.info("\n" + summary) synchronize() self.save_ckpt("last_epoch", update_best_ckpt, ap=ap50_95) if self.save_history_ckpt: self.save_ckpt(f"epoch_{self.epoch + 1}", ap=ap50_95) def save_ckpt(self, ckpt_name, update_best_ckpt=False, ap=None): if self.rank == 0: save_model = self.ema_model.ema if self.use_model_ema else self.model logger.info("Save weights to {}".format(self.file_name)) ckpt_state = { "start_epoch": self.epoch + 1, "model": save_model.state_dict(), "optimizer": self.optimizer.state_dict(), "best_ap": self.best_ap, "curr_ap": ap, } save_checkpoint( ckpt_state, update_best_ckpt, self.file_name, ckpt_name, ) if self.args.logger == "wandb": self.wandb_logger.save_checkpoint( self.file_name, ckpt_name, update_best_ckpt, metadata={ "epoch": self.epoch + 1, "optimizer": self.optimizer.state_dict(), "best_ap": self.best_ap, "curr_ap": ap } ) ================================================ FILE: asone/detectors/yolox/yolox/data/__init__.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. from .data_augment import TrainTransform, ValTransform from .data_prefetcher import DataPrefetcher from .dataloading import DataLoader, get_yolox_datadir, worker_init_reset_seed from .datasets import * from .samplers import InfiniteSampler, YoloBatchSampler ================================================ FILE: asone/detectors/yolox/yolox/data/data_augment.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. """ Data augmentation functionality. Passed as callable transformations to Dataset classes. The data augmentation procedures were interpreted from @weiliu89's SSD paper http://arxiv.org/abs/1512.02325 """ import math import random import cv2 import numpy as np from yolox.utils import xyxy2cxcywh def augment_hsv(img, hgain=5, sgain=30, vgain=30): hsv_augs = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] # random gains hsv_augs *= np.random.randint(0, 2, 3) # random selection of h, s, v hsv_augs = hsv_augs.astype(np.int16) img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV).astype(np.int16) img_hsv[..., 0] = (img_hsv[..., 0] + hsv_augs[0]) % 180 img_hsv[..., 1] = np.clip(img_hsv[..., 1] + hsv_augs[1], 0, 255) img_hsv[..., 2] = np.clip(img_hsv[..., 2] + hsv_augs[2], 0, 255) cv2.cvtColor(img_hsv.astype(img.dtype), cv2.COLOR_HSV2BGR, dst=img) # no return needed def get_aug_params(value, center=0): if isinstance(value, float): return random.uniform(center - value, center + value) elif len(value) == 2: return random.uniform(value[0], value[1]) else: raise ValueError( "Affine params should be either a sequence containing two values\ or single float values. Got {}".format(value) ) def get_affine_matrix( target_size, degrees=10, translate=0.1, scales=0.1, shear=10, ): twidth, theight = target_size # Rotation and Scale angle = get_aug_params(degrees) scale = get_aug_params(scales, center=1.0) if scale <= 0.0: raise ValueError("Argument scale should be positive") R = cv2.getRotationMatrix2D(angle=angle, center=(0, 0), scale=scale) M = np.ones([2, 3]) # Shear shear_x = math.tan(get_aug_params(shear) * math.pi / 180) shear_y = math.tan(get_aug_params(shear) * math.pi / 180) M[0] = R[0] + shear_y * R[1] M[1] = R[1] + shear_x * R[0] # Translation translation_x = get_aug_params(translate) * twidth # x translation (pixels) translation_y = get_aug_params(translate) * theight # y translation (pixels) M[0, 2] = translation_x M[1, 2] = translation_y return M, scale def apply_affine_to_bboxes(targets, target_size, M, scale): num_gts = len(targets) # warp corner points twidth, theight = target_size corner_points = np.ones((4 * num_gts, 3)) corner_points[:, :2] = targets[:, [0, 1, 2, 3, 0, 3, 2, 1]].reshape( 4 * num_gts, 2 ) # x1y1, x2y2, x1y2, x2y1 corner_points = corner_points @ M.T # apply affine transform corner_points = corner_points.reshape(num_gts, 8) # create new boxes corner_xs = corner_points[:, 0::2] corner_ys = corner_points[:, 1::2] new_bboxes = ( np.concatenate( (corner_xs.min(1), corner_ys.min(1), corner_xs.max(1), corner_ys.max(1)) ) .reshape(4, num_gts) .T ) # clip boxes new_bboxes[:, 0::2] = new_bboxes[:, 0::2].clip(0, twidth) new_bboxes[:, 1::2] = new_bboxes[:, 1::2].clip(0, theight) targets[:, :4] = new_bboxes return targets def random_affine( img, targets=(), target_size=(640, 640), degrees=10, translate=0.1, scales=0.1, shear=10, ): M, scale = get_affine_matrix(target_size, degrees, translate, scales, shear) img = cv2.warpAffine(img, M, dsize=target_size, borderValue=(114, 114, 114)) # Transform label coordinates if len(targets) > 0: targets = apply_affine_to_bboxes(targets, target_size, M, scale) return img, targets def _mirror(image, boxes, prob=0.5): _, width, _ = image.shape if random.random() < prob: image = image[:, ::-1] boxes[:, 0::2] = width - boxes[:, 2::-2] return image, boxes def preproc(img, input_size, swap=(2, 0, 1)): if len(img.shape) == 3: padded_img = np.ones((input_size[0], input_size[1], 3), dtype=np.uint8) * 114 else: padded_img = np.ones(input_size, dtype=np.uint8) * 114 r = min(input_size[0] / img.shape[0], input_size[1] / img.shape[1]) resized_img = cv2.resize( img, (int(img.shape[1] * r), int(img.shape[0] * r)), interpolation=cv2.INTER_LINEAR, ).astype(np.uint8) padded_img[: int(img.shape[0] * r), : int(img.shape[1] * r)] = resized_img padded_img = padded_img.transpose(swap) padded_img = np.ascontiguousarray(padded_img, dtype=np.float32) return padded_img, r class TrainTransform: def __init__(self, max_labels=50, flip_prob=0.5, hsv_prob=1.0): self.max_labels = max_labels self.flip_prob = flip_prob self.hsv_prob = hsv_prob def __call__(self, image, targets, input_dim): boxes = targets[:, :4].copy() labels = targets[:, 4].copy() if len(boxes) == 0: targets = np.zeros((self.max_labels, 5), dtype=np.float32) image, r_o = preproc(image, input_dim) return image, targets image_o = image.copy() targets_o = targets.copy() height_o, width_o, _ = image_o.shape boxes_o = targets_o[:, :4] labels_o = targets_o[:, 4] # bbox_o: [xyxy] to [c_x,c_y,w,h] boxes_o = xyxy2cxcywh(boxes_o) if random.random() < self.hsv_prob: augment_hsv(image) image_t, boxes = _mirror(image, boxes, self.flip_prob) height, width, _ = image_t.shape image_t, r_ = preproc(image_t, input_dim) # boxes [xyxy] 2 [cx,cy,w,h] boxes = xyxy2cxcywh(boxes) boxes *= r_ mask_b = np.minimum(boxes[:, 2], boxes[:, 3]) > 1 boxes_t = boxes[mask_b] labels_t = labels[mask_b] if len(boxes_t) == 0: image_t, r_o = preproc(image_o, input_dim) boxes_o *= r_o boxes_t = boxes_o labels_t = labels_o labels_t = np.expand_dims(labels_t, 1) targets_t = np.hstack((labels_t, boxes_t)) padded_labels = np.zeros((self.max_labels, 5)) padded_labels[range(len(targets_t))[: self.max_labels]] = targets_t[ : self.max_labels ] padded_labels = np.ascontiguousarray(padded_labels, dtype=np.float32) return image_t, padded_labels class ValTransform: """ Defines the transformations that should be applied to test PIL image for input into the network dimension -> tensorize -> color adj Arguments: resize (int): input dimension to SSD rgb_means ((int,int,int)): average RGB of the dataset (104,117,123) swap ((int,int,int)): final order of channels Returns: transform (transform) : callable transform to be applied to test/val data """ def __init__(self, swap=(2, 0, 1), legacy=False): self.swap = swap self.legacy = legacy # assume input is cv2 img for now def __call__(self, img, res, input_size): img, _ = preproc(img, input_size, self.swap) if self.legacy: img = img[::-1, :, :].copy() img /= 255.0 img -= np.array([0.485, 0.456, 0.406]).reshape(3, 1, 1) img /= np.array([0.229, 0.224, 0.225]).reshape(3, 1, 1) return img, np.zeros((1, 5)) ================================================ FILE: asone/detectors/yolox/yolox/data/data_prefetcher.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import torch class DataPrefetcher: """ DataPrefetcher is inspired by code of following file: https://github.com/NVIDIA/apex/blob/master/examples/imagenet/main_amp.py It could speedup your pytorch dataloader. For more information, please check https://github.com/NVIDIA/apex/issues/304#issuecomment-493562789. """ def __init__(self, loader): self.loader = iter(loader) self.stream = torch.cuda.Stream() self.input_cuda = self._input_cuda_for_image self.record_stream = DataPrefetcher._record_stream_for_image self.preload() def preload(self): try: self.next_input, self.next_target, _, _ = next(self.loader) except StopIteration: self.next_input = None self.next_target = None return with torch.cuda.stream(self.stream): self.input_cuda() self.next_target = self.next_target.cuda(non_blocking=True) def next(self): torch.cuda.current_stream().wait_stream(self.stream) input = self.next_input target = self.next_target if input is not None: self.record_stream(input) if target is not None: target.record_stream(torch.cuda.current_stream()) self.preload() return input, target def _input_cuda_for_image(self): self.next_input = self.next_input.cuda(non_blocking=True) @staticmethod def _record_stream_for_image(input): input.record_stream(torch.cuda.current_stream()) ================================================ FILE: asone/detectors/yolox/yolox/data/dataloading.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import os import random import uuid import numpy as np import torch from torch.utils.data.dataloader import DataLoader as torchDataLoader from torch.utils.data.dataloader import default_collate from .samplers import YoloBatchSampler def get_yolox_datadir(): """ get dataset dir of YOLOX. If environment variable named `YOLOX_DATADIR` is set, this function will return value of the environment variable. Otherwise, use data """ yolox_datadir = os.getenv("YOLOX_DATADIR", None) if yolox_datadir is None: import yolox yolox_path = os.path.dirname(os.path.dirname(yolox.__file__)) yolox_datadir = os.path.join(yolox_path, "datasets") return yolox_datadir class DataLoader(torchDataLoader): """ Lightnet dataloader that enables on the fly resizing of the images. See :class:`torch.utils.data.DataLoader` for more information on the arguments. Check more on the following website: https://gitlab.com/EAVISE/lightnet/-/blob/master/lightnet/data/_dataloading.py """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__initialized = False shuffle = False batch_sampler = None if len(args) > 5: shuffle = args[2] sampler = args[3] batch_sampler = args[4] elif len(args) > 4: shuffle = args[2] sampler = args[3] if "batch_sampler" in kwargs: batch_sampler = kwargs["batch_sampler"] elif len(args) > 3: shuffle = args[2] if "sampler" in kwargs: sampler = kwargs["sampler"] if "batch_sampler" in kwargs: batch_sampler = kwargs["batch_sampler"] else: if "shuffle" in kwargs: shuffle = kwargs["shuffle"] if "sampler" in kwargs: sampler = kwargs["sampler"] if "batch_sampler" in kwargs: batch_sampler = kwargs["batch_sampler"] # Use custom BatchSampler if batch_sampler is None: if sampler is None: if shuffle: sampler = torch.utils.data.sampler.RandomSampler(self.dataset) # sampler = torch.utils.data.DistributedSampler(self.dataset) else: sampler = torch.utils.data.sampler.SequentialSampler(self.dataset) batch_sampler = YoloBatchSampler( sampler, self.batch_size, self.drop_last, input_dimension=self.dataset.input_dim, ) # batch_sampler = IterationBasedBatchSampler(batch_sampler, num_iterations = self.batch_sampler = batch_sampler self.__initialized = True def close_mosaic(self): self.batch_sampler.mosaic = False def list_collate(batch): """ Function that collates lists or tuples together into one list (of lists/tuples). Use this as the collate function in a Dataloader, if you want to have a list of items as an output, as opposed to tensors (eg. Brambox.boxes). """ items = list(zip(*batch)) for i in range(len(items)): if isinstance(items[i][0], (list, tuple)): items[i] = list(items[i]) else: items[i] = default_collate(items[i]) return items def worker_init_reset_seed(worker_id): seed = uuid.uuid4().int % 2**32 random.seed(seed) torch.set_rng_state(torch.manual_seed(seed).get_state()) np.random.seed(seed) ================================================ FILE: asone/detectors/yolox/yolox/data/datasets/__init__.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. from .coco import COCODataset from .coco_classes import COCO_CLASSES from .datasets_wrapper import CacheDataset, ConcatDataset, Dataset, MixConcatDataset from .mosaicdetection import MosaicDetection from .voc import VOCDetection ================================================ FILE: asone/detectors/yolox/yolox/data/datasets/coco.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import copy import os import cv2 import numpy as np from pycocotools.coco import COCO from ..dataloading import get_yolox_datadir from .datasets_wrapper import CacheDataset, cache_read_img def remove_useless_info(coco): """ Remove useless info in coco dataset. COCO object is modified inplace. This function is mainly used for saving memory (save about 30% mem). """ if isinstance(coco, COCO): dataset = coco.dataset dataset.pop("info", None) dataset.pop("licenses", None) for img in dataset["images"]: img.pop("license", None) img.pop("coco_url", None) img.pop("date_captured", None) img.pop("flickr_url", None) if "annotations" in coco.dataset: for anno in coco.dataset["annotations"]: anno.pop("segmentation", None) class COCODataset(CacheDataset): """ COCO dataset class. """ def __init__( self, data_dir=None, json_file="instances_train2017.json", name="train2017", img_size=(416, 416), preproc=None, cache=False, cache_type="ram", ): """ COCO dataset initialization. Annotation data are read into memory by COCO API. Args: data_dir (str): dataset root directory json_file (str): COCO json file name name (str): COCO data name (e.g. 'train2017' or 'val2017') img_size (int): target image size after pre-processing preproc: data augmentation strategy """ if data_dir is None: data_dir = os.path.join(get_yolox_datadir(), "COCO") self.data_dir = data_dir self.json_file = json_file self.coco = COCO(os.path.join(self.data_dir, "annotations", self.json_file)) remove_useless_info(self.coco) self.ids = self.coco.getImgIds() self.num_imgs = len(self.ids) self.class_ids = sorted(self.coco.getCatIds()) self.cats = self.coco.loadCats(self.coco.getCatIds()) self._classes = tuple([c["name"] for c in self.cats]) self.name = name self.img_size = img_size self.preproc = preproc self.annotations = self._load_coco_annotations() path_filename = [os.path.join(name, anno[3]) for anno in self.annotations] super().__init__( input_dimension=img_size, num_imgs=self.num_imgs, data_dir=data_dir, cache_dir_name=f"cache_{name}", path_filename=path_filename, cache=cache, cache_type=cache_type ) def __len__(self): return self.num_imgs def _load_coco_annotations(self): return [self.load_anno_from_ids(_ids) for _ids in self.ids] def load_anno_from_ids(self, id_): im_ann = self.coco.loadImgs(id_)[0] width = im_ann["width"] height = im_ann["height"] anno_ids = self.coco.getAnnIds(imgIds=[int(id_)], iscrowd=False) annotations = self.coco.loadAnns(anno_ids) objs = [] for obj in annotations: x1 = np.max((0, obj["bbox"][0])) y1 = np.max((0, obj["bbox"][1])) x2 = np.min((width, x1 + np.max((0, obj["bbox"][2])))) y2 = np.min((height, y1 + np.max((0, obj["bbox"][3])))) if obj["area"] > 0 and x2 >= x1 and y2 >= y1: obj["clean_bbox"] = [x1, y1, x2, y2] objs.append(obj) num_objs = len(objs) res = np.zeros((num_objs, 5)) for ix, obj in enumerate(objs): cls = self.class_ids.index(obj["category_id"]) res[ix, 0:4] = obj["clean_bbox"] res[ix, 4] = cls r = min(self.img_size[0] / height, self.img_size[1] / width) res[:, :4] *= r img_info = (height, width) resized_info = (int(height * r), int(width * r)) file_name = ( im_ann["file_name"] if "file_name" in im_ann else "{:012}".format(id_) + ".jpg" ) return (res, img_info, resized_info, file_name) def load_anno(self, index): return self.annotations[index][0] def load_resized_img(self, index): img = self.load_image(index) r = min(self.img_size[0] / img.shape[0], self.img_size[1] / img.shape[1]) resized_img = cv2.resize( img, (int(img.shape[1] * r), int(img.shape[0] * r)), interpolation=cv2.INTER_LINEAR, ).astype(np.uint8) return resized_img def load_image(self, index): file_name = self.annotations[index][3] img_file = os.path.join(self.data_dir, self.name, file_name) img = cv2.imread(img_file) assert img is not None, f"file named {img_file} not found" return img @cache_read_img(use_cache=True) def read_img(self, index): return self.load_resized_img(index) def pull_item(self, index): id_ = self.ids[index] label, origin_image_size, _, _ = self.annotations[index] img = self.read_img(index) return img, copy.deepcopy(label), origin_image_size, np.array([id_]) @CacheDataset.mosaic_getitem def __getitem__(self, index): """ One image / label pair for the given index is picked up and pre-processed. Args: index (int): data index Returns: img (numpy.ndarray): pre-processed image padded_labels (torch.Tensor): pre-processed label data. The shape is :math:`[max_labels, 5]`. each label consists of [class, xc, yc, w, h]: class (float): class index. xc, yc (float) : center of bbox whose values range from 0 to 1. w, h (float) : size of bbox whose values range from 0 to 1. info_img : tuple of h, w. h, w (int): original shape of the image img_id (int): same as the input index. Used for evaluation. """ img, target, img_info, img_id = self.pull_item(index) if self.preproc is not None: img, target = self.preproc(img, target, self.input_dim) return img, target, img_info, img_id ================================================ FILE: asone/detectors/yolox/yolox/data/datasets/coco_classes.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. COCO_CLASSES = ( "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush", ) ================================================ FILE: asone/detectors/yolox/yolox/data/datasets/datasets_wrapper.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import bisect import copy import os import random from abc import ABCMeta, abstractmethod from functools import partial, wraps from multiprocessing.pool import ThreadPool import psutil from loguru import logger from tqdm import tqdm import numpy as np from torch.utils.data.dataset import ConcatDataset as torchConcatDataset from torch.utils.data.dataset import Dataset as torchDataset class ConcatDataset(torchConcatDataset): def __init__(self, datasets): super(ConcatDataset, self).__init__(datasets) if hasattr(self.datasets[0], "input_dim"): self._input_dim = self.datasets[0].input_dim self.input_dim = self.datasets[0].input_dim def pull_item(self, idx): if idx < 0: if -idx > len(self): raise ValueError( "absolute value of index should not exceed dataset length" ) idx = len(self) + idx dataset_idx = bisect.bisect_right(self.cumulative_sizes, idx) if dataset_idx == 0: sample_idx = idx else: sample_idx = idx - self.cumulative_sizes[dataset_idx - 1] return self.datasets[dataset_idx].pull_item(sample_idx) class MixConcatDataset(torchConcatDataset): def __init__(self, datasets): super(MixConcatDataset, self).__init__(datasets) if hasattr(self.datasets[0], "input_dim"): self._input_dim = self.datasets[0].input_dim self.input_dim = self.datasets[0].input_dim def __getitem__(self, index): if not isinstance(index, int): idx = index[1] if idx < 0: if -idx > len(self): raise ValueError( "absolute value of index should not exceed dataset length" ) idx = len(self) + idx dataset_idx = bisect.bisect_right(self.cumulative_sizes, idx) if dataset_idx == 0: sample_idx = idx else: sample_idx = idx - self.cumulative_sizes[dataset_idx - 1] if not isinstance(index, int): index = (index[0], sample_idx, index[2]) return self.datasets[dataset_idx][index] class Dataset(torchDataset): """ This class is a subclass of the base :class:`torch.utils.data.Dataset`, that enables on the fly resizing of the ``input_dim``. Args: input_dimension (tuple): (width,height) tuple with default dimensions of the network """ def __init__(self, input_dimension, mosaic=True): super().__init__() self.__input_dim = input_dimension[:2] self.enable_mosaic = mosaic @property def input_dim(self): """ Dimension that can be used by transforms to set the correct image size, etc. This allows transforms to have a single source of truth for the input dimension of the network. Return: list: Tuple containing the current width,height """ if hasattr(self, "_input_dim"): return self._input_dim return self.__input_dim @staticmethod def mosaic_getitem(getitem_fn): """ Decorator method that needs to be used around the ``__getitem__`` method. |br| This decorator enables the closing mosaic Example: >>> class CustomSet(ln.data.Dataset): ... def __len__(self): ... return 10 ... @ln.data.Dataset.mosaic_getitem ... def __getitem__(self, index): ... return self.enable_mosaic """ @wraps(getitem_fn) def wrapper(self, index): if not isinstance(index, int): self.enable_mosaic = index[0] index = index[1] ret_val = getitem_fn(self, index) return ret_val return wrapper class CacheDataset(Dataset, metaclass=ABCMeta): """ This class is a subclass of the base :class:`yolox.data.datasets.Dataset`, that enables cache images to ram or disk. Args: input_dimension (tuple): (width,height) tuple with default dimensions of the network num_imgs (int): datset size data_dir (str): the root directory of the dataset, e.g. `/path/to/COCO`. cache_dir_name (str): the name of the directory to cache to disk, e.g. `"custom_cache"`. The files cached to disk will be saved under `/path/to/COCO/custom_cache`. path_filename (str): a list of paths to the data relative to the `data_dir`, e.g. if you have data `/path/to/COCO/train/1.jpg`, `/path/to/COCO/train/2.jpg`, then `path_filename = ['train/1.jpg', ' train/2.jpg']`. cache (bool): whether to cache the images to ram or disk. cache_type (str): the type of cache, "ram" : Caching imgs to ram for fast training. "disk": Caching imgs to disk for fast training. """ def __init__( self, input_dimension, num_imgs=None, data_dir=None, cache_dir_name=None, path_filename=None, cache=False, cache_type="ram", ): super().__init__(input_dimension) self.cache = cache self.cache_type = cache_type if self.cache and self.cache_type == "disk": self.cache_dir = os.path.join(data_dir, cache_dir_name) self.path_filename = path_filename if self.cache and self.cache_type == "ram": self.imgs = None if self.cache: self.cache_images( num_imgs=num_imgs, data_dir=data_dir, cache_dir_name=cache_dir_name, path_filename=path_filename, ) def __del__(self): if self.cache and self.cache_type == "ram": del self.imgs @abstractmethod def read_img(self, index): """ Given index, return the corresponding image Args: index (int): image index """ raise NotImplementedError def cache_images( self, num_imgs=None, data_dir=None, cache_dir_name=None, path_filename=None, ): assert num_imgs is not None, "num_imgs must be specified as the size of the dataset" if self.cache_type == "disk": assert (data_dir and cache_dir_name and path_filename) is not None, \ "data_dir, cache_name and path_filename must be specified if cache_type is disk" self.path_filename = path_filename mem = psutil.virtual_memory() mem_required = self.cal_cache_occupy(num_imgs) gb = 1 << 30 if self.cache_type == "ram": if mem_required > mem.available: self.cache = False else: logger.info( f"{mem_required / gb:.1f}GB RAM required, " f"{mem.available / gb:.1f}/{mem.total / gb:.1f}GB RAM available, " f"Since the first thing we do is cache, " f"there is no guarantee that the remaining memory space is sufficient" ) if self.cache and self.imgs is None: if self.cache_type == 'ram': self.imgs = [None] * num_imgs logger.info("You are using cached images in RAM to accelerate training!") else: # 'disk' if not os.path.exists(self.cache_dir): os.mkdir(self.cache_dir) logger.warning( f"\n*******************************************************************\n" f"You are using cached images in DISK to accelerate training.\n" f"This requires large DISK space.\n" f"Make sure you have {mem_required / gb:.1f} " f"available DISK space for training your dataset.\n" f"*******************************************************************\\n" ) else: logger.info(f"Found disk cache at {self.cache_dir}") return logger.info( "Caching images...\n" "This might take some time for your dataset" ) num_threads = min(8, max(1, os.cpu_count() - 1)) b = 0 load_imgs = ThreadPool(num_threads).imap( partial(self.read_img, use_cache=False), range(num_imgs) ) pbar = tqdm(enumerate(load_imgs), total=num_imgs) for i, x in pbar: # x = self.read_img(self, i, use_cache=False) if self.cache_type == 'ram': self.imgs[i] = x else: # 'disk' cache_filename = f'{self.path_filename[i].split(".")[0]}.npy' cache_path_filename = os.path.join(self.cache_dir, cache_filename) os.makedirs(os.path.dirname(cache_path_filename), exist_ok=True) np.save(cache_path_filename, x) b += x.nbytes pbar.desc = \ f'Caching images ({b / gb:.1f}/{mem_required / gb:.1f}GB {self.cache_type})' pbar.close() def cal_cache_occupy(self, num_imgs): cache_bytes = 0 num_samples = min(num_imgs, 32) for _ in range(num_samples): img = self.read_img(index=random.randint(0, num_imgs - 1), use_cache=False) cache_bytes += img.nbytes mem_required = cache_bytes * num_imgs / num_samples return mem_required def cache_read_img(use_cache=True): def decorator(read_img_fn): """ Decorate the read_img function to cache the image Args: read_img_fn: read_img function use_cache (bool, optional): For the decorated read_img function, whether to read the image from cache. Defaults to True. """ @wraps(read_img_fn) def wrapper(self, index, use_cache=use_cache): cache = self.cache and use_cache if cache: if self.cache_type == "ram": img = self.imgs[index] img = copy.deepcopy(img) elif self.cache_type == "disk": img = np.load( os.path.join( self.cache_dir, f"{self.path_filename[index].split('.')[0]}.npy")) else: raise ValueError(f"Unknown cache type: {self.cache_type}") else: img = read_img_fn(self, index) return img return wrapper return decorator ================================================ FILE: asone/detectors/yolox/yolox/data/datasets/mosaicdetection.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import random import cv2 import numpy as np from yolox.utils import adjust_box_anns, get_local_rank from ..data_augment import random_affine from .datasets_wrapper import Dataset def get_mosaic_coordinate(mosaic_image, mosaic_index, xc, yc, w, h, input_h, input_w): # TODO update doc # index0 to top left part of image if mosaic_index == 0: x1, y1, x2, y2 = max(xc - w, 0), max(yc - h, 0), xc, yc small_coord = w - (x2 - x1), h - (y2 - y1), w, h # index1 to top right part of image elif mosaic_index == 1: x1, y1, x2, y2 = xc, max(yc - h, 0), min(xc + w, input_w * 2), yc small_coord = 0, h - (y2 - y1), min(w, x2 - x1), h # index2 to bottom left part of image elif mosaic_index == 2: x1, y1, x2, y2 = max(xc - w, 0), yc, xc, min(input_h * 2, yc + h) small_coord = w - (x2 - x1), 0, w, min(y2 - y1, h) # index2 to bottom right part of image elif mosaic_index == 3: x1, y1, x2, y2 = xc, yc, min(xc + w, input_w * 2), min(input_h * 2, yc + h) # noqa small_coord = 0, 0, min(w, x2 - x1), min(y2 - y1, h) return (x1, y1, x2, y2), small_coord class MosaicDetection(Dataset): """Detection dataset wrapper that performs mixup for normal dataset.""" def __init__( self, dataset, img_size, mosaic=True, preproc=None, degrees=10.0, translate=0.1, mosaic_scale=(0.5, 1.5), mixup_scale=(0.5, 1.5), shear=2.0, enable_mixup=True, mosaic_prob=1.0, mixup_prob=1.0, *args ): """ Args: dataset(Dataset) : Pytorch dataset object. img_size (tuple): mosaic (bool): enable mosaic augmentation or not. preproc (func): degrees (float): translate (float): mosaic_scale (tuple): mixup_scale (tuple): shear (float): enable_mixup (bool): *args(tuple) : Additional arguments for mixup random sampler. """ super().__init__(img_size, mosaic=mosaic) self._dataset = dataset self.preproc = preproc self.degrees = degrees self.translate = translate self.scale = mosaic_scale self.shear = shear self.mixup_scale = mixup_scale self.enable_mosaic = mosaic self.enable_mixup = enable_mixup self.mosaic_prob = mosaic_prob self.mixup_prob = mixup_prob self.local_rank = get_local_rank() def __len__(self): return len(self._dataset) @Dataset.mosaic_getitem def __getitem__(self, idx): if self.enable_mosaic and random.random() < self.mosaic_prob: mosaic_labels = [] input_dim = self._dataset.input_dim input_h, input_w = input_dim[0], input_dim[1] # yc, xc = s, s # mosaic center x, y yc = int(random.uniform(0.5 * input_h, 1.5 * input_h)) xc = int(random.uniform(0.5 * input_w, 1.5 * input_w)) # 3 additional image indices indices = [idx] + [random.randint(0, len(self._dataset) - 1) for _ in range(3)] for i_mosaic, index in enumerate(indices): img, _labels, _, img_id = self._dataset.pull_item(index) h0, w0 = img.shape[:2] # orig hw scale = min(1. * input_h / h0, 1. * input_w / w0) img = cv2.resize( img, (int(w0 * scale), int(h0 * scale)), interpolation=cv2.INTER_LINEAR ) # generate output mosaic image (h, w, c) = img.shape[:3] if i_mosaic == 0: mosaic_img = np.full((input_h * 2, input_w * 2, c), 114, dtype=np.uint8) # suffix l means large image, while s means small image in mosaic aug. (l_x1, l_y1, l_x2, l_y2), (s_x1, s_y1, s_x2, s_y2) = get_mosaic_coordinate( mosaic_img, i_mosaic, xc, yc, w, h, input_h, input_w ) mosaic_img[l_y1:l_y2, l_x1:l_x2] = img[s_y1:s_y2, s_x1:s_x2] padw, padh = l_x1 - s_x1, l_y1 - s_y1 labels = _labels.copy() # Normalized xywh to pixel xyxy format if _labels.size > 0: labels[:, 0] = scale * _labels[:, 0] + padw labels[:, 1] = scale * _labels[:, 1] + padh labels[:, 2] = scale * _labels[:, 2] + padw labels[:, 3] = scale * _labels[:, 3] + padh mosaic_labels.append(labels) if len(mosaic_labels): mosaic_labels = np.concatenate(mosaic_labels, 0) np.clip(mosaic_labels[:, 0], 0, 2 * input_w, out=mosaic_labels[:, 0]) np.clip(mosaic_labels[:, 1], 0, 2 * input_h, out=mosaic_labels[:, 1]) np.clip(mosaic_labels[:, 2], 0, 2 * input_w, out=mosaic_labels[:, 2]) np.clip(mosaic_labels[:, 3], 0, 2 * input_h, out=mosaic_labels[:, 3]) mosaic_img, mosaic_labels = random_affine( mosaic_img, mosaic_labels, target_size=(input_w, input_h), degrees=self.degrees, translate=self.translate, scales=self.scale, shear=self.shear, ) # ----------------------------------------------------------------- # CopyPaste: https://arxiv.org/abs/2012.07177 # ----------------------------------------------------------------- if ( self.enable_mixup and not len(mosaic_labels) == 0 and random.random() < self.mixup_prob ): mosaic_img, mosaic_labels = self.mixup(mosaic_img, mosaic_labels, self.input_dim) mix_img, padded_labels = self.preproc(mosaic_img, mosaic_labels, self.input_dim) img_info = (mix_img.shape[1], mix_img.shape[0]) # ----------------------------------------------------------------- # img_info and img_id are not used for training. # They are also hard to be specified on a mosaic image. # ----------------------------------------------------------------- return mix_img, padded_labels, img_info, img_id else: self._dataset._input_dim = self.input_dim img, label, img_info, img_id = self._dataset.pull_item(idx) img, label = self.preproc(img, label, self.input_dim) return img, label, img_info, img_id def mixup(self, origin_img, origin_labels, input_dim): jit_factor = random.uniform(*self.mixup_scale) FLIP = random.uniform(0, 1) > 0.5 cp_labels = [] while len(cp_labels) == 0: cp_index = random.randint(0, self.__len__() - 1) cp_labels = self._dataset.load_anno(cp_index) img, cp_labels, _, _ = self._dataset.pull_item(cp_index) if len(img.shape) == 3: cp_img = np.ones((input_dim[0], input_dim[1], 3), dtype=np.uint8) * 114 else: cp_img = np.ones(input_dim, dtype=np.uint8) * 114 cp_scale_ratio = min(input_dim[0] / img.shape[0], input_dim[1] / img.shape[1]) resized_img = cv2.resize( img, (int(img.shape[1] * cp_scale_ratio), int(img.shape[0] * cp_scale_ratio)), interpolation=cv2.INTER_LINEAR, ) cp_img[ : int(img.shape[0] * cp_scale_ratio), : int(img.shape[1] * cp_scale_ratio) ] = resized_img cp_img = cv2.resize( cp_img, (int(cp_img.shape[1] * jit_factor), int(cp_img.shape[0] * jit_factor)), ) cp_scale_ratio *= jit_factor if FLIP: cp_img = cp_img[:, ::-1, :] origin_h, origin_w = cp_img.shape[:2] target_h, target_w = origin_img.shape[:2] padded_img = np.zeros( (max(origin_h, target_h), max(origin_w, target_w), 3), dtype=np.uint8 ) padded_img[:origin_h, :origin_w] = cp_img x_offset, y_offset = 0, 0 if padded_img.shape[0] > target_h: y_offset = random.randint(0, padded_img.shape[0] - target_h - 1) if padded_img.shape[1] > target_w: x_offset = random.randint(0, padded_img.shape[1] - target_w - 1) padded_cropped_img = padded_img[ y_offset: y_offset + target_h, x_offset: x_offset + target_w ] cp_bboxes_origin_np = adjust_box_anns( cp_labels[:, :4].copy(), cp_scale_ratio, 0, 0, origin_w, origin_h ) if FLIP: cp_bboxes_origin_np[:, 0::2] = ( origin_w - cp_bboxes_origin_np[:, 0::2][:, ::-1] ) cp_bboxes_transformed_np = cp_bboxes_origin_np.copy() cp_bboxes_transformed_np[:, 0::2] = np.clip( cp_bboxes_transformed_np[:, 0::2] - x_offset, 0, target_w ) cp_bboxes_transformed_np[:, 1::2] = np.clip( cp_bboxes_transformed_np[:, 1::2] - y_offset, 0, target_h ) cls_labels = cp_labels[:, 4:5].copy() box_labels = cp_bboxes_transformed_np labels = np.hstack((box_labels, cls_labels)) origin_labels = np.vstack((origin_labels, labels)) origin_img = origin_img.astype(np.float32) origin_img = 0.5 * origin_img + 0.5 * padded_cropped_img.astype(np.float32) return origin_img.astype(np.uint8), origin_labels ================================================ FILE: asone/detectors/yolox/yolox/data/datasets/voc.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Code are based on # https://github.com/fmassa/vision/blob/voc_dataset/torchvision/datasets/voc.py # Copyright (c) Francisco Massa. # Copyright (c) Ellis Brown, Max deGroot. # Copyright (c) Megvii, Inc. and its affiliates. import os import os.path import pickle import xml.etree.ElementTree as ET import cv2 import numpy as np from yolox.evaluators.voc_eval import voc_eval from .datasets_wrapper import CacheDataset, cache_read_img from .voc_classes import VOC_CLASSES class AnnotationTransform(object): """Transforms a VOC annotation into a Tensor of bbox coords and label index Initilized with a dictionary lookup of classnames to indexes Arguments: class_to_ind (dict, optional): dictionary lookup of classnames -> indexes (default: alphabetic indexing of VOC's 20 classes) keep_difficult (bool, optional): keep difficult instances or not (default: False) height (int): height width (int): width """ def __init__(self, class_to_ind=None, keep_difficult=True): self.class_to_ind = class_to_ind or dict( zip(VOC_CLASSES, range(len(VOC_CLASSES))) ) self.keep_difficult = keep_difficult def __call__(self, target): """ Arguments: target (annotation) : the target annotation to be made usable will be an ET.Element Returns: a list containing lists of bounding boxes [bbox coords, class name] """ res = np.empty((0, 5)) for obj in target.iter("object"): difficult = obj.find("difficult") if difficult is not None: difficult = int(difficult.text) == 1 else: difficult = False if not self.keep_difficult and difficult: continue name = obj.find("name").text.strip() bbox = obj.find("bndbox") pts = ["xmin", "ymin", "xmax", "ymax"] bndbox = [] for i, pt in enumerate(pts): cur_pt = int(float(bbox.find(pt).text)) - 1 # scale height or width # cur_pt = cur_pt / width if i % 2 == 0 else cur_pt / height bndbox.append(cur_pt) label_idx = self.class_to_ind[name] bndbox.append(label_idx) res = np.vstack((res, bndbox)) # [xmin, ymin, xmax, ymax, label_ind] # img_id = target.find('filename').text[:-4] width = int(target.find("size").find("width").text) height = int(target.find("size").find("height").text) img_info = (height, width) return res, img_info class VOCDetection(CacheDataset): """ VOC Detection Dataset Object input is image, target is annotation Args: root (string): filepath to VOCdevkit folder. image_set (string): imageset to use (eg. 'train', 'val', 'test') transform (callable, optional): transformation to perform on the input image target_transform (callable, optional): transformation to perform on the target `annotation` (eg: take in caption string, return tensor of word indices) dataset_name (string, optional): which dataset to load (default: 'VOC2007') """ def __init__( self, data_dir, image_sets=[("2007", "trainval"), ("2012", "trainval")], img_size=(416, 416), preproc=None, target_transform=AnnotationTransform(), dataset_name="VOC0712", cache=False, cache_type="ram", ): self.root = data_dir self.image_set = image_sets self.img_size = img_size self.preproc = preproc self.target_transform = target_transform self.name = dataset_name self._annopath = os.path.join("%s", "Annotations", "%s.xml") self._imgpath = os.path.join("%s", "JPEGImages", "%s.jpg") self._classes = VOC_CLASSES self.cats = [ {"id": idx, "name": val} for idx, val in enumerate(VOC_CLASSES) ] self.class_ids = list(range(len(VOC_CLASSES))) self.ids = list() for (year, name) in image_sets: self._year = year rootpath = os.path.join(self.root, "VOC" + year) for line in open( os.path.join(rootpath, "ImageSets", "Main", name + ".txt") ): self.ids.append((rootpath, line.strip())) self.num_imgs = len(self.ids) self.annotations = self._load_coco_annotations() path_filename = [ (self._imgpath % self.ids[i]).split(self.root + "/")[1] for i in range(self.num_imgs) ] super().__init__( input_dimension=img_size, num_imgs=self.num_imgs, data_dir=self.root, cache_dir_name=f"cache_{self.name}", path_filename=path_filename, cache=cache, cache_type=cache_type ) def __len__(self): return self.num_imgs def _load_coco_annotations(self): return [self.load_anno_from_ids(_ids) for _ids in range(self.num_imgs)] def load_anno_from_ids(self, index): img_id = self.ids[index] target = ET.parse(self._annopath % img_id).getroot() assert self.target_transform is not None res, img_info = self.target_transform(target) height, width = img_info r = min(self.img_size[0] / height, self.img_size[1] / width) res[:, :4] *= r resized_info = (int(height * r), int(width * r)) return (res, img_info, resized_info) def load_anno(self, index): return self.annotations[index][0] def load_resized_img(self, index): img = self.load_image(index) r = min(self.img_size[0] / img.shape[0], self.img_size[1] / img.shape[1]) resized_img = cv2.resize( img, (int(img.shape[1] * r), int(img.shape[0] * r)), interpolation=cv2.INTER_LINEAR, ).astype(np.uint8) return resized_img def load_image(self, index): img_id = self.ids[index] img = cv2.imread(self._imgpath % img_id, cv2.IMREAD_COLOR) assert img is not None, f"file named {self._imgpath % img_id} not found" return img @cache_read_img(use_cache=True) def read_img(self, index): return self.load_resized_img(index) def pull_item(self, index): """Returns the original image and target at an index for mixup Note: not using self.__getitem__(), as any transformations passed in could mess up this functionality. Argument: index (int): index of img to show Return: img, target """ target, img_info, _ = self.annotations[index] img = self.read_img(index) return img, target, img_info, index @CacheDataset.mosaic_getitem def __getitem__(self, index): img, target, img_info, img_id = self.pull_item(index) if self.preproc is not None: img, target = self.preproc(img, target, self.input_dim) return img, target, img_info, img_id def evaluate_detections(self, all_boxes, output_dir=None): """ all_boxes is a list of length number-of-classes. Each list element is a list of length number-of-images. Each of those list elements is either an empty list [] or a numpy array of detection. all_boxes[class][image] = [] or np.array of shape #dets x 5 """ self._write_voc_results_file(all_boxes) IouTh = np.linspace( 0.5, 0.95, int(np.round((0.95 - 0.5) / 0.05)) + 1, endpoint=True ) mAPs = [] for iou in IouTh: mAP = self._do_python_eval(output_dir, iou) mAPs.append(mAP) print("--------------------------------------------------------------") print("map_5095:", np.mean(mAPs)) print("map_50:", mAPs[0]) print("--------------------------------------------------------------") return np.mean(mAPs), mAPs[0] def _get_voc_results_file_template(self): filename = "comp4_det_test" + "_{:s}.txt" filedir = os.path.join(self.root, "results", "VOC" + self._year, "Main") if not os.path.exists(filedir): os.makedirs(filedir) path = os.path.join(filedir, filename) return path def _write_voc_results_file(self, all_boxes): for cls_ind, cls in enumerate(VOC_CLASSES): cls_ind = cls_ind if cls == "__background__": continue print("Writing {} VOC results file".format(cls)) filename = self._get_voc_results_file_template().format(cls) with open(filename, "wt") as f: for im_ind, index in enumerate(self.ids): index = index[1] dets = all_boxes[cls_ind][im_ind] if dets == []: continue for k in range(dets.shape[0]): f.write( "{:s} {:.3f} {:.1f} {:.1f} {:.1f} {:.1f}\n".format( index, dets[k, -1], dets[k, 0] + 1, dets[k, 1] + 1, dets[k, 2] + 1, dets[k, 3] + 1, ) ) def _do_python_eval(self, output_dir="output", iou=0.5): rootpath = os.path.join(self.root, "VOC" + self._year) name = self.image_set[0][1] annopath = os.path.join(rootpath, "Annotations", "{:s}.xml") imagesetfile = os.path.join(rootpath, "ImageSets", "Main", name + ".txt") cachedir = os.path.join( self.root, "annotations_cache", "VOC" + self._year, name ) if not os.path.exists(cachedir): os.makedirs(cachedir) aps = [] # The PASCAL VOC metric changed in 2010 use_07_metric = True if int(self._year) < 2010 else False print("Eval IoU : {:.2f}".format(iou)) if output_dir is not None and not os.path.isdir(output_dir): os.mkdir(output_dir) for i, cls in enumerate(VOC_CLASSES): if cls == "__background__": continue filename = self._get_voc_results_file_template().format(cls) rec, prec, ap = voc_eval( filename, annopath, imagesetfile, cls, cachedir, ovthresh=iou, use_07_metric=use_07_metric, ) aps += [ap] if iou == 0.5: print("AP for {} = {:.4f}".format(cls, ap)) if output_dir is not None: with open(os.path.join(output_dir, cls + "_pr.pkl"), "wb") as f: pickle.dump({"rec": rec, "prec": prec, "ap": ap}, f) if iou == 0.5: print("Mean AP = {:.4f}".format(np.mean(aps))) print("~~~~~~~~") print("Results:") for ap in aps: print("{:.3f}".format(ap)) print("{:.3f}".format(np.mean(aps))) print("~~~~~~~~") print("") print("--------------------------------------------------------------") print("Results computed with the **unofficial** Python eval code.") print("Results should be very close to the official MATLAB eval code.") print("Recompute with `./tools/reval.py --matlab ...` for your paper.") print("-- Thanks, The Management") print("--------------------------------------------------------------") return np.mean(aps) ================================================ FILE: asone/detectors/yolox/yolox/data/datasets/voc_classes.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. # VOC_CLASSES = ( '__background__', # always index 0 VOC_CLASSES = ( "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor", ) ================================================ FILE: asone/detectors/yolox/yolox/data/samplers.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import itertools from typing import Optional import torch import torch.distributed as dist from torch.utils.data.sampler import BatchSampler as torchBatchSampler from torch.utils.data.sampler import Sampler class YoloBatchSampler(torchBatchSampler): """ This batch sampler will generate mini-batches of (mosaic, index) tuples from another sampler. It works just like the :class:`torch.utils.data.sampler.BatchSampler`, but it will turn on/off the mosaic aug. """ def __init__(self, *args, mosaic=True, **kwargs): super().__init__(*args, **kwargs) self.mosaic = mosaic def __iter__(self): for batch in super().__iter__(): yield [(self.mosaic, idx) for idx in batch] class InfiniteSampler(Sampler): """ In training, we only care about the "infinite stream" of training data. So this sampler produces an infinite stream of indices and all workers cooperate to correctly shuffle the indices and sample different indices. The samplers in each worker effectively produces `indices[worker_id::num_workers]` where `indices` is an infinite stream of indices consisting of `shuffle(range(size)) + shuffle(range(size)) + ...` (if shuffle is True) or `range(size) + range(size) + ...` (if shuffle is False) """ def __init__( self, size: int, shuffle: bool = True, seed: Optional[int] = 0, rank=0, world_size=1, ): """ Args: size (int): the total number of data of the underlying dataset to sample from shuffle (bool): whether to shuffle the indices or not seed (int): the initial seed of the shuffle. Must be the same across all workers. If None, will use a random seed shared among workers (require synchronization among all workers). """ self._size = size assert size > 0 self._shuffle = shuffle self._seed = int(seed) if dist.is_available() and dist.is_initialized(): self._rank = dist.get_rank() self._world_size = dist.get_world_size() else: self._rank = rank self._world_size = world_size def __iter__(self): start = self._rank yield from itertools.islice( self._infinite_indices(), start, None, self._world_size ) def _infinite_indices(self): g = torch.Generator() g.manual_seed(self._seed) while True: if self._shuffle: yield from torch.randperm(self._size, generator=g) else: yield from torch.arange(self._size) def __len__(self): return self._size // self._world_size ================================================ FILE: asone/detectors/yolox/yolox/evaluators/__init__.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. from .coco_evaluator import COCOEvaluator from .voc_evaluator import VOCEvaluator ================================================ FILE: asone/detectors/yolox/yolox/evaluators/coco_evaluator.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import contextlib import io import itertools import json import tempfile import time from collections import ChainMap, defaultdict from loguru import logger from tabulate import tabulate from tqdm import tqdm import numpy as np import torch from yolox.data.datasets import COCO_CLASSES from yolox.utils import ( gather, is_main_process, postprocess, synchronize, time_synchronized, xyxy2xywh ) def per_class_AR_table(coco_eval, class_names=COCO_CLASSES, headers=["class", "AR"], colums=6): per_class_AR = {} recalls = coco_eval.eval["recall"] # dimension of recalls: [TxKxAxM] # recall has dims (iou, cls, area range, max dets) assert len(class_names) == recalls.shape[1] for idx, name in enumerate(class_names): recall = recalls[:, idx, 0, -1] recall = recall[recall > -1] ar = np.mean(recall) if recall.size else float("nan") per_class_AR[name] = float(ar * 100) num_cols = min(colums, len(per_class_AR) * len(headers)) result_pair = [x for pair in per_class_AR.items() for x in pair] row_pair = itertools.zip_longest(*[result_pair[i::num_cols] for i in range(num_cols)]) table_headers = headers * (num_cols // len(headers)) table = tabulate( row_pair, tablefmt="pipe", floatfmt=".3f", headers=table_headers, numalign="left", ) return table def per_class_AP_table(coco_eval, class_names=COCO_CLASSES, headers=["class", "AP"], colums=6): per_class_AP = {} precisions = coco_eval.eval["precision"] # dimension of precisions: [TxRxKxAxM] # precision has dims (iou, recall, cls, area range, max dets) assert len(class_names) == precisions.shape[2] for idx, name in enumerate(class_names): # area range index 0: all area ranges # max dets index -1: typically 100 per image precision = precisions[:, :, idx, 0, -1] precision = precision[precision > -1] ap = np.mean(precision) if precision.size else float("nan") per_class_AP[name] = float(ap * 100) num_cols = min(colums, len(per_class_AP) * len(headers)) result_pair = [x for pair in per_class_AP.items() for x in pair] row_pair = itertools.zip_longest(*[result_pair[i::num_cols] for i in range(num_cols)]) table_headers = headers * (num_cols // len(headers)) table = tabulate( row_pair, tablefmt="pipe", floatfmt=".3f", headers=table_headers, numalign="left", ) return table class COCOEvaluator: """ COCO AP Evaluation class. All the data in the val2017 dataset are processed and evaluated by COCO API. """ def __init__( self, dataloader, img_size: int, confthre: float, nmsthre: float, num_classes: int, testdev: bool = False, per_class_AP: bool = True, per_class_AR: bool = True, ): """ Args: dataloader (Dataloader): evaluate dataloader. img_size: image size after preprocess. images are resized to squares whose shape is (img_size, img_size). confthre: confidence threshold ranging from 0 to 1, which is defined in the config file. nmsthre: IoU threshold of non-max supression ranging from 0 to 1. per_class_AP: Show per class AP during evalution or not. Default to True. per_class_AR: Show per class AR during evalution or not. Default to True. """ self.dataloader = dataloader self.img_size = img_size self.confthre = confthre self.nmsthre = nmsthre self.num_classes = num_classes self.testdev = testdev self.per_class_AP = per_class_AP self.per_class_AR = per_class_AR def evaluate( self, model, distributed=False, half=False, trt_file=None, decoder=None, test_size=None, return_outputs=False ): """ COCO average precision (AP) Evaluation. Iterate inference on the test dataset and the results are evaluated by COCO API. NOTE: This function will change training mode to False, please save states if needed. Args: model : model to evaluate. Returns: ap50_95 (float) : COCO AP of IoU=50:95 ap50 (float) : COCO AP of IoU=50 summary (sr): summary info of evaluation. """ # TODO half to amp_test tensor_type = torch.cuda.HalfTensor if half else torch.cuda.FloatTensor model = model.eval() if half: model = model.half() ids = [] data_list = [] output_data = defaultdict() progress_bar = tqdm if is_main_process() else iter inference_time = 0 nms_time = 0 n_samples = max(len(self.dataloader) - 1, 1) if trt_file is not None: from torch2trt import TRTModule model_trt = TRTModule() model_trt.load_state_dict(torch.load(trt_file)) x = torch.ones(1, 3, test_size[0], test_size[1]).cuda() model(x) model = model_trt for cur_iter, (imgs, _, info_imgs, ids) in enumerate( progress_bar(self.dataloader) ): with torch.no_grad(): imgs = imgs.type(tensor_type) # skip the last iters since batchsize might be not enough for batch inference is_time_record = cur_iter < len(self.dataloader) - 1 if is_time_record: start = time.time() outputs = model(imgs) if decoder is not None: outputs = decoder(outputs, dtype=outputs.type()) if is_time_record: infer_end = time_synchronized() inference_time += infer_end - start outputs = postprocess( outputs, self.num_classes, self.confthre, self.nmsthre ) if is_time_record: nms_end = time_synchronized() nms_time += nms_end - infer_end data_list_elem, image_wise_data = self.convert_to_coco_format( outputs, info_imgs, ids, return_outputs=True) data_list.extend(data_list_elem) output_data.update(image_wise_data) statistics = torch.cuda.FloatTensor([inference_time, nms_time, n_samples]) if distributed: # different process/device might have different speed, # to make sure the process will not be stucked, sync func is used here. synchronize() data_list = gather(data_list, dst=0) output_data = gather(output_data, dst=0) data_list = list(itertools.chain(*data_list)) output_data = dict(ChainMap(*output_data)) torch.distributed.reduce(statistics, dst=0) eval_results = self.evaluate_prediction(data_list, statistics) synchronize() if return_outputs: return eval_results, output_data return eval_results def convert_to_coco_format(self, outputs, info_imgs, ids, return_outputs=False): data_list = [] image_wise_data = defaultdict(dict) for (output, img_h, img_w, img_id) in zip( outputs, info_imgs[0], info_imgs[1], ids ): if output is None: continue output = output.cpu() bboxes = output[:, 0:4] # preprocessing: resize scale = min( self.img_size[0] / float(img_h), self.img_size[1] / float(img_w) ) bboxes /= scale cls = output[:, 6] scores = output[:, 4] * output[:, 5] image_wise_data.update({ int(img_id): { "bboxes": [box.numpy().tolist() for box in bboxes], "scores": [score.numpy().item() for score in scores], "categories": [ self.dataloader.dataset.class_ids[int(cls[ind])] for ind in range(bboxes.shape[0]) ], } }) bboxes = xyxy2xywh(bboxes) for ind in range(bboxes.shape[0]): label = self.dataloader.dataset.class_ids[int(cls[ind])] pred_data = { "image_id": int(img_id), "category_id": label, "bbox": bboxes[ind].numpy().tolist(), "score": scores[ind].numpy().item(), "segmentation": [], } # COCO json format data_list.append(pred_data) if return_outputs: return data_list, image_wise_data return data_list def evaluate_prediction(self, data_dict, statistics): if not is_main_process(): return 0, 0, None logger.info("Evaluate in main process...") annType = ["segm", "bbox", "keypoints"] inference_time = statistics[0].item() nms_time = statistics[1].item() n_samples = statistics[2].item() a_infer_time = 1000 * inference_time / (n_samples * self.dataloader.batch_size) a_nms_time = 1000 * nms_time / (n_samples * self.dataloader.batch_size) time_info = ", ".join( [ "Average {} time: {:.2f} ms".format(k, v) for k, v in zip( ["forward", "NMS", "inference"], [a_infer_time, a_nms_time, (a_infer_time + a_nms_time)], ) ] ) info = time_info + "\n" # Evaluate the Dt (detection) json comparing with the ground truth if len(data_dict) > 0: cocoGt = self.dataloader.dataset.coco # TODO: since pycocotools can't process dict in py36, write data to json file. if self.testdev: json.dump(data_dict, open("./yolox_testdev_2017.json", "w")) cocoDt = cocoGt.loadRes("./yolox_testdev_2017.json") else: _, tmp = tempfile.mkstemp() json.dump(data_dict, open(tmp, "w")) cocoDt = cocoGt.loadRes(tmp) try: from yolox.layers import COCOeval_opt as COCOeval except ImportError: from pycocotools.cocoeval import COCOeval logger.warning("Use standard COCOeval.") cocoEval = COCOeval(cocoGt, cocoDt, annType[1]) cocoEval.evaluate() cocoEval.accumulate() redirect_string = io.StringIO() with contextlib.redirect_stdout(redirect_string): cocoEval.summarize() info += redirect_string.getvalue() cat_ids = list(cocoGt.cats.keys()) cat_names = [cocoGt.cats[catId]['name'] for catId in sorted(cat_ids)] if self.per_class_AP: AP_table = per_class_AP_table(cocoEval, class_names=cat_names) info += "per class AP:\n" + AP_table + "\n" if self.per_class_AR: AR_table = per_class_AR_table(cocoEval, class_names=cat_names) info += "per class AR:\n" + AR_table + "\n" return cocoEval.stats[0], cocoEval.stats[1], info else: return 0, 0, info ================================================ FILE: asone/detectors/yolox/yolox/evaluators/voc_eval.py ================================================ #!/usr/bin/env python3 # Code are based on # https://github.com/rbgirshick/py-faster-rcnn/blob/master/lib/datasets/voc_eval.py # Copyright (c) Bharath Hariharan. # Copyright (c) Megvii, Inc. and its affiliates. import os import pickle import xml.etree.ElementTree as ET import numpy as np def parse_rec(filename): """Parse a PASCAL VOC xml file""" tree = ET.parse(filename) objects = [] for obj in tree.findall("object"): obj_struct = {} obj_struct["name"] = obj.find("name").text obj_struct["pose"] = obj.find("pose").text obj_struct["truncated"] = int(obj.find("truncated").text) obj_struct["difficult"] = int(obj.find("difficult").text) bbox = obj.find("bndbox") obj_struct["bbox"] = [ int(bbox.find("xmin").text), int(bbox.find("ymin").text), int(bbox.find("xmax").text), int(bbox.find("ymax").text), ] objects.append(obj_struct) return objects def voc_ap(rec, prec, use_07_metric=False): """ Compute VOC AP given precision and recall. If use_07_metric is true, uses the VOC 07 11 point method (default:False). """ if use_07_metric: # 11 point metric ap = 0.0 for t in np.arange(0.0, 1.1, 0.1): if np.sum(rec >= t) == 0: p = 0 else: p = np.max(prec[rec >= t]) ap = ap + p / 11.0 else: # correct AP calculation # first append sentinel values at the end mrec = np.concatenate(([0.0], rec, [1.0])) mpre = np.concatenate(([0.0], prec, [0.0])) # compute the precision envelope for i in range(mpre.size - 1, 0, -1): mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i]) # to calculate area under PR curve, look for points # where X axis (recall) changes value i = np.where(mrec[1:] != mrec[:-1])[0] # and sum (\Delta recall) * prec ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) return ap def voc_eval( detpath, annopath, imagesetfile, classname, cachedir, ovthresh=0.5, use_07_metric=False, ): # first load gt if not os.path.isdir(cachedir): os.mkdir(cachedir) cachefile = os.path.join(cachedir, "annots.pkl") # read list of images with open(imagesetfile, "r") as f: lines = f.readlines() imagenames = [x.strip() for x in lines] if not os.path.isfile(cachefile): # load annots recs = {} for i, imagename in enumerate(imagenames): recs[imagename] = parse_rec(annopath.format(imagename)) if i % 100 == 0: print(f"Reading annotation for {i + 1}/{len(imagenames)}") # save print(f"Saving cached annotations to {cachefile}") with open(cachefile, "wb") as f: pickle.dump(recs, f) else: # load with open(cachefile, "rb") as f: recs = pickle.load(f) # extract gt objects for this class class_recs = {} npos = 0 for imagename in imagenames: R = [obj for obj in recs[imagename] if obj["name"] == classname] bbox = np.array([x["bbox"] for x in R]) difficult = np.array([x["difficult"] for x in R]).astype(bool) det = [False] * len(R) npos = npos + sum(~difficult) class_recs[imagename] = {"bbox": bbox, "difficult": difficult, "det": det} # read dets detfile = detpath.format(classname) with open(detfile, "r") as f: lines = f.readlines() if len(lines) == 0: return 0, 0, 0 splitlines = [x.strip().split(" ") for x in lines] image_ids = [x[0] for x in splitlines] confidence = np.array([float(x[1]) for x in splitlines]) BB = np.array([[float(z) for z in x[2:]] for x in splitlines]) # sort by confidence sorted_ind = np.argsort(-confidence) BB = BB[sorted_ind, :] image_ids = [image_ids[x] for x in sorted_ind] # go down dets and mark TPs and FPs nd = len(image_ids) tp = np.zeros(nd) fp = np.zeros(nd) for d in range(nd): R = class_recs[image_ids[d]] bb = BB[d, :].astype(float) ovmax = -np.inf BBGT = R["bbox"].astype(float) if BBGT.size > 0: # compute overlaps # intersection ixmin = np.maximum(BBGT[:, 0], bb[0]) iymin = np.maximum(BBGT[:, 1], bb[1]) ixmax = np.minimum(BBGT[:, 2], bb[2]) iymax = np.minimum(BBGT[:, 3], bb[3]) iw = np.maximum(ixmax - ixmin + 1.0, 0.0) ih = np.maximum(iymax - iymin + 1.0, 0.0) inters = iw * ih # union uni = ( (bb[2] - bb[0] + 1.0) * (bb[3] - bb[1] + 1.0) + (BBGT[:, 2] - BBGT[:, 0] + 1.0) * (BBGT[:, 3] - BBGT[:, 1] + 1.0) - inters ) overlaps = inters / uni ovmax = np.max(overlaps) jmax = np.argmax(overlaps) if ovmax > ovthresh: if not R["difficult"][jmax]: if not R["det"][jmax]: tp[d] = 1.0 R["det"][jmax] = 1 else: fp[d] = 1.0 else: fp[d] = 1.0 # compute precision recall fp = np.cumsum(fp) tp = np.cumsum(tp) rec = tp / float(npos) # avoid divide by zero in case the first detection matches a difficult # ground truth prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps) ap = voc_ap(rec, prec, use_07_metric) return rec, prec, ap ================================================ FILE: asone/detectors/yolox/yolox/evaluators/voc_evaluator.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii, Inc. and its affiliates. import sys import tempfile import time from collections import ChainMap from loguru import logger from tqdm import tqdm import numpy as np import torch from yolox.utils import gather, is_main_process, postprocess, synchronize, time_synchronized class VOCEvaluator: """ VOC AP Evaluation class. """ def __init__(self, dataloader, img_size, confthre, nmsthre, num_classes): """ Args: dataloader (Dataloader): evaluate dataloader. img_size (int): image size after preprocess. images are resized to squares whose shape is (img_size, img_size). confthre (float): confidence threshold ranging from 0 to 1, which is defined in the config file. nmsthre (float): IoU threshold of non-max supression ranging from 0 to 1. """ self.dataloader = dataloader self.img_size = img_size self.confthre = confthre self.nmsthre = nmsthre self.num_classes = num_classes self.num_images = len(dataloader.dataset) def evaluate( self, model, distributed=False, half=False, trt_file=None, decoder=None, test_size=None, return_outputs=False, ): """ VOC average precision (AP) Evaluation. Iterate inference on the test dataset and the results are evaluated by COCO API. NOTE: This function will change training mode to False, please save states if needed. Args: model : model to evaluate. Returns: ap50_95 (float) : COCO style AP of IoU=50:95 ap50 (float) : VOC 2007 metric AP of IoU=50 summary (sr): summary info of evaluation. """ # TODO half to amp_test tensor_type = torch.cuda.HalfTensor if half else torch.cuda.FloatTensor model = model.eval() if half: model = model.half() ids = [] data_list = {} progress_bar = tqdm if is_main_process() else iter inference_time = 0 nms_time = 0 n_samples = max(len(self.dataloader) - 1, 1) if trt_file is not None: from torch2trt import TRTModule model_trt = TRTModule() model_trt.load_state_dict(torch.load(trt_file)) x = torch.ones(1, 3, test_size[0], test_size[1]).cuda() model(x) model = model_trt for cur_iter, (imgs, _, info_imgs, ids) in enumerate(progress_bar(self.dataloader)): with torch.no_grad(): imgs = imgs.type(tensor_type) # skip the last iters since batchsize might be not enough for batch inference is_time_record = cur_iter < len(self.dataloader) - 1 if is_time_record: start = time.time() outputs = model(imgs) if decoder is not None: outputs = decoder(outputs, dtype=outputs.type()) if is_time_record: infer_end = time_synchronized() inference_time += infer_end - start outputs = postprocess( outputs, self.num_classes, self.confthre, self.nmsthre ) if is_time_record: nms_end = time_synchronized() nms_time += nms_end - infer_end data_list.update(self.convert_to_voc_format(outputs, info_imgs, ids)) statistics = torch.cuda.FloatTensor([inference_time, nms_time, n_samples]) if distributed: data_list = gather(data_list, dst=0) data_list = ChainMap(*data_list) torch.distributed.reduce(statistics, dst=0) eval_results = self.evaluate_prediction(data_list, statistics) synchronize() if return_outputs: return eval_results, data_list return eval_results def convert_to_voc_format(self, outputs, info_imgs, ids): predictions = {} for output, img_h, img_w, img_id in zip(outputs, info_imgs[0], info_imgs[1], ids): if output is None: predictions[int(img_id)] = (None, None, None) continue output = output.cpu() bboxes = output[:, 0:4] # preprocessing: resize scale = min(self.img_size[0] / float(img_h), self.img_size[1] / float(img_w)) bboxes /= scale cls = output[:, 6] scores = output[:, 4] * output[:, 5] predictions[int(img_id)] = (bboxes, cls, scores) return predictions def evaluate_prediction(self, data_dict, statistics): if not is_main_process(): return 0, 0, None logger.info("Evaluate in main process...") inference_time = statistics[0].item() nms_time = statistics[1].item() n_samples = statistics[2].item() a_infer_time = 1000 * inference_time / (n_samples * self.dataloader.batch_size) a_nms_time = 1000 * nms_time / (n_samples * self.dataloader.batch_size) time_info = ", ".join( [ "Average {} time: {:.2f} ms".format(k, v) for k, v in zip( ["forward", "NMS", "inference"], [a_infer_time, a_nms_time, (a_infer_time + a_nms_time)], ) ] ) info = time_info + "\n" all_boxes = [ [[] for _ in range(self.num_images)] for _ in range(self.num_classes) ] for img_num in range(self.num_images): bboxes, cls, scores = data_dict[img_num] if bboxes is None: for j in range(self.num_classes): all_boxes[j][img_num] = np.empty([0, 5], dtype=np.float32) continue for j in range(self.num_classes): mask_c = cls == j if sum(mask_c) == 0: all_boxes[j][img_num] = np.empty([0, 5], dtype=np.float32) continue c_dets = torch.cat((bboxes, scores.unsqueeze(1)), dim=1) all_boxes[j][img_num] = c_dets[mask_c].numpy() sys.stdout.write(f"im_eval: {img_num + 1}/{self.num_images} \r") sys.stdout.flush() with tempfile.TemporaryDirectory() as tempdir: mAP50, mAP70 = self.dataloader.dataset.evaluate_detections(all_boxes, tempdir) return mAP50, mAP70, info ================================================ FILE: asone/detectors/yolox/yolox/exp/__init__.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. from .base_exp import BaseExp from .build import get_exp from .yolox_base import Exp ================================================ FILE: asone/detectors/yolox/yolox/exp/base_exp.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import ast import pprint from abc import ABCMeta, abstractmethod from typing import Dict from tabulate import tabulate from asone.detectors.yolox.yolox.utils import LRScheduler import torch from torch.nn import Module class BaseExp(metaclass=ABCMeta): """Basic class for any experiment.""" def __init__(self): self.seed = None self.output_dir = "./YOLOX_outputs" self.print_interval = 100 self.eval_interval = 10 @abstractmethod def get_model(self) -> Module: pass @abstractmethod def get_data_loader( self, batch_size: int, is_distributed: bool ) -> Dict[str, torch.utils.data.DataLoader]: pass @abstractmethod def get_optimizer(self, batch_size: int) -> torch.optim.Optimizer: pass @abstractmethod def get_lr_scheduler( self, lr: float, iters_per_epoch: int, **kwargs ) -> LRScheduler: pass @abstractmethod def get_evaluator(self): pass @abstractmethod def eval(self, model, evaluator, weights): pass def __repr__(self): table_header = ["keys", "values"] exp_table = [ (str(k), pprint.pformat(v)) for k, v in vars(self).items() if not k.startswith("_") ] return tabulate(exp_table, headers=table_header, tablefmt="fancy_grid") def merge(self, cfg_list): assert len(cfg_list) % 2 == 0 for k, v in zip(cfg_list[0::2], cfg_list[1::2]): # only update value with same key if hasattr(self, k): src_value = getattr(self, k) src_type = type(src_value) if src_value is not None and src_type != type(v): try: v = src_type(v) except Exception: v = ast.literal_eval(v) setattr(self, k, v) ================================================ FILE: asone/detectors/yolox/yolox/exp/build.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import importlib import os import sys def get_exp_by_file(exp_file): try: _dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) _dir = os.path.join(_dir, 'exps') sys.path.append(_dir) current_exp = importlib.import_module(os.path.basename(exp_file).split(".")[0]) exp = current_exp.Exp() except Exception: raise ImportError("{} doesn't contains class named 'Exp'".format(exp_file)) return exp def get_exp_by_name(exp_name): exp = exp_name.replace("-", "_") # convert string like "yolox-s" to "yolox_s" module_name = ".".join(["yolox", "exp", "default", exp]) exp_object = importlib.import_module(module_name).Exp() return exp_object def get_exp(exp_file=None, exp_name=None): """ get Exp object by file or name. If exp_file and exp_name are both provided, get Exp by exp_file. Args: exp_file (str): file path of experiment. exp_name (str): name of experiment. "yolo-s", """ assert ( exp_file is not None or exp_name is not None ), "plz provide exp file or exp name." if exp_file is not None: return get_exp_by_file(exp_file) else: return get_exp_by_name(exp_name) ================================================ FILE: asone/detectors/yolox/yolox/exp/default/__init__.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. # This file is used for package installation and find default exp file import importlib import sys from pathlib import Path _EXP_PATH = Path(__file__).resolve().parent.parent.parent.parent / "exps" / "default" if _EXP_PATH.is_dir(): # This is true only for in-place installation (pip install -e, setup.py develop), # where setup(package_dir=) does not work: https://github.com/pypa/setuptools/issues/230 class _ExpFinder(importlib.abc.MetaPathFinder): def find_spec(self, name, path, target=None): if not name.startswith("yolox.exp.default"): return project_name = name.split(".")[-1] + ".py" target_file = _EXP_PATH / project_name if not target_file.is_file(): return return importlib.util.spec_from_file_location(name, target_file) sys.meta_path.append(_ExpFinder()) ================================================ FILE: asone/detectors/yolox/yolox/exp/yolox_base.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import os import random import torch import torch.distributed as dist import torch.nn as nn from asone.detectors.yolox.yolox.exp.base_exp import BaseExp class Exp(BaseExp): def __init__(self): super().__init__() # ---------------- model config ---------------- # # detect classes number of model self.num_classes = 80 # factor of model depth self.depth = 1.00 # factor of model width self.width = 1.00 # activation name. For example, if using "relu", then "silu" will be replaced to "relu". self.act = "silu" # ---------------- dataloader config ---------------- # # set worker to 4 for shorter dataloader init time # If your training process cost many memory, reduce this value. self.data_num_workers = 4 self.input_size = (640, 640) # (height, width) # Actual multiscale ranges: [640 - 5 * 32, 640 + 5 * 32]. # To disable multiscale training, set the value to 0. self.multiscale_range = 5 # You can uncomment this line to specify a multiscale range # self.random_size = (14, 26) # dir of dataset images, if data_dir is None, this project will use `datasets` dir self.data_dir = None # name of annotation file for training self.train_ann = "instances_train2017.json" # name of annotation file for evaluation self.val_ann = "instances_val2017.json" # name of annotation file for testing self.test_ann = "instances_test2017.json" # --------------- transform config ----------------- # # prob of applying mosaic aug self.mosaic_prob = 1.0 # prob of applying mixup aug self.mixup_prob = 1.0 # prob of applying hsv aug self.hsv_prob = 1.0 # prob of applying flip aug self.flip_prob = 0.5 # rotation angle range, for example, if set to 2, the true range is (-2, 2) self.degrees = 10.0 # translate range, for example, if set to 0.1, the true range is (-0.1, 0.1) self.translate = 0.1 self.mosaic_scale = (0.1, 2) # apply mixup aug or not self.enable_mixup = True self.mixup_scale = (0.5, 1.5) # shear angle range, for example, if set to 2, the true range is (-2, 2) self.shear = 2.0 # -------------- training config --------------------- # # epoch number used for warmup self.warmup_epochs = 5 # max training epoch self.max_epoch = 300 # minimum learning rate during warmup self.warmup_lr = 0 self.min_lr_ratio = 0.05 # learning rate for one image. During training, lr will multiply batchsize. self.basic_lr_per_img = 0.01 / 64.0 # name of LRScheduler self.scheduler = "yoloxwarmcos" # last #epoch to close augmention like mosaic self.no_aug_epochs = 15 # apply EMA during training self.ema = True # weight decay of optimizer self.weight_decay = 5e-4 # momentum of optimizer self.momentum = 0.9 # log period in iter, for example, # if set to 1, user could see log every iteration. self.print_interval = 10 # eval period in epoch, for example, # if set to 1, model will be evaluate after every epoch. self.eval_interval = 10 # save history checkpoint or not. # If set to False, yolox will only save latest and best ckpt. self.save_history_ckpt = True # name of experiment self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0] # ----------------- testing config ------------------ # # output image size during evaluation/test self.test_size = (640, 640) # confidence threshold during evaluation/test, # boxes whose scores are less than test_conf will be filtered self.test_conf = 0.01 # nms threshold self.nmsthre = 0.65 def get_model(self): from asone.detectors.yolox.yolox.models import YOLOX, YOLOPAFPN, YOLOXHead def init_yolo(M): for m in M.modules(): if isinstance(m, nn.BatchNorm2d): m.eps = 1e-3 m.momentum = 0.03 if getattr(self, "model", None) is None: in_channels = [256, 512, 1024] backbone = YOLOPAFPN(self.depth, self.width, in_channels=in_channels, act=self.act) head = YOLOXHead(self.num_classes, self.width, in_channels=in_channels, act=self.act) self.model = YOLOX(backbone, head) self.model.apply(init_yolo) self.model.head.initialize_biases(1e-2) self.model.train() return self.model def get_data_loader(self, batch_size, is_distributed, no_aug=False, cache_img=False): from asone.detectors.yolox.yolox.data import ( COCODataset, TrainTransform, YoloBatchSampler, DataLoader, InfiniteSampler, MosaicDetection, worker_init_reset_seed, ) from asone.detectors.yolox.yolox.utils import wait_for_the_master with wait_for_the_master(): dataset = COCODataset( data_dir=self.data_dir, json_file=self.train_ann, img_size=self.input_size, preproc=TrainTransform( max_labels=50, flip_prob=self.flip_prob, hsv_prob=self.hsv_prob), cache=cache_img, ) dataset = MosaicDetection( dataset, mosaic=not no_aug, img_size=self.input_size, preproc=TrainTransform( max_labels=120, flip_prob=self.flip_prob, hsv_prob=self.hsv_prob), degrees=self.degrees, translate=self.translate, mosaic_scale=self.mosaic_scale, mixup_scale=self.mixup_scale, shear=self.shear, enable_mixup=self.enable_mixup, mosaic_prob=self.mosaic_prob, mixup_prob=self.mixup_prob, ) self.dataset = dataset if is_distributed: batch_size = batch_size // dist.get_world_size() sampler = InfiniteSampler(len(self.dataset), seed=self.seed if self.seed else 0) batch_sampler = YoloBatchSampler( sampler=sampler, batch_size=batch_size, drop_last=False, mosaic=not no_aug, ) dataloader_kwargs = {"num_workers": self.data_num_workers, "pin_memory": True} dataloader_kwargs["batch_sampler"] = batch_sampler # Make sure each process has different random seed, especially for 'fork' method. # Check https://github.com/pytorch/pytorch/issues/63311 for more details. dataloader_kwargs["worker_init_fn"] = worker_init_reset_seed train_loader = DataLoader(self.dataset, **dataloader_kwargs) return train_loader def random_resize(self, data_loader, epoch, rank, is_distributed): tensor = torch.LongTensor(2).cuda() if rank == 0: size_factor = self.input_size[1] * 1.0 / self.input_size[0] if not hasattr(self, 'random_size'): min_size = int(self.input_size[0] / 32) - self.multiscale_range max_size = int(self.input_size[0] / 32) + self.multiscale_range self.random_size = (min_size, max_size) size = random.randint(*self.random_size) size = (int(32 * size), 32 * int(size * size_factor)) tensor[0] = size[0] tensor[1] = size[1] if is_distributed: dist.barrier() dist.broadcast(tensor, 0) input_size = (tensor[0].item(), tensor[1].item()) return input_size def preprocess(self, inputs, targets, tsize): scale_y = tsize[0] / self.input_size[0] scale_x = tsize[1] / self.input_size[1] if scale_x != 1 or scale_y != 1: inputs = nn.functional.interpolate( inputs, size=tsize, mode="bilinear", align_corners=False ) targets[..., 1::2] = targets[..., 1::2] * scale_x targets[..., 2::2] = targets[..., 2::2] * scale_y return inputs, targets def get_optimizer(self, batch_size): if "optimizer" not in self.__dict__: if self.warmup_epochs > 0: lr = self.warmup_lr else: lr = self.basic_lr_per_img * batch_size pg0, pg1, pg2 = [], [], [] # optimizer parameter groups for k, v in self.model.named_modules(): if hasattr(v, "bias") and isinstance(v.bias, nn.Parameter): pg2.append(v.bias) # biases if isinstance(v, nn.BatchNorm2d) or "bn" in k: pg0.append(v.weight) # no decay elif hasattr(v, "weight") and isinstance(v.weight, nn.Parameter): pg1.append(v.weight) # apply decay optimizer = torch.optim.SGD( pg0, lr=lr, momentum=self.momentum, nesterov=True ) optimizer.add_param_group( {"params": pg1, "weight_decay": self.weight_decay} ) # add pg1 with weight_decay optimizer.add_param_group({"params": pg2}) self.optimizer = optimizer return self.optimizer def get_lr_scheduler(self, lr, iters_per_epoch): from asone.detectors.yolox.yolox.utils import LRScheduler scheduler = LRScheduler( self.scheduler, lr, iters_per_epoch, self.max_epoch, warmup_epochs=self.warmup_epochs, warmup_lr_start=self.warmup_lr, no_aug_epochs=self.no_aug_epochs, min_lr_ratio=self.min_lr_ratio, ) return scheduler def get_eval_loader(self, batch_size, is_distributed, testdev=False, legacy=False): from asone.detectors.yolox.yolox.data import COCODataset, ValTransform valdataset = COCODataset( data_dir=self.data_dir, json_file=self.val_ann if not testdev else self.test_ann, name="val2017" if not testdev else "test2017", img_size=self.test_size, preproc=ValTransform(legacy=legacy), ) if is_distributed: batch_size = batch_size // dist.get_world_size() sampler = torch.utils.data.distributed.DistributedSampler( valdataset, shuffle=False ) else: sampler = torch.utils.data.SequentialSampler(valdataset) dataloader_kwargs = { "num_workers": self.data_num_workers, "pin_memory": True, "sampler": sampler, } dataloader_kwargs["batch_size"] = batch_size val_loader = torch.utils.data.DataLoader(valdataset, **dataloader_kwargs) return val_loader def get_evaluator(self, batch_size, is_distributed, testdev=False, legacy=False): from asone.detectors.yolox.yolox.evaluators import COCOEvaluator val_loader = self.get_eval_loader(batch_size, is_distributed, testdev, legacy) evaluator = COCOEvaluator( dataloader=val_loader, img_size=self.test_size, confthre=self.test_conf, nmsthre=self.nmsthre, num_classes=self.num_classes, testdev=testdev, ) return evaluator def get_trainer(self, args): from asone.detectors.yolox.yolox.core import Trainer trainer = Trainer(self, args) # NOTE: trainer shouldn't be an attribute of exp object return trainer def eval(self, model, evaluator, is_distributed, half=False, return_outputs=False): return evaluator.evaluate(model, is_distributed, half, return_outputs=return_outputs) ================================================ FILE: asone/detectors/yolox/yolox/models/__init__.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. from .build import * from .darknet import CSPDarknet, Darknet from .losses import IOUloss from .yolo_fpn import YOLOFPN from .yolo_head import YOLOXHead from .yolo_pafpn import YOLOPAFPN from .yolox import YOLOX ================================================ FILE: asone/detectors/yolox/yolox/models/build.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- import torch from torch import nn from torch.hub import load_state_dict_from_url __all__ = [ "create_yolox_model", "yolox_nano", "yolox_tiny", "yolox_s", "yolox_m", "yolox_l", "yolox_x", "yolov3", "yolox_custom" ] _CKPT_ROOT_URL = "https://github.com/Megvii-BaseDetection/YOLOX/releases/download" _CKPT_FULL_PATH = { "yolox-nano": f"{_CKPT_ROOT_URL}/0.1.1rc0/yolox_nano.pth", "yolox-tiny": f"{_CKPT_ROOT_URL}/0.1.1rc0/yolox_tiny.pth", "yolox-s": f"{_CKPT_ROOT_URL}/0.1.1rc0/yolox_s.pth", "yolox-m": f"{_CKPT_ROOT_URL}/0.1.1rc0/yolox_m.pth", "yolox-l": f"{_CKPT_ROOT_URL}/0.1.1rc0/yolox_l.pth", "yolox-x": f"{_CKPT_ROOT_URL}/0.1.1rc0/yolox_x.pth", "yolov3": f"{_CKPT_ROOT_URL}/0.1.1rc0/yolox_darknet.pth", } def create_yolox_model(name: str, pretrained: bool = True, num_classes: int = 80, device=None, exp_path: str = None, ckpt_path: str = None) -> nn.Module: """creates and loads a YOLOX model Args: name (str): name of model. for example, "yolox-s", "yolox-tiny" or "yolox_custom" if you want to load your own model. pretrained (bool): load pretrained weights into the model. Default to True. device (str): default device to for model. Default to None. num_classes (int): number of model classes. Default to 80. exp_path (str): path to your own experiment file. Required if name="yolox_custom" ckpt_path (str): path to your own ckpt. Required if name="yolox_custom" and you want to load a pretrained model Returns: YOLOX model (nn.Module) """ from asone.detectors.yolox.yolox.exp import get_exp, Exp if device is None: device = "cuda:0" if torch.cuda.is_available() else "cpu" device = torch.device(device) assert name in _CKPT_FULL_PATH or name == "yolox_custom", \ f"user should use one of value in {_CKPT_FULL_PATH.keys()} or \"yolox_custom\"" if name in _CKPT_FULL_PATH: exp: Exp = get_exp(exp_name=name) exp.num_classes = num_classes yolox_model = exp.get_model() if pretrained and num_classes == 80: weights_url = _CKPT_FULL_PATH[name] ckpt = load_state_dict_from_url(weights_url, map_location="cpu") if "model" in ckpt: ckpt = ckpt["model"] yolox_model.load_state_dict(ckpt) else: assert exp_path is not None, "for a \"yolox_custom\" model exp_path must be provided" exp: Exp = get_exp(exp_file=exp_path) yolox_model = exp.get_model() if ckpt_path: ckpt = torch.load(ckpt_path, map_location="cpu") if "model" in ckpt: ckpt = ckpt["model"] yolox_model.load_state_dict(ckpt) yolox_model.to(device) return yolox_model def yolox_nano(pretrained: bool = True, num_classes: int = 80, device: str = None) -> nn.Module: return create_yolox_model("yolox-nano", pretrained, num_classes, device) def yolox_tiny(pretrained: bool = True, num_classes: int = 80, device: str = None) -> nn.Module: return create_yolox_model("yolox-tiny", pretrained, num_classes, device) def yolox_s(pretrained: bool = True, num_classes: int = 80, device: str = None) -> nn.Module: return create_yolox_model("yolox-s", pretrained, num_classes, device) def yolox_m(pretrained: bool = True, num_classes: int = 80, device: str = None) -> nn.Module: return create_yolox_model("yolox-m", pretrained, num_classes, device) def yolox_l(pretrained: bool = True, num_classes: int = 80, device: str = None) -> nn.Module: return create_yolox_model("yolox-l", pretrained, num_classes, device) def yolox_x(pretrained: bool = True, num_classes: int = 80, device: str = None) -> nn.Module: return create_yolox_model("yolox-x", pretrained, num_classes, device) def yolov3(pretrained: bool = True, num_classes: int = 80, device: str = None) -> nn.Module: return create_yolox_model("yolov3", pretrained, num_classes, device) def yolox_custom(ckpt_path: str = None, exp_path: str = None, device: str = None) -> nn.Module: return create_yolox_model("yolox_custom", ckpt_path=ckpt_path, exp_path=exp_path, device=device) ================================================ FILE: asone/detectors/yolox/yolox/models/darknet.py ================================================ #!/usr/bin/env python # -*- encoding: utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. from torch import nn from asone.detectors.yolox.yolox.models.network_blocks import BaseConv, CSPLayer, DWConv, Focus, ResLayer, SPPBottleneck class Darknet(nn.Module): # number of blocks from dark2 to dark5. depth2blocks = {21: [1, 2, 2, 1], 53: [2, 8, 8, 4]} def __init__( self, depth, in_channels=3, stem_out_channels=32, out_features=("dark3", "dark4", "dark5"), ): """ Args: depth (int): depth of darknet used in model, usually use [21, 53] for this param. in_channels (int): number of input channels, for example, use 3 for RGB image. stem_out_channels (int): number of output channels of darknet stem. It decides channels of darknet layer2 to layer5. out_features (Tuple[str]): desired output layer name. """ super().__init__() assert out_features, "please provide output features of Darknet" self.out_features = out_features self.stem = nn.Sequential( BaseConv(in_channels, stem_out_channels, ksize=3, stride=1, act="lrelu"), *self.make_group_layer(stem_out_channels, num_blocks=1, stride=2), ) in_channels = stem_out_channels * 2 # 64 num_blocks = Darknet.depth2blocks[depth] # create darknet with `stem_out_channels` and `num_blocks` layers. # to make model structure more clear, we don't use `for` statement in python. self.dark2 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[0], stride=2) ) in_channels *= 2 # 128 self.dark3 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[1], stride=2) ) in_channels *= 2 # 256 self.dark4 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[2], stride=2) ) in_channels *= 2 # 512 self.dark5 = nn.Sequential( *self.make_group_layer(in_channels, num_blocks[3], stride=2), *self.make_spp_block([in_channels, in_channels * 2], in_channels * 2), ) def make_group_layer(self, in_channels: int, num_blocks: int, stride: int = 1): "starts with conv layer then has `num_blocks` `ResLayer`" return [ BaseConv(in_channels, in_channels * 2, ksize=3, stride=stride, act="lrelu"), *[(ResLayer(in_channels * 2)) for _ in range(num_blocks)], ] def make_spp_block(self, filters_list, in_filters): m = nn.Sequential( *[ BaseConv(in_filters, filters_list[0], 1, stride=1, act="lrelu"), BaseConv(filters_list[0], filters_list[1], 3, stride=1, act="lrelu"), SPPBottleneck( in_channels=filters_list[1], out_channels=filters_list[0], activation="lrelu", ), BaseConv(filters_list[0], filters_list[1], 3, stride=1, act="lrelu"), BaseConv(filters_list[1], filters_list[0], 1, stride=1, act="lrelu"), ] ) return m def forward(self, x): outputs = {} x = self.stem(x) outputs["stem"] = x x = self.dark2(x) outputs["dark2"] = x x = self.dark3(x) outputs["dark3"] = x x = self.dark4(x) outputs["dark4"] = x x = self.dark5(x) outputs["dark5"] = x return {k: v for k, v in outputs.items() if k in self.out_features} class CSPDarknet(nn.Module): def __init__( self, dep_mul, wid_mul, out_features=("dark3", "dark4", "dark5"), depthwise=False, act="silu", ): super().__init__() assert out_features, "please provide output features of Darknet" self.out_features = out_features Conv = DWConv if depthwise else BaseConv base_channels = int(wid_mul * 64) # 64 base_depth = max(round(dep_mul * 3), 1) # 3 # stem self.stem = Focus(3, base_channels, ksize=3, act=act) # dark2 self.dark2 = nn.Sequential( Conv(base_channels, base_channels * 2, 3, 2, act=act), CSPLayer( base_channels * 2, base_channels * 2, n=base_depth, depthwise=depthwise, act=act, ), ) # dark3 self.dark3 = nn.Sequential( Conv(base_channels * 2, base_channels * 4, 3, 2, act=act), CSPLayer( base_channels * 4, base_channels * 4, n=base_depth * 3, depthwise=depthwise, act=act, ), ) # dark4 self.dark4 = nn.Sequential( Conv(base_channels * 4, base_channels * 8, 3, 2, act=act), CSPLayer( base_channels * 8, base_channels * 8, n=base_depth * 3, depthwise=depthwise, act=act, ), ) # dark5 self.dark5 = nn.Sequential( Conv(base_channels * 8, base_channels * 16, 3, 2, act=act), SPPBottleneck(base_channels * 16, base_channels * 16, activation=act), CSPLayer( base_channels * 16, base_channels * 16, n=base_depth, shortcut=False, depthwise=depthwise, act=act, ), ) def forward(self, x): outputs = {} x = self.stem(x) outputs["stem"] = x x = self.dark2(x) outputs["dark2"] = x x = self.dark3(x) outputs["dark3"] = x x = self.dark4(x) outputs["dark4"] = x x = self.dark5(x) outputs["dark5"] = x return {k: v for k, v in outputs.items() if k in self.out_features} ================================================ FILE: asone/detectors/yolox/yolox/models/losses.py ================================================ #!/usr/bin/env python # -*- encoding: utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import torch import torch.nn as nn class IOUloss(nn.Module): def __init__(self, reduction="none", loss_type="iou"): super(IOUloss, self).__init__() self.reduction = reduction self.loss_type = loss_type def forward(self, pred, target): assert pred.shape[0] == target.shape[0] pred = pred.view(-1, 4) target = target.view(-1, 4) tl = torch.max( (pred[:, :2] - pred[:, 2:] / 2), (target[:, :2] - target[:, 2:] / 2) ) br = torch.min( (pred[:, :2] + pred[:, 2:] / 2), (target[:, :2] + target[:, 2:] / 2) ) area_p = torch.prod(pred[:, 2:], 1) area_g = torch.prod(target[:, 2:], 1) en = (tl < br).type(tl.type()).prod(dim=1) area_i = torch.prod(br - tl, 1) * en area_u = area_p + area_g - area_i iou = (area_i) / (area_u + 1e-16) if self.loss_type == "iou": loss = 1 - iou ** 2 elif self.loss_type == "giou": c_tl = torch.min( (pred[:, :2] - pred[:, 2:] / 2), (target[:, :2] - target[:, 2:] / 2) ) c_br = torch.max( (pred[:, :2] + pred[:, 2:] / 2), (target[:, :2] + target[:, 2:] / 2) ) area_c = torch.prod(c_br - c_tl, 1) giou = iou - (area_c - area_u) / area_c.clamp(1e-16) loss = 1 - giou.clamp(min=-1.0, max=1.0) if self.reduction == "mean": loss = loss.mean() elif self.reduction == "sum": loss = loss.sum() return loss ================================================ FILE: asone/detectors/yolox/yolox/models/network_blocks.py ================================================ #!/usr/bin/env python # -*- encoding: utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import torch import torch.nn as nn class SiLU(nn.Module): """export-friendly version of nn.SiLU()""" @staticmethod def forward(x): return x * torch.sigmoid(x) def get_activation(name="silu", inplace=True): if name == "silu": module = nn.SiLU(inplace=inplace) elif name == "relu": module = nn.ReLU(inplace=inplace) elif name == "lrelu": module = nn.LeakyReLU(0.1, inplace=inplace) else: raise AttributeError("Unsupported act type: {}".format(name)) return module class BaseConv(nn.Module): """A Conv2d -> Batchnorm -> silu/leaky relu block""" def __init__( self, in_channels, out_channels, ksize, stride, groups=1, bias=False, act="silu" ): super().__init__() # same padding pad = (ksize - 1) // 2 self.conv = nn.Conv2d( in_channels, out_channels, kernel_size=ksize, stride=stride, padding=pad, groups=groups, bias=bias, ) self.bn = nn.BatchNorm2d(out_channels) self.act = get_activation(act, inplace=True) def forward(self, x): return self.act(self.bn(self.conv(x))) def fuseforward(self, x): return self.act(self.conv(x)) class DWConv(nn.Module): """Depthwise Conv + Conv""" def __init__(self, in_channels, out_channels, ksize, stride=1, act="silu"): super().__init__() self.dconv = BaseConv( in_channels, in_channels, ksize=ksize, stride=stride, groups=in_channels, act=act, ) self.pconv = BaseConv( in_channels, out_channels, ksize=1, stride=1, groups=1, act=act ) def forward(self, x): x = self.dconv(x) return self.pconv(x) class Bottleneck(nn.Module): # Standard bottleneck def __init__( self, in_channels, out_channels, shortcut=True, expansion=0.5, depthwise=False, act="silu", ): super().__init__() hidden_channels = int(out_channels * expansion) Conv = DWConv if depthwise else BaseConv self.conv1 = BaseConv(in_channels, hidden_channels, 1, stride=1, act=act) self.conv2 = Conv(hidden_channels, out_channels, 3, stride=1, act=act) self.use_add = shortcut and in_channels == out_channels def forward(self, x): y = self.conv2(self.conv1(x)) if self.use_add: y = y + x return y class ResLayer(nn.Module): "Residual layer with `in_channels` inputs." def __init__(self, in_channels: int): super().__init__() mid_channels = in_channels // 2 self.layer1 = BaseConv( in_channels, mid_channels, ksize=1, stride=1, act="lrelu" ) self.layer2 = BaseConv( mid_channels, in_channels, ksize=3, stride=1, act="lrelu" ) def forward(self, x): out = self.layer2(self.layer1(x)) return x + out class SPPBottleneck(nn.Module): """Spatial pyramid pooling layer used in YOLOv3-SPP""" def __init__( self, in_channels, out_channels, kernel_sizes=(5, 9, 13), activation="silu" ): super().__init__() hidden_channels = in_channels // 2 self.conv1 = BaseConv(in_channels, hidden_channels, 1, stride=1, act=activation) self.m = nn.ModuleList( [ nn.MaxPool2d(kernel_size=ks, stride=1, padding=ks // 2) for ks in kernel_sizes ] ) conv2_channels = hidden_channels * (len(kernel_sizes) + 1) self.conv2 = BaseConv(conv2_channels, out_channels, 1, stride=1, act=activation) def forward(self, x): x = self.conv1(x) x = torch.cat([x] + [m(x) for m in self.m], dim=1) x = self.conv2(x) return x class CSPLayer(nn.Module): """C3 in yolov5, CSP Bottleneck with 3 convolutions""" def __init__( self, in_channels, out_channels, n=1, shortcut=True, expansion=0.5, depthwise=False, act="silu", ): """ Args: in_channels (int): input channels. out_channels (int): output channels. n (int): number of Bottlenecks. Default value: 1. """ # ch_in, ch_out, number, shortcut, groups, expansion super().__init__() hidden_channels = int(out_channels * expansion) # hidden channels self.conv1 = BaseConv(in_channels, hidden_channels, 1, stride=1, act=act) self.conv2 = BaseConv(in_channels, hidden_channels, 1, stride=1, act=act) self.conv3 = BaseConv(2 * hidden_channels, out_channels, 1, stride=1, act=act) module_list = [ Bottleneck( hidden_channels, hidden_channels, shortcut, 1.0, depthwise, act=act ) for _ in range(n) ] self.m = nn.Sequential(*module_list) def forward(self, x): x_1 = self.conv1(x) x_2 = self.conv2(x) x_1 = self.m(x_1) x = torch.cat((x_1, x_2), dim=1) return self.conv3(x) class Focus(nn.Module): """Focus width and height information into channel space.""" def __init__(self, in_channels, out_channels, ksize=1, stride=1, act="silu"): super().__init__() self.conv = BaseConv(in_channels * 4, out_channels, ksize, stride, act=act) def forward(self, x): # shape of x (b,c,w,h) -> y(b,4c,w/2,h/2) patch_top_left = x[..., ::2, ::2] patch_top_right = x[..., ::2, 1::2] patch_bot_left = x[..., 1::2, ::2] patch_bot_right = x[..., 1::2, 1::2] x = torch.cat( ( patch_top_left, patch_bot_left, patch_top_right, patch_bot_right, ), dim=1, ) return self.conv(x) ================================================ FILE: asone/detectors/yolox/yolox/models/yolo_fpn.py ================================================ #!/usr/bin/env python # -*- encoding: utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import torch import torch.nn as nn from asone.detectors.yolox.yolox.models.darknet import Darknet from asone.detectors.yolox.yolox.models.network_blocks import BaseConv class YOLOFPN(nn.Module): """ YOLOFPN module. Darknet 53 is the default backbone of this model. """ def __init__( self, depth=53, in_features=["dark3", "dark4", "dark5"], ): super().__init__() self.backbone = Darknet(depth) self.in_features = in_features # out 1 self.out1_cbl = self._make_cbl(512, 256, 1) self.out1 = self._make_embedding([256, 512], 512 + 256) # out 2 self.out2_cbl = self._make_cbl(256, 128, 1) self.out2 = self._make_embedding([128, 256], 256 + 128) # upsample self.upsample = nn.Upsample(scale_factor=2, mode="nearest") def _make_cbl(self, _in, _out, ks): return BaseConv(_in, _out, ks, stride=1, act="lrelu") def _make_embedding(self, filters_list, in_filters): m = nn.Sequential( *[ self._make_cbl(in_filters, filters_list[0], 1), self._make_cbl(filters_list[0], filters_list[1], 3), self._make_cbl(filters_list[1], filters_list[0], 1), self._make_cbl(filters_list[0], filters_list[1], 3), self._make_cbl(filters_list[1], filters_list[0], 1), ] ) return m def load_pretrained_model(self, filename="./weights/darknet53.mix.pth"): with open(filename, "rb") as f: state_dict = torch.load(f, map_location="cpu") print("loading pretrained weights...") self.backbone.load_state_dict(state_dict) def forward(self, inputs): """ Args: inputs (Tensor): input image. Returns: Tuple[Tensor]: FPN output features.. """ # backbone out_features = self.backbone(inputs) x2, x1, x0 = [out_features[f] for f in self.in_features] # yolo branch 1 x1_in = self.out1_cbl(x0) x1_in = self.upsample(x1_in) x1_in = torch.cat([x1_in, x1], 1) out_dark4 = self.out1(x1_in) # yolo branch 2 x2_in = self.out2_cbl(out_dark4) x2_in = self.upsample(x2_in) x2_in = torch.cat([x2_in, x2], 1) out_dark3 = self.out2(x2_in) outputs = (out_dark3, out_dark4, x0) return outputs ================================================ FILE: asone/detectors/yolox/yolox/models/yolo_head.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import math from loguru import logger import torch import torch.nn as nn import torch.nn.functional as F from asone.detectors.yolox.yolox.utils import bboxes_iou, meshgrid from asone.detectors.yolox.yolox.models.losses import IOUloss from asone.detectors.yolox.yolox.models.network_blocks import BaseConv, DWConv class YOLOXHead(nn.Module): def __init__( self, num_classes, width=1.0, strides=[8, 16, 32], in_channels=[256, 512, 1024], act="silu", depthwise=False, ): """ Args: act (str): activation type of conv. Defalut value: "silu". depthwise (bool): whether apply depthwise conv in conv branch. Defalut value: False. """ super().__init__() self.n_anchors = 1 self.num_classes = num_classes self.decode_in_inference = True # for deploy, set to False self.cls_convs = nn.ModuleList() self.reg_convs = nn.ModuleList() self.cls_preds = nn.ModuleList() self.reg_preds = nn.ModuleList() self.obj_preds = nn.ModuleList() self.stems = nn.ModuleList() Conv = DWConv if depthwise else BaseConv for i in range(len(in_channels)): self.stems.append( BaseConv( in_channels=int(in_channels[i] * width), out_channels=int(256 * width), ksize=1, stride=1, act=act, ) ) self.cls_convs.append( nn.Sequential( *[ Conv( in_channels=int(256 * width), out_channels=int(256 * width), ksize=3, stride=1, act=act, ), Conv( in_channels=int(256 * width), out_channels=int(256 * width), ksize=3, stride=1, act=act, ), ] ) ) self.reg_convs.append( nn.Sequential( *[ Conv( in_channels=int(256 * width), out_channels=int(256 * width), ksize=3, stride=1, act=act, ), Conv( in_channels=int(256 * width), out_channels=int(256 * width), ksize=3, stride=1, act=act, ), ] ) ) self.cls_preds.append( nn.Conv2d( in_channels=int(256 * width), out_channels=self.n_anchors * self.num_classes, kernel_size=1, stride=1, padding=0, ) ) self.reg_preds.append( nn.Conv2d( in_channels=int(256 * width), out_channels=4, kernel_size=1, stride=1, padding=0, ) ) self.obj_preds.append( nn.Conv2d( in_channels=int(256 * width), out_channels=self.n_anchors * 1, kernel_size=1, stride=1, padding=0, ) ) self.use_l1 = False self.l1_loss = nn.L1Loss(reduction="none") self.bcewithlog_loss = nn.BCEWithLogitsLoss(reduction="none") self.iou_loss = IOUloss(reduction="none") self.strides = strides self.grids = [torch.zeros(1)] * len(in_channels) def initialize_biases(self, prior_prob): for conv in self.cls_preds: b = conv.bias.view(self.n_anchors, -1) b.data.fill_(-math.log((1 - prior_prob) / prior_prob)) conv.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) for conv in self.obj_preds: b = conv.bias.view(self.n_anchors, -1) b.data.fill_(-math.log((1 - prior_prob) / prior_prob)) conv.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) def forward(self, xin, labels=None, imgs=None): outputs = [] origin_preds = [] x_shifts = [] y_shifts = [] expanded_strides = [] for k, (cls_conv, reg_conv, stride_this_level, x) in enumerate( zip(self.cls_convs, self.reg_convs, self.strides, xin) ): x = self.stems[k](x) cls_x = x reg_x = x cls_feat = cls_conv(cls_x) cls_output = self.cls_preds[k](cls_feat) reg_feat = reg_conv(reg_x) reg_output = self.reg_preds[k](reg_feat) obj_output = self.obj_preds[k](reg_feat) if self.training: output = torch.cat([reg_output, obj_output, cls_output], 1) output, grid = self.get_output_and_grid( output, k, stride_this_level, xin[0].type() ) x_shifts.append(grid[:, :, 0]) y_shifts.append(grid[:, :, 1]) expanded_strides.append( torch.zeros(1, grid.shape[1]) .fill_(stride_this_level) .type_as(xin[0]) ) if self.use_l1: batch_size = reg_output.shape[0] hsize, wsize = reg_output.shape[-2:] reg_output = reg_output.view( batch_size, self.n_anchors, 4, hsize, wsize ) reg_output = reg_output.permute(0, 1, 3, 4, 2).reshape( batch_size, -1, 4 ) origin_preds.append(reg_output.clone()) else: output = torch.cat( [reg_output, obj_output.sigmoid(), cls_output.sigmoid()], 1 ) outputs.append(output) if self.training: return self.get_losses( imgs, x_shifts, y_shifts, expanded_strides, labels, torch.cat(outputs, 1), origin_preds, dtype=xin[0].dtype, ) else: self.hw = [x.shape[-2:] for x in outputs] # [batch, n_anchors_all, 85] outputs = torch.cat( [x.flatten(start_dim=2) for x in outputs], dim=2 ).permute(0, 2, 1) if self.decode_in_inference: return self.decode_outputs(outputs, dtype=xin[0].type()) else: return outputs def get_output_and_grid(self, output, k, stride, dtype): grid = self.grids[k] batch_size = output.shape[0] n_ch = 5 + self.num_classes hsize, wsize = output.shape[-2:] if grid.shape[2:4] != output.shape[2:4]: yv, xv = meshgrid([torch.arange(hsize), torch.arange(wsize)]) grid = torch.stack((xv, yv), 2).view(1, 1, hsize, wsize, 2).type(dtype) self.grids[k] = grid output = output.view(batch_size, self.n_anchors, n_ch, hsize, wsize) output = output.permute(0, 1, 3, 4, 2).reshape( batch_size, self.n_anchors * hsize * wsize, -1 ) grid = grid.view(1, -1, 2) output[..., :2] = (output[..., :2] + grid) * stride output[..., 2:4] = torch.exp(output[..., 2:4]) * stride return output, grid def decode_outputs(self, outputs, dtype): grids = [] strides = [] for (hsize, wsize), stride in zip(self.hw, self.strides): yv, xv = meshgrid([torch.arange(hsize), torch.arange(wsize)]) grid = torch.stack((xv, yv), 2).view(1, -1, 2) grids.append(grid) shape = grid.shape[:2] strides.append(torch.full((*shape, 1), stride)) grids = torch.cat(grids, dim=1).type(dtype) strides = torch.cat(strides, dim=1).type(dtype) outputs[..., :2] = (outputs[..., :2] + grids) * strides outputs[..., 2:4] = torch.exp(outputs[..., 2:4]) * strides return outputs def get_losses( self, imgs, x_shifts, y_shifts, expanded_strides, labels, outputs, origin_preds, dtype, ): bbox_preds = outputs[:, :, :4] # [batch, n_anchors_all, 4] obj_preds = outputs[:, :, 4].unsqueeze(-1) # [batch, n_anchors_all, 1] cls_preds = outputs[:, :, 5:] # [batch, n_anchors_all, n_cls] # calculate targets nlabel = (labels.sum(dim=2) > 0).sum(dim=1) # number of objects total_num_anchors = outputs.shape[1] x_shifts = torch.cat(x_shifts, 1) # [1, n_anchors_all] y_shifts = torch.cat(y_shifts, 1) # [1, n_anchors_all] expanded_strides = torch.cat(expanded_strides, 1) if self.use_l1: origin_preds = torch.cat(origin_preds, 1) cls_targets = [] reg_targets = [] l1_targets = [] obj_targets = [] fg_masks = [] num_fg = 0.0 num_gts = 0.0 for batch_idx in range(outputs.shape[0]): num_gt = int(nlabel[batch_idx]) num_gts += num_gt if num_gt == 0: cls_target = outputs.new_zeros((0, self.num_classes)) reg_target = outputs.new_zeros((0, 4)) l1_target = outputs.new_zeros((0, 4)) obj_target = outputs.new_zeros((total_num_anchors, 1)) fg_mask = outputs.new_zeros(total_num_anchors).bool() else: gt_bboxes_per_image = labels[batch_idx, :num_gt, 1:5] gt_classes = labels[batch_idx, :num_gt, 0] bboxes_preds_per_image = bbox_preds[batch_idx] try: ( gt_matched_classes, fg_mask, pred_ious_this_matching, matched_gt_inds, num_fg_img, ) = self.get_assignments( # noqa batch_idx, num_gt, total_num_anchors, gt_bboxes_per_image, gt_classes, bboxes_preds_per_image, expanded_strides, x_shifts, y_shifts, cls_preds, bbox_preds, obj_preds, labels, imgs, ) except RuntimeError as e: # TODO: the string might change, consider a better way if "CUDA out of memory. " not in str(e): raise # RuntimeError might not caused by CUDA OOM logger.error( "OOM RuntimeError is raised due to the huge memory cost during label assignment. \ CPU mode is applied in this batch. If you want to avoid this issue, \ try to reduce the batch size or image size." ) torch.cuda.empty_cache() ( gt_matched_classes, fg_mask, pred_ious_this_matching, matched_gt_inds, num_fg_img, ) = self.get_assignments( # noqa batch_idx, num_gt, total_num_anchors, gt_bboxes_per_image, gt_classes, bboxes_preds_per_image, expanded_strides, x_shifts, y_shifts, cls_preds, bbox_preds, obj_preds, labels, imgs, "cpu", ) torch.cuda.empty_cache() num_fg += num_fg_img cls_target = F.one_hot( gt_matched_classes.to(torch.int64), self.num_classes ) * pred_ious_this_matching.unsqueeze(-1) obj_target = fg_mask.unsqueeze(-1) reg_target = gt_bboxes_per_image[matched_gt_inds] if self.use_l1: l1_target = self.get_l1_target( outputs.new_zeros((num_fg_img, 4)), gt_bboxes_per_image[matched_gt_inds], expanded_strides[0][fg_mask], x_shifts=x_shifts[0][fg_mask], y_shifts=y_shifts[0][fg_mask], ) cls_targets.append(cls_target) reg_targets.append(reg_target) obj_targets.append(obj_target.to(dtype)) fg_masks.append(fg_mask) if self.use_l1: l1_targets.append(l1_target) cls_targets = torch.cat(cls_targets, 0) reg_targets = torch.cat(reg_targets, 0) obj_targets = torch.cat(obj_targets, 0) fg_masks = torch.cat(fg_masks, 0) if self.use_l1: l1_targets = torch.cat(l1_targets, 0) num_fg = max(num_fg, 1) loss_iou = ( self.iou_loss(bbox_preds.view(-1, 4)[fg_masks], reg_targets) ).sum() / num_fg loss_obj = ( self.bcewithlog_loss(obj_preds.view(-1, 1), obj_targets) ).sum() / num_fg loss_cls = ( self.bcewithlog_loss( cls_preds.view(-1, self.num_classes)[fg_masks], cls_targets ) ).sum() / num_fg if self.use_l1: loss_l1 = ( self.l1_loss(origin_preds.view(-1, 4)[fg_masks], l1_targets) ).sum() / num_fg else: loss_l1 = 0.0 reg_weight = 5.0 loss = reg_weight * loss_iou + loss_obj + loss_cls + loss_l1 return ( loss, reg_weight * loss_iou, loss_obj, loss_cls, loss_l1, num_fg / max(num_gts, 1), ) def get_l1_target(self, l1_target, gt, stride, x_shifts, y_shifts, eps=1e-8): l1_target[:, 0] = gt[:, 0] / stride - x_shifts l1_target[:, 1] = gt[:, 1] / stride - y_shifts l1_target[:, 2] = torch.log(gt[:, 2] / stride + eps) l1_target[:, 3] = torch.log(gt[:, 3] / stride + eps) return l1_target @torch.no_grad() def get_assignments( self, batch_idx, num_gt, total_num_anchors, gt_bboxes_per_image, gt_classes, bboxes_preds_per_image, expanded_strides, x_shifts, y_shifts, cls_preds, bbox_preds, obj_preds, labels, imgs, mode="gpu", ): if mode == "cpu": print("------------CPU Mode for This Batch-------------") gt_bboxes_per_image = gt_bboxes_per_image.cpu().float() bboxes_preds_per_image = bboxes_preds_per_image.cpu().float() gt_classes = gt_classes.cpu().float() expanded_strides = expanded_strides.cpu().float() x_shifts = x_shifts.cpu() y_shifts = y_shifts.cpu() fg_mask, is_in_boxes_and_center = self.get_in_boxes_info( gt_bboxes_per_image, expanded_strides, x_shifts, y_shifts, total_num_anchors, num_gt, ) bboxes_preds_per_image = bboxes_preds_per_image[fg_mask] cls_preds_ = cls_preds[batch_idx][fg_mask] obj_preds_ = obj_preds[batch_idx][fg_mask] num_in_boxes_anchor = bboxes_preds_per_image.shape[0] if mode == "cpu": gt_bboxes_per_image = gt_bboxes_per_image.cpu() bboxes_preds_per_image = bboxes_preds_per_image.cpu() pair_wise_ious = bboxes_iou(gt_bboxes_per_image, bboxes_preds_per_image, False) gt_cls_per_image = ( F.one_hot(gt_classes.to(torch.int64), self.num_classes) .float() .unsqueeze(1) .repeat(1, num_in_boxes_anchor, 1) ) pair_wise_ious_loss = -torch.log(pair_wise_ious + 1e-8) if mode == "cpu": cls_preds_, obj_preds_ = cls_preds_.cpu(), obj_preds_.cpu() with torch.cuda.amp.autocast(enabled=False): cls_preds_ = ( cls_preds_.float().unsqueeze(0).repeat(num_gt, 1, 1).sigmoid_() * obj_preds_.float().unsqueeze(0).repeat(num_gt, 1, 1).sigmoid_() ) pair_wise_cls_loss = F.binary_cross_entropy( cls_preds_.sqrt_(), gt_cls_per_image, reduction="none" ).sum(-1) del cls_preds_ cost = ( pair_wise_cls_loss + 3.0 * pair_wise_ious_loss + 100000.0 * (~is_in_boxes_and_center) ) ( num_fg, gt_matched_classes, pred_ious_this_matching, matched_gt_inds, ) = self.dynamic_k_matching(cost, pair_wise_ious, gt_classes, num_gt, fg_mask) del pair_wise_cls_loss, cost, pair_wise_ious, pair_wise_ious_loss if mode == "cpu": gt_matched_classes = gt_matched_classes.cuda() fg_mask = fg_mask.cuda() pred_ious_this_matching = pred_ious_this_matching.cuda() matched_gt_inds = matched_gt_inds.cuda() return ( gt_matched_classes, fg_mask, pred_ious_this_matching, matched_gt_inds, num_fg, ) def get_in_boxes_info( self, gt_bboxes_per_image, expanded_strides, x_shifts, y_shifts, total_num_anchors, num_gt, ): expanded_strides_per_image = expanded_strides[0] x_shifts_per_image = x_shifts[0] * expanded_strides_per_image y_shifts_per_image = y_shifts[0] * expanded_strides_per_image x_centers_per_image = ( (x_shifts_per_image + 0.5 * expanded_strides_per_image) .unsqueeze(0) .repeat(num_gt, 1) ) # [n_anchor] -> [n_gt, n_anchor] y_centers_per_image = ( (y_shifts_per_image + 0.5 * expanded_strides_per_image) .unsqueeze(0) .repeat(num_gt, 1) ) gt_bboxes_per_image_l = ( (gt_bboxes_per_image[:, 0] - 0.5 * gt_bboxes_per_image[:, 2]) .unsqueeze(1) .repeat(1, total_num_anchors) ) gt_bboxes_per_image_r = ( (gt_bboxes_per_image[:, 0] + 0.5 * gt_bboxes_per_image[:, 2]) .unsqueeze(1) .repeat(1, total_num_anchors) ) gt_bboxes_per_image_t = ( (gt_bboxes_per_image[:, 1] - 0.5 * gt_bboxes_per_image[:, 3]) .unsqueeze(1) .repeat(1, total_num_anchors) ) gt_bboxes_per_image_b = ( (gt_bboxes_per_image[:, 1] + 0.5 * gt_bboxes_per_image[:, 3]) .unsqueeze(1) .repeat(1, total_num_anchors) ) b_l = x_centers_per_image - gt_bboxes_per_image_l b_r = gt_bboxes_per_image_r - x_centers_per_image b_t = y_centers_per_image - gt_bboxes_per_image_t b_b = gt_bboxes_per_image_b - y_centers_per_image bbox_deltas = torch.stack([b_l, b_t, b_r, b_b], 2) is_in_boxes = bbox_deltas.min(dim=-1).values > 0.0 is_in_boxes_all = is_in_boxes.sum(dim=0) > 0 # in fixed center center_radius = 2.5 gt_bboxes_per_image_l = (gt_bboxes_per_image[:, 0]).unsqueeze(1).repeat( 1, total_num_anchors ) - center_radius * expanded_strides_per_image.unsqueeze(0) gt_bboxes_per_image_r = (gt_bboxes_per_image[:, 0]).unsqueeze(1).repeat( 1, total_num_anchors ) + center_radius * expanded_strides_per_image.unsqueeze(0) gt_bboxes_per_image_t = (gt_bboxes_per_image[:, 1]).unsqueeze(1).repeat( 1, total_num_anchors ) - center_radius * expanded_strides_per_image.unsqueeze(0) gt_bboxes_per_image_b = (gt_bboxes_per_image[:, 1]).unsqueeze(1).repeat( 1, total_num_anchors ) + center_radius * expanded_strides_per_image.unsqueeze(0) c_l = x_centers_per_image - gt_bboxes_per_image_l c_r = gt_bboxes_per_image_r - x_centers_per_image c_t = y_centers_per_image - gt_bboxes_per_image_t c_b = gt_bboxes_per_image_b - y_centers_per_image center_deltas = torch.stack([c_l, c_t, c_r, c_b], 2) is_in_centers = center_deltas.min(dim=-1).values > 0.0 is_in_centers_all = is_in_centers.sum(dim=0) > 0 # in boxes and in centers is_in_boxes_anchor = is_in_boxes_all | is_in_centers_all is_in_boxes_and_center = ( is_in_boxes[:, is_in_boxes_anchor] & is_in_centers[:, is_in_boxes_anchor] ) return is_in_boxes_anchor, is_in_boxes_and_center def dynamic_k_matching(self, cost, pair_wise_ious, gt_classes, num_gt, fg_mask): # Dynamic K # --------------------------------------------------------------- matching_matrix = torch.zeros_like(cost, dtype=torch.uint8) ious_in_boxes_matrix = pair_wise_ious n_candidate_k = min(10, ious_in_boxes_matrix.size(1)) topk_ious, _ = torch.topk(ious_in_boxes_matrix, n_candidate_k, dim=1) dynamic_ks = torch.clamp(topk_ious.sum(1).int(), min=1) dynamic_ks = dynamic_ks.tolist() for gt_idx in range(num_gt): _, pos_idx = torch.topk( cost[gt_idx], k=dynamic_ks[gt_idx], largest=False ) matching_matrix[gt_idx][pos_idx] = 1 del topk_ious, dynamic_ks, pos_idx anchor_matching_gt = matching_matrix.sum(0) if (anchor_matching_gt > 1).sum() > 0: _, cost_argmin = torch.min(cost[:, anchor_matching_gt > 1], dim=0) matching_matrix[:, anchor_matching_gt > 1] *= 0 matching_matrix[cost_argmin, anchor_matching_gt > 1] = 1 fg_mask_inboxes = matching_matrix.sum(0) > 0 num_fg = fg_mask_inboxes.sum().item() fg_mask[fg_mask.clone()] = fg_mask_inboxes matched_gt_inds = matching_matrix[:, fg_mask_inboxes].argmax(0) gt_matched_classes = gt_classes[matched_gt_inds] pred_ious_this_matching = (matching_matrix * pair_wise_ious).sum(0)[ fg_mask_inboxes ] return num_fg, gt_matched_classes, pred_ious_this_matching, matched_gt_inds ================================================ FILE: asone/detectors/yolox/yolox/models/yolo_pafpn.py ================================================ #!/usr/bin/env python # -*- encoding: utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import torch import torch.nn as nn from asone.detectors.yolox.yolox.models.darknet import CSPDarknet from asone.detectors.yolox.yolox.models.network_blocks import BaseConv, CSPLayer, DWConv class YOLOPAFPN(nn.Module): """ YOLOv3 model. Darknet 53 is the default backbone of this model. """ def __init__( self, depth=1.0, width=1.0, in_features=("dark3", "dark4", "dark5"), in_channels=[256, 512, 1024], depthwise=False, act="silu", ): super().__init__() self.backbone = CSPDarknet(depth, width, depthwise=depthwise, act=act) self.in_features = in_features self.in_channels = in_channels Conv = DWConv if depthwise else BaseConv self.upsample = nn.Upsample(scale_factor=2, mode="nearest") self.lateral_conv0 = BaseConv( int(in_channels[2] * width), int(in_channels[1] * width), 1, 1, act=act ) self.C3_p4 = CSPLayer( int(2 * in_channels[1] * width), int(in_channels[1] * width), round(3 * depth), False, depthwise=depthwise, act=act, ) # cat self.reduce_conv1 = BaseConv( int(in_channels[1] * width), int(in_channels[0] * width), 1, 1, act=act ) self.C3_p3 = CSPLayer( int(2 * in_channels[0] * width), int(in_channels[0] * width), round(3 * depth), False, depthwise=depthwise, act=act, ) # bottom-up conv self.bu_conv2 = Conv( int(in_channels[0] * width), int(in_channels[0] * width), 3, 2, act=act ) self.C3_n3 = CSPLayer( int(2 * in_channels[0] * width), int(in_channels[1] * width), round(3 * depth), False, depthwise=depthwise, act=act, ) # bottom-up conv self.bu_conv1 = Conv( int(in_channels[1] * width), int(in_channels[1] * width), 3, 2, act=act ) self.C3_n4 = CSPLayer( int(2 * in_channels[1] * width), int(in_channels[2] * width), round(3 * depth), False, depthwise=depthwise, act=act, ) def forward(self, input): """ Args: inputs: input images. Returns: Tuple[Tensor]: FPN feature. """ # backbone out_features = self.backbone(input) features = [out_features[f] for f in self.in_features] [x2, x1, x0] = features fpn_out0 = self.lateral_conv0(x0) # 1024->512/32 f_out0 = self.upsample(fpn_out0) # 512/16 f_out0 = torch.cat([f_out0, x1], 1) # 512->1024/16 f_out0 = self.C3_p4(f_out0) # 1024->512/16 fpn_out1 = self.reduce_conv1(f_out0) # 512->256/16 f_out1 = self.upsample(fpn_out1) # 256/8 f_out1 = torch.cat([f_out1, x2], 1) # 256->512/8 pan_out2 = self.C3_p3(f_out1) # 512->256/8 p_out1 = self.bu_conv2(pan_out2) # 256->256/16 p_out1 = torch.cat([p_out1, fpn_out1], 1) # 256->512/16 pan_out1 = self.C3_n3(p_out1) # 512->512/16 p_out0 = self.bu_conv1(pan_out1) # 512->512/32 p_out0 = torch.cat([p_out0, fpn_out0], 1) # 512->1024/32 pan_out0 = self.C3_n4(p_out0) # 1024->1024/32 outputs = (pan_out2, pan_out1, pan_out0) return outputs ================================================ FILE: asone/detectors/yolox/yolox/models/yolox.py ================================================ #!/usr/bin/env python # -*- encoding: utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import torch.nn as nn from asone.detectors.yolox.yolox.models.yolo_head import YOLOXHead from asone.detectors.yolox.yolox.models.yolo_pafpn import YOLOPAFPN class YOLOX(nn.Module): """ YOLOX model module. The module list is defined by create_yolov3_modules function. The network returns loss values from three YOLO layers during training and detection results during test. """ def __init__(self, backbone=None, head=None): super().__init__() if backbone is None: backbone = YOLOPAFPN() if head is None: head = YOLOXHead(80) self.backbone = backbone self.head = head def forward(self, x, targets=None): # fpn output content features of [dark3, dark4, dark5] fpn_outs = self.backbone(x) if self.training: assert targets is not None loss, iou_loss, conf_loss, cls_loss, l1_loss, num_fg = self.head( fpn_outs, targets, x ) outputs = { "total_loss": loss, "iou_loss": iou_loss, "l1_loss": l1_loss, "conf_loss": conf_loss, "cls_loss": cls_loss, "num_fg": num_fg, } else: outputs = self.head(fpn_outs) return outputs ================================================ FILE: asone/detectors/yolox/yolox/utils/__init__.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. from .allreduce_norm import * from .boxes import * from .checkpoint import load_ckpt, save_checkpoint from .compat import meshgrid from .demo_utils import * from .dist import * from .ema import * from .logger import WandbLogger, setup_logger from .lr_scheduler import LRScheduler from .metric import * from .model_utils import * from .setup_env import * from .visualize import * ================================================ FILE: asone/detectors/yolox/yolox/utils/allreduce_norm.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import pickle from collections import OrderedDict import torch from torch import distributed as dist from torch import nn from asone.detectors.yolox.yolox.utils.dist import _get_global_gloo_group, get_world_size ASYNC_NORM = ( nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d, nn.InstanceNorm1d, nn.InstanceNorm2d, nn.InstanceNorm3d, ) __all__ = [ "get_async_norm_states", "pyobj2tensor", "tensor2pyobj", "all_reduce", "all_reduce_norm", ] def get_async_norm_states(module): async_norm_states = OrderedDict() for name, child in module.named_modules(): if isinstance(child, ASYNC_NORM): for k, v in child.state_dict().items(): async_norm_states[".".join([name, k])] = v return async_norm_states def pyobj2tensor(pyobj, device="cuda"): """serialize picklable python object to tensor""" storage = torch.ByteStorage.from_buffer(pickle.dumps(pyobj)) return torch.ByteTensor(storage).to(device=device) def tensor2pyobj(tensor): """deserialize tensor to picklable python object""" return pickle.loads(tensor.cpu().numpy().tobytes()) def _get_reduce_op(op_name): return { "sum": dist.ReduceOp.SUM, "mean": dist.ReduceOp.SUM, }[op_name.lower()] def all_reduce(py_dict, op="sum", group=None): """ Apply all reduce function for python dict object. NOTE: make sure that every py_dict has the same keys and values are in the same shape. Args: py_dict (dict): dict to apply all reduce op. op (str): operator, could be "sum" or "mean". """ world_size = get_world_size() if world_size == 1: return py_dict if group is None: group = _get_global_gloo_group() if dist.get_world_size(group) == 1: return py_dict # all reduce logic across different devices. py_key = list(py_dict.keys()) py_key_tensor = pyobj2tensor(py_key) dist.broadcast(py_key_tensor, src=0) py_key = tensor2pyobj(py_key_tensor) tensor_shapes = [py_dict[k].shape for k in py_key] tensor_numels = [py_dict[k].numel() for k in py_key] flatten_tensor = torch.cat([py_dict[k].flatten() for k in py_key]) dist.all_reduce(flatten_tensor, op=_get_reduce_op(op)) if op == "mean": flatten_tensor /= world_size split_tensors = [ x.reshape(shape) for x, shape in zip(torch.split(flatten_tensor, tensor_numels), tensor_shapes) ] return OrderedDict({k: v for k, v in zip(py_key, split_tensors)}) def all_reduce_norm(module): """ All reduce norm statistics in different devices. """ states = get_async_norm_states(module) states = all_reduce(states, op="mean") module.load_state_dict(states, strict=False) ================================================ FILE: asone/detectors/yolox/yolox/utils/boxes.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import numpy as np import torch import torchvision __all__ = [ "filter_box", "postprocess", "bboxes_iou", "matrix_iou", "adjust_box_anns", "xyxy2xywh", "xyxy2cxcywh", ] def filter_box(output, scale_range): """ output: (N, 5+class) shape """ min_scale, max_scale = scale_range w = output[:, 2] - output[:, 0] h = output[:, 3] - output[:, 1] keep = (w * h > min_scale * min_scale) & (w * h < max_scale * max_scale) return output[keep] def postprocess(prediction, num_classes, conf_thre=0.7, nms_thre=0.45, class_agnostic=False): box_corner = prediction.new(prediction.shape) box_corner[:, :, 0] = prediction[:, :, 0] - prediction[:, :, 2] / 2 box_corner[:, :, 1] = prediction[:, :, 1] - prediction[:, :, 3] / 2 box_corner[:, :, 2] = prediction[:, :, 0] + prediction[:, :, 2] / 2 box_corner[:, :, 3] = prediction[:, :, 1] + prediction[:, :, 3] / 2 prediction[:, :, :4] = box_corner[:, :, :4] output = [None for _ in range(len(prediction))] for i, image_pred in enumerate(prediction): # If none are remaining => process next image if not image_pred.size(0): continue # Get score and class with highest confidence class_conf, class_pred = torch.max(image_pred[:, 5: 5 + num_classes], 1, keepdim=True) conf_mask = (image_pred[:, 4] * class_conf.squeeze() >= conf_thre).squeeze() # Detections ordered as (x1, y1, x2, y2, obj_conf, class_conf, class_pred) detections = torch.cat((image_pred[:, :5], class_conf, class_pred.float()), 1) detections = detections[conf_mask] # print(detections.shape) # exit() if not detections.size(0): continue if class_agnostic: nms_out_index = torchvision.ops.nms( detections[:, :4], detections[:, 4] * detections[:, 5], nms_thre, ) else: nms_out_index = torchvision.ops.batched_nms( detections[:, :4], detections[:, 4] * detections[:, 5], detections[:, 6], nms_thre, ) detections = detections[nms_out_index] if output[i] is None: output[i] = detections else: output[i] = torch.cat((output[i], detections)) return output def bboxes_iou(bboxes_a, bboxes_b, xyxy=True): if bboxes_a.shape[1] != 4 or bboxes_b.shape[1] != 4: raise IndexError if xyxy: tl = torch.max(bboxes_a[:, None, :2], bboxes_b[:, :2]) br = torch.min(bboxes_a[:, None, 2:], bboxes_b[:, 2:]) area_a = torch.prod(bboxes_a[:, 2:] - bboxes_a[:, :2], 1) area_b = torch.prod(bboxes_b[:, 2:] - bboxes_b[:, :2], 1) else: tl = torch.max( (bboxes_a[:, None, :2] - bboxes_a[:, None, 2:] / 2), (bboxes_b[:, :2] - bboxes_b[:, 2:] / 2), ) br = torch.min( (bboxes_a[:, None, :2] + bboxes_a[:, None, 2:] / 2), (bboxes_b[:, :2] + bboxes_b[:, 2:] / 2), ) area_a = torch.prod(bboxes_a[:, 2:], 1) area_b = torch.prod(bboxes_b[:, 2:], 1) en = (tl < br).type(tl.type()).prod(dim=2) area_i = torch.prod(br - tl, 2) * en # * ((tl < br).all()) return area_i / (area_a[:, None] + area_b - area_i) def matrix_iou(a, b): """ return iou of a and b, numpy version for data augenmentation """ lt = np.maximum(a[:, np.newaxis, :2], b[:, :2]) rb = np.minimum(a[:, np.newaxis, 2:], b[:, 2:]) area_i = np.prod(rb - lt, axis=2) * (lt < rb).all(axis=2) area_a = np.prod(a[:, 2:] - a[:, :2], axis=1) area_b = np.prod(b[:, 2:] - b[:, :2], axis=1) return area_i / (area_a[:, np.newaxis] + area_b - area_i + 1e-12) def adjust_box_anns(bbox, scale_ratio, padw, padh, w_max, h_max): bbox[:, 0::2] = np.clip(bbox[:, 0::2] * scale_ratio + padw, 0, w_max) bbox[:, 1::2] = np.clip(bbox[:, 1::2] * scale_ratio + padh, 0, h_max) return bbox def xyxy2xywh(bboxes): bboxes[:, 2] = bboxes[:, 2] - bboxes[:, 0] bboxes[:, 3] = bboxes[:, 3] - bboxes[:, 1] return bboxes def xyxy2cxcywh(bboxes): bboxes[:, 2] = bboxes[:, 2] - bboxes[:, 0] bboxes[:, 3] = bboxes[:, 3] - bboxes[:, 1] bboxes[:, 0] = bboxes[:, 0] + bboxes[:, 2] * 0.5 bboxes[:, 1] = bboxes[:, 1] + bboxes[:, 3] * 0.5 return bboxes ================================================ FILE: asone/detectors/yolox/yolox/utils/checkpoint.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import os import shutil from loguru import logger import torch def load_ckpt(model, ckpt): model_state_dict = model.state_dict() load_dict = {} for key_model, v in model_state_dict.items(): if key_model not in ckpt: logger.warning( "{} is not in the ckpt. Please double check and see if this is desired.".format( key_model ) ) continue v_ckpt = ckpt[key_model] if v.shape != v_ckpt.shape: logger.warning( "Shape of {} in checkpoint is {}, while shape of {} in model is {}.".format( key_model, v_ckpt.shape, key_model, v.shape ) ) continue load_dict[key_model] = v_ckpt model.load_state_dict(load_dict, strict=False) return model def save_checkpoint(state, is_best, save_dir, model_name=""): if not os.path.exists(save_dir): os.makedirs(save_dir) filename = os.path.join(save_dir, model_name + "_ckpt.pth") torch.save(state, filename) if is_best: best_filename = os.path.join(save_dir, "best_ckpt.pth") shutil.copyfile(filename, best_filename) ================================================ FILE: asone/detectors/yolox/yolox/utils/compat.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- import torch _TORCH_VER = [int(x) for x in torch.__version__.split(".")[:2]] __all__ = ["meshgrid"] def meshgrid(*tensors): if _TORCH_VER >= [1, 10]: return torch.meshgrid(*tensors, indexing="ij") else: return torch.meshgrid(*tensors) ================================================ FILE: asone/detectors/yolox/yolox/utils/demo_utils.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import os import numpy as np __all__ = ["mkdir", "nms", "multiclass_nms", "demo_postprocess"] def mkdir(path): if not os.path.exists(path): os.makedirs(path) def nms(boxes, scores, nms_thr): """Single class NMS implemented in Numpy.""" x1 = boxes[:, 0] y1 = boxes[:, 1] x2 = boxes[:, 2] y2 = boxes[:, 3] areas = (x2 - x1 + 1) * (y2 - y1 + 1) order = scores.argsort()[::-1] keep = [] while order.size > 0: i = order[0] keep.append(i) xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]]) w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) inter = w * h ovr = inter / (areas[i] + areas[order[1:]] - inter) inds = np.where(ovr <= nms_thr)[0] order = order[inds + 1] return keep def multiclass_nms(boxes, scores, nms_thr, score_thr, class_agnostic=True): """Multiclass NMS implemented in Numpy""" if class_agnostic: nms_method = multiclass_nms_class_agnostic else: nms_method = multiclass_nms_class_aware return nms_method(boxes, scores, nms_thr, score_thr) def multiclass_nms_class_aware(boxes, scores, nms_thr, score_thr): """Multiclass NMS implemented in Numpy. Class-aware version.""" final_dets = [] num_classes = scores.shape[1] for cls_ind in range(num_classes): cls_scores = scores[:, cls_ind] valid_score_mask = cls_scores > score_thr if valid_score_mask.sum() == 0: continue else: valid_scores = cls_scores[valid_score_mask] valid_boxes = boxes[valid_score_mask] keep = nms(valid_boxes, valid_scores, nms_thr) if len(keep) > 0: cls_inds = np.ones((len(keep), 1)) * cls_ind dets = np.concatenate( [valid_boxes[keep], valid_scores[keep, None], cls_inds], 1 ) final_dets.append(dets) if len(final_dets) == 0: return None return np.concatenate(final_dets, 0) def multiclass_nms_class_agnostic(boxes, scores, nms_thr, score_thr): """Multiclass NMS implemented in Numpy. Class-agnostic version.""" cls_inds = scores.argmax(1) cls_scores = scores[np.arange(len(cls_inds)), cls_inds] valid_score_mask = cls_scores > score_thr if valid_score_mask.sum() == 0: return None valid_scores = cls_scores[valid_score_mask] valid_boxes = boxes[valid_score_mask] valid_cls_inds = cls_inds[valid_score_mask] keep = nms(valid_boxes, valid_scores, nms_thr) if keep: dets = np.concatenate( [valid_boxes[keep], valid_scores[keep, None], valid_cls_inds[keep, None]], 1 ) return dets def demo_postprocess(outputs, img_size, p6=False): grids = [] expanded_strides = [] if not p6: strides = [8, 16, 32] else: strides = [8, 16, 32, 64] hsizes = [img_size[0] // stride for stride in strides] wsizes = [img_size[1] // stride for stride in strides] for hsize, wsize, stride in zip(hsizes, wsizes, strides): xv, yv = np.meshgrid(np.arange(wsize), np.arange(hsize)) grid = np.stack((xv, yv), 2).reshape(1, -1, 2) grids.append(grid) shape = grid.shape[:2] expanded_strides.append(np.full((*shape, 1), stride)) grids = np.concatenate(grids, 1) expanded_strides = np.concatenate(expanded_strides, 1) outputs[..., :2] = (outputs[..., :2] + grids) * expanded_strides outputs[..., 2:4] = np.exp(outputs[..., 2:4]) * expanded_strides return outputs ================================================ FILE: asone/detectors/yolox/yolox/utils/dist.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # This file mainly comes from # https://github.com/facebookresearch/detectron2/blob/master/detectron2/utils/comm.py # Copyright (c) Facebook, Inc. and its affiliates. # Copyright (c) Megvii Inc. All rights reserved. """ This file contains primitives for multi-gpu communication. This is useful when doing distributed training. """ import functools import os import pickle import time from contextlib import contextmanager from loguru import logger import numpy as np import torch from torch import distributed as dist __all__ = [ "get_num_devices", "wait_for_the_master", "is_main_process", "synchronize", "get_world_size", "get_rank", "get_local_rank", "get_local_size", "time_synchronized", "gather", "all_gather", ] _LOCAL_PROCESS_GROUP = None def get_num_devices(): gpu_list = os.getenv('CUDA_VISIBLE_DEVICES', None) if gpu_list is not None: return len(gpu_list.split(',')) else: devices_list_info = os.popen("nvidia-smi -L") devices_list_info = devices_list_info.read().strip().split("\n") return len(devices_list_info) @contextmanager def wait_for_the_master(local_rank: int = None): """ Make all processes waiting for the master to do some task. Args: local_rank (int): the rank of the current process. Default to None. If None, it will use the rank of the current process. """ if local_rank is None: local_rank = get_local_rank() if local_rank > 0: dist.barrier() yield if local_rank == 0: if not dist.is_available(): return if not dist.is_initialized(): return else: dist.barrier() def synchronize(): """ Helper function to synchronize (barrier) among all processes when using distributed training """ if not dist.is_available(): return if not dist.is_initialized(): return world_size = dist.get_world_size() if world_size == 1: return dist.barrier() def get_world_size() -> int: if not dist.is_available(): return 1 if not dist.is_initialized(): return 1 return dist.get_world_size() def get_rank() -> int: if not dist.is_available(): return 0 if not dist.is_initialized(): return 0 return dist.get_rank() def get_local_rank() -> int: """ Returns: The rank of the current process within the local (per-machine) process group. """ if _LOCAL_PROCESS_GROUP is None: return get_rank() if not dist.is_available(): return 0 if not dist.is_initialized(): return 0 return dist.get_rank(group=_LOCAL_PROCESS_GROUP) def get_local_size() -> int: """ Returns: The size of the per-machine process group, i.e. the number of processes per machine. """ if not dist.is_available(): return 1 if not dist.is_initialized(): return 1 return dist.get_world_size(group=_LOCAL_PROCESS_GROUP) def is_main_process() -> bool: return get_rank() == 0 @functools.lru_cache() def _get_global_gloo_group(): """ Return a process group based on gloo backend, containing all the ranks The result is cached. """ if dist.get_backend() == "nccl": return dist.new_group(backend="gloo") else: return dist.group.WORLD def _serialize_to_tensor(data, group): backend = dist.get_backend(group) assert backend in ["gloo", "nccl"] device = torch.device("cpu" if backend == "gloo" else "cuda") buffer = pickle.dumps(data) if len(buffer) > 1024 ** 3: logger.warning( "Rank {} trying to all-gather {:.2f} GB of data on device {}".format( get_rank(), len(buffer) / (1024 ** 3), device ) ) storage = torch.ByteStorage.from_buffer(buffer) tensor = torch.ByteTensor(storage).to(device=device) return tensor def _pad_to_largest_tensor(tensor, group): """ Returns: list[int]: size of the tensor, on each rank Tensor: padded tensor that has the max size """ world_size = dist.get_world_size(group=group) assert ( world_size >= 1 ), "comm.gather/all_gather must be called from ranks within the given group!" local_size = torch.tensor([tensor.numel()], dtype=torch.int64, device=tensor.device) size_list = [ torch.zeros([1], dtype=torch.int64, device=tensor.device) for _ in range(world_size) ] dist.all_gather(size_list, local_size, group=group) size_list = [int(size.item()) for size in size_list] max_size = max(size_list) # we pad the tensor because torch all_gather does not support # gathering tensors of different shapes if local_size != max_size: padding = torch.zeros( (max_size - local_size,), dtype=torch.uint8, device=tensor.device ) tensor = torch.cat((tensor, padding), dim=0) return size_list, tensor def all_gather(data, group=None): """ Run all_gather on arbitrary picklable data (not necessarily tensors). Args: data: any picklable object group: a torch process group. By default, will use a group which contains all ranks on gloo backend. Returns: list[data]: list of data gathered from each rank """ if get_world_size() == 1: return [data] if group is None: group = _get_global_gloo_group() if dist.get_world_size(group) == 1: return [data] tensor = _serialize_to_tensor(data, group) size_list, tensor = _pad_to_largest_tensor(tensor, group) max_size = max(size_list) # receiving Tensor from all ranks tensor_list = [ torch.empty((max_size,), dtype=torch.uint8, device=tensor.device) for _ in size_list ] dist.all_gather(tensor_list, tensor, group=group) data_list = [] for size, tensor in zip(size_list, tensor_list): buffer = tensor.cpu().numpy().tobytes()[:size] data_list.append(pickle.loads(buffer)) return data_list def gather(data, dst=0, group=None): """ Run gather on arbitrary picklable data (not necessarily tensors). Args: data: any picklable object dst (int): destination rank group: a torch process group. By default, will use a group which contains all ranks on gloo backend. Returns: list[data]: on dst, a list of data gathered from each rank. Otherwise, an empty list. """ if get_world_size() == 1: return [data] if group is None: group = _get_global_gloo_group() if dist.get_world_size(group=group) == 1: return [data] rank = dist.get_rank(group=group) tensor = _serialize_to_tensor(data, group) size_list, tensor = _pad_to_largest_tensor(tensor, group) # receiving Tensor from all ranks if rank == dst: max_size = max(size_list) tensor_list = [ torch.empty((max_size,), dtype=torch.uint8, device=tensor.device) for _ in size_list ] dist.gather(tensor, tensor_list, dst=dst, group=group) data_list = [] for size, tensor in zip(size_list, tensor_list): buffer = tensor.cpu().numpy().tobytes()[:size] data_list.append(pickle.loads(buffer)) return data_list else: dist.gather(tensor, [], dst=dst, group=group) return [] def shared_random_seed(): """ Returns: int: a random number that is the same across all workers. If workers need a shared RNG, they can use this shared seed to create one. All workers must call this function, otherwise it will deadlock. """ ints = np.random.randint(2 ** 31) all_ints = all_gather(ints) return all_ints[0] def time_synchronized(): """pytorch-accurate time""" if torch.cuda.is_available(): torch.cuda.synchronize() return time.time() ================================================ FILE: asone/detectors/yolox/yolox/utils/ema.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import math from copy import deepcopy import torch import torch.nn as nn __all__ = ["ModelEMA", "is_parallel"] def is_parallel(model): """check if model is in parallel mode.""" parallel_type = ( nn.parallel.DataParallel, nn.parallel.DistributedDataParallel, ) return isinstance(model, parallel_type) class ModelEMA: """ Model Exponential Moving Average from https://github.com/rwightman/pytorch-image-models Keep a moving average of everything in the model state_dict (parameters and buffers). This is intended to allow functionality like https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage A smoothed version of the weights is necessary for some training schemes to perform well. This class is sensitive where it is initialized in the sequence of model init, GPU assignment and distributed training wrappers. """ def __init__(self, model, decay=0.9999, updates=0): """ Args: model (nn.Module): model to apply EMA. decay (float): ema decay reate. updates (int): counter of EMA updates. """ # Create EMA(FP32) self.ema = deepcopy(model.module if is_parallel(model) else model).eval() self.updates = updates # decay exponential ramp (to help early epochs) self.decay = lambda x: decay * (1 - math.exp(-x / 2000)) for p in self.ema.parameters(): p.requires_grad_(False) def update(self, model): # Update EMA parameters with torch.no_grad(): self.updates += 1 d = self.decay(self.updates) msd = ( model.module.state_dict() if is_parallel(model) else model.state_dict() ) # model state_dict for k, v in self.ema.state_dict().items(): if v.dtype.is_floating_point: v *= d v += (1.0 - d) * msd[k].detach() ================================================ FILE: asone/detectors/yolox/yolox/utils/logger.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import inspect import os import sys from collections import defaultdict from loguru import logger import cv2 import numpy as np import torch def get_caller_name(depth=0): """ Args: depth (int): Depth of caller conext, use 0 for caller depth. Default value: 0. Returns: str: module name of the caller """ # the following logic is a little bit faster than inspect.stack() logic frame = inspect.currentframe().f_back for _ in range(depth): frame = frame.f_back return frame.f_globals["__name__"] class StreamToLoguru: """ stream object that redirects writes to a logger instance. """ def __init__(self, level="INFO", caller_names=("apex", "pycocotools")): """ Args: level(str): log level string of loguru. Default value: "INFO". caller_names(tuple): caller names of redirected module. Default value: (apex, pycocotools). """ self.level = level self.linebuf = "" self.caller_names = caller_names def write(self, buf): full_name = get_caller_name(depth=1) module_name = full_name.rsplit(".", maxsplit=-1)[0] if module_name in self.caller_names: for line in buf.rstrip().splitlines(): # use caller level log logger.opt(depth=2).log(self.level, line.rstrip()) else: sys.__stdout__.write(buf) def flush(self): pass def isatty(self): # when using colab, jax is installed by default and issue like # https://github.com/Megvii-BaseDetection/YOLOX/issues/1437 might be raised # due to missing attribute like`isatty`. # For more details, checked the following link: # https://github.com/google/jax/blob/10720258ea7fb5bde997dfa2f3f71135ab7a6733/jax/_src/pretty_printer.py#L54 # noqa return True def redirect_sys_output(log_level="INFO"): redirect_logger = StreamToLoguru(log_level) sys.stderr = redirect_logger sys.stdout = redirect_logger def setup_logger(save_dir, distributed_rank=0, filename="log.txt", mode="a"): """setup logger for training and testing. Args: save_dir(str): location to save log file distributed_rank(int): device rank when multi-gpu environment filename (string): log save name. mode(str): log file write mode, `append` or `override`. default is `a`. Return: logger instance. """ loguru_format = ( "{time:YYYY-MM-DD HH:mm:ss} | " "{level: <8} | " "{name}:{line} - {message}" ) logger.remove() save_file = os.path.join(save_dir, filename) if mode == "o" and os.path.exists(save_file): os.remove(save_file) # only keep logger in rank0 process if distributed_rank == 0: logger.add( sys.stderr, format=loguru_format, level="INFO", enqueue=True, ) logger.add(save_file) # redirect stdout/stderr to loguru redirect_sys_output("INFO") class WandbLogger(object): """ Log training runs, datasets, models, and predictions to Weights & Biases. This logger sends information to W&B at wandb.ai. By default, this information includes hyperparameters, system configuration and metrics, model metrics, and basic data metrics and analyses. For more information, please refer to: https://docs.wandb.ai/guides/track https://docs.wandb.ai/guides/integrations/other/yolox """ def __init__(self, project=None, name=None, id=None, entity=None, save_dir=None, config=None, val_dataset=None, num_eval_images=100, log_checkpoints=False, **kwargs): """ Args: project (str): wandb project name. name (str): wandb run name. id (str): wandb run id. entity (str): wandb entity name. save_dir (str): save directory. config (dict): config dict. val_dataset (Dataset): validation dataset. num_eval_images (int): number of images from the validation set to log. log_checkpoints (bool): log checkpoints **kwargs: other kwargs. Usage: Any arguments for wandb.init can be provided on the command line using the prefix `wandb-`. Example ``` python tools/train.py .... --logger wandb wandb-project \ wandb-name \ wandb-id \ wandb-save_dir \ wandb-num_eval_imges \ wandb-log_checkpoints ``` The val_dataset argument is not open to the command line. """ try: import wandb self.wandb = wandb except ModuleNotFoundError: raise ModuleNotFoundError( "wandb is not installed." "Please install wandb using pip install wandb" ) self.project = project self.name = name self.id = id self.save_dir = save_dir self.config = config self.kwargs = kwargs self.entity = entity self._run = None self.val_artifact = None if num_eval_images == -1: self.num_log_images = len(val_dataset) else: self.num_log_images = min(num_eval_images, len(val_dataset)) self.log_checkpoints = (log_checkpoints == "True" or log_checkpoints == "true") self._wandb_init = dict( project=self.project, name=self.name, id=self.id, entity=self.entity, dir=self.save_dir, resume="allow" ) self._wandb_init.update(**kwargs) _ = self.run if self.config: self.run.config.update(self.config) self.run.define_metric("train/epoch") self.run.define_metric("val/*", step_metric="train/epoch") self.run.define_metric("train/step") self.run.define_metric("train/*", step_metric="train/step") if val_dataset and self.num_log_images != 0: self.cats = val_dataset.cats self.id_to_class = { cls['id']: cls['name'] for cls in self.cats } self._log_validation_set(val_dataset) @property def run(self): if self._run is None: if self.wandb.run is not None: logger.info( "There is a wandb run already in progress " "and newly created instances of `WandbLogger` will reuse" " this run. If this is not desired, call `wandb.finish()`" "before instantiating `WandbLogger`." ) self._run = self.wandb.run else: self._run = self.wandb.init(**self._wandb_init) return self._run def _log_validation_set(self, val_dataset): """ Log validation set to wandb. Args: val_dataset (Dataset): validation dataset. """ if self.val_artifact is None: self.val_artifact = self.wandb.Artifact(name="validation_images", type="dataset") self.val_table = self.wandb.Table(columns=["id", "input"]) for i in range(self.num_log_images): data_point = val_dataset[i] img = data_point[0] id = data_point[3] img = np.transpose(img, (1, 2, 0)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) self.val_table.add_data( id.item(), self.wandb.Image(img) ) self.val_artifact.add(self.val_table, "validation_images_table") self.run.use_artifact(self.val_artifact) self.val_artifact.wait() def log_metrics(self, metrics, step=None): """ Args: metrics (dict): metrics dict. step (int): step number. """ for k, v in metrics.items(): if isinstance(v, torch.Tensor): metrics[k] = v.item() if step is not None: metrics.update({"train/step": step}) self.run.log(metrics) else: self.run.log(metrics) def log_images(self, predictions): if len(predictions) == 0 or self.val_artifact is None or self.num_log_images == 0: return table_ref = self.val_artifact.get("validation_images_table") columns = ["id", "predicted"] for cls in self.cats: columns.append(cls["name"]) result_table = self.wandb.Table(columns=columns) for idx, val in table_ref.iterrows(): avg_scores = defaultdict(int) num_occurrences = defaultdict(int) if val[0] in predictions: prediction = predictions[val[0]] boxes = [] for i in range(len(prediction["bboxes"])): bbox = prediction["bboxes"][i] x0 = bbox[0] y0 = bbox[1] x1 = bbox[2] y1 = bbox[3] box = { "position": { "minX": min(x0, x1), "minY": min(y0, y1), "maxX": max(x0, x1), "maxY": max(y0, y1) }, "class_id": prediction["categories"][i], "domain": "pixel" } avg_scores[ self.id_to_class[prediction["categories"][i]] ] += prediction["scores"][i] num_occurrences[self.id_to_class[prediction["categories"][i]]] += 1 boxes.append(box) else: boxes = [] average_class_score = [] for cls in self.cats: if cls["name"] not in num_occurrences: score = 0 else: score = avg_scores[cls["name"]] / num_occurrences[cls["name"]] average_class_score.append(score) result_table.add_data( idx, self.wandb.Image(val[1], boxes={ "prediction": { "box_data": boxes, "class_labels": self.id_to_class } } ), *average_class_score ) self.wandb.log({"val_results/result_table": result_table}) def save_checkpoint(self, save_dir, model_name, is_best, metadata=None): """ Args: save_dir (str): save directory. model_name (str): model name. is_best (bool): whether the model is the best model. metadata (dict): metadata to save corresponding to the checkpoint. """ if not self.log_checkpoints: return if "epoch" in metadata: epoch = metadata["epoch"] else: epoch = None filename = os.path.join(save_dir, model_name + "_ckpt.pth") artifact = self.wandb.Artifact( name=f"run_{self.run.id}_model", type="model", metadata=metadata ) artifact.add_file(filename, name="model_ckpt.pth") aliases = ["latest"] if is_best: aliases.append("best") if epoch: aliases.append(f"epoch-{epoch}") self.run.log_artifact(artifact, aliases=aliases) def finish(self): self.run.finish() @classmethod def initialize_wandb_logger(cls, args, exp, val_dataset): wandb_params = dict() prefix = "wandb-" for k, v in zip(args.opts[0::2], args.opts[1::2]): if k.startswith("wandb-"): try: wandb_params.update({k[len(prefix):]: int(v)}) except ValueError: wandb_params.update({k[len(prefix):]: v}) return cls(config=vars(exp), val_dataset=val_dataset, **wandb_params) ================================================ FILE: asone/detectors/yolox/yolox/utils/lr_scheduler.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import math from functools import partial class LRScheduler: def __init__(self, name, lr, iters_per_epoch, total_epochs, **kwargs): """ Supported lr schedulers: [cos, warmcos, multistep] Args: lr (float): learning rate. iters_per_peoch (int): number of iterations in one epoch. total_epochs (int): number of epochs in training. kwargs (dict): - cos: None - warmcos: [warmup_epochs, warmup_lr_start (default 1e-6)] - multistep: [milestones (epochs), gamma (default 0.1)] """ self.lr = lr self.iters_per_epoch = iters_per_epoch self.total_epochs = total_epochs self.total_iters = iters_per_epoch * total_epochs self.__dict__.update(kwargs) self.lr_func = self._get_lr_func(name) def update_lr(self, iters): return self.lr_func(iters) def _get_lr_func(self, name): if name == "cos": # cosine lr schedule lr_func = partial(cos_lr, self.lr, self.total_iters) elif name == "warmcos": warmup_total_iters = self.iters_per_epoch * self.warmup_epochs warmup_lr_start = getattr(self, "warmup_lr_start", 1e-6) lr_func = partial( warm_cos_lr, self.lr, self.total_iters, warmup_total_iters, warmup_lr_start, ) elif name == "yoloxwarmcos": warmup_total_iters = self.iters_per_epoch * self.warmup_epochs no_aug_iters = self.iters_per_epoch * self.no_aug_epochs warmup_lr_start = getattr(self, "warmup_lr_start", 0) min_lr_ratio = getattr(self, "min_lr_ratio", 0.2) lr_func = partial( yolox_warm_cos_lr, self.lr, min_lr_ratio, self.total_iters, warmup_total_iters, warmup_lr_start, no_aug_iters, ) elif name == "yoloxsemiwarmcos": warmup_lr_start = getattr(self, "warmup_lr_start", 0) min_lr_ratio = getattr(self, "min_lr_ratio", 0.2) warmup_total_iters = self.iters_per_epoch * self.warmup_epochs no_aug_iters = self.iters_per_epoch * self.no_aug_epochs normal_iters = self.iters_per_epoch * self.semi_epoch semi_iters = self.iters_per_epoch_semi * ( self.total_epochs - self.semi_epoch - self.no_aug_epochs ) lr_func = partial( yolox_semi_warm_cos_lr, self.lr, min_lr_ratio, warmup_lr_start, self.total_iters, normal_iters, no_aug_iters, warmup_total_iters, semi_iters, self.iters_per_epoch, self.iters_per_epoch_semi, ) elif name == "multistep": # stepwise lr schedule milestones = [ int(self.total_iters * milestone / self.total_epochs) for milestone in self.milestones ] gamma = getattr(self, "gamma", 0.1) lr_func = partial(multistep_lr, self.lr, milestones, gamma) else: raise ValueError("Scheduler version {} not supported.".format(name)) return lr_func def cos_lr(lr, total_iters, iters): """Cosine learning rate""" lr *= 0.5 * (1.0 + math.cos(math.pi * iters / total_iters)) return lr def warm_cos_lr(lr, total_iters, warmup_total_iters, warmup_lr_start, iters): """Cosine learning rate with warm up.""" if iters <= warmup_total_iters: lr = (lr - warmup_lr_start) * iters / float( warmup_total_iters ) + warmup_lr_start else: lr *= 0.5 * ( 1.0 + math.cos( math.pi * (iters - warmup_total_iters) / (total_iters - warmup_total_iters) ) ) return lr def yolox_warm_cos_lr( lr, min_lr_ratio, total_iters, warmup_total_iters, warmup_lr_start, no_aug_iter, iters, ): """Cosine learning rate with warm up.""" min_lr = lr * min_lr_ratio if iters <= warmup_total_iters: # lr = (lr - warmup_lr_start) * iters / float(warmup_total_iters) + warmup_lr_start lr = (lr - warmup_lr_start) * pow( iters / float(warmup_total_iters), 2 ) + warmup_lr_start elif iters >= total_iters - no_aug_iter: lr = min_lr else: lr = min_lr + 0.5 * (lr - min_lr) * ( 1.0 + math.cos( math.pi * (iters - warmup_total_iters) / (total_iters - warmup_total_iters - no_aug_iter) ) ) return lr def yolox_semi_warm_cos_lr( lr, min_lr_ratio, warmup_lr_start, total_iters, normal_iters, no_aug_iters, warmup_total_iters, semi_iters, iters_per_epoch, iters_per_epoch_semi, iters, ): """Cosine learning rate with warm up.""" min_lr = lr * min_lr_ratio if iters <= warmup_total_iters: # lr = (lr - warmup_lr_start) * iters / float(warmup_total_iters) + warmup_lr_start lr = (lr - warmup_lr_start) * pow( iters / float(warmup_total_iters), 2 ) + warmup_lr_start elif iters >= normal_iters + semi_iters: lr = min_lr elif iters <= normal_iters: lr = min_lr + 0.5 * (lr - min_lr) * ( 1.0 + math.cos( math.pi * (iters - warmup_total_iters) / (total_iters - warmup_total_iters - no_aug_iters) ) ) else: lr = min_lr + 0.5 * (lr - min_lr) * ( 1.0 + math.cos( math.pi * ( normal_iters - warmup_total_iters + (iters - normal_iters) * iters_per_epoch * 1.0 / iters_per_epoch_semi ) / (total_iters - warmup_total_iters - no_aug_iters) ) ) return lr def multistep_lr(lr, milestones, gamma, iters): """MultiStep learning rate""" for milestone in milestones: lr *= gamma if iters >= milestone else 1.0 return lr ================================================ FILE: asone/detectors/yolox/yolox/utils/metric.py ================================================ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import functools import os import time from collections import defaultdict, deque import numpy as np import torch __all__ = [ "AverageMeter", "MeterBuffer", "get_total_and_free_memory_in_Mb", "occupy_mem", "gpu_mem_usage", ] def get_total_and_free_memory_in_Mb(cuda_device): devices_info_str = os.popen( "nvidia-smi --query-gpu=memory.total,memory.used --format=csv,nounits,noheader" ) devices_info = devices_info_str.read().strip().split("\n") if "CUDA_VISIBLE_DEVICES" in os.environ: visible_devices = os.environ["CUDA_VISIBLE_DEVICES"].split(',') cuda_device = int(visible_devices[cuda_device]) total, used = devices_info[int(cuda_device)].split(",") return int(total), int(used) def occupy_mem(cuda_device, mem_ratio=0.9): """ pre-allocate gpu memory for training to avoid memory Fragmentation. """ total, used = get_total_and_free_memory_in_Mb(cuda_device) max_mem = int(total * mem_ratio) block_mem = max_mem - used x = torch.cuda.FloatTensor(256, 1024, block_mem) del x time.sleep(5) def gpu_mem_usage(): """ Compute the GPU memory usage for the current device (MB). """ mem_usage_bytes = torch.cuda.max_memory_allocated() return mem_usage_bytes / (1024 * 1024) class AverageMeter: """Track a series of values and provide access to smoothed values over a window or the global series average. """ def __init__(self, window_size=50): self._deque = deque(maxlen=window_size) self._total = 0.0 self._count = 0 def update(self, value): self._deque.append(value) self._count += 1 self._total += value @property def median(self): d = np.array(list(self._deque)) return np.median(d) @property def avg(self): # if deque is empty, nan will be returned. d = np.array(list(self._deque)) return d.mean() @property def global_avg(self): return self._total / max(self._count, 1e-5) @property def latest(self): return self._deque[-1] if len(self._deque) > 0 else None @property def total(self): return self._total def reset(self): self._deque.clear() self._total = 0.0 self._count = 0 def clear(self): self._deque.clear() class MeterBuffer(defaultdict): """Computes and stores the average and current value""" def __init__(self, window_size=20): factory = functools.partial(AverageMeter, window_size=window_size) super().__init__(factory) def reset(self): for v in self.values(): v.reset() def get_filtered_meter(self, filter_key="time"): return {k: v for k, v in self.items() if filter_key in k} def update(self, values=None, **kwargs): if values is None: values = {} values.update(kwargs) for k, v in values.items(): if isinstance(v, torch.Tensor): v = v.detach() self[k].update(v) def clear_meters(self): for v in self.values(): v.clear() ================================================ FILE: asone/detectors/yolox/yolox/utils/model_utils.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import contextlib from copy import deepcopy from typing import Sequence import torch import torch.nn as nn __all__ = [ "fuse_conv_and_bn", "fuse_model", "get_model_info", "replace_module", "freeze_module", "adjust_status", ] def get_model_info(model: nn.Module, tsize: Sequence[int]) -> str: from thop import profile stride = 64 img = torch.zeros((1, 3, stride, stride), device=next(model.parameters()).device) flops, params = profile(deepcopy(model), inputs=(img,), verbose=False) params /= 1e6 flops /= 1e9 flops *= tsize[0] * tsize[1] / stride / stride * 2 # Gflops info = "Params: {:.2f}M, Gflops: {:.2f}".format(params, flops) return info def fuse_conv_and_bn(conv: nn.Conv2d, bn: nn.BatchNorm2d) -> nn.Conv2d: """ Fuse convolution and batchnorm layers. check more info on https://tehnokv.com/posts/fusing-batchnorm-and-conv/ Args: conv (nn.Conv2d): convolution to fuse. bn (nn.BatchNorm2d): batchnorm to fuse. Returns: nn.Conv2d: fused convolution behaves the same as the input conv and bn. """ fusedconv = ( nn.Conv2d( conv.in_channels, conv.out_channels, kernel_size=conv.kernel_size, stride=conv.stride, padding=conv.padding, groups=conv.groups, bias=True, ) .requires_grad_(False) .to(conv.weight.device) ) # prepare filters w_conv = conv.weight.clone().view(conv.out_channels, -1) w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.shape)) # prepare spatial bias b_conv = ( torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias ) b_bn = bn.bias - bn.weight.mul(bn.running_mean).div( torch.sqrt(bn.running_var + bn.eps) ) fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) return fusedconv def fuse_model(model: nn.Module) -> nn.Module: """fuse conv and bn in model Args: model (nn.Module): model to fuse Returns: nn.Module: fused model """ from asone.detectors.yolox.yolox.models.network_blocks import BaseConv for m in model.modules(): if type(m) is BaseConv and hasattr(m, "bn"): m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv delattr(m, "bn") # remove batchnorm m.forward = m.fuseforward # update forward return model def replace_module(module, replaced_module_type, new_module_type, replace_func=None) -> nn.Module: """ Replace given type in module to a new type. mostly used in deploy. Args: module (nn.Module): model to apply replace operation. replaced_module_type (Type): module type to be replaced. new_module_type (Type) replace_func (function): python function to describe replace logic. Defalut value None. Returns: model (nn.Module): module that already been replaced. """ def default_replace_func(replaced_module_type, new_module_type): return new_module_type() if replace_func is None: replace_func = default_replace_func model = module if isinstance(module, replaced_module_type): model = replace_func(replaced_module_type, new_module_type) else: # recurrsively replace for name, child in module.named_children(): new_child = replace_module(child, replaced_module_type, new_module_type) if new_child is not child: # child is already replaced model.add_module(name, new_child) return model def freeze_module(module: nn.Module, name=None) -> nn.Module: """freeze module inplace Args: module (nn.Module): module to freeze. name (str, optional): name to freeze. If not given, freeze the whole module. Note that fuzzy match is not supported. Defaults to None. Examples: freeze the backbone of model >>> freeze_moudle(model.backbone) or freeze the backbone of model by name >>> freeze_moudle(model, name="backbone") """ for param_name, parameter in module.named_parameters(): if name is None or name in param_name: parameter.requires_grad = False # ensure module like BN and dropout are freezed for module_name, sub_module in module.named_modules(): # actually there are no needs to call eval for every single sub_module if name is None or name in module_name: sub_module.eval() return module @contextlib.contextmanager def adjust_status(module: nn.Module, training: bool = False) -> nn.Module: """Adjust module to training/eval mode temporarily. Args: module (nn.Module): module to adjust status. training (bool): training mode to set. True for train mode, False fro eval mode. Examples: >>> with adjust_status(model, training=False): ... model(data) """ status = {} def backup_status(module): for m in module.modules(): # save prev status to dict status[m] = m.training m.training = training def recover_status(module): for m in module.modules(): # recover prev status from dict m.training = status.pop(m) backup_status(module) yield module recover_status(module) ================================================ FILE: asone/detectors/yolox/yolox/utils/setup_env.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import os import subprocess from loguru import logger import cv2 from asone.detectors.yolox.yolox.utils.dist import get_world_size, is_main_process __all__ = ["configure_nccl", "configure_module", "configure_omp"] def configure_nccl(): """Configure multi-machine environment variables of NCCL.""" os.environ["NCCL_LAUNCH_MODE"] = "PARALLEL" os.environ["NCCL_IB_HCA"] = subprocess.getoutput( "pushd /sys/class/infiniband/ > /dev/null; for i in mlx5_*; " "do cat $i/ports/1/gid_attrs/types/* 2>/dev/null " "| grep v >/dev/null && echo $i ; done; popd > /dev/null" ) os.environ["NCCL_IB_GID_INDEX"] = "3" os.environ["NCCL_IB_TC"] = "106" def configure_omp(num_threads=1): """ If OMP_NUM_THREADS is not configured and world_size is greater than 1, Configure OMP_NUM_THREADS environment variables of NCCL to `num_thread`. Args: num_threads (int): value of `OMP_NUM_THREADS` to set. """ # We set OMP_NUM_THREADS=1 by default, which achieves the best speed on our machines # feel free to change it for better performance. if "OMP_NUM_THREADS" not in os.environ and get_world_size() > 1: os.environ["OMP_NUM_THREADS"] = str(num_threads) if is_main_process(): logger.info( "\n***************************************************************\n" "We set `OMP_NUM_THREADS` for each process to {} to speed up.\n" "please further tune the variable for optimal performance.\n" "***************************************************************".format( os.environ["OMP_NUM_THREADS"] ) ) def configure_module(ulimit_value=8192): """ Configure pytorch module environment. setting of ulimit and cv2 will be set. Args: ulimit_value(int): default open file number on linux. Default value: 8192. """ # system setting try: import resource rlimit = resource.getrlimit(resource.RLIMIT_NOFILE) resource.setrlimit(resource.RLIMIT_NOFILE, (ulimit_value, rlimit[1])) except Exception: # Exception might be raised in Windows OS or rlimit reaches max limit number. # However, set rlimit value might not be necessary. pass # cv2 # multiprocess might be harmful on performance of torch dataloader os.environ["OPENCV_OPENCL_RUNTIME"] = "disabled" try: cv2.setNumThreads(0) cv2.ocl.setUseOpenCL(False) except Exception: # cv2 version mismatch might rasie exceptions. pass ================================================ FILE: asone/detectors/yolox/yolox/utils/visualize.py ================================================ #!/usr/bin/env python3 # -*- coding:utf-8 -*- # Copyright (c) Megvii Inc. All rights reserved. import cv2 import numpy as np __all__ = ["vis"] def vis(img, boxes, scores, cls_ids, conf=0.5, class_names=None): for i in range(len(boxes)): box = boxes[i] cls_id = int(cls_ids[i]) score = scores[i] if score < conf: continue x0 = int(box[0]) y0 = int(box[1]) x1 = int(box[2]) y1 = int(box[3]) color = (_COLORS[cls_id] * 255).astype(np.uint8).tolist() text = '{}:{:.1f}%'.format(class_names[cls_id], score * 100) txt_color = (0, 0, 0) if np.mean(_COLORS[cls_id]) > 0.5 else (255, 255, 255) font = cv2.FONT_HERSHEY_SIMPLEX txt_size = cv2.getTextSize(text, font, 0.4, 1)[0] cv2.rectangle(img, (x0, y0), (x1, y1), color, 2) txt_bk_color = (_COLORS[cls_id] * 255 * 0.7).astype(np.uint8).tolist() cv2.rectangle( img, (x0, y0 + 1), (x0 + txt_size[0] + 1, y0 + int(1.5*txt_size[1])), txt_bk_color, -1 ) cv2.putText(img, text, (x0, y0 + txt_size[1]), font, 0.4, txt_color, thickness=1) return img _COLORS = np.array( [ 0.000, 0.447, 0.741, 0.850, 0.325, 0.098, 0.929, 0.694, 0.125, 0.494, 0.184, 0.556, 0.466, 0.674, 0.188, 0.301, 0.745, 0.933, 0.635, 0.078, 0.184, 0.300, 0.300, 0.300, 0.600, 0.600, 0.600, 1.000, 0.000, 0.000, 1.000, 0.500, 0.000, 0.749, 0.749, 0.000, 0.000, 1.000, 0.000, 0.000, 0.000, 1.000, 0.667, 0.000, 1.000, 0.333, 0.333, 0.000, 0.333, 0.667, 0.000, 0.333, 1.000, 0.000, 0.667, 0.333, 0.000, 0.667, 0.667, 0.000, 0.667, 1.000, 0.000, 1.000, 0.333, 0.000, 1.000, 0.667, 0.000, 1.000, 1.000, 0.000, 0.000, 0.333, 0.500, 0.000, 0.667, 0.500, 0.000, 1.000, 0.500, 0.333, 0.000, 0.500, 0.333, 0.333, 0.500, 0.333, 0.667, 0.500, 0.333, 1.000, 0.500, 0.667, 0.000, 0.500, 0.667, 0.333, 0.500, 0.667, 0.667, 0.500, 0.667, 1.000, 0.500, 1.000, 0.000, 0.500, 1.000, 0.333, 0.500, 1.000, 0.667, 0.500, 1.000, 1.000, 0.500, 0.000, 0.333, 1.000, 0.000, 0.667, 1.000, 0.000, 1.000, 1.000, 0.333, 0.000, 1.000, 0.333, 0.333, 1.000, 0.333, 0.667, 1.000, 0.333, 1.000, 1.000, 0.667, 0.000, 1.000, 0.667, 0.333, 1.000, 0.667, 0.667, 1.000, 0.667, 1.000, 1.000, 1.000, 0.000, 1.000, 1.000, 0.333, 1.000, 1.000, 0.667, 1.000, 0.333, 0.000, 0.000, 0.500, 0.000, 0.000, 0.667, 0.000, 0.000, 0.833, 0.000, 0.000, 1.000, 0.000, 0.000, 0.000, 0.167, 0.000, 0.000, 0.333, 0.000, 0.000, 0.500, 0.000, 0.000, 0.667, 0.000, 0.000, 0.833, 0.000, 0.000, 1.000, 0.000, 0.000, 0.000, 0.167, 0.000, 0.000, 0.333, 0.000, 0.000, 0.500, 0.000, 0.000, 0.667, 0.000, 0.000, 0.833, 0.000, 0.000, 1.000, 0.000, 0.000, 0.000, 0.143, 0.143, 0.143, 0.286, 0.286, 0.286, 0.429, 0.429, 0.429, 0.571, 0.571, 0.571, 0.714, 0.714, 0.714, 0.857, 0.857, 0.857, 0.000, 0.447, 0.741, 0.314, 0.717, 0.741, 0.50, 0.5, 0 ] ).astype(np.float32).reshape(-1, 3) ================================================ FILE: asone/detectors/yolox/yolox_detector.py ================================================ import os from asone.utils import get_names import numpy as np import warnings import torch import onnxruntime from asone import utils from asone.detectors.yolox.yolox.utils import fuse_model, postprocess from asone.detectors.yolox.yolox.exp import get_exp from asone.detectors.yolox.yolox_utils import preprocess, multiclass_nms, demo_postprocess from asone.utils.utils import PathResolver class YOLOxDetector: def __init__(self, model_name=None, exp_file=None, weights=None, use_onnx=False, use_cuda=False ): self.use_onnx = use_onnx self.device = 'cuda' if use_cuda else 'cpu' if not os.path.exists(weights): utils.download_weights(weights) self.weights_name = os.path.basename(weights) if model_name is None: model_name = 'yolox-s' if exp_file is None: exp_file = os.path.join("exps", "default", "yolox_s.py") with PathResolver(): # Load Model if self.use_onnx: self.model = self.load_onnx_model(use_cuda, weights) else: self.model = self.load_torch_model(weights, exp_file, model_name) def load_onnx_model(self, use_cuda, weights): # Load onnx if use_cuda: providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] else: providers = ['CPUExecutionProvider'] model = onnxruntime.InferenceSession(weights, providers=providers) return model def load_torch_model(self, weights, exp_file, model_name, fp16=True, fuse=False): # Device: CUDA and if fp16=True only then half precision floating point works self.fp16 = bool(fp16) & ( (not self.use_onnx or self.use_onnx) and self.device != 'cpu') exp = get_exp(exp_file, model_name) ckpt = torch.load(weights, map_location="cpu") # get number of classes from weights # head.cls_preds.0.weight weights contains number of classes so simply extract it and with in exp file. exp.num_classes = ckpt['model']['head.cls_preds.0.weight'].size()[0] self.classes = exp.num_classes model = exp.get_model() if self.device == "cuda": model.cuda() if self.fp16: # to FP16 model.half() model.eval() # load the model state dict model.load_state_dict(ckpt["model"]) if fuse: model = fuse_model(model) return model def detect(self, image: list, input_shape: tuple = (640, 640), conf_thres: float = 0.25, iou_thres: float = 0.45, max_det: int = 1000, filter_classes: bool = None, agnostic_nms: bool = True, with_p6: bool = False, return_image=False ) -> list: if self.weights_name in ['yolox_tiny.onnx', 'yolox_nano.onnx']: input_shape = (416, 416) self.input_shape = input_shape # Image Preprocess for onnx models if self.use_onnx: processed_image, ratio = preprocess(image, self.input_shape) else: processed_image, ratio = preprocess(image, self.input_shape) processed_image = torch.from_numpy(processed_image).unsqueeze(0) processed_image = processed_image.float() if self.device == "cuda": processed_image = processed_image.cuda() if self.fp16: processed_image = processed_image.half() detection = [] # Inference if self.use_onnx: # Run ONNX model # Model Input and Output model_inputs = {self.model.get_inputs( )[0].name: processed_image[None, :, :, :]} detection = self.model.run(None, model_inputs)[0] # Postprrocessing detection = demo_postprocess( detection, self.input_shape, p6=with_p6)[0] boxes = detection[:, :4] scores = detection[:, 4:5] * detection[:, 5:] boxes_xyxy = np.ones_like(boxes) boxes_xyxy[:, 0] = boxes[:, 0] - boxes[:, 2]/2. boxes_xyxy[:, 1] = boxes[:, 1] - boxes[:, 3]/2. boxes_xyxy[:, 2] = boxes[:, 0] + boxes[:, 2]/2. boxes_xyxy[:, 3] = boxes[:, 1] + boxes[:, 3]/2. boxes_xyxy /= ratio detection = multiclass_nms( boxes_xyxy, scores, nms_thr=iou_thres, score_thr=conf_thres) # Run Pytorch model else: with torch.no_grad(): prediction = self.model(processed_image) prediction = postprocess(prediction, self.classes, conf_thres, iou_thres, class_agnostic=agnostic_nms )[0] if prediction is not None: prediction = prediction.detach().cpu().numpy() bboxes = prediction[:, 0:4] # Postprocessing bboxes /= ratio cls = prediction[:, 6] scores = prediction[:, 4] * prediction[:, 5] for box in range(len(bboxes)): pred = np.append(bboxes[box], scores[box]) pred = np.append(pred, cls[box]) detection.append(pred) detection = np.array(detection) else: detection = prediction if filter_classes: class_names = get_names() filter_class_idx = [] if filter_classes: for _class in filter_classes: if _class.lower() in class_names: filter_class_idx.append( class_names.index(_class.lower())) else: warnings.warn( f"class {_class} not found in model classes list.") detection = detection[np.in1d( detection[:, 5].astype(int), filter_class_idx)] image_info = { 'width': image.shape[1], 'height': image.shape[0], } if return_image: return detection, image else: return detection, image_info ================================================ FILE: asone/detectors/yolox/yolox_utils.py ================================================ import cv2 import numpy as np def preprocess(img, input_size, swap=(2, 0, 1)): if len(img.shape) == 3: padded_img = np.ones((input_size[0], input_size[1], 3), dtype=np.uint8) * 114 else: padded_img = np.ones(input_size, dtype=np.uint8) * 114 r = min(input_size[0] / img.shape[0], input_size[1] / img.shape[1]) resized_img = cv2.resize( img, (int(img.shape[1] * r), int(img.shape[0] * r)), interpolation=cv2.INTER_LINEAR, ).astype(np.uint8) padded_img[: int(img.shape[0] * r), : int(img.shape[1] * r)] = resized_img padded_img = padded_img.transpose(swap) padded_img = np.ascontiguousarray(padded_img, dtype=np.float32) return padded_img, r def nms(boxes, scores, nms_thr): """Single class NMS implemented in Numpy.""" x1 = boxes[:, 0] y1 = boxes[:, 1] x2 = boxes[:, 2] y2 = boxes[:, 3] areas = (x2 - x1 + 1) * (y2 - y1 + 1) order = scores.argsort()[::-1] keep = [] while order.size > 0: i = order[0] keep.append(i) xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]]) w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) inter = w * h ovr = inter / (areas[i] + areas[order[1:]] - inter) inds = np.where(ovr <= nms_thr)[0] order = order[inds + 1] return keep def multiclass_nms(boxes, scores, nms_thr, score_thr, class_agnostic=True): """Multiclass NMS implemented in Numpy""" if class_agnostic: nms_method = multiclass_nms_class_agnostic else: nms_method = multiclass_nms_class_aware return nms_method(boxes, scores, nms_thr, score_thr) def multiclass_nms_class_aware(boxes, scores, nms_thr, score_thr): """Multiclass NMS implemented in Numpy. Class-aware version.""" final_dets = [] num_classes = scores.shape[1] for cls_ind in range(num_classes): cls_scores = scores[:, cls_ind] valid_score_mask = cls_scores > score_thr if valid_score_mask.sum() == 0: continue else: valid_scores = cls_scores[valid_score_mask] valid_boxes = boxes[valid_score_mask] keep = nms(valid_boxes, valid_scores, nms_thr) if len(keep) > 0: cls_inds = np.ones((len(keep), 1)) * cls_ind dets = np.concatenate( [valid_boxes[keep], valid_scores[keep, None], cls_inds], 1 ) final_dets.append(dets) if len(final_dets) == 0: return None return np.concatenate(final_dets, 0) def multiclass_nms_class_agnostic(boxes, scores, nms_thr, score_thr): """Multiclass NMS implemented in Numpy. Class-agnostic version.""" cls_inds = scores.argmax(1) cls_scores = scores[np.arange(len(cls_inds)), cls_inds] valid_score_mask = cls_scores > score_thr if valid_score_mask.sum() == 0: return None valid_scores = cls_scores[valid_score_mask] valid_boxes = boxes[valid_score_mask] valid_cls_inds = cls_inds[valid_score_mask] keep = nms(valid_boxes, valid_scores, nms_thr) if keep: dets = np.concatenate( [valid_boxes[keep], valid_scores[keep, None], valid_cls_inds[keep, None]], 1 ) return dets def demo_postprocess(outputs, img_size, p6=False): grids = [] expanded_strides = [] if not p6: strides = [8, 16, 32] else: strides = [8, 16, 32, 64] hsizes = [img_size[0] // stride for stride in strides] wsizes = [img_size[1] // stride for stride in strides] for hsize, wsize, stride in zip(hsizes, wsizes, strides): xv, yv = np.meshgrid(np.arange(wsize), np.arange(hsize)) grid = np.stack((xv, yv), 2).reshape(1, -1, 2) grids.append(grid) shape = grid.shape[:2] expanded_strides.append(np.full((*shape, 1), stride)) grids = np.concatenate(grids, 1) expanded_strides = np.concatenate(expanded_strides, 1) outputs[..., :2] = (outputs[..., :2] + grids) * expanded_strides outputs[..., 2:4] = np.exp(outputs[..., 2:4]) * expanded_strides return outputs ================================================ FILE: asone/linux/Instructions/Benchmarking.md ================================================ # Model Zoo ## Hardware Used: - CPU: Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz - GPU: 8GB (RTX2080) ## Trackers ### DeepSort | Model | Model Flag | FPS-GPU | FPS-CPU |---------------- |-----------| -----------| -------- |DeepSort-ONNX-Yolov5s|DEEPSORT|13|3.2| |DeepSort-Pytorch-Yolov5s|DEEPSORT|13|3.2| ### ByteTrack | Model | Model Flag | FPS-GPU | FPS-CPU |---------------- |-----------| -----------| -------- |ByteTrack-ONNX-YOLOv5s|BYTETRACK|33.7|17.4| |ByteTrack-Pytorch-Sample-YOLOv5s|BYTETRACK|33.7|17.4| ### NorFair | Model | Model Flag | FPS-GPU | FPS-CPU |---------------- |-----------| -----------| -------- |tryolab-ONNX-YOLOv5s|NORFAIR|25.8|12| |tryolab-Pytorch-YOLOv5s|NORFAIR|25.8|12| ### MOTPY | Model | Model Flag | FPS-GPU | FPS-CPU |---------------- |-----------| -----------| -------- |MOTPY-ONNX-YOLOv7|MOTPY|27.5|4.2| |MOTPY-Pytorch-YOLOv7|MOTPY|32.4|3.5| ### StrongSort | Model | Model Flag | FPS-GPU | FPS-CPU |---------------- |-----------| -----------| -------- |StrongSort-ONNX-YOLOv7|STRONGSORT|7.6|3.1| |StrongSort-Pytorch-YOLOv7|STRONGSORT|7.9|3.1| ### OCSORT | Model | Model Flag | FPS-GPU | FPS-CPU |---------------- |-----------| -----------| -------- |OCSORT-ONNX-YOLOv7|OCSORT|25.7|3.4| |OCSORT-Pytorch-YOLOv7|OCSORT|31.4|3.2| ## Detectors ### YOLOv5 | PyTorch |ONNX |COREML | |:-------------------------------:|:-----------------------------:|:-----------------------------:| |
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOV5X6_PYTORCH 20.8 3.69
YOLOV5S_PYTORCH 57.25 25.4
YOLOV5N_PYTORCH 68 45
YOLOV5M_PYTORCH 54 14
YOLOV5L_PYTORCH 40.06 8.28
YOLOV5X_PYTORCH 28.8 4.32
YOLOV5N6_PYTORCH 63.5 39
YOLOV5S6_PYTORCH 58 23
YOLOV5M6_PYTORCH 49 10
YOLOV5L6_PYTORCH 33 6.5
|
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOV5X6_ONNX 2.58 2.46
YOLOV5S_ONNX 17 16.35
YOLOV5N_ONNX 57.25 35.23
YOLOV5M_ONNX 45.8 11.17
YOLOV5L_ONNX 4.07 4.36
YOLOV5X_ONNX 2.32 2.6
YOLOV5N6_ONNX 28.6 32.7
YOLOV5S6_ONNX 17 16.35
YOLOV5M6_ONNX 7.5 7.6
YOLOV5L6_ONNX 3.7 3.98
|
Model Name / Model Flag
YOLOV5X6_MLMODEL
YOLOV5S_MLMODEL
YOLOV5N_MLMODEL
YOLOV5M_MLMODEL
YOLOV5L_MLMODEL
YOLOV5X_MLMODEL
YOLOV5N6_MLMODEL
YOLOV5S6_MLMODEL
YOLOV5M6_MLMODEL
YOLOV5L6_MLMODEL
| ### YOLOv6 | PyTorch |ONNX | |:-------------------------------:|:-----------------------------:| |
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOV6N_PYTORCH 65.4 35.32
YOLOV6T_PYTORCH 63 15.21
YOLOV6S_PYTORCH 49.24 20
YOLOV6M_PYTORCH 35 9.96
YOLOV6L_PYTORCH 31 6.2
YOLOV6L_RELU_PYTORCH 27 6.3
YOLOV6S_REPOPT_PYTORCH 63.5 39
|
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOV6N_ONNX 50 30
YOLOV6T_ONNX 45.8 16
YOLOV6S_ONNX 41 13.8
YOLOV6M_ONNX 25 6.07
YOLOV6L_ONNNX 17.7 3.32
YOLOV6L_RELU_ONNX 19.15 4.36
YOLOV6S_REPOPT_ONNX 63.5 39
| ### YOLOv7 | PyTorch |ONNX |COREML |POSE | |:-------------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:| |
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOV7_TINY_PYTORCH 53 19
YOLOV7_PYTORCH 38 6.83
YOLOV7_X_PYTORCH 28 4.36
YOLOV7_W6_PYTORCH 32.7 7.26
YOLOV7_E6_PYTORCH 15.26 3.07
YOLOV7_D6_PYTORCH 21 3.78
YOLOV7_E6E_PYTORCH 24 3.36
|
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOV7_TINY_ONNX 41.6 22
YOLOV7_ONNX 26 3.78
YOLOV7_X_ONNX 19.08 2.35
YOLOV7_W6_ONNX 28.6 5.2
YOLOV7_E6_ONNX 14.3 2.97
YOLOV7_D6_ONNX 18.32 2.58
YOLOV7_E6E_ONNX 15.26 2.09
|
Model Name / Model Flag
YOLOV7_TINY_MLMODEL
YOLOV7_MLMODEL
YOLOV7_X_MLMODEL
YOLOV7_W6_MLMODEL
YOLOV7_E6_MLMODEL
YOLOV7_D6_MLMODEL
YOLOV7_E6E_MLMODEL
|
Model Name / Model Flag
---
---
---
YOLOV7_W6_POSE
---
---
---
| ### YOLOR | PyTorch |ONNX | |:-------------------------------:|:-----------------------------:| |
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOR_CSP_X_PYTORCH 28.6 1.83
YOLOR_CSP_X_STAR_PYTORCH 30 1.76
YOLOR_CSP_STAR_PYTORCH 38.1 2.86
YOLOR_CSP_PYTORCH 38 2.77
YOLOR_P6_PYTORCH 20 1.57
|
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOR_CSP_X_ONNX 15.7 2.53
YOLOR_CSP_X_STAR_ONNX 15.79 2.05
YOLOR_CSP_STAR_ONNX 18.32 3.34
YOLOR_CSP_ONNX 15.7 2.53
YOLOR_P6_ONNX 25.4 5.58
| ### YOLOX | PyTorch |ONNX | |:-------------------------------:|:-----------------------------:| |
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOX_L_PYTORCH 2.58 2.31
YOLOX_NANO_PYTORCH 35 32
YOLOX_TINY_PYTORCH 25.4 25.4
YOLOX_DARKNET_PYTORCH 2 1.94
YOLOX_S_PYTORCH 9.54 9.7
YOLOX_M_PYTORCH 4.4 4.36
YOLOX_X_PYTORCH 15.64 1.39
|
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOX_L_ONNX 22.9 3.07
YOLOX_NANO_ONNX 59 54
YOLOX_TINY_ONNX 60 35
YOLOX_DARKNET_ONNX 24 3.36
YOLOX_S_ONNX 45 13.8
YOLOX_M_ONNX 32 6.54
YOLOX_X_ONNX 15.79 2.03
| ### YOLOv8 | PyTorch |ONNX |COREML |POSE | |:-------------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:| |
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOV8N_PYTORCH 26.7 17.0
YOLOV8S_PYTORCH 26.4 12.3
YOLOV8M_PYTORCH 25.1 6.8
YOLOV8L_PYTORCH 23.6 4.0
YOLOV8X_PYTORCH 20.7 2.8
|
Model Name / Model Flag FPS-GPU FPS-CPU
YOLOV8N_ONNX 25.1 10.5
YOLOV8S_ONNX 24.5 7.5
YOLOV8M_ONNX 22.9 4.7
YOLOV8l_ONNX 20.4 2.9
YOLOV8X_ONNX 19.0 2.0
|
Model Name / Model Flag
YOLOV8N_MLMODEL
YOLOV8S_MLMODEL
YOLOV8M_MLMODEL
YOLOV8L_MLMODEL
YOLOV8X_MLMODEL
|
Model Name / Model Flag
YOLOV8N_POSE
YOLOV8S_POSE
YOLOV8M_POSE
YOLOV8L_POSE
YOLOV8X_POSE
| ### YOLO-NAS | Model Name/Model Flag | |----------------------- | | YOLONAS_S_PYTORCH | | YOLONAS_M_PYTORCH | | YOLONAS_L_PYTORCH | ## OCR | Model | Model Flag |---------------- |-----------| |Craft|CRAFT| |dbnet18|DBNET18 Return to [Installation Page](../../../README.md) ================================================ FILE: asone/linux/Instructions/Demo-Detectron2.md ================================================ # ASOne #### Table of Contents - [Docker Demo](#docker-demo) - [Docker Demo](#docker-demo-1) - [Setup Detectron](#setup-detectron) - [Demo using docker compose](#demo-using-docker-compose-file) - [Test Detectron2](#test-detectron2) # Docker Installation - If you haven't installed docker first install it by following provided instructions [here](../) ## Docker Demo ### Setting Up detectron2 1. Clone the Repo ``` git clone https://github.com/facebookresearch/detectron2.git ``` 2. Goto the detectron2 directory ``` cd detectron2 ``` 3. Download some sample images in this folder ### Demo Using Docker Compose File 1. Run container without gpu ``` docker compose run linux ``` 2. Run container with gpu ``` docker compose run linux-gpu ``` - To test DISPLAY is shared with docker properly: ``` python main.py ``` - if an image show up then everything is working properly. - if you see an error saying `qt.qpa.xcb: could not connect to display` that means your display is not accessible to docker. Try this: ``` sudo xhost +local:docker ``` - To build and run docker container manually follow instructions for [Manual Build](Manual-Build.md) ### Test Detectron2 1. After docker container starts properly, in docker terminal change directory using. ``` cd detectron2 ``` 2. In Docker terminal run demo.py file ``` python demo/demo.py --input [PATH_TO_TEST_IMAGE] --output [PATH_TO_OUTPUT_IMAGE] \ --opts MODEL.DEVICE [DEVICE] \ MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl ``` - `PATH_TO_TEST_IMAGE` = Path of test image - `PATH_TO_OUTPUT_IMAGE` = Path of Results - `DEVICE` = device to use i.e. `cpu` or `gpu` e.g. ``` python demo/demo.py --input ../test.jpeg --output ../result.jpg \ --opts MODEL.DEVICE gpu \ MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl ``` ================================================ FILE: asone/linux/Instructions/Docker-Setup.md ================================================ # Setting ASOne on Docker 1. Clone the repo ``` git clone https://github.com/axcelerateai/asone.git cd asone ``` 2. If using windows, Run this command in command prompt. ``` set PWD=%cd% ``` 2. Run docker coompose command. ``` # To test on Linux with GPU docker compose run linux-gpu # To test on Windows with GPU docker compose run windows-gpu ``` ``` # To test on Linux with CPU docker compose run linux # To test on Windows with CPU docker compose run windows ``` 3. In docker terminal. ``` # if using gpu python main.py [VIDEO_PATH] # if using cpu python main.py [VIDEO_PATH] --cpu ``` Return to [main page](../../README.md) ================================================ FILE: asone/linux/Instructions/Driver-Installations.md ================================================ # Driver Installations ### Linux For systems with `GPU` please verify you have nvidia drivers installed. Run ``` nvidia-smi ``` Drivers are installed if you see following. ![](../imgs/nvidia-drivers.png) If drivers are not installed, you can do so using following command: ``` sudo apt-get install nvidia-driver-YYY nvidia-dkms-YYY ``` where, - `YYY`= Nvidia driver version e.g `sudo apt-get install nvidia-driver-510 nvidia-dkms-510` - `Reboot` your system after installing nvidia-drivers. ``` sudo reboot ``` Return to [Installation Page](../../../README.md) ================================================ FILE: asone/linux/Instructions/Manual-Build.md ================================================ # ASOne # Docker Manual Build ## Docker Installation - If you haven't installed docker first install it by following provided instructions [here](../) ## Build Image Manually 1. Run the follwoing command to build docker image ``` docker build -t [IMAGE_NAME]:[TAG] . ``` - `IMAGE_NAME` = Asign a name to image - `TAG` = Asign a tag to image e.g. `docker build -t asone:latest .` ## Run Build Image 1. To run the build image in docker container with `cpu`. ``` docker run --env="DISPLAY" --net=host -v [PATH_TO_LOCAL_DIR]:/workspace/ -it [IMAGE_NAME]:[TAG] ``` - `IMAGE_NAME` = Asign a name to image - `TAG` = Asign a tag to image - `PATH_TO_LOCAL_DIR` = Path to detectron2 directory or use `$PWD` if already in that directory e.g `docker run --env="DISPLAY" --net=host -v $PWD:/workspace/ -it asone:latest` 2. To run th ebuild image in docker container with `gpu` ``` docker run --gpus all --env="DISPLAY" --net=host -v [PATH_TO_LOCAL_DIR]:/workspace/ -it [IMAGE_NAME]:[TAG] ``` - `IMAGE_NAME` = Asign a name to image - `TAG` = Asign a tag to image - `PATH_TO_LOCAL_DIR` = Path to detectron2 directory or use `$PWD` if already in that directory e.g `docker run --gpus all --env="DISPLAY" --net=host -v $PWD:/workspace/ -it asone:latest` ================================================ FILE: asone/linux/Instructions/Manual-Installation.md ================================================ # ASOne # Docker Manual Installation ## Ubuntu 1. Run following command to remove all old versions on docker ``` sudo apt-get remove docker docker-engine docker.io containerd runc ``` 2. Set up Repository - Update the apt package index and install packages to allow apt to use a repository over HTTPS: ``` sudo apt-get update sudo apt-get install \ ca-certificates \ curl \ gnupg \ lsb-release ``` - Add Docker’s official GPG key: ``` sudo mkdir -p /etc/apt/keyrings curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg ``` - Use the following command to set up the repository: ``` echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null ``` 3. Install Docker Engine - Update the apt package index, and install the latest version of Docker Engine, containerd, and Docker Compose: ``` sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin ``` 4. Install `nvidia-docker` to allow docker interact with GPU. ``` sudo apt-get install -y nvidia-docker2 sudo systemctl restart docker ``` 5. Give docker access to devices. ``` sudo xhost +local:docker sudo groupadd docker sudo gpasswd -a $USER docker newgrp docker ``` ================================================ FILE: asone/linux/README.md ================================================ # ASOne #### Table of Contents - [Docker Intallation](#docker-installation) - [Ubuntu](#ubuntu) - [Prerequisite](#prerequisite) - [Install Using Shell Script](#install-using-shell-script) # Docker Installation ## Ubuntu #### Prerequisite 1. For systems with `GPU` please verify you have nvidia drivers installed. run ``` nvidia-smi ``` if you see something like the following. Then you can continue running [shell script](#install-using-shell-script) ![](imgs/nvidia-drivers.png) or you can install nvidia drivers using following command: ``` sudo apt-get install nvidia-driver-YYY nvidia-dkms-YYY ``` - `YYY`= Nvidia driver version e.g `sudo apt-get install nvidia-driver-510 nvidia-dkms-510` - `Reboot` your system after installing nvidia-drivers. ``` sudo reboot ``` #### Install using Shell Script ``` cd asone-linux chmod a+x docker-installation.sh ./docker-installation.sh ``` - For systems with `GPU` run following commands after installing docker. Setup the package repository and the GPG key: ``` distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \ && curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \ && curl -s -L https://nvidia.github.io/libnvidia-container/experimental/$distribution/libnvidia-container.list | \ sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \ sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list ``` Install the `nvidia-docker2` package (and dependencies) after updating the package listing: ``` sudo apt-get update sudo apt-get install -y nvidia-docker2 sudo systemctl restart docker sudo xhost +local:docker ``` - [NOTE] If there is an error while installing docker, try removing apt-lists and resinstalling. ``` sudo rm -rf /var/lib/apt/lists/* ./docker-installation.sh ``` ``` # jump back to main folder cd .. ``` If everything is done successfully you can return to [Installation Page](../README.md) In case shell script keeps failing or you want to install manually follow steps in [Manual Installation](Instructions/Manual-Installation.md) ================================================ FILE: asone/linux/docker-installation.sh ================================================ #!/bin/bash echo "[INFO]: Removing previous verions of dockers..." echo "[INFO]: Removing previous verions of dockers..." > logs.txt if sudo apt-get remove docker docker-engine docker.io containerd runc -y >> logs.txt; then echo "[INFO]: Previous docker removed successfully!" echo "[INFO]: Previous docker removed successfully!" >> logs.txt fi echo "[INFO]: Updating apt-package index..." echo "[INFO]: Updating apt-package index..." >> logs.txt if sudo apt-get update -y >> logs.txt; then echo "[INFO]: apt-package index updated successfuly!" echo "[INFO]: apt-package index updated successfuly!" >> logs.txt else echo "[ERROR]: Error while updating apt-package index. Check logs.txt file for more info." echo "[ERROR]: Error while updating apt-package index." >> logs.txt # exit 1 fi echo "[INFO]: Installing required apt packages..." echo "[INFO]: Installing required apt packages..." >> logs.txt if sudo apt-get install \ ca-certificates \ curl \ gnupg \ lsb-release -y ; then echo "[INFO]: Required apt packages installed successfully!" echo "[INFO]: Required apt packages installed successfully!" >> logs.txt else echo "[ERROR]: Error installing required apt packages. Check logs.txt file for more info." echo "[ERROR]: Error installing required apt packages." >> logs.txt exit 1 fi echo "[INFO]: Adding docker GPG key..." echo "[INFO]: Adding docker GPG key..." >> logs.txt sudo mkdir -p /etc/apt/keyrings if curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg >> logs.txt;then echo "[INFO]: Docker GPG key added successfully!" echo "[INFO]: Docker GPG key added successfully!" >> logs.txt else echo "[ERROR]: Error adding docker GPG key. Check logs.txt file for more info." echo "[ERROR]: Error adding docker GPG key." >> logs.txt exit 1 fi echo "[INFO]: Setting docker repository..." echo "[INFO]: Setting docker repository..." >> logs.txt if echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null >> logs.txt; then echo "[INFO]: Docker repository setup done." echo "[INFO]: Docker repository setup done." >> logs.txt else echo "[ERROR]: Error setting up docker repository. Check logs.txt file for more info." echo "[ERROR]: Error setting up docker repository." >> logs.txt exit 1 fi echo "[INFO]: Installing Docker Engine..." echo "[INFO]: Installing Docker Engine..." >> logs.txt if sudo apt-get update -y >> logs.txt; then if sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y >> logs.txt; then if sudo docker --version; then echo "[INFO]: Docker Engine instaleld successfully!" echo "[INFO]: Docker Engine instaleld successfully!" >> logs.txt fi else echo "[ERROR]: Error installing docker engine. Check logs.txt file for more info." echo "[ERROR]: Error installing docker engine." >> logs.txt exit 1 fi else echo "[ERROR]: Error updating apt packages. Check logs.txt file for more info." echo "[ERROR]: Error updating apt packages." >> logs.txt # exit 1 fi echo "[INFO]: Adding docker to sudo group..." echo "[INFO]: Adding docker to sudo group..." >> logs.txt sudo xhost +local:docker sudo groupadd docker sudo gpasswd -a $USER docker newgrp docker echo "[INFO]: Docker Installation and setup completed successfully!" echo "[INFO]: Docker Installation and setup completed successfully!" >> logs.txt ================================================ FILE: asone/linux/main.py ================================================ import cv2 import numpy as np img = cv2.imread('test-asone.jpeg') cv2.imshow('RESULT', img) cv2.waitKey(0) ================================================ FILE: asone/pose_estimator.py ================================================ import cv2 import numpy as np import os from .utils import draw_kpts, plot_skeleton_kpts import cv2 import time import warnings from asone.pose_estimators.yolov7_pose import Yolov7PoseEstimator from asone.pose_estimators.yolov8_pose import Yolov8PoseEstimator from asone.utils import get_weight_path, download_weights from asone.schemas.output_schemas import ModelOutput class PoseEstimator: def __init__(self, estimator_flag, weights: str=None, use_cuda=True): if weights: weights = weights else: weights = get_weight_path(estimator_flag) if not os.path.exists(weights): download_weights(weights) self.estimator = self.get_estimator(estimator_flag, weights, use_cuda) self.model_output = ModelOutput() def get_estimator(self, estimator_flag: int, weights: str, use_cuda: bool): if estimator_flag in range(149, 155): estimator = Yolov7PoseEstimator(weights=weights, use_cuda=use_cuda) elif estimator_flag in range(144, 149): estimator = Yolov8PoseEstimator(weights=weights, use_cuda=use_cuda) return estimator def estimate_image(self, frame): keypoints = self.estimator.estimate(frame) return keypoints.cpu().numpy().xy def estimate_video(self, video_path, save=True, conf_thresh=0.5, display=True): # Emit the warning for DeprecationWarning with warnings.catch_warnings(): warnings.simplefilter("always", DeprecationWarning) warnings.warn("estimate_video function is deprecated. Kindly use video_estimator instead", DeprecationWarning) if video_path == 0: cap = cv2.VideoCapture(0) video_path = 'webcam.mp4' else: cap = cv2.VideoCapture(video_path) width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) FPS = cap.get(cv2.CAP_PROP_FPS) if save: video_writer = cv2.VideoWriter( os.path.basename(video_path), cv2.VideoWriter_fourcc(*"mp4v"), FPS, (int(width), int(height)), ) frame_no = 1 tic = time.time() frame_id = 1 prevTime = 0 fframe_num = 0 while True: start_time = time.time() ret, img = cap.read() if not ret: break frame = img.copy() kpts = self.estimator.estimate(img) currTime = time.time() fps = 1 / (currTime - prevTime) prevTime = currTime if kpts is not None: img = draw_kpts(kpts, image=img) cv2.line(img, (20, 25), (127, 25), [85, 45, 255], 30) cv2.putText(img, f'FPS: {int(fps)}', (11, 35), 0, 1, [ 225, 255, 255], thickness=2, lineType=cv2.LINE_AA) frame_id += 1 frame_no+=1 if display: cv2.imshow('Window', img) if save: video_writer.write(img) if cv2.waitKey(25) & 0xFF == ord('q'): break yield (kpts), (img if display else frame, frame_id-1, fps) def video_estimator(self, video_path, save=True, conf_thresh=0.5, display=True): if video_path == 0: cap = cv2.VideoCapture(0) video_path = 'webcam.mp4' else: cap = cv2.VideoCapture(video_path) width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) FPS = cap.get(cv2.CAP_PROP_FPS) if save: video_writer = cv2.VideoWriter( os.path.basename(video_path), cv2.VideoWriter_fourcc(*"mp4v"), FPS, (int(width), int(height)), ) frame_no = 1 tic = time.time() frame_id = 1 prevTime = 0 fframe_num = 0 while True: start_time = time.time() ret, img = cap.read() if not ret: break frame = img.copy() kpts = self.estimator.estimate(img) currTime = time.time() fps = 1 / (currTime - prevTime) prevTime = currTime if kpts is not None: img = draw_kpts(kpts, image=img) cv2.line(img, (20, 25), (127, 25), [85, 45, 255], 30) cv2.putText(img, f'FPS: {int(fps)}', (11, 35), 0, 1, [ 225, 255, 255], thickness=2, lineType=cv2.LINE_AA) frame_id += 1 frame_no+=1 if display: cv2.imshow('Window', img) if save: video_writer.write(img) if cv2.waitKey(25) & 0xFF == ord('q'): break yield self.format_output((kpts), (img if display else frame, frame_id-1, fps)) def format_output(self, bbox_details, frame_details): # Set detections self.model_output.dets.bbox = bbox_details if frame_details: # Set image info self.model_output.info.image = frame_details[0] self.model_output.info.frame_no = frame_details[1] self.model_output.info.fps = frame_details[2] return self.model_output ================================================ FILE: asone/pose_estimators/__init__.py ================================================ from .yolov7_pose import Yolov7PoseEstimator from .yolov8_pose import Yolov8PoseEstimator __all__ = ['Yolov7PoseEstimator', 'Yolov8PoseEstimator'] ================================================ FILE: asone/pose_estimators/yolov7_pose/__init__.py ================================================ from .yolov7 import Yolov7PoseEstimator __all__ = ['Yolov7PoseEstimator'] ================================================ FILE: asone/pose_estimators/yolov7_pose/main.py ================================================ from pose_estimate import PoseEstimator a = PoseEstimator() a.run() # parser.add_argument('--poseweights', nargs='+', type=str, default='yolov7-w6-pose.pt', help='model path(s)') # parser.add_argument('--source', type=str, default='football1.mp4', help='video/0 for webcam') #video source # parser.add_argument('--device', type=str, default='cpu', help='cpu/0,1,2,3(gpu)') #device arugments # parser.add_argument('--view-img', action='store_true', help='display results') #display results # parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels') #save confidence in txt writing # parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)') #box linethickness # parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels') #box hidelabel # parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences') #boxhideconf ================================================ FILE: asone/pose_estimators/yolov7_pose/models/__init__.py ================================================ # init ================================================ FILE: asone/pose_estimators/yolov7_pose/models/common.py ================================================ import math from copy import copy from pathlib import Path import numpy as np import pandas as pd import requests import torch import torch.nn as nn import torch.nn.functional as F from torchvision.ops import DeformConv2d from PIL import Image from torch.cuda import amp from asone.pose_estimators.yolov7_pose.utils.datasets import letterbox from asone.pose_estimators.yolov7_pose.utils.general import non_max_suppression, make_divisible, scale_coords, increment_path, xyxy2xywh from asone.pose_estimators.yolov7_pose.utils.plots import color_list, plot_one_box from asone.pose_estimators.yolov7_pose.utils.torch_utils import time_synchronized ##### basic #### def autopad(k, p=None): # kernel, padding # Pad to 'same' if p is None: p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad return p class MP(nn.Module): def __init__(self, k=2): super(MP, self).__init__() self.m = nn.MaxPool2d(kernel_size=k, stride=k) def forward(self, x): return self.m(x) class SP(nn.Module): def __init__(self, k=3, s=1): super(SP, self).__init__() self.m = nn.MaxPool2d(kernel_size=k, stride=s, padding=k // 2) def forward(self, x): return self.m(x) class ReOrg(nn.Module): def __init__(self): super(ReOrg, self).__init__() def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2) return torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1) class Concat(nn.Module): def __init__(self, dimension=1): super(Concat, self).__init__() self.d = dimension def forward(self, x): return torch.cat(x, self.d) class Chuncat(nn.Module): def __init__(self, dimension=1): super(Chuncat, self).__init__() self.d = dimension def forward(self, x): x1 = [] x2 = [] for xi in x: xi1, xi2 = xi.chunk(2, self.d) x1.append(xi1) x2.append(xi2) return torch.cat(x1+x2, self.d) class Shortcut(nn.Module): def __init__(self, dimension=0): super(Shortcut, self).__init__() self.d = dimension def forward(self, x): return x[0]+x[1] class Foldcut(nn.Module): def __init__(self, dimension=0): super(Foldcut, self).__init__() self.d = dimension def forward(self, x): x1, x2 = x.chunk(2, self.d) return x1+x2 class Conv(nn.Module): # Standard convolution def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super(Conv, self).__init__() self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) self.bn = nn.BatchNorm2d(c2) self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) def forward(self, x): return self.act(self.bn(self.conv(x))) def fuseforward(self, x): return self.act(self.conv(x)) class RobustConv(nn.Module): # Robust convolution (use high kernel size 7-11 for: downsampling and other layers). Train for 300 - 450 epochs. def __init__(self, c1, c2, k=7, s=1, p=None, g=1, act=True, layer_scale_init_value=1e-6): # ch_in, ch_out, kernel, stride, padding, groups super(RobustConv, self).__init__() self.conv_dw = Conv(c1, c1, k=k, s=s, p=p, g=c1, act=act) self.conv1x1 = nn.Conv2d(c1, c2, 1, 1, 0, groups=1, bias=True) self.gamma = nn.Parameter(layer_scale_init_value * torch.ones(c2)) if layer_scale_init_value > 0 else None def forward(self, x): x = x.to(memory_format=torch.channels_last) x = self.conv1x1(self.conv_dw(x)) if self.gamma is not None: x = x.mul(self.gamma.reshape(1, -1, 1, 1)) return x class RobustConv2(nn.Module): # Robust convolution 2 (use [32, 5, 2] or [32, 7, 4] or [32, 11, 8] for one of the paths in CSP). def __init__(self, c1, c2, k=7, s=4, p=None, g=1, act=True, layer_scale_init_value=1e-6): # ch_in, ch_out, kernel, stride, padding, groups super(RobustConv2, self).__init__() self.conv_strided = Conv(c1, c1, k=k, s=s, p=p, g=c1, act=act) self.conv_deconv = nn.ConvTranspose2d(in_channels=c1, out_channels=c2, kernel_size=s, stride=s, padding=0, bias=True, dilation=1, groups=1 ) self.gamma = nn.Parameter(layer_scale_init_value * torch.ones(c2)) if layer_scale_init_value > 0 else None def forward(self, x): x = self.conv_deconv(self.conv_strided(x)) if self.gamma is not None: x = x.mul(self.gamma.reshape(1, -1, 1, 1)) return x def DWConv(c1, c2, k=1, s=1, act=True): # Depthwise convolution return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act) class GhostConv(nn.Module): # Ghost Convolution https://github.com/huawei-noah/ghostnet def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups super(GhostConv, self).__init__() c_ = c2 // 2 # hidden channels self.cv1 = Conv(c1, c_, k, s, None, g, act) self.cv2 = Conv(c_, c_, 5, 1, None, c_, act) def forward(self, x): y = self.cv1(x) return torch.cat([y, self.cv2(y)], 1) class Stem(nn.Module): # Stem def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super(Stem, self).__init__() c_ = int(c2/2) # hidden channels self.cv1 = Conv(c1, c_, 3, 2) self.cv2 = Conv(c_, c_, 1, 1) self.cv3 = Conv(c_, c_, 3, 2) self.pool = torch.nn.MaxPool2d(2, stride=2) self.cv4 = Conv(2 * c_, c2, 1, 1) def forward(self, x): x = self.cv1(x) return self.cv4(torch.cat((self.cv3(self.cv2(x)), self.pool(x)), dim=1)) class DownC(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, n=1, k=2): super(DownC, self).__init__() c_ = int(c1) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c2//2, 3, k) self.cv3 = Conv(c1, c2//2, 1, 1) self.mp = nn.MaxPool2d(kernel_size=k, stride=k) def forward(self, x): return torch.cat((self.cv2(self.cv1(x)), self.cv3(self.mp(x))), dim=1) class SPP(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP def __init__(self, c1, c2, k=(5, 9, 13)): super(SPP, self).__init__() c_ = c1 // 2 # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1) self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) def forward(self, x): x = self.cv1(x) return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1)) class Bottleneck(nn.Module): # Darknet bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super(Bottleneck, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c2, 3, 1, g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class Res(nn.Module): # ResNet bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super(Res, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c_, 3, 1, g=g) self.cv3 = Conv(c_, c2, 1, 1) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv3(self.cv2(self.cv1(x))) if self.add else self.cv3(self.cv2(self.cv1(x))) class ResX(Res): # ResNet bottleneck def __init__(self, c1, c2, shortcut=True, g=32, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super().__init__(c1, c2, shortcut, g, e) c_ = int(c2 * e) # hidden channels class Ghost(nn.Module): # Ghost Bottleneck https://github.com/huawei-noah/ghostnet def __init__(self, c1, c2, k=3, s=1): # ch_in, ch_out, kernel, stride super(Ghost, self).__init__() c_ = c2 // 2 self.conv = nn.Sequential(GhostConv(c1, c_, 1, 1), # pw DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw GhostConv(c_, c2, 1, 1, act=False)) # pw-linear self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False), Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity() def forward(self, x): return self.conv(x) + self.shortcut(x) ##### end of basic ##### ##### cspnet ##### class SPPCSPC(nn.Module): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)): super(SPPCSPC, self).__init__() c_ = int(2 * c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(c_, c_, 3, 1) self.cv4 = Conv(c_, c_, 1, 1) self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) self.cv5 = Conv(4 * c_, c_, 1, 1) self.cv6 = Conv(c_, c_, 3, 1) self.cv7 = Conv(2 * c_, c2, 1, 1) def forward(self, x): x1 = self.cv4(self.cv3(self.cv1(x))) y1 = self.cv6(self.cv5(torch.cat([x1] + [m(x1) for m in self.m], 1))) y2 = self.cv2(x) return self.cv7(torch.cat((y1, y2), dim=1)) class GhostSPPCSPC(SPPCSPC): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5, k=(5, 9, 13)): super().__init__(c1, c2, n, shortcut, g, e, k) c_ = int(2 * c2 * e) # hidden channels self.cv1 = GhostConv(c1, c_, 1, 1) self.cv2 = GhostConv(c1, c_, 1, 1) self.cv3 = GhostConv(c_, c_, 3, 1) self.cv4 = GhostConv(c_, c_, 1, 1) self.cv5 = GhostConv(4 * c_, c_, 1, 1) self.cv6 = GhostConv(c_, c_, 3, 1) self.cv7 = GhostConv(2 * c_, c2, 1, 1) class GhostStem(Stem): # Stem def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super().__init__(c1, c2, k, s, p, g, act) c_ = int(c2/2) # hidden channels self.cv1 = GhostConv(c1, c_, 3, 2) self.cv2 = GhostConv(c_, c_, 1, 1) self.cv3 = GhostConv(c_, c_, 3, 2) self.cv4 = GhostConv(2 * c_, c2, 1, 1) class BottleneckCSPA(nn.Module): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPA, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.m(self.cv1(x)) y2 = self.cv2(x) return self.cv3(torch.cat((y1, y2), dim=1)) class BottleneckCSPB(nn.Module): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPB, self).__init__() c_ = int(c2) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x1 = self.cv1(x) y1 = self.m(x1) y2 = self.cv2(x1) return self.cv3(torch.cat((y1, y2), dim=1)) class BottleneckCSPC(nn.Module): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(BottleneckCSPC, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(c_, c_, 1, 1) self.cv4 = Conv(2 * c_, c2, 1, 1) self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(torch.cat((y1, y2), dim=1)) class ResCSPA(BottleneckCSPA): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class ResCSPB(BottleneckCSPB): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class ResCSPC(BottleneckCSPC): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class ResXCSPA(ResCSPA): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class ResXCSPB(ResCSPB): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class ResXCSPC(ResCSPC): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Res(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class GhostCSPA(BottleneckCSPA): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Ghost(c_, c_) for _ in range(n)]) class GhostCSPB(BottleneckCSPB): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[Ghost(c_, c_) for _ in range(n)]) class GhostCSPC(BottleneckCSPC): # CSP https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[Ghost(c_, c_) for _ in range(n)]) ##### end of cspnet ##### ##### yolor ##### class ImplicitA(nn.Module): def __init__(self, channel, mean=0., std=.02): super(ImplicitA, self).__init__() self.channel = channel self.mean = mean self.std = std self.implicit = nn.Parameter(torch.zeros(1, channel, 1, 1)) nn.init.normal_(self.implicit, mean=self.mean, std=self.std) def forward(self, x): return self.implicit + x class ImplicitM(nn.Module): def __init__(self, channel, mean=0., std=.02): super(ImplicitM, self).__init__() self.channel = channel self.mean = mean self.std = std self.implicit = nn.Parameter(torch.ones(1, channel, 1, 1)) nn.init.normal_(self.implicit, mean=self.mean, std=self.std) def forward(self, x): return self.implicit * x ##### end of yolor ##### ##### repvgg ##### class RepConv(nn.Module): # Represented convolution # https://arxiv.org/abs/2101.03697 def __init__(self, c1, c2, k=3, s=1, p=None, g=1, act=True, deploy=False): super(RepConv, self).__init__() self.deploy = deploy self.groups = g self.in_channels = c1 self.out_channels = c2 assert k == 3 assert autopad(k, p) == 1 padding_11 = autopad(k, p) - k // 2 self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity()) if deploy: self.rbr_reparam = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=True) else: self.rbr_identity = (nn.BatchNorm2d(num_features=c1) if c2 == c1 and s == 1 else None) self.rbr_dense = nn.Sequential( nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False), nn.BatchNorm2d(num_features=c2), ) self.rbr_1x1 = nn.Sequential( nn.Conv2d( c1, c2, 1, s, padding_11, groups=g, bias=False), nn.BatchNorm2d(num_features=c2), ) def forward(self, inputs): if hasattr(self, "rbr_reparam"): return self.act(self.rbr_reparam(inputs)) if self.rbr_identity is None: id_out = 0 else: id_out = self.rbr_identity(inputs) return self.act(self.rbr_dense(inputs) + self.rbr_1x1(inputs) + id_out) def get_equivalent_kernel_bias(self): kernel3x3, bias3x3 = self._fuse_bn_tensor(self.rbr_dense) kernel1x1, bias1x1 = self._fuse_bn_tensor(self.rbr_1x1) kernelid, biasid = self._fuse_bn_tensor(self.rbr_identity) return ( kernel3x3 + self._pad_1x1_to_3x3_tensor(kernel1x1) + kernelid, bias3x3 + bias1x1 + biasid, ) def _pad_1x1_to_3x3_tensor(self, kernel1x1): if kernel1x1 is None: return 0 else: return nn.functional.pad(kernel1x1, [1, 1, 1, 1]) def _fuse_bn_tensor(self, branch): if branch is None: return 0, 0 if isinstance(branch, nn.Sequential): kernel = branch[0].weight running_mean = branch[1].running_mean running_var = branch[1].running_var gamma = branch[1].weight beta = branch[1].bias eps = branch[1].eps else: assert isinstance(branch, nn.BatchNorm2d) if not hasattr(self, "id_tensor"): input_dim = self.in_channels // self.groups kernel_value = np.zeros( (self.in_channels, input_dim, 3, 3), dtype=np.float32 ) for i in range(self.in_channels): kernel_value[i, i % input_dim, 1, 1] = 1 self.id_tensor = torch.from_numpy(kernel_value).to(branch.weight.device) kernel = self.id_tensor running_mean = branch.running_mean running_var = branch.running_var gamma = branch.weight beta = branch.bias eps = branch.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta - running_mean * gamma / std def repvgg_convert(self): kernel, bias = self.get_equivalent_kernel_bias() return ( kernel.detach().cpu().numpy(), bias.detach().cpu().numpy(), ) def fuse_conv_bn(self, conv, bn): std = (bn.running_var + bn.eps).sqrt() bias = bn.bias - bn.running_mean * bn.weight / std t = (bn.weight / std).reshape(-1, 1, 1, 1) weights = conv.weight * t bn = nn.Identity() conv = nn.Conv2d(in_channels = conv.in_channels, out_channels = conv.out_channels, kernel_size = conv.kernel_size, stride=conv.stride, padding = conv.padding, dilation = conv.dilation, groups = conv.groups, bias = True, padding_mode = conv.padding_mode) conv.weight = torch.nn.Parameter(weights) conv.bias = torch.nn.Parameter(bias) return conv def fuse_repvgg_block(self): if self.deploy: return print(f"RepConv.fuse_repvgg_block") self.rbr_dense = self.fuse_conv_bn(self.rbr_dense[0], self.rbr_dense[1]) self.rbr_1x1 = self.fuse_conv_bn(self.rbr_1x1[0], self.rbr_1x1[1]) rbr_1x1_bias = self.rbr_1x1.bias weight_1x1_expanded = torch.nn.functional.pad(self.rbr_1x1.weight, [1, 1, 1, 1]) # Fuse self.rbr_identity if (isinstance(self.rbr_identity, nn.BatchNorm2d) or isinstance(self.rbr_identity, nn.modules.batchnorm.SyncBatchNorm)): # print(f"fuse: rbr_identity == BatchNorm2d or SyncBatchNorm") identity_conv_1x1 = nn.Conv2d( in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=1, stride=1, padding=0, groups=self.groups, bias=False) identity_conv_1x1.weight.data = identity_conv_1x1.weight.data.to(self.rbr_1x1.weight.data.device) identity_conv_1x1.weight.data = identity_conv_1x1.weight.data.squeeze().squeeze() # print(f" identity_conv_1x1.weight = {identity_conv_1x1.weight.shape}") identity_conv_1x1.weight.data.fill_(0.0) identity_conv_1x1.weight.data.fill_diagonal_(1.0) identity_conv_1x1.weight.data = identity_conv_1x1.weight.data.unsqueeze(2).unsqueeze(3) # print(f" identity_conv_1x1.weight = {identity_conv_1x1.weight.shape}") identity_conv_1x1 = self.fuse_conv_bn(identity_conv_1x1, self.rbr_identity) bias_identity_expanded = identity_conv_1x1.bias weight_identity_expanded = torch.nn.functional.pad(identity_conv_1x1.weight, [1, 1, 1, 1]) else: # print(f"fuse: rbr_identity != BatchNorm2d, rbr_identity = {self.rbr_identity}") bias_identity_expanded = torch.nn.Parameter( torch.zeros_like(rbr_1x1_bias) ) weight_identity_expanded = torch.nn.Parameter( torch.zeros_like(weight_1x1_expanded) ) #print(f"self.rbr_1x1.weight = {self.rbr_1x1.weight.shape}, ") #print(f"weight_1x1_expanded = {weight_1x1_expanded.shape}, ") #print(f"self.rbr_dense.weight = {self.rbr_dense.weight.shape}, ") self.rbr_dense.weight = torch.nn.Parameter(self.rbr_dense.weight + weight_1x1_expanded + weight_identity_expanded) self.rbr_dense.bias = torch.nn.Parameter(self.rbr_dense.bias + rbr_1x1_bias + bias_identity_expanded) self.rbr_reparam = self.rbr_dense self.deploy = True if self.rbr_identity is not None: del self.rbr_identity self.rbr_identity = None if self.rbr_1x1 is not None: del self.rbr_1x1 self.rbr_1x1 = None if self.rbr_dense is not None: del self.rbr_dense self.rbr_dense = None class RepBottleneck(Bottleneck): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super().__init__(c1, c2, shortcut=True, g=1, e=0.5) c_ = int(c2 * e) # hidden channels self.cv2 = RepConv(c_, c2, 3, 1, g=g) class RepBottleneckCSPA(BottleneckCSPA): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepBottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class RepBottleneckCSPB(BottleneckCSPB): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[RepBottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class RepBottleneckCSPC(BottleneckCSPC): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepBottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) class RepRes(Res): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super().__init__(c1, c2, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.cv2 = RepConv(c_, c_, 3, 1, g=g) class RepResCSPA(ResCSPA): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepRes(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class RepResCSPB(ResCSPB): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[RepRes(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class RepResCSPC(ResCSPC): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepRes(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class RepResX(ResX): # Standard bottleneck def __init__(self, c1, c2, shortcut=True, g=32, e=0.5): # ch_in, ch_out, shortcut, groups, expansion super().__init__(c1, c2, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.cv2 = RepConv(c_, c_, 3, 1, g=g) class RepResXCSPA(ResXCSPA): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepResX(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class RepResXCSPB(ResXCSPB): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2) # hidden channels self.m = nn.Sequential(*[RepResX(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) class RepResXCSPC(ResXCSPC): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=32, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super().__init__(c1, c2, n, shortcut, g, e) c_ = int(c2 * e) # hidden channels self.m = nn.Sequential(*[RepResX(c_, c_, shortcut, g, e=0.5) for _ in range(n)]) ##### end of repvgg ##### ##### transformer ##### class TransformerLayer(nn.Module): # Transformer layer https://arxiv.org/abs/2010.11929 (LayerNorm layers removed for better performance) def __init__(self, c, num_heads): super().__init__() self.q = nn.Linear(c, c, bias=False) self.k = nn.Linear(c, c, bias=False) self.v = nn.Linear(c, c, bias=False) self.ma = nn.MultiheadAttention(embed_dim=c, num_heads=num_heads) self.fc1 = nn.Linear(c, c, bias=False) self.fc2 = nn.Linear(c, c, bias=False) def forward(self, x): x = self.ma(self.q(x), self.k(x), self.v(x))[0] + x x = self.fc2(self.fc1(x)) + x return x class TransformerBlock(nn.Module): # Vision Transformer https://arxiv.org/abs/2010.11929 def __init__(self, c1, c2, num_heads, num_layers): super().__init__() self.conv = None if c1 != c2: self.conv = Conv(c1, c2) self.linear = nn.Linear(c2, c2) # learnable position embedding self.tr = nn.Sequential(*[TransformerLayer(c2, num_heads) for _ in range(num_layers)]) self.c2 = c2 def forward(self, x): if self.conv is not None: x = self.conv(x) b, _, w, h = x.shape p = x.flatten(2) p = p.unsqueeze(0) p = p.transpose(0, 3) p = p.squeeze(3) e = self.linear(p) x = p + e x = self.tr(x) x = x.unsqueeze(3) x = x.transpose(0, 3) x = x.reshape(b, self.c2, w, h) return x ##### end of transformer ##### ##### yolov5 ##### class Focus(nn.Module): # Focus wh information into c-space def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super(Focus, self).__init__() self.conv = Conv(c1 * 4, c2, k, s, p, g, act) # self.contract = Contract(gain=2) def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2) return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)) # return self.conv(self.contract(x)) class SPPF(nn.Module): # Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13)) super().__init__() c_ = c1 // 2 # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_ * 4, c2, 1, 1) self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2) def forward(self, x): x = self.cv1(x) y1 = self.m(x) y2 = self.m(y1) return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1)) class Contract(nn.Module): # Contract width-height into channels, i.e. x(1,64,80,80) to x(1,256,40,40) def __init__(self, gain=2): super().__init__() self.gain = gain def forward(self, x): N, C, H, W = x.size() # assert (H / s == 0) and (W / s == 0), 'Indivisible gain' s = self.gain x = x.view(N, C, H // s, s, W // s, s) # x(1,64,40,2,40,2) x = x.permute(0, 3, 5, 1, 2, 4).contiguous() # x(1,2,2,64,40,40) return x.view(N, C * s * s, H // s, W // s) # x(1,256,40,40) class Expand(nn.Module): # Expand channels into width-height, i.e. x(1,64,80,80) to x(1,16,160,160) def __init__(self, gain=2): super().__init__() self.gain = gain def forward(self, x): N, C, H, W = x.size() # assert C / s ** 2 == 0, 'Indivisible gain' s = self.gain x = x.view(N, s, s, C // s ** 2, H, W) # x(1,2,2,16,80,80) x = x.permute(0, 3, 4, 1, 5, 2).contiguous() # x(1,16,80,2,80,2) return x.view(N, C // s ** 2, H * s, W * s) # x(1,16,160,160) class NMS(nn.Module): # Non-Maximum Suppression (NMS) module conf = 0.25 # confidence threshold iou = 0.45 # IoU threshold classes = None # (optional list) filter by class def __init__(self): super(NMS, self).__init__() def forward(self, x): return non_max_suppression(x[0], conf_thres=self.conf, iou_thres=self.iou, classes=self.classes) class autoShape(nn.Module): # input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS conf = 0.25 # NMS confidence threshold iou = 0.45 # NMS IoU threshold classes = None # (optional list) filter by class def __init__(self, model): super(autoShape, self).__init__() self.model = model.eval() def autoshape(self): print('autoShape already enabled, skipping... ') # model already converted to model.autoshape() return self @torch.no_grad() def forward(self, imgs, size=640, augment=False, profile=False): # Inference from various sources. For height=640, width=1280, RGB images example inputs are: # filename: imgs = 'data/samples/zidane.jpg' # URI: = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/zidane.jpg' # OpenCV: = cv2.imread('image.jpg')[:,:,::-1] # HWC BGR to RGB x(640,1280,3) # PIL: = Image.open('image.jpg') # HWC x(640,1280,3) # numpy: = np.zeros((640,1280,3)) # HWC # torch: = torch.zeros(16,3,320,640) # BCHW (scaled to size=640, 0-1 values) # multiple: = [Image.open('image1.jpg'), Image.open('image2.jpg'), ...] # list of images t = [time_synchronized()] p = next(self.model.parameters()) # for device and type if isinstance(imgs, torch.Tensor): # torch with amp.autocast(enabled=p.device.type != 'cpu'): return self.model(imgs.to(p.device).type_as(p), augment, profile) # inference # Pre-process n, imgs = (len(imgs), imgs) if isinstance(imgs, list) else (1, [imgs]) # number of images, list of images shape0, shape1, files = [], [], [] # image and inference shapes, filenames for i, im in enumerate(imgs): f = f'image{i}' # filename if isinstance(im, str): # filename or uri im, f = np.asarray(Image.open(requests.get(im, stream=True).raw if im.startswith('http') else im)), im elif isinstance(im, Image.Image): # PIL Image im, f = np.asarray(im), getattr(im, 'filename', f) or f files.append(Path(f).with_suffix('.jpg').name) if im.shape[0] < 5: # image in CHW im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1) im = im[:, :, :3] if im.ndim == 3 else np.tile(im[:, :, None], 3) # enforce 3ch input s = im.shape[:2] # HWC shape0.append(s) # image shape g = (size / max(s)) # gain shape1.append([y * g for y in s]) imgs[i] = im # update shape1 = [make_divisible(x, int(self.stride.max())) for x in np.stack(shape1, 0).max(0)] # inference shape x = [letterbox(im, new_shape=shape1, auto=False)[0] for im in imgs] # pad x = np.stack(x, 0) if n > 1 else x[0][None] # stack x = np.ascontiguousarray(x.transpose((0, 3, 1, 2))) # BHWC to BCHW x = torch.from_numpy(x).to(p.device).type_as(p) / 255. # uint8 to fp16/32 t.append(time_synchronized()) with amp.autocast(enabled=p.device.type != 'cpu'): # Inference y = self.model(x, augment, profile)[0] # forward t.append(time_synchronized()) # Post-process y = non_max_suppression(y, conf_thres=self.conf, iou_thres=self.iou, classes=self.classes) # NMS for i in range(n): scale_coords(shape1, y[i][:, :4], shape0[i]) t.append(time_synchronized()) return Detections(imgs, y, files, t, self.names, x.shape) class Detections: # detections class for YOLOv5 inference results def __init__(self, imgs, pred, files, times=None, names=None, shape=None): super(Detections, self).__init__() d = pred[0].device # device gn = [torch.tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1., 1.], device=d) for im in imgs] # normalizations self.imgs = imgs # list of images as numpy arrays self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls) self.names = names # class names self.files = files # image filenames self.xyxy = pred # xyxy pixels self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized self.n = len(self.pred) # number of images (batch size) self.t = tuple((times[i + 1] - times[i]) * 1000 / self.n for i in range(3)) # timestamps (ms) self.s = shape # inference BCHW shape def display(self, pprint=False, show=False, save=False, render=False, save_dir=''): colors = color_list() for i, (img, pred) in enumerate(zip(self.imgs, self.pred)): str = f'image {i + 1}/{len(self.pred)}: {img.shape[0]}x{img.shape[1]} ' if pred is not None: for c in pred[:, -1].unique(): n = (pred[:, -1] == c).sum() # detections per class str += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string if show or save or render: for *box, conf, cls in pred: # xyxy, confidence, class label = f'{self.names[int(cls)]} {conf:.2f}' plot_one_box(box, img, label=label, color=colors[int(cls) % 10]) img = Image.fromarray(img.astype(np.uint8)) if isinstance(img, np.ndarray) else img # from np if pprint: print(str.rstrip(', ')) if show: img.show(self.files[i]) # show if save: f = self.files[i] img.save(Path(save_dir) / f) # save print(f"{'Saved' * (i == 0)} {f}", end=',' if i < self.n - 1 else f' to {save_dir}\n') if render: self.imgs[i] = np.asarray(img) def print(self): self.display(pprint=True) # print results print(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {tuple(self.s)}' % self.t) def show(self): self.display(show=True) # show results def save(self, save_dir='runs/hub/exp'): save_dir = increment_path(save_dir, exist_ok=save_dir != 'runs/hub/exp') # increment save_dir Path(save_dir).mkdir(parents=True, exist_ok=True) self.display(save=True, save_dir=save_dir) # save results def render(self): self.display(render=True) # render results return self.imgs def pandas(self): # return detections as pandas DataFrames, i.e. print(results.pandas().xyxy[0]) new = copy(self) # return copy ca = 'xmin', 'ymin', 'xmax', 'ymax', 'confidence', 'class', 'name' # xyxy columns cb = 'xcenter', 'ycenter', 'width', 'height', 'confidence', 'class', 'name' # xywh columns for k, c in zip(['xyxy', 'xyxyn', 'xywh', 'xywhn'], [ca, ca, cb, cb]): a = [[x[:5] + [int(x[5]), self.names[int(x[5])]] for x in x.tolist()] for x in getattr(self, k)] # update setattr(new, k, [pd.DataFrame(x, columns=c) for x in a]) return new def tolist(self): # return a list of Detections objects, i.e. 'for result in results.tolist():' x = [Detections([self.imgs[i]], [self.pred[i]], self.names, self.s) for i in range(self.n)] for d in x: for k in ['imgs', 'pred', 'xyxy', 'xyxyn', 'xywh', 'xywhn']: setattr(d, k, getattr(d, k)[0]) # pop out of list return x def __len__(self): return self.n class Classify(nn.Module): # Classification head, i.e. x(b,c1,20,20) to x(b,c2) def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups super(Classify, self).__init__() self.aap = nn.AdaptiveAvgPool2d(1) # to x(b,c1,1,1) self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g) # to x(b,c2,1,1) self.flat = nn.Flatten() def forward(self, x): z = torch.cat([self.aap(y) for y in (x if isinstance(x, list) else [x])], 1) # cat if list return self.flat(self.conv(z)) # flatten to x(b,c2) ##### end of yolov5 ###### ##### orepa ##### def transI_fusebn(kernel, bn): gamma = bn.weight std = (bn.running_var + bn.eps).sqrt() return kernel * ((gamma / std).reshape(-1, 1, 1, 1)), bn.bias - bn.running_mean * gamma / std class ConvBN(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, deploy=False, nonlinear=None): super().__init__() if nonlinear is None: self.nonlinear = nn.Identity() else: self.nonlinear = nonlinear if deploy: self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=True) else: self.conv = nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=False) self.bn = nn.BatchNorm2d(num_features=out_channels) def forward(self, x): if hasattr(self, 'bn'): return self.nonlinear(self.bn(self.conv(x))) else: return self.nonlinear(self.conv(x)) def switch_to_deploy(self): kernel, bias = transI_fusebn(self.conv.weight, self.bn) conv = nn.Conv2d(in_channels=self.conv.in_channels, out_channels=self.conv.out_channels, kernel_size=self.conv.kernel_size, stride=self.conv.stride, padding=self.conv.padding, dilation=self.conv.dilation, groups=self.conv.groups, bias=True) conv.weight.data = kernel conv.bias.data = bias for para in self.parameters(): para.detach_() self.__delattr__('conv') self.__delattr__('bn') self.conv = conv class OREPA_3x3_RepConv(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, internal_channels_1x1_3x3=None, deploy=False, nonlinear=None, single_init=False): super(OREPA_3x3_RepConv, self).__init__() self.deploy = deploy if nonlinear is None: self.nonlinear = nn.Identity() else: self.nonlinear = nonlinear self.kernel_size = kernel_size self.in_channels = in_channels self.out_channels = out_channels self.groups = groups assert padding == kernel_size // 2 self.stride = stride self.padding = padding self.dilation = dilation self.branch_counter = 0 self.weight_rbr_origin = nn.Parameter(torch.Tensor(out_channels, int(in_channels/self.groups), kernel_size, kernel_size)) nn.init.kaiming_uniform_(self.weight_rbr_origin, a=math.sqrt(1.0)) self.branch_counter += 1 if groups < out_channels: self.weight_rbr_avg_conv = nn.Parameter(torch.Tensor(out_channels, int(in_channels/self.groups), 1, 1)) self.weight_rbr_pfir_conv = nn.Parameter(torch.Tensor(out_channels, int(in_channels/self.groups), 1, 1)) nn.init.kaiming_uniform_(self.weight_rbr_avg_conv, a=1.0) nn.init.kaiming_uniform_(self.weight_rbr_pfir_conv, a=1.0) self.weight_rbr_avg_conv.data self.weight_rbr_pfir_conv.data self.register_buffer('weight_rbr_avg_avg', torch.ones(kernel_size, kernel_size).mul(1.0/kernel_size/kernel_size)) self.branch_counter += 1 else: raise NotImplementedError self.branch_counter += 1 if internal_channels_1x1_3x3 is None: internal_channels_1x1_3x3 = in_channels if groups < out_channels else 2 * in_channels # For mobilenet, it is better to have 2X internal channels if internal_channels_1x1_3x3 == in_channels: self.weight_rbr_1x1_kxk_idconv1 = nn.Parameter(torch.zeros(in_channels, int(in_channels/self.groups), 1, 1)) id_value = np.zeros((in_channels, int(in_channels/self.groups), 1, 1)) for i in range(in_channels): id_value[i, i % int(in_channels/self.groups), 0, 0] = 1 id_tensor = torch.from_numpy(id_value).type_as(self.weight_rbr_1x1_kxk_idconv1) self.register_buffer('id_tensor', id_tensor) else: self.weight_rbr_1x1_kxk_conv1 = nn.Parameter(torch.Tensor(internal_channels_1x1_3x3, int(in_channels/self.groups), 1, 1)) nn.init.kaiming_uniform_(self.weight_rbr_1x1_kxk_conv1, a=math.sqrt(1.0)) self.weight_rbr_1x1_kxk_conv2 = nn.Parameter(torch.Tensor(out_channels, int(internal_channels_1x1_3x3/self.groups), kernel_size, kernel_size)) nn.init.kaiming_uniform_(self.weight_rbr_1x1_kxk_conv2, a=math.sqrt(1.0)) self.branch_counter += 1 expand_ratio = 8 self.weight_rbr_gconv_dw = nn.Parameter(torch.Tensor(in_channels*expand_ratio, 1, kernel_size, kernel_size)) self.weight_rbr_gconv_pw = nn.Parameter(torch.Tensor(out_channels, in_channels*expand_ratio, 1, 1)) nn.init.kaiming_uniform_(self.weight_rbr_gconv_dw, a=math.sqrt(1.0)) nn.init.kaiming_uniform_(self.weight_rbr_gconv_pw, a=math.sqrt(1.0)) self.branch_counter += 1 if out_channels == in_channels and stride == 1: self.branch_counter += 1 self.vector = nn.Parameter(torch.Tensor(self.branch_counter, self.out_channels)) self.bn = nn.BatchNorm2d(out_channels) self.fre_init() nn.init.constant_(self.vector[0, :], 0.25) #origin nn.init.constant_(self.vector[1, :], 0.25) #avg nn.init.constant_(self.vector[2, :], 0.0) #prior nn.init.constant_(self.vector[3, :], 0.5) #1x1_kxk nn.init.constant_(self.vector[4, :], 0.5) #dws_conv def fre_init(self): prior_tensor = torch.Tensor(self.out_channels, self.kernel_size, self.kernel_size) half_fg = self.out_channels/2 for i in range(self.out_channels): for h in range(3): for w in range(3): if i < half_fg: prior_tensor[i, h, w] = math.cos(math.pi*(h+0.5)*(i+1)/3) else: prior_tensor[i, h, w] = math.cos(math.pi*(w+0.5)*(i+1-half_fg)/3) self.register_buffer('weight_rbr_prior', prior_tensor) def weight_gen(self): weight_rbr_origin = torch.einsum('oihw,o->oihw', self.weight_rbr_origin, self.vector[0, :]) weight_rbr_avg = torch.einsum('oihw,o->oihw', torch.einsum('oihw,hw->oihw', self.weight_rbr_avg_conv, self.weight_rbr_avg_avg), self.vector[1, :]) weight_rbr_pfir = torch.einsum('oihw,o->oihw', torch.einsum('oihw,ohw->oihw', self.weight_rbr_pfir_conv, self.weight_rbr_prior), self.vector[2, :]) weight_rbr_1x1_kxk_conv1 = None if hasattr(self, 'weight_rbr_1x1_kxk_idconv1'): weight_rbr_1x1_kxk_conv1 = (self.weight_rbr_1x1_kxk_idconv1 + self.id_tensor).squeeze() elif hasattr(self, 'weight_rbr_1x1_kxk_conv1'): weight_rbr_1x1_kxk_conv1 = self.weight_rbr_1x1_kxk_conv1.squeeze() else: raise NotImplementedError weight_rbr_1x1_kxk_conv2 = self.weight_rbr_1x1_kxk_conv2 if self.groups > 1: g = self.groups t, ig = weight_rbr_1x1_kxk_conv1.size() o, tg, h, w = weight_rbr_1x1_kxk_conv2.size() weight_rbr_1x1_kxk_conv1 = weight_rbr_1x1_kxk_conv1.view(g, int(t/g), ig) weight_rbr_1x1_kxk_conv2 = weight_rbr_1x1_kxk_conv2.view(g, int(o/g), tg, h, w) weight_rbr_1x1_kxk = torch.einsum('gti,gothw->goihw', weight_rbr_1x1_kxk_conv1, weight_rbr_1x1_kxk_conv2).view(o, ig, h, w) else: weight_rbr_1x1_kxk = torch.einsum('ti,othw->oihw', weight_rbr_1x1_kxk_conv1, weight_rbr_1x1_kxk_conv2) weight_rbr_1x1_kxk = torch.einsum('oihw,o->oihw', weight_rbr_1x1_kxk, self.vector[3, :]) weight_rbr_gconv = self.dwsc2full(self.weight_rbr_gconv_dw, self.weight_rbr_gconv_pw, self.in_channels) weight_rbr_gconv = torch.einsum('oihw,o->oihw', weight_rbr_gconv, self.vector[4, :]) weight = weight_rbr_origin + weight_rbr_avg + weight_rbr_1x1_kxk + weight_rbr_pfir + weight_rbr_gconv return weight def dwsc2full(self, weight_dw, weight_pw, groups): t, ig, h, w = weight_dw.size() o, _, _, _ = weight_pw.size() tg = int(t/groups) i = int(ig*groups) weight_dw = weight_dw.view(groups, tg, ig, h, w) weight_pw = weight_pw.squeeze().view(o, groups, tg) weight_dsc = torch.einsum('gtihw,ogt->ogihw', weight_dw, weight_pw) return weight_dsc.view(o, i, h, w) def forward(self, inputs): weight = self.weight_gen() out = F.conv2d(inputs, weight, bias=None, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups) return self.nonlinear(self.bn(out)) class RepConv_OREPA(nn.Module): def __init__(self, c1, c2, k=3, s=1, padding=1, dilation=1, groups=1, padding_mode='zeros', deploy=False, use_se=False, nonlinear=nn.SiLU()): super(RepConv_OREPA, self).__init__() self.deploy = deploy self.groups = groups self.in_channels = c1 self.out_channels = c2 self.padding = padding self.dilation = dilation self.groups = groups assert k == 3 assert padding == 1 padding_11 = padding - k // 2 if nonlinear is None: self.nonlinearity = nn.Identity() else: self.nonlinearity = nonlinear if use_se: self.se = SEBlock(self.out_channels, internal_neurons=self.out_channels // 16) else: self.se = nn.Identity() if deploy: self.rbr_reparam = nn.Conv2d(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=k, stride=s, padding=padding, dilation=dilation, groups=groups, bias=True, padding_mode=padding_mode) else: self.rbr_identity = nn.BatchNorm2d(num_features=self.in_channels) if self.out_channels == self.in_channels and s == 1 else None self.rbr_dense = OREPA_3x3_RepConv(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=k, stride=s, padding=padding, groups=groups, dilation=1) self.rbr_1x1 = ConvBN(in_channels=self.in_channels, out_channels=self.out_channels, kernel_size=1, stride=s, padding=padding_11, groups=groups, dilation=1) print('RepVGG Block, identity = ', self.rbr_identity) def forward(self, inputs): if hasattr(self, 'rbr_reparam'): return self.nonlinearity(self.se(self.rbr_reparam(inputs))) if self.rbr_identity is None: id_out = 0 else: id_out = self.rbr_identity(inputs) out1 = self.rbr_dense(inputs) out2 = self.rbr_1x1(inputs) out3 = id_out out = out1 + out2 + out3 return self.nonlinearity(self.se(out)) # Optional. This improves the accuracy and facilitates quantization. # 1. Cancel the original weight decay on rbr_dense.conv.weight and rbr_1x1.conv.weight. # 2. Use like this. # loss = criterion(....) # for every RepVGGBlock blk: # loss += weight_decay_coefficient * 0.5 * blk.get_cust_L2() # optimizer.zero_grad() # loss.backward() # Not used for OREPA def get_custom_L2(self): K3 = self.rbr_dense.weight_gen() K1 = self.rbr_1x1.conv.weight t3 = (self.rbr_dense.bn.weight / ((self.rbr_dense.bn.running_var + self.rbr_dense.bn.eps).sqrt())).reshape(-1, 1, 1, 1).detach() t1 = (self.rbr_1x1.bn.weight / ((self.rbr_1x1.bn.running_var + self.rbr_1x1.bn.eps).sqrt())).reshape(-1, 1, 1, 1).detach() l2_loss_circle = (K3 ** 2).sum() - (K3[:, :, 1:2, 1:2] ** 2).sum() # The L2 loss of the "circle" of weights in 3x3 kernel. Use regular L2 on them. eq_kernel = K3[:, :, 1:2, 1:2] * t3 + K1 * t1 # The equivalent resultant central point of 3x3 kernel. l2_loss_eq_kernel = (eq_kernel ** 2 / (t3 ** 2 + t1 ** 2)).sum() # Normalize for an L2 coefficient comparable to regular L2. return l2_loss_eq_kernel + l2_loss_circle def get_equivalent_kernel_bias(self): kernel3x3, bias3x3 = self._fuse_bn_tensor(self.rbr_dense) kernel1x1, bias1x1 = self._fuse_bn_tensor(self.rbr_1x1) kernelid, biasid = self._fuse_bn_tensor(self.rbr_identity) return kernel3x3 + self._pad_1x1_to_3x3_tensor(kernel1x1) + kernelid, bias3x3 + bias1x1 + biasid def _pad_1x1_to_3x3_tensor(self, kernel1x1): if kernel1x1 is None: return 0 else: return torch.nn.functional.pad(kernel1x1, [1,1,1,1]) def _fuse_bn_tensor(self, branch): if branch is None: return 0, 0 if not isinstance(branch, nn.BatchNorm2d): if isinstance(branch, OREPA_3x3_RepConv): kernel = branch.weight_gen() elif isinstance(branch, ConvBN): kernel = branch.conv.weight else: raise NotImplementedError running_mean = branch.bn.running_mean running_var = branch.bn.running_var gamma = branch.bn.weight beta = branch.bn.bias eps = branch.bn.eps else: if not hasattr(self, 'id_tensor'): input_dim = self.in_channels // self.groups kernel_value = np.zeros((self.in_channels, input_dim, 3, 3), dtype=np.float32) for i in range(self.in_channels): kernel_value[i, i % input_dim, 1, 1] = 1 self.id_tensor = torch.from_numpy(kernel_value).to(branch.weight.device) kernel = self.id_tensor running_mean = branch.running_mean running_var = branch.running_var gamma = branch.weight beta = branch.bias eps = branch.eps std = (running_var + eps).sqrt() t = (gamma / std).reshape(-1, 1, 1, 1) return kernel * t, beta - running_mean * gamma / std def switch_to_deploy(self): if hasattr(self, 'rbr_reparam'): return print(f"RepConv_OREPA.switch_to_deploy") kernel, bias = self.get_equivalent_kernel_bias() self.rbr_reparam = nn.Conv2d(in_channels=self.rbr_dense.in_channels, out_channels=self.rbr_dense.out_channels, kernel_size=self.rbr_dense.kernel_size, stride=self.rbr_dense.stride, padding=self.rbr_dense.padding, dilation=self.rbr_dense.dilation, groups=self.rbr_dense.groups, bias=True) self.rbr_reparam.weight.data = kernel self.rbr_reparam.bias.data = bias for para in self.parameters(): para.detach_() self.__delattr__('rbr_dense') self.__delattr__('rbr_1x1') if hasattr(self, 'rbr_identity'): self.__delattr__('rbr_identity') ##### end of orepa ##### ##### swin transformer ##### class WindowAttention(nn.Module): def __init__(self, dim, window_size, num_heads, qkv_bias=True, qk_scale=None, attn_drop=0., proj_drop=0.): super().__init__() self.dim = dim self.window_size = window_size # Wh, Ww self.num_heads = num_heads head_dim = dim // num_heads self.scale = qk_scale or head_dim ** -0.5 # define a parameter table of relative position bias self.relative_position_bias_table = nn.Parameter( torch.zeros((2 * window_size[0] - 1) * (2 * window_size[1] - 1), num_heads)) # 2*Wh-1 * 2*Ww-1, nH # get pair-wise relative position index for each token inside the window coords_h = torch.arange(self.window_size[0]) coords_w = torch.arange(self.window_size[1]) coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2 relative_coords[:, :, 0] += self.window_size[0] - 1 # shift to start from 0 relative_coords[:, :, 1] += self.window_size[1] - 1 relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1 relative_position_index = relative_coords.sum(-1) # Wh*Ww, Wh*Ww self.register_buffer("relative_position_index", relative_position_index) self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim) self.proj_drop = nn.Dropout(proj_drop) nn.init.normal_(self.relative_position_bias_table, std=.02) self.softmax = nn.Softmax(dim=-1) def forward(self, x, mask=None): B_, N, C = x.shape qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple) q = q * self.scale attn = (q @ k.transpose(-2, -1)) relative_position_bias = self.relative_position_bias_table[self.relative_position_index.view(-1)].view( self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], -1) # Wh*Ww,Wh*Ww,nH relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww attn = attn + relative_position_bias.unsqueeze(0) if mask is not None: nW = mask.shape[0] attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0) attn = attn.view(-1, self.num_heads, N, N) attn = self.softmax(attn) else: attn = self.softmax(attn) attn = self.attn_drop(attn) # print(attn.dtype, v.dtype) try: x = (attn @ v).transpose(1, 2).reshape(B_, N, C) except: #print(attn.dtype, v.dtype) x = (attn.half() @ v).transpose(1, 2).reshape(B_, N, C) x = self.proj(x) x = self.proj_drop(x) return x class Mlp(nn.Module): def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.SiLU, drop=0.): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features self.fc1 = nn.Linear(in_features, hidden_features) self.act = act_layer() self.fc2 = nn.Linear(hidden_features, out_features) self.drop = nn.Dropout(drop) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) x = self.drop(x) return x def window_partition(x, window_size): B, H, W, C = x.shape assert H % window_size == 0, 'feature map h and w can not divide by window size' x = x.view(B, H // window_size, window_size, W // window_size, window_size, C) windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C) return windows def window_reverse(windows, window_size, H, W): B = int(windows.shape[0] / (H * W / window_size / window_size)) x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1) x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1) return x class SwinTransformerLayer(nn.Module): def __init__(self, dim, num_heads, window_size=8, shift_size=0, mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0., drop_path=0., act_layer=nn.SiLU, norm_layer=nn.LayerNorm): super().__init__() self.dim = dim self.num_heads = num_heads self.window_size = window_size self.shift_size = shift_size self.mlp_ratio = mlp_ratio # if min(self.input_resolution) <= self.window_size: # # if window size is larger than input resolution, we don't partition windows # self.shift_size = 0 # self.window_size = min(self.input_resolution) assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size" self.norm1 = norm_layer(dim) self.attn = WindowAttention( dim, window_size=(self.window_size, self.window_size), num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop) self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() self.norm2 = norm_layer(dim) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop) def create_mask(self, H, W): # calculate attention mask for SW-MSA img_mask = torch.zeros((1, H, W, 1)) # 1 H W 1 h_slices = (slice(0, -self.window_size), slice(-self.window_size, -self.shift_size), slice(-self.shift_size, None)) w_slices = (slice(0, -self.window_size), slice(-self.window_size, -self.shift_size), slice(-self.shift_size, None)) cnt = 0 for h in h_slices: for w in w_slices: img_mask[:, h, w, :] = cnt cnt += 1 mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1 mask_windows = mask_windows.view(-1, self.window_size * self.window_size) attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2) attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0)) return attn_mask def forward(self, x): # reshape x[b c h w] to x[b l c] _, _, H_, W_ = x.shape Padding = False if min(H_, W_) < self.window_size or H_ % self.window_size!=0 or W_ % self.window_size!=0: Padding = True # print(f'img_size {min(H_, W_)} is less than (or not divided by) window_size {self.window_size}, Padding.') pad_r = (self.window_size - W_ % self.window_size) % self.window_size pad_b = (self.window_size - H_ % self.window_size) % self.window_size x = F.pad(x, (0, pad_r, 0, pad_b)) # print('2', x.shape) B, C, H, W = x.shape L = H * W x = x.permute(0, 2, 3, 1).contiguous().view(B, L, C) # b, L, c # create mask from init to forward if self.shift_size > 0: attn_mask = self.create_mask(H, W).to(x.device) else: attn_mask = None shortcut = x x = self.norm1(x) x = x.view(B, H, W, C) # cyclic shift if self.shift_size > 0: shifted_x = torch.roll(x, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2)) else: shifted_x = x # partition windows x_windows = window_partition(shifted_x, self.window_size) # nW*B, window_size, window_size, C x_windows = x_windows.view(-1, self.window_size * self.window_size, C) # nW*B, window_size*window_size, C # W-MSA/SW-MSA attn_windows = self.attn(x_windows, mask=attn_mask) # nW*B, window_size*window_size, C # merge windows attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C) shifted_x = window_reverse(attn_windows, self.window_size, H, W) # B H' W' C # reverse cyclic shift if self.shift_size > 0: x = torch.roll(shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2)) else: x = shifted_x x = x.view(B, H * W, C) # FFN x = shortcut + self.drop_path(x) x = x + self.drop_path(self.mlp(self.norm2(x))) x = x.permute(0, 2, 1).contiguous().view(-1, C, H, W) # b c h w if Padding: x = x[:, :, :H_, :W_] # reverse padding return x class SwinTransformerBlock(nn.Module): def __init__(self, c1, c2, num_heads, num_layers, window_size=8): super().__init__() self.conv = None if c1 != c2: self.conv = Conv(c1, c2) # remove input_resolution self.blocks = nn.Sequential(*[SwinTransformerLayer(dim=c2, num_heads=num_heads, window_size=window_size, shift_size=0 if (i % 2 == 0) else window_size // 2) for i in range(num_layers)]) def forward(self, x): if self.conv is not None: x = self.conv(x) x = self.blocks(x) return x class STCSPA(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(STCSPA, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformerBlock(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.m(self.cv1(x)) y2 = self.cv2(x) return self.cv3(torch.cat((y1, y2), dim=1)) class STCSPB(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(STCSPB, self).__init__() c_ = int(c2) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformerBlock(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x1 = self.cv1(x) y1 = self.m(x1) y2 = self.cv2(x1) return self.cv3(torch.cat((y1, y2), dim=1)) class STCSPC(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(STCSPC, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(c_, c_, 1, 1) self.cv4 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformerBlock(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(torch.cat((y1, y2), dim=1)) ##### end of swin transformer ##### ##### swin transformer v2 ##### class WindowAttention_v2(nn.Module): def __init__(self, dim, window_size, num_heads, qkv_bias=True, attn_drop=0., proj_drop=0., pretrained_window_size=[0, 0]): super().__init__() self.dim = dim self.window_size = window_size # Wh, Ww self.pretrained_window_size = pretrained_window_size self.num_heads = num_heads self.logit_scale = nn.Parameter(torch.log(10 * torch.ones((num_heads, 1, 1))), requires_grad=True) # mlp to generate continuous relative position bias self.cpb_mlp = nn.Sequential(nn.Linear(2, 512, bias=True), nn.ReLU(inplace=True), nn.Linear(512, num_heads, bias=False)) # get relative_coords_table relative_coords_h = torch.arange(-(self.window_size[0] - 1), self.window_size[0], dtype=torch.float32) relative_coords_w = torch.arange(-(self.window_size[1] - 1), self.window_size[1], dtype=torch.float32) relative_coords_table = torch.stack( torch.meshgrid([relative_coords_h, relative_coords_w])).permute(1, 2, 0).contiguous().unsqueeze(0) # 1, 2*Wh-1, 2*Ww-1, 2 if pretrained_window_size[0] > 0: relative_coords_table[:, :, :, 0] /= (pretrained_window_size[0] - 1) relative_coords_table[:, :, :, 1] /= (pretrained_window_size[1] - 1) else: relative_coords_table[:, :, :, 0] /= (self.window_size[0] - 1) relative_coords_table[:, :, :, 1] /= (self.window_size[1] - 1) relative_coords_table *= 8 # normalize to -8, 8 relative_coords_table = torch.sign(relative_coords_table) * torch.log2( torch.abs(relative_coords_table) + 1.0) / np.log2(8) self.register_buffer("relative_coords_table", relative_coords_table) # get pair-wise relative position index for each token inside the window coords_h = torch.arange(self.window_size[0]) coords_w = torch.arange(self.window_size[1]) coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2 relative_coords[:, :, 0] += self.window_size[0] - 1 # shift to start from 0 relative_coords[:, :, 1] += self.window_size[1] - 1 relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1 relative_position_index = relative_coords.sum(-1) # Wh*Ww, Wh*Ww self.register_buffer("relative_position_index", relative_position_index) self.qkv = nn.Linear(dim, dim * 3, bias=False) if qkv_bias: self.q_bias = nn.Parameter(torch.zeros(dim)) self.v_bias = nn.Parameter(torch.zeros(dim)) else: self.q_bias = None self.v_bias = None self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim) self.proj_drop = nn.Dropout(proj_drop) self.softmax = nn.Softmax(dim=-1) def forward(self, x, mask=None): B_, N, C = x.shape qkv_bias = None if self.q_bias is not None: qkv_bias = torch.cat((self.q_bias, torch.zeros_like(self.v_bias, requires_grad=False), self.v_bias)) qkv = F.linear(input=x, weight=self.qkv.weight, bias=qkv_bias) qkv = qkv.reshape(B_, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4) q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple) # cosine attention attn = (F.normalize(q, dim=-1) @ F.normalize(k, dim=-1).transpose(-2, -1)) logit_scale = torch.clamp(self.logit_scale, max=torch.log(torch.tensor(1. / 0.01))).exp() attn = attn * logit_scale relative_position_bias_table = self.cpb_mlp(self.relative_coords_table).view(-1, self.num_heads) relative_position_bias = relative_position_bias_table[self.relative_position_index.view(-1)].view( self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], -1) # Wh*Ww,Wh*Ww,nH relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww relative_position_bias = 16 * torch.sigmoid(relative_position_bias) attn = attn + relative_position_bias.unsqueeze(0) if mask is not None: nW = mask.shape[0] attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0) attn = attn.view(-1, self.num_heads, N, N) attn = self.softmax(attn) else: attn = self.softmax(attn) attn = self.attn_drop(attn) try: x = (attn @ v).transpose(1, 2).reshape(B_, N, C) except: x = (attn.half() @ v).transpose(1, 2).reshape(B_, N, C) x = self.proj(x) x = self.proj_drop(x) return x def extra_repr(self) -> str: return f'dim={self.dim}, window_size={self.window_size}, ' \ f'pretrained_window_size={self.pretrained_window_size}, num_heads={self.num_heads}' def flops(self, N): # calculate flops for 1 window with token length of N flops = 0 # qkv = self.qkv(x) flops += N * self.dim * 3 * self.dim # attn = (q @ k.transpose(-2, -1)) flops += self.num_heads * N * (self.dim // self.num_heads) * N # x = (attn @ v) flops += self.num_heads * N * N * (self.dim // self.num_heads) # x = self.proj(x) flops += N * self.dim * self.dim return flops class Mlp_v2(nn.Module): def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.SiLU, drop=0.): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features self.fc1 = nn.Linear(in_features, hidden_features) self.act = act_layer() self.fc2 = nn.Linear(hidden_features, out_features) self.drop = nn.Dropout(drop) def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) x = self.drop(x) return x def window_partition_v2(x, window_size): B, H, W, C = x.shape x = x.view(B, H // window_size, window_size, W // window_size, window_size, C) windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C) return windows def window_reverse_v2(windows, window_size, H, W): B = int(windows.shape[0] / (H * W / window_size / window_size)) x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1) x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1) return x class SwinTransformerLayer_v2(nn.Module): def __init__(self, dim, num_heads, window_size=7, shift_size=0, mlp_ratio=4., qkv_bias=True, drop=0., attn_drop=0., drop_path=0., act_layer=nn.SiLU, norm_layer=nn.LayerNorm, pretrained_window_size=0): super().__init__() self.dim = dim #self.input_resolution = input_resolution self.num_heads = num_heads self.window_size = window_size self.shift_size = shift_size self.mlp_ratio = mlp_ratio #if min(self.input_resolution) <= self.window_size: # # if window size is larger than input resolution, we don't partition windows # self.shift_size = 0 # self.window_size = min(self.input_resolution) assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size" self.norm1 = norm_layer(dim) self.attn = WindowAttention_v2( dim, window_size=(self.window_size, self.window_size), num_heads=num_heads, qkv_bias=qkv_bias, attn_drop=attn_drop, proj_drop=drop, pretrained_window_size=(pretrained_window_size, pretrained_window_size)) self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() self.norm2 = norm_layer(dim) mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = Mlp_v2(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop) def create_mask(self, H, W): # calculate attention mask for SW-MSA img_mask = torch.zeros((1, H, W, 1)) # 1 H W 1 h_slices = (slice(0, -self.window_size), slice(-self.window_size, -self.shift_size), slice(-self.shift_size, None)) w_slices = (slice(0, -self.window_size), slice(-self.window_size, -self.shift_size), slice(-self.shift_size, None)) cnt = 0 for h in h_slices: for w in w_slices: img_mask[:, h, w, :] = cnt cnt += 1 mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1 mask_windows = mask_windows.view(-1, self.window_size * self.window_size) attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2) attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0)) return attn_mask def forward(self, x): # reshape x[b c h w] to x[b l c] _, _, H_, W_ = x.shape Padding = False if min(H_, W_) < self.window_size or H_ % self.window_size!=0 or W_ % self.window_size!=0: Padding = True # print(f'img_size {min(H_, W_)} is less than (or not divided by) window_size {self.window_size}, Padding.') pad_r = (self.window_size - W_ % self.window_size) % self.window_size pad_b = (self.window_size - H_ % self.window_size) % self.window_size x = F.pad(x, (0, pad_r, 0, pad_b)) # print('2', x.shape) B, C, H, W = x.shape L = H * W x = x.permute(0, 2, 3, 1).contiguous().view(B, L, C) # b, L, c # create mask from init to forward if self.shift_size > 0: attn_mask = self.create_mask(H, W).to(x.device) else: attn_mask = None shortcut = x x = x.view(B, H, W, C) # cyclic shift if self.shift_size > 0: shifted_x = torch.roll(x, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2)) else: shifted_x = x # partition windows x_windows = window_partition_v2(shifted_x, self.window_size) # nW*B, window_size, window_size, C x_windows = x_windows.view(-1, self.window_size * self.window_size, C) # nW*B, window_size*window_size, C # W-MSA/SW-MSA attn_windows = self.attn(x_windows, mask=attn_mask) # nW*B, window_size*window_size, C # merge windows attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C) shifted_x = window_reverse_v2(attn_windows, self.window_size, H, W) # B H' W' C # reverse cyclic shift if self.shift_size > 0: x = torch.roll(shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2)) else: x = shifted_x x = x.view(B, H * W, C) x = shortcut + self.drop_path(self.norm1(x)) # FFN x = x + self.drop_path(self.norm2(self.mlp(x))) x = x.permute(0, 2, 1).contiguous().view(-1, C, H, W) # b c h w if Padding: x = x[:, :, :H_, :W_] # reverse padding return x def extra_repr(self) -> str: return f"dim={self.dim}, input_resolution={self.input_resolution}, num_heads={self.num_heads}, " \ f"window_size={self.window_size}, shift_size={self.shift_size}, mlp_ratio={self.mlp_ratio}" def flops(self): flops = 0 H, W = self.input_resolution # norm1 flops += self.dim * H * W # W-MSA/SW-MSA nW = H * W / self.window_size / self.window_size flops += nW * self.attn.flops(self.window_size * self.window_size) # mlp flops += 2 * H * W * self.dim * self.dim * self.mlp_ratio # norm2 flops += self.dim * H * W return flops class SwinTransformer2Block(nn.Module): def __init__(self, c1, c2, num_heads, num_layers, window_size=7): super().__init__() self.conv = None if c1 != c2: self.conv = Conv(c1, c2) # remove input_resolution self.blocks = nn.Sequential(*[SwinTransformerLayer_v2(dim=c2, num_heads=num_heads, window_size=window_size, shift_size=0 if (i % 2 == 0) else window_size // 2) for i in range(num_layers)]) def forward(self, x): if self.conv is not None: x = self.conv(x) x = self.blocks(x) return x class ST2CSPA(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(ST2CSPA, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformer2Block(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.m(self.cv1(x)) y2 = self.cv2(x) return self.cv3(torch.cat((y1, y2), dim=1)) class ST2CSPB(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(ST2CSPB, self).__init__() c_ = int(c2) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c_, c_, 1, 1) self.cv3 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformer2Block(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): x1 = self.cv1(x) y1 = self.m(x1) y2 = self.cv2(x1) return self.cv3(torch.cat((y1, y2), dim=1)) class ST2CSPC(nn.Module): # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion super(ST2CSPC, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, 1, 1) self.cv2 = Conv(c1, c_, 1, 1) self.cv3 = Conv(c_, c_, 1, 1) self.cv4 = Conv(2 * c_, c2, 1, 1) num_heads = c_ // 32 self.m = SwinTransformer2Block(c_, c_, num_heads, n) #self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) def forward(self, x): y1 = self.cv3(self.m(self.cv1(x))) y2 = self.cv2(x) return self.cv4(torch.cat((y1, y2), dim=1)) ##### end of swin transformer v2 ##### ================================================ FILE: asone/pose_estimators/yolov7_pose/models/experimental.py ================================================ import numpy as np import random import torch import torch.nn as nn from asone.pose_estimators.yolov7_pose.models.common import Conv, DWConv from asone.pose_estimators.yolov7_pose.utils.google_utils import attempt_download from asone.pose_estimators.yolov7_pose.utils.yolov7_pose_utils import yolov7_in_syspath class CrossConv(nn.Module): # Cross Convolution Downsample def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False): # ch_in, ch_out, kernel, stride, groups, expansion, shortcut super(CrossConv, self).__init__() c_ = int(c2 * e) # hidden channels self.cv1 = Conv(c1, c_, (1, k), (1, s)) self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g) self.add = shortcut and c1 == c2 def forward(self, x): return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) class Sum(nn.Module): # Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 def __init__(self, n, weight=False): # n: number of inputs super(Sum, self).__init__() self.weight = weight # apply weights boolean self.iter = range(n - 1) # iter object if weight: self.w = nn.Parameter(-torch.arange(1., n) / 2, requires_grad=True) # layer weights def forward(self, x): y = x[0] # no weight if self.weight: w = torch.sigmoid(self.w) * 2 for i in self.iter: y = y + x[i + 1] * w[i] else: for i in self.iter: y = y + x[i + 1] return y class MixConv2d(nn.Module): # Mixed Depthwise Conv https://arxiv.org/abs/1907.09595 def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): super(MixConv2d, self).__init__() groups = len(k) if equal_ch: # equal c_ per group i = torch.linspace(0, groups - 1E-6, c2).floor() # c2 indices c_ = [(i == g).sum() for g in range(groups)] # intermediate channels else: # equal weight.numel() per group b = [c2] + [0] * groups a = np.eye(groups + 1, groups, k=-1) a -= np.roll(a, 1, axis=1) a *= np.array(k) ** 2 a[0] = 1 c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b self.m = nn.ModuleList([nn.Conv2d(c1, int(c_[g]), k[g], s, k[g] // 2, bias=False) for g in range(groups)]) self.bn = nn.BatchNorm2d(c2) self.act = nn.LeakyReLU(0.1, inplace=True) def forward(self, x): return x + self.act(self.bn(torch.cat([m(x) for m in self.m], 1))) class Ensemble(nn.ModuleList): # Ensemble of models def __init__(self): super(Ensemble, self).__init__() def forward(self, x, augment=False): y = [] for module in self: y.append(module(x, augment)[0]) # y = torch.stack(y).max(0)[0] # max ensemble # y = torch.stack(y).mean(0) # mean ensemble y = torch.cat(y, 1) # nms ensemble return y, None # inference, train output class ORT_NMS(torch.autograd.Function): '''ONNX-Runtime NMS operation''' @staticmethod def forward(ctx, boxes, scores, max_output_boxes_per_class=torch.tensor([100]), iou_threshold=torch.tensor([0.45]), score_threshold=torch.tensor([0.25])): device = boxes.device batch = scores.shape[0] num_det = random.randint(0, 100) batches = torch.randint(0, batch, (num_det,)).sort()[0].to(device) idxs = torch.arange(100, 100 + num_det).to(device) zeros = torch.zeros((num_det,), dtype=torch.int64).to(device) selected_indices = torch.cat([batches[None], zeros[None], idxs[None]], 0).T.contiguous() selected_indices = selected_indices.to(torch.int64) return selected_indices @staticmethod def symbolic(g, boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold): return g.op("NonMaxSuppression", boxes, scores, max_output_boxes_per_class, iou_threshold, score_threshold) class TRT_NMS(torch.autograd.Function): '''TensorRT NMS operation''' @staticmethod def forward( ctx, boxes, scores, background_class=-1, box_coding=1, iou_threshold=0.45, max_output_boxes=100, plugin_version="1", score_activation=0, score_threshold=0.25, ): batch_size, num_boxes, num_classes = scores.shape num_det = torch.randint(0, max_output_boxes, (batch_size, 1), dtype=torch.int32) det_boxes = torch.randn(batch_size, max_output_boxes, 4) det_scores = torch.randn(batch_size, max_output_boxes) det_classes = torch.randint(0, num_classes, (batch_size, max_output_boxes), dtype=torch.int32) return num_det, det_boxes, det_scores, det_classes @staticmethod def symbolic(g, boxes, scores, background_class=-1, box_coding=1, iou_threshold=0.45, max_output_boxes=100, plugin_version="1", score_activation=0, score_threshold=0.25): out = g.op("TRT::EfficientNMS_TRT", boxes, scores, background_class_i=background_class, box_coding_i=box_coding, iou_threshold_f=iou_threshold, max_output_boxes_i=max_output_boxes, plugin_version_s=plugin_version, score_activation_i=score_activation, score_threshold_f=score_threshold, outputs=4) nums, boxes, scores, classes = out return nums, boxes, scores, classes class ONNX_ORT(nn.Module): '''onnx module with ONNX-Runtime NMS operation.''' def __init__(self, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=640, device=None): super().__init__() self.device = device if device else torch.device("cpu") self.max_obj = torch.tensor([max_obj]).to(device) self.iou_threshold = torch.tensor([iou_thres]).to(device) self.score_threshold = torch.tensor([score_thres]).to(device) self.max_wh = max_wh # if max_wh != 0 : non-agnostic else : agnostic self.convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]], dtype=torch.float32, device=self.device) def forward(self, x): boxes = x[:, :, :4] conf = x[:, :, 4:5] scores = x[:, :, 5:] scores *= conf boxes @= self.convert_matrix max_score, category_id = scores.max(2, keepdim=True) dis = category_id.float() * self.max_wh nmsbox = boxes + dis max_score_tp = max_score.transpose(1, 2).contiguous() selected_indices = ORT_NMS.apply(nmsbox, max_score_tp, self.max_obj, self.iou_threshold, self.score_threshold) X, Y = selected_indices[:, 0], selected_indices[:, 2] selected_boxes = boxes[X, Y, :] selected_categories = category_id[X, Y, :].float() selected_scores = max_score[X, Y, :] X = X.unsqueeze(1).float() return torch.cat([X, selected_boxes, selected_categories, selected_scores], 1) class ONNX_TRT(nn.Module): '''onnx module with TensorRT NMS operation.''' def __init__(self, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=None ,device=None): super().__init__() assert max_wh is None self.device = device if device else torch.device('cpu') self.background_class = -1, self.box_coding = 1, self.iou_threshold = iou_thres self.max_obj = max_obj self.plugin_version = '1' self.score_activation = 0 self.score_threshold = score_thres def forward(self, x): boxes = x[:, :, :4] conf = x[:, :, 4:5] scores = x[:, :, 5:] scores *= conf num_det, det_boxes, det_scores, det_classes = TRT_NMS.apply(boxes, scores, self.background_class, self.box_coding, self.iou_threshold, self.max_obj, self.plugin_version, self.score_activation, self.score_threshold) return num_det, det_boxes, det_scores, det_classes class End2End(nn.Module): '''export onnx or tensorrt model with NMS operation.''' def __init__(self, model, max_obj=100, iou_thres=0.45, score_thres=0.25, max_wh=None, device=None): super().__init__() device = device if device else torch.device('cpu') assert isinstance(max_wh,(int)) or max_wh is None self.model = model.to(device) self.model.model[-1].end2end = True self.patch_model = ONNX_TRT if max_wh is None else ONNX_ORT self.end2end = self.patch_model(max_obj, iou_thres, score_thres, max_wh, device) self.end2end.eval() def forward(self, x): x = self.model(x) x = self.end2end(x) return x def attempt_load(weights, map_location=None): # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a model = Ensemble() for w in weights if isinstance(weights, list) else [weights]: attempt_download(w) with yolov7_in_syspath(): ckpt = torch.load(w, map_location=map_location) # load model.append(ckpt['ema' if ckpt.get('ema') else 'model'].float().fuse().eval()) # FP32 model # Compatibility updates for m in model.modules(): if type(m) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU]: m.inplace = True # pytorch 1.7.0 compatibility elif type(m) is nn.Upsample: m.recompute_scale_factor = None # torch 1.11.0 compatibility elif type(m) is Conv: m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility if len(model) == 1: return model[-1] # return model else: print('Ensemble created with %s\n' % weights) for k in ['names', 'stride']: setattr(model, k, getattr(model[-1], k)) return model # return ensemble ================================================ FILE: asone/pose_estimators/yolov7_pose/models/yolo.py ================================================ import argparse import logging import sys from copy import deepcopy # sys.path.append('./') # to run '$ python *.py' files in subdirectories logger = logging.getLogger(__name__) import torch from asone.pose_estimators.yolov7_pose.models.common import * from asone.pose_estimators.yolov7_pose.models.experimental import * from asone.pose_estimators.yolov7_pose.utils.autoanchor import check_anchor_order from asone.pose_estimators.yolov7_pose.utils.general import make_divisible, check_file, set_logging from asone.pose_estimators.yolov7_pose.utils.torch_utils import time_synchronized, fuse_conv_and_bn, model_info, scale_img, initialize_weights, \ select_device, copy_attr from asone.pose_estimators.yolov7_pose.utils.loss import SigmoidBin try: import thop # for FLOPS computation except ImportError: thop = None class Detect(nn.Module): stride = None # strides computed during build export = False # onnx export end2end = False include_nms = False def __init__(self, nc=80, anchors=(), ch=()): # detection layer super(Detect, self).__init__() self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer('anchors', a) # shape(nl,na,2) self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv def forward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](x[i]) # conv bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() if not torch.onnx.is_in_onnx_export(): y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh else: xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh y = torch.cat((xy, wh, y[..., 4:]), -1) z.append(y.view(bs, -1, self.no)) if self.training: out = x elif self.end2end: out = torch.cat(z, 1) elif self.include_nms: z = self.convert(z) out = (z, ) else: out = (torch.cat(z, 1), x) return out @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() def convert(self, z): z = torch.cat(z, 1) box = z[:, :, :4] conf = z[:, :, 4:5] score = z[:, :, 5:] score *= conf convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]], dtype=torch.float32, device=z.device) box @= convert_matrix return (box, score) class IDetect(nn.Module): stride = None # strides computed during build export = False # onnx export end2end = False include_nms = False def __init__(self, nc=80, anchors=(), ch=()): # detection layer super(IDetect, self).__init__() self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer('anchors', a) # shape(nl,na,2) self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv self.ia = nn.ModuleList(ImplicitA(x) for x in ch) self.im = nn.ModuleList(ImplicitM(self.no * self.na) for _ in ch) def forward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](self.ia[i](x[i])) # conv x[i] = self.im[i](x[i]) bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh z.append(y.view(bs, -1, self.no)) return x if self.training else (torch.cat(z, 1), x) def fuseforward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](x[i]) # conv bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh z.append(y.view(bs, -1, self.no)) if self.training: out = x elif self.end2end: out = torch.cat(z, 1) elif self.include_nms: z = self.convert(z) out = (z, ) else: out = (torch.cat(z, 1), x) return out def fuse(self): print("IDetect.fuse") # fuse ImplicitA and Convolution for i in range(len(self.m)): c1,c2,_,_ = self.m[i].weight.shape c1_,c2_, _,_ = self.ia[i].implicit.shape self.m[i].bias += torch.matmul(self.m[i].weight.reshape(c1,c2),self.ia[i].implicit.reshape(c2_,c1_)).squeeze(1) # fuse ImplicitM and Convolution for i in range(len(self.m)): c1,c2, _,_ = self.im[i].implicit.shape self.m[i].bias *= self.im[i].implicit.reshape(c2) self.m[i].weight *= self.im[i].implicit.transpose(0,1) @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() def convert(self, z): z = torch.cat(z, 1) box = z[:, :, :4] conf = z[:, :, 4:5] score = z[:, :, 5:] score *= conf convert_matrix = torch.tensor([[1, 0, 1, 0], [0, 1, 0, 1], [-0.5, 0, 0.5, 0], [0, -0.5, 0, 0.5]], dtype=torch.float32, device=z.device) box @= convert_matrix return (box, score) class IKeypoint(nn.Module): stride = None # strides computed during build export = False # onnx export def __init__(self, nc=80, anchors=(), nkpt=17, ch=(), inplace=True, dw_conv_kpt=False): # detection layer super(IKeypoint, self).__init__() self.nc = nc # number of classes self.nkpt = nkpt self.dw_conv_kpt = dw_conv_kpt self.no_det=(nc + 5) # number of outputs per anchor for box and class self.no_kpt = 3*self.nkpt ## number of outputs per anchor for keypoints self.no = self.no_det+self.no_kpt self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid self.flip_test = False a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer('anchors', a) # shape(nl,na,2) self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no_det * self.na, 1) for x in ch) # output conv self.ia = nn.ModuleList(ImplicitA(x) for x in ch) self.im = nn.ModuleList(ImplicitM(self.no_det * self.na) for _ in ch) if self.nkpt is not None: if self.dw_conv_kpt: #keypoint head is slightly more complex self.m_kpt = nn.ModuleList( nn.Sequential(DWConv(x, x, k=3), Conv(x,x), DWConv(x, x, k=3), Conv(x, x), DWConv(x, x, k=3), Conv(x,x), DWConv(x, x, k=3), Conv(x, x), DWConv(x, x, k=3), Conv(x, x), DWConv(x, x, k=3), nn.Conv2d(x, self.no_kpt * self.na, 1)) for x in ch) else: #keypoint head is a single convolution self.m_kpt = nn.ModuleList(nn.Conv2d(x, self.no_kpt * self.na, 1) for x in ch) self.inplace = inplace # use in-place ops (e.g. slice assignment) def forward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): if self.nkpt is None or self.nkpt==0: x[i] = self.im[i](self.m[i](self.ia[i](x[i]))) # conv else : x[i] = torch.cat((self.im[i](self.m[i](self.ia[i](x[i]))), self.m_kpt[i](x[i])), axis=1) bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() x_det = x[i][..., :6] x_kpt = x[i][..., 6:] if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) kpt_grid_x = self.grid[i][..., 0:1] kpt_grid_y = self.grid[i][..., 1:2] if self.nkpt == 0: y = x[i].sigmoid() else: y = x_det.sigmoid() if self.inplace: xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i].view(1, self.na, 1, 1, 2) # wh if self.nkpt != 0: x_kpt[..., 0::3] = (x_kpt[..., ::3] * 2. - 0.5 + kpt_grid_x.repeat(1,1,1,1,17)) * self.stride[i] # xy x_kpt[..., 1::3] = (x_kpt[..., 1::3] * 2. - 0.5 + kpt_grid_y.repeat(1,1,1,1,17)) * self.stride[i] # xy #x_kpt[..., 0::3] = (x_kpt[..., ::3] + kpt_grid_x.repeat(1,1,1,1,17)) * self.stride[i] # xy #x_kpt[..., 1::3] = (x_kpt[..., 1::3] + kpt_grid_y.repeat(1,1,1,1,17)) * self.stride[i] # xy #print('=============') #print(self.anchor_grid[i].shape) #print(self.anchor_grid[i][...,0].unsqueeze(4).shape) #print(x_kpt[..., 0::3].shape) #x_kpt[..., 0::3] = ((x_kpt[..., 0::3].tanh() * 2.) ** 3 * self.anchor_grid[i][...,0].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_x.repeat(1,1,1,1,17) * self.stride[i] # xy #x_kpt[..., 1::3] = ((x_kpt[..., 1::3].tanh() * 2.) ** 3 * self.anchor_grid[i][...,1].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_y.repeat(1,1,1,1,17) * self.stride[i] # xy #x_kpt[..., 0::3] = (((x_kpt[..., 0::3].sigmoid() * 4.) ** 2 - 8.) * self.anchor_grid[i][...,0].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_x.repeat(1,1,1,1,17) * self.stride[i] # xy #x_kpt[..., 1::3] = (((x_kpt[..., 1::3].sigmoid() * 4.) ** 2 - 8.) * self.anchor_grid[i][...,1].unsqueeze(4).repeat(1,1,1,1,self.nkpt)) + kpt_grid_y.repeat(1,1,1,1,17) * self.stride[i] # xy x_kpt[..., 2::3] = x_kpt[..., 2::3].sigmoid() y = torch.cat((xy, wh, y[..., 4:], x_kpt), dim = -1) else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953 xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh if self.nkpt != 0: y[..., 6:] = (y[..., 6:] * 2. - 0.5 + self.grid[i].repeat((1,1,1,1,self.nkpt))) * self.stride[i] # xy y = torch.cat((xy, wh, y[..., 4:]), -1) z.append(y.view(bs, -1, self.no)) return x if self.training else (torch.cat(z, 1), x) @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() class IAuxDetect(nn.Module): stride = None # strides computed during build export = False # onnx export def __init__(self, nc=80, anchors=(), ch=()): # detection layer super(IAuxDetect, self).__init__() self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer('anchors', a) # shape(nl,na,2) self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch[:self.nl]) # output conv self.m2 = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch[self.nl:]) # output conv self.ia = nn.ModuleList(ImplicitA(x) for x in ch[:self.nl]) self.im = nn.ModuleList(ImplicitM(self.no * self.na) for _ in ch[:self.nl]) def forward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](self.ia[i](x[i])) # conv x[i] = self.im[i](x[i]) bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() x[i+self.nl] = self.m2[i](x[i+self.nl]) x[i+self.nl] = x[i+self.nl].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh z.append(y.view(bs, -1, self.no)) return x if self.training else (torch.cat(z, 1), x[:self.nl]) @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() class IBin(nn.Module): stride = None # strides computed during build export = False # onnx export def __init__(self, nc=80, anchors=(), ch=(), bin_count=21): # detection layer super(IBin, self).__init__() self.nc = nc # number of classes self.bin_count = bin_count self.w_bin_sigmoid = SigmoidBin(bin_count=self.bin_count, min=0.0, max=4.0) self.h_bin_sigmoid = SigmoidBin(bin_count=self.bin_count, min=0.0, max=4.0) # classes, x,y,obj self.no = nc + 3 + \ self.w_bin_sigmoid.get_length() + self.h_bin_sigmoid.get_length() # w-bce, h-bce # + self.x_bin_sigmoid.get_length() + self.y_bin_sigmoid.get_length() self.nl = len(anchors) # number of detection layers self.na = len(anchors[0]) // 2 # number of anchors self.grid = [torch.zeros(1)] * self.nl # init grid a = torch.tensor(anchors).float().view(self.nl, -1, 2) self.register_buffer('anchors', a) # shape(nl,na,2) self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv self.ia = nn.ModuleList(ImplicitA(x) for x in ch) self.im = nn.ModuleList(ImplicitM(self.no * self.na) for _ in ch) def forward(self, x): #self.x_bin_sigmoid.use_fw_regression = True #self.y_bin_sigmoid.use_fw_regression = True self.w_bin_sigmoid.use_fw_regression = True self.h_bin_sigmoid.use_fw_regression = True # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](self.ia[i](x[i])) # conv x[i] = self.im[i](x[i]) bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() if not self.training: # inference if self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i] = self._make_grid(nx, ny).to(x[i].device) y = x[i].sigmoid() y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy #y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh #px = (self.x_bin_sigmoid.forward(y[..., 0:12]) + self.grid[i][..., 0]) * self.stride[i] #py = (self.y_bin_sigmoid.forward(y[..., 12:24]) + self.grid[i][..., 1]) * self.stride[i] pw = self.w_bin_sigmoid.forward(y[..., 2:24]) * self.anchor_grid[i][..., 0] ph = self.h_bin_sigmoid.forward(y[..., 24:46]) * self.anchor_grid[i][..., 1] #y[..., 0] = px #y[..., 1] = py y[..., 2] = pw y[..., 3] = ph y = torch.cat((y[..., 0:4], y[..., 46:]), dim=-1) z.append(y.view(bs, -1, y.shape[-1])) return x if self.training else (torch.cat(z, 1), x) @staticmethod def _make_grid(nx=20, ny=20): yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() class Model(nn.Module): def __init__(self, cfg='yolor-csp-c.yaml', ch=3, nc=None, anchors=None): # model, input channels, number of classes super(Model, self).__init__() self.traced = False if isinstance(cfg, dict): self.yaml = cfg # model dict else: # is *.yaml import yaml # for torch hub self.yaml_file = Path(cfg).name with open(cfg) as f: self.yaml = yaml.load(f, Loader=yaml.SafeLoader) # model dict # Define model ch = self.yaml['ch'] = self.yaml.get('ch', ch) # input channels if nc and nc != self.yaml['nc']: logger.info(f"Overriding model.yaml nc={self.yaml['nc']} with nc={nc}") self.yaml['nc'] = nc # override yaml value if anchors: logger.info(f'Overriding model.yaml anchors with anchors={anchors}') self.yaml['anchors'] = round(anchors) # override yaml value self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist self.names = [str(i) for i in range(self.yaml['nc'])] # default names # print([x.shape for x in self.forward(torch.zeros(1, ch, 64, 64))]) # Build strides, anchors m = self.model[-1] # Detect() if isinstance(m, Detect): s = 256 # 2x min stride m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward m.anchors /= m.stride.view(-1, 1, 1) check_anchor_order(m) self.stride = m.stride self._initialize_biases() # only run once # print('Strides: %s' % m.stride.tolist()) if isinstance(m, IDetect): s = 256 # 2x min stride m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward m.anchors /= m.stride.view(-1, 1, 1) check_anchor_order(m) self.stride = m.stride self._initialize_biases() # only run once # print('Strides: %s' % m.stride.tolist()) if isinstance(m, IAuxDetect): s = 256 # 2x min stride m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))[:4]]) # forward #print(m.stride) m.anchors /= m.stride.view(-1, 1, 1) check_anchor_order(m) self.stride = m.stride self._initialize_aux_biases() # only run once # print('Strides: %s' % m.stride.tolist()) if isinstance(m, IBin): s = 256 # 2x min stride m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward m.anchors /= m.stride.view(-1, 1, 1) check_anchor_order(m) self.stride = m.stride self._initialize_biases_bin() # only run once # print('Strides: %s' % m.stride.tolist()) if isinstance(m, IKeypoint): s = 256 # 2x min stride m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward m.anchors /= m.stride.view(-1, 1, 1) check_anchor_order(m) self.stride = m.stride self._initialize_biases_kpt() # only run once # print('Strides: %s' % m.stride.tolist()) # Init weights, biases initialize_weights(self) self.info() logger.info('') def forward(self, x, augment=False, profile=False): if augment: img_size = x.shape[-2:] # height, width s = [1, 0.83, 0.67] # scales f = [None, 3, None] # flips (2-ud, 3-lr) y = [] # outputs for si, fi in zip(s, f): xi = scale_img(x.flip(fi) if fi else x, si, gs=int(self.stride.max())) yi = self.forward_once(xi)[0] # forward # cv2.imwrite(f'img_{si}.jpg', 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1]) # save yi[..., :4] /= si # de-scale if fi == 2: yi[..., 1] = img_size[0] - yi[..., 1] # de-flip ud elif fi == 3: yi[..., 0] = img_size[1] - yi[..., 0] # de-flip lr y.append(yi) return torch.cat(y, 1), None # augmented inference, train else: return self.forward_once(x, profile) # single-scale inference, train def forward_once(self, x, profile=False): y, dt = [], [] # outputs for m in self.model: if m.f != -1: # if not from previous layer x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers if not hasattr(self, 'traced'): self.traced=False if self.traced: if isinstance(m, Detect) or isinstance(m, IDetect) or isinstance(m, IAuxDetect) or isinstance(m, IKeypoint): break if profile: c = isinstance(m, (Detect, IDetect, IAuxDetect, IBin)) o = thop.profile(m, inputs=(x.copy() if c else x,), verbose=False)[0] / 1E9 * 2 if thop else 0 # FLOPS for _ in range(10): m(x.copy() if c else x) t = time_synchronized() for _ in range(10): m(x.copy() if c else x) dt.append((time_synchronized() - t) * 100) print('%10.1f%10.0f%10.1fms %-40s' % (o, m.np, dt[-1], m.type)) x = m(x) # run y.append(x if m.i in self.save else None) # save output if profile: print('%.1fms total' % sum(dt)) return x def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency # https://arxiv.org/abs/1708.02002 section 3.3 # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. m = self.model[-1] # Detect() module for mi, s in zip(m.m, m.stride): # from b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) b.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image) b.data[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) def _initialize_aux_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency # https://arxiv.org/abs/1708.02002 section 3.3 # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. m = self.model[-1] # Detect() module for mi, mi2, s in zip(m.m, m.m2, m.stride): # from b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) b.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image) b.data[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) b2 = mi2.bias.view(m.na, -1) # conv.bias(255) to (3,85) b2.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image) b2.data[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls mi2.bias = torch.nn.Parameter(b2.view(-1), requires_grad=True) def _initialize_biases_bin(self, cf=None): # initialize biases into Detect(), cf is class frequency # https://arxiv.org/abs/1708.02002 section 3.3 # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. m = self.model[-1] # Bin() module bc = m.bin_count for mi, s in zip(m.m, m.stride): # from b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) old = b[:, (0,1,2,bc+3)].data obj_idx = 2*bc+4 b[:, :obj_idx].data += math.log(0.6 / (bc + 1 - 0.99)) b[:, obj_idx].data += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image) b[:, (obj_idx+1):].data += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls b[:, (0,1,2,bc+3)].data = old mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) def _initialize_biases_kpt(self, cf=None): # initialize biases into Detect(), cf is class frequency # https://arxiv.org/abs/1708.02002 section 3.3 # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. m = self.model[-1] # Detect() module for mi, s in zip(m.m, m.stride): # from b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) b.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image) b.data[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) def _print_biases(self): m = self.model[-1] # Detect() module for mi in m.m: # from b = mi.bias.detach().view(m.na, -1).T # conv.bias(255) to (3,85) print(('%6g Conv2d.bias:' + '%10.3g' * 6) % (mi.weight.shape[1], *b[:5].mean(1).tolist(), b[5:].mean())) # def _print_weights(self): # for m in self.model.modules(): # if type(m) is Bottleneck: # print('%10.3g' % (m.w.detach().sigmoid() * 2)) # shortcut weights def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers print('Fusing layers... ') for m in self.model.modules(): if isinstance(m, RepConv): #print(f" fuse_repvgg_block") m.fuse_repvgg_block() elif isinstance(m, RepConv_OREPA): #print(f" switch_to_deploy") m.switch_to_deploy() elif type(m) is Conv and hasattr(m, 'bn'): m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv delattr(m, 'bn') # remove batchnorm m.forward = m.fuseforward # update forward elif isinstance(m, IDetect): m.fuse() m.forward = m.fuseforward self.info() return self def nms(self, mode=True): # add or remove NMS module present = type(self.model[-1]) is NMS # last layer is NMS if mode and not present: print('Adding NMS... ') m = NMS() # module m.f = -1 # from m.i = self.model[-1].i + 1 # index self.model.add_module(name='%s' % m.i, module=m) # add self.eval() elif not mode and present: print('Removing NMS... ') self.model = self.model[:-1] # remove return self def autoshape(self): # add autoShape module print('Adding autoShape... ') m = autoShape(self) # wrap model copy_attr(m, self, include=('yaml', 'nc', 'hyp', 'names', 'stride'), exclude=()) # copy attributes return m def info(self, verbose=False, img_size=640): # print model information model_info(self, verbose, img_size) def parse_model(d, ch): # model_dict, input_channels(3) logger.info('\n%3s%18s%3s%10s %-40s%-30s' % ('', 'from', 'n', 'params', 'module', 'arguments')) anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple'] na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors no = na * (nc + 5) # number of outputs = anchors * (classes + 5) layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number, module, args m = eval(m) if isinstance(m, str) else m # eval strings for j, a in enumerate(args): try: args[j] = eval(a) if isinstance(a, str) else a # eval strings except: pass n = max(round(n * gd), 1) if n > 1 else n # depth gain if m in [nn.Conv2d, Conv, RobustConv, RobustConv2, DWConv, GhostConv, RepConv, RepConv_OREPA, DownC, SPP, SPPF, SPPCSPC, GhostSPPCSPC, MixConv2d, Focus, Stem, GhostStem, CrossConv, Bottleneck, BottleneckCSPA, BottleneckCSPB, BottleneckCSPC, RepBottleneck, RepBottleneckCSPA, RepBottleneckCSPB, RepBottleneckCSPC, Res, ResCSPA, ResCSPB, ResCSPC, RepRes, RepResCSPA, RepResCSPB, RepResCSPC, ResX, ResXCSPA, ResXCSPB, ResXCSPC, RepResX, RepResXCSPA, RepResXCSPB, RepResXCSPC, Ghost, GhostCSPA, GhostCSPB, GhostCSPC, SwinTransformerBlock, STCSPA, STCSPB, STCSPC, SwinTransformer2Block, ST2CSPA, ST2CSPB, ST2CSPC]: c1, c2 = ch[f], args[0] if c2 != no: # if not output c2 = make_divisible(c2 * gw, 8) args = [c1, c2, *args[1:]] if m in [DownC, SPPCSPC, GhostSPPCSPC, BottleneckCSPA, BottleneckCSPB, BottleneckCSPC, RepBottleneckCSPA, RepBottleneckCSPB, RepBottleneckCSPC, ResCSPA, ResCSPB, ResCSPC, RepResCSPA, RepResCSPB, RepResCSPC, ResXCSPA, ResXCSPB, ResXCSPC, RepResXCSPA, RepResXCSPB, RepResXCSPC, GhostCSPA, GhostCSPB, GhostCSPC, STCSPA, STCSPB, STCSPC, ST2CSPA, ST2CSPB, ST2CSPC]: args.insert(2, n) # number of repeats n = 1 elif m is nn.BatchNorm2d: args = [ch[f]] elif m is Concat: c2 = sum([ch[x] for x in f]) elif m is Chuncat: c2 = sum([ch[x] for x in f]) elif m is Shortcut: c2 = ch[f[0]] elif m is Foldcut: c2 = ch[f] // 2 elif m in [Detect, IDetect, IAuxDetect, IBin, IKeypoint]: args.append([ch[x] for x in f]) if isinstance(args[1], int): # number of anchors args[1] = [list(range(args[1] * 2))] * len(f) elif m is ReOrg: c2 = ch[f] * 4 elif m is Contract: c2 = ch[f] * args[0] ** 2 elif m is Expand: c2 = ch[f] // args[0] ** 2 else: c2 = ch[f] m_ = nn.Sequential(*[m(*args) for _ in range(n)]) if n > 1 else m(*args) # module t = str(m)[8:-2].replace('__main__.', '') # module type np = sum([x.numel() for x in m_.parameters()]) # number params m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params logger.info('%3s%18s%3s%10.0f %-40s%-30s' % (i, f, n, np, t, args)) # print save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist layers.append(m_) if i == 0: ch = [] ch.append(c2) return nn.Sequential(*layers), sorted(save) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--cfg', type=str, default='yolor-csp-c.yaml', help='model.yaml') parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') parser.add_argument('--profile', action='store_true', help='profile model speed') opt = parser.parse_args() opt.cfg = check_file(opt.cfg) # check file set_logging() device = select_device(opt.device) # Create model model = Model(opt.cfg).to(device) model.train() if opt.profile: img = torch.rand(1, 3, 640, 640).to(device) y = model(img, profile=True) # Profile # img = torch.rand(8 if torch.cuda.is_available() else 1, 3, 640, 640).to(device) # y = model(img, profile=True) # Tensorboard # from torch.utils.tensorboard import SummaryWriter # tb_writer = SummaryWriter() # print("Run 'tensorboard --logdir=models/runs' to view tensorboard at http://localhost:6006/") # tb_writer.add_graph(model.model, img) # add model to tensorboard # tb_writer.add_image('test', img[0], dataformats='CWH') # add model to tensorboard ================================================ FILE: asone/pose_estimators/yolov7_pose/requirements.txt ================================================ # Usage: pip install -r requirements.txt # Base ---------------------------------------- matplotlib>=3.2.2 numpy>=1.18.5 opencv-python>=4.1.1 Pillow>=7.1.2 PyYAML>=5.3.1 requests>=2.23.0 scipy>=1.4.1 torch>=1.7.0,!=1.12.0 torchvision>=0.8.1,!=0.13.0 tqdm>=4.41.0 protobuf<4.21.3 # Logging ------------------------------------- tensorboard>=2.4.1 # wandb # Plotting ------------------------------------ pandas>=1.1.4 seaborn>=0.11.0 # Extras -------------------------------------- ipython # interactive notebook psutil # system utilization thop # FLOPs computation ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/__init__.py ================================================ # init ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/activations.py ================================================ # Activation functions import torch import torch.nn as nn import torch.nn.functional as F # SiLU https://arxiv.org/pdf/1606.08415.pdf ---------------------------------------------------------------------------- class SiLU(nn.Module): # export-friendly version of nn.SiLU() @staticmethod def forward(x): return x * torch.sigmoid(x) class Hardswish(nn.Module): # export-friendly version of nn.Hardswish() @staticmethod def forward(x): # return x * F.hardsigmoid(x) # for torchscript and CoreML return x * F.hardtanh(x + 3, 0., 6.) / 6. # for torchscript, CoreML and ONNX class MemoryEfficientSwish(nn.Module): class F(torch.autograd.Function): @staticmethod def forward(ctx, x): ctx.save_for_backward(x) return x * torch.sigmoid(x) @staticmethod def backward(ctx, grad_output): x = ctx.saved_tensors[0] sx = torch.sigmoid(x) return grad_output * (sx * (1 + x * (1 - sx))) def forward(self, x): return self.F.apply(x) # Mish https://github.com/digantamisra98/Mish -------------------------------------------------------------------------- class Mish(nn.Module): @staticmethod def forward(x): return x * F.softplus(x).tanh() class MemoryEfficientMish(nn.Module): class F(torch.autograd.Function): @staticmethod def forward(ctx, x): ctx.save_for_backward(x) return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x))) @staticmethod def backward(ctx, grad_output): x = ctx.saved_tensors[0] sx = torch.sigmoid(x) fx = F.softplus(x).tanh() return grad_output * (fx + x * sx * (1 - fx * fx)) def forward(self, x): return self.F.apply(x) # FReLU https://arxiv.org/abs/2007.11824 ------------------------------------------------------------------------------- class FReLU(nn.Module): def __init__(self, c1, k=3): # ch_in, kernel super().__init__() self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False) self.bn = nn.BatchNorm2d(c1) def forward(self, x): return torch.max(x, self.bn(self.conv(x))) ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/add_nms.py ================================================ import numpy as np import onnx from onnx import shape_inference try: import onnx_graphsurgeon as gs except Exception as e: print('Import onnx_graphsurgeon failure: %s' % e) import logging LOGGER = logging.getLogger(__name__) class RegisterNMS(object): def __init__( self, onnx_model_path: str, precision: str = "fp32", ): self.graph = gs.import_onnx(onnx.load(onnx_model_path)) assert self.graph LOGGER.info("ONNX graph created successfully") # Fold constants via ONNX-GS that PyTorch2ONNX may have missed self.graph.fold_constants() self.precision = precision self.batch_size = 1 def infer(self): """ Sanitize the graph by cleaning any unconnected nodes, do a topological resort, and fold constant inputs values. When possible, run shape inference on the ONNX graph to determine tensor shapes. """ for _ in range(3): count_before = len(self.graph.nodes) self.graph.cleanup().toposort() try: for node in self.graph.nodes: for o in node.outputs: o.shape = None model = gs.export_onnx(self.graph) model = shape_inference.infer_shapes(model) self.graph = gs.import_onnx(model) except Exception as e: LOGGER.info(f"Shape inference could not be performed at this time:\n{e}") try: self.graph.fold_constants(fold_shapes=True) except TypeError as e: LOGGER.error( "This version of ONNX GraphSurgeon does not support folding shapes, " f"please upgrade your onnx_graphsurgeon module. Error:\n{e}" ) raise count_after = len(self.graph.nodes) if count_before == count_after: # No new folding occurred in this iteration, so we can stop for now. break def save(self, output_path): """ Save the ONNX model to the given location. Args: output_path: Path pointing to the location where to write out the updated ONNX model. """ self.graph.cleanup().toposort() model = gs.export_onnx(self.graph) onnx.save(model, output_path) LOGGER.info(f"Saved ONNX model to {output_path}") def register_nms( self, *, score_thresh: float = 0.25, nms_thresh: float = 0.45, detections_per_img: int = 100, ): """ Register the ``EfficientNMS_TRT`` plugin node. NMS expects these shapes for its input tensors: - box_net: [batch_size, number_boxes, 4] - class_net: [batch_size, number_boxes, number_labels] Args: score_thresh (float): The scalar threshold for score (low scoring boxes are removed). nms_thresh (float): The scalar threshold for IOU (new boxes that have high IOU overlap with previously selected boxes are removed). detections_per_img (int): Number of best detections to keep after NMS. """ self.infer() # Find the concat node at the end of the network op_inputs = self.graph.outputs op = "EfficientNMS_TRT" attrs = { "plugin_version": "1", "background_class": -1, # no background class "max_output_boxes": detections_per_img, "score_threshold": score_thresh, "iou_threshold": nms_thresh, "score_activation": False, "box_coding": 0, } if self.precision == "fp32": dtype_output = np.float32 elif self.precision == "fp16": dtype_output = np.float16 else: raise NotImplementedError(f"Currently not supports precision: {self.precision}") # NMS Outputs output_num_detections = gs.Variable( name="num_detections", dtype=np.int32, shape=[self.batch_size, 1], ) # A scalar indicating the number of valid detections per batch image. output_boxes = gs.Variable( name="detection_boxes", dtype=dtype_output, shape=[self.batch_size, detections_per_img, 4], ) output_scores = gs.Variable( name="detection_scores", dtype=dtype_output, shape=[self.batch_size, detections_per_img], ) output_labels = gs.Variable( name="detection_classes", dtype=np.int32, shape=[self.batch_size, detections_per_img], ) op_outputs = [output_num_detections, output_boxes, output_scores, output_labels] # Create the NMS Plugin node with the selected inputs. The outputs of the node will also # become the final outputs of the graph. self.graph.layer(op=op, name="batched_nms", inputs=op_inputs, outputs=op_outputs, attrs=attrs) LOGGER.info(f"Created NMS plugin '{op}' with attributes: {attrs}") self.graph.outputs = op_outputs self.infer() def save(self, output_path): """ Save the ONNX model to the given location. Args: output_path: Path pointing to the location where to write out the updated ONNX model. """ self.graph.cleanup().toposort() model = gs.export_onnx(self.graph) onnx.save(model, output_path) LOGGER.info(f"Saved ONNX model to {output_path}") ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/autoanchor.py ================================================ # Auto-anchor utils import numpy as np import torch import yaml from scipy.cluster.vq import kmeans from tqdm import tqdm from asone.pose_estimators.yolov7_pose.utils.general import colorstr def check_anchor_order(m): # Check anchor order against stride order for YOLO Detect() module m, and correct if necessary a = m.anchor_grid.prod(-1).view(-1) # anchor area da = a[-1] - a[0] # delta a ds = m.stride[-1] - m.stride[0] # delta s if da.sign() != ds.sign(): # same order print('Reversing anchor order') m.anchors[:] = m.anchors.flip(0) m.anchor_grid[:] = m.anchor_grid.flip(0) def check_anchors(dataset, model, thr=4.0, imgsz=640): # Check anchor fit to data, recompute if necessary prefix = colorstr('autoanchor: ') print(f'\n{prefix}Analyzing anchors... ', end='') m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1] # Detect() shapes = imgsz * dataset.shapes / dataset.shapes.max(1, keepdims=True) scale = np.random.uniform(0.9, 1.1, size=(shapes.shape[0], 1)) # augment scale wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes * scale, dataset.labels)])).float() # wh def metric(k): # compute metric r = wh[:, None] / k[None] x = torch.min(r, 1. / r).min(2)[0] # ratio metric best = x.max(1)[0] # best_x aat = (x > 1. / thr).float().sum(1).mean() # anchors above threshold bpr = (best > 1. / thr).float().mean() # best possible recall return bpr, aat anchors = m.anchor_grid.clone().cpu().view(-1, 2) # current anchors bpr, aat = metric(anchors) print(f'anchors/target = {aat:.2f}, Best Possible Recall (BPR) = {bpr:.4f}', end='') if bpr < 0.98: # threshold to recompute print('. Attempting to improve anchors, please wait...') na = m.anchor_grid.numel() // 2 # number of anchors try: anchors = kmean_anchors(dataset, n=na, img_size=imgsz, thr=thr, gen=1000, verbose=False) except Exception as e: print(f'{prefix}ERROR: {e}') new_bpr = metric(anchors)[0] if new_bpr > bpr: # replace anchors anchors = torch.tensor(anchors, device=m.anchors.device).type_as(m.anchors) m.anchor_grid[:] = anchors.clone().view_as(m.anchor_grid) # for inference m.anchors[:] = anchors.clone().view_as(m.anchors) / m.stride.to(m.anchors.device).view(-1, 1, 1) # loss check_anchor_order(m) print(f'{prefix}New anchors saved to model. Update model *.yaml to use these anchors in the future.') else: print(f'{prefix}Original anchors better than new anchors. Proceeding with original anchors.') print('') # newline def kmean_anchors(path='./data/coco.yaml', n=9, img_size=640, thr=4.0, gen=1000, verbose=True): """ Creates kmeans-evolved anchors from training dataset Arguments: path: path to dataset *.yaml, or a loaded dataset n: number of anchors img_size: image size used for training thr: anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0 gen: generations to evolve anchors using genetic algorithm verbose: print all results Return: k: kmeans evolved anchors Usage: from utils.autoanchor import *; _ = kmean_anchors() """ thr = 1. / thr prefix = colorstr('autoanchor: ') def metric(k, wh): # compute metrics r = wh[:, None] / k[None] x = torch.min(r, 1. / r).min(2)[0] # ratio metric # x = wh_iou(wh, torch.tensor(k)) # iou metric return x, x.max(1)[0] # x, best_x def anchor_fitness(k): # mutation fitness _, best = metric(torch.tensor(k, dtype=torch.float32), wh) return (best * (best > thr).float()).mean() # fitness def print_results(k): k = k[np.argsort(k.prod(1))] # sort small to large x, best = metric(k, wh0) bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n # best possible recall, anch > thr print(f'{prefix}thr={thr:.2f}: {bpr:.4f} best possible recall, {aat:.2f} anchors past thr') print(f'{prefix}n={n}, img_size={img_size}, metric_all={x.mean():.3f}/{best.mean():.3f}-mean/best, ' f'past_thr={x[x > thr].mean():.3f}-mean: ', end='') for i, x in enumerate(k): print('%i,%i' % (round(x[0]), round(x[1])), end=', ' if i < len(k) - 1 else '\n') # use in *.cfg return k if isinstance(path, str): # *.yaml file with open(path) as f: data_dict = yaml.load(f, Loader=yaml.SafeLoader) # model dict from asone.pose_estimators.yolov7_pose.utils.datasets import LoadImagesAndLabels dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True) else: dataset = path # dataset # Get label wh shapes = img_size * dataset.shapes / dataset.shapes.max(1, keepdims=True) wh0 = np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)]) # wh # Filter i = (wh0 < 3.0).any(1).sum() if i: print(f'{prefix}WARNING: Extremely small objects found. {i} of {len(wh0)} labels are < 3 pixels in size.') wh = wh0[(wh0 >= 2.0).any(1)] # filter > 2 pixels # wh = wh * (np.random.rand(wh.shape[0], 1) * 0.9 + 0.1) # multiply by random scale 0-1 # Kmeans calculation print(f'{prefix}Running kmeans for {n} anchors on {len(wh)} points...') s = wh.std(0) # sigmas for whitening k, dist = kmeans(wh / s, n, iter=30) # points, mean distance assert len(k) == n, print(f'{prefix}ERROR: scipy.cluster.vq.kmeans requested {n} points but returned only {len(k)}') k *= s wh = torch.tensor(wh, dtype=torch.float32) # filtered wh0 = torch.tensor(wh0, dtype=torch.float32) # unfiltered k = print_results(k) # Plot # k, d = [None] * 20, [None] * 20 # for i in tqdm(range(1, 21)): # k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance # fig, ax = plt.subplots(1, 2, figsize=(14, 7), tight_layout=True) # ax = ax.ravel() # ax[0].plot(np.arange(1, 21), np.array(d) ** 2, marker='.') # fig, ax = plt.subplots(1, 2, figsize=(14, 7)) # plot wh # ax[0].hist(wh[wh[:, 0]<100, 0],400) # ax[1].hist(wh[wh[:, 1]<100, 1],400) # fig.savefig('wh.png', dpi=200) # Evolve npr = np.random f, sh, mp, s = anchor_fitness(k), k.shape, 0.9, 0.1 # fitness, generations, mutation prob, sigma pbar = tqdm(range(gen), desc=f'{prefix}Evolving anchors with Genetic Algorithm:') # progress bar for _ in pbar: v = np.ones(sh) while (v == 1).all(): # mutate until a change occurs (prevent duplicates) v = ((npr.random(sh) < mp) * npr.random() * npr.randn(*sh) * s + 1).clip(0.3, 3.0) kg = (k.copy() * v).clip(min=2.0) fg = anchor_fitness(kg) if fg > f: f, k = fg, kg.copy() pbar.desc = f'{prefix}Evolving anchors with Genetic Algorithm: fitness = {f:.4f}' if verbose: print_results(k) return print_results(k) ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/datasets.py ================================================ # Dataset utils and dataloaders import glob import logging import math import os import random import shutil import time from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path from threading import Thread import cv2 import numpy as np import torch import torch.nn.functional as F from PIL import Image, ExifTags from torch.utils.data import Dataset from tqdm import tqdm import pickle from copy import deepcopy #from pycocotools import mask as maskUtils from torchvision.utils import save_image from torchvision.ops import roi_pool, roi_align, ps_roi_pool, ps_roi_align from asone.pose_estimators.yolov7_pose.utils.general import check_requirements, xyxy2xywh, xywh2xyxy, xywhn2xyxy, xyn2xy, segment2box, segments2boxes, \ resample_segments, clean_str from asone.pose_estimators.yolov7_pose.utils.torch_utils import torch_distributed_zero_first # Parameters help_url = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data' img_formats = ['bmp', 'jpg', 'jpeg', 'png', 'tif', 'tiff', 'dng', 'webp', 'mpo'] # acceptable image suffixes vid_formats = ['mov', 'avi', 'mp4', 'mpg', 'mpeg', 'm4v', 'wmv', 'mkv'] # acceptable video suffixes logger = logging.getLogger(__name__) # Get orientation exif tag for orientation in ExifTags.TAGS.keys(): if ExifTags.TAGS[orientation] == 'Orientation': break def get_hash(files): # Returns a single hash value of a list of files return sum(os.path.getsize(f) for f in files if os.path.isfile(f)) def exif_size(img): # Returns exif-corrected PIL size s = img.size # (width, height) try: rotation = dict(img._getexif().items())[orientation] if rotation == 6: # rotation 270 s = (s[1], s[0]) elif rotation == 8: # rotation 90 s = (s[1], s[0]) except: pass return s def create_dataloader(path, imgsz, batch_size, stride, opt, hyp=None, augment=False, cache=False, pad=0.0, rect=False, rank=-1, world_size=1, workers=8, image_weights=False, quad=False, prefix=''): # Make sure only the first process in DDP process the dataset first, and the following others can use the cache with torch_distributed_zero_first(rank): dataset = LoadImagesAndLabels(path, imgsz, batch_size, augment=augment, # augment images hyp=hyp, # augmentation hyperparameters rect=rect, # rectangular training cache_images=cache, single_cls=opt.single_cls, stride=int(stride), pad=pad, image_weights=image_weights, prefix=prefix) batch_size = min(batch_size, len(dataset)) nw = min([os.cpu_count() // world_size, batch_size if batch_size > 1 else 0, workers]) # number of workers sampler = torch.utils.data.distributed.DistributedSampler(dataset) if rank != -1 else None loader = torch.utils.data.DataLoader if image_weights else InfiniteDataLoader # Use torch.utils.data.DataLoader() if dataset.properties will update during training else InfiniteDataLoader() dataloader = loader(dataset, batch_size=batch_size, num_workers=nw, sampler=sampler, pin_memory=True, collate_fn=LoadImagesAndLabels.collate_fn4 if quad else LoadImagesAndLabels.collate_fn) return dataloader, dataset class InfiniteDataLoader(torch.utils.data.dataloader.DataLoader): """ Dataloader that reuses workers Uses same syntax as vanilla DataLoader """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) object.__setattr__(self, 'batch_sampler', _RepeatSampler(self.batch_sampler)) self.iterator = super().__iter__() def __len__(self): return len(self.batch_sampler.sampler) def __iter__(self): for i in range(len(self)): yield next(self.iterator) class _RepeatSampler(object): """ Sampler that repeats forever Args: sampler (Sampler) """ def __init__(self, sampler): self.sampler = sampler def __iter__(self): while True: yield from iter(self.sampler) class LoadImages: # for inference def __init__(self, path, img_size=640, stride=32): p = str(Path(path).absolute()) # os-agnostic absolute path if '*' in p: files = sorted(glob.glob(p, recursive=True)) # glob elif os.path.isdir(p): files = sorted(glob.glob(os.path.join(p, '*.*'))) # dir elif os.path.isfile(p): files = [p] # files else: raise Exception(f'ERROR: {p} does not exist') images = [x for x in files if x.split('.')[-1].lower() in img_formats] videos = [x for x in files if x.split('.')[-1].lower() in vid_formats] ni, nv = len(images), len(videos) self.img_size = img_size self.stride = stride self.files = images + videos self.nf = ni + nv # number of files self.video_flag = [False] * ni + [True] * nv self.mode = 'image' if any(videos): self.new_video(videos[0]) # new video else: self.cap = None assert self.nf > 0, f'No images or videos found in {p}. ' \ f'Supported formats are:\nimages: {img_formats}\nvideos: {vid_formats}' def __iter__(self): self.count = 0 return self def __next__(self): if self.count == self.nf: raise StopIteration path = self.files[self.count] if self.video_flag[self.count]: # Read video self.mode = 'video' ret_val, img0 = self.cap.read() if not ret_val: self.count += 1 self.cap.release() if self.count == self.nf: # last video raise StopIteration else: path = self.files[self.count] self.new_video(path) ret_val, img0 = self.cap.read() self.frame += 1 print(f'video {self.count + 1}/{self.nf} ({self.frame}/{self.nframes}) {path}: ', end='') else: # Read image self.count += 1 img0 = cv2.imread(path) # BGR assert img0 is not None, 'Image Not Found ' + path #print(f'image {self.count}/{self.nf} {path}: ', end='') # Padded resize img = letterbox(img0, self.img_size, stride=self.stride)[0] # Convert img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 img = np.ascontiguousarray(img) return path, img, img0, self.cap def new_video(self, path): self.frame = 0 self.cap = cv2.VideoCapture(path) self.nframes = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT)) def __len__(self): return self.nf # number of files class LoadWebcam: # for inference def __init__(self, pipe='0', img_size=640, stride=32): self.img_size = img_size self.stride = stride if pipe.isnumeric(): pipe = eval(pipe) # local camera # pipe = 'rtsp://192.168.1.64/1' # IP camera # pipe = 'rtsp://username:password@192.168.1.64/1' # IP camera with login # pipe = 'http://wmccpinetop.axiscam.net/mjpg/video.mjpg' # IP golf camera self.pipe = pipe self.cap = cv2.VideoCapture(pipe) # video capture object self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # set buffer size def __iter__(self): self.count = -1 return self def __next__(self): self.count += 1 if cv2.waitKey(1) == ord('q'): # q to quit self.cap.release() cv2.destroyAllWindows() raise StopIteration # Read frame if self.pipe == 0: # local camera ret_val, img0 = self.cap.read() img0 = cv2.flip(img0, 1) # flip left-right else: # IP camera n = 0 while True: n += 1 self.cap.grab() if n % 30 == 0: # skip frames ret_val, img0 = self.cap.retrieve() if ret_val: break # Print assert ret_val, f'Camera Error {self.pipe}' img_path = 'webcam.jpg' print(f'webcam {self.count}: ', end='') # Padded resize img = letterbox(img0, self.img_size, stride=self.stride)[0] # Convert img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 img = np.ascontiguousarray(img) return img_path, img, img0, None def __len__(self): return 0 class LoadStreams: # multiple IP or RTSP cameras def __init__(self, sources='streams.txt', img_size=640, stride=32): self.mode = 'stream' self.img_size = img_size self.stride = stride if os.path.isfile(sources): with open(sources, 'r') as f: sources = [x.strip() for x in f.read().strip().splitlines() if len(x.strip())] else: sources = [sources] n = len(sources) self.imgs = [None] * n self.sources = [clean_str(x) for x in sources] # clean source names for later for i, s in enumerate(sources): # Start the thread to read frames from the video stream print(f'{i + 1}/{n}: {s}... ', end='') url = eval(s) if s.isnumeric() else s if 'youtube.com/' in str(url) or 'youtu.be/' in str(url): # if source is YouTube video check_requirements(('pafy', 'youtube_dl')) import pafy url = pafy.new(url).getbest(preftype="mp4").url cap = cv2.VideoCapture(url) assert cap.isOpened(), f'Failed to open {s}' w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) self.fps = cap.get(cv2.CAP_PROP_FPS) % 100 _, self.imgs[i] = cap.read() # guarantee first frame thread = Thread(target=self.update, args=([i, cap]), daemon=True) print(f' success ({w}x{h} at {self.fps:.2f} FPS).') thread.start() print('') # newline # check for common shapes s = np.stack([letterbox(x, self.img_size, stride=self.stride)[0].shape for x in self.imgs], 0) # shapes self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal if not self.rect: print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.') def update(self, index, cap): # Read next stream frame in a daemon thread n = 0 while cap.isOpened(): n += 1 # _, self.imgs[index] = cap.read() cap.grab() if n == 4: # read every 4th frame success, im = cap.retrieve() self.imgs[index] = im if success else self.imgs[index] * 0 n = 0 time.sleep(1 / self.fps) # wait time def __iter__(self): self.count = -1 return self def __next__(self): self.count += 1 img0 = self.imgs.copy() if cv2.waitKey(1) == ord('q'): # q to quit cv2.destroyAllWindows() raise StopIteration # Letterbox img = [letterbox(x, self.img_size, auto=self.rect, stride=self.stride)[0] for x in img0] # Stack img = np.stack(img, 0) # Convert img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416 img = np.ascontiguousarray(img) return self.sources, img, img0, None def __len__(self): return 0 # 1E12 frames = 32 streams at 30 FPS for 30 years def img2label_paths(img_paths): # Define label paths as a function of image paths sa, sb = os.sep + 'images' + os.sep, os.sep + 'labels' + os.sep # /images/, /labels/ substrings return ['txt'.join(x.replace(sa, sb, 1).rsplit(x.split('.')[-1], 1)) for x in img_paths] class LoadImagesAndLabels(Dataset): # for training/testing def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, cache_images=False, single_cls=False, stride=32, pad=0.0, prefix=''): self.img_size = img_size self.augment = augment self.hyp = hyp self.image_weights = image_weights self.rect = False if image_weights else rect self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training) self.mosaic_border = [-img_size // 2, -img_size // 2] self.stride = stride self.path = path #self.albumentations = Albumentations() if augment else None try: f = [] # image files for p in path if isinstance(path, list) else [path]: p = Path(p) # os-agnostic if p.is_dir(): # dir f += glob.glob(str(p / '**' / '*.*'), recursive=True) # f = list(p.rglob('**/*.*')) # pathlib elif p.is_file(): # file with open(p, 'r') as t: t = t.read().strip().splitlines() parent = str(p.parent) + os.sep f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path # f += [p.parent / x.lstrip(os.sep) for x in t] # local to global path (pathlib) else: raise Exception(f'{prefix}{p} does not exist') self.img_files = sorted([x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in img_formats]) # self.img_files = sorted([x for x in f if x.suffix[1:].lower() in img_formats]) # pathlib assert self.img_files, f'{prefix}No images found' except Exception as e: raise Exception(f'{prefix}Error loading data from {path}: {e}\nSee {help_url}') # Check cache self.label_files = img2label_paths(self.img_files) # labels cache_path = (p if p.is_file() else Path(self.label_files[0]).parent).with_suffix('.cache') # cached labels if cache_path.is_file(): cache, exists = torch.load(cache_path), True # load #if cache['hash'] != get_hash(self.label_files + self.img_files) or 'version' not in cache: # changed # cache, exists = self.cache_labels(cache_path, prefix), False # re-cache else: cache, exists = self.cache_labels(cache_path, prefix), False # cache # Display cache nf, nm, ne, nc, n = cache.pop('results') # found, missing, empty, corrupted, total if exists: d = f"Scanning '{cache_path}' images and labels... {nf} found, {nm} missing, {ne} empty, {nc} corrupted" tqdm(None, desc=prefix + d, total=n, initial=n) # display cache results assert nf > 0 or not augment, f'{prefix}No labels in {cache_path}. Can not train without labels. See {help_url}' # Read cache cache.pop('hash') # remove hash cache.pop('version') # remove version labels, shapes, self.segments = zip(*cache.values()) self.labels = list(labels) self.shapes = np.array(shapes, dtype=np.float64) self.img_files = list(cache.keys()) # update self.label_files = img2label_paths(cache.keys()) # update if single_cls: for x in self.labels: x[:, 0] = 0 n = len(shapes) # number of images bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index nb = bi[-1] + 1 # number of batches self.batch = bi # batch index of image self.n = n self.indices = range(n) # Rectangular Training if self.rect: # Sort by aspect ratio s = self.shapes # wh ar = s[:, 1] / s[:, 0] # aspect ratio irect = ar.argsort() self.img_files = [self.img_files[i] for i in irect] self.label_files = [self.label_files[i] for i in irect] self.labels = [self.labels[i] for i in irect] self.shapes = s[irect] # wh ar = ar[irect] # Set training image shapes shapes = [[1, 1]] * nb for i in range(nb): ari = ar[bi == i] mini, maxi = ari.min(), ari.max() if maxi < 1: shapes[i] = [maxi, 1] elif mini > 1: shapes[i] = [1, 1 / mini] self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(np.int) * stride # Cache images into memory for faster training (WARNING: large datasets may exceed system RAM) self.imgs = [None] * n if cache_images: if cache_images == 'disk': self.im_cache_dir = Path(Path(self.img_files[0]).parent.as_posix() + '_npy') self.img_npy = [self.im_cache_dir / Path(f).with_suffix('.npy').name for f in self.img_files] self.im_cache_dir.mkdir(parents=True, exist_ok=True) gb = 0 # Gigabytes of cached images self.img_hw0, self.img_hw = [None] * n, [None] * n results = ThreadPool(8).imap(lambda x: load_image(*x), zip(repeat(self), range(n))) pbar = tqdm(enumerate(results), total=n) for i, x in pbar: if cache_images == 'disk': if not self.img_npy[i].exists(): np.save(self.img_npy[i].as_posix(), x[0]) gb += self.img_npy[i].stat().st_size else: self.imgs[i], self.img_hw0[i], self.img_hw[i] = x gb += self.imgs[i].nbytes pbar.desc = f'{prefix}Caching images ({gb / 1E9:.1f}GB)' pbar.close() def cache_labels(self, path=Path('./labels.cache'), prefix=''): # Cache dataset labels, check images and read shapes x = {} # dict nm, nf, ne, nc = 0, 0, 0, 0 # number missing, found, empty, duplicate pbar = tqdm(zip(self.img_files, self.label_files), desc='Scanning images', total=len(self.img_files)) for i, (im_file, lb_file) in enumerate(pbar): try: # verify images im = Image.open(im_file) im.verify() # PIL verify shape = exif_size(im) # image size segments = [] # instance segments assert (shape[0] > 9) & (shape[1] > 9), f'image size {shape} <10 pixels' assert im.format.lower() in img_formats, f'invalid image format {im.format}' # verify labels if os.path.isfile(lb_file): nf += 1 # label found with open(lb_file, 'r') as f: l = [x.split() for x in f.read().strip().splitlines()] if any([len(x) > 8 for x in l]): # is segment classes = np.array([x[0] for x in l], dtype=np.float32) segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in l] # (cls, xy1...) l = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1) # (cls, xywh) l = np.array(l, dtype=np.float32) if len(l): assert l.shape[1] == 5, 'labels require 5 columns each' assert (l >= 0).all(), 'negative labels' assert (l[:, 1:] <= 1).all(), 'non-normalized or out of bounds coordinate labels' assert np.unique(l, axis=0).shape[0] == l.shape[0], 'duplicate labels' else: ne += 1 # label empty l = np.zeros((0, 5), dtype=np.float32) else: nm += 1 # label missing l = np.zeros((0, 5), dtype=np.float32) x[im_file] = [l, shape, segments] except Exception as e: nc += 1 print(f'{prefix}WARNING: Ignoring corrupted image and/or label {im_file}: {e}') pbar.desc = f"{prefix}Scanning '{path.parent / path.stem}' images and labels... " \ f"{nf} found, {nm} missing, {ne} empty, {nc} corrupted" pbar.close() if nf == 0: print(f'{prefix}WARNING: No labels found in {path}. See {help_url}') x['hash'] = get_hash(self.label_files + self.img_files) x['results'] = nf, nm, ne, nc, i + 1 x['version'] = 0.1 # cache version torch.save(x, path) # save for next time logging.info(f'{prefix}New cache created: {path}') return x def __len__(self): return len(self.img_files) # def __iter__(self): # self.count = -1 # print('ran dataset iter') # #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF) # return self def __getitem__(self, index): index = self.indices[index] # linear, shuffled, or image_weights hyp = self.hyp mosaic = self.mosaic and random.random() < hyp['mosaic'] if mosaic: # Load mosaic if random.random() < 0.8: img, labels = load_mosaic(self, index) else: img, labels = load_mosaic9(self, index) shapes = None # MixUp https://arxiv.org/pdf/1710.09412.pdf if random.random() < hyp['mixup']: if random.random() < 0.8: img2, labels2 = load_mosaic(self, random.randint(0, len(self.labels) - 1)) else: img2, labels2 = load_mosaic9(self, random.randint(0, len(self.labels) - 1)) r = np.random.beta(8.0, 8.0) # mixup ratio, alpha=beta=8.0 img = (img * r + img2 * (1 - r)).astype(np.uint8) labels = np.concatenate((labels, labels2), 0) else: # Load image img, (h0, w0), (h, w) = load_image(self, index) # Letterbox shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment) shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling labels = self.labels[index].copy() if labels.size: # normalized xywh to pixel xyxy format labels[:, 1:] = xywhn2xyxy(labels[:, 1:], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1]) if self.augment: # Augment imagespace if not mosaic: img, labels = random_perspective(img, labels, degrees=hyp['degrees'], translate=hyp['translate'], scale=hyp['scale'], shear=hyp['shear'], perspective=hyp['perspective']) #img, labels = self.albumentations(img, labels) # Augment colorspace augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v']) # Apply cutouts # if random.random() < 0.9: # labels = cutout(img, labels) if random.random() < hyp['paste_in']: sample_labels, sample_images, sample_masks = [], [], [] while len(sample_labels) < 30: sample_labels_, sample_images_, sample_masks_ = load_samples(self, random.randint(0, len(self.labels) - 1)) sample_labels += sample_labels_ sample_images += sample_images_ sample_masks += sample_masks_ #print(len(sample_labels)) if len(sample_labels) == 0: break labels = pastein(img, labels, sample_labels, sample_images, sample_masks) nL = len(labels) # number of labels if nL: labels[:, 1:5] = xyxy2xywh(labels[:, 1:5]) # convert xyxy to xywh labels[:, [2, 4]] /= img.shape[0] # normalized height 0-1 labels[:, [1, 3]] /= img.shape[1] # normalized width 0-1 if self.augment: # flip up-down if random.random() < hyp['flipud']: img = np.flipud(img) if nL: labels[:, 2] = 1 - labels[:, 2] # flip left-right if random.random() < hyp['fliplr']: img = np.fliplr(img) if nL: labels[:, 1] = 1 - labels[:, 1] labels_out = torch.zeros((nL, 6)) if nL: labels_out[:, 1:] = torch.from_numpy(labels) # Convert img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 img = np.ascontiguousarray(img) return torch.from_numpy(img), labels_out, self.img_files[index], shapes @staticmethod def collate_fn(batch): img, label, path, shapes = zip(*batch) # transposed for i, l in enumerate(label): l[:, 0] = i # add target image index for build_targets() return torch.stack(img, 0), torch.cat(label, 0), path, shapes @staticmethod def collate_fn4(batch): img, label, path, shapes = zip(*batch) # transposed n = len(shapes) // 4 img4, label4, path4, shapes4 = [], [], path[:n], shapes[:n] ho = torch.tensor([[0., 0, 0, 1, 0, 0]]) wo = torch.tensor([[0., 0, 1, 0, 0, 0]]) s = torch.tensor([[1, 1, .5, .5, .5, .5]]) # scale for i in range(n): # zidane torch.zeros(16,3,720,1280) # BCHW i *= 4 if random.random() < 0.5: im = F.interpolate(img[i].unsqueeze(0).float(), scale_factor=2., mode='bilinear', align_corners=False)[ 0].type(img[i].type()) l = label[i] else: im = torch.cat((torch.cat((img[i], img[i + 1]), 1), torch.cat((img[i + 2], img[i + 3]), 1)), 2) l = torch.cat((label[i], label[i + 1] + ho, label[i + 2] + wo, label[i + 3] + ho + wo), 0) * s img4.append(im) label4.append(l) for i, l in enumerate(label4): l[:, 0] = i # add target image index for build_targets() return torch.stack(img4, 0), torch.cat(label4, 0), path4, shapes4 # Ancillary functions -------------------------------------------------------------------------------------------------- def load_image(self, index): # loads 1 image from dataset, returns img, original hw, resized hw img = self.imgs[index] if img is None: # not cached path = self.img_files[index] img = cv2.imread(path) # BGR assert img is not None, 'Image Not Found ' + path h0, w0 = img.shape[:2] # orig hw r = self.img_size / max(h0, w0) # resize image to img_size if r != 1: # always resize down, only resize up if training with augmentation interp = cv2.INTER_AREA if r < 1 and not self.augment else cv2.INTER_LINEAR img = cv2.resize(img, (int(w0 * r), int(h0 * r)), interpolation=interp) return img, (h0, w0), img.shape[:2] # img, hw_original, hw_resized else: return self.imgs[index], self.img_hw0[index], self.img_hw[index] # img, hw_original, hw_resized def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5): r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) dtype = img.dtype # uint8 x = np.arange(0, 256, dtype=np.int16) lut_hue = ((x * r[0]) % 180).astype(dtype) lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) lut_val = np.clip(x * r[2], 0, 255).astype(dtype) img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype) cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed def hist_equalize(img, clahe=True, bgr=False): # Equalize histogram on BGR image 'img' with img.shape(n,m,3) and range 0-255 yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV if bgr else cv2.COLOR_RGB2YUV) if clahe: c = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) yuv[:, :, 0] = c.apply(yuv[:, :, 0]) else: yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0]) # equalize Y channel histogram return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR if bgr else cv2.COLOR_YUV2RGB) # convert YUV image to RGB def load_mosaic(self, index): # loads images in a 4-mosaic labels4, segments4 = [], [] s = self.img_size yc, xc = [int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border] # mosaic center x, y indices = [index] + random.choices(self.indices, k=3) # 3 additional image indices for i, index in enumerate(indices): # Load image img, _, (h, w) = load_image(self, index) # place img in img4 if i == 0: # top left img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image) x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image) elif i == 1: # top right x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h elif i == 2: # bottom left x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h) x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h) elif i == 3: # bottom right x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h) x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h) img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax] padw = x1a - x1b padh = y1a - y1b # Labels labels, segments = self.labels[index].copy(), self.segments[index].copy() if labels.size: labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format segments = [xyn2xy(x, w, h, padw, padh) for x in segments] labels4.append(labels) segments4.extend(segments) # Concat/clip labels labels4 = np.concatenate(labels4, 0) for x in (labels4[:, 1:], *segments4): np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective() # img4, labels4 = replicate(img4, labels4) # replicate # Augment #img4, labels4, segments4 = remove_background(img4, labels4, segments4) #sample_segments(img4, labels4, segments4, probability=self.hyp['copy_paste']) img4, labels4, segments4 = copy_paste(img4, labels4, segments4, probability=self.hyp['copy_paste']) img4, labels4 = random_perspective(img4, labels4, segments4, degrees=self.hyp['degrees'], translate=self.hyp['translate'], scale=self.hyp['scale'], shear=self.hyp['shear'], perspective=self.hyp['perspective'], border=self.mosaic_border) # border to remove return img4, labels4 def load_mosaic9(self, index): # loads images in a 9-mosaic labels9, segments9 = [], [] s = self.img_size indices = [index] + random.choices(self.indices, k=8) # 8 additional image indices for i, index in enumerate(indices): # Load image img, _, (h, w) = load_image(self, index) # place img in img9 if i == 0: # center img9 = np.full((s * 3, s * 3, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles h0, w0 = h, w c = s, s, s + w, s + h # xmin, ymin, xmax, ymax (base) coordinates elif i == 1: # top c = s, s - h, s + w, s elif i == 2: # top right c = s + wp, s - h, s + wp + w, s elif i == 3: # right c = s + w0, s, s + w0 + w, s + h elif i == 4: # bottom right c = s + w0, s + hp, s + w0 + w, s + hp + h elif i == 5: # bottom c = s + w0 - w, s + h0, s + w0, s + h0 + h elif i == 6: # bottom left c = s + w0 - wp - w, s + h0, s + w0 - wp, s + h0 + h elif i == 7: # left c = s - w, s + h0 - h, s, s + h0 elif i == 8: # top left c = s - w, s + h0 - hp - h, s, s + h0 - hp padx, pady = c[:2] x1, y1, x2, y2 = [max(x, 0) for x in c] # allocate coords # Labels labels, segments = self.labels[index].copy(), self.segments[index].copy() if labels.size: labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padx, pady) # normalized xywh to pixel xyxy format segments = [xyn2xy(x, w, h, padx, pady) for x in segments] labels9.append(labels) segments9.extend(segments) # Image img9[y1:y2, x1:x2] = img[y1 - pady:, x1 - padx:] # img9[ymin:ymax, xmin:xmax] hp, wp = h, w # height, width previous # Offset yc, xc = [int(random.uniform(0, s)) for _ in self.mosaic_border] # mosaic center x, y img9 = img9[yc:yc + 2 * s, xc:xc + 2 * s] # Concat/clip labels labels9 = np.concatenate(labels9, 0) labels9[:, [1, 3]] -= xc labels9[:, [2, 4]] -= yc c = np.array([xc, yc]) # centers segments9 = [x - c for x in segments9] for x in (labels9[:, 1:], *segments9): np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective() # img9, labels9 = replicate(img9, labels9) # replicate # Augment #img9, labels9, segments9 = remove_background(img9, labels9, segments9) img9, labels9, segments9 = copy_paste(img9, labels9, segments9, probability=self.hyp['copy_paste']) img9, labels9 = random_perspective(img9, labels9, segments9, degrees=self.hyp['degrees'], translate=self.hyp['translate'], scale=self.hyp['scale'], shear=self.hyp['shear'], perspective=self.hyp['perspective'], border=self.mosaic_border) # border to remove return img9, labels9 def load_samples(self, index): # loads images in a 4-mosaic labels4, segments4 = [], [] s = self.img_size yc, xc = [int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border] # mosaic center x, y indices = [index] + random.choices(self.indices, k=3) # 3 additional image indices for i, index in enumerate(indices): # Load image img, _, (h, w) = load_image(self, index) # place img in img4 if i == 0: # top left img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image) x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image) elif i == 1: # top right x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h elif i == 2: # bottom left x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h) x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h) elif i == 3: # bottom right x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h) x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h) img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax] padw = x1a - x1b padh = y1a - y1b # Labels labels, segments = self.labels[index].copy(), self.segments[index].copy() if labels.size: labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format segments = [xyn2xy(x, w, h, padw, padh) for x in segments] labels4.append(labels) segments4.extend(segments) # Concat/clip labels labels4 = np.concatenate(labels4, 0) for x in (labels4[:, 1:], *segments4): np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective() # img4, labels4 = replicate(img4, labels4) # replicate # Augment #img4, labels4, segments4 = remove_background(img4, labels4, segments4) sample_labels, sample_images, sample_masks = sample_segments(img4, labels4, segments4, probability=0.5) return sample_labels, sample_images, sample_masks def copy_paste(img, labels, segments, probability=0.5): # Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy) n = len(segments) if probability and n: h, w, c = img.shape # height, width, channels im_new = np.zeros(img.shape, np.uint8) for j in random.sample(range(n), k=round(probability * n)): l, s = labels[j], segments[j] box = w - l[3], l[2], w - l[1], l[4] ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area if (ioa < 0.30).all(): # allow 30% obscuration of existing labels labels = np.concatenate((labels, [[l[0], *box]]), 0) segments.append(np.concatenate((w - s[:, 0:1], s[:, 1:2]), 1)) cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED) result = cv2.bitwise_and(src1=img, src2=im_new) result = cv2.flip(result, 1) # augment segments (flip left-right) i = result > 0 # pixels to replace # i[:, :] = result.max(2).reshape(h, w, 1) # act over ch img[i] = result[i] # cv2.imwrite('debug.jpg', img) # debug return img, labels, segments def remove_background(img, labels, segments): # Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy) n = len(segments) h, w, c = img.shape # height, width, channels im_new = np.zeros(img.shape, np.uint8) img_new = np.ones(img.shape, np.uint8) * 114 for j in range(n): cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED) result = cv2.bitwise_and(src1=img, src2=im_new) i = result > 0 # pixels to replace img_new[i] = result[i] # cv2.imwrite('debug.jpg', img) # debug return img_new, labels, segments def sample_segments(img, labels, segments, probability=0.5): # Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy) n = len(segments) sample_labels = [] sample_images = [] sample_masks = [] if probability and n: h, w, c = img.shape # height, width, channels for j in random.sample(range(n), k=round(probability * n)): l, s = labels[j], segments[j] box = l[1].astype(int).clip(0,w-1), l[2].astype(int).clip(0,h-1), l[3].astype(int).clip(0,w-1), l[4].astype(int).clip(0,h-1) #print(box) if (box[2] <= box[0]) or (box[3] <= box[1]): continue sample_labels.append(l[0]) mask = np.zeros(img.shape, np.uint8) cv2.drawContours(mask, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED) sample_masks.append(mask[box[1]:box[3],box[0]:box[2],:]) result = cv2.bitwise_and(src1=img, src2=mask) i = result > 0 # pixels to replace mask[i] = result[i] # cv2.imwrite('debug.jpg', img) # debug #print(box) sample_images.append(mask[box[1]:box[3],box[0]:box[2],:]) return sample_labels, sample_images, sample_masks def replicate(img, labels): # Replicate labels h, w = img.shape[:2] boxes = labels[:, 1:].astype(int) x1, y1, x2, y2 = boxes.T s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels) for i in s.argsort()[:round(s.size * 0.5)]: # smallest indices x1b, y1b, x2b, y2b = boxes[i] bh, bw = y2b - y1b, x2b - x1b yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh] img[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax] labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0) return img, labels def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32): # Resize and pad image while meeting stride-multiple constraints shape = img.shape[:2] # current shape [height, width] if isinstance(new_shape, int): new_shape = (new_shape, new_shape) # Scale ratio (new / old) r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) if not scaleup: # only scale down, do not scale up (for better test mAP) r = min(r, 1.0) # Compute padding ratio = r, r # width, height ratios new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding if auto: # minimum rectangle dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding elif scaleFill: # stretch dw, dh = 0.0, 0.0 new_unpad = (new_shape[1], new_shape[0]) ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios dw /= 2 # divide padding into 2 sides dh /= 2 if shape[::-1] != new_unpad: # resize img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border return img, ratio, (dw, dh) def random_perspective(img, targets=(), segments=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0, border=(0, 0)): # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10)) # targets = [cls, xyxy] height = img.shape[0] + border[0] * 2 # shape(h,w,c) width = img.shape[1] + border[1] * 2 # Center C = np.eye(3) C[0, 2] = -img.shape[1] / 2 # x translation (pixels) C[1, 2] = -img.shape[0] / 2 # y translation (pixels) # Perspective P = np.eye(3) P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y) P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x) # Rotation and Scale R = np.eye(3) a = random.uniform(-degrees, degrees) # a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations s = random.uniform(1 - scale, 1.1 + scale) # s = 2 ** random.uniform(-scale, scale) R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s) # Shear S = np.eye(3) S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg) S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg) # Translation T = np.eye(3) T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels) T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels) # Combined rotation matrix M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed if perspective: img = cv2.warpPerspective(img, M, dsize=(width, height), borderValue=(114, 114, 114)) else: # affine img = cv2.warpAffine(img, M[:2], dsize=(width, height), borderValue=(114, 114, 114)) # Visualize # import matplotlib.pyplot as plt # ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel() # ax[0].imshow(img[:, :, ::-1]) # base # ax[1].imshow(img2[:, :, ::-1]) # warped # Transform label coordinates n = len(targets) if n: use_segments = any(x.any() for x in segments) new = np.zeros((n, 4)) if use_segments: # warp segments segments = resample_segments(segments) # upsample for i, segment in enumerate(segments): xy = np.ones((len(segment), 3)) xy[:, :2] = segment xy = xy @ M.T # transform xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2] # perspective rescale or affine # clip new[i] = segment2box(xy, width, height) else: # warp boxes xy = np.ones((n * 4, 3)) xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1 xy = xy @ M.T # transform xy = (xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]).reshape(n, 8) # perspective rescale or affine # create new boxes x = xy[:, [0, 2, 4, 6]] y = xy[:, [1, 3, 5, 7]] new = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T # clip new[:, [0, 2]] = new[:, [0, 2]].clip(0, width) new[:, [1, 3]] = new[:, [1, 3]].clip(0, height) # filter candidates i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01 if use_segments else 0.10) targets = targets[i] targets[:, 1:5] = new[i] return img, targets def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, eps=1e-16): # box1(4,n), box2(4,n) # Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio w1, h1 = box1[2] - box1[0], box1[3] - box1[1] w2, h2 = box2[2] - box2[0], box2[3] - box2[1] ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps)) # aspect ratio return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr) # candidates def bbox_ioa(box1, box2): # Returns the intersection over box2 area given box1, box2. box1 is 4, box2 is nx4. boxes are x1y1x2y2 box2 = box2.transpose() # Get the coordinates of bounding boxes b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3] b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3] # Intersection area inter_area = (np.minimum(b1_x2, b2_x2) - np.maximum(b1_x1, b2_x1)).clip(0) * \ (np.minimum(b1_y2, b2_y2) - np.maximum(b1_y1, b2_y1)).clip(0) # box2 area box2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + 1e-16 # Intersection over box2 area return inter_area / box2_area def cutout(image, labels): # Applies image cutout augmentation https://arxiv.org/abs/1708.04552 h, w = image.shape[:2] # create random masks scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16 # image size fraction for s in scales: mask_h = random.randint(1, int(h * s)) mask_w = random.randint(1, int(w * s)) # box xmin = max(0, random.randint(0, w) - mask_w // 2) ymin = max(0, random.randint(0, h) - mask_h // 2) xmax = min(w, xmin + mask_w) ymax = min(h, ymin + mask_h) # apply random color mask image[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)] # return unobscured labels if len(labels) and s > 0.03: box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32) ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area labels = labels[ioa < 0.60] # remove >60% obscured labels return labels def pastein(image, labels, sample_labels, sample_images, sample_masks): # Applies image cutout augmentation https://arxiv.org/abs/1708.04552 h, w = image.shape[:2] # create random masks scales = [0.75] * 2 + [0.5] * 4 + [0.25] * 4 + [0.125] * 4 + [0.0625] * 6 # image size fraction for s in scales: if random.random() < 0.2: continue mask_h = random.randint(1, int(h * s)) mask_w = random.randint(1, int(w * s)) # box xmin = max(0, random.randint(0, w) - mask_w // 2) ymin = max(0, random.randint(0, h) - mask_h // 2) xmax = min(w, xmin + mask_w) ymax = min(h, ymin + mask_h) box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32) if len(labels): ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area else: ioa = np.zeros(1) if (ioa < 0.30).all() and len(sample_labels) and (xmax > xmin+20) and (ymax > ymin+20): # allow 30% obscuration of existing labels sel_ind = random.randint(0, len(sample_labels)-1) #print(len(sample_labels)) #print(sel_ind) #print((xmax-xmin, ymax-ymin)) #print(image[ymin:ymax, xmin:xmax].shape) #print([[sample_labels[sel_ind], *box]]) #print(labels.shape) hs, ws, cs = sample_images[sel_ind].shape r_scale = min((ymax-ymin)/hs, (xmax-xmin)/ws) r_w = int(ws*r_scale) r_h = int(hs*r_scale) if (r_w > 10) and (r_h > 10): r_mask = cv2.resize(sample_masks[sel_ind], (r_w, r_h)) r_image = cv2.resize(sample_images[sel_ind], (r_w, r_h)) temp_crop = image[ymin:ymin+r_h, xmin:xmin+r_w] m_ind = r_mask > 0 if m_ind.astype(np.int).sum() > 60: temp_crop[m_ind] = r_image[m_ind] #print(sample_labels[sel_ind]) #print(sample_images[sel_ind].shape) #print(temp_crop.shape) box = np.array([xmin, ymin, xmin+r_w, ymin+r_h], dtype=np.float32) if len(labels): labels = np.concatenate((labels, [[sample_labels[sel_ind], *box]]), 0) else: labels = np.array([[sample_labels[sel_ind], *box]]) image[ymin:ymin+r_h, xmin:xmin+r_w] = temp_crop return labels class Albumentations: # YOLOv5 Albumentations class (optional, only used if package is installed) def __init__(self): self.transform = None import albumentations as A self.transform = A.Compose([ A.CLAHE(p=0.01), A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.01), A.RandomGamma(gamma_limit=[80, 120], p=0.01), A.Blur(p=0.01), A.MedianBlur(p=0.01), A.ToGray(p=0.01), A.ImageCompression(quality_lower=75, p=0.01),], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['class_labels'])) #logging.info(colorstr('albumentations: ') + ', '.join(f'{x}' for x in self.transform.transforms if x.p)) def __call__(self, im, labels, p=1.0): if self.transform and random.random() < p: new = self.transform(image=im, bboxes=labels[:, 1:], class_labels=labels[:, 0]) # transformed im, labels = new['image'], np.array([[c, *b] for c, b in zip(new['class_labels'], new['bboxes'])]) return im, labels def create_folder(path='./new'): # Create folder if os.path.exists(path): shutil.rmtree(path) # delete output folder os.makedirs(path) # make new output folder def flatten_recursive(path='../coco'): # Flatten a recursive directory by bringing all files to top level new_path = Path(path + '_flat') create_folder(new_path) for file in tqdm(glob.glob(str(Path(path)) + '/**/*.*', recursive=True)): shutil.copyfile(file, new_path / Path(file).name) def extract_boxes(path='../coco/'): # from utils.datasets import *; extract_boxes('../coco128') # Convert detection dataset into classification dataset, with one directory per class path = Path(path) # images dir shutil.rmtree(path / 'classifier') if (path / 'classifier').is_dir() else None # remove existing files = list(path.rglob('*.*')) n = len(files) # number of files for im_file in tqdm(files, total=n): if im_file.suffix[1:] in img_formats: # image im = cv2.imread(str(im_file))[..., ::-1] # BGR to RGB h, w = im.shape[:2] # labels lb_file = Path(img2label_paths([str(im_file)])[0]) if Path(lb_file).exists(): with open(lb_file, 'r') as f: lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32) # labels for j, x in enumerate(lb): c = int(x[0]) # class f = (path / 'classifier') / f'{c}' / f'{path.stem}_{im_file.stem}_{j}.jpg' # new filename if not f.parent.is_dir(): f.parent.mkdir(parents=True) b = x[1:] * [w, h, w, h] # box # b[2:] = b[2:].max() # rectangle to square b[2:] = b[2:] * 1.2 + 3 # pad b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(np.int) b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image b[[1, 3]] = np.clip(b[[1, 3]], 0, h) assert cv2.imwrite(str(f), im[b[1]:b[3], b[0]:b[2]]), f'box failure in {f}' def autosplit(path='../coco', weights=(0.9, 0.1, 0.0), annotated_only=False): """ Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files Usage: from utils.datasets import *; autosplit('../coco') Arguments path: Path to images directory weights: Train, val, test weights (list) annotated_only: Only use images with an annotated txt file """ path = Path(path) # images dir files = sum([list(path.rglob(f"*.{img_ext}")) for img_ext in img_formats], []) # image files only n = len(files) # number of files indices = random.choices([0, 1, 2], weights=weights, k=n) # assign each image to a split txt = ['autosplit_train.txt', 'autosplit_val.txt', 'autosplit_test.txt'] # 3 txt files [(path / x).unlink() for x in txt if (path / x).exists()] # remove existing print(f'Autosplitting images from {path}' + ', using *.txt labeled images only' * annotated_only) for i, img in tqdm(zip(indices, files), total=n): if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label with open(path / txt[i], 'a') as f: f.write(str(img) + '\n') # add image to txt file def load_segmentations(self, index): key = '/work/handsomejw66/coco17/' + self.img_files[index] #print(key) # /work/handsomejw66/coco17/ return self.segs[key] ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/general.py ================================================ # YOLOR general utils import glob import logging import math import os import platform import random import re import subprocess import time from pathlib import Path import cv2 import numpy as np import pandas as pd import torch import torchvision import yaml from asone.pose_estimators.yolov7_pose.utils.google_utils import gsutil_getsize from asone.pose_estimators.yolov7_pose.utils.metrics import fitness from asone.pose_estimators.yolov7_pose.utils.torch_utils import init_torch_seeds # Settings torch.set_printoptions(linewidth=320, precision=5, profile='long') np.set_printoptions(linewidth=320, formatter={'float_kind': '{:11.5g}'.format}) # format short g, %precision=5 pd.options.display.max_columns = 10 cv2.setNumThreads(0) # prevent OpenCV from multithreading (incompatible with PyTorch DataLoader) os.environ['NUMEXPR_MAX_THREADS'] = str(min(os.cpu_count(), 8)) # NumExpr max threads def set_logging(rank=-1): logging.basicConfig( format="%(message)s", level=logging.INFO if rank in [-1, 0] else logging.WARN) def init_seeds(seed=0): # Initialize random number generator (RNG) seeds random.seed(seed) np.random.seed(seed) init_torch_seeds(seed) def get_latest_run(search_dir='.'): # Return path to most recent 'last.pt' in /runs (i.e. to --resume from) last_list = glob.glob(f'{search_dir}/**/last*.pt', recursive=True) return max(last_list, key=os.path.getctime) if last_list else '' def isdocker(): # Is environment a Docker container return Path('/workspace').exists() # or Path('/.dockerenv').exists() def emojis(str=''): # Return platform-dependent emoji-safe version of string return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str def check_online(): # Check internet connectivity import socket try: socket.create_connection(("1.1.1.1", 443), 5) # check host accesability return True except OSError: return False def check_git_status(): # Recommend 'git pull' if code is out of date print(colorstr('github: '), end='') try: assert Path('.git').exists(), 'skipping check (not a git repository)' assert not isdocker(), 'skipping check (Docker image)' assert check_online(), 'skipping check (offline)' cmd = 'git fetch && git config --get remote.origin.url' url = subprocess.check_output(cmd, shell=True).decode().strip().rstrip('.git') # github repo url branch = subprocess.check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out n = int(subprocess.check_output(f'git rev-list {branch}..origin/master --count', shell=True)) # commits behind if n > 0: s = f"⚠️ WARNING: code is out of date by {n} commit{'s' * (n > 1)}. " \ f"Use 'git pull' to update or 'git clone {url}' to download latest." else: s = f'up to date with {url} ✅' print(emojis(s)) # emoji-safe except Exception as e: print(e) def check_requirements(requirements='requirements.txt', exclude=()): # Check installed dependencies meet requirements (pass *.txt file or list of packages) import pkg_resources as pkg prefix = colorstr('red', 'bold', 'requirements:') if isinstance(requirements, (str, Path)): # requirements.txt file file = Path(requirements) if not file.exists(): print(f"{prefix} {file.resolve()} not found, check failed.") return requirements = [f'{x.name}{x.specifier}' for x in pkg.parse_requirements(file.open()) if x.name not in exclude] else: # list or tuple of packages requirements = [x for x in requirements if x not in exclude] n = 0 # number of packages updates for r in requirements: try: pkg.require(r) except Exception as e: # DistributionNotFound or VersionConflict if requirements not met n += 1 print(f"{prefix} {e.req} not found and is required by YOLOR, attempting auto-update...") print(subprocess.check_output(f"pip install '{e.req}'", shell=True).decode()) if n: # if packages updated source = file.resolve() if 'file' in locals() else requirements s = f"{prefix} {n} package{'s' * (n > 1)} updated per {source}\n" \ f"{prefix} ⚠️ {colorstr('bold', 'Restart runtime or rerun command for updates to take effect')}\n" print(emojis(s)) # emoji-safe def check_img_size(img_size, s=32): # Verify img_size is a multiple of stride s new_size = make_divisible(img_size, int(s)) # ceil gs-multiple if new_size != img_size: print('WARNING: --img-size %g must be multiple of max stride %g, updating to %g' % (img_size, s, new_size)) return new_size def check_imshow(): # Check if environment supports image displays try: assert not isdocker(), 'cv2.imshow() is disabled in Docker environments' cv2.imshow('test', np.zeros((1, 1, 3))) cv2.waitKey(1) cv2.destroyAllWindows() cv2.waitKey(1) return True except Exception as e: print(f'WARNING: Environment does not support cv2.imshow() or PIL Image.show() image displays\n{e}') return False def check_file(file): # Search for file if not found if Path(file).is_file() or file == '': return file else: files = glob.glob('./**/' + file, recursive=True) # find file assert len(files), f'File Not Found: {file}' # assert file was found assert len(files) == 1, f"Multiple files match '{file}', specify exact path: {files}" # assert unique return files[0] # return file def check_dataset(dict): # Download dataset if not found locally val, s = dict.get('val'), dict.get('download') if val and len(val): val = [Path(x).resolve() for x in (val if isinstance(val, list) else [val])] # val path if not all(x.exists() for x in val): print('\nWARNING: Dataset not found, nonexistent paths: %s' % [str(x) for x in val if not x.exists()]) if s and len(s): # download script print('Downloading %s ...' % s) if s.startswith('http') and s.endswith('.zip'): # URL f = Path(s).name # filename torch.hub.download_url_to_file(s, f) r = os.system('unzip -q %s -d ../ && rm %s' % (f, f)) # unzip else: # bash script r = os.system(s) print('Dataset autodownload %s\n' % ('success' if r == 0 else 'failure')) # analyze return value else: raise Exception('Dataset not found.') def make_divisible(x, divisor): # Returns x evenly divisible by divisor return math.ceil(x / divisor) * divisor def clean_str(s): # Cleans a string by replacing special characters with underscore _ return re.sub(pattern="[|@#!¡·$€%&()=?¿^*;:,¨´><+]", repl="_", string=s) def one_cycle(y1=0.0, y2=1.0, steps=100): # lambda function for sinusoidal ramp from y1 to y2 return lambda x: ((1 - math.cos(x * math.pi / steps)) / 2) * (y2 - y1) + y1 def colorstr(*input): # Colors a string https://en.wikipedia.org/wiki/ANSI_escape_code, i.e. colorstr('blue', 'hello world') *args, string = input if len(input) > 1 else ('blue', 'bold', input[0]) # color arguments, string colors = {'black': '\033[30m', # basic colors 'red': '\033[31m', 'green': '\033[32m', 'yellow': '\033[33m', 'blue': '\033[34m', 'magenta': '\033[35m', 'cyan': '\033[36m', 'white': '\033[37m', 'bright_black': '\033[90m', # bright colors 'bright_red': '\033[91m', 'bright_green': '\033[92m', 'bright_yellow': '\033[93m', 'bright_blue': '\033[94m', 'bright_magenta': '\033[95m', 'bright_cyan': '\033[96m', 'bright_white': '\033[97m', 'end': '\033[0m', # misc 'bold': '\033[1m', 'underline': '\033[4m'} return ''.join(colors[x] for x in args) + f'{string}' + colors['end'] def labels_to_class_weights(labels, nc=80): # Get class weights (inverse frequency) from training labels if labels[0] is None: # no labels loaded return torch.Tensor() labels = np.concatenate(labels, 0) # labels.shape = (866643, 5) for COCO classes = labels[:, 0].astype(np.int) # labels = [class xywh] weights = np.bincount(classes, minlength=nc) # occurrences per class # Prepend gridpoint count (for uCE training) # gpi = ((320 / 32 * np.array([1, 2, 4])) ** 2 * 3).sum() # gridpoints per image # weights = np.hstack([gpi * len(labels) - weights.sum() * 9, weights * 9]) ** 0.5 # prepend gridpoints to start weights[weights == 0] = 1 # replace empty bins with 1 weights = 1 / weights # number of targets per class weights /= weights.sum() # normalize return torch.from_numpy(weights) def labels_to_image_weights(labels, nc=80, class_weights=np.ones(80)): # Produces image weights based on class_weights and image contents class_counts = np.array([np.bincount(x[:, 0].astype(np.int), minlength=nc) for x in labels]) image_weights = (class_weights.reshape(1, nc) * class_counts).sum(1) # index = random.choices(range(n), weights=image_weights, k=1) # weight image sample return image_weights def coco80_to_coco91_class(): # converts 80-index (val2014) to 91-index (paper) # https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/ # a = np.loadtxt('data/coco.names', dtype='str', delimiter='\n') # b = np.loadtxt('data/coco_paper.names', dtype='str', delimiter='\n') # x1 = [list(a[i] == b).index(True) + 1 for i in range(80)] # darknet to coco # x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)] # coco to darknet x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90] return x def xyxy2xywh(x): # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = (x[:, 0] + x[:, 2]) / 2 # x center y[:, 1] = (x[:, 1] + x[:, 3]) / 2 # y center y[:, 2] = x[:, 2] - x[:, 0] # width y[:, 3] = x[:, 3] - x[:, 1] # height return y def xywh2xyxy(x): # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y return y def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0): # Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = w * (x[:, 0] - x[:, 2] / 2) + padw # top left x y[:, 1] = h * (x[:, 1] - x[:, 3] / 2) + padh # top left y y[:, 2] = w * (x[:, 0] + x[:, 2] / 2) + padw # bottom right x y[:, 3] = h * (x[:, 1] + x[:, 3] / 2) + padh # bottom right y return y def xyn2xy(x, w=640, h=640, padw=0, padh=0): # Convert normalized segments into pixel segments, shape (n,2) y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = w * x[:, 0] + padw # top left x y[:, 1] = h * x[:, 1] + padh # top left y return y def segment2box(segment, width=640, height=640): # Convert 1 segment label to 1 box label, applying inside-image constraint, i.e. (xy1, xy2, ...) to (xyxy) x, y = segment.T # segment xy inside = (x >= 0) & (y >= 0) & (x <= width) & (y <= height) x, y, = x[inside], y[inside] return np.array([x.min(), y.min(), x.max(), y.max()]) if any(x) else np.zeros((1, 4)) # xyxy def segments2boxes(segments): # Convert segment labels to box labels, i.e. (cls, xy1, xy2, ...) to (cls, xywh) boxes = [] for s in segments: x, y = s.T # segment xy boxes.append([x.min(), y.min(), x.max(), y.max()]) # cls, xyxy return xyxy2xywh(np.array(boxes)) # cls, xywh def resample_segments(segments, n=1000): # Up-sample an (n,2) segment for i, s in enumerate(segments): x = np.linspace(0, len(s) - 1, n) xp = np.arange(len(s)) segments[i] = np.concatenate([np.interp(x, xp, s[:, i]) for i in range(2)]).reshape(2, -1).T # segment xy return segments def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None): # Rescale coords (xyxy) from img1_shape to img0_shape if ratio_pad is None: # calculate from img0_shape gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding else: gain = ratio_pad[0][0] pad = ratio_pad[1] coords[:, [0, 2]] -= pad[0] # x padding coords[:, [1, 3]] -= pad[1] # y padding coords[:, :4] /= gain clip_coords(coords, img0_shape) return coords def clip_coords(boxes, img_shape): # Clip bounding xyxy bounding boxes to image shape (height, width) boxes[:, 0].clamp_(0, img_shape[1]) # x1 boxes[:, 1].clamp_(0, img_shape[0]) # y1 boxes[:, 2].clamp_(0, img_shape[1]) # x2 boxes[:, 3].clamp_(0, img_shape[0]) # y2 def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7): # Returns the IoU of box1 to box2. box1 is 4, box2 is nx4 box2 = box2.T # Get the coordinates of bounding boxes if x1y1x2y2: # x1, y1, x2, y2 = box1 b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3] b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3] else: # transform from xywh to xyxy b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2 b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2 b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2 b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2 # Intersection area inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \ (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0) # Union Area w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps union = w1 * h1 + w2 * h2 - inter + eps iou = inter / union if GIoU or DIoU or CIoU: cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1 c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared if DIoU: return iou - rho2 / c2 # DIoU elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47 v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / (h2 + eps)) - torch.atan(w1 / (h1 + eps)), 2) with torch.no_grad(): alpha = v / (v - iou + (1 + eps)) return iou - (rho2 / c2 + v * alpha) # CIoU else: # GIoU https://arxiv.org/pdf/1902.09630.pdf c_area = cw * ch + eps # convex area return iou - (c_area - union) / c_area # GIoU else: return iou # IoU def bbox_alpha_iou(box1, box2, x1y1x2y2=False, GIoU=False, DIoU=False, CIoU=False, alpha=2, eps=1e-9): # Returns tsqrt_he IoU of box1 to box2. box1 is 4, box2 is nx4 box2 = box2.T # Get the coordinates of bounding boxes if x1y1x2y2: # x1, y1, x2, y2 = box1 b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3] b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3] else: # transform from xywh to xyxy b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2 b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2 b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2 b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2 # Intersection area inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \ (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0) # Union Area w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps union = w1 * h1 + w2 * h2 - inter + eps # change iou into pow(iou+eps) # iou = inter / union iou = torch.pow(inter/union + eps, alpha) # beta = 2 * alpha if GIoU or DIoU or CIoU: cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1 c2 = (cw ** 2 + ch ** 2) ** alpha + eps # convex diagonal rho_x = torch.abs(b2_x1 + b2_x2 - b1_x1 - b1_x2) rho_y = torch.abs(b2_y1 + b2_y2 - b1_y1 - b1_y2) rho2 = ((rho_x ** 2 + rho_y ** 2) / 4) ** alpha # center distance if DIoU: return iou - rho2 / c2 # DIoU elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47 v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2) with torch.no_grad(): alpha_ciou = v / ((1 + eps) - inter / union + v) # return iou - (rho2 / c2 + v * alpha_ciou) # CIoU return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)) # CIoU else: # GIoU https://arxiv.org/pdf/1902.09630.pdf # c_area = cw * ch + eps # convex area # return iou - (c_area - union) / c_area # GIoU c_area = torch.max(cw * ch + eps, union) # convex area return iou - torch.pow((c_area - union) / c_area + eps, alpha) # GIoU else: return iou # torch.log(iou+eps) or iou def box_iou(box1, box2): # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Arguments: box1 (Tensor[N, 4]) box2 (Tensor[M, 4]) Returns: iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 """ def box_area(box): # box = 4xn return (box[2] - box[0]) * (box[3] - box[1]) area1 = box_area(box1.T) area2 = box_area(box2.T) # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2) inter = (torch.min(box1[:, None, 2:], box2[:, 2:]) - torch.max(box1[:, None, :2], box2[:, :2])).clamp(0).prod(2) return inter / (area1[:, None] + area2 - inter) # iou = inter / (area1 + area2 - inter) def wh_iou(wh1, wh2): # Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2 wh1 = wh1[:, None] # [N,1,2] wh2 = wh2[None] # [1,M,2] inter = torch.min(wh1, wh2).prod(2) # [N,M] return inter / (wh1.prod(2) + wh2.prod(2) - inter) # iou = inter / (area1 + area2 - inter) def box_giou(box1, box2): """ Return generalized intersection-over-union (Jaccard index) between two sets of boxes. Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with ``0 <= x1 < x2`` and ``0 <= y1 < y2``. Args: boxes1 (Tensor[N, 4]): first set of boxes boxes2 (Tensor[M, 4]): second set of boxes Returns: Tensor[N, M]: the NxM matrix containing the pairwise generalized IoU values for every element in boxes1 and boxes2 """ def box_area(box): # box = 4xn return (box[2] - box[0]) * (box[3] - box[1]) area1 = box_area(box1.T) area2 = box_area(box2.T) inter = (torch.min(box1[:, None, 2:], box2[:, 2:]) - torch.max(box1[:, None, :2], box2[:, :2])).clamp(0).prod(2) union = (area1[:, None] + area2 - inter) iou = inter / union lti = torch.min(box1[:, None, :2], box2[:, :2]) rbi = torch.max(box1[:, None, 2:], box2[:, 2:]) whi = (rbi - lti).clamp(min=0) # [N,M,2] areai = whi[:, :, 0] * whi[:, :, 1] return iou - (areai - union) / areai def box_ciou(box1, box2, eps: float = 1e-7): """ Return complete intersection-over-union (Jaccard index) between two sets of boxes. Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with ``0 <= x1 < x2`` and ``0 <= y1 < y2``. Args: boxes1 (Tensor[N, 4]): first set of boxes boxes2 (Tensor[M, 4]): second set of boxes eps (float, optional): small number to prevent division by zero. Default: 1e-7 Returns: Tensor[N, M]: the NxM matrix containing the pairwise complete IoU values for every element in boxes1 and boxes2 """ def box_area(box): # box = 4xn return (box[2] - box[0]) * (box[3] - box[1]) area1 = box_area(box1.T) area2 = box_area(box2.T) inter = (torch.min(box1[:, None, 2:], box2[:, 2:]) - torch.max(box1[:, None, :2], box2[:, :2])).clamp(0).prod(2) union = (area1[:, None] + area2 - inter) iou = inter / union lti = torch.min(box1[:, None, :2], box2[:, :2]) rbi = torch.max(box1[:, None, 2:], box2[:, 2:]) whi = (rbi - lti).clamp(min=0) # [N,M,2] diagonal_distance_squared = (whi[:, :, 0] ** 2) + (whi[:, :, 1] ** 2) + eps # centers of boxes x_p = (box1[:, None, 0] + box1[:, None, 2]) / 2 y_p = (box1[:, None, 1] + box1[:, None, 3]) / 2 x_g = (box2[:, 0] + box2[:, 2]) / 2 y_g = (box2[:, 1] + box2[:, 3]) / 2 # The distance between boxes' centers squared. centers_distance_squared = (x_p - x_g) ** 2 + (y_p - y_g) ** 2 w_pred = box1[:, None, 2] - box1[:, None, 0] h_pred = box1[:, None, 3] - box1[:, None, 1] w_gt = box2[:, 2] - box2[:, 0] h_gt = box2[:, 3] - box2[:, 1] v = (4 / (torch.pi ** 2)) * torch.pow((torch.atan(w_gt / h_gt) - torch.atan(w_pred / h_pred)), 2) with torch.no_grad(): alpha = v / (1 - iou + v + eps) return iou - (centers_distance_squared / diagonal_distance_squared) - alpha * v def box_diou(box1, box2, eps: float = 1e-7): """ Return distance intersection-over-union (Jaccard index) between two sets of boxes. Both sets of boxes are expected to be in ``(x1, y1, x2, y2)`` format with ``0 <= x1 < x2`` and ``0 <= y1 < y2``. Args: boxes1 (Tensor[N, 4]): first set of boxes boxes2 (Tensor[M, 4]): second set of boxes eps (float, optional): small number to prevent division by zero. Default: 1e-7 Returns: Tensor[N, M]: the NxM matrix containing the pairwise distance IoU values for every element in boxes1 and boxes2 """ def box_area(box): # box = 4xn return (box[2] - box[0]) * (box[3] - box[1]) area1 = box_area(box1.T) area2 = box_area(box2.T) inter = (torch.min(box1[:, None, 2:], box2[:, 2:]) - torch.max(box1[:, None, :2], box2[:, :2])).clamp(0).prod(2) union = (area1[:, None] + area2 - inter) iou = inter / union lti = torch.min(box1[:, None, :2], box2[:, :2]) rbi = torch.max(box1[:, None, 2:], box2[:, 2:]) whi = (rbi - lti).clamp(min=0) # [N,M,2] diagonal_distance_squared = (whi[:, :, 0] ** 2) + (whi[:, :, 1] ** 2) + eps # centers of boxes x_p = (box1[:, None, 0] + box1[:, None, 2]) / 2 y_p = (box1[:, None, 1] + box1[:, None, 3]) / 2 x_g = (box2[:, 0] + box2[:, 2]) / 2 y_g = (box2[:, 1] + box2[:, 3]) / 2 # The distance between boxes' centers squared. centers_distance_squared = (x_p - x_g) ** 2 + (y_p - y_g) ** 2 # The distance IoU is the IoU penalized by a normalized # distance between boxes' centers squared. return iou - (centers_distance_squared / diagonal_distance_squared) def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False, labels=()): """Runs Non-Maximum Suppression (NMS) on inference results Returns: list of detections, on (n,6) tensor per image [xyxy, conf, cls] """ nc = prediction.shape[2] - 5 # number of classes xc = prediction[..., 4] > conf_thres # candidates # Settings min_wh, max_wh = 2, 4096 # (pixels) minimum and maximum box width and height max_det = 300 # maximum number of detections per image max_nms = 30000 # maximum number of boxes into torchvision.ops.nms() time_limit = 10.0 # seconds to quit after redundant = True # require redundant detections multi_label &= nc > 1 # multiple labels per box (adds 0.5ms/img) merge = False # use merge-NMS t = time.time() output = [torch.zeros((0, 6), device=prediction.device)] * prediction.shape[0] for xi, x in enumerate(prediction): # image index, image inference # Apply constraints # x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height x = x[xc[xi]] # confidence # Cat apriori labels if autolabelling if labels and len(labels[xi]): l = labels[xi] v = torch.zeros((len(l), nc + 5), device=x.device) v[:, :4] = l[:, 1:5] # box v[:, 4] = 1.0 # conf v[range(len(l)), l[:, 0].long() + 5] = 1.0 # cls x = torch.cat((x, v), 0) # If none remain process next image if not x.shape[0]: continue # Compute conf x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf # Box (center x, center y, width, height) to (x1, y1, x2, y2) box = xywh2xyxy(x[:, :4]) # Detections matrix nx6 (xyxy, conf, cls) if multi_label: i, j = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T x = torch.cat((box[i], x[i, j + 5, None], j[:, None].float()), 1) else: # best class only conf, j = x[:, 5:].max(1, keepdim=True) x = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres] # Filter by class if classes is not None: x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)] # Apply finite constraint # if not torch.isfinite(x).all(): # x = x[torch.isfinite(x).all(1)] # Check shape n = x.shape[0] # number of boxes if not n: # no boxes continue elif n > max_nms: # excess boxes x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence # Batched NMS c = x[:, 5:6] * (0 if agnostic else max_wh) # classes boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS if i.shape[0] > max_det: # limit detections i = i[:max_det] if merge and (1 < n < 3E3): # Merge NMS (boxes merged using weighted mean) # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4) iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix weights = iou * scores[None] # box weights x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes if redundant: i = i[iou.sum(1) > 1] # require redundancy output[xi] = x[i] if (time.time() - t) > time_limit: print(f'WARNING: NMS time limit {time_limit}s exceeded') break # time limit exceeded return output def non_max_suppression_kpt(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False, labels=(), kpt_label=False, nc=None, nkpt=None): """Runs Non-Maximum Suppression (NMS) on inference results Returns: list of detections, on (n,6) tensor per image [xyxy, conf, cls] """ if nc is None: nc = prediction.shape[2] - 5 if not kpt_label else prediction.shape[2] - 56 # number of classes xc = prediction[..., 4] > conf_thres # candidates # Settings min_wh, max_wh = 2, 4096 # (pixels) minimum and maximum box width and height max_det = 300 # maximum number of detections per image max_nms = 30000 # maximum number of boxes into torchvision.ops.nms() time_limit = 10.0 # seconds to quit after redundant = True # require redundant detections multi_label &= nc > 1 # multiple labels per box (adds 0.5ms/img) merge = False # use merge-NMS t = time.time() output = [torch.zeros((0,6), device=prediction.device)] * prediction.shape[0] for xi, x in enumerate(prediction): # image index, image inference # Apply constraints # x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height x = x[xc[xi]] # confidence # Cat apriori labels if autolabelling if labels and len(labels[xi]): l = labels[xi] v = torch.zeros((len(l), nc + 5), device=x.device) v[:, :4] = l[:, 1:5] # box v[:, 4] = 1.0 # conf v[range(len(l)), l[:, 0].long() + 5] = 1.0 # cls x = torch.cat((x, v), 0) # If none remain process next image if not x.shape[0]: continue # Compute conf x[:, 5:5+nc] *= x[:, 4:5] # conf = obj_conf * cls_conf # Box (center x, center y, width, height) to (x1, y1, x2, y2) box = xywh2xyxy(x[:, :4]) # Detections matrix nx6 (xyxy, conf, cls) if multi_label: i, j = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T x = torch.cat((box[i], x[i, j + 5, None], j[:, None].float()), 1) else: # best class only if not kpt_label: conf, j = x[:, 5:].max(1, keepdim=True) x = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres] else: kpts = x[:, 6:] conf, j = x[:, 5:6].max(1, keepdim=True) x = torch.cat((box, conf, j.float(), kpts), 1)[conf.view(-1) > conf_thres] # Filter by class if classes is not None: x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)] # Apply finite constraint # if not torch.isfinite(x).all(): # x = x[torch.isfinite(x).all(1)] # Check shape n = x.shape[0] # number of boxes if not n: # no boxes continue elif n > max_nms: # excess boxes x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence # Batched NMS c = x[:, 5:6] * (0 if agnostic else max_wh) # classes boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS if i.shape[0] > max_det: # limit detections i = i[:max_det] if merge and (1 < n < 3E3): # Merge NMS (boxes merged using weighted mean) # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4) iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix weights = iou * scores[None] # box weights x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes if redundant: i = i[iou.sum(1) > 1] # require redundancy output[xi] = x[i] if (time.time() - t) > time_limit: print(f'WARNING: NMS time limit {time_limit}s exceeded') break # time limit exceeded return output def strip_optimizer(device='cpu',f='yolov7-w6-pose.pt', s=''): # from utils.general import *; strip_optimizer() # Strip optimizer from 'f' to finalize training, optionally save as 's' x = torch.load(f, map_location=torch.device('cpu')) if x.get('ema'): x['model'] = x['ema'] # replace model with ema for k in 'optimizer', 'training_results', 'wandb_id', 'ema', 'updates': # keys x[k] = None x['epoch'] = -1 if device!='cpu': x['model'].half() # to FP16 else: x['model'].float() for p in x['model'].parameters(): p.requires_grad = False torch.save(x, s or f) mb = os.path.getsize(s or f) / 1E6 # filesize print(f"Optimizer stripped from {f},{(' saved as %s,' % s) if s else ''} {mb:.1f}MB") def print_mutation(hyp, results, yaml_file='hyp_evolved.yaml', bucket=''): # Print mutation results to evolve.txt (for use with train.py --evolve) a = '%10s' * len(hyp) % tuple(hyp.keys()) # hyperparam keys b = '%10.3g' * len(hyp) % tuple(hyp.values()) # hyperparam values c = '%10.4g' * len(results) % results # results (P, R, mAP@0.5, mAP@0.5:0.95, val_losses x 3) print('\n%s\n%s\nEvolved fitness: %s\n' % (a, b, c)) if bucket: url = 'gs://%s/evolve.txt' % bucket if gsutil_getsize(url) > (os.path.getsize('evolve.txt') if os.path.exists('evolve.txt') else 0): os.system('gsutil cp %s .' % url) # download evolve.txt if larger than local with open('evolve.txt', 'a') as f: # append result f.write(c + b + '\n') x = np.unique(np.loadtxt('evolve.txt', ndmin=2), axis=0) # load unique rows x = x[np.argsort(-fitness(x))] # sort np.savetxt('evolve.txt', x, '%10.3g') # save sort by fitness # Save yaml for i, k in enumerate(hyp.keys()): hyp[k] = float(x[0, i + 7]) with open(yaml_file, 'w') as f: results = tuple(x[0, :7]) c = '%10.4g' * len(results) % results # results (P, R, mAP@0.5, mAP@0.5:0.95, val_losses x 3) f.write('# Hyperparameter Evolution Results\n# Generations: %g\n# Metrics: ' % len(x) + c + '\n\n') yaml.dump(hyp, f, sort_keys=False) if bucket: os.system('gsutil cp evolve.txt %s gs://%s' % (yaml_file, bucket)) # upload def apply_classifier(x, model, img, im0): # applies a second stage classifier to yolo outputs im0 = [im0] if isinstance(im0, np.ndarray) else im0 for i, d in enumerate(x): # per image if d is not None and len(d): d = d.clone() # Reshape and pad cutouts b = xyxy2xywh(d[:, :4]) # boxes b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1) # rectangle to square b[:, 2:] = b[:, 2:] * 1.3 + 30 # pad d[:, :4] = xywh2xyxy(b).long() # Rescale boxes from img_size to im0 size scale_coords(img.shape[2:], d[:, :4], im0[i].shape) # Classes pred_cls1 = d[:, 5].long() ims = [] for j, a in enumerate(d): # per item cutout = im0[i][int(a[1]):int(a[3]), int(a[0]):int(a[2])] im = cv2.resize(cutout, (224, 224)) # BGR # cv2.imwrite('test%i.jpg' % j, cutout) im = im[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 im = np.ascontiguousarray(im, dtype=np.float32) # uint8 to float32 im /= 255.0 # 0 - 255 to 0.0 - 1.0 ims.append(im) pred_cls2 = model(torch.Tensor(ims).to(d.device)).argmax(1) # classifier prediction x[i] = x[i][pred_cls1 == pred_cls2] # retain matching class detections return x def increment_path(path, exist_ok=True, sep=''): # Increment path, i.e. runs/exp --> runs/exp{sep}0, runs/exp{sep}1 etc. path = Path(path) # os-agnostic if (path.exists() and exist_ok) or (not path.exists()): return str(path) else: dirs = glob.glob(f"{path}{sep}*") # similar paths matches = [re.search(rf"%s{sep}(\d+)" % path.stem, d) for d in dirs] i = [int(m.groups()[0]) for m in matches if m] # indices n = max(i) + 1 if i else 2 # increment number return f"{path}{sep}{n}" # update path ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/google_utils.py ================================================ # Google utils: https://cloud.google.com/storage/docs/reference/libraries import os import platform import subprocess import time from pathlib import Path import requests import torch def gsutil_getsize(url=''): # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du s = subprocess.check_output(f'gsutil du {url}', shell=True).decode('utf-8') return eval(s.split(' ')[0]) if len(s) else 0 # bytes def attempt_download(file, repo='WongKinYiu/yolov7'): # Attempt file download if does not exist file = Path(str(file).strip().replace("'", '').lower()) if not file.exists(): try: response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest').json() # github api assets = [x['name'] for x in response['assets']] # release assets tag = response['tag_name'] # i.e. 'v1.0' except: # fallback plan assets = ['yolov7.pt'] tag = subprocess.check_output('git tag', shell=True).decode().split()[-1] name = file.name if name in assets: msg = f'{file} missing, try downloading from https://github.com/{repo}/releases/' redundant = False # second download option try: # GitHub url = f'https://github.com/{repo}/releases/download/{tag}/{name}' print(f'Downloading {url} to {file}...') torch.hub.download_url_to_file(url, file) assert file.exists() and file.stat().st_size > 1E6 # check except Exception as e: # GCP print(f'Download error: {e}') assert redundant, 'No secondary mirror' url = f'https://storage.googleapis.com/{repo}/ckpt/{name}' print(f'Downloading {url} to {file}...') os.system(f'curl -L {url} -o {file}') # torch.hub.download_url_to_file(url, weights) finally: if not file.exists() or file.stat().st_size < 1E6: # check file.unlink(missing_ok=True) # remove partial downloads print(f'ERROR: Download failure: {msg}') print('') return def gdrive_download(id='', file='tmp.zip'): # Downloads a file from Google Drive. from yolov7.utils.google_utils import *; gdrive_download() t = time.time() file = Path(file) cookie = Path('cookie') # gdrive cookie print(f'Downloading https://drive.google.com/uc?export=download&id={id} as {file}... ', end='') file.unlink(missing_ok=True) # remove existing file cookie.unlink(missing_ok=True) # remove existing cookie # Attempt file download out = "NUL" if platform.system() == "Windows" else "/dev/null" os.system(f'curl -c ./cookie -s -L "drive.google.com/uc?export=download&id={id}" > {out}') if os.path.exists('cookie'): # large file s = f'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm={get_token()}&id={id}" -o {file}' else: # small file s = f'curl -s -L -o {file} "drive.google.com/uc?export=download&id={id}"' r = os.system(s) # execute, capture return cookie.unlink(missing_ok=True) # remove existing cookie # Error check if r != 0: file.unlink(missing_ok=True) # remove partial print('Download error ') # raise Exception('Download error') return r # Unzip if archive if file.suffix == '.zip': print('unzipping... ', end='') os.system(f'unzip -q {file}') # unzip file.unlink() # remove zip to free space print(f'Done ({time.time() - t:.1f}s)') return r def get_token(cookie="./cookie"): with open(cookie) as f: for line in f: if "download" in line: return line.split()[-1] return "" # def upload_blob(bucket_name, source_file_name, destination_blob_name): # # Uploads a file to a bucket # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python # # storage_client = storage.Client() # bucket = storage_client.get_bucket(bucket_name) # blob = bucket.blob(destination_blob_name) # # blob.upload_from_filename(source_file_name) # # print('File {} uploaded to {}.'.format( # source_file_name, # destination_blob_name)) # # # def download_blob(bucket_name, source_blob_name, destination_file_name): # # Uploads a blob from a bucket # storage_client = storage.Client() # bucket = storage_client.get_bucket(bucket_name) # blob = bucket.blob(source_blob_name) # # blob.download_to_filename(destination_file_name) # # print('Blob {} downloaded to {}.'.format( # source_blob_name, # destination_file_name)) ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/loss.py ================================================ # Loss functions import torch import torch.nn as nn import torch.nn.functional as F from asone.pose_estimators.yolov7_pose.utils.general import bbox_iou, bbox_alpha_iou, box_iou, box_giou, box_diou, box_ciou, xywh2xyxy from asone.pose_estimators.yolov7_pose.utils.torch_utils import is_parallel def smooth_BCE(eps=0.1): # https://github.com/ultralytics/yolov3/issues/238#issuecomment-598028441 # return positive, negative label smoothing BCE targets return 1.0 - 0.5 * eps, 0.5 * eps class BCEBlurWithLogitsLoss(nn.Module): # BCEwithLogitLoss() with reduced missing label effects. def __init__(self, alpha=0.05): super(BCEBlurWithLogitsLoss, self).__init__() self.loss_fcn = nn.BCEWithLogitsLoss(reduction='none') # must be nn.BCEWithLogitsLoss() self.alpha = alpha def forward(self, pred, true): loss = self.loss_fcn(pred, true) pred = torch.sigmoid(pred) # prob from logits dx = pred - true # reduce only missing label effects # dx = (pred - true).abs() # reduce missing label and false label effects alpha_factor = 1 - torch.exp((dx - 1) / (self.alpha + 1e-4)) loss *= alpha_factor return loss.mean() class SigmoidBin(nn.Module): stride = None # strides computed during build export = False # onnx export def __init__(self, bin_count=10, min=0.0, max=1.0, reg_scale = 2.0, use_loss_regression=True, use_fw_regression=True, BCE_weight=1.0, smooth_eps=0.0): super(SigmoidBin, self).__init__() self.bin_count = bin_count self.length = bin_count + 1 self.min = min self.max = max self.scale = float(max - min) self.shift = self.scale / 2.0 self.use_loss_regression = use_loss_regression self.use_fw_regression = use_fw_regression self.reg_scale = reg_scale self.BCE_weight = BCE_weight start = min + (self.scale/2.0) / self.bin_count end = max - (self.scale/2.0) / self.bin_count step = self.scale / self.bin_count self.step = step #print(f" start = {start}, end = {end}, step = {step} ") bins = torch.range(start, end + 0.0001, step).float() self.register_buffer('bins', bins) self.cp = 1.0 - 0.5 * smooth_eps self.cn = 0.5 * smooth_eps self.BCEbins = nn.BCEWithLogitsLoss(pos_weight=torch.Tensor([BCE_weight])) self.MSELoss = nn.MSELoss() def get_length(self): return self.length def forward(self, pred): assert pred.shape[-1] == self.length, 'pred.shape[-1]=%d is not equal to self.length=%d' % (pred.shape[-1], self.length) pred_reg = (pred[..., 0] * self.reg_scale - self.reg_scale/2.0) * self.step pred_bin = pred[..., 1:(1+self.bin_count)] _, bin_idx = torch.max(pred_bin, dim=-1) bin_bias = self.bins[bin_idx] if self.use_fw_regression: result = pred_reg + bin_bias else: result = bin_bias result = result.clamp(min=self.min, max=self.max) return result def training_loss(self, pred, target): assert pred.shape[-1] == self.length, 'pred.shape[-1]=%d is not equal to self.length=%d' % (pred.shape[-1], self.length) assert pred.shape[0] == target.shape[0], 'pred.shape=%d is not equal to the target.shape=%d' % (pred.shape[0], target.shape[0]) device = pred.device pred_reg = (pred[..., 0].sigmoid() * self.reg_scale - self.reg_scale/2.0) * self.step pred_bin = pred[..., 1:(1+self.bin_count)] diff_bin_target = torch.abs(target[..., None] - self.bins) _, bin_idx = torch.min(diff_bin_target, dim=-1) bin_bias = self.bins[bin_idx] bin_bias.requires_grad = False result = pred_reg + bin_bias target_bins = torch.full_like(pred_bin, self.cn, device=device) # targets n = pred.shape[0] target_bins[range(n), bin_idx] = self.cp loss_bin = self.BCEbins(pred_bin, target_bins) # BCE if self.use_loss_regression: loss_regression = self.MSELoss(result, target) # MSE loss = loss_bin + loss_regression else: loss = loss_bin out_result = result.clamp(min=self.min, max=self.max) return loss, out_result class FocalLoss(nn.Module): # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5) def __init__(self, loss_fcn, gamma=1.5, alpha=0.25): super(FocalLoss, self).__init__() self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss() self.gamma = gamma self.alpha = alpha self.reduction = loss_fcn.reduction self.loss_fcn.reduction = 'none' # required to apply FL to each element def forward(self, pred, true): loss = self.loss_fcn(pred, true) # p_t = torch.exp(-loss) # loss *= self.alpha * (1.000001 - p_t) ** self.gamma # non-zero power for gradient stability # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py pred_prob = torch.sigmoid(pred) # prob from logits p_t = true * pred_prob + (1 - true) * (1 - pred_prob) alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha) modulating_factor = (1.0 - p_t) ** self.gamma loss *= alpha_factor * modulating_factor if self.reduction == 'mean': return loss.mean() elif self.reduction == 'sum': return loss.sum() else: # 'none' return loss class QFocalLoss(nn.Module): # Wraps Quality focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5) def __init__(self, loss_fcn, gamma=1.5, alpha=0.25): super(QFocalLoss, self).__init__() self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss() self.gamma = gamma self.alpha = alpha self.reduction = loss_fcn.reduction self.loss_fcn.reduction = 'none' # required to apply FL to each element def forward(self, pred, true): loss = self.loss_fcn(pred, true) pred_prob = torch.sigmoid(pred) # prob from logits alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha) modulating_factor = torch.abs(true - pred_prob) ** self.gamma loss *= alpha_factor * modulating_factor if self.reduction == 'mean': return loss.mean() elif self.reduction == 'sum': return loss.sum() else: # 'none' return loss class RankSort(torch.autograd.Function): @staticmethod def forward(ctx, logits, targets, delta_RS=0.50, eps=1e-10): classification_grads=torch.zeros(logits.shape).cuda() #Filter fg logits fg_labels = (targets > 0.) fg_logits = logits[fg_labels] fg_targets = targets[fg_labels] fg_num = len(fg_logits) #Do not use bg with scores less than minimum fg logit #since changing its score does not have an effect on precision threshold_logit = torch.min(fg_logits)-delta_RS relevant_bg_labels=((targets==0) & (logits>=threshold_logit)) relevant_bg_logits = logits[relevant_bg_labels] relevant_bg_grad=torch.zeros(len(relevant_bg_logits)).cuda() sorting_error=torch.zeros(fg_num).cuda() ranking_error=torch.zeros(fg_num).cuda() fg_grad=torch.zeros(fg_num).cuda() #sort the fg logits order=torch.argsort(fg_logits) #Loops over each positive following the order for ii in order: # Difference Transforms (x_ij) fg_relations=fg_logits-fg_logits[ii] bg_relations=relevant_bg_logits-fg_logits[ii] if delta_RS > 0: fg_relations=torch.clamp(fg_relations/(2*delta_RS)+0.5,min=0,max=1) bg_relations=torch.clamp(bg_relations/(2*delta_RS)+0.5,min=0,max=1) else: fg_relations = (fg_relations >= 0).float() bg_relations = (bg_relations >= 0).float() # Rank of ii among pos and false positive number (bg with larger scores) rank_pos=torch.sum(fg_relations) FP_num=torch.sum(bg_relations) # Rank of ii among all examples rank=rank_pos+FP_num # Ranking error of example ii. target_ranking_error is always 0. (Eq. 7) ranking_error[ii]=FP_num/rank # Current sorting error of example ii. (Eq. 7) current_sorting_error = torch.sum(fg_relations*(1-fg_targets))/rank_pos #Find examples in the target sorted order for example ii iou_relations = (fg_targets >= fg_targets[ii]) target_sorted_order = iou_relations * fg_relations #The rank of ii among positives in sorted order rank_pos_target = torch.sum(target_sorted_order) #Compute target sorting error. (Eq. 8) #Since target ranking error is 0, this is also total target error target_sorting_error= torch.sum(target_sorted_order*(1-fg_targets))/rank_pos_target #Compute sorting error on example ii sorting_error[ii] = current_sorting_error - target_sorting_error #Identity Update for Ranking Error if FP_num > eps: #For ii the update is the ranking error fg_grad[ii] -= ranking_error[ii] #For negatives, distribute error via ranking pmf (i.e. bg_relations/FP_num) relevant_bg_grad += (bg_relations*(ranking_error[ii]/FP_num)) #Find the positives that are misranked (the cause of the error) #These are the ones with smaller IoU but larger logits missorted_examples = (~ iou_relations) * fg_relations #Denominotor of sorting pmf sorting_pmf_denom = torch.sum(missorted_examples) #Identity Update for Sorting Error if sorting_pmf_denom > eps: #For ii the update is the sorting error fg_grad[ii] -= sorting_error[ii] #For positives, distribute error via sorting pmf (i.e. missorted_examples/sorting_pmf_denom) fg_grad += (missorted_examples*(sorting_error[ii]/sorting_pmf_denom)) #Normalize gradients by number of positives classification_grads[fg_labels]= (fg_grad/fg_num) classification_grads[relevant_bg_labels]= (relevant_bg_grad/fg_num) ctx.save_for_backward(classification_grads) return ranking_error.mean(), sorting_error.mean() @staticmethod def backward(ctx, out_grad1, out_grad2): g1, =ctx.saved_tensors return g1*out_grad1, None, None, None class aLRPLoss(torch.autograd.Function): @staticmethod def forward(ctx, logits, targets, regression_losses, delta=1., eps=1e-5): classification_grads=torch.zeros(logits.shape).cuda() #Filter fg logits fg_labels = (targets == 1) fg_logits = logits[fg_labels] fg_num = len(fg_logits) #Do not use bg with scores less than minimum fg logit #since changing its score does not have an effect on precision threshold_logit = torch.min(fg_logits)-delta #Get valid bg logits relevant_bg_labels=((targets==0)&(logits>=threshold_logit)) relevant_bg_logits=logits[relevant_bg_labels] relevant_bg_grad=torch.zeros(len(relevant_bg_logits)).cuda() rank=torch.zeros(fg_num).cuda() prec=torch.zeros(fg_num).cuda() fg_grad=torch.zeros(fg_num).cuda() max_prec=0 #sort the fg logits order=torch.argsort(fg_logits) #Loops over each positive following the order for ii in order: #x_ij s as score differences with fgs fg_relations=fg_logits-fg_logits[ii] #Apply piecewise linear function and determine relations with fgs fg_relations=torch.clamp(fg_relations/(2*delta)+0.5,min=0,max=1) #Discard i=j in the summation in rank_pos fg_relations[ii]=0 #x_ij s as score differences with bgs bg_relations=relevant_bg_logits-fg_logits[ii] #Apply piecewise linear function and determine relations with bgs bg_relations=torch.clamp(bg_relations/(2*delta)+0.5,min=0,max=1) #Compute the rank of the example within fgs and number of bgs with larger scores rank_pos=1+torch.sum(fg_relations) FP_num=torch.sum(bg_relations) #Store the total since it is normalizer also for aLRP Regression error rank[ii]=rank_pos+FP_num #Compute precision for this example to compute classification loss prec[ii]=rank_pos/rank[ii] #For stability, set eps to a infinitesmall value (e.g. 1e-6), then compute grads if FP_num > eps: fg_grad[ii] = -(torch.sum(fg_relations*regression_losses)+FP_num)/rank[ii] relevant_bg_grad += (bg_relations*(-fg_grad[ii]/FP_num)) #aLRP with grad formulation fg gradient classification_grads[fg_labels]= fg_grad #aLRP with grad formulation bg gradient classification_grads[relevant_bg_labels]= relevant_bg_grad classification_grads /= (fg_num) cls_loss=1-prec.mean() ctx.save_for_backward(classification_grads) return cls_loss, rank, order @staticmethod def backward(ctx, out_grad1, out_grad2, out_grad3): g1, =ctx.saved_tensors return g1*out_grad1, None, None, None, None class APLoss(torch.autograd.Function): @staticmethod def forward(ctx, logits, targets, delta=1.): classification_grads=torch.zeros(logits.shape).cuda() #Filter fg logits fg_labels = (targets == 1) fg_logits = logits[fg_labels] fg_num = len(fg_logits) #Do not use bg with scores less than minimum fg logit #since changing its score does not have an effect on precision threshold_logit = torch.min(fg_logits)-delta #Get valid bg logits relevant_bg_labels=((targets==0)&(logits>=threshold_logit)) relevant_bg_logits=logits[relevant_bg_labels] relevant_bg_grad=torch.zeros(len(relevant_bg_logits)).cuda() rank=torch.zeros(fg_num).cuda() prec=torch.zeros(fg_num).cuda() fg_grad=torch.zeros(fg_num).cuda() max_prec=0 #sort the fg logits order=torch.argsort(fg_logits) #Loops over each positive following the order for ii in order: #x_ij s as score differences with fgs fg_relations=fg_logits-fg_logits[ii] #Apply piecewise linear function and determine relations with fgs fg_relations=torch.clamp(fg_relations/(2*delta)+0.5,min=0,max=1) #Discard i=j in the summation in rank_pos fg_relations[ii]=0 #x_ij s as score differences with bgs bg_relations=relevant_bg_logits-fg_logits[ii] #Apply piecewise linear function and determine relations with bgs bg_relations=torch.clamp(bg_relations/(2*delta)+0.5,min=0,max=1) #Compute the rank of the example within fgs and number of bgs with larger scores rank_pos=1+torch.sum(fg_relations) FP_num=torch.sum(bg_relations) #Store the total since it is normalizer also for aLRP Regression error rank[ii]=rank_pos+FP_num #Compute precision for this example current_prec=rank_pos/rank[ii] #Compute interpolated AP and store gradients for relevant bg examples if (max_prec<=current_prec): max_prec=current_prec relevant_bg_grad += (bg_relations/rank[ii]) else: relevant_bg_grad += (bg_relations/rank[ii])*(((1-max_prec)/(1-current_prec))) #Store fg gradients fg_grad[ii]=-(1-max_prec) prec[ii]=max_prec #aLRP with grad formulation fg gradient classification_grads[fg_labels]= fg_grad #aLRP with grad formulation bg gradient classification_grads[relevant_bg_labels]= relevant_bg_grad classification_grads /= fg_num cls_loss=1-prec.mean() ctx.save_for_backward(classification_grads) return cls_loss @staticmethod def backward(ctx, out_grad1): g1, =ctx.saved_tensors return g1*out_grad1, None, None class ComputeLoss: # Compute losses def __init__(self, model, autobalance=False): super(ComputeLoss, self).__init__() device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['cls_pw']], device=device)) BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device)) # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) # positive, negative BCE targets # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g) det = model.module.model[-1] if is_parallel(model) else model.model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(det.nl, [4.0, 1.0, 0.25, 0.06, .02]) # P3-P7 #self.balance = {3: [4.0, 1.0, 0.4]}.get(det.nl, [4.0, 1.0, 0.25, 0.1, .05]) # P3-P7 #self.balance = {3: [4.0, 1.0, 0.4]}.get(det.nl, [4.0, 1.0, 0.5, 0.4, .1]) # P3-P7 self.ssi = list(det.stride).index(16) if autobalance else 0 # stride 16 index self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, model.gr, h, autobalance for k in 'na', 'nc', 'nl', 'anchors': setattr(self, k, getattr(det, k)) def __call__(self, p, targets): # predictions, targets, model device = targets.device lcls, lbox, lobj = torch.zeros(1, device=device), torch.zeros(1, device=device), torch.zeros(1, device=device) tcls, tbox, indices, anchors = self.build_targets(p, targets) # targets # Losses for i, pi in enumerate(p): # layer index, layer predictions b, a, gj, gi = indices[i] # image, anchor, gridy, gridx tobj = torch.zeros_like(pi[..., 0], device=device) # target obj n = b.shape[0] # number of targets if n: ps = pi[b, a, gj, gi] # prediction subset corresponding to targets # Regression pxy = ps[:, :2].sigmoid() * 2. - 0.5 pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i] pbox = torch.cat((pxy, pwh), 1) # predicted box iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True) # iou(prediction, target) lbox += (1.0 - iou).mean() # iou loss # Objectness tobj[b, a, gj, gi] = (1.0 - self.gr) + self.gr * iou.detach().clamp(0).type(tobj.dtype) # iou ratio # Classification if self.nc > 1: # cls loss (only if multiple classes) t = torch.full_like(ps[:, 5:], self.cn, device=device) # targets t[range(n), tcls[i]] = self.cp #t[t==self.cp] = iou.detach().clamp(0).type(t.dtype) lcls += self.BCEcls(ps[:, 5:], t) # BCE # Append targets to text file # with open('targets.txt', 'a') as file: # [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)] obji = self.BCEobj(pi[..., 4], tobj) lobj += obji * self.balance[i] # obj loss if self.autobalance: self.balance[i] = self.balance[i] * 0.9999 + 0.0001 / obji.detach().item() if self.autobalance: self.balance = [x / self.balance[self.ssi] for x in self.balance] lbox *= self.hyp['box'] lobj *= self.hyp['obj'] lcls *= self.hyp['cls'] bs = tobj.shape[0] # batch size loss = lbox + lobj + lcls return loss * bs, torch.cat((lbox, lobj, lcls, loss)).detach() def build_targets(self, p, targets): # Build targets for compute_loss(), input targets(image,class,x,y,w,h) na, nt = self.na, targets.shape[0] # number of anchors, targets tcls, tbox, indices, anch = [], [], [], [] gain = torch.ones(7, device=targets.device).long() # normalized to gridspace gain ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt) targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices g = 0.5 # bias off = torch.tensor([[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m # [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm ], device=targets.device).float() * g # offsets for i in range(self.nl): anchors = self.anchors[i] gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain # Match targets to anchors t = targets * gain if nt: # Matches r = t[:, :, 4:6] / anchors[:, None] # wh ratio j = torch.max(r, 1. / r).max(2)[0] < self.hyp['anchor_t'] # compare # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2)) t = t[j] # filter # Offsets gxy = t[:, 2:4] # grid xy gxi = gain[[2, 3]] - gxy # inverse j, k = ((gxy % 1. < g) & (gxy > 1.)).T l, m = ((gxi % 1. < g) & (gxi > 1.)).T j = torch.stack((torch.ones_like(j), j, k, l, m)) t = t.repeat((5, 1, 1))[j] offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j] else: t = targets[0] offsets = 0 # Define b, c = t[:, :2].long().T # image, class gxy = t[:, 2:4] # grid xy gwh = t[:, 4:6] # grid wh gij = (gxy - offsets).long() gi, gj = gij.T # grid xy indices # Append a = t[:, 6].long() # anchor indices indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices tbox.append(torch.cat((gxy - gij, gwh), 1)) # box anch.append(anchors[a]) # anchors tcls.append(c) # class return tcls, tbox, indices, anch class ComputeLossOTA: # Compute losses def __init__(self, model, autobalance=False): super(ComputeLossOTA, self).__init__() device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['cls_pw']], device=device)) BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device)) # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) # positive, negative BCE targets # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g) det = model.module.model[-1] if is_parallel(model) else model.model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(det.nl, [4.0, 1.0, 0.25, 0.06, .02]) # P3-P7 self.ssi = list(det.stride).index(16) if autobalance else 0 # stride 16 index self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, model.gr, h, autobalance for k in 'na', 'nc', 'nl', 'anchors', 'stride': setattr(self, k, getattr(det, k)) def __call__(self, p, targets, imgs): # predictions, targets, model device = targets.device lcls, lbox, lobj = torch.zeros(1, device=device), torch.zeros(1, device=device), torch.zeros(1, device=device) bs, as_, gjs, gis, targets, anchors = self.build_targets(p, targets, imgs) pre_gen_gains = [torch.tensor(pp.shape, device=device)[[3, 2, 3, 2]] for pp in p] # Losses for i, pi in enumerate(p): # layer index, layer predictions b, a, gj, gi = bs[i], as_[i], gjs[i], gis[i] # image, anchor, gridy, gridx tobj = torch.zeros_like(pi[..., 0], device=device) # target obj n = b.shape[0] # number of targets if n: ps = pi[b, a, gj, gi] # prediction subset corresponding to targets # Regression grid = torch.stack([gi, gj], dim=1) pxy = ps[:, :2].sigmoid() * 2. - 0.5 #pxy = ps[:, :2].sigmoid() * 3. - 1. pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i] pbox = torch.cat((pxy, pwh), 1) # predicted box selected_tbox = targets[i][:, 2:6] * pre_gen_gains[i] selected_tbox[:, :2] -= grid iou = bbox_iou(pbox.T, selected_tbox, x1y1x2y2=False, CIoU=True) # iou(prediction, target) lbox += (1.0 - iou).mean() # iou loss # Objectness tobj[b, a, gj, gi] = (1.0 - self.gr) + self.gr * iou.detach().clamp(0).type(tobj.dtype) # iou ratio # Classification selected_tcls = targets[i][:, 1].long() if self.nc > 1: # cls loss (only if multiple classes) t = torch.full_like(ps[:, 5:], self.cn, device=device) # targets t[range(n), selected_tcls] = self.cp lcls += self.BCEcls(ps[:, 5:], t) # BCE # Append targets to text file # with open('targets.txt', 'a') as file: # [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)] obji = self.BCEobj(pi[..., 4], tobj) lobj += obji * self.balance[i] # obj loss if self.autobalance: self.balance[i] = self.balance[i] * 0.9999 + 0.0001 / obji.detach().item() if self.autobalance: self.balance = [x / self.balance[self.ssi] for x in self.balance] lbox *= self.hyp['box'] lobj *= self.hyp['obj'] lcls *= self.hyp['cls'] bs = tobj.shape[0] # batch size loss = lbox + lobj + lcls return loss * bs, torch.cat((lbox, lobj, lcls, loss)).detach() def build_targets(self, p, targets, imgs): #indices, anch = self.find_positive(p, targets) indices, anch = self.find_3_positive(p, targets) #indices, anch = self.find_4_positive(p, targets) #indices, anch = self.find_5_positive(p, targets) #indices, anch = self.find_9_positive(p, targets) matching_bs = [[] for pp in p] matching_as = [[] for pp in p] matching_gjs = [[] for pp in p] matching_gis = [[] for pp in p] matching_targets = [[] for pp in p] matching_anchs = [[] for pp in p] nl = len(p) for batch_idx in range(p[0].shape[0]): b_idx = targets[:, 0]==batch_idx this_target = targets[b_idx] if this_target.shape[0] == 0: continue txywh = this_target[:, 2:6] * imgs[batch_idx].shape[1] txyxy = xywh2xyxy(txywh) pxyxys = [] p_cls = [] p_obj = [] from_which_layer = [] all_b = [] all_a = [] all_gj = [] all_gi = [] all_anch = [] for i, pi in enumerate(p): b, a, gj, gi = indices[i] idx = (b == batch_idx) b, a, gj, gi = b[idx], a[idx], gj[idx], gi[idx] all_b.append(b) all_a.append(a) all_gj.append(gj) all_gi.append(gi) all_anch.append(anch[i][idx]) from_which_layer.append(torch.ones(size=(len(b),)) * i) fg_pred = pi[b, a, gj, gi] p_obj.append(fg_pred[:, 4:5]) p_cls.append(fg_pred[:, 5:]) grid = torch.stack([gi, gj], dim=1) pxy = (fg_pred[:, :2].sigmoid() * 2. - 0.5 + grid) * self.stride[i] #/ 8. #pxy = (fg_pred[:, :2].sigmoid() * 3. - 1. + grid) * self.stride[i] pwh = (fg_pred[:, 2:4].sigmoid() * 2) ** 2 * anch[i][idx] * self.stride[i] #/ 8. pxywh = torch.cat([pxy, pwh], dim=-1) pxyxy = xywh2xyxy(pxywh) pxyxys.append(pxyxy) pxyxys = torch.cat(pxyxys, dim=0) if pxyxys.shape[0] == 0: continue p_obj = torch.cat(p_obj, dim=0) p_cls = torch.cat(p_cls, dim=0) from_which_layer = torch.cat(from_which_layer, dim=0) all_b = torch.cat(all_b, dim=0) all_a = torch.cat(all_a, dim=0) all_gj = torch.cat(all_gj, dim=0) all_gi = torch.cat(all_gi, dim=0) all_anch = torch.cat(all_anch, dim=0) pair_wise_iou = box_iou(txyxy, pxyxys) pair_wise_iou_loss = -torch.log(pair_wise_iou + 1e-8) top_k, _ = torch.topk(pair_wise_iou, min(10, pair_wise_iou.shape[1]), dim=1) dynamic_ks = torch.clamp(top_k.sum(1).int(), min=1) gt_cls_per_image = ( F.one_hot(this_target[:, 1].to(torch.int64), self.nc) .float() .unsqueeze(1) .repeat(1, pxyxys.shape[0], 1) ) num_gt = this_target.shape[0] cls_preds_ = ( p_cls.float().unsqueeze(0).repeat(num_gt, 1, 1).sigmoid_() * p_obj.unsqueeze(0).repeat(num_gt, 1, 1).sigmoid_() ) y = cls_preds_.sqrt_() pair_wise_cls_loss = F.binary_cross_entropy_with_logits( torch.log(y/(1-y)) , gt_cls_per_image, reduction="none" ).sum(-1) del cls_preds_ cost = ( pair_wise_cls_loss + 3.0 * pair_wise_iou_loss ) matching_matrix = torch.zeros_like(cost) for gt_idx in range(num_gt): _, pos_idx = torch.topk( cost[gt_idx], k=dynamic_ks[gt_idx].item(), largest=False ) matching_matrix[gt_idx][pos_idx] = 1.0 del top_k, dynamic_ks anchor_matching_gt = matching_matrix.sum(0) if (anchor_matching_gt > 1).sum() > 0: _, cost_argmin = torch.min(cost[:, anchor_matching_gt > 1], dim=0) matching_matrix[:, anchor_matching_gt > 1] *= 0.0 matching_matrix[cost_argmin, anchor_matching_gt > 1] = 1.0 fg_mask_inboxes = matching_matrix.sum(0) > 0.0 matched_gt_inds = matching_matrix[:, fg_mask_inboxes].argmax(0) from_which_layer = from_which_layer[fg_mask_inboxes] all_b = all_b[fg_mask_inboxes] all_a = all_a[fg_mask_inboxes] all_gj = all_gj[fg_mask_inboxes] all_gi = all_gi[fg_mask_inboxes] all_anch = all_anch[fg_mask_inboxes] this_target = this_target[matched_gt_inds] for i in range(nl): layer_idx = from_which_layer == i matching_bs[i].append(all_b[layer_idx]) matching_as[i].append(all_a[layer_idx]) matching_gjs[i].append(all_gj[layer_idx]) matching_gis[i].append(all_gi[layer_idx]) matching_targets[i].append(this_target[layer_idx]) matching_anchs[i].append(all_anch[layer_idx]) for i in range(nl): if matching_targets[i] != []: matching_bs[i] = torch.cat(matching_bs[i], dim=0) matching_as[i] = torch.cat(matching_as[i], dim=0) matching_gjs[i] = torch.cat(matching_gjs[i], dim=0) matching_gis[i] = torch.cat(matching_gis[i], dim=0) matching_targets[i] = torch.cat(matching_targets[i], dim=0) matching_anchs[i] = torch.cat(matching_anchs[i], dim=0) else: matching_bs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_as[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_gjs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_gis[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_targets[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_anchs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) return matching_bs, matching_as, matching_gjs, matching_gis, matching_targets, matching_anchs def find_3_positive(self, p, targets): # Build targets for compute_loss(), input targets(image,class,x,y,w,h) na, nt = self.na, targets.shape[0] # number of anchors, targets indices, anch = [], [] gain = torch.ones(7, device=targets.device).long() # normalized to gridspace gain ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt) targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices g = 0.5 # bias off = torch.tensor([[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m # [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm ], device=targets.device).float() * g # offsets for i in range(self.nl): anchors = self.anchors[i] gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain # Match targets to anchors t = targets * gain if nt: # Matches r = t[:, :, 4:6] / anchors[:, None] # wh ratio j = torch.max(r, 1. / r).max(2)[0] < self.hyp['anchor_t'] # compare # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2)) t = t[j] # filter # Offsets gxy = t[:, 2:4] # grid xy gxi = gain[[2, 3]] - gxy # inverse j, k = ((gxy % 1. < g) & (gxy > 1.)).T l, m = ((gxi % 1. < g) & (gxi > 1.)).T j = torch.stack((torch.ones_like(j), j, k, l, m)) t = t.repeat((5, 1, 1))[j] offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j] else: t = targets[0] offsets = 0 # Define b, c = t[:, :2].long().T # image, class gxy = t[:, 2:4] # grid xy gwh = t[:, 4:6] # grid wh gij = (gxy - offsets).long() gi, gj = gij.T # grid xy indices # Append a = t[:, 6].long() # anchor indices indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices anch.append(anchors[a]) # anchors return indices, anch class ComputeLossBinOTA: # Compute losses def __init__(self, model, autobalance=False): super(ComputeLossBinOTA, self).__init__() device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['cls_pw']], device=device)) BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device)) #MSEangle = nn.MSELoss().to(device) # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) # positive, negative BCE targets # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g) det = model.module.model[-1] if is_parallel(model) else model.model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(det.nl, [4.0, 1.0, 0.25, 0.06, .02]) # P3-P7 self.ssi = list(det.stride).index(16) if autobalance else 0 # stride 16 index self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, model.gr, h, autobalance for k in 'na', 'nc', 'nl', 'anchors', 'stride', 'bin_count': setattr(self, k, getattr(det, k)) #xy_bin_sigmoid = SigmoidBin(bin_count=11, min=-0.5, max=1.5, use_loss_regression=False).to(device) wh_bin_sigmoid = SigmoidBin(bin_count=self.bin_count, min=0.0, max=4.0, use_loss_regression=False).to(device) #angle_bin_sigmoid = SigmoidBin(bin_count=31, min=-1.1, max=1.1, use_loss_regression=False).to(device) self.wh_bin_sigmoid = wh_bin_sigmoid def __call__(self, p, targets, imgs): # predictions, targets, model device = targets.device lcls, lbox, lobj = torch.zeros(1, device=device), torch.zeros(1, device=device), torch.zeros(1, device=device) bs, as_, gjs, gis, targets, anchors = self.build_targets(p, targets, imgs) pre_gen_gains = [torch.tensor(pp.shape, device=device)[[3, 2, 3, 2]] for pp in p] # Losses for i, pi in enumerate(p): # layer index, layer predictions b, a, gj, gi = bs[i], as_[i], gjs[i], gis[i] # image, anchor, gridy, gridx tobj = torch.zeros_like(pi[..., 0], device=device) # target obj obj_idx = self.wh_bin_sigmoid.get_length()*2 + 2 # x,y, w-bce, h-bce # xy_bin_sigmoid.get_length()*2 n = b.shape[0] # number of targets if n: ps = pi[b, a, gj, gi] # prediction subset corresponding to targets # Regression grid = torch.stack([gi, gj], dim=1) selected_tbox = targets[i][:, 2:6] * pre_gen_gains[i] selected_tbox[:, :2] -= grid #pxy = ps[:, :2].sigmoid() * 2. - 0.5 ##pxy = ps[:, :2].sigmoid() * 3. - 1. #pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i] #pbox = torch.cat((pxy, pwh), 1) # predicted box #x_loss, px = xy_bin_sigmoid.training_loss(ps[..., 0:12], tbox[i][..., 0]) #y_loss, py = xy_bin_sigmoid.training_loss(ps[..., 12:24], tbox[i][..., 1]) w_loss, pw = self.wh_bin_sigmoid.training_loss(ps[..., 2:(3+self.bin_count)], selected_tbox[..., 2] / anchors[i][..., 0]) h_loss, ph = self.wh_bin_sigmoid.training_loss(ps[..., (3+self.bin_count):obj_idx], selected_tbox[..., 3] / anchors[i][..., 1]) pw *= anchors[i][..., 0] ph *= anchors[i][..., 1] px = ps[:, 0].sigmoid() * 2. - 0.5 py = ps[:, 1].sigmoid() * 2. - 0.5 lbox += w_loss + h_loss # + x_loss + y_loss #print(f"\n px = {px.shape}, py = {py.shape}, pw = {pw.shape}, ph = {ph.shape} \n") pbox = torch.cat((px.unsqueeze(1), py.unsqueeze(1), pw.unsqueeze(1), ph.unsqueeze(1)), 1).to(device) # predicted box iou = bbox_iou(pbox.T, selected_tbox, x1y1x2y2=False, CIoU=True) # iou(prediction, target) lbox += (1.0 - iou).mean() # iou loss # Objectness tobj[b, a, gj, gi] = (1.0 - self.gr) + self.gr * iou.detach().clamp(0).type(tobj.dtype) # iou ratio # Classification selected_tcls = targets[i][:, 1].long() if self.nc > 1: # cls loss (only if multiple classes) t = torch.full_like(ps[:, (1+obj_idx):], self.cn, device=device) # targets t[range(n), selected_tcls] = self.cp lcls += self.BCEcls(ps[:, (1+obj_idx):], t) # BCE # Append targets to text file # with open('targets.txt', 'a') as file: # [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)] obji = self.BCEobj(pi[..., obj_idx], tobj) lobj += obji * self.balance[i] # obj loss if self.autobalance: self.balance[i] = self.balance[i] * 0.9999 + 0.0001 / obji.detach().item() if self.autobalance: self.balance = [x / self.balance[self.ssi] for x in self.balance] lbox *= self.hyp['box'] lobj *= self.hyp['obj'] lcls *= self.hyp['cls'] bs = tobj.shape[0] # batch size loss = lbox + lobj + lcls return loss * bs, torch.cat((lbox, lobj, lcls, loss)).detach() def build_targets(self, p, targets, imgs): #indices, anch = self.find_positive(p, targets) indices, anch = self.find_3_positive(p, targets) #indices, anch = self.find_4_positive(p, targets) #indices, anch = self.find_5_positive(p, targets) #indices, anch = self.find_9_positive(p, targets) matching_bs = [[] for pp in p] matching_as = [[] for pp in p] matching_gjs = [[] for pp in p] matching_gis = [[] for pp in p] matching_targets = [[] for pp in p] matching_anchs = [[] for pp in p] nl = len(p) for batch_idx in range(p[0].shape[0]): b_idx = targets[:, 0]==batch_idx this_target = targets[b_idx] if this_target.shape[0] == 0: continue txywh = this_target[:, 2:6] * imgs[batch_idx].shape[1] txyxy = xywh2xyxy(txywh) pxyxys = [] p_cls = [] p_obj = [] from_which_layer = [] all_b = [] all_a = [] all_gj = [] all_gi = [] all_anch = [] for i, pi in enumerate(p): obj_idx = self.wh_bin_sigmoid.get_length()*2 + 2 b, a, gj, gi = indices[i] idx = (b == batch_idx) b, a, gj, gi = b[idx], a[idx], gj[idx], gi[idx] all_b.append(b) all_a.append(a) all_gj.append(gj) all_gi.append(gi) all_anch.append(anch[i][idx]) from_which_layer.append(torch.ones(size=(len(b),)) * i) fg_pred = pi[b, a, gj, gi] p_obj.append(fg_pred[:, obj_idx:(obj_idx+1)]) p_cls.append(fg_pred[:, (obj_idx+1):]) grid = torch.stack([gi, gj], dim=1) pxy = (fg_pred[:, :2].sigmoid() * 2. - 0.5 + grid) * self.stride[i] #/ 8. #pwh = (fg_pred[:, 2:4].sigmoid() * 2) ** 2 * anch[i][idx] * self.stride[i] #/ 8. pw = self.wh_bin_sigmoid.forward(fg_pred[..., 2:(3+self.bin_count)].sigmoid()) * anch[i][idx][:, 0] * self.stride[i] ph = self.wh_bin_sigmoid.forward(fg_pred[..., (3+self.bin_count):obj_idx].sigmoid()) * anch[i][idx][:, 1] * self.stride[i] pxywh = torch.cat([pxy, pw.unsqueeze(1), ph.unsqueeze(1)], dim=-1) pxyxy = xywh2xyxy(pxywh) pxyxys.append(pxyxy) pxyxys = torch.cat(pxyxys, dim=0) if pxyxys.shape[0] == 0: continue p_obj = torch.cat(p_obj, dim=0) p_cls = torch.cat(p_cls, dim=0) from_which_layer = torch.cat(from_which_layer, dim=0) all_b = torch.cat(all_b, dim=0) all_a = torch.cat(all_a, dim=0) all_gj = torch.cat(all_gj, dim=0) all_gi = torch.cat(all_gi, dim=0) all_anch = torch.cat(all_anch, dim=0) pair_wise_iou = box_iou(txyxy, pxyxys) pair_wise_iou_loss = -torch.log(pair_wise_iou + 1e-8) top_k, _ = torch.topk(pair_wise_iou, min(10, pair_wise_iou.shape[1]), dim=1) dynamic_ks = torch.clamp(top_k.sum(1).int(), min=1) gt_cls_per_image = ( F.one_hot(this_target[:, 1].to(torch.int64), self.nc) .float() .unsqueeze(1) .repeat(1, pxyxys.shape[0], 1) ) num_gt = this_target.shape[0] cls_preds_ = ( p_cls.float().unsqueeze(0).repeat(num_gt, 1, 1).sigmoid_() * p_obj.unsqueeze(0).repeat(num_gt, 1, 1).sigmoid_() ) y = cls_preds_.sqrt_() pair_wise_cls_loss = F.binary_cross_entropy_with_logits( torch.log(y/(1-y)) , gt_cls_per_image, reduction="none" ).sum(-1) del cls_preds_ cost = ( pair_wise_cls_loss + 3.0 * pair_wise_iou_loss ) matching_matrix = torch.zeros_like(cost) for gt_idx in range(num_gt): _, pos_idx = torch.topk( cost[gt_idx], k=dynamic_ks[gt_idx].item(), largest=False ) matching_matrix[gt_idx][pos_idx] = 1.0 del top_k, dynamic_ks anchor_matching_gt = matching_matrix.sum(0) if (anchor_matching_gt > 1).sum() > 0: _, cost_argmin = torch.min(cost[:, anchor_matching_gt > 1], dim=0) matching_matrix[:, anchor_matching_gt > 1] *= 0.0 matching_matrix[cost_argmin, anchor_matching_gt > 1] = 1.0 fg_mask_inboxes = matching_matrix.sum(0) > 0.0 matched_gt_inds = matching_matrix[:, fg_mask_inboxes].argmax(0) from_which_layer = from_which_layer[fg_mask_inboxes] all_b = all_b[fg_mask_inboxes] all_a = all_a[fg_mask_inboxes] all_gj = all_gj[fg_mask_inboxes] all_gi = all_gi[fg_mask_inboxes] all_anch = all_anch[fg_mask_inboxes] this_target = this_target[matched_gt_inds] for i in range(nl): layer_idx = from_which_layer == i matching_bs[i].append(all_b[layer_idx]) matching_as[i].append(all_a[layer_idx]) matching_gjs[i].append(all_gj[layer_idx]) matching_gis[i].append(all_gi[layer_idx]) matching_targets[i].append(this_target[layer_idx]) matching_anchs[i].append(all_anch[layer_idx]) for i in range(nl): if matching_targets[i] != []: matching_bs[i] = torch.cat(matching_bs[i], dim=0) matching_as[i] = torch.cat(matching_as[i], dim=0) matching_gjs[i] = torch.cat(matching_gjs[i], dim=0) matching_gis[i] = torch.cat(matching_gis[i], dim=0) matching_targets[i] = torch.cat(matching_targets[i], dim=0) matching_anchs[i] = torch.cat(matching_anchs[i], dim=0) else: matching_bs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_as[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_gjs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_gis[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_targets[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_anchs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) return matching_bs, matching_as, matching_gjs, matching_gis, matching_targets, matching_anchs def find_3_positive(self, p, targets): # Build targets for compute_loss(), input targets(image,class,x,y,w,h) na, nt = self.na, targets.shape[0] # number of anchors, targets indices, anch = [], [] gain = torch.ones(7, device=targets.device).long() # normalized to gridspace gain ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt) targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices g = 0.5 # bias off = torch.tensor([[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m # [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm ], device=targets.device).float() * g # offsets for i in range(self.nl): anchors = self.anchors[i] gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain # Match targets to anchors t = targets * gain if nt: # Matches r = t[:, :, 4:6] / anchors[:, None] # wh ratio j = torch.max(r, 1. / r).max(2)[0] < self.hyp['anchor_t'] # compare # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2)) t = t[j] # filter # Offsets gxy = t[:, 2:4] # grid xy gxi = gain[[2, 3]] - gxy # inverse j, k = ((gxy % 1. < g) & (gxy > 1.)).T l, m = ((gxi % 1. < g) & (gxi > 1.)).T j = torch.stack((torch.ones_like(j), j, k, l, m)) t = t.repeat((5, 1, 1))[j] offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j] else: t = targets[0] offsets = 0 # Define b, c = t[:, :2].long().T # image, class gxy = t[:, 2:4] # grid xy gwh = t[:, 4:6] # grid wh gij = (gxy - offsets).long() gi, gj = gij.T # grid xy indices # Append a = t[:, 6].long() # anchor indices indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices anch.append(anchors[a]) # anchors return indices, anch class ComputeLossAuxOTA: # Compute losses def __init__(self, model, autobalance=False): super(ComputeLossAuxOTA, self).__init__() device = next(model.parameters()).device # get model device h = model.hyp # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['cls_pw']], device=device)) BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device)) # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) # positive, negative BCE targets # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g) det = model.module.model[-1] if is_parallel(model) else model.model[-1] # Detect() module self.balance = {3: [4.0, 1.0, 0.4]}.get(det.nl, [4.0, 1.0, 0.25, 0.06, .02]) # P3-P7 self.ssi = list(det.stride).index(16) if autobalance else 0 # stride 16 index self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, model.gr, h, autobalance for k in 'na', 'nc', 'nl', 'anchors', 'stride': setattr(self, k, getattr(det, k)) def __call__(self, p, targets, imgs): # predictions, targets, model device = targets.device lcls, lbox, lobj = torch.zeros(1, device=device), torch.zeros(1, device=device), torch.zeros(1, device=device) bs_aux, as_aux_, gjs_aux, gis_aux, targets_aux, anchors_aux = self.build_targets2(p[:self.nl], targets, imgs) bs, as_, gjs, gis, targets, anchors = self.build_targets(p[:self.nl], targets, imgs) pre_gen_gains_aux = [torch.tensor(pp.shape, device=device)[[3, 2, 3, 2]] for pp in p[:self.nl]] pre_gen_gains = [torch.tensor(pp.shape, device=device)[[3, 2, 3, 2]] for pp in p[:self.nl]] # Losses for i in range(self.nl): # layer index, layer predictions pi = p[i] pi_aux = p[i+self.nl] b, a, gj, gi = bs[i], as_[i], gjs[i], gis[i] # image, anchor, gridy, gridx b_aux, a_aux, gj_aux, gi_aux = bs_aux[i], as_aux_[i], gjs_aux[i], gis_aux[i] # image, anchor, gridy, gridx tobj = torch.zeros_like(pi[..., 0], device=device) # target obj tobj_aux = torch.zeros_like(pi_aux[..., 0], device=device) # target obj n = b.shape[0] # number of targets if n: ps = pi[b, a, gj, gi] # prediction subset corresponding to targets # Regression grid = torch.stack([gi, gj], dim=1) pxy = ps[:, :2].sigmoid() * 2. - 0.5 pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i] pbox = torch.cat((pxy, pwh), 1) # predicted box selected_tbox = targets[i][:, 2:6] * pre_gen_gains[i] selected_tbox[:, :2] -= grid iou = bbox_iou(pbox.T, selected_tbox, x1y1x2y2=False, CIoU=True) # iou(prediction, target) lbox += (1.0 - iou).mean() # iou loss # Objectness tobj[b, a, gj, gi] = (1.0 - self.gr) + self.gr * iou.detach().clamp(0).type(tobj.dtype) # iou ratio # Classification selected_tcls = targets[i][:, 1].long() if self.nc > 1: # cls loss (only if multiple classes) t = torch.full_like(ps[:, 5:], self.cn, device=device) # targets t[range(n), selected_tcls] = self.cp lcls += self.BCEcls(ps[:, 5:], t) # BCE # Append targets to text file # with open('targets.txt', 'a') as file: # [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)] n_aux = b_aux.shape[0] # number of targets if n_aux: ps_aux = pi_aux[b_aux, a_aux, gj_aux, gi_aux] # prediction subset corresponding to targets grid_aux = torch.stack([gi_aux, gj_aux], dim=1) pxy_aux = ps_aux[:, :2].sigmoid() * 2. - 0.5 #pxy_aux = ps_aux[:, :2].sigmoid() * 3. - 1. pwh_aux = (ps_aux[:, 2:4].sigmoid() * 2) ** 2 * anchors_aux[i] pbox_aux = torch.cat((pxy_aux, pwh_aux), 1) # predicted box selected_tbox_aux = targets_aux[i][:, 2:6] * pre_gen_gains_aux[i] selected_tbox_aux[:, :2] -= grid_aux iou_aux = bbox_iou(pbox_aux.T, selected_tbox_aux, x1y1x2y2=False, CIoU=True) # iou(prediction, target) lbox += 0.25 * (1.0 - iou_aux).mean() # iou loss # Objectness tobj_aux[b_aux, a_aux, gj_aux, gi_aux] = (1.0 - self.gr) + self.gr * iou_aux.detach().clamp(0).type(tobj_aux.dtype) # iou ratio # Classification selected_tcls_aux = targets_aux[i][:, 1].long() if self.nc > 1: # cls loss (only if multiple classes) t_aux = torch.full_like(ps_aux[:, 5:], self.cn, device=device) # targets t_aux[range(n_aux), selected_tcls_aux] = self.cp lcls += 0.25 * self.BCEcls(ps_aux[:, 5:], t_aux) # BCE obji = self.BCEobj(pi[..., 4], tobj) obji_aux = self.BCEobj(pi_aux[..., 4], tobj_aux) lobj += obji * self.balance[i] + 0.25 * obji_aux * self.balance[i] # obj loss if self.autobalance: self.balance[i] = self.balance[i] * 0.9999 + 0.0001 / obji.detach().item() if self.autobalance: self.balance = [x / self.balance[self.ssi] for x in self.balance] lbox *= self.hyp['box'] lobj *= self.hyp['obj'] lcls *= self.hyp['cls'] bs = tobj.shape[0] # batch size loss = lbox + lobj + lcls return loss * bs, torch.cat((lbox, lobj, lcls, loss)).detach() def build_targets(self, p, targets, imgs): indices, anch = self.find_3_positive(p, targets) matching_bs = [[] for pp in p] matching_as = [[] for pp in p] matching_gjs = [[] for pp in p] matching_gis = [[] for pp in p] matching_targets = [[] for pp in p] matching_anchs = [[] for pp in p] nl = len(p) for batch_idx in range(p[0].shape[0]): b_idx = targets[:, 0]==batch_idx this_target = targets[b_idx] if this_target.shape[0] == 0: continue txywh = this_target[:, 2:6] * imgs[batch_idx].shape[1] txyxy = xywh2xyxy(txywh) pxyxys = [] p_cls = [] p_obj = [] from_which_layer = [] all_b = [] all_a = [] all_gj = [] all_gi = [] all_anch = [] for i, pi in enumerate(p): b, a, gj, gi = indices[i] idx = (b == batch_idx) b, a, gj, gi = b[idx], a[idx], gj[idx], gi[idx] all_b.append(b) all_a.append(a) all_gj.append(gj) all_gi.append(gi) all_anch.append(anch[i][idx]) from_which_layer.append(torch.ones(size=(len(b),)) * i) fg_pred = pi[b, a, gj, gi] p_obj.append(fg_pred[:, 4:5]) p_cls.append(fg_pred[:, 5:]) grid = torch.stack([gi, gj], dim=1) pxy = (fg_pred[:, :2].sigmoid() * 2. - 0.5 + grid) * self.stride[i] #/ 8. #pxy = (fg_pred[:, :2].sigmoid() * 3. - 1. + grid) * self.stride[i] pwh = (fg_pred[:, 2:4].sigmoid() * 2) ** 2 * anch[i][idx] * self.stride[i] #/ 8. pxywh = torch.cat([pxy, pwh], dim=-1) pxyxy = xywh2xyxy(pxywh) pxyxys.append(pxyxy) pxyxys = torch.cat(pxyxys, dim=0) if pxyxys.shape[0] == 0: continue p_obj = torch.cat(p_obj, dim=0) p_cls = torch.cat(p_cls, dim=0) from_which_layer = torch.cat(from_which_layer, dim=0) all_b = torch.cat(all_b, dim=0) all_a = torch.cat(all_a, dim=0) all_gj = torch.cat(all_gj, dim=0) all_gi = torch.cat(all_gi, dim=0) all_anch = torch.cat(all_anch, dim=0) pair_wise_iou = box_iou(txyxy, pxyxys) pair_wise_iou_loss = -torch.log(pair_wise_iou + 1e-8) top_k, _ = torch.topk(pair_wise_iou, min(20, pair_wise_iou.shape[1]), dim=1) dynamic_ks = torch.clamp(top_k.sum(1).int(), min=1) gt_cls_per_image = ( F.one_hot(this_target[:, 1].to(torch.int64), self.nc) .float() .unsqueeze(1) .repeat(1, pxyxys.shape[0], 1) ) num_gt = this_target.shape[0] cls_preds_ = ( p_cls.float().unsqueeze(0).repeat(num_gt, 1, 1).sigmoid_() * p_obj.unsqueeze(0).repeat(num_gt, 1, 1).sigmoid_() ) y = cls_preds_.sqrt_() pair_wise_cls_loss = F.binary_cross_entropy_with_logits( torch.log(y/(1-y)) , gt_cls_per_image, reduction="none" ).sum(-1) del cls_preds_ cost = ( pair_wise_cls_loss + 3.0 * pair_wise_iou_loss ) matching_matrix = torch.zeros_like(cost) for gt_idx in range(num_gt): _, pos_idx = torch.topk( cost[gt_idx], k=dynamic_ks[gt_idx].item(), largest=False ) matching_matrix[gt_idx][pos_idx] = 1.0 del top_k, dynamic_ks anchor_matching_gt = matching_matrix.sum(0) if (anchor_matching_gt > 1).sum() > 0: _, cost_argmin = torch.min(cost[:, anchor_matching_gt > 1], dim=0) matching_matrix[:, anchor_matching_gt > 1] *= 0.0 matching_matrix[cost_argmin, anchor_matching_gt > 1] = 1.0 fg_mask_inboxes = matching_matrix.sum(0) > 0.0 matched_gt_inds = matching_matrix[:, fg_mask_inboxes].argmax(0) from_which_layer = from_which_layer[fg_mask_inboxes] all_b = all_b[fg_mask_inboxes] all_a = all_a[fg_mask_inboxes] all_gj = all_gj[fg_mask_inboxes] all_gi = all_gi[fg_mask_inboxes] all_anch = all_anch[fg_mask_inboxes] this_target = this_target[matched_gt_inds] for i in range(nl): layer_idx = from_which_layer == i matching_bs[i].append(all_b[layer_idx]) matching_as[i].append(all_a[layer_idx]) matching_gjs[i].append(all_gj[layer_idx]) matching_gis[i].append(all_gi[layer_idx]) matching_targets[i].append(this_target[layer_idx]) matching_anchs[i].append(all_anch[layer_idx]) for i in range(nl): if matching_targets[i] != []: matching_bs[i] = torch.cat(matching_bs[i], dim=0) matching_as[i] = torch.cat(matching_as[i], dim=0) matching_gjs[i] = torch.cat(matching_gjs[i], dim=0) matching_gis[i] = torch.cat(matching_gis[i], dim=0) matching_targets[i] = torch.cat(matching_targets[i], dim=0) matching_anchs[i] = torch.cat(matching_anchs[i], dim=0) else: matching_bs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_as[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_gjs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_gis[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_targets[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_anchs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) return matching_bs, matching_as, matching_gjs, matching_gis, matching_targets, matching_anchs def build_targets2(self, p, targets, imgs): indices, anch = self.find_5_positive(p, targets) matching_bs = [[] for pp in p] matching_as = [[] for pp in p] matching_gjs = [[] for pp in p] matching_gis = [[] for pp in p] matching_targets = [[] for pp in p] matching_anchs = [[] for pp in p] nl = len(p) for batch_idx in range(p[0].shape[0]): b_idx = targets[:, 0]==batch_idx this_target = targets[b_idx] if this_target.shape[0] == 0: continue txywh = this_target[:, 2:6] * imgs[batch_idx].shape[1] txyxy = xywh2xyxy(txywh) pxyxys = [] p_cls = [] p_obj = [] from_which_layer = [] all_b = [] all_a = [] all_gj = [] all_gi = [] all_anch = [] for i, pi in enumerate(p): b, a, gj, gi = indices[i] idx = (b == batch_idx) b, a, gj, gi = b[idx], a[idx], gj[idx], gi[idx] all_b.append(b) all_a.append(a) all_gj.append(gj) all_gi.append(gi) all_anch.append(anch[i][idx]) from_which_layer.append(torch.ones(size=(len(b),)) * i) fg_pred = pi[b, a, gj, gi] p_obj.append(fg_pred[:, 4:5]) p_cls.append(fg_pred[:, 5:]) grid = torch.stack([gi, gj], dim=1) pxy = (fg_pred[:, :2].sigmoid() * 2. - 0.5 + grid) * self.stride[i] #/ 8. #pxy = (fg_pred[:, :2].sigmoid() * 3. - 1. + grid) * self.stride[i] pwh = (fg_pred[:, 2:4].sigmoid() * 2) ** 2 * anch[i][idx] * self.stride[i] #/ 8. pxywh = torch.cat([pxy, pwh], dim=-1) pxyxy = xywh2xyxy(pxywh) pxyxys.append(pxyxy) pxyxys = torch.cat(pxyxys, dim=0) if pxyxys.shape[0] == 0: continue p_obj = torch.cat(p_obj, dim=0) p_cls = torch.cat(p_cls, dim=0) from_which_layer = torch.cat(from_which_layer, dim=0) all_b = torch.cat(all_b, dim=0) all_a = torch.cat(all_a, dim=0) all_gj = torch.cat(all_gj, dim=0) all_gi = torch.cat(all_gi, dim=0) all_anch = torch.cat(all_anch, dim=0) pair_wise_iou = box_iou(txyxy, pxyxys) pair_wise_iou_loss = -torch.log(pair_wise_iou + 1e-8) top_k, _ = torch.topk(pair_wise_iou, min(20, pair_wise_iou.shape[1]), dim=1) dynamic_ks = torch.clamp(top_k.sum(1).int(), min=1) gt_cls_per_image = ( F.one_hot(this_target[:, 1].to(torch.int64), self.nc) .float() .unsqueeze(1) .repeat(1, pxyxys.shape[0], 1) ) num_gt = this_target.shape[0] cls_preds_ = ( p_cls.float().unsqueeze(0).repeat(num_gt, 1, 1).sigmoid_() * p_obj.unsqueeze(0).repeat(num_gt, 1, 1).sigmoid_() ) y = cls_preds_.sqrt_() pair_wise_cls_loss = F.binary_cross_entropy_with_logits( torch.log(y/(1-y)) , gt_cls_per_image, reduction="none" ).sum(-1) del cls_preds_ cost = ( pair_wise_cls_loss + 3.0 * pair_wise_iou_loss ) matching_matrix = torch.zeros_like(cost) for gt_idx in range(num_gt): _, pos_idx = torch.topk( cost[gt_idx], k=dynamic_ks[gt_idx].item(), largest=False ) matching_matrix[gt_idx][pos_idx] = 1.0 del top_k, dynamic_ks anchor_matching_gt = matching_matrix.sum(0) if (anchor_matching_gt > 1).sum() > 0: _, cost_argmin = torch.min(cost[:, anchor_matching_gt > 1], dim=0) matching_matrix[:, anchor_matching_gt > 1] *= 0.0 matching_matrix[cost_argmin, anchor_matching_gt > 1] = 1.0 fg_mask_inboxes = matching_matrix.sum(0) > 0.0 matched_gt_inds = matching_matrix[:, fg_mask_inboxes].argmax(0) from_which_layer = from_which_layer[fg_mask_inboxes] all_b = all_b[fg_mask_inboxes] all_a = all_a[fg_mask_inboxes] all_gj = all_gj[fg_mask_inboxes] all_gi = all_gi[fg_mask_inboxes] all_anch = all_anch[fg_mask_inboxes] this_target = this_target[matched_gt_inds] for i in range(nl): layer_idx = from_which_layer == i matching_bs[i].append(all_b[layer_idx]) matching_as[i].append(all_a[layer_idx]) matching_gjs[i].append(all_gj[layer_idx]) matching_gis[i].append(all_gi[layer_idx]) matching_targets[i].append(this_target[layer_idx]) matching_anchs[i].append(all_anch[layer_idx]) for i in range(nl): if matching_targets[i] != []: matching_bs[i] = torch.cat(matching_bs[i], dim=0) matching_as[i] = torch.cat(matching_as[i], dim=0) matching_gjs[i] = torch.cat(matching_gjs[i], dim=0) matching_gis[i] = torch.cat(matching_gis[i], dim=0) matching_targets[i] = torch.cat(matching_targets[i], dim=0) matching_anchs[i] = torch.cat(matching_anchs[i], dim=0) else: matching_bs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_as[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_gjs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_gis[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_targets[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) matching_anchs[i] = torch.tensor([], device='cuda:0', dtype=torch.int64) return matching_bs, matching_as, matching_gjs, matching_gis, matching_targets, matching_anchs def find_5_positive(self, p, targets): # Build targets for compute_loss(), input targets(image,class,x,y,w,h) na, nt = self.na, targets.shape[0] # number of anchors, targets indices, anch = [], [] gain = torch.ones(7, device=targets.device).long() # normalized to gridspace gain ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt) targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices g = 1.0 # bias off = torch.tensor([[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m # [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm ], device=targets.device).float() * g # offsets for i in range(self.nl): anchors = self.anchors[i] gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain # Match targets to anchors t = targets * gain if nt: # Matches r = t[:, :, 4:6] / anchors[:, None] # wh ratio j = torch.max(r, 1. / r).max(2)[0] < self.hyp['anchor_t'] # compare # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2)) t = t[j] # filter # Offsets gxy = t[:, 2:4] # grid xy gxi = gain[[2, 3]] - gxy # inverse j, k = ((gxy % 1. < g) & (gxy > 1.)).T l, m = ((gxi % 1. < g) & (gxi > 1.)).T j = torch.stack((torch.ones_like(j), j, k, l, m)) t = t.repeat((5, 1, 1))[j] offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j] else: t = targets[0] offsets = 0 # Define b, c = t[:, :2].long().T # image, class gxy = t[:, 2:4] # grid xy gwh = t[:, 4:6] # grid wh gij = (gxy - offsets).long() gi, gj = gij.T # grid xy indices # Append a = t[:, 6].long() # anchor indices indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices anch.append(anchors[a]) # anchors return indices, anch def find_3_positive(self, p, targets): # Build targets for compute_loss(), input targets(image,class,x,y,w,h) na, nt = self.na, targets.shape[0] # number of anchors, targets indices, anch = [], [] gain = torch.ones(7, device=targets.device).long() # normalized to gridspace gain ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt) targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices g = 0.5 # bias off = torch.tensor([[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m # [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm ], device=targets.device).float() * g # offsets for i in range(self.nl): anchors = self.anchors[i] gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain # Match targets to anchors t = targets * gain if nt: # Matches r = t[:, :, 4:6] / anchors[:, None] # wh ratio j = torch.max(r, 1. / r).max(2)[0] < self.hyp['anchor_t'] # compare # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2)) t = t[j] # filter # Offsets gxy = t[:, 2:4] # grid xy gxi = gain[[2, 3]] - gxy # inverse j, k = ((gxy % 1. < g) & (gxy > 1.)).T l, m = ((gxi % 1. < g) & (gxi > 1.)).T j = torch.stack((torch.ones_like(j), j, k, l, m)) t = t.repeat((5, 1, 1))[j] offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j] else: t = targets[0] offsets = 0 # Define b, c = t[:, :2].long().T # image, class gxy = t[:, 2:4] # grid xy gwh = t[:, 4:6] # grid wh gij = (gxy - offsets).long() gi, gj = gij.T # grid xy indices # Append a = t[:, 6].long() # anchor indices indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices anch.append(anchors[a]) # anchors return indices, anch ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/metrics.py ================================================ # Model validation metrics from pathlib import Path import matplotlib.pyplot as plt import numpy as np import torch from asone.pose_estimators.yolov7_pose.utils import general def fitness(x): # Model fitness as a weighted combination of metrics w = [0.0, 0.0, 0.1, 0.9] # weights for [P, R, mAP@0.5, mAP@0.5:0.95] return (x[:, :4] * w).sum(1) def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names=()): """ Compute the average precision, given the recall and precision curves. Source: https://github.com/rafaelpadilla/Object-Detection-Metrics. # Arguments tp: True positives (nparray, nx1 or nx10). conf: Objectness value from 0-1 (nparray). pred_cls: Predicted object classes (nparray). target_cls: True object classes (nparray). plot: Plot precision-recall curve at mAP@0.5 save_dir: Plot save directory # Returns The average precision as computed in py-faster-rcnn. """ # Sort by objectness i = np.argsort(-conf) tp, conf, pred_cls = tp[i], conf[i], pred_cls[i] # Find unique classes unique_classes = np.unique(target_cls) nc = unique_classes.shape[0] # number of classes, number of detections # Create Precision-Recall curve and compute AP for each class px, py = np.linspace(0, 1, 1000), [] # for plotting ap, p, r = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000)) for ci, c in enumerate(unique_classes): i = pred_cls == c n_l = (target_cls == c).sum() # number of labels n_p = i.sum() # number of predictions if n_p == 0 or n_l == 0: continue else: # Accumulate FPs and TPs fpc = (1 - tp[i]).cumsum(0) tpc = tp[i].cumsum(0) # Recall recall = tpc / (n_l + 1e-16) # recall curve r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # negative x, xp because xp decreases # Precision precision = tpc / (tpc + fpc) # precision curve p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1) # p at pr_score # AP from recall-precision curve for j in range(tp.shape[1]): ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j]) if plot and j == 0: py.append(np.interp(px, mrec, mpre)) # precision at mAP@0.5 # Compute F1 (harmonic mean of precision and recall) f1 = 2 * p * r / (p + r + 1e-16) if plot: plot_pr_curve(px, py, ap, Path(save_dir) / 'PR_curve.png', names) plot_mc_curve(px, f1, Path(save_dir) / 'F1_curve.png', names, ylabel='F1') plot_mc_curve(px, p, Path(save_dir) / 'P_curve.png', names, ylabel='Precision') plot_mc_curve(px, r, Path(save_dir) / 'R_curve.png', names, ylabel='Recall') i = f1.mean(0).argmax() # max F1 index return p[:, i], r[:, i], ap, f1[:, i], unique_classes.astype('int32') def compute_ap(recall, precision): """ Compute the average precision, given the recall and precision curves # Arguments recall: The recall curve (list) precision: The precision curve (list) # Returns Average precision, precision curve, recall curve """ # Append sentinel values to beginning and end mrec = np.concatenate(([0.], recall, [recall[-1] + 0.01])) mpre = np.concatenate(([1.], precision, [0.])) # Compute the precision envelope mpre = np.flip(np.maximum.accumulate(np.flip(mpre))) # Integrate area under curve method = 'interp' # methods: 'continuous', 'interp' if method == 'interp': x = np.linspace(0, 1, 101) # 101-point interp (COCO) ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate else: # 'continuous' i = np.where(mrec[1:] != mrec[:-1])[0] # points where x axis (recall) changes ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve return ap, mpre, mrec class ConfusionMatrix: # Updated version of https://github.com/kaanakan/object_detection_confusion_matrix def __init__(self, nc, conf=0.25, iou_thres=0.45): self.matrix = np.zeros((nc + 1, nc + 1)) self.nc = nc # number of classes self.conf = conf self.iou_thres = iou_thres def process_batch(self, detections, labels): """ Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. Arguments: detections (Array[N, 6]), x1, y1, x2, y2, conf, class labels (Array[M, 5]), class, x1, y1, x2, y2 Returns: None, updates confusion matrix accordingly """ detections = detections[detections[:, 4] > self.conf] gt_classes = labels[:, 0].int() detection_classes = detections[:, 5].int() iou = general.box_iou(labels[:, 1:], detections[:, :4]) x = torch.where(iou > self.iou_thres) if x[0].shape[0]: matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy() if x[0].shape[0] > 1: matches = matches[matches[:, 2].argsort()[::-1]] matches = matches[np.unique(matches[:, 1], return_index=True)[1]] matches = matches[matches[:, 2].argsort()[::-1]] matches = matches[np.unique(matches[:, 0], return_index=True)[1]] else: matches = np.zeros((0, 3)) n = matches.shape[0] > 0 m0, m1, _ = matches.transpose().astype(np.int16) for i, gc in enumerate(gt_classes): j = m0 == i if n and sum(j) == 1: self.matrix[gc, detection_classes[m1[j]]] += 1 # correct else: self.matrix[self.nc, gc] += 1 # background FP if n: for i, dc in enumerate(detection_classes): if not any(m1 == i): self.matrix[dc, self.nc] += 1 # background FN def matrix(self): return self.matrix def plot(self, save_dir='', names=()): try: import seaborn as sn array = self.matrix / (self.matrix.sum(0).reshape(1, self.nc + 1) + 1E-6) # normalize array[array < 0.005] = np.nan # don't annotate (would appear as 0.00) fig = plt.figure(figsize=(12, 9), tight_layout=True) sn.set(font_scale=1.0 if self.nc < 50 else 0.8) # for label size labels = (0 < len(names) < 99) and len(names) == self.nc # apply names to ticklabels sn.heatmap(array, annot=self.nc < 30, annot_kws={"size": 8}, cmap='Blues', fmt='.2f', square=True, xticklabels=names + ['background FP'] if labels else "auto", yticklabels=names + ['background FN'] if labels else "auto").set_facecolor((1, 1, 1)) fig.axes[0].set_xlabel('True') fig.axes[0].set_ylabel('Predicted') fig.savefig(Path(save_dir) / 'confusion_matrix.png', dpi=250) except Exception as e: pass def print(self): for i in range(self.nc + 1): print(' '.join(map(str, self.matrix[i]))) # Plots ---------------------------------------------------------------------------------------------------------------- def plot_pr_curve(px, py, ap, save_dir='pr_curve.png', names=()): # Precision-recall curve fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) py = np.stack(py, axis=1) if 0 < len(names) < 21: # display per-class legend if < 21 classes for i, y in enumerate(py.T): ax.plot(px, y, linewidth=1, label=f'{names[i]} {ap[i, 0]:.3f}') # plot(recall, precision) else: ax.plot(px, py, linewidth=1, color='grey') # plot(recall, precision) ax.plot(px, py.mean(1), linewidth=3, color='blue', label='all classes %.3f mAP@0.5' % ap[:, 0].mean()) ax.set_xlabel('Recall') ax.set_ylabel('Precision') ax.set_xlim(0, 1) ax.set_ylim(0, 1) plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left") fig.savefig(Path(save_dir), dpi=250) def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence', ylabel='Metric'): # Metric-confidence curve fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True) if 0 < len(names) < 21: # display per-class legend if < 21 classes for i, y in enumerate(py): ax.plot(px, y, linewidth=1, label=f'{names[i]}') # plot(confidence, metric) else: ax.plot(px, py.T, linewidth=1, color='grey') # plot(confidence, metric) y = py.mean(0) ax.plot(px, y, linewidth=3, color='blue', label=f'all classes {y.max():.2f} at {px[y.argmax()]:.3f}') ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_xlim(0, 1) ax.set_ylim(0, 1) plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left") fig.savefig(Path(save_dir), dpi=250) ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/plots.py ================================================ # Plotting utils import glob import math import os import random from copy import copy from pathlib import Path import cv2 import matplotlib import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns import torch import yaml from PIL import Image, ImageDraw, ImageFont from scipy.signal import butter, filtfilt from asone.pose_estimators.yolov7_pose.utils.general import xywh2xyxy, xyxy2xywh from asone.pose_estimators.yolov7_pose.utils.metrics import fitness # Settings matplotlib.rc('font', **{'size': 11}) matplotlib.use('Agg') # for writing to files only class Colors: # Ultralytics color palette https://ultralytics.com/ def __init__(self): self.palette = [self.hex2rgb(c) for c in matplotlib.colors.TABLEAU_COLORS.values()] self.n = len(self.palette) def __call__(self, i, bgr=False): c = self.palette[int(i) % self.n] return (c[2], c[1], c[0]) if bgr else c @staticmethod def hex2rgb(h): # rgb order (PIL) return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4)) def plot_one_box_kpt(x, im, color=None, label=None, line_thickness=3, kpt_label=False, kpts=None, steps=2, orig_shape=None): # Plots one bounding box on image 'im' using OpenCV assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to plot_on_box() input image.' tl = line_thickness or round(0.002 * (im.shape[0] + im.shape[1]) / 2) + 1 # line/font thickness color = color or [random.randint(0, 255) for _ in range(3)] c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) # cv2.rectangle(im, c1, c2, (255,0,0), thickness=tl*1//3, lineType=cv2.LINE_AA) if label: if len(label.split(' ')) > 1: label = label.split(' ')[-1] tf = max(tl - 1, 1) # font thickness t_size = cv2.getTextSize(label, 0, fontScale=tl / 6, thickness=tf)[0] c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 cv2.rectangle(im, c1, c2, color, -1, cv2.LINE_AA) # filled cv2.putText(im, label, (c1[0], c1[1] - 2), 0, tl / 6, [225, 255, 255], thickness=tf//2, lineType=cv2.LINE_AA) if kpt_label: plot_skeleton_kpts(im, kpts, steps, orig_shape=orig_shape) colors = Colors() def color_list(): def hex2rgb(h): return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4)) return [hex2rgb(h) for h in matplotlib.colors.TABLEAU_COLORS.values()] # or BASE_ (8), CSS4_ (148), XKCD_ (949) def hist2d(x, y, n=100): # 2d histogram used in labels.png and evolve.png xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n) hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges)) xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1) yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1) return np.log(hist[xidx, yidx]) def butter_lowpass_filtfilt(data, cutoff=1500, fs=50000, order=5): # https://stackoverflow.com/questions/28536191/how-to-filter-smooth-with-scipy-numpy def butter_lowpass(cutoff, fs, order): nyq = 0.5 * fs normal_cutoff = cutoff / nyq return butter(order, normal_cutoff, btype='low', analog=False) b, a = butter_lowpass(cutoff, fs, order=order) return filtfilt(b, a, data) # forward-backward filter def plot_one_box(x, img, color=None, label=None, line_thickness=1): # Plots one bounding box on image img tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 2 # line/font thickness color = color or [random.randint(0, 255) for _ in range(3)] c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) if label: tf = max(tl - 1, 1) # font thickness t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) def plot_one_box_PIL(box, img, color=None, label=None, line_thickness=None): img = Image.fromarray(img) draw = ImageDraw.Draw(img) line_thickness = line_thickness or max(int(min(img.size) / 200), 2) draw.rectangle(box, width=line_thickness, outline=tuple(color)) # plot if label: fontsize = max(round(max(img.size) / 40), 12) font = ImageFont.truetype("Arial.ttf", fontsize) txt_width, txt_height = font.getsize(label) draw.rectangle([box[0], box[1] - txt_height + 4, box[0] + txt_width, box[1]], fill=tuple(color)) draw.text((box[0], box[1] - txt_height + 1), label, fill=(255, 255, 255), font=font) return np.asarray(img) def plot_wh_methods(): # from utils.plots import *; plot_wh_methods() # Compares the two methods for width-height anchor multiplication # https://github.com/ultralytics/yolov3/issues/168 x = np.arange(-4.0, 4.0, .1) ya = np.exp(x) yb = torch.sigmoid(torch.from_numpy(x)).numpy() * 2 fig = plt.figure(figsize=(6, 3), tight_layout=True) plt.plot(x, ya, '.-', label='YOLOv3') plt.plot(x, yb ** 2, '.-', label='YOLOR ^2') plt.plot(x, yb ** 1.6, '.-', label='YOLOR ^1.6') plt.xlim(left=-4, right=4) plt.ylim(bottom=0, top=6) plt.xlabel('input') plt.ylabel('output') plt.grid() plt.legend() fig.savefig('comparison.png', dpi=200) def output_to_target(output): # Convert model output to target format [batch_id, class_id, x, y, w, h, conf] targets = [] for i, o in enumerate(output): for *box, conf, cls in o.cpu().numpy(): targets.append([i, cls, *list(*xyxy2xywh(np.array(box)[None])), conf]) return np.array(targets) def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max_size=640, max_subplots=16): # Plot image grid with labels if isinstance(images, torch.Tensor): images = images.cpu().float().numpy() if isinstance(targets, torch.Tensor): targets = targets.cpu().numpy() # un-normalise if np.max(images[0]) <= 1: images *= 255 tl = 3 # line thickness tf = max(tl - 1, 1) # font thickness bs, _, h, w = images.shape # batch size, _, height, width bs = min(bs, max_subplots) # limit plot images ns = np.ceil(bs ** 0.5) # number of subplots (square) # Check if we should resize scale_factor = max_size / max(h, w) if scale_factor < 1: h = math.ceil(scale_factor * h) w = math.ceil(scale_factor * w) colors = color_list() # list of colors mosaic = np.full((int(ns * h), int(ns * w), 3), 255, dtype=np.uint8) # init for i, img in enumerate(images): if i == max_subplots: # if last batch has fewer images than we expect break block_x = int(w * (i // ns)) block_y = int(h * (i % ns)) img = img.transpose(1, 2, 0) if scale_factor < 1: img = cv2.resize(img, (w, h)) mosaic[block_y:block_y + h, block_x:block_x + w, :] = img if len(targets) > 0: image_targets = targets[targets[:, 0] == i] boxes = xywh2xyxy(image_targets[:, 2:6]).T classes = image_targets[:, 1].astype('int') labels = image_targets.shape[1] == 6 # labels if no conf column conf = None if labels else image_targets[:, 6] # check for confidence presence (label vs pred) if boxes.shape[1]: if boxes.max() <= 1.01: # if normalized with tolerance 0.01 boxes[[0, 2]] *= w # scale to pixels boxes[[1, 3]] *= h elif scale_factor < 1: # absolute coords need scale if image scales boxes *= scale_factor boxes[[0, 2]] += block_x boxes[[1, 3]] += block_y for j, box in enumerate(boxes.T): cls = int(classes[j]) color = colors[cls % len(colors)] cls = names[cls] if names else cls if labels or conf[j] > 0.25: # 0.25 conf thresh label = '%s' % cls if labels else '%s %.1f' % (cls, conf[j]) plot_one_box(box, mosaic, label=label, color=color, line_thickness=tl) # Draw image filename labels if paths: label = Path(paths[i]).name[:40] # trim to 40 char t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] cv2.putText(mosaic, label, (block_x + 5, block_y + t_size[1] + 5), 0, tl / 3, [220, 220, 220], thickness=tf, lineType=cv2.LINE_AA) # Image border cv2.rectangle(mosaic, (block_x, block_y), (block_x + w, block_y + h), (255, 255, 255), thickness=3) if fname: r = min(1280. / max(h, w) / ns, 1.0) # ratio to limit image size mosaic = cv2.resize(mosaic, (int(ns * w * r), int(ns * h * r)), interpolation=cv2.INTER_AREA) # cv2.imwrite(fname, cv2.cvtColor(mosaic, cv2.COLOR_BGR2RGB)) # cv2 save Image.fromarray(mosaic).save(fname) # PIL save return mosaic def plot_lr_scheduler(optimizer, scheduler, epochs=300, save_dir=''): # Plot LR simulating training for full epochs optimizer, scheduler = copy(optimizer), copy(scheduler) # do not modify originals y = [] for _ in range(epochs): scheduler.step() y.append(optimizer.param_groups[0]['lr']) plt.plot(y, '.-', label='LR') plt.xlabel('epoch') plt.ylabel('LR') plt.grid() plt.xlim(0, epochs) plt.ylim(0) plt.savefig(Path(save_dir) / 'LR.png', dpi=200) plt.close() def plot_test_txt(): # from utils.plots import *; plot_test() # Plot test.txt histograms x = np.loadtxt('test.txt', dtype=np.float32) box = xyxy2xywh(x[:, :4]) cx, cy = box[:, 0], box[:, 1] fig, ax = plt.subplots(1, 1, figsize=(6, 6), tight_layout=True) ax.hist2d(cx, cy, bins=600, cmax=10, cmin=0) ax.set_aspect('equal') plt.savefig('hist2d.png', dpi=300) fig, ax = plt.subplots(1, 2, figsize=(12, 6), tight_layout=True) ax[0].hist(cx, bins=600) ax[1].hist(cy, bins=600) plt.savefig('hist1d.png', dpi=200) def plot_targets_txt(): # from utils.plots import *; plot_targets_txt() # Plot targets.txt histograms x = np.loadtxt('targets.txt', dtype=np.float32).T s = ['x targets', 'y targets', 'width targets', 'height targets'] fig, ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True) ax = ax.ravel() for i in range(4): ax[i].hist(x[i], bins=100, label='%.3g +/- %.3g' % (x[i].mean(), x[i].std())) ax[i].legend() ax[i].set_title(s[i]) plt.savefig('targets.jpg', dpi=200) def plot_study_txt(path='', x=None): # from utils.plots import *; plot_study_txt() # Plot study.txt generated by test.py fig, ax = plt.subplots(2, 4, figsize=(10, 6), tight_layout=True) # ax = ax.ravel() fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4), tight_layout=True) # for f in [Path(path) / f'study_coco_{x}.txt' for x in ['yolor-p6', 'yolor-w6', 'yolor-e6', 'yolor-d6']]: for f in sorted(Path(path).glob('study*.txt')): y = np.loadtxt(f, dtype=np.float32, usecols=[0, 1, 2, 3, 7, 8, 9], ndmin=2).T x = np.arange(y.shape[1]) if x is None else np.array(x) s = ['P', 'R', 'mAP@.5', 'mAP@.5:.95', 't_inference (ms/img)', 't_NMS (ms/img)', 't_total (ms/img)'] # for i in range(7): # ax[i].plot(x, y[i], '.-', linewidth=2, markersize=8) # ax[i].set_title(s[i]) j = y[3].argmax() + 1 ax2.plot(y[6, 1:j], y[3, 1:j] * 1E2, '.-', linewidth=2, markersize=8, label=f.stem.replace('study_coco_', '').replace('yolo', 'YOLO')) ax2.plot(1E3 / np.array([209, 140, 97, 58, 35, 18]), [34.6, 40.5, 43.0, 47.5, 49.7, 51.5], 'k.-', linewidth=2, markersize=8, alpha=.25, label='EfficientDet') ax2.grid(alpha=0.2) ax2.set_yticks(np.arange(20, 60, 5)) ax2.set_xlim(0, 57) ax2.set_ylim(30, 55) ax2.set_xlabel('GPU Speed (ms/img)') ax2.set_ylabel('COCO AP val') ax2.legend(loc='lower right') plt.savefig(str(Path(path).name) + '.png', dpi=300) def plot_labels(labels, names=(), save_dir=Path(''), loggers=None): # plot dataset labels print('Plotting labels... ') c, b = labels[:, 0], labels[:, 1:].transpose() # classes, boxes nc = int(c.max() + 1) # number of classes colors = color_list() x = pd.DataFrame(b.transpose(), columns=['x', 'y', 'width', 'height']) # seaborn correlogram sns.pairplot(x, corner=True, diag_kind='auto', kind='hist', diag_kws=dict(bins=50), plot_kws=dict(pmax=0.9)) plt.savefig(save_dir / 'labels_correlogram.jpg', dpi=200) plt.close() # matplotlib labels matplotlib.use('svg') # faster ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True)[1].ravel() ax[0].hist(c, bins=np.linspace(0, nc, nc + 1) - 0.5, rwidth=0.8) ax[0].set_ylabel('instances') if 0 < len(names) < 30: ax[0].set_xticks(range(len(names))) ax[0].set_xticklabels(names, rotation=90, fontsize=10) else: ax[0].set_xlabel('classes') sns.histplot(x, x='x', y='y', ax=ax[2], bins=50, pmax=0.9) sns.histplot(x, x='width', y='height', ax=ax[3], bins=50, pmax=0.9) # rectangles labels[:, 1:3] = 0.5 # center labels[:, 1:] = xywh2xyxy(labels[:, 1:]) * 2000 img = Image.fromarray(np.ones((2000, 2000, 3), dtype=np.uint8) * 255) for cls, *box in labels[:1000]: ImageDraw.Draw(img).rectangle(box, width=1, outline=colors[int(cls) % 10]) # plot ax[1].imshow(img) ax[1].axis('off') for a in [0, 1, 2, 3]: for s in ['top', 'right', 'left', 'bottom']: ax[a].spines[s].set_visible(False) plt.savefig(save_dir / 'labels.jpg', dpi=200) matplotlib.use('Agg') plt.close() # loggers for k, v in loggers.items() or {}: if k == 'wandb' and v: v.log({"Labels": [v.Image(str(x), caption=x.name) for x in save_dir.glob('*labels*.jpg')]}, commit=False) def plot_evolution(yaml_file='data/hyp.finetune.yaml'): # from utils.plots import *; plot_evolution() # Plot hyperparameter evolution results in evolve.txt with open(yaml_file) as f: hyp = yaml.load(f, Loader=yaml.SafeLoader) x = np.loadtxt('evolve.txt', ndmin=2) f = fitness(x) # weights = (f - f.min()) ** 2 # for weighted results plt.figure(figsize=(10, 12), tight_layout=True) matplotlib.rc('font', **{'size': 8}) for i, (k, v) in enumerate(hyp.items()): y = x[:, i + 7] # mu = (y * weights).sum() / weights.sum() # best weighted result mu = y[f.argmax()] # best single result plt.subplot(6, 5, i + 1) plt.scatter(y, f, c=hist2d(y, f, 20), cmap='viridis', alpha=.8, edgecolors='none') plt.plot(mu, f.max(), 'k+', markersize=15) plt.title('%s = %.3g' % (k, mu), fontdict={'size': 9}) # limit to 40 characters if i % 5 != 0: plt.yticks([]) print('%15s: %.3g' % (k, mu)) plt.savefig('evolve.png', dpi=200) print('\nPlot saved as evolve.png') def profile_idetection(start=0, stop=0, labels=(), save_dir=''): # Plot iDetection '*.txt' per-image logs. from utils.plots import *; profile_idetection() ax = plt.subplots(2, 4, figsize=(12, 6), tight_layout=True)[1].ravel() s = ['Images', 'Free Storage (GB)', 'RAM Usage (GB)', 'Battery', 'dt_raw (ms)', 'dt_smooth (ms)', 'real-world FPS'] files = list(Path(save_dir).glob('frames*.txt')) for fi, f in enumerate(files): try: results = np.loadtxt(f, ndmin=2).T[:, 90:-30] # clip first and last rows n = results.shape[1] # number of rows x = np.arange(start, min(stop, n) if stop else n) results = results[:, x] t = (results[0] - results[0].min()) # set t0=0s results[0] = x for i, a in enumerate(ax): if i < len(results): label = labels[fi] if len(labels) else f.stem.replace('frames_', '') a.plot(t, results[i], marker='.', label=label, linewidth=1, markersize=5) a.set_title(s[i]) a.set_xlabel('time (s)') # if fi == len(files) - 1: # a.set_ylim(bottom=0) for side in ['top', 'right']: a.spines[side].set_visible(False) else: a.remove() except Exception as e: print('Warning: Plotting error for %s; %s' % (f, e)) ax[1].legend() plt.savefig(Path(save_dir) / 'idetection_profile.png', dpi=200) def plot_results_overlay(start=0, stop=0): # from utils.plots import *; plot_results_overlay() # Plot training 'results*.txt', overlaying train and val losses s = ['train', 'train', 'train', 'Precision', 'mAP@0.5', 'val', 'val', 'val', 'Recall', 'mAP@0.5:0.95'] # legends t = ['Box', 'Objectness', 'Classification', 'P-R', 'mAP-F1'] # titles for f in sorted(glob.glob('results*.txt') + glob.glob('../../Downloads/results*.txt')): results = np.loadtxt(f, usecols=[2, 3, 4, 8, 9, 12, 13, 14, 10, 11], ndmin=2).T n = results.shape[1] # number of rows x = range(start, min(stop, n) if stop else n) fig, ax = plt.subplots(1, 5, figsize=(14, 3.5), tight_layout=True) ax = ax.ravel() for i in range(5): for j in [i, i + 5]: y = results[j, x] ax[i].plot(x, y, marker='.', label=s[j]) # y_smooth = butter_lowpass_filtfilt(y) # ax[i].plot(x, np.gradient(y_smooth), marker='.', label=s[j]) ax[i].set_title(t[i]) ax[i].legend() ax[i].set_ylabel(f) if i == 0 else None # add filename fig.savefig(f.replace('.txt', '.png'), dpi=200) def plot_results(start=0, stop=0, bucket='', id=(), labels=(), save_dir=''): # Plot training 'results*.txt'. from utils.plots import *; plot_results(save_dir='runs/train/exp') fig, ax = plt.subplots(2, 5, figsize=(12, 6), tight_layout=True) ax = ax.ravel() s = ['Box', 'Objectness', 'Classification', 'Precision', 'Recall', 'val Box', 'val Objectness', 'val Classification', 'mAP@0.5', 'mAP@0.5:0.95'] if bucket: # files = ['https://storage.googleapis.com/%s/results%g.txt' % (bucket, x) for x in id] files = ['results%g.txt' % x for x in id] c = ('gsutil cp ' + '%s ' * len(files) + '.') % tuple('gs://%s/results%g.txt' % (bucket, x) for x in id) os.system(c) else: files = list(Path(save_dir).glob('results*.txt')) assert len(files), 'No results.txt files found in %s, nothing to plot.' % os.path.abspath(save_dir) for fi, f in enumerate(files): try: results = np.loadtxt(f, usecols=[2, 3, 4, 8, 9, 12, 13, 14, 10, 11], ndmin=2).T n = results.shape[1] # number of rows x = range(start, min(stop, n) if stop else n) for i in range(10): y = results[i, x] if i in [0, 1, 2, 5, 6, 7]: y[y == 0] = np.nan # don't show zero loss values # y /= y[0] # normalize label = labels[fi] if len(labels) else f.stem ax[i].plot(x, y, marker='.', label=label, linewidth=2, markersize=8) ax[i].set_title(s[i]) # if i in [5, 6, 7]: # share train and val loss y axes # ax[i].get_shared_y_axes().join(ax[i], ax[i - 5]) except Exception as e: print('Warning: Plotting error for %s; %s' % (f, e)) ax[1].legend() fig.savefig(Path(save_dir) / 'results.png', dpi=200) def output_to_keypoint(output): # Convert model output to target format [batch_id, class_id, x, y, w, h, conf] targets = [] for i, o in enumerate(output): kpts = o[:,6:] o = o[:,:6] for index, (*box, conf, cls) in enumerate(o.cpu().numpy()): targets.append([i, cls, *list(*xyxy2xywh(np.array(box)[None])), conf, *list(kpts.cpu().numpy()[index])]) return np.array(targets) def plot_skeleton_kpts(im, kpts, steps, orig_shape=None): #Plot the skeleton and keypointsfor coco datatset palette = np.array([[255, 128, 0], [255, 153, 51], [255, 178, 102], [230, 230, 0], [255, 153, 255], [153, 204, 255], [255, 102, 255], [255, 51, 255], [102, 178, 255], [51, 153, 255], [255, 153, 153], [255, 102, 102], [255, 51, 51], [153, 255, 153], [102, 255, 102], [51, 255, 51], [0, 255, 0], [0, 0, 255], [255, 0, 0], [255, 255, 255]]) skeleton = [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]] pose_limb_color = palette[[9, 9, 9, 9, 7, 7, 7, 0, 0, 0, 0, 0, 16, 16, 16, 16, 16, 16, 16]] pose_kpt_color = palette[[16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9]] radius = 5 num_kpts = len(kpts) // steps for kid in range(num_kpts): r, g, b = pose_kpt_color[kid] x_coord, y_coord = kpts[steps * kid], kpts[steps * kid + 1] if not (x_coord % 640 == 0 or y_coord % 640 == 0): if steps == 3: conf = kpts[steps * kid + 2] if conf < 0.5: continue overlay = im.copy() alpha = 0.4 cv2.circle(overlay, (int(x_coord), int(y_coord)), 8, (int(220), int(237), int(245)), 8) cv2.circle(im, (int(x_coord), int(y_coord)), 5, (int(255), int(255), int(255)), -1) # im = output cv2.addWeighted(overlay, alpha, im, 1 - alpha, 0, im) for sk_id, sk in enumerate(skeleton): r, g, b = pose_limb_color[sk_id] pos1 = (int(kpts[(sk[0]-1)*steps]), int(kpts[(sk[0]-1)*steps+1])) pos2 = (int(kpts[(sk[1]-1)*steps]), int(kpts[(sk[1]-1)*steps+1])) if steps == 3: conf1 = kpts[(sk[0]-1)*steps+2] conf2 = kpts[(sk[1]-1)*steps+2] if conf1<0.5 or conf2<0.5: continue if pos1[0]%640 == 0 or pos1[1]%640==0 or pos1[0]<0 or pos1[1]<0: continue if pos2[0] % 640 == 0 or pos2[1] % 640 == 0 or pos2[0]<0 or pos2[1]<0: continue cv2.line(im, pos1, pos2, (int(255), int(255), int(255)), thickness=2) ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/torch_utils.py ================================================ # YOLOR PyTorch utils import datetime import logging import math import os import platform import subprocess import time from contextlib import contextmanager from copy import deepcopy from pathlib import Path import torch import torch.backends.cudnn as cudnn import torch.nn as nn import torch.nn.functional as F import torchvision try: import thop # for FLOPS computation except ImportError: thop = None logger = logging.getLogger(__name__) @contextmanager def torch_distributed_zero_first(local_rank: int): """ Decorator to make all processes in distributed training wait for each local_master to do something. """ if local_rank not in [-1, 0]: torch.distributed.barrier() yield if local_rank == 0: torch.distributed.barrier() def init_torch_seeds(seed=0): # Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html torch.manual_seed(seed) if seed == 0: # slower, more reproducible cudnn.benchmark, cudnn.deterministic = False, True else: # faster, less reproducible cudnn.benchmark, cudnn.deterministic = True, False def date_modified(path=__file__): # return human-readable file modification date, i.e. '2021-3-26' t = datetime.datetime.fromtimestamp(Path(path).stat().st_mtime) return f'{t.year}-{t.month}-{t.day}' def git_describe(path=Path(__file__).parent): # path must be a directory # return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe s = f'git -C {path} describe --tags --long --always' try: return subprocess.check_output(s, shell=True, stderr=subprocess.STDOUT).decode()[:-1] except subprocess.CalledProcessError as e: return '' # not a git repository def select_device(device='', batch_size=None): # device = 'cpu' or '0' or '0,1,2,3' s = f'YOLOR 🚀 {git_describe() or date_modified()} torch {torch.__version__} ' # string cpu = device.lower() == 'cpu' if cpu: os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False elif device: # non-cpu device requested os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested' # check availability cuda = not cpu and torch.cuda.is_available() if cuda: n = torch.cuda.device_count() if n > 1 and batch_size: # check that batch_size is compatible with device_count assert batch_size % n == 0, f'batch-size {batch_size} not multiple of GPU count {n}' space = ' ' * len(s) for i, d in enumerate(device.split(',') if device else range(n)): p = torch.cuda.get_device_properties(i) s += f"{'' if i == 0 else space}CUDA:{d} ({p.name}, {p.total_memory / 1024 ** 2}MB)\n" # bytes to MB else: s += 'CPU\n' logger.info(s.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else s) # emoji-safe return torch.device('cuda:0' if cuda else 'cpu') def time_synchronized(): # pytorch-accurate time if torch.cuda.is_available(): torch.cuda.synchronize() return time.time() def profile(x, ops, n=100, device=None): # profile a pytorch module or list of modules. Example usage: # x = torch.randn(16, 3, 640, 640) # input # m1 = lambda x: x * torch.sigmoid(x) # m2 = nn.SiLU() # profile(x, [m1, m2], n=100) # profile speed over 100 iterations device = device or torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') x = x.to(device) x.requires_grad = True print(torch.__version__, device.type, torch.cuda.get_device_properties(0) if device.type == 'cuda' else '') print(f"\n{'Params':>12s}{'GFLOPS':>12s}{'forward (ms)':>16s}{'backward (ms)':>16s}{'input':>24s}{'output':>24s}") for m in ops if isinstance(ops, list) else [ops]: m = m.to(device) if hasattr(m, 'to') else m # device m = m.half() if hasattr(m, 'half') and isinstance(x, torch.Tensor) and x.dtype is torch.float16 else m # type dtf, dtb, t = 0., 0., [0., 0., 0.] # dt forward, backward try: flops = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 # GFLOPS except: flops = 0 for _ in range(n): t[0] = time_synchronized() y = m(x) t[1] = time_synchronized() try: _ = y.sum().backward() t[2] = time_synchronized() except: # no backward method t[2] = float('nan') dtf += (t[1] - t[0]) * 1000 / n # ms per op forward dtb += (t[2] - t[1]) * 1000 / n # ms per op backward s_in = tuple(x.shape) if isinstance(x, torch.Tensor) else 'list' s_out = tuple(y.shape) if isinstance(y, torch.Tensor) else 'list' p = sum(list(x.numel() for x in m.parameters())) if isinstance(m, nn.Module) else 0 # parameters print(f'{p:12}{flops:12.4g}{dtf:16.4g}{dtb:16.4g}{str(s_in):>24s}{str(s_out):>24s}') def is_parallel(model): return type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel) def intersect_dicts(da, db, exclude=()): # Dictionary intersection of matching keys and shapes, omitting 'exclude' keys, using da values return {k: v for k, v in da.items() if k in db and not any(x in k for x in exclude) and v.shape == db[k].shape} def initialize_weights(model): for m in model.modules(): t = type(m) if t is nn.Conv2d: pass # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif t is nn.BatchNorm2d: m.eps = 1e-3 m.momentum = 0.03 elif t in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6]: m.inplace = True def find_modules(model, mclass=nn.Conv2d): # Finds layer indices matching module class 'mclass' return [i for i, m in enumerate(model.module_list) if isinstance(m, mclass)] def sparsity(model): # Return global model sparsity a, b = 0., 0. for p in model.parameters(): a += p.numel() b += (p == 0).sum() return b / a def prune(model, amount=0.3): # Prune model to requested global sparsity import torch.nn.utils.prune as prune print('Pruning model... ', end='') for name, m in model.named_modules(): if isinstance(m, nn.Conv2d): prune.l1_unstructured(m, name='weight', amount=amount) # prune prune.remove(m, 'weight') # make permanent print(' %.3g global sparsity' % sparsity(model)) def fuse_conv_and_bn(conv, bn): # Fuse convolution and batchnorm layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/ fusedconv = nn.Conv2d(conv.in_channels, conv.out_channels, kernel_size=conv.kernel_size, stride=conv.stride, padding=conv.padding, groups=conv.groups, bias=True).requires_grad_(False).to(conv.weight.device) # prepare filters w_conv = conv.weight.clone().view(conv.out_channels, -1) w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.shape)) # prepare spatial bias b_conv = torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps)) fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) return fusedconv def model_info(model, verbose=False, img_size=640): # Model information. img_size may be int or list, i.e. img_size=640 or img_size=[640, 320] n_p = sum(x.numel() for x in model.parameters()) # number parameters n_g = sum(x.numel() for x in model.parameters() if x.requires_grad) # number gradients if verbose: print('%5s %40s %9s %12s %20s %10s %10s' % ('layer', 'name', 'gradient', 'parameters', 'shape', 'mu', 'sigma')) for i, (name, p) in enumerate(model.named_parameters()): name = name.replace('module_list.', '') print('%5g %40s %9s %12g %20s %10.3g %10.3g' % (i, name, p.requires_grad, p.numel(), list(p.shape), p.mean(), p.std())) try: # FLOPS from thop import profile stride = max(int(model.stride.max()), 32) if hasattr(model, 'stride') else 32 img = torch.zeros((1, model.yaml.get('ch', 3), stride, stride), device=next(model.parameters()).device) # input flops = profile(deepcopy(model), inputs=(img,), verbose=False)[0] / 1E9 * 2 # stride GFLOPS img_size = img_size if isinstance(img_size, list) else [img_size, img_size] # expand if int/float fs = ', %.1f GFLOPS' % (flops * img_size[0] / stride * img_size[1] / stride) # 640x640 GFLOPS except (ImportError, Exception): fs = '' logger.info(f"Model Summary: {len(list(model.modules()))} layers, {n_p} parameters, {n_g} gradients{fs}") def load_classifier(name='resnet101', n=2): # Loads a pretrained model reshaped to n-class output model = torchvision.models.__dict__[name](pretrained=True) # ResNet model properties # input_size = [3, 224, 224] # input_space = 'RGB' # input_range = [0, 1] # mean = [0.485, 0.456, 0.406] # std = [0.229, 0.224, 0.225] # Reshape output to n classes filters = model.fc.weight.shape[1] model.fc.bias = nn.Parameter(torch.zeros(n), requires_grad=True) model.fc.weight = nn.Parameter(torch.zeros(n, filters), requires_grad=True) model.fc.out_features = n return model def scale_img(img, ratio=1.0, same_shape=False, gs=32): # img(16,3,256,416) # scales img(bs,3,y,x) by ratio constrained to gs-multiple if ratio == 1.0: return img else: h, w = img.shape[2:] s = (int(h * ratio), int(w * ratio)) # new size img = F.interpolate(img, size=s, mode='bilinear', align_corners=False) # resize if not same_shape: # pad/crop img h, w = [math.ceil(x * ratio / gs) * gs for x in (h, w)] return F.pad(img, [0, w - s[1], 0, h - s[0]], value=0.447) # value = imagenet mean def copy_attr(a, b, include=(), exclude=()): # Copy attributes from b to a, options to only include [...] and to exclude [...] for k, v in b.__dict__.items(): if (len(include) and k not in include) or k.startswith('_') or k in exclude: continue else: setattr(a, k, v) class ModelEMA: """ Model Exponential Moving Average from https://github.com/rwightman/pytorch-image-models Keep a moving average of everything in the model state_dict (parameters and buffers). This is intended to allow functionality like https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage A smoothed version of the weights is necessary for some training schemes to perform well. This class is sensitive where it is initialized in the sequence of model init, GPU assignment and distributed training wrappers. """ def __init__(self, model, decay=0.9999, updates=0): # Create EMA self.ema = deepcopy(model.module if is_parallel(model) else model).eval() # FP32 EMA # if next(model.parameters()).device.type != 'cpu': # self.ema.half() # FP16 EMA self.updates = updates # number of EMA updates self.decay = lambda x: decay * (1 - math.exp(-x / 2000)) # decay exponential ramp (to help early epochs) for p in self.ema.parameters(): p.requires_grad_(False) def update(self, model): # Update EMA parameters with torch.no_grad(): self.updates += 1 d = self.decay(self.updates) msd = model.module.state_dict() if is_parallel(model) else model.state_dict() # model state_dict for k, v in self.ema.state_dict().items(): if v.dtype.is_floating_point: v *= d v += (1. - d) * msd[k].detach() def update_attr(self, model, include=(), exclude=('process_group', 'reducer')): # Update EMA attributes copy_attr(self.ema, model, include, exclude) class BatchNormXd(torch.nn.modules.batchnorm._BatchNorm): def _check_input_dim(self, input): # The only difference between BatchNorm1d, BatchNorm2d, BatchNorm3d, etc # is this method that is overwritten by the sub-class # This original goal of this method was for tensor sanity checks # If you're ok bypassing those sanity checks (eg. if you trust your inference # to provide the right dimensional inputs), then you can just use this method # for easy conversion from SyncBatchNorm # (unfortunately, SyncBatchNorm does not store the original class - if it did # we could return the one that was originally created) return def revert_sync_batchnorm(module): # this is very similar to the function that it is trying to revert: # https://github.com/pytorch/pytorch/blob/c8b3686a3e4ba63dc59e5dcfe5db3430df256833/torch/nn/modules/batchnorm.py#L679 module_output = module if isinstance(module, torch.nn.modules.batchnorm.SyncBatchNorm): new_cls = BatchNormXd module_output = BatchNormXd(module.num_features, module.eps, module.momentum, module.affine, module.track_running_stats) if module.affine: with torch.no_grad(): module_output.weight = module.weight module_output.bias = module.bias module_output.running_mean = module.running_mean module_output.running_var = module.running_var module_output.num_batches_tracked = module.num_batches_tracked if hasattr(module, "qconfig"): module_output.qconfig = module.qconfig for name, child in module.named_children(): module_output.add_module(name, revert_sync_batchnorm(child)) del module return module_output class TracedModel(nn.Module): def __init__(self, model=None, device=None, img_size=(640,640)): super(TracedModel, self).__init__() print(" Convert model to Traced-model... ") self.stride = model.stride self.names = model.names self.model = model self.model = revert_sync_batchnorm(self.model) self.model.to('cpu') self.model.eval() self.detect_layer = self.model.model[-1] self.model.traced = True rand_example = torch.rand(1, 3, img_size, img_size) traced_script_module = torch.jit.trace(self.model, rand_example, strict=False) #traced_script_module = torch.jit.script(self.model) traced_script_module.save("traced_model.pt") print(" traced_script_module saved! ") self.model = traced_script_module self.model.to(device) self.detect_layer.to(device) print(" model is traced! \n") def forward(self, x, augment=False, profile=False): out = self.model(x) out = self.detect_layer(out) return out ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/wandb_logging/__init__.py ================================================ # init ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/wandb_logging/log_dataset.py ================================================ import argparse import yaml from wandb_utils import WandbLogger WANDB_ARTIFACT_PREFIX = 'wandb-artifact://' def create_dataset_artifact(opt): with open(opt.data) as f: data = yaml.load(f, Loader=yaml.SafeLoader) # data dict logger = WandbLogger(opt, '', None, data, job_type='Dataset Creation') if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--data', type=str, default='data/coco.yaml', help='data.yaml path') parser.add_argument('--single-cls', action='store_true', help='train as single-class dataset') parser.add_argument('--project', type=str, default='YOLOR', help='name of W&B Project') opt = parser.parse_args() opt.resume = False # Explicitly disallow resume check for dataset upload job create_dataset_artifact(opt) ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/wandb_logging/wandb_utils.py ================================================ import json import sys from pathlib import Path import torch import yaml from tqdm import tqdm # sys.path.append(str(Path(__file__).parent.parent.parent)) # add utils/ to path from utils.datasets import LoadImagesAndLabels from utils.datasets import img2label_paths from utils.general import colorstr, xywh2xyxy, check_dataset try: import wandb from wandb import init, finish except ImportError: wandb = None WANDB_ARTIFACT_PREFIX = 'wandb-artifact://' def remove_prefix(from_string, prefix=WANDB_ARTIFACT_PREFIX): return from_string[len(prefix):] def check_wandb_config_file(data_config_file): wandb_config = '_wandb.'.join(data_config_file.rsplit('.', 1)) # updated data.yaml path if Path(wandb_config).is_file(): return wandb_config return data_config_file def get_run_info(run_path): run_path = Path(remove_prefix(run_path, WANDB_ARTIFACT_PREFIX)) run_id = run_path.stem project = run_path.parent.stem model_artifact_name = 'run_' + run_id + '_model' return run_id, project, model_artifact_name def check_wandb_resume(opt): process_wandb_config_ddp_mode(opt) if opt.global_rank not in [-1, 0] else None if isinstance(opt.resume, str): if opt.resume.startswith(WANDB_ARTIFACT_PREFIX): if opt.global_rank not in [-1, 0]: # For resuming DDP runs run_id, project, model_artifact_name = get_run_info(opt.resume) api = wandb.Api() artifact = api.artifact(project + '/' + model_artifact_name + ':latest') modeldir = artifact.download() opt.weights = str(Path(modeldir) / "last.pt") return True return None def process_wandb_config_ddp_mode(opt): with open(opt.data) as f: data_dict = yaml.load(f, Loader=yaml.SafeLoader) # data dict train_dir, val_dir = None, None if isinstance(data_dict['train'], str) and data_dict['train'].startswith(WANDB_ARTIFACT_PREFIX): api = wandb.Api() train_artifact = api.artifact(remove_prefix(data_dict['train']) + ':' + opt.artifact_alias) train_dir = train_artifact.download() train_path = Path(train_dir) / 'data/images/' data_dict['train'] = str(train_path) if isinstance(data_dict['val'], str) and data_dict['val'].startswith(WANDB_ARTIFACT_PREFIX): api = wandb.Api() val_artifact = api.artifact(remove_prefix(data_dict['val']) + ':' + opt.artifact_alias) val_dir = val_artifact.download() val_path = Path(val_dir) / 'data/images/' data_dict['val'] = str(val_path) if train_dir or val_dir: ddp_data_path = str(Path(val_dir) / 'wandb_local_data.yaml') with open(ddp_data_path, 'w') as f: yaml.dump(data_dict, f) opt.data = ddp_data_path class WandbLogger(): def __init__(self, opt, name, run_id, data_dict, job_type='Training'): # Pre-training routine -- self.job_type = job_type self.wandb, self.wandb_run, self.data_dict = wandb, None if not wandb else wandb.run, data_dict # It's more elegant to stick to 1 wandb.init call, but useful config data is overwritten in the WandbLogger's wandb.init call if isinstance(opt.resume, str): # checks resume from artifact if opt.resume.startswith(WANDB_ARTIFACT_PREFIX): run_id, project, model_artifact_name = get_run_info(opt.resume) model_artifact_name = WANDB_ARTIFACT_PREFIX + model_artifact_name assert wandb, 'install wandb to resume wandb runs' # Resume wandb-artifact:// runs here| workaround for not overwriting wandb.config self.wandb_run = wandb.init(id=run_id, project=project, resume='allow') opt.resume = model_artifact_name elif self.wandb: self.wandb_run = wandb.init(config=opt, resume="allow", project='YOLOR' if opt.project == 'runs/train' else Path(opt.project).stem, name=name, job_type=job_type, id=run_id) if not wandb.run else wandb.run if self.wandb_run: if self.job_type == 'Training': if not opt.resume: wandb_data_dict = self.check_and_upload_dataset(opt) if opt.upload_dataset else data_dict # Info useful for resuming from artifacts self.wandb_run.config.opt = vars(opt) self.wandb_run.config.data_dict = wandb_data_dict self.data_dict = self.setup_training(opt, data_dict) if self.job_type == 'Dataset Creation': self.data_dict = self.check_and_upload_dataset(opt) else: prefix = colorstr('wandb: ') print(f"{prefix}Install Weights & Biases for YOLOR logging with 'pip install wandb' (recommended)") def check_and_upload_dataset(self, opt): assert wandb, 'Install wandb to upload dataset' check_dataset(self.data_dict) config_path = self.log_dataset_artifact(opt.data, opt.single_cls, 'YOLOR' if opt.project == 'runs/train' else Path(opt.project).stem) print("Created dataset config file ", config_path) with open(config_path) as f: wandb_data_dict = yaml.load(f, Loader=yaml.SafeLoader) return wandb_data_dict def setup_training(self, opt, data_dict): self.log_dict, self.current_epoch, self.log_imgs = {}, 0, 16 # Logging Constants self.bbox_interval = opt.bbox_interval if isinstance(opt.resume, str): modeldir, _ = self.download_model_artifact(opt) if modeldir: self.weights = Path(modeldir) / "last.pt" config = self.wandb_run.config opt.weights, opt.save_period, opt.batch_size, opt.bbox_interval, opt.epochs, opt.hyp = str( self.weights), config.save_period, config.total_batch_size, config.bbox_interval, config.epochs, \ config.opt['hyp'] data_dict = dict(self.wandb_run.config.data_dict) # eliminates the need for config file to resume if 'val_artifact' not in self.__dict__: # If --upload_dataset is set, use the existing artifact, don't download self.train_artifact_path, self.train_artifact = self.download_dataset_artifact(data_dict.get('train'), opt.artifact_alias) self.val_artifact_path, self.val_artifact = self.download_dataset_artifact(data_dict.get('val'), opt.artifact_alias) self.result_artifact, self.result_table, self.val_table, self.weights = None, None, None, None if self.train_artifact_path is not None: train_path = Path(self.train_artifact_path) / 'data/images/' data_dict['train'] = str(train_path) if self.val_artifact_path is not None: val_path = Path(self.val_artifact_path) / 'data/images/' data_dict['val'] = str(val_path) self.val_table = self.val_artifact.get("val") self.map_val_table_path() if self.val_artifact is not None: self.result_artifact = wandb.Artifact("run_" + wandb.run.id + "_progress", "evaluation") self.result_table = wandb.Table(["epoch", "id", "prediction", "avg_confidence"]) if opt.bbox_interval == -1: self.bbox_interval = opt.bbox_interval = (opt.epochs // 10) if opt.epochs > 10 else 1 return data_dict def download_dataset_artifact(self, path, alias): if isinstance(path, str) and path.startswith(WANDB_ARTIFACT_PREFIX): dataset_artifact = wandb.use_artifact(remove_prefix(path, WANDB_ARTIFACT_PREFIX) + ":" + alias) assert dataset_artifact is not None, "'Error: W&B dataset artifact doesn\'t exist'" datadir = dataset_artifact.download() return datadir, dataset_artifact return None, None def download_model_artifact(self, opt): if opt.resume.startswith(WANDB_ARTIFACT_PREFIX): model_artifact = wandb.use_artifact(remove_prefix(opt.resume, WANDB_ARTIFACT_PREFIX) + ":latest") assert model_artifact is not None, 'Error: W&B model artifact doesn\'t exist' modeldir = model_artifact.download() epochs_trained = model_artifact.metadata.get('epochs_trained') total_epochs = model_artifact.metadata.get('total_epochs') assert epochs_trained < total_epochs, 'training to %g epochs is finished, nothing to resume.' % ( total_epochs) return modeldir, model_artifact return None, None def log_model(self, path, opt, epoch, fitness_score, best_model=False): model_artifact = wandb.Artifact('run_' + wandb.run.id + '_model', type='model', metadata={ 'original_url': str(path), 'epochs_trained': epoch + 1, 'save period': opt.save_period, 'project': opt.project, 'total_epochs': opt.epochs, 'fitness_score': fitness_score }) model_artifact.add_file(str(path / 'last.pt'), name='last.pt') wandb.log_artifact(model_artifact, aliases=['latest', 'epoch ' + str(self.current_epoch), 'best' if best_model else '']) print("Saving model artifact on epoch ", epoch + 1) def log_dataset_artifact(self, data_file, single_cls, project, overwrite_config=False): with open(data_file) as f: data = yaml.load(f, Loader=yaml.SafeLoader) # data dict nc, names = (1, ['item']) if single_cls else (int(data['nc']), data['names']) names = {k: v for k, v in enumerate(names)} # to index dictionary self.train_artifact = self.create_dataset_table(LoadImagesAndLabels( data['train']), names, name='train') if data.get('train') else None self.val_artifact = self.create_dataset_table(LoadImagesAndLabels( data['val']), names, name='val') if data.get('val') else None if data.get('train'): data['train'] = WANDB_ARTIFACT_PREFIX + str(Path(project) / 'train') if data.get('val'): data['val'] = WANDB_ARTIFACT_PREFIX + str(Path(project) / 'val') path = data_file if overwrite_config else '_wandb.'.join(data_file.rsplit('.', 1)) # updated data.yaml path data.pop('download', None) with open(path, 'w') as f: yaml.dump(data, f) if self.job_type == 'Training': # builds correct artifact pipeline graph self.wandb_run.use_artifact(self.val_artifact) self.wandb_run.use_artifact(self.train_artifact) self.val_artifact.wait() self.val_table = self.val_artifact.get('val') self.map_val_table_path() else: self.wandb_run.log_artifact(self.train_artifact) self.wandb_run.log_artifact(self.val_artifact) return path def map_val_table_path(self): self.val_table_map = {} print("Mapping dataset") for i, data in enumerate(tqdm(self.val_table.data)): self.val_table_map[data[3]] = data[0] def create_dataset_table(self, dataset, class_to_id, name='dataset'): # TODO: Explore multiprocessing to slpit this loop parallely| This is essential for speeding up the the logging artifact = wandb.Artifact(name=name, type="dataset") img_files = tqdm([dataset.path]) if isinstance(dataset.path, str) and Path(dataset.path).is_dir() else None img_files = tqdm(dataset.img_files) if not img_files else img_files for img_file in img_files: if Path(img_file).is_dir(): artifact.add_dir(img_file, name='data/images') labels_path = 'labels'.join(dataset.path.rsplit('images', 1)) artifact.add_dir(labels_path, name='data/labels') else: artifact.add_file(img_file, name='data/images/' + Path(img_file).name) label_file = Path(img2label_paths([img_file])[0]) artifact.add_file(str(label_file), name='data/labels/' + label_file.name) if label_file.exists() else None table = wandb.Table(columns=["id", "train_image", "Classes", "name"]) class_set = wandb.Classes([{'id': id, 'name': name} for id, name in class_to_id.items()]) for si, (img, labels, paths, shapes) in enumerate(tqdm(dataset)): height, width = shapes[0] labels[:, 2:] = (xywh2xyxy(labels[:, 2:].view(-1, 4))) * torch.Tensor([width, height, width, height]) box_data, img_classes = [], {} for cls, *xyxy in labels[:, 1:].tolist(): cls = int(cls) box_data.append({"position": {"minX": xyxy[0], "minY": xyxy[1], "maxX": xyxy[2], "maxY": xyxy[3]}, "class_id": cls, "box_caption": "%s" % (class_to_id[cls]), "scores": {"acc": 1}, "domain": "pixel"}) img_classes[cls] = class_to_id[cls] boxes = {"ground_truth": {"box_data": box_data, "class_labels": class_to_id}} # inference-space table.add_data(si, wandb.Image(paths, classes=class_set, boxes=boxes), json.dumps(img_classes), Path(paths).name) artifact.add(table, name) return artifact def log_training_progress(self, predn, path, names): if self.val_table and self.result_table: class_set = wandb.Classes([{'id': id, 'name': name} for id, name in names.items()]) box_data = [] total_conf = 0 for *xyxy, conf, cls in predn.tolist(): if conf >= 0.25: box_data.append( {"position": {"minX": xyxy[0], "minY": xyxy[1], "maxX": xyxy[2], "maxY": xyxy[3]}, "class_id": int(cls), "box_caption": "%s %.3f" % (names[cls], conf), "scores": {"class_score": conf}, "domain": "pixel"}) total_conf = total_conf + conf boxes = {"predictions": {"box_data": box_data, "class_labels": names}} # inference-space id = self.val_table_map[Path(path).name] self.result_table.add_data(self.current_epoch, id, wandb.Image(self.val_table.data[id][1], boxes=boxes, classes=class_set), total_conf / max(1, len(box_data)) ) def log(self, log_dict): if self.wandb_run: for key, value in log_dict.items(): self.log_dict[key] = value def end_epoch(self, best_result=False): if self.wandb_run: wandb.log(self.log_dict) self.log_dict = {} if self.result_artifact: train_results = wandb.JoinedTable(self.val_table, self.result_table, "id") self.result_artifact.add(train_results, 'result') wandb.log_artifact(self.result_artifact, aliases=['latest', 'epoch ' + str(self.current_epoch), ('best' if best_result else '')]) self.result_table = wandb.Table(["epoch", "id", "prediction", "avg_confidence"]) self.result_artifact = wandb.Artifact("run_" + wandb.run.id + "_progress", "evaluation") def finish_run(self): if self.wandb_run: if self.log_dict: wandb.log(self.log_dict) wandb.run.finish() ================================================ FILE: asone/pose_estimators/yolov7_pose/utils/yolov7_pose_utils.py ================================================ import contextlib from pathlib import Path import sys @contextlib.contextmanager def yolov7_in_syspath(): """ Temporarily add yolov5 folder to `sys.path`. torch.hub handles it in the same way: https://github.com/pytorch/pytorch/blob/75024e228ca441290b6a1c2e564300ad507d7af6/torch/hub.py#L387 Proper fix for: #22, #134, #353, #1155, #1389, #1680, #2531, #3071 No need for such workarounds: #869, #1052, #2949 """ yolov7_folder_dir = str(Path(__file__).parents[1].absolute()) try: sys.path.insert(0, yolov7_folder_dir) yield finally: sys.path.remove(yolov7_folder_dir) ================================================ FILE: asone/pose_estimators/yolov7_pose/yolov7.py ================================================ import cv2 import time import torch import numpy as np import matplotlib.pyplot as plt from torchvision import transforms from asone.pose_estimators.yolov7_pose.utils.datasets import letterbox from asone.pose_estimators.yolov7_pose.utils.torch_utils import select_device from asone.pose_estimators.yolov7_pose.models.experimental import attempt_load from asone.pose_estimators.yolov7_pose.utils.general import non_max_suppression_kpt,strip_optimizer,xyxy2xywh from asone.pose_estimators.yolov7_pose.utils.plots import output_to_keypoint, plot_skeleton_kpts,colors,plot_one_box_kpt import os import sys class Yolov7PoseEstimator: def __init__(self, weights="yolov7-w6-pose.pt", use_cuda=True): self.weights=weights # device = device device = '0' if use_cuda else 'cpu' self.device = select_device(device) half = self.device.type != 'cpu' self.model = attempt_load(self.weights, map_location=self.device) _ = self.model.eval() @torch.no_grad() def estimate(self, frame): frame_height, frame_width = frame.shape[:2] image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = letterbox(image, (frame_width), stride=64, auto=True)[0] image_ = image.copy() image = transforms.ToTensor()(image) image = torch.tensor(np.array([image.numpy()])) image = image.to(self.device) image = image.float() start_time = time.time() with torch.no_grad(): output, _ = self.model(image) output = non_max_suppression_kpt(output, 0.25, 0.65, nc=self.model.yaml['nc'], nkpt=self.model.yaml['nkpt'], kpt_label=True) output = output_to_keypoint(output) # ............................................ # converting output to the format as yolov8 reformated_output = [] steps = 3 kpts = output new_size = frame.shape[:2] old_size = image.shape[2:] ratio = (new_size[0] / old_size[0], new_size[1] / old_size[1]) for idx in range(output.shape[0]): single_person_kpts = kpts[idx, 7:].T num_kpts = len(single_person_kpts) // steps xyc = [] for kid in range(num_kpts): x_coord, y_coord = single_person_kpts[steps * kid], single_person_kpts[steps * kid + 1] xyc.append([x_coord * ratio[1], y_coord* ratio[0], single_person_kpts[steps * kid + 2]]) reformated_output.append(xyc) out = np.array(reformated_output) output = torch.from_numpy(out) # exit() return output ================================================ FILE: asone/pose_estimators/yolov8_pose/__init__.py ================================================ from .yolov8 import Yolov8PoseEstimator __all__ = ['Yolov8PoseEstimator'] ================================================ FILE: asone/pose_estimators/yolov8_pose/plots.py ================================================ # Plotting utils import glob import math import os import random from copy import copy from pathlib import Path import cv2 import matplotlib import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns import torch import yaml from PIL import Image, ImageDraw, ImageFont from scipy.signal import butter, filtfilt from .general import xywh2xyxy, xyxy2xywh from .metrics import fitness # Settings matplotlib.rc('font', **{'size': 11}) matplotlib.use('Agg') # for writing to files only class Colors: # Ultralytics color palette https://ultralytics.com/ def __init__(self): self.palette = [self.hex2rgb(c) for c in matplotlib.colors.TABLEAU_COLORS.values()] self.n = len(self.palette) def __call__(self, i, bgr=False): c = self.palette[int(i) % self.n] return (c[2], c[1], c[0]) if bgr else c @staticmethod def hex2rgb(h): # rgb order (PIL) return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4)) def plot_one_box_kpt(x, im, color=None, label=None, line_thickness=3, kpt_label=False, kpts=None, steps=2, orig_shape=None): # Plots one bounding box on image 'im' using OpenCV assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to plot_on_box() input image.' tl = line_thickness or round(0.002 * (im.shape[0] + im.shape[1]) / 2) + 1 # line/font thickness color = color or [random.randint(0, 255) for _ in range(3)] c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) cv2.rectangle(im, c1, c2, (255,0,0), thickness=tl*1//3, lineType=cv2.LINE_AA) if label: if len(label.split(' ')) > 1: label = label.split(' ')[-1] tf = max(tl - 1, 1) # font thickness t_size = cv2.getTextSize(label, 0, fontScale=tl / 6, thickness=tf)[0] c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 cv2.rectangle(im, c1, c2, color, -1, cv2.LINE_AA) # filled cv2.putText(im, label, (c1[0], c1[1] - 2), 0, tl / 6, [225, 255, 255], thickness=tf//2, lineType=cv2.LINE_AA) if kpt_label: plot_skeleton_kpts(im, kpts, steps, orig_shape=orig_shape) colors = Colors() def color_list(): def hex2rgb(h): return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4)) return [hex2rgb(h) for h in matplotlib.colors.TABLEAU_COLORS.values()] # or BASE_ (8), CSS4_ (148), XKCD_ (949) def hist2d(x, y, n=100): # 2d histogram used in labels.png and evolve.png xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n) hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges)) xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1) yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1) return np.log(hist[xidx, yidx]) def butter_lowpass_filtfilt(data, cutoff=1500, fs=50000, order=5): # https://stackoverflow.com/questions/28536191/how-to-filter-smooth-with-scipy-numpy def butter_lowpass(cutoff, fs, order): nyq = 0.5 * fs normal_cutoff = cutoff / nyq return butter(order, normal_cutoff, btype='low', analog=False) b, a = butter_lowpass(cutoff, fs, order=order) return filtfilt(b, a, data) # forward-backward filter def plot_one_box(x, img, color=None, label=None, line_thickness=1): # Plots one bounding box on image img tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 2 # line/font thickness color = color or [random.randint(0, 255) for _ in range(3)] c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) if label: tf = max(tl - 1, 1) # font thickness t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) def plot_one_box_PIL(box, img, color=None, label=None, line_thickness=None): img = Image.fromarray(img) draw = ImageDraw.Draw(img) line_thickness = line_thickness or max(int(min(img.size) / 200), 2) draw.rectangle(box, width=line_thickness, outline=tuple(color)) # plot if label: fontsize = max(round(max(img.size) / 40), 12) font = ImageFont.truetype("Arial.ttf", fontsize) txt_width, txt_height = font.getsize(label) draw.rectangle([box[0], box[1] - txt_height + 4, box[0] + txt_width, box[1]], fill=tuple(color)) draw.text((box[0], box[1] - txt_height + 1), label, fill=(255, 255, 255), font=font) return np.asarray(img) def plot_wh_methods(): # from utils.plots import *; plot_wh_methods() # Compares the two methods for width-height anchor multiplication # https://github.com/ultralytics/yolov3/issues/168 x = np.arange(-4.0, 4.0, .1) ya = np.exp(x) yb = torch.sigmoid(torch.from_numpy(x)).numpy() * 2 fig = plt.figure(figsize=(6, 3), tight_layout=True) plt.plot(x, ya, '.-', label='YOLOv3') plt.plot(x, yb ** 2, '.-', label='YOLOR ^2') plt.plot(x, yb ** 1.6, '.-', label='YOLOR ^1.6') plt.xlim(left=-4, right=4) plt.ylim(bottom=0, top=6) plt.xlabel('input') plt.ylabel('output') plt.grid() plt.legend() fig.savefig('comparison.png', dpi=200) def output_to_target(output): # Convert model output to target format [batch_id, class_id, x, y, w, h, conf] targets = [] for i, o in enumerate(output): for *box, conf, cls in o.cpu().numpy(): targets.append([i, cls, *list(*xyxy2xywh(np.array(box)[None])), conf]) return np.array(targets) def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max_size=640, max_subplots=16): # Plot image grid with labels if isinstance(images, torch.Tensor): images = images.cpu().float().numpy() if isinstance(targets, torch.Tensor): targets = targets.cpu().numpy() # un-normalise if np.max(images[0]) <= 1: images *= 255 tl = 3 # line thickness tf = max(tl - 1, 1) # font thickness bs, _, h, w = images.shape # batch size, _, height, width bs = min(bs, max_subplots) # limit plot images ns = np.ceil(bs ** 0.5) # number of subplots (square) # Check if we should resize scale_factor = max_size / max(h, w) if scale_factor < 1: h = math.ceil(scale_factor * h) w = math.ceil(scale_factor * w) colors = color_list() # list of colors mosaic = np.full((int(ns * h), int(ns * w), 3), 255, dtype=np.uint8) # init for i, img in enumerate(images): if i == max_subplots: # if last batch has fewer images than we expect break block_x = int(w * (i // ns)) block_y = int(h * (i % ns)) img = img.transpose(1, 2, 0) if scale_factor < 1: img = cv2.resize(img, (w, h)) mosaic[block_y:block_y + h, block_x:block_x + w, :] = img if len(targets) > 0: image_targets = targets[targets[:, 0] == i] boxes = xywh2xyxy(image_targets[:, 2:6]).T classes = image_targets[:, 1].astype('int') labels = image_targets.shape[1] == 6 # labels if no conf column conf = None if labels else image_targets[:, 6] # check for confidence presence (label vs pred) if boxes.shape[1]: if boxes.max() <= 1.01: # if normalized with tolerance 0.01 boxes[[0, 2]] *= w # scale to pixels boxes[[1, 3]] *= h elif scale_factor < 1: # absolute coords need scale if image scales boxes *= scale_factor boxes[[0, 2]] += block_x boxes[[1, 3]] += block_y for j, box in enumerate(boxes.T): cls = int(classes[j]) color = colors[cls % len(colors)] cls = names[cls] if names else cls if labels or conf[j] > 0.25: # 0.25 conf thresh label = '%s' % cls if labels else '%s %.1f' % (cls, conf[j]) plot_one_box(box, mosaic, label=label, color=color, line_thickness=tl) # Draw image filename labels if paths: label = Path(paths[i]).name[:40] # trim to 40 char t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] cv2.putText(mosaic, label, (block_x + 5, block_y + t_size[1] + 5), 0, tl / 3, [220, 220, 220], thickness=tf, lineType=cv2.LINE_AA) # Image border cv2.rectangle(mosaic, (block_x, block_y), (block_x + w, block_y + h), (255, 255, 255), thickness=3) if fname: r = min(1280. / max(h, w) / ns, 1.0) # ratio to limit image size mosaic = cv2.resize(mosaic, (int(ns * w * r), int(ns * h * r)), interpolation=cv2.INTER_AREA) # cv2.imwrite(fname, cv2.cvtColor(mosaic, cv2.COLOR_BGR2RGB)) # cv2 save Image.fromarray(mosaic).save(fname) # PIL save return mosaic def plot_lr_scheduler(optimizer, scheduler, epochs=300, save_dir=''): # Plot LR simulating training for full epochs optimizer, scheduler = copy(optimizer), copy(scheduler) # do not modify originals y = [] for _ in range(epochs): scheduler.step() y.append(optimizer.param_groups[0]['lr']) plt.plot(y, '.-', label='LR') plt.xlabel('epoch') plt.ylabel('LR') plt.grid() plt.xlim(0, epochs) plt.ylim(0) plt.savefig(Path(save_dir) / 'LR.png', dpi=200) plt.close() def plot_test_txt(): # from utils.plots import *; plot_test() # Plot test.txt histograms x = np.loadtxt('test.txt', dtype=np.float32) box = xyxy2xywh(x[:, :4]) cx, cy = box[:, 0], box[:, 1] fig, ax = plt.subplots(1, 1, figsize=(6, 6), tight_layout=True) ax.hist2d(cx, cy, bins=600, cmax=10, cmin=0) ax.set_aspect('equal') plt.savefig('hist2d.png', dpi=300) fig, ax = plt.subplots(1, 2, figsize=(12, 6), tight_layout=True) ax[0].hist(cx, bins=600) ax[1].hist(cy, bins=600) plt.savefig('hist1d.png', dpi=200) def plot_targets_txt(): # from utils.plots import *; plot_targets_txt() # Plot targets.txt histograms x = np.loadtxt('targets.txt', dtype=np.float32).T s = ['x targets', 'y targets', 'width targets', 'height targets'] fig, ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True) ax = ax.ravel() for i in range(4): ax[i].hist(x[i], bins=100, label='%.3g +/- %.3g' % (x[i].mean(), x[i].std())) ax[i].legend() ax[i].set_title(s[i]) plt.savefig('targets.jpg', dpi=200) def plot_study_txt(path='', x=None): # from utils.plots import *; plot_study_txt() # Plot study.txt generated by test.py fig, ax = plt.subplots(2, 4, figsize=(10, 6), tight_layout=True) # ax = ax.ravel() fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4), tight_layout=True) # for f in [Path(path) / f'study_coco_{x}.txt' for x in ['yolor-p6', 'yolor-w6', 'yolor-e6', 'yolor-d6']]: for f in sorted(Path(path).glob('study*.txt')): y = np.loadtxt(f, dtype=np.float32, usecols=[0, 1, 2, 3, 7, 8, 9], ndmin=2).T x = np.arange(y.shape[1]) if x is None else np.array(x) s = ['P', 'R', 'mAP@.5', 'mAP@.5:.95', 't_inference (ms/img)', 't_NMS (ms/img)', 't_total (ms/img)'] # for i in range(7): # ax[i].plot(x, y[i], '.-', linewidth=2, markersize=8) # ax[i].set_title(s[i]) j = y[3].argmax() + 1 ax2.plot(y[6, 1:j], y[3, 1:j] * 1E2, '.-', linewidth=2, markersize=8, label=f.stem.replace('study_coco_', '').replace('yolo', 'YOLO')) ax2.plot(1E3 / np.array([209, 140, 97, 58, 35, 18]), [34.6, 40.5, 43.0, 47.5, 49.7, 51.5], 'k.-', linewidth=2, markersize=8, alpha=.25, label='EfficientDet') ax2.grid(alpha=0.2) ax2.set_yticks(np.arange(20, 60, 5)) ax2.set_xlim(0, 57) ax2.set_ylim(30, 55) ax2.set_xlabel('GPU Speed (ms/img)') ax2.set_ylabel('COCO AP val') ax2.legend(loc='lower right') plt.savefig(str(Path(path).name) + '.png', dpi=300) def plot_labels(labels, names=(), save_dir=Path(''), loggers=None): # plot dataset labels print('Plotting labels... ') c, b = labels[:, 0], labels[:, 1:].transpose() # classes, boxes nc = int(c.max() + 1) # number of classes colors = color_list() x = pd.DataFrame(b.transpose(), columns=['x', 'y', 'width', 'height']) # seaborn correlogram sns.pairplot(x, corner=True, diag_kind='auto', kind='hist', diag_kws=dict(bins=50), plot_kws=dict(pmax=0.9)) plt.savefig(save_dir / 'labels_correlogram.jpg', dpi=200) plt.close() # matplotlib labels matplotlib.use('svg') # faster ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True)[1].ravel() ax[0].hist(c, bins=np.linspace(0, nc, nc + 1) - 0.5, rwidth=0.8) ax[0].set_ylabel('instances') if 0 < len(names) < 30: ax[0].set_xticks(range(len(names))) ax[0].set_xticklabels(names, rotation=90, fontsize=10) else: ax[0].set_xlabel('classes') sns.histplot(x, x='x', y='y', ax=ax[2], bins=50, pmax=0.9) sns.histplot(x, x='width', y='height', ax=ax[3], bins=50, pmax=0.9) # rectangles labels[:, 1:3] = 0.5 # center labels[:, 1:] = xywh2xyxy(labels[:, 1:]) * 2000 img = Image.fromarray(np.ones((2000, 2000, 3), dtype=np.uint8) * 255) for cls, *box in labels[:1000]: ImageDraw.Draw(img).rectangle(box, width=1, outline=colors[int(cls) % 10]) # plot ax[1].imshow(img) ax[1].axis('off') for a in [0, 1, 2, 3]: for s in ['top', 'right', 'left', 'bottom']: ax[a].spines[s].set_visible(False) plt.savefig(save_dir / 'labels.jpg', dpi=200) matplotlib.use('Agg') plt.close() # loggers for k, v in loggers.items() or {}: if k == 'wandb' and v: v.log({"Labels": [v.Image(str(x), caption=x.name) for x in save_dir.glob('*labels*.jpg')]}, commit=False) def plot_evolution(yaml_file='data/hyp.finetune.yaml'): # from utils.plots import *; plot_evolution() # Plot hyperparameter evolution results in evolve.txt with open(yaml_file) as f: hyp = yaml.load(f, Loader=yaml.SafeLoader) x = np.loadtxt('evolve.txt', ndmin=2) f = fitness(x) # weights = (f - f.min()) ** 2 # for weighted results plt.figure(figsize=(10, 12), tight_layout=True) matplotlib.rc('font', **{'size': 8}) for i, (k, v) in enumerate(hyp.items()): y = x[:, i + 7] # mu = (y * weights).sum() / weights.sum() # best weighted result mu = y[f.argmax()] # best single result plt.subplot(6, 5, i + 1) plt.scatter(y, f, c=hist2d(y, f, 20), cmap='viridis', alpha=.8, edgecolors='none') plt.plot(mu, f.max(), 'k+', markersize=15) plt.title('%s = %.3g' % (k, mu), fontdict={'size': 9}) # limit to 40 characters if i % 5 != 0: plt.yticks([]) print('%15s: %.3g' % (k, mu)) plt.savefig('evolve.png', dpi=200) print('\nPlot saved as evolve.png') def profile_idetection(start=0, stop=0, labels=(), save_dir=''): # Plot iDetection '*.txt' per-image logs. from utils.plots import *; profile_idetection() ax = plt.subplots(2, 4, figsize=(12, 6), tight_layout=True)[1].ravel() s = ['Images', 'Free Storage (GB)', 'RAM Usage (GB)', 'Battery', 'dt_raw (ms)', 'dt_smooth (ms)', 'real-world FPS'] files = list(Path(save_dir).glob('frames*.txt')) for fi, f in enumerate(files): try: results = np.loadtxt(f, ndmin=2).T[:, 90:-30] # clip first and last rows n = results.shape[1] # number of rows x = np.arange(start, min(stop, n) if stop else n) results = results[:, x] t = (results[0] - results[0].min()) # set t0=0s results[0] = x for i, a in enumerate(ax): if i < len(results): label = labels[fi] if len(labels) else f.stem.replace('frames_', '') a.plot(t, results[i], marker='.', label=label, linewidth=1, markersize=5) a.set_title(s[i]) a.set_xlabel('time (s)') # if fi == len(files) - 1: # a.set_ylim(bottom=0) for side in ['top', 'right']: a.spines[side].set_visible(False) else: a.remove() except Exception as e: print('Warning: Plotting error for %s; %s' % (f, e)) ax[1].legend() plt.savefig(Path(save_dir) / 'idetection_profile.png', dpi=200) def plot_results_overlay(start=0, stop=0): # from utils.plots import *; plot_results_overlay() # Plot training 'results*.txt', overlaying train and val losses s = ['train', 'train', 'train', 'Precision', 'mAP@0.5', 'val', 'val', 'val', 'Recall', 'mAP@0.5:0.95'] # legends t = ['Box', 'Objectness', 'Classification', 'P-R', 'mAP-F1'] # titles for f in sorted(glob.glob('results*.txt') + glob.glob('../../Downloads/results*.txt')): results = np.loadtxt(f, usecols=[2, 3, 4, 8, 9, 12, 13, 14, 10, 11], ndmin=2).T n = results.shape[1] # number of rows x = range(start, min(stop, n) if stop else n) fig, ax = plt.subplots(1, 5, figsize=(14, 3.5), tight_layout=True) ax = ax.ravel() for i in range(5): for j in [i, i + 5]: y = results[j, x] ax[i].plot(x, y, marker='.', label=s[j]) # y_smooth = butter_lowpass_filtfilt(y) # ax[i].plot(x, np.gradient(y_smooth), marker='.', label=s[j]) ax[i].set_title(t[i]) ax[i].legend() ax[i].set_ylabel(f) if i == 0 else None # add filename fig.savefig(f.replace('.txt', '.png'), dpi=200) def plot_results(start=0, stop=0, bucket='', id=(), labels=(), save_dir=''): # Plot training 'results*.txt'. from utils.plots import *; plot_results(save_dir='runs/train/exp') fig, ax = plt.subplots(2, 5, figsize=(12, 6), tight_layout=True) ax = ax.ravel() s = ['Box', 'Objectness', 'Classification', 'Precision', 'Recall', 'val Box', 'val Objectness', 'val Classification', 'mAP@0.5', 'mAP@0.5:0.95'] if bucket: # files = ['https://storage.googleapis.com/%s/results%g.txt' % (bucket, x) for x in id] files = ['results%g.txt' % x for x in id] c = ('gsutil cp ' + '%s ' * len(files) + '.') % tuple('gs://%s/results%g.txt' % (bucket, x) for x in id) os.system(c) else: files = list(Path(save_dir).glob('results*.txt')) assert len(files), 'No results.txt files found in %s, nothing to plot.' % os.path.abspath(save_dir) for fi, f in enumerate(files): try: results = np.loadtxt(f, usecols=[2, 3, 4, 8, 9, 12, 13, 14, 10, 11], ndmin=2).T n = results.shape[1] # number of rows x = range(start, min(stop, n) if stop else n) for i in range(10): y = results[i, x] if i in [0, 1, 2, 5, 6, 7]: y[y == 0] = np.nan # don't show zero loss values # y /= y[0] # normalize label = labels[fi] if len(labels) else f.stem ax[i].plot(x, y, marker='.', label=label, linewidth=2, markersize=8) ax[i].set_title(s[i]) # if i in [5, 6, 7]: # share train and val loss y axes # ax[i].get_shared_y_axes().join(ax[i], ax[i - 5]) except Exception as e: print('Warning: Plotting error for %s; %s' % (f, e)) ax[1].legend() fig.savefig(Path(save_dir) / 'results.png', dpi=200) def output_to_keypoint(output): # Convert model output to target format [batch_id, class_id, x, y, w, h, conf] targets = [] for i, o in enumerate(output): kpts = o[:,6:] o = o[:,:6] for index, (*box, conf, cls) in enumerate(o.cpu().numpy()): targets.append([i, cls, *list(*xyxy2xywh(np.array(box)[None])), conf, *list(kpts.cpu().numpy()[index])]) return np.array(targets) def plot_skeleton_kpts(im, kpts, steps, orig_shape=None): #Plot the skeleton and keypointsfor coco datatset palette = np.array([[255, 128, 0], [255, 153, 51], [255, 178, 102], [230, 230, 0], [255, 153, 255], [153, 204, 255], [255, 102, 255], [255, 51, 255], [102, 178, 255], [51, 153, 255], [255, 153, 153], [255, 102, 102], [255, 51, 51], [153, 255, 153], [102, 255, 102], [51, 255, 51], [0, 255, 0], [0, 0, 255], [255, 0, 0], [255, 255, 255]]) skeleton = [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]] pose_limb_color = palette[[9, 9, 9, 9, 7, 7, 7, 0, 0, 0, 0, 0, 16, 16, 16, 16, 16, 16, 16]] pose_kpt_color = palette[[16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9]] radius = 5 num_kpts = len(kpts) // steps for kid in range(num_kpts): r, g, b = pose_kpt_color[kid] x_coord, y_coord = kpts[steps * kid], kpts[steps * kid + 1] if not (x_coord % 640 == 0 or y_coord % 640 == 0): if steps == 3: conf = kpts[steps * kid + 2] if conf < 0.5: continue cv2.circle(im, (int(x_coord), int(y_coord)), radius, (int(r), int(g), int(b)), -1) for sk_id, sk in enumerate(skeleton): r, g, b = pose_limb_color[sk_id] pos1 = (int(kpts[(sk[0]-1)*steps]), int(kpts[(sk[0]-1)*steps+1])) pos2 = (int(kpts[(sk[1]-1)*steps]), int(kpts[(sk[1]-1)*steps+1])) if steps == 3: conf1 = kpts[(sk[0]-1)*steps+2] conf2 = kpts[(sk[1]-1)*steps+2] if conf1<0.5 or conf2<0.5: continue if pos1[0]%640 == 0 or pos1[1]%640==0 or pos1[0]<0 or pos1[1]<0: continue if pos2[0] % 640 == 0 or pos2[1] % 640 == 0 or pos2[0]<0 or pos2[1]<0: continue cv2.line(im, pos1, pos2, (int(r), int(g), int(b)), thickness=2) ================================================ FILE: asone/pose_estimators/yolov8_pose/yolov8.py ================================================ from ultralytics import YOLO import torch class Yolov8PoseEstimator: def __init__(self, weights, use_cuda=True): self.model = YOLO(weights) self.device = 0 if use_cuda and torch.cuda.is_available() else 'cpu' def estimate(self, source): results = self.model(source, device=self.device) return results[0].keypoints ================================================ FILE: asone/recognizers/__init__.py ================================================ from asone.recognizers.easyocr_recognizer import EasyOCRRecognizer from asone.recognizers.recognizer import TextRecognizer __all__ = [ 'TextRecognizer', 'EasyOCRRecognizer'] ================================================ FILE: asone/recognizers/easyocr_recognizer/__init__.py ================================================ from .easyocr_recognizer import EasyOCRRecognizer __all__ = ['EasyOCRRecognizer'] ================================================ FILE: asone/recognizers/easyocr_recognizer/easyocr_recognizer.py ================================================ import easyocr import numpy as np from PIL import * class EasyOCRRecognizer: def __init__ (self, languages: list = ['en'], detect_network="craft", recog_network='standard', gpu=True): self.detect_network = detect_network self.gpu = gpu self.model = easyocr.Reader(languages, detect_network=self.detect_network, gpu=self.gpu) def detect(self, img): horizontal_list, free_list = self.model.detect(img) return horizontal_list, free_list def recognize(self, img, horizontal_list=None, free_list=None): horizontal_list = np.array(horizontal_list) horizontal_list = horizontal_list.astype(int) horizontal_list = horizontal_list.tolist() reformated_input = [] for bbx in horizontal_list: bbx[1], bbx[2] = bbx[2], bbx[1] reformated_input.append(bbx[:4]) horizontal_list = reformated_input free_list_format = [] if horizontal_list!=[]: for text in horizontal_list: xmin, xmax, ymin, ymax = text free_list_format.append([[xmin,ymin], [xmax,ymin], [xmax,ymax] , [xmin,ymax]]) free_list.extend(free_list_format) results = self.model.recognize(img, horizontal_list=[], free_list=free_list) formated_output = [] for data in results: x_list = [] y_list = [] for bbx in data[0]: x_list.append(int(bbx[0])) y_list.append(int(bbx[1])) formated_output.append([min(x_list), min(y_list), max(x_list), max(y_list), data[1], data[2]]) return formated_output ================================================ FILE: asone/recognizers/recognizer.py ================================================ from asone.recognizers import EasyOCRRecognizer from asone.recognizers.utils.recognizer_name import get_recognizer_name class TextRecognizer: def __init__(self, model_flag: int, languages: list=['en'], use_cuda: bool=True): self.model = self._select_recognizer(model_flag, use_cuda, languages) def _select_recognizer(self, model_flag, use_cuda, languages): recognizer_name = get_recognizer_name(model_flag) if recognizer_name == 'easyocr': _recognizer = EasyOCRRecognizer(gpu=use_cuda, languages=languages) return _recognizer def get_recognizer(self): return self.model def recognize(self, image: list, horizontal_list=None, free_list=None): print(horizontal_list) return self.model.recognize(image, horizontal_list=horizontal_list, free_list=free_list) ================================================ FILE: asone/recognizers/utils/__init__.py ================================================ ================================================ FILE: asone/recognizers/utils/recognizer_name.py ================================================ import os recognizers = { '200': 'easyocr' } def get_recognizer_name(model_flag): if model_flag == 200: recognizer = recognizers[str(model_flag)] return recognizer ================================================ FILE: asone/schemas/output_schemas.py ================================================ class ModelOutput: def __init__(self): self.dets = Detections() self.info = ImageInfo() class Detections: def __init__(self): self.bbox = None self.ids = [] self.score = [] self.class_ids = [] class ImageInfo: def __init__(self): self.image = None self.frame_no = None self.fps = None ================================================ FILE: asone/segmentors/__init__.py ================================================ from asone.segmentors.segmentor import Segmentor __all__ = ['Segmentor'] ================================================ FILE: asone/segmentors/segment_anything/__init__.py ================================================ ================================================ FILE: asone/segmentors/segment_anything/sam.py ================================================ import numpy as np import cv2 import torch from segment_anything import sam_model_registry, SamPredictor from asone.utils.utils import PathResolver class SamSegmentor: def __init__(self, weights: str=None, use_cuda: bool = True): self.device = "cuda" if use_cuda and torch.cuda.is_available() else "cpu" with PathResolver(): self.model = self.load_models(weights) def load_models(self, ckpt: str) -> None: sam = sam_model_registry["vit_h"](checkpoint=ckpt).to(device=self.device) model = SamPredictor(sam) return model def draw_masks_fromList(self, image, masks_generated, labels, colors=[0, 255, 0]): masked_image = image.copy() for i in range(len(masks_generated)): mask = masks_generated[i].squeeze() # Squeeze to remove singleton dimension color = np.asarray(colors, dtype='uint8') mask_color = np.expand_dims(mask, axis=-1) * color # Apply color to the mask # Apply the mask to the image masked_image = np.where(mask_color > 0, mask_color, masked_image) masked_image = masked_image.astype(np.uint8) return cv2.addWeighted(image, 0.5, masked_image, 0.5, 0) def create_mask(self, bbox_xyxy, image): self.model.set_image(image) input_boxes = torch.from_numpy(bbox_xyxy).to(self.device) transformed_boxes = self.model.transform.apply_boxes_torch(input_boxes, image.shape[:2]) masks, _, _ = self.model.predict_torch( point_coords=None, point_labels=None, boxes=transformed_boxes, multimask_output=False, ) # result_image = self.draw_masks_fromList(image, masks.cpu(), bbox_xyxy) return masks.cpu() ================================================ FILE: asone/segmentors/segmentor.py ================================================ import os from asone import utils from asone.segmentors.utils.weights_path import get_weight_path from asone.segmentors.segment_anything.sam import SamSegmentor class Segmentor: def __init__(self, model_flag, weights: str=None, use_cuda: bool=True): if weights is None: weight = get_weight_path(model_flag) if not os.path.exists(weight): utils.download_weights(weight) self.model = self._select_segmentor(model_flag, weight, use_cuda) def _select_segmentor(self, model_flag, weights, use_cuda): if model_flag == 171: model = SamSegmentor(weights, use_cuda=use_cuda) return model def create_mask(self, bbox_xyxy, image): return self.model.create_mask(bbox_xyxy, image) ================================================ FILE: asone/segmentors/utils/__init__.py ================================================ ================================================ FILE: asone/segmentors/utils/weights_path.py ================================================ import os weights = { # Segmentor '171':os.path.join('sam','weights','sam_vit_h_4b8939.pth'), } def get_weight_path(model_flag): if model_flag == 171: weight = weights[str(model_flag)] return weight ================================================ FILE: asone/trackers/__init__.py ================================================ from asone.trackers.byte_track.bytetracker import ByteTrack from asone.trackers.deep_sort.deepsort import DeepSort from asone.trackers.nor_fair.norfair import NorFair from asone.trackers.motpy.motpy import Motpy from asone.trackers.oc_sort.ocsort import OcSort from asone.trackers.strong_sort.strongsort import StrongSort from asone.trackers.tracker import Tracker __all__ = ['Tracker', 'ByteTrack', 'DeepSort', 'NorFair', 'Motpy', 'OcSort', 'StrongSort'] ================================================ FILE: asone/trackers/byte_track/__init__.py ================================================ ================================================ FILE: asone/trackers/byte_track/bytetracker.py ================================================ from .tracker.byte_tracker import BYTETracker import numpy as np from asone import utils class ByteTrack(object): def __init__(self, detector, min_box_area: int = 10, aspect_ratio_thresh:float= 3.0) -> None: self.min_box_area = min_box_area self.aspect_ratio_thresh = aspect_ratio_thresh self.min_box_area = min_box_area self.rgb_means = (0.485, 0.456, 0.406) self.std = (0.229, 0.224, 0.225) self.detector = detector try: self.input_shape = tuple(detector.model.get_inputs()[0].shape[2:]) except AttributeError as e: self.input_shape = (640, 640) self.tracker = BYTETracker(frame_rate=30) def detect_and_track(self, image: np.ndarray, config: dict) -> tuple: dets_xyxy, free_list = self.detector.detect(image, **config) image_info = {"width":image.shape[0],"height":image.shape[1]} class_ids = [] ids = [] bboxes_xyxy = [] scores = [] if isinstance(dets_xyxy, np.ndarray) and len(dets_xyxy) > 0: class_ids = [int(i) for i in dets_xyxy[:, -1].tolist()] bboxes_xyxy, ids, scores = self._tracker_update( dets_xyxy, image_info, ) return bboxes_xyxy, ids, scores, class_ids def _tracker_update(self, dets: np.ndarray, image_info: dict): online_targets = [] class_id = 0 if dets is not None: online_targets = self.tracker.update( dets[:, :-1], [image_info['height'], image_info['width']], [image_info['height'], image_info['width']], ) online_xyxys = [] online_ids = [] online_scores = [] for online_target in online_targets: tlwh = online_target.tlwh track_id = online_target.track_id vertical = tlwh[2] / tlwh[3] > self.aspect_ratio_thresh if tlwh[2] * tlwh[3] > self.min_box_area and not vertical: online_xyxys.append(utils.tlwh_to_xyxy(tlwh)) online_ids.append(track_id) online_scores.append(online_target.score) return online_xyxys, online_ids, online_scores ================================================ FILE: asone/trackers/byte_track/tracker/__init__.py ================================================ ================================================ FILE: asone/trackers/byte_track/tracker/basetrack.py ================================================ import numpy as np from collections import OrderedDict class TrackState(object): New = 0 Tracked = 1 Lost = 2 Removed = 3 class BaseTrack(object): _count = 0 track_id = 0 is_activated = False state = TrackState.New history = OrderedDict() features = [] curr_feature = None score = 0 start_frame = 0 frame_id = 0 time_since_update = 0 # multi-camera location = (np.inf, np.inf) @property def end_frame(self): return self.frame_id @staticmethod def next_id(): BaseTrack._count += 1 return BaseTrack._count def activate(self, *args): raise NotImplementedError def predict(self): raise NotImplementedError def update(self, *args, **kwargs): raise NotImplementedError def mark_lost(self): self.state = TrackState.Lost def mark_removed(self): self.state = TrackState.Removed ================================================ FILE: asone/trackers/byte_track/tracker/byte_tracker.py ================================================ import numpy as np from .kalman_filter import KalmanFilter from asone.trackers.byte_track.tracker import matching from .basetrack import BaseTrack, TrackState class STrack(BaseTrack): shared_kalman = KalmanFilter() def __init__(self, tlwh, score): # wait activate self._tlwh = np.asarray(tlwh, dtype=np.float) self.kalman_filter = None self.mean, self.covariance = None, None self.is_activated = False self.score = score self.tracklet_len = 0 def predict(self): mean_state = self.mean.copy() if self.state != TrackState.Tracked: mean_state[7] = 0 self.mean, self.covariance = self.kalman_filter.predict(mean_state, self.covariance) @staticmethod def multi_predict(stracks): if len(stracks) > 0: multi_mean = np.asarray([st.mean.copy() for st in stracks]) multi_covariance = np.asarray([st.covariance for st in stracks]) for i, st in enumerate(stracks): if st.state != TrackState.Tracked: multi_mean[i][7] = 0 multi_mean, multi_covariance = STrack.shared_kalman.multi_predict(multi_mean, multi_covariance) for i, (mean, cov) in enumerate(zip(multi_mean, multi_covariance)): stracks[i].mean = mean stracks[i].covariance = cov def activate(self, kalman_filter, frame_id): """Start a new tracklet""" self.kalman_filter = kalman_filter self.track_id = self.next_id() self.mean, self.covariance = self.kalman_filter.initiate(self.tlwh_to_xyah(self._tlwh)) self.tracklet_len = 0 self.state = TrackState.Tracked if frame_id == 1: self.is_activated = True # self.is_activated = True self.frame_id = frame_id self.start_frame = frame_id def re_activate(self, new_track, frame_id, new_id=False): self.mean, self.covariance = self.kalman_filter.update( self.mean, self.covariance, self.tlwh_to_xyah(new_track.tlwh) ) self.tracklet_len = 0 self.state = TrackState.Tracked self.is_activated = True self.frame_id = frame_id if new_id: self.track_id = self.next_id() self.score = new_track.score def update(self, new_track, frame_id): """ Update a matched track :type new_track: STrack :type frame_id: int :type update_feature: bool :return: """ self.frame_id = frame_id self.tracklet_len += 1 new_tlwh = new_track.tlwh self.mean, self.covariance = self.kalman_filter.update( self.mean, self.covariance, self.tlwh_to_xyah(new_tlwh)) self.state = TrackState.Tracked self.is_activated = True self.score = new_track.score @property # @jit(nopython=True) def tlwh(self): """Get current position in bounding box format `(top left x, top left y, width, height)`. """ if self.mean is None: return self._tlwh.copy() ret = self.mean[:4].copy() ret[2] *= ret[3] ret[:2] -= ret[2:] / 2 return ret @property # @jit(nopython=True) def tlbr(self): """Convert bounding box to format `(min x, min y, max x, max y)`, i.e., `(top left, bottom right)`. """ ret = self.tlwh.copy() ret[2:] += ret[:2] return ret @staticmethod # @jit(nopython=True) def tlwh_to_xyah(tlwh): """Convert bounding box to format `(center x, center y, aspect ratio, height)`, where the aspect ratio is `width / height`. """ ret = np.asarray(tlwh).copy() ret[:2] += ret[2:] / 2 ret[2] /= ret[3] return ret def to_xyah(self): return self.tlwh_to_xyah(self.tlwh) @staticmethod # @jit(nopython=True) def tlbr_to_tlwh(tlbr): ret = np.asarray(tlbr).copy() ret[2:] -= ret[:2] return ret @staticmethod # @jit(nopython=True) def tlwh_to_tlbr(tlwh): ret = np.asarray(tlwh).copy() ret[2:] += ret[:2] return ret def __repr__(self): return 'OT_{}_({}-{})'.format(self.track_id, self.start_frame, self.end_frame) class BYTETracker(object): def __init__(self, track_thresh=0.5,match_thresh=0.8, track_buffer=30, mot20=False, frame_rate=30): self.tracked_stracks = [] # type: list[STrack] self.lost_stracks = [] # type: list[STrack] self.removed_stracks = [] # type: list[STrack] self.track_thresh = track_thresh self.track_buffer = track_buffer self.mot20 = mot20 self.match_thresh = match_thresh self.frame_id = 0 self.det_thresh = track_thresh + 0.1 self.buffer_size = int(frame_rate / 30.0 * self.track_buffer) self.max_time_lost = self.buffer_size self.kalman_filter = KalmanFilter() def update(self, output_results, img_info, img_size): self.frame_id += 1 activated_starcks = [] refind_stracks = [] lost_stracks = [] removed_stracks = [] if output_results.shape[1] == 5: scores = output_results[:, 4] bboxes = output_results[:, :4] else: output_results = output_results.cpu().numpy() scores = output_results[:, 4] * output_results[:, 5] bboxes = output_results[:, :4] # x1y1x2y2 img_h, img_w = img_info[0], img_info[1] scale = min(img_size[0] / float(img_h), img_size[1] / float(img_w)) bboxes /= scale remain_inds = scores > self.track_thresh inds_low = scores > 0.1 inds_high = scores < self.track_thresh inds_second = np.logical_and(inds_low, inds_high) dets_second = bboxes[inds_second] dets = bboxes[remain_inds] scores_keep = scores[remain_inds] scores_second = scores[inds_second] if len(dets) > 0: '''Detections''' detections = [STrack(STrack.tlbr_to_tlwh(tlbr), s) for (tlbr, s) in zip(dets, scores_keep)] else: detections = [] ''' Add newly detected tracklets to tracked_stracks''' unconfirmed = [] tracked_stracks = [] # type: list[STrack] for track in self.tracked_stracks: if not track.is_activated: unconfirmed.append(track) else: tracked_stracks.append(track) ''' Step 2: First association, with high score detection boxes''' strack_pool = joint_stracks(tracked_stracks, self.lost_stracks) # Predict the current location with KF STrack.multi_predict(strack_pool) dists = matching.iou_distance(strack_pool, detections) if not self.mot20: dists = matching.fuse_score(dists, detections) matches, u_track, u_detection = matching.linear_assignment(dists, thresh=self.match_thresh) for itracked, idet in matches: track = strack_pool[itracked] det = detections[idet] if track.state == TrackState.Tracked: track.update(detections[idet], self.frame_id) activated_starcks.append(track) else: track.re_activate(det, self.frame_id, new_id=False) refind_stracks.append(track) ''' Step 3: Second association, with low score detection boxes''' # association the untrack to the low score detections if len(dets_second) > 0: '''Detections''' detections_second = [STrack(STrack.tlbr_to_tlwh(tlbr), s) for (tlbr, s) in zip(dets_second, scores_second)] else: detections_second = [] r_tracked_stracks = [strack_pool[i] for i in u_track if strack_pool[i].state == TrackState.Tracked] dists = matching.iou_distance(r_tracked_stracks, detections_second) matches, u_track, u_detection_second = matching.linear_assignment(dists, thresh=0.5) for itracked, idet in matches: track = r_tracked_stracks[itracked] det = detections_second[idet] if track.state == TrackState.Tracked: track.update(det, self.frame_id) activated_starcks.append(track) else: track.re_activate(det, self.frame_id, new_id=False) refind_stracks.append(track) for it in u_track: track = r_tracked_stracks[it] if not track.state == TrackState.Lost: track.mark_lost() lost_stracks.append(track) '''Deal with unconfirmed tracks, usually tracks with only one beginning frame''' detections = [detections[i] for i in u_detection] dists = matching.iou_distance(unconfirmed, detections) if not self.mot20: dists = matching.fuse_score(dists, detections) matches, u_unconfirmed, u_detection = matching.linear_assignment(dists, thresh=0.7) for itracked, idet in matches: unconfirmed[itracked].update(detections[idet], self.frame_id) activated_starcks.append(unconfirmed[itracked]) for it in u_unconfirmed: track = unconfirmed[it] track.mark_removed() removed_stracks.append(track) """ Step 4: Init new stracks""" for inew in u_detection: track = detections[inew] if track.score < self.det_thresh: continue track.activate(self.kalman_filter, self.frame_id) activated_starcks.append(track) """ Step 5: Update state""" for track in self.lost_stracks: if self.frame_id - track.end_frame > self.max_time_lost: track.mark_removed() removed_stracks.append(track) # print('Ramained match {} s'.format(t4-t3)) self.tracked_stracks = [t for t in self.tracked_stracks if t.state == TrackState.Tracked] self.tracked_stracks = joint_stracks(self.tracked_stracks, activated_starcks) self.tracked_stracks = joint_stracks(self.tracked_stracks, refind_stracks) self.lost_stracks = sub_stracks(self.lost_stracks, self.tracked_stracks) self.lost_stracks.extend(lost_stracks) self.lost_stracks = sub_stracks(self.lost_stracks, self.removed_stracks) self.removed_stracks.extend(removed_stracks) self.tracked_stracks, self.lost_stracks = remove_duplicate_stracks(self.tracked_stracks, self.lost_stracks) # get scores of lost tracks output_stracks = [track for track in self.tracked_stracks if track.is_activated] return output_stracks def joint_stracks(tlista, tlistb): exists = {} res = [] for t in tlista: exists[t.track_id] = 1 res.append(t) for t in tlistb: tid = t.track_id if not exists.get(tid, 0): exists[tid] = 1 res.append(t) return res def sub_stracks(tlista, tlistb): stracks = {} for t in tlista: stracks[t.track_id] = t for t in tlistb: tid = t.track_id if stracks.get(tid, 0): del stracks[tid] return list(stracks.values()) def remove_duplicate_stracks(stracksa, stracksb): pdist = matching.iou_distance(stracksa, stracksb) pairs = np.where(pdist < 0.15) dupa, dupb = list(), list() for p, q in zip(*pairs): timep = stracksa[p].frame_id - stracksa[p].start_frame timeq = stracksb[q].frame_id - stracksb[q].start_frame if timep > timeq: dupb.append(q) else: dupa.append(p) resa = [t for i, t in enumerate(stracksa) if not i in dupa] resb = [t for i, t in enumerate(stracksb) if not i in dupb] return resa, resb ================================================ FILE: asone/trackers/byte_track/tracker/kalman_filter.py ================================================ # vim: expandtab:ts=4:sw=4 import numpy as np import scipy.linalg """ Table for the 0.95 quantile of the chi-square distribution with N degrees of freedom (contains values for N=1, ..., 9). Taken from MATLAB/Octave's chi2inv function and used as Mahalanobis gating threshold. """ chi2inv95 = { 1: 3.8415, 2: 5.9915, 3: 7.8147, 4: 9.4877, 5: 11.070, 6: 12.592, 7: 14.067, 8: 15.507, 9: 16.919} class KalmanFilter(object): """ A simple Kalman filter for tracking bounding boxes in image space. The 8-dimensional state space x, y, a, h, vx, vy, va, vh contains the bounding box center position (x, y), aspect ratio a, height h, and their respective velocities. Object motion follows a constant velocity model. The bounding box location (x, y, a, h) is taken as direct observation of the state space (linear observation model). """ def __init__(self): ndim, dt = 4, 1. # Create Kalman filter model matrices. self._motion_mat = np.eye(2 * ndim, 2 * ndim) for i in range(ndim): self._motion_mat[i, ndim + i] = dt self._update_mat = np.eye(ndim, 2 * ndim) # Motion and observation uncertainty are chosen relative to the current # state estimate. These weights control the amount of uncertainty in # the model. This is a bit hacky. self._std_weight_position = 1. / 20 self._std_weight_velocity = 1. / 160 def initiate(self, measurement): """Create track from unassociated measurement. Parameters ---------- measurement : ndarray Bounding box coordinates (x, y, a, h) with center position (x, y), aspect ratio a, and height h. Returns ------- (ndarray, ndarray) Returns the mean vector (8 dimensional) and covariance matrix (8x8 dimensional) of the new track. Unobserved velocities are initialized to 0 mean. """ mean_pos = measurement mean_vel = np.zeros_like(mean_pos) mean = np.r_[mean_pos, mean_vel] std = [ 2 * self._std_weight_position * measurement[3], 2 * self._std_weight_position * measurement[3], 1e-2, 2 * self._std_weight_position * measurement[3], 10 * self._std_weight_velocity * measurement[3], 10 * self._std_weight_velocity * measurement[3], 1e-5, 10 * self._std_weight_velocity * measurement[3]] covariance = np.diag(np.square(std)) return mean, covariance def predict(self, mean, covariance): """Run Kalman filter prediction step. Parameters ---------- mean : ndarray The 8 dimensional mean vector of the object state at the previous time step. covariance : ndarray The 8x8 dimensional covariance matrix of the object state at the previous time step. Returns ------- (ndarray, ndarray) Returns the mean vector and covariance matrix of the predicted state. Unobserved velocities are initialized to 0 mean. """ std_pos = [ self._std_weight_position * mean[3], self._std_weight_position * mean[3], 1e-2, self._std_weight_position * mean[3]] std_vel = [ self._std_weight_velocity * mean[3], self._std_weight_velocity * mean[3], 1e-5, self._std_weight_velocity * mean[3]] motion_cov = np.diag(np.square(np.r_[std_pos, std_vel])) #mean = np.dot(self._motion_mat, mean) mean = np.dot(mean, self._motion_mat.T) covariance = np.linalg.multi_dot(( self._motion_mat, covariance, self._motion_mat.T)) + motion_cov return mean, covariance def project(self, mean, covariance): """Project state distribution to measurement space. Parameters ---------- mean : ndarray The state's mean vector (8 dimensional array). covariance : ndarray The state's covariance matrix (8x8 dimensional). Returns ------- (ndarray, ndarray) Returns the projected mean and covariance matrix of the given state estimate. """ std = [ self._std_weight_position * mean[3], self._std_weight_position * mean[3], 1e-1, self._std_weight_position * mean[3]] innovation_cov = np.diag(np.square(std)) mean = np.dot(self._update_mat, mean) covariance = np.linalg.multi_dot(( self._update_mat, covariance, self._update_mat.T)) return mean, covariance + innovation_cov def multi_predict(self, mean, covariance): """Run Kalman filter prediction step (Vectorized version). Parameters ---------- mean : ndarray The Nx8 dimensional mean matrix of the object states at the previous time step. covariance : ndarray The Nx8x8 dimensional covariance matrics of the object states at the previous time step. Returns ------- (ndarray, ndarray) Returns the mean vector and covariance matrix of the predicted state. Unobserved velocities are initialized to 0 mean. """ std_pos = [ self._std_weight_position * mean[:, 3], self._std_weight_position * mean[:, 3], 1e-2 * np.ones_like(mean[:, 3]), self._std_weight_position * mean[:, 3]] std_vel = [ self._std_weight_velocity * mean[:, 3], self._std_weight_velocity * mean[:, 3], 1e-5 * np.ones_like(mean[:, 3]), self._std_weight_velocity * mean[:, 3]] sqr = np.square(np.r_[std_pos, std_vel]).T motion_cov = [] for i in range(len(mean)): motion_cov.append(np.diag(sqr[i])) motion_cov = np.asarray(motion_cov) mean = np.dot(mean, self._motion_mat.T) left = np.dot(self._motion_mat, covariance).transpose((1, 0, 2)) covariance = np.dot(left, self._motion_mat.T) + motion_cov return mean, covariance def update(self, mean, covariance, measurement): """Run Kalman filter correction step. Parameters ---------- mean : ndarray The predicted state's mean vector (8 dimensional). covariance : ndarray The state's covariance matrix (8x8 dimensional). measurement : ndarray The 4 dimensional measurement vector (x, y, a, h), where (x, y) is the center position, a the aspect ratio, and h the height of the bounding box. Returns ------- (ndarray, ndarray) Returns the measurement-corrected state distribution. """ projected_mean, projected_cov = self.project(mean, covariance) chol_factor, lower = scipy.linalg.cho_factor( projected_cov, lower=True, check_finite=False) kalman_gain = scipy.linalg.cho_solve( (chol_factor, lower), np.dot(covariance, self._update_mat.T).T, check_finite=False).T innovation = measurement - projected_mean new_mean = mean + np.dot(innovation, kalman_gain.T) new_covariance = covariance - np.linalg.multi_dot(( kalman_gain, projected_cov, kalman_gain.T)) return new_mean, new_covariance def gating_distance(self, mean, covariance, measurements, only_position=False, metric='maha'): """Compute gating distance between state distribution and measurements. A suitable distance threshold can be obtained from `chi2inv95`. If `only_position` is False, the chi-square distribution has 4 degrees of freedom, otherwise 2. Parameters ---------- mean : ndarray Mean vector over the state distribution (8 dimensional). covariance : ndarray Covariance of the state distribution (8x8 dimensional). measurements : ndarray An Nx4 dimensional matrix of N measurements, each in format (x, y, a, h) where (x, y) is the bounding box center position, a the aspect ratio, and h the height. only_position : Optional[bool] If True, distance computation is done with respect to the bounding box center position only. Returns ------- ndarray Returns an array of length N, where the i-th element contains the squared Mahalanobis distance between (mean, covariance) and `measurements[i]`. """ mean, covariance = self.project(mean, covariance) if only_position: mean, covariance = mean[:2], covariance[:2, :2] measurements = measurements[:, :2] d = measurements - mean if metric == 'gaussian': return np.sum(d * d, axis=1) elif metric == 'maha': cholesky_factor = np.linalg.cholesky(covariance) z = scipy.linalg.solve_triangular( cholesky_factor, d.T, lower=True, check_finite=False, overwrite_b=True) squared_maha = np.sum(z * z, axis=0) return squared_maha else: raise ValueError('invalid distance metric') ================================================ FILE: asone/trackers/byte_track/tracker/matching.py ================================================ import numpy as np import scipy import lap from scipy.spatial.distance import cdist from cython_bbox import bbox_overlaps as bbox_ious from asone.trackers.byte_track.tracker import kalman_filter def merge_matches(m1, m2, shape): O,P,Q = shape m1 = np.asarray(m1) m2 = np.asarray(m2) M1 = scipy.sparse.coo_matrix((np.ones(len(m1)), (m1[:, 0], m1[:, 1])), shape=(O, P)) M2 = scipy.sparse.coo_matrix((np.ones(len(m2)), (m2[:, 0], m2[:, 1])), shape=(P, Q)) mask = M1*M2 match = mask.nonzero() match = list(zip(match[0], match[1])) unmatched_O = tuple(set(range(O)) - set([i for i, j in match])) unmatched_Q = tuple(set(range(Q)) - set([j for i, j in match])) return match, unmatched_O, unmatched_Q def _indices_to_matches(cost_matrix, indices, thresh): matched_cost = cost_matrix[tuple(zip(*indices))] matched_mask = (matched_cost <= thresh) matches = indices[matched_mask] unmatched_a = tuple(set(range(cost_matrix.shape[0])) - set(matches[:, 0])) unmatched_b = tuple(set(range(cost_matrix.shape[1])) - set(matches[:, 1])) return matches, unmatched_a, unmatched_b def linear_assignment(cost_matrix, thresh): if cost_matrix.size == 0: return np.empty((0, 2), dtype=int), tuple(range(cost_matrix.shape[0])), tuple(range(cost_matrix.shape[1])) matches, unmatched_a, unmatched_b = [], [], [] cost, x, y = lap.lapjv(cost_matrix, extend_cost=True, cost_limit=thresh) for ix, mx in enumerate(x): if mx >= 0: matches.append([ix, mx]) unmatched_a = np.where(x < 0)[0] unmatched_b = np.where(y < 0)[0] matches = np.asarray(matches) return matches, unmatched_a, unmatched_b def ious(atlbrs, btlbrs): """ Compute cost based on IoU :type atlbrs: list[tlbr] | np.ndarray :type atlbrs: list[tlbr] | np.ndarray :rtype ious np.ndarray """ ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float64) if ious.size == 0: return ious ious = bbox_ious( np.ascontiguousarray(atlbrs, dtype=np.float64), np.ascontiguousarray(btlbrs, dtype=np.float64) ) return ious def iou_distance(atracks, btracks): """ Compute cost based on IoU :type atracks: list[STrack] :type btracks: list[STrack] :rtype cost_matrix np.ndarray """ if (len(atracks)>0 and isinstance(atracks[0], np.ndarray)) or (len(btracks) > 0 and isinstance(btracks[0], np.ndarray)): atlbrs = atracks btlbrs = btracks else: atlbrs = [track.tlbr for track in atracks] btlbrs = [track.tlbr for track in btracks] _ious = ious(atlbrs, btlbrs) cost_matrix = 1 - _ious return cost_matrix def v_iou_distance(atracks, btracks): """ Compute cost based on IoU :type atracks: list[STrack] :type btracks: list[STrack] :rtype cost_matrix np.ndarray """ if (len(atracks)>0 and isinstance(atracks[0], np.ndarray)) or (len(btracks) > 0 and isinstance(btracks[0], np.ndarray)): atlbrs = atracks btlbrs = btracks else: atlbrs = [track.tlwh_to_tlbr(track.pred_bbox) for track in atracks] btlbrs = [track.tlwh_to_tlbr(track.pred_bbox) for track in btracks] _ious = ious(atlbrs, btlbrs) cost_matrix = 1 - _ious return cost_matrix def embedding_distance(tracks, detections, metric='cosine'): """ :param tracks: list[STrack] :param detections: list[BaseTrack] :param metric: :return: cost_matrix np.ndarray """ cost_matrix = np.zeros((len(tracks), len(detections)), dtype=np.float64) if cost_matrix.size == 0: return cost_matrix det_features = np.asarray([track.curr_feat for track in detections], dtype=np.float64) #for i, track in enumerate(tracks): #cost_matrix[i, :] = np.maximum(0.0, cdist(track.smooth_feat.reshape(1,-1), det_features, metric)) track_features = np.asarray([track.smooth_feat for track in tracks], dtype=np.float64) cost_matrix = np.maximum(0.0, cdist(track_features, det_features, metric)) # Nomalized features return cost_matrix def gate_cost_matrix(kf, cost_matrix, tracks, detections, only_position=False): if cost_matrix.size == 0: return cost_matrix gating_dim = 2 if only_position else 4 gating_threshold = kalman_filter.chi2inv95[gating_dim] measurements = np.asarray([det.to_xyah() for det in detections]) for row, track in enumerate(tracks): gating_distance = kf.gating_distance( track.mean, track.covariance, measurements, only_position) cost_matrix[row, gating_distance > gating_threshold] = np.inf return cost_matrix def fuse_motion(kf, cost_matrix, tracks, detections, only_position=False, lambda_=0.98): if cost_matrix.size == 0: return cost_matrix gating_dim = 2 if only_position else 4 gating_threshold = kalman_filter.chi2inv95[gating_dim] measurements = np.asarray([det.to_xyah() for det in detections]) for row, track in enumerate(tracks): gating_distance = kf.gating_distance( track.mean, track.covariance, measurements, only_position, metric='maha') cost_matrix[row, gating_distance > gating_threshold] = np.inf cost_matrix[row] = lambda_ * cost_matrix[row] + (1 - lambda_) * gating_distance return cost_matrix def fuse_iou(cost_matrix, tracks, detections): if cost_matrix.size == 0: return cost_matrix reid_sim = 1 - cost_matrix iou_dist = iou_distance(tracks, detections) iou_sim = 1 - iou_dist fuse_sim = reid_sim * (1 + iou_sim) / 2 det_scores = np.array([det.score for det in detections]) det_scores = np.expand_dims(det_scores, axis=0).repeat(cost_matrix.shape[0], axis=0) #fuse_sim = fuse_sim * (1 + det_scores) / 2 fuse_cost = 1 - fuse_sim return fuse_cost def fuse_score(cost_matrix, detections): if cost_matrix.size == 0: return cost_matrix iou_sim = 1 - cost_matrix det_scores = np.array([det.score for det in detections]) det_scores = np.expand_dims(det_scores, axis=0).repeat(cost_matrix.shape[0], axis=0) fuse_sim = iou_sim * det_scores fuse_cost = 1 - fuse_sim return fuse_cost ================================================ FILE: asone/trackers/deep_sort/__init__.py ================================================ ================================================ FILE: asone/trackers/deep_sort/deepsort.py ================================================ from .tracker import build_tracker import numpy as np import os from asone import utils class DeepSort: def __init__(self, detector, weights=None, use_cuda=True): if weights is None: weights = os.path.join(os.path.dirname( os.path.abspath(__file__)), "tracker/deep/checkpoint/ckpt.t7") if not os.path.exists(weights): utils.download_weights(weights) cfg = { 'MAX_DIST': 0.2, 'MIN_CONFIDENCE': 0.3, 'NMS_MAX_OVERLAP': 0.5, 'MAX_IOU_DISTANCE': 0.7, 'MAX_AGE': 70, 'N_INIT': 3, 'NN_BUDGET': 100 } self.tracker = build_tracker(weights, cfg, use_cuda=use_cuda) self.detector = detector try: self.input_shape = tuple(detector.model.get_inputs()[0].shape[2:]) except AttributeError as e: self.input_shape = (640, 640) def detect_and_track(self, image: np.ndarray, config: dict) -> tuple: dets_xyxy, image_info = self.detector.detect( image, **config ) image_info['im0'] = image class_ids = [] ids = [] bboxes_xyxy = [] scores = [] if isinstance(dets_xyxy, np.ndarray) and len(dets_xyxy) > 0: class_ids = dets_xyxy[:, -1].tolist() bboxes_xyxy, ids, class_ids = self._tracker_update( dets_xyxy, image_info, ) return bboxes_xyxy, ids, [], class_ids def _tracker_update(self, dets_xyxy: np.ndarray, image_info: dict): bbox_xyxy = [] ids = [] object_id = [] if dets_xyxy is not None: dets_xywh = np.array([np.array(utils.xyxy_to_xywh(det)) for det in dets_xyxy[:, :4]]) outputs = self.tracker.update( dets_xywh, dets_xyxy[:, -2].tolist(), dets_xyxy[:, -1].tolist(), image_info['im0']) if len(outputs) > 0: bbox_xyxy = outputs[:, :4] ids = outputs[:, -2] object_id = outputs[:, -1] return bbox_xyxy, ids, object_id ================================================ FILE: asone/trackers/deep_sort/tracker/.gitignore ================================================ # Folders __pycache__/ build/ *.egg-info # Files *.weights *.t7 *.mp4 *.avi *.so *.txt ================================================ FILE: asone/trackers/deep_sort/tracker/README.md ================================================ # Deep Sort with PyTorch ![](demo/demo.gif) ## Update(1-1-2020) Changes - fix bugs - refactor code - accerate detection by adding nms on gpu ## Latest Update(07-22) Changes - bug fix (Thanks @JieChen91 and @yingsen1 for bug reporting). - using batch for feature extracting for each frame, which lead to a small speed up. - code improvement. Futher improvement direction - Train detector on specific dataset rather than the official one. - Retrain REID model on pedestrain dataset for better performance. - Replace YOLOv3 detector with advanced ones. **Any contributions to this repository is welcome!** ## Introduction This is an implement of MOT tracking algorithm deep sort. Deep sort is basicly the same with sort but added a CNN model to extract features in image of human part bounded by a detector. This CNN model is indeed a RE-ID model and the detector used in [PAPER](https://arxiv.org/abs/1703.07402) is FasterRCNN , and the original source code is [HERE](https://github.com/nwojke/deep_sort). However in original code, the CNN model is implemented with tensorflow, which I'm not familier with. SO I re-implemented the CNN feature extraction model with PyTorch, and changed the CNN model a little bit. Also, I use **YOLOv3** to generate bboxes instead of FasterRCNN. ## Dependencies - python 3 (python2 not sure) - numpy - scipy - opencv-python - sklearn - torch >= 0.4 - torchvision >= 0.1 - pillow - vizer - edict ## Quick Start 0. Check all dependencies installed ```bash pip install -r requirements.txt ``` for user in china, you can specify pypi source to accelerate install like: ```bash pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 1. Clone this repository ``` git clone git@github.com:ZQPei/deep_sort_pytorch.git ``` 2. Download YOLOv3 parameters ``` cd detector/YOLOv3/weight/ wget https://pjreddie.com/media/files/yolov3.weights wget https://pjreddie.com/media/files/yolov3-tiny.weights cd ../../../ ``` 3. Download deepsort parameters ckpt.t7 ``` cd deep_sort/deep/checkpoint # download ckpt.t7 from https://drive.google.com/drive/folders/1xhG0kRH1EX5B9_Iz8gQJb7UNnn_riXi6 to this folder cd ../../../ ``` 4. Compile nms module ```bash cd detector/YOLOv3/nms sh build.sh cd ../../.. ``` Notice: If compiling failed, the simplist way is to **Upgrade your pytorch >= 1.1 and torchvision >= 0.3" and you can avoid the troublesome compiling problems which are most likely caused by either `gcc version too low` or `libraries missing`. 5. Run demo ``` usage: python yolov3_deepsort.py VIDEO_PATH [--help] [--frame_interval FRAME_INTERVAL] [--config_detection CONFIG_DETECTION] [--config_deepsort CONFIG_DEEPSORT] [--display] [--display_width DISPLAY_WIDTH] [--display_height DISPLAY_HEIGHT] [--save_path SAVE_PATH] [--cpu] # yolov3 + deepsort python yolov3_deepsort.py [VIDEO_PATH] # yolov3_tiny + deepsort python yolov3_deepsort.py [VIDEO_PATH] --config_detection ./configs/yolov3_tiny.yaml # yolov3 + deepsort on webcam python3 yolov3_deepsort.py /dev/video0 --camera 0 # yolov3_tiny + deepsort on webcam python3 yolov3_deepsort.py /dev/video0 --config_detection ./configs/yolov3_tiny.yaml --camera 0 ``` Use `--display` to enable display. Results will be saved to `./output/results.avi` and `./output/results.txt`. All files above can also be accessed from BaiduDisk! linker:[BaiduDisk](https://pan.baidu.com/s/1YJ1iPpdFTlUyLFoonYvozg) passwd:fbuw ## Training the RE-ID model The original model used in paper is in original_model.py, and its parameter here [original_ckpt.t7](https://drive.google.com/drive/folders/1xhG0kRH1EX5B9_Iz8gQJb7UNnn_riXi6). To train the model, first you need download [Market1501](http://www.liangzheng.com.cn/Project/project_reid.html) dataset or [Mars](http://www.liangzheng.com.cn/Project/project_mars.html) dataset. Then you can try [train.py](deep_sort/deep/train.py) to train your own parameter and evaluate it using [test.py](deep_sort/deep/test.py) and [evaluate.py](deep_sort/deep/evalute.py). ![train.jpg](deep_sort/deep/train.jpg) ## Demo videos and images [demo.avi](https://drive.google.com/drive/folders/1xhG0kRH1EX5B9_Iz8gQJb7UNnn_riXi6) [demo2.avi](https://drive.google.com/drive/folders/1xhG0kRH1EX5B9_Iz8gQJb7UNnn_riXi6) ![1.jpg](demo/1.jpg) ![2.jpg](demo/2.jpg) ## References - paper: [Simple Online and Realtime Tracking with a Deep Association Metric](https://arxiv.org/abs/1703.07402) - code: [nwojke/deep_sort](https://github.com/nwojke/deep_sort) - paper: [YOLOv3](https://pjreddie.com/media/files/papers/YOLOv3.pdf) - code: [Joseph Redmon/yolov3](https://pjreddie.com/darknet/yolo/) ================================================ FILE: asone/trackers/deep_sort/tracker/__init__.py ================================================ from .deep_sort import DeepSORT from .parser import get_config __all__ = ['DeepSORT', 'build_tracker'] def build_tracker(weights, cfg, use_cuda=True): # cfg = get_config() # cfg.merge_from_file(cfg_deep) return DeepSORT(weights, max_dist=cfg['MAX_DIST'], min_confidence=cfg['MIN_CONFIDENCE'], nms_max_overlap=cfg['NMS_MAX_OVERLAP'], max_iou_distance=cfg['MAX_IOU_DISTANCE'], max_age=cfg['MAX_AGE'], n_init=cfg['N_INIT'], nn_budget=cfg['NN_BUDGET'], use_cuda=use_cuda) ================================================ FILE: asone/trackers/deep_sort/tracker/deep/__init__.py ================================================ ================================================ FILE: asone/trackers/deep_sort/tracker/deep/evaluate.py ================================================ import torch features = torch.load("features.pth") qf = features["qf"] ql = features["ql"] gf = features["gf"] gl = features["gl"] scores = qf.mm(gf.t()) res = scores.topk(5, dim=1)[1][:, 0] top1correct = gl[res].eq(ql).sum().item() print("Acc top1:{:.3f}".format(top1correct / ql.size(0))) ================================================ FILE: asone/trackers/deep_sort/tracker/deep/feature_extractor.py ================================================ import torch import torchvision.transforms as transforms import numpy as np import cv2 import logging from .model import Net class Extractor(object): def __init__(self, model_path, use_cuda=True): self.net = Net(reid=True) self.device = "cuda" if torch.cuda.is_available() and use_cuda else "cpu" state_dict = torch.load(model_path, map_location=torch.device(self.device))[ 'net_dict'] self.net.load_state_dict(state_dict) logger = logging.getLogger("root.tracker") logger.info("Loading weights from {}... Done!".format(model_path)) self.net.to(self.device) self.size = (64, 128) self.norm = transforms.Compose([ transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ]) def _preprocess(self, im_crops): """ TODO: 1. to float with scale from 0 to 1 2. resize to (64, 128) as Market1501 dataset did 3. concatenate to a numpy array 3. to torch Tensor 4. normalize """ def _resize(im, size): return cv2.resize(im.astype(np.float32)/255., size) im_batch = torch.cat([self.norm(_resize(im, self.size)).unsqueeze( 0) for im in im_crops], dim=0).float() return im_batch def __call__(self, im_crops): im_batch = self._preprocess(im_crops) with torch.no_grad(): im_batch = im_batch.to(self.device) features = self.net(im_batch) return features.cpu().numpy() if __name__ == '__main__': img = cv2.imread("demo.jpg")[:, :, (2, 1, 0)] extr = Extractor("checkpoint/ckpt.t7") feature = extr(img) print(feature.shape) ================================================ FILE: asone/trackers/deep_sort/tracker/deep/model.py ================================================ import torch import torch.nn as nn import torch.nn.functional as F class BasicBlock(nn.Module): def __init__(self, c_in, c_out, is_downsample=False): super(BasicBlock, self).__init__() self.is_downsample = is_downsample if is_downsample: self.conv1 = nn.Conv2d( c_in, c_out, 3, stride=2, padding=1, bias=False) else: self.conv1 = nn.Conv2d( c_in, c_out, 3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(c_out) self.relu = nn.ReLU(True) self.conv2 = nn.Conv2d(c_out, c_out, 3, stride=1, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(c_out) if is_downsample: self.downsample = nn.Sequential( nn.Conv2d(c_in, c_out, 1, stride=2, bias=False), nn.BatchNorm2d(c_out) ) elif c_in != c_out: self.downsample = nn.Sequential( nn.Conv2d(c_in, c_out, 1, stride=1, bias=False), nn.BatchNorm2d(c_out) ) self.is_downsample = True def forward(self, x): y = self.conv1(x) y = self.bn1(y) y = self.relu(y) y = self.conv2(y) y = self.bn2(y) if self.is_downsample: x = self.downsample(x) return F.relu(x.add(y), True) def make_layers(c_in, c_out, repeat_times, is_downsample=False): blocks = [] for i in range(repeat_times): if i == 0: blocks += [BasicBlock(c_in, c_out, is_downsample=is_downsample), ] else: blocks += [BasicBlock(c_out, c_out), ] return nn.Sequential(*blocks) class Net(nn.Module): def __init__(self, num_classes=751, reid=False): super(Net, self).__init__() # 3 128 64 self.conv = nn.Sequential( nn.Conv2d(3, 64, 3, stride=1, padding=1), nn.BatchNorm2d(64), nn.ReLU(inplace=True), # nn.Conv2d(32,32,3,stride=1,padding=1), # nn.BatchNorm2d(32), # nn.ReLU(inplace=True), nn.MaxPool2d(3, 2, padding=1), ) # 32 64 32 self.layer1 = make_layers(64, 64, 2, False) # 32 64 32 self.layer2 = make_layers(64, 128, 2, True) # 64 32 16 self.layer3 = make_layers(128, 256, 2, True) # 128 16 8 self.layer4 = make_layers(256, 512, 2, True) # 256 8 4 self.avgpool = nn.AvgPool2d((8, 4), 1) # 256 1 1 self.reid = reid self.classifier = nn.Sequential( nn.Linear(512, 256), nn.BatchNorm1d(256), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(256, num_classes), ) def forward(self, x): x = self.conv(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.avgpool(x) x = x.view(x.size(0), -1) # B x 128 if self.reid: x = x.div(x.norm(p=2, dim=1, keepdim=True)) return x # classifier x = self.classifier(x) return x if __name__ == '__main__': net = Net() x = torch.randn(4, 3, 128, 64) y = net(x) import ipdb ipdb.set_trace() ================================================ FILE: asone/trackers/deep_sort/tracker/deep/original_model.py ================================================ import torch import torch.nn as nn import torch.nn.functional as F class BasicBlock(nn.Module): def __init__(self, c_in, c_out, is_downsample=False): super(BasicBlock, self).__init__() self.is_downsample = is_downsample if is_downsample: self.conv1 = nn.Conv2d( c_in, c_out, 3, stride=2, padding=1, bias=False) else: self.conv1 = nn.Conv2d( c_in, c_out, 3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(c_out) self.relu = nn.ReLU(True) self.conv2 = nn.Conv2d(c_out, c_out, 3, stride=1, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(c_out) if is_downsample: self.downsample = nn.Sequential( nn.Conv2d(c_in, c_out, 1, stride=2, bias=False), nn.BatchNorm2d(c_out) ) elif c_in != c_out: self.downsample = nn.Sequential( nn.Conv2d(c_in, c_out, 1, stride=1, bias=False), nn.BatchNorm2d(c_out) ) self.is_downsample = True def forward(self, x): y = self.conv1(x) y = self.bn1(y) y = self.relu(y) y = self.conv2(y) y = self.bn2(y) if self.is_downsample: x = self.downsample(x) return F.relu(x.add(y), True) def make_layers(c_in, c_out, repeat_times, is_downsample=False): blocks = [] for i in range(repeat_times): if i == 0: blocks += [BasicBlock(c_in, c_out, is_downsample=is_downsample), ] else: blocks += [BasicBlock(c_out, c_out), ] return nn.Sequential(*blocks) class Net(nn.Module): def __init__(self, num_classes=625, reid=False): super(Net, self).__init__() # 3 128 64 self.conv = nn.Sequential( nn.Conv2d(3, 32, 3, stride=1, padding=1), nn.BatchNorm2d(32), nn.ELU(inplace=True), nn.Conv2d(32, 32, 3, stride=1, padding=1), nn.BatchNorm2d(32), nn.ELU(inplace=True), nn.MaxPool2d(3, 2, padding=1), ) # 32 64 32 self.layer1 = make_layers(32, 32, 2, False) # 32 64 32 self.layer2 = make_layers(32, 64, 2, True) # 64 32 16 self.layer3 = make_layers(64, 128, 2, True) # 128 16 8 self.dense = nn.Sequential( nn.Dropout(p=0.6), nn.Linear(128*16*8, 128), nn.BatchNorm1d(128), nn.ELU(inplace=True) ) # 256 1 1 self.reid = reid self.batch_norm = nn.BatchNorm1d(128) self.classifier = nn.Sequential( nn.Linear(128, num_classes), ) def forward(self, x): x = self.conv(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = x.view(x.size(0), -1) if self.reid: x = self.dense[0](x) x = self.dense[1](x) x = x.div(x.norm(p=2, dim=1, keepdim=True)) return x x = self.dense(x) # B x 128 # classifier x = self.classifier(x) return x if __name__ == '__main__': net = Net(reid=True) x = torch.randn(4, 3, 128, 64) y = net(x) import ipdb ipdb.set_trace() ================================================ FILE: asone/trackers/deep_sort/tracker/deep/test.py ================================================ import torch import torch.backends.cudnn as cudnn import torchvision import argparse import os from model import Net parser = argparse.ArgumentParser(description="Train on market1501") parser.add_argument("--data-dir", default='data', type=str) parser.add_argument("--no-cuda", action="store_true") parser.add_argument("--gpu-id", default=0, type=int) args = parser.parse_args() # device device = "cuda:{}".format( args.gpu_id) if torch.cuda.is_available() and not args.no_cuda else "cpu" if torch.cuda.is_available() and not args.no_cuda: cudnn.benchmark = True # data loader root = args.data_dir query_dir = os.path.join(root, "query") gallery_dir = os.path.join(root, "gallery") transform = torchvision.transforms.Compose([ torchvision.transforms.Resize((128, 64)), torchvision.transforms.ToTensor(), torchvision.transforms.Normalize( [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) queryloader = torch.utils.data.DataLoader( torchvision.datasets.ImageFolder(query_dir, transform=transform), batch_size=64, shuffle=False ) galleryloader = torch.utils.data.DataLoader( torchvision.datasets.ImageFolder(gallery_dir, transform=transform), batch_size=64, shuffle=False ) # net definition net = Net(reid=True) assert os.path.isfile( "./checkpoint/ckpt.t7"), "Error: no checkpoint file found!" print('Loading from checkpoint/ckpt.t7') checkpoint = torch.load("./checkpoint/ckpt.t7") net_dict = checkpoint['net_dict'] net.load_state_dict(net_dict, strict=False) net.eval() net.to(device) # compute features query_features = torch.tensor([]).float() query_labels = torch.tensor([]).long() gallery_features = torch.tensor([]).float() gallery_labels = torch.tensor([]).long() with torch.no_grad(): for idx, (inputs, labels) in enumerate(queryloader): inputs = inputs.to(device) features = net(inputs).cpu() query_features = torch.cat((query_features, features), dim=0) query_labels = torch.cat((query_labels, labels)) for idx, (inputs, labels) in enumerate(galleryloader): inputs = inputs.to(device) features = net(inputs).cpu() gallery_features = torch.cat((gallery_features, features), dim=0) gallery_labels = torch.cat((gallery_labels, labels)) gallery_labels -= 2 # save features features = { "qf": query_features, "ql": query_labels, "gf": gallery_features, "gl": gallery_labels } torch.save(features, "features.pth") ================================================ FILE: asone/trackers/deep_sort/tracker/deep/train.py ================================================ import argparse import os import time import numpy as np import matplotlib.pyplot as plt import torch import torch.backends.cudnn as cudnn import torchvision from model import Net parser = argparse.ArgumentParser(description="Train on market1501") parser.add_argument("--data-dir", default='data', type=str) parser.add_argument("--no-cuda", action="store_true") parser.add_argument("--gpu-id", default=0, type=int) parser.add_argument("--lr", default=0.1, type=float) parser.add_argument("--interval", '-i', default=20, type=int) parser.add_argument('--resume', '-r', action='store_true') args = parser.parse_args() # device device = "cuda:{}".format( args.gpu_id) if torch.cuda.is_available() and not args.no_cuda else "cpu" if torch.cuda.is_available() and not args.no_cuda: cudnn.benchmark = True # data loading root = args.data_dir train_dir = os.path.join(root, "train") test_dir = os.path.join(root, "test") transform_train = torchvision.transforms.Compose([ torchvision.transforms.RandomCrop((128, 64), padding=4), torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.ToTensor(), torchvision.transforms.Normalize( [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) transform_test = torchvision.transforms.Compose([ torchvision.transforms.Resize((128, 64)), torchvision.transforms.ToTensor(), torchvision.transforms.Normalize( [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) trainloader = torch.utils.data.DataLoader( torchvision.datasets.ImageFolder(train_dir, transform=transform_train), batch_size=64, shuffle=True ) testloader = torch.utils.data.DataLoader( torchvision.datasets.ImageFolder(test_dir, transform=transform_test), batch_size=64, shuffle=True ) num_classes = max(len(trainloader.dataset.classes), len(testloader.dataset.classes)) # net definition start_epoch = 0 net = Net(num_classes=num_classes) if args.resume: assert os.path.isfile( "./checkpoint/ckpt.t7"), "Error: no checkpoint file found!" print('Loading from checkpoint/ckpt.t7') checkpoint = torch.load("./checkpoint/ckpt.t7") # import ipdb; ipdb.set_trace() net_dict = checkpoint['net_dict'] net.load_state_dict(net_dict) best_acc = checkpoint['acc'] start_epoch = checkpoint['epoch'] net.to(device) # loss and optimizer criterion = torch.nn.CrossEntropyLoss() optimizer = torch.optim.SGD( net.parameters(), args.lr, momentum=0.9, weight_decay=5e-4) best_acc = 0. # train function for each epoch def train(epoch): print("\nEpoch : %d" % (epoch+1)) net.train() training_loss = 0. train_loss = 0. correct = 0 total = 0 interval = args.interval start = time.time() for idx, (inputs, labels) in enumerate(trainloader): # forward inputs, labels = inputs.to(device), labels.to(device) outputs = net(inputs) loss = criterion(outputs, labels) # backward optimizer.zero_grad() loss.backward() optimizer.step() # accumurating training_loss += loss.item() train_loss += loss.item() correct += outputs.max(dim=1)[1].eq(labels).sum().item() total += labels.size(0) # print if (idx+1) % interval == 0: end = time.time() print("[progress:{:.1f}%]time:{:.2f}s Loss:{:.5f} Correct:{}/{} Acc:{:.3f}%".format( 100.*(idx+1)/len(trainloader), end-start, training_loss / interval, correct, total, 100.*correct/total )) training_loss = 0. start = time.time() return train_loss/len(trainloader), 1. - correct/total def test(epoch): global best_acc net.eval() test_loss = 0. correct = 0 total = 0 start = time.time() with torch.no_grad(): for idx, (inputs, labels) in enumerate(testloader): inputs, labels = inputs.to(device), labels.to(device) outputs = net(inputs) loss = criterion(outputs, labels) test_loss += loss.item() correct += outputs.max(dim=1)[1].eq(labels).sum().item() total += labels.size(0) print("Testing ...") end = time.time() print("[progress:{:.1f}%]time:{:.2f}s Loss:{:.5f} Correct:{}/{} Acc:{:.3f}%".format( 100.*(idx+1)/len(testloader), end-start, test_loss / len(testloader), correct, total, 100.*correct/total )) # saving checkpoint acc = 100.*correct/total if acc > best_acc: best_acc = acc print("Saving parameters to checkpoint/ckpt.t7") checkpoint = { 'net_dict': net.state_dict(), 'acc': acc, 'epoch': epoch, } if not os.path.isdir('checkpoint'): os.mkdir('checkpoint') torch.save(checkpoint, './checkpoint/ckpt.t7') return test_loss/len(testloader), 1. - correct/total # plot figure x_epoch = [] record = {'train_loss': [], 'train_err': [], 'test_loss': [], 'test_err': []} fig = plt.figure() ax0 = fig.add_subplot(121, title="loss") ax1 = fig.add_subplot(122, title="top1err") def draw_curve(epoch, train_loss, train_err, test_loss, test_err): global record record['train_loss'].append(train_loss) record['train_err'].append(train_err) record['test_loss'].append(test_loss) record['test_err'].append(test_err) x_epoch.append(epoch) ax0.plot(x_epoch, record['train_loss'], 'bo-', label='train') ax0.plot(x_epoch, record['test_loss'], 'ro-', label='val') ax1.plot(x_epoch, record['train_err'], 'bo-', label='train') ax1.plot(x_epoch, record['test_err'], 'ro-', label='val') if epoch == 0: ax0.legend() ax1.legend() fig.savefig("train.jpg") # lr decay def lr_decay(): global optimizer for params in optimizer.param_groups: params['lr'] *= 0.1 lr = params['lr'] print("Learning rate adjusted to {}".format(lr)) def main(): for epoch in range(start_epoch, start_epoch+40): train_loss, train_err = train(epoch) test_loss, test_err = test(epoch) draw_curve(epoch, train_loss, train_err, test_loss, test_err) if (epoch+1) % 20 == 0: lr_decay() if __name__ == '__main__': main() ================================================ FILE: asone/trackers/deep_sort/tracker/deep_sort.py ================================================ import numpy as np import torch from .deep.feature_extractor import Extractor from .sort.nn_matching import NearestNeighborDistanceMetric from .sort.detection import Detection from .sort.tracker import Tracker __all__ = ['DeepSORT'] class DeepSORT(object): def __init__(self, model_path, max_dist=0.2, min_confidence=0.3, nms_max_overlap=1.0, max_iou_distance=0.7, max_age=70, n_init=3, nn_budget=100, use_cuda=True): self.min_confidence = min_confidence self.nms_max_overlap = nms_max_overlap self.extractor = Extractor(model_path, use_cuda=use_cuda) max_cosine_distance = max_dist metric = NearestNeighborDistanceMetric( "cosine", max_cosine_distance, nn_budget) self.tracker = Tracker( metric, max_iou_distance=max_iou_distance, max_age=max_age, n_init=n_init) def update(self, bbox_xywh, confidences, oids, ori_img): self.height, self.width = ori_img.shape[:2] # generate detections features = self._get_features(bbox_xywh, ori_img) bbox_tlwh = self._xywh_to_tlwh(bbox_xywh) detections = [Detection(bbox_tlwh[i], conf, features[i], oid) for i, (conf,oid) in enumerate(zip(confidences,oids)) if conf > self.min_confidence] # print(detections) # run on non-maximum supression boxes = np.array([d.tlwh for d in detections]) scores = np.array([d.confidence for d in detections]) # update tracker self.tracker.predict() self.tracker.update(detections) # print("len(scores):", len(scores)) # print("self.tracker.tracks",len(self.tracker.tracks)) # output bbox identities outputs = [] for track in self.tracker.tracks: if not track.is_confirmed() or track.time_since_update > 1: continue box = track.to_tlwh() x1, y1, x2, y2 = self._tlwh_to_xyxy(box) track_id = track.track_id track_oid = track.oid outputs.append(np.array([x1, y1, x2, y2, track_id, track_oid], dtype=np.int)) if len(outputs) > 0: outputs = np.stack(outputs, axis=0) return outputs """ TODO: Convert bbox from xc_yc_w_h to xtl_ytl_w_h Thanks JieChen91@github.com for reporting this bug! """ @staticmethod def _xywh_to_tlwh(bbox_xywh): if isinstance(bbox_xywh, np.ndarray): bbox_tlwh = bbox_xywh.copy() elif isinstance(bbox_xywh, torch.Tensor): bbox_tlwh = bbox_xywh.clone() bbox_tlwh[:, 0] = bbox_xywh[:, 0] - bbox_xywh[:, 2] / 2. bbox_tlwh[:, 1] = bbox_xywh[:, 1] - bbox_xywh[:, 3] / 2. return bbox_tlwh def _xywh_to_xyxy(self, bbox_xywh): x, y, w, h = bbox_xywh x1 = max(int(x - w / 2), 0) x2 = min(int(x + w / 2), self.width - 1) y1 = max(int(y - h / 2), 0) y2 = min(int(y + h / 2), self.height - 1) return x1, y1, x2, y2 def _tlwh_to_xyxy(self, bbox_tlwh): """ TODO: Convert bbox from xtl_ytl_w_h to xc_yc_w_h Thanks JieChen91@github.com for reporting this bug! """ x, y, w, h = bbox_tlwh x1 = max(int(x), 0) x2 = min(int(x+w), self.width - 1) y1 = max(int(y), 0) y2 = min(int(y+h), self.height - 1) return x1, y1, x2, y2 def increment_ages(self): self.tracker.increment_ages() def _xyxy_to_tlwh(self, bbox_xyxy): x1, y1, x2, y2 = bbox_xyxy t = x1 l = y1 w = int(x2 - x1) h = int(y2 - y1) return t, l, w, h def _get_features(self, bbox_xywh, ori_img): im_crops = [] for box in bbox_xywh: x1, y1, x2, y2 = self._xywh_to_xyxy(box) im = ori_img[y1:y2, x1:x2] im_crops.append(im) if im_crops: features = self.extractor(im_crops) else: features = np.array([]) return features ================================================ FILE: asone/trackers/deep_sort/tracker/parser.py ================================================ import os import yaml from easydict import EasyDict as edict class YamlParser(edict): """ This is yaml parser based on EasyDict. """ def __init__(self, cfg_dict=None, config_file=None): if cfg_dict is None: cfg_dict = {} if config_file is not None: assert(os.path.isfile(config_file)) with open(config_file, 'r') as fo: yaml_ = yaml.load(fo.read(), Loader=yaml.FullLoader) cfg_dict.update(yaml_) super(YamlParser, self).__init__(cfg_dict) def merge_from_file(self, config_file): with open(config_file, 'r') as fo: yaml_ = yaml.load(fo.read(), Loader=yaml.FullLoader) self.update(yaml_) def merge_from_dict(self, config_dict): self.update(config_dict) def get_config(config_file=None): return YamlParser(config_file=config_file) if __name__ == "__main__": cfg = YamlParser(config_file="../configs/yolov3.yaml") cfg.merge_from_file("../configs/deep_sort.yaml") import ipdb ipdb.set_trace() ================================================ FILE: asone/trackers/deep_sort/tracker/sort/__init__.py ================================================ ================================================ FILE: asone/trackers/deep_sort/tracker/sort/detection.py ================================================ # vim: expandtab:ts=4:sw=4 import numpy as np class Detection(object): """ This class represents a bounding box detection in a single image. Parameters ---------- tlwh : array_like Bounding box in format `(x, y, w, h)`. confidence : float Detector confidence score. feature : array_like A feature vector that describes the object contained in this image. Attributes ---------- tlwh : ndarray Bounding box in format `(top left x, top left y, width, height)`. confidence : ndarray Detector confidence score. feature : ndarray | NoneType A feature vector that describes the object contained in this image. """ def __init__(self, tlwh, confidence, feature, oid): self.tlwh = np.asarray(tlwh, dtype=np.float) self.confidence = float(confidence) self.feature = np.asarray(feature, dtype=np.float32) self.oid = oid def to_tlbr(self): """Convert bounding box to format `(min x, min y, max x, max y)`, i.e., `(top left, bottom right)`. """ ret = self.tlwh.copy() ret[2:] += ret[:2] return ret def to_xyah(self): """Convert bounding box to format `(center x, center y, aspect ratio, height)`, where the aspect ratio is `width / height`. """ ret = self.tlwh.copy() ret[:2] += ret[2:] / 2 ret[2] /= ret[3] return ret ================================================ FILE: asone/trackers/deep_sort/tracker/sort/iou_matching.py ================================================ # vim: expandtab:ts=4:sw=4 from __future__ import absolute_import import numpy as np from . import linear_assignment def iou(bbox, candidates): """Computer intersection over union. Parameters ---------- bbox : ndarray A bounding box in format `(top left x, top left y, width, height)`. candidates : ndarray A matrix of candidate bounding boxes (one per row) in the same format as `bbox`. Returns ------- ndarray The intersection over union in [0, 1] between the `bbox` and each candidate. A higher score means a larger fraction of the `bbox` is occluded by the candidate. """ bbox_tl, bbox_br = bbox[:2], bbox[:2] + bbox[2:] candidates_tl = candidates[:, :2] candidates_br = candidates[:, :2] + candidates[:, 2:] tl = np.c_[np.maximum(bbox_tl[0], candidates_tl[:, 0])[:, np.newaxis], np.maximum(bbox_tl[1], candidates_tl[:, 1])[:, np.newaxis]] br = np.c_[np.minimum(bbox_br[0], candidates_br[:, 0])[:, np.newaxis], np.minimum(bbox_br[1], candidates_br[:, 1])[:, np.newaxis]] wh = np.maximum(0., br - tl) area_intersection = wh.prod(axis=1) area_bbox = bbox[2:].prod() area_candidates = candidates[:, 2:].prod(axis=1) return area_intersection / (area_bbox + area_candidates - area_intersection) def iou_cost(tracks, detections, track_indices=None, detection_indices=None): """An intersection over union distance metric. Parameters ---------- tracks : List[deep_sort.track.Track] A list of tracks. detections : List[deep_sort.detection.Detection] A list of detections. track_indices : Optional[List[int]] A list of indices to tracks that should be matched. Defaults to all `tracks`. detection_indices : Optional[List[int]] A list of indices to detections that should be matched. Defaults to all `detections`. Returns ------- ndarray Returns a cost matrix of shape len(track_indices), len(detection_indices) where entry (i, j) is `1 - iou(tracks[track_indices[i]], detections[detection_indices[j]])`. """ if track_indices is None: track_indices = np.arange(len(tracks)) if detection_indices is None: detection_indices = np.arange(len(detections)) cost_matrix = np.zeros((len(track_indices), len(detection_indices))) for row, track_idx in enumerate(track_indices): if tracks[track_idx].time_since_update > 1: cost_matrix[row, :] = linear_assignment.INFTY_COST continue bbox = tracks[track_idx].to_tlwh() candidates = np.asarray( [detections[i].tlwh for i in detection_indices]) cost_matrix[row, :] = 1. - iou(bbox, candidates) return cost_matrix ================================================ FILE: asone/trackers/deep_sort/tracker/sort/kalman_filter.py ================================================ # vim: expandtab:ts=4:sw=4 import numpy as np import scipy.linalg """ Table for the 0.95 quantile of the chi-square distribution with N degrees of freedom (contains values for N=1, ..., 9). Taken from MATLAB/Octave's chi2inv function and used as Mahalanobis gating threshold. """ chi2inv95 = { 1: 3.8415, 2: 5.9915, 3: 7.8147, 4: 9.4877, 5: 11.070, 6: 12.592, 7: 14.067, 8: 15.507, 9: 16.919} class KalmanFilter(object): """ A simple Kalman filter for tracking bounding boxes in image space. The 8-dimensional state space x, y, a, h, vx, vy, va, vh contains the bounding box center position (x, y), aspect ratio a, height h, and their respective velocities. Object motion follows a constant velocity model. The bounding box location (x, y, a, h) is taken as direct observation of the state space (linear observation model). """ def __init__(self): ndim, dt = 4, 1. # Create Kalman filter model matrices. self._motion_mat = np.eye(2 * ndim, 2 * ndim) for i in range(ndim): self._motion_mat[i, ndim + i] = dt self._update_mat = np.eye(ndim, 2 * ndim) # Motion and observation uncertainty are chosen relative to the current # state estimate. These weights control the amount of uncertainty in # the model. This is a bit hacky. self._std_weight_position = 1. / 20 self._std_weight_velocity = 1. / 160 def initiate(self, measurement): """Create track from unassociated measurement. Parameters ---------- measurement : ndarray Bounding box coordinates (x, y, a, h) with center position (x, y), aspect ratio a, and height h. Returns ------- (ndarray, ndarray) Returns the mean vector (8 dimensional) and covariance matrix (8x8 dimensional) of the new track. Unobserved velocities are initialized to 0 mean. """ mean_pos = measurement mean_vel = np.zeros_like(mean_pos) mean = np.r_[mean_pos, mean_vel] std = [ 2 * self._std_weight_position * measurement[3], 2 * self._std_weight_position * measurement[3], 1e-2, 2 * self._std_weight_position * measurement[3], 10 * self._std_weight_velocity * measurement[3], 10 * self._std_weight_velocity * measurement[3], 1e-5, 10 * self._std_weight_velocity * measurement[3]] covariance = np.diag(np.square(std)) return mean, covariance def predict(self, mean, covariance): """Run Kalman filter prediction step. Parameters ---------- mean : ndarray The 8 dimensional mean vector of the object state at the previous time step. covariance : ndarray The 8x8 dimensional covariance matrix of the object state at the previous time step. Returns ------- (ndarray, ndarray) Returns the mean vector and covariance matrix of the predicted state. Unobserved velocities are initialized to 0 mean. """ std_pos = [ self._std_weight_position * mean[3], self._std_weight_position * mean[3], 1e-2, self._std_weight_position * mean[3]] std_vel = [ self._std_weight_velocity * mean[3], self._std_weight_velocity * mean[3], 1e-5, self._std_weight_velocity * mean[3]] motion_cov = np.diag(np.square(np.r_[std_pos, std_vel])) mean = np.dot(self._motion_mat, mean) covariance = np.linalg.multi_dot(( self._motion_mat, covariance, self._motion_mat.T)) + motion_cov return mean, covariance def project(self, mean, covariance): """Project state distribution to measurement space. Parameters ---------- mean : ndarray The state's mean vector (8 dimensional array). covariance : ndarray The state's covariance matrix (8x8 dimensional). Returns ------- (ndarray, ndarray) Returns the projected mean and covariance matrix of the given state estimate. """ std = [ self._std_weight_position * mean[3], self._std_weight_position * mean[3], 1e-1, self._std_weight_position * mean[3]] innovation_cov = np.diag(np.square(std)) mean = np.dot(self._update_mat, mean) covariance = np.linalg.multi_dot(( self._update_mat, covariance, self._update_mat.T)) return mean, covariance + innovation_cov def update(self, mean, covariance, measurement): """Run Kalman filter correction step. Parameters ---------- mean : ndarray The predicted state's mean vector (8 dimensional). covariance : ndarray The state's covariance matrix (8x8 dimensional). measurement : ndarray The 4 dimensional measurement vector (x, y, a, h), where (x, y) is the center position, a the aspect ratio, and h the height of the bounding box. Returns ------- (ndarray, ndarray) Returns the measurement-corrected state distribution. """ projected_mean, projected_cov = self.project(mean, covariance) chol_factor, lower = scipy.linalg.cho_factor( projected_cov, lower=True, check_finite=False) kalman_gain = scipy.linalg.cho_solve( (chol_factor, lower), np.dot(covariance, self._update_mat.T).T, check_finite=False).T innovation = measurement - projected_mean new_mean = mean + np.dot(innovation, kalman_gain.T) new_covariance = covariance - np.linalg.multi_dot(( kalman_gain, projected_cov, kalman_gain.T)) return new_mean, new_covariance def gating_distance(self, mean, covariance, measurements, only_position=False): """Compute gating distance between state distribution and measurements. A suitable distance threshold can be obtained from `chi2inv95`. If `only_position` is False, the chi-square distribution has 4 degrees of freedom, otherwise 2. Parameters ---------- mean : ndarray Mean vector over the state distribution (8 dimensional). covariance : ndarray Covariance of the state distribution (8x8 dimensional). measurements : ndarray An Nx4 dimensional matrix of N measurements, each in format (x, y, a, h) where (x, y) is the bounding box center position, a the aspect ratio, and h the height. only_position : Optional[bool] If True, distance computation is done with respect to the bounding box center position only. Returns ------- ndarray Returns an array of length N, where the i-th element contains the squared Mahalanobis distance between (mean, covariance) and `measurements[i]`. """ mean, covariance = self.project(mean, covariance) if only_position: mean, covariance = mean[:2], covariance[:2, :2] measurements = measurements[:, :2] cholesky_factor = np.linalg.cholesky(covariance) d = measurements - mean z = scipy.linalg.solve_triangular( cholesky_factor, d.T, lower=True, check_finite=False, overwrite_b=True) squared_maha = np.sum(z * z, axis=0) return squared_maha ================================================ FILE: asone/trackers/deep_sort/tracker/sort/linear_assignment.py ================================================ # vim: expandtab:ts=4:sw=4 from __future__ import absolute_import import numpy as np # from sklearn.utils.linear_assignment_ import linear_assignment from scipy.optimize import linear_sum_assignment as linear_assignment from . import kalman_filter INFTY_COST = 1e+5 def min_cost_matching( distance_metric, max_distance, tracks, detections, track_indices=None, detection_indices=None): """Solve linear assignment problem. Parameters ---------- distance_metric : Callable[List[Track], List[Detection], List[int], List[int]) -> ndarray The distance metric is given a list of tracks and detections as well as a list of N track indices and M detection indices. The metric should return the NxM dimensional cost matrix, where element (i, j) is the association cost between the i-th track in the given track indices and the j-th detection in the given detection_indices. max_distance : float Gating threshold. Associations with cost larger than this value are disregarded. tracks : List[track.Track] A list of predicted tracks at the current time step. detections : List[detection.Detection] A list of detections at the current time step. track_indices : List[int] List of track indices that maps rows in `cost_matrix` to tracks in `tracks` (see description above). detection_indices : List[int] List of detection indices that maps columns in `cost_matrix` to detections in `detections` (see description above). Returns ------- (List[(int, int)], List[int], List[int]) Returns a tuple with the following three entries: * A list of matched track and detection indices. * A list of unmatched track indices. * A list of unmatched detection indices. """ if track_indices is None: track_indices = np.arange(len(tracks)) if detection_indices is None: detection_indices = np.arange(len(detections)) if len(detection_indices) == 0 or len(track_indices) == 0: return [], track_indices, detection_indices # Nothing to match. cost_matrix = distance_metric( tracks, detections, track_indices, detection_indices) cost_matrix[cost_matrix > max_distance] = max_distance + 1e-5 row_indices, col_indices = linear_assignment(cost_matrix) matches, unmatched_tracks, unmatched_detections = [], [], [] for col, detection_idx in enumerate(detection_indices): if col not in col_indices: unmatched_detections.append(detection_idx) for row, track_idx in enumerate(track_indices): if row not in row_indices: unmatched_tracks.append(track_idx) for row, col in zip(row_indices, col_indices): track_idx = track_indices[row] detection_idx = detection_indices[col] if cost_matrix[row, col] > max_distance: unmatched_tracks.append(track_idx) unmatched_detections.append(detection_idx) else: matches.append((track_idx, detection_idx)) return matches, unmatched_tracks, unmatched_detections def matching_cascade( distance_metric, max_distance, cascade_depth, tracks, detections, track_indices=None, detection_indices=None): """Run matching cascade. Parameters ---------- distance_metric : Callable[List[Track], List[Detection], List[int], List[int]) -> ndarray The distance metric is given a list of tracks and detections as well as a list of N track indices and M detection indices. The metric should return the NxM dimensional cost matrix, where element (i, j) is the association cost between the i-th track in the given track indices and the j-th detection in the given detection indices. max_distance : float Gating threshold. Associations with cost larger than this value are disregarded. cascade_depth: int The cascade depth, should be se to the maximum track age. tracks : List[track.Track] A list of predicted tracks at the current time step. detections : List[detection.Detection] A list of detections at the current time step. track_indices : Optional[List[int]] List of track indices that maps rows in `cost_matrix` to tracks in `tracks` (see description above). Defaults to all tracks. detection_indices : Optional[List[int]] List of detection indices that maps columns in `cost_matrix` to detections in `detections` (see description above). Defaults to all detections. Returns ------- (List[(int, int)], List[int], List[int]) Returns a tuple with the following three entries: * A list of matched track and detection indices. * A list of unmatched track indices. * A list of unmatched detection indices. """ if track_indices is None: track_indices = list(range(len(tracks))) if detection_indices is None: detection_indices = list(range(len(detections))) unmatched_detections = detection_indices matches = [] for level in range(cascade_depth): if len(unmatched_detections) == 0: # No detections left break track_indices_l = [ k for k in track_indices if tracks[k].time_since_update == 1 + level ] if len(track_indices_l) == 0: # Nothing to match at this level continue matches_l, _, unmatched_detections = \ min_cost_matching( distance_metric, max_distance, tracks, detections, track_indices_l, unmatched_detections) matches += matches_l unmatched_tracks = list(set(track_indices) - set(k for k, _ in matches)) return matches, unmatched_tracks, unmatched_detections def gate_cost_matrix( kf, cost_matrix, tracks, detections, track_indices, detection_indices, gated_cost=INFTY_COST, only_position=False): """Invalidate infeasible entries in cost matrix based on the state distributions obtained by Kalman filtering. Parameters ---------- kf : The Kalman filter. cost_matrix : ndarray The NxM dimensional cost matrix, where N is the number of track indices and M is the number of detection indices, such that entry (i, j) is the association cost between `tracks[track_indices[i]]` and `detections[detection_indices[j]]`. tracks : List[track.Track] A list of predicted tracks at the current time step. detections : List[detection.Detection] A list of detections at the current time step. track_indices : List[int] List of track indices that maps rows in `cost_matrix` to tracks in `tracks` (see description above). detection_indices : List[int] List of detection indices that maps columns in `cost_matrix` to detections in `detections` (see description above). gated_cost : Optional[float] Entries in the cost matrix corresponding to infeasible associations are set this value. Defaults to a very large value. only_position : Optional[bool] If True, only the x, y position of the state distribution is considered during gating. Defaults to False. Returns ------- ndarray Returns the modified cost matrix. """ gating_dim = 2 if only_position else 4 gating_threshold = kalman_filter.chi2inv95[gating_dim] measurements = np.asarray( [detections[i].to_xyah() for i in detection_indices]) for row, track_idx in enumerate(track_indices): track = tracks[track_idx] gating_distance = kf.gating_distance( track.mean, track.covariance, measurements, only_position) cost_matrix[row, gating_distance > gating_threshold] = gated_cost return cost_matrix ================================================ FILE: asone/trackers/deep_sort/tracker/sort/nn_matching.py ================================================ # vim: expandtab:ts=4:sw=4 import numpy as np def _pdist(a, b): """Compute pair-wise squared distance between points in `a` and `b`. Parameters ---------- a : array_like An NxM matrix of N samples of dimensionality M. b : array_like An LxM matrix of L samples of dimensionality M. Returns ------- ndarray Returns a matrix of size len(a), len(b) such that eleement (i, j) contains the squared distance between `a[i]` and `b[j]`. """ a, b = np.asarray(a), np.asarray(b) if len(a) == 0 or len(b) == 0: return np.zeros((len(a), len(b))) a2, b2 = np.square(a).sum(axis=1), np.square(b).sum(axis=1) r2 = -2. * np.dot(a, b.T) + a2[:, None] + b2[None, :] r2 = np.clip(r2, 0., float(np.inf)) return r2 def _cosine_distance(a, b, data_is_normalized=False): """Compute pair-wise cosine distance between points in `a` and `b`. Parameters ---------- a : array_like An NxM matrix of N samples of dimensionality M. b : array_like An LxM matrix of L samples of dimensionality M. data_is_normalized : Optional[bool] If True, assumes rows in a and b are unit length vectors. Otherwise, a and b are explicitly normalized to lenght 1. Returns ------- ndarray Returns a matrix of size len(a), len(b) such that eleement (i, j) contains the squared distance between `a[i]` and `b[j]`. """ if not data_is_normalized: a = np.asarray(a) / np.linalg.norm(a, axis=1, keepdims=True) b = np.asarray(b) / np.linalg.norm(b, axis=1, keepdims=True) return 1. - np.dot(a, b.T) def _nn_euclidean_distance(x, y): """ Helper function for nearest neighbor distance metric (Euclidean). Parameters ---------- x : ndarray A matrix of N row-vectors (sample points). y : ndarray A matrix of M row-vectors (query points). Returns ------- ndarray A vector of length M that contains for each entry in `y` the smallest Euclidean distance to a sample in `x`. """ distances = _pdist(x, y) return np.maximum(0.0, distances.min(axis=0)) def _nn_cosine_distance(x, y): """ Helper function for nearest neighbor distance metric (cosine). Parameters ---------- x : ndarray A matrix of N row-vectors (sample points). y : ndarray A matrix of M row-vectors (query points). Returns ------- ndarray A vector of length M that contains for each entry in `y` the smallest cosine distance to a sample in `x`. """ distances = _cosine_distance(x, y) return distances.min(axis=0) class NearestNeighborDistanceMetric(object): """ A nearest neighbor distance metric that, for each target, returns the closest distance to any sample that has been observed so far. Parameters ---------- metric : str Either "euclidean" or "cosine". matching_threshold: float The matching threshold. Samples with larger distance are considered an invalid match. budget : Optional[int] If not None, fix samples per class to at most this number. Removes the oldest samples when the budget is reached. Attributes ---------- samples : Dict[int -> List[ndarray]] A dictionary that maps from target identities to the list of samples that have been observed so far. """ def __init__(self, metric, matching_threshold, budget=None): if metric == "euclidean": self._metric = _nn_euclidean_distance elif metric == "cosine": self._metric = _nn_cosine_distance else: raise ValueError( "Invalid metric; must be either 'euclidean' or 'cosine'") self.matching_threshold = matching_threshold self.budget = budget self.samples = {} def partial_fit(self, features, targets, active_targets): """Update the distance metric with new data. Parameters ---------- features : ndarray An NxM matrix of N features of dimensionality M. targets : ndarray An integer array of associated target identities. active_targets : List[int] A list of targets that are currently present in the scene. """ for feature, target in zip(features, targets): self.samples.setdefault(target, []).append(feature) if self.budget is not None: self.samples[target] = self.samples[target][-self.budget:] self.samples = {k: self.samples[k] for k in active_targets} def distance(self, features, targets): """Compute distance between features and targets. Parameters ---------- features : ndarray An NxM matrix of N features of dimensionality M. targets : List[int] A list of targets to match the given `features` against. Returns ------- ndarray Returns a cost matrix of shape len(targets), len(features), where element (i, j) contains the closest squared distance between `targets[i]` and `features[j]`. """ cost_matrix = np.zeros((len(targets), len(features))) for i, target in enumerate(targets): cost_matrix[i, :] = self._metric(self.samples[target], features) return cost_matrix ================================================ FILE: asone/trackers/deep_sort/tracker/sort/preprocessing.py ================================================ # vim: expandtab:ts=4:sw=4 import numpy as np import cv2 def non_max_suppression(boxes, max_bbox_overlap, scores=None): """Suppress overlapping detections. Original code from [1]_ has been adapted to include confidence score. .. [1] http://www.pyimagesearch.com/2015/02/16/ faster-non-maximum-suppression-python/ Examples -------- >>> boxes = [d.roi for d in detections] >>> scores = [d.confidence for d in detections] >>> indices = non_max_suppression(boxes, max_bbox_overlap, scores) >>> detections = [detections[i] for i in indices] Parameters ---------- boxes : ndarray Array of ROIs (x, y, width, height). max_bbox_overlap : float ROIs that overlap more than this values are suppressed. scores : Optional[array_like] Detector confidence score. Returns ------- List[int] Returns indices of detections that have survived non-maxima suppression. """ if len(boxes) == 0: return [] boxes = boxes.astype(np.float) pick = [] x1 = boxes[:, 0] y1 = boxes[:, 1] x2 = boxes[:, 2] + boxes[:, 0] y2 = boxes[:, 3] + boxes[:, 1] area = (x2 - x1 + 1) * (y2 - y1 + 1) if scores is not None: idxs = np.argsort(scores) else: idxs = np.argsort(y2) while len(idxs) > 0: last = len(idxs) - 1 i = idxs[last] pick.append(i) xx1 = np.maximum(x1[i], x1[idxs[:last]]) yy1 = np.maximum(y1[i], y1[idxs[:last]]) xx2 = np.minimum(x2[i], x2[idxs[:last]]) yy2 = np.minimum(y2[i], y2[idxs[:last]]) w = np.maximum(0, xx2 - xx1 + 1) h = np.maximum(0, yy2 - yy1 + 1) overlap = (w * h) / area[idxs[:last]] idxs = np.delete( idxs, np.concatenate( ([last], np.where(overlap > max_bbox_overlap)[0]))) return pick ================================================ FILE: asone/trackers/deep_sort/tracker/sort/track.py ================================================ # vim: expandtab:ts=4:sw=4 class TrackState: """ Enumeration type for the single target track state. Newly created tracks are classified as `tentative` until enough evidence has been collected. Then, the track state is changed to `confirmed`. Tracks that are no longer alive are classified as `deleted` to mark them for removal from the set of active tracks. """ Tentative = 1 Confirmed = 2 Deleted = 3 class Track: """ A single target track with state space `(x, y, a, h)` and associated velocities, where `(x, y)` is the center of the bounding box, `a` is the aspect ratio and `h` is the height. Parameters ---------- mean : ndarray Mean vector of the initial state distribution. covariance : ndarray Covariance matrix of the initial state distribution. track_id : int A unique track identifier. n_init : int Number of consecutive detections before the track is confirmed. The track state is set to `Deleted` if a miss occurs within the first `n_init` frames. max_age : int The maximum number of consecutive misses before the track state is set to `Deleted`. feature : Optional[ndarray] Feature vector of the detection this track originates from. If not None, this feature is added to the `features` cache. Attributes ---------- mean : ndarray Mean vector of the initial state distribution. covariance : ndarray Covariance matrix of the initial state distribution. track_id : int A unique track identifier. hits : int Total number of measurement updates. age : int Total number of frames since first occurance. time_since_update : int Total number of frames since last measurement update. state : TrackState The current track state. features : List[ndarray] A cache of features. On each measurement update, the associated feature vector is added to this list. """ def __init__(self, mean, covariance, track_id, n_init, max_age, oid, feature=None): self.mean = mean self.covariance = covariance self.track_id = track_id self.oid = oid self.hits = 1 self.age = 1 self.time_since_update = 0 self.state = TrackState.Tentative self.features = [] if feature is not None: self.features.append(feature) self._n_init = n_init self._max_age = max_age def to_tlwh(self): """Get current position in bounding box format `(top left x, top left y, width, height)`. Returns ------- ndarray The bounding box. """ ret = self.mean[:4].copy() ret[2] *= ret[3] ret[:2] -= ret[2:] / 2 return ret def to_tlbr(self): """Get current position in bounding box format `(min x, miny, max x, max y)`. Returns ------- ndarray The bounding box. """ ret = self.to_tlwh() ret[2:] = ret[:2] + ret[2:] return ret def increment_age(self): self.age += 1 self.time_since_update += 1 def predict(self, kf): """Propagate the state distribution to the current time step using a Kalman filter prediction step. Parameters ---------- kf : kalman_filter.KalmanFilter The Kalman filter. """ self.mean, self.covariance = kf.predict(self.mean, self.covariance) self.increment_age() def update(self, kf, detection): """Perform Kalman filter measurement update step and update the feature cache. Parameters ---------- kf : kalman_filter.KalmanFilter The Kalman filter. detection : Detection The associated detection. """ self.mean, self.covariance = kf.update( self.mean, self.covariance, detection.to_xyah()) self.features.append(detection.feature) self.hits += 1 self.time_since_update = 0 if self.state == TrackState.Tentative and self.hits >= self._n_init: self.state = TrackState.Confirmed def mark_missed(self): """Mark this track as missed (no association at the current time step). """ if self.state == TrackState.Tentative: self.state = TrackState.Deleted elif self.time_since_update > self._max_age: self.state = TrackState.Deleted def is_tentative(self): """Returns True if this track is tentative (unconfirmed). """ return self.state == TrackState.Tentative def is_confirmed(self): """Returns True if this track is confirmed.""" return self.state == TrackState.Confirmed def is_deleted(self): """Returns True if this track is dead and should be deleted.""" return self.state == TrackState.Deleted ================================================ FILE: asone/trackers/deep_sort/tracker/sort/tracker.py ================================================ # vim: expandtab:ts=4:sw=4 from __future__ import absolute_import import numpy as np from . import kalman_filter from . import linear_assignment from . import iou_matching from .track import Track class Tracker: """ This is the multi-target tracker. Parameters ---------- metric : nn_matching.NearestNeighborDistanceMetric A distance metric for measurement-to-track association. max_age : int Maximum number of missed misses before a track is deleted. n_init : int Number of consecutive detections before the track is confirmed. The track state is set to `Deleted` if a miss occurs within the first `n_init` frames. Attributes ---------- metric : nn_matching.NearestNeighborDistanceMetric The distance metric used for measurement to track association. max_age : int Maximum number of missed misses before a track is deleted. n_init : int Number of frames that a track remains in initialization phase. kf : kalman_filter.KalmanFilter A Kalman filter to filter target trajectories in image space. tracks : List[Track] The list of active tracks at the current time step. """ def __init__(self, metric, max_iou_distance=0.7, max_age=70, n_init=3): self.metric = metric self.max_iou_distance = max_iou_distance self.max_age = max_age self.n_init = n_init self.kf = kalman_filter.KalmanFilter() self.tracks = [] self._next_id = 1 def predict(self): """Propagate track state distributions one time step forward. This function should be called once every time step, before `update`. """ for track in self.tracks: track.predict(self.kf) def increment_ages(self): for track in self.tracks: track.increment_age() track.mark_missed() def update(self, detections): """Perform measurement update and track management. Parameters ---------- detections : List[deep_sort.detection.Detection] A list of detections at the current time step. """ # Run matching cascade. matches, unmatched_tracks, unmatched_detections = \ self._match(detections) # Update track set. for track_idx, detection_idx in matches: self.tracks[track_idx].update(self.kf, detections[detection_idx]) for track_idx in unmatched_tracks: self.tracks[track_idx].mark_missed() for detection_idx in unmatched_detections: self._initiate_track(detections[detection_idx]) self.tracks = [t for t in self.tracks if not t.is_deleted()] # print("LEN self.tracks", len(self.tracks)) # for t in self.tracks: # if not t.is_deleted(): # print(t.__dict__) # Update distance metric. active_targets = [t.track_id for t in self.tracks if t.is_confirmed()] # print(active_targets) features, targets, oids = [], [], [] for track in self.tracks: if not track.is_confirmed(): continue features += track.features targets += [track.track_id for _ in track.features] # oids += track.oid track.features = [] self.metric.partial_fit( np.asarray(features), np.asarray(targets), active_targets) def _match(self, detections): def gated_metric(tracks, dets, track_indices, detection_indices): features = np.array([dets[i].feature for i in detection_indices]) targets = np.array([tracks[i].track_id for i in track_indices]) cost_matrix = self.metric.distance(features, targets) cost_matrix = linear_assignment.gate_cost_matrix( self.kf, cost_matrix, tracks, dets, track_indices, detection_indices) return cost_matrix # Split track set into confirmed and unconfirmed tracks. confirmed_tracks = [ i for i, t in enumerate(self.tracks) if t.is_confirmed()] unconfirmed_tracks = [ i for i, t in enumerate(self.tracks) if not t.is_confirmed()] # Associate confirmed tracks using appearance features. matches_a, unmatched_tracks_a, unmatched_detections = linear_assignment.matching_cascade(gated_metric, self.metric.matching_threshold, self.max_age, self.tracks, detections, confirmed_tracks) # Associate remaining tracks together with unconfirmed tracks using IOU. iou_track_candidates = unconfirmed_tracks + [ k for k in unmatched_tracks_a if self.tracks[k].time_since_update == 1] unmatched_tracks_a = [ k for k in unmatched_tracks_a if self.tracks[k].time_since_update != 1] matches_b, unmatched_tracks_b, unmatched_detections = \ linear_assignment.min_cost_matching( iou_matching.iou_cost, self.max_iou_distance, self.tracks, detections, iou_track_candidates, unmatched_detections) matches = matches_a + matches_b unmatched_tracks = list(set(unmatched_tracks_a + unmatched_tracks_b)) return matches, unmatched_tracks, unmatched_detections def _initiate_track(self, detection): mean, covariance = self.kf.initiate(detection.to_xyah()) self.tracks.append(Track( mean, covariance, self._next_id, self.n_init, self.max_age, detection.oid, detection.feature)) self._next_id += 1 ================================================ FILE: asone/trackers/motpy/__init__.py ================================================ ================================================ FILE: asone/trackers/motpy/motpy.py ================================================ from motpy import Detection, MultiObjectTracker import numpy as np class Motpy: def __init__(self, detector, dt=0.1) -> None: self.tracker = MultiObjectTracker(dt=dt) self.detector = detector try: self.input_shape = tuple(detector.model.get_inputs()[0].shape[2:]) except AttributeError as e: self.input_shape = (640, 640) self.obj_count = 0 self.uuids = {} def detect_and_track(self, image: np.ndarray, config: dict) -> tuple: _dets_xyxy, image_info = self.detector.detect( image, **config ) class_ids = [] ids = [] bboxes_xyxy = [] scores = [] if isinstance(_dets_xyxy, np.ndarray) and len(_dets_xyxy) > 0: self.tracker.step(detections=[ Detection( box=box[:4], score= box[4], class_id=box[5] ) for box in _dets_xyxy ]) bboxes_xyxy, ids, scores, class_ids = self._tracker_update() return bboxes_xyxy, ids, scores, class_ids def _tracker_update(self): bboxes_xyxy = [] class_ids = [] scores = [] ids = [] tracked_objects = self.tracker.active_tracks() for obj in tracked_objects: if obj[0] in self.uuids: obj_id = self.uuids[obj[0]] else: self.obj_count += 1 self.uuids[obj[0]] = self.obj_count obj_id = self.uuids[obj[0]] bboxes_xyxy.append(obj[1:2][0].tolist()) class_ids.append(obj[3]) scores.append(obj[2]) ids.append(obj_id) return np.array(bboxes_xyxy), ids, scores, class_ids ================================================ FILE: asone/trackers/nor_fair/__init__.py ================================================ ================================================ FILE: asone/trackers/nor_fair/norfair.py ================================================ from norfair import Detection, Tracker import numpy as np class NorFair: def __init__(self, detector, max_distance_between_points=30) -> None: self.tracker = Tracker( distance_function=self._euclidean_distance, distance_threshold=max_distance_between_points, ) self.detector = detector try: self.input_shape = tuple(detector.model.get_inputs()[0].shape[2:]) except AttributeError as e: self.input_shape = (640, 640) def _euclidean_distance(self, detection, tracked_object): return np.linalg.norm(detection.points - tracked_object.estimate) def detect_and_track(self, image: np.ndarray, config: dict) -> tuple: _dets_xyxy, image_info = self.detector.detect( image, **config ) class_ids = [] ids = [] bboxes_xyxy = [] scores = [] if isinstance(_dets_xyxy, np.ndarray) and len(_dets_xyxy) > 0: dets_xyxy = [ Detection( np.array([(box[2] + box[0])/2, (box[3] + box[1])/2]), data=box) for box in _dets_xyxy # if box[-1] == 2 ] bboxes_xyxy, ids, scores, class_ids = self._tracker_update( dets_xyxy, image_info) return bboxes_xyxy, ids, scores, class_ids def _tracker_update(self, dets_xyxy: list, image_info: dict): bboxes_xyxy = [] class_ids = [] scores = [] ids = [] tracked_objects = self.tracker.update(detections=dets_xyxy) for obj in tracked_objects: det = obj.last_detection.data bboxes_xyxy.append(det[:4]) class_ids.append(int(det[-1])) scores.append(int(det[-2])) ids.append(obj.id) return np.array(bboxes_xyxy), ids, scores, class_ids ================================================ FILE: asone/trackers/oc_sort/__init__.py ================================================ from asone.trackers.oc_sort.tracker.ocsort import OCSort ================================================ FILE: asone/trackers/oc_sort/ocsort.py ================================================ from motpy import Detection, MultiObjectTracker import numpy as np from .tracker.ocsort import OCSort class OcSort: def __init__(self, detector) -> None: self.tracker = OCSort(det_thresh=0.2) self.detector = detector try: self.input_shape = tuple(detector.model.get_inputs()[0].shape[2:]) except AttributeError as e: self.input_shape = (640, 640) def detect_and_track(self, image: np.ndarray, config: dict) -> tuple: _dets_xyxy, image_info = self.detector.detect( image, **config ) image_info = [image_info['height'], image_info['width']] if isinstance(_dets_xyxy, np.ndarray) and len(_dets_xyxy) > 0: dets = self.tracker.update(_dets_xyxy, image_info) bbox_xyxy = dets[:, :4] ids = dets[:, 4] class_ids = dets[:, 5] scores = dets[:, 6] return bbox_xyxy, ids, scores, class_ids return [],[],[],[] ================================================ FILE: asone/trackers/oc_sort/tracker/__init__.py ================================================ ================================================ FILE: asone/trackers/oc_sort/tracker/association.py ================================================ import os import numpy as np def iou_batch(bboxes1, bboxes2): """ From SORT: Computes IOU between two bboxes in the form [x1,y1,x2,y2] """ bboxes2 = np.expand_dims(bboxes2, 0) bboxes1 = np.expand_dims(bboxes1, 1) xx1 = np.maximum(bboxes1[..., 0], bboxes2[..., 0]) yy1 = np.maximum(bboxes1[..., 1], bboxes2[..., 1]) xx2 = np.minimum(bboxes1[..., 2], bboxes2[..., 2]) yy2 = np.minimum(bboxes1[..., 3], bboxes2[..., 3]) w = np.maximum(0., xx2 - xx1) h = np.maximum(0., yy2 - yy1) wh = w * h o = wh / ((bboxes1[..., 2] - bboxes1[..., 0]) * (bboxes1[..., 3] - bboxes1[..., 1]) + (bboxes2[..., 2] - bboxes2[..., 0]) * (bboxes2[..., 3] - bboxes2[..., 1]) - wh) return(o) def giou_batch(bboxes1, bboxes2): """ :param bbox_p: predict of bbox(N,4)(x1,y1,x2,y2) :param bbox_g: groundtruth of bbox(N,4)(x1,y1,x2,y2) :return: """ # for details should go to https://arxiv.org/pdf/1902.09630.pdf # ensure predict's bbox form bboxes2 = np.expand_dims(bboxes2, 0) bboxes1 = np.expand_dims(bboxes1, 1) xx1 = np.maximum(bboxes1[..., 0], bboxes2[..., 0]) yy1 = np.maximum(bboxes1[..., 1], bboxes2[..., 1]) xx2 = np.minimum(bboxes1[..., 2], bboxes2[..., 2]) yy2 = np.minimum(bboxes1[..., 3], bboxes2[..., 3]) w = np.maximum(0., xx2 - xx1) h = np.maximum(0., yy2 - yy1) wh = w * h union = ((bboxes1[..., 2] - bboxes1[..., 0]) * (bboxes1[..., 3] - bboxes1[..., 1]) + (bboxes2[..., 2] - bboxes2[..., 0]) * (bboxes2[..., 3] - bboxes2[..., 1]) - wh) iou = wh / union xxc1 = np.minimum(bboxes1[..., 0], bboxes2[..., 0]) yyc1 = np.minimum(bboxes1[..., 1], bboxes2[..., 1]) xxc2 = np.maximum(bboxes1[..., 2], bboxes2[..., 2]) yyc2 = np.maximum(bboxes1[..., 3], bboxes2[..., 3]) wc = xxc2 - xxc1 hc = yyc2 - yyc1 assert((wc > 0).all() and (hc > 0).all()) area_enclose = wc * hc giou = iou - (area_enclose - union) / area_enclose giou = (giou + 1.)/2.0 # resize from (-1,1) to (0,1) return giou def diou_batch(bboxes1, bboxes2): """ :param bbox_p: predict of bbox(N,4)(x1,y1,x2,y2) :param bbox_g: groundtruth of bbox(N,4)(x1,y1,x2,y2) :return: """ # for details should go to https://arxiv.org/pdf/1902.09630.pdf # ensure predict's bbox form bboxes2 = np.expand_dims(bboxes2, 0) bboxes1 = np.expand_dims(bboxes1, 1) # calculate the intersection box xx1 = np.maximum(bboxes1[..., 0], bboxes2[..., 0]) yy1 = np.maximum(bboxes1[..., 1], bboxes2[..., 1]) xx2 = np.minimum(bboxes1[..., 2], bboxes2[..., 2]) yy2 = np.minimum(bboxes1[..., 3], bboxes2[..., 3]) w = np.maximum(0., xx2 - xx1) h = np.maximum(0., yy2 - yy1) wh = w * h union = ((bboxes1[..., 2] - bboxes1[..., 0]) * (bboxes1[..., 3] - bboxes1[..., 1]) + (bboxes2[..., 2] - bboxes2[..., 0]) * (bboxes2[..., 3] - bboxes2[..., 1]) - wh) iou = wh / union centerx1 = (bboxes1[..., 0] + bboxes1[..., 2]) / 2.0 centery1 = (bboxes1[..., 1] + bboxes1[..., 3]) / 2.0 centerx2 = (bboxes2[..., 0] + bboxes2[..., 2]) / 2.0 centery2 = (bboxes2[..., 1] + bboxes2[..., 3]) / 2.0 inner_diag = (centerx1 - centerx2) ** 2 + (centery1 - centery2) ** 2 xxc1 = np.minimum(bboxes1[..., 0], bboxes2[..., 0]) yyc1 = np.minimum(bboxes1[..., 1], bboxes2[..., 1]) xxc2 = np.maximum(bboxes1[..., 2], bboxes2[..., 2]) yyc2 = np.maximum(bboxes1[..., 3], bboxes2[..., 3]) outer_diag = (xxc2 - xxc1) ** 2 + (yyc2 - yyc1) ** 2 diou = iou - inner_diag / outer_diag return (diou + 1) / 2.0 # resize from (-1,1) to (0,1) def ciou_batch(bboxes1, bboxes2): """ :param bbox_p: predict of bbox(N,4)(x1,y1,x2,y2) :param bbox_g: groundtruth of bbox(N,4)(x1,y1,x2,y2) :return: """ # for details should go to https://arxiv.org/pdf/1902.09630.pdf # ensure predict's bbox form bboxes2 = np.expand_dims(bboxes2, 0) bboxes1 = np.expand_dims(bboxes1, 1) # calculate the intersection box xx1 = np.maximum(bboxes1[..., 0], bboxes2[..., 0]) yy1 = np.maximum(bboxes1[..., 1], bboxes2[..., 1]) xx2 = np.minimum(bboxes1[..., 2], bboxes2[..., 2]) yy2 = np.minimum(bboxes1[..., 3], bboxes2[..., 3]) w = np.maximum(0., xx2 - xx1) h = np.maximum(0., yy2 - yy1) wh = w * h union = ((bboxes1[..., 2] - bboxes1[..., 0]) * (bboxes1[..., 3] - bboxes1[..., 1]) + (bboxes2[..., 2] - bboxes2[..., 0]) * (bboxes2[..., 3] - bboxes2[..., 1]) - wh) iou = wh / union centerx1 = (bboxes1[..., 0] + bboxes1[..., 2]) / 2.0 centery1 = (bboxes1[..., 1] + bboxes1[..., 3]) / 2.0 centerx2 = (bboxes2[..., 0] + bboxes2[..., 2]) / 2.0 centery2 = (bboxes2[..., 1] + bboxes2[..., 3]) / 2.0 inner_diag = (centerx1 - centerx2) ** 2 + (centery1 - centery2) ** 2 xxc1 = np.minimum(bboxes1[..., 0], bboxes2[..., 0]) yyc1 = np.minimum(bboxes1[..., 1], bboxes2[..., 1]) xxc2 = np.maximum(bboxes1[..., 2], bboxes2[..., 2]) yyc2 = np.maximum(bboxes1[..., 3], bboxes2[..., 3]) outer_diag = (xxc2 - xxc1) ** 2 + (yyc2 - yyc1) ** 2 w1 = bboxes1[..., 2] - bboxes1[..., 0] h1 = bboxes1[..., 3] - bboxes1[..., 1] w2 = bboxes2[..., 2] - bboxes2[..., 0] h2 = bboxes2[..., 3] - bboxes2[..., 1] # prevent dividing over zero. add one pixel shift h2 = h2 + 1. h1 = h1 + 1. arctan = np.arctan(w2/h2) - np.arctan(w1/h1) v = (4 / (np.pi ** 2)) * (arctan ** 2) S = 1 - iou alpha = v / (S+v) ciou = iou - inner_diag / outer_diag - alpha * v return (ciou + 1) / 2.0 # resize from (-1,1) to (0,1) def ct_dist(bboxes1, bboxes2): """ Measure the center distance between two sets of bounding boxes, this is a coarse implementation, we don't recommend using it only for association, which can be unstable and sensitive to frame rate and object speed. """ bboxes2 = np.expand_dims(bboxes2, 0) bboxes1 = np.expand_dims(bboxes1, 1) centerx1 = (bboxes1[..., 0] + bboxes1[..., 2]) / 2.0 centery1 = (bboxes1[..., 1] + bboxes1[..., 3]) / 2.0 centerx2 = (bboxes2[..., 0] + bboxes2[..., 2]) / 2.0 centery2 = (bboxes2[..., 1] + bboxes2[..., 3]) / 2.0 ct_dist2 = (centerx1 - centerx2) ** 2 + (centery1 - centery2) ** 2 ct_dist = np.sqrt(ct_dist2) # The linear rescaling is a naive version and needs more study ct_dist = ct_dist / ct_dist.max() return ct_dist.max() - ct_dist # resize to (0,1) def speed_direction_batch(dets, tracks): tracks = tracks[..., np.newaxis] CX1, CY1 = (dets[:,0] + dets[:,2])/2.0, (dets[:,1]+dets[:,3])/2.0 CX2, CY2 = (tracks[:,0] + tracks[:,2]) /2.0, (tracks[:,1]+tracks[:,3])/2.0 dx = CX1 - CX2 dy = CY1 - CY2 norm = np.sqrt(dx**2 + dy**2) + 1e-6 dx = dx / norm dy = dy / norm return dy, dx # size: num_track x num_det def linear_assignment(cost_matrix): try: import lap _, x, y = lap.lapjv(cost_matrix, extend_cost=True) return np.array([[y[i],i] for i in x if i >= 0]) # except ImportError: from scipy.optimize import linear_sum_assignment x, y = linear_sum_assignment(cost_matrix) return np.array(list(zip(x, y))) def associate_detections_to_trackers(detections,trackers,iou_threshold = 0.3): """ Assigns detections to tracked object (both represented as bounding boxes) Returns 3 lists of matches, unmatched_detections and unmatched_trackers """ if(len(trackers)==0): return np.empty((0,2),dtype=int), np.arange(len(detections)), np.empty((0,5),dtype=int) iou_matrix = iou_batch(detections, trackers) if min(iou_matrix.shape) > 0: a = (iou_matrix > iou_threshold).astype(np.int32) if a.sum(1).max() == 1 and a.sum(0).max() == 1: matched_indices = np.stack(np.where(a), axis=1) else: matched_indices = linear_assignment(-iou_matrix) else: matched_indices = np.empty(shape=(0,2)) unmatched_detections = [] for d, det in enumerate(detections): if(d not in matched_indices[:,0]): unmatched_detections.append(d) unmatched_trackers = [] for t, trk in enumerate(trackers): if(t not in matched_indices[:,1]): unmatched_trackers.append(t) #filter out matched with low IOU matches = [] for m in matched_indices: if(iou_matrix[m[0], m[1]] 0: a = (iou_matrix > iou_threshold).astype(np.int32) if a.sum(1).max() == 1 and a.sum(0).max() == 1: matched_indices = np.stack(np.where(a), axis=1) else: matched_indices = linear_assignment(-(iou_matrix+angle_diff_cost)) else: matched_indices = np.empty(shape=(0,2)) unmatched_detections = [] for d, det in enumerate(detections): if(d not in matched_indices[:,0]): unmatched_detections.append(d) unmatched_trackers = [] for t, trk in enumerate(trackers): if(t not in matched_indices[:,1]): unmatched_trackers.append(t) # filter out matched with low IOU matches = [] for m in matched_indices: if(iou_matrix[m[0], m[1]] 0: a = (iou_matrix > iou_threshold).astype(np.int32) if a.sum(1).max() == 1 and a.sum(0).max() == 1: matched_indices = np.stack(np.where(a), axis=1) else: matched_indices = linear_assignment(cost_matrix) else: matched_indices = np.empty(shape=(0,2)) unmatched_detections = [] for d, det in enumerate(detections): if(d not in matched_indices[:,0]): unmatched_detections.append(d) unmatched_trackers = [] for t, trk in enumerate(trackers): if(t not in matched_indices[:,1]): unmatched_trackers.append(t) #filter out matched with low IOU matches = [] for m in matched_indices: if(iou_matrix[m[0], m[1]]update cycle. The predict step, implemented with the method or function predict(), uses the state transition matrix F to predict the state in the next time period (epoch). The state is stored as a gaussian (x, P), where x is the state (column) vector, and P is its covariance. Covariance matrix Q specifies the process covariance. In Bayesian terms, this prediction is called the *prior*, which you can think of colloquially as the estimate prior to incorporating the measurement. The update step, implemented with the method or function `update()`, incorporates the measurement z with covariance R, into the state estimate (x, P). The class stores the system uncertainty in S, the innovation (residual between prediction and measurement in measurement space) in y, and the Kalman gain in k. The procedural form returns these variables to you. In Bayesian terms this computes the *posterior* - the estimate after the information from the measurement is incorporated. Whether you use the OO form or procedural form is up to you. If matrices such as H, R, and F are changing each epoch, you'll probably opt to use the procedural form. If they are unchanging, the OO form is perhaps easier to use since you won't need to keep track of these matrices. This is especially useful if you are implementing banks of filters or comparing various KF designs for performance; a trivial coding bug could lead to using the wrong sets of matrices. This module also offers an implementation of the RTS smoother, and other helper functions, such as log likelihood computations. The Saver class allows you to easily save the state of the KalmanFilter class after every update This module expects NumPy arrays for all values that expect arrays, although in a few cases, particularly method parameters, it will accept types that convert to NumPy arrays, such as lists of lists. These exceptions are documented in the method or function. Examples -------- The following example constructs a constant velocity kinematic filter, filters noisy data, and plots the results. It also demonstrates using the Saver class to save the state of the filter at each epoch. .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from filterpy.kalman import KalmanFilter from filterpy.common import Q_discrete_white_noise, Saver r_std, q_std = 2., 0.003 cv = KalmanFilter(dim_x=2, dim_z=1) cv.x = np.array([[0., 1.]]) # position, velocity cv.F = np.array([[1, dt],[ [0, 1]]) cv.R = np.array([[r_std^^2]]) f.H = np.array([[1., 0.]]) f.P = np.diag([.1^^2, .03^^2) f.Q = Q_discrete_white_noise(2, dt, q_std**2) saver = Saver(cv) for z in range(100): cv.predict() cv.update([z + randn() * r_std]) saver.save() # save the filter's state saver.to_array() plt.plot(saver.x[:, 0]) # plot all of the priors plt.plot(saver.x_prior[:, 0]) # plot mahalanobis distance plt.figure() plt.plot(saver.mahalanobis) This code implements the same filter using the procedural form x = np.array([[0., 1.]]) # position, velocity F = np.array([[1, dt],[ [0, 1]]) R = np.array([[r_std^^2]]) H = np.array([[1., 0.]]) P = np.diag([.1^^2, .03^^2) Q = Q_discrete_white_noise(2, dt, q_std**2) for z in range(100): x, P = predict(x, P, F=F, Q=Q) x, P = update(x, P, z=[z + randn() * r_std], R=R, H=H) xs.append(x[0, 0]) plt.plot(xs) For more examples see the test subdirectory, or refer to the book cited below. In it I both teach Kalman filtering from basic principles, and teach the use of this library in great detail. FilterPy library. http://github.com/rlabbe/filterpy Documentation at: https://filterpy.readthedocs.org Supporting book at: https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python This is licensed under an MIT license. See the readme.MD file for more information. Copyright 2014-2018 Roger R Labbe Jr. """ from __future__ import absolute_import, division from copy import deepcopy from math import log, exp, sqrt import sys import numpy as np from numpy import dot, zeros, eye, isscalar, shape import numpy.linalg as linalg from filterpy.stats import logpdf from filterpy.common import pretty_str, reshape_z class KalmanFilterNew(object): """ Implements a Kalman filter. You are responsible for setting the various state variables to reasonable values; the defaults will not give you a functional filter. For now the best documentation is my free book Kalman and Bayesian Filters in Python [2]_. The test files in this directory also give you a basic idea of use, albeit without much description. In brief, you will first construct this object, specifying the size of the state vector with dim_x and the size of the measurement vector that you will be using with dim_z. These are mostly used to perform size checks when you assign values to the various matrices. For example, if you specified dim_z=2 and then try to assign a 3x3 matrix to R (the measurement noise matrix you will get an assert exception because R should be 2x2. (If for whatever reason you need to alter the size of things midstream just use the underscore version of the matrices to assign directly: your_filter._R = a_3x3_matrix.) After construction the filter will have default matrices created for you, but you must specify the values for each. It’s usually easiest to just overwrite them rather than assign to each element yourself. This will be clearer in the example below. All are of type numpy.array. Examples -------- Here is a filter that tracks position and velocity using a sensor that only reads position. First construct the object with the required dimensionality. Here the state (`dim_x`) has 2 coefficients (position and velocity), and the measurement (`dim_z`) has one. In FilterPy `x` is the state, `z` is the measurement. .. code:: from filterpy.kalman import KalmanFilter f = KalmanFilter (dim_x=2, dim_z=1) Assign the initial value for the state (position and velocity). You can do this with a two dimensional array like so: .. code:: f.x = np.array([[2.], # position [0.]]) # velocity or just use a one dimensional array, which I prefer doing. .. code:: f.x = np.array([2., 0.]) Define the state transition matrix: .. code:: f.F = np.array([[1.,1.], [0.,1.]]) Define the measurement function. Here we need to convert a position-velocity vector into just a position vector, so we use: .. code:: f.H = np.array([[1., 0.]]) Define the state's covariance matrix P. .. code:: f.P = np.array([[1000., 0.], [ 0., 1000.] ]) Now assign the measurement noise. Here the dimension is 1x1, so I can use a scalar .. code:: f.R = 5 I could have done this instead: .. code:: f.R = np.array([[5.]]) Note that this must be a 2 dimensional array. Finally, I will assign the process noise. Here I will take advantage of another FilterPy library function: .. code:: from filterpy.common import Q_discrete_white_noise f.Q = Q_discrete_white_noise(dim=2, dt=0.1, var=0.13) Now just perform the standard predict/update loop: .. code:: while some_condition_is_true: z = get_sensor_reading() f.predict() f.update(z) do_something_with_estimate (f.x) **Procedural Form** This module also contains stand alone functions to perform Kalman filtering. Use these if you are not a fan of objects. **Example** .. code:: while True: z, R = read_sensor() x, P = predict(x, P, F, Q) x, P = update(x, P, z, R, H) See my book Kalman and Bayesian Filters in Python [2]_. You will have to set the following attributes after constructing this object for the filter to perform properly. Please note that there are various checks in place to ensure that you have made everything the 'correct' size. However, it is possible to provide incorrectly sized arrays such that the linear algebra can not perform an operation. It can also fail silently - you can end up with matrices of a size that allows the linear algebra to work, but are the wrong shape for the problem you are trying to solve. Parameters ---------- dim_x : int Number of state variables for the Kalman filter. For example, if you are tracking the position and velocity of an object in two dimensions, dim_x would be 4. This is used to set the default size of P, Q, and u dim_z : int Number of of measurement inputs. For example, if the sensor provides you with position in (x,y), dim_z would be 2. dim_u : int (optional) size of the control input, if it is being used. Default value of 0 indicates it is not used. compute_log_likelihood : bool (default = True) Computes log likelihood by default, but this can be a slow computation, so if you never use it you can turn this computation off. Attributes ---------- x : numpy.array(dim_x, 1) Current state estimate. Any call to update() or predict() updates this variable. P : numpy.array(dim_x, dim_x) Current state covariance matrix. Any call to update() or predict() updates this variable. x_prior : numpy.array(dim_x, 1) Prior (predicted) state estimate. The *_prior and *_post attributes are for convenience; they store the prior and posterior of the current epoch. Read Only. P_prior : numpy.array(dim_x, dim_x) Prior (predicted) state covariance matrix. Read Only. x_post : numpy.array(dim_x, 1) Posterior (updated) state estimate. Read Only. P_post : numpy.array(dim_x, dim_x) Posterior (updated) state covariance matrix. Read Only. z : numpy.array Last measurement used in update(). Read only. R : numpy.array(dim_z, dim_z) Measurement noise covariance matrix. Also known as the observation covariance. Q : numpy.array(dim_x, dim_x) Process noise covariance matrix. Also known as the transition covariance. F : numpy.array() State Transition matrix. Also known as `A` in some formulation. H : numpy.array(dim_z, dim_x) Measurement function. Also known as the observation matrix, or as `C`. y : numpy.array Residual of the update step. Read only. K : numpy.array(dim_x, dim_z) Kalman gain of the update step. Read only. S : numpy.array System uncertainty (P projected to measurement space). Read only. SI : numpy.array Inverse system uncertainty. Read only. log_likelihood : float log-likelihood of the last measurement. Read only. likelihood : float likelihood of last measurement. Read only. Computed from the log-likelihood. The log-likelihood can be very small, meaning a large negative value such as -28000. Taking the exp() of that results in 0.0, which can break typical algorithms which multiply by this value, so by default we always return a number >= sys.float_info.min. mahalanobis : float mahalanobis distance of the innovation. Read only. inv : function, default numpy.linalg.inv If you prefer another inverse function, such as the Moore-Penrose pseudo inverse, set it to that instead: kf.inv = np.linalg.pinv This is only used to invert self.S. If you know it is diagonal, you might choose to set it to filterpy.common.inv_diagonal, which is several times faster than numpy.linalg.inv for diagonal matrices. alpha : float Fading memory setting. 1.0 gives the normal Kalman filter, and values slightly larger than 1.0 (such as 1.02) give a fading memory effect - previous measurements have less influence on the filter's estimates. This formulation of the Fading memory filter (there are many) is due to Dan Simon [1]_. References ---------- .. [1] Dan Simon. "Optimal State Estimation." John Wiley & Sons. p. 208-212. (2006) .. [2] Roger Labbe. "Kalman and Bayesian Filters in Python" https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python """ def __init__(self, dim_x, dim_z, dim_u=0): if dim_x < 1: raise ValueError('dim_x must be 1 or greater') if dim_z < 1: raise ValueError('dim_z must be 1 or greater') if dim_u < 0: raise ValueError('dim_u must be 0 or greater') self.dim_x = dim_x self.dim_z = dim_z self.dim_u = dim_u self.x = zeros((dim_x, 1)) # state self.P = eye(dim_x) # uncertainty covariance self.Q = eye(dim_x) # process uncertainty self.B = None # control transition matrix self.F = eye(dim_x) # state transition matrix self.H = zeros((dim_z, dim_x)) # measurement function self.R = eye(dim_z) # measurement uncertainty self._alpha_sq = 1. # fading memory control self.M = np.zeros((dim_x, dim_z)) # process-measurement cross correlation self.z = np.array([[None]*self.dim_z]).T # gain and residual are computed during the innovation step. We # save them so that in case you want to inspect them for various # purposes self.K = np.zeros((dim_x, dim_z)) # kalman gain self.y = zeros((dim_z, 1)) self.S = np.zeros((dim_z, dim_z)) # system uncertainty self.SI = np.zeros((dim_z, dim_z)) # inverse system uncertainty # identity matrix. Do not alter this. self._I = np.eye(dim_x) # these will always be a copy of x,P after predict() is called self.x_prior = self.x.copy() self.P_prior = self.P.copy() # these will always be a copy of x,P after update() is called self.x_post = self.x.copy() self.P_post = self.P.copy() # Only computed only if requested via property self._log_likelihood = log(sys.float_info.min) self._likelihood = sys.float_info.min self._mahalanobis = None # keep all observations self.history_obs = [] self.inv = np.linalg.inv self.attr_saved = None self.observed = False def predict(self, u=None, B=None, F=None, Q=None): """ Predict next state (prior) using the Kalman filter state propagation equations. Parameters ---------- u : np.array, default 0 Optional control vector. B : np.array(dim_x, dim_u), or None Optional control transition matrix; a value of None will cause the filter to use `self.B`. F : np.array(dim_x, dim_x), or None Optional state transition matrix; a value of None will cause the filter to use `self.F`. Q : np.array(dim_x, dim_x), scalar, or None Optional process noise matrix; a value of None will cause the filter to use `self.Q`. """ if B is None: B = self.B if F is None: F = self.F if Q is None: Q = self.Q elif isscalar(Q): Q = eye(self.dim_x) * Q # x = Fx + Bu if B is not None and u is not None: self.x = dot(F, self.x) + dot(B, u) else: self.x = dot(F, self.x) # P = FPF' + Q self.P = self._alpha_sq * dot(dot(F, self.P), F.T) + Q # save prior self.x_prior = self.x.copy() self.P_prior = self.P.copy() def freeze(self): """ Save the parameters before non-observation forward """ self.attr_saved = deepcopy(self.__dict__) def unfreeze(self): if self.attr_saved is not None: new_history = deepcopy(self.history_obs) self.__dict__ = self.attr_saved # self.history_obs = new_history self.history_obs = self.history_obs[:-1] occur = [int(d is None) for d in new_history] indices = np.where(np.array(occur)==0)[0] index1 = indices[-2] index2 = indices[-1] box1 = new_history[index1] x1, y1, s1, r1 = box1 w1 = np.sqrt(s1 * r1) h1 = np.sqrt(s1 / r1) box2 = new_history[index2] x2, y2, s2, r2 = box2 w2 = np.sqrt(s2 * r2) h2 = np.sqrt(s2 / r2) time_gap = index2 - index1 dx = (x2-x1)/time_gap dy = (y2-y1)/time_gap dw = (w2-w1)/time_gap dh = (h2-h1)/time_gap for i in range(index2 - index1): """ The default virtual trajectory generation is by linear motion (constant speed hypothesis), you could modify this part to implement your own. """ x = x1 + (i+1) * dx y = y1 + (i+1) * dy w = w1 + (i+1) * dw h = h1 + (i+1) * dh s = w * h r = w / float(h) new_box = np.array([x, y, s, r]).reshape((4, 1)) """ I still use predict-update loop here to refresh the parameters, but this can be faster by directly modifying the internal parameters as suggested in the paper. I keep this naive but slow way for easy read and understanding """ self.update(new_box) if not i == (index2-index1-1): self.predict() def update(self, z, R=None, H=None): """ Add a new measurement (z) to the Kalman filter. If z is None, nothing is computed. However, x_post and P_post are updated with the prior (x_prior, P_prior), and self.z is set to None. Parameters ---------- z : (dim_z, 1): array_like measurement for this update. z can be a scalar if dim_z is 1, otherwise it must be convertible to a column vector. If you pass in a value of H, z must be a column vector the of the correct size. R : np.array, scalar, or None Optionally provide R to override the measurement noise for this one call, otherwise self.R will be used. H : np.array, or None Optionally provide H to override the measurement function for this one call, otherwise self.H will be used. """ # set to None to force recompute self._log_likelihood = None self._likelihood = None self._mahalanobis = None # append the observation self.history_obs.append(z) if z is None: if self.observed: """ Got no observation so freeze the current parameters for future potential online smoothing. """ self.freeze() self.observed = False self.z = np.array([[None]*self.dim_z]).T self.x_post = self.x.copy() self.P_post = self.P.copy() self.y = zeros((self.dim_z, 1)) return # self.observed = True if not self.observed: """ Get observation, use online smoothing to re-update parameters """ self.unfreeze() self.observed = True if R is None: R = self.R elif isscalar(R): R = eye(self.dim_z) * R if H is None: z = reshape_z(z, self.dim_z, self.x.ndim) H = self.H # y = z - Hx # error (residual) between measurement and prediction self.y = z - dot(H, self.x) # common subexpression for speed PHT = dot(self.P, H.T) # S = HPH' + R # project system uncertainty into measurement space self.S = dot(H, PHT) + R self.SI = self.inv(self.S) # K = PH'inv(S) # map system uncertainty into kalman gain self.K = dot(PHT, self.SI) # x = x + Ky # predict new x with residual scaled by the kalman gain self.x = self.x + dot(self.K, self.y) # P = (I-KH)P(I-KH)' + KRK' # This is more numerically stable # and works for non-optimal K vs the equation # P = (I-KH)P usually seen in the literature. I_KH = self._I - dot(self.K, H) self.P = dot(dot(I_KH, self.P), I_KH.T) + dot(dot(self.K, R), self.K.T) # save measurement and posterior state self.z = deepcopy(z) self.x_post = self.x.copy() self.P_post = self.P.copy() def predict_steadystate(self, u=0, B=None): """ Predict state (prior) using the Kalman filter state propagation equations. Only x is updated, P is left unchanged. See update_steadstate() for a longer explanation of when to use this method. Parameters ---------- u : np.array Optional control vector. If non-zero, it is multiplied by B to create the control input into the system. B : np.array(dim_x, dim_u), or None Optional control transition matrix; a value of None will cause the filter to use `self.B`. """ if B is None: B = self.B # x = Fx + Bu if B is not None: self.x = dot(self.F, self.x) + dot(B, u) else: self.x = dot(self.F, self.x) # save prior self.x_prior = self.x.copy() self.P_prior = self.P.copy() def update_steadystate(self, z): """ Add a new measurement (z) to the Kalman filter without recomputing the Kalman gain K, the state covariance P, or the system uncertainty S. You can use this for LTI systems since the Kalman gain and covariance converge to a fixed value. Precompute these and assign them explicitly, or run the Kalman filter using the normal predict()/update(0 cycle until they converge. The main advantage of this call is speed. We do significantly less computation, notably avoiding a costly matrix inversion. Use in conjunction with predict_steadystate(), otherwise P will grow without bound. Parameters ---------- z : (dim_z, 1): array_like measurement for this update. z can be a scalar if dim_z is 1, otherwise it must be convertible to a column vector. Examples -------- >>> cv = kinematic_kf(dim=3, order=2) # 3D const velocity filter >>> # let filter converge on representative data, then save k and P >>> for i in range(100): >>> cv.predict() >>> cv.update([i, i, i]) >>> saved_k = np.copy(cv.K) >>> saved_P = np.copy(cv.P) later on: >>> cv = kinematic_kf(dim=3, order=2) # 3D const velocity filter >>> cv.K = np.copy(saved_K) >>> cv.P = np.copy(saved_P) >>> for i in range(100): >>> cv.predict_steadystate() >>> cv.update_steadystate([i, i, i]) """ # set to None to force recompute self._log_likelihood = None self._likelihood = None self._mahalanobis = None if z is None: self.z = np.array([[None]*self.dim_z]).T self.x_post = self.x.copy() self.P_post = self.P.copy() self.y = zeros((self.dim_z, 1)) return z = reshape_z(z, self.dim_z, self.x.ndim) # y = z - Hx # error (residual) between measurement and prediction self.y = z - dot(self.H, self.x) # x = x + Ky # predict new x with residual scaled by the kalman gain self.x = self.x + dot(self.K, self.y) self.z = deepcopy(z) self.x_post = self.x.copy() self.P_post = self.P.copy() # set to None to force recompute self._log_likelihood = None self._likelihood = None self._mahalanobis = None def update_correlated(self, z, R=None, H=None): """ Add a new measurement (z) to the Kalman filter assuming that process noise and measurement noise are correlated as defined in the `self.M` matrix. A partial derivation can be found in [1] If z is None, nothing is changed. Parameters ---------- z : (dim_z, 1): array_like measurement for this update. z can be a scalar if dim_z is 1, otherwise it must be convertible to a column vector. R : np.array, scalar, or None Optionally provide R to override the measurement noise for this one call, otherwise self.R will be used. H : np.array, or None Optionally provide H to override the measurement function for this one call, otherwise self.H will be used. References ---------- .. [1] Bulut, Y. (2011). Applied Kalman filter theory (Doctoral dissertation, Northeastern University). http://people.duke.edu/~hpgavin/SystemID/References/Balut-KalmanFilter-PhD-NEU-2011.pdf """ # set to None to force recompute self._log_likelihood = None self._likelihood = None self._mahalanobis = None if z is None: self.z = np.array([[None]*self.dim_z]).T self.x_post = self.x.copy() self.P_post = self.P.copy() self.y = zeros((self.dim_z, 1)) return if R is None: R = self.R elif isscalar(R): R = eye(self.dim_z) * R # rename for readability and a tiny extra bit of speed if H is None: z = reshape_z(z, self.dim_z, self.x.ndim) H = self.H # handle special case: if z is in form [[z]] but x is not a column # vector dimensions will not match if self.x.ndim == 1 and shape(z) == (1, 1): z = z[0] if shape(z) == (): # is it scalar, e.g. z=3 or z=np.array(3) z = np.asarray([z]) # y = z - Hx # error (residual) between measurement and prediction self.y = z - dot(H, self.x) # common subexpression for speed PHT = dot(self.P, H.T) # project system uncertainty into measurement space self.S = dot(H, PHT) + dot(H, self.M) + dot(self.M.T, H.T) + R self.SI = self.inv(self.S) # K = PH'inv(S) # map system uncertainty into kalman gain self.K = dot(PHT + self.M, self.SI) # x = x + Ky # predict new x with residual scaled by the kalman gain self.x = self.x + dot(self.K, self.y) self.P = self.P - dot(self.K, dot(H, self.P) + self.M.T) self.z = deepcopy(z) self.x_post = self.x.copy() self.P_post = self.P.copy() def batch_filter(self, zs, Fs=None, Qs=None, Hs=None, Rs=None, Bs=None, us=None, update_first=False, saver=None): """ Batch processes a sequences of measurements. Parameters ---------- zs : list-like list of measurements at each time step `self.dt`. Missing measurements must be represented by `None`. Fs : None, list-like, default=None optional value or list of values to use for the state transition matrix F. If Fs is None then self.F is used for all epochs. Otherwise it must contain a list-like list of F's, one for each epoch. This allows you to have varying F per epoch. Qs : None, np.array or list-like, default=None optional value or list of values to use for the process error covariance Q. If Qs is None then self.Q is used for all epochs. Otherwise it must contain a list-like list of Q's, one for each epoch. This allows you to have varying Q per epoch. Hs : None, np.array or list-like, default=None optional list of values to use for the measurement matrix H. If Hs is None then self.H is used for all epochs. If Hs contains a single matrix, then it is used as H for all epochs. Otherwise it must contain a list-like list of H's, one for each epoch. This allows you to have varying H per epoch. Rs : None, np.array or list-like, default=None optional list of values to use for the measurement error covariance R. If Rs is None then self.R is used for all epochs. Otherwise it must contain a list-like list of R's, one for each epoch. This allows you to have varying R per epoch. Bs : None, np.array or list-like, default=None optional list of values to use for the control transition matrix B. If Bs is None then self.B is used for all epochs. Otherwise it must contain a list-like list of B's, one for each epoch. This allows you to have varying B per epoch. us : None, np.array or list-like, default=None optional list of values to use for the control input vector; If us is None then None is used for all epochs (equivalent to 0, or no control input). Otherwise it must contain a list-like list of u's, one for each epoch. update_first : bool, optional, default=False controls whether the order of operations is update followed by predict, or predict followed by update. Default is predict->update. saver : filterpy.common.Saver, optional filterpy.common.Saver object. If provided, saver.save() will be called after every epoch Returns ------- means : np.array((n,dim_x,1)) array of the state for each time step after the update. Each entry is an np.array. In other words `means[k,:]` is the state at step `k`. covariance : np.array((n,dim_x,dim_x)) array of the covariances for each time step after the update. In other words `covariance[k,:,:]` is the covariance at step `k`. means_predictions : np.array((n,dim_x,1)) array of the state for each time step after the predictions. Each entry is an np.array. In other words `means[k,:]` is the state at step `k`. covariance_predictions : np.array((n,dim_x,dim_x)) array of the covariances for each time step after the prediction. In other words `covariance[k,:,:]` is the covariance at step `k`. Examples -------- .. code-block:: Python # this example demonstrates tracking a measurement where the time # between measurement varies, as stored in dts. This requires # that F be recomputed for each epoch. The output is then smoothed # with an RTS smoother. zs = [t + random.randn()*4 for t in range (40)] Fs = [np.array([[1., dt], [0, 1]] for dt in dts] (mu, cov, _, _) = kf.batch_filter(zs, Fs=Fs) (xs, Ps, Ks, Pps) = kf.rts_smoother(mu, cov, Fs=Fs) """ #pylint: disable=too-many-statements n = np.size(zs, 0) if Fs is None: Fs = [self.F] * n if Qs is None: Qs = [self.Q] * n if Hs is None: Hs = [self.H] * n if Rs is None: Rs = [self.R] * n if Bs is None: Bs = [self.B] * n if us is None: us = [0] * n # mean estimates from Kalman Filter if self.x.ndim == 1: means = zeros((n, self.dim_x)) means_p = zeros((n, self.dim_x)) else: means = zeros((n, self.dim_x, 1)) means_p = zeros((n, self.dim_x, 1)) # state covariances from Kalman Filter covariances = zeros((n, self.dim_x, self.dim_x)) covariances_p = zeros((n, self.dim_x, self.dim_x)) if update_first: for i, (z, F, Q, H, R, B, u) in enumerate(zip(zs, Fs, Qs, Hs, Rs, Bs, us)): self.update(z, R=R, H=H) means[i, :] = self.x covariances[i, :, :] = self.P self.predict(u=u, B=B, F=F, Q=Q) means_p[i, :] = self.x covariances_p[i, :, :] = self.P if saver is not None: saver.save() else: for i, (z, F, Q, H, R, B, u) in enumerate(zip(zs, Fs, Qs, Hs, Rs, Bs, us)): self.predict(u=u, B=B, F=F, Q=Q) means_p[i, :] = self.x covariances_p[i, :, :] = self.P self.update(z, R=R, H=H) means[i, :] = self.x covariances[i, :, :] = self.P if saver is not None: saver.save() return (means, covariances, means_p, covariances_p) def rts_smoother(self, Xs, Ps, Fs=None, Qs=None, inv=np.linalg.inv): """ Runs the Rauch-Tung-Striebel Kalman smoother on a set of means and covariances computed by a Kalman filter. The usual input would come from the output of `KalmanFilter.batch_filter()`. Parameters ---------- Xs : numpy.array array of the means (state variable x) of the output of a Kalman filter. Ps : numpy.array array of the covariances of the output of a kalman filter. Fs : list-like collection of numpy.array, optional State transition matrix of the Kalman filter at each time step. Optional, if not provided the filter's self.F will be used Qs : list-like collection of numpy.array, optional Process noise of the Kalman filter at each time step. Optional, if not provided the filter's self.Q will be used inv : function, default numpy.linalg.inv If you prefer another inverse function, such as the Moore-Penrose pseudo inverse, set it to that instead: kf.inv = np.linalg.pinv Returns ------- x : numpy.ndarray smoothed means P : numpy.ndarray smoothed state covariances K : numpy.ndarray smoother gain at each step Pp : numpy.ndarray Predicted state covariances Examples -------- .. code-block:: Python zs = [t + random.randn()*4 for t in range (40)] (mu, cov, _, _) = kalman.batch_filter(zs) (x, P, K, Pp) = rts_smoother(mu, cov, kf.F, kf.Q) """ if len(Xs) != len(Ps): raise ValueError('length of Xs and Ps must be the same') n = Xs.shape[0] dim_x = Xs.shape[1] if Fs is None: Fs = [self.F] * n if Qs is None: Qs = [self.Q] * n # smoother gain K = zeros((n, dim_x, dim_x)) x, P, Pp = Xs.copy(), Ps.copy(), Ps.copy() for k in range(n-2, -1, -1): Pp[k] = dot(dot(Fs[k+1], P[k]), Fs[k+1].T) + Qs[k+1] #pylint: disable=bad-whitespace K[k] = dot(dot(P[k], Fs[k+1].T), inv(Pp[k])) x[k] += dot(K[k], x[k+1] - dot(Fs[k+1], x[k])) P[k] += dot(dot(K[k], P[k+1] - Pp[k]), K[k].T) return (x, P, K, Pp) def get_prediction(self, u=None, B=None, F=None, Q=None): """ Predict next state (prior) using the Kalman filter state propagation equations and returns it without modifying the object. Parameters ---------- u : np.array, default 0 Optional control vector. B : np.array(dim_x, dim_u), or None Optional control transition matrix; a value of None will cause the filter to use `self.B`. F : np.array(dim_x, dim_x), or None Optional state transition matrix; a value of None will cause the filter to use `self.F`. Q : np.array(dim_x, dim_x), scalar, or None Optional process noise matrix; a value of None will cause the filter to use `self.Q`. Returns ------- (x, P) : tuple State vector and covariance array of the prediction. """ if B is None: B = self.B if F is None: F = self.F if Q is None: Q = self.Q elif isscalar(Q): Q = eye(self.dim_x) * Q # x = Fx + Bu if B is not None and u is not None: x = dot(F, self.x) + dot(B, u) else: x = dot(F, self.x) # P = FPF' + Q P = self._alpha_sq * dot(dot(F, self.P), F.T) + Q return x, P def get_update(self, z=None): """ Computes the new estimate based on measurement `z` and returns it without altering the state of the filter. Parameters ---------- z : (dim_z, 1): array_like measurement for this update. z can be a scalar if dim_z is 1, otherwise it must be convertible to a column vector. Returns ------- (x, P) : tuple State vector and covariance array of the update. """ if z is None: return self.x, self.P z = reshape_z(z, self.dim_z, self.x.ndim) R = self.R H = self.H P = self.P x = self.x # error (residual) between measurement and prediction y = z - dot(H, x) # common subexpression for speed PHT = dot(P, H.T) # project system uncertainty into measurement space S = dot(H, PHT) + R # map system uncertainty into kalman gain K = dot(PHT, self.inv(S)) # predict new x with residual scaled by the kalman gain x = x + dot(K, y) # P = (I-KH)P(I-KH)' + KRK' I_KH = self._I - dot(K, H) P = dot(dot(I_KH, P), I_KH.T) + dot(dot(K, R), K.T) return x, P def residual_of(self, z): """ Returns the residual for the given measurement (z). Does not alter the state of the filter. """ z = reshape_z(z, self.dim_z, self.x.ndim) return z - dot(self.H, self.x_prior) def measurement_of_state(self, x): """ Helper function that converts a state into a measurement. Parameters ---------- x : np.array kalman state vector Returns ------- z : (dim_z, 1): array_like measurement for this update. z can be a scalar if dim_z is 1, otherwise it must be convertible to a column vector. """ return dot(self.H, x) @property def log_likelihood(self): """ log-likelihood of the last measurement. """ if self._log_likelihood is None: self._log_likelihood = logpdf(x=self.y, cov=self.S) return self._log_likelihood @property def likelihood(self): """ Computed from the log-likelihood. The log-likelihood can be very small, meaning a large negative value such as -28000. Taking the exp() of that results in 0.0, which can break typical algorithms which multiply by this value, so by default we always return a number >= sys.float_info.min. """ if self._likelihood is None: self._likelihood = exp(self.log_likelihood) if self._likelihood == 0: self._likelihood = sys.float_info.min return self._likelihood @property def mahalanobis(self): """" Mahalanobis distance of measurement. E.g. 3 means measurement was 3 standard deviations away from the predicted value. Returns ------- mahalanobis : float """ if self._mahalanobis is None: self._mahalanobis = sqrt(float(dot(dot(self.y.T, self.SI), self.y))) return self._mahalanobis @property def alpha(self): """ Fading memory setting. 1.0 gives the normal Kalman filter, and values slightly larger than 1.0 (such as 1.02) give a fading memory effect - previous measurements have less influence on the filter's estimates. This formulation of the Fading memory filter (there are many) is due to Dan Simon [1]_. """ return self._alpha_sq**.5 def log_likelihood_of(self, z): """ log likelihood of the measurement `z`. This should only be called after a call to update(). Calling after predict() will yield an incorrect result.""" if z is None: return log(sys.float_info.min) return logpdf(z, dot(self.H, self.x), self.S) @alpha.setter def alpha(self, value): if not np.isscalar(value) or value < 1: raise ValueError('alpha must be a float greater than 1') self._alpha_sq = value**2 def __repr__(self): return '\n'.join([ 'KalmanFilter object', pretty_str('dim_x', self.dim_x), pretty_str('dim_z', self.dim_z), pretty_str('dim_u', self.dim_u), pretty_str('x', self.x), pretty_str('P', self.P), pretty_str('x_prior', self.x_prior), pretty_str('P_prior', self.P_prior), pretty_str('x_post', self.x_post), pretty_str('P_post', self.P_post), pretty_str('F', self.F), pretty_str('Q', self.Q), pretty_str('R', self.R), pretty_str('H', self.H), pretty_str('K', self.K), pretty_str('y', self.y), pretty_str('S', self.S), pretty_str('SI', self.SI), pretty_str('M', self.M), pretty_str('B', self.B), pretty_str('z', self.z), pretty_str('log-likelihood', self.log_likelihood), pretty_str('likelihood', self.likelihood), pretty_str('mahalanobis', self.mahalanobis), pretty_str('alpha', self.alpha), pretty_str('inv', self.inv) ]) def test_matrix_dimensions(self, z=None, H=None, R=None, F=None, Q=None): """ Performs a series of asserts to check that the size of everything is what it should be. This can help you debug problems in your design. If you pass in H, R, F, Q those will be used instead of this object's value for those matrices. Testing `z` (the measurement) is problamatic. x is a vector, and can be implemented as either a 1D array or as a nx1 column vector. Thus Hx can be of different shapes. Then, if Hx is a single value, it can be either a 1D array or 2D vector. If either is true, z can reasonably be a scalar (either '3' or np.array('3') are scalars under this definition), a 1D, 1 element array, or a 2D, 1 element array. You are allowed to pass in any combination that works. """ if H is None: H = self.H if R is None: R = self.R if F is None: F = self.F if Q is None: Q = self.Q x = self.x P = self.P assert x.ndim == 1 or x.ndim == 2, \ "x must have one or two dimensions, but has {}".format(x.ndim) if x.ndim == 1: assert x.shape[0] == self.dim_x, \ "Shape of x must be ({},{}), but is {}".format( self.dim_x, 1, x.shape) else: assert x.shape == (self.dim_x, 1), \ "Shape of x must be ({},{}), but is {}".format( self.dim_x, 1, x.shape) assert P.shape == (self.dim_x, self.dim_x), \ "Shape of P must be ({},{}), but is {}".format( self.dim_x, self.dim_x, P.shape) assert Q.shape == (self.dim_x, self.dim_x), \ "Shape of Q must be ({},{}), but is {}".format( self.dim_x, self.dim_x, P.shape) assert F.shape == (self.dim_x, self.dim_x), \ "Shape of F must be ({},{}), but is {}".format( self.dim_x, self.dim_x, F.shape) assert np.ndim(H) == 2, \ "Shape of H must be (dim_z, {}), but is {}".format( P.shape[0], shape(H)) assert H.shape[1] == P.shape[0], \ "Shape of H must be (dim_z, {}), but is {}".format( P.shape[0], H.shape) # shape of R must be the same as HPH' hph_shape = (H.shape[0], H.shape[0]) r_shape = shape(R) if H.shape[0] == 1: # r can be scalar, 1D, or 2D in this case assert r_shape in [(), (1,), (1, 1)], \ "R must be scalar or one element array, but is shaped {}".format( r_shape) else: assert r_shape == hph_shape, \ "shape of R should be {} but it is {}".format(hph_shape, r_shape) if z is not None: z_shape = shape(z) else: z_shape = (self.dim_z, 1) # H@x must have shape of z Hx = dot(H, x) if z_shape == (): # scalar or np.array(scalar) assert Hx.ndim == 1 or shape(Hx) == (1, 1), \ "shape of z should be {}, not {} for the given H".format( shape(Hx), z_shape) elif shape(Hx) == (1,): assert z_shape[0] == 1, 'Shape of z must be {} for the given H'.format(shape(Hx)) else: assert (z_shape == shape(Hx) or (len(z_shape) == 1 and shape(Hx) == (z_shape[0], 1))), \ "shape of z should be {}, not {} for the given H".format( shape(Hx), z_shape) if np.ndim(Hx) > 1 and shape(Hx) != (1, 1): assert shape(Hx) == z_shape, \ 'shape of z should be {} for the given H, but it is {}'.format( shape(Hx), z_shape) def update(x, P, z, R, H=None, return_all=False): """ Add a new measurement (z) to the Kalman filter. If z is None, nothing is changed. This can handle either the multidimensional or unidimensional case. If all parameters are floats instead of arrays the filter will still work, and return floats for x, P as the result. update(1, 2, 1, 1, 1) # univariate update(x, P, 1 Parameters ---------- x : numpy.array(dim_x, 1), or float State estimate vector P : numpy.array(dim_x, dim_x), or float Covariance matrix z : (dim_z, 1): array_like measurement for this update. z can be a scalar if dim_z is 1, otherwise it must be convertible to a column vector. R : numpy.array(dim_z, dim_z), or float Measurement noise matrix H : numpy.array(dim_x, dim_x), or float, optional Measurement function. If not provided, a value of 1 is assumed. return_all : bool, default False If true, y, K, S, and log_likelihood are returned, otherwise only x and P are returned. Returns ------- x : numpy.array Posterior state estimate vector P : numpy.array Posterior covariance matrix y : numpy.array or scalar Residua. Difference between measurement and state in measurement space K : numpy.array Kalman gain S : numpy.array System uncertainty in measurement space log_likelihood : float log likelihood of the measurement """ #pylint: disable=bare-except if z is None: if return_all: return x, P, None, None, None, None return x, P if H is None: H = np.array([1]) if np.isscalar(H): H = np.array([H]) Hx = np.atleast_1d(dot(H, x)) z = reshape_z(z, Hx.shape[0], x.ndim) # error (residual) between measurement and prediction y = z - Hx # project system uncertainty into measurement space S = dot(dot(H, P), H.T) + R # map system uncertainty into kalman gain try: K = dot(dot(P, H.T), linalg.inv(S)) except: # can't invert a 1D array, annoyingly K = dot(dot(P, H.T), 1./S) # predict new x with residual scaled by the kalman gain x = x + dot(K, y) # P = (I-KH)P(I-KH)' + KRK' KH = dot(K, H) try: I_KH = np.eye(KH.shape[0]) - KH except: I_KH = np.array([1 - KH]) P = dot(dot(I_KH, P), I_KH.T) + dot(dot(K, R), K.T) if return_all: # compute log likelihood log_likelihood = logpdf(z, dot(H, x), S) return x, P, y, K, S, log_likelihood return x, P def update_steadystate(x, z, K, H=None): """ Add a new measurement (z) to the Kalman filter. If z is None, nothing is changed. Parameters ---------- x : numpy.array(dim_x, 1), or float State estimate vector z : (dim_z, 1): array_like measurement for this update. z can be a scalar if dim_z is 1, otherwise it must be convertible to a column vector. K : numpy.array, or float Kalman gain matrix H : numpy.array(dim_x, dim_x), or float, optional Measurement function. If not provided, a value of 1 is assumed. Returns ------- x : numpy.array Posterior state estimate vector Examples -------- This can handle either the multidimensional or unidimensional case. If all parameters are floats instead of arrays the filter will still work, and return floats for x, P as the result. >>> update_steadystate(1, 2, 1) # univariate >>> update_steadystate(x, P, z, H) """ if z is None: return x if H is None: H = np.array([1]) if np.isscalar(H): H = np.array([H]) Hx = np.atleast_1d(dot(H, x)) z = reshape_z(z, Hx.shape[0], x.ndim) # error (residual) between measurement and prediction y = z - Hx # estimate new x with residual scaled by the kalman gain return x + dot(K, y) def predict(x, P, F=1, Q=0, u=0, B=1, alpha=1.): """ Predict next state (prior) using the Kalman filter state propagation equations. Parameters ---------- x : numpy.array State estimate vector P : numpy.array Covariance matrix F : numpy.array() State Transition matrix Q : numpy.array, Optional Process noise matrix u : numpy.array, Optional, default 0. Control vector. If non-zero, it is multiplied by B to create the control input into the system. B : numpy.array, optional, default 0. Control transition matrix. alpha : float, Optional, default=1.0 Fading memory setting. 1.0 gives the normal Kalman filter, and values slightly larger than 1.0 (such as 1.02) give a fading memory effect - previous measurements have less influence on the filter's estimates. This formulation of the Fading memory filter (there are many) is due to Dan Simon Returns ------- x : numpy.array Prior state estimate vector P : numpy.array Prior covariance matrix """ if np.isscalar(F): F = np.array(F) x = dot(F, x) + dot(B, u) P = (alpha * alpha) * dot(dot(F, P), F.T) + Q return x, P def predict_steadystate(x, F=1, u=0, B=1): """ Predict next state (prior) using the Kalman filter state propagation equations. This steady state form only computes x, assuming that the covariance is constant. Parameters ---------- x : numpy.array State estimate vector P : numpy.array Covariance matrix F : numpy.array() State Transition matrix u : numpy.array, Optional, default 0. Control vector. If non-zero, it is multiplied by B to create the control input into the system. B : numpy.array, optional, default 0. Control transition matrix. Returns ------- x : numpy.array Prior state estimate vector """ if np.isscalar(F): F = np.array(F) x = dot(F, x) + dot(B, u) return x def batch_filter(x, P, zs, Fs, Qs, Hs, Rs, Bs=None, us=None, update_first=False, saver=None): """ Batch processes a sequences of measurements. Parameters ---------- zs : list-like list of measurements at each time step. Missing measurements must be represented by None. Fs : list-like list of values to use for the state transition matrix matrix. Qs : list-like list of values to use for the process error covariance. Hs : list-like list of values to use for the measurement matrix. Rs : list-like list of values to use for the measurement error covariance. Bs : list-like, optional list of values to use for the control transition matrix; a value of None in any position will cause the filter to use `self.B` for that time step. us : list-like, optional list of values to use for the control input vector; a value of None in any position will cause the filter to use 0 for that time step. update_first : bool, optional controls whether the order of operations is update followed by predict, or predict followed by update. Default is predict->update. saver : filterpy.common.Saver, optional filterpy.common.Saver object. If provided, saver.save() will be called after every epoch Returns ------- means : np.array((n,dim_x,1)) array of the state for each time step after the update. Each entry is an np.array. In other words `means[k,:]` is the state at step `k`. covariance : np.array((n,dim_x,dim_x)) array of the covariances for each time step after the update. In other words `covariance[k,:,:]` is the covariance at step `k`. means_predictions : np.array((n,dim_x,1)) array of the state for each time step after the predictions. Each entry is an np.array. In other words `means[k,:]` is the state at step `k`. covariance_predictions : np.array((n,dim_x,dim_x)) array of the covariances for each time step after the prediction. In other words `covariance[k,:,:]` is the covariance at step `k`. Examples -------- .. code-block:: Python zs = [t + random.randn()*4 for t in range (40)] Fs = [kf.F for t in range (40)] Hs = [kf.H for t in range (40)] (mu, cov, _, _) = kf.batch_filter(zs, Rs=R_list, Fs=Fs, Hs=Hs, Qs=None, Bs=None, us=None, update_first=False) (xs, Ps, Ks, Pps) = kf.rts_smoother(mu, cov, Fs=Fs, Qs=None) """ n = np.size(zs, 0) dim_x = x.shape[0] # mean estimates from Kalman Filter if x.ndim == 1: means = zeros((n, dim_x)) means_p = zeros((n, dim_x)) else: means = zeros((n, dim_x, 1)) means_p = zeros((n, dim_x, 1)) # state covariances from Kalman Filter covariances = zeros((n, dim_x, dim_x)) covariances_p = zeros((n, dim_x, dim_x)) if us is None: us = [0.] * n Bs = [0.] * n if update_first: for i, (z, F, Q, H, R, B, u) in enumerate(zip(zs, Fs, Qs, Hs, Rs, Bs, us)): x, P = update(x, P, z, R=R, H=H) means[i, :] = x covariances[i, :, :] = P x, P = predict(x, P, u=u, B=B, F=F, Q=Q) means_p[i, :] = x covariances_p[i, :, :] = P if saver is not None: saver.save() else: for i, (z, F, Q, H, R, B, u) in enumerate(zip(zs, Fs, Qs, Hs, Rs, Bs, us)): x, P = predict(x, P, u=u, B=B, F=F, Q=Q) means_p[i, :] = x covariances_p[i, :, :] = P x, P = update(x, P, z, R=R, H=H) means[i, :] = x covariances[i, :, :] = P if saver is not None: saver.save() return (means, covariances, means_p, covariances_p) def rts_smoother(Xs, Ps, Fs, Qs): """ Runs the Rauch-Tung-Striebel Kalman smoother on a set of means and covariances computed by a Kalman filter. The usual input would come from the output of `KalmanFilter.batch_filter()`. Parameters ---------- Xs : numpy.array array of the means (state variable x) of the output of a Kalman filter. Ps : numpy.array array of the covariances of the output of a kalman filter. Fs : list-like collection of numpy.array State transition matrix of the Kalman filter at each time step. Qs : list-like collection of numpy.array, optional Process noise of the Kalman filter at each time step. Returns ------- x : numpy.ndarray smoothed means P : numpy.ndarray smoothed state covariances K : numpy.ndarray smoother gain at each step pP : numpy.ndarray predicted state covariances Examples -------- .. code-block:: Python zs = [t + random.randn()*4 for t in range (40)] (mu, cov, _, _) = kalman.batch_filter(zs) (x, P, K, pP) = rts_smoother(mu, cov, kf.F, kf.Q) """ if len(Xs) != len(Ps): raise ValueError('length of Xs and Ps must be the same') n = Xs.shape[0] dim_x = Xs.shape[1] # smoother gain K = zeros((n, dim_x, dim_x)) x, P, pP = Xs.copy(), Ps.copy(), Ps.copy() for k in range(n-2, -1, -1): pP[k] = dot(dot(Fs[k], P[k]), Fs[k].T) + Qs[k] #pylint: disable=bad-whitespace K[k] = dot(dot(P[k], Fs[k].T), linalg.inv(pP[k])) x[k] += dot(K[k], x[k+1] - dot(Fs[k], x[k])) P[k] += dot(dot(K[k], P[k+1] - pP[k]), K[k].T) return (x, P, K, pP) ================================================ FILE: asone/trackers/oc_sort/tracker/ocsort.py ================================================ """ This script is adopted from the SORT script by Alex Bewley alex@bewley.ai """ from __future__ import print_function import numpy as np from .association import * def k_previous_obs(observations, cur_age, k): if len(observations) == 0: return [-1, -1, -1, -1, -1] for i in range(k): dt = k - i if cur_age - dt in observations: return observations[cur_age - dt] max_age = max(observations.keys()) return observations[max_age] def convert_bbox_to_z(bbox): """ Takes a bounding box in the form [x1,y1,x2,y2] and returns z in the form [x,y,s,r] where x,y is the centre of the box and s is the scale/area and r is the aspect ratio """ w = bbox[2] - bbox[0] h = bbox[3] - bbox[1] x = bbox[0] + w / 2.0 y = bbox[1] + h / 2.0 s = w * h # scale is just area r = w / float(h + 1e-6) return np.array([x, y, s, r]).reshape((4, 1)) def convert_x_to_bbox(x, score=None): """ Takes a bounding box in the centre form [x,y,s,r] and returns it in the form [x1,y1,x2,y2] where x1,y1 is the top left and x2,y2 is the bottom right """ w = np.sqrt(x[2] * x[3]) h = x[2] / w if score == None: return np.array([x[0] - w / 2.0, x[1] - h / 2.0, x[0] + w / 2.0, x[1] + h / 2.0]).reshape((1, 4)) else: return np.array([x[0] - w / 2.0, x[1] - h / 2.0, x[0] + w / 2.0, x[1] + h / 2.0, score]).reshape((1, 5)) def speed_direction(bbox1, bbox2): cx1, cy1 = (bbox1[0] + bbox1[2]) / 2.0, (bbox1[1] + bbox1[3]) / 2.0 cx2, cy2 = (bbox2[0] + bbox2[2]) / 2.0, (bbox2[1] + bbox2[3]) / 2.0 speed = np.array([cy2 - cy1, cx2 - cx1]) norm = np.sqrt((cy2 - cy1) ** 2 + (cx2 - cx1) ** 2) + 1e-6 return speed / norm class KalmanBoxTracker(object): """ This class represents the internal state of individual tracked objects observed as bbox. """ count = 0 def __init__(self, bbox, conf, cls, delta_t=3, orig=False): """ Initialises a tracker using initial bounding box. """ # define constant velocity model if not orig: from .kalmanfilter import KalmanFilterNew as KalmanFilter self.kf = KalmanFilter(dim_x=7, dim_z=4) else: from filterpy.kalman import KalmanFilter self.kf = KalmanFilter(dim_x=7, dim_z=4) self.kf.F = np.array( [ [1, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 1], ] ) self.kf.H = np.array( [[1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0]] ) self.kf.R[2:, 2:] *= 10.0 self.kf.P[4:, 4:] *= 1000.0 # give high uncertainty to the unobservable initial velocities self.kf.P *= 10.0 self.kf.Q[-1, -1] *= 0.01 self.kf.Q[4:, 4:] *= 0.01 self.kf.x[:4] = convert_bbox_to_z(bbox) self.time_since_update = 0 self.id = KalmanBoxTracker.count KalmanBoxTracker.count += 1 self.history = [] self.hits = 0 self.hit_streak = 0 self.age = 0 self.conf = conf self.cls = cls """ NOTE: [-1,-1,-1,-1,-1] is a compromising placeholder for non-observation status, the same for the return of function k_previous_obs. It is ugly and I do not like it. But to support generate observation array in a fast and unified way, which you would see below k_observations = np.array([k_previous_obs(...]]), let's bear it for now. """ self.last_observation = np.array([-1, -1, -1, -1, -1]) # placeholder self.observations = dict() self.history_observations = [] self.velocity = None self.delta_t = delta_t def update(self, bbox, cls): """ Updates the state vector with observed bbox. """ if bbox is not None: self.cls = cls if self.last_observation.sum() >= 0: # no previous observation previous_box = None for i in range(self.delta_t): dt = self.delta_t - i if self.age - dt in self.observations: previous_box = self.observations[self.age - dt] break if previous_box is None: previous_box = self.last_observation """ Estimate the track speed direction with observations \Delta t steps away """ self.velocity = speed_direction(previous_box, bbox) """ Insert new observations. This is a ugly way to maintain both self.observations and self.history_observations. Bear it for the moment. """ self.last_observation = bbox self.observations[self.age] = bbox self.history_observations.append(bbox) self.time_since_update = 0 self.history = [] self.hits += 1 self.hit_streak += 1 self.kf.update(convert_bbox_to_z(bbox)) else: self.kf.update(bbox) def predict(self): """ Advances the state vector and returns the predicted bounding box estimate. """ if (self.kf.x[6] + self.kf.x[2]) <= 0: self.kf.x[6] *= 0.0 self.kf.predict() self.age += 1 if self.time_since_update > 0: self.hit_streak = 0 self.time_since_update += 1 self.history.append(convert_x_to_bbox(self.kf.x)) return self.history[-1] def get_state(self): """ Returns the current bounding box estimate. """ return convert_x_to_bbox(self.kf.x) """ We support multiple ways for association cost calculation, by default we use IoU. GIoU may have better performance in some situations. We note that we hardly normalize the cost by all methods to (0,1) which may not be the best practice. """ ASSO_FUNCS = {"iou": iou_batch, "giou": giou_batch, "ciou": ciou_batch, "diou": diou_batch, "ct_dist": ct_dist} class OCSort(object): def __init__( self, det_thresh=0.2, max_age=100, min_hits=3, iou_threshold=0.3, delta_t=3, asso_func="iou", inertia=0.2, use_byte=False, ): """ Sets key parameters for SORT """ self.max_age = max_age self.min_hits = min_hits self.iou_threshold = iou_threshold self.trackers = [] self.frame_count = 0 self.det_thresh = det_thresh self.delta_t = delta_t self.asso_func = ASSO_FUNCS[asso_func] self.inertia = inertia self.use_byte = use_byte KalmanBoxTracker.count = 0 def update(self, dets, _): """ Params: dets - a numpy array of detections in the format [[x1,y1,x2,y2,score],[x1,y1,x2,y2,score],...] Requires: this method must be called once for each frame even with empty detections (use np.empty((0, 5)) for frames without detections). Returns the a similar array, where the last column is the object ID. NOTE: The number of objects returned may differ from the number of detections provided. """ self.frame_count += 1 xyxys = dets[:, 0:4] confs = dets[:, 4] clss = dets[:, 5] classes = clss xyxys = xyxys confs = confs output_results = np.column_stack((xyxys, confs, classes)) inds_low = confs > 0.1 inds_high = confs < self.det_thresh inds_second = np.logical_and(inds_low, inds_high) # self.det_thresh > score > 0.1, for second matching dets_second = output_results[inds_second] # detections for second matching remain_inds = confs > self.det_thresh dets = output_results[remain_inds] # get predicted locations from existing trackers. trks = np.zeros((len(self.trackers), 5)) to_del = [] ret = [] for t, trk in enumerate(trks): pos = self.trackers[t].predict()[0] trk[:] = [pos[0], pos[1], pos[2], pos[3], 0] if np.any(np.isnan(pos)): to_del.append(t) trks = np.ma.compress_rows(np.ma.masked_invalid(trks)) for t in reversed(to_del): self.trackers.pop(t) velocities = np.array([trk.velocity if trk.velocity is not None else np.array((0, 0)) for trk in self.trackers]) last_boxes = np.array([trk.last_observation for trk in self.trackers]) k_observations = np.array([k_previous_obs(trk.observations, trk.age, self.delta_t) for trk in self.trackers]) """ First round of association """ matched, unmatched_dets, unmatched_trks = associate( dets, trks, self.iou_threshold, velocities, k_observations, self.inertia ) for m in matched: self.trackers[m[1]].update(dets[m[0], :5], dets[m[0], 5]) """ Second round of associaton by OCR """ # BYTE association if self.use_byte and len(dets_second) > 0 and unmatched_trks.shape[0] > 0: u_trks = trks[unmatched_trks] iou_left = self.asso_func(dets_second, u_trks) # iou between low score detections and unmatched tracks iou_left = np.array(iou_left) if iou_left.max() > self.iou_threshold: """ NOTE: by using a lower threshold, e.g., self.iou_threshold - 0.1, you may get a higher performance especially on MOT17/MOT20 datasets. But we keep it uniform here for simplicity """ matched_indices = linear_assignment(-iou_left) to_remove_trk_indices = [] for m in matched_indices: det_ind, trk_ind = m[0], unmatched_trks[m[1]] if iou_left[m[0], m[1]] < self.iou_threshold: continue self.trackers[trk_ind].update(dets_second[det_ind, :5], dets_second[det_ind, 5]) to_remove_trk_indices.append(trk_ind) unmatched_trks = np.setdiff1d(unmatched_trks, np.array(to_remove_trk_indices)) if unmatched_dets.shape[0] > 0 and unmatched_trks.shape[0] > 0: left_dets = dets[unmatched_dets] left_trks = last_boxes[unmatched_trks] iou_left = self.asso_func(left_dets, left_trks) iou_left = np.array(iou_left) if iou_left.max() > self.iou_threshold: """ NOTE: by using a lower threshold, e.g., self.iou_threshold - 0.1, you may get a higher performance especially on MOT17/MOT20 datasets. But we keep it uniform here for simplicity """ rematched_indices = linear_assignment(-iou_left) to_remove_det_indices = [] to_remove_trk_indices = [] for m in rematched_indices: det_ind, trk_ind = unmatched_dets[m[0]], unmatched_trks[m[1]] if iou_left[m[0], m[1]] < self.iou_threshold: continue self.trackers[trk_ind].update(dets[det_ind, :5], dets[det_ind, 5]) to_remove_det_indices.append(det_ind) to_remove_trk_indices.append(trk_ind) unmatched_dets = np.setdiff1d(unmatched_dets, np.array(to_remove_det_indices)) unmatched_trks = np.setdiff1d(unmatched_trks, np.array(to_remove_trk_indices)) for m in unmatched_trks: self.trackers[m].update(None, None) # create and initialise new trackers for unmatched detections for i in unmatched_dets: trk = KalmanBoxTracker(dets[i, :4], dets[i, 4], dets[i, 5], delta_t=self.delta_t) self.trackers.append(trk) i = len(self.trackers) for trk in reversed(self.trackers): if trk.last_observation.sum() < 0: d = trk.get_state()[0] else: """ this is optional to use the recent observation or the kalman filter prediction, we didn't notice significant difference here """ d = trk.last_observation[:4] if (trk.time_since_update < 1) and (trk.hit_streak >= self.min_hits or self.frame_count <= self.min_hits): # +1 as MOT benchmark requires positive ret.append(np.concatenate((d, [trk.id + 1], [trk.cls], [trk.conf])).reshape(1, -1)) i -= 1 # remove dead tracklet if trk.time_since_update > self.max_age: self.trackers.pop(i) if len(ret) > 0: return np.concatenate(ret) return np.empty((0, 7)) ================================================ FILE: asone/trackers/strong_sort/__init__.py ================================================ ================================================ FILE: asone/trackers/strong_sort/strongsort.py ================================================ import numpy as np from .tracker.strong_sort import StrongSORT class StrongSort: def __init__(self, detector) -> None: self.tracker = StrongSORT(model_weights='osnet_x0_25_msmt17.pt', device='cpu') self.detector = detector try: self.input_shape = tuple(detector.model.get_inputs()[0].shape[2:]) except AttributeError as e: self.input_shape = (640, 640) def detect_and_track(self, image: np.ndarray, config: dict) -> tuple: _dets_xyxy, img = self.detector.detect( image, **config, return_image=True ) bbox_xyxy = _dets_xyxy[:, :4] conf = _dets_xyxy[:, 4] classes = _dets_xyxy[:, 5] # if isinstance(_dets_xyxy, np.ndarray) and len(_dets_xyxy) > 0: dets = self.tracker.update(bbox_xyxy, conf, classes, img) dets = np.array(dets) if dets != []: bbox_xyxy = dets[:, :4] ids = dets[:, 4] class_ids = dets[:, 5] scores = dets[:, 6] return bbox_xyxy, ids, scores, class_ids else: return [], [], [], [] ================================================ FILE: asone/trackers/strong_sort/tracker/__init__.py ================================================ from .strong_sort import StrongSORT __all__ = ['StrongSORT', 'build_tracker'] def build_tracker(cfg, use_cuda): return StrongSORT(cfg.STRONGSORT.REID_CKPT, max_dist=cfg.STRONGSORT.MAX_DIST, min_confidence=cfg.STRONGSORT.MIN_CONFIDENCE, nms_max_overlap=cfg.STRONGSORT.NMS_MAX_OVERLAP, max_iou_distance=cfg.STRONGSORT.MAX_IOU_DISTANCE, max_age=cfg.STRONGSORT.MAX_AGE, n_init=cfg.STRONGSORT.N_INIT, nn_budget=cfg.STRONGSORT.NN_BUDGET, use_cuda=use_cuda) ================================================ FILE: asone/trackers/strong_sort/tracker/configs/strong_sort.yaml ================================================ STRONGSORT: ECC: True # activate camera motion compensation MC_LAMBDA: 0.995 # matching with both appearance (1 - MC_LAMBDA) and motion cost EMA_ALPHA: 0.9 # updates appearance state in an exponential moving average manner MAX_DIST: 0.2 # The matching threshold. Samples with larger distance are considered an invalid match MAX_IOU_DISTANCE: 0.7 # Gating threshold. Associations with cost larger than this value are disregarded. MAX_AGE: 30 # Maximum number of missed misses before a track is deleted N_INIT: 3 # Number of frames that a track remains in initialization phase NN_BUDGET: 100 # Maximum size of the appearance descriptors gallery ================================================ FILE: asone/trackers/strong_sort/tracker/deep/__init__.py ================================================ ================================================ FILE: asone/trackers/strong_sort/tracker/deep/checkpoint/.gitkeep ================================================ ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/.flake8 ================================================ [flake8] ignore = # At least two spaces before inline comment E261, # Line lengths are recommended to be no greater than 79 characters E501, # Missing whitespace around arithmetic operator E226, # Blank line contains whitespace W293, # Do not use bare 'except' E722, # Line break after binary operator W504, # isort found an import in the wrong position I001 max-line-length = 79 exclude = __init__.py, build, torchreid/metrics/rank_cylib/ ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/.gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ pip-wheel-metadata/ share/python-wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .nox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # IPython profile_default/ ipython_config.py # pyenv .python-version # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # However, in case of collaboration, if having platform-specific dependencies or dependencies # having no cross-platform support, pipenv may install dependencies that don't work, or not # install all needed dependencies. #Pipfile.lock # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ .dmypy.json dmypy.json # Pyre type checker .pyre/ # Cython eval code *.c *.html # OS X .DS_Store .Spotlight-V100 .Trashes ._* # ReID reid-data/ log/ saved-models/ model-zoo/ debug* ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/.isort.cfg ================================================ [isort] line_length=79 multi_line_output=3 length_sort=true known_standard_library=numpy,setuptools known_myself=torchreid known_third_party=matplotlib,cv2,torch,torchvision,PIL,yacs no_lines_before=STDLIB,THIRDPARTY sections=FUTURE,STDLIB,THIRDPARTY,myself,FIRSTPARTY,LOCALFOLDER default_section=FIRSTPARTY ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/.style.yapf ================================================ [style] BASED_ON_STYLE = pep8 BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF = true SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN = true DEDENT_CLOSING_BRACKETS = true SPACES_BEFORE_COMMENT = 1 ARITHMETIC_PRECEDENCE_INDICATION = true ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/LICENSE ================================================ MIT License Copyright (c) 2018 Kaiyang Zhou Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/README.rst ================================================ Torchreid =========== Torchreid is a library for deep-learning person re-identification, written in `PyTorch `_ and developed for our ICCV'19 project, `Omni-Scale Feature Learning for Person Re-Identification `_. It features: - multi-GPU training - support both image- and video-reid - end-to-end training and evaluation - incredibly easy preparation of reid datasets - multi-dataset training - cross-dataset evaluation - standard protocol used by most research papers - highly extensible (easy to add models, datasets, training methods, etc.) - implementations of state-of-the-art deep reid models - access to pretrained reid models - advanced training techniques - visualization tools (tensorboard, ranks, etc.) Code: https://github.com/KaiyangZhou/deep-person-reid. Documentation: https://kaiyangzhou.github.io/deep-person-reid/. How-to instructions: https://kaiyangzhou.github.io/deep-person-reid/user_guide. Model zoo: https://kaiyangzhou.github.io/deep-person-reid/MODEL_ZOO. Tech report: https://arxiv.org/abs/1910.10093. You can find some research projects that are built on top of Torchreid `here `_. What's new ------------ - [Aug 2021] We have released the ImageNet-pretrained models of ``osnet_ain_x0_75``, ``osnet_ain_x0_5`` and ``osnet_ain_x0_25``. The pretraining setup follows `pycls `_. - [Apr 2021] We have updated the appendix in the `TPAMI version of OSNet `_ to include results in the multi-source domain generalization setting. The trained models can be found in the `Model Zoo `_. - [Apr 2021] We have added a script to automate the process of calculating average results over multiple splits. For more details please see ``tools/parse_test_res.py``. - [Apr 2021] ``v1.4.0``: We added the person search dataset, `CUHK-SYSU `_. Please see the `documentation `_ regarding how to download the dataset (it contains cropped person images). - [Apr 2021] All models in the model zoo have been moved to google drive. Please raise an issue if any model's performance is inconsistent with the numbers shown in the model zoo page (could be caused by wrong links). - [Mar 2021] `OSNet `_ will appear in the TPAMI journal! Compared with the conference version, which focuses on discriminative feature learning using the omni-scale building block, this journal extension further considers generalizable feature learning by integrating `instance normalization layers `_ with the OSNet architecture. We hope this journal paper can motivate more future work to taclke the generalization issue in cross-dataset re-ID. - [Mar 2021] Generalization across domains (datasets) in person re-ID is crucial in real-world applications, which is closely related to the topic of *domain generalization*. Interested in learning how the field of domain generalization has developed over the last decade? Check our recent survey in this topic at https://arxiv.org/abs/2103.02503, with coverage on the history, datasets, related problems, methodologies, potential directions, and so on (*methods designed for generalizable re-ID are also covered*!). - [Feb 2021] ``v1.3.6`` Added `University-1652 `_, a new dataset for multi-view multi-source geo-localization (credit to `Zhedong Zheng `_). - [Feb 2021] ``v1.3.5``: Now the `cython code `_ works on Windows (credit to `lablabla `_). - [Jan 2021] Our recent work, `MixStyle `_ (mixing instance-level feature statistics of samples of different domains for improving domain generalization), has been accepted to ICLR'21. The code has been released at https://github.com/KaiyangZhou/mixstyle-release where the person re-ID part is based on Torchreid. - [Jan 2021] A new evaluation metric called `mean Inverse Negative Penalty (mINP)` for person re-ID has been introduced in `Deep Learning for Person Re-identification: A Survey and Outlook (TPAMI 2021) `_. Their code can be accessed at ``_. - [Aug 2020] ``v1.3.3``: Fixed bug in ``visrank`` (caused by not unpacking ``dsetid``). - [Aug 2020] ``v1.3.2``: Added ``_junk_pids`` to ``grid`` and ``prid``. This avoids using mislabeled gallery images for training when setting ``combineall=True``. - [Aug 2020] ``v1.3.0``: (1) Added ``dsetid`` to the existing 3-tuple data source, resulting in ``(impath, pid, camid, dsetid)``. This variable denotes the dataset ID and is useful when combining multiple datasets for training (as a dataset indicator). E.g., when combining ``market1501`` and ``cuhk03``, the former will be assigned ``dsetid=0`` while the latter will be assigned ``dsetid=1``. (2) Added ``RandomDatasetSampler``. Analogous to ``RandomDomainSampler``, ``RandomDatasetSampler`` samples a certain number of images (``batch_size // num_datasets``) from each of specified datasets (the amount is determined by ``num_datasets``). - [Aug 2020] ``v1.2.6``: Added ``RandomDomainSampler`` (it samples ``num_cams`` cameras each with ``batch_size // num_cams`` images to form a mini-batch). - [Jun 2020] ``v1.2.5``: (1) Dataloader's output from ``__getitem__`` has been changed from ``list`` to ``dict``. Previously, an element, e.g. image tensor, was fetched with ``imgs=data[0]``. Now it should be obtained by ``imgs=data['img']``. See this `commit `_ for detailed changes. (2) Added ``k_tfm`` as an option to image data loader, which allows data augmentation to be applied ``k_tfm`` times *independently* to an image. If ``k_tfm > 1``, ``imgs=data['img']`` returns a list with ``k_tfm`` image tensors. - [May 2020] Added the person attribute recognition code used in `Omni-Scale Feature Learning for Person Re-Identification (ICCV'19) `_. See ``projects/attribute_recognition/``. - [May 2020] ``v1.2.1``: Added a simple API for feature extraction (``torchreid/utils/feature_extractor.py``). See the `documentation `_ for the instruction. - [Apr 2020] Code for reproducing the experiments of `deep mutual learning `_ in the `OSNet paper `__ (Supp. B) has been released at ``projects/DML``. - [Apr 2020] Upgraded to ``v1.2.0``. The engine class has been made more model-agnostic to improve extensibility. See `Engine `_ and `ImageSoftmaxEngine `_ for more details. Credit to `Dassl.pytorch `_. - [Dec 2019] Our `OSNet paper `_ has been updated, with additional experiments (in section B of the supplementary) showing some useful techniques for improving OSNet's performance in practice. - [Nov 2019] ``ImageDataManager`` can load training data from target datasets by setting ``load_train_targets=True``, and the train-loader can be accessed with ``train_loader_t = datamanager.train_loader_t``. This feature is useful for domain adaptation research. Installation --------------- Make sure `conda `_ is installed. .. code-block:: bash # cd to your preferred directory and clone this repo git clone https://github.com/KaiyangZhou/deep-person-reid.git # create environment cd deep-person-reid/ conda create --name torchreid python=3.7 conda activate torchreid # install dependencies # make sure `which python` and `which pip` point to the correct path pip install -r requirements.txt # install torch and torchvision (select the proper cuda version to suit your machine) conda install pytorch torchvision cudatoolkit=9.0 -c pytorch # install torchreid (don't need to re-build it if you modify the source code) python setup.py develop Get started: 30 seconds to Torchreid ------------------------------------- 1. Import ``torchreid`` .. code-block:: python import torchreid 2. Load data manager .. code-block:: python datamanager = torchreid.data.ImageDataManager( root="reid-data", sources="market1501", targets="market1501", height=256, width=128, batch_size_train=32, batch_size_test=100, transforms=["random_flip", "random_crop"] ) 3 Build model, optimizer and lr_scheduler .. code-block:: python model = torchreid.models.build_model( name="resnet50", num_classes=datamanager.num_train_pids, loss="softmax", pretrained=True ) model = model.cuda() optimizer = torchreid.optim.build_optimizer( model, optim="adam", lr=0.0003 ) scheduler = torchreid.optim.build_lr_scheduler( optimizer, lr_scheduler="single_step", stepsize=20 ) 4. Build engine .. code-block:: python engine = torchreid.engine.ImageSoftmaxEngine( datamanager, model, optimizer=optimizer, scheduler=scheduler, label_smooth=True ) 5. Run training and test .. code-block:: python engine.run( save_dir="log/resnet50", max_epoch=60, eval_freq=10, print_freq=10, test_only=False ) A unified interface ----------------------- In "deep-person-reid/scripts/", we provide a unified interface to train and test a model. See "scripts/main.py" and "scripts/default_config.py" for more details. The folder "configs/" contains some predefined configs which you can use as a starting point. Below we provide an example to train and test `OSNet (Zhou et al. ICCV'19) `_. Assume :code:`PATH_TO_DATA` is the directory containing reid datasets. The environmental variable :code:`CUDA_VISIBLE_DEVICES` is omitted, which you need to specify if you have a pool of gpus and want to use a specific set of them. Conventional setting ^^^^^^^^^^^^^^^^^^^^^ To train OSNet on Market1501, do .. code-block:: bash python scripts/main.py \ --config-file configs/im_osnet_x1_0_softmax_256x128_amsgrad_cosine.yaml \ --transforms random_flip random_erase \ --root $PATH_TO_DATA The config file sets Market1501 as the default dataset. If you wanna use DukeMTMC-reID, do .. code-block:: bash python scripts/main.py \ --config-file configs/im_osnet_x1_0_softmax_256x128_amsgrad_cosine.yaml \ -s dukemtmcreid \ -t dukemtmcreid \ --transforms random_flip random_erase \ --root $PATH_TO_DATA \ data.save_dir log/osnet_x1_0_dukemtmcreid_softmax_cosinelr The code will automatically (download and) load the ImageNet pretrained weights. After the training is done, the model will be saved as "log/osnet_x1_0_market1501_softmax_cosinelr/model.pth.tar-250". Under the same folder, you can find the `tensorboard `_ file. To visualize the learning curves using tensorboard, you can run :code:`tensorboard --logdir=log/osnet_x1_0_market1501_softmax_cosinelr` in the terminal and visit :code:`http://localhost:6006/` in your web browser. Evaluation is automatically performed at the end of training. To run the test again using the trained model, do .. code-block:: bash python scripts/main.py \ --config-file configs/im_osnet_x1_0_softmax_256x128_amsgrad_cosine.yaml \ --root $PATH_TO_DATA \ model.load_weights log/osnet_x1_0_market1501_softmax_cosinelr/model.pth.tar-250 \ test.evaluate True Cross-domain setting ^^^^^^^^^^^^^^^^^^^^^ Suppose you wanna train OSNet on DukeMTMC-reID and test its performance on Market1501, you can do .. code-block:: bash python scripts/main.py \ --config-file configs/im_osnet_x1_0_softmax_256x128_amsgrad.yaml \ -s dukemtmcreid \ -t market1501 \ --transforms random_flip color_jitter \ --root $PATH_TO_DATA Here we only test the cross-domain performance. However, if you also want to test the performance on the source dataset, i.e. DukeMTMC-reID, you can set :code:`-t dukemtmcreid market1501`, which will evaluate the model on the two datasets separately. Different from the same-domain setting, here we replace :code:`random_erase` with :code:`color_jitter`. This can improve the generalization performance on the unseen target dataset. Pretrained models are available in the `Model Zoo `_. Datasets -------- Image-reid datasets ^^^^^^^^^^^^^^^^^^^^^ - `Market1501 `_ - `CUHK03 `_ - `DukeMTMC-reID `_ - `MSMT17 `_ - `VIPeR `_ - `GRID `_ - `CUHK01 `_ - `SenseReID `_ - `QMUL-iLIDS `_ - `PRID `_ Geo-localization datasets ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - `University-1652 `_ Video-reid datasets ^^^^^^^^^^^^^^^^^^^^^^^ - `MARS `_ - `iLIDS-VID `_ - `PRID2011 `_ - `DukeMTMC-VideoReID `_ Models ------- ImageNet classification models ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - `ResNet `_ - `ResNeXt `_ - `SENet `_ - `DenseNet `_ - `Inception-ResNet-V2 `_ - `Inception-V4 `_ - `Xception `_ - `IBN-Net `_ Lightweight models ^^^^^^^^^^^^^^^^^^^ - `NASNet `_ - `MobileNetV2 `_ - `ShuffleNet `_ - `ShuffleNetV2 `_ - `SqueezeNet `_ ReID-specific models ^^^^^^^^^^^^^^^^^^^^^^ - `MuDeep `_ - `ResNet-mid `_ - `HACNN `_ - `PCB `_ - `MLFN `_ - `OSNet `_ - `OSNet-AIN `_ Useful links ------------- - `OSNet-IBN1-Lite (test-only code with lite docker container) `_ - `Deep Learning for Person Re-identification: A Survey and Outlook `_ Citation --------- If you use this code or the models in your research, please give credit to the following papers: .. code-block:: bash @article{torchreid, title={Torchreid: A Library for Deep Learning Person Re-Identification in Pytorch}, author={Zhou, Kaiyang and Xiang, Tao}, journal={arXiv preprint arXiv:1910.10093}, year={2019} } @inproceedings{zhou2019osnet, title={Omni-Scale Feature Learning for Person Re-Identification}, author={Zhou, Kaiyang and Yang, Yongxin and Cavallaro, Andrea and Xiang, Tao}, booktitle={ICCV}, year={2019} } @article{zhou2021osnet, title={Learning Generalisable Omni-Scale Representations for Person Re-Identification}, author={Zhou, Kaiyang and Yang, Yongxin and Cavallaro, Andrea and Xiang, Tao}, journal={TPAMI}, year={2021} } ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/configs/im_osnet_ain_x1_0_softmax_256x128_amsgrad_cosine.yaml ================================================ model: name: 'osnet_ain_x1_0' pretrained: True data: type: 'image' sources: ['market1501'] targets: ['market1501', 'dukemtmcreid'] height: 256 width: 128 combineall: False transforms: ['random_flip', 'color_jitter'] save_dir: 'log/osnet_ain_x1_0_market1501_softmax_cosinelr' loss: name: 'softmax' softmax: label_smooth: True train: optim: 'amsgrad' lr: 0.0015 max_epoch: 100 batch_size: 64 fixbase_epoch: 10 open_layers: ['classifier'] lr_scheduler: 'cosine' test: batch_size: 300 dist_metric: 'cosine' normalize_feature: False evaluate: False eval_freq: -1 rerank: False ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/configs/im_osnet_ibn_x1_0_softmax_256x128_amsgrad.yaml ================================================ model: name: 'osnet_ibn_x1_0' pretrained: True data: type: 'image' sources: ['market1501'] targets: ['dukemtmcreid'] height: 256 width: 128 combineall: False transforms: ['random_flip', 'color_jitter'] save_dir: 'log/osnet_ibn_x1_0_market2duke_softmax' loss: name: 'softmax' softmax: label_smooth: True train: optim: 'amsgrad' lr: 0.0015 max_epoch: 150 batch_size: 64 fixbase_epoch: 10 open_layers: ['classifier'] lr_scheduler: 'single_step' stepsize: [60] test: batch_size: 300 dist_metric: 'euclidean' normalize_feature: False evaluate: False eval_freq: -1 rerank: False ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/configs/im_osnet_x0_25_softmax_256x128_amsgrad.yaml ================================================ model: name: 'osnet_x0_25' pretrained: True data: type: 'image' sources: ['market1501'] targets: ['market1501'] height: 256 width: 128 combineall: False transforms: ['random_flip'] save_dir: 'log/osnet_x0_25_market1501_softmax' loss: name: 'softmax' softmax: label_smooth: True train: optim: 'amsgrad' lr: 0.003 max_epoch: 180 batch_size: 128 fixbase_epoch: 10 open_layers: ['classifier'] lr_scheduler: 'single_step' stepsize: [80] test: batch_size: 300 dist_metric: 'euclidean' normalize_feature: False evaluate: False eval_freq: -1 rerank: False ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/configs/im_osnet_x0_5_softmax_256x128_amsgrad.yaml ================================================ model: name: 'osnet_x0_5' pretrained: True data: type: 'image' sources: ['market1501'] targets: ['market1501'] height: 256 width: 128 combineall: False transforms: ['random_flip'] save_dir: 'log/osnet_x0_5_market1501_softmax' loss: name: 'softmax' softmax: label_smooth: True train: optim: 'amsgrad' lr: 0.003 max_epoch: 180 batch_size: 128 fixbase_epoch: 10 open_layers: ['classifier'] lr_scheduler: 'single_step' stepsize: [80] test: batch_size: 300 dist_metric: 'euclidean' normalize_feature: False evaluate: False eval_freq: -1 rerank: False ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/configs/im_osnet_x0_75_softmax_256x128_amsgrad.yaml ================================================ model: name: 'osnet_x0_75' pretrained: True data: type: 'image' sources: ['market1501'] targets: ['market1501'] height: 256 width: 128 combineall: False transforms: ['random_flip'] save_dir: 'log/osnet_x0_75_market1501_softmax' loss: name: 'softmax' softmax: label_smooth: True train: optim: 'amsgrad' lr: 0.0015 max_epoch: 150 batch_size: 64 fixbase_epoch: 10 open_layers: ['classifier'] lr_scheduler: 'single_step' stepsize: [60] test: batch_size: 300 dist_metric: 'euclidean' normalize_feature: False evaluate: False eval_freq: -1 rerank: False ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/configs/im_osnet_x1_0_softmax_256x128_amsgrad.yaml ================================================ model: name: 'osnet_x1_0' pretrained: True data: type: 'image' sources: ['market1501'] targets: ['market1501'] height: 256 width: 128 combineall: False transforms: ['random_flip'] save_dir: 'log/osnet_x1_0_market1501_softmax' loss: name: 'softmax' softmax: label_smooth: True train: optim: 'amsgrad' lr: 0.0015 max_epoch: 150 batch_size: 64 fixbase_epoch: 10 open_layers: ['classifier'] lr_scheduler: 'single_step' stepsize: [60] test: batch_size: 300 dist_metric: 'euclidean' normalize_feature: False evaluate: False eval_freq: -1 rerank: False ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/configs/im_osnet_x1_0_softmax_256x128_amsgrad_cosine.yaml ================================================ model: name: 'osnet_x1_0' pretrained: True data: type: 'image' sources: ['market1501'] targets: ['market1501'] height: 256 width: 128 combineall: False transforms: ['random_flip'] save_dir: 'log/osnet_x1_0_market1501_softmax_cosinelr' loss: name: 'softmax' softmax: label_smooth: True train: optim: 'amsgrad' lr: 0.0015 max_epoch: 250 batch_size: 64 fixbase_epoch: 10 open_layers: ['classifier'] lr_scheduler: 'cosine' test: batch_size: 300 dist_metric: 'euclidean' normalize_feature: False evaluate: False eval_freq: -1 rerank: False ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/configs/im_r50_softmax_256x128_amsgrad.yaml ================================================ model: name: 'resnet50_fc512' pretrained: True data: type: 'image' sources: ['market1501'] targets: ['market1501'] height: 256 width: 128 combineall: False transforms: ['random_flip'] save_dir: 'log/resnet50_market1501_softmax' loss: name: 'softmax' softmax: label_smooth: True train: optim: 'amsgrad' lr: 0.0003 max_epoch: 60 batch_size: 32 fixbase_epoch: 5 open_layers: ['classifier'] lr_scheduler: 'single_step' stepsize: [20] test: batch_size: 100 dist_metric: 'euclidean' normalize_feature: False evaluate: False eval_freq: -1 rerank: False ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/configs/im_r50fc512_softmax_256x128_amsgrad.yaml ================================================ model: name: 'resnet50_fc512' pretrained: True data: type: 'image' sources: ['market1501'] targets: ['market1501'] height: 256 width: 128 combineall: False transforms: ['random_flip'] save_dir: 'log/resnet50_fc512_market1501_softmax' loss: name: 'softmax' softmax: label_smooth: True train: optim: 'amsgrad' lr: 0.0003 max_epoch: 60 batch_size: 32 fixbase_epoch: 5 open_layers: ['fc', 'classifier'] lr_scheduler: 'single_step' stepsize: [20] test: batch_size: 100 dist_metric: 'euclidean' normalize_feature: False evaluate: False eval_freq: -1 rerank: False ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/AWESOME_REID.md ================================================ # Awesome-ReID Here is a collection of ReID-related research with links to papers and code. You are welcome to submit [PR](https://help.github.com/articles/creating-a-pull-request/)s if you find something missing. - [TPAMI21] Learning Generalisable Omni-Scale Representations for Person Re-Identification [[paper](https://arxiv.org/abs/1910.06827)][[code](https://github.com/KaiyangZhou/deep-person-reid)] - [TPAMI21] Deep Learning for Person Re-identification: A Survey and Outlook [[paper](https://arxiv.org/abs/2001.04193)] [[code](https://github.com/mangye16/ReID-Survey)] - [ICCV19] RGB-Infrared Cross-Modality Person Re-Identification via Joint Pixel and Feature Alignment. [[paper](http://openaccess.thecvf.com/content_ICCV_2019/papers/Wang_RGB-Infrared_Cross-Modality_Person_Re-Identification_via_Joint_Pixel_and_Feature_Alignment_ICCV_2019_paper.pdf)] [[code](https://github.com/wangguanan/AlignGAN)] - [ICCV19] Unsupervised Graph Association for Person Re-identification. [[paper](https://github.com/yichuan9527/Unsupervised-Graph-Association-for-Person-Re-identification)] [[code](https://github.com/yichuan9527/Unsupervised-Graph-Association-for-Person-Re-identification)] - [ICCV19] Self-similarity Grouping: A Simple Unsupervised Cross Domain Adaptation Approach for Person Re-identification. [[paper](http://openaccess.thecvf.com/content_ICCV_2019/papers/Fu_Self-Similarity_Grouping_A_Simple_Unsupervised_Cross_Domain_Adaptation_Approach_for_ICCV_2019_paper.pdf)] [[code](https://github.com/OasisYang/SSG)] - [ICCV19] Spectral Feature Transformation for Person Re-Identification. [[paper](http://openaccess.thecvf.com/content_ICCV_2019/papers/Luo_Spectral_Feature_Transformation_for_Person_Re-Identification_ICCV_2019_paper.pdf)] [[code](https://github.com/LuckyDC/SFT_REID)] - [ICCV19] Beyond Human Parts: Dual Part-Aligned Representations for Person Re-Identification. [[paper](http://openaccess.thecvf.com/content_ICCV_2019/papers/Guo_Beyond_Human_Parts_Dual_Part-Aligned_Representations_for_Person_Re-Identification_ICCV_2019_paper.pdf)] [[code](https://github.com/ggjy/P2Net.pytorch)] - [ICCV19] Co-segmentation Inspired Attention Networks for Video-based Person Re-identification. [[paper](http://openaccess.thecvf.com/content_ICCV_2019/papers/Subramaniam_Co-Segmentation_Inspired_Attention_Networks_for_Video-Based_Person_Re-Identification_ICCV_2019_paper.pdf)][[code](https://github.com/InnovArul/vidreid_cosegmentation)] - [ICCV19] Mixed High-Order Attention Network for Person Re-Identification. [[paper](https://arxiv.org/abs/1908.05819)][[code](https://github.com/chenbinghui1/MHN)] - [ICCV19] ABD-Net: Attentive but Diverse Person Re-Identification. [[paper](https://arxiv.org/abs/1908.01114)] [[code](https://github.com/TAMU-VITA/ABD-Net)] - [ICCV19] Omni-Scale Feature Learning for Person Re-Identification. [[paper](https://arxiv.org/abs/1905.00953)] [[code](https://github.com/KaiyangZhou/deep-person-reid)] - [CVPR19] Joint Discriminative and Generative Learning for Person Re-identification. [[paper](https://arxiv.org/abs/1904.07223)][[code](https://github.com/NVlabs/DG-Net)] - [CVPR19] Invariance Matters: Exemplar Memory for Domain Adaptive Person Re-identification. [[paper](https://arxiv.org/abs/1904.01990)][[code](https://github.com/zhunzhong07/ECN)] - [CVPR19] Dissecting Person Re-identification from the Viewpoint of Viewpoint. [[paper](https://arxiv.org/abs/1812.02162)][[code](https://github.com/sxzrt/Dissecting-Person-Re-ID-from-the-Viewpoint-of-Viewpoint)] - [CVPR19] Unsupervised Person Re-identification by Soft Multilabel Learning. [[paper](https://arxiv.org/abs/1903.06325)][[code](https://github.com/KovenYu/MAR)] - [CVPR19] Patch-based Discriminative Feature Learning for Unsupervised Person Re-identification. [[paper](https://kovenyu.com/publication/2019-cvpr-pedal/)][[code](https://github.com/QizeYang/PAUL)] - [AAAI19] Spatial and Temporal Mutual Promotion for Video-based Person Re-identification. [[paper](https://arxiv.org/abs/1812.10305)][[code](https://github.com/yolomax/person-reid-lib)] - [AAAI19] Spatial-Temporal Person Re-identification. [[paper](https://arxiv.org/abs/1812.03282)][[code](https://github.com/Wanggcong/Spatial-Temporal-Re-identification)] - [AAAI19] Horizontal Pyramid Matching for Person Re-identification. [[paper](https://arxiv.org/abs/1804.05275)][[code](https://github.com/OasisYang/HPM)] - [AAAI19] Backbone Can Not be Trained at Once: Rolling Back to Pre-trained Network for Person Re-identification. [[paper](https://arxiv.org/abs/1901.06140)][[code](https://github.com/youngminPIL/rollback)] - [AAAI19] A Bottom-Up Clustering Approach to Unsupervised Person Re-identification. [[paper](https://vana77.github.io/vana77.github.io/images/AAAI19.pdf)][[code](https://github.com/vana77/Bottom-up-Clustering-Person-Re-identification)] - [NIPS18] FD-GAN: Pose-guided Feature Distilling GAN for Robust Person Re-identification. [[paper](https://arxiv.org/abs/1810.02936)][[code](https://github.com/yxgeee/FD-GAN)] - [ECCV18] Generalizing A Person Retrieval Model Hetero- and Homogeneously. [[paper](http://openaccess.thecvf.com/content_ECCV_2018/papers/Zhun_Zhong_Generalizing_A_Person_ECCV_2018_paper.pdf)][[code](https://github.com/zhunzhong07/HHL)] - [ECCV18] Pose-Normalized Image Generation for Person Re-identification. [[paper](https://arxiv.org/abs/1712.02225)][[code](https://github.com/naiq/PN_GAN)] - [CVPR18] Camera Style Adaptation for Person Re-Identification. [[paper](https://arxiv.org/abs/1711.10295)][[code](https://github.com/zhunzhong07/CamStyle)] - [CVPR18] Deep Group-Shuffling Random Walk for Person Re-Identification. [[paper](https://arxiv.org/abs/1807.11178)][[code](https://github.com/YantaoShen/kpm_rw_person_reid)] - [CVPR18] End-to-End Deep Kronecker-Product Matching for Person Re-identification. [[paper](https://arxiv.org/abs/1807.11182)][[code](https://github.com/YantaoShen/kpm_rw_person_reid)] - [CVPR18] Features for Multi-Target Multi-Camera Tracking and Re-Identification. [[paper](https://arxiv.org/abs/1803.10859)][[code](https://github.com/ergysr/DeepCC)] - [CVPR18] Group Consistent Similarity Learning via Deep CRF for Person Re-Identification. [[paper](http://openaccess.thecvf.com/content_cvpr_2018/papers/Chen_Group_Consistent_Similarity_CVPR_2018_paper.pdf)][[code](https://github.com/dapengchen123/crf_affinity)] - [CVPR18] Harmonious Attention Network for Person Re-Identification. [[paper](https://arxiv.org/abs/1802.08122)][[code](https://github.com/KaiyangZhou/deep-person-reid)] - [CVPR18] Human Semantic Parsing for Person Re-Identification. [[paper](https://arxiv.org/abs/1804.00216)][[code](https://github.com/emrahbasaran/SPReID)] - [CVPR18] Multi-Level Factorisation Net for Person Re-Identification. [[paper](https://arxiv.org/abs/1803.09132)][[code](https://github.com/KaiyangZhou/deep-person-reid)] - [CVPR18] Resource Aware Person Re-identification across Multiple Resolutions. [[paper](https://arxiv.org/abs/1805.08805)][[code](https://github.com/mileyan/DARENet)] - [CVPR18] Exploit the Unknown Gradually: One-Shot Video-Based Person Re-Identification by Stepwise Learning. [[paper](https://yu-wu.net/pdf/CVPR2018_Exploit-Unknown-Gradually.pdf)][[code](https://github.com/Yu-Wu/Exploit-Unknown-Gradually)] - [ArXiv18] Revisiting Temporal Modeling for Video-based Person ReID. [[paper](https://arxiv.org/abs/1805.02104)][[code](https://github.com/jiyanggao/Video-Person-ReID)] ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/MODEL_ZOO.md ================================================ # Model Zoo - Results are presented in the format of **. - When computing model size and FLOPs, only layers that are used at test time are considered (see `torchreid.utils.compute_model_complexity`). - Asterisk (\*) means the model is trained from scratch. - `combineall=True` means all images in the dataset are used for model training. - Why not use heavy data augmentation like [random erasing](https://arxiv.org/abs/1708.04896) for model training? It's because heavy data augmentation might harm the cross-dataset generalization performance (see [this paper](https://arxiv.org/abs/1708.04896)). ## ImageNet pretrained models | Model | Download | | :--- | :---: | | shufflenet | [model](https://drive.google.com/file/d/1RFnYcHK1TM-yt3yLsNecaKCoFO4Yb6a-/view?usp=sharing) | | mobilenetv2_x1_0 | [model](https://drive.google.com/file/d/1K7_CZE_L_Tf-BRY6_vVm0G-0ZKjVWh3R/view?usp=sharing) | | mobilenetv2_x1_4 | [model](https://drive.google.com/file/d/10c0ToIGIVI0QZTx284nJe8QfSJl5bIta/view?usp=sharing) | | mlfn | [model](https://drive.google.com/file/d/1PP8Eygct5OF4YItYRfA3qypYY9xiqHuV/view?usp=sharing) | | osnet_x1_0 | [model](https://drive.google.com/file/d/1LaG1EJpHrxdAxKnSCJ_i0u-nbxSAeiFY/view?usp=sharing) | | osnet_x0_75 | [model](https://drive.google.com/file/d/1uwA9fElHOk3ZogwbeY5GkLI6QPTX70Hq/view?usp=sharing) | | osnet_x0_5 | [model](https://drive.google.com/file/d/16DGLbZukvVYgINws8u8deSaOqjybZ83i/view?usp=sharing) | | osnet_x0_25 | [model](https://drive.google.com/file/d/1rb8UN5ZzPKRc_xvtHlyDh-cSz88YX9hs/view?usp=sharing) | | osnet_ibn_x1_0 | [model](https://drive.google.com/file/d/1sr90V6irlYYDd4_4ISU2iruoRG8J__6l/view?usp=sharing) | | osnet_ain_x1_0 | [model](https://drive.google.com/file/d/1-CaioD9NaqbHK_kzSMW8VE4_3KcsRjEo/view?usp=sharing) | | osnet_ain_x0_75 | [model](https://drive.google.com/file/d/1apy0hpsMypqstfencdH-jKIUEFOW4xoM/view?usp=sharing) | | osnet_ain_x0_5 | [model](https://drive.google.com/file/d/1KusKvEYyKGDTUBVRxRiz55G31wkihB6l/view?usp=sharing) | | osnet_ain_x0_25 | [model](https://drive.google.com/file/d/1SxQt2AvmEcgWNhaRb2xC4rP6ZwVDP0Wt/view?usp=sharing) | ## Same-domain ReID | Model | # Param (10^6) | GFLOPs | Loss | Input | Transforms | Distance | market1501 | dukemtmcreid | msmt17 | | :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | resnet50 | 23.5 | 2.7 | softmax | (256, 128) | `random_flip`, `random_crop` | `euclidean` | [87.9 (70.4)](https://drive.google.com/file/d/1dUUZ4rHDWohmsQXCRe2C_HbYkzz94iBV/view?usp=sharing) | [78.3 (58.9)](https://drive.google.com/file/d/17ymnLglnc64NRvGOitY3BqMRS9UWd1wg/view?usp=sharing) | [63.2 (33.9)](https://drive.google.com/file/d/1ep7RypVDOthCRIAqDnn4_N-UhkkFHJsj/view?usp=sharing) | | resnet50_fc512 | 24.6 | 4.1 | softmax | (256, 128) | `random_flip`, `random_crop` | `euclidean` | [90.8 (75.3)](https://drive.google.com/file/d/1kv8l5laX_YCdIGVCetjlNdzKIA3NvsSt/view?usp=sharing) | [81.0 (64.0)](https://drive.google.com/file/d/13QN8Mp3XH81GK4BPGXobKHKyTGH50Rtx/view?usp=sharing) | [69.6 (38.4)](https://drive.google.com/file/d/1fDJLcz4O5wxNSUvImIIjoaIF9u1Rwaud/view?usp=sharing) | | mlfn | 32.5 | 2.8 | softmax | (256, 128) | `random_flip`, `random_crop` | `euclidean` | [90.1 (74.3)](https://drive.google.com/file/d/1wXcvhA_b1kpDfrt9s2Pma-MHxtj9pmvS/view?usp=sharing) | [81.1 (63.2)](https://drive.google.com/file/d/1rExgrTNb0VCIcOnXfMsbwSUW1h2L1Bum/view?usp=sharing) | [66.4 (37.2)](https://drive.google.com/file/d/18JzsZlJb3Wm7irCbZbZ07TN4IFKvR6p-/view?usp=sharing) | | hacnn* | 4.5 | 0.5 | softmax | (160, 64) | `random_flip`, `random_crop` | `euclidean` | [90.9 (75.6)](https://drive.google.com/file/d/1LRKIQduThwGxMDQMiVkTScBwR7WidmYF/view?usp=sharing) | [80.1 (63.2)](https://drive.google.com/file/d/1zNm6tP4ozFUCUQ7Sv1Z98EAJWXJEhtYH/view?usp=sharing) | [64.7 (37.2)](https://drive.google.com/file/d/1MsKRtPM5WJ3_Tk2xC0aGOO7pM3VaFDNZ/view?usp=sharing) | | mobilenetv2_x1_0 | 2.2 | 0.2 | softmax | (256, 128) | `random_flip`, `random_crop` | `euclidean` | [85.6 (67.3)](https://drive.google.com/file/d/18DgHC2ZJkjekVoqBWszD8_Xiikz-fewp/view?usp=sharing) | [74.2 (54.7)](https://drive.google.com/file/d/1q1WU2FETRJ3BXcpVtfJUuqq4z3psetds/view?usp=sharing) | [57.4 (29.3)](https://drive.google.com/file/d/1j50Hv14NOUAg7ZeB3frzfX-WYLi7SrhZ/view?usp=sharing) | | mobilenetv2_x1_4 | 4.3 | 0.4 | softmax | (256, 128) | `random_flip`, `random_crop` | `euclidean` | [87.0 (68.5)](https://drive.google.com/file/d/1t6JCqphJG-fwwPVkRLmGGyEBhGOf2GO5/view?usp=sharing) | [76.2 (55.8)](https://drive.google.com/file/d/12uD5FeVqLg9-AFDju2L7SQxjmPb4zpBN/view?usp=sharing) | [60.1 (31.5)](https://drive.google.com/file/d/1ZY5P2Zgm-3RbDpbXM0kIBMPvspeNIbXz/view?usp=sharing) | | osnet_x1_0 | 2.2 | 0.98 | softmax | (256, 128) | `random_flip` | `euclidean` | [94.2 (82.6)](https://drive.google.com/file/d/1vduhq5DpN2q1g4fYEZfPI17MJeh9qyrA/view?usp=sharing) | [87.0 (70.2)](https://drive.google.com/file/d/1QZO_4sNf4hdOKKKzKc-TZU9WW1v6zQbq/view?usp=sharing) | [74.9 (43.8)](https://drive.google.com/file/d/112EMUfBPYeYg70w-syK6V6Mx8-Qb9Q1M/view?usp=sharing) | | osnet_x0_75 | 1.3 | 0.57 | softmax | (256, 128) | `random_flip` | `euclidean` | [93.7 (81.2)](https://drive.google.com/file/d/1ozRaDSQw_EQ8_93OUmjDbvLXw9TnfPer/view?usp=sharing) | [85.8 (69.8)](https://drive.google.com/file/d/1IE3KRaTPp4OUa6PGTFL_d5_KQSJbP0Or/view?usp=sharing) | [72.8 (41.4)](https://drive.google.com/file/d/1QEGO6WnJ-BmUzVPd3q9NoaO_GsPNlmWc/view?usp=sharing) | | osnet_x0_5 | 0.6 | 0.27 | softmax | (256, 128) | `random_flip` | `euclidean` | [92.5 (79.8)](https://drive.google.com/file/d/1PLB9rgqrUM7blWrg4QlprCuPT7ILYGKT/view?usp=sharing) | [85.1 (67.4)](https://drive.google.com/file/d/1KoUVqmiST175hnkALg9XuTi1oYpqcyTu/view?usp=sharing) | [69.7 (37.5)](https://drive.google.com/file/d/1UT3AxIaDvS2PdxzZmbkLmjtiqq7AIKCv/view?usp=sharing) | | osnet_x0_25 | 0.2 | 0.08 | softmax | (256, 128) | `random_flip` | `euclidean` | [91.2 (75.0)](https://drive.google.com/file/d/1z1UghYvOTtjx7kEoRfmqSMu-z62J6MAj/view?usp=sharing) | [82.0 (61.4)](https://drive.google.com/file/d/1eumrtiXT4NOspjyEV4j8cHmlOaaCGk5l/view?usp=sharing) | [61.4 (29.5)](https://drive.google.com/file/d/1sSwXSUlj4_tHZequ_iZ8w_Jh0VaRQMqF/view?usp=sharing) | ## Cross-domain ReID #### Market1501 -> DukeMTMC-reID | Model | # Param (10^6) | GFLOPs | Loss | Input | Transforms | Distance | Rank-1 | Rank-5 | Rank-10 | mAP | Download | | :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | osnet_ibn_x1_0 | 2.2 | 0.98 | softmax | (256, 128) | `random_flip`, `color_jitter` | `euclidean` | 48.5 | 62.3 | 67.4 | 26.7 | [model](https://drive.google.com/file/d/1uWW7_z_IcUmRNPqQOrEBdsvic94fWH37/view?usp=sharing) | | osnet_ain_x1_0 | 2.2 | 0.98 | softmax | (256, 128) | `random_flip`, `color_jitter` | `cosine` | 52.4 | 66.1 | 71.2 | 30.5 | [model](https://drive.google.com/file/d/14bNFGm0FhwHEkEpYKqKiDWjLNhXywFAd/view?usp=sharing) | #### DukeMTMC-reID -> Market1501 | Model | # Param (10^6) | GFLOPs | Loss | Input | Transforms | Distance | Rank-1 | Rank-5 | Rank-10 | mAP | Download | | :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | osnet_ibn_x1_0 | 2.2 | 0.98 | softmax | (256, 128) | `random_flip`, `color_jitter` | `euclidean` | 57.7 | 73.7 | 80.0 | 26.1 | [model](https://drive.google.com/file/d/1CNxL1IP0BjcE1TSttiVOID1VNipAjiF3/view?usp=sharing) | | osnet_ain_x1_0 | 2.2 | 0.98 | softmax | (256, 128) | `random_flip`, `color_jitter` | `cosine` | 61.0 | 77.0 | 82.5 | 30.6 | [model](https://drive.google.com/file/d/1hypJvq8G04SOby6jvF337GEkg5K_bmCw/view?usp=sharing) | #### MSMT17 (`combineall=True`) -> Market1501 & DukeMTMC-reID | Model | # Param (10^6) | GFLOPs | Loss | Input | Transforms | Distance | msmt17 -> market1501 | msmt17 -> dukemtmcreid | Download | | :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | resnet50 | 23.5 | 2.7 | softmax | (256, 128) | `random_flip`, `color_jitter` | `euclidean` | 46.3 (22.8) | 52.3 (32.1) | [model](https://drive.google.com/file/d/1yiBteqgIZoOeywE8AhGmEQl7FTVwrQmf/view?usp=sharing) | | osnet_x1_0 | 2.2 | 0.98 | softmax | (256, 128) | `random_flip`, `color_jitter` | `euclidean` | 66.6 (37.5) | 66.0 (45.3) | [model](https://drive.google.com/file/d/1IosIFlLiulGIjwW3H8uMRmx3MzPwf86x/view?usp=sharing) | | osnet_x0_75 | 1.3 | 0.57 | softmax | (256, 128) | `random_flip`, `color_jitter` | `euclidean` | 63.6 (35.5) | 65.3 (44.5) | [model](https://drive.google.com/file/d/1fhjSS_7SUGCioIf2SWXaRGPqIY9j7-uw/view?usp=sharing) | | osnet_x0_5 | 0.6 | 0.27 | softmax | (256, 128) | `random_flip`, `color_jitter` | `euclidean` | 64.3 (34.9) | 65.2 (43.3) | [model](https://drive.google.com/file/d/1DHgmb6XV4fwG3n-CnCM0zdL9nMsZ9_RF/view?usp=sharing) | | osnet_x0_25 | 0.2 | 0.08 | softmax | (256, 128) | `random_flip`, `color_jitter` | `euclidean` | 59.9 (31.0) | 61.5 (39.6) | [model](https://drive.google.com/file/d/1Kkx2zW89jq_NETu4u42CFZTMVD5Hwm6e/view?usp=sharing) | | osnet_ibn_x1_0 | 2.2 | 0.98 | softmax | (256, 128) | `random_flip`, `color_jitter` | `euclidean` | 66.5 (37.2) | 67.4 (45.6) | [model](https://drive.google.com/file/d/1q3Sj2ii34NlfxA4LvmHdWO_75NDRmECJ/view?usp=sharing) | | osnet_ain_x1_0 | 2.2 | 0.98 | softmax | (256, 128) | `random_flip`, `color_jitter` | `cosine` | 70.1 (43.3) | 71.1 (52.7) | [model](https://drive.google.com/file/d/1SigwBE6mPdqiJMqhuIY4aqC7--5CsMal/view?usp=sharing) | #### Multi-source domain generalization The models below are trained using multiple source datasets, as described in [Zhou et al. TPAMI'21](https://arxiv.org/abs/1910.06827). Regarding the abbreviations, MS is MSMT17; M is Market1501; D is DukeMTMC-reID; and C is CUHK03. All models were trained with [im_osnet_ain_x1_0_softmax_256x128_amsgrad_cosine.yaml](https://github.com/KaiyangZhou/deep-person-reid/blob/master/configs/im_osnet_ain_x1_0_softmax_256x128_amsgrad_cosine.yaml) and `max_epoch=50`. | Model | # Param (10^6) | GFLOPs | Loss | Input | Transforms | Distance | MS+D+C->M | MS+M+C->D | MS+D+M->C |D+M+C->MS | | :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | osnet_x1_0 | 2.2 | 0.98 | softmax | (256, 128) | `random_flip`, `color_jitter` | `cosine` | [72.5 (44.2)](https://drive.google.com/file/d/1tuYY1vQXReEd8N8_npUkc7npPDDmjNCV/view?usp=sharing) | [65.2 (47.0)](https://drive.google.com/file/d/1UxUI4NsE108UCvcy3O1Ufe73nIVPKCiu/view?usp=sharing) | [23.9 (23.3)](https://drive.google.com/file/d/1kAA6qHJvbaJtyh1b39ZyEqWROwUgWIhl/view?usp=sharing) | [33.2 (12.6)](https://drive.google.com/file/d/1wAHuYVTzj8suOwqCNcEmu6YdbVnHDvA2/view?usp=sharing) | | osnet_ibn_x1_0 | 2.2 | 0.98 | softmax | (256, 128) | `random_flip`, `color_jitter` | `cosine` | [73.0 (44.9)](https://drive.google.com/file/d/14sH6yZwuNHPTElVoEZ26zozOOZIej5Mf/view?usp=sharing) | [64.6 (45.7)](https://drive.google.com/file/d/1Sk-2SSwKAF8n1Z4p_Lm_pl0E6v2WlIBn/view?usp=sharing) | [25.7 (25.4)](https://drive.google.com/file/d/1actHP7byqWcK4eBE1ojnspSMdo7k2W4G/view?usp=sharing) | [39.8 (16.2)](https://drive.google.com/file/d/1BGOSdLdZgqHe2qFafatb-5sPY40JlYfp/view?usp=sharing) | | osnet_ain_x1_0 | 2.2 | 0.98 | softmax | (256, 128) | `random_flip`, `color_jitter` | `cosine` | [73.3 (45.8)](https://drive.google.com/file/d/1nIrszJVYSHf3Ej8-j6DTFdWz8EnO42PB/view?usp=sharing) | [65.6 (47.2)](https://drive.google.com/file/d/1YjJ1ZprCmaKG6MH2P9nScB9FL_Utf9t1/view?usp=sharing) | [27.4 (27.1)](https://drive.google.com/file/d/1IxIg5P0cei3KPOJQ9ZRWDE_Mdrz01ha2/view?usp=sharing) | [40.2 (16.2)](https://drive.google.com/file/d/1KcoUKzLmsUoGHI7B6as_Z2fXL50gzexS/view?usp=sharing) | ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/Makefile ================================================ # Minimal makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build SOURCEDIR = . BUILDDIR = _build # Put it first so that "make" without argument is like "make help". help: @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) .PHONY: help Makefile # Catch-all target: route all unknown targets to Sphinx using the new # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). %: Makefile @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/conf.py ================================================ # -*- coding: utf-8 -*- # # Configuration file for the Sphinx documentation builder. # # This file does only contain a selection of the most common options. For a # full list see the documentation: # http://www.sphinx-doc.org/en/master/config # -- Path setup -------------------------------------------------------------- # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # import os import sys sys.path.insert(0, os.path.abspath('..')) # -- Project information ----------------------------------------------------- project = u'torchreid' copyright = u'2019, Kaiyang Zhou' author = u'Kaiyang Zhou' version_file = '../torchreid/__init__.py' with open(version_file, 'r') as f: exec(compile(f.read(), version_file, 'exec')) __version__ = locals()['__version__'] # The short X.Y version version = __version__ # The full version, including alpha/beta/rc tags release = __version__ # -- General configuration --------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. # # needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'sphinx.ext.autodoc', 'sphinxcontrib.napoleon', 'sphinx.ext.viewcode', 'sphinx.ext.githubpages', 'sphinx_markdown_tables', ] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # source_suffix = ['.rst', '.md'] # source_suffix = '.rst' source_parsers = {'.md': 'recommonmark.parser.CommonMarkParser'} # The master toctree document. master_doc = 'index' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. language = None # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This pattern also affects html_static_path and html_extra_path. exclude_patterns = [u'_build', 'Thumbs.db', '.DS_Store'] # The name of the Pygments (syntax highlighting) style to use. pygments_style = None # -- Options for HTML output ------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. # # html_theme_options = {} # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # Custom sidebar templates, must be a dictionary that maps document names # to template names. # # The default sidebars (for documents that don't match any pattern) are # defined by theme itself. Builtin themes are using these templates by # default: ``['localtoc.html', 'relations.html', 'sourcelink.html', # 'searchbox.html']``. # # html_sidebars = {} # -- Options for HTMLHelp output --------------------------------------------- # Output file base name for HTML help builder. htmlhelp_basename = 'torchreiddoc' # -- Options for LaTeX output ------------------------------------------------ latex_elements = { # The paper size ('letterpaper' or 'a4paper'). # # 'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). # # 'pointsize': '10pt', # Additional stuff for the LaTeX preamble. # # 'preamble': '', # Latex figure (float) alignment # # 'figure_align': 'htbp', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ ( master_doc, 'torchreid.tex', u'torchreid Documentation', u'Kaiyang Zhou', 'manual' ), ] # -- Options for manual page output ------------------------------------------ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ (master_doc, 'torchreid', u'torchreid Documentation', [author], 1) ] # -- Options for Texinfo output ---------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ ( master_doc, 'torchreid', u'torchreid Documentation', author, 'torchreid', 'One line description of project.', 'Miscellaneous' ), ] # -- Options for Epub output ------------------------------------------------- # Bibliographic Dublin Core info. epub_title = project # The unique identifier of the text. This can be a ISBN number # or the project homepage. # # epub_identifier = '' # A unique identification for the text. # # epub_uid = '' # A list of files that should not be packed into the epub file. epub_exclude_files = ['search.html'] # -- Extension configuration ------------------------------------------------- ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/datasets.rst ================================================ .. _datasets: Datasets ========= Here we provide a comprehensive guide on how to prepare the datasets. Suppose you want to store the reid data in a directory called "path/to/reid-data/", you need to specify the ``root`` as *root='path/to/reid-data/'* when initializing ``DataManager``. Below we use ``$REID`` to denote "path/to/reid-data". Please refer to :ref:`torchreid_data` for details regarding the arguments. .. note:: Dataset with a :math:`\dagger` symbol means that the process is automated, so you can directly call the dataset in ``DataManager`` (which automatically downloads the dataset and organizes the data structure). However, we also provide a way below to help the manual setup in case the automation fails. .. note:: The keys to use specific datasets are enclosed in the parantheses beside the datasets' names. .. note:: You are suggested to use the provided names for dataset folders such as "market1501" for Market1501 and "dukemtmcreid" for DukeMTMC-reID when doing the manual setup, otherwise you need to modify the source code accordingly (i.e. the ``dataset_dir`` attribute). .. note:: Some download links provided by the original authors might not work. You can email `Kaiyang Zhou `_ to reqeust new links. Please do provide your full name, institution, and purpose of using the data in the email (best use your work email address). .. contents:: :local: Image Datasets -------------- Market1501 :math:`^\dagger` (``market1501``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Create a directory named "market1501" under ``$REID``. - Download the dataset to "market1501" from http://www.liangzheng.org/Project/project_reid.html and extract the files. - The data structure should look like .. code-block:: none market1501/ Market-1501-v15.09.15/ query/ bounding_box_train/ bounding_box_test/ - To use the extra 500K distractors (i.e. Market1501 + 500K), go to the **Market-1501+500k Dataset** section at http://www.liangzheng.org/Project/project_reid.html, download the zip file "distractors_500k.zip" and extract it under "market1501/Market-1501-v15.09.15". The argument to use these 500K distrctors is ``market1501_500k`` in ``ImageDataManager``. CUHK03 (``cuhk03``) ^^^^^^^^^^^^^^^^^^^^^ - Create a folder named "cuhk03" under ``$REID``. - Download the dataset to "cuhk03/" from http://www.ee.cuhk.edu.hk/~xgwang/CUHK_identification.html and extract "cuhk03_release.zip", resulting in "cuhk03/cuhk03_release/". - Download the new split (767/700) from `person-re-ranking `_. What you need are "cuhk03_new_protocol_config_detected.mat" and "cuhk03_new_protocol_config_labeled.mat". Put these two mat files under "cuhk03/". - The data structure should look like .. code-block:: none cuhk03/ cuhk03_release/ cuhk03_new_protocol_config_detected.mat cuhk03_new_protocol_config_labeled.mat - In the default mode, we load data using the new split (767/700). If you wanna use the original (20) splits (1367/100), please set ``cuhk03_classic_split`` to True in ``ImageDataManager``. As the CMC is computed differently from Market1501 for the 1367/100 split (see `here `_), you need to enable ``use_metric_cuhk03`` in ``ImageDataManager`` to activate the *single-gallery-shot* metric for fair comparison with some methods that adopt the old splits (*do not need to report mAP*). In addition, we support both *labeled* and *detected* modes. The default mode loads *detected* images. Enable ``cuhk03_labeled`` in ``ImageDataManager`` if you wanna train and test on *labeled* images. .. note:: The code will extract images in "cuhk-03.mat" and save them under "cuhk03/images_detected" and "cuhk03/images_labeled". Also, four json files will be automatically generated, i.e. "splits_classic_detected.json", "splits_classic_labeled.json", "splits_new_detected.json" and "splits_new_labeled.json". If the parent path of ``$REID`` is changed, these json files should be manually deleted. The code can automatically generate new json files to match the new path. DukeMTMC-reID :math:`^\dagger` (``dukemtmcreid``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Create a directory called "dukemtmc-reid" under ``$REID``. - Download "DukeMTMC-reID" from http://vision.cs.duke.edu/DukeMTMC/ and extract it under "dukemtmc-reid". - The data structure should look like .. code-block:: none dukemtmc-reid/ DukeMTMC-reID/ query/ bounding_box_train/ bounding_box_test/ ... MSMT17 (``msmt17``) ^^^^^^^^^^^^^^^^^^^^^ - Create a directory called "msmt17" under ``$REID``. - Download the dataset from http://www.pkuvmc.com/publications/msmt17.html to "msmt17" and extract the files. - The data structure should look like .. code-block:: none msmt17/ MSMT17_V1/ # or MSMT17_V2 train/ test/ list_train.txt list_query.txt list_gallery.txt list_val.txt VIPeR :math:`^\dagger` (``viper``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - The download link is http://users.soe.ucsc.edu/~manduchi/VIPeR.v1.0.zip. - Organize the dataset in a folder named "viper" as follows .. code-block:: none viper/ VIPeR/ cam_a/ cam_b/ GRID :math:`^\dagger` (``grid``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - The download link is http://personal.ie.cuhk.edu.hk/~ccloy/files/datasets/underground_reid.zip. - Organize the dataset in a folder named "grid" as follows .. code-block:: none grid/ underground_reid/ probe/ gallery/ ... CUHK01 (``cuhk01``) ^^^^^^^^^^^^^^^^^^^^^^^^ - Create a folder named "cuhk01" under ``$REID``. - Download "CUHK01.zip" from http://www.ee.cuhk.edu.hk/~xgwang/CUHK_identification.html and place it under "cuhk01/". - The code can automatically extract the files, or you can do it yourself. - The data structure should look like .. code-block:: none cuhk01/ campus/ SenseReID (``sensereid``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Create "sensereid" under ``$REID``. - Download the dataset from this `link `_ and extract it to "sensereid". - Organize the data to be like .. code-block:: none sensereid/ SenseReID/ test_probe/ test_gallery/ QMUL-iLIDS :math:`^\dagger` (``ilids``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Create a folder named "ilids" under ``$REID``. - Download the dataset from http://www.eecs.qmul.ac.uk/~jason/data/i-LIDS_Pedestrian.tgz and organize it to look like .. code-block:: none ilids/ i-LIDS_Pedestrian/ Persons/ PRID (``prid``) ^^^^^^^^^^^^^^^^^^^ - Create a directory named "prid2011" under ``$REID``. - Download the dataset from https://www.tugraz.at/institute/icg/research/team-bischof/lrs/downloads/PRID11/ and extract it under "prid2011". - The data structure should end up with .. code-block:: none prid2011/ prid_2011/ single_shot/ multi_shot/ CUHK02 (``cuhk02``) ^^^^^^^^^^^^^^^^^^^^^ - Create a folder named "cuhk02" under ``$REID``. - Download the data from http://www.ee.cuhk.edu.hk/~xgwang/CUHK_identification.html and put it under "cuhk02/". - Extract the file so the data structure looks like .. code-block:: none cuhk02/ Dataset/ P1/ P2/ P3/ P4/ P5/ CUHKSYSU (``cuhksysu``) ^^^^^^^^^^^^^^^^^^^^^^^^^^ - Create a folder named "cuhksysu" under ``$REID``. - Download the data to "cuhksysu/" from this `google drive link `_. - Extract the zip file under "cuhksysu/". - The data structure should look like .. code-block:: none cuhksysu/ cropped_images Video Datasets -------------- MARS (``mars``) ^^^^^^^^^^^^^^^^^ - Create "mars/" under ``$REID``. - Download the dataset from http://www.liangzheng.com.cn/Project/project_mars.html and place it in "mars/". - Extract "bbox_train.zip" and "bbox_test.zip". - Download the split metadata from https://github.com/liangzheng06/MARS-evaluation/tree/master/info and put "info/" in "mars/". - The data structure should end up with .. code-block:: none mars/ bbox_test/ bbox_train/ info/ iLIDS-VID :math:`^\dagger` (``ilidsvid``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Create "ilids-vid" under ``$REID``. - Download the dataset from https://xiatian-zhu.github.io/downloads_qmul_iLIDS-VID_ReID_dataset.html to "ilids-vid". - Organize the data structure to match .. code-block:: none ilids-vid/ i-LIDS-VID/ train-test people splits/ PRID2011 (``prid2011``) ^^^^^^^^^^^^^^^^^^^^^^^^^ - Create a directory named "prid2011" under ``$REID``. - Download the dataset from https://www.tugraz.at/institute/icg/research/team-bischof/lrs/downloads/PRID11/ and extract it under "prid2011". - Download the split created by *iLIDS-VID* from `this google drive `_ and put it under "prid2011/". Following the standard protocol, only 178 persons whose sequences are more than a threshold are used. - The data structure should end up with .. code-block:: none prid2011/ splits_prid2011.json prid_2011/ single_shot/ multi_shot/ DukeMTMC-VideoReID :math:`^\dagger` (``dukemtmcvidreid``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Create "dukemtmc-vidreid" under ``$REID``. - Download "DukeMTMC-VideoReID" from http://vision.cs.duke.edu/DukeMTMC/ and unzip the file to "dukemtmc-vidreid/". - The data structure should look like .. code-block:: none dukemtmc-vidreid/ DukeMTMC-VideoReID/ train/ query/ gallery/ ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/evaluation.rst ================================================ Evaluation ========== Image ReID ----------- - **Market1501**, **DukeMTMC-reID**, **CUHK03 (767/700 split)** and **MSMT17** have fixed split so keeping ``split_id=0`` is fine. - **CUHK03 (classic split)** has 20 fixed splits, so do ``split_id=0~19``. - **VIPeR** contains 632 identities each with 2 images under two camera views. Evaluation should be done for 10 random splits. Each split randomly divides 632 identities to 316 train ids (632 images) and the other 316 test ids (632 images). Note that, in each random split, there are two sub-splits, one using camera-A as query and camera-B as gallery while the other one using camera-B as query and camera-A as gallery. Thus, there are totally 20 splits generated with ``split_id`` starting from 0 to 19. Models can be trained on ``split_id=[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]`` (because ``split_id=0`` and ``split_id=1`` share the same train set, and so on and so forth.). At test time, models trained on ``split_id=0`` can be directly evaluated on ``split_id=1``, models trained on ``split_id=2`` can be directly evaluated on ``split_id=3``, and so on and so forth. - **CUHK01** is similar to VIPeR in the split generation. - **GRID** , **iLIDS** and **PRID** have 10 random splits, so evaluation should be done by varying ``split_id`` from 0 to 9. - **SenseReID** has no training images and is used for evaluation only. .. note:: The ``split_id`` argument is defined in ``ImageDataManager`` and ``VideoDataManager``. Please refer to :ref:`torchreid_data`. Video ReID ----------- - **MARS** and **DukeMTMC-VideoReID** have fixed single split so using ``split_id=0`` is ok. - **iLIDS-VID** and **PRID2011** have 10 predefined splits so evaluation should be done by varying ``split_id`` from 0 to 9. ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/index.rst ================================================ .. include:: ../README.rst .. toctree:: :hidden: user_guide datasets evaluation .. toctree:: :caption: Package Reference :hidden: pkg/data pkg/engine pkg/losses pkg/metrics pkg/models pkg/optim pkg/utils .. toctree:: :caption: Resources :hidden: AWESOME_REID.md MODEL_ZOO.md Indices and tables ================== * :ref:`genindex` * :ref:`modindex` ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/pkg/data.rst ================================================ .. _torchreid_data: torchreid.data ============== Data Manager --------------------------- .. automodule:: torchreid.data.datamanager :members: Sampler ----------------------- .. automodule:: torchreid.data.sampler :members: Transforms --------------------------- .. automodule:: torchreid.data.transforms :members: Dataset --------------------------- .. automodule:: torchreid.data.datasets.dataset :members: .. automodule:: torchreid.data.datasets.__init__ :members: Image Datasets ------------------------------ .. automodule:: torchreid.data.datasets.image.market1501 :members: .. automodule:: torchreid.data.datasets.image.cuhk03 :members: .. automodule:: torchreid.data.datasets.image.dukemtmcreid :members: .. automodule:: torchreid.data.datasets.image.msmt17 :members: .. automodule:: torchreid.data.datasets.image.viper :members: .. automodule:: torchreid.data.datasets.image.grid :members: .. automodule:: torchreid.data.datasets.image.cuhk01 :members: .. automodule:: torchreid.data.datasets.image.ilids :members: .. automodule:: torchreid.data.datasets.image.sensereid :members: .. automodule:: torchreid.data.datasets.image.prid :members: Video Datasets ------------------------------ .. automodule:: torchreid.data.datasets.video.mars :members: .. automodule:: torchreid.data.datasets.video.ilidsvid :members: .. automodule:: torchreid.data.datasets.video.prid2011 :members: .. automodule:: torchreid.data.datasets.video.dukemtmcvidreid :members: ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/pkg/engine.rst ================================================ .. _torchreid_engine: torchreid.engine ================== Base Engine ------------ .. autoclass:: torchreid.engine.engine.Engine :members: Image Engines ------------- .. autoclass:: torchreid.engine.image.softmax.ImageSoftmaxEngine :members: .. autoclass:: torchreid.engine.image.triplet.ImageTripletEngine :members: Video Engines ------------- .. autoclass:: torchreid.engine.video.softmax.VideoSoftmaxEngine .. autoclass:: torchreid.engine.video.triplet.VideoTripletEngine ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/pkg/losses.rst ================================================ .. _torchreid_losses: torchreid.losses ================= Softmax -------- .. automodule:: torchreid.losses.cross_entropy_loss :members: Triplet ------- .. automodule:: torchreid.losses.hard_mine_triplet_loss :members: ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/pkg/metrics.rst ================================================ .. _torchreid_metrics: torchreid.metrics ================= Distance --------- .. automodule:: torchreid.metrics.distance :members: Accuracy -------- .. automodule:: torchreid.metrics.accuracy :members: Rank ----- .. automodule:: torchreid.metrics.rank :members: evaluate_rank ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/pkg/models.rst ================================================ .. _torchreid_models: torchreid.models ================= Interface --------- .. automodule:: torchreid.models.__init__ :members: ImageNet Classification Models ------------------------------- .. autoclass:: torchreid.models.resnet.ResNet .. autoclass:: torchreid.models.senet.SENet .. autoclass:: torchreid.models.densenet.DenseNet .. autoclass:: torchreid.models.inceptionresnetv2.InceptionResNetV2 .. autoclass:: torchreid.models.inceptionv4.InceptionV4 .. autoclass:: torchreid.models.xception.Xception Lightweight Models ------------------ .. autoclass:: torchreid.models.nasnet.NASNetAMobile .. autoclass:: torchreid.models.mobilenetv2.MobileNetV2 .. autoclass:: torchreid.models.shufflenet.ShuffleNet .. autoclass:: torchreid.models.squeezenet.SqueezeNet .. autoclass:: torchreid.models.shufflenetv2.ShuffleNetV2 ReID-specific Models -------------------- .. autoclass:: torchreid.models.mudeep.MuDeep .. autoclass:: torchreid.models.resnetmid.ResNetMid .. autoclass:: torchreid.models.hacnn.HACNN .. autoclass:: torchreid.models.pcb.PCB .. autoclass:: torchreid.models.mlfn.MLFN .. autoclass:: torchreid.models.osnet.OSNet .. autoclass:: torchreid.models.osnet_ain.OSNet ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/pkg/optim.rst ================================================ .. _torchreid_optim: torchreid.optim ================= Optimizer ---------- .. automodule:: torchreid.optim.optimizer :members: build_optimizer LR Scheduler ------------- .. automodule:: torchreid.optim.lr_scheduler :members: build_lr_scheduler ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/pkg/utils.rst ================================================ .. _torchreid_utils: torchreid.utils ================= Average Meter -------------- .. automodule:: torchreid.utils.avgmeter :members: Loggers ------- .. automodule:: torchreid.utils.loggers :members: Generic Tools --------------- .. automodule:: torchreid.utils.tools :members: ReID Tools ---------- .. automodule:: torchreid.utils.reidtools :members: Torch Tools ------------ .. automodule:: torchreid.utils.torchtools :members: .. automodule:: torchreid.utils.model_complexity :members: ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/docs/user_guide.rst ================================================ How-to ============ .. contents:: :local: Prepare datasets ----------------- See :ref:`datasets`. Find model keys ----------------- Keys are listed under the *Public keys* section within each model class in :ref:`torchreid_models`. Show available models ---------------------- .. code-block:: python import torchreid torchreid.models.show_avai_models() Change the training sampler ----------------------------- The default ``train_sampler`` is "RandomSampler". You can give the specific sampler name as input to ``train_sampler``, e.g. ``train_sampler='RandomIdentitySampler'`` for triplet loss. Choose an optimizer/lr_scheduler ---------------------------------- Please refer to the source code of ``build_optimizer``/``build_lr_scheduler`` in :ref:`torchreid_optim` for details. Resume training ---------------- Suppose the checkpoint is saved in "log/resnet50/model.pth.tar-30", you can do .. code-block:: python start_epoch = torchreid.utils.resume_from_checkpoint( 'log/resnet50/model.pth.tar-30', model, optimizer ) engine.run( save_dir='log/resnet50', max_epoch=60, start_epoch=start_epoch ) Compute model complexity -------------------------- We provide a tool in ``torchreid.utils.model_complexity.py`` to automatically compute the model complexity, i.e. number of parameters and FLOPs. .. code-block:: python from torchreid import models, utils model = models.build_model(name='resnet50', num_classes=1000) num_params, flops = utils.compute_model_complexity(model, (1, 3, 256, 128)) # show detailed complexity for each module utils.compute_model_complexity(model, (1, 3, 256, 128), verbose=True) # count flops for all layers including ReLU and BatchNorm utils.compute_model_complexity(model, (1, 3, 256, 128), verbose=True, only_conv_linear=False) Note that (1) this function only provides an estimate of the theoretical time complexity rather than the actual running time which depends on implementations and hardware; (2) the FLOPs is only counted for layers that are used at test time. This means that redundant layers such as person ID classification layer will be ignored. The inference graph depends on how you define the computations in ``forward()``. Combine multiple datasets --------------------------- Easy. Just give whatever datasets (keys) you want to the ``sources`` argument when instantiating a data manager. For example, .. code-block:: python datamanager = torchreid.data.ImageDataManager( root='reid-data', sources=['market1501', 'dukemtmcreid', 'cuhk03', 'msmt17'], height=256, width=128, batch_size=32 ) In this example, the target datasets are Market1501, DukeMTMC-reID, CUHK03 and MSMT17 as the ``targets`` argument is not specified. Please refer to ``Engine.test()`` in :ref:`torchreid_engine` for details regarding how evaluation is performed. Do cross-dataset evaluation ----------------------------- Easy. Just give whatever datasets (keys) you want to the argument ``targets``, like .. code-block:: python datamanager = torchreid.data.ImageDataManager( root='reid-data', sources='market1501', targets='dukemtmcreid', # or targets='cuhk03' or targets=['dukemtmcreid', 'cuhk03'] height=256, width=128, batch_size=32 ) Combine train, query and gallery --------------------------------- This can be easily done by setting ``combineall=True`` when instantiating a data manager. Below is an example of using Market1501, .. code-block:: python datamanager = torchreid.data.ImageDataManager( root='reid-data', sources='market1501', height=256, width=128, batch_size=32, market1501_500k=False, combineall=True # it's me, here ) More specifically, with ``combineall=False``, you will get .. code-block:: none => Loaded Market1501 ---------------------------------------- subset | # ids | # images | # cameras ---------------------------------------- train | 751 | 12936 | 6 query | 750 | 3368 | 6 gallery | 751 | 15913 | 6 --------------------------------------- with ``combineall=True``, you will get .. code-block:: none => Loaded Market1501 ---------------------------------------- subset | # ids | # images | # cameras ---------------------------------------- train | 1501 | 29419 | 6 query | 750 | 3368 | 6 gallery | 751 | 15913 | 6 --------------------------------------- Optimize layers with different learning rates ----------------------------------------------- A common practice for fine-tuning pretrained models is to use a smaller learning rate for base layers and a large learning rate for randomly initialized layers (referred to as ``new_layers``). ``torchreid.optim.optimizer`` has implemented such feature. What you need to do is to set ``staged_lr=True`` and give the names of ``new_layers`` such as "classifier". Below is an example of setting different learning rates for base layers and new layers in ResNet50, .. code-block:: python # New layer "classifier" has a learning rate of 0.01 # The base layers have a learning rate of 0.001 optimizer = torchreid.optim.build_optimizer( model, optim='sgd', lr=0.01, staged_lr=True, new_layers='classifier', base_lr_mult=0.1 ) Please refer to :ref:`torchreid_optim` for more details. Do two-stepped transfer learning ------------------------------------- To prevent the pretrained layers from being damaged by harmful gradients back-propagated from randomly initialized layers, one can adopt the *two-stepped transfer learning strategy* presented in `Deep Transfer Learning for Person Re-identification `_. The basic idea is to pretrain the randomly initialized layers for few epochs while keeping the base layers frozen before training all layers end-to-end. This has been implemented in ``Engine.train()`` (see :ref:`torchreid_engine`). The arguments related to this feature are ``fixbase_epoch`` and ``open_layers``. Intuitively, ``fixbase_epoch`` denotes the number of epochs to keep the base layers frozen; ``open_layers`` means which layer is open for training. For example, say you want to pretrain the classification layer named "classifier" in ResNet50 for 5 epochs before training all layers, you can do .. code-block:: python engine.run( save_dir='log/resnet50', max_epoch=60, eval_freq=10, print_freq=10, test_only=False, fixbase_epoch=5, open_layers='classifier' ) # or open_layers=['fc', 'classifier'] if there is another fc layer that # is randomly initialized, like resnet50_fc512 Note that ``fixbase_epoch`` is counted into ``max_epoch``. In the above example, the base network will be fixed for 5 epochs and then open for training for 55 epochs. Thus, if you want to freeze some layers throughout the training, what you can do is to set ``fixbase_epoch`` equal to ``max_epoch`` and put the layer names in ``open_layers`` which you want to train. Test a trained model ---------------------- You can load a trained model using :code:`torchreid.utils.load_pretrained_weights(model, weight_path)` and set ``test_only=True`` in ``engine.run()``. Fine-tune a model pre-trained on reid datasets ----------------------------------------------- Use :code:`torchreid.utils.load_pretrained_weights(model, weight_path)` to load the pre-trained weights and then fine-tune on the dataset you want. Visualize learning curves with tensorboard -------------------------------------------- The ``SummaryWriter()`` for tensorboard will be automatically initialized in ``engine.run()`` when you are training your model. Therefore, you do not need to do extra jobs. After the training is done, the ``*tf.events*`` file will be saved in ``save_dir``. Then, you just call ``tensorboard --logdir=your_save_dir`` in your terminal and visit ``http://localhost:6006/`` in a web browser. See `pytorch tensorboard `_ for further information. Visualize ranking results --------------------------- This can be achieved by setting ``visrank`` to true in ``engine.run()``. ``visrank_topk`` determines the top-k images to be visualized (Default is ``visrank_topk=10``). Note that ``visrank`` can only be used in test mode, i.e. ``test_only=True`` in ``engine.run()``. The output will be saved under ``save_dir/visrank_DATASETNAME`` where each plot contains the top-k similar gallery images given a query. An example is shown below where red and green denote incorrect and correct matches respectively. .. image:: figures/ranking_results.jpg :width: 800px :align: center Visualize activation maps -------------------------- To understand where the CNN focuses on to extract features for ReID, you can visualize the activation maps as in `OSNet `_. This is implemented in ``tools/visualize_actmap.py`` (check the code for more details). An example running command is .. code-block:: shell python tools/visualize_actmap.py \ --root $DATA/reid \ -d market1501 \ -m osnet_x1_0 \ --weights PATH_TO_PRETRAINED_WEIGHTS \ --save-dir log/visactmap_osnet_x1_0_market1501 The output will look like (from left to right: image, activation map, overlapped image) .. image:: figures/actmap.jpg :width: 300px :align: center .. note:: In order to visualize activation maps, the CNN needs to output the last convolutional feature maps at eval mode. See ``torchreid/models/osnet.py`` for example. Use your own dataset ---------------------- 1. Write your own dataset class. Below is a template for image dataset. However, it can also be applied to a video dataset class, for which you simply change ``ImageDataset`` to ``VideoDataset``. .. code-block:: python from __future__ import absolute_import from __future__ import print_function from __future__ import division import sys import os import os.path as osp from torchreid.data import ImageDataset class NewDataset(ImageDataset): dataset_dir = 'new_dataset' def __init__(self, root='', **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) # All you need to do here is to generate three lists, # which are train, query and gallery. # Each list contains tuples of (img_path, pid, camid), # where # - img_path (str): absolute path to an image. # - pid (int): person ID, e.g. 0, 1. # - camid (int): camera ID, e.g. 0, 1. # Note that # - pid and camid should be 0-based. # - query and gallery should share the same pid scope (e.g. # pid=0 in query refers to the same person as pid=0 in gallery). # - train, query and gallery share the same camid scope (e.g. # camid=0 in train refers to the same camera as camid=0 # in query/gallery). train = ... query = ... gallery = ... super(NewDataset, self).__init__(train, query, gallery, **kwargs) 2. Register your dataset. .. code-block:: python import torchreid torchreid.data.register_image_dataset('new_dataset', NewDataset) 3. Initialize a data manager with your dataset. .. code-block:: python # use your own dataset only datamanager = torchreid.data.ImageDataManager( root='reid-data', sources='new_dataset' ) # combine with other datasets datamanager = torchreid.data.ImageDataManager( root='reid-data', sources=['new_dataset', 'dukemtmcreid'] ) # cross-dataset evaluation datamanager = torchreid.data.ImageDataManager( root='reid-data', sources=['new_dataset', 'dukemtmcreid'], targets='market1501' # or targets=['market1501', 'cuhk03'] ) Design your own Engine ------------------------ A new Engine should be designed if you have your own loss function. The base Engine class ``torchreid.engine.Engine`` has implemented some generic methods which you can inherit to avoid re-writing. Please refer to the source code for more details. You are suggested to see how ``ImageSoftmaxEngine`` and ``ImageTripletEngine`` are constructed (also ``VideoSoftmaxEngine`` and ``VideoTripletEngine``). All you need to implement might be just a ``forward_backward()`` function. Use Torchreid as a feature extractor in your projects ------------------------------------------------------- We have provided a simple API for feature extraction, which accepts input of various types such as a list of image paths or numpy arrays. More details can be found in the code at ``torchreid/utils/feature_extractor.py``. Here we show a simple example of how to extract features given a list of image paths. .. code-block:: python from torchreid.utils import FeatureExtractor extractor = FeatureExtractor( model_name='osnet_x1_0', model_path='a/b/c/model.pth.tar', device='cuda' ) image_list = [ 'a/b/c/image001.jpg', 'a/b/c/image002.jpg', 'a/b/c/image003.jpg', 'a/b/c/image004.jpg', 'a/b/c/image005.jpg' ] features = extractor(image_list) print(features.shape) # output (5, 512) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/linter.sh ================================================ echo "Running isort" isort -y -sp . echo "Done" echo "Running yapf" yapf -i -r -vv -e build . echo "Done" echo "Running flake8" flake8 . echo "Done" ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/DML/README.md ================================================ # Deep mutual learning This repo implements [Deep Mutual Learning (CVPR'18)](https://zpascal.net/cvpr2018/Zhang_Deep_Mutual_Learning_CVPR_2018_paper.pdf) (DML) for person re-id. We used this code in our [OSNet](https://arxiv.org/pdf/1905.00953.pdf) paper (see Supp. B). The training command to reproduce the result of "triplet + DML" (Table 12f in the paper) is ```bash python main.py \ --config-file im_osnet_x1_0_dml_256x128_amsgrad_cosine.yaml \ --root $DATA ``` `$DATA` corresponds to the path to your dataset folder. Change `model.deploy` to `both` if you wanna enable model ensembling. If you have any questions, please raise an issue in the Issues area. ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/DML/default_config.py ================================================ from yacs.config import CfgNode as CN def get_default_config(): cfg = CN() # model cfg.model = CN() cfg.model.name = 'resnet50' cfg.model.pretrained = True # automatically load pretrained model weights if available cfg.model.load_weights1 = '' # path to model-1 weights cfg.model.load_weights2 = '' # path to model-2 weights cfg.model.resume1 = '' # path to checkpoint for resume training cfg.model.resume2 = '' # path to checkpoint for resume training cfg.model.deploy = 'model1' # model1, model2 or both # data cfg.data = CN() cfg.data.type = 'image' cfg.data.root = 'reid-data' cfg.data.sources = ['market1501'] cfg.data.targets = ['market1501'] cfg.data.workers = 4 # number of data loading workers cfg.data.split_id = 0 # split index cfg.data.height = 256 # image height cfg.data.width = 128 # image width cfg.data.combineall = False # combine train, query and gallery for training cfg.data.transforms = ['random_flip'] # data augmentation cfg.data.norm_mean = [0.485, 0.456, 0.406] # default is imagenet mean cfg.data.norm_std = [0.229, 0.224, 0.225] # default is imagenet std cfg.data.save_dir = 'log' # path to save log cfg.data.load_train_targets = False # specific datasets cfg.market1501 = CN() cfg.market1501.use_500k_distractors = False # add 500k distractors to the gallery set for market1501 cfg.cuhk03 = CN() cfg.cuhk03.labeled_images = False # use labeled images, if False, use detected images cfg.cuhk03.classic_split = False # use classic split by Li et al. CVPR14 cfg.cuhk03.use_metric_cuhk03 = False # use cuhk03's metric for evaluation # sampler cfg.sampler = CN() cfg.sampler.train_sampler = 'RandomSampler' cfg.sampler.num_instances = 4 # number of instances per identity for RandomIdentitySampler # video reid setting cfg.video = CN() cfg.video.seq_len = 15 # number of images to sample in a tracklet cfg.video.sample_method = 'evenly' # how to sample images from a tracklet cfg.video.pooling_method = 'avg' # how to pool features over a tracklet # train cfg.train = CN() cfg.train.optim = 'adam' cfg.train.lr = 0.0003 cfg.train.weight_decay = 5e-4 cfg.train.max_epoch = 60 cfg.train.start_epoch = 0 cfg.train.batch_size = 32 cfg.train.fixbase_epoch = 0 # number of epochs to fix base layers cfg.train.open_layers = [ 'classifier' ] # layers for training while keeping others frozen cfg.train.staged_lr = False # set different lr to different layers cfg.train.new_layers = ['classifier'] # newly added layers with default lr cfg.train.base_lr_mult = 0.1 # learning rate multiplier for base layers cfg.train.lr_scheduler = 'single_step' cfg.train.stepsize = [20] # stepsize to decay learning rate cfg.train.gamma = 0.1 # learning rate decay multiplier cfg.train.print_freq = 20 # print frequency cfg.train.seed = 1 # random seed # optimizer cfg.sgd = CN() cfg.sgd.momentum = 0.9 # momentum factor for sgd and rmsprop cfg.sgd.dampening = 0. # dampening for momentum cfg.sgd.nesterov = False # Nesterov momentum cfg.rmsprop = CN() cfg.rmsprop.alpha = 0.99 # smoothing constant cfg.adam = CN() cfg.adam.beta1 = 0.9 # exponential decay rate for first moment cfg.adam.beta2 = 0.999 # exponential decay rate for second moment # loss cfg.loss = CN() cfg.loss.name = 'triplet' cfg.loss.softmax = CN() cfg.loss.softmax.label_smooth = True # use label smoothing regularizer cfg.loss.triplet = CN() cfg.loss.triplet.margin = 0.3 # distance margin cfg.loss.triplet.weight_t = 1. # weight to balance hard triplet loss cfg.loss.triplet.weight_x = 0. # weight to balance cross entropy loss cfg.loss.dml = CN() cfg.loss.dml.weight_ml = 1. # weight for mutual learning loss # test cfg.test = CN() cfg.test.batch_size = 100 cfg.test.dist_metric = 'euclidean' # distance metric, ['euclidean', 'cosine'] cfg.test.normalize_feature = False # normalize feature vectors before computing distance cfg.test.ranks = [1, 5, 10, 20] # cmc ranks cfg.test.evaluate = False # test only cfg.test.eval_freq = -1 # evaluation frequency (-1 means to only test after training) cfg.test.start_eval = 0 # start to evaluate after a specific epoch cfg.test.rerank = False # use person re-ranking cfg.test.visrank = False # visualize ranked results (only available when cfg.test.evaluate=True) cfg.test.visrank_topk = 10 # top-k ranks to visualize return cfg def imagedata_kwargs(cfg): return { 'root': cfg.data.root, 'sources': cfg.data.sources, 'targets': cfg.data.targets, 'height': cfg.data.height, 'width': cfg.data.width, 'transforms': cfg.data.transforms, 'norm_mean': cfg.data.norm_mean, 'norm_std': cfg.data.norm_std, 'use_gpu': cfg.use_gpu, 'split_id': cfg.data.split_id, 'combineall': cfg.data.combineall, 'load_train_targets': cfg.data.load_train_targets, 'batch_size_train': cfg.train.batch_size, 'batch_size_test': cfg.test.batch_size, 'workers': cfg.data.workers, 'num_instances': cfg.sampler.num_instances, 'train_sampler': cfg.sampler.train_sampler, # image 'cuhk03_labeled': cfg.cuhk03.labeled_images, 'cuhk03_classic_split': cfg.cuhk03.classic_split, 'market1501_500k': cfg.market1501.use_500k_distractors, } def videodata_kwargs(cfg): return { 'root': cfg.data.root, 'sources': cfg.data.sources, 'targets': cfg.data.targets, 'height': cfg.data.height, 'width': cfg.data.width, 'transforms': cfg.data.transforms, 'norm_mean': cfg.data.norm_mean, 'norm_std': cfg.data.norm_std, 'use_gpu': cfg.use_gpu, 'split_id': cfg.data.split_id, 'combineall': cfg.data.combineall, 'batch_size_train': cfg.train.batch_size, 'batch_size_test': cfg.test.batch_size, 'workers': cfg.data.workers, 'num_instances': cfg.sampler.num_instances, 'train_sampler': cfg.sampler.train_sampler, # video 'seq_len': cfg.video.seq_len, 'sample_method': cfg.video.sample_method } def optimizer_kwargs(cfg): return { 'optim': cfg.train.optim, 'lr': cfg.train.lr, 'weight_decay': cfg.train.weight_decay, 'momentum': cfg.sgd.momentum, 'sgd_dampening': cfg.sgd.dampening, 'sgd_nesterov': cfg.sgd.nesterov, 'rmsprop_alpha': cfg.rmsprop.alpha, 'adam_beta1': cfg.adam.beta1, 'adam_beta2': cfg.adam.beta2, 'staged_lr': cfg.train.staged_lr, 'new_layers': cfg.train.new_layers, 'base_lr_mult': cfg.train.base_lr_mult } def lr_scheduler_kwargs(cfg): return { 'lr_scheduler': cfg.train.lr_scheduler, 'stepsize': cfg.train.stepsize, 'gamma': cfg.train.gamma, 'max_epoch': cfg.train.max_epoch } def engine_run_kwargs(cfg): return { 'save_dir': cfg.data.save_dir, 'max_epoch': cfg.train.max_epoch, 'start_epoch': cfg.train.start_epoch, 'fixbase_epoch': cfg.train.fixbase_epoch, 'open_layers': cfg.train.open_layers, 'start_eval': cfg.test.start_eval, 'eval_freq': cfg.test.eval_freq, 'test_only': cfg.test.evaluate, 'print_freq': cfg.train.print_freq, 'dist_metric': cfg.test.dist_metric, 'normalize_feature': cfg.test.normalize_feature, 'visrank': cfg.test.visrank, 'visrank_topk': cfg.test.visrank_topk, 'use_metric_cuhk03': cfg.cuhk03.use_metric_cuhk03, 'ranks': cfg.test.ranks, 'rerank': cfg.test.rerank } ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/DML/dml.py ================================================ from __future__ import division, print_function, absolute_import import torch from torch.nn import functional as F from torchreid.utils import open_all_layers, open_specified_layers from torchreid.engine import Engine from torchreid.losses import TripletLoss, CrossEntropyLoss class ImageDMLEngine(Engine): def __init__( self, datamanager, model1, optimizer1, scheduler1, model2, optimizer2, scheduler2, margin=0.3, weight_t=0.5, weight_x=1., weight_ml=1., use_gpu=True, label_smooth=True, deploy='model1' ): super(ImageDMLEngine, self).__init__(datamanager, use_gpu) self.model1 = model1 self.optimizer1 = optimizer1 self.scheduler1 = scheduler1 self.register_model('model1', model1, optimizer1, scheduler1) self.model2 = model2 self.optimizer2 = optimizer2 self.scheduler2 = scheduler2 self.register_model('model2', model2, optimizer2, scheduler2) self.weight_t = weight_t self.weight_x = weight_x self.weight_ml = weight_ml assert deploy in ['model1', 'model2', 'both'] self.deploy = deploy self.criterion_t = TripletLoss(margin=margin) self.criterion_x = CrossEntropyLoss( num_classes=self.datamanager.num_train_pids, use_gpu=self.use_gpu, label_smooth=label_smooth ) def forward_backward(self, data): imgs, pids = self.parse_data_for_train(data) if self.use_gpu: imgs = imgs.cuda() pids = pids.cuda() outputs1, features1 = self.model1(imgs) loss1_x = self.compute_loss(self.criterion_x, outputs1, pids) loss1_t = self.compute_loss(self.criterion_t, features1, pids) outputs2, features2 = self.model2(imgs) loss2_x = self.compute_loss(self.criterion_x, outputs2, pids) loss2_t = self.compute_loss(self.criterion_t, features2, pids) loss1_ml = self.compute_kl_div( outputs2.detach(), outputs1, is_logit=True ) loss2_ml = self.compute_kl_div( outputs1.detach(), outputs2, is_logit=True ) loss1 = 0 loss1 += loss1_x * self.weight_x loss1 += loss1_t * self.weight_t loss1 += loss1_ml * self.weight_ml loss2 = 0 loss2 += loss2_x * self.weight_x loss2 += loss2_t * self.weight_t loss2 += loss2_ml * self.weight_ml self.optimizer1.zero_grad() loss1.backward() self.optimizer1.step() self.optimizer2.zero_grad() loss2.backward() self.optimizer2.step() loss_dict = { 'loss1_x': loss1_x.item(), 'loss1_t': loss1_t.item(), 'loss1_ml': loss1_ml.item(), 'loss2_x': loss1_x.item(), 'loss2_t': loss1_t.item(), 'loss2_ml': loss1_ml.item() } return loss_dict @staticmethod def compute_kl_div(p, q, is_logit=True): if is_logit: p = F.softmax(p, dim=1) q = F.softmax(q, dim=1) return -(p * torch.log(q + 1e-8)).sum(1).mean() def two_stepped_transfer_learning( self, epoch, fixbase_epoch, open_layers, model=None ): """Two stepped transfer learning. The idea is to freeze base layers for a certain number of epochs and then open all layers for training. Reference: https://arxiv.org/abs/1611.05244 """ model1 = self.model1 model2 = self.model2 if (epoch + 1) <= fixbase_epoch and open_layers is not None: print( '* Only train {} (epoch: {}/{})'.format( open_layers, epoch + 1, fixbase_epoch ) ) open_specified_layers(model1, open_layers) open_specified_layers(model2, open_layers) else: open_all_layers(model1) open_all_layers(model2) def extract_features(self, input): if self.deploy == 'model1': return self.model1(input) elif self.deploy == 'model2': return self.model2(input) else: features = [] features.append(self.model1(input)) features.append(self.model2(input)) return torch.cat(features, 1) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/DML/im_osnet_x1_0_dml_256x128_amsgrad_cosine.yaml ================================================ model: name: 'osnet_x1_0' pretrained: True deploy: 'model1' data: type: 'image' sources: ['market1501'] targets: ['market1501'] height: 256 width: 128 combineall: False transforms: ['random_flip', 'random_erase'] save_dir: 'log/osnet_x1_0_market1501_dml_cosinelr' loss: name: 'triplet' softmax: label_smooth: True triplet: margin: 0.3 weight_t: 0.5 weight_x: 1. dml: weight_ml: 1. train: optim: 'amsgrad' lr: 0.0015 max_epoch: 250 batch_size: 64 fixbase_epoch: 10 open_layers: ['classifier'] lr_scheduler: 'cosine' test: batch_size: 300 dist_metric: 'cosine' normalize_feature: False evaluate: False eval_freq: -1 rerank: False ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/DML/main.py ================================================ import sys import copy import time import os.path as osp import argparse import torch import torch.nn as nn import torchreid from torchreid.utils import ( Logger, check_isfile, set_random_seed, collect_env_info, resume_from_checkpoint, load_pretrained_weights, compute_model_complexity ) from dml import ImageDMLEngine from default_config import ( imagedata_kwargs, optimizer_kwargs, engine_run_kwargs, get_default_config, lr_scheduler_kwargs ) def reset_config(cfg, args): if args.root: cfg.data.root = args.root if args.sources: cfg.data.sources = args.sources if args.targets: cfg.data.targets = args.targets if args.transforms: cfg.data.transforms = args.transforms def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument( '--config-file', type=str, default='', help='path to config file' ) parser.add_argument( '-s', '--sources', type=str, nargs='+', help='source datasets (delimited by space)' ) parser.add_argument( '-t', '--targets', type=str, nargs='+', help='target datasets (delimited by space)' ) parser.add_argument( '--transforms', type=str, nargs='+', help='data augmentation' ) parser.add_argument( '--root', type=str, default='', help='path to data root' ) parser.add_argument( 'opts', default=None, nargs=argparse.REMAINDER, help='Modify config options using the command-line' ) args = parser.parse_args() cfg = get_default_config() cfg.use_gpu = torch.cuda.is_available() if args.config_file: cfg.merge_from_file(args.config_file) reset_config(cfg, args) cfg.merge_from_list(args.opts) set_random_seed(cfg.train.seed) log_name = 'test.log' if cfg.test.evaluate else 'train.log' log_name += time.strftime('-%Y-%m-%d-%H-%M-%S') sys.stdout = Logger(osp.join(cfg.data.save_dir, log_name)) print('Show configuration\n{}\n'.format(cfg)) print('Collecting env info ...') print('** System info **\n{}\n'.format(collect_env_info())) if cfg.use_gpu: torch.backends.cudnn.benchmark = True datamanager = torchreid.data.ImageDataManager(**imagedata_kwargs(cfg)) print('Building model-1: {}'.format(cfg.model.name)) model1 = torchreid.models.build_model( name=cfg.model.name, num_classes=datamanager.num_train_pids, loss=cfg.loss.name, pretrained=cfg.model.pretrained, use_gpu=cfg.use_gpu ) num_params, flops = compute_model_complexity( model1, (1, 3, cfg.data.height, cfg.data.width) ) print('Model complexity: params={:,} flops={:,}'.format(num_params, flops)) print('Copying model-1 to model-2') model2 = copy.deepcopy(model1) if cfg.model.load_weights1 and check_isfile(cfg.model.load_weights1): load_pretrained_weights(model1, cfg.model.load_weights1) if cfg.model.load_weights2 and check_isfile(cfg.model.load_weights2): load_pretrained_weights(model2, cfg.model.load_weights2) if cfg.use_gpu: model1 = nn.DataParallel(model1).cuda() model2 = nn.DataParallel(model2).cuda() optimizer1 = torchreid.optim.build_optimizer( model1, **optimizer_kwargs(cfg) ) scheduler1 = torchreid.optim.build_lr_scheduler( optimizer1, **lr_scheduler_kwargs(cfg) ) optimizer2 = torchreid.optim.build_optimizer( model2, **optimizer_kwargs(cfg) ) scheduler2 = torchreid.optim.build_lr_scheduler( optimizer2, **lr_scheduler_kwargs(cfg) ) if cfg.model.resume1 and check_isfile(cfg.model.resume1): cfg.train.start_epoch = resume_from_checkpoint( cfg.model.resume1, model1, optimizer=optimizer1, scheduler=scheduler1 ) if cfg.model.resume2 and check_isfile(cfg.model.resume2): resume_from_checkpoint( cfg.model.resume2, model2, optimizer=optimizer2, scheduler=scheduler2 ) print('Building DML-engine for image-reid') engine = ImageDMLEngine( datamanager, model1, optimizer1, scheduler1, model2, optimizer2, scheduler2, margin=cfg.loss.triplet.margin, weight_t=cfg.loss.triplet.weight_t, weight_x=cfg.loss.triplet.weight_x, weight_ml=cfg.loss.dml.weight_ml, use_gpu=cfg.use_gpu, label_smooth=cfg.loss.softmax.label_smooth, deploy=cfg.model.deploy ) engine.run(**engine_run_kwargs(cfg)) if __name__ == '__main__': main() ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/OSNet_AIN/README.md ================================================ # Differentiable NAS for OSNet-AIN ## Introduction This repository contains the neural architecture search (NAS) code (based on [Torchreid](https://arxiv.org/abs/1910.10093)) for [OSNet-AIN](https://arxiv.org/abs/1910.06827), an extension of [OSNet](https://arxiv.org/abs/1905.00953) that achieves strong performance on cross-domain person re-identification (re-ID) benchmarks (*without using any target data*). OSNet-AIN builds on the idea of using [instance normalisation](https://arxiv.org/abs/1607.08022) (IN) layers to eliminate instance-specific contrast in images for domain-generalisable representation learning. This is inspired by the [neural style transfer](https://arxiv.org/abs/1703.06868) works that use IN to remove image styles. Though IN naturally suits the cross-domain person re-ID task, it still remains unclear that where to insert IN to a re-ID CNN can maximise the performance gain. To avoid exhaustively evaluating all possible designs, OSNet-AIN learns to search for the optimal OSNet+IN design from data using a differentiable NAS algorithm. For technical details, please refer to our paper at https://arxiv.org/abs/1910.06827.
## Training Assume the reid data is stored at `$DATA`. Run ``` python main.py --config-file nas.yaml --root $DATA ``` The structure of the found architecture will be shown at the end of training. The default config was designed for 8 Tesla V100 32GB GPUs. You can modify the batch size based on your device memory. **Note** that the test result obtained at the end of architecture search is not meaningful (due to the stochastic sampling layers). Therefore, do not rely on the result to judge the model performance. Instead, you should construct the found architecture in `osnet_child.py` and re-train and evaluate the model on the reid datasets. ## Citation If you find this code useful to your research, please consider citing the following papers. ``` @article{zhou2021osnet, title={Learning Generalisable Omni-Scale Representations for Person Re-Identification}, author={Zhou, Kaiyang and Yang, Yongxin and Cavallaro, Andrea and Xiang, Tao}, journal={TPAMI}, year={2021} } @inproceedings{zhou2019osnet, title={Omni-Scale Feature Learning for Person Re-Identification}, author={Zhou, Kaiyang and Yang, Yongxin and Cavallaro, Andrea and Xiang, Tao}, booktitle={ICCV}, year={2019} } ``` ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/OSNet_AIN/default_config.py ================================================ from yacs.config import CfgNode as CN def get_default_config(): cfg = CN() # model cfg.model = CN() cfg.model.name = 'resnet50' cfg.model.pretrained = True # automatically load pretrained model weights if available cfg.model.load_weights = '' # path to model weights cfg.model.resume = '' # path to checkpoint for resume training # NAS cfg.nas = CN() cfg.nas.mc_iter = 1 # Monte Carlo sampling cfg.nas.init_lmda = 10. # initial lambda value cfg.nas.min_lmda = 1. # minimum lambda value cfg.nas.lmda_decay_step = 20 # decay step for lambda cfg.nas.lmda_decay_rate = 0.5 # decay rate for lambda cfg.nas.fixed_lmda = False # keep lambda unchanged # data cfg.data = CN() cfg.data.type = 'image' cfg.data.root = 'reid-data' cfg.data.sources = ['market1501'] cfg.data.targets = ['market1501'] cfg.data.workers = 4 # number of data loading workers cfg.data.split_id = 0 # split index cfg.data.height = 256 # image height cfg.data.width = 128 # image width cfg.data.combineall = False # combine train, query and gallery for training cfg.data.transforms = ['random_flip'] # data augmentation cfg.data.norm_mean = [0.485, 0.456, 0.406] # default is imagenet mean cfg.data.norm_std = [0.229, 0.224, 0.225] # default is imagenet std cfg.data.save_dir = 'log' # path to save log # specific datasets cfg.market1501 = CN() cfg.market1501.use_500k_distractors = False # add 500k distractors to the gallery set for market1501 cfg.cuhk03 = CN() cfg.cuhk03.labeled_images = False # use labeled images, if False, use detected images cfg.cuhk03.classic_split = False # use classic split by Li et al. CVPR14 cfg.cuhk03.use_metric_cuhk03 = False # use cuhk03's metric for evaluation # sampler cfg.sampler = CN() cfg.sampler.train_sampler = 'RandomSampler' cfg.sampler.num_instances = 4 # number of instances per identity for RandomIdentitySampler # video reid setting cfg.video = CN() cfg.video.seq_len = 15 # number of images to sample in a tracklet cfg.video.sample_method = 'evenly' # how to sample images from a tracklet cfg.video.pooling_method = 'avg' # how to pool features over a tracklet # train cfg.train = CN() cfg.train.optim = 'adam' cfg.train.lr = 0.0003 cfg.train.weight_decay = 5e-4 cfg.train.max_epoch = 60 cfg.train.start_epoch = 0 cfg.train.batch_size = 32 cfg.train.fixbase_epoch = 0 # number of epochs to fix base layers cfg.train.open_layers = [ 'classifier' ] # layers for training while keeping others frozen cfg.train.staged_lr = False # set different lr to different layers cfg.train.new_layers = ['classifier'] # newly added layers with default lr cfg.train.base_lr_mult = 0.1 # learning rate multiplier for base layers cfg.train.lr_scheduler = 'single_step' cfg.train.stepsize = [20] # stepsize to decay learning rate cfg.train.gamma = 0.1 # learning rate decay multiplier cfg.train.print_freq = 20 # print frequency cfg.train.seed = 1 # random seed # optimizer cfg.sgd = CN() cfg.sgd.momentum = 0.9 # momentum factor for sgd and rmsprop cfg.sgd.dampening = 0. # dampening for momentum cfg.sgd.nesterov = False # Nesterov momentum cfg.rmsprop = CN() cfg.rmsprop.alpha = 0.99 # smoothing constant cfg.adam = CN() cfg.adam.beta1 = 0.9 # exponential decay rate for first moment cfg.adam.beta2 = 0.999 # exponential decay rate for second moment # loss cfg.loss = CN() cfg.loss.name = 'softmax' cfg.loss.softmax = CN() cfg.loss.softmax.label_smooth = True # use label smoothing regularizer cfg.loss.triplet = CN() cfg.loss.triplet.margin = 0.3 # distance margin cfg.loss.triplet.weight_t = 1. # weight to balance hard triplet loss cfg.loss.triplet.weight_x = 0. # weight to balance cross entropy loss # test cfg.test = CN() cfg.test.batch_size = 100 cfg.test.dist_metric = 'euclidean' # distance metric, ['euclidean', 'cosine'] cfg.test.normalize_feature = False # normalize feature vectors before computing distance cfg.test.ranks = [1, 5, 10, 20] # cmc ranks cfg.test.evaluate = False # test only cfg.test.eval_freq = -1 # evaluation frequency (-1 means to only test after training) cfg.test.start_eval = 0 # start to evaluate after a specific epoch cfg.test.rerank = False # use person re-ranking cfg.test.visrank = False # visualize ranked results (only available when cfg.test.evaluate=True) cfg.test.visrank_topk = 10 # top-k ranks to visualize cfg.test.visactmap = False # visualize CNN activation maps return cfg def imagedata_kwargs(cfg): return { 'root': cfg.data.root, 'sources': cfg.data.sources, 'targets': cfg.data.targets, 'height': cfg.data.height, 'width': cfg.data.width, 'transforms': cfg.data.transforms, 'norm_mean': cfg.data.norm_mean, 'norm_std': cfg.data.norm_std, 'use_gpu': cfg.use_gpu, 'split_id': cfg.data.split_id, 'combineall': cfg.data.combineall, 'batch_size_train': cfg.train.batch_size, 'batch_size_test': cfg.test.batch_size, 'workers': cfg.data.workers, 'num_instances': cfg.sampler.num_instances, 'train_sampler': cfg.sampler.train_sampler, # image 'cuhk03_labeled': cfg.cuhk03.labeled_images, 'cuhk03_classic_split': cfg.cuhk03.classic_split, 'market1501_500k': cfg.market1501.use_500k_distractors, } def videodata_kwargs(cfg): return { 'root': cfg.data.root, 'sources': cfg.data.sources, 'targets': cfg.data.targets, 'height': cfg.data.height, 'width': cfg.data.width, 'transforms': cfg.data.transforms, 'norm_mean': cfg.data.norm_mean, 'norm_std': cfg.data.norm_std, 'use_gpu': cfg.use_gpu, 'split_id': cfg.data.split_id, 'combineall': cfg.data.combineall, 'batch_size_train': cfg.train.batch_size, 'batch_size_test': cfg.test.batch_size, 'workers': cfg.data.workers, 'num_instances': cfg.sampler.num_instances, 'train_sampler': cfg.sampler.train_sampler, # video 'seq_len': cfg.video.seq_len, 'sample_method': cfg.video.sample_method } def optimizer_kwargs(cfg): return { 'optim': cfg.train.optim, 'lr': cfg.train.lr, 'weight_decay': cfg.train.weight_decay, 'momentum': cfg.sgd.momentum, 'sgd_dampening': cfg.sgd.dampening, 'sgd_nesterov': cfg.sgd.nesterov, 'rmsprop_alpha': cfg.rmsprop.alpha, 'adam_beta1': cfg.adam.beta1, 'adam_beta2': cfg.adam.beta2, 'staged_lr': cfg.train.staged_lr, 'new_layers': cfg.train.new_layers, 'base_lr_mult': cfg.train.base_lr_mult } def lr_scheduler_kwargs(cfg): return { 'lr_scheduler': cfg.train.lr_scheduler, 'stepsize': cfg.train.stepsize, 'gamma': cfg.train.gamma, 'max_epoch': cfg.train.max_epoch } def engine_run_kwargs(cfg): return { 'save_dir': cfg.data.save_dir, 'max_epoch': cfg.train.max_epoch, 'start_epoch': cfg.train.start_epoch, 'fixbase_epoch': cfg.train.fixbase_epoch, 'open_layers': cfg.train.open_layers, 'start_eval': cfg.test.start_eval, 'eval_freq': cfg.test.eval_freq, 'test_only': cfg.test.evaluate, 'print_freq': cfg.train.print_freq, 'dist_metric': cfg.test.dist_metric, 'normalize_feature': cfg.test.normalize_feature, 'visrank': cfg.test.visrank, 'visrank_topk': cfg.test.visrank_topk, 'use_metric_cuhk03': cfg.cuhk03.use_metric_cuhk03, 'ranks': cfg.test.ranks, 'rerank': cfg.test.rerank } ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/OSNet_AIN/main.py ================================================ import os import sys import time import os.path as osp import argparse import torch import torch.nn as nn import torchreid from torchreid.utils import ( Logger, check_isfile, set_random_seed, collect_env_info, resume_from_checkpoint, compute_model_complexity ) import osnet_search as osnet_models from softmax_nas import ImageSoftmaxNASEngine from default_config import ( imagedata_kwargs, optimizer_kwargs, engine_run_kwargs, get_default_config, lr_scheduler_kwargs ) def reset_config(cfg, args): if args.root: cfg.data.root = args.root if args.sources: cfg.data.sources = args.sources if args.targets: cfg.data.targets = args.targets if args.transforms: cfg.data.transforms = args.transforms def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument( '--config-file', type=str, default='', help='path to config file' ) parser.add_argument( '-s', '--sources', type=str, nargs='+', help='source datasets (delimited by space)' ) parser.add_argument( '-t', '--targets', type=str, nargs='+', help='target datasets (delimited by space)' ) parser.add_argument( '--transforms', type=str, nargs='+', help='data augmentation' ) parser.add_argument( '--root', type=str, default='', help='path to data root' ) parser.add_argument( '--gpu-devices', type=str, default='', ) parser.add_argument( 'opts', default=None, nargs=argparse.REMAINDER, help='Modify config options using the command-line' ) args = parser.parse_args() cfg = get_default_config() cfg.use_gpu = torch.cuda.is_available() if args.config_file: cfg.merge_from_file(args.config_file) reset_config(cfg, args) cfg.merge_from_list(args.opts) set_random_seed(cfg.train.seed) if cfg.use_gpu and args.gpu_devices: # if gpu_devices is not specified, all available gpus will be used os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu_devices log_name = 'test.log' if cfg.test.evaluate else 'train.log' log_name += time.strftime('-%Y-%m-%d-%H-%M-%S') sys.stdout = Logger(osp.join(cfg.data.save_dir, log_name)) print('Show configuration\n{}\n'.format(cfg)) print('Collecting env info ...') print('** System info **\n{}\n'.format(collect_env_info())) if cfg.use_gpu: torch.backends.cudnn.benchmark = True datamanager = torchreid.data.ImageDataManager(**imagedata_kwargs(cfg)) print('Building model: {}'.format(cfg.model.name)) model = osnet_models.build_model( cfg.model.name, num_classes=datamanager.num_train_pids ) num_params, flops = compute_model_complexity( model, (1, 3, cfg.data.height, cfg.data.width) ) print('Model complexity: params={:,} flops={:,}'.format(num_params, flops)) if cfg.use_gpu: model = nn.DataParallel(model).cuda() optimizer = torchreid.optim.build_optimizer(model, **optimizer_kwargs(cfg)) scheduler = torchreid.optim.build_lr_scheduler( optimizer, **lr_scheduler_kwargs(cfg) ) if cfg.model.resume and check_isfile(cfg.model.resume): cfg.train.start_epoch = resume_from_checkpoint( cfg.model.resume, model, optimizer=optimizer ) print('Building NAS engine') engine = ImageSoftmaxNASEngine( datamanager, model, optimizer, scheduler=scheduler, use_gpu=cfg.use_gpu, label_smooth=cfg.loss.softmax.label_smooth, mc_iter=cfg.nas.mc_iter, init_lmda=cfg.nas.init_lmda, min_lmda=cfg.nas.min_lmda, lmda_decay_step=cfg.nas.lmda_decay_step, lmda_decay_rate=cfg.nas.lmda_decay_rate, fixed_lmda=cfg.nas.fixed_lmda ) engine.run(**engine_run_kwargs(cfg)) print('*** Display the found architecture ***') if cfg.use_gpu: model.module.build_child_graph() else: model.build_child_graph() if __name__ == '__main__': main() ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/OSNet_AIN/nas.yaml ================================================ model: name: 'osnet_nas' pretrained: False nas: mc_iter: 1 init_lmda: 10. min_lmda: 1. lmda_decay_step: 20 lmda_decay_rate: 0.5 fixed_lmda: False data: type: 'image' sources: ['msmt17'] targets: ['market1501'] height: 256 width: 128 combineall: True transforms: ['random_flip', 'color_jitter'] save_dir: 'log/osnet_nas' loss: name: 'softmax' softmax: label_smooth: True train: optim: 'sgd' lr: 0.1 max_epoch: 120 batch_size: 512 fixbase_epoch: 0 open_layers: ['classifier'] lr_scheduler: 'cosine' test: batch_size: 300 dist_metric: 'cosine' normalize_feature: False evaluate: False eval_freq: -1 rerank: False visactmap: False ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/OSNet_AIN/osnet_child.py ================================================ from __future__ import division, absolute_import from torch import nn from torch.nn import functional as F ########## # Basic layers ########## class ConvLayer(nn.Module): """Convolution layer (conv + bn + relu).""" def __init__( self, in_channels, out_channels, kernel_size, stride=1, padding=0, groups=1, IN=False ): super(ConvLayer, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, kernel_size, stride=stride, padding=padding, bias=False, groups=groups ) if IN: self.bn = nn.InstanceNorm2d(out_channels, affine=True) else: self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) return self.relu(x) class Conv1x1(nn.Module): """1x1 convolution + bn + relu.""" def __init__(self, in_channels, out_channels, stride=1, groups=1): super(Conv1x1, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=stride, padding=0, bias=False, groups=groups ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) return self.relu(x) class Conv1x1Linear(nn.Module): """1x1 convolution + bn (w/o non-linearity).""" def __init__(self, in_channels, out_channels, stride=1, bn=True): super(Conv1x1Linear, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=stride, padding=0, bias=False ) self.bn = None if bn: self.bn = nn.BatchNorm2d(out_channels) def forward(self, x): x = self.conv(x) if self.bn is not None: x = self.bn(x) return x class Conv3x3(nn.Module): """3x3 convolution + bn + relu.""" def __init__(self, in_channels, out_channels, stride=1, groups=1): super(Conv3x3, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 3, stride=stride, padding=1, bias=False, groups=groups ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) return self.relu(x) class LightConv3x3(nn.Module): """Lightweight 3x3 convolution. 1x1 (linear) + dw 3x3 (nonlinear). """ def __init__(self, in_channels, out_channels): super(LightConv3x3, self).__init__() self.conv1 = nn.Conv2d( in_channels, out_channels, 1, stride=1, padding=0, bias=False ) self.conv2 = nn.Conv2d( out_channels, out_channels, 3, stride=1, padding=1, bias=False, groups=out_channels ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.bn(x) return self.relu(x) class LightConvStream(nn.Module): """Lightweight convolution stream.""" def __init__(self, in_channels, out_channels, depth): super(LightConvStream, self).__init__() assert depth >= 1, 'depth must be equal to or larger than 1, but got {}'.format( depth ) layers = [] layers += [LightConv3x3(in_channels, out_channels)] for i in range(depth - 1): layers += [LightConv3x3(out_channels, out_channels)] self.layers = nn.Sequential(*layers) def forward(self, x): return self.layers(x) ########## # Building blocks for omni-scale feature learning ########## class ChannelGate(nn.Module): """A mini-network that generates channel-wise gates conditioned on input tensor.""" def __init__( self, in_channels, num_gates=None, return_gates=False, gate_activation='sigmoid', reduction=16, layer_norm=False ): super(ChannelGate, self).__init__() if num_gates is None: num_gates = in_channels self.return_gates = return_gates self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.fc1 = nn.Conv2d( in_channels, in_channels // reduction, kernel_size=1, bias=True, padding=0 ) self.norm1 = None if layer_norm: self.norm1 = nn.LayerNorm((in_channels // reduction, 1, 1)) self.relu = nn.ReLU(inplace=True) self.fc2 = nn.Conv2d( in_channels // reduction, num_gates, kernel_size=1, bias=True, padding=0 ) if gate_activation == 'sigmoid': self.gate_activation = nn.Sigmoid() elif gate_activation == 'relu': self.gate_activation = nn.ReLU(inplace=True) elif gate_activation == 'linear': self.gate_activation = None else: raise RuntimeError( "Unknown gate activation: {}".format(gate_activation) ) def forward(self, x): input = x x = self.global_avgpool(x) x = self.fc1(x) if self.norm1 is not None: x = self.norm1(x) x = self.relu(x) x = self.fc2(x) if self.gate_activation is not None: x = self.gate_activation(x) if self.return_gates: return x return input * x class OSBlock(nn.Module): """Omni-scale feature learning block.""" def __init__(self, in_channels, out_channels, reduction=4, T=4, **kwargs): super(OSBlock, self).__init__() assert T >= 1 assert out_channels >= reduction and out_channels % reduction == 0 mid_channels = out_channels // reduction self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2 = nn.ModuleList() for t in range(1, T + 1): self.conv2 += [LightConvStream(mid_channels, mid_channels, t)] self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) def forward(self, x): identity = x x1 = self.conv1(x) x2 = 0 for conv2_t in self.conv2: x2_t = conv2_t(x1) x2 = x2 + self.gate(x2_t) x3 = self.conv3(x2) if self.downsample is not None: identity = self.downsample(identity) out = x3 + identity return F.relu(out) class OSBlockINv1(nn.Module): """Omni-scale feature learning block with instance normalization.""" def __init__(self, in_channels, out_channels, reduction=4, T=4, **kwargs): super(OSBlockINv1, self).__init__() assert T >= 1 assert out_channels >= reduction and out_channels % reduction == 0 mid_channels = out_channels // reduction self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2 = nn.ModuleList() for t in range(1, T + 1): self.conv2 += [LightConvStream(mid_channels, mid_channels, t)] self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels, bn=False) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) self.IN = nn.InstanceNorm2d(out_channels, affine=True) def forward(self, x): identity = x x1 = self.conv1(x) x2 = 0 for conv2_t in self.conv2: x2_t = conv2_t(x1) x2 = x2 + self.gate(x2_t) x3 = self.conv3(x2) x3 = self.IN(x3) # IN inside residual if self.downsample is not None: identity = self.downsample(identity) out = x3 + identity return F.relu(out) class OSBlockINv2(nn.Module): """Omni-scale feature learning block with instance normalization.""" def __init__(self, in_channels, out_channels, reduction=4, T=4, **kwargs): super(OSBlockINv2, self).__init__() assert T >= 1 assert out_channels >= reduction and out_channels % reduction == 0 mid_channels = out_channels // reduction self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2 = nn.ModuleList() for t in range(1, T + 1): self.conv2 += [LightConvStream(mid_channels, mid_channels, t)] self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) self.IN = nn.InstanceNorm2d(out_channels, affine=True) def forward(self, x): identity = x x1 = self.conv1(x) x2 = 0 for conv2_t in self.conv2: x2_t = conv2_t(x1) x2 = x2 + self.gate(x2_t) x3 = self.conv3(x2) if self.downsample is not None: identity = self.downsample(identity) out = x3 + identity out = self.IN(out) # IN outside residual return F.relu(out) class OSBlockINv3(nn.Module): """Omni-scale feature learning block with instance normalization.""" def __init__(self, in_channels, out_channels, reduction=4, T=4, **kwargs): super(OSBlockINv3, self).__init__() assert T >= 1 assert out_channels >= reduction and out_channels % reduction == 0 mid_channels = out_channels // reduction self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2 = nn.ModuleList() for t in range(1, T + 1): self.conv2 += [LightConvStream(mid_channels, mid_channels, t)] self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels, bn=False) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) self.IN_in = nn.InstanceNorm2d(out_channels, affine=True) self.IN_out = nn.InstanceNorm2d(out_channels, affine=True) def forward(self, x): identity = x x1 = self.conv1(x) x2 = 0 for conv2_t in self.conv2: x2_t = conv2_t(x1) x2 = x2 + self.gate(x2_t) x3 = self.conv3(x2) x3 = self.IN_in(x3) # IN inside residual if self.downsample is not None: identity = self.downsample(identity) out = x3 + identity out = self.IN_out(out) # IN outside residual return F.relu(out) ########## # Network architecture ########## class OSNet(nn.Module): """Omni-Scale Network. Reference: - Zhou et al. Omni-Scale Feature Learning for Person Re-Identification. ICCV, 2019. - Zhou et al. Learning Generalisable Omni-Scale Representations for Person Re-Identification. TPAMI, 2021. """ def __init__( self, num_classes, blocks, layers, channels, feature_dim=512, loss='softmax', conv1_IN=True, **kwargs ): super(OSNet, self).__init__() num_blocks = len(blocks) assert num_blocks == len(layers) assert num_blocks == len(channels) - 1 self.loss = loss self.feature_dim = feature_dim # convolutional backbone self.conv1 = ConvLayer( 3, channels[0], 7, stride=2, padding=3, IN=conv1_IN ) self.maxpool = nn.MaxPool2d(3, stride=2, padding=1) self.conv2 = self._make_layer( blocks[0], layers[0], channels[0], channels[1] ) self.pool2 = nn.Sequential( Conv1x1(channels[1], channels[1]), nn.AvgPool2d(2, stride=2) ) self.conv3 = self._make_layer( blocks[1], layers[1], channels[1], channels[2] ) self.pool3 = nn.Sequential( Conv1x1(channels[2], channels[2]), nn.AvgPool2d(2, stride=2) ) self.conv4 = self._make_layer( blocks[2], layers[2], channels[2], channels[3] ) self.conv5 = Conv1x1(channels[3], channels[3]) self.global_avgpool = nn.AdaptiveAvgPool2d(1) # fully connected layer self.fc = self._construct_fc_layer( self.feature_dim, channels[3], dropout_p=None ) # identity classification layer self.classifier = nn.Linear(self.feature_dim, num_classes) self._init_params() def _make_layer(self, blocks, layer, in_channels, out_channels): layers = [] layers += [blocks[0](in_channels, out_channels)] for i in range(1, len(blocks)): layers += [blocks[i](out_channels, out_channels)] return nn.Sequential(*layers) def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): if fc_dims is None or fc_dims < 0: self.feature_dim = input_dim return None if isinstance(fc_dims, int): fc_dims = [fc_dims] layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def _init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.InstanceNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def featuremaps(self, x): x = self.conv1(x) x = self.maxpool(x) x = self.conv2(x) x = self.pool2(x) x = self.conv3(x) x = self.pool3(x) x = self.conv4(x) return self.conv5(x) def forward(self, x, return_featuremaps=False, **kwargs): x = self.featuremaps(x) if return_featuremaps: return x v = self.global_avgpool(x) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError("Unsupported loss: {}".format(self.loss)) ########## # Instantiation ########## def osnet_ain_x1_0( num_classes=1000, pretrained=True, loss='softmax', **kwargs ): model = OSNet( num_classes, blocks=[ [OSBlockINv1, OSBlockINv1], [OSBlock, OSBlockINv1], [OSBlockINv1, OSBlock] ], layers=[2, 2, 2], channels=[64, 256, 384, 512], loss=loss, conv1_IN=True, **kwargs ) return model __models = {'osnet_ain_x1_0': osnet_ain_x1_0} def build_model(name, num_classes=100): avai_models = list(__models.keys()) if name not in avai_models: raise KeyError( 'Unknown model: {}. Must be one of {}'.format(name, avai_models) ) return __models[name](num_classes=num_classes) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/OSNet_AIN/osnet_search.py ================================================ from __future__ import division, absolute_import import torch from torch import nn from torch.nn import functional as F EPS = 1e-12 NORM_AFFINE = False # enable affine transformations for normalization layer ########## # Basic layers ########## class IBN(nn.Module): """Instance + Batch Normalization.""" def __init__(self, num_channels): super(IBN, self).__init__() half1 = int(num_channels / 2) self.half = half1 half2 = num_channels - half1 self.IN = nn.InstanceNorm2d(half1, affine=NORM_AFFINE) self.BN = nn.BatchNorm2d(half2, affine=NORM_AFFINE) def forward(self, x): split = torch.split(x, self.half, 1) out1 = self.IN(split[0].contiguous()) out2 = self.BN(split[1].contiguous()) return torch.cat((out1, out2), 1) class ConvLayer(nn.Module): """Convolution layer (conv + bn + relu).""" def __init__( self, in_channels, out_channels, kernel_size, stride=1, padding=0, groups=1, IN=False ): super(ConvLayer, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, kernel_size, stride=stride, padding=padding, bias=False, groups=groups ) if IN: self.bn = nn.InstanceNorm2d(out_channels, affine=NORM_AFFINE) else: self.bn = nn.BatchNorm2d(out_channels, affine=NORM_AFFINE) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) return self.relu(x) class Conv1x1(nn.Module): """1x1 convolution + bn + relu.""" def __init__( self, in_channels, out_channels, stride=1, groups=1, ibn=False ): super(Conv1x1, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=stride, padding=0, bias=False, groups=groups ) if ibn: self.bn = IBN(out_channels) else: self.bn = nn.BatchNorm2d(out_channels, affine=NORM_AFFINE) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) return self.relu(x) class Conv1x1Linear(nn.Module): """1x1 convolution + bn (w/o non-linearity).""" def __init__(self, in_channels, out_channels, stride=1, bn=True): super(Conv1x1Linear, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=stride, padding=0, bias=False ) self.bn = None if bn: self.bn = nn.BatchNorm2d(out_channels, affine=NORM_AFFINE) def forward(self, x): x = self.conv(x) if self.bn is not None: x = self.bn(x) return x class Conv3x3(nn.Module): """3x3 convolution + bn + relu.""" def __init__(self, in_channels, out_channels, stride=1, groups=1): super(Conv3x3, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 3, stride=stride, padding=1, bias=False, groups=groups ) self.bn = nn.BatchNorm2d(out_channels, affine=NORM_AFFINE) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) return self.relu(x) class LightConv3x3(nn.Module): """Lightweight 3x3 convolution. 1x1 (linear) + dw 3x3 (nonlinear). """ def __init__(self, in_channels, out_channels): super(LightConv3x3, self).__init__() self.conv1 = nn.Conv2d( in_channels, out_channels, 1, stride=1, padding=0, bias=False ) self.conv2 = nn.Conv2d( out_channels, out_channels, 3, stride=1, padding=1, bias=False, groups=out_channels ) self.bn = nn.BatchNorm2d(out_channels, affine=NORM_AFFINE) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.bn(x) return self.relu(x) class LightConvStream(nn.Module): """Lightweight convolution stream.""" def __init__(self, in_channels, out_channels, depth): super(LightConvStream, self).__init__() assert depth >= 1, 'depth must be equal to or larger than 1, but got {}'.format( depth ) layers = [] layers += [LightConv3x3(in_channels, out_channels)] for i in range(depth - 1): layers += [LightConv3x3(out_channels, out_channels)] self.layers = nn.Sequential(*layers) def forward(self, x): return self.layers(x) ########## # Building blocks for omni-scale feature learning ########## class ChannelGate(nn.Module): """A mini-network that generates channel-wise gates conditioned on input tensor.""" def __init__( self, in_channels, num_gates=None, return_gates=False, gate_activation='sigmoid', reduction=16, layer_norm=False ): super(ChannelGate, self).__init__() if num_gates is None: num_gates = in_channels self.return_gates = return_gates self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.fc1 = nn.Conv2d( in_channels, in_channels // reduction, kernel_size=1, bias=True, padding=0 ) self.norm1 = None if layer_norm: self.norm1 = nn.LayerNorm((in_channels // reduction, 1, 1)) self.relu = nn.ReLU(inplace=True) self.fc2 = nn.Conv2d( in_channels // reduction, num_gates, kernel_size=1, bias=True, padding=0 ) if gate_activation == 'sigmoid': self.gate_activation = nn.Sigmoid() elif gate_activation == 'relu': self.gate_activation = nn.ReLU(inplace=True) elif gate_activation == 'linear': self.gate_activation = None else: raise RuntimeError( "Unknown gate activation: {}".format(gate_activation) ) def forward(self, x): input = x x = self.global_avgpool(x) x = self.fc1(x) if self.norm1 is not None: x = self.norm1(x) x = self.relu(x) x = self.fc2(x) if self.gate_activation is not None: x = self.gate_activation(x) if self.return_gates: return x return input * x class OSBlock(nn.Module): """Omni-scale feature learning block.""" def __init__(self, in_channels, out_channels, reduction=4, T=4, **kwargs): super(OSBlock, self).__init__() assert T >= 1 assert out_channels >= reduction and out_channels % reduction == 0 mid_channels = out_channels // reduction self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2 = nn.ModuleList() for t in range(1, T + 1): self.conv2 += [LightConvStream(mid_channels, mid_channels, t)] self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) def forward(self, x): identity = x x1 = self.conv1(x) x2 = 0 for conv2_t in self.conv2: x2_t = conv2_t(x1) x2 = x2 + self.gate(x2_t) x3 = self.conv3(x2) if self.downsample is not None: identity = self.downsample(identity) out = x3 + identity return F.relu(out) class OSBlockINv1(nn.Module): """Omni-scale feature learning block with instance normalization.""" def __init__(self, in_channels, out_channels, reduction=4, T=4, **kwargs): super(OSBlockINv1, self).__init__() assert T >= 1 assert out_channels >= reduction and out_channels % reduction == 0 mid_channels = out_channels // reduction self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2 = nn.ModuleList() for t in range(1, T + 1): self.conv2 += [LightConvStream(mid_channels, mid_channels, t)] self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels, bn=False) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) self.IN = nn.InstanceNorm2d(out_channels, affine=NORM_AFFINE) def forward(self, x): identity = x x1 = self.conv1(x) x2 = 0 for conv2_t in self.conv2: x2_t = conv2_t(x1) x2 = x2 + self.gate(x2_t) x3 = self.conv3(x2) x3 = self.IN(x3) # IN inside residual if self.downsample is not None: identity = self.downsample(identity) out = x3 + identity return F.relu(out) class OSBlockINv2(nn.Module): """Omni-scale feature learning block with instance normalization.""" def __init__(self, in_channels, out_channels, reduction=4, T=4, **kwargs): super(OSBlockINv2, self).__init__() assert T >= 1 assert out_channels >= reduction and out_channels % reduction == 0 mid_channels = out_channels // reduction self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2 = nn.ModuleList() for t in range(1, T + 1): self.conv2 += [LightConvStream(mid_channels, mid_channels, t)] self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) self.IN = nn.InstanceNorm2d(out_channels, affine=NORM_AFFINE) def forward(self, x): identity = x x1 = self.conv1(x) x2 = 0 for conv2_t in self.conv2: x2_t = conv2_t(x1) x2 = x2 + self.gate(x2_t) x3 = self.conv3(x2) if self.downsample is not None: identity = self.downsample(identity) out = x3 + identity out = self.IN(out) # IN outside residual return F.relu(out) class OSBlockINv3(nn.Module): """Omni-scale feature learning block with instance normalization.""" def __init__(self, in_channels, out_channels, reduction=4, T=4, **kwargs): super(OSBlockINv3, self).__init__() assert T >= 1 assert out_channels >= reduction and out_channels % reduction == 0 mid_channels = out_channels // reduction self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2 = nn.ModuleList() for t in range(1, T + 1): self.conv2 += [LightConvStream(mid_channels, mid_channels, t)] self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels, bn=False) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) self.IN_in = nn.InstanceNorm2d(out_channels, affine=NORM_AFFINE) self.IN_out = nn.InstanceNorm2d(out_channels, affine=NORM_AFFINE) def forward(self, x): identity = x x1 = self.conv1(x) x2 = 0 for conv2_t in self.conv2: x2_t = conv2_t(x1) x2 = x2 + self.gate(x2_t) x3 = self.conv3(x2) x3 = self.IN_in(x3) # inside residual if self.downsample is not None: identity = self.downsample(identity) out = x3 + identity out = self.IN_out(out) # IN outside residual return F.relu(out) class NASBlock(nn.Module): """Neural architecture search layer.""" def __init__(self, in_channels, out_channels, search_space=None): super(NASBlock, self).__init__() self._is_child_graph = False self.search_space = search_space if self.search_space is None: raise ValueError('search_space is None') self.os_block = nn.ModuleList() for block in self.search_space: self.os_block += [block(in_channels, out_channels)] self.weights = nn.Parameter(torch.ones(len(self.search_space))) def build_child_graph(self): if self._is_child_graph: raise RuntimeError('build_child_graph() can only be called once') idx = self.weights.data.max(dim=0)[1].item() self.os_block = self.os_block[idx] self.weights = None self._is_child_graph = True return self.search_space[idx] def forward(self, x, lmda=1.): if self._is_child_graph: return self.os_block(x) uniform = torch.rand_like(self.weights) gumbel = -torch.log(-torch.log(uniform + EPS)) nonneg_weights = F.relu(self.weights) logits = torch.log(nonneg_weights + EPS) + gumbel exp = torch.exp(logits / lmda) weights_softmax = exp / (exp.sum() + EPS) output = 0 for i, weight in enumerate(weights_softmax): output = output + weight * self.os_block[i](x) return output ########## # Network architecture ########## class OSNet(nn.Module): """Omni-Scale Network. Reference: - Zhou et al. Omni-Scale Feature Learning for Person Re-Identification. ICCV, 2019. - Zhou et al. Learning Generalisable Omni-Scale Representations for Person Re-Identification. TPAMI, 2021. """ def __init__( self, num_classes, blocks, layers, channels, feature_dim=512, loss='softmax', search_space=None, **kwargs ): super(OSNet, self).__init__() num_blocks = len(blocks) assert num_blocks == len(layers) assert num_blocks == len(channels) - 1 # no matter what loss is specified, the model only returns the ID predictions self.loss = loss self.feature_dim = feature_dim # convolutional backbone self.conv1 = ConvLayer(3, channels[0], 7, stride=2, padding=3, IN=True) self.maxpool = nn.MaxPool2d(3, stride=2, padding=1) self.conv2 = self._make_layer( blocks[0], layers[0], channels[0], channels[1], search_space ) self.pool2 = nn.Sequential( Conv1x1(channels[1], channels[1]), nn.AvgPool2d(2, stride=2) ) self.conv3 = self._make_layer( blocks[1], layers[1], channels[1], channels[2], search_space ) self.pool3 = nn.Sequential( Conv1x1(channels[2], channels[2]), nn.AvgPool2d(2, stride=2) ) self.conv4 = self._make_layer( blocks[2], layers[2], channels[2], channels[3], search_space ) self.conv5 = Conv1x1(channels[3], channels[3]) self.global_avgpool = nn.AdaptiveAvgPool2d(1) # fully connected layer self.fc = self._construct_fc_layer( self.feature_dim, channels[3], dropout_p=None ) # identity classification layer self.classifier = nn.Linear(self.feature_dim, num_classes) def _make_layer( self, block, layer, in_channels, out_channels, search_space ): layers = nn.ModuleList() layers += [block(in_channels, out_channels, search_space=search_space)] for i in range(1, layer): layers += [ block(out_channels, out_channels, search_space=search_space) ] return layers def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): if fc_dims is None or fc_dims < 0: self.feature_dim = input_dim return None if isinstance(fc_dims, int): fc_dims = [fc_dims] layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim, affine=NORM_AFFINE)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def build_child_graph(self): print('Building child graph') for i, conv in enumerate(self.conv2): block = conv.build_child_graph() print('- conv2-{} Block={}'.format(i + 1, block.__name__)) for i, conv in enumerate(self.conv3): block = conv.build_child_graph() print('- conv3-{} Block={}'.format(i + 1, block.__name__)) for i, conv in enumerate(self.conv4): block = conv.build_child_graph() print('- conv4-{} Block={}'.format(i + 1, block.__name__)) def featuremaps(self, x, lmda): x = self.conv1(x) x = self.maxpool(x) for conv in self.conv2: x = conv(x, lmda) x = self.pool2(x) for conv in self.conv3: x = conv(x, lmda) x = self.pool3(x) for conv in self.conv4: x = conv(x, lmda) return self.conv5(x) def forward(self, x, lmda=1., return_featuremaps=False): # lmda (float): temperature parameter for concrete distribution x = self.featuremaps(x, lmda) if return_featuremaps: return x v = self.global_avgpool(x) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v return self.classifier(v) ########## # Instantiation ########## def osnet_nas(num_classes=1000, loss='softmax', **kwargs): # standard size (width x1.0) return OSNet( num_classes, blocks=[NASBlock, NASBlock, NASBlock], layers=[2, 2, 2], channels=[64, 256, 384, 512], loss=loss, search_space=[OSBlock, OSBlockINv1, OSBlockINv2, OSBlockINv3], **kwargs ) __NAS_models = {'osnet_nas': osnet_nas} def build_model(name, num_classes=100): avai_models = list(__NAS_models.keys()) if name not in avai_models: raise KeyError( 'Unknown model: {}. Must be one of {}'.format(name, avai_models) ) return __NAS_models[name](num_classes=num_classes) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/OSNet_AIN/softmax_nas.py ================================================ from __future__ import division, print_function, absolute_import from torchreid import metrics from torchreid.engine import Engine from torchreid.losses import CrossEntropyLoss class ImageSoftmaxNASEngine(Engine): def __init__( self, datamanager, model, optimizer, scheduler=None, use_gpu=False, label_smooth=True, mc_iter=1, init_lmda=1., min_lmda=1., lmda_decay_step=20, lmda_decay_rate=0.5, fixed_lmda=False ): super(ImageSoftmaxNASEngine, self).__init__(datamanager, use_gpu) self.mc_iter = mc_iter self.init_lmda = init_lmda self.min_lmda = min_lmda self.lmda_decay_step = lmda_decay_step self.lmda_decay_rate = lmda_decay_rate self.fixed_lmda = fixed_lmda self.model = model self.optimizer = optimizer self.scheduler = scheduler self.register_model('model', model, optimizer, scheduler) self.criterion = CrossEntropyLoss( num_classes=self.datamanager.num_train_pids, use_gpu=self.use_gpu, label_smooth=label_smooth ) def forward_backward(self, data): imgs, pids = self.parse_data_for_train(data) if self.use_gpu: imgs = imgs.cuda() pids = pids.cuda() # softmax temporature if self.fixed_lmda or self.lmda_decay_step == -1: lmda = self.init_lmda else: lmda = self.init_lmda * self.lmda_decay_rate**( self.epoch // self.lmda_decay_step ) if lmda < self.min_lmda: lmda = self.min_lmda for k in range(self.mc_iter): outputs = self.model(imgs, lmda=lmda) loss = self.compute_loss(self.criterion, outputs, pids) self.optimizer.zero_grad() loss.backward() self.optimizer.step() loss_dict = { 'loss': loss.item(), 'acc': metrics.accuracy(outputs, pids)[0].item() } return loss_dict ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/README.md ================================================ Here are some research projects built on [Torchreid](https://arxiv.org/abs/1910.10093). + `OSNet_AIN`: [Learning Generalisable Omni-Scale Representations for Person Re-Identification](https://arxiv.org/abs/1910.06827) + `DML`: [Deep Mutual Learning (CVPR'18)](https://arxiv.org/abs/1706.00384) + `attribute_recognition`: [Omni-Scale Feature Learning for Person Re-Identification (ICCV'19)](https://arxiv.org/abs/1905.00953) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/attribute_recognition/README.md ================================================ # Person Attribute Recognition This code was developed for the experiment of person attribute recognition in [Omni-Scale Feature Learning for Person Re-Identification (ICCV'19)](https://arxiv.org/abs/1905.00953). ## Download data Download the PA-100K dataset from [https://github.com/xh-liu/HydraPlus-Net](https://github.com/xh-liu/HydraPlus-Net), and extract the file under the folder where you store your data (say $DATASET). The folder structure should look like ```bash $DATASET/ pa100k/ data/ # images annotation/ annotation.mat ``` ## Train The training command is provided in `train.sh`. Run `bash train.sh $DATASET` to start training. ## Test To test a pretrained model, add the following two arguments to `train.sh`: `--load-weights $PATH_TO_WEIGHTS --evaluate`. ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/attribute_recognition/datasets/__init__.py ================================================ from __future__ import division, print_function, absolute_import from .pa100k import PA100K __datasets = {'pa100k': PA100K} def init_dataset(name, **kwargs): avai_datasets = list(__datasets.keys()) if name not in avai_datasets: raise ValueError( 'Invalid dataset name. Received "{}", ' 'but expected to be one of {}'.format(name, avai_datasets) ) return __datasets[name](**kwargs) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/attribute_recognition/datasets/dataset.py ================================================ from __future__ import division, print_function, absolute_import import os.path as osp from torchreid.utils import read_image class Dataset(object): def __init__( self, train, val, test, attr_dict, transform=None, mode='train', verbose=True, **kwargs ): self.train = train self.val = val self.test = test self._attr_dict = attr_dict self._num_attrs = len(self.attr_dict) self.transform = transform if mode == 'train': self.data = self.train elif mode == 'val': self.data = self.val else: self.data = self.test if verbose: self.show_summary() @property def num_attrs(self): return self._num_attrs @property def attr_dict(self): return self._attr_dict def __len__(self): return len(self.data) def __getitem__(self, index): img_path, attrs = self.data[index] img = read_image(img_path) if self.transform is not None: img = self.transform(img) return img, attrs, img_path def check_before_run(self, required_files): """Checks if required files exist before going deeper. Args: required_files (str or list): string file name(s). """ if isinstance(required_files, str): required_files = [required_files] for fpath in required_files: if not osp.exists(fpath): raise RuntimeError('"{}" is not found'.format(fpath)) def show_summary(self): num_train = len(self.train) num_val = len(self.val) num_test = len(self.test) num_total = num_train + num_val + num_test print('=> Loaded {}'.format(self.__class__.__name__)) print(" ------------------------------") print(" subset | # images") print(" ------------------------------") print(" train | {:8d}".format(num_train)) print(" val | {:8d}".format(num_val)) print(" test | {:8d}".format(num_test)) print(" ------------------------------") print(" total | {:8d}".format(num_total)) print(" ------------------------------") print(" # attributes: {}".format(len(self.attr_dict))) print(" attributes:") for label, attr in self.attr_dict.items(): print(' {:3d}: {}'.format(label, attr)) print(" ------------------------------") ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/attribute_recognition/datasets/pa100k.py ================================================ from __future__ import division, print_function, absolute_import import numpy as np import os.path as osp from scipy.io import loadmat from .dataset import Dataset class PA100K(Dataset): """Pedestrian attribute dataset. 80k training images + 20k test images. The folder structure should be: pa100k/ data/ # images annotation/ annotation.mat """ dataset_dir = 'pa100k' def __init__(self, root='', **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.data_dir = osp.join(self.dataset_dir, 'data') self.anno_mat_path = osp.join( self.dataset_dir, 'annotation', 'annotation.mat' ) required_files = [self.data_dir, self.anno_mat_path] self.check_before_run(required_files) train, val, test, attr_dict = self.extract_data() super(PA100K, self).__init__(train, val, test, attr_dict, **kwargs) def extract_data(self): # anno_mat is a dictionary with keys: ['test_images_name', 'val_images_name', # 'train_images_name', 'val_label', 'attributes', 'test_label', 'train_label'] anno_mat = loadmat(self.anno_mat_path) def _extract(key_name, key_label): names = anno_mat[key_name] labels = anno_mat[key_label] num_imgs = names.shape[0] data = [] for i in range(num_imgs): name = names[i, 0][0] attrs = labels[i, :].astype(np.float32) img_path = osp.join(self.data_dir, name) data.append((img_path, attrs)) return data train = _extract('train_images_name', 'train_label') val = _extract('val_images_name', 'val_label') test = _extract('test_images_name', 'test_label') attrs = anno_mat['attributes'] attr_dict = {i: str(attr[0][0]) for i, attr in enumerate(attrs)} return train, val, test, attr_dict ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/attribute_recognition/default_parser.py ================================================ from __future__ import print_function, absolute_import import argparse def init_parser(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter ) # ************************************************************ # Datasets # ************************************************************ parser.add_argument( '--root', type=str, default='', required=True, help='root path to data directory' ) parser.add_argument( '-d', '--dataset', type=str, required=True, help='which dataset to choose' ) parser.add_argument( '-j', '--workers', type=int, default=4, help='number of data loading workers (tips: 4 or 8 times number of gpus)' ) parser.add_argument( '--height', type=int, default=256, help='height of an image' ) parser.add_argument( '--width', type=int, default=128, help='width of an image' ) # ************************************************************ # Optimization options # ************************************************************ parser.add_argument( '--optim', type=str, default='adam', help='optimization algorithm (see optimizers.py)' ) parser.add_argument( '--lr', type=float, default=0.0003, help='initial learning rate' ) parser.add_argument( '--weight-decay', type=float, default=5e-04, help='weight decay' ) # sgd parser.add_argument( '--momentum', type=float, default=0.9, help='momentum factor for sgd and rmsprop' ) parser.add_argument( '--sgd-dampening', type=float, default=0, help='sgd\'s dampening for momentum' ) parser.add_argument( '--sgd-nesterov', action='store_true', help='whether to enable sgd\'s Nesterov momentum' ) # rmsprop parser.add_argument( '--rmsprop-alpha', type=float, default=0.99, help='rmsprop\'s smoothing constant' ) # adam/amsgrad parser.add_argument( '--adam-beta1', type=float, default=0.9, help='exponential decay rate for adam\'s first moment' ) parser.add_argument( '--adam-beta2', type=float, default=0.999, help='exponential decay rate for adam\'s second moment' ) # ************************************************************ # Training hyperparameters # ************************************************************ parser.add_argument( '--max-epoch', type=int, default=60, help='maximum epochs to run' ) parser.add_argument( '--start-epoch', type=int, default=0, help='manual epoch number (useful when restart)' ) parser.add_argument( '--batch-size', type=int, default=32, help='batch size' ) parser.add_argument( '--fixbase-epoch', type=int, default=0, help='number of epochs to fix base layers' ) parser.add_argument( '--open-layers', type=str, nargs='+', default=['classifier'], help='open specified layers for training while keeping others frozen' ) parser.add_argument( '--staged-lr', action='store_true', help='set different lr to different layers' ) parser.add_argument( '--new-layers', type=str, nargs='+', default=['classifier'], help='newly added layers with default lr' ) parser.add_argument( '--base-lr-mult', type=float, default=0.1, help='learning rate multiplier for base layers' ) # ************************************************************ # Learning rate scheduler options # ************************************************************ parser.add_argument( '--lr-scheduler', type=str, default='multi_step', help='learning rate scheduler (see lr_schedulers.py)' ) parser.add_argument( '--stepsize', type=int, default=[20, 40], nargs='+', help='stepsize to decay learning rate' ) parser.add_argument( '--gamma', type=float, default=0.1, help='learning rate decay' ) # ************************************************************ # Architecture # ************************************************************ parser.add_argument( '-a', '--arch', type=str, default='', help='model architecture' ) parser.add_argument( '--no-pretrained', action='store_true', help='do not load pretrained weights' ) # ************************************************************ # Loss # ************************************************************ parser.add_argument( '--weighted-bce', action='store_true', help='use weighted BCELoss' ) # ************************************************************ # Test settings # ************************************************************ parser.add_argument( '--load-weights', type=str, default='', help='load pretrained weights' ) parser.add_argument( '--evaluate', action='store_true', help='evaluate only' ) parser.add_argument( '--save-prediction', action='store_true', help='save prediction' ) # ************************************************************ # Miscs # ************************************************************ parser.add_argument( '--print-freq', type=int, default=20, help='print frequency' ) parser.add_argument('--seed', type=int, default=1, help='manual seed') parser.add_argument( '--resume', type=str, default='', metavar='PATH', help='resume from a checkpoint' ) parser.add_argument( '--save-dir', type=str, default='log', help='path to save log and model weights' ) parser.add_argument('--use-cpu', action='store_true', help='use cpu') return parser def optimizer_kwargs(parsed_args): return { 'optim': parsed_args.optim, 'lr': parsed_args.lr, 'weight_decay': parsed_args.weight_decay, 'momentum': parsed_args.momentum, 'sgd_dampening': parsed_args.sgd_dampening, 'sgd_nesterov': parsed_args.sgd_nesterov, 'rmsprop_alpha': parsed_args.rmsprop_alpha, 'adam_beta1': parsed_args.adam_beta1, 'adam_beta2': parsed_args.adam_beta2, 'staged_lr': parsed_args.staged_lr, 'new_layers': parsed_args.new_layers, 'base_lr_mult': parsed_args.base_lr_mult } def lr_scheduler_kwargs(parsed_args): return { 'lr_scheduler': parsed_args.lr_scheduler, 'stepsize': parsed_args.stepsize, 'gamma': parsed_args.gamma } ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/attribute_recognition/main.py ================================================ from __future__ import division, print_function import sys import copy import time import numpy as np import os.path as osp import datetime import warnings import torch import torch.nn as nn import torchreid from torchreid.utils import ( Logger, AverageMeter, check_isfile, open_all_layers, save_checkpoint, set_random_seed, collect_env_info, open_specified_layers, load_pretrained_weights, compute_model_complexity ) from torchreid.data.transforms import ( Resize, Compose, ToTensor, Normalize, Random2DTranslation, RandomHorizontalFlip ) import models import datasets from default_parser import init_parser, optimizer_kwargs, lr_scheduler_kwargs parser = init_parser() args = parser.parse_args() def init_dataset(use_gpu): normalize = Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] ) transform_tr = Compose( [ Random2DTranslation(args.height, args.width, p=0.5), RandomHorizontalFlip(), ToTensor(), normalize ] ) transform_te = Compose( [Resize([args.height, args.width]), ToTensor(), normalize] ) trainset = datasets.init_dataset( args.dataset, root=args.root, transform=transform_tr, mode='train', verbose=True ) valset = datasets.init_dataset( args.dataset, root=args.root, transform=transform_te, mode='val', verbose=False ) testset = datasets.init_dataset( args.dataset, root=args.root, transform=transform_te, mode='test', verbose=False ) num_attrs = trainset.num_attrs attr_dict = trainset.attr_dict trainloader = torch.utils.data.DataLoader( trainset, batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=use_gpu, drop_last=True ) valloader = torch.utils.data.DataLoader( valset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=use_gpu, drop_last=False ) testloader = torch.utils.data.DataLoader( testset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=use_gpu, drop_last=False ) return trainloader, valloader, testloader, num_attrs, attr_dict def main(): global args set_random_seed(args.seed) use_gpu = torch.cuda.is_available() and not args.use_cpu log_name = 'test.log' if args.evaluate else 'train.log' sys.stdout = Logger(osp.join(args.save_dir, log_name)) print('** Arguments **') arg_keys = list(args.__dict__.keys()) arg_keys.sort() for key in arg_keys: print('{}: {}'.format(key, args.__dict__[key])) print('\n') print('Collecting env info ...') print('** System info **\n{}\n'.format(collect_env_info())) if use_gpu: torch.backends.cudnn.benchmark = True else: warnings.warn( 'Currently using CPU, however, GPU is highly recommended' ) dataset_vars = init_dataset(use_gpu) trainloader, valloader, testloader, num_attrs, attr_dict = dataset_vars if args.weighted_bce: print('Use weighted binary cross entropy') print('Computing the weights ...') bce_weights = torch.zeros(num_attrs, dtype=torch.float) for _, attrs, _ in trainloader: bce_weights += attrs.sum(0) # sum along the batch dim bce_weights /= len(trainloader) * args.batch_size print('Sample ratio for each attribute: {}'.format(bce_weights)) bce_weights = torch.exp(-1 * bce_weights) print('BCE weights: {}'.format(bce_weights)) bce_weights = bce_weights.expand(args.batch_size, num_attrs) criterion = nn.BCEWithLogitsLoss(weight=bce_weights) else: print('Use plain binary cross entropy') criterion = nn.BCEWithLogitsLoss() print('Building model: {}'.format(args.arch)) model = models.build_model( args.arch, num_attrs, pretrained=not args.no_pretrained, use_gpu=use_gpu ) num_params, flops = compute_model_complexity( model, (1, 3, args.height, args.width) ) print('Model complexity: params={:,} flops={:,}'.format(num_params, flops)) if args.load_weights and check_isfile(args.load_weights): load_pretrained_weights(model, args.load_weights) if use_gpu: model = nn.DataParallel(model).cuda() criterion = criterion.cuda() if args.evaluate: test(model, testloader, attr_dict, use_gpu) return optimizer = torchreid.optim.build_optimizer( model, **optimizer_kwargs(args) ) scheduler = torchreid.optim.build_lr_scheduler( optimizer, **lr_scheduler_kwargs(args) ) start_epoch = args.start_epoch best_result = -np.inf if args.resume and check_isfile(args.resume): checkpoint = torch.load(args.resume) model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) start_epoch = checkpoint['epoch'] best_result = checkpoint['label_mA'] print('Loaded checkpoint from "{}"'.format(args.resume)) print('- start epoch: {}'.format(start_epoch)) print('- label_mA: {}'.format(best_result)) time_start = time.time() for epoch in range(start_epoch, args.max_epoch): train( epoch, model, criterion, optimizer, scheduler, trainloader, use_gpu ) test_outputs = test(model, testloader, attr_dict, use_gpu) label_mA = test_outputs[0] is_best = label_mA > best_result if is_best: best_result = label_mA save_checkpoint( { 'state_dict': model.state_dict(), 'epoch': epoch + 1, 'label_mA': label_mA, 'optimizer': optimizer.state_dict(), }, args.save_dir, is_best=is_best ) elapsed = round(time.time() - time_start) elapsed = str(datetime.timedelta(seconds=elapsed)) print('Elapsed {}'.format(elapsed)) def train(epoch, model, criterion, optimizer, scheduler, trainloader, use_gpu): losses = AverageMeter() batch_time = AverageMeter() data_time = AverageMeter() model.train() if (epoch + 1) <= args.fixbase_epoch and args.open_layers is not None: print( '* Only train {} (epoch: {}/{})'.format( args.open_layers, epoch + 1, args.fixbase_epoch ) ) open_specified_layers(model, args.open_layers) else: open_all_layers(model) end = time.time() for batch_idx, data in enumerate(trainloader): data_time.update(time.time() - end) imgs, attrs = data[0], data[1] if use_gpu: imgs = imgs.cuda() attrs = attrs.cuda() optimizer.zero_grad() outputs = model(imgs) loss = criterion(outputs, attrs) loss.backward() optimizer.step() batch_time.update(time.time() - end) losses.update(loss.item(), imgs.size(0)) if (batch_idx+1) % args.print_freq == 0: # estimate remaining time num_batches = len(trainloader) eta_seconds = batch_time.avg * ( num_batches - (batch_idx+1) + (args.max_epoch - (epoch+1)) * num_batches ) eta_str = str(datetime.timedelta(seconds=int(eta_seconds))) print( 'Epoch: [{0}/{1}][{2}/{3}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 'Lr {lr:.6f}\t' 'Eta {eta}'.format( epoch + 1, args.max_epoch, batch_idx + 1, len(trainloader), batch_time=batch_time, data_time=data_time, loss=losses, lr=optimizer.param_groups[0]['lr'], eta=eta_str ) ) end = time.time() scheduler.step() @torch.no_grad() def test(model, testloader, attr_dict, use_gpu): batch_time = AverageMeter() model.eval() num_persons = 0 prob_thre = 0.5 ins_acc = 0 ins_prec = 0 ins_rec = 0 mA_history = { 'correct_pos': 0, 'real_pos': 0, 'correct_neg': 0, 'real_neg': 0 } print('Testing ...') for batch_idx, data in enumerate(testloader): imgs, attrs, img_paths = data if use_gpu: imgs = imgs.cuda() end = time.time() orig_outputs = model(imgs) batch_time.update(time.time() - end) orig_outputs = orig_outputs.data.cpu().numpy() attrs = attrs.data.numpy() # transform raw outputs to attributes (binary codes) outputs = copy.deepcopy(orig_outputs) outputs[outputs < prob_thre] = 0 outputs[outputs >= prob_thre] = 1 # compute label-based metric overlaps = outputs * attrs mA_history['correct_pos'] += overlaps.sum(0) mA_history['real_pos'] += attrs.sum(0) inv_overlaps = (1-outputs) * (1-attrs) mA_history['correct_neg'] += inv_overlaps.sum(0) mA_history['real_neg'] += (1 - attrs).sum(0) outputs = outputs.astype(bool) attrs = attrs.astype(bool) # compute instabce-based accuracy intersect = (outputs & attrs).astype(float) union = (outputs | attrs).astype(float) ins_acc += (intersect.sum(1) / union.sum(1)).sum() ins_prec += (intersect.sum(1) / outputs.astype(float).sum(1)).sum() ins_rec += (intersect.sum(1) / attrs.astype(float).sum(1)).sum() num_persons += imgs.size(0) if (batch_idx+1) % args.print_freq == 0: print( 'Processed batch {}/{}'.format(batch_idx + 1, len(testloader)) ) if args.save_prediction: txtfile = open(osp.join(args.save_dir, 'prediction.txt'), 'a') for idx in range(imgs.size(0)): img_path = img_paths[idx] probs = orig_outputs[idx, :] labels = attrs[idx, :] txtfile.write('{}\n'.format(img_path)) txtfile.write('*** Correct prediction ***\n') for attr_idx, (label, prob) in enumerate(zip(labels, probs)): if label: attr_name = attr_dict[attr_idx] info = '{}: {:.1%} '.format(attr_name, prob) txtfile.write(info) txtfile.write('\n*** Incorrect prediction ***\n') for attr_idx, (label, prob) in enumerate(zip(labels, probs)): if not label and prob > 0.5: attr_name = attr_dict[attr_idx] info = '{}: {:.1%} '.format(attr_name, prob) txtfile.write(info) txtfile.write('\n\n') txtfile.close() print( '=> BatchTime(s)/BatchSize(img): {:.4f}/{}'.format( batch_time.avg, args.batch_size ) ) ins_acc /= num_persons ins_prec /= num_persons ins_rec /= num_persons ins_f1 = (2*ins_prec*ins_rec) / (ins_prec+ins_rec) term1 = mA_history['correct_pos'] / mA_history['real_pos'] term2 = mA_history['correct_neg'] / mA_history['real_neg'] label_mA_verbose = (term1+term2) * 0.5 label_mA = label_mA_verbose.mean() print('* Results *') print(' # test persons: {}'.format(num_persons)) print(' (instance-based) accuracy: {:.1%}'.format(ins_acc)) print(' (instance-based) precition: {:.1%}'.format(ins_prec)) print(' (instance-based) recall: {:.1%}'.format(ins_rec)) print(' (instance-based) f1-score: {:.1%}'.format(ins_f1)) print(' (label-based) mean accuracy: {:.1%}'.format(label_mA)) print(' mA for each attribute: {}'.format(label_mA_verbose)) return label_mA, ins_acc, ins_prec, ins_rec, ins_f1 if __name__ == '__main__': main() ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/attribute_recognition/models/__init__.py ================================================ from __future__ import absolute_import from .osnet import * __model_factory = { 'osnet_avgpool': osnet_avgpool, 'osnet_maxpool': osnet_maxpool } def build_model(name, num_classes, pretrained=True, use_gpu=True): avai_models = list(__model_factory.keys()) if name not in avai_models: raise KeyError return __model_factory[name]( num_classes=num_classes, pretrained=pretrained, use_gpu=use_gpu ) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/attribute_recognition/models/osnet.py ================================================ from __future__ import division, absolute_import import torch from torch import nn from torch.nn import functional as F __all__ = ['osnet_avgpool', 'osnet_maxpool'] ########## # Basic layers ########## class ConvLayer(nn.Module): """Convolution layer.""" def __init__( self, in_channels, out_channels, kernel_size, stride=1, padding=0, groups=1 ): super(ConvLayer, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, kernel_size, stride=stride, padding=padding, bias=False, groups=groups ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) x = self.relu(x) return x class Conv1x1(nn.Module): """1x1 convolution.""" def __init__(self, in_channels, out_channels, stride=1, groups=1): super(Conv1x1, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=stride, padding=0, bias=False, groups=groups ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) x = self.relu(x) return x class Conv1x1Linear(nn.Module): """1x1 convolution without non-linearity.""" def __init__(self, in_channels, out_channels, stride=1): super(Conv1x1Linear, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=stride, padding=0, bias=False ) self.bn = nn.BatchNorm2d(out_channels) def forward(self, x): x = self.conv(x) x = self.bn(x) return x class Conv3x3(nn.Module): """3x3 convolution.""" def __init__(self, in_channels, out_channels, stride=1, groups=1): super(Conv3x3, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 3, stride=stride, padding=1, bias=False, groups=groups ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) x = self.relu(x) return x class LightConv3x3(nn.Module): """Lightweight 3x3 convolution. 1x1 (linear) + dw 3x3 (nonlinear). """ def __init__(self, in_channels, out_channels): super(LightConv3x3, self).__init__() self.conv1 = nn.Conv2d( in_channels, out_channels, 1, stride=1, padding=0, bias=False ) self.conv2 = nn.Conv2d( out_channels, out_channels, 3, stride=1, padding=1, bias=False, groups=out_channels ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.bn(x) x = self.relu(x) return x ########## # Building blocks for omni-scale feature learning ########## class ChannelGate(nn.Module): """A mini-network that generates channel-wise gates conditioned on input.""" def __init__( self, in_channels, num_gates=None, return_gates=False, gate_activation='sigmoid', reduction=16, layer_norm=False ): super(ChannelGate, self).__init__() if num_gates is None: num_gates = in_channels self.return_gates = return_gates self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.fc1 = nn.Conv2d( in_channels, in_channels // reduction, kernel_size=1, bias=True, padding=0 ) self.norm1 = None if layer_norm: self.norm1 = nn.LayerNorm((in_channels // reduction, 1, 1)) self.relu = nn.ReLU(inplace=True) self.fc2 = nn.Conv2d( in_channels // reduction, num_gates, kernel_size=1, bias=True, padding=0 ) if gate_activation == 'sigmoid': self.gate_activation = nn.Sigmoid() elif gate_activation == 'relu': self.gate_activation = nn.ReLU(inplace=True) elif gate_activation == 'linear': self.gate_activation = None else: raise RuntimeError( "Unknown gate activation: {}".format(gate_activation) ) def forward(self, x): input = x x = self.global_avgpool(x) x = self.fc1(x) if self.norm1 is not None: x = self.norm1(x) x = self.relu(x) x = self.fc2(x) if self.gate_activation is not None: x = self.gate_activation(x) if self.return_gates: return x return input * x class OSBlock(nn.Module): """Omni-scale feature learning block.""" def __init__(self, in_channels, out_channels, **kwargs): super(OSBlock, self).__init__() mid_channels = out_channels // 4 self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2a = LightConv3x3(mid_channels, mid_channels) self.conv2b = nn.Sequential( LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), ) self.conv2c = nn.Sequential( LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), ) self.conv2d = nn.Sequential( LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), ) self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) def forward(self, x): residual = x x1 = self.conv1(x) x2a = self.conv2a(x1) x2b = self.conv2b(x1) x2c = self.conv2c(x1) x2d = self.conv2d(x1) x2 = self.gate(x2a) + self.gate(x2b) + self.gate(x2c) + self.gate(x2d) x3 = self.conv3(x2) if self.downsample is not None: residual = self.downsample(residual) out = x3 + residual return F.relu(out) ########## # Network architecture ########## class BaseNet(nn.Module): def _make_layer( self, block, layer, in_channels, out_channels, reduce_spatial_size ): layers = [] layers.append(block(in_channels, out_channels)) for i in range(1, layer): layers.append(block(out_channels, out_channels)) if reduce_spatial_size: layers.append( nn.Sequential( Conv1x1(out_channels, out_channels), nn.AvgPool2d(2, stride=2) ) ) return nn.Sequential(*layers) def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): if fc_dims is None or fc_dims < 0: self.feature_dim = input_dim return None if isinstance(fc_dims, int): fc_dims = [fc_dims] layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) class OSNet(BaseNet): def __init__( self, num_classes, blocks, layers, channels, feature_dim=512, loss='softmax', pool='avg', **kwargs ): super(OSNet, self).__init__() num_blocks = len(blocks) assert num_blocks == len(layers) assert num_blocks == len(channels) - 1 self.loss = loss # convolutional backbone self.conv1 = ConvLayer(3, channels[0], 7, stride=2, padding=3) self.maxpool = nn.MaxPool2d(3, stride=2, padding=1) self.conv2 = self._make_layer( blocks[0], layers[0], channels[0], channels[1], reduce_spatial_size=True ) self.conv3 = self._make_layer( blocks[1], layers[1], channels[1], channels[2], reduce_spatial_size=True ) self.conv4 = self._make_layer( blocks[2], layers[2], channels[2], channels[3], reduce_spatial_size=False ) self.conv5 = Conv1x1(channels[3], channels[3]) if pool == 'avg': self.global_pool = nn.AdaptiveAvgPool2d(1) else: self.global_pool = nn.AdaptiveMaxPool2d(1) # fully connected layer self.fc = self._construct_fc_layer( feature_dim, channels[3], dropout_p=None ) # classification layer self.classifier = nn.Linear(self.feature_dim, num_classes) self.init_params() def featuremaps(self, x): x = self.conv1(x) x = self.maxpool(x) x = self.conv2(x) x = self.conv3(x) x = self.conv4(x) x = self.conv5(x) return x def forward(self, x): x = self.featuremaps(x) v = self.global_pool(x) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) y = self.classifier(v) if not self.training: y = torch.sigmoid(y) return y def osnet_avgpool(num_classes=1000, loss='softmax', **kwargs): return OSNet( num_classes, blocks=[OSBlock, OSBlock, OSBlock], layers=[2, 2, 2], channels=[64, 256, 384, 512], loss=loss, pool='avg', **kwargs ) def osnet_maxpool(num_classes=1000, loss='softmax', **kwargs): return OSNet( num_classes, blocks=[OSBlock, OSBlock, OSBlock], layers=[2, 2, 2], channels=[64, 256, 384, 512], loss=loss, pool='max', **kwargs ) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/projects/attribute_recognition/train.sh ================================================ #!/bin/bash # DATASET points to the directory containing pa100k/ DATASET=$1 python main.py \ --root ${DATASET} \ -d pa100k \ -a osnet_maxpool \ --max-epoch 50 \ --stepsize 30 40 \ --batch-size 32 \ --lr 0.065 \ --optim sgd \ --weighted-bce \ --save-dir log/pa100k-osnet_maxpool ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/scripts/default_config.py ================================================ from yacs.config import CfgNode as CN def get_default_config(): cfg = CN() # model cfg.model = CN() cfg.model.name = 'resnet50' cfg.model.pretrained = True # automatically load pretrained model weights if available cfg.model.load_weights = '' # path to model weights cfg.model.resume = '' # path to checkpoint for resume training # data cfg.data = CN() cfg.data.type = 'image' cfg.data.root = 'reid-data' cfg.data.sources = ['market1501'] cfg.data.targets = ['market1501'] cfg.data.workers = 4 # number of data loading workers cfg.data.split_id = 0 # split index cfg.data.height = 256 # image height cfg.data.width = 128 # image width cfg.data.combineall = False # combine train, query and gallery for training cfg.data.transforms = ['random_flip'] # data augmentation cfg.data.k_tfm = 1 # number of times to apply augmentation to an image independently cfg.data.norm_mean = [0.485, 0.456, 0.406] # default is imagenet mean cfg.data.norm_std = [0.229, 0.224, 0.225] # default is imagenet std cfg.data.save_dir = 'log' # path to save log cfg.data.load_train_targets = False # load training set from target dataset # specific datasets cfg.market1501 = CN() cfg.market1501.use_500k_distractors = False # add 500k distractors to the gallery set for market1501 cfg.cuhk03 = CN() cfg.cuhk03.labeled_images = False # use labeled images, if False, use detected images cfg.cuhk03.classic_split = False # use classic split by Li et al. CVPR14 cfg.cuhk03.use_metric_cuhk03 = False # use cuhk03's metric for evaluation # sampler cfg.sampler = CN() cfg.sampler.train_sampler = 'RandomSampler' # sampler for source train loader cfg.sampler.train_sampler_t = 'RandomSampler' # sampler for target train loader cfg.sampler.num_instances = 4 # number of instances per identity for RandomIdentitySampler cfg.sampler.num_cams = 1 # number of cameras to sample in a batch (for RandomDomainSampler) cfg.sampler.num_datasets = 1 # number of datasets to sample in a batch (for RandomDatasetSampler) # video reid setting cfg.video = CN() cfg.video.seq_len = 15 # number of images to sample in a tracklet cfg.video.sample_method = 'evenly' # how to sample images from a tracklet cfg.video.pooling_method = 'avg' # how to pool features over a tracklet # train cfg.train = CN() cfg.train.optim = 'adam' cfg.train.lr = 0.0003 cfg.train.weight_decay = 5e-4 cfg.train.max_epoch = 60 cfg.train.start_epoch = 0 cfg.train.batch_size = 32 cfg.train.fixbase_epoch = 0 # number of epochs to fix base layers cfg.train.open_layers = [ 'classifier' ] # layers for training while keeping others frozen cfg.train.staged_lr = False # set different lr to different layers cfg.train.new_layers = ['classifier'] # newly added layers with default lr cfg.train.base_lr_mult = 0.1 # learning rate multiplier for base layers cfg.train.lr_scheduler = 'single_step' cfg.train.stepsize = [20] # stepsize to decay learning rate cfg.train.gamma = 0.1 # learning rate decay multiplier cfg.train.print_freq = 20 # print frequency cfg.train.seed = 1 # random seed # optimizer cfg.sgd = CN() cfg.sgd.momentum = 0.9 # momentum factor for sgd and rmsprop cfg.sgd.dampening = 0. # dampening for momentum cfg.sgd.nesterov = False # Nesterov momentum cfg.rmsprop = CN() cfg.rmsprop.alpha = 0.99 # smoothing constant cfg.adam = CN() cfg.adam.beta1 = 0.9 # exponential decay rate for first moment cfg.adam.beta2 = 0.999 # exponential decay rate for second moment # loss cfg.loss = CN() cfg.loss.name = 'softmax' cfg.loss.softmax = CN() cfg.loss.softmax.label_smooth = True # use label smoothing regularizer cfg.loss.triplet = CN() cfg.loss.triplet.margin = 0.3 # distance margin cfg.loss.triplet.weight_t = 1. # weight to balance hard triplet loss cfg.loss.triplet.weight_x = 0. # weight to balance cross entropy loss # test cfg.test = CN() cfg.test.batch_size = 100 cfg.test.dist_metric = 'euclidean' # distance metric, ['euclidean', 'cosine'] cfg.test.normalize_feature = False # normalize feature vectors before computing distance cfg.test.ranks = [1, 5, 10, 20] # cmc ranks cfg.test.evaluate = False # test only cfg.test.eval_freq = -1 # evaluation frequency (-1 means to only test after training) cfg.test.start_eval = 0 # start to evaluate after a specific epoch cfg.test.rerank = False # use person re-ranking cfg.test.visrank = False # visualize ranked results (only available when cfg.test.evaluate=True) cfg.test.visrank_topk = 10 # top-k ranks to visualize return cfg def imagedata_kwargs(cfg): return { 'root': cfg.data.root, 'sources': cfg.data.sources, 'targets': cfg.data.targets, 'height': cfg.data.height, 'width': cfg.data.width, 'transforms': cfg.data.transforms, 'k_tfm': cfg.data.k_tfm, 'norm_mean': cfg.data.norm_mean, 'norm_std': cfg.data.norm_std, 'use_gpu': cfg.use_gpu, 'split_id': cfg.data.split_id, 'combineall': cfg.data.combineall, 'load_train_targets': cfg.data.load_train_targets, 'batch_size_train': cfg.train.batch_size, 'batch_size_test': cfg.test.batch_size, 'workers': cfg.data.workers, 'num_instances': cfg.sampler.num_instances, 'num_cams': cfg.sampler.num_cams, 'num_datasets': cfg.sampler.num_datasets, 'train_sampler': cfg.sampler.train_sampler, 'train_sampler_t': cfg.sampler.train_sampler_t, # image dataset specific 'cuhk03_labeled': cfg.cuhk03.labeled_images, 'cuhk03_classic_split': cfg.cuhk03.classic_split, 'market1501_500k': cfg.market1501.use_500k_distractors, } def videodata_kwargs(cfg): return { 'root': cfg.data.root, 'sources': cfg.data.sources, 'targets': cfg.data.targets, 'height': cfg.data.height, 'width': cfg.data.width, 'transforms': cfg.data.transforms, 'norm_mean': cfg.data.norm_mean, 'norm_std': cfg.data.norm_std, 'use_gpu': cfg.use_gpu, 'split_id': cfg.data.split_id, 'combineall': cfg.data.combineall, 'batch_size_train': cfg.train.batch_size, 'batch_size_test': cfg.test.batch_size, 'workers': cfg.data.workers, 'num_instances': cfg.sampler.num_instances, 'num_cams': cfg.sampler.num_cams, 'num_datasets': cfg.sampler.num_datasets, 'train_sampler': cfg.sampler.train_sampler, # video dataset specific 'seq_len': cfg.video.seq_len, 'sample_method': cfg.video.sample_method } def optimizer_kwargs(cfg): return { 'optim': cfg.train.optim, 'lr': cfg.train.lr, 'weight_decay': cfg.train.weight_decay, 'momentum': cfg.sgd.momentum, 'sgd_dampening': cfg.sgd.dampening, 'sgd_nesterov': cfg.sgd.nesterov, 'rmsprop_alpha': cfg.rmsprop.alpha, 'adam_beta1': cfg.adam.beta1, 'adam_beta2': cfg.adam.beta2, 'staged_lr': cfg.train.staged_lr, 'new_layers': cfg.train.new_layers, 'base_lr_mult': cfg.train.base_lr_mult } def lr_scheduler_kwargs(cfg): return { 'lr_scheduler': cfg.train.lr_scheduler, 'stepsize': cfg.train.stepsize, 'gamma': cfg.train.gamma, 'max_epoch': cfg.train.max_epoch } def engine_run_kwargs(cfg): return { 'save_dir': cfg.data.save_dir, 'max_epoch': cfg.train.max_epoch, 'start_epoch': cfg.train.start_epoch, 'fixbase_epoch': cfg.train.fixbase_epoch, 'open_layers': cfg.train.open_layers, 'start_eval': cfg.test.start_eval, 'eval_freq': cfg.test.eval_freq, 'test_only': cfg.test.evaluate, 'print_freq': cfg.train.print_freq, 'dist_metric': cfg.test.dist_metric, 'normalize_feature': cfg.test.normalize_feature, 'visrank': cfg.test.visrank, 'visrank_topk': cfg.test.visrank_topk, 'use_metric_cuhk03': cfg.cuhk03.use_metric_cuhk03, 'ranks': cfg.test.ranks, 'rerank': cfg.test.rerank } ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/scripts/main.py ================================================ import sys import time import os.path as osp import argparse import torch import torch.nn as nn import torchreid from torchreid.utils import ( Logger, check_isfile, set_random_seed, collect_env_info, resume_from_checkpoint, load_pretrained_weights, compute_model_complexity ) from default_config import ( imagedata_kwargs, optimizer_kwargs, videodata_kwargs, engine_run_kwargs, get_default_config, lr_scheduler_kwargs ) def build_datamanager(cfg): if cfg.data.type == 'image': return torchreid.data.ImageDataManager(**imagedata_kwargs(cfg)) else: return torchreid.data.VideoDataManager(**videodata_kwargs(cfg)) def build_engine(cfg, datamanager, model, optimizer, scheduler): if cfg.data.type == 'image': if cfg.loss.name == 'softmax': engine = torchreid.engine.ImageSoftmaxEngine( datamanager, model, optimizer=optimizer, scheduler=scheduler, use_gpu=cfg.use_gpu, label_smooth=cfg.loss.softmax.label_smooth ) else: engine = torchreid.engine.ImageTripletEngine( datamanager, model, optimizer=optimizer, margin=cfg.loss.triplet.margin, weight_t=cfg.loss.triplet.weight_t, weight_x=cfg.loss.triplet.weight_x, scheduler=scheduler, use_gpu=cfg.use_gpu, label_smooth=cfg.loss.softmax.label_smooth ) else: if cfg.loss.name == 'softmax': engine = torchreid.engine.VideoSoftmaxEngine( datamanager, model, optimizer=optimizer, scheduler=scheduler, use_gpu=cfg.use_gpu, label_smooth=cfg.loss.softmax.label_smooth, pooling_method=cfg.video.pooling_method ) else: engine = torchreid.engine.VideoTripletEngine( datamanager, model, optimizer=optimizer, margin=cfg.loss.triplet.margin, weight_t=cfg.loss.triplet.weight_t, weight_x=cfg.loss.triplet.weight_x, scheduler=scheduler, use_gpu=cfg.use_gpu, label_smooth=cfg.loss.softmax.label_smooth ) return engine def reset_config(cfg, args): if args.root: cfg.data.root = args.root if args.sources: cfg.data.sources = args.sources if args.targets: cfg.data.targets = args.targets if args.transforms: cfg.data.transforms = args.transforms def check_cfg(cfg): if cfg.loss.name == 'triplet' and cfg.loss.triplet.weight_x == 0: assert cfg.train.fixbase_epoch == 0, \ 'The output of classifier is not included in the computational graph' def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument( '--config-file', type=str, default='', help='path to config file' ) parser.add_argument( '-s', '--sources', type=str, nargs='+', help='source datasets (delimited by space)' ) parser.add_argument( '-t', '--targets', type=str, nargs='+', help='target datasets (delimited by space)' ) parser.add_argument( '--transforms', type=str, nargs='+', help='data augmentation' ) parser.add_argument( '--root', type=str, default='', help='path to data root' ) parser.add_argument( 'opts', default=None, nargs=argparse.REMAINDER, help='Modify config options using the command-line' ) args = parser.parse_args() cfg = get_default_config() cfg.use_gpu = torch.cuda.is_available() if args.config_file: cfg.merge_from_file(args.config_file) reset_config(cfg, args) cfg.merge_from_list(args.opts) set_random_seed(cfg.train.seed) check_cfg(cfg) log_name = 'test.log' if cfg.test.evaluate else 'train.log' log_name += time.strftime('-%Y-%m-%d-%H-%M-%S') sys.stdout = Logger(osp.join(cfg.data.save_dir, log_name)) print('Show configuration\n{}\n'.format(cfg)) print('Collecting env info ...') print('** System info **\n{}\n'.format(collect_env_info())) if cfg.use_gpu: torch.backends.cudnn.benchmark = True datamanager = build_datamanager(cfg) print('Building model: {}'.format(cfg.model.name)) model = torchreid.models.build_model( name=cfg.model.name, num_classes=datamanager.num_train_pids, loss=cfg.loss.name, pretrained=cfg.model.pretrained, use_gpu=cfg.use_gpu ) num_params, flops = compute_model_complexity( model, (1, 3, cfg.data.height, cfg.data.width) ) print('Model complexity: params={:,} flops={:,}'.format(num_params, flops)) if cfg.model.load_weights and check_isfile(cfg.model.load_weights): load_pretrained_weights(model, cfg.model.load_weights) if cfg.use_gpu: model = nn.DataParallel(model).cuda() optimizer = torchreid.optim.build_optimizer(model, **optimizer_kwargs(cfg)) scheduler = torchreid.optim.build_lr_scheduler( optimizer, **lr_scheduler_kwargs(cfg) ) if cfg.model.resume and check_isfile(cfg.model.resume): cfg.train.start_epoch = resume_from_checkpoint( cfg.model.resume, model, optimizer=optimizer, scheduler=scheduler ) print( 'Building {}-engine for {}-reid'.format(cfg.loss.name, cfg.data.type) ) engine = build_engine(cfg, datamanager, model, optimizer, scheduler) engine.run(**engine_run_kwargs(cfg)) if __name__ == '__main__': main() ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/setup.py ================================================ import numpy as np import os.path as osp from setuptools import setup, find_packages from distutils.extension import Extension from Cython.Build import cythonize def readme(): with open('README.rst') as f: content = f.read() return content def find_version(): version_file = 'torchreid/__init__.py' with open(version_file, 'r') as f: exec(compile(f.read(), version_file, 'exec')) return locals()['__version__'] def numpy_include(): try: numpy_include = np.get_include() except AttributeError: numpy_include = np.get_numpy_include() return numpy_include ext_modules = [ Extension( 'torchreid.metrics.rank_cylib.rank_cy', ['torchreid/metrics/rank_cylib/rank_cy.pyx'], include_dirs=[numpy_include()], ) ] def get_requirements(filename='requirements.txt'): here = osp.dirname(osp.realpath(__file__)) with open(osp.join(here, filename), 'r') as f: requires = [line.replace('\n', '') for line in f.readlines()] return requires setup( name='torchreid', version=find_version(), description='A library for deep learning person re-ID in PyTorch', author='Kaiyang Zhou', license='MIT', long_description=readme(), url='https://github.com/KaiyangZhou/deep-person-reid', packages=find_packages(), install_requires=get_requirements(), keywords=['Person Re-Identification', 'Deep Learning', 'Computer Vision'], ext_modules=cythonize(ext_modules) ) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/tools/compute_mean_std.py ================================================ """ Compute channel-wise mean and standard deviation of a dataset. Usage: $ python compute_mean_std.py DATASET_ROOT DATASET_KEY - The first argument points to the root path where you put the datasets. - The second argument means the specific dataset key. For instance, your datasets are put under $DATA and you wanna compute the statistics of Market1501, do $ python compute_mean_std.py $DATA market1501 """ import argparse import torchreid def main(): parser = argparse.ArgumentParser() parser.add_argument('root', type=str) parser.add_argument('sources', type=str) args = parser.parse_args() datamanager = torchreid.data.ImageDataManager( root=args.root, sources=args.sources, targets=None, height=256, width=128, batch_size_train=100, batch_size_test=100, transforms=None, norm_mean=[0., 0., 0.], norm_std=[1., 1., 1.], train_sampler='SequentialSampler' ) train_loader = datamanager.train_loader print('Computing mean and std ...') mean = 0. std = 0. n_samples = 0. for data in train_loader: data = data['img'] batch_size = data.size(0) data = data.view(batch_size, data.size(1), -1) mean += data.mean(2).sum(0) std += data.std(2).sum(0) n_samples += batch_size mean /= n_samples std /= n_samples print('Mean: {}'.format(mean)) print('Std: {}'.format(std)) if __name__ == '__main__': main() ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/tools/parse_test_res.py ================================================ """ This script aims to automate the process of calculating average results stored in the test.log files over multiple splits. How to use: For example, you have done evaluation over 20 splits on VIPeR, leading to the following file structure log/ eval_viper/ split_0/ test.log-xxxx split_1/ test.log-xxxx split_2/ test.log-xxxx ... You can run the following command in your terminal to get the average performance: $ python tools/parse_test_res.py log/eval_viper """ import os import re import glob import numpy as np import argparse from collections import defaultdict from torchreid.utils import check_isfile, listdir_nohidden def parse_file(filepath, regex_mAP, regex_r1, regex_r5, regex_r10, regex_r20): results = {} with open(filepath, 'r') as f: lines = f.readlines() for line in lines: line = line.strip() match_mAP = regex_mAP.search(line) if match_mAP: mAP = float(match_mAP.group(1)) results['mAP'] = mAP match_r1 = regex_r1.search(line) if match_r1: r1 = float(match_r1.group(1)) results['r1'] = r1 match_r5 = regex_r5.search(line) if match_r5: r5 = float(match_r5.group(1)) results['r5'] = r5 match_r10 = regex_r10.search(line) if match_r10: r10 = float(match_r10.group(1)) results['r10'] = r10 match_r20 = regex_r20.search(line) if match_r20: r20 = float(match_r20.group(1)) results['r20'] = r20 return results def main(args): regex_mAP = re.compile(r'mAP: ([\.\deE+-]+)%') regex_r1 = re.compile(r'Rank-1 : ([\.\deE+-]+)%') regex_r5 = re.compile(r'Rank-5 : ([\.\deE+-]+)%') regex_r10 = re.compile(r'Rank-10 : ([\.\deE+-]+)%') regex_r20 = re.compile(r'Rank-20 : ([\.\deE+-]+)%') final_res = defaultdict(list) directories = listdir_nohidden(args.directory, sort=True) num_dirs = len(directories) for directory in directories: fullpath = os.path.join(args.directory, directory) filepath = glob.glob(os.path.join(fullpath, 'test.log*'))[0] check_isfile(filepath) print(f'Parsing {filepath}') res = parse_file( filepath, regex_mAP, regex_r1, regex_r5, regex_r10, regex_r20 ) for key, value in res.items(): final_res[key].append(value) print('Finished parsing') print(f'The average results over {num_dirs} splits are shown below') for key, values in final_res.items(): mean_val = np.mean(values) print(f'{key}: {mean_val:.1f}') if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('directory', type=str, help='Path to directory') args = parser.parse_args() main(args) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/tools/visualize_actmap.py ================================================ """Visualizes CNN activation maps to see where the CNN focuses on to extract features. Reference: - Zagoruyko and Komodakis. Paying more attention to attention: Improving the performance of convolutional neural networks via attention transfer. ICLR, 2017 - Zhou et al. Omni-Scale Feature Learning for Person Re-Identification. ICCV, 2019. """ import numpy as np import os.path as osp import argparse import cv2 import torch from torch.nn import functional as F import torchreid from torchreid.utils import ( check_isfile, mkdir_if_missing, load_pretrained_weights ) IMAGENET_MEAN = [0.485, 0.456, 0.406] IMAGENET_STD = [0.229, 0.224, 0.225] GRID_SPACING = 10 @torch.no_grad() def visactmap( model, test_loader, save_dir, width, height, use_gpu, img_mean=None, img_std=None ): if img_mean is None or img_std is None: # use imagenet mean and std img_mean = IMAGENET_MEAN img_std = IMAGENET_STD model.eval() for target in list(test_loader.keys()): data_loader = test_loader[target]['query'] # only process query images # original images and activation maps are saved individually actmap_dir = osp.join(save_dir, 'actmap_' + target) mkdir_if_missing(actmap_dir) print('Visualizing activation maps for {} ...'.format(target)) for batch_idx, data in enumerate(data_loader): imgs, paths = data['img'], data['impath'] if use_gpu: imgs = imgs.cuda() # forward to get convolutional feature maps try: outputs = model(imgs, return_featuremaps=True) except TypeError: raise TypeError( 'forward() got unexpected keyword argument "return_featuremaps". ' 'Please add return_featuremaps as an input argument to forward(). When ' 'return_featuremaps=True, return feature maps only.' ) if outputs.dim() != 4: raise ValueError( 'The model output is supposed to have ' 'shape of (b, c, h, w), i.e. 4 dimensions, but got {} dimensions. ' 'Please make sure you set the model output at eval mode ' 'to be the last convolutional feature maps'.format( outputs.dim() ) ) # compute activation maps outputs = (outputs**2).sum(1) b, h, w = outputs.size() outputs = outputs.view(b, h * w) outputs = F.normalize(outputs, p=2, dim=1) outputs = outputs.view(b, h, w) if use_gpu: imgs, outputs = imgs.cpu(), outputs.cpu() for j in range(outputs.size(0)): # get image name path = paths[j] imname = osp.basename(osp.splitext(path)[0]) # RGB image img = imgs[j, ...] for t, m, s in zip(img, img_mean, img_std): t.mul_(s).add_(m).clamp_(0, 1) img_np = np.uint8(np.floor(img.numpy() * 255)) img_np = img_np.transpose((1, 2, 0)) # (c, h, w) -> (h, w, c) # activation map am = outputs[j, ...].numpy() am = cv2.resize(am, (width, height)) am = 255 * (am - np.min(am)) / ( np.max(am) - np.min(am) + 1e-12 ) am = np.uint8(np.floor(am)) am = cv2.applyColorMap(am, cv2.COLORMAP_JET) # overlapped overlapped = img_np*0.3 + am*0.7 overlapped[overlapped > 255] = 255 overlapped = overlapped.astype(np.uint8) # save images in a single figure (add white spacing between images) # from left to right: original image, activation map, overlapped image grid_img = 255 * np.ones( (height, 3*width + 2*GRID_SPACING, 3), dtype=np.uint8 ) grid_img[:, :width, :] = img_np[:, :, ::-1] grid_img[:, width + GRID_SPACING:2*width + GRID_SPACING, :] = am grid_img[:, 2*width + 2*GRID_SPACING:, :] = overlapped cv2.imwrite(osp.join(actmap_dir, imname + '.jpg'), grid_img) if (batch_idx+1) % 10 == 0: print( '- done batch {}/{}'.format( batch_idx + 1, len(data_loader) ) ) def main(): parser = argparse.ArgumentParser() parser.add_argument('--root', type=str) parser.add_argument('-d', '--dataset', type=str, default='market1501') parser.add_argument('-m', '--model', type=str, default='osnet_x1_0') parser.add_argument('--weights', type=str) parser.add_argument('--save-dir', type=str, default='log') parser.add_argument('--height', type=int, default=256) parser.add_argument('--width', type=int, default=128) args = parser.parse_args() use_gpu = torch.cuda.is_available() datamanager = torchreid.data.ImageDataManager( root=args.root, sources=args.dataset, height=args.height, width=args.width, batch_size_train=100, batch_size_test=100, transforms=None, train_sampler='SequentialSampler' ) test_loader = datamanager.test_loader model = torchreid.models.build_model( name=args.model, num_classes=datamanager.num_train_pids, use_gpu=use_gpu ) if use_gpu: model = model.cuda() if args.weights and check_isfile(args.weights): load_pretrained_weights(model, args.weights) visactmap( model, test_loader, args.save_dir, args.width, args.height, use_gpu ) if __name__ == '__main__': main() ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/__init__.py ================================================ from __future__ import print_function, absolute_import from torchreid import data, optim, utils, engine, losses, models, metrics __version__ = '1.4.0' __author__ = 'Kaiyang Zhou' __homepage__ = 'https://kaiyangzhou.github.io/' __description__ = 'Deep learning person re-identification in PyTorch' __url__ = 'https://github.com/KaiyangZhou/deep-person-reid' ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/__init__.py ================================================ from __future__ import print_function, absolute_import from .datasets import ( Dataset, ImageDataset, VideoDataset, register_image_dataset, register_video_dataset ) from .datamanager import ImageDataManager, VideoDataManager ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datamanager.py ================================================ from __future__ import division, print_function, absolute_import import torch from torchreid.data.sampler import build_train_sampler from torchreid.data.datasets import init_image_dataset, init_video_dataset from torchreid.data.transforms import build_transforms class DataManager(object): r"""Base data manager. Args: sources (str or list): source dataset(s). targets (str or list, optional): target dataset(s). If not given, it equals to ``sources``. height (int, optional): target image height. Default is 256. width (int, optional): target image width. Default is 128. transforms (str or list of str, optional): transformations applied to model training. Default is 'random_flip'. norm_mean (list or None, optional): data mean. Default is None (use imagenet mean). norm_std (list or None, optional): data std. Default is None (use imagenet std). use_gpu (bool, optional): use gpu. Default is True. """ def __init__( self, sources=None, targets=None, height=256, width=128, transforms='random_flip', norm_mean=None, norm_std=None, use_gpu=False ): self.sources = sources self.targets = targets self.height = height self.width = width if self.sources is None: raise ValueError('sources must not be None') if isinstance(self.sources, str): self.sources = [self.sources] if self.targets is None: self.targets = self.sources if isinstance(self.targets, str): self.targets = [self.targets] self.transform_tr, self.transform_te = build_transforms( self.height, self.width, transforms=transforms, norm_mean=norm_mean, norm_std=norm_std ) self.use_gpu = (torch.cuda.is_available() and use_gpu) @property def num_train_pids(self): """Returns the number of training person identities.""" return self._num_train_pids @property def num_train_cams(self): """Returns the number of training cameras.""" return self._num_train_cams def fetch_test_loaders(self, name): """Returns query and gallery of a test dataset, each containing tuples of (img_path(s), pid, camid). Args: name (str): dataset name. """ query_loader = self.test_dataset[name]['query'] gallery_loader = self.test_dataset[name]['gallery'] return query_loader, gallery_loader def preprocess_pil_img(self, img): """Transforms a PIL image to torch tensor for testing.""" return self.transform_te(img) class ImageDataManager(DataManager): r"""Image data manager. Args: root (str): root path to datasets. sources (str or list): source dataset(s). targets (str or list, optional): target dataset(s). If not given, it equals to ``sources``. height (int, optional): target image height. Default is 256. width (int, optional): target image width. Default is 128. transforms (str or list of str, optional): transformations applied to model training. Default is 'random_flip'. k_tfm (int): number of times to apply augmentation to an image independently. If k_tfm > 1, the transform function will be applied k_tfm times to an image. This variable will only be useful for training and is currently valid for image datasets only. norm_mean (list or None, optional): data mean. Default is None (use imagenet mean). norm_std (list or None, optional): data std. Default is None (use imagenet std). use_gpu (bool, optional): use gpu. Default is True. split_id (int, optional): split id (*0-based*). Default is 0. combineall (bool, optional): combine train, query and gallery in a dataset for training. Default is False. load_train_targets (bool, optional): construct train-loader for target datasets. Default is False. This is useful for domain adaptation research. batch_size_train (int, optional): number of images in a training batch. Default is 32. batch_size_test (int, optional): number of images in a test batch. Default is 32. workers (int, optional): number of workers. Default is 4. num_instances (int, optional): number of instances per identity in a batch. Default is 4. num_cams (int, optional): number of cameras to sample in a batch (when using ``RandomDomainSampler``). Default is 1. num_datasets (int, optional): number of datasets to sample in a batch (when using ``RandomDatasetSampler``). Default is 1. train_sampler (str, optional): sampler. Default is RandomSampler. train_sampler_t (str, optional): sampler for target train loader. Default is RandomSampler. cuhk03_labeled (bool, optional): use cuhk03 labeled images. Default is False (defaul is to use detected images). cuhk03_classic_split (bool, optional): use the classic split in cuhk03. Default is False. market1501_500k (bool, optional): add 500K distractors to the gallery set in market1501. Default is False. Examples:: datamanager = torchreid.data.ImageDataManager( root='path/to/reid-data', sources='market1501', height=256, width=128, batch_size_train=32, batch_size_test=100 ) # return train loader of source data train_loader = datamanager.train_loader # return test loader of target data test_loader = datamanager.test_loader # return train loader of target data train_loader_t = datamanager.train_loader_t """ data_type = 'image' def __init__( self, root='', sources=None, targets=None, height=256, width=128, transforms='random_flip', k_tfm=1, norm_mean=None, norm_std=None, use_gpu=True, split_id=0, combineall=False, load_train_targets=False, batch_size_train=32, batch_size_test=32, workers=4, num_instances=4, num_cams=1, num_datasets=1, train_sampler='RandomSampler', train_sampler_t='RandomSampler', cuhk03_labeled=False, cuhk03_classic_split=False, market1501_500k=False ): super(ImageDataManager, self).__init__( sources=sources, targets=targets, height=height, width=width, transforms=transforms, norm_mean=norm_mean, norm_std=norm_std, use_gpu=use_gpu ) print('=> Loading train (source) dataset') trainset = [] for name in self.sources: trainset_ = init_image_dataset( name, transform=self.transform_tr, k_tfm=k_tfm, mode='train', combineall=combineall, root=root, split_id=split_id, cuhk03_labeled=cuhk03_labeled, cuhk03_classic_split=cuhk03_classic_split, market1501_500k=market1501_500k ) trainset.append(trainset_) trainset = sum(trainset) self._num_train_pids = trainset.num_train_pids self._num_train_cams = trainset.num_train_cams self.train_loader = torch.utils.data.DataLoader( trainset, sampler=build_train_sampler( trainset.train, train_sampler, batch_size=batch_size_train, num_instances=num_instances, num_cams=num_cams, num_datasets=num_datasets ), batch_size=batch_size_train, shuffle=False, num_workers=workers, pin_memory=self.use_gpu, drop_last=True ) self.train_loader_t = None if load_train_targets: # check if sources and targets are identical assert len(set(self.sources) & set(self.targets)) == 0, \ 'sources={} and targets={} must not have overlap'.format(self.sources, self.targets) print('=> Loading train (target) dataset') trainset_t = [] for name in self.targets: trainset_t_ = init_image_dataset( name, transform=self.transform_tr, k_tfm=k_tfm, mode='train', combineall=False, # only use the training data root=root, split_id=split_id, cuhk03_labeled=cuhk03_labeled, cuhk03_classic_split=cuhk03_classic_split, market1501_500k=market1501_500k ) trainset_t.append(trainset_t_) trainset_t = sum(trainset_t) self.train_loader_t = torch.utils.data.DataLoader( trainset_t, sampler=build_train_sampler( trainset_t.train, train_sampler_t, batch_size=batch_size_train, num_instances=num_instances, num_cams=num_cams, num_datasets=num_datasets ), batch_size=batch_size_train, shuffle=False, num_workers=workers, pin_memory=self.use_gpu, drop_last=True ) print('=> Loading test (target) dataset') self.test_loader = { name: { 'query': None, 'gallery': None } for name in self.targets } self.test_dataset = { name: { 'query': None, 'gallery': None } for name in self.targets } for name in self.targets: # build query loader queryset = init_image_dataset( name, transform=self.transform_te, mode='query', combineall=combineall, root=root, split_id=split_id, cuhk03_labeled=cuhk03_labeled, cuhk03_classic_split=cuhk03_classic_split, market1501_500k=market1501_500k ) self.test_loader[name]['query'] = torch.utils.data.DataLoader( queryset, batch_size=batch_size_test, shuffle=False, num_workers=workers, pin_memory=self.use_gpu, drop_last=False ) # build gallery loader galleryset = init_image_dataset( name, transform=self.transform_te, mode='gallery', combineall=combineall, verbose=False, root=root, split_id=split_id, cuhk03_labeled=cuhk03_labeled, cuhk03_classic_split=cuhk03_classic_split, market1501_500k=market1501_500k ) self.test_loader[name]['gallery'] = torch.utils.data.DataLoader( galleryset, batch_size=batch_size_test, shuffle=False, num_workers=workers, pin_memory=self.use_gpu, drop_last=False ) self.test_dataset[name]['query'] = queryset.query self.test_dataset[name]['gallery'] = galleryset.gallery print('\n') print(' **************** Summary ****************') print(' source : {}'.format(self.sources)) print(' # source datasets : {}'.format(len(self.sources))) print(' # source ids : {}'.format(self.num_train_pids)) print(' # source images : {}'.format(len(trainset))) print(' # source cameras : {}'.format(self.num_train_cams)) if load_train_targets: print( ' # target images : {} (unlabeled)'.format(len(trainset_t)) ) print(' target : {}'.format(self.targets)) print(' *****************************************') print('\n') class VideoDataManager(DataManager): r"""Video data manager. Args: root (str): root path to datasets. sources (str or list): source dataset(s). targets (str or list, optional): target dataset(s). If not given, it equals to ``sources``. height (int, optional): target image height. Default is 256. width (int, optional): target image width. Default is 128. transforms (str or list of str, optional): transformations applied to model training. Default is 'random_flip'. norm_mean (list or None, optional): data mean. Default is None (use imagenet mean). norm_std (list or None, optional): data std. Default is None (use imagenet std). use_gpu (bool, optional): use gpu. Default is True. split_id (int, optional): split id (*0-based*). Default is 0. combineall (bool, optional): combine train, query and gallery in a dataset for training. Default is False. batch_size_train (int, optional): number of tracklets in a training batch. Default is 3. batch_size_test (int, optional): number of tracklets in a test batch. Default is 3. workers (int, optional): number of workers. Default is 4. num_instances (int, optional): number of instances per identity in a batch. Default is 4. num_cams (int, optional): number of cameras to sample in a batch (when using ``RandomDomainSampler``). Default is 1. num_datasets (int, optional): number of datasets to sample in a batch (when using ``RandomDatasetSampler``). Default is 1. train_sampler (str, optional): sampler. Default is RandomSampler. seq_len (int, optional): how many images to sample in a tracklet. Default is 15. sample_method (str, optional): how to sample images in a tracklet. Default is "evenly". Choices are ["evenly", "random", "all"]. "evenly" and "random" will sample ``seq_len`` images in a tracklet while "all" samples all images in a tracklet, where the batch size needs to be set to 1. Examples:: datamanager = torchreid.data.VideoDataManager( root='path/to/reid-data', sources='mars', height=256, width=128, batch_size_train=3, batch_size_test=3, seq_len=15, sample_method='evenly' ) # return train loader of source data train_loader = datamanager.train_loader # return test loader of target data test_loader = datamanager.test_loader .. note:: The current implementation only supports image-like training. Therefore, each image in a sampled tracklet will undergo independent transformation functions. To achieve tracklet-aware training, you need to modify the transformation functions for video reid such that each function applies the same operation to all images in a tracklet to keep consistency. """ data_type = 'video' def __init__( self, root='', sources=None, targets=None, height=256, width=128, transforms='random_flip', norm_mean=None, norm_std=None, use_gpu=True, split_id=0, combineall=False, batch_size_train=3, batch_size_test=3, workers=4, num_instances=4, num_cams=1, num_datasets=1, train_sampler='RandomSampler', seq_len=15, sample_method='evenly' ): super(VideoDataManager, self).__init__( sources=sources, targets=targets, height=height, width=width, transforms=transforms, norm_mean=norm_mean, norm_std=norm_std, use_gpu=use_gpu ) print('=> Loading train (source) dataset') trainset = [] for name in self.sources: trainset_ = init_video_dataset( name, transform=self.transform_tr, mode='train', combineall=combineall, root=root, split_id=split_id, seq_len=seq_len, sample_method=sample_method ) trainset.append(trainset_) trainset = sum(trainset) self._num_train_pids = trainset.num_train_pids self._num_train_cams = trainset.num_train_cams train_sampler = build_train_sampler( trainset.train, train_sampler, batch_size=batch_size_train, num_instances=num_instances, num_cams=num_cams, num_datasets=num_datasets ) self.train_loader = torch.utils.data.DataLoader( trainset, sampler=train_sampler, batch_size=batch_size_train, shuffle=False, num_workers=workers, pin_memory=self.use_gpu, drop_last=True ) print('=> Loading test (target) dataset') self.test_loader = { name: { 'query': None, 'gallery': None } for name in self.targets } self.test_dataset = { name: { 'query': None, 'gallery': None } for name in self.targets } for name in self.targets: # build query loader queryset = init_video_dataset( name, transform=self.transform_te, mode='query', combineall=combineall, root=root, split_id=split_id, seq_len=seq_len, sample_method=sample_method ) self.test_loader[name]['query'] = torch.utils.data.DataLoader( queryset, batch_size=batch_size_test, shuffle=False, num_workers=workers, pin_memory=self.use_gpu, drop_last=False ) # build gallery loader galleryset = init_video_dataset( name, transform=self.transform_te, mode='gallery', combineall=combineall, verbose=False, root=root, split_id=split_id, seq_len=seq_len, sample_method=sample_method ) self.test_loader[name]['gallery'] = torch.utils.data.DataLoader( galleryset, batch_size=batch_size_test, shuffle=False, num_workers=workers, pin_memory=self.use_gpu, drop_last=False ) self.test_dataset[name]['query'] = queryset.query self.test_dataset[name]['gallery'] = galleryset.gallery print('\n') print(' **************** Summary ****************') print(' source : {}'.format(self.sources)) print(' # source datasets : {}'.format(len(self.sources))) print(' # source ids : {}'.format(self.num_train_pids)) print(' # source tracklets : {}'.format(len(trainset))) print(' # source cameras : {}'.format(self.num_train_cams)) print(' target : {}'.format(self.targets)) print(' *****************************************') print('\n') ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/__init__.py ================================================ from __future__ import print_function, absolute_import from .image import ( GRID, PRID, CUHK01, CUHK02, CUHK03, MSMT17, CUHKSYSU, VIPeR, SenseReID, Market1501, DukeMTMCreID, University1652, iLIDS ) from .video import PRID2011, Mars, DukeMTMCVidReID, iLIDSVID from .dataset import Dataset, ImageDataset, VideoDataset __image_datasets = { 'market1501': Market1501, 'cuhk03': CUHK03, 'dukemtmcreid': DukeMTMCreID, 'msmt17': MSMT17, 'viper': VIPeR, 'grid': GRID, 'cuhk01': CUHK01, 'ilids': iLIDS, 'sensereid': SenseReID, 'prid': PRID, 'cuhk02': CUHK02, 'university1652': University1652, 'cuhksysu': CUHKSYSU } __video_datasets = { 'mars': Mars, 'ilidsvid': iLIDSVID, 'prid2011': PRID2011, 'dukemtmcvidreid': DukeMTMCVidReID } def init_image_dataset(name, **kwargs): """Initializes an image dataset.""" avai_datasets = list(__image_datasets.keys()) if name not in avai_datasets: raise ValueError( 'Invalid dataset name. Received "{}", ' 'but expected to be one of {}'.format(name, avai_datasets) ) return __image_datasets[name](**kwargs) def init_video_dataset(name, **kwargs): """Initializes a video dataset.""" avai_datasets = list(__video_datasets.keys()) if name not in avai_datasets: raise ValueError( 'Invalid dataset name. Received "{}", ' 'but expected to be one of {}'.format(name, avai_datasets) ) return __video_datasets[name](**kwargs) def register_image_dataset(name, dataset): """Registers a new image dataset. Args: name (str): key corresponding to the new dataset. dataset (Dataset): the new dataset class. Examples:: import torchreid import NewDataset torchreid.data.register_image_dataset('new_dataset', NewDataset) # single dataset case datamanager = torchreid.data.ImageDataManager( root='reid-data', sources='new_dataset' ) # multiple dataset case datamanager = torchreid.data.ImageDataManager( root='reid-data', sources=['new_dataset', 'dukemtmcreid'] ) """ global __image_datasets curr_datasets = list(__image_datasets.keys()) if name in curr_datasets: raise ValueError( 'The given name already exists, please choose ' 'another name excluding {}'.format(curr_datasets) ) __image_datasets[name] = dataset def register_video_dataset(name, dataset): """Registers a new video dataset. Args: name (str): key corresponding to the new dataset. dataset (Dataset): the new dataset class. Examples:: import torchreid import NewDataset torchreid.data.register_video_dataset('new_dataset', NewDataset) # single dataset case datamanager = torchreid.data.VideoDataManager( root='reid-data', sources='new_dataset' ) # multiple dataset case datamanager = torchreid.data.VideoDataManager( root='reid-data', sources=['new_dataset', 'ilidsvid'] ) """ global __video_datasets curr_datasets = list(__video_datasets.keys()) if name in curr_datasets: raise ValueError( 'The given name already exists, please choose ' 'another name excluding {}'.format(curr_datasets) ) __video_datasets[name] = dataset ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/dataset.py ================================================ from __future__ import division, print_function, absolute_import import copy import numpy as np import os.path as osp import tarfile import zipfile import torch from torchreid.utils import read_image, download_url, mkdir_if_missing class Dataset(object): """An abstract class representing a Dataset. This is the base class for ``ImageDataset`` and ``VideoDataset``. Args: train (list): contains tuples of (img_path(s), pid, camid). query (list): contains tuples of (img_path(s), pid, camid). gallery (list): contains tuples of (img_path(s), pid, camid). transform: transform function. k_tfm (int): number of times to apply augmentation to an image independently. If k_tfm > 1, the transform function will be applied k_tfm times to an image. This variable will only be useful for training and is currently valid for image datasets only. mode (str): 'train', 'query' or 'gallery'. combineall (bool): combines train, query and gallery in a dataset for training. verbose (bool): show information. """ # junk_pids contains useless person IDs, e.g. background, # false detections, distractors. These IDs will be ignored # when combining all images in a dataset for training, i.e. # combineall=True _junk_pids = [] # Some datasets are only used for training, like CUHK-SYSU # In this case, "combineall=True" is not used for them _train_only = False def __init__( self, train, query, gallery, transform=None, k_tfm=1, mode='train', combineall=False, verbose=True, **kwargs ): # extend 3-tuple (img_path(s), pid, camid) to # 4-tuple (img_path(s), pid, camid, dsetid) by # adding a dataset indicator "dsetid" if len(train[0]) == 3: train = [(*items, 0) for items in train] if len(query[0]) == 3: query = [(*items, 0) for items in query] if len(gallery[0]) == 3: gallery = [(*items, 0) for items in gallery] self.train = train self.query = query self.gallery = gallery self.transform = transform self.k_tfm = k_tfm self.mode = mode self.combineall = combineall self.verbose = verbose self.num_train_pids = self.get_num_pids(self.train) self.num_train_cams = self.get_num_cams(self.train) self.num_datasets = self.get_num_datasets(self.train) if self.combineall: self.combine_all() if self.mode == 'train': self.data = self.train elif self.mode == 'query': self.data = self.query elif self.mode == 'gallery': self.data = self.gallery else: raise ValueError( 'Invalid mode. Got {}, but expected to be ' 'one of [train | query | gallery]'.format(self.mode) ) if self.verbose: self.show_summary() def __getitem__(self, index): raise NotImplementedError def __len__(self): return len(self.data) def __add__(self, other): """Adds two datasets together (only the train set).""" train = copy.deepcopy(self.train) for img_path, pid, camid, dsetid in other.train: pid += self.num_train_pids camid += self.num_train_cams dsetid += self.num_datasets train.append((img_path, pid, camid, dsetid)) ################################### # Note that # 1. set verbose=False to avoid unnecessary print # 2. set combineall=False because combineall would have been applied # if it was True for a specific dataset; setting it to True will # create new IDs that should have already been included ################################### if isinstance(train[0][0], str): return ImageDataset( train, self.query, self.gallery, transform=self.transform, mode=self.mode, combineall=False, verbose=False ) else: return VideoDataset( train, self.query, self.gallery, transform=self.transform, mode=self.mode, combineall=False, verbose=False, seq_len=self.seq_len, sample_method=self.sample_method ) def __radd__(self, other): """Supports sum([dataset1, dataset2, dataset3]).""" if other == 0: return self else: return self.__add__(other) def get_num_pids(self, data): """Returns the number of training person identities. Each tuple in data contains (img_path(s), pid, camid, dsetid). """ pids = set() for items in data: pid = items[1] pids.add(pid) return len(pids) def get_num_cams(self, data): """Returns the number of training cameras. Each tuple in data contains (img_path(s), pid, camid, dsetid). """ cams = set() for items in data: camid = items[2] cams.add(camid) return len(cams) def get_num_datasets(self, data): """Returns the number of datasets included. Each tuple in data contains (img_path(s), pid, camid, dsetid). """ dsets = set() for items in data: dsetid = items[3] dsets.add(dsetid) return len(dsets) def show_summary(self): """Shows dataset statistics.""" pass def combine_all(self): """Combines train, query and gallery in a dataset for training.""" if self._train_only: return combined = copy.deepcopy(self.train) # relabel pids in gallery (query shares the same scope) g_pids = set() for items in self.gallery: pid = items[1] if pid in self._junk_pids: continue g_pids.add(pid) pid2label = {pid: i for i, pid in enumerate(g_pids)} def _combine_data(data): for img_path, pid, camid, dsetid in data: if pid in self._junk_pids: continue pid = pid2label[pid] + self.num_train_pids combined.append((img_path, pid, camid, dsetid)) _combine_data(self.query) _combine_data(self.gallery) self.train = combined self.num_train_pids = self.get_num_pids(self.train) def download_dataset(self, dataset_dir, dataset_url): """Downloads and extracts dataset. Args: dataset_dir (str): dataset directory. dataset_url (str): url to download dataset. """ if osp.exists(dataset_dir): return if dataset_url is None: raise RuntimeError( '{} dataset needs to be manually ' 'prepared, please follow the ' 'document to prepare this dataset'.format( self.__class__.__name__ ) ) print('Creating directory "{}"'.format(dataset_dir)) mkdir_if_missing(dataset_dir) fpath = osp.join(dataset_dir, osp.basename(dataset_url)) print( 'Downloading {} dataset to "{}"'.format( self.__class__.__name__, dataset_dir ) ) download_url(dataset_url, fpath) print('Extracting "{}"'.format(fpath)) try: tar = tarfile.open(fpath) tar.extractall(path=dataset_dir) tar.close() except: zip_ref = zipfile.ZipFile(fpath, 'r') zip_ref.extractall(dataset_dir) zip_ref.close() print('{} dataset is ready'.format(self.__class__.__name__)) def check_before_run(self, required_files): """Checks if required files exist before going deeper. Args: required_files (str or list): string file name(s). """ if isinstance(required_files, str): required_files = [required_files] for fpath in required_files: if not osp.exists(fpath): raise RuntimeError('"{}" is not found'.format(fpath)) def __repr__(self): num_train_pids = self.get_num_pids(self.train) num_train_cams = self.get_num_cams(self.train) num_query_pids = self.get_num_pids(self.query) num_query_cams = self.get_num_cams(self.query) num_gallery_pids = self.get_num_pids(self.gallery) num_gallery_cams = self.get_num_cams(self.gallery) msg = ' ----------------------------------------\n' \ ' subset | # ids | # items | # cameras\n' \ ' ----------------------------------------\n' \ ' train | {:5d} | {:7d} | {:9d}\n' \ ' query | {:5d} | {:7d} | {:9d}\n' \ ' gallery | {:5d} | {:7d} | {:9d}\n' \ ' ----------------------------------------\n' \ ' items: images/tracklets for image/video dataset\n'.format( num_train_pids, len(self.train), num_train_cams, num_query_pids, len(self.query), num_query_cams, num_gallery_pids, len(self.gallery), num_gallery_cams ) return msg def _transform_image(self, tfm, k_tfm, img0): """Transforms a raw image (img0) k_tfm times with the transform function tfm. """ img_list = [] for k in range(k_tfm): img_list.append(tfm(img0)) img = img_list if len(img) == 1: img = img[0] return img class ImageDataset(Dataset): """A base class representing ImageDataset. All other image datasets should subclass it. ``__getitem__`` returns an image given index. It will return ``img``, ``pid``, ``camid`` and ``img_path`` where ``img`` has shape (channel, height, width). As a result, data in each batch has shape (batch_size, channel, height, width). """ def __init__(self, train, query, gallery, **kwargs): super(ImageDataset, self).__init__(train, query, gallery, **kwargs) def __getitem__(self, index): img_path, pid, camid, dsetid = self.data[index] img = read_image(img_path) if self.transform is not None: img = self._transform_image(self.transform, self.k_tfm, img) item = { 'img': img, 'pid': pid, 'camid': camid, 'impath': img_path, 'dsetid': dsetid } return item def show_summary(self): num_train_pids = self.get_num_pids(self.train) num_train_cams = self.get_num_cams(self.train) num_query_pids = self.get_num_pids(self.query) num_query_cams = self.get_num_cams(self.query) num_gallery_pids = self.get_num_pids(self.gallery) num_gallery_cams = self.get_num_cams(self.gallery) print('=> Loaded {}'.format(self.__class__.__name__)) print(' ----------------------------------------') print(' subset | # ids | # images | # cameras') print(' ----------------------------------------') print( ' train | {:5d} | {:8d} | {:9d}'.format( num_train_pids, len(self.train), num_train_cams ) ) print( ' query | {:5d} | {:8d} | {:9d}'.format( num_query_pids, len(self.query), num_query_cams ) ) print( ' gallery | {:5d} | {:8d} | {:9d}'.format( num_gallery_pids, len(self.gallery), num_gallery_cams ) ) print(' ----------------------------------------') class VideoDataset(Dataset): """A base class representing VideoDataset. All other video datasets should subclass it. ``__getitem__`` returns an image given index. It will return ``imgs``, ``pid`` and ``camid`` where ``imgs`` has shape (seq_len, channel, height, width). As a result, data in each batch has shape (batch_size, seq_len, channel, height, width). """ def __init__( self, train, query, gallery, seq_len=15, sample_method='evenly', **kwargs ): super(VideoDataset, self).__init__(train, query, gallery, **kwargs) self.seq_len = seq_len self.sample_method = sample_method if self.transform is None: raise RuntimeError('transform must not be None') def __getitem__(self, index): img_paths, pid, camid, dsetid = self.data[index] num_imgs = len(img_paths) if self.sample_method == 'random': # Randomly samples seq_len images from a tracklet of length num_imgs, # if num_imgs is smaller than seq_len, then replicates images indices = np.arange(num_imgs) replace = False if num_imgs >= self.seq_len else True indices = np.random.choice( indices, size=self.seq_len, replace=replace ) # sort indices to keep temporal order (comment it to be order-agnostic) indices = np.sort(indices) elif self.sample_method == 'evenly': # Evenly samples seq_len images from a tracklet if num_imgs >= self.seq_len: num_imgs -= num_imgs % self.seq_len indices = np.arange(0, num_imgs, num_imgs / self.seq_len) else: # if num_imgs is smaller than seq_len, simply replicate the last image # until the seq_len requirement is satisfied indices = np.arange(0, num_imgs) num_pads = self.seq_len - num_imgs indices = np.concatenate( [ indices, np.ones(num_pads).astype(np.int32) * (num_imgs-1) ] ) assert len(indices) == self.seq_len elif self.sample_method == 'all': # Samples all images in a tracklet. batch_size must be set to 1 indices = np.arange(num_imgs) else: raise ValueError( 'Unknown sample method: {}'.format(self.sample_method) ) imgs = [] for index in indices: img_path = img_paths[int(index)] img = read_image(img_path) if self.transform is not None: img = self.transform(img) img = img.unsqueeze(0) # img must be torch.Tensor imgs.append(img) imgs = torch.cat(imgs, dim=0) item = {'img': imgs, 'pid': pid, 'camid': camid, 'dsetid': dsetid} return item def show_summary(self): num_train_pids = self.get_num_pids(self.train) num_train_cams = self.get_num_cams(self.train) num_query_pids = self.get_num_pids(self.query) num_query_cams = self.get_num_cams(self.query) num_gallery_pids = self.get_num_pids(self.gallery) num_gallery_cams = self.get_num_cams(self.gallery) print('=> Loaded {}'.format(self.__class__.__name__)) print(' -------------------------------------------') print(' subset | # ids | # tracklets | # cameras') print(' -------------------------------------------') print( ' train | {:5d} | {:11d} | {:9d}'.format( num_train_pids, len(self.train), num_train_cams ) ) print( ' query | {:5d} | {:11d} | {:9d}'.format( num_query_pids, len(self.query), num_query_cams ) ) print( ' gallery | {:5d} | {:11d} | {:9d}'.format( num_gallery_pids, len(self.gallery), num_gallery_cams ) ) print(' -------------------------------------------') ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/__init__.py ================================================ from __future__ import print_function, absolute_import from .grid import GRID from .prid import PRID from .ilids import iLIDS from .viper import VIPeR from .cuhk01 import CUHK01 from .cuhk02 import CUHK02 from .cuhk03 import CUHK03 from .msmt17 import MSMT17 from .cuhksysu import CUHKSYSU from .sensereid import SenseReID from .market1501 import Market1501 from .dukemtmcreid import DukeMTMCreID from .university1652 import University1652 ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/cuhk01.py ================================================ from __future__ import division, print_function, absolute_import import glob import numpy as np import os.path as osp import zipfile from torchreid.utils import read_json, write_json from ..dataset import ImageDataset class CUHK01(ImageDataset): """CUHK01. Reference: Li et al. Human Reidentification with Transferred Metric Learning. ACCV 2012. URL: ``_ Dataset statistics: - identities: 971. - images: 3884. - cameras: 4. Note: CUHK01 and CUHK02 overlap. """ dataset_dir = 'cuhk01' dataset_url = None def __init__(self, root='', split_id=0, **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.zip_path = osp.join(self.dataset_dir, 'CUHK01.zip') self.campus_dir = osp.join(self.dataset_dir, 'campus') self.split_path = osp.join(self.dataset_dir, 'splits.json') self.extract_file() required_files = [self.dataset_dir, self.campus_dir] self.check_before_run(required_files) self.prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): raise ValueError( 'split_id exceeds range, received {}, but expected between 0 and {}' .format(split_id, len(splits) - 1) ) split = splits[split_id] train = split['train'] query = split['query'] gallery = split['gallery'] train = [tuple(item) for item in train] query = [tuple(item) for item in query] gallery = [tuple(item) for item in gallery] super(CUHK01, self).__init__(train, query, gallery, **kwargs) def extract_file(self): if not osp.exists(self.campus_dir): print('Extracting files') zip_ref = zipfile.ZipFile(self.zip_path, 'r') zip_ref.extractall(self.dataset_dir) zip_ref.close() def prepare_split(self): """ Image name format: 0001001.png, where first four digits represent identity and last four digits represent cameras. Camera 1&2 are considered the same view and camera 3&4 are considered the same view. """ if not osp.exists(self.split_path): print('Creating 10 random splits of train ids and test ids') img_paths = sorted(glob.glob(osp.join(self.campus_dir, '*.png'))) img_list = [] pid_container = set() for img_path in img_paths: img_name = osp.basename(img_path) pid = int(img_name[:4]) - 1 camid = (int(img_name[4:7]) - 1) // 2 # result is either 0 or 1 img_list.append((img_path, pid, camid)) pid_container.add(pid) num_pids = len(pid_container) num_train_pids = num_pids // 2 splits = [] for _ in range(10): order = np.arange(num_pids) np.random.shuffle(order) train_idxs = order[:num_train_pids] train_idxs = np.sort(train_idxs) idx2label = { idx: label for label, idx in enumerate(train_idxs) } train, test_a, test_b = [], [], [] for img_path, pid, camid in img_list: if pid in train_idxs: train.append((img_path, idx2label[pid], camid)) else: if camid == 0: test_a.append((img_path, pid, camid)) else: test_b.append((img_path, pid, camid)) # use cameraA as query and cameraB as gallery split = { 'train': train, 'query': test_a, 'gallery': test_b, 'num_train_pids': num_train_pids, 'num_query_pids': num_pids - num_train_pids, 'num_gallery_pids': num_pids - num_train_pids } splits.append(split) # use cameraB as query and cameraA as gallery split = { 'train': train, 'query': test_b, 'gallery': test_a, 'num_train_pids': num_train_pids, 'num_query_pids': num_pids - num_train_pids, 'num_gallery_pids': num_pids - num_train_pids } splits.append(split) print('Totally {} splits are created'.format(len(splits))) write_json(splits, self.split_path) print('Split file saved to {}'.format(self.split_path)) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/cuhk02.py ================================================ from __future__ import division, print_function, absolute_import import glob import os.path as osp from ..dataset import ImageDataset class CUHK02(ImageDataset): """CUHK02. Reference: Li and Wang. Locally Aligned Feature Transforms across Views. CVPR 2013. URL: ``_ Dataset statistics: - 5 camera view pairs each with two cameras - 971, 306, 107, 193 and 239 identities from P1 - P5 - totally 1,816 identities - image format is png Protocol: Use P1 - P4 for training and P5 for evaluation. Note: CUHK01 and CUHK02 overlap. """ dataset_dir = 'cuhk02' cam_pairs = ['P1', 'P2', 'P3', 'P4', 'P5'] test_cam_pair = 'P5' def __init__(self, root='', **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir, 'Dataset') required_files = [self.dataset_dir] self.check_before_run(required_files) train, query, gallery = self.get_data_list() super(CUHK02, self).__init__(train, query, gallery, **kwargs) def get_data_list(self): num_train_pids, camid = 0, 0 train, query, gallery = [], [], [] for cam_pair in self.cam_pairs: cam_pair_dir = osp.join(self.dataset_dir, cam_pair) cam1_dir = osp.join(cam_pair_dir, 'cam1') cam2_dir = osp.join(cam_pair_dir, 'cam2') impaths1 = glob.glob(osp.join(cam1_dir, '*.png')) impaths2 = glob.glob(osp.join(cam2_dir, '*.png')) if cam_pair == self.test_cam_pair: # add images to query for impath in impaths1: pid = osp.basename(impath).split('_')[0] pid = int(pid) query.append((impath, pid, camid)) camid += 1 # add images to gallery for impath in impaths2: pid = osp.basename(impath).split('_')[0] pid = int(pid) gallery.append((impath, pid, camid)) camid += 1 else: pids1 = [ osp.basename(impath).split('_')[0] for impath in impaths1 ] pids2 = [ osp.basename(impath).split('_')[0] for impath in impaths2 ] pids = set(pids1 + pids2) pid2label = { pid: label + num_train_pids for label, pid in enumerate(pids) } # add images to train from cam1 for impath in impaths1: pid = osp.basename(impath).split('_')[0] pid = pid2label[pid] train.append((impath, pid, camid)) camid += 1 # add images to train from cam2 for impath in impaths2: pid = osp.basename(impath).split('_')[0] pid = pid2label[pid] train.append((impath, pid, camid)) camid += 1 num_train_pids += len(pids) return train, query, gallery ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/cuhk03.py ================================================ from __future__ import division, print_function, absolute_import import os.path as osp from torchreid.utils import read_json, write_json, mkdir_if_missing from ..dataset import ImageDataset class CUHK03(ImageDataset): """CUHK03. Reference: Li et al. DeepReID: Deep Filter Pairing Neural Network for Person Re-identification. CVPR 2014. URL: ``_ Dataset statistics: - identities: 1360. - images: 13164. - cameras: 6. - splits: 20 (classic). """ dataset_dir = 'cuhk03' dataset_url = None def __init__( self, root='', split_id=0, cuhk03_labeled=False, cuhk03_classic_split=False, **kwargs ): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.data_dir = osp.join(self.dataset_dir, 'cuhk03_release') self.raw_mat_path = osp.join(self.data_dir, 'cuhk-03.mat') self.imgs_detected_dir = osp.join(self.dataset_dir, 'images_detected') self.imgs_labeled_dir = osp.join(self.dataset_dir, 'images_labeled') self.split_classic_det_json_path = osp.join( self.dataset_dir, 'splits_classic_detected.json' ) self.split_classic_lab_json_path = osp.join( self.dataset_dir, 'splits_classic_labeled.json' ) self.split_new_det_json_path = osp.join( self.dataset_dir, 'splits_new_detected.json' ) self.split_new_lab_json_path = osp.join( self.dataset_dir, 'splits_new_labeled.json' ) self.split_new_det_mat_path = osp.join( self.dataset_dir, 'cuhk03_new_protocol_config_detected.mat' ) self.split_new_lab_mat_path = osp.join( self.dataset_dir, 'cuhk03_new_protocol_config_labeled.mat' ) required_files = [ self.dataset_dir, self.data_dir, self.raw_mat_path, self.split_new_det_mat_path, self.split_new_lab_mat_path ] self.check_before_run(required_files) self.preprocess_split() if cuhk03_labeled: split_path = self.split_classic_lab_json_path if cuhk03_classic_split else self.split_new_lab_json_path else: split_path = self.split_classic_det_json_path if cuhk03_classic_split else self.split_new_det_json_path splits = read_json(split_path) assert split_id < len( splits ), 'Condition split_id ({}) < len(splits) ({}) is false'.format( split_id, len(splits) ) split = splits[split_id] train = split['train'] query = split['query'] gallery = split['gallery'] super(CUHK03, self).__init__(train, query, gallery, **kwargs) def preprocess_split(self): # This function is a bit complex and ugly, what it does is # 1. extract data from cuhk-03.mat and save as png images # 2. create 20 classic splits (Li et al. CVPR'14) # 3. create new split (Zhong et al. CVPR'17) if osp.exists(self.imgs_labeled_dir) \ and osp.exists(self.imgs_detected_dir) \ and osp.exists(self.split_classic_det_json_path) \ and osp.exists(self.split_classic_lab_json_path) \ and osp.exists(self.split_new_det_json_path) \ and osp.exists(self.split_new_lab_json_path): return import h5py import imageio from scipy.io import loadmat mkdir_if_missing(self.imgs_detected_dir) mkdir_if_missing(self.imgs_labeled_dir) print( 'Extract image data from "{}" and save as png'.format( self.raw_mat_path ) ) mat = h5py.File(self.raw_mat_path, 'r') def _deref(ref): return mat[ref][:].T def _process_images(img_refs, campid, pid, save_dir): img_paths = [] # Note: some persons only have images for one view for imgid, img_ref in enumerate(img_refs): img = _deref(img_ref) if img.size == 0 or img.ndim < 3: continue # skip empty cell # images are saved with the following format, index-1 (ensure uniqueness) # campid: index of camera pair (1-5) # pid: index of person in 'campid'-th camera pair # viewid: index of view, {1, 2} # imgid: index of image, (1-10) viewid = 1 if imgid < 5 else 2 img_name = '{:01d}_{:03d}_{:01d}_{:02d}.png'.format( campid + 1, pid + 1, viewid, imgid + 1 ) img_path = osp.join(save_dir, img_name) if not osp.isfile(img_path): imageio.imwrite(img_path, img) img_paths.append(img_path) return img_paths def _extract_img(image_type): print('Processing {} images ...'.format(image_type)) meta_data = [] imgs_dir = self.imgs_detected_dir if image_type == 'detected' else self.imgs_labeled_dir for campid, camp_ref in enumerate(mat[image_type][0]): camp = _deref(camp_ref) num_pids = camp.shape[0] for pid in range(num_pids): img_paths = _process_images( camp[pid, :], campid, pid, imgs_dir ) assert len(img_paths) > 0, \ 'campid{}-pid{} has no images'.format(campid, pid) meta_data.append((campid + 1, pid + 1, img_paths)) print( '- done camera pair {} with {} identities'.format( campid + 1, num_pids ) ) return meta_data meta_detected = _extract_img('detected') meta_labeled = _extract_img('labeled') def _extract_classic_split(meta_data, test_split): train, test = [], [] num_train_pids, num_test_pids = 0, 0 num_train_imgs, num_test_imgs = 0, 0 for i, (campid, pid, img_paths) in enumerate(meta_data): if [campid, pid] in test_split: for img_path in img_paths: camid = int( osp.basename(img_path).split('_')[2] ) - 1 # make it 0-based test.append((img_path, num_test_pids, camid)) num_test_pids += 1 num_test_imgs += len(img_paths) else: for img_path in img_paths: camid = int( osp.basename(img_path).split('_')[2] ) - 1 # make it 0-based train.append((img_path, num_train_pids, camid)) num_train_pids += 1 num_train_imgs += len(img_paths) return train, num_train_pids, num_train_imgs, test, num_test_pids, num_test_imgs print('Creating classic splits (# = 20) ...') splits_classic_det, splits_classic_lab = [], [] for split_ref in mat['testsets'][0]: test_split = _deref(split_ref).tolist() # create split for detected images train, num_train_pids, num_train_imgs, test, num_test_pids, num_test_imgs = \ _extract_classic_split(meta_detected, test_split) splits_classic_det.append( { 'train': train, 'query': test, 'gallery': test, 'num_train_pids': num_train_pids, 'num_train_imgs': num_train_imgs, 'num_query_pids': num_test_pids, 'num_query_imgs': num_test_imgs, 'num_gallery_pids': num_test_pids, 'num_gallery_imgs': num_test_imgs } ) # create split for labeled images train, num_train_pids, num_train_imgs, test, num_test_pids, num_test_imgs = \ _extract_classic_split(meta_labeled, test_split) splits_classic_lab.append( { 'train': train, 'query': test, 'gallery': test, 'num_train_pids': num_train_pids, 'num_train_imgs': num_train_imgs, 'num_query_pids': num_test_pids, 'num_query_imgs': num_test_imgs, 'num_gallery_pids': num_test_pids, 'num_gallery_imgs': num_test_imgs } ) write_json(splits_classic_det, self.split_classic_det_json_path) write_json(splits_classic_lab, self.split_classic_lab_json_path) def _extract_set(filelist, pids, pid2label, idxs, img_dir, relabel): tmp_set = [] unique_pids = set() for idx in idxs: img_name = filelist[idx][0] camid = int(img_name.split('_')[2]) - 1 # make it 0-based pid = pids[idx] if relabel: pid = pid2label[pid] img_path = osp.join(img_dir, img_name) tmp_set.append((img_path, int(pid), camid)) unique_pids.add(pid) return tmp_set, len(unique_pids), len(idxs) def _extract_new_split(split_dict, img_dir): train_idxs = split_dict['train_idx'].flatten() - 1 # index-0 pids = split_dict['labels'].flatten() train_pids = set(pids[train_idxs]) pid2label = {pid: label for label, pid in enumerate(train_pids)} query_idxs = split_dict['query_idx'].flatten() - 1 gallery_idxs = split_dict['gallery_idx'].flatten() - 1 filelist = split_dict['filelist'].flatten() train_info = _extract_set( filelist, pids, pid2label, train_idxs, img_dir, relabel=True ) query_info = _extract_set( filelist, pids, pid2label, query_idxs, img_dir, relabel=False ) gallery_info = _extract_set( filelist, pids, pid2label, gallery_idxs, img_dir, relabel=False ) return train_info, query_info, gallery_info print('Creating new split for detected images (767/700) ...') train_info, query_info, gallery_info = _extract_new_split( loadmat(self.split_new_det_mat_path), self.imgs_detected_dir ) split = [ { 'train': train_info[0], 'query': query_info[0], 'gallery': gallery_info[0], 'num_train_pids': train_info[1], 'num_train_imgs': train_info[2], 'num_query_pids': query_info[1], 'num_query_imgs': query_info[2], 'num_gallery_pids': gallery_info[1], 'num_gallery_imgs': gallery_info[2] } ] write_json(split, self.split_new_det_json_path) print('Creating new split for labeled images (767/700) ...') train_info, query_info, gallery_info = _extract_new_split( loadmat(self.split_new_lab_mat_path), self.imgs_labeled_dir ) split = [ { 'train': train_info[0], 'query': query_info[0], 'gallery': gallery_info[0], 'num_train_pids': train_info[1], 'num_train_imgs': train_info[2], 'num_query_pids': query_info[1], 'num_query_imgs': query_info[2], 'num_gallery_pids': gallery_info[1], 'num_gallery_imgs': gallery_info[2] } ] write_json(split, self.split_new_lab_json_path) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/cuhksysu.py ================================================ from __future__ import division, print_function, absolute_import import copy import glob import os.path as osp from ..dataset import ImageDataset class CUHKSYSU(ImageDataset): """CUHKSYSU. This dataset can only be used for model training. Reference: Xiao et al. End-to-end deep learning for person search. URL: ``_ Dataset statistics: - identities: 11,934 - images: 34,574 """ _train_only = True dataset_dir = 'cuhksysu' def __init__(self, root='', **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.data_dir = osp.join(self.dataset_dir, 'cropped_images') # image name format: p11422_s16929_1.jpg train = self.process_dir(self.data_dir) query = [copy.deepcopy(train[0])] gallery = [copy.deepcopy(train[0])] super(CUHKSYSU, self).__init__(train, query, gallery, **kwargs) def process_dir(self, dirname): img_paths = glob.glob(osp.join(dirname, '*.jpg')) # num_imgs = len(img_paths) # get all identities: pid_container = set() for img_path in img_paths: img_name = osp.basename(img_path) pid = img_name.split('_')[0] pid_container.add(pid) pid2label = {pid: label for label, pid in enumerate(pid_container)} # num_pids = len(pid_container) # extract data data = [] for img_path in img_paths: img_name = osp.basename(img_path) pid = img_name.split('_')[0] label = pid2label[pid] data.append((img_path, label, 0)) # dummy camera id return data ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/dukemtmcreid.py ================================================ from __future__ import division, print_function, absolute_import import re import glob import os.path as osp from ..dataset import ImageDataset class DukeMTMCreID(ImageDataset): """DukeMTMC-reID. Reference: - Ristani et al. Performance Measures and a Data Set for Multi-Target, Multi-Camera Tracking. ECCVW 2016. - Zheng et al. Unlabeled Samples Generated by GAN Improve the Person Re-identification Baseline in vitro. ICCV 2017. URL: ``_ Dataset statistics: - identities: 1404 (train + query). - images:16522 (train) + 2228 (query) + 17661 (gallery). - cameras: 8. """ dataset_dir = 'dukemtmc-reid' dataset_url = 'http://vision.cs.duke.edu/DukeMTMC/data/misc/DukeMTMC-reID.zip' def __init__(self, root='', **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.train_dir = osp.join( self.dataset_dir, 'DukeMTMC-reID/bounding_box_train' ) self.query_dir = osp.join(self.dataset_dir, 'DukeMTMC-reID/query') self.gallery_dir = osp.join( self.dataset_dir, 'DukeMTMC-reID/bounding_box_test' ) required_files = [ self.dataset_dir, self.train_dir, self.query_dir, self.gallery_dir ] self.check_before_run(required_files) train = self.process_dir(self.train_dir, relabel=True) query = self.process_dir(self.query_dir, relabel=False) gallery = self.process_dir(self.gallery_dir, relabel=False) super(DukeMTMCreID, self).__init__(train, query, gallery, **kwargs) def process_dir(self, dir_path, relabel=False): img_paths = glob.glob(osp.join(dir_path, '*.jpg')) pattern = re.compile(r'([-\d]+)_c(\d)') pid_container = set() for img_path in img_paths: pid, _ = map(int, pattern.search(img_path).groups()) pid_container.add(pid) pid2label = {pid: label for label, pid in enumerate(pid_container)} data = [] for img_path in img_paths: pid, camid = map(int, pattern.search(img_path).groups()) assert 1 <= camid <= 8 camid -= 1 # index starts from 0 if relabel: pid = pid2label[pid] data.append((img_path, pid, camid)) return data ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/grid.py ================================================ from __future__ import division, print_function, absolute_import import glob import os.path as osp from scipy.io import loadmat from torchreid.utils import read_json, write_json from ..dataset import ImageDataset class GRID(ImageDataset): """GRID. Reference: Loy et al. Multi-camera activity correlation analysis. CVPR 2009. URL: ``_ Dataset statistics: - identities: 250. - images: 1275. - cameras: 8. """ dataset_dir = 'grid' dataset_url = 'http://personal.ie.cuhk.edu.hk/~ccloy/files/datasets/underground_reid.zip' _junk_pids = [0] def __init__(self, root='', split_id=0, **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.probe_path = osp.join( self.dataset_dir, 'underground_reid', 'probe' ) self.gallery_path = osp.join( self.dataset_dir, 'underground_reid', 'gallery' ) self.split_mat_path = osp.join( self.dataset_dir, 'underground_reid', 'features_and_partitions.mat' ) self.split_path = osp.join(self.dataset_dir, 'splits.json') required_files = [ self.dataset_dir, self.probe_path, self.gallery_path, self.split_mat_path ] self.check_before_run(required_files) self.prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): raise ValueError( 'split_id exceeds range, received {}, ' 'but expected between 0 and {}'.format( split_id, len(splits) - 1 ) ) split = splits[split_id] train = split['train'] query = split['query'] gallery = split['gallery'] train = [tuple(item) for item in train] query = [tuple(item) for item in query] gallery = [tuple(item) for item in gallery] super(GRID, self).__init__(train, query, gallery, **kwargs) def prepare_split(self): if not osp.exists(self.split_path): print('Creating 10 random splits') split_mat = loadmat(self.split_mat_path) trainIdxAll = split_mat['trainIdxAll'][0] # length = 10 probe_img_paths = sorted( glob.glob(osp.join(self.probe_path, '*.jpeg')) ) gallery_img_paths = sorted( glob.glob(osp.join(self.gallery_path, '*.jpeg')) ) splits = [] for split_idx in range(10): train_idxs = trainIdxAll[split_idx][0][0][2][0].tolist() assert len(train_idxs) == 125 idx2label = { idx: label for label, idx in enumerate(train_idxs) } train, query, gallery = [], [], [] # processing probe folder for img_path in probe_img_paths: img_name = osp.basename(img_path) img_idx = int(img_name.split('_')[0]) camid = int( img_name.split('_')[1] ) - 1 # index starts from 0 if img_idx in train_idxs: train.append((img_path, idx2label[img_idx], camid)) else: query.append((img_path, img_idx, camid)) # process gallery folder for img_path in gallery_img_paths: img_name = osp.basename(img_path) img_idx = int(img_name.split('_')[0]) camid = int( img_name.split('_')[1] ) - 1 # index starts from 0 if img_idx in train_idxs: train.append((img_path, idx2label[img_idx], camid)) else: gallery.append((img_path, img_idx, camid)) split = { 'train': train, 'query': query, 'gallery': gallery, 'num_train_pids': 125, 'num_query_pids': 125, 'num_gallery_pids': 900 } splits.append(split) print('Totally {} splits are created'.format(len(splits))) write_json(splits, self.split_path) print('Split file saved to {}'.format(self.split_path)) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/ilids.py ================================================ from __future__ import division, print_function, absolute_import import copy import glob import random import os.path as osp from collections import defaultdict from torchreid.utils import read_json, write_json from ..dataset import ImageDataset class iLIDS(ImageDataset): """QMUL-iLIDS. Reference: Zheng et al. Associating Groups of People. BMVC 2009. Dataset statistics: - identities: 119. - images: 476. - cameras: 8 (not explicitly provided). """ dataset_dir = 'ilids' dataset_url = 'http://www.eecs.qmul.ac.uk/~jason/data/i-LIDS_Pedestrian.tgz' def __init__(self, root='', split_id=0, **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.data_dir = osp.join(self.dataset_dir, 'i-LIDS_Pedestrian/Persons') self.split_path = osp.join(self.dataset_dir, 'splits.json') required_files = [self.dataset_dir, self.data_dir] self.check_before_run(required_files) self.prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): raise ValueError( 'split_id exceeds range, received {}, but ' 'expected between 0 and {}'.format(split_id, len(splits) - 1) ) split = splits[split_id] train, query, gallery = self.process_split(split) super(iLIDS, self).__init__(train, query, gallery, **kwargs) def prepare_split(self): if not osp.exists(self.split_path): print('Creating splits ...') paths = glob.glob(osp.join(self.data_dir, '*.jpg')) img_names = [osp.basename(path) for path in paths] num_imgs = len(img_names) assert num_imgs == 476, 'There should be 476 images, but ' \ 'got {}, please check the data'.format(num_imgs) # store image names # image naming format: # the first four digits denote the person ID # the last four digits denote the sequence index pid_dict = defaultdict(list) for img_name in img_names: pid = int(img_name[:4]) pid_dict[pid].append(img_name) pids = list(pid_dict.keys()) num_pids = len(pids) assert num_pids == 119, 'There should be 119 identities, ' \ 'but got {}, please check the data'.format(num_pids) num_train_pids = int(num_pids * 0.5) splits = [] for _ in range(10): # randomly choose num_train_pids train IDs and the rest for test IDs pids_copy = copy.deepcopy(pids) random.shuffle(pids_copy) train_pids = pids_copy[:num_train_pids] test_pids = pids_copy[num_train_pids:] train = [] query = [] gallery = [] # for train IDs, all images are used in the train set. for pid in train_pids: img_names = pid_dict[pid] train.extend(img_names) # for each test ID, randomly choose two images, one for # query and the other one for gallery. for pid in test_pids: img_names = pid_dict[pid] samples = random.sample(img_names, 2) query.append(samples[0]) gallery.append(samples[1]) split = {'train': train, 'query': query, 'gallery': gallery} splits.append(split) print('Totally {} splits are created'.format(len(splits))) write_json(splits, self.split_path) print('Split file is saved to {}'.format(self.split_path)) def get_pid2label(self, img_names): pid_container = set() for img_name in img_names: pid = int(img_name[:4]) pid_container.add(pid) pid2label = {pid: label for label, pid in enumerate(pid_container)} return pid2label def parse_img_names(self, img_names, pid2label=None): data = [] for img_name in img_names: pid = int(img_name[:4]) if pid2label is not None: pid = pid2label[pid] camid = int(img_name[4:7]) - 1 # 0-based img_path = osp.join(self.data_dir, img_name) data.append((img_path, pid, camid)) return data def process_split(self, split): train_pid2label = self.get_pid2label(split['train']) train = self.parse_img_names(split['train'], train_pid2label) query = self.parse_img_names(split['query']) gallery = self.parse_img_names(split['gallery']) return train, query, gallery ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/market1501.py ================================================ from __future__ import division, print_function, absolute_import import re import glob import os.path as osp import warnings from ..dataset import ImageDataset class Market1501(ImageDataset): """Market1501. Reference: Zheng et al. Scalable Person Re-identification: A Benchmark. ICCV 2015. URL: ``_ Dataset statistics: - identities: 1501 (+1 for background). - images: 12936 (train) + 3368 (query) + 15913 (gallery). """ _junk_pids = [0, -1] dataset_dir = 'market1501' dataset_url = 'http://188.138.127.15:81/Datasets/Market-1501-v15.09.15.zip' def __init__(self, root='', market1501_500k=False, **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) # allow alternative directory structure self.data_dir = self.dataset_dir data_dir = osp.join(self.data_dir, 'Market-1501-v15.09.15') if osp.isdir(data_dir): self.data_dir = data_dir else: warnings.warn( 'The current data structure is deprecated. Please ' 'put data folders such as "bounding_box_train" under ' '"Market-1501-v15.09.15".' ) self.train_dir = osp.join(self.data_dir, 'bounding_box_train') self.query_dir = osp.join(self.data_dir, 'query') self.gallery_dir = osp.join(self.data_dir, 'bounding_box_test') self.extra_gallery_dir = osp.join(self.data_dir, 'images') self.market1501_500k = market1501_500k required_files = [ self.data_dir, self.train_dir, self.query_dir, self.gallery_dir ] if self.market1501_500k: required_files.append(self.extra_gallery_dir) self.check_before_run(required_files) train = self.process_dir(self.train_dir, relabel=True) query = self.process_dir(self.query_dir, relabel=False) gallery = self.process_dir(self.gallery_dir, relabel=False) if self.market1501_500k: gallery += self.process_dir(self.extra_gallery_dir, relabel=False) super(Market1501, self).__init__(train, query, gallery, **kwargs) def process_dir(self, dir_path, relabel=False): img_paths = glob.glob(osp.join(dir_path, '*.jpg')) pattern = re.compile(r'([-\d]+)_c(\d)') pid_container = set() for img_path in img_paths: pid, _ = map(int, pattern.search(img_path).groups()) if pid == -1: continue # junk images are just ignored pid_container.add(pid) pid2label = {pid: label for label, pid in enumerate(pid_container)} data = [] for img_path in img_paths: pid, camid = map(int, pattern.search(img_path).groups()) if pid == -1: continue # junk images are just ignored assert 0 <= pid <= 1501 # pid == 0 means background assert 1 <= camid <= 6 camid -= 1 # index starts from 0 if relabel: pid = pid2label[pid] data.append((img_path, pid, camid)) return data ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/msmt17.py ================================================ from __future__ import division, print_function, absolute_import import os.path as osp from ..dataset import ImageDataset # Log # 22.01.2019 # - add v2 # - v1 and v2 differ in dir names # - note that faces in v2 are blurred TRAIN_DIR_KEY = 'train_dir' TEST_DIR_KEY = 'test_dir' VERSION_DICT = { 'MSMT17_V1': { TRAIN_DIR_KEY: 'train', TEST_DIR_KEY: 'test', }, 'MSMT17_V2': { TRAIN_DIR_KEY: 'mask_train_v2', TEST_DIR_KEY: 'mask_test_v2', } } class MSMT17(ImageDataset): """MSMT17. Reference: Wei et al. Person Transfer GAN to Bridge Domain Gap for Person Re-Identification. CVPR 2018. URL: ``_ Dataset statistics: - identities: 4101. - images: 32621 (train) + 11659 (query) + 82161 (gallery). - cameras: 15. """ dataset_dir = 'msmt17' dataset_url = None def __init__(self, root='', **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) has_main_dir = False for main_dir in VERSION_DICT: if osp.exists(osp.join(self.dataset_dir, main_dir)): train_dir = VERSION_DICT[main_dir][TRAIN_DIR_KEY] test_dir = VERSION_DICT[main_dir][TEST_DIR_KEY] has_main_dir = True break assert has_main_dir, 'Dataset folder not found' self.train_dir = osp.join(self.dataset_dir, main_dir, train_dir) self.test_dir = osp.join(self.dataset_dir, main_dir, test_dir) self.list_train_path = osp.join( self.dataset_dir, main_dir, 'list_train.txt' ) self.list_val_path = osp.join( self.dataset_dir, main_dir, 'list_val.txt' ) self.list_query_path = osp.join( self.dataset_dir, main_dir, 'list_query.txt' ) self.list_gallery_path = osp.join( self.dataset_dir, main_dir, 'list_gallery.txt' ) required_files = [self.dataset_dir, self.train_dir, self.test_dir] self.check_before_run(required_files) train = self.process_dir(self.train_dir, self.list_train_path) val = self.process_dir(self.train_dir, self.list_val_path) query = self.process_dir(self.test_dir, self.list_query_path) gallery = self.process_dir(self.test_dir, self.list_gallery_path) # Note: to fairly compare with published methods on the conventional ReID setting, # do not add val images to the training set. if 'combineall' in kwargs and kwargs['combineall']: train += val super(MSMT17, self).__init__(train, query, gallery, **kwargs) def process_dir(self, dir_path, list_path): with open(list_path, 'r') as txt: lines = txt.readlines() data = [] for img_idx, img_info in enumerate(lines): img_path, pid = img_info.split(' ') pid = int(pid) # no need to relabel camid = int(img_path.split('_')[2]) - 1 # index starts from 0 img_path = osp.join(dir_path, img_path) data.append((img_path, pid, camid)) return data ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/prid.py ================================================ from __future__ import division, print_function, absolute_import import random import os.path as osp from torchreid.utils import read_json, write_json from ..dataset import ImageDataset class PRID(ImageDataset): """PRID (single-shot version of prid-2011) Reference: Hirzer et al. Person Re-Identification by Descriptive and Discriminative Classification. SCIA 2011. URL: ``_ Dataset statistics: - Two views. - View A captures 385 identities. - View B captures 749 identities. - 200 identities appear in both views (index starts from 1 to 200). """ dataset_dir = 'prid2011' dataset_url = None _junk_pids = list(range(201, 750)) def __init__(self, root='', split_id=0, **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.cam_a_dir = osp.join( self.dataset_dir, 'prid_2011', 'single_shot', 'cam_a' ) self.cam_b_dir = osp.join( self.dataset_dir, 'prid_2011', 'single_shot', 'cam_b' ) self.split_path = osp.join(self.dataset_dir, 'splits_single_shot.json') required_files = [self.dataset_dir, self.cam_a_dir, self.cam_b_dir] self.check_before_run(required_files) self.prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): raise ValueError( 'split_id exceeds range, received {}, but expected between 0 and {}' .format(split_id, len(splits) - 1) ) split = splits[split_id] train, query, gallery = self.process_split(split) super(PRID, self).__init__(train, query, gallery, **kwargs) def prepare_split(self): if not osp.exists(self.split_path): print('Creating splits ...') splits = [] for _ in range(10): # randomly sample 100 IDs for train and use the rest 100 IDs for test # (note: there are only 200 IDs appearing in both views) pids = [i for i in range(1, 201)] train_pids = random.sample(pids, 100) train_pids.sort() test_pids = [i for i in pids if i not in train_pids] split = {'train': train_pids, 'test': test_pids} splits.append(split) print('Totally {} splits are created'.format(len(splits))) write_json(splits, self.split_path) print('Split file is saved to {}'.format(self.split_path)) def process_split(self, split): train_pids = split['train'] test_pids = split['test'] train_pid2label = {pid: label for label, pid in enumerate(train_pids)} # train train = [] for pid in train_pids: img_name = 'person_' + str(pid).zfill(4) + '.png' pid = train_pid2label[pid] img_a_path = osp.join(self.cam_a_dir, img_name) train.append((img_a_path, pid, 0)) img_b_path = osp.join(self.cam_b_dir, img_name) train.append((img_b_path, pid, 1)) # query and gallery query, gallery = [], [] for pid in test_pids: img_name = 'person_' + str(pid).zfill(4) + '.png' img_a_path = osp.join(self.cam_a_dir, img_name) query.append((img_a_path, pid, 0)) img_b_path = osp.join(self.cam_b_dir, img_name) gallery.append((img_b_path, pid, 1)) for pid in range(201, 750): img_name = 'person_' + str(pid).zfill(4) + '.png' img_b_path = osp.join(self.cam_b_dir, img_name) gallery.append((img_b_path, pid, 1)) return train, query, gallery ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/sensereid.py ================================================ from __future__ import division, print_function, absolute_import import copy import glob import os.path as osp from ..dataset import ImageDataset class SenseReID(ImageDataset): """SenseReID. This dataset is used for test purpose only. Reference: Zhao et al. Spindle Net: Person Re-identification with Human Body Region Guided Feature Decomposition and Fusion. CVPR 2017. URL: ``_ Dataset statistics: - query: 522 ids, 1040 images. - gallery: 1717 ids, 3388 images. """ dataset_dir = 'sensereid' dataset_url = None def __init__(self, root='', **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.query_dir = osp.join(self.dataset_dir, 'SenseReID', 'test_probe') self.gallery_dir = osp.join( self.dataset_dir, 'SenseReID', 'test_gallery' ) required_files = [self.dataset_dir, self.query_dir, self.gallery_dir] self.check_before_run(required_files) query = self.process_dir(self.query_dir) gallery = self.process_dir(self.gallery_dir) # relabel g_pids = set() for _, pid, _ in gallery: g_pids.add(pid) pid2label = {pid: i for i, pid in enumerate(g_pids)} query = [ (img_path, pid2label[pid], camid) for img_path, pid, camid in query ] gallery = [ (img_path, pid2label[pid], camid) for img_path, pid, camid in gallery ] train = copy.deepcopy(query) + copy.deepcopy(gallery) # dummy variable super(SenseReID, self).__init__(train, query, gallery, **kwargs) def process_dir(self, dir_path): img_paths = glob.glob(osp.join(dir_path, '*.jpg')) data = [] for img_path in img_paths: img_name = osp.splitext(osp.basename(img_path))[0] pid, camid = img_name.split('_') pid, camid = int(pid), int(camid) data.append((img_path, pid, camid)) return data ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/university1652.py ================================================ from __future__ import division, print_function, absolute_import import os import glob import os.path as osp import gdown from ..dataset import ImageDataset class University1652(ImageDataset): """University-1652. Reference: - Zheng et al. University-1652: A Multi-view Multi-source Benchmark for Drone-based Geo-localization. ACM MM 2020. URL: ``_ OneDrive: https://studentutsedu-my.sharepoint.com/:u:/g/personal/12639605_student_uts_edu_au/Ecrz6xK-PcdCjFdpNb0T0s8B_9J5ynaUy3q63_XumjJyrA?e=z4hpcz [Backup] GoogleDrive: https://drive.google.com/file/d/1iVnP4gjw-iHXa0KerZQ1IfIO0i1jADsR/view?usp=sharing [Backup] Baidu Yun: https://pan.baidu.com/s/1H_wBnWwikKbaBY1pMPjoqQ password: hrqp Dataset statistics: - buildings: 1652 (train + query). - The dataset split is as follows: | Split | #imgs | #buildings | #universities| | -------- | ----- | ----| ----| | Training | 50,218 | 701 | 33 | | Query_drone | 37,855 | 701 | 39 | | Query_satellite | 701 | 701 | 39| | Query_ground | 2,579 | 701 | 39| | Gallery_drone | 51,355 | 951 | 39| | Gallery_satellite | 951 | 951 | 39| | Gallery_ground | 2,921 | 793 | 39| - cameras: None. datamanager = torchreid.data.ImageDataManager( root='reid-data', sources='university1652', targets='university1652', height=256, width=256, batch_size_train=32, batch_size_test=100, transforms=['random_flip', 'random_crop'] ) """ dataset_dir = 'university1652' dataset_url = 'https://drive.google.com/uc?id=1iVnP4gjw-iHXa0KerZQ1IfIO0i1jADsR' def __init__(self, root='', **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) print(self.dataset_dir) if not os.path.isdir(self.dataset_dir): os.mkdir(self.dataset_dir) gdown.download( self.dataset_url, self.dataset_dir + 'data.zip', quiet=False ) os.system('unzip %s' % (self.dataset_dir + 'data.zip')) self.train_dir = osp.join( self.dataset_dir, 'University-Release/train/' ) self.query_dir = osp.join( self.dataset_dir, 'University-Release/test/query_drone' ) self.gallery_dir = osp.join( self.dataset_dir, 'University-Release/test/gallery_satellite' ) required_files = [ self.dataset_dir, self.train_dir, self.query_dir, self.gallery_dir ] self.check_before_run(required_files) self.fake_camid = 0 train = self.process_dir(self.train_dir, relabel=True, train=True) query = self.process_dir(self.query_dir, relabel=False) gallery = self.process_dir(self.gallery_dir, relabel=False) super(University1652, self).__init__(train, query, gallery, **kwargs) def process_dir(self, dir_path, relabel=False, train=False): IMG_EXTENSIONS = ( '.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif', '.tiff', '.webp' ) if train: img_paths = glob.glob(osp.join(dir_path, '*/*/*')) else: img_paths = glob.glob(osp.join(dir_path, '*/*')) pid_container = set() for img_path in img_paths: if not img_path.lower().endswith(IMG_EXTENSIONS): continue pid = int(os.path.basename(os.path.dirname(img_path))) pid_container.add(pid) pid2label = {pid: label for label, pid in enumerate(pid_container)} data = [] # no camera for university for img_path in img_paths: if not img_path.lower().endswith(IMG_EXTENSIONS): continue pid = int(os.path.basename(os.path.dirname(img_path))) if relabel: pid = pid2label[pid] data.append((img_path, pid, self.fake_camid)) self.fake_camid += 1 return data ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/image/viper.py ================================================ from __future__ import division, print_function, absolute_import import glob import numpy as np import os.path as osp from torchreid.utils import read_json, write_json from ..dataset import ImageDataset class VIPeR(ImageDataset): """VIPeR. Reference: Gray et al. Evaluating appearance models for recognition, reacquisition, and tracking. PETS 2007. URL: ``_ Dataset statistics: - identities: 632. - images: 632 x 2 = 1264. - cameras: 2. """ dataset_dir = 'viper' dataset_url = 'http://users.soe.ucsc.edu/~manduchi/VIPeR.v1.0.zip' def __init__(self, root='', split_id=0, **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.cam_a_dir = osp.join(self.dataset_dir, 'VIPeR', 'cam_a') self.cam_b_dir = osp.join(self.dataset_dir, 'VIPeR', 'cam_b') self.split_path = osp.join(self.dataset_dir, 'splits.json') required_files = [self.dataset_dir, self.cam_a_dir, self.cam_b_dir] self.check_before_run(required_files) self.prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): raise ValueError( 'split_id exceeds range, received {}, ' 'but expected between 0 and {}'.format( split_id, len(splits) - 1 ) ) split = splits[split_id] train = split['train'] query = split['query'] # query and gallery share the same images gallery = split['gallery'] train = [tuple(item) for item in train] query = [tuple(item) for item in query] gallery = [tuple(item) for item in gallery] super(VIPeR, self).__init__(train, query, gallery, **kwargs) def prepare_split(self): if not osp.exists(self.split_path): print('Creating 10 random splits of train ids and test ids') cam_a_imgs = sorted(glob.glob(osp.join(self.cam_a_dir, '*.bmp'))) cam_b_imgs = sorted(glob.glob(osp.join(self.cam_b_dir, '*.bmp'))) assert len(cam_a_imgs) == len(cam_b_imgs) num_pids = len(cam_a_imgs) print('Number of identities: {}'.format(num_pids)) num_train_pids = num_pids // 2 """ In total, there will be 20 splits because each random split creates two sub-splits, one using cameraA as query and cameraB as gallery while the other using cameraB as query and cameraA as gallery. Therefore, results should be averaged over 20 splits (split_id=0~19). In practice, a model trained on split_id=0 can be applied to split_id=0&1 as split_id=0&1 share the same training data (so on and so forth). """ splits = [] for _ in range(10): order = np.arange(num_pids) np.random.shuffle(order) train_idxs = order[:num_train_pids] test_idxs = order[num_train_pids:] assert not bool(set(train_idxs) & set(test_idxs)), \ 'Error: train and test overlap' train = [] for pid, idx in enumerate(train_idxs): cam_a_img = cam_a_imgs[idx] cam_b_img = cam_b_imgs[idx] train.append((cam_a_img, pid, 0)) train.append((cam_b_img, pid, 1)) test_a = [] test_b = [] for pid, idx in enumerate(test_idxs): cam_a_img = cam_a_imgs[idx] cam_b_img = cam_b_imgs[idx] test_a.append((cam_a_img, pid, 0)) test_b.append((cam_b_img, pid, 1)) # use cameraA as query and cameraB as gallery split = { 'train': train, 'query': test_a, 'gallery': test_b, 'num_train_pids': num_train_pids, 'num_query_pids': num_pids - num_train_pids, 'num_gallery_pids': num_pids - num_train_pids } splits.append(split) # use cameraB as query and cameraA as gallery split = { 'train': train, 'query': test_b, 'gallery': test_a, 'num_train_pids': num_train_pids, 'num_query_pids': num_pids - num_train_pids, 'num_gallery_pids': num_pids - num_train_pids } splits.append(split) print('Totally {} splits are created'.format(len(splits))) write_json(splits, self.split_path) print('Split file saved to {}'.format(self.split_path)) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/video/__init__.py ================================================ from __future__ import print_function, absolute_import from .mars import Mars from .ilidsvid import iLIDSVID from .prid2011 import PRID2011 from .dukemtmcvidreid import DukeMTMCVidReID ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/video/dukemtmcvidreid.py ================================================ from __future__ import division, print_function, absolute_import import glob import os.path as osp import warnings from torchreid.utils import read_json, write_json from ..dataset import VideoDataset class DukeMTMCVidReID(VideoDataset): """DukeMTMCVidReID. Reference: - Ristani et al. Performance Measures and a Data Set for Multi-Target, Multi-Camera Tracking. ECCVW 2016. - Wu et al. Exploit the Unknown Gradually: One-Shot Video-Based Person Re-Identification by Stepwise Learning. CVPR 2018. URL: ``_ Dataset statistics: - identities: 702 (train) + 702 (test). - tracklets: 2196 (train) + 2636 (test). """ dataset_dir = 'dukemtmc-vidreid' dataset_url = 'http://vision.cs.duke.edu/DukeMTMC/data/misc/DukeMTMC-VideoReID.zip' def __init__(self, root='', min_seq_len=0, **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.train_dir = osp.join(self.dataset_dir, 'DukeMTMC-VideoReID/train') self.query_dir = osp.join(self.dataset_dir, 'DukeMTMC-VideoReID/query') self.gallery_dir = osp.join( self.dataset_dir, 'DukeMTMC-VideoReID/gallery' ) self.split_train_json_path = osp.join( self.dataset_dir, 'split_train.json' ) self.split_query_json_path = osp.join( self.dataset_dir, 'split_query.json' ) self.split_gallery_json_path = osp.join( self.dataset_dir, 'split_gallery.json' ) self.min_seq_len = min_seq_len required_files = [ self.dataset_dir, self.train_dir, self.query_dir, self.gallery_dir ] self.check_before_run(required_files) train = self.process_dir( self.train_dir, self.split_train_json_path, relabel=True ) query = self.process_dir( self.query_dir, self.split_query_json_path, relabel=False ) gallery = self.process_dir( self.gallery_dir, self.split_gallery_json_path, relabel=False ) super(DukeMTMCVidReID, self).__init__(train, query, gallery, **kwargs) def process_dir(self, dir_path, json_path, relabel): if osp.exists(json_path): split = read_json(json_path) return split['tracklets'] print('=> Generating split json file (** this might take a while **)') pdirs = glob.glob(osp.join(dir_path, '*')) # avoid .DS_Store print( 'Processing "{}" with {} person identities'.format( dir_path, len(pdirs) ) ) pid_container = set() for pdir in pdirs: pid = int(osp.basename(pdir)) pid_container.add(pid) pid2label = {pid: label for label, pid in enumerate(pid_container)} tracklets = [] for pdir in pdirs: pid = int(osp.basename(pdir)) if relabel: pid = pid2label[pid] tdirs = glob.glob(osp.join(pdir, '*')) for tdir in tdirs: raw_img_paths = glob.glob(osp.join(tdir, '*.jpg')) num_imgs = len(raw_img_paths) if num_imgs < self.min_seq_len: continue img_paths = [] for img_idx in range(num_imgs): # some tracklet starts from 0002 instead of 0001 img_idx_name = 'F' + str(img_idx + 1).zfill(4) res = glob.glob( osp.join(tdir, '*' + img_idx_name + '*.jpg') ) if len(res) == 0: warnings.warn( 'Index name {} in {} is missing, skip'.format( img_idx_name, tdir ) ) continue img_paths.append(res[0]) img_name = osp.basename(img_paths[0]) if img_name.find('_') == -1: # old naming format: 0001C6F0099X30823.jpg camid = int(img_name[5]) - 1 else: # new naming format: 0001_C6_F0099_X30823.jpg camid = int(img_name[6]) - 1 img_paths = tuple(img_paths) tracklets.append((img_paths, pid, camid)) print('Saving split to {}'.format(json_path)) split_dict = {'tracklets': tracklets} write_json(split_dict, json_path) return tracklets ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/video/ilidsvid.py ================================================ from __future__ import division, print_function, absolute_import import glob import os.path as osp from scipy.io import loadmat from torchreid.utils import read_json, write_json from ..dataset import VideoDataset class iLIDSVID(VideoDataset): """iLIDS-VID. Reference: Wang et al. Person Re-Identification by Video Ranking. ECCV 2014. URL: ``_ Dataset statistics: - identities: 300. - tracklets: 600. - cameras: 2. """ dataset_dir = 'ilids-vid' dataset_url = 'http://www.eecs.qmul.ac.uk/~xiatian/iLIDS-VID/iLIDS-VID.tar' def __init__(self, root='', split_id=0, **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.data_dir = osp.join(self.dataset_dir, 'i-LIDS-VID') self.split_dir = osp.join(self.dataset_dir, 'train-test people splits') self.split_mat_path = osp.join( self.split_dir, 'train_test_splits_ilidsvid.mat' ) self.split_path = osp.join(self.dataset_dir, 'splits.json') self.cam_1_path = osp.join( self.dataset_dir, 'i-LIDS-VID/sequences/cam1' ) self.cam_2_path = osp.join( self.dataset_dir, 'i-LIDS-VID/sequences/cam2' ) required_files = [self.dataset_dir, self.data_dir, self.split_dir] self.check_before_run(required_files) self.prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): raise ValueError( 'split_id exceeds range, received {}, but expected between 0 and {}' .format(split_id, len(splits) - 1) ) split = splits[split_id] train_dirs, test_dirs = split['train'], split['test'] train = self.process_data(train_dirs, cam1=True, cam2=True) query = self.process_data(test_dirs, cam1=True, cam2=False) gallery = self.process_data(test_dirs, cam1=False, cam2=True) super(iLIDSVID, self).__init__(train, query, gallery, **kwargs) def prepare_split(self): if not osp.exists(self.split_path): print('Creating splits ...') mat_split_data = loadmat(self.split_mat_path)['ls_set'] num_splits = mat_split_data.shape[0] num_total_ids = mat_split_data.shape[1] assert num_splits == 10 assert num_total_ids == 300 num_ids_each = num_total_ids // 2 # pids in mat_split_data are indices, so we need to transform them # to real pids person_cam1_dirs = sorted( glob.glob(osp.join(self.cam_1_path, '*')) ) person_cam2_dirs = sorted( glob.glob(osp.join(self.cam_2_path, '*')) ) person_cam1_dirs = [ osp.basename(item) for item in person_cam1_dirs ] person_cam2_dirs = [ osp.basename(item) for item in person_cam2_dirs ] # make sure persons in one camera view can be found in the other camera view assert set(person_cam1_dirs) == set(person_cam2_dirs) splits = [] for i_split in range(num_splits): # first 50% for testing and the remaining for training, following Wang et al. ECCV'14. train_idxs = sorted( list(mat_split_data[i_split, num_ids_each:]) ) test_idxs = sorted( list(mat_split_data[i_split, :num_ids_each]) ) train_idxs = [int(i) - 1 for i in train_idxs] test_idxs = [int(i) - 1 for i in test_idxs] # transform pids to person dir names train_dirs = [person_cam1_dirs[i] for i in train_idxs] test_dirs = [person_cam1_dirs[i] for i in test_idxs] split = {'train': train_dirs, 'test': test_dirs} splits.append(split) print( 'Totally {} splits are created, following Wang et al. ECCV\'14' .format(len(splits)) ) print('Split file is saved to {}'.format(self.split_path)) write_json(splits, self.split_path) def process_data(self, dirnames, cam1=True, cam2=True): tracklets = [] dirname2pid = {dirname: i for i, dirname in enumerate(dirnames)} for dirname in dirnames: if cam1: person_dir = osp.join(self.cam_1_path, dirname) img_names = glob.glob(osp.join(person_dir, '*.png')) assert len(img_names) > 0 img_names = tuple(img_names) pid = dirname2pid[dirname] tracklets.append((img_names, pid, 0)) if cam2: person_dir = osp.join(self.cam_2_path, dirname) img_names = glob.glob(osp.join(person_dir, '*.png')) assert len(img_names) > 0 img_names = tuple(img_names) pid = dirname2pid[dirname] tracklets.append((img_names, pid, 1)) return tracklets ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/video/mars.py ================================================ from __future__ import division, print_function, absolute_import import os.path as osp import warnings from scipy.io import loadmat from ..dataset import VideoDataset class Mars(VideoDataset): """MARS. Reference: Zheng et al. MARS: A Video Benchmark for Large-Scale Person Re-identification. ECCV 2016. URL: ``_ Dataset statistics: - identities: 1261. - tracklets: 8298 (train) + 1980 (query) + 9330 (gallery). - cameras: 6. """ dataset_dir = 'mars' dataset_url = None def __init__(self, root='', **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.train_name_path = osp.join( self.dataset_dir, 'info/train_name.txt' ) self.test_name_path = osp.join(self.dataset_dir, 'info/test_name.txt') self.track_train_info_path = osp.join( self.dataset_dir, 'info/tracks_train_info.mat' ) self.track_test_info_path = osp.join( self.dataset_dir, 'info/tracks_test_info.mat' ) self.query_IDX_path = osp.join(self.dataset_dir, 'info/query_IDX.mat') required_files = [ self.dataset_dir, self.train_name_path, self.test_name_path, self.track_train_info_path, self.track_test_info_path, self.query_IDX_path ] self.check_before_run(required_files) train_names = self.get_names(self.train_name_path) test_names = self.get_names(self.test_name_path) track_train = loadmat(self.track_train_info_path )['track_train_info'] # numpy.ndarray (8298, 4) track_test = loadmat(self.track_test_info_path )['track_test_info'] # numpy.ndarray (12180, 4) query_IDX = loadmat(self.query_IDX_path )['query_IDX'].squeeze() # numpy.ndarray (1980,) query_IDX -= 1 # index from 0 track_query = track_test[query_IDX, :] gallery_IDX = [ i for i in range(track_test.shape[0]) if i not in query_IDX ] track_gallery = track_test[gallery_IDX, :] train = self.process_data( train_names, track_train, home_dir='bbox_train', relabel=True ) query = self.process_data( test_names, track_query, home_dir='bbox_test', relabel=False ) gallery = self.process_data( test_names, track_gallery, home_dir='bbox_test', relabel=False ) super(Mars, self).__init__(train, query, gallery, **kwargs) def get_names(self, fpath): names = [] with open(fpath, 'r') as f: for line in f: new_line = line.rstrip() names.append(new_line) return names def process_data( self, names, meta_data, home_dir=None, relabel=False, min_seq_len=0 ): assert home_dir in ['bbox_train', 'bbox_test'] num_tracklets = meta_data.shape[0] pid_list = list(set(meta_data[:, 2].tolist())) if relabel: pid2label = {pid: label for label, pid in enumerate(pid_list)} tracklets = [] for tracklet_idx in range(num_tracklets): data = meta_data[tracklet_idx, ...] start_index, end_index, pid, camid = data if pid == -1: continue # junk images are just ignored assert 1 <= camid <= 6 if relabel: pid = pid2label[pid] camid -= 1 # index starts from 0 img_names = names[start_index - 1:end_index] # make sure image names correspond to the same person pnames = [img_name[:4] for img_name in img_names] assert len( set(pnames) ) == 1, 'Error: a single tracklet contains different person images' # make sure all images are captured under the same camera camnames = [img_name[5] for img_name in img_names] assert len( set(camnames) ) == 1, 'Error: images are captured under different cameras!' # append image names with directory information img_paths = [ osp.join(self.dataset_dir, home_dir, img_name[:4], img_name) for img_name in img_names ] if len(img_paths) >= min_seq_len: img_paths = tuple(img_paths) tracklets.append((img_paths, pid, camid)) return tracklets def combine_all(self): warnings.warn( 'Some query IDs do not appear in gallery. Therefore, combineall ' 'does not make any difference to Mars' ) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/datasets/video/prid2011.py ================================================ from __future__ import division, print_function, absolute_import import glob import os.path as osp from torchreid.utils import read_json from ..dataset import VideoDataset class PRID2011(VideoDataset): """PRID2011. Reference: Hirzer et al. Person Re-Identification by Descriptive and Discriminative Classification. SCIA 2011. URL: ``_ Dataset statistics: - identities: 200. - tracklets: 400. - cameras: 2. """ dataset_dir = 'prid2011' dataset_url = None def __init__(self, root='', split_id=0, **kwargs): self.root = osp.abspath(osp.expanduser(root)) self.dataset_dir = osp.join(self.root, self.dataset_dir) self.download_dataset(self.dataset_dir, self.dataset_url) self.split_path = osp.join(self.dataset_dir, 'splits_prid2011.json') self.cam_a_dir = osp.join( self.dataset_dir, 'prid_2011', 'multi_shot', 'cam_a' ) self.cam_b_dir = osp.join( self.dataset_dir, 'prid_2011', 'multi_shot', 'cam_b' ) required_files = [self.dataset_dir, self.cam_a_dir, self.cam_b_dir] self.check_before_run(required_files) splits = read_json(self.split_path) if split_id >= len(splits): raise ValueError( 'split_id exceeds range, received {}, but expected between 0 and {}' .format(split_id, len(splits) - 1) ) split = splits[split_id] train_dirs, test_dirs = split['train'], split['test'] train = self.process_dir(train_dirs, cam1=True, cam2=True) query = self.process_dir(test_dirs, cam1=True, cam2=False) gallery = self.process_dir(test_dirs, cam1=False, cam2=True) super(PRID2011, self).__init__(train, query, gallery, **kwargs) def process_dir(self, dirnames, cam1=True, cam2=True): tracklets = [] dirname2pid = {dirname: i for i, dirname in enumerate(dirnames)} for dirname in dirnames: if cam1: person_dir = osp.join(self.cam_a_dir, dirname) img_names = glob.glob(osp.join(person_dir, '*.png')) assert len(img_names) > 0 img_names = tuple(img_names) pid = dirname2pid[dirname] tracklets.append((img_names, pid, 0)) if cam2: person_dir = osp.join(self.cam_b_dir, dirname) img_names = glob.glob(osp.join(person_dir, '*.png')) assert len(img_names) > 0 img_names = tuple(img_names) pid = dirname2pid[dirname] tracklets.append((img_names, pid, 1)) return tracklets ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/sampler.py ================================================ from __future__ import division, absolute_import import copy import numpy as np import random from collections import defaultdict from torch.utils.data.sampler import Sampler, RandomSampler, SequentialSampler AVAI_SAMPLERS = [ 'RandomIdentitySampler', 'SequentialSampler', 'RandomSampler', 'RandomDomainSampler', 'RandomDatasetSampler' ] class RandomIdentitySampler(Sampler): """Randomly samples N identities each with K instances. Args: data_source (list): contains tuples of (img_path(s), pid, camid, dsetid). batch_size (int): batch size. num_instances (int): number of instances per identity in a batch. """ def __init__(self, data_source, batch_size, num_instances): if batch_size < num_instances: raise ValueError( 'batch_size={} must be no less ' 'than num_instances={}'.format(batch_size, num_instances) ) self.data_source = data_source self.batch_size = batch_size self.num_instances = num_instances self.num_pids_per_batch = self.batch_size // self.num_instances self.index_dic = defaultdict(list) for index, items in enumerate(data_source): pid = items[1] self.index_dic[pid].append(index) self.pids = list(self.index_dic.keys()) assert len(self.pids) >= self.num_pids_per_batch # estimate number of examples in an epoch # TODO: improve precision self.length = 0 for pid in self.pids: idxs = self.index_dic[pid] num = len(idxs) if num < self.num_instances: num = self.num_instances self.length += num - num % self.num_instances def __iter__(self): batch_idxs_dict = defaultdict(list) for pid in self.pids: idxs = copy.deepcopy(self.index_dic[pid]) if len(idxs) < self.num_instances: idxs = np.random.choice( idxs, size=self.num_instances, replace=True ) random.shuffle(idxs) batch_idxs = [] for idx in idxs: batch_idxs.append(idx) if len(batch_idxs) == self.num_instances: batch_idxs_dict[pid].append(batch_idxs) batch_idxs = [] avai_pids = copy.deepcopy(self.pids) final_idxs = [] while len(avai_pids) >= self.num_pids_per_batch: selected_pids = random.sample(avai_pids, self.num_pids_per_batch) for pid in selected_pids: batch_idxs = batch_idxs_dict[pid].pop(0) final_idxs.extend(batch_idxs) if len(batch_idxs_dict[pid]) == 0: avai_pids.remove(pid) return iter(final_idxs) def __len__(self): return self.length class RandomDomainSampler(Sampler): """Random domain sampler. We consider each camera as a visual domain. How does the sampling work: 1. Randomly sample N cameras (based on the "camid" label). 2. From each camera, randomly sample K images. Args: data_source (list): contains tuples of (img_path(s), pid, camid, dsetid). batch_size (int): batch size. n_domain (int): number of cameras to sample in a batch. """ def __init__(self, data_source, batch_size, n_domain): self.data_source = data_source # Keep track of image indices for each domain self.domain_dict = defaultdict(list) for i, items in enumerate(data_source): camid = items[2] self.domain_dict[camid].append(i) self.domains = list(self.domain_dict.keys()) # Make sure each domain can be assigned an equal number of images if n_domain is None or n_domain <= 0: n_domain = len(self.domains) assert batch_size % n_domain == 0 self.n_img_per_domain = batch_size // n_domain self.batch_size = batch_size self.n_domain = n_domain self.length = len(list(self.__iter__())) def __iter__(self): domain_dict = copy.deepcopy(self.domain_dict) final_idxs = [] stop_sampling = False while not stop_sampling: selected_domains = random.sample(self.domains, self.n_domain) for domain in selected_domains: idxs = domain_dict[domain] selected_idxs = random.sample(idxs, self.n_img_per_domain) final_idxs.extend(selected_idxs) for idx in selected_idxs: domain_dict[domain].remove(idx) remaining = len(domain_dict[domain]) if remaining < self.n_img_per_domain: stop_sampling = True return iter(final_idxs) def __len__(self): return self.length class RandomDatasetSampler(Sampler): """Random dataset sampler. How does the sampling work: 1. Randomly sample N datasets (based on the "dsetid" label). 2. From each dataset, randomly sample K images. Args: data_source (list): contains tuples of (img_path(s), pid, camid, dsetid). batch_size (int): batch size. n_dataset (int): number of datasets to sample in a batch. """ def __init__(self, data_source, batch_size, n_dataset): self.data_source = data_source # Keep track of image indices for each dataset self.dataset_dict = defaultdict(list) for i, items in enumerate(data_source): dsetid = items[3] self.dataset_dict[dsetid].append(i) self.datasets = list(self.dataset_dict.keys()) # Make sure each dataset can be assigned an equal number of images if n_dataset is None or n_dataset <= 0: n_dataset = len(self.datasets) assert batch_size % n_dataset == 0 self.n_img_per_dset = batch_size // n_dataset self.batch_size = batch_size self.n_dataset = n_dataset self.length = len(list(self.__iter__())) def __iter__(self): dataset_dict = copy.deepcopy(self.dataset_dict) final_idxs = [] stop_sampling = False while not stop_sampling: selected_datasets = random.sample(self.datasets, self.n_dataset) for dset in selected_datasets: idxs = dataset_dict[dset] selected_idxs = random.sample(idxs, self.n_img_per_dset) final_idxs.extend(selected_idxs) for idx in selected_idxs: dataset_dict[dset].remove(idx) remaining = len(dataset_dict[dset]) if remaining < self.n_img_per_dset: stop_sampling = True return iter(final_idxs) def __len__(self): return self.length def build_train_sampler( data_source, train_sampler, batch_size=32, num_instances=4, num_cams=1, num_datasets=1, **kwargs ): """Builds a training sampler. Args: data_source (list): contains tuples of (img_path(s), pid, camid). train_sampler (str): sampler name (default: ``RandomSampler``). batch_size (int, optional): batch size. Default is 32. num_instances (int, optional): number of instances per identity in a batch (when using ``RandomIdentitySampler``). Default is 4. num_cams (int, optional): number of cameras to sample in a batch (when using ``RandomDomainSampler``). Default is 1. num_datasets (int, optional): number of datasets to sample in a batch (when using ``RandomDatasetSampler``). Default is 1. """ assert train_sampler in AVAI_SAMPLERS, \ 'train_sampler must be one of {}, but got {}'.format(AVAI_SAMPLERS, train_sampler) if train_sampler == 'RandomIdentitySampler': sampler = RandomIdentitySampler(data_source, batch_size, num_instances) elif train_sampler == 'RandomDomainSampler': sampler = RandomDomainSampler(data_source, batch_size, num_cams) elif train_sampler == 'RandomDatasetSampler': sampler = RandomDatasetSampler(data_source, batch_size, num_datasets) elif train_sampler == 'SequentialSampler': sampler = SequentialSampler(data_source) elif train_sampler == 'RandomSampler': sampler = RandomSampler(data_source) return sampler ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/data/transforms.py ================================================ from __future__ import division, print_function, absolute_import import math import random from collections import deque import torch from PIL import Image from torchvision.transforms import ( Resize, Compose, ToTensor, Normalize, ColorJitter, RandomHorizontalFlip ) class Random2DTranslation(object): """Randomly translates the input image with a probability. Specifically, given a predefined shape (height, width), the input is first resized with a factor of 1.125, leading to (height*1.125, width*1.125), then a random crop is performed. Such operation is done with a probability. Args: height (int): target image height. width (int): target image width. p (float, optional): probability that this operation takes place. Default is 0.5. interpolation (int, optional): desired interpolation. Default is ``PIL.Image.BILINEAR`` """ def __init__(self, height, width, p=0.5, interpolation=Image.BILINEAR): self.height = height self.width = width self.p = p self.interpolation = interpolation def __call__(self, img): if random.uniform(0, 1) > self.p: return img.resize((self.width, self.height), self.interpolation) new_width, new_height = int(round(self.width * 1.125) ), int(round(self.height * 1.125)) resized_img = img.resize((new_width, new_height), self.interpolation) x_maxrange = new_width - self.width y_maxrange = new_height - self.height x1 = int(round(random.uniform(0, x_maxrange))) y1 = int(round(random.uniform(0, y_maxrange))) croped_img = resized_img.crop( (x1, y1, x1 + self.width, y1 + self.height) ) return croped_img class RandomErasing(object): """Randomly erases an image patch. Origin: ``_ Reference: Zhong et al. Random Erasing Data Augmentation. Args: probability (float, optional): probability that this operation takes place. Default is 0.5. sl (float, optional): min erasing area. sh (float, optional): max erasing area. r1 (float, optional): min aspect ratio. mean (list, optional): erasing value. """ def __init__( self, probability=0.5, sl=0.02, sh=0.4, r1=0.3, mean=[0.4914, 0.4822, 0.4465] ): self.probability = probability self.mean = mean self.sl = sl self.sh = sh self.r1 = r1 def __call__(self, img): if random.uniform(0, 1) > self.probability: return img for attempt in range(100): area = img.size()[1] * img.size()[2] target_area = random.uniform(self.sl, self.sh) * area aspect_ratio = random.uniform(self.r1, 1 / self.r1) h = int(round(math.sqrt(target_area * aspect_ratio))) w = int(round(math.sqrt(target_area / aspect_ratio))) if w < img.size()[2] and h < img.size()[1]: x1 = random.randint(0, img.size()[1] - h) y1 = random.randint(0, img.size()[2] - w) if img.size()[0] == 3: img[0, x1:x1 + h, y1:y1 + w] = self.mean[0] img[1, x1:x1 + h, y1:y1 + w] = self.mean[1] img[2, x1:x1 + h, y1:y1 + w] = self.mean[2] else: img[0, x1:x1 + h, y1:y1 + w] = self.mean[0] return img return img class ColorAugmentation(object): """Randomly alters the intensities of RGB channels. Reference: Krizhevsky et al. ImageNet Classification with Deep ConvolutionalNeural Networks. NIPS 2012. Args: p (float, optional): probability that this operation takes place. Default is 0.5. """ def __init__(self, p=0.5): self.p = p self.eig_vec = torch.Tensor( [ [0.4009, 0.7192, -0.5675], [-0.8140, -0.0045, -0.5808], [0.4203, -0.6948, -0.5836], ] ) self.eig_val = torch.Tensor([[0.2175, 0.0188, 0.0045]]) def _check_input(self, tensor): assert tensor.dim() == 3 and tensor.size(0) == 3 def __call__(self, tensor): if random.uniform(0, 1) > self.p: return tensor alpha = torch.normal(mean=torch.zeros_like(self.eig_val)) * 0.1 quatity = torch.mm(self.eig_val * alpha, self.eig_vec) tensor = tensor + quatity.view(3, 1, 1) return tensor class RandomPatch(object): """Random patch data augmentation. There is a patch pool that stores randomly extracted pathces from person images. For each input image, RandomPatch 1) extracts a random patch and stores the patch in the patch pool; 2) randomly selects a patch from the patch pool and pastes it on the input (at random position) to simulate occlusion. Reference: - Zhou et al. Omni-Scale Feature Learning for Person Re-Identification. ICCV, 2019. - Zhou et al. Learning Generalisable Omni-Scale Representations for Person Re-Identification. TPAMI, 2021. """ def __init__( self, prob_happen=0.5, pool_capacity=50000, min_sample_size=100, patch_min_area=0.01, patch_max_area=0.5, patch_min_ratio=0.1, prob_rotate=0.5, prob_flip_leftright=0.5, ): self.prob_happen = prob_happen self.patch_min_area = patch_min_area self.patch_max_area = patch_max_area self.patch_min_ratio = patch_min_ratio self.prob_rotate = prob_rotate self.prob_flip_leftright = prob_flip_leftright self.patchpool = deque(maxlen=pool_capacity) self.min_sample_size = min_sample_size def generate_wh(self, W, H): area = W * H for attempt in range(100): target_area = random.uniform( self.patch_min_area, self.patch_max_area ) * area aspect_ratio = random.uniform( self.patch_min_ratio, 1. / self.patch_min_ratio ) h = int(round(math.sqrt(target_area * aspect_ratio))) w = int(round(math.sqrt(target_area / aspect_ratio))) if w < W and h < H: return w, h return None, None def transform_patch(self, patch): if random.uniform(0, 1) > self.prob_flip_leftright: patch = patch.transpose(Image.FLIP_LEFT_RIGHT) if random.uniform(0, 1) > self.prob_rotate: patch = patch.rotate(random.randint(-10, 10)) return patch def __call__(self, img): W, H = img.size # original image size # collect new patch w, h = self.generate_wh(W, H) if w is not None and h is not None: x1 = random.randint(0, W - w) y1 = random.randint(0, H - h) new_patch = img.crop((x1, y1, x1 + w, y1 + h)) self.patchpool.append(new_patch) if len(self.patchpool) < self.min_sample_size: return img if random.uniform(0, 1) > self.prob_happen: return img # paste a randomly selected patch on a random position patch = random.sample(self.patchpool, 1)[0] patchW, patchH = patch.size x1 = random.randint(0, W - patchW) y1 = random.randint(0, H - patchH) patch = self.transform_patch(patch) img.paste(patch, (x1, y1)) return img def build_transforms( height, width, transforms='random_flip', norm_mean=[0.485, 0.456, 0.406], norm_std=[0.229, 0.224, 0.225], **kwargs ): """Builds train and test transform functions. Args: height (int): target image height. width (int): target image width. transforms (str or list of str, optional): transformations applied to model training. Default is 'random_flip'. norm_mean (list or None, optional): normalization mean values. Default is ImageNet means. norm_std (list or None, optional): normalization standard deviation values. Default is ImageNet standard deviation values. """ if transforms is None: transforms = [] if isinstance(transforms, str): transforms = [transforms] if not isinstance(transforms, list): raise ValueError( 'transforms must be a list of strings, but found to be {}'.format( type(transforms) ) ) if len(transforms) > 0: transforms = [t.lower() for t in transforms] if norm_mean is None or norm_std is None: norm_mean = [0.485, 0.456, 0.406] # imagenet mean norm_std = [0.229, 0.224, 0.225] # imagenet std normalize = Normalize(mean=norm_mean, std=norm_std) print('Building train transforms ...') transform_tr = [] print('+ resize to {}x{}'.format(height, width)) transform_tr += [Resize((height, width))] if 'random_flip' in transforms: print('+ random flip') transform_tr += [RandomHorizontalFlip()] if 'random_crop' in transforms: print( '+ random crop (enlarge to {}x{} and ' 'crop {}x{})'.format( int(round(height * 1.125)), int(round(width * 1.125)), height, width ) ) transform_tr += [Random2DTranslation(height, width)] if 'random_patch' in transforms: print('+ random patch') transform_tr += [RandomPatch()] if 'color_jitter' in transforms: print('+ color jitter') transform_tr += [ ColorJitter(brightness=0.2, contrast=0.15, saturation=0, hue=0) ] print('+ to torch tensor of range [0, 1]') transform_tr += [ToTensor()] print('+ normalization (mean={}, std={})'.format(norm_mean, norm_std)) transform_tr += [normalize] if 'random_erase' in transforms: print('+ random erase') transform_tr += [RandomErasing(mean=norm_mean)] transform_tr = Compose(transform_tr) print('Building test transforms ...') print('+ resize to {}x{}'.format(height, width)) print('+ to torch tensor of range [0, 1]') print('+ normalization (mean={}, std={})'.format(norm_mean, norm_std)) transform_te = Compose([ Resize((height, width)), ToTensor(), normalize, ]) return transform_tr, transform_te ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/engine/__init__.py ================================================ from __future__ import print_function, absolute_import from .image import ImageSoftmaxEngine, ImageTripletEngine from .video import VideoSoftmaxEngine, VideoTripletEngine from .engine import Engine ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/engine/engine.py ================================================ from __future__ import division, print_function, absolute_import import time import numpy as np import os.path as osp import datetime from collections import OrderedDict import torch from torch.nn import functional as F from torch.utils.tensorboard import SummaryWriter from torchreid import metrics from torchreid.utils import ( MetricMeter, AverageMeter, re_ranking, open_all_layers, save_checkpoint, open_specified_layers, visualize_ranked_results ) from torchreid.losses import DeepSupervision class Engine(object): r"""A generic base Engine class for both image- and video-reid. Args: datamanager (DataManager): an instance of ``torchreid.data.ImageDataManager`` or ``torchreid.data.VideoDataManager``. use_gpu (bool, optional): use gpu. Default is True. """ def __init__(self, datamanager, use_gpu=True): self.datamanager = datamanager self.train_loader = self.datamanager.train_loader self.test_loader = self.datamanager.test_loader self.use_gpu = (torch.cuda.is_available() and use_gpu) self.writer = None self.epoch = 0 self.model = None self.optimizer = None self.scheduler = None self._models = OrderedDict() self._optims = OrderedDict() self._scheds = OrderedDict() def register_model(self, name='model', model=None, optim=None, sched=None): if self.__dict__.get('_models') is None: raise AttributeError( 'Cannot assign model before super().__init__() call' ) if self.__dict__.get('_optims') is None: raise AttributeError( 'Cannot assign optim before super().__init__() call' ) if self.__dict__.get('_scheds') is None: raise AttributeError( 'Cannot assign sched before super().__init__() call' ) self._models[name] = model self._optims[name] = optim self._scheds[name] = sched def get_model_names(self, names=None): names_real = list(self._models.keys()) if names is not None: if not isinstance(names, list): names = [names] for name in names: assert name in names_real return names else: return names_real def save_model(self, epoch, rank1, save_dir, is_best=False): names = self.get_model_names() for name in names: save_checkpoint( { 'state_dict': self._models[name].state_dict(), 'epoch': epoch + 1, 'rank1': rank1, 'optimizer': self._optims[name].state_dict(), 'scheduler': self._scheds[name].state_dict() }, osp.join(save_dir, name), is_best=is_best ) def set_model_mode(self, mode='train', names=None): assert mode in ['train', 'eval', 'test'] names = self.get_model_names(names) for name in names: if mode == 'train': self._models[name].train() else: self._models[name].eval() def get_current_lr(self, names=None): names = self.get_model_names(names) name = names[0] return self._optims[name].param_groups[-1]['lr'] def update_lr(self, names=None): names = self.get_model_names(names) for name in names: if self._scheds[name] is not None: self._scheds[name].step() def run( self, save_dir='log', max_epoch=0, start_epoch=0, print_freq=10, fixbase_epoch=0, open_layers=None, start_eval=0, eval_freq=-1, test_only=False, dist_metric='euclidean', normalize_feature=False, visrank=False, visrank_topk=10, use_metric_cuhk03=False, ranks=[1, 5, 10, 20], rerank=False ): r"""A unified pipeline for training and evaluating a model. Args: save_dir (str): directory to save model. max_epoch (int): maximum epoch. start_epoch (int, optional): starting epoch. Default is 0. print_freq (int, optional): print_frequency. Default is 10. fixbase_epoch (int, optional): number of epochs to train ``open_layers`` (new layers) while keeping base layers frozen. Default is 0. ``fixbase_epoch`` is counted in ``max_epoch``. open_layers (str or list, optional): layers (attribute names) open for training. start_eval (int, optional): from which epoch to start evaluation. Default is 0. eval_freq (int, optional): evaluation frequency. Default is -1 (meaning evaluation is only performed at the end of training). test_only (bool, optional): if True, only runs evaluation on test datasets. Default is False. dist_metric (str, optional): distance metric used to compute distance matrix between query and gallery. Default is "euclidean". normalize_feature (bool, optional): performs L2 normalization on feature vectors before computing feature distance. Default is False. visrank (bool, optional): visualizes ranked results. Default is False. It is recommended to enable ``visrank`` when ``test_only`` is True. The ranked images will be saved to "save_dir/visrank_dataset", e.g. "save_dir/visrank_market1501". visrank_topk (int, optional): top-k ranked images to be visualized. Default is 10. use_metric_cuhk03 (bool, optional): use single-gallery-shot setting for cuhk03. Default is False. This should be enabled when using cuhk03 classic split. ranks (list, optional): cmc ranks to be computed. Default is [1, 5, 10, 20]. rerank (bool, optional): uses person re-ranking (by Zhong et al. CVPR'17). Default is False. This is only enabled when test_only=True. """ if visrank and not test_only: raise ValueError( 'visrank can be set to True only if test_only=True' ) if test_only: self.test( dist_metric=dist_metric, normalize_feature=normalize_feature, visrank=visrank, visrank_topk=visrank_topk, save_dir=save_dir, use_metric_cuhk03=use_metric_cuhk03, ranks=ranks, rerank=rerank ) return if self.writer is None: self.writer = SummaryWriter(log_dir=save_dir) time_start = time.time() self.start_epoch = start_epoch self.max_epoch = max_epoch print('=> Start training') for self.epoch in range(self.start_epoch, self.max_epoch): self.train( print_freq=print_freq, fixbase_epoch=fixbase_epoch, open_layers=open_layers ) if (self.epoch + 1) >= start_eval \ and eval_freq > 0 \ and (self.epoch+1) % eval_freq == 0 \ and (self.epoch + 1) != self.max_epoch: rank1 = self.test( dist_metric=dist_metric, normalize_feature=normalize_feature, visrank=visrank, visrank_topk=visrank_topk, save_dir=save_dir, use_metric_cuhk03=use_metric_cuhk03, ranks=ranks ) self.save_model(self.epoch, rank1, save_dir) if self.max_epoch > 0: print('=> Final test') rank1 = self.test( dist_metric=dist_metric, normalize_feature=normalize_feature, visrank=visrank, visrank_topk=visrank_topk, save_dir=save_dir, use_metric_cuhk03=use_metric_cuhk03, ranks=ranks ) self.save_model(self.epoch, rank1, save_dir) elapsed = round(time.time() - time_start) elapsed = str(datetime.timedelta(seconds=elapsed)) print('Elapsed {}'.format(elapsed)) if self.writer is not None: self.writer.close() def train(self, print_freq=10, fixbase_epoch=0, open_layers=None): losses = MetricMeter() batch_time = AverageMeter() data_time = AverageMeter() self.set_model_mode('train') self.two_stepped_transfer_learning( self.epoch, fixbase_epoch, open_layers ) self.num_batches = len(self.train_loader) end = time.time() for self.batch_idx, data in enumerate(self.train_loader): data_time.update(time.time() - end) loss_summary = self.forward_backward(data) batch_time.update(time.time() - end) losses.update(loss_summary) if (self.batch_idx + 1) % print_freq == 0: nb_this_epoch = self.num_batches - (self.batch_idx + 1) nb_future_epochs = ( self.max_epoch - (self.epoch + 1) ) * self.num_batches eta_seconds = batch_time.avg * (nb_this_epoch+nb_future_epochs) eta_str = str(datetime.timedelta(seconds=int(eta_seconds))) print( 'epoch: [{0}/{1}][{2}/{3}]\t' 'time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'eta {eta}\t' '{losses}\t' 'lr {lr:.6f}'.format( self.epoch + 1, self.max_epoch, self.batch_idx + 1, self.num_batches, batch_time=batch_time, data_time=data_time, eta=eta_str, losses=losses, lr=self.get_current_lr() ) ) if self.writer is not None: n_iter = self.epoch * self.num_batches + self.batch_idx self.writer.add_scalar('Train/time', batch_time.avg, n_iter) self.writer.add_scalar('Train/data', data_time.avg, n_iter) for name, meter in losses.meters.items(): self.writer.add_scalar('Train/' + name, meter.avg, n_iter) self.writer.add_scalar( 'Train/lr', self.get_current_lr(), n_iter ) end = time.time() self.update_lr() def forward_backward(self, data): raise NotImplementedError def test( self, dist_metric='euclidean', normalize_feature=False, visrank=False, visrank_topk=10, save_dir='', use_metric_cuhk03=False, ranks=[1, 5, 10, 20], rerank=False ): r"""Tests model on target datasets. .. note:: This function has been called in ``run()``. .. note:: The test pipeline implemented in this function suits both image- and video-reid. In general, a subclass of Engine only needs to re-implement ``extract_features()`` and ``parse_data_for_eval()`` (most of the time), but not a must. Please refer to the source code for more details. """ self.set_model_mode('eval') targets = list(self.test_loader.keys()) for name in targets: domain = 'source' if name in self.datamanager.sources else 'target' print('##### Evaluating {} ({}) #####'.format(name, domain)) query_loader = self.test_loader[name]['query'] gallery_loader = self.test_loader[name]['gallery'] rank1, mAP = self._evaluate( dataset_name=name, query_loader=query_loader, gallery_loader=gallery_loader, dist_metric=dist_metric, normalize_feature=normalize_feature, visrank=visrank, visrank_topk=visrank_topk, save_dir=save_dir, use_metric_cuhk03=use_metric_cuhk03, ranks=ranks, rerank=rerank ) if self.writer is not None: self.writer.add_scalar(f'Test/{name}/rank1', rank1, self.epoch) self.writer.add_scalar(f'Test/{name}/mAP', mAP, self.epoch) return rank1 @torch.no_grad() def _evaluate( self, dataset_name='', query_loader=None, gallery_loader=None, dist_metric='euclidean', normalize_feature=False, visrank=False, visrank_topk=10, save_dir='', use_metric_cuhk03=False, ranks=[1, 5, 10, 20], rerank=False ): batch_time = AverageMeter() def _feature_extraction(data_loader): f_, pids_, camids_ = [], [], [] for batch_idx, data in enumerate(data_loader): imgs, pids, camids = self.parse_data_for_eval(data) if self.use_gpu: imgs = imgs.cuda() end = time.time() features = self.extract_features(imgs) batch_time.update(time.time() - end) features = features.cpu() f_.append(features) pids_.extend(pids.tolist()) camids_.extend(camids.tolist()) f_ = torch.cat(f_, 0) pids_ = np.asarray(pids_) camids_ = np.asarray(camids_) return f_, pids_, camids_ print('Extracting features from query set ...') qf, q_pids, q_camids = _feature_extraction(query_loader) print('Done, obtained {}-by-{} matrix'.format(qf.size(0), qf.size(1))) print('Extracting features from gallery set ...') gf, g_pids, g_camids = _feature_extraction(gallery_loader) print('Done, obtained {}-by-{} matrix'.format(gf.size(0), gf.size(1))) print('Speed: {:.4f} sec/batch'.format(batch_time.avg)) if normalize_feature: print('Normalzing features with L2 norm ...') qf = F.normalize(qf, p=2, dim=1) gf = F.normalize(gf, p=2, dim=1) print( 'Computing distance matrix with metric={} ...'.format(dist_metric) ) distmat = metrics.compute_distance_matrix(qf, gf, dist_metric) distmat = distmat.numpy() if rerank: print('Applying person re-ranking ...') distmat_qq = metrics.compute_distance_matrix(qf, qf, dist_metric) distmat_gg = metrics.compute_distance_matrix(gf, gf, dist_metric) distmat = re_ranking(distmat, distmat_qq, distmat_gg) print('Computing CMC and mAP ...') cmc, mAP = metrics.evaluate_rank( distmat, q_pids, g_pids, q_camids, g_camids, use_metric_cuhk03=use_metric_cuhk03 ) print('** Results **') print('mAP: {:.1%}'.format(mAP)) print('CMC curve') for r in ranks: print('Rank-{:<3}: {:.1%}'.format(r, cmc[r - 1])) if visrank: visualize_ranked_results( distmat, self.datamanager.fetch_test_loaders(dataset_name), self.datamanager.data_type, width=self.datamanager.width, height=self.datamanager.height, save_dir=osp.join(save_dir, 'visrank_' + dataset_name), topk=visrank_topk ) return cmc[0], mAP def compute_loss(self, criterion, outputs, targets): if isinstance(outputs, (tuple, list)): loss = DeepSupervision(criterion, outputs, targets) else: loss = criterion(outputs, targets) return loss def extract_features(self, input): return self.model(input) def parse_data_for_train(self, data): imgs = data['img'] pids = data['pid'] return imgs, pids def parse_data_for_eval(self, data): imgs = data['img'] pids = data['pid'] camids = data['camid'] return imgs, pids, camids def two_stepped_transfer_learning( self, epoch, fixbase_epoch, open_layers, model=None ): """Two-stepped transfer learning. The idea is to freeze base layers for a certain number of epochs and then open all layers for training. Reference: https://arxiv.org/abs/1611.05244 """ model = self.model if model is None else model if model is None: return if (epoch + 1) <= fixbase_epoch and open_layers is not None: print( '* Only train {} (epoch: {}/{})'.format( open_layers, epoch + 1, fixbase_epoch ) ) open_specified_layers(model, open_layers) else: open_all_layers(model) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/engine/image/__init__.py ================================================ from __future__ import absolute_import from .softmax import ImageSoftmaxEngine from .triplet import ImageTripletEngine ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/engine/image/softmax.py ================================================ from __future__ import division, print_function, absolute_import from torchreid import metrics from torchreid.losses import CrossEntropyLoss from ..engine import Engine class ImageSoftmaxEngine(Engine): r"""Softmax-loss engine for image-reid. Args: datamanager (DataManager): an instance of ``torchreid.data.ImageDataManager`` or ``torchreid.data.VideoDataManager``. model (nn.Module): model instance. optimizer (Optimizer): an Optimizer. scheduler (LRScheduler, optional): if None, no learning rate decay will be performed. use_gpu (bool, optional): use gpu. Default is True. label_smooth (bool, optional): use label smoothing regularizer. Default is True. Examples:: import torchreid datamanager = torchreid.data.ImageDataManager( root='path/to/reid-data', sources='market1501', height=256, width=128, combineall=False, batch_size=32 ) model = torchreid.models.build_model( name='resnet50', num_classes=datamanager.num_train_pids, loss='softmax' ) model = model.cuda() optimizer = torchreid.optim.build_optimizer( model, optim='adam', lr=0.0003 ) scheduler = torchreid.optim.build_lr_scheduler( optimizer, lr_scheduler='single_step', stepsize=20 ) engine = torchreid.engine.ImageSoftmaxEngine( datamanager, model, optimizer, scheduler=scheduler ) engine.run( max_epoch=60, save_dir='log/resnet50-softmax-market1501', print_freq=10 ) """ def __init__( self, datamanager, model, optimizer, scheduler=None, use_gpu=True, label_smooth=True ): super(ImageSoftmaxEngine, self).__init__(datamanager, use_gpu) self.model = model self.optimizer = optimizer self.scheduler = scheduler self.register_model('model', model, optimizer, scheduler) self.criterion = CrossEntropyLoss( num_classes=self.datamanager.num_train_pids, use_gpu=self.use_gpu, label_smooth=label_smooth ) def forward_backward(self, data): imgs, pids = self.parse_data_for_train(data) if self.use_gpu: imgs = imgs.cuda() pids = pids.cuda() outputs = self.model(imgs) loss = self.compute_loss(self.criterion, outputs, pids) self.optimizer.zero_grad() loss.backward() self.optimizer.step() loss_summary = { 'loss': loss.item(), 'acc': metrics.accuracy(outputs, pids)[0].item() } return loss_summary ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/engine/image/triplet.py ================================================ from __future__ import division, print_function, absolute_import from torchreid import metrics from torchreid.losses import TripletLoss, CrossEntropyLoss from ..engine import Engine class ImageTripletEngine(Engine): r"""Triplet-loss engine for image-reid. Args: datamanager (DataManager): an instance of ``torchreid.data.ImageDataManager`` or ``torchreid.data.VideoDataManager``. model (nn.Module): model instance. optimizer (Optimizer): an Optimizer. margin (float, optional): margin for triplet loss. Default is 0.3. weight_t (float, optional): weight for triplet loss. Default is 1. weight_x (float, optional): weight for softmax loss. Default is 1. scheduler (LRScheduler, optional): if None, no learning rate decay will be performed. use_gpu (bool, optional): use gpu. Default is True. label_smooth (bool, optional): use label smoothing regularizer. Default is True. Examples:: import torchreid datamanager = torchreid.data.ImageDataManager( root='path/to/reid-data', sources='market1501', height=256, width=128, combineall=False, batch_size=32, num_instances=4, train_sampler='RandomIdentitySampler' # this is important ) model = torchreid.models.build_model( name='resnet50', num_classes=datamanager.num_train_pids, loss='triplet' ) model = model.cuda() optimizer = torchreid.optim.build_optimizer( model, optim='adam', lr=0.0003 ) scheduler = torchreid.optim.build_lr_scheduler( optimizer, lr_scheduler='single_step', stepsize=20 ) engine = torchreid.engine.ImageTripletEngine( datamanager, model, optimizer, margin=0.3, weight_t=0.7, weight_x=1, scheduler=scheduler ) engine.run( max_epoch=60, save_dir='log/resnet50-triplet-market1501', print_freq=10 ) """ def __init__( self, datamanager, model, optimizer, margin=0.3, weight_t=1, weight_x=1, scheduler=None, use_gpu=True, label_smooth=True ): super(ImageTripletEngine, self).__init__(datamanager, use_gpu) self.model = model self.optimizer = optimizer self.scheduler = scheduler self.register_model('model', model, optimizer, scheduler) assert weight_t >= 0 and weight_x >= 0 assert weight_t + weight_x > 0 self.weight_t = weight_t self.weight_x = weight_x self.criterion_t = TripletLoss(margin=margin) self.criterion_x = CrossEntropyLoss( num_classes=self.datamanager.num_train_pids, use_gpu=self.use_gpu, label_smooth=label_smooth ) def forward_backward(self, data): imgs, pids = self.parse_data_for_train(data) if self.use_gpu: imgs = imgs.cuda() pids = pids.cuda() outputs, features = self.model(imgs) loss = 0 loss_summary = {} if self.weight_t > 0: loss_t = self.compute_loss(self.criterion_t, features, pids) loss += self.weight_t * loss_t loss_summary['loss_t'] = loss_t.item() if self.weight_x > 0: loss_x = self.compute_loss(self.criterion_x, outputs, pids) loss += self.weight_x * loss_x loss_summary['loss_x'] = loss_x.item() loss_summary['acc'] = metrics.accuracy(outputs, pids)[0].item() assert loss_summary self.optimizer.zero_grad() loss.backward() self.optimizer.step() return loss_summary ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/engine/video/__init__.py ================================================ from __future__ import absolute_import from .softmax import VideoSoftmaxEngine from .triplet import VideoTripletEngine ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/engine/video/softmax.py ================================================ from __future__ import division, print_function, absolute_import import torch from torchreid.engine.image import ImageSoftmaxEngine class VideoSoftmaxEngine(ImageSoftmaxEngine): """Softmax-loss engine for video-reid. Args: datamanager (DataManager): an instance of ``torchreid.data.ImageDataManager`` or ``torchreid.data.VideoDataManager``. model (nn.Module): model instance. optimizer (Optimizer): an Optimizer. scheduler (LRScheduler, optional): if None, no learning rate decay will be performed. use_gpu (bool, optional): use gpu. Default is True. label_smooth (bool, optional): use label smoothing regularizer. Default is True. pooling_method (str, optional): how to pool features for a tracklet. Default is "avg" (average). Choices are ["avg", "max"]. Examples:: import torch import torchreid # Each batch contains batch_size*seq_len images datamanager = torchreid.data.VideoDataManager( root='path/to/reid-data', sources='mars', height=256, width=128, combineall=False, batch_size=8, # number of tracklets seq_len=15 # number of images in each tracklet ) model = torchreid.models.build_model( name='resnet50', num_classes=datamanager.num_train_pids, loss='softmax' ) model = model.cuda() optimizer = torchreid.optim.build_optimizer( model, optim='adam', lr=0.0003 ) scheduler = torchreid.optim.build_lr_scheduler( optimizer, lr_scheduler='single_step', stepsize=20 ) engine = torchreid.engine.VideoSoftmaxEngine( datamanager, model, optimizer, scheduler=scheduler, pooling_method='avg' ) engine.run( max_epoch=60, save_dir='log/resnet50-softmax-mars', print_freq=10 ) """ def __init__( self, datamanager, model, optimizer, scheduler=None, use_gpu=True, label_smooth=True, pooling_method='avg' ): super(VideoSoftmaxEngine, self).__init__( datamanager, model, optimizer, scheduler=scheduler, use_gpu=use_gpu, label_smooth=label_smooth ) self.pooling_method = pooling_method def parse_data_for_train(self, data): imgs = data['img'] pids = data['pid'] if imgs.dim() == 5: # b: batch size # s: sqeuence length # c: channel depth # h: height # w: width b, s, c, h, w = imgs.size() imgs = imgs.view(b * s, c, h, w) pids = pids.view(b, 1).expand(b, s) pids = pids.contiguous().view(b * s) return imgs, pids def extract_features(self, input): # b: batch size # s: sqeuence length # c: channel depth # h: height # w: width b, s, c, h, w = input.size() input = input.view(b * s, c, h, w) features = self.model(input) features = features.view(b, s, -1) if self.pooling_method == 'avg': features = torch.mean(features, 1) else: features = torch.max(features, 1)[0] return features ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/engine/video/triplet.py ================================================ from __future__ import division, print_function, absolute_import import torch from torchreid.engine.image import ImageTripletEngine class VideoTripletEngine(ImageTripletEngine): """Triplet-loss engine for video-reid. Args: datamanager (DataManager): an instance of ``torchreid.data.ImageDataManager`` or ``torchreid.data.VideoDataManager``. model (nn.Module): model instance. optimizer (Optimizer): an Optimizer. margin (float, optional): margin for triplet loss. Default is 0.3. weight_t (float, optional): weight for triplet loss. Default is 1. weight_x (float, optional): weight for softmax loss. Default is 1. scheduler (LRScheduler, optional): if None, no learning rate decay will be performed. use_gpu (bool, optional): use gpu. Default is True. label_smooth (bool, optional): use label smoothing regularizer. Default is True. pooling_method (str, optional): how to pool features for a tracklet. Default is "avg" (average). Choices are ["avg", "max"]. Examples:: import torch import torchreid # Each batch contains batch_size*seq_len images # Each identity is sampled with num_instances tracklets datamanager = torchreid.data.VideoDataManager( root='path/to/reid-data', sources='mars', height=256, width=128, combineall=False, num_instances=4, train_sampler='RandomIdentitySampler' batch_size=8, # number of tracklets seq_len=15 # number of images in each tracklet ) model = torchreid.models.build_model( name='resnet50', num_classes=datamanager.num_train_pids, loss='triplet' ) model = model.cuda() optimizer = torchreid.optim.build_optimizer( model, optim='adam', lr=0.0003 ) scheduler = torchreid.optim.build_lr_scheduler( optimizer, lr_scheduler='single_step', stepsize=20 ) engine = torchreid.engine.VideoTripletEngine( datamanager, model, optimizer, margin=0.3, weight_t=0.7, weight_x=1, scheduler=scheduler, pooling_method='avg' ) engine.run( max_epoch=60, save_dir='log/resnet50-triplet-mars', print_freq=10 ) """ def __init__( self, datamanager, model, optimizer, margin=0.3, weight_t=1, weight_x=1, scheduler=None, use_gpu=True, label_smooth=True, pooling_method='avg' ): super(VideoTripletEngine, self).__init__( datamanager, model, optimizer, margin=margin, weight_t=weight_t, weight_x=weight_x, scheduler=scheduler, use_gpu=use_gpu, label_smooth=label_smooth ) self.pooling_method = pooling_method def parse_data_for_train(self, data): imgs = data['img'] pids = data['pid'] if imgs.dim() == 5: # b: batch size # s: sqeuence length # c: channel depth # h: height # w: width b, s, c, h, w = imgs.size() imgs = imgs.view(b * s, c, h, w) pids = pids.view(b, 1).expand(b, s) pids = pids.contiguous().view(b * s) return imgs, pids def extract_features(self, input): # b: batch size # s: sqeuence length # c: channel depth # h: height # w: width b, s, c, h, w = input.size() input = input.view(b * s, c, h, w) features = self.model(input) features = features.view(b, s, -1) if self.pooling_method == 'avg': features = torch.mean(features, 1) else: features = torch.max(features, 1)[0] return features ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/losses/__init__.py ================================================ from __future__ import division, print_function, absolute_import from .cross_entropy_loss import CrossEntropyLoss from .hard_mine_triplet_loss import TripletLoss def DeepSupervision(criterion, xs, y): """DeepSupervision Applies criterion to each element in a list. Args: criterion: loss function xs: tuple of inputs y: ground truth """ loss = 0. for x in xs: loss += criterion(x, y) loss /= len(xs) return loss ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/losses/cross_entropy_loss.py ================================================ from __future__ import division, absolute_import import torch import torch.nn as nn class CrossEntropyLoss(nn.Module): r"""Cross entropy loss with label smoothing regularizer. Reference: Szegedy et al. Rethinking the Inception Architecture for Computer Vision. CVPR 2016. With label smoothing, the label :math:`y` for a class is computed by .. math:: \begin{equation} (1 - \eps) \times y + \frac{\eps}{K}, \end{equation} where :math:`K` denotes the number of classes and :math:`\eps` is a weight. When :math:`\eps = 0`, the loss function reduces to the normal cross entropy. Args: num_classes (int): number of classes. eps (float, optional): weight. Default is 0.1. use_gpu (bool, optional): whether to use gpu devices. Default is True. label_smooth (bool, optional): whether to apply label smoothing. Default is True. """ def __init__(self, num_classes, eps=0.1, use_gpu=True, label_smooth=True): super(CrossEntropyLoss, self).__init__() self.num_classes = num_classes self.eps = eps if label_smooth else 0 self.use_gpu = use_gpu self.logsoftmax = nn.LogSoftmax(dim=1) def forward(self, inputs, targets): """ Args: inputs (torch.Tensor): prediction matrix (before softmax) with shape (batch_size, num_classes). targets (torch.LongTensor): ground truth labels with shape (batch_size). Each position contains the label index. """ log_probs = self.logsoftmax(inputs) zeros = torch.zeros(log_probs.size()) targets = zeros.scatter_(1, targets.unsqueeze(1).data.cpu(), 1) if self.use_gpu: targets = targets.cuda() targets = (1 - self.eps) * targets + self.eps / self.num_classes return (-targets * log_probs).mean(0).sum() ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/losses/hard_mine_triplet_loss.py ================================================ from __future__ import division, absolute_import import torch import torch.nn as nn class TripletLoss(nn.Module): """Triplet loss with hard positive/negative mining. Reference: Hermans et al. In Defense of the Triplet Loss for Person Re-Identification. arXiv:1703.07737. Imported from ``_. Args: margin (float, optional): margin for triplet. Default is 0.3. """ def __init__(self, margin=0.3): super(TripletLoss, self).__init__() self.margin = margin self.ranking_loss = nn.MarginRankingLoss(margin=margin) def forward(self, inputs, targets): """ Args: inputs (torch.Tensor): feature matrix with shape (batch_size, feat_dim). targets (torch.LongTensor): ground truth labels with shape (num_classes). """ n = inputs.size(0) # Compute pairwise distance, replace by the official when merged dist = torch.pow(inputs, 2).sum(dim=1, keepdim=True).expand(n, n) dist = dist + dist.t() dist.addmm_(inputs, inputs.t(), beta=1, alpha=-2) dist = dist.clamp(min=1e-12).sqrt() # for numerical stability # For each anchor, find the hardest positive and negative mask = targets.expand(n, n).eq(targets.expand(n, n).t()) dist_ap, dist_an = [], [] for i in range(n): dist_ap.append(dist[i][mask[i]].max().unsqueeze(0)) dist_an.append(dist[i][mask[i] == 0].min().unsqueeze(0)) dist_ap = torch.cat(dist_ap) dist_an = torch.cat(dist_an) # Compute ranking hinge loss y = torch.ones_like(dist_an) return self.ranking_loss(dist_an, dist_ap, y) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/metrics/__init__.py ================================================ from __future__ import absolute_import from .rank import evaluate_rank from .accuracy import accuracy from .distance import compute_distance_matrix ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/metrics/accuracy.py ================================================ from __future__ import division, print_function, absolute_import def accuracy(output, target, topk=(1, )): """Computes the accuracy over the k top predictions for the specified values of k. Args: output (torch.Tensor): prediction matrix with shape (batch_size, num_classes). target (torch.LongTensor): ground truth labels with shape (batch_size). topk (tuple, optional): accuracy at top-k will be computed. For example, topk=(1, 5) means accuracy at top-1 and top-5 will be computed. Returns: list: accuracy at top-k. Examples:: >>> from torchreid import metrics >>> metrics.accuracy(output, target) """ maxk = max(topk) batch_size = target.size(0) if isinstance(output, (tuple, list)): output = output[0] _, pred = output.topk(maxk, 1, True, True) pred = pred.t() correct = pred.eq(target.view(1, -1).expand_as(pred)) res = [] for k in topk: correct_k = correct[:k].view(-1).float().sum(0, keepdim=True) acc = correct_k.mul_(100.0 / batch_size) res.append(acc) return res ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/metrics/distance.py ================================================ from __future__ import division, print_function, absolute_import import torch from torch.nn import functional as F def compute_distance_matrix(input1, input2, metric='euclidean'): """A wrapper function for computing distance matrix. Args: input1 (torch.Tensor): 2-D feature matrix. input2 (torch.Tensor): 2-D feature matrix. metric (str, optional): "euclidean" or "cosine". Default is "euclidean". Returns: torch.Tensor: distance matrix. Examples:: >>> from torchreid import metrics >>> input1 = torch.rand(10, 2048) >>> input2 = torch.rand(100, 2048) >>> distmat = metrics.compute_distance_matrix(input1, input2) >>> distmat.size() # (10, 100) """ # check input assert isinstance(input1, torch.Tensor) assert isinstance(input2, torch.Tensor) assert input1.dim() == 2, 'Expected 2-D tensor, but got {}-D'.format( input1.dim() ) assert input2.dim() == 2, 'Expected 2-D tensor, but got {}-D'.format( input2.dim() ) assert input1.size(1) == input2.size(1) if metric == 'euclidean': distmat = euclidean_squared_distance(input1, input2) elif metric == 'cosine': distmat = cosine_distance(input1, input2) else: raise ValueError( 'Unknown distance metric: {}. ' 'Please choose either "euclidean" or "cosine"'.format(metric) ) return distmat def euclidean_squared_distance(input1, input2): """Computes euclidean squared distance. Args: input1 (torch.Tensor): 2-D feature matrix. input2 (torch.Tensor): 2-D feature matrix. Returns: torch.Tensor: distance matrix. """ m, n = input1.size(0), input2.size(0) mat1 = torch.pow(input1, 2).sum(dim=1, keepdim=True).expand(m, n) mat2 = torch.pow(input2, 2).sum(dim=1, keepdim=True).expand(n, m).t() distmat = mat1 + mat2 distmat.addmm_(input1, input2.t(), beta=1, alpha=-2) return distmat def cosine_distance(input1, input2): """Computes cosine distance. Args: input1 (torch.Tensor): 2-D feature matrix. input2 (torch.Tensor): 2-D feature matrix. Returns: torch.Tensor: distance matrix. """ input1_normed = F.normalize(input1, p=2, dim=1) input2_normed = F.normalize(input2, p=2, dim=1) distmat = 1 - torch.mm(input1_normed, input2_normed.t()) return distmat ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/metrics/rank.py ================================================ from __future__ import division, print_function, absolute_import import numpy as np import warnings from collections import defaultdict try: from torchreid.metrics.rank_cylib.rank_cy import evaluate_cy IS_CYTHON_AVAI = True except ImportError: IS_CYTHON_AVAI = False warnings.warn( 'Cython evaluation (very fast so highly recommended) is ' 'unavailable, now use python evaluation.' ) def eval_cuhk03(distmat, q_pids, g_pids, q_camids, g_camids, max_rank): """Evaluation with cuhk03 metric Key: one image for each gallery identity is randomly sampled for each query identity. Random sampling is performed num_repeats times. """ num_repeats = 10 num_q, num_g = distmat.shape if num_g < max_rank: max_rank = num_g print( 'Note: number of gallery samples is quite small, got {}'. format(num_g) ) indices = np.argsort(distmat, axis=1) matches = (g_pids[indices] == q_pids[:, np.newaxis]).astype(np.int32) # compute cmc curve for each query all_cmc = [] all_AP = [] num_valid_q = 0. # number of valid query for q_idx in range(num_q): # get query pid and camid q_pid = q_pids[q_idx] q_camid = q_camids[q_idx] # remove gallery samples that have the same pid and camid with query order = indices[q_idx] remove = (g_pids[order] == q_pid) & (g_camids[order] == q_camid) keep = np.invert(remove) # compute cmc curve raw_cmc = matches[q_idx][ keep] # binary vector, positions with value 1 are correct matches if not np.any(raw_cmc): # this condition is true when query identity does not appear in gallery continue kept_g_pids = g_pids[order][keep] g_pids_dict = defaultdict(list) for idx, pid in enumerate(kept_g_pids): g_pids_dict[pid].append(idx) cmc = 0. for repeat_idx in range(num_repeats): mask = np.zeros(len(raw_cmc), dtype=np.bool) for _, idxs in g_pids_dict.items(): # randomly sample one image for each gallery person rnd_idx = np.random.choice(idxs) mask[rnd_idx] = True masked_raw_cmc = raw_cmc[mask] _cmc = masked_raw_cmc.cumsum() _cmc[_cmc > 1] = 1 cmc += _cmc[:max_rank].astype(np.float32) cmc /= num_repeats all_cmc.append(cmc) # compute AP num_rel = raw_cmc.sum() tmp_cmc = raw_cmc.cumsum() tmp_cmc = [x / (i+1.) for i, x in enumerate(tmp_cmc)] tmp_cmc = np.asarray(tmp_cmc) * raw_cmc AP = tmp_cmc.sum() / num_rel all_AP.append(AP) num_valid_q += 1. assert num_valid_q > 0, 'Error: all query identities do not appear in gallery' all_cmc = np.asarray(all_cmc).astype(np.float32) all_cmc = all_cmc.sum(0) / num_valid_q mAP = np.mean(all_AP) return all_cmc, mAP def eval_market1501(distmat, q_pids, g_pids, q_camids, g_camids, max_rank): """Evaluation with market1501 metric Key: for each query identity, its gallery images from the same camera view are discarded. """ num_q, num_g = distmat.shape if num_g < max_rank: max_rank = num_g print( 'Note: number of gallery samples is quite small, got {}'. format(num_g) ) indices = np.argsort(distmat, axis=1) matches = (g_pids[indices] == q_pids[:, np.newaxis]).astype(np.int32) # compute cmc curve for each query all_cmc = [] all_AP = [] num_valid_q = 0. # number of valid query for q_idx in range(num_q): # get query pid and camid q_pid = q_pids[q_idx] q_camid = q_camids[q_idx] # remove gallery samples that have the same pid and camid with query order = indices[q_idx] remove = (g_pids[order] == q_pid) & (g_camids[order] == q_camid) keep = np.invert(remove) # compute cmc curve raw_cmc = matches[q_idx][ keep] # binary vector, positions with value 1 are correct matches if not np.any(raw_cmc): # this condition is true when query identity does not appear in gallery continue cmc = raw_cmc.cumsum() cmc[cmc > 1] = 1 all_cmc.append(cmc[:max_rank]) num_valid_q += 1. # compute average precision # reference: https://en.wikipedia.org/wiki/Evaluation_measures_(information_retrieval)#Average_precision num_rel = raw_cmc.sum() tmp_cmc = raw_cmc.cumsum() tmp_cmc = [x / (i+1.) for i, x in enumerate(tmp_cmc)] tmp_cmc = np.asarray(tmp_cmc) * raw_cmc AP = tmp_cmc.sum() / num_rel all_AP.append(AP) assert num_valid_q > 0, 'Error: all query identities do not appear in gallery' all_cmc = np.asarray(all_cmc).astype(np.float32) all_cmc = all_cmc.sum(0) / num_valid_q mAP = np.mean(all_AP) return all_cmc, mAP def evaluate_py( distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_metric_cuhk03 ): if use_metric_cuhk03: return eval_cuhk03( distmat, q_pids, g_pids, q_camids, g_camids, max_rank ) else: return eval_market1501( distmat, q_pids, g_pids, q_camids, g_camids, max_rank ) def evaluate_rank( distmat, q_pids, g_pids, q_camids, g_camids, max_rank=50, use_metric_cuhk03=False, use_cython=True ): """Evaluates CMC rank. Args: distmat (numpy.ndarray): distance matrix of shape (num_query, num_gallery). q_pids (numpy.ndarray): 1-D array containing person identities of each query instance. g_pids (numpy.ndarray): 1-D array containing person identities of each gallery instance. q_camids (numpy.ndarray): 1-D array containing camera views under which each query instance is captured. g_camids (numpy.ndarray): 1-D array containing camera views under which each gallery instance is captured. max_rank (int, optional): maximum CMC rank to be computed. Default is 50. use_metric_cuhk03 (bool, optional): use single-gallery-shot setting for cuhk03. Default is False. This should be enabled when using cuhk03 classic split. use_cython (bool, optional): use cython code for evaluation. Default is True. This is highly recommended as the cython code can speed up the cmc computation by more than 10x. This requires Cython to be installed. """ if use_cython and IS_CYTHON_AVAI: return evaluate_cy( distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_metric_cuhk03 ) else: return evaluate_py( distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_metric_cuhk03 ) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/metrics/rank_cylib/Makefile ================================================ all: $(PYTHON) setup.py build_ext --inplace rm -rf build clean: rm -rf build rm -f rank_cy.c *.so ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/metrics/rank_cylib/__init__.py ================================================ ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/metrics/rank_cylib/rank_cy.pyx ================================================ # cython: boundscheck=False, wraparound=False, nonecheck=False, cdivision=True from __future__ import print_function import numpy as np from libc.stdint cimport int64_t, uint64_t import cython cimport numpy as np import random from collections import defaultdict """ Compiler directives: https://github.com/cython/cython/wiki/enhancements-compilerdirectives Cython tutorial: https://cython.readthedocs.io/en/latest/src/userguide/numpy_tutorial.html Credit to https://github.com/luzai """ # Main interface cpdef evaluate_cy(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_metric_cuhk03=False): distmat = np.asarray(distmat, dtype=np.float32) q_pids = np.asarray(q_pids, dtype=np.int64) g_pids = np.asarray(g_pids, dtype=np.int64) q_camids = np.asarray(q_camids, dtype=np.int64) g_camids = np.asarray(g_camids, dtype=np.int64) if use_metric_cuhk03: return eval_cuhk03_cy(distmat, q_pids, g_pids, q_camids, g_camids, max_rank) return eval_market1501_cy(distmat, q_pids, g_pids, q_camids, g_camids, max_rank) cpdef eval_cuhk03_cy(float[:,:] distmat, int64_t[:] q_pids, int64_t[:]g_pids, int64_t[:]q_camids, int64_t[:]g_camids, int64_t max_rank): cdef int64_t num_q = distmat.shape[0] cdef int64_t num_g = distmat.shape[1] if num_g < max_rank: max_rank = num_g print('Note: number of gallery samples is quite small, got {}'.format(num_g)) cdef: int64_t num_repeats = 10 int64_t[:,:] indices = np.argsort(distmat, axis=1) int64_t[:,:] matches = (np.asarray(g_pids)[np.asarray(indices)] == np.asarray(q_pids)[:, np.newaxis]).astype(np.int64) float[:,:] all_cmc = np.zeros((num_q, max_rank), dtype=np.float32) float[:] all_AP = np.zeros(num_q, dtype=np.float32) float num_valid_q = 0. # number of valid query int64_t q_idx, q_pid, q_camid, g_idx int64_t[:] order = np.zeros(num_g, dtype=np.int64) int64_t keep float[:] raw_cmc = np.zeros(num_g, dtype=np.float32) # binary vector, positions with value 1 are correct matches float[:] masked_raw_cmc = np.zeros(num_g, dtype=np.float32) float[:] cmc, masked_cmc int64_t num_g_real, num_g_real_masked, rank_idx, rnd_idx uint64_t meet_condition float AP int64_t[:] kept_g_pids, mask float num_rel float[:] tmp_cmc = np.zeros(num_g, dtype=np.float32) float tmp_cmc_sum for q_idx in range(num_q): # get query pid and camid q_pid = q_pids[q_idx] q_camid = q_camids[q_idx] # remove gallery samples that have the same pid and camid with query for g_idx in range(num_g): order[g_idx] = indices[q_idx, g_idx] num_g_real = 0 meet_condition = 0 kept_g_pids = np.zeros(num_g, dtype=np.int64) for g_idx in range(num_g): if (g_pids[order[g_idx]] != q_pid) or (g_camids[order[g_idx]] != q_camid): raw_cmc[num_g_real] = matches[q_idx][g_idx] kept_g_pids[num_g_real] = g_pids[order[g_idx]] num_g_real += 1 if matches[q_idx][g_idx] > 1e-31: meet_condition = 1 if not meet_condition: # this condition is true when query identity does not appear in gallery continue # cuhk03-specific setting g_pids_dict = defaultdict(list) # overhead! for g_idx in range(num_g_real): g_pids_dict[kept_g_pids[g_idx]].append(g_idx) cmc = np.zeros(max_rank, dtype=np.float32) for _ in range(num_repeats): mask = np.zeros(num_g_real, dtype=np.int64) for _, idxs in g_pids_dict.items(): # randomly sample one image for each gallery person rnd_idx = np.random.choice(idxs) #rnd_idx = idxs[0] # use deterministic for debugging mask[rnd_idx] = 1 num_g_real_masked = 0 for g_idx in range(num_g_real): if mask[g_idx] == 1: masked_raw_cmc[num_g_real_masked] = raw_cmc[g_idx] num_g_real_masked += 1 masked_cmc = np.zeros(num_g, dtype=np.float32) function_cumsum(masked_raw_cmc, masked_cmc, num_g_real_masked) for g_idx in range(num_g_real_masked): if masked_cmc[g_idx] > 1: masked_cmc[g_idx] = 1 for rank_idx in range(max_rank): cmc[rank_idx] += masked_cmc[rank_idx] / num_repeats for rank_idx in range(max_rank): all_cmc[q_idx, rank_idx] = cmc[rank_idx] # compute average precision # reference: https://en.wikipedia.org/wiki/Evaluation_measures_(information_retrieval)#Average_precision function_cumsum(raw_cmc, tmp_cmc, num_g_real) num_rel = 0 tmp_cmc_sum = 0 for g_idx in range(num_g_real): tmp_cmc_sum += (tmp_cmc[g_idx] / (g_idx + 1.)) * raw_cmc[g_idx] num_rel += raw_cmc[g_idx] all_AP[q_idx] = tmp_cmc_sum / num_rel num_valid_q += 1. assert num_valid_q > 0, 'Error: all query identities do not appear in gallery' # compute averaged cmc cdef float[:] avg_cmc = np.zeros(max_rank, dtype=np.float32) for rank_idx in range(max_rank): for q_idx in range(num_q): avg_cmc[rank_idx] += all_cmc[q_idx, rank_idx] avg_cmc[rank_idx] /= num_valid_q cdef float mAP = 0 for q_idx in range(num_q): mAP += all_AP[q_idx] mAP /= num_valid_q return np.asarray(avg_cmc).astype(np.float32), mAP cpdef eval_market1501_cy(float[:,:] distmat, int64_t[:] q_pids, int64_t[:]g_pids, int64_t[:]q_camids, int64_t[:]g_camids, int64_t max_rank): cdef int64_t num_q = distmat.shape[0] cdef int64_t num_g = distmat.shape[1] if num_g < max_rank: max_rank = num_g print('Note: number of gallery samples is quite small, got {}'.format(num_g)) cdef: int64_t[:,:] indices = np.argsort(distmat, axis=1) int64_t[:,:] matches = (np.asarray(g_pids)[np.asarray(indices)] == np.asarray(q_pids)[:, np.newaxis]).astype(np.int64) float[:,:] all_cmc = np.zeros((num_q, max_rank), dtype=np.float32) float[:] all_AP = np.zeros(num_q, dtype=np.float32) float num_valid_q = 0. # number of valid query int64_t q_idx, q_pid, q_camid, g_idx int64_t[:] order = np.zeros(num_g, dtype=np.int64) int64_t keep float[:] raw_cmc = np.zeros(num_g, dtype=np.float32) # binary vector, positions with value 1 are correct matches float[:] cmc = np.zeros(num_g, dtype=np.float32) int64_t num_g_real, rank_idx uint64_t meet_condition float num_rel float[:] tmp_cmc = np.zeros(num_g, dtype=np.float32) float tmp_cmc_sum for q_idx in range(num_q): # get query pid and camid q_pid = q_pids[q_idx] q_camid = q_camids[q_idx] # remove gallery samples that have the same pid and camid with query for g_idx in range(num_g): order[g_idx] = indices[q_idx, g_idx] num_g_real = 0 meet_condition = 0 for g_idx in range(num_g): if (g_pids[order[g_idx]] != q_pid) or (g_camids[order[g_idx]] != q_camid): raw_cmc[num_g_real] = matches[q_idx][g_idx] num_g_real += 1 if matches[q_idx][g_idx] > 1e-31: meet_condition = 1 if not meet_condition: # this condition is true when query identity does not appear in gallery continue # compute cmc function_cumsum(raw_cmc, cmc, num_g_real) for g_idx in range(num_g_real): if cmc[g_idx] > 1: cmc[g_idx] = 1 for rank_idx in range(max_rank): all_cmc[q_idx, rank_idx] = cmc[rank_idx] num_valid_q += 1. # compute average precision # reference: https://en.wikipedia.org/wiki/Evaluation_measures_(information_retrieval)#Average_precision function_cumsum(raw_cmc, tmp_cmc, num_g_real) num_rel = 0 tmp_cmc_sum = 0 for g_idx in range(num_g_real): tmp_cmc_sum += (tmp_cmc[g_idx] / (g_idx + 1.)) * raw_cmc[g_idx] num_rel += raw_cmc[g_idx] all_AP[q_idx] = tmp_cmc_sum / num_rel assert num_valid_q > 0, 'Error: all query identities do not appear in gallery' # compute averaged cmc cdef float[:] avg_cmc = np.zeros(max_rank, dtype=np.float32) for rank_idx in range(max_rank): for q_idx in range(num_q): avg_cmc[rank_idx] += all_cmc[q_idx, rank_idx] avg_cmc[rank_idx] /= num_valid_q cdef float mAP = 0 for q_idx in range(num_q): mAP += all_AP[q_idx] mAP /= num_valid_q return np.asarray(avg_cmc).astype(np.float32), mAP # Compute the cumulative sum cdef void function_cumsum(cython.numeric[:] src, cython.numeric[:] dst, int64_t n): cdef int64_t i dst[0] = src[0] for i in range(1, n): dst[i] = src[i] + dst[i - 1] ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/metrics/rank_cylib/setup.py ================================================ import numpy as np from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize def numpy_include(): try: numpy_include = np.get_include() except AttributeError: numpy_include = np.get_numpy_include() return numpy_include ext_modules = [ Extension( 'rank_cy', ['rank_cy.pyx'], include_dirs=[numpy_include()], ) ] setup( name='Cython-based reid evaluation code', ext_modules=cythonize(ext_modules) ) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/metrics/rank_cylib/test_cython.py ================================================ from __future__ import print_function import sys import numpy as np import timeit import os.path as osp from torchreid import metrics sys.path.insert(0, osp.dirname(osp.abspath(__file__)) + '/../../..') """ Test the speed of cython-based evaluation code. The speed improvements can be much bigger when using the real reid data, which contains a larger amount of query and gallery images. Note: you might encounter the following error: 'AssertionError: Error: all query identities do not appear in gallery'. This is normal because the inputs are random numbers. Just try again. """ print('*** Compare running time ***') setup = ''' import sys import os.path as osp import numpy as np sys.path.insert(0, osp.dirname(osp.abspath(__file__)) + '/../../..') from torchreid import metrics num_q = 30 num_g = 300 max_rank = 5 distmat = np.random.rand(num_q, num_g) * 20 q_pids = np.random.randint(0, num_q, size=num_q) g_pids = np.random.randint(0, num_g, size=num_g) q_camids = np.random.randint(0, 5, size=num_q) g_camids = np.random.randint(0, 5, size=num_g) ''' print('=> Using market1501\'s metric') pytime = timeit.timeit( 'metrics.evaluate_rank(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_cython=False)', setup=setup, number=20 ) cytime = timeit.timeit( 'metrics.evaluate_rank(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_cython=True)', setup=setup, number=20 ) print('Python time: {} s'.format(pytime)) print('Cython time: {} s'.format(cytime)) print('Cython is {} times faster than python\n'.format(pytime / cytime)) print('=> Using cuhk03\'s metric') pytime = timeit.timeit( 'metrics.evaluate_rank(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_metric_cuhk03=True, use_cython=False)', setup=setup, number=20 ) cytime = timeit.timeit( 'metrics.evaluate_rank(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_metric_cuhk03=True, use_cython=True)', setup=setup, number=20 ) print('Python time: {} s'.format(pytime)) print('Cython time: {} s'.format(cytime)) print('Cython is {} times faster than python\n'.format(pytime / cytime)) """ print("=> Check precision") num_q = 30 num_g = 300 max_rank = 5 distmat = np.random.rand(num_q, num_g) * 20 q_pids = np.random.randint(0, num_q, size=num_q) g_pids = np.random.randint(0, num_g, size=num_g) q_camids = np.random.randint(0, 5, size=num_q) g_camids = np.random.randint(0, 5, size=num_g) cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_cython=False) print("Python:\nmAP = {} \ncmc = {}\n".format(mAP, cmc)) cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_cython=True) print("Cython:\nmAP = {} \ncmc = {}\n".format(mAP, cmc)) """ ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/__init__.py ================================================ from __future__ import absolute_import import torch from .pcb import * from .mlfn import * from .hacnn import * from .osnet import * from .senet import * from .mudeep import * from .nasnet import * from .resnet import * from .densenet import * from .xception import * from .osnet_ain import * from .resnetmid import * from .shufflenet import * from .squeezenet import * from .inceptionv4 import * from .mobilenetv2 import * from .resnet_ibn_a import * from .resnet_ibn_b import * from .shufflenetv2 import * from .inceptionresnetv2 import * __model_factory = { # image classification models 'resnet18': resnet18, 'resnet34': resnet34, 'resnet50': resnet50, 'resnet101': resnet101, 'resnet152': resnet152, 'resnext50_32x4d': resnext50_32x4d, 'resnext101_32x8d': resnext101_32x8d, 'resnet50_fc512': resnet50_fc512, 'se_resnet50': se_resnet50, 'se_resnet50_fc512': se_resnet50_fc512, 'se_resnet101': se_resnet101, 'se_resnext50_32x4d': se_resnext50_32x4d, 'se_resnext101_32x4d': se_resnext101_32x4d, 'densenet121': densenet121, 'densenet169': densenet169, 'densenet201': densenet201, 'densenet161': densenet161, 'densenet121_fc512': densenet121_fc512, 'inceptionresnetv2': inceptionresnetv2, 'inceptionv4': inceptionv4, 'xception': xception, 'resnet50_ibn_a': resnet50_ibn_a, 'resnet50_ibn_b': resnet50_ibn_b, # lightweight models 'nasnsetmobile': nasnetamobile, 'mobilenetv2_x1_0': mobilenetv2_x1_0, 'mobilenetv2_x1_4': mobilenetv2_x1_4, 'shufflenet': shufflenet, 'squeezenet1_0': squeezenet1_0, 'squeezenet1_0_fc512': squeezenet1_0_fc512, 'squeezenet1_1': squeezenet1_1, 'shufflenet_v2_x0_5': shufflenet_v2_x0_5, 'shufflenet_v2_x1_0': shufflenet_v2_x1_0, 'shufflenet_v2_x1_5': shufflenet_v2_x1_5, 'shufflenet_v2_x2_0': shufflenet_v2_x2_0, # reid-specific models 'mudeep': MuDeep, 'resnet50mid': resnet50mid, 'hacnn': HACNN, 'pcb_p6': pcb_p6, 'pcb_p4': pcb_p4, 'mlfn': mlfn, 'osnet_x1_0': osnet_x1_0, 'osnet_x0_75': osnet_x0_75, 'osnet_x0_5': osnet_x0_5, 'osnet_x0_25': osnet_x0_25, 'osnet_ibn_x1_0': osnet_ibn_x1_0, 'osnet_ain_x1_0': osnet_ain_x1_0, 'osnet_ain_x0_75': osnet_ain_x0_75, 'osnet_ain_x0_5': osnet_ain_x0_5, 'osnet_ain_x0_25': osnet_ain_x0_25 } def show_avai_models(): """Displays available models. Examples:: >>> from torchreid import models >>> models.show_avai_models() """ print(list(__model_factory.keys())) def build_model( name, num_classes, loss='softmax', pretrained=True, use_gpu=True ): """A function wrapper for building a model. Args: name (str): model name. num_classes (int): number of training identities. loss (str, optional): loss function to optimize the model. Currently supports "softmax" and "triplet". Default is "softmax". pretrained (bool, optional): whether to load ImageNet-pretrained weights. Default is True. use_gpu (bool, optional): whether to use gpu. Default is True. Returns: nn.Module Examples:: >>> from torchreid import models >>> model = models.build_model('resnet50', 751, loss='softmax') """ avai_models = list(__model_factory.keys()) if name not in avai_models: raise KeyError( 'Unknown model: {}. Must be one of {}'.format(name, avai_models) ) return __model_factory[name]( num_classes=num_classes, loss=loss, pretrained=pretrained, use_gpu=use_gpu ) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/densenet.py ================================================ """ Code source: https://github.com/pytorch/vision """ from __future__ import division, absolute_import import re from collections import OrderedDict import torch import torch.nn as nn from torch.nn import functional as F from torch.utils import model_zoo __all__ = [ 'densenet121', 'densenet169', 'densenet201', 'densenet161', 'densenet121_fc512' ] model_urls = { 'densenet121': 'https://download.pytorch.org/models/densenet121-a639ec97.pth', 'densenet169': 'https://download.pytorch.org/models/densenet169-b2777c0a.pth', 'densenet201': 'https://download.pytorch.org/models/densenet201-c1103571.pth', 'densenet161': 'https://download.pytorch.org/models/densenet161-8d451a50.pth', } class _DenseLayer(nn.Sequential): def __init__(self, num_input_features, growth_rate, bn_size, drop_rate): super(_DenseLayer, self).__init__() self.add_module('norm1', nn.BatchNorm2d(num_input_features)), self.add_module('relu1', nn.ReLU(inplace=True)), self.add_module( 'conv1', nn.Conv2d( num_input_features, bn_size * growth_rate, kernel_size=1, stride=1, bias=False ) ), self.add_module('norm2', nn.BatchNorm2d(bn_size * growth_rate)), self.add_module('relu2', nn.ReLU(inplace=True)), self.add_module( 'conv2', nn.Conv2d( bn_size * growth_rate, growth_rate, kernel_size=3, stride=1, padding=1, bias=False ) ), self.drop_rate = drop_rate def forward(self, x): new_features = super(_DenseLayer, self).forward(x) if self.drop_rate > 0: new_features = F.dropout( new_features, p=self.drop_rate, training=self.training ) return torch.cat([x, new_features], 1) class _DenseBlock(nn.Sequential): def __init__( self, num_layers, num_input_features, bn_size, growth_rate, drop_rate ): super(_DenseBlock, self).__init__() for i in range(num_layers): layer = _DenseLayer( num_input_features + i*growth_rate, growth_rate, bn_size, drop_rate ) self.add_module('denselayer%d' % (i+1), layer) class _Transition(nn.Sequential): def __init__(self, num_input_features, num_output_features): super(_Transition, self).__init__() self.add_module('norm', nn.BatchNorm2d(num_input_features)) self.add_module('relu', nn.ReLU(inplace=True)) self.add_module( 'conv', nn.Conv2d( num_input_features, num_output_features, kernel_size=1, stride=1, bias=False ) ) self.add_module('pool', nn.AvgPool2d(kernel_size=2, stride=2)) class DenseNet(nn.Module): """Densely connected network. Reference: Huang et al. Densely Connected Convolutional Networks. CVPR 2017. Public keys: - ``densenet121``: DenseNet121. - ``densenet169``: DenseNet169. - ``densenet201``: DenseNet201. - ``densenet161``: DenseNet161. - ``densenet121_fc512``: DenseNet121 + FC. """ def __init__( self, num_classes, loss, growth_rate=32, block_config=(6, 12, 24, 16), num_init_features=64, bn_size=4, drop_rate=0, fc_dims=None, dropout_p=None, **kwargs ): super(DenseNet, self).__init__() self.loss = loss # First convolution self.features = nn.Sequential( OrderedDict( [ ( 'conv0', nn.Conv2d( 3, num_init_features, kernel_size=7, stride=2, padding=3, bias=False ) ), ('norm0', nn.BatchNorm2d(num_init_features)), ('relu0', nn.ReLU(inplace=True)), ( 'pool0', nn.MaxPool2d(kernel_size=3, stride=2, padding=1) ), ] ) ) # Each denseblock num_features = num_init_features for i, num_layers in enumerate(block_config): block = _DenseBlock( num_layers=num_layers, num_input_features=num_features, bn_size=bn_size, growth_rate=growth_rate, drop_rate=drop_rate ) self.features.add_module('denseblock%d' % (i+1), block) num_features = num_features + num_layers*growth_rate if i != len(block_config) - 1: trans = _Transition( num_input_features=num_features, num_output_features=num_features // 2 ) self.features.add_module('transition%d' % (i+1), trans) num_features = num_features // 2 # Final batch norm self.features.add_module('norm5', nn.BatchNorm2d(num_features)) self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.feature_dim = num_features self.fc = self._construct_fc_layer(fc_dims, num_features, dropout_p) # Linear layer self.classifier = nn.Linear(self.feature_dim, num_classes) self._init_params() def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): """Constructs fully connected layer. Args: fc_dims (list or tuple): dimensions of fc layers, if None, no fc layers are constructed input_dim (int): input dimension dropout_p (float): dropout probability, if None, dropout is unused """ if fc_dims is None: self.feature_dim = input_dim return None assert isinstance( fc_dims, (list, tuple) ), 'fc_dims must be either list or tuple, but got {}'.format( type(fc_dims) ) layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def _init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def forward(self, x): f = self.features(x) f = F.relu(f, inplace=True) v = self.global_avgpool(f) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) # '.'s are no longer allowed in module names, but pervious _DenseLayer # has keys 'norm.1', 'relu.1', 'conv.1', 'norm.2', 'relu.2', 'conv.2'. # They are also in the checkpoints in model_urls. This pattern is used # to find such keys. pattern = re.compile( r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$' ) for key in list(pretrain_dict.keys()): res = pattern.match(key) if res: new_key = res.group(1) + res.group(2) pretrain_dict[new_key] = pretrain_dict[key] del pretrain_dict[key] model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) """ Dense network configurations: -- densenet121: num_init_features=64, growth_rate=32, block_config=(6, 12, 24, 16) densenet169: num_init_features=64, growth_rate=32, block_config=(6, 12, 32, 32) densenet201: num_init_features=64, growth_rate=32, block_config=(6, 12, 48, 32) densenet161: num_init_features=96, growth_rate=48, block_config=(6, 12, 36, 24) """ def densenet121(num_classes, loss='softmax', pretrained=True, **kwargs): model = DenseNet( num_classes=num_classes, loss=loss, num_init_features=64, growth_rate=32, block_config=(6, 12, 24, 16), fc_dims=None, dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['densenet121']) return model def densenet169(num_classes, loss='softmax', pretrained=True, **kwargs): model = DenseNet( num_classes=num_classes, loss=loss, num_init_features=64, growth_rate=32, block_config=(6, 12, 32, 32), fc_dims=None, dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['densenet169']) return model def densenet201(num_classes, loss='softmax', pretrained=True, **kwargs): model = DenseNet( num_classes=num_classes, loss=loss, num_init_features=64, growth_rate=32, block_config=(6, 12, 48, 32), fc_dims=None, dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['densenet201']) return model def densenet161(num_classes, loss='softmax', pretrained=True, **kwargs): model = DenseNet( num_classes=num_classes, loss=loss, num_init_features=96, growth_rate=48, block_config=(6, 12, 36, 24), fc_dims=None, dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['densenet161']) return model def densenet121_fc512(num_classes, loss='softmax', pretrained=True, **kwargs): model = DenseNet( num_classes=num_classes, loss=loss, num_init_features=64, growth_rate=32, block_config=(6, 12, 24, 16), fc_dims=[512], dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['densenet121']) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/hacnn.py ================================================ from __future__ import division, absolute_import import torch from torch import nn from torch.nn import functional as F __all__ = ['HACNN'] class ConvBlock(nn.Module): """Basic convolutional block. convolution + batch normalization + relu. Args: in_c (int): number of input channels. out_c (int): number of output channels. k (int or tuple): kernel size. s (int or tuple): stride. p (int or tuple): padding. """ def __init__(self, in_c, out_c, k, s=1, p=0): super(ConvBlock, self).__init__() self.conv = nn.Conv2d(in_c, out_c, k, stride=s, padding=p) self.bn = nn.BatchNorm2d(out_c) def forward(self, x): return F.relu(self.bn(self.conv(x))) class InceptionA(nn.Module): def __init__(self, in_channels, out_channels): super(InceptionA, self).__init__() mid_channels = out_channels // 4 self.stream1 = nn.Sequential( ConvBlock(in_channels, mid_channels, 1), ConvBlock(mid_channels, mid_channels, 3, p=1), ) self.stream2 = nn.Sequential( ConvBlock(in_channels, mid_channels, 1), ConvBlock(mid_channels, mid_channels, 3, p=1), ) self.stream3 = nn.Sequential( ConvBlock(in_channels, mid_channels, 1), ConvBlock(mid_channels, mid_channels, 3, p=1), ) self.stream4 = nn.Sequential( nn.AvgPool2d(3, stride=1, padding=1), ConvBlock(in_channels, mid_channels, 1), ) def forward(self, x): s1 = self.stream1(x) s2 = self.stream2(x) s3 = self.stream3(x) s4 = self.stream4(x) y = torch.cat([s1, s2, s3, s4], dim=1) return y class InceptionB(nn.Module): def __init__(self, in_channels, out_channels): super(InceptionB, self).__init__() mid_channels = out_channels // 4 self.stream1 = nn.Sequential( ConvBlock(in_channels, mid_channels, 1), ConvBlock(mid_channels, mid_channels, 3, s=2, p=1), ) self.stream2 = nn.Sequential( ConvBlock(in_channels, mid_channels, 1), ConvBlock(mid_channels, mid_channels, 3, p=1), ConvBlock(mid_channels, mid_channels, 3, s=2, p=1), ) self.stream3 = nn.Sequential( nn.MaxPool2d(3, stride=2, padding=1), ConvBlock(in_channels, mid_channels * 2, 1), ) def forward(self, x): s1 = self.stream1(x) s2 = self.stream2(x) s3 = self.stream3(x) y = torch.cat([s1, s2, s3], dim=1) return y class SpatialAttn(nn.Module): """Spatial Attention (Sec. 3.1.I.1)""" def __init__(self): super(SpatialAttn, self).__init__() self.conv1 = ConvBlock(1, 1, 3, s=2, p=1) self.conv2 = ConvBlock(1, 1, 1) def forward(self, x): # global cross-channel averaging x = x.mean(1, keepdim=True) # 3-by-3 conv x = self.conv1(x) # bilinear resizing x = F.upsample( x, (x.size(2) * 2, x.size(3) * 2), mode='bilinear', align_corners=True ) # scaling conv x = self.conv2(x) return x class ChannelAttn(nn.Module): """Channel Attention (Sec. 3.1.I.2)""" def __init__(self, in_channels, reduction_rate=16): super(ChannelAttn, self).__init__() assert in_channels % reduction_rate == 0 self.conv1 = ConvBlock(in_channels, in_channels // reduction_rate, 1) self.conv2 = ConvBlock(in_channels // reduction_rate, in_channels, 1) def forward(self, x): # squeeze operation (global average pooling) x = F.avg_pool2d(x, x.size()[2:]) # excitation operation (2 conv layers) x = self.conv1(x) x = self.conv2(x) return x class SoftAttn(nn.Module): """Soft Attention (Sec. 3.1.I) Aim: Spatial Attention + Channel Attention Output: attention maps with shape identical to input. """ def __init__(self, in_channels): super(SoftAttn, self).__init__() self.spatial_attn = SpatialAttn() self.channel_attn = ChannelAttn(in_channels) self.conv = ConvBlock(in_channels, in_channels, 1) def forward(self, x): y_spatial = self.spatial_attn(x) y_channel = self.channel_attn(x) y = y_spatial * y_channel y = torch.sigmoid(self.conv(y)) return y class HardAttn(nn.Module): """Hard Attention (Sec. 3.1.II)""" def __init__(self, in_channels): super(HardAttn, self).__init__() self.fc = nn.Linear(in_channels, 4 * 2) self.init_params() def init_params(self): self.fc.weight.data.zero_() self.fc.bias.data.copy_( torch.tensor( [0, -0.75, 0, -0.25, 0, 0.25, 0, 0.75], dtype=torch.float ) ) def forward(self, x): # squeeze operation (global average pooling) x = F.avg_pool2d(x, x.size()[2:]).view(x.size(0), x.size(1)) # predict transformation parameters theta = torch.tanh(self.fc(x)) theta = theta.view(-1, 4, 2) return theta class HarmAttn(nn.Module): """Harmonious Attention (Sec. 3.1)""" def __init__(self, in_channels): super(HarmAttn, self).__init__() self.soft_attn = SoftAttn(in_channels) self.hard_attn = HardAttn(in_channels) def forward(self, x): y_soft_attn = self.soft_attn(x) theta = self.hard_attn(x) return y_soft_attn, theta class HACNN(nn.Module): """Harmonious Attention Convolutional Neural Network. Reference: Li et al. Harmonious Attention Network for Person Re-identification. CVPR 2018. Public keys: - ``hacnn``: HACNN. """ # Args: # num_classes (int): number of classes to predict # nchannels (list): number of channels AFTER concatenation # feat_dim (int): feature dimension for a single stream # learn_region (bool): whether to learn region features (i.e. local branch) def __init__( self, num_classes, loss='softmax', nchannels=[128, 256, 384], feat_dim=512, learn_region=True, use_gpu=True, **kwargs ): super(HACNN, self).__init__() self.loss = loss self.learn_region = learn_region self.use_gpu = use_gpu self.conv = ConvBlock(3, 32, 3, s=2, p=1) # Construct Inception + HarmAttn blocks # ============== Block 1 ============== self.inception1 = nn.Sequential( InceptionA(32, nchannels[0]), InceptionB(nchannels[0], nchannels[0]), ) self.ha1 = HarmAttn(nchannels[0]) # ============== Block 2 ============== self.inception2 = nn.Sequential( InceptionA(nchannels[0], nchannels[1]), InceptionB(nchannels[1], nchannels[1]), ) self.ha2 = HarmAttn(nchannels[1]) # ============== Block 3 ============== self.inception3 = nn.Sequential( InceptionA(nchannels[1], nchannels[2]), InceptionB(nchannels[2], nchannels[2]), ) self.ha3 = HarmAttn(nchannels[2]) self.fc_global = nn.Sequential( nn.Linear(nchannels[2], feat_dim), nn.BatchNorm1d(feat_dim), nn.ReLU(), ) self.classifier_global = nn.Linear(feat_dim, num_classes) if self.learn_region: self.init_scale_factors() self.local_conv1 = InceptionB(32, nchannels[0]) self.local_conv2 = InceptionB(nchannels[0], nchannels[1]) self.local_conv3 = InceptionB(nchannels[1], nchannels[2]) self.fc_local = nn.Sequential( nn.Linear(nchannels[2] * 4, feat_dim), nn.BatchNorm1d(feat_dim), nn.ReLU(), ) self.classifier_local = nn.Linear(feat_dim, num_classes) self.feat_dim = feat_dim * 2 else: self.feat_dim = feat_dim def init_scale_factors(self): # initialize scale factors (s_w, s_h) for four regions self.scale_factors = [] self.scale_factors.append( torch.tensor([[1, 0], [0, 0.25]], dtype=torch.float) ) self.scale_factors.append( torch.tensor([[1, 0], [0, 0.25]], dtype=torch.float) ) self.scale_factors.append( torch.tensor([[1, 0], [0, 0.25]], dtype=torch.float) ) self.scale_factors.append( torch.tensor([[1, 0], [0, 0.25]], dtype=torch.float) ) def stn(self, x, theta): """Performs spatial transform x: (batch, channel, height, width) theta: (batch, 2, 3) """ grid = F.affine_grid(theta, x.size()) x = F.grid_sample(x, grid) return x def transform_theta(self, theta_i, region_idx): """Transforms theta to include (s_w, s_h), resulting in (batch, 2, 3)""" scale_factors = self.scale_factors[region_idx] theta = torch.zeros(theta_i.size(0), 2, 3) theta[:, :, :2] = scale_factors theta[:, :, -1] = theta_i if self.use_gpu: theta = theta.cuda() return theta def forward(self, x): assert x.size(2) == 160 and x.size(3) == 64, \ 'Input size does not match, expected (160, 64) but got ({}, {})'.format(x.size(2), x.size(3)) x = self.conv(x) # ============== Block 1 ============== # global branch x1 = self.inception1(x) x1_attn, x1_theta = self.ha1(x1) x1_out = x1 * x1_attn # local branch if self.learn_region: x1_local_list = [] for region_idx in range(4): x1_theta_i = x1_theta[:, region_idx, :] x1_theta_i = self.transform_theta(x1_theta_i, region_idx) x1_trans_i = self.stn(x, x1_theta_i) x1_trans_i = F.upsample( x1_trans_i, (24, 28), mode='bilinear', align_corners=True ) x1_local_i = self.local_conv1(x1_trans_i) x1_local_list.append(x1_local_i) # ============== Block 2 ============== # Block 2 # global branch x2 = self.inception2(x1_out) x2_attn, x2_theta = self.ha2(x2) x2_out = x2 * x2_attn # local branch if self.learn_region: x2_local_list = [] for region_idx in range(4): x2_theta_i = x2_theta[:, region_idx, :] x2_theta_i = self.transform_theta(x2_theta_i, region_idx) x2_trans_i = self.stn(x1_out, x2_theta_i) x2_trans_i = F.upsample( x2_trans_i, (12, 14), mode='bilinear', align_corners=True ) x2_local_i = x2_trans_i + x1_local_list[region_idx] x2_local_i = self.local_conv2(x2_local_i) x2_local_list.append(x2_local_i) # ============== Block 3 ============== # Block 3 # global branch x3 = self.inception3(x2_out) x3_attn, x3_theta = self.ha3(x3) x3_out = x3 * x3_attn # local branch if self.learn_region: x3_local_list = [] for region_idx in range(4): x3_theta_i = x3_theta[:, region_idx, :] x3_theta_i = self.transform_theta(x3_theta_i, region_idx) x3_trans_i = self.stn(x2_out, x3_theta_i) x3_trans_i = F.upsample( x3_trans_i, (6, 7), mode='bilinear', align_corners=True ) x3_local_i = x3_trans_i + x2_local_list[region_idx] x3_local_i = self.local_conv3(x3_local_i) x3_local_list.append(x3_local_i) # ============== Feature generation ============== # global branch x_global = F.avg_pool2d(x3_out, x3_out.size()[2:] ).view(x3_out.size(0), x3_out.size(1)) x_global = self.fc_global(x_global) # local branch if self.learn_region: x_local_list = [] for region_idx in range(4): x_local_i = x3_local_list[region_idx] x_local_i = F.avg_pool2d(x_local_i, x_local_i.size()[2:] ).view(x_local_i.size(0), -1) x_local_list.append(x_local_i) x_local = torch.cat(x_local_list, 1) x_local = self.fc_local(x_local) if not self.training: # l2 normalization before concatenation if self.learn_region: x_global = x_global / x_global.norm(p=2, dim=1, keepdim=True) x_local = x_local / x_local.norm(p=2, dim=1, keepdim=True) return torch.cat([x_global, x_local], 1) else: return x_global prelogits_global = self.classifier_global(x_global) if self.learn_region: prelogits_local = self.classifier_local(x_local) if self.loss == 'softmax': if self.learn_region: return (prelogits_global, prelogits_local) else: return prelogits_global elif self.loss == 'triplet': if self.learn_region: return (prelogits_global, prelogits_local), (x_global, x_local) else: return prelogits_global, x_global else: raise KeyError("Unsupported loss: {}".format(self.loss)) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/inceptionresnetv2.py ================================================ """ Code imported from https://github.com/Cadene/pretrained-models.pytorch """ from __future__ import division, absolute_import import torch import torch.nn as nn import torch.utils.model_zoo as model_zoo __all__ = ['inceptionresnetv2'] pretrained_settings = { 'inceptionresnetv2': { 'imagenet': { 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionresnetv2-520b38e4.pth', 'input_space': 'RGB', 'input_size': [3, 299, 299], 'input_range': [0, 1], 'mean': [0.5, 0.5, 0.5], 'std': [0.5, 0.5, 0.5], 'num_classes': 1000 }, 'imagenet+background': { 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionresnetv2-520b38e4.pth', 'input_space': 'RGB', 'input_size': [3, 299, 299], 'input_range': [0, 1], 'mean': [0.5, 0.5, 0.5], 'std': [0.5, 0.5, 0.5], 'num_classes': 1001 } } } class BasicConv2d(nn.Module): def __init__(self, in_planes, out_planes, kernel_size, stride, padding=0): super(BasicConv2d, self).__init__() self.conv = nn.Conv2d( in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, bias=False ) # verify bias false self.bn = nn.BatchNorm2d( out_planes, eps=0.001, # value found in tensorflow momentum=0.1, # default pytorch value affine=True ) self.relu = nn.ReLU(inplace=False) def forward(self, x): x = self.conv(x) x = self.bn(x) x = self.relu(x) return x class Mixed_5b(nn.Module): def __init__(self): super(Mixed_5b, self).__init__() self.branch0 = BasicConv2d(192, 96, kernel_size=1, stride=1) self.branch1 = nn.Sequential( BasicConv2d(192, 48, kernel_size=1, stride=1), BasicConv2d(48, 64, kernel_size=5, stride=1, padding=2) ) self.branch2 = nn.Sequential( BasicConv2d(192, 64, kernel_size=1, stride=1), BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1), BasicConv2d(96, 96, kernel_size=3, stride=1, padding=1) ) self.branch3 = nn.Sequential( nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), BasicConv2d(192, 64, kernel_size=1, stride=1) ) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) x3 = self.branch3(x) out = torch.cat((x0, x1, x2, x3), 1) return out class Block35(nn.Module): def __init__(self, scale=1.0): super(Block35, self).__init__() self.scale = scale self.branch0 = BasicConv2d(320, 32, kernel_size=1, stride=1) self.branch1 = nn.Sequential( BasicConv2d(320, 32, kernel_size=1, stride=1), BasicConv2d(32, 32, kernel_size=3, stride=1, padding=1) ) self.branch2 = nn.Sequential( BasicConv2d(320, 32, kernel_size=1, stride=1), BasicConv2d(32, 48, kernel_size=3, stride=1, padding=1), BasicConv2d(48, 64, kernel_size=3, stride=1, padding=1) ) self.conv2d = nn.Conv2d(128, 320, kernel_size=1, stride=1) self.relu = nn.ReLU(inplace=False) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) out = torch.cat((x0, x1, x2), 1) out = self.conv2d(out) out = out * self.scale + x out = self.relu(out) return out class Mixed_6a(nn.Module): def __init__(self): super(Mixed_6a, self).__init__() self.branch0 = BasicConv2d(320, 384, kernel_size=3, stride=2) self.branch1 = nn.Sequential( BasicConv2d(320, 256, kernel_size=1, stride=1), BasicConv2d(256, 256, kernel_size=3, stride=1, padding=1), BasicConv2d(256, 384, kernel_size=3, stride=2) ) self.branch2 = nn.MaxPool2d(3, stride=2) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) out = torch.cat((x0, x1, x2), 1) return out class Block17(nn.Module): def __init__(self, scale=1.0): super(Block17, self).__init__() self.scale = scale self.branch0 = BasicConv2d(1088, 192, kernel_size=1, stride=1) self.branch1 = nn.Sequential( BasicConv2d(1088, 128, kernel_size=1, stride=1), BasicConv2d( 128, 160, kernel_size=(1, 7), stride=1, padding=(0, 3) ), BasicConv2d( 160, 192, kernel_size=(7, 1), stride=1, padding=(3, 0) ) ) self.conv2d = nn.Conv2d(384, 1088, kernel_size=1, stride=1) self.relu = nn.ReLU(inplace=False) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) out = torch.cat((x0, x1), 1) out = self.conv2d(out) out = out * self.scale + x out = self.relu(out) return out class Mixed_7a(nn.Module): def __init__(self): super(Mixed_7a, self).__init__() self.branch0 = nn.Sequential( BasicConv2d(1088, 256, kernel_size=1, stride=1), BasicConv2d(256, 384, kernel_size=3, stride=2) ) self.branch1 = nn.Sequential( BasicConv2d(1088, 256, kernel_size=1, stride=1), BasicConv2d(256, 288, kernel_size=3, stride=2) ) self.branch2 = nn.Sequential( BasicConv2d(1088, 256, kernel_size=1, stride=1), BasicConv2d(256, 288, kernel_size=3, stride=1, padding=1), BasicConv2d(288, 320, kernel_size=3, stride=2) ) self.branch3 = nn.MaxPool2d(3, stride=2) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) x3 = self.branch3(x) out = torch.cat((x0, x1, x2, x3), 1) return out class Block8(nn.Module): def __init__(self, scale=1.0, noReLU=False): super(Block8, self).__init__() self.scale = scale self.noReLU = noReLU self.branch0 = BasicConv2d(2080, 192, kernel_size=1, stride=1) self.branch1 = nn.Sequential( BasicConv2d(2080, 192, kernel_size=1, stride=1), BasicConv2d( 192, 224, kernel_size=(1, 3), stride=1, padding=(0, 1) ), BasicConv2d( 224, 256, kernel_size=(3, 1), stride=1, padding=(1, 0) ) ) self.conv2d = nn.Conv2d(448, 2080, kernel_size=1, stride=1) if not self.noReLU: self.relu = nn.ReLU(inplace=False) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) out = torch.cat((x0, x1), 1) out = self.conv2d(out) out = out * self.scale + x if not self.noReLU: out = self.relu(out) return out # ---------------- # Model Definition # ---------------- class InceptionResNetV2(nn.Module): """Inception-ResNet-V2. Reference: Szegedy et al. Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning. AAAI 2017. Public keys: - ``inceptionresnetv2``: Inception-ResNet-V2. """ def __init__(self, num_classes, loss='softmax', **kwargs): super(InceptionResNetV2, self).__init__() self.loss = loss # Modules self.conv2d_1a = BasicConv2d(3, 32, kernel_size=3, stride=2) self.conv2d_2a = BasicConv2d(32, 32, kernel_size=3, stride=1) self.conv2d_2b = BasicConv2d( 32, 64, kernel_size=3, stride=1, padding=1 ) self.maxpool_3a = nn.MaxPool2d(3, stride=2) self.conv2d_3b = BasicConv2d(64, 80, kernel_size=1, stride=1) self.conv2d_4a = BasicConv2d(80, 192, kernel_size=3, stride=1) self.maxpool_5a = nn.MaxPool2d(3, stride=2) self.mixed_5b = Mixed_5b() self.repeat = nn.Sequential( Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17), Block35(scale=0.17) ) self.mixed_6a = Mixed_6a() self.repeat_1 = nn.Sequential( Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10), Block17(scale=0.10) ) self.mixed_7a = Mixed_7a() self.repeat_2 = nn.Sequential( Block8(scale=0.20), Block8(scale=0.20), Block8(scale=0.20), Block8(scale=0.20), Block8(scale=0.20), Block8(scale=0.20), Block8(scale=0.20), Block8(scale=0.20), Block8(scale=0.20) ) self.block8 = Block8(noReLU=True) self.conv2d_7b = BasicConv2d(2080, 1536, kernel_size=1, stride=1) self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.classifier = nn.Linear(1536, num_classes) def load_imagenet_weights(self): settings = pretrained_settings['inceptionresnetv2']['imagenet'] pretrain_dict = model_zoo.load_url(settings['url']) model_dict = self.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) self.load_state_dict(model_dict) def featuremaps(self, x): x = self.conv2d_1a(x) x = self.conv2d_2a(x) x = self.conv2d_2b(x) x = self.maxpool_3a(x) x = self.conv2d_3b(x) x = self.conv2d_4a(x) x = self.maxpool_5a(x) x = self.mixed_5b(x) x = self.repeat(x) x = self.mixed_6a(x) x = self.repeat_1(x) x = self.mixed_7a(x) x = self.repeat_2(x) x = self.block8(x) x = self.conv2d_7b(x) return x def forward(self, x): f = self.featuremaps(x) v = self.global_avgpool(f) v = v.view(v.size(0), -1) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError('Unsupported loss: {}'.format(self.loss)) def inceptionresnetv2(num_classes, loss='softmax', pretrained=True, **kwargs): model = InceptionResNetV2(num_classes=num_classes, loss=loss, **kwargs) if pretrained: model.load_imagenet_weights() return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/inceptionv4.py ================================================ from __future__ import division, absolute_import import torch import torch.nn as nn import torch.utils.model_zoo as model_zoo __all__ = ['inceptionv4'] """ Code imported from https://github.com/Cadene/pretrained-models.pytorch """ pretrained_settings = { 'inceptionv4': { 'imagenet': { 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionv4-8e4777a0.pth', 'input_space': 'RGB', 'input_size': [3, 299, 299], 'input_range': [0, 1], 'mean': [0.5, 0.5, 0.5], 'std': [0.5, 0.5, 0.5], 'num_classes': 1000 }, 'imagenet+background': { 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionv4-8e4777a0.pth', 'input_space': 'RGB', 'input_size': [3, 299, 299], 'input_range': [0, 1], 'mean': [0.5, 0.5, 0.5], 'std': [0.5, 0.5, 0.5], 'num_classes': 1001 } } } class BasicConv2d(nn.Module): def __init__(self, in_planes, out_planes, kernel_size, stride, padding=0): super(BasicConv2d, self).__init__() self.conv = nn.Conv2d( in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, bias=False ) # verify bias false self.bn = nn.BatchNorm2d( out_planes, eps=0.001, # value found in tensorflow momentum=0.1, # default pytorch value affine=True ) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) x = self.relu(x) return x class Mixed_3a(nn.Module): def __init__(self): super(Mixed_3a, self).__init__() self.maxpool = nn.MaxPool2d(3, stride=2) self.conv = BasicConv2d(64, 96, kernel_size=3, stride=2) def forward(self, x): x0 = self.maxpool(x) x1 = self.conv(x) out = torch.cat((x0, x1), 1) return out class Mixed_4a(nn.Module): def __init__(self): super(Mixed_4a, self).__init__() self.branch0 = nn.Sequential( BasicConv2d(160, 64, kernel_size=1, stride=1), BasicConv2d(64, 96, kernel_size=3, stride=1) ) self.branch1 = nn.Sequential( BasicConv2d(160, 64, kernel_size=1, stride=1), BasicConv2d(64, 64, kernel_size=(1, 7), stride=1, padding=(0, 3)), BasicConv2d(64, 64, kernel_size=(7, 1), stride=1, padding=(3, 0)), BasicConv2d(64, 96, kernel_size=(3, 3), stride=1) ) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) out = torch.cat((x0, x1), 1) return out class Mixed_5a(nn.Module): def __init__(self): super(Mixed_5a, self).__init__() self.conv = BasicConv2d(192, 192, kernel_size=3, stride=2) self.maxpool = nn.MaxPool2d(3, stride=2) def forward(self, x): x0 = self.conv(x) x1 = self.maxpool(x) out = torch.cat((x0, x1), 1) return out class Inception_A(nn.Module): def __init__(self): super(Inception_A, self).__init__() self.branch0 = BasicConv2d(384, 96, kernel_size=1, stride=1) self.branch1 = nn.Sequential( BasicConv2d(384, 64, kernel_size=1, stride=1), BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1) ) self.branch2 = nn.Sequential( BasicConv2d(384, 64, kernel_size=1, stride=1), BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1), BasicConv2d(96, 96, kernel_size=3, stride=1, padding=1) ) self.branch3 = nn.Sequential( nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), BasicConv2d(384, 96, kernel_size=1, stride=1) ) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) x3 = self.branch3(x) out = torch.cat((x0, x1, x2, x3), 1) return out class Reduction_A(nn.Module): def __init__(self): super(Reduction_A, self).__init__() self.branch0 = BasicConv2d(384, 384, kernel_size=3, stride=2) self.branch1 = nn.Sequential( BasicConv2d(384, 192, kernel_size=1, stride=1), BasicConv2d(192, 224, kernel_size=3, stride=1, padding=1), BasicConv2d(224, 256, kernel_size=3, stride=2) ) self.branch2 = nn.MaxPool2d(3, stride=2) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) out = torch.cat((x0, x1, x2), 1) return out class Inception_B(nn.Module): def __init__(self): super(Inception_B, self).__init__() self.branch0 = BasicConv2d(1024, 384, kernel_size=1, stride=1) self.branch1 = nn.Sequential( BasicConv2d(1024, 192, kernel_size=1, stride=1), BasicConv2d( 192, 224, kernel_size=(1, 7), stride=1, padding=(0, 3) ), BasicConv2d( 224, 256, kernel_size=(7, 1), stride=1, padding=(3, 0) ) ) self.branch2 = nn.Sequential( BasicConv2d(1024, 192, kernel_size=1, stride=1), BasicConv2d( 192, 192, kernel_size=(7, 1), stride=1, padding=(3, 0) ), BasicConv2d( 192, 224, kernel_size=(1, 7), stride=1, padding=(0, 3) ), BasicConv2d( 224, 224, kernel_size=(7, 1), stride=1, padding=(3, 0) ), BasicConv2d( 224, 256, kernel_size=(1, 7), stride=1, padding=(0, 3) ) ) self.branch3 = nn.Sequential( nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), BasicConv2d(1024, 128, kernel_size=1, stride=1) ) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) x3 = self.branch3(x) out = torch.cat((x0, x1, x2, x3), 1) return out class Reduction_B(nn.Module): def __init__(self): super(Reduction_B, self).__init__() self.branch0 = nn.Sequential( BasicConv2d(1024, 192, kernel_size=1, stride=1), BasicConv2d(192, 192, kernel_size=3, stride=2) ) self.branch1 = nn.Sequential( BasicConv2d(1024, 256, kernel_size=1, stride=1), BasicConv2d( 256, 256, kernel_size=(1, 7), stride=1, padding=(0, 3) ), BasicConv2d( 256, 320, kernel_size=(7, 1), stride=1, padding=(3, 0) ), BasicConv2d(320, 320, kernel_size=3, stride=2) ) self.branch2 = nn.MaxPool2d(3, stride=2) def forward(self, x): x0 = self.branch0(x) x1 = self.branch1(x) x2 = self.branch2(x) out = torch.cat((x0, x1, x2), 1) return out class Inception_C(nn.Module): def __init__(self): super(Inception_C, self).__init__() self.branch0 = BasicConv2d(1536, 256, kernel_size=1, stride=1) self.branch1_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1) self.branch1_1a = BasicConv2d( 384, 256, kernel_size=(1, 3), stride=1, padding=(0, 1) ) self.branch1_1b = BasicConv2d( 384, 256, kernel_size=(3, 1), stride=1, padding=(1, 0) ) self.branch2_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1) self.branch2_1 = BasicConv2d( 384, 448, kernel_size=(3, 1), stride=1, padding=(1, 0) ) self.branch2_2 = BasicConv2d( 448, 512, kernel_size=(1, 3), stride=1, padding=(0, 1) ) self.branch2_3a = BasicConv2d( 512, 256, kernel_size=(1, 3), stride=1, padding=(0, 1) ) self.branch2_3b = BasicConv2d( 512, 256, kernel_size=(3, 1), stride=1, padding=(1, 0) ) self.branch3 = nn.Sequential( nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), BasicConv2d(1536, 256, kernel_size=1, stride=1) ) def forward(self, x): x0 = self.branch0(x) x1_0 = self.branch1_0(x) x1_1a = self.branch1_1a(x1_0) x1_1b = self.branch1_1b(x1_0) x1 = torch.cat((x1_1a, x1_1b), 1) x2_0 = self.branch2_0(x) x2_1 = self.branch2_1(x2_0) x2_2 = self.branch2_2(x2_1) x2_3a = self.branch2_3a(x2_2) x2_3b = self.branch2_3b(x2_2) x2 = torch.cat((x2_3a, x2_3b), 1) x3 = self.branch3(x) out = torch.cat((x0, x1, x2, x3), 1) return out class InceptionV4(nn.Module): """Inception-v4. Reference: Szegedy et al. Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning. AAAI 2017. Public keys: - ``inceptionv4``: InceptionV4. """ def __init__(self, num_classes, loss, **kwargs): super(InceptionV4, self).__init__() self.loss = loss self.features = nn.Sequential( BasicConv2d(3, 32, kernel_size=3, stride=2), BasicConv2d(32, 32, kernel_size=3, stride=1), BasicConv2d(32, 64, kernel_size=3, stride=1, padding=1), Mixed_3a(), Mixed_4a(), Mixed_5a(), Inception_A(), Inception_A(), Inception_A(), Inception_A(), Reduction_A(), # Mixed_6a Inception_B(), Inception_B(), Inception_B(), Inception_B(), Inception_B(), Inception_B(), Inception_B(), Reduction_B(), # Mixed_7a Inception_C(), Inception_C(), Inception_C() ) self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.classifier = nn.Linear(1536, num_classes) def forward(self, x): f = self.features(x) v = self.global_avgpool(f) v = v.view(v.size(0), -1) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def inceptionv4(num_classes, loss='softmax', pretrained=True, **kwargs): model = InceptionV4(num_classes, loss, **kwargs) if pretrained: model_url = pretrained_settings['inceptionv4']['imagenet']['url'] init_pretrained_weights(model, model_url) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/mlfn.py ================================================ from __future__ import division, absolute_import import torch import torch.utils.model_zoo as model_zoo from torch import nn from torch.nn import functional as F __all__ = ['mlfn'] model_urls = { # training epoch = 5, top1 = 51.6 'imagenet': 'https://mega.nz/#!YHxAhaxC!yu9E6zWl0x5zscSouTdbZu8gdFFytDdl-RAdD2DEfpk', } class MLFNBlock(nn.Module): def __init__( self, in_channels, out_channels, stride, fsm_channels, groups=32 ): super(MLFNBlock, self).__init__() self.groups = groups mid_channels = out_channels // 2 # Factor Modules self.fm_conv1 = nn.Conv2d(in_channels, mid_channels, 1, bias=False) self.fm_bn1 = nn.BatchNorm2d(mid_channels) self.fm_conv2 = nn.Conv2d( mid_channels, mid_channels, 3, stride=stride, padding=1, bias=False, groups=self.groups ) self.fm_bn2 = nn.BatchNorm2d(mid_channels) self.fm_conv3 = nn.Conv2d(mid_channels, out_channels, 1, bias=False) self.fm_bn3 = nn.BatchNorm2d(out_channels) # Factor Selection Module self.fsm = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(in_channels, fsm_channels[0], 1), nn.BatchNorm2d(fsm_channels[0]), nn.ReLU(inplace=True), nn.Conv2d(fsm_channels[0], fsm_channels[1], 1), nn.BatchNorm2d(fsm_channels[1]), nn.ReLU(inplace=True), nn.Conv2d(fsm_channels[1], self.groups, 1), nn.BatchNorm2d(self.groups), nn.Sigmoid(), ) self.downsample = None if in_channels != out_channels or stride > 1: self.downsample = nn.Sequential( nn.Conv2d( in_channels, out_channels, 1, stride=stride, bias=False ), nn.BatchNorm2d(out_channels), ) def forward(self, x): residual = x s = self.fsm(x) # reduce dimension x = self.fm_conv1(x) x = self.fm_bn1(x) x = F.relu(x, inplace=True) # group convolution x = self.fm_conv2(x) x = self.fm_bn2(x) x = F.relu(x, inplace=True) # factor selection b, c = x.size(0), x.size(1) n = c // self.groups ss = s.repeat(1, n, 1, 1) # from (b, g, 1, 1) to (b, g*n=c, 1, 1) ss = ss.view(b, n, self.groups, 1, 1) ss = ss.permute(0, 2, 1, 3, 4).contiguous() ss = ss.view(b, c, 1, 1) x = ss * x # recover dimension x = self.fm_conv3(x) x = self.fm_bn3(x) x = F.relu(x, inplace=True) if self.downsample is not None: residual = self.downsample(residual) return F.relu(residual + x, inplace=True), s class MLFN(nn.Module): """Multi-Level Factorisation Net. Reference: Chang et al. Multi-Level Factorisation Net for Person Re-Identification. CVPR 2018. Public keys: - ``mlfn``: MLFN (Multi-Level Factorisation Net). """ def __init__( self, num_classes, loss='softmax', groups=32, channels=[64, 256, 512, 1024, 2048], embed_dim=1024, **kwargs ): super(MLFN, self).__init__() self.loss = loss self.groups = groups # first convolutional layer self.conv1 = nn.Conv2d(3, channels[0], 7, stride=2, padding=3) self.bn1 = nn.BatchNorm2d(channels[0]) self.maxpool = nn.MaxPool2d(3, stride=2, padding=1) # main body self.feature = nn.ModuleList( [ # layer 1-3 MLFNBlock(channels[0], channels[1], 1, [128, 64], self.groups), MLFNBlock(channels[1], channels[1], 1, [128, 64], self.groups), MLFNBlock(channels[1], channels[1], 1, [128, 64], self.groups), # layer 4-7 MLFNBlock( channels[1], channels[2], 2, [256, 128], self.groups ), MLFNBlock( channels[2], channels[2], 1, [256, 128], self.groups ), MLFNBlock( channels[2], channels[2], 1, [256, 128], self.groups ), MLFNBlock( channels[2], channels[2], 1, [256, 128], self.groups ), # layer 8-13 MLFNBlock( channels[2], channels[3], 2, [512, 128], self.groups ), MLFNBlock( channels[3], channels[3], 1, [512, 128], self.groups ), MLFNBlock( channels[3], channels[3], 1, [512, 128], self.groups ), MLFNBlock( channels[3], channels[3], 1, [512, 128], self.groups ), MLFNBlock( channels[3], channels[3], 1, [512, 128], self.groups ), MLFNBlock( channels[3], channels[3], 1, [512, 128], self.groups ), # layer 14-16 MLFNBlock( channels[3], channels[4], 2, [512, 128], self.groups ), MLFNBlock( channels[4], channels[4], 1, [512, 128], self.groups ), MLFNBlock( channels[4], channels[4], 1, [512, 128], self.groups ), ] ) self.global_avgpool = nn.AdaptiveAvgPool2d(1) # projection functions self.fc_x = nn.Sequential( nn.Conv2d(channels[4], embed_dim, 1, bias=False), nn.BatchNorm2d(embed_dim), nn.ReLU(inplace=True), ) self.fc_s = nn.Sequential( nn.Conv2d(self.groups * 16, embed_dim, 1, bias=False), nn.BatchNorm2d(embed_dim), nn.ReLU(inplace=True), ) self.classifier = nn.Linear(embed_dim, num_classes) self.init_params() def init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = F.relu(x, inplace=True) x = self.maxpool(x) s_hat = [] for block in self.feature: x, s = block(x) s_hat.append(s) s_hat = torch.cat(s_hat, 1) x = self.global_avgpool(x) x = self.fc_x(x) s_hat = self.fc_s(s_hat) v = (x+s_hat) * 0.5 v = v.view(v.size(0), -1) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def mlfn(num_classes, loss='softmax', pretrained=True, **kwargs): model = MLFN(num_classes, loss, **kwargs) if pretrained: # init_pretrained_weights(model, model_urls['imagenet']) import warnings warnings.warn( 'The imagenet pretrained weights need to be manually downloaded from {}' .format(model_urls['imagenet']) ) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/mobilenetv2.py ================================================ from __future__ import division, absolute_import import torch.utils.model_zoo as model_zoo from torch import nn from torch.nn import functional as F __all__ = ['mobilenetv2_x1_0', 'mobilenetv2_x1_4'] model_urls = { # 1.0: top-1 71.3 'mobilenetv2_x1_0': 'https://mega.nz/#!NKp2wAIA!1NH1pbNzY_M2hVk_hdsxNM1NUOWvvGPHhaNr-fASF6c', # 1.4: top-1 73.9 'mobilenetv2_x1_4': 'https://mega.nz/#!RGhgEIwS!xN2s2ZdyqI6vQ3EwgmRXLEW3khr9tpXg96G9SUJugGk', } class ConvBlock(nn.Module): """Basic convolutional block. convolution (bias discarded) + batch normalization + relu6. Args: in_c (int): number of input channels. out_c (int): number of output channels. k (int or tuple): kernel size. s (int or tuple): stride. p (int or tuple): padding. g (int): number of blocked connections from input channels to output channels (default: 1). """ def __init__(self, in_c, out_c, k, s=1, p=0, g=1): super(ConvBlock, self).__init__() self.conv = nn.Conv2d( in_c, out_c, k, stride=s, padding=p, bias=False, groups=g ) self.bn = nn.BatchNorm2d(out_c) def forward(self, x): return F.relu6(self.bn(self.conv(x))) class Bottleneck(nn.Module): def __init__(self, in_channels, out_channels, expansion_factor, stride=1): super(Bottleneck, self).__init__() mid_channels = in_channels * expansion_factor self.use_residual = stride == 1 and in_channels == out_channels self.conv1 = ConvBlock(in_channels, mid_channels, 1) self.dwconv2 = ConvBlock( mid_channels, mid_channels, 3, stride, 1, g=mid_channels ) self.conv3 = nn.Sequential( nn.Conv2d(mid_channels, out_channels, 1, bias=False), nn.BatchNorm2d(out_channels), ) def forward(self, x): m = self.conv1(x) m = self.dwconv2(m) m = self.conv3(m) if self.use_residual: return x + m else: return m class MobileNetV2(nn.Module): """MobileNetV2. Reference: Sandler et al. MobileNetV2: Inverted Residuals and Linear Bottlenecks. CVPR 2018. Public keys: - ``mobilenetv2_x1_0``: MobileNetV2 x1.0. - ``mobilenetv2_x1_4``: MobileNetV2 x1.4. """ def __init__( self, num_classes, width_mult=1, loss='softmax', fc_dims=None, dropout_p=None, **kwargs ): super(MobileNetV2, self).__init__() self.loss = loss self.in_channels = int(32 * width_mult) self.feature_dim = int(1280 * width_mult) if width_mult > 1 else 1280 # construct layers self.conv1 = ConvBlock(3, self.in_channels, 3, s=2, p=1) self.conv2 = self._make_layer( Bottleneck, 1, int(16 * width_mult), 1, 1 ) self.conv3 = self._make_layer( Bottleneck, 6, int(24 * width_mult), 2, 2 ) self.conv4 = self._make_layer( Bottleneck, 6, int(32 * width_mult), 3, 2 ) self.conv5 = self._make_layer( Bottleneck, 6, int(64 * width_mult), 4, 2 ) self.conv6 = self._make_layer( Bottleneck, 6, int(96 * width_mult), 3, 1 ) self.conv7 = self._make_layer( Bottleneck, 6, int(160 * width_mult), 3, 2 ) self.conv8 = self._make_layer( Bottleneck, 6, int(320 * width_mult), 1, 1 ) self.conv9 = ConvBlock(self.in_channels, self.feature_dim, 1) self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.fc = self._construct_fc_layer( fc_dims, self.feature_dim, dropout_p ) self.classifier = nn.Linear(self.feature_dim, num_classes) self._init_params() def _make_layer(self, block, t, c, n, s): # t: expansion factor # c: output channels # n: number of blocks # s: stride for first layer layers = [] layers.append(block(self.in_channels, c, t, s)) self.in_channels = c for i in range(1, n): layers.append(block(self.in_channels, c, t)) return nn.Sequential(*layers) def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): """Constructs fully connected layer. Args: fc_dims (list or tuple): dimensions of fc layers, if None, no fc layers are constructed input_dim (int): input dimension dropout_p (float): dropout probability, if None, dropout is unused """ if fc_dims is None: self.feature_dim = input_dim return None assert isinstance( fc_dims, (list, tuple) ), 'fc_dims must be either list or tuple, but got {}'.format( type(fc_dims) ) layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def _init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def featuremaps(self, x): x = self.conv1(x) x = self.conv2(x) x = self.conv3(x) x = self.conv4(x) x = self.conv5(x) x = self.conv6(x) x = self.conv7(x) x = self.conv8(x) x = self.conv9(x) return x def forward(self, x): f = self.featuremaps(x) v = self.global_avgpool(f) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError("Unsupported loss: {}".format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def mobilenetv2_x1_0(num_classes, loss, pretrained=True, **kwargs): model = MobileNetV2( num_classes, loss=loss, width_mult=1, fc_dims=None, dropout_p=None, **kwargs ) if pretrained: # init_pretrained_weights(model, model_urls['mobilenetv2_x1_0']) import warnings warnings.warn( 'The imagenet pretrained weights need to be manually downloaded from {}' .format(model_urls['mobilenetv2_x1_0']) ) return model def mobilenetv2_x1_4(num_classes, loss, pretrained=True, **kwargs): model = MobileNetV2( num_classes, loss=loss, width_mult=1.4, fc_dims=None, dropout_p=None, **kwargs ) if pretrained: # init_pretrained_weights(model, model_urls['mobilenetv2_x1_4']) import warnings warnings.warn( 'The imagenet pretrained weights need to be manually downloaded from {}' .format(model_urls['mobilenetv2_x1_4']) ) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/mudeep.py ================================================ from __future__ import division, absolute_import import torch from torch import nn from torch.nn import functional as F __all__ = ['MuDeep'] class ConvBlock(nn.Module): """Basic convolutional block. convolution + batch normalization + relu. Args: in_c (int): number of input channels. out_c (int): number of output channels. k (int or tuple): kernel size. s (int or tuple): stride. p (int or tuple): padding. """ def __init__(self, in_c, out_c, k, s, p): super(ConvBlock, self).__init__() self.conv = nn.Conv2d(in_c, out_c, k, stride=s, padding=p) self.bn = nn.BatchNorm2d(out_c) def forward(self, x): return F.relu(self.bn(self.conv(x))) class ConvLayers(nn.Module): """Preprocessing layers.""" def __init__(self): super(ConvLayers, self).__init__() self.conv1 = ConvBlock(3, 48, k=3, s=1, p=1) self.conv2 = ConvBlock(48, 96, k=3, s=1, p=1) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.maxpool(x) return x class MultiScaleA(nn.Module): """Multi-scale stream layer A (Sec.3.1)""" def __init__(self): super(MultiScaleA, self).__init__() self.stream1 = nn.Sequential( ConvBlock(96, 96, k=1, s=1, p=0), ConvBlock(96, 24, k=3, s=1, p=1), ) self.stream2 = nn.Sequential( nn.AvgPool2d(kernel_size=3, stride=1, padding=1), ConvBlock(96, 24, k=1, s=1, p=0), ) self.stream3 = ConvBlock(96, 24, k=1, s=1, p=0) self.stream4 = nn.Sequential( ConvBlock(96, 16, k=1, s=1, p=0), ConvBlock(16, 24, k=3, s=1, p=1), ConvBlock(24, 24, k=3, s=1, p=1), ) def forward(self, x): s1 = self.stream1(x) s2 = self.stream2(x) s3 = self.stream3(x) s4 = self.stream4(x) y = torch.cat([s1, s2, s3, s4], dim=1) return y class Reduction(nn.Module): """Reduction layer (Sec.3.1)""" def __init__(self): super(Reduction, self).__init__() self.stream1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.stream2 = ConvBlock(96, 96, k=3, s=2, p=1) self.stream3 = nn.Sequential( ConvBlock(96, 48, k=1, s=1, p=0), ConvBlock(48, 56, k=3, s=1, p=1), ConvBlock(56, 64, k=3, s=2, p=1), ) def forward(self, x): s1 = self.stream1(x) s2 = self.stream2(x) s3 = self.stream3(x) y = torch.cat([s1, s2, s3], dim=1) return y class MultiScaleB(nn.Module): """Multi-scale stream layer B (Sec.3.1)""" def __init__(self): super(MultiScaleB, self).__init__() self.stream1 = nn.Sequential( nn.AvgPool2d(kernel_size=3, stride=1, padding=1), ConvBlock(256, 256, k=1, s=1, p=0), ) self.stream2 = nn.Sequential( ConvBlock(256, 64, k=1, s=1, p=0), ConvBlock(64, 128, k=(1, 3), s=1, p=(0, 1)), ConvBlock(128, 256, k=(3, 1), s=1, p=(1, 0)), ) self.stream3 = ConvBlock(256, 256, k=1, s=1, p=0) self.stream4 = nn.Sequential( ConvBlock(256, 64, k=1, s=1, p=0), ConvBlock(64, 64, k=(1, 3), s=1, p=(0, 1)), ConvBlock(64, 128, k=(3, 1), s=1, p=(1, 0)), ConvBlock(128, 128, k=(1, 3), s=1, p=(0, 1)), ConvBlock(128, 256, k=(3, 1), s=1, p=(1, 0)), ) def forward(self, x): s1 = self.stream1(x) s2 = self.stream2(x) s3 = self.stream3(x) s4 = self.stream4(x) return s1, s2, s3, s4 class Fusion(nn.Module): """Saliency-based learning fusion layer (Sec.3.2)""" def __init__(self): super(Fusion, self).__init__() self.a1 = nn.Parameter(torch.rand(1, 256, 1, 1)) self.a2 = nn.Parameter(torch.rand(1, 256, 1, 1)) self.a3 = nn.Parameter(torch.rand(1, 256, 1, 1)) self.a4 = nn.Parameter(torch.rand(1, 256, 1, 1)) # We add an average pooling layer to reduce the spatial dimension # of feature maps, which differs from the original paper. self.avgpool = nn.AvgPool2d(kernel_size=4, stride=4, padding=0) def forward(self, x1, x2, x3, x4): s1 = self.a1.expand_as(x1) * x1 s2 = self.a2.expand_as(x2) * x2 s3 = self.a3.expand_as(x3) * x3 s4 = self.a4.expand_as(x4) * x4 y = self.avgpool(s1 + s2 + s3 + s4) return y class MuDeep(nn.Module): """Multiscale deep neural network. Reference: Qian et al. Multi-scale Deep Learning Architectures for Person Re-identification. ICCV 2017. Public keys: - ``mudeep``: Multiscale deep neural network. """ def __init__(self, num_classes, loss='softmax', **kwargs): super(MuDeep, self).__init__() self.loss = loss self.block1 = ConvLayers() self.block2 = MultiScaleA() self.block3 = Reduction() self.block4 = MultiScaleB() self.block5 = Fusion() # Due to this fully connected layer, input image has to be fixed # in shape, i.e. (3, 256, 128), such that the last convolutional feature # maps are of shape (256, 16, 8). If input shape is changed, # the input dimension of this layer has to be changed accordingly. self.fc = nn.Sequential( nn.Linear(256 * 16 * 8, 4096), nn.BatchNorm1d(4096), nn.ReLU(), ) self.classifier = nn.Linear(4096, num_classes) self.feat_dim = 4096 def featuremaps(self, x): x = self.block1(x) x = self.block2(x) x = self.block3(x) x = self.block4(x) x = self.block5(*x) return x def forward(self, x): x = self.featuremaps(x) x = x.view(x.size(0), -1) x = self.fc(x) y = self.classifier(x) if not self.training: return x if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, x else: raise KeyError('Unsupported loss: {}'.format(self.loss)) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/nasnet.py ================================================ from __future__ import division, absolute_import import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.model_zoo as model_zoo __all__ = ['nasnetamobile'] """ NASNet Mobile Thanks to Anastasiia (https://github.com/DagnyT) for the great help, support and motivation! ------------------------------------------------------------------------------------ Architecture | Top-1 Acc | Top-5 Acc | Multiply-Adds | Params (M) ------------------------------------------------------------------------------------ | NASNet-A (4 @ 1056) | 74.08% | 91.74% | 564 M | 5.3 | ------------------------------------------------------------------------------------ # References: - [Learning Transferable Architectures for Scalable Image Recognition] (https://arxiv.org/abs/1707.07012) """ """ Code imported from https://github.com/Cadene/pretrained-models.pytorch """ pretrained_settings = { 'nasnetamobile': { 'imagenet': { # 'url': 'https://github.com/veronikayurchuk/pretrained-models.pytorch/releases/download/v1.0/nasnetmobile-7e03cead.pth.tar', 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/nasnetamobile-7e03cead.pth', 'input_space': 'RGB', 'input_size': [3, 224, 224], # resize 256 'input_range': [0, 1], 'mean': [0.5, 0.5, 0.5], 'std': [0.5, 0.5, 0.5], 'num_classes': 1000 }, # 'imagenet+background': { # # 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/nasnetalarge-a1897284.pth', # 'input_space': 'RGB', # 'input_size': [3, 224, 224], # resize 256 # 'input_range': [0, 1], # 'mean': [0.5, 0.5, 0.5], # 'std': [0.5, 0.5, 0.5], # 'num_classes': 1001 # } } } class MaxPoolPad(nn.Module): def __init__(self): super(MaxPoolPad, self).__init__() self.pad = nn.ZeroPad2d((1, 0, 1, 0)) self.pool = nn.MaxPool2d(3, stride=2, padding=1) def forward(self, x): x = self.pad(x) x = self.pool(x) x = x[:, :, 1:, 1:].contiguous() return x class AvgPoolPad(nn.Module): def __init__(self, stride=2, padding=1): super(AvgPoolPad, self).__init__() self.pad = nn.ZeroPad2d((1, 0, 1, 0)) self.pool = nn.AvgPool2d( 3, stride=stride, padding=padding, count_include_pad=False ) def forward(self, x): x = self.pad(x) x = self.pool(x) x = x[:, :, 1:, 1:].contiguous() return x class SeparableConv2d(nn.Module): def __init__( self, in_channels, out_channels, dw_kernel, dw_stride, dw_padding, bias=False ): super(SeparableConv2d, self).__init__() self.depthwise_conv2d = nn.Conv2d( in_channels, in_channels, dw_kernel, stride=dw_stride, padding=dw_padding, bias=bias, groups=in_channels ) self.pointwise_conv2d = nn.Conv2d( in_channels, out_channels, 1, stride=1, bias=bias ) def forward(self, x): x = self.depthwise_conv2d(x) x = self.pointwise_conv2d(x) return x class BranchSeparables(nn.Module): def __init__( self, in_channels, out_channels, kernel_size, stride, padding, name=None, bias=False ): super(BranchSeparables, self).__init__() self.relu = nn.ReLU() self.separable_1 = SeparableConv2d( in_channels, in_channels, kernel_size, stride, padding, bias=bias ) self.bn_sep_1 = nn.BatchNorm2d( in_channels, eps=0.001, momentum=0.1, affine=True ) self.relu1 = nn.ReLU() self.separable_2 = SeparableConv2d( in_channels, out_channels, kernel_size, 1, padding, bias=bias ) self.bn_sep_2 = nn.BatchNorm2d( out_channels, eps=0.001, momentum=0.1, affine=True ) self.name = name def forward(self, x): x = self.relu(x) if self.name == 'specific': x = nn.ZeroPad2d((1, 0, 1, 0))(x) x = self.separable_1(x) if self.name == 'specific': x = x[:, :, 1:, 1:].contiguous() x = self.bn_sep_1(x) x = self.relu1(x) x = self.separable_2(x) x = self.bn_sep_2(x) return x class BranchSeparablesStem(nn.Module): def __init__( self, in_channels, out_channels, kernel_size, stride, padding, bias=False ): super(BranchSeparablesStem, self).__init__() self.relu = nn.ReLU() self.separable_1 = SeparableConv2d( in_channels, out_channels, kernel_size, stride, padding, bias=bias ) self.bn_sep_1 = nn.BatchNorm2d( out_channels, eps=0.001, momentum=0.1, affine=True ) self.relu1 = nn.ReLU() self.separable_2 = SeparableConv2d( out_channels, out_channels, kernel_size, 1, padding, bias=bias ) self.bn_sep_2 = nn.BatchNorm2d( out_channels, eps=0.001, momentum=0.1, affine=True ) def forward(self, x): x = self.relu(x) x = self.separable_1(x) x = self.bn_sep_1(x) x = self.relu1(x) x = self.separable_2(x) x = self.bn_sep_2(x) return x class BranchSeparablesReduction(BranchSeparables): def __init__( self, in_channels, out_channels, kernel_size, stride, padding, z_padding=1, bias=False ): BranchSeparables.__init__( self, in_channels, out_channels, kernel_size, stride, padding, bias ) self.padding = nn.ZeroPad2d((z_padding, 0, z_padding, 0)) def forward(self, x): x = self.relu(x) x = self.padding(x) x = self.separable_1(x) x = x[:, :, 1:, 1:].contiguous() x = self.bn_sep_1(x) x = self.relu1(x) x = self.separable_2(x) x = self.bn_sep_2(x) return x class CellStem0(nn.Module): def __init__(self, stem_filters, num_filters=42): super(CellStem0, self).__init__() self.num_filters = num_filters self.stem_filters = stem_filters self.conv_1x1 = nn.Sequential() self.conv_1x1.add_module('relu', nn.ReLU()) self.conv_1x1.add_module( 'conv', nn.Conv2d( self.stem_filters, self.num_filters, 1, stride=1, bias=False ) ) self.conv_1x1.add_module( 'bn', nn.BatchNorm2d( self.num_filters, eps=0.001, momentum=0.1, affine=True ) ) self.comb_iter_0_left = BranchSeparables( self.num_filters, self.num_filters, 5, 2, 2 ) self.comb_iter_0_right = BranchSeparablesStem( self.stem_filters, self.num_filters, 7, 2, 3, bias=False ) self.comb_iter_1_left = nn.MaxPool2d(3, stride=2, padding=1) self.comb_iter_1_right = BranchSeparablesStem( self.stem_filters, self.num_filters, 7, 2, 3, bias=False ) self.comb_iter_2_left = nn.AvgPool2d( 3, stride=2, padding=1, count_include_pad=False ) self.comb_iter_2_right = BranchSeparablesStem( self.stem_filters, self.num_filters, 5, 2, 2, bias=False ) self.comb_iter_3_right = nn.AvgPool2d( 3, stride=1, padding=1, count_include_pad=False ) self.comb_iter_4_left = BranchSeparables( self.num_filters, self.num_filters, 3, 1, 1, bias=False ) self.comb_iter_4_right = nn.MaxPool2d(3, stride=2, padding=1) def forward(self, x): x1 = self.conv_1x1(x) x_comb_iter_0_left = self.comb_iter_0_left(x1) x_comb_iter_0_right = self.comb_iter_0_right(x) x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right x_comb_iter_1_left = self.comb_iter_1_left(x1) x_comb_iter_1_right = self.comb_iter_1_right(x) x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right x_comb_iter_2_left = self.comb_iter_2_left(x1) x_comb_iter_2_right = self.comb_iter_2_right(x) x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0) x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1 x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0) x_comb_iter_4_right = self.comb_iter_4_right(x1) x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right x_out = torch.cat( [x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1 ) return x_out class CellStem1(nn.Module): def __init__(self, stem_filters, num_filters): super(CellStem1, self).__init__() self.num_filters = num_filters self.stem_filters = stem_filters self.conv_1x1 = nn.Sequential() self.conv_1x1.add_module('relu', nn.ReLU()) self.conv_1x1.add_module( 'conv', nn.Conv2d( 2 * self.num_filters, self.num_filters, 1, stride=1, bias=False ) ) self.conv_1x1.add_module( 'bn', nn.BatchNorm2d( self.num_filters, eps=0.001, momentum=0.1, affine=True ) ) self.relu = nn.ReLU() self.path_1 = nn.Sequential() self.path_1.add_module( 'avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False) ) self.path_1.add_module( 'conv', nn.Conv2d( self.stem_filters, self.num_filters // 2, 1, stride=1, bias=False ) ) self.path_2 = nn.ModuleList() self.path_2.add_module('pad', nn.ZeroPad2d((0, 1, 0, 1))) self.path_2.add_module( 'avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False) ) self.path_2.add_module( 'conv', nn.Conv2d( self.stem_filters, self.num_filters // 2, 1, stride=1, bias=False ) ) self.final_path_bn = nn.BatchNorm2d( self.num_filters, eps=0.001, momentum=0.1, affine=True ) self.comb_iter_0_left = BranchSeparables( self.num_filters, self.num_filters, 5, 2, 2, name='specific', bias=False ) self.comb_iter_0_right = BranchSeparables( self.num_filters, self.num_filters, 7, 2, 3, name='specific', bias=False ) # self.comb_iter_1_left = nn.MaxPool2d(3, stride=2, padding=1) self.comb_iter_1_left = MaxPoolPad() self.comb_iter_1_right = BranchSeparables( self.num_filters, self.num_filters, 7, 2, 3, name='specific', bias=False ) # self.comb_iter_2_left = nn.AvgPool2d(3, stride=2, padding=1, count_include_pad=False) self.comb_iter_2_left = AvgPoolPad() self.comb_iter_2_right = BranchSeparables( self.num_filters, self.num_filters, 5, 2, 2, name='specific', bias=False ) self.comb_iter_3_right = nn.AvgPool2d( 3, stride=1, padding=1, count_include_pad=False ) self.comb_iter_4_left = BranchSeparables( self.num_filters, self.num_filters, 3, 1, 1, name='specific', bias=False ) # self.comb_iter_4_right = nn.MaxPool2d(3, stride=2, padding=1) self.comb_iter_4_right = MaxPoolPad() def forward(self, x_conv0, x_stem_0): x_left = self.conv_1x1(x_stem_0) x_relu = self.relu(x_conv0) # path 1 x_path1 = self.path_1(x_relu) # path 2 x_path2 = self.path_2.pad(x_relu) x_path2 = x_path2[:, :, 1:, 1:] x_path2 = self.path_2.avgpool(x_path2) x_path2 = self.path_2.conv(x_path2) # final path x_right = self.final_path_bn(torch.cat([x_path1, x_path2], 1)) x_comb_iter_0_left = self.comb_iter_0_left(x_left) x_comb_iter_0_right = self.comb_iter_0_right(x_right) x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right x_comb_iter_1_left = self.comb_iter_1_left(x_left) x_comb_iter_1_right = self.comb_iter_1_right(x_right) x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right x_comb_iter_2_left = self.comb_iter_2_left(x_left) x_comb_iter_2_right = self.comb_iter_2_right(x_right) x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0) x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1 x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0) x_comb_iter_4_right = self.comb_iter_4_right(x_left) x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right x_out = torch.cat( [x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1 ) return x_out class FirstCell(nn.Module): def __init__( self, in_channels_left, out_channels_left, in_channels_right, out_channels_right ): super(FirstCell, self).__init__() self.conv_1x1 = nn.Sequential() self.conv_1x1.add_module('relu', nn.ReLU()) self.conv_1x1.add_module( 'conv', nn.Conv2d( in_channels_right, out_channels_right, 1, stride=1, bias=False ) ) self.conv_1x1.add_module( 'bn', nn.BatchNorm2d( out_channels_right, eps=0.001, momentum=0.1, affine=True ) ) self.relu = nn.ReLU() self.path_1 = nn.Sequential() self.path_1.add_module( 'avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False) ) self.path_1.add_module( 'conv', nn.Conv2d( in_channels_left, out_channels_left, 1, stride=1, bias=False ) ) self.path_2 = nn.ModuleList() self.path_2.add_module('pad', nn.ZeroPad2d((0, 1, 0, 1))) self.path_2.add_module( 'avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False) ) self.path_2.add_module( 'conv', nn.Conv2d( in_channels_left, out_channels_left, 1, stride=1, bias=False ) ) self.final_path_bn = nn.BatchNorm2d( out_channels_left * 2, eps=0.001, momentum=0.1, affine=True ) self.comb_iter_0_left = BranchSeparables( out_channels_right, out_channels_right, 5, 1, 2, bias=False ) self.comb_iter_0_right = BranchSeparables( out_channels_right, out_channels_right, 3, 1, 1, bias=False ) self.comb_iter_1_left = BranchSeparables( out_channels_right, out_channels_right, 5, 1, 2, bias=False ) self.comb_iter_1_right = BranchSeparables( out_channels_right, out_channels_right, 3, 1, 1, bias=False ) self.comb_iter_2_left = nn.AvgPool2d( 3, stride=1, padding=1, count_include_pad=False ) self.comb_iter_3_left = nn.AvgPool2d( 3, stride=1, padding=1, count_include_pad=False ) self.comb_iter_3_right = nn.AvgPool2d( 3, stride=1, padding=1, count_include_pad=False ) self.comb_iter_4_left = BranchSeparables( out_channels_right, out_channels_right, 3, 1, 1, bias=False ) def forward(self, x, x_prev): x_relu = self.relu(x_prev) # path 1 x_path1 = self.path_1(x_relu) # path 2 x_path2 = self.path_2.pad(x_relu) x_path2 = x_path2[:, :, 1:, 1:] x_path2 = self.path_2.avgpool(x_path2) x_path2 = self.path_2.conv(x_path2) # final path x_left = self.final_path_bn(torch.cat([x_path1, x_path2], 1)) x_right = self.conv_1x1(x) x_comb_iter_0_left = self.comb_iter_0_left(x_right) x_comb_iter_0_right = self.comb_iter_0_right(x_left) x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right x_comb_iter_1_left = self.comb_iter_1_left(x_left) x_comb_iter_1_right = self.comb_iter_1_right(x_left) x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right x_comb_iter_2_left = self.comb_iter_2_left(x_right) x_comb_iter_2 = x_comb_iter_2_left + x_left x_comb_iter_3_left = self.comb_iter_3_left(x_left) x_comb_iter_3_right = self.comb_iter_3_right(x_left) x_comb_iter_3 = x_comb_iter_3_left + x_comb_iter_3_right x_comb_iter_4_left = self.comb_iter_4_left(x_right) x_comb_iter_4 = x_comb_iter_4_left + x_right x_out = torch.cat( [ x_left, x_comb_iter_0, x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4 ], 1 ) return x_out class NormalCell(nn.Module): def __init__( self, in_channels_left, out_channels_left, in_channels_right, out_channels_right ): super(NormalCell, self).__init__() self.conv_prev_1x1 = nn.Sequential() self.conv_prev_1x1.add_module('relu', nn.ReLU()) self.conv_prev_1x1.add_module( 'conv', nn.Conv2d( in_channels_left, out_channels_left, 1, stride=1, bias=False ) ) self.conv_prev_1x1.add_module( 'bn', nn.BatchNorm2d( out_channels_left, eps=0.001, momentum=0.1, affine=True ) ) self.conv_1x1 = nn.Sequential() self.conv_1x1.add_module('relu', nn.ReLU()) self.conv_1x1.add_module( 'conv', nn.Conv2d( in_channels_right, out_channels_right, 1, stride=1, bias=False ) ) self.conv_1x1.add_module( 'bn', nn.BatchNorm2d( out_channels_right, eps=0.001, momentum=0.1, affine=True ) ) self.comb_iter_0_left = BranchSeparables( out_channels_right, out_channels_right, 5, 1, 2, bias=False ) self.comb_iter_0_right = BranchSeparables( out_channels_left, out_channels_left, 3, 1, 1, bias=False ) self.comb_iter_1_left = BranchSeparables( out_channels_left, out_channels_left, 5, 1, 2, bias=False ) self.comb_iter_1_right = BranchSeparables( out_channels_left, out_channels_left, 3, 1, 1, bias=False ) self.comb_iter_2_left = nn.AvgPool2d( 3, stride=1, padding=1, count_include_pad=False ) self.comb_iter_3_left = nn.AvgPool2d( 3, stride=1, padding=1, count_include_pad=False ) self.comb_iter_3_right = nn.AvgPool2d( 3, stride=1, padding=1, count_include_pad=False ) self.comb_iter_4_left = BranchSeparables( out_channels_right, out_channels_right, 3, 1, 1, bias=False ) def forward(self, x, x_prev): x_left = self.conv_prev_1x1(x_prev) x_right = self.conv_1x1(x) x_comb_iter_0_left = self.comb_iter_0_left(x_right) x_comb_iter_0_right = self.comb_iter_0_right(x_left) x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right x_comb_iter_1_left = self.comb_iter_1_left(x_left) x_comb_iter_1_right = self.comb_iter_1_right(x_left) x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right x_comb_iter_2_left = self.comb_iter_2_left(x_right) x_comb_iter_2 = x_comb_iter_2_left + x_left x_comb_iter_3_left = self.comb_iter_3_left(x_left) x_comb_iter_3_right = self.comb_iter_3_right(x_left) x_comb_iter_3 = x_comb_iter_3_left + x_comb_iter_3_right x_comb_iter_4_left = self.comb_iter_4_left(x_right) x_comb_iter_4 = x_comb_iter_4_left + x_right x_out = torch.cat( [ x_left, x_comb_iter_0, x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4 ], 1 ) return x_out class ReductionCell0(nn.Module): def __init__( self, in_channels_left, out_channels_left, in_channels_right, out_channels_right ): super(ReductionCell0, self).__init__() self.conv_prev_1x1 = nn.Sequential() self.conv_prev_1x1.add_module('relu', nn.ReLU()) self.conv_prev_1x1.add_module( 'conv', nn.Conv2d( in_channels_left, out_channels_left, 1, stride=1, bias=False ) ) self.conv_prev_1x1.add_module( 'bn', nn.BatchNorm2d( out_channels_left, eps=0.001, momentum=0.1, affine=True ) ) self.conv_1x1 = nn.Sequential() self.conv_1x1.add_module('relu', nn.ReLU()) self.conv_1x1.add_module( 'conv', nn.Conv2d( in_channels_right, out_channels_right, 1, stride=1, bias=False ) ) self.conv_1x1.add_module( 'bn', nn.BatchNorm2d( out_channels_right, eps=0.001, momentum=0.1, affine=True ) ) self.comb_iter_0_left = BranchSeparablesReduction( out_channels_right, out_channels_right, 5, 2, 2, bias=False ) self.comb_iter_0_right = BranchSeparablesReduction( out_channels_right, out_channels_right, 7, 2, 3, bias=False ) self.comb_iter_1_left = MaxPoolPad() self.comb_iter_1_right = BranchSeparablesReduction( out_channels_right, out_channels_right, 7, 2, 3, bias=False ) self.comb_iter_2_left = AvgPoolPad() self.comb_iter_2_right = BranchSeparablesReduction( out_channels_right, out_channels_right, 5, 2, 2, bias=False ) self.comb_iter_3_right = nn.AvgPool2d( 3, stride=1, padding=1, count_include_pad=False ) self.comb_iter_4_left = BranchSeparablesReduction( out_channels_right, out_channels_right, 3, 1, 1, bias=False ) self.comb_iter_4_right = MaxPoolPad() def forward(self, x, x_prev): x_left = self.conv_prev_1x1(x_prev) x_right = self.conv_1x1(x) x_comb_iter_0_left = self.comb_iter_0_left(x_right) x_comb_iter_0_right = self.comb_iter_0_right(x_left) x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right x_comb_iter_1_left = self.comb_iter_1_left(x_right) x_comb_iter_1_right = self.comb_iter_1_right(x_left) x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right x_comb_iter_2_left = self.comb_iter_2_left(x_right) x_comb_iter_2_right = self.comb_iter_2_right(x_left) x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0) x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1 x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0) x_comb_iter_4_right = self.comb_iter_4_right(x_right) x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right x_out = torch.cat( [x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1 ) return x_out class ReductionCell1(nn.Module): def __init__( self, in_channels_left, out_channels_left, in_channels_right, out_channels_right ): super(ReductionCell1, self).__init__() self.conv_prev_1x1 = nn.Sequential() self.conv_prev_1x1.add_module('relu', nn.ReLU()) self.conv_prev_1x1.add_module( 'conv', nn.Conv2d( in_channels_left, out_channels_left, 1, stride=1, bias=False ) ) self.conv_prev_1x1.add_module( 'bn', nn.BatchNorm2d( out_channels_left, eps=0.001, momentum=0.1, affine=True ) ) self.conv_1x1 = nn.Sequential() self.conv_1x1.add_module('relu', nn.ReLU()) self.conv_1x1.add_module( 'conv', nn.Conv2d( in_channels_right, out_channels_right, 1, stride=1, bias=False ) ) self.conv_1x1.add_module( 'bn', nn.BatchNorm2d( out_channels_right, eps=0.001, momentum=0.1, affine=True ) ) self.comb_iter_0_left = BranchSeparables( out_channels_right, out_channels_right, 5, 2, 2, name='specific', bias=False ) self.comb_iter_0_right = BranchSeparables( out_channels_right, out_channels_right, 7, 2, 3, name='specific', bias=False ) # self.comb_iter_1_left = nn.MaxPool2d(3, stride=2, padding=1) self.comb_iter_1_left = MaxPoolPad() self.comb_iter_1_right = BranchSeparables( out_channels_right, out_channels_right, 7, 2, 3, name='specific', bias=False ) # self.comb_iter_2_left = nn.AvgPool2d(3, stride=2, padding=1, count_include_pad=False) self.comb_iter_2_left = AvgPoolPad() self.comb_iter_2_right = BranchSeparables( out_channels_right, out_channels_right, 5, 2, 2, name='specific', bias=False ) self.comb_iter_3_right = nn.AvgPool2d( 3, stride=1, padding=1, count_include_pad=False ) self.comb_iter_4_left = BranchSeparables( out_channels_right, out_channels_right, 3, 1, 1, name='specific', bias=False ) # self.comb_iter_4_right = nn.MaxPool2d(3, stride=2, padding=1) self.comb_iter_4_right = MaxPoolPad() def forward(self, x, x_prev): x_left = self.conv_prev_1x1(x_prev) x_right = self.conv_1x1(x) x_comb_iter_0_left = self.comb_iter_0_left(x_right) x_comb_iter_0_right = self.comb_iter_0_right(x_left) x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right x_comb_iter_1_left = self.comb_iter_1_left(x_right) x_comb_iter_1_right = self.comb_iter_1_right(x_left) x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right x_comb_iter_2_left = self.comb_iter_2_left(x_right) x_comb_iter_2_right = self.comb_iter_2_right(x_left) x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0) x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1 x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0) x_comb_iter_4_right = self.comb_iter_4_right(x_right) x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right x_out = torch.cat( [x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1 ) return x_out class NASNetAMobile(nn.Module): """Neural Architecture Search (NAS). Reference: Zoph et al. Learning Transferable Architectures for Scalable Image Recognition. CVPR 2018. Public keys: - ``nasnetamobile``: NASNet-A Mobile. """ def __init__( self, num_classes, loss, stem_filters=32, penultimate_filters=1056, filters_multiplier=2, **kwargs ): super(NASNetAMobile, self).__init__() self.stem_filters = stem_filters self.penultimate_filters = penultimate_filters self.filters_multiplier = filters_multiplier self.loss = loss filters = self.penultimate_filters // 24 # 24 is default value for the architecture self.conv0 = nn.Sequential() self.conv0.add_module( 'conv', nn.Conv2d( in_channels=3, out_channels=self.stem_filters, kernel_size=3, padding=0, stride=2, bias=False ) ) self.conv0.add_module( 'bn', nn.BatchNorm2d( self.stem_filters, eps=0.001, momentum=0.1, affine=True ) ) self.cell_stem_0 = CellStem0( self.stem_filters, num_filters=filters // (filters_multiplier**2) ) self.cell_stem_1 = CellStem1( self.stem_filters, num_filters=filters // filters_multiplier ) self.cell_0 = FirstCell( in_channels_left=filters, out_channels_left=filters // 2, # 1, 0.5 in_channels_right=2 * filters, out_channels_right=filters ) # 2, 1 self.cell_1 = NormalCell( in_channels_left=2 * filters, out_channels_left=filters, # 2, 1 in_channels_right=6 * filters, out_channels_right=filters ) # 6, 1 self.cell_2 = NormalCell( in_channels_left=6 * filters, out_channels_left=filters, # 6, 1 in_channels_right=6 * filters, out_channels_right=filters ) # 6, 1 self.cell_3 = NormalCell( in_channels_left=6 * filters, out_channels_left=filters, # 6, 1 in_channels_right=6 * filters, out_channels_right=filters ) # 6, 1 self.reduction_cell_0 = ReductionCell0( in_channels_left=6 * filters, out_channels_left=2 * filters, # 6, 2 in_channels_right=6 * filters, out_channels_right=2 * filters ) # 6, 2 self.cell_6 = FirstCell( in_channels_left=6 * filters, out_channels_left=filters, # 6, 1 in_channels_right=8 * filters, out_channels_right=2 * filters ) # 8, 2 self.cell_7 = NormalCell( in_channels_left=8 * filters, out_channels_left=2 * filters, # 8, 2 in_channels_right=12 * filters, out_channels_right=2 * filters ) # 12, 2 self.cell_8 = NormalCell( in_channels_left=12 * filters, out_channels_left=2 * filters, # 12, 2 in_channels_right=12 * filters, out_channels_right=2 * filters ) # 12, 2 self.cell_9 = NormalCell( in_channels_left=12 * filters, out_channels_left=2 * filters, # 12, 2 in_channels_right=12 * filters, out_channels_right=2 * filters ) # 12, 2 self.reduction_cell_1 = ReductionCell1( in_channels_left=12 * filters, out_channels_left=4 * filters, # 12, 4 in_channels_right=12 * filters, out_channels_right=4 * filters ) # 12, 4 self.cell_12 = FirstCell( in_channels_left=12 * filters, out_channels_left=2 * filters, # 12, 2 in_channels_right=16 * filters, out_channels_right=4 * filters ) # 16, 4 self.cell_13 = NormalCell( in_channels_left=16 * filters, out_channels_left=4 * filters, # 16, 4 in_channels_right=24 * filters, out_channels_right=4 * filters ) # 24, 4 self.cell_14 = NormalCell( in_channels_left=24 * filters, out_channels_left=4 * filters, # 24, 4 in_channels_right=24 * filters, out_channels_right=4 * filters ) # 24, 4 self.cell_15 = NormalCell( in_channels_left=24 * filters, out_channels_left=4 * filters, # 24, 4 in_channels_right=24 * filters, out_channels_right=4 * filters ) # 24, 4 self.relu = nn.ReLU() self.dropout = nn.Dropout() self.classifier = nn.Linear(24 * filters, num_classes) self._init_params() def _init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def features(self, input): x_conv0 = self.conv0(input) x_stem_0 = self.cell_stem_0(x_conv0) x_stem_1 = self.cell_stem_1(x_conv0, x_stem_0) x_cell_0 = self.cell_0(x_stem_1, x_stem_0) x_cell_1 = self.cell_1(x_cell_0, x_stem_1) x_cell_2 = self.cell_2(x_cell_1, x_cell_0) x_cell_3 = self.cell_3(x_cell_2, x_cell_1) x_reduction_cell_0 = self.reduction_cell_0(x_cell_3, x_cell_2) x_cell_6 = self.cell_6(x_reduction_cell_0, x_cell_3) x_cell_7 = self.cell_7(x_cell_6, x_reduction_cell_0) x_cell_8 = self.cell_8(x_cell_7, x_cell_6) x_cell_9 = self.cell_9(x_cell_8, x_cell_7) x_reduction_cell_1 = self.reduction_cell_1(x_cell_9, x_cell_8) x_cell_12 = self.cell_12(x_reduction_cell_1, x_cell_9) x_cell_13 = self.cell_13(x_cell_12, x_reduction_cell_1) x_cell_14 = self.cell_14(x_cell_13, x_cell_12) x_cell_15 = self.cell_15(x_cell_14, x_cell_13) x_cell_15 = self.relu(x_cell_15) x_cell_15 = F.avg_pool2d( x_cell_15, x_cell_15.size()[2:] ) # global average pool x_cell_15 = x_cell_15.view(x_cell_15.size(0), -1) x_cell_15 = self.dropout(x_cell_15) return x_cell_15 def forward(self, input): v = self.features(input) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def nasnetamobile(num_classes, loss='softmax', pretrained=True, **kwargs): model = NASNetAMobile(num_classes, loss, **kwargs) if pretrained: model_url = pretrained_settings['nasnetamobile']['imagenet']['url'] init_pretrained_weights(model, model_url) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/osnet.py ================================================ from __future__ import division, absolute_import import warnings import torch from torch import nn from torch.nn import functional as F __all__ = [ 'osnet_x1_0', 'osnet_x0_75', 'osnet_x0_5', 'osnet_x0_25', 'osnet_ibn_x1_0' ] pretrained_urls = { 'osnet_x1_0': 'https://drive.google.com/uc?id=1LaG1EJpHrxdAxKnSCJ_i0u-nbxSAeiFY', 'osnet_x0_75': 'https://drive.google.com/uc?id=1uwA9fElHOk3ZogwbeY5GkLI6QPTX70Hq', 'osnet_x0_5': 'https://drive.google.com/uc?id=16DGLbZukvVYgINws8u8deSaOqjybZ83i', 'osnet_x0_25': 'https://drive.google.com/uc?id=1rb8UN5ZzPKRc_xvtHlyDh-cSz88YX9hs', 'osnet_ibn_x1_0': 'https://drive.google.com/uc?id=1sr90V6irlYYDd4_4ISU2iruoRG8J__6l' } ########## # Basic layers ########## class ConvLayer(nn.Module): """Convolution layer (conv + bn + relu).""" def __init__( self, in_channels, out_channels, kernel_size, stride=1, padding=0, groups=1, IN=False ): super(ConvLayer, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, kernel_size, stride=stride, padding=padding, bias=False, groups=groups ) if IN: self.bn = nn.InstanceNorm2d(out_channels, affine=True) else: self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) x = self.relu(x) return x class Conv1x1(nn.Module): """1x1 convolution + bn + relu.""" def __init__(self, in_channels, out_channels, stride=1, groups=1): super(Conv1x1, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=stride, padding=0, bias=False, groups=groups ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) x = self.relu(x) return x class Conv1x1Linear(nn.Module): """1x1 convolution + bn (w/o non-linearity).""" def __init__(self, in_channels, out_channels, stride=1): super(Conv1x1Linear, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=stride, padding=0, bias=False ) self.bn = nn.BatchNorm2d(out_channels) def forward(self, x): x = self.conv(x) x = self.bn(x) return x class Conv3x3(nn.Module): """3x3 convolution + bn + relu.""" def __init__(self, in_channels, out_channels, stride=1, groups=1): super(Conv3x3, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 3, stride=stride, padding=1, bias=False, groups=groups ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv(x) x = self.bn(x) x = self.relu(x) return x class LightConv3x3(nn.Module): """Lightweight 3x3 convolution. 1x1 (linear) + dw 3x3 (nonlinear). """ def __init__(self, in_channels, out_channels): super(LightConv3x3, self).__init__() self.conv1 = nn.Conv2d( in_channels, out_channels, 1, stride=1, padding=0, bias=False ) self.conv2 = nn.Conv2d( out_channels, out_channels, 3, stride=1, padding=1, bias=False, groups=out_channels ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.bn(x) x = self.relu(x) return x ########## # Building blocks for omni-scale feature learning ########## class ChannelGate(nn.Module): """A mini-network that generates channel-wise gates conditioned on input tensor.""" def __init__( self, in_channels, num_gates=None, return_gates=False, gate_activation='sigmoid', reduction=16, layer_norm=False ): super(ChannelGate, self).__init__() if num_gates is None: num_gates = in_channels self.return_gates = return_gates self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.fc1 = nn.Conv2d( in_channels, in_channels // reduction, kernel_size=1, bias=True, padding=0 ) self.norm1 = None if layer_norm: self.norm1 = nn.LayerNorm((in_channels // reduction, 1, 1)) self.relu = nn.ReLU(inplace=True) self.fc2 = nn.Conv2d( in_channels // reduction, num_gates, kernel_size=1, bias=True, padding=0 ) if gate_activation == 'sigmoid': self.gate_activation = nn.Sigmoid() elif gate_activation == 'relu': self.gate_activation = nn.ReLU(inplace=True) elif gate_activation == 'linear': self.gate_activation = None else: raise RuntimeError( "Unknown gate activation: {}".format(gate_activation) ) def forward(self, x): input = x x = self.global_avgpool(x) x = self.fc1(x) if self.norm1 is not None: x = self.norm1(x) x = self.relu(x) x = self.fc2(x) if self.gate_activation is not None: x = self.gate_activation(x) if self.return_gates: return x return input * x class OSBlock(nn.Module): """Omni-scale feature learning block.""" def __init__( self, in_channels, out_channels, IN=False, bottleneck_reduction=4, **kwargs ): super(OSBlock, self).__init__() mid_channels = out_channels // bottleneck_reduction self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2a = LightConv3x3(mid_channels, mid_channels) self.conv2b = nn.Sequential( LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), ) self.conv2c = nn.Sequential( LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), ) self.conv2d = nn.Sequential( LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), LightConv3x3(mid_channels, mid_channels), ) self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) self.IN = None if IN: self.IN = nn.InstanceNorm2d(out_channels, affine=True) def forward(self, x): identity = x x1 = self.conv1(x) x2a = self.conv2a(x1) x2b = self.conv2b(x1) x2c = self.conv2c(x1) x2d = self.conv2d(x1) x2 = self.gate(x2a) + self.gate(x2b) + self.gate(x2c) + self.gate(x2d) x3 = self.conv3(x2) if self.downsample is not None: identity = self.downsample(identity) out = x3 + identity if self.IN is not None: out = self.IN(out) return F.relu(out) ########## # Network architecture ########## class OSNet(nn.Module): """Omni-Scale Network. Reference: - Zhou et al. Omni-Scale Feature Learning for Person Re-Identification. ICCV, 2019. - Zhou et al. Learning Generalisable Omni-Scale Representations for Person Re-Identification. TPAMI, 2021. """ def __init__( self, num_classes, blocks, layers, channels, feature_dim=512, loss='softmax', IN=False, **kwargs ): super(OSNet, self).__init__() num_blocks = len(blocks) assert num_blocks == len(layers) assert num_blocks == len(channels) - 1 self.loss = loss self.feature_dim = feature_dim # convolutional backbone self.conv1 = ConvLayer(3, channels[0], 7, stride=2, padding=3, IN=IN) self.maxpool = nn.MaxPool2d(3, stride=2, padding=1) self.conv2 = self._make_layer( blocks[0], layers[0], channels[0], channels[1], reduce_spatial_size=True, IN=IN ) self.conv3 = self._make_layer( blocks[1], layers[1], channels[1], channels[2], reduce_spatial_size=True ) self.conv4 = self._make_layer( blocks[2], layers[2], channels[2], channels[3], reduce_spatial_size=False ) self.conv5 = Conv1x1(channels[3], channels[3]) self.global_avgpool = nn.AdaptiveAvgPool2d(1) # fully connected layer self.fc = self._construct_fc_layer( self.feature_dim, channels[3], dropout_p=None ) # identity classification layer self.classifier = nn.Linear(self.feature_dim, num_classes) self._init_params() def _make_layer( self, block, layer, in_channels, out_channels, reduce_spatial_size, IN=False ): layers = [] layers.append(block(in_channels, out_channels, IN=IN)) for i in range(1, layer): layers.append(block(out_channels, out_channels, IN=IN)) if reduce_spatial_size: layers.append( nn.Sequential( Conv1x1(out_channels, out_channels), nn.AvgPool2d(2, stride=2) ) ) return nn.Sequential(*layers) def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): if fc_dims is None or fc_dims < 0: self.feature_dim = input_dim return None if isinstance(fc_dims, int): fc_dims = [fc_dims] layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def _init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def featuremaps(self, x): x = self.conv1(x) x = self.maxpool(x) x = self.conv2(x) x = self.conv3(x) x = self.conv4(x) x = self.conv5(x) return x def forward(self, x, return_featuremaps=False): x = self.featuremaps(x) if return_featuremaps: return x v = self.global_avgpool(x) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError("Unsupported loss: {}".format(self.loss)) def init_pretrained_weights(model, key=''): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ import os import errno import gdown from collections import OrderedDict def _get_torch_home(): ENV_TORCH_HOME = 'TORCH_HOME' ENV_XDG_CACHE_HOME = 'XDG_CACHE_HOME' DEFAULT_CACHE_DIR = '~/.cache' torch_home = os.path.expanduser( os.getenv( ENV_TORCH_HOME, os.path.join( os.getenv(ENV_XDG_CACHE_HOME, DEFAULT_CACHE_DIR), 'torch' ) ) ) return torch_home torch_home = _get_torch_home() model_dir = os.path.join(torch_home, 'checkpoints') try: os.makedirs(model_dir) except OSError as e: if e.errno == errno.EEXIST: # Directory already exists, ignore. pass else: # Unexpected OSError, re-raise. raise filename = key + '_imagenet.pth' cached_file = os.path.join(model_dir, filename) if not os.path.exists(cached_file): gdown.download(pretrained_urls[key], cached_file, quiet=False) state_dict = torch.load(cached_file) model_dict = model.state_dict() new_state_dict = OrderedDict() matched_layers, discarded_layers = [], [] for k, v in state_dict.items(): if k.startswith('module.'): k = k[7:] # discard module. if k in model_dict and model_dict[k].size() == v.size(): new_state_dict[k] = v matched_layers.append(k) else: discarded_layers.append(k) model_dict.update(new_state_dict) model.load_state_dict(model_dict) if len(matched_layers) == 0: warnings.warn( 'The pretrained weights from "{}" cannot be loaded, ' 'please check the key names manually ' '(** ignored and continue **)'.format(cached_file) ) else: print( 'Successfully loaded imagenet pretrained weights from "{}"'. format(cached_file) ) if len(discarded_layers) > 0: print( '** The following layers are discarded ' 'due to unmatched keys or layer size: {}'. format(discarded_layers) ) ########## # Instantiation ########## def osnet_x1_0(num_classes=1000, pretrained=True, loss='softmax', **kwargs): # standard size (width x1.0) model = OSNet( num_classes, blocks=[OSBlock, OSBlock, OSBlock], layers=[2, 2, 2], channels=[64, 256, 384, 512], loss=loss, **kwargs ) if pretrained: init_pretrained_weights(model, key='osnet_x1_0') return model def osnet_x0_75(num_classes=1000, pretrained=True, loss='softmax', **kwargs): # medium size (width x0.75) model = OSNet( num_classes, blocks=[OSBlock, OSBlock, OSBlock], layers=[2, 2, 2], channels=[48, 192, 288, 384], loss=loss, **kwargs ) if pretrained: init_pretrained_weights(model, key='osnet_x0_75') return model def osnet_x0_5(num_classes=1000, pretrained=True, loss='softmax', **kwargs): # tiny size (width x0.5) model = OSNet( num_classes, blocks=[OSBlock, OSBlock, OSBlock], layers=[2, 2, 2], channels=[32, 128, 192, 256], loss=loss, **kwargs ) if pretrained: init_pretrained_weights(model, key='osnet_x0_5') return model def osnet_x0_25(num_classes=1000, pretrained=True, loss='softmax', **kwargs): # very tiny size (width x0.25) model = OSNet( num_classes, blocks=[OSBlock, OSBlock, OSBlock], layers=[2, 2, 2], channels=[16, 64, 96, 128], loss=loss, **kwargs ) if pretrained: init_pretrained_weights(model, key='osnet_x0_25') return model def osnet_ibn_x1_0( num_classes=1000, pretrained=True, loss='softmax', **kwargs ): # standard size (width x1.0) + IBN layer # Ref: Pan et al. Two at Once: Enhancing Learning and Generalization Capacities via IBN-Net. ECCV, 2018. model = OSNet( num_classes, blocks=[OSBlock, OSBlock, OSBlock], layers=[2, 2, 2], channels=[64, 256, 384, 512], loss=loss, IN=True, **kwargs ) if pretrained: init_pretrained_weights(model, key='osnet_ibn_x1_0') return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/osnet_ain.py ================================================ from __future__ import division, absolute_import import warnings import torch from torch import nn from torch.nn import functional as F __all__ = [ 'osnet_ain_x1_0', 'osnet_ain_x0_75', 'osnet_ain_x0_5', 'osnet_ain_x0_25' ] pretrained_urls = { 'osnet_ain_x1_0': 'https://drive.google.com/uc?id=1-CaioD9NaqbHK_kzSMW8VE4_3KcsRjEo', 'osnet_ain_x0_75': 'https://drive.google.com/uc?id=1apy0hpsMypqstfencdH-jKIUEFOW4xoM', 'osnet_ain_x0_5': 'https://drive.google.com/uc?id=1KusKvEYyKGDTUBVRxRiz55G31wkihB6l', 'osnet_ain_x0_25': 'https://drive.google.com/uc?id=1SxQt2AvmEcgWNhaRb2xC4rP6ZwVDP0Wt' } ########## # Basic layers ########## class ConvLayer(nn.Module): """Convolution layer (conv + bn + relu).""" def __init__( self, in_channels, out_channels, kernel_size, stride=1, padding=0, groups=1, IN=False ): super(ConvLayer, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, kernel_size, stride=stride, padding=padding, bias=False, groups=groups ) if IN: self.bn = nn.InstanceNorm2d(out_channels, affine=True) else: self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU() def forward(self, x): x = self.conv(x) x = self.bn(x) return self.relu(x) class Conv1x1(nn.Module): """1x1 convolution + bn + relu.""" def __init__(self, in_channels, out_channels, stride=1, groups=1): super(Conv1x1, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=stride, padding=0, bias=False, groups=groups ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU() def forward(self, x): x = self.conv(x) x = self.bn(x) return self.relu(x) class Conv1x1Linear(nn.Module): """1x1 convolution + bn (w/o non-linearity).""" def __init__(self, in_channels, out_channels, stride=1, bn=True): super(Conv1x1Linear, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 1, stride=stride, padding=0, bias=False ) self.bn = None if bn: self.bn = nn.BatchNorm2d(out_channels) def forward(self, x): x = self.conv(x) if self.bn is not None: x = self.bn(x) return x class Conv3x3(nn.Module): """3x3 convolution + bn + relu.""" def __init__(self, in_channels, out_channels, stride=1, groups=1): super(Conv3x3, self).__init__() self.conv = nn.Conv2d( in_channels, out_channels, 3, stride=stride, padding=1, bias=False, groups=groups ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU() def forward(self, x): x = self.conv(x) x = self.bn(x) return self.relu(x) class LightConv3x3(nn.Module): """Lightweight 3x3 convolution. 1x1 (linear) + dw 3x3 (nonlinear). """ def __init__(self, in_channels, out_channels): super(LightConv3x3, self).__init__() self.conv1 = nn.Conv2d( in_channels, out_channels, 1, stride=1, padding=0, bias=False ) self.conv2 = nn.Conv2d( out_channels, out_channels, 3, stride=1, padding=1, bias=False, groups=out_channels ) self.bn = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU() def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.bn(x) return self.relu(x) class LightConvStream(nn.Module): """Lightweight convolution stream.""" def __init__(self, in_channels, out_channels, depth): super(LightConvStream, self).__init__() assert depth >= 1, 'depth must be equal to or larger than 1, but got {}'.format( depth ) layers = [] layers += [LightConv3x3(in_channels, out_channels)] for i in range(depth - 1): layers += [LightConv3x3(out_channels, out_channels)] self.layers = nn.Sequential(*layers) def forward(self, x): return self.layers(x) ########## # Building blocks for omni-scale feature learning ########## class ChannelGate(nn.Module): """A mini-network that generates channel-wise gates conditioned on input tensor.""" def __init__( self, in_channels, num_gates=None, return_gates=False, gate_activation='sigmoid', reduction=16, layer_norm=False ): super(ChannelGate, self).__init__() if num_gates is None: num_gates = in_channels self.return_gates = return_gates self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.fc1 = nn.Conv2d( in_channels, in_channels // reduction, kernel_size=1, bias=True, padding=0 ) self.norm1 = None if layer_norm: self.norm1 = nn.LayerNorm((in_channels // reduction, 1, 1)) self.relu = nn.ReLU() self.fc2 = nn.Conv2d( in_channels // reduction, num_gates, kernel_size=1, bias=True, padding=0 ) if gate_activation == 'sigmoid': self.gate_activation = nn.Sigmoid() elif gate_activation == 'relu': self.gate_activation = nn.ReLU() elif gate_activation == 'linear': self.gate_activation = None else: raise RuntimeError( "Unknown gate activation: {}".format(gate_activation) ) def forward(self, x): input = x x = self.global_avgpool(x) x = self.fc1(x) if self.norm1 is not None: x = self.norm1(x) x = self.relu(x) x = self.fc2(x) if self.gate_activation is not None: x = self.gate_activation(x) if self.return_gates: return x return input * x class OSBlock(nn.Module): """Omni-scale feature learning block.""" def __init__(self, in_channels, out_channels, reduction=4, T=4, **kwargs): super(OSBlock, self).__init__() assert T >= 1 assert out_channels >= reduction and out_channels % reduction == 0 mid_channels = out_channels // reduction self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2 = nn.ModuleList() for t in range(1, T + 1): self.conv2 += [LightConvStream(mid_channels, mid_channels, t)] self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) def forward(self, x): identity = x x1 = self.conv1(x) x2 = 0 for conv2_t in self.conv2: x2_t = conv2_t(x1) x2 = x2 + self.gate(x2_t) x3 = self.conv3(x2) if self.downsample is not None: identity = self.downsample(identity) out = x3 + identity return F.relu(out) class OSBlockINin(nn.Module): """Omni-scale feature learning block with instance normalization.""" def __init__(self, in_channels, out_channels, reduction=4, T=4, **kwargs): super(OSBlockINin, self).__init__() assert T >= 1 assert out_channels >= reduction and out_channels % reduction == 0 mid_channels = out_channels // reduction self.conv1 = Conv1x1(in_channels, mid_channels) self.conv2 = nn.ModuleList() for t in range(1, T + 1): self.conv2 += [LightConvStream(mid_channels, mid_channels, t)] self.gate = ChannelGate(mid_channels) self.conv3 = Conv1x1Linear(mid_channels, out_channels, bn=False) self.downsample = None if in_channels != out_channels: self.downsample = Conv1x1Linear(in_channels, out_channels) self.IN = nn.InstanceNorm2d(out_channels, affine=True) def forward(self, x): identity = x x1 = self.conv1(x) x2 = 0 for conv2_t in self.conv2: x2_t = conv2_t(x1) x2 = x2 + self.gate(x2_t) x3 = self.conv3(x2) x3 = self.IN(x3) # IN inside residual if self.downsample is not None: identity = self.downsample(identity) out = x3 + identity return F.relu(out) ########## # Network architecture ########## class OSNet(nn.Module): """Omni-Scale Network. Reference: - Zhou et al. Omni-Scale Feature Learning for Person Re-Identification. ICCV, 2019. - Zhou et al. Learning Generalisable Omni-Scale Representations for Person Re-Identification. TPAMI, 2021. """ def __init__( self, num_classes, blocks, layers, channels, feature_dim=512, loss='softmax', conv1_IN=False, **kwargs ): super(OSNet, self).__init__() num_blocks = len(blocks) assert num_blocks == len(layers) assert num_blocks == len(channels) - 1 self.loss = loss self.feature_dim = feature_dim # convolutional backbone self.conv1 = ConvLayer( 3, channels[0], 7, stride=2, padding=3, IN=conv1_IN ) self.maxpool = nn.MaxPool2d(3, stride=2, padding=1) self.conv2 = self._make_layer( blocks[0], layers[0], channels[0], channels[1] ) self.pool2 = nn.Sequential( Conv1x1(channels[1], channels[1]), nn.AvgPool2d(2, stride=2) ) self.conv3 = self._make_layer( blocks[1], layers[1], channels[1], channels[2] ) self.pool3 = nn.Sequential( Conv1x1(channels[2], channels[2]), nn.AvgPool2d(2, stride=2) ) self.conv4 = self._make_layer( blocks[2], layers[2], channels[2], channels[3] ) self.conv5 = Conv1x1(channels[3], channels[3]) self.global_avgpool = nn.AdaptiveAvgPool2d(1) # fully connected layer self.fc = self._construct_fc_layer( self.feature_dim, channels[3], dropout_p=None ) # identity classification layer self.classifier = nn.Linear(self.feature_dim, num_classes) self._init_params() def _make_layer(self, blocks, layer, in_channels, out_channels): layers = [] layers += [blocks[0](in_channels, out_channels)] for i in range(1, len(blocks)): layers += [blocks[i](out_channels, out_channels)] return nn.Sequential(*layers) def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): if fc_dims is None or fc_dims < 0: self.feature_dim = input_dim return None if isinstance(fc_dims, int): fc_dims = [fc_dims] layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU()) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def _init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.InstanceNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def featuremaps(self, x): x = self.conv1(x) x = self.maxpool(x) x = self.conv2(x) x = self.pool2(x) x = self.conv3(x) x = self.pool3(x) x = self.conv4(x) x = self.conv5(x) return x def forward(self, x, return_featuremaps=False): x = self.featuremaps(x) if return_featuremaps: return x v = self.global_avgpool(x) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError("Unsupported loss: {}".format(self.loss)) def init_pretrained_weights(model, key=''): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ import os import errno import gdown from collections import OrderedDict def _get_torch_home(): ENV_TORCH_HOME = 'TORCH_HOME' ENV_XDG_CACHE_HOME = 'XDG_CACHE_HOME' DEFAULT_CACHE_DIR = '~/.cache' torch_home = os.path.expanduser( os.getenv( ENV_TORCH_HOME, os.path.join( os.getenv(ENV_XDG_CACHE_HOME, DEFAULT_CACHE_DIR), 'torch' ) ) ) return torch_home torch_home = _get_torch_home() model_dir = os.path.join(torch_home, 'checkpoints') try: os.makedirs(model_dir) except OSError as e: if e.errno == errno.EEXIST: # Directory already exists, ignore. pass else: # Unexpected OSError, re-raise. raise filename = key + '_imagenet.pth' cached_file = os.path.join(model_dir, filename) if not os.path.exists(cached_file): gdown.download(pretrained_urls[key], cached_file, quiet=False) state_dict = torch.load(cached_file) model_dict = model.state_dict() new_state_dict = OrderedDict() matched_layers, discarded_layers = [], [] for k, v in state_dict.items(): if k.startswith('module.'): k = k[7:] # discard module. if k in model_dict and model_dict[k].size() == v.size(): new_state_dict[k] = v matched_layers.append(k) else: discarded_layers.append(k) model_dict.update(new_state_dict) model.load_state_dict(model_dict) if len(matched_layers) == 0: warnings.warn( 'The pretrained weights from "{}" cannot be loaded, ' 'please check the key names manually ' '(** ignored and continue **)'.format(cached_file) ) else: print( 'Successfully loaded imagenet pretrained weights from "{}"'. format(cached_file) ) if len(discarded_layers) > 0: print( '** The following layers are discarded ' 'due to unmatched keys or layer size: {}'. format(discarded_layers) ) ########## # Instantiation ########## def osnet_ain_x1_0( num_classes=1000, pretrained=True, loss='softmax', **kwargs ): model = OSNet( num_classes, blocks=[ [OSBlockINin, OSBlockINin], [OSBlock, OSBlockINin], [OSBlockINin, OSBlock] ], layers=[2, 2, 2], channels=[64, 256, 384, 512], loss=loss, conv1_IN=True, **kwargs ) if pretrained: init_pretrained_weights(model, key='osnet_ain_x1_0') return model def osnet_ain_x0_75( num_classes=1000, pretrained=True, loss='softmax', **kwargs ): model = OSNet( num_classes, blocks=[ [OSBlockINin, OSBlockINin], [OSBlock, OSBlockINin], [OSBlockINin, OSBlock] ], layers=[2, 2, 2], channels=[48, 192, 288, 384], loss=loss, conv1_IN=True, **kwargs ) if pretrained: init_pretrained_weights(model, key='osnet_ain_x0_75') return model def osnet_ain_x0_5( num_classes=1000, pretrained=True, loss='softmax', **kwargs ): model = OSNet( num_classes, blocks=[ [OSBlockINin, OSBlockINin], [OSBlock, OSBlockINin], [OSBlockINin, OSBlock] ], layers=[2, 2, 2], channels=[32, 128, 192, 256], loss=loss, conv1_IN=True, **kwargs ) if pretrained: init_pretrained_weights(model, key='osnet_ain_x0_5') return model def osnet_ain_x0_25( num_classes=1000, pretrained=True, loss='softmax', **kwargs ): model = OSNet( num_classes, blocks=[ [OSBlockINin, OSBlockINin], [OSBlock, OSBlockINin], [OSBlockINin, OSBlock] ], layers=[2, 2, 2], channels=[16, 64, 96, 128], loss=loss, conv1_IN=True, **kwargs ) if pretrained: init_pretrained_weights(model, key='osnet_ain_x0_25') return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/pcb.py ================================================ from __future__ import division, absolute_import import torch.utils.model_zoo as model_zoo from torch import nn from torch.nn import functional as F __all__ = ['pcb_p6', 'pcb_p4'] model_urls = { 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth', 'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth', 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth', 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth', 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth', } def conv3x3(in_planes, out_planes, stride=1): """3x3 convolution with padding""" return nn.Conv2d( in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False ) class BasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = nn.BatchNorm2d(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out class Bottleneck(nn.Module): expansion = 4 def __init__(self, inplanes, planes, stride=1, downsample=None): super(Bottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d( planes, planes, kernel_size=3, stride=stride, padding=1, bias=False ) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = nn.Conv2d( planes, planes * self.expansion, kernel_size=1, bias=False ) self.bn3 = nn.BatchNorm2d(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out class DimReduceLayer(nn.Module): def __init__(self, in_channels, out_channels, nonlinear): super(DimReduceLayer, self).__init__() layers = [] layers.append( nn.Conv2d( in_channels, out_channels, 1, stride=1, padding=0, bias=False ) ) layers.append(nn.BatchNorm2d(out_channels)) if nonlinear == 'relu': layers.append(nn.ReLU(inplace=True)) elif nonlinear == 'leakyrelu': layers.append(nn.LeakyReLU(0.1)) self.layers = nn.Sequential(*layers) def forward(self, x): return self.layers(x) class PCB(nn.Module): """Part-based Convolutional Baseline. Reference: Sun et al. Beyond Part Models: Person Retrieval with Refined Part Pooling (and A Strong Convolutional Baseline). ECCV 2018. Public keys: - ``pcb_p4``: PCB with 4-part strips. - ``pcb_p6``: PCB with 6-part strips. """ def __init__( self, num_classes, loss, block, layers, parts=6, reduced_dim=256, nonlinear='relu', **kwargs ): self.inplanes = 64 super(PCB, self).__init__() self.loss = loss self.parts = parts self.feature_dim = 512 * block.expansion # backbone network self.conv1 = nn.Conv2d( 3, 64, kernel_size=7, stride=2, padding=3, bias=False ) self.bn1 = nn.BatchNorm2d(64) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0]) self.layer2 = self._make_layer(block, 128, layers[1], stride=2) self.layer3 = self._make_layer(block, 256, layers[2], stride=2) self.layer4 = self._make_layer(block, 512, layers[3], stride=1) # pcb layers self.parts_avgpool = nn.AdaptiveAvgPool2d((self.parts, 1)) self.dropout = nn.Dropout(p=0.5) self.conv5 = DimReduceLayer( 512 * block.expansion, reduced_dim, nonlinear=nonlinear ) self.feature_dim = reduced_dim self.classifier = nn.ModuleList( [ nn.Linear(self.feature_dim, num_classes) for _ in range(self.parts) ] ) self._init_params() def _make_layer(self, block, planes, blocks, stride=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d( self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False ), nn.BatchNorm2d(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample)) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append(block(self.inplanes, planes)) return nn.Sequential(*layers) def _init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def featuremaps(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) return x def forward(self, x): f = self.featuremaps(x) v_g = self.parts_avgpool(f) if not self.training: v_g = F.normalize(v_g, p=2, dim=1) return v_g.view(v_g.size(0), -1) v_g = self.dropout(v_g) v_h = self.conv5(v_g) y = [] for i in range(self.parts): v_h_i = v_h[:, :, i, :] v_h_i = v_h_i.view(v_h_i.size(0), -1) y_i = self.classifier[i](v_h_i) y.append(y_i) if self.loss == 'softmax': return y elif self.loss == 'triplet': v_g = F.normalize(v_g, p=2, dim=1) return y, v_g.view(v_g.size(0), -1) else: raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def pcb_p6(num_classes, loss='softmax', pretrained=True, **kwargs): model = PCB( num_classes=num_classes, loss=loss, block=Bottleneck, layers=[3, 4, 6, 3], last_stride=1, parts=6, reduced_dim=256, nonlinear='relu', **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnet50']) return model def pcb_p4(num_classes, loss='softmax', pretrained=True, **kwargs): model = PCB( num_classes=num_classes, loss=loss, block=Bottleneck, layers=[3, 4, 6, 3], last_stride=1, parts=4, reduced_dim=256, nonlinear='relu', **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnet50']) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/resnet.py ================================================ """ Code source: https://github.com/pytorch/vision """ from __future__ import division, absolute_import import torch.utils.model_zoo as model_zoo from torch import nn __all__ = [ 'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152', 'resnext50_32x4d', 'resnext101_32x8d', 'resnet50_fc512' ] model_urls = { 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth', 'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth', 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth', 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth', 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth', 'resnext50_32x4d': 'https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth', 'resnext101_32x8d': 'https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth', } def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1): """3x3 convolution with padding""" return nn.Conv2d( in_planes, out_planes, kernel_size=3, stride=stride, padding=dilation, groups=groups, bias=False, dilation=dilation ) def conv1x1(in_planes, out_planes, stride=1): """1x1 convolution""" return nn.Conv2d( in_planes, out_planes, kernel_size=1, stride=stride, bias=False ) class BasicBlock(nn.Module): expansion = 1 def __init__( self, inplanes, planes, stride=1, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=None ): super(BasicBlock, self).__init__() if norm_layer is None: norm_layer = nn.BatchNorm2d if groups != 1 or base_width != 64: raise ValueError( 'BasicBlock only supports groups=1 and base_width=64' ) if dilation > 1: raise NotImplementedError( "Dilation > 1 not supported in BasicBlock" ) # Both self.conv1 and self.downsample layers downsample the input when stride != 1 self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = norm_layer(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = norm_layer(planes) self.downsample = downsample self.stride = stride def forward(self, x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return out class Bottleneck(nn.Module): expansion = 4 def __init__( self, inplanes, planes, stride=1, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=None ): super(Bottleneck, self).__init__() if norm_layer is None: norm_layer = nn.BatchNorm2d width = int(planes * (base_width/64.)) * groups # Both self.conv2 and self.downsample layers downsample the input when stride != 1 self.conv1 = conv1x1(inplanes, width) self.bn1 = norm_layer(width) self.conv2 = conv3x3(width, width, stride, groups, dilation) self.bn2 = norm_layer(width) self.conv3 = conv1x1(width, planes * self.expansion) self.bn3 = norm_layer(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return out class ResNet(nn.Module): """Residual network. Reference: - He et al. Deep Residual Learning for Image Recognition. CVPR 2016. - Xie et al. Aggregated Residual Transformations for Deep Neural Networks. CVPR 2017. Public keys: - ``resnet18``: ResNet18. - ``resnet34``: ResNet34. - ``resnet50``: ResNet50. - ``resnet101``: ResNet101. - ``resnet152``: ResNet152. - ``resnext50_32x4d``: ResNeXt50. - ``resnext101_32x8d``: ResNeXt101. - ``resnet50_fc512``: ResNet50 + FC. """ def __init__( self, num_classes, loss, block, layers, zero_init_residual=False, groups=1, width_per_group=64, replace_stride_with_dilation=None, norm_layer=None, last_stride=2, fc_dims=None, dropout_p=None, **kwargs ): super(ResNet, self).__init__() if norm_layer is None: norm_layer = nn.BatchNorm2d self._norm_layer = norm_layer self.loss = loss self.feature_dim = 512 * block.expansion self.inplanes = 64 self.dilation = 1 if replace_stride_with_dilation is None: # each element in the tuple indicates if we should replace # the 2x2 stride with a dilated convolution instead replace_stride_with_dilation = [False, False, False] if len(replace_stride_with_dilation) != 3: raise ValueError( "replace_stride_with_dilation should be None " "or a 3-element tuple, got {}". format(replace_stride_with_dilation) ) self.groups = groups self.base_width = width_per_group self.conv1 = nn.Conv2d( 3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False ) self.bn1 = norm_layer(self.inplanes) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0]) self.layer2 = self._make_layer( block, 128, layers[1], stride=2, dilate=replace_stride_with_dilation[0] ) self.layer3 = self._make_layer( block, 256, layers[2], stride=2, dilate=replace_stride_with_dilation[1] ) self.layer4 = self._make_layer( block, 512, layers[3], stride=last_stride, dilate=replace_stride_with_dilation[2] ) self.global_avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.fc = self._construct_fc_layer( fc_dims, 512 * block.expansion, dropout_p ) self.classifier = nn.Linear(self.feature_dim, num_classes) self._init_params() # Zero-initialize the last BN in each residual branch, # so that the residual branch starts with zeros, and each residual block behaves like an identity. # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677 if zero_init_residual: for m in self.modules(): if isinstance(m, Bottleneck): nn.init.constant_(m.bn3.weight, 0) elif isinstance(m, BasicBlock): nn.init.constant_(m.bn2.weight, 0) def _make_layer(self, block, planes, blocks, stride=1, dilate=False): norm_layer = self._norm_layer downsample = None previous_dilation = self.dilation if dilate: self.dilation *= stride stride = 1 if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( conv1x1(self.inplanes, planes * block.expansion, stride), norm_layer(planes * block.expansion), ) layers = [] layers.append( block( self.inplanes, planes, stride, downsample, self.groups, self.base_width, previous_dilation, norm_layer ) ) self.inplanes = planes * block.expansion for _ in range(1, blocks): layers.append( block( self.inplanes, planes, groups=self.groups, base_width=self.base_width, dilation=self.dilation, norm_layer=norm_layer ) ) return nn.Sequential(*layers) def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): """Constructs fully connected layer Args: fc_dims (list or tuple): dimensions of fc layers, if None, no fc layers are constructed input_dim (int): input dimension dropout_p (float): dropout probability, if None, dropout is unused """ if fc_dims is None: self.feature_dim = input_dim return None assert isinstance( fc_dims, (list, tuple) ), 'fc_dims must be either list or tuple, but got {}'.format( type(fc_dims) ) layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def _init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def featuremaps(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) return x def forward(self, x): f = self.featuremaps(x) v = self.global_avgpool(f) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError("Unsupported loss: {}".format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) """ResNet""" def resnet18(num_classes, loss='softmax', pretrained=True, **kwargs): model = ResNet( num_classes=num_classes, loss=loss, block=BasicBlock, layers=[2, 2, 2, 2], last_stride=2, fc_dims=None, dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnet18']) return model def resnet34(num_classes, loss='softmax', pretrained=True, **kwargs): model = ResNet( num_classes=num_classes, loss=loss, block=BasicBlock, layers=[3, 4, 6, 3], last_stride=2, fc_dims=None, dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnet34']) return model def resnet50(num_classes, loss='softmax', pretrained=True, **kwargs): model = ResNet( num_classes=num_classes, loss=loss, block=Bottleneck, layers=[3, 4, 6, 3], last_stride=2, fc_dims=None, dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnet50']) return model def resnet101(num_classes, loss='softmax', pretrained=True, **kwargs): model = ResNet( num_classes=num_classes, loss=loss, block=Bottleneck, layers=[3, 4, 23, 3], last_stride=2, fc_dims=None, dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnet101']) return model def resnet152(num_classes, loss='softmax', pretrained=True, **kwargs): model = ResNet( num_classes=num_classes, loss=loss, block=Bottleneck, layers=[3, 8, 36, 3], last_stride=2, fc_dims=None, dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnet152']) return model """ResNeXt""" def resnext50_32x4d(num_classes, loss='softmax', pretrained=True, **kwargs): model = ResNet( num_classes=num_classes, loss=loss, block=Bottleneck, layers=[3, 4, 6, 3], last_stride=2, fc_dims=None, dropout_p=None, groups=32, width_per_group=4, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnext50_32x4d']) return model def resnext101_32x8d(num_classes, loss='softmax', pretrained=True, **kwargs): model = ResNet( num_classes=num_classes, loss=loss, block=Bottleneck, layers=[3, 4, 23, 3], last_stride=2, fc_dims=None, dropout_p=None, groups=32, width_per_group=8, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnext101_32x8d']) return model """ ResNet + FC """ def resnet50_fc512(num_classes, loss='softmax', pretrained=True, **kwargs): model = ResNet( num_classes=num_classes, loss=loss, block=Bottleneck, layers=[3, 4, 6, 3], last_stride=1, fc_dims=[512], dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnet50']) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/resnet_ibn_a.py ================================================ """ Credit to https://github.com/XingangPan/IBN-Net. """ from __future__ import division, absolute_import import math import torch import torch.nn as nn import torch.utils.model_zoo as model_zoo __all__ = ['resnet50_ibn_a'] model_urls = { 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth', 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth', 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth', } def conv3x3(in_planes, out_planes, stride=1): "3x3 convolution with padding" return nn.Conv2d( in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False ) class BasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = nn.BatchNorm2d(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out class IBN(nn.Module): def __init__(self, planes): super(IBN, self).__init__() half1 = int(planes / 2) self.half = half1 half2 = planes - half1 self.IN = nn.InstanceNorm2d(half1, affine=True) self.BN = nn.BatchNorm2d(half2) def forward(self, x): split = torch.split(x, self.half, 1) out1 = self.IN(split[0].contiguous()) out2 = self.BN(split[1].contiguous()) out = torch.cat((out1, out2), 1) return out class Bottleneck(nn.Module): expansion = 4 def __init__(self, inplanes, planes, ibn=False, stride=1, downsample=None): super(Bottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) if ibn: self.bn1 = IBN(planes) else: self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d( planes, planes, kernel_size=3, stride=stride, padding=1, bias=False ) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = nn.Conv2d( planes, planes * self.expansion, kernel_size=1, bias=False ) self.bn3 = nn.BatchNorm2d(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out class ResNet(nn.Module): """Residual network + IBN layer. Reference: - He et al. Deep Residual Learning for Image Recognition. CVPR 2016. - Pan et al. Two at Once: Enhancing Learning and Generalization Capacities via IBN-Net. ECCV 2018. """ def __init__( self, block, layers, num_classes=1000, loss='softmax', fc_dims=None, dropout_p=None, **kwargs ): scale = 64 self.inplanes = scale super(ResNet, self).__init__() self.loss = loss self.feature_dim = scale * 8 * block.expansion self.conv1 = nn.Conv2d( 3, scale, kernel_size=7, stride=2, padding=3, bias=False ) self.bn1 = nn.BatchNorm2d(scale) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, scale, layers[0]) self.layer2 = self._make_layer(block, scale * 2, layers[1], stride=2) self.layer3 = self._make_layer(block, scale * 4, layers[2], stride=2) self.layer4 = self._make_layer(block, scale * 8, layers[3], stride=2) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.fc = self._construct_fc_layer( fc_dims, scale * 8 * block.expansion, dropout_p ) self.classifier = nn.Linear(self.feature_dim, num_classes) for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() elif isinstance(m, nn.InstanceNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() def _make_layer(self, block, planes, blocks, stride=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d( self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False ), nn.BatchNorm2d(planes * block.expansion), ) layers = [] ibn = True if planes == 512: ibn = False layers.append(block(self.inplanes, planes, ibn, stride, downsample)) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append(block(self.inplanes, planes, ibn)) return nn.Sequential(*layers) def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): """Constructs fully connected layer Args: fc_dims (list or tuple): dimensions of fc layers, if None, no fc layers are constructed input_dim (int): input dimension dropout_p (float): dropout probability, if None, dropout is unused """ if fc_dims is None: self.feature_dim = input_dim return None assert isinstance( fc_dims, (list, tuple) ), 'fc_dims must be either list or tuple, but got {}'.format( type(fc_dims) ) layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def featuremaps(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) return x def forward(self, x): f = self.featuremaps(x) v = self.avgpool(f) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError("Unsupported loss: {}".format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def resnet50_ibn_a(num_classes, loss='softmax', pretrained=False, **kwargs): model = ResNet( Bottleneck, [3, 4, 6, 3], num_classes=num_classes, loss=loss, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnet50']) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/resnet_ibn_b.py ================================================ """ Credit to https://github.com/XingangPan/IBN-Net. """ from __future__ import division, absolute_import import math import torch.nn as nn import torch.utils.model_zoo as model_zoo __all__ = ['resnet50_ibn_b'] model_urls = { 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth', 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth', 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth', } def conv3x3(in_planes, out_planes, stride=1): "3x3 convolution with padding" return nn.Conv2d( in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False ) class BasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = nn.BatchNorm2d(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out class Bottleneck(nn.Module): expansion = 4 def __init__(self, inplanes, planes, stride=1, downsample=None, IN=False): super(Bottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d( planes, planes, kernel_size=3, stride=stride, padding=1, bias=False ) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = nn.Conv2d( planes, planes * self.expansion, kernel_size=1, bias=False ) self.bn3 = nn.BatchNorm2d(planes * self.expansion) self.IN = None if IN: self.IN = nn.InstanceNorm2d(planes * 4, affine=True) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: residual = self.downsample(x) out += residual if self.IN is not None: out = self.IN(out) out = self.relu(out) return out class ResNet(nn.Module): """Residual network + IBN layer. Reference: - He et al. Deep Residual Learning for Image Recognition. CVPR 2016. - Pan et al. Two at Once: Enhancing Learning and Generalization Capacities via IBN-Net. ECCV 2018. """ def __init__( self, block, layers, num_classes=1000, loss='softmax', fc_dims=None, dropout_p=None, **kwargs ): scale = 64 self.inplanes = scale super(ResNet, self).__init__() self.loss = loss self.feature_dim = scale * 8 * block.expansion self.conv1 = nn.Conv2d( 3, scale, kernel_size=7, stride=2, padding=3, bias=False ) self.bn1 = nn.InstanceNorm2d(scale, affine=True) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer( block, scale, layers[0], stride=1, IN=True ) self.layer2 = self._make_layer( block, scale * 2, layers[1], stride=2, IN=True ) self.layer3 = self._make_layer(block, scale * 4, layers[2], stride=2) self.layer4 = self._make_layer(block, scale * 8, layers[3], stride=2) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.fc = self._construct_fc_layer( fc_dims, scale * 8 * block.expansion, dropout_p ) self.classifier = nn.Linear(self.feature_dim, num_classes) for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() elif isinstance(m, nn.InstanceNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() def _make_layer(self, block, planes, blocks, stride=1, IN=False): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d( self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False ), nn.BatchNorm2d(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample)) self.inplanes = planes * block.expansion for i in range(1, blocks - 1): layers.append(block(self.inplanes, planes)) layers.append(block(self.inplanes, planes, IN=IN)) return nn.Sequential(*layers) def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): """Constructs fully connected layer Args: fc_dims (list or tuple): dimensions of fc layers, if None, no fc layers are constructed input_dim (int): input dimension dropout_p (float): dropout probability, if None, dropout is unused """ if fc_dims is None: self.feature_dim = input_dim return None assert isinstance( fc_dims, (list, tuple) ), 'fc_dims must be either list or tuple, but got {}'.format( type(fc_dims) ) layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def featuremaps(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) return x def forward(self, x): f = self.featuremaps(x) v = self.avgpool(f) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError("Unsupported loss: {}".format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def resnet50_ibn_b(num_classes, loss='softmax', pretrained=False, **kwargs): model = ResNet( Bottleneck, [3, 4, 6, 3], num_classes=num_classes, loss=loss, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnet50']) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/resnetmid.py ================================================ from __future__ import division, absolute_import import torch import torch.utils.model_zoo as model_zoo from torch import nn __all__ = ['resnet50mid'] model_urls = { 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth', 'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth', 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth', 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth', 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth', } def conv3x3(in_planes, out_planes, stride=1): """3x3 convolution with padding""" return nn.Conv2d( in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False ) class BasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = nn.BatchNorm2d(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out class Bottleneck(nn.Module): expansion = 4 def __init__(self, inplanes, planes, stride=1, downsample=None): super(Bottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d( planes, planes, kernel_size=3, stride=stride, padding=1, bias=False ) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = nn.Conv2d( planes, planes * self.expansion, kernel_size=1, bias=False ) self.bn3 = nn.BatchNorm2d(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out class ResNetMid(nn.Module): """Residual network + mid-level features. Reference: Yu et al. The Devil is in the Middle: Exploiting Mid-level Representations for Cross-Domain Instance Matching. arXiv:1711.08106. Public keys: - ``resnet50mid``: ResNet50 + mid-level feature fusion. """ def __init__( self, num_classes, loss, block, layers, last_stride=2, fc_dims=None, **kwargs ): self.inplanes = 64 super(ResNetMid, self).__init__() self.loss = loss self.feature_dim = 512 * block.expansion # backbone network self.conv1 = nn.Conv2d( 3, 64, kernel_size=7, stride=2, padding=3, bias=False ) self.bn1 = nn.BatchNorm2d(64) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0]) self.layer2 = self._make_layer(block, 128, layers[1], stride=2) self.layer3 = self._make_layer(block, 256, layers[2], stride=2) self.layer4 = self._make_layer( block, 512, layers[3], stride=last_stride ) self.global_avgpool = nn.AdaptiveAvgPool2d(1) assert fc_dims is not None self.fc_fusion = self._construct_fc_layer( fc_dims, 512 * block.expansion * 2 ) self.feature_dim += 512 * block.expansion self.classifier = nn.Linear(self.feature_dim, num_classes) self._init_params() def _make_layer(self, block, planes, blocks, stride=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d( self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False ), nn.BatchNorm2d(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample)) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append(block(self.inplanes, planes)) return nn.Sequential(*layers) def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): """Constructs fully connected layer Args: fc_dims (list or tuple): dimensions of fc layers, if None, no fc layers are constructed input_dim (int): input dimension dropout_p (float): dropout probability, if None, dropout is unused """ if fc_dims is None: self.feature_dim = input_dim return None assert isinstance( fc_dims, (list, tuple) ), 'fc_dims must be either list or tuple, but got {}'.format( type(fc_dims) ) layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def _init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def featuremaps(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x4a = self.layer4[0](x) x4b = self.layer4[1](x4a) x4c = self.layer4[2](x4b) return x4a, x4b, x4c def forward(self, x): x4a, x4b, x4c = self.featuremaps(x) v4a = self.global_avgpool(x4a) v4b = self.global_avgpool(x4b) v4c = self.global_avgpool(x4c) v4ab = torch.cat([v4a, v4b], 1) v4ab = v4ab.view(v4ab.size(0), -1) v4ab = self.fc_fusion(v4ab) v4c = v4c.view(v4c.size(0), -1) v = torch.cat([v4ab, v4c], 1) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) """ Residual network configurations: -- resnet18: block=BasicBlock, layers=[2, 2, 2, 2] resnet34: block=BasicBlock, layers=[3, 4, 6, 3] resnet50: block=Bottleneck, layers=[3, 4, 6, 3] resnet101: block=Bottleneck, layers=[3, 4, 23, 3] resnet152: block=Bottleneck, layers=[3, 8, 36, 3] """ def resnet50mid(num_classes, loss='softmax', pretrained=True, **kwargs): model = ResNetMid( num_classes=num_classes, loss=loss, block=Bottleneck, layers=[3, 4, 6, 3], last_stride=2, fc_dims=[1024], **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['resnet50']) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/senet.py ================================================ from __future__ import division, absolute_import import math from collections import OrderedDict import torch.nn as nn from torch.utils import model_zoo __all__ = [ 'senet154', 'se_resnet50', 'se_resnet101', 'se_resnet152', 'se_resnext50_32x4d', 'se_resnext101_32x4d', 'se_resnet50_fc512' ] """ Code imported from https://github.com/Cadene/pretrained-models.pytorch """ pretrained_settings = { 'senet154': { 'imagenet': { 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/senet154-c7b49a05.pth', 'input_space': 'RGB', 'input_size': [3, 224, 224], 'input_range': [0, 1], 'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225], 'num_classes': 1000 } }, 'se_resnet50': { 'imagenet': { 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet50-ce0d4300.pth', 'input_space': 'RGB', 'input_size': [3, 224, 224], 'input_range': [0, 1], 'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225], 'num_classes': 1000 } }, 'se_resnet101': { 'imagenet': { 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet101-7e38fcc6.pth', 'input_space': 'RGB', 'input_size': [3, 224, 224], 'input_range': [0, 1], 'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225], 'num_classes': 1000 } }, 'se_resnet152': { 'imagenet': { 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet152-d17c99b7.pth', 'input_space': 'RGB', 'input_size': [3, 224, 224], 'input_range': [0, 1], 'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225], 'num_classes': 1000 } }, 'se_resnext50_32x4d': { 'imagenet': { 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnext50_32x4d-a260b3a4.pth', 'input_space': 'RGB', 'input_size': [3, 224, 224], 'input_range': [0, 1], 'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225], 'num_classes': 1000 } }, 'se_resnext101_32x4d': { 'imagenet': { 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnext101_32x4d-3b2fe3d8.pth', 'input_space': 'RGB', 'input_size': [3, 224, 224], 'input_range': [0, 1], 'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225], 'num_classes': 1000 } }, } class SEModule(nn.Module): def __init__(self, channels, reduction): super(SEModule, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.fc1 = nn.Conv2d( channels, channels // reduction, kernel_size=1, padding=0 ) self.relu = nn.ReLU(inplace=True) self.fc2 = nn.Conv2d( channels // reduction, channels, kernel_size=1, padding=0 ) self.sigmoid = nn.Sigmoid() def forward(self, x): module_input = x x = self.avg_pool(x) x = self.fc1(x) x = self.relu(x) x = self.fc2(x) x = self.sigmoid(x) return module_input * x class Bottleneck(nn.Module): """ Base class for bottlenecks that implements `forward()` method. """ def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: residual = self.downsample(x) out = self.se_module(out) + residual out = self.relu(out) return out class SEBottleneck(Bottleneck): """ Bottleneck for SENet154. """ expansion = 4 def __init__( self, inplanes, planes, groups, reduction, stride=1, downsample=None ): super(SEBottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes * 2, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes * 2) self.conv2 = nn.Conv2d( planes * 2, planes * 4, kernel_size=3, stride=stride, padding=1, groups=groups, bias=False ) self.bn2 = nn.BatchNorm2d(planes * 4) self.conv3 = nn.Conv2d( planes * 4, planes * 4, kernel_size=1, bias=False ) self.bn3 = nn.BatchNorm2d(planes * 4) self.relu = nn.ReLU(inplace=True) self.se_module = SEModule(planes * 4, reduction=reduction) self.downsample = downsample self.stride = stride class SEResNetBottleneck(Bottleneck): """ ResNet bottleneck with a Squeeze-and-Excitation module. It follows Caffe implementation and uses `stride=stride` in `conv1` and not in `conv2` (the latter is used in the torchvision implementation of ResNet). """ expansion = 4 def __init__( self, inplanes, planes, groups, reduction, stride=1, downsample=None ): super(SEResNetBottleneck, self).__init__() self.conv1 = nn.Conv2d( inplanes, planes, kernel_size=1, bias=False, stride=stride ) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d( planes, planes, kernel_size=3, padding=1, groups=groups, bias=False ) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(planes * 4) self.relu = nn.ReLU(inplace=True) self.se_module = SEModule(planes * 4, reduction=reduction) self.downsample = downsample self.stride = stride class SEResNeXtBottleneck(Bottleneck): """ResNeXt bottleneck type C with a Squeeze-and-Excitation module""" expansion = 4 def __init__( self, inplanes, planes, groups, reduction, stride=1, downsample=None, base_width=4 ): super(SEResNeXtBottleneck, self).__init__() width = int(math.floor(planes * (base_width/64.)) * groups) self.conv1 = nn.Conv2d( inplanes, width, kernel_size=1, bias=False, stride=1 ) self.bn1 = nn.BatchNorm2d(width) self.conv2 = nn.Conv2d( width, width, kernel_size=3, stride=stride, padding=1, groups=groups, bias=False ) self.bn2 = nn.BatchNorm2d(width) self.conv3 = nn.Conv2d(width, planes * 4, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(planes * 4) self.relu = nn.ReLU(inplace=True) self.se_module = SEModule(planes * 4, reduction=reduction) self.downsample = downsample self.stride = stride class SENet(nn.Module): """Squeeze-and-excitation network. Reference: Hu et al. Squeeze-and-Excitation Networks. CVPR 2018. Public keys: - ``senet154``: SENet154. - ``se_resnet50``: ResNet50 + SE. - ``se_resnet101``: ResNet101 + SE. - ``se_resnet152``: ResNet152 + SE. - ``se_resnext50_32x4d``: ResNeXt50 (groups=32, width=4) + SE. - ``se_resnext101_32x4d``: ResNeXt101 (groups=32, width=4) + SE. - ``se_resnet50_fc512``: (ResNet50 + SE) + FC. """ def __init__( self, num_classes, loss, block, layers, groups, reduction, dropout_p=0.2, inplanes=128, input_3x3=True, downsample_kernel_size=3, downsample_padding=1, last_stride=2, fc_dims=None, **kwargs ): """ Parameters ---------- block (nn.Module): Bottleneck class. - For SENet154: SEBottleneck - For SE-ResNet models: SEResNetBottleneck - For SE-ResNeXt models: SEResNeXtBottleneck layers (list of ints): Number of residual blocks for 4 layers of the network (layer1...layer4). groups (int): Number of groups for the 3x3 convolution in each bottleneck block. - For SENet154: 64 - For SE-ResNet models: 1 - For SE-ResNeXt models: 32 reduction (int): Reduction ratio for Squeeze-and-Excitation modules. - For all models: 16 dropout_p (float or None): Drop probability for the Dropout layer. If `None` the Dropout layer is not used. - For SENet154: 0.2 - For SE-ResNet models: None - For SE-ResNeXt models: None inplanes (int): Number of input channels for layer1. - For SENet154: 128 - For SE-ResNet models: 64 - For SE-ResNeXt models: 64 input_3x3 (bool): If `True`, use three 3x3 convolutions instead of a single 7x7 convolution in layer0. - For SENet154: True - For SE-ResNet models: False - For SE-ResNeXt models: False downsample_kernel_size (int): Kernel size for downsampling convolutions in layer2, layer3 and layer4. - For SENet154: 3 - For SE-ResNet models: 1 - For SE-ResNeXt models: 1 downsample_padding (int): Padding for downsampling convolutions in layer2, layer3 and layer4. - For SENet154: 1 - For SE-ResNet models: 0 - For SE-ResNeXt models: 0 num_classes (int): Number of outputs in `classifier` layer. """ super(SENet, self).__init__() self.inplanes = inplanes self.loss = loss if input_3x3: layer0_modules = [ ( 'conv1', nn.Conv2d(3, 64, 3, stride=2, padding=1, bias=False) ), ('bn1', nn.BatchNorm2d(64)), ('relu1', nn.ReLU(inplace=True)), ( 'conv2', nn.Conv2d(64, 64, 3, stride=1, padding=1, bias=False) ), ('bn2', nn.BatchNorm2d(64)), ('relu2', nn.ReLU(inplace=True)), ( 'conv3', nn.Conv2d( 64, inplanes, 3, stride=1, padding=1, bias=False ) ), ('bn3', nn.BatchNorm2d(inplanes)), ('relu3', nn.ReLU(inplace=True)), ] else: layer0_modules = [ ( 'conv1', nn.Conv2d( 3, inplanes, kernel_size=7, stride=2, padding=3, bias=False ) ), ('bn1', nn.BatchNorm2d(inplanes)), ('relu1', nn.ReLU(inplace=True)), ] # To preserve compatibility with Caffe weights `ceil_mode=True` # is used instead of `padding=1`. layer0_modules.append( ('pool', nn.MaxPool2d(3, stride=2, ceil_mode=True)) ) self.layer0 = nn.Sequential(OrderedDict(layer0_modules)) self.layer1 = self._make_layer( block, planes=64, blocks=layers[0], groups=groups, reduction=reduction, downsample_kernel_size=1, downsample_padding=0 ) self.layer2 = self._make_layer( block, planes=128, blocks=layers[1], stride=2, groups=groups, reduction=reduction, downsample_kernel_size=downsample_kernel_size, downsample_padding=downsample_padding ) self.layer3 = self._make_layer( block, planes=256, blocks=layers[2], stride=2, groups=groups, reduction=reduction, downsample_kernel_size=downsample_kernel_size, downsample_padding=downsample_padding ) self.layer4 = self._make_layer( block, planes=512, blocks=layers[3], stride=last_stride, groups=groups, reduction=reduction, downsample_kernel_size=downsample_kernel_size, downsample_padding=downsample_padding ) self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.fc = self._construct_fc_layer( fc_dims, 512 * block.expansion, dropout_p ) self.classifier = nn.Linear(self.feature_dim, num_classes) def _make_layer( self, block, planes, blocks, groups, reduction, stride=1, downsample_kernel_size=1, downsample_padding=0 ): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d( self.inplanes, planes * block.expansion, kernel_size=downsample_kernel_size, stride=stride, padding=downsample_padding, bias=False ), nn.BatchNorm2d(planes * block.expansion), ) layers = [] layers.append( block( self.inplanes, planes, groups, reduction, stride, downsample ) ) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append(block(self.inplanes, planes, groups, reduction)) return nn.Sequential(*layers) def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): """ Construct fully connected layer - fc_dims (list or tuple): dimensions of fc layers, if None, no fc layers are constructed - input_dim (int): input dimension - dropout_p (float): dropout probability, if None, dropout is unused """ if fc_dims is None: self.feature_dim = input_dim return None assert isinstance( fc_dims, (list, tuple) ), 'fc_dims must be either list or tuple, but got {}'.format( type(fc_dims) ) layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def featuremaps(self, x): x = self.layer0(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) return x def forward(self, x): f = self.featuremaps(x) v = self.global_avgpool(f) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError("Unsupported loss: {}".format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def senet154(num_classes, loss='softmax', pretrained=True, **kwargs): model = SENet( num_classes=num_classes, loss=loss, block=SEBottleneck, layers=[3, 8, 36, 3], groups=64, reduction=16, dropout_p=0.2, last_stride=2, fc_dims=None, **kwargs ) if pretrained: model_url = pretrained_settings['senet154']['imagenet']['url'] init_pretrained_weights(model, model_url) return model def se_resnet50(num_classes, loss='softmax', pretrained=True, **kwargs): model = SENet( num_classes=num_classes, loss=loss, block=SEResNetBottleneck, layers=[3, 4, 6, 3], groups=1, reduction=16, dropout_p=None, inplanes=64, input_3x3=False, downsample_kernel_size=1, downsample_padding=0, last_stride=2, fc_dims=None, **kwargs ) if pretrained: model_url = pretrained_settings['se_resnet50']['imagenet']['url'] init_pretrained_weights(model, model_url) return model def se_resnet50_fc512(num_classes, loss='softmax', pretrained=True, **kwargs): model = SENet( num_classes=num_classes, loss=loss, block=SEResNetBottleneck, layers=[3, 4, 6, 3], groups=1, reduction=16, dropout_p=None, inplanes=64, input_3x3=False, downsample_kernel_size=1, downsample_padding=0, last_stride=1, fc_dims=[512], **kwargs ) if pretrained: model_url = pretrained_settings['se_resnet50']['imagenet']['url'] init_pretrained_weights(model, model_url) return model def se_resnet101(num_classes, loss='softmax', pretrained=True, **kwargs): model = SENet( num_classes=num_classes, loss=loss, block=SEResNetBottleneck, layers=[3, 4, 23, 3], groups=1, reduction=16, dropout_p=None, inplanes=64, input_3x3=False, downsample_kernel_size=1, downsample_padding=0, last_stride=2, fc_dims=None, **kwargs ) if pretrained: model_url = pretrained_settings['se_resnet101']['imagenet']['url'] init_pretrained_weights(model, model_url) return model def se_resnet152(num_classes, loss='softmax', pretrained=True, **kwargs): model = SENet( num_classes=num_classes, loss=loss, block=SEResNetBottleneck, layers=[3, 8, 36, 3], groups=1, reduction=16, dropout_p=None, inplanes=64, input_3x3=False, downsample_kernel_size=1, downsample_padding=0, last_stride=2, fc_dims=None, **kwargs ) if pretrained: model_url = pretrained_settings['se_resnet152']['imagenet']['url'] init_pretrained_weights(model, model_url) return model def se_resnext50_32x4d(num_classes, loss='softmax', pretrained=True, **kwargs): model = SENet( num_classes=num_classes, loss=loss, block=SEResNeXtBottleneck, layers=[3, 4, 6, 3], groups=32, reduction=16, dropout_p=None, inplanes=64, input_3x3=False, downsample_kernel_size=1, downsample_padding=0, last_stride=2, fc_dims=None, **kwargs ) if pretrained: model_url = pretrained_settings['se_resnext50_32x4d']['imagenet']['url' ] init_pretrained_weights(model, model_url) return model def se_resnext101_32x4d( num_classes, loss='softmax', pretrained=True, **kwargs ): model = SENet( num_classes=num_classes, loss=loss, block=SEResNeXtBottleneck, layers=[3, 4, 23, 3], groups=32, reduction=16, dropout_p=None, inplanes=64, input_3x3=False, downsample_kernel_size=1, downsample_padding=0, last_stride=2, fc_dims=None, **kwargs ) if pretrained: model_url = pretrained_settings['se_resnext101_32x4d']['imagenet'][ 'url'] init_pretrained_weights(model, model_url) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/shufflenet.py ================================================ from __future__ import division, absolute_import import torch import torch.utils.model_zoo as model_zoo from torch import nn from torch.nn import functional as F __all__ = ['shufflenet'] model_urls = { # training epoch = 90, top1 = 61.8 'imagenet': 'https://mega.nz/#!RDpUlQCY!tr_5xBEkelzDjveIYBBcGcovNCOrgfiJO9kiidz9fZM', } class ChannelShuffle(nn.Module): def __init__(self, num_groups): super(ChannelShuffle, self).__init__() self.g = num_groups def forward(self, x): b, c, h, w = x.size() n = c // self.g # reshape x = x.view(b, self.g, n, h, w) # transpose x = x.permute(0, 2, 1, 3, 4).contiguous() # flatten x = x.view(b, c, h, w) return x class Bottleneck(nn.Module): def __init__( self, in_channels, out_channels, stride, num_groups, group_conv1x1=True ): super(Bottleneck, self).__init__() assert stride in [1, 2], 'Warning: stride must be either 1 or 2' self.stride = stride mid_channels = out_channels // 4 if stride == 2: out_channels -= in_channels # group conv is not applied to first conv1x1 at stage 2 num_groups_conv1x1 = num_groups if group_conv1x1 else 1 self.conv1 = nn.Conv2d( in_channels, mid_channels, 1, groups=num_groups_conv1x1, bias=False ) self.bn1 = nn.BatchNorm2d(mid_channels) self.shuffle1 = ChannelShuffle(num_groups) self.conv2 = nn.Conv2d( mid_channels, mid_channels, 3, stride=stride, padding=1, groups=mid_channels, bias=False ) self.bn2 = nn.BatchNorm2d(mid_channels) self.conv3 = nn.Conv2d( mid_channels, out_channels, 1, groups=num_groups, bias=False ) self.bn3 = nn.BatchNorm2d(out_channels) if stride == 2: self.shortcut = nn.AvgPool2d(3, stride=2, padding=1) def forward(self, x): out = F.relu(self.bn1(self.conv1(x))) out = self.shuffle1(out) out = self.bn2(self.conv2(out)) out = self.bn3(self.conv3(out)) if self.stride == 2: res = self.shortcut(x) out = F.relu(torch.cat([res, out], 1)) else: out = F.relu(x + out) return out # configuration of (num_groups: #out_channels) based on Table 1 in the paper cfg = { 1: [144, 288, 576], 2: [200, 400, 800], 3: [240, 480, 960], 4: [272, 544, 1088], 8: [384, 768, 1536], } class ShuffleNet(nn.Module): """ShuffleNet. Reference: Zhang et al. ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices. CVPR 2018. Public keys: - ``shufflenet``: ShuffleNet (groups=3). """ def __init__(self, num_classes, loss='softmax', num_groups=3, **kwargs): super(ShuffleNet, self).__init__() self.loss = loss self.conv1 = nn.Sequential( nn.Conv2d(3, 24, 3, stride=2, padding=1, bias=False), nn.BatchNorm2d(24), nn.ReLU(), nn.MaxPool2d(3, stride=2, padding=1), ) self.stage2 = nn.Sequential( Bottleneck( 24, cfg[num_groups][0], 2, num_groups, group_conv1x1=False ), Bottleneck(cfg[num_groups][0], cfg[num_groups][0], 1, num_groups), Bottleneck(cfg[num_groups][0], cfg[num_groups][0], 1, num_groups), Bottleneck(cfg[num_groups][0], cfg[num_groups][0], 1, num_groups), ) self.stage3 = nn.Sequential( Bottleneck(cfg[num_groups][0], cfg[num_groups][1], 2, num_groups), Bottleneck(cfg[num_groups][1], cfg[num_groups][1], 1, num_groups), Bottleneck(cfg[num_groups][1], cfg[num_groups][1], 1, num_groups), Bottleneck(cfg[num_groups][1], cfg[num_groups][1], 1, num_groups), Bottleneck(cfg[num_groups][1], cfg[num_groups][1], 1, num_groups), Bottleneck(cfg[num_groups][1], cfg[num_groups][1], 1, num_groups), Bottleneck(cfg[num_groups][1], cfg[num_groups][1], 1, num_groups), Bottleneck(cfg[num_groups][1], cfg[num_groups][1], 1, num_groups), ) self.stage4 = nn.Sequential( Bottleneck(cfg[num_groups][1], cfg[num_groups][2], 2, num_groups), Bottleneck(cfg[num_groups][2], cfg[num_groups][2], 1, num_groups), Bottleneck(cfg[num_groups][2], cfg[num_groups][2], 1, num_groups), Bottleneck(cfg[num_groups][2], cfg[num_groups][2], 1, num_groups), ) self.classifier = nn.Linear(cfg[num_groups][2], num_classes) self.feat_dim = cfg[num_groups][2] def forward(self, x): x = self.conv1(x) x = self.stage2(x) x = self.stage3(x) x = self.stage4(x) x = F.avg_pool2d(x, x.size()[2:]).view(x.size(0), -1) if not self.training: return x y = self.classifier(x) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, x else: raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def shufflenet(num_classes, loss='softmax', pretrained=True, **kwargs): model = ShuffleNet(num_classes, loss, **kwargs) if pretrained: # init_pretrained_weights(model, model_urls['imagenet']) import warnings warnings.warn( 'The imagenet pretrained weights need to be manually downloaded from {}' .format(model_urls['imagenet']) ) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/shufflenetv2.py ================================================ """ Code source: https://github.com/pytorch/vision """ from __future__ import division, absolute_import import torch import torch.utils.model_zoo as model_zoo from torch import nn __all__ = [ 'shufflenet_v2_x0_5', 'shufflenet_v2_x1_0', 'shufflenet_v2_x1_5', 'shufflenet_v2_x2_0' ] model_urls = { 'shufflenetv2_x0.5': 'https://download.pytorch.org/models/shufflenetv2_x0.5-f707e7126e.pth', 'shufflenetv2_x1.0': 'https://download.pytorch.org/models/shufflenetv2_x1-5666bf0f80.pth', 'shufflenetv2_x1.5': None, 'shufflenetv2_x2.0': None, } def channel_shuffle(x, groups): batchsize, num_channels, height, width = x.data.size() channels_per_group = num_channels // groups # reshape x = x.view(batchsize, groups, channels_per_group, height, width) x = torch.transpose(x, 1, 2).contiguous() # flatten x = x.view(batchsize, -1, height, width) return x class InvertedResidual(nn.Module): def __init__(self, inp, oup, stride): super(InvertedResidual, self).__init__() if not (1 <= stride <= 3): raise ValueError('illegal stride value') self.stride = stride branch_features = oup // 2 assert (self.stride != 1) or (inp == branch_features << 1) if self.stride > 1: self.branch1 = nn.Sequential( self.depthwise_conv( inp, inp, kernel_size=3, stride=self.stride, padding=1 ), nn.BatchNorm2d(inp), nn.Conv2d( inp, branch_features, kernel_size=1, stride=1, padding=0, bias=False ), nn.BatchNorm2d(branch_features), nn.ReLU(inplace=True), ) self.branch2 = nn.Sequential( nn.Conv2d( inp if (self.stride > 1) else branch_features, branch_features, kernel_size=1, stride=1, padding=0, bias=False ), nn.BatchNorm2d(branch_features), nn.ReLU(inplace=True), self.depthwise_conv( branch_features, branch_features, kernel_size=3, stride=self.stride, padding=1 ), nn.BatchNorm2d(branch_features), nn.Conv2d( branch_features, branch_features, kernel_size=1, stride=1, padding=0, bias=False ), nn.BatchNorm2d(branch_features), nn.ReLU(inplace=True), ) @staticmethod def depthwise_conv(i, o, kernel_size, stride=1, padding=0, bias=False): return nn.Conv2d( i, o, kernel_size, stride, padding, bias=bias, groups=i ) def forward(self, x): if self.stride == 1: x1, x2 = x.chunk(2, dim=1) out = torch.cat((x1, self.branch2(x2)), dim=1) else: out = torch.cat((self.branch1(x), self.branch2(x)), dim=1) out = channel_shuffle(out, 2) return out class ShuffleNetV2(nn.Module): """ShuffleNetV2. Reference: Ma et al. ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design. ECCV 2018. Public keys: - ``shufflenet_v2_x0_5``: ShuffleNetV2 x0.5. - ``shufflenet_v2_x1_0``: ShuffleNetV2 x1.0. - ``shufflenet_v2_x1_5``: ShuffleNetV2 x1.5. - ``shufflenet_v2_x2_0``: ShuffleNetV2 x2.0. """ def __init__( self, num_classes, loss, stages_repeats, stages_out_channels, **kwargs ): super(ShuffleNetV2, self).__init__() self.loss = loss if len(stages_repeats) != 3: raise ValueError( 'expected stages_repeats as list of 3 positive ints' ) if len(stages_out_channels) != 5: raise ValueError( 'expected stages_out_channels as list of 5 positive ints' ) self._stage_out_channels = stages_out_channels input_channels = 3 output_channels = self._stage_out_channels[0] self.conv1 = nn.Sequential( nn.Conv2d(input_channels, output_channels, 3, 2, 1, bias=False), nn.BatchNorm2d(output_channels), nn.ReLU(inplace=True), ) input_channels = output_channels self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) stage_names = ['stage{}'.format(i) for i in [2, 3, 4]] for name, repeats, output_channels in zip( stage_names, stages_repeats, self._stage_out_channels[1:] ): seq = [InvertedResidual(input_channels, output_channels, 2)] for i in range(repeats - 1): seq.append( InvertedResidual(output_channels, output_channels, 1) ) setattr(self, name, nn.Sequential(*seq)) input_channels = output_channels output_channels = self._stage_out_channels[-1] self.conv5 = nn.Sequential( nn.Conv2d(input_channels, output_channels, 1, 1, 0, bias=False), nn.BatchNorm2d(output_channels), nn.ReLU(inplace=True), ) self.global_avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.classifier = nn.Linear(output_channels, num_classes) def featuremaps(self, x): x = self.conv1(x) x = self.maxpool(x) x = self.stage2(x) x = self.stage3(x) x = self.stage4(x) x = self.conv5(x) return x def forward(self, x): f = self.featuremaps(x) v = self.global_avgpool(f) v = v.view(v.size(0), -1) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError("Unsupported loss: {}".format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ if model_url is None: import warnings warnings.warn( 'ImageNet pretrained weights are unavailable for this model' ) return pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def shufflenet_v2_x0_5(num_classes, loss='softmax', pretrained=True, **kwargs): model = ShuffleNetV2( num_classes, loss, [4, 8, 4], [24, 48, 96, 192, 1024], **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['shufflenetv2_x0.5']) return model def shufflenet_v2_x1_0(num_classes, loss='softmax', pretrained=True, **kwargs): model = ShuffleNetV2( num_classes, loss, [4, 8, 4], [24, 116, 232, 464, 1024], **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['shufflenetv2_x1.0']) return model def shufflenet_v2_x1_5(num_classes, loss='softmax', pretrained=True, **kwargs): model = ShuffleNetV2( num_classes, loss, [4, 8, 4], [24, 176, 352, 704, 1024], **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['shufflenetv2_x1.5']) return model def shufflenet_v2_x2_0(num_classes, loss='softmax', pretrained=True, **kwargs): model = ShuffleNetV2( num_classes, loss, [4, 8, 4], [24, 244, 488, 976, 2048], **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['shufflenetv2_x2.0']) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/squeezenet.py ================================================ """ Code source: https://github.com/pytorch/vision """ from __future__ import division, absolute_import import torch import torch.nn as nn import torch.utils.model_zoo as model_zoo __all__ = ['squeezenet1_0', 'squeezenet1_1', 'squeezenet1_0_fc512'] model_urls = { 'squeezenet1_0': 'https://download.pytorch.org/models/squeezenet1_0-a815701f.pth', 'squeezenet1_1': 'https://download.pytorch.org/models/squeezenet1_1-f364aa15.pth', } class Fire(nn.Module): def __init__( self, inplanes, squeeze_planes, expand1x1_planes, expand3x3_planes ): super(Fire, self).__init__() self.inplanes = inplanes self.squeeze = nn.Conv2d(inplanes, squeeze_planes, kernel_size=1) self.squeeze_activation = nn.ReLU(inplace=True) self.expand1x1 = nn.Conv2d( squeeze_planes, expand1x1_planes, kernel_size=1 ) self.expand1x1_activation = nn.ReLU(inplace=True) self.expand3x3 = nn.Conv2d( squeeze_planes, expand3x3_planes, kernel_size=3, padding=1 ) self.expand3x3_activation = nn.ReLU(inplace=True) def forward(self, x): x = self.squeeze_activation(self.squeeze(x)) return torch.cat( [ self.expand1x1_activation(self.expand1x1(x)), self.expand3x3_activation(self.expand3x3(x)) ], 1 ) class SqueezeNet(nn.Module): """SqueezeNet. Reference: Iandola et al. SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and< 0.5 MB model size. arXiv:1602.07360. Public keys: - ``squeezenet1_0``: SqueezeNet (version=1.0). - ``squeezenet1_1``: SqueezeNet (version=1.1). - ``squeezenet1_0_fc512``: SqueezeNet (version=1.0) + FC. """ def __init__( self, num_classes, loss, version=1.0, fc_dims=None, dropout_p=None, **kwargs ): super(SqueezeNet, self).__init__() self.loss = loss self.feature_dim = 512 if version not in [1.0, 1.1]: raise ValueError( 'Unsupported SqueezeNet version {version}:' '1.0 or 1.1 expected'.format(version=version) ) if version == 1.0: self.features = nn.Sequential( nn.Conv2d(3, 96, kernel_size=7, stride=2), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), Fire(96, 16, 64, 64), Fire(128, 16, 64, 64), Fire(128, 32, 128, 128), nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), Fire(256, 32, 128, 128), Fire(256, 48, 192, 192), Fire(384, 48, 192, 192), Fire(384, 64, 256, 256), nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), Fire(512, 64, 256, 256), ) else: self.features = nn.Sequential( nn.Conv2d(3, 64, kernel_size=3, stride=2), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), Fire(64, 16, 64, 64), Fire(128, 16, 64, 64), nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), Fire(128, 32, 128, 128), Fire(256, 32, 128, 128), nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), Fire(256, 48, 192, 192), Fire(384, 48, 192, 192), Fire(384, 64, 256, 256), Fire(512, 64, 256, 256), ) self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.fc = self._construct_fc_layer(fc_dims, 512, dropout_p) self.classifier = nn.Linear(self.feature_dim, num_classes) self._init_params() def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): """Constructs fully connected layer Args: fc_dims (list or tuple): dimensions of fc layers, if None, no fc layers are constructed input_dim (int): input dimension dropout_p (float): dropout probability, if None, dropout is unused """ if fc_dims is None: self.feature_dim = input_dim return None assert isinstance( fc_dims, (list, tuple) ), 'fc_dims must be either list or tuple, but got {}'.format( type(fc_dims) ) layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def _init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def forward(self, x): f = self.features(x) v = self.global_avgpool(f) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): """Initializes model with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url, map_location=None) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def squeezenet1_0(num_classes, loss='softmax', pretrained=True, **kwargs): model = SqueezeNet( num_classes, loss, version=1.0, fc_dims=None, dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['squeezenet1_0']) return model def squeezenet1_0_fc512( num_classes, loss='softmax', pretrained=True, **kwargs ): model = SqueezeNet( num_classes, loss, version=1.0, fc_dims=[512], dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['squeezenet1_0']) return model def squeezenet1_1(num_classes, loss='softmax', pretrained=True, **kwargs): model = SqueezeNet( num_classes, loss, version=1.1, fc_dims=None, dropout_p=None, **kwargs ) if pretrained: init_pretrained_weights(model, model_urls['squeezenet1_1']) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/models/xception.py ================================================ from __future__ import division, absolute_import import torch.nn as nn import torch.nn.functional as F import torch.utils.model_zoo as model_zoo __all__ = ['xception'] pretrained_settings = { 'xception': { 'imagenet': { 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/xception-43020ad28.pth', 'input_space': 'RGB', 'input_size': [3, 299, 299], 'input_range': [0, 1], 'mean': [0.5, 0.5, 0.5], 'std': [0.5, 0.5, 0.5], 'num_classes': 1000, 'scale': 0.8975 # The resize parameter of the validation transform should be 333, and make sure to center crop at 299x299 } } } class SeparableConv2d(nn.Module): def __init__( self, in_channels, out_channels, kernel_size=1, stride=1, padding=0, dilation=1, bias=False ): super(SeparableConv2d, self).__init__() self.conv1 = nn.Conv2d( in_channels, in_channels, kernel_size, stride, padding, dilation, groups=in_channels, bias=bias ) self.pointwise = nn.Conv2d( in_channels, out_channels, 1, 1, 0, 1, 1, bias=bias ) def forward(self, x): x = self.conv1(x) x = self.pointwise(x) return x class Block(nn.Module): def __init__( self, in_filters, out_filters, reps, strides=1, start_with_relu=True, grow_first=True ): super(Block, self).__init__() if out_filters != in_filters or strides != 1: self.skip = nn.Conv2d( in_filters, out_filters, 1, stride=strides, bias=False ) self.skipbn = nn.BatchNorm2d(out_filters) else: self.skip = None self.relu = nn.ReLU(inplace=True) rep = [] filters = in_filters if grow_first: rep.append(self.relu) rep.append( SeparableConv2d( in_filters, out_filters, 3, stride=1, padding=1, bias=False ) ) rep.append(nn.BatchNorm2d(out_filters)) filters = out_filters for i in range(reps - 1): rep.append(self.relu) rep.append( SeparableConv2d( filters, filters, 3, stride=1, padding=1, bias=False ) ) rep.append(nn.BatchNorm2d(filters)) if not grow_first: rep.append(self.relu) rep.append( SeparableConv2d( in_filters, out_filters, 3, stride=1, padding=1, bias=False ) ) rep.append(nn.BatchNorm2d(out_filters)) if not start_with_relu: rep = rep[1:] else: rep[0] = nn.ReLU(inplace=False) if strides != 1: rep.append(nn.MaxPool2d(3, strides, 1)) self.rep = nn.Sequential(*rep) def forward(self, inp): x = self.rep(inp) if self.skip is not None: skip = self.skip(inp) skip = self.skipbn(skip) else: skip = inp x += skip return x class Xception(nn.Module): """Xception. Reference: Chollet. Xception: Deep Learning with Depthwise Separable Convolutions. CVPR 2017. Public keys: - ``xception``: Xception. """ def __init__( self, num_classes, loss, fc_dims=None, dropout_p=None, **kwargs ): super(Xception, self).__init__() self.loss = loss self.conv1 = nn.Conv2d(3, 32, 3, 2, 0, bias=False) self.bn1 = nn.BatchNorm2d(32) self.conv2 = nn.Conv2d(32, 64, 3, bias=False) self.bn2 = nn.BatchNorm2d(64) self.block1 = Block( 64, 128, 2, 2, start_with_relu=False, grow_first=True ) self.block2 = Block( 128, 256, 2, 2, start_with_relu=True, grow_first=True ) self.block3 = Block( 256, 728, 2, 2, start_with_relu=True, grow_first=True ) self.block4 = Block( 728, 728, 3, 1, start_with_relu=True, grow_first=True ) self.block5 = Block( 728, 728, 3, 1, start_with_relu=True, grow_first=True ) self.block6 = Block( 728, 728, 3, 1, start_with_relu=True, grow_first=True ) self.block7 = Block( 728, 728, 3, 1, start_with_relu=True, grow_first=True ) self.block8 = Block( 728, 728, 3, 1, start_with_relu=True, grow_first=True ) self.block9 = Block( 728, 728, 3, 1, start_with_relu=True, grow_first=True ) self.block10 = Block( 728, 728, 3, 1, start_with_relu=True, grow_first=True ) self.block11 = Block( 728, 728, 3, 1, start_with_relu=True, grow_first=True ) self.block12 = Block( 728, 1024, 2, 2, start_with_relu=True, grow_first=False ) self.conv3 = SeparableConv2d(1024, 1536, 3, 1, 1) self.bn3 = nn.BatchNorm2d(1536) self.conv4 = SeparableConv2d(1536, 2048, 3, 1, 1) self.bn4 = nn.BatchNorm2d(2048) self.global_avgpool = nn.AdaptiveAvgPool2d(1) self.feature_dim = 2048 self.fc = self._construct_fc_layer(fc_dims, 2048, dropout_p) self.classifier = nn.Linear(self.feature_dim, num_classes) self._init_params() def _construct_fc_layer(self, fc_dims, input_dim, dropout_p=None): """Constructs fully connected layer. Args: fc_dims (list or tuple): dimensions of fc layers, if None, no fc layers are constructed input_dim (int): input dimension dropout_p (float): dropout probability, if None, dropout is unused """ if fc_dims is None: self.feature_dim = input_dim return None assert isinstance( fc_dims, (list, tuple) ), 'fc_dims must be either list or tuple, but got {}'.format( type(fc_dims) ) layers = [] for dim in fc_dims: layers.append(nn.Linear(input_dim, dim)) layers.append(nn.BatchNorm1d(dim)) layers.append(nn.ReLU(inplace=True)) if dropout_p is not None: layers.append(nn.Dropout(p=dropout_p)) input_dim = dim self.feature_dim = fc_dims[-1] return nn.Sequential(*layers) def _init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_( m.weight, mode='fan_out', nonlinearity='relu' ) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm1d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) elif isinstance(m, nn.Linear): nn.init.normal_(m.weight, 0, 0.01) if m.bias is not None: nn.init.constant_(m.bias, 0) def featuremaps(self, input): x = self.conv1(input) x = self.bn1(x) x = F.relu(x, inplace=True) x = self.conv2(x) x = self.bn2(x) x = F.relu(x, inplace=True) x = self.block1(x) x = self.block2(x) x = self.block3(x) x = self.block4(x) x = self.block5(x) x = self.block6(x) x = self.block7(x) x = self.block8(x) x = self.block9(x) x = self.block10(x) x = self.block11(x) x = self.block12(x) x = self.conv3(x) x = self.bn3(x) x = F.relu(x, inplace=True) x = self.conv4(x) x = self.bn4(x) x = F.relu(x, inplace=True) return x def forward(self, x): f = self.featuremaps(x) v = self.global_avgpool(f) v = v.view(v.size(0), -1) if self.fc is not None: v = self.fc(v) if not self.training: return v y = self.classifier(v) if self.loss == 'softmax': return y elif self.loss == 'triplet': return y, v else: raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): """Initialize models with pretrained weights. Layers that don't match with pretrained layers in name or size are kept unchanged. """ pretrain_dict = model_zoo.load_url(model_url) model_dict = model.state_dict() pretrain_dict = { k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size() } model_dict.update(pretrain_dict) model.load_state_dict(model_dict) def xception(num_classes, loss='softmax', pretrained=True, **kwargs): model = Xception(num_classes, loss, fc_dims=None, dropout_p=None, **kwargs) if pretrained: model_url = pretrained_settings['xception']['imagenet']['url'] init_pretrained_weights(model, model_url) return model ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/optim/__init__.py ================================================ from __future__ import absolute_import from .optimizer import build_optimizer from .lr_scheduler import build_lr_scheduler ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/optim/lr_scheduler.py ================================================ from __future__ import print_function, absolute_import import torch AVAI_SCH = ['single_step', 'multi_step', 'cosine'] def build_lr_scheduler( optimizer, lr_scheduler='single_step', stepsize=1, gamma=0.1, max_epoch=1 ): """A function wrapper for building a learning rate scheduler. Args: optimizer (Optimizer): an Optimizer. lr_scheduler (str, optional): learning rate scheduler method. Default is single_step. stepsize (int or list, optional): step size to decay learning rate. When ``lr_scheduler`` is "single_step", ``stepsize`` should be an integer. When ``lr_scheduler`` is "multi_step", ``stepsize`` is a list. Default is 1. gamma (float, optional): decay rate. Default is 0.1. max_epoch (int, optional): maximum epoch (for cosine annealing). Default is 1. Examples:: >>> # Decay learning rate by every 20 epochs. >>> scheduler = torchreid.optim.build_lr_scheduler( >>> optimizer, lr_scheduler='single_step', stepsize=20 >>> ) >>> # Decay learning rate at 30, 50 and 55 epochs. >>> scheduler = torchreid.optim.build_lr_scheduler( >>> optimizer, lr_scheduler='multi_step', stepsize=[30, 50, 55] >>> ) """ if lr_scheduler not in AVAI_SCH: raise ValueError( 'Unsupported scheduler: {}. Must be one of {}'.format( lr_scheduler, AVAI_SCH ) ) if lr_scheduler == 'single_step': if isinstance(stepsize, list): stepsize = stepsize[-1] if not isinstance(stepsize, int): raise TypeError( 'For single_step lr_scheduler, stepsize must ' 'be an integer, but got {}'.format(type(stepsize)) ) scheduler = torch.optim.lr_scheduler.StepLR( optimizer, step_size=stepsize, gamma=gamma ) elif lr_scheduler == 'multi_step': if not isinstance(stepsize, list): raise TypeError( 'For multi_step lr_scheduler, stepsize must ' 'be a list, but got {}'.format(type(stepsize)) ) scheduler = torch.optim.lr_scheduler.MultiStepLR( optimizer, milestones=stepsize, gamma=gamma ) elif lr_scheduler == 'cosine': scheduler = torch.optim.lr_scheduler.CosineAnnealingLR( optimizer, float(max_epoch) ) return scheduler ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/optim/optimizer.py ================================================ from __future__ import print_function, absolute_import import warnings import torch import torch.nn as nn from .radam import RAdam AVAI_OPTIMS = ['adam', 'amsgrad', 'sgd', 'rmsprop', 'radam'] def build_optimizer( model, optim='adam', lr=0.0003, weight_decay=5e-04, momentum=0.9, sgd_dampening=0, sgd_nesterov=False, rmsprop_alpha=0.99, adam_beta1=0.9, adam_beta2=0.99, staged_lr=False, new_layers='', base_lr_mult=0.1 ): """A function wrapper for building an optimizer. Args: model (nn.Module): model. optim (str, optional): optimizer. Default is "adam". lr (float, optional): learning rate. Default is 0.0003. weight_decay (float, optional): weight decay (L2 penalty). Default is 5e-04. momentum (float, optional): momentum factor in sgd. Default is 0.9, sgd_dampening (float, optional): dampening for momentum. Default is 0. sgd_nesterov (bool, optional): enables Nesterov momentum. Default is False. rmsprop_alpha (float, optional): smoothing constant for rmsprop. Default is 0.99. adam_beta1 (float, optional): beta-1 value in adam. Default is 0.9. adam_beta2 (float, optional): beta-2 value in adam. Default is 0.99, staged_lr (bool, optional): uses different learning rates for base and new layers. Base layers are pretrained layers while new layers are randomly initialized, e.g. the identity classification layer. Enabling ``staged_lr`` can allow the base layers to be trained with a smaller learning rate determined by ``base_lr_mult``, while the new layers will take the ``lr``. Default is False. new_layers (str or list): attribute names in ``model``. Default is empty. base_lr_mult (float, optional): learning rate multiplier for base layers. Default is 0.1. Examples:: >>> # A normal optimizer can be built by >>> optimizer = torchreid.optim.build_optimizer(model, optim='sgd', lr=0.01) >>> # If you want to use a smaller learning rate for pretrained layers >>> # and the attribute name for the randomly initialized layer is 'classifier', >>> # you can do >>> optimizer = torchreid.optim.build_optimizer( >>> model, optim='sgd', lr=0.01, staged_lr=True, >>> new_layers='classifier', base_lr_mult=0.1 >>> ) >>> # Now the `classifier` has learning rate 0.01 but the base layers >>> # have learning rate 0.01 * 0.1. >>> # new_layers can also take multiple attribute names. Say the new layers >>> # are 'fc' and 'classifier', you can do >>> optimizer = torchreid.optim.build_optimizer( >>> model, optim='sgd', lr=0.01, staged_lr=True, >>> new_layers=['fc', 'classifier'], base_lr_mult=0.1 >>> ) """ if optim not in AVAI_OPTIMS: raise ValueError( 'Unsupported optim: {}. Must be one of {}'.format( optim, AVAI_OPTIMS ) ) if not isinstance(model, nn.Module): raise TypeError( 'model given to build_optimizer must be an instance of nn.Module' ) if staged_lr: if isinstance(new_layers, str): if new_layers is None: warnings.warn( 'new_layers is empty, therefore, staged_lr is useless' ) new_layers = [new_layers] if isinstance(model, nn.DataParallel): model = model.module base_params = [] base_layers = [] new_params = [] for name, module in model.named_children(): if name in new_layers: new_params += [p for p in module.parameters()] else: base_params += [p for p in module.parameters()] base_layers.append(name) param_groups = [ { 'params': base_params, 'lr': lr * base_lr_mult }, { 'params': new_params }, ] else: param_groups = model.parameters() if optim == 'adam': optimizer = torch.optim.Adam( param_groups, lr=lr, weight_decay=weight_decay, betas=(adam_beta1, adam_beta2), ) elif optim == 'amsgrad': optimizer = torch.optim.Adam( param_groups, lr=lr, weight_decay=weight_decay, betas=(adam_beta1, adam_beta2), amsgrad=True, ) elif optim == 'sgd': optimizer = torch.optim.SGD( param_groups, lr=lr, momentum=momentum, weight_decay=weight_decay, dampening=sgd_dampening, nesterov=sgd_nesterov, ) elif optim == 'rmsprop': optimizer = torch.optim.RMSprop( param_groups, lr=lr, momentum=momentum, weight_decay=weight_decay, alpha=rmsprop_alpha, ) elif optim == 'radam': optimizer = RAdam( param_groups, lr=lr, weight_decay=weight_decay, betas=(adam_beta1, adam_beta2) ) return optimizer ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/optim/radam.py ================================================ """ Imported from: https://github.com/LiyuanLucasLiu/RAdam Paper: https://arxiv.org/abs/1908.03265 @article{liu2019radam, title={On the Variance of the Adaptive Learning Rate and Beyond}, author={Liu, Liyuan and Jiang, Haoming and He, Pengcheng and Chen, Weizhu and Liu, Xiaodong and Gao, Jianfeng and Han, Jiawei}, journal={arXiv preprint arXiv:1908.03265}, year={2019} } """ from __future__ import print_function, absolute_import import math import torch from torch.optim.optimizer import Optimizer class RAdam(Optimizer): def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, degenerated_to_sgd=True ): if not 0.0 <= lr: raise ValueError("Invalid learning rate: {}".format(lr)) if not 0.0 <= eps: raise ValueError("Invalid epsilon value: {}".format(eps)) if not 0.0 <= betas[0] < 1.0: raise ValueError( "Invalid beta parameter at index 0: {}".format(betas[0]) ) if not 0.0 <= betas[1] < 1.0: raise ValueError( "Invalid beta parameter at index 1: {}".format(betas[1]) ) self.degenerated_to_sgd = degenerated_to_sgd defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) self.buffer = [[None, None, None] for ind in range(10)] super(RAdam, self).__init__(params, defaults) def __setstate__(self, state): super(RAdam, self).__setstate__(state) def step(self, closure=None): loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data.float() if grad.is_sparse: raise RuntimeError( 'RAdam does not support sparse gradients' ) p_data_fp32 = p.data.float() state = self.state[p] if len(state) == 0: state['step'] = 0 state['exp_avg'] = torch.zeros_like(p_data_fp32) state['exp_avg_sq'] = torch.zeros_like(p_data_fp32) else: state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32) state['exp_avg_sq'] = state['exp_avg_sq'].type_as( p_data_fp32 ) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] beta1, beta2 = group['betas'] exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) exp_avg.mul_(beta1).add_(1 - beta1, grad) state['step'] += 1 buffered = self.buffer[int(state['step'] % 10)] if state['step'] == buffered[0]: N_sma, step_size = buffered[1], buffered[2] else: buffered[0] = state['step'] beta2_t = beta2**state['step'] N_sma_max = 2 / (1-beta2) - 1 N_sma = N_sma_max - 2 * state['step' ] * beta2_t / (1-beta2_t) buffered[1] = N_sma # more conservative since it's an approximated value if N_sma >= 5: step_size = math.sqrt( (1-beta2_t) * (N_sma-4) / (N_sma_max-4) * (N_sma-2) / N_sma * N_sma_max / (N_sma_max-2) ) / (1 - beta1**state['step']) elif self.degenerated_to_sgd: step_size = 1.0 / (1 - beta1**state['step']) else: step_size = -1 buffered[2] = step_size # more conservative since it's an approximated value if N_sma >= 5: if group['weight_decay'] != 0: p_data_fp32.add_( -group['weight_decay'] * group['lr'], p_data_fp32 ) denom = exp_avg_sq.sqrt().add_(group['eps']) p_data_fp32.addcdiv_( -step_size * group['lr'], exp_avg, denom ) p.data.copy_(p_data_fp32) elif step_size > 0: if group['weight_decay'] != 0: p_data_fp32.add_( -group['weight_decay'] * group['lr'], p_data_fp32 ) p_data_fp32.add_(-step_size * group['lr'], exp_avg) p.data.copy_(p_data_fp32) return loss class PlainRAdam(Optimizer): def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, degenerated_to_sgd=True ): if not 0.0 <= lr: raise ValueError("Invalid learning rate: {}".format(lr)) if not 0.0 <= eps: raise ValueError("Invalid epsilon value: {}".format(eps)) if not 0.0 <= betas[0] < 1.0: raise ValueError( "Invalid beta parameter at index 0: {}".format(betas[0]) ) if not 0.0 <= betas[1] < 1.0: raise ValueError( "Invalid beta parameter at index 1: {}".format(betas[1]) ) self.degenerated_to_sgd = degenerated_to_sgd defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) super(PlainRAdam, self).__init__(params, defaults) def __setstate__(self, state): super(PlainRAdam, self).__setstate__(state) def step(self, closure=None): loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data.float() if grad.is_sparse: raise RuntimeError( 'RAdam does not support sparse gradients' ) p_data_fp32 = p.data.float() state = self.state[p] if len(state) == 0: state['step'] = 0 state['exp_avg'] = torch.zeros_like(p_data_fp32) state['exp_avg_sq'] = torch.zeros_like(p_data_fp32) else: state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32) state['exp_avg_sq'] = state['exp_avg_sq'].type_as( p_data_fp32 ) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] beta1, beta2 = group['betas'] exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) exp_avg.mul_(beta1).add_(1 - beta1, grad) state['step'] += 1 beta2_t = beta2**state['step'] N_sma_max = 2 / (1-beta2) - 1 N_sma = N_sma_max - 2 * state['step'] * beta2_t / (1-beta2_t) # more conservative since it's an approximated value if N_sma >= 5: if group['weight_decay'] != 0: p_data_fp32.add_( -group['weight_decay'] * group['lr'], p_data_fp32 ) step_size = group['lr'] * math.sqrt( (1-beta2_t) * (N_sma-4) / (N_sma_max-4) * (N_sma-2) / N_sma * N_sma_max / (N_sma_max-2) ) / (1 - beta1**state['step']) denom = exp_avg_sq.sqrt().add_(group['eps']) p_data_fp32.addcdiv_(-step_size, exp_avg, denom) p.data.copy_(p_data_fp32) elif self.degenerated_to_sgd: if group['weight_decay'] != 0: p_data_fp32.add_( -group['weight_decay'] * group['lr'], p_data_fp32 ) step_size = group['lr'] / (1 - beta1**state['step']) p_data_fp32.add_(-step_size, exp_avg) p.data.copy_(p_data_fp32) return loss class AdamW(Optimizer): def __init__( self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, warmup=0 ): if not 0.0 <= lr: raise ValueError("Invalid learning rate: {}".format(lr)) if not 0.0 <= eps: raise ValueError("Invalid epsilon value: {}".format(eps)) if not 0.0 <= betas[0] < 1.0: raise ValueError( "Invalid beta parameter at index 0: {}".format(betas[0]) ) if not 0.0 <= betas[1] < 1.0: raise ValueError( "Invalid beta parameter at index 1: {}".format(betas[1]) ) defaults = dict( lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, warmup=warmup ) super(AdamW, self).__init__(params, defaults) def __setstate__(self, state): super(AdamW, self).__setstate__(state) def step(self, closure=None): loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data.float() if grad.is_sparse: raise RuntimeError( 'Adam does not support sparse gradients, please consider SparseAdam instead' ) p_data_fp32 = p.data.float() state = self.state[p] if len(state) == 0: state['step'] = 0 state['exp_avg'] = torch.zeros_like(p_data_fp32) state['exp_avg_sq'] = torch.zeros_like(p_data_fp32) else: state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32) state['exp_avg_sq'] = state['exp_avg_sq'].type_as( p_data_fp32 ) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] beta1, beta2 = group['betas'] state['step'] += 1 exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) exp_avg.mul_(beta1).add_(1 - beta1, grad) denom = exp_avg_sq.sqrt().add_(group['eps']) bias_correction1 = 1 - beta1**state['step'] bias_correction2 = 1 - beta2**state['step'] if group['warmup'] > state['step']: scheduled_lr = 1e-8 + state['step'] * group['lr'] / group[ 'warmup'] else: scheduled_lr = group['lr'] step_size = scheduled_lr * math.sqrt( bias_correction2 ) / bias_correction1 if group['weight_decay'] != 0: p_data_fp32.add_( -group['weight_decay'] * scheduled_lr, p_data_fp32 ) p_data_fp32.addcdiv_(-step_size, exp_avg, denom) p.data.copy_(p_data_fp32) return loss ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/GPU-Re-Ranking/README.md ================================================ # Understanding Image Retrieval Re-Ranking: A Graph Neural Network Perspective [[Paper]](https://arxiv.org/abs/2012.07620v2) On the Market-1501 dataset, we accelerate the re-ranking processing from **89.2s** to **9.4ms** with one K40m GPU, facilitating the real-time post-processing. Similarly, we observe that our method achieves comparable or even better retrieval results on the other four image retrieval benchmarks, i.e., VeRi-776, Oxford-5k, Paris-6k and University-1652, with limited time cost. ## Prerequisites The code was mainly developed and tested with python 3.7, PyTorch 1.4.1, CUDA 10.2, and CentOS release 6.10. The code has been included in `/extension`. To compile it: ```shell cd extension sh make.sh ``` ## Demo The demo script `main.py` provides the gnn re-ranking method using the prepared feature. ```shell python main.py --data_path PATH_TO_DATA --k1 26 --k2 7 ``` ## Citation ```bibtex @article{zhang2020understanding, title={Understanding Image Retrieval Re-Ranking: A Graph Neural Network Perspective}, author={Xuanmeng Zhang, Minyue Jiang, Zhedong Zheng, Xiao Tan, Errui Ding, Yi Yang}, journal={arXiv preprint arXiv:2012.07620}, year={2020} } ``` ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/GPU-Re-Ranking/extension/adjacency_matrix/build_adjacency_matrix.cpp ================================================ #include #include #include at::Tensor build_adjacency_matrix_forward(torch::Tensor initial_rank); #define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be a CUDA tensor") #define CHECK_CONTIGUOUS(x) AT_ASSERTM(x.is_contiguous(), #x " must be contiguous") #define CHECK_INPUT(x) CHECK_CUDA(x); CHECK_CONTIGUOUS(x) at::Tensor build_adjacency_matrix(at::Tensor initial_rank) { CHECK_INPUT(initial_rank); return build_adjacency_matrix_forward(initial_rank); } PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("forward", &build_adjacency_matrix, "build_adjacency_matrix (CUDA)"); } ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/GPU-Re-Ranking/extension/adjacency_matrix/build_adjacency_matrix_kernel.cu ================================================ #include #include #include #include #define CUDA_1D_KERNEL_LOOP(i, n) for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < n; i += blockDim.x * gridDim.x) __global__ void build_adjacency_matrix_kernel(float* initial_rank, float* A, const int total_num, const int topk, const int nthreads, const int all_num) { int index = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = index; i < all_num; i += stride) { int ii = i / topk; A[ii * total_num + int(initial_rank[i])] = float(1.0); } } at::Tensor build_adjacency_matrix_forward(at::Tensor initial_rank) { const auto total_num = initial_rank.size(0); const auto topk = initial_rank.size(1); const auto all_num = total_num * topk; auto A = torch::zeros({total_num, total_num}, at::device(initial_rank.device()).dtype(at::ScalarType::Float)); const int threads = 1024; const int blocks = (all_num + threads - 1) / threads; build_adjacency_matrix_kernel<<>>(initial_rank.data_ptr(), A.data_ptr(), total_num, topk, threads, all_num); return A; } ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/GPU-Re-Ranking/extension/adjacency_matrix/setup.py ================================================ """ Understanding Image Retrieval Re-Ranking: A Graph Neural Network Perspective Xuanmeng Zhang, Minyue Jiang, Zhedong Zheng, Xiao Tan, Errui Ding, Yi Yang Project Page : https://github.com/Xuanmeng-Zhang/gnn-re-ranking Paper: https://arxiv.org/abs/2012.07620v2 ====================================================================== On the Market-1501 dataset, we accelerate the re-ranking processing from 89.2s to 9.4ms with one K40m GPU, facilitating the real-time post-processing. Similarly, we observe that our method achieves comparable or even better retrieval results on the other four image retrieval benchmarks, i.e., VeRi-776, Oxford-5k, Paris-6k and University-1652, with limited time cost. """ from setuptools import Extension, setup import torch import torch.nn as nn from torch.autograd import Function from torch.utils.cpp_extension import CUDAExtension, BuildExtension setup( name='build_adjacency_matrix', ext_modules=[ CUDAExtension( 'build_adjacency_matrix', [ 'build_adjacency_matrix.cpp', 'build_adjacency_matrix_kernel.cu', ] ), ], cmdclass={'build_ext': BuildExtension} ) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/GPU-Re-Ranking/extension/make.sh ================================================ cd adjacency_matrix python setup.py install cd ../propagation python setup.py install ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/GPU-Re-Ranking/extension/propagation/gnn_propagate.cpp ================================================ #include #include #include at::Tensor gnn_propagate_forward(at::Tensor A, at::Tensor initial_rank, at::Tensor S); #define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be a CUDA tensor") #define CHECK_CONTIGUOUS(x) AT_ASSERTM(x.is_contiguous(), #x " must be contiguous") #define CHECK_INPUT(x) CHECK_CUDA(x); CHECK_CONTIGUOUS(x) at::Tensor gnn_propagate(at::Tensor A ,at::Tensor initial_rank, at::Tensor S) { CHECK_INPUT(A); CHECK_INPUT(initial_rank); CHECK_INPUT(S); return gnn_propagate_forward(A, initial_rank, S); } PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("forward", &gnn_propagate, "gnn propagate (CUDA)"); } ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/GPU-Re-Ranking/extension/propagation/gnn_propagate_kernel.cu ================================================ #include #include #include #include #include __global__ void gnn_propagate_forward_kernel(float* initial_rank, float* A, float* A_qe, float* S, const int sample_num, const int topk, const int total_num) { int index = blockIdx.x * blockDim.x + threadIdx.x; int stride = blockDim.x * gridDim.x; for (int i = index; i < total_num; i += stride) { int fea = i % sample_num; int sample_index = i / sample_num; float sum = 0.0; for (int j = 0; j < topk ; j++) { int topk_fea_index = int(initial_rank[sample_index*topk+j]) * sample_num + fea; sum += A[ topk_fea_index] * S[sample_index*topk+j]; } A_qe[i] = sum; } } at::Tensor gnn_propagate_forward(at::Tensor A, at::Tensor initial_rank, at::Tensor S) { const auto sample_num = A.size(0); const auto topk = initial_rank.size(1); const auto total_num = sample_num * sample_num ; auto A_qe = torch::zeros({sample_num, sample_num}, at::device(initial_rank.device()).dtype(at::ScalarType::Float)); const int threads = 1024; const int blocks = (total_num + threads - 1) / threads; gnn_propagate_forward_kernel<<>>(initial_rank.data_ptr(), A.data_ptr(), A_qe.data_ptr(), S.data_ptr(), sample_num, topk, total_num); return A_qe; } ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/GPU-Re-Ranking/extension/propagation/setup.py ================================================ """ Understanding Image Retrieval Re-Ranking: A Graph Neural Network Perspective Xuanmeng Zhang, Minyue Jiang, Zhedong Zheng, Xiao Tan, Errui Ding, Yi Yang Project Page : https://github.com/Xuanmeng-Zhang/gnn-re-ranking Paper: https://arxiv.org/abs/2012.07620v2 ====================================================================== On the Market-1501 dataset, we accelerate the re-ranking processing from 89.2s to 9.4ms with one K40m GPU, facilitating the real-time post-processing. Similarly, we observe that our method achieves comparable or even better retrieval results on the other four image retrieval benchmarks, i.e., VeRi-776, Oxford-5k, Paris-6k and University-1652, with limited time cost. """ from setuptools import Extension, setup import torch import torch.nn as nn from torch.autograd import Function from torch.utils.cpp_extension import CUDAExtension, BuildExtension setup( name='gnn_propagate', ext_modules=[ CUDAExtension( 'gnn_propagate', [ 'gnn_propagate.cpp', 'gnn_propagate_kernel.cu', ] ), ], cmdclass={'build_ext': BuildExtension} ) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/GPU-Re-Ranking/gnn_reranking.py ================================================ """ Understanding Image Retrieval Re-Ranking: A Graph Neural Network Perspective Xuanmeng Zhang, Minyue Jiang, Zhedong Zheng, Xiao Tan, Errui Ding, Yi Yang Project Page : https://github.com/Xuanmeng-Zhang/gnn-re-ranking Paper: https://arxiv.org/abs/2012.07620v2 ====================================================================== On the Market-1501 dataset, we accelerate the re-ranking processing from 89.2s to 9.4ms with one K40m GPU, facilitating the real-time post-processing. Similarly, we observe that our method achieves comparable or even better retrieval results on the other four image retrieval benchmarks, i.e., VeRi-776, Oxford-5k, Paris-6k and University-1652, with limited time cost. """ import numpy as np import torch import gnn_propagate import build_adjacency_matrix from utils import * def gnn_reranking(X_q, X_g, k1, k2): query_num, gallery_num = X_q.shape[0], X_g.shape[0] X_u = torch.cat((X_q, X_g), axis=0) original_score = torch.mm(X_u, X_u.t()) del X_u, X_q, X_g # initial ranking list S, initial_rank = original_score.topk( k=k1, dim=-1, largest=True, sorted=True ) # stage 1 A = build_adjacency_matrix.forward(initial_rank.float()) S = S * S # stage 2 if k2 != 1: for i in range(2): A = A + A.T A = gnn_propagate.forward( A, initial_rank[:, :k2].contiguous().float(), S[:, :k2].contiguous().float() ) A_norm = torch.norm(A, p=2, dim=1, keepdim=True) A = A.div(A_norm.expand_as(A)) cosine_similarity = torch.mm(A[:query_num, ], A[query_num:, ].t()) del A, S L = torch.sort(-cosine_similarity, dim=1)[1] L = L.data.cpu().numpy() return L ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/GPU-Re-Ranking/main.py ================================================ """ Understanding Image Retrieval Re-Ranking: A Graph Neural Network Perspective Xuanmeng Zhang, Minyue Jiang, Zhedong Zheng, Xiao Tan, Errui Ding, Yi Yang Project Page : https://github.com/Xuanmeng-Zhang/gnn-re-ranking Paper: https://arxiv.org/abs/2012.07620v2 ====================================================================== On the Market-1501 dataset, we accelerate the re-ranking processing from 89.2s to 9.4ms with one K40m GPU, facilitating the real-time post-processing. Similarly, we observe that our method achieves comparable or even better retrieval results on the other four image retrieval benchmarks, i.e., VeRi-776, Oxford-5k, Paris-6k and University-1652, with limited time cost. """ import os import numpy as np import argparse import torch from utils import * from gnn_reranking import * parser = argparse.ArgumentParser(description='Reranking_is_GNN') parser.add_argument( '--data_path', type=str, default='../xm_rerank_gpu_2/features/market_88_test.pkl', help='path to dataset' ) parser.add_argument( '--k1', type=int, default=26, # Market-1501 # default=60, # Veri-776 help='parameter k1' ) parser.add_argument( '--k2', type=int, default=7, # Market-1501 # default=10, # Veri-776 help='parameter k2' ) args = parser.parse_args() def main(): data = load_pickle(args.data_path) query_cam = data['query_cam'] query_label = data['query_label'] gallery_cam = data['gallery_cam'] gallery_label = data['gallery_label'] gallery_feature = torch.FloatTensor(data['gallery_f']) query_feature = torch.FloatTensor(data['query_f']) query_feature = query_feature.cuda() gallery_feature = gallery_feature.cuda() indices = gnn_reranking(query_feature, gallery_feature, args.k1, args.k2) evaluate_ranking_list( indices, query_label, query_cam, gallery_label, gallery_cam ) if __name__ == '__main__': main() ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/GPU-Re-Ranking/utils.py ================================================ """ Understanding Image Retrieval Re-Ranking: A Graph Neural Network Perspective Xuanmeng Zhang, Minyue Jiang, Zhedong Zheng, Xiao Tan, Errui Ding, Yi Yang Project Page : https://github.com/Xuanmeng-Zhang/gnn-re-ranking Paper: https://arxiv.org/abs/2012.07620v2 ====================================================================== On the Market-1501 dataset, we accelerate the re-ranking processing from 89.2s to 9.4ms with one K40m GPU, facilitating the real-time post-processing. Similarly, we observe that our method achieves comparable or even better retrieval results on the other four image retrieval benchmarks, i.e., VeRi-776, Oxford-5k, Paris-6k and University-1652, with limited time cost. """ import os import numpy as np import pickle import torch def load_pickle(pickle_path): with open(pickle_path, 'rb') as f: data = pickle.load(f) return data def save_pickle(pickle_path, data): with open(pickle_path, 'wb') as f: pickle.dump(data, f, protocol=pickle.HIGHEST_PROTOCOL) def pairwise_squared_distance(x): ''' x : (n_samples, n_points, dims) return : (n_samples, n_points, n_points) ''' x2s = (x * x).sum(-1, keepdim=True) return x2s + x2s.transpose(-1, -2) - 2 * x @ x.transpose(-1, -2) def pairwise_distance(x, y): m, n = x.size(0), y.size(0) x = x.view(m, -1) y = y.view(n, -1) dist = torch.pow(x, 2).sum( dim=1, keepdim=True ).expand(m, n) + torch.pow(y, 2).sum( dim=1, keepdim=True ).expand(n, m).t() dist.addmm_(1, -2, x, y.t()) return dist def cosine_similarity(x, y): m, n = x.size(0), y.size(0) x = x.view(m, -1) y = y.view(n, -1) y = y.t() score = torch.mm(x, y) return score def evaluate_ranking_list( indices, query_label, query_cam, gallery_label, gallery_cam ): CMC = np.zeros((len(gallery_label)), dtype=np.int) ap = 0.0 for i in range(len(query_label)): ap_tmp, CMC_tmp = evaluate( indices[i], query_label[i], query_cam[i], gallery_label, gallery_cam ) if CMC_tmp[0] == -1: continue CMC = CMC + CMC_tmp ap += ap_tmp CMC = CMC.astype(np.float32) CMC = CMC / len(query_label) #average CMC print( 'Rank@1:%f Rank@5:%f Rank@10:%f mAP:%f' % (CMC[0], CMC[4], CMC[9], ap / len(query_label)) ) def evaluate(index, ql, qc, gl, gc): query_index = np.argwhere(gl == ql) camera_index = np.argwhere(gc == qc) good_index = np.setdiff1d(query_index, camera_index, assume_unique=True) junk_index1 = np.argwhere(gl == -1) junk_index2 = np.intersect1d(query_index, camera_index) junk_index = np.append(junk_index2, junk_index1) #.flatten()) CMC_tmp = compute_mAP(index, good_index, junk_index) return CMC_tmp def compute_mAP(index, good_index, junk_index): ap = 0 cmc = np.zeros((len(index)), dtype=np.int) if good_index.size == 0: # if empty cmc[0] = -1 return ap, cmc # remove junk_index mask = np.in1d(index, junk_index, invert=True) index = index[mask] # find good_index index ngood = len(good_index) mask = np.in1d(index, good_index) rows_good = np.argwhere(mask == True) rows_good = rows_good.flatten() cmc[rows_good[0]:] = 1 for i in range(ngood): d_recall = 1.0 / ngood precision = (i+1) * 1.0 / (rows_good[i] + 1) if rows_good[i] != 0: old_precision = i * 1.0 / rows_good[i] else: old_precision = 1.0 ap = ap + d_recall * (old_precision+precision) / 2 return ap, cmc ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/__init__.py ================================================ from __future__ import absolute_import from .tools import * from .rerank import re_ranking from .loggers import * from .avgmeter import * from .reidtools import * from .torchtools import * from .model_complexity import compute_model_complexity from .feature_extractor import FeatureExtractor ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/avgmeter.py ================================================ from __future__ import division, absolute_import from collections import defaultdict import torch __all__ = ['AverageMeter', 'MetricMeter'] class AverageMeter(object): """Computes and stores the average and current value. Examples:: >>> # Initialize a meter to record loss >>> losses = AverageMeter() >>> # Update meter after every minibatch update >>> losses.update(loss_value, batch_size) """ def __init__(self): self.reset() def reset(self): self.val = 0 self.avg = 0 self.sum = 0 self.count = 0 def update(self, val, n=1): self.val = val self.sum += val * n self.count += n self.avg = self.sum / self.count class MetricMeter(object): """A collection of metrics. Source: https://github.com/KaiyangZhou/Dassl.pytorch Examples:: >>> # 1. Create an instance of MetricMeter >>> metric = MetricMeter() >>> # 2. Update using a dictionary as input >>> input_dict = {'loss_1': value_1, 'loss_2': value_2} >>> metric.update(input_dict) >>> # 3. Convert to string and print >>> print(str(metric)) """ def __init__(self, delimiter='\t'): self.meters = defaultdict(AverageMeter) self.delimiter = delimiter def update(self, input_dict): if input_dict is None: return if not isinstance(input_dict, dict): raise TypeError( 'Input to MetricMeter.update() must be a dictionary' ) for k, v in input_dict.items(): if isinstance(v, torch.Tensor): v = v.item() self.meters[k].update(v) def __str__(self): output_str = [] for name, meter in self.meters.items(): output_str.append( '{} {:.4f} ({:.4f})'.format(name, meter.val, meter.avg) ) return self.delimiter.join(output_str) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/feature_extractor.py ================================================ from __future__ import absolute_import import numpy as np import torch import torchvision.transforms as T from PIL import Image from torchreid.utils import ( check_isfile, load_pretrained_weights, compute_model_complexity ) from torchreid.models import build_model class FeatureExtractor(object): """A simple API for feature extraction. FeatureExtractor can be used like a python function, which accepts input of the following types: - a list of strings (image paths) - a list of numpy.ndarray each with shape (H, W, C) - a single string (image path) - a single numpy.ndarray with shape (H, W, C) - a torch.Tensor with shape (B, C, H, W) or (C, H, W) Returned is a torch tensor with shape (B, D) where D is the feature dimension. Args: model_name (str): model name. model_path (str): path to model weights. image_size (sequence or int): image height and width. pixel_mean (list): pixel mean for normalization. pixel_std (list): pixel std for normalization. pixel_norm (bool): whether to normalize pixels. device (str): 'cpu' or 'cuda' (could be specific gpu devices). verbose (bool): show model details. Examples:: from torchreid.utils import FeatureExtractor extractor = FeatureExtractor( model_name='osnet_x1_0', model_path='a/b/c/model.pth.tar', device='cuda' ) image_list = [ 'a/b/c/image001.jpg', 'a/b/c/image002.jpg', 'a/b/c/image003.jpg', 'a/b/c/image004.jpg', 'a/b/c/image005.jpg' ] features = extractor(image_list) print(features.shape) # output (5, 512) """ def __init__( self, model_name='', model_path='', image_size=(256, 128), pixel_mean=[0.485, 0.456, 0.406], pixel_std=[0.229, 0.224, 0.225], pixel_norm=True, device='cuda', verbose=True ): # Build model model = build_model( model_name, num_classes=1, pretrained=not (model_path and check_isfile(model_path)), use_gpu=device.startswith('cuda') ) model.eval() if verbose: num_params, flops = compute_model_complexity( model, (1, 3, image_size[0], image_size[1]) ) print('Model: {}'.format(model_name)) print('- params: {:,}'.format(num_params)) print('- flops: {:,}'.format(flops)) if model_path and check_isfile(model_path): load_pretrained_weights(model, model_path) # Build transform functions transforms = [] transforms += [T.Resize(image_size)] transforms += [T.ToTensor()] if pixel_norm: transforms += [T.Normalize(mean=pixel_mean, std=pixel_std)] preprocess = T.Compose(transforms) to_pil = T.ToPILImage() device = torch.device(device) model.to(device) # Class attributes self.model = model self.preprocess = preprocess self.to_pil = to_pil self.device = device def __call__(self, input): if isinstance(input, list): images = [] for element in input: if isinstance(element, str): image = Image.open(element).convert('RGB') elif isinstance(element, np.ndarray): image = self.to_pil(element) else: raise TypeError( 'Type of each element must belong to [str | numpy.ndarray]' ) image = self.preprocess(image) images.append(image) images = torch.stack(images, dim=0) images = images.to(self.device) elif isinstance(input, str): image = Image.open(input).convert('RGB') image = self.preprocess(image) images = image.unsqueeze(0).to(self.device) elif isinstance(input, np.ndarray): image = self.to_pil(input) image = self.preprocess(image) images = image.unsqueeze(0).to(self.device) elif isinstance(input, torch.Tensor): if input.dim() == 3: input = input.unsqueeze(0) images = input.to(self.device) else: raise NotImplementedError with torch.no_grad(): features = self.model(images) return features ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/loggers.py ================================================ from __future__ import absolute_import import os import sys import os.path as osp from .tools import mkdir_if_missing __all__ = ['Logger', 'RankLogger'] class Logger(object): """Writes console output to external text file. Imported from ``_ Args: fpath (str): directory to save logging file. Examples:: >>> import sys >>> import os >>> import os.path as osp >>> from torchreid.utils import Logger >>> save_dir = 'log/resnet50-softmax-market1501' >>> log_name = 'train.log' >>> sys.stdout = Logger(osp.join(args.save_dir, log_name)) """ def __init__(self, fpath=None): self.console = sys.stdout self.file = None if fpath is not None: mkdir_if_missing(osp.dirname(fpath)) self.file = open(fpath, 'w') def __del__(self): self.close() def __enter__(self): pass def __exit__(self, *args): self.close() def write(self, msg): self.console.write(msg) if self.file is not None: self.file.write(msg) def flush(self): self.console.flush() if self.file is not None: self.file.flush() os.fsync(self.file.fileno()) def close(self): self.console.close() if self.file is not None: self.file.close() class RankLogger(object): """Records the rank1 matching accuracy obtained for each test dataset at specified evaluation steps and provides a function to show the summarized results, which are convenient for analysis. Args: sources (str or list): source dataset name(s). targets (str or list): target dataset name(s). Examples:: >>> from torchreid.utils import RankLogger >>> s = 'market1501' >>> t = 'market1501' >>> ranklogger = RankLogger(s, t) >>> ranklogger.write(t, 10, 0.5) >>> ranklogger.write(t, 20, 0.7) >>> ranklogger.write(t, 30, 0.9) >>> ranklogger.show_summary() >>> # You will see: >>> # => Show performance summary >>> # market1501 (source) >>> # - epoch 10 rank1 50.0% >>> # - epoch 20 rank1 70.0% >>> # - epoch 30 rank1 90.0% >>> # If there are multiple test datasets >>> t = ['market1501', 'dukemtmcreid'] >>> ranklogger = RankLogger(s, t) >>> ranklogger.write(t[0], 10, 0.5) >>> ranklogger.write(t[0], 20, 0.7) >>> ranklogger.write(t[0], 30, 0.9) >>> ranklogger.write(t[1], 10, 0.1) >>> ranklogger.write(t[1], 20, 0.2) >>> ranklogger.write(t[1], 30, 0.3) >>> ranklogger.show_summary() >>> # You can see: >>> # => Show performance summary >>> # market1501 (source) >>> # - epoch 10 rank1 50.0% >>> # - epoch 20 rank1 70.0% >>> # - epoch 30 rank1 90.0% >>> # dukemtmcreid (target) >>> # - epoch 10 rank1 10.0% >>> # - epoch 20 rank1 20.0% >>> # - epoch 30 rank1 30.0% """ def __init__(self, sources, targets): self.sources = sources self.targets = targets if isinstance(self.sources, str): self.sources = [self.sources] if isinstance(self.targets, str): self.targets = [self.targets] self.logger = { name: { 'epoch': [], 'rank1': [] } for name in self.targets } def write(self, name, epoch, rank1): """Writes result. Args: name (str): dataset name. epoch (int): current epoch. rank1 (float): rank1 result. """ self.logger[name]['epoch'].append(epoch) self.logger[name]['rank1'].append(rank1) def show_summary(self): """Shows saved results.""" print('=> Show performance summary') for name in self.targets: from_where = 'source' if name in self.sources else 'target' print('{} ({})'.format(name, from_where)) for epoch, rank1 in zip( self.logger[name]['epoch'], self.logger[name]['rank1'] ): print('- epoch {}\t rank1 {:.1%}'.format(epoch, rank1)) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/model_complexity.py ================================================ from __future__ import division, print_function, absolute_import import math import numpy as np from itertools import repeat from collections import namedtuple, defaultdict import torch __all__ = ['compute_model_complexity'] """ Utility """ def _ntuple(n): def parse(x): if isinstance(x, int): return tuple(repeat(x, n)) return x return parse _single = _ntuple(1) _pair = _ntuple(2) _triple = _ntuple(3) """ Convolution """ def hook_convNd(m, x, y): k = torch.prod(torch.Tensor(m.kernel_size)).item() cin = m.in_channels flops_per_ele = k * cin # + (k*cin-1) if m.bias is not None: flops_per_ele += 1 flops = flops_per_ele * y.numel() / m.groups return int(flops) """ Pooling """ def hook_maxpool1d(m, x, y): flops_per_ele = m.kernel_size - 1 flops = flops_per_ele * y.numel() return int(flops) def hook_maxpool2d(m, x, y): k = _pair(m.kernel_size) k = torch.prod(torch.Tensor(k)).item() # ops: compare flops_per_ele = k - 1 flops = flops_per_ele * y.numel() return int(flops) def hook_maxpool3d(m, x, y): k = _triple(m.kernel_size) k = torch.prod(torch.Tensor(k)).item() flops_per_ele = k - 1 flops = flops_per_ele * y.numel() return int(flops) def hook_avgpool1d(m, x, y): flops_per_ele = m.kernel_size flops = flops_per_ele * y.numel() return int(flops) def hook_avgpool2d(m, x, y): k = _pair(m.kernel_size) k = torch.prod(torch.Tensor(k)).item() flops_per_ele = k flops = flops_per_ele * y.numel() return int(flops) def hook_avgpool3d(m, x, y): k = _triple(m.kernel_size) k = torch.prod(torch.Tensor(k)).item() flops_per_ele = k flops = flops_per_ele * y.numel() return int(flops) def hook_adapmaxpool1d(m, x, y): x = x[0] out_size = m.output_size k = math.ceil(x.size(2) / out_size) flops_per_ele = k - 1 flops = flops_per_ele * y.numel() return int(flops) def hook_adapmaxpool2d(m, x, y): x = x[0] out_size = _pair(m.output_size) k = torch.Tensor(list(x.size()[2:])) / torch.Tensor(out_size) k = torch.prod(torch.ceil(k)).item() flops_per_ele = k - 1 flops = flops_per_ele * y.numel() return int(flops) def hook_adapmaxpool3d(m, x, y): x = x[0] out_size = _triple(m.output_size) k = torch.Tensor(list(x.size()[2:])) / torch.Tensor(out_size) k = torch.prod(torch.ceil(k)).item() flops_per_ele = k - 1 flops = flops_per_ele * y.numel() return int(flops) def hook_adapavgpool1d(m, x, y): x = x[0] out_size = m.output_size k = math.ceil(x.size(2) / out_size) flops_per_ele = k flops = flops_per_ele * y.numel() return int(flops) def hook_adapavgpool2d(m, x, y): x = x[0] out_size = _pair(m.output_size) k = torch.Tensor(list(x.size()[2:])) / torch.Tensor(out_size) k = torch.prod(torch.ceil(k)).item() flops_per_ele = k flops = flops_per_ele * y.numel() return int(flops) def hook_adapavgpool3d(m, x, y): x = x[0] out_size = _triple(m.output_size) k = torch.Tensor(list(x.size()[2:])) / torch.Tensor(out_size) k = torch.prod(torch.ceil(k)).item() flops_per_ele = k flops = flops_per_ele * y.numel() return int(flops) """ Non-linear activations """ def hook_relu(m, x, y): # eq: max(0, x) num_ele = y.numel() return int(num_ele) def hook_leakyrelu(m, x, y): # eq: max(0, x) + negative_slope*min(0, x) num_ele = y.numel() flops = 3 * num_ele return int(flops) """ Normalization """ def hook_batchnormNd(m, x, y): num_ele = y.numel() flops = 2 * num_ele # mean and std if m.affine: flops += 2 * num_ele # gamma and beta return int(flops) def hook_instancenormNd(m, x, y): return hook_batchnormNd(m, x, y) def hook_groupnorm(m, x, y): return hook_batchnormNd(m, x, y) def hook_layernorm(m, x, y): num_ele = y.numel() flops = 2 * num_ele # mean and std if m.elementwise_affine: flops += 2 * num_ele # gamma and beta return int(flops) """ Linear """ def hook_linear(m, x, y): flops_per_ele = m.in_features # + (m.in_features-1) if m.bias is not None: flops_per_ele += 1 flops = flops_per_ele * y.numel() return int(flops) __generic_flops_counter = { # Convolution 'Conv1d': hook_convNd, 'Conv2d': hook_convNd, 'Conv3d': hook_convNd, # Pooling 'MaxPool1d': hook_maxpool1d, 'MaxPool2d': hook_maxpool2d, 'MaxPool3d': hook_maxpool3d, 'AvgPool1d': hook_avgpool1d, 'AvgPool2d': hook_avgpool2d, 'AvgPool3d': hook_avgpool3d, 'AdaptiveMaxPool1d': hook_adapmaxpool1d, 'AdaptiveMaxPool2d': hook_adapmaxpool2d, 'AdaptiveMaxPool3d': hook_adapmaxpool3d, 'AdaptiveAvgPool1d': hook_adapavgpool1d, 'AdaptiveAvgPool2d': hook_adapavgpool2d, 'AdaptiveAvgPool3d': hook_adapavgpool3d, # Non-linear activations 'ReLU': hook_relu, 'ReLU6': hook_relu, 'LeakyReLU': hook_leakyrelu, # Normalization 'BatchNorm1d': hook_batchnormNd, 'BatchNorm2d': hook_batchnormNd, 'BatchNorm3d': hook_batchnormNd, 'InstanceNorm1d': hook_instancenormNd, 'InstanceNorm2d': hook_instancenormNd, 'InstanceNorm3d': hook_instancenormNd, 'GroupNorm': hook_groupnorm, 'LayerNorm': hook_layernorm, # Linear 'Linear': hook_linear, } __conv_linear_flops_counter = { # Convolution 'Conv1d': hook_convNd, 'Conv2d': hook_convNd, 'Conv3d': hook_convNd, # Linear 'Linear': hook_linear, } def _get_flops_counter(only_conv_linear): if only_conv_linear: return __conv_linear_flops_counter return __generic_flops_counter def compute_model_complexity( model, input_size, verbose=False, only_conv_linear=True ): """Returns number of parameters and FLOPs. .. note:: (1) this function only provides an estimate of the theoretical time complexity rather than the actual running time which depends on implementations and hardware, and (2) the FLOPs is only counted for layers that are used at test time. This means that redundant layers such as person ID classification layer will be ignored as it is discarded when doing feature extraction. Note that the inference graph depends on how you construct the computations in ``forward()``. Args: model (nn.Module): network model. input_size (tuple): input size, e.g. (1, 3, 256, 128). verbose (bool, optional): shows detailed complexity of each module. Default is False. only_conv_linear (bool, optional): only considers convolution and linear layers when counting flops. Default is True. If set to False, flops of all layers will be counted. Examples:: >>> from torchreid import models, utils >>> model = models.build_model(name='resnet50', num_classes=1000) >>> num_params, flops = utils.compute_model_complexity(model, (1, 3, 256, 128), verbose=True) """ registered_handles = [] layer_list = [] layer = namedtuple('layer', ['class_name', 'params', 'flops']) def _add_hooks(m): def _has_submodule(m): return len(list(m.children())) > 0 def _hook(m, x, y): params = sum(p.numel() for p in m.parameters()) class_name = str(m.__class__.__name__) flops_counter = _get_flops_counter(only_conv_linear) if class_name in flops_counter: flops = flops_counter[class_name](m, x, y) else: flops = 0 layer_list.append( layer(class_name=class_name, params=params, flops=flops) ) # only consider the very basic nn layer if _has_submodule(m): return handle = m.register_forward_hook(_hook) registered_handles.append(handle) default_train_mode = model.training model.eval().apply(_add_hooks) input = torch.rand(input_size) if next(model.parameters()).is_cuda: input = input.cuda() model(input) # forward for handle in registered_handles: handle.remove() model.train(default_train_mode) if verbose: per_module_params = defaultdict(list) per_module_flops = defaultdict(list) total_params, total_flops = 0, 0 for layer in layer_list: total_params += layer.params total_flops += layer.flops if verbose: per_module_params[layer.class_name].append(layer.params) per_module_flops[layer.class_name].append(layer.flops) if verbose: num_udscore = 55 print(' {}'.format('-' * num_udscore)) print(' Model complexity with input size {}'.format(input_size)) print(' {}'.format('-' * num_udscore)) for class_name in per_module_params: params = int(np.sum(per_module_params[class_name])) flops = int(np.sum(per_module_flops[class_name])) print( ' {} (params={:,}, flops={:,})'.format( class_name, params, flops ) ) print(' {}'.format('-' * num_udscore)) print( ' Total (params={:,}, flops={:,})'.format( total_params, total_flops ) ) print(' {}'.format('-' * num_udscore)) return total_params, total_flops ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/reidtools.py ================================================ from __future__ import print_function, absolute_import import numpy as np import shutil import os.path as osp import cv2 from .tools import mkdir_if_missing __all__ = ['visualize_ranked_results'] GRID_SPACING = 10 QUERY_EXTRA_SPACING = 90 BW = 5 # border width GREEN = (0, 255, 0) RED = (0, 0, 255) def visualize_ranked_results( distmat, dataset, data_type, width=128, height=256, save_dir='', topk=10 ): """Visualizes ranked results. Supports both image-reid and video-reid. For image-reid, ranks will be plotted in a single figure. For video-reid, ranks will be saved in folders each containing a tracklet. Args: distmat (numpy.ndarray): distance matrix of shape (num_query, num_gallery). dataset (tuple): a 2-tuple containing (query, gallery), each of which contains tuples of (img_path(s), pid, camid, dsetid). data_type (str): "image" or "video". width (int, optional): resized image width. Default is 128. height (int, optional): resized image height. Default is 256. save_dir (str): directory to save output images. topk (int, optional): denoting top-k images in the rank list to be visualized. Default is 10. """ num_q, num_g = distmat.shape mkdir_if_missing(save_dir) print('# query: {}\n# gallery {}'.format(num_q, num_g)) print('Visualizing top-{} ranks ...'.format(topk)) query, gallery = dataset assert num_q == len(query) assert num_g == len(gallery) indices = np.argsort(distmat, axis=1) def _cp_img_to(src, dst, rank, prefix, matched=False): """ Args: src: image path or tuple (for vidreid) dst: target directory rank: int, denoting ranked position, starting from 1 prefix: string matched: bool """ if isinstance(src, (tuple, list)): if prefix == 'gallery': suffix = 'TRUE' if matched else 'FALSE' dst = osp.join( dst, prefix + '_top' + str(rank).zfill(3) ) + '_' + suffix else: dst = osp.join(dst, prefix + '_top' + str(rank).zfill(3)) mkdir_if_missing(dst) for img_path in src: shutil.copy(img_path, dst) else: dst = osp.join( dst, prefix + '_top' + str(rank).zfill(3) + '_name_' + osp.basename(src) ) shutil.copy(src, dst) for q_idx in range(num_q): qimg_path, qpid, qcamid = query[q_idx][:3] qimg_path_name = qimg_path[0] if isinstance( qimg_path, (tuple, list) ) else qimg_path if data_type == 'image': qimg = cv2.imread(qimg_path) qimg = cv2.resize(qimg, (width, height)) qimg = cv2.copyMakeBorder( qimg, BW, BW, BW, BW, cv2.BORDER_CONSTANT, value=(0, 0, 0) ) # resize twice to ensure that the border width is consistent across images qimg = cv2.resize(qimg, (width, height)) num_cols = topk + 1 grid_img = 255 * np.ones( ( height, num_cols*width + topk*GRID_SPACING + QUERY_EXTRA_SPACING, 3 ), dtype=np.uint8 ) grid_img[:, :width, :] = qimg else: qdir = osp.join( save_dir, osp.basename(osp.splitext(qimg_path_name)[0]) ) mkdir_if_missing(qdir) _cp_img_to(qimg_path, qdir, rank=0, prefix='query') rank_idx = 1 for g_idx in indices[q_idx, :]: gimg_path, gpid, gcamid = gallery[g_idx][:3] invalid = (qpid == gpid) & (qcamid == gcamid) if not invalid: matched = gpid == qpid if data_type == 'image': border_color = GREEN if matched else RED gimg = cv2.imread(gimg_path) gimg = cv2.resize(gimg, (width, height)) gimg = cv2.copyMakeBorder( gimg, BW, BW, BW, BW, cv2.BORDER_CONSTANT, value=border_color ) gimg = cv2.resize(gimg, (width, height)) start = rank_idx*width + rank_idx*GRID_SPACING + QUERY_EXTRA_SPACING end = ( rank_idx+1 ) * width + rank_idx*GRID_SPACING + QUERY_EXTRA_SPACING grid_img[:, start:end, :] = gimg else: _cp_img_to( gimg_path, qdir, rank=rank_idx, prefix='gallery', matched=matched ) rank_idx += 1 if rank_idx > topk: break if data_type == 'image': imname = osp.basename(osp.splitext(qimg_path_name)[0]) cv2.imwrite(osp.join(save_dir, imname + '.jpg'), grid_img) if (q_idx+1) % 100 == 0: print('- done {}/{}'.format(q_idx + 1, num_q)) print('Done. Images have been saved to "{}" ...'.format(save_dir)) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/rerank.py ================================================ #!/usr/bin/env python2/python3 # -*- coding: utf-8 -*- """ Source: https://github.com/zhunzhong07/person-re-ranking Created on Mon Jun 26 14:46:56 2017 @author: luohao Modified by Houjing Huang, 2017-12-22. - This version accepts distance matrix instead of raw features. - The difference of `/` division between python 2 and 3 is handled. - numpy.float16 is replaced by numpy.float32 for numerical precision. CVPR2017 paper:Zhong Z, Zheng L, Cao D, et al. Re-ranking Person Re-identification with k-reciprocal Encoding[J]. 2017. url:http://openaccess.thecvf.com/content_cvpr_2017/papers/Zhong_Re-Ranking_Person_Re-Identification_CVPR_2017_paper.pdf Matlab version: https://github.com/zhunzhong07/person-re-ranking API q_g_dist: query-gallery distance matrix, numpy array, shape [num_query, num_gallery] q_q_dist: query-query distance matrix, numpy array, shape [num_query, num_query] g_g_dist: gallery-gallery distance matrix, numpy array, shape [num_gallery, num_gallery] k1, k2, lambda_value: parameters, the original paper is (k1=20, k2=6, lambda_value=0.3) Returns: final_dist: re-ranked distance, numpy array, shape [num_query, num_gallery] """ from __future__ import division, print_function, absolute_import import numpy as np __all__ = ['re_ranking'] def re_ranking(q_g_dist, q_q_dist, g_g_dist, k1=20, k2=6, lambda_value=0.3): # The following naming, e.g. gallery_num, is different from outer scope. # Don't care about it. original_dist = np.concatenate( [ np.concatenate([q_q_dist, q_g_dist], axis=1), np.concatenate([q_g_dist.T, g_g_dist], axis=1) ], axis=0 ) original_dist = np.power(original_dist, 2).astype(np.float32) original_dist = np.transpose( 1. * original_dist / np.max(original_dist, axis=0) ) V = np.zeros_like(original_dist).astype(np.float32) initial_rank = np.argsort(original_dist).astype(np.int32) query_num = q_g_dist.shape[0] gallery_num = q_g_dist.shape[0] + q_g_dist.shape[1] all_num = gallery_num for i in range(all_num): # k-reciprocal neighbors forward_k_neigh_index = initial_rank[i, :k1 + 1] backward_k_neigh_index = initial_rank[forward_k_neigh_index, :k1 + 1] fi = np.where(backward_k_neigh_index == i)[0] k_reciprocal_index = forward_k_neigh_index[fi] k_reciprocal_expansion_index = k_reciprocal_index for j in range(len(k_reciprocal_index)): candidate = k_reciprocal_index[j] candidate_forward_k_neigh_index = initial_rank[ candidate, :int(np.around(k1 / 2.)) + 1] candidate_backward_k_neigh_index = initial_rank[ candidate_forward_k_neigh_index, :int(np.around(k1 / 2.)) + 1] fi_candidate = np.where( candidate_backward_k_neigh_index == candidate )[0] candidate_k_reciprocal_index = candidate_forward_k_neigh_index[ fi_candidate] if len( np. intersect1d(candidate_k_reciprocal_index, k_reciprocal_index) ) > 2. / 3 * len(candidate_k_reciprocal_index): k_reciprocal_expansion_index = np.append( k_reciprocal_expansion_index, candidate_k_reciprocal_index ) k_reciprocal_expansion_index = np.unique(k_reciprocal_expansion_index) weight = np.exp(-original_dist[i, k_reciprocal_expansion_index]) V[i, k_reciprocal_expansion_index] = 1. * weight / np.sum(weight) original_dist = original_dist[:query_num, ] if k2 != 1: V_qe = np.zeros_like(V, dtype=np.float32) for i in range(all_num): V_qe[i, :] = np.mean(V[initial_rank[i, :k2], :], axis=0) V = V_qe del V_qe del initial_rank invIndex = [] for i in range(gallery_num): invIndex.append(np.where(V[:, i] != 0)[0]) jaccard_dist = np.zeros_like(original_dist, dtype=np.float32) for i in range(query_num): temp_min = np.zeros(shape=[1, gallery_num], dtype=np.float32) indNonZero = np.where(V[i, :] != 0)[0] indImages = [] indImages = [invIndex[ind] for ind in indNonZero] for j in range(len(indNonZero)): temp_min[0, indImages[j]] = temp_min[0, indImages[j]] + np.minimum( V[i, indNonZero[j]], V[indImages[j], indNonZero[j]] ) jaccard_dist[i] = 1 - temp_min / (2.-temp_min) final_dist = jaccard_dist * (1-lambda_value) + original_dist*lambda_value del original_dist del V del jaccard_dist final_dist = final_dist[:query_num, query_num:] return final_dist ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/tools.py ================================================ from __future__ import division, print_function, absolute_import import os import sys import json import time import errno import numpy as np import random import os.path as osp import warnings import PIL import torch from PIL import Image __all__ = [ 'mkdir_if_missing', 'check_isfile', 'read_json', 'write_json', 'set_random_seed', 'download_url', 'read_image', 'collect_env_info', 'listdir_nohidden' ] def mkdir_if_missing(dirname): """Creates dirname if it is missing.""" if not osp.exists(dirname): try: os.makedirs(dirname) except OSError as e: if e.errno != errno.EEXIST: raise def check_isfile(fpath): """Checks if the given path is a file. Args: fpath (str): file path. Returns: bool """ isfile = osp.isfile(fpath) if not isfile: warnings.warn('No file found at "{}"'.format(fpath)) return isfile def read_json(fpath): """Reads json file from a path.""" with open(fpath, 'r') as f: obj = json.load(f) return obj def write_json(obj, fpath): """Writes to a json file.""" mkdir_if_missing(osp.dirname(fpath)) with open(fpath, 'w') as f: json.dump(obj, f, indent=4, separators=(',', ': ')) def set_random_seed(seed): random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) def download_url(url, dst): """Downloads file from a url to a destination. Args: url (str): url to download file. dst (str): destination path. """ from six.moves import urllib print('* url="{}"'.format(url)) print('* destination="{}"'.format(dst)) def _reporthook(count, block_size, total_size): global start_time if count == 0: start_time = time.time() return duration = time.time() - start_time progress_size = int(count * block_size) speed = int(progress_size / (1024*duration)) percent = int(count * block_size * 100 / total_size) sys.stdout.write( '\r...%d%%, %d MB, %d KB/s, %d seconds passed' % (percent, progress_size / (1024*1024), speed, duration) ) sys.stdout.flush() urllib.request.urlretrieve(url, dst, _reporthook) sys.stdout.write('\n') def read_image(path): """Reads image from path using ``PIL.Image``. Args: path (str): path to an image. Returns: PIL image """ got_img = False if not osp.exists(path): raise IOError('"{}" does not exist'.format(path)) while not got_img: try: img = Image.open(path).convert('RGB') got_img = True except IOError: print( 'IOError incurred when reading "{}". Will redo. Don\'t worry. Just chill.' .format(path) ) return img def collect_env_info(): """Returns env info as a string. Code source: github.com/facebookresearch/maskrcnn-benchmark """ from torch.utils.collect_env import get_pretty_env_info env_str = get_pretty_env_info() env_str += '\n Pillow ({})'.format(PIL.__version__) return env_str def listdir_nohidden(path, sort=False): """List non-hidden items in a directory. Args: path (str): directory path. sort (bool): sort the items. """ items = [f for f in os.listdir(path) if not f.startswith('.')] if sort: items.sort() return items ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid/torchreid/utils/torchtools.py ================================================ from __future__ import division, print_function, absolute_import import pickle import shutil import os.path as osp import warnings from functools import partial from collections import OrderedDict import torch import torch.nn as nn from .tools import mkdir_if_missing __all__ = [ 'save_checkpoint', 'load_checkpoint', 'resume_from_checkpoint', 'open_all_layers', 'open_specified_layers', 'count_num_param', 'load_pretrained_weights' ] def save_checkpoint( state, save_dir, is_best=False, remove_module_from_keys=False ): r"""Saves checkpoint. Args: state (dict): dictionary. save_dir (str): directory to save checkpoint. is_best (bool, optional): if True, this checkpoint will be copied and named ``model-best.pth.tar``. Default is False. remove_module_from_keys (bool, optional): whether to remove "module." from layer names. Default is False. Examples:: >>> state = { >>> 'state_dict': model.state_dict(), >>> 'epoch': 10, >>> 'rank1': 0.5, >>> 'optimizer': optimizer.state_dict() >>> } >>> save_checkpoint(state, 'log/my_model') """ mkdir_if_missing(save_dir) if remove_module_from_keys: # remove 'module.' in state_dict's keys state_dict = state['state_dict'] new_state_dict = OrderedDict() for k, v in state_dict.items(): if k.startswith('module.'): k = k[7:] new_state_dict[k] = v state['state_dict'] = new_state_dict # save epoch = state['epoch'] fpath = osp.join(save_dir, 'model.pth.tar-' + str(epoch)) torch.save(state, fpath) print('Checkpoint saved to "{}"'.format(fpath)) if is_best: shutil.copy(fpath, osp.join(osp.dirname(fpath), 'model-best.pth.tar')) def load_checkpoint(fpath): r"""Loads checkpoint. ``UnicodeDecodeError`` can be well handled, which means python2-saved files can be read from python3. Args: fpath (str): path to checkpoint. Returns: dict Examples:: >>> from torchreid.utils import load_checkpoint >>> fpath = 'log/my_model/model.pth.tar-10' >>> checkpoint = load_checkpoint(fpath) """ if fpath is None: raise ValueError('File path is None') fpath = osp.abspath(osp.expanduser(fpath)) if not osp.exists(fpath): raise FileNotFoundError('File is not found at "{}"'.format(fpath)) map_location = None if torch.cuda.is_available() else 'cpu' try: checkpoint = torch.load(fpath, map_location=map_location) except UnicodeDecodeError: pickle.load = partial(pickle.load, encoding="latin1") pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1") checkpoint = torch.load( fpath, pickle_module=pickle, map_location=map_location ) except Exception: print('Unable to load checkpoint from "{}"'.format(fpath)) raise return checkpoint def resume_from_checkpoint(fpath, model, optimizer=None, scheduler=None): r"""Resumes training from a checkpoint. This will load (1) model weights and (2) ``state_dict`` of optimizer if ``optimizer`` is not None. Args: fpath (str): path to checkpoint. model (nn.Module): model. optimizer (Optimizer, optional): an Optimizer. scheduler (LRScheduler, optional): an LRScheduler. Returns: int: start_epoch. Examples:: >>> from torchreid.utils import resume_from_checkpoint >>> fpath = 'log/my_model/model.pth.tar-10' >>> start_epoch = resume_from_checkpoint( >>> fpath, model, optimizer, scheduler >>> ) """ print('Loading checkpoint from "{}"'.format(fpath)) checkpoint = load_checkpoint(fpath) model.load_state_dict(checkpoint['state_dict']) print('Loaded model weights') if optimizer is not None and 'optimizer' in checkpoint.keys(): optimizer.load_state_dict(checkpoint['optimizer']) print('Loaded optimizer') if scheduler is not None and 'scheduler' in checkpoint.keys(): scheduler.load_state_dict(checkpoint['scheduler']) print('Loaded scheduler') start_epoch = checkpoint['epoch'] print('Last epoch = {}'.format(start_epoch)) if 'rank1' in checkpoint.keys(): print('Last rank1 = {:.1%}'.format(checkpoint['rank1'])) return start_epoch def adjust_learning_rate( optimizer, base_lr, epoch, stepsize=20, gamma=0.1, linear_decay=False, final_lr=0, max_epoch=100 ): r"""Adjusts learning rate. Deprecated. """ if linear_decay: # linearly decay learning rate from base_lr to final_lr frac_done = epoch / max_epoch lr = frac_done*final_lr + (1.-frac_done) * base_lr else: # decay learning rate by gamma for every stepsize lr = base_lr * (gamma**(epoch // stepsize)) for param_group in optimizer.param_groups: param_group['lr'] = lr def set_bn_to_eval(m): r"""Sets BatchNorm layers to eval mode.""" # 1. no update for running mean and var # 2. scale and shift parameters are still trainable classname = m.__class__.__name__ if classname.find('BatchNorm') != -1: m.eval() def open_all_layers(model): r"""Opens all layers in model for training. Examples:: >>> from torchreid.utils import open_all_layers >>> open_all_layers(model) """ model.train() for p in model.parameters(): p.requires_grad = True def open_specified_layers(model, open_layers): r"""Opens specified layers in model for training while keeping other layers frozen. Args: model (nn.Module): neural net model. open_layers (str or list): layers open for training. Examples:: >>> from torchreid.utils import open_specified_layers >>> # Only model.classifier will be updated. >>> open_layers = 'classifier' >>> open_specified_layers(model, open_layers) >>> # Only model.fc and model.classifier will be updated. >>> open_layers = ['fc', 'classifier'] >>> open_specified_layers(model, open_layers) """ if isinstance(model, nn.DataParallel): model = model.module if isinstance(open_layers, str): open_layers = [open_layers] for layer in open_layers: assert hasattr( model, layer ), '"{}" is not an attribute of the model, please provide the correct name'.format( layer ) for name, module in model.named_children(): if name in open_layers: module.train() for p in module.parameters(): p.requires_grad = True else: module.eval() for p in module.parameters(): p.requires_grad = False def count_num_param(model): r"""Counts number of parameters in a model while ignoring ``self.classifier``. Args: model (nn.Module): network model. Examples:: >>> from torchreid.utils import count_num_param >>> model_size = count_num_param(model) .. warning:: This method is deprecated in favor of ``torchreid.utils.compute_model_complexity``. """ warnings.warn( 'This method is deprecated and will be removed in the future.' ) num_param = sum(p.numel() for p in model.parameters()) if isinstance(model, nn.DataParallel): model = model.module if hasattr(model, 'classifier') and isinstance(model.classifier, nn.Module): # we ignore the classifier because it is unused at test time num_param -= sum(p.numel() for p in model.classifier.parameters()) return num_param def load_pretrained_weights(model, weight_path): r"""Loads pretrianed weights to model. Features:: - Incompatible layers (unmatched in name or size) will be ignored. - Can automatically deal with keys containing "module.". Args: model (nn.Module): network model. weight_path (str): path to pretrained weights. Examples:: >>> from torchreid.utils import load_pretrained_weights >>> weight_path = 'log/my_model/model-best.pth.tar' >>> load_pretrained_weights(model, weight_path) """ checkpoint = load_checkpoint(weight_path) if 'state_dict' in checkpoint: state_dict = checkpoint['state_dict'] else: state_dict = checkpoint model_dict = model.state_dict() new_state_dict = OrderedDict() matched_layers, discarded_layers = [], [] for k, v in state_dict.items(): if k.startswith('module.'): k = k[7:] # discard module. if k in model_dict and model_dict[k].size() == v.size(): new_state_dict[k] = v matched_layers.append(k) else: discarded_layers.append(k) model_dict.update(new_state_dict) model.load_state_dict(model_dict) if len(matched_layers) == 0: warnings.warn( 'The pretrained weights "{}" cannot be loaded, ' 'please check the key names manually ' '(** ignored and continue **)'.format(weight_path) ) else: print( 'Successfully loaded pretrained weights from "{}"'. format(weight_path) ) if len(discarded_layers) > 0: print( '** The following layers are discarded ' 'due to unmatched keys or layer size: {}'. format(discarded_layers) ) ================================================ FILE: asone/trackers/strong_sort/tracker/deep/reid_model_factory.py ================================================ __model_types = [ 'resnet50', 'mlfn', 'hacnn', 'mobilenetv2_x1_0', 'mobilenetv2_x1_4', 'osnet_x1_0', 'osnet_x0_75', 'osnet_x0_5', 'osnet_x0_25', 'osnet_ibn_x1_0', 'osnet_ain_x1_0'] __trained_urls = { # market1501 models ######################################################## 'resnet50_market1501.pt': 'https://drive.google.com/uc?id=1dUUZ4rHDWohmsQXCRe2C_HbYkzz94iBV', 'resnet50_dukemtmcreid.pt': 'https://drive.google.com/uc?id=17ymnLglnc64NRvGOitY3BqMRS9UWd1wg', 'resnet50_msmt17.pt': 'https://drive.google.com/uc?id=1ep7RypVDOthCRIAqDnn4_N-UhkkFHJsj', 'resnet50_fc512_market1501.pt': 'https://drive.google.com/uc?id=1kv8l5laX_YCdIGVCetjlNdzKIA3NvsSt', 'resnet50_fc512_dukemtmcreid.pt': 'https://drive.google.com/uc?id=13QN8Mp3XH81GK4BPGXobKHKyTGH50Rtx', 'resnet50_fc512_msmt17.pt': 'https://drive.google.com/uc?id=1fDJLcz4O5wxNSUvImIIjoaIF9u1Rwaud', 'mlfn_market1501.pt': 'https://drive.google.com/uc?id=1wXcvhA_b1kpDfrt9s2Pma-MHxtj9pmvS', 'mlfn_dukemtmcreid.pt': 'https://drive.google.com/uc?id=1rExgrTNb0VCIcOnXfMsbwSUW1h2L1Bum', 'mlfn_msmt17.pt': 'https://drive.google.com/uc?id=18JzsZlJb3Wm7irCbZbZ07TN4IFKvR6p-', 'hacnn_market1501.pt': 'https://drive.google.com/uc?id=1LRKIQduThwGxMDQMiVkTScBwR7WidmYF', 'hacnn_dukemtmcreid.pt': 'https://drive.google.com/uc?id=1zNm6tP4ozFUCUQ7Sv1Z98EAJWXJEhtYH', 'hacnn_msmt17.pt': 'https://drive.google.com/uc?id=1MsKRtPM5WJ3_Tk2xC0aGOO7pM3VaFDNZ', 'mobilenetv2_x1_0_market1501.pt': 'https://drive.google.com/uc?id=18DgHC2ZJkjekVoqBWszD8_Xiikz-fewp', 'mobilenetv2_x1_0_dukemtmcreid.pt': 'https://drive.google.com/uc?id=1q1WU2FETRJ3BXcpVtfJUuqq4z3psetds', 'mobilenetv2_x1_0_msmt17.pt': 'https://drive.google.com/uc?id=1j50Hv14NOUAg7ZeB3frzfX-WYLi7SrhZ', 'mobilenetv2_x1_4_market1501.pt': 'https://drive.google.com/uc?id=1t6JCqphJG-fwwPVkRLmGGyEBhGOf2GO5', 'mobilenetv2_x1_4_dukemtmcreid.pt': 'https://drive.google.com/uc?id=12uD5FeVqLg9-AFDju2L7SQxjmPb4zpBN', 'mobilenetv2_x1_4_msmt17.pt': 'https://drive.google.com/uc?id=1ZY5P2Zgm-3RbDpbXM0kIBMPvspeNIbXz', 'osnet_x1_0_market1501.pt': 'https://drive.google.com/uc?id=1vduhq5DpN2q1g4fYEZfPI17MJeh9qyrA', 'osnet_x1_0_dukemtmcreid.pt': 'https://drive.google.com/uc?id=1QZO_4sNf4hdOKKKzKc-TZU9WW1v6zQbq', 'osnet_x1_0_msmt17.pt': 'https://drive.google.com/uc?id=112EMUfBPYeYg70w-syK6V6Mx8-Qb9Q1M', 'osnet_x0_75_market1501.pt': 'https://drive.google.com/uc?id=1ozRaDSQw_EQ8_93OUmjDbvLXw9TnfPer', 'osnet_x0_75_dukemtmcreid.pt': 'https://drive.google.com/uc?id=1IE3KRaTPp4OUa6PGTFL_d5_KQSJbP0Or', 'osnet_x0_75_msmt17.pt': 'https://drive.google.com/uc?id=1QEGO6WnJ-BmUzVPd3q9NoaO_GsPNlmWc', 'osnet_x0_5_market1501.pt': 'https://drive.google.com/uc?id=1PLB9rgqrUM7blWrg4QlprCuPT7ILYGKT', 'osnet_x0_5_dukemtmcreid.pt': 'https://drive.google.com/uc?id=1KoUVqmiST175hnkALg9XuTi1oYpqcyTu', 'osnet_x0_5_msmt17.pt': 'https://drive.google.com/uc?id=1UT3AxIaDvS2PdxzZmbkLmjtiqq7AIKCv', 'osnet_x0_25_market1501.pt': 'https://drive.google.com/uc?id=1z1UghYvOTtjx7kEoRfmqSMu-z62J6MAj', 'osnet_x0_25_dukemtmcreid.pt': 'https://drive.google.com/uc?id=1eumrtiXT4NOspjyEV4j8cHmlOaaCGk5l', 'osnet_x0_25_msmt17.pt': 'https://drive.google.com/uc?id=1sSwXSUlj4_tHZequ_iZ8w_Jh0VaRQMqF', ####### market1501 models ################################################## 'resnet50_msmt17.pt': 'https://drive.google.com/uc?id=1yiBteqgIZoOeywE8AhGmEQl7FTVwrQmf', 'osnet_x1_0_msmt17.pt': 'https://drive.google.com/uc?id=1IosIFlLiulGIjwW3H8uMRmx3MzPwf86x', 'osnet_x0_75_msmt17.pt': 'https://drive.google.com/uc?id=1fhjSS_7SUGCioIf2SWXaRGPqIY9j7-uw', 'osnet_x0_5_msmt17.pt': 'https://drive.google.com/uc?id=1DHgmb6XV4fwG3n-CnCM0zdL9nMsZ9_RF', 'osnet_x0_25_msmt17.pt': 'https://drive.google.com/uc?id=1Kkx2zW89jq_NETu4u42CFZTMVD5Hwm6e', 'osnet_ibn_x1_0_msmt17.pt': 'https://drive.google.com/uc?id=1q3Sj2ii34NlfxA4LvmHdWO_75NDRmECJ', 'osnet_ain_x1_0_msmt17.pt': 'https://drive.google.com/uc?id=1SigwBE6mPdqiJMqhuIY4aqC7--5CsMal', } def show_downloadeable_models(): print('\nAvailable ReID models for automatic download') print(list(__trained_urls.keys())) def get_model_url(model): model = str(model).rsplit('/', 1)[-1] if model in __trained_urls: return __trained_urls[model] else: None def is_model_in_model_types(model): model = str(model).rsplit('/', 1)[-1].split('.')[0] if model in __model_types: return True else: return False def get_model_name(model): model = str(model).rsplit('/', 1)[-1].split('.')[0] for x in __model_types: if x in model: return x return None ================================================ FILE: asone/trackers/strong_sort/tracker/sort/__init__.py ================================================ ================================================ FILE: asone/trackers/strong_sort/tracker/sort/detection.py ================================================ # vim: expandtab:ts=4:sw=4 import numpy as np class Detection(object): """ This class represents a bounding box detection in a single image. Parameters ---------- tlwh : array_like Bounding box in format `(x, y, w, h)`. confidence : float Detector confidence score. feature : array_like A feature vector that describes the object contained in this image. Attributes ---------- tlwh : ndarray Bounding box in format `(top left x, top left y, width, height)`. confidence : ndarray Detector confidence score. feature : ndarray | NoneType A feature vector that describes the object contained in this image. """ def __init__(self, tlwh, confidence, feature): self.tlwh = np.asarray(tlwh, dtype=np.float) self.confidence = float(confidence) self.feature = np.asarray(feature.cpu(), dtype=np.float32) def to_tlbr(self): """Convert bounding box to format `(min x, min y, max x, max y)`, i.e., `(top left, bottom right)`. """ ret = self.tlwh.copy() ret[2:] += ret[:2] return ret def to_xyah(self): """Convert bounding box to format `(center x, center y, aspect ratio, height)`, where the aspect ratio is `width / height`. """ ret = self.tlwh.copy() ret[:2] += ret[2:] / 2 ret[2] /= ret[3] return ret ================================================ FILE: asone/trackers/strong_sort/tracker/sort/iou_matching.py ================================================ # vim: expandtab:ts=4:sw=4 from __future__ import absolute_import import numpy as np from . import linear_assignment def iou(bbox, candidates): """Computer intersection over union. Parameters ---------- bbox : ndarray A bounding box in format `(top left x, top left y, width, height)`. candidates : ndarray A matrix of candidate bounding boxes (one per row) in the same format as `bbox`. Returns ------- ndarray The intersection over union in [0, 1] between the `bbox` and each candidate. A higher score means a larger fraction of the `bbox` is occluded by the candidate. """ bbox_tl, bbox_br = bbox[:2], bbox[:2] + bbox[2:] candidates_tl = candidates[:, :2] candidates_br = candidates[:, :2] + candidates[:, 2:] tl = np.c_[np.maximum(bbox_tl[0], candidates_tl[:, 0])[:, np.newaxis], np.maximum(bbox_tl[1], candidates_tl[:, 1])[:, np.newaxis]] br = np.c_[np.minimum(bbox_br[0], candidates_br[:, 0])[:, np.newaxis], np.minimum(bbox_br[1], candidates_br[:, 1])[:, np.newaxis]] wh = np.maximum(0., br - tl) area_intersection = wh.prod(axis=1) area_bbox = bbox[2:].prod() area_candidates = candidates[:, 2:].prod(axis=1) return area_intersection / (area_bbox + area_candidates - area_intersection) def iou_cost(tracks, detections, track_indices=None, detection_indices=None): """An intersection over union distance metric. Parameters ---------- tracks : List[deep_sort.track.Track] A list of tracks. detections : List[deep_sort.detection.Detection] A list of detections. track_indices : Optional[List[int]] A list of indices to tracks that should be matched. Defaults to all `tracks`. detection_indices : Optional[List[int]] A list of indices to detections that should be matched. Defaults to all `detections`. Returns ------- ndarray Returns a cost matrix of shape len(track_indices), len(detection_indices) where entry (i, j) is `1 - iou(tracks[track_indices[i]], detections[detection_indices[j]])`. """ if track_indices is None: track_indices = np.arange(len(tracks)) if detection_indices is None: detection_indices = np.arange(len(detections)) cost_matrix = np.zeros((len(track_indices), len(detection_indices))) for row, track_idx in enumerate(track_indices): if tracks[track_idx].time_since_update > 1: cost_matrix[row, :] = linear_assignment.INFTY_COST continue bbox = tracks[track_idx].to_tlwh() candidates = np.asarray( [detections[i].tlwh for i in detection_indices]) cost_matrix[row, :] = 1. - iou(bbox, candidates) return cost_matrix ================================================ FILE: asone/trackers/strong_sort/tracker/sort/kalman_filter.py ================================================ # vim: expandtab:ts=4:sw=4 import numpy as np import scipy.linalg """ Table for the 0.95 quantile of the chi-square distribution with N degrees of freedom (contains values for N=1, ..., 9). Taken from MATLAB/Octave's chi2inv function and used as Mahalanobis gating threshold. """ chi2inv95 = { 1: 3.8415, 2: 5.9915, 3: 7.8147, 4: 9.4877, 5: 11.070, 6: 12.592, 7: 14.067, 8: 15.507, 9: 16.919} class KalmanFilter(object): """ A simple Kalman filter for tracking bounding boxes in image space. The 8-dimensional state space x, y, a, h, vx, vy, va, vh contains the bounding box center position (x, y), aspect ratio a, height h, and their respective velocities. Object motion follows a constant velocity model. The bounding box location (x, y, a, h) is taken as direct observation of the state space (linear observation model). """ def __init__(self): ndim, dt = 4, 1. # Create Kalman filter model matrices. self._motion_mat = np.eye(2 * ndim, 2 * ndim) for i in range(ndim): self._motion_mat[i, ndim + i] = dt self._update_mat = np.eye(ndim, 2 * ndim) # Motion and observation uncertainty are chosen relative to the current # state estimate. These weights control the amount of uncertainty in # the model. This is a bit hacky. self._std_weight_position = 1. / 20 self._std_weight_velocity = 1. / 160 def initiate(self, measurement): """Create track from unassociated measurement. Parameters ---------- measurement : ndarray Bounding box coordinates (x, y, a, h) with center position (x, y), aspect ratio a, and height h. Returns ------- (ndarray, ndarray) Returns the mean vector (8 dimensional) and covariance matrix (8x8 dimensional) of the new track. Unobserved velocities are initialized to 0 mean. """ mean_pos = measurement mean_vel = np.zeros_like(mean_pos) mean = np.r_[mean_pos, mean_vel] std = [ 2 * self._std_weight_position * measurement[0], # the center point x 2 * self._std_weight_position * measurement[1], # the center point y 1 * measurement[2], # the ratio of width/height 2 * self._std_weight_position * measurement[3], # the height 10 * self._std_weight_velocity * measurement[0], 10 * self._std_weight_velocity * measurement[1], 0.1 * measurement[2], 10 * self._std_weight_velocity * measurement[3]] covariance = np.diag(np.square(std)) return mean, covariance def predict(self, mean, covariance): """Run Kalman filter prediction step. Parameters ---------- mean : ndarray The 8 dimensional mean vector of the object state at the previous time step. covariance : ndarray The 8x8 dimensional covariance matrix of the object state at the previous time step. Returns ------- (ndarray, ndarray) Returns the mean vector and covariance matrix of the predicted state. Unobserved velocities are initialized to 0 mean. """ std_pos = [ self._std_weight_position * mean[0], self._std_weight_position * mean[1], 1 * mean[2], self._std_weight_position * mean[3]] std_vel = [ self._std_weight_velocity * mean[0], self._std_weight_velocity * mean[1], 0.1 * mean[2], self._std_weight_velocity * mean[3]] motion_cov = np.diag(np.square(np.r_[std_pos, std_vel])) mean = np.dot(self._motion_mat, mean) covariance = np.linalg.multi_dot(( self._motion_mat, covariance, self._motion_mat.T)) + motion_cov return mean, covariance def project(self, mean, covariance, confidence=.0): """Project state distribution to measurement space. Parameters ---------- mean : ndarray The state's mean vector (8 dimensional array). covariance : ndarray The state's covariance matrix (8x8 dimensional). confidence: (dyh) 检测框置信度 Returns ------- (ndarray, ndarray) Returns the projected mean and covariance matrix of the given state estimate. """ std = [ self._std_weight_position * mean[3], self._std_weight_position * mean[3], 1e-1, self._std_weight_position * mean[3]] std = [(1 - confidence) * x for x in std] innovation_cov = np.diag(np.square(std)) mean = np.dot(self._update_mat, mean) covariance = np.linalg.multi_dot(( self._update_mat, covariance, self._update_mat.T)) return mean, covariance + innovation_cov def update(self, mean, covariance, measurement, confidence=.0): """Run Kalman filter correction step. Parameters ---------- mean : ndarray The predicted state's mean vector (8 dimensional). covariance : ndarray The state's covariance matrix (8x8 dimensional). measurement : ndarray The 4 dimensional measurement vector (x, y, a, h), where (x, y) is the center position, a the aspect ratio, and h the height of the bounding box. confidence: (dyh)检测框置信度 Returns ------- (ndarray, ndarray) Returns the measurement-corrected state distribution. """ projected_mean, projected_cov = self.project(mean, covariance, confidence) chol_factor, lower = scipy.linalg.cho_factor( projected_cov, lower=True, check_finite=False) kalman_gain = scipy.linalg.cho_solve( (chol_factor, lower), np.dot(covariance, self._update_mat.T).T, check_finite=False).T innovation = measurement - projected_mean new_mean = mean + np.dot(innovation, kalman_gain.T) new_covariance = covariance - np.linalg.multi_dot(( kalman_gain, projected_cov, kalman_gain.T)) return new_mean, new_covariance def gating_distance(self, mean, covariance, measurements, only_position=False): """Compute gating distance between state distribution and measurements. A suitable distance threshold can be obtained from `chi2inv95`. If `only_position` is False, the chi-square distribution has 4 degrees of freedom, otherwise 2. Parameters ---------- mean : ndarray Mean vector over the state distribution (8 dimensional). covariance : ndarray Covariance of the state distribution (8x8 dimensional). measurements : ndarray An Nx4 dimensional matrix of N measurements, each in format (x, y, a, h) where (x, y) is the bounding box center position, a the aspect ratio, and h the height. only_position : Optional[bool] If True, distance computation is done with respect to the bounding box center position only. Returns ------- ndarray Returns an array of length N, where the i-th element contains the squared Mahalanobis distance between (mean, covariance) and `measurements[i]`. """ mean, covariance = self.project(mean, covariance) if only_position: mean, covariance = mean[:2], covariance[:2, :2] measurements = measurements[:, :2] cholesky_factor = np.linalg.cholesky(covariance) d = measurements - mean z = scipy.linalg.solve_triangular( cholesky_factor, d.T, lower=True, check_finite=False, overwrite_b=True) squared_maha = np.sum(z * z, axis=0) return squared_maha ================================================ FILE: asone/trackers/strong_sort/tracker/sort/linear_assignment.py ================================================ # vim: expandtab:ts=4:sw=4 from __future__ import absolute_import import numpy as np from scipy.optimize import linear_sum_assignment from . import kalman_filter INFTY_COST = 1e+5 def min_cost_matching( distance_metric, max_distance, tracks, detections, track_indices=None, detection_indices=None): """Solve linear assignment problem. Parameters ---------- distance_metric : Callable[List[Track], List[Detection], List[int], List[int]) -> ndarray The distance metric is given a list of tracks and detections as well as a list of N track indices and M detection indices. The metric should return the NxM dimensional cost matrix, where element (i, j) is the association cost between the i-th track in the given track indices and the j-th detection in the given detection_indices. max_distance : float Gating threshold. Associations with cost larger than this value are disregarded. tracks : List[track.Track] A list of predicted tracks at the current time step. detections : List[detection.Detection] A list of detections at the current time step. track_indices : List[int] List of track indices that maps rows in `cost_matrix` to tracks in `tracks` (see description above). detection_indices : List[int] List of detection indices that maps columns in `cost_matrix` to detections in `detections` (see description above). Returns ------- (List[(int, int)], List[int], List[int]) Returns a tuple with the following three entries: * A list of matched track and detection indices. * A list of unmatched track indices. * A list of unmatched detection indices. """ if track_indices is None: track_indices = np.arange(len(tracks)) if detection_indices is None: detection_indices = np.arange(len(detections)) if len(detection_indices) == 0 or len(track_indices) == 0: return [], track_indices, detection_indices # Nothing to match. cost_matrix = distance_metric( tracks, detections, track_indices, detection_indices) cost_matrix[cost_matrix > max_distance] = max_distance + 1e-5 row_indices, col_indices = linear_sum_assignment(cost_matrix) matches, unmatched_tracks, unmatched_detections = [], [], [] for col, detection_idx in enumerate(detection_indices): if col not in col_indices: unmatched_detections.append(detection_idx) for row, track_idx in enumerate(track_indices): if row not in row_indices: unmatched_tracks.append(track_idx) for row, col in zip(row_indices, col_indices): track_idx = track_indices[row] detection_idx = detection_indices[col] if cost_matrix[row, col] > max_distance: unmatched_tracks.append(track_idx) unmatched_detections.append(detection_idx) else: matches.append((track_idx, detection_idx)) return matches, unmatched_tracks, unmatched_detections def matching_cascade( distance_metric, max_distance, cascade_depth, tracks, detections, track_indices=None, detection_indices=None): """Run matching cascade. Parameters ---------- distance_metric : Callable[List[Track], List[Detection], List[int], List[int]) -> ndarray The distance metric is given a list of tracks and detections as well as a list of N track indices and M detection indices. The metric should return the NxM dimensional cost matrix, where element (i, j) is the association cost between the i-th track in the given track indices and the j-th detection in the given detection indices. max_distance : float Gating threshold. Associations with cost larger than this value are disregarded. cascade_depth: int The cascade depth, should be se to the maximum track age. tracks : List[track.Track] A list of predicted tracks at the current time step. detections : List[detection.Detection] A list of detections at the current time step. track_indices : Optional[List[int]] List of track indices that maps rows in `cost_matrix` to tracks in `tracks` (see description above). Defaults to all tracks. detection_indices : Optional[List[int]] List of detection indices that maps columns in `cost_matrix` to detections in `detections` (see description above). Defaults to all detections. Returns ------- (List[(int, int)], List[int], List[int]) Returns a tuple with the following three entries: * A list of matched track and detection indices. * A list of unmatched track indices. * A list of unmatched detection indices. """ if track_indices is None: track_indices = list(range(len(tracks))) if detection_indices is None: detection_indices = list(range(len(detections))) unmatched_detections = detection_indices matches = [] track_indices_l = [ k for k in track_indices # if tracks[k].time_since_update == 1 + level ] matches_l, _, unmatched_detections = \ min_cost_matching( distance_metric, max_distance, tracks, detections, track_indices_l, unmatched_detections) matches += matches_l unmatched_tracks = list(set(track_indices) - set(k for k, _ in matches)) return matches, unmatched_tracks, unmatched_detections def gate_cost_matrix( cost_matrix, tracks, detections, track_indices, detection_indices, gated_cost=INFTY_COST, only_position=False): """Invalidate infeasible entries in cost matrix based on the state distributions obtained by Kalman filtering. Parameters ---------- kf : The Kalman filter. cost_matrix : ndarray The NxM dimensional cost matrix, where N is the number of track indices and M is the number of detection indices, such that entry (i, j) is the association cost between `tracks[track_indices[i]]` and `detections[detection_indices[j]]`. tracks : List[track.Track] A list of predicted tracks at the current time step. detections : List[detection.Detection] A list of detections at the current time step. track_indices : List[int] List of track indices that maps rows in `cost_matrix` to tracks in `tracks` (see description above). detection_indices : List[int] List of detection indices that maps columns in `cost_matrix` to detections in `detections` (see description above). gated_cost : Optional[float] Entries in the cost matrix corresponding to infeasible associations are set this value. Defaults to a very large value. only_position : Optional[bool] If True, only the x, y position of the state distribution is considered during gating. Defaults to False. Returns ------- ndarray Returns the modified cost matrix. """ gating_dim = 2 if only_position else 4 gating_threshold = kalman_filter.chi2inv95[gating_dim] measurements = np.asarray( [detections[i].to_xyah() for i in detection_indices]) for row, track_idx in enumerate(track_indices): track = tracks[track_idx] gating_distance = track.kf.gating_distance(track.mean, track.covariance, measurements, only_position) cost_matrix[row, gating_distance > gating_threshold] = gated_cost cost_matrix[row] = 0.995 * cost_matrix[row] + (1 - 0.995) * gating_distance return cost_matrix ================================================ FILE: asone/trackers/strong_sort/tracker/sort/nn_matching.py ================================================ # vim: expandtab:ts=4:sw=4 import sys import numpy as np import torch def _pdist(a, b): """Compute pair-wise squared distance between points in `a` and `b`. Parameters ---------- a : array_like An NxM matrix of N samples of dimensionality M. b : array_like An LxM matrix of L samples of dimensionality M. Returns ------- ndarray Returns a matrix of size len(a), len(b) such that eleement (i, j) contains the squared distance between `a[i]` and `b[j]`. """ a, b = np.asarray(a), np.asarray(b) if len(a) == 0 or len(b) == 0: return np.zeros((len(a), len(b))) a2, b2 = np.square(a).sum(axis=1), np.square(b).sum(axis=1) r2 = -2.0 * np.dot(a, b.T) + a2[:, None] + b2[None, :] r2 = np.clip(r2, 0.0, float(np.inf)) return r2 def _cosine_distance(a, b, data_is_normalized=False): """Compute pair-wise cosine distance between points in `a` and `b`. Parameters ---------- a : array_like An NxM matrix of N samples of dimensionality M. b : array_like An LxM matrix of L samples of dimensionality M. data_is_normalized : Optional[bool] If True, assumes rows in a and b are unit length vectors. Otherwise, a and b are explicitly normalized to lenght 1. Returns ------- ndarray Returns a matrix of size len(a), len(b) such that eleement (i, j) contains the squared distance between `a[i]` and `b[j]`. """ if not data_is_normalized: a = np.asarray(a) / np.linalg.norm(a, axis=1, keepdims=True) b = np.asarray(b) / np.linalg.norm(b, axis=1, keepdims=True) return 1.0 - np.dot(a, b.T) def _nn_euclidean_distance(x, y): """Helper function for nearest neighbor distance metric (Euclidean). Parameters ---------- x : ndarray A matrix of N row-vectors (sample points). y : ndarray A matrix of M row-vectors (query points). Returns ------- ndarray A vector of length M that contains for each entry in `y` the smallest Euclidean distance to a sample in `x`. """ # x_ = torch.from_numpy(np.asarray(x) / np.linalg.norm(x, axis=1, keepdims=True)) # y_ = torch.from_numpy(np.asarray(y) / np.linalg.norm(y, axis=1, keepdims=True)) distances = distances = _pdist(x, y) return np.maximum(0.0, torch.min(distances, axis=0)[0].numpy()) def _nn_cosine_distance(x, y): """Helper function for nearest neighbor distance metric (cosine). Parameters ---------- x : ndarray A matrix of N row-vectors (sample points). y : ndarray A matrix of M row-vectors (query points). Returns ------- ndarray A vector of length M that contains for each entry in `y` the smallest cosine distance to a sample in `x`. """ x_ = torch.from_numpy(np.asarray(x)) y_ = torch.from_numpy(np.asarray(y)) distances = _cosine_distance(x_, y_) distances = distances return distances.min(axis=0) class NearestNeighborDistanceMetric(object): """ A nearest neighbor distance metric that, for each target, returns the closest distance to any sample that has been observed so far. Parameters ---------- metric : str Either "euclidean" or "cosine". matching_threshold: float The matching threshold. Samples with larger distance are considered an invalid match. budget : Optional[int] If not None, fix samples per class to at most this number. Removes the oldest samples when the budget is reached. Attributes ---------- samples : Dict[int -> List[ndarray]] A dictionary that maps from target identities to the list of samples that have been observed so far. """ def __init__(self, metric, matching_threshold, budget=None): if metric == "euclidean": self._metric = _nn_euclidean_distance elif metric == "cosine": self._metric = _nn_cosine_distance else: raise ValueError("Invalid metric; must be either 'euclidean' or 'cosine'") self.matching_threshold = matching_threshold self.budget = budget self.samples = {} def partial_fit(self, features, targets, active_targets): """Update the distance metric with new data. Parameters ---------- features : ndarray An NxM matrix of N features of dimensionality M. targets : ndarray An integer array of associated target identities. active_targets : List[int] A list of targets that are currently present in the scene. """ for feature, target in zip(features, targets): self.samples.setdefault(target, []).append(feature) if self.budget is not None: self.samples[target] = self.samples[target][-self.budget :] self.samples = {k: self.samples[k] for k in active_targets} def distance(self, features, targets): """Compute distance between features and targets. Parameters ---------- features : ndarray An NxM matrix of N features of dimensionality M. targets : List[int] A list of targets to match the given `features` against. Returns ------- ndarray Returns a cost matrix of shape len(targets), len(features), where element (i, j) contains the closest squared distance between `targets[i]` and `features[j]`. """ cost_matrix = np.zeros((len(targets), len(features))) for i, target in enumerate(targets): cost_matrix[i, :] = self._metric(self.samples[target], features) return cost_matrix ================================================ FILE: asone/trackers/strong_sort/tracker/sort/preprocessing.py ================================================ # vim: expandtab:ts=4:sw=4 import numpy as np import cv2 def non_max_suppression(boxes, max_bbox_overlap, scores=None): """Suppress overlapping detections. Original code from [1]_ has been adapted to include confidence score. .. [1] http://www.pyimagesearch.com/2015/02/16/ faster-non-maximum-suppression-python/ Examples -------- >>> boxes = [d.roi for d in detections] >>> scores = [d.confidence for d in detections] >>> indices = non_max_suppression(boxes, max_bbox_overlap, scores) >>> detections = [detections[i] for i in indices] Parameters ---------- boxes : ndarray Array of ROIs (x, y, width, height). max_bbox_overlap : float ROIs that overlap more than this values are suppressed. scores : Optional[array_like] Detector confidence score. Returns ------- List[int] Returns indices of detections that have survived non-maxima suppression. """ if len(boxes) == 0: return [] boxes = boxes.astype(np.float) pick = [] x1 = boxes[:, 0] y1 = boxes[:, 1] x2 = boxes[:, 2] + boxes[:, 0] y2 = boxes[:, 3] + boxes[:, 1] area = (x2 - x1 + 1) * (y2 - y1 + 1) if scores is not None: idxs = np.argsort(scores) else: idxs = np.argsort(y2) while len(idxs) > 0: last = len(idxs) - 1 i = idxs[last] pick.append(i) xx1 = np.maximum(x1[i], x1[idxs[:last]]) yy1 = np.maximum(y1[i], y1[idxs[:last]]) xx2 = np.minimum(x2[i], x2[idxs[:last]]) yy2 = np.minimum(y2[i], y2[idxs[:last]]) w = np.maximum(0, xx2 - xx1 + 1) h = np.maximum(0, yy2 - yy1 + 1) overlap = (w * h) / area[idxs[:last]] idxs = np.delete( idxs, np.concatenate( ([last], np.where(overlap > max_bbox_overlap)[0]))) return pick ================================================ FILE: asone/trackers/strong_sort/tracker/sort/track.py ================================================ # vim: expandtab:ts=4:sw=4 import cv2 import numpy as np from asone.trackers.strong_sort.tracker.sort.kalman_filter import KalmanFilter class TrackState: """ Enumeration type for the single target track state. Newly created tracks are classified as `tentative` until enough evidence has been collected. Then, the track state is changed to `confirmed`. Tracks that are no longer alive are classified as `deleted` to mark them for removal from the set of active tracks. """ Tentative = 1 Confirmed = 2 Deleted = 3 class Track: """ A single target track with state space `(x, y, a, h)` and associated velocities, where `(x, y)` is the center of the bounding box, `a` is the aspect ratio and `h` is the height. Parameters ---------- mean : ndarray Mean vector of the initial state distribution. covariance : ndarray Covariance matrix of the initial state distribution. track_id : int A unique track identifier. n_init : int Number of consecutive detections before the track is confirmed. The track state is set to `Deleted` if a miss occurs within the first `n_init` frames. max_age : int The maximum number of consecutive misses before the track state is set to `Deleted`. feature : Optional[ndarray] Feature vector of the detection this track originates from. If not None, this feature is added to the `features` cache. Attributes ---------- mean : ndarray Mean vector of the initial state distribution. covariance : ndarray Covariance matrix of the initial state distribution. track_id : int A unique track identifier. hits : int Total number of measurement updates. age : int Total number of frames since first occurance. time_since_update : int Total number of frames since last measurement update. state : TrackState The current track state. features : List[ndarray] A cache of features. On each measurement update, the associated feature vector is added to this list. """ def __init__(self, detection, track_id, class_id, conf, n_init, max_age, ema_alpha, feature=None): self.track_id = track_id self.class_id = int(class_id) self.hits = 1 self.age = 1 self.time_since_update = 0 self.ema_alpha = ema_alpha self.state = TrackState.Tentative self.features = [] if feature is not None: feature /= np.linalg.norm(feature) self.features.append(feature) self.conf = conf self._n_init = n_init self._max_age = max_age self.kf = KalmanFilter() self.mean, self.covariance = self.kf.initiate(detection) def to_tlwh(self): """Get current position in bounding box format `(top left x, top left y, width, height)`. Returns ------- ndarray The bounding box. """ ret = self.mean[:4].copy() ret[2] *= ret[3] ret[:2] -= ret[2:] / 2 return ret def to_tlbr(self): """Get kf estimated current position in bounding box format `(min x, miny, max x, max y)`. Returns ------- ndarray The predicted kf bounding box. """ ret = self.to_tlwh() ret[2:] = ret[:2] + ret[2:] return ret def ECC(self, src, dst, warp_mode = cv2.MOTION_EUCLIDEAN, eps = 1e-5, max_iter = 100, scale = 0.1, align = False): """Compute the warp matrix from src to dst. Parameters ---------- src : ndarray An NxM matrix of source img(BGR or Gray), it must be the same format as dst. dst : ndarray An NxM matrix of target img(BGR or Gray). warp_mode: flags of opencv translation: cv2.MOTION_TRANSLATION rotated and shifted: cv2.MOTION_EUCLIDEAN affine(shift,rotated,shear): cv2.MOTION_AFFINE homography(3d): cv2.MOTION_HOMOGRAPHY eps: float the threshold of the increment in the correlation coefficient between two iterations max_iter: int the number of iterations. scale: float or [int, int] scale_ratio: float scale_size: [W, H] align: bool whether to warp affine or perspective transforms to the source image Returns ------- warp matrix : ndarray Returns the warp matrix from src to dst. if motion models is homography, the warp matrix will be 3x3, otherwise 2x3 src_aligned: ndarray aligned source image of gray """ # skip if current and previous frame are not initialized (1st inference) if (src.any() or dst.any() is None): return None, None # skip if current and previous fames are not the same size elif (src.shape != dst.shape): return None, None # BGR2GRAY if src.ndim == 3: # Convert images to grayscale src = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) dst = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY) # make the imgs smaller to speed up if scale is not None: if isinstance(scale, float) or isinstance(scale, int): if scale != 1: src_r = cv2.resize(src, (0, 0), fx = scale, fy = scale,interpolation = cv2.INTER_LINEAR) dst_r = cv2.resize(dst, (0, 0), fx = scale, fy = scale,interpolation = cv2.INTER_LINEAR) scale = [scale, scale] else: src_r, dst_r = src, dst scale = None else: if scale[0] != src.shape[1] and scale[1] != src.shape[0]: src_r = cv2.resize(src, (scale[0], scale[1]), interpolation = cv2.INTER_LINEAR) dst_r = cv2.resize(dst, (scale[0], scale[1]), interpolation=cv2.INTER_LINEAR) scale = [scale[0] / src.shape[1], scale[1] / src.shape[0]] else: src_r, dst_r = src, dst scale = None else: src_r, dst_r = src, dst # Define 2x3 or 3x3 matrices and initialize the matrix to identity if warp_mode == cv2.MOTION_HOMOGRAPHY : warp_matrix = np.eye(3, 3, dtype=np.float32) else : warp_matrix = np.eye(2, 3, dtype=np.float32) # Define termination criteria criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, max_iter, eps) # Run the ECC algorithm. The results are stored in warp_matrix. try: (cc, warp_matrix) = cv2.findTransformECC (src_r, dst_r, warp_matrix, warp_mode, criteria, None, 1) except cv2.error as e: return None, None if scale is not None: warp_matrix[0, 2] = warp_matrix[0, 2] / scale[0] warp_matrix[1, 2] = warp_matrix[1, 2] / scale[1] if align: sz = src.shape if warp_mode == cv2.MOTION_HOMOGRAPHY: # Use warpPerspective for Homography src_aligned = cv2.warpPerspective(src, warp_matrix, (sz[1],sz[0]), flags=cv2.INTER_LINEAR) else : # Use warpAffine for Translation, Euclidean and Affine src_aligned = cv2.warpAffine(src, warp_matrix, (sz[1],sz[0]), flags=cv2.INTER_LINEAR) return warp_matrix, src_aligned else: return warp_matrix, None def get_matrix(self, matrix): eye = np.eye(3) dist = np.linalg.norm(eye - matrix) if dist < 100: return matrix else: return eye def camera_update(self, previous_frame, next_frame): warp_matrix, src_aligned = self.ECC(previous_frame, next_frame) if warp_matrix is None and src_aligned is None: return [a,b] = warp_matrix warp_matrix=np.array([a,b,[0,0,1]]) warp_matrix = warp_matrix.tolist() matrix = self.get_matrix(warp_matrix) x1, y1, x2, y2 = self.to_tlbr() x1_, y1_, _ = matrix @ np.array([x1, y1, 1]).T x2_, y2_, _ = matrix @ np.array([x2, y2, 1]).T w, h = x2_ - x1_, y2_ - y1_ cx, cy = x1_ + w / 2, y1_ + h / 2 self.mean[:4] = [cx, cy, w / h, h] def increment_age(self): self.age += 1 self.time_since_update += 1 def predict(self, kf): """Propagate the state distribution to the current time step using a Kalman filter prediction step. Parameters ---------- kf : kalman_filter.KalmanFilter The Kalman filter. """ self.mean, self.covariance = self.kf.predict(self.mean, self.covariance) self.age += 1 self.time_since_update += 1 def update(self, detection, class_id, conf): """Perform Kalman filter measurement update step and update the feature cache. Parameters ---------- detection : Detection The associated detection. """ self.conf = conf self.class_id = int(class_id) self.mean, self.covariance = self.kf.update(self.mean, self.covariance, detection.to_xyah(), detection.confidence) feature = detection.feature / np.linalg.norm(detection.feature) smooth_feat = self.ema_alpha * self.features[-1] + (1 - self.ema_alpha) * feature smooth_feat /= np.linalg.norm(smooth_feat) self.features = [smooth_feat] self.hits += 1 self.time_since_update = 0 if self.state == TrackState.Tentative and self.hits >= self._n_init: self.state = TrackState.Confirmed def mark_missed(self): """Mark this track as missed (no association at the current time step). """ if self.state == TrackState.Tentative: self.state = TrackState.Deleted elif self.time_since_update > self._max_age: self.state = TrackState.Deleted def is_tentative(self): """Returns True if this track is tentative (unconfirmed). """ return self.state == TrackState.Tentative def is_confirmed(self): """Returns True if this track is confirmed.""" return self.state == TrackState.Confirmed def is_deleted(self): """Returns True if this track is dead and should be deleted.""" return self.state == TrackState.Deleted ================================================ FILE: asone/trackers/strong_sort/tracker/sort/tracker.py ================================================ # vim: expandtab:ts=4:sw=4 from __future__ import absolute_import import numpy as np from . import kalman_filter from . import linear_assignment from . import iou_matching from .track import Track class Tracker: """ This is the multi-target tracker. Parameters ---------- metric : nn_matching.NearestNeighborDistanceMetric A distance metric for measurement-to-track association. max_age : int Maximum number of missed misses before a track is deleted. n_init : int Number of consecutive detections before the track is confirmed. The track state is set to `Deleted` if a miss occurs within the first `n_init` frames. Attributes ---------- metric : nn_matching.NearestNeighborDistanceMetric The distance metric used for measurement to track association. max_age : int Maximum number of missed misses before a track is deleted. n_init : int Number of frames that a track remains in initialization phase. kf : kalman_filter.KalmanFilter A Kalman filter to filter target trajectories in image space. tracks : List[Track] The list of active tracks at the current time step. """ GATING_THRESHOLD = np.sqrt(kalman_filter.chi2inv95[4]) def __init__(self, metric, max_iou_distance=0.9, max_age=30, n_init=3, _lambda=0, ema_alpha=0.9, mc_lambda=0.995): self.metric = metric self.max_iou_distance = max_iou_distance self.max_age = max_age self.n_init = n_init self._lambda = _lambda self.ema_alpha = ema_alpha self.mc_lambda = mc_lambda self.kf = kalman_filter.KalmanFilter() self.tracks = [] self._next_id = 1 def predict(self): """Propagate track state distributions one time step forward. This function should be called once every time step, before `update`. """ for track in self.tracks: track.predict(self.kf) def increment_ages(self): for track in self.tracks: track.increment_age() track.mark_missed() def camera_update(self, previous_img, current_img): for track in self.tracks: track.camera_update(previous_img, current_img) def update(self, detections, classes, confidences): """Perform measurement update and track management. Parameters ---------- detections : List[deep_sort.detection.Detection] A list of detections at the current time step. """ # Run matching cascade. matches, unmatched_tracks, unmatched_detections = \ self._match(detections) # Update track set. for track_idx, detection_idx in matches: self.tracks[track_idx].update( detections[detection_idx], classes[detection_idx], confidences[detection_idx]) for track_idx in unmatched_tracks: self.tracks[track_idx].mark_missed() for detection_idx in unmatched_detections: self._initiate_track(detections[detection_idx], classes[detection_idx].item(), confidences[detection_idx].item()) self.tracks = [t for t in self.tracks if not t.is_deleted()] # Update distance metric. active_targets = [t.track_id for t in self.tracks if t.is_confirmed()] features, targets = [], [] for track in self.tracks: if not track.is_confirmed(): continue features += track.features targets += [track.track_id for _ in track.features] self.metric.partial_fit(np.asarray(features), np.asarray(targets), active_targets) def _full_cost_metric(self, tracks, dets, track_indices, detection_indices): """ This implements the full lambda-based cost-metric. However, in doing so, it disregards the possibility to gate the position only which is provided by linear_assignment.gate_cost_matrix(). Instead, I gate by everything. Note that the Mahalanobis distance is itself an unnormalised metric. Given the cosine distance being normalised, we employ a quick and dirty normalisation based on the threshold: that is, we divide the positional-cost by the gating threshold, thus ensuring that the valid values range 0-1. Note also that the authors work with the squared distance. I also sqrt this, so that it is more intuitive in terms of values. """ # Compute First the Position-based Cost Matrix pos_cost = np.empty([len(track_indices), len(detection_indices)]) msrs = np.asarray([dets[i].to_xyah() for i in detection_indices]) for row, track_idx in enumerate(track_indices): pos_cost[row, :] = np.sqrt( self.kf.gating_distance( tracks[track_idx].mean, tracks[track_idx].covariance, msrs, False ) ) / self.GATING_THRESHOLD pos_gate = pos_cost > 1.0 # Now Compute the Appearance-based Cost Matrix app_cost = self.metric.distance( np.array([dets[i].feature for i in detection_indices]), np.array([tracks[i].track_id for i in track_indices]), ) app_gate = app_cost > self.metric.matching_threshold # Now combine and threshold cost_matrix = self._lambda * pos_cost + (1 - self._lambda) * app_cost cost_matrix[np.logical_or(pos_gate, app_gate)] = linear_assignment.INFTY_COST # Return Matrix return cost_matrix def _match(self, detections): def gated_metric(tracks, dets, track_indices, detection_indices): features = np.array([dets[i].feature for i in detection_indices]) targets = np.array([tracks[i].track_id for i in track_indices]) cost_matrix = self.metric.distance(features, targets) cost_matrix = linear_assignment.gate_cost_matrix(cost_matrix, tracks, dets, track_indices, detection_indices) return cost_matrix # Split track set into confirmed and unconfirmed tracks. confirmed_tracks = [ i for i, t in enumerate(self.tracks) if t.is_confirmed()] unconfirmed_tracks = [ i for i, t in enumerate(self.tracks) if not t.is_confirmed()] # Associate confirmed tracks using appearance features. matches_a, unmatched_tracks_a, unmatched_detections = \ linear_assignment.matching_cascade( gated_metric, self.metric.matching_threshold, self.max_age, self.tracks, detections, confirmed_tracks) # Associate remaining tracks together with unconfirmed tracks using IOU. iou_track_candidates = unconfirmed_tracks + [ k for k in unmatched_tracks_a if self.tracks[k].time_since_update == 1] unmatched_tracks_a = [ k for k in unmatched_tracks_a if self.tracks[k].time_since_update != 1] matches_b, unmatched_tracks_b, unmatched_detections = \ linear_assignment.min_cost_matching( iou_matching.iou_cost, self.max_iou_distance, self.tracks, detections, iou_track_candidates, unmatched_detections) matches = matches_a + matches_b unmatched_tracks = list(set(unmatched_tracks_a + unmatched_tracks_b)) return matches, unmatched_tracks, unmatched_detections def _initiate_track(self, detection, class_id, conf): self.tracks.append(Track( detection.to_xyah(), self._next_id, class_id, conf, self.n_init, self.max_age, self.ema_alpha, detection.feature)) self._next_id += 1 ================================================ FILE: asone/trackers/strong_sort/tracker/strong_sort.py ================================================ import numpy as np import torch import sys import gdown from os.path import exists as file_exists, join from .sort.nn_matching import NearestNeighborDistanceMetric from .sort.detection import Detection from .sort.tracker import Tracker from .deep.reid_model_factory import show_downloadeable_models, get_model_url, get_model_name from torchreid.reid.utils import FeatureExtractor from torchreid.reid.utils.tools import download_url __all__ = ['StrongSORT'] def xyxy2xywh(x): # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = (x[:, 0] + x[:, 2]) / 2 # x center y[:, 1] = (x[:, 1] + x[:, 3]) / 2 # y center y[:, 2] = x[:, 2] - x[:, 0] # width y[:, 3] = x[:, 3] - x[:, 1] # height return y class StrongSORT(object): def __init__(self, model_weights, device, max_dist=0.2, max_iou_distance=0.7, max_age=70, n_init=3, nn_budget=100, mc_lambda=0.995, ema_alpha=0.9 ): model_name = get_model_name(model_weights) model_url = get_model_url(model_weights) if not file_exists(model_weights) and model_url is not None: gdown.download(model_url, str(model_weights), quiet=False) elif file_exists(model_weights): pass elif model_url is None: print('No URL associated to the chosen DeepSort weights. Choose between:') show_downloadeable_models() exit() self.extractor = FeatureExtractor( # get rid of dataset information DeepSort model name model_name=model_name, model_path=model_weights, device=str(device) ) self.max_dist = max_dist metric = NearestNeighborDistanceMetric( "cosine", self.max_dist, nn_budget) self.tracker = Tracker( metric, max_iou_distance=max_iou_distance, max_age=max_age, n_init=n_init) def update(self, bbox_xywh, confs, classes, ori_img): xywhs = xyxy2xywh(bbox_xywh) confs = confs self.height, self.width = ori_img.shape[:2] # generate detections features = self._get_features(xywhs, ori_img) bbox_tlwh = self._xywh_to_tlwh(xywhs) detections = [Detection(bbox_tlwh[i], conf, features[i]) for i, conf in enumerate(confs)] # run on non-maximum supression boxes = np.array([d.tlwh for d in detections]) scores = np.array([d.confidence for d in detections]) # update tracker self.tracker.predict() self.tracker.update(detections, classes, confs) # output bbox identities outputs = [] for track in self.tracker.tracks: if not track.is_confirmed() or track.time_since_update > 1: continue box = track.to_tlwh() x1, y1, x2, y2 = self._tlwh_to_xyxy(box) track_id = track.track_id class_id = track.class_id conf = track.conf outputs.append(np.array([x1, y1, x2, y2, track_id, class_id, conf])) if len(outputs) > 0: outputs = np.stack(outputs, axis=0) return outputs """ TODO: Convert bbox from xc_yc_w_h to xtl_ytl_w_h Thanks JieChen91@github.com for reporting this bug! """ @staticmethod def _xywh_to_tlwh(bbox_xywh): if isinstance(bbox_xywh, np.ndarray): bbox_tlwh = bbox_xywh.copy() elif isinstance(bbox_xywh, torch.Tensor): bbox_tlwh = bbox_xywh.clone() bbox_tlwh[:, 0] = bbox_xywh[:, 0] - bbox_xywh[:, 2] / 2. bbox_tlwh[:, 1] = bbox_xywh[:, 1] - bbox_xywh[:, 3] / 2. return bbox_tlwh def _xywh_to_xyxy(self, bbox_xywh): x, y, w, h = bbox_xywh x1 = max(int(x - w / 2), 0) x2 = min(int(x + w / 2), self.width - 1) y1 = max(int(y - h / 2), 0) y2 = min(int(y + h / 2), self.height - 1) return x1, y1, x2, y2 def _tlwh_to_xyxy(self, bbox_tlwh): """ TODO: Convert bbox from xtl_ytl_w_h to xc_yc_w_h Thanks JieChen91@github.com for reporting this bug! """ x, y, w, h = bbox_tlwh x1 = max(int(x), 0) x2 = min(int(x+w), self.width - 1) y1 = max(int(y), 0) y2 = min(int(y+h), self.height - 1) return x1, y1, x2, y2 def increment_ages(self): self.tracker.increment_ages() def _xyxy_to_tlwh(self, bbox_xyxy): x1, y1, x2, y2 = bbox_xyxy t = x1 l = y1 w = int(x2 - x1) h = int(y2 - y1) return t, l, w, h def _get_features(self, bbox_xywh, ori_img): im_crops = [] for box in bbox_xywh: x1, y1, x2, y2 = self._xywh_to_xyxy(box) im = ori_img[y1:y2, x1:x2] im_crops.append(im) if im_crops: features = self.extractor(im_crops) else: features = np.array([]) return features ================================================ FILE: asone/trackers/strong_sort/tracker/utils/__init__.py ================================================ ================================================ FILE: asone/trackers/strong_sort/tracker/utils/asserts.py ================================================ from os import environ def assert_in(file, files_to_check): if file not in files_to_check: raise AssertionError("{} does not exist in the list".format(str(file))) return True def assert_in_env(check_list: list): for item in check_list: assert_in(item, environ.keys()) return True ================================================ FILE: asone/trackers/strong_sort/tracker/utils/draw.py ================================================ import numpy as np import cv2 palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1) def compute_color_for_labels(label): """ Simple function that adds fixed color depending on the class """ color = [int((p * (label ** 2 - label + 1)) % 255) for p in palette] return tuple(color) def draw_boxes(img, bbox, identities=None, offset=(0,0)): for i,box in enumerate(bbox): x1,y1,x2,y2 = [int(i) for i in box] x1 += offset[0] x2 += offset[0] y1 += offset[1] y2 += offset[1] # box text and bar id = int(identities[i]) if identities is not None else 0 color = compute_color_for_labels(id) label = '{}{:d}'.format("", id) t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 2 , 2)[0] cv2.rectangle(img,(x1, y1),(x2,y2),color,3) cv2.rectangle(img,(x1, y1),(x1+t_size[0]+3,y1+t_size[1]+4), color,-1) cv2.putText(img,label,(x1,y1+t_size[1]+4), cv2.FONT_HERSHEY_PLAIN, 2, [255,255,255], 2) return img if __name__ == '__main__': for i in range(82): print(compute_color_for_labels(i)) ================================================ FILE: asone/trackers/strong_sort/tracker/utils/evaluation.py ================================================ import os import numpy as np import copy import motmetrics as mm mm.lap.default_solver = 'lap' from utils.io import read_results, unzip_objs class Evaluator(object): def __init__(self, data_root, seq_name, data_type): self.data_root = data_root self.seq_name = seq_name self.data_type = data_type self.load_annotations() self.reset_accumulator() def load_annotations(self): assert self.data_type == 'mot' gt_filename = os.path.join(self.data_root, self.seq_name, 'gt', 'gt.txt') self.gt_frame_dict = read_results(gt_filename, self.data_type, is_gt=True) self.gt_ignore_frame_dict = read_results(gt_filename, self.data_type, is_ignore=True) def reset_accumulator(self): self.acc = mm.MOTAccumulator(auto_id=True) def eval_frame(self, frame_id, trk_tlwhs, trk_ids, rtn_events=False): # results trk_tlwhs = np.copy(trk_tlwhs) trk_ids = np.copy(trk_ids) # gts gt_objs = self.gt_frame_dict.get(frame_id, []) gt_tlwhs, gt_ids = unzip_objs(gt_objs)[:2] # ignore boxes ignore_objs = self.gt_ignore_frame_dict.get(frame_id, []) ignore_tlwhs = unzip_objs(ignore_objs)[0] # remove ignored results keep = np.ones(len(trk_tlwhs), dtype=bool) iou_distance = mm.distances.iou_matrix(ignore_tlwhs, trk_tlwhs, max_iou=0.5) if len(iou_distance) > 0: match_is, match_js = mm.lap.linear_sum_assignment(iou_distance) match_is, match_js = map(lambda a: np.asarray(a, dtype=int), [match_is, match_js]) match_ious = iou_distance[match_is, match_js] match_js = np.asarray(match_js, dtype=int) match_js = match_js[np.logical_not(np.isnan(match_ious))] keep[match_js] = False trk_tlwhs = trk_tlwhs[keep] trk_ids = trk_ids[keep] # get distance matrix iou_distance = mm.distances.iou_matrix(gt_tlwhs, trk_tlwhs, max_iou=0.5) # acc self.acc.update(gt_ids, trk_ids, iou_distance) if rtn_events and iou_distance.size > 0 and hasattr(self.acc, 'last_mot_events'): events = self.acc.last_mot_events # only supported by https://github.com/longcw/py-motmetrics else: events = None return events def eval_file(self, filename): self.reset_accumulator() result_frame_dict = read_results(filename, self.data_type, is_gt=False) frames = sorted(list(set(self.gt_frame_dict.keys()) | set(result_frame_dict.keys()))) for frame_id in frames: trk_objs = result_frame_dict.get(frame_id, []) trk_tlwhs, trk_ids = unzip_objs(trk_objs)[:2] self.eval_frame(frame_id, trk_tlwhs, trk_ids, rtn_events=False) return self.acc @staticmethod def get_summary(accs, names, metrics=('mota', 'num_switches', 'idp', 'idr', 'idf1', 'precision', 'recall')): names = copy.deepcopy(names) if metrics is None: metrics = mm.metrics.motchallenge_metrics metrics = copy.deepcopy(metrics) mh = mm.metrics.create() summary = mh.compute_many( accs, metrics=metrics, names=names, generate_overall=True ) return summary @staticmethod def save_summary(summary, filename): import pandas as pd writer = pd.ExcelWriter(filename) summary.to_excel(writer) writer.save() ================================================ FILE: asone/trackers/strong_sort/tracker/utils/io.py ================================================ import os from typing import Dict import numpy as np # from utils.log import get_logger def write_results(filename, results, data_type): if data_type == 'mot': save_format = '{frame},{id},{x1},{y1},{w},{h},-1,-1,-1,-1\n' elif data_type == 'kitti': save_format = '{frame} {id} pedestrian 0 0 -10 {x1} {y1} {x2} {y2} -10 -10 -10 -1000 -1000 -1000 -10\n' else: raise ValueError(data_type) with open(filename, 'w') as f: for frame_id, tlwhs, track_ids in results: if data_type == 'kitti': frame_id -= 1 for tlwh, track_id in zip(tlwhs, track_ids): if track_id < 0: continue x1, y1, w, h = tlwh x2, y2 = x1 + w, y1 + h line = save_format.format(frame=frame_id, id=track_id, x1=x1, y1=y1, x2=x2, y2=y2, w=w, h=h) f.write(line) # def write_results(filename, results_dict: Dict, data_type: str): # if not filename: # return # path = os.path.dirname(filename) # if not os.path.exists(path): # os.makedirs(path) # if data_type in ('mot', 'mcmot', 'lab'): # save_format = '{frame},{id},{x1},{y1},{w},{h},1,-1,-1,-1\n' # elif data_type == 'kitti': # save_format = '{frame} {id} pedestrian -1 -1 -10 {x1} {y1} {x2} {y2} -1 -1 -1 -1000 -1000 -1000 -10 {score}\n' # else: # raise ValueError(data_type) # with open(filename, 'w') as f: # for frame_id, frame_data in results_dict.items(): # if data_type == 'kitti': # frame_id -= 1 # for tlwh, track_id in frame_data: # if track_id < 0: # continue # x1, y1, w, h = tlwh # x2, y2 = x1 + w, y1 + h # line = save_format.format(frame=frame_id, id=track_id, x1=x1, y1=y1, x2=x2, y2=y2, w=w, h=h, score=1.0) # f.write(line) # logger.info('Save results to {}'.format(filename)) def read_results(filename, data_type: str, is_gt=False, is_ignore=False): if data_type in ('mot', 'lab'): read_fun = read_mot_results else: raise ValueError('Unknown data type: {}'.format(data_type)) return read_fun(filename, is_gt, is_ignore) """ labels={'ped', ... % 1 'person_on_vhcl', ... % 2 'car', ... % 3 'bicycle', ... % 4 'mbike', ... % 5 'non_mot_vhcl', ... % 6 'static_person', ... % 7 'distractor', ... % 8 'occluder', ... % 9 'occluder_on_grnd', ... %10 'occluder_full', ... % 11 'reflection', ... % 12 'crowd' ... % 13 }; """ def read_mot_results(filename, is_gt, is_ignore): valid_labels = {1} ignore_labels = {2, 7, 8, 12} results_dict = dict() if os.path.isfile(filename): with open(filename, 'r') as f: for line in f.readlines(): linelist = line.split(',') if len(linelist) < 7: continue fid = int(linelist[0]) if fid < 1: continue results_dict.setdefault(fid, list()) if is_gt: if 'MOT16-' in filename or 'MOT17-' in filename: label = int(float(linelist[7])) mark = int(float(linelist[6])) if mark == 0 or label not in valid_labels: continue score = 1 elif is_ignore: if 'MOT16-' in filename or 'MOT17-' in filename: label = int(float(linelist[7])) vis_ratio = float(linelist[8]) if label not in ignore_labels and vis_ratio >= 0: continue else: continue score = 1 else: score = float(linelist[6]) tlwh = tuple(map(float, linelist[2:6])) target_id = int(linelist[1]) results_dict[fid].append((tlwh, target_id, score)) return results_dict def unzip_objs(objs): if len(objs) > 0: tlwhs, ids, scores = zip(*objs) else: tlwhs, ids, scores = [], [], [] tlwhs = np.asarray(tlwhs, dtype=float).reshape(-1, 4) return tlwhs, ids, scores ================================================ FILE: asone/trackers/strong_sort/tracker/utils/json_logger.py ================================================ """ References: https://medium.com/analytics-vidhya/creating-a-custom-logging-mechanism-for-real-time-object-detection-using-tdd-4ca2cfcd0a2f """ import json from os import makedirs from os.path import exists, join from datetime import datetime class JsonMeta(object): HOURS = 3 MINUTES = 59 SECONDS = 59 PATH_TO_SAVE = 'LOGS' DEFAULT_FILE_NAME = 'remaining' class BaseJsonLogger(object): """ This is the base class that returns __dict__ of its own it also returns the dicts of objects in the attributes that are list instances """ def dic(self): # returns dicts of objects out = {} for k, v in self.__dict__.items(): if hasattr(v, 'dic'): out[k] = v.dic() elif isinstance(v, list): out[k] = self.list(v) else: out[k] = v return out @staticmethod def list(values): # applies the dic method on items in the list return [v.dic() if hasattr(v, 'dic') else v for v in values] class Label(BaseJsonLogger): """ For each bounding box there are various categories with confidences. Label class keeps track of that information. """ def __init__(self, category: str, confidence: float): self.category = category self.confidence = confidence class Bbox(BaseJsonLogger): """ This module stores the information for each frame and use them in JsonParser Attributes: labels (list): List of label module. top (int): left (int): width (int): height (int): Args: bbox_id (float): top (int): left (int): width (int): height (int): References: Check Label module for better understanding. """ def __init__(self, bbox_id, top, left, width, height): self.labels = [] self.bbox_id = bbox_id self.top = top self.left = left self.width = width self.height = height def add_label(self, category, confidence): # adds category and confidence only if top_k is not exceeded. self.labels.append(Label(category, confidence)) def labels_full(self, value): return len(self.labels) == value class Frame(BaseJsonLogger): """ This module stores the information for each frame and use them in JsonParser Attributes: timestamp (float): The elapsed time of captured frame frame_id (int): The frame number of the captured video bboxes (list of Bbox objects): Stores the list of bbox objects. References: Check Bbox class for better information Args: timestamp (float): frame_id (int): """ def __init__(self, frame_id: int, timestamp: float = None): self.frame_id = frame_id self.timestamp = timestamp self.bboxes = [] def add_bbox(self, bbox_id: int, top: int, left: int, width: int, height: int): bboxes_ids = [bbox.bbox_id for bbox in self.bboxes] if bbox_id not in bboxes_ids: self.bboxes.append(Bbox(bbox_id, top, left, width, height)) else: raise ValueError("Frame with id: {} already has a Bbox with id: {}".format(self.frame_id, bbox_id)) def add_label_to_bbox(self, bbox_id: int, category: str, confidence: float): bboxes = {bbox.id: bbox for bbox in self.bboxes} if bbox_id in bboxes.keys(): res = bboxes.get(bbox_id) res.add_label(category, confidence) else: raise ValueError('the bbox with id: {} does not exists!'.format(bbox_id)) class BboxToJsonLogger(BaseJsonLogger): """ ُ This module is designed to automate the task of logging jsons. An example json is used to show the contents of json file shortly Example: { "video_details": { "frame_width": 1920, "frame_height": 1080, "frame_rate": 20, "video_name": "/home/gpu/codes/MSD/pedestrian_2/project/public/camera1.avi" }, "frames": [ { "frame_id": 329, "timestamp": 3365.1254 "bboxes": [ { "labels": [ { "category": "pedestrian", "confidence": 0.9 } ], "bbox_id": 0, "top": 1257, "left": 138, "width": 68, "height": 109 } ] }], Attributes: frames (dict): It's a dictionary that maps each frame_id to json attributes. video_details (dict): information about video file. top_k_labels (int): shows the allowed number of labels start_time (datetime object): we use it to automate the json output by time. Args: top_k_labels (int): shows the allowed number of labels """ def __init__(self, top_k_labels: int = 1): self.frames = {} self.video_details = self.video_details = dict(frame_width=None, frame_height=None, frame_rate=None, video_name=None) self.top_k_labels = top_k_labels self.start_time = datetime.now() def set_top_k(self, value): self.top_k_labels = value def frame_exists(self, frame_id: int) -> bool: """ Args: frame_id (int): Returns: bool: true if frame_id is recognized """ return frame_id in self.frames.keys() def add_frame(self, frame_id: int, timestamp: float = None) -> None: """ Args: frame_id (int): timestamp (float): opencv captured frame time property Raises: ValueError: if frame_id would not exist in class frames attribute Returns: None """ if not self.frame_exists(frame_id): self.frames[frame_id] = Frame(frame_id, timestamp) else: raise ValueError("Frame id: {} already exists".format(frame_id)) def bbox_exists(self, frame_id: int, bbox_id: int) -> bool: """ Args: frame_id: bbox_id: Returns: bool: if bbox exists in frame bboxes list """ bboxes = [] if self.frame_exists(frame_id=frame_id): bboxes = [bbox.bbox_id for bbox in self.frames[frame_id].bboxes] return bbox_id in bboxes def find_bbox(self, frame_id: int, bbox_id: int): """ Args: frame_id: bbox_id: Returns: bbox_id (int): Raises: ValueError: if bbox_id does not exist in the bbox list of specific frame. """ if not self.bbox_exists(frame_id, bbox_id): raise ValueError("frame with id: {} does not contain bbox with id: {}".format(frame_id, bbox_id)) bboxes = {bbox.bbox_id: bbox for bbox in self.frames[frame_id].bboxes} return bboxes.get(bbox_id) def add_bbox_to_frame(self, frame_id: int, bbox_id: int, top: int, left: int, width: int, height: int) -> None: """ Args: frame_id (int): bbox_id (int): top (int): left (int): width (int): height (int): Returns: None Raises: ValueError: if bbox_id already exist in frame information with frame_id ValueError: if frame_id does not exist in frames attribute """ if self.frame_exists(frame_id): frame = self.frames[frame_id] if not self.bbox_exists(frame_id, bbox_id): frame.add_bbox(bbox_id, top, left, width, height) else: raise ValueError( "frame with frame_id: {} already contains the bbox with id: {} ".format(frame_id, bbox_id)) else: raise ValueError("frame with frame_id: {} does not exist".format(frame_id)) def add_label_to_bbox(self, frame_id: int, bbox_id: int, category: str, confidence: float): """ Args: frame_id: bbox_id: category: confidence: the confidence value returned from yolo detection Returns: None Raises: ValueError: if labels quota (top_k_labels) exceeds. """ bbox = self.find_bbox(frame_id, bbox_id) if not bbox.labels_full(self.top_k_labels): bbox.add_label(category, confidence) else: raise ValueError("labels in frame_id: {}, bbox_id: {} is fulled".format(frame_id, bbox_id)) def add_video_details(self, frame_width: int = None, frame_height: int = None, frame_rate: int = None, video_name: str = None): self.video_details['frame_width'] = frame_width self.video_details['frame_height'] = frame_height self.video_details['frame_rate'] = frame_rate self.video_details['video_name'] = video_name def output(self): output = {'video_details': self.video_details} result = list(self.frames.values()) output['frames'] = [item.dic() for item in result] return output def json_output(self, output_name): """ Args: output_name: Returns: None Notes: It creates the json output with `output_name` name. """ if not output_name.endswith('.json'): output_name += '.json' with open(output_name, 'w') as file: json.dump(self.output(), file) file.close() def set_start(self): self.start_time = datetime.now() def schedule_output_by_time(self, output_dir=JsonMeta.PATH_TO_SAVE, hours: int = 0, minutes: int = 0, seconds: int = 60) -> None: """ Notes: Creates folder and then periodically stores the jsons on that address. Args: output_dir (str): the directory where output files will be stored hours (int): minutes (int): seconds (int): Returns: None """ end = datetime.now() interval = 0 interval += abs(min([hours, JsonMeta.HOURS]) * 3600) interval += abs(min([minutes, JsonMeta.MINUTES]) * 60) interval += abs(min([seconds, JsonMeta.SECONDS])) diff = (end - self.start_time).seconds if diff > interval: output_name = self.start_time.strftime('%Y-%m-%d %H-%M-%S') + '.json' if not exists(output_dir): makedirs(output_dir) output = join(output_dir, output_name) self.json_output(output_name=output) self.frames = {} self.start_time = datetime.now() def schedule_output_by_frames(self, frames_quota, frame_counter, output_dir=JsonMeta.PATH_TO_SAVE): """ saves as the number of frames quota increases higher. :param frames_quota: :param frame_counter: :param output_dir: :return: """ pass def flush(self, output_dir): """ Notes: We use this function to output jsons whenever possible. like the time that we exit the while loop of opencv. Args: output_dir: Returns: None """ filename = self.start_time.strftime('%Y-%m-%d %H-%M-%S') + '-remaining.json' output = join(output_dir, filename) self.json_output(output_name=output) ================================================ FILE: asone/trackers/strong_sort/tracker/utils/log.py ================================================ import logging def get_logger(name='root'): formatter = logging.Formatter( # fmt='%(asctime)s [%(levelname)s]: %(filename)s(%(funcName)s:%(lineno)s) >> %(message)s') fmt='%(asctime)s [%(levelname)s]: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') handler = logging.StreamHandler() handler.setFormatter(formatter) logger = logging.getLogger(name) logger.setLevel(logging.INFO) logger.addHandler(handler) return logger ================================================ FILE: asone/trackers/strong_sort/tracker/utils/parser.py ================================================ import os import yaml from easydict import EasyDict as edict class YamlParser(edict): """ This is yaml parser based on EasyDict. """ def __init__(self, cfg_dict=None, config_file=None): if cfg_dict is None: cfg_dict = {} if config_file is not None: assert(os.path.isfile(config_file)) with open(config_file, 'r') as fo: yaml_ = yaml.load(fo.read(), Loader=yaml.FullLoader) cfg_dict.update(yaml_) super(YamlParser, self).__init__(cfg_dict) def merge_from_file(self, config_file): with open(config_file, 'r') as fo: yaml_ = yaml.load(fo.read(), Loader=yaml.FullLoader) self.update(yaml_) def merge_from_dict(self, config_dict): self.update(config_dict) def get_config(config_file=None): return YamlParser(config_file=config_file) if __name__ == "__main__": cfg = YamlParser(config_file="../configs/yolov3.yaml") cfg.merge_from_file("../configs/strong_sort.yaml") import ipdb ipdb.set_trace() ================================================ FILE: asone/trackers/strong_sort/tracker/utils/tools.py ================================================ from functools import wraps from time import time def is_video(ext: str): """ Returns true if ext exists in allowed_exts for video files. Args: ext: Returns: """ allowed_exts = ('.mp4', '.webm', '.ogg', '.avi', '.wmv', '.mkv', '.3gp') return any((ext.endswith(x) for x in allowed_exts)) def tik_tok(func): """ keep track of time for each process. Args: func: Returns: """ @wraps(func) def _time_it(*args, **kwargs): start = time() try: return func(*args, **kwargs) finally: end_ = time() print("time: {:.03f}s, fps: {:.03f}".format(end_ - start, 1 / (end_ - start))) return _time_it ================================================ FILE: asone/trackers/tracker.py ================================================ from asone.trackers import ByteTrack from asone.trackers import NorFair from asone.trackers import DeepSort from asone.trackers import Motpy from asone.trackers import OcSort from asone.trackers import StrongSort class Tracker: def __init__(self, tracker: int, detector: object, use_cuda=True) -> None: self.trackers = { '0': ByteTrack, '1': DeepSort, '2': NorFair, '3': Motpy, '4': OcSort, '5': StrongSort } self.tracker = self._select_tracker(tracker, detector, use_cuda=use_cuda) def _select_tracker(self, tracker, detector, use_cuda): _tracker = self.trackers.get(str(tracker), None) if _tracker is not None: if _tracker is DeepSort: return _tracker(detector, use_cuda=use_cuda) else: return _tracker(detector) else: raise ValueError(f'Invalid tracker: {tracker}') def detect_and_track(self, image, config: dict): return self.tracker.detect_and_track(image, config) def get_tracker(self): return self.tracker ================================================ FILE: asone/utils/__init__.py ================================================ from asone.utils.classes import get_names from asone.utils.download import download_weights from asone.utils.colors import compute_color_for_labels from asone.utils.counting import estimateSpeed, intersect from asone.utils.ponits_conversion import xyxy_to_tlwh, xyxy_to_xywh, tlwh_to_xyxy from asone.utils.temp_loader import get_detector, get_tracker from asone.utils.draw import draw_boxes from asone.utils.draw import draw_text from asone.utils.draw import draw_kpts from asone.utils.draw import plot_skeleton_kpts from asone.utils.pose_estimators_weights import get_weight_path ================================================ FILE: asone/utils/classes.py ================================================ # names = [] # with open('classes.txt') as f: # names.append(f.read()) global names names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush', "text"] def get_names(): return names ================================================ FILE: asone/utils/colors.py ================================================ from numpy import random from asone.utils import get_names names = get_names() colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1) def compute_color_for_labels(label): """ Simple function that adds fixed color depending on the class """ if label == 0: # person #BGR color = (85, 45, 255) elif label == 2: # Car color = (222, 82, 175) elif label == 3: # Motobike color = (0, 204, 255) elif label == 5: # Bus color = (0, 149, 255) else: color = [int((p * (label ** 2 - label + 1)) % 255) for p in palette] return tuple(color) ================================================ FILE: asone/utils/counting.py ================================================ import math def estimateSpeed(location1, location2): d_pixels = math.sqrt(math.pow( location2[0] - location1[0], 2) + math.pow(location2[1] - location1[1], 2)) ppm = 8 # Pixels per Meter d_meters = d_pixels / ppm time_constant = 15 * 3.6 speed = d_meters * time_constant return speed # Return true if line segments AB and CD intersect def intersect(A, B, C, D): return ccw(A, C, D) != ccw(B, C, D) and ccw(A, B, C) != ccw(A, B, D) def ccw(A, B, C): return (C[1]-A[1]) * (B[0]-A[0]) > (B[1]-A[1]) * (C[0]-A[0]) ================================================ FILE: asone/utils/default_cfg.py ================================================ config = { "output_dir": "results", "filename": None, "fps": None, "save_result": True, "display": False, "draw_trails": False, "filter_classes": None, "class_names": None, "input_shape" : (640, 640), "conf_thres": 0.25, "iou_thres" : 0.45, "max_det" : 1000, "agnostic_nms" : False, "with_p6" : False } ================================================ FILE: asone/utils/download.py ================================================ import gdown import os import zipfile def exractfile(file, dest): with zipfile.ZipFile(file, 'r') as zip_ref: zip_ref.extractall(dest) def download_weights(weights): outputpath = os.path.dirname(weights) model = os.path.splitext(os.path.basename(weights))[0] filename = f'{model}.zip' if model == 'yolov5s': model_key = '1W5ypZmrYE4_Aqu3Jqsl-IDvLK6SOtJCK' elif model == 'yolov5x6': model_key = '1CTYtGC8VFZD0uJbU4fcSPSjcYb8Be0jU' elif model == 'yolov5n': model_key = '1q9_e76T_b353QmG5xGi3zkselGQrxuBk' elif model == 'yolov5m': model_key = '1Vv3VEkgYd7WB-3e2MPo0QUKq_-F7biBP' elif model == 'yolov5l': model_key = '1Wr4S7BTqqCOCP14T_aDgVxGV5h9pMM-n' elif model == 'yolov5x': model_key = '18g_pjpwsnOlBKbhApuaTsbQUIDO75BSt' elif model == 'yolov5n6': model_key = '1gOAZ90nKcvo7bhNZCZM-JRuY8FBeEkhl' elif model == 'yolov5s6': model_key = '12W1Z0esjFc9UhiWWxCESjhm5KA3nzSwt' elif model == 'yolov5m6': model_key = '1O-bbJ8WcqLig40IUyJ8ulKwj-J_KXvHr' elif model == 'yolov5l6': model_key = '1sPZ-YpenYojZSIB5G1SMv2hPWX5oRwlb' elif model == 'yolov6n': model_key = '1NA_u4BkPE_N8HcPmZrd7HyLmvFHOk8qd' elif model == 'yolov6t': model_key = '16OWncBp-vh-sLDMOR58th3WOGv4envQ1' elif model == 'yolov6s': model_key = '14BE0j654ClLxMq2ySWZNhTCmf48mLyXi' elif model == 'yolov6l_relu': model_key = '14UfY057QUQoAj6q39PX_qE7U1bBIrIGi' elif model == 'yolov6l': model_key = '1HdRIs0uMPbqs5E2aEX8O3d3dJTh-KBTf' elif model == 'yolov6m': model_key = '1t_w9SCwbZAW7icwX_z97-SQz-plXzBgM' elif model == 'yolov6s_repopt': model_key = '1L_1Crxx-4059xDDUZEf_asWRBVd3PF05' elif model == 'yolov7-e6e': model_key = '1W9vEbEv8expm0Yz9R9VIn8ToqDM9TgIt' elif model == 'yolov7-d6': model_key = '1_Ybtx7EAXnBwIZ59Vgo0FkXsCdjRuq2s' elif model == 'yolov7': model_key = '17iFeNfq5hKZVpQLQEgZzxF9Da5o5llLG' elif model == 'yolov7-tiny': model_key = '18Fels44wVJ1vG7yDuDPuWwuiAqeFxKI7' elif model == 'yolov7-e6': model_key = '1g_2nYpeJ28cLYcOAUeztHUA5R-stk3Cm' elif model == 'yolov7-w6': model_key = '1wv3M23RFo0MhaujegBPY6gZ30IA894CO' elif model == 'yolov7x': model_key = '1zZskyvdgU45Ke8TtCA6csdCzqLrhmFYx' elif model == 'yolor_csp': model_key = '1G3FBZKrznW_64mGfs6b3nAJiJv6GmmV0' elif model == 'yolor_csp_star': model_key = '15WDl46ZthFGZfpOyI3qXx6gC9FQLH_wH' elif model == 'yolor_csp_x': model_key = '1LU2ckh7eSpVD0nyPSdq1n34lKmNAX39T' elif model == 'yolor_csp_x_star': model_key = '1jheqFDm7BpHQpR60wuWSBpbuyK5SoKdV' elif model == 'yolor_p6': model_key = '1XKREKdxQCO8OXiW2IWGFhczolIaIr9sm' elif model == 'yolox_l': model_key = '1jX1KHerOdZ5-dmXh6cWcRAn80aKD-7sP' elif model == 'yolox_nano': model_key = '1783Os6uTpYjunL-MfK0WE1Wcwk58fIUi' elif model == 'yolox_tiny': model_key = '1Lcv1ITvfPdWsu6Kb8Hq6cOSfJE7lbbf2' elif model == 'yolox_darknet': model_key = '17f4UI06TWJ25Oqo2OoQGu8AoGVX1lPta' elif model == 'yolox_s': model_key = '1IUAOv62XuwkwwTCVD2y3xJg7KUA3M0-M' elif model == 'yolox_m': model_key = '1ktHj8UEwl0V8Qz9G74E-yj-o13FLeD0-' elif model == 'yolox_x': model_key = '13HNnlILCx_XamNJWwJ1MG5x0XfP6HL1U' elif model == 'ckpt': model_key = '1VZ05gzg249Q1m8BJVQxl3iHoNIbjzJf8' elif model == 'yolov8s': model_key = '1hUhjQWw1cJL7TtBG0zD3MO52iCYh0DEn' elif model == 'yolov8n': model_key = '1x6zHzsEcyhuyWy2xY3swAQ4vIQvBYrsr' elif model == 'yolov8l': model_key = '1xQxHTEIpoiP4d73F6dtedU7hIZpFTqY2' elif model == 'yolov8m': model_key = '1_FoKnqkaoWchVy4B24Hn2PanEKfh-eSp' elif model == 'yolov8x': model_key = '1s60fsjiyDlPQ1L5H_GAoWahHONLbvz7T' elif model == 'yolov8s-pose': model_key = '1Gfehs7CzQJgBOceLK6WrWpHXilUlgRyc' elif model == 'yolov8n-pose': model_key = '1NE0BR9OuwWKwadrBQN904R7NBXMpdyqy' elif model == 'yolov8l-pose': model_key = '1nzArC823KleZk0_DCA-D6Jm6kHLrPQw_' elif model == 'yolov8m-pose': model_key = '1p8W_FNUsY9_fIVsDAZjbLnqA2ViF0j8P' elif model == 'yolov8x-pose': model_key = '1Z0tZg1OYStkbzFNa5DJS_5qegNM_YtWH' elif model == 'yolov7-w6-pose': model_key = '1R_7QH-Y9TJ0jnDn3STfZvZzbx1fqs1mZ' elif model == 'yolo_nas_l': model_key = '1FYDcp-LNxlVfxvKU2kG8FmkqZNIOSAnN' elif model == 'yolo_nas_m': model_key = '1wHf3q5Jl5Uk1iGgRjm6TYChInAoYGNhf' elif model == 'yolo_nas_s': model_key = '1jtI-L9J8G7sRBa-sziqryMp11vlFKWyh' elif model == 'yolov9-c-converted': model_key = '1PCT4dCKosBB0B26sLdQ5xdjQ2vTPjWaH' elif model == 'yolov9-e-converted': model_key = '16rNqbrlmN3YgsaxOsYLad1IRK2eo_3AL' elif model == 'yolov9-c': model_key = '1FRnyftVwTYyH2Or8BkMWE7HB_08dsNkA' elif model == 'yolov9-e': model_key = '1ayY7dJH6r9hzl1MSOdVFx0UqmWixhEJu' elif model == 'gelan-c': model_key = '1AhGd8Ex0Kr1iXiSytu-OBpyvLykL9tMF' elif model == 'gelan-e': model_key = '1vc7nBJTMm3tt_gLD-zJSbmFAaWoP8ZbS' elif model == "sam_vit_h_4b8939": model_key = '1-QSw_IqF4WczsC3pdbiIpmjTEaxPc5nO' else: raise ValueError(f'No model named {model} found.') url = f'https://drive.google.com/uc?id={model_key}&confirm=t' gdown.download(url, output=filename, quiet=False) if not os.path.exists(outputpath): os.makedirs(outputpath) exractfile(filename, outputpath) os.remove(filename) # def download_weights(weights): # f = open('asone/utils/weights.json') # data = json.load(f) # outputpath = os.path.dirname(weights) # model = os.path.splitext(os.path.basename(weights))[0] # filename = f'{model}.zip' # if model in data: # model_key = data[model] # else: # raise ValueError(f'No model named {model} found.') # url = f'https://drive.google.com/uc?id={model_key}&confirm=t' # gdown.download(url, output=filename, quiet=False) # if not os.path.exists(outputpath): # os.makedirs(outputpath) # exractfile(filename, outputpath) # os.remove(filename) ================================================ FILE: asone/utils/draw.py ================================================ import cv2 from numpy import random import numpy as np from asone.utils import compute_color_for_labels from asone.utils import get_names from collections import deque from asone.schemas.output_schemas import ModelOutput names = get_names() data_deque = {} def draw_ui_box(x, img, label=None, color=None, line_thickness=None): # Plots one bounding box on image img tl = line_thickness or round( 0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness color = color or [random.randint(0, 255) for _ in range(3)] c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) if label: tf = max(tl - 1, 1) # font thickness t_size = cv2.getTextSize(str(label), 0, fontScale=tl / 3, thickness=tf)[0] # c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 img = draw_border(img, (c1[0], c1[1] - t_size[1] - 3), (c1[0] + t_size[0], c1[1]+3), color, 1, 8, 2) # cv2.line(img, c1, c2, color, 30) # cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled cv2.putText(img, str(label), (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) return img def draw_border(img, pt1, pt2, color, thickness, r, d): x1, y1 = pt1 x2, y2 = pt2 # Top leftfrom collections import deque (x1, y1 + r + d), color, thickness) cv2.ellipse(img, (x1 + r, y1 + r), (r, r), 180, 0, 90, color, thickness) # Top right cv2.line(img, (x2 - r, y1), (x2 - r - d, y1), color, thickness) cv2.line(img, (x2, y1 + r), (x2, y1 + r + d), color, thickness) cv2.ellipse(img, (x2 - r, y1 + r), (r, r), 270, 0, 90, color, thickness) # Bottom left cv2.line(img, (x1 + r, y2), (x1 + r + d, y2), color, thickness) cv2.line(img, (x1, y2 - r), (x1, y2 - r - d), color, thickness) cv2.ellipse(img, (x1 + r, y2 - r), (r, r), 90, 0, 90, color, thickness) # Bottom right cv2.line(img, (x2 - r, y2), (x2 - r - d, y2), color, thickness) cv2.line(img, (x2, y2 - r), (x2, y2 - r - d), color, thickness) cv2.ellipse(img, (x2 - r, y2 - r), (r, r), 0, 0, 90, color, thickness) cv2.rectangle(img, (x1 + r, y1), (x2 - r, y2), color, -1, cv2.LINE_AA) cv2.rectangle(img, (x1, y1 + r), (x2, y2 - r - d), color, -1, cv2.LINE_AA) cv2.circle(img, (x1 + r, y1+r), 2, color, 12) cv2.circle(img, (x2 - r, y1+r), 2, color, 12) cv2.circle(img, (x1 + r, y2-r), 2, color, 12) cv2.circle(img, (x2 - r, y2-r), 2, color, 12) return img def draw_boxes(img, bbox_xyxy, class_ids, identities=None, draw_trails=False, offset=(0, 0), class_names=None): # cv2.line(img, line2[0], line2[1], (0,200,0), 3) height, width, _ = img.shape # remove tracked point from buffer if object is lost if draw_trails: for key in list(data_deque): if key not in identities: data_deque.pop(key) for i, box in enumerate(bbox_xyxy): x1, y1, x2, y2 = [int(i) for i in box] x1 += offset[0] x2 += offset[0] y1 += offset[1] y2 += offset[1] # get ID of object id = int(identities[i]) if identities is not None else None # if class_ids is not None: color = compute_color_for_labels(int(class_ids[i])) if class_names: obj_name = class_names[int(class_ids[i])] else: obj_name = names[int(class_ids[i])] label = f'{obj_name}' if id is None else f'{id}' draw_ui_box(box, img, label=label, color=color, line_thickness=2) # Draw trails # code to find center of bottom edge center = (int((x2+x1) / 2), int((y2+y2)/2)) if draw_trails: # create new buffer for new object if id not in data_deque: data_deque[id] = deque(maxlen= 64) data_deque[id].appendleft(center) drawtrails(data_deque, id, color, img) return img def drawtrails(data_deque, id, color, img): # draw trail for i in range(1, len(data_deque[id])): # check if on buffer value is none if data_deque[id][i - 1] is None or data_deque[id][i] is None: continue # generate dynamic thickness of trails thickness = int(np.sqrt(64 / float(i + i)) * 1.5) # draw trails cv2.line(img, data_deque[id][i - 1], data_deque[id][i], color, thickness) def draw_text(img, results, offset=(0, 0), display: bool = False): color = compute_color_for_labels(int(0)) for res in results: box = res[:4] text = res[4] x1, y1, x2, y2 = box x1 += offset[0] x2 += offset[0] y1 += offset[1] y2 += offset[1] label = text img = draw_ui_box(box, img, label=label, color=color, line_thickness=2) center = (int((x2+x1) / 2), int((y2+y2)/2)) if display: cv2.imshow(' Sample', img) return img # Utils for code estimation class Colors: # Ultralytics color palette https://ultralytics.com/ def __init__(self): """Initialize colors as hex = matplotlib.colors.TABLEAU_COLORS.values().""" hexs = ('FF3838', 'FF9D97', 'FF701F', 'FFB21D', 'CFD231', '48F90A', '92CC17', '3DDB86', '1A9334', '00D4BB', '2C99A8', '00C2FF', '344593', '6473FF', '0018EC', '8438FF', '520085', 'CB38FF', 'FF95C8', 'FF37C7') self.palette = [self.hex2rgb(f'#{c}') for c in hexs] self.n = len(self.palette) self.pose_palette = np.array([[255, 128, 0], [255, 153, 51], [255, 178, 102], [230, 230, 0], [255, 153, 255], [153, 204, 255], [255, 102, 255], [255, 51, 255], [102, 178, 255], [51, 153, 255], [255, 153, 153], [255, 102, 102], [255, 51, 51], [153, 255, 153], [102, 255, 102], [51, 255, 51], [0, 255, 0], [0, 0, 255], [255, 0, 0], [255, 255, 255]], dtype=np.uint8) def __call__(self, i, bgr=False): """Converts hex color codes to rgb values.""" c = self.palette[int(i) % self.n] return (c[2], c[1], c[0]) if bgr else c @staticmethod def hex2rgb(h): # rgb order (PIL) return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4)) colors = Colors() # create instance for 'from utils.plots import colors' def draw_kpts(keypoints, image=None, shape=(640, 640), radius=5, kpt_line=True, display: bool=True): """Plot keypoints on the image. Args: kpts (tensor): Predicted keypoints with shape [17, 3]. Each keypoint has (x, y, confidence). shape (tuple): Image shape as a tuple (h, w), where h is the height and w is the width. radius (int, optional): Radius of the drawn keypoints. Default is 5. kpt_line (bool, optional): If True, the function will draw lines connecting keypoints for human pose. Default is True. Note: `kpt_line=True` currently only supports human pose plotting. """ # if self.pil: # # Convert to numpy first # self.im = np.asarray(self.im).copy() if isinstance(keypoints, ModelOutput): image = keypoints.info.image keypoints = keypoints.dets.bbox if keypoints is not None: for kpts in reversed(keypoints): limb_color = colors.pose_palette[[9, 9, 9, 9, 7, 7, 7, 0, 0, 0, 0, 0, 16, 16, 16, 16, 16, 16, 16]] kpt_color = colors.pose_palette[[16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9]] skeleton = [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]] nkpt, ndim = kpts.shape is_pose = nkpt == 17 and ndim in [2, 3] kpt_line &= is_pose # `kpt_line=True` for now only supports human pose plotting for i, k in enumerate(kpts): color_k = [int(x) for x in kpt_color[i]] if is_pose else colors(i) x_coord, y_coord = k[0], k[1] if x_coord % shape[1] != 0 and y_coord % shape[0] != 0: if len(k) == 3: conf = k[2] if conf < 0.5: continue cv2.circle(image, (int(x_coord), int(y_coord)), radius, color_k, -1, lineType=cv2.LINE_AA) if kpt_line: ndim = kpts.shape[-1] for i, sk in enumerate(skeleton): pos1 = (int(kpts[(sk[0] - 1), 0]), int(kpts[(sk[0] - 1), 1])) pos2 = (int(kpts[(sk[1] - 1), 0]), int(kpts[(sk[1] - 1), 1])) if ndim == 3: conf1 = kpts[(sk[0] - 1), 2] conf2 = kpts[(sk[1] - 1), 2] if conf1 < 0.5 or conf2 < 0.5: continue if pos1[0] % shape[1] == 0 or pos1[1] % shape[0] == 0 or pos1[0] < 0 or pos1[1] < 0: continue if pos2[0] % shape[1] == 0 or pos2[1] % shape[0] == 0 or pos2[0] < 0 or pos2[1] < 0: continue cv2.line(image, pos1, pos2, [int(x) for x in limb_color[i]], thickness=2, lineType=cv2.LINE_AA) if display: cv2.imshow(' Sample', image) return image def plot_skeleton_kpts(im, kpts, steps, orig_shape=None): #Plot the skeleton and keypointsfor coco datatset palette = np.array([[255, 128, 0], [255, 153, 51], [255, 178, 102], [230, 230, 0], [255, 153, 255], [153, 204, 255], [255, 102, 255], [255, 51, 255], [102, 178, 255], [51, 153, 255], [255, 153, 153], [255, 102, 102], [255, 51, 51], [153, 255, 153], [102, 255, 102], [51, 255, 51], [0, 255, 0], [0, 0, 255], [255, 0, 0], [255, 255, 255]]) skeleton = [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]] pose_limb_color = palette[[9, 9, 9, 9, 7, 7, 7, 0, 0, 0, 0, 0, 16, 16, 16, 16, 16, 16, 16]] pose_kpt_color = palette[[16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9]] radius = 5 num_kpts = len(kpts) // steps for kid in range(num_kpts): r, g, b = pose_kpt_color[kid] x_coord, y_coord = kpts[steps * kid], kpts[steps * kid + 1] if not (x_coord % 640 == 0 or y_coord % 640 == 0): if steps == 3: conf = kpts[steps * kid + 2] if conf < 0.5: continue overlay = im.copy() alpha = 0.4 cv2.circle(overlay, (int(x_coord), int(y_coord)), 8, (int(220), int(237), int(245)), 8) cv2.circle(im, (int(x_coord), int(y_coord)), 5, (int(255), int(255), int(255)), -1) # im = output cv2.addWeighted(overlay, alpha, im, 1 - alpha, 0, im) for sk_id, sk in enumerate(skeleton): r, g, b = pose_limb_color[sk_id] pos1 = (int(kpts[(sk[0]-1)*steps]), int(kpts[(sk[0]-1)*steps+1])) pos2 = (int(kpts[(sk[1]-1)*steps]), int(kpts[(sk[1]-1)*steps+1])) if steps == 3: conf1 = kpts[(sk[0]-1)*steps+2] conf2 = kpts[(sk[1]-1)*steps+2] if conf1<0.5 or conf2<0.5: continue if pos1[0]%640 == 0 or pos1[1]%640==0 or pos1[0]<0 or pos1[1]<0: continue if pos2[0] % 640 == 0 or pos2[1] % 640 == 0 or pos2[0]<0 or pos2[1]<0: continue cv2.line(im, pos1, pos2, (int(255), int(255), int(255)), thickness=2) ================================================ FILE: asone/utils/ponits_conversion.py ================================================ def xyxy_to_xywh(xyxy): """" Calculates the relative bounding box from absolute pixel values. """ bbox_left = min([xyxy[0], xyxy[2]]) bbox_top = min([xyxy[1], xyxy[3]]) bbox_w = abs(xyxy[0] - xyxy[2]) bbox_h = abs(xyxy[1] - xyxy[3]) x_c = (bbox_left + bbox_w / 2) y_c = (bbox_top + bbox_h / 2) w = bbox_w h = bbox_h return [x_c, y_c, w, h] def tlwh_to_xyxy(tlwh): """" Convert tlwh to xyxy """ x1 = tlwh[0] y1 = tlwh[1] x2 = tlwh[2] + x1 y2 = tlwh[3] + y1 return [x1, y1, x2, y2] def xyxy_to_tlwh(bbox_xyxy): tlwh_bboxs = [] for i, box in enumerate(bbox_xyxy): x1, y1, x2, y2 = [int(i) for i in box] top = x1 left = y1 w = int(x2 - x1) h = int(y2 - y1) tlwh_obj = [top, left, w, h] tlwh_bboxs.append(tlwh_obj) return tlwh_bboxs ================================================ FILE: asone/utils/pose_estimators_weights.py ================================================ import os weights = { # '130': os.path.join('yolov7','weights','yolov7-tiny.mlmodel'), # '131': os.path.join('yolov7','weights','yolov7.mlmodel'), # '132': os.path.join('yolov7','weights','yolov7x.mlmodel'), '149': os.path.join('yolov7','weights','yolov7-w6-pose.pt'), # '134': os.path.join('yolov7','weights','yolov7-e6.mlmodel'), # '135': os.path.join('yolov7','weights','yolov7-d6.mlmodel'), # '136': os.path.join('yolov7','weights','yolov7-e6e.mlmodel'), '144': os.path.join('yolov8','weights','yolov8n-pose.pt'), '145': os.path.join('yolov8','weights','yolov8s-pose.pt'), '146': os.path.join('yolov8','weights','yolov8m-pose.pt'), '147': os.path.join('yolov8','weights','yolov8l-pose.pt'), '148': os.path.join('yolov8','weights','yolov8x-pose.pt') } def get_weight_path(model_flag): weight = weights[str(model_flag)] return weight ================================================ FILE: asone/utils/temp_loader.py ================================================ from asone.detectors import YOLOv5Detector from asone.detectors import YOLOv7Detector from asone.trackers import ByteTrack from asone.trackers import NorFair from asone.trackers import DeepSort detectors = { 'yolov5s': YOLOv5Detector, 'yolov7': YOLOv7Detector } trackers = { 'byte_track': ByteTrack, 'norfair': NorFair, 'deepsort': DeepSort } def get_detector(detector, use_cuda=True, use_onnx=False): detector = detectors.get(detector, None) if detector is not None: return detector(use_cuda=use_cuda, use_onnx=use_onnx) else: return None def get_tracker(tracker, detector, use_cuda=True, use_onnx=False): tracker = trackers.get(tracker, None) if tracker is not None: return tracker(detector) else: return None ================================================ FILE: asone/utils/utils.py ================================================ import os import sys import traceback class PathResolver: def __init__(self, path: str = None) -> None: if path is None: stack = traceback.extract_stack() file_path = stack[-2].filename dir_path = os.path.dirname(file_path) path = os.path.join(dir_path, os.path.basename(dir_path)) self.path = path def __enter__(self): sys.path.insert(0, self.path) def __exit__(self, exc_type, exc_value, traceback): sys.path.pop(0) ================================================ FILE: asone/utils/video_reader.py ================================================ import cv2 from tqdm import tqdm class VideoReader: def __init__( self, video_path: str): """ A simple wrapper class over OpenCVs cv2.VideoCapture that has the ability to return frames in batches as opposed to one by one. Args: video_path: path to the video file """ self.video_path = video_path self.video = cv2.VideoCapture(video_path) def __len__(self): return self.video.get(cv2.CAP_PROP_FRAME_COUNT) @property def frame_counts(self): return self.__len__() @property def fps(self): return int(self.video.get(cv2.CAP_PROP_FPS)) @property def frame_size(self): return ( int(self.video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(self.video.get(cv2.CAP_PROP_FRAME_HEIGHT)), ) def __iter__(self): while True: success, frame = self.video.read() if success: yield frame else: break ================================================ FILE: asone/utils/weights.json ================================================ { "yolov5s": "1W5ypZmrYE4_Aqu3Jqsl-IDvLK6SOtJCK", "yolov5x6": "1CTYtGC8VFZD0uJbU4fcSPSjcYb8Be0jU", "yolov5n": "1q9_e76T_b353QmG5xGi3zkselGQrxuBk", "yolov5m": "1Vv3VEkgYd7WB-3e2MPo0QUKq_-F7biBP", "yolov5l": "1Wr4S7BTqqCOCP14T_aDgVxGV5h9pMM-n", "yolov5x": "18g_pjpwsnOlBKbhApuaTsbQUIDO75BSt", "yolov5n6": "1gOAZ90nKcvo7bhNZCZM-JRuY8FBeEkhl", "yolov5s6": "12W1Z0esjFc9UhiWWxCESjhm5KA3nzSwt", "yolov5m6": "1O-bbJ8WcqLig40IUyJ8ulKwj-J_KXvHr", "yolov5l6": "1sPZ-YpenYojZSIB5G1SMv2hPWX5oRwlb", "yolov6n": "1NA_u4BkPE_N8HcPmZrd7HyLmvFHOk8qd", "yolov6t": "16OWncBp-vh-sLDMOR58th3WOGv4envQ1", "yolov6s": "14BE0j654ClLxMq2ySWZNhTCmf48mLyXi", "yolov6l_relu": "14UfY057QUQoAj6q39PX_qE7U1bBIrIGi", "yolov6l": "1HdRIs0uMPbqs5E2aEX8O3d3dJTh-KBTf", "yolov6m": "1t_w9SCwbZAW7icwX_z97-SQz-plXzBgM", "yolov6s_repopt": "1L_1Crxx-4059xDDUZEf_asWRBVd3PF05", "yolov7-d6": "1_Ybtx7EAXnBwIZ59Vgo0FkXsCdjRuq2s", "yolov7-e6": "1g_2nYpeJ28cLYcOAUeztHUA5R-stk3Cm", "yolov7-e6e": "1PZwpKdHEP4Li3FkKNYn90HmGNiIjMMfi", "yolov7-tiny": "18Fels44wVJ1vG7yDuDPuWwuiAqeFxKI7", "yolov7-w6": "1wv3M23RFo0MhaujegBPY6gZ30IA894CO", "yolov7": "17iFeNfq5hKZVpQLQEgZzxF9Da5o5llLG", "yolov7x": "1zZskyvdgU45Ke8TtCA6csdCzqLrhmFYx", "yolor_csp": "1G3FBZKrznW_64mGfs6b3nAJiJv6GmmV0", "yolor_csp_star": "15WDl46ZthFGZfpOyI3qXx6gC9FQLH_wH", "yolor_csp_x": "1LU2ckh7eSpVD0nyPSdq1n34lKmNAX39T", "yolor_csp_x_star": "1jheqFDm7BpHQpR60wuWSBpbuyK5SoKdV", "yolor_p6": "1XKREKdxQCO8OXiW2IWGFhczolIaIr9sm", "yolox_l": "1jX1KHerOdZ5-dmXh6cWcRAn80aKD-7sP", "yolox_nano": "1783Os6uTpYjunL-MfK0WE1Wcwk58fIUi", "yolox_tiny": "1Lcv1ITvfPdWsu6Kb8Hq6cOSfJE7lbbf2", "yolox_darknet": "17f4UI06TWJ25Oqo2OoQGu8AoGVX1lPta", "yolox_s": "1IUAOv62XuwkwwTCVD2y3xJg7KUA3M0-M", "yolox_m": "1ktHj8UEwl0V8Qz9G74E-yj-o13FLeD0-", "yolox_x": "13HNnlILCx_XamNJWwJ1MG5x0XfP6HL1U", "ckpt": "1VZ05gzg249Q1m8BJVQxl3iHoNIbjzJf8", "yolov8s": "1hUhjQWw1cJL7TtBG0zD3MO52iCYh0DEn", "yolov8n": "1x6zHzsEcyhuyWy2xY3swAQ4vIQvBYrsr", "yolov8l": "1xQxHTEIpoiP4d73F6dtedU7hIZpFTqY2", "yolov8m": "1_FoKnqkaoWchVy4B24Hn2PanEKfh-eSp", "yolov8x": "1s60fsjiyDlPQ1L5H_GAoWahHONLbvz7T" } ================================================ FILE: asone/windows/README.md ================================================ # ASOne ## Docker Installation Instructions For Windows #### Table of Contents - [System Requirements](#system-requirements) - [Installation with Batch and Configuring Devices](#installation-with-batch-and-configuring-devices) ### System Requirements Windows machine must meet the following requirements to successfully install the docker: **With WSL 2 backend**
Type **winver** in RUN to check the version of the installed windows. - Windows 11 64-bit: Home or Pro version 21H2 or higher,\ or Enterprise or Education version 21H2 or higher - Windows 10 64-bit: Home or Pro 21H1 (build 19043) or higher,\ or Enterprise or Education 20H2 (build 19042) or higher - Enable the WSL 2 feature on Windows. For detailed instructions,\ refer to the [wsl installation](https://docs.microsoft.com/en-us/windows/wsl/install) **Hardware Requirements to run WSL 2 on Windows 10 or Windows 11**
- 64-bit processor with Second Level Address Translation (SLAT) - 4GB system RAM - BIOS-level hardware virtualization support must be enabled in the \ BIOS settings. For more information, see [Virtualization](https://docs.docker.com/desktop/troubleshoot/topics/) ## Installation with Batch and Configuring Devices 1. Download the [enable_feature.bat](enable_feature.bat) and run it as administrator. - Reboot your system. 2. Download the [installation.bat](installation.bat) and run it as administrator. - Again Reboot your system. 3. Open XLaunch and select Multiple windows - Select the option Start no client - In Extra Settings, select the option 1. Clipboard 2. Primary Selection 3. Native opengl 4. Disable access control - Save configuration file for later use 4. Open [cam2ip.exe](cam2ip-1.6-64bit-cv/cam2ip.exe) see "Listening on: 56000" - IP stream will be on `http://localhost:56000/mjpeg` You can now go back to [Installation Page](../README.md). ================================================ FILE: asone/windows/cam2ip-1.6-64bit-cv/AUTHORS ================================================ Milan Nikolic ================================================ FILE: asone/windows/cam2ip-1.6-64bit-cv/COPYING ================================================ GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . ================================================ FILE: asone/windows/enable_feature.bat ================================================ @echo off @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin" powershell.exe dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart powershell.exe dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart choco feature enable -name=exitOnRebootDetected choco feature enable -n allowGlobalConfirmation echo "REBOOT YOUR SYSTEM" pause ================================================ FILE: asone/windows/installation.bat ================================================ @echo on choco install wsl2 --params "/Version:2 /Retry:true" wsl --set-default-version 2 choco install docker-desktop choco install vcxsrv for /F "tokens=14" %i in ('"ipconfig | findstr IPv4 | findstr /i "192""') do setx DISPLAY %i:0.0 echo "REBOOT YOUR SYSTEM" Pause ================================================ FILE: asone/windows/test-display.py ================================================ import cv2 img = cv2.imread('test.jpg') cv2.imshow("TEST DISPLAY", cv2.resize(img, (600, 600)) ) cv2.waitKey(0) ================================================ FILE: asone/windows/test-webcam.py ================================================ import cv2 import numpy as np img = np.zeros((400, 600, 3)) ##Fetch IP Address # importing socket module import socket # getting the hostname by socket.gethostname() method hostname = socket.gethostname() # getting the IP address using socket.gethostbyname() method ip_address = socket.gethostbyname(hostname) # capturing the video from ip stream cap = cv2.VideoCapture(f'http://{ip_address}:56000/mjpeg') # cap.open("") while True: if not cap.isOpened(): print('Unable to load camera. Use the command "xhost +"') pass # Capture frame-by-frame ret, frame = cap.read() print(frame) # Display the resulting frame cv2.imshow('Video', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything is done, release the capture cap.release() cv2.destroyAllWindows() ================================================ FILE: docker-compose.yml ================================================ version: "3.9" services: linux: build: . image: "asone:latest" volumes: - ${PWD}:/workspace - $HOME/.Xauthority:/root/.Xauthority:rw - /dev/video0:/dev/video0 network_mode: host restart: always privileged: true environment: DISPLAY: $DISPLAY linux-gpu: build: . image: "asone:latest" volumes: - ${PWD}:/workspace - $HOME/.Xauthority:/root/.Xauthority:rw - /dev/video0:/dev/video0 network_mode: host restart: always privileged: true environment: DISPLAY: $DISPLAY deploy: resources: reservations: devices: - capabilities: [gpu] windows: build: . image: "asone:latest" volumes: - ${PWD}:/workspace # - $HOME/.Xauthority:/root/.Xauthority:rw # - /dev/video0:/dev/video0 network_mode: host restart: always privileged: true environment: DISPLAY: $DISPLAY windows-gpu: build: . image: "asone:latest" volumes: - ${PWD}:/workspace # - $HOME/.Xauthority:/root/.Xauthority:rw # - /dev/video0:/dev/video0 network_mode: host restart: always privileged: true environment: DISPLAY: $DISPLAY deploy: resources: reservations: devices: - capabilities: [gpu] ================================================ FILE: main.py ================================================ import sys import argparse import asone from asone import ASOne import torch def main(args): filter_classes = args.filter_classes if filter_classes: filter_classes = ['person'] # Check if cuda available if args.use_cuda and torch.cuda.is_available(): args.use_cuda = True else: args.use_cuda = False if sys.platform.startswith('darwin'): detector = asone.YOLOV7_MLMODEL else: detector = asone.YOLOV7_PYTORCH detect = ASOne( tracker=asone.BYTETRACK, detector=detector, weights=args.weights, sam_weights=args.sam_weights, use_cuda=args.use_cuda ) # Get tracking function track = detect.track_video(args.video_path, output_dir=args.output_dir, conf_thres=args.conf_thres, iou_thres=args.iou_thres, display=args.display, draw_trails=args.draw_trails, filter_classes=filter_classes, class_names=None) # class_names=['License Plate'] for custom weights # Loop over track_fn to retrieve outputs of each frame for bbox_details, frame_details in track: bbox_xyxy, ids, scores, class_ids = bbox_details frame, frame_num, fps = frame_details print(frame_num) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('video_path', help='Path to input video') parser.add_argument('--cpu', default=True, action='store_false', dest='use_cuda', help='run on cpu if not provided the program will run on gpu.') parser.add_argument('--no_save', default=True, action='store_false', dest='save_result', help='whether or not save results') parser.add_argument('--no_display', default=True, action='store_false', dest='display', help='whether or not display results on screen') parser.add_argument('--output_dir', default='data/results', help='Path to output directory') parser.add_argument('--draw_trails', action='store_true', default=False, help='if provided object motion trails will be drawn.') parser.add_argument('--filter_classes', default=None, help='Filter class name') parser.add_argument('-w', '--weights', default=None, help='Path of trained weights') parser.add_argument('--sam_weights', default=None, help='Path of sam weights') parser.add_argument('-ct', '--conf_thres', default=0.25, type=float, help='confidence score threshold') parser.add_argument('-it', '--iou_thres', default=0.45, type=float, help='iou score threshold') args = parser.parse_args() main(args) ================================================ FILE: requirements.txt ================================================ torch torchvision opencv-python lap loguru norfair numpy==1.23.3 #onnxruntime-gpu==1.12.1 scipy pyyaml easydict gdown pandas tabulate wheel numpy==1.23.3 asone-ocr motpy ultralytics==8.1.30 torchreid==0.2.5 tensorboard protobuf==3.20.* # requirements for coreml onnxruntime coremltools Cython==3.0.9 typing_extensions super_gradients cython_bbox thop IPython segment-anything pillow==9.5.0 ================================================ FILE: setup.py ================================================ from setuptools import setup, find_packages from pkg_resources import parse_requirements import pathlib DISTNAME = 'asone' DESCRIPTION = '' MAINTAINER = 'AxcelerateAI' MAINTAINER_EMAIL = 'umair.imran@axcelerate.ai' URL = 'https://github.com/axcelerateai/asone' DOWNLOAD_URL = URL VERSION = '0.3.2' with open('README.md') as f: long_description = f.read() requirements_txt = pathlib.Path('requirements.txt').open() def setup_package(): setup( name=DISTNAME, version=VERSION, description=DESCRIPTION, long_description = long_description, long_description_content_type='text/markdown', url=DOWNLOAD_URL, author=MAINTAINER, author_email=MAINTAINER_EMAIL, license='BSD 2-clause', keywords='asone bytetrack deepsort norfair yolo yolox yolor yolov5 yolov7 installation inferencing', # package_dir={"":""}, packages=find_packages(), dependency_links=[ "https://download.pytorch.org/whl/cu113/", 'https://pypi.python.org/simple/'], install_requires=[str(requirement) for requirement in parse_requirements(requirements_txt)], package_data={ "": ["detectors/yolor/cfg/*.cfg", "detectors/data/*.yaml", "detectors/data/*.yml", "detectors/data/*.names"], }, include_package_data=True, classifiers=[ 'Development Status :: 1 - Planning', 'Intended Audience :: Science/Research', 'License :: OSI Approved :: MIT License', 'Operating System :: POSIX :: Linux', 'Operating System :: Microsoft :: Windows :: Windows 10', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: 3.10', ], ) if __name__ == "__main__": setup_package()