Pytorch学习
从文件夹中导入数据并进行预处理
import torch
import torch.utils.data as Data
from torchvision.datasets import FashionMNIST
import numpy as np
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
from torchvision.datasets import ImageFolder
from PIL import Image, ImageFont, ImageDraw, ImageSequence
"""
从文件夹中导入数据并进行预处理
"""
train_data_transfroms = transforms.Compose([
transforms.RandomResizedCrop(224), # 随机长宽比裁剪为224*224
transforms.RandomHorizontalFlip(), # 依概率p=0.5水平翻转
transforms.ToTensor(), # 转化为张量并归一化质[0.0-1.0]
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]) # imagenet训练集中抽样算出来的
])
train_data_dir = "E:/sotsugyosextsukei/learn_pytorch/imagedata/"
train_data = ImageFolder(train_data_dir, transform=train_data_transfroms)
"""
shuffle=True 表示从数据集中获取每个批量的图片先打乱数据,不重复选取
batch_size 表示每个批量中有batch_size张图片
num_workers 指定导入数据使用的进程数量
特别标注:当num_workers不等于0时,需要用在main中,不然会报错。
if __name__ == '__main__':
"""
if __name__ == '__main__':
train_data_loader = Data.DataLoader(train_data, batch_size=4,
shuffle=True, num_workers=2)
print(train_data_loader)
print("数据集的label:", train_data.targets)
# 每一个step里有四张RGB图片,格式为(c,h,w)
for step, (b_x, b_y) in enumerate(train_data_loader):
if step > 0:
break
print(b_x.shape)
print(b_y.shape)
print("图像的取值范围为:", b_x.min(), "~", b_x.max())
其中
"""
enumerate函数
"""
import numpy as np
a = ["a", "b", "c", "d"]
for i, element in enumerate(a):
print(i, element)
image = [(np.array([[2, 3],
[3, 4]]), np.array([1, 0, 1, 1]))]
for i, (b_x, b_y) in enumerate(image):
print(i, (b_x, b_y))
print(b_x.shape)
print(b_y.shape)
运行结果:
0 a
1 b
2 c
3 d
0 (array([[2, 3],
[3, 4]]), array([1, 0, 1, 1]))
(2, 2)
(4,)