当前位置:网站首页>Treatment of tensorflow sequelae - simple example record torch utils. data. dataset. Picture dimension problem when rewriting dataset
Treatment of tensorflow sequelae - simple example record torch utils. data. dataset. Picture dimension problem when rewriting dataset
2022-04-23 05:53:00 【umbrellalalalala】
torch Great God, please ignore this article ...
1, A simple example review DataSet
from torch.utils.data import Dataset
class dataset(Dataset):
def __init__(self):
# We need to convert to array, Otherwise, the running result will be very strange
self.data = np.array([[1,1,1,1],
[2,2,2,2],
[3,3,3,3],
[4,4,4,4],
[5,5,5,5],
[6,6,6,6]])
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
# return data[idx], This sentence is missing self, Report errors ...
return self.data[idx]
dataset = dataset()
dataloader = torch.utils.data.DataLoader(dataset,
batch_size=2,
shuffle=False,
num_workers=1)
for i, data_ in enumerate(dataloader):
print(i)
print(data_)
Run the observation :
0
tensor([[1, 1, 1, 1],
[2, 2, 2, 2]])
1
tensor([[3, 3, 3, 3],
[4, 4, 4, 4]])
2
tensor([[5, 5, 5, 5],
[6, 6, 6, 6]])
This example is enough to understand DataSet 了
2, dimension
Refer to this article :
https://blog.csdn.net/xddwz/article/details/108405817
# -*- coding: utf-8 -*-
import numpy as np
import torch
from torch.utils.data.dataset import Dataset
from torch.utils.data.dataloader import DataLoader
from torchvision.transforms import transforms
import os
import cv2
from PIL import Image
class MyDataset(Dataset):
def __init__(self, transform=None):
self.transform = transforms.Compose([
transforms.ToTensor() # Here is only the most basic example
])
self.image_path = './image_data2/'
self.image_names = os.listdir(self.image_path)
def __len__(self):
return len(self.image_names)
def __getitem__(self, item):
image_name = self.image_names[item]
image = cv2.imread(os.path.join(self.image_path, image_name)) # What I read is BGR data
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Turn into RGB, It can also be used. img = img[:, :, (2, 1, 0)]
# At this moment image yes H,W,C The order of , Therefore, the following needs to be transformed into C, H, W
image = torch.from_numpy(image).permute(2, 0, 1)
# image = Image.open(os.path.join(self.image_path, image_name))
# # print(image.shape)
# image = self.transform(image)
return image
This code is from the above link , The reason for moving here is to emphasize one thing : We know torch The order of dimensions is BCHW, And in the above code __getitem__()
, Is to return a picture , So what we need to pay attention to at this time is , The original dimensional order of a single picture is HWC, That is, the dimension is (height, width, channel), We need to adjust its dimension to (channel, height, width), And then back again .
meanwhile , According to the above code , Also need attention , Back to image Of shape No (1, channel, height, width), It is (channel, height, width),batch The corresponding dimension is __getitem__()
There is no need to consider .
For some channel by 1 Pictures of the , Add if necessary channel dimension , So just squeeze(0) That's it .
版权声明
本文为[umbrellalalalala]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230543450851.html
边栏推荐
- Pytorch learning record (IX): convolutional neural network in pytorch
- PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包
- Configure domestic image accelerator for yarn
- Conda 虚拟环境管理(创建、删除、克隆、重命名、导出和导入)
- redhat实现目录下特定文本类型内关键字查找及vim模式下关键字查找
- The role of brackets' [] 'in the parameter writing method in MDN documents
- MySQL的锁机制
- io.lettuce.core.RedisCommandExecutionException: ERR wrong number of arguments for ‘auth‘ command
- Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
- 多线程与高并发(3)——synchronized原理
猜你喜欢
图解numpy数组矩阵
mysql sql优化之Explain
JDBC连接数据库
Issue 36 summary of atcoder beginer contest 248
JVM系列(4)——内存溢出(OOM)
Multithreading and high concurrency (1) -- basic knowledge of threads (implementation, common methods, state)
opensips(1)——安装opensips详细流程
Dva中在effects中获取state的值
Get the value of state in effects in DVA
Configure domestic image accelerator for yarn
随机推荐
Multithreading and high concurrency (1) -- basic knowledge of threads (implementation, common methods, state)
关于二叉树的遍历
治疗TensorFlow后遗症——简单例子记录torch.utils.data.dataset.Dataset重写时的图片维度问题
PyQy5学习(三):QLineEdit+QTextEdit
自定义异常类
JDBC连接数据库
Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
Object to map
Dwsurvey is an open source questionnaire system. Solve the problem that cannot be run and modify the bug.
Pytorch學習記錄(十三):循環神經網絡((Recurrent Neural Network)
Pytoch learning record (x): data preprocessing + batch normalization (BN)
容器
Ptorch learning record (XIII): recurrent neural network
框架解析2.源码-登录认证
SQL基础:初识数据库与SQL-安装与基本介绍等—阿里云天池
JVM family (4) -- memory overflow (OOM)
Insert picture in freemark
The list attribute in the entity is empty or null, and is set to an empty array
mysql-触发器、存储过程、存储函数
金蝶EAS“总账”系统召唤“反过账”按钮