自学内容网 自学内容网

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 = path
        self.list = os.listdir(path)
        self.len = len(self.list)
        self.name = ['cloudy','rain','shine','sunrise']
        self.trans = transforms.ToTensor()
    def __len__(self):
        return self.len
    def __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+1
                break
        return img,label

ds = 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 transforms


trans = 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,图片放大缩小功能。


原文地址:https://blog.csdn.net/klp1358484518/article/details/136333748

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!