Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery Starbot ⭐ refactored buts101/Detectron #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SourceryAI
Copy link

Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨

Here's your pull request refactoring your most popular Python repo.

If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.

Review changes via command line

To manually merge these changes, make sure you're on the master branch, then run:

git fetch https://github.com/sourcery-ai-bot/Detectron master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Copy link
Author

@SourceryAI SourceryAI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to GitHub API limits, only the first 60 comments can be shown.

Comment on lines -81 to +82
binary = os.path.join(binary_dir, 'test_net' + binary_ext)
assert os.path.exists(binary), 'Binary \'{}\' not found'.format(binary)
binary = os.path.join(binary_dir, f'test_net{binary_ext}')
assert os.path.exists(binary), f"Binary '{binary}' not found"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function multi_gpu_generate_rpn_on_dataset refactored with the following changes:

logger.info('Wrote RPN proposals to {}'.format(os.path.abspath(rpn_file)))
logger.info(f'Wrote RPN proposals to {os.path.abspath(rpn_file)}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function generate_rpn_on_range refactored with the following changes:

Comment on lines -196 to +204
core.ScopedName('rpn_rois_fpn' + str(l))
core.ScopedName(f'rpn_rois_fpn{str(l)}')
for l in range(k_min, k_max + 1)
]

score_names = [
core.ScopedName('rpn_roi_probs_fpn' + str(l))
core.ScopedName(f'rpn_roi_probs_fpn{str(l)}')
for l in range(k_min, k_max + 1)
]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function im_proposals refactored with the following changes:

Comment on lines -272 to +275
im_size_min = np.min(im_shape[0:2])
im_size_max = np.max(im_shape[0:2])

processed_ims = []
im_size_min = np.min(im_shape[:2])
im_size_max = np.max(im_shape[:2])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _get_image_blob refactored with the following changes:

