pytorch 入门 GoogleNet(InceptionNet)

这篇内容并未debug

知识点1、GoogleNet的结构
知识点2、写大型网络的技巧
知识点3、batchnorm
知识点4、不改变图像长宽的s k p
知识点5、 torch.cat((), dim=1) 构造并联网络

知识点1
google net 的结构像是 电路中的 几个并联的串联
在这里插入图片描述

知识点2
对于一个大型的网络(GoogleNet),可以先拆分为几个小的网络(inception),先编好小的网络(inception),然后用小的网络(inception)组成大网络(GoogleNet)。为了变好小网络(inception),可以先编写一个更基础的小小网络(conv_relu),以便可以灵活的调用小网络(inception)

import torch
import numpy as np
from torch.autograd import Variable
from torchvision.datasets import CIFAR10
from torch import nn

知识点3
nn.BatchNorm 批标准化,可以理解为对channel之间的数据处理方式

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.BatchNorm(out_channel, out_channel, eps=1e-3),
	nn.ReLU(True)
	)
	return layer 

知识点4
s = 1 时 k = 1 / k = 3 , padding=1 / k = 5, padding=2 这些情况下的卷积池化不改变图片高宽
知识点5
用out = torch.cat((f1, f2, f3, f4), dim=1) 这种方式 构造“并联”的网络, 记住

class inception(nn.Module):
	def __init__(self, in_channels, out1_1, out2_1, out2_3, out3_1, out3_5, out4_1):
		super(inception, self).__init__()
		self.branch1x1 = conv_relu(in_channels, out1_1, 1)
		self.branch2x2 = nn.Sequential(
		conv_relu(in_channels, out2_1, 1),
		conv_relu(out2_1, out2_3, 3, padding=1)
		)
		self.branch3x3 = nn.Sequential(
		conv_relu(in_channel, out3_1, 1),
		conv_relu(out3_1, out3_5, 5, padding=2)
		)
		self.branch_pool = nn.Sequential(
		nn.MaxPool2d(3, stride=1, padding=1),
		nn.conv_relu(in_channels, out4_1, 1)
		)
	def forward(self, x):
		f1 = self.branch1x1(x)
		f2 = self.branch2x2(x)
		f3 = self.branch3x3(x)
		f4 = self.branch_pool(x)
		out = torch.cat((f1, f2, f3, f4), dim=1)          # 记住在这里插入代码片
		return output

验证

test_net = inception(3, 64, 48, 64, 64, 96, 32)
test_x = Variable(torch.zeros(5, 3, 96, 96))
print('input shape: {} x {} x {} x {}'.format(test_x.shape[0], test_x.shape[1], test_x.shape[2], test_x.shape[3]))
test_y = test_net(test_x)
print('output shape: {} x {} x {} x {}'.format(test_y.shape[0], test_y.shape[1], test_y.shape[2], test_y.shape[3]))
class googlenet(nn.Module):
    def __init__(self, in_channel, num_classes, verbose=False):
        super(googlenet, self).__init__()
        self.verbose = verbose
        self.block1 = nn.Sequential(
            conv_relu(in_channel, out_channel=64, kernel=7, stride=2, padding=3),
            nn.MaxPool2d(3, 2)
        )
        self.block2 = nn.Sequential(
            conv_relu(64, 64, kernel=1),
            conv_relu(64, 192, kernel=3, padding=1),
            nn.MaxPool2d(3, 2)
        )
        self.block3 = nn.Sequential(
            inception(192, 64, 96, 128, 16, 32, 32),
            inception(256, 128, 128, 192, 32, 96, 64),
            nn.MaxPool2d(3, 2)
        )
        self.block4 = nn.Sequential(
            inception(480, 192, 96, 208, 16, 48, 64),
            inception(512, 160, 112, 224, 24, 64, 64),
            inception(512, 128, 128, 256, 24, 64, 64),
            inception(512, 112, 114, 288, 32, 64, 64),
            inception(528, 256, 160, 320, 32, 128, 128),
            nn.MaxPool2d(3, 2)
        )
        self.block5 = nn.Sequential(
            inception(832, 256, 160, 320, 32, 128, 128),
            inception(832, 384, 182, 384, 48, 128, 128),
            nn.AvgPool2d(2)
        )
        self.classifier = nn.Linear(1024, num_classes)
	def forward(self, x):
        x = self.block1(x)
        if self.verbose:
            print('block 1 output: {}'.format(x.shape))
        x = self.block2(x)
        if self.verbose:
            print('block 2 output: {}'.format(x.shape))
        x = self.block3(x)
        if self.verbose:
            print('block 3 output: {}'.format(x.shape))
        x = self.block4(x)
        if self.verbose:
            print('block 4 output: {}'.format(x.shape))
        x = self.block5(x)
        if self.verbose:
            print('block 5 output: {}'.format(x.shape))
        x = x.view(x.shape[0], -1)
        x = self.classifier(x)
        return x
