2023年11月29日发(作者:)

【猫狗数据集】使⽤top1top5准确率衡量模型

数据集下载地址:

链接:/s/1l1AnBgkAAEhh0vI5_loWKw

提取码:2xq4

创建数据集:

读取数据集:

进⾏训练:

保存模型并继续进⾏训练:

加载保存的模型并测试:

划分验证集并边训练边验证:

使⽤学习率衰减策略并边训练边测试:

利⽤tensorboard可视化训练和测试过程:

从命令⾏接收参数:

epochbatchsizestep之间的关系:

之前使⽤的仅仅是top1准确率。在图像分类中,⼀般使⽤top1top5来衡量分类模型的好坏。下⾯来看看。

⾸先在util下新建⼀个⽂件,向⾥⾯加⼊计算top1top5准确率的代码:

import torch

def accu(output, target, topk=(1,)):

"""Computes the accuracy over the k top predictions for the specified values of k"""

with _grad():

maxk = max(topk)

batch_size = (0)

_, pred = output.topk(maxk, 1, True, True)

pred = pred.t()

correct = ((1, -1).expand_as(pred))

res = []

for k in topk:

correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)

(correct__(100.0 / batch_size))

return res

重点就是topk()函数:

(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor)

input:输⼊张量

k:指定返回的前⼏位的值

dim:排序的维度

largest:返回最⼤值

sorted:返回值是否排序

out:可选输出张量

需要注意的是我们这⾥只有两类,因此不存在top5。因此如果设置参数topk=(1,5),则会报错:

RuntimeError:invalid argument 5:k not in range for

dimension at /pytorch/ate ...

因此我们只能设置topk=(1,2),⽽且top2的值肯定是100%。最终res中第⼀位存储的是top1准确率,第⼆位存储的是top2准确率。

然后修改对应的

import torch

from tqdm import tqdm

from tensorflow import summary

import datetime

from utils import acc

"""

current_time = str(().timestamp())

train_log_dir = '/content/drive/My Drive/colab notebooks/output/tsboardx/train/' + current_time

test_log_dir = '/content/drive/My Drive/colab notebooks/output/tsboardx/test/' + current_time

val_log_dir = '/content/drive/My Drive/colab notebooks/output/tsboardx/val/' + current_time

train_summary_writer = _file_writer(train_log_dir)

val_summary_writer = _file_writer(val_log_dir)

test_summary_writer = _file_writer(test_log_dir)

"""

class Trainer:

def __init__(self,criterion,optimizer,model):

ion=criterion

zer=optimizer

=model

def get_lr(self):

for param_group in _groups:

return param_group['lr']

def loop(self,num_epochs,train_loader,val_loader,test_loader,scheduler=None,acc1=0.0):

1=acc1

for epoch in range(1,num_epochs+1):

lr=_lr()

print("epoch{}lr{:.6f}".format(epoch,lr))

(train_loader,epoch,num_epochs)

(val_loader,epoch,num_epochs)

(test_loader,epoch,num_epochs)

if scheduler is not None:

()

def train(self,dataloader,epoch,num_epochs):

()

with _grad():

self._iteration_train(dataloader,epoch,num_epochs)

def val(self,dataloader,epoch,num_epochs):

()

with _grad():

self._iteration_val(dataloader,epoch,num_epochs)

def test(self,dataloader,epoch,num_epochs):

'train_loss':train_,

'train_acc':train_,

}

save_path="/content/drive/My Drive/colab notebooks/output/"

(state,save_path+"/resnet18_final_v2"+".t7")

"""

t_loss = train_,

t_top1 = train_

t_top2 = train_

return t_loss,t_top1,t_top2

def _iteration_val(self,dataloader,epoch,num_epochs):

#total_step=len(dataloader)

#tot_loss = 0.0

#correct = 0

#for i ,(images, labels) in enumerate(dataloader):

t_top1 = test_

t_top2 = test_

"""

with test_summary__default():

('loss', test_, epoch)

('accuracy', test_, epoch)

"""

"""

if epoch_acc > 1:

state = {

"model": _dict(),

"optimizer": _dict(),

"epoch": epoch,