|
- '''
- # @time:2023/4/16 10:11
- # Author:Tuan
- # @File:segformer_memory.py
- '''
- # ---------------------------------------------------------------
- # Copyright (c) 2021, NVIDIA Corporation. All rights reserved.
- #
- # This work is licensed under the NVIDIA Source Code License
- # ---------------------------------------------------------------
- import math
- import warnings
- import numpy as np
- from functools import partial
- import cv2
-
- import torch
- import torch.nn as nn
- import torch.nn.functional as F
-
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
-
- '''
- memory
- '''
- class SelfAttentionBlock(nn.Module):
- def __init__(self, key_in_channels, query_in_channels, transform_channels, out_channels, share_key_query,
- query_downsample, key_downsample, key_query_num_convs, value_out_num_convs, key_query_norm,
- value_out_norm, matmul_norm, with_out_project, norm_cfg=None, act_cfg=None):
- super(SelfAttentionBlock, self).__init__()
- # key project
- self.key_project = self.buildproject(
- in_channels=key_in_channels,
- out_channels=transform_channels,
- num_convs=key_query_num_convs,
- use_norm=key_query_norm,
- norm_cfg=norm_cfg,
- act_cfg=act_cfg,
- )
- # query project
- if share_key_query:
- assert key_in_channels == query_in_channels
- self.query_project = self.key_project
- else:
- self.query_project = self.buildproject(
- in_channels=query_in_channels,
- out_channels=transform_channels,
- num_convs=key_query_num_convs,
- use_norm=key_query_norm,
- norm_cfg=norm_cfg,
- act_cfg=act_cfg,
- )
- # value project
- self.value_project = self.buildproject(
- in_channels=key_in_channels,
- out_channels=transform_channels if with_out_project else out_channels,
- num_convs=value_out_num_convs,
- use_norm=value_out_norm,
- norm_cfg=norm_cfg,
- act_cfg=act_cfg,
- )
- # out project
- self.out_project = None
- if with_out_project:
- self.out_project = self.buildproject(
- in_channels=transform_channels,
- out_channels=out_channels,
- num_convs=value_out_num_convs,
- use_norm=value_out_norm,
- norm_cfg=norm_cfg,
- act_cfg=act_cfg,
- )
- # downsample
- self.query_downsample = query_downsample
- self.key_downsample = key_downsample
- self.matmul_norm = matmul_norm
- self.transform_channels = transform_channels
- '''forward'''
- def forward(self, query_feats, key_feats):
- batch_size = query_feats.size(0)
- query = self.query_project(query_feats)
- if self.query_downsample is not None: query = self.query_downsample(query)
- query = query.reshape(*query.shape[:2], -1)
- query = query.permute(0, 2, 1).contiguous()
- key = self.key_project(key_feats)
- value = self.value_project(key_feats)
- if self.key_downsample is not None:
- key = self.key_downsample(key)
- value = self.key_downsample(value)
- key = key.reshape(*key.shape[:2], -1)
- value = value.reshape(*value.shape[:2], -1)
- value = value.permute(0, 2, 1).contiguous()
- sim_map = torch.matmul(query, key)
- if self.matmul_norm:
- sim_map = (self.transform_channels ** -0.5) * sim_map
- sim_map = F.softmax(sim_map, dim=-1)
- context = torch.matmul(sim_map, value)
- context = context.permute(0, 2, 1).contiguous()
- context = context.reshape(batch_size, -1, *query_feats.shape[2:])
- if self.out_project is not None:
- context = self.out_project(context)
- return context
- '''build project'''
- def buildproject(self, in_channels, out_channels, num_convs, use_norm, norm_cfg, act_cfg):
- if use_norm:
- convs = [nn.Sequential(
- nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False),
- nn.BatchNorm2d(out_channels),
- nn.ReLU(),
- # BuildNormalization(constructnormcfg(placeholder=out_channels, norm_cfg=norm_cfg)),
- # BuildActivation(act_cfg),
- )]
- for _ in range(num_convs - 1):
- convs.append(nn.Sequential(
- nn.Conv2d(out_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False),
- nn.BatchNorm2d(out_channels),
- nn.ReLU(),
- # BuildNormalization(constructnormcfg(placeholder=out_channels, norm_cfg=norm_cfg)),
- # BuildActivation(act_cfg),
- ))
- else:
- convs = [nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False)]
- for _ in range(num_convs - 1):
- convs.append(
- nn.Conv2d(out_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False)
- )
- if len(convs) > 1: return nn.Sequential(*convs)
- return convs[0]
-
- class Memoy(nn.Module):
- def __init__(self,num_classes,num_feats_per_cls):
- super(Memoy, self).__init__()
- self.num_feats_per_cls = num_feats_per_cls
- self.memory = nn.Parameter(torch.zeros(num_classes, self.num_feats_per_cls, 1024, dtype=torch.float),
- requires_grad=False)
- self.feats_channels = 1024
- if self.num_feats_per_cls > 1:
- self.self_attentions = nn.ModuleList()
- for _ in range(self.num_feats_per_cls):
- self_attention = SelfAttentionBlock(
- key_in_channels=1024,
- query_in_channels=1024,
- transform_channels=512,
- out_channels=1024,
- share_key_query=False,
- query_downsample=None,
- key_downsample=None,
- key_query_num_convs=2,
- value_out_num_convs=1,
- key_query_norm=True,
- value_out_norm=True,
- matmul_norm=True,
- with_out_project=True,
-
- )
- self.self_attentions.append(self_attention)
- self.fuse_memory_conv = nn.Sequential(
- nn.Conv2d(1024 * self.num_feats_per_cls, 1024, kernel_size=1, stride=1, padding=0, bias=False),
- nn.BatchNorm2d(1024),
- nn.ReLU(),
- )
- else:
- self.self_attention = SelfAttentionBlock(
- key_in_channels=1024,
- query_in_channels=1024,
- transform_channels=512,
- out_channels=1024,
- share_key_query=False,
- query_downsample=None,
- key_downsample=None,
- key_query_num_convs=2,
- value_out_num_convs=1,
- key_query_norm=True,
- value_out_norm=True,
- matmul_norm=True,
- with_out_project=True,
-
- )
- self.bottleneck = nn.Sequential(
- nn.Conv2d(1024 * 2, 1024, kernel_size=3, stride=1, padding=1, bias=False),
- nn.BatchNorm2d(1024),
- nn.ReLU(),
- )
- self.up = Up_sample()
- def forward(self,x ,):
- batch_size, num_channels, h, w = x.size()
- # h, w = 256, 256
- x_m, x_m_1 = self.up(x) # [1, 7,16 ,16 ]
- x_m = x_m.reshape(x_m.shape[0] * x_m.shape[1], x_m.shape[2] * x_m.shape[3])
- x_m = x_m.permute(1, 0)
- selected_memory_list = []
- for idx in range(self.num_feats_per_cls):
- memory = self.memory.data[:, idx, :]
- selected_memory = torch.matmul(x_m, memory)
- selected_memory_list.append(selected_memory.unsqueeze(1))
- if self.num_feats_per_cls > 1:
- relation_selected_memory_list = []
- for idx, selected_memory in enumerate(selected_memory_list):
- # --(B*H*W, C) --> (B, H, W, C)
- selected_memory = selected_memory.view(batch_size, h, w, num_channels)
- # --(B, H, W, C) --> (B, C, H, W)
- selected_memory = selected_memory.permute(0, 3, 1, 2).contiguous()
- # --append
- relation_selected_memory_list.append(self.self_attentions[idx](x, selected_memory))
- # --concat
- selected_memory = torch.cat(relation_selected_memory_list, dim=1)
- selected_memory = self.fuse_memory_conv(selected_memory)
- else:
- assert len(selected_memory_list) == 1
- selected_memory = selected_memory_list[0].squeeze(1)
- # --(B*H*W, C) --> (B, H, W, C)
- selected_memory = selected_memory.view(batch_size, h, w, num_channels)
- # --(B, H, W, C) --> (B, C, H, W)
- selected_memory = selected_memory.permute(0, 3, 1, 2).contiguous()
- # --feed into the self attention module
- selected_memory = self.self_attention(x, selected_memory)
- memory_output = self.bottleneck(torch.cat([x, selected_memory], dim=1))
-
- return memory_output
- def update(self, features, segmentation, ignore_index=255, momentum=0.9, learning_rate=None):
- # assert strategy in ['mean', 'cosine_similarity']
- batch_size, num_channels, h, w = features.size()
- # momentum = momentum_cfg['base_momentum']
- # if momentum_cfg['adjust_by_learning_rate']:
- # momentum = momentum_cfg['base_momentum'] / momentum_cfg['base_lr'] * learning_rate
- # use features to update memory
-
- # momentum = 0.9
- segmentation = segmentation.long()
- features = features.permute(0, 2, 3, 1).contiguous()
- features = features.view(batch_size * h * w, num_channels)
- clsids = segmentation.unique()
- for clsid in clsids:
- if clsid == ignore_index: continue
- # --(B, H, W) --> (B*H*W,)
- seg_cls = segmentation.view(-1)
- # --extract the corresponding feats: (K, C)
- feats_cls = features[seg_cls == clsid]
- # --init memory by using extracted features
- need_update = True
- for idx in range(self.num_feats_per_cls):
- if (self.memory[clsid][idx] == 0).sum() == self.feats_channels:
- self.memory[clsid][idx].data.copy_(feats_cls.mean(0))
- need_update = False
- break
- if not need_update: continue
- # --update according to the selected strategy
- if self.num_feats_per_cls == 1:
- # if strategy == 'mean':
- feats_cls = feats_cls.mean(0)
- # elif strategy == 'cosine_similarity':
- # similarity = F.cosine_similarity(feats_cls, self.memory[clsid].data.expand_as(feats_cls))
- # weight = (1 - similarity) / (1 - similarity).sum()
- # feats_cls = (feats_cls * weight.unsqueeze(-1)).sum(0)
- feats_cls = (1 - momentum) * self.memory[clsid].data + momentum * feats_cls.unsqueeze(0)
- self.memory[clsid].data.copy_(feats_cls)
- # print("成功更新!")
- # else:
- # assert strategy in ['cosine_similarity']
- # # ----(K, C) * (C, num_feats_per_cls) --> (K, num_feats_per_cls)
- # relation = torch.matmul(
- # F.normalize(feats_cls, p=2, dim=1),
- # F.normalize(self.memory[clsid].data.permute(1, 0).contiguous(), p=2, dim=0),
- # )
- # argmax = relation.argmax(dim=1)
- # # ----for saving memory during training
- # for idx in range(self.num_feats_per_cls):
- # mask = (argmax == idx)
- # feats_cls_iter = feats_cls[mask]
- # memory_cls_iter = self.memory[clsid].data[idx].unsqueeze(0).expand_as(feats_cls_iter)
- # similarity = F.cosine_similarity(feats_cls_iter, memory_cls_iter)
- # weight = (1 - similarity) / (1 - similarity).sum()
- # feats_cls_iter = (feats_cls_iter * weight.unsqueeze(-1)).sum(0)
- # self.memory[clsid].data[idx].copy_(self.memory[clsid].data[idx] * (1 - momentum) + feats_cls_iter * momentum)
-
- class Up_sample(nn.Module):
- def __init__(self):
- super(Up_sample, self).__init__()
-
- self.UP_stage_1 = nn.Sequential(
- nn.Conv2d(1024, 512, 3, padding=1),
- nn.BatchNorm2d(512),
- nn.ReLU(),
- # nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
- )
- self.UP_stage_2 = nn.Sequential(
- nn.Conv2d(512, 256, 3, padding=1),
- nn.BatchNorm2d(256),
- nn.ReLU(),
- # nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
- )
- self.UP_stage_3 = nn.Sequential(
- nn.Conv2d(256, 128, 3, padding=1),
- nn.BatchNorm2d(128),
- nn.ReLU(),
- # nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
- )
- self.UP_stage_4 = nn.Sequential(
- nn.Conv2d(128, 7, 3, padding=1),
- nn.BatchNorm2d(7),
- nn.ReLU(),
- # nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
- )
-
- self.UP = nn.Sequential(
- # nn.Conv2d(1024, 512, 3, padding=1),
- # nn.BatchNorm2d(512),
- # nn.ReLU(),
- nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
- )
-
-
-
-
- def forward(self,x):
- x1 = x
- x = self.UP_stage_1(x)
- x = self.UP_stage_2(x)
- x = self.UP_stage_3(x)
- x = self.UP_stage_4(x)
- # x = self.cls_seg(x)
- x1 = self.UP(x1)
- x1 = self.UP(x1)
- x1 = self.UP(x1)
- x1 = self.UP(x1)
- return x , x1
- '''
- line
- '''
- class Conv(nn.Module):
- def __init__(self,inchanel,outchanel):
- super(Conv, self).__init__()
- self.conv = nn.Sequential(
- nn.Conv2d(inchanel, inchanel // 2, (3, 3), padding=1),
- nn.BatchNorm2d(inchanel // 2),
- nn.ReLU(),
- nn.Conv2d(inchanel//2, inchanel // 4, (3, 3), padding=1),
- nn.BatchNorm2d(inchanel // 4),
- nn.ReLU(),
- nn.Conv2d(inchanel//4, outchanel, (3, 3), padding=1),
- nn.BatchNorm2d(outchanel),
- nn.ReLU(),
- )
- def forward(self,input):
- return self.conv(input)
- class Conv_line(nn.Module):
- def __init__(self,inchanel,outchanel):
- super(Conv_line, self).__init__()
- self.conv = nn.Sequential(
- nn.Conv2d(inchanel, inchanel * 4, (3, 3), padding=1),
- nn.BatchNorm2d(inchanel * 4),
- nn.ReLU(),
- nn.Conv2d(inchanel*4, inchanel * 8, (3, 3), padding=1),
- nn.BatchNorm2d(inchanel * 8),
- nn.ReLU(),
- nn.Conv2d(inchanel*8, inchanel*16, (3, 3), padding=1),
- nn.BatchNorm2d(inchanel*16),
- nn.ReLU(),
- nn.Conv2d(inchanel*16, inchanel *8, (3, 3), padding=1),
- nn.BatchNorm2d(inchanel *8),
- nn.ReLU(),
- # nn.Conv2d(inchanel // 2, inchanel // 4, (3, 3), padding=1),
- # nn.BatchNorm2d(inchanel // 4),
- # nn.ReLU(),
- nn.Conv2d(inchanel * 8, outchanel, (3, 3), padding=1),
- )
- def forward(self,input):
- return self.conv(input)
- class Line_attention(nn.Module):
- def __init__(self):
- super(Line_attention, self).__init__()
- def forward(self,img):
- bs, w, h = img.shape
-
- sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0)
- sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1)
- sobelx = cv2.convertScaleAbs(sobelx) # 转回uint8
- sobely = cv2.convertScaleAbs(sobely)
- sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
- # imageio.imwrite(save_path_line, sobelxy)
-
- img = cv2.GaussianBlur(sobelxy, (29, 29), 0)
- # imageio.imwrite(save_path_atten, img2)
- return img
- def line_attention(img):
- if len(img.shape) == 3:
- pass
- else:
- img = img.unsqueeze(0)
- bs, w, h = img.shape
-
- att = []
-
- for i in range(bs):
- image = img[i:i+1,:,:].squeeze().detach().cpu().numpy()
- sobelx = cv2.Sobel(image, cv2.CV_64F, 1, 0)
- sobely = cv2.Sobel(image, cv2.CV_64F, 0, 1)
- sobelx = cv2.convertScaleAbs(sobelx) # 转回uint8
- sobely = cv2.convertScaleAbs(sobely)
- sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
- # sobelxy[sobelxy==1] = 255
- # imageio.imwrite(save_path_line, sobelxy)
-
- # image = cv2.GaussianBlur(sobelxy, (101, 101), 0)
- # if image.max() != image.min():
- # image = (image - image.min())/(image.max() - image.min())
- att.append(sobelxy)
-
- # imageio.imwrite(save_path_atten, img2)
- # x = torch.cat([a.tensor() for a in att])
- att = torch.cat([torch.Tensor(a).unsqueeze(dim=0) for a in att], 0).unsqueeze(dim=1)
- # att = (att - att.min())/(att.max() - att.min())
- # att1 = F.sigmoid(att)
- return att
-
- '''
- sefomer
- '''
- def _no_grad_trunc_normal_(tensor, mean, std, a, b):
- # Cut & paste from PyTorch official master until it's in a few official releases - RW
- # Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf
- def norm_cdf(x):
- # Computes standard normal cumulative distribution function
- return (1. + math.erf(x / math.sqrt(2.))) / 2.
-
- if (mean < a - 2 * std) or (mean > b + 2 * std):
- warnings.warn("mean is more than 2 std from [a, b] in nn.init.trunc_normal_. "
- "The distribution of values may be incorrect.",
- stacklevel=2)
-
- with torch.no_grad():
- # Values are generated by using a truncated uniform distribution and
- # then using the inverse CDF for the normal distribution.
- # Get upper and lower cdf values
- l = norm_cdf((a - mean) / std)
- u = norm_cdf((b - mean) / std)
-
- # Uniformly fill tensor with values from [l, u], then translate to
- # [2l-1, 2u-1].
- tensor.uniform_(2 * l - 1, 2 * u - 1)
-
- # Use inverse cdf transform for normal distribution to get truncated
- # standard normal
- tensor.erfinv_()
-
- # Transform to proper mean, std
- tensor.mul_(std * math.sqrt(2.))
- tensor.add_(mean)
-
- # Clamp to ensure it's in the proper range
- tensor.clamp_(min=a, max=b)
- return tensor
-
-
- def trunc_normal_(tensor, mean=0., std=1., a=-2., b=2.):
- r"""
- Fills the input Tensor with values drawn from a truncated
- normal distribution. The values are effectively drawn from the
- normal distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)`
- with values outside :math:`[a, b]` redrawn until they are within
- the bounds. The method used for generating the random values works
- best when :math:`a \leq \text{mean} \leq b`.
- Args:
- tensor: an n-dimensional `torch.Tensor`
- mean: the mean of the normal distribution
- std: the standard deviation of the normal distribution
- a: the minimum cutoff value
- b: the maximum cutoff value
- Examples:
- >>> w = torch.empty(3, 5)
- >>> nn.init.trunc_normal_(w)
- """
- return _no_grad_trunc_normal_(tensor, mean, std, a, b)
-
-
- # --------------------------------------#
- # Gelu激活函数的实现
- # 利用近似的数学公式
- # --------------------------------------#
- class GELU(nn.Module):
- def __init__(self):
- super(GELU, self).__init__()
-
- def forward(self, x):
- return 0.5 * x * (1 + torch.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * torch.pow(x, 3))))
-
-
- class OverlapPatchEmbed(nn.Module):
- def __init__(self, patch_size=7, stride=4, in_chans=4, embed_dim=768):
- super().__init__()
- patch_size = (patch_size, patch_size)
- self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=stride,
- padding=(patch_size[0] // 2, patch_size[1] // 2))
- self.norm = nn.LayerNorm(embed_dim)
-
- self.apply(self._init_weights)
-
- def _init_weights(self, m):
- if isinstance(m, nn.Linear):
- trunc_normal_(m.weight, std=.02)
- if isinstance(m, nn.Linear) and m.bias is not None:
- nn.init.constant_(m.bias, 0)
- elif isinstance(m, nn.LayerNorm):
- nn.init.constant_(m.bias, 0)
- nn.init.constant_(m.weight, 1.0)
- elif isinstance(m, nn.Conv2d):
- fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
- fan_out //= m.groups
- m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))
- if m.bias is not None:
- m.bias.data.zero_()
-
- def forward(self, x):
- x = self.proj(x)
- _, _, H, W = x.shape
- x = x.flatten(2).transpose(1, 2)
- x = self.norm(x)
-
- return x, H, W
-
-
- # --------------------------------------------------------------------------------------------------------------------#
- # Attention机制
- # 将输入的特征qkv特征进行划分,首先生成query, key, value。query是查询向量、key是键向量、v是值向量。
- # 然后利用 查询向量query 叉乘 转置后的键向量key,这一步可以通俗的理解为,利用查询向量去查询序列的特征,获得序列每个部分的重要程度score。
- # 然后利用 score 叉乘 value,这一步可以通俗的理解为,将序列每个部分的重要程度重新施加到序列的值上去。
- #
- # 在segformer中,为了减少计算量,首先对特征图进行了浓缩,所有特征层都压缩到原图的1/32。
- # 当输入图片为512, 512时,Block1的特征图为128, 128,此时就先将特征层压缩为16, 16。
- # 在Block1的Attention模块中,相当于将8x8个特征点进行特征浓缩,浓缩为一个特征点。
- # 然后利用128x128个查询向量对16x16个键向量与值向量进行查询。尽管键向量与值向量的数量较少,但因为查询向量的不同,依然可以获得不同的输出。
- # --------------------------------------------------------------------------------------------------------------------#
- class Attention(nn.Module):
- def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0., sr_ratio=1):
- super().__init__()
- assert dim % num_heads == 0, f"dim {dim} should be divided by num_heads {num_heads}."
-
- self.dim = dim
- self.num_heads = num_heads
- head_dim = dim // num_heads
- self.scale = qk_scale or head_dim ** -0.5
-
- self.q = nn.Linear(dim, dim, bias=qkv_bias)
-
- self.sr_ratio = sr_ratio
- if sr_ratio > 1:
- self.sr = nn.Conv2d(dim, dim, kernel_size=sr_ratio, stride=sr_ratio)
- self.norm = nn.LayerNorm(dim)
- self.kv = nn.Linear(dim, dim * 2, bias=qkv_bias)
-
- self.attn_drop = nn.Dropout(attn_drop)
-
- self.proj = nn.Linear(dim, dim)
- self.proj_drop = nn.Dropout(proj_drop)
-
- self.apply(self._init_weights)
-
- def _init_weights(self, m):
- if isinstance(m, nn.Linear):
- trunc_normal_(m.weight, std=.02)
- if isinstance(m, nn.Linear) and m.bias is not None:
- nn.init.constant_(m.bias, 0)
- elif isinstance(m, nn.LayerNorm):
- nn.init.constant_(m.bias, 0)
- nn.init.constant_(m.weight, 1.0)
- elif isinstance(m, nn.Conv2d):
- fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
- fan_out //= m.groups
- m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))
- if m.bias is not None:
- m.bias.data.zero_()
-
- def forward(self, x, H, W):
- B, N, C = x.shape
- # bs, 16384, 32 => bs, 16384, 32 => bs, 16384, 8, 4 => bs, 8, 16384, 4
- q = self.q(x).reshape(B, N, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3)
-
- if self.sr_ratio > 1:
- # bs, 16384, 32 => bs, 32, 128, 128
- x_ = x.permute(0, 2, 1).reshape(B, C, H, W)
- # bs, 32, 128, 128 => bs, 32, 16, 16 => bs, 256, 32
- x_ = self.sr(x_).reshape(B, C, -1).permute(0, 2, 1)
- x_ = self.norm(x_)
- # bs, 256, 32 => bs, 256, 64 => bs, 256, 2, 8, 4 => 2, bs, 8, 256, 4
- kv = self.kv(x_).reshape(B, -1, 2, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
- else:
- kv = self.kv(x).reshape(B, -1, 2, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
- k, v = kv[0], kv[1]
-
- # bs, 8, 16384, 4 @ bs, 8, 4, 256 => bs, 8, 16384, 256
- attn = (q @ k.transpose(-2, -1)) * self.scale
- attn = attn.softmax(dim=-1)
- attn = self.attn_drop(attn)
-
- # bs, 8, 16384, 256 @ bs, 8, 256, 4 => bs, 8, 16384, 4 => bs, 16384, 32
- x = (attn @ v).transpose(1, 2).reshape(B, N, C)
- # bs, 16384, 32 => bs, 16384, 32
- x = self.proj(x)
- x = self.proj_drop(x)
-
- return x
-
-
- def drop_path(x, drop_prob: float = 0., training: bool = False, scale_by_keep: bool = True):
- """
- Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
- This is the same as the DropConnect impl I created for EfficientNet, etc networks, however,
- the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
- See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for
- changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use
- 'survival rate' as the argument.
- """
- if drop_prob == 0. or not training:
- return x
- keep_prob = 1 - drop_prob
- shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets
- random_tensor = x.new_empty(shape).bernoulli_(keep_prob)
- if keep_prob > 0.0 and scale_by_keep:
- random_tensor.div_(keep_prob)
- return x * random_tensor
-
-
- class DropPath(nn.Module):
- def __init__(self, drop_prob=None, scale_by_keep=True):
- super(DropPath, self).__init__()
- self.drop_prob = drop_prob
- self.scale_by_keep = scale_by_keep
-
- def forward(self, x):
- return drop_path(x, self.drop_prob, self.training, self.scale_by_keep)
-
-
- class DWConv(nn.Module):
- def __init__(self, dim=768):
- super(DWConv, self).__init__()
- self.dwconv = nn.Conv2d(dim, dim, 3, 1, 1, bias=True, groups=dim)
-
- def forward(self, x, H, W):
- B, N, C = x.shape
- x = x.transpose(1, 2).view(B, C, H, W)
- x = self.dwconv(x)
- x = x.flatten(2).transpose(1, 2)
-
- return x
-
-
- class Mlp(nn.Module):
- def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=GELU, drop=0.):
- super().__init__()
- out_features = out_features or in_features
- hidden_features = hidden_features or in_features
-
- self.fc1 = nn.Linear(in_features, hidden_features)
- self.dwconv = DWConv(hidden_features)
- self.act = act_layer()
-
- self.fc2 = nn.Linear(hidden_features, out_features)
-
- self.drop = nn.Dropout(drop)
-
- self.apply(self._init_weights)
-
- def _init_weights(self, m):
- if isinstance(m, nn.Linear):
- trunc_normal_(m.weight, std=.02)
- if isinstance(m, nn.Linear) and m.bias is not None:
- nn.init.constant_(m.bias, 0)
- elif isinstance(m, nn.LayerNorm):
- nn.init.constant_(m.bias, 0)
- nn.init.constant_(m.weight, 1.0)
- elif isinstance(m, nn.Conv2d):
- fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
- fan_out //= m.groups
- m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))
- if m.bias is not None:
- m.bias.data.zero_()
-
- def forward(self, x, H, W):
- x = self.fc1(x)
- x = self.dwconv(x, H, W)
- x = self.act(x)
- x = self.drop(x)
- x = self.fc2(x)
- x = self.drop(x)
- return x
-
-
- class Block(nn.Module):
- def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0.,
- drop_path=0., act_layer=GELU, norm_layer=nn.LayerNorm, sr_ratio=1):
- super().__init__()
- self.norm1 = norm_layer(dim)
-
- self.attn = Attention(
- dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale,
- attn_drop=attn_drop, proj_drop=drop, sr_ratio=sr_ratio
- )
- self.norm2 = norm_layer(dim)
- self.mlp = Mlp(in_features=dim, hidden_features=int(dim * mlp_ratio), act_layer=act_layer, drop=drop)
-
- self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
-
- self.apply(self._init_weights)
-
- def _init_weights(self, m):
- if isinstance(m, nn.Linear):
- trunc_normal_(m.weight, std=.02)
- if isinstance(m, nn.Linear) and m.bias is not None:
- nn.init.constant_(m.bias, 0)
- elif isinstance(m, nn.LayerNorm):
- nn.init.constant_(m.bias, 0)
- nn.init.constant_(m.weight, 1.0)
- elif isinstance(m, nn.Conv2d):
- fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
- fan_out //= m.groups
- m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))
- if m.bias is not None:
- m.bias.data.zero_()
-
- def forward(self, x, H, W):
- x = x + self.drop_path(self.attn(self.norm1(x), H, W))
- x = x + self.drop_path(self.mlp(self.norm2(x), H, W))
- return x
-
-
- class MixVisionTransformer(nn.Module):
- def __init__(self, in_chans=4, num_classes=1000, embed_dims=[32, 64, 160, 256],
- num_heads=[1, 2, 4, 8], mlp_ratios=[4, 4, 4, 4], qkv_bias=False, qk_scale=None, drop_rate=0.,
- attn_drop_rate=0., drop_path_rate=0., norm_layer=nn.LayerNorm,
- depths=[3, 4, 6, 3], sr_ratios=[8, 4, 2, 1]):
- super().__init__()
- self.num_classes = num_classes
- self.depths = depths
-
- # ----------------------------------#
- # Transformer模块,共有四个部分
- # ----------------------------------#
- dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))]
-
- # ----------------------------------#
- # block1
- # ----------------------------------#
- # -----------------------------------------------#
- # 对输入图像进行分区,并下采样
- # 512, 512, 3 => 128, 128, 32 => 16384, 32
- # -----------------------------------------------#
- self.patch_embed1 = OverlapPatchEmbed(patch_size=7, stride=4, in_chans=in_chans, embed_dim=embed_dims[0])
- # -----------------------------------------------#
- # 利用transformer模块进行特征提取
- # 16384, 32 => 16384, 32
- # -----------------------------------------------#
- cur = 0
- self.block1 = nn.ModuleList(
- [
- Block(
- dim=embed_dims[0], num_heads=num_heads[0], mlp_ratio=mlp_ratios[0], qkv_bias=qkv_bias,
- qk_scale=qk_scale,
- drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[cur + i], norm_layer=norm_layer,
- sr_ratio=sr_ratios[0]
- )
- for i in range(depths[0])
- ]
- )
- self.norm1 = norm_layer(embed_dims[0])
-
- # ----------------------------------#
- # block2
- # ----------------------------------#
- # -----------------------------------------------#
- # 对输入图像进行分区,并下采样
- # 128, 128, 32 => 64, 64, 64 => 4096, 64
- # -----------------------------------------------#
- self.patch_embed2 = OverlapPatchEmbed(patch_size=3, stride=2, in_chans=embed_dims[0], embed_dim=embed_dims[1])
- # -----------------------------------------------#
- # 利用transformer模块进行特征提取
- # 4096, 64 => 4096, 64
- # -----------------------------------------------#
- cur += depths[0]
- self.block2 = nn.ModuleList(
- [
- Block(
- dim=embed_dims[1], num_heads=num_heads[1], mlp_ratio=mlp_ratios[1], qkv_bias=qkv_bias,
- qk_scale=qk_scale,
- drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[cur + i], norm_layer=norm_layer,
- sr_ratio=sr_ratios[1]
- )
- for i in range(depths[1])
- ]
- )
- self.norm2 = norm_layer(embed_dims[1])
-
- # ----------------------------------#
- # block3
- # ----------------------------------#
- # -----------------------------------------------#
- # 对输入图像进行分区,并下采样
- # 64, 64, 64 => 32, 32, 160 => 1024, 160
- # -----------------------------------------------#
- self.patch_embed3 = OverlapPatchEmbed(patch_size=3, stride=2, in_chans=embed_dims[1], embed_dim=embed_dims[2])
- # -----------------------------------------------#
- # 利用transformer模块进行特征提取
- # 1024, 160 => 1024, 160
- # -----------------------------------------------#
- cur += depths[1]
- self.block3 = nn.ModuleList(
- [
- Block(
- dim=embed_dims[2], num_heads=num_heads[2], mlp_ratio=mlp_ratios[2], qkv_bias=qkv_bias,
- qk_scale=qk_scale,
- drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[cur + i], norm_layer=norm_layer,
- sr_ratio=sr_ratios[2]
- )
- for i in range(depths[2])
- ]
- )
- self.norm3 = norm_layer(embed_dims[2])
-
- # ----------------------------------#
- # block4
- # ----------------------------------#
- # -----------------------------------------------#
- # 对输入图像进行分区,并下采样
- # 32, 32, 160 => 16, 16, 256 => 256, 256
- # -----------------------------------------------#
- self.patch_embed4 = OverlapPatchEmbed(patch_size=3, stride=2, in_chans=embed_dims[2], embed_dim=embed_dims[3])
- # -----------------------------------------------#
- # 利用transformer模块进行特征提取
- # 256, 256 => 256, 256
- # -----------------------------------------------#
- cur += depths[2]
- self.block4 = nn.ModuleList(
- [
- Block(
- dim=embed_dims[3], num_heads=num_heads[3], mlp_ratio=mlp_ratios[3], qkv_bias=qkv_bias,
- qk_scale=qk_scale,
- drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[cur + i], norm_layer=norm_layer,
- sr_ratio=sr_ratios[3]
- )
- for i in range(depths[3])
- ]
- )
- self.norm4 = norm_layer(embed_dims[3])
-
- self.apply(self._init_weights)
-
- def _init_weights(self, m):
- if isinstance(m, nn.Linear):
- trunc_normal_(m.weight, std=.02)
- if isinstance(m, nn.Linear) and m.bias is not None:
- nn.init.constant_(m.bias, 0)
- elif isinstance(m, nn.LayerNorm):
- nn.init.constant_(m.bias, 0)
- nn.init.constant_(m.weight, 1.0)
- elif isinstance(m, nn.Conv2d):
- fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
- fan_out //= m.groups
- m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))
- if m.bias is not None:
- m.bias.data.zero_()
-
- def forward(self, x):
- B = x.shape[0]
- outs = []
-
- # ----------------------------------#
- # block1
- # ----------------------------------#
- x, H, W = self.patch_embed1.forward(x)
- for i, blk in enumerate(self.block1):
- x = blk.forward(x, H, W)
- x = self.norm1(x)
- x = x.reshape(B, H, W, -1).permute(0, 3, 1, 2).contiguous()
- outs.append(x)
-
- # ----------------------------------#
- # block2
- # ----------------------------------#
- x, H, W = self.patch_embed2.forward(x)
- for i, blk in enumerate(self.block2):
- x = blk.forward(x, H, W)
- x = self.norm2(x)
- x = x.reshape(B, H, W, -1).permute(0, 3, 1, 2).contiguous()
- outs.append(x)
-
- # ----------------------------------#
- # block3
- # ----------------------------------#
- x, H, W = self.patch_embed3.forward(x)
- for i, blk in enumerate(self.block3):
- x = blk.forward(x, H, W)
- x = self.norm3(x)
- x = x.reshape(B, H, W, -1).permute(0, 3, 1, 2).contiguous()
- outs.append(x)
-
- # ----------------------------------#
- # block4
- # ----------------------------------#
- x, H, W = self.patch_embed4.forward(x)
- for i, blk in enumerate(self.block4):
- x = blk.forward(x, H, W)
- x = self.norm4(x)
- x = x.reshape(B, H, W, -1).permute(0, 3, 1, 2).contiguous()
- outs.append(x)
-
- return outs
-
-
- class mit_b0(MixVisionTransformer):
- def __init__(self, pretrained=False):
- super(mit_b0, self).__init__(
- embed_dims=[32, 64, 160, 256], num_heads=[1, 2, 5, 8], mlp_ratios=[4, 4, 4, 4],
- qkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[2, 2, 2, 2], sr_ratios=[8, 4, 2, 1],
- drop_rate=0.0, drop_path_rate=0.1)
- if pretrained:
- print("Load backbone weights")
- self.load_state_dict(torch.load("model_data/segformer_b0_backbone_weights.pth"), strict=False)
-
-
- class mit_b1(MixVisionTransformer):
- def __init__(self, pretrained=False):
- super(mit_b1, self).__init__(
- embed_dims=[64, 128, 320, 512], num_heads=[1, 2, 5, 8], mlp_ratios=[4, 4, 4, 4],
- qkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[2, 2, 2, 2], sr_ratios=[8, 4, 2, 1],
- drop_rate=0.0, drop_path_rate=0.1)
- if pretrained:
- print("Load backbone weights")
- self.load_state_dict(torch.load("model_data/segformer_b1_backbone_weights.pth"), strict=False)
-
-
- class mit_b2(MixVisionTransformer):
- def __init__(self, pretrained=False):
- super(mit_b2, self).__init__(
- embed_dims=[64, 128, 320, 512], num_heads=[1, 2, 5, 8], mlp_ratios=[4, 4, 4, 4],
- qkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[3, 4, 6, 3], sr_ratios=[8, 4, 2, 1],
- drop_rate=0.0, drop_path_rate=0.1)
- if pretrained:
- print("Load backbone weights")
- self.load_state_dict(torch.load("model_data/segformer_b2_backbone_weights.pth"), strict=False)
-
- class mit_b3(MixVisionTransformer):
- def __init__(self, pretrained=False):
- super(mit_b3, self).__init__(
- embed_dims=[64, 128, 320, 512], num_heads=[1, 2, 5, 8], mlp_ratios=[4, 4, 4, 4],
- qkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[3, 4, 18, 3], sr_ratios=[8, 4, 2, 1],
- drop_rate=0.0, drop_path_rate=0.1)
- if pretrained:
- print("Load backbone weights")
- self.load_state_dict(torch.load("model_data/segformer_b3_backbone_weights.pth"), strict=False)
-
-
- class mit_b4(MixVisionTransformer):
- def __init__(self, pretrained=False):
- super(mit_b4, self).__init__(
- embed_dims=[64, 128, 320, 512], num_heads=[1, 2, 5, 8], mlp_ratios=[4, 4, 4, 4],
- qkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[3, 8, 27, 3], sr_ratios=[8, 4, 2, 1],
- drop_rate=0.0, drop_path_rate=0.1)
- if pretrained:
- print("Load backbone weights")
- self.load_state_dict(torch.load("model_data/segformer_b4_backbone_weights.pth"), strict=False)
-
-
- class mit_b5(MixVisionTransformer):
- def __init__(self, pretrained=False):
- super(mit_b5, self).__init__(
- embed_dims=[64, 128, 320, 512], num_heads=[1, 2, 5, 8], mlp_ratios=[4, 4, 4, 4],
- qkv_bias=True, norm_layer=partial(nn.LayerNorm, eps=1e-6), depths=[3, 6, 40, 3], sr_ratios=[8, 4, 2, 1],
- drop_rate=0.0, drop_path_rate=0.1)
- if pretrained:
- print("Load backbone weights")
- self.load_state_dict(torch.load("model_data/segformer_b5_backbone_weights.pth"), strict=False)
-
- class MLP(nn.Module):
- """
- Linear Embedding
- """
-
- def __init__(self, input_dim=2048, embed_dim=768):
- super().__init__()
- self.proj = nn.Linear(input_dim, embed_dim)
-
- def forward(self, x):
- x = x.flatten(2).transpose(1, 2)
- x = self.proj(x)
- return x
-
- class ConvModule(nn.Module):
- def __init__(self, c1, c2, k=1, s=1, p=0, g=1, act=True):
- super(ConvModule, self).__init__()
- self.conv = nn.Conv2d(c1, c2, k, s, p, groups=g, bias=False)
- self.bn = nn.BatchNorm2d(c2, eps=0.001, momentum=0.03)
- self.act = nn.ReLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
-
- def forward(self, x):
- return self.act(self.bn(self.conv(x)))
-
- def fuseforward(self, x):
- return self.act(self.conv(x))
-
-
- class SegFormerHead(nn.Module):
- """
- SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
- """
-
- def __init__(self, num_classes=20, in_channels=[32, 64, 160, 256], embedding_dim=768, dropout_ratio=0.1):
- super(SegFormerHead, self).__init__()
- c1_in_channels, c2_in_channels, c3_in_channels, c4_in_channels = in_channels
-
- self.linear_c4 = MLP(input_dim=c4_in_channels, embed_dim=embedding_dim)
- self.linear_c3 = MLP(input_dim=c3_in_channels, embed_dim=embedding_dim)
- self.linear_c2 = MLP(input_dim=c2_in_channels, embed_dim=embedding_dim)
- self.linear_c1 = MLP(input_dim=c1_in_channels, embed_dim=embedding_dim)
-
- self.linear_fuse = ConvModule(
- c1=embedding_dim * 4,
- c2=embedding_dim,
- k=1,
- )
-
- self.linear_pred = nn.Conv2d(embedding_dim, num_classes, kernel_size=1)
-
- self.dropout = nn.Dropout2d(dropout_ratio)
-
- def forward(self, inputs):
- c1, c2, c3, c4 = inputs
-
- ############## MLP decoder on C1-C4 ###########
- n, _, h, w = c4.shape
-
- _c4 = self.linear_c4(c4).permute(0, 2, 1).reshape(n, -1, c4.shape[2], c4.shape[3])
- _c4 = F.interpolate(_c4, size=c1.size()[2:], mode='bilinear', align_corners=False)
-
- _c3 = self.linear_c3(c3).permute(0, 2, 1).reshape(n, -1, c3.shape[2], c3.shape[3])
- _c3 = F.interpolate(_c3, size=c1.size()[2:], mode='bilinear', align_corners=False)
-
- _c2 = self.linear_c2(c2).permute(0, 2, 1).reshape(n, -1, c2.shape[2], c2.shape[3])
- _c2 = F.interpolate(_c2, size=c1.size()[2:], mode='bilinear', align_corners=False)
-
- _c1 = self.linear_c1(c1).permute(0, 2, 1).reshape(n, -1, c1.shape[2], c1.shape[3])
-
- _c = self.linear_fuse(torch.cat([_c4, _c3, _c2, _c1], dim=1))
-
- x = self.dropout(_c)
- x = self.linear_pred(x)
- # x = self.conv_pred(x)
-
- return x
-
- class SegFormerHead_line(nn.Module):
- """
- SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
- """
-
- def __init__(self, num_classes=20, in_channels=[32, 64, 160, 256], embedding_dim=768, dropout_ratio=0.1):
- super(SegFormerHead_line, self).__init__()
- c1_in_channels, c2_in_channels, c3_in_channels, c4_in_channels = in_channels
-
- self.linear_c4 = MLP(input_dim=c4_in_channels, embed_dim=embedding_dim)
- self.linear_c3 = MLP(input_dim=c3_in_channels, embed_dim=embedding_dim)
- self.linear_c2 = MLP(input_dim=c2_in_channels, embed_dim=embedding_dim)
- self.linear_c1 = MLP(input_dim=c1_in_channels, embed_dim=embedding_dim)
-
- self.linear_fuse = ConvModule(
- c1=embedding_dim * 4,
- c2=embedding_dim,
- k=1,
- )
-
- # self.linear_pred = nn.Conv2d(embedding_dim, num_classes, kernel_size=1)
- self.linear_pred = nn.Sequential(
- nn.Conv2d(embedding_dim, 512, kernel_size=1),
- nn.BatchNorm2d(512),
- nn.ReLU(),
- nn.Upsample(scale_factor=2),
- nn.Conv2d(512, 128, kernel_size=1),
- nn.BatchNorm2d(128),
- nn.ReLU(),
- nn.Upsample(scale_factor=2),
- nn.Conv2d(128, 64, kernel_size=1),
- nn.BatchNorm2d(64),
- nn.ReLU(),
- nn.Conv2d(64, num_classes, kernel_size=1)
-
- )
-
- self.dropout = nn.Dropout2d(dropout_ratio)
-
- def forward(self, inputs):
- c1, c2, c3, c4 = inputs
-
- ############## MLP decoder on C1-C4 ###########
- n, _, h, w = c4.shape
-
- _c4 = self.linear_c4(c4).permute(0, 2, 1).reshape(n, -1, c4.shape[2], c4.shape[3])
- _c4 = F.interpolate(_c4, size=c1.size()[2:], mode='bilinear', align_corners=False)
-
- _c3 = self.linear_c3(c3).permute(0, 2, 1).reshape(n, -1, c3.shape[2], c3.shape[3])
- _c3 = F.interpolate(_c3, size=c1.size()[2:], mode='bilinear', align_corners=False)
-
- _c2 = self.linear_c2(c2).permute(0, 2, 1).reshape(n, -1, c2.shape[2], c2.shape[3])
- _c2 = F.interpolate(_c2, size=c1.size()[2:], mode='bilinear', align_corners=False)
-
- _c1 = self.linear_c1(c1).permute(0, 2, 1).reshape(n, -1, c1.shape[2], c1.shape[3])
-
- _c = self.linear_fuse(torch.cat([_c4, _c3, _c2, _c1], dim=1))
-
- x = self.dropout(_c)
- x = self.linear_pred(x)
- # x = self.conv_pred(x)
-
- return x
-
-
- class SegFormer(nn.Module):
- def __init__(self, num_classes=21, phi='b0', pretrained=False):
- super(SegFormer, self).__init__()
- self.in_channels = {
- 'b0': [32, 64, 160, 256], 'b1': [64, 128, 320, 512], 'b2': [64, 128, 320, 512],
- 'b3': [64, 128, 320, 512], 'b4': [64, 128, 320, 512], 'b5': [64, 128, 320, 512],
- }[phi]
- self.backbone_1 = {
- 'b0': mit_b0, 'b1': mit_b1, 'b2': mit_b2,
- 'b3': mit_b3, 'b4': mit_b4, 'b5': mit_b5,
- }[phi](pretrained)
- self.embedding_dim = {
- 'b0': 768, 'b1': 256, 'b2': 1024,
- 'b3': 1024, 'b4': 768, 'b5': 768,
- }[phi]
-
- self.decode_head_1 = SegFormerHead(num_classes, self.in_channels, self.embedding_dim)
-
- self.memory = Memoy(num_classes, num_feats_per_cls=1)
-
-
- def forward(self, inputs):
- H, W = inputs.size(2), inputs.size(3)
-
- x = self.backbone_1.forward(inputs)
-
- memory_input = x[-1]
- x_m = self.memory.forward(memory_input)
-
- x = self.decode_head_1.forward(x)
-
- x = F.interpolate(x, size=(H, W), mode='bilinear', align_corners=True)
- # sea-land
- x_sea = x[:, 6:7, :, :]
- x_sea = torch.cat([x_sea,torch.zeros(x_sea.shape).to(device)], dim=1)
- return x, x_sea
-
- # def forward_line(self, seg,):
- # '''
- # :param inputs: 原图 seg:分割结果 skip:跳跃链接
- # :return: seg for line
- # '''
- # #获取海岸线注意力图
- # seg = F.softmax(seg,dim=1)
- # x_sea = seg[:, 6:7, :, :].squeeze() #海陆分割图
- # seg = torch.argmax(seg,dim=1).unsqueeze(1)
- # att = line_attention(x_sea).to(device)
- # inputs = torch.cat([att,seg],1)
- # x = self.conv_line(inputs)
- # return x
-
-
- # model = SegFormer(num_classes=7, phi='b0', pretrained=False).to(device)
- #
- # # print(model.named_parameters())
- #
- # '''选择要更新的参数'''
- # pg0, pg1 = [], []
- # for k, v in model.named_parameters():
- # # print(k)
- # v.requires_grad = True
- # if '_2.' in k:
- # pg0.append(v)
- # else:
- # pg1.append(v)
- #
- # print(pg1)
- # model = SegFormer(num_classes=7, phi='b2', pretrained=False).to(device)
- # #
- # # # for k, v in model.named_parameters():
- # # # print(k)
- # #
- # modelPath = r"E:\yqj\try\code\torch\Train\save_model\segformer\pretrain_b2_best.pth"
- # parmeters = torch.load(modelPath, map_location=torch.device("cuda" if torch.cuda.is_available() else "cpu"))
- #
- # part = {k: v for k, v in parmeters.items()}
- # print("============")
- # # print(part.keys())
- # # model.load_state_dict(torch.load(modelPath, map_location=torch.device("cuda" if torch.cuda.is_available() else "cpu")))
- # model.state_dict().update(part)
- #
- # for param in model.backbone_1.parameters():
- # param.requires_grad = False
- #
- # for param in model.decode_head_1.parameters():
- # param.requires_grad = False
- # # pg0, pg1 = [], []
- # # for k, v in model.named_parameters():
- # # # print(k)
- # # if '_2.' in k:
- # # pg0.append(v)
- # # else:
- # # pg1.append(v)
- #
- # model.eval()
- model = SegFormer(num_classes=7, phi='b1', pretrained=False).to(device)
- input = torch.rand((2, 4, 256, 256))
- output,output_sea = model.forward(input)
- print(output.shape)
- # output = model.forward_line(output, )
- # print(output.shape)
|