def __init__(self, in_channels, num_classes, num_anchors):
        super(PredictionHead, self).__init__()
        tile_file_cls = getTileFileFromConvDimensions(tiles_path, in_channels, num_classes*num_anchors, 1, 1)
        tile_file_rgs = getTileFileFromConvDimensions(tiles_path, in_channels, num_anchors*4, 1, 1)


        self.classification = nn.SimulatedConv2d(
            in_channels, num_classes * num_anchors, 1, simulation_file, tile_file_cls, sparsity_ratio
        )
        self.regression = nn.SimulatedConv2d(in_channels, num_anchors * 4, 1, simulation_file, tile_file_rgs, sparsity_ratio)

        self.num_classes = num_classes
        self.num_anchors = num_anchors
 def __init__(self, in_channels, mid_channels, out_channels):
     tile_file_1 = getTileFileFromConvDimensions(tiles_path, in_channels, mid_channels, 1, 1)
     tile_file_2 = getTileFileFromConvDimensions(tiles_path, mid_channels, out_channels, 3, 1)
     super(Block, self).__init__(
         nn.SimulatedConv2d(in_channels, mid_channels, 1,simulation_file, tile_file_1, sparsity_ratio),
         nn.ReLU6(),
         SimulatedConv2d_tf(
             mid_channels, out_channels, 3, simulation_file, tile_file_2, sparsity_ratio, stride=2, padding="SAME"
         ),
         nn.ReLU6(),
     )
Beispiel #3
0
    def __init__(self, block, layers, num_classes=1000, zero_init_residual=False,
                 groups=1, width_per_group=64, replace_stride_with_dilation=None,
                 norm_layer=None):
        super(ResNet, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        self._norm_layer = norm_layer

        self.inplanes = 64
        self.dilation = 1
        if replace_stride_with_dilation is None:
            # each element in the tuple indicates if we should replace
            # the 2x2 stride with a dilated convolution instead
            replace_stride_with_dilation = [False, False, False]
        if len(replace_stride_with_dilation) != 3:
            raise ValueError("replace_stride_with_dilation should be None "
                             "or a 3-element tuple, got {}".format(replace_stride_with_dilation))
        self.groups = groups
        self.base_width = width_per_group
        tile_file_1 = getTileFileFromConvDimensions(tiles_path, 3, self.inplanes, 7, 1)
        self.conv1 = nn.SimulatedConv2d(3, self.inplanes, 7, simulation_file, tile_file_1, sparsity_ratio, stride=2, padding=3, bias=False)
        self.bn1 = norm_layer(self.inplanes)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
                                       dilate=replace_stride_with_dilation[0])
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
                                       dilate=replace_stride_with_dilation[1])
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
                                       dilate=replace_stride_with_dilation[2])
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

        # Zero-initialize the last BN in each residual branch,
        # so that the residual branch starts with zeros, and each residual block behaves like an identity.
        # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
        if zero_init_residual:
            for m in self.modules():
                if isinstance(m, Bottleneck):
                    nn.init.constant_(m.bn3.weight, 0)
                elif isinstance(m, BasicBlock):
                    nn.init.constant_(m.bn2.weight, 0)
Beispiel #4
0
 def __init__(self):
     super().__init__()
     self.conv1 = nn.SimulatedConv2d(
         4,
         4,
         4,
         '../simulation_files/tpu_16_16_pes.cfg',
         'dogsandcats_tpu_tile.txt',
         sparsity_ratio=0.0,
         groups=4
     )  # Number of input feature maps (input channels), number of output feature maps (output channels), filter dimension size
     #print(self.conv1.weight)
     self.real_conv1 = nn.Conv2d(4, 4, 4, groups=4)
     self.real_conv1.weight = self.conv1.weight
     self.real_conv1.bias = self.conv1.bias
     print(self.conv1.bias.shape)
     print("Weight size is ")
     print(self.real_conv1.weight.shape)
