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

网上商城用wordpress杭州seo推广服务

网上商城用wordpress,杭州seo推广服务,微信网站模版,网站建设ppt简介open-cd 目录 open-cd1.安装2.源码结构分析主干网络1.1 主干网络类2.neck2.Decoder3.测试模型6. changer主干网络 总结 该开源库基于#xff1a; mmcv mmseg mmdet mmengine 1.安装 在安装过程中遇到的问题#xff1a; 1.pytorch版本问题#xff0c;open-cd采用的mmcv版本比…open-cd 目录 open-cd1.安装2.源码结构分析主干网络1.1 主干网络类2.neck2.Decoder3.测试模型6. changer主干网络 总结 该开源库基于 mmcv mmseg mmdet mmengine 1.安装 在安装过程中遇到的问题 1.pytorch版本问题open-cd采用的mmcv版本比较低建议安装2.3以下版本pytorch太高了mmcv可能不太适配先安装pytorch在安装mmcv我在安装时用的版本 pytorch 2.1.2 py3.9_cuda12.1_cudnn8_0 pytorch mmcv 2.1.0 pypi_0 pypimmcv安装方式 该方式同样适用于解决 note: This error originates from a subprocess, and is likely not a problem with pip.ERROR: Failed building wheel for mmcvRunning setup.py clean for mmcv Failed to build mmcv ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (mmcv)参考 之后参照博主的的安装步骤安装opencd的开原文件即可(建议安装源文件直接 opencd第三方包形式后期不方便调试) # Install OpenMMLab Toolkits as Python packages pip install -U openmim mim install mmengine mim install mmpretrain1.0.0rc7 pip install mmsegmentation1.2.2 pip install mmdet3.0.0# Install Opencd git clone https://github.com/likyoo/open-cd.git cd open-cd pip install -v -e .2.源码结构分析 该库主要的文件时1.config参数文件2.opencd模型架构文件3.训练推理分析工具4.mmlab 这里主要介绍2.opencd模型框架文件,文件下包含 其中model文件夹下包含模型结构基础文件 变化检测大致遵循语义分割的编码结构、neck结构、以及解码结构。 如果使用过mmsegmentation机会发现backbone中存放着雨参数文件对应的主干网络相应的是neckdecoder这里changer_detector里面是主要的模型架构如Encoder-Decoder。 open-cd与mmseg这类参数化的文件非常适合进行模型复现或者进行工程化应用但对一些科研小白特别是非计算机专业的科研小白需要改进网络就不太友好这里直观的作用下模型架构的组合使用方便大家理解和魔改~~别越看越迷糊就行 这里以changerformer-mitb0为例 前提是已完成open-cd的安装官方issue 下面内容摘自opencd/model/ 删除每个类前面的注册表装饰器 MODELS.register_module()报错提示类已注册 主干网络 # Copyright (c) OpenMMLab. All rights reserved. import math import warningsimport torch import torch.nn as nn import torch.utils.checkpoint as cp from mmcv.cnn import Conv2d, build_activation_layer, build_norm_layer from mmcv.cnn.bricks.drop import build_dropout from mmcv.cnn.bricks.transformer import MultiheadAttention from mmengine.model import BaseModule, ModuleList, Sequential from mmengine.model.weight_init import (constant_init, normal_init,trunc_normal_init)from mmseg.registry import MODELS ## 下面两个依赖在opencd/model/utils中可以找到 from .embed import PatchEmbed from .shape_convert import nchw_to_nlc, nlc_to_nchwclass MixFFN(BaseModule):An implementation of MixFFN of Segformer.The differences between MixFFN FFN:1. Use 1X1 Conv to replace Linear layer.2. Introduce 3X3 Conv to encode positional information.Args:embed_dims (int): The feature dimension. Same asMultiheadAttention. Defaults: 256.feedforward_channels (int): The hidden dimension of FFNs.Defaults: 1024.act_cfg (dict, optional): The activation config for FFNs.Default: dict(typeReLU)ffn_drop (float, optional): Probability of an element to bezeroed in FFN. Default 0.0.dropout_layer (obj:ConfigDict): The dropout_layer usedwhen adding the shortcut.init_cfg (obj:mmcv.ConfigDict): The Config for initialization.Default: None.def __init__(self,embed_dims,feedforward_channels,act_cfgdict(typeGELU),ffn_drop0.,dropout_layerNone,init_cfgNone):super().__init__(init_cfg)self.embed_dims embed_dimsself.feedforward_channels feedforward_channelsself.act_cfg act_cfgself.activate build_activation_layer(act_cfg)in_channels embed_dimsfc1 Conv2d(in_channelsin_channels,out_channelsfeedforward_channels,kernel_size1,stride1,biasTrue)# 3x3 depth wise conv to provide positional encode informationpe_conv Conv2d(in_channelsfeedforward_channels,out_channelsfeedforward_channels,kernel_size3,stride1,padding(3 - 1) // 2,biasTrue,groupsfeedforward_channels)fc2 Conv2d(in_channelsfeedforward_channels,out_channelsin_channels,kernel_size1,stride1,biasTrue)drop nn.Dropout(ffn_drop)layers [fc1, pe_conv, self.activate, drop, fc2, drop]self.layers Sequential(*layers)self.dropout_layer build_dropout(dropout_layer) if dropout_layer else torch.nn.Identity()def forward(self, x, hw_shape, identityNone):out nlc_to_nchw(x, hw_shape)out self.layers(out)out nchw_to_nlc(out)if identity is None:identity xreturn identity self.dropout_layer(out)class EfficientMultiheadAttention(MultiheadAttention):An implementation of Efficient Multi-head Attention of Segformer.This module is modified from MultiheadAttention which is a module frommmcv.cnn.bricks.transformer.Args:embed_dims (int): The embedding dimension.num_heads (int): Parallel attention heads.attn_drop (float): A Dropout layer on attn_output_weights.Default: 0.0.proj_drop (float): A Dropout layer after nn.MultiheadAttention.Default: 0.0.dropout_layer (obj:ConfigDict): The dropout_layer usedwhen adding the shortcut. Default: None.init_cfg (obj:mmcv.ConfigDict): The Config for initialization.Default: None.batch_first (bool): Key, Query and Value are shape of(batch, n, embed_dim)or (n, batch, embed_dim). Default: False.qkv_bias (bool): enable bias for qkv if True. Default True.norm_cfg (dict): Config dict for normalization layer.Default: dict(typeLN).sr_ratio (int): The ratio of spatial reduction of Efficient Multi-headAttention of Segformer. Default: 1.def __init__(self,embed_dims,num_heads,attn_drop0.,proj_drop0.,dropout_layerNone,init_cfgNone,batch_firstTrue,qkv_biasFalse,norm_cfgdict(typeLN),sr_ratio1):super().__init__(embed_dims,num_heads,attn_drop,proj_drop,dropout_layerdropout_layer,init_cfginit_cfg,batch_firstbatch_first,biasqkv_bias)self.sr_ratio sr_ratioif sr_ratio 1:self.sr Conv2d(in_channelsembed_dims,out_channelsembed_dims,kernel_sizesr_ratio,stridesr_ratio)# The ret[0] of build_norm_layer is norm name.self.norm build_norm_layer(norm_cfg, embed_dims)[1]# handle the BC-breaking from https://github.com/open-mmlab/mmcv/pull/1418 # noqafrom mmseg import digit_version, mmcv_versionif mmcv_version digit_version(1.3.17):warnings.warn(The legacy version of forward function inEfficientMultiheadAttention is deprecated inmmcv1.3.17 and will no longer support in thefuture. Please upgrade your mmcv.)self.forward self.legacy_forwarddef forward(self, x, hw_shape, identityNone):x_q xif self.sr_ratio 1:x_kv nlc_to_nchw(x, hw_shape)x_kv self.sr(x_kv)x_kv nchw_to_nlc(x_kv)x_kv self.norm(x_kv)else:x_kv xif identity is None:identity x_q# Because the dataflow(key, query, value) of# torch.nn.MultiheadAttention is (num_query, batch,# embed_dims), We should adjust the shape of dataflow from# batch_first (batch, num_query, embed_dims) to num_query_first# (num_query ,batch, embed_dims), and recover attn_output# from num_query_first to batch_first.if self.batch_first:x_q x_q.transpose(0, 1)x_kv x_kv.transpose(0, 1)out self.attn(queryx_q, keyx_kv, valuex_kv)[0]if self.batch_first:out out.transpose(0, 1)return identity self.dropout_layer(self.proj_drop(out))def legacy_forward(self, x, hw_shape, identityNone):multi head attention forward in mmcv version 1.3.17.x_q xif self.sr_ratio 1:x_kv nlc_to_nchw(x, hw_shape)x_kv self.sr(x_kv)x_kv nchw_to_nlc(x_kv)x_kv self.norm(x_kv)else:x_kv xif identity is None:identity x_q# need_weightsTrue will let nn.MultiHeadAttention# return attn_output, attn_output_weights.sum(dim1) / num_heads# The attn_output_weights.sum(dim1) may cause cuda error. So, we set# need_weightsFalse to ignore attn_output_weights.sum(dim1).# This issue - https://github.com/pytorch/pytorch/issues/37583 report# the error that large scale tensor sum operation may cause cuda error.out self.attn(queryx_q, keyx_kv, valuex_kv, need_weightsFalse)[0]return identity self.dropout_layer(self.proj_drop(out))class TransformerEncoderLayer(BaseModule):Implements one encoder layer in Segformer.Args:embed_dims (int): The feature dimension.num_heads (int): Parallel attention heads.feedforward_channels (int): The hidden dimension for FFNs.drop_rate (float): Probability of an element to be zeroed.after the feed forward layer. Default 0.0.attn_drop_rate (float): The drop out rate for attention layer.Default 0.0.drop_path_rate (float): stochastic depth rate. Default 0.0.qkv_bias (bool): enable bias for qkv if True.Default: True.act_cfg (dict): The activation config for FFNs.Default: dict(typeGELU).norm_cfg (dict): Config dict for normalization layer.Default: dict(typeLN).batch_first (bool): Key, Query and Value are shape of(batch, n, embed_dim)or (n, batch, embed_dim). Default: False.init_cfg (dict, optional): Initialization config dict.Default:None.sr_ratio (int): The ratio of spatial reduction of Efficient Multi-headAttention of Segformer. Default: 1.with_cp (bool): Use checkpoint or not. Using checkpoint will savesome memory while slowing down the training speed. Default: False.def __init__(self,embed_dims,num_heads,feedforward_channels,drop_rate0.,attn_drop_rate0.,drop_path_rate0.,qkv_biasTrue,act_cfgdict(typeGELU),norm_cfgdict(typeLN),batch_firstTrue,sr_ratio1,with_cpFalse):super().__init__()# The ret[0] of build_norm_layer is norm name.self.norm1 build_norm_layer(norm_cfg, embed_dims)[1]self.attn EfficientMultiheadAttention(embed_dimsembed_dims,num_headsnum_heads,attn_dropattn_drop_rate,proj_dropdrop_rate,dropout_layerdict(typeDropPath, drop_probdrop_path_rate),batch_firstbatch_first,qkv_biasqkv_bias,norm_cfgnorm_cfg,sr_ratiosr_ratio)# The ret[0] of build_norm_layer is norm name.self.norm2 build_norm_layer(norm_cfg, embed_dims)[1]self.ffn MixFFN(embed_dimsembed_dims,feedforward_channelsfeedforward_channels,ffn_dropdrop_rate,dropout_layerdict(typeDropPath, drop_probdrop_path_rate),act_cfgact_cfg)self.with_cp with_cpdef forward(self, x, hw_shape):def _inner_forward(x):x self.attn(self.norm1(x), hw_shape, identityx)x self.ffn(self.norm2(x), hw_shape, identityx)return xif self.with_cp and x.requires_grad:x cp.checkpoint(_inner_forward, x)else:x _inner_forward(x)return x# MODELS.register_module() class MixVisionTransformer(BaseModule):The backbone of Segformer.This backbone is the implementation of SegFormer: Simple andEfficient Design for Semantic Segmentation withTransformers https://arxiv.org/abs/2105.15203_.Args:in_channels (int): Number of input channels. Default: 3.embed_dims (int): Embedding dimension. Default: 768.num_stags (int): The num of stages. Default: 4.num_layers (Sequence[int]): The layer number of each transformer encodelayer. Default: [3, 4, 6, 3].num_heads (Sequence[int]): The attention heads of each transformerencode layer. Default: [1, 2, 4, 8].patch_sizes (Sequence[int]): The patch_size of each overlapped patchembedding. Default: [7, 3, 3, 3].strides (Sequence[int]): The stride of each overlapped patch embedding.Default: [4, 2, 2, 2].sr_ratios (Sequence[int]): The spatial reduction rate of eachtransformer encode layer. Default: [8, 4, 2, 1].out_indices (Sequence[int] | int): Output from which stages.Default: (0, 1, 2, 3).mlp_ratio (int): ratio of mlp hidden dim to embedding dim.Default: 4.qkv_bias (bool): Enable bias for qkv if True. Default: True.drop_rate (float): Probability of an element to be zeroed.Default 0.0attn_drop_rate (float): The drop out rate for attention layer.Default 0.0drop_path_rate (float): stochastic depth rate. Default 0.0norm_cfg (dict): Config dict for normalization layer.Default: dict(typeLN)act_cfg (dict): The activation config for FFNs.Default: dict(typeGELU).pretrained (str, optional): model pretrained path. Default: None.init_cfg (dict or list[dict], optional): Initialization config dict.Default: None.with_cp (bool): Use checkpoint or not. Using checkpoint will savesome memory while slowing down the training speed. Default: False.def __init__(self,in_channels3,embed_dims64,num_stages4,num_layers[3, 4, 6, 3],num_heads[1, 2, 4, 8],patch_sizes[7, 3, 3, 3],strides[4, 2, 2, 2],sr_ratios[8, 4, 2, 1],out_indices(0, 1, 2, 3),mlp_ratio4,qkv_biasTrue,drop_rate0.,attn_drop_rate0.,drop_path_rate0.,act_cfgdict(typeGELU),norm_cfgdict(typeLN, eps1e-6),pretrainedNone,init_cfgNone,with_cpFalse):super().__init__(init_cfginit_cfg)assert not (init_cfg and pretrained), \init_cfg and pretrained cannot be set at the same timeif isinstance(pretrained, str):warnings.warn(DeprecationWarning: pretrained is deprecated, please use init_cfg instead)self.init_cfg dict(typePretrained, checkpointpretrained)elif pretrained is not None:raise TypeError(pretrained must be a str or None)self.embed_dims embed_dimsself.num_stages num_stagesself.num_layers num_layersself.num_heads num_headsself.patch_sizes patch_sizesself.strides stridesself.sr_ratios sr_ratiosself.with_cp with_cpassert num_stages len(num_layers) len(num_heads) \ len(patch_sizes) len(strides) len(sr_ratios)self.out_indices out_indicesassert max(out_indices) self.num_stages# transformer encoderdpr [x.item()for x in torch.linspace(0, drop_path_rate, sum(num_layers))] # stochastic num_layer decay rulecur 0self.layers ModuleList()for i, num_layer in enumerate(num_layers):embed_dims_i embed_dims * num_heads[i]patch_embed PatchEmbed(in_channelsin_channels,embed_dimsembed_dims_i,kernel_sizepatch_sizes[i],stridestrides[i],paddingpatch_sizes[i] // 2,norm_cfgnorm_cfg)layer ModuleList([TransformerEncoderLayer(embed_dimsembed_dims_i,num_headsnum_heads[i],feedforward_channelsmlp_ratio * embed_dims_i,drop_ratedrop_rate,attn_drop_rateattn_drop_rate,drop_path_ratedpr[cur idx],qkv_biasqkv_bias,act_cfgact_cfg,norm_cfgnorm_cfg,with_cpwith_cp,sr_ratiosr_ratios[i]) for idx in range(num_layer)])in_channels embed_dims_i# The ret[0] of build_norm_layer is norm name.norm build_norm_layer(norm_cfg, embed_dims_i)[1]self.layers.append(ModuleList([patch_embed, layer, norm]))cur num_layerdef init_weights(self):if self.init_cfg is None:for m in self.modules():if isinstance(m, nn.Linear):trunc_normal_init(m, std.02, bias0.)elif isinstance(m, nn.LayerNorm):constant_init(m, val1.0, bias0.)elif isinstance(m, nn.Conv2d):fan_out m.kernel_size[0] * m.kernel_size[1] * m.out_channelsfan_out // m.groupsnormal_init(m, mean0, stdmath.sqrt(2.0 / fan_out), bias0)else:super().init_weights()def forward(self, x):outs []for i, layer in enumerate(self.layers):x, hw_shape layer[0](x)for block in layer[1]:x block(x, hw_shape)x layer[2](x)x nlc_to_nchw(x, hw_shape)if i in self.out_indices:outs.append(x)return outs 1.1 主干网络类 from torch import nn from CDmodel.ly_utils.MixVisionTransformer import MixVisionTransformerclass backbone(nn.Module):def __init__(self):super(backbone,self).__init__()self.model MixVisionTransformer(in_channels3,embed_dims32,num_stages4,num_layers[2, 2, 2, 2],num_heads[1, 2, 5, 8],patch_sizes[7, 3, 3, 3],sr_ratios[8, 4, 2, 1],out_indices(0, 1, 2, 3),mlp_ratio4,qkv_biasTrue,drop_rate0.0,attn_drop_rate0.0,drop_path_rate0.1)def forward(self,x1):return self.model.forward(x1)2.neck # Copyright (c) Open-CD. All rights reserved. import torch from torch import nn from opencd.registry import MODELSclass FeatureFusionNeck(nn.Module):Feature Fusion Neck.Args:policy (str): The operation to fuse features. candidatesare concat, sum, diff and Lp_distance.in_channels (Sequence(int)): Input channels.channels (int): Channels after modules, before conv_seg.out_indices (tuple[int]): Output from which layer.def __init__(self,policy concat,in_channelsNone,channelsNone,out_indices(0, 1, 2, 3)):super(FeatureFusionNeck,self).__init__()self.policy policyself.in_channels in_channelsself.channels channelsself.out_indices out_indicesstaticmethoddef fusion(x1, x2, policy):Specify the form of feature fusion_fusion_policies [concat, sum, diff, abs_diff]assert policy in _fusion_policies, The fusion policies {} are \supported.format(_fusion_policies)if policy concat:x torch.cat([x1, x2], dim1)elif policy sum:x x1 x2elif policy diff:x x2 - x1elif policy abs_diff:x torch.abs(x1 - x2)return xdef forward(self, x1, x2):Forward function.assert len(x1) len(x2), The features x1 and x2 from the \backbone should be of equal lengthouts []for i in range(len(x1)):out self.fusion(x1[i], x2[i], self.policy)outs.append(out)outs [outs[i] for i in self.out_indices]return tuple(outs)2.Decoder changerformer解码头采用的是’mmseg.SegformerHead’ 这个得在mmsegmentation/mmseg/model/head里面找 找到后删除损失函数、预测推理保留下面部分内容 # Copyright (c) OpenMMLab. All rights reserved. import warnings from abc import ABCMeta, abstractmethod from typing import List, Tuple from mmcv.cnn import ConvModule import torch import torch.nn as nn from mmengine.model import BaseModule from torch import Tensorfrom mmseg.registry import MODELS from mmseg.structures import build_pixel_sampler from mmseg.utils import ConfigType, SampleList # from ..losses import accuracy from layer.resize import resizeclass BaseDecodeHead(nn.Module):Base class for BaseDecodeHead.1. The init_weights method is used to initialize decode_headsmodel parameters. After segmentor initialization, init_weightsis triggered when segmentor.init_weights() is called externally.2. The loss method is used to calculate the loss of decode_head,which includes two steps: (1) the decode_head model performs forwardpropagation to obtain the feature maps (2) The loss_by_feat methodis called based on the feature maps to calculate the loss... code:: textloss(): forward() - loss_by_feat()3. The predict method is used to predict segmentation results,which includes two steps: (1) the decode_head model performs forwardpropagation to obtain the feature maps (2) The predict_by_feat methodis called based on the feature maps to predict segmentation resultsincluding post-processing... code:: textpredict(): forward() - predict_by_feat()Args:in_channels (int|Sequence[int]): Input channels.channels (int): Channels after modules, before conv_seg.num_classes (int): Number of classes.out_channels (int): Output channels of conv_seg. Default: None.threshold (float): Threshold for binary segmentation in the case ofnum_classes1. Default: None.dropout_ratio (float): Ratio of dropout layer. Default: 0.1.conv_cfg (dict|None): Config of conv layers. Default: None.norm_cfg (dict|None): Config of norm layers. Default: None.act_cfg (dict): Config of activation layers.Default: dict(typeReLU)in_index (int|Sequence[int]): Input feature index. Default: -1ignore_index (int | None): The label index to be ignored. When usingmasked BCE loss, ignore_index should be set to None. Default: 255.align_corners (bool): align_corners argument of F.interpolate.Default: False.The all mlp Head of segformer.This head is the implementation ofSegformer https://arxiv.org/abs/2105.15203 _.Args:interpolate_mode: The interpolate mode of MLP head upsample operation.Default: bilinear.def __init__(self,in_channels [v * 2 for v in [32, 64, 160, 256]],channels 256,num_classes 2,out_channelsNone,thresholdNone,dropout_ratio0.1,conv_cfgNone,norm_cfgdict(typeSyncBN, requires_gradTrue),act_cfgdict(typeReLU),in_index[0, 1, 2, 3],input_transformmultiple_select,ignore_index255,interpolate_modebilinear,align_cornersFalse,):super(BaseDecodeHead,self).__init__()# self._init_inputs(in_channels, in_index, input_transform)self.in_channels in_channelsself.channels channelsself.dropout_ratio dropout_ratioself.conv_cfg conv_cfgself.norm_cfg norm_cfgself.act_cfg act_cfgself.in_index in_indexself.input_transform input_transformself.ignore_index ignore_indexself.align_corners align_cornersself.interpolate_mode interpolate_modenum_inputs len(self.in_channels)assert num_inputs len(self.in_index)self.convs nn.ModuleList()self.convs nn.ModuleList()for i in range(num_inputs):self.convs.append(ConvModule(in_channelsself.in_channels[i],out_channelsself.channels,kernel_size1,stride1,norm_cfgself.norm_cfg,act_cfgself.act_cfg))self.fusion_conv ConvModule(in_channelsself.channels * num_inputs,out_channelsself.channels,kernel_size1,norm_cfgself.norm_cfg)if out_channels is None:if num_classes 2:warnings.warn(For binary segmentation, we suggest usingout_channels 1 to define the outputchannels of segmentor, and use thresholdto convert seg_logits into a predictionapplying a threshold)out_channels num_classesif out_channels ! num_classes and out_channels ! 1:raise ValueError(out_channels should be equal to num_classes,except binary segmentation set out_channels 1 andfnum_classes 2, but got out_channels{out_channels}fand num_classes{num_classes})if out_channels 1 and threshold is None:threshold 0.3warnings.warn(threshold is not defined for binary, and defaultsto 0.3)self.num_classes num_classesself.out_channels out_channelsself.threshold thresholdself.conv_seg nn.Conv2d(channels, self.out_channels, kernel_size1)if dropout_ratio 0:self.dropout nn.Dropout2d(dropout_ratio)else:self.dropout Nonedef _transform_inputs(self, inputs):Transform inputs for decoder.Args:inputs (list[Tensor]): List of multi-level img features.Returns:Tensor: The transformed inputsif self.input_transform resize_concat:inputs [inputs[i] for i in self.in_index]upsampled_inputs [resize(inputx,sizeinputs[0].shape[2:],modebilinear,align_cornersself.align_corners) for x in inputs]inputs torch.cat(upsampled_inputs, dim1)elif self.input_transform multiple_select:inputs [inputs[i] for i in self.in_index]else:inputs inputs[self.in_index]return inputsabstractmethoddef forward(self, inputs):Placeholder of forward function.# Receive 4 stage backbone feature map: 1/4, 1/8, 1/16, 1/32inputs self._transform_inputs(inputs)outs []for idx in range(len(inputs)):x inputs[idx]conv self.convs[idx]outs.append(resize(inputconv(x),sizeinputs[0].shape[2:],modeself.interpolate_mode,align_cornersself.align_corners))out self.fusion_conv(torch.cat(outs, dim1))out self.cls_seg(out)return outdef cls_seg(self, feat):Classify each pixel.if self.dropout is not None:feat self.dropout(feat)output self.conv_seg(feat)return output 3.测试模型 import torch from torch import nn # from torchsummary import summary from torchinfo import summaryfrom backbone1 import backbone from neck import FeatureFusionNeck from head import BaseDecodeHeadclass siamencoderdecoder(nn.Module):def __init__(self):super(siamencoderdecoder, self).__init__()self.backbone backbone()# self.backbone2 backbone()self.neck FeatureFusionNeck()self.head BaseDecodeHead()def backboneforward(self,x1,x2):# 孪生主干网络特征提取x1,x2 self.backbone(x1),self.backbone(x2)return x1,x2def forward(self,x1,x2):x1,x2 self.backboneforward(x1,x2)x self.neck(x1,x2)logit self.head(x)return logitif __name__ __main__:inputs torch.rand(3, 224, 224)model siamencoderdecoder()device torch.device(cuda if torch.cuda.is_available() else cpu)model.to(device)summary(model,input_size[(1,3,256,256),(1,3,256,256)]) summary Layer (type:depth-idx) Output Shape Param #siamencoderdecoder [1, 2, 64, 64] -- ├─backbone: 1-1 -- (recursive) │ └─MixVisionTransformer: 2-2 -- (recursive) │ │ └─ModuleList: 3-2 -- (recursive) ├─backbone: 1-2 [1, 32, 64, 64] 3,319,392 │ └─MixVisionTransformer: 2-2 -- (recursive) │ │ └─ModuleList: 3-2 -- (recursive) ├─FeatureFusionNeck: 1-3 [1, 64, 64, 64] -- ├─BaseDecodeHead: 1-4 [1, 2, 64, 64] -- │ └─ModuleList: 2-3 -- -- │ │ └─ConvModule: 3-3 [1, 256, 64, 64] 16,896 │ │ └─ConvModule: 3-4 [1, 256, 32, 32] 33,280 │ │ └─ConvModule: 3-5 [1, 256, 16, 16] 82,432 │ │ └─ConvModule: 3-6 [1, 256, 8, 8] 131,584 │ └─ConvModule: 2-4 [1, 256, 64, 64] -- │ │ └─Conv2d: 3-7 [1, 256, 64, 64] 262,144 │ │ └─SyncBatchNorm: 3-8 [1, 256, 64, 64] 512 │ │ └─ReLU: 3-9 [1, 256, 64, 64] -- │ └─Dropout2d: 2-5 [1, 256, 64, 64] -- │ └─Conv2d: 2-6 [1, 2, 64, 64] 514Total params: 7,166,146 Trainable params: 7,166,146 Non-trainable params: 0 Total mult-adds (G): 2.09Input size (MB): 1.57 Forward/backward pass size (MB): 141.75 Params size (MB): 12.29 Estimated Total Size (MB): 155.62 Process finished with exit code 06. changer主干网络 opencd作者提了一个主干网络特征交换的idea通过继承ResNet实现的网络结构并在主网络中给出了使用实例 Example: # from opencd.models import IA_ResNet # import torch # self IA_ResNet(depth18) # self.eval() # inputs torch.rand(1, 3, 32, 32) # level_outputs self.forward(inputs, inputs) # for level_out in level_outputs: # … print(tuple(level_out.shape)) (1, 128, 8, 8) (1, 256, 4, 4) (1, 512, 2, 2) (1, 1024, 1, 1) “” # Copyright (c) Open-CD. All rights reserved. import torch import torch.nn as nnfrom mmseg.models.backbones import ResNet from opencd.registry import MODELS# MODELS.register_module() class IA_ResNet(ResNet):Interaction ResNet backbone.Args:interaction_cfg (Sequence[dict]): Interaction strategies for the stages.The length should be the same as num_stages. The details can befound in opencd/models/ly_utils/interaction_layer.py.Default: (None, None, None, None).depth (int): Depth of resnet, from {18, 34, 50, 101, 152}.in_channels (int): Number of input image channels. Default: 3.stem_channels (int): Number of stem channels. Default: 64.base_channels (int): Number of base channels of res layer. Default: 64.num_stages (int): Resnet stages, normally 4. Default: 4.strides (Sequence[int]): Strides of the first block of each stage.Default: (1, 2, 2, 2).dilations (Sequence[int]): Dilation of each stage.Default: (1, 1, 1, 1).out_indices (Sequence[int]): Output from which stages.Default: (0, 1, 2, 3).style (str): pytorch or caffe. If set to pytorch, the stride-twolayer is the 3x3 conv layer, otherwise the stride-two layer isthe first 1x1 conv layer. Default: pytorch.deep_stem (bool): Replace 7x7 conv in input stem with 3 3x3 conv.Default: False.avg_down (bool): Use AvgPool instead of stride conv whendownsampling in the bottleneck. Default: False.frozen_stages (int): Stages to be frozen (stop grad and set eval mode).-1 means not freezing any parameters. Default: -1.conv_cfg (dict | None): Dictionary to construct and config conv layer.When conv_cfg is None, cfg will be set to dict(typeConv2d).Default: None.norm_cfg (dict): Dictionary to construct and config norm layer.Default: dict(typeBN, requires_gradTrue).norm_eval (bool): Whether to set norm layers to eval mode, namely,freeze running stats (mean and var). Note: Effect on Batch Normand its variants only. Default: False.dcn (dict | None): Dictionary to construct and config DCN conv layer.When dcn is not None, conv_cfg must be None. Default: None.stage_with_dcn (Sequence[bool]): Whether to set DCN conv for eachstage. The length of stage_with_dcn is equal to num_stages.Default: (False, False, False, False).plugins (list[dict]): List of plugins for stages, each dict contains:- cfg (dict, required): Cfg dict to build plugin.- position (str, required): Position inside block to insert plugin,options: after_conv1, after_conv2, after_conv3.- stages (tuple[bool], optional): Stages to apply plugin, lengthshould be same as num_stages.Default: None.multi_grid (Sequence[int]|None): Multi grid dilation rates of laststage. Default: None.contract_dilation (bool): Whether contract first dilation of each layerDefault: False.with_cp (bool): Use checkpoint or not. Using checkpoint will save somememory while slowing down the training speed. Default: False.zero_init_residual (bool): Whether to use zero init for last norm layerin resblocks to let them behave as identity. Default: True.pretrained (str, optional): model pretrained path. Default: None.init_cfg (dict or list[dict], optional): Initialization config dict.Default: None.Example:# from opencd.models import IA_ResNet# import torch# self IA_ResNet(depth18)# self.eval()# inputs torch.rand(1, 3, 32, 32)# level_outputs self.forward(inputs, inputs)# for level_out in level_outputs:# ... print(tuple(level_out.shape))(1, 128, 8, 8)(1, 256, 4, 4)(1, 512, 2, 2)(1, 1024, 1, 1)def __init__(self,interaction_cfg(None, None, None, None),**kwargs):super().__init__(**kwargs)assert self.num_stages len(interaction_cfg), \The length of the interaction_cfg should be same as the num_stages.# cross-correlationself.ccs []for ia_cfg in interaction_cfg:if ia_cfg is None:ia_cfg dict(typeTwoIdentity)self.ccs.append(MODELS.build(ia_cfg))self.ccs nn.ModuleList(self.ccs)def forward(self, x1, x2):Forward function.def _stem_forward(x):if self.deep_stem:x self.stem(x)else:x self.conv1(x)x self.norm1(x)x self.relu(x)x self.maxpool(x)return xx1 _stem_forward(x1)x2 _stem_forward(x2)outs []for i, layer_name in enumerate(self.res_layers):res_layer getattr(self, layer_name)x1 res_layer(x1)x2 res_layer(x2)x1, x2 self.ccs[i](x1, x2)if i in self.out_indices:outs.append(torch.cat([x1, x2], dim1))return tuple(outs)# MODELS.register_module() class IA_ResNetV1c(IA_ResNet):ResNetV1c variant described in [1]_.Compared with default ResNet(ResNetV1b), ResNetV1c replaces the 7x7 conv inthe input stem with three 3x3 convs. For more details please refer to Bagof Tricks for Image Classification with Convolutional Neural Networkshttps://arxiv.org/abs/1812.01187_.def __init__(self, **kwargs):super(IA_ResNetV1c, self).__init__(deep_stemTrue, avg_downFalse, **kwargs)# MODELS.register_module() class IA_ResNetV1d(IA_ResNet):ResNetV1d variant described in [1]_.Compared with default ResNet(ResNetV1b), ResNetV1d replaces the 7x7 conv inthe input stem with three 3x3 convs. And in the downsampling block, a 2x2avg_pool with stride 2 is added before conv, whose stride is changed to 1.def __init__(self, **kwargs):super(IA_ResNetV1d, self).__init__(deep_stemTrue, avg_downTrue, **kwargs)下面在上述环境下我们来调用一下 import torch from IA_ResNet import IA_ResNetV1c backbone IA_ResNetV1c(depth18) backbone.eval() inputs torch.rand(1, 3, 256, 256) level_outputs backbone.forward(inputs, inputs) for level_out in level_outputs:print(tuple(level_out.shape)) # output (1, 128, 64, 64) (1, 256, 32, 32) (1, 512, 16, 16) (1, 1024, 8, 8)总结 通过上述内容我们可以根据参数文件中的内容提取opencd中任意网络结构或采用timm来设置主干网络结构或添加到自己的训练框架中如pytorch_segmentation中进行训练。相应的我们可以进一步去学习mmalb的框架结构
http://www.dnsts.com.cn/news/33060.html

