Pytorch實現簡單CNN以及遷移學習

包導入

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
# torchvision是獨立於pytorch的關於圖像操作的一些方便工具庫。
# torchvision的詳細介紹在:https://pypi.org/project/torchvision/0.1.8/
# torchvision主要包括一下幾個包:
# vision.datasets : 幾個常用視覺數據集,可以下載和加載
# vision.models : 流行的模型,例如 AlexNet, VGG, and ResNet 以及 與訓練好的參數。
# vision.transforms : 常用的圖像操作,例如:隨機切割,旋轉等。
# vision.utils : 用於把形似 (3 x H x W) 的張量保存到硬盤中,給一個mini-batch的圖像可以產生一個圖像格網。

print("PyTorch Version: ",torch.__version__)

首先我們定義一個基於ConvNet的簡單神經網絡

1、加載數據(順序調整一下)

torch.manual_seed(53113)  #cpu隨機種子

#沒gpu下面可以忽略
use_cuda = torch.cuda.is_available()  
device = torch.device("cuda" if use_cuda else "cpu")  
batch_size = test_batch_size = 32  
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}


#torch.utils.data.DataLoader在訓練模型時使用到此函數,用來把訓練數據分成多個batch,
#此函數每次拋出一個batch數據,直至把所有的數據都拋出,也就是個數據迭代器。
train_loader = torch.utils.data.DataLoader(
    datasets.MNIST('./mnist_data', 
                   train=True, #如果true,從training.pt創建數據集
                   download=True, #如果ture,從網上自動下載
                   
#transform 接受一個圖像返回變換後的圖像的函數,相當於圖像先預處理下
#常用的操作如 ToTensor, RandomCrop,Normalize等. 
#他們可以通過transforms.Compose被組合在一起 
                   transform=transforms.Compose([
                       
                       transforms.ToTensor(), 
#.ToTensor()將shape爲(H, W, C)的nump.ndarray或img轉爲shape爲(C, H, W)的tensor,
#其將每一個數值歸一化到[0,1],其歸一化方法比較簡單,直接除以255即可。
                       
                       transforms.Normalize((0.1307,), (0.3081,)) # 所有圖片像素均值和方差
#.Normalize作用就是.ToTensor將輸入歸一化到(0,1)後,再使用公式”(x-mean)/std”,將每個元素分佈到(-1,1)  
                   ])), # 第一個參數dataset:數據集
    batch_size=batch_size, 
    shuffle=True,  #隨機打亂數據
    **kwargs)##kwargs是上面gpu的設置
  


test_loader = torch.utils.data.DataLoader(
    datasets.MNIST('./mnist_data', 
                   train=False, #如果False,從test.pt創建數據集
                   transform=transforms.Compose([
                       transforms.ToTensor(),
                       transforms.Normalize((0.1307,), (0.3081,))
                   ])),
    batch_size=test_batch_size, 
    shuffle=True, 
    **kwargs)

#%%

train_loader.dataset[0][0].shape

2、定義CNN模型

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5, 1) 
        #torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1)
        #in_channels:輸入圖像通道數,手寫數字圖像爲1,彩色圖像爲3
        #out_channels:輸出通道數,這個等於卷積核的數量
        #kernel_size:卷積核大小
        #stride:步長
         
        self.conv2 = nn.Conv2d(20, 50, 5, 1)
        #上個卷積網絡的out_channels,就是下一個網絡的in_channels,所以這裏是20
        #out_channels:卷積核數量50
        
        
        self.fc1 = nn.Linear(4*4*50, 500)
        #全連接層torch.nn.Linear(in_features, out_features)
        #in_features:輸入特徵維度,4*4*50是自己算出來的,跟輸入圖像維度有關
        #out_features;輸出特徵維度
        
        self.fc2 = nn.Linear(500, 10)
        #輸出維度10,10分類

    def forward(self, x):  
        #print(x.shape)  #手寫數字的輸入維度,(N,1,28,28), N爲batch_size
        x = F.relu(self.conv1(x)) # x = (N,50,24,24)
        x = F.max_pool2d(x, 2, 2) # x = (N,50,12,12)
        x = F.relu(self.conv2(x)) # x = (N,50,8,8)
        x = F.max_pool2d(x, 2, 2) # x = (N,50,4,4)
        x = x.view(-1, 4*4*50)    # x = (N,4*4*50)
        x = F.relu(self.fc1(x))   # x = (N,4*4*50)*(4*4*50, 500)=(N,500)
        x = self.fc2(x)           # x = (N,500)*(500, 10)=(N,10)
        return F.log_softmax(x, dim=1)  #帶log的softmax分類,每張圖片返回10個概率

3、初始化模型和定義優化函數

lr = 0.01
momentum = 0.5
model = Net().to(device) #模型初始化
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=momentum) #定義優化器

