简介: 图像分类模型评估之用python绘制混淆矩阵confusion_matrix_python confusion_matrix 设置设备 device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”) 定义数据增强 transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=...
4 label for_,label in class_indect.items():不要key,只要value 5 实例化上面定义的ConfusionMatrix类 6 启动验证模式 7 上下文管理器 torch.no_grad(),停止pytorch对变量梯度的跟踪 8 遍历dataloader数据集 9 分为图片,标签 10 把图片储存到设备中,输入网络,得到输出 11 softmax处理 12 通过argmax得到最大...
测试的代码: confmat = ConfusionMatrix(num_classes=3) # 实例化混淆矩阵 ture = torch.LongTensor([[1,2,1],[0,2,2],[0,1,1]]) pred = torch.LongTensor([[1,2,0],[1,2,1],[0,2,2]]) confmat.update(ture, pred) # update 混淆矩阵的值 print(confmat) ''' global correct: 0.444...
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") device 交通标志识别 德国交通标志识别基准(GTSRB)包含了超过50,000张带有40多种交通标志注释的图像。给定一张图像,您需要识别出其中的交通标志。 !unzip -qq GTSRB_Final_Training_Images.zip 代码模拟 让我们先来了解一下数据。每个交...
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") device 交通标志识别 德国交通标志识别基准(GTSRB)包含了超过50,000张带有40多种交通标志注释的图像。给定一张图像,您需要识别出其中的交通标志。 !unzip -qq GTSRB_Final_Training_Images.zip ...
import torch.nn as nn import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline 我们可以使用pandas库的read_csv()方法来导入包含我们的数据集的CSV文件。 dataset = pd.read_csv(r'E:Datasetscustomer_data.csv') ...
print("Confusion matrix:\n", confusion)print("Accuracy:", accuracy)```2. 准确率(accuracy_score):准确率是最简单、最直接的计算模型性能的指标。它是指模型正确分类样本数量与总样本数量的比例。Scikit-learn库中提供了accuracy_score函数来计算准确率。以下是使用accuracy_score计算准确率的示例代码:```...
1defset_seed(args):2random.seed(args.seed)3np.random.seed(args.seed)4torch.manual_seed(args.seed)5ifargs.n_gpu > 0:6torch.cuda.manual_seed_all(args.seed)先加载少量数据 如果你的数据量太大,并且你正在处理比如清理数据或建模等后续编码时,请使用 `nrows `来避免每次都加载大量数据。当你只...
model.load_state_dict(torch.load(f'./{data_path}/cnn.pth')) criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) # Train the model train_cnn(model, train_loader, criterion, optimizer) ...
PyTorch使用torch.save(model.state_dict(), filepath)保存模型的状态字典,使用model.load_state_dict(torch.load(filepath))加载模型状态字典。每种方法都有其特定的使用场景和优缺点,选择合适的方法可以帮助更有效地管理和复用机器学习模型。 参考文档:Python 机器学习 模型保存和加载-CJavaPy...