From 5ce43185b156c19c60d19be97dfb5ad89423e52c Mon Sep 17 00:00:00 2001 From: KaiyangZhou Date: Tue, 5 May 2020 15:58:00 +0100 Subject: [PATCH] linting code --- .flake8 | 18 ++++++++++++++++-- docs/conf.py | 2 +- linter.sh | 10 +++++++++- projects/DML/dml.py | 3 --- projects/DML/main.py | 5 ++--- projects/OSNet_AIN/osnet_search.py | 18 ++++++++++++++++++ projects/OSNet_AIN/softmax_nas.py | 7 +------ scripts/main.py | 1 - setup.py | 3 +-- torchreid/data/datamanager.py | 2 +- torchreid/data/datasets/dataset.py | 6 +++--- torchreid/data/datasets/image/cuhk03.py | 6 ++---- torchreid/data/datasets/image/dukemtmcreid.py | 3 ++- torchreid/data/datasets/image/ilids.py | 2 +- torchreid/data/datasets/image/msmt17.py | 2 +- torchreid/data/datasets/image/viper.py | 6 ++---- torchreid/data/datasets/video/mars.py | 3 ++- torchreid/data/transforms.py | 13 ++++++++++--- torchreid/engine/engine.py | 3 +-- torchreid/engine/image/softmax.py | 2 -- torchreid/engine/image/triplet.py | 2 -- torchreid/engine/video/triplet.py | 4 ++-- torchreid/metrics/distance.py | 5 +++-- torchreid/models/hacnn.py | 3 ++- torchreid/models/inceptionresnetv2.py | 6 +++--- torchreid/models/mlfn.py | 2 +- torchreid/models/mobilenetv2.py | 4 ++-- torchreid/models/nasnet.py | 2 +- torchreid/models/shufflenet.py | 8 +++++--- torchreid/models/squeezenet.py | 1 - torchreid/utils/model_complexity.py | 4 ++-- torchreid/utils/rerank.py | 4 ++-- torchreid/utils/torchtools.py | 2 +- 33 files changed, 97 insertions(+), 65 deletions(-) diff --git a/.flake8 b/.flake8 index d953e4c..4fc103c 100644 --- a/.flake8 +++ b/.flake8 @@ -1,4 +1,18 @@ [flake8] -ignore = E261, E501, W293 +ignore = + # At least two spaces before inline comment + E261, + # Line lengths are recommended to be no greater than 79 characters + E501, + # Missing whitespace around arithmetic operator + E226, + # Blank line contains whitespace + W293, + # Do not use bare 'except' + E722, + # Line break after binary operator + W504, + # isort found an import in the wrong position + I001 max-line-length = 79 -exclude = __init__.py, build \ No newline at end of file +exclude = __init__.py, build, torchreid/metrics/rank_cylib/ \ No newline at end of file diff --git a/docs/conf.py b/docs/conf.py index 03c9ee5..4d27eed 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -57,7 +57,7 @@ templates_path = ['_templates'] # You can specify multiple suffix as a list of string: # source_suffix = ['.rst', '.md'] -#source_suffix = '.rst' +# source_suffix = '.rst' source_parsers = {'.md': 'recommonmark.parser.CommonMarkParser'} # The master toctree document. diff --git a/linter.sh b/linter.sh index e9eeca4..9db34f9 100644 --- a/linter.sh +++ b/linter.sh @@ -1,3 +1,11 @@ +echo "Running isort" isort -y -sp . +echo "Done" -yapf -i -r -vv . -e build \ No newline at end of file +echo "Running yapf" +yapf -i -r -vv -e build . +echo "Done" + +echo "Running flake8" +flake8 . +echo "Done" \ No newline at end of file diff --git a/projects/DML/dml.py b/projects/DML/dml.py index fe89567..546e573 100644 --- a/projects/DML/dml.py +++ b/projects/DML/dml.py @@ -1,10 +1,7 @@ from __future__ import division, print_function, absolute_import -import time -import datetime import torch from torch.nn import functional as F -from torchreid import metrics from torchreid.utils import open_all_layers, open_specified_layers from torchreid.engine import Engine from torchreid.losses import TripletLoss, CrossEntropyLoss diff --git a/projects/DML/main.py b/projects/DML/main.py index 9585b61..9af33d9 100755 --- a/projects/DML/main.py +++ b/projects/DML/main.py @@ -1,4 +1,3 @@ -import os import sys import copy import time @@ -15,8 +14,8 @@ from torchreid.utils import ( from dml import ImageDMLEngine from default_config import ( - imagedata_kwargs, optimizer_kwargs, videodata_kwargs, engine_run_kwargs, - get_default_config, lr_scheduler_kwargs + imagedata_kwargs, optimizer_kwargs, engine_run_kwargs, get_default_config, + lr_scheduler_kwargs ) diff --git a/projects/OSNet_AIN/osnet_search.py b/projects/OSNet_AIN/osnet_search.py index 1160152..a2a2cfa 100644 --- a/projects/OSNet_AIN/osnet_search.py +++ b/projects/OSNet_AIN/osnet_search.py @@ -10,6 +10,24 @@ NORM_AFFINE = False # enable affine transformations for normalization layer ########## # Basic layers ########## +class IBN(nn.Module): + """Instance + Batch Normalization.""" + + def __init__(self, num_channels): + super(IBN, self).__init__() + half1 = int(num_channels / 2) + self.half = half1 + half2 = num_channels - half1 + self.IN = nn.InstanceNorm2d(half1, affine=NORM_AFFINE) + self.BN = nn.BatchNorm2d(half2, affine=NORM_AFFINE) + + def forward(self, x): + split = torch.split(x, self.half, 1) + out1 = self.IN(split[0].contiguous()) + out2 = self.BN(split[1].contiguous()) + return torch.cat((out1, out2), 1) + + class ConvLayer(nn.Module): """Convolution layer (conv + bn + relu).""" diff --git a/projects/OSNet_AIN/softmax_nas.py b/projects/OSNet_AIN/softmax_nas.py index 005fab4..e2a03b6 100644 --- a/projects/OSNet_AIN/softmax_nas.py +++ b/projects/OSNet_AIN/softmax_nas.py @@ -1,11 +1,6 @@ from __future__ import division, print_function, absolute_import -import time -import datetime from torchreid import metrics -from torchreid.utils import ( - AverageMeter, open_all_layers, open_specified_layers -) from torchreid.engine import Engine from torchreid.losses import CrossEntropyLoss @@ -58,7 +53,7 @@ class ImageSoftmaxNASEngine(Engine): lmda = self.init_lmda else: lmda = self.init_lmda * self.lmda_decay_rate**( - epoch // self.lmda_decay_step + self.epoch // self.lmda_decay_step ) if lmda < self.min_lmda: lmda = self.min_lmda diff --git a/scripts/main.py b/scripts/main.py index 465ef66..05df742 100755 --- a/scripts/main.py +++ b/scripts/main.py @@ -1,4 +1,3 @@ -import os import sys import time import os.path as osp diff --git a/setup.py b/setup.py index ce0450e..a8ee83e 100644 --- a/setup.py +++ b/setup.py @@ -45,8 +45,7 @@ def get_requirements(filename='requirements.txt'): setup( name='torchreid', version=find_version(), - description= - 'A library for deep learning person re-identification in PyTorch', + description='A library for deep learning person re-ID in PyTorch', author='Kaiyang Zhou', license='MIT', long_description=readme(), diff --git a/torchreid/data/datamanager.py b/torchreid/data/datamanager.py index f7abeb8..c970f2f 100644 --- a/torchreid/data/datamanager.py +++ b/torchreid/data/datamanager.py @@ -70,7 +70,7 @@ class DataManager(object): """Returns the number of training cameras.""" return self._num_train_cams - def return_query_and_gallery_by_name(self, name): + def fetch_qg(self, name): """Returns query and gallery of a test dataset, each containing tuples of (img_path(s), pid, camid). diff --git a/torchreid/data/datasets/dataset.py b/torchreid/data/datasets/dataset.py index f95dc5c..666ca76 100644 --- a/torchreid/data/datasets/dataset.py +++ b/torchreid/data/datasets/dataset.py @@ -238,9 +238,9 @@ class Dataset(object): ' gallery | {:5d} | {:7d} | {:9d}\n' \ ' ----------------------------------------\n' \ ' items: images/tracklets for image/video dataset\n'.format( - num_train_pids, len(self.train), num_train_cams, - num_query_pids, len(self.query), num_query_cams, - num_gallery_pids, len(self.gallery), num_gallery_cams + num_train_pids, len(self.train), num_train_cams, + num_query_pids, len(self.query), num_query_cams, + num_gallery_pids, len(self.gallery), num_gallery_cams ) return msg diff --git a/torchreid/data/datasets/image/cuhk03.py b/torchreid/data/datasets/image/cuhk03.py index c800079..2543e06 100644 --- a/torchreid/data/datasets/image/cuhk03.py +++ b/torchreid/data/datasets/image/cuhk03.py @@ -151,10 +151,8 @@ class CUHK03(ImageDataset): 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( diff --git a/torchreid/data/datasets/image/dukemtmcreid.py b/torchreid/data/datasets/image/dukemtmcreid.py index 948069d..5915da5 100644 --- a/torchreid/data/datasets/image/dukemtmcreid.py +++ b/torchreid/data/datasets/image/dukemtmcreid.py @@ -61,7 +61,8 @@ class DukeMTMCreID(ImageDataset): pid, camid = map(int, pattern.search(img_path).groups()) assert 1 <= camid <= 8 camid -= 1 # index starts from 0 - if relabel: pid = pid2label[pid] + if relabel: + pid = pid2label[pid] data.append((img_path, pid, camid)) return data diff --git a/torchreid/data/datasets/image/ilids.py b/torchreid/data/datasets/image/ilids.py index 2213d23..42971b0 100644 --- a/torchreid/data/datasets/image/ilids.py +++ b/torchreid/data/datasets/image/ilids.py @@ -18,7 +18,7 @@ class iLIDS(ImageDataset): Dataset statistics: - identities: 119. - - images: 476. + - images: 476. - cameras: 8 (not explicitly provided). """ dataset_dir = 'ilids' diff --git a/torchreid/data/datasets/image/msmt17.py b/torchreid/data/datasets/image/msmt17.py index aa30f20..c4741e6 100644 --- a/torchreid/data/datasets/image/msmt17.py +++ b/torchreid/data/datasets/image/msmt17.py @@ -3,7 +3,7 @@ import os.path as osp from ..dataset import ImageDataset -##### Log ##### +# Log # 22.01.2019 # - add v2 # - v1 and v2 differ in dir names diff --git a/torchreid/data/datasets/image/viper.py b/torchreid/data/datasets/image/viper.py index e60500f..161dd99 100644 --- a/torchreid/data/datasets/image/viper.py +++ b/torchreid/data/datasets/image/viper.py @@ -83,10 +83,8 @@ class VIPeR(ImageDataset): 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): diff --git a/torchreid/data/datasets/video/mars.py b/torchreid/data/datasets/video/mars.py index e4aae68..4128e1c 100644 --- a/torchreid/data/datasets/video/mars.py +++ b/torchreid/data/datasets/video/mars.py @@ -98,7 +98,8 @@ class Mars(VideoDataset): if pid == -1: continue # junk images are just ignored assert 1 <= camid <= 6 - if relabel: pid = pid2label[pid] + if relabel: + pid = pid2label[pid] camid -= 1 # index starts from 0 img_names = names[start_index - 1:end_index] diff --git a/torchreid/data/transforms.py b/torchreid/data/transforms.py index 321c88c..f86c8c1 100644 --- a/torchreid/data/transforms.py +++ b/torchreid/data/transforms.py @@ -4,7 +4,9 @@ import random from collections import deque import torch from PIL import Image -from torchvision.transforms import * +from torchvision.transforms import ( + Resize, Compose, ToTensor, Normalize, ColorJitter, RandomHorizontalFlip +) class Random2DTranslation(object): @@ -279,8 +281,13 @@ def build_transforms( transform_tr += [RandomHorizontalFlip()] if 'random_crop' in transforms: - print('+ random crop (enlarge to {}x{} and ' \ - 'crop {}x{})'.format(int(round(height*1.125)), int(round(width*1.125)), height, width)) + print( + '+ random crop (enlarge to {}x{} and ' + 'crop {}x{})'.format( + int(round(height * 1.125)), int(round(width * 1.125)), height, + width + ) + ) transform_tr += [Random2DTranslation(height, width)] if 'random_patch' in transforms: diff --git a/torchreid/engine/engine.py b/torchreid/engine/engine.py index 61b82f2..6fdf95f 100644 --- a/torchreid/engine/engine.py +++ b/torchreid/engine/engine.py @@ -428,8 +428,7 @@ class Engine(object): if visrank: visualize_ranked_results( distmat, - self.datamanager. - return_query_and_gallery_by_name(dataset_name), + self.datamanager.fetch_qg(dataset_name), self.datamanager.data_type, width=self.datamanager.width, height=self.datamanager.height, diff --git a/torchreid/engine/image/softmax.py b/torchreid/engine/image/softmax.py index 58bac87..5785d4f 100644 --- a/torchreid/engine/image/softmax.py +++ b/torchreid/engine/image/softmax.py @@ -1,6 +1,4 @@ from __future__ import division, print_function, absolute_import -import time -import datetime from torchreid import metrics from torchreid.losses import CrossEntropyLoss diff --git a/torchreid/engine/image/triplet.py b/torchreid/engine/image/triplet.py index 3d73330..174d6d2 100644 --- a/torchreid/engine/image/triplet.py +++ b/torchreid/engine/image/triplet.py @@ -1,6 +1,4 @@ from __future__ import division, print_function, absolute_import -import time -import datetime from torchreid import metrics from torchreid.losses import TripletLoss, CrossEntropyLoss diff --git a/torchreid/engine/video/triplet.py b/torchreid/engine/video/triplet.py index 5e0ea35..5d43b8b 100644 --- a/torchreid/engine/video/triplet.py +++ b/torchreid/engine/video/triplet.py @@ -22,11 +22,11 @@ class VideoTripletEngine(ImageTripletEngine, VideoSoftmaxEngine): Default is "avg" (average). Choices are ["avg", "max"]. Examples:: - + import torch import torchreid # Each batch contains batch_size*seq_len images - # Each identity is sampled with num_instances tracklets + # Each identity is sampled with num_instances tracklets datamanager = torchreid.data.VideoDataManager( root='path/to/reid-data', sources='mars', diff --git a/torchreid/metrics/distance.py b/torchreid/metrics/distance.py index c5346ff..6278c34 100644 --- a/torchreid/metrics/distance.py +++ b/torchreid/metrics/distance.py @@ -57,8 +57,9 @@ def euclidean_squared_distance(input1, input2): torch.Tensor: distance matrix. """ m, n = input1.size(0), input2.size(0) - distmat = torch.pow(input1, 2).sum(dim=1, keepdim=True).expand(m, n) + \ - torch.pow(input2, 2).sum(dim=1, keepdim=True).expand(n, m).t() + mat1 = torch.pow(input1, 2).sum(dim=1, keepdim=True).expand(m, n) + mat2 = torch.pow(input2, 2).sum(dim=1, keepdim=True).expand(n, m).t() + distmat = mat1 + mat2 distmat.addmm_(1, -2, input1, input2.t()) return distmat diff --git a/torchreid/models/hacnn.py b/torchreid/models/hacnn.py index 3405157..f21cc82 100644 --- a/torchreid/models/hacnn.py +++ b/torchreid/models/hacnn.py @@ -300,7 +300,8 @@ class HACNN(nn.Module): theta = torch.zeros(theta_i.size(0), 2, 3) theta[:, :, :2] = scale_factors theta[:, :, -1] = theta_i - if self.use_gpu: theta = theta.cuda() + if self.use_gpu: + theta = theta.cuda() return theta def forward(self, x): diff --git a/torchreid/models/inceptionresnetv2.py b/torchreid/models/inceptionresnetv2.py index 2c79f09..03e4034 100644 --- a/torchreid/models/inceptionresnetv2.py +++ b/torchreid/models/inceptionresnetv2.py @@ -249,9 +249,9 @@ class Block8(nn.Module): return out -##################### Model Definition ######################### - - +# ---------------- +# Model Definition +# ---------------- class InceptionResNetV2(nn.Module): """Inception-ResNet-V2. diff --git a/torchreid/models/mlfn.py b/torchreid/models/mlfn.py index b289c9d..ac7e126 100644 --- a/torchreid/models/mlfn.py +++ b/torchreid/models/mlfn.py @@ -260,7 +260,7 @@ def init_pretrained_weights(model, model_url): def mlfn(num_classes, loss='softmax', pretrained=True, **kwargs): model = MLFN(num_classes, loss, **kwargs) if pretrained: - #init_pretrained_weights(model, model_urls['imagenet']) + # init_pretrained_weights(model, model_urls['imagenet']) import warnings warnings.warn( 'The imagenet pretrained weights need to be manually downloaded from {}' diff --git a/torchreid/models/mobilenetv2.py b/torchreid/models/mobilenetv2.py index 62a8822..c451ef8 100644 --- a/torchreid/models/mobilenetv2.py +++ b/torchreid/models/mobilenetv2.py @@ -246,7 +246,7 @@ def mobilenetv2_x1_0(num_classes, loss, pretrained=True, **kwargs): **kwargs ) if pretrained: - #init_pretrained_weights(model, model_urls['mobilenetv2_x1_0']) + # init_pretrained_weights(model, model_urls['mobilenetv2_x1_0']) import warnings warnings.warn( 'The imagenet pretrained weights need to be manually downloaded from {}' @@ -265,7 +265,7 @@ def mobilenetv2_x1_4(num_classes, loss, pretrained=True, **kwargs): **kwargs ) if pretrained: - #init_pretrained_weights(model, model_urls['mobilenetv2_x1_4']) + # init_pretrained_weights(model, model_urls['mobilenetv2_x1_4']) import warnings warnings.warn( 'The imagenet pretrained weights need to be manually downloaded from {}' diff --git a/torchreid/models/nasnet.py b/torchreid/models/nasnet.py index 3dca2db..b1f31de 100644 --- a/torchreid/models/nasnet.py +++ b/torchreid/models/nasnet.py @@ -26,7 +26,7 @@ Code imported from https://github.com/Cadene/pretrained-models.pytorch pretrained_settings = { 'nasnetamobile': { 'imagenet': { - #'url': 'https://github.com/veronikayurchuk/pretrained-models.pytorch/releases/download/v1.0/nasnetmobile-7e03cead.pth.tar', + # 'url': 'https://github.com/veronikayurchuk/pretrained-models.pytorch/releases/download/v1.0/nasnetmobile-7e03cead.pth.tar', 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/nasnetamobile-7e03cead.pth', 'input_space': 'RGB', diff --git a/torchreid/models/shufflenet.py b/torchreid/models/shufflenet.py index 013dc11..bc4d34f 100644 --- a/torchreid/models/shufflenet.py +++ b/torchreid/models/shufflenet.py @@ -45,7 +45,8 @@ class Bottleneck(nn.Module): 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 + if stride == 2: + out_channels -= in_channels # group conv is not applied to first conv1x1 at stage 2 num_groups_conv1x1 = num_groups if group_conv1x1 else 1 self.conv1 = nn.Conv2d( @@ -71,7 +72,8 @@ class Bottleneck(nn.Module): mid_channels, out_channels, 1, groups=num_groups, bias=False ) self.bn3 = nn.BatchNorm2d(out_channels) - if stride == 2: self.shortcut = nn.AvgPool2d(3, stride=2, padding=1) + if stride == 2: + self.shortcut = nn.AvgPool2d(3, stride=2, padding=1) def forward(self, x): out = F.relu(self.bn1(self.conv1(x))) @@ -187,7 +189,7 @@ def init_pretrained_weights(model, model_url): def shufflenet(num_classes, loss='softmax', pretrained=True, **kwargs): model = ShuffleNet(num_classes, loss, **kwargs) if pretrained: - #init_pretrained_weights(model, model_urls['imagenet']) + # init_pretrained_weights(model, model_urls['imagenet']) import warnings warnings.warn( 'The imagenet pretrained weights need to be manually downloaded from {}' diff --git a/torchreid/models/squeezenet.py b/torchreid/models/squeezenet.py index 1411ebc..83e8dc9 100644 --- a/torchreid/models/squeezenet.py +++ b/torchreid/models/squeezenet.py @@ -5,7 +5,6 @@ from __future__ import division, absolute_import import torch import torch.nn as nn import torch.utils.model_zoo as model_zoo -from torch.utils import model_zoo as model_zoo __all__ = ['squeezenet1_0', 'squeezenet1_1', 'squeezenet1_0_fc512'] diff --git a/torchreid/utils/model_complexity.py b/torchreid/utils/model_complexity.py index 3cd91e4..7d1dc1e 100644 --- a/torchreid/utils/model_complexity.py +++ b/torchreid/utils/model_complexity.py @@ -32,7 +32,7 @@ Convolution def hook_convNd(m, x, y): k = torch.prod(torch.Tensor(m.kernel_size)).item() cin = m.in_channels - flops_per_ele = k * cin #+ (k*cin-1) + flops_per_ele = k * cin # + (k*cin-1) if m.bias is not None: flops_per_ele += 1 flops = flops_per_ele * y.numel() / m.groups @@ -200,7 +200,7 @@ Linear def hook_linear(m, x, y): - flops_per_ele = m.in_features #+ (m.in_features-1) + flops_per_ele = m.in_features # + (m.in_features-1) if m.bias is not None: flops_per_ele += 1 flops = flops_per_ele * y.numel() diff --git a/torchreid/utils/rerank.py b/torchreid/utils/rerank.py index 0e8a276..efadf5a 100644 --- a/torchreid/utils/rerank.py +++ b/torchreid/utils/rerank.py @@ -5,8 +5,8 @@ Source: https://github.com/zhunzhong07/person-re-ranking Created on Mon Jun 26 14:46:56 2017 @author: luohao -Modified by Houjing Huang, 2017-12-22. -- This version accepts distance matrix instead of raw features. +Modified by Houjing Huang, 2017-12-22. +- This version accepts distance matrix instead of raw features. - The difference of `/` division between python 2 and 3 is handled. - numpy.float16 is replaced by numpy.float32 for numerical precision. diff --git a/torchreid/utils/torchtools.py b/torchreid/utils/torchtools.py index a139215..b6df8d8 100644 --- a/torchreid/utils/torchtools.py +++ b/torchreid/utils/torchtools.py @@ -70,7 +70,7 @@ def load_checkpoint(fpath): Returns: dict - Examples:: + Examples:: >>> from torchreid.utils import load_checkpoint >>> fpath = 'log/my_model/model.pth.tar-10' >>> checkpoint = load_checkpoint(fpath)