当前位置:网站首页>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
边栏推荐
- 线程的底部实现原理—静态代理模式
- JDBC工具类封装
- Development environment EAS login license modification
- Navicate连接oracle(11g)时ORA:28547 Connection to server failed probable Oeacle Net admin error
- Pytorch学习记录(四):参数初始化
- 编写一个自己的 RedisTemplate
- 建表到页面完整实例演示—联表查询
- Postfix变成垃圾邮件中转站后的补救
- K/3 WISE系统考勤客户端日期只能选到2019年问题
- io. lettuce. core. RedisCommandExecutionException: ERR wrong number of arguments for ‘auth‘ command
猜你喜欢
图解numpy数组矩阵
Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
JVM series (3) -- memory allocation and recycling strategy
数字图像处理基础(冈萨雷斯)一
Pyqy5 learning (4): qabstractbutton + qradiobutton + qcheckbox
Ptorch learning record (XIII): recurrent neural network
Understand the current commonly used encryption technology system (symmetric, asymmetric, information abstract, digital signature, digital certificate, public key system)
一文读懂当前常用的加密技术体系(对称、非对称、信息摘要、数字签名、数字证书、公钥体系)
Pytoch learning record (x): data preprocessing + batch normalization (BN)
随机推荐
线性规划问题中可行解,基本解和基本可行解有什么区别?
rsync实现文件服务器备份
JDBC工具类封装
PreparedStatement防止SQL注入
你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问
多线程与高并发(1)——线程的基本知识(实现,常用方法,状态)
poi生成excel,插入图片
JSP语法及JSTL标签
The attendance client date of K / 3 wise system can only be selected to 2019
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
Get the value of state in effects in DVA
Pyqy5 learning (III): qlineedit + qtextedit
Ora: 28547 connection to server failed probable Oracle net admin error
What is JSON? First acquaintance with JSON
软件架构设计——软件架构风格
Pytorch学习记录(十二):学习率衰减+正则化
You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
异常的处理:抓抛模型
开发环境 EAS登录 license 许可修改
框架解析2.源码-登录认证