深度学习----DenseNet

news2024/10/5 23:28:15

1. 结构图

Input Shape : (3, 7, 7) — Output Shape : (2, 3, 3) — K : (3, 3) — P : (1, 1) — S : (2, 2) — D : (2, 2) — G : 1 

The parts of this post will be divided according to the following arguments. These arguments can be found in the Pytorch documentation of the Conv2d module :

  • in_channels (int) — Number of channels in the input image
  • out_channels (int) — Number of channels produced by the convolution
  • kernel_size (int or tuple) — Size of the convolving kernel
  • stride (int or tuple, optional) — Stride of the convolution. Default: 1
  • padding (int or tuple, optional) — Zero-padding added to both sides of the input. Default: 0
  • dilation (int or tuple, optional) — Spacing between kernel elements. Default: 1
  • groups (int, optional) — Number of blocked connections from input channels to output channels. Default: 1
  • bias (bool, optional) — If True, adds a learnable bias to the output. Default: True

  1.  在Fig 1中, the last layer  is called the translation layer. this layer is responsible for the reducing of the dimension (如图 2, pooling)
  2. 尽管该技术可以阻断identity backpropagation, 但是这仅仅发生在DenseNet block中的部分layers, 并不影响梯度流
  3. DenseNet 的代码结构主要分为以下三块

                #. DenseLayer: 其主要在 DenseBlock 中完成一个单个的layer

                #. DenseBlock:

               #. TransitionLayer

2. 建立模型

O= Size (width) of output image.
I= Size (width) of input image.
N= Size (width) of kernels used in the Conv Layer.
K= Number of kernels.
S= Stride of the convolution operation.
P= Padding.

The size O of the output image is given by

   

 




class _DenseLayer(nn.Module):
    def __init__(
        self, num_input_features: int, growth_rate: int, bn_size: int, drop_rate: float, memory_efficient: bool = False
    ) -> None:
        super().__init__()
        self.norm1 = nn.BatchNorm2d(num_input_features)
        self.relu1 = nn.ReLU(inplace=True)
        self.conv1 = nn.Conv2d(num_input_features, bn_size * growth_rate, kernel_size=1, stride=1, bias=False)

        self.norm2 = nn.BatchNorm2d(bn_size * growth_rate)
        self.relu2 = nn.ReLU(inplace=True)
        self.conv2 = nn.Conv2d(bn_size * growth_rate, growth_rate, kernel_size=3, stride=1, padding=1, bias=False)

        self.drop_rate = float(drop_rate)
        self.memory_efficient = memory_efficient

    def bn_function(self, inputs: List[Tensor]) -> Tensor:
        concated_features = torch.cat(inputs, 1)
        bottleneck_output = self.conv1(self.relu1(self.norm1(concated_features)))  # noqa: T484
        return bottleneck_output

    # todo: rewrite when torchscript supports any
    def any_requires_grad(self, input: List[Tensor]) -> bool:
        for tensor in input:
            if tensor.requires_grad:
                return True
        return False

    @torch.jit.unused  # noqa: T484
    def call_checkpoint_bottleneck(self, input: List[Tensor]) -> Tensor:
        def closure(*inputs):
            return self.bn_function(inputs)

        return cp.checkpoint(closure, *input)

    @torch.jit._overload_method  # noqa: F811
    def forward(self, input: List[Tensor]) -> Tensor:  # noqa: F811
        pass

    @torch.jit._overload_method  # noqa: F811
    def forward(self, input: Tensor) -> Tensor:  # noqa: F811
        pass

    # torchscript does not yet support *args, so we overload method
    # allowing it to take either a List[Tensor] or single Tensor
    def forward(self, input: Tensor) -> Tensor:  # noqa: F811
        if isinstance(input, Tensor):
            prev_features = [input]
        else:
            prev_features = input

        if self.memory_efficient and self.any_requires_grad(prev_features):
            if torch.jit.is_scripting():
                raise Exception("Memory Efficient not supported in JIT")

            bottleneck_output = self.call_checkpoint_bottleneck(prev_features)
        else:
            bottleneck_output = self.bn_function(prev_features)

        new_features = self.conv2(self.relu2(self.norm2(bottleneck_output)))
        if self.drop_rate > 0:
            new_features = F.dropout(new_features, p=self.drop_rate, training=self.training)
        return new_features


