MMSegmentation训练日志可视化全攻略:从JSON解析到Matplotlib图表生成(附完整代码)

张开发
2026/4/6 9:28:49 15 分钟阅读

分享文章

MMSegmentation训练日志可视化全攻略:从JSON解析到Matplotlib图表生成(附完整代码)
MMSegmentation训练日志可视化全攻略从JSON解析到Matplotlib图表生成附完整代码在计算机视觉领域语义分割模型的训练过程监控至关重要。MMSegmentation作为开源的语义分割工具箱虽然功能强大但其训练日志的分析与可视化却常让研究者感到棘手。本文将带您深入探索如何从原始JSON日志中提取关键指标通过Pandas进行高效数据处理并最终生成专业级的Matplotlib可视化图表。1. 日志解析基础理解MMSegmentation输出结构MMSegmentation默认会在work_dirs目录下生成两种关键日志文件scalars.json和.log文件。前者包含结构化指标数据后者记录详细训练过程。我们需要重点关注scalars.json文件其内部采用每行一个JSON对象的形式存储训练指标。典型的scalars.json数据结构如下{mode: train, iter: 500, lr: 0.01, memory: 3241, data_time: 0.008, decode.loss_ce: 1.234} {mode: val, iter: 500, aAcc: 0.876, mIoU: 0.756, Acc: 0.892}关键解析步骤逐行读取JSON文件区分训练集和验证集指标转换时间戳格式如存在处理嵌套指标如decode.loss_ce提示使用Python的json模块时建议先用eval()处理每行文本比json.loads()更高效2. 数据预处理Pandas高效处理技巧将JSON数据转换为Pandas DataFrame是后续分析的基础。以下是优化的数据处理流程import pandas as pd import json def load_logs(json_path): with open(json_path) as f: lines [eval(line) for line in f if line.strip()] df pd.DataFrame(lines) df[time] pd.to_datetime(df.get(time, ), errorscoerce) return df # 分离训练和验证数据 df load_logs(work_dirs/your_exp/scalars.json) train_df df[df[mode] train].copy() val_df df[df[mode] val].copy()常见数据处理场景场景处理方法示例代码缺失值处理前向填充df.ffill(inplaceTrue)异常值检测3σ原则df[(df[loss] df[loss].mean()3*df[loss].std())]指标计算滑动平均df[loss_smooth] df[loss].rolling(100).mean()时间转换相对时间df[elapsed] (df[time]-df[time].min()).dt.total_seconds()3. 可视化核心指标Matplotlib高级技巧3.1 损失函数可视化import matplotlib.pyplot as plt plt.style.use(seaborn) def plot_loss(train_df, metrics[loss, decode.loss_ce]): fig, ax plt.subplots(figsize(12, 6)) for metric in metrics: if metric in train_df: ax.plot(train_df[iter], train_df[metric], labelmetric.replace(., ).title()) ax.set_xlabel(Iteration) ax.set_ylabel(Loss Value) ax.set_title(Training Loss Curves) ax.legend() ax.grid(True) return fig样式优化技巧使用plt.style.available查看所有预设样式通过rcParams定制全局样式plt.rcParams.update({ font.size: 12, axes.titlesize: 14, lines.linewidth: 2 })3.2 验证指标对比def plot_metrics(val_df, metrics[aAcc, mIoU, mDice]): fig, ax plt.subplots(figsize(12, 6)) for metric in metrics: if metric in val_df: ax.plot(val_df[iter], val_df[metric]*100, markero, markersize4, labelmetric) ax.set_ylim(0, 100) ax.set_xlabel(Iteration) ax.set_ylabel(Score (%)) ax.set_title(Validation Metrics) ax.legend(loclower right) return fig4. 高级可视化多视图与类别分析4.1 创建多面板视图def multi_panel_plot(train_df, val_df): fig, (ax1, ax2) plt.subplots(2, 1, figsize(12, 10), sharexTrue) # 损失函数面板 ax1.plot(train_df[iter], train_df[loss], labelTotal Loss) ax1.set_ylabel(Loss) ax1.legend() # 验证指标面板 ax2.plot(val_df[iter], val_df[mIoU]*100, labelmIoU) ax2.set_xlabel(Iteration) ax2.set_ylabel(Score (%)) ax2.legend() plt.tight_layout() return fig4.2 类别级指标分析对于语义分割任务不同类别的表现差异往往包含重要信息def plot_class_iou(log_path, class_names): class_metrics {name: [] for name in class_names} # 解析.log文件获取类别指标 with open(log_path) as f: for line in f: if IoU in line: for name in class_names: if name in line: iou float(line.split(|)[1].strip()) class_metrics[name].append(iou) # 绘制类别指标 fig, ax plt.subplots(figsize(12, 6)) for name, values in class_metrics.items(): ax.plot(range(len(values)), values, labelname) ax.set_xlabel(Epoch) ax.set_ylabel(IoU) ax.legend(bbox_to_anchor(1.05, 1)) return fig5. 实战完整可视化流程代码以下是将所有组件整合的完整示例import pandas as pd import matplotlib.pyplot as plt from pathlib import Path class LogVisualizer: def __init__(self, exp_dir): self.exp_dir Path(exp_dir) self.train_df None self.val_df None def load_data(self): json_path self.exp_dir / vis_data / scalars.json df pd.DataFrame([eval(line) for line in open(json_path) if line.strip()]) self.train_df df[df[mode] train].copy() self.val_df df[df[mode] val].copy() # 数据清洗 self.train_df[iter] pd.to_numeric(self.train_df[iter]) self.val_df[iter] pd.to_numeric(self.val_df[iter]) def plot_training_curves(self, save_pathNone): fig, (ax1, ax2) plt.subplots(2, 1, figsize(12, 10)) # 损失曲线 loss_cols [c for c in self.train_df if loss in c] for col in loss_cols: ax1.plot(self.train_df[iter], self.train_df[col], labelcol) ax1.set_title(Training Loss) ax1.legend() # 学习率曲线 if lr in self.train_df: ax2.plot(self.train_df[iter], self.train_df[lr]) ax2.set_title(Learning Rate Schedule) plt.tight_layout() if save_path: fig.savefig(save_path, dpi300, bbox_inchestight) return fig def plot_validation_metrics(self, save_pathNone): fig, ax plt.subplots(figsize(12, 6)) metrics [aAcc, mIoU, mDice, mFscore] for metric in metrics: if metric in self.val_df: ax.plot(self.val_df[iter], self.val_df[metric]*100, markero, markersize3, labelmetric) ax.set_title(Validation Metrics) ax.set_ylim(0, 100) ax.legend() if save_path: fig.savefig(save_path, dpi300, bbox_inchestight) return fig # 使用示例 if __name__ __main__: visualizer LogVisualizer(work_dirs/your_experiment) visualizer.load_data() visualizer.plot_training_curves(training.png) visualizer.plot_validation_metrics(validation.png)6. 性能优化与实用技巧大数据量处理方案分块读取对于超大日志文件chunks pd.read_json(scalars.json, linesTrue, chunksize10000) df pd.concat(chunks)采样策略当数据点过多时plot_df train_df.iloc[::10] # 每10个点取一个交互式可视化使用Plotly替代Matplotlibimport plotly.express as px fig px.line(train_df, xiter, yloss, titleTraining Loss) fig.show()常见问题解决方案中文显示问题plt.rcParams[font.sans-serif] [SimHei] plt.rcParams[axes.unicode_minus] False图表模糊问题plt.savefig(output.png, dpi300, bbox_inchestight)内存不足问题del train_df, val_df # 及时释放内存 gc.collect()

更多文章