pytorch學(xué)習(xí)記錄-alexnet

\color{red}{1.讀取數(shù)據(jù)}
https://blog.csdn.net/sinat_42239797/article/details/90641659

#***************************一些必要的包的調(diào)用********************************
import torch.nn.functional as F
import torch
import torch 
import torch.nn as nn
from torch.autograd import Variable
import torchvision.models as models
from torchvision import transforms, utils
from torch.utils.data import Dataset, DataLoader
from PIL import Image
import numpy as np
import torch.optim as optim
import os
#***************************初始化一些函數(shù)********************************
#torch.cuda.set_device(gpu_id)#使用GPU
learning_rate = 0.0001#學(xué)習(xí)率的設(shè)置

#*************************************數(shù)據(jù)集的設(shè)置****************************************************************************
root =os.getcwd()+"\\"#數(shù)據(jù)集的地址
#定義讀取文件的格式
def default_loader(path):
    return Image.open(path).convert('RGB')

class MyDataset(Dataset): 
                                 #創(chuàng)建自己的類: MyDataset,這個類是繼承的torch.utils.data.Dataset
        #**********************************  #使用__init__()初始化一些需要傳入的參數(shù)及數(shù)據(jù)集的調(diào)用**********************
    def __init__(self,txt, transform=None,target_transform=None, loader=default_loader):

        super(MyDataset,self).__init__()
        imgs = []
                           #對繼承自父類的屬性進行初始化
        fh = open(txt, 'r')
                    #按照傳入的路徑和txt文本參數(shù),以只讀的方式打開這個文本
        for line in fh: #迭代該列表#按行循環(huán)txt文本中的內(nèi)
            line = line.strip('\n')
            line = line.rstrip('\n')
                # 刪除 本行string 字符串末尾的指定字符苍鲜,這個方法的詳細介紹自己查詢python
            words = line.split()
                  #用split將該行分割成列表  split的默認參數(shù)是空格,所以不傳遞任何參數(shù)時分割空格
            imgs.append((words[0],int(words[1])))
                 #把txt里的內(nèi)容讀入imgs列表保存玷犹,具體是words幾要看txt內(nèi)容而定 
                        # 很顯然混滔,根據(jù)我剛才截圖所示txt的內(nèi)容,words[0]是圖片信息歹颓,words[1]是lable       
        self.imgs = imgs
        self.transform = transform
        self.target_transform = target_transform
        self.loader = loader        
                #*************************** #使用__getitem__()對數(shù)據(jù)進行預(yù)處理并返回想要的信息**********************
    def __getitem__(self, index):#這個方法是必須要有的坯屿,用于按照索引讀取每個元素的具體內(nèi)容
        fn, label = self.imgs[index]
                       #fn是圖片path #fn和label分別獲得imgs[index]也即是剛才每行中word[0]和word[1]的信息
        img = self.loader(fn) 
                      # 按照路徑讀取圖片
        if self.transform is not None:
            img = self.transform(img) 
                        #數(shù)據(jù)標(biāo)簽轉(zhuǎn)換為Tensor
        return img,label
                      #return回哪些內(nèi)容,那么我們在訓(xùn)練時循環(huán)讀取每個batch時巍扛,就能獲得哪些內(nèi)容
         #**********************************  #使用__len__()初始化一些需要傳入的參數(shù)及數(shù)據(jù)集的調(diào)用**********************
    def __len__(self):
            #這個函數(shù)也必須要寫领跛,它返回的是數(shù)據(jù)集的長度,也就是多少張圖片撤奸,要和loader的長度作區(qū)分
        return len(self.imgs)
train_data=MyDataset(txt=root+'train.txt', transform=transforms.ToTensor())
test_data = MyDataset(txt=root+'text.txt', transform=transforms.ToTensor())
#然后就是調(diào)用DataLoader和剛剛創(chuàng)建的數(shù)據(jù)集吠昭,來創(chuàng)建dataloader,這里提一句胧瓜,loader的長度是有多少個batch矢棚,所以和batch_size有關(guān)
train_loader = DataLoader(dataset=train_data, batch_size=32, shuffle=True,num_workers=0)
test_loader = DataLoader(dataset=test_data, batch_size=32, shuffle=False,num_workers=0)
print('num_of_trainData:', len(train_loader))
print('num_of_testData:', len(test_loader))

要使用上面的代碼,首先要制作供調(diào)用的txt文件府喳,我的文件夾里面的內(nèi)容是這樣的


我的文件夾.JPG

train里面的數(shù)據(jù)如下蒲肋,一共10類,10個數(shù)字钝满,我預(yù)先resize成224x224


train文件夾.JPG

用的制作txt的代碼如下

import os

dir = 'D:/cv/TEST/alexnet/test/'#圖片文件的地址
#os.listdir的結(jié)果就是一個list集兜粘,可以使用list的sort方法來排序。如果文件名中有數(shù)字弯蚜,就用數(shù)字的排序
files = os.listdir(dir)#列出dirname下的目錄和文件
files.sort()#排序
text = open('./text.txt', 'a')
for file in files:
    label = file.split('.')[0].split('_')[-1]
    name = str(dir) +file + ' ' + label +'\n'
    text.write(name)
text.close()

dir = 'D:/cv/TEST/alexnet/train/'#圖片文件的地址
#os.listdir的結(jié)果就是一個list集孔轴,可以使用list的sort方法來排序。如果文件名中有數(shù)字熟吏,就用數(shù)字的排序
files = os.listdir(dir)#列出dirname下的目錄和文件
files.sort()#排序
train = open('./train.txt','a')
for file in files:
    label = file.split('.')[0].split('_')[-1]
    name = str(dir) +file + ' ' + label +'\n'
    train.write(name)
