欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytorch卷積神經(jīng)網(wǎng)絡(luò)resent網(wǎng)絡(luò)實踐

 更新時間:2022年05月12日 14:01:56   作者:淺念念52  
這篇文章主要為大家介紹了Pytorch卷積神經(jīng)網(wǎng)絡(luò)resent網(wǎng)絡(luò)實踐,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

前言

上篇文章,講了經(jīng)典卷積神經(jīng)網(wǎng)絡(luò)-resnet,這篇文章通過resnet網(wǎng)絡(luò),做一些具體的事情。

一、技術(shù)介紹

總的來說,第一步首先要加載數(shù)據(jù)集,對數(shù)據(jù)進行一些處理,第二步,調(diào)整學(xué)習(xí)率一些參數(shù),訓(xùn)練好resnet網(wǎng)絡(luò)模型,第三步輸入圖片或者視頻通過訓(xùn)練好的模型,得到結(jié)果。

二、實現(xiàn)途徑

1.加載數(shù)據(jù)集,對數(shù)據(jù)進行處理,加載的圖片是(N,C,H,W )對圖片進行處理成(C,H,W),通過圖片名稱獲取標(biāo)簽,進行分類。

train_paper=r'E:\桌面\資料\cv3\數(shù)據(jù)集\罰拳_公開\train\paper'
train_rock=r'E:\桌面\資料\cv3\數(shù)據(jù)集\罰拳_公開\train\rock'
train_scissors=r'E:\桌面\資料\cv3\數(shù)據(jù)集\罰拳_公開\train\scissors'
test_paper=r'E:\桌面\資料\cv3\數(shù)據(jù)集\罰拳_公開\test\paper'
test_rock=r'E:\桌面\資料\cv3\數(shù)據(jù)集\罰拳_公開\test\rock'
test_scission=r'E:\桌面\資料\cv3\數(shù)據(jù)集\罰拳_公開\test\scissors'
Batch_files=10
transs=trans.Compose([
    trans.ToTensor(),
    trans.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))
])
def read_img(batch_files):
    images=[]
    labels=[]
    for file in batch_files:
        image=Image.open(file)
        image=image.convert('RGB')
        image=image.resize((64,64))
        tensor=transs(image)
        images.append(tensor)
        if 'rock' in file :
            labels.append(torch.tensor(0,dtype=torch.int64))
        if 'paper' in file:
            labels.append(torch.tensor(1,dtype=torch.int64))
        if 'scissors' in file:
            labels.append(torch.tensor(2,dtype=torch.int64))
    return images,labels
if __name__ == '__main__':

2.寫入resnet模型:

這里用的是resnet18

class tiao(nn.Module):
    def __init__(self,shuru,shuchu):
        super(tiao, self).__init__()
        self.conv1=nn.Conv2d(in_channels=shuru,out_channels=shuchu,kernel_size=(3,3),padding=(1,1))
        self.bath=nn.BatchNorm2d(shuchu)
        self.relu=nn.ReLU()
    def forward(self,x):
        x1=self.conv1(x)
        x2=self.bath(x1)
        x3=self.relu(x2)
        x4=self.conv1(x3)
        x5=self.bath(x4)
        x6=self.relu(x5)
        x7=x6+x
        return x7
class tiao2(nn.Module):
    def __init__(self,shuru):
        super(tiao2, self).__init__()
        self.conv1=nn.Conv2d(in_channels=shuru,out_channels=shuru*2,kernel_size=(3,3),stride=(2,2),padding=(1,1))
        self.conv11=nn.Conv2d(in_channels=shuru,out_channels=shuru*2,kernel_size=(1,1),stride=(2,2))
        self.batch=nn.BatchNorm2d(shuru*2)
        self.relu=nn.ReLU()
        self.conv2=nn.Conv2d(in_channels=shuru*2,out_channels=shuru*2,kernel_size=(3,3),stride=(1,1),padding=(1,1))
    def forward(self,x):
        x1=self.conv1(x)
        x2=self.batch(x1)
        x3=self.relu(x2)
        x4=self.conv2(x3)
        x5=self.batch(x4)
        x6=self.relu(x5)
        x11=self.conv11(x)
        x7=x11+x6
        return x7
