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

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

QT Mingw编译ffmpeg源码以及测试

文章目录 前言下载msys2ysamFFmpeg 搭建编译环境安装msys2安装QT Mingw编译器到msys环境中安装ysam测试 编译FFmpeg 前言 FFmpeg不像VLC有支持QT的库文件&#xff0c;它仅提供源码&#xff0c;需要使用者自行编译成对应的库&#xff0c;当使用QTFFmpeg实现播放视频以及视频流时…

Linux下快速创建大文件的4种方法总结

1、使用 dd 命令创建大文件 dd 命令用于复制和转换文件&#xff0c;它最常见的用途是创建实时 Linux USB。dd 命令是实际写入硬盘&#xff0c;文件产生的速度取决于硬盘的读写速度&#xff0c;根据文件的大小&#xff0c;该命令将需要一些时间才能完成。 假设我们要创建一个名…

Vuepress的使用

介绍 将markdown静态资源转换成html。 动态资源的转换还有很多&#xff0c;为什么要使用Vuepress&#xff1f; 目录分析 项目配置 详情 具体配置请看文档 插件配置 vuepress-theme-vdoing 主题插件 npm install vuepress-theme-vdoing -D先安装依赖配置主题 使用vuep…

外包干了6个月,技术退步明显。。。。。

先说一下自己的情况&#xff0c;本科生&#xff0c;2019年我通过校招踏入了重庆一家软件公司&#xff0c;开始了我的职业生涯。那时的我&#xff0c;满怀热血和憧憬&#xff0c;期待着在这个行业中闯出一片天地。然而&#xff0c;随着时间的推移&#xff0c;我发现自己逐渐陷入…

GEE入门篇|遥感专业术语(实践操作4):光谱分辨率(Spectral Resolution)

目录 光谱分辨率&#xff08;Spectral Resolution&#xff09; 1.MODIS 2.EO-1 光谱分辨率&#xff08;Spectral Resolution&#xff09; 光谱分辨率是指传感器进行测量的光谱带的数量和宽度。 您可以将光谱带的宽度视为每个波段的波长间隔&#xff0c;在多个波段测量辐射亮…

android开发与实战,那些年Android面试官常问的知识点

前言 在做android项目开发时&#xff0c;大家都知道如果程序出错了&#xff0c;会弹出来一个强制退出的弹 出框&#xff0c;这个本身没什么问题&#xff0c;但是这个UI实在是太丑了&#xff0c;别说用户接受不了&#xff0c;就连 我们自己本身可能都接受不了。虽然我们在发布程…

Vue:【亲测可用】父组件数组包对象,传给子组件对象,子组件修改属性(字段)后,父组件没有更新

场景&#xff1a;vue中父组件数组包对象&#xff0c;传给子组件对象&#xff0c;子组件修改属性&#xff08;字段&#xff09;后&#xff0c;父组件没有更新 代码&#xff1a; # 父组件 <div v-for"(object, name, index) in arr" :key"index"><…

【MySQL】数据管理——DML操作数据

目录 DML&#xff08;数据操作语言&#xff09;添加数据插入单行语法插入多行语法SQL示例将查询结果插入到新表中语法1&#xff1a;语法2&#xff1a; 修改数据语法示例关于SQL的运算符算术运算符比较运算符逻辑运算符 案例 删除数据DELETE命令语法 TRUNCATE TABLE 命令语法代码…

宝塔FTP服务设置并结合cpolar内网穿透实现远程传输文件

文章目录 1. Linux安装Cpolar2. 创建FTP公网地址3. 宝塔FTP服务设置4. FTP服务远程连接小结 5. 固定FTP公网地址6. 固定FTP地址连接 宝塔FTP是宝塔面板中的一项功能&#xff0c;用于设置和管理FTP服务。通过宝塔FTP&#xff0c;用户可以创建FTP账号&#xff0c;配置FTP用户权限…

数据结构——lesson4带头双向循环链表实现

前言✨✨ &#x1f4a5;个人主页&#xff1a;大耳朵土土垚-CSDN博客 &#x1f4a5; 所属专栏&#xff1a;数据结构学习笔记​​​​​​ &#x1f4a5;双链表与单链表的区分&#xff1a;单链表介绍与实现 &#x1f4a5;对于malloc函数有疑问的:动态内存函数介绍 感谢大家的观看…

