Beispiel #1
0
    def __init__(self):
        super(Net, self).__init__()
        # Convolution Block 1
        self.conv1 = nn.Sequential(
            nn.Conv2d(in_channels=3,
                      out_channels=32,
                      kernel_size=3,
                      padding=1,
                      dilation=2,
                      bias=False), nn.ReLU(), nn.BatchNorm2d(32),
            nn.Dropout2d(0.03))
        # output_size = 32, RF = 3
        self.conv2 = nn.Sequential(
            nn.Conv2d(in_channels=32,
                      out_channels=32,
                      kernel_size=3,
                      padding=1,
                      bias=False), nn.ReLU(), nn.BatchNorm2d(32),
            nn.Dropout2d(0.03))
        # output_size = 32, RF = 5
        self.conv3 = nn.Sequential(
            nn.Conv2d(in_channels=32,
                      out_channels=32,
                      kernel_size=3,
                      padding=1,
                      bias=False), nn.ReLU(), nn.BatchNorm2d(32),
            nn.Dropout2d(0.03))
        # output_size = 32, RF = 7
        self.pool1 = nn.MaxPool2d(2, 2)  # 16
        # output_size = 16, RF = 8

        # Convolution Block 2
        self.conv4 = nn.Sequential(
            nn.Conv2d(in_channels=32,
                      out_channels=64,
                      kernel_size=3,
                      padding=1,
                      groups=32,
                      bias=False), nn.ReLU(), nn.BatchNorm2d(64),
            nn.Dropout2d(0.03))
        # output_size = 16, RF = 12
        self.conv5 = nn.Sequential(
            nn.Conv2d(in_channels=64,
                      out_channels=64,
                      kernel_size=3,
                      padding=1,
                      bias=False), nn.ReLU(), nn.BatchNorm2d(64),
            nn.Dropout2d(0.03))
        # output_size = 16, RF = 16
        self.conv6 = nn.Sequential(
            nn.Conv2d(in_channels=64,
                      out_channels=64,
                      kernel_size=3,
                      padding=1,
                      bias=False), nn.ReLU(), nn.BatchNorm2d(64),
            nn.Dropout2d(0.03))
        # output_size = 16, RF = 20
        self.pool2 = nn.MaxPool2d(2, 2)  # 8
        # output_size = 8, RF = 21

        # Convolution Block 3
        self.conv7 = nn.Sequential(
            nn.Conv2d(in_channels=64,
                      out_channels=128,
                      kernel_size=3,
                      padding=1,
                      bias=False), nn.ReLU(), nn.BatchNorm2d(128),
            nn.Dropout2d(0.03))
        # output_size = 8, RF = 29
        self.conv8 = nn.Sequential(
            nn.Conv2d(in_channels=128,
                      out_channels=128,
                      kernel_size=3,
                      padding=1,
                      bias=False), nn.ReLU(), nn.BatchNorm2d(128),
            nn.Dropout2d(0.03))
        # output_size = 8, RF = 37
        self.conv9 = nn.Sequential(
            nn.Conv2d(in_channels=128,
                      out_channels=128,
                      kernel_size=3,
                      padding=1,
                      bias=False), nn.ReLU(), nn.BatchNorm2d(128),
            nn.Dropout2d(0.03))
        # output_size = 8, RF = 45
        self.pool3 = nn.MaxPool2d(2, 2)  # 4

        # Convolution Block 4
        self.conv10 = nn.Conv2d(in_channels=128,
                                out_channels=32,
                                kernel_size=1)
        self.gap = nn.AvgPool2d(3)

        # Fully Connected Block
        self.fc1 = nn.Sequential(nn.Linear(32, 16), nn.ReLU())
        self.fc2 = nn.Linear(16, 10)
Beispiel #2
0
 def make_head(out_size):
     layers = []
     for _ in range(4):
         layers += [nn.Conv2d(256, 256, 3, padding=1), nn.ReLU()]
     layers += [nn.Conv2d(256, out_size, 3, padding=1)]
     return nn.Sequential(*layers)
    def __init__(
        self,
        nc=1,
        ngf=128,
        ndf=128,
        latent_variable_size=128,
        imsize=64,
        batchnorm=False,
    ):
        super(VAE, self).__init__()

        self.nc = nc
        self.ngf = ngf
        self.ndf = ndf
        self.imsize = imsize
        self.latent_variable_size = latent_variable_size
        self.batchnorm = batchnorm

        self.encoder = nn.Sequential(
            # input is 3 x 64 x 64
            nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf) x 32 x 32
            nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*2) x 16 x 16
            nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*4) x 8 x 8
            nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*8) x 4 x 4
            nn.Conv2d(ndf * 8, ndf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ndf * 8),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ndf*8) x 2 x 2
        )

        self.fc1 = nn.Linear(ndf * 8 * 2 * 2, latent_variable_size)
        self.fc2 = nn.Linear(ndf * 8 * 2 * 2, latent_variable_size)

        # decoder

        self.decoder = nn.Sequential(
            # input is Z, going into a convolution
            # state size. (ngf*8) x 2 x 2
            nn.ConvTranspose2d(ngf * 8, ngf * 8, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 8),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ngf*8) x 4 x 4
            nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 4),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ngf*4) x 8 x 8
            nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf * 2),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ngf*2) x 16 x 16
            nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False),
            nn.BatchNorm2d(ngf),
            nn.LeakyReLU(0.2, inplace=True),
            # state size. (ngf) x 32 x 32
            nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False),
            nn.Sigmoid(),
            # state size. (nc) x 64 x 64
        )

        self.d1 = nn.Sequential(
            nn.Linear(latent_variable_size, ngf * 8 * 2 * 2), nn.ReLU(inplace=True),
        )
        self.bn_mean = nn.BatchNorm1d(latent_variable_size)
Beispiel #4
0
 def __init__(self, num_input_features, num_output_features):
     super(_Transition, self).__init__()
     self.add_module('relu', nn.ReLU(inplace=False))
     self.add_module('conv', nn.Conv2d(num_input_features, num_output_features,
                                       kernel_size=1, stride=1, bias=True))
