예제 #1
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 num_outs,
                 activation=None,
                 start_level=0,
                 end_level=-1,
                 add_extra_convs=None,
                 extra_convs_on_inputs=None,
                 relu_before_extra_convs=None):
        """Init FPN.

        :param desc: config dict
        """
        super(FPN, self).__init__()
        self.num_ins = len(in_channels)
        self.num_outs = num_outs
        self.start_level = start_level
        self.add_extra_convs = add_extra_convs
        self.extra_convs_on_inputs = extra_convs_on_inputs
        self.relu_before_extra_convs = relu_before_extra_convs
        if end_level == -1:
            self.backbone_end_level = self.num_ins
            assert self.num_outs >= self.num_ins - self.start_level
        else:
            self.backbone_end_level = end_level
            assert end_level <= len(self.in_channels)
            assert self.num_outs == end_level - self.start_level
        self.lateral_convs = ops.MoudleList()
        self.fpn_convs = ops.MoudleList()
        for i in range(self.start_level, self.backbone_end_level):
            l_conv = ConvModule(in_channels[i],
                                out_channels,
                                1,
                                activation=activation,
                                inplace=False)
            fpn_conv = ConvModule(out_channels,
                                  out_channels,
                                  3,
                                  padding=1,
                                  activation=activation,
                                  inplace=False)
            self.lateral_convs.append(l_conv)
            self.fpn_convs.append(fpn_conv)
        extra_levels = self.num_outs - self.backbone_end_level + self.start_level
        if self.add_extra_convs and extra_levels >= 1:
            for i in range(extra_levels):
                if i == 0 and self.extra_convs_on_inputs:
                    in_channels = in_channels[self.backbone_end_level - 1]
                else:
                    in_channels = out_channels
                extra_fpn_conv = ConvModule(in_channels,
                                            out_channels,
                                            3,
                                            stride=2,
                                            padding=1,
                                            activation=activation,
                                            inplace=False)
                self.fpn_convs.append(extra_fpn_conv)
예제 #2
0
파일: erdb_esr.py 프로젝트: ylfzr/vega
    def __init__(self, InChannel, OutChannel, growRate, nConvLayers, kSize=3):
        """Initialize Block.

        :param InChannel: channel number of input
        :type InChannel: int
        :param OutChannel: channel number of output
        :type OutChannel: int
        :param growRate: growth rate of block
        :type growRate: int
        :param nConvLayers: the number of convlution layer
        :type nConvLayers: int
        :param kSize: kernel size of convolution operation
        :type kSize: int
        """
        super(Cont_RDB, self).__init__()
        self.InChan = InChannel
        self.OutChan = OutChannel
        self.G = growRate
        self.C = nConvLayers
        if self.InChan != self.G:
            self.InConv = ops.Conv2d(self.InChan,
                                     self.G,
                                     1,
                                     padding=0,
                                     stride=1)
        if self.OutChan != self.G and self.OutChan != self.InChan:
            self.OutConv = ops.Conv2d(self.InChan,
                                      self.OutChan,
                                      1,
                                      padding=0,
                                      stride=1)
        self.pool = ops.AvgPool2d(2, 2)
        self.shup = ops.PixelShuffle(2)
        self.Convs = ops.MoudleList()
        self.ShrinkConv = ops.MoudleList()
        for i in range(self.C):
            self.Convs.append(
                Sequential(
                    ops.Conv2d(self.G,
                               self.G,
                               kSize,
                               padding=(kSize - 1) // 2,
                               stride=1), ops.Relu()))
            if i < (self.C - 1):
                self.ShrinkConv.append(
                    ops.Conv2d((2 + i) * self.G,
                               self.G,
                               1,
                               padding=0,
                               stride=1))
            else:
                self.ShrinkConv.append(
                    ops.Conv2d(int((2 + i) * self.G / 4),
                               self.OutChan,
                               1,
                               padding=0,
                               stride=1))
