Exemple #1
0
 def __init__(self, *args, seq_channels=512, **kwargs):
     super().__init__(*args, **kwargs)
     self.seq_channels = seq_channels
     del self.linear
     nf = 32
     in_c = self.observation_space.shape[0]
     self.convs = nn.ModuleList([
         self._make_layer(
             nn.Conv2d(in_c, nf, 8, 4, 0, bias=self.norm is None)),
         self._make_layer(
             nn.Conv2d(nf, nf * 2, 6, 3, 0, bias=self.norm is None)),
         self._make_layer(
             nn.Conv2d(nf * 2, nf * 4, 4, 2, 0, bias=self.norm is None)),
     ])
     layer_norm = self.norm is not None and 'layer' in self.norm
     self.seq_conv = nn.Sequential(
         nn.ReplicationPad1d((3, 0)),
         nn.Conv1d(1920, seq_channels, 4),
         *([nn.GroupNorm(1, seq_channels)] if layer_norm else []),
         nn.ReLU(),
         nn.ReplicationPad1d((3, 0)),
         nn.Conv1d(seq_channels, seq_channels, 4, bias=False),
         *([nn.GroupNorm(1, seq_channels)] if layer_norm else []),
         nn.ReLU(),
     )
     self.reset_weights()
Exemple #2
0
 def __init__(self, param=None):
     super(TxtNet, self).__init__()
     self.conv1 = nn.Conv1d(in_channels=300,
                            out_channels=300,
                            kernel_size=1,
                            bias=True)
     self.conv2 = nn.Sequential(
         nn.ReplicationPad1d((0, 1)),
         nn.Conv1d(in_channels=300,
                   out_channels=300,
                   kernel_size=2,
                   bias=True))
     self.conv3 = nn.Sequential(
         nn.ReplicationPad1d((1, 1)),
         nn.Conv1d(in_channels=300,
                   out_channels=300,
                   kernel_size=3,
                   bias=True))
     self.conv5 = nn.Sequential(
         nn.ReplicationPad1d((2, 2)),
         nn.Conv1d(in_channels=300,
                   out_channels=300,
                   kernel_size=5,
                   bias=True))
     self.conv7 = nn.Sequential(
         nn.ReplicationPad1d((3, 3)),
         nn.Conv1d(in_channels=300,
                   out_channels=300,
                   kernel_size=7,
                   bias=True))
     self.slp = nn.Linear(300 * 5, 512)
Exemple #3
0
    def __init__(self, *args, h_action_size=32, seq_channels=512, **kwargs):
        super().__init__(*args, **kwargs)
        self.seq_channels = seq_channels

        del self.linear

        self.h_action_space = gym.spaces.Box(-1, 1, h_action_size)
        self.h_observation_space = gym.spaces.Box(-1, 1, seq_channels)
        self.h_pd = make_pd(self.h_action_space)

        nf = 32
        in_c = self.observation_space.shape[0]
        self.convs = nn.ModuleList([
            self._make_layer(
                nn.Conv2d(in_c, nf, 8, 4, 0, bias=self.norm is None)),
            self._make_layer(
                nn.Conv2d(nf, nf * 2, 6, 3, 0, bias=self.norm is None)),
            self._make_layer(
                nn.Conv2d(nf * 2, nf * 4, 4, 2, 0, bias=self.norm is None)),
            self._make_layer(
                nn.Conv2d(nf * 4, nf * 8, 3, 1, 0, bias=self.norm is None)),
        ])
        self.l1_seq_conv = nn.Sequential(
            nn.ReplicationPad1d((3, 0)),
            nn.Conv1d(768, seq_channels, 4, bias=False),
            nn.GroupNorm(1, seq_channels),
            nn.ReLU(),
            nn.ReplicationPad1d((3, 0)),
            nn.Conv1d(seq_channels, seq_channels, 4, bias=False),
            nn.GroupNorm(1, seq_channels),
            nn.ReLU(),
        )
        self.l2_seq_conv = nn.Sequential(
            nn.ReplicationPad1d((3, 0)),
            nn.Conv1d(seq_channels, seq_channels, 4, bias=False),
            nn.GroupNorm(1, seq_channels),
            nn.ReLU(),
            nn.ReplicationPad1d((3, 0)),
            nn.Conv1d(seq_channels, seq_channels, 4, bias=False),
            nn.GroupNorm(1, seq_channels),
            nn.ReLU(),
        )
        self.l2_action_upsample = nn.Sequential(
            nn.Linear(h_action_size, seq_channels, bias=False),
            nn.GroupNorm(1, seq_channels),
            nn.ReLU(),
        )
        self.l1_merge = nn.Sequential(
            nn.Linear(seq_channels * 2, seq_channels, bias=False),
            nn.GroupNorm(1, seq_channels),
            nn.ReLU(),
        )

        self.head_l2 = ActorCriticHead(seq_channels, self.h_pd)
        self.reset_weights()
