pytorch 图像数据集管理

目录

1.数据集的管理说明

2.数据集Dataset类说明

3.图像分类常用的类 ImageFolder


1.数据集的管理说明

        pytorch使用Dataset来管理训练和测试数据集,前文说过 

torchvision.datasets.MNIST

        这些 torchvision.datasets里面的数据集都是继承Dataset而来,对Datasetd 管理使用DataLoader我们使用的的时候,只需要把Dataset类放在DataLoader这个容器里面,在训练的时候 for循环从DataLoader容器里面取出批次的数据,对模型进行训练。

2.数据集Dataset类说明

        我们可以继承Dataset类,对训练和测试数据进行管理,继承Dataset示例:

import torch
from torch.utils.data import Dataset
from torchvision import datasets
from torch.utils.data import DataLoader
from torchvision import transforms
import os
import cv2
#继承from torch.utils.data import Dataset
class CDataSet(Dataset):def __init__(self,path):self.path = pathself.list = os.listdir(path)self.len = len(self.list)self.name = ['cloudy','rain','shine','sunrise']self.trans = transforms.ToTensor()def __len__(self):return self.lendef __getitem__(self, item):self.imgpath = os.path.join(self.path,self.list[item])print(self.imgpath)img = cv2.imread(self.imgpath)img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)img = cv2.resize(img,(100,100))img = self.trans(img)for i,n in enumerate(self.name):if n in self.imgpath:label = i+1breakreturn img,labelds = CDataSet(r'E:\test\pythonProject\dataset\cloudy')
dl = DataLoader(ds,batch_size=16,shuffle=True)
print(len(ds))
print(len(dl))
print(type(ds))
print(type(dl))
print(next(iter(dl)))'''
D:\anaconda3\python.exe E:\test\pythonProject\test.py 
300
19
<class '__main__.CDataSet'>
<class 'torch.utils.data.dataloader.DataLoader'>
E:\test\pythonProject\dataset\cloudy\cloudy294.jpg
E:\test\pythonProject\dataset\cloudy\cloudy156.jpg
E:\test\pythonProject\dataset\cloudy\cloudy149.jpg
E:\test\pythonProject\dataset\cloudy\cloudy148.jpg
E:\test\pythonProject\dataset\cloudy\cloudy3.jpg
E:\test\pythonProject\dataset\cloudy\cloudy106.jpg
E:\test\pythonProject\dataset\cloudy\cloudy137.jpg
E:\test\pythonProject\dataset\cloudy\cloudy276.jpg
E:\test\pythonProject\dataset\cloudy\cloudy147.jpg
E:\test\pythonProject\dataset\cloudy\cloudy8.jpg
E:\test\pythonProject\dataset\cloudy\cloudy164.jpg
E:\test\pythonProject\dataset\cloudy\cloudy293.jpg
E:\test\pythonProject\dataset\cloudy\cloudy116.jpg
E:\test\pythonProject\dataset\cloudy\cloudy56.jpg
E:\test\pythonProject\dataset\cloudy\cloudy187.jpg
E:\test\pythonProject\dataset\cloudy\cloudy177.jpg
[tensor([[[[0.2235, 0.2471, 0.3569,  ..., 0.1490, 0.1373, 0.1373],[0.2902, 0.4039, 0.4078,  ..., 0.1529, 0.1373, 0.1294],[0.3294, 0.4941, 0.4000,  ..., 0.1529, 0.1333, 0.1137],...,[0.0118, 0.0118, 0.0118,  ..., 0.0078, 0.0078, 0.0078],[0.0118, 0.0118, 0.0118,  ..., 0.0039, 0.0039, 0.0039],[0.0118, 0.0118, 0.0118,  ..., 0.0039, 0.0039, 0.0039]],[[0.2196, 0.2471, 0.3608,  ..., 0.1725, 0.1608, 0.1608],[0.2824, 0.3961, 0.4118,  ..., 0.1765, 0.1608, 0.1529],[0.3216, 0.4863, 0.4039,  ..., 0.1765, 0.1569, 0.1373],...,[0.0235, 0.0235, 0.0235,  ..., 0.0078, 0.0078, 0.0078],[0.0235, 0.0235, 0.0235,  ..., 0.0078, 0.0078, 0.0078],[0.0235, 0.0235, 0.0235,  ..., 0.0157, 0.0196, 0.0157]],[[0.3098, 0.3412, 0.4510,  ..., 0.2196, 0.2078, 0.2078],[0.3686, 0.4824, 0.4980,  ..., 0.2235, 0.2078, 0.2000],[0.4078, 0.5725, 0.4863,  ..., 0.2235, 0.2039, 0.1843],...,[0.0000, 0.0000, 0.0000,  ..., 0.0157, 0.0157, 0.0157],[0.0000, 0.0000, 0.0000,  ..., 0.0157, 0.0157, 0.0157],[0.0000, 0.0000, 0.0000,  ..., 0.0078, 0.0039, 0.0078]]],[[[0.7059, 0.6902, 0.6824,  ..., 0.5961, 0.6000, 0.6118],[0.6980, 0.6824, 0.6745,  ..., 0.6039, 0.6078, 0.6196],[0.6863, 0.6706, 0.6588,  ..., 0.6196, 0.6235, 0.6353],...,[0.2706, 0.2941, 0.2706,  ..., 0.2745, 0.2745, 0.2706],[0.2745, 0.2745, 0.2667,  ..., 0.2784, 0.2902, 0.2745],[0.2784, 0.2706, 0.2784,  ..., 0.2824, 0.3020, 0.2784]],[[0.7176, 0.7020, 0.6941,  ..., 0.6235, 0.6275, 0.6392],[0.7098, 0.6941, 0.6863,  ..., 0.6314, 0.6353, 0.6471],[0.6941, 0.6863, 0.6706,  ..., 0.6471, 0.6510, 0.6627],...,[0.2784, 0.3020, 0.2824,  ..., 0.2824, 0.2824, 0.2784],[0.2824, 0.2824, 0.2745,  ..., 0.2863, 0.2980, 0.2824],[0.2863, 0.2784, 0.2863,  ..., 0.2902, 0.3098, 0.2824]],[[0.7412, 0.7294, 0.7176,  ..., 0.6471, 0.6510, 0.6627],[0.7373, 0.7216, 0.7137,  ..., 0.6549, 0.6588, 0.6706],[0.7255, 0.7098, 0.6980,  ..., 0.6706, 0.6745, 0.6863],...,[0.1961, 0.2196, 0.2000,  ..., 0.2000, 0.2000, 0.1961],[0.2000, 0.2000, 0.1922,  ..., 0.2039, 0.2157, 0.2000],[0.2039, 0.1961, 0.2039,  ..., 0.2078, 0.2275, 0.2039]]],[[[0.3176, 0.3255, 0.3294,  ..., 0.5529, 0.5255, 0.4824],[0.3098, 0.3176, 0.3216,  ..., 0.5608, 0.5255, 0.4824],[0.3059, 0.3098, 0.3098,  ..., 0.5686, 0.4941, 0.4588],...,[0.4510, 0.4549, 0.3176,  ..., 0.2627, 0.3059, 0.3333],[0.3843, 0.4980, 0.4000,  ..., 0.3804, 0.4235, 0.3804],[0.4549, 0.6353, 0.7333,  ..., 0.4902, 0.5882, 0.6627]],[[0.3333, 0.3373, 0.3412,  ..., 0.5961, 0.5765, 0.5333],[0.3255, 0.3333, 0.3373,  ..., 0.6039, 0.5686, 0.5333],[0.3216, 0.3255, 0.3255,  ..., 0.6157, 0.5412, 0.5098],...,[0.4275, 0.4275, 0.3255,  ..., 0.2627, 0.2902, 0.3176],[0.3804, 0.4510, 0.3961,  ..., 0.3529, 0.3843, 0.3529],[0.4275, 0.5333, 0.6039,  ..., 0.4353, 0.5098, 0.5569]],[[0.3804, 0.3961, 0.4000,  ..., 0.6667, 0.6431, 0.6000],[0.3725, 0.3804, 0.3843,  ..., 0.6745, 0.6392, 0.6000],[0.3686, 0.3725, 0.3725,  ..., 0.6784, 0.6118, 0.5843],...,[0.3843, 0.3843, 0.3255,  ..., 0.2353, 0.2549, 0.2706],[0.3412, 0.3882, 0.3725,  ..., 0.2902, 0.3098, 0.2863],[0.3804, 0.4039, 0.4275,  ..., 0.3294, 0.3333, 0.3529]]],...,[[[0.5843, 0.6000, 0.6471,  ..., 0.3294, 0.3255, 0.3333],[0.5412, 0.5529, 0.6627,  ..., 0.3373, 0.3333, 0.3373],[0.5137, 0.5098, 0.6235,  ..., 0.3451, 0.3451, 0.3412],...,[0.2980, 0.1098, 0.0824,  ..., 0.0000, 0.0000, 0.0000],[0.0078, 0.0000, 0.0039,  ..., 0.0000, 0.0000, 0.0000],[0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.0000]],[[0.5843, 0.6000, 0.6471,  ..., 0.3294, 0.3255, 0.3333],[0.5412, 0.5529, 0.6627,  ..., 0.3373, 0.3333, 0.3373],[0.5137, 0.5098, 0.6235,  ..., 0.3451, 0.3451, 0.3412],...,[0.2980, 0.1098, 0.0824,  ..., 0.0000, 0.0000, 0.0000],[0.0078, 0.0000, 0.0039,  ..., 0.0000, 0.0000, 0.0000],[0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.0000]],[[0.5843, 0.6000, 0.6471,  ..., 0.3294, 0.3255, 0.3333],[0.5412, 0.5529, 0.6627,  ..., 0.3373, 0.3333, 0.3373],[0.5137, 0.5098, 0.6235,  ..., 0.3451, 0.3451, 0.3412],...,[0.2980, 0.1098, 0.0824,  ..., 0.0000, 0.0000, 0.0000],[0.0078, 0.0000, 0.0039,  ..., 0.0000, 0.0000, 0.0000],[0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.0000]]],[[[0.5608, 0.5843, 0.6196,  ..., 0.4431, 0.4314, 0.4275],[0.5529, 0.5725, 0.6039,  ..., 0.4510, 0.4392, 0.4392],[0.5569, 0.5647, 0.5922,  ..., 0.4588, 0.4510, 0.4549],...,[0.1020, 0.0784, 0.0627,  ..., 0.1255, 0.1373, 0.1216],[0.0431, 0.0627, 0.0510,  ..., 0.0902, 0.1176, 0.1294],[0.0902, 0.1059, 0.0588,  ..., 0.0902, 0.0941, 0.1020]],[[0.6275, 0.6510, 0.6863,  ..., 0.5020, 0.4902, 0.4863],[0.6235, 0.6392, 0.6706,  ..., 0.5098, 0.4980, 0.4980],[0.6196, 0.6314, 0.6588,  ..., 0.5176, 0.5098, 0.5098],...,[0.1373, 0.1176, 0.0980,  ..., 0.1569, 0.1725, 0.1569],[0.0784, 0.0941, 0.0863,  ..., 0.1255, 0.1529, 0.1647],[0.1255, 0.1412, 0.0941,  ..., 0.1255, 0.1294, 0.1373]],[[0.6039, 0.6275, 0.6627,  ..., 0.4824, 0.4706, 0.4667],[0.5961, 0.6157, 0.6471,  ..., 0.4902, 0.4784, 0.4784],[0.5961, 0.6078, 0.6353,  ..., 0.4980, 0.4902, 0.4941],...,[0.1255, 0.1020, 0.0863,  ..., 0.1451, 0.1608, 0.1451],[0.0667, 0.0863, 0.0745,  ..., 0.1137, 0.1412, 0.1529],[0.1137, 0.1294, 0.0824,  ..., 0.1137, 0.1176, 0.1255]]],[[[0.1922, 0.1882, 0.1843,  ..., 0.1608, 0.1647, 0.1686],[0.1961, 0.1922, 0.1882,  ..., 0.1686, 0.1686, 0.1725],[0.2000, 0.2000, 0.1961,  ..., 0.1804, 0.1804, 0.1843],...,[0.3686, 0.3882, 0.3961,  ..., 0.3098, 0.3098, 0.3098],[0.3765, 0.3882, 0.3882,  ..., 0.2980, 0.2980, 0.2980],[0.3725, 0.3804, 0.3804,  ..., 0.2941, 0.2941, 0.2941]],[[0.1922, 0.1882, 0.1843,  ..., 0.1608, 0.1647, 0.1686],[0.1961, 0.1922, 0.1882,  ..., 0.1686, 0.1686, 0.1725],[0.2000, 0.2000, 0.1961,  ..., 0.1804, 0.1804, 0.1843],...,[0.3686, 0.3882, 0.3961,  ..., 0.3098, 0.3098, 0.3098],[0.3765, 0.3882, 0.3882,  ..., 0.2980, 0.2980, 0.2980],[0.3725, 0.3804, 0.3804,  ..., 0.2941, 0.2941, 0.2941]],[[0.1922, 0.1882, 0.1843,  ..., 0.1608, 0.1647, 0.1686],[0.1961, 0.1922, 0.1882,  ..., 0.1686, 0.1686, 0.1725],[0.2000, 0.2000, 0.1961,  ..., 0.1804, 0.1804, 0.1843],...,[0.3686, 0.3882, 0.3961,  ..., 0.3098, 0.3098, 0.3098],[0.3765, 0.3882, 0.3882,  ..., 0.2980, 0.2980, 0.2980],[0.3725, 0.3804, 0.3804,  ..., 0.2941, 0.2941, 0.2941]]]]), tensor([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])]进程已结束,退出代码为 0'''

