其他分享
首页 > 其他分享> > pytorch下载自带数据集并transform

pytorch下载自带数据集并transform

作者:互联网

import os.path
import numpy as np
import torch
import cv2
from PIL import Image
from torch.utils.data import Dataset
import re
from functools import reduce
from torch.utils.tensorboard import SummaryWriter as Writer
from torchvision import transforms
import torchvision as tv
t1=transforms.ToTensor()
#中心裁剪
t2=transforms.CenterCrop(300)
#原来的0-1最小值0则变成(0-0.5)/0.5=-1,而最大值1则变成(1-0.5)/0.5=1.
t3=transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
#传入tensor,输出tensor;传入PIL,输出PIL
t4=transforms.Resize((500,500))
t=transforms.Compose([t1,t2,t3,t4])
#If train=True, creates dataset from training set, otherwise creates from test set.
#数据集下载后为PIL格式:
myDataSet=tv.datasets.CIFAR10(root="./myDataSet",train=True,download=True,transform=t)
#载入日志写入器:
writer=Writer("./myBorderText")
for index,datas in enumerate(myDataSet):
    #存储100张图像:
    if index>10:
        break
    #通道数已转移至第一维:
    writer.add_image("图片中心裁剪处理", img_tensor=datas[0], global_step=index)
writer.close()
#查看命令:tensorboard --logdir=./myBorderText

标签:torch,PIL,writer,0.5,transform,pytorch,transforms,import,自带
来源: https://blog.csdn.net/hh1357102/article/details/123591283