轻量级人像分割模型:SINet 和 ExtremeC3Net

本文介绍SINet和ExtremeC3Net两个轻量级人像分割模型,二者参数分别为0.087M、0.038M,Flop为0.064G、0.128G。可通过PaddleHub快速调用,也能基于PaddleInference推理部署,并给出了Paddle2.0上的实现代码。

☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

轻量级人像分割模型:sinet 和 extremec3net - 创想鸟

引入

随着算力和算法的不断提升,能够训练的模型也越来越大了,当然精度也越来越高了不过过于巨大的模型也带来了部署上的不便今天就介绍两个轻量级的人像分割模型:SINet 和 ExtremeC3Net

项目说明

项目模型转换至开源项目ext_portrait_segmentation感谢上述项目提供的开源代码和模型

模型规格

具体的模型规格如下表:

model Param Flop

SINet0.087 M0.064 GExtremeC30.038 M0.128 G可以看出这两个模型算是相当轻量的了

效果展示

ExtremeC3Net:

轻量级人像分割模型:SINet 和 ExtremeC3Net - 创想鸟        

SINet:

轻量级人像分割模型:SINet 和 ExtremeC3Net - 创想鸟        

快速使用

按照惯例已经将两个模型封装为PaddleHub Module可通过PaddleHub进行快速调用In [1]

!pip install paddlehub==2.0.0b2

   In [9]

# 导入PaddleHubimport paddlehub as hub# 加载模型# 模型可选:SINet_Portrait_Segmentation 和 ExtremeC3_Portrait_Segmentationmodel = hub.Module(directory='SINet_Portrait_Segmentation')# 人像分割outputs = model.Segmentation(images=None,                       paths=['00001.jpg'],                       batch_size=1,                       output_dir='output',                       visualization=True)# 结果显示%matplotlib inlineimport cv2import numpy as npimport matplotlib.pyplot as pltimg = np.concatenate([    cv2.imread('00001.jpg'),    cv2.cvtColor(outputs[0]['mask'], cv2.COLOR_GRAY2BGR),    outputs[0]['result']], 1)plt.axis('off')plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))plt.show()

       

               In [3]

# 导入PaddleHubimport paddlehub as hub# 加载模型# 模型可选:SINet_Portrait_Segmentation 和 ExtremeC3_Portrait_Segmentationmodel = hub.Module(directory='ExtremeC3_Portrait_Segmentation')# 人像分割outputs = model.Segmentation(images=None,                       paths=['00001.jpg'],                       batch_size=1,                       output_dir='output',                       visualization=True)# 结果显示%matplotlib inlineimport cv2import numpy as npimport matplotlib.pyplot as pltimg = np.concatenate([    cv2.imread('00001.jpg'),    cv2.cvtColor(outputs[0]['mask'], cv2.COLOR_GRAY2BGR),    outputs[0]['result']], 1)plt.axis('off')plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))plt.show()

       

               

推理部署

除了使用PaddleHub一键调用之外,当然也可以使用推理模型进行推理部署接下来简单介绍一下如何基于PaddleInference完成推理部署更多详情可以参考我的另一个项目:PaddleQuickInferenceIn [4]

# 安装PaddleQuickInference!pip install ppqi -i https://pypi.python.org/simple

   In [5]

from ppqi import InferenceModelfrom SINet.processor import preprocess, postprocess# 参数配置configs = {    'img_path': '00001.jpg',    'save_dir': 'save_img',    'model_name': 'SINet_Portrait_Segmentation',    'use_gpu': False,    'use_mkldnn': False}# 第一步:数据预处理input_data = preprocess(configs['img_path'])# 第二步:加载模型model = InferenceModel(    modelpath='SINet/'+configs['model_name'],     use_gpu=configs['use_gpu'],     use_mkldnn=configs['use_mkldnn'])model.eval()# 第三步:模型推理output = model(input_data)# 第四步:结果后处理postprocess(    output,     configs['save_dir'],    configs['img_path'],    configs['model_name'])

   In [6]

from ppqi import InferenceModelfrom ExtremeC3Net.processor import preprocess, postprocess# 参数配置configs = {    'img_path': '00001.jpg',    'save_dir': 'save_img',    'model_name': 'ExtremeC3_Portrait_Segmentation',    'use_gpu': False,    'use_mkldnn': False}# 第一步:数据预处理input_data = preprocess(configs['img_path'])# 第二步:加载模型model = InferenceModel(    modelpath='ExtremeC3Net/'+configs['model_name'],     use_gpu=configs['use_gpu'],     use_mkldnn=configs['use_mkldnn'])model.eval()# 第三步:模型推理output = model(input_data)# 第四步:结果后处理postprocess(    output,     configs['save_dir'],    configs['img_path'],    configs['model_name'])

   

模型实现

接下来再介绍一下如何在Paddle2.0上实现这两个模型吧代码上与原项目的Pytorch代码是相似的针对框架之间的差异将其中的一些算子做了替换具体详情请参考下方代码In [7]

