Pytorch 实现计算分类器准确率(总分类及子分类)
2020-02-13 10:56
1491 查看
分类器平均准确率计算:
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 实现计算分类器准确率(总分类及子分类)就是小编分享给大家的全部内容了,希望能给大家一个参考
您可能感兴趣的文章:
相关文章推荐
- pytorch 实现cross entropy损失函数计算方式
- pytorch 实现将自己的图片数据处理成可以训练的图片类型
- 图像神经风格转移(neural transfer)-------Pytorch 实现
- pytorch RNN实现分类
- Attention is all you need pytorch实现 源码解析01 - 数据预处理、词表的构建
- 计算一下pytorch中Resnet34模型前传一次所需要的时间
- Pytorch实现简单的回归
- pytorch cnn 识别手写的字实现自建图片数据
- Attention is all you need pytorch实现 源码解析04 - 模型的测试以及翻译
- pytorch之添加BN的实现
- caffe机器学习自带图片分类器classify.py实现输出预测结果的概率及caffe的web_demo例子运行实例
- 从零开始 Pytorch-yolo v3 目标检测与实现(一)
- 【语义分割系列:三】FCN 论文阅读翻译笔记+资源整理+pytorch实现
- 画pytorch模型图,以及参数计算的方法
- pytorch 1.0实现faster R-CNN,检验自己的数据集
- pytorch使用Variable实现线性回归
- 【语义分割系列:四】 4000 Unet 论文阅读翻译笔记 医学图像 pytorch实现
- Pytorch 实现focal_loss 多类别和二分类示例
- Pytorch 实现自定义参数层的例子
- pytorch实现LeNet5