【Python/Pytorch - 网络模型】-- TV Loss损失函数

文章目录

文章目录

  • [00 写在前面](#00 写在前面)
  • [01 基于Pytorch版本的TV Loss代码](#01 基于Pytorch版本的TV Loss代码)
  • [02 论文下载](#02 论文下载)

00 写在前面

在医学图像重建过程中,经常在代价方程中加入TV 正则项,该正则项作为去噪项,对于重建可以起到很大帮助作用。但是对于一些纹理细节要求较高的任务,加入TV 正则项,在一定程度上可能会降低纹理细节。

对于连续函数,其表达式为:

对于图片而言,即为离散的数值,求每一个像素和横向下一个像素的差的平方,加上纵向下一个像素的差的平方,再开β/2次根:

01 基于Pytorch版本的TV Loss代码

python 复制代码
import torch
from torch.autograd import Variable


class TVLoss(torch.nn.Module):
    """
    TV loss
    """

    def __init__(self, weight=1):
        super(TVLoss, self).__init__()
        self.weight = weight

    def forward(self, x):
        batch_size = x.size()[0]
        h_x = x.size()[2]
        w_x = x.size()[3]
        count_h = self._tensor_size(x[:, :, 1:, :])
        count_w = self._tensor_size(x[:, :, :, 1:])
        h_tv = torch.pow((x[:, :, 1:, :] - x[:, :, :h_x - 1, :]), 2).sum()
        w_tv = torch.pow((x[:, :, :, 1:] - x[:, :, :, :w_x - 1]), 2).sum()
        return self.weight * 2 * (h_tv / count_h + w_tv / count_w) / batch_size

    def _tensor_size(self, t):
        return t.size()[1] * t.size()[2] * t.size()[3]


if __name__ == "__main__":
    x = Variable(
        torch.FloatTensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]]).view(1, 2, 3, 3),
        requires_grad=True)
    tv = TVLoss()
    result = tv(x)
    print(result)

02 论文下载

Understanding Deep Image Representations by Inverting Them

相关推荐
min1811234561 分钟前
PC端零基础跨职能流程图制作教程
大数据·人工智能·信息可视化·架构·流程图
愚公搬代码14 分钟前
【愚公系列】《AI+直播营销》015-直播的选品策略(设计直播产品矩阵)
人工智能·线性代数·矩阵
静听松涛13319 分钟前
中文PC端多人协作泳道图制作平台
大数据·论文阅读·人工智能·搜索引擎·架构·流程图·软件工程
GIS之路33 分钟前
GDAL 实现矢量裁剪
前端·python·信息可视化
学历真的很重要39 分钟前
LangChain V1.0 Context Engineering(上下文工程)详细指南
人工智能·后端·学习·语言模型·面试·职场和发展·langchain
IT=>小脑虎39 分钟前
Python零基础衔接进阶知识点【详解版】
开发语言·人工智能·python
智航GIS41 分钟前
10.6 Scrapy:Python 网页爬取框架
python·scrapy·信息可视化
UnderTurrets1 小时前
A_Survey_on_3D_object_Affordance
pytorch·深度学习·计算机视觉·3d
koo3641 小时前
pytorch深度学习笔记13
pytorch·笔记·深度学习
黄焖鸡能干四碗1 小时前
智能制造工业大数据应用及探索方案(PPT文件)
大数据·运维·人工智能·制造·需求分析