### Install New Dependencies in Dockerfile Source: https://github.com/jdai-cv/fast-reid/blob/master/docker/README.md Demonstrates how to add new system dependencies to the FastReID Docker image by modifying the Dockerfile. This example shows how to install the 'vim' editor using apt-get. ```shell script RUN sudo apt-get update && sudo apt-get install -y vim ``` -------------------------------- ### Command Line Training and Evaluation Source: https://context7.com/jdai-cv/fast-reid/llms.txt Examples of how to initiate training and evaluation from the command line, covering single-GPU, multi-GPU, distributed training, and evaluation-only modes. ```bash # Single GPU training python tools/train_net.py \ --config-file configs/Market1501/bagtricks_R50.yml \ MODEL.DEVICE "cuda:0" # Multi-GPU training (4 GPUs) python tools/train_net.py \ --config-file configs/Market1501/bagtricks_R50.yml \ --num-gpus 4 # Distributed training across multiple machines # Machine 1: export GLOO_SOCKET_IFNAME=eth0 export NCCL_SOCKET_IFNAME=eth0 python tools/train_net.py \ --config-file configs/Market1501/bagtricks_R50.yml \ --num-gpus 4 --num-machines 2 --machine-rank 0 \ --dist-url tcp://master_ip:port # Machine 2: python tools/train_net.py \ --config-file configs/Market1501/bagtricks_R50.yml \ --num-gpus 4 --num-machines 2 --machine-rank 1 \ --dist-url tcp://master_ip:port # Evaluation only with pre-trained model python tools/train_net.py \ --config-file configs/Market1501/bagtricks_R50.yml \ --eval-only \ MODEL.WEIGHTS /path/to/model_final.pth \ MODEL.DEVICE "cuda:0" ``` -------------------------------- ### Install FastReID and Dependencies Source: https://context7.com/jdai-cv/fast-reid/llms.txt Commands to set up the Conda environment, install PyTorch, and other project dependencies. Includes optional compilation for faster evaluation. ```bash # Create and activate conda environment conda create -n fastreid python=3.7 conda activate fastreid conda install pytorch==1.6.0 torchvision tensorboard -c pytorch pip install -r docs/requirements.txt # Optional: Compile cython evaluation for faster ranking cd fastreid/evaluation/rank_cylib && make all # Set dataset path (default is ./datasets/) export FASTREID_DATASETS=/path/to/datasets/ ``` -------------------------------- ### Setup Fast-ReID Environment with Conda Source: https://github.com/jdai-cv/fast-reid/blob/master/INSTALL.md This script creates a new Conda environment, installs the required PyTorch and torchvision versions, and installs additional project dependencies from the requirements file. ```shell conda create -n fastreid python=3.7 conda activate fastreid conda install pytorch==1.6.0 torchvision tensorboard -c pytorch pip install -r docs/requirements.txt ``` -------------------------------- ### Training Pipeline with DefaultTrainer Source: https://context7.com/jdai-cv/fast-reid/llms.txt Illustrates the usage of the `DefaultTrainer` class for a complete training pipeline. Includes setup, model building, checkpoint loading, and the training/evaluation loop. ```python import sys sys.path.append('.') from fastreid.config import get_cfg from fastreid.engine import DefaultTrainer, default_argument_parser, default_setup, launch def setup(args): """Create configs and perform basic setups.""" cfg = get_cfg() cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() default_setup(cfg, args) return cfg def main(args): cfg = setup(args) # For evaluation only if args.eval_only: cfg.defrost() cfg.MODEL.BACKBONE.PRETRAIN = False model = DefaultTrainer.build_model(cfg) from fastreid.utils.checkpoint import Checkpointer Checkpointer(model).load(cfg.MODEL.WEIGHTS) res = DefaultTrainer.test(cfg, model) return res # For training trainer = DefaultTrainer(cfg) trainer.resume_or_load(resume=args.resume) return trainer.train() if __name__ == "__main__": args = default_argument_parser().parse_args() launch( main, args.num_gpus, num_machines=args.num_machines, machine_rank=args.machine_rank, dist_url=args.dist_url, args=(args,), ) ``` -------------------------------- ### CMake Build Configuration and Installation Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/fastrt/CMakeLists.txt This snippet configures C++ compiler flags for optimization and defines the installation rules for the built library. It also includes subdirectories for various project modules. ```cmake set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${SOLUTION_DIR}/libs/${PROJECT_NAME}) add_subdirectory(layers) add_subdirectory(engine) add_subdirectory(heads) add_subdirectory(backbones) add_subdirectory(meta_arch) add_subdirectory(factory) ``` -------------------------------- ### CMake Project Setup and Library Inclusion Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/fastrt/CMakeLists.txt This snippet defines the project name and includes source files using GLOB_RECURSE. It also finds and includes directories for CUDA and TensorRT, essential for GPU acceleration and deep learning inference. ```cmake project(FastRTEngine) file(GLOB_RECURSE COMMON_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/common/utils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/common/calibrator.cpp ) find_package(CUDA REQUIRED) # include and link dirs of cuda and tensorrt, you need adapt them if yours are different # cuda include_directories(/usr/local/cuda/include) link_directories(/usr/local/cuda/lib64) # tensorrt include_directories(/usr/include/x86_64-linux-gnu/) link_directories(/usr/lib/x86_64-linux-gnu/) ``` -------------------------------- ### Train FastAttr Model Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastAttr/README.md Executes the training process for the FastAttr model using a specified configuration file. This command requires a multi-GPU setup to reproduce the baseline performance results. ```bash python3 projects/FastAttr/train_net.py --config-file projects/FastAttr/configs/pa100.yml --num-gpus 4 ``` -------------------------------- ### Build and Use ReID Models with build_model Source: https://context7.com/jdai-cv/fast-reid/llms.txt Constructs ReID model architectures defined in configuration files. It supports loading custom weights and performing inference to extract features. Requires PyTorch and FastReID installation. ```python import torch from fastreid.config import get_cfg from fastreid.modeling.meta_arch import build_model from fastreid.utils.checkpoint import Checkpointer # Setup configuration cfg = get_cfg() cfg.merge_from_file("configs/Market1501/bagtricks_R50.yml") cfg.MODEL.BACKBONE.PRETRAIN = False # Disable pretrain when loading custom weights # Build model model = build_model(cfg) model.eval() # Load trained weights Checkpointer(model).load("/path/to/model_final.pth") # Run inference # Input format: dict with 'images' key containing tensor of shape (B, C, H, W) with torch.no_grad(): inputs = {"images": torch.randn(4, 3, 256, 128).to(model.device)} features = model(inputs) # Output: (B, feature_dim) normalized features print(f"Feature shape: {features.shape}") # Expected: torch.Size([4, 2048]) ``` -------------------------------- ### Build TensorRT model as shared libraries Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/README.md This process builds the TensorRT model as shared libraries, disabling the demo functionality. The libraries are installed in 'FastRT/libs/FastRTEngine/'. Subsequently, a separate build command is used to create an application executable that utilizes these libraries. ```bash mkdir build cd build cmake -DBUILD_FASTRT_ENGINE=ON \ -DBUILD_DEMO=OFF \ -DBUILD_FP16=ON .. make make install ``` ```bash cmake -DBUILD_FASTRT_ENGINE=OFF -DBUILD_DEMO=ON .. make ``` -------------------------------- ### Compile Cython evaluation modules Source: https://github.com/jdai-cv/fast-reid/blob/master/GETTING_STARTED.md Compiles the Cython-based evaluation libraries to improve performance during the evaluation phase. This requires a standard build environment with make installed. ```bash cd fastreid/evaluation/rank_cylib; make all ``` -------------------------------- ### Python FastRT Model Inference Example Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/README.md This Python code snippet demonstrates how to use the FastRT model with its Python interface. It involves importing the ReID class, initializing the model with a GPU ID, building the engine from a file, and performing inference on a frame. ```python from PATH_TO_SO_FILE import ReID model = ReID(GPU_ID) model.build(PATH_TO_YOUR_ENGINEFILE) numpy_feature = np.array([model.infer(CV2_FRAME)]) ``` -------------------------------- ### Define FastReID Model Configuration Source: https://github.com/jdai-cv/fast-reid/blob/master/docs/modules/config.md Initializes the configuration object and defines core model parameters including device, architecture, backbone settings, and head configurations. This structure allows for modular setup of the ReID pipeline. ```python _C = CN() _C.MODEL = CN() _C.MODEL.DEVICE = "cuda" _C.MODEL.META_ARCHITECTURE = "Baseline" _C.MODEL.BACKBONE = CN() _C.MODEL.BACKBONE.NAME = "build_resnet_backbone" _C.MODEL.HEADS = CN() _C.MODEL.HEADS.NAME = "EmbeddingHead" ``` -------------------------------- ### TensorRT Model Configuration for kd-r18-r101_ibn Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/README.md Configuration for a knowledge distillation (kd) model with r18 backbone. This example uses a higher batch size and a different device ID, with ibn enabled. ```cpp static const std::string WEIGHTS_PATH = "../kd-r18-r101_ibn.wts"; static const std::string ENGINE_PATH = "./kd_r18_distill.engine"; static const int MAX_BATCH_SIZE = 16; static const int INPUT_H = 384; static const int INPUT_W = 128; static const int OUTPUT_SIZE = 512; static const int DEVICE_ID = 1; static const FastreidBackboneType BACKBONE = FastreidBackboneType::r18_distill; static const FastreidHeadType HEAD = FastreidHeadType::EmbeddingHead; static const FastreidPoolingType HEAD_POOLING = FastreidPoolingType::gempoolP; static const int LAST_STRIDE = 1; static const bool WITH_IBNA = true; static const bool WITH_NL = false; static const int EMBEDDING_DIM = 0; ``` -------------------------------- ### TensorRT Model Configuration for kd-r34-r101_ibn Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/README.md Configuration for a knowledge distillation (kd) model with r34 backbone, similar to the distilled version but potentially for a different training setup. ```cpp static const std::string WEIGHTS_PATH = "../kd_r34_distill.wts"; static const std::string ENGINE_PATH = "./kd_r34_distill.engine"; static const int MAX_BATCH_SIZE = 4; static const int INPUT_H = 384; static const int INPUT_W = 128; static const int OUTPUT_SIZE = 512; static const int DEVICE_ID = 0; static const FastreidBackboneType BACKBONE = FastreidBackboneType::r34_distill; static const FastreidHeadType HEAD = FastreidHeadType::EmbeddingHead; static const FastreidPoolingType HEAD_POOLING = FastreidPoolingType::gempoolP; static const int LAST_STRIDE = 1; static const bool WITH_IBNA = false; static const bool WITH_NL = false; static const int EMBEDDING_DIM = 0; ``` -------------------------------- ### Configuration System with get_cfg Source: https://context7.com/jdai-cv/fast-reid/llms.txt Demonstrates how to use the YACS CfgNode for configuration management. Shows loading defaults, merging from files, overriding options, and freezing the configuration. ```python from fastreid.config import get_cfg # Get default configuration cfg = get_cfg() # Load configuration from YAML file cfg.merge_from_file("configs/Market1501/bagtricks_R50.yml") # Override specific options from command line or code cfg.merge_from_list([ "MODEL.DEVICE", "cuda:0", "MODEL.WEIGHTS", "/path/to/checkpoint.pth", "SOLVER.IMS_PER_BATCH", "32" ]) # Freeze configuration to prevent modifications cfg.freeze() # Access configuration values print(f"Model architecture: {cfg.MODEL.META_ARCHITECTURE}") print(f"Backbone: {cfg.MODEL.BACKBONE.NAME}") print(f"Input size: {cfg.INPUT.SIZE_TRAIN}") print(f"Learning rate: {cfg.SOLVER.BASE_LR}") ``` -------------------------------- ### Configure Fast-ReID Training Parameters Source: https://github.com/jdai-cv/fast-reid/blob/master/docs/modules/config.md This snippet demonstrates the initialization of configuration parameters for input processing, dataset definitions, data loader behavior, solver optimization, and testing evaluation in the Fast-ReID framework. ```python _C.INPUT = CN() _C.INPUT.SIZE_TRAIN = [256, 128] _C.INPUT.CROP = CN({"ENABLED": False}) _C.DATASETS = CN() _C.DATASETS.NAMES = ("Market1501",) _C.DATALOADER = CN() _C.DATALOADER.SAMPLER_TRAIN = "TrainingSampler" _C.SOLVER = CN() _C.SOLVER.OPT = "Adam" _C.SOLVER.MAX_EPOCH = 120 _C.TEST = CN() _C.TEST.METRIC = "cosine" ``` -------------------------------- ### Prototxt File Handling Source: https://github.com/jdai-cv/fast-reid/blob/master/tools/deploy/Caffe/ReadMe.md Shows how to load a prototxt definition and generate a corresponding caffemodel file using the caffe command-line tool. ```python from nn_tools.Caffe import caffe_net # Load prototxt net = caffe_net.Prototxt('deploy.prototxt') # Generate caffemodel net.init_caffemodel(caffe_cmd_path='/usr/bin/caffe') ``` -------------------------------- ### Implementing and Using a Registry in FastReID Source: https://github.com/jdai-cv/fast-reid/blob/master/docs/modules/utils.md Demonstrates how to initialize a new Registry instance and register objects using both the decorator pattern and direct method calls. This allows for flexible configuration and modular design. ```python from fastreid.utils.registry import Registry # Initialize a new registry BACKBONE_REGISTRY = Registry('BACKBONE') # Register using a decorator @BACKBONE_REGISTRY.register() class MyBackbone(): pass # Register using a direct method call BACKBONE_REGISTRY.register(MyBackbone) ``` -------------------------------- ### Build and Run FastReID Docker Container Source: https://github.com/jdai-cv/fast-reid/blob/master/docker/README.md Instructions for building the FastReID Docker image and launching a container. Requires NVIDIA Docker for GPU support. Mounts volumes for data persistence and sets up network and IPC configurations. ```shell script cd docker/ # Build: docker build -t=fastreid:v0 . # Launch (requires GPUs) nvidia-docker run -v server_path:docker_path --name=fastreid --net=host --ipc=host -it fastreid:v0 /bin/sh ``` -------------------------------- ### Initialize and Manipulate Caffe Models Source: https://github.com/jdai-cv/fast-reid/blob/master/tools/deploy/Caffe/ReadMe.md Demonstrates how to import the caffe_net module, load a model from a file, and perform basic layer data retrieval and modification. This utility requires a valid caffe.proto file for parsing. ```python from nn_tools.Caffe import caffe_net # Load a model net = caffe_net.Caffemodel('model.caffemodel') # Get layer data as numpy array weights = net.get_layer_data('conv1') # Modify layer data net.set_layer_date('conv1', [new_weights, new_bias]) # Save changes net.save('modified_model.caffemodel') ``` -------------------------------- ### Manage Model Checkpoints Source: https://context7.com/jdai-cv/fast-reid/llms.txt Illustrates saving and loading model weights, optimizer states, and scheduler states using the Checkpointer class, including resuming training. ```python import torch.nn as nn from fastreid.utils.checkpoint import Checkpointer model = build_model(cfg) checkpointer = Checkpointer(model, save_dir="logs/") checkpointer.save("model_epoch_10", epoch=10, metric=85.5) checkpoint = checkpointer.load("logs/model_epoch_10.pth") print(f"Loaded from epoch: {checkpoint.get('epoch')}") optimizer = torch.optim.Adam(model.parameters(), lr=0.001) scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=30) checkpointer = Checkpointer( model, save_dir="logs/", save_to_disk=True, optimizer=optimizer, scheduler=scheduler ) checkpointer.save("model_full", epoch=50, best_metric=92.3) checkpoint = checkpointer.resume_or_load( "pretrained_model.pth", resume=True ) if checkpointer.has_checkpoint(): latest = checkpointer.get_checkpoint_file() print(f"Found checkpoint: {latest}") ``` -------------------------------- ### Run FastReID Demo for Image Similarity Source: https://github.com/jdai-cv/fast-reid/blob/master/demo/README.md This command line script executes a demo of FastReID's built-in models. It calculates cosine similarities between different images using a specified configuration and model weights. The output includes visualizations of the results. Ensure you have the necessary configuration files and model checkpoints. ```bash python demo/visualize_result.py --config-file logs/dukemtmc/mgn_R50-ibn/config.yaml \ --parallel --vis-label --dataset-name DukeMTMC --output logs/mgn_duke_vis \ --opts MODEL.WEIGHTS logs/dukemtmc/mgn_R50-ibn/model_final.pth ``` -------------------------------- ### Configure Loss Functions Source: https://github.com/jdai-cv/fast-reid/blob/master/docs/modules/config.md Sets up various loss function parameters within the configuration object, including Cross Entropy, Focal, Triplet, and Circle losses. Each loss type includes specific hyperparameters like margins, scales, and gamma values. ```python _C.MODEL.LOSSES = CN() _C.MODEL.LOSSES.NAME = ("CrossEntropyLoss",) _C.MODEL.LOSSES.TRI = CN() _C.MODEL.LOSSES.TRI.MARGIN = 0.3 _C.MODEL.LOSSES.TRI.SCALE = 1.0 _C.MODEL.LOSSES.CIRCLE = CN() _C.MODEL.LOSSES.CIRCLE.MARGIN = 0.25 _C.MODEL.LOSSES.CIRCLE.SCALE = 1.0 ``` -------------------------------- ### Run fastrt for model serialization and inference Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/README.md These commands demonstrate how to run the 'fastrt' executable after building. The first command serializes the model and saves it as an engine file. The second command deserializes the engine file and performs inference. ```bash ./demo/fastrt -s // serialize model & save as 'xxx.engine' file ``` ```bash ./demo/fastrt -d // deserialize 'xxx.engine' file and run inference ``` -------------------------------- ### Train models via command line Source: https://github.com/jdai-cv/fast-reid/blob/master/GETTING_STARTED.md Executes training using the train_net.py script. Supports single-GPU, multi-GPU, and multi-machine distributed training configurations. ```bash # Single GPU python3 tools/train_net.py --config-file ./configs/Market1501/bagtricks_R50.yml MODEL.DEVICE "cuda:0" # Multi-GPU python3 tools/train_net.py --config-file ./configs/Market1501/bagtricks_R50.yml --num-gpus 4 # Distributed Multi-Machine export GLOO_SOCKET_IFNAME=eth0 export NCCL_SOCKET_IFNAME=eth0 python3 tools/train_net.py --config-file configs/Market1501/bagtricks_R50.yml --num-gpus 4 --num-machines 2 --machine-rank 0 --dist-url tcp://ip:port ``` -------------------------------- ### Evaluate Re-ID Models on Datasets Source: https://context7.com/jdai-cv/fast-reid/llms.txt Demonstrates how to initialize a model, load weights, and run inference on a dataset using ReidEvaluator to compute mAP, CMC, and mINP metrics. ```python from collections import OrderedDict from fastreid.config import get_cfg from fastreid.engine import DefaultTrainer from fastreid.evaluation import ReidEvaluator, inference_on_dataset from fastreid.modeling.meta_arch import build_model from fastreid.utils.checkpoint import Checkpointer cfg = get_cfg() cfg.merge_from_file("configs/Market1501/bagtricks_R50.yml") cfg.MODEL.BACKBONE.PRETRAIN = False model = build_model(cfg) Checkpointer(model).load("model_final.pth") model.eval() data_loader, num_query = DefaultTrainer.build_test_loader(cfg, "Market1501") evaluator = ReidEvaluator(cfg, num_query) results = inference_on_dataset( model, data_loader, evaluator, flip_test=cfg.TEST.FLIP.ENABLED ) print("Evaluation Results:") print(f" Rank-1: {results['Rank-1']:.2f}%") print(f" Rank-5: {results['Rank-5']:.2f}%") print(f" Rank-10: {results['Rank-10']:.2f}%") print(f" mAP: {results['mAP']:.2f}%") print(f" mINP: {results['mINP']:.2f}%") ``` -------------------------------- ### Train SSKD Models Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/DG-ReID/README.md Commands to train the student and teacher models using the FastReID framework. These scripts require a configuration file and specify the number of GPUs to utilize. ```bash python3 projects/Basic_Project/train_net.py --config-file projects/Basic_Project/configs/r34-ibn.yml --num-gpu 4 python3 projects/Basic_Project/train_net.py --config-file projects/Basic_Project/configs/r101-ibn.yml --num-gpu 4 python3 projects/SSKD/train_net.py --config-file projects/SSKD/configs/sskd.yml --num-gpu 4 ``` -------------------------------- ### Train FastReID Model with Configuration Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/PartialReID/README.md This script trains a FastReID model using a specified configuration file. It supports distributed training across multiple GPUs and is essential for reproducing the results published in the papers. ```bash python3 projects/PartialReID/train_net.py --config-file ``` ```bash CUDA_VISIBLE_DEVICES='0,1,2,3' python3 projects/PartialReID/train_net.py --config-file 'projects/PartialReID/configs/partial_market.yml' ``` -------------------------------- ### Build fastrt executable with TensorRT Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/README.md This snippet shows the CMake commands to build the 'fastrt' executable. It includes options to enable TensorRT engine building, demo functionality, and CNumPy integration. The build process involves creating a build directory, configuring with CMake, and then compiling with make. ```bash mkdir build cd build cmake -DBUILD_FASTRT_ENGINE=ON \ -DBUILD_DEMO=ON \ -DUSE_CNUMPY=ON .. make ``` -------------------------------- ### Configure Dataset Environment Source: https://context7.com/jdai-cv/fast-reid/llms.txt Set the environment variable for dataset locations and ensure the directory structure matches the expected format for standard datasets like Market1501 and MSMT17. ```bash export FASTREID_DATASETS=/path/to/datasets ``` -------------------------------- ### Data Loading with build_reid_train_loader and build_reid_test_loader Source: https://context7.com/jdai-cv/fast-reid/llms.txt Configurable data loaders for training and testing ReID models, supporting various sampling strategies and augmentations. These functions abstract the complexities of dataset handling and batch creation. ```python from fastreid.config import get_cfg from fastreid.data import build_reid_train_loader, build_reid_test_loader cfg = get_cfg() cfg.merge_from_file("configs/Market1501/bagtricks_R50.yml") # Build training data loader # Supports samplers: TrainingSampler, NaiveIdentitySampler, BalancedIdentitySampler train_loader = build_reid_train_loader(cfg) # Iterate through training batches for batch in train_loader: images = batch["images"] # (B, C, H, W) tensor targets = batch["targets"] # (B,) person IDs camids = batch["camids"] # (B,) camera IDs print(f"Batch shape: {images.shape}, targets: {targets.shape}") break # Build test data loader # Returns loader and number of query images test_loader, num_query = build_reid_test_loader(cfg, dataset_name="Market1501") print(f"Test set size: {len(test_loader.dataset)}") print(f"Number of query images: {num_query}") # Test images are split: first num_query are queries, rest are gallery for batch in test_loader: images = batch["images"] targets = batch["targets"] camids = batch["camids"] print(f"Test batch shape: {images.shape}") break ``` -------------------------------- ### Build fastrt with INT8 quantization Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/README.md This configuration enables INT8 quantization for further speedup. It requires a calibration dataset, the path to which is provided via the -DINT8_CALIBRATE_DATASET_PATH cmake argument. Ensure the path ends with a '/'. ```bash mkdir build cd build cmake -DBUILD_FASTRT_ENGINE=ON \ -DBUILD_DEMO=ON \ -DBUILD_INT8=ON \ -DINT8_CALIBRATE_DATASET_PATH="/data/Market-1501-v15.09.15/bounding_box_test/" .. make ``` -------------------------------- ### Evaluate trained models Source: https://github.com/jdai-cv/fast-reid/blob/master/GETTING_STARTED.md Runs the evaluation process on a pre-trained model checkpoint. Requires specifying the config file and the path to the model weights. ```bash python3 tools/train_net.py --config-file ./configs/Market1501/bagtricks_R50.yml --eval-only MODEL.WEIGHTS /path/to/checkpoint_file MODEL.DEVICE "cuda:0" ``` -------------------------------- ### Train Teacher Model in FastReID Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastDistill/README.md Trains the teacher model using the specified configuration file and number of GPUs. This is the initial step before applying distillation methods. ```shell python3 projects/FastDistill/train_net.py \ --config-file projects/FastDistill/configs/sbs_r101ibn.yml \ --num-gpus 4 ``` -------------------------------- ### Caffe Prototxt API Source: https://github.com/jdai-cv/fast-reid/blob/master/tools/deploy/Caffe/ReadMe.md Methods for initializing and managing Caffe prototxt network definitions. ```APIDOC ## Prototxt Initialization ### Description Loads a Caffe prototxt file for programmatic access. ### Method Constructor ### Endpoint `caffe_net.Prototxt(file_name)` ### Parameters - **file_name** (string) - Required - Path to the .prototxt file ### Methods - **init_caffemodel(caffe_cmd_path='caffe')**: Generates a caffemodel file from the prototxt. Specify `caffe_cmd_path` if the caffe binary is not in your system PATH. ``` -------------------------------- ### Set Performance and Output Configuration Source: https://github.com/jdai-cv/fast-reid/blob/master/docs/modules/config.md Configures the output directory for logs and toggles the cuDNN benchmarking feature. Enabling cuDNN benchmarking can optimize performance for fixed-size input images but may introduce overhead for variable-sized inputs. ```python _C.OUTPUT_DIR = "logs/" _C.CUDNN_BENCHMARK = False ``` -------------------------------- ### Train FastRetri Model via CLI Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRetri/README.md Executes the training process for a FastRetri model using a specified configuration file and a defined number of GPUs. This command invokes the train_net.py script with the path to the YAML config file. ```bash python3 projects/FastRetri/train_net.py --config-file projects/FastRetri/config/cub.yml --num-gpus 4 ``` -------------------------------- ### Perform Loss Distillation in FastReID Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastDistill/README.md Applies loss distillation using a specified configuration file, number of GPUs, and the Distiller meta-architecture. It requires paths to the teacher model's configuration and weights. ```shell python3 projects/FastDistill/train_net.py \ --config-file projects/FastDistill/configs/kd-sbs_r101ibn-sbs_r34.yaml \ --num-gpus 4 \ MODEL.META_ARCHITECTURE Distiller \ KD.MODEL_CONFIG '("projects/FastDistill/logs/dukemtmc/r101_ibn/config.yaml",)' \ KD.MODEL_WEIGHTS '("projects/FastDistill/logs/dukemtmc/r101_ibn/model_best.pth",)' ``` -------------------------------- ### Train FastClas Model with Multi-GPU Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastClas/README.md Executes the training script for the FastClas project. It requires a configuration file path and the number of GPUs to be utilized for distributed training. ```bash python3 projects/FastClas/train_net.py --config-file projects/FastClas/config/base-clas.yml --num-gpus 4 ``` -------------------------------- ### Configure Re-ranking and Batch Normalization Settings Source: https://github.com/jdai-cv/fast-reid/blob/master/docs/modules/config.md Defines the configuration parameters for test-time re-ranking and precise batch normalization. These settings allow users to toggle features and adjust hyperparameters like K1, K2, and iteration counts for BN calibration. ```python _C.TEST.RERANK = CN({"ENABLED": False}) _C.TEST.RERANK.K1 = 20 _C.TEST.RERANK.K2 = 6 _C.TEST.RERANK.LAMBDA = 0.3 _C.TEST.PRECISE_BN = CN({"ENABLED": False}) _C.TEST.PRECISE_BN.DATASET = 'Market1501' _C.TEST.PRECISE_BN.NUM_ITER = 300 ``` -------------------------------- ### Configure FastReID Dataset Path Source: https://github.com/jdai-cv/fast-reid/blob/master/datasets/README.md Sets the environment variable to define the root directory for all FastReID datasets. If not set, the framework defaults to a local 'datasets/' directory. ```bash export FASTREID_DATASETS=/path/to/datasets/ ``` -------------------------------- ### Build fastrt with FP16 optimization Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/README.md This configuration enables FP16 precision for faster inference. The CMake command includes the -DBUILD_FP16=ON flag. After building, the user can proceed to step 5 for running the optimized model. ```bash mkdir build cd build cmake -DBUILD_FASTRT_ENGINE=ON \ -DBUILD_DEMO=ON \ -DBUILD_FP16=ON .. make ``` -------------------------------- ### CMake Library Build and Linking Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/fastrt/CMakeLists.txt This section builds the main project library, linking against CUDA runtime, TensorRT, and OpenCV. It also sets target include directories and defines library versioning properties. ```cmake # build engine as library add_library(${PROJECT_NAME} ${TARGET} ${COMMON_SRC_FILES}) target_include_directories(${PROJECT_NAME} PUBLIC ../include ) find_package(OpenCV) target_include_directories(${PROJECT_NAME} PUBLIC ${OpenCV_INCLUDE_DIRS} ) target_link_libraries(${PROJECT_NAME} nvinfer cudart ${OpenCV_LIBS} ) SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SOVERSION ${LIBARARY_SOVERSION} VERSION ${LIBARARY_VERSION} ) ``` -------------------------------- ### Convert Fast-ReID to Caffe Model Source: https://github.com/jdai-cv/fast-reid/blob/master/tools/deploy/README.md This script converts a Fast-ReID model to the Caffe format. It requires the model configuration and weights as input and outputs the Caffe model files. Customization might be needed for complex architectures. The converted model can then be used for Caffe inference. ```bash python tools/deploy/caffe_export.py --config-file configs/market1501/bagtricks_R50/config.yml --name baseline_R50 --output caffe_R50_model --opts MODEL.WEIGHTS logs/market1501/bagtricks_R50/model_final.pth ``` ```protobuf layer { name: "avgpool1" type: "Pooling" bottom: "relu_blob49" top: "avgpool_blob1" pooling_param { pool: AVE global_pooling: true } } ``` ```protobuf layer { name: "bn_scale54" type: "Scale" bottom: "batch_norm_blob54" top: "output" # bn_norm_blob54 scale_param { bias_term: true } } ``` ```bash python caffe_inference.py --model-def outputs/caffe_model/baseline_R50.prototxt --model-weights outputs/caffe_model/baseline_R50.caffemodel --input test_data/*.jpg --output caffe_output ``` ```python import numpy as np np.testing.assert_allclose(torch_out, ort_out, rtol=1e-3, atol=1e-6) ``` -------------------------------- ### Convert Fast-ReID to ONNX Model Source: https://github.com/jdai-cv/fast-reid/blob/master/tools/deploy/README.md This script converts a Fast-ReID model to the ONNX format. It takes the model configuration and weights as input and generates an ONNX file. ONNX supports most PyTorch operators, but custom implementations may be needed for unsupported ones. The ONNX model can then be used with ONNX Runtime for inference. ```bash python onnx_export.py --config-file root-path/bagtricks_R50/config.yml --name baseline_R50 --output outputs/onnx_model --opts MODEL.WEIGHTS root-path/logs/market1501/bagtricks_R50/model_final.pth ``` ```bash python onnx_inference.py --model-path outputs/onnx_model/baseline_R50.onnx --input test_data/*.jpg --output onnx_output ``` ```python import numpy as np np.testing.assert_allclose(torch_out, ort_out, rtol=1e-3, atol=1e-6) ``` -------------------------------- ### Train Person Re-ID Model (Bash) Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/HAA/Readme.md Command to initiate the training process for a person re-identification model. Requires specifying the GPU devices and a configuration file. ```bash CUDA_VISIBLE_DEVICES=gpus python train_net.py --config-file ``` -------------------------------- ### Market1501 Dataset Structure Source: https://github.com/jdai-cv/fast-reid/blob/master/datasets/README.md The required directory layout for the Market1501 dataset after extraction. It expects subdirectories for training and testing bounding boxes. ```bash datasets/ Market-1501-v15.09.15/ bounding_box_test/ bounding_box_train/ ``` -------------------------------- ### Define Model Configuration via YAML Source: https://context7.com/jdai-cv/fast-reid/llms.txt Shows a hierarchical YAML configuration file for FastReID, demonstrating inheritance, model architecture settings, loss functions, and data augmentation parameters. ```yaml _BASE_: ../Base-bagtricks.yml MODEL: META_ARCHITECTURE: Baseline BACKBONE: NAME: build_resnet_backbone DEPTH: 50x LAST_STRIDE: 1 WITH_IBN: True PRETRAIN: True HEADS: NAME: EmbeddingHead EMBEDDING_DIM: 0 WITH_BNNECK: True POOL_LAYER: GlobalAvgPool CLS_LAYER: Linear LOSSES: NAME: ("CrossEntropyLoss", "TripletLoss", "CircleLoss") CE: EPSILON: 0.1 SCALE: 1.0 TRI: MARGIN: 0.3 HARD_MINING: True SCALE: 1.0 CIRCLE: MARGIN: 0.25 GAMMA: 128 SCALE: 1.0 DATASETS: NAMES: ("Market1501",) TESTS: ("Market1501", "DukeMTMC") INPUT: SIZE_TRAIN: [256, 128] SIZE_TEST: [256, 128] FLIP: ENABLED: True PROB: 0.5 REA: ENABLED: True PROB: 0.5 AUTOAUG: ENABLED: True PROB: 0.5 DATALOADER: SAMPLER_TRAIN: NaiveIdentitySampler NUM_INSTANCE: 4 NUM_WORKERS: 8 SOLVER: AMP: ENABLED: True OPT: Adam MAX_EPOCH: 120 BASE_LR: 0.00035 WEIGHT_DECAY: 0.0005 IMS_PER_BATCH: 64 SCHED: CosineAnnealingLR ETA_MIN_LR: 1e-7 WARMUP_ITERS: 2000 CHECKPOINT_PERIOD: 20 TEST: EVAL_PERIOD: 20 IMS_PER_BATCH: 128 FLIP: ENABLED: True RERANK: ENABLED: False K1: 20 K2: 6 LAMBDA: 0.3 OUTPUT_DIR: logs/custom_model ``` -------------------------------- ### Compiling C++ code with cnpy Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/third_party/cnpy/README.md This command shows how to compile a C++ source file that uses the cnpy library. It links against the cnpy library and zlib, and specifies C++11 standard. ```bash g++ -o mycode mycode.cpp -L/path/to/install/dir -lcnpy -lz --std=c++11 ``` -------------------------------- ### fastreid.checkpoint API Source: https://github.com/jdai-cv/fast-reid/blob/master/docs/index.md Documentation for the checkpoint module, which handles the saving and loading of model weights and training states. ```APIDOC ## Checkpoint Module ### Description The fastreid.checkpoint module provides utilities for managing model checkpoints, including saving training progress and loading pre-trained weights. ### Method N/A (Python Module) ### Endpoint fastreid.checkpoint ### Parameters #### Methods - **Checkpointer** (class) - Main class for saving/loading models. - **load_checkpoint** (function) - Utility to load weights from a file path. ### Request Example from fastreid.checkpoint import Checkpointer checkpointer = Checkpointer(model, save_dir='output/') ### Response #### Success Response - **model** (object) - The model with loaded weights. ``` -------------------------------- ### CMake Build Configuration for Fast-ReID Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/CMakeLists.txt This snippet configures the build process for the Fast-ReID project using CMake. It sets library names, versions, C++ standards, and build types. It also includes options for building shared or static libraries, enabling CUDA, and controlling the inclusion of specific features like FP16, INT8, CNPY, and Python interfaces. ```cmake cmake_minimum_required(VERSION 2.6) set(LIBARARY_NAME "FastRT" CACHE STRING "The Fastreid-tensorrt library name") set(LIBARARY_VERSION_MAJOR "0") set(LIBARARY_VERSION_MINOR "0") set(LIBARARY_VERSION_SINOR "5") set(LIBARARY_SOVERSION "0") set(LIBARARY_VERSION "${LIBARARY_VERSION_MAJOR}.${LIBARARY_VERSION_MINOR}.${LIBARARY_VERSION_SINOR}") project(${LIBARARY_NAME}${LIBARARY_VERSION}) add_definitions(-std=c++11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -Wall -Ofast -Wfatal-errors -D_MWAITXINTRIN_H_INCLUDED") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/") set(CMAKE_BUILD_TYPE Release) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_C_LINK_EXECUTABLE ${CMAKE_CXX_LINK_EXECUTABLE}) # option for shared or static set(TARGET "SHARED" CACHE STRING "SHARED or STATIC" FORCE) if("${TARGET}" STREQUAL "SHARED") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") message("Build Engine as shared library") else() message("Build Engine as static library") endif() option(CUDA_USE_STATIC_CUDA_RUNTIME "Use Static CUDA" OFF) option(BUILD_FASTRT_ENGINE "Build FastRT Engine" ON) option(BUILD_DEMO "Build DEMO" ON) option(BUILD_FP16 "Build Engine as FP16" OFF) option(BUILD_INT8 "Build Engine as INT8" OFF) option(USE_CNUMPY "Include CNPY libs" OFF) option(BUILD_PYTHON_INTERFACE "Build Python Interface" OFF) set(SOLUTION_DIR ${CMAKE_CURRENT_SOURCE_DIR}) message("CMAKE_CURRENT_SOURCE_DIR: " ${SOLUTION_DIR}) if(USE_CNUMPY) add_definitions(-DUSE_CNUMPY) endif() if(BUILD_INT8) add_definitions(-DBUILD_INT8) message("Build Engine as INT8") set(INT8_CALIBRATE_DATASET_PATH "/data/Market-1501-v15.09.15/bounding_box_test/" CACHE STRING "Path to calibrate dataset(end with /)") message("INT8_CALIBRATE_DATASET_PATH: " ${INT8_CALIBRATE_DATASET_PATH}) configure_file(${SOLUTION_DIR}/include/fastrt/config.h.in ${SOLUTION_DIR}/include/fastrt/config.h @ONLY) elseif(BUILD_FP16) add_definitions(-DBUILD_FP16) message("Build Engine as FP16") else() message("Build Engine as FP32") endif() if(BUILD_FASTRT_ENGINE) add_subdirectory(fastrt) message(STATUS "BUILD_FASTREID_ENGINE: ON") else() message(STATUS "BUILD_FASTREID_ENGINE: OFF") endif() if(BUILD_DEMO) add_subdirectory(demo) message(STATUS "BUILD_DEMO: ON") else() message(STATUS "BUILD_DEMO: OFF") endif() if(BUILD_PYTHON_INTERFACE) add_subdirectory(pybind_interface) message(STATUS "BUILD_PYTHON_INTERFACE: ON") else() message(STATUS "BUILD_PYTHON_INTERFACE: OFF") endif() ``` -------------------------------- ### Export Fast-ReID to ONNX and TensorRT Source: https://github.com/jdai-cv/fast-reid/blob/master/tools/deploy/README.md This snippet demonstrates the command-line process for converting a PyTorch Fast-ReID model first to ONNX format and then to TensorRT. It requires specifying model name, output directory, precision mode, batch size, and input dimensions. The ONNX model is a prerequisite for TensorRT conversion. ```bash python trt_export.py --name baseline_R50 --output outputs/trt_model \ --mode fp32 --batch-size 8 --height 256 --width 128 \ --onnx-model outputs/onnx_model/baseline.onnx ``` -------------------------------- ### DukeMTMC-reID Dataset Structure Source: https://github.com/jdai-cv/fast-reid/blob/master/datasets/README.md The required directory layout for the DukeMTMC-reID dataset. It expects the root folder to contain training and testing bounding box directories. ```bash datasets/ DukeMTMC-reID/ bounding_box_train/ bounding_box_test/ ``` -------------------------------- ### Export Model to ONNX Source: https://context7.com/jdai-cv/fast-reid/llms.txt Provides command-line instructions for exporting a trained FastReID model to ONNX format and running inference using the exported model. ```bash python tools/deploy/onnx_export.py \ --config-file configs/Market1501/bagtricks_R50.yml \ --name baseline_R50 \ --output outputs/onnx_model \ --opts MODEL.WEIGHTS logs/market1501/model_final.pth python tools/deploy/onnx_inference.py \ --model-path outputs/onnx_model/baseline_R50.onnx \ --input test_images/*.jpg \ --output onnx_features ``` -------------------------------- ### Convert FastReID to TensorRT .wts format using gen_wts.py Source: https://github.com/jdai-cv/fast-reid/blob/master/projects/FastRT/tools/How_to_Generate.md This script converts a PyTorch FastReID model to a .wts file, which is then used directly by FastRT. It requires the path to the PyTorch configuration file, the model weights, and specifies the output path for the .wts file. The conversion is performed on CUDA. ```bash python projects/FastRT/tools/gen_wts.py --config-file='config/you/use/in/fastreid/xxx.yml' --verify --show_model --wts_path='outputs/trt_model_file/xxx.wts' MODEL.WEIGHTS '/path/to/checkpoint_file/model_best.pth' MODEL.DEVICE "cuda:0" ``` ```bash python projects/FastRT/tools/gen_wts.py --config-file='configs/DukeMTMC/sbs_R50-ibn.yml' --verify --show_model --wts_path='outputs/trt_model_file/sbs_R50-ibn.wts' MODEL.WEIGHTS '/path/to/checkpoint_file/model_best.pth' MODEL.DEVICE "cuda:0" ``` ```bash python projects/FastRT/tools/gen_wts.py --config-file='configs/DukeMTMC/sbs_R50.yml' --verify --show_model --wts_path='outputs/trt_model_file/sbs_R50.wts' MODEL.WEIGHTS '/path/to/checkpoint_file/model_best.pth' MODEL.DEVICE "cuda:0" ``` ```bash python projects/FastRT/tools/gen_wts.py --config-file='projects/FastDistill/configs/sbs_r34.yml' --verify --show_model --wts_path='outputs/to/trt_model_file/sbs_r34_distill.wts' MODEL.WEIGHTS '/path/to/checkpoint_file/model_best.pth' MODEL.DEVICE "cuda:0" ``` ```bash python projects/FastRT/tools/gen_wts.py --config-file='projects/FastDistill/configs/kd-sbs_r101ibn-sbs_r34.yml' --verify --show_model --wts_path='outputs/to/trt_model_file/kd_r34_distill.wts' MODEL.WEIGHTS '/path/to/checkpoint_file/model_best.pth' MODEL.DEVICE "cuda:0" ``` -------------------------------- ### Visualize Ranking Results Source: https://context7.com/jdai-cv/fast-reid/llms.txt Visualize re-identification ranking results using either the command-line interface or the Python API. These tools help evaluate model performance by displaying top-k matches. ```bash python demo/visualize_result.py --config-file configs/Market1501/bagtricks_R50.yml --dataset-name Market1501 --output ./vis_rank_list --vis-label --num-vis 100 --rank-sort ascending --max-rank 10 --opts MODEL.WEIGHTS logs/market1501/model_final.pth ``` ```python from fastreid.utils.visualizer import Visualizer from fastreid.evaluation import evaluate_rank import torch distmat = 1 - torch.mm(q_feat, g_feat.t()).numpy() cmc, all_ap, all_inp = evaluate_rank(distmat, q_pids, g_pids, q_camids, g_camids) visualizer = Visualizer(test_dataset) visualizer.get_model_output(all_ap, distmat, q_pids, g_pids, q_camids, g_camids) visualizer.vis_rank_list(output_dir="output_dir", vis_label=True, num_vis=100, rank_sort="ascending", label_sort="ascending", max_rank=10) ```