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.mzph.cn/news/717208.shtml

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

相关文章

[Redis]——Redis命令手册set、list、sortedset

&#x1f333;List类型常见命令 LPUSH / RPUSH [KEY] [element] …… 向列表左侧或者右侧插入一个或多个元素 LPOP / RPOP [key] 删除左边或者右边第一个元素 LRANGE [key] start end 返回索引start到end的元素&#xff08;索引从0开始&#xff09; BLPOP / BRPOP [key] [等…

【C++】类的默认成员函数(上)

&#x1f525;博客主页&#xff1a; 小羊失眠啦. &#x1f3a5;系列专栏&#xff1a;《C语言》 《数据结构》 《C》 《Linux》 《Cpolar》 ❤️感谢大家点赞&#x1f44d;收藏⭐评论✍️ 文章目录 一、默认成员函数二、构造函数构造函数的概念及特性 三、析构函数析构函数的特性…

蓝桥杯倒计时 43天 - 前缀和,单调栈

最大数组和 算法思路&#xff1a;利用前缀和化简 for 循环将 n^2 简化成 nn&#xff0c;以空间换时间。枚举每个 m&#xff0c;m是删除最小两个数&#xff0c;那k-m就是删除最大数&#xff0c;m<k&#xff0c;求和最大的值。暴力就是枚举 m-O(n)&#xff0c;计算前 n-(k-m)的…

PCSA时钟控制集成之时钟门控级别

这一部分描述了&#xff1a; • 时钟门控的级别。 • 实现最大效果的时钟门位置。 • 实现有效和高效时钟门控的集成方法。 时钟树是由时钟缓冲器构建的&#xff0c;这些缓冲器在时钟源&#xff08;时钟输入或PLL&#xff09;与时钟终端&#xff08;寄存器或RAM&#xff09…

ULN2003(COM的作用)

单路内部电路原理图 三极管多级放大电路&#xff0c;最大可达到500ma&#xff1b; 典型应用&#xff1a; ULN2003属于灌电流驱动方式&#xff1b;输入与输出电平反向&#xff0c;下拉4K电阻&#xff0c;为解决单片机上电IO瞬间不稳定&#xff1b; COM端 1.可悬空&#xff1a…

git标签操作

一.标签管理 1.理解标签 标签 tag &#xff0c;可以简单的理解为是对某次 commit 的⼀个标识&#xff0c;相当于起了⼀个别名,当我们需要回退到某个重要版本时&#xff0c;直接使⽤标签就能很快定位到 2.创建标签 ⾸先&#xff0c;切换到需要打标签的分⽀上,然后&#xf…

经典目标检测网络Yolo——原理部分

目标检测问题 分为两个子问题: 找到图片中哪些位置、哪些区域含有目标对象识别这些区域中的目标对象是什么基于CNN的目标检测算法能够很好的解决第二个问题,在一张图片仅含一个对象,且该对象占据了整张图片绝大部分面积时,基于CNN的对象识别算法具有很高的准确率。 一种定…

操作系统(1)——学习导论(Ⅱ)

