PyTorch实现GoogLeNet(InceptionNet)

本文档详细介绍了如何使用PyTorch实现GoogLeNet(又称InceptionNet),首先定义了卷积、批归一化和ReLU作为基本组件,接着构建了Inception模块,并测试了其输出。随后,通过堆叠多个Inception模块,构建了一个简化版的GoogLeNet网络,该网络仅有一个输出,而原始模型为避免梯度消失问题设计有多重输出。
import numpy as np
import torch
from torch import nn
from torch.autograd import Variable

定义一个卷积加batchnorm,以及relu激活函数作为基本结构

def conv_relu(in_channel,out_channel, kernel, stride=1, padding=0):
    layer = nn.Sequential(
        nn.Conv2d(in_channel, out_channel, kernel, stride, padding),
        nn.BatchNorm2d(out_channel, eps=1e-3),
        nn.ReLU(True)
    )
    return layer

定义inception模块

class inception(nn.Module):
    def __init__(self, in_channel, out1_1,out2_1, out2_3, out3_1, out3_5,out4_1):
        super(inception, self).__init__()
        
        # 定义inception模块第一条线路
        self.branch1x1 = conv_relu(in_channel,out1_1, 1)
        
        # 定义inception模块第二条线路
        self.branch3x3 = nn.Sequential(
            conv_relu(in_channel, out2_1, 1),
            conv_relu(out2_1, out2_3, 3, padding=1)
        )
        
        #定义inception模块的第三条线路
        self.branch5x5 = nn.Sequential(
            conv_relu(in_channel, out3_1, 1),
            conv_relu(out3_1, out3_5, 5, padding=2)
        )
        
        # 定义inception模块第四条线路
        
        s
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值