forked from luuuyi/CBAM.PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
37 lines (34 loc) · 1.02 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import traceback
class Logger(object):
'''Save training process to log file with simple plot function.'''
def __init__(self, fpath,resume=False):
self.file = None
self.resume = resume
if os.path.isfile(fpath):
if resume:
self.file = open(fpath, 'a')
else:
self.file = open(fpath, 'w')
else:
self.file = open(fpath, 'w')
def append(self, target_str):
if not isinstance(target_str, str):
try:
target_str = str(target_str)
except:
traceback.print_exc()
else:
print(target_str)
self.file.write(target_str + '\n')
self.file.flush()
else:
print(target_str)
self.file.write(target_str + '\n')
self.file.flush()
def close(self):
if self.file is not None:
self.file.close()