{"id":13441597,"url":"https://github.com/SurajDonthi/Multi-Camera-Person-Re-Identification","last_synced_at":"2025-03-20T12:31:52.667Z","repository":{"id":44444193,"uuid":"267641546","full_name":"SurajDonthi/Multi-Camera-Person-Re-Identification","owner":"SurajDonthi","description":"State-of-the-art model for person re-identification in Multi-camera Multi-Target Tracking. Benchmarked on Market-1501 and DukeMTMTC-reID datasets.","archived":false,"fork":false,"pushed_at":"2024-01-15T11:26:00.000Z","size":1563,"stargazers_count":184,"open_issues_count":5,"forks_count":36,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-08-01T03:35:04.537Z","etag":null,"topics":["computer-vision","convolutional-neural-networks","deep-learning","dukemtmc-reid","market-1501","mtmct","multi-camera-tracking","person-reidentification","pytorch","pytorch-lightning","spatial-temporal"],"latest_commit_sha":null,"homepage":"","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/SurajDonthi.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,"roadmap":null,"authors":null}},"created_at":"2020-05-28T16:35:29.000Z","updated_at":"2024-07-20T15:36:09.000Z","dependencies_parsed_at":"2024-01-15T13:14:32.803Z","dependency_job_id":null,"html_url":"https://github.com/SurajDonthi/Multi-Camera-Person-Re-Identification","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/SurajDonthi%2FMulti-Camera-Person-Re-Identification","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SurajDonthi%2FMulti-Camera-Person-Re-Identification/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SurajDonthi%2FMulti-Camera-Person-Re-Identification/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SurajDonthi%2FMulti-Camera-Person-Re-Identification/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SurajDonthi","download_url":"https://codeload.github.com/SurajDonthi/Multi-Camera-Person-Re-Identification/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221760057,"owners_count":16876343,"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":["computer-vision","convolutional-neural-networks","deep-learning","dukemtmc-reid","market-1501","mtmct","multi-camera-tracking","person-reidentification","pytorch","pytorch-lightning","spatial-temporal"],"created_at":"2024-07-31T03:01:35.886Z","updated_at":"2025-03-20T12:31:52.637Z","avatar_url":"https://github.com/SurajDonthi.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Multi-Camera Person Re-Identification\r\n\r\nThis repository is inspired by the paper [Spatial-Temporal Reidentification (ST-ReID)](https://arxiv.org/abs/1812.03282v1)[1]. The state-of-the-art for Person Re-identification tasks. This repository offers a flexible, and easy to understand clean implementation of the model architecture, training and evaluation.\r\n\r\nThis repository has been trained \u0026 tested on [DukeMTMTC-reID](https://megapixels.cc/duke_mtmc/) and [Market-1501 datasets](https://www.kaggle.com/pengcw1/market-1501). The model can be easily trained on any new datasets with a few tweaks to parse the files!\r\n\r\n\u003eYou can do a quick run on Google Colab: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/SurajDonthi/Multi-Camera-Person-Re-Identification/blob/master/demo.ipynb)\r\n\r\n\r\nBelow are the metrics on the various datasets.\r\n\r\n| Model                 | Size | Dataset | mAP  | CMC: Top1 | CMC: Top5 |\r\n| --------------------- | ---- | ------- | ---- | --------- | --------- |\r\n| `resnet50-PCB+rerank` |      | Market  | 95.5 | 98.0      | 98.9      |\r\n| `resnet50-PCB+rerank` |      | Duke    | 92.7 | 94.5      | 96.8      |\r\n\u003c!---| `resnet50-ft_dense`   |      | Market  |      |           |           |---\u003e\r\n\r\n\r\n## Model Architecture\r\n\r\n\u003cp align=\"center\"\u003e\r\n  \u003cimg src=\"https://raw.githubusercontent.com/SurajDonthi/Clean-ST-ReID-Multi-Target-Multi-Camera-Tracking/master/imgs/model-architecture.png\" width=800 alt=\"MTMCT ST-ReID Model Architecture\"\u003e\r\n  \u003cbr\u003e\r\n  \u003ci\u003eSource: \u003ca href=\"https://arxiv.org/pdf/1812.03282.pdf\"\u003eSpatial-Temporal Reidentification(ST-ReID)\u003c/a\u003e\u003c/i\u003e\r\n\u003c/p\u003e\r\n\r\n1. A pre-trained ResNet-50 backbone model with layers up until Adaptive Average Pooling(excluded) is used\r\n\r\n**During Training**\r\n\r\n\u003e 1. The last Convolutional layer is broken into 6 (Final output size: 6 x 1) parts and separately used for predicting the person label.\r\n\u003e 2. The total loss of the 6 part predictions are calculated for backpropagation \u0026 weights update.\r\n\r\n**During Testing/Evaluation/Deployment**\r\n\r\n\u003e 1. Only the visual feature stream up until Adaptive Average Pooling is used.\r\n\u003e 2. The feature vector of the query image is compared against all the feature vectors of the gallery images using a simple dot product \u0026 normalization.\r\n\u003e 3. The Spatio-Temporal distribution is used to calculate their spatio-temporal scores.\r\n\u003e 4. The joint score is then calculated from the feature score and the spatio-temporal scores.\r\n\u003e 5. The Cumulated Matching Score is used to find the best matching for person from the gallet set.\r\n\r\n## Getting Started\r\nRun the below commands in the shell.\r\n\r\n1. Clone this repo, cd into it \u0026 install setup.py: \r\n```sh\r\ngit clone https://github.com/SurajDonthi/MTMCT-Person-Re-Identification\r\n\r\ncd MTMCT-Person-Re-Identification\r\n\r\npip install -r requirements.txt\r\n```\r\n2. Download the datasets. (By default you can download \u0026 unzip them to `data/raw/` directory)\r\n\r\nYou can get started by training this model. Trained models will be available soon!\r\n\r\n*Dependencies*\r\n\r\nThis project requires `pytorch\u003e=1.5.0`, `torchvision\u003e=0.6.0`, `pytorch-lightning=1.1.1`, `tensorboard`, `joblib` and other common packages like `numpy`, `matplotlib` and `csv`.\r\n\r\nNOTE: This project uses [pytorch-lightning](https://pytorch-lightning.readthedocs.io/en/latest/introduction_guide.html) which is a high-level interface to abstract away repeating Pytorch code. It helps achieve clean, \u0026 easy to maintain code with hardly any learning curve!\r\n\r\n#### Train with your own dataset\r\n\r\nRun the below command in the shell.\r\n\r\n```sh\r\npython -m mtmct_reid.train --data_dir path/to/dataset/ --dataset 'market' \\\r\n    --save_distribution path/to/dataset/st_distribution.pkl --gpus 1 --max_epochs 60\r\n```\r\n\r\nFor a detailed list of arguments you can pass, refer to [`hparams.csv`](https://github.com/SurajDonthi/MTMCT-Person-Re-Identification/blob/master/hparams.csv)\r\n\r\n#### Monitor the training on Tensorboard\r\n\r\nLog files are created to track the training in a new folder `logs`. To monitor the training, run the below command in the shell\r\n\r\n```sh\r\ntensorboard --logdir logs/\r\n```\r\n\r\n### Prediction/Evaluation\r\n\r\nUsing commandline:\r\n\r\n```sh\r\npython -m mtmct_reid.eval model_path 'path/to/model' --dataset 'market' \\\r\n    --query_data_dir 'path/to/query_data/' --gallery_data_dir 'path/to/gallery_data' \\\r\n    --st_distribution_path 'path/to/spatio-temporal_distribution' \\\r\n    --batch_size 64 --num_workers 4 --re_rank True\r\n```\r\n\u003c!--\r\nOr simply use the code below:\r\n\r\n```python\r\nfrom argparse import Namespace\r\n\r\nimport joblib\r\nimport torch\r\nfrom torch.utils.data import DataLoader\r\nfrom torchvision.transforms import transforms\r\n\r\nfrom mtmct_reid.data import ReIDDataset\r\nfrom mtmct_reid.metrics import joint_scores, mAP\r\nfrom mtmct_reid.model import PCB\r\nfrom mtmct_reid.re_ranking import re_ranking\r\nfrom mtmct_reid.utils import fliplr, l2_norm_standardize\r\n\r\n\r\ndef generate_features(model, dataloader):\r\n    all_features = torch.tensor()\r\n    all_targets = torch.Tensor()\r\n    all_cam_ids = torch.Tensor()\r\n    all_frames = torch.Tensor()\r\n\r\n    for batch in dataloader:\r\n        x, targets, cam_ids, frames = batch\r\n\r\n        features = model(x).detach().cpu()\r\n        features += model(fliplr(x, x.device)).detach.cpu()\r\n\r\n        all_features = torch.cat([all_features, features])\r\n        all_targets = torch.cat([all_targets, targets])\r\n        all_cam_ids = torch.cat([all_cam_ids, cam_ids])\r\n        all_frames = torch.cat([all_frames, frames])\r\n\r\n    all_features = l2_norm_standardize(all_features)\r\n\r\n    return all_features, all_targets, all_cam_ids, all_frames\r\n\r\n\r\ndef main(args):\r\n    # Load the data\r\n    transform = transforms.Compose([\r\n        transforms.Resize(size=(384, 192), interpolation=3),\r\n        transforms.ToTensor(),\r\n        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\r\n    ])\r\n    query_data = ReIDDataset(data_dir=args.query_data_dir, dataset=args.dataset, transform=transform)\r\n    query_dataloader = DataLoader(query_data, batch_size=args.batch_size,\r\n                                  shuffle=True, num_workers=args.num_workers,\r\n                                  pin_memory=True)\r\n\r\n    gal_data = ReIDDataset(data_dir=args.gallery_data_dir, dataset=args.dataset, transform=transform)\r\n    gal_dataloader = DataLoader(gal_data, batch_size=args.batch_size,\r\n                                shuffle=True, num_workers=args.num_workers,\r\n                                pin_memory=True)\r\n\r\n    # Load the model\r\n    model = PCB(num_classes=len(query_data.num_classes))\r\n    model.load_state_dict(args.model_path)\r\n    model.eval()\r\n\r\n    # Generate the feature vectors\r\n    q_features, q_targets, q_cam_ids, q_frames = generate_features(\r\n        model, query_dataloader)\r\n    g_features, g_targets, g_cam_ids, g_frames = generate_features(\r\n        model, gal_dataloader)\r\n\r\n    # Load Spatial-Temporal Distribution\r\n    st_distribution = joblib.load(args.st_distribution_path)\r\n\r\n    scores = joint_scores(q_features, q_cam_ids, q_frames,\r\n                          g_features, g_cam_ids, g_frames,\r\n                          st_distribution)\r\n\r\n    if args.re_rank:\r\n        scores = re_ranking(scores)\r\n\r\n    mean_ap, cmc = mAP(scores, q_targets,\r\n                       q_cam_ids,\r\n                       g_targets,\r\n                       g_cam_ids)\r\n\r\n    print_result = \\\r\n        f\"\"\"\r\n    x-------------x TEST RESULT x-------------x\r\n        mAP: {mean_ap},\r\n        Rank-1: {cmc[0]},\r\n        Rank-5: {cmc[4]},\r\n        Rank-10: {cmc[9]}\r\n    x-----------------------------------------x\r\n    \"\"\"\r\n    print(print_result)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n\r\n    args = dict(\r\n        model_path='path/to/model',\r\n        dataset='market',\r\n        query_data_dir='path/to/query_data',\r\n        gallery_data_dir='path/to/gallery_data',\r\n        st_distribution_path='path/to/spatio-temporal_distribution',\r\n        batch_size=64,\r\n        num_workers=4,\r\n        re_rank=True\r\n    )\r\n    args = Namespace(**args)    \r\n\r\n    main(args)\r\n```\r\n--\u003e\r\n\r\n## Metrics\r\n\r\nThe evaluation metrics used are mAP (mean Average Precision) \u0026 CMC (Cumulated Matching Characteristics)\r\n\r\nFinding the best matches during testing:\r\n\r\n Step 1: From a given dataset, compute it's Spatial-Temporal Distribution.\r\n \r\n \u003e\u003e Requires: cam_ids, targets(labels), frames, MODEL is not required!\r\n         \r\n Step 2: Compute it's Gaussian smoothed ST-Distribution.\r\n \r\n\u003e\u003e Requires: cam_ids, targets(labels), frames, MODEL is not required!\r\n         \r\n Step 3: Compute the L2-Normed features that is generated from the model.\r\n \r\n\u003e\u003e Requires: Features - Performed once training is finished!\r\n \r\n Step 4: Compute the Joint Scores.\r\n \r\n\u003e\u003e Requires: Smoothed Distribution \u0026 L2-Normed Features, cam_ids, frames\r\n \r\n Step 5: Optionally perform Re-ranking of the Generated scores.\r\n\r\n\u003e\u003e Requires: Joint Scores\r\n \r\n Step 6: Compute mAP \u0026 CMC (Cumulated Matching Characteristics; for Rank-1,Rank-5, Rank-10) for each query.\r\n \r\n\u003e\u003e Requires: Reranked/Joint Scores, (query labels \u0026 cams),\r\n                   (gallery labels \u0026 cams)\r\n\r\n\r\n**References:**\r\n\r\n[1] - [Spatial-Temporal Reidentification(ST-ReID)](https://arxiv.org/pdf/1812.03282.pdf)\r\n\r\n[2] - [Beyond Parts Models: Person Retrieval with Refined Part Pooling](https://arxiv.org/pdf/1711.09349)\r\n\r\n**Related repos:**\r\n\r\nThe model logic is mainly based on this [repository](https://github.com/Wanggcong/Spatial-Temporal-Re-identification).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSurajDonthi%2FMulti-Camera-Person-Re-Identification","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSurajDonthi%2FMulti-Camera-Person-Re-Identification","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSurajDonthi%2FMulti-Camera-Person-Re-Identification/lists"}