pytorch【生成自己的数据集】

一、导入相关的库

import skimage
from skimage import io #用来导入图片
import numpy as np
import pandas as pd
from skimage import io
import torch, torchvision
from torchvision import transforms
import matplotlib.pyplot as plt
import random
import os
from torch.utils.data import Dataset

二、生成数据集

这里我们的数据是分别存储在一个文件夹中,这个文件夹下有子文件夹,
每一个子文件夹就是一个子类,然后每一个子文件夹都有若干张图片

然后我们的每一张图片的名称和标签我们都是存在一个txt文件当中的
#读取单一的一张图片
io.imread("/Users/Documents/000001.jpg")

这是我们查看到的结果

array([[[253, 231, 192],
        [253, 231, 192],
        [253, 231, 192],
        ...,
        [254, 244, 219],
        [254, 244, 219],
        [254, 244, 219]],

       [[253, 231, 192],
        [253, 231, 192],
        [253, 231, 192],
        ...,
        [254, 244, 219],
        [254, 244, 219],
        [254, 244, 219]],

       [[253, 231, 192],
        [253, 231, 192],
        [253, 231, 192],
        ...,
        [254, 244, 219],
        [254, 244, 219],
        [254, 244, 219]],

       ...,

       [[253, 218, 176],
        [253, 218, 176],
        [253, 218, 176],
        ...,
        [244, 173, 121],
        [236, 170, 120],
        [239, 179, 129]],

       [[253, 218, 176],
        [253, 218, 176],
        [253, 218, 176],
        ...,
        [251, 180, 126],
        [239, 173, 121],
        [238, 178, 128]],

       [[253, 218, 176],
        [253, 218, 176],
        [253, 218, 176],
        ...,
        [255, 185, 126],
        [242, 177, 123],
        [240, 178, 127]]], dtype=uint8)
#读取我们一张图片的标签
identity=pd.read_csv("/Users/Documents/celebAsubset/Anno/identity_CelebA_1000.txt",header=None,sep=" ")
identity.head()
01
0000001.jpg2880
1000002.jpg2937
2000003.jpg8692
3000004.jpg5805
4000005.jpg9295
#保存我们的图片的路径和我们的
imgpath="Users/Documents"
csvpath="/Users/Documents/celebAsubset/Anno/identity_CelebA_1000.txt"
#这样我们就能够得到我们拼接之后的图片名称
imgdic=os.path.join(imgpath,identity.iloc[0,0])
#查看单个图像的标签(第0行第1列)
identity.iloc[0,1]
2880
idx=0
#保存我们的图片的路径和我们的
imgpath="Users/Documents"
csvpath="/Users/Documents/celebAsubset/Anno/identity_CelebA_1000.txt"

imgdic=os.path.join(imgpath,identity.iloc[idx,0])#图像目录
image=io.imread(imgdic)#提取出的,索引为idx的图像的像素值矩阵
label=identity.iloc[idx,1]#提取出对应的图像的标签
sample=(torch.tensor(image),int(label))#将我们图像的图像矩阵和标签进行封装
sample
(tensor([[[253, 231, 192],
          [253, 231, 192],
          [253, 231, 192],
          ...,
          [254, 244, 219],
          [254, 244, 219],
          [254, 244, 219]],
 
         [[253, 231, 192],
          [253, 231, 192],
          [253, 231, 192],
          ...,
          [254, 244, 219],
          [254, 244, 219],
          [254, 244, 219]],
 
         [[253, 231, 192],
          [253, 231, 192],
          [253, 231, 192],
          ...,
          [254, 244, 219],
          [254, 244, 219],
          [254, 244, 219]],
 
         ...,
 
         [[253, 218, 176],
          [253, 218, 176],
          [253, 218, 176],
          ...,
          [244, 173, 121],
          [236, 170, 120],
          [239, 179, 129]],
 
         [[253, 218, 176],
          [253, 218, 176],
          [253, 218, 176],
          ...,
          [251, 180, 126],
          [239, 173, 121],
          [238, 178, 128]],
 
         [[253, 218, 176],
          [253, 218, 176],
          [253, 218, 176],
          ...,
          [255, 185, 126],
          [242, 177, 123],
          [240, 178, 127]]], dtype=torch.uint8),
 2880)

定义一个创建数据集的函数

#创建我们自己的数据集,我们需要创建一个继承Dataset的类
class CustomDataset(Dataset):
    #transform是为了判断是否需要让我们的数据发生更改
    def __init__(self,csv_file,root_dir,transform=None):
        super().__init__()
        self.identity=pd.read_csv(csv_file,sep=" ",header=None)
        self.root_dir=root_dir
        self.transform=transform

    def __len__(self):
        #展示数据集中总共有多少个样本
        return len(self.identity)

    def __info__(self):
        #打印数据集的名称
        print("CustomData")
        print(self.root_dir)

    #建立索引值
    def __getitem__(self, idx):
        #保证idx不是一个tensor
        if torch.is_tensor(idx):
            idx=idx.tolist()

        imgdic=os.path.join(self.root_dir,self.identity.iloc[idx,0])#图像目录
        image=io.imread(imgdic)#提取出的,索引为idx的图像的像素值矩阵
        label=self.identity.iloc[idx,1]#提取出对应的图像的标签

        if self.transform !=None:
            image=self.transform(image)

        sample=(image,label)#将我们图像的图像矩阵和标签进行封装
        return sample
