diff --git a/args.py b/args.py index 6aa565f..20a29d7 100644 --- a/args.py +++ b/args.py @@ -8,41 +8,41 @@ def argument_parser(): # Datasets (general) # ************************************************************ parser.add_argument('--root', type=str, default='data', - help="root path to data directory") + help='root path to data directory') parser.add_argument('-s', '--source-names', type=str, required=True, nargs='+', - help="source datasets (delimited by space)") + help='source datasets (delimited by space)') parser.add_argument('-t', '--target-names', type=str, required=True, nargs='+', - help="target datasets (delimited by space)") + help='target datasets (delimited by space)') parser.add_argument('-j', '--workers', default=4, type=int, - help="number of data loading workers (tips: 4 or 8 times number of gpus)") + help='number of data loading workers (tips: 4 or 8 times number of gpus)') parser.add_argument('--height', type=int, default=256, - help="height of an image") + help='height of an image') parser.add_argument('--width', type=int, default=128, - help="width of an image") + help='width of an image') parser.add_argument('--split-id', type=int, default=0, - help="split index (note: 0-based)") + help='split index (note: 0-based)') parser.add_argument('--train-sampler', type=str, default='RandomSampler', - help="sampler for trainloader") + help='sampler for trainloader') # ************************************************************ # Video datasets # ************************************************************ parser.add_argument('--seq-len', type=int, default=15, - help="number of images to sample in a tracklet") + help='number of images to sample in a tracklet') parser.add_argument('--sample-method', type=str, default='evenly', - help="how to sample images from a tracklet") + help='how to sample images from a tracklet') parser.add_argument('--pool-tracklet-features', type=str, default='avg', choices=['avg', 'max'], - help="how to pool features over a tracklet (for video reid)") + help='how to pool features over a tracklet (for video reid)') # ************************************************************ # Dataset-specific setting # ************************************************************ parser.add_argument('--cuhk03-labeled', action='store_true', - help="use labeled images, if false, use detected images") + help='use labeled images, if false, use detected images') parser.add_argument('--cuhk03-classic-split', action='store_true', - help="use classic split by Li et al. CVPR'14") + help='use classic split by Li et al. CVPR\'14') parser.add_argument('--use-metric-cuhk03', action='store_true', - help="use cuhk03's metric for evaluation") + help='use cuhk03\'s metric for evaluation') parser.add_argument('--market1501-500k', action='store_true', help='add 500k distractors to the gallery set for market1501') @@ -51,70 +51,70 @@ def argument_parser(): # Optimization options # ************************************************************ parser.add_argument('--optim', type=str, default='adam', - help="optimization algorithm (see optimizers.py)") + help='optimization algorithm (see optimizers.py)') parser.add_argument('--lr', default=0.0003, type=float, - help="initial learning rate") + help='initial learning rate') parser.add_argument('--weight-decay', default=5e-04, type=float, - help="weight decay") + help='weight decay') # sgd parser.add_argument('--momentum', default=0.9, type=float, - help="momentum factor for sgd and rmsprop") + help='momentum factor for sgd and rmsprop') parser.add_argument('--sgd-dampening', default=0, type=float, - help="sgd's dampening for momentum") + help='sgd\'s dampening for momentum') parser.add_argument('--sgd-nesterov', action='store_true', - help="whether to enable sgd's Nesterov momentum") + help='whether to enable sgd\'s Nesterov momentum') # rmsprop parser.add_argument('--rmsprop-alpha', default=0.99, type=float, - help="rmsprop's smoothing constant") + help='rmsprop\'s smoothing constant') # adam/amsgrad parser.add_argument('--adam-beta1', default=0.9, type=float, - help="exponential decay rate for adam's first moment") + help='exponential decay rate for adam\'s first moment') parser.add_argument('--adam-beta2', default=0.999, type=float, - help="exponential decay rate for adam's second moment") + help='exponential decay rate for adam\'s second moment') # ************************************************************ # Training hyperparameters # ************************************************************ parser.add_argument('--max-epoch', default=60, type=int, - help="maximum epochs to run") + help='maximum epochs to run') parser.add_argument('--start-epoch', default=0, type=int, - help="manual epoch number (useful when restart)") + help='manual epoch number (useful when restart)') parser.add_argument('--stepsize', default=[20, 40], nargs='+', type=int, - help="stepsize to decay learning rate") + help='stepsize to decay learning rate') parser.add_argument('--gamma', default=0.1, type=float, - help="learning rate decay") + help='learning rate decay') parser.add_argument('--train-batch-size', default=32, type=int, - help="training batch size") + help='training batch size') parser.add_argument('--test-batch-size', default=100, type=int, - help="test batch size") + help='test batch size') parser.add_argument('--always-fixbase', action='store_true', - help="always fix base network and only train specified layers") + help='always fix base network and only train specified layers') parser.add_argument('--fixbase-epoch', type=int, default=0, - help="how many epochs to fix base network (only train randomly initialized classifier)") + help='how many epochs to fix base network (only train randomly initialized classifier)') parser.add_argument('--open-layers', type=str, nargs='+', default=['classifier'], - help="open specified layers for training while keeping others frozen") + help='open specified layers for training while keeping others frozen') # ************************************************************ # Cross entropy loss-specific setting # ************************************************************ parser.add_argument('--label-smooth', action='store_true', - help="use label smoothing regularizer in cross entropy loss") + help='use label smoothing regularizer in cross entropy loss') # ************************************************************ # Hard triplet loss-specific setting # ************************************************************ parser.add_argument('--margin', type=float, default=0.3, - help="margin for triplet loss") + help='margin for triplet loss') parser.add_argument('--num-instances', type=int, default=4, - help="number of instances per identity") + help='number of instances per identity') parser.add_argument('--htri-only', action='store_true', - help="only use hard triplet loss") + help='only use hard triplet loss') parser.add_argument('--lambda-xent', type=float, default=1, - help="weight to balance cross entropy loss") + help='weight to balance cross entropy loss') parser.add_argument('--lambda-htri', type=float, default=1, - help="weight to balance hard triplet loss") + help='weight to balance hard triplet loss') # ************************************************************ # Architecture @@ -125,33 +125,33 @@ def argument_parser(): # Test settings # ************************************************************ parser.add_argument('--load-weights', type=str, default='', - help="load pretrained weights but ignore layers that don't match in size") + help='load pretrained weights but ignore layers that don\'t match in size') parser.add_argument('--evaluate', action='store_true', - help="evaluate only") + help='evaluate only') parser.add_argument('--eval-freq', type=int, default=-1, - help="evaluation frequency (set to -1 to test only in the end)") + help='evaluation frequency (set to -1 to test only in the end)') parser.add_argument('--start-eval', type=int, default=0, - help="start to evaluate after a specific epoch") + help='start to evaluate after a specific epoch') # ************************************************************ # Miscs # ************************************************************ parser.add_argument('--print-freq', type=int, default=10, - help="print frequency") + help='print frequency') parser.add_argument('--seed', type=int, default=1, - help="manual seed") + help='manual seed') parser.add_argument('--resume', type=str, default='', metavar='PATH', - help="resume from a checkpoint") + help='resume from a checkpoint') parser.add_argument('--save-dir', type=str, default='log', - help="path to save log and model weights") + help='path to save log and model weights') parser.add_argument('--use-cpu', action='store_true', - help="use cpu") + help='use cpu') parser.add_argument('--gpu-devices', default='0', type=str, help='gpu device ids for CUDA_VISIBLE_DEVICES') parser.add_argument('--use-avai-gpus', action='store_true', - help="use available gpus instead of specified devices (useful when using managed clusters)") + help='use available gpus instead of specified devices (useful when using managed clusters)') parser.add_argument('--visualize-ranks', action='store_true', - help="visualize ranked results, only available in evaluation mode") + help='visualize ranked results, only available in evaluation mode') return parser diff --git a/torchreid/data_manager.py b/torchreid/data_manager.py index c406d73..b97d184 100644 --- a/torchreid/data_manager.py +++ b/torchreid/data_manager.py @@ -56,7 +56,7 @@ class ImageDataManager(BaseDataManager): ): super(ImageDataManager, self).__init__() - print("=> Initializing TRAIN (source) datasets") + print('=> Initializing TRAIN (source) datasets') train = [] self._num_train_pids = 0 self._num_train_cams = 0 @@ -88,7 +88,7 @@ class ImageDataManager(BaseDataManager): pin_memory=use_gpu, drop_last=True ) - print("=> Initializing TEST (target) datasets") + print('=> Initializing TEST (target) datasets') self.testloader_dict = {name: {'query': None, 'gallery': None} for name in target_names} self.testdataset_dict = {name: {'query': None, 'gallery': None} for name in target_names} @@ -115,16 +115,16 @@ class ImageDataManager(BaseDataManager): self.testdataset_dict[name]['query'] = dataset.query self.testdataset_dict[name]['gallery'] = dataset.gallery - print("\n") - print(" **************** Summary ****************") - print(" train names : {}".format(source_names)) - print(" # train datasets : {}".format(len(source_names))) - print(" # train ids : {}".format(self.num_train_pids)) - print(" # train images : {}".format(len(train))) - print(" # train cameras : {}".format(self.num_train_cams)) - print(" test names : {}".format(target_names)) - print(" *****************************************") - print("\n") + print('\n') + print(' **************** Summary ****************') + print(' train names : {}'.format(source_names)) + print(' # train datasets : {}'.format(len(source_names))) + print(' # train ids : {}'.format(self.num_train_pids)) + print(' # train images : {}'.format(len(train))) + print(' # train cameras : {}'.format(self.num_train_cams)) + print(' test names : {}'.format(target_names)) + print(' *****************************************') + print('\n') class VideoDataManager(BaseDataManager): @@ -151,7 +151,7 @@ class VideoDataManager(BaseDataManager): ): super(VideoDataManager, self).__init__() - print("=> Initializing TRAIN (source) datasets") + print('=> Initializing TRAIN (source) datasets') train = [] self._num_train_pids = 0 self._num_train_cams = 0 @@ -196,7 +196,7 @@ class VideoDataManager(BaseDataManager): pin_memory=use_gpu, drop_last=True ) - print("=> Initializing TEST (target) datasets") + print('=> Initializing TEST (target) datasets') self.testloader_dict = {name: {'query': None, 'gallery': None} for name in target_names} self.testdataset_dict = {name: {'query': None, 'gallery': None} for name in target_names} @@ -220,16 +220,16 @@ class VideoDataManager(BaseDataManager): self.testdataset_dict[name]['query'] = dataset.query self.testdataset_dict[name]['gallery'] = dataset.gallery - print("\n") - print(" **************** Summary ****************") - print(" train names : {}".format(source_names)) - print(" # train datasets : {}".format(len(source_names))) - print(" # train ids : {}".format(self.num_train_pids)) + print('\n') + print(' **************** Summary ****************') + print(' train names : {}'.format(source_names)) + print(' # train datasets : {}'.format(len(source_names))) + print(' # train ids : {}'.format(self.num_train_pids)) if image_training: - print(" # train images : {}".format(len(train))) + print(' # train images : {}'.format(len(train))) else: - print(" # train tracklets: {}".format(len(train))) - print(" # train cameras : {}".format(self.num_train_cams)) - print(" test names : {}".format(target_names)) - print(" *****************************************") - print("\n") \ No newline at end of file + print(' # train tracklets: {}'.format(len(train))) + print(' # train cameras : {}'.format(self.num_train_cams)) + print(' test names : {}'.format(target_names)) + print(' *****************************************') + print('\n') \ No newline at end of file diff --git a/torchreid/dataset_loader.py b/torchreid/dataset_loader.py index 1cf388f..633dc16 100644 --- a/torchreid/dataset_loader.py +++ b/torchreid/dataset_loader.py @@ -17,13 +17,13 @@ def read_image(img_path): This can avoid IOError incurred by heavy IO process.""" got_img = False if not osp.exists(img_path): - raise IOError("{} does not exist".format(img_path)) + raise IOError('{} does not exist'.format(img_path)) while not got_img: try: img = Image.open(img_path).convert('RGB') got_img = True except IOError: - print("IOError incurred when reading '{}'. Will redo. Don't worry. Just chill.".format(img_path)) + print('IOError incurred when reading "{}". Will redo. Don\'t worry. Just chill.'.format(img_path)) pass return img @@ -100,7 +100,7 @@ class VideoDataset(Dataset): indices = np.arange(num) else: - raise ValueError("Unknown sample method: {}. Expected one of {}".format(self.sample_method, self._sample_methods)) + raise ValueError('Unknown sample method: {}. Expected one of {}'.format(self.sample_method, self._sample_methods)) imgs = [] for index in indices: @@ -112,4 +112,4 @@ class VideoDataset(Dataset): imgs.append(img) imgs = torch.cat(imgs, dim=0) - return imgs, pid, camid + return imgs, pid, camid \ No newline at end of file diff --git a/torchreid/datasets/__init__.py b/torchreid/datasets/__init__.py index a724a6e..9bb5632 100644 --- a/torchreid/datasets/__init__.py +++ b/torchreid/datasets/__init__.py @@ -43,11 +43,11 @@ __vidreid_factory = { def init_imgreid_dataset(name, **kwargs): if name not in list(__imgreid_factory.keys()): - raise KeyError("Invalid dataset, got '{}', but expected to be one of {}".format(name, list(__imgreid_factory.keys()))) + raise KeyError('Invalid dataset, got "{}", but expected to be one of {}'.format(name, list(__imgreid_factory.keys()))) return __imgreid_factory[name](**kwargs) def init_vidreid_dataset(name, **kwargs): if name not in list(__vidreid_factory.keys()): - raise KeyError("Invalid dataset, got '{}', but expected to be one of {}".format(name, list(__vidreid_factory.keys()))) + raise KeyError('Invalid dataset, got "{}", but expected to be one of {}'.format(name, list(__vidreid_factory.keys()))) return __vidreid_factory[name](**kwargs) \ No newline at end of file diff --git a/torchreid/datasets/bases.py b/torchreid/datasets/bases.py index 646dbcf..136b780 100644 --- a/torchreid/datasets/bases.py +++ b/torchreid/datasets/bases.py @@ -54,14 +54,14 @@ class BaseImageDataset(BaseDataset): num_query_pids, num_query_imgs, num_query_cams = self.get_imagedata_info(query) num_gallery_pids, num_gallery_imgs, num_gallery_cams = self.get_imagedata_info(gallery) - print("Dataset statistics:") - print(" ----------------------------------------") - print(" subset | # ids | # images | # cameras") - print(" ----------------------------------------") - print(" train | {:5d} | {:8d} | {:9d}".format(num_train_pids, num_train_imgs, num_train_cams)) - print(" query | {:5d} | {:8d} | {:9d}".format(num_query_pids, num_query_imgs, num_query_cams)) - print(" gallery | {:5d} | {:8d} | {:9d}".format(num_gallery_pids, num_gallery_imgs, num_gallery_cams)) - print(" ----------------------------------------") + print('Image Dataset statistics:') + print(' ----------------------------------------') + print(' subset | # ids | # images | # cameras') + print(' ----------------------------------------') + print(' train | {:5d} | {:8d} | {:9d}'.format(num_train_pids, num_train_imgs, num_train_cams)) + print(' query | {:5d} | {:8d} | {:9d}'.format(num_query_pids, num_query_imgs, num_query_cams)) + print(' gallery | {:5d} | {:8d} | {:9d}'.format(num_gallery_pids, num_gallery_imgs, num_gallery_cams)) + print(' ----------------------------------------') class BaseVideoDataset(BaseDataset): @@ -84,13 +84,13 @@ class BaseVideoDataset(BaseDataset): max_num = np.max(tracklet_stats) avg_num = np.mean(tracklet_stats) - print("Dataset statistics:") - print(" -------------------------------------------") - print(" subset | # ids | # tracklets | # cameras") - print(" -------------------------------------------") - print(" train | {:5d} | {:11d} | {:9d}".format(num_train_pids, num_train_tracklets, num_train_cams)) - print(" query | {:5d} | {:11d} | {:9d}".format(num_query_pids, num_query_tracklets, num_query_cams)) - print(" gallery | {:5d} | {:11d} | {:9d}".format(num_gallery_pids, num_gallery_tracklets, num_gallery_cams)) - print(" -------------------------------------------") - print(" number of images per tracklet: {} ~ {}, average {:.2f}".format(min_num, max_num, avg_num)) - print(" -------------------------------------------") \ No newline at end of file + print('Video Dataset statistics:') + print(' -------------------------------------------') + print(' subset | # ids | # tracklets | # cameras') + print(' -------------------------------------------') + print(' train | {:5d} | {:11d} | {:9d}'.format(num_train_pids, num_train_tracklets, num_train_cams)) + print(' query | {:5d} | {:11d} | {:9d}'.format(num_query_pids, num_query_tracklets, num_query_cams)) + print(' gallery | {:5d} | {:11d} | {:9d}'.format(num_gallery_pids, num_gallery_tracklets, num_gallery_cams)) + print(' -------------------------------------------') + print(' number of images per tracklet: {} ~ {}, average {:.2f}'.format(min_num, max_num, avg_num)) + print(' -------------------------------------------') \ No newline at end of file diff --git a/torchreid/datasets/cuhk01.py b/torchreid/datasets/cuhk01.py index 6ef32c1..8231f28 100644 --- a/torchreid/datasets/cuhk01.py +++ b/torchreid/datasets/cuhk01.py @@ -48,7 +48,7 @@ class CUHK01(BaseImageDataset): self._prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): - raise ValueError("split_id exceeds range, received {}, but expected between 0 and {}".format(split_id, len(splits)-1)) + raise ValueError('split_id exceeds range, received {}, but expected between 0 and {}'.format(split_id, len(splits)-1)) split = splits[split_id] train = split['train'] @@ -60,7 +60,7 @@ class CUHK01(BaseImageDataset): gallery = [tuple(item) for item in gallery] if verbose: - print("=> CUHK01 loaded") + print('=> CUHK01 loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -73,18 +73,18 @@ class CUHK01(BaseImageDataset): def _extract_file(self): if not osp.exists(self.campus_dir): - print("Extracting files") + print('Extracting files') zip_ref = zipfile.ZipFile(self.zip_path, 'r') zip_ref.extractall(self.dataset_dir) zip_ref.close() - print("Files extracted") + print('Files extracted') 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)) + raise RuntimeError('"{}" is not available'.format(self.dataset_dir)) if not osp.exists(self.campus_dir): - raise RuntimeError("'{}' is not available".format(self.campus_dir)) + raise RuntimeError('"{}" is not available'.format(self.campus_dir)) def _prepare_split(self): """ @@ -93,7 +93,7 @@ class CUHK01(BaseImageDataset): view and camera 3&4 are considered the same view. """ if not osp.exists(self.split_path): - print("Creating 10 random splits of train ids and test ids") + print('Creating 10 random splits of train ids and test ids') img_paths = sorted(glob.glob(osp.join(self.campus_dir, '*.png'))) img_list = [] pid_container = set() @@ -142,8 +142,8 @@ class CUHK01(BaseImageDataset): } splits.append(split) - print("Totally {} splits are created".format(len(splits))) + print('Totally {} splits are created'.format(len(splits))) write_json(splits, self.split_path) - print("Split file saved to {}".format(self.split_path)) + print('Split file saved to {}'.format(self.split_path)) - print("Splits created") + print('Splits created') diff --git a/torchreid/datasets/cuhk03.py b/torchreid/datasets/cuhk03.py index 58f4f41..c1d75bd 100644 --- a/torchreid/datasets/cuhk03.py +++ b/torchreid/datasets/cuhk03.py @@ -69,16 +69,16 @@ class CUHK03(BaseImageDataset): split_path = self.split_classic_det_json_path if cuhk03_classic_split else self.split_new_det_json_path splits = read_json(split_path) - assert split_id < len(splits), "Condition split_id ({}) < len(splits) ({}) is false".format(split_id, len(splits)) + assert split_id < len(splits), 'Condition split_id ({}) < len(splits) ({}) is false'.format(split_id, len(splits)) split = splits[split_id] - print("Split index = {}".format(split_id)) + print('Split index = {}'.format(split_id)) train = split['train'] query = split['query'] gallery = split['gallery'] if verbose: - print("=> CUHK03 ({}) loaded".format(image_type)) + print('=> CUHK03 ({}) loaded'.format(image_type)) self.print_dataset_statistics(train, query, gallery) self.train = train @@ -92,15 +92,15 @@ class CUHK03(BaseImageDataset): 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)) + raise RuntimeError('"{}" is not available'.format(self.dataset_dir)) if not osp.exists(self.data_dir): - raise RuntimeError("'{}' is not available".format(self.data_dir)) + raise RuntimeError('"{}" is not available'.format(self.data_dir)) if not osp.exists(self.raw_mat_path): - raise RuntimeError("'{}' is not available".format(self.raw_mat_path)) + raise RuntimeError('"{}" is not available'.format(self.raw_mat_path)) if not osp.exists(self.split_new_det_mat_path): - raise RuntimeError("'{}' is not available".format(self.split_new_det_mat_path)) + raise RuntimeError('"{}" is not available'.format(self.split_new_det_mat_path)) if not osp.exists(self.split_new_lab_mat_path): - raise RuntimeError("'{}' is not available".format(self.split_new_lab_mat_path)) + raise RuntimeError('"{}" is not available'.format(self.split_new_lab_mat_path)) def _preprocess(self): """ @@ -109,7 +109,7 @@ class CUHK03(BaseImageDataset): 2. Create 20 classic splits. (Li et al. CVPR'14) 3. Create new split. (Zhong et al. CVPR'17) """ - print("Note: if root path is changed, the previously generated json files need to be re-generated (delete them first)") + print('Note: if root path is changed, the previously generated json files need to be re-generated (delete them first)') if osp.exists(self.imgs_labeled_dir) and \ osp.exists(self.imgs_detected_dir) and \ osp.exists(self.split_classic_det_json_path) and \ @@ -121,7 +121,7 @@ class CUHK03(BaseImageDataset): mkdir_if_missing(self.imgs_detected_dir) mkdir_if_missing(self.imgs_labeled_dir) - print("Extract image data from {} and save as png".format(self.raw_mat_path)) + print('Extract image data from {} and save as png'.format(self.raw_mat_path)) mat = h5py.File(self.raw_mat_path, 'r') def _deref(ref): @@ -147,7 +147,7 @@ class CUHK03(BaseImageDataset): return img_paths def _extract_img(name): - print("Processing {} images (extract and save) ...".format(name)) + print('Processing {} images (extract and save) ...'.format(name)) meta_data = [] imgs_dir = self.imgs_detected_dir if name == 'detected' else self.imgs_labeled_dir for campid, camp_ref in enumerate(mat[name][0]): @@ -155,9 +155,9 @@ class CUHK03(BaseImageDataset): num_pids = camp.shape[0] for pid in range(num_pids): img_paths = _process_images(camp[pid,:], campid, pid, imgs_dir) - assert len(img_paths) > 0, "campid{}-pid{} has no images".format(campid, pid) + assert len(img_paths) > 0, 'campid{}-pid{} has no images'.format(campid, pid) meta_data.append((campid+1, pid+1, img_paths)) - print("- done camera pair {} with {} identities".format(campid+1, num_pids)) + print('- done camera pair {} with {} identities'.format(campid+1, num_pids)) return meta_data meta_detected = _extract_img('detected') @@ -183,7 +183,7 @@ class CUHK03(BaseImageDataset): num_train_imgs += len(img_paths) return train, num_train_pids, num_train_imgs, test, num_test_pids, num_test_imgs - print("Creating classic splits (# = 20) ...") + print('Creating classic splits (# = 20) ...') splits_classic_det, splits_classic_lab = [], [] for split_ref in mat['testsets'][0]: test_split = _deref(split_ref).tolist() @@ -237,7 +237,7 @@ class CUHK03(BaseImageDataset): gallery_info = _extract_set(filelist, pids, pid2label, gallery_idxs, img_dir, relabel=False) return train_info, query_info, gallery_info - print("Creating new splits for detected images (767/700) ...") + print('Creating new splits for detected images (767/700) ...') train_info, query_info, gallery_info = _extract_new_split( loadmat(self.split_new_det_mat_path), self.imgs_detected_dir, @@ -250,7 +250,7 @@ class CUHK03(BaseImageDataset): }] write_json(splits, self.split_new_det_json_path) - print("Creating new splits for labeled images (767/700) ...") + print('Creating new splits for labeled images (767/700) ...') train_info, query_info, gallery_info = _extract_new_split( loadmat(self.split_new_lab_mat_path), self.imgs_labeled_dir, diff --git a/torchreid/datasets/dukemtmcreid.py b/torchreid/datasets/dukemtmcreid.py index b505ef5..bbf16ce 100644 --- a/torchreid/datasets/dukemtmcreid.py +++ b/torchreid/datasets/dukemtmcreid.py @@ -52,7 +52,7 @@ class DukeMTMCreID(BaseImageDataset): gallery = self._process_dir(self.gallery_dir, relabel=False) if verbose: - print("=> DukeMTMC-reID loaded") + print('=> DukeMTMC-reID loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -65,17 +65,17 @@ class DukeMTMCreID(BaseImageDataset): def _download_data(self): if osp.exists(self.dataset_dir): - print("This dataset has been downloaded.") + print('This dataset has been downloaded.') return - print("Creating directory {}".format(self.dataset_dir)) + print('Creating directory {}'.format(self.dataset_dir)) mkdir_if_missing(self.dataset_dir) fpath = osp.join(self.dataset_dir, osp.basename(self.dataset_url)) - print("Downloading DukeMTMC-reID dataset") + print('Downloading DukeMTMC-reID dataset') urllib.urlretrieve(self.dataset_url, fpath) - print("Extracting files") + print('Extracting files') zip_ref = zipfile.ZipFile(fpath, 'r') zip_ref.extractall(self.dataset_dir) zip_ref.close() @@ -83,13 +83,13 @@ class DukeMTMCreID(BaseImageDataset): 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)) + 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)) + raise RuntimeError('"{}" is not available'.format(self.train_dir)) if not osp.exists(self.query_dir): - raise RuntimeError("'{}' is not available".format(self.query_dir)) + raise RuntimeError('"{}" is not available'.format(self.query_dir)) if not osp.exists(self.gallery_dir): - raise RuntimeError("'{}' is not available".format(self.gallery_dir)) + raise RuntimeError('"{}" is not available'.format(self.gallery_dir)) def _process_dir(self, dir_path, relabel=False): img_paths = glob.glob(osp.join(dir_path, '*.jpg')) diff --git a/torchreid/datasets/dukemtmcvidreid.py b/torchreid/datasets/dukemtmcvidreid.py index ee5152e..d15c979 100644 --- a/torchreid/datasets/dukemtmcvidreid.py +++ b/torchreid/datasets/dukemtmcvidreid.py @@ -49,14 +49,14 @@ class DukeMTMCVidReID(BaseVideoDataset): self.min_seq_len = min_seq_len self._download_data() self._check_before_run() - print("Note: if root path is changed, the previously generated json files need to be re-generated (so delete them first)") + print('Note: if root path is changed, the previously generated json files need to be re-generated (so delete them first)') train = self._process_dir(self.train_dir, self.split_train_json_path, relabel=True) query = self._process_dir(self.query_dir, self.split_query_json_path, relabel=False) gallery = self._process_dir(self.gallery_dir, self.split_gallery_json_path, relabel=False) if verbose: - print("=> DukeMTMC-VideoReID loaded") + print('=> DukeMTMC-VideoReID loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -69,17 +69,17 @@ class DukeMTMCVidReID(BaseVideoDataset): def _download_data(self): if osp.exists(self.dataset_dir): - print("This dataset has been downloaded.") + print('This dataset has been downloaded.') return - print("Creating directory {}".format(self.dataset_dir)) + print('Creating directory {}'.format(self.dataset_dir)) mkdir_if_missing(self.dataset_dir) fpath = osp.join(self.dataset_dir, osp.basename(self.dataset_url)) - print("Downloading DukeMTMC-VideoReID dataset") + print('Downloading DukeMTMC-VideoReID dataset') urllib.urlretrieve(self.dataset_url, fpath) - print("Extracting files") + print('Extracting files') zip_ref = zipfile.ZipFile(fpath, 'r') zip_ref.extractall(self.dataset_dir) zip_ref.close() @@ -87,23 +87,23 @@ class DukeMTMCVidReID(BaseVideoDataset): 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)) + 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)) + raise RuntimeError('"{}" is not available'.format(self.train_dir)) if not osp.exists(self.query_dir): - raise RuntimeError("'{}' is not available".format(self.query_dir)) + raise RuntimeError('"{}" is not available'.format(self.query_dir)) if not osp.exists(self.gallery_dir): - raise RuntimeError("'{}' is not available".format(self.gallery_dir)) + raise RuntimeError('"{}" is not available'.format(self.gallery_dir)) def _process_dir(self, dir_path, json_path, relabel): if osp.exists(json_path): - print("=> {} generated before, awesome!".format(json_path)) + print('=> {} generated before, awesome!'.format(json_path)) split = read_json(json_path) return split['tracklets'] - print("=> Automatically generating split (might take a while for the first time, have a coffe)") + print('=> Automatically generating split (might take a while for the first time, have a coffe)') pdirs = glob.glob(osp.join(dir_path, '*')) # avoid .DS_Store - print("Processing '{}' with {} person identities".format(dir_path, len(pdirs))) + print('Processing "{}" with {} person identities'.format(dir_path, len(pdirs))) pid_container = set() for pdir in pdirs: @@ -129,7 +129,7 @@ class DukeMTMCVidReID(BaseVideoDataset): img_idx_name = 'F' + str(img_idx+1).zfill(4) res = glob.glob(osp.join(tdir, '*' + img_idx_name + '*.jpg')) if len(res) == 0: - print("Warn: index name {} in {} is missing, jump to next".format(img_idx_name, tdir)) + print('Warn: index name {} in {} is missing, jump to next'.format(img_idx_name, tdir)) continue img_paths.append(res[0]) img_name = osp.basename(img_paths[0]) @@ -142,7 +142,7 @@ class DukeMTMCVidReID(BaseVideoDataset): img_paths = tuple(img_paths) tracklets.append((img_paths, pid, camid)) - print("Saving split to {}".format(json_path)) + print('Saving split to {}'.format(json_path)) split_dict = { 'tracklets': tracklets, } diff --git a/torchreid/datasets/grid.py b/torchreid/datasets/grid.py index b8fb2b4..b4441b4 100644 --- a/torchreid/datasets/grid.py +++ b/torchreid/datasets/grid.py @@ -50,7 +50,7 @@ class GRID(BaseImageDataset): self._prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): - raise ValueError("split_id exceeds range, received {}, but expected between 0 and {}".format(split_id, len(splits)-1)) + raise ValueError('split_id exceeds range, received {}, but expected between 0 and {}'.format(split_id, len(splits)-1)) split = splits[split_id] train = split['train'] @@ -62,7 +62,7 @@ class GRID(BaseImageDataset): gallery = [tuple(item) for item in gallery] if verbose: - print("=> GRID loaded") + print('=> GRID loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -76,34 +76,34 @@ class GRID(BaseImageDataset): 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)) + raise RuntimeError('"{}" is not available'.format(self.dataset_dir)) if not osp.exists(self.probe_path): - raise RuntimeError("'{}' is not available".format(self.probe_path)) + raise RuntimeError('"{}" is not available'.format(self.probe_path)) if not osp.exists(self.gallery_path): - raise RuntimeError("'{}' is not available".format(self.gallery_path)) + raise RuntimeError('"{}" is not available'.format(self.gallery_path)) if not osp.exists(self.split_mat_path): - raise RuntimeError("'{}' is not available".format(self.split_mat_path)) + raise RuntimeError('"{}" is not available'.format(self.split_mat_path)) def _download_data(self): if osp.exists(self.dataset_dir): - print("This dataset has been downloaded.") + print('This dataset has been downloaded.') return - print("Creating directory {}".format(self.dataset_dir)) + print('Creating directory {}'.format(self.dataset_dir)) mkdir_if_missing(self.dataset_dir) fpath = osp.join(self.dataset_dir, osp.basename(self.dataset_url)) - print("Downloading GRID dataset") + print('Downloading GRID dataset') urllib.urlretrieve(self.dataset_url, fpath) - print("Extracting files") + print('Extracting files') zip_ref = zipfile.ZipFile(fpath, 'r') zip_ref.extractall(self.dataset_dir) zip_ref.close() def _prepare_split(self): if not osp.exists(self.split_path): - print("Creating 10 random splits") + print('Creating 10 random splits') split_mat = loadmat(self.split_mat_path) trainIdxAll = split_mat['trainIdxAll'][0] # length = 10 probe_img_paths = sorted(glob.glob(osp.join(self.probe_path, '*.jpeg'))) @@ -148,8 +148,8 @@ class GRID(BaseImageDataset): } splits.append(split) - print("Totally {} splits are created".format(len(splits))) + print('Totally {} splits are created'.format(len(splits))) write_json(splits, self.split_path) - print("Split file saved to {}".format(self.split_path)) + print('Split file saved to {}'.format(self.split_path)) - print("Splits created") + print('Splits created') diff --git a/torchreid/datasets/ilids.py b/torchreid/datasets/ilids.py index 2f5aeec..f3beb5f 100644 --- a/torchreid/datasets/ilids.py +++ b/torchreid/datasets/ilids.py @@ -52,17 +52,17 @@ class iLIDS(BaseImageDataset): self._prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): - raise ValueError("split_id exceeds range, received {}, but expected between 0 and {}".format(split_id, len(splits)-1)) + raise ValueError('split_id exceeds range, received {}, but expected between 0 and {}'.format(split_id, len(splits)-1)) split = splits[split_id] train_dirs, test_dirs = split['train'], split['test'] - print("# train identites: {}, # test identites {}".format(len(train_dirs), len(test_dirs))) + print('# train identites: {}, # test identites {}'.format(len(train_dirs), len(test_dirs))) train = self._process_data(train_dirs, cam1=True, cam2=True) query = self._process_data(test_dirs, cam1=True, cam2=False) gallery = self._process_data(test_dirs, cam1=False, cam2=True) if verbose: - print("=> iLIDS (single-shot) loaded") + print('=> iLIDS (single-shot) loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -75,16 +75,16 @@ class iLIDS(BaseImageDataset): def _download_data(self): if osp.exists(self.dataset_dir): - print("This dataset has been downloaded.") + print('This dataset has been downloaded.') return mkdir_if_missing(self.dataset_dir) fpath = osp.join(self.dataset_dir, osp.basename(self.dataset_url)) - print("Downloading iLIDS-VID dataset") + print('Downloading iLIDS-VID dataset') urllib.urlretrieve(self.dataset_url, fpath) - print("Extracting files") + print('Extracting files') tar = tarfile.open(fpath) tar.extractall(path=self.dataset_dir) tar.close() @@ -92,15 +92,15 @@ class iLIDS(BaseImageDataset): 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)) + raise RuntimeError('"{}" is not available'.format(self.dataset_dir)) if not osp.exists(self.data_dir): - raise RuntimeError("'{}' is not available".format(self.data_dir)) + raise RuntimeError('"{}" is not available'.format(self.data_dir)) if not osp.exists(self.split_dir): - raise RuntimeError("'{}' is not available".format(self.split_dir)) + raise RuntimeError('"{}" is not available'.format(self.split_dir)) def _prepare_split(self): if not osp.exists(self.split_path): - print("Creating splits ...") + print('Creating splits ...') mat_split_data = loadmat(self.split_mat_path)['ls_set'] num_splits = mat_split_data.shape[0] @@ -136,8 +136,8 @@ class iLIDS(BaseImageDataset): split = {'train': train_dirs, 'test': test_dirs} splits.append(split) - print("Totally {} splits are created, following Wang et al. ECCV'14".format(len(splits))) - print("Split file is saved to {}".format(self.split_path)) + print('Totally {} splits are created, following Wang et al. ECCV\'14'.format(len(splits))) + print('Split file is saved to {}'.format(self.split_path)) write_json(splits, self.split_path) def _process_data(self, dirnames, cam1=True, cam2=True): diff --git a/torchreid/datasets/ilidsvid.py b/torchreid/datasets/ilidsvid.py index 859f822..f7f7f98 100644 --- a/torchreid/datasets/ilidsvid.py +++ b/torchreid/datasets/ilidsvid.py @@ -52,17 +52,17 @@ class iLIDSVID(BaseVideoDataset): self._prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): - raise ValueError("split_id exceeds range, received {}, but expected between 0 and {}".format(split_id, len(splits)-1)) + raise ValueError('split_id exceeds range, received {}, but expected between 0 and {}'.format(split_id, len(splits)-1)) split = splits[split_id] train_dirs, test_dirs = split['train'], split['test'] - print("# train identites: {}, # test identites {}".format(len(train_dirs), len(test_dirs))) + print('# train identites: {}, # test identites {}'.format(len(train_dirs), len(test_dirs))) train = self._process_data(train_dirs, cam1=True, cam2=True) query = self._process_data(test_dirs, cam1=True, cam2=False) gallery = self._process_data(test_dirs, cam1=False, cam2=True) if verbose: - print("=> iLIDS-VID loaded") + print('=> iLIDS-VID loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -75,16 +75,16 @@ class iLIDSVID(BaseVideoDataset): def _download_data(self): if osp.exists(self.dataset_dir): - print("This dataset has been downloaded.") + print('This dataset has been downloaded.') return mkdir_if_missing(self.dataset_dir) fpath = osp.join(self.dataset_dir, osp.basename(self.dataset_url)) - print("Downloading iLIDS-VID dataset") + print('Downloading iLIDS-VID dataset') urllib.urlretrieve(self.dataset_url, fpath) - print("Extracting files") + print('Extracting files') tar = tarfile.open(fpath) tar.extractall(path=self.dataset_dir) tar.close() @@ -92,15 +92,15 @@ class iLIDSVID(BaseVideoDataset): 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)) + raise RuntimeError('"{}" is not available'.format(self.dataset_dir)) if not osp.exists(self.data_dir): - raise RuntimeError("'{}' is not available".format(self.data_dir)) + raise RuntimeError('"{}" is not available'.format(self.data_dir)) if not osp.exists(self.split_dir): - raise RuntimeError("'{}' is not available".format(self.split_dir)) + raise RuntimeError('"{}" is not available'.format(self.split_dir)) def _prepare_split(self): if not osp.exists(self.split_path): - print("Creating splits ...") + print('Creating splits ...') mat_split_data = loadmat(self.split_mat_path)['ls_set'] num_splits = mat_split_data.shape[0] @@ -136,11 +136,11 @@ class iLIDSVID(BaseVideoDataset): split = {'train': train_dirs, 'test': test_dirs} splits.append(split) - print("Totally {} splits are created, following Wang et al. ECCV'14".format(len(splits))) - print("Split file is saved to {}".format(self.split_path)) + print('Totally {} splits are created, following Wang et al. ECCV\'14'.format(len(splits))) + print('Split file is saved to {}'.format(self.split_path)) write_json(splits, self.split_path) - print("Splits created") + print('Splits created') def _process_data(self, dirnames, cam1=True, cam2=True): tracklets = [] diff --git a/torchreid/datasets/market1501.py b/torchreid/datasets/market1501.py index a30c5e6..6e5c875 100644 --- a/torchreid/datasets/market1501.py +++ b/torchreid/datasets/market1501.py @@ -51,7 +51,7 @@ class Market1501(BaseImageDataset): gallery += self._process_dir(self.extra_gallery_dir, relabel=False) if verbose: - print("=> Market1501 loaded") + print('=> Market1501 loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -65,15 +65,15 @@ class Market1501(BaseImageDataset): 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)) + 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)) + raise RuntimeError('"{}" is not available'.format(self.train_dir)) if not osp.exists(self.query_dir): - raise RuntimeError("'{}' is not available".format(self.query_dir)) + raise RuntimeError('"{}" is not available'.format(self.query_dir)) if not osp.exists(self.gallery_dir): - raise RuntimeError("'{}' is not available".format(self.gallery_dir)) + raise RuntimeError('"{}" is not available'.format(self.gallery_dir)) if self.market1501_500k and not osp.exists(self.extra_gallery_dir): - raise RuntimeError("'{}' is not available".format(self.extra_gallery_dir)) + raise RuntimeError('"{}" is not available'.format(self.extra_gallery_dir)) def _process_dir(self, dir_path, relabel=False): img_paths = glob.glob(osp.join(dir_path, '*.jpg')) diff --git a/torchreid/datasets/mars.py b/torchreid/datasets/mars.py index eaec173..52f765d 100644 --- a/torchreid/datasets/mars.py +++ b/torchreid/datasets/mars.py @@ -61,7 +61,7 @@ class Mars(BaseVideoDataset): gallery = self._process_data(test_names, track_gallery, home_dir='bbox_test', relabel=False, min_seq_len=min_seq_len) if verbose: - print("=> MARS loaded") + print('=> MARS loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -75,17 +75,17 @@ class Mars(BaseVideoDataset): 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)) + raise RuntimeError('"{}" is not available'.format(self.dataset_dir)) if not osp.exists(self.train_name_path): - raise RuntimeError("'{}' is not available".format(self.train_name_path)) + raise RuntimeError('"{}" is not available'.format(self.train_name_path)) if not osp.exists(self.test_name_path): - raise RuntimeError("'{}' is not available".format(self.test_name_path)) + raise RuntimeError('"{}" is not available'.format(self.test_name_path)) if not osp.exists(self.track_train_info_path): - raise RuntimeError("'{}' is not available".format(self.track_train_info_path)) + raise RuntimeError('"{}" is not available'.format(self.track_train_info_path)) if not osp.exists(self.track_test_info_path): - raise RuntimeError("'{}' is not available".format(self.track_test_info_path)) + raise RuntimeError('"{}" is not available'.format(self.track_test_info_path)) if not osp.exists(self.query_IDX_path): - raise RuntimeError("'{}' is not available".format(self.query_IDX_path)) + raise RuntimeError('"{}" is not available'.format(self.query_IDX_path)) def _get_names(self, fpath): names = [] @@ -115,11 +115,11 @@ class Mars(BaseVideoDataset): # make sure image names correspond to the same person pnames = [img_name[:4] for img_name in img_names] - assert len(set(pnames)) == 1, "Error: a single tracklet contains different person images" + assert len(set(pnames)) == 1, 'Error: a single tracklet contains different person images' # make sure all images are captured under the same camera camnames = [img_name[5] for img_name in img_names] - assert len(set(camnames)) == 1, "Error: images are captured under different cameras!" + assert len(set(camnames)) == 1, 'Error: images are captured under different cameras!' # append image names with directory information img_paths = [osp.join(self.dataset_dir, home_dir, img_name[:4], img_name) for img_name in img_names] diff --git a/torchreid/datasets/msmt17.py b/torchreid/datasets/msmt17.py index 5b8cf43..0f1bdc9 100644 --- a/torchreid/datasets/msmt17.py +++ b/torchreid/datasets/msmt17.py @@ -61,7 +61,7 @@ class MSMT17(BaseImageDataset): test_dir = _VERSION[main_dir][_TEST_DIR_KEY] has_main_dir = True break - assert has_main_dir, "Dataset folder not found" + 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') @@ -80,7 +80,7 @@ class MSMT17(BaseImageDataset): #num_train_imgs += num_val_imgs if verbose: - print("=> MSMT17 loaded") + print('=> MSMT17 loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -94,11 +94,11 @@ class MSMT17(BaseImageDataset): 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)) + 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)) + 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)) + raise RuntimeError('"{}" is not available'.format(self.test_dir)) def _process_dir(self, dir_path, list_path): with open(list_path, 'r') as txt: @@ -115,5 +115,5 @@ class MSMT17(BaseImageDataset): 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" + assert idx == pid, 'See code comment for explanation' return dataset \ No newline at end of file diff --git a/torchreid/datasets/prid2011.py b/torchreid/datasets/prid2011.py index 87e74b5..e413200 100644 --- a/torchreid/datasets/prid2011.py +++ b/torchreid/datasets/prid2011.py @@ -45,17 +45,17 @@ class PRID2011(BaseVideoDataset): self._check_before_run() splits = read_json(self.split_path) if split_id >= len(splits): - raise ValueError("split_id exceeds range, received {}, but expected between 0 and {}".format(split_id, len(splits)-1)) + raise ValueError('split_id exceeds range, received {}, but expected between 0 and {}'.format(split_id, len(splits)-1)) split = splits[split_id] train_dirs, test_dirs = split['train'], split['test'] - print("# train identites: {}, # test identites {}".format(len(train_dirs), len(test_dirs))) + print('# train identites: {}, # test identites {}'.format(len(train_dirs), len(test_dirs))) train = self._process_data(train_dirs, cam1=True, cam2=True) query = self._process_data(test_dirs, cam1=True, cam2=False) gallery = self._process_data(test_dirs, cam1=False, cam2=True) if verbose: - print("=> PRID2011 loaded") + print('=> PRID2011 loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -69,7 +69,7 @@ class PRID2011(BaseVideoDataset): 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)) + raise RuntimeError('"{}" is not available'.format(self.dataset_dir)) def _process_data(self, dirnames, cam1=True, cam2=True): tracklets = [] diff --git a/torchreid/datasets/prid450s.py b/torchreid/datasets/prid450s.py index f4002f6..52dcb10 100644 --- a/torchreid/datasets/prid450s.py +++ b/torchreid/datasets/prid450s.py @@ -49,7 +49,7 @@ class PRID450S(BaseImageDataset): self._prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): - raise ValueError("split_id exceeds range, received {}, but expected between 0 and {}".format(split_id, len(splits)-1)) + raise ValueError('split_id exceeds range, received {}, but expected between 0 and {}'.format(split_id, len(splits)-1)) split = splits[split_id] train = split['train'] @@ -61,7 +61,7 @@ class PRID450S(BaseImageDataset): gallery = [tuple(item) for item in gallery] if verbose: - print("=> PRID450S loaded") + print('=> PRID450S loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -75,25 +75,25 @@ class PRID450S(BaseImageDataset): 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)) + raise RuntimeError('"{}" is not available'.format(self.dataset_dir)) if not osp.exists(self.cam_a_path): - raise RuntimeError("'{}' is not available".format(self.cam_a_path)) + raise RuntimeError('"{}" is not available'.format(self.cam_a_path)) if not osp.exists(self.cam_b_path): - raise RuntimeError("'{}' is not available".format(self.cam_b_path)) + raise RuntimeError('"{}" is not available'.format(self.cam_b_path)) def _download_data(self): if osp.exists(self.dataset_dir): - print("This dataset has been downloaded.") + print('This dataset has been downloaded.') return - print("Creating directory {}".format(self.dataset_dir)) + print('Creating directory {}'.format(self.dataset_dir)) mkdir_if_missing(self.dataset_dir) fpath = osp.join(self.dataset_dir, 'prid_450s.zip') - print("Downloading PRID450S dataset") + print('Downloading PRID450S dataset') urllib.urlretrieve(self.dataset_url, fpath) - print("Extracting files") + print('Extracting files') zip_ref = zipfile.ZipFile(fpath, 'r') zip_ref.extractall(self.dataset_dir) zip_ref.close() @@ -141,8 +141,8 @@ class PRID450S(BaseImageDataset): } splits.append(split) - print("Totally {} splits are created".format(len(splits))) + print('Totally {} splits are created'.format(len(splits))) write_json(splits, self.split_path) - print("Split file saved to {}".format(self.split_path)) + print('Split file saved to {}'.format(self.split_path)) - print("Splits created") + print('Splits created') diff --git a/torchreid/datasets/sensereid.py b/torchreid/datasets/sensereid.py index 994c270..834339c 100644 --- a/torchreid/datasets/sensereid.py +++ b/torchreid/datasets/sensereid.py @@ -50,7 +50,7 @@ class SenseReID(BaseImageDataset): gallery = self._process_dir(self.gallery_dir) if verbose: - print("=> SenseReID loaded (test only)") + print('=> SenseReID loaded (test only)') self.print_dataset_statistics(query, query, gallery) self.train = copy.deepcopy(query) # only used to initialize trainloader @@ -64,11 +64,11 @@ class SenseReID(BaseImageDataset): 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)) + raise RuntimeError('"{}" is not available'.format(self.dataset_dir)) if not osp.exists(self.query_dir): - raise RuntimeError("'{}' is not available".format(self.query_dir)) + raise RuntimeError('"{}" is not available'.format(self.query_dir)) if not osp.exists(self.gallery_dir): - raise RuntimeError("'{}' is not available".format(self.gallery_dir)) + raise RuntimeError('"{}" is not available'.format(self.gallery_dir)) def _process_dir(self, dir_path): img_paths = glob.glob(osp.join(dir_path, '*.jpg')) diff --git a/torchreid/datasets/viper.py b/torchreid/datasets/viper.py index cb841b3..05f1def 100755 --- a/torchreid/datasets/viper.py +++ b/torchreid/datasets/viper.py @@ -48,7 +48,7 @@ class VIPeR(BaseImageDataset): self._prepare_split() splits = read_json(self.split_path) if split_id >= len(splits): - raise ValueError("split_id exceeds range, received {}, but expected between 0 and {}".format(split_id, len(splits)-1)) + raise ValueError('split_id exceeds range, received {}, but expected between 0 and {}'.format(split_id, len(splits)-1)) split = splits[split_id] train = split['train'] @@ -60,7 +60,7 @@ class VIPeR(BaseImageDataset): gallery = [tuple(item) for item in gallery] if verbose: - print("=> VIPeR loaded") + print('=> VIPeR loaded') self.print_dataset_statistics(train, query, gallery) self.train = train @@ -73,17 +73,17 @@ class VIPeR(BaseImageDataset): def _download_data(self): if osp.exists(self.dataset_dir): - print("This dataset has been downloaded.") + print('This dataset has been downloaded.') return - print("Creating directory {}".format(self.dataset_dir)) + print('Creating directory {}'.format(self.dataset_dir)) mkdir_if_missing(self.dataset_dir) fpath = osp.join(self.dataset_dir, osp.basename(self.dataset_url)) - print("Downloading VIPeR dataset") + print('Downloading VIPeR dataset') urllib.urlretrieve(self.dataset_url, fpath) - print("Extracting files") + print('Extracting files') zip_ref = zipfile.ZipFile(fpath, 'r') zip_ref.extractall(self.dataset_dir) zip_ref.close() @@ -91,21 +91,21 @@ class VIPeR(BaseImageDataset): 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)) + raise RuntimeError('"{}" is not available'.format(self.dataset_dir)) if not osp.exists(self.cam_a_path): - raise RuntimeError("'{}' is not available".format(self.cam_a_path)) + raise RuntimeError('"{}" is not available'.format(self.cam_a_path)) if not osp.exists(self.cam_b_path): - raise RuntimeError("'{}' is not available".format(self.cam_b_path)) + raise RuntimeError('"{}" is not available'.format(self.cam_b_path)) def _prepare_split(self): if not osp.exists(self.split_path): - print("Creating 10 random splits of train ids and test ids") + print('Creating 10 random splits of train ids and test ids') cam_a_imgs = sorted(glob.glob(osp.join(self.cam_a_path, '*.bmp'))) cam_b_imgs = sorted(glob.glob(osp.join(self.cam_b_path, '*.bmp'))) assert len(cam_a_imgs) == len(cam_b_imgs) num_pids = len(cam_a_imgs) - print("Number of identities: {}".format(num_pids)) + print('Number of identities: {}'.format(num_pids)) num_train_pids = num_pids // 2 """ @@ -123,7 +123,7 @@ class VIPeR(BaseImageDataset): np.random.shuffle(order) train_idxs = order[:num_train_pids] test_idxs = order[num_train_pids:] - assert not bool(set(train_idxs) & set(test_idxs)), "Error: train and test overlap" + assert not bool(set(train_idxs) & set(test_idxs)), 'Error: train and test overlap' train = [] for pid, idx in enumerate(train_idxs): @@ -156,8 +156,8 @@ class VIPeR(BaseImageDataset): } splits.append(split) - print("Totally {} splits are created".format(len(splits))) + print('Totally {} splits are created'.format(len(splits))) write_json(splits, self.split_path) - print("Split file saved to {}".format(self.split_path)) + print('Split file saved to {}'.format(self.split_path)) - print("Splits created") + print('Splits created') diff --git a/torchreid/eval_cylib/eval_metrics_cy.pyx b/torchreid/eval_cylib/eval_metrics_cy.pyx index 89a5ab2..c545612 100644 --- a/torchreid/eval_cylib/eval_metrics_cy.pyx +++ b/torchreid/eval_cylib/eval_metrics_cy.pyx @@ -37,7 +37,7 @@ cpdef eval_cuhk03_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, if num_g < max_rank: max_rank = num_g - print("Note: number of gallery samples is quite small, got {}".format(num_g)) + print('Note: number of gallery samples is quite small, got {}'.format(num_g)) cdef: long num_repeats = 10 @@ -133,7 +133,7 @@ cpdef eval_cuhk03_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, all_cmc[q_idx, rank_idx] = cmc[rank_idx] num_valid_q += 1. - assert num_valid_q > 0, "Error: all query identities do not appear in gallery" + assert num_valid_q > 0, 'Error: all query identities do not appear in gallery' # compute averaged cmc cdef float[:] avg_cmc = np.zeros(max_rank, dtype=np.float32) @@ -158,7 +158,7 @@ cpdef eval_market1501_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, if num_g < max_rank: max_rank = num_g - print("Note: number of gallery samples is quite small, got {}".format(num_g)) + print('Note: number of gallery samples is quite small, got {}'.format(num_g)) cdef: long[:,:] indices = np.argsort(distmat, axis=1) @@ -223,7 +223,7 @@ cpdef eval_market1501_cy(float[:,:] distmat, long[:] q_pids, long[:]g_pids, num_rel += raw_cmc[g_idx] all_AP[q_idx] = tmp_cmc_sum / num_rel - assert num_valid_q > 0, "Error: all query identities do not appear in gallery" + assert num_valid_q > 0, 'Error: all query identities do not appear in gallery' # compute averaged cmc cdef float[:] avg_cmc = np.zeros(max_rank, dtype=np.float32) diff --git a/torchreid/eval_cylib/test_cython.py b/torchreid/eval_cylib/test_cython.py index 4b370bd..9ab88eb 100644 --- a/torchreid/eval_cylib/test_cython.py +++ b/torchreid/eval_cylib/test_cython.py @@ -19,7 +19,7 @@ Note: you might encounter the following error: This is normal because the inputs are random numbers. Just try again. """ -print("*** Compare running time ***") +print('*** Compare running time ***') setup = ''' import sys @@ -37,19 +37,19 @@ q_camids = np.random.randint(0, 5, size=num_q) g_camids = np.random.randint(0, 5, size=num_g) ''' -print("=> Using market1501's metric") +print('=> Using market1501\'s metric') pytime = timeit.timeit('evaluate(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_cython=False)', setup=setup, number=20) cytime = timeit.timeit('evaluate(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_cython=True)', setup=setup, number=20) -print("Python time: {} s".format(pytime)) -print("Cython time: {} s".format(cytime)) -print("Cython is {} times faster than python\n".format(pytime / cytime)) +print('Python time: {} s'.format(pytime)) +print('Cython time: {} s'.format(cytime)) +print('Cython is {} times faster than python\n'.format(pytime / cytime)) -print("=> Using cuhk03's metric") +print('=> Using cuhk03\'s metric') pytime = timeit.timeit('evaluate(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_metric_cuhk03=True, use_cython=False)', setup=setup, number=20) cytime = timeit.timeit('evaluate(distmat, q_pids, g_pids, q_camids, g_camids, max_rank, use_metric_cuhk03=True, use_cython=True)', setup=setup, number=20) -print("Python time: {} s".format(pytime)) -print("Cython time: {} s".format(cytime)) -print("Cython is {} times faster than python\n".format(pytime / cytime)) +print('Python time: {} s'.format(pytime)) +print('Cython time: {} s'.format(cytime)) +print('Cython is {} times faster than python\n'.format(pytime / cytime)) """ print("=> Check precision") diff --git a/torchreid/losses/center_loss.py b/torchreid/losses/center_loss.py index 8bf8378..7e8bd3f 100644 --- a/torchreid/losses/center_loss.py +++ b/torchreid/losses/center_loss.py @@ -19,7 +19,7 @@ class CenterLoss(nn.Module): """ def __init__(self, num_classes=10, feat_dim=2, use_gpu=True): super(CenterLoss, self).__init__() - warnings.warn("This method is deprecated") + warnings.warn('This method is deprecated') self.num_classes = num_classes self.feat_dim = feat_dim self.use_gpu = use_gpu diff --git a/torchreid/losses/ring_loss.py b/torchreid/losses/ring_loss.py index d56cc45..5568976 100644 --- a/torchreid/losses/ring_loss.py +++ b/torchreid/losses/ring_loss.py @@ -15,7 +15,7 @@ class RingLoss(nn.Module): """ def __init__(self): super(RingLoss, self).__init__() - warnings.warn("This method is deprecated") + warnings.warn('This method is deprecated') self.radius = nn.Parameter(torch.ones(1, dtype=torch.float)) def forward(self, x): diff --git a/torchreid/models/__init__.py b/torchreid/models/__init__.py index 9579ffb..51e7375 100644 --- a/torchreid/models/__init__.py +++ b/torchreid/models/__init__.py @@ -59,5 +59,5 @@ def get_names(): def init_model(name, *args, **kwargs): if name not in list(__model_factory.keys()): - raise KeyError("Unknown model: {}".format(name)) + raise KeyError('Unknown model: {}'.format(name)) return __model_factory[name](*args, **kwargs) \ No newline at end of file diff --git a/torchreid/models/densenet.py b/torchreid/models/densenet.py index 08902e4..cfb117b 100644 --- a/torchreid/models/densenet.py +++ b/torchreid/models/densenet.py @@ -119,7 +119,7 @@ class DenseNet(nn.Module): self.feature_dim = input_dim return None - assert isinstance(fc_dims, (list, tuple)), "fc_dims must be either list or tuple, but got {}".format(type(fc_dims)) + assert isinstance(fc_dims, (list, tuple)), 'fc_dims must be either list or tuple, but got {}'.format(type(fc_dims)) layers = [] for dim in fc_dims: @@ -170,7 +170,7 @@ class DenseNet(nn.Module): elif self.loss == {'xent', 'htri'}: return y, v else: - raise KeyError("Unsupported loss: {}".format(self.loss)) + raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): @@ -197,7 +197,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) """ diff --git a/torchreid/models/hacnn.py b/torchreid/models/hacnn.py index 7c45dc7..384aea8 100644 --- a/torchreid/models/hacnn.py +++ b/torchreid/models/hacnn.py @@ -280,7 +280,7 @@ class HACNN(nn.Module): def forward(self, x): assert x.size(2) == 160 and x.size(3) == 64, \ - "Input size does not match, expected (160, 64) but got ({}, {})".format(x.size(2), x.size(3)) + 'Input size does not match, expected (160, 64) but got ({}, {})'.format(x.size(2), x.size(3)) x = self.conv(x) # ============== Block 1 ============== diff --git a/torchreid/models/inceptionresnetv2.py b/torchreid/models/inceptionresnetv2.py index 380f23f..1968ee9 100644 --- a/torchreid/models/inceptionresnetv2.py +++ b/torchreid/models/inceptionresnetv2.py @@ -248,7 +248,7 @@ def inceptionresnetv2(num_classes=1000, pretrained='imagenet'): if pretrained: settings = pretrained_settings['inceptionresnetv2'][pretrained] assert num_classes == settings['num_classes'], \ - "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes) + 'num_classes should be {}, but is {}'.format(settings['num_classes'], num_classes) # both 'imagenet'&'imagenet+background' are loaded from same parameters model = InceptionResNetV2(num_classes=1001) @@ -357,7 +357,7 @@ class InceptionResNetV2(nn.Module): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) self.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(settings['url'])) + print('Initialized model with pretrained weights from {}'.format(settings['url'])) def featuremaps(self, x): x = self.conv2d_1a(x) @@ -392,4 +392,4 @@ class InceptionResNetV2(nn.Module): elif self.loss == {'xent', 'htri'}: return y, v else: - raise KeyError("Unsupported loss: {}".format(self.loss)) \ No newline at end of file + raise KeyError('Unsupported loss: {}'.format(self.loss)) \ No newline at end of file diff --git a/torchreid/models/inceptionv4.py b/torchreid/models/inceptionv4.py index afa08e6..29d5a9b 100644 --- a/torchreid/models/inceptionv4.py +++ b/torchreid/models/inceptionv4.py @@ -324,7 +324,7 @@ class InceptionV4Base(nn.Module): elif self.loss == {'xent', 'htri'}: return y, v else: - raise KeyError("Unsupported loss: {}".format(self.loss)) + raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): @@ -337,7 +337,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) def inceptionv4(num_classes, loss, pretrained='imagenet', **kwargs): diff --git a/torchreid/models/mlfn.py b/torchreid/models/mlfn.py index 320a568..498b0e3 100644 --- a/torchreid/models/mlfn.py +++ b/torchreid/models/mlfn.py @@ -187,7 +187,7 @@ class MLFN(nn.Module): elif self.loss == {'xent', 'htri'}: return y, v else: - raise KeyError("Unsupported loss: {}".format(self.loss)) + raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): @@ -200,7 +200,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) def mlfn(num_classes, loss, pretrained='imagenet', **kwargs): diff --git a/torchreid/models/mobilenetv2.py b/torchreid/models/mobilenetv2.py index 6f605f0..68cf3bf 100644 --- a/torchreid/models/mobilenetv2.py +++ b/torchreid/models/mobilenetv2.py @@ -125,4 +125,4 @@ class MobileNetV2(nn.Module): elif self.loss == {'xent', 'htri'}: return y, x else: - raise KeyError("Unsupported loss: {}".format(self.loss)) \ No newline at end of file + raise KeyError('Unsupported loss: {}'.format(self.loss)) \ No newline at end of file diff --git a/torchreid/models/mudeep.py b/torchreid/models/mudeep.py index 8bc2193..d571664 100644 --- a/torchreid/models/mudeep.py +++ b/torchreid/models/mudeep.py @@ -193,4 +193,4 @@ class MuDeep(nn.Module): elif self.loss == {'xent', 'htri'}: return y, x else: - raise KeyError("Unsupported loss: {}".format(self.loss)) \ No newline at end of file + raise KeyError('Unsupported loss: {}'.format(self.loss)) \ No newline at end of file diff --git a/torchreid/models/nasnet.py b/torchreid/models/nasnet.py index 8bdaea5..ebdab49 100644 --- a/torchreid/models/nasnet.py +++ b/torchreid/models/nasnet.py @@ -652,7 +652,7 @@ class NASNetAMobile(nn.Module): elif self.loss == {'xent', 'htri'}: return y, v else: - raise KeyError("Unsupported loss: {}".format(self.loss)) + raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): @@ -665,7 +665,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) def nasnetamobile(num_classes, loss, pretrained='imagenet', **kwargs): diff --git a/torchreid/models/pcb.py b/torchreid/models/pcb.py index c781d7c..fd841ab 100644 --- a/torchreid/models/pcb.py +++ b/torchreid/models/pcb.py @@ -222,7 +222,7 @@ class PCB(nn.Module): v_g = F.normalize(v_g, p=2, dim=1) return y, v_g.view(v_g.size(0), -1) else: - raise KeyError("Unsupported loss: {}".format(self.loss)) + raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): @@ -235,7 +235,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) def pcb_p6(num_classes, loss, pretrained='imagenet', **kwargs): diff --git a/torchreid/models/resnet.py b/torchreid/models/resnet.py index 842429e..4ec6e76 100644 --- a/torchreid/models/resnet.py +++ b/torchreid/models/resnet.py @@ -160,7 +160,7 @@ class ResNet(nn.Module): self.feature_dim = input_dim return None - assert isinstance(fc_dims, (list, tuple)), "fc_dims must be either list or tuple, but got {}".format(type(fc_dims)) + assert isinstance(fc_dims, (list, tuple)), 'fc_dims must be either list or tuple, but got {}'.format(type(fc_dims)) layers = [] for dim in fc_dims: @@ -234,7 +234,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) """ diff --git a/torchreid/models/resnetmid.py b/torchreid/models/resnetmid.py index d48dcee..002d4fe 100644 --- a/torchreid/models/resnetmid.py +++ b/torchreid/models/resnetmid.py @@ -162,7 +162,7 @@ class ResNet(nn.Module): self.feature_dim = input_dim return None - assert isinstance(fc_dims, (list, tuple)), "fc_dims must be either list or tuple, but got {}".format(type(fc_dims)) + assert isinstance(fc_dims, (list, tuple)), 'fc_dims must be either list or tuple, but got {}'.format(type(fc_dims)) layers = [] for dim in fc_dims: @@ -229,7 +229,7 @@ class ResNet(nn.Module): elif self.loss == {'xent', 'htri'}: return y, v else: - raise KeyError("Unsupported loss: {}".format(self.loss)) + raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): @@ -242,7 +242,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) """ diff --git a/torchreid/models/resnext.py b/torchreid/models/resnext.py index 3e5ab65..f28081c 100644 --- a/torchreid/models/resnext.py +++ b/torchreid/models/resnext.py @@ -123,7 +123,7 @@ class ResNeXt(nn.Module): self.feature_dim = input_dim return None - assert isinstance(fc_dims, (list, tuple)), "fc_dims must be either list or tuple, but got {}".format(type(fc_dims)) + assert isinstance(fc_dims, (list, tuple)), 'fc_dims must be either list or tuple, but got {}'.format(type(fc_dims)) layers = [] for dim in fc_dims: @@ -197,7 +197,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) def resnext50_32x4d(num_classes, loss, pretrained='imagenet', **kwargs): diff --git a/torchreid/models/senet.py b/torchreid/models/senet.py index 028df14..4807d1c 100644 --- a/torchreid/models/senet.py +++ b/torchreid/models/senet.py @@ -370,7 +370,7 @@ class SENet(nn.Module): self.feature_dim = input_dim return None - assert isinstance(fc_dims, (list, tuple)), "fc_dims must be either list or tuple, but got {}".format(type(fc_dims)) + assert isinstance(fc_dims, (list, tuple)), 'fc_dims must be either list or tuple, but got {}'.format(type(fc_dims)) layers = [] for dim in fc_dims: @@ -424,7 +424,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) def senet154(num_classes, loss, pretrained='imagenet', **kwargs): diff --git a/torchreid/models/shufflenet.py b/torchreid/models/shufflenet.py index a989726..e0a2856 100644 --- a/torchreid/models/shufflenet.py +++ b/torchreid/models/shufflenet.py @@ -37,7 +37,7 @@ class ChannelShuffle(nn.Module): class Bottleneck(nn.Module): def __init__(self, in_channels, out_channels, stride, num_groups, group_conv1x1=True): super(Bottleneck, self).__init__() - assert stride in [1, 2], "Warning: stride must be either 1 or 2" + assert stride in [1, 2], 'Warning: stride must be either 1 or 2' self.stride = stride mid_channels = out_channels // 4 if stride == 2: out_channels -= in_channels @@ -139,7 +139,7 @@ class ShuffleNet(nn.Module): elif self.loss == {'xent', 'htri'}: return y, x else: - raise KeyError("Unsupported loss: {}".format(self.loss)) + raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): @@ -152,7 +152,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) def shufflenet(num_classes, loss, pretrained='imagenet', **kwargs): diff --git a/torchreid/models/squeezenet.py b/torchreid/models/squeezenet.py index 9a8c591..0851618 100644 --- a/torchreid/models/squeezenet.py +++ b/torchreid/models/squeezenet.py @@ -59,8 +59,8 @@ class SqueezeNet(nn.Module): self.feature_dim = 512 if version not in [1.0, 1.1]: - raise ValueError("Unsupported SqueezeNet version {version}:" - "1.0 or 1.1 expected".format(version=version)) + raise ValueError('Unsupported SqueezeNet version {version}:' + '1.0 or 1.1 expected'.format(version=version)) if version == 1.0: self.features = nn.Sequential( @@ -114,7 +114,7 @@ class SqueezeNet(nn.Module): self.feature_dim = input_dim return None - assert isinstance(fc_dims, (list, tuple)), "fc_dims must be either list or tuple, but got {}".format(type(fc_dims)) + assert isinstance(fc_dims, (list, tuple)), 'fc_dims must be either list or tuple, but got {}'.format(type(fc_dims)) layers = [] for dim in fc_dims: @@ -164,7 +164,7 @@ class SqueezeNet(nn.Module): elif self.loss == {'xent', 'htri'}: return y, v else: - raise KeyError("Unsupported loss: {}".format(self.loss)) + raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): @@ -177,7 +177,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) def squeezenet1_0(num_classes, loss, pretrained=True, **kwargs): diff --git a/torchreid/models/xception.py b/torchreid/models/xception.py index e171412..d156e4f 100644 --- a/torchreid/models/xception.py +++ b/torchreid/models/xception.py @@ -154,7 +154,7 @@ class Xception(nn.Module): self.feature_dim = input_dim return None - assert isinstance(fc_dims, (list, tuple)), "fc_dims must be either list or tuple, but got {}".format(type(fc_dims)) + assert isinstance(fc_dims, (list, tuple)), 'fc_dims must be either list or tuple, but got {}'.format(type(fc_dims)) layers = [] for dim in fc_dims: @@ -235,7 +235,7 @@ class Xception(nn.Module): elif self.loss == {'xent', 'htri'}: return y, v else: - raise KeyError("Unsupported loss: {}".format(self.loss)) + raise KeyError('Unsupported loss: {}'.format(self.loss)) def init_pretrained_weights(model, model_url): @@ -248,7 +248,7 @@ def init_pretrained_weights(model, model_url): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Initialized model with pretrained weights from {}".format(model_url)) + print('Initialized model with pretrained weights from {}'.format(model_url)) def xception(num_classes, loss, pretrained='imagenet', **kwargs): diff --git a/torchreid/optimizers.py b/torchreid/optimizers.py index b3e6793..4edf5d5 100644 --- a/torchreid/optimizers.py +++ b/torchreid/optimizers.py @@ -31,4 +31,4 @@ def init_optimizer(params, alpha=rmsprop_alpha) else: - raise ValueError("Unsupported optimizer: {}".format(optim)) \ No newline at end of file + raise ValueError('Unsupported optimizer: {}'.format(optim)) \ No newline at end of file diff --git a/torchreid/utils/iotools.py b/torchreid/utils/iotools.py index 9151afc..4f6c85e 100644 --- a/torchreid/utils/iotools.py +++ b/torchreid/utils/iotools.py @@ -21,7 +21,7 @@ def mkdir_if_missing(directory): def check_isfile(path): isfile = osp.isfile(path) if not isfile: - print("=> Warning: no file found at '{}' (ignored)".format(path)) + print('=> Warning: no file found at "{}" (ignored)'.format(path)) return isfile diff --git a/torchreid/utils/loggers.py b/torchreid/utils/loggers.py index f0c984e..d15c055 100644 --- a/torchreid/utils/loggers.py +++ b/torchreid/utils/loggers.py @@ -65,9 +65,9 @@ class RankLogger(object): self.logger[name]['rank1'].append(rank1) def show_summary(self): - print("=> Show summary") + print('=> Show summary') for name in self.target_names: from_where = 'source' if name in self.source_names else 'target' - print("{} ({})".format(name, from_where)) + print('{} ({})'.format(name, from_where)) for epoch, rank1 in zip(self.logger[name]['epoch'], self.logger[name]['rank1']): - print("- epoch {}\t rank1 {:.1%}".format(epoch, rank1)) \ No newline at end of file + print('- epoch {}\t rank1 {:.1%}'.format(epoch, rank1)) \ No newline at end of file diff --git a/torchreid/utils/reidtools.py b/torchreid/utils/reidtools.py index 4dd5b84..46ed33b 100644 --- a/torchreid/utils/reidtools.py +++ b/torchreid/utils/reidtools.py @@ -25,9 +25,9 @@ def visualize_ranked_results(distmat, dataset, save_dir='log/ranked_results', to """ num_q, num_g = distmat.shape - print("Visualizing top-{} ranks".format(topk)) - print("# query: {}\n# gallery {}".format(num_q, num_g)) - print("Saving images to '{}'".format(save_dir)) + print('Visualizing top-{} ranks'.format(topk)) + print('# query: {}\n# gallery {}'.format(num_q, num_g)) + print('Saving images to "{}"'.format(save_dir)) query, gallery = dataset assert num_q == len(query) diff --git a/torchreid/utils/torchtools.py b/torchreid/utils/torchtools.py index bf7a756..a9bf96a 100644 --- a/torchreid/utils/torchtools.py +++ b/torchreid/utils/torchtools.py @@ -53,7 +53,7 @@ def open_specified_layers(model, open_layers): model = model.module for layer in open_layers: - assert hasattr(model, layer), "'{}' is not an attribute of the model, please provide the correct name".format(layer) + assert hasattr(model, layer), '"{}" is not an attribute of the model, please provide the correct name'.format(layer) for name, module in model.named_children(): if name in open_layers: diff --git a/train_imgreid_xent.py b/train_imgreid_xent.py index a3a51c7..8e16d9d 100755 --- a/train_imgreid_xent.py +++ b/train_imgreid_xent.py @@ -41,21 +41,21 @@ def main(): if args.use_cpu: use_gpu = False log_name = 'log_test.txt' if args.evaluate else 'log_train.txt' sys.stdout = Logger(osp.join(args.save_dir, log_name)) - print("==========\nArgs:{}\n==========".format(args)) + print('==========\nArgs:{}\n=========='.format(args)) if use_gpu: - print("Currently using GPU {}".format(args.gpu_devices)) + print('Currently using GPU {}'.format(args.gpu_devices)) cudnn.benchmark = True else: - print("Currently using CPU, however, GPU is highly recommended") + print('Currently using CPU, however, GPU is highly recommended') - print("Initializing image data manager") + print('Initializing image data manager') dm = ImageDataManager(use_gpu, **image_dataset_kwargs(args)) trainloader, testloader_dict = dm.return_dataloaders() - print("Initializing model: {}".format(args.arch)) + print('Initializing model: {}'.format(args.arch)) model = models.init_model(name=args.arch, num_classes=dm.num_train_pids, loss={'xent'}, use_gpu=use_gpu) - print("Model size: {:.3f} M".format(count_num_param(model))) + print('Model size: {:.3f} M'.format(count_num_param(model))) criterion = CrossEntropyLoss(num_classes=dm.num_train_pids, use_gpu=use_gpu, label_smooth=args.label_smooth) optimizer = init_optimizer(model.parameters(), **optimizer_kwargs(args)) @@ -69,23 +69,23 @@ def main(): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Loaded pretrained weights from '{}'".format(args.load_weights)) + print('Loaded pretrained weights from "{}"'.format(args.load_weights)) if args.resume and check_isfile(args.resume): checkpoint = torch.load(args.resume) model.load_state_dict(checkpoint['state_dict']) args.start_epoch = checkpoint['epoch'] + 1 - print("Loaded checkpoint from '{}'".format(args.resume)) - print("- start_epoch: {}\n- rank1: {}".format(args.start_epoch, checkpoint['rank1'])) + print('Loaded checkpoint from "{}"'.format(args.resume)) + print('- start_epoch: {}\n- rank1: {}'.format(args.start_epoch, checkpoint['rank1'])) if use_gpu: model = nn.DataParallel(model).cuda() if args.evaluate: - print("Evaluate only") + print('Evaluate only') for name in args.target_names: - print("Evaluating {} ...".format(name)) + print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] distmat = test(model, queryloader, galleryloader, use_gpu, return_distmat=True) @@ -101,10 +101,10 @@ def main(): start_time = time.time() ranklogger = RankLogger(args.source_names, args.target_names) train_time = 0 - print("=> Start training") + print('=> Start training') if args.fixbase_epoch > 0: - print("Train {} for {} epochs while keeping other layers frozen".format(args.open_layers, args.fixbase_epoch)) + print('Train {} for {} epochs while keeping other layers frozen'.format(args.open_layers, args.fixbase_epoch)) initial_optim_state = optimizer.state_dict() for epoch in range(args.fixbase_epoch): @@ -112,7 +112,7 @@ def main(): train(epoch, model, criterion, optimizer, trainloader, use_gpu, fixbase=True) train_time += round(time.time() - start_train_time) - print("Done. All layers are open to train for {} epochs".format(args.max_epoch)) + print('Done. All layers are open to train for {} epochs'.format(args.max_epoch)) optimizer.load_state_dict(initial_optim_state) for epoch in range(args.start_epoch, args.max_epoch): @@ -123,10 +123,10 @@ def main(): scheduler.step() if (epoch + 1) > args.start_eval and args.eval_freq > 0 and (epoch + 1) % args.eval_freq == 0 or (epoch + 1) == args.max_epoch: - print("=> Test") + print('=> Test') for name in args.target_names: - print("Evaluating {} ...".format(name)) + print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] rank1 = test(model, queryloader, galleryloader, use_gpu) @@ -146,7 +146,7 @@ def main(): elapsed = round(time.time() - start_time) elapsed = str(datetime.timedelta(seconds=elapsed)) train_time = str(datetime.timedelta(seconds=train_time)) - print("Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.".format(elapsed, train_time)) + print('Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.'.format(elapsed, train_time)) ranklogger.show_summary() @@ -222,7 +222,7 @@ def test(model, queryloader, galleryloader, use_gpu, ranks=[1, 5, 10, 20], retur q_pids = np.asarray(q_pids) q_camids = np.asarray(q_camids) - print("Extracted features for query set, obtained {}-by-{} matrix".format(qf.size(0), qf.size(1))) + print('Extracted features for query set, obtained {}-by-{} matrix'.format(qf.size(0), qf.size(1))) gf, g_pids, g_camids = [], [], [] end = time.time() @@ -241,9 +241,9 @@ def test(model, queryloader, galleryloader, use_gpu, ranks=[1, 5, 10, 20], retur g_pids = np.asarray(g_pids) g_camids = np.asarray(g_camids) - print("Extracted features for gallery set, obtained {}-by-{} matrix".format(gf.size(0), gf.size(1))) + print('Extracted features for gallery set, obtained {}-by-{} matrix'.format(gf.size(0), gf.size(1))) - print("=> BatchTime(s)/BatchSize(img): {:.3f}/{}".format(batch_time.avg, args.test_batch_size)) + print('=> BatchTime(s)/BatchSize(img): {:.3f}/{}'.format(batch_time.avg, args.test_batch_size)) m, n = qf.size(0), gf.size(0) distmat = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, n) + \ @@ -251,15 +251,15 @@ def test(model, queryloader, galleryloader, use_gpu, ranks=[1, 5, 10, 20], retur distmat.addmm_(1, -2, qf, gf.t()) distmat = distmat.numpy() - print("Computing CMC and mAP") + print('Computing CMC and mAP') cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids, use_metric_cuhk03=args.use_metric_cuhk03) - print("Results ----------") - print("mAP: {:.1%}".format(mAP)) - print("CMC curve") + print('Results ----------') + print('mAP: {:.1%}'.format(mAP)) + print('CMC curve') for r in ranks: - print("Rank-{:<3}: {:.1%}".format(r, cmc[r-1])) - print("------------------") + print('Rank-{:<3}: {:.1%}'.format(r, cmc[r-1])) + print('------------------') if return_distmat: return distmat diff --git a/train_imgreid_xent_htri.py b/train_imgreid_xent_htri.py index 5088270..039fadb 100755 --- a/train_imgreid_xent_htri.py +++ b/train_imgreid_xent_htri.py @@ -42,21 +42,21 @@ def main(): if args.use_cpu: use_gpu = False log_name = 'log_test.txt' if args.evaluate else 'log_train.txt' sys.stdout = Logger(osp.join(args.save_dir, log_name)) - print("==========\nArgs:{}\n==========".format(args)) + print('==========\nArgs:{}\n=========='.format(args)) if use_gpu: - print("Currently using GPU {}".format(args.gpu_devices)) + print('Currently using GPU {}'.format(args.gpu_devices)) cudnn.benchmark = True else: - print("Currently using CPU, however, GPU is highly recommended") + print('Currently using CPU, however, GPU is highly recommended') - print("Initializing image data manager") + print('Initializing image data manager') dm = ImageDataManager(use_gpu, **image_dataset_kwargs(args)) trainloader, testloader_dict = dm.return_dataloaders() - print("Initializing model: {}".format(args.arch)) + print('Initializing model: {}'.format(args.arch)) model = models.init_model(name=args.arch, num_classes=dm.num_train_pids, loss={'xent', 'htri'}) - print("Model size: {:.3f} M".format(count_num_param(model))) + print('Model size: {:.3f} M'.format(count_num_param(model))) criterion_xent = CrossEntropyLoss(num_classes=dm.num_train_pids, use_gpu=use_gpu, label_smooth=args.label_smooth) criterion_htri = TripletLoss(margin=args.margin) @@ -72,23 +72,23 @@ def main(): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Loaded pretrained weights from '{}'".format(args.load_weights)) + print('Loaded pretrained weights from "{}"'.format(args.load_weights)) if args.resume and check_isfile(args.resume): checkpoint = torch.load(args.resume) model.load_state_dict(checkpoint['state_dict']) args.start_epoch = checkpoint['epoch'] + 1 - print("Loaded checkpoint from '{}'".format(args.resume)) - print("- start_epoch: {}\n- rank1: {}".format(args.start_epoch, checkpoint['rank1'])) + print('Loaded checkpoint from "{}"'.format(args.resume)) + print('- start_epoch: {}\n- rank1: {}'.format(args.start_epoch, checkpoint['rank1'])) if use_gpu: model = nn.DataParallel(model).cuda() if args.evaluate: - print("Evaluate only") + print('Evaluate only') for name in args.target_names: - print("Evaluating {} ...".format(name)) + print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] distmat = test(model, queryloader, galleryloader, use_gpu, return_distmat=True) @@ -104,10 +104,10 @@ def main(): start_time = time.time() ranklogger = RankLogger(args.source_names, args.target_names) train_time = 0 - print("=> Start training") + print('=> Start training') if args.fixbase_epoch > 0: - print("Train {} for {} epochs while keeping other layers frozen".format(args.open_layers, args.fixbase_epoch)) + print('Train {} for {} epochs while keeping other layers frozen'.format(args.open_layers, args.fixbase_epoch)) initial_optim_state = optimizer.state_dict() for epoch in range(args.fixbase_epoch): @@ -115,7 +115,7 @@ def main(): train(epoch, model, criterion_xent, criterion_htri, optimizer, trainloader, use_gpu, fixbase=True) train_time += round(time.time() - start_train_time) - print("Done. All layers are open to train for {} epochs".format(args.max_epoch)) + print('Done. All layers are open to train for {} epochs'.format(args.max_epoch)) optimizer.load_state_dict(initial_optim_state) for epoch in range(args.start_epoch, args.max_epoch): @@ -126,10 +126,10 @@ def main(): scheduler.step() if (epoch + 1) > args.start_eval and args.eval_freq > 0 and (epoch + 1) % args.eval_freq == 0 or (epoch + 1) == args.max_epoch: - print("=> Test") + print('=> Test') for name in args.target_names: - print("Evaluating {} ...".format(name)) + print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] rank1 = test(model, queryloader, galleryloader, use_gpu) @@ -149,7 +149,7 @@ def main(): elapsed = round(time.time() - start_time) elapsed = str(datetime.timedelta(seconds=elapsed)) train_time = str(datetime.timedelta(seconds=train_time)) - print("Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.".format(elapsed, train_time)) + print('Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.'.format(elapsed, train_time)) ranklogger.show_summary() @@ -239,7 +239,7 @@ def test(model, queryloader, galleryloader, use_gpu, ranks=[1, 5, 10, 20], retur q_pids = np.asarray(q_pids) q_camids = np.asarray(q_camids) - print("Extracted features for query set, obtained {}-by-{} matrix".format(qf.size(0), qf.size(1))) + print('Extracted features for query set, obtained {}-by-{} matrix'.format(qf.size(0), qf.size(1))) gf, g_pids, g_camids = [], [], [] for batch_idx, (imgs, pids, camids, _) in enumerate(galleryloader): @@ -258,9 +258,9 @@ def test(model, queryloader, galleryloader, use_gpu, ranks=[1, 5, 10, 20], retur g_pids = np.asarray(g_pids) g_camids = np.asarray(g_camids) - print("Extracted features for gallery set, obtained {}-by-{} matrix".format(gf.size(0), gf.size(1))) + print('Extracted features for gallery set, obtained {}-by-{} matrix'.format(gf.size(0), gf.size(1))) - print("=> BatchTime(s)/BatchSize(img): {:.3f}/{}".format(batch_time.avg, args.test_batch_size)) + print('=> BatchTime(s)/BatchSize(img): {:.3f}/{}'.format(batch_time.avg, args.test_batch_size)) m, n = qf.size(0), gf.size(0) distmat = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, n) + \ @@ -268,15 +268,15 @@ def test(model, queryloader, galleryloader, use_gpu, ranks=[1, 5, 10, 20], retur distmat.addmm_(1, -2, qf, gf.t()) distmat = distmat.numpy() - print("Computing CMC and mAP") + print('Computing CMC and mAP') cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids, use_metric_cuhk03=args.use_metric_cuhk03) - print("Results ----------") - print("mAP: {:.1%}".format(mAP)) - print("CMC curve") + print('Results ----------') + print('mAP: {:.1%}'.format(mAP)) + print('CMC curve') for r in ranks: - print("Rank-{:<3}: {:.1%}".format(r, cmc[r-1])) - print("------------------") + print('Rank-{:<3}: {:.1%}'.format(r, cmc[r-1])) + print('------------------') if return_distmat: return distmat diff --git a/train_vidreid_xent.py b/train_vidreid_xent.py index b352363..2cd5ab9 100755 --- a/train_vidreid_xent.py +++ b/train_vidreid_xent.py @@ -42,21 +42,21 @@ def main(): if args.use_cpu: use_gpu = False log_name = 'log_test.txt' if args.evaluate else 'log_train.txt' sys.stdout = Logger(osp.join(args.save_dir, log_name)) - print("==========\nArgs:{}\n==========".format(args)) + print('==========\nArgs:{}\n=========='.format(args)) if use_gpu: - print("Currently using GPU {}".format(args.gpu_devices)) + print('Currently using GPU {}'.format(args.gpu_devices)) cudnn.benchmark = True else: - print("Currently using CPU, however, GPU is highly recommended") + print('Currently using CPU, however, GPU is highly recommended') - print("Initializing video data manager") + print('Initializing video data manager') dm = VideoDataManager(use_gpu, **video_dataset_kwargs(args)) trainloader, testloader_dict = dm.return_dataloaders() - print("Initializing model: {}".format(args.arch)) + print('Initializing model: {}'.format(args.arch)) model = models.init_model(name=args.arch, num_classes=dm.num_train_pids, loss={'xent'}) - print("Model size: {:.3f} M".format(count_num_param(model))) + print('Model size: {:.3f} M'.format(count_num_param(model))) criterion = CrossEntropyLoss(num_classes=dm.num_train_pids, use_gpu=use_gpu, label_smooth=args.label_smooth) optimizer = init_optimizer(model.parameters(), **optimizer_kwargs(args)) @@ -70,23 +70,23 @@ def main(): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Loaded pretrained weights from '{}'".format(args.load_weights)) + print('Loaded pretrained weights from "{}"'.format(args.load_weights)) if args.resume and check_isfile(args.resume): checkpoint = torch.load(args.resume) model.load_state_dict(checkpoint['state_dict']) args.start_epoch = checkpoint['epoch'] + 1 - print("Loaded checkpoint from '{}'".format(args.resume)) - print("- start_epoch: {}\n- rank1: {}".format(args.start_epoch, checkpoint['rank1'])) + print('Loaded checkpoint from "{}"'.format(args.resume)) + print('- start_epoch: {}\n- rank1: {}'.format(args.start_epoch, checkpoint['rank1'])) if use_gpu: model = nn.DataParallel(model).cuda() if args.evaluate: - print("Evaluate only") + print('Evaluate only') for name in args.target_names: - print("Evaluating {} ...".format(name)) + print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] distmat = test(model, queryloader, galleryloader, args.pool_tracklet_features, use_gpu, return_distmat=True) @@ -102,10 +102,10 @@ def main(): start_time = time.time() ranklogger = RankLogger(args.source_names, args.target_names) train_time = 0 - print("=> Start training") + print('=> Start training') if args.fixbase_epoch > 0: - print("Train {} for {} epochs while keeping other layers frozen".format(args.open_layers, args.fixbase_epoch)) + print('Train {} for {} epochs while keeping other layers frozen'.format(args.open_layers, args.fixbase_epoch)) initial_optim_state = optimizer.state_dict() for epoch in range(args.fixbase_epoch): @@ -113,7 +113,7 @@ def main(): train(epoch, model, criterion, optimizer, trainloader, use_gpu, fixbase=True) train_time += round(time.time() - start_train_time) - print("Done. All layers are open to train for {} epochs".format(args.max_epoch)) + print('Done. All layers are open to train for {} epochs'.format(args.max_epoch)) optimizer.load_state_dict(initial_optim_state) for epoch in range(args.start_epoch, args.max_epoch): @@ -124,10 +124,10 @@ def main(): scheduler.step() if (epoch + 1) > args.start_eval and args.eval_freq > 0 and (epoch + 1) % args.eval_freq == 0 or (epoch + 1) == args.max_epoch: - print("=> Test") + print('=> Test') for name in args.target_names: - print("Evaluating {} ...".format(name)) + print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] rank1 = test(model, queryloader, galleryloader, args.pool_tracklet_features, use_gpu) @@ -147,7 +147,7 @@ def main(): elapsed = round(time.time() - start_time) elapsed = str(datetime.timedelta(seconds=elapsed)) train_time = str(datetime.timedelta(seconds=train_time)) - print("Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.".format(elapsed, train_time)) + print('Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.'.format(elapsed, train_time)) ranklogger.show_summary() @@ -230,7 +230,7 @@ def test(model, queryloader, galleryloader, pool, use_gpu, ranks=[1, 5, 10, 20], q_pids = np.asarray(q_pids) q_camids = np.asarray(q_camids) - print("Extracted features for query set, obtained {}-by-{} matrix".format(qf.size(0), qf.size(1))) + print('Extracted features for query set, obtained {}-by-{} matrix'.format(qf.size(0), qf.size(1))) gf, g_pids, g_camids = [], [], [] for batch_idx, (imgs, pids, camids) in enumerate(galleryloader): @@ -255,9 +255,9 @@ def test(model, queryloader, galleryloader, pool, use_gpu, ranks=[1, 5, 10, 20], g_pids = np.asarray(g_pids) g_camids = np.asarray(g_camids) - print("Extracted features for gallery set, obtained {}-by-{} matrix".format(gf.size(0), gf.size(1))) + print('Extracted features for gallery set, obtained {}-by-{} matrix'.format(gf.size(0), gf.size(1))) - print("=> BatchTime(s)/BatchSize(img): {:.3f}/{}".format(batch_time.avg, args.test_batch_size * args.seq_len)) + print('=> BatchTime(s)/BatchSize(img): {:.3f}/{}'.format(batch_time.avg, args.test_batch_size * args.seq_len)) m, n = qf.size(0), gf.size(0) distmat = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, n) + \ @@ -265,15 +265,15 @@ def test(model, queryloader, galleryloader, pool, use_gpu, ranks=[1, 5, 10, 20], distmat.addmm_(1, -2, qf, gf.t()) distmat = distmat.numpy() - print("Computing CMC and mAP") + print('Computing CMC and mAP') cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids) - print("Results ----------") - print("mAP: {:.1%}".format(mAP)) - print("CMC curve") + print('Results ----------') + print('mAP: {:.1%}'.format(mAP)) + print('CMC curve') for r in ranks: - print("Rank-{:<3}: {:.1%}".format(r, cmc[r-1])) - print("------------------") + print('Rank-{:<3}: {:.1%}'.format(r, cmc[r-1])) + print('------------------') if return_distmat: return distmat diff --git a/train_vidreid_xent_htri.py b/train_vidreid_xent_htri.py index a9f65dc..477c790 100755 --- a/train_vidreid_xent_htri.py +++ b/train_vidreid_xent_htri.py @@ -43,21 +43,21 @@ def main(): if args.use_cpu: use_gpu = False log_name = 'log_test.txt' if args.evaluate else 'log_train.txt' sys.stdout = Logger(osp.join(args.save_dir, log_name)) - print("==========\nArgs:{}\n==========".format(args)) + print('==========\nArgs:{}\n=========='.format(args)) if use_gpu: - print("Currently using GPU {}".format(args.gpu_devices)) + print('Currently using GPU {}'.format(args.gpu_devices)) cudnn.benchmark = True else: - print("Currently using CPU, however, GPU is highly recommended") + print('Currently using CPU, however, GPU is highly recommended') - print("Initializing video data manager") + print('Initializing video data manager') dm = VideoDataManager(use_gpu, **video_dataset_kwargs(args)) trainloader, testloader_dict = dm.return_dataloaders() - print("Initializing model: {}".format(args.arch)) + print('Initializing model: {}'.format(args.arch)) model = models.init_model(name=args.arch, num_classes=dm.num_train_pids, loss={'xent', 'htri'}) - print("Model size: {:.3f} M".format(count_num_param(model))) + print('Model size: {:.3f} M'.format(count_num_param(model))) criterion = CrossEntropyLoss(num_classes=dm.num_train_pids, use_gpu=use_gpu, label_smooth=args.label_smooth) criterion_htri = TripletLoss(margin=args.margin) @@ -73,24 +73,24 @@ def main(): pretrain_dict = {k: v for k, v in pretrain_dict.items() if k in model_dict and model_dict[k].size() == v.size()} model_dict.update(pretrain_dict) model.load_state_dict(model_dict) - print("Loaded pretrained weights from '{}'".format(args.load_weights)) + print('Loaded pretrained weights from "{}"'.format(args.load_weights)) if args.resume and check_isfile(args.resume): checkpoint = torch.load(args.resume) model.load_state_dict(checkpoint['state_dict']) args.start_epoch = checkpoint['epoch'] + 1 best_rank1 = checkpoint['rank1'] - print("Loaded checkpoint from '{}'".format(args.resume)) - print("- start_epoch: {}\n- rank1: {}".format(args.start_epoch, best_rank1)) + print('Loaded checkpoint from "{}"'.format(args.resume)) + print('- start_epoch: {}\n- rank1: {}'.format(args.start_epoch, best_rank1)) if use_gpu: model = nn.DataParallel(model).cuda() if args.evaluate: - print("Evaluate only") + print('Evaluate only') for name in args.target_names: - print("Evaluating {} ...".format(name)) + print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] distmat = test(model, queryloader, galleryloader, args.pool_tracklet_features, use_gpu, return_distmat=True) @@ -106,10 +106,10 @@ def main(): start_time = time.time() ranklogger = RankLogger(args.source_names, args.target_names) train_time = 0 - print("=> Start training") + print('=> Start training') if args.fixbase_epoch > 0: - print("Train {} for {} epochs while keeping other layers frozen".format(args.open_layers, args.fixbase_epoch)) + print('Train {} for {} epochs while keeping other layers frozen'.format(args.open_layers, args.fixbase_epoch)) initial_optim_state = optimizer.state_dict() for epoch in range(args.fixbase_epoch): @@ -117,7 +117,7 @@ def main(): train(epoch, model, criterion_xent, criterion_htri, optimizer, trainloader, use_gpu, fixbase=True) train_time += round(time.time() - start_train_time) - print("Done. All layers are open to train for {} epochs".format(args.max_epoch)) + print('Done. All layers are open to train for {} epochs'.format(args.max_epoch)) optimizer.load_state_dict(initial_optim_state) for epoch in range(args.start_epoch, args.max_epoch): @@ -128,10 +128,10 @@ def main(): scheduler.step() if (epoch + 1) > args.start_eval and args.eval_freq > 0 and (epoch + 1) % args.eval_freq == 0 or (epoch + 1) == args.max_epoch: - print("=> Test") + print('=> Test') for name in args.target_names: - print("Evaluating {} ...".format(name)) + print('Evaluating {} ...'.format(name)) queryloader = testloader_dict[name]['query'] galleryloader = testloader_dict[name]['gallery'] rank1 = test(model, queryloader, galleryloader, args.pool_tracklet_features, use_gpu) @@ -151,7 +151,7 @@ def main(): elapsed = round(time.time() - start_time) elapsed = str(datetime.timedelta(seconds=elapsed)) train_time = str(datetime.timedelta(seconds=train_time)) - print("Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.".format(elapsed, train_time)) + print('Finished. Total elapsed time (h:m:s): {}. Training time (h:m:s): {}.'.format(elapsed, train_time)) ranklogger.show_summary() @@ -247,7 +247,7 @@ def test(model, queryloader, galleryloader, pool, use_gpu, ranks=[1, 5, 10, 20], q_pids = np.asarray(q_pids) q_camids = np.asarray(q_camids) - print("Extracted features for query set, obtained {}-by-{} matrix".format(qf.size(0), qf.size(1))) + print('Extracted features for query set, obtained {}-by-{} matrix'.format(qf.size(0), qf.size(1))) gf, g_pids, g_camids = [], [], [] for batch_idx, (imgs, pids, camids) in enumerate(galleryloader): @@ -272,9 +272,9 @@ def test(model, queryloader, galleryloader, pool, use_gpu, ranks=[1, 5, 10, 20], g_pids = np.asarray(g_pids) g_camids = np.asarray(g_camids) - print("Extracted features for gallery set, obtained {}-by-{} matrix".format(gf.size(0), gf.size(1))) + print('Extracted features for gallery set, obtained {}-by-{} matrix'.format(gf.size(0), gf.size(1))) - print("=> BatchTime(s)/BatchSize(img): {:.3f}/{}".format(batch_time.avg, args.test_batch_size * args.seq_len)) + print('=> BatchTime(s)/BatchSize(img): {:.3f}/{}'.format(batch_time.avg, args.test_batch_size * args.seq_len)) m, n = qf.size(0), gf.size(0) distmat = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, n) + \ @@ -282,15 +282,15 @@ def test(model, queryloader, galleryloader, pool, use_gpu, ranks=[1, 5, 10, 20], distmat.addmm_(1, -2, qf, gf.t()) distmat = distmat.numpy() - print("Computing CMC and mAP") + print('Computing CMC and mAP') cmc, mAP = evaluate(distmat, q_pids, g_pids, q_camids, g_camids) - print("Results ----------") - print("mAP: {:.1%}".format(mAP)) - print("CMC curve") + print('Results ----------') + print('mAP: {:.1%}'.format(mAP)) + print('CMC curve') for r in ranks: - print("Rank-{:<3}: {:.1%}".format(r, cmc[r-1])) - print("------------------") + print('Rank-{:<3}: {:.1%}'.format(r, cmc[r-1])) + print('------------------') if return_distmat: return distmat