mmclassification中test.py指标打印到excel
import xlwings as xw
import math
import matplotlib.pyplot as plt
#matplotlib画图中中文显示会有问题,需要这两行设置默认字体
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
1,图片文件名写入excel
G:\mmlab_code_20221103\mmclassification-master\mmcls\datasets\my_filelist.py
with open(self.ann_file) as f:
samples = [x.strip().split(' ') for x in f.readlines()]
p = 0
for filename, gt_label in samples:
info = {'img_prefix': self.data_prefix}
info['img_info'] = {'filename': filename}
# qcy20221201
wb = xw.Book('G:\mmlab_code_20221103\mmclassification-master\mmcls\datasets\melon_class_result.xlsx')
sht = wb.sheets[0]
sht.range('A1').value = 'filename' # 向A1单元格写入该行数据lable
# sht.range('A2').options(transpose=True).value = samples[0][0] # 从A2单元格开始存入gt_labels第0列
sht.range(p + 2, 1).options(transpose=True).value = samples[p][0]
p += 1
2,分类标签,预测为两个类别的概率值写入excel
G:\mmlab_code_20221103\mmclassification-master\mmcls\datasets\base_dataset.py
else:
eval_results.update(
{k: v.item()
for k, v in eval_results_.items()})
# qcy20221201
app = xw.App(visible=False, add_book=False)
wb = xw.Book('G:\mmlab_code_20221103\mmclassification-master\mmcls\datasets\melon_class_result.xlsx')
sht = wb.sheets[0]
sht.range('B1').value = 'gt_label' # 向B1单元格写入该行数据lable
sht.range('B2').options(transpose=True).value = gt_labels # 从A2单元格开始存入gt_labels第0列
sht.range('D1').value = 'class_1' # 向D1单元格写入该行数据lable
sht.range('D2').options(transpose=True).value = results[:,0] # 从D2单元格开始存入results第0列
sht.range('E1').value = 'class_2' # 向C1单元格写入该行数据lable
sht.range('E2').options(transpose=True).value = results[:,1] # 从E2单元格开始存入results第1列
3,预测标签写入excel
G:\mmlab_code_20221103\mmclassification-master\mmcls\core\evaluation\eval_metrics.py
num_classes = pred.size(1)
pred_score, pred_label = torch.topk(pred, k=1)
pred_score = pred_score.flatten()
pred_label = pred_label.flatten()
# qcy20221201
wb = xw.Book('G:\mmlab_code_20221103\mmclassification-master\mmcls\datasets\melon_class_result.xlsx')
sht = wb.sheets[0]
sht.range('C1').value = 'pred_label1' # 向C1单元格写入该行数据lable
sht.range('C2').options(transpose=True).value = pred_label1 # 从C2单元格开始存入gt_labels第0列