Exemplo n.º 1
0
 def __init__(self):
     #3 input image channels and 12 possible actionTrue
     super(FocusPolicy, self).__init__()
     self.pool_size = 23
     self.resnet = resnet(
         pretrained=True,
         num_classes=64)  # num_classes torch.rand(10,3,256,256)
     self.conv1 = nn.Conv2d(64 * 11, 64, 3)
     self.pool = nn.MaxPool2d(2, 2)
     self.conv2 = nn.Conv2d(64, 32, 3)
     self.bn1 = nn.BatchNorm2d(64)
     #self.conv4c=nn.Conv2d(16,16,3)
     #self.fc1=nn.Linear(16*9*13+16*self.pool_size*self.pool_size,120)
     self.fc1 = nn.Linear(32 * (8) * (8), 128)
     #self.fc2=nn.Linear(128,128)
     self.roialign = ROIAlign((self.pool_size, self.pool_size), 0.25, 2)
     self.action_head = nn.Linear(128, 12)
     angle = -5 * math.pi / 180
     self.theta1 = torch.tensor(
         [[math.cos(angle), math.sin(-angle), 0],
          [math.sin(angle), math.cos(angle), 0]],
         dtype=torch.float)
     angle = 5 * math.pi / 180
     self.theta2 = torch.tensor(
         [[math.cos(angle), math.sin(-angle), 0],
          [math.sin(angle), math.cos(angle), 0]],
         dtype=torch.float)
Exemplo n.º 2
0
 def __init__(self,
              output_size,
              scales,
              sampling_ratio,
              deformable=False,
              output_channel=256):
     """
     Arguments:
         output_size (list[tuple[int]] or list[int]): output size for the pooled region
         scales (list[float]): scales for each Pooler
         sampling_ratio (int): sampling ratio for ROIAlign
     """
     super(Pooler, self).__init__()
     poolers = []
     for scale in scales:
         poolers.append(
             ROIAlign(output_size,
                      spatial_scale=scale,
                      sampling_ratio=sampling_ratio)
             if not deformable else DCNPooling(spatial_scale=scale,
                                               pooled_size=output_size,
                                               no_trans=False,
                                               group_size=1,
                                               trans_std=0.1,
                                               output_dim=output_channel))
     self.poolers = nn.ModuleList(poolers)
     self.output_size = output_size
     # get the levels in the feature map by leveraging the fact that the network always
     # downsamples by a factor of 2 at each level.
     lvl_min = -torch.log2(torch.tensor(scales[0],
                                        dtype=torch.float32)).item()
     lvl_max = -torch.log2(torch.tensor(scales[-1],
                                        dtype=torch.float32)).item()
     self.map_levels = LevelMapper(lvl_min, lvl_max, canonical_scale=160)
Exemplo n.º 3
0
    def __init__(self, neighbor_expand, roi_expand, output_size, scales,
                 sampling_ratio):
        """
        Arguments:
            neighbor_expand (float): scale for enlarged proposals
            roi_expand (bool): if the output size is expanded like the proposals
            output_size (list[tuple[int]] or list[int]): output size for the pooled region
            scales (list[float]): scales for each Pooler
            sampling_ratio (int): sampling ratio for ROIAlign
        """
        super(PoolerNeighbor, self).__init__()

        self.neighbor_expand = neighbor_expand
        ## expand the output as well
        if roi_expand:
            output_size = tuple(
                [int(x * neighbor_expand) for x in output_size])

        poolers = []
        for scale in scales:
            poolers.append(
                ROIAlign(output_size,
                         spatial_scale=scale,
                         sampling_ratio=sampling_ratio))
        self.poolers = nn.ModuleList(poolers)
        self.output_size = output_size

        # get the levels in the feature map by leveraging the fact that the network always
        # downsamples by a factor of 2 at each level.
        lvl_min = -torch.log2(torch.tensor(scales[0],
                                           dtype=torch.float32)).item()
        lvl_max = -torch.log2(torch.tensor(scales[-1],
                                           dtype=torch.float32)).item()
        self.map_levels = LevelMapper(lvl_min, lvl_max)
