Ejemplo n.º 1
0
	def __init__(
		self,
		output_size,
		scales,
		sampling_ratio,
		pooler_type,
		canonical_box_size=224,
		canonical_level=4,
	):
		"""
		Args:
			output_size (int, tuple[int] or list[int]): output size of the pooled region,
				e.g., 14 x 14. If tuple or list is given, the length must be 2.
			scales (list[float]): The scale for each low-level pooling op relative to
				the input image. For a feature map with stride s relative to the input
				image, scale is defined as a 1 / s. The stride must be power of 2.
				When there are multiple scales, they must form a pyramid, i.e. they must be
				a monotically decreasing geometric sequence with a factor of 1/2.
			sampling_ratio (int): The `sampling_ratio` parameter for the ROIAlign op.
			pooler_type (string): Name of the type of pooling operation that should be applied.
				For instance, "ROIPool" or "ROIAlignV2".
			canonical_box_size (int): A canonical box size in pixels (sqrt(box area)). The default
				is heuristically defined as 224 pixels in the FPN paper (based on ImageNet
				pre-training).
			canonical_level (int): The feature map level index from which a canonically-sized box
				should be placed. The default is defined as level 4 (stride=16) in the FPN paper,
				i.e., a box of size 224x224 will be placed on the feature with stride=16.
				The box placement for all boxes will be determined from their sizes w.r.t
				canonical_box_size. For example, a box whose area is 4x that of a canonical box
				should be used to pool features from feature level ``canonical_level+1``.

				Note that the actual input feature maps given to this module may not have
				sufficiently many levels for the input boxes. If the boxes are too large or too
				small for the input feature maps, the closest level will be used.
		"""
		super().__init__()

		if isinstance(output_size, int):
			output_size = (output_size, output_size)
		assert len(output_size) == 2
		assert isinstance(output_size[0], int) and isinstance(output_size[1], int)
		self.output_size = output_size

		if pooler_type == "ROIAlign":
			self.level_poolers = nn.ModuleList(
				ROIAlign(
					output_size, spatial_scale=scale, sampling_ratio=sampling_ratio, aligned=False
				)
				for scale in scales
			)
		elif pooler_type == "ROIAlignV2":
			self.level_poolers = nn.ModuleList(
				ROIAlign(
					output_size, spatial_scale=scale, sampling_ratio=sampling_ratio, aligned=True
				)
				for scale in scales
			)
		elif pooler_type == "ROIPool":
			self.level_poolers = nn.ModuleList(
				RoIPool(output_size, spatial_scale=scale) for scale in scales
			)
		elif pooler_type == "ROIAlignRotated":
			self.level_poolers = nn.ModuleList(
				ROIAlignRotated(output_size, spatial_scale=scale, sampling_ratio=sampling_ratio)
				for scale in scales
			)
		else:
			raise ValueError("Unknown pooler type: {}".format(pooler_type))

		# Map scale (defined as 1 / stride) to its feature map level under the
		# assumption that stride is a power of 2.
		min_level = -(math.log2(scales[0]))
		max_level = -(math.log2(scales[-1]))
		assert math.isclose(min_level, int(min_level)) and math.isclose(
			max_level, int(max_level)
		), "Featuremap stride is not power of 2!"
		self.min_level = int(min_level)
		self.max_level = int(max_level)
		assert (
			len(scales) == self.max_level - self.min_level + 1
		), "[ROIPooler] Sizes of input featuremaps do not form a pyramid!"
		assert 0 <= self.min_level and self.min_level <= self.max_level
		self.canonical_level = canonical_level
		assert canonical_box_size > 0
		self.canonical_box_size = canonical_box_size
Ejemplo n.º 2
0
 def create_roi_pool(self, scale): # 32 resnet18
     return RoIPool(self.pool_size, scale)
Ejemplo n.º 3
0
 def _roi_pool_layer(self, bottom, rois):
     return RoIPool((cfg.POOLING_SIZE, cfg.POOLING_SIZE),
                    1.0 / 16.0)(bottom, rois)
Ejemplo n.º 4
0
 def create_roi_pool(self):
     # see #64
     if self.backbone.__name__ == 'resnet18':
         return RoIPool(self.pool_size, 1 / 32)
     else:
         raise NotImplementedError
Ejemplo n.º 5
0
import torch
from torchvision.ops import RoIPool

