MCPcopy Create free account
hub / github.com/explainingai-code/FasterRCNN-PyTorch / forward

Method forward

model/faster_rcnn.py:418–513  ·  view source on GitHub ↗

r""" Main method for RPN does the following: 1. Call RPN specific conv layers to generate classification and bbox transformation predictions for anchors 2. Generate anchors for entire image 3. Transform generated anchors based on predicted bbox transformat

(self, image, feat, target=None)

Source from the content-addressed store, hash-verified

416 return proposals, cls_scores
417
418 def forward(self, image, feat, target=None):
419 r"""
420 Main method for RPN does the following:
421 1. Call RPN specific conv layers to generate classification and
422 bbox transformation predictions for anchors
423 2. Generate anchors for entire image
424 3. Transform generated anchors based on predicted bbox transformation to generate proposals
425 4. Filter proposals
426 5. For training additionally we do the following:
427 a. Assign target ground truth labels and boxes to each anchors
428 b. Sample positive and negative anchors
429 c. Compute classification loss using sampled pos/neg anchors
430 d. Compute Localization loss using sampled pos anchors
431 :param image:
432 :param feat:
433 :param target:
434 :return:
435 """
436 # Call RPN layers
437 rpn_feat = nn.ReLU()(self.rpn_conv(feat))
438 cls_scores = self.cls_layer(rpn_feat)
439 box_transform_pred = self.bbox_reg_layer(rpn_feat)
440
441 # Generate anchors
442 anchors = self.generate_anchors(image, feat)
443
444 # Reshape classification scores to be (Batch Size * H_feat * W_feat * Number of Anchors Per Location, 1)
445 # cls_score -> (Batch_Size, Number of Anchors per location, H_feat, W_feat)
446 number_of_anchors_per_location = cls_scores.size(1)
447 cls_scores = cls_scores.permute(0, 2, 3, 1)
448 cls_scores = cls_scores.reshape(-1, 1)
449 # cls_score -> (Batch_Size*H_feat*W_feat*Number of Anchors per location, 1)
450
451 # Reshape bbox predictions to be (Batch Size * H_feat * W_feat * Number of Anchors Per Location, 4)
452 # box_transform_pred -> (Batch_Size, Number of Anchors per location*4, H_feat, W_feat)
453 box_transform_pred = box_transform_pred.view(
454 box_transform_pred.size(0),
455 number_of_anchors_per_location,
456 4,
457 rpn_feat.shape[-2],
458 rpn_feat.shape[-1])
459 box_transform_pred = box_transform_pred.permute(0, 3, 4, 1, 2)
460 box_transform_pred = box_transform_pred.reshape(-1, 4)
461 # box_transform_pred -> (Batch_Size*H_feat*W_feat*Number of Anchors per location, 4)
462
463 # Transform generated anchors according to box transformation prediction
464 proposals = apply_regression_pred_to_anchors_or_proposals(
465 box_transform_pred.detach().reshape(-1, 1, 4),
466 anchors)
467 proposals = proposals.reshape(proposals.size(0), 4)
468 ######################
469
470 proposals, scores = self.filter_proposals(proposals, cls_scores.detach(), image.shape)
471 rpn_output = {
472 'proposals': proposals,
473 'scores': scores
474 }
475 if not self.training or target is None:

Callers

nothing calls this directly

Tested by

no test coverage detected