Comment on lines -294 to +298
if not cfg.MODEL.FASTER_RCNN:
box_proposals_hf = box_utils.flip_boxes(box_proposals, im_width)
else:
box_proposals_hf = None
box_proposals_hf = (
None
if cfg.MODEL.FASTER_RCNN
else box_utils.flip_boxes(box_proposals, im_width)
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function im_detect_bbox_hflip refactored with the following changes:

ds.classes = {i: name for i, name in enumerate(classes)}
ds.classes = dict(enumerate(classes))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_coco_dataset refactored with the following changes:

Comment on lines -57 to +66
assert name in DATASETS.keys(), \
'Unknown dataset name: {}'.format(name)
assert os.path.exists(DATASETS[name][IM_DIR]), \
'Image directory \'{}\' not found'.format(DATASETS[name][IM_DIR])
assert os.path.exists(DATASETS[name][ANN_FN]), \
'Annotation file \'{}\' not found'.format(DATASETS[name][ANN_FN])
logger.debug('Creating: {}'.format(name))
assert name in DATASETS.keys(), f'Unknown dataset name: {name}'
assert os.path.exists(
DATASETS[name][IM_DIR]
), f"Image directory '{DATASETS[name][IM_DIR]}' not found"

assert os.path.exists(
DATASETS[name][ANN_FN]
), f"Annotation file '{DATASETS[name][ANN_FN]}' not found"

logger.debug(f'Creating: {name}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function JsonDataset.__init__ refactored with the following changes:

Comment on lines -255 to +258
logger.info('Loading proposals from: {}'.format(proposal_file))
logger.info(f'Loading proposals from: {proposal_file}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function JsonDataset._add_proposals_from_file refactored with the following changes:

Comment on lines -316 to +319
x = kp[0::3] # 0-indexed x coordinates
x = kp[::3]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function JsonDataset._get_gt_keypoints refactored with the following changes:

This removes the following comments ( why? ):

# 0-indexed x coordinates

Comment on lines -47 to +51
output_dir, 'segmentations_' + json_dataset.name + '_results'
output_dir, f'segmentations_{json_dataset.name}_results'
)

if use_salt:
res_file += '_{}'.format(str(uuid.uuid4()))
res_file += f'_{str(uuid.uuid4())}'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function evaluate_masks refactored with the following changes:

Comment on lines -82 to +85
'Writing segmentation results json to: {}'.format(
os.path.abspath(res_file)))
f'Writing segmentation results json to: {os.path.abspath(res_file)}'
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _write_coco_segms_results_file refactored with the following changes:

Comment on lines -122 to +124
logger.info('Wrote json eval results to: {}'.format(eval_file))
logger.info(f'Wrote json eval results to: {eval_file}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _do_segmentation_eval refactored with the following changes:

Comment on lines -129 to +133
res_file = os.path.join(
output_dir, 'bbox_' + json_dataset.name + '_results'
)
res_file = os.path.join(output_dir, f'bbox_{json_dataset.name}_results')
if use_salt:
res_file += '_{}'.format(str(uuid.uuid4()))
res_file += f'_{str(uuid.uuid4())}'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function evaluate_boxes refactored with the following changes:

logger.info(
'Writing bbox results json to: {}'.format(os.path.abspath(res_file)))
logger.info(f'Writing bbox results json to: {os.path.abspath(res_file)}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _write_coco_bbox_results_file refactored with the following changes:

Comment on lines -199 to +198
logger.info('Wrote json eval results to: {}'.format(eval_file))
logger.info(f'Wrote json eval results to: {eval_file}')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _do_detection_eval refactored with the following changes:

Comment on lines -203 to +201
assert dataset in results, 'Dataset {} not in results'.format(dataset)
assert task in results[dataset], 'Task {} not in results'.format(task)
assert metric in results[dataset][task], \
'Metric {} not in results'.format(metric)
assert dataset in results, f'Dataset {dataset} not in results'
assert task in results[dataset], f'Task {task} not in results'
assert metric in results[dataset][task], f'Metric {metric} not in results'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function check_expected_results refactored with the following changes:

salt = '_{}'.format(str(uuid.uuid4())) if use_salt else ''
salt = f'_{str(uuid.uuid4())}' if use_salt else ''
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function evaluate_boxes refactored with the following changes:

Comment on lines -61 to +64
assert os.path.exists(image_set_path), \
'Image set path does not exist: {}'.format(image_set_path)
assert os.path.exists(
image_set_path
), f'Image set path does not exist: {image_set_path}'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _write_voc_results_files refactored with the following changes:

Comment on lines -101 to +104
filename = 'comp4' + salt + '_det_' + image_set + '_{:s}.txt'
return os.path.join(devkit_path, 'results', 'VOC' + year, 'Main', filename)
filename = f'comp4{salt}_det_{image_set}' + '_{:s}.txt'
return os.path.join(devkit_path, 'results', f'VOC{year}', 'Main', filename)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _get_voc_results_file_template refactored with the following changes:

Comment on lines -114 to +120
use_07_metric = True if int(year) < 2010 else False
use_07_metric = int(year) < 2010
logger.info('VOC07 metric? ' + ('Yes' if use_07_metric else 'No'))
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
for _, cls in enumerate(json_dataset.classes):
for cls in json_dataset.classes:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _do_python_eval refactored with the following changes:

Comment on lines -154 to +161
cmd = 'cd {} && '.format(path)
cmd += '{:s} -nodisplay -nodesktop '.format(cfg.MATLAB)
cmd = f'cd {path} && ' + '{:s} -nodisplay -nodesktop '.format(cfg.MATLAB)
cmd += '-r "dbstop if error; '
cmd += 'voc_eval(\'{:s}\',\'{:s}\',\'{:s}\',\'{:s}\'); quit;"' \
.format(info['devkit_path'], 'comp4' + salt, info['image_set'],
output_dir)
cmd += 'voc_eval(\'{:s}\',\'{:s}\',\'{:s}\',\'{:s}\'); quit;"'.format(
info['devkit_path'], f'comp4{salt}', info['image_set'], output_dir
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _do_matlab_eval refactored with the following changes:

Comment on lines -168 to +175
assert os.path.exists(devkit_path), \
'Devkit directory {} not found'.format(devkit_path)
anno_path = os.path.join(
devkit_path, 'VOC' + year, 'Annotations', '{:s}.xml')
assert os.path.exists(devkit_path), f'Devkit directory {devkit_path} not found'
anno_path = os.path.join(devkit_path, f'VOC{year}', 'Annotations', '{:s}.xml')
image_set_path = os.path.join(
devkit_path, 'VOC' + year, 'ImageSets', 'Main', image_set + '.txt')
devkit_path, f'VOC{year}', 'ImageSets', 'Main', f'{image_set}.txt'
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function voc_info refactored with the following changes:

obj_struct = {}
obj_struct['name'] = obj.find('name').text
obj_struct = {'name': obj.find('name').text}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function parse_rec refactored with the following changes:

Comment on lines -62 to +61
if np.sum(rec >= t) == 0:
p = 0
else:
p = np.max(prec[rec >= t])
p = 0 if np.sum(rec >= t) == 0 else np.max(prec[rec >= t])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function voc_ap refactored with the following changes:

Comment on lines -122 to +118
cachefile = os.path.join(cachedir, imageset + '_annots.pkl')
cachefile = os.path.join(cachedir, f'{imageset}_annots.pkl')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function voc_eval refactored with the following changes:

Comment on lines -294 to 377
'conv_rpn_fpn' + slvl,
f'conv_rpn_fpn{slvl}',
dim_in,
dim_out,
kernel=3,
pad=1,
stride=1,
weight_init=gauss_fill(0.01),
bias_init=const_fill(0.0)
bias_init=const_fill(0.0),
)

model.Relu(conv_rpn_fpn, conv_rpn_fpn)
# Proposal classification scores
rpn_cls_logits_fpn = model.Conv(
conv_rpn_fpn,
'rpn_cls_logits_fpn' + slvl,
f'rpn_cls_logits_fpn{slvl}',
dim_in,
num_anchors,
kernel=1,
pad=0,
stride=1,
weight_init=gauss_fill(0.01),
bias_init=const_fill(0.0)
bias_init=const_fill(0.0),
)

# Proposal bbox regression deltas
rpn_bbox_pred_fpn = model.Conv(
conv_rpn_fpn,
'rpn_bbox_pred_fpn' + slvl,
f'rpn_bbox_pred_fpn{slvl}',
dim_in,
4 * num_anchors,
kernel=1,
pad=0,
stride=1,
weight_init=gauss_fill(0.01),
bias_init=const_fill(0.0)
bias_init=const_fill(0.0),
)

else:
# Share weights and biases
sk_min = str(k_min)
# RPN hidden representation
conv_rpn_fpn = model.ConvShared(
bl_in,
'conv_rpn_fpn' + slvl,
f'conv_rpn_fpn{slvl}',
dim_in,
dim_out,
kernel=3,
pad=1,
stride=1,
weight='conv_rpn_fpn' + sk_min + '_w',
bias='conv_rpn_fpn' + sk_min + '_b'
weight=f'conv_rpn_fpn{sk_min}_w',
bias=f'conv_rpn_fpn{sk_min}_b',
)

model.Relu(conv_rpn_fpn, conv_rpn_fpn)
# Proposal classification scores
rpn_cls_logits_fpn = model.ConvShared(
conv_rpn_fpn,
'rpn_cls_logits_fpn' + slvl,
f'rpn_cls_logits_fpn{slvl}',
dim_in,
num_anchors,
kernel=1,
pad=0,
stride=1,
weight='rpn_cls_logits_fpn' + sk_min + '_w',
bias='rpn_cls_logits_fpn' + sk_min + '_b'
weight=f'rpn_cls_logits_fpn{sk_min}_w',
bias=f'rpn_cls_logits_fpn{sk_min}_b',
)

# Proposal bbox regression deltas
rpn_bbox_pred_fpn = model.ConvShared(
conv_rpn_fpn,
'rpn_bbox_pred_fpn' + slvl,
f'rpn_bbox_pred_fpn{slvl}',
dim_in,
4 * num_anchors,
kernel=1,
pad=0,
stride=1,
weight='rpn_bbox_pred_fpn' + sk_min + '_w',
bias='rpn_bbox_pred_fpn' + sk_min + '_b'
weight=f'rpn_bbox_pred_fpn{sk_min}_w',
bias=f'rpn_bbox_pred_fpn{sk_min}_b',
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function add_fpn_rpn_outputs refactored with the following changes:

Comment on lines -477 to +492
blobs[blob_prefix + '_fpn' + str(lvl)] = rois[idx_lvl, :]
blobs[f'{blob_prefix}_fpn{str(lvl)}'] = rois[idx_lvl, :]
rois_idx_order = np.concatenate((rois_idx_order, idx_lvl))
rois_stacked = np.vstack(
[rois_stacked, blobs[blob_prefix + '_fpn' + str(lvl)]]
)
rois_stacked = np.vstack([rois_stacked, blobs[f'{blob_prefix}_fpn{str(lvl)}']])
rois_idx_restore = np.argsort(rois_idx_order).astype(np.int32, copy=False)
blobs[blob_prefix + '_idx_restore_int32'] = rois_idx_restore
blobs[f'{blob_prefix}_idx_restore_int32'] = rois_idx_restore
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function add_multilevel_roi_blobs refactored with the following changes:

Comment on lines -74 to +83
'{}_{}'.format(prefix, i),
f'{prefix}_{i}',
blob_in,
dim_in,
dim_out,
dim_inner,
dilation,
stride_init,
# Not using inplace for the last block;
# it may be fetched externally or used by FPN
inplace_sum=i < n - 1
inplace_sum=i < n - 1,
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function add_stage refactored with the following changes:

This removes the following comments ( why? ):

# Not using inplace for the last block;
# it may be fetched externally or used by FPN

Comment on lines -189 to +188
s = model.net.Sum([tr, sc], prefix + '_sum')
s = model.net.Sum([tr, sc], f'{prefix}_sum')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function add_residual_block refactored with the following changes:

Comment on lines -200 to +207
prefix + '_branch1',
f'{prefix}_branch1',
dim_in,
dim_out,
kernel=1,
stride=stride,
no_bias=1
no_bias=1,
)
return model.AffineChannel(c, prefix + '_branch1_bn', dim=dim_out)

return model.AffineChannel(c, f'{prefix}_branch1_bn', dim=dim_out)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function add_shortcut refactored with the following changes:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant