def __init__(self, layers=50, pretrained=None, lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0]): super(ResNetTweaksTSN, self).__init__() self.pretrained = pretrained self.layers = layers supported_layers = [18, 34, 50, 101, 152, 200] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) self.lr_mult_list = lr_mult_list assert isinstance( self.lr_mult_list, (list, tuple )), "lr_mult_list should be in (list, tuple) but got {}".format( type(self.lr_mult_list)) assert len( self.lr_mult_list ) == 5, "lr_mult_list length should should be 5 but got {}".format( len(self.lr_mult_list)) if layers == 18: depth = [2, 2, 2, 2] elif layers == 34 or layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] elif layers == 200: depth = [3, 12, 48, 3] num_channels = [64, 256, 512, 1024 ] if layers >= 50 else [64, 64, 128, 256] num_filters = [64, 128, 256, 512] self.conv1_1 = ConvBNLayer(in_channels=3, out_channels=32, kernel_size=3, stride=2, act='relu', lr_mult=self.lr_mult_list[0], name="conv1_1") self.conv1_2 = ConvBNLayer(in_channels=32, out_channels=32, kernel_size=3, stride=1, act='relu', lr_mult=self.lr_mult_list[0], name="conv1_2") self.conv1_3 = ConvBNLayer(in_channels=32, out_channels=64, kernel_size=3, stride=1, act='relu', lr_mult=self.lr_mult_list[0], name="conv1_3") self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] if layers >= 50: for block in range(len(depth)): shortcut = False for i in range(depth[block]): if layers in [101, 152, 200] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BottleneckBlock( in_channels=num_channels[block] if i == 0 else num_filters[block] * 4, out_channels=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, if_first=block == i == 0, lr_mult=self.lr_mult_list[block + 1], name=conv_name)) self.block_list.append(bottleneck_block) shortcut = True else: for block in range(len(depth)): shortcut = False for i in range(depth[block]): conv_name = "res" + str(block + 2) + chr(97 + i) basic_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BasicBlock(in_channels=num_channels[block] if i == 0 else num_filters[block], out_channels=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, if_first=block == i == 0, name=conv_name, lr_mult=self.lr_mult_list[block + 1])) self.block_list.append(basic_block) shortcut = True
def __init__(self, args): super(CDRModel, self).__init__() self.args = args self.use_mut = args.use_mut self.use_gexp = args.use_gexp self.use_methy = args.use_methy self.units_list = args.units_list # [256, 256, 256, 100] self.gnn_type = args.gnn_type # 'gcn' self.act = args.act self.layer_num = args.layer_num # 4 self.pool_type = args.pool_type # 'max' self.gnn_layers = paddle.nn.LayerList() self.bns = paddle.nn.LayerList() self.dropout = paddle.nn.LayerList() if self.gnn_type == 'gcn': for layer_id in range(self.layer_num): self.gnn_layers.append( pgl.nn.GCNConv(self._get_in_size(layer_id), self.units_list[layer_id], activation=self.act)) bn = BatchNorm1D(self.units_list[layer_id], data_format='NC') self.bns.append(bn) dp = Dropout(0.2) self.dropout.append(dp) self.graph_pooling = pgl.nn.GraphPool(self.pool_type) self.linear = Linear(self.units_list[self.layer_num - 1] + 300, 300) elif self.gnn_type == 'gin': for layer_id in range(self.layer_num): self.gnn_layers.append( pgl.nn.GINConv(self._get_in_size(layer_id), self.units_list[layer_id], activation=self.act)) dp = Dropout(0.2) self.dropout.append(dp) self.graph_pooling = pgl.nn.GraphPool(self.pool_type) self.linear = Linear(self.units_list[self.layer_num - 1] + 300, 300) elif self.gnn_type == 'graphsage': for layer_id in range(self.layer_num): self.gnn_layers.append( pgl.nn.GraphSageConv(self._get_in_size(layer_id), self.units_list[layer_id])) dp = Dropout(0.2) self.dropout.append(dp) self.graph_pooling = pgl.nn.GraphPool(self.pool_type) self.linear = Linear(self.units_list[self.layer_num - 1] + 300, 300) self.MP2 = MaxPool2D(kernel_size=(1, 5), data_format='NHWC') self.MP3 = MaxPool2D(kernel_size=(1, 10), data_format='NHWC') self.MP4 = MaxPool2D(kernel_size=(1, 2), data_format='NHWC') self.MP5 = MaxPool2D(kernel_size=(1, 3), data_format='NHWC') self.MP6 = MaxPool2D(kernel_size=(1, 3), data_format='NHWC') self.Conv1 = Conv2D(in_channels=1, out_channels=50, kernel_size=(1, 700), stride=(1, 5), padding='valid', data_format='NHWC') self.Conv2 = Conv2D(in_channels=50, out_channels=30, kernel_size=(1, 5), stride=(1, 2), padding='valid', data_format='NHWC') self.Conv3 = Conv2D(in_channels=1, out_channels=30, kernel_size=(1, 150), stride=(1, 1), padding='VALID', data_format='NHWC') self.Conv4 = Conv2D(in_channels=30, out_channels=10, kernel_size=(1, 5), stride=(1, 1), padding='VALID', data_format='NHWC') self.Conv5 = Conv2D(in_channels=10, out_channels=5, kernel_size=(1, 5), stride=(1, 1), padding='VALID', data_format='NHWC') self.fc1 = Linear(2010, 100) self.fc2 = Linear(697, 256) self.fc3 = Linear(256, 100) self.fc4 = Linear(808, 256) self.fc5 = Linear(256, 100) self.fc6 = Linear(30, 1) self.tanhs = paddle.nn.LayerList([paddle.nn.Tanh() for _ in range(8)]) self.relus = paddle.nn.LayerList([paddle.nn.ReLU() for _ in range(8)]) self.dropout1 = paddle.nn.LayerList([Dropout(0.1) for _ in range(8)]) self.dropout2 = Dropout(0.2) self.flat = paddle.nn.LayerList([Flatten() for _ in range(5)])
def __init__(self, layers=50, class_dim=1000, cardinality=32): super(ResNeXt, self).__init__() self.layers = layers self.cardinality = cardinality self.reduction_ratio = 16 supported_layers = [50, 101, 152] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) supported_cardinality = [32, 64] assert cardinality in supported_cardinality, \ "supported cardinality is {} but input cardinality is {}" \ .format(supported_cardinality, cardinality) if layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] num_channels = [64, 256, 512, 1024] num_filters = [128, 256, 512, 1024] if cardinality == 32 else [256, 512, 1024, 2048] if layers < 152: self.conv = ConvBNLayer( num_channels=3, num_filters=64, filter_size=7, stride=2, act='relu', name="conv1") else: self.conv1_1 = ConvBNLayer( num_channels=3, num_filters=64, filter_size=3, stride=2, act='relu', name="conv1") self.conv1_2 = ConvBNLayer( num_channels=64, num_filters=64, filter_size=3, stride=1, act='relu', name="conv2") self.conv1_3 = ConvBNLayer( num_channels=64, num_filters=128, filter_size=3, stride=1, act='relu', name="conv3") self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] n = 1 if layers == 50 or layers == 101 else 3 for block in range(len(depth)): n += 1 shortcut = False for i in range(depth[block]): bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BottleneckBlock( num_channels=num_channels[block] if i == 0 else num_filters[block] * int(64 // self.cardinality), num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, cardinality=self.cardinality, reduction_ratio=self.reduction_ratio, shortcut=shortcut, if_first=block == 0, name=str(n) + '_' + str(i + 1))) self.block_list.append(bottleneck_block) shortcut = True self.pool2d_avg = AdaptiveAvgPool2D(1) self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear( self.pool2d_avg_channels, class_dim, weight_attr=ParamAttr( initializer=Uniform(-stdv, stdv), name="fc6_weights"), bias_attr=ParamAttr(name="fc6_offset"))
def __init__(self, layers=60, bn_size=4, dropout=0, class_dim=1000): super(DenseNet, self).__init__() supported_layers = [121, 161, 169, 201, 264] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) densenet_spec = { 121: (64, 32, [6, 12, 24, 16]), 161: (96, 48, [6, 12, 36, 24]), 169: (64, 32, [6, 12, 32, 32]), 201: (64, 32, [6, 12, 48, 32]), 264: (64, 32, [6, 12, 64, 48]) } num_init_features, growth_rate, block_config = densenet_spec[layers] self.conv1_func = ConvBNLayer(num_channels=3, num_filters=num_init_features, filter_size=7, stride=2, pad=3, act='relu', name="conv1") self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_config = block_config self.dense_block_func_list = [] self.transition_func_list = [] pre_num_channels = num_init_features num_features = num_init_features for i, num_layers in enumerate(block_config): self.dense_block_func_list.append( self.add_sublayer( "db_conv_{}".format(i + 2), DenseBlock(num_channels=pre_num_channels, num_layers=num_layers, bn_size=bn_size, growth_rate=growth_rate, dropout=dropout, name='conv' + str(i + 2)))) num_features = num_features + num_layers * growth_rate pre_num_channels = num_features if i != len(block_config) - 1: self.transition_func_list.append( self.add_sublayer( "tr_conv{}_blk".format(i + 2), TransitionLayer(num_channels=pre_num_channels, num_output_features=num_features // 2, name='conv' + str(i + 2) + "_blk"))) pre_num_channels = num_features // 2 num_features = num_features // 2 self.batch_norm = BatchNorm( num_features, act="relu", param_attr=ParamAttr(name='conv5_blk_bn_scale'), bias_attr=ParamAttr(name='conv5_blk_bn_offset'), moving_mean_name='conv5_blk_bn_mean', moving_variance_name='conv5_blk_bn_variance') self.pool2d_avg = AdaptiveAvgPool2D(1) stdv = 1.0 / math.sqrt(num_features * 1.0) self.out = Linear(num_features, class_dim, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc_weights"), bias_attr=ParamAttr(name="fc_offset"))
def __init__(self, layers=50, scales=4, width=26, class_dim=1000): super(Res2Net_vd, self).__init__() self.layers = layers self.scales = scales self.width = width basic_width = self.width * self.scales supported_layers = [50, 101, 152, 200] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) if layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] elif layers == 200: depth = [3, 12, 48, 3] num_channels = [64, 256, 512, 1024] num_channels2 = [256, 512, 1024, 2048] num_filters = [basic_width * t for t in [1, 2, 4, 8]] self.conv1_1 = ConvBNLayer(num_channels=3, num_filters=32, filter_size=3, stride=2, act='relu', name="conv1_1") self.conv1_2 = ConvBNLayer(num_channels=32, num_filters=32, filter_size=3, stride=1, act='relu', name="conv1_2") self.conv1_3 = ConvBNLayer(num_channels=32, num_filters=64, filter_size=3, stride=1, act='relu', name="conv1_3") self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] for block in range(len(depth)): shortcut = False for i in range(depth[block]): if layers in [101, 152, 200] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BottleneckBlock(num_channels1=num_channels[block] if i == 0 else num_channels2[block], num_channels2=num_channels2[block], num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, scales=scales, shortcut=shortcut, if_first=block == i == 0, name=conv_name)) self.block_list.append(bottleneck_block) shortcut = True self.pool2d_avg = AdaptiveAvgPool2D(1) self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear(self.pool2d_avg_channels, class_dim, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc_weights"), bias_attr=ParamAttr(name="fc_offset"))
def __init__(self, layers=101, cardinality=32, width=16, pretrained=False): super(ResNeXt101_32x16d_wsl, self).__init__() self.pretrained = pretrained self.layers = layers self.cardinality = cardinality self.width = width self.scale = width // 8 self.depth = [3, 4, 23, 3] self.base_width = cardinality * width num_filters = [self.base_width * i for i in [1, 2, 4, 8]] # [256, 512, 1024, 2048] self._conv_stem = ConvBNLayer( 3, 64, 7, stride=2, act="relu", name="conv1") self._pool = MaxPool2D(kernel_size=3, stride=2, padding=1) self._conv1_0 = BottleneckBlock( 64, num_filters[0], stride=1, cardinality=self.cardinality, width=self.width, name="layer1.0") self._conv1_1 = BottleneckBlock( num_filters[0] // (width // 8), num_filters[0], stride=1, cardinality=self.cardinality, width=self.width, name="layer1.1") self._conv1_2 = BottleneckBlock( num_filters[0] // (width // 8), num_filters[0], stride=1, cardinality=self.cardinality, width=self.width, name="layer1.2") self._conv2_0 = BottleneckBlock( num_filters[0] // (width // 8), num_filters[1], stride=2, cardinality=self.cardinality, width=self.width, name="layer2.0") self._conv2_1 = BottleneckBlock( num_filters[1] // (width // 8), num_filters[1], stride=1, cardinality=self.cardinality, width=self.width, name="layer2.1") self._conv2_2 = BottleneckBlock( num_filters[1] // (width // 8), num_filters[1], stride=1, cardinality=self.cardinality, width=self.width, name="layer2.2") self._conv2_3 = BottleneckBlock( num_filters[1] // (width // 8), num_filters[1], stride=1, cardinality=self.cardinality, width=self.width, name="layer2.3") self._conv3_0 = BottleneckBlock( num_filters[1] // (width // 8), num_filters[2], stride=2, cardinality=self.cardinality, width=self.width, name="layer3.0") self._conv3_1 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.1") self._conv3_2 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.2") self._conv3_3 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.3") self._conv3_4 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.4") self._conv3_5 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.5") self._conv3_6 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.6") self._conv3_7 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.7") self._conv3_8 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.8") self._conv3_9 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.9") self._conv3_10 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.10") self._conv3_11 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.11") self._conv3_12 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.12") self._conv3_13 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.13") self._conv3_14 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.14") self._conv3_15 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.15") self._conv3_16 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.16") self._conv3_17 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.17") self._conv3_18 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.18") self._conv3_19 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.19") self._conv3_20 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.20") self._conv3_21 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.21") self._conv3_22 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.22") self._conv4_0 = BottleneckBlock( num_filters[2] // (width // 8), num_filters[3], stride=2, cardinality=self.cardinality, width=self.width, name="layer4.0") self._conv4_1 = BottleneckBlock( num_filters[3] // (width // 8), num_filters[3], stride=1, cardinality=self.cardinality, width=self.width, name="layer4.1") self._conv4_2 = BottleneckBlock( num_filters[3] // (width // 8), num_filters[3], stride=1, cardinality=self.cardinality, width=self.width, name="layer4.2") if pretrained: utils.load_entire_model(self, 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNeXt101_32x16_wsl_pretrained.pdparams')
def __init__(self): super(VGG, self).__init__() in_channels = [3, 64, 128, 256, 512, 512] # 定义第一个卷积块,包含两个卷积 self.conv1_1 = Conv2D( in_channels=in_channels[0], out_channels=in_channels[1], kernel_size=3, padding=1, stride=1) self.conv1_2 = Conv2D( in_channels=in_channels[1], out_channels=in_channels[1], kernel_size=3, padding=1, stride=1) # 定义第二个卷积块,包含两个卷积 self.conv2_1 = Conv2D( in_channels=in_channels[1], out_channels=in_channels[2], kernel_size=3, padding=1, stride=1) self.conv2_2 = Conv2D( in_channels=in_channels[2], out_channels=in_channels[2], kernel_size=3, padding=1, stride=1) # 定义第三个卷积块,包含三个卷积 self.conv3_1 = Conv2D( in_channels=in_channels[2], out_channels=in_channels[3], kernel_size=3, padding=1, stride=1) self.conv3_2 = Conv2D( in_channels=in_channels[3], out_channels=in_channels[3], kernel_size=3, padding=1, stride=1) self.conv3_3 = Conv2D( in_channels=in_channels[3], out_channels=in_channels[3], kernel_size=3, padding=1, stride=1) # 定义第四个卷积块,包含三个卷积 self.conv4_1 = Conv2D( in_channels=in_channels[3], out_channels=in_channels[4], kernel_size=3, padding=1, stride=1) self.conv4_2 = Conv2D( in_channels=in_channels[4], out_channels=in_channels[4], kernel_size=3, padding=1, stride=1) self.conv4_3 = Conv2D( in_channels=in_channels[4], out_channels=in_channels[4], kernel_size=3, padding=1, stride=1) # 定义第五个卷积块,包含三个卷积 self.conv5_1 = Conv2D( in_channels=in_channels[4], out_channels=in_channels[5], kernel_size=3, padding=1, stride=1) self.conv5_2 = Conv2D( in_channels=in_channels[5], out_channels=in_channels[5], kernel_size=3, padding=1, stride=1) self.conv5_3 = Conv2D( in_channels=in_channels[5], out_channels=in_channels[5], kernel_size=3, padding=1, stride=1) # 使用Sequential 将卷积和relu组成一个线性结构(fc + relu) # 当输入为224x224时,经过五个卷积块和池化层后,特征维度变为[512x7x7] self.fc1 = paddle.nn.Sequential( paddle.nn.Linear(512 * 7 * 7, 4096), paddle.nn.ReLU()) self.drop1_ratio = 0.5 self.dropout1 = paddle.nn.Dropout( self.drop1_ratio, mode='upscale_in_train') # 使用Sequential 将卷积和relu组成一个线性结构(fc + relu) self.fc2 = paddle.nn.Sequential( paddle.nn.Linear(4096, 4096), paddle.nn.ReLU()) self.drop2_ratio = 0.5 self.dropout2 = paddle.nn.Dropout( self.drop2_ratio, mode='upscale_in_train') self.fc3 = paddle.nn.Linear(4096, 1) self.relu = paddle.nn.ReLU() self.pool = MaxPool2D(stride=2, kernel_size=2)
def __init__(self, layers=50, class_dim=1000, lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0]): super(ResNet_vd, self).__init__() self.layers = layers supported_layers = [18, 34, 50, 101, 152, 200] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) self.lr_mult_list = lr_mult_list assert isinstance(self.lr_mult_list, ( list, tuple )), "lr_mult_list should be in (list, tuple) but got {}".format( type(self.lr_mult_list)) assert len( self.lr_mult_list ) == 5, "lr_mult_list length should should be 5 but got {}".format( len(self.lr_mult_list)) if layers == 18: depth = [2, 2, 2, 2] elif layers == 34 or layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] elif layers == 200: depth = [3, 12, 48, 3] num_channels = [64, 256, 512, 1024] if layers >= 50 else [64, 64, 128, 256] num_filters = [64, 128, 256, 512] self.conv1_1 = ConvBNLayer( num_channels=3, num_filters=32, filter_size=3, stride=2, act='relu', lr_mult=self.lr_mult_list[0], name="conv1_1") self.conv1_2 = ConvBNLayer( num_channels=32, num_filters=32, filter_size=3, stride=1, act='relu', lr_mult=self.lr_mult_list[0], name="conv1_2") self.conv1_3 = ConvBNLayer( num_channels=32, num_filters=64, filter_size=3, stride=1, act='relu', lr_mult=self.lr_mult_list[0], name="conv1_3") self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] if layers >= 50: for block in range(len(depth)): shortcut = False for i in range(depth[block]): if layers in [101, 152, 200] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BottleneckBlock( num_channels=num_channels[block] if i == 0 else num_filters[block] * 4, num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, if_first=block == i == 0, lr_mult=self.lr_mult_list[block + 1], name=conv_name)) self.block_list.append(bottleneck_block) shortcut = True else: for block in range(len(depth)): shortcut = False for i in range(depth[block]): conv_name = "res" + str(block + 2) + chr(97 + i) basic_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BasicBlock( num_channels=num_channels[block] if i == 0 else num_filters[block], num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, if_first=block == i == 0, name=conv_name, lr_mult=self.lr_mult_list[block + 1])) self.block_list.append(basic_block) shortcut = True self.pool2d_avg = AdaptiveAvgPool2D(1) self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear( self.pool2d_avg_channels, class_dim, weight_attr=ParamAttr( initializer=Uniform(-stdv, stdv), name="fc_0.w_0"), bias_attr=ParamAttr(name="fc_0.b_0"))
def __init__(self, layers=50, num_classes=1000): super(ResNet, self).__init__() self.layers = layers supported_layers = [18, 34, 50, 101, 152] assert layers in supported_layers if layers == 18: depth = [2, 2, 2, 2] elif layers == 34: depth = [3, 4, 6, 3] elif layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] if layers < 50: num_channels = [64, 64, 128, 256] else: num_channels = [64, 256, 512, 1024] num_filters = [64, 128, 256, 512] self.conv = ConvBNLayer(num_channels=3, num_filters=64, filter_size=7, stride=2, act='relu') self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) if layers < 50: block = BasicBlock l1_shortcut = True else: block = BottleneckBlock l1_shortcut = False self.layer1 = Sequential(*self.make_layer(block, num_channels[0], num_filters[0], depth[0], stride=1, shortcut=l1_shortcut, name='layer1')) self.layer2 = Sequential(*self.make_layer(block, num_channels[1], num_filters[1], depth[1], stride=2, name='layer2')) self.layer3 = Sequential(*self.make_layer(block, num_channels[2], num_filters[2], depth[2], stride=1, name='layer3', dilation=2)) self.layer4 = Sequential(*self.make_layer(block, num_channels[3], num_filters[3], depth[3], stride=1, name='layer4', dilation=4)) self.last_pool = AvgPool2D( kernel_size=7 # ignore if global_pooling is True ) self.fc = Linear(in_features=num_filters[-1] * block.expansion, out_features=num_classes) self.out_dim = num_filters[-1] * block.expansion
def __init__(self, layers=200, scales=4, width=26, pretrained=False): super(Res2Net200_vd_26w_4s_ssld, self).__init__() self.layers = layers self.pretrained = pretrained self.scales = scales self.width = width basic_width = self.width * self.scales supported_layers = [50, 101, 152, 200] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) if layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] elif layers == 200: depth = [3, 12, 48, 3] num_channels = [64, 256, 512, 1024] num_channels2 = [256, 512, 1024, 2048] num_filters = [basic_width * t for t in [1, 2, 4, 8]] self.conv1_1 = ConvBNLayer(num_channels=3, num_filters=32, filter_size=3, stride=2, act='relu', name="conv1_1") self.conv1_2 = ConvBNLayer(num_channels=32, num_filters=32, filter_size=3, stride=1, act='relu', name="conv1_2") self.conv1_3 = ConvBNLayer(num_channels=32, num_filters=64, filter_size=3, stride=1, act='relu', name="conv1_3") self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] for block in range(len(depth)): shortcut = False for i in range(depth[block]): if layers in [101, 152, 200] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BottleneckBlock(num_channels1=num_channels[block] if i == 0 else num_channels2[block], num_channels2=num_channels2[block], num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, scales=scales, shortcut=shortcut, if_first=block == i == 0, name=conv_name)) self.block_list.append(bottleneck_block) shortcut = True if pretrained: utils.load_entire_model( self, 'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/Res2Net200_vd_26w_4s_pretrained.pdparams' )
def __init__(self, depth=50, variant='b', lr_mult=1., norm_type='bn', norm_decay=0, freeze_norm=True, freeze_at=0, return_idx=[0, 1, 2, 3], num_stages=4): super(ResNet, self).__init__() self.depth = depth self.variant = variant self.norm_type = norm_type self.norm_decay = norm_decay self.freeze_norm = freeze_norm self.freeze_at = freeze_at if isinstance(return_idx, Integral): return_idx = [return_idx] assert max(return_idx) < num_stages, \ 'the maximum return index must smaller than num_stages, ' \ 'but received maximum return index is {} and num_stages ' \ 'is {}'.format(max(return_idx), num_stages) self.return_idx = return_idx self.num_stages = num_stages block_nums = ResNet_cfg[depth] na = NameAdapter(self) conv1_name = na.fix_c1_stage_name() if variant in ['c', 'd']: conv_def = [ [3, 32, 3, 2, "conv1_1"], [32, 32, 3, 1, "conv1_2"], [32, 64, 3, 1, "conv1_3"], ] else: conv_def = [[3, 64, 7, 2, conv1_name]] self.conv1 = nn.Sequential() for (c_in, c_out, k, s, _name) in conv_def: self.conv1.add_sublayer( _name, ConvNormLayer(ch_in=c_in, ch_out=c_out, filter_size=k, stride=s, name_adapter=na, act='relu', norm_type=norm_type, norm_decay=norm_decay, freeze_norm=freeze_norm, lr=lr_mult, name=_name)) self.pool = MaxPool2D(kernel_size=3, stride=2, padding=1) ch_in_list = [64, 256, 512, 1024] ch_out_list = [64, 128, 256, 512] self.res_layers = [] for i in range(num_stages): stage_num = i + 2 res_name = "res{}".format(stage_num) res_layer = self.add_sublayer( res_name, Blocks(ch_in_list[i], ch_out_list[i], count=block_nums[i], name_adapter=na, stage_num=stage_num, lr=lr_mult, norm_type=norm_type, norm_decay=norm_decay, freeze_norm=freeze_norm)) self.res_layers.append(res_layer)
def __init__(self, config, version="vb", class_num=1000, lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0], data_format="NCHW", input_image_channel=3, return_patterns=None): super().__init__() self.cfg = config self.lr_mult_list = lr_mult_list self.is_vd_mode = version == "vd" self.class_num = class_num self.num_filters = [64, 128, 256, 512] self.block_depth = self.cfg["block_depth"] self.block_type = self.cfg["block_type"] self.num_channels = self.cfg["num_channels"] self.channels_mult = 1 if self.num_channels[-1] == 256 else 4 assert isinstance(self.lr_mult_list, ( list, tuple )), "lr_mult_list should be in (list, tuple) but got {}".format( type(self.lr_mult_list)) assert len(self.lr_mult_list ) == 5, "lr_mult_list length should be 5 but got {}".format( len(self.lr_mult_list)) self.stem_cfg = { #num_channels, num_filters, filter_size, stride "vb": [[input_image_channel, 64, 7, 2]], "vd": [[input_image_channel, 32, 3, 2], [32, 32, 3, 1], [32, 64, 3, 1]] } self.stem = nn.Sequential(* [ ConvBNLayer( num_channels=in_c, num_filters=out_c, filter_size=k, stride=s, act="relu", lr_mult=self.lr_mult_list[0], data_format=data_format) for in_c, out_c, k, s in self.stem_cfg[version] ]) self.max_pool = MaxPool2D( kernel_size=3, stride=2, padding=1, data_format=data_format) block_list = [] for block_idx in range(len(self.block_depth)): shortcut = False for i in range(self.block_depth[block_idx]): block_list.append(globals()[self.block_type]( num_channels=self.num_channels[block_idx] if i == 0 else self.num_filters[block_idx] * self.channels_mult, num_filters=self.num_filters[block_idx], stride=2 if i == 0 and block_idx != 0 else 1, shortcut=shortcut, if_first=block_idx == i == 0 if version == "vd" else True, lr_mult=self.lr_mult_list[block_idx + 1], data_format=data_format)) shortcut = True self.blocks = nn.Sequential(*block_list) self.avg_pool = AdaptiveAvgPool2D(1, data_format=data_format) self.flatten = nn.Flatten() self.avg_pool_channels = self.num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.avg_pool_channels * 1.0) self.fc = Linear( self.avg_pool_channels, self.class_num, weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv))) self.data_format = data_format if return_patterns is not None: self.update_res(return_patterns) self.register_forward_post_hook(self._return_dict_hook)
def __init__(self, depth, num_seg=8, pretrained=None): super(ResNetTweaksTSM, self).__init__() self.pretrained = pretrained self.layers = depth self.num_seg = num_seg supported_layers = [18, 34, 50, 101, 152] assert self.layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, self.layers) if self.layers == 18: depth = [2, 2, 2, 2] elif self.layers == 34 or self.layers == 50: depth = [3, 4, 6, 3] elif self.layers == 101: depth = [3, 4, 23, 3] elif self.layers == 152: depth = [3, 8, 36, 3] in_channels = 64 out_channels = [64, 128, 256, 512] #ResNet-C: use three 3x3 conv, replace, one 7x7 conv self.conv1_1 = ConvBNLayer(in_channels=3, out_channels=32, kernel_size=3, stride=2, act='leaky_relu', name="conv1_1") self.conv1_2 = ConvBNLayer(in_channels=32, out_channels=32, kernel_size=3, stride=1, act='leaky_relu', name="conv1_2") self.conv1_3 = ConvBNLayer(in_channels=32, out_channels=64, kernel_size=3, stride=1, act='leaky_relu', name="conv1_3") self.pool2D_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] if self.layers >= 50: for block in range(len(depth)): shortcut = False for i in range(depth[block]): if self.layers in [101, 152] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), #same with PaddleClas, for loading pretrain BottleneckBlock( in_channels=in_channels if i == 0 else out_channels[block] * 4, out_channels=out_channels[block], stride=2 if i == 0 and block != 0 else 1, num_seg=self.num_seg, shortcut=shortcut, if_first=block == i == 0, name=conv_name)) in_channels = out_channels[block] * 4 self.block_list.append(bottleneck_block) shortcut = True else: for block in range(len(depth)): shortcut = False for i in range(depth[block]): conv_name = "res" + str(block + 2) + chr(97 + i) basic_block = self.add_sublayer( conv_name, BasicBlock(in_channels=in_channels[block] if i == 0 else out_channels[block], out_channels=out_channels[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, name=conv_name)) self.block_list.append(basic_block) shortcut = True
def __init__(self, label_list: list = None, load_checkpoint: str = None): super(ResNet50_vd, self).__init__() self.layers = 50 if label_list is not None: self.labels = label_list class_dim = len(self.labels) else: label_list = [] label_file = os.path.join(self.directory, 'label_list.txt') files = open(label_file) for line in files.readlines(): line = line.strip('\n') label_list.append(line) self.labels = label_list class_dim = len(self.labels) depth = [3, 4, 6, 3] num_channels = [64, 256, 512, 1024] num_filters = [64, 128, 256, 512] self.conv1_1 = ConvBNLayer(num_channels=3, num_filters=32, filter_size=3, stride=2, act='relu', name="conv1_1") self.conv1_2 = ConvBNLayer(num_channels=32, num_filters=32, filter_size=3, stride=1, act='relu', name="conv1_2") self.conv1_3 = ConvBNLayer(num_channels=32, num_filters=64, filter_size=3, stride=1, act='relu', name="conv1_3") self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] for block in range(len(depth)): shortcut = False for i in range(depth[block]): conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BottleneckBlock(num_channels=num_channels[block] if i == 0 else num_filters[block] * 4, num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, if_first=block == i == 0, name=conv_name)) self.block_list.append(bottleneck_block) shortcut = True self.pool2d_avg = AdaptiveAvgPool2D(1) self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear(self.pool2d_avg_channels, class_dim, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc_0.w_0"), bias_attr=ParamAttr(name="fc_0.b_0")) if load_checkpoint is not None: self.model_dict = paddle.load(load_checkpoint)[0] self.set_dict(self.model_dict) print("load custom checkpoint success") else: checkpoint = os.path.join(self.directory, 'resnet50_vd_ssld.pdparams') self.model_dict = paddle.load(checkpoint) self.set_dict(self.model_dict) print("load pretrained checkpoint success")
def __init__(self, layers=68, class_num=1000): super(DPN, self).__init__() self._class_num = class_num args = self.get_net_args(layers) bws = args['bw'] inc_sec = args['inc_sec'] rs = args['r'] k_r = args['k_r'] k_sec = args['k_sec'] G = args['G'] init_num_filter = args['init_num_filter'] init_filter_size = args['init_filter_size'] init_padding = args['init_padding'] self.k_sec = k_sec self.conv1_x_1_func = ConvBNLayer(num_channels=3, num_filters=init_num_filter, filter_size=init_filter_size, stride=2, pad=init_padding, act='relu', name="conv1") self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1) num_channel_dpn = init_num_filter self.dpn_func_list = [] #conv2 - conv5 match_list, num = [], 0 for gc in range(4): bw = bws[gc] inc = inc_sec[gc] R = (k_r * bw) // rs[gc] if gc == 0: _type1 = 'proj' _type2 = 'normal' match = 1 else: _type1 = 'down' _type2 = 'normal' match = match + k_sec[gc - 1] match_list.append(match) self.dpn_func_list.append( self.add_sublayer( "dpn{}".format(match), DualPathFactory(num_channels=num_channel_dpn, num_1x1_a=R, num_3x3_b=R, num_1x1_c=bw, inc=inc, G=G, _type=_type1, name="dpn" + str(match)))) num_channel_dpn = [bw, 3 * inc] for i_ly in range(2, k_sec[gc] + 1): num += 1 if num in match_list: num += 1 self.dpn_func_list.append( self.add_sublayer( "dpn{}".format(num), DualPathFactory(num_channels=num_channel_dpn, num_1x1_a=R, num_3x3_b=R, num_1x1_c=bw, inc=inc, G=G, _type=_type2, name="dpn" + str(num)))) num_channel_dpn = [ num_channel_dpn[0], num_channel_dpn[1] + inc ] out_channel = sum(num_channel_dpn) self.conv5_x_x_bn = BatchNorm( num_channels=sum(num_channel_dpn), act="relu", param_attr=ParamAttr(name='final_concat_bn_scale'), bias_attr=ParamAttr('final_concat_bn_offset'), moving_mean_name='final_concat_bn_mean', moving_variance_name='final_concat_bn_variance') self.pool2d_avg = AdaptiveAvgPool2D(1) stdv = 0.01 self.out = Linear(out_channel, class_num, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc_weights"), bias_attr=ParamAttr(name="fc_offset"))
def __init__(self, scale=1.0, act="hard_swish", feature_maps=[4, 11, 14], channel_ratio=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]): super(ESNet, self).__init__() self.scale = scale if isinstance(feature_maps, Integral): feature_maps = [feature_maps] self.feature_maps = feature_maps stage_repeats = [3, 7, 3] stage_out_channels = [ -1, 24, make_divisible(128 * scale), make_divisible(256 * scale), make_divisible(512 * scale), 1024 ] self._out_channels = [] self._feature_idx = 0 # 1. conv1 self._conv1 = ConvBNLayer(in_channels=3, out_channels=stage_out_channels[1], kernel_size=3, stride=2, padding=1, act=act) self._max_pool = MaxPool2D(kernel_size=3, stride=2, padding=1) self._feature_idx += 1 # 2. bottleneck sequences self._block_list = [] arch_idx = 0 for stage_id, num_repeat in enumerate(stage_repeats): for i in range(num_repeat): channels_scales = channel_ratio[arch_idx] mid_c = make_divisible(int(stage_out_channels[stage_id + 2] * channels_scales), divisor=8) if i == 0: block = self.add_sublayer( name=str(stage_id + 2) + '_' + str(i + 1), sublayer=InvertedResidualDS( in_channels=stage_out_channels[stage_id + 1], mid_channels=mid_c, out_channels=stage_out_channels[stage_id + 2], stride=2, act=act)) else: block = self.add_sublayer( name=str(stage_id + 2) + '_' + str(i + 1), sublayer=InvertedResidual( in_channels=stage_out_channels[stage_id + 2], mid_channels=mid_c, out_channels=stage_out_channels[stage_id + 2], stride=1, act=act)) self._block_list.append(block) arch_idx += 1 self._feature_idx += 1 self._update_out_channels(stage_out_channels[stage_id + 2], self._feature_idx, self.feature_maps)
def __init__(self, layers=50, classes_num=1000, input_image_channel=3, data_format="NCHW", return_feats=False, get_feats_before_relu=True, pretrained=None): super(ResNet, self).__init__() self.layers = layers self.data_format = data_format self.input_image_channel = input_image_channel self.return_feats = return_feats self.get_feats_before_relu = get_feats_before_relu supported_layers = [18, 34, 50, 101, 152] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) if layers == 18: depth = [2, 2, 2, 2] elif layers == 34 or layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] num_channels = [64, 256, 512, 1024 ] if layers >= 50 else [64, 64, 128, 256] num_filters = [64, 128, 256, 512] self.depth = depth self.conv = ConvBNLayer(num_channels=self.input_image_channel, num_filters=64, filter_size=7, stride=2, act="relu", data_format=self.data_format) self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1, data_format=self.data_format) self.block_list = [] if layers >= 50: for block in range(len(depth)): shortcut = False for i in range(depth[block]): if layers in [101, 152] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( conv_name, BottleneckBlock( num_channels=num_channels[block] if i == 0 else num_filters[block] * 4, num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, data_format=self.data_format, is_last=i == depth[block] - 1)) self.block_list.append(bottleneck_block) shortcut = True else: for block in range(len(depth)): shortcut = False for i in range(depth[block]): conv_name = "res" + str(block + 2) + chr(97 + i) basic_block = self.add_sublayer( conv_name, BasicBlock(num_channels=num_channels[block] if i == 0 else num_filters[block], num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, data_format=self.data_format)) self.block_list.append(basic_block) shortcut = True self.pool2d_avg = AdaptiveAvgPool2D(1, data_format=self.data_format) self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear( self.pool2d_avg_channels, classes_num, weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv)), bias_attr=ParamAttr()) if pretrained is not None: params = paddle.load(pretrained) self.set_dict(params) print("load pretrained model from: {}".format(pretrained))
def __init__(self, layers=101, cardinality=32, width=48, class_dim=1000): super(ResNeXt101WSL, self).__init__() self.class_dim = class_dim self.layers = layers self.cardinality = cardinality self.width = width self.scale = width // 8 self.depth = [3, 4, 23, 3] self.base_width = cardinality * width num_filters = [self.base_width * i for i in [1, 2, 4, 8]] # [256, 512, 1024, 2048] self._conv_stem = ConvBNLayer(3, 64, 7, stride=2, act="relu", name="conv1") self._pool = MaxPool2D(kernel_size=3, stride=2, padding=1) self._conv1_0 = BottleneckBlock(64, num_filters[0], stride=1, cardinality=self.cardinality, width=self.width, name="layer1.0") self._conv1_1 = BottleneckBlock(num_filters[0] // (width // 8), num_filters[0], stride=1, cardinality=self.cardinality, width=self.width, name="layer1.1") self._conv1_2 = BottleneckBlock(num_filters[0] // (width // 8), num_filters[0], stride=1, cardinality=self.cardinality, width=self.width, name="layer1.2") self._conv2_0 = BottleneckBlock(num_filters[0] // (width // 8), num_filters[1], stride=2, cardinality=self.cardinality, width=self.width, name="layer2.0") self._conv2_1 = BottleneckBlock(num_filters[1] // (width // 8), num_filters[1], stride=1, cardinality=self.cardinality, width=self.width, name="layer2.1") self._conv2_2 = BottleneckBlock(num_filters[1] // (width // 8), num_filters[1], stride=1, cardinality=self.cardinality, width=self.width, name="layer2.2") self._conv2_3 = BottleneckBlock(num_filters[1] // (width // 8), num_filters[1], stride=1, cardinality=self.cardinality, width=self.width, name="layer2.3") self._conv3_0 = BottleneckBlock(num_filters[1] // (width // 8), num_filters[2], stride=2, cardinality=self.cardinality, width=self.width, name="layer3.0") self._conv3_1 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.1") self._conv3_2 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.2") self._conv3_3 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.3") self._conv3_4 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.4") self._conv3_5 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.5") self._conv3_6 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.6") self._conv3_7 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.7") self._conv3_8 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.8") self._conv3_9 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.9") self._conv3_10 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.10") self._conv3_11 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.11") self._conv3_12 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.12") self._conv3_13 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.13") self._conv3_14 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.14") self._conv3_15 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.15") self._conv3_16 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.16") self._conv3_17 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.17") self._conv3_18 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.18") self._conv3_19 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.19") self._conv3_20 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.20") self._conv3_21 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.21") self._conv3_22 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[2], stride=1, cardinality=self.cardinality, width=self.width, name="layer3.22") self._conv4_0 = BottleneckBlock(num_filters[2] // (width // 8), num_filters[3], stride=2, cardinality=self.cardinality, width=self.width, name="layer4.0") self._conv4_1 = BottleneckBlock(num_filters[3] // (width // 8), num_filters[3], stride=1, cardinality=self.cardinality, width=self.width, name="layer4.1") self._conv4_2 = BottleneckBlock(num_filters[3] // (width // 8), num_filters[3], stride=1, cardinality=self.cardinality, width=self.width, name="layer4.2") self._avg_pool = AdaptiveAvgPool2D(1) self._out = Linear(num_filters[3] // (width // 8), class_dim, weight_attr=ParamAttr(name="fc.weight"), bias_attr=ParamAttr(name="fc.bias"))
def __init__(self, depth, num_seg=8, pretrained=None): super(ResNetTSM, self).__init__() self.pretrained = pretrained self.layers = depth self.num_seg = num_seg supported_layers = [18, 34, 50, 101, 152] assert self.layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) if self.layers == 18: depth = [2, 2, 2, 2] elif self.layers == 34 or self.layers == 50: depth = [3, 4, 6, 3] elif self.layers == 101: depth = [3, 4, 23, 3] elif self.layers == 152: depth = [3, 8, 36, 3] in_channels = 64 out_channels = [64, 128, 256, 512] self.conv = ConvBNLayer(in_channels=3, out_channels=64, kernel_size=7, stride=2, act="relu", name="conv1") self.pool2D_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] if self.layers >= 50: for block in range(len(depth)): shortcut = False for i in range(depth[block]): if self.layers in [101, 152] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( conv_name, BottleneckBlock( in_channels=in_channels if i == 0 else out_channels[block] * 4, out_channels=out_channels[block], stride=2 if i == 0 and block != 0 else 1, num_seg=self.num_seg, shortcut=shortcut, name=conv_name)) in_channels = out_channels[block] * 4 self.block_list.append(bottleneck_block) shortcut = True else: for block in range(len(depth)): shortcut = False for i in range(depth[block]): conv_name = "res" + str(block + 2) + chr(97 + i) basic_block = self.add_sublayer( conv_name, BasicBlock(in_channels=in_channels[block] if i == 0 else out_channels[block], out_channels=out_channels[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, name=conv_name)) self.block_list.append(basic_block) shortcut = True
def __getitem__(self, index): data = cv2.imread(osp.join(self.data_path, self.data[index][0])) data = self.transform(data) label = self.data[index][1] return data, label def __len__(self): return len(self.data) from paddle.nn import Conv2D, BatchNorm2D, ReLU, Softmax, MaxPool2D, Flatten, Linear ClasModel = paddle.nn.Sequential(Conv2D(3, 6, (3, 3)), BatchNorm2D(6), ReLU(), Conv2D(6, 6, (3, 3)), BatchNorm2D(6), ReLU(), MaxPool2D((2, 2)), Conv2D(6, 12, (3, 3)), BatchNorm2D(12), ReLU(), Conv2D(12, 12, (3, 3)), BatchNorm2D(12), ReLU(), MaxPool2D((2, 2)), Conv2D(12, 8, (3, 3)), BatchNorm2D(8), ReLU(), Conv2D(8, 8, (3, 3)), BatchNorm2D(8), ReLU(), MaxPool2D((2, 2)), Flatten(), Linear(128, 128), ReLU(), Linear(128, 32), ReLU(), Linear(32, 2), Softmax()) train_dataset = HumanClasDataset(mode="train") eval_dataset = HumanClasDataset(mode="eval") # train_loader = paddle.io.DataLoader(train_dataset, batch_size=1, shuffle=True)
def __init__(self, layers=50, class_dim=1000, input_image_channel=3, data_format="NCHW"): super(ResNet, self).__init__() self.layers = layers self.data_format = data_format self.input_image_channel = input_image_channel supported_layers = [18, 34, 50, 101, 152] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) if layers == 18: depth = [2, 2, 2, 2] elif layers == 34 or layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] num_channels = [64, 256, 512, 1024 ] if layers >= 50 else [64, 64, 128, 256] num_filters = [64, 128, 256, 512] self.conv = ConvBNLayer(num_channels=self.input_image_channel, num_filters=64, filter_size=7, stride=2, act="relu", name="conv1", data_format=self.data_format) self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1, data_format=self.data_format) self.block_list = [] if layers >= 50: for block in range(len(depth)): shortcut = False for i in range(depth[block]): if layers in [101, 152] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( conv_name, BottleneckBlock( num_channels=num_channels[block] if i == 0 else num_filters[block] * 4, num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, name=conv_name, data_format=self.data_format)) self.block_list.append(bottleneck_block) shortcut = True else: for block in range(len(depth)): shortcut = False for i in range(depth[block]): conv_name = "res" + str(block + 2) + chr(97 + i) basic_block = self.add_sublayer( conv_name, BasicBlock(num_channels=num_channels[block] if i == 0 else num_filters[block], num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, name=conv_name, data_format=self.data_format)) self.block_list.append(basic_block) shortcut = True self.pool2d_avg = AdaptiveAvgPool2D(1, data_format=self.data_format) self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear(self.pool2d_avg_channels, class_dim, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc_0.w_0"), bias_attr=ParamAttr(name="fc_0.b_0"))
def __init__(self, config): super(TSN_ResNet, self).__init__() self.layers = config.MODEL.num_layers self.seg_num = config.MODEL.seg_num self.class_dim = config.MODEL.num_classes supported_layers = [18, 34, 50, 101, 152] assert self.layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) if self.layers == 18: depth = [2, 2, 2, 2] elif self.layers == 34 or self.layers == 50: depth = [3, 4, 6, 3] elif self.layers == 101: depth = [3, 4, 23, 3] elif self.layers == 152: depth = [3, 8, 36, 3] in_channels = [64, 256, 512, 1024 ] if self.layers >= 50 else [64, 64, 128, 256] out_channels = [64, 128, 256, 512] self.conv = ConvBNLayer(in_channels=3, out_channels=64, kernel_size=7, stride=2, act="relu", name="conv1") self.pool2D_max = MaxPool2D(kernel_size=3, stride=2, padding=1) self.block_list = [] if self.layers >= 50: for block in range(len(depth)): shortcut = False for i in range(depth[block]): if self.layers in [101, 152] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( conv_name, BottleneckBlock( in_channels=in_channels[block] if i == 0 else out_channels[block] * 4, out_channels=out_channels[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, name=conv_name)) self.block_list.append(bottleneck_block) shortcut = True else: for block in range(len(depth)): shortcut = False for i in range(depth[block]): conv_name = "res" + str(block + 2) + chr(97 + i) basic_block = self.add_sublayer( conv_name, BasicBlock(in_channels=in_channels[block] if i == 0 else out_channels[block], out_channels=out_channels[block], stride=2 if i == 0 and block != 0 else 1, shortcut=shortcut, name=conv_name)) self.block_list.append(basic_block) shortcut = True self.pool2D_avg = AvgPool2D(kernel_size=7) self.pool2D_avg_channels = in_channels[-1] * 2 self.out = Linear( self.pool2D_avg_channels, self.class_dim, weight_attr=ParamAttr(initializer=paddle.nn.initializer.Normal( mean=0.0, std=0.01), name="fc_0.w_0"), bias_attr=ParamAttr( initializer=paddle.nn.initializer.Constant(value=0.0), name="fc_0.b_0"))
def __init__(self, class_num=1000): super(GoogLeNetDY, self).__init__() self._conv = ConvLayer(3, 64, 7, 2, name="conv1") self._pool = MaxPool2D(kernel_size=3, stride=2) self._conv_1 = ConvLayer(64, 64, 1, name="conv2_1x1") self._conv_2 = ConvLayer(64, 192, 3, name="conv2_3x3") self._ince3a = Inception(192, 192, 64, 96, 128, 16, 32, 32, name="ince3a") self._ince3b = Inception(256, 256, 128, 128, 192, 32, 96, 64, name="ince3b") self._ince4a = Inception(480, 480, 192, 96, 208, 16, 48, 64, name="ince4a") self._ince4b = Inception(512, 512, 160, 112, 224, 24, 64, 64, name="ince4b") self._ince4c = Inception(512, 512, 128, 128, 256, 24, 64, 64, name="ince4c") self._ince4d = Inception(512, 512, 112, 144, 288, 32, 64, 64, name="ince4d") self._ince4e = Inception(528, 528, 256, 160, 320, 32, 128, 128, name="ince4e") self._ince5a = Inception(832, 832, 256, 160, 320, 32, 128, 128, name="ince5a") self._ince5b = Inception(832, 832, 384, 192, 384, 48, 128, 128, name="ince5b") self._pool_5 = AdaptiveAvgPool2D(1) self._drop = Dropout(p=0.4, mode="downscale_in_infer") self._fc_out = Linear(1024, class_num, weight_attr=xavier(1024, 1, "out"), bias_attr=ParamAttr(name="out_offset")) self._pool_o1 = AvgPool2D(kernel_size=5, stride=3) self._conv_o1 = ConvLayer(512, 128, 1, name="conv_o1") self._fc_o1 = Linear(1152, 1024, weight_attr=xavier(2048, 1, "fc_o1"), bias_attr=ParamAttr(name="fc_o1_offset")) self._drop_o1 = Dropout(p=0.7, mode="downscale_in_infer") self._out1 = Linear(1024, class_num, weight_attr=xavier(1024, 1, "out1"), bias_attr=ParamAttr(name="out1_offset")) self._pool_o2 = AvgPool2D(kernel_size=5, stride=3) self._conv_o2 = ConvLayer(528, 128, 1, name="conv_o2") self._fc_o2 = Linear(1152, 1024, weight_attr=xavier(2048, 1, "fc_o2"), bias_attr=ParamAttr(name="fc_o2_offset")) self._drop_o2 = Dropout(p=0.7, mode="downscale_in_infer") self._out2 = Linear(1024, class_num, weight_attr=xavier(1024, 1, "out2"), bias_attr=ParamAttr(name="out2_offset"))
def __init__(self, scale=1.0, act="relu", num_classes=1000, with_pool=True): super(ShuffleNetV2, self).__init__() self.scale = scale self.num_classes = num_classes self.with_pool = with_pool stage_repeats = [4, 8, 4] activation_layer = create_activation_layer(act) if scale == 0.25: stage_out_channels = [-1, 24, 24, 48, 96, 512] elif scale == 0.33: stage_out_channels = [-1, 24, 32, 64, 128, 512] elif scale == 0.5: stage_out_channels = [-1, 24, 48, 96, 192, 1024] elif scale == 1.0: stage_out_channels = [-1, 24, 116, 232, 464, 1024] elif scale == 1.5: stage_out_channels = [-1, 24, 176, 352, 704, 1024] elif scale == 2.0: stage_out_channels = [-1, 24, 224, 488, 976, 2048] else: raise NotImplementedError("This scale size:[" + str(scale) + "] is not implemented!") # 1. conv1 self._conv1 = ConvNormActivation(in_channels=3, out_channels=stage_out_channels[1], kernel_size=3, stride=2, padding=1, activation_layer=activation_layer) self._max_pool = MaxPool2D(kernel_size=3, stride=2, padding=1) # 2. bottleneck sequences self._block_list = [] for stage_id, num_repeat in enumerate(stage_repeats): for i in range(num_repeat): if i == 0: block = self.add_sublayer(sublayer=InvertedResidualDS( in_channels=stage_out_channels[stage_id + 1], out_channels=stage_out_channels[stage_id + 2], stride=2, activation_layer=activation_layer), name=str(stage_id + 2) + "_" + str(i + 1)) else: block = self.add_sublayer(sublayer=InvertedResidual( in_channels=stage_out_channels[stage_id + 2], out_channels=stage_out_channels[stage_id + 2], stride=1, activation_layer=activation_layer), name=str(stage_id + 2) + "_" + str(i + 1)) self._block_list.append(block) # 3. last_conv self._last_conv = ConvNormActivation( in_channels=stage_out_channels[-2], out_channels=stage_out_channels[-1], kernel_size=1, stride=1, padding=0, activation_layer=activation_layer) # 4. pool if with_pool: self._pool2d_avg = AdaptiveAvgPool2D(1) # 5. fc if num_classes > 0: self._out_c = stage_out_channels[-1] self._fc = Linear(stage_out_channels[-1], num_classes)
def __init__(self, class_dim=1000, scale=1.0, act="relu"): super(ShuffleNet, self).__init__() self.scale = scale self.class_dim = class_dim stage_repeats = [4, 8, 4] if scale == 0.25: stage_out_channels = [-1, 24, 24, 48, 96, 512] elif scale == 0.33: stage_out_channels = [-1, 24, 32, 64, 128, 512] elif scale == 0.5: stage_out_channels = [-1, 24, 48, 96, 192, 1024] elif scale == 1.0: stage_out_channels = [-1, 24, 116, 232, 464, 1024] elif scale == 1.5: stage_out_channels = [-1, 24, 176, 352, 704, 1024] elif scale == 2.0: stage_out_channels = [-1, 24, 224, 488, 976, 2048] else: raise NotImplementedError("This scale size:[" + str(scale) + "] is not implemented!") # 1. conv1 self._conv1 = ConvBNLayer( in_channels=3, out_channels=stage_out_channels[1], kernel_size=3, stride=2, padding=1, act=act, name='stage1_conv') self._max_pool = MaxPool2D(kernel_size=3, stride=2, padding=1) # 2. bottleneck sequences self._block_list = [] for stage_id, num_repeat in enumerate(stage_repeats): for i in range(num_repeat): if i == 0: block = self.add_sublayer( name=str(stage_id + 2) + '_' + str(i + 1), sublayer=InvertedResidualDS( in_channels=stage_out_channels[stage_id + 1], out_channels=stage_out_channels[stage_id + 2], stride=2, act=act, name=str(stage_id + 2) + '_' + str(i + 1))) else: block = self.add_sublayer( name=str(stage_id + 2) + '_' + str(i + 1), sublayer=InvertedResidual( in_channels=stage_out_channels[stage_id + 2], out_channels=stage_out_channels[stage_id + 2], stride=1, act=act, name=str(stage_id + 2) + '_' + str(i + 1))) self._block_list.append(block) # 3. last_conv self._last_conv = ConvBNLayer( in_channels=stage_out_channels[-2], out_channels=stage_out_channels[-1], kernel_size=1, stride=1, padding=0, act=act, name='conv5') # 4. pool self._pool2d_avg = AdaptiveAvgPool2D(1) self._out_c = stage_out_channels[-1] # 5. fc self._fc = Linear( stage_out_channels[-1], class_dim, weight_attr=ParamAttr(name='fc6_weights'), bias_attr=ParamAttr(name='fc6_offset'))
def __init__(self, num_channels, ch1x1, ch3x3reduced, ch3x3, doublech3x3reduced, doublech3x3_1, doublech3x3_2, pool_proj): ''' @Brief `Inception5b` @Parameters num_channels : channel numbers of input tensor ch1x1 : output channel numbers of 1x1 conv ch3x3reduced : channel numbers of 1x1 conv before 3x3 conv ch3x3 : output channel numbers of 3x3 conv doublech3x3reduced : channel numbers of 1x1 conv before the double 3x3 convs doublech3x3_1 : output channel numbers of the first 3x3 conv doublech3x3_2 : output channel numbers of the second 3x3 conv pool_proj : output channel numbers of 1x1 conv after pool @Return `Inception_5b` model ''' super(Inception5b, self).__init__() self.branch1 = ConvBNLayer(num_channels=num_channels, num_filters=ch1x1, filter_size=1, stride=1, padding=0) self.branch2 = paddle.nn.Sequential( ConvBNLayer(num_channels=num_channels, num_filters=ch3x3reduced, filter_size=1, stride=1, padding=0), ConvBNLayer(num_channels=ch3x3reduced, num_filters=ch3x3, filter_size=3, stride=1, padding=1)) self.branch3 = paddle.nn.Sequential( ConvBNLayer(num_channels=num_channels, num_filters=doublech3x3reduced, filter_size=1, stride=1, padding=0), ConvBNLayer(num_channels=doublech3x3reduced, num_filters=doublech3x3_1, filter_size=3, stride=1, padding=1), ConvBNLayer(num_channels=doublech3x3_1, num_filters=doublech3x3_2, filter_size=3, stride=1, padding=1)) self.branch4 = paddle.nn.Sequential( MaxPool2D(kernel_size=3, stride=1, padding=1), ConvBNLayer(num_channels=num_channels, num_filters=pool_proj, filter_size=1, stride=1, padding=0))
def __init__(self, layers=50, class_num=1000, cardinality=32, input_image_channel=3, data_format="NCHW"): super(ResNeXt, self).__init__() self.layers = layers self.data_format = data_format self.input_image_channel = input_image_channel self.cardinality = cardinality supported_layers = [50, 101, 152] assert layers in supported_layers, \ "supported layers are {} but input layer is {}".format( supported_layers, layers) supported_cardinality = [32, 64] assert cardinality in supported_cardinality, \ "supported cardinality is {} but input cardinality is {}" \ .format(supported_cardinality, cardinality) if layers == 50: depth = [3, 4, 6, 3] elif layers == 101: depth = [3, 4, 23, 3] elif layers == 152: depth = [3, 8, 36, 3] num_channels = [64, 256, 512, 1024] num_filters = [128, 256, 512, 1024 ] if cardinality == 32 else [256, 512, 1024, 2048] self.conv = ConvBNLayer(num_channels=self.input_image_channel, num_filters=64, filter_size=7, stride=2, act='relu', name="res_conv1", data_format=self.data_format) self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1, data_format=self.data_format) self.block_list = [] for block in range(len(depth)): shortcut = False for i in range(depth[block]): if layers in [101, 152] and block == 2: if i == 0: conv_name = "res" + str(block + 2) + "a" else: conv_name = "res" + str(block + 2) + "b" + str(i) else: conv_name = "res" + str(block + 2) + chr(97 + i) bottleneck_block = self.add_sublayer( 'bb_%d_%d' % (block, i), BottleneckBlock(num_channels=num_channels[block] if i == 0 else num_filters[block] * int(64 // self.cardinality), num_filters=num_filters[block], stride=2 if i == 0 and block != 0 else 1, cardinality=self.cardinality, shortcut=shortcut, name=conv_name, data_format=self.data_format)) self.block_list.append(bottleneck_block) shortcut = True self.pool2d_avg = AdaptiveAvgPool2D(1, data_format=self.data_format) self.pool2d_avg_channels = num_channels[-1] * 2 stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0) self.out = Linear(self.pool2d_avg_channels, class_num, weight_attr=ParamAttr(initializer=Uniform( -stdv, stdv), name="fc_weights"), bias_attr=ParamAttr(name="fc_offset"))
def __init__(self, class_dim=101, seg_num=24, seglen=1, modality="RGB", weight_devay=None): ''' @Brief: `GoogLeNet` model input image should be 224 * 224 @Parameters: num_channels : channel numbers of input tensor out_dim : the number of classes for classification @Return: out : shape=(X, class_dim) >>> import numpy as np >>> data = np.ones(shape=(8, 3, 224, 224), dtype=np.float32) >>> googlenet = GoogLeNet(class_dim=10) data = paddle.to_tensor(data) y = googlenet(data) print(y.numpy().shape) (8, 10) ''' self.seg_num = seg_num self.seglen = seglen self.modality = modality self.channels = 3 * self.seglen if self.modality == "RGB" else 2 * self.seglen super(GoogLeNet, self).__init__() self.part1_list = paddle.nn.Sequential( ConvBNLayer(num_channels=self.channels, num_filters=64, filter_size=7, stride=2, padding=3), MaxPool2D(kernel_size=3, stride=2, padding=1), ) self.part2_list = paddle.nn.Sequential( ConvBNLayer(num_channels=64, num_filters=64, filter_size=1, stride=1, padding=0), ConvBNLayer(num_channels=64, num_filters=192, filter_size=3, stride=1, padding=1), MaxPool2D(kernel_size=3, stride=2, padding=1), ) ##the values of the two pool_padding layers above are changed from 0 to 1 in order to comply with 28x28 in the paper。However it is 27x27 in the original Caffe code self.googLeNet_part1 = paddle.nn.Sequential( ('part1', self.part1_list), ('part2', self.part2_list), ('inception_3a', Inception(192, 64, 64, 64, 64, 96, 96, 32)), ('inception_3b', Inception(256, 64, 64, 96, 64, 96, 96, 64)), ) self.before3d = Inception3c(320, 128, 160, 64, 96, 96) self.googLeNet_part2 = paddle.nn.Sequential( ('inception_4a', Inception(576, 224, 64, 96, 96, 128, 128, 128)), ('inception_4b', Inception(576, 192, 96, 128, 96, 128, 128, 128)), ('inception_4c', Inception(576, 160, 128, 160, 128, 160, 160, 128)), ('inception_4d', Inception(608, 96, 128, 192, 160, 192, 192, 128)), ) self.googLeNet_part3 = paddle.nn.Sequential( ('inception_4e', Inception4e(608, 128, 192, 192, 256, 256, 608)), ('inception_5a', Inception5a(1056, 352, 192, 320, 160, 224, 224, 128)), ('inception_5b', Inception5b(1024, 352, 192, 320, 192, 224, 224, 128)), ('AvgPool1', paddle.nn.AdaptiveAvgPool2D(output_size=1)), ) self.res3d = Res3D.ResNet3D('resnet', modality='RGB', channels=96) self.out = Linear( in_features=1536, out_features=class_dim, weight_attr=paddle.ParamAttr( initializer=paddle.nn.initializer.XavierNormal())) self.out_3d = []
def __init__(self, num_channels, ch1x1, ch3x3reduced, ch3x3, doublech3x3reduced, doublech3x3_1, doublech3x3_2, pool_proj): ''' @Brief 传入参数用于定义 `Inception5b` 结构 @Parameters num_channels : 传入图片通道数 ch1x1 : 1x1卷积操作的输出通道数 ch3x3reduced : 3x3卷积之前的1x1卷积的通道数 ch3x3 : 3x3卷积操作的输出通道数 doublech3x3reduced : 两个3x3卷积叠加之前的1x1卷积的通道数 doublech3x3_1 : 第一个3x3卷积操作的输出通道数 doublech3x3_2 : 第而个3x3卷积操作的输出通道数 pool_proj : 池化操作之后1x1卷积的通道数 @Return 返回 `Inception_5b` 网络模型 @Examples ------------ ''' super(Inception5b, self).__init__() self.branch1 = ConvBNLayer(num_channels=num_channels, num_filters=ch1x1, filter_size=1, stride=1, padding=0) self.branch2 = paddle.nn.Sequential( ConvBNLayer(num_channels=num_channels, num_filters=ch3x3reduced, filter_size=1, stride=1, padding=0), ConvBNLayer(num_channels=ch3x3reduced, num_filters=ch3x3, filter_size=3, stride=1, padding=1) ) self.branch3 = paddle.nn.Sequential( ConvBNLayer(num_channels=num_channels, num_filters=doublech3x3reduced, filter_size=1, stride=1, padding=0), ConvBNLayer(num_channels=doublech3x3reduced, num_filters=doublech3x3_1, filter_size=3, stride=1, padding=1), ConvBNLayer(num_channels=doublech3x3_1, num_filters=doublech3x3_2, filter_size=3, stride=1, padding=1) ) self.branch4 = paddle.nn.Sequential( MaxPool2D(kernel_size=3, stride=1, padding=1), ConvBNLayer(num_channels=num_channels, num_filters=pool_proj, filter_size=1, stride=1, padding=0) )
def __init__(self, layers, radix=1, groups=1, bottleneck_width=64, dilated=False, dilation=1, deep_stem=False, stem_width=64, avg_down=False, rectify_avg=False, avd=False, avd_first=False, final_drop=0.0, last_gamma=False, class_num=1000): super(ResNeSt, self).__init__() self.cardinality = groups self.bottleneck_width = bottleneck_width # ResNet-D params self.inplanes = stem_width * 2 if deep_stem else 64 self.avg_down = avg_down self.last_gamma = last_gamma # ResNeSt params self.radix = radix self.avd = avd self.avd_first = avd_first self.deep_stem = deep_stem self.stem_width = stem_width self.layers = layers self.final_drop = final_drop self.dilated = dilated self.dilation = dilation self.rectify_avg = rectify_avg if self.deep_stem: self.stem = nn.Sequential(("conv1", ConvBNLayer(num_channels=3, num_filters=stem_width, filter_size=3, stride=2, act="relu", name="conv1")), ("conv2", ConvBNLayer(num_channels=stem_width, num_filters=stem_width, filter_size=3, stride=1, act="relu", name="conv2")), ("conv3", ConvBNLayer(num_channels=stem_width, num_filters=stem_width * 2, filter_size=3, stride=1, act="relu", name="conv3"))) else: self.stem = ConvBNLayer(num_channels=3, num_filters=stem_width, filter_size=7, stride=2, act="relu", name="conv1") self.max_pool2d = MaxPool2D(kernel_size=3, stride=2, padding=1) self.layer1 = ResNeStLayer(inplanes=self.stem_width * 2 if self.deep_stem else self.stem_width, planes=64, blocks=self.layers[0], radix=radix, cardinality=self.cardinality, bottleneck_width=bottleneck_width, avg_down=self.avg_down, avd=avd, avd_first=avd_first, rectify_avg=rectify_avg, last_gamma=last_gamma, stride=1, dilation=1, is_first=False, name="layer1") # return self.layer2 = ResNeStLayer(inplanes=256, planes=128, blocks=self.layers[1], radix=radix, cardinality=self.cardinality, bottleneck_width=bottleneck_width, avg_down=self.avg_down, avd=avd, avd_first=avd_first, rectify_avg=rectify_avg, last_gamma=last_gamma, stride=2, name="layer2") if self.dilated or self.dilation == 4: self.layer3 = ResNeStLayer(inplanes=512, planes=256, blocks=self.layers[2], radix=radix, cardinality=self.cardinality, bottleneck_width=bottleneck_width, avg_down=self.avg_down, avd=avd, avd_first=avd_first, rectify_avg=rectify_avg, last_gamma=last_gamma, stride=1, dilation=2, name="layer3") self.layer4 = ResNeStLayer(inplanes=1024, planes=512, blocks=self.layers[3], radix=radix, cardinality=self.cardinality, bottleneck_width=bottleneck_width, avg_down=self.avg_down, avd=avd, avd_first=avd_first, rectify_avg=rectify_avg, last_gamma=last_gamma, stride=1, dilation=4, name="layer4") elif self.dilation == 2: self.layer3 = ResNeStLayer(inplanes=512, planes=256, blocks=self.layers[2], radix=radix, cardinality=self.cardinality, bottleneck_width=bottleneck_width, avg_down=self.avg_down, avd=avd, avd_first=avd_first, rectify_avg=rectify_avg, last_gamma=last_gamma, stride=2, dilation=1, name="layer3") self.layer4 = ResNeStLayer(inplanes=1024, planes=512, blocks=self.layers[3], radix=radix, cardinality=self.cardinality, bottleneck_width=bottleneck_width, avg_down=self.avg_down, avd=avd, avd_first=avd_first, rectify_avg=rectify_avg, last_gamma=last_gamma, stride=1, dilation=2, name="layer4") else: self.layer3 = ResNeStLayer(inplanes=512, planes=256, blocks=self.layers[2], radix=radix, cardinality=self.cardinality, bottleneck_width=bottleneck_width, avg_down=self.avg_down, avd=avd, avd_first=avd_first, rectify_avg=rectify_avg, last_gamma=last_gamma, stride=2, name="layer3") self.layer4 = ResNeStLayer(inplanes=1024, planes=512, blocks=self.layers[3], radix=radix, cardinality=self.cardinality, bottleneck_width=bottleneck_width, avg_down=self.avg_down, avd=avd, avd_first=avd_first, rectify_avg=rectify_avg, last_gamma=last_gamma, stride=2, name="layer4") self.pool2d_avg = AdaptiveAvgPool2D(1) self.out_channels = 2048 stdv = 1.0 / math.sqrt(self.out_channels * 1.0) self.out = Linear(self.out_channels, class_num, weight_attr=ParamAttr( initializer=nn.initializer.Uniform(-stdv, stdv), name="fc_weights"), bias_attr=ParamAttr(name="fc_offset"))