-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
82 lines (65 loc) · 2.44 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
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
# Copyright Niantic 2019. Patent Pending. All rights reserved.
#
# This software is licensed under the terms of the Monodepth2 licence
# which allows for non-commercial use only, the full terms of which are made
# available in the LICENSE file.
from __future__ import absolute_import, division, print_function
import os
import hashlib
import zipfile
from six.moves import urllib
import logging.config
import numpy as np
from thop import clever_format
from thop import profile
def profile_once(encoder, decoder, x):
x_e = x[0, :, :, :].unsqueeze(0)
x_d = encoder(x_e)
flops_e, params_e = profile(encoder, inputs=(x_e, ), verbose=False)
flops_d, params_d = profile(decoder, inputs=(x_d, ), verbose=False)
flops, params = clever_format([flops_e + flops_d, params_e + params_d], "%.3f")
flops_e, params_e = clever_format([flops_e, params_e], "%.3f")
flops_d, params_d = clever_format([flops_d, params_d], "%.3f")
return flops, params, flops_e, params_e, flops_d, params_d
def setup_logging(log_file='log.txt',filemode='w', rank=0):
"""Setup logging configuration
"""
logging.basicConfig(level=logging.INFO if rank == 0 else logging.WARN,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
filename=log_file,
filemode=filemode)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
def readlines(filename):
"""Read all the lines in a text file and return as a list
"""
with open(filename, 'r') as f:
lines = f.read().splitlines()
return lines
def normalize_image(x):
"""Rescale image pixels to span range [0, 1]
"""
ma = float(x.max().cpu().data)
mi = float(x.min().cpu().data)
d = ma - mi if ma != mi else 1e5
return (x - mi) / d
def sec_to_hm(t):
"""Convert time in seconds to time in hours, minutes and seconds
e.g. 10239 -> (2, 50, 39)
"""
t = int(t)
s = t % 60
t //= 60
m = t % 60
t //= 60
return t, m, s
def sec_to_hm_str(t):
"""Convert time in seconds to a nice string
e.g. 10239 -> '02h50m39s'
"""
h, m, s = sec_to_hm(t)
return "{:02d}h{:02d}m{:02d}s".format(h, m, s)