def conv_dw(inp, oup, stride):
    tile_file_1 = getTileFileFromConvDimensions(tiles_path, inp, inp, 3, inp)
    tile_file_2 = getTileFileFromConvDimensions(tiles_path, inp, oup, 1, 1)
    return nn.Sequential(
        OrderedDict(
            [
                (
                    "depthwise",
                    SimulatedConv2d_tf(
                        inp, inp, 3, simulation_file, tile_file_1, sparsity_ratio, stride, padding="SAME", groups=inp, bias=False
                    ),
                ),
                ("depthwise/BatchNorm", BatchNorm2d(inp)),
                ("depthwise/ReLU", nn.ReLU6(inplace=True)),
                ("pointwise", nn.SimulatedConv2d(inp, oup, 1, simulation_file, tile_file_2, sparsity_ratio, 1, 0, bias=False)),
                ("pointwise/BatchNorm", BiasAdd(oup)),
                ("pointwise/ReLU", nn.ReLU6(inplace=True)),
            ]
        )
    )
Beispiel #6
0
    def __init__(self, num_classes=1000):
        super(AlexNet, self).__init__()
        '''self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )'''

        self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
        '''self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )'''

        self.features = nn.Sequential(
            nn.SimulatedConv2d(
                3,
                64,
                kernel_size=11,
                path_to_arch_file=
                '/home/paco/Desktop/PyTorch/stonne/architectures/sparse_arch_test.cfg',
                path_to_tile='tiles/tile_configuration_conv1.txt',
                sparsity_ratio=0.90,
                stride=4,
                padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.SimulatedConv2d(
                64,
                192,
                kernel_size=5,
                path_to_arch_file=
                '/home/paco/Desktop/PyTorch/stonne/architectures/sparse_arch_test.cfg',
                path_to_tile='tiles/tile_configuration_conv2.txt',
                sparsity_ratio=0.90,
                padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.SimulatedConv2d(
                192,
                384,
                kernel_size=3,
                path_to_arch_file=
                '/home/paco/Desktop/PyTorch/stonne/architectures/sparse_arch_test.cfg',
                path_to_tile='tiles/tile_configuration_conv3.txt',
                sparsity_ratio=0.90,
                padding=1),
            nn.ReLU(inplace=True),
            nn.SimulatedConv2d(
                384,
                256,
                kernel_size=3,
                path_to_arch_file=
                '/home/paco/Desktop/PyTorch/stonne/architectures/sparse_arch_test.cfg',
                path_to_tile='tiles/tile_configuration_conv4.txt',
                sparsity_ratio=0.90,
                padding=1),
            nn.ReLU(inplace=True),
            nn.SimulatedConv2d(
                256,
                256,
                kernel_size=3,
                path_to_arch_file=
                '/home/paco/Desktop/PyTorch/stonne/architectures/sparse_arch_test.cfg',
                path_to_tile='tiles/tile_configuration_conv5.txt',
                sparsity_ratio=0.90,
                padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )

        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.SimulatedLinear(
                256 * 6 * 6,
                4096,
                path_to_arch_file=
                '/home/paco/Desktop/PyTorch/stonne/architectures/sparse_arch_test.cfg',
                path_to_tile='tiles/tile_configuration_fc6.txt',
                sparsity_ratio=0.90),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.SimulatedLinear(
                4096,
                4096,
                path_to_arch_file=
                '/home/paco/Desktop/PyTorch/stonne/architectures/sparse_arch_test.cfg',
                path_to_tile='tiles/tile_configuration_fc7.txt',
                sparsity_ratio=0.90),
            nn.ReLU(inplace=True),
            nn.SimulatedLinear(
                4096,
                num_classes,
                path_to_arch_file=
                '/home/paco/Desktop/PyTorch/stonne/architectures/sparse_arch_test.cfg',
                path_to_tile='tiles/tile_configuration_fc8.txt',
                sparsity_ratio=0.90),
        )
Beispiel #7
0
def conv1x1(in_planes, out_planes, stride=1):
    """1x1 convolution"""
    tile_file_1 = getTileFileFromConvDimensions(tiles_path, in_planes, out_planes, 1, 1)
    return nn.SimulatedConv2d(in_planes, out_planes, 1, simulation_file, tile_file_1, sparsity_ratio, stride=stride, bias=False)
Beispiel #8
0
def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
    """3x3 convolution with padding"""
    tile_file_1 = getTileFileFromConvDimensions(tiles_path, in_planes, out_planes, 3, groups)
    return nn.SimulatedConv2d(in_planes, out_planes, 3, simulation_file, tile_file_1, sparsity_ratio, stride=stride,
                     padding=dilation, groups=groups, bias=False, dilation=dilation)
Beispiel #9
0
    def _build_additional_features(self, input_channels):
        idx = 0
        self.additional_blocks = []
        tile_file_1 = getTileFileFromConvDimensions(tiles_path,
                                                    input_channels[idx], 256,
                                                    3, 1)
        tile_file_2 = getTileFileFromConvDimensions(tiles_path, 256,
                                                    input_channels[idx + 1], 3,
                                                    1)
        self.additional_blocks.append(
            nn.Sequential(
                nn.SimulatedConv2d(input_channels[idx], 256, 1,
                                   simulation_file, tile_file_1,
                                   sparsity_ratio),
                nn.ReLU(inplace=True),
                nn.SimulatedConv2d(256,
                                   input_channels[idx + 1],
                                   3,
                                   simulation_file,
                                   tile_file_2,
                                   sparsity_ratio,
                                   padding=1,
                                   stride=self.strides[2]),
                nn.ReLU(inplace=True),
            ))
        idx += 1

        tile_file_1 = getTileFileFromConvDimensions(tiles_path,
                                                    input_channels[idx], 256,
                                                    1, 1)
        tile_file_2 = getTileFileFromConvDimensions(tiles_path, 256,
                                                    input_channels[idx + 1], 3,
                                                    1)
        self.additional_blocks.append(
            nn.Sequential(
                nn.SimulatedConv2d(input_channels[idx], 256, 1,
                                   simulation_file, tile_file_1,
                                   sparsity_ratio),
                nn.ReLU(inplace=True),
                nn.SimulatedConv2d(256,
                                   input_channels[idx + 1],
                                   3,
                                   simulation_file,
                                   tile_file_2,
                                   sparsity_ratio,
                                   padding=1,
                                   stride=self.strides[3]),
                nn.ReLU(inplace=True),
            ))
        idx += 1

        # conv9_1, conv9_2
        tile_file_1 = getTileFileFromConvDimensions(tiles_path,
                                                    input_channels[idx], 128,
                                                    1, 1)
        tile_file_2 = getTileFileFromConvDimensions(tiles_path, 128,
                                                    input_channels[idx + 1], 3,
                                                    1)
        self.additional_blocks.append(
            nn.Sequential(
                nn.SimulatedConv2d(input_channels[idx], 128, 1,
                                   simulation_file, tile_file_1,
                                   sparsity_ratio),
                nn.ReLU(inplace=True),
                nn.SimulatedConv2d(128,
                                   input_channels[idx + 1],
                                   3,
                                   simulation_file,
                                   tile_file_2,
                                   sparsity_ratio,
                                   padding=1,
                                   stride=self.strides[4]),
                nn.ReLU(inplace=True),
            ))
        idx += 1

        # conv10_1, conv10_2
        tile_file_1 = getTileFileFromConvDimensions(tiles_path,
                                                    input_channels[idx], 128,
                                                    1, 1)
        tile_file_2 = getTileFileFromConvDimensions(tiles_path, 128,
                                                    input_channels[idx + 1], 3,
                                                    1)
        self.additional_blocks.append(
            nn.Sequential(
                nn.SimulatedConv2d(input_channels[idx], 128, 1,
                                   simulation_file, tile_file_1,
                                   sparsity_ratio),
                nn.ReLU(inplace=True),
                nn.SimulatedConv2d(128,
                                   input_channels[idx + 1],
                                   3,
                                   simulation_file,
                                   tile_file_2,
                                   sparsity_ratio,
                                   stride=self.strides[5]),
                nn.ReLU(inplace=True),
            ))
        idx += 1

        # conv11_1, conv11_2
        tile_file_1 = getTileFileFromConvDimensions(tiles_path,
                                                    input_channels[idx], 128,
                                                    1, 1)
        tile_file_2 = getTileFileFromConvDimensions(tiles_path, 128,
                                                    input_channels[idx + 1], 3,
                                                    1)
        self.additional_blocks.append(
            nn.Sequential(
                nn.SimulatedConv2d(input_channels[idx], 128, 1,
                                   simulation_file, tile_file_1,
                                   sparsity_ratio),
                nn.ReLU(inplace=True),
                nn.SimulatedConv2d(128, input_channels[idx + 1], 3,
                                   simulation_file, tile_file_2,
                                   sparsity_ratio),
                nn.ReLU(inplace=True),
            ))

        self.additional_blocks = nn.ModuleList(self.additional_blocks)
Beispiel #10
0
    def __init__(self,
                 sim_file='../../../simulation_files/sigma_128mses_64_bw.cfg',
                 tiles='tiles/accumulation_buffer/128_mses/',
                 sparsity=0.0,
                 stats_path='',
                 label_num=81,
                 backbone='resnet34',
                 model_path="./resnet34-333f7ec4.pth",
                 strides=[3, 3, 2, 2, 2, 2],
                 extract_shapes=False):

        super(SSD_R34, self).__init__()
        # Global variables to be used in all the models
        global tiles_path
        tiles_path = tiles
        global sparsity_ratio
        sparsity_ratio = sparsity
        global simulation_file
        simulation_file = sim_file
        if (stats_path != ''):
            os.environ['OUTPUT_DIR'] = stats_path

        self.label_num = label_num
        self.strides = strides
        if backbone == 'resnet34':
            self.model = ResNet34(tiles_path, sparsity_ratio, simulation_file)
            out_channels = 256
            self.out_chan = [out_channels, 512, 512, 256, 256, 256]
        else:
            raise ValueError('Invalid backbone chosen')

        self._build_additional_features(self.out_chan)
        self.extract_shapes = extract_shapes
        # after l2norm, conv7, conv8_2, conv9_2, conv10_2, conv11_2
        # classifer 1, 2, 3, 4, 5 ,6

        self.num_defaults = [4, 6, 6, 6, 4, 4]
        self.loc = []
        self.conf = []
        for nd, oc in zip(self.num_defaults, self.out_chan):
            tile_file_1 = getTileFileFromConvDimensions(
                tiles_path, oc, nd * 4, 3, 1)
            tile_file_2 = getTileFileFromConvDimensions(
                tiles_path, oc, nd * label_num, 3, 1)
            self.loc.append(
                nn.SimulatedConv2d(oc,
                                   nd * 4,
                                   3,
                                   simulation_file,
                                   tile_file_1,
                                   sparsity_ratio,
                                   padding=1,
                                   stride=self.strides[0]))
            self.conf.append(
                nn.SimulatedConv2d(oc,
                                   nd * label_num,
                                   3,
                                   simulation_file,
                                   tile_file_2,
                                   sparsity_ratio,
                                   padding=1,
                                   stride=self.strides[1]))

        self.loc = nn.ModuleList(self.loc)
        self.conf = nn.ModuleList(self.conf)
        if not extract_shapes:
            self.size = (1200, 1200)
            dboxes = dboxes_R34_coco(list(self.size), [3, 3, 2, 2, 2, 2])
            self.encoder = Encoder(dboxes)
        # intitalize all weights
        self._init_weights()
        self.device = 1