# model/sinet.py'''ExtPortraitSegCopyright (c) 2019-present NAVER Corp.MIT license'''import paddleimport paddle.nn as nnBN_moment = 0.1def channel_shuffle(x, groups):    batchsize, num_channels, height, width = x.shape    channels_per_group = num_channels // groups    # reshape    x = x.reshape([batchsize, groups,               channels_per_group, height, width])    # transpose    x = paddle.transpose(x, [0, 2, 1, 3, 4])        # flatten    x = x.reshape([batchsize, groups*channels_per_group, height, width])    return xclass CBR(nn.Layer):    '''    This class defines the convolution layer with batch normalization and PReLU activation    '''    def __init__(self, nIn, nOut, kSize, stride=1):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param kSize: kernel size        :param stride: stride rate for down-sampling. Default is 1        '''        super().__init__()        padding = int((kSize - 1) / 2)        self.conv = nn.Conv2D(nIn, nOut, (kSize, kSize), stride=stride, padding=(padding, padding), bias_attr=False)        self.bn = nn.BatchNorm2D(nOut, epsilon=1e-03, momentum=BN_moment)        self.act = nn.PReLU(nOut)    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        output = self.conv(input)        output = self.bn(output)        output = self.act(output)        return outputclass separableCBR(nn.Layer):    '''    This class defines the convolution layer with batch normalization and PReLU activation    '''    def __init__(self, nIn, nOut, kSize, stride=1):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param kSize: kernel size        :param stride: stride rate for down-sampling. Default is 1        '''        super().__init__()        padding = int((kSize - 1) / 2)        self.conv = nn.Sequential(            nn.Conv2D(nIn, nIn, (kSize, kSize), stride=stride, padding=(padding, padding), groups=nIn, bias_attr=False),            nn.Conv2D(nIn, nOut,  kernel_size=1, stride=1, bias_attr=False),        )        self.bn = nn.BatchNorm2D(nOut, epsilon=1e-03, momentum= BN_moment)        self.act = nn.PReLU(nOut)    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        output = self.conv(input)        output = self.bn(output)        output = self.act(output)        return outputclass SqueezeBlock(nn.Layer):    def __init__(self, exp_size, divide=4.0):        super(SqueezeBlock, self).__init__()        if divide > 1:            self.dense = nn.Sequential(                nn.Linear(exp_size, int(exp_size / divide)),                nn.PReLU(int(exp_size / divide)),                nn.Linear(int(exp_size / divide), exp_size),                nn.PReLU(exp_size),            )        else:            self.dense = nn.Sequential(                nn.Linear(exp_size, exp_size),                nn.PReLU(exp_size)            )    def forward(self, x):        batch, channels, height, width = x.shape        out = paddle.nn.functional.avg_pool2d(x, kernel_size=[height, width]).reshape([batch, channels])                out = self.dense(out)        out = out.reshape([batch, channels, 1, 1])        return paddle.multiply(out, x)class SEseparableCBR(nn.Layer):    '''    This class defines the convolution layer with batch normalization and PReLU activation    '''    def __init__(self, nIn, nOut, kSize, stride=1, divide=2.0):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param kSize: kernel size        :param stride: stride rate for down-sampling. Default is 1        '''        super().__init__()        padding = int((kSize - 1) / 2)        self.conv = nn.Sequential(            nn.Conv2D(nIn, nIn, (kSize, kSize), stride=stride, padding=(padding, padding), groups=nIn, bias_attr=False),            SqueezeBlock(nIn, divide=divide),            nn.Conv2D(nIn, nOut,  kernel_size=1, stride=1, bias_attr=False),        )        self.bn = nn.BatchNorm2D(nOut, epsilon=1e-03, momentum= BN_moment)        self.act = nn.PReLU(nOut)    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        output = self.conv(input)        output = self.bn(output)        output = self.act(output)        return outputclass BR(nn.Layer):    '''        This class groups the batch normalization and PReLU activation    '''    def __init__(self, nOut):        '''        :param nOut: output feature maps        '''        super().__init__()        self.bn = nn.BatchNorm2D(nOut, epsilon=1e-03, momentum= BN_moment)        self.act = nn.PReLU(nOut)    def forward(self, input):        '''        :param input: input feature map        :return: normalized and thresholded feature map        '''        output = self.bn(input)        output = self.act(output)        return outputclass CB(nn.Layer):    '''       This class groups the convolution and batch normalization    '''    def __init__(self, nIn, nOut, kSize, stride=1):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param kSize: kernel size        :param stride: optinal stide for down-sampling        '''        super().__init__()        padding = int((kSize - 1) / 2)        self.conv = nn.Conv2D(nIn, nOut, (kSize, kSize), stride=stride, padding=(padding, padding), bias_attr=False)        self.bn = nn.BatchNorm2D(nOut, epsilon=1e-03, momentum= BN_moment)    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        output = self.conv(input)        output = self.bn(output)        return outputclass C(nn.Layer):    '''    This class is for a convolutional layer.    '''    def __init__(self, nIn, nOut, kSize, stride=1,group=1):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param kSize: kernel size        :param stride: optional stride rate for down-sampling        '''        super().__init__()        padding = int((kSize - 1) / 2)        self.conv = nn.Conv2D(nIn, nOut, (kSize, kSize), stride=stride,                              padding=(padding, padding), bias_attr=False, groups=group)    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        output = self.conv(input)        return outputclass S2block(nn.Layer):    '''    This class defines the dilated convolution.    '''    def __init__(self, nIn, nOut, config):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param kSize: kernel size        :param stride: optional stride rate for down-sampling        :param d: optional dilation rate        '''        super().__init__()        kSize = config[0]        avgsize = config[1]        self.resolution_down = False        if avgsize >1:            self.resolution_down = True            self.down_res = nn.AvgPool2D(avgsize, avgsize)            self.up_res = nn.Upsample(mode='bilinear', align_corners=True, align_mode=0, scale_factor=avgsize)            self.avgsize = avgsize        padding = int((kSize - 1) / 2 )        self.conv = nn.Sequential(                        nn.Conv2D(nIn, nIn, kernel_size=(kSize, kSize), stride=1,                                  padding=(padding, padding), groups=nIn, bias_attr=False),                        nn.BatchNorm2D(nIn, epsilon=1e-03, momentum=BN_moment))        self.act_conv1x1 = nn.Sequential(            nn.PReLU(nIn),            nn.Conv2D(nIn, nOut, kernel_size=1, stride=1, bias_attr=False),        )        self.bn = nn.BatchNorm2D(nOut, epsilon=1e-03, momentum=BN_moment)    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        if self.resolution_down:            input = self.down_res(input)        output = self.conv(input)        output = self.act_conv1x1(output)        if self.resolution_down:            output = self.up_res(output)        return self.bn(output)class S2module(nn.Layer):    '''    This class defines the ESP block, which is based on the following principle        Reduce ---> Split ---> Transform --> Merge    '''    def __init__(self, nIn, nOut, add=True, config= [[3,1],[5,1]]):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param add: if true, add a residual connection through identity operation. You can use projection too as                in ResNet paper, but we avoid to use it if the dimensions are not the same because we do not want to                increase the module complexity        '''        super().__init__()        group_n = len(config)        n = int(nOut / group_n)        n1 = nOut - group_n * n        self.c1 = C(nIn, n, 1, 1, group=group_n)        for i in range(group_n):            var_name = 'd{}'.format(i + 1)            if i == 0:                self.__dict__["_sub_layers"][var_name] = S2block(n, n + n1, config[i])            else:                self.__dict__["_sub_layers"][var_name] = S2block(n, n,  config[i])        self.BR = BR(nOut)        self.add = add        self.group_n = group_n    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        # reduce        output1 = self.c1(input)        output1= channel_shuffle(output1, self.group_n)        for i in range(self.group_n):            var_name = 'd{}'.format(i + 1)            result_d = self.__dict__["_sub_layers"][var_name](output1)            if i == 0:                combine = result_d            else:                combine = paddle.concat([combine, result_d], 1)        # if residual version        if self.add:            combine = paddle.add(input, combine)        output = self.BR(combine)        return outputclass InputProjectionA(nn.Layer):    '''    This class projects the input image to the same spatial dimensions as the feature map.    For example, if the input image is 512 x512 x3 and spatial dimensions of feature map size are 56x56xF, then    this class will generate an output of 56x56x3    '''    def __init__(self, samplingTimes):        '''        :param samplingTimes: The rate at which you want to down-sample the image        '''        super().__init__()        self.pool = nn.LayerList()        for i in range(0, samplingTimes):            self.pool.append(nn.AvgPool2D(2, stride=2))    def forward(self, input):        '''        :param input: Input RGB Image        :return: down-sampled image (pyramid-based approach)        '''        for pool in self.pool:            input = pool(input)        return inputclass SINet_Encoder(nn.Layer):    def __init__(self, config,classes=20, p=5, q=3,  chnn=1.0):        '''        :param classes: number of classes in the dataset. Default is 20 for the cityscapes        :param p: depth multiplier        :param q: depth multiplier        '''        super().__init__()        dim1 = 16        dim2 = 48 + 4 * (chnn - 1)        dim3 = 96 + 4 * (chnn - 1)        self.level1 = CBR(3, 12, 3, 2)        self.level2_0 = SEseparableCBR(12,dim1, 3,2, divide=1)        self.level2 = nn.LayerList()        for i in range(0, p):            if i ==0:                self.level2.append(S2module(dim1, dim2, config=config[i], add=False))            else:                self.level2.append(S2module(dim2, dim2,config=config[i]))        self.BR2 = BR(dim2+dim1)        self.level3_0 =SEseparableCBR(dim2+dim1,dim2, 3,2, divide=2)        self.level3 = nn.LayerList()        for i in range(0, q):            if i==0:                self.level3.append(S2module(dim2, dim3, config=config[2 + i], add=False))            else:                self.level3.append(S2module(dim3, dim3,config=config[2+i]))        self.BR3 = BR(dim3+dim2)        self.classifier = C(dim3+dim2, classes, 1, 1)    def forward(self, input):        '''        :param input: Receives the input RGB image        :return: the transformed feature map with spatial dimensions 1/8th of the input image        '''        output1 = self.level1(input) #8h 8w        output2_0 = self.level2_0(output1)  # 4h 4w        for i, layer in enumerate(self.level2):            if i == 0:                output2 = layer(output2_0)            else:                output2 = layer(output2) # 2h 2w        output3_0 = self.level3_0(self.BR2(paddle.concat([output2_0, output2],1)))  # h w        for i, layer in enumerate(self.level3):            if i == 0:                output3 = layer(output3_0)            else:                output3 = layer(output3)        output3_cat = self.BR3(paddle.concat([output3_0, output3], 1))        classifier = self.classifier(output3_cat)        return classifierclass SINet(nn.Layer):    def __init__(self,config, classes=20, p=2, q=3, chnn=1.0):        '''        :param classes: number of classes in the dataset. Default is 20 for the cityscapes        :param p: depth multiplier        :param q: depth multiplier        '''        super().__init__()        dim2 = 48 + 4 * (chnn - 1)        self.encoder = SINet_Encoder(config, classes, p, q, chnn)        self.up = nn.Upsample(mode='bilinear', align_corners=True, align_mode=0, scale_factor=2)        self.bn_3 = nn.BatchNorm2D(classes, epsilon=1e-03)        self.level2_C = CBR(dim2, classes, 1, 1)        self.bn_2 = nn.BatchNorm2D(classes, epsilon=1e-03)        self.classifier = nn.Sequential(        nn.Upsample(mode='bilinear', align_corners=True, align_mode=0, scale_factor=2),        nn.Conv2D(classes, classes, 3, 1, 1, bias_attr=False))    def forward(self, input):        '''        :param input: RGB image        :return: transformed feature map        '''        output1 = self.encoder.level1(input)  # 8h 8w        output2_0 = self.encoder.level2_0(output1)  # 4h 4w        for i, layer in enumerate(self.encoder.level2):            if i == 0:                output2 = layer(output2_0)            else:                output2 = layer(output2)  # 2h 2w        output3_0 = self.encoder.level3_0(self.encoder.BR2(paddle.concat([output2_0, output2], 1)))  # h w        for i, layer in enumerate(self.encoder.level3):            if i == 0:                output3 = layer(output3_0)            else:                output3 = layer(output3)        output3_cat = self.encoder.BR3(paddle.concat([output3_0, output3], 1))        Enc_final = self.encoder.classifier(output3_cat) #1/8        Dnc_stage1 = self.bn_3(self.up(Enc_final))  # 1/4        stage1_confidence = paddle.max(nn.functional.softmax(Dnc_stage1, 1), axis=1)        b, c, h, w = Dnc_stage1.shape        stage1_gate = (1-stage1_confidence).unsqueeze(1).expand([b, c, h, w])        Dnc_stage2_0 = self.level2_C(output2)  # 2h 2w        Dnc_stage2 = self.bn_2(self.up(paddle.add(paddle.multiply(Dnc_stage2_0, stage1_gate), (Dnc_stage1))))  # 4h 4w        classifier = self.classifier(Dnc_stage2)        return classifier

   In [8]

