-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
42 lines (32 loc) · 1.03 KB
/
utils.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
38
39
40
41
42
import torch
import torch.nn
import argparse
import logging
from functools import wraps
FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
LEVEL = logging.DEBUG
logging.basicConfig(format=FORMAT, level=LEVEL)
log = logging.getLogger(__name__)
log.info('Entered module: %s' % __name__)
def logger(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
log = logging.getLogger(fn.__name__)
log.info('Start running %s' % fn.__name__)
out = fn(*args, **kwargs)
log.info('Done running %s' % fn.__name__)
# Return the return value
return out
return wrapper
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def weight_init(m):
if isinstance(m, torch.nn.Conv2d):
torch.nn.init.xavier_uniform_(m.weight)
if m.bias is not None:
torch.nn.init.constant_(m.bias, 0)