为什么推荐使用ref而不是reactive

为什么推荐使用ref而不是reactive 局限性问题&#xff1a; reactive本身存在一些局限性&#xff0c;可能会在开发过程中引发一些问题。这需要额外的注意力和处理&#xff0c;否则可能对开发造成麻烦。数据类型限制&#xff1a; reactive声明的数据类型仅限于对象&#xff0c;而…

RK3568 android11 调试陀螺仪模块 MPU6500

一&#xff0c;MPU6500功能介绍 1.简介 MPU6500是一款由TDK生产的运动/惯性传感器&#xff0c;属于惯性测量设备&#xff08;IMU&#xff09;的一种。MPU6500集成了3轴加速度计、3轴陀螺仪和一个板载数字运动处理器&#xff08;DMP&#xff09;&#xff0c;能够提供6轴的运动…

【毛毛讲书】【端粒:年轻、健康、长寿的新科学】是什么决定了我们的寿命?

重磅推荐专栏&#xff1a; 《大模型AIGC》 《课程大纲》 《知识星球》 本专栏致力于探索和讨论当今最前沿的技术趋势和应用领域&#xff0c;包括但不限于ChatGPT和Stable Diffusion等。我们将深入研究大型模型的开发和应用&#xff0c;以及与之相关的人工智能生成内容&#xff…

es获取某个索引下字段的分词结果

//查看某个索引下字段的分词结果 GET /haha/_analyze { "field": "title", "text":"哈哈。" }

云尚办公-0.1.0

二、用户管理接口 1. 建表 角色与用户是多对多的关系&#xff0c;所以除了角色表和用户表外&#xff0c;还需要第三张表表示这两者间的对应关系。关系表中的用户id和角色id分别以对应表中的id作为外键。 CREATE TABLE sys_user (id BIGINT(20) NOT NULL AUTO_INCREMENT COM…

Vue3切换路由白屏刷新后才显示页面内容

1.首先检查页面路由以及页面路径配置是否配置错误。 在router-view 中给路由添加key标识。 &#xff01;&#xff01;注意&#xff1a;有使用layout封装布局的&#xff0c;是在layout下的主页面中的 router-view 添加标识&#xff0c;不是在src根目录下main.vue中修改&#xf…

[云原生] K8s之pod进阶

一、pod的状态说明 &#xff08;1&#xff09;Pod 一直处于Pending状态 Pending状态意味着Pod的YAML文件已经提交给Kubernetes&#xff0c;API对象已经被创建并保存在Etcd当中。但是&#xff0c;这个Pod里有些容器因为某种原因而不能被顺利创建。比如&#xff0c;调度不成功(…

【rust】11、所有权

文章目录 一、背景二、Stack 和 Heap2.1 Stack2.2 Heap2.3 性能区别2.4 所有权和堆栈 三、所有权原则3.1 变量作用域3.2 String 类型示例 四、变量绑定背后的数据交互4.1 所有权转移4.1.1 基本类型: 拷贝, 不转移所有权4.1.2 分配在 Heap 的类型: 转移所有权 4.2 Clone(深拷贝)…

linux系统Jenkins的安装

Jenkins安装 安装上传安装包解压包首次登录要去服务器查看密码&#xff0c;更改密码选择需要安装的插件设置Admin用户和密码安装完成 安装 上传安装包 上传 jdk17 tomcat jenkins.war的安装包 . 上传 tomcat安装包解压包 解压jdk tar xf jdk-11.0.18_linux-x64_bin.tar.gz解…

甲级!亚信安全再次荣膺CNCERT国家级网络安全应急服务支撑单位称号

再获认可 近日&#xff0c;国家计算机网络应急技术处理协调中心&#xff08;CNCERT&#xff09;正式公布第十届CNCERT网络安全应急服务支撑单位遴选结果&#xff0c;亚信安全凭借创新技术实力&#xff0c;以及重大突发网络安全事件的应对能力&#xff0c;再次荣获CNCERT甲级网…