# model/extremeC3.py'''ExtPortraitSegCopyright (c) 2019-present NAVER Corp.MIT license'''import paddleimport paddle.nn as nnbasic_0 = 24basic_1 = 48basic_2 = 56basic_3 = 24class CBR(nn.Layer):    '''    This class defines the convolution layer with batch normalization and PReLU activation    '''    def __init__(self, nIn, nOut, kSize, stride=1):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param kSize: kernel size        :param stride: stride rate for down-sampling. Default is 1        '''        super().__init__()        padding = int((kSize - 1) / 2)        # self.conv = nn.Conv2D(nIn, nOut, kSize, stride=stride, padding=padding, bias_attr=False)        self.conv = nn.Conv2D(nIn, nOut, (kSize, kSize), stride=stride, padding=(padding, padding), bias_attr=False)        # self.conv1 = nn.Conv2D(nOut, nOut, (1, kSize), stride=1, padding=(0, padding), bias_attr=False)        self.bn = nn.BatchNorm2D(nOut, epsilon=1e-03)        self.act = nn.PReLU(nOut)        # self.act = nn.ReLU()    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        output = self.conv(input)        # output = self.conv1(output)        output = self.bn(output)        output = self.act(output)        return outputclass BR(nn.Layer):    '''        This class groups the batch normalization and PReLU activation    '''    def __init__(self, nOut):        '''        :param nOut: output feature maps        '''        super().__init__()        self.bn = nn.BatchNorm2D(nOut, epsilon=1e-03)        self.act = nn.PReLU(nOut)        # self.act = nn.ReLU()    def forward(self, input):        '''        :param input: input feature map        :return: normalized and thresholded feature map        '''        output = self.bn(input)        output = self.act(output)        return outputclass CB(nn.Layer):    '''       This class groups the convolution and batch normalization    '''    def __init__(self, nIn, nOut, kSize, stride=1):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param kSize: kernel size        :param stride: optinal stide for down-sampling        '''        super().__init__()        padding = int((kSize - 1) / 2)        self.conv = nn.Conv2D(nIn, nOut, (kSize, kSize), stride=stride, padding=(padding, padding), bias_attr=False)        self.bn = nn.BatchNorm2D(nOut, epsilon=1e-03)    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        output = self.conv(input)        output = self.bn(output)        return outputclass C(nn.Layer):    '''    This class is for a convolutional layer.    '''    def __init__(self, nIn, nOut, kSize, stride=1):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param kSize: kernel size        :param stride: optional stride rate for down-sampling        '''        super().__init__()        padding = int((kSize - 1) / 2)        self.conv = nn.Conv2D(nIn, nOut, (kSize, kSize), stride=stride, padding=(padding, padding), bias_attr=False)    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        output = self.conv(input)        return outputclass C3block(nn.Layer):    '''    This class defines the dilated convolution.    '''    def __init__(self, nIn, nOut, kSize, stride=1, d=1):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param kSize: kernel size        :param stride: optional stride rate for down-sampling        :param d: optional dilation rate        '''        super().__init__()        padding = int((kSize - 1) / 2) * d        if d == 1:            self.conv =nn.Sequential(                nn.Conv2D(nIn, nIn, (kSize, kSize), stride=stride, padding=(padding, padding), groups=nIn, bias_attr=False,                          dilation=d),                nn.Conv2D(nIn, nOut, kernel_size=1, stride=1, bias_attr=False)            )        else:            combine_kernel = 2 * d - 1            self.conv = nn.Sequential(                nn.Conv2D(nIn, nIn, kernel_size=(combine_kernel, 1), stride=stride, padding=(padding - 1, 0),                          groups=nIn, bias_attr=False),                nn.BatchNorm2D(nIn),                nn.PReLU(nIn),                nn.Conv2D(nIn, nIn, kernel_size=(1, combine_kernel), stride=stride, padding=(0, padding - 1),                          groups=nIn, bias_attr=False),                nn.BatchNorm2D(nIn),                nn.Conv2D(nIn, nIn, (kSize, kSize), stride=stride, padding=(padding, padding), groups=nIn, bias_attr=False,                          dilation=d),                nn.Conv2D(nIn, nOut, kernel_size=1, stride=1, bias_attr=False))    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        output = self.conv(input)        return outputclass Down_advancedC3(nn.Layer):    def __init__(self, nIn, nOut, ratio=[2,4,8]):        super().__init__()        n = int(nOut // 3)        n1 = nOut - 3 * n        self.c1 = C(nIn, n, 3, 2)        self.d1 = C3block(n, n+n1, 3, 1, ratio[0])        self.d2 = C3block(n, n, 3, 1, ratio[1])        self.d3 = C3block(n, n, 3, 1, ratio[2])        self.bn = nn.BatchNorm2D(nOut, epsilon=1e-3)        self.act = nn.PReLU(nOut)    def forward(self, input):        output1 = self.c1(input)        d1 = self.d1(output1)        d2 = self.d2(output1)        d3 = self.d3(output1)        combine = paddle.concat([d1, d2, d3], 1)        output = self.bn(combine)        output = self.act(output)        return outputclass AdvancedC3(nn.Layer):    '''    This class defines the ESP block, which is based on the following principle        Reduce ---> Split ---> Transform --> Merge    '''    def __init__(self, nIn, nOut, add=True, ratio=[2,4,8]):        '''        :param nIn: number of input channels        :param nOut: number of output channels        :param add: if true, add a residual connection through identity operation. You can use projection too as                in ResNet paper, but we avoid to use it if the dimensions are not the same because we do not want to                increase the module complexity        '''        super().__init__()        n = int(nOut // 3)        n1 = nOut - 3 * n        self.c1 = C(nIn, n, 1, 1)        self.d1 = C3block(n, n + n1, 3, 1, ratio[0])        self.d2 = C3block(n, n, 3, 1, ratio[1])        self.d3 = C3block(n, n, 3, 1, ratio[2])        # self.d4 = Double_CDilated(n, n, 3, 1, 12)        # self.conv =C(nOut, nOut, 1,1)        self.bn = BR(nOut)        self.add = add    def forward(self, input):        '''        :param input: input feature map        :return: transformed feature map        '''        # reduce        output1 = self.c1(input)        d1 = self.d1(output1)        d2 = self.d2(output1)        d3 = self.d3(output1)        combine = paddle.concat([d1, d2, d3], 1)        if self.add:            combine = paddle.add(input, combine)        output = self.bn(combine)        return outputclass InputProjectionA(nn.Layer):    '''    This class projects the input image to the same spatial dimensions as the feature map.    For example, if the input image is 512 x512 x3 and spatial dimensions of feature map size are 56x56xF, then    this class will generate an output of 56x56x3    '''    def __init__(self, samplingTimes):        '''        :param samplingTimes: The rate at which you want to down-sample the image        '''        super().__init__()        self.pool = nn.LayerList()        for i in range(0, samplingTimes):            # pyramid-based approach for down-sampling            self.pool.append(nn.AvgPool2D(2, stride=2, padding=0))    def forward(self, input):        '''        :param input: Input RGB Image        :return: down-sampled image (pyramid-based approach)        '''        for pool in self.pool:            input = pool(input)        return inputclass ExtremeC3NetCoarse(nn.Layer):    '''    This class defines the ESPNet-C network in the paper    '''    def __init__(self, classes=20, p=5, q=3):        '''        :param classes: number of classes in the dataset. Default is 20 for the cityscapes        :param p: depth multiplier        :param q: depth multiplier        '''        super().__init__()        self.level1 = CBR(3, basic_0, 3, 2)        self.sample1 = InputProjectionA(1)        self.sample2 = InputProjectionA(2)        self.b1 = BR(basic_0 + 3)        self.level2_0 = Down_advancedC3(basic_0 + 3, basic_1, ratio=[1, 2, 3])  # , ratio=[1,2,3]        self.level2 = nn.LayerList()        for i in range(0, p):            self.level2.append(                AdvancedC3(basic_1, basic_1, ratio=[1, 3, 4]))  # , ratio=[1,3,4]        self.b2 = BR(basic_1 * 2 + 3)        self.level3_0 = AdvancedC3(basic_1 * 2 + 3, basic_2, add=False,                                                            ratio=[1, 3, 5])  # , ratio=[1,3,5]        self.level3 = nn.LayerList()        for i in range(0, q):            self.level3.append(AdvancedC3(basic_2, basic_2))        self.b3 = BR(basic_2 * 2)        self.Coarseclassifier = C(basic_2*2, classes, 1, 1)    def forward(self, input):        '''        :param input: Receives the input RGB image        :return: the transformed feature map with spatial dimensions 1/8th of the input image        '''        output0 = self.level1(input)        inp1 = self.sample1(input)        inp2 = self.sample2(input)        output0_cat = self.b1(paddle.concat([output0, inp1], 1))        output1_0 = self.level2_0(output0_cat)  # down-sampled        for i, layer in enumerate(self.level2):            if i == 0:                output1 = layer(output1_0)            else:                output1 = layer(output1)        output1_cat = self.b2(paddle.concat([output1, output1_0, inp2], 1))        output2_0 = self.level3_0(output1_cat)  # down-sampled        for i, layer in enumerate(self.level3):            if i == 0:                output2 = layer(output2_0)            else:                output2 = layer(output2)        output2_cat = self.b3(paddle.concat([output2_0, output2], 1))        classifier = self.Coarseclassifier(output2_cat)        return classifierclass ExtremeC3Net(nn.Layer):    '''    This class defines the ESPNet-C network in the paper    '''    def __init__(self, classes=20, p=5, q=3):        '''        :param classes: number of classes in the dataset. Default is 20 for the cityscapes        :param p: depth multiplier        :param q: depth multiplier        '''        super().__init__()        self.encoder = ExtremeC3NetCoarse(classes, p, q)        # # load the encoder modules        del self.encoder.Coarseclassifier        self.upsample = nn.Sequential(            nn.Conv2D(kernel_size=(1, 1), in_channels=basic_2*2, out_channels=basic_3,bias_attr=False),            nn.BatchNorm2D(basic_3),            nn.Upsample(mode='bilinear', align_corners=True, align_mode=0, scale_factor=2)        )        self.Fine = nn.Sequential(            # nn.Conv2D(kernel_size=3, stride=2, padding=1, in_channels=3, out_channels=basic_3,bias_attr=False),            C(3, basic_3, 3, 2),            AdvancedC3(basic_3, basic_3, add=True),            # nn.BatchNorm2D(basic_3, epsilon=1e-03),        )        self.classifier = nn.Sequential(            BR(basic_3),            nn.Upsample(mode='bilinear', align_corners=True, align_mode=0, scale_factor=2),            nn.Conv2D(kernel_size=(1, 1), in_channels=basic_3, out_channels=classes, bias_attr=False),        )    def forward(self, input):        '''        :param input: Receives the input RGB image        :return: the transformed feature map with spatial dimensions 1/8th of the input image        '''        output0 = self.encoder.level1(input)        inp1 = self.encoder.sample1(input)        inp2 = self.encoder.sample2(input)        output0_cat = self.encoder.b1(paddle.concat([output0, inp1], 1))        output1_0 = self.encoder.level2_0(output0_cat)  # down-sampled        for i, layer in enumerate(self.encoder.level2):            if i == 0:                output1 = layer(output1_0)            else:                output1 = layer(output1)        output1_cat = self.encoder.b2(paddle.concat([output1, output1_0, inp2], 1))        output2_0 = self.encoder.level3_0(output1_cat)  # down-sampled        for i, layer in enumerate(self.encoder.level3):            if i == 0:                output2 = layer(output2_0)            else:                output2 = layer(output2)        output2_cat = self.encoder.b3(paddle.concat([output2_0, output2], 1))        Coarse = self.upsample(output2_cat)        Fine =  self.Fine(input)        classifier = self.classifier(paddle.add(Coarse, Fine))                return classifier

   

