diff --git a/examples/classifier_compression/compress_classifier.py b/examples/classifier_compression/compress_classifier.py index d56c50f94c5de05bb30dbba367d5d1d2d35f65ee..62f1af9e09e1faeb934e44ddf9ff59acfbc4249a 100755 --- a/examples/classifier_compression/compress_classifier.py +++ b/examples/classifier_compression/compress_classifier.py @@ -57,6 +57,7 @@ import os import sys import random import traceback +import logging from collections import OrderedDict, defaultdict from functools import partial import numpy as np @@ -790,7 +791,14 @@ if __name__ == '__main__': print("\n-- KeyboardInterrupt --") except Exception as e: if msglogger is not None: + # We catch unhandled exceptions here in order to log them to the log file + # However, using the msglogger as-is to do that means we get the trace twice in stdout - once from the + # logging operation and once from re-raising the exception. So we remove the stdout logging handler + # before logging the exception + handlers_bak = msglogger.handlers + msglogger.handlers = [h for h in msglogger.handlers if type(h) != logging.StreamHandler] msglogger.error(traceback.format_exc()) + msglogger.handlers = handlers_bak raise finally: if msglogger is not None: