def __init__(self, inp_dim, hidden_dim, num_layers, batch_norm=False, dropout=0.): super(MLP, self).__init__() layer_list = OrderedDict() in_dim = inp_dim for l in range(num_layers): layer_list['fc{}'.format(l)] = nn.Linear(in_dim, hidden_dim) if l < num_layers - 1: if batch_norm: layer_list['norm{}'.format(l)] = nn.BatchNorm1D( num_features=hidden_dim) layer_list['relu{}'.format(l)] = nn.LeakyReLU() if dropout > 0: layer_list['drop{}'.format(l)] = nn.Dropout(p=dropout) in_dim = hidden_dim if num_layers > 0: self.network = nn.Sequential() for i in layer_list: self.network.add_sublayer(i, layer_list[i]) else: self.network = nn.Identity()
def __init__(self, num_inputs, input_size, embedding_size, width, depth, pairs, stage, use_bn=True): super().__init__() self.stage = stage self.depth = depth self.w_embeddings = nn.Embedding(input_size, 1, weight_attr=xavier_init([input_size])) self.v_embeddings = nn.Embedding(input_size, embedding_size, weight_attr=xavier_init( [input_size, embedding_size])) in_features = [num_inputs * embedding_size] + [width] * depth out_features = [width] * depth + [1] if use_bn: self.bn = nn.LayerList([nn.BatchNorm(width) for _ in range(depth)]) else: self.bn = nn.LayerList([nn.Identity() for _ in range(depth)]) self.linear = nn.LayerList([ nn.Linear(*_, weight_attr=xavier_init([_])) for _ in zip(in_features, out_features) ]) self.comb_mask = None if stage == 0 else np.load('comb_mask.npy') pairs = pairs if stage == 0 else sum(self.comb_mask) self.mask = paddle.create_parameter( [1, pairs], 'float32', default_initializer=nn.initializer.Uniform(0.6 - 0.001, 0.6 + 0.001)) self.bn2 = nn.BatchNorm(pairs) if use_bn else nn.Identity()
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups super(Conv, self).__init__() self.conv = nn.Conv2D(c1, c2, k, s, autopad(k, p), groups=g, bias_attr=False) self.bn = nn.BatchNorm2D(c2) self.act = nn.LeakyReLU(0.1) if act else nn.Identity()
def __init__(self, path=None, features=256, non_negative=True): """Init. Args: path (str, optional): Path to saved model. Defaults to None. features (int, optional): Number of features. Defaults to 256. backbone (str, optional): Backbone network for encoder. Defaults to resnet50 """ print("Loading weights: ", path) super(MidasNet, self).__init__() use_pretrained = False if path is None else True self.pretrained, self.scratch = _make_encoder( backbone="resnext101_wsl", features=features, use_pretrained=use_pretrained) self.scratch.refinenet4 = FeatureFusionBlock(features) self.scratch.refinenet3 = FeatureFusionBlock(features) self.scratch.refinenet2 = FeatureFusionBlock(features) self.scratch.refinenet1 = FeatureFusionBlock(features) output_conv = [ nn.Conv2D(features, 128, kernel_size=3, stride=1, padding=1), nn.Upsample(scale_factor=2, mode="bilinear"), nn.Conv2D(128, 32, kernel_size=3, stride=1, padding=1), nn.ReLU(), nn.Conv2D(32, 1, kernel_size=1, stride=1, padding=0), nn.ReLU() if non_negative else nn.Identity(), ] if non_negative: output_conv.append(nn.ReLU()) self.scratch.output_conv = nn.Sequential(*output_conv) if path: self.load(path)
def reset_classifier(self, num_classes, global_pool=''): self.num_classes = num_classes self.head = nn.Linear( self.embed_dim, num_classes) if num_classes > 0 else nn.Identity()
def __init__(self, img_size=224, patch_size=4, in_chans=3, class_num=1000, embed_dim=96, depths=[2, 2, 6, 2], num_heads=[3, 6, 12, 24], window_size=7, mlp_ratio=4., qkv_bias=True, qk_scale=None, drop_rate=0., attn_drop_rate=0., drop_path_rate=0.1, norm_layer=nn.LayerNorm, ape=False, patch_norm=True, use_checkpoint=False, **kwargs): super(SwinTransformer, self).__init__() self.num_classes = num_classes = class_num self.num_layers = len(depths) self.embed_dim = embed_dim self.ape = ape self.patch_norm = patch_norm self.num_features = int(embed_dim * 2**(self.num_layers - 1)) self.mlp_ratio = mlp_ratio # split image into non-overlapping patches self.patch_embed = PatchEmbed( img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim, norm_layer=norm_layer if self.patch_norm else None) num_patches = self.patch_embed.num_patches patches_resolution = self.patch_embed.patches_resolution self.patches_resolution = patches_resolution # absolute position embedding if self.ape: self.absolute_pos_embed = self.create_parameter( shape=(1, num_patches, embed_dim), default_initializer=zeros_) self.add_parameter("absolute_pos_embed", self.absolute_pos_embed) trunc_normal_(self.absolute_pos_embed) self.pos_drop = nn.Dropout(p=drop_rate) # stochastic depth dpr = np.linspace(0, drop_path_rate, sum(depths)).tolist() # stochastic depth decay rule # build layers self.layers = nn.LayerList() for i_layer in range(self.num_layers): layer = BasicLayer( dim=int(embed_dim * 2**i_layer), input_resolution=(patches_resolution[0] // (2**i_layer), patches_resolution[1] // (2**i_layer)), depth=depths[i_layer], num_heads=num_heads[i_layer], window_size=window_size, mlp_ratio=self.mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale, drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[sum(depths[:i_layer]):sum(depths[:i_layer + 1])], norm_layer=norm_layer, downsample=PatchMerging if (i_layer < self.num_layers - 1) else None, use_checkpoint=use_checkpoint) self.layers.append(layer) self.norm = norm_layer(self.num_features) self.avgpool = nn.AdaptiveAvgPool1D(1) self.head = nn.Linear( self.num_features, num_classes) if self.num_classes > 0 else nn.Identity() self.apply(self._init_weights)