【第一周】附:猫狗大战

第五部分 猫狗大战(Colab)

5.1 基础知识储备

1.Pytorch:transforms的二十二个方法,推荐博客链接:

5.2 代码详解

1.导入相关库,检查是否在使用GPU设备(Colab)

import numpy as np
import matplotlib.pyplot as plt
import os
import torch
import torch.nn as nn
import torchvision
from torchvision import models,transforms,datasets
import time
import json


# 判断是否存在GPU设备
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print('Using gpu: %s ' % torch.cuda.is_available())

2.下载数据集(缩小版数据集)

! wget http://fenggao-image.stor.sinaapp.com/dogscats.zip
! unzip dogscats.zip

3.数据处理

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

vgg_format = transforms.Compose([
                transforms.CenterCrop(224),
                transforms.ToTensor(),
                normalize,
            ])

data_dir = './dogscats'

dsets = {x: datasets.ImageFolder(os.path.join(data_dir, x), vgg_format)
         for x in ['train', 'valid']}

dset_sizes = {x: len(dsets[x]) for x in ['train', 'valid']}
dset_classes = dsets['train'].classes

代码详解:

transforms.Normalize():对图片进行标准化处理,使用Imagenet的均值和标准差
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]为Imagenet预设模型transforms.Compose():串联多个图片变换的操作

CenterCrop(224):依据给定的size从中心裁剪

transforms.ToTensor():将PIL Image(Python Image Library)或者ndarray(N维数组对象)转化为tensor(张量),并且归一化至[0-1]