Exemplo n.º 4
0
    def __init__(self, output_size, scales, sampling_ratio):
        """
        Arguments:
            output_size (list[tuple[int]] or list[int]): output size for the pooled region
            scales (list[float]): scales for each Pooler
            sampling_ratio (int): sampling ratio for ROIAlign
        """
        super(Pooler, self).__init__()
        poolers = []
        for scale in scales:
            poolers.append(
                ROIAlign(output_size,
                         spatial_scale=scale,
                         sampling_ratio=sampling_ratio))
        self.poolers = nn.ModuleList(poolers)
        self.output_size = output_size
        # get the levels in the feature map by leveraging the fact that the network always
        # downsamples by a factor of 2 at each level.
        lvl_min = -torch.log2(torch.tensor(scales[0],
                                           dtype=torch.float32)).item()
        lvl_max = -torch.log2(torch.tensor(scales[-1],
                                           dtype=torch.float32)).item()
        self.map_levels = LevelMapper(lvl_min, lvl_max)

        self.onnx_export = False
Exemplo n.º 5
0
def _make_roi_align(cfg, scale):
    resolution = cfg.POOLER_RESOLUTION
    sampling_ratio = cfg.POOLER_SAMPLING_RATIO

    return ROIAlign((resolution, resolution),
                    spatial_scale=scale,
                    sampling_ratio=sampling_ratio)
Exemplo n.º 6
0
    def __init__(self, pooler_type, output_size, scales, sampling_ratio=None):
        """
        Arguments:
            pooler_type (str): Type of pooling, roi_align or prpool supported
            output_size (list[tuple[int]] or list[int]): output size for the pooled region
            scales (list[float]): scales for each Pooler
            sampling_ratio (int): sampling ratio for ROIAlign
        """
        super(Pooler, self).__init__()
        poolers = []
        for scale in scales:
            if pooler_type == 'roi_align':
                poolers.append(
                    ROIAlign(output_size,
                             spatial_scale=scale,
                             sampling_ratio=sampling_ratio))
            elif pooler_type == 'prpool':
                poolers.append(PrPool(output_size, spatial_scale=scale))
            else:
                raise ValueError

        self.poolers = nn.ModuleList(poolers)
        self.output_size = output_size
        # get the levels in the feature map by leveraging the fact that the network always
        # downsamples by a factor of 2 at each level.
        lvl_min = -torch.log2(torch.tensor(scales[0],
                                           dtype=torch.float32)).item()
        lvl_max = -torch.log2(torch.tensor(scales[-1],
                                           dtype=torch.float32)).item()
        self.map_levels = LevelMapper(lvl_min, lvl_max)
Exemplo n.º 7
0
 def __init__(self, scales, output_size, sampling_ratio):
     super(SuppAlignLayer, self).__init__()
     poolers = []
     for scale in scales:
         poolers.append(
             ROIAlign(output_size,
                      spatial_scale=scale,
                      sampling_ratio=sampling_ratio))
     self.poolers = nn.ModuleList(poolers)
Exemplo n.º 8
0
 def __init__(self, output_size, scales, sampling_ratio):
     """
     Arguments:
         output_size (list[tuple[int]] or list[int]): output size for the pooled region
         scales (list[flaot]): scales for each Pooler
         sampling_ratio (int): sampling ratio for ROIAlign
     """
     super(IndividualPooler, self).__init__()
     poolers = []
     for scale in scales:
         poolers.append(
             ROIAlign(output_size,
                      spatial_scale=scale,
                      sampling_ratio=sampling_ratio))
     self.poolers = nn.ModuleList(poolers)
     self.output_size = output_size
Exemplo n.º 9
0
 def __init__(self, output_sizes, scales, sampling_ratio):
     """
     Arguments:
         output_size (list[tuple[int]] or list[int]): output size for the pooled region
         scales (list[float]): scales for each Pooler
         sampling_ratio (int): sampling ratio for ROIAlign
     """
     super(Encoder_Pooler, self).__init__()
     assert len(output_sizes) == len(scales)
     poolers = []
     for output_size, scale in zip(output_sizes, scales):
         poolers.append(
             ROIAlign((output_size, output_size),
                      spatial_scale=scale,
                      sampling_ratio=sampling_ratio))
     self.poolers = nn.ModuleList(poolers)
     self.output_sizes = output_sizes