data=CustomDataset(csvpath,imgpath)
#查看第0号图片
data[0]
(array([[[253, 231, 192],
         [253, 231, 192],
         [253, 231, 192],
         ...,
         [254, 244, 219],
         [254, 244, 219],
         [254, 244, 219]],
 
        [[253, 231, 192],
         [253, 231, 192],
         [253, 231, 192],
         ...,
         [254, 244, 219],
         [254, 244, 219],
         [254, 244, 219]],
 
        [[253, 231, 192],
         [253, 231, 192],
         [253, 231, 192],
         ...,
         [254, 244, 219],
         [254, 244, 219],
         [254, 244, 219]],
 
        ...,
 
        [[253, 218, 176],
         [253, 218, 176],
         [253, 218, 176],
         ...,
         [244, 173, 121],
         [236, 170, 120],
         [239, 179, 129]],
 
        [[253, 218, 176],
         [253, 218, 176],
         [253, 218, 176],
         ...,
         [251, 180, 126],
         [239, 173, 121],
         [238, 178, 128]],
 
        [[253, 218, 176],
         [253, 218, 176],
         [253, 218, 176],
         ...,
         [255, 185, 126],
         [242, 177, 123],
         [240, 178, 127]]], dtype=uint8),
 2880)
#产看一共有多少个数据
data.__len__()
1000
#查看我们数据集的相关信息
data.__info__()
CustomData
/Users/Documents
#查看个体
for x,y in data:
    print(x.shape)
    print(y)
    break
(687, 409, 3)
2880

三、创建我们分类数据集

这里我们的图像还是刚刚的图像
但是我们的txt中存储的是每一张图片对应每一个类别的独热编码
#读取属性识别的标签
imgpath="/Users/Documents"
csvpath="/Users/Documents/celebAsubset/Anno/list_attr_celeba_1000.txt"
attr_=pd.read_csv(csvpath,header=None)
attr_
0
0202599 5_o_Clock_Shadow Arched_Eyebrows Attrac...
1000001.jpg -1 1 1 -1 -1 -1 -1 -1 -1 -1 -1 1...
2000002.jpg -1 -1 -1 1 -1 -1 -1 1 -1 -1 -1 1...
3000003.jpg -1 -1 -1 -1 -1 -1 1 -1 -1 -1 1 -1...
4000004.jpg -1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1...
......
996000996.jpg -1 1 -1 -1 -1 1 -1 -1 -1 1 -1 -1...
997000997.jpg -1 -1 1 -1 -1 1 -1 -1 -1 1 -1 -1...
998000998.jpg -1 1 1 -1 -1 -1 -1 -1 -1 1 -1 -1...
999000999.jpg -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1...
1000001000.jpg -1 -1 1 -1 -1 -1 1 -1 -1 -1 -1 -1...

1001 rows × 1 columns

#帮助我们按照空格进行分类,它会把多个空格也当成一个空格
#因为我们这里相邻的两个数据之间的空格数是不确定有1个还是2个的
len(attr_.iloc[0,0].split())
41
attr_.iloc[0,0].split()
['202599',
 '5_o_Clock_Shadow',
 'Arched_Eyebrows',
 'Attractive',
 'Bags_Under_Eyes',
 'Bald',
 'Bangs',
 'Big_Lips',
 'Big_Nose',
 'Black_Hair',
 'Blond_Hair',
 'Blurry',
 'Brown_Hair',
 'Bushy_Eyebrows',
 'Chubby',
 'Double_Chin',
 'Eyeglasses',
 'Goatee',
 'Gray_Hair',
 'Heavy_Makeup',
 'High_Cheekbones',
 'Male',
 'Mouth_Slightly_Open',
 'Mustache',
 'Narrow_Eyes',
 'No_Beard',
 'Oval_Face',
 'Pale_Skin',
 'Pointy_Nose',
 'Receding_Hairline',
 'Rosy_Cheeks',
 'Sideburns',
 'Smiling',
 'Straight_Hair',
 'Wavy_Hair',
 'Wearing_Earrings',
 'Wearing_Hat',
 'Wearing_Lipstick',
 'Wearing_Necklace',
 'Wearing_Necktie',
 'Young']
