عکس pooya-mohammadi
This repository presents a full process of training a vehicle tracking model using `deep-person-reid` repository. Further, a complete web-application service based on FastAPI is provided that extracts features using YoloV5 and trained model.Python
موضوع‌ها
۰
فورک‌ها
۰
ستاره‌ها
۱۱
تاریخ ایجاد
۴ فروردین ۱۴۰۱
آخرین بروزرسانی
حدود ۱ ماه قبل
لایسنس
Apache License 2.0

Vehicle ReID using ReID-Person-Deep

Vehicle tracking/re-identification is one of the major deep learning problems that is amply popular in surveillance applications. In this repository, we'll train a vehicle tracking model using deep-person-reid repository.

Preparing Dataset

Based on the instructions provided by deep-person-reid in their blog, 3 subset of data should be generated:

  1. train: Main training set should be larger than the others
  2. gallery: Used for evaluation
  3. query: Used for evaluation

Link-to-instruction

Install Requirements

pip install requirements.txt

Download dataset

Download the dataset from the following google-drive link and put it in veri directory. Then, unzip it.

Link-to-dataset

# Download
cd veri
gdown --id 1wLmUWY5clm88Jcmu1e5ITMYNCht_mnds
# Unzip
unzip VeRi_ReID_Simulation.zip

Note: I don't remember where did I get the link to this dataset. Although this is a simulation dataset, if it's private and should not be published publicly open an issue or email me I remove it immediately!

Each dataset sample should have 3 identifiers in its name:

  1. image_id: different images belonging to a vehicle should have a unique image_id
  2. camera_id: different images that are taken with a camera should have a unique camera_id. If generalization for various cameras is not your concern you may give all your samples a same id.
  3. An arbitrary number to unify images

As explained in the dataset instructions which is provided in the following quote, image_id should be the main key for generating train, gallery, and query datasets.

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)

Using the following code generate 3 aforementioned subsets:

cd veri
python generate_subsets.py

Train

Clone the deep-person-reid repo and install the requirements:

git clone https://github.com/KaiyangZhou/deep-person-reid.git
cd deep-person-reid
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
python setup.py develop

For training run the train.py

# cd to project root
python train.py --source veri --model_type osnet_x0_25

The output should be like the following:

** Results **
mAP: 84.9%
CMC curve
Rank-1  : 97.7%
Rank-5  : 99.6%
Rank-10 : 99.8%
Rank-20 : 99.9%
Checkpoint saved to "log/osnet_x0_25/model/model.pth.tar-20"
Elapsed 0:25:59

Inference

Inference can be applied on any video. To make it easier, there is sample that can be downloaded with the following command:

gdown --id 1JVnzhFcUmZxMAxMazJ__AbkRksoBSag0

For Tracking deep_sort is used which is taken from Yolov5_DeepSort_Pytorch GitHub repository and wrapped by deep_utils. In addition to a re-identification model, an object detection model is required which in this case yolov5 with deep_utils wrapper is used!

Used the pretrained models

Download yolov5s.pt & vehicle-reid models using the following commands:

gdown --id 1JVnzhFcUmZxMAxMazJ__AbkRksoBSag0
gdown --id 1vyFhX18vcDb5BQIGa2jGXnQJ9Bi2tU25

For inference run the following command:

# pretrained
python inference.py --video_path test_traffic.mp4 --model_path model.pth.tar-20
# trained by you
python inference.py --video_path test_traffic.mp4 --model_path log/osnet_x0_25/model/model.pth.tar-20

The result should be like the following gif:

References

  1. https://github.com/KaiyangZhou/deep-person-reid.git
  2. https://kaiyangzhou.github.io/deep-person-reid/user_guide#use-your-own-dataset
  3. https://github.com/mikel-brostrom/Yolov5_DeepSort_Pytorch
  4. https://github.com/pooya-mohammadi/deep_utils