Exemple #4
0
    def _forward(self, x):

        # Compute padding to compromise the downsample loss
        input_len = x.shape[0]
        left_over = input_len % self.dr

        if left_over % 2 == 0:
            left_pad = left_over // 2
            right_pad = left_pad
        else:
            left_pad = left_over // 2
            right_pad = left_over // 2 + 1

        # Model forwarding
        x = x.permute(1, 0, 2).contiguous()  # (T, B, D) -> (B, T, D)
        spec_stacked, pos_enc, attn_mask = self.process_MAM_data(x)
        x = self.model(spec_stacked,
                       pos_enc,
                       attn_mask,
                       output_all_encoded_layers=False)  # (B, T, D)

        # If using a downsampling model, apply tile and padding
        if x.shape[1] != input_len:
            x = self.tile_representations(x)

            # padding
            x = x.permute(0, 2, 1).contiguous()  # (B, T, D) -> (B, D, T)
            padding = nn.ReplicationPad1d((left_pad, right_pad))
            x = padding(x)
            x = x.permute(2, 0, 1).contiguous()  # (B, D, T) -> (T, B, D)

        # If not using a downsampling model, permute to output
        else:
            x = x.permute(1, 0, 2).contiguous()  # (B, T, D) -> (T, B, D)
        return x
Exemple #5
0
    def __init__(self, width, input_len, level=None, device=None):
        super().__init__()

        self.filter_len = 2

        if level is not None:
            self.level = level
        else:
            self.level = self.max_dwt_level(input_len)
        self.input_len = input_len
        self.width = width

        # This is bit manipulation to assert that xlen is a power of 2
        if not ((self.input_len &
                 (self.input_len - 1) == 0) and self.input_len != 0):
            raise ValueError("Input array length {} is not power of 2".format(
                self.input_len))
        if self.level > self.max_dwt_level(self.input_len):
            s = "Input array length {} gives max DWT level {}".format(
                self.input_len, self.max_dwt_level(self.input_len))
            raise ValueError(s)

        self.c_filter = torch.tensor(np.divide(np.array([1., 1.]), np.sqrt(2)),
                                     dtype=torch.float).reshape((1, 1, 2))
        self.d_filter = torch.tensor(np.divide(np.array([1., -1.]),
                                               np.sqrt(2)),
                                     dtype=torch.float).reshape((1, 1, 2))
        if device is not None:
            self.c_filter = self.c_filter.to(device)
            self.d_filter = self.d_filter.to(device)

        # If the input array is odd-length, pad the array by repeating the
        # last element
        self.padder = nn.ReplicationPad1d((0, 1))
Exemple #6
0
 def __init__(self, n_channels, dilations, use_skip=True):
     super(DenseResBlocks1d, self).__init__()
     self.use_skip = use_skip
     self.n_layers = len(dilations)
     self.convs = nn.ModuleList()
     self.skips = nn.ModuleList()
     for i, dilation in enumerate(dilations):
         self.convs.append(
             nn.Sequential(
                 nn.ReplicationPad1d(dilation),
                 utils.wn_xavier(
                     nn.Conv1d(n_channels,
                               2 * n_channels,
                               3,
                               dilation=dilation)),
                 Func(F.glu, dim=1),
             ))
         last_c = (4 *
                   n_channels if use_skip and i < self.n_layers - 1 else 2 *
                   n_channels)
         self.skips.append(
             nn.Sequential(
                 utils.wn_xavier(nn.Conv1d(n_channels, last_c, 1)),
                 Func(F.glu, dim=1),
             ))
