主頁 > 知識庫 > PyTorch 如何將CIFAR100數(shù)據(jù)按類標(biāo)歸類保存

PyTorch 如何將CIFAR100數(shù)據(jù)按類標(biāo)歸類保存

熱門標(biāo)簽:廣告地圖標(biāo)注app 白銀外呼系統(tǒng) 哈爾濱ai外呼系統(tǒng)定制 激戰(zhàn)2地圖標(biāo)注 騰訊外呼線路 海南400電話如何申請 公司電話機器人 唐山智能外呼系統(tǒng)一般多少錢 陜西金融外呼系統(tǒng)

few-shot learning的采樣

Few-shot learning 基于任務(wù)對模型進行訓(xùn)練,在N-way-K-shot中,一個任務(wù)中的meta-training中含有N類,每一類抽取K個樣本構(gòu)成support set, query set則是在剛才抽取的N類剩余的樣本中sample一定數(shù)量的樣本(可以是均勻采樣,也可以是不均勻采樣)。

對數(shù)據(jù)按類標(biāo)歸類

針對上述情況,我們需要使用不同類別放置在不同文件夾的數(shù)據(jù)集。但有時,數(shù)據(jù)并沒有按類放置,這時就需要對數(shù)據(jù)進行處理。

下面以CIFAR100為列(不含N-way-k-shot的采樣):

import os
from skimage import io
import torchvision as tv
import numpy as np
import torch
def Cifar100(root):
    character = [[] for i in range(100)]
    train_set = tv.datasets.CIFAR100(root, train=True, download=True)
    test_set = tv.datasets.CIFAR100(root, train=False, download=True)
    dataset = []
    for (X, Y) in zip(train_set.train_data, train_set.train_labels):  # 將train_set的數(shù)據(jù)和label讀入列表
        dataset.append(list((X, Y)))
    for (X, Y) in zip(test_set.test_data, test_set.test_labels):  # 將test_set的數(shù)據(jù)和label讀入列表
        dataset.append(list((X, Y)))
    for X, Y in dataset:
        character[Y].append(X)  # 32*32*3
    character = np.array(character)
    character = torch.from_numpy(character)
    # 按類打亂
    np.random.seed(6)
    shuffle_class = np.arange(len(character))
    np.random.shuffle(shuffle_class)
    character = character[shuffle_class]
    # shape = self.character.shape
    # self.character = self.character.view(shape[0], shape[1], shape[4], shape[2], shape[3])  # 將數(shù)據(jù)轉(zhuǎn)成channel在前
    meta_training, meta_validation, meta_testing = \

    character[:64], character[64:80], character[80:]  # meta_training : meta_validation : Meta_testing = 64類:16類:20類
    dataset = []  # 釋放內(nèi)存
    character = []
    os.mkdir(os.path.join(root, 'meta_training'))
    for i, per_class in enumerate(meta_training):
        character_path = os.path.join(root, 'meta_training', 'character_' + str(i))
        os.mkdir(character_path)
        for j, img in enumerate(per_class):
            img_path = character_path + '/' + str(j) + ".jpg"
            io.imsave(img_path, img)
    os.mkdir(os.path.join(root, 'meta_validation'))
    for i, per_class in enumerate(meta_validation):
        character_path = os.path.join(root, 'meta_validation', 'character_' + str(i))
        os.mkdir(character_path)
        for j, img in enumerate(per_class):
            img_path = character_path + '/' + str(j) + ".jpg"
            io.imsave(img_path, img)
    os.mkdir(os.path.join(root, 'meta_testing'))
    for i, per_class in enumerate(meta_testing):
        character_path = os.path.join(root, 'meta_testing', 'character_' + str(i))
        os.mkdir(character_path)
        for j, img in enumerate(per_class):
            img_path = character_path + '/' + str(j) + ".jpg"
            io.imsave(img_path, img)
if __name__ == '__main__':
    root = '/home/xie/文檔/datasets/cifar_100'
    Cifar100(root)
    print("-----------------")

補充:使用Pytorch對數(shù)據(jù)集CIFAR-10進行分類

主要是以下幾個步驟:

1、下載并預(yù)處理數(shù)據(jù)集

2、定義網(wǎng)絡(luò)結(jié)構(gòu)

3、定義損失函數(shù)和優(yōu)化器

4、訓(xùn)練網(wǎng)絡(luò)并更新參數(shù)

5、測試網(wǎng)絡(luò)效果

#數(shù)據(jù)加載和預(yù)處理
#使用CIFAR-10數(shù)據(jù)進行分類實驗
import torch as t
import torchvision as tv
import torchvision.transforms as transforms
from torchvision.transforms import ToPILImage
show = ToPILImage() # 可以把Tensor轉(zhuǎn)成Image,方便可視化
 
#定義對數(shù)據(jù)的預(yù)處理
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)),  #歸一化
])
 
#訓(xùn)練集
trainset = tv.datasets.CIFAR10(
    root = './data/',
    train = True,
    download = True,
    transform = transform
)
 
