1 Star 0 Fork 0

Hui Li(李辉)/densefuse-pytorch

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
fusion_strategy.py 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
Hui Li(李辉) 提交于 5年前 . codes
import torch
EPSILON = 1e-10
# addition fusion strategy
def addition_fusion(tensor1, tensor2):
return (tensor1 + tensor2)/2
# attention fusion strategy, average based on weight maps
def attention_fusion_weight(tensor1, tensor2):
# avg, max, nuclear
f_spatial = spatial_fusion(tensor1, tensor2)
tensor_f = f_spatial
return tensor_f
def spatial_fusion(tensor1, tensor2, spatial_type='sum'):
shape = tensor1.size()
# calculate spatial attention
spatial1 = spatial_attention(tensor1, spatial_type)
spatial2 = spatial_attention(tensor2, spatial_type)
# get weight map, soft-max
spatial_w1 = torch.exp(spatial1) / (torch.exp(spatial1) + torch.exp(spatial2) + EPSILON)
spatial_w2 = torch.exp(spatial2) / (torch.exp(spatial1) + torch.exp(spatial2) + EPSILON)
spatial_w1 = spatial_w1.repeat(1, shape[1], 1, 1)
spatial_w2 = spatial_w2.repeat(1, shape[1], 1, 1)
tensor_f = spatial_w1 * tensor1 + spatial_w2 * tensor2
return tensor_f
# spatial attention
def spatial_attention(tensor, spatial_type='sum'):
if spatial_type is 'mean':
spatial = tensor.mean(dim=1, keepdim=True)
elif spatial_type is 'sum':
spatial = tensor.sum(dim=1, keepdim=True)
return spatial
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/hli1221/densefuse-pytorch.git
git@gitee.com:hli1221/densefuse-pytorch.git
hli1221
densefuse-pytorch
densefuse-pytorch
master

搜索帮助