栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【PyTorch

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【PyTorch

视频教程 所用到的数据集在视频的简介里:数据集-hymenoptera_data-train
或者在官网下载数据集

用Dataset加载图片数据集

用到的一些python的基础知识

conda install opencv-python
pip install opencv-python
img = Image.open(img_path)   # 打开图片
img.show()  # 展示图片
root_dir = "dataset/train"
label_dir = "ants"
# 连接两个字符串
path = os.path.join(root_dir, label_dir)
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)

if __name__=='__main__':
    root_dir = "dataset/train"
    ants_label_dir = "ants"
    bees_label_dir = "bees"
    ants_dataset = MyData(root_dir=root_dir, label_dir=ants_label_dir)
    bees_dataset = MyData(root_dir, bees_label_dir)
    print(len(ants_dataset))
    train_dataset = ants_dataset + bees_dataset
    print(len(train_dataset))
import os

root_dir = "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)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324195.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号