img = torch.randn(size=(1, 3, 100, 100))
box = torch.tensor([10., 10., 20., 20.]).unsqueeze(0)
#box2 = torch.tensor([30.,30.,70.,70.]).unsqueeze(0)
boxes = [box]
#print(boxes)
pooler = RoIPool(output_size=3, spatial_scale=1.)
#print(pooler)
out_tsr = pooler(img, boxes)
print(out_tsr.size())
Ejemplo n.º 6
0
    def __init__(self, bn=False, num_classes=10):
        super(ori, self).__init__()

        self.num_classes = num_classes
        self.base_layer = nn.Sequential(
            Conv2d(1, 16, 9, same_padding=True, NL='prelu', bn=bn),
            Conv2d(16, 32, 7, same_padding=True, NL='prelu', bn=bn))

        self.hl_prior = nn.Sequential(
            Conv2d(32, 16, 9, same_padding=True, NL='prelu', bn=bn),
            nn.MaxPool2d(2),
            Conv2d(16, 32, 7, same_padding=True, NL='prelu', bn=bn),
            nn.MaxPool2d(2),
            Conv2d(32, 32, 7, same_padding=True, NL='prelu', bn=bn),
            Conv2d(32, 32, 7, same_padding=True, NL='prelu', bn=bn))

        self.roi_pool = RoIPool([16, 16], 1 / 4.0)
        self.hl_prior_conv2d = Conv2d(32,
                                      16,
                                      1,
                                      same_padding=True,
                                      NL='prelu',
                                      bn=bn)

        self.bbx_pred = nn.Sequential(FC(16 * 16 * 16, 512, NL='prelu'),
                                      FC(512, 256, NL='prelu'),
                                      FC(256, self.num_classes, NL='prelu'))

        # generate dense map
        self.den_stage_1 = nn.Sequential(
            Conv2d(32, 32, 7, same_padding=True, NL='prelu', bn=bn),
            nn.MaxPool2d(2),
            Conv2d(32, 64, 5, same_padding=True, NL='prelu', bn=bn),
            nn.MaxPool2d(2),
            Conv2d(64, 32, 5, same_padding=True, NL='prelu', bn=bn),
            Conv2d(32, 32, 5, same_padding=True, NL='prelu', bn=bn))

        self.den_stage_DULR = nn.Sequential(
            convDU(in_out_channels=32, kernel_size=(1, 9)),
            convLR(in_out_channels=32, kernel_size=(9, 1)))

        self.den_stage_2 = nn.Sequential(
            Conv2d(64, 64, 3, same_padding=True, NL='prelu', bn=bn),
            Conv2d(64, 32, 3, same_padding=True, NL='prelu', bn=bn),
            nn.ConvTranspose2d(32,
                               16,
                               4,
                               stride=2,
                               padding=1,
                               output_padding=0,
                               bias=True), nn.PReLU(),
            nn.ConvTranspose2d(16,
                               8,
                               4,
                               stride=2,
                               padding=1,
                               output_padding=0,
                               bias=True), nn.PReLU())

        # generrate seg map
        self.seg_stage = nn.Sequential(
            Conv2d(32, 32, 1, same_padding=True, NL='prelu', bn=bn),
            Conv2d(32, 64, 3, same_padding=True, NL='prelu', bn=bn),
            Conv2d(64, 32, 3, same_padding=True, NL='prelu', bn=bn),
            nn.ConvTranspose2d(32,
                               16,
                               4,
                               stride=2,
                               padding=1,
                               output_padding=0,
                               bias=True), nn.PReLU(),
            nn.ConvTranspose2d(16,
                               8,
                               4,
                               stride=2,
                               padding=1,
                               output_padding=0,
                               bias=True), nn.PReLU())

        self.seg_pred = Conv2d(8, 2, 1, same_padding=True, NL='relu', bn=bn)

        self.trans_den = Conv2d(8, 8, 1, same_padding=True, NL='relu', bn=bn)

        self.den_pred = Conv2d(16, 1, 1, same_padding=True, NL='relu', bn=bn)

        # initialize_weights(self.modules())

        weights_normal_init(self.base_layer, self.hl_prior, self.hl_prior_conv2d, self.bbx_pred, self.den_stage_1, \
                            self.den_stage_DULR, self.den_stage_2, self.trans_den, self.den_pred)
        initialize_weights(self.seg_stage, self.seg_pred)