trainloader = t.utils.data.DataLoader(
    trainset,
    batch_size = 4,
    shuffle = True,
    num_workers = 2,
)
 
#測試集
testset = tv.datasets.CIFAR10(
    root = './data/',
    train = False,
    download = True,
    transform = transform,
)
testloader = t.utils.data.DataLoader(
    testset,
    batch_size = 4,
    shuffle = False,
    num_workers = 2,
)
 
classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

初次下載需要一些時間,運行結(jié)束后,顯示如下:

import torch.nn as nn
import torch.nn.functional as F
import time
start = time.time()#計時
#定義網(wǎng)絡(luò)結(jié)構(gòu)
class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.conv1 = nn.Conv2d(3,6,5)
        self.conv2 = nn.Conv2d(6,16,5)
        self.fc1 = nn.Linear(16*5*5,120)
        self.fc2 = nn.Linear(120,84)
        self.fc3 = nn.Linear(84,10)
        
    def forward(self,x):
        x = F.max_pool2d(F.relu(self.conv1(x)),2)
        x = F.max_pool2d(F.relu(self.conv2(x)),2)
        
        x = x.view(x.size()[0],-1)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
net = Net()
print(net)

顯示net結(jié)構(gòu)如下:

#定義優(yōu)化和損失
loss_func = nn.CrossEntropyLoss()  #交叉熵損失函數(shù)
optimizer = t.optim.SGD(net.parameters(),lr = 0.001,momentum = 0.9)
 
#訓(xùn)練網(wǎng)絡(luò)
for epoch in range(2):
    running_loss = 0
    for i,data in enumerate(trainloader,0):
        inputs,labels = data
       
        outputs = net(inputs)
        loss = loss_func(outputs,labels)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        running_loss +=loss.item()
        if i%2000 ==1999:
            print('epoch:',epoch+1,'|i:',i+1,'|loss:%.3f'%(running_loss/2000))
            running_loss = 0.0
end = time.time()
time_using = end - start
print('finish training')
print('time:',time_using)

結(jié)果如下:

下一步進行使用測試集進行網(wǎng)絡(luò)測試:

#測試網(wǎng)絡(luò)
correct = 0 #定義的預(yù)測正確的圖片數(shù)
total = 0#總共圖片個數(shù)
with t.no_grad():
    for data in testloader:
        images,labels = data
        outputs = net(images)
        _,predict = t.max(outputs,1)
        total += labels.size(0)
        correct += (predict == labels).sum()
print('測試集中的準(zhǔn)確率為:%d%%'%(100*correct/total))

結(jié)果如下:

簡單的網(wǎng)絡(luò)訓(xùn)練確實要比10%的比例高一點:)

在GPU中訓(xùn)練:

#在GPU中訓(xùn)練
device = t.device('cuda:0' if t.cuda.is_available() else 'cpu')
 
net.to(device)
images = images.to(device)
labels = labels.to(device)
 
output = net(images)
loss = loss_func(output,labels)
 
loss

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • pytorch_detach 切斷網(wǎng)絡(luò)反傳方式
  • pytorch 禁止/允許計算局部梯度的操作
  • 如何利用Pytorch計算三角函數(shù)
  • 聊聊PyTorch中eval和no_grad的關(guān)系
  • Pytorch實現(xiàn)圖像識別之?dāng)?shù)字識別(附詳細注釋)
  • Pytorch實現(xiàn)全連接層的操作
  • pytorch 優(yōu)化器(optim)不同參數(shù)組,不同學(xué)習(xí)率設(shè)置的操作
  • PyTorch的Debug指南
  • Python深度學(xué)習(xí)之使用Pytorch搭建ShuffleNetv2
  • win10系統(tǒng)配置GPU版本Pytorch的詳細教程
  • 淺談pytorch中的nn.Sequential(*net[3: 5])是啥意思
  • pytorch visdom安裝開啟及使用方法
  • PyTorch CUDA環(huán)境配置及安裝的步驟(圖文教程)
  • pytorch中的nn.ZeroPad2d()零填充函數(shù)實例詳解
  • 使用pytorch實現(xiàn)線性回歸
  • pytorch實現(xiàn)線性回歸以及多元回歸
  • PyTorch學(xué)習(xí)之軟件準(zhǔn)備與基本操作總結(jié)

標(biāo)簽:益陽 鷹潭 黑龍江 常德 四川 上海 惠州 黔西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PyTorch 如何將CIFAR100數(shù)據(jù)按類標(biāo)歸類保存》,本文關(guān)鍵詞  PyTorch,如何,將,CIFAR100,數(shù)據(jù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PyTorch 如何將CIFAR100數(shù)據(jù)按類標(biāo)歸類保存》相關(guān)的同類信息!
  • 本頁收集關(guān)于PyTorch 如何將CIFAR100數(shù)據(jù)按類標(biāo)歸類保存的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章