GoogLeNet网络的Pytorch实现

1.文章原文地址Goingdeeperwithconvolutions2.文章摘要我们提出了一种代号为Inception的深度卷积神经网络,它在ILSVRC2014的分类和检测任务上都取得当前最佳成绩。这种结构的主要特点是提高了网络内部计算资源的利用率。这是通过精心的设计实现的,它允许增加网络的深度和宽度,同时保持计算预算不变。为了提高效果,这个网络的架构确定是基于Hebbian原则和多尺度处理的...

GoogLeNet网络的Pytorch实现

1.文章原文地址

Going deeper with convolutions

2.文章摘要

我们提出了一种代号为Inception的深度卷积神经网络,它在ILSVRC2014的分类和检测任务上都取得当前最佳成绩。这种结构的主要特点是提高了网络内部计算资源的利用率。这是通过精心的设计实现的,它允许增加网络的深度和宽度,同时保持计算预算不变。为了提高效果,这个网络的架构确定是基于Hebbian原则和多尺度处理的直觉。其中一个典型的实例用于提交到ILSVRC2014上,我们称之为GoogLeNet,它是一个22层的深度网络,该网络的效果通过分类和检测任务来加以评估。

3.网络结构

4.Pytorch实现

  1 import warnings  2 from collections import namedtuple  3 import torch  4 import torch.nn as nn  5 import torch.nn.functional as F  6 from torch.utils.model_zoo import load_url as load_state_dict_from_url  7 from torchsummary import summary  8   9 __all__ = ['GoogLeNet', 'googlenet'] 10  11 model_urls = { 12  # GoogLeNet ported from TensorFlow 13  'googlenet': 'https://download.pytorch.org/models/googlenet-1378be20.pth', 14 } 15  16 _GoogLeNetOuputs = namedtuple('GoogLeNetOuputs', ['logits', 'aux_logits2', 'aux_logits1']) 17  18  19 def googlenet(pretrained=False, progress=True, **kwargs): 20  r"""GoogLeNet (Inception v1) model architecture from 21  `"Going Deeper with Convolutions" <http://arxiv.org/abs/1409.4842>`_. 22  Args: 23   pretrained (bool): If True, returns a model pre-trained on ImageNet 24   progress (bool): If True, displays a progress bar of the download to stderr 25   aux_logits (bool): If True, adds two auxiliary branches that can improve training. 26 Default: *False* when pretrained is True otherwise *True* 27   transform_input (bool): If True, preprocesses the input according to the method with which it 28 was trained on ImageNet. Default: *False* 29  """ 30  if pretrained: 31if 'transform_input' not in kwargs: 32 kwargs['transform_input'] = True 33if 'aux_logits' not in kwargs: 34 kwargs['aux_logits'] = False 35if kwargs['aux_logits']: 36 warnings.warn('auxiliary heads in the pretrained googlenet model are NOT pretrained, ' 37'so make sure to train them') 38original_aux_logits = kwargs['aux_logits'] 39kwargs['aux_logits'] = True 40kwargs['init_weights'] = False 41model = GoogLeNet(**kwargs) 42state_dict = load_state_dict_from_url(model_urls['googlenet'], 43  progress=progress) 44   model.load_state_dict(state_dict) 45if not original_aux_logits: 46 model.aux_logits = False 47 del model.aux1, model.aux2 48return model 49  50  return GoogLeNet(**kwargs) 51  52  53 class GoogLeNet(nn.Module): 54  55  def __init__(self, num_classes=1000, aux_logits=True, transform_input=False, init_weights=True): 56super(GoogLeNet, self).__init__() 57self.aux_logits = aux_logits 58self.transform_input = transform_input 59  60self.conv1 = BasicConv2d(3, 64, kernel_size=7, stride=2, padding=3) 61self.maxpool1 = nn.MaxPool2d(3, stride=2, ceil_mode=True)#向上取整 62self.conv2 = BasicConv2d(64, 64, kernel_size=1) 63self.conv3 = BasicConv2d(64, 192, kernel_size=3, padding=1) 64self.maxpool2 = nn.MaxPool2d(3, stride=2, ceil_mode=True) 65  66self.inception3a = Inception(192, 64, 96, 128, 16, 32, 32) 67self.inception3b = Inception(256, 128, 128, 192, 32, 96, 64) 68self.maxpool3 = nn.MaxPool2d(3, stride=2, ceil_mode=True) 69  70self.inception4a = Inception(480, 192, 96, 208, 16, 48, 64) 71self.inception4b = Inception(512, 160, 112, 224, 24, 64, 64) 72self.inception4c = Inception(512, 128, 128, 256, 24, 64, 64) 73self.inception4d = Inception(512, 112, 144, 288, 32, 64, 64) 74self.inception4e = Inception(528, 256, 160, 320, 32, 128, 128) 75self.maxpool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True) 76  77self.inception5a = Inception(832, 256, 160, 320, 32, 128, 128) 78self.inception5b = Inception(832, 384, 192, 384, 48, 128, 128) 79  80if aux_logits: 81 self.aux1 = InceptionAux(512, num_classes) 82 self.aux2 = InceptionAux(528, num_classes) 83  84self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) 85self.dropout = nn.Dropout(0
源文地址:https://www.guoxiongfei.cn/cntech/17070.html