Ejemplo n.º 7
0
    def __init__(
        self,
        output_size,
        scales,
        sampling_ratio,
        pooler_type,
        canonical_box_size=224,
        canonical_level=4,
    ):
        """
        Args:
            output_size (int, tuple[int] or list[int]): output size of the pooled region,
                e.g., 14 x 14. If tuple or list is given, the length must be 2.
            scales (list[float]): The scale for each low-level pooling op relative to
                the input image. For a feature map with stride s relative to the input
                image, scale is defined as a 1 / s.
            sampling_ratio (int): The `sampling_ratio` parameter for the ROIAlign op.
            pooler_type (string): Name of the type of pooling operation that should be applied.
                For instance, "ROIPool" or "ROIAlignV2".
            canonical_box_size (int): A canonical box size in pixels (sqrt(box area)). The default
                is heuristically defined as 224 pixels in the FPN paper (based on ImageNet
                pre-training).
            canonical_level (int): The feature map level index on which a canonically-sized box
                should be placed. The default is defined as level 4 in the FPN paper.
        """
        super().__init__()

        if isinstance(output_size, int):
            output_size = (output_size, output_size)
        assert len(output_size) == 2
        assert isinstance(output_size[0], int) and isinstance(output_size[1], int)
        self.output_size = output_size

        if pooler_type == "ROIAlign":
            self.level_poolers = nn.ModuleList(
                ROIAlign(
                    output_size, spatial_scale=scale, sampling_ratio=sampling_ratio, aligned=False
                )
                for scale in scales
            )
        elif pooler_type == "ROIAlignV2":
            self.level_poolers = nn.ModuleList(
                ROIAlign(
                    output_size, spatial_scale=scale, sampling_ratio=sampling_ratio, aligned=True
                )
                for scale in scales
            )
        elif pooler_type == "ROIPool":
            self.level_poolers = nn.ModuleList(
                RoIPool(output_size, spatial_scale=scale) for scale in scales
            )
        elif pooler_type == "ROIAlignRotated":
            self.level_poolers = nn.ModuleList(
                ROIAlignRotated(output_size, spatial_scale=scale, sampling_ratio=sampling_ratio)
                for scale in scales
            )
        else:
            raise ValueError("Unknown pooler type: {}".format(pooler_type))

        # Map scale (defined as 1 / stride) to its feature map level under the
        # assumption that stride is a power of 2.
        min_level = -math.log2(scales[0])
        max_level = -math.log2(scales[-1])
        assert math.isclose(min_level, int(min_level)) and math.isclose(max_level, int(max_level))
        self.min_level = int(min_level)
        self.max_level = int(max_level)
        assert 0 < self.min_level and self.min_level <= self.max_level
        assert self.min_level <= canonical_level and canonical_level <= self.max_level
        self.canonical_level = canonical_level
        assert canonical_box_size > 0
        self.canonical_box_size = canonical_box_size
Ejemplo n.º 8
0
def roi_pool_layer(bottom, rois):
    return RoIPool((opt.pooling_size, opt.pooling_size),
                   1.0 / opt.feat_stride)(bottom, rois)
Ejemplo n.º 9
0
    def __init__(self,
                 input_dim=(16, 32),
                 pred_input_dim=(32, 32),
                 pred_inter_dim=(32, 32),
                 cpu=False):
        super().__init__(input_dim, pred_input_dim, pred_inter_dim)
        # _r for reference, _t for test
        # in: 36x36x16  out: 36x36x16
        self.conv3_1r = conv(input_dim[0], 16, kernel_size=3, stride=1)
        # in: 36x36x16  out: 36x36x32
        self.conv3_1t = conv(input_dim[0], 32, kernel_size=3, stride=1)

        # in: 36x36x32  out: 36x36x32
        self.conv3_2t = conv(32, pred_input_dim[0], kernel_size=3, stride=1)

        if cpu:
            self.prroi_pool3r = RoIPool((3, 3), 1 / 8)
            self.prroi_pool3t = RoIPool((5, 5), 1 / 8)
        else:
            # in: 36x36x16  out:3x3x16
            self.prroi_pool3r = PrRoIPool2D(3, 3, 1 / 8)
            # in: 36x36x32  out:5x5x32
            self.prroi_pool3t = PrRoIPool2D(5, 5, 1 / 8)

        # in: 3x3x16  out:1x1x32
        self.fc3_1r = conv(16, 32, kernel_size=3, stride=1, padding=0)

        # in: 18x18x32  out: 18x18x32
        self.conv4_1r = conv(input_dim[1], 32, kernel_size=3, stride=1)
        # in: 18x18x32  out: 18x18x32
        self.conv4_1t = conv(input_dim[1], 32, kernel_size=3, stride=1)

        # in: 18x18x32  out: 18x18x32
        self.conv4_2t = conv(32, pred_input_dim[1], kernel_size=3, stride=1)

        if cpu:
            self.prroi_pool4r = RoIPool((1, 1), 1 / 16)
            self.prroi_pool4t = RoIPool((3, 3), 1 / 16)
        else:
            # in: 18x18x32  out:1x1x32
            self.prroi_pool4r = PrRoIPool2D(1, 1, 1 / 16)
            # in: 18x18x32  out: 3x3x32
            self.prroi_pool4t = PrRoIPool2D(3, 3, 1 / 16)

        # in: 1x1x64  out: 1x1x32
        self.fc34_3r = conv(32 + 32,
                            pred_input_dim[0],
                            kernel_size=1,
                            stride=1,
                            padding=0)
        # in: 1x1x64  out: 1x1x32
        self.fc34_4r = conv(32 + 32,
                            pred_input_dim[1],
                            kernel_size=1,
                            stride=1,
                            padding=0)

        # in: 5x5x32  out: 1x1x32
        self.fc3_rt = LinearBlock(pred_input_dim[0], pred_inter_dim[0], 5)
        # in: 3x3x32  out: 1x1x32
        self.fc4_rt = LinearBlock(pred_input_dim[1], pred_inter_dim[1], 3)

        # in: 1x1x64  out: 1x1x1
        self.iou_predictor = nn.Linear(pred_inter_dim[0] + pred_inter_dim[1],
                                       1,
                                       bias=True)

        # Init weights
        for m in self.modules():
            if isinstance(m, nn.Conv2d) or isinstance(
                    m, nn.ConvTranspose2d) or isinstance(m, nn.Linear):
                nn.init.kaiming_normal_(m.weight.data, mode='fan_in')
                if m.bias is not None:
                    m.bias.data.zero_()
            elif isinstance(m, nn.BatchNorm2d):
                # In earlier versions batch norm parameters was initialized with default initialization,
                # which changed in pytorch 1.2. In 1.1 and earlier the weight was set to U(0,1).
                # So we use the same initialization here.
                # m.weight.data.fill_(1)
                m.weight.data.uniform_()
                m.bias.data.zero_()
