Spaces:
Running
on
Zero
Running
on
Zero
File size: 3,763 Bytes
7f2690b |
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import copy
import errno
import inspect
import numpy as np
import os
import sys
import torch
import pdb
class LoggerOutput(object):
def __init__(self, fpath=None):
self.console = sys.stdout
self.file = None
if fpath is not None:
self.mkdir_if_missing(os.path.dirname(fpath))
self.file = open(fpath, 'w')
def __del__(self):
self.close()
def __enter__(self):
pass
def __exit__(self, *args):
self.close()
def write(self, msg):
self.console.write(msg)
if self.file is not None:
self.file.write(msg)
def flush(self):
self.console.flush()
if self.file is not None:
self.file.flush()
os.fsync(self.file.fileno())
def close(self):
self.console.close()
if self.file is not None:
self.file.close()
def mkdir_if_missing(self, dir_path):
try:
os.makedirs(dir_path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.initialized = False
self.val = None
self.avg = None
self.sum = None
self.count = None
def initialize(self, val, weight):
self.val = val
self.avg = val
self.sum = val*weight
self.count = weight
self.initialized = True
def update(self, val, weight=1):
val = np.asarray(val)
if not self.initialized:
self.initialize(val, weight)
else:
self.add(val, weight)
def add(self, val, weight):
self.val = val
self.sum += val * weight
self.count += weight
self.avg = self.sum / self.count
def value(self):
if self.val is None:
return 0.
else:
return self.val.tolist()
def average(self):
if self.avg is None:
return 0.
else:
return self.avg.tolist()
class Struct:
def __init__(self, *dicts, **fields):
for d in dicts:
for k, v in d.iteritems():
setattr(self, k, v)
self.__dict__.update(fields)
def to_dict(self):
return {a: getattr(self, a) for a in self.attrs()}
def attrs(self):
#return sorted(set(dir(self)) - set(dir(Struct)))
xs = set(dir(self)) - set(dir(Struct))
xs = [x for x in xs if ((not (hasattr(self.__class__, x) and isinstance(getattr(self.__class__, x), property))) \
and (not inspect.ismethod(getattr(self, x))))]
return sorted(xs)
def updated(self, other_struct_=None, **kwargs):
s = copy.deepcopy(self)
if other_struct_ is not None:
s.__dict__.update(other_struct_.to_dict())
s.__dict__.update(kwargs)
return s
def copy(self):
return copy.deepcopy(self)
def __str__(self):
attrs = ', '.join('%s=%s' % (a, getattr(self, a)) for a in self.attrs())
return 'Struct(%s)' % attrs
class Params(Struct):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def normalize_rms(samples, desired_rms=0.1, eps=1e-4):
rms = torch.max(torch.tensor(eps), torch.sqrt(
torch.mean(samples**2, dim=1)).float())
samples = samples * desired_rms / rms.unsqueeze(1)
return samples
def normalize_rms_np(samples, desired_rms=0.1, eps=1e-4):
rms = np.maximum(eps, np.sqrt(np.mean(samples**2, 1)))
samples = samples * (desired_rms / rms)
return samples
def angle(real, imag):
return torch.atan2(imag, real)
def atleast_2d_col(x):
x = np.asarray(x)
if np.ndim(x) == 0:
return x[np.newaxis, np.newaxis]
if np.ndim(x) == 1:
return x[:, np.newaxis]
else:
return x
|