最近开始水一篇论文,好像还是第一次自己给论文做图,我把方法记录下来:
最省事的是直接看matplotlib官方的cheatsheet,这应该是最快的速成方法了吧:🔗Matplotlib cheatsheets and handouts
感觉很多第三方包用于论文、出版美化,试了好几个包我都不太喜欢,暂时没有发现特别好用的,感觉matplotlib本身是够用的。
想要图好看,可以参考其他漂亮论文(可以选一些大厂出品的论文,审美都在线,什么Google,Facebook之类)的配色,我就依葫芦画瓢选的配色,下面的几个图我都觉得挺好看的(出自论文Simple Copy-Paste is a Strong Data Augmentation Method for Instance Segmentation):
三个颜色的搭配:红绿蓝;圆、三角形和正方形 两个颜色的搭配:红蓝;两类:实线虚线;圆、正方形 红色和绿色搭配也很好看! 红、黄、蓝一样有眼前一亮的感觉 分析我们得到的数据类型,如果已经是csv, txt这样结构化的数据,直接用pandas做;json应该也很简单,不要绕远路!
下面是我这次做图的代码,我的数据是训练完后得到的result.csv
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 如果你已经明确了用哪些列做图,可以不用导入全部,用use_col指定特定的列就好
results = pd.read_csv("results/results.csv")
results.head()
epoch | train/box_loss | train/obj_loss | train/cls_loss | metrics/precision | metrics/recall | metrics/mAP_0.5 | metrics/mAP_0.5:0.95 | val/box_loss | val/obj_loss | val/cls_loss | x/lr0 | x/lr1 | x/lr2 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0.033319 | 0.025466 | 0 | 0.93297 | 0.81964 | 0.91421 | 0.63031 | 0.017351 | 0.012417 | 0 | 0.070291 | 0.003301 | 0.003301 |
1 | 1 | 0.027304 | 0.017404 | 0 | 0.89533 | 0.80561 | 0.90397 | 0.56975 | 0.019814 | 0.014309 | 0 | 0.040269 | 0.006612 | 0.006612 |
2 | 2 | 0.025737 | 0.017176 | 0 | 0.92179 | 0.80310 | 0.90728 | 0.61065 | 0.020163 | 0.013901 | 0 | 0.010225 | 0.009902 | 0.009902 |
3 | 3 | 0.026707 | 0.018136 | 0 | 0.64618 | 0.39679 | 0.47659 | 0.21491 | 0.031275 | 0.027192 | 0 | 0.009901 | 0.009901 | 0.009901 |
4 | 4 | 0.027679 | 0.016580 | 0 | 0.78914 | 0.67735 | 0.73899 | 0.42854 | 0.026546 | 0.018784 | 0 | 0.009901 | 0.009901 | 0.009901 |
# 这里的列名还是很烦的,确认一下列名到底是怎样的
results.columns
Index([' epoch', ' train/box_loss', ' train/obj_loss',
' train/cls_loss', ' metrics/precision', ' metrics/recall',
' metrics/mAP_0.5', 'metrics/mAP_0.5:0.95', ' val/box_loss',
' val/obj_loss', ' val/cls_loss', ' x/lr0',
' x/lr1', ' x/lr2'],
dtype='object')
# 把列名全改了!
results.rename(columns = {' epoch':'epoch', ' train/box_loss':'train_box_loss',
' train/obj_loss':'train_obj_loss', ' metrics/precision':'precision',
' metrics/recall':'recall', ' metrics/mAP_0.5':'mAP_0.5',
'metrics/mAP_0.5:0.95':'mAP_0.5:0.95',' val/box_loss':'val_box_loss',
' val/obj_loss':'val_obj_loss'}, inplace = True)
# 读入另外一个模型的数据,做比较
results_anno = pd.read_csv("results/results_anno.csv")
results_anno.rename(columns = {' epoch':'epoch', ' train/box_loss':'train_box_loss',
' train/obj_loss':'train_obj_loss', ' metrics/precision':'precision',
' metrics/recall':'recall', ' metrics/mAP_0.5':'mAP_0.5',
'metrics/mAP_0.5:0.95':'mAP_0.5:0.95',' val/box_loss':'val_box_loss',
' val/obj_loss':'val_obj_loss'}, inplace = True)
# 合并loss
results["loss"] = results["train_obj_loss"] + results["train_box_loss"]
results_anno["loss"] = results_anno["train_obj_loss"] + results_anno["train_box_loss"]
# plt.subplots()可以做到多个小图,不过这里不需要
fig, ax = plt.subplots()
# 'o-'确定了画的是实线,点是实心圆,markevery确定多少个epoch做一次圆圈标记,color可以自己去选,我在cheatsheet里找的
ax.plot(results['epoch'], results['loss'],"o-", color="C3", markevery=50,
label='With Copy-Paste')
# 's-'确定了画的是实线,点是方形
ax.plot(results_anno['epoch'], results_anno['loss'], 's-', color="C0", markevery=50,
label='Without Copy-Paste')
ax.set_xlabel('Epoch')
ax.set_ylabel('Loss')
# autoscale()可以自动确定画出的图x轴和y轴的取值范围
plt.autoscale()
ax.legend()
# grid就是画方格线
ax.grid()
plt.show()
# 放论文的图存到pdf文件,再将pdf插入latex里
fig.savefig("loss.pdf")
最后的效果: