Dataset類提供一種方式去獲取數(shù)據(jù)及其label
Dataloader類為后面的網(wǎng)絡(luò)提供不同的數(shù)據(jù)形式
繼承Dataset類寫init和getitem方法兽泄,實(shí)例化處理數(shù)據(jù)集中的圖片:
from torch.utils.data import Dataset
from PIL import Image
import os
class MyData(Dataset):
def __init__(self,path):
self.path=path
self.image_list=os.listdir(self.path)
def __getitem__(self, idx):
image_name=self.image_list[idx]
image_full_path=os.path.join(self.path,image_name)
img=Image.open(image_full_path)
return img
path="C:\\Users\\Y0NGL1N\\PycharmProjects\\pythonProject1\\dataset\\images"
dataset_image=MyData(path)
img=dataset_image[2]
img.show()
TensorBoard:SummaryWriter類的使用(一個(gè)本地服務(wù)器顯示圖表圖片),通過(guò)以下代碼開啟服務(wù)器询吴,端口號(hào)6006
tensorboard --logdir=logs
若要修改默認(rèn)端口號(hào)為6007:
tensorboard --logdir=logs --port=6007
(1)add_scalar方法:顯示"y=2x"散點(diǎn)圖
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")
for i in range(100):
writer.add_scalar("y=2x",2*i,i)
writer.close()
(2)add_image方法:顯示"1.jpg"圖片
import numpy as np
from PIL import Image
writer = SummaryWriter("logs")
path="C:\\Users\\Y0NGL1N\\PycharmProjects\\pythonProject1\\dataset\\images\\1.jpg"
img_PIL = Image.open(path)
img_array = np.array(img_PIL)
#參數(shù)1:標(biāo)簽名稱
#參數(shù)2:np.array圖片格式(不支持PIL格式)溺忧,可通過(guò)type()查看圖片格式
#參數(shù)3:Step
#參數(shù)4:圖片通道格式,可通過(guò)img_array.shape查看通道格式
writer.add_image("img_1",img_array,1,dataformats='HWC')
writer.close()