test_net = googlenet(3, 10, True)
test_x = Variable(torch.zeros(1, 3, 96, 96))
test_y = test_net(test_x)
print('output: {}'.format(test_y.shape))
def data_tf(x):
    x = x.resize((96, 96), 2)
    x = np.array(x, dtype='float32') / 255
    x = (x - 0.5) / 0.5
    x = x.transpose((2, 0, 1))
    x = torch.from_numpy(x)
    return x


from torch.utils.data import DataLoader
from jc_utils import train
train_set = CIFAR10('./data', train=True, transform=data_tf)
train_data = DataLoader(train_set, batch_size=64, shuffle=True)
test_set = CIFAR10('./data', train=False, transform=data_tf)
test_data = DataLoader(test_set, batch_size=128, shuffle=False)


net = googlenet(3, 10)
optimizer = torch.optim.SGD(net.parameters(), lr=0.01)
criterion = nn.CrossEntropyLoss()
train(net, train_data, test_data, 20, optimizer, criterion)

完整代码,如果需要,请用这部分代码

import torch
import numpy as np
from torch import nn
from torch.autograd import Variable
from torchvision.datasets import CIFAR10


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


class inception(nn.Module):
    def __init__(self, in_channels, out1_1, out2_1, out2_3, out3_1, out3_5, out4_1):
        super(inception, self).__init__()
        self.branch1x1 = conv_relu(in_channels, out1_1, 1)
        self.branch3x3 = nn.Sequential(
            conv_relu(in_channels, out2_1, 1),
            conv_relu(out2_1, out2_3, 3, padding=1)
        )
        self.branch5x5 = nn.Sequential(
            conv_relu(in_channels, out3_1, 1),
            conv_relu(out3_1, out3_5, 5, padding=2)
        )
        self.branch_pool = nn.Sequential(
            nn.MaxPool2d(3, stride=1, padding=1),
            conv_relu(in_channels, out4_1, 1)
        )

    def forward(self, x):
        f1 = self.branch1x1(x)
        f2 = self.branch3x3(x)
        f3 = self.branch5x5(x)
        f4 = self.branch_pool(x)
        output = torch.cat((f1, f2, f3, f4), dim=1)
        return output


test_net = inception(3, 64, 48, 64, 64, 96, 32)
test_x = Variable(torch.zeros(5, 3, 96, 96))
print('input shape: {} x {} x {} x {}'.format(test_x.shape[0], test_x.shape[1], test_x.shape[2], test_x.shape[3]))
test_y = test_net(test_x)
print('output shape: {} x {} x {} x {}'.format(test_y.shape[0], test_y.shape[1], test_y.shape[2], test_y.shape[3]))


class googlenet(nn.Module):
    def __init__(self, in_channel, num_classes, verbose=False):
        super(googlenet, self).__init__()
        self.verbose = verbose
        self.block1 = nn.Sequential(
            conv_relu(in_channel, out_channel=64, kernel=7, stride=2, padding=3),
            nn.MaxPool2d(3, 2)
        )
        self.block2 = nn.Sequential(
            conv_relu(64, 64, kernel=1),
            conv_relu(64, 192, kernel=3, padding=1),
            nn.MaxPool2d(3, 2)
        )
        self.block3 = nn.Sequential(
            inception(192, 64, 96, 128, 16, 32, 32),
            inception(256, 128, 128, 192, 32, 96, 64),
            nn.MaxPool2d(3, 2)
        )
        self.block4 = nn.Sequential(
            inception(480, 192, 96, 208, 16, 48, 64),
            inception(512, 160, 112, 224, 24, 64, 64),
            inception(512, 128, 128, 256, 24, 64, 64),
            inception(512, 112, 114, 288, 32, 64, 64),
            inception(528, 256, 160, 320, 32, 128, 128),
            nn.MaxPool2d(3, 2)
        )
        self.block5 = nn.Sequential(
            inception(832, 256, 160, 320, 32, 128, 128),
            inception(832, 384, 182, 384, 48, 128, 128),
            nn.AvgPool2d(2)
        )
        self.classifier = nn.Linear(1024, num_classes)


    def forward(self, x):
        x = self.block1(x)
        if self.verbose:
            print('block 1 output: {}'.format(x.shape))
        x = self.block2(x)
        if self.verbose:
            print('block 2 output: {}'.format(x.shape))
        x = self.block3(x)
        if self.verbose:
            print('block 3 output: {}'.format(x.shape))
        x = self.block4(x)
        if self.verbose:
            print('block 4 output: {}'.format(x.shape))
        x = self.block5(x)
        if self.verbose:
            print('block 5 output: {}'.format(x.shape))
        x = x.view(x.shape[0], -1)
        x = self.classifier(x)
        return x

