cc.日常

这段代码展示了如何利用PyTorch的`torch.utils.data.Dataset`创建自定义数据集,用于加载和处理`dataset/train`目录下`ants`和`bees`两个子目录的图像数据。数据集被组合成`train_dataset`,其长度为245,可以用于训练分类模型。示例中展示了如何获取第124个样本并显示图像。
摘要由CSDN通过智能技术生成

可以说土堆真的保姆级

学习读取数据集中的各类数据

from torch.utils.data import Dataset
from PIL import Image
import os

class MyData(Dataset):

    def __init__(self,root_dir,label_dir):
        self.root_dir = root_dir
        self.label_dir = label_dir
        self.path = os.path.join(self.root_dir,self.label_dir)
        self.img_path = os.listdir(self.path)


    def __getitem__(self, idx):
        img_name = self.img_path[idx]
        img_item_path=os.path.join(self.root_dir,self.label_dir,img_name)
        img = Image.open(img_item_path)
        label = self.label_dir
        return img,label

    def __len__(self):
        return len(self.img_path)


root_dir = "dataset/train"
ants_label_dir = "ants"
bees_label_dir = "bees"
ants_dataset = MyData(root_dir,ants_label_dir)
bees_dataset = MyData(root_dir,bees_label_dir)

train_dataset = ants_dataset + bees_dataset

python console

C:\ProgramData\Anaconda3\envs\pytorch\python.exe "C:\D\ccpython\PyCharm Community Edition 2021.3.1\plugins\python-ce\helpers\pydev\pydevconsole.py" --mode=client --port=53938
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['C:\\Users\\26940\\Desktop\\learn_pytorchc', 'C:/Users/26940/Desktop/learn_pytorchc'])
Python 3.6.13 |Anaconda, Inc.| (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 6.1.0
Python 3.6.13 |Anaconda, Inc.| (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)] on win32
from torch.utils.data import Dataset
from PIL import Image
import os
class MyData(Dataset):
    def __init__(self,root_dir,label_dir):
        self.root_dir = root_dir
        self.label_dir = label_dir
        self.path = os.path.join(self.root_dir,self.label_dir)
        self.img_path = os.listdir(self.path)
    def __getitem__(self, idx):
        img_name = self.img_path[idx]
        img_item_path=os.path.join(self.root_dir,self.label_dir,img_name)
        img = Image.open(img_item_path)
        label = self.label_dir
        return img,label
    def __len__(self):
        return len(self.img_path)
    
root_dir = "dataset/train"
ants_label_dir = "ants"
ants_dataset = MyData(root_dir,ants_label_dir)
root_dir = "dataset/train"
ants_label_dir = "ants"
bees_label_dir = "bees"
ants_dataset = MyData(root_dir,ants_label_dir)
bees_dataset = MyData(root_dir,bees_label_dir)
train_dataset = ants_dataset + bees_dataset
len(train_dataset)
Out[7]: 245
img,label=train_dataset[124]
img.show()

蜜蜂蚂蚁数据集:

https://download.pytorch.org/tutorial/hymenoptera_data.ziphttps://download.pytorch.org/tutorial/hymenoptera_data.zip


conception 观念

irritate 激怒

lapse 失误

accommodate 容纳

fiber 纤维素

maneuver 演习

intelligible 可理解的

ashore 上岸

amplify 放大

discreet 谨慎的

council 委员会

lateral 侧面的

illustration 例证

breeze 微风

strain 拉紧

marital 夫妻的

indicative 表示的

therapy 治疗

transplant 迁移

moral 道德的

nasty 烦人的

ingenious 精致的

arena 竞技场

explicit 明确的

assimilate 同化

presumably 大概

liberty 自由

stalk 跟踪

stimulate 刺激

ancestor 祖宗

sway 摇摆

evaporate 蒸发

dread 恐惧

terminate 结束


wy补录,投wb,bz

明天一面md

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值