Exemplo n.º 10
0
 def __init__(self,
              output_size,
              scales,
              sampling_ratio,
              output_channel=256,
              canonical_scale=160,
              mode='align'):
     """
     Arguments:
         output_size (list[tuple[int]] or list[int]): output size for the pooled region
         scales (list[float]): scales for each Pooler
         sampling_ratio (int): sampling ratio for ROIAlign
     """
     super(Pooler, self).__init__()
     poolers = []
     for scale in scales:
         if mode == 'align':
             pooler = ROIAlign(output_size,
                               spatial_scale=scale,
                               sampling_ratio=sampling_ratio)
         elif mode == 'deformable':
             pooler = ModulatedDeformRoIPoolingPack(
                 spatial_scale=scale,
                 out_size=output_size[0],
                 out_channels=output_channel,
                 no_trans=False,
                 group_size=1,
                 trans_std=0.1)
         elif mode == 'bezier':
             pooler = BezierAlign(output_size,
                                  spatial_scale=scale,
                                  sampling_ratio=1)
         else:
             raise NotImplementedError()
         poolers.append(pooler)
     self.poolers = nn.ModuleList(poolers)
     self.output_size = output_size
     # get the levels in the feature map by leveraging the fact that the network always
     # downsamples by a factor of 2 at each level.
     lvl_min = -torch.log2(torch.tensor(scales[0],
                                        dtype=torch.float32)).item()
     lvl_max = -torch.log2(torch.tensor(scales[-1],
                                        dtype=torch.float32)).item()
     self.map_levels = LevelMapper(lvl_min,
                                   lvl_max,
                                   canonical_scale=canonical_scale)
Exemplo n.º 11
0
 def __init__(self, output_size, scales, sampling_ratio, drop_last):
     """
     Arguments:
         output_size (list[tuple[int]] or list[int]): output size for the pooled region
         scales (list[flaot]): scales for each Pooler
         sampling_ratio (int): sampling ratio for ROIAlign
         drop_last (bool): if passed, drop the last feature map
     """
     super(Pooler, self).__init__()
     poolers = []
     for scale in scales:
         poolers.append(
             ROIAlign(output_size,
                      spatial_scale=scale,
                      sampling_ratio=sampling_ratio))
     self.poolers = nn.ModuleList(poolers)
     self.drop_last = drop_last
Exemplo n.º 12
0
def build_roi_align(cfg, head_name):
    """
    Arguments:
        output_size (list[tuple[int]] or list[int]): output size for the pooled region
        scales (list[float]): scales for each Pooler
        sampling_ratio (int): sampling ratio for ROIAlign
    """
    resolution = cfg.MODEL[head_name].POOLER_RESOLUTION
    scales = cfg.MODEL[head_name].POOLER_SCALES
    sampling_ratio = cfg.MODEL[head_name].POOLER_SAMPLING_RATIO
    output_size = (resolution, resolution)
    use_torchvision = cfg.MODEL[head_name].USE_TORCH_VISION_POOLING

    poolers = []
    for scale in scales:
        pooler = ROIAlign(output_size, spatial_scale=scale, sampling_ratio=sampling_ratio, use_torchvision=use_torchvision)
        poolers.append(pooler)
    return poolers
Exemplo n.º 13
0
    def __init__(self,
                 output_size,
                 scales,
                 sampling_ratio,
                 level_map='scale',
                 level_map_kwargs=None):  # add by hui
        """
        Arguments:
            output_size (list[tuple[int]] or list[int]): output size for the pooled region
            scales (list[float]): scales for each Pooler
            sampling_ratio (int): sampling ratio for ROIAlign

            # add by hui
            level_map: 'scale' mean origin FPN map;
                    'fixed' will use given 'level_min' and 'level_max' in level_map_kwargs, such as, 2, 5 mean use P2~P5
        """
        super(Pooler, self).__init__()
        poolers = []
        for scale in scales:
            poolers.append(
                ROIAlign(output_size,
                         spatial_scale=scale,
                         sampling_ratio=sampling_ratio))
        self.poolers = nn.ModuleList(poolers)
        self.output_size = output_size
        # get the levels in the feature map by leveraging the fact that the network always
        # downsamples by a factor of 2 at each level.
        # ######################## changed by hui ################################################
        if level_map == 'scale':
            lvl_min = -torch.log2(torch.tensor(scales[0],
                                               dtype=torch.float32)).item()
            lvl_max = -torch.log2(torch.tensor(scales[-1],
                                               dtype=torch.float32)).item()
            self.map_levels = LevelMapper(lvl_min, lvl_max)
        elif level_map == 'fixed':
            lvl_min, lvl_max = level_map_kwargs.LEVEL_MIN, level_map_kwargs.LEVEL_MAX
            self.map_levels = LevelMapper(lvl_min, lvl_max)
