当前位置:网站首页>Pytorch creates its own dataset and loads the dataset
Pytorch creates its own dataset and loads the dataset
2022-07-25 13:08:00 【1 + 1= Wang】
List of articles
Create a class and inherit torch.utils.data.dataset.Datase class
class MyDataset(Dataset):
''' data_path: Dataset path img_size: Picture size train_lines: Image name array '''
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)
establish __getitem__ Method
class MyDataset(Dataset):
''' data_path: Dataset path img_size: Picture size train_lines: Image name array '''
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] # Get the picture name
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
Load data set
If you don't know how to write the names of all pictures in the folder TXT We can refer to :python Read all the pictures in the folder and write the picture names line by line txt in :https://blog.csdn.net/weixin_43598687/article/details/125666776?spm=1001.2014.3001.5501
dataset_path = r"E:/workspace/PyCharmProject/dem_feature/dem/512"
# Open the of the dataset txt, Read the picture name line by line
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)
边栏推荐
- 微软提出CodeT:代码生成新SOTA,20个点的性能提升
- pytorch创建自己的Dataset加载数据集
- 【问题解决】ibatis.binding.BindingException: Type interface xxDao is not known to the MapperRegistry.
- Ministry of Public Security: the international community generally believes that China is one of the safest countries in the world
- B tree and b+ tree
- 业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
- ECCV 2022 | climb to the top semantickitti! Semantic segmentation of LIDAR point cloud based on two-dimensional prior assistance
- Atcoder beginer contest 261 f / / tree array
- OAuth,JWT ,OIDC你们搞得我好乱啊
- Mysql 远程连接权限错误1045问题
猜你喜欢

AtCoder Beginner Contest 261 F // 树状数组

卷积神经网络模型之——LeNet网络结构与代码实现

Docekr学习 - MySQL8主从复制搭建部署

Word style and multi-level list setting skills (II)

零基础学习CANoe Panel(14)——二极管( LED Control )和液晶屏(LCD Control)

massCode 一款优秀的开源代码片段管理器

Zero basic learning canoe panel (14) -- led control and LCD control
![[operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+](/img/d8/90116f967ef0f5920848eca1f55cdc.png)
[operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+

Zero basic learning canoe panel (13) -- trackbar

Atcoder beginer contest 261e / / bitwise thinking + DP
随机推荐
【问题解决】ibatis.binding.BindingException: Type interface xxDao is not known to the MapperRegistry.
ECCV2022 | TransGrasp类级别抓取姿态迁移
手写一个博客平台~第一天
B tree and b+ tree
[machine learning] experimental notes - emotion recognition
卷积神经网络模型之——VGG-16网络结构与代码实现
Moving Chinese figure liushenglan
Substance Designer 2021软件安装包下载及安装教程
【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
Summary of Niuke forum project deployment
2022.07.24 (lc_6125_equal row and column pairs)
跌荡的人生
What is ci/cd?
吕蒙正《破窑赋》
零基础学习CANoe Panel(12)—— 进度条(Progress Bar)
The larger the convolution kernel, the stronger the performance? An interpretation of replknet model
conda常用命令:安装,更新,创建,激活,关闭,查看,卸载,删除,清理,重命名,换源,问题
How to understand metrics in keras
[rust] reference and borrowing, string slice type (& STR) - rust language foundation 12
深度学习的训练、预测过程详解【以LeNet模型和CIFAR10数据集为例】