import torch
import torch.nn as nn
from torch.nn import functional as F
from utils.CovKernelFW import get_kernels
from utils.FeatureMap import GenLikeMap
from tensorboardX import SummaryWriter
from torchvision.utils import make_grid
global global_step
global_step = 0
conv_writer = SummaryWriter(comment='--conv')
class FENetwFW(nn.Module):
"""
基于固定权值卷积核的特征提取模块
A feature extraction network based on convolution kernel with fixed weight.(FENetwFW)
"""
def __init__(self):
super(FENetwFW, self).__init__()
kernels = [get_kernels(i) for i in range(1, 6)] # 获取各种尺寸的卷积核
self.weights = [
nn.Parameter(data=torch.FloatTensor(k).unsqueeze(0).unsqueeze(0), requires_grad=False).cuda()
for ks in kernels for k in ks
] # 将卷积核转换为成pytorch中的格式
def forward(self, img):
feature_maps = [img] # 融合的特征
for ws in self.weights: # 用各个卷积核对图像进行卷积,提取不同的特征图
feature_maps.append(F.conv2d(img, ws, stride=1, padding = 'same'))
feature_maps = torch.cat(feature_maps, dim=1) # 对各个卷积核卷积的结果进行融合
conv_writer.add_image('固定权值卷积',
make_grid(torch.unsqueeze(feature_maps[0], dim=0).transpose(0, 1), normalize=True,
nrow=3),
global_step=global_step)
return feature_maps
class FENetwVW(nn.Module):
"""
基于变化权值卷积核的特征提取模块
A Feature extraction network based on convolution kernel with variable weight
"""
def __init__(self):
super(FENetwVW, self).__init__()
self.c1 = nn.Conv2d(16, 32, kernel_size=(11, 11), padding='same', stride=(1, 1), bias=None) # Convolution_1
# torch.nn.init.xavier_normal_(self.c1.weight, gain=1.0)
self.c2 = P1C2(32, 64) # Pooling_1 & Convolution_2
self.c3 = P2C3(64, 128) # Pooling_2 & Convolution_3
self.FCsubnet = FCsubnet(128, 256) # Feature concatenation subnetwork
self.c5 = nn.Conv2d(768, 128, kernel_size=(1, 1), padding='same', stride=(1, 1), bias=None) # Convolution_5
def forward(self, fw_out):
global global_step
c1 = self.c1(fw_out)
conv_writer.add_image('c1',
make_grid(torch.unsqueeze(c1[0], dim=0).transpose(0, 1), normalize=True, nrow=4),
global_step=global_step)
c2 = self.c2(c1)
conv_writer.add_image('c2',
make_grid(torch.unsqueeze(c2[0], dim=0).transpose(0, 1), normalize=True, nrow=4),
global_step=global_step)
c3 = self.c3(c2)
conv_writer.add_image('c3',
make_grid(torch.unsqueeze(c3[0], dim=0).transpose(0, 1), normalize=True, nrow=4),
global_step=global_step)
FC_subnet = self.FCsubnet(c3)
conv_writer.add_image('特征级联子网络',
make_grid(torch.unsqueeze(FC_subnet[0], dim=0).transpose(0, 1), normalize=True, nrow=4),
global_step=global_step)
c5 = self.c5(FC_subnet)
conv_writer.add_image('c5',
make_grid(torch.unsqueeze(c5[0], dim=0).transpose(0, 1), normalize=True, nrow=4),
global_step=global_step)
global_step += 1
return c5
class FMNet(nn.Module):
"""
特征图映射
Feature mapping process
"""
def __init__(self):
super(FMNet, self).__init__()
self.sigmoid = torch.nn.Sigmoid()
def forward(self, FV, batch_size, W, H):
# first64 = FV[:, :64, :, :] # 背景似然
last64 = FV[:, 64:, :, :] # 目标似然
# bg_likemap = GenLikeMap(first64, batch_size, W, H)
tg_likemap = GenLikeMap(last64, batch_size, W, H)
# bg_likelihood = self.sigmoid(
# bg_likemap)
tg_likelihood = self.sigmoid(
tg_likemap)
return torch.unsqueeze(tg_likelihood, dim=1)
class FCsubnet(nn.Module):
"""
特征级联子网络
A Feature concatenation subnetwork
"""
def __init__(self, in_c, out_c):
super(FCsubnet, self).__init__()
self.reorg = ReOrg() # 特征重组
self.p3c4 = P3C4(in_c, out_c) # Pooling_3 & Convolution_4
def forward(self, c3):
return torch.cat([self.reorg(c3), self.p3c4(c3)], dim=1) # 特征拼接
class ReOrg(nn.Module):
"""
特征重组
"""
def __init__(self):
super(ReOrg, self).__init__()
def forward(self, p2c3):
w = p2c3.shape[2]
h = p2c3.shape[3]
pink = p2c3[:, :, :w // 2, :h // 2]
green = p2c3[:, :, w // 2:, :h // 2]
purple = p2c3[:, :, :w // 2, h // 2:]
red = p2c3[:, :, w // 2:, h // 2:]
return torch.cat([pink, green, purple, red], dim=1)
class P1C2(nn.Module):
def __init__(self, in_c, out_c):
super(P1C2, self).__init__()
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(in_c, out_c, kernel_size=(7, 7), padding='same', stride=(1, 1), bias=None)
def forward(self, c1):
p1 = self.pool1(c1)
c2 = self.conv2(p1)
return c2
class P2C3(nn.Module):
def __init__(self, in_c, out_c):
super(P2C3, self).__init__()
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv3 = nn.Conv2d(in_c, out_c, kernel_size=(5, 5), padding='same', stride=(1, 1), bias=None)
def forward(self, c2):
p2 = self.pool2(c2)
c3 = self.conv3(p2)
return c3
class P3C4(nn.Module):
def __init__(self, in_c, out_c):
super(P3C4, self).__init__()
self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv4 = nn.Conv2d(in_c, out_c, kernel_size=(3, 3), padding='same', stride=(1, 1), bias=None)
def forward(self, c3):
p3 = self.pool3(c3)
c4 = self.conv4(p3)
return c4
.whl
- 粉丝: 3960
- 资源: 4905
最新资源
- 发那科系统整套梯形图设计 FANUC全套PMC设计 发那科标 准PLC 完美解决方案 ##带中文解释## 内容很全 请仔细看完: 1.刀库程序设计(斗笠 圆盘 夹臂 机械手 伞型 都包
- stm32 gd32爱玛电动车控制器资料 电动车控制器原理图、PCB和程序 大厂成熟电机foc控制 送eg89m52的原理图和pcb
- 基于Go语言的现代化开源K8s面板——1Panel官方出品KubePi设计源码
- 基于JavaScript的Sewise Player网页HTML5视频播放器设计源码
- 小神农V7(修复眼镜).zip
- 基于Vue框架的银行科技岗AI云账户系统前端设计源码
- Screenshot_2025-01-15-00-13-41-471_com.tencent.mtt.jpg
- Screenshot_2025-01-15-00-08-47-906_com.tencent.mobileqq.jpg
- 250467c518b0a66217a647d11a6a9c831736870765675.jpg
- 基于Tcl语言的GNU TeXmacs插件集合设计源码
- Screenshot_2025-01-15-00-04-33-283_com.kuaishou.nebula.jpg
- 基于Vue框架的easy-send局域网文本文件共享小工具设计源码
- 基于JavaScript和微信小程序技术的钓场信息预约与管理小程序设计源码
- PSO-RBF和RBF粒子群优化径向基神经网络多输入多输出预测(Matlab完整源码和数据)
- 三菱fx3u+485ADP-MB与3台台达变频器modbus通讯程序 功能:通过三菱fx3u 485ADP-MB板对3台台达变频器进行modbus通讯,实现频率设定,启停控制,输出频率读取,输出电压读
- 除尘程序 写的FB块了,可以直接调用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