main.bib
Study on Aspect Ratio Variability toward Robustness of Vision Transformer-based Vehicle Re-identification
Abstract
Vision Transformers (ViTs) have excelled in vehicle re-identification (ReID) tasks. However, non-square aspect ratios of image or video input might significantly affect the re-identification performance. To address this issue, we propose a novel ViT-based ReID framework in this paper, which fuses models trained on a variety of aspect ratios. Our main contributions are threefold: (i) We analyze aspect ratio performance on VeRi-776 and VehicleID datasets, guiding input settings based on aspect ratios of original images. (ii) We introduce patch-wise mixup intra-image during ViT patchification (guided by spatial attention scores) and implement uneven stride for better object aspect ratio matching. (iii) We propose a dynamic feature fusing ReID network, enhancing model robustness. Our ReID method achieves a significantly improved mean Average Precision (mAP) of 91.0% compared to the the closest state-of-the-art (CAL) result of 80.9% on VehicleID dataset.
I INTRODUCTION
![Refer to caption](https://cdn.statically.io/img/arxiv.org/extracted/5723346/images/veri_veh_ar_dist.png)
A fundamental task in intelligent transportation systems is vehicle re-identification (Re-ID): identifying vehicles across multiple non-overlapping cameras [wang2019survey]. Despite its significance, vehicle Re-ID encounters challenges due to variations in vehicle appearance across different viewpoints, poses, illumination, and backgrounds. Deep learning models face the challenge of extracting discriminative features resistant to viewpoint variations [zheng2020vehiclenet, chen2023global]. Both global and local features are essential for generating robust representations for vehicle pairs [peng2019learning, zhang2020part, gu2021efficient].
Numerous benchmark datasets contain images from real-world surveillance scenes, including VeRi-776 [zheng2020vehiclenet], PKU-VD [yan2017exploiting], VehicleID [liu2016deep], Vehicle-1M [guo2018learning], and VERI-Wild [lou2019veri], crucial for vehicle Re-ID AI development [zakria2021trends]. State-of-the-art models leverage the self-attention mechanism. Vision transformers (ViTs) have demonstrated superior ability in capturing discriminative details compared to previous CNN-based methods [he2021transreid]. Additionally, images in different datasets exhibit various aspect ratios, as illustrated in Fig. 1. VeRi-776 and VehicleID display different size and shape distributions, posing a significant challenge in model training.
However, unlike CNN-based models, which can handle varying aspect ratios to some extent due to their translation invariance and local receptive fields, vision transformers consider the entire image as a sequence of patches. The fixed patch size and sequential nature of transformers necessitate careful consideration of how input images are resized and cropped [ke2021musiq, liu2022aspect, mao2022towards, dehghani2023patch]. Early implementations of vision transformers adopted resizing strategies from CNNs, often distorting the original aspect ratio and potentially compromising performance, particularly in tasks reliant on object shape and scale.
To address this, subsequent studies explored padding strategies, adaptive post-patch extraction, trainable resizing networks, aspect-ratio aware attention mechanisms, and multi-scale/multi-aspect training [xia2022vision, lv2022scvit, hwang2022vision, zhu2022aret, ke2021musiq, li2022multi]. However, these approaches entail computational burdens, data requirements, and optimization challenges.
In summary, gaps persist in applying Vision Transformers (ViTs) to vehicle Re-ID. Optimization of scaling and resizing strategies, understanding aspect ratio effects, and exploring data augmentations such as mixup [zhang2018mixup] at the patch level for ViTs in multi-aspect ratio scenarios are needed. Intra-image mixup can potentially enhance the model’s ability to learn detailed features, particularly when the whole image is distorted by resizing with unsatisfactory aspect ratios. TransReID [he2021transreid] introduces a jigsaw patch module (JPM) to enhance feature robustness and discrimination ability, representing a step towards addressing these challenges. However, this intra-mixup occurs at the feature level; its potential at the pixel level needs to be comprehensively explored.
![Refer to caption](https://cdn.statically.io/img/arxiv.org/extracted/5723346/images/contribution.png)
![Refer to caption](https://cdn.statically.io/img/arxiv.org/extracted/5723346/images/training_procedure.png)
In this work, we are the first to propose that aspect ratio is a key factor affecting vehicle Re-ID performance and the robustness of feature learning in Vision Transformers (ViTs). We conduct a series of novel, scientific, and comprehensive experiments to explore the effects of various aspect ratios on ViT-based ReID.
To enhance the model’s generality to various aspect ratios in the input, we dynamically fuse features extracted from several models trained on images with different aspect ratios, as shown in Fig. 2. Additionally, when training a single model, we propose a novel intra-image patch mixup (PM) data augmentation method to improve the model’s learning ability on details and mitigate overfitting during training. Furthermore, to mitigate the distortion caused by unsatisfactory resizing, we employ an uneven stride strategy in the patchify step.
The key contributions of this work are:
-
•
Proposed using aspect ratio as a critical factor impacting vehicle Re-ID performance and ViT’s feature learning robustness.
-
•
Conducted novel and comprehensive experiments to explore the effects of various aspect ratios on ViT-based ReID.
-
•
Introducted dynamic feature fusion from models trained on different aspect ratios to enhance model generality.
-
•
Proposed an intra-image patch mixup (PM) data augmentation method to improve model learning ability and prevent overfitting.
-
•
Implementated an uneven stride strategy to reduce distortion in unsatisfactory resizing.
II Related Works.
Vision Transformer. The Vision Transformer (ViT) is an adaptation of the transformer architecture from natural language processing (NLP) tasks [vaswani2017attention] to computer vision. Dosovitskiy et al. introduced the ViT [dosovitskiy2010image], the first to demonstrate that a pure transformer applied directly to sequences of image patches can excel in large-scale image classification tasks. Within ViTs, the multi-head self-attention mechanism enables the model to capture diverse dependencies in images, including shapes, textures, and contextual relationships between objects in parallel, facilitating efficient learning of richer data representations. ViTs and their variants, such as DeiT [touvron2021training], Swin Transformer [liu2021swin], PVT [wang2021pyramid], and CPVT [chu2021conditional], prove beneficial across tasks including image classification, object detection, and Re-ID [khan2022transformers].
Vehicle Re-Identification. As discussed, vehicle Re-ID constitutes the primary focus of this study. Numerous works leveraging deep learning have achieved notable performance across various public vehicle Re-ID benchmarks. These works often utilize either CNN backbones [he2016deep, bashir2019vr, roman2021improving] or ViT backbones [he2021transreid, lian2022transformer, luo2021empirical, wei2022transformer]. Common loss functions employed in deep vehicle Re-ID network training include cross-entropy loss (ID loss) [zheng2017discriminatively], triple loss [liu2017end], and contrastive loss [hadsell2006dimensionality].
![Refer to caption](https://cdn.statically.io/img/arxiv.org/extracted/5723346/images/intra_pm_2.png)
III Method
Model Structure. We train one model for each major aspect ratio. The determination of how many models are needed for training with various aspect ratios is learned from the data, as illustrated in Fig. 3. For each model with a fixed aspect ratio, the resized input images undergo augmentation using a Patch Mixup (PM) module. Subsequently, a chosen Vision Transformer (ViT) backbone and its pre-trained weights are used for initialization. Features extracted from the last transformer layer are utilized for the ReID task.
Image Input with Adaptive Size and Shape. We employ a statistical method to estimate the dataset’s size and aspect ratios. Starting with the mean or median value of the dataset’s size is a suitable approach. For aspect ratios, clustering methods such as K-means can be used to generate several clusters representing the aspect ratios present in the original dataset. Subsequently, a combination of size and aspect ratio guides the resizing of images for the training of each specific model. Each model is trained on a fixed size and aspect ratio.
Patchification with uneven stride. To enhance the model’s ability to learn spatial relationships from image data, we incorporate uneven strides in different dimensions, based on the aspect ratio. Specifically, in our approach, the stride sizes and can vary, with the stride size being smaller in the shorter dimension compared to the longer dimension. We maintain a fixed patch size of 16. The total patch number is calculated as .
Patch Mixup intra image module. We propose a novel intra-image data augmentation method where each patch of an image has a probability to mix adaptively with another randomly chosen patch from the same image. Mixup weights are determined based on the spatial distance between these two patches in pixels, with closer patches assigned higher mixup weights. Let denote the original patches with patches, and their positions are denoted by the coordinates of their top-left corners for the -th patch. After patch partitioning, all patch indexes are fixed, with each patch having a unique index ranging from to . The Euclidean distance between the centers of two patches indexed by and can be computed as:
(1) |
The patch distance matrix for all patches can then be represented as:
(2) |
Given a distance matrix where each element represents the distance between patch and patch , the attention scores matrix can be computed as:
(3) |
In this formula, the attention score increases as the distance decreases, indicating that closer patches have more influence on each other. After shuffling patches, we generate a new patch-to-patch mixup weights matrix based on the patches’ indexes, as shown in Equation 4, where each value of is fetched from using the indexes of the patches:
(4) |
Here, is the patch index of a patch from the original patches , and is the patch index of the selected patch from the shuffled patches for mixup processing. Assuming the original patches are generated using non-overlapping patchification, and its patch distance matrix is calculated based on Equation 1 and 2, with as the patch is a fixed-size square. The original spatial attention scores and mixup weights matrix based on the shuffled patches are calculated using Equations 3 and 4. The final patches of this image are given by: . Several image samples before and after Patch Mixing (PM) data augmentation are shown in Fig. 4, where it can be observed that after PM, detailed information from different parts of the same vehicle is mixed. The ablation study result is shown in Table I. The pseudocode of this method is shown in Algorithm 1.
ReID Task. We optimize the network by constructing ID loss and triplet loss for global features. The ID loss is the cross-entropy loss without label smoothing. For a triplet set , the triplet loss with a soft-margin. The triplet loss with soft-margin is defined as:
where:
-
•
, , and are the feature embeddings of the anchor, positive, and negative samples, respectively.
-
•
denotes the squared Euclidean distance.
Inference Phase. As shown in Fig. 2, we use a Dynamic Feature Fusing strategy during inference to fuse features from models trained on multiple inputs. The output feature vector can be represented as:
or as a weighted concatenation:
where , , and are weights. For the adaptive weight assignment :
We investigated the ReID performance by summing or concatenating features, and the results are shown in Table I.
IV Experiments
IV-A Experiment Settings
Dataset. Two popular vehicle Re-ID benmark datasets: VeRi-776 [zheng2020vehiclenet] and VehicleID [liu2016deep], are used in this work.
Evaluation Metrics. In line with the Re-ID field, we employ mAP (mean Average Precision) and CMC (Cumulative Matching Characteristic) as our performance estimation metrics. Specifically, we consider Rank-1 (R1), Rank-5 (R5), and Rank-10 (R10) accuracy.
Dataset |
|
Stride Size | Fusing Strategy | mAP(%) | R1(%) | R5(%) | R10(%) | ||||
[224,224]/1.0 (w/o) | [16,16] | - | 74.9 | 95.0 | 97.6 | 98.6 | |||||
[224,224]/1.0 (w/) | [16,16] | - | 77.8 | 96.1 | 98.8 | 99.1 | |||||
[224,212]/0.95 (w/o) | [16,12] | - | 75.5 | 95.2 | 97.9 | 98.5 | |||||
[224,212]/0.95 (w/) | [16,12] | - | 73.2 | 93.5 | 97.4 | 98.4 | |||||
[224,298]/1.33 (w/o) | [12,16] | - | 78.5 | 96.5 | 98.0 | 99.0 | |||||
[224,298]/1.33 (w/) | [12,16] | - | 79.4 (4.5) | 96.1 | 98.2 | 99.0 | |||||
|
- | Weighted Sum | 78.1 | 96.1 | 98.1 | 98.7 | |||||
Fusing three models (w/) | - | Weighted Sum | 81.4(6.5) | 97.0(2.0) | 98.5 | 99.1 | |||||
|
- |
|
78.0 | 95.9 | 98.1 | 98.6 | |||||
VeRi776 |
|
- |
|
81.4 | 97.0 | 98.5 | 99.0 | ||||
[384,384]/1.0 (w/o) | [16,16] | - | 90.2 | 85.0 | 97.5 | 99.3 | |||||
[384,384]/1.0 (w/) | [16,16] | - | 90.8(0.6) | 85.7 | 97.8 | 99.0 | |||||
[384,308]/0.80 (w/o) | [16,12] | - | 89.9 | 84.1 | 97.6 | 99.2 | |||||
[384,308]/0.80 (w/) | [16,12] | - | 89.6 | 83.8 | 97.1 | 98.8 | |||||
[384,396]/1.03 (w/o) | [12,16] | - | 87.8 | 81.7 | 96.1 | 98.6 | |||||
[384,396]/1.03 (w/) | [12,16] | - | 90.2 | 85.2 | 97.1 | 98.8 | |||||
|
- | Weighted Sum | 90.1 | 84.8 | 97.3 | 98.9 | |||||
Fusing three models (w/) | - | Weighted Sum | 91.0(0.8) | 86.3(1.3) | 97.4 | 99.0 | |||||
|
- |
|
90.5 | 85.4 | 97.4 | 99.0 | |||||
VehicleID |
|
- |
|
90.7 | 85.3 | 97.7 | 99.1 |
Implementation Details. After analyzing the data size and aspect ratio distributions from the VeRi-776 and VehicleID training datasets, we standardized the resized image heights to 224 pixels for VeRi-776 and 384 pixels for VehicleID. We used a uniform patch size of for both training and testing phases, with stride sizes set to 16 for the longer dimension and 12 for the shorter. The aspect ratios were for VeRi-776 and for VehicleID. In the testing stage, we used the entire VeRi-776 test dataset and the largest VehicleID subset, containing 800 vehicles. Image preprocessing included 50% random horizontal flipping, padding, cropping, and erasing.
All experiments were conducted on four NVIDIA RTX A6000 GPUs using PyTorch with FP16 training. We used an SGD optimizer with a momentum of 0.1 and a weight decay of 1e-4, maintaining equal weights of 1.0 for ID and triplet losses. The batch size was set at 128, with four images per ID, across 120 training epochs. Initial learning rates were set at 0.035 for VeRi-776 and 0.045 for VehicleID, both decreasing linearly.
IV-B Results
Major Results of Our Method. Our experiments (Table I) compare baseline performance with different input shapes and the absence of patch mixup (PM) data augmentation. On VeRi-776, non-square input of without PM outperforms square input of by 4.5% in mAP. With sum-based weighted feature fusion from three models, the mAP improves by over 6.5%. Similarly, on VehicleID, non-square input of without PM achieves 0.6% higher mAP than square input of , while feature fusion raises mAP by over 0.8%.
Comparsion with the State-of-the-Art Methods. In Tables II and III, we compare our best model’s ReID performance with three state-of-the-art methods (RPTM [ghosh2023relation], CAL [rao2021counterfactual], and TransReID [he2021transreid]) on VeRi-776 and VehicleID large test datasets. Our model, using ViT-B/16 backbone, outperforms pure ViT-B/16 by 23% mAP and 0.5% R1 on VeRi-776. On VehicleID, our approach achieves 91.0% mAP and 97.4% R5, surpassing RPTM by 10.5% mAP and 1.1% R5. These results demonstrate the significant potential for improving ReID performance by solely modifying ViTs’ input without altering their architectures.
![Refer to caption](https://cdn.statically.io/img/arxiv.org/extracted/5723346/images/same_ar_Veri.png)
![Refer to caption](https://cdn.statically.io/img/arxiv.org/extracted/5723346/images/same_ar_Veh.png)
Backbone |
|
Method | mAP | R1 | R5 | ||
ResNet-101 | 240x240 | RPTM[ghosh2023relation] | 88.0 | 97.3 | 98.4 | ||
ResNet-50 | 384x192 | CAL[rao2021counterfactual] | 74.3 | 95.4 | 97.9 | ||
ViT/B-16 | 384x128 | TransReID[he2021transreid] | 82.0 | 97.1 | - | ||
ViT/B-16 | 256x128 |
|
78.2 | 96.5 | - | ||
ViT/B-16 | Fused | Ours | 81.5 | 97.0 | 98.5 |
Backbone |
|
Method |
|
R1(%) | R5(%) | ||||
---|---|---|---|---|---|---|---|---|---|
ResNet-101 | 240x240 | RPTM[ghosh2023relation] | 80.5 | 92.9 | 96.3 | ||||
ResNet-50 | 384x192 | [rao2021counterfactual] | 80.9 | 75.1 | 88.5 | ||||
ViT/B-16 | 256x128 | TransReID[he2021transreid] | - | 85.2 | 97.5 | ||||
ViT/B-16 | 256x128 |
|
- | 83.5 | 96.7 | ||||
ViT/B-16 | Fused | Ours | 91.0 | 86.3 | 97.4 |
Ablation Study: Square input with different sizes. We assess the impact of input size on ReID performance using square shapes of , , , and for both VeRI-776 and VehicleID datasets. All the trainings are conducted without using the PM module. The results are shown in Fig. 6 and 6. On both datasets, the ReID performance does not linearly increase with the input size. The highest ReID accuracy is observed when the input size is 256 pixels for VeRi-776 and 384 pixels for VehicleID.
![Refer to caption](https://cdn.statically.io/img/arxiv.org/extracted/5723346/images/average_inference_time.png)
![Refer to caption](https://cdn.statically.io/img/arxiv.org/extracted/5723346/images/grad_cam_veri.png)
![Refer to caption](https://cdn.statically.io/img/arxiv.org/extracted/5723346/images/grad_cam_veh.png)
IV-C Results Analysis
Patch Number affects Average Inference Time. Fig. 7 illustrates the correlation between patch count and inference time per object. Increased patches escalate time costs, indicating optimization opportunities. Despite longer inference times from model fusion, mitigation is possible through mask or attention strategies to trim patch count during training, thus curtailing parameter size.
How does patch mixup (PM) work? Grad-CAM visualizes attention (Fig. 8 and 9). Intra-image Patch Mixup improves object focus, especially in VeRi-776, enhancing robustness to view and aspect ratios, notably in VehicleID. Utilizing self-attention, our method enhances spatial relations, improving global feature learning in ViTs.
V Conclusion
We explore aspect ratio’s impact on ViT-based vehicle ReID. Fusion of varied aspect ratio models boosts robustness and Re-ID performance. Our intra-image Patch Mixup augmentation enhances generalization. Outperforming baselines on VeRi-776 and VehicleID, though fusion may raise inference time, network pruning can be used to mitigate this. Future work aims at efficient ReID models for diverse aspect ratios.