jittor.models.googlenet 源代码

# ***************************************************************
# Copyright (c) 2023 Jittor. All Rights Reserved. 
# Maintainers: 
#     Wenyang Zhou <576825820@qq.com>
#     Dun Liang <randonlang@gmail.com>. 
# 
# This file is subject to the terms and conditions defined in
# file 'LICENSE.txt', which is part of this source code package.
# ***************************************************************
# This model is generated by pytorch converter.
import jittor as jt
from jittor import nn

__all__ = ['GoogLeNet', 'googlenet']

[文档] def googlenet(pretrained=False, **kwargs): ''' 构建一个GoogLeNet模型 GoogLeNet源自论文 `Going Deeper with Convolutions <https://arxiv.org/abs/1409.4842>`__ , 它由多个称为Inception模块的子网络构成。 参数: - `pretrained` (bool, optional): 表示是否预加载预训练模型。默认为 `False`。 - `kwargs`: 其他optional参数。 返回值: - 返回构建好的googlenet模型实例。如果 `pretrained` 为 `True`, 则返回在ImageNet上预训练的模型。 代码示例: >>> import jittor as jt >>> from jittor.models.googlenet import * >>> net = googlenet(pretrained=False) >>> x = jt.rand(1, 3, 224, 224) >>> y = net(x) >>> y.shape [1, 1000] ''' model = GoogLeNet(**kwargs) if pretrained: model.load("jittorhub://googlenet.pkl") return model
[文档] class GoogLeNet(nn.Module): ''' GoogLeNet源自论文 `Going Deeper with Convolutions <https://arxiv.org/abs/1409.4842>`__ , 也称为Inception v1, 它由多个称为Inception模块的子网络构成。 注意: - 输入数据的维度应当是[batch_size, 3, height, width], 且height和width应该足够大, 以保证在网络结构中可以经历多次池化操作后依然保有空间分辨率。 - 如果关闭了aux_logits, aux1和aux2的输出将为None。 参数: - num_classes (int, optional): 分类的类别数量, 默认值: 1000。 - aux_logits (bool, optional): 若为True, 则添加辅助分支以帮助训练。默认值: 1000。 - init_weights (bool, optional): 若为True, 则对模型的权重进行初始化。默认值: True。 - blocks (list[nn.Module]): 由三个模块组成的列表, 依次为[conv_block, inception_block, inception_aux_block]。若为None, 则使用[BasicConv2d, Inception, InceptionAux]作为默认值。默认值: None。 属性: - conv1 (nn.Module): 输入层的卷积层。 - maxpool1 (nn.Pool): 输入层的最大池化层。 - conv2 (nn.Module): 第二个卷积层。 - conv3 (nn.Module): 第三个卷积层。 - maxpool2 (nn.Pool): 第二个最大池化层。 - inception3a (nn.Module): 第一个Inception模块。 - inception3b (nn.Module): 第二个Inception模块。 - maxpool3 (nn.Pool): 第三个最大池化层。 - inception4a (nn.Module): 第三个Inception模块。 - inception4b (nn.Module): 第四个Inception模块。 - inception4c (nn.Module): 第五个Inception模块。 - inception4d (nn.Module): 第六个Inception模块。 - inception4e (nn.Module): 第七个Inception模块。 - maxpool4 (nn.Pool): 第四个最大池化层。 - inception5a (nn.Module): 第八个Inception模块。 - inception5b (nn.Module): 第九个Inception模块。 - aux1 (nn.Module): 第一个辅助分类器。 - aux2 (nn.Module): 第二个辅助分类器。 - avgpool (nn.AdaptiveAvgPool2d): 全局平均池化层。 - dropout (nn.Dropout): 丢弃层。 - fc (nn.Linear): 全连接层。 代码示例: >>> import jittor as jt >>> from jittor.models.googlenet import GoogLeNet >>> model = GoogLeNet(num_classes=1000, aux_logits=True, init_weights=True) >>> input_tensor = jt.randn(1, 3, 224, 224) >>> model(input_tensor).shape [1,1000,] ''' def __init__(self, num_classes=1000, aux_logits=True, init_weights=True, blocks=None): super(GoogLeNet, self).__init__() if (blocks is None): blocks = [BasicConv2d, Inception, InceptionAux] assert (len(blocks) == 3) conv_block = blocks[0] inception_block = blocks[1] inception_aux_block = blocks[2] self.aux_logits = aux_logits self.conv1 = conv_block(3, 64, kernel_size=7, stride=2, padding=3) self.maxpool1 = nn.Pool(3, stride=2, ceil_mode=True, op='maximum') self.conv2 = conv_block(64, 64, kernel_size=1) self.conv3 = conv_block(64, 192, kernel_size=3, padding=1) self.maxpool2 = nn.Pool(3, stride=2, ceil_mode=True, op='maximum') self.inception3a = inception_block(192, 64, 96, 128, 16, 32, 32) self.inception3b = inception_block(256, 128, 128, 192, 32, 96, 64) self.maxpool3 = nn.Pool(3, stride=2, ceil_mode=True, op='maximum') self.inception4a = inception_block(480, 192, 96, 208, 16, 48, 64) self.inception4b = inception_block(512, 160, 112, 224, 24, 64, 64) self.inception4c = inception_block(512, 128, 128, 256, 24, 64, 64) self.inception4d = inception_block(512, 112, 144, 288, 32, 64, 64) self.inception4e = inception_block(528, 256, 160, 320, 32, 128, 128) self.maxpool4 = nn.Pool(2, stride=2, ceil_mode=True, op='maximum') self.inception5a = inception_block(832, 256, 160, 320, 32, 128, 128) self.inception5b = inception_block(832, 384, 192, 384, 48, 128, 128) if aux_logits: self.aux1 = inception_aux_block(512, num_classes) self.aux2 = inception_aux_block(528, num_classes) else: self.aux1 = None self.aux2 = None self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.dropout = nn.Dropout(0.2) self.fc = nn.Linear(1024, num_classes) def _forward(self, x): x = self.conv1(x) x = self.maxpool1(x) x = self.conv2(x) x = self.conv3(x) x = self.maxpool2(x) x = self.inception3a(x) x = self.inception3b(x) x = self.maxpool3(x) x = self.inception4a(x) if (self.aux1 is not None): aux1 = self.aux1(x) x = self.inception4b(x) x = self.inception4c(x) x = self.inception4d(x) if (self.aux2 is not None): aux2 = self.aux2(x) x = self.inception4e(x) x = self.maxpool4(x) x = self.inception5a(x) x = self.inception5b(x) x = self.avgpool(x) x = jt.reshape(x, (x.shape[0], (- 1))) x = self.dropout(x) x = self.fc(x) return (x, aux2, aux1) def eager_outputs(self, x, aux2, aux1): return x def execute(self, x): (x, aux1, aux2) = self._forward(x) aux_defined = (self.aux_logits) return self.eager_outputs(x, aux2, aux1)
class Inception(nn.Module): ''' Inception模块是一种结构化的多分支神经网络架构单元, 该架构在GoogleNet中被广泛使用。它允许模型在同一层上学习多种尺度的特征。 Inception模块包含四个分支: - 1x1的卷积 - 1x1的卷积接3x3的卷积 - 1x1的卷积接5x5的卷积 - 最大池化层接1x1的卷积 通过1x1卷积分支可以进行特征降维来减少计算复杂度。此模块的每个分支都可以被认为是执行不同类型的特征提取。分支之间的结果会按深度(通道数)连接在一起, 从而在单个卷积层中进行多尺度特征的融合。 参数: - in_channels (int): 输入通道的数量。 - ch1x1 (int): 第一个分支的卷积层的输出通道数。 - ch3x3red (int): 为3x3卷积层之前的1x1卷积做减维所用的输出通道数。 - ch3x3 (int): 第二个分支中3x3卷积层后的输出通道数。 - ch5x5red (int): 为5x5卷积层之前的1x1卷积做减维所用的输出通道数。 - ch5x5 (int): 第三个分支中5x5卷积层后的输出通道数。 - pool_proj (int): 最大池化之后1x1卷积的输出通道数。 - conv_block (callable, optional): 用于构建卷积层的类或函数。 `None` 表示 `BasicConv2d`。默认值: `None` 代码示例: >>> import jittor as jt >>> from jittor.models.googlenet import Inception >>> inc = Inception(in_channels=192, ch1x1=64, ch3x3red=96, ch3x3=128, ... ch5x5red=16, ch5x5=32, pool_proj=32) >>> input = jt.randn(1, 192, 28, 28) >>> inc(input).shape [1,256,28,28,] >>> # 四个分支分别生成 64, 128, 32, 32 通道并在深度上拼接 ''' def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_proj, conv_block=None): super(Inception, self).__init__() if (conv_block is None): conv_block = BasicConv2d self.branch1 = conv_block(in_channels, ch1x1, kernel_size=1) self.branch2 = nn.Sequential(conv_block(in_channels, ch3x3red, kernel_size=1), conv_block(ch3x3red, ch3x3, kernel_size=3, padding=1)) self.branch3 = nn.Sequential(conv_block(in_channels, ch5x5red, kernel_size=1), conv_block(ch5x5red, ch5x5, kernel_size=3, padding=1)) self.branch4 = nn.Sequential(nn.Pool(kernel_size=3, stride=1, padding=1, ceil_mode=True, op='maximum'), conv_block(in_channels, pool_proj, kernel_size=1)) def _forward(self, x): branch1 = self.branch1(x) branch2 = self.branch2(x) branch3 = self.branch3(x) branch4 = self.branch4(x) outputs = [branch1, branch2, branch3, branch4] return outputs def execute(self, x): outputs = self._forward(x) return jt.concat(outputs, dim=1) class InceptionAux(nn.Module): def __init__(self, in_channels, num_classes, conv_block=None): super(InceptionAux, self).__init__() if (conv_block is None): conv_block = BasicConv2d self.conv = conv_block(in_channels, 128, kernel_size=1) self.fc1 = nn.Linear(2048, 1024) self.fc2 = nn.Linear(1024, num_classes) def execute(self, x): x = nn.AdaptiveAvgPool2d(4)(x) x = self.conv(x) x = jt.reshape(x, (x.shape[0], (- 1))) x = nn.relu(self.fc1(x)) x = nn.Dropout(0.7)(x) x = self.fc2(x) return x class BasicConv2d(nn.Module): def __init__(self, in_channels, out_channels, **kwargs): super(BasicConv2d, self).__init__() self.conv = nn.Conv(in_channels, out_channels, bias=False, **kwargs) self.bn = nn.BatchNorm(out_channels, eps=0.001) def execute(self, x): x = self.conv(x) x = self.bn(x) return nn.relu(x)