貓狗大戰--使用 “VGG16進行CIFAR10分類” 遷移學習實現

Vision_Tung發表於2020-11-14

 

狗大戰--使用 “VGG16進行CIFAR10分類”  遷移學習實現

使用VGG模型進行貓狗大戰

原文見:https://github.com/mlelarge/dataflowr/blob/master/CEA_EDF_INRIA/01_intro_DLDIY_colab.ipynb

 

  • VGG是由Simonyan 和Zisserman在文獻《Very Deep Convolutional Networks for Large Scale Image Recognition》中提出卷積神經網路模型,其名稱來源於作者所在的牛津大學視覺幾何組(Visual Geometry Group)的縮寫。該模型參加2014年的 ImageNet影像分類與定位挑戰賽,取得了優異成績:在分類任務上排名第二,在定位任務上排名第一。

  • 遷移學習是一種機器學習方法,就是把為任務 A 開發的模型作為初始點,重新使用在為任務 B 開發模型的過程中。

  • ImageNet影像分類10類中存在貓和狗,所以用VGG來作為“貓狗大戰”的預訓練是十分合理的

一、程式碼部分

  • 標頭檔案
import os
import torch
import torch.nn as nn
from torchvision import models,transforms,datasets
from tqdm import trange,tqdm

# 判斷是否存在GPU裝置
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print('Using gpu: %s ' % torch.cuda.is_available())
  • 資料處理

datasets 是 torchvision 中的一個包,可以用做載入影像資料。它可以以多執行緒(multi-thread)的形式從硬碟中讀取資料,使用 mini-batch 的形式,在網路訓練中向 GPU 輸送。在使用CNN處理影像時,需要進行預處理。圖片將被整理成 224*224*3 的大小,同時還將進行歸一化處理。

這裡我將https://static.leiphone.com/cat_dog.rar的訓練檔案一起加入了到了訓練集中,因為原來的訓練集只有1800張圖片,我希望能在更大的資料集上進行訓練,以求獲得更好的結果。


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 = r'G:\作業\#1.人工智慧\colab_demo-master\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

loader_train = torch.utils.data.DataLoader(dsets['train'], batch_size=40, shuffle=True, num_workers=0)
loader_valid = torch.utils.data.DataLoader(dsets['valid'], batch_size=16, shuffle=False, num_workers=0)
  •  載入 VGG Model 

這裡我把訓練出來比較好的模型序列化到硬碟上

首先,準確率比較高的情況往往不是訓練的最終結果,選擇準確率較高的模型儲存到本地可以獲得比較好的效果

其次,2w+資料的訓練週期較長,拿出其中表現較好時刻的模型可以提前進行測試,提高效率

n = input("是否重新訓練?(Y/N)")
if n=='N':
    path = input("輸入檔名:")
    CNT = input("輸入輪數:")
    model_vgg_new = torch.load(path)
    model_vgg_new = model_vgg_new.to(device)
else:
    model_vgg = models.vgg16(pretrained=True)
    model_vgg = model_vgg.to(device)
    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)

criterion = nn.NLLLoss()

# 學習率
lr = 0.001


optimizer_vgg = torch.optim.Adam(model_vgg_new.classifier[6].parameters(), lr=lr)

'''
第二步:訓練模型
'''
N_ = int(CNT)+1


def train_model(model, dataloader, size, epochs=100, optimizer=None):
    model.train()
    global N_
    for epoch in range(epochs):
        running_loss = 0.0
        running_corrects = 0
        count = 0
        for inputs, classes in tqdm(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('{} \tLoss: {:.4f} Acc: {:.4f}'.format(N_,epoch_loss, epoch_acc))
        if epoch_acc > 0.97:
            torch.save(model, './model'+str(N_)+'_'+str(epoch_acc)+'_'+'.pth')
            print("Got A Nice Model")
        N_ = N_ + 1


# 模型訓練
train_model(model_vgg_new, loader_train, size=dset_sizes['train'], epochs=100,
            optimizer=optimizer_vgg)
  • 訓練過程

  • AI研習社結果

  • 測試程式碼
import os
import torch
from torchvision import transforms,datasets
from tqdm import tqdm


device = torch.device("cuda:0")
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 = r'G:\作業\#1.人工智慧\colab_demo-master\dogscats'

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

dset_sizes = {x: len(dsets[x]) for x in ['valid']}

loader_valid = torch.utils.data.DataLoader(dsets['valid'], batch_size=8, shuffle=False, num_workers=0)

model_vgg_new = torch.load('xxxxxx')
model_vgg_new = model_vgg_new.to(device)

def test_model(model,dataloader,size):
    model.eval()
    running_corrects = 0
    for inputs,classes in tqdm(dataloader):
        inputs = inputs.to(device)
        classes = classes.to(device)
        outputs = model(inputs)
        _,preds = torch.max(outputs.data,1)
        running_corrects += torch.sum(preds == classes.data)
    epoch_acc = running_corrects.data.item() / size
    print('Acc: {:.4f} '.format(epoch_acc))


test_model(model_vgg_new, loader_valid, size=dset_sizes['valid'])
  • 研習社資料測試程式碼
import torch
import numpy as np
from torchvision import transforms,datasets
from tqdm import tqdm
device = torch.device("cuda:0" )
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,
            ])
dsets_mine = datasets.ImageFolder(r"G:\作業\#1.人工智慧\colab_demo-master\dogscats2", vgg_format)

loader_test = torch.utils.data.DataLoader(dsets_mine, batch_size=1, shuffle=False, num_workers=0)

model_vgg_new = torch.load('')
model_vgg_new = model_vgg_new.to(device)

dic = {}
def test(model,dataloader,size):
    model.eval()
    predictions = np.zeros(size)
    cnt = 0
    for inputs,_ in tqdm(dataloader):
        inputs = inputs.to(device)
        outputs = model(inputs)
        _,preds = torch.max(outputs.data,1)
        key = dsets_mine.imgs[cnt][0].split("\\")[-1].split('.')[0]
        dic[key] = preds[0]
        cnt = cnt +1
test(model_vgg_new,loader_test,size=2000)

with open("result.csv",'a+') as f:
    for key in range(2000):
        f.write("{},{}\n".format(key,dic[str(key)]))

 

 

相關文章