当前位置: 首页 > article >正文

深度学习指标可视化案例

在这里插入图片描述

  1. TensorBoard

    • 代码案例:
      from torch.utils.tensorboard import SummaryWriter
      import torch
      import torchvision
      from torchvision import datasets, transforms
      
      # 设置TensorBoard日志路径
      writer = SummaryWriter('runs/mnist')
      
      # 加载数据集
      transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
      trainset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=True, transform=transform)
      trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
      
      # 添加图像到TensorBoard
      images, labels = next(iter(trainloader))
      img_grid = torchvision.utils.make_grid(images)
      writer.add_image('mnist_images', img_grid)
      writer.close()
      
      此代码展示了如何使用TensorBoard记录和可视化图像数据。
  2. t-SNE降维可视化

    • 代码案例:
      from sklearn.manifold import TSNE
      from sklearn.datasets import load_iris
      import matplotlib.pyplot as plt
      
      # 加载数据
      iris = load_iris()
      X = iris.data
      y = iris.target
      
      # t-SNE降维
      tsne = TSNE(n_components=2, random_state=42)
      X_tsne = tsne.fit_transform(X)
      
      # 可视化
      plt.figure(figsize=(8, 8))
      colors = ['red', 'green', 'blue']
      for i in range(len(colors)):
          plt.scatter(X_tsne[y == i, 0], X_tsne[y == i, 1], c=colors[i], label=iris.target_names[i])
      plt.legend()
      plt.show()
      
      此代码使用t-SNE将鸢尾花数据集从4维降至2维,并进行可视化。
  3. 特征图可视化

    • 代码案例:
      import torch
      import torchvision.models as models
      import torchvision.transforms as transforms
      from PIL import Image
      import matplotlib.pyplot as plt
      
      # 加载预训练模型
      model = models.resnet18(pretrained=True)
      model.eval()
      
      # 加载图像
      img = Image.open('path_to_image.jpg')
      transform = transforms.Compose([transforms.Resize(256),
                                      transforms.CenterCrop(224),
                                      transforms.ToTensor(),
                                      transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
      img = transform(img).unsqueeze(0)
      
      # 获取特征图
      with torch.no_grad():
          features = model.conv1(img)
          features = features.squeeze(0)
      
      # 可视化特征图
      fig, ax = plt.subplots(1, 3, figsize=(15, 5))
      for i in range(3):
          ax[i].imshow(features[i].cpu().numpy(), cmap='viridis')
          ax[i].axis('off')
      plt.show()
      
      此代码展示了如何提取并可视化ResNet模型中的特征图。
  4. 混淆矩阵和ROC曲线可视化

    • 代码案例:
      from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, roc_curve, auc
      import matplotlib.pyplot as plt
      
      # 假设y_true是真实标签,y_pred是预测标签,y_scores是预测概率
      y_true = [0, 1, 0, 1, 0, 1, 0, 1]
      y_pred = [0, 0, 0, 1, 0, 1, 1, 1]
      y_scores = [0.1, 0.4, 0.35, 0.8, 0.2, 0.7, 0.6, 0.9]
      
      # 绘制混淆矩阵
      cm = confusion_matrix(y_true, y_pred)
      disp = ConfusionMatrixDisplay(confusion_matrix=cm)
      disp.plot(cmap=plt.cm.Blues)
      plt.title('Confusion Matrix')
      plt.show()
      
      # 绘制ROC曲线
      fpr, tpr, _ = roc_curve(y_true, y_scores)
      roc_auc = auc(fpr, tpr)
      plt.plot(fpr, tpr, color='blue', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
      plt.plot([0, 1], [0, 1], color='gray', linestyle='--')
      plt.title('Receiver Operating Characteristic (ROC) Curve')
      plt.xlabel('False Positive Rate')
      plt.ylabel('True Positive Rate')
      plt.legend(loc='lower right')
      plt.show()
      
      此代码展示了如何绘制混淆矩阵和ROC曲线。
  5. Neptune团队协作

    • 代码案例:
      import neptune.new as neptune
      from sklearn.model_selection import train_test_split
      from sklearn.linear_model import LogisticRegression
      from sklearn.metrics import accuracy_score
      
      # 初始化Neptune项目
      run = neptune.init_run(project="your_project_name", api_token="your_api_token")
      
      # 记录实验参数
      params = {"n_estimators": 100, "learning_rate": 0.1}
      run["parameters"] = params
      
      # 加载数据
      X, y = load_data()
      X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
      
      # 训练模型
      model = LogisticRegression()
      model.fit(X_train, y_train)
      y_pred = model.predict(X_test)
      accuracy = accuracy_score(y_test, y_pred)
      
      # 记录模型性能
      run["accuracy"] = accuracy
      
      # 保存模型
      run["model"].upload("model.pkl")
      
      # 结束实验
      run.stop()
      
      此代码展示了如何使用Neptune记录实验参数、模型性能和模型文件。
  6. WandB

    • 代码案例:
      import wandb
      import torch
      import torch.nn as nn
      import torch.optim as optim
      from torch.utils.data import DataLoader
      from torchvision import datasets, transforms
      
      # 初始化WandB项目
      wandb.init(project="your_project_name")
      
      # 加载数据集
      transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
      trainset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=True, transform=transform)
      trainloader = DataLoader(trainset, batch_size=64, shuffle=True)
      
      # 定义模型
      model = nn.Sequential(nn.Linear(784, 128),
                            nn.ReLU(),
                            nn.Linear(128, 64),
                            nn.ReLU(),
                            nn.Linear(64, 10),
                            nn.LogSoftmax(dim=1))
      criterion = nn.NLLLoss()
      optimizer = optim.SGD(model.parameters(), lr=0.003)
      
      # 训练模型
      epochs = 5
      for epoch in range(epochs):
          running_loss = 0
          for images, labels in trainloader:
              images = images.view(images.shape[0], -1)
              optimizer.zero_grad()
              output = model(images)
              loss = criterion(output, labels)
              loss.backward()
              optimizer.step()
              running_loss += loss.item()
          wandb.log({"loss": running_loss / len(trainloader)})
          print(f"Epoch {epoch+1}/{epochs}, Loss: {running_loss / len(trainloader)}")
      
      # 结束WandB实验
      wandb.finish()
      
      此代码展示了如何使用WandB记录训练过程中的损失。
  7. VisualDL(PaddlePaddle)

    • 代码案例:
      import paddle
      from paddle.vision.models import resnet18
      from paddle.vision.datasets import Cifar10
      from paddle.vision.transforms import Compose, Normalize
      from visualdl import LogWriter
      
      # 初始化VisualDL日志
      log_writer = LogWriter("log")
      
      # 加载数据集
      transform = Compose([Normalize(mean=[127.5, 127.5, 127.5], std=[127.5, 127.5, 127.5], data_format='HWC')])
      train_dataset = Cifar10(mode='train', transform=transform)
      train_loader = paddle.io.DataLoader(train_dataset, batch_size=64, shuffle=True)
      
      # 定义模型
      model = resnet18(pretrained=True)
      criterion = paddle.nn.CrossEntropyLoss()
      optimizer = paddle.optimizer.Adam(parameters=model.parameters(), learning_rate=0.001)
      
      # 训练模型
      epochs = 5
      for epoch in range(epochs):
          for i, (images, labels) in enumerate(train_loader()):
              output = model(images)
              loss = criterion(output, labels)
              loss.backward()
              optimizer.step()
              optimizer.clear_grad()
              log_writer.add_scalar(tag="loss", step=i + epoch * len(train_loader()), value=loss.numpy())
          print(f"Epoch {epoch+1}/{epochs}, Loss: {loss.numpy()[0]}")
      
      此代码展示了如何使用VisualDL记录PaddlePaddle模型训练过程中的损失。
  8. 高维特征降维可视化(PCA)

    • 代码案例:
      from sklearn.decomposition import PCA
      from sklearn.datasets import load_iris
      import matplotlib.pyplot as plt
      
      # 加载数据
      iris = load_iris()
      X = iris.data
      y = iris.target
      
      # PCA降维
      pca = PCA(n_components=2)
      X_pca = pca.fit_transform(X)
      
      # 可视化
      plt.figure(figsize=(8, 8))
      colors = ['red', 'green', 'blue']
      for i in range(len(colors)):
          plt.scatter(X_pca[y == i, 0], X_pca[y == i, 1], c=colors[i], label=iris.target_names[i])
      plt.legend()
      plt.show()
      
      此代码使用PCA将鸢尾花数据集从4维降至2维,并进行可视化。
  9. 特征图可视化(卷积层)

    • 代码案例:
      import torch
      import torch.nn as nn
      import torchvision.models as models
      import torchvision.transforms as transforms
      from PIL import Image
      import matplotlib.pyplot as plt
      
      # 加载预训练模型
      model = models.resnet18(pretrained=True)
      model.eval()
      
      # 加载图像
      img = Image.open('path_to_image.jpg')
      transform = transforms.Compose([
          transforms.Resize(256),
          transforms.CenterCrop(224),
          transforms.ToTensor(),
          transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
      ])
      img = transform(img).unsqueeze(0)
      
      # 获取特征图
      with torch.no_grad():
          features = model.conv1(img)
          features = features.squeeze(0)
      
      # 可视化特征图
      fig, ax = plt.subplots(1, 3, figsize=(15, 5))
      for i in range(3):
          ax[i].imshow(features[i].cpu().numpy(), cmap='viridis')
          ax[i].axis('off')
      plt.show()
      
      此代码展示了如何提取并可视化ResNet模型中的特征图。通过选择不同的卷积层(如model.layer1model.layer2等),可以进一步探索模型在不同层次提取的特征。
  10. Grad-CAM 可视化

    • 代码案例:
      import cv2
      import numpy as np
      import torch
      import torch.nn as nn
      import torchvision.models as models
      import torchvision.transforms as transforms
      from PIL import Image
      import matplotlib.pyplot as plt
      
      # 加载预训练模型
      model = models.resnet18(pretrained=True)
      model.eval()
      
      # 加载图像
      img = Image.open('path_to_image.jpg')
      transform = transforms.Compose([
          transforms.Resize(256),
          transforms.CenterCrop(224),
          transforms.ToTensor(),
          transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
      ])
      img_tensor = transform(img).unsqueeze(0)
      
      # 获取模型的最后一个卷积层的输出
      def get_last_conv_layer(model):
          for name, module in model.named_modules():
              if isinstance(module, nn.Conv2d):
                  last_conv_layer = module
          return last_conv_layer
      
      last_conv_layer = get_last_conv_layer(model)
      
      # 前向传播
      outputs = []
      def hook(module, input, output):
          outputs.append(output)
      handle = last_conv_layer.register_forward_hook(hook)
      
      with torch.no_grad():
          output = model(img_tensor)
          handle.remove()
      
      # 获取特征图和类别权重
      feature_map = outputs[0].squeeze(0).cpu().numpy()
      weights = model.fc.weight.data.cpu().numpy()
      
      # 计算 Grad-CAM
      class_idx = torch.argmax(output).item()
      cam = np.zeros(feature_map.shape[1:], dtype=np.float32)
      for i, w in enumerate(weights[class_idx]):
          cam += w * feature_map[i, :, :]
      
      cam = cv2.resize(cam, (224, 224))
      cam = np.maximum(cam, 0)
      cam = cam / np.max(cam)
      
      # 可视化
      img = cv2.imread('path_to_image.jpg')
      img = cv2.resize(img, (224, 224))
      heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET)
      heatmap = cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB)
      superimposed_img = heatmap * 0.4 + img * 0.6
      
      plt.imshow(superimposed_img.astype(np.uint8))
      plt.axis('off')
      plt.show()
      
      此代码通过 Grad-CAM 生成热力图,可视化模型对输入图像的注意力区域。
  11. 混淆矩阵和 ROC 曲线可视化

    • 代码案例:
      from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, roc_curve, auc
      import matplotlib.pyplot as plt
      
      # 假设 y_true 是真实标签,y_pred 是预测标签,y_scores 是预测概率
      y_true = [0, 1, 0, 1, 0, 1, 0, 1]
      y_pred = [0, 0, 0, 1, 0, 1, 1, 1]
      y_scores = [0.1, 0.4, 0.35, 0.8, 0.2, 0.7, 0.6, 0.9]
      
      # 绘制混淆矩阵
      cm = confusion_matrix(y_true, y_pred)
      disp = ConfusionMatrixDisplay(confusion_matrix=cm)
      disp.plot(cmap=plt.cm.Blues)
      plt.title('Confusion Matrix')
      plt.show()
      
      # 绘制 ROC 曲线
      fpr, tpr, _ = roc_curve(y_true, y_scores)
      roc_auc = auc(fpr, tpr)
      plt.plot(fpr, tpr, color='blue', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
      plt.plot([0, 1], [0, 1], color='gray', linestyle='--')
      plt.title('Receiver Operating Characteristic (ROC) Curve')
      plt.xlabel('False Positive Rate')
      plt.ylabel('True Positive Rate')
      plt.legend(loc='lower right')
      plt.show()
      
      此代码展示了如何绘制混淆矩阵和 ROC 曲线,用于评估分类模型的性能。

http://www.kler.cn/a/524360.html

相关文章:

  • concurrent.futures.Future对象详解:利用线程池与进程池实现异步操作
  • ios swift画中画技术尝试
  • 快速提升网站收录:内容创作的艺术
  • 【C++】特殊类设计、单例模式与类型转换
  • uniapp使用uni.navigateBack返回页面时携带参数到上个页面
  • 02.05、链表求和
  • 每日 Java 面试题分享【第 16 天】
  • 【初/高中生讲机器学习】0. 本专栏 “食用” 指南——写在一周年之际⭐
  • sem_init的概念和使用案例-简洁版
  • 信息学奥赛一本通 1342:【例4-1】最短路径问题
  • 本地项目上传到码云
  • 代码随想录算法训练营第三十八天-动态规划-完全背包-139.单词拆分
  • 【go语言】指针
  • 2025 = 1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 6^3 + 7^3 + 8^3 + 9^3
  • mac安装dockerdesktop优化
  • ECMAScript--promise的使用
  • AutoDL 云服务器:普通 用户 miniconda 配置
  • 二叉树介绍
  • Java多线程与高并发专题——JMM
  • 实验作业管理系统的设计与实现
  • Leetcode刷题-不定长滑动窗口
  • Vue 组件开发:构建高效可复用的前端界面要素
  • Spark Streaming的背压机制的原理与实现代码及分析
  • 力扣面试150 快乐数 循环链表找环 链表抽象 哈希
  • Java中实现ECDSA算法介绍、应用场景和示例代码
  • 机器人介绍