jittor.models.alexnet 源代码

# ***************************************************************
# 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
import jittor.nn as nn

__all__ = ['AlexNet', 'alexnet']

[文档] class AlexNet(nn.Module): ''' AlexNet模型源于 `ImageNet Classification with Deep Convolutional Neural Networks <https://papers.nips.cc/paper/2012/hash/c399862d3b9d6b76c8436e924a68c45b-Abstract.html>`__, 其架构来自于论文 `One weird trick for parallelizing convolutional neural networks <https://arxiv.org/abs/1404.5997>`__。 参数: - num_classes (int, optional): 分类任务中类别的数量, 默认值: 1000。 属性: - features (nn.Sequential): AlexNet模型的特征提取部分。 - avgpool (nn.AdaptiveAvgPool2d): 用于将特征提取部分的输出转换为固定大小的特征图。 - classifier (nn.Sequential): AlexNet模型的分类部分。 代码示例: >>> import jittor as jt >>> from jittor import nn >>> from jittor.models import AlexNet >>> model = AlexNet(500) # 创建一个有500个类别的AlexNet模型 >>> x = jt.random([10, 3, 224, 224]) # 创建一个随机的图像数据批次 >>> y = model(x) # 得到模型的输出 >>> # y的形状将会是 [10, 500], 表示10幅图像在500个类别上的分类结果 ''' def __init__(self, num_classes=1000): super(AlexNet, self).__init__() self.features = nn.Sequential( nn.Conv(3, 64, kernel_size=11, stride=4, padding=2), nn.Relu(), nn.Pool(kernel_size=3, stride=2, op='maximum'), nn.Conv(64, 192, kernel_size=5, padding=2), nn.Relu(), nn.Pool(kernel_size=3, stride=2, op='maximum'), nn.Conv(192, 384, kernel_size=3, padding=1), nn.Relu(), nn.Conv(384, 256, kernel_size=3, padding=1), nn.Relu(), nn.Conv(256, 256, kernel_size=3, padding=1), nn.Relu(), nn.Pool(kernel_size=3, stride=2, op='maximum') ) self.avgpool = nn.AdaptiveAvgPool2d((6, 6)) self.classifier = nn.Sequential( nn.Dropout(), nn.Linear(((256 * 6) * 6), 4096), nn.Relu(), nn.Dropout(), nn.Linear(4096, 4096), nn.Relu(), nn.Linear(4096, num_classes) ) def execute(self, x): x = self.features(x) x = self.avgpool(x) x = jt.reshape(x, (x.shape[0], (- 1))) x = self.classifier(x) return x
[文档] def alexnet(pretrained=False, **kwargs): ''' 构建一个AlexNet模型 AlexNet模型源于 `ImageNet Classification with Deep Convolutional Neural Networks <https://papers.nips.cc/paper/2012/hash/c399862d3b9d6b76c8436e924a68c45b-Abstract.html>`__ 。 参数: - `pretrained` (bool, optional): 表示是否预加载预训练模型。默认为 `False`。 - `**kwargs` (dict, optional): AlexNet模型的其他参数。默认为 `None。 返回值: - 返回构建好的AlexNet模型实例。如果 `pretrained` 为 `True`, 则返回在ImageNet上预训练的模型。 代码示例: >>> import jittor as jt >>> from jittor.models.alexnet import * >>> net = alexnet(pretrained=False) >>> x = jt.rand(1, 3, 224, 224) >>> y = net(x) >>> y.shape [1, 1000] ''' model = AlexNet(**kwargs) if pretrained: model.load("jittorhub://alexnet.pkl") return model