#对其他的所有的行都进行分裂
attr_.iloc[1:,0].head()
1    000002.jpg
2    000003.jpg
3    000004.jpg
4    000005.jpg
5    000006.jpg
Name: 202599, dtype: object
#先将除了我们上面分类名的那一行的数据全部都去除,然后将每一行都转换成字符串
#然后再进行分隔
attr_.iloc[1:,0].str.split()
1       [000001.jpg, -1, 1, 1, -1, -1, -1, -1, -1, -1,...
2       [000002.jpg, -1, -1, -1, 1, -1, -1, -1, 1, -1,...
3       [000003.jpg, -1, -1, -1, -1, -1, -1, 1, -1, -1...
4       [000004.jpg, -1, -1, 1, -1, -1, -1, -1, -1, -1...
5       [000005.jpg, -1, 1, 1, -1, -1, -1, 1, -1, -1, ...
                              ...                        
996     [000996.jpg, -1, 1, -1, -1, -1, 1, -1, -1, -1,...
997     [000997.jpg, -1, -1, 1, -1, -1, 1, -1, -1, -1,...
998     [000998.jpg, -1, 1, 1, -1, -1, -1, -1, -1, -1,...
999     [000999.jpg, -1, -1, -1, -1, -1, -1, -1, -1, -...
1000    [001000.jpg, -1, -1, 1, -1, -1, -1, 1, -1, -1,...
Name: 0, Length: 1000, dtype: object
#再转换成一个列表
attr_.iloc[1:,0].str.split().tolist()
#再将这个列表转换成我们的dataframe
attr_=pd.DataFrame(attr_.iloc[1:,0].str.split().tolist(),
                   columns=attr_.iloc[0,0].split())
attr_.head()
2025995_o_Clock_ShadowArched_EyebrowsAttractiveBags_Under_EyesBaldBangsBig_LipsBig_NoseBlack_Hair...SideburnsSmilingStraight_HairWavy_HairWearing_EarringsWearing_HatWearing_LipstickWearing_NecklaceWearing_NecktieYoung
0000001.jpg-111-1-1-1-1-1-1...-111-11-11-1-11
1000002.jpg-1-1-11-1-1-11-1...-11-1-1-1-1-1-1-11
2000003.jpg-1-1-1-1-1-11-1-1...-1-1-11-1-1-1-1-11
3000004.jpg-1-11-1-1-1-1-1-1...-1-11-11-111-11
4000005.jpg-111-1-1-11-1-1...-1-1-1-1-1-11-1-11

5 rows × 41 columns

attr_.loc[:,"Attractive"].head()
0     1
1    -1
2    -1
3     1
4     1
Name: Attractive, dtype: object

定义一个创建数据集的函数

#创建我们自己的数据集,我们需要创建一个继承Dataset的类
#这个类我们用于属性识别
class CustomDataset_attr(Dataset):
    #transform是为了判断是否需要让我们的数据发生更改
    def __init__(self,csv_file,root_dir,labelname,transform=None):
        super().__init__()
        self.attr_=pd.read_csv(csv_file,header=None)
        self.root_dir=root_dir
        self.labelname=labelname
        self.transform=transform

    def __len__(self):
        #展示数据集中总共有多少个样本
        return len(self.attr_)-1

    def __info__(self):
        #打印数据集的名称
        print("CustomData")
        print(self.root_dir)

    #建立索引值
    def __getitem__(self, idx):
        #保证idx不是一个tensor
        if torch.is_tensor(idx):
            idx=idx.tolist()

        self.attr_=pd.DataFrame(self.attr_.iloc[1:,0].str.split().tolist(),
                   columns=self.attr_.iloc[0,0].split())

        imgdic=os.path.join(self.root_dir,self.attr_.iloc[idx,0])#图像目录
        image=io.imread(imgdic)#提取出的,索引为idx的图像的像素值矩阵
        label=int(self.attr_.loc[idx,self.labelname])#提取出对应的图像的标签

        if self.transform !=None:
            image=self.transform(image)

        sample=(image,label)#将我们图像的图像矩阵和标签进行封装
        return sample
#读取属性识别的标签
imgpath="/Users/Documents"
csvpath="/Users/Documents/celebAsubset/Anno/list_attr_celeba_1000.txt"
#选择我们想要提取的那一列的特征
labelname="Attractive"
data=CustomDataset_attr(csvpath,imgpath,labelname)
data.__len__()
1000
data[500]
(array([[[ 56,  44,  32],
         [ 56,  44,  32],
         [ 56,  44,  32],
         ...,
         [ 46,  38,  19],
         [ 46,  38,  19],
         [ 46,  38,  19]],
 
        [[ 56,  44,  32],
         [ 56,  44,  32],
         [ 56,  44,  32],
         ...,
         [ 46,  38,  19],
         [ 46,  38,  19],
         [ 46,  38,  19]],
 
        [[ 56,  44,  32],
         [ 56,  44,  32],
         [ 56,  44,  32],
         ...,
         [ 46,  38,  19],
         [ 46,  38,  19],
         [ 46,  38,  19]],
 
        ...,
 
        [[ 54,  52,  37],
         [ 53,  51,  36],
         [ 53,  51,  36],
         ...,
         [196, 210, 211],
         [197, 211, 212],
         [198, 212, 213]],
 
        [[ 56,  54,  39],
         [ 55,  53,  38],
         [ 55,  53,  38],
         ...,
         [197, 211, 212],
         [198, 212, 213],
         [199, 213, 214]],
 
        [[ 57,  55,  40],
         [ 57,  55,  40],
         [ 56,  54,  39],
         ...,
         [199, 213, 214],
         [200, 214, 215],
         [201, 215, 216]]], dtype=uint8),
 1)
我们就成功生成了我们的数据集
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桜キャンドル淵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值