NLL loss的定義

(x,y)=L={l1,,lN},ln=wynxn,yn,wc=weight[c]1{cignore_index}\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad l_n = - w_{y_n} x_{n,y_n}, \quad w_{c} = \text{weight}[c] \cdot \mathbb{1}\{c \not= \text{ignore\_index}\}

4、定義訓練和測試模型

def train(model, device, train_loader, optimizer, epoch, log_interval=100):
    model.train() #進入訓練模式
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad() #梯度歸零
        output = model(data)  #輸出的維度[N,10] 這裏的data是函數的forward參數x
        loss = F.nll_loss(output, target) #這裏loss求的是平均數,除以了batch
#F.nll_loss(F.log_softmax(input), target) :
#單分類交叉熵損失函數,一張圖片裏只能有一個類別,輸入input的需要softmax
#還有一種是多分類損失函數,一張圖片有多個類別,輸入的input需要sigmoid
        
        loss.backward()
        optimizer.step()
        if batch_idx % log_interval == 0:
            print("Train Epoch: {} [{}/{} ({:0f}%)]\tLoss: {:.6f}".format(
                epoch, 
                batch_idx * len(data), #100*32
                len(train_loader.dataset), #60000
                100. * batch_idx / len(train_loader), #len(train_loader)=60000/32=1875
                loss.item()
            ))
            #print(len(train_loader))


#%%

def test(model, device, test_loader):
    model.eval() #進入測試模式
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data) 
            test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss
            #reduction='sum'代表batch的每個元素loss累加求和,默認是mean求平均
                       
            pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
            
            #print(target.shape) #torch.Size([32])
            #print(pred.shape) #torch.Size([32, 1])
            correct += pred.eq(target.view_as(pred)).sum().item()
            #pred和target的維度不一樣
            #pred.eq()相等返回1,不相等返回0,返回的tensor維度(32,1)。

    test_loss /= len(test_loader.dataset)

    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))

5、查看運行結果

epochs = 2
for epoch in range(1, epochs + 1):
    train(model, device, train_loader, optimizer, epoch)
    test(model, device, test_loader)

save_model = True
if (save_model):
    torch.save(model.state_dict(),"mnist_cnn.pt") 
    #詞典格式,model.state_dict()只保存模型參數

CNN模型的遷移學習

  • 很多時候當我們需要訓練一個新的圖像分類任務,我們不會完全從一個隨機的模型開始訓練,而是利用_預訓練_的模型來加速訓練的過程。我們經常使用在ImageNet上的預訓練模型。
  • 這是一種transfer learning的方法。我們常用以下兩種方法做遷移學習。
    • fine tuning: 從一個預訓練模型開始,我們改變一些模型的架構,然後繼續訓練整個模型的參數。
    • feature extraction: 我們不再改變與訓練模型的參數,而是隻更新我們改變過的部分模型參數。我們之所以叫它feature extraction是因爲我們把預訓練的CNN模型當做一個特徵提取模型,利用提取出來的特徵做來完成我們的訓練任務。

以下是構建和訓練遷移學習模型的基本步驟:

  • 初始化預訓練模型
  • 把最後一層的輸出層改變成我們想要分的類別總數
  • 定義一個optimizer來更新參數
  • 模型訓練
import numpy as np
import torchvision
from torchvision import datasets, transforms, models

import matplotlib.pyplot as plt
import time
import os
import copy
print("Torchvision Version: ",torchvision.__version__)

數據

我們會使用hymenoptera_data數據集,下載.

這個數據集包括兩類圖片, beesants, 這些數據都被處理成了可以使用ImageFolder <https://pytorch.org/docs/stable/torchvision/datasets.html#torchvision.datasets.ImageFolder>來讀取的格式。我們只需要把data_dir設置成數據的根目錄,然後把model_name設置成我們想要使用的與訓練模型:
[resnet, alexnet, vgg, squeezenet, densenet, inception]

其他的參數有:

  • num_classes表示數據集分類的類別數
  • batch_size
  • num_epochs
  • feature_extract表示我們訓練的時候使用fine tuning還是feature extraction方法。如果feature_extract = False,整個模型都會被同時更新。如果feature_extract = True,只有模型的最後一層被更新。

1、查看數據,只是查看作用

# Top level data directory. Here we assume the format of the directory conforms 
#   to the ImageFolder structure
data_dir = "./hymenoptera_data"
# Batch size for training (change depending on how much memory you have)
batch_size = 32


#蜜蜂和螞蟻數據集不會自動下載,請下載,並放在當前代碼目錄下
#os.path.join() 連接路徑,相當於.../data_dir/train
all_imgs = datasets.ImageFolder(os.path.join(data_dir, "train"),
                                transforms.Compose([
        transforms.RandomResizedCrop(input_size), #把每張圖片變成resnet需要輸入的維度224
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
    ]))
