基Pytorch实现GoogleNet模型实现图片分类,能输出分类结果的那种,附代码
时间: 2023-06-24 19:02:56 浏览: 77
以下是基于PyTorch实现的GoogleNet模型代码,可以用于图片分类,并输出分类结果:
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
class Inception_Module(nn.Module):
def __init__(self, input_channels, kernel_1x1, reduce_3x3, kernel_3x3, reduce_5x5, kernel_5x5, pool_proj):
super(Inception_Module, self).__init__()
# 1x1 conv branch
self.branch1 = nn.Conv2d(input_channels, kernel_1x1, kernel_size=1)
# 1x1 conv -> 3x3 conv branch
self.branch2 = nn.Sequential(
nn.Conv2d(input_channels, reduce_3x3, kernel_size=1),
nn.ReLU(inplace=True),
nn.Conv2d(reduce_3x3, kernel_3x3, kernel_size=3, padding=1)
)
# 1x1 conv -> 5x5 conv branch
self.branch3 = nn.Sequential(
nn.Conv2d(input_channels, reduce_5x5, kernel_size=1),
nn.ReLU(inplace=True),
nn.Conv2d(reduce_5x5, kernel_5x5, kernel_size=5, padding=2)
)
# 3x3 max pool -> 1x1 conv branch
self.branch4 = nn.Sequential(
nn.MaxPool2d(kernel_size=3, stride=1, padding=1),
nn.Conv2d(input_channels, pool_proj, kernel_size=1)
)
def forward(self, x):
branch1_output = self.branch1(x)
branch2_output = self.branch2(x)
branch3_output = self.branch3(x)
branch4_output = self.branch4(x)
outputs = [branch1_output, branch2_output, branch3_output, branch4_output]
return torch.cat(outputs, 1)
class GoogleNet(nn.Module):
def __init__(self, num_classes=1000):
super(GoogleNet, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)
self.max_pool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.conv2a = nn.Conv2d(64, 64, kernel_size=1)
self.conv2b = nn.Conv2d(64, 192, kernel_size=3, padding=1)
self.max_pool2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.inception3a = Inception_Module(192, 64, 96, 128, 16, 32, 32)
self.inception3b = Inception_Module(256, 128, 128, 192, 32, 96, 64)
self.max_pool3 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.inception4a = Inception_Module(480, 192, 96, 208, 16, 48, 64)
self.inception4b = Inception_Module(512, 160, 112, 224, 24, 64, 64)
self.inception4c = Inception_Module(512, 128, 128, 256, 24, 64, 64)
self.inception4d = Inception_Module(512, 112, 144, 288, 32, 64, 64)
self.inception4e = Inception_Module(528, 256, 160, 320, 32, 128, 128)
self.max_pool4 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.inception5a = Inception_Module(832, 256, 160, 320, 32, 128, 128)
self.inception5b = Inception_Module(832, 384, 192, 384, 48, 128, 128)
self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
self.dropout = nn.Dropout(p=0.4)
self.fc1 = nn.Linear(1024, num_classes)
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.max_pool1(x)
x = F.relu(self.conv2a(x))
x = F.relu(self.conv2b(x))
x = self.max_pool2(x)
x = self.inception3a(x)
x = self.inception3b(x)
x = self.max_pool3(x)
x = self.inception4a(x)
x = self.inception4b(x)
x = self.inception4c(x)
x = self.inception4d(x)
x = self.inception4e(x)
x = self.max_pool4(x)
x = self.inception5a(x)
x = self.inception5b(x)
x = self.avg_pool(x)
x = x.view(x.size(0), -1)
x = self.dropout(x)
x = self.fc1(x)
return x
```
使用时,可以像下面这样调用:
```python
model = GoogleNet(num_classes=10) # num_classes指定分类的类别数
input = torch.randn(1, 3, 224, 224) # 输入图片大小为224x224
output = model(input)
pred = output.argmax(dim=1)
print(pred)
```
其中,`model(input)`会输出一个大小为`(1, num_classes)`的张量,每一行表示一张图片对应的各个类别的概率。`pred`是预测出来的类别,即概率最大的那个类别。
阅读全文