一、配置环境
安装Anaconda
先检查电脑配置:NVIDIA RTX2060 + i7-10750H
我这里使用的是Windows操作系统 + 集成显卡:Inter® UHD Graphics + 独立显卡:NVIDIA RTX2060 ,以下实验基于此配置
安装的链接地址:Anacodan安装包官网地址https://repo.anaconda.com/archive/
这里我选择的是Anaconda3-5.2.0-Windows-x86_64,根据自己的操作系统选对应的版本,下载下来,右键管理员运行安装
Windows开始菜单管理员运行Anaconda Prompt控制台看到有base说明安装成功,输入conda --version
创建项目仓库,建议不要将项目的配置环境都装在base根目录仓库下,因为每个项目的配置环境不一样,如果项目运行是一样的环境就都用一样的解释器来运行,不一样的运行环境就建立不同的仓库,命令如下:
输入conda create -n pytorch python=3.6
创建pytorch仓库,选择y进行安装
输入:nvidia-smi
检查显卡配置
安装pytorch
下拉网页,选择配置如下图,
运行命令:conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
选y确认安装,如下图。
安装过程大概10分钟
输入如下命令,没有报错,说明torch能正常调用显卡驱动
python import torch torch.cuda.is_available()
安装pyCharm
官网下载地址https://www.jetbrains.com/pycharm/download/#section=windows
傻瓜式安装即可,社区版和专业版都行,如果有edu邮箱的建议用一下专业版,白嫖它不香吗
新建项目如图:
Location:我选择的是E:\code\learn_pytorch
使用的虚拟环境是:E:\code\learn_pytorch\venv
Base 解释器:E:\Anaconda3-5.2.0\python.exe
测试pyCharm能否正确解析pytorch:在控制台输入
import torch torch.cuda.is_available()
如图,没有报错,说明解析成功
安装jupyter
运行Anaconda Prompt控制台,输入
conda activate pytorch conda install nb_conda
提示输入y,等待安装大概5分钟
输入:jupyter notebook
然后会跳转网页如图:
点击右边的new --> Python[conda env:pytorch],新建一个终端
如果出现如下报错,删掉提示中的路径下的pythoncom36.dll文件即可
安装tensorboard
在Terminal中输入:
pip install tensorboard tensorboard --logdir=logs --port=6007
安装opencv
pip install opencv-python
二、下载数据集
蚂蚁蜜蜂/练手数据集:链接:https://pan.baidu.com/s/1jZoTmoFzaTLWh4lKBHVbEA
密码: 5suq
解压下载好的数据集,粘贴在项目里
新建项目,编写测试代码
三、工具类的使用
tensorboard使用
# test_tensorboard.py from torch.utils.tensorboard import SummaryWriter import numpy as np from PIL import Image writer = SummaryWriter("logs") image_path = "data/train/bees_image/16838648_415acd9e3f.jpg" img_PIL = Image.open(image_path) # 转换成numpy型数组 img_array = np.array(img_PIL) print(type(img_array)) print(img_array.shape) writer.add_image("test", img_array, 2, dataformats='HWC') # y = x for i in range(100): writer.add_scalar("y=2x", 3*i, i) writer.close()
在Terminal中输入:
如果不–port=6007,就使用系统默认指定的6006端口
tensorboard --logdir=logs --port=6007
openCV读取图片
# read_data.py from torch.utils.data import Dataset #import cv2 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 = "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) train_dataset = ants_dataset + bees_dataset
# rename_dataset.py import os root_dir = "dataset/train" target_dir = "ants_image" img_path = os.listdir(os.path.join(root_dir, target_dir)) label = target_dir.split('_')[0] out_dir = "ants_label" for i in img_path: file_name = i.split('_')[0] with open(os.path.join(root_dir, out_dir, "{}.txt".format(file_name)), 'w') as f: f.write(label)
Transforms使用
# useful_Transforms.py from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms writer = SummaryWriter("logs") img = Image.open("images/fb93a.jpeg") print(img) # ToTensor的使用 trans_totensor = transforms.ToTensor() img_tensor = trans_totensor(img) writer.add_image("ToTensor", img_tensor) # Normalize() 归一化 # [channel] = (input[channel] - mean[channel]) / std[channel] print(img_tensor[0][0][0]) trans_norma = transforms.Normalize([9, 6, 3], [1, 4, 7]) img_norma = trans_norma(img_tensor) print(img_tensor[0][0][0]) writer.add_image("Normalize", img_norma, 2) # Resize print(img.size) trans_resize = transforms.Resize((512, 512)) # img -> resize -> img_resize PIL img_resize = trans_resize(img) # img_resize PIL -> 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) # PIL -> PIL -> tensor 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()
# Transforms.py from PIL import Image from torch.utils.tensorboard import SummaryWriter from torchvision import transforms # tensor的数据类型 # 通过transforms.ToTensor调用 # 2.为什么需要tensor数据类型 # 绝对路径 E:\code\learn_pytorch\data\train\ants_image\0013035.jpg Windows操作系统会把 \ 当做转义字符,不推荐使用绝对路径 # 相对路径 data/train/ants_image/0013035.jpg img_path = "data/train/ants_image/0013035.jpg" img = Image.open(img_path) writer = SummaryWriter("logs") # 1.transforms该如何使用 tensor_trans = transforms.ToTensor() tensor_img = tensor_trans(img) writer.add_image("Tensor_image", tensor_img) writer.close()
CIFAR10公开数据集的使用
CIFAR-10是一个更接近普适物体的彩色图像数据集。CIFAR-10 是由Hinton 的学生Alex Krizhevsky 和Ilya Sutskever 整理的一个用于识别普适物体的小型数据集。一共包含10 个类别的RGB 彩色图片:飞机( airplane )、汽车( automobile )、鸟类( bird )、猫( cat )、鹿( deer )、狗( dog )、蛙类( frog )、马( horse )、船( ship )和卡车( truck )。
每个图片的尺寸为32 × 32 ,每个类别有6000个图像,数据集中一共有50000 张训练图片和10000 张测试图片。
# dataset_transforms.py import torchvision from torch.utils.tensorboard import SummaryWriter dataset_transforms = torchvision.transforms.Compose([ torchvision.transforms.ToTensor() ]) train_set = torchvision.datasets.CIFAR10(root="./dataset", train=True, transform=dataset_transforms, download=True) test_set = torchvision.datasets.CIFAR10(root="./dataset", train=False, transform=dataset_transforms, download=True) # print(test_set[0]) # print(test_set.classes) # # img, target = test_set[0] # print(img) # print(target) # print(test_set.classes[3]) # img.show() writer = SummaryWriter("logs") for i in range(10): img, target = test_set[i] writer.add_image("test_set", img, i) writer.close()
DataLoader的使用
# dataloader.py import torchvision from torch.utils.data import DataLoader # 准备测试数据集 from torch.utils.tensorboard import SummaryWriter test_data = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor()) test_loader = DataLoader(dataset=test_data, batch_size=64, shuffle=True, num_workers=0, drop_last=True) #测试数据集中第一张图片及target img, target = test_data[0] print(img.shape) print(target) writer = SummaryWriter("logs") for epoch in range(2): step =0 for data in test_loader: imgs, targets = data writer.add_images("Epoch: {}".format(epoch), imgs, step) step += 1 writer.close()
shuffle=False 每次都是一样的取
shuffle=True 随机抽取