-
TensorBoard
- 代码案例:
此代码展示了如何使用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()
- 代码案例:
-
t-SNE降维可视化
- 代码案例:
此代码使用t-SNE将鸢尾花数据集从4维降至2维,并进行可视化。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()
- 代码案例:
-
特征图可视化
- 代码案例:
此代码展示了如何提取并可视化ResNet模型中的特征图。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()
- 代码案例:
-
混淆矩阵和ROC曲线可视化
- 代码案例:
此代码展示了如何绘制混淆矩阵和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()
- 代码案例:
-
Neptune团队协作
- 代码案例:
此代码展示了如何使用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()
- 代码案例:
-
WandB
- 代码案例:
此代码展示了如何使用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 = 0for 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()
- 代码案例:
-
VisualDL(PaddlePaddle)
- 代码案例:
此代码展示了如何使用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]}")
- 代码案例:
-
高维特征降维可视化(PCA)
- 代码案例:
此代码使用PCA将鸢尾花数据集从4维降至2维,并进行可视化。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()
- 代码案例:
-
特征图可视化(卷积层)
- 代码案例:
此代码展示了如何提取并可视化ResNet模型中的特征图。通过选择不同的卷积层(如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()
model.layer1
、model.layer2
等),可以进一步探索模型在不同层次提取的特征。
- 代码案例:
-
Grad-CAM 可视化
- 代码案例:
此代码通过 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 = modulereturn last_conv_layerlast_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.6plt.imshow(superimposed_img.astype(np.uint8)) plt.axis('off') plt.show()
- 代码案例:
-
混淆矩阵和 ROC 曲线可视化
- 代码案例:
此代码展示了如何绘制混淆矩阵和 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()
- 代码案例: