Back to paper library
arXiv 2022AAAI 2023Person re-identificationPaper notes

CLIP-ReID

Give It a Name

Original paper

CLIP-ReID: Exploiting Vision-Language Model for Image Re-Identification without Concrete Text Labels

Paper authors

Siyuan Li · Li Sun · Qingli Li

Publication
AAAI 2023Conference
First submitted to arXiv
arXiv:2211.13977
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{personreidclipreid2022,
  title = {{CLIP-ReID: Exploiting Vision-Language Model for Image Re-Identification without Concrete Text Labels}},
  author = {Siyuan Li and Li Sun and Qingli Li},
  year = {2022},
  eprint = {2211.13977},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2211.13977}
}
On this page

After [21.03] CLIP: Breaking the Dimensional Barrier, many vision tasks switched to CLIP's image encoder. Classification can simply slot the class name into a sentence like "A photo of a [class]." and let the text encoder produce the class features, with no training at all; [21.09] CoOp: Whispering in the Wind then replaced the leading prompt words with learnable vectors and did even better.

Person re-identification (ReID) never caught this wave, for a plain reason. ReID labels are ID numbers. In the training set, pedestrian No. 0001 and pedestrian No. 0002 have no words describing how they differ, and nobody knows what to put in the [class] slot of "A photo of a [class]."

Siyuan Li, Li Sun and Qingli Li of East China Normal University wanted to know whether CLIP's text encoder could still help when there are no text labels.

Defining the problem

The authors started with something very plain, taking CLIP's image encoder as the initial weights and fine-tuning it directly with the usual ReID losses.

Baseline (MSMT17)mAPrank-1
BoT (ResNet-50, ImageNet)51.375.3
ResNet-50, CLIP init60.782.1
TransReID (ViT, ImageNet)61.081.8
ViT-B/16, CLIP init66.184.4

MSMT17 has 4,101 people, more than 120,000 images and 15 cameras, and was the hardest ReID dataset at the time. With the same ResNet-50, just switching the initial weights from ImageNet to CLIP adds 9 points of mAP; ViT gains 5 points as well.

The authors attribute this to how the models were pretrained. ImageNet uses one-hot labels over a thousand classes, and any semantics beyond the class get thrown away; CLIP learns the correspondence between images and whole-sentence descriptions, so its image encoder sees more high-level semantics.

This baseline is already strong, and the next question is how to put the text encoder to work too.

Solving the problem

Original Figure 2. CLIP, CoOp and CLIP-ReID compared. (a) CLIP trains both encoders together on paired images and text. (b) CoOp freezes both encoders and learns only the prompt words [P]1 to [P]K. (c) In its first stage, CLIP-ReID freezes both encoders and learns each identity's own [X]1 to [X]M, in the sentence "A photo of a [X]1[X]2…[X]M person.", trained with L_i2t and L_t2i; in the second stage it freezes the text side and trains only the image encoder, with the losses L_id, L_tri and L_i2tce.
Original Figure 2, Li et al., from PDF page 2, arXiv license, scholarly quotation.

A quick look back at CLIP

CLIP's image encoder and text encoder each output a vector, which is linearly projected into a shared space, and the inner product serves as the similarity:

s(Vi,Ti)=gV(imgi)⋅gT(texti)s(V_i, T_i) = g_V(\mathrm{img}_i) \cdot g_T(\mathrm{text}_i)

During training, a batch holds BB image–text pairs; the matching pair's similarity should be high and the others low. Seen from the image side:

Li2t(i)=−log⁡exp⁡(s(Vi,Ti))∑a=1Bexp⁡(s(Vi,Ta))\mathcal{L}_{i2t}(i) = -\log \frac{\exp(s(V_i, T_i))}{\sum_{a=1}^{B} \exp(s(V_i, T_a))}

Lt2i\mathcal{L}_{t2i}, seen from the text side, has the same form, with the denominator replaced by the similarities between one piece of text and every image in the batch.

Stage one: learning a description for each person

Since there are no words to fill in, the authors let the model learn its own. Each identity gets its own set of learnable tokens, and the sentence reads:

A photo of a [X]1[X]2…[X]M person.\text{A photo of a } [X]_1 [X]_2 \dots [X]_M \text{ person.}

Each [X] is a vector with the same dimension as a word embedding, and MM defaults to 4. In this stage both the image and text encoders are frozen, and only these tokens are learned, with the loss Li2t+Lt2i\mathcal{L}_{i2t} + \mathcal{L}_{t2i}.