datasets.ImageFolder(os.path.join(data_dir, x):加载该路径的数据

下图为dsets的属性:

# 通过下面代码可以查看 dsets 的一些属性

print(dsets['train'].classes)
print(dsets['train'].class_to_idx)
print(dsets['train'].imgs[:5])
print('dset_sizes: ', dset_sizes)
loader_train = torch.utils.data.DataLoader(dsets['train'], batch_size=64, shuffle=True, num_workers=6)
loader_valid = torch.utils.data.DataLoader(dsets['valid'], batch_size=5, shuffle=False, num_workers=6)


'''
valid 数据一共有2000张图,每个batch是5张,因此,下面进行遍历一共会输出到 400
同时,把第一个 batch 保存到 inputs_try, labels_try,分别查看
'''
count = 1
for data in loader_valid:
    print(count, end='\n')
    if count == 1:
        inputs_try,labels_try = data
    count +=1

print(labels_try)
print(inputs_try.shape)

代码详解:

loader_train = torch.utils.data.DataLoader(dsets['train'], batch_size=64, shuffle=True, num_workers=6):

输入的数据类型为dataset;每次输入数据的行数为batch_size;shuffle为true,即将输入数据的顺序打乱(数据为无序列特征的可以使用);num_workers:使用6个子进程来导入数据。

显示图片

# 显示图片的小程序

def imshow(inp, title=None):
#   Imshow for Tensor.
    inp = inp.numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    inp = np.clip(std * inp + mean, 0,1)
    plt.imshow(inp)
    if title is not None:
        plt.title(title)
    plt.pause(0.001)  # pause a bit so that plots are updated
    
    
# 显示 labels_try 的5张图片,即valid里第一个batch的5张图片
out = torchvision.utils.make_grid(inputs_try)
imshow(out, title=[dset_classes[x] for x in labels_try])

4.创建VGG Model

直接使用预训练好的VGG模型进行预测,使用softmax对结果进行预处理,展示识别结果。

softmax详解,

#下载ImgaeNet1000个类的JSON文件
!wget https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json

model_vgg = models.vgg16(pretrained=True)

with open('./imagenet_class_index.json') as f:
    class_dict = json.load(f)
dic_imagenet = [class_dict[str(i)][1] for i in range(len(class_dict))]

inputs_try , labels_try = inputs_try.to(device), labels_try.to(device)
model_vgg = model_vgg.to(device)

outputs_try = model_vgg(inputs_try)

print(outputs_try)
print(outputs_try.shape)

'''
可以看到结果为5行,1000列的数据,每一列代表对每一种目标识别的结果。
但是我也可以观察到,结果非常奇葩,有负数,有正数,
为了将VGG网络输出的结果转化为对每一类的预测概率,我们把结果输入到 Softmax 函数
'''
m_softm = nn.Softmax(dim=1)
probs = m_softm(outputs_try)
vals_try,pred_try = torch.max(probs,dim=1)

print( 'prob sum: ', torch.sum(probs,1))
print( 'vals_try: ', vals_try)
print( 'pred_try: ', pred_try)

print([dic_imagenet[i] for i in pred_try.data])
imshow(torchvision.utils.make_grid(inputs_try.data.cpu()), 
       title=[dset_classes[x] for x in labels_try.data.cpu()])

代码详解:

VGG16详细过程,见博客https://blog.csdn.net/qq_43270687/article/details/93471659

inputs_try.to(device):将所有最开始读取数据时的tensor变量copy一份到device所指定的GPU上去,之后的运算都在GPU上进行。

print(model_vgg)

model_vgg_new = model_vgg;

for param in model_vgg_new.parameters():
    param.requires_grad = False
model_vgg_new.classifier._modules['6'] = nn.Linear(4096, 2)
model_vgg_new.classifier._modules['7'] = torch.nn.LogSoftmax(dim = 1)

model_vgg_new = model_vgg_new.to(device)

print(model_vgg_new.classifier)

代码详解

model_vgg_new.parameters():返回一个迭代器,迭代器每次生成的是Tensor类型的数据。

param.requires_grad:所有的tensor都有.requires_grad属性,requires_grad=True(要求梯度)

kernel_size表示卷积核的大小为3X3的,stride表示步长,padding表示的是填充值。

5.修改最后一层,冻结前面层的参数

'''
第一步:创建损失函数和优化器

损失函数 NLLLoss() 的 输入 是一个对数概率向量和一个目标标签. 
它不会为我们计算对数概率,适合最后一层是log_softmax()的网络. 
'''
criterion = nn.NLLLoss()

# 学习率
lr = 0.001

# 随机梯度下降
optimizer_vgg = torch.optim.SGD(model_vgg_new.classifier[6].parameters(),lr = lr)

'''
第二步:训练模型
'''

def train_model(model,dataloader,size,epochs=1,optimizer=None):
    model.train()
    
    for epoch in range(epochs):
        running_loss = 0.0
        running_corrects = 0
        count = 0
        for inputs,classes in dataloader:
            inputs = inputs.to(device)
            classes = classes.to(device)
            outputs = model(inputs)
            loss = criterion(outputs,classes)           
            optimizer = optimizer
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            _,preds = torch.max(outputs.data,1)
            # statistics
            running_loss += loss.data.item()
            running_corrects += torch.sum(preds == classes.data)
            count += len(inputs)
            print('Training: No. ', count, ' process ... total: ', size)
        epoch_loss = running_loss / size
        epoch_acc = running_corrects.data.item() / size
        print('Loss: {:.4f} Acc: {:.4f}'.format(
                     epoch_loss, epoch_acc))
        
        
# 模型训练
train_model(model_vgg_new,loader_train,size=dset_sizes['train'], epochs=1, 
            optimizer=optimizer_vgg)

上述代码采用的SGD优化器,且epoch为1,在接下来的代码中,修改为Adam优化器,epoch的次数选择为10。

def train_model(model,criterion,optimizer,num_epochs):

    model.train()
    print(0)
#保存验证集上准确率最高的模型
    best_model = copy.deepcopy(model.state_dict())
    best_acc = 0.0
    
    for epoch in range(num_epochs):
                  
        running_loss = 0.0
        running_corrects = 0
        for inputs,labels in loader_train:
            
            inputs = inputs.to(device)
            labels = labels.to(device)
            
            optimizer.zero_grad()
            outputs = model(inputs)
            _,preds = torch.max(outputs,1)
            
            loss = criterion(outputs,labels)
            loss.backward()
            optimizer.step()
                
            running_loss += loss.item()
            running_corrects += (preds == labels).sum().item()
        
        epoch_loss = running_loss / dset_sizes['train']
        epoch_acc = running_corrects / dset_sizes['train']
        
        print("Train Loss:{:.4f}  Acc:{:.4f}".format(epoch_loss,epoch_acc))
        
        with torch.set_grad_enabled(False):
            model.eval()
            running_loss = 0.0
            running_corrects = 0
            for inputs,labels in loader_valid:
                inputs = inputs.to(device)
                labels = labels.to(device)
                outputs = model(inputs)
                _,preds = torch.max(outputs,1)
                loss = criterion(outputs,labels)
                running_loss += loss.item()
                running_corrects += (preds == labels).sum().item()
            epoch_loss = running_loss / dset_sizes['valid']
            epoch_acc = running_corrects / dset_sizes['valid']
            print("Valid Loss:{:.4f}  Acc:{:.4f}".format(epoch_loss,epoch_acc))
            if  epoch_acc > best_acc:
                best_model = copy.deepcopy(model.state_dict())
                best_acc = epoch_acc
    print("Best val Acc:{:.4f}".format(best_acc))
    model.load_state_dict(best_model)        
    return model

def test_model(model):
    pred = []
    for inputs in test_loader:
        inputs = inputs.to(device)
        outputs = model(inputs)
        _,preds = torch.max(outputs,1)
        for i in preds:
            pred.append(i.item())
    return pred
    
model_vgg = models.vgg19(pretrained=True)
for param in model_vgg.features.parameters():
    param.requires_grad = False
model_vgg.classifier._modules['0'] = nn.Linear(25088, 1024)
model_vgg.classifier._modules['3'] = nn.Linear(1024, 128)
model_vgg.classifier._modules['6'] = nn.Linear(128, 2)
model_vgg.to(device)



criterion = nn.CrossEntropyLoss()

# Observe that all parameters are being optimized
#Adam是一种学习率自适应的算法
optimizer = optim.Adam(model_vgg.parameters(), lr=0.001)      
model = train_model(model_vgg, criterion, optimizer, 
                       num_epochs=10)        

pre = test_model(model)

import csv       
f = open('result.csv','w',encoding='utf-8',newline="")       
csv_writer = csv.writer(f)
for i,pred in enumerate(pre):a
    csv_writer.writerow([i,pred])
f.close()      

总结

通过此次对进阶练习的学习,清楚的认识到了自己的不足,且收获很多,为我今后的学习指明了方向(特别感谢解志杰同学的帮助!!)。以上代码取材于老师给的材料,目前的实力还没能达到修改其他内容,仅仅对VGG模型和模型训练的过程有初步的了解,因此保留原代码添加注释,在今后学习的过程中再回顾。

posted @ 2020-07-25 14:51  陳半仙  阅读(306)  评论(0编辑  收藏  举报