class resnet18(nn.Module):
    def __init__(self):
        super(resnet18, self).__init__()
        self.conv1=nn.Conv2d(in_channels=3,out_channels=64,kernel_size=(7,7),stride=(2,2),padding=(3,3))
        self.bath=nn.BatchNorm2d(64)
        self.relu=nn.ReLU()
        self.max=nn.MaxPool2d(2,2)
        self.tiao1=tiao(64,64)
        self.tiao2=tiao(64,64)
        self.tiao3=tiao2(64)
        self.tiao4=tiao(128,128)
        self.tiao5=tiao2(128)
        self.tiao6=tiao(256,256)
        self.tiao7=tiao2(256)
        self.tiao8=tiao(512,512)
        self.a=nn.AdaptiveAvgPool2d(output_size=(1,1))
        self.l=nn.Linear(512,3)
    def forward(self,x):
        x1=self.conv1(x)
        x2=self.bath(x1)
        x3=self.relu(x2)
        x4=self.tiao1(x3)
        x5=self.tiao2(x4)
        x6=self.tiao3(x5)
        x7=self.tiao4(x6)
        x8=self.tiao5(x7)
        x9=self.tiao6(x8)
        x10=self.tiao7(x9)
        x11=self.tiao8(x10)
        x12=self.a(x11)
        x13=x12.view(x12.size()[0],-1)
        x14=self.l(x13)
        return x14

第三步:調(diào)用讀取數(shù)據(jù)函數(shù),讀取數(shù)據(jù),打亂,開始訓(xùn)練:

 train_rock=[os.path.join(train_rock,file) for file in os.listdir(train_rock)]
    train_paper= [os.path.join(train_paper, file) for file in os.listdir(train_paper)]
    train_scissors = [os.path.join(train_scissors, file) for file in os.listdir(train_scissors)]
    test_rock=[os.path.join(test_rock,file) for file in os.listdir(test_rock)]
    test_paper=[os.path.join(test_paper,file) for file in os.listdir(test_paper)]
    test_scission=[os.path.join(test_scission,file) for file in os.listdir(test_scission)]
    train=train_rock+train_paper+train_scissors
    test=test_rock+test_paper+test_scission
    random.shuffle(train)
    random.shuffle(test)
    model=resnet18().cuda()
    opt = torch.optim.ASGD(model.parameters(), lr=0.001, weight_decay=0.8)
    loss = nn.CrossEntropyLoss()
    print("開始訓(xùn)練")

第四步:訓(xùn)練模型,完成后保存模型:

  for i in range(5):
        running_loss=0
        for index in range(0,len(train),Batch_files):
            images,labels=read_img(train[index:index+Batch_files])
            inputs=torch.stack(images,0).cuda()
            labels=torch.stack(labels,0).cuda()
            inputs, labels = Variable(inputs), Variable(labels)
            opt.zero_grad()
            h=model(inputs)
            loss1=loss(h,labels)
            loss1.backward()
            opt.step()
            running_loss+=loss1.item()
            if index%41==40:
                avg_loos=running_loss/41
                running_loss=0
                print('avg_loss',avg_loos)
            if index%101==99:
                test_files=random.sample(test,100)
                test_image,test_label=read_img(test_files)
                test_images=torch.stack(test_image,0).cuda()
                test_labels=torch.stack(test_label,0).cuda()
                test_h=model(test_images)
                _,prediction=torch.max(test_h.data,1)
                total=test_labels.size(0)
                correct=(prediction==test_labels).sum()
                print('100張測試集準(zhǔn)確率%d %%'%(100*correct/total))
    torch.save(model.state_dict(),'resnet_caiq猜拳.pth')

第五步:加載模型,進行測試:

model.load_state_dict(torch.load('resnet_caiq猜拳.pth'))
labels={0:'rock',1:'paper',2:'scissors'}
    images=[]
    image=Image.open(r'E:\桌面\1.png')
    image=image.convert('RGB')
    image=image.resize((64,64))
    image=transs(image)
    images.append(image)
    image= torch.stack(images, 0).cuda()
    label=model(image)
    _,prediction=torch.max(label.data,1)
    print("預(yù)測類別",labels[prediction.item()])

三、總結(jié)

