├── .gitignore ├── LICENSE ├── README.md ├── backbone.py ├── batch_sampler.py ├── datasets ├── Market1501.py ├── __init__.py └── download_market1501.sh ├── embed.py ├── eval.py ├── logger.py ├── loss.py ├── optimizer.py ├── random_erasing.py ├── train.py ├── triplet_selector.py └── utils.py /.gitignore: -------------------------------------------------------------------------------- 1 | /datasets/Market-1501-v15.09.15/ 2 | /datasets/Market-1501-v15.09.15.zip 3 | /res/* 4 | *.pyc 5 | *.so 6 | *.o 7 | *.a 8 | /datasets/__pycache__/ 9 | /__pycache__/ 10 | train.log 11 | emb_eval.sh 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # triplet-ReID-pytorch 2 | This is a simple implementation of the algorithm proposed in paper [In Defense of the Triplet Loss for Person Re-Identification](https://arxiv.org/abs/1703.07737). 3 | 4 | This project is based on pytorch0.4.0 and python3. 5 | 6 | To be straight-forward and simple, only the method of training on pretrained Resnet-50 with batch-hard sampler(*TriNet* according to the authors) is implemented. 7 | 8 | 9 | ### prepare dataset 10 | Run the script of ```datasets/download_market1501.sh``` to download and uncompress the Market1501 dataset. 11 | ``` 12 | $ cd triplet-reid-pytorch/datasets 13 | $ sh download_market1501.sh 14 | ``` 15 | 16 | ### train the model 17 | * To train on the Market1501 dataset, just run the training script: 18 | ``` 19 | $ cd triplet-reid-pytorch 20 | $ python3 train.py 21 | ``` 22 | This will train an embedder model based on ResNet-50. The trained model will be stored in the path of ```/res/model.pkl```. 23 | 24 | 25 | ### embed the query and gallery dataset 26 | * To embed the gallery set and query set of Market1501, run the corresponding embedding scripts: 27 | ``` 28 | $ python3 embed.py \ 29 | --store_pth ./res/emb_gallery.pkl \ 30 | --data_pth datasets/Market-1501-v15.09.15/bounding_box_test 31 | 32 | $ python3 embed.py \ 33 | --store_pth ./res/emb_query.pkl \ 34 | --data_pth datasets/Market-1501-v15.09.15/query 35 | ``` 36 | These scripts will use the trained embedder to embed the gallery and query set of Market1501, and store the embeddings as ```/res/embd_gallery.pkl``` and ```/res/emb_query.pkl```. 37 | 38 | 39 | ### evaluate the embeddings 40 | * Then compute the rank-1 cmc and mAP: 41 | ``` 42 | $ python3 eval.py --gallery_embs ./res/emb_gallery.pkl \ 43 | --query_embs ./res/emb_query.pkl \ 44 | --cmc_rank 1 45 | ``` 46 | This will evaluate the model with the query and gallery dataset. 47 | 48 | 49 | ### Notes 50 | After refering to some other paper and implementations, I got to to know two tricks that help to boost the performance: 51 | * adjust the stride of the last stage of resnet from 2 to 1. 52 | * use augmentation method of [random erasing](https://arxiv.org/abs/1708.04896). 53 | 54 | With these two tricks, the mAP and rank-1 cmc on Market1501 dataset reaches 76.04/88.27, much higher than the result claimed in the paper. 55 | -------------------------------------------------------------------------------- /backbone.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | 5 | import torch 6 | import torch.nn as nn 7 | import torch.nn.functional as F 8 | import torchvision 9 | import torch.utils.model_zoo as model_zoo 10 | 11 | 12 | param_url = 'https://download.pytorch.org/models/resnet50-19c8e357.pth' 13 | 14 | 15 | class EmbedNetwork(nn.Module): 16 | def __init__(self, dims = 128, pretrained_base = True, *args, **kwargs): 17 | super(EmbedNetwork, self).__init__(*args, **kwargs) 18 | self.pretrained_base = pretrained_base 19 | 20 | resnet50 = torchvision.models.resnet50() 21 | self.conv1 = resnet50.conv1 22 | self.bn1 = resnet50.bn1 23 | self.relu = resnet50.relu 24 | self.maxpool = resnet50.maxpool 25 | self.layer1 = create_layer(64, 64, 3, stride=1) 26 | self.layer2 = create_layer(256, 128, 4, stride=2) 27 | self.layer3 = create_layer(512, 256, 6, stride=2) 28 | self.layer4 = create_layer(1024, 512, 3, stride=1) 29 | 30 | self.fc_head = DenseNormReLU(in_feats = 2048, out_feats = 1024) 31 | self.embed = nn.Linear(in_features = 1024, out_features = dims) 32 | 33 | if self.pretrained_base: 34 | new_state = model_zoo.load_url(param_url) 35 | state_dict = self.state_dict() 36 | for k, v in new_state.items(): 37 | if 'fc' in k: continue 38 | state_dict.update({k: v}) 39 | self.load_state_dict(state_dict) 40 | 41 | for el in self.fc_head.children(): 42 | if isinstance(el, nn.Linear): 43 | nn.init.kaiming_normal_(el.weight, a=1) 44 | nn.init.constant_(el.bias, 0) 45 | 46 | nn.init.kaiming_normal_(self.embed.weight, a=1) 47 | # nn.init.xavier_normal_(self.embed.weight, gain=1) 48 | nn.init.constant_(self.embed.bias, 0) 49 | 50 | def forward(self, x): 51 | x = self.conv1(x) 52 | x = self.bn1(x) 53 | x = self.relu(x) 54 | x = self.maxpool(x) 55 | 56 | x = self.layer1(x) 57 | x = self.layer2(x) 58 | x = self.layer3(x) 59 | x = self.layer4(x) 60 | 61 | x = F.avg_pool2d(x, x.size()[2:]) 62 | x = x.contiguous().view(-1, 2048) 63 | x = self.fc_head(x) 64 | x = self.embed(x) 65 | return x 66 | 67 | 68 | class Bottleneck(nn.Module): 69 | def __init__(self, in_chan, mid_chan, stride=1, stride_at_1x1=False, *args, **kwargs): 70 | super(Bottleneck, self).__init__(*args, **kwargs) 71 | 72 | stride1x1, stride3x3 = (stride, 1) if stride_at_1x1 else (1, stride) 73 | 74 | out_chan = 4 * mid_chan 75 | self.conv1 = nn.Conv2d(in_chan, mid_chan, kernel_size=1, stride=stride1x1, 76 | bias=False) 77 | self.bn1 = nn.BatchNorm2d(mid_chan) 78 | self.conv2 = nn.Conv2d(mid_chan, mid_chan, kernel_size=3, stride=stride3x3, 79 | padding=1, bias=False) 80 | self.bn2 = nn.BatchNorm2d(mid_chan) 81 | self.conv3 = nn.Conv2d(mid_chan, out_chan, kernel_size=1, bias=False) 82 | self.bn3 = nn.BatchNorm2d(out_chan) 83 | self.relu = nn.ReLU(inplace=True) 84 | 85 | self.downsample = None 86 | if in_chan != out_chan or stride != 1: 87 | self.downsample = nn.Sequential( 88 | nn.Conv2d(in_chan, out_chan, kernel_size=1, stride=stride, bias=False), 89 | nn.BatchNorm2d(out_chan)) 90 | 91 | def forward(self, x): 92 | out = self.conv1(x) 93 | out = self.bn1(out) 94 | out = self.relu(out) 95 | out = self.conv2(out) 96 | out = self.bn2(out) 97 | out = self.relu(out) 98 | out = self.conv3(out) 99 | out = self.bn3(out) 100 | 101 | if self.downsample == None: 102 | residual = x 103 | else: 104 | residual = self.downsample(x) 105 | 106 | out += residual 107 | out = self.relu(out) 108 | 109 | return out 110 | 111 | 112 | def create_layer(in_chan, mid_chan, b_num, stride): 113 | out_chan = mid_chan * 4 114 | blocks = [Bottleneck(in_chan, mid_chan, stride=stride),] 115 | for i in range(1, b_num): 116 | blocks.append(Bottleneck(out_chan, mid_chan, stride=1)) 117 | return nn.Sequential(*blocks) 118 | 119 | 120 | class DenseNormReLU(nn.Module): 121 | def __init__(self, in_feats, out_feats, *args, **kwargs): 122 | super(DenseNormReLU, self).__init__(*args, **kwargs) 123 | self.dense = nn.Linear(in_features = in_feats, out_features = out_feats) 124 | self.bn = nn.BatchNorm1d(out_feats) 125 | self.relu = nn.ReLU(inplace = True) 126 | 127 | def forward(self, x): 128 | x = self.dense(x) 129 | x = self.bn(x) 130 | x = self.relu(x) 131 | return x 132 | 133 | 134 | 135 | if __name__ == "__main__": 136 | embed_net = EmbedNetwork(pretrained_base = True) 137 | 138 | in_ten = torch.randn(32, 3, 256, 128) 139 | out = embed_net(in_ten) 140 | print(out.shape) 141 | -------------------------------------------------------------------------------- /batch_sampler.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | 5 | import torch 6 | from torch.utils.data import Dataset, DataLoader 7 | from torch.utils.data.sampler import Sampler 8 | import cv2 9 | import numpy as np 10 | import random 11 | import logging 12 | import sys 13 | 14 | 15 | 16 | class BatchSampler(Sampler): 17 | ''' 18 | sampler used in dataloader. method __iter__ should output the indices each time it is called 19 | ''' 20 | def __init__(self, dataset, n_class, n_num, *args, **kwargs): 21 | super(BatchSampler, self).__init__(dataset, *args, **kwargs) 22 | self.n_class = n_class 23 | self.n_num = n_num 24 | self.batch_size = n_class * n_num 25 | self.dataset = dataset 26 | self.labels = np.array(dataset.lb_ids) 27 | self.labels_uniq = np.array(list(dataset.lb_ids_uniq)) 28 | self.len = len(dataset) // self.batch_size 29 | self.lb_img_dict = dataset.lb_img_dict 30 | self.iter_num = len(self.labels_uniq) // self.n_class 31 | 32 | def __iter__(self): 33 | curr_p = 0 34 | np.random.shuffle(self.labels_uniq) 35 | for k, v in self.lb_img_dict.items(): 36 | random.shuffle(self.lb_img_dict[k]) 37 | for i in range(self.iter_num): 38 | label_batch = self.labels_uniq[curr_p: curr_p + self.n_class] 39 | curr_p += self.n_class 40 | idx = [] 41 | for lb in label_batch: 42 | if len(self.lb_img_dict[lb]) > self.n_num: 43 | idx_smp = np.random.choice(self.lb_img_dict[lb], 44 | self.n_num, replace = False) 45 | else: 46 | idx_smp = np.random.choice(self.lb_img_dict[lb], 47 | self.n_num, replace = True) 48 | idx.extend(idx_smp.tolist()) 49 | yield idx 50 | 51 | def __len__(self): 52 | return self.iter_num 53 | 54 | 55 | if __name__ == "__main__": 56 | from datasets.Market1501 import Market1501 57 | ds = Market1501('datasets/Market-1501-v15.09.15/bounding_box_train', is_train = False) 58 | sampler = BatchSampler(ds, 18, 4) 59 | dl = DataLoader(ds, batch_sampler = sampler, num_workers = 4) 60 | import itertools 61 | 62 | diter = itertools.cycle(dl) 63 | 64 | while True: 65 | ims, lbs, _ = next(diter) 66 | print(lbs.shape) 67 | print(len(list(ds.lb_ids_uniq))) 68 | -------------------------------------------------------------------------------- /datasets/Market1501.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | 5 | import os 6 | import torch 7 | import torchvision 8 | import torchvision.transforms as transforms 9 | from torch.utils.data import Dataset 10 | import cv2 11 | import numpy as np 12 | from PIL import Image 13 | 14 | from random_erasing import RandomErasing 15 | 16 | 17 | class Market1501(Dataset): 18 | ''' 19 | a wrapper of Market1501 dataset 20 | ''' 21 | def __init__(self, data_path, is_train = True, *args, **kwargs): 22 | super(Market1501, self).__init__(*args, **kwargs) 23 | self.is_train = is_train 24 | self.data_path = data_path 25 | self.imgs = os.listdir(data_path) 26 | self.imgs = [el for el in self.imgs if os.path.splitext(el)[1] == '.jpg'] 27 | self.lb_ids = [int(el.split('_')[0]) for el in self.imgs] 28 | self.lb_cams = [int(el.split('_')[1][1]) for el in self.imgs] 29 | self.imgs = [os.path.join(data_path, el) for el in self.imgs] 30 | if is_train: 31 | self.trans = transforms.Compose([ 32 | transforms.Resize((288, 144)), 33 | transforms.RandomCrop((256, 128)), 34 | transforms.RandomHorizontalFlip(), 35 | transforms.ToTensor(), 36 | transforms.Normalize((0.486, 0.459, 0.408), (0.229, 0.224, 0.225)), 37 | RandomErasing(0.5, mean=[0.0, 0.0, 0.0]) 38 | ]) 39 | else: 40 | self.trans_tuple = transforms.Compose([ 41 | transforms.ToTensor(), 42 | transforms.Normalize((0.486, 0.459, 0.408), (0.229, 0.224, 0.225)) 43 | ]) 44 | self.Lambda = transforms.Lambda( 45 | lambda crops: [self.trans_tuple(crop) for crop in crops]) 46 | self.trans = transforms.Compose([ 47 | transforms.Resize((288, 144)), 48 | transforms.TenCrop((256, 128)), 49 | self.Lambda, 50 | ]) 51 | 52 | # useful for sampler 53 | self.lb_img_dict = dict() 54 | self.lb_ids_uniq = set(self.lb_ids) 55 | lb_array = np.array(self.lb_ids) 56 | for lb in self.lb_ids_uniq: 57 | idx = np.where(lb_array == lb)[0] 58 | self.lb_img_dict.update({lb: idx}) 59 | 60 | def __len__(self): 61 | return len(self.imgs) 62 | 63 | def __getitem__(self, idx): 64 | img = Image.open(self.imgs[idx]) 65 | img = self.trans(img) 66 | return img, self.lb_ids[idx], self.lb_cams[idx] 67 | 68 | 69 | 70 | if __name__ == "__main__": 71 | ds = Market1501('./Market-1501-v15.09.15/bounding_box_train', is_train = True) 72 | im, _, _ = ds[1] 73 | print(im.shape) 74 | print(im.max()) 75 | print(im.min()) 76 | ran_er = RandomErasing() 77 | im = ran_er(im) 78 | cv2.imshow('erased', im) 79 | cv2.waitKey(0) 80 | -------------------------------------------------------------------------------- /datasets/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoinCheung/triplet-reid-pytorch/b32f5df1084c70aeceb5084001f242dce31d0ebb/datasets/__init__.py -------------------------------------------------------------------------------- /datasets/download_market1501.sh: -------------------------------------------------------------------------------- 1 | 2 | wget -c http://188.138.127.15:81/Datasets/Market-1501-v15.09.15.zip 3 | unzip Market-1501-v15.09.15.zip 4 | -------------------------------------------------------------------------------- /embed.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | import torch 5 | import torch.nn as nn 6 | import torchvision 7 | from torch.utils.data import DataLoader 8 | 9 | import pickle 10 | import numpy as np 11 | import sys 12 | import logging 13 | import argparse 14 | import cv2 15 | 16 | from backbone import EmbedNetwork 17 | from datasets.Market1501 import Market1501 18 | 19 | 20 | torch.multiprocessing.set_sharing_strategy('file_system') 21 | 22 | def parse_args(): 23 | parse = argparse.ArgumentParser() 24 | parse.add_argument( 25 | '--store_pth', 26 | dest = 'store_pth', 27 | type = str, 28 | required = True, 29 | help = 'path that the embeddings are stored: e.g.: ./res/emb.pkl', 30 | ) 31 | parse.add_argument( 32 | '--data_pth', 33 | dest = 'data_pth', 34 | type = str, 35 | required = True, 36 | help = 'path that the raw images are stored', 37 | ) 38 | 39 | return parse.parse_args() 40 | 41 | 42 | 43 | def embed(args): 44 | ## logging 45 | FORMAT = '%(levelname)s %(filename)s:%(lineno)d: %(message)s' 46 | logging.basicConfig(level=logging.INFO, format=FORMAT, stream=sys.stdout) 47 | logger = logging.getLogger(__name__) 48 | 49 | ## restore model 50 | logger.info('restoring model') 51 | model = EmbedNetwork().cuda() 52 | model.load_state_dict(torch.load('./res/model.pkl')) 53 | model = nn.DataParallel(model) 54 | model.eval() 55 | 56 | ## load gallery dataset 57 | batchsize = 32 58 | ds = Market1501(args.data_pth, is_train = False) 59 | dl = DataLoader(ds, batch_size = batchsize, drop_last = False, num_workers = 4) 60 | 61 | 62 | ## embedding samples 63 | logger.info('start embedding') 64 | all_iter_nums = len(ds) // batchsize + 1 65 | embeddings = [] 66 | label_ids = [] 67 | label_cams = [] 68 | for it, (img, lb_id, lb_cam) in enumerate(dl): 69 | print('\r=======> processing iter {} / {}'.format(it, all_iter_nums), 70 | end = '', flush = True) 71 | label_ids.append(lb_id) 72 | label_cams.append(lb_cam) 73 | embds = [] 74 | for im in img: 75 | im = im.cuda() 76 | embd = model(im).detach().cpu().numpy() 77 | embds.append(embd) 78 | embed = sum(embds) / len(embds) 79 | embeddings.append(embed) 80 | print(' ... completed') 81 | 82 | embeddings = np.vstack(embeddings) 83 | label_ids = np.hstack(label_ids) 84 | label_cams = np.hstack(label_cams) 85 | 86 | ## dump results 87 | logger.info('dump embeddings') 88 | embd_res = {'embeddings': embeddings, 'label_ids': label_ids, 'label_cams': label_cams} 89 | with open(args.store_pth, 'wb') as fw: 90 | pickle.dump(embd_res, fw) 91 | 92 | logger.info('embedding finished') 93 | 94 | 95 | if __name__ == '__main__': 96 | args = parse_args() 97 | embed(args) 98 | -------------------------------------------------------------------------------- /eval.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | import torch 5 | 6 | import pickle 7 | import numpy as np 8 | import sys 9 | import logging 10 | import argparse 11 | from tqdm import tqdm 12 | 13 | from utils import pdist_np as pdist 14 | 15 | 16 | def parse_args(): 17 | parse = argparse.ArgumentParser() 18 | parse.add_argument( 19 | '--gallery_embs', 20 | dest = 'gallery_embs', 21 | type = str, 22 | default = './res/emb_gallery.pkl', 23 | help = 'path to embeddings of gallery dataset' 24 | ) 25 | parse.add_argument( 26 | '--query_embs', 27 | dest = 'query_embs', 28 | type = str, 29 | default = './res/emb_query.pkl', 30 | help = 'path to embeddings of query dataset' 31 | ) 32 | parse.add_argument( 33 | '--cmc_rank', 34 | dest = 'cmc_rank', 35 | type = int, 36 | default = 1, 37 | help = 'path to embeddings of query dataset' 38 | ) 39 | 40 | return parse.parse_args() 41 | 42 | 43 | def evaluate(args): 44 | ## logging 45 | FORMAT = '%(levelname)s %(filename)s:%(lineno)d: %(message)s' 46 | logging.basicConfig(level=logging.INFO, format=FORMAT, stream=sys.stdout) 47 | logger = logging.getLogger(__name__) 48 | 49 | ## load embeddings 50 | logger.info('loading gallery embeddings') 51 | with open(args.gallery_embs, 'rb') as fr: 52 | gallery_dict = pickle.load(fr) 53 | emb_gallery, lb_ids_gallery, lb_cams_gallery = gallery_dict['embeddings'], gallery_dict['label_ids'], gallery_dict['label_cams'] 54 | logger.info('loading query embeddings') 55 | with open(args.query_embs, 'rb') as fr: 56 | query_dict = pickle.load(fr) 57 | emb_query, lb_ids_query, lb_cams_query = query_dict['embeddings'], query_dict['label_ids'], query_dict['label_cams'] 58 | 59 | ## compute and clean distance matrix 60 | dist_mtx = pdist(emb_query, emb_gallery) 61 | n_q, n_g = dist_mtx.shape 62 | indices = np.argsort(dist_mtx, axis = 1) 63 | matches = lb_ids_gallery[indices] == lb_ids_query[:, np.newaxis] 64 | matches = matches.astype(np.int32) 65 | all_aps = [] 66 | all_cmcs = [] 67 | logger.info('starting evaluating ...') 68 | for qidx in tqdm(range(n_q)): 69 | qpid = lb_ids_query[qidx] 70 | qcam = lb_cams_query[qidx] 71 | 72 | order = indices[qidx] 73 | pid_diff = lb_ids_gallery[order] != qpid 74 | cam_diff = lb_cams_gallery[order] != qcam 75 | useful = lb_ids_gallery[order] != -1 76 | keep = np.logical_or(pid_diff, cam_diff) 77 | keep = np.logical_and(keep, useful) 78 | match = matches[qidx][keep] 79 | 80 | if not np.any(match): continue 81 | 82 | cmc = match.cumsum() 83 | cmc[cmc > 1] = 1 84 | all_cmcs.append(cmc[:args.cmc_rank]) 85 | 86 | num_real = match.sum() 87 | match_cum = match.cumsum() 88 | match_cum = [el / (1.0 + i) for i, el in enumerate(match_cum)] 89 | match_cum = np.array(match_cum) * match 90 | ap = match_cum.sum() / num_real 91 | all_aps.append(ap) 92 | 93 | assert len(all_aps) > 0, "NO QUERY MATCHED" 94 | mAP = sum(all_aps) / len(all_aps) 95 | all_cmcs = np.array(all_cmcs, dtype = np.float32) 96 | cmc = np.mean(all_cmcs, axis = 0) 97 | 98 | print('mAP is: {}, cmc is: {}'.format(mAP, cmc)) 99 | 100 | 101 | if __name__ == '__main__': 102 | args = parse_args() 103 | evaluate(args) 104 | -------------------------------------------------------------------------------- /logger.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | 5 | import os 6 | import logging 7 | import time 8 | 9 | 10 | def get_logger(): 11 | if not os.path.exists('./res'): os.makedirs('./res') 12 | FORMAT = '%(levelname)s %(filename)s(%(lineno)d): %(message)s' 13 | logfile = 'triplet_reid-{}.log'.format(time.strftime('%Y-%m-%d-%H-%M-%S')) 14 | logfile = os.path.join('res', logfile) 15 | logging.basicConfig(level=logging.INFO, format=FORMAT, filename=logfile) 16 | logger = logging.getLogger(__name__) 17 | logger.addHandler(logging.StreamHandler()) 18 | 19 | return logger 20 | 21 | logger = get_logger() 22 | -------------------------------------------------------------------------------- /loss.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | 5 | import torch 6 | import torch.nn as nn 7 | 8 | 9 | class TripletLoss(nn.Module): 10 | ''' 11 | Compute normal triplet loss or soft margin triplet loss given triplets 12 | ''' 13 | def __init__(self, margin = None): 14 | super(TripletLoss, self).__init__() 15 | self.margin = margin 16 | if self.margin is None: # use soft-margin 17 | self.Loss = nn.SoftMarginLoss() 18 | else: 19 | self.Loss = nn.TripletMarginLoss(margin = margin, p = 2) 20 | 21 | def forward(self, anchor, pos, neg): 22 | if self.margin is None: 23 | num_samples = anchor.shape[0] 24 | y = torch.ones((num_samples, 1)).view(-1) 25 | if anchor.is_cuda: y = y.cuda() 26 | ap_dist = torch.norm(anchor - pos, 2, dim = 1).view(-1) 27 | an_dist = torch.norm(anchor - neg, 2, dim = 1).view(-1) 28 | loss = self.Loss(an_dist - ap_dist, y) 29 | else: 30 | loss = self.Loss(anchor, pos, neg) 31 | 32 | return loss 33 | 34 | 35 | if __name__ == '__main__': 36 | pass 37 | -------------------------------------------------------------------------------- /optimizer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | 5 | import torch 6 | import logging 7 | import sys 8 | from logger import logger 9 | 10 | 11 | 12 | class AdamOptimWrapper(object): 13 | ''' 14 | A wrapper of Adam optimizer which allows to adjust the optimizing parameters 15 | according to the stategy presented in the paper 16 | ''' 17 | def __init__(self, params, lr, wd=0, t0=15000, t1=25000, *args, **kwargs): 18 | super(AdamOptimWrapper, self).__init__(*args, **kwargs) 19 | self.base_lr = lr 20 | self.wd = wd 21 | self.t0 = t0 22 | self.t1 = t1 23 | self.step_count = 0 24 | self.optim = torch.optim.Adam(params, 25 | lr = self.base_lr, 26 | weight_decay = self.wd) 27 | 28 | 29 | def step(self): 30 | self.step_count += 1 31 | self.optim.step() 32 | # adjust optimizer parameters 33 | if self.step_count == self.t0: 34 | betas_old = self.optim.param_groups[0]['betas'] 35 | self.optim.param_groups[0]['betas'] = (0.5, 0.999) 36 | betas_new = self.optim.param_groups[0]['betas'] 37 | logger.info('==> changing adam betas from {} to {}'.format(betas_old, betas_new)) 38 | logger.info('==> start droping lr exponentially') 39 | elif self.t0 < self.step_count < self.t1: 40 | lr = self.base_lr * (0.001 ** ((self.step_count + 1.0 - self.t0) / (self.t1 + 1.0 - self.t0))) 41 | for pg in self.optim.param_groups: 42 | pg['lr'] = lr 43 | self.optim.defaults['lr'] = lr 44 | 45 | def zero_grad(self): 46 | self.optim.zero_grad() 47 | 48 | @property 49 | def lr(self): 50 | return self.optim.param_groups[0]['lr'] 51 | -------------------------------------------------------------------------------- /random_erasing.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | 3 | from torchvision.transforms import * 4 | 5 | from PIL import Image 6 | import random 7 | import math 8 | import numpy as np 9 | import torch 10 | 11 | class RandomErasing(object): 12 | """ Randomly selects a rectangle region in an image and erases its pixels. 13 | 'Random Erasing Data Augmentation' by Zhong et al. 14 | See https://arxiv.org/pdf/1708.04896.pdf 15 | Args: 16 | probability: The probability that the Random Erasing operation will be performed. 17 | sl: Minimum proportion of erased area against input image. 18 | sh: Maximum proportion of erased area against input image. 19 | r1: Minimum aspect ratio of erased area. 20 | mean: Erasing value. 21 | """ 22 | 23 | def __init__(self, probability = 0.5, sl = 0.02, sh = 0.4, r1 = 0.3, mean=[0.4914, 0.4822, 0.4465]): 24 | self.probability = probability 25 | self.mean = mean 26 | self.sl = sl 27 | self.sh = sh 28 | self.r1 = r1 29 | 30 | def __call__(self, img): 31 | 32 | if random.uniform(0, 1) > self.probability: 33 | return img 34 | 35 | for attempt in range(100): 36 | area = img.size()[1] * img.size()[2] 37 | 38 | target_area = random.uniform(self.sl, self.sh) * area 39 | aspect_ratio = random.uniform(self.r1, 1/self.r1) 40 | 41 | h = int(round(math.sqrt(target_area * aspect_ratio))) 42 | w = int(round(math.sqrt(target_area / aspect_ratio))) 43 | 44 | if w < img.size()[2] and h < img.size()[1]: 45 | x1 = random.randint(0, img.size()[1] - h) 46 | y1 = random.randint(0, img.size()[2] - w) 47 | if img.size()[0] == 3: 48 | img[0, x1:x1+h, y1:y1+w] = self.mean[0] 49 | img[1, x1:x1+h, y1:y1+w] = self.mean[1] 50 | img[2, x1:x1+h, y1:y1+w] = self.mean[2] 51 | else: 52 | img[0, x1:x1+h, y1:y1+w] = self.mean[0] 53 | return img 54 | 55 | return img 56 | -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | import torch 5 | import torch.nn as nn 6 | import torchvision 7 | from torch.utils.data import DataLoader 8 | 9 | import sys 10 | import os 11 | import logging 12 | import time 13 | import itertools 14 | 15 | from backbone import EmbedNetwork 16 | from loss import TripletLoss 17 | from triplet_selector import BatchHardTripletSelector 18 | from batch_sampler import BatchSampler 19 | from datasets.Market1501 import Market1501 20 | from optimizer import AdamOptimWrapper 21 | from logger import logger 22 | 23 | 24 | 25 | def train(): 26 | ## setup 27 | torch.multiprocessing.set_sharing_strategy('file_system') 28 | if not os.path.exists('./res'): os.makedirs('./res') 29 | 30 | ## model and loss 31 | logger.info('setting up backbone model and loss') 32 | net = EmbedNetwork().cuda() 33 | net = nn.DataParallel(net) 34 | triplet_loss = TripletLoss(margin = None).cuda() # no margin means soft-margin 35 | 36 | ## optimizer 37 | logger.info('creating optimizer') 38 | optim = AdamOptimWrapper(net.parameters(), lr = 3e-4, wd = 0, t0 = 15000, t1 = 25000) 39 | 40 | ## dataloader 41 | selector = BatchHardTripletSelector() 42 | ds = Market1501('datasets/Market-1501-v15.09.15/bounding_box_train', is_train = True) 43 | sampler = BatchSampler(ds, 18, 4) 44 | dl = DataLoader(ds, batch_sampler = sampler, num_workers = 4) 45 | diter = iter(dl) 46 | 47 | ## train 48 | logger.info('start training ...') 49 | loss_avg = [] 50 | count = 0 51 | t_start = time.time() 52 | while True: 53 | try: 54 | imgs, lbs, _ = next(diter) 55 | except StopIteration: 56 | diter = iter(dl) 57 | imgs, lbs, _ = next(diter) 58 | 59 | net.train() 60 | imgs = imgs.cuda() 61 | lbs = lbs.cuda() 62 | embds = net(imgs) 63 | anchor, positives, negatives = selector(embds, lbs) 64 | 65 | loss = triplet_loss(anchor, positives, negatives) 66 | optim.zero_grad() 67 | loss.backward() 68 | optim.step() 69 | 70 | loss_avg.append(loss.detach().cpu().numpy()) 71 | if count % 20 == 0 and count != 0: 72 | loss_avg = sum(loss_avg) / len(loss_avg) 73 | t_end = time.time() 74 | time_interval = t_end - t_start 75 | logger.info('iter: {}, loss: {:4f}, lr: {:4f}, time: {:3f}'.format(count, loss_avg, optim.lr, time_interval)) 76 | loss_avg = [] 77 | t_start = t_end 78 | 79 | count += 1 80 | if count == 25000: break 81 | 82 | ## dump model 83 | logger.info('saving trained model') 84 | torch.save(net.module.state_dict(), './res/model.pkl') 85 | 86 | logger.info('everything finished') 87 | 88 | 89 | if __name__ == '__main__': 90 | train() 91 | -------------------------------------------------------------------------------- /triplet_selector.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | 5 | import torch 6 | import numpy as np 7 | from utils import pdist_torch as pdist 8 | 9 | 10 | class BatchHardTripletSelector(object): 11 | ''' 12 | a selector to generate hard batch embeddings from the embedded batch 13 | ''' 14 | def __init__(self, *args, **kwargs): 15 | super(BatchHardTripletSelector, self).__init__() 16 | 17 | def __call__(self, embeds, labels): 18 | dist_mtx = pdist(embeds, embeds).detach().cpu().numpy() 19 | labels = labels.contiguous().cpu().numpy().reshape((-1, 1)) 20 | num = labels.shape[0] 21 | dia_inds = np.diag_indices(num) 22 | lb_eqs = labels == labels.T 23 | lb_eqs[dia_inds] = False 24 | dist_same = dist_mtx.copy() 25 | dist_same[lb_eqs == False] = -np.inf 26 | pos_idxs = np.argmax(dist_same, axis = 1) 27 | dist_diff = dist_mtx.copy() 28 | lb_eqs[dia_inds] = True 29 | dist_diff[lb_eqs == True] = np.inf 30 | neg_idxs = np.argmin(dist_diff, axis = 1) 31 | pos = embeds[pos_idxs].contiguous().view(num, -1) 32 | neg = embeds[neg_idxs].contiguous().view(num, -1) 33 | return embeds, pos, neg 34 | 35 | 36 | 37 | if __name__ == '__main__': 38 | embds = torch.randn(10, 128) 39 | labels = torch.tensor([0,1,2,2,0,1,2,1,1,0]) 40 | selector = BatchHardTripletSelector() 41 | anchor, pos, neg = selector(embds, labels) 42 | -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | import torch 5 | import numpy as np 6 | from PIL import Image 7 | 8 | 9 | def pdist_torch(emb1, emb2): 10 | ''' 11 | compute the eucilidean distance matrix between embeddings1 and embeddings2 12 | using gpu 13 | ''' 14 | m, n = emb1.shape[0], emb2.shape[0] 15 | emb1_pow = torch.pow(emb1, 2).sum(dim = 1, keepdim = True).expand(m, n) 16 | emb2_pow = torch.pow(emb2, 2).sum(dim = 1, keepdim = True).expand(n, m).t() 17 | dist_mtx = emb1_pow + emb2_pow 18 | dist_mtx = dist_mtx.addmm_(1, -2, emb1, emb2.t()) 19 | dist_mtx = dist_mtx.clamp(min = 1e-12).sqrt() 20 | return dist_mtx 21 | 22 | 23 | def pdist_np(emb1, emb2): 24 | ''' 25 | compute the eucilidean distance matrix between embeddings1 and embeddings2 26 | using cpu 27 | ''' 28 | m, n = emb1.shape[0], emb2.shape[0] 29 | emb1_pow = np.square(emb1).sum(axis = 1)[..., np.newaxis] 30 | emb2_pow = np.square(emb2).sum(axis = 1)[np.newaxis, ...] 31 | dist_mtx = -2 * np.matmul(emb1, emb2.T) + emb1_pow + emb2_pow 32 | dist_mtx = np.sqrt(dist_mtx.clip(min = 1e-12)) 33 | return dist_mtx 34 | 35 | 36 | 37 | if __name__ == "__main__": 38 | a = np.arange(4*128).reshape(4, 128) 39 | b = np.arange(10, 10 + 5*128).reshape(5, 128) 40 | r1 = pdist_np(a, b) 41 | print(r1.shape) 42 | print(r1) 43 | 44 | a = torch.Tensor(a) 45 | b = torch.Tensor(b) 46 | r2 = pdist_torch(a, b) 47 | print(r2.shape) 48 | print(r2) 49 | --------------------------------------------------------------------------------