-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
46 lines (36 loc) · 1.33 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
43
44
45
46
import numpy as np
import yaml
import tensorflow as tf
from absl import logging
def set_memory_growth():
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.experimental.list_logical_devices(
'GPU')
logging.info(
"Detect {} Physical GPUs, {} Logical GPUs.".format(
len(gpus), len(logical_gpus)))
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
logging.info(e)
def load_yaml(load_path):
"""load yaml file"""
with open(load_path, 'r') as f:
loaded = yaml.load(f, Loader=yaml.Loader)
return loaded
def get_ckpt_inf(ckpt_path, steps_per_epoch):
"""get ckpt information"""
split_list = ckpt_path.split('e_')[-1].split('_b_')
epochs = int(split_list[0])
batchs = int(split_list[-1].split('.ckpt')[0])
steps = (epochs - 1) * steps_per_epoch + batchs
return epochs, steps + 1
def l2_norm(x, axis=1):
"""l2 norm"""
norm = np.linalg.norm(x, axis=axis, keepdims=True)
output = x / norm
return output