{"id":15027981,"url":"https://github.com/qfgaohao/pytorch-ssd","last_synced_at":"2025-05-15T18:03:35.617Z","repository":{"id":40636280,"uuid":"133904817","full_name":"qfgaohao/pytorch-ssd","owner":"qfgaohao","description":"MobileNetV1, MobileNetV2, VGG based SSD/SSD-lite implementation in Pytorch 1.0 / Pytorch 0.4. Out-of-box support for retraining on Open Images dataset. ONNX and Caffe2 support. Experiment Ideas like CoordConv.","archived":false,"fork":false,"pushed_at":"2023-03-11T09:50:27.000Z","size":1081,"stargazers_count":1408,"open_issues_count":112,"forks_count":535,"subscribers_count":39,"default_branch":"master","last_synced_at":"2025-03-31T22:16:55.617Z","etag":null,"topics":["object-detection","open-images","pytorch","ssd"],"latest_commit_sha":null,"homepage":"https://medium.com/@smallfishbigsea/understand-ssd-and-implement-your-own-caa3232cd6ad","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qfgaohao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-05-18T04:56:09.000Z","updated_at":"2025-03-31T13:41:17.000Z","dependencies_parsed_at":"2023-01-31T11:45:44.503Z","dependency_job_id":"20011412-13f9-4273-9727-eb461b475569","html_url":"https://github.com/qfgaohao/pytorch-ssd","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qfgaohao%2Fpytorch-ssd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qfgaohao%2Fpytorch-ssd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qfgaohao%2Fpytorch-ssd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qfgaohao%2Fpytorch-ssd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qfgaohao","download_url":"https://codeload.github.com/qfgaohao/pytorch-ssd/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247744333,"owners_count":20988783,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["object-detection","open-images","pytorch","ssd"],"created_at":"2024-09-24T20:07:24.583Z","updated_at":"2025-04-07T23:06:37.852Z","avatar_url":"https://github.com/qfgaohao.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Single Shot MultiBox Detector Implementation in Pytorch\n\nThis repo implements [SSD (Single Shot MultiBox Detector)](https://arxiv.org/abs/1512.02325). The implementation is heavily influenced by the projects [ssd.pytorch](https://github.com/amdegroot/ssd.pytorch) and [Detectron](https://github.com/facebookresearch/Detectron).\nThe design goal is modularity and extensibility.\n\nCurrently, it has MobileNetV1, MobileNetV2, and VGG based SSD/SSD-Lite implementations. \n\nIt also has out-of-box support for retraining on Google Open Images dataset.\n\n![Example of Mobile SSD](readme_ssd_example.jpg  \"Example of Mobile SSD(Courtesy of https://www.pexels.com/@mirit-assaf-299757 for the image.\")\n\n## Dependencies\n1. Python 3.6+\n2. OpenCV\n3. Pytorch 1.0 or Pytorch 0.4+\n4. Caffe2\n5. Pandas\n6. Boto3 if you want to train models on the Google OpenImages Dataset.\n\n## Download models\n\n**Please download the models and put them into the folder \"./models\". The following sections will need them.** URL: https://drive.google.com/drive/folders/1pKn-RifvJGWiOx0ZCRLtCXM5GT5lAluu?usp=sharing\n\n## Run the demo\n### Run the live MobilenetV1 SSD demo\n\n```bash\n# If you haven't downloaded the models, please download from https://drive.google.com/drive/folders/1pKn-RifvJGWiOx0ZCRLtCXM5GT5lAluu?usp=sharing.\npython run_ssd_live_demo.py mb1-ssd models/mobilenet-v1-ssd-mp-0_675.pth models/voc-model-labels.txt \n```\n### Run the live demo in Caffe2\n\n```bash\n# If you haven't downloaded the models, please download from https://drive.google.com/drive/folders/1pKn-RifvJGWiOx0ZCRLtCXM5GT5lAluu?usp=sharing.\npython run_ssd_live_caffe2.py models/mobilenet-v1-ssd_init_net.pb models/mobilenet-v1-ssd_predict_net.pb models/voc-model-labels.txt \n```\n\nYou can see a decent speed boost by using Caffe2.\n\n### Run the live MobileNetV2 SSD Lite demo\n\n```bash\n# If you haven't downloaded the models, please download from https://drive.google.com/drive/folders/1pKn-RifvJGWiOx0ZCRLtCXM5GT5lAluu?usp=sharing.\npython run_ssd_live_demo.py mb2-ssd-lite models/mb2-ssd-lite-mp-0_686.pth models/voc-model-labels.txt \n```\n\nThe above MobileNetV2 SSD-Lite model is not ONNX-Compatible, as it uses Relu6 which is not supported by ONNX.\nThe code supports the ONNX-Compatible version. Once I have trained a good enough MobileNetV2 model with Relu, I will upload\nthe corresponding Pytorch and Caffe2 models.\n\nYou may notice MobileNetV2 SSD/SSD-Lite is slower than MobileNetV1 SSD/Lite on PC. However, MobileNetV2 is faster on mobile devices.\n\n## Pretrained Models\n\n### Mobilenet V1 SSD\n\nIf you haven't downloaded the models, please download from https://drive.google.com/drive/folders/1pKn-RifvJGWiOx0ZCRLtCXM5GT5lAluu?usp=sharing.\n\nModel: mobilenet-v1-ssd-mp-0_675.pth\n\n```\nAverage Precision Per-class:\naeroplane: 0.6742489426027927\nbicycle: 0.7913672875238116\nbird: 0.612096015101108\nboat: 0.5616407126931772\nbottle: 0.3471259064860268\nbus: 0.7742298893362103\ncar: 0.7284171192326804\ncat: 0.8360675520354323\nchair: 0.5142295855384792\ncow: 0.6244090341627014\ndiningtable: 0.7060035669312754\ndog: 0.7849252606216821\nhorse: 0.8202146617282785\nmotorbike: 0.793578272243471\nperson: 0.7042670984734087\npottedplant: 0.40257147509774405\nsheep: 0.6071252282334352\nsofa: 0.7549120254763918\ntrain: 0.8270992920206008\ntvmonitor: 0.6459903029666852\n\nAverage Precision Across All Classes:0.6755\n```\n\n### MobileNetV2 SSD-Lite\n\nIf you haven't downloaded the models, please download from https://drive.google.com/drive/folders/1pKn-RifvJGWiOx0ZCRLtCXM5GT5lAluu?usp=sharing.\n\nModel: mb2-ssd-lite-mp-0_686.pth\n\n```\nAverage Precision Per-class:\naeroplane: 0.6973327307871002\nbicycle: 0.7823755921687233\nbird: 0.6342429230125619\nboat: 0.5478160937380846\nbottle: 0.3564069147093762\nbus: 0.7882037885117419\ncar: 0.7444122242934775\ncat: 0.8198865557991936\nchair: 0.5378973422880109\ncow: 0.6186076149254742\ndiningtable: 0.7369559500950861\ndog: 0.7848265495754562\nhorse: 0.8222948787839229\nmotorbike: 0.8057808854619948\nperson: 0.7176976451996411\npottedplant: 0.42802932547480066\nsheep: 0.6259124005994047\nsofa: 0.7840368059271103\ntrain: 0.8331588002612781\ntvmonitor: 0.6555051795079904\nAverage Precision Across All Classes:0.6860690100560214\n```\n\nThe code to re-produce the model:\n\n```bash\n# If you haven't downloaded the models, please download from https://drive.google.com/drive/folders/1pKn-RifvJGWiOx0ZCRLtCXM5GT5lAluu?usp=sharing.\npython train_ssd.py --dataset_type voc  --datasets ~/data/VOC0712/VOC2007 ~/data/VOC0712/VOC2012 --validation_dataset ~/data/VOC0712/test/VOC2007/ --net mb2-ssd-lite --base_net models/mb2-imagenet-71_8.pth  --scheduler cosine --lr 0.01 --t_max 200 --validation_epochs 5 --num_epochs 200\n```\n\n### VGG SSD\n\nModel: vgg16-ssd-mp-0_7726.pth\n\n\n```\nAverage Precision Per-class:\naeroplane: 0.7957406334737802\nbicycle: 0.8305351156180996\nbird: 0.7570969203281721\nboat: 0.7043869846367731\nbottle: 0.5151666571756393\nbus: 0.8375121237865507\ncar: 0.8581508869699901\ncat: 0.8696185705648963\nchair: 0.6165431194526735\ncow: 0.8066422244852381\ndiningtable: 0.7629391213959706\ndog: 0.8444541531856452\nhorse: 0.8691922094815812\nmotorbike: 0.8496564646906418\nperson: 0.793785185549561\npottedplant: 0.5233462463152305\nsheep: 0.7786762429478917\nsofa: 0.8024887701948746\ntrain: 0.8713861172265407\ntvmonitor: 0.7650514925384194\nAverage Precision Across All Classes:0.7726184620009084\n```\n\nThe code to re-produce the model:\n\n```bash\nwget -P models https://s3.amazonaws.com/amdegroot-models/vgg16_reducedfc.pth\npython train_ssd.py --datasets ~/data/VOC0712/VOC2007/ ~/data/VOC0712/VOC2012/ --validation_dataset ~/data/VOC0712/test/VOC2007/ --net vgg16-ssd --base_net models/vgg16_reducedfc.pth  --batch_size 24 --num_epochs 200 --scheduler \"multi-step” —-milestones “120,160”\n```\n## Training\n\n```bash\npython train_ssd.py --datasets ~/data/VOC0712/VOC2007/ ~/data/VOC0712/VOC2012/ --validation_dataset ~/data/VOC0712/test/VOC2007/ --net mb1-ssd --base_net models/mobilenet_v1_with_relu_69_5.pth  --batch_size 24 --num_epochs 200 --scheduler cosine --lr 0.01 --t_max 200\n```\n\n\nThe dataset path is the parent directory of the folders: Annotations, ImageSets, JPEGImages, SegmentationClass and SegmentationObject. You can use multiple datasets to train.\n\n\n## Evaluation\n\n```bash\npython eval_ssd.py --net mb1-ssd  --dataset ~/data/VOC0712/test/VOC2007/ --trained_model models/mobilenet-v1-ssd-mp-0_675.pth --label_file models/voc-model-labels.txt \n```\n\n## Convert models to ONNX and Caffe2 models\n\n```bash\npython convert_to_caffe2_models.py mb1-ssd models/mobilenet-v1-ssd-mp-0_675.pth models/voc-model-labels.txt \n```\n\nThe converted models are models/mobilenet-v1-ssd.onnx, models/mobilenet-v1-ssd_init_net.pb and models/mobilenet-v1-ssd_predict_net.pb. The models in the format of pbtxt are also saved for reference.\n\n## Retrain on Open Images Dataset\n\nLet's we are building a model to detect guns for security purpose.\n\nBefore you start you can try the demo.\n\n```bash\npython run_ssd_example.py mb1-ssd models/gun_model_2.21.pth models/open-images-model-labels.txt ~/Downloads/big.JPG\n```\n\n![Example of Gun Detection](gun.jpg)\n\n\nIf you manage to get more annotated data, the accuracy could become much higher.\n\n### Download data\n\n```bash\npython open_images_downloader.py --root ~/data/open_images --class_names \"Handgun,Shotgun\" --num_workers 20\n```\n\nIt will download data into the folder ~/data/open_images.\n\nThe content of the data directory looks as follows.\n\n```\nclass-descriptions-boxable.csv       test                        validation\nsub-test-annotations-bbox.csv        test-annotations-bbox.csv   validation-annotations-bbox.csv\nsub-train-annotations-bbox.csv       train\nsub-validation-annotations-bbox.csv  train-annotations-bbox.csv\n```\n\nThe folders train, test, validation contain the images. The files like sub-train-annotations-bbox.csv \nis the annotation file.\n\n### Retrain\n\n```bash\npython train_ssd.py --dataset_type open_images --datasets ~/data/open_images --net mb1-ssd --pretrained_ssd models/mobilenet-v1-ssd-mp-0_675.pth --scheduler cosine --lr 0.01 --t_max 100 --validation_epochs 5 --num_epochs 100 --base_net_lr 0.001  --batch_size 5\n```\n\nYou can freeze the base net, or all the layers except the prediction heads. \n\n```\n  --freeze_base_net     Freeze base net layers.\n  --freeze_net          Freeze all the layers except the prediction head.\n```\n\nYou can also use different learning rates \nfor the base net, the extra layers and the prediction heads.\n\n```\n  --lr LR, --learning-rate LR\n  --base_net_lr BASE_NET_LR\n                        initial learning rate for base net.\n  --extra_layers_lr EXTRA_LAYERS_LR\n```\n\nAs subsets of open images data can be very unbalanced, it also provides\na handy option to roughly balance the data.\n\n```\n  --balance_data        Balance training data by down-sampling more frequent\n                        labels.\n```\n\n### Test on image\n\n```bash\npython run_ssd_example.py mb1-ssd models/mobilenet-v1-ssd-Epoch-99-Loss-2.2184619531035423.pth models/open-images-model-labels.txt ~/Downloads/gun.JPG\n```\n\n\n## ONNX Friendly VGG16 SSD\n\n! The model is not really ONNX-Friendly due the issue mentioned here \"https://github.com/qfgaohao/pytorch-ssd/issues/33#issuecomment-467533485\"\n\nThe Scaled L2 Norm Layer has been replaced with BatchNorm to make the net ONNX compatible.\n\n### Train\n\nThe pretrained based is borrowed from https://s3.amazonaws.com/amdegroot-models/vgg16_reducedfc.pth .\n\n```bash\npython train_ssd.py --datasets ~/data/VOC0712/VOC2007/ ~/data/VOC0712/VOC2012/ --validation_dataset ~/data/VOC0712/test/VOC2007/ --net \"vgg16-ssd\" --base_net models/vgg16_reducedfc.pth  --batch_size 24 --num_epochs 150 --scheduler cosine --lr 0.0012 --t_max 150 --validation_epochs 5\n```\n\n### Eval\n\n```bash\npython eval_ssd.py --net vgg16-ssd  --dataset ~/data/VOC0712/test/VOC2007/ --trained_model models/vgg16-ssd-Epoch-115-Loss-2.819455094383535.pth --label_file models/voc-model-labels.txt\n```\n\n## TODO\n\n1. Resnet34 Based Model.\n2. BatchNorm Fusion.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqfgaohao%2Fpytorch-ssd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqfgaohao%2Fpytorch-ssd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqfgaohao%2Fpytorch-ssd/lists"}