-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
59 lines (50 loc) · 1.72 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
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import os
import copy
import numpy as np
import tensorflow as tf
class ItemPool(object):
def __init__(self, max_num=50):
self.max_num = max_num
self.num = 0
self.items = []
def __call__(self, in_items):
""" in_items is a list of item"""
if self.max_num == 0:
return in_items
return_items = []
for in_item in in_items:
if self.num < self.max_num:
self.items.append(in_item)
self.num = self.num + 1
return_items.append(in_item)
else:
if np.random.rand() > 0.5:
idx = np.random.randint(0, self.max_num)
tmp = copy.copy(self.items[idx])
self.items[idx] = in_item
return_items.append(tmp)
else:
return_items.append(in_item)
return return_items
def mkdir(paths):
if not isinstance(paths, list):
paths = [paths]
for path in paths:
path_dir, _ = os.path.split(path)
if not os.path.isdir(path_dir):
os.makedirs(path_dir)
def load_checkpoint(checkpoint_dir, sess, saver):
print(" [*] Loading checkpoint...")
ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
if ckpt and ckpt.model_checkpoint_path:
ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
ckpt_path = os.path.join(checkpoint_dir, ckpt_name)
saver.restore(sess, ckpt_path)
print(" [*] Loading successful!")
return ckpt_path
else:
print(" [*] No suitable checkpoint!")
return None