当前位置:网站首页>pytorch创建自己的Dataset加载数据集
pytorch创建自己的Dataset加载数据集
2022-07-25 12:43:00 【1 + 1=王】
创建一个类并继承torch.utils.data.dataset.Datase类
class MyDataset(Dataset):
''' data_path: 数据集路径 img_size: 图片大小 train_lines: 图片名数组 '''
def __init__(self,data_path,img_size,train_lines):
super(MyDataset, self).__init__()
self.data_path = data_path
self.img_size = img_size
self.train_lines = train_lines
self.length = len(train_lines)
创建__getitem__方法
class MyDataset(Dataset):
''' data_path: 数据集路径 img_size: 图片大小 train_lines: 图片名数组 '''
def __init__(self,data_path,img_size,train_lines):
super(MyDataset, self).__init__()
self.data_path = data_path
self.img_size = img_size
self.train_lines = train_lines
def __getitem__(self, index):
annotation_line = self.train_lines[index]
name = annotation_line.split()[0] # 获取图片名
image = Image.open(os.path.join(os.path.join(self.data_path,"dem"),name+".tif"))
label = Image.open(os.path.join(os.path.join(self.data_path, "label"), name + ".png"))
image = np.array(image)
label = np.array(label)
image = cv2.resize(image,(self.img_size,self.img_size))
label = cv2.resize(label,(self.img_size,self.img_size))
# image = image[np.newaxis,:]
print("images size: {}, label size: {}".format(image.shape,label.shape))
return image,label
加载数据集
如果不知道如何将文件夹中所有图片名称写入TXT中可以参考:python读取文件夹中的所有图片并将图片名逐行写入txt中:https://blog.csdn.net/weixin_43598687/article/details/125666776?spm=1001.2014.3001.5501
dataset_path = r"E:/workspace/PyCharmProject/dem_feature/dem/512"
# 打开数据集的txt, 逐行读取图片名
with open(os.path.join(dataset_path, "dem/train.txt"), "r") as f:
train_lines = f.readlines()
with open(os.path.join(dataset_path, "dem/val.txt"), "r") as f:
val_lines = f.readlines()
train_dataset = MyDataset(dataset_path, img_size=512,train_lines=train_lines)
train_dataloader = DataLoader(train_dataset,batch_size=8,shuffle=False)
for iteration,data in enumerate(train_dataloader):
imgs,labels = data
print(imgs,labels)
边栏推荐
- AtCoder Beginner Contest 261E // 按位思考 + dp
- go : gin 自定义日志输出格式
- JS 将伪数组转换成数组
- ECCV2022 | TransGrasp类级别抓取姿态迁移
- Is the securities account opened by qiniu safe? How to open an account
- "Autobiography of Franklin" cultivation
- Docker学习 - Redis集群-3主3从-扩容-缩容搭建
- LeetCode 0133. 克隆图
- SSTI 模板注入漏洞总结之[BJDCTF2020]Cookie is so stable
- [advanced C language] dynamic memory management
猜你喜欢
What does the software testing process include? What are the test methods?

艰辛的旅程

If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing

Use of hystrix
![[shutter -- layout] stacked layout (stack and positioned)](/img/01/c588f75313580063cf32cc01677600.jpg)
[shutter -- layout] stacked layout (stack and positioned)

Substance Designer 2021软件安装包下载及安装教程

word样式和多级列表设置技巧(二)

【运维、实施精品】月薪10k+的技术岗位面试技巧
![SSTI 模板注入漏洞总结之[BJDCTF2020]Cookie is so stable](/img/19/0b943019fe1c959c4b79035a814410.png)
SSTI 模板注入漏洞总结之[BJDCTF2020]Cookie is so stable

零基础学习CANoe Panel(12)—— 进度条(Progress Bar)
随机推荐
Moving Chinese figure liushenglan
ECCV2022 | TransGrasp类级别抓取姿态迁移
Does MySQL have flush privileges
零基础学习CANoe Panel(13)—— 滑条(TrackBar )
Migrate PaloAlto ha high availability firewall to panorama
[problem solving] ibatis.binding BindingException: Type interface xxDao is not known to the MapperRegistry.
How to use causal inference and experiments to drive user growth| July 28 tf67
【AI4Code】《Contrastive Code Representation Learning》 (EMNLP 2021)
[fluent -- example] case 1: comprehensive example of basic components and layout components
Intval MD5 bypass [wustctf2020] plain
How to understand metrics in keras
我想问DMS有没有定时备份某一个数据库的功能?
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
LeetCode 1184. 公交站间的距离
Mysql 远程连接权限错误1045问题
如何理解Keras中的指标Metrics
零基础学习CANoe Panel(14)——二极管( LED Control )和液晶屏(LCD Control)
AtCoder Beginner Contest 261E // 按位思考 + dp
业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
Lu MENGZHENG's "Fu of broken kiln"