Skip to content

Commit

Permalink
Merge pull request deepinsight#2070 from maldil/master
Browse files Browse the repository at this point in the history
[Refactoring] making the code more Pythonic
  • Loading branch information
nttstar committed Aug 13, 2022
2 parents e089d66 + 5fb2bae commit 8c337ca
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 26 deletions.
5 changes: 2 additions & 3 deletions body/human_pose/ambiguity_aware/lib/core/function1.py
Expand Up @@ -319,9 +319,8 @@ def evaluate(val_loader, pose_model, config, is_train=False):
factor = 0.680019 if prefix == "h36m" else 0.7577316
if not use_same_norm_3d:
factor_filename = f"../data/{prefix}_test_factor_3d.pkl" if not is_train else f"../data/{prefix}_train_factor_3d.pkl"
f = open(factor_filename, "rb")
factor = torch.from_numpy(pkl.load(f)).cuda()
f.close()
with open(factor_filename, "rb") as f:
factor = torch.from_numpy(pkl.load(f)).cuda()
# print('eval with 15 joints')
# exclude spine: 14, neck: 15
# joint_indices = list(range(14)) + [16]
Expand Down
5 changes: 2 additions & 3 deletions body/human_pose/ambiguity_aware/lib/core/function2.py
Expand Up @@ -318,9 +318,8 @@ def evaluate(val_loader, pose_model, config, is_train=False):
factor = 0.680019 if prefix == "h36m" else 0.7577316
if not use_same_norm_3d:
factor_filename = f"../data/{prefix}_test_factor_3d.pkl" if not is_train else f"../data/{prefix}_train_factor_3d.pkl"
f = open(factor_filename, "rb")
factor = torch.from_numpy(pkl.load(f)).cuda()
f.close()
with open(factor_filename, "rb") as f:
factor = torch.from_numpy(pkl.load(f)).cuda()
# print('eval with 15 joints')
# joint_indices = list(range(14)) + [16]
with torch.no_grad():
Expand Down
10 changes: 4 additions & 6 deletions body/human_pose/ambiguity_aware/lib/dataloader/mpiinf.py
Expand Up @@ -75,9 +75,8 @@ def _load_data_set(self):
pkl.dump(factor_3d, f)

if osp.exists(self.scale_path):
f = open(self.scale_path, "rb")
self.scales = pkl.load(f)['scale']
f.close()
with open(self.scale_path, "rb") as f:
self.scales = pkl.load(f)['scale']
else:
if self.use_scaler:
pass
Expand All @@ -86,9 +85,8 @@ def _load_data_set(self):

if self.use_ideal_scale:
# scales computed from projection of 3d
f = open("../data/mpi_{}_scales.pkl".format("train" if self.is_train else "valid"), "rb")
scales = pkl.load(f)
f.close()
with open("../data/mpi_{}_scales.pkl".format("train" if self.is_train else "valid"), "rb") as f:
scales = pkl.load(f)
self.kp2ds = self.kp2ds * scales

fp.close()
Expand Down
10 changes: 4 additions & 6 deletions body/human_pose/ambiguity_aware/lib/utils/misc.py
Expand Up @@ -48,15 +48,13 @@ def init_weights(model):
m.bias.data.zero_()

def save_pickle(data, save_path):
f = open(save_path, "wb")
pkl.dump(data, f)
f.close()
with open(save_path, "wb") as f:
pkl.dump(data, f)
print(f"=> saved to {save_path}")

def load_pickle(load_path):
f = open(load_path, "rb")
data = pkl.load(f)
f.close()
with open(load_path, "rb") as f:
data = pkl.load(f)
print(f"<= loaded from {load_path}")
return data

Expand Down
10 changes: 4 additions & 6 deletions detection/scrfd/mmdet/core/evaluation/widerface.py
Expand Up @@ -153,9 +153,8 @@ def get_gt_boxes_from_txt(gt_path, cache_dir):

cache_file = os.path.join(cache_dir, 'gt_cache.pkl')
if os.path.exists(cache_file):
f = open(cache_file, 'rb')
boxes = pickle.load(f)
f.close()
with open(cache_file, 'rb') as f:
boxes = pickle.load(f)
return boxes

f = open(gt_path, 'r')
Expand Down Expand Up @@ -188,9 +187,8 @@ def get_gt_boxes_from_txt(gt_path, cache_dir):
current_boxes.append(box)
continue

f = open(cache_file, 'wb')
pickle.dump(boxes, f)
f.close()
with open(cache_file, 'wb') as f:
pickle.dump(boxes, f)
return boxes


Expand Down
4 changes: 2 additions & 2 deletions detection/scrfd/mmdet/models/necks/bfp.py
Expand Up @@ -2,7 +2,7 @@
import torch.nn.functional as F
from mmcv.cnn import ConvModule, xavier_init
from mmcv.cnn.bricks import NonLocal2d

import numpy as np
from ..builder import NECKS


Expand Down Expand Up @@ -85,7 +85,7 @@ def forward(self, inputs):
inputs[i], size=gather_size, mode='nearest')
feats.append(gathered)

bsf = sum(feats) / len(feats)
bsf = np.mean(feats)

# step 2: refine gathered features
if self.refine_type is not None:
Expand Down

0 comments on commit 8c337ca

Please sign in to comment.