class _DenseBlock(nn.ModuleDict):
    _version = 2

    def __init__(
        self,
        num_layers: int,
        num_input_features: int,
        bn_size: int,
        growth_rate: int,
        drop_rate: float,
        memory_efficient: bool = False,
    ) -> None:
        super().__init__()
        for i in range(num_layers):
            layer = _DenseLayer(
                num_input_features + i * growth_rate,
                growth_rate=growth_rate,
                bn_size=bn_size,
                drop_rate=drop_rate,
                memory_efficient=memory_efficient,
            )
            self.add_module("denselayer%d" % (i + 1), layer)

    def forward(self, init_features: Tensor) -> Tensor:
        features = [init_features]
        for name, layer in self.items():
            new_features = layer(features)
            features.append(new_features)
        return torch.cat(features, 1)


class _Transition(nn.Sequential):
    def __init__(self, num_input_features: int, num_output_features: int) -> None:
        super().__init__()
        self.norm = nn.BatchNorm2d(num_input_features)
        self.relu = nn.ReLU(inplace=True)
        self.conv = nn.Conv2d(num_input_features, num_output_features, kernel_size=1, stride=1, bias=False)
        self.pool = nn.AvgPool2d(kernel_size=2, stride=2)


class DenseNet(nn.Module):
    r"""Densenet-BC model class, based on
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_.

    Args:
        growth_rate (int) - how many filters to add each layer (`k` in paper)
        block_config (list of 4 ints) - how many layers in each pooling block
        num_init_features (int) - the number of filters to learn in the first convolution layer
        bn_size (int) - multiplicative factor for number of bottle neck layers
          (i.e. bn_size * k features in the bottleneck layer)
        drop_rate (float) - dropout rate after each dense layer
        num_classes (int) - number of classification classes
        memory_efficient (bool) - If True, uses checkpointing. Much more memory efficient,
          but slower. Default: *False*. See `"paper" <https://arxiv.org/pdf/1707.06990.pdf>`_.
    """

    def __init__(
        self,
        growth_rate: int = 32,
        block_config: Tuple[int, int, int, int] = (6, 12, 24, 16),
        num_init_features: int = 64,
        bn_size: int = 4,
        drop_rate: float = 0,
        num_classes: int = 1000,
        memory_efficient: bool = False,
    ) -> None:

        super().__init__()
        # _log_api_usage_once(self)

        # First convolution
        self.features = nn.Sequential(
            OrderedDict(
                [
                    ("conv0", nn.Conv2d(3, num_init_features, kernel_size=7, stride=2, padding=3, bias=False)),
                    ("norm0", nn.BatchNorm2d(num_init_features)),
                    ("relu0", nn.ReLU(inplace=True)),
                    ("pool0", nn.MaxPool2d(kernel_size=3, stride=2, padding=1)),
                ]
            )
        )

        # Each denseblock
        num_features = num_init_features
        for i, num_layers in enumerate(block_config):
            block = _DenseBlock(
                num_layers=num_layers,
                num_input_features=num_features,
                bn_size=bn_size,
                growth_rate=growth_rate,
                drop_rate=drop_rate,
                memory_efficient=memory_efficient,
            )
            self.features.add_module("denseblock%d" % (i + 1), block)

            num_features = num_features + num_layers * growth_rate

            if i != len(block_config) - 1:
                trans = _Transition(num_input_features=num_features, num_output_features=num_features // 2)
                # self.features.add_module("transition%d" % (i + 1), trans)
                # num_features = num_features // 2

        # Final batch norm
        self.features.add_module("norm5", nn.BatchNorm2d(num_features))

        # Linear layer
        self.classifier = nn.Linear(num_features, num_classes)

        # Official init from torch repo.
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.constant_(m.bias, 0)

    def forward(self, x: Tensor) -> Tensor:
        features = self.features(x)
        out = F.relu(features, inplace=True)
        out = F.adaptive_avg_pool2d(out, (1, 1))
        out = torch.flatten(out, 1)
        out = self.classifier(out)
        return out

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/426126.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

STM32CubeMx+HAL库实现USB CDC+MSC复合设备

之前的文章中介绍过STM32的USB应用&#xff0c;包括虚拟串口&#xff08;CDC&#xff09;和大容量存储设备&#xff08;MSC&#xff09;。今天来介绍USB实现CDC和MSC复合设备的方法。 硬件&#xff1a;STM32F407VET6 软件&#xff1a;STM32CubeMx v6.5F4库v1.27.1 编译环境&a…

行内元素之间出现空白间隙及解决办法

这里的行内元素包括 display 为 inline 和 inline-block 的元素。 基本布局 <div class"container"><span class"item">1</span><span class"item">2</span><span class"item">3</span> …

15. unity官网资源商店的免费资源引入自己项目中

1. 说明 在unity开发中可以在官网引入一些免费的资源&#xff0c;免得自己找不到合适的素材 第一步&#xff1a; 首先进入Unity资源商店官网&#xff0c;https://assetstore.unity.com/&#xff0c;计入并登录自己的unity账号&#xff0c;如果没账号&#xff0c;可以注册一个…

什么是堆叠面积图?如何解读?

简介 在之前的文章中提到过&#xff0c;面积图&#xff08;Area Chart&#xff09;是在折线图的基础上&#xff0c;对折线以下区域进行颜色填充&#xff0c;主要是用于在连续间隔或时间跨度上展示数值。而今天我们要说的是堆叠面积图&#xff08;Stacked Area Chart&#xff0…

一切都是命中注定的!

“光锥之内就是命运”&#xff0c;这是刘慈欣的《三体黑暗森林》里一句话&#xff0c;如果我们看到一件事情正在发生&#xff0c;那么它早在过去无论是几秒前还是几千年前&#xff0c;就已经发生了&#xff0c;我们无法改变这个命运。 孔明叹曰&#xff1a;“谋事在人&#xf…

( “树” 之 DFS) 671. 二叉树中第二小的节点 ——【Leetcode每日一题】

671. 二叉树中第二小的节点 给定一个非空特殊的二叉树&#xff0c;每个节点都是正数&#xff0c;并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话&#xff0c;那么该节点的值等于两个子节点中较小的一个。 更正式地说&#xff0c;即 root.val min(roo…

VSCODE 配置MARKDOWN

参考博客 VSCode如何将Markdown转为PDF 一篇博客让你学会在vscode上编写markdown 1&#xff1a;VSCODE 上配置插件 Markdown All in OneMakrdown Preview EnhancedPaste ImageMarkdown Pdf 2&#xff1a;插件详解 1&#xff1a;Makrdown Preview Enhanced 用来进行MARK…

Harmony OS 开发指南——源码下载和编译

本文介绍了如何下载鸿蒙系统源码&#xff0c;如何一次性配置可以编译三个目标平台&#xff08;Hi3516&#xff0c;Hi3518和Hi3861&#xff09;的编译环境&#xff0c;以及如何将源码编译为三个目标平台的二进制文件。 坑点总结&#xff1a; 下载源码基本上没有太多坑&#xf…

Rocket MQ详解

Rocket MQ 一&#xff0c;是啥&#xff0c;从哪来 RocketMQ是一个开源的分布式消息中间件&#xff0c;最初由阿里巴巴集团开发。它的设计目标是为了在高并发、高吞吐量的场景下&#xff0c;实现可靠的消息传输&#xff0c;并且具有良好的可伸缩性和可扩展性。 RocketMQ支持多…

杨志丰:一文详解,什么是单机分布式一体化?

欢迎访问 OceanBase 官网获取更多信息&#xff1a;https://www.oceanbase.com/ 3 月 25 日&#xff0c;第一届 OceanBase 开发者大会在北京举行&#xff0c;OceanBase 首席架构师杨志丰&#xff08;花名&#xff1a;竹翁&#xff09;带来了 《OceanBase 的单机分布式一体化》 的…

【分享】如何移除PDF密码?

相信不少小伙伴在工作的时候&#xff0c;经常会为了保证PDF文档的安全和私密而给文件设置“打开密码”&#xff0c;但如果后续需要频繁使用该文件&#xff0c;每次打开都要查找输入密码&#xff0c;就会使得工作效率大大降低耽误工作时间。那后续不需要设置保护了&#xff0c;要…

【深度学习】nvidia-smi 各参数意义

文章目录前言1.重点概念解析2.限制GPU显卡功率前言 一个服务器遇到问题了&#xff0c;GPU Fan 和 Perf 两个都是err。之前没遇到这个问题&#xff0c;所以这次机会要搞搞清楚。每个参数都是在干事&#xff0c;能够收到哪些hint&#xff0c;如何查问题。 --------------------…

充电桩测试设备TK4860C交流充电桩检定装置

TK4860C是一款在交流充电桩充电过程中实时检测充电电量的充电桩测试标准仪器&#xff0c;仪器以新能源车为负载&#xff0c;结合宽动态范围测量技术、电能ms级高速刷新等技术&#xff0c;实现充电全过程的累积电能精准计量&#xff0c;相比于传统的预设检定点的稳态计量&#x…

攻防世界Web_php_include

php文件包含题 启动场景 进行代码审计&#xff1a; 对get类型参数进行循环过滤字符串php://&#xff0c;所以考虑其他伪协议&#xff0c;伪协议详见PHP伪协议详解_Snakin_ya的博客-CSDN博客 show_source() 函数 str_replace() 函数 strstr() 函数 方法一 使用data协议 da…

Linux服务使用宝塔面板搭建网站,并发布公网访问 - 内网穿透

文章目录前言1. 环境安装2. 安装cpolar内网穿透3. 内网穿透4. 固定http地址5. 配置二级子域名6. 创建一个测试页面转载自远程内网穿透的文章&#xff1a;Linux使用宝塔面板搭建网站&#xff0c;并内网穿透实现公网访问 前言 宝塔面板作为简单好用的服务器运维管理面板&#xf…

淘宝模拟登录 +淘宝商品详情数据、淘宝商品列表数据爬取

PYTHON环境&#xff1a; * requests库 * time库 * re库 实现思路&#xff1a; * 检查此账号需不需要验证&#xff08;滑动验证/验证码&#xff09; * 浏览器/工具 获取ua和加密后的密码(一劳永逸的方法) * post请求登录url获取st申请url * 根据获得的st申请地址获取st…

Session详解(重点)

类似于去服务器注册账号&#xff0c;只要服务器不停机&#xff0c;自己注册的账号一直会存在服务器。 什么是Session&#xff1a; 1.服务器会给每一个用户&#xff08;浏览器&#xff09;创建一个对象&#xff1b; 2.一个Session独占一个浏览器&#xff0c;只要浏览器没有关…

如何高效清洗数据?试试这款神器

在大数据时代&#xff0c;数据的来源具有多样性、复杂性。 针对数量庞大、渠道及格式多样的数据&#xff0c;数据清洗就成为刚需。 在数据分析中&#xff0c;数据清洗实际上是十分繁重且关键的一步。 Power Query作为数据清洗的工具&#xff0c;能将这些多源的数据集中并统一…

优化城市布局:地下管网解决方案的应用

城市地下管网是城市基础设施中非常重要的一部分&#xff0c;包括排水系统、供水系统、天然气管道、电缆管道等多种管网。然而&#xff0c;城市地下管网也是一个复杂而庞大的系统&#xff0c;由于年久失修和规划不当等原因&#xff0c;经常出现漏水、爆炸、停电等问题&#xff0…

【Unity入门】10.物体的运动

【Unity入门】物体的运动 大家好&#xff0c;我是Lampard~~ 欢迎来到Unity入门系列博客&#xff0c;所学知识来自B站阿发老师~感谢 &#xff08;一&#xff09;用脚本驱动物体移动 &#xff08;1&#xff09;制作一台运动的小车 回顾上一篇文章&#xff0c;我们已经可以用脚本…