使用pytorch搭建AlexNet操作(微調(diào)預(yù)訓(xùn)練模型及手動(dòng)搭建)
本文介紹了如何在pytorch下搭建AlexNet,使用了兩種方法,一種是直接加載預(yù)訓(xùn)練模型,并根據(jù)自己的需要微調(diào)(將最后一層全連接層輸出由1000改為10),另一種是手動(dòng)搭建。
構(gòu)建模型類的時(shí)候需要繼承自torch.nn.Module類,要自己重寫__ \_\___init__ \_\___方法和正向傳遞時(shí)的forward方法,這里我自己的理解是,搭建網(wǎng)絡(luò)寫在__ \_\___init__ \_\___中,每次正向傳遞需要計(jì)算的部分寫在forward中,例如把矩陣壓平之類的。
加載預(yù)訓(xùn)練alexnet之后,可以print出來查看模型的結(jié)構(gòu)及信息:
model = models.alexnet(pretrained=True) print(model)
分為兩個(gè)部分,features及classifier,后續(xù)搭建模型時(shí)可以也寫成這兩部分,并且從打印出來的模型信息中也可以看出每一層的引用方式,便于修改,例如model.classifier[1]指的就是Linear(in_features=9216, out_features=4096, bias=True)這層。
下面放出完整的搭建代碼:
import torch.nn as nn from torchvision import models class BuildAlexNet(nn.Module): def __init__(self, model_type, n_output): super(BuildAlexNet, self).__init__() self.model_type = model_type if model_type == 'pre': model = models.alexnet(pretrained=True) self.features = model.features fc1 = nn.Linear(9216, 4096) fc1.bias = model.classifier[1].bias fc1.weight = model.classifier[1].weight fc2 = nn.Linear(4096, 4096) fc2.bias = model.classifier[4].bias fc2.weight = model.classifier[4].weight self.classifier = nn.Sequential( nn.Dropout(), fc1, nn.ReLU(inplace=True), nn.Dropout(), fc2, nn.ReLU(inplace=True), nn.Linear(4096, n_output)) #或者直接修改為 # model.classifier[6]==nn.Linear(4096,n_output) # self.classifier = model.classifier if model_type == 'new': self.features = nn.Sequential( nn.Conv2d(3, 64, 11, 4, 2), nn.ReLU(inplace = True), nn.MaxPool2d(3, 2, 0), nn.Conv2d(64, 192, 5, 1, 2), nn.ReLU(inplace=True), nn.MaxPool2d(3, 2, 0), nn.Conv2d(192, 384, 3, 1, 1), nn.ReLU(inplace = True), nn.Conv2d(384, 256, 3, 1, 1), nn.ReLU(inplace=True), nn.MaxPool2d(3, 2, 0)) self.classifier = nn.Sequential( nn.Dropout(), nn.Linear(9216, 4096), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(4096, 4096), nn.ReLU(inplace=True), nn.Linear(4096, n_output)) def forward(self, x): x = self.features(x) x = x.view(x.size(0), -1) out = self.classifier(x) return out
微調(diào)預(yù)訓(xùn)練模型的思路為:直接保留原模型的features部分,重寫classifier部分。在classifier部分中,我們實(shí)際需要修改的只有最后一層全連接層,之前的兩個(gè)全連接層不需要修改,所以重寫的時(shí)候需要把這兩層的預(yù)訓(xùn)練權(quán)重和偏移保留下來,也可以像注釋掉的兩行代碼里那樣直接引用最后一層全連接層進(jìn)行修改。
網(wǎng)絡(luò)搭好之后可以小小的測試一下以檢驗(yàn)維度是否正確。
import numpy as np from torch.autograd import Variable import torch if __name__ == '__main__': model_type = 'pre' n_output = 10 alexnet = BuildAlexNet(model_type, n_output) print(alexnet) x = np.random.rand(1,3,224,224) x = x.astype(np.float32) x_ts = torch.from_numpy(x) x_in = Variable(x_ts) y = alexnet(x_in)
這里如果不加“x = x.astype(np.float32)”的話會(huì)報(bào)一個(gè)類型錯(cuò)誤,感覺有點(diǎn)奇怪。
輸出y.data.numpy()可得10維輸出,表明網(wǎng)絡(luò)搭建正確。
以上這篇使用pytorch搭建AlexNet操作(微調(diào)預(yù)訓(xùn)練模型及手動(dòng)搭建)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用pyshp包進(jìn)行shapefile文件修改的例子
今天小編就為大家分享一篇使用pyshp包進(jìn)行shapefile文件修改的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫的方法
今天小編就為大家分享一篇Python 獲取 datax 執(zhí)行結(jié)果保存到數(shù)據(jù)庫的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07使用Python的pygame庫實(shí)現(xiàn)下雪效果的示例代碼
這篇文章給大家介紹了如何使用Python的pygame庫實(shí)現(xiàn)下雪的效果,文中通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的的幫助,需要的朋友可以參考下2024-01-01Python制作簡易版小工具之計(jì)算天數(shù)的實(shí)現(xiàn)思路
這篇文章主要介紹了Python制作簡易版小工具之計(jì)算天數(shù)的實(shí)現(xiàn)思路,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02python爬蟲獲取小區(qū)經(jīng)緯度以及結(jié)構(gòu)化地址
這篇文章主要為大家詳細(xì)介紹了python爬蟲獲取小區(qū)經(jīng)緯度,以及結(jié)構(gòu)化的地址,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Python多進(jìn)程模式實(shí)現(xiàn)多核CPU并行計(jì)算
隨著計(jì)算機(jī)硬件的不斷發(fā)展,多核CPU已經(jīng)成為普及的硬件設(shè)備,在本文中,我們將重點(diǎn)介紹在Python中如何利用多進(jìn)程模式提高程序的執(zhí)行效率,感興趣的可以了解一下2023-05-05