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

Converted python 2 to python 3 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 72 additions & 25 deletions cv/example_demo.ipynb

Large diffs are not rendered by default.

28 changes: 24 additions & 4 deletions lib/loaders/gt_mrcn_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,15 @@ def sample_neg_ids(self, ann_id, seq_per_ref, sample_ratio):
elif len(dt_ann_ids) > 0:
neg_ann_id = random.choice(dt_ann_ids)
else:
neg_ann_id = random.choice(self.Anns.keys())
neg_ann_id = random.choice(list(self.Anns.keys()))
neg_ann_ids += [neg_ann_id]
# neg_ref_id for negative language representations: mainly from same-type "referred" objects
if len(st_ref_ids) > 0 and np.random.uniform(0, 1, 1) < sample_ratio:
neg_ref_id = random.choice(st_ref_ids)
elif len(dt_ref_ids) > 0:
neg_ref_id = random.choice(dt_ref_ids)
else:
neg_ref_id = random.choice(self.Refs.keys())
neg_ref_id = random.choice(list(self.Refs.keys()))
neg_sent_id = random.choice(self.Refs[neg_ref_id]['sent_ids'])
neg_sent_ids += [neg_sent_id]

Expand Down Expand Up @@ -284,9 +284,29 @@ def compare(ann_id0, ann_id1):
else:
return 1
image = self.Images[ref_ann['image_id']]


def cmp_to_key(mycmp):
'Convert a cmp= function into a key= function'
class K(object):
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return K

ann_ids = list(image['ann_ids']) # copy in case the raw list is changed
ann_ids = sorted(ann_ids, cmp=compare)
#ann_ids = sorted(ann_ids, cmp=compare)
ann_ids = sorted(ann_ids, key=cmp_to_key(compare))

st_ref_ids, st_ann_ids, dt_ref_ids, dt_ann_ids = [], [], [], []
for ann_id in ann_ids:
Expand Down
1 change: 1 addition & 0 deletions lib/mrcn/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def boxes_to_masks(self, img_path, boxes, labels):
rles = []
for m in masks:
rle = COCOmask.encode(np.asfortranarray(m))
rle['counts'] = rle['counts'].decode('ascii')
rles += [rle]

return masks, rles
Expand Down
2 changes: 1 addition & 1 deletion lib/mrcn/inference_no_imdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def box_to_pool5_fc7(self, net_conv, im_info, ori_boxes):

fc7 = self.net._head_to_tail(pool5) # (n, 2048, 7, 7)
pool5 = pool5.mean(3).mean(2)
fc7 = fc7.mean(3).mean(2) # (n, 2048)
fc7 = fc7.mean(3, keepdim=True).mean(2, keepdim=True) # (n, 2048)
return pool5, fc7

def box_to_fc7(self, net_conv, im_info, ori_boxes):
Expand Down
3 changes: 3 additions & 0 deletions tools/eval_masks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

# compute IoU
def computeIoU(pred_rle, gd_rle):
#pred_rle['counts'] = pred_rle['counts'].encode('ascii')
#gd_rle['counts'] = gd_rle['counts'].encode('ascii')

pred_seg = maskUtils.decode(pred_rle) # (H, W)
gd_seg = maskUtils.decode(gd_rle) # (H, W)
I = np.sum(np.logical_and(pred_seg, gd_seg))
Expand Down
22 changes: 21 additions & 1 deletion tools/mattnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,27 @@ def compare(det_id0, det_id1):
return 1

det_ids = list(Dets.keys()) # copy in case the raw list is changed
det_ids = sorted(det_ids, cmp=compare)

def cmp_to_key(mycmp):
#'Convert a cmp= function into a key= function'
class K(object):
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return K

det_ids = sorted(det_ids, key=cmp_to_key(compare))
st_det_ids, dt_det_ids = [], []
for det_id in det_ids:
if det_id != ref_det_id:
Expand Down
4 changes: 2 additions & 2 deletions tools/prepro.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def build_vocab(refer, params):
vocab = good_words

# add category words
category_names = refer.Cats.values() + ['__background__']
category_names = list(refer.Cats.values()) + ['__background__']
for cat_name in category_names:
for wd in cat_name.split():
if wd not in word2count or word2count[wd] <= count_thr:
Expand Down Expand Up @@ -192,7 +192,7 @@ def build_att_vocab(refer, params, att_types=['r1', 'r2', 'r7']):
sentToRef = refer.sentToRef
ref_to_att_wds = {}
forbidden = forbidden_noun + forbidden_att + forbidden_verb \
+ refer.Cats.values() # we also forbid category name here
+ list(refer.Cats.values()) # we also forbid category name here
for sent in sents:
sent_id = sent['sent_id']
atts = sent['atts']
Expand Down
4 changes: 2 additions & 2 deletions tools/run_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def main(args):
scores, boxes = mrcn.predict(img_path)

# get cls_to_dets, class_name -> [xyxysc] (n, 5)
cls_to_dets, num_dets = cls_to_detections(scores, boxes, imdb, args.nms_thresh, args.conf_thresh)
cls_to_dets, num_dets = cls_to_detections(scores, boxes, imdb, args.nms_thresh, float(args.conf_thresh))

# make sure num_dets > 0 for this image, otherwise we lower down the conf_thresh
thresh = args.conf_thresh
thresh = float(args.conf_thresh)
while num_dets == 0:
thresh = thresh-0.1
cls_to_dets, num_dets = cls_to_detections(scores, boxes, imdb, args.nms_thresh, thresh)
Expand Down
5 changes: 5 additions & 0 deletions tools/run_detect_to_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def main(args):
# load detections = [{det_id, box, image_id, category_id, category_name, score}]
save_dir = osp.join('cache/detections', args.dataset+'_'+args.splitBy)
detections = json.load(open(osp.join(save_dir, args.dets_file_name)))

image_to_dets = {}
for det in detections:
image_id = det['image_id']
Expand All @@ -67,6 +68,7 @@ def main(args):
image_to_dets[image_id] += [det]

# run mask rcnn
#for i, image_id in enumerate(list(image_to_dets.keys())[:2]):
for i, image_id in enumerate(image_to_dets.keys()):
dets = image_to_dets[image_id]

Expand All @@ -89,6 +91,9 @@ def main(args):

# save dets.json = [{det_id, box, image_id, score}]
# to cache/detections/

#print(detections[0])

save_path = osp.join(save_dir, args.dets_file_name[:-10] + '_masks.json')
with open(save_path, 'w') as f:
json.dump(detections, f)
Expand Down
3 changes: 2 additions & 1 deletion tools/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ def main(args):
infos['val_result_history'] = val_result_history
infos['word_to_ix'] = loader.word_to_ix
infos['att_to_ix'] = loader.att_to_ix
with open(osp.join(checkpoint_dir, opt['id']+'.json'), 'wb') as io:
#with open(osp.join(checkpoint_dir, opt['id']+'.json'), 'wb') as io:
with open(osp.join(checkpoint_dir, opt['id']+'.json'), 'w') as io:
json.dump(infos, io)

# update iter and epoch
Expand Down