pytorch简单了解 读取数据
from torch.utils.data import Dataset
from PIL import Image
import os
class mydata(Dataset):
def __init__(self,root_dir,label_dir):
self.root_dir=root_dir
self.label_dir=label_dir
self.path=os.path.join(root_dir,label_dir)
self.img_path=os.listdir(self.path)
def __getitem__(self, idx):
imag_name=self.img_path[idx]
imag_item_path=os.path.join(self.root_dir,self.label_dir,imag_name)
img=Image.open(imag_item_path)
label=self.label_dir
return img,label
def __len__(self):
return len(self.img_path)
root_dir='dataset\\hymenoptera_data\\train'
ants_dir='ants'
ants=mydata(root_dir,ants_dir)
tensorboard使用
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
writer=SummaryWriter("logs")
imagepath=r'dataset\hymenoptera_data\train\ants\0013035.jpg'
imag_PIL=Image.open(imagepath)
ima_array=np.array(imag_PIL)
writer.add_image('test',ima_array,1,dataformats='HWC')
for i in range(100):
writer.add_scalar('y=2x',2*i,i)
writer.close()
transform常用用法
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
writer=SummaryWriter('logs')
img=Image.open(r'dataset\hymenoptera_data\train\ants\5650366_e22b7e1065.jpg')
print(img) #pil数据
trans_totensor=transforms.ToTensor()
img_totensor=trans_totensor(img) #转化成tensor数据
writer.add_image('totensor1',img_totensor)
print(img_totensor[0][0][0])
transnorm=transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
imagnorm=transnorm(img_totensor) #对tensor数据进行规划
print(imagnorm[0][0][0])
writer.add_image('normal1',imagnorm)
print(img.size)
tran_resize=transforms.Resize((512,512)) #创建类
imgresize=trans_totensor(img)
writer.add_image('resize',imgresize,0)
print(imgresize)
#compose
writer.close()
标签:课题,img,self,writer,学习,深度,import,path,dir
From: https://www.cnblogs.com/tgfoven/p/17707259.html