def vgg_block(num_convs, channels): """ define the basic structure of vgg structure: n convolution with 3x3 kernel, a 2x2 pooling mgnn.Sequential() is a structure for building net work, see: http://zh.gluon.ai/chapter_gluon-basics/index.html :param num_convs: :param channels: :return: """ out = mgnn.Sequential() for _ in range(channels): out.add( mgnn.Conv2D(channels=channels, kernel_size=3, padding=1, activation='relu')) out.add(mgnn.MaxPool2D(pool_size=2, strides=2)) return out
def __init__(self, block, layers, channels, classes=1000, thumbnail=False, **kwargs): super(ResNetV2, self).__init__(channels, classes, **kwargs) assert len(layers) == len(channels) - 1 # self.features.add(nn.BatchNorm(scale=False, center=False)) self.features.add(nn.BatchNorm(scale=False, epsilon=2e-5)) if thumbnail: self.features.add( nn.Conv2D(channels[0], kernel_size=3, strides=1, padding=1, in_channels=0)) else: self.features.add(nn.Conv2D(channels[0], 7, 2, 3, use_bias=False)) # fix_gamma=False missing ? self.features.add(nn.BatchNorm()) self.features.add(nn.Activation('relu')) self.features.add(nn.MaxPool2D(3, 2, 1)) in_channels = channels[0] for i, num_layer in enumerate(layers): stride = 1 if i == 0 else 2 self.features.add( self._make_layer(block, num_layer, channels[i + 1], stride, i + 1, in_channels=in_channels)) in_channels = channels[i + 1] # fix_gamma=False missing ? self.features.add(nn.BatchNorm()) self.features.add(nn.Activation('relu')) self.features.add(nn.GlobalAvgPool2D()) self.features.add(nn.Flatten())
def __init__(self,num_class,verbose = False,**kwargs): super(ResNet,self).__init__(**kwargs) self.verbose = verbose with self.name_scope(): # block 1 b1 = nn.Conv2D(64,kernel_size=7,strides=2) # block 2 b2 = nn.Sequential() b2.add( nn.MaxPool2D(pool_size=3,strides=2), Residual(64), Residual(64) ) # block 3 b3 = nn.Sequential() b3.add( Residual(128,same_shape=False), Residual(128) ) # block 4 b4 = nn.Sequential() b4.add( Residual(256,same_shape=False), Residual(256) ) # block 5 b5 = nn.Sequential() b5.add( Residual(512,same_shape=False), Residual(512) ) # block 6 b6 = nn.Sequential() b6.add( nn.AvgPool2D(pool_size=3), nn.Dense(num_class)) self.net = nn.Sequential() self.net.add(b1,b2,b3,b4,b5,b6)
def __init__(self, c1, c2, c3, c4, **kwargs): super(Inception, self).__init__(**kwargs) # line one: 1 \to 1 convolution layer self.p1_1 = nn.Conv2D(c1, kernel_size=1, activation='relu') # line two: 1 \to 1 convolution layer followed by 3\ to 3 convolution layer self.p2_1 = nn.Conv2D(c2[0], kernel_size=1, activation='relu') self.p2_2 = nn.Conv2D(c2[1], kernel_size=3, padding=1, activation='relu') # line three: 1 \to 1 convolution layer followed by 5\ to 5 convolution layer self.p3_1 = nn.Conv2D(c3[0], kernel_size=1, activation='relu') self.p3_2 = nn.Conv2D(c3[1], kernel_size=5, padding=2, activation='relu') # line four: 3 \to 3 maxpooling followed by 1 \to 1 convolution layer self.p4_1 = nn.MaxPool2D(pool_size=3, strides=1, padding=1) self.p4_2 = nn.Conv2D(c4, kernel_size=1, activation='relu')
def __init__(self, channels, final_block_channels, transpose_descriptors=True, hybridizable=True, batch_size=1, in_size=(224, 224), in_channels=1, **kwargs): super(SuperPointNet, self).__init__(**kwargs) assert ((batch_size is not None) or not hybridizable) assert ((in_size is not None) or not hybridizable) self.batch_size = batch_size self.in_size = in_size with self.name_scope(): self.features = nn.HybridSequential(prefix="") for i, channels_per_stage in enumerate(channels): stage = nn.HybridSequential(prefix="stage{}_".format(i + 1)) for j, out_channels in enumerate(channels_per_stage): if (j == 0) and (i != 0): stage.add(nn.MaxPool2D(pool_size=2, strides=2)) stage.add( vgg_conv3x3(in_channels=in_channels, out_channels=out_channels, use_bias=True, use_bn=False)) in_channels = out_channels self.features.add(stage) self.detector = SPDetector(in_channels=in_channels, mid_channels=final_block_channels, hybridizable=hybridizable, batch_size=batch_size, in_size=in_size) self.descriptor = SPDescriptor( in_channels=in_channels, mid_channels=final_block_channels, transpose_descriptors=transpose_descriptors, hybridizable=hybridizable, batch_size=batch_size, in_size=in_size)
def __init__(self, n1_1, n2_1, n2_3, n3_1, n3_5, n4_1, **kwargs): super(Inception, self).__init__(**kwargs) # path 1 self.p1_conv_1 = nn.Conv2D(n1_1, kernel_size=1, activation='relu') # path 2 self.p2_conv_1 = nn.Conv2D(n2_1, kernel_size=1, activation='relu') self.p2_conv_3 = nn.Conv2D(n2_3, kernel_size=3, padding=1, activation='relu') # path 3 self.p3_conv_1 = nn.Conv2D(n3_1, kernel_size=1, activation='relu') self.p3_conv_5 = nn.Conv2D(n3_5, kernel_size=5, padding=2, activation='relu') # path 4 self.p4_pool_3 = nn.MaxPool2D(pool_size=3, padding=1, strides=1) self.p4_conv_1 = nn.Conv2D(n4_1, kernel_size=1, activation='relu')
def dense_net(): net = nn.HybridSequential() # add name_scope on the outermost Sequential with net.name_scope(): # first block net.add(nn.Conv2D(init_channels, kernel_size=7, strides=2, padding=3), nn.BatchNorm(), nn.Activation('relu'), nn.MaxPool2D(pool_size=3, strides=2, padding=1)) # dense blocks channels = init_channels for i, layers in enumerate(block_layers): net.add(DenseBlock(layers, growth_rate)) channels += layers * growth_rate if i != len(block_layers) - 1: net.add(transition_block(channels // 2)) # last block net.add(nn.BatchNorm(), nn.Activation('relu'), nn.AvgPool2D(pool_size=1), nn.Flatten(), nn.Dense(num_classes)) return net
def _make_layers(self, cfg, use_bias, use_bn): layers = [] in_channels = 3 for x in cfg: if x == 'M': layers += [nn.MaxPool2D(pool_size=2, strides=2)] else: if use_bn: layers += [ nn.Conv2D(in_channels=in_channels, channels=x, kernel_size=(3, 3), padding=(1, 1), use_bias=False), nn.BatchNorm(), nn.Activation('relu') ] elif use_bias: layers += [ nn.Conv2D(in_channels=in_channels, channels=x, kernel_size=(3, 3), padding=(1, 1), use_bias=True), nn.Activation('relu') ] else: layers += [ nn.Conv2D(in_channels=in_channels, channels=x, kernel_size=(3, 3), padding=(1, 1), use_bias=False), nn.Activation('relu') ] in_channels = x with self.name_scope(): model = nn.HybridSequential(prefix='features_') for block in layers: model.add(block) return model
def __init__(self, c1, c2, c3, c4, **kwargs): super(Inception, self).__init__(**kwargs) self.p1 = nn.Conv2D(channels=c1, kernel_size=1, activation='relu') self.p2_1 = nn.Conv2D(channels=c2[0], kernel_size=1, activation='relu') self.p2_2 = nn.Conv2D(channels=c2[1], kernel_size=3, padding=1, activation='relu') self.p3_1 = nn.Conv2D(channels=c3[0], kernel_size=1, activation='relu') self.p3_2 = nn.Conv2D(channels=c3[1], kernel_size=5, padding=2, activation='relu') self.p4_1 = nn.MaxPool2D(pool_size=3, strides=1, padding=1) self.p4_2 = nn.Conv2D(channels=c4, kernel_size=1, activation='relu')
def __init__(self, channels, size1=14, scale=(1, 2, 1), norm_layer=BatchNorm, norm_kwargs=None, **kwargs): super(AttentionModule_stage3, self).__init__(**kwargs) p, t, r = scale with self.name_scope(): self.first_residual_blocks = nn.HybridSequential() _add_block(self.first_residual_blocks, ResidualBlock, p, channels, norm_layer=norm_layer, norm_kwargs=norm_kwargs) self.trunk_branches = nn.HybridSequential() _add_block(self.trunk_branches, ResidualBlock, t, channels, norm_layer=norm_layer, norm_kwargs=norm_kwargs) self.mpool1 = nn.MaxPool2D(pool_size=3, strides=2, padding=1) self.softmax1_blocks = nn.HybridSequential() _add_block(self.softmax1_blocks, ResidualBlock, 2 * r, channels, norm_layer=norm_layer, norm_kwargs=norm_kwargs) self.interpolation1 = UpsamplingBilinear2d(size=size1) self.softmax2_blocks = nn.HybridSequential() _add_sigmoid_layer(self.softmax2_blocks, channels, norm_layer, norm_kwargs) self.last_blocks = ResidualBlock(channels)
def __init__(self, in_channels, out_channels, bn_use_global_stats, avg_pool, **kwargs): super(InceptionPoolBranch, self).__init__(**kwargs) with self.name_scope(): if avg_pool: self.pool = nn.AvgPool2D(pool_size=3, strides=1, padding=1, ceil_mode=True, count_include_pad=True) else: self.pool = nn.MaxPool2D(pool_size=3, strides=1, padding=1, ceil_mode=True) self.conv = conv1x1_block(in_channels=in_channels, out_channels=out_channels, use_bias=True, bn_use_global_stats=bn_use_global_stats)
def __init__(self, in_channels, out_channels, strides, reps, start_with_relu=True, grow_first=True, bn_use_global_stats=False, **kwargs): super(XceptionUnit, self).__init__(**kwargs) self.resize_identity = (in_channels != out_channels) or (strides != 1) with self.name_scope(): if self.resize_identity: self.identity_conv = conv1x1_block( in_channels=in_channels, out_channels=out_channels, strides=strides, bn_use_global_stats=bn_use_global_stats, activate=False) self.body = nn.HybridSequential(prefix='') for i in range(reps): if (grow_first and (i == 0)) or ((not grow_first) and (i == reps - 1)): in_channels_i = in_channels out_channels_i = out_channels else: if grow_first: in_channels_i = out_channels out_channels_i = out_channels else: in_channels_i = in_channels out_channels_i = in_channels activate = start_with_relu if (i == 0) else True self.body.add( dws_conv3x3_block(in_channels=in_channels_i, out_channels=out_channels_i, bn_use_global_stats=bn_use_global_stats, activate=activate)) if strides != 1: self.body.add( nn.MaxPool2D(pool_size=3, strides=strides, padding=1))
def _make_branch(use_pool, norm_layer, norm_kwargs, *conv_settings): out = nn.HybridSequential(prefix='') if use_pool == 'avg': out.add(nn.AvgPool2D(pool_size=3, strides=1, padding=1)) elif use_pool == 'max': out.add(nn.MaxPool2D(pool_size=3, strides=1, padding=1)) setting_names = ['in_channels', 'channels', 'kernel_size', 'strides', 'padding'] for setting in conv_settings: kwargs = {} for i, value in enumerate(setting): if value is not None: if setting_names[i] == 'in_channels': in_channels = value elif setting_names[i] == 'channels': channels = value else: kwargs[setting_names[i]] = value out.add(_make_basic_conv(in_channels, channels, channels, norm_layer, norm_kwargs, **kwargs)) return out
def __init__(self, layers, batch_size, width, classes=1000, last_gamma=False, **kwargs): super(DBTNet, self).__init__(**kwargs) channels = 64 self.gconvs = nn.HybridSequential(prefix='') with self.name_scope(): self.features = nn.HybridSequential(prefix='') self.features1 = nn.HybridSequential(prefix='') self.features.add(nn.BatchNorm(scale=False, center=False)) self.features.add(nn.Conv2D(channels, 7, 2, 3, use_bias=False)) self.features.add(nn.BatchNorm()) self.features.add(nn.Activation('relu')) self.features.add(nn.MaxPool2D(3, 2, 1)) for i, num_layer in enumerate(layers): stride = 1 if i == 0 else 2 self.features.add( self._make_layer(channels, num_layer, stride, last_gamma, False, i + 1, batch_size, width)) channels *= 2 gconv = GroupConv2(2048, batch_size=batch_size, kernel_size=1, use_bias=False, width=width // 32) self.gconvs.add(gconv) self.features.add(gconv) self.features.add( GB2HybridLayer(32, np.int(2048 / 32), width // 32, 'gb')) self.features1.add(nn.GlobalAvgPool2D()) self.features1.add(nn.Flatten()) self.myoutput = nn.HybridSequential(prefix='new') with self.myoutput.name_scope(): self.myoutput.add( nn.Conv2D(classes, kernel_size=1, padding=0, use_bias=True))
def GetNet(): net = nn.HybridSequential() with net.name_scope(): net.add(nn.Conv2D(channels=32, kernel_size=5, activation='relu')) net.add(nn.MaxPool2D(pool_size=2, strides=1)) net.add(nn.Conv2D(channels=32, kernel_size=5, activation='relu')) net.add(nn.AvgPool2D(pool_size=2, strides=1)) net.add(nn.Conv2D(channels=32, kernel_size=3, activation='relu')) net.add(nn.AvgPool2D(pool_size=2, strides=1)) net.add(nn.Conv2D(channels=32, kernel_size=3, activation='relu')) net.add(nn.AvgPool2D(pool_size=2, strides=1)) net.add(nn.Flatten()) net.add(nn.Dense(256)) net.add(Cont()) return net
def _make_features(self, layers, filters, strides, batch_norm): featurizer = nn.HybridSequential(prefix='') for i, num in enumerate(layers): for _ in range(num): featurizer.add( nn.Conv2D(filters[i], kernel_size=3, padding=1, weight_initializer=Xavier(rnd_type='gaussian', factor_type='out', magnitude=2), bias_initializer='zeros')) if batch_norm: featurizer.add( self.norm_layer(**({} if self.norm_kwargs is None else self.norm_kwargs))) featurizer.add(nn.Activation('relu')) featurizer.add(nn.MaxPool2D(strides=strides[i])) return featurizer
def __init__(self, c1, c2, c3, c4, **kwargs): super(Inception, self).__init__(**kwargs) # path1, 1x1 Conv self.p1_1 = nn.Conv2D(c1, kernel_size=1, activation='relu') # path2, 1x1 Conv & 3x3 Conv self.p2_1 = nn.Conv2D(c2[0], kernel_size=1, activation='relu') self.p2_2 = nn.Conv2D(c2[1], kernel_size=3, padding=1, activation='relu') # path3, 1x1 Conv & 5x5 Conv self.p3_1 = nn.Conv2D(c3[0], kernel_size=1, activation='relu') self.p3_2 = nn.Conv2D(c3[1], kernel_size=5, padding=2, activation='relu') # path4, 3x3 Max_pooling & 1x1 Conv self.p4_1 = nn.MaxPool2D(pool_size=3, strides=1, padding=1) self.p4_2 = nn.Conv2D(c4, kernel_size=1, activation='relu')
def __init__(self, in_channels, out_channels, **kwargs): super(ShuffleInitBlock, self).__init__(**kwargs) with self.name_scope(): self.conv = nn.Conv2D( channels=out_channels, kernel_size=3, strides=2, padding=1, use_bias=False, in_channels=in_channels) self.bn = nn.BatchNorm(in_channels=out_channels) self.activ = nn.Activation('relu') self.pool = nn.MaxPool2D( pool_size=3, strides=2, padding=1)
def __init__(self, c1, c2, c3, c4, **kwargs): super(Inception, self).__init__(**kwargs) # 线路 1,单 1 x 1 卷积层。 self.p1_1 = nn.Conv2D(c1, kernel_size=1, activation='relu') # 线路 2,1 x 1 卷积层后接 3 x 3 卷积层。 self.p2_1 = nn.Conv2D(c2[0], kernel_size=1, activation='relu') self.p2_2 = nn.Conv2D(c2[1], kernel_size=3, padding=1, activation='relu') # 线路 3,1 x 1 卷积层后接 5 x 5 卷积层。 self.p3_1 = nn.Conv2D(c3[0], kernel_size=1, activation='relu') self.p3_2 = nn.Conv2D(c3[1], kernel_size=5, padding=2, activation='relu') # 线路 4,3 x 3 最⼤池化层后接 1 x 1 卷积层。 self.p4_1 = nn.MaxPool2D(pool_size=3, strides=1, padding=1) self.p4_2 = nn.Conv2D(c4, kernel_size=1, activation='relu')
def __init__(self, init_channels, growth_rate, num_layers_list, strides_list, bn_size=4, dropout=0, num_classes=1000, use_backbone=True, **kwargs): super(DenseNet, self).__init__(**kwargs) with self.name_scope(): self.first_layer = nn.HybridSequential() self.features = nn.HybridSequential() self.first_layer.add( nn.Conv2D(init_channels, kernel_size=7, strides=2, padding=3, use_bias=False)) self.first_layer.add(nn.BatchNorm()) self.first_layer.add(nn.Activation('relu')) self.first_pool = nn.MaxPool2D((2, 2), (2, 2)) num_channels = init_channels for i, num_layers in enumerate(num_layers_list): self.features.add( DenseBlock(num_layers, bn_size, growth_rate, dropout)) num_channels = num_channels + num_layers * growth_rate if i != len(num_layers_list) - 1: num_channels = num_channels // 2 self.features.add(Transition(num_channels, strides_list[i])) if not use_backbone: self.output = nn.HybridSequential() self.output.add(nn.BatchNorm()) self.output.add(nn.Activation('relu')) self.output.add(nn.GlobalAvgPool2D()) self.output.add(nn.Dense(num_classes)) else: self.output = None
def __init__(self, backbone, heads, head_conv_channel=0, scale=4.0, topk=100, in_channels=3, in_size=(512, 512), **kwargs): super(CenterNet, self).__init__(**kwargs) self.in_size = in_size self.in_channels = in_channels with self.name_scope(): self.backbone = backbone self.heatmap_nms = nn.MaxPool2D( pool_size=3, strides=1, padding=1) self.decoder = CenterNetDecoder( topk=topk, scale=scale) self.heads = nn.HybridSequential(prefix="heads") for name, values in heads.items(): head = nn.HybridSequential(name) num_output = values['num_output'] if head_conv_channel > 0: head.add(nn.Conv2D( head_conv_channel, kernel_size=3, padding=1, use_bias=True)) head.add(nn.Activation('relu')) head.add(nn.Conv2D( num_output, kernel_size=1, strides=1, padding=0, use_bias=True)) self.heads.add(head)
def __init__(self, num_init_features, growth_rate, block_config, bn_size=4, dropout=0, classes=1000, norm_layer=BatchNorm, norm_kwargs=None, **kwargs): super(DenseNet, self).__init__(**kwargs) with self.name_scope(): self.features = nn.HybridSequential(prefix='') self.features.add( nn.Conv2D(num_init_features, kernel_size=7, strides=2, padding=3, use_bias=False)) self.features.add( norm_layer(**({} if norm_kwargs is None else norm_kwargs))) self.features.add(nn.Activation('relu')) self.features.add(nn.MaxPool2D(pool_size=3, strides=2, padding=1)) # Add dense blocks num_features = num_init_features for i, num_layers in enumerate(block_config): self.features.add( _make_dense_block(num_layers, bn_size, growth_rate, dropout, i + 1, norm_layer, norm_kwargs)) num_features = num_features + num_layers * growth_rate if i != len(block_config) - 1: self.features.add( _make_transition(num_features // 2, norm_layer, norm_kwargs)) num_features = num_features // 2 self.features.add( norm_layer(**({} if norm_kwargs is None else norm_kwargs))) self.features.add(nn.Activation('relu')) self.features.add(nn.AvgPool2D(pool_size=7)) self.features.add(nn.Flatten()) self.output = nn.Dense(classes)
def __init__(self, num_of_pts=98, **kwargs): super(BASE, self).__init__(**kwargs) self.pts_num = num_of_pts self.lmks_net = mx.gluon.nn.HybridSequential() self.lmks_net.add( nn.Conv2D(channels=16, kernel_size=(3, 3), strides=(2, 2), padding=(1, 1)), nn.BatchNorm(), nn.Activation('relu'), nn.Conv2D(channels=32, kernel_size=(3, 3), strides=(1, 1), padding=(1, 1)), nn.BatchNorm(), nn.Activation('relu'), nn.Conv2D(channels=32, kernel_size=(3, 3), strides=(2, 2), padding=(1, 1)), nn.BatchNorm(), nn.Activation('relu'), nn.Conv2D(channels=32, kernel_size=(3, 3), strides=(1, 1), padding=(1, 1)), nn.BatchNorm(), nn.Activation('relu'), nn.Conv2D(channels=32, kernel_size=(3, 3), strides=(2, 2), padding=(1, 1)), nn.BatchNorm(), nn.Activation('relu'), nn.Conv2D(channels=64, kernel_size=(3, 3), strides=(1, 1), padding=(1, 1)), nn.BatchNorm(), nn.Activation('relu'), nn.Conv2D(channels=64, kernel_size=(3, 3), strides=(2, 2), padding=(1, 1)), nn.BatchNorm(), nn.Activation('relu'), nn.Conv2D(channels=128, kernel_size=(3, 3), strides=(1, 1), padding=(1, 1)), nn.BatchNorm(), nn.Activation('relu'), nn.MaxPool2D(pool_size=(2, 2), strides=(2, 2)), nn.Conv2D(channels=num_of_pts * 2, kernel_size=(3, 3), strides=(1, 1), padding=(0, 0)))
def __init__(self, block, layers, channels, classes=1000, thumbnail=False, last_gamma=False, use_se=False, **kwargs): super(ResNetV2, self).__init__(**kwargs) assert len(layers) == len(channels) - 1 with self.name_scope(): self.features = nn.HybridSequential(prefix='') self.features.add(nn.BatchNorm(scale=False, center=False)) if thumbnail: self.features.add(_conv3x3(channels[0], 1, 0)) else: self.features.add( nn.Conv2D(channels[0], 7, 2, 3, use_bias=False)) self.features.add(nn.BatchNorm()) self.features.add(nn.Activation('relu')) self.features.add(nn.MaxPool2D(3, 2, 1)) in_channels = channels[0] for i, num_layer in enumerate(layers): stride = 1 if i == 0 else 2 self.features.add( self._make_layer(block, num_layer, channels[i + 1], stride, i + 1, in_channels=in_channels, last_gamma=last_gamma, use_se=use_se)) in_channels = channels[i + 1] self.features.add(nn.BatchNorm()) self.features.add(nn.Activation('relu')) self.features.add(nn.GlobalAvgPool2D()) self.features.add(nn.Flatten()) self.output = nn.Dense(classes, in_units=in_channels)
def __init__(self, **kwargs): super(ResidualAttentionModel_92, self).__init__(**kwargs) """ input size 224 """ with self.name_scope(): self.conv1 = nn.HybridSequential() with self.conv1.name_scope(): self.conv1.add( nn.Conv2D(64, kernel_size=7, strides=2, padding=3, use_bias=False)) self.conv1.add(nn.BatchNorm()) self.conv1.add(nn.Activation('relu')) self.mpool1 = nn.MaxPool2D(pool_size=3, strides=2, padding=1) self.residual_block1 = ResidualBlock(256, in_channels=64) self.attention_module1 = AttentionModule_stage1(256) self.residual_block2 = ResidualBlock(512, in_channels=256, stride=2) self.attention_module2 = AttentionModule_stage2(512) self.attention_module2_2 = AttentionModule_stage2(512) self.residual_block3 = ResidualBlock(1024, in_channels=512, stride=2) self.attention_module3 = AttentionModule_stage3(1024) self.attention_module3_2 = AttentionModule_stage3(1024) self.attention_module3_3 = AttentionModule_stage3(1024) self.residual_block4 = ResidualBlock(2048, in_channels=1024, stride=2) self.residual_block5 = ResidualBlock(2048) self.residual_block6 = ResidualBlock(2048) self.mpool2 = nn.HybridSequential() with self.mpool2.name_scope(): self.mpool2.add(nn.BatchNorm()) self.mpool2.add(nn.Activation('relu')) self.mpool2.add(nn.AvgPool2D(pool_size=7, strides=1)) self.fc = nn.Conv2D(10, kernel_size=1)
def __init__(self, in_channels, out_channels, bn_use_global_stats=False, **kwargs): super(SEInitBlock, self).__init__(**kwargs) mid_channels = out_channels // 2 with self.name_scope(): self.conv1 = conv3x3_block(in_channels=in_channels, out_channels=mid_channels, strides=2, bn_use_global_stats=bn_use_global_stats) self.conv2 = conv3x3_block(in_channels=mid_channels, out_channels=mid_channels, bn_use_global_stats=bn_use_global_stats) self.conv3 = conv3x3_block(in_channels=mid_channels, out_channels=out_channels, bn_use_global_stats=bn_use_global_stats) self.pool = nn.MaxPool2D(pool_size=3, strides=2, padding=1)
def __init__(self, **kwargs): super(Network, self).__init__(**kwargs) with self.name_scope(): self.conv = nn.Conv2D(230, kernel_size=[3, DIMENSION], padding=[2, 0], strides=1) self.pmp = nn.MaxPool2D(pool_size=[FIXED_WORD_LENGTH, 1]) self.conv_out = nn.Sequential() self.conv_out.add(nn.Flatten()) self.conv_out.add(nn.Activation(activation='relu')) self.center_att = nn.Sequential() self.center_att.add(nn.Dense(1, in_units=200, flatten=False, activation="sigmoid")) self.center_out = nn.Sequential() # self.center_out.add(nn.Dense(200, activation="relu")) self.center_out.add(nn.Activation(activation='relu')) self.output = nn.Sequential() self.output.add(nn.Dropout(0.5)) self.output.add(nn.Dense(128, activation="sigmoid")) self.output.add(nn.Dense(7, activation='tanh'))
def _make_features(self, layers, filters, batch_norm): featurizer = nn.HybridSequential(prefix='') first = True for i, num in enumerate(layers): for _ in range(num): conv_block = nn.Conv2D if first else nn.QConv2D first = False featurizer.add( conv_block(filters[i], kernel_size=3, padding=1, weight_initializer=Xavier(rnd_type='gaussian', factor_type='out', magnitude=2), bias_initializer='zeros')) if batch_norm: featurizer.add(nn.BatchNorm()) featurizer.add(nn.Activation('relu')) featurizer.add(nn.MaxPool2D(strides=2)) return featurizer
def resnet18(num_classes): """The ResNet-18 model.""" net = nn.Sequential() net.add(nn.Conv2D(64, kernel_size=3, strides=1, padding=1), nn.BatchNorm(), nn.Activation('relu'), nn.MaxPool2D(pool_size=3, strides=2, padding=1)) def resnet_block(num_channels, num_residuals, first_block=False): blk = nn.Sequential() for i in range(num_residuals): if i == 0 and not first_block: blk.add(Residual(num_channels, use_1x1conv=True, strides=2)) else: blk.add(Residual(num_channels)) return blk net.add(resnet_block(64, 2, first_block=True), resnet_block(128, 2), resnet_block(256, 2), resnet_block(512, 2)) net.add(nn.GlobalAvgPool2D(), nn.Dense(num_classes)) return net
def _make_features(self, layers, filters, batch_norm): featurizer = nn.HybridSequential(prefix='') for i, num in enumerate(layers): for _ in range(num): featurizer.add( nn.Conv2D( filters[i], kernel_size=3, padding=1, weight_initializer=Xavier(rnd_type='gaussian', factor_type='out', magnitude=2), bias_initializer='zeros', #name = 'conv%s_%s'%(str(i+1),str(_+1))\ )) if batch_norm: featurizer.add(nn.BatchNorm()) featurizer.add(nn.Activation('relu')) featurizer.add(nn.MaxPool2D(strides=2)) return featurizer