一、win10搭建pytorch环境
查看我的另一篇博客:博客链接
二、jupyter的基本使用
1. 特定环境使用jupyter
共有两种方法:
- 1.在环境中下载jupyter
- 2.在jupyter中导入环境
在环境中下载jupyter
下载jupyter,输入
pip install jupyter
或者
conda install jupyter
下载好之后,运行jupyter,输入
jupyter notebook
注意:如果打开报错的话,应该是路径没有添加到环境中,把anaconda3\lib添加到环境变量的环境中!
在Jupyter导入环境
pip install ipykernel #将环境添加到ipython的kernel中 python -m ipykernel install --user --name tensorflow --display-name tf
使用shift+回车运行代码块,并跳转到下一个代码块
2. 两个帮助函数:dir()、help()
3. 三种运行方法对比
(1)Python文件运行:以整个文件运行,以大型项目为主
(2)Python控制台运行:以行运行,可以显示每个变量属性,不利于代码阅读和修改
(3)jupyter运行:以块运行
三、一些基本使用
from torch.utils.data import Dataset
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(self.root_dir, self.label_dir) # dataset/train/ants self.img_path = os.listdir(self.path) def __getitem__(self, idx): img_name = self.img_path[idx] img_item_path = os.path.join(self.root_dir, self.label_dir, img_name) img = Image.open(img_item_path) label = self.label_dir return img, label def __len__(self): return len(self.img_path) root_dir = "dataset/train" ants_label_dir = "ants" bees_label_dir = "bees" ants_dataset = MyData(root_dir, ants_label_dir) bees_dataset = MyData(root_dir, bees_label_dir)
TensofBoard的使用
from torch.utils.tensorboard import SummaryWriter import numpy as np from PIL import Image writer = SummaryWriter("logs") image_path = "dataset/train/bees_image/16838648_415acd9e3f.jpg" img_PIL = Image.open(image_path) img_array = np.array(img_PIL) writer.add_image('test', img_array, 2, dataformats='HWC') for i in range(100): writer.add_scalar('y=2x', 3*i, i) writer.close()
from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms # python的用法 -> tensor数据类型 # 通过transform.ToTensor去解决两个问题 # 1、transforms该如何使用(Python) # 2、为什么我们需要Tensor数据类型 img_path = 'dataset/train/ants_image/0013035.jpg' img = Image.open(img_path) writer = SummaryWriter('logs') tensor_trans = transforms.ToTensor() tensor_image = tensor_trans(img) writer.add_image("Tensor_img", tensor_image) writer.close() print(img) print(tensor_image)
图片类型转化为tensor
from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms writer = SummaryWriter("logs") img = Image.open("dataset/train/ants_image/0013035.jpg") print(img) trans_totensor = transforms.ToTensor() img_tensor = trans_totensor(img) writer.add_image("ToTensor", img_tensor) writer.close()
归一化
from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms writer = SummaryWriter("logs") img = Image.open("dataset/train/ants_image/0013035.jpg") print(img) # ToTensor trans_totensor = transforms.ToTensor() img_tensor = trans_totensor(img) writer.add_image("ToTensor", img_tensor) # Normalize 归一化 print(img_tensor[0][0][0]) # input = (input - mean) / std trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]) img_norm = trans_norm(img_tensor) print(img_norm[0][0][0]) writer.add_image("Normalize", img_norm) writer.close()
from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms writer = SummaryWriter("logs") img = Image.open("dataset/train/ants_image/0013035.jpg") print(img) # ToTensor trans_totensor = transforms.ToTensor() img_tensor = trans_totensor(img) writer.add_image("ToTensor", img_tensor) # Normalize 归一化 print(img_tensor[0][0][0]) # input = (input - mean) / std trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]) img_norm = trans_norm(img_tensor) print(img_norm[0][0][0]) writer.add_image("Normalize", img_norm) # Resize print(img.size) trans_resize = transforms.Resize((512, 512)) # img PIL -> totensor -> img_resize img_resize = trans_resize(img) # img_resize -> totensor -> img_resize tensor img_resize = trans_totensor(img_resize) writer.add_image("Resize", img_resize, 0) print(img_resize) writer.close()
from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms writer = SummaryWriter("logs") img = Image.open("dataset/train/ants_image/0013035.jpg") print(img) # ToTensor trans_totensor = transforms.ToTensor() img_tensor = trans_totensor(img) writer.add_image("ToTensor", img_tensor) # Normalize 归一化 print(img_tensor[0][0][0]) # input = (input - mean) / std trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]) img_norm = trans_norm(img_tensor) print(img_norm[0][0][0]) writer.add_image("Normalize", img_norm) # Resize print(img.size) trans_resize = transforms.Resize((512, 512)) # img PIL -> totensor -> img_resize img_resize = trans_resize(img) # img_resize -> totensor -> img_resize tensor img_resize = trans_totensor(img_resize) writer.add_image("Resize", img_resize, 0) print(img_resize) # Compose -resize -2 trans_resize_2 = transforms.Resize(512) trans_compose = transforms.Compose([trans_resize_2, trans_totensor]) img_resize_2 = trans_compose(img) writer.add_image("Resize", img_resize_2, 1) writer.close()