Unlike in CLIP, a batch may contain several images of the same person, and they all share the same description. Lt2i\mathcal{L}_{t2i} is therefore changed into a multi-positive version that averages over all images of the same identity.

Because the image encoder doesn't move, the authors compute and store the image features of the whole training set before stage one starts, and from then on simply learn the tokens on these fixed vectors. When stage one ends, the text features of the NN identities are each computed once and stored for stage two.

The authors call what gets learned "ambiguous descriptions". It isn't any readable word, only a set of vectors that the text encoder accepts and that happens to land near this person's image features.

Stage two: using the descriptions to constrain the images

Stage two runs the other way. The tokens and the text encoder are frozen, and only the image encoder is trained. The loss has three terms:

Lstage2=Lid+Ltri+Li2tce\mathcal{L}_{\text{stage2}} = \mathcal{L}_{id} + \mathcal{L}_{tri} + \mathcal{L}_{i2tce}

The first two are the standard ReID recipe, following Luo et al.'s BoT strong baseline: an ID classification loss with label smoothing, and a triplet loss with a margin of 0.3.

The third term is new. Each image's feature is scored for similarity against the text features of all NN identities in the training set, and a cross-entropy is taken:

Li2tce(i)=−∑k=1Nqklog⁡pk(i)\mathcal{L}_{i2tce}(i) = -\sum_{k=1}^{N} q_k \log p_k(i) pk(i)=exp⁡(s(Vi,Tyk))∑a=1Nexp⁡(s(Vi,Tya))p_k(i) = \frac{\exp(s(V_i, T_{y_k}))}{\sum_{a=1}^{N} \exp(s(V_i, T_{y_a}))}

pk(i)p_k(i) is the probability that image ii is assigned to identity kk, and qkq_k is the target distribution with label smoothing.

Look closely and this term is simply a classifier whose weights are the NN fixed text features, which take no part in training. Stage one finds an anchor for each person in CLIP's text space, and stage two pulls the image features toward their own anchors.

Original Figure 1. t-SNE of 10 random people from MSMT17. (a) Stage one: hollow stars are the initial text features, all crowded at the top of the plot; solid stars are the learned text features, each landing inside the cluster of same-colored image features; dots are the original image features, with the colors mixed together and the boundaries blurry. (b) Stage two: each color's image features shrink into a tight cluster right next to its own text feature, and different people are far apart.
Original Figure 1, Li et al., from PDF page 1, arXiv license, scholarly quotation.

This figure lays out the two stages clearly. In stage one, the text features set out from the same starting point and each walks into the middle of its own group of images; in stage two, the image features close in on the text features, and different people move apart.

Other details

There are two image encoders. ViT-B/16's output is linearly reduced from 768 to 512 dimensions to match the text features; ResNet-50 changes its last stride from 2 to 1 to keep a larger feature map, followed by CLIP's original attention pooling. Besides the final features, the triplet loss is also applied after ViT's 11th layer and after ResNet's 3rd residual stage.

The ViT version can also take two tricks from TransReID. SIE encodes the camera ID as an embedding and adds it in; the authors add it only to the [CLS] token, whereas TransReID adds it to every token. OLP makes the patches overlap when the image is cut up, at the cost of more computation.

At inference, only the image encoder is used. The authors also confirmed in an issue that the prompts and the text encoder matter only during training. What gets deployed is a single ResNet-50 or ViT-B/16, and the text side can stay behind.

Discussion

Person re-identification

MethodMSMT17Market-1501
ResNet-50 baseline60.7/82.188.1/94.7
ResNet-50 CLIP-ReID63.0/84.489.8/95.7
TransReID + SIE + OLP67.4/85.388.9/95.2
ViT baseline66.1/84.486.4/93.3
ViT CLIP-ReID73.4/88.789.6/95.5
ViT CLIP-ReID + SIE + OLP75.8/89.790.5/95.4

Numbers in the table are mAP/rank-1, with 256×128 inputs, all without re-ranking.

The biggest gains are on MSMT17. The ViT version goes from the baseline's 66.1 to 73.4, up 7 points, and 6 points above TransReID + SIE + OLP. The ResNet-50 version gains 2.3 points.

Market-1501 is less striking. On Market, the ViT baseline actually trails the ResNet-50 baseline, and once CLIP-ReID makes up the difference, it's within a point of TransReID. DukeMTMC-reID and its occluded version, Occluded-Duke, look similar. On Market, Duke and Occluded-Duke, the version with SIE and OLP is 1.1 to 1.6 mAP above TransReID with the same 256×128 inputs; against TransReID with enlarged inputs, it is only 1.0 and 0.5 higher on Market and Duke.

