#Tensorboard#
from torch.utils.tensorboard import SummaryWriter import numpy as np from PIL import Image writer = SummaryWriter("../logs") image_path = "D:/dev/resources/MLR/dataset/练手数据集/train/ants_image/0013035.jpg" img_PIL = Image.open(image_path) img_array = np.array(img_PIL) print(type(img_array)) print(img_array.shape) writer.add_image("test", img_array, 1, dataformats="HWC") # writer.add_img() for i in range(100): # 范围 writer.add_scalar("y=2x", 2*i, i) # 函数名, x轴, y轴 writer.close()
#TorchVision#
import torchvision def basic(): # 下载训练/测试集 train_set = torchvision.datasets.CIFAR10(root="D:/dev/python/pyWork/Season2/Stage1/data/myimg", train=True, download=False) test_set = torchvision.datasets.CIFAR10(root="D:/dev/python/pyWork/Season2/Stage1/data/myimg", train=False, download=False) img, target = test_set[0] print(img) # 图片信息 print(target) # 索引 print(test_set[0]) print(test_set.classes) print(test_set.classes[target]) img.show() return None def compose(): dataset_transform = torchvision.transforms.Compose( [torchvision.transforms.ToTensor()] ) train_set = torchvision.datasets.CIFAR10(root="D:/dev/python/pyWork/Season2/Stage1/data/myimg", train=True, transform=dataset_transform, download=False) test_set = torchvision.datasets.CIFAR10(root="D:/dev/python/pyWork/Season2/Stage1/data/myimg", train=False, transform=dataset_transform, download=False) print(test_set[0]) return None if __name__ == "__main__": # 代码1:基本使用 # basic() # 代码2:转换类型 compose() print()