Exemplo n.º 14
0
 def __init__(self,
              output_size,
              scales,
              sampling_ratio,
              in_channels=512,
              cat_all_levels=False):
     """
     Arguments:
         output_size (list[tuple[int]] or list[int]): output size for the pooled region
         scales (list[float]): scales for each Pooler
         sampling_ratio (int): sampling ratio for ROIAlign
     """
     super(Pooler, self).__init__()
     poolers = []
     for scale in scales:
         poolers.append(
             ROIAlign(output_size,
                      spatial_scale=scale,
                      sampling_ratio=sampling_ratio))
     self.poolers = nn.ModuleList(poolers)
     self.output_size = output_size
     self.cat_all_levels = cat_all_levels
     # get the levels in the feature map by leveraging the fact that the network always
     # downsamples by a factor of 2 at each level.
     lvl_min = -torch.log2(torch.tensor(scales[0],
                                        dtype=torch.float32)).item()
     lvl_max = -torch.log2(torch.tensor(scales[-1],
                                        dtype=torch.float32)).item()
     self.map_levels = LevelMapper(lvl_min, lvl_max)
     # reduce the channels
     if self.cat_all_levels:
         self.reduce_channel = make_conv3x3(in_channels * len(self.poolers),
                                            in_channels,
                                            dilation=1,
                                            stride=1,
                                            use_relu=True)
Exemplo n.º 15
0
    return x


import numpy as np
import cv2
from sklearn.externals import joblib
import torch.nn as nn
import torch.nn.functional as F
from maskrcnn_benchmark.layers import nms as _box_nms
from maskrcnn_benchmark.layers import ROIAlign
from MTCNN.tool.P_net import P_net
from MTCNN.tool.R_net import R_net
from MTCNN.tool.config import Config
from datetime import datetime

roialign_24 = ROIAlign((24, 24), 1 / 1., 2)


def loc2bbox(pre_loc, anchor):
    c_hw = anchor[..., 2:4] - anchor[..., 0:2]
    hw = pre_loc[..., 2:4] * c_hw
    yx1 = anchor[..., :2] + pre_loc[..., :2] * c_hw
    yx2 = yx1 + hw
    bboxes = torch.cat((yx1, yx2), dim=-1)
    return bboxes


def bbox2square(bboxes):
    if bboxes.shape[0] > 0:
        wh = bboxes[:, 2:4] - bboxes[:, :2]
        wh, _ = wh.max(dim=-1)
Exemplo n.º 16
0
        x = x.cuda()
    return x


import numpy as np
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from maskrcnn_benchmark.layers import ROIAlign
from MTCNN.tool.R_net import R_net
from MTCNN.tool.read_Data_step2 import Read_Data
from MTCNN.tool.torch_ATC import AnchorTargetCreator
from MTCNN.tool.config import Config
from datetime import datetime

roialign = ROIAlign((24, 24), 1 / 1., 2)
ce_loss = nn.CrossEntropyLoss(reduction='none')
mse_loss = nn.MSELoss()


def bbox2square(bboxes):
    if bboxes.shape[0] > 0:
        wh = bboxes[..., 2:4] - bboxes[..., :2]
        wh, _ = wh.max(dim=-1)
        wh = wh[:, None]
        xy = (bboxes[..., 2:4] + bboxes[..., :2]) / 2
        xy1 = xy - wh / 2
        xy1 = torch.max(xy1, cuda(torch.Tensor([0])))
        xy2 = xy1 + wh
        bboxes = torch.cat([xy1, xy2], dim=-1)
    return bboxes
Exemplo n.º 17
0
from pytorch_object_detection.tool.torch_ATC_FPN import AnchorTargetCreator
from pytorch_object_detection.tool.torch_PC_FPN import ProposalCreator
from pytorch_object_detection.tool.torch_PTC_mask import ProposalTargetCreator
from maskrcnn_benchmark.layers import ROIAlign
from pytorch_object_detection.tool.resnet import resnet101
from pytorch_object_detection.tool.FPN_net import FPN_net
from pytorch_object_detection.tool.FPN_RPN import RPN_net
from pytorch_object_detection.tool.FPN_Fast import Fast_net
from pytorch_object_detection.tool.Mask_net import Mask_net
import torch.nn.functional as F
from pytorch_object_detection.tool.read_Data_mask import Read_Data
from torch.utils.data import Dataset, DataLoader

