当前位置:网站首页>Pytoch -- data loading and processing
Pytoch -- data loading and processing
2022-04-23 05:54:00 【Zuo Xiaotian ^ o^】
Pytorch A tool for data processing in
scikit-image: For images IO And transformation
pandas: Used to make it easier to csv analysis
from here Download datasets .
The data is stored in “data / faces /” A directory of . This dataset is actually imagenet Dataset standard
Note is face In the picture of dlib Face detection (dlib’s pose estimation) A good picture . What we need to deal with is a data set of facial posture . Here's the picture
Dataset class
torch.utils.data.Dataset
An abstract class that represents a dataset .
Custom datasets should inherit Dataset And cover the following methods * __len__
Realization len(dataset) Return the size of the dataset . * __getitem__
Used to get some index data , for example dataset[i] Medium (i).
Create dataset classes
Create a dataset class for the facial dataset . We will be in __init__
Read from csv File contents of , stay __getitem__
Read pictures from .
Our data samples will be based on such a dictionary {‘image’: image, ‘landmarks’: landmarks} organization .
Our dataset class will add a Optional parameters transform
To facilitate the pretreatment of samples . init The method is shown in the figure below :
class FaceLandmarksDataset(Dataset):
""" Facial marker dataset ."""
def __init__(self, csv_file, root_dir, transform=None):
""" csv_file(string): Annotated csv Path to file . root_dir(string): Directory containing all images . transform(callable, optional): Optional transformations available on a sample """
self.landmarks_frame = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.landmarks_frame)
def __getitem__(self, idx):
img_name = os.path.join(self.root_dir,self.landmarks_frame.iloc[idx, 0])
image = io.imread(img_name)
landmarks = self.landmarks_frame.iloc[idx, 1:]
landmarks = np.array([landmarks])
landmarks = landmarks.astype('float').reshape(-1, 2)
sample = {
'image': image, 'landmarks': landmarks}
if self.transform:
sample = self.transform(sample)
return sample
Data visualization
face_dataset = FaceLandmarksDataset(csv_file='data/faces/face_landmarks.csv',root_dir='data/faces/')
fig = plt.figure()
for i in range(len(face_dataset)):
sample = face_dataset[i]
print(i, sample['image'].shape, sample['landmarks'].shape)
ax = plt.subplot(1, 4, i + 1)
plt.tight_layout()
ax.set_title('Sample #{}'.format(i))
ax.axis('off')
show_landmarks(**sample)
if i == 3:
plt.show()
break
Data transformation
Through the above example, we will find that the picture is not the same size .
Most neural networks assume that the size of the image is the same .
So we need to do some pretreatment . Let's create three transformations :
* Rescale
: Zoom in and out
* RandomCrop
: Randomly crop the picture .
* ToTensor
: This is a data enhancement operation , hold numpy Format picture to torch Format picture ( We need to exchange axes ).
We will write them in the form of callable classes instead of simple functions , So you don't need to pass parameters every time you call . We just need to achieve call Method , have to When necessary init Method . We can call these transformations like this :
版权声明
本文为[Zuo Xiaotian ^ o^]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230543244432.html
边栏推荐
- SQL基础:初识数据库与SQL-安装与基本介绍等—阿里云天池
- No.1.#_ 6 Navicat shortcuts
- Software architecture design - software architecture style
- JSP语法及JSTL标签
- 创建企业邮箱账户命令
- Insert picture in freemark
- 治疗TensorFlow后遗症——简单例子记录torch.utils.data.dataset.Dataset重写时的图片维度问题
- Manually delete registered services on Eureka
- The list attribute in the entity is empty or null, and is set to an empty array
- MySQL事务
猜你喜欢
数字图像处理基础(冈萨雷斯)一
Ptorch learning record (XIII): recurrent neural network
Development environment EAS login license modification
Pyemd installation and simple use
PreparedStatement防止SQL注入
MySQL的锁机制
filebrowser实现私有网盘
JVM系列(4)——内存溢出(OOM)
JVM series (3) -- memory allocation and recycling strategy
Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
随机推荐
多线程与高并发(1)——线程的基本知识(实现,常用方法,状态)
数字图像处理基础(冈萨雷斯)一
PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包
The attendance client date of K / 3 wise system can only be selected to 2019
jdbc入门\获取数据库连接\使用PreparedStatement
Duplicate key update in MySQL
2.devops-sonar安装
Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
excel获取两列数据的差异数据
Dva中在effects中获取state的值
DWSurvey是一个开源的调查问卷系统。解决无法运行问题,修改bug。
C3P0数据库连接池使用
图解numpy数组矩阵
治療TensorFlow後遺症——簡單例子記錄torch.utils.data.dataset.Dataset重寫時的圖片維度問題
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
io.lettuce.core.RedisCommandExecutionException: ERR wrong number of arguments for ‘auth‘ command
Common status codes
治疗TensorFlow后遗症——简单例子记录torch.utils.data.dataset.Dataset重写时的图片维度问题
Pytorch Learning record (XIII): Recurrent Neural Network
The list attribute in the entity is empty or null, and is set to an empty array