Ejemplo n.º 10
0
    def __init__(self,
                 num_classes=2,
                 in_channels=3,
                 arch='resnet101',
                 output_stride=16,
                 bn_momentum=0.9,
                 freeze_bn=False,
                 noStyle=False,
                 noGlobal=False,
                 noLocal=False,
                 pretrained=False,
                 patch_size=16,
                 patch_num=4,
                 patch_out_channel=False,
                 pross_num=28,
                 **kwargs):
        super(DeepLab, self).__init__(**kwargs)
        self.model_name = 'deeplabv3plus_' + arch

        # Setup arch
        if arch == 'resnet18':
            NotImplementedError('resnet18 backbone is not implemented yet.')
        elif arch == 'resnet34':
            NotImplementedError('resnet34 backbone is not implemented yet.')
        elif arch == 'resnet50':
            self.backbone = ResNet.resnet50(bn_momentum, pretrained)
            if in_channels != 3:
                self.backbone.conv1 = nn.Conv2d(in_channels,
                                                64,
                                                kernel_size=7,
                                                stride=2,
                                                padding=3,
                                                bias=False)
        elif arch == 'resnet101':
            self.backbone = ResNet.resnet101(bn_momentum, pretrained)
            if in_channels != 3:
                self.backbone.conv1 = nn.Conv2d(in_channels,
                                                64,
                                                kernel_size=7,
                                                stride=2,
                                                padding=3,
                                                bias=False)

        self.encoder = Encoder(bn_momentum, output_stride)
        self.decoder = Decoder(num_classes, bn_momentum)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.avgpool1 = nn.AdaptiveAvgPool2d((1, 1))
        self.patch_num = patch_num
        self.roi_pool = RoIPool((patch_size, patch_size),
                                1.0)  #RoIAlign((1,1),1.0,4)
        self.patch_size = patch_size
        self.pross_num = pross_num
        self.noStyle = noStyle
        self.noGlobal = noGlobal
        self.noLocal = noLocal

        # projection head
        '''
        self.proj = nn.Sequential(
            nn.Conv2d(256, 256, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 10, 1, bias=True)
        )
        '''
        if self.noStyle:
            self.proj = nn.Sequential(nn.Linear(256, 256), nn.ReLU(),
                                      nn.Linear(256, num_classes))
        else:
            self.proj = nn.Sequential(nn.Linear(256 * 2, 256), nn.ReLU(),
                                      nn.Linear(256, num_classes))
        if not patch_out_channel:
            patch_out_channel = num_classes
        self.proj1 = nn.Sequential(nn.Linear(num_classes, num_classes),
                                   nn.ReLU(),
                                   nn.Linear(num_classes, patch_out_channel))