本文只是簡單介紹了,通過pytorch訓(xùn)練resnet模型。調(diào)用訓(xùn)練好的模型,對圖片,視頻,攝像頭進行檢測。

本文只是簡單對圖片進行檢測,得到預(yù)測結(jié)果。

在這里運用了resnet18模型進行訓(xùn)練,其實還有更好的模型,得到更好的訓(xùn)練結(jié)果。

在目標(biāo)檢測領(lǐng)域,最著名的是YOLO,檢測速度非??欤趯崟r檢測領(lǐng)域很受歡迎,在一些游戲上,可以通過YOLO腳本,實現(xiàn)自動鎖定,追蹤之類的,比如現(xiàn)在歡迎的吃雞游戲,玩家通過腳本,實現(xiàn)自動識別人,進行射擊操作。在yolov3中,作者提到過yolo已經(jīng)運用到軍事中,出于道德層面的考慮,作者暫停了yolo的更新,在這之后v4,v5,v6以及之后的版本都是一些大佬接棒的。

在實時檢測中,現(xiàn)在AI在一些方面已經(jīng)超越人類了,在準(zhǔn)確率上雖然人腦的高層次演繹歸納能力是遠勝于AI的,但是在低級信息處理速度和精確度上,人類就很難比得過專精某個功能的AI了。

以上就是Pytorch卷積神經(jīng)網(wǎng)絡(luò)resent網(wǎng)絡(luò)實踐的詳細內(nèi)容,更多關(guān)于卷積神經(jīng)網(wǎng)絡(luò)resent的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python生成器,可迭代對象,迭代器區(qū)別和聯(lián)系

    python生成器,可迭代對象,迭代器區(qū)別和聯(lián)系

    這篇文章主要介紹了python生成器,可迭代對象,迭代器區(qū)別和聯(lián)系,通過對比用法讓大家更加深入理解相關(guān)知識,需要的朋友參考學(xué)習(xí)下吧。
    2018-02-02
  • 基于Python實現(xiàn)原生的登錄驗證碼詳情

    基于Python實現(xiàn)原生的登錄驗證碼詳情

    在前面的文章中,我有分享到 vue+drf+第三方滑動驗證碼接入的實現(xiàn),本文將要分享的是基于 python 實現(xiàn)原生的登錄驗證碼,需要的朋友可以參考一下
    2021-10-10
  • Python辦公自動化解決world文件批量轉(zhuǎn)換

    Python辦公自動化解決world文件批量轉(zhuǎn)換

    本文分享如何用 Python 來讀取 Word、寫入 Word、將 Word 轉(zhuǎn)換為 pdf。學(xué)會之后,如果遇到大量 Word 文件需要處理的時候,就不慌了
    2021-09-09
  • Python實現(xiàn)異步IO的示例

    Python實現(xiàn)異步IO的示例

    這篇文章主要介紹了Python實現(xiàn)異步IO的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-11-11
  • Python編程實現(xiàn)小姐姐跳舞并生成詞云視頻示例

    Python編程實現(xiàn)小姐姐跳舞并生成詞云視頻示例

    本文用Python做了一個詞云視頻,以另一種角度來看小姐姐跳舞視頻左半部分是小姐姐跳舞視頻,右半部分是根據(jù)動作生成的的詞云視頻,有需要的朋友可以借鑒參考下
    2021-10-10
  • PyTorch的Debug指南

    PyTorch的Debug指南

    這篇文章主要介紹了PyTorch的Debug的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用PyTorch,感興趣的朋友可以了解下
    2021-05-05
  • python3中rank函數(shù)的用法

    python3中rank函數(shù)的用法

    今天小編就為大家分享一篇python3中rank函數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python datetime模塊使用方法小結(jié)

    Python datetime模塊使用方法小結(jié)

    這篇文章主要介紹了Python datetime模塊使用方法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • python一行sql太長折成多行并且有多個參數(shù)的方法

    python一行sql太長折成多行并且有多個參數(shù)的方法

    今天小編就為大家分享一篇python一行sql太長折成多行并且有多個參數(shù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • VsCode終端激活anconda環(huán)境問題解決

    VsCode終端激活anconda環(huán)境問題解決

    本文主要介紹了VsCode終端激活anconda環(huán)境問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01

最新評論