Exemple #7
0
 def __init__(self, channels, ratio, kernel_size, negative_slope=0.01, dropout=0.5):
     super(BaseComponent, self).__init__()
     self.layers = nn.Sequential(
         nn.ReplicationPad1d(kernel_size // 2 * 2), nn.Conv1d(channels, int(channels * ratio), kernel_size),
         nn.LeakyReLU(negative_slope), nn.Dropout(dropout),
         nn.Conv1d(int(channels * ratio), channels, kernel_size), nn.Tanh()
     )
    def __init__(self, sample_rate=16000, n_fft=401, hop_length=256, n_mels=23, context_size=7, subsample=16):
        super(LogMel, self).__init__()

        self.stft = MelSpectrogram(sample_rate=sample_rate, n_fft=n_fft, hop_length=hop_length, n_mels=n_mels)
        self.pad = nn.ReplicationPad1d(padding=context_size)
        self.context_size = context_size
        self.subsample = subsample
Exemple #9
0
 def __init__(self, pad):
     super(ReplicationPad, self).__init__()
     if len(pad) == 6:
         self.pad = nn.ReplicationPad3d(list(reversed(pad)))
     if len(pad) == 4:
         self.pad = nn.ReplicationPad2d(list(reversed(pad)))
     if len(pad) == 2:
         self.pad = nn.ReplicationPad1d(list(reversed(pad)))
Exemple #10
0
 def __init__(self):
     super(Resblock, self).__init__()
     self.pad = nn.ReplicationPad1d((2, 0))
     self.conv1 = nn.Conv1d(256, 512, kernel_size=3, stride=1)
     self.ins_norm_1 = nn.InstanceNorm1d(512)
     self.conv_gate_1 = nn.Conv1d(256, 512, kernel_size=3, stride=1)
     self.ins_gate_1 = nn.InstanceNorm1d(512)
     self.conv2 = nn.Conv1d(512, 256, kernel_size=3, stride=1)
     self.ins_norm_2 = nn.InstanceNorm1d(256)
 def __init__(self, dim, dilation=1):
     super().__init__()
     self.block = nn.Sequential(
         nn.LeakyReLU(0.2),
         nn.ReplicationPad1d(dilation),
         WNConv1d(dim, dim, kernel_size=3, dilation=dilation),
         nn.LeakyReLU(0.2),
         WNConv1d(dim, dim, kernel_size=1),
     )
     self.shortcut = WNConv1d(dim, dim, kernel_size=1)
Exemple #12
0
    def _forward(self, x):

        if self.permute_input:
            x = x.permute(1, 0, 2).contiguous() # (T, B, D) -> (B, T, D)
            input_len = x.shape[0]
        else:
            input_len = x.shape[1]

        # Compute padding to compromise the downsample loss
        left_over = input_len % self.dr
        if left_over % 2 == 0:
            left_pad = left_over // 2
            right_pad = left_pad
        else:
            left_pad = left_over // 2
            right_pad = left_over // 2 + 1

        # Model forwarding
        spec_stacked, pos_enc, attn_mask = self.process_input_data(x) # x shape: (B, T, D)
        x = self.model(spec_stacked, pos_enc, attn_mask, output_all_encoded_layers=self.weighted_sum or self.select_layer != -1) # (B, T, D) or # (N, B, T, D)

        # Apply weighted sum
        if self.weighted_sum:
            if type(x) is list: x = torch.stack(x)
            softmax_weight = nn.functional.softmax(self.weight, dim=-1)
            B, T, D = x.shape[1], x.shape[2], x.shape[3]
            x = x.reshape(self.num_layers, -1)
            x = torch.matmul(softmax_weight, x).reshape(B, T, D)
        # Select a specific layer
        elif self.select_layer != -1:
            x = x[self.select_layer]

        if self.spec_aug and not self.spec_aug_prev and self.model.training:
            x = spec_augment(x, mask_T=70, mask_F=86, num_T=2, num_F=2, p=1.0) # (B, T, D)

        # If using a downsampling model, apply tile and padding
        if x.shape[1] != input_len:
            x = self.tile_representations(x)

            # padding
            x = x.permute(0, 2, 1).contiguous() # (B, T, D) -> (B, D, T)
            padding = nn.ReplicationPad1d((left_pad, right_pad))
            x = padding(x)
            
            if self.permute_input: x = x.permute(2, 0, 1).contiguous() # (B, D, T) -> (T, B, D)
            else: x = x.permute(0, 2, 1).contiguous() # (B, D, T) -> (B, T, D)
        
        # If not using a downsampling model, permute to output
        elif self.permute_input:
            x = x.permute(1, 0, 2).contiguous() # (B, T, D) -> (T, B, D)
        
        # else: (B, T, D)
        return x
Exemple #13
0
 def __init__(self, in_channels, out_channels):
     super(G_Downsample, self).__init__()
     self.pad = nn.ReplicationPad1d((4, 0))
     self.conv = nn.Conv1d(in_channels=in_channels,
                           out_channels=out_channels,
                           kernel_size=5,
                           stride=2)
     self.conv_gate = nn.Conv1d(in_channels=in_channels,
                                out_channels=out_channels,
                                kernel_size=5,
                                stride=2)
     self.ins_norm = nn.InstanceNorm1d(out_channels)
     self.ins_gate = nn.InstanceNorm1d(out_channels)
    def build_conv_block(self, dim, padding_type, norm_layer, use_dropout,
                         use_bias):
        conv_block = []
        p = 0
        if padding_type == 'reflect':
            conv_block += [nn.ReflectionPad1d(1)]
        elif padding_type == 'replicate':
            conv_block += [nn.ReplicationPad1d(1)]
        elif padding_type == 'zero':
            p = 1
        else:
            raise NotImplementedError('padding [%s] is not implemented' %
                                      padding_type)

        conv_block += [
            nn.Conv1d(dim, dim, kernel_size=3, padding=p, bias=use_bias),
            norm_layer(dim),
            nn.ReLU(True)
        ]
        if use_dropout:
            conv_block += [nn.Dropout(0.5)]

        p = 0
        if padding_type == 'reflect':
            conv_block += [nn.ReflectionPad1d(1)]
        elif padding_type == 'replicate':
            conv_block += [nn.ReplicationPad1d(1)]
        elif padding_type == 'zero':
            p = 1
        else:
            raise NotImplementedError('padding [%s] is not implemented' %
                                      padding_type)

        conv_block += [
            nn.Conv1d(dim, dim, kernel_size=3, padding=p, bias=use_bias),
            norm_layer(dim)
        ]

        return nn.Sequential(*conv_block)
    def __init__(self, input_size, ngf, n_residual_layers):
        super().__init__()
        ratios = [8, 8, 2, 2]
        self.hop_length = np.prod(ratios)
        mult = int(2**len(ratios))

        model = [
            nn.ReplicationPad1d(3),
            WNConv1d(input_size, mult * ngf, kernel_size=7, padding=0),
        ]

        # Upsample to raw audio scale
        for i, r in enumerate(ratios):
            model += [
                nn.LeakyReLU(0.2),
                WNConvTranspose1d(
                    mult * ngf,
                    mult * ngf // 2,
                    kernel_size=r * 2,
                    stride=r,
                    padding=r // 2 + r % 2,
                    output_padding=r % 2,
                ),
            ]

            for j in range(n_residual_layers):
                model += [ResnetBlock(mult * ngf // 2, dilation=3**j)]

            mult //= 2

        model += [
            nn.LeakyReLU(0.2),
            nn.ReplicationPad1d(3),
            WNConv1d(ngf, 1, kernel_size=7, padding=0),
            nn.Tanh(),
        ]

        self.model = nn.Sequential(*model)
        self.apply(weights_init)
Exemple #16
0
        def block(n_in, n_out, k, stride):
            # padding dims only really make sense for stride = 1
            ka = k // 2
            kb = ka - 1 if k % 2 == 0 else ka

            pad = ZeroPad1d(ka + kb, 0) if zero_pad else nn.ReplicationPad1d((ka + kb, 0))

            return nn.Sequential(
                pad,
                nn.Conv1d(n_in, n_out, k, stride=stride, bias=conv_bias),
                nn.Dropout(p=dropout),
                norm_block(False, n_out, affine=not non_affine_group_norm),
                nn.ReLU(),
            )
Exemple #17
0
    def __init__(self, in_channels=80, num_features=512, out_channels=4, time_window=2, num_blocks=2):
        super().__init__()
        self.in_channels = in_channels
        self.num_features = num_features
        self.out_channels = out_channels
        self.num_blocks = num_blocks
        self.time_window = time_window

        self.conv1 = nn.Sequential(
                         nn.ReplicationPad1d(1),
                         nn.Conv1d(self.in_channels, self.num_features, kernel_size=3, bias=False),
                         nn.BatchNorm1d(self.num_features),
                         nn.ReLU(inplace=True),
                         nn.Dropout(p=0.25)
                     )
        self._make_blocks()
        self.pad = nn.ReplicationPad1d(1)
        self.relu = nn.ReLU(inplace=True)
        self.drop = nn.Dropout(p=0.25)
        # Reduce the dimension
        self.reduce = nn.Conv1d(self.num_features, self.num_features, kernel_size=2*self.time_window+1)
        # Output logits for training mode
        self.out = nn.Linear(self.num_features, self.out_channels)
    def upsample(self, x, input_len):
        # Compute padding to compromise the downsample loss
        left_over = input_len % self.dr
        if left_over % 2 == 0:
            left_pad = left_over // 2
            right_pad = left_pad
        else:
            left_pad = left_over // 2
            right_pad = left_over // 2 + 1
        
        x = self.tile_representations(x)

        # padding
        x = x.permute(0, 2, 1).contiguous() # (B, T, D) -> (B, D, T)
        padding = nn.ReplicationPad1d((left_pad, right_pad))
        x = padding(x)
        
        x = x.permute(0, 2, 1).contiguous() # (B, D, T) -> (B, T, D)
        return x
Exemple #19
0
    def __init__(self, flows, n_group, sr, window_size, n_mels, use_conv1x1,
                 memory_efficient, **kwargs):
        super().__init__()
        self.flows = flows
        self.n_group = n_group
        self.win_size = window_size
        self.hop_size = 256
        self.n_mels = n_mels
        self.sr = sr
        self.sub_sr = self.hop_size // n_group

        self.upsampler = nn.Sequential(
            nn.ReplicationPad1d((0, 1)),
            nn.ConvTranspose1d(n_mels,
                               n_mels,
                               self.sub_sr * 2 + 1,
                               self.sub_sr,
                               padding=self.sub_sr), nn.LeakyReLU(0.4, True))
        self.upsampler.apply(add_weight_norms)

        self.WNs = nn.ModuleList()

        if use_conv1x1:
            self.invconv1x1 = nn.ModuleList()

        # Set up layers with the right sizes based on how many dimensions
        # have been output already
        for k in range(flows):
            self.WNs.append(WN2D(n_group, n_mels, **kwargs))
            if use_conv1x1:
                self.invconv1x1.append(
                    InvertibleConv1x1(n_group,
                                      memory_efficient=memory_efficient))

        self.mel = nn.Sequential(
            nn.ReflectionPad1d((window_size // 2 - self.hop_size // 2,
                                window_size // 2 + self.hop_size // 2)),
            MelSpectrogram(sr,
                           window_size,
                           n_mels,
                           self.hop_size,
                           center=False,
                           fmax=8000))
Exemple #20
0
 def __init__(self, input_dim, output_dim, kernel_size):
     """Construct a convolution layer with the specified sizes
     
     Arguments:
         input_dim {int} -- the dimension of the input
         output_dim {int} -- the dimension of the ouput
         kernel_size {[type]} -- the size of the filters. With a kernel_size of k, the filter will look at the (k - 1) / 2
             previous and next rows to compute the current one
     """
     super().__init__()
     
     self.input_dim = input_dim
     self.output_dim = output_dim
     
     # the size of the kernel must be odd
     assert kernel_size % 2 == 1
     
     self.padder = nn.ReplicationPad1d(kernel_size // 2)
     self.conv = nn.Conv1d(input_dim, output_dim, kernel_size)
Exemple #21
0
 def __init__(self, input_dim, output_dim, kernel_size):
     """Construct a convolution layer with the specified sizes
     
     Arguments:
         input_dim {int} -- the dimension of the input
         output_dim {int} -- the dimension of the ouput
         kernel_size {[type]} -- the size of the filters. With a kernel_size of k, the filter will generate values for the (k - 1) / 2
             previous and next rows as well as the current one.
     """
     super().__init__()
     
     self.input_dim = input_dim
     self.output_dim = output_dim
     self.kernel_size = kernel_size
     
     # the size of the kernel must be odd
     assert kernel_size % 2 == 1
     
     self.padder = nn.ReplicationPad1d(kernel_size // 2)
     self.conv = nn.ConvTranspose1d(input_dim, output_dim, kernel_size)
Exemple #22
0
    def __init__(self, nf0, per_feature, grid_dim):
        super().__init__()

        in_channels = nf0

        if per_feature:
            weights_channels = nf0
        else:
            weights_channels = 1
        self.new_integration_octree = nn.Sequential(
            nn.ReplicationPad1d(1),
            nn.Conv1d(in_channels, nf0, kernel_size=3, padding=0, bias=True)
        )
        self.old_integration_octree = nn.Sequential(
            nn.ReplicationPad1d(1),
            nn.Conv1d(in_channels, nf0, kernel_size=3, padding=0, bias=False)
        )

        self.update_old_net_octree = nn.Sequential(
            nn.ReplicationPad1d(1),
            nn.Conv1d(in_channels, weights_channels, kernel_size=3, padding=0, bias=True)
        )

        self.update_new_net_octree = nn.Sequential(
            nn.ReplicationPad1d(1),
            nn.Conv1d(in_channels, weights_channels, kernel_size=3, padding=0, bias=False)
        )

        self.reset_old_net = nn.Sequential(
            nn.ReplicationPad1d(1),
            nn.Conv1d(in_channels, weights_channels, kernel_size=3, padding=0, bias=True)
        )

        self.reset_new_net = nn.Sequential(
            nn.ReplicationPad1d(1),
            nn.Conv1d(in_channels, weights_channels, kernel_size=3, padding=0, bias=False),
        )
        self.sigmoid = nn.Sigmoid()

        self.relu = nn.ReLU()

        coord_conv_volume = np.mgrid[-grid_dim // 2:grid_dim // 2,
                                     -grid_dim // 2:grid_dim // 2,
                                     -grid_dim // 2:grid_dim // 2]

        coord_conv_volume = np.stack(coord_conv_volume, axis=0).astype(np.float32)
        coord_conv_volume = coord_conv_volume / grid_dim
        self.coord_conv_volume = torch.Tensor(coord_conv_volume).float().cuda()[None, :, :, :, :]
        self.counter = 0
Exemple #23
0
 def __init__(self):
     super(Generator, self).__init__()
     self.pad = nn.ReplicationPad1d((14, 0))
     self.conv_first = nn.Conv1d(in_channels=24,
                                 out_channels=128,
                                 kernel_size=15,
                                 stride=1)
     self.conv_first_gate = nn.Conv1d(in_channels=24,
                                      out_channels=128,
                                      kernel_size=15,
                                      stride=1)
     self.down_sample1 = G_Downsample(128, 256)
     self.down_sample2 = G_Downsample(256, 256)
     self.res_block = Res6block()
     self.up_sample1 = G_Upsample(256, 512)
     self.up_sample2 = G_Upsample(256, 256)
     self.conv_last = nn.Conv1d(in_channels=128,
                                out_channels=24,
                                kernel_size=15,
                                stride=1)
    def __init__(self, ndf, n_layers, downsampling_factor):
        super().__init__()
        model = nn.ModuleDict()

        model["layer_0"] = nn.Sequential(
            nn.ReplicationPad1d(7),
            WNConv1d(1, ndf, kernel_size=15),
            nn.LeakyReLU(0.2, True),
        )

        nf = ndf
        stride = downsampling_factor
        for n in range(1, n_layers + 1):
            nf_prev = nf
            nf = min(nf * stride, 1024)

            model["layer_%d" % n] = nn.Sequential(
                WNConv1d(
                    nf_prev,
                    nf,
                    kernel_size=stride * 10 + 1,
                    stride=stride,
                    padding=stride * 5,
                    groups=nf_prev // 4,
                ),
                nn.LeakyReLU(0.2, True),
            )

        nf = min(nf * 2, 1024)
        model["layer_%d" % (n_layers + 1)] = nn.Sequential(
            WNConv1d(nf_prev, nf, kernel_size=5, stride=1, padding=2),
            nn.LeakyReLU(0.2, True),
        )

        model["layer_%d" % (n_layers + 2)] = WNConv1d(nf,
                                                      1,
                                                      kernel_size=3,
                                                      stride=1,
                                                      padding=1)

        self.model = model
Exemple #25
0
def Gaussin_smooth(x):
    '''
    x: N * timestep * 4
    '''
    # Create gaussian kernels
    win_size = 5
    std = 1
    box_dim = 4

    x_mask = x > 0
    x_mask_neg = 1 - x_mask.float()

    x = x * x_mask.float()

    obj_num, ftr_dim = x.shape
    time_step = int(ftr_dim / box_dim)
    x_trans = x.view(obj_num, time_step, box_dim).permute(0, 2, 1)

    pad_size = int((win_size - 1) / 2)
    filter_param = signal.gaussian(win_size, std)
    filter_param = filter_param / np.sum(filter_param)
    kernel = torch.tensor(filter_param, dtype=x.dtype, device=x.device)

    pad_fun = nn.ReplicationPad1d(pad_size)
    x_trans_pad = pad_fun(x_trans)

    # Apply smoothing
    x_smooth_trans = F.conv1d(x_trans_pad.contiguous().view(
        -1, 1, time_step + pad_size * 2),
                              kernel.unsqueeze(0).unsqueeze(0),
                              padding=0)
    x_smooth_trans = x_smooth_trans.view(obj_num, box_dim, time_step)
    x_smooth = x_smooth_trans.permute(0, 2, 1)
    x_smooth = x_smooth.contiguous().view(obj_num, ftr_dim)
    # remask
    x_smooth = x_smooth * x_mask.float()
    x_smooth += x_mask_neg.float() * (-1)
    #pdb.set_trace()
    return x_smooth.squeeze()
Exemple #26
0
    def __init__(self):
        super(NNPaddingModule, self).__init__()
        self.input1d = torch.randn(1, 4, 50)
        self.module1d = nn.ModuleList([
            nn.ReflectionPad1d(2),
            nn.ReplicationPad1d(2),
            nn.ConstantPad1d(2, 3.5),
        ])

        self.input2d = torch.randn(1, 4, 30, 10)
        self.module2d = nn.ModuleList([
            nn.ReflectionPad2d(2),
            nn.ReplicationPad2d(2),
            nn.ZeroPad2d(2),
            nn.ConstantPad2d(2, 3.5),
        ])

        self.input3d = torch.randn(1, 4, 10, 4, 4)
        self.module3d = nn.ModuleList([
            nn.ReflectionPad3d(1),
            nn.ReplicationPad3d(3),
            nn.ConstantPad3d(3, 3.5),
        ])
    def __init__(self):
        super(Model, self).__init__()

        self.pad_0 = nn.ReplicationPad1d(2)
        self.pad_1 = nn.ReplicationPad1d(padding=(3,4))
        self.pad_2 = nn.ReplicationPad1d(padding=(1,0))
    def __init__(self,
                 input_dim,
                 output_dim,
                 kernel_size,
                 stride,
                 padding=0,
                 norm='none',
                 activation='relu',
                 pad_type='zero'):
        super(Conv1dBlock, self).__init__()
        self.use_bias = True
        # initialize padding
        if pad_type == 'reflect':
            self.pad = nn.ReflectionPad1d(padding)
        elif pad_type == 'replicate':
            self.pad = nn.ReplicationPad1d(padding)
        elif pad_type == 'zero':
            self.pad = None  # just using default function
        elif pad_type == 'none':
            self.pad = None
        else:
            assert 0, "Unsupported padding type: {}".format(pad_type)

        # initialize normalization
        norm_dim = output_dim
        if norm == 'bn':
            self.norm = nn.BatchNorm1d(norm_dim)
        elif norm == 'in':
            self.norm = nn.InstanceNorm1d(norm_dim)
        elif norm == 'ln':
            self.norm = nn.LayerNorm(norm_dim)
        elif norm == 'none':
            self.norm = None
        else:
            assert 0, "Unsupported normalization: {}".format(norm)

        # initialize activation
        if activation == 'relu':
            self.activation = nn.ReLU(inplace=True)
        elif activation == 'lrelu':
            self.activation = nn.LeakyReLU(0.2, inplace=True)
        elif activation == 'prelu':
            self.activation = nn.PReLU()
        elif activation == 'selu':
            self.activation = nn.SELU(inplace=True)
        elif activation == 'tanh':
            self.activation = nn.Tanh()
        elif activation == 'none':
            self.activation = None
        else:
            assert 0, "Unsupported activation: {}".format(activation)

        # initialize convolution
        if pad_type == 'zero':
            self.conv = nn.Conv1d(input_dim,
                                  output_dim,
                                  kernel_size,
                                  stride,
                                  padding,
                                  bias=self.use_bias)
        else:
            self.conv = nn.Conv1d(input_dim,
                                  output_dim,
                                  kernel_size,
                                  stride,
                                  bias=self.use_bias)
Exemple #29
0
    def __init__(self, args, in_planes, splitting=True, dropout=0.5,
                 simple_lifting=False):
        super(Interactor, self).__init__()
        self.modified = args.INN
        kernel_size = args.kernel
        dilation = args.dilation
        self.dropout = args.dropout

        pad = dilation * (kernel_size - 1) // 2 + 1  # 2 1 0 0
        # pad = k_size // 2
        self.splitting = splitting
        self.split = Splitting()

        # Dynamic build sequential network
        modules_P = []
        modules_U = []
        modules_psi = []
        modules_phi = []
        prev_size = 1

        # HARD CODED Architecture
        if simple_lifting:
            modules_P += [
                nn.ReplicationPad1d(pad),
                nn.Conv2d(in_planes, in_planes,
                          kernel_size=kernel_size, stride=1),
                nn.Dropout(self.dropout),
                nn.Tanh()
            ]
            modules_U += [
                nn.ReplicationPad1d(pad),
                nn.Conv2d(in_planes, in_planes,
                          kernel_size=kernel_size, stride=1),

                nn.Dropout(self.dropout),
                nn.Tanh()
            ]
        else:
            size_hidden = args.hidden_size
            modules_P += [
                nn.ReplicationPad1d(pad),
                nn.Conv1d(in_planes * prev_size, int(in_planes * size_hidden),
                          kernel_size=kernel_size, dilation=dilation, stride=1, groups= args.groups),
                nn.LeakyReLU(negative_slope=0.01, inplace=True),
                nn.Dropout(self.dropout),
                nn.Conv1d(int(in_planes * size_hidden), in_planes,
                          kernel_size=3, stride=1, groups= args.groups),
                nn.Tanh()
            ]
            modules_U += [
                nn.ReplicationPad1d(pad),
                nn.Conv1d(in_planes * prev_size, int(in_planes * size_hidden),
                          kernel_size=kernel_size, dilation=dilation, stride=1, groups= args.groups),
                nn.LeakyReLU(negative_slope=0.01, inplace=True),
                nn.Dropout(self.dropout),
                nn.Conv1d(int(in_planes * size_hidden), in_planes,
                          kernel_size=3, stride=1, groups= args.groups),
                nn.Tanh()
            ]
            if self.modified:
                modules_phi += [
                    nn.ReplicationPad1d(pad),
                    nn.Conv1d(in_planes * prev_size, int(in_planes * size_hidden),
                              kernel_size=kernel_size, dilation=dilation, stride=1, groups= args.groups),
                    nn.LeakyReLU(negative_slope=0.01, inplace=True),
                    nn.Dropout(self.dropout),
                    nn.Conv1d(int(in_planes * size_hidden), in_planes,
                              kernel_size=3, stride=1, groups= args.groups),
                    nn.Tanh()
                ]
                modules_psi += [
                    nn.ReplicationPad1d(pad),
                    nn.Conv1d(in_planes * prev_size, int(in_planes * size_hidden),
                              kernel_size=kernel_size, dilation=dilation, stride=1, groups= args.groups),
                    nn.LeakyReLU(negative_slope=0.01, inplace=True),
                    nn.Dropout(self.dropout),
                    nn.Conv1d(int(in_planes * size_hidden), in_planes,
                              kernel_size=3, stride=1, groups= args.groups),
                    nn.Tanh()
                ]
                self.phi = nn.Sequential(*modules_phi)
                self.psi = nn.Sequential(*modules_psi)

        self.P = nn.Sequential(*modules_P)
        self.U = nn.Sequential(*modules_U)
Exemple #30
0
    def __init__(self,
                 in_planes,
                 modified=False,
                 size=[],
                 splitting=True,
                 k_size=4,
                 dropout=0.5,
                 simple_lifting=False):
        super(LiftingScheme, self).__init__()
        self.modified = modified

        kernel_size = k_size
        pad = k_size // 2  # 2 1 0 0

        self.splitting = splitting
        self.split = Splitting()

        # Dynamic build sequential network
        modules_P = []
        modules_U = []
        modules_psi = []
        modules_phi = []
        prev_size = 1

        # HARD CODED Architecture
        if simple_lifting:
            modules_P += [
                nn.ReflectionPad2d(pad),
                nn.Conv2d(in_planes,
                          in_planes,
                          kernel_size=kernel_size,
                          stride=1),
                nn.Tanh()
            ]
            modules_U += [
                nn.ReflectionPad2d(pad),
                nn.Conv2d(in_planes,
                          in_planes,
                          kernel_size=kernel_size,
                          stride=1),
                nn.Tanh()
            ]
        else:
            size_hidden = 4  # Todo:   extend

            modules_P += [
                nn.ReflectionPad1d(pad),
                # nn.ReplicationPad1d(pad),
                nn.Conv1d(
                    in_planes * prev_size,
                    in_planes * size_hidden,  #3-6
                    kernel_size=kernel_size,
                    stride=1),
                #  Todo:  BN
                nn.ReLU(),
                # nn.BatchNorm1d(in_planes * size_hidden, momentum=0.1),
                # nn.LeakyReLU(negative_slope=0.01, inplace=True),
                nn.Dropout(dropout),  # Todo: change value
            ]
            modules_U += [
                nn.ReplicationPad1d(pad),
                # nn.ReplicationPad1d(pad),
                nn.Conv1d(in_planes * prev_size,
                          in_planes * size_hidden,
                          kernel_size=kernel_size,
                          stride=1),
                nn.ReLU(),  # Todo: leakyrelu  0.1
                # nn.BatchNorm1d(in_planes * size_hidden, momentum=0.1),
                # nn.LeakyReLU(negative_slope=0.01, inplace=True),
                nn.Dropout(dropout),
            ]
            prev_size = size_hidden

            # Final dense
            modules_P += [
                nn.Conv1d(in_planes * prev_size,
                          in_planes,
                          kernel_size=2,
                          stride=1),
                nn.Tanh(),  # Todo: leakyrelu  0.1
                # nn.BatchNorm1d(in_planes , momentum=0.1),
                # nn.LeakyReLU(negative_slope=0.01, inplace=True),
                nn.Dropout(dropout),
            ]
            modules_U += [
                nn.Conv1d(
                    in_planes * prev_size,
                    in_planes,  #6-3
                    kernel_size=2,
                    stride=1),
                nn.Tanh(),
                # nn.BatchNorm1d(in_planes, momentum=0.1),
                # nn.LeakyReLU(negative_slope=0.01, inplace=True),
                nn.Dropout(dropout),
            ]

            modules_phi += [
                nn.ReplicationPad1d(pad),
                nn.Conv1d(in_planes,
                          in_planes,
                          kernel_size=kernel_size,
                          stride=1),
                nn.Conv1d(in_planes, in_planes, kernel_size=2, stride=1),
                nn.Tanh()
            ]
            modules_psi += [
                nn.ReplicationPad1d(pad),
                nn.Conv1d(in_planes,
                          in_planes,
                          kernel_size=kernel_size,
                          stride=1),
                nn.Conv1d(in_planes, in_planes, kernel_size=2, stride=1),
                nn.Tanh()
            ]

        self.P = nn.Sequential(*modules_P)
        self.U = nn.Sequential(*modules_U)
        self.phi = nn.Sequential(*modules_phi)
        self.psi = nn.Sequential(*modules_psi)