-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathRPN.py
executable file
·55 lines (41 loc) · 1.89 KB
/
RPN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# Copyrigh 2018 [email protected]
# MIT Licence
from __future__ import absolute_import
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from RNNs import GRU
from basic_nodes import LinearBlock
from proposal_layer import ProposalLayer
import numpy as np
import random
import sys
from config import cfg
class RPN(nn.Module):
"""
region proposal network
"""
def __init__(self, input_dim, num_anchors_per_frame, output_dim):
super(RPN,self).__init__()
self.num_class = output_dim
self.input_dim = input_dim
self.num_anchors_per_frame = num_anchors_per_frame
# this is a global value which indicate the number of anchors used in our experiments
self.min_window_size = cfg.MIN_WINDOW_SIZE
self.max_window_size = cfg.MAX_WINDOW_SIZE
self.num_score_out = self.num_anchors_per_frame * self.num_class # 2(bg/fg) * num anchors)
self.num_bbox_out = self.num_anchors_per_frame * 2 # 2(coords) * num anchors)
self.cls_score_RPN = nn.Linear(self.input_dim, self.num_score_out, bias=True)
self.bbox_score_RPN = nn.Linear(self.input_dim, self.num_bbox_out, bias=True)
self.RPN_proposal_layer = ProposalLayer(self.num_anchors_per_frame, self.min_window_size, self.max_window_size)
def forward(self, x):
batch_size = x.size(0)
feature_len = x.size(1)
rpn_cls_score = self.cls_score_RPN(x)
rpn_cls_score = rpn_cls_score.reshape(batch_size, feature_len * self.num_anchors_per_frame, self.num_class)
rpn_bbox_pred = self.bbox_score_RPN(x)
rpn_bbox_pred = rpn_bbox_pred.reshape(batch_size, feature_len * self.num_anchors_per_frame, 2)
anchors_per_utt, proposals = self.RPN_proposal_layer(rpn_bbox_pred)
return anchors_per_utt, proposals, rpn_cls_score, rpn_bbox_pred