ce_loss = nn.CrossEntropyLoss()
bce_loss = nn.BCEWithLogitsLoss()
roialign_list_7 = [ROIAlign((7, 7), 1 / 4., 2), ROIAlign((7, 7), 1 / 8., 2), ROIAlign((7, 7), 1 / 16., 2),
                   ROIAlign((7, 7), 1 / 32., 2)]
roialign_list_14 = [ROIAlign((14, 14), 1 / 4., 2), ROIAlign((14, 14), 1 / 8., 2), ROIAlign((14, 14), 1 / 16., 2),
                    ROIAlign((14, 14), 1 / 32., 2)]

roialign_28 = ROIAlign((28, 28), 1 / 1., 2)


def SmoothL1Loss(net_loc_train, loc, sigma, num):
    t = torch.abs(net_loc_train - loc)
    a = t[t < 1]
    b = t[t >= 1]
    loss1 = (a * sigma) ** 2 / 2
    loss2 = b - 0.5 / sigma ** 2
    loss = (loss1.sum() + loss2.sum()) / num
    return loss
from cascade_rcnn.tool.config import Config

from cascade_rcnn.tool.get_anchors import get_anchors
from cascade_rcnn.tool.torch_ATC_test import AnchorTargetCreator
from cascade_rcnn.tool.torch_PC_test import ProposalCreator
from cascade_rcnn.tool.torch_PTC_test import ProposalTargetCreator
from maskrcnn_benchmark.layers import ROIAlign
from torchvision.models import vgg16
from cascade_rcnn.tool.RPN_net import RPN_net
from cascade_rcnn.tool.Fast_net import Fast_net
import torch.nn.functional as F
from cascade_rcnn.tool.read_Data import Read_Data
from torch.utils.data import DataLoader

ce_loss = nn.CrossEntropyLoss()
roialign = ROIAlign((7, 7), 1 / 16., 2)


def SmoothL1Loss(net_loc_train, loc, sigma, num):
    t = torch.abs(net_loc_train - loc)
    a = t[t < 1]
    b = t[t >= 1]
    loss1 = (a * sigma)**2 / 2
    loss2 = b - 0.5 / sigma**2
    loss = (loss1.sum() + loss2.sum()) / num
    return loss


class Faster_Rcnn(nn.Module):
    def __init__(self, config):
        super(Faster_Rcnn, self).__init__()
Exemplo n.º 19
0
        x = x.cuda()
    return x


import numpy as np
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from maskrcnn_benchmark.layers import ROIAlign
from MTCNN.tool.P_net import P_net
from MTCNN.tool.read_Data_step1 import Read_Data
from MTCNN.tool.torch_ATC import AnchorTargetCreator
from MTCNN.tool.config import Config
from datetime import datetime

roialign = ROIAlign((12, 12), 1 / 1., 2)
ce_loss = nn.CrossEntropyLoss(reduction='none')
mse_loss = nn.MSELoss()


def get_xy(N):
    t = np.arange(N)
    x, y = np.meshgrid(t, t)
    x = x[..., None]
    y = y[..., None]
    xy = np.concatenate((x, y), axis=-1)

    return xy


def get_anchors(N):
Exemplo n.º 20
0
        super(Fast_net, self).__init__()
        self.Linear1 = nn.Linear(2048, num_classes + 1)
        self.Linear2 = nn.Linear(2048, (num_classes + 1) * 4)
        nn.init.normal_(self.Linear1.weight, std=0.01)
        nn.init.normal_(self.Linear2.weight, std=0.001)
        pass

    def forward(self, x):
        fast_logits = self.Linear1(x)
        fast_loc = self.Linear2(x)
        fast_loc = fast_loc.view(fast_loc.shape[0], -1, 4)
        return fast_logits, fast_loc


ce_loss = nn.CrossEntropyLoss()
roialign = ROIAlign((14, 14), 1 / 16., 2)


def SmoothL1Loss(net_loc_train, loc, sigma, num):
    t = torch.abs(net_loc_train - loc)
    a = t[t < 1]
    b = t[t >= 1]
    loss1 = (a * sigma)**2 / 2
    loss2 = b - 0.5 / sigma**2
    loss = (loss1.sum() + loss2.sum()) / num
    return loss


