【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

相关推荐
java_logo2 小时前
LobeHub Docker 容器化部署指南
运维·人工智能·docker·ai·容器·ai编程·ai写作
Pocker_Spades_A2 小时前
在家写的代码,办公室接着改?Jupyter通过cpolar实现远程访问这么玩
ide·python·jupyter
清云逸仙2 小时前
AI Prompt应用实战:评论审核系统实现
人工智能·经验分享·ai·语言模型·prompt·ai编程
正宗咸豆花2 小时前
Prompt Minder:重塑 AI 时代的提示词工程基础设施
人工智能·prompt
清云逸仙3 小时前
使用AI(GPT-4)实现AI prompt 应用--自动审核评论系统
人工智能·经验分享·ai·语言模型·ai编程
m5655bj3 小时前
使用 Python 高效复制 Excel 行、列、单元格
开发语言·python·excel
龙言龙论3 小时前
身份证信息批量处理系统:从入门到实战(附exe工具+核心源码)
数据库·python
m0_626535203 小时前
代码分析 长音频分割为短音频
javascript·python·音视频
Mintopia3 小时前
Claude Code CLI UI
人工智能·aigc·全栈
Mr.Winter`3 小时前
基于Proto3和单例模式的系统参数配置模块设计(附C++案例实现)
c++·人工智能·单例模式·机器人