-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
executable file
·56 lines (46 loc) · 1.14 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
"""Code from https://github.com/devsisters/DQN-tensorflow/blob/master/dqn/utils.py"""
import time
import numpy as np
import tensorflow as tf
import sys
if (sys.version_info[0]==2):
import cPickle
elif (sys.version_info[0]==3):
import _pickle as cPickle
try:
from scipy.misc import imresize
except:
import cv2
imresize = cv2.resize
def rgb2gray(image):
return np.dot(image[...,:3], [0.299, 0.587, 0.114])
def timeit(f):
def timed(*args, **kwargs):
start_time = time.time()
result = f(*args, **kwargs)
end_time = time.time()
print(" [-] %s : %2.5f sec" % (f.__name__, end_time - start_time))
return result
return timed
def get_time():
return time.strftime("%Y-%m-%d_%H:%M:%S", time.gmtime())
@timeit
def save_pkl(obj, path):
with open(path, 'w') as f:
cPickle.dump(obj, f)
print(" [*] save %s" % path)
@timeit
def load_pkl(path):
with open(path) as f:
obj = cPickle.load(f)
print(" [*] load %s" % path)
return obj
@timeit
def save_npy(obj, path):
np.save(path, obj)
print(" [*] save %s" % path)
@timeit
def load_npy(path):
obj = np.load(path)
print(" [*] load %s" % path)
return obj