目录 小程一言专栏链接: [link](http://t.csdnimg.cn/6grrU) 学习导论&#xff08;Ⅱ&#xff09;操作系统-赏前人佳作大型操作系统大型操作系统的一些特点和功能举例 服务器操作系统服务器操作系统特点和功能举例 多处理器操作系统举例 个人计算机操作系统举例 掌上计算机操作…

设计模式:什么是设计模式?①

一、什么是设计模式&#xff1f; 1. 是一类程序设计思想 2. 是在大量实践过程中摸索总结出的标准经验提炼 3. 具有多样性和丰富性&#xff0c;不同情况应用的思想不同 二、设计模式的好处 1. 代码生产力和效率的提升 2. 让代码表现更为规整&#xff0c;简洁。阅读维护管理的成本…

【竞技宝】DOTA2-梦幻联赛S22:AR命悬一线 XG确定晋级淘汰赛

北京时间2024年2月28日&#xff0c;DOTA2梦幻联赛S22的比赛在昨日进入小组赛第三个比赛日&#xff0c;本次梦幻联赛共有AR、XG、IG三支中国区的队伍参赛&#xff0c;那么经过三日激烈的比赛之后&#xff0c;目前三支队伍的积分情况以及晋级形势如何呢&#xff1f; XG XG是小组…

贪心(基础算法)--- 区间选点

905. 区间选点 思路 &#xff08;贪心&#xff09;O(nlogn) 根据右端点排序 将区间按右端点排序 遍历区间&#xff0c;如果当前区间左端点不包含在前一个区间中&#xff0c;则选取新区间&#xff0c;所选点个数加1&#xff0c;更新当前区间右端点。如果包含&#xff0c;则跳…

常见的算法

查找算法 基本查找 package MyApi.search;public class a01BasicSearchdemo01 {public static void main(String[] args) { int[] arr{131,127,147,81,103,23,7,79}; int number82;System.out.println(BasicSearch(arr,number));}public static boolean BasicSearch(int[] ar…

Java二叉树(1)

&#x1f435;本篇文章将对二叉树的相关概念、性质和遍历等知识进行讲解 一、什么是树 在讲二叉树之前&#xff0c;先了解一下什么是树&#xff1a;树是一种非线性结构&#xff0c;其由许多节点和子节点组成&#xff0c;整体形状如一颗倒挂的树&#xff0c;比如下图&#xff1…

给nginx部署https及自签名ssl证书

一、生成服务器root证书 openssl genrsa -out root.key 2048 openssl req -new -key root.key -out root.csr#Country Name (2 letter code) [XX]:---> CN#Country Name (2 letter code) [XX]:---> CN#State or Province Name (full name) []:---> Shanghai#Locality…

多层感知机 + 代码实现 - 动手学深度学习v2 | 李沐动手学深度学习课程笔记

感知机 感知机≈二分类问题 感知机和其他问题的对比 训练感知机 如果小于等于零&#xff0c;说明预测错啦 &#xff0c;其实就是同号为正&#xff0c;异号为负 举个分类的例子 增加样本&#xff0c;改变分类线 继续分类 感知机的收敛定理 XOR问题 XOR问题其实就是第1、3象限数…

【踩坑】一条指令解决torch_scatter等安装报错安装不上问题

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhang.cn] 目录 背景说明 (推荐方法)解决方法一&#xff1a;使用conda安装。 解决方法二&#xff1a;指定pip的网站。 解决方法三&#xff1a;直接去下载whl文件。 (终极方法)解决方法四&#xff1a;配置MSVC 特殊情况…

构建 ESLint 内存泄露检测插件入门:提升代码质量与防范运行时风险

前言 本文目的是介绍如何创建开发一个自定义规则 ESLint 插件。利用其能力,检测一些代码中可能存在的内存泄露并及时进行提示,避免潜在的后期影响。 本文实现其中一部分功能–检测事件监听器的使用是否存在内存泄露为例来演示基本的 ESLint 自定义规则插件开发的过程。用以…

nginx笔记整理

目录 一.Nginx基础介绍 二.nginx安装配置 三.Nginx配置文件 3.1nginx主配置文件(/etc/nginx/nginx.conf) 3.2默认的网站配置文件(/etc/nginx/conf.d/default.conf) 四.创建新的虚拟主机 五.Nginx日志 5.1nginx日志格式 5.2查看日志 5.3日志缓存(了解) 5.4日志轮转(/…

COMPOSER安装使用WIN下升级PHP-V

想用TP6使用phpspreadsheet但是说我PHP版本低&#xff0c;原来是PHP7.0 composer要求至少7.4 直接修改环境变量&#xff0c;把PHP目录切换到7.4 composer升级比较简单&#xff0c;在PHP目录下CMD然后官网的命令执行下即可 下面就可以在TP根目录下执行命令安装PHPSPREADSHEET…

Domain-Wall Memory Buffer for Low-Energy NoCs

目录 Domain-Wall Memory Buffer for Low-Energy NoCs主要工作DWM&#xff1a; Domain-wall memory磁畴壁存储器磁性纳米线阵列设计 开销分析实验设计实验结果分析 参考资料 Domain-Wall Memory Buffer for Low-Energy NoCs 主要工作 我们基于SRAM在NoC中使用的头尾指针概念&a…