Skip to content
12 changes: 12 additions & 0 deletions val.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import argparse
import json
import logging
import os
import sys
from pathlib import Path
Expand All @@ -33,6 +34,8 @@
from utils.plots import output_to_target, plot_images, plot_val_study
from utils.torch_utils import select_device, time_sync

LOGGER.addHandler(logging.FileHandler('log.txt'))


def save_one_txt(predn, save_conf, shape, file):
# Save one txt result
Expand Down Expand Up @@ -93,6 +96,7 @@ def run(data,
augment=False, # augmented inference
verbose=False, # verbose output
save_txt=False, # save results to *.txt
save_metrics=False, # save metrics to *.txt
save_hybrid=False, # save label+prediction hybrid results to *.txt
save_conf=False, # save confidences in --save-txt labels
save_json=False, # save a COCO-JSON results file
Expand Down Expand Up @@ -255,6 +259,13 @@ def run(data,
shape = (batch_size, 3, imgsz, imgsz)
LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {shape}' % t)

# save the metrics
if save_metrics:
with open(save_dir / 'metrics.txt', 'a') as f:
f.write(s + '\n'
+ pf % ('all', seen, nt.sum(), mp, mr, map50, map) + '\n'
+ f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {shape}' % t)

# Plots
if plots:
confusion_matrix.plot(save_dir=save_dir, names=list(names.values()))
Expand Down Expand Up @@ -314,6 +325,7 @@ def parse_opt():
parser.add_argument('--save-hybrid', action='store_true', help='save label+prediction hybrid results to *.txt')
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
parser.add_argument('--save-json', action='store_true', help='save a COCO-JSON results file')
parser.add_argument('--save-metrics', action='store_true', help='save metrics to *.txt')
parser.add_argument('--project', default=ROOT / 'runs/val', help='save to project/name')
parser.add_argument('--name', default='exp', help='save to project/name')
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
Expand Down