本文已参与「新人创作礼」活动,一起开启掘金创作之路。
一、GoggLeNet 网络的特点
GoogLeNet是2014年提出的一种全新的深度学习结构,在这之前的AlexNet、VGG等结构都是通过增大网络的深度(层数)来获得更好的训练效果,但层数的增加会带来很多负作用,比如overfit、梯度消失、梯度爆炸等。GoogLeNet通过引入inception从另一种角度来提升训练结果:能更高效的利用计算资源,在相同的计算量下能提取到更多的特征,从而提升训练结果。
网络亮点:
(1)引入inception结构。
inception中包含了1x1,3x3,5x5等不同大小的卷积核,从而将不同尺度的特征信息融合到一起。
inception通过并联多个卷积层,增加了网络层的宽度,打破了以往通过增加网络层深度来提升网络性能的思路。
(2)inception中引入1x1卷积层用于降低channel,从而降低模型参数。
(3)丢弃了全连接层,引入平均池化层,大大减少了模型参数。
(4)引入两个辅助分类器帮助训练。
研究发现神经网络的中间层也具有很强的识别能力,因此在某些中间层中添加分类器,输出预测结果,就这是辅助分类器。
使用辅助分类器可以增加低层网络的分类能力,防止梯度消失,增加正则化。
GoggLeNet网络共有三个输出,一个是最终输出(权重为0.4),还有两个辅助分类器的输出(权重均为0.3)
二、代码结构
注:dataset.py,train.py,predict.py的大部分内容与 图像分类:AlexNet网络、五分类 flower 数据集、pytorch 的代码一致,详细的代码注释可以看AlexNet这篇博客。
三、dataset.py
import os
import json
import torch
from torchvision import transforms, datasets
def dataset(batch_size):
train_path = "flower_data/train"
val_path = "flower_data/val"
assert os.path.exists(train_path), "{} path does not exist.".format(train_path)
nw = min([os.cpu_count(), batch_size if batch_size > 1 else 0, 8]) # number of workers
print('Using {} dataloader workers every process'.format(nw))
data_transform = {
"train": transforms.Compose([transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]),
"val": transforms.Compose([transforms.Resize((224, 224)), # cannot 224, must (224, 224)
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])}
train_dataset = datasets.ImageFolder(root=train_path,transform=data_transform["train"])
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,
shuffle=True, num_workers=nw)
validate_dataset = datasets.ImageFolder(root=val_path, transform=data_transform["val"])
valid_loader = torch.utils.data.DataLoader(validate_dataset, batch_size=batch_size,
shuffle=True, num_workers=nw)
train_num = len(train_dataset)
val_num = len(validate_dataset)
print(f"using {train_num} images for training, {val_num} images for valid.")
flower_class_id = train_dataset.class_to_idx
cla_dict = dict((val, key) for key, val in flower_class_id.items())
json_str = json.dumps(cla_dict, indent=4)
with open('class_indices.json', 'w') as json_file:
json_file.write(json_str)
return train_loader,valid_loader,val_num
四、model.py
import torch.nn as nn
import torch
import torch.nn.functional as F
class GoogLeNet(nn.Module):
def __init__(self, num_classes=1000, aux_logits=True, init_weights=False):
super(GoogLeNet, self).__init__()
self.aux_logits = aux_logits
# aux_logits表示是否使用辅助分类器,只有在训练中使用aux,在测试中不使用。
self.conv1 = BasicConv2d(3, 64, kernel_size=7, stride=2, padding=3)
self.maxpool1 = nn.MaxPool2d(3, stride=2, ceil_mode=True)
# 如果通过MaxPool2d得到的size是小数,设置ceil_mode=True则向上取整,默认值为ceil_mode=False表示向下取整。
self.conv2 = BasicConv2d(64, 64, kernel_size=1)
self.conv3 = BasicConv2d(64, 192, kernel_size=3, padding=1)
self.maxpool2 = nn.MaxPool2d(3, stride=2, ceil_mode=True)
self.inception3a = Inception(192, 64, 96, 128, 16, 32, 32)
self.inception3b = Inception(256, 128, 128, 192, 32, 96, 64)
self.maxpool3 = nn.MaxPool2d(3, stride=2, ceil_mode=True)
self.inception4a = Inception(480, 192, 96, 208, 16, 48, 64)
self.inception4b = Inception(512, 160, 112, 224, 24, 64, 64)
self.inception4c = Inception(512, 128, 128, 256, 24, 64, 64)
self.inception4d = Inception(512, 112, 144, 288, 32, 64, 64)
self.inception4e = Inception(528, 256, 160, 320, 32, 128, 128)
self.maxpool4 = nn.MaxPool2d(3, stride=2, ceil_mode=True)
self.inception5a = Inception(832, 256, 160, 320, 32, 128, 128)
self.inception5b = Inception(832, 384, 192, 384, 48, 128, 128)
if self.aux_logits:
self.aux1 = InceptionAux(512, num_classes)
self.aux2 = InceptionAux(528, num_classes)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
"""
自适应平均池化,输出大小为1x1
采用nn.AdaptiveAvgPool2d的好处是无论输入多大尺寸的图像,都可以得到1x1的输出
而nn.AvgPool2d只能对特定输入尺寸的图像进行操作
"""
self.dropout = nn.Dropout(0.4)
self.fc = nn.Linear(1024, num_classes)
if init_weights:
self._initialize_weights()
def forward(self, x):
# N x 3 x 224 x 224
x = self.conv1(x)
# N x 64 x 112 x 112
x = self.maxpool1(x)
# N x 64 x 56 x 56
x = self.conv2(x)
# N x 64 x 56 x 56
x = self.conv3(x)
# N x 192 x 56 x 56
x = self.maxpool2(x)
# N x 192 x 28 x 28
x = self.inception3a(x)
# N x 256 x 28 x 28
x = self.inception3b(x)
# N x 480 x 28 x 28
x = self.maxpool3(x)
# N x 480 x 14 x 14
x = self.inception4a(x)
# N x 512 x 14 x 14
if self.training and self.aux_logits: # eval model lose this layer
aux1 = self.aux1(x)
x = self.inception4b(x)
# N x 512 x 14 x 14
x = self.inception4c(x)
# N x 512 x 14 x 14
x = self.inception4d(x)
# N x 528 x 14 x 14
if self.training and self.aux_logits: # eval model lose this layer
aux2 = self.aux2(x)
x = self.inception4e(x)
# N x 832 x 14 x 14
x = self.maxpool4(x)
# N x 832 x 7 x 7
x = self.inception5a(x)
# N x 832 x 7 x 7
x = self.inception5b(x)
# N x 1024 x 7 x 7
x = self.avgpool(x)
# N x 1024 x 1 x 1
x = torch.flatten(x, 1)
# N x 1024
x = self.dropout(x)
x = self.fc(x)
# N x 1000 (num_classes)
"""
训练过程中输出x和两个辅助分类器,即三个输出。
验证和测试过程中只输出x,辅助分类器不起作用。
"""
if self.training and self.aux_logits:
return x, aux1, aux2
return x
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
nn.init.constant_(m.bias, 0)
class Inception(nn.Module):
def __init__(self, in_channels, ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_proj):
super(Inception, self).__init__()
"""
Inception结构有四个分支(branch),从左到右依次是branch1、branch2、branch3、branch4
参数说明:
ch1x1, ch3x3red, ch3x3, ch5x5red, ch5x5, pool_proj 表示对对应操作的卷积核个数(即通道数)
ch1x1:branch1的1x1卷积的channel
ch3x3red:branch2的1x1卷积的channel,因为1x1卷积的目的是用小channel减少参数数量,所以叫做red(reduce)
ch3x3:branch2的3x3卷积的channel
ch5x5red:branch3的1x1卷积的channel
ch5x5:branch3的5x5卷积的channel
pool_proj:branch4的1x1卷积的channel,branch4中第一层的MaxPool不改变channel,所以不需要设置MaxPool的channel
"""
self.branch1 = BasicConv2d(in_channels, ch1x1, kernel_size=1)
self.branch2 = nn.Sequential(
BasicConv2d(in_channels, ch3x3red, kernel_size=1),
BasicConv2d(ch3x3red, ch3x3, kernel_size=3, padding=1)
# padding=1是为了保证输入和输出的H、W相等,因为四个branch合并时要保证H和W相等
)
self.branch3 = nn.Sequential(
BasicConv2d(in_channels, ch5x5red, kernel_size=1),
BasicConv2d(ch5x5red, ch5x5, kernel_size=5, padding=2)
# padding=1是为了保证输入和输出的H、W相等
)
self.branch4 = nn.Sequential(
nn.MaxPool2d(kernel_size=3, stride=1, padding=1),
BasicConv2d(in_channels, pool_proj, kernel_size=1)
)
def forward(self, x):
branch1 = self.branch1(x)
branch2 = self.branch2(x)
branch3 = self.branch3(x)
branch4 = self.branch4(x)
outputs = [branch1, branch2, branch3, branch4]
return torch.cat(outputs, 1)
# 在第一维(channel)对outputs进行合并,outputs的通道排列为 [N,C,H,W]
# 辅助分类器
class InceptionAux(nn.Module):
def __init__(self, in_channels, num_classes):
super(InceptionAux, self).__init__()
self.averagePool = nn.AvgPool2d(kernel_size=5, stride=3)
self.conv = BasicConv2d(in_channels, 128, kernel_size=1) # output[batch, 128, 4, 4]
self.fc1 = nn.Linear(2048, 1024)
self.fc2 = nn.Linear(1024, num_classes)
def forward(self, x):
# aux1是辅助分类器一,aux2是辅助分类器二
# aux1: N x 512 x 14 x 14, aux2: N x 528 x 14 x 14
x = self.averagePool(x)
# aux1: N x 512 x 4 x 4, aux2: N x 528 x 4 x 4
x = self.conv(x)
# N x 128 x 4 x 4
x = torch.flatten(x, 1)
x = F.dropout(x, 0.5, training=self.training)
"""
这里用的是F.dropout,而不是nn.dropout,其实二者功能是一样的,只是参数写法不同。
self.training是随着运行过程变化的:
在model.train()中,self.training=True,启用dropout
在model.eval()中,self.training=False,屏蔽dropout
注意参数self.training不需要自己定义,是nn.Module自带的参数。
"""
# N x 2048
x = F.relu(self.fc1(x), inplace=True)
x = F.dropout(x, 0.5, training=self.training)
# N x 1024
x = self.fc2(x)
# N x num_classes
return x
# 将Conv2d和ReLU结合到一起
# 因为每次做完Conv2d都有跟一个ReLU,所以为了方便可以把二者合在一起
class BasicConv2d(nn.Module):
def __init__(self, in_channels, out_channels, **kwargs):
super(BasicConv2d, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, **kwargs)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv(x)
x = self.relu(x)
return x
五、train.py
import sys
import torch
import torch.nn as nn
import torch.optim as optim
from tqdm import tqdm
from model import GoogLeNet
from dataset import dataset
def train():
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("using {} device.".format(device))
epochs = 5
batch_size = 32
train_loader, valid_loader, val_num=dataset(batch_size)
model = GoogLeNet(num_classes=5, aux_logits=True, init_weights=True)
model.to(device)
loss_function = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.0003)
best_acc = 0.0
save_path = './GoogleNet.pth'
train_steps = len(train_loader)
for epoch in range(epochs):
# train
model.train()
running_loss = 0.0
train_bar = tqdm(train_loader, file=sys.stdout)
for step, data in enumerate(train_bar):
images, labels = data
optimizer.zero_grad()
logits, aux_logits2, aux_logits1 = model(images.to(device))
loss0 = loss_function(logits, labels.to(device))
loss1 = loss_function(aux_logits1, labels.to(device))
loss2 = loss_function(aux_logits2, labels.to(device))
# 训练时使用两个辅助分类器,三个输入loss的权重分别为0.4,0.3,0.3
loss = loss0 + loss1 * 0.3 + loss2 * 0.3
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
train_bar.desc = "train epoch[{}/{}] loss:{:.3f}".format(epoch + 1, epochs, loss)
# validate
model.eval()
acc = 0.0 # accumulate accurate number / epoch
with torch.no_grad():
val_bar = tqdm(valid_loader, file=sys.stdout)
for val_data in val_bar:
val_images, val_labels = val_data
# 测试时不使用辅助分类器,只有一个输出
outputs = model(val_images.to(device))
predict_y = torch.max(outputs, dim=1)[1]
acc += torch.eq(predict_y, val_labels.to(device)).sum().item()
val_accurate = acc / val_num
print('[epoch %d] train_loss: %.3f val_accuracy: %.3f' %
(epoch + 1, running_loss / train_steps, val_accurate))
if val_accurate > best_acc:
best_acc = val_accurate
torch.save(model.state_dict(), save_path)
print('Finished Training')
if __name__ == '__main__':
train()
训练结果(cpu训练速度太慢没有跑完):
六、predict.py
import os
import json
import torch
from PIL import Image
from torchvision import transforms
import matplotlib.pyplot as plt
from model import GoogLeNet
def main():
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
data_transform = transforms.Compose(
[transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
# load image
img_path = "flower_data/tulip.png"
assert os.path.exists(img_path), "file: '{}' dose not exist.".format(img_path)
img = Image.open(img_path)
plt.imshow(img)
img = data_transform(img)
# expand batch dimension
img = torch.unsqueeze(img, dim=0) # [N, C, H, W]
# read class_indict
json_path = './class_indices.json'
assert os.path.exists(json_path), "file: '{}' dose not exist.".format(json_path)
json_file = open(json_path, "r")
class_indict = json.load(json_file)
# create model
model = GoogLeNet(num_classes=5, aux_logits=False).to(device)
# load model weights
weights_path = "./GoogleNet.pth"
assert os.path.exists(weights_path), "file: '{}' dose not exist.".format(weights_path)
# missing_keys, unexpected_keys =
model.load_state_dict(torch.load(weights_path, map_location=device),
strict=False)
model.eval()
with torch.no_grad():
# predict class
output = torch.squeeze(model(img.to(device))).cpu()
predict = torch.softmax(output, dim=0)
predict_cla = torch.argmax(predict).numpy()
print_res = "class: {} prob: {:.3}".format(class_indict[str(predict_cla)],
predict[predict_cla].numpy())
plt.title(print_res)
for i in range(len(predict)):
print("class: {:10} prob: {:.3}".format(class_indict[str(i)],
predict[i].numpy()))
plt.show()
if __name__ == '__main__':
main()
测试结果:
class: daisy prob: 0.000195
class: dandelion prob: 8.31e-06
class: roses prob: 0.213
class: sunflowers prob: 9.43e-05
class: tulips prob: 0.787
测试图片及类别预测: