这段代码的目的是读取 JSON 文件,提取关键信息,然后使用 Matplotlib 绘制四个子图,分别显示不同的指标随着 iter 变化的情况。这种图形化分析有助于直观地了解模型的性能。
画图结果如下:
json文件格式如下:下面只粘贴出来前6行数据
{"mode": "train", "epoch": 1, "iter": 50, "lr": 0.0, "memory": 19479, "data_time": 0.00483, "decode.loss_ce": 4.60138, "decode.acc_seg": 0.23948, "aux_identity.loss_ce": 4.31477, "aux_identity.acc_seg": 0.14401, "loss": 8.91615, "time": 0.65278}
{"mode": "train", "epoch": 1, "iter": 100, "lr": 1e-05, "memory": 19479, "data_time": 0.00338, "decode.loss_ce": 4.43467, "decode.acc_seg": 0.62701, "aux_identity.loss_ce": 3.0979, "aux_identity.acc_seg": 0.85732, "loss": 7.53257, "time": 0.60686}
{"mode": "train", "epoch": 1, "iter": 150, "lr": 1e-05, "memory": 19479, "data_time": 0.00342, "decode.loss_ce": 4.10557, "decode.acc_seg": 2.23878, "aux_identity.loss_ce": 2.13645, "aux_identity.acc_seg": 7.20875, "loss": 6.24202, "time": 0.60892}
{"mode": "train", "epoch": 1, "iter": 200, "lr": 1e-05, "memory": 19479, "data_time": 0.00344, "decode.loss_ce": 3.77826, "decode.acc_seg": 8.71241, "aux_identity.loss_ce": 1.75049, "aux_identity.acc_seg": 13.31084, "loss": 5.52875, "time": 0.60606}
{"mode": "train", "epoch": 1, "iter": 250, "lr": 2e-05, "memory": 19479, "data_time": 0.00344, "decode.loss_ce": 3.17202, "decode.acc_seg": 19.45514, "aux_identity.loss_ce": 1.451, "aux_identity.acc_seg": 16.99208, "loss": 4.62302, "time": 0.59555}
{"mode": "train", "epoch": 1, "iter": 300, "lr": 2e-05, "memory": 19479, "data_time": 0.00348, "decode.loss_ce": 2.56726, "decode.acc_seg": 29.93745, "aux_identity.loss_ce": 1.16545, "aux_identity.acc_seg": 23.99233, "loss": 3.73271, "time": 0.59519}
具体绘制代码如下:
import json
import matplotlib.pyplot as plt
import osprint(os.path.abspath(__file__))#打印当前路径# 存储读取的数据,创建一个字典 data 用于存储 JSON 文件中的数据,每个键对应一个需要提取的数据列。
data = {"mode": [], "epoch": [], "iter": [], "lr": [], "memory": [],"data_time": [], "decode.loss_ce": [], "decode.acc_seg": [],"aux_identity.loss_ce": [], "aux_identity.acc_seg": [],"loss": [], "time": []}# 读取JSON文件
json_file_path = '/home/lsj/code/model/ZegCLIP/draw/20240120_151005.log.json' # 请替换成实际的JSON文件路径
print("json_file_path:",json_file_path)with open(json_file_path, 'r') as json_file:for line in json_file:#遍历 JSON 文件的每一行。json_data = json.loads(line)#解析 JSON 行并将其转换为 Python 字典。if json_data["mode"]=="val":continue# 提取所需的键值for key in data.keys():#遍历 data 字典的键,将每个键对应的值添加到 data 字典中。# print("key:",key)# print("data[key]:",data[key])# print("json_data[key]:",json_data[key])data[key].append(json_data[key])# 绘制图形
fig, axs = plt.subplots(2, 2, figsize=(12, 8))#创建一个包含 2x2 子图的 Matplotlib 图形。#在第一个子图中绘制 decode_loss_ace 随着 iter 的变化图,并设置标签。
axs[0, 0].plot(data['iter'], data['decode.loss_ce'], label='decode.loss_ce')
axs[0, 0].set_title('Decode Loss (ACE)')
axs[0, 0].set_xlabel('iter')
axs[0, 0].set_ylabel('Loss')
axs[0, 0].legend()#在第一个子图中绘制 decode_acc_seg 随着 iter 的变化图,并设置标签。
axs[0, 1].plot(data['iter'], data['decode.acc_seg'], label='decode.acc_seg')
axs[0, 1].set_title('Decode Accuracy (Seg)')
axs[0, 1].set_xlabel('iter')
axs[0, 1].set_ylabel('Accuracy')
axs[0, 1].legend()#在第一个子图中绘制 aux_identity_loss_ce 随着 iter 的变化图,并设置标签。
axs[1, 0].plot(data['iter'], data['aux_identity.loss_ce'], label='aux_identity.loss_ce')
axs[1, 0].set_title('Aux Identity Loss (CE)')
axs[1, 0].set_xlabel('iter')
axs[1, 0].set_ylabel('Loss')
axs[1, 0].legend()#在第一个子图中绘制 aux_identity.acc_seg 随着 iter 的变化图,并设置标签。
axs[1, 1].plot(data['iter'], data['aux_identity.acc_seg'], label='aux_identity.acc_seg')
axs[1, 1].set_title('Aux Identity Accuracy (Seg)')
#axs[1, 1].set_xlabel('Epoch')
axs[1, 1].set_xlabel('iter')
axs[1, 1].set_ylabel('Accuracy')
axs[1, 1].legend()plt.tight_layout()#调整子图的布局,以确保它们不会重叠
plt.show()#展示 Matplotlib 图形