test_net = googlenet(3, 10, True)
test_x = Variable(torch.zeros(1, 3, 96, 96))
test_y = test_net(test_x)
print('output: {}'.format(test_y.shape))





def data_tf(x):
    x = x.resize((96, 96), 2)
    x = np.array(x, dtype='float32') / 255
    x = (x - 0.5) / 0.5
    x = x.transpose((2, 0, 1))
    x = torch.from_numpy(x)
    return x


from torch.utils.data import DataLoader
from jc_utils import train
train_set = CIFAR10('./data', train=True, transform=data_tf)
train_data = DataLoader(train_set, batch_size=64, shuffle=True)
test_set = CIFAR10('./data', train=False, transform=data_tf)
test_data = DataLoader(test_set, batch_size=128, shuffle=False)


net = googlenet(3, 10)
optimizer = torch.optim.SGD(net.parameters(), lr=0.01)
criterion = nn.CrossEntropyLoss()
train(net, train_data, test_data, 20, optimizer, criterion)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用PyTorch训练GoogleNet,可以按照以下步骤操作: 1. 准备数据集:首先需要准备图片数据集,可以使用PyTorch提供的torchvision.datasets.ImageFolder类加载数据集。 2. 定义模型:使用PyTorch定义GoogleNet模型,可以参考PyTorch官方提供的实现或者自己实现。 3. 定义损失函数:根据任务需要选择适当的损失函数,比如交叉熵损失函数。 4. 定义优化器:选择适当的优化器进行模型参数的优化,比如SGD或Adam。 5. 训练模型:使用训练集对模型进行训练,可以使用PyTorch提供的torch.utils.data.DataLoader类进行数据加载,使用torch.optim提供的优化器对模型进行优化,使用torch.nn提供的损失函数计算损失。 6. 评估模型:使用测试集对模型进行评估,可以使用PyTorch提供的torch.utils.data.DataLoader类进行数据加载,使用torch.nn提供的损失函数计算损失和准确率等指标。 7. 保存模型:在训练完成后,可以使用torch.save函数将训练好的模型保存到文件中,以备后续使用。 下面是一个简单的示例代码,可以帮助你更好地理解训练GoogleNet的过程: ```python import torch import torch.nn as nn import torch.optim as optim import torchvision.datasets as datasets import torchvision.transforms as transforms from torch.utils.data import DataLoader from torchvision.models import googlenet # 定义超参数 batch_size = 64 num_epochs = 10 learning_rate = 0.01 momentum = 0.9 # 准备数据集 train_dataset = datasets.ImageFolder('path/to/train/dataset', transform=transforms.ToTensor()) test_dataset = datasets.ImageFolder('path/to/test/dataset', transform=transforms.ToTensor()) # 定义数据加载器 train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False) # 定义模型 model = googlenet(pretrained=False, num_classes=10) # 定义损失函数和优化器 criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=learning_rate, momentum=momentum) # 训练模型 for epoch in range(num_epochs): for i, (images, labels) in enumerate(train_loader): # 前向传播 outputs = model(images) loss = criterion(outputs, labels) # 反向传播和优化 optimizer.zero_grad() loss.backward() optimizer.step() if (i+1) % 100 == 0: print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' .format(epoch+1, num_epochs, i+1, len(train_loader), loss.item())) # 评估模型 with torch.no_grad(): correct = 0 total = 0 for images, labels in test_loader: outputs = model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Accuracy of the model on the test images: {} %'.format(100 * correct / total)) # 保存模型 torch.save(model.state_dict(), 'googlenet.pth') ``` 注意,上述代码中的路径需要替换为实际的数据集路径。另外,在定义模型时,我们使用了PyTorch提供的预训练的GoogleNet模型,并将输出层的类别数设为10,因为我们的数据集有10个类别。如果你要训练的是其他类型的数据集,需要相应地修改输出层的类别数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值