loader = torch.utils.data.DataLoader(all_imgs, batch_size=batch_size, shuffle=True, num_workers=4)
#訓練數據分batch,變成tensor迭代器

#%%

img = next(iter(loader))[0] #這個img是一個batch的tensor

#%%

img.shape

#%%

unloader = transforms.ToPILImage()  # reconvert into PIL image
#transforms:torchvision的子模塊,常用的圖像操作
#.ToPILImage() 把tensor或數組轉換成圖像
#詳細轉換過程可以看這個:https://blog.csdn.net/qq_37385726/article/details/81811466

plt.ion() #交互模式,默認是交互模式,可以不寫
#詳細瞭解看這個:https://blog.csdn.net/SZuoDao/article/details/52973621
#plt.ioff()

def imshow(tensor, title=None):
    image = tensor.cpu().clone()  # we clone the tensor to not do changes on it
    image = image.squeeze(0)      # remove the fake batch dimension 
    #這個.squeeze(0)看不懂,去掉也可以運行
    
    image = unloader(image) #tensor轉換成圖像
    plt.imshow(image)
    if title is not None:
        plt.title(title)
    plt.pause(1) # pause a bit so that plots are updated
    #可以去掉看看,只是延遲顯示作用


plt.figure()
imshow(img[8], title='Image') 
imshow(img[9], title='Image')
imshow(img[10], title='Image')

#%% md

2、把訓練集和驗證集分batch轉換成迭代器

現在我們知道了模型輸入的size,我們就可以把數據預處理成相應的格式。

data_transforms = {
    "train": transforms.Compose([
        transforms.RandomResizedCrop(input_size),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
    "val": transforms.Compose([
        transforms.Resize(input_size),
        transforms.CenterCrop(input_size),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]),
}

print("Initializing Datasets and Dataloaders...")


# Create training and validation datasets
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x]) for x in ['train', 'val']}
# Create training and validation dataloaders
dataloaders_dict = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True, num_workers=4) for x in ['train', 'val']}
#把迭代器存放到字典裏作爲value,key是train和val,後面調用key即可。

# Detect if we have a GPU available
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

#%%

inputs, labels=next(iter(dataloaders_dict["train"])) #一個batch
print(inputs.shape)
print(labels)

#%%

for inputs, labels in dataloaders_dict["train"]:
    #print(inputs)
    #print(labels)
    print(labels.size()) #最後一個batch不足32

3、加載resnet模型並修改全連接層

# Models to choose from [resnet, alexnet, vgg, squeezenet, densenet, inception]
model_name = "resnet"
# Number of classes in the dataset
num_classes = 2
# Number of epochs to train for 
num_epochs = 2
# Flag for feature extracting. When False, we finetune the whole model, 
#   when True we only update the reshaped layer params
feature_extract = True  #只更新修改的層

#%%

def set_parameter_requires_grad(model, feature_extracting):
    if feature_extracting:
        for param in model.parameters():
            param.requires_grad = False #提取的參數梯度不更新

#%%

def initialize_model(model_name, num_classes, feature_extract, use_pretrained=True):
    if model_name == "resnet":
        model_ft = models.resnet18(pretrained=use_pretrained) 
        #如果True,從imagenet上返回預訓練的模型和參數
        
        set_parameter_requires_grad(model_ft, feature_extract)#提取的參數梯度不更新
        #print(model_ft) 可以打印看下
        num_ftrs = model_ft.fc.in_features 
        #model_ft.fc是resnet的最後全連接層
        #(fc): Linear(in_features=512, out_features=1000, bias=True)
        #in_features 是全連接層的輸入特徵維度
        #print(num_ftrs)
        model_ft.fc = nn.Linear(num_ftrs, num_classes)
        #out_features=1000 改爲 num_classes=2
        input_size = 224 #resnet18網絡輸入圖片維度是224,resnet34,50,101,152也是
        
    return model_ft, input_size
model_ft, input_size = initialize_model(model_name, num_classes, feature_extract, use_pretrained=True)
print(model_ft)

4、查看需要更新的參數、定義優化器

next(iter(model_ft.named_parameters()))

#%%

len(next(iter(model_ft.named_parameters()))) #是元組,只有兩個值

#%%

for name,param in model_ft.named_parameters():
    print(name) #看下都有哪些參數

#%%

# Send the model to GPU
model_ft = model_ft.to(device)

# Gather the parameters to be optimized/updated in this run. If we are
#  finetuning we will be updating all parameters. However, if we are 
#  doing feature extract method, we will only update the parameters
#  that we have just initialized, i.e. the parameters with requires_grad
#  is True.
params_to_update = model_ft.parameters() #需要更新的參數
print("Params to learn:")
if feature_extract:
    params_to_update = [] #需要更新的參數存放在此
    for name,param in model_ft.named_parameters(): 
        #model_ft.named_parameters()有啥看上面cell
        if param.requires_grad == True: 
