1.两大法宝
dir()
函数,可以知道工具箱以及工具箱中的分隔区有什么东西。
help()
函数,知道工具如何使用。
打开PyCharm,在控制台运行逐步运行一下代码。
dir(torch)
dir(torch.cuda)
dir(torch.cuda.is_available)
help(torch.cuda.is_available)
2.比较
3.PyTorch加载数据
Dataset
提供一种方式去获取数据及其label(获取每一个数据及其label,告诉我们总共有多少的数据)。
Dataloader
为后面的网络提供不同的数据形式。
在pytorch环境下安装opencv。
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 # root_dir 图片地址
self.label_dir = label_dir # label_dir label
self.path = os.path.join(self.root_dir, self.label_dir) # 获取图片的地址,此函数的方法可以根据不同的系统对路径及逆行拼接
self.img_path_list = os.listdir(self.path) # 将数据转换成list列表
def __getitem__(self, idx): # 通过索引idx获取图片的地址,首先获取所有图片地址的list
img_name = self.img_path_list[idx] # 获取图片的名称
img_path = os.path.join(self.root_dir, self.label_dir, img_name) # 每一个图片的位置
img = Image.open(img_path) # 读取图片
label = self.label_dir
return img, label
def __len__(self): # 返回数据的长度
return len(self.img_path_list)
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 # 数据集拼接
4.TensorBoard的使用
4.1 安装tensorboard
法一:anaconda prompt中激活pytorch环境进行安装
法二:打开pycharm中,在pytorch环境下的项目中,点击terminal,输入pip install tensorboard
上图已经成功安装了。
在terminal中输入tensorboard --logdir=logs --port=6007
,logdir=事件文件所在文件夹名,–port=端口号,自己可以指定一个端口号。也可以使用anaconda的命令行prompt。
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter('logs') # save file
# writer.add_image() # add image ,常用来观察训练结果
# y = x
for i in range(100):
writer.add_scalar('y = x', 2*i, i) # 参数tag--title, scalar_value--y axis, global_step (int)--x axis
# writer.add_scalar() # add scalar data to summary
writer.close()
4.2 add_image()
的使用
def add_image(self, tag, img_tensor, global_step=None, walltime=None, dataformats='CHW'):
"""Add image data to summary.
Note that this requires the ``pillow`` package.
Args:
tag (string): Data identifier
img_tensor (torch.Tensor, numpy.array, or string/blobname): Image data
global_step (int): Global step value to record
walltime (float): Optional override default walltime (time.time())
seconds after epoch of event
Shape:
img_tensor: Default is :math:`(3, H, W)`. You can use ``torchvision.utils.make_grid()`` to
convert a batch of tensor into 3xHxW format or call ``add_images`` and let us do the job.
Tensor with :math:`(1, H, W)`, :math:`(H, W)`, :math:`(H, W, 3)` is also suitable as long as
corresponding ``dataformats`` argument is passed, e.g. ``CHW``, ``HWC``, ``HW``.
Examples::
from torch.utils.tensorboard import SummaryWriter
import numpy as np
img = np.zeros((3, 100, 100))
img[0] = np.arange(0, 10000).reshape(100, 100) / 10000
img[1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000
img_HWC = np.zeros((100, 100, 3))
img_HWC[:, :, 0] = np.arange(0, 10000).reshape(100, 100) / 10000
img_HWC[:, :, 1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000
writer = SummaryWriter()
writer.add_image('my_image', img, 0)
# If you have non-default dimension setting, set the dataformats argument.
writer.add_image('my_image_HWC', img_HWC, 0, dataformats='HWC')
writer.close()
Expected result:
.. image:: _static/img/tensorboard/add_image.png
:scale: 50 %
"""
torch._C._log_api_usage_once("tensorboard.logging.add_image")
if self._check_caffe2_blob(img_tensor):
img_tensor = workspace.FetchBlob(img_tensor)
self._get_file_writer().add_summary(
image(tag, img_tensor, dataformats=dataformats), global_step, walltime)
主要关注tag, img_tensor, global_step三个参数,tag是标题,img_tensor是image data ,对数据的类型有要求。
Shape中img_tensor默认的是(3,H,W),3通道,H高度,W宽度;如果3在后面(H, W, 3),要设置dataformats参数,设置为CHW。
(1)利用opencv读取图片,获得numpy型图片数据。
image_path = 'dataset/train/ants/0013035.jpg'
import cv2
cv_img = cv2.imread(image_path) # 读取图片
(2)利用numpy.array()
,对PIL图片进行转换。
从PIL到numpy,需要在add_image()中指定shape中每一个数字/维表示的含义。
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image
writer = SummaryWriter('logs') # save file
image_path = 'dataset/train/ants/0013035.jpg'
image = Image.open(image_path)
image_array = np.array(image)
writer.add_image('test', image_array, 1, dataformats='HWC') # add image ,常用来观察训练结果,第三个参数1,表示第一步
# y = x
for i in range(100):
writer.add_scalar('y = x', i, i) # 参数tag--title, scalar_value--y axis, global_step (int)--x axis
# writer.add_scalar() # add scalar data to summary
writer.close()
5.Transforms的使用
5.1 transforms的结构及用法
(1)transforms.ToTensor()
的使用
from torchvision import transforms
from PIL import Image
image_path = 'dataset/train/ants/0013035.jpg'
image = Image.open(image_path)
tensor_trans = transforms.ToTensor() # 返回一个ToTensor的对象
tensor_img = tensor_trans(image) # 使用ctrl + p可以查看括号中需要的参数。将一个image的图片转换成一个tensor的image
print(type(tensor_img))
(2)Normalize()
的使用
(3)Resize()
的使用
(4)Compose
的使用
(5)RandomCrop()
用法
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
from PIL import Image
writer = SummaryWriter('logs')
image_path = 'dataset/train/ants/0013035.jpg'
image = Image.open(image_path)
# 1.ToTensor
tensor_trans = transforms.ToTensor() # 返回一个ToTensor的对象
tensor_img = tensor_trans(image) # 使用ctrl + p可以查看括号中需要的参数。将一个image的图片转换成一个tensor的image
# print(type(tensor_img))
writer.add_image('ToTensor', tensor_img)
# 2.Normalize
print(tensor_img[0][0][0])
norm_trans = transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5]) # 第一个参数是mean均值,第二个参数是标准差
norm_img = norm_trans(tensor_img)
print(norm_img[0][0][0])
writer.add_image('Normalize', norm_img)
# 3.Resize,输入必须是一个PIL image
print(image.size) # 输出一下原始图片尺寸的大小
resize_trans = transforms.Resize((512,512))
# img PIL -> resize -> resize_img PIL
resize_img = resize_trans(image)
# resize_img PIL -> ToTensor -> resize_img tensor
resize_img = tensor_trans(resize_img)
writer.add_image('Resize',resize_img,0)
# 4.Compose - Resize,输入必须是一个PIL image
resize_trans2 = transforms.Resize(512)
# PIL -> PIL -> tensor
trans_compose = transforms.Compose([resize_trans2, tensor_trans])
resize_img2 = trans_compose(image)
writer.add_image('Resize',resize_img2,1)
# 5.RandomCrop()用法,随即裁剪
trans_random = transforms.RandomCrop(512)
trans_compose_2 = transforms.Compose([trans_random, tensor_trans])
for i in range(10):
img_crop = trans_compose_2(image)
writer.add_image("RandomCrop", img_crop, i)
writer.close()