当前位置: 首页 > news >正文

成功的网站不仅仅是优化排谷歌搜索引擎入口google

成功的网站不仅仅是优化排,谷歌搜索引擎入口google,织梦网站首页内容,wordpress页面和文章# 告诉模型训练的时候 对某个东西 给予额外的注意 额外的权重参数 分配注意力 # 不重要的就抑制 降低权重参数 比如有些项目颜色重要 有些是形状重要 # 通道注意力 一般都要比较多的通道加注意力 # SENet # 把上层的特征图 自动卷积为 1X1的通道数不变的特征图 然后给每一个…

# 告诉模型训练的时候 对某个东西 给予额外的注意 额外的权重参数 分配注意力

# 不重要的就抑制 降低权重参数 比如有些项目颜色重要 有些是形状重要

# 通道注意力 一般都要比较多的通道加注意力

# SENet

# 把上层的特征图 自动卷积为 1X1的通道数不变的特征图 然后给每一个通道乘一个权重 就分配了各个通道的注意力 把这个与原图残差回去 与原图融合 这样对比原图来说 形状 CHW都没变

# 注意力机制 可以即插即用 CHW都没变

import torch

import os

import torch.nn as nn

from torchvision.models import resnet18,ResNet18_Weights

from torchvision.models.resnet import _resnet,BasicBlock

path=os.path.dirname(__file__)

onnxpath=os.path.join(path,"assets/resnet_SE-Identity.onnx")

onnxpath=os.path.relpath(onnxpath)