For vehicle re-identification, the ViT version gets 83.3/97.4 on VeRi-776, and 85.3/97.6 in rank-1/rank-5 on the small VehicleID test set, both slightly above TransReID; VeRi-776 mAP is 1.3 higher, and every other gap is within half a point.

Why two stages

Training (MSMT17)ResNet-50ViT
Baseline60.7/82.166.1/84.4
One-stage61.9/82.868.9/85.9
Two-stage63.0/84.473.4/88.7

One-stage means training the tokens and the image encoder at the same time. The authors' explanation is that early in training the tokens can't describe the images yet, but they already steer how the image encoder is optimized. For the ViT version, the gap is 4.5 points.

Which text loss to use in stage two

If stage two keeps CLIP's original Li2t\mathcal{L}_{i2t} and Lt2i\mathcal{L}_{t2i}, the ViT version gets 71.3; keeping only Li2tce\mathcal{L}_{i2tce} gives 73.4. The difference lies in what gets compared. Li2t\mathcal{L}_{i2t} compares only against the identities in the same batch, while Li2tce\mathcal{L}_{i2tce} compares against every identity in the training set. A batch holds only 16 people, and MSMT17's training set has more than a thousand. Lt2i\mathcal{L}_{t2i}, for its part, doesn't help in stage two.

Where it isn't very sensitive

With MM, the number of tokens per person, at 1, 4 and 8, ViT on MSMT17 gets 72.7, 73.4 and 73.4, a very small spread.

Stage one can also be made cheaper. Average each identity's image features into a single vector first and learn the tokens on that, and stage one takes only 2 minutes on a single RTX 2080 Ti instead of 37.5 minutes training image by image, at the cost of 1 point of mAP.

Original Figure 3. Attention visualizations for four input images; each group shows, in order, the original image, the TransReID baseline, the CLIP-ReID baseline and CLIP-ReID. The high attention of the two baselines concentrates on small regions such as the legs or patches of clothing; CLIP-ReID's high attention covers most of the person's body.
Original Figure 3, Li et al., from PDF page 7, arXiv license, scholarly quotation.

The attention visualizations agree with the motivation. Both baselines fixate on small patches, while CLIP-ReID takes in the person more completely.

Re-ranking

Before its tables, the paper makes a point of saying "all data listed here are without re-ranking". The README adds one more number, "With re-ranking, ViT-CLIP-ReID-SIE-OLP achieves 86.7% mAP and 91.1% R1 on MSMT17."

Compared with 75.8/89.7 without it, mAP gains nearly 11 points while rank-1 gains only 1.4. It's the same pattern [17.01] k-reciprocal: Mistaken Identity saw on Market-1501 back then, with re-ranking mainly pulling up positives that were ranked further down.

The code uses that very k-reciprocal implementation, but with different parameters. In the evaluation code, the line with the original paper's k1=20k_1 = 20, k2=6k_2 = 6 is commented out, and the actual call uses k1=50k_1 = 50, k2=15k_2 = 15. The README doesn't mention this, so anyone comparing against 86.7 has to dig through the code themselves.

Code and reproduction

The official code is MIT-licensed, has 531 stars, and comes with weights and test logs for each dataset.

The authors replied 17 times in the issues in all, the last batch on September 24, 2023, six replies in one day. The 52 issues and 1 PR opened since then have had no reply from the authors (later askers have taken to answering each other).

Several of them are about reproduction. Someone retrained on VeRi-776 with the stated settings and got a rank-1 4.8 below the paper; someone else came out 4 to 5 points low on Market-1501, and another user replied that they had reproduced 90.5/95.4 and suspected an environment problem. Someone else downloaded the official MSMT17 weights and evaluated them directly, and the numbers didn't match, though Market and Duke did; the asker finally guessed that the test resolution differed and that the authors may have used 384×128. None of these got a confirmation from the authors.

Conclusion

CLIP-ReID's approach comes down to two things. First, CLIP's image encoder is in itself a better initialization for ReID, and swapping out the ImageNet weights is worth several points. Second, with no text labels, it first learns a set of vectors for each person in the text space, then uses these fixed vectors as classifier weights to constrain the image encoder.

The text encoder here is only scaffolding for training, and it comes down entirely at inference.

More than two years later, [25.04] CLIMB-ReID: No Words Needed drops the text altogether, using the mean of CLIP image features directly as the anchors and updating them as training goes on.