import torch
import torch.nn as nn
from functools import partial
import torch.nn.functional as F
import math
from timm.models.layers import trunc_normal_tf_
from timm.models.helpers import named_apply
def gcd(a, b):
while b:
a, b = b, a % b
return a
# Other types of layers can go here (e.g., nn.Linear, etc.)
def _init_weights(module, name, scheme=''):
if isinstance(module, nn.Conv2d) or isinstance(module, nn.Conv3d):
if scheme == 'normal':
nn.init.normal_(module.weight, std=.02)
if module.bias is not None:
nn.init.zeros_(module.bias)
elif scheme == 'trunc_normal':
trunc_normal_tf_(module.weight, std=.02)
if module.bias is not None:
nn.init.zeros_(module.bias)
elif scheme == 'xavier_normal':
nn.init.xavier_normal_(module.weight)
if module.bias is not None:
nn.init.zeros_(module.bias)
elif scheme == 'kaiming_normal':
nn.init.kaiming_normal_(module.weight, mode='fan_out', nonlinearity='relu')
if module.bias is not None:
nn.init.zeros_(module.bias)
else:
# efficientnet like
fan_out = module.kernel_size[0] * module.kernel_size[1] * module.out_channels
fan_out //= module.groups
nn.init.normal_(module.weight, 0, math.sqrt(2.0 / fan_out))
if module.bias is not None:
nn.init.zeros_(module.bias)
elif isinstance(module, nn.BatchNorm2d) or isinstance(module, nn.BatchNorm3d):
nn.init.constant_(module.weight, 1)
nn.init.constant_(module.bias, 0)
elif isinstance(module, nn.LayerNorm):
nn.init.constant_(module.weight, 1)
nn.init.constant_(module.bias, 0)
def act_layer(act, inplace=False, neg_slope=0.2, n_prelu=1):
# activation layer
act = act.lower()
if act == 'relu':
layer = nn.ReLU(inplace)
elif act == 'relu6':
layer = nn.ReLU6(inplace)
elif act == 'leakyrelu':
layer = nn.LeakyReLU(neg_slope, inplace)
elif act == 'prelu':
layer = nn.PReLU(num_parameters=n_prelu, init=neg_slope)
elif act == 'gelu':
layer = nn.GELU()
elif act == 'hswish':
layer = nn.Hardswish(inplace)
else:
raise NotImplementedError('activation layer [%s] is not found' % act)
return layer
class LGAG(nn.Module):
def __init__(self, F_g, F_l, F_int, kernel_size=3, groups=1, activation='relu'):
super(LGAG, self).__init__()
if kernel_size == 1:
groups = 1
self.W_g = nn.Sequential(
nn.Conv2d(F_g, F_int, kernel_size=kernel_size, stride=1, padding=kernel_size // 2, groups=groups,
bias=True),
nn.BatchNorm2d(F_int)
)
self.W_x = nn.Sequential(
nn.Conv2d(F_l, F_int, kernel_size=kernel_size, stride=1, padding=kernel_size // 2, groups=groups,
bias=True),
nn.BatchNorm2d(F_int)
)
self.psi = nn.Sequential(
nn.Conv2d(F_int, 1, kernel_size=1, stride=1, padding=0, bias=True),
nn.BatchNorm2d(1),
nn.Sigmoid()
)
self.activation = act_layer(activation, inplace=True)
def forward(self, x):
#print(f"Output size: {x[1].size()}")
g1 = self.W_g(x[0])
x1 = self.W_x(x[1])
psi = self.activation(g1 + x1)
psi = self.psi(psi)
if x[1].size() != psi.size():
# print(f"Resizing psi from {psi.size()} to {x[1].size()}")
# 只改变空间尺寸,不改变通道数
# x[1].size()[2:] 获取的是高和宽(height 和 width)
psi = F.interpolate(psi, size=x[1].size()[2:], mode='bilinear', align_corners=False)
return x[1] * psi
elif m is LGAG:
# f should be a list of two layers' indices
if isinstance(f, list) and len(f) == 2:
c1, c2 = ch[f[0]], ch[f[1]] # Get feature map channels from two layers
else:
raise ValueError(f"LGAG expects two input layers, but got {f}")
F_int = args[0] # Intermediate channels for LGAG
args = [c1, c2, F_int, *args[1:]]
我们引入了一种新的大核分组注意门(LGAG),逐步结合特征图和注意系数,通过网络学习,允许相关特征的激活和抑制不相关特征。该过程采用来自更高层次特征的门控信号来控制网络不同阶段的信息流,从而提高其医学图像分割的精度。