def update_model_kwargs_for_generation(outputs, model_kwargs): # update cache if isinstance(outputs, tuple): model_kwargs["cache"] = outputs[1] # update token_type_ids with last value if "token_type_ids" in model_kwargs: token_type_ids = model_kwargs["token_type_ids"] model_kwargs["token_type_ids"] = paddle.concat( [token_type_ids, token_type_ids[:, -1].unsqueeze(-1)], axis=-1) # update position_ids if "position_ids" in model_kwargs: position_ids = model_kwargs["position_ids"] model_kwargs["position_ids"] = paddle.concat( [position_ids, position_ids[:, -1].unsqueeze(-1) + 1], axis=-1) # update attention_mask if "attention_mask" in model_kwargs: attention_mask = model_kwargs["attention_mask"] # TODO attention_mask = nn.Pad2D([0, 0, 0, 1], mode='replicate')(attention_mask) attention_mask = nn.Pad2D([0, 1, 0, 0], value=-1e9)(attention_mask) dtype = convert_dtype(attention_mask.dtype) if dtype == 'bool': attention_mask[:, :, -1, -1] = True elif 'int' in dtype: attention_mask[:, :, -1, -1] = 1 else: attention_mask[:, :, -1, -1] = 0.0 model_kwargs["attention_mask"] = attention_mask return model_kwargs
def __init__(self, dim, use_bias, norm_layer): super(ResnetBlock, self).__init__() conv_block = [] conv_block += [ nn.Pad2D(padding=[1, 1, 1, 1], mode="reflect"), nn.Conv2D(dim, dim, kernel_size=3, stride=1, padding=0, bias_attr=use_bias), norm_layer(dim), nn.ReLU() ] conv_block += [ nn.Pad2D(padding=[1, 1, 1, 1], mode="reflect"), nn.Conv2D(dim, dim, kernel_size=3, stride=1, padding=0, bias_attr=use_bias), norm_layer(dim) ] self.conv_block = nn.Sequential(*conv_block)
def __init__(self, name, channels, norm_layer, use_dropout, use_dilation, use_bias): super(ResBlock, self).__init__() if use_dilation: padding_mat = [1, 1, 1, 1] else: padding_mat = [0, 0, 0, 0] self._pad1 = nn.Pad2D(padding_mat, mode="replicate") self._sn_conv1 = SNConv(name=name + "_sn_conv1", in_channels=channels, out_channels=channels, kernel_size=3, padding=0, norm_layer=norm_layer, use_bias=use_bias, act="ReLU", act_attr=None) if use_dropout: self._dropout = nn.Dropout(0.5) else: self._dropout = None self._pad2 = nn.Pad2D([1, 1, 1, 1], mode="replicate") self._sn_conv2 = SNConv(name=name + "_sn_conv2", in_channels=channels, out_channels=channels, kernel_size=3, norm_layer=norm_layer, use_bias=use_bias, act="ReLU", act_attr=None)
def test_class(self): paddle.disable_static() paddle.device.set_device("npu") input_shape = (3, 4, 5, 6) pad = [1, 2, 2, 1] pad_int = 1 value = 0 input_data = np.random.rand(*input_shape).astype(np.float32) pad_constant = nn.Pad2D(padding=pad, mode="constant", value=value) pad_constant_int = nn.Pad2D(padding=pad_int, mode="constant", value=value) data = paddle.to_tensor(input_data) output = pad_constant(data) np_out = self._get_numpy_out(input_data, pad, "constant", value=value, data_format="NCHW") self.assertTrue(np.allclose(output.numpy(), np_out)) output = pad_constant_int(data) np_out = self._get_numpy_out(input_data, [pad_int] * 4, "constant", value=value, data_format="NCHW") self.assertTrue(np.allclose(output.numpy(), np_out))
def __init__(self, dim, use_bias=False): super(ResnetSoftAdaLINBlock, self).__init__() self.pad1 = nn.Pad2D([1, 1, 1, 1], 'reflect') self.conv1 = nn.Conv2D(dim, dim, kernel_size=3, stride=1, bias_attr=use_bias) self.norm1 = SoftAdaLIN(dim) self.relu1 = nn.ReLU() self.pad2 = nn.Pad2D([1, 1, 1, 1], 'reflect') self.conv2 = nn.Conv2D(dim, dim, kernel_size=3, stride=1, bias_attr=use_bias) self.norm2 = SoftAdaLIN(dim)
def __init__(self, input_nc, ndf=64, n_layers=5): super(Discriminator, self).__init__() model = [ nn.Pad2D([1, 1, 1, 1], 'reflect'), spectral_norm( nn.Conv2D(input_nc, ndf, kernel_size=4, stride=2, bias_attr=True)), nn.LeakyReLU(0.2) ] for i in range(1, n_layers - 2): mult = 2**(i - 1) model += [ nn.Pad2D([1, 1, 1, 1], 'reflect'), spectral_norm( nn.Conv2D(ndf * mult, ndf * mult * 2, kernel_size=4, stride=2, bias_attr=True)), nn.LeakyReLU(0.2) ] mult = 2**(n_layers - 2 - 1) model += [ nn.Pad2D([1, 1, 1, 1], 'reflect'), spectral_norm( nn.Conv2D(ndf * mult, ndf * mult * 2, kernel_size=4, stride=1, bias_attr=True)), nn.LeakyReLU(0.2) ] # Class Activation Map mult = 2**(n_layers - 2) self.gap_fc = spectral_norm(nn.Linear(ndf * mult, 1, bias_attr=False)) self.gmp_fc = spectral_norm(nn.Linear(ndf * mult, 1, bias_attr=False)) self.conv1x1 = nn.Conv2D(ndf * mult * 2, ndf * mult, kernel_size=1, stride=1, bias_attr=True) self.leaky_relu = nn.LeakyReLU(0.2) self.pad = nn.Pad2D([1, 1, 1, 1], 'reflect') self.conv = spectral_norm( nn.Conv2D(ndf * mult, 1, kernel_size=4, stride=1, bias_attr=False)) self.model = nn.Sequential(*model)
def test_class(self): paddle.disable_static() for place in self.places: input_shape = (3, 4, 5, 6) pad = [1, 2, 2, 1] pad_int = 1 value = 100 input_data = np.random.rand(*input_shape).astype(np.float32) pad_reflection = nn.Pad2D(padding=pad, mode="reflect") pad_replication = nn.Pad2D(padding=pad, mode="replicate") pad_constant = nn.Pad2D(padding=pad, mode="constant", value=value) pad_constant_int = nn.Pad2D(padding=pad_int, mode="constant", value=value) pad_circular = nn.Pad2D(padding=pad, mode="circular") data = paddle.to_tensor(input_data) output = pad_reflection(data) np_out = self._get_numpy_out(input_data, pad, "reflect", data_format="NCHW") self.assertTrue(np.allclose(output.numpy(), np_out)) output = pad_replication(data) np_out = self._get_numpy_out(input_data, pad, "replicate", data_format="NCHW") self.assertTrue(np.allclose(output.numpy(), np_out)) output = pad_constant(data) np_out = self._get_numpy_out(input_data, pad, "constant", value=value, data_format="NCHW") self.assertTrue(np.allclose(output.numpy(), np_out)) output = pad_constant_int(data) np_out = self._get_numpy_out(input_data, [pad_int] * 4, "constant", value=value, data_format="NCHW") self.assertTrue(np.allclose(output.numpy(), np_out)) output = pad_circular(data) np_out = self._get_numpy_out(input_data, pad, "circular", data_format="NCHW") self.assertTrue(np.allclose(output.numpy(), np_out))
def __init__(self): super(SiameseNet, self).__init__() self.cnt = 0 self.cnn1 = nn.Sequential( nn.Pad2D(padding=[1, 1, 1, 1], mode="constant"), nn.Conv2D(1, 4, (3, 3)), nn.ReLU(), nn.BatchNorm2D(4), nn.Pad2D(padding=[1, 1, 1, 1], mode="constant"), nn.Conv2D(4, 8, (3, 3)), nn.ReLU(), nn.BatchNorm2D(8), nn.Pad2D(padding=[1, 1, 1, 1], mode="constant"), nn.Conv2D(8, 8, (3, 3)), nn.ReLU(), nn.BatchNorm2D(8)) self.fc1 = nn.Sequential(nn.Linear(8 * 100 * 100, 500), nn.ReLU(), nn.Linear(500, 500), nn.ReLU(), nn.Linear(500, 5))
def __init__(self, in_c, out_c, padding_type, norm_layer, use_dropout, use_bias): super(MobileResnetBlock, self).__init__() self.padding_type = padding_type self.use_dropout = use_dropout self.conv_block = nn.LayerList([]) p = 0 if self.padding_type == 'reflect': self.conv_block.extend([nn.Pad2D([1, 1, 1, 1], mode='reflect')]) elif self.padding_type == 'replicate': self.conv_block.extend([nn.Pad2D([1, 1, 1, 1], mode='replicate')]) elif self.padding_type == 'zero': p = 1 else: raise NotImplementedError('padding [%s] is not implemented' % self.padding_type) self.conv_block.extend([ SeparableConv2D(num_channels=in_c, num_filters=out_c, filter_size=3, padding=p, stride=1), norm_layer(out_c), nn.ReLU() ]) self.conv_block.extend([nn.Dropout(0.5)]) if self.padding_type == 'reflect': self.conv_block.extend([nn.ReflectionPad2d([1, 1, 1, 1])]) elif self.padding_type == 'replicate': self.conv_block.extend([nn.ReplicationPad2d([1, 1, 1, 1])]) elif self.padding_type == 'zero': p = 1 else: raise NotImplementedError('padding [%s] is not implemented' % self.padding_type) self.conv_block.extend([ SeparableConv2D(num_channels=out_c, num_filters=in_c, filter_size=3, padding=p, stride=1), norm_layer(in_c) ])
def update_model_kwargs_for_generation(outputs, model_kwargs): """ Update the model inputs during generation. Note that If `token_type_ids` and `attention_mask` in `model_kwargs` and they contain pad value, the result vectors updated by this method may be different from expected. In this case, you need to rewrite the method. """ # update cache if isinstance(outputs, tuple): model_kwargs["cache"] = outputs[1] # update token_type_ids with last value if "token_type_ids" in model_kwargs: token_type_ids = model_kwargs["token_type_ids"] model_kwargs["token_type_ids"] = paddle.concat( [token_type_ids, token_type_ids[:, -1].unsqueeze(-1)], axis=-1) # update position_ids if "position_ids" in model_kwargs: position_ids = model_kwargs["position_ids"] model_kwargs["position_ids"] = paddle.concat([ position_ids, paddle.max(position_ids, axis=-1, keepdim=True) + 1 ], axis=-1) # update attention_mask if "attention_mask" in model_kwargs: attention_mask = model_kwargs["attention_mask"] # nn.Pad2D don't support the data type `bool` if convert_dtype(attention_mask.dtype) == 'bool': attention_mask = paddle.cast(attention_mask, 'int64') attention_mask = nn.Pad2D([0, 0, 0, 1], mode='replicate')(attention_mask) attention_mask = nn.Pad2D([0, 1, 0, 0], value=-1e9)(attention_mask) dtype = convert_dtype(attention_mask.dtype) if 'int' in dtype: attention_mask[:, :, -1, -1] = 1 elif 'float' in dtype: attention_mask[:, :, -1, -1] = 0.0 else: raise ValueError( 'The data type of input `attention_mask` must ' 'be bool, int or float') model_kwargs["attention_mask"] = attention_mask return model_kwargs
def __init__(self, in_channels, out_channels, kernel_size, image_size=None, **kwargs): if 'stride' in kwargs and isinstance(kwargs['stride'], list): kwargs['stride'] = kwargs['stride'][0] super().__init__(in_channels, out_channels, kernel_size, **kwargs) self.stride = self._stride if len( self._stride) == 2 else [self._stride[0]] * 2 # Calculate padding based on image size and save it assert image_size is not None ih, iw = image_size if type(image_size) == list else [ image_size, image_size ] kh, kw = self.weight.shape[-2:] sh, sw = self.stride oh, ow = math.ceil(ih / sh), math.ceil(iw / sw) pad_h = max( (oh - 1) * self.stride[0] + (kh - 1) * self._dilation[0] + 1 - ih, 0) pad_w = max( (ow - 1) * self.stride[1] + (kw - 1) * self._dilation[1] + 1 - iw, 0) if pad_h > 0 or pad_w > 0: self.static_padding = nn.Pad2D([ pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2 ]) else: self.static_padding = Identity()
def __init__(self, channels, kernel_size): """ Args: channels (int): Channel for input tensor kernel_size (int): Size of the kernel used in blurring """ super(GaussianBlurLayer, self).__init__() self.channels = channels self.kernel_size = kernel_size assert self.kernel_size % 2 != 0 self.op = nn.Sequential( nn.Pad2D(int(self.kernel_size / 2), mode='reflect'), nn.Conv2D( channels, channels, self.kernel_size, stride=1, padding=0, bias_attr=False, groups=channels)) self._init_kernel() self.op[1].weight.stop_gradient = True
def __convblock(dim_in, dim_out): return nn.Sequential( nn.InstanceNorm2D(dim_in, weight_attr=False, bias_attr=False), nn.ReLU(), nn.Pad2D([1, 1, 1, 1], 'reflect'), nn.Conv2D(dim_in, dim_out, kernel_size=3, stride=1, bias_attr=False) )
def __init__(self, dim, use_bias=False): super(ResnetBlock, self).__init__() conv_block = [] conv_block += [ nn.Pad2D([1, 1, 1, 1], 'reflect'), nn.Conv2D(dim, dim, kernel_size=3, stride=1, bias_attr=use_bias), nn.InstanceNorm2D(dim, weight_attr=False, bias_attr=False), nn.ReLU() ] conv_block += [ nn.Pad2D([1, 1, 1, 1], 'reflect'), nn.Conv2D(dim, dim, kernel_size=3, stride=1, bias_attr=use_bias), nn.InstanceNorm2D(dim, weight_attr=False, bias_attr=False) ] self.conv_block = nn.Sequential(*conv_block)
def build_conv_block(self, dim, padding_type, norm_layer, use_dropout, use_bias): """Construct a convolutional block. Parameters: dim (int) -- the number of channels in the conv layer. padding_type (str) -- the name of padding layer: reflect | replicate | zero norm_layer -- normalization layer use_dropout (bool) -- if use dropout layers. use_bias (bool) -- if the conv layer uses bias or not Returns a conv block (with a conv layer, a normalization layer, and a non-linearity layer (ReLU)) """ conv_block = [] p = 0 if padding_type in ['reflect', 'replicate']: conv_block += [nn.Pad2D(padding=[1, 1, 1, 1], mode=padding_type)] elif padding_type == 'zero': p = 1 else: raise NotImplementedError('padding [%s] is not implemented' % padding_type) conv_block += [ nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias_attr=use_bias), norm_layer(dim), nn.ReLU() ] if use_dropout: conv_block += [nn.Dropout(0.5)] p = 0 if padding_type in ['reflect', 'replicate']: conv_block += [nn.Pad2D(padding=[1, 1, 1, 1], mode=padding_type)] elif padding_type == 'zero': p = 1 else: raise NotImplementedError('padding [%s] is not implemented' % padding_type) conv_block += [ nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias_attr=use_bias), norm_layer(dim) ] return nn.Sequential(*conv_block)
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int, upsample=None): super(UpsampleConvLayer, self).__init__() self.upsample = upsample if upsample: self.upsample_layer = nn.Upsample(scale_factor=upsample) self.pad = int(np.floor(kernel_size / 2)) if self.pad != 0: self.reflection_pad = nn.Pad2D([self.pad, self.pad, self.pad, self.pad], mode='reflect') self.conv2d = nn.Conv2D(in_channels, out_channels, kernel_size, stride)
def __init__(self, dim, use_bias): super(ResnetAdaILNBlock, self).__init__() self.pad1 = nn.Pad2D(padding=[1, 1, 1, 1], mode="reflect") self.conv1 = nn.Conv2D(dim, dim, kernel_size=3, stride=1, padding=0, bias_attr=use_bias) self.norm1 = AdaILN(dim) self.relu1 = nn.ReLU() self.pad2 = nn.Pad2D(padding=[1, 1, 1, 1], mode="reflect") self.conv2 = nn.Conv2D(dim, dim, kernel_size=3, stride=1, padding=0, bias_attr=use_bias) self.norm2 = AdaILN(dim)
def __init__(self, name, in_channels, mid_channels, out_channels, use_bias): super(MiddleNet, self).__init__() self._sn_conv1 = SNConv(name=name + "_sn_conv1", in_channels=in_channels, out_channels=mid_channels, kernel_size=1, use_bias=use_bias, norm_layer=None, act=None) self._pad2d = nn.Pad2D(padding=[1, 1, 1, 1], mode="replicate") self._sn_conv2 = SNConv(name=name + "_sn_conv2", in_channels=mid_channels, out_channels=mid_channels, kernel_size=3, use_bias=use_bias) self._sn_conv3 = SNConv(name=name + "_sn_conv3", in_channels=mid_channels, out_channels=out_channels, kernel_size=1, use_bias=use_bias)
def __init__(self, ngf=32, img_size=256, n_blocks=4, light=True): super(ResnetGenerator, self).__init__() self.light = light self.n_blocks = n_blocks DownBlock = [] DownBlock += [ nn.Pad2D([3, 3, 3, 3], 'reflect'), nn.Conv2D(3, ngf, kernel_size=7, stride=1, bias_attr=False), nn.InstanceNorm2D(ngf, weight_attr=False, bias_attr=False), nn.ReLU() ] DownBlock += [HourGlass(ngf, ngf), HourGlass(ngf, ngf)] # Down-Sampling n_downsampling = 2 for i in range(n_downsampling): mult = 2**i DownBlock += [ nn.Pad2D([1, 1, 1, 1], 'reflect'), nn.Conv2D(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, bias_attr=False), nn.InstanceNorm2D(ngf * mult * 2, weight_attr=False, bias_attr=False), nn.ReLU() ] # Encoder Bottleneck mult = 2**n_downsampling for i in range(n_blocks): setattr(self, 'EncodeBlock' + str(i + 1), ResnetBlock(ngf * mult)) # Class Activation Map self.gap_fc = nn.Linear(ngf * mult, 1, bias_attr=False) self.gmp_fc = nn.Linear(ngf * mult, 1, bias_attr=False) self.conv1x1 = nn.Conv2D(ngf * mult * 2, ngf * mult, kernel_size=1, stride=1) self.relu = nn.ReLU() # Gamma, Beta block FC = [] if self.light: FC += [ nn.Linear(ngf * mult, ngf * mult, bias_attr=False), nn.ReLU(), nn.Linear(ngf * mult, ngf * mult, bias_attr=False), nn.ReLU() ] else: FC += [ nn.Linear(img_size // mult * img_size // mult * ngf * mult, ngf * mult, bias_attr=False), nn.ReLU(), nn.Linear(ngf * mult, ngf * mult, bias_attr=False), nn.ReLU() ] # Decoder Bottleneck mult = 2**n_downsampling for i in range(n_blocks): setattr(self, 'DecodeBlock' + str(i + 1), ResnetSoftAdaLINBlock(ngf * mult)) # Up-Sampling UpBlock = [] for i in range(n_downsampling): mult = 2**(n_downsampling - i) UpBlock += [ nn.Upsample(scale_factor=2), nn.Pad2D([1, 1, 1, 1], 'reflect'), nn.Conv2D(ngf * mult, ngf * mult // 2, kernel_size=3, stride=1, bias_attr=False), LIN(ngf * mult // 2), nn.ReLU() ] UpBlock += [HourGlass(ngf, ngf), HourGlass(ngf, ngf, False)] UpBlock += [ nn.Pad2D([3, 3, 3, 3], 'reflect'), nn.Conv2D(3, 3, kernel_size=7, stride=1, bias_attr=False), nn.Tanh() ] self.DownBlock = nn.Sequential(*DownBlock) self.FC = nn.Sequential(*FC) self.UpBlock = nn.Sequential(*UpBlock)
def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int): super(ConvLayer, self).__init__() pad = int(np.floor(kernel_size / 2)) self.reflection_pad = nn.Pad2D([pad, pad, pad, pad], mode='reflect') self.conv2d = nn.Conv2D(in_channels, out_channels, kernel_size, stride)
def __init__(self, input_nc, output_nc, ngf=64, norm_type='instance', use_dropout=False, n_blocks=6, padding_type='reflect'): """Construct a Resnet-based generator Args: input_nc (int) -- the number of channels in input images output_nc (int) -- the number of channels in output images ngf (int) -- the number of filters in the last conv layer norm_layer -- normalization layer use_dropout (bool) -- if use dropout layers n_blocks (int) -- the number of ResNet blocks padding_type (str) -- the name of padding layer in conv layers: reflect | replicate | zero """ assert (n_blocks >= 0) super(ResnetGenerator, self).__init__() norm_layer = build_norm_layer(norm_type) if type(norm_layer) == functools.partial: use_bias = norm_layer.func == nn.InstanceNorm2d else: use_bias = norm_layer == nn.InstanceNorm2d model = [ nn.Pad2D(padding=[3, 3, 3, 3], mode="reflect"), nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0, bias_attr=use_bias), norm_layer(ngf), nn.ReLU() ] n_downsampling = 2 for i in range(n_downsampling): # add downsampling layers mult = 2**i model += [ nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, padding=1, bias_attr=use_bias), norm_layer(ngf * mult * 2), nn.ReLU() ] mult = 2**n_downsampling for i in range(n_blocks): # add ResNet blocks model += [ ResnetBlock(ngf * mult, padding_type=padding_type, norm_layer=norm_layer, use_dropout=use_dropout, use_bias=use_bias) ] for i in range(n_downsampling): # add upsampling layers mult = 2**(n_downsampling - i) model += [ nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2), kernel_size=3, stride=2, padding=1, output_padding=1, bias_attr=use_bias), norm_layer(int(ngf * mult / 2)), nn.ReLU() ] model += [nn.Pad2D(padding=[3, 3, 3, 3], mode="reflect")] model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)] model += [nn.Tanh()] self.model = nn.Sequential(*model)
def __init__(self, input_nc, output_nc, ngf=64, n_blocks=6, img_size=256, light=False, norm_type='instance'): assert (n_blocks >= 0) super(ResnetUGATITGenerator, self).__init__() self.input_nc = input_nc self.output_nc = output_nc self.ngf = ngf self.n_blocks = n_blocks self.img_size = img_size self.light = light norm_layer = build_norm_layer(norm_type) DownBlock = [] DownBlock += [ nn.Pad2D(padding=[3, 3, 3, 3], mode="reflect"), nn.Conv2D(input_nc, ngf, kernel_size=7, stride=1, padding=0, bias_attr=False), norm_layer(ngf), nn.ReLU() ] # Down-Sampling n_downsampling = 2 for i in range(n_downsampling): mult = 2**i DownBlock += [ nn.Pad2D(padding=[1, 1, 1, 1], mode="reflect"), nn.Conv2D(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, padding=0, bias_attr=False), norm_layer(ngf * mult * 2), nn.ReLU() ] # Down-Sampling Bottleneck mult = 2**n_downsampling for i in range(n_blocks): DownBlock += [ ResnetBlock(ngf * mult, use_bias=False, norm_layer=norm_layer) ] # Class Activation Map self.gap_fc = nn.Linear(ngf * mult, 1, bias_attr=False) self.gmp_fc = nn.Linear(ngf * mult, 1, bias_attr=False) self.conv1x1 = nn.Conv2D(ngf * mult * 2, ngf * mult, kernel_size=1, stride=1, bias_attr=True) self.relu = nn.ReLU() # Gamma, Beta block if self.light: FC = [ nn.Linear(ngf * mult, ngf * mult, bias_attr=False), nn.ReLU(), nn.Linear(ngf * mult, ngf * mult, bias_attr=False), nn.ReLU() ] else: FC = [ nn.Linear(img_size // mult * img_size // mult * ngf * mult, ngf * mult, bias_attr=False), nn.ReLU(), nn.Linear(ngf * mult, ngf * mult, bias_attr=False), nn.ReLU() ] self.gamma = nn.Linear(ngf * mult, ngf * mult, bias_attr=False) self.beta = nn.Linear(ngf * mult, ngf * mult, bias_attr=False) # Up-Sampling Bottleneck for i in range(n_blocks): setattr(self, 'UpBlock1_' + str(i + 1), ResnetAdaILNBlock(ngf * mult, use_bias=False)) # Up-Sampling UpBlock2 = [] for i in range(n_downsampling): mult = 2**(n_downsampling - i) UpBlock2 += [ nn.Upsample(scale_factor=2, mode='nearest'), nn.Pad2D(padding=[1, 1, 1, 1], mode="reflect"), nn.Conv2D(ngf * mult, int(ngf * mult / 2), kernel_size=3, stride=1, padding=0, bias_attr=False), ILN(int(ngf * mult / 2)), nn.ReLU() ] UpBlock2 += [ nn.Pad2D(padding=[3, 3, 3, 3], mode="reflect"), nn.Conv2D(ngf, output_nc, kernel_size=7, stride=1, padding=0, bias_attr=False), nn.Tanh() ] self.DownBlock = nn.Sequential(*DownBlock) self.FC = nn.Sequential(*FC) self.UpBlock2 = nn.Sequential(*UpBlock2)
def __init__(self, use_norm=True, num_class=2, 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_filters=128, num_anchor_per_loc=2, encode_background_as_zeros=True, use_direction_classifier=True, use_groupnorm=False, num_groups=32, use_bev=False, box_code_size=7, name='rpn'): super(RPN, self).__init__() self._num_anchor_per_loc = num_anchor_per_loc self._use_direction_classifier = use_direction_classifier self._use_bev = use_bev assert len(layer_nums) == 3 assert len(layer_strides) == len(layer_nums) assert len(num_filters) == len(layer_nums) assert len(upsample_strides) == len(layer_nums) assert len(num_upsample_filters) == len(layer_nums) factors = [] for i in range(len(layer_nums)): assert int(np.prod( layer_strides[:i + 1])) % upsample_strides[i] == 0 factors.append( np.prod(layer_strides[:i + 1]) // upsample_strides[i]) assert all([x == factors[0] for x in factors]) if use_norm: if use_groupnorm: BatchNorm2d = change_default_args(num_groups=num_groups, epsilon=1e-3)(GroupNorm) else: BatchNorm2d = change_default_args(epsilon=1e-3, momentum=0.01)( nn.BatchNorm2D) Conv2d = change_default_args(bias_attr=False)(nn.Conv2D) ConvTranspose2d = change_default_args(bias_attr=False)( nn.Conv2DTranspose) else: BatchNorm2d = Empty Conv2d = change_default_args(bias_attr=True)(nn.Conv2D) ConvTranspose2d = change_default_args(bias_attr=True)( nn.Conv2DTranspose) # note that when stride > 1, conv2d with same padding isn't # equal to pad-conv2d. we should use pad-conv2d. block2_input_filters = num_filters[0] if use_bev: self.bev_extractor = nn.Sequential( Conv2d(6, 32, 3, padding=1), BatchNorm2d(32), nn.ReLU(), # nn.MaxPool2d(2, 2), Conv2d(32, 64, 3, padding=1), BatchNorm2d(64), nn.ReLU(), nn.MaxPool2D(2, 2), ) block2_input_filters += 64 block1_layer = [ nn.Pad2D(1), Conv2d(num_input_filters, num_filters[0], 3, stride=layer_strides[0]), BatchNorm2d(num_filters[0]), nn.ReLU(), ] for i in range(layer_nums[0]): block1_layer.append( Conv2d(num_filters[0], num_filters[0], 3, padding=1)) block1_layer.append(BatchNorm2d(num_filters[0])) block1_layer.append(nn.ReLU()) self.block1 = nn.Sequential(*block1_layer) self.deconv1 = nn.Sequential( ConvTranspose2d(num_filters[0], num_upsample_filters[0], upsample_strides[0], stride=upsample_strides[0]), BatchNorm2d(num_upsample_filters[0]), nn.ReLU(), ) block2_layer = [ nn.Pad2D(1), Conv2d(block2_input_filters, num_filters[1], 3, stride=layer_strides[1]), BatchNorm2d(num_filters[1]), nn.ReLU() ] for i in range(layer_nums[1]): block2_layer.append( Conv2d(num_filters[1], num_filters[1], 3, padding=1)) block2_layer.append(BatchNorm2d(num_filters[1])) block2_layer.append(nn.ReLU()) self.block2 = nn.Sequential(*block2_layer) self.deconv2 = nn.Sequential( ConvTranspose2d(num_filters[1], num_upsample_filters[1], upsample_strides[1], stride=upsample_strides[1]), BatchNorm2d(num_upsample_filters[1]), nn.ReLU(), ) block3_layer = [ nn.Pad2D(1), Conv2d(num_filters[1], num_filters[2], 3, stride=layer_strides[2]), BatchNorm2d(num_filters[2]), nn.ReLU(), ] for i in range(layer_nums[2]): block3_layer.append( Conv2d(num_filters[2], num_filters[2], 3, padding=1)) block3_layer.append(BatchNorm2d(num_filters[2])) block3_layer.append(nn.ReLU()) self.block3 = nn.Sequential(*block3_layer) self.deconv3 = nn.Sequential( ConvTranspose2d(num_filters[2], num_upsample_filters[2], upsample_strides[2], stride=upsample_strides[2]), BatchNorm2d(num_upsample_filters[2]), nn.ReLU(), ) if encode_background_as_zeros: num_cls = num_anchor_per_loc * num_class else: num_cls = num_anchor_per_loc * (num_class + 1) self.conv_cls = nn.Conv2D(sum(num_upsample_filters), num_cls, 1) self.conv_box = nn.Conv2D(sum(num_upsample_filters), num_anchor_per_loc * box_code_size, 1) if use_direction_classifier: self.conv_dir_cls = nn.Conv2D(sum(num_upsample_filters), num_anchor_per_loc * 2, 1)
def func_test_layer_str(self): module = nn.ELU(0.2) self.assertEqual(str(module), 'ELU(alpha=0.2)') module = nn.CELU(0.2) self.assertEqual(str(module), 'CELU(alpha=0.2)') module = nn.GELU(True) self.assertEqual(str(module), 'GELU(approximate=True)') module = nn.Hardshrink() self.assertEqual(str(module), 'Hardshrink(threshold=0.5)') module = nn.Hardswish(name="Hardswish") self.assertEqual(str(module), 'Hardswish(name=Hardswish)') module = nn.Tanh(name="Tanh") self.assertEqual(str(module), 'Tanh(name=Tanh)') module = nn.Hardtanh(name="Hardtanh") self.assertEqual(str(module), 'Hardtanh(min=-1.0, max=1.0, name=Hardtanh)') module = nn.PReLU(1, 0.25, name="PReLU", data_format="NCHW") self.assertEqual( str(module), 'PReLU(num_parameters=1, data_format=NCHW, init=0.25, dtype=float32, name=PReLU)' ) module = nn.ReLU() self.assertEqual(str(module), 'ReLU()') module = nn.ReLU6() self.assertEqual(str(module), 'ReLU6()') module = nn.SELU() self.assertEqual( str(module), 'SELU(scale=1.0507009873554805, alpha=1.6732632423543772)') module = nn.LeakyReLU() self.assertEqual(str(module), 'LeakyReLU(negative_slope=0.01)') module = nn.Sigmoid() self.assertEqual(str(module), 'Sigmoid()') module = nn.Hardsigmoid() self.assertEqual(str(module), 'Hardsigmoid()') module = nn.Softplus() self.assertEqual(str(module), 'Softplus(beta=1, threshold=20)') module = nn.Softshrink() self.assertEqual(str(module), 'Softshrink(threshold=0.5)') module = nn.Softsign() self.assertEqual(str(module), 'Softsign()') module = nn.Swish() self.assertEqual(str(module), 'Swish()') module = nn.Tanhshrink() self.assertEqual(str(module), 'Tanhshrink()') module = nn.ThresholdedReLU() self.assertEqual(str(module), 'ThresholdedReLU(threshold=1.0)') module = nn.LogSigmoid() self.assertEqual(str(module), 'LogSigmoid()') module = nn.Softmax() self.assertEqual(str(module), 'Softmax(axis=-1)') module = nn.LogSoftmax() self.assertEqual(str(module), 'LogSoftmax(axis=-1)') module = nn.Maxout(groups=2) self.assertEqual(str(module), 'Maxout(groups=2, axis=1)') module = nn.Linear(2, 4, name='linear') self.assertEqual( str(module), 'Linear(in_features=2, out_features=4, dtype=float32, name=linear)' ) module = nn.Upsample(size=[12, 12]) self.assertEqual( str(module), 'Upsample(size=[12, 12], mode=nearest, align_corners=False, align_mode=0, data_format=NCHW)' ) module = nn.UpsamplingNearest2D(size=[12, 12]) self.assertEqual( str(module), 'UpsamplingNearest2D(size=[12, 12], data_format=NCHW)') module = nn.UpsamplingBilinear2D(size=[12, 12]) self.assertEqual( str(module), 'UpsamplingBilinear2D(size=[12, 12], data_format=NCHW)') module = nn.Bilinear(in1_features=5, in2_features=4, out_features=1000) self.assertEqual( str(module), 'Bilinear(in1_features=5, in2_features=4, out_features=1000, dtype=float32)' ) module = nn.Dropout(p=0.5) self.assertEqual(str(module), 'Dropout(p=0.5, axis=None, mode=upscale_in_train)') module = nn.Dropout2D(p=0.5) self.assertEqual(str(module), 'Dropout2D(p=0.5, data_format=NCHW)') module = nn.Dropout3D(p=0.5) self.assertEqual(str(module), 'Dropout3D(p=0.5, data_format=NCDHW)') module = nn.AlphaDropout(p=0.5) self.assertEqual(str(module), 'AlphaDropout(p=0.5)') module = nn.Pad1D(padding=[1, 2], mode='constant') self.assertEqual( str(module), 'Pad1D(padding=[1, 2], mode=constant, value=0.0, data_format=NCL)') module = nn.Pad2D(padding=[1, 0, 1, 2], mode='constant') self.assertEqual( str(module), 'Pad2D(padding=[1, 0, 1, 2], mode=constant, value=0.0, data_format=NCHW)' ) module = nn.ZeroPad2D(padding=[1, 0, 1, 2]) self.assertEqual(str(module), 'ZeroPad2D(padding=[1, 0, 1, 2], data_format=NCHW)') module = nn.Pad3D(padding=[1, 0, 1, 2, 0, 0], mode='constant') self.assertEqual( str(module), 'Pad3D(padding=[1, 0, 1, 2, 0, 0], mode=constant, value=0.0, data_format=NCDHW)' ) module = nn.CosineSimilarity(axis=0) self.assertEqual(str(module), 'CosineSimilarity(axis=0, eps=1e-08)') module = nn.Embedding(10, 3, sparse=True) self.assertEqual(str(module), 'Embedding(10, 3, sparse=True)') module = nn.Conv1D(3, 2, 3) self.assertEqual(str(module), 'Conv1D(3, 2, kernel_size=[3], data_format=NCL)') module = nn.Conv1DTranspose(2, 1, 2) self.assertEqual( str(module), 'Conv1DTranspose(2, 1, kernel_size=[2], data_format=NCL)') module = nn.Conv2D(4, 6, (3, 3)) self.assertEqual(str(module), 'Conv2D(4, 6, kernel_size=[3, 3], data_format=NCHW)') module = nn.Conv2DTranspose(4, 6, (3, 3)) self.assertEqual( str(module), 'Conv2DTranspose(4, 6, kernel_size=[3, 3], data_format=NCHW)') module = nn.Conv3D(4, 6, (3, 3, 3)) self.assertEqual( str(module), 'Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)') module = nn.Conv3DTranspose(4, 6, (3, 3, 3)) self.assertEqual( str(module), 'Conv3DTranspose(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)') module = nn.PairwiseDistance() self.assertEqual(str(module), 'PairwiseDistance(p=2.0)') module = nn.InstanceNorm1D(2) self.assertEqual(str(module), 'InstanceNorm1D(num_features=2, epsilon=1e-05)') module = nn.InstanceNorm2D(2) self.assertEqual(str(module), 'InstanceNorm2D(num_features=2, epsilon=1e-05)') module = nn.InstanceNorm3D(2) self.assertEqual(str(module), 'InstanceNorm3D(num_features=2, epsilon=1e-05)') module = nn.GroupNorm(num_channels=6, num_groups=6) self.assertEqual( str(module), 'GroupNorm(num_groups=6, num_channels=6, epsilon=1e-05)') module = nn.LayerNorm([2, 2, 3]) self.assertEqual( str(module), 'LayerNorm(normalized_shape=[2, 2, 3], epsilon=1e-05)') module = nn.BatchNorm1D(1) self.assertEqual( str(module), 'BatchNorm1D(num_features=1, momentum=0.9, epsilon=1e-05, data_format=NCL)' ) module = nn.BatchNorm2D(1) self.assertEqual( str(module), 'BatchNorm2D(num_features=1, momentum=0.9, epsilon=1e-05)') module = nn.BatchNorm3D(1) self.assertEqual( str(module), 'BatchNorm3D(num_features=1, momentum=0.9, epsilon=1e-05, data_format=NCDHW)' ) module = nn.SyncBatchNorm(2) self.assertEqual( str(module), 'SyncBatchNorm(num_features=2, momentum=0.9, epsilon=1e-05)') module = nn.LocalResponseNorm(size=5) self.assertEqual( str(module), 'LocalResponseNorm(size=5, alpha=0.0001, beta=0.75, k=1.0)') module = nn.AvgPool1D(kernel_size=2, stride=2, padding=0) self.assertEqual(str(module), 'AvgPool1D(kernel_size=2, stride=2, padding=0)') module = nn.AvgPool2D(kernel_size=2, stride=2, padding=0) self.assertEqual(str(module), 'AvgPool2D(kernel_size=2, stride=2, padding=0)') module = nn.AvgPool3D(kernel_size=2, stride=2, padding=0) self.assertEqual(str(module), 'AvgPool3D(kernel_size=2, stride=2, padding=0)') module = nn.MaxPool1D(kernel_size=2, stride=2, padding=0) self.assertEqual(str(module), 'MaxPool1D(kernel_size=2, stride=2, padding=0)') module = nn.MaxPool2D(kernel_size=2, stride=2, padding=0) self.assertEqual(str(module), 'MaxPool2D(kernel_size=2, stride=2, padding=0)') module = nn.MaxPool3D(kernel_size=2, stride=2, padding=0) self.assertEqual(str(module), 'MaxPool3D(kernel_size=2, stride=2, padding=0)') module = nn.AdaptiveAvgPool1D(output_size=16) self.assertEqual(str(module), 'AdaptiveAvgPool1D(output_size=16)') module = nn.AdaptiveAvgPool2D(output_size=3) self.assertEqual(str(module), 'AdaptiveAvgPool2D(output_size=3)') module = nn.AdaptiveAvgPool3D(output_size=3) self.assertEqual(str(module), 'AdaptiveAvgPool3D(output_size=3)') module = nn.AdaptiveMaxPool1D(output_size=16, return_mask=True) self.assertEqual( str(module), 'AdaptiveMaxPool1D(output_size=16, return_mask=True)') module = nn.AdaptiveMaxPool2D(output_size=3, return_mask=True) self.assertEqual(str(module), 'AdaptiveMaxPool2D(output_size=3, return_mask=True)') module = nn.AdaptiveMaxPool3D(output_size=3, return_mask=True) self.assertEqual(str(module), 'AdaptiveMaxPool3D(output_size=3, return_mask=True)') module = nn.SimpleRNNCell(16, 32) self.assertEqual(str(module), 'SimpleRNNCell(16, 32)') module = nn.LSTMCell(16, 32) self.assertEqual(str(module), 'LSTMCell(16, 32)') module = nn.GRUCell(16, 32) self.assertEqual(str(module), 'GRUCell(16, 32)') module = nn.PixelShuffle(3) self.assertEqual(str(module), 'PixelShuffle(upscale_factor=3)') module = nn.SimpleRNN(16, 32, 2) self.assertEqual( str(module), 'SimpleRNN(16, 32, num_layers=2\n (0): RNN(\n (cell): SimpleRNNCell(16, 32)\n )\n (1): RNN(\n (cell): SimpleRNNCell(32, 32)\n )\n)' ) module = nn.LSTM(16, 32, 2) self.assertEqual( str(module), 'LSTM(16, 32, num_layers=2\n (0): RNN(\n (cell): LSTMCell(16, 32)\n )\n (1): RNN(\n (cell): LSTMCell(32, 32)\n )\n)' ) module = nn.GRU(16, 32, 2) self.assertEqual( str(module), 'GRU(16, 32, num_layers=2\n (0): RNN(\n (cell): GRUCell(16, 32)\n )\n (1): RNN(\n (cell): GRUCell(32, 32)\n )\n)' ) module1 = nn.Sequential( ('conv1', nn.Conv2D(1, 20, 5)), ('relu1', nn.ReLU()), ('conv2', nn.Conv2D(20, 64, 5)), ('relu2', nn.ReLU())) self.assertEqual( str(module1), 'Sequential(\n '\ '(conv1): Conv2D(1, 20, kernel_size=[5, 5], data_format=NCHW)\n '\ '(relu1): ReLU()\n '\ '(conv2): Conv2D(20, 64, kernel_size=[5, 5], data_format=NCHW)\n '\ '(relu2): ReLU()\n)' ) module2 = nn.Sequential( nn.Conv3DTranspose(4, 6, (3, 3, 3)), nn.AvgPool3D(kernel_size=2, stride=2, padding=0), nn.Tanh(name="Tanh"), module1, nn.Conv3D(4, 6, (3, 3, 3)), nn.MaxPool3D(kernel_size=2, stride=2, padding=0), nn.GELU(True)) self.assertEqual( str(module2), 'Sequential(\n '\ '(0): Conv3DTranspose(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)\n '\ '(1): AvgPool3D(kernel_size=2, stride=2, padding=0)\n '\ '(2): Tanh(name=Tanh)\n '\ '(3): Sequential(\n (conv1): Conv2D(1, 20, kernel_size=[5, 5], data_format=NCHW)\n (relu1): ReLU()\n'\ ' (conv2): Conv2D(20, 64, kernel_size=[5, 5], data_format=NCHW)\n (relu2): ReLU()\n )\n '\ '(4): Conv3D(4, 6, kernel_size=[3, 3, 3], data_format=NCDHW)\n '\ '(5): MaxPool3D(kernel_size=2, stride=2, padding=0)\n '\ '(6): GELU(approximate=True)\n)' )