当前位置:网站首页>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注入
- Latex quick start
- The list attribute in the entity is empty or null, and is set to an empty array
- Object to map
- Dwsurvey is an open source questionnaire system. Solve the problem that cannot be run and modify the bug.
- POI generates excel and inserts pictures
- Get the value of state in effects in DVA
- 多个一维数组拆分合并为二维数组
- 事实最终变量与最终变量
- 如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
猜你喜欢

软件架构设计——软件架构风格

Pyqy5 learning (4): qabstractbutton + qradiobutton + qcheckbox

Duplicate key update in MySQL

PyEMD安装及简单使用

Get the value of state in effects in DVA

You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it

Pytorch學習記錄(十三):循環神經網絡((Recurrent Neural Network)

Pytorch learning record (XII): learning rate attenuation + regularization

filebrowser实现私有网盘

Pytorch learning record (XI): data enhancement, torchvision Explanation of various functions of transforms
随机推荐
你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问
PyQt5学习(一):布局管理+信号和槽关联+菜单栏与工具栏+打包资源包
2-軟件設計原則
线性规划问题中可行解,基本解和基本可行解有什么区别?
PyQy5学习(三):QLineEdit+QTextEdit
POI exports to excel, and the same row of data is automatically merged into cells
线程的底部实现原理—静态代理模式
sklearn之 Gaussian Processes
Add days to date
Manually delete registered services on Eureka
MySQL事务
Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
给yarn配置国内镜像加速器
Pytorch Learning record (XIII): Recurrent Neural Network
实操—Nacos安装与配置
图解HashCode存在的意义
Pytorch learning record (IV): parameter initialization
freemark中插入图片
CONDA virtual environment management (create, delete, clone, rename, export and import)
Software architecture design - software architecture style