From 4e612ebbdcc277b0c7f95229cea9ec16fd7c9324 Mon Sep 17 00:00:00 2001 From: Ycr <34575412+yaochaorui@users.noreply.github.com> Date: Thu, 12 Nov 2020 18:06:36 +0800 Subject: [PATCH] No-self-use. (#275) --- mmpose/core/fp16/hooks.py | 6 ++++-- mmpose/core/post_processing/group.py | 6 ++++-- mmpose/datasets/datasets/bottom_up/bottom_up_coco.py | 3 ++- mmpose/datasets/datasets/hand/hand_base_dataset.py | 6 ++++-- mmpose/datasets/datasets/hand/interhand2d_dataset.py | 6 ++++-- mmpose/datasets/datasets/mesh/mesh_h36m_dataset.py | 6 ++++-- mmpose/datasets/datasets/mesh/mosh_dataset.py | 3 ++- mmpose/datasets/datasets/top_down/topdown_coco_dataset.py | 3 ++- .../datasets/top_down/topdown_coco_wholebody_dataset.py | 3 ++- .../datasets/top_down/topdown_mpii_trb_dataset.py | 3 ++- .../datasets/top_down/topdown_posetrack18_dataset.py | 3 ++- mmpose/datasets/pipelines/bottom_up_transform.py | 6 ++++-- mmpose/models/backbones/hrnet.py | 4 ++-- mmpose/models/backbones/regnet.py | 4 ++-- .../keypoint_heads/bottom_up_higher_resolution_head.py | 8 +++++--- mmpose/models/keypoint_heads/bottom_up_simple_head.py | 3 ++- mmpose/models/keypoint_heads/top_down_multi_stage_head.py | 3 ++- mmpose/models/keypoint_heads/top_down_simple_head.py | 3 ++- mmpose/models/losses/mesh_loss.py | 3 ++- mmpose/models/losses/multi_loss_factory.py | 3 ++- 20 files changed, 55 insertions(+), 30 deletions(-) diff --git a/mmpose/core/fp16/hooks.py b/mmpose/core/fp16/hooks.py index 7f49add0a9..b503a621c0 100644 --- a/mmpose/core/fp16/hooks.py +++ b/mmpose/core/fp16/hooks.py @@ -52,7 +52,8 @@ def before_run(self, runner): # convert model to fp16 wrap_fp16_model(runner.model) - def copy_grads_to_fp32(self, fp16_net, fp32_weights): + @staticmethod + def copy_grads_to_fp32(fp16_net, fp32_weights): """Copy gradients from fp16 model to fp32 weight copy.""" for fp32_param, fp16_param in zip(fp32_weights, fp16_net.parameters()): if fp16_param.grad is not None: @@ -60,7 +61,8 @@ def copy_grads_to_fp32(self, fp16_net, fp32_weights): fp32_param.grad = fp32_param.data.new(fp32_param.size()) fp32_param.grad.copy_(fp16_param.grad) - def copy_params_to_fp16(self, fp16_net, fp32_weights): + @staticmethod + def copy_params_to_fp16(fp16_net, fp32_weights): """Copy updated params from fp32 weight copy to fp16 model.""" for fp16_param, fp32_param in zip(fp16_net.parameters(), fp32_weights): fp16_param.data.copy_(fp32_param.data) diff --git a/mmpose/core/post_processing/group.py b/mmpose/core/post_processing/group.py index f86ea21847..8fc8126e54 100644 --- a/mmpose/core/post_processing/group.py +++ b/mmpose/core/post_processing/group.py @@ -235,7 +235,8 @@ def top_k(self, heatmaps, tags): return ans - def adjust(self, ans, heatmaps): + @staticmethod + def adjust(ans, heatmaps): """Adjust the coordinates for better accuracy. Note: @@ -271,7 +272,8 @@ def adjust(self, ans, heatmaps): 0:2] = (x + 0.5, y + 0.5) return ans - def refine(self, heatmap, tag, keypoints): + @staticmethod + def refine(heatmap, tag, keypoints): """Given initial keypoint predictions, we identify missing joints. Note: diff --git a/mmpose/datasets/datasets/bottom_up/bottom_up_coco.py b/mmpose/datasets/datasets/bottom_up/bottom_up_coco.py index 765e621e78..8e0ebac575 100644 --- a/mmpose/datasets/datasets/bottom_up/bottom_up_coco.py +++ b/mmpose/datasets/datasets/bottom_up/bottom_up_coco.py @@ -97,7 +97,8 @@ def __init__(self, print(f'=> num_images: {self.num_images}') - def _get_mapping_id_name(self, imgs): + @staticmethod + def _get_mapping_id_name(imgs): """ Args: imgs (dict): dict of image info. diff --git a/mmpose/datasets/datasets/hand/hand_base_dataset.py b/mmpose/datasets/datasets/hand/hand_base_dataset.py index fa6feb7b25..4dfeffd758 100644 --- a/mmpose/datasets/datasets/hand/hand_base_dataset.py +++ b/mmpose/datasets/datasets/hand/hand_base_dataset.py @@ -62,7 +62,8 @@ def __init__(self, self.pipeline = Compose(self.pipeline) - def _get_mapping_id_name(self, imgs): + @staticmethod + def _get_mapping_id_name(imgs): """ Args: imgs (dict): dict of image info. @@ -121,7 +122,8 @@ def evaluate(self, cfg, preds, output_dir, *args, **kwargs): """Evaluate keypoint results.""" raise NotImplementedError - def _write_keypoint_results(self, keypoints, res_file): + @staticmethod + def _write_keypoint_results(keypoints, res_file): """Write results into a json file.""" with open(res_file, 'w') as f: diff --git a/mmpose/datasets/datasets/hand/interhand2d_dataset.py b/mmpose/datasets/datasets/hand/interhand2d_dataset.py index 2ce1d6d414..dea2f908a4 100644 --- a/mmpose/datasets/datasets/hand/interhand2d_dataset.py +++ b/mmpose/datasets/datasets/hand/interhand2d_dataset.py @@ -78,7 +78,8 @@ def __init__(self, print(f'=> num_images: {self.num_images}') print(f'=> load {len(self.db)} samples') - def _cam2pixel(self, cam_coord, f, c): + @staticmethod + def _cam2pixel(cam_coord, f, c): """Transform the joints from their camera coordinates to their pixel coordinates. @@ -101,7 +102,8 @@ def _cam2pixel(self, cam_coord, f, c): img_coord = np.concatenate((x[:, None], y[:, None], z[:, None]), 1) return img_coord - def _world2cam(self, world_coord, R, T): + @staticmethod + def _world2cam(world_coord, R, T): """Transform the joints from their world coordinates to their camera coordinates. diff --git a/mmpose/datasets/datasets/mesh/mesh_h36m_dataset.py b/mmpose/datasets/datasets/mesh/mesh_h36m_dataset.py index 229014091b..c1778b617e 100644 --- a/mmpose/datasets/datasets/mesh/mesh_h36m_dataset.py +++ b/mmpose/datasets/datasets/mesh/mesh_h36m_dataset.py @@ -52,7 +52,8 @@ def evaluate(self, outputs, res_folder, metric='joint_error', logger=None): name_value = OrderedDict(info_str) return name_value - def _write_keypoint_results(self, keypoints, res_file): + @staticmethod + def _write_keypoint_results(keypoints, res_file): """Write results into a json file.""" with open(res_file, 'w') as f: @@ -87,7 +88,8 @@ def _report_metric(self, res_file): info_str.append(('MPJPE-PA', mpjpe_pa * 1000)) return info_str - def evaluate_kernel(self, pred_joints_3d, joints_3d, joints_3d_visible): + @staticmethod + def evaluate_kernel(pred_joints_3d, joints_3d, joints_3d_visible): """Evaluate one example.""" # Only 14 lsp joints are used for evaluation joint_mapper = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 18] diff --git a/mmpose/datasets/datasets/mesh/mosh_dataset.py b/mmpose/datasets/datasets/mesh/mosh_dataset.py index 40692575a5..36b1893c63 100644 --- a/mmpose/datasets/datasets/mesh/mosh_dataset.py +++ b/mmpose/datasets/datasets/mesh/mosh_dataset.py @@ -31,7 +31,8 @@ def __init__(self, ann_file, pipeline, test_mode=False): self.db = self._get_db(ann_file) self.pipeline = Compose(self.pipeline) - def _get_db(self, ann_file): + @staticmethod + def _get_db(ann_file): """Load dataset.""" data = np.load(ann_file) _betas = data['shape'].astype(np.float32) diff --git a/mmpose/datasets/datasets/top_down/topdown_coco_dataset.py b/mmpose/datasets/datasets/top_down/topdown_coco_dataset.py index 4de21a0cd9..c7cb7dec67 100644 --- a/mmpose/datasets/datasets/top_down/topdown_coco_dataset.py +++ b/mmpose/datasets/datasets/top_down/topdown_coco_dataset.py @@ -112,7 +112,8 @@ def __init__(self, print(f'=> num_images: {self.num_images}') print(f'=> load {len(self.db)} samples') - def _get_mapping_id_name(self, imgs): + @staticmethod + def _get_mapping_id_name(imgs): """ Args: imgs (dict): dict of image info. diff --git a/mmpose/datasets/datasets/top_down/topdown_coco_wholebody_dataset.py b/mmpose/datasets/datasets/top_down/topdown_coco_wholebody_dataset.py index 0c4fcce85b..ac5f64dc70 100644 --- a/mmpose/datasets/datasets/top_down/topdown_coco_wholebody_dataset.py +++ b/mmpose/datasets/datasets/top_down/topdown_coco_wholebody_dataset.py @@ -125,7 +125,8 @@ def __init__(self, print(f'=> num_images: {self.num_images}') print(f'=> load {len(self.db)} samples') - def _make_flip_pairs(self): + @staticmethod + def _make_flip_pairs(): body = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13, 14], [15, 16]] foot = [[17, 20], [18, 21], [19, 22]] diff --git a/mmpose/datasets/datasets/top_down/topdown_mpii_trb_dataset.py b/mmpose/datasets/datasets/top_down/topdown_mpii_trb_dataset.py index e4e0bb3c32..58ef18e945 100644 --- a/mmpose/datasets/datasets/top_down/topdown_mpii_trb_dataset.py +++ b/mmpose/datasets/datasets/top_down/topdown_mpii_trb_dataset.py @@ -194,7 +194,8 @@ def evaluate(self, outputs, res_folder, metric='PCKh', **kwargs): return name_value - def _write_keypoint_results(self, keypoints, res_file): + @staticmethod + def _write_keypoint_results(keypoints, res_file): """Write results into a json file.""" with open(res_file, 'w') as f: diff --git a/mmpose/datasets/datasets/top_down/topdown_posetrack18_dataset.py b/mmpose/datasets/datasets/top_down/topdown_posetrack18_dataset.py index 5aee21a215..7b090c79f0 100644 --- a/mmpose/datasets/datasets/top_down/topdown_posetrack18_dataset.py +++ b/mmpose/datasets/datasets/top_down/topdown_posetrack18_dataset.py @@ -199,7 +199,8 @@ def evaluate(self, outputs, res_folder, metric='mAP', **kwargs): return name_value - def _write_posetrack18_keypoint_results(self, keypoint_results, gt_folder, + @staticmethod + def _write_posetrack18_keypoint_results(keypoint_results, gt_folder, pred_folder): """Write results into a json file. diff --git a/mmpose/datasets/pipelines/bottom_up_transform.py b/mmpose/datasets/pipelines/bottom_up_transform.py index 89e31a8397..a019d64307 100644 --- a/mmpose/datasets/pipelines/bottom_up_transform.py +++ b/mmpose/datasets/pipelines/bottom_up_transform.py @@ -229,7 +229,8 @@ def __init__(self, rot_factor, scale_factor, scale_type, trans_factor): self.scale_type = scale_type self.trans_factor = trans_factor - def _get_affine_matrix(self, center, scale, res, rot=0): + @staticmethod + def _get_affine_matrix(center, scale, res, rot=0): """Generate transformation matrix.""" h = scale t = np.zeros((3, 3), dtype=np.float32) @@ -255,7 +256,8 @@ def _get_affine_matrix(self, center, scale, res, rot=0): t = np.dot(t_inv, np.dot(rot_mat, np.dot(t_mat, t))) return t - def _affine_joints(self, joints, mat): + @staticmethod + def _affine_joints(joints, mat): """Affine the joints by the transform matrix.""" joints = np.array(joints) shape = joints.shape diff --git a/mmpose/models/backbones/hrnet.py b/mmpose/models/backbones/hrnet.py index 64b7c714c2..8e266fd0cc 100644 --- a/mmpose/models/backbones/hrnet.py +++ b/mmpose/models/backbones/hrnet.py @@ -42,8 +42,8 @@ def __init__(self, self.fuse_layers = self._make_fuse_layers() self.relu = nn.ReLU(inplace=True) - def _check_branches(self, num_branches, num_blocks, in_channels, - num_channels): + @staticmethod + def _check_branches(num_branches, num_blocks, in_channels, num_channels): """Check input to avoid ValueError.""" if num_branches != len(num_blocks): error_msg = f'NUM_BRANCHES({num_branches}) ' \ diff --git a/mmpose/models/backbones/regnet.py b/mmpose/models/backbones/regnet.py index d7adfd9ba1..1a26badd1a 100644 --- a/mmpose/models/backbones/regnet.py +++ b/mmpose/models/backbones/regnet.py @@ -202,8 +202,8 @@ def _make_stem_layer(self, in_channels, base_channels): self.add_module(self.norm1_name, norm1) self.relu = nn.ReLU(inplace=True) - def generate_regnet(self, - initial_width, + @staticmethod + def generate_regnet(initial_width, width_slope, width_parameter, depth, diff --git a/mmpose/models/keypoint_heads/bottom_up_higher_resolution_head.py b/mmpose/models/keypoint_heads/bottom_up_higher_resolution_head.py index c68b7cbbd8..f68c1734e0 100644 --- a/mmpose/models/keypoint_heads/bottom_up_higher_resolution_head.py +++ b/mmpose/models/keypoint_heads/bottom_up_higher_resolution_head.py @@ -77,8 +77,9 @@ def __init__(self, num_deconv_filters, num_deconv_kernels, num_basic_blocks, cat_output) - def _make_final_layers(self, in_channels, final_layer_output_channels, - extra, num_deconv_layers, num_deconv_filters): + @staticmethod + def _make_final_layers(in_channels, final_layer_output_channels, extra, + num_deconv_layers, num_deconv_filters): """Make final layers.""" if extra is not None and 'final_conv_kernel' in extra: assert extra['final_conv_kernel'] in [1, 3] @@ -147,7 +148,8 @@ def _make_deconv_layers(self, in_channels, deconv_layer_output_channels, return nn.ModuleList(deconv_layers) - def _get_deconv_cfg(self, deconv_kernel): + @staticmethod + def _get_deconv_cfg(deconv_kernel): """Get configurations for deconv layers.""" if deconv_kernel == 4: padding = 1 diff --git a/mmpose/models/keypoint_heads/bottom_up_simple_head.py b/mmpose/models/keypoint_heads/bottom_up_simple_head.py index fbd4f7a753..a12d406c18 100644 --- a/mmpose/models/keypoint_heads/bottom_up_simple_head.py +++ b/mmpose/models/keypoint_heads/bottom_up_simple_head.py @@ -120,7 +120,8 @@ def _make_deconv_layer(self, num_layers, num_filters, num_kernels): return nn.Sequential(*layers) - def _get_deconv_cfg(self, deconv_kernel): + @staticmethod + def _get_deconv_cfg(deconv_kernel): """Get configurations for deconv layers.""" if deconv_kernel == 4: padding = 1 diff --git a/mmpose/models/keypoint_heads/top_down_multi_stage_head.py b/mmpose/models/keypoint_heads/top_down_multi_stage_head.py index fd835342f5..c8051558b2 100644 --- a/mmpose/models/keypoint_heads/top_down_multi_stage_head.py +++ b/mmpose/models/keypoint_heads/top_down_multi_stage_head.py @@ -139,7 +139,8 @@ def _make_deconv_layer(self, num_layers, num_filters, num_kernels): return nn.Sequential(*layers) - def _get_deconv_cfg(self, deconv_kernel): + @staticmethod + def _get_deconv_cfg(deconv_kernel): """Get configurations for deconv layers.""" if deconv_kernel == 4: padding = 1 diff --git a/mmpose/models/keypoint_heads/top_down_simple_head.py b/mmpose/models/keypoint_heads/top_down_simple_head.py index df718f8161..d5b9f3dd79 100644 --- a/mmpose/models/keypoint_heads/top_down_simple_head.py +++ b/mmpose/models/keypoint_heads/top_down_simple_head.py @@ -118,7 +118,8 @@ def _make_deconv_layer(self, num_layers, num_filters, num_kernels): return nn.Sequential(*layers) - def _get_deconv_cfg(self, deconv_kernel): + @staticmethod + def _get_deconv_cfg(deconv_kernel): """Get configurations for deconv layers.""" if deconv_kernel == 4: padding = 1 diff --git a/mmpose/models/losses/mesh_loss.py b/mmpose/models/losses/mesh_loss.py index b7c6140300..0ed6507310 100644 --- a/mmpose/models/losses/mesh_loss.py +++ b/mmpose/models/losses/mesh_loss.py @@ -282,7 +282,8 @@ def __init__(self, raise NotImplementedError( f'GAN type {self.gan_type} is not implemented.') - def _wgan_loss(self, input, target): + @staticmethod + def _wgan_loss(input, target): """wgan loss. Args: diff --git a/mmpose/models/losses/multi_loss_factory.py b/mmpose/models/losses/multi_loss_factory.py index fd70b4d516..cc95e5ba5a 100644 --- a/mmpose/models/losses/multi_loss_factory.py +++ b/mmpose/models/losses/multi_loss_factory.py @@ -29,7 +29,8 @@ def _make_input(t, requires_grad=False, device=torch.device('cpu')): class HeatmapLoss(nn.Module): """Accumulate the heatmap loss for each image in the batch.""" - def forward(self, pred, gt, mask): + @staticmethod + def forward(pred, gt, mask): """ Note: batch_size: N