这里用到的文件夹如图:

注意:这里主要写 

def __init__(self,path):
def __len__(self):
def __getitem__(self, item):

这三个函数

3.图像分类常用的类 ImageFolder

        ImageFolder 使用示例:

        首先整理图像分类分别放在不同的文件夹里面:

然后直接使用 ImageFolder 装载 dataset 文件夹,就会自动分类图片形成数据集可以直接使用:

import torch
from torch.utils.data import Dataset
from torchvision import datasets
from torch.utils.data import DataLoader
from torchvision import transformstrans = transforms.Compose([transforms.Resize((96,96)),transforms.ToTensor()])
ds = datasets.ImageFolder("./dataset",transform=trans)test_ds,train_ds = torch.utils.data.random_split(ds,[len(ds)//5,len(ds)-len(ds)//5])#注意这里需要整除因为这里使用整数
dl = DataLoader(train_ds,batch_size=16,shuffle=True)print(ds.classes)
print(ds.class_to_idx)
print(len(test_ds))
print(len(train_ds))
print(next(iter(dl)))'''
D:\anaconda3\python.exe E:\test\pythonProject\test.py 
['cloudy', 'rain', 'shine', 'sunrise']
{'cloudy': 0, 'rain': 1, 'shine': 2, 'sunrise': 3}
225
900
[tensor([[[[0.0980, 0.0745, 0.0706,  ..., 0.4431, 0.4314, 0.4157],[0.0627, 0.0667, 0.0706,  ..., 0.4941, 0.4510, 0.4510],[0.1529, 0.1451, 0.1412,  ..., 0.3882, 0.4275, 0.4510],...,[0.1176, 0.1176, 0.1176,  ..., 0.1333, 0.1255, 0.1608],[0.1137, 0.1137, 0.1137,  ..., 0.1373, 0.1569, 0.2039],[0.1098, 0.1098, 0.1098,  ..., 0.1294, 0.1961, 0.2824]],[[0.2745, 0.2314, 0.2118,  ..., 0.3843, 0.3725, 0.3569],[0.1922, 0.1765, 0.1686,  ..., 0.4353, 0.3922, 0.3922],[0.2275, 0.2000, 0.1843,  ..., 0.3294, 0.3725, 0.3961],...,[0.0353, 0.0353, 0.0353,  ..., 0.0784, 0.0667, 0.1059],[0.0314, 0.0314, 0.0314,  ..., 0.0784, 0.0824, 0.1216],[0.0275, 0.0275, 0.0275,  ..., 0.0745, 0.1137, 0.1725]],[[0.4471, 0.4118, 0.3961,  ..., 0.3647, 0.3529, 0.3373],[0.3490, 0.3373, 0.3333,  ..., 0.4235, 0.3804, 0.3765],[0.3529, 0.3333, 0.3255,  ..., 0.3216, 0.3608, 0.3882],...,[0.0235, 0.0235, 0.0235,  ..., 0.0431, 0.0353, 0.0549],[0.0196, 0.0196, 0.0196,  ..., 0.0471, 0.0392, 0.0392],[0.0157, 0.0157, 0.0157,  ..., 0.0353, 0.0549, 0.0706]]],[[[0.0941, 0.0941, 0.0196,  ..., 0.1490, 0.1961, 0.1490],[0.1059, 0.1137, 0.0471,  ..., 0.1529, 0.1412, 0.1176],[0.0745, 0.1255, 0.1059,  ..., 0.1569, 0.1373, 0.1176],...,[0.2196, 0.2549, 0.3059,  ..., 0.4000, 0.3922, 0.3765],[0.2118, 0.2471, 0.3020,  ..., 0.3804, 0.3686, 0.3608],[0.1922, 0.2235, 0.2784,  ..., 0.3882, 0.3843, 0.3725]],[[0.2000, 0.1725, 0.0431,  ..., 0.1686, 0.2196, 0.1569],[0.2196, 0.2039, 0.0706,  ..., 0.1765, 0.1647, 0.1373],[0.2000, 0.2275, 0.1373,  ..., 0.1804, 0.1608, 0.1412],...,[0.2157, 0.2510, 0.3059,  ..., 0.3804, 0.3686, 0.3647],[0.2118, 0.2471, 0.3020,  ..., 0.3686, 0.3529, 0.3569],[0.1922, 0.2235, 0.2784,  ..., 0.3843, 0.3804, 0.3686]],[[0.1961, 0.1765, 0.0627,  ..., 0.1725, 0.2196, 0.1647],[0.2118, 0.2039, 0.0941,  ..., 0.1804, 0.1647, 0.1451],[0.1882, 0.2235, 0.1569,  ..., 0.1843, 0.1608, 0.1608],...,[0.1961, 0.2314, 0.2980,  ..., 0.3804, 0.3686, 0.3608],[0.1961, 0.2314, 0.2941,  ..., 0.3647, 0.3529, 0.3490],[0.1843, 0.2118, 0.2706,  ..., 0.3765, 0.3725, 0.3608]]],[[[0.7804, 0.7804, 0.7804,  ..., 0.6627, 0.6588, 0.6549],[0.7765, 0.7765, 0.7765,  ..., 0.6588, 0.6549, 0.6510],[0.7725, 0.7725, 0.7725,  ..., 0.6471, 0.6431, 0.6431],...,[0.1216, 0.1333, 0.1490,  ..., 0.1647, 0.1647, 0.1608],[0.1216, 0.1255, 0.1451,  ..., 0.1725, 0.1725, 0.1765],[0.1176, 0.1255, 0.1451,  ..., 0.1686, 0.1569, 0.1451]],[[0.7843, 0.7843, 0.7843,  ..., 0.6667, 0.6627, 0.6588],[0.7804, 0.7804, 0.7804,  ..., 0.6627, 0.6588, 0.6549],[0.7765, 0.7765, 0.7765,  ..., 0.6510, 0.6471, 0.6471],...,[0.1608, 0.1490, 0.1373,  ..., 0.1686, 0.1686, 0.1647],[0.1569, 0.1451, 0.1294,  ..., 0.1765, 0.1765, 0.1804],[0.1569, 0.1412, 0.1294,  ..., 0.1725, 0.1608, 0.1490]],[[0.8039, 0.8039, 0.8039,  ..., 0.6863, 0.6824, 0.6784],[0.8000, 0.8000, 0.8000,  ..., 0.6824, 0.6784, 0.6745],[0.7961, 0.7961, 0.7961,  ..., 0.6706, 0.6667, 0.6667],...,[0.0706, 0.0667, 0.0745,  ..., 0.1059, 0.1059, 0.1020],[0.0745, 0.0667, 0.0745,  ..., 0.1137, 0.1137, 0.1176],[0.0745, 0.0706, 0.0745,  ..., 0.1098, 0.0980, 0.0863]]],...,[[[0.0275, 0.1059, 0.2157,  ..., 0.0196, 0.0196, 0.0196],[0.0235, 0.1020, 0.1765,  ..., 0.0235, 0.0235, 0.0196],[0.0196, 0.0902, 0.1255,  ..., 0.0314, 0.0314, 0.0275],...,[0.0784, 0.1059, 0.1255,  ..., 0.1294, 0.1020, 0.0745],[0.0745, 0.0863, 0.1020,  ..., 0.0627, 0.0588, 0.0431],[0.0588, 0.0667, 0.0824,  ..., 0.0667, 0.0627, 0.0353]],[[0.0275, 0.1059, 0.2157,  ..., 0.0157, 0.0157, 0.0157],[0.0235, 0.1020, 0.1765,  ..., 0.0235, 0.0235, 0.0196],[0.0196, 0.0902, 0.1255,  ..., 0.0314, 0.0314, 0.0275],...,[0.0588, 0.0863, 0.1059,  ..., 0.1059, 0.0824, 0.0549],[0.0549, 0.0667, 0.0824,  ..., 0.0471, 0.0431, 0.0275],[0.0392, 0.0471, 0.0627,  ..., 0.0588, 0.0510, 0.0275]],[[0.0275, 0.1059, 0.2157,  ..., 0.0275, 0.0275, 0.0235],[0.0235, 0.1020, 0.1765,  ..., 0.0314, 0.0314, 0.0275],[0.0196, 0.0902, 0.1255,  ..., 0.0392, 0.0392, 0.0353],...,[0.0471, 0.0745, 0.0941,  ..., 0.1059, 0.0824, 0.0549],[0.0431, 0.0549, 0.0706,  ..., 0.0431, 0.0392, 0.0235],[0.0275, 0.0353, 0.0510,  ..., 0.0510, 0.0471, 0.0235]]],[[[0.1412, 0.1412, 0.1412,  ..., 0.1647, 0.1686, 0.1765],[0.1451, 0.1373, 0.1333,  ..., 0.1647, 0.1686, 0.1765],[0.1490, 0.1412, 0.1373,  ..., 0.1725, 0.1765, 0.1843],...,[0.0039, 0.0039, 0.0039,  ..., 0.0118, 0.0078, 0.0078],[0.0039, 0.0039, 0.0039,  ..., 0.0078, 0.0039, 0.0039],[0.0039, 0.0039, 0.0039,  ..., 0.0078, 0.0039, 0.0039]],[[0.2118, 0.2078, 0.2078,  ..., 0.2353, 0.2353, 0.2353],[0.2157, 0.2118, 0.2078,  ..., 0.2392, 0.2392, 0.2431],[0.2196, 0.2157, 0.2118,  ..., 0.2431, 0.2431, 0.2431],...,[0.0039, 0.0039, 0.0039,  ..., 0.0118, 0.0078, 0.0078],[0.0039, 0.0039, 0.0039,  ..., 0.0078, 0.0039, 0.0039],[0.0039, 0.0039, 0.0039,  ..., 0.0078, 0.0039, 0.0039]],[[0.3137, 0.3137, 0.3216,  ..., 0.3373, 0.3373, 0.3255],[0.3176, 0.3137, 0.3216,  ..., 0.3412, 0.3412, 0.3412],[0.3137, 0.3176, 0.3294,  ..., 0.3451, 0.3451, 0.3451],...,[0.0039, 0.0039, 0.0039,  ..., 0.0118, 0.0078, 0.0078],[0.0039, 0.0039, 0.0039,  ..., 0.0078, 0.0039, 0.0039],[0.0039, 0.0039, 0.0039,  ..., 0.0078, 0.0039, 0.0039]]],[[[0.0157, 0.0157, 0.0157,  ..., 0.0980, 0.0941, 0.0824],[0.0196, 0.0196, 0.0196,  ..., 0.0980, 0.0941, 0.0824],[0.0235, 0.0235, 0.0235,  ..., 0.0980, 0.0941, 0.0824],...,[0.0078, 0.0078, 0.0039,  ..., 0.0157, 0.0196, 0.0196],[0.0039, 0.0039, 0.0039,  ..., 0.0157, 0.0118, 0.0039],[0.0000, 0.0000, 0.0000,  ..., 0.0157, 0.0078, 0.0000]],[[0.0510, 0.0510, 0.0510,  ..., 0.1294, 0.1255, 0.1333],[0.0549, 0.0549, 0.0549,  ..., 0.1294, 0.1255, 0.1333],[0.0588, 0.0588, 0.0588,  ..., 0.1294, 0.1255, 0.1333],...,[0.0078, 0.0078, 0.0039,  ..., 0.0118, 0.0157, 0.0157],[0.0039, 0.0039, 0.0039,  ..., 0.0118, 0.0078, 0.0000],[0.0000, 0.0000, 0.0000,  ..., 0.0118, 0.0039, 0.0000]],[[0.1647, 0.1647, 0.1647,  ..., 0.2824, 0.2784, 0.2706],[0.1686, 0.1686, 0.1686,  ..., 0.2824, 0.2784, 0.2706],[0.1725, 0.1725, 0.1725,  ..., 0.2824, 0.2784, 0.2706],...,[0.0157, 0.0157, 0.0118,  ..., 0.0353, 0.0392, 0.0392],[0.0118, 0.0118, 0.0118,  ..., 0.0353, 0.0314, 0.0235],[0.0078, 0.0078, 0.0078,  ..., 0.0353, 0.0275, 0.0196]]]]), tensor([3, 1, 0, 3, 3, 2, 1, 0, 0, 0, 2, 3, 0, 0, 3, 3])]进程已结束,退出代码为 0'''

注意:这里使用函数

train_ds,test_ds = torch.utils.data.random_split(ds,[len(ds)//5,len(ds)-len(ds)//5])#注意这里需要整除,因为这里需要使用整数。

        把数据集分为了训练和测试数据集,从Dataset继承的类都可以用这个分类,记住DatasetDataLoader这个基础类是在torch里面,而关于图片的处理类基本都在torchvision 里面,比如图片的转换到tensor,图片放大缩小功能。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/498346.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【LeetCode:108. 将有序数组转换为二叉搜索树 + 二叉树+递归】

&#x1f680; 算法题 &#x1f680; &#x1f332; 算法刷题专栏 | 面试必备算法 | 面试高频算法 &#x1f340; &#x1f332; 越难的东西,越要努力坚持&#xff0c;因为它具有很高的价值&#xff0c;算法就是这样✨ &#x1f332; 作者简介&#xff1a;硕风和炜&#xff0c;…

Node.js+Express后端,自定义接口

6分钟学会Express 后端 API 开发 Node.js 2020最新版_哔哩哔哩_bilibili 要使用Node.js和Express搭建一个简单的后台服务器,用于接收带有token的请求头,你可以按照以下步骤进行操作: 首先,确保你已经安装了Node.js和npm(Node.js的包管理器)。 创建一个新的项目文件夹并…

VUE3:统计分析页面布局+自适应页面参考

一、布局 <template><div class"container1"><div class"form white"><el-form :inline"true" :rules"rules" :model"queryParams" label-width"80px" ref"querParmRef"><e…

apache 模式、优化、功能 与 nginx优化、应用

一、I/O模型——Input/Output模型 1.同步/异步 A程序需要调用B程序的某一个功能&#xff0c;A发送一个请求需要B完成一个任务 同步&#xff1a;B不会主动去通知A是否完成需要A自己去问 异步&#xff1a;B会主动通知A是否完成 2.阻塞/非阻塞 A发送一个请求需要B完成一个任务 …

hive报错:FAILED: NullPointerException null

发现问题 起因是我虚拟机的hive不管执行什么命令都报空指针异常的错误 我也在网上找了很多相关问题的资料&#xff0c;发现都不是我这个问题的解决方法&#xff0c;后来在hive官网上与hive 3.1.3版本相匹配的hadoop版本是3.x的版本&#xff0c;而我的hadoop版本还是2.7.2的版本…

简单1步搞定 NET Framework 3.5 ,离线下载

https://download.csdn.net/download/m0_72735063/88889747?spm1001.2014.3001.5503

Redisson限流算法

引入依赖 <dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.12.3</version> </dependency>建议版本使用3.15.5以上 使用 这边写了一个demo示例&#xff0c;定…

给MATLAB安装runtime插件

给MATLAB安装runtime插件&#xff0c;将MATLAB程序打包成exe文件方法 1.查看已安装的MATLAB是否有runtime插件。操作:在command窗口直接输入mcrinstaller。 如果如图上所示&#xff0c;那就是没有。(2019版本以后包括2019一般均没有) 2.下载MATLAB对应子版本的runtime。(很多…

测评ONLYOFFICE 8.0版本:办公利器再升级

测评ONLYOFFICE 8.0版本&#xff1a;办公利器再升级 前言注册使用升级功能速览全新外观设计wordexcelPPTPDF 协作功能强化更强大的功能复杂表单的填写 移动端优化结语 前言 随着科技的不断发展&#xff0c;办公软件在提升用户体验和工作效率方面扮演着越来越重要的角色。作为一…

王者荣耀,急于补齐内容短板

问十个人&#xff0c;有九个人知道《王者荣耀》&#xff1b;但如果再问十个知道《王者荣耀》的人&#xff0c;这款游戏到底讲了一个什么故事&#xff0c;每个角色又有怎样的背景&#xff0c;可能十个人都不知道。 整个新年档口&#xff0c;《王者荣耀》都很忙碌。 1月&#x…

网上申请手机流量卡:便捷、快速、无忧的选择

随着互联网的普及和科技的发展&#xff0c;越来越多的人选择在网上办理各种业务。其中&#xff0c;网上申请手机流量卡成为了一种便捷、快速、无忧的选择。今天小编将详细介绍网上申请手机流量卡的流程和优势&#xff0c;帮助您更好地了解这种新型的办理方式&#xff0c;希望能…

JAVA计算表达式

需求&#xff1a; 1、例如if(score>85){return 1;}else if(score>70){return 2;}else if(score>60){return 3;}else{return 4;}有这一串字符串&#xff0c;要执行这个字符串&#xff0c; 如果score为86分&#xff0c;则能得到1&#xff1b;如果score为30分&#xff…