pytorch入门

详细安装教程和环境配置可以看:Python深度学习:安装Anaconda、PyTorch(GPU版)库与PyCharm_哔哩哔哩_bilibili

跟学课程:B站我是土堆

pytorch中两个实用函数: dir():打开          help():说明书

PyCharm及jupyter的对比

python文件:代码是以块为一个整体运行的(python文件的块是所有行的代码),如果中间有错误会重头运行

python控制台:代码是任意行为块运行(可以自己划分块),有错误会从错误的地方开始运行。

jupyter:根据自己划分的代码块执行,如果有错误,会从错误的代码块开始运行。

 

 pytorch如何加载数据

Dataset :提供一种方式获取数据及其label

如何获取每一个数据及其lable

告诉我们一共有多少数据

Dataloader :为后面的网络提供不同的数据形式 

dataset类代码实战

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 = r"dataset\train"
ants_label_dir = "ants_image"
bees_label_dir = "bees_image"
bees_dataset = MyData(root_dir, ants_label_dir)
ants_dataset = MyData(root_dir, ants_label_dir)

train_dataset = ants_dataset + bees_dataset
from torch.utils.data import Dataset
from PIL import Image
import os

root_dir = r"dataset\train"
target_dir = "ants_image"
img_path = os.listdir(os.path.join(root_dir, target_dir))
label = target_dir.split('-')[0]
out_dir = "ants_label"
for i in img_path:
    file_name = i.split('.jpg')[0]
    with open(os.path.join(root_dir, out_dir, "{}.txt".format(file_name)),"w") as f:
        f.write(label)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值