train.close()

dir = 'D:/cv/TEST/alexnet/vaild/'#圖片文件的地址
#os.listdir的結(jié)果就是一個list集距糖,可以使用list的sort方法來排序。如果文件名中有數(shù)字牵寺,就用數(shù)字的排序
files = os.listdir(dir)#列出dirname下的目錄和文件
files.sort()#排序
vaild = open('./vaild.txt','a')
for file in files:
    label = file.split('.')[0].split('_')[-1]
    name = str(dir) +file + ' ' + label +'\n'
    vaild.write(name)
vaild.close()

\color{red}{2.模型構(gòu)建}
用的是官網(wǎng)的模型悍引,沒有想到pytorch做模型怎么簡單
https://pytorch.org/docs/master/_modules/torchvision/models/alexnet.html#alexnet

class AlexNet(nn.Module):

    def __init__(self, num_classes=1000):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.classifier(x)
        return x

\color{red}{3.訓(xùn)練}

net = AlexNet()
net.cuda()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
"""
###畫圖
import matplotlib.pyplot as plt
import numpy as np
import torchvision

# functions to show an image


def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()


# get some random training images
dataiter = iter(train_loader)
images, labels = dataiter.next()
# show images
print(images[0])
imshow(torchvision.utils.make_grid(images[0]))
# print labels
print(' '.join('%5s' % labels[j] for j in range(32)))
###畫圖結(jié)束
"""
# 訓(xùn)練網(wǎng)絡(luò)
# 迭代epoch
for epoch in range(30):
    running_loss = 0.0
    for i, data in enumerate(train_loader, 0):
        # get the input
        inputs, labels = data
        # zeros the paramster gradients
        optimizer.zero_grad()       # 
        # print(inputs)
        # forward + backward + optimize
        outputs = net(inputs.cuda())
        loss = criterion(outputs, labels.cuda())  # 計算loss
        #print(outputs,labels)
        loss.backward()     # loss 求導(dǎo)
        optimizer.step()    # 更新參數(shù)

        # print statistics
        running_loss += loss.item()  # tensor.item()  獲取tensor的數(shù)值
        if i % 50 == 49:
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 5))  # 每2000次迭代,輸出loss的平均值
            running_loss = 0.0

print('Finished Training')

\color{red}{4.保存}

# 保存
torch.save(net, 'model.pth')

\color{red}{5.提取和測試}

def evaluteTop1(model, loader):
    model.eval()
    correct = 0
    total = len(loader.dataset)
    for x,y in loader:
        x,y = x.cuda(), y.cuda()
        with torch.no_grad():
            logits = model(x)
            pred = logits.argmax(dim=1)
            correct += torch.eq(pred, y).sum().float().item()
        #correct += torch.eq(pred, y).sum().item()
    return correct / total
model = AlexNet()
model = torch.load('model.pth')
print(evaluteTop1(model,test_loader))
"""
###錯誤的圖片帽氓,要上面的imshow代碼
#net.eval()
eval_loss = 0.
eval_acc = 0.
errorimg = []
for i, data in enumerate(test_loader, 0):
    inputs, labels = data
    outputs = model(inputs.cuda())
    pred = torch.max(outputs, 1)[1]
    for i in range(len(labels)):
        if labels[i] != pred[i]:
            errorimg.append(inputs[i])
imshow(torchvision.utils.make_grid(errorimg))
###
"""

\color{red}{6.用到的數(shù)據(jù)}
圖片及處理代碼和很潦草的jupyter文件
鏈接: https://pan.baidu.com/s/19sA7brPbKbv9tPByuY2Rkw 提取碼: t2ih 復(fù)制這段內(nèi)容后打開百度網(wǎng)盤手機App趣斤,操作更方便哦
訓(xùn)練出的模型
鏈接: https://pan.baidu.com/s/1WAevnAAJA8J5JHWaStJ_NQ 提取碼: b9we

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市黎休,隨后出現(xiàn)的幾起案子浓领,更是在濱河造成了極大的恐慌,老刑警劉巖势腮,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件联贩,死亡現(xiàn)場離奇詭異,居然都是意外死亡捎拯,警方通過查閱死者的電腦和手機泪幌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來署照,“玉大人祸泪,你說我怎么就攤上這事〗ㄜ剑” “怎么了没隘?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長禁荸。 經(jīng)常有香客問我右蒲,道長,這世上最難降的妖魔是什么赶熟? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任品嚣,我火速辦了婚禮,結(jié)果婚禮上钧大,老公的妹妹穿的比我還像新娘翰撑。我一直安慰自己,他們只是感情好啊央,可當(dāng)我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布眶诈。 她就那樣靜靜地躺著,像睡著了一般瓜饥。 火紅的嫁衣襯著肌膚如雪逝撬。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天乓土,我揣著相機與錄音宪潮,去河邊找鬼溯警。 笑死,一個胖子當(dāng)著我的面吹牛狡相,可吹牛的內(nèi)容都是我干的梯轻。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼尽棕,長吁一口氣:“原來是場噩夢啊……” “哼喳挑!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起滔悉,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤伊诵,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后回官,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體曹宴,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年歉提,在試婚紗的時候發(fā)現(xiàn)自己被綠了浙炼。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡唯袄,死狀恐怖弯屈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情恋拷,我是刑警寧澤资厉,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站蔬顾,受9級特大地震影響宴偿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜诀豁,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一窄刘、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧舷胜,春花似錦娩践、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至沮焕,卻和暖如春吨岭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背峦树。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工辣辫, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留旦事,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓急灭,卻偏偏與公主長得像姐浮,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子化戳,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,941評論 2 355