예제 #3
0
    def __init__(self, op_names, config, inp, repeats=1, concat=False):
        """Construct ContextualCell_v1 class.

        :param op_names: list of operation indices
        :param config: list of config numbers
        :param inp: input channel
        :param repeats: number of repeated times
        :param concat: concat the result if set to True, otherwise add the result
        """
        super(ContextualCell_v1, self).__init__()
        self.ops = ops.MoudleList()
        self._pos = []
        self._collect_inds = [0]
        self._pools = ['x']
        for ind, op in enumerate(config):
            # first op is always applied on x
            if ind == 0:
                pos = 0
                op_id = op
                self._collect_inds.remove(pos)
                op_name = op_names[op_id]
                # turn-off scaling in batch norm
                self.ops.append(OPS[op_name](inp, 1, True, repeats))
                self._pos.append(pos)
                self._collect_inds.append(ind + 1)
                self._pools.append('{}({})'.format(op_name, self._pools[pos]))
            else:
                pos1, pos2, op_id1, op_id2 = op
                # drop op_id from loose ends
                for ind2, (pos, op_id) in enumerate(
                        zip([pos1, pos2], [op_id1, op_id2])):
                    if pos in self._collect_inds:
                        self._collect_inds.remove(pos)
                    op_name = op_names[op_id]
                    # turn-off scaling in batch norm
                    self.ops.append(OPS[op_name](inp, 1, True, repeats))
                    self._pos.append(pos)
                    # self._collect_inds.append(ind * 3 + ind2 - 1) # Do not collect intermediate
                    self._pools.append('{}({})'.format(op_name,
                                                       self._pools[pos]))
                # summation
                op_name = 'sum'
                self.ops.append(
                    AggregateCell(size_1=None,
                                  size_2=None,
                                  agg_size=inp,
                                  pre_transform=False,
                                  concat=concat))  # turn-off convbnrelu
                self._pos.append([ind * 3 - 1, ind * 3])
                self._collect_inds.append(ind * 3 + 1)
                self._pools.append('{}({},{})'.format(op_name,
                                                      self._pools[ind * 3 - 1],
                                                      self._pools[ind * 3]))
예제 #4
0
파일: erdb_esr.py 프로젝트: ylfzr/vega
    def __init__(self, arch, G0, kSize):
        """Create ERDBLayer.

        :param arch: arch
        :type arch: dict
        :param G0: G0
        :type G0: G0
        :param kSize: kSize
        :type kSize: int
        """
        super(ERDBLayer, self).__init__()
        self.SFENet2 = ops.Conv2d(G0,
                                  G0,
                                  kSize,
                                  padding=(kSize - 1) // 2,
                                  stride=1)
        b_in_chan = G0
        b_out_chan = 0
        Conc_all = 0
        ERDBs = ops.MoudleList()
        for i in range(len(arch)):
            name = arch[i]
            key = name.split('_')
            if i > 0:
                b_in_chan = b_out_chan
            b_conv_num = int(key[1])
            b_grow_rat = int(key[2])
            b_out_chan = int(key[3])
            Conc_all += b_out_chan
            if key[0] == 'S':
                ERDBs.append(
                    Shrink_RDB(InChannel=b_in_chan,
                               OutChannel=b_out_chan,
                               growRate=b_grow_rat,
                               nConvLayers=b_conv_num))
            elif key[0] == 'G':
                ERDBs.append(
                    Group_RDB(InChannel=b_in_chan,
                              OutChannel=b_out_chan,
                              growRate=b_grow_rat,
                              nConvLayers=b_conv_num))
            elif key[0] == 'C':
                ERDBs.append(
                    Cont_RDB(InChannel=b_in_chan,
                             OutChannel=b_out_chan,
                             growRate=b_grow_rat,
                             nConvLayers=b_conv_num))
        self.ERBD = ERDBs
        self.GFF = Sequential(
            ops.Conv2d(Conc_all, G0, 1, padding=0, stride=1),
            ops.Conv2d(G0, G0, kSize, padding=(kSize - 1) // 2, stride=1))
예제 #5
0
    def __init__(self,
                 in_channels=[64, 128, 256, 512],
                 out_channels=256,
                 num_outs=5,
                 activation=None,
                 start_level=0,
                 end_level=-1):
        """Init FPN.

        :param desc: config dict
        """
        super(FPN, self).__init__()
        self.num_ins = len(in_channels)
        self.num_outs = num_outs
        self.start_level = start_level
        if end_level == -1:
            self.backbone_end_level = self.num_ins
            assert self.num_outs >= self.num_ins - self.start_level
        else:
            self.backbone_end_level = end_level
            assert end_level <= len(self.in_channels)
            assert self.num_outs == end_level - self.start_level
        self.lateral_convs = ops.MoudleList()
        self.fpn_convs = ops.MoudleList()
        for i in range(self.start_level, self.backbone_end_level):
            l_conv = ConvModule(in_channels[i],
                                out_channels,
                                1,
                                activation=activation,
                                inplace=False)
            fpn_conv = ConvModule(out_channels,
                                  out_channels,
                                  3,
                                  padding=1,
                                  activation=activation,
                                  inplace=False)
            self.lateral_convs.append(l_conv)
            self.fpn_convs.append(fpn_conv)