def conv3x3(in_planes, out_planes, stride=1):
    """3x3 convolution with padding"""
    return nn.Conv2d(in_planes,
Exemplo n.º 21
0
        x = x.cuda()
    return x


import numpy as np
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from maskrcnn_benchmark.layers import ROIAlign
from MTCNN.tool.O_net import O_net
from MTCNN.tool.read_Data_step2 import Read_Data
from MTCNN.tool.torch_ATC import AnchorTargetCreator
from MTCNN.tool.config import Config
from datetime import datetime

roialign = ROIAlign((48, 48), 1 / 1., 2)
ce_loss = nn.CrossEntropyLoss(reduction='none')
mse_loss = nn.MSELoss()


def bbox2square(bboxes):
    if bboxes.shape[0] > 0:
        wh = bboxes[..., 2:4] - bboxes[..., :2]
        wh, _ = wh.max(dim=-1)
        wh = wh[:, None]
        xy = (bboxes[..., 2:4] + bboxes[..., :2]) / 2
        xy1 = xy - wh / 2
        xy1 = torch.max(xy1, cuda(torch.Tensor([0])))
        xy2 = xy1 + wh
        bboxes = torch.cat([xy1, xy2], dim=-1)
    return bboxes
Exemplo n.º 22
0
import numpy as np
import cv2
import codecs
from sklearn.externals import joblib
import torch.nn.functional as F
from maskrcnn_benchmark.layers import nms as _box_nms
from maskrcnn_benchmark.layers import ROIAlign
from MTCNN.tool.P_net import P_net
from MTCNN.tool.R_net import R_net
from MTCNN.tool.O_net import O_net
from MTCNN.tool.nms import nms, py_nms
from MTCNN.tool.config import Config
from datetime import datetime

roialign_24 = ROIAlign((24, 24), 1 / 1., 2)
roialign_48 = ROIAlign((48, 48), 1 / 1., 2)


def loc2bbox(pre_loc, anchor):
    c_hw = anchor[..., 2:4] - anchor[..., 0:2]
    hw = pre_loc[..., 2:4] * c_hw
    yx1 = anchor[..., :2] + pre_loc[..., :2] * c_hw
    yx2 = yx1 + hw
    bboxes = torch.cat((yx1, yx2), dim=-1)
    return bboxes


def landmarks2landmark2(bboxes, landmarks):
    xy1 = bboxes[..., :2]
    wh = bboxes[..., 2:4] - xy1
Exemplo n.º 23
0
    def get_P_net_res(self, x):

        h, w = x.shape[2:]

        roi = cuda(torch.tensor([[0, 0, 0, w, h]]).float())
        i = 0
        all_bboxes = []
        all_score = []
        while True:
            n_h, n_w = int(h * self.scale ** i), int(w * self.scale ** i)
            if n_h < 12 or n_w < 12:
                break

            roialign = ROIAlign((n_h, n_w), 1 / 1., 2)
            xx = roialign(x, roi)

            # xx = F.interpolate(x, size=(n_h, n_w))

            a = np.ceil(n_h / 12.) * 12
            b = np.ceil(n_w / 12.) * 12
            a = int(a)
            b = int(b)
            xx = F.pad(xx, (0, b - n_w, 0, a - n_h), mode='constant', value=127.5)
            xx = (xx - 127.5) / 128.0

            P_net_logits, P_net_loc, P_net_landmarks = self.P_net(xx)

            map_H, map_W = P_net_logits.shape[2:]
            P_net_logits = P_net_logits.permute(0, 2, 3, 1).contiguous().view(-1, 2)
            P_net_loc = P_net_loc.permute(0, 2, 3, 1).contiguous().view(-1, 4)
            P_net_landmarks = P_net_landmarks.permute(0, 2, 3, 1).contiguous().view(-1, 10)
            anchors = self.anchors[:map_H, :map_W].contiguous().view(-1, 4) / self.scale ** i
            i += 1

            score = F.softmax(P_net_logits, dim=-1)[..., 1]
            inds = score >= self.config.P_net_conf_thresh
            if inds.sum() == 0:
                continue

            score = score[inds]
            P_net_loc = P_net_loc[inds]

            anchors = anchors[inds]
            bboxes = loc2bbox(P_net_loc, anchors)
            bboxes[..., slice(0, 4, 2)] = torch.clamp(bboxes[..., slice(0, 4, 2)], 0, w - 1)
            bboxes[..., slice(1, 4, 2)] = torch.clamp(bboxes[..., slice(1, 4, 2)], 0, h - 1)

            hw = bboxes[..., 2:4] - bboxes[..., :2]
            inds = hw >= self.config.roi_min_size[0]
            inds = inds.all(dim=-1)
            if inds.sum() == 0:
                continue

            bboxes = bboxes[inds]
            score = score[inds]

            score, inds = score.sort(descending=True)
            bboxes = bboxes[inds]
            keep = _box_nms(bboxes, score, 0.5)
            score = score[keep]
            bboxes = bboxes[keep]

            all_bboxes.append(bboxes)
            all_score.append(score)
        if len(all_bboxes) == 0:
            return cuda(torch.zeros((0, 5)))

        bboxes = torch.cat(all_bboxes, dim=0)
        score = torch.cat(all_score, dim=0)

        score, inds = score.sort(descending=True)
        bboxes = bboxes[inds]
        keep = _box_nms(bboxes, score, self.config.P_net_iou_thresh)
        bboxes = bboxes[keep]
        score = score[keep]

        return torch.cat([bboxes, score.view(-1, 1)], dim=1)
Exemplo n.º 24
0
import torch.nn as nn
import torch.nn.functional as F
# torch.backends.cudnn.benchmark =True
from cascade_rcnn.tool.config import Config
from cascade_rcnn.tool.get_anchors import get_anchors
from cascade_rcnn.tool.torch_PC_FPN import ProposalCreator
from maskrcnn_benchmark.layers import ROIAlign
from cascade_rcnn.tool.resnet import resnet101
from cascade_rcnn.tool.FPN_net import FPN_net
from cascade_rcnn.tool.FPN_RPN import RPN_net
from cascade_rcnn.tool.FPN_Fast import Fast_net
from cascade_rcnn.tool.Mask_net import Mask_net
from cascade_rcnn.tool.cascade_predict import predict

roialign_list_7 = [
    ROIAlign((7, 7), 1 / 4., 2),
    ROIAlign((7, 7), 1 / 8., 2),
    ROIAlign((7, 7), 1 / 16., 2),
    ROIAlign((7, 7), 1 / 32., 2)
]
roialign_list_14 = [
    ROIAlign((14, 14), 1 / 4., 2),
    ROIAlign((14, 14), 1 / 8., 2),
    ROIAlign((14, 14), 1 / 16., 2),
    ROIAlign((14, 14), 1 / 32., 2)
]

roialign_28 = ROIAlign((28, 28), 1 / 1., 2)


def SmoothL1Loss(net_loc_train, loc, sigma, num):
Exemplo n.º 25
0
# torch.backends.cudnn.benchmark =True
from cascade_rcnn.tool.config import Config

from cascade_rcnn.tool.get_anchors import get_anchors

from cascade_rcnn.tool.torch_PC_FPN import ProposalCreator

from maskrcnn_benchmark.layers import ROIAlign
from cascade_rcnn.tool.resnet import resnet101
from cascade_rcnn.tool.FPN_net import FPN_net
from cascade_rcnn.tool.FPN_RPN import RPN_net
from cascade_rcnn.tool.FPN_Fast import Fast_net
from cascade_rcnn.tool.faster_predict import predict
import torch.nn.functional as F

roialign_list = [ROIAlign((7, 7), 1 / 4., 2), ROIAlign((7, 7), 1 / 8., 2), ROIAlign((7, 7), 1 / 16., 2),
                 ROIAlign((7, 7), 1 / 32., 2)]


class Faster_Rcnn(nn.Module):
    def __init__(self, config):
        super(Faster_Rcnn, self).__init__()
        self.config = config
        self.Mean = torch.tensor(config.Mean, dtype=torch.float32)
        self.num_anchor = len(config.anchor_scales) * len(config.anchor_ratios)
        self.anchors = []
        self.num_anchor = []
        for i in range(5):
            self.num_anchor.append(len(config.anchor_scales[i]) * len(config.anchor_ratios[i]))
            stride = 4 * 2 ** i
            print(stride, self.config.anchor_scales[i], self.config.anchor_ratios[i])