#這裏要知道全連接層之前的層param.requires_grad == Flase
#後面加的全連接層param.requires_grad == True
            params_to_update.append(param)
            print("\t",name)
else: #否則,所有的參數都會更新
    for name,param in model_ft.named_parameters():
        if param.requires_grad == True:
            print("\t",name)

# Observe that all parameters are being optimized
optimizer_ft = optim.SGD(params_to_update, lr=0.001, momentum=0.9) #定義優化器
# Setup the loss fxn
criterion = nn.CrossEntropyLoss() #定義損失函數

5、定義訓練模型

#訓練測試合一起了
def train_model(model, dataloaders, criterion, optimizer, num_epochs=5):
    since = time.time()
    val_acc_history = [] 
    best_model_wts = copy.deepcopy(model.state_dict())#深拷貝上面resnet模型參數
#.copy和.deepcopy區別看這個:https://blog.csdn.net/u011630575/article/details/78604226 
    best_acc = 0.
    for epoch in range(num_epochs):
        print("Epoch {}/{}".format(epoch, num_epochs-1))
        print("-"*10)
        
        for phase in ["train", "val"]:
            running_loss = 0.
            running_corrects = 0.
            if phase == "train":
                model.train()
            else: 
                model.eval()
            
            for inputs, labels in dataloaders[phase]:
                inputs = inputs.to(device)
                labels = labels.to(device)
                
                with torch.autograd.set_grad_enabled(phase=="train"):
                    #torch.autograd.set_grad_enabled梯度管理器,可設置爲打開或關閉
                    #phase=="train"是True和False,雙等號要注意
                    outputs = model(inputs)
                    loss = criterion(outputs, labels)
                    
                _, preds = torch.max(outputs, 1)
                #返回每一行最大的數和索引,prds的位置是索引的位置
                #也可以preds = outputs.argmax(dim=1)
                if phase == "train":
                    optimizer.zero_grad()
                    loss.backward()
                    optimizer.step()
                    
                running_loss += loss.item() * inputs.size(0) #交叉熵損失函數是平均過的
                running_corrects += torch.sum(preds.view(-1) == labels.view(-1)).item()
                #.view(-1)展開到一維,並自己計算
            
            epoch_loss = running_loss / len(dataloaders[phase].dataset)
            epoch_acc = running_corrects / len(dataloaders[phase].dataset)
       
            print("{} Loss: {} Acc: {}".format(phase, epoch_loss, epoch_acc))
            if phase == "val" and epoch_acc > best_acc:
                best_acc = epoch_acc
                best_model_wts = copy.deepcopy(model.state_dict())
                #模型變好,就拷貝更新後的模型參數
                
            if phase == "val":
                val_acc_history.append(epoch_acc) #記錄每個epoch驗證集的準確率
            
        print()
    
    time_elapsed = time.time() - since
    print("Training compete in {}m {}s".format(time_elapsed // 60, time_elapsed % 60))
    print("Best val Acc: {}".format(best_acc))
    
    model.load_state_dict(best_model_wts) #把最新的參數複製到model中
    return model, val_acc_history

6、運行模型

# Train and evaluate
model_ft, ohist = train_model(model_ft, dataloaders_dict, criterion, optimizer_ft, num_epochs=num_epochs)

# Initialize the non-pretrained version of the model used for this run
scratch_model,_ = initialize_model(model_name, 
                                   num_classes, 
                                   feature_extract=False, #所有參數都訓練
                                   use_pretrained=False)# 不要imagenet的參數
scratch_model = scratch_model.to(device)
scratch_optimizer = optim.SGD(scratch_model.parameters(), 
                              lr=0.001, momentum=0.9)
scratch_criterion = nn.CrossEntropyLoss()
_,scratch_hist = train_model(scratch_model, 
                             dataloaders_dict, 
                             scratch_criterion, 
                             scratch_optimizer, 
                             num_epochs=num_epochs)

#%%


# Plot the training curves of validation accuracy vs. number 
#  of training epochs for the transfer learning method and
#  the model trained from scratch
# ohist = []
# shist = []

# ohist = [h.cpu().numpy() for h in ohist]
# shist = [h.cpu().numpy() for h in scratch_hist]

plt.title("Validation Accuracy vs. Number of Training Epochs")
plt.xlabel("Training Epochs")
plt.ylabel("Validation Accuracy")
plt.plot(range(1,num_epochs+1),ohist,label="Pretrained")
plt.plot(range(1,num_epochs+1),scratch_hist,label="Scratch")
plt.ylim((0,1.))
plt.xticks(np.arange(1, num_epochs+1, 1.0))
plt.legend()
plt.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章