Pytorch 实现计算分类器准确率(总分类及子分类)

yipeiwu_com6年前Python基础

分类器平均准确率计算:

correct = torch.zeros(1).squeeze().cuda()
total = torch.zeros(1).squeeze().cuda()
for i, (images, labels) in enumerate(train_loader):
      images = Variable(images.cuda())
      labels = Variable(labels.cuda())

      output = model(images)

      prediction = torch.argmax(output, 1)
      correct += (prediction == labels).sum().float()
      total += len(labels)
acc_str = 'Accuracy: %f'%((correct/total).cpu().detach().data.numpy())

分类器各个子类准确率计算:

correct = list(0. for i in range(args.class_num))
total = list(0. for i in range(args.class_num))
for i, (images, labels) in enumerate(train_loader):
      images = Variable(images.cuda())
      labels = Variable(labels.cuda())

      output = model(images)

      prediction = torch.argmax(output, 1)
      res = prediction == labels
      for label_idx in range(len(labels)):
        label_single = label[label_idx]
        correct[label_single] += res[label_idx].item()
        total[label_single] += 1
 acc_str = 'Accuracy: %f'%(sum(correct)/sum(total))
 for acc_idx in range(len(train_class_correct)):
      try:
        acc = correct[acc_idx]/total[acc_idx]
      except:
        acc = 0
      finally:
        acc_str += '\tclassID:%d\tacc:%f\t'%(acc_idx+1, acc)

以上这篇Pytorch 实现计算分类器准确率(总分类及子分类)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python主线程与子线程的结束顺序实例解析

这篇文章主要介绍了python主线程与子线程的结束顺序实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 引用自 主线程退出对子线...

python+django+sql学生信息管理后台开发

python+django+sql学生信息管理后台开发

一、功能 实现对学生对个人信息的增删查改 实现后台对所有学生信息的操作 二、平台 windows+pycharm(python开发工具) 三、逻辑框图 四、代码展示 此后台采用的是M...

pygame加载中文名mp3文件出现error

pygame加载中文名mp3文件出现error

前言:  今天刚刚做了个音乐列表,但笔者发现在指定目录mp3文件名为中文时,便出现pygame.error,不能正确加载指定mp3文件.写好的代码不想再改了,来个小测试吧 pygame播...

Python类的用法实例浅析

本文实例讲述了Python类的用法。分享给大家供大家参考。具体如下: 先看一段代码: #!/usr/bin/env python class Test: def __init__...

Python识别快递条形码及Tesseract-OCR使用详解

Python识别快递条形码及Tesseract-OCR使用详解

识别快递单号 这次跟老师做项目,这项目大概是流水线上识别快递上的快递单号。首先我尝试了解条形码的基本知识 百度百科:条形码 条形码(barcode)是将宽度不等的多个黑条和空...