Loading... # Pytorch学习 ## 配置环境 python3.9+anaconda+pytorch 查看cuda版本号:WIN+S搜NVIDIA控制面板,帮助—系统信息-组件 查看 <img src="http://xherlock.top/usr/uploads/2022/08/1914778630.png" alt="image-20220815221841700" style="zoom:50%;" style=""> 或者控制台输入nvidia-smi ![image-20220815222002069.png](http://xherlock.top/usr/uploads/2022/08/1101877361.png) 创建新的conda虚拟环境: ~~~ conda create -n pytorch python=3.9 ~~~ 查看已安装的虚拟环境 ~~~ conda env list ~~~ 删除环境 ~~~ conda remove -n pytorch --all ~~~ 进入虚拟环境 ~~~ conda activate pytorch ~~~ 打开Pytorch官网,按照需求选择安装对应的Pytorch,输入对应命令行 下载过慢:https://blog.csdn.net/watermelon1123/article/details/88122020 测试 ~~~ import torch torch.cuda.is_available() ~~~ out ~~~ True ~~~ 补充: **dir()**: 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表 ![image-20220816141315388.png](http://xherlock.top/usr/uploads/2022/08/949549279.png) **help()**:类似说明书 ![image-20220816141423004.png](http://xherlock.top/usr/uploads/2022/08/193965926.png) --- 虚拟anaconda环境中安装jupyter notebook https://blog.inkuang.com/2019/319/ ## pytorch加载数据 数据集: * 提供一种方式取获取数据及其标签 * 如何获取每一个数据及其标签 * 告诉我们总共有多少的数据 eg: 获取数据集图片信息 ~~~python from PIL import Image img_path = "./hymenoptera_data/train/ants/0013035.jpg" img = Image.open(img_path) img.size # Out[]: (768, 512) ~~~ 获取数据集目录列表 ~~~python dir_path = "hymenoptera_data/train/ants" import os img_path_list = os.listdir(dir_path) img_path_list[0] # Out[]: '0013035.jpg' ~~~ 拼接路径: ![image-20220816151330511.png](http://xherlock.top/usr/uploads/2022/08/1378051242.png) ~~~python import os root_dir = "hymenoptera_data/train" label_dir = "ants" path = os.path.join(root_dir, label_dir) ~~~ 最终查看 ~~~python 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) 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 = "hymenoptera_data/train" label_dir = "ants" ants_dataset = MyData(root_dir, label_dir) ~~~ 查看单个图片 ~~~python img, label = ants_dataset[0] img.show() ~~~ 可以拼接两个数据集 ~~~python train_dataset = ants_dataset + bees_dataset ~~~ ## tensorboard的使用 导入包 ~~~python from torch.utils.tensorboard import SummaryWriter ~~~ 1.**writer.add_scalar()**: 功能:将标量添加到 summary 参数: tag (string):数据标识符 scalar_value (float or string/blobname):要保存的数值 global_step (int):全局步值 walltime (float):可选参数,用于记录发生的时间,默认为 time.time() ~~~python from torch.utils.tensorboard import SummaryWriter writer = SummaryWriter("logs") for i in range(100): writer.add_scalar("y=x", i, i) writer.close() ~~~ 控制台打开tensorboard:tensorboard --logdir=logs ![image-20220816172808324.png](http://xherlock.top/usr/uploads/2022/08/1509024713.png) 2.**writer.add_image()**: add_image(self, tag, img_tensor, global_step=None, walltime=None, dataformats=‘CHW’): 绘制图片,可用于检查模型的输入,监测 feature map 的变化,或是观察 weight。 tag:图的名称 img_tensor:图片的类型要是**torch.Tensor, numpy.array, string**这三种 global_step:第几张图片 dataformats=‘CHW’,默认CHW,**tensor是CHW,numpy是HWC** ![image-20220816175620437.png](http://xherlock.top/usr/uploads/2022/08/54678647.png) ## Transforms ToTensor():将PIL或np形式图片转换为tensor形式 ```python from PIL import Image from torchvision import transforms image_path = "hymenoptera_data/train/ants/0013035.jpg" img = Image.open(image_path) print(img) tensor_trans = transforms.ToTensor() tensor_img = tensor_trans(img) print(tensor_img) ``` 输出: ~~~ <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=768x512 at 0x150B5D38EE0> tensor([[[0.3137, 0.3137, 0.3137, ..., 0.3176, 0.3098, 0.2980], [0.3176, 0.3176, 0.3176, ..., 0.3176, 0.3098, 0.2980], [0.3216, 0.3216, 0.3216, ..., 0.3137, 0.3098, 0.3020], ..., [0.3412, 0.3412, 0.3373, ..., 0.1725, 0.3725, 0.3529], [0.3412, 0.3412, 0.3373, ..., 0.3294, 0.3529, 0.3294], [0.3412, 0.3412, 0.3373, ..., 0.3098, 0.3059, 0.3294]], [[0.5922, 0.5922, 0.5922, ..., 0.5961, 0.5882, 0.5765], [0.5961, 0.5961, 0.5961, ..., 0.5961, 0.5882, 0.5765], [0.6000, 0.6000, 0.6000, ..., 0.5922, 0.5882, 0.5804], ..., [0.6275, 0.6275, 0.6235, ..., 0.3608, 0.6196, 0.6157], [0.6275, 0.6275, 0.6235, ..., 0.5765, 0.6275, 0.5961], [0.6275, 0.6275, 0.6235, ..., 0.6275, 0.6235, 0.6314]], [[0.9137, 0.9137, 0.9137, ..., 0.9176, 0.9098, 0.8980], [0.9176, 0.9176, 0.9176, ..., 0.9176, 0.9098, 0.8980], [0.9216, 0.9216, 0.9216, ..., 0.9137, 0.9098, 0.9020], ..., [0.9294, 0.9294, 0.9255, ..., 0.5529, 0.9216, 0.8941], [0.9294, 0.9294, 0.9255, ..., 0.8863, 1.0000, 0.9137], [0.9294, 0.9294, 0.9255, ..., 0.9490, 0.9804, 0.9137]]]) Process finished with exit code 0 ~~~ <img src="http://xherlock.top/usr/uploads/2022/08/850891730.png" alt="image-20220816213946449" style="zoom:50%;" style=""> 最后修改:2022 年 08 月 19 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 0 如果觉得我的文章对你有用,请随意赞赏