results.txt中的文本数据是这样的.
{"epoch":0, "imgindex":0, "imgcount":63, "lr":0.0002 , "loss":3.2448 , "loss_classifier":2.5456 , "loss_box_reg":0.0000 , "loss_mask":0.0000 , "loss_objectness":0.6903 , "loss_rpn_box_reg":0.0089 , "time":31.9483, "data":12.9320, "GPUMemory":2177 }{"epoch":0, "imgindex":20, "imgcount":63, "lr":0.0034 , "loss":3.6288 , "loss_classifier":2.2065 , "loss_box_reg":0.0035 , "loss_mask":0.7142 , "loss_objectness":0.6809 , "loss_rpn_box_reg":0.0142 , "time":1.7869, "data":0.1222, "GPUMemory":3140 }{"epoch":0, "imgindex":40, "imgcount":63, "lr":0.0066 , "loss":1.0768 , "loss_classifier":0.0840 , "loss_box_reg":0.0093 , "loss_mask":0.7100 , "loss_objectness":0.1529 , "loss_rpn_box_reg":0.0186 , "time":0.7657, "data":0.1278, "GPUMemory":3140 }{"epoch":0, "imgindex":60, "imgcount":63, "lr":0.0098 , "loss":0.8712 , "loss_classifier":0.0616 , "loss_box_reg":0.0064 , "loss_mask":0.6750 , "loss_objectness":0.0762 , "loss_rpn_box_reg":0.0127 , "time":0.7116, "data":0.0756, "GPUMemory":3156 }{"epoch":0, "imgindex":62, "imgcount":63, "lr":0.0100 , "loss":0.8712 , "loss_classifier":0.0616 , "loss_box_reg":0.0069 , "loss_mask":0.6750 , "loss_objectness":0.0704 , "loss_rpn_box_reg":0.0112 , "time":0.7035, "data":0.0756, "GPUMemory":3156 }{"epoch":1, "imgindex":0, "imgcount":63, "lr":0.0100 , "loss":0.8573 , "loss_classifier":0.0519 , "loss_box_reg":0.0147 , "loss_mask":0.7056 , "loss_objectness":0.0710 , "loss_rpn_box_reg":0.0140 , "time":12.6762, "data":12.0246, "GPUMemory":3156 }{"epoch":1, "imgindex":20, "imgcount":63, "lr":0.0100 , "loss":0.8407 , "loss_classifier":0.0779 , "loss_box_reg":0.0193 , "loss_mask":0.6626 , "loss_objectness":0.0539 , "loss_rpn_box_reg":0.0151 , "time":0.7691, "data":0.1215, "GPUMemory":3156 }{"epoch":1, "imgindex":40, "imgcount":63, "lr":0.0100 , "loss":0.7553 , "loss_classifier":0.0395 , "loss_box_reg":0.0011 , "loss_mask":0.6084 , "loss_objectness":0.0866 , "loss_rpn_box_reg":0.0156 , "time":0.6852, "data":0.0516, "GPUMemory":3156 }{"epoch":1, "imgindex":60, "imgcount":63, "lr":0.0100 , "loss":0.8489 , "loss_classifier":0.0594 , "loss_box_reg":0.0133 , "loss_mask":0.6666 , "loss_objectness":0.0681 , "loss_rpn_box_reg":0.0146 , "time":0.8134, "data":0.1637, "GPUMemory":3156 }{"epoch":1, "imgindex":62, "imgcount":63, "lr":0.0100 , "loss":0.8576 , "loss_classifier":0.0601 , "loss_box_reg":0.0166 , "loss_mask":0.6531 , "loss_objectness":0.0699 , "loss_rpn_box_reg":0.0146 , "time":0.8066, "data":0.1636, "GPUMemory":3156 }{"epoch":2, "imgindex":0, "imgcount":63, "lr":0.0100 , "loss":0.1494 , "loss_classifier":0.0470 , "loss_box_reg":0.0000 , "loss_mask":0.0000 , "loss_objectness":0.0822 , "loss_rpn_box_reg":0.0202 , "time":13.3777, "data":12.7521, "GPUMemory":3156 }{"epoch":2, "imgindex":20, "imgcount":63, "lr":0.0100 , "loss":0.8790 , "loss_classifier":0.0629 , "loss_box_reg":0.0055 , "loss_mask":0.6817 , "loss_objectness":0.0797 , "loss_rpn_box_reg":0.0121 , "time":0.8539, "data":0.2024, "GPUMemory":3156 }{"epoch":2, "imgindex":40, "imgcount":63, "lr":0.0100 , "loss":0.7611 , "loss_classifier":0.0461 , "loss_box_reg":0.0101 , "loss_mask":0.6072 , "loss_objectness":0.0531 , "loss_rpn_box_reg":0.0119 , "time":0.6516, "data":0.0061, "GPUMemory":3156 }{"epoch":2, "imgindex":60, "imgcount":63, "lr":0.0100 , "loss":0.8804 , "loss_classifier":0.0712 , "loss_box_reg":0.0410 , "loss_mask":0.6410 , "loss_objectness":0.0652 , "loss_rpn_box_reg":0.0173 , "time":0.6745, "data":0.0346, "GPUMemory":3156 }{"epoch":2, "imgindex":62, "imgcount":63, "lr":0.0100 , "loss":0.8526 , "loss_classifier":0.0663 , "loss_box_reg":0.0350 , "loss_mask":0.6372 , "loss_objectness":0.0652 , "loss_rpn_box_reg":0.0170 , "time":0.6737, "data":0.0334, "GPUMemory":3156 }{"epoch":3, "imgindex":0, "imgcount":63, "lr":0.0100 , "loss":1.0212 , "loss_classifier":0.1881 , "loss_box_reg":0.0681 , "loss_mask":0.6798 , "loss_objectness":0.0648 , "loss_rpn_box_reg":0.0204 , "time":12.8021, "data":12.1545, "GPUMemory":3156 }{"epoch":3, "imgindex":20, "imgcount":63, "lr":0.0100 , "loss":0.8134 , "loss_classifier":0.0495 , "loss_box_reg":0.0202 , "loss_mask":0.6327 , "loss_objectness":0.0553 , "loss_rpn_box_reg":0.0143 , "time":0.7610, "data":0.1089, "GPUMemory":3156 }{"epoch":3, "imgindex":40, "imgcount":63, "lr":0.0100 , "loss":0.8319 , "loss_classifier":0.0645 , "loss_box_reg":0.0041 , "loss_mask":0.6497 , "loss_objectness":0.0677 , "loss_rpn_box_reg":0.0143 , "time":0.7960, "data":0.1426, "GPUMemory":3156 }{"epoch":3, "imgindex":60, "imgcount":63, "lr":0.0100 , "loss":0.8450 , "loss_classifier":0.0851 , "loss_box_reg":0.0353 , "loss_mask":0.6324 , "loss_objectness":0.0646 , "loss_rpn_box_reg":0.0112 , "time":0.6900, "data":0.0534, "GPUMemory":3156 }{"epoch":3, "imgindex":62, "imgcount":63, "lr":0.0100 , "loss":0.8455 , "loss_classifier":0.0882 , "loss_box_reg":0.0360 , "loss_mask":0.6351 , "loss_objectness":0.0534 , "loss_rpn_box_reg":0.0105 , "time":0.6847, "data":0.0535, "GPUMemory":3156 }{"epoch":4, "imgindex":0, "imgcount":63, "lr":0.0100 , "loss":0.1181 , "loss_classifier":0.0425 , "loss_box_reg":0.0000 , "loss_mask":0.0000 , "loss_objectness":0.0597 , "loss_rpn_box_reg":0.0159 , "time":12.8091, "data":12.1755, "GPUMemory":3156 }{"epoch":4, "imgindex":20, "imgcount":63, "lr":0.0100 , "loss":0.8551 , "loss_classifier":0.0512 , "loss_box_reg":0.0236 , "loss_mask":0.5953 , "loss_objectness":0.0606 , "loss_rpn_box_reg":0.0136 , "time":0.8668, "data":0.2161, "GPUMemory":3156 }{"epoch":4, "imgindex":40, "imgcount":63, "lr":0.0100 , "loss":0.8365 , "loss_classifier":0.0867 , "loss_box_reg":0.0317 , "loss_mask":0.6267 , "loss_objectness":0.0467 , "loss_rpn_box_reg":0.0139 , "time":0.7096, "data":0.0670, "GPUMemory":3156 }{"epoch":4, "imgindex":60, "imgcount":63, "lr":0.0100 , "loss":0.8199 , "loss_classifier":0.1017 , "loss_box_reg":0.0467 , "loss_mask":0.5918 , "loss_objectness":0.0750 , "loss_rpn_box_reg":0.0159 , "time":0.7013, "data":0.0584, "GPUMemory":3156 }{"epoch":4, "imgindex":62, "imgcount":63, "lr":0.0100 , "loss":0.9040 , "loss_classifier":0.1018 , "loss_box_reg":0.0560 , "loss_mask":0.6074 , "loss_objectness":0.0750 , "loss_rpn_box_reg":0.0159 , "time":0.6953, "data":0.0575, "GPUMemory":3156 }{"epoch":5, "imgindex":0, "imgcount":63, "lr":0.0100 , "loss":0.0743 , "loss_classifier":0.0040 , "loss_box_reg":0.0000 , "loss_mask":0.0000 , "loss_objectness":0.0536 , "loss_rpn_box_reg":0.0168 , "time":12.8451, "data":12.2135, "GPUMemory":3156 }{"epoch":5, "imgindex":20, "imgcount":63, "lr":0.0100 , "loss":0.7978 , "loss_classifier":0.0431 , "loss_box_reg":0.0129 , "loss_mask":0.6468 , "loss_objectness":0.0534 , "loss_rpn_box_reg":0.0108 , "time":0.7597, "data":0.1117, "GPUMemory":3156 }{"epoch":5, "imgindex":40, "imgcount":63, "lr":0.0100 , "loss":0.8960 , "loss_classifier":0.0784 , "loss_box_reg":0.0326 , "loss_mask":0.6608 , "loss_objectness":0.0534 , "loss_rpn_box_reg":0.0162 , "time":0.7539, "data":0.1020, "GPUMemory":3156 }{"epoch":5, "imgindex":60, "imgcount":63, "lr":0.0100 , "loss":0.8618 , "loss_classifier":0.0936 , "loss_box_reg":0.0499 , "loss_mask":0.6044 , "loss_objectness":0.0594 , "loss_rpn_box_reg":0.0152 , "time":0.7365, "data":0.0846, "GPUMemory":3156 }{"epoch":5, "imgindex":62, "imgcount":63, "lr":0.0100 , "loss":0.9147 , "loss_classifier":0.0936 , "loss_box_reg":0.0499 , "loss_mask":0.6383 , "loss_objectness":0.0447 , "loss_rpn_box_reg":0.0152 , "time":0.6980, "data":0.0501, "GPUMemory":3156 }{"epoch":6, "imgindex":0, "imgcount":63, "lr":0.0100 , "loss":1.5819 , "loss_classifier":0.2886 , "loss_box_reg":0. , "loss_mask":0.9627 , "loss_objectness":0.1051 , "loss_rpn_box_reg":0.0240 , "time":13.2378, "data":12.5113, "GPUMemory":3156 }{"epoch":6, "imgindex":20, "imgcount":63, "lr":0.0100 , "loss":0.8689 , "loss_classifier":0.0960 , "loss_box_reg":0.0419 , "loss_mask":0.6413 , "loss_objectness":0.0611 , "loss_rpn_box_reg":0.0136 , "time":0.7089, "data":0.0535, "GPUMemory":3156 }{"epoch":6, "imgindex":40, "imgcount":63, "lr":0.0100 , "loss":0.7758 , "loss_classifier":0.0517 , "loss_box_reg":0.0251 , "loss_mask":0.5768 , "loss_objectness":0.0629 , "loss_rpn_box_reg":0.0119 , "time":0.9487, "data":0.2802, "GPUMemory":3156 }{"epoch":6, "imgindex":60, "imgcount":63, "lr":0.0100 , "loss":0.8633 , "loss_classifier":0.0808 , "loss_box_reg":0.0355 , "loss_mask":0.6135 , "loss_objectness":0.0591 , "loss_rpn_box_reg":0.0149 , "time":0.6702, "data":0.0343, "GPUMemory":3156 }{"epoch":6, "imgindex":62, "imgcount":63, "lr":0.0100 , "loss":0.8633 , "loss_classifier":0.0808 , "loss_box_reg":0.0355 , "loss_mask":0.6135 , "loss_objectness":0.0591 , "loss_rpn_box_reg":0.0149 , "time":0.6635, "data":0.0342, "GPUMemory":3156 }{"epoch":7, "imgindex":0, "imgcount":63, "lr":0.0100 , "loss":0.2361 , "loss_classifier":0.0167 , "loss_box_reg":0.0002 , "loss_mask":0.1616 , "loss_objectness":0.0511 , "loss_rpn_box_reg":0.0065 , "time":12.7431, "data":12.1155, "GPUMemory":3156 }{"epoch":7, "imgindex":20, "imgcount":63, "lr":0.0100 , "loss":0.8250 , "loss_classifier":0.0889 , "loss_box_reg":0.0388 , "loss_mask":0.6147 , "loss_objectness":0.0535 , "loss_rpn_box_reg":0.0140 , "time":0.8481, "data":0.2082, "GPUMemory":3156 }{"epoch":7, "imgindex":40, "imgcount":63, "lr":0.0100 , "loss":0.8195 , "loss_classifier":0.0845 , "loss_box_reg":0.0551 , "loss_mask":0.5938 , "loss_objectness":0.0565 , "loss_rpn_box_reg":0.0155 , "time":0.8044, "data":0.1416, "GPUMemory":3156 }{"epoch":7, "imgindex":60, "imgcount":63, "lr":0.0100 , "loss":0.7695 , "loss_classifier":0.0654 , "loss_box_reg":0.0317 , "loss_mask":0.5683 , "loss_objectness":0.0475 , "loss_rpn_box_reg":0.0105 , "time":0.7677, "data":0.1099, "GPUMemory":3156 }{"epoch":7, "imgindex":62, "imgcount":63, "lr":0.0100 , "loss":0.7389 , "loss_classifier":0.0654 , "loss_box_reg":0.0317 , "loss_mask":0.5634 , "loss_objectness":0.0472 , "loss_rpn_box_reg":0.0127 , "time":0.7940, "data":0.1415, "GPUMemory":3156 }{"epoch":8, "imgindex":0, "imgcount":63, "lr":0.0100 , "loss":0.5342 , "loss_classifier":0.0315 , "loss_box_reg":0.0125 , "loss_mask":0.4456 , "loss_objectness":0.0313 , "loss_rpn_box_reg":0.0133 , "time":13.4497, "data":12.7742, "GPUMemory":3156 }
刚开始是这样的.代码
# 读取文本文件, # 1. 自动去除utf8 BOM 头# 2. 统一 \r\n 转成 \ndef ReadAllTextFromFile(str,encoding='utf-8'):try: with open(str,'r+',encoding=encoding) as f:s = f.read().replace('\r\n','\n')if ord(s[0]) == 65279:# 65279 对应的是 \xfeff 是BOM头.所以去掉return s[1:]if ord(s[0]) == 65534:# 65534 对应的是 \xfffe 是BOM头.所以去掉return s[1:]if ord(s[0]) == 4294836224:# 4294836224 对应的是 \xfffe0000 是BOM头.所以去掉return s[1:]return sexcept Exception as ex:print(ex)return ""# 取得所有的行数, splitlines 不带换行符 def ReadAllLinesFromFile(str,encoding='utf-8'):txt = ReadAllTextFromFile(str,encoding)return txt.splitlines()# 根据results.txt 绘制关键参数results.png图, def plot_resultsimage(save_dir=''): # from utils.general import *; plot_results()# Plot training 'results*.txt' as seen in /ultralytics/yolov5#reproduce-our-trainingfig, ax = plt.subplots(2, 4, figsize=(12, 6))ax = ax.ravel()labels = ['lr','loss','loss_classifier','loss_box_reg','loss_mask','loss_objectness','loss_rpn_box_reg']files = glob.glob(str(Path(save_dir) / 'results*.txt'))for fi, f in enumerate(files):# try:# results = np.loadtxt(f, usecols=[2, 3, 4, 8, 9, 12, 13, 14, 10, 11], ndmin=2).Tlines = ReadAllLinesFromFile(f)results = []for txtrow in lines:results.append(json.loads(txtrow))for colindex, label in enumerate(labels): for rowindex, row in enumerate(results):ax[colindex].plot(rowindex, row[label], linestyle='--', marker='.', label=label, linewidth=2, markersize=2)ax[colindex].set_title(label)# except Exception as e:#print(str(e))# fig.tight_layout()# ax[1].legend()fig.savefig(Path(save_dir) / 'results.png', dpi=200)# fig.show()pass
得到结果是这样的.没有折线.百思不得其解
而且看上去好像是不对的.后来仔细的看了很多次, 改成下面的代码就可以了
正确的绘制折线图的方法
import matplotlibimport matplotlib.pyplot as pltimport globfrom pathlib import Pathimport numpy as npimport json# 读取文本文件, # 1. 自动去除utf8 BOM 头# 2. 统一 \r\n 转成 \ndef ReadAllTextFromFile(str,encoding='utf-8'):try: with open(str,'r+',encoding=encoding) as f:s = f.read().replace('\r\n','\n')if ord(s[0]) == 65279:# 65279 对应的是 \xfeff 是BOM头.所以去掉return s[1:]if ord(s[0]) == 65534:# 65534 对应的是 \xfffe 是BOM头.所以去掉return s[1:]if ord(s[0]) == 4294836224:# 4294836224 对应的是 \xfffe0000 是BOM头.所以去掉return s[1:]return sexcept Exception as ex:print(ex)return ""# 取得所有的行数, splitlines 不带换行符 def ReadAllLinesFromFile(str,encoding='utf-8'):txt = ReadAllTextFromFile(str,encoding)return txt.splitlines()def plot_resultsimage(save_dir=''): # from utils.general import *; plot_results()# Plot training 'results*.txt' as seen in /ultralytics/yolov5#reproduce-our-trainingfig, ax = plt.subplots(2, 4, figsize=(12, 6))ax = ax.ravel()labels = ['lr','loss','loss_classifier','loss_box_reg','loss_mask','loss_objectness','loss_rpn_box_reg']files = glob.glob(str(Path(save_dir) / 'results*.txt'))for fi, f in enumerate(files):# try:# results = np.loadtxt(f, usecols=[2, 3, 4, 8, 9, 12, 13, 14, 10, 11], ndmin=2).Tlines = ReadAllLinesFromFile(f)results = []for txtrow in lines:results.append(json.loads(txtrow)) for colindex, label in enumerate(labels): # for rowindex, row in enumerate(results):coldatas = [row[label] for row in results] ax[colindex].plot(coldatas, marker='.', label=label, linewidth=1, markersize=3)ax[colindex].set_title(label)# except Exception as e:#print(str(e))# fig.tight_layout()# ax[1].legend()fig.savefig(Path(save_dir) / 'results.png', dpi=200)# fig.show()pass
这样的结果就正常了.
第一种代码在输出的时候, 依次循环每个坐标图, 每次画一个点,导致它无法知道上一个点的位置, 所以也就无法连续性的绘制折线,
只要一次性给每个子图投入所有的数据点即可绘制出折线, 而且第二次再调用的时候, 线的颜色会自动变化.
主要的代码变化在 coldatas = [row[label] for row in results] 这一行.
会猜真的是太重要了…