Beispiel #5
0
    elif model_arch == "alexnet":
        since = time.time()
        print("LOADING alexnet CLASSIFIER (might take a few minutes)")
        model = models.alexnet(pretrained=True)
        time_elapsed = time.time() - since
        print('Loading complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
        print(model.classifier[6].out_features)
    else: 
        print("IMPLEMENT MORE CLASSIFIERS HERE")
        sys.exit(0)
        
    in_dim = model.classifier[-1].in_features
    out_dim = len(class_names)

    if hidden_units == 0: # just append directly to this classifier
        model.classifier[-1] = nn.Linear(in_features = in_dim, 
                                        out_features = out_dim)
    else: # add the hidden layer and then append our output classifier
        model.classifier[-1] = nn.Linear(in_features = in_dim, 
                                        out_features = hidden_units)
        
        model.add_module("relu", nn.ReLU())
        model.add_module("dropout", nn.Dropout(p=0.5))
        model.add_module("output", nn.Linear(in_features = hidden_units,
                                  out_features = out_dim))

    trained_model, optimzer, epoch = train_and_test_model(model, save_dir, data_loader, learning_rate, 
                                      epochs, gpu, plot=True)

    utils.save(trained_model, optimzer.state_dict(), 
                       epoch, class_2_idx, save_dir)   
Beispiel #6
0
 def __init__(self, skip_channels, end_channels, channels):
     super(PostProcess, self).__init__()
     self.conv1 = nn.Conv1d(skip_channels, end_channels, 1)
     self.conv2 = nn.Conv1d(end_channels, channels, 1)
     self.relu = nn.ReLU(inplace=True)
Beispiel #7
0
    def __init__(self,
                 use_norm=True,
                 layer_nums=(3, 5, 5),
                 layer_strides=(2, 2, 2),
                 num_filters=(128, 128, 256),
                 upsample_strides=(1, 2, 4),
                 num_upsample_filters=(256, 256, 256),
                 num_input_features=128,
                 use_groupnorm=False,
                 num_groups=32,):
        """upsample_strides support float: [0.25, 0.5, 1]
        if upsample_strides < 1, conv2d will be used instead of convtranspose2d.
        """
        super(RPNNoHeadBase, self).__init__()
        from det3.methods.second.utils.torch_utils import change_default_args, Empty
        self._layer_strides = layer_strides
        self._num_filters = num_filters
        self._layer_nums = layer_nums
        self._upsample_strides = upsample_strides
        self._num_upsample_filters = num_upsample_filters
        self._num_input_features = num_input_features
        self._use_norm = use_norm
        self._use_groupnorm = use_groupnorm
        self._num_groups = num_groups
        assert len(layer_strides) == len(layer_nums)
        assert len(num_filters) == len(layer_nums)
        assert len(num_upsample_filters) == len(upsample_strides)
        self._upsample_start_idx = len(layer_nums) - len(upsample_strides)
        must_equal_list = []
        for i in range(len(upsample_strides)):
            must_equal_list.append(upsample_strides[i] / np.prod(
                layer_strides[:i + self._upsample_start_idx + 1]))
        for val in must_equal_list:
            assert val == must_equal_list[0]

        if use_norm:
            if use_groupnorm:
                raise NotImplementedError
            else:
                BatchNorm2d = change_default_args(
                    eps=1e-3, momentum=0.01)(nn.BatchNorm2d)
            Conv2d = change_default_args(bias=False)(nn.Conv2d)
            ConvTranspose2d = change_default_args(bias=False)(
                nn.ConvTranspose2d)
        else:
            BatchNorm2d = Empty
            Conv2d = change_default_args(bias=True)(nn.Conv2d)
            ConvTranspose2d = change_default_args(bias=True)(
                nn.ConvTranspose2d)

        in_filters = [num_input_features, *num_filters[:-1]]
        blocks = []
        deblocks = []
        for i, layer_num in enumerate(layer_nums):
            block, num_out_filters = self._make_layer(
                in_filters[i],
                num_filters[i],
                layer_num,
                stride=layer_strides[i])
            blocks.append(block)
            if i - self._upsample_start_idx >= 0:
                stride = upsample_strides[i - self._upsample_start_idx]
                if stride >= 1:
                    stride = np.round(stride).astype(np.int64)
                    deblock = nn.Sequential(
                        ConvTranspose2d(
                            num_out_filters,
                            num_upsample_filters[i - self._upsample_start_idx],
                            stride,
                            stride=stride),
                        BatchNorm2d(
                            num_upsample_filters[i -
                                                 self._upsample_start_idx]),
                        nn.ReLU(),
                    )
                else:
                    stride = np.round(1 / stride).astype(np.int64)
                    deblock = nn.Sequential(
                        Conv2d(
                            num_out_filters,
                            num_upsample_filters[i - self._upsample_start_idx],
                            stride,
                            stride=stride),
                        BatchNorm2d(
                            num_upsample_filters[i -
                                                 self._upsample_start_idx]),
                        nn.ReLU(),
                    )
                deblocks.append(deblock)
        self._num_out_filters = num_out_filters
        self.blocks = nn.ModuleList(blocks)
        self.deblocks = nn.ModuleList(deblocks)
Beispiel #8
0
 def __init__(self, in_channels, out_channels, **kwargs):
     super(conv_block, self).__init__()
     self.relu = nn.ReLU()
     self.conv = nn.Conv2d(in_channels, out_channels, **kwargs)
     self.batchnorm = nn.BatchNorm2d(out_channels)
    def create_network(self, blocks):
        models = nn.ModuleList()

        prev_filters = 3
        out_filters = []
        prev_stride = 1
        out_strides = []
        conv_id = 0
        for block in blocks:
            if block['type'] == 'net':
                prev_filters = int(block['channels'])
                continue
            elif block['type'] == 'convolutional':
                conv_id = conv_id + 1
                batch_normalize = int(block['batch_normalize'])
                filters = int(block['filters'])
                kernel_size = int(block['size'])
                stride = int(block['stride'])
                is_pad = int(block['pad'])
                pad = (kernel_size - 1) // 2 if is_pad else 0
                activation = block['activation']
                model = nn.Sequential()
                if batch_normalize:
                    model.add_module('conv{0}'.format(conv_id),
                                     nn.Conv2d(prev_filters, filters, kernel_size, stride, pad, bias=False))
                    model.add_module('bn{0}'.format(conv_id), nn.BatchNorm2d(filters))
                    # model.add_module('bn{0}'.format(conv_id), BN2d(filters))
                else:
                    model.add_module('conv{0}'.format(conv_id),
                                     nn.Conv2d(prev_filters, filters, kernel_size, stride, pad))
                if activation == 'leaky':
                    model.add_module('leaky{0}'.format(conv_id), nn.LeakyReLU(0.1, inplace=True))
                elif activation == 'relu':
                    model.add_module('relu{0}'.format(conv_id), nn.ReLU(inplace=True))
                elif activation == 'mish':
                    model.add_module('mish{0}'.format(conv_id), Mish())
                else:
                    print("convalution havn't activate {}".format(activation))

                prev_filters = filters
                out_filters.append(prev_filters)
                prev_stride = stride * prev_stride
                out_strides.append(prev_stride)
                models.append(model)
            elif block['type'] == 'maxpool':
                pool_size = int(block['size'])
                stride = int(block['stride'])
                if stride == 1 and pool_size % 2:
                    # You can use Maxpooldark instead, here is convenient to convert onnx.
                    # Example: [maxpool] size=3 stride=1
                    model = nn.MaxPool2d(kernel_size=pool_size, stride=stride, padding=pool_size // 2)
                elif stride == pool_size:
                    # You can use Maxpooldark instead, here is convenient to convert onnx.
                    # Example: [maxpool] size=2 stride=2
                    model = nn.MaxPool2d(kernel_size=pool_size, stride=stride, padding=0)
                else:
                    model = MaxPoolDark(pool_size, stride)
                out_filters.append(prev_filters)
                prev_stride = stride * prev_stride
                out_strides.append(prev_stride)
                models.append(model)
            elif block['type'] == 'avgpool':
                model = GlobalAvgPool2d()
                out_filters.append(prev_filters)
                models.append(model)
            elif block['type'] == 'softmax':
                model = nn.Softmax()
                out_strides.append(prev_stride)
                out_filters.append(prev_filters)
                models.append(model)
            elif block['type'] == 'cost':
                if block['_type'] == 'sse':
                    model = nn.MSELoss(reduction='mean')
                elif block['_type'] == 'L1':
                    model = nn.L1Loss(reduction='mean')
                elif block['_type'] == 'smooth':
                    model = nn.SmoothL1Loss(reduction='mean')
                out_filters.append(1)
                out_strides.append(prev_stride)
                models.append(model)
            elif block['type'] == 'reorg':
                stride = int(block['stride'])
                prev_filters = stride * stride * prev_filters
                out_filters.append(prev_filters)
                prev_stride = prev_stride * stride
                out_strides.append(prev_stride)
                models.append(Reorg(stride))
            elif block['type'] == 'upsample':
                stride = int(block['stride'])
                out_filters.append(prev_filters)
                prev_stride = prev_stride // stride
                out_strides.append(prev_stride)

                models.append(Upsample_expand(stride))
                # models.append(Upsample_interpolate(stride))

            elif block['type'] == 'route':
                layers = block['layers'].split(',')
                ind = len(models)
                layers = [int(i) if int(i) > 0 else int(i) + ind for i in layers]
                if len(layers) == 1:
                    if 'groups' not in block.keys() or int(block['groups']) == 1:
                        prev_filters = out_filters[layers[0]]
                        prev_stride = out_strides[layers[0]]
                    else:
                        prev_filters = out_filters[layers[0]] // int(block['groups'])
                        prev_stride = out_strides[layers[0]] // int(block['groups'])
                elif len(layers) == 2:
                    assert (layers[0] == ind - 1 or layers[1] == ind - 1)
                    prev_filters = out_filters[layers[0]] + out_filters[layers[1]]
                    prev_stride = out_strides[layers[0]]
                elif len(layers) == 4:
                    assert (layers[0] == ind - 1)
                    prev_filters = out_filters[layers[0]] + out_filters[layers[1]] + out_filters[layers[2]] + \
                                   out_filters[layers[3]]
                    prev_stride = out_strides[layers[0]]
                else:
                    print("route error!!!")

                out_filters.append(prev_filters)
                out_strides.append(prev_stride)
                models.append(EmptyModule())
            elif block['type'] == 'shortcut':
                ind = len(models)
                prev_filters = out_filters[ind - 1]
                out_filters.append(prev_filters)
                prev_stride = out_strides[ind - 1]
                out_strides.append(prev_stride)
                models.append(EmptyModule())
            elif block['type'] == 'connected':
                filters = int(block['output'])
                if block['activation'] == 'linear':
                    model = nn.Linear(prev_filters, filters)
                elif block['activation'] == 'leaky':
                    model = nn.Sequential(
                        nn.Linear(prev_filters, filters),
                        nn.LeakyReLU(0.1, inplace=True))
                elif block['activation'] == 'relu':
                    model = nn.Sequential(
                        nn.Linear(prev_filters, filters),
                        nn.ReLU(inplace=True))
                prev_filters = filters
                out_filters.append(prev_filters)
                out_strides.append(prev_stride)
                models.append(model)
            elif block['type'] == 'region':
                loss = RegionLoss()
                anchors = block['anchors'].split(',')
                loss.anchors = [float(i) for i in anchors]
                loss.num_classes = int(block['classes'])
                loss.num_anchors = int(block['num'])
                loss.anchor_step = len(loss.anchors) // loss.num_anchors
                loss.object_scale = float(block['object_scale'])
                loss.noobject_scale = float(block['noobject_scale'])
                loss.class_scale = float(block['class_scale'])
                loss.coord_scale = float(block['coord_scale'])
                out_filters.append(prev_filters)
                out_strides.append(prev_stride)
                models.append(loss)
            elif block['type'] == 'yolo':
                yolo_layer = YoloLayer()
                anchors = block['anchors'].split(',')
                anchor_mask = block['mask'].split(',')
                yolo_layer.anchor_mask = [int(i) for i in anchor_mask]
                yolo_layer.anchors = [float(i) for i in anchors]
                yolo_layer.num_classes = int(block['classes'])
                self.num_classes = yolo_layer.num_classes
                yolo_layer.num_anchors = int(block['num'])
                yolo_layer.anchor_step = len(yolo_layer.anchors) // yolo_layer.num_anchors
                yolo_layer.stride = prev_stride
                yolo_layer.scale_x_y = float(block['scale_x_y'])
                # yolo_layer.object_scale = float(block['object_scale'])
                # yolo_layer.noobject_scale = float(block['noobject_scale'])
                # yolo_layer.class_scale = float(block['class_scale'])
                # yolo_layer.coord_scale = float(block['coord_scale'])
                out_filters.append(prev_filters)
                out_strides.append(prev_stride)
                models.append(yolo_layer)
            else:
                print('unknown type %s' % (block['type']))

        return models
from typing import Union

import torch
from torch import nn

Activation = Union[str, nn.Module]


_str_to_activation = {
    'relu': nn.ReLU(),
    'tanh': nn.Tanh(),
    'leaky_relu': nn.LeakyReLU(),
    'sigmoid': nn.Sigmoid(),
    'selu': nn.SELU(),
    'softplus': nn.Softplus(),
    'identity': nn.Identity(),
}


def build_mlp(
        input_size: int,
        output_size: int,
        n_layers: int,
        size: int,
        activation: Activation = 'tanh',
        output_activation: Activation = 'identity',
) -> nn.Module:
    """
        Builds a feedforward neural network

        arguments:
Beispiel #11
0
    def __init__(self,
                 block,
                 layers,
                 arch='D',
                 channels=(16, 32, 64, 128, 256, 512, 512, 512),
                 BatchNorm=None):
        super(DRN, self).__init__()
        self.inplanes = channels[0]
        self.out_dim = channels[-1]
        self.arch = arch

        if arch == 'C':
            self.conv1 = nn.Conv2d(3,
                                   channels[0],
                                   kernel_size=7,
                                   stride=1,
                                   padding=3,
                                   bias=False)
            self.bn1 = BatchNorm(channels[0])
            self.relu = nn.ReLU(inplace=True)

            self.layer1 = self._make_layer(BasicBlock,
                                           channels[0],
                                           layers[0],
                                           stride=1,
                                           BatchNorm=BatchNorm)
            self.layer2 = self._make_layer(BasicBlock,
                                           channels[1],
                                           layers[1],
                                           stride=2,
                                           BatchNorm=BatchNorm)

        elif arch == 'D':
            self.layer0 = nn.Sequential(
                nn.Conv2d(3,
                          channels[0],
                          kernel_size=7,
                          stride=1,
                          padding=3,
                          bias=False), BatchNorm(channels[0]),
                nn.ReLU(inplace=True))

            self.layer1 = self._make_conv_layers(channels[0],
                                                 layers[0],
                                                 stride=1,
                                                 BatchNorm=BatchNorm)
            self.layer2 = self._make_conv_layers(channels[1],
                                                 layers[1],
                                                 stride=2,
                                                 BatchNorm=BatchNorm)

        self.layer3 = self._make_layer(block,
                                       channels[2],
                                       layers[2],
                                       stride=2,
                                       BatchNorm=BatchNorm)
        self.layer4 = self._make_layer(block,
                                       channels[3],
                                       layers[3],
                                       stride=2,
                                       BatchNorm=BatchNorm)
        self.layer5 = self._make_layer(block,
                                       channels[4],
                                       layers[4],
                                       dilation=2,
                                       new_level=False,
                                       BatchNorm=BatchNorm)
        self.layer6 = None if layers[5] == 0 else \
            self._make_layer(block, channels[5], layers[5], dilation=4,
                             new_level=False, BatchNorm=BatchNorm)

        if arch == 'C':
            self.layer7 = None if layers[6] == 0 else \
                self._make_layer(BasicBlock, channels[6], layers[6], dilation=2,
                                 new_level=False, residual=False, BatchNorm=BatchNorm)
            self.layer8 = None if layers[7] == 0 else \
                self._make_layer(BasicBlock, channels[7], layers[7], dilation=1,
                                 new_level=False, residual=False, BatchNorm=BatchNorm)
        elif arch == 'D':
            self.layer7 = None if layers[6] == 0 else \
                self._make_conv_layers(channels[6], layers[6], dilation=2, BatchNorm=BatchNorm)
            self.layer8 = None if layers[7] == 0 else \
                self._make_conv_layers(channels[7], layers[7], dilation=1, BatchNorm=BatchNorm)

        self._init_weight()
Beispiel #12
0
    def __init__(
            self,
            sources,
            # Channels
            audio_channels=2,
            channels=64,
            growth=2.,
            # Main structure
            depth=6,
            rewrite=True,
            lstm_layers=0,
            # Convolutions
            kernel_size=8,
            stride=4,
            context=1,
            # Activations
            gelu=True,
            glu=True,
            # Normalization
            norm_starts=4,
            norm_groups=4,
            # DConv residual branch
            dconv_mode=1,
            dconv_depth=2,
            dconv_comp=4,
            dconv_attn=4,
            dconv_lstm=4,
            dconv_init=1e-4,
            # Pre/post processing
            normalize=True,
            resample=True,
            # Weight init
            rescale=0.1,
            # Metadata
            samplerate=44100,
            segment=4 * 10):
        """
        Args:
            sources (list[str]): list of source names
            audio_channels (int): stereo or mono
            channels (int): first convolution channels
            depth (int): number of encoder/decoder layers
            growth (float): multiply (resp divide) number of channels by that
                for each layer of the encoder (resp decoder)
            depth (int): number of layers in the encoder and in the decoder.
            rewrite (bool): add 1x1 convolution to each layer.
            lstm_layers (int): number of lstm layers, 0 = no lstm. Deactivated
                by default, as this is now replaced by the smaller and faster small LSTMs
                in the DConv branches.
            kernel_size (int): kernel size for convolutions
            stride (int): stride for convolutions
            context (int): kernel size of the convolution in the
                decoder before the transposed convolution. If > 1,
                will provide some context from neighboring time steps.
            gelu: use GELU activation function.
            glu (bool): use glu instead of ReLU for the 1x1 rewrite conv.
            norm_starts: layer at which group norm starts being used.
                decoder layers are numbered in reverse order.
            norm_groups: number of groups for group norm.
            dconv_mode: if 1: dconv in encoder only, 2: decoder only, 3: both.
            dconv_depth: depth of residual DConv branch.
            dconv_comp: compression of DConv branch.
            dconv_attn: adds attention layers in DConv branch starting at this layer.
            dconv_lstm: adds a LSTM layer in DConv branch starting at this layer.
            dconv_init: initial scale for the DConv branch LayerScale.
            normalize (bool): normalizes the input audio on the fly, and scales back
                the output by the same amount.
            resample (bool): upsample x2 the input and downsample /2 the output.
            rescale (int): rescale initial weights of convolutions
                to get their standard deviation closer to `rescale`.
            samplerate (int): stored as meta information for easing
                future evaluations of the model.
            segment (float): duration of the chunks of audio to ideally evaluate the model on.
                This is used by `demucs.apply.apply_model`.
        """

        super().__init__()
        self.audio_channels = audio_channels
        self.sources = sources
        self.kernel_size = kernel_size
        self.context = context
        self.stride = stride
        self.depth = depth
        self.resample = resample
        self.channels = channels
        self.normalize = normalize
        self.samplerate = samplerate
        self.segment = segment
        self.encoder = nn.ModuleList()
        self.decoder = nn.ModuleList()
        self.skip_scales = nn.ModuleList()

        if glu:
            activation = nn.GLU(dim=1)
            ch_scale = 2
        else:
            activation = nn.ReLU()
            ch_scale = 1
        if gelu:
            act2 = nn.GELU
        else:
            act2 = nn.ReLU

        in_channels = audio_channels
        padding = 0
        for index in range(depth):
            norm_fn = lambda d: nn.Identity()  # noqa
            if index >= norm_starts:
                norm_fn = lambda d: nn.GroupNorm(norm_groups, d)  # noqa

            encode = []
            encode += [
                nn.Conv1d(in_channels, channels, kernel_size, stride),
                norm_fn(channels),
                act2(),
            ]
            attn = index >= dconv_attn
            lstm = index >= dconv_lstm
            if dconv_mode & 1:
                encode += [
                    DConv(channels,
                          depth=dconv_depth,
                          init=dconv_init,
                          compress=dconv_comp,
                          attn=attn,
                          lstm=lstm)
                ]
            if rewrite:
                encode += [
                    nn.Conv1d(channels, ch_scale * channels, 1),
                    norm_fn(ch_scale * channels), activation
                ]
            self.encoder.append(nn.Sequential(*encode))

            decode = []
            if index > 0:
                out_channels = in_channels
            else:
                out_channels = len(self.sources) * audio_channels
            if rewrite:
                decode += [
                    nn.Conv1d(channels,
                              ch_scale * channels,
                              2 * context + 1,
                              padding=context),
                    norm_fn(ch_scale * channels), activation
                ]
            if dconv_mode & 2:
                decode += [
                    DConv(channels,
                          depth=dconv_depth,
                          init=dconv_init,
                          compress=dconv_comp,
                          attn=attn,
                          lstm=lstm)
                ]
            decode += [
                nn.ConvTranspose1d(channels,
                                   out_channels,
                                   kernel_size,
                                   stride,
                                   padding=padding)
            ]
            if index > 0:
                decode += [norm_fn(out_channels), act2()]
            self.decoder.insert(0, nn.Sequential(*decode))
            in_channels = channels
            channels = int(growth * channels)

        channels = in_channels
        if lstm_layers:
            self.lstm = BLSTM(channels, lstm_layers)
        else:
            self.lstm = None

        if rescale:
            rescale_module(self, reference=rescale)
Beispiel #13
0
 def __init__(self):
     super(DummyModel, self).__init__()
     self.conv = nn.Conv2d(3, 8, kernel_size=10)
     self.relu = nn.ReLU()
     self.dropout = nn.Dropout()
     self.linear = nn.Linear(8, 2)
Beispiel #14
0
 def __init__(self, input_size, hidden_size, num_classes):
     super(NeuralNet, self).__init__()
     self.fc1 = nn.Linear(input_size, hidden_size)
     self.relu = nn.ReLU()
     self.fc2 = nn.Linear(hidden_size, num_classes)
    def __init__(self, n_channels, groupnorm=False, resnet_type='resnet50',
                 resnet_pretrained=True, resnet_chls=64, image_downscale=1):

        self.image_downscale = image_downscale
        super(PixorNet_Fusion, self).__init__()
        block = Basic_Block
        self.inplanes = 32

        self.inc = inconv(n_channels, 32)
        self.down1 = self._make_layer(block, [24, 24, 96], stride=2)
        self.down2 = self._make_layer(
            block, [48, 48, 48, 48, 192, 192], stride=2)

        self.inplanes += resnet_chls
        self.down3 = self._make_layer(
            block, [64, 64, 64, 64, 256, 256], stride=2, groupnorm=groupnorm)
        self.inplanes += resnet_chls

        self.down4 = self._make_layer(block, [96, 96, 384], stride=2, groupnorm=groupnorm)
        self.inplanes += resnet_chls

        self.bridge = nn.Sequential(
            nn.Conv2d(self.inplanes, 192, 1),
            nn.BatchNorm2d(192) if not groupnorm else nn.GroupNorm(8, 192),
            nn.ReLU(inplace=True),
        )

        self.up1 = up(192, 256+resnet_chls, 128, groupnorm=groupnorm)
        self.up2 = up(128, 192+resnet_chls, 96, groupnorm=groupnorm)

        self.shared_header = nn.Sequential(
            nn.Conv2d(96, 96, 3, stride=1, padding=1),
            nn.BatchNorm2d(96) if not groupnorm else nn.GroupNorm(8, 96),
            nn.ReLU(inplace=True),
            nn.Conv2d(96, 96, 3, stride=1, padding=1),
            nn.BatchNorm2d(96) if not groupnorm else nn.GroupNorm(8, 96),
            nn.ReLU(inplace=True),
            nn.Conv2d(96, 96, 3, stride=1, padding=1),
            nn.BatchNorm2d(96) if not groupnorm else nn.GroupNorm(8, 96),
            nn.ReLU(inplace=True),
            nn.Conv2d(96, 96, 3, stride=1, padding=1),
            nn.BatchNorm2d(96) if not groupnorm else nn.GroupNorm(8, 96),
            nn.ReLU(inplace=True)
        )

        self.classification_header = nn.Sequential(
            nn.Conv2d(96, 1, 3, stride=1, padding=1),
        )
        self.regression_header = nn.Conv2d(96, 6, 3, stride=1, padding=1)

        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) or isinstance(m, nn.GroupNorm):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

        if resnet_type == 'resnet50':
            self.resnet = resnet50(resnet_pretrained, resnet_chls=resnet_chls)
        else:
            self.resnet = resnet18(resnet_pretrained, resnet_chls=resnet_chls)
Beispiel #16
0
    def __init__(self,
                 inplanes,
                 planes,
                 reps,
                 stride=1,
                 dilation=1,
                 start_with_relu=True,
                 grow_first=True,
                 is_last=False):
        super(Block2, self).__init__()

        if planes != inplanes or stride != 1:
            self.skip = nn.Conv2d(inplanes,
                                  planes,
                                  1,
                                  stride=stride,
                                  bias=False)
            self.skipbn = nn.BatchNorm2d(planes)
        else:
            self.skip = None

        self.relu = nn.ReLU(inplace=True)
        rep = []

        filters = inplanes
        if grow_first:
            rep.append(self.relu)
            rep.append(
                SeparableConv2d_same(inplanes,
                                     planes,
                                     3,
                                     stride=1,
                                     dilation=dilation))
            #             rep.append(nn.BatchNorm2d(planes))
            filters = planes

        for i in range(reps - 1):
            rep.append(self.relu)
            rep.append(
                SeparableConv2d_same(filters,
                                     filters,
                                     3,
                                     stride=1,
                                     dilation=dilation))
#             rep.append(nn.BatchNorm2d(filters))

        if not grow_first:
            rep.append(self.relu)
            rep.append(
                SeparableConv2d_same(inplanes,
                                     planes,
                                     3,
                                     stride=1,
                                     dilation=dilation))


#             rep.append(nn.BatchNorm2d(planes))

        if not start_with_relu:
            rep = rep[1:]

        if stride != 1:
            self.block2_lastconv = nn.Sequential(*[
                self.relu,
                SeparableConv2d_same(
                    planes, planes, 3, stride=stride, dilation=dilation)
            ])

        if is_last:
            rep.append(SeparableConv2d_same(planes, planes, 3, stride=1))

        self.rep = nn.Sequential(*rep)
Beispiel #17
0
 def conv_bn_relu(in_planes, out_planes, kernel_size, stride=1, padding=0, groups=1):
     return[
         nn.Conv2d(in_planes, out_planes, kernel_size, stride=stride, padding=padding, groups=groups, bias=False),
         nn.BatchNorm2d(out_planes),
         nn.ReLU(inplace=True)
     ]
Beispiel #18
0
    def __init__(self, inplanes=3, os=16, pretrained=False):
        super(Xception, self).__init__()

        if os == 16:
            entry_block3_stride = 2
            middle_block_rate = 1
            exit_block_rates = (1, 2)
        elif os == 8:
            entry_block3_stride = 1
            middle_block_rate = 2
            exit_block_rates = (2, 4)
        else:
            raise NotImplementedError

        # Entry flow
        self.conv1 = nn.Conv2d(inplanes,
                               16,
                               3,
                               stride=2,
                               padding=1,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(16)
        self.relu = nn.ReLU(inplace=True)

        self.conv2 = nn.Conv2d(16, 32, 3, stride=1, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(32)

        self.block1 = Block(32,
                            64,
                            reps=2,
                            stride=2,
                            start_with_relu=False,
                            grow_first=True)
        self.block2 = Block2(64,
                             128,
                             reps=2,
                             stride=2,
                             start_with_relu=True,
                             grow_first=True)
        self.block3 = Block(128,
                            364,
                            reps=2,
                            stride=entry_block3_stride,
                            start_with_relu=True,
                            grow_first=True)

        # Middle flow
        # self.block4  = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block5  = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block6  = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block7  = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block8  = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block9  = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block10 = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block11 = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block12 = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block13 = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block14 = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block15 = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block16 = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block17 = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block18 = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)
        # self.block19 = Block(728, 728, reps=3, stride=1, dilation=middle_block_rate, start_with_relu=True, grow_first=True)

        # Exit flow
        self.block20 = Block(364,
                             512,
                             reps=2,
                             stride=1,
                             dilation=exit_block_rates[0],
                             start_with_relu=True,
                             grow_first=False,
                             is_last=True)

        self.conv3 = SeparableConv2d_aspp(512,
                                          768,
                                          3,
                                          stride=1,
                                          dilation=exit_block_rates[1],
                                          padding=exit_block_rates[1])
        # self.bn3 = nn.BatchNorm2d(1536)

        self.conv4 = SeparableConv2d_aspp(768,
                                          768,
                                          3,
                                          stride=1,
                                          dilation=exit_block_rates[1],
                                          padding=exit_block_rates[1])
        # self.bn4 = nn.BatchNorm2d(1536)

        self.conv5 = SeparableConv2d_aspp(768,
                                          1024,
                                          3,
                                          stride=1,
                                          dilation=exit_block_rates[1],
                                          padding=exit_block_rates[1])
        # self.bn5 = nn.BatchNorm2d(2048)

        # Init weights
        # self.__init_weight()

        # Load pretrained model
        if pretrained:
            self.__load_xception_pretrained()
Beispiel #19
0
def create_spatial_conv2d_group_bn_relu(prefix,
                                        in_channels,
                                        out_channels,
                                        kernel_size,
                                        stride,
                                        padding=0,
                                        dilation=1,
                                        groups=1,
                                        bias=False,
                                        has_bn=True,
                                        has_relu=True,
                                        channel_shuffle=False,
                                        has_spatial_conv=True,
                                        has_spatial_conv_bn=True,
                                        conv_name_fun=None,
                                        bn_name_fun=None,
                                        bn_training=True,
                                        fix_weights=False):
    conv_name = prefix
    if conv_name_fun:
        conv_name = conv_name_fun(prefix)

    layer = nn.Sequential()

    if has_spatial_conv:
        spatial_conv_name = conv_name + '_s'
        layer.add_module(
            spatial_conv_name,
            nn.Conv2d(in_channels=in_channels,
                      out_channels=in_channels,
                      kernel_size=kernel_size,
                      stride=stride,
                      padding=padding,
                      dilation=dilation,
                      groups=in_channels,
                      bias=bias))
        if fix_weights:
            pass

        if has_spatial_conv_bn:
            layer.add_module(spatial_conv_name + '_bn',
                             nn.BatchNorm2d(in_channels))

    if channel_shuffle:
        pass

    assert in_channels % groups == 0
    assert out_channels % groups == 0

    layer.add_module(
        conv_name,
        nn.Conv2d(in_channels=in_channels,
                  out_channels=out_channels,
                  kernel_size=1,
                  stride=1,
                  padding=0,
                  groups=groups,
                  bias=bias))
    if fix_weights:
        pass

    if has_bn:
        bn_name = 'bn_' + prefix
        if bn_name_fun:
            bn_name = bn_name_fun(prefix)
        layer.add_module(bn_name, nn.BatchNorm2d(out_channels))
        if bn_training:
            pass

    if has_relu:
        layer.add_module('relu' + prefix, nn.ReLU(inplace=True))

    return layer
Beispiel #20
0
    def __init__(self,
                 nInputChannels=3,
                 n_classes=21,
                 os=16,
                 pretrained=False,
                 _print=True,
                 final_sigmoid=False,
                 hidden_layers=128,
                 gcn_mode=0,
                 device=None):
        if _print:
            print("Constructing DeepLabv3+ model...")
            print("Number of classes: {}".format(n_classes))
            print("Output stride: {}".format(os))
            print("Number of Input Channels: {}".format(nInputChannels))
        super(DeepLabv3_plus_gcn_skipconnection_2d, self).__init__()
        self.in_channels = nInputChannels
        self.gcn_mode = gcn_mode
        self.device = device
        # Atrous Conv
        self.xception_features = Xception(nInputChannels, os, pretrained)

        # ASPP
        if os == 16:
            rates = [1, 6, 12, 18]
        elif os == 8:
            rates = [1, 12, 24, 36]
            raise NotImplementedError
        else:
            raise NotImplementedError

        self.aspp1 = ASPP_module_rate0(1024, 128, rate=rates[0])
        self.aspp2 = ASPP_module(1024, 128, rate=rates[1])
        self.aspp3 = ASPP_module(1024, 128, rate=rates[2])
        self.aspp4 = ASPP_module(1024, 128, rate=rates[3])

        self.relu = nn.ReLU()

        self.global_avg_pool = nn.Sequential(
            nn.AdaptiveAvgPool2d((1, 1)),
            nn.Conv2d(1024, 128, 1, stride=1, bias=False), nn.BatchNorm2d(128),
            nn.ReLU())

        self.concat_projection_conv1 = nn.Conv2d(640, 128, 1, bias=False)
        self.concat_projection_bn1 = nn.BatchNorm2d(128)

        # adopt [1x1, 48] for channel reduction.
        self.feature_projection_conv1 = nn.Conv2d(128, 24, 1, bias=False)
        self.feature_projection_bn1 = nn.BatchNorm2d(24)

        self.feature_projection_conv2 = nn.Conv2d(32, 64, 1, bias=False)
        self.feature_projection_bn2 = nn.BatchNorm2d(64)

        self.decoder1 = nn.Sequential(Decoder_module(152, 128),
                                      Decoder_module(128, 128))
        self.decoder2 = nn.Sequential(Decoder_module(192, 256),
                                      Decoder_module(256, 256))

        if self.gcn_mode == 0:
            self.featuremap_2_graph = gcn.Featuremaps_to_Graph(
                input_channels=128,
                hidden_layers=hidden_layers,
                nodes=n_classes)
            self.graph_conv1 = gcn.GraphConvolution(hidden_layers,
                                                    hidden_layers)
            self.graph_conv2 = gcn.GraphConvolution(hidden_layers,
                                                    hidden_layers)
            self.graph_conv3 = gcn.GraphConvolution(hidden_layers,
                                                    hidden_layers)

            self.graph_2_fea = gcn.Graph_to_Featuremaps_savemem(
                input_channels=128,
                output_channels=128,
                hidden_layers=hidden_layers,
                nodes=n_classes)
        elif self.gcn_mode == 1:
            self.global_reasoning_unit = GloRe_Unit_2D(num_in=128,
                                                       num_s=128,
                                                       num_n=64)
        elif self.gcn_mode == 2:
            self.featuremap_2_graph = gcn.My_Featuremaps_to_Graph(
                input_channels=128,
                hidden_layers=hidden_layers,
                nodes=n_classes)
            self.graph_conv1 = gcn.GraphConvolution(hidden_layers,
                                                    hidden_layers)
            self.graph_conv2 = gcn.GraphConvolution(hidden_layers,
                                                    hidden_layers)
            self.graph_conv3 = gcn.GraphConvolution(hidden_layers,
                                                    hidden_layers)

            self.graph_2_fea = gcn.My_Graph_to_Featuremaps(
                hidden_layers=hidden_layers, output_channels=128, dimension=2)

        self.skip_conv = nn.Sequential(
            *[nn.Conv2d(128, 128, kernel_size=1),
              nn.ReLU(True)])

        self.semantic = nn.Conv2d(256, n_classes, kernel_size=1, stride=1)

        if final_sigmoid:
            self.final_activation = nn.Sigmoid()
        else:
            self.final_activation = nn.Softmax(dim=1)
Beispiel #21
0
    def __init__(self, cfg, in_channels):
        """
        Arguments:
            in_channels (int): number of channels of the input feature
        """
        super(FCOSHead, self).__init__()
        # TODO: Implement the sigmoid version first.
        num_classes = cfg.MODEL.FCOS.NUM_CLASSES - 1
        self.fpn_strides = cfg.MODEL.FCOS.FPN_STRIDES
        self.norm_reg_targets = cfg.MODEL.FCOS.NORM_REG_TARGETS
        self.centerness_on_reg = cfg.MODEL.FCOS.CENTERNESS_ON_REG
        self.use_dcn_in_tower = cfg.MODEL.FCOS.USE_DCN_IN_TOWER

        cls_tower = []
        bbox_tower = []
        for i in range(cfg.MODEL.FCOS.NUM_CONVS):
            if self.use_dcn_in_tower and \
                    i == cfg.MODEL.FCOS.NUM_CONVS - 1:
                conv_func = DFConv2d
            else:
                conv_func = nn.Conv2d

            cls_tower.append(
                conv_func(
                    in_channels,
                    in_channels,
                    kernel_size=3,
                    stride=1,
                    padding=1,
                    bias=True
                )
            )
            cls_tower.append(nn.GroupNorm(32, in_channels))
            cls_tower.append(nn.ReLU())
            bbox_tower.append(
                conv_func(
                    in_channels,
                    in_channels,
                    kernel_size=3,
                    stride=1,
                    padding=1,
                    bias=True
                )
            )
            bbox_tower.append(nn.GroupNorm(32, in_channels))
            bbox_tower.append(nn.ReLU())

        self.add_module('cls_tower', nn.Sequential(*cls_tower))
        self.add_module('bbox_tower', nn.Sequential(*bbox_tower))
        self.cls_logits = nn.Conv2d(
            in_channels, num_classes, kernel_size=3, stride=1,
            padding=1
        )
        self.bbox_pred = nn.Conv2d(
            in_channels, 4, kernel_size=3, stride=1,
            padding=1
        )
        self.centerness = nn.Conv2d(
            in_channels, 1, kernel_size=3, stride=1,
            padding=1
        )

        # initialization
        for modules in [self.cls_tower, self.bbox_tower,
                        self.cls_logits, self.bbox_pred,
                        self.centerness]:
            for l in modules.modules():
                if isinstance(l, nn.Conv2d):
                    torch.nn.init.normal_(l.weight, std=0.01)
                    torch.nn.init.constant_(l.bias, 0)

        # initialize the bias for focal loss
        prior_prob = cfg.MODEL.FCOS.PRIOR_PROB
        bias_value = -math.log((1 - prior_prob) / prior_prob)
        torch.nn.init.constant_(self.cls_logits.bias, bias_value)

        self.scales = nn.ModuleList([Scale(init_value=1.0) for _ in range(5)])
    def __init__(self, img_size, d_conv_dim, d_spectral_norm, attention,
                 attention_after_nth_dis_block, activation_fn,
                 conditional_strategy, hypersphere_dim, num_classes,
                 nonlinear_embed, normalize_embed, initialize, D_depth,
                 mixed_precision, **kwargs):
        super(Discriminator, self).__init__()
        self.in_dims = [3] + [64, 128]
        self.out_dims = [64, 128, 256]

        self.d_spectral_norm = d_spectral_norm
        self.conditional_strategy = conditional_strategy
        self.num_classes = num_classes
        self.nonlinear_embed = nonlinear_embed
        self.normalize_embed = normalize_embed
        self.mixed_precision = mixed_precision

        self.blocks = []
        for index in range(len(self.in_dims)):
            self.blocks += [[
                DiscBlock(in_channels=self.in_dims[index],
                          out_channels=self.out_dims[index],
                          d_spectral_norm=d_spectral_norm,
                          activation_fn=activation_fn)
            ]]

            if index + 1 == attention_after_nth_dis_block and attention is True:
                self.blocks += [[
                    Self_Attn(self.out_dims[index], d_spectral_norm)
                ]]

        self.blocks = nn.ModuleList(
            [nn.ModuleList(block) for block in self.blocks])

        if self.d_spectral_norm:
            self.conv = snconv2d(in_channels=256,
                                 out_channels=512,
                                 kernel_size=3,
                                 stride=1,
                                 padding=1)
        else:
            self.conv = conv2d(in_channels=256,
                               out_channels=512,
                               kernel_size=3,
                               stride=1,
                               padding=1)
            self.bn = batchnorm_2d(in_features=512)

        if activation_fn == "ReLU":
            self.activation = nn.ReLU(inplace=True)
        elif activation_fn == "Leaky_ReLU":
            self.activation = nn.LeakyReLU(negative_slope=0.1, inplace=True)
        elif activation_fn == "ELU":
            self.activation = nn.ELU(alpha=1.0, inplace=True)
        elif activation_fn == "GELU":
            self.activation = nn.GELU()
        else:
            raise NotImplementedError

        if d_spectral_norm:
            self.linear1 = snlinear(in_features=512, out_features=1)
            if self.conditional_strategy in [
                    'ContraGAN', 'Proxy_NCA_GAN', 'NT_Xent_GAN'
            ]:
                self.linear2 = snlinear(in_features=512,
                                        out_features=hypersphere_dim)
                if self.nonlinear_embed:
                    self.linear3 = snlinear(in_features=hypersphere_dim,
                                            out_features=hypersphere_dim)
                self.embedding = sn_embedding(num_classes, hypersphere_dim)
            elif self.conditional_strategy == 'ProjGAN':
                self.embedding = sn_embedding(num_classes, 512)
            elif self.conditional_strategy == 'ACGAN':
                self.linear4 = snlinear(in_features=512,
                                        out_features=num_classes)
            else:
                pass
        else:
            self.linear1 = linear(in_features=512, out_features=1)
            if self.conditional_strategy in [
                    'ContraGAN', 'Proxy_NCA_GAN', 'NT_Xent_GAN'
            ]:
                self.linear2 = linear(in_features=512,
                                      out_features=hypersphere_dim)
                if self.nonlinear_embed:
                    self.linear3 = linear(in_features=hypersphere_dim,
                                          out_features=hypersphere_dim)
                self.embedding = embedding(num_classes, hypersphere_dim)
            elif self.conditional_strategy == 'ProjGAN':
                self.embedding = embedding(num_classes, 512)
            elif self.conditional_strategy == 'ACGAN':
                self.linear4 = linear(in_features=512,
                                      out_features=num_classes)
            else:
                pass

        # Weight init
        if initialize is not False:
            init_weights(self.modules, initialize)
Beispiel #23
0
 def eval(self):
     self._module['relu'] = nn.ReLU(inplace=True)
     return super().eval()
    def __init__(self, *args, **kwargs):
        super(Vgg_Deeplab, self).__init__()
        vgg16 = torchvision.models.vgg16()

        layers = []
        layers.append(nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1))
        layers.append(nn.ReLU(inplace=True))
        layers.append(nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1))
        layers.append(nn.ReLU(inplace=True))
        layers.append(nn.MaxPool2d(3, stride=2, padding=1))

        layers.append(nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1))
        layers.append(nn.ReLU(inplace=True))
        layers.append(nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1))
        layers.append(nn.ReLU(inplace=True))
        layers.append(nn.MaxPool2d(3, stride=2, padding=1))

        layers.append(nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1))
        layers.append(nn.ReLU(inplace=True))
        layers.append(nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1))
        layers.append(nn.ReLU(inplace=True))
        layers.append(nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1))
        layers.append(nn.ReLU(inplace=True))
        layers.append(nn.MaxPool2d(3, stride=2, padding=1))

        layers.append(nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1))
        layers.append(nn.ReLU(inplace=True))
        layers.append(nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1))
        layers.append(nn.ReLU(inplace=True))
        layers.append(nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1))
        layers.append(nn.ReLU(inplace=True))
        layers.append(nn.MaxPool2d(3, stride=1, padding=1))

        layers.append(
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=2,
                      dilation=2))
        layers.append(nn.ReLU(inplace=True))
        layers.append(
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=2,
                      dilation=2))
        layers.append(nn.ReLU(inplace=True))
        layers.append(
            nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=2,
                      dilation=2))
        layers.append(nn.ReLU(inplace=True))
        layers.append(nn.MaxPool2d(3, stride=1, padding=1))
        self.features = nn.Sequential(*layers)

        classifier = []
        classifier.append(nn.AvgPool2d(3, stride=1, padding=1))
        classifier.append(
            nn.Conv2d(512,
                      1024,
                      kernel_size=3,
                      stride=1,
                      padding=12,
                      dilation=12))
        classifier.append(nn.ReLU(inplace=True))
        classifier.append(nn.Dropout(p=0.5))
        self.classifier = nn.Sequential(*classifier)

        self.init_weights()
Beispiel #25
0
    def __init__(self, model_cfg, input_channels, num_class, num_anchors_per_location, code_size, rpn_head_cfg=None,
                 head_label_indices=None, separate_reg_config=None):
        super().__init__(rpn_head_cfg, input_channels)

        self.num_anchors_per_location = num_anchors_per_location
        self.num_class = num_class
        self.code_size = code_size
        self.model_cfg = model_cfg
        self.separate_reg_config = separate_reg_config
        self.register_buffer('head_label_indices', head_label_indices)

        if self.separate_reg_config is not None:
            code_size_cnt = 0
            self.conv_box = nn.ModuleDict()
            self.conv_box_names = []
            num_middle_conv = self.separate_reg_config.NUM_MIDDLE_CONV
            num_middle_filter = self.separate_reg_config.NUM_MIDDLE_FILTER
            conv_cls_list = []
            c_in = input_channels
            for k in range(num_middle_conv):
                conv_cls_list.extend([
                    nn.Conv2d(
                        c_in, num_middle_filter,
                        kernel_size=3, stride=1, padding=1, bias=False
                    ),
                    nn.BatchNorm2d(num_middle_filter),
                    nn.ReLU()
                ])
                c_in = num_middle_filter
            conv_cls_list.append(nn.Conv2d(
                c_in, self.num_anchors_per_location * self.num_class,
                kernel_size=3, stride=1, padding=1
            ))
            self.conv_cls = nn.Sequential(*conv_cls_list)

            for reg_config in self.separate_reg_config.REG_LIST:
                reg_name, reg_channel = reg_config.split(':')
                reg_channel = int(reg_channel)
                cur_conv_list = []
                c_in = input_channels
                for k in range(num_middle_conv):
                    cur_conv_list.extend([
                        nn.Conv2d(
                            c_in, num_middle_filter,
                            kernel_size=3, stride=1, padding=1, bias=False
                        ),
                        nn.BatchNorm2d(num_middle_filter),
                        nn.ReLU()
                    ])
                    c_in = num_middle_filter

                cur_conv_list.append(nn.Conv2d(
                    c_in, self.num_anchors_per_location * int(reg_channel),
                    kernel_size=3, stride=1, padding=1, bias=True
                ))
                code_size_cnt += reg_channel
                self.conv_box[f'conv_{reg_name}'] = nn.Sequential(*cur_conv_list)
                self.conv_box_names.append(f'conv_{reg_name}')

            for m in self.conv_box.modules():
                if isinstance(m, nn.Conv2d):
                    nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                    if m.bias is not None:
                        nn.init.constant_(m.bias, 0)

            assert code_size_cnt == code_size, f'Code size does not match: {code_size_cnt}:{code_size}'
        else:
            self.conv_cls = nn.Conv2d(
                input_channels, self.num_anchors_per_location * self.num_class,
                kernel_size=1
            )
            self.conv_box = nn.Conv2d(
                input_channels, self.num_anchors_per_location * self.code_size,
                kernel_size=1
            )

        if self.model_cfg.get('USE_DIRECTION_CLASSIFIER', None) is not None:
            self.conv_dir_cls = nn.Conv2d(
                input_channels,
                self.num_anchors_per_location * self.model_cfg.NUM_DIR_BINS,
                kernel_size=1
            )
        else:
            self.conv_dir_cls = None
        self.use_multihead = self.model_cfg.get('USE_MULTIHEAD', False)
        self.init_weights()
Beispiel #26
0
    def __init__(self, loss_type='classification'):
        super(ColorizationNet, self).__init__()
        self.loss_type = loss_type
        
        self.model1 = nn.Sequential(
            nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1, bias=True),
            nn.ReLU(),
            nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1, bias=True),
            nn.ReLU(),
            nn.BatchNorm2d(64),
            nn.Dropout(0.1),
        )

        self.model2 = nn.Sequential(
            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1, bias=True),
            nn.ReLU(),
            nn.Conv2d(128, 128, kernel_size=3, stride=2, padding=1, bias=True),
            nn.ReLU(),
            nn.BatchNorm2d(128),
            nn.Dropout(0.1),
        )

        self.model3 = nn.Sequential(
            nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1, bias=True),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=True),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, stride=2, padding=1, bias=True),
            nn.ReLU(),
            nn.BatchNorm2d(256),
            nn.Dropout(0.1),
        )

        self.model4 = nn.Sequential(
            nn.Conv2d(256, 512, kernel_size=3, padding=1, bias=True),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, padding=1, bias=True),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, padding=1, bias=True),
            nn.ReLU(),
            nn.BatchNorm2d(512),
            nn.Dropout(0.1),
        )

        self.model5 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, dilation=2, padding=2, bias=True),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, dilation=2, padding=2, bias=True),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, dilation=2, padding=2, bias=True),
            nn.ReLU(),
            nn.BatchNorm2d(512),
            nn.Dropout(0.1),
        )

        self.model6 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, dilation=2, padding=2, bias=True),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, dilation=2, padding=2, bias=True),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=3, dilation=2, padding=2, bias=True),
            nn.ReLU(),
            nn.BatchNorm2d(512),
            nn.Dropout(0.1),
        )

        self.model7 = nn.Sequential(
            nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1, bias=True),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=True),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=True),
            nn.ReLU(),
            nn.BatchNorm2d(256),
            nn.Dropout(0.1),
        )

        self.model8 = nn.Sequential(
            nn.Upsample(scale_factor=2),
            nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1, bias=True),
            nn.ReLU(),
            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1, bias=True),
            nn.ReLU(),
            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1, bias=True),
            nn.ReLU(),
            nn.BatchNorm2d(128),
            nn.Dropout(0.1),
        )

        if loss_type == 'classification':
            self.model9 = nn.Sequential(
                nn.Conv2d(128, 313, kernel_size=1, stride=1, dilation=1, padding=0, bias=False),
                nn.Upsample(scale_factor=4),
                nn.Softmax(dim=1)
            )
        elif loss_type == 'regression':
            self.model9 = nn.Sequential(
                nn.Conv2d(128, 2, kernel_size=1, stride=1, dilation=1, padding=0, bias=False),
                nn.Upsample(scale_factor=4),
                nn.ReLU()
            )
Beispiel #27
0
#import tensorflow as tf
import numpy as np
import torch
import math
from torch import nn
import torch.optim as optim
from torch.autograd import Variable
import pandas as pd
import matplotlib.pyplot as plt

net = nn.Sequential(
    nn.Linear(2, 60),
    #通常會加入一個 non-linear layer
    nn.Linear(60, 60),
    nn.ReLU(),
    nn.Linear(60, 1),
    nn.Sigmoid())


class Bandit:
    def __init__(self,
                 alpha=10,
                 regulation_parameter=1,
                 exporation_parameter=0.01,
                 confidence_parameter=1,
                 norm_parameter=1,
                 step_size=20,
                 gradient_num=100,
                 network_width=2,
                 network_depth=100):
Beispiel #28
0
    def __init__(self, num_classes=20):
        super(PredictorNet, self).__init__()
        # TODO: Define model
        self.forward_conv = nn.Sequential(
            # Input: 256x256
            nn.Conv2d(12, 64,kernel_size=4, stride=1),
            nn.ReLU(True),
            nn.Conv2d(64, 128,kernel_size=3,stride=1),
            nn.ReLU(True),
            nn.Conv2d(128, 128, kernel_size=3, stride=1),
            nn.ReLU(True),
            nn.Conv2d(128, 128, kernel_size=3, stride=1),
            nn.ReLU(True),
        )
        self.encoder = nn.Sequential(
            nn.Linear(128 * 7 * 7, 1024),
            nn.ReLU(True),
            nn.Linear(1024, 2048)
        )
        self.forward_actions = nn.Sequential(
            nn.Linear(5, 1024),
            nn.ReLU(True),
            nn.Linear(1024, 2048)
        )

        self.reward = nn.Sequential(
            nn.Linear(2048, 512),
            nn.ReLU(True),
            nn.Linear(512,128),
            nn.ReLU(True),
            nn.Linear(128,3),
        )
        self.decoder = nn.Sequential(
            nn.Linear(2048, 1024),
            nn.ReLU(True),
            nn.Linear(1024, 128 * 7 * 7) #12 for 256
        )
        self.forward_deconv = nn.Sequential(
            nn.ConvTranspose2d(128, 128, kernel_size=3, stride=1),
            nn.ReLU(True),
            nn.ConvTranspose2d(128, 128, kernel_size=3, stride=1),
            nn.ReLU(True),
            nn.ConvTranspose2d(128, 64, kernel_size=3, stride=1),
            nn.ReLU(True),
            nn.ConvTranspose2d(64, 3, kernel_size=4, stride=1),
        )
 def __init__(self, in_channels, mid_channels, stride, dilation):
     super(bottleNeckIdentifyPSP, self).__init__()
     self.cbnr_1 = Conv2DBatchNormRelu(in_channels, mid_channels, kernel_size=1, stride=1, padding=0, dilation=1, bias=False)
     self.cbnr_2 = Conv2DBatchNormRelu(mid_channels, mid_channels, kernel_size=3, stride=1, padding=dilation, dilation=1, bias=False)
     self.cbn_1 = Conv2DBacthNorm(mid_channels, in_channels, kernel_size=1, stride = 1, padding=0, dilation=1, bias=False)
     self.relu = nn.ReLU(inplace=True)
    def __init__(self,
                 block,
                 layers,
                 sample_size,
                 sample_duration,
                 shortcut_type='B',
                 cardinality=16,
                 num_classes=400):
        self.inplanes = 64
        super(ResNeXt, self).__init__()
        self.conv1 = nn.Conv3d(in_channels=2,
                               out_channels=32,
                               kernel_size=3,
                               stride=(1, 1, 1),
                               padding=1,
                               bias=False)
        self.bn1 = nn.BatchNorm3d(32)
        self.bn2 = nn.BatchNorm3d(128)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool3d(kernel_size=(3, 3, 3), stride=1, padding=1)
        self.conv2 = nn.Conv3d(in_channels=32,
                               out_channels=64,
                               kernel_size=3,
                               stride=(1, 2, 2),
                               padding=1,
                               bias=False)
        self.conv3 = nn.Conv3d(in_channels=64,
                               out_channels=64,
                               kernel_size=3,
                               stride=(2, 2, 2),
                               padding=1,
                               bias=False)
        self.layer1 = self._make_layer(block, 64, layers[0], shortcut_type,
                                       cardinality)
        self.layer2 = self._make_layer(block,
                                       64,
                                       layers[1],
                                       shortcut_type,
                                       cardinality,
                                       stride=1)
        self.layer3 = self._make_layer(block,
                                       32,
                                       layers[1],
                                       shortcut_type,
                                       cardinality,
                                       stride=2)
        self.layer4 = self._make_layer(block,
                                       128,
                                       layers[1],
                                       shortcut_type,
                                       cardinality,
                                       stride=2)
        last_duration = int(math.ceil(sample_duration / 16))
        last_size = int(math.ceil(sample_size / 32))
        self.avgpool = nn.AvgPool3d((last_duration, last_size, last_size),
                                    stride=1)

        self.conv3d_8 = nn.Conv3d(in_channels=128,
                                  out_channels=128,
                                  kernel_size=3,
                                  stride=(1, 1, 1),
                                  padding=1)
        self.conv3d_9 = nn.Conv3d(in_channels=128,
                                  out_channels=16,
                                  kernel_size=5,
                                  stride=(1, 1, 1),
                                  padding=0)

        self.fc = nn.Linear(cardinality * 32 * block.expansion, num_classes)
        self.fc_layer1 = nn.Linear(36864, 128)
        self.fc_layer2 = nn.Linear(128, 6)

        self.fc1 = nn.Linear(76800, 128)
        self.fc2 = nn.Linear(128, 32)
        self.fc3 = nn.Linear(32, 6)

        for m in self.modules():
            if isinstance(m, nn.Conv3d):
                m.weight = nn.init.kaiming_normal(m.weight, mode='fan_out')
            elif isinstance(m, nn.BatchNorm3d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()