当前位置:网站首页>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
边栏推荐
- Split and merge multiple one-dimensional arrays into two-dimensional arrays
- mysql如何将存储的秒转换为日期
- PyQy5学习(四):QAbstractButton+QRadioButton+QCheckBox
- MySQL create Oracle exercise table
- 深入源码分析Servlet第一个程序
- MySQL triggers, stored procedures, stored functions
- 实操—Nacos安装与配置
- PyEMD安装及简单使用
- 数据处理之Numpy常用函数表格整理
- 实体中list属性为空或者null,设置为空数组
猜你喜欢
Configure domestic image accelerator for yarn
一文读懂当前常用的加密技术体系(对称、非对称、信息摘要、数字签名、数字证书、公钥体系)
域内用户访问域外samba服务器用户名密码错误
Getting started with JDBC \ getting a database connection \ using Preparedstatement
多线程与高并发(2)——synchronized用法详解
PyQy5学习(四):QAbstractButton+QRadioButton+QCheckBox
Manually delete registered services on Eureka
Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
创建二叉树
Issue 36 summary of atcoder beginer contest 248
随机推荐
excel获取两列数据的差异数据
What is JSON? First acquaintance with JSON
数据处理之Numpy常用函数表格整理
基于thymeleaf实现数据库图片展示到浏览器表格
JDBC工具类封装
MySQL transaction
Total score of [Huawei machine test] (how to deal with the wrong answer? Go back once to represent one wrong answer)
Pytorch learning record (XI): data enhancement, torchvision Explanation of various functions of transforms
Duplicate key update in MySQL
一文读懂当前常用的加密技术体系(对称、非对称、信息摘要、数字签名、数字证书、公钥体系)
POI generates excel and inserts pictures
多个一维数组拆分合并为二维数组
filebrowser实现私有网盘
Record a project experience and technologies encountered in the project
MySQL realizes master-slave replication / master-slave synchronization
The role of brackets' [] 'in the parameter writing method in MDN documents
MySQL query uses \ g, column to row
Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.
mysql sql优化之Explain
事实最终变量与最终变量