以上就是轻量级人像分割模型:SINet 和 ExtremeC3Net的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/42243.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
java数组 怎么多加一个值
上一篇 2025年11月6日 18:16:36
u大师怎么引导修复c盘
下一篇 2025年11月6日 18:18:23

相关推荐

  • Matplotlib 地图中多类型图例的创建与优化

    Matplotlib 地图中多类型图例的创建与优化Matplotlib 地图中多类型图例的创建与优化Matplotlib 地图中多类型图例的创建与优化Matplotlib 地图中多类型图例的创建与优化

    本教程旨在解决matplotlib地图可视化中,如何在一个图例中同时展示颜色块(如区域分类)和自定义标记(如特定兴趣点)的问题。文章详细介绍了当传统`patch`对象无法正确显示标记时,如何利用`matplotlib.lines.line2d`创建标记图例句柄,并将其与颜色块图例句柄合并,从而生成一…

    2026年5月10日 用户投稿
    100
  • Golang JSON序列化:控制敏感字段暴露的最佳实践

    本教程探讨golang中如何高效控制结构体字段在json序列化时的可见性。当需要将包含敏感信息的结构体数组转换为json响应时,通过利用`encoding/json`包提供的结构体标签,特别是`json:”-“`,可以轻松实现对特定字段的忽略,从而避免敏感数据泄露,确保api…

    2026年5月10日
    000
  • 利用海象运算符简化条件赋值:Python教程与最佳实践

    本文旨在探讨Python中海象运算符(:=)在条件赋值场景下的应用。通过对比传统if/else语句与海象运算符,以及条件表达式,分析海象运算符在简化代码、提高可读性方面的优势与局限性。并通过具体示例,展示如何在列表推导式等场景下合理使用海象运算符,同时强调其潜在的复杂性及替代方案,帮助开发者更好地掌…

    2026年5月10日
    100
  • 比特币新手教程 比特币交易平台有哪些

    比特币是一种去中心化的数字货币,基于区块链技术实现点对点交易,具有匿名性、有限发行和不可篡改等特点;新手可通过交易所购买,P2P交易获得比特币,常用平台包括Binance、OKX和Huobi;交易流程包括注册账户、实名认证、绑定支付方式、充值法币并下单购买,可选择市价单或限价单;比特币存储方式有交易…

    2026年5月10日
    000
  • c++中的SFINAE技术是什么_c++模板编程中的SFINAE原理与应用

    SFINAE 是“替换失败不是错误”的原则,指模板实例化时若参数替换导致错误,只要存在其他合法候选,编译器不报错而是继续重载决议。它用于条件启用模板、类型检测等场景,如通过 decltype 或 enable_if 控制函数重载,实现类型特征判断。尽管 C++20 引入 Concepts 简化了部分…

    2026年5月10日
    000
  • Go语言mgo查询构建:深入理解bson.M与日期范围查询的正确实践

    本文旨在解决go语言mgo库中构建复杂查询时,特别是涉及嵌套`bson.m`和日期范围筛选的常见错误。我们将深入剖析`bson.m`的类型特性,解释为何直接索引`interface{}`会导致“invalid operation”错误,并提供一种推荐的、结构清晰的代码重构方案,以确保查询条件能够正确…

    2026年5月10日
    100
  • RichHandler与Rich Progress集成:解决显示冲突的教程

    在使用rich库的`richhandler`进行日志输出并同时使用`progress`组件时,可能会遇到显示错乱或溢出问题。这通常是由于为`richhandler`和`progress`分别创建了独立的`console`实例导致的。解决方案是确保日志处理器和进度条组件共享同一个`console`实例…

    2026年5月10日
    000
  • Golang goroutine与channel调试技巧

    使用go run -race检测数据竞争,结合runtime.NumGoroutine监控协程数量,通过pprof分析阻塞调用栈,利用select超时避免永久阻塞,有效排查goroutine泄漏、死锁和数据竞争问题。 Go语言的goroutine和channel是并发编程的核心,但它们也带来了调试上…

    2026年5月10日
    000
  • 《魔兽世界》将于6月11日开启国服回归技术测试

    《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试

    《%ign%ignore_a_1%re_a_1%》官方宣布,将于6月11日开启国服回归技术测试,时间为7天,并称可以在6月内正式开服,玩家们可以访问官网下载战网客户端并预下载“巫妖王之怒”客户端,技术测试详情见下图。 WordAi WordAI是一个AI驱动的内容重写平台 53 查看详情 以上就是《…

    2026年5月10日 用户投稿
    200
  • 使用 Jupyter Notebook 进行探索性数据分析

    Jupyter Notebook通过单元格实现代码与Markdown结合,支持数据导入(pandas)、清洗(fillna)、探索(matplotlib/seaborn可视化)、统计分析(describe/corr)和特征工程,便于记录与分享分析过程。 Jupyter Notebook 是进行探索性…

    2026年5月10日
    000
  • 如何在HTML中插入表单元素_HTML表单控件与输入类型使用指南

    HTML表单通过标签构建,包含action和method属性定义数据提交目标与方式,常用input类型如text、password、email等适配不同输入需求,配合label、required、placeholder提升可用性,结合textarea、select、button等控件实现完整交互,是…

    2026年5月10日
    100
  • 创建指定大小并填充特定数据的Golang文件教程

    本文将介绍如何使用Golang创建一个指定大小的文件,并用特定数据填充它。我们将使用 `os` 包提供的函数来创建和截断文件,从而实现快速生成大文件的目的。示例代码展示了如何创建一个10MB的文件,并将其填充为全零数据。掌握这些方法,可以方便地在例如日志系统或磁盘队列等场景中,预先创建测试文件或初始…

    2026年5月10日
    000
  • Python命令怎样使用profile分析脚本性能 Python命令性能分析的基础教程

    使用Python的cProfile模块分析脚本性能最直接的方式是通过命令行执行python -m cProfile your_script.py,它会输出每个函数的调用次数、总耗时、累积耗时等关键指标,帮助定位性能瓶颈;为进一步分析,可将结果保存为文件python -m cProfile -o ou…

    2026年5月10日
    000
  • 如何插入查询结果数据_SQL插入Select查询结果方法

    如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法

    使用INSERT INTO…SELECT语句可高效插入数据,通过NOT EXISTS、LEFT JOIN、MERGE语句或唯一约束避免重复;表结构不一致时可通过别名、类型转换、默认值或计算字段处理;结合存储过程可提升可维护性,支持参数化与动态SQL。 将查询结果数据插入到另一个表中,可以…

    2026年5月10日 用户投稿
    300
  • 使用 WebCodecs VideoDecoder 实现精确逐帧回退

    本文档旨在解决在使用 WebCodecs VideoDecoder 进行视频解码时,实现精确逐帧回退的问题。通过比较帧的时间戳与目标帧的时间戳,可以避免渲染中间帧,从而提高用户体验。本文将提供详细的解决方案和示例代码,帮助开发者实现精确的视频帧控制。 在使用 WebCodecs VideoDecod…

    2026年5月10日
    000
  • Debian Copilot的社区活跃度如何

    debian copilot是codeberg社区维护的ai助手,旨在为debian用户提供服务。尽管搜索结果中没有直接提供关于debian copilot社区支持活跃度的具体数据,但我们可以通过debian社区的整体活跃度和特点来推断其活跃性。 Debian社区的一般情况: Debian拥有详尽的…

    2026年5月10日
    000
  • Discord.py 交互按钮超时与持久化解决方案

    本教程旨在解决Discord.py中交互按钮在一段时间后出现“This Interaction Failed”错误的问题。我们将深入探讨视图(View)的超时机制,并提供通过正确设置timeout参数以及利用bot.add_view()方法实现按钮持久化的具体方案,确保您的机器人交互功能稳定可靠,即…

    2026年5月10日
    000
  • Python递归函数追踪与性能考量:以序列打印为例

    本文深入探讨了Python中一种递归打印序列元素的方法,并着重演示了如何通过引入缩进参数来有效追踪递归函数的执行流程和参数变化。通过实际代码示例,文章揭示了递归调用可能带来的潜在性能开销,特别是对调用栈空间的需求,以及Python默认递归深度限制可能导致的错误,为读者提供了理解和优化递归算法的实用见…

    2026年5月10日
    000
  • python中zip函数详解 python多序列压缩zip函数应用场景

    zip函数的应用场景包括:1) 同时遍历多个序列,2) 合并多个列表的数据,3) 数据分析和科学计算中的元素运算,4) 处理csv文件,5) 性能优化。zip函数是一个强大的工具,能够简化代码并提高处理多个序列时的效率。 在Python中,zip函数是一个非常有用的工具,它能够将多个可迭代对象打包成…

    2026年5月10日
    000
  • JavaScript 动态菜单点击高亮效果实现教程

    本教程详细介绍了如何使用 JavaScript 实现动态菜单的点击高亮功能。通过事件委托和状态管理,当用户点击菜单项时,被点击项会高亮显示(绿色),同时其他菜单项恢复默认样式(白色)。这种方法避免了不必要的DOM操作,提高了性能和代码可维护性,确保了无论点击方向如何,功能都能稳定运行。 动态菜单高亮…

    2026年5月10日
    200

发表回复

登录后才能评论
关注微信