deep-person-reid/torchreid/datasets/msmt17.py

119 lines
4.3 KiB
Python
Raw Normal View History

2018-07-04 17:32:43 +08:00
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
2018-07-02 17:17:14 +08:00
import os
import glob
import re
import sys
import urllib
import tarfile
import zipfile
import os.path as osp
from scipy.io import loadmat
import numpy as np
import h5py
from scipy.misc import imsave
2018-11-05 06:59:46 +08:00
from .bases import BaseImageDataset
2018-07-02 17:17:14 +08:00
2018-11-05 06:59:46 +08:00
2019-01-23 02:18:59 +08:00
# To adapt to different versions
# Log:
# 22.01.2019: v1 and v2 only differ in dir names
_TRAIN_DIR_KEY = 'train_dir'
2019-01-25 06:12:02 +08:00
_TEST_DIR_KEY = 'test_dir'
2019-01-23 02:18:59 +08:00
_VERSION = {
'MSMT17_V1': {
_TRAIN_DIR_KEY: 'train',
_TEST_DIR_KEY: 'test',
},
'MSMT17_V2': {
_TRAIN_DIR_KEY: 'mask_train_v2',
_TEST_DIR_KEY: 'mask_test_v2',
}
}
2018-11-05 06:59:46 +08:00
class MSMT17(BaseImageDataset):
2018-07-02 17:17:14 +08:00
"""
MSMT17
Reference:
Wei et al. Person Transfer GAN to Bridge Domain Gap for Person Re-Identification. CVPR 2018.
URL: http://www.pkuvmc.com/publications/msmt17.html
Dataset statistics:
# identities: 4101
# images: 32621 (train) + 11659 (query) + 82161 (gallery)
# cameras: 15
"""
dataset_dir = 'msmt17'
2018-08-12 05:22:48 +08:00
def __init__(self, root='data', verbose=True, **kwargs):
super(MSMT17, self).__init__(root)
self.dataset_dir = osp.join(self.root, self.dataset_dir)
2019-01-23 02:18:59 +08:00
has_main_dir = False
for main_dir in _VERSION:
if osp.exists(osp.join(self.dataset_dir, main_dir)):
2019-01-25 06:12:02 +08:00
train_dir = _VERSION[main_dir][_TRAIN_DIR_KEY]
test_dir = _VERSION[main_dir][_TEST_DIR_KEY]
2019-01-23 02:18:59 +08:00
has_main_dir = True
break
assert has_main_dir, "Dataset folder not found"
self.train_dir = osp.join(self.dataset_dir, main_dir, train_dir)
self.test_dir = osp.join(self.dataset_dir, main_dir, test_dir)
self.list_train_path = osp.join(self.dataset_dir, main_dir, 'list_train.txt')
self.list_val_path = osp.join(self.dataset_dir, main_dir, 'list_val.txt')
self.list_query_path = osp.join(self.dataset_dir, main_dir, 'list_query.txt')
self.list_gallery_path = osp.join(self.dataset_dir, main_dir, 'list_gallery.txt')
2018-07-02 17:17:14 +08:00
self._check_before_run()
2018-11-05 06:59:46 +08:00
train = self._process_dir(self.train_dir, self.list_train_path)
#val = self._process_dir(self.train_dir, self.list_val_path)
query = self._process_dir(self.test_dir, self.list_query_path)
gallery = self._process_dir(self.test_dir, self.list_gallery_path)
2018-07-02 17:17:14 +08:00
2019-01-23 02:18:59 +08:00
# To fairly compare with published methods, don't use val images for training
2018-07-02 17:17:14 +08:00
#train += val
#num_train_imgs += num_val_imgs
2018-07-02 18:57:01 +08:00
if verbose:
print("=> MSMT17 loaded")
2018-11-05 06:59:46 +08:00
self.print_dataset_statistics(train, query, gallery)
2018-07-02 17:17:14 +08:00
self.train = train
self.query = query
self.gallery = gallery
2018-11-05 06:59:46 +08:00
self.num_train_pids, self.num_train_imgs, self.num_train_cams = self.get_imagedata_info(self.train)
self.num_query_pids, self.num_query_imgs, self.num_query_cams = self.get_imagedata_info(self.query)
self.num_gallery_pids, self.num_gallery_imgs, self.num_gallery_cams = self.get_imagedata_info(self.gallery)
2018-07-02 17:17:14 +08:00
def _check_before_run(self):
"""Check if all files are available before going deeper"""
if not osp.exists(self.dataset_dir):
raise RuntimeError("'{}' is not available".format(self.dataset_dir))
if not osp.exists(self.train_dir):
raise RuntimeError("'{}' is not available".format(self.train_dir))
if not osp.exists(self.test_dir):
raise RuntimeError("'{}' is not available".format(self.test_dir))
def _process_dir(self, dir_path, list_path):
with open(list_path, 'r') as txt:
lines = txt.readlines()
dataset = []
pid_container = set()
for img_idx, img_info in enumerate(lines):
img_path, pid = img_info.split(' ')
pid = int(pid) # no need to relabel
2018-11-05 05:27:08 +08:00
camid = int(img_path.split('_')[2]) - 1 # index starts from 0
2018-07-02 17:17:14 +08:00
img_path = osp.join(dir_path, img_path)
dataset.append((img_path, pid, camid))
pid_container.add(pid)
num_pids = len(pid_container)
# check if pid starts from 0 and increments with 1
for idx, pid in enumerate(pid_container):
assert idx == pid, "See code comment for explanation"
2018-11-05 06:59:46 +08:00
return dataset