相关文章:

  • 网站外链带nofollow是什么意思新开传奇网站合击
  • 做衣服的教程网站有哪些演示网站怎么做
  • 盂县在线这个网站是谁做的网站服务器免费申请
  • 做网站用vs深圳龙华区发达吗
  • 高端网站制作报价一级消防工程师考试条件
  • 北京网站建设东轩seo金融行业做网站需要什么
  • 专业做网站建设公司有哪些北海百度seo
  • 邢台企业做网站哪家好邢台网站制作费用
  • 个人网站主机的配置北京建网站价格
  • 珠海中国建设银行招聘信息网站Wordpress标签与分类
  • 有网站前端如何做后台天津营销网站建设联系方式
  • 检测网站名 注册怎么seo关键词优化排名
  • 医院 网站源码wordpress自适应吸附菜单
  • 西安市建设厅网站影视网站建设要多少钱
  • 上海网站建设的价格低重庆网站建设推荐
  • 营销型网站建设目的和意义济南住建局官方网站
  • 上传的网站打不开wordpress插件汉化教程视频
  • 北京高端网站wordpress下载类主题系统主题
  • 自助建站竹子易书网上书城网站建设方案
  • 公众号怎么做微网站凡客v 网上商城
  • 网上做兼职的网站有哪些建立网店
  • 网站域名是什有什么字体设计的网站
  • 当当网网站的建设过程php 企业网站模板
  • 公司怎么制作网站企业网站搭建及优化
  • 个人网站怎么快速推广公众号运营总结
  • 做加盟正规网站微信公众号小程序搭建
  • 做网站卖流量无锡君通科技服务有限公司
  • 广州网站建设优化方案深圳品牌网站制作
  • 北京移动端网站百度点击软件找名风
  • 哪个网站科技新闻好在百度备案网站