目录
一、Tensorboard
Tensorboard介绍
TensorBoard 是Google开发的一个机器学习可视化工具。其主要用于记录机器学习过程,例如:记录损失变化、准确率变化等。
Tensorboard界面
- SCALARS:展示训练过程中的准确率、损失值和权重/偏值的变化情况。
- IMAGES:展示训练过程中记录的图像。
- GRAPHS:展示模型的数据流图,以及各个设备上小号的内存及时间。
- DISTRIBUTIONS:展示训练过程中记录数据的分布图。
- HISTOGRAMS:展示训练过程中记录数据的流程图。
Tensorboard安装
在虚拟环境中用pip安装即可:
pip install tensorboard
pip install tensorflow
安装后,在命令行输入:
tensorboard --help
若可以正常输出,则说明安装成功。
Tensorboard运行
在终端输入:
tensorboard --logdir my_log --port=6006
my_log是Tensorboard的log文件所在的目录。Tensorboard面板中展示的数据都来源于log文件,一般一次完整的运行生成一份log文件。
如有如下输出,则说明启动成功:
- tensorBoard 2.13.0 at http://localhost:6006/ (Press CTRL+C to quit)
此时只需要在浏览器中输入http://localhost:6006/或直接在Pycharm终端点击这个网址即可进入TensorBoard界面。
Pytorch使用Tensorboard
Pytorch使用Tensorboard主要用到了三个API:
SummaryWriter
:这个用来创建一个log文件,TensorBoard面板查看时,也是需要选择查看那个log
文件。add_something
: 向log文件里面增添数据。例如可以通过add_scalar
增添折线图数据,add_image
可以增添图片。close
:当训练结束后,我们可以通过close
方法结束log写入。
接下来,我们来模拟记录训练过程中准确率的变化。
首先需要new一个SummaryWriter
对象:
from torch.utils.tensorboard import SummaryWriter
tb_writer = SummaryWriter(log_dir="runs/flower_experiment")
当运行完该行代码后,可以看到当前目录下生成了一个runs/flower_experiment文件夹,并且里面有event日志
此时已经可以启动tensorboard来查看了
tensorboard --logdir E:\deep-learning-for-image-processing-master\pytorch_classification\tensorboard_test\runs\flower_experiment --port=6006
因为我们还没有向tensorboard写入任何数据,所以看不到:
二、训练模型
1.定义ResNet34网络模型
### model.py
import torch.nn as nn
import torch
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_channel, out_channel, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel,
kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channel)
self.relu = nn.ReLU()
self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel,
kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channel)
self.downsample = downsample
def forward(self, x):
identity = x
if self.downsample is not None:
identity = self.downsample(x)
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out += identity
out = self.relu(out)
return out
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, in_channel, out_channel, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel,
kernel_size=1, stride=1, bias=False) # squeeze channels
self.bn1 = nn.BatchNorm2d(out_channel)
# -----------------------------------------
self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel,
kernel_size=3, stride=stride, bias=False, padding=1)
self.bn2 = nn.BatchNorm2d(out_channel)
# -----------------------------------------
self.conv3 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel*self.expansion,
kernel_size=1, stride=1, bias=False) # unsqueeze channels
self.bn3 = nn.BatchNorm2d(out_channel*self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
def forward(self, x):
identity = x
if self.downsample is not None:
identity = self.downsample(x)
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
out += identity
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, blocks_num, num_classes=1000, include_top=True):
super(ResNet, self).__init__()
self.include_top = include_top
self.in_channel = 64
self.conv1 = nn.Conv2d(3, self.in_channel, kernel_size=7, stride=2,
padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(self.in_channel)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, blocks_num[0])
self.layer2 = self._make_layer(block, 128, blocks_num[1], stride=2)
self.layer3 = self._make_layer(block, 256, blocks_num[2], stride=2)
self.layer4 = self._make_layer(block, 512, blocks_num[3], stride=2)
if self.include_top:
self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) # output size = (1, 1)
self.fc = nn.Linear(512 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
def _make_layer(self, block, channel, block_num, stride=1):
downsample = None
if stride != 1 or self.in_channel != channel * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.in_channel, channel * block.expansion, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(channel * block.expansion))
layers = []
layers.append(block(self.in_channel, channel, downsample=downsample, stride=stride))
self.in_channel = channel * block.expansion
for _ in range(1, block_num):
layers.append(block(self.in_channel, channel))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
if self.include_top:
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
return x
def resnet34(num_classes=1000, include_top=True):
return ResNet(BasicBlock, [3, 4, 6, 3], num_classes=num_classes, include_top=include_top)
def resnet101(num_classes=1000, include_top=True):
return ResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes, include_top=include_top)
2.训练网络参数
###train.py
import os
import math
import argparse
import torch
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
import torch.optim.lr_scheduler as lr_scheduler
from model import resnet34
from my_dataset import MyDataSet
from data_utils import read_split_data, plot_class_preds
from train_eval_utils import train_one_epoch, evaluate
def main(args):
device = torch.device(args.device if torch.cuda.is_available() else "cpu")
print(args)
print('Start Tensorboard with "tensorboard --logdir=runs", view at http://localhost:6006/')
# 实例化SummaryWriter对象
tb_writer = SummaryWriter(log_dir="runs/flower_experiment")
if os.path.exists("./weights") is False:
os.makedirs("./weights")
# 划分数据为训练集和验证集
train_images_path, train_images_label, val_images_path, val_images_label = read_split_data(args.data_path)
# 定义训练以及预测时的预处理方法
data_transform = {
"train": transforms.Compose([transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]),
"val": transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])}
# 实例化训练数据集
train_data_set = MyDataSet(images_path=train_images_path,
images_class=train_images_label,
transform=data_transform["train"])
# 实例化验证数据集
val_data_set = MyDataSet(images_path=val_images_path,
images_class=val_images_label,
transform=data_transform["val"])
batch_size = args.batch_size
# 计算使用num_workers的数量
nw = min([os.cpu_count(), batch_size if batch_size > 1 else 0, 8]) # number of workers
print('Using {} dataloader workers every process'.format(nw))
train_loader = torch.utils.data.DataLoader(train_data_set,
batch_size=batch_size,
shuffle=True,
pin_memory=True,
num_workers=nw,
collate_fn=train_data_set.collate_fn)
val_loader = torch.utils.data.DataLoader(val_data_set,
batch_size=batch_size,
shuffle=False,
pin_memory=True,
num_workers=nw,
collate_fn=val_data_set.collate_fn)
# 实例化模型
model = resnet34(num_classes=args.num_classes).to(device)
# 将模型写入tensorboard
init_img = torch.zeros((1, 3, 224, 224), device=device)
tb_writer.add_graph(model, init_img)
# 如果存在预训练权重则载入
if os.path.exists(args.weights):
weights_dict = torch.load(args.weights, map_location=device)
load_weights_dict = {k: v for k, v in weights_dict.items()
if model.state_dict()[k].numel() == v.numel()}
model.load_state_dict(load_weights_dict, strict=False)
else:
print("not using pretrain-weights.")
# 是否冻结权重
if args.freeze_layers:
print("freeze layers except fc layer.")
for name, para in model.named_parameters():
# 除最后的全连接层外,其他权重全部冻结
if "fc" not in name:
para.requires_grad_(False)
pg = [p for p in model.parameters() if p.requires_grad]
optimizer = optim.SGD(pg, lr=args.lr, momentum=0.9, weight_decay=0.005)
# Scheduler https://arxiv.org/pdf/1812.01187.pdf
lf = lambda x: ((1 + math.cos(x * math.pi / args.epochs)) / 2) * (1 - args.lrf) + args.lrf # cosine
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf)
for epoch in range(args.epochs):
# train
mean_loss = train_one_epoch(model=model,
optimizer=optimizer,
data_loader=train_loader,
device=device,
epoch=epoch)
# update learning rate
scheduler.step()
# validate
acc = evaluate(model=model,
data_loader=val_loader,
device=device)
# add loss, acc and lr into tensorboard
print("[epoch {}] accuracy: {}".format(epoch, round(acc, 3)))
tags = ["train_loss", "accuracy", "learning_rate"]
tb_writer.add_scalar(tags[0], mean_loss, epoch)
tb_writer.add_scalar(tags[1], acc, epoch)
tb_writer.add_scalar(tags[2], optimizer.param_groups[0]["lr"], epoch)
# add figure into tensorboard
fig = plot_class_preds(net=model,
images_dir="pytorch_classification/tensorboard_test/plot_img",
transform=data_transform["val"],
num_plot=5,
device=device)
if fig is not None:
tb_writer.add_figure("predictions vs. actuals",
figure=fig,
global_step=epoch)
# add conv1 weights into tensorboard
tb_writer.add_histogram(tag="conv1",
values=model.conv1.weight,
global_step=epoch)
tb_writer.add_histogram(tag="layer1/block0/conv1",
values=model.layer1[0].conv1.weight,
global_step=epoch)
# save weights
torch.save(model.state_dict(), "./weights/model-{}.pth".format(epoch))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--num_classes', type=int, default=5)
parser.add_argument('--epochs', type=int, default=30)
parser.add_argument('--batch-size', type=int, default=16)
parser.add_argument('--lr', type=float, default=0.001)
parser.add_argument('--lrf', type=float, default=0.1)
# 数据集所在根目录
#https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz
img_root = "E:/deep-learning-for-image-processing-master/data_set/flower_data/flower_photos"
parser.add_argument('--data-path', type=str, default=img_root)
# resnet34 官方权重下载地址
#https://download.pytorch.org/models/resnet34-333f7ec4.pth
parser.add_argument('--weights', type=str, default='',
help='initial weights path')
parser.add_argument('--freeze-layers', type=bool, default=False)
parser.add_argument('--device', default='cuda', help='device id (i.e. 0 or 0,1 or cpu)')
opt = parser.parse_args()
main(opt)
其中参数设置如下:
parser.add_argument(‘–num_classes’, type=int, default=5)#类别设为5
parser.add_argument(‘–epochs’, type=int, default=30)#迭代轮次为30
parser.add_argument(‘–batch-size’, type=int, default=16)#批量读取大小为16
parser.add_argument(‘–lr’, type=float, default=0.001)#学习率设为0.001
绘制折线图可用add_scalar()
方法实现,并将其写入tensorboard:
tb_writer.add_scalar(tags[0], mean_loss, epoch)
tb_writer.add_scalar(tags[1], acc, epoch)
tb_writer.add_scalar(tags[2],optimizer.param_groups[0][“lr”], epoch)
运行train.py脚本后,可以看见训练过程:
此时我们接着打开Pycharm终端,输入这条指令:
tensorboard --logdir E:\deep-learning-for-image-processing-master\pytorch_classification\tensorboard_test\runs\flower_experiment --s
amples_per_plugin=images=50 --port=6006
打开日志文件:
tensorboard --logdir mylogs
更改显示的图片数量:
--samples_per_plugin=images=50
指定输出端口:
--port 6006 #用于更改tensorboard的端口
点击生成的 http://localhost:6006/ 网址即可进入到tensorboard界面当中。
三、Tensorboard数据图
1、SCALARS
准确率是分类问题中最常用的评估指标之一。它表示分类正确的样本数占总样本数的比例。准确率越高,模型的性能越好。
损失函数用于计算模型预测的输出与实际标签之间的差异。这个差异值,即损失值,能够直观地反映出模型在当前参数下的性能。通过最小化这个损失值,我们可以驱使模型不断学习和改进,直至达到满意的预测精度。
从上述三图中可以看见随着训练轮次的叠加,模型预测准确率越来越高,而学习率和损失函数则在不断下降,这代表着模型的性能逐步提升。
2、IMAGES
通过上述结果可以看见,第1幅图雏菊(daisy)和第2幅图蒲公英(dandelion)的预测准确率越来越高,但是第3幅图却时而被预测为玫瑰(roses),时而被预测为郁金香(tulips),这可能是因为二者在花朵形状和花瓣特征上类似导致预测结果的偏差。
3、GRAPHS
计算图(GRAPHS)是由节点和边组成的,其中节点表示操作(如加法、乘法、卷积等运算),边表示数据的流动。
Graphs 可以展示模型的层次结构。它可以将模型的不同部分(如不同的层、模块等)进行分组和折叠,比如,在一个深度神经网络中,可以展开某一层来查看该层中具体的操作和参数,也可以折叠整个模型的某些部分以获得一个整体的架构概览。
4、HISTOGRAMS
5、DISTRIBUTIONS
四、计算FLOPs
1、手算
2、程序计算
import torch
from thop import profile
from torch.utils.tensorboard import SummaryWriter
from model import resnet34
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = resnet34(num_classes=5,).to(device) # 您的模型
input = torch.zeros(1, 3, 224, 224).cuda() # 一个示例输入
flops, params = profile(model, inputs=(input, ))
print("FLOPs: ",flops)
print("params: ",params)
thop库:是一个用于计算 PyTorch 模型的计算复杂度(FLOPs)和参数数量的库。
- 首先,根据系统是否有可用的 CUDA(GPU 加速)来选择设备(cuda或cpu)。
- 然后,实例化resnet34模型,并将其移动到选择的设备上。这里的num_classes = 5表示模型最后输出的类别数为 5。
- 创建一个形状为(1, 3, 224, 224)的零张量,其中1表示批量大小,3表示图像的通道数(RGB),224和224是图像的高度和宽度。这个张量被移动到cuda设备上,作为模型的输入示例。
- 使用thop库的profile函数来计算模型的 FLOPs 和参数数量。profile函数接受模型和输入张量作为参数,并返回计算得到的 FLOPs 和参数数量,最后打印出来。
结果如图:
我们手动计算出resnet34模型的FLOPs为3.8*10^9
,
使用thop库计算模型的FLOPs为3.68*10^9
。