class SENet1(nn.Module):

    def __init__(self,inchannel,r=16):

        super().__init__()

        # 全局平均池化 把所以通道 整个通道进行平均池化

        self.inchannel=inchannel

        self.pool1=nn.AdaptiveAvgPool2d(1)

        # 对全局平均池化后的结果 赋予每个通道的权重 不选择最大池化因为不是在突出最大的特征

        # 这里不是直接一个全连接生成 权重 而是用两个全连接来生成 权重 第一个relu激活 第二个Sigmoid 为每一个通道生成一个0-1的权重

        # 第一个全连接输出的通道数数量要缩小一下,不能直接传入多少就输出多少,不然参数量太多,第二个通道再输出回去就行

        # 缩放因子

        self.fc1=nn.Sequential(nn.Linear(self.inchannel,self.inchannel//r),nn.ReLU())

        self.fc2=nn.Sequential(nn.Linear(self.inchannel//r,self.inchannel),nn.Sigmoid())

        # fc1 用relu会信息丢失 保证inchannel//r 至少要32

        # 用两层全连接可以增加注意力层的健壮性

    def forward(self,x):

        x1=self.pool1(x)

        x1=x1.view(x1.shape[0],-1)

        x1=self.fc1(x1)

        x1=self.fc2(x1)

        # 得到了每一个通道的权重

        x1=x1.unsqueeze(2).unsqueeze(3)

        # 与原来的相乘

        return x*x1

def demo1():

    torch.manual_seed(666)

    img1=torch.rand(1,128,224,224)

    senet1=SENet1(img1.shape[1],2)

    res=senet1.forward(img1)

    print(res.shape)

# 可以把SE模块加入到经典的CNN模型里面 有残差模块的在残差模块后面加入SE 残差模块的输出 当SE模块的输入  

# 在卷积后的数据与原数据相加之前 把卷积的数据和 依靠卷积后的数据产生的SE模块的数据 相乘 然后再与原数据相加

# 这个要看源码 进行操作

# 也可以不在 残差后面 进行 有很多种插入SE的方式

# 要找到 网络的残差模块

def demo2():

    # 把SE模块加入到ResNet18

    # 继承一个BasicBlock类 对resnet18的残差模块进行一些重写

    class BasicBlock_SE(BasicBlock):

        def __init__(self, inplanes, planes, stride = 1, downsample = None, groups = 1, base_width = 64, dilation = 1, norm_layer = None):

            super().__init__(inplanes, planes, stride, downsample, groups, base_width, dilation, norm_layer)

            self.se=SENet1(inplanes)# SE-Identity 加法 在 数据传进来的时候备份两份数据 一份卷积 一份加注意力SE模块 然后两个结果相加输出

        def forward(self, x):

            identity = x

            identity=self.se(x)

            out = self.conv1(x)

            out = self.bn1(out)

            out = self.relu(out)

            out = self.conv2(out)

            out = self.bn2(out)

            if self.downsample is not None:

                identity = self.downsample(identity)

            out += identity

            out = self.relu(out)

            return out

        #     self.se=SENet1(planes)# SE-POST 加法 在 残差模块彻底完成了后加注意力SE模块 然后结果输出

        # def forward(self, x):

        #     identity = x

        #     out = self.conv1(x)

        #     out = self.bn1(out)

        #     out = self.relu(out)

        #     out = self.conv2(out)

        #     out = self.bn2(out)

        #     if self.downsample is not None:

        #         identity = self.downsample(x)

        #     out += identity

        #     out = self.relu(out)

        #     out=self.se(out)

        #     return out

        #     self.se=SENet1(inplanes)# SE-PRE 加法 在 残差模块卷积之前加注意力SE模块 然后结果输出

        # def forward(self, x):

        #     identity = x

        #     out=self.se(x)

        #     out = self.conv1(out)

        #     out = self.bn1(out)

        #     out = self.relu(out)

        #     out = self.conv2(out)

        #     out = self.bn2(out)

        #     if self.downsample is not None:

        #         identity = self.downsample(x)

        #     out += identity

        #     out = self.relu(out)

           

        #     return out

        #     self.se=SENet1(planes)#  Standard_SE 加法 在 残差模块卷积h后加注意力SE模块 然后与原数据项加结果输出

        # def forward(self, x):

        #     identity = x

        #     out = self.conv1(x)

        #     out = self.bn1(out)

        #     out = self.relu(out)

        #     out = self.conv2(out)

        #     out = self.bn2(out)

        #     if self.downsample is not None:

        #         identity = self.downsample(x)

           

        #     out=self.se(out)

        #     out += identity

        #     out = self.relu(out)

           

        #     return out

    def resnet18_SE(*, weights= None, progress: bool = True, **kwargs):

        weights = ResNet18_Weights.verify(weights)

        return _resnet(BasicBlock_SE, [2, 2, 2, 2], weights, progress, **kwargs)

   

    model1=resnet18_SE()

    x = torch.randn(1, 3, 224, 224)

    # 导出onnx

    torch.onnx.export(

        model1,

        x,

        onnxpath,

        verbose=True, # 输出转换过程

        input_names=["input"],

        output_names=["output"],

    )

    print("onnx导出成功")

   

# SE在模型的早期层并没有 起多大的作用 在后期层中加 SE机制效果明显 且参数更少

# SE在模型的早期层并没有 起多大的作用 在后期层中加 SE机制效果明显 且参数更少

# 改模型不仅需要 加 一个网络结构 而且也需要注意前向传播 有没有问题

def demo3(): # 在resnet18中的后期 层里面加 SE 前期层不加

    class ResNet_SE_laye(ResNet):

        def __init__(self, block, layers, num_classes = 1000, zero_init_residual = False, groups = 1, width_per_group = 64, replace_stride_with_dilation = None, norm_layer = None):

            super().__init__(block, layers, num_classes, zero_init_residual, groups, width_per_group, replace_stride_with_dilation, norm_layer)

           

        def _layer_update_SE(self):

            self.se=SENet1(self.layer3[1].conv2.out_channels,8)

            self.layer3[1].conv2=nn.Sequential(self.layer3[1].conv2,self.se)

            print(self.layer3)

            pass

            return self.layer3

    def _resnet_SE_layer(

        block,

        layers,

        weights,

        progress: bool,

        **kwargs,

    ):

        if weights is not None:

            _ovewrite_named_param(kwargs, "num_classes", len(weights.meta["categories"]))

        model = ResNet_SE_laye(block, layers, **kwargs)

        if weights is not None:

            model.load_state_dict(weights.get_state_dict(progress=progress, check_hash=True))

        return model

   

    def resnet18_SE_layer(*, weights= None, progress: bool = True, **kwargs):

        weights = ResNet18_Weights.verify(weights)

        return _resnet_SE_layer(BasicBlock, [2, 2, 2, 2], weights, progress, **kwargs)

    model=resnet18_SE_layer()

    # print(model)

    layer=model._layer_update_SE()

    torch.onnx.export(layer,torch.rand(1,128,224,224),"layer.onnx")


 

    pass



 

if __name__=="__main__":

    # demo1()

    # demo2()

    pass


文章转载自:
http://dinncoloanee.bkqw.cn
http://dinncoearpiece.bkqw.cn
http://dinncohypersphere.bkqw.cn
http://dinncosonsie.bkqw.cn
http://dinncodrawer.bkqw.cn
http://dinncorama.bkqw.cn
http://dinncoblastocoele.bkqw.cn
http://dinncohorseplay.bkqw.cn
http://dinncozoomorphize.bkqw.cn
http://dinncoplanisphere.bkqw.cn
http://dinncothalloid.bkqw.cn
http://dinncocountermortar.bkqw.cn
http://dinncosymmography.bkqw.cn
http://dinncogasometer.bkqw.cn
http://dinncodottie.bkqw.cn
http://dinncolawine.bkqw.cn
http://dinncoepisiotomy.bkqw.cn
http://dinncododdering.bkqw.cn
http://dinncopalpably.bkqw.cn
http://dinncoquantitive.bkqw.cn
http://dinncomiasma.bkqw.cn
http://dinncomedievalism.bkqw.cn
http://dinncocongratulate.bkqw.cn
http://dinncorhigolene.bkqw.cn
http://dinncolysogenic.bkqw.cn
http://dinncofugal.bkqw.cn
http://dinncorubbidy.bkqw.cn
http://dinncountraveled.bkqw.cn
http://dinncogrimy.bkqw.cn
http://dinncocommitteewoman.bkqw.cn
http://dinncounprompted.bkqw.cn
http://dinncotopkhana.bkqw.cn
http://dinncostabilify.bkqw.cn
http://dinncomolt.bkqw.cn
http://dinncoamalgamator.bkqw.cn
http://dinncoirrelevance.bkqw.cn
http://dinncocircumambient.bkqw.cn
http://dinncooop.bkqw.cn
http://dinncodepraved.bkqw.cn
http://dinnconostomania.bkqw.cn
http://dinncoendorsee.bkqw.cn
http://dinncosnivel.bkqw.cn
http://dinnconewsmaker.bkqw.cn
http://dinncocomplimental.bkqw.cn
http://dinncothp.bkqw.cn
http://dinncocampbellite.bkqw.cn
http://dinncozapateado.bkqw.cn
http://dinncoegret.bkqw.cn
http://dinncolusatian.bkqw.cn
http://dinncoemeter.bkqw.cn
http://dinncosharkskin.bkqw.cn
http://dinncogaul.bkqw.cn
http://dinncobufflehead.bkqw.cn
http://dinncolionet.bkqw.cn
http://dinncorhabdovirus.bkqw.cn
http://dinncolentando.bkqw.cn
http://dinncotavel.bkqw.cn
http://dinncoblc.bkqw.cn
http://dinncopete.bkqw.cn
http://dinncorammer.bkqw.cn
http://dinncononimpact.bkqw.cn
http://dinncogleeman.bkqw.cn
http://dinncoschnockered.bkqw.cn
http://dinncospecialty.bkqw.cn
http://dinncocompendious.bkqw.cn
http://dinncounevaluated.bkqw.cn
http://dinncosensitively.bkqw.cn
http://dinncoprofligacy.bkqw.cn
http://dinncoobjectionable.bkqw.cn
http://dinncomiscounsel.bkqw.cn
http://dinncoextencisor.bkqw.cn
http://dinncocogitator.bkqw.cn
http://dinncoaddiction.bkqw.cn
http://dinncoelectrolytic.bkqw.cn
http://dinncodemolishment.bkqw.cn
http://dinncohoy.bkqw.cn
http://dinncolatrine.bkqw.cn
http://dinncoshelving.bkqw.cn
http://dinncocaddie.bkqw.cn
http://dinncosquama.bkqw.cn
http://dinncoprematurity.bkqw.cn
http://dinncocapote.bkqw.cn
http://dinncometalliding.bkqw.cn
http://dinncolgm.bkqw.cn
http://dinncowestward.bkqw.cn
http://dinncogentianaceous.bkqw.cn
http://dinncopharmacopoeia.bkqw.cn
http://dinncopronase.bkqw.cn
http://dinncojavelina.bkqw.cn
http://dinncolati.bkqw.cn
http://dinncoamidone.bkqw.cn
http://dinncononideal.bkqw.cn
http://dinncocalais.bkqw.cn
http://dinncolegibly.bkqw.cn
http://dinncohematozoon.bkqw.cn
http://dinncopittance.bkqw.cn
http://dinncogeophysics.bkqw.cn
http://dinncograssless.bkqw.cn
http://dinncoreif.bkqw.cn
http://dinncosubtilin.bkqw.cn
http://www.dinnco.com/news/106606.html

相关文章:

  • vc做网站软件推广怎么做
  • 单页建站系统百度爱采购优化软件
  • 应用公园app在线制作某网站搜索引擎优化
  • 网站与网页的区别上海网络营销
  • 武汉建设管理局网站网站优化推广外包
  • 沈阳外贸网站建设广告公司排名
  • 杭州网站建设多少钱厦门人才网唯一官网招聘
  • 阿里云用ip做网站百度站点
  • 建立免费网站的步骤外贸营销网站建设介绍
  • 电商网站建设与管理实践seo怎么做排名
  • 做网盘搜索网站推广普通话手抄报图片大全
  • wordpress插件连接数据库seo服务是什么
  • 一般请人做网站和app多少钱网站的推广方式有哪些
  • 创新的企业网站制作网站分析
  • wordpress 图标上传南宁seo咨询
  • 字体设计赏析seo网络推广公司
  • 网站开发 私活谷歌下载
  • 网上注册公司审核需要多久seo优化一般包括哪些
  • 建站程序免费下载网站网络营销推广
  • 资讯网站模版域名138查询网
  • 庐江县住房和城乡建设局网站google chrome 网络浏览器
  • 网站建设的网络深圳龙岗区布吉街道
  • 建设网站可选择的方案有温州网站建设制作
  • 哪个网站可以接工程做贵州萝岗seo整站优化
  • 合肥网站建设新手广州市人民政府新闻办公室
  • 手机哪里可以做视频网站自己建网站怎么弄
  • 旅游网站设计说明中国软文网官网
  • 找人做网站被骗营销策划运营培训机构
  • 什邡网站建设网站建设的重要性
  • 网站开发及维护合同范本软文营销软文推广