Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
d12306 committed May 8, 2022
1 parent 635432c commit 19f20b8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 42 deletions.
8 changes: 4 additions & 4 deletions bdd_coco_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
assert len(id_data['inter_feat'][0]) == 11 #+ 1024

if args.energy:
id_score = -torch.stack(id_data['logistic_score'])
ood_score = -torch.stack(ood_data['logistic_score'])
#id_score = -args.T * torch.logsumexp(torch.stack(id_data['inter_feat'])[:, :-1] / args.T, dim=1).cpu().data.numpy()
#ood_score = -args.T * torch.logsumexp(torch.stack(ood_data['inter_feat'])[:, :-1] / args.T, dim=1).cpu().data.numpy()
#id_score = -torch.stack(id_data['logistic_score'])
#ood_score = -torch.stack(ood_data['logistic_score'])
id_score = -args.T * torch.logsumexp(torch.stack(id_data['inter_feat'])[:, :-1] / args.T, dim=1).cpu().data.numpy()
ood_score = -args.T * torch.logsumexp(torch.stack(ood_data['inter_feat'])[:, :-1] / args.T, dim=1).cpu().data.numpy()
else:
id_score = -np.max(F.softmax(torch.stack(id_data['inter_feat'])[:, :-1], dim=1).cpu().data.numpy(), axis=1)
ood_score = -np.max(F.softmax(torch.stack(ood_data['inter_feat'])[:, :-1], dim=1).cpu().data.numpy(), axis=1)
Expand Down
17 changes: 2 additions & 15 deletions detection/inference/rcnn_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ def generalized_rcnn_probabilistic_inference(self,
box_delta = outputs['box_delta']

inter_feat = box_cls
logistic_score = self.model.roi_heads.logistic_regression(
torch.logsumexp(box_cls[:, :-1], 1).reshape(-1,1)).sigmoid().reshape(-1)
box_cls = torch.nn.functional.softmax(box_cls, dim=-1)


Expand All @@ -159,7 +157,6 @@ def generalized_rcnn_probabilistic_inference(self,
det_labels = det_labels[filter_mask]

inter_feat = inter_feat[filter_inds[:, 0]]
logistic_score = logistic_score[filter_inds[:, 0]]
proposal_boxes = proposals.proposal_boxes.tensor[filter_inds[:, 0]]

# predict boxes
Expand All @@ -168,20 +165,17 @@ def generalized_rcnn_probabilistic_inference(self,
boxes_covars = []


return boxes, boxes_covars, scores, inter_feat, logistic_score, filter_inds[:,
return boxes, boxes_covars, scores, inter_feat, filter_inds[:,
1], box_cls[filter_inds[:, 0]], det_labels

def post_processing_standard_nms(self, input_im):
"""
This function produces results using standard non-maximum suppression. The function takes into
account any probabilistic modeling method when computing the results.
Args:
input_im (list): an input im list generated from dataset handler.
Returns:
result (instances): object instances
"""
outputs = self.generalized_rcnn_probabilistic_inference(input_im)

Expand All @@ -191,10 +185,8 @@ def post_processing_standard_nms(self, input_im):
def post_processing_output_statistics(self, input_im):
"""
This function produces results using anchor statistics.
Args:
input_im (list): an input im list generated from dataset handler.
Returns:
result (instances): object instances
"""
Expand All @@ -211,10 +203,8 @@ def post_processing_output_statistics(self, input_im):
def post_processing_mc_dropout_ensembles(self, input_im):
"""
This function produces results using monte-carlo dropout ensembles.
Args:
input_im (list): an input im list generated from dataset handler.
Returns:
result (instances): object instances
"""
Expand Down Expand Up @@ -315,13 +305,10 @@ def post_processing_bayes_od(self, input_im):
"""
This function produces results using forms of bayesian inference instead of NMS for both category
and box results.
Args:
input_im (list): an input im list generated from dataset handler.
Returns:
result (instances): object instances
"""
box_merge_mode = self.cfg.PROBABILISTIC_INFERENCE.BAYES_OD.BOX_MERGE_MODE
cls_merge_mode = self.cfg.PROBABILISTIC_INFERENCE.BAYES_OD.CLS_MERGE_MODE
Expand Down Expand Up @@ -414,4 +401,4 @@ def post_processing_bayes_od(self, input_im):
result.pred_cls_probs = predicted_prob_vectors
result.pred_boxes_covariance = torch.empty(
(predicted_boxes.shape + (4,))).to(self.model.device)
return result
return result
30 changes: 11 additions & 19 deletions detection/modeling/roihead_gmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,7 @@ def __init__(
self.start_iter = self.cfg.VOS.STARTING_ITER
# print(self.sample_number, self.start_iter)

self.logistic_regression = nn.Sequential(
torch.nn.Linear(1, 512),
torch.nn.ReLU(),
torch.nn.Linear(512, 1)
)
self.logistic_regression = torch.nn.Linear(1, 2)
self.logistic_regression.cuda()
# torch.nn.init.xavier_normal_(self.logistic_regression.weight)

Expand Down Expand Up @@ -732,7 +728,6 @@ def forward_with_given_boxes(

def log_sum_exp(self, value, dim=None, keepdim=False):
"""Numerically stable implementation of the operation
value.exp().sum(dim, keepdim).log()
"""
import math
Expand Down Expand Up @@ -870,16 +865,16 @@ def _forward_box(self, features: Dict[str, torch.Tensor], proposals: List[Instan
input_for_lr = torch.cat((energy_score_for_fg, energy_score_for_bg), -1)
labels_for_lr = torch.cat((torch.ones(len(selected_fg_samples)).cuda(),
torch.zeros(len(ood_samples)).cuda()), -1)
if True:
if False:
output = self.logistic_regression(input_for_lr.view(-1, 1))
lr_reg_loss = F.binary_cross_entropy_with_logits(
output.view(-1), labels_for_lr)
#print(torch.cat((
# F.sigmoid(output[:len(selected_fg_samples)]).view(-1, 1),
# torch.ones(len(selected_fg_samples), 1).cuda()), 1))
#print(torch.cat((
# F.sigmoid(output[len(selected_fg_samples):]).view(-1, 1),
# torch.zeros(len(ood_samples), 1).cuda()), 1))
print(torch.cat((
F.sigmoid(output[:len(selected_fg_samples)]).view(-1, 1),
torch.ones(len(selected_fg_samples), 1).cuda()), 1))
print(torch.cat((
F.sigmoid(output[len(selected_fg_samples):]).view(-1, 1),
torch.zeros(len(ood_samples), 1).cuda()), 1))
else:
weights_fg_bg = torch.Tensor([len(selected_fg_samples) / float(len(input_for_lr)),
len(ood_samples) / float(len(input_for_lr))]).cuda()
Expand All @@ -899,10 +894,8 @@ def _forward_box(self, features: Dict[str, torch.Tensor], proposals: List[Instan
self.data_dict[dict_key][self.number_dict[dict_key]] = box_features[index].detach()
self.number_dict[dict_key] += 1
# create a dummy in order to have all weights to get involved in for a loss.
loss_dummy = self.cos(self.logistic_regression(torch.zeros(1).cuda()),
self.logistic_regression(torch.zeros(1).cuda()))
loss_dummy1 = self.cos(self.weight_energy(torch.zeros(self.num_classes).cuda()),
self.weight_energy(torch.zeros(self.num_classes).cuda()))
loss_dummy = self.cos(self.logistic_regression(torch.zeros(1).cuda()), self.logistic_regression.bias)
loss_dummy1 = self.cos(self.weight_energy(torch.zeros(self.num_classes).cuda()), self.weight_energy.bias)
del box_features
# print(self.number_dict)

Expand Down Expand Up @@ -1007,5 +1000,4 @@ def _forward_keypoint(self, features: Dict[str, torch.Tensor], instances: List[I
features = self.keypoint_pooler(features, boxes)
else:
features = dict([(f, features[f]) for f in self.keypoint_in_features])
return self.keypoint_head(features, instances)

return self.keypoint_head(features, instances)
8 changes: 4 additions & 4 deletions voc_coco_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@

assert len(id_data['inter_feat'][0]) == 21# + 1024
if args.energy:
id_score = -torch.stack(id_data['logistic_score'])
ood_score = -torch.stack(ood_data['logistic_score'])
#id_score = -args.T * torch.logsumexp(torch.stack(id_data['inter_feat'])[:, :-1] / args.T, dim=1).cpu().data.numpy()
#ood_score = -args.T * torch.logsumexp(torch.stack(ood_data['inter_feat'])[:, :-1] / args.T, dim=1).cpu().data.numpy()
#id_score = -torch.stack(id_data['logistic_score'])
#ood_score = -torch.stack(ood_data['logistic_score'])
id_score = -args.T * torch.logsumexp(torch.stack(id_data['inter_feat'])[:, :-1] / args.T, dim=1).cpu().data.numpy()
ood_score = -args.T * torch.logsumexp(torch.stack(ood_data['inter_feat'])[:, :-1] / args.T, dim=1).cpu().data.numpy()
else:
id_score = -np.max(F.softmax(torch.stack(id_data['inter_feat'])[:, :-1], dim=1).cpu().data.numpy(), axis=1)
ood_score = -np.max(F.softmax(torch.stack(ood_data['inter_feat'])[:, :-1], dim=1).cpu().data.numpy(), axis=1)
Expand Down

0 comments on commit 19f20b8

Please sign in to comment.