forked from yosinski/deep-visualization-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 6
/
misc.py
126 lines (89 loc) · 3.88 KB
/
misc.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
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
#! /usr/bin/env python
import os
import time
import errno
import re
class WithTimer:
def __init__(self, title = '', quiet = False):
self.title = title
self.quiet = quiet
def elapsed(self):
return time.time() - self.wall, time.clock() - self.proc
def enter(self):
'''Manually trigger enter'''
self.__enter__()
def __enter__(self):
self.proc = time.clock()
self.wall = time.time()
return self
def __exit__(self, *args):
if not self.quiet:
titlestr = (' ' + self.title) if self.title else ''
print 'Elapsed%s: wall: %.06f, sys: %.06f' % ((titlestr,) + self.elapsed())
def mkdir_p(path):
# From https://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def combine_dicts(dicts_tuple):
'''Combines multiple dictionaries into one by adding a prefix to keys'''
ret = {}
for prefix,dictionary in dicts_tuple:
for key in dictionary.keys():
ret['%s%s' % (prefix, key)] = dictionary[key]
return ret
def tsplit(string, no_empty_strings, *delimiters):
# split string using multiple delimiters
pattern = '|'.join(map(re.escape, delimiters))
strings = re.split(pattern, string)
if no_empty_strings:
strings = filter(None, strings)
return strings
def get_files_from_directory(settings):
# returns list of files in requested directory
available_files = []
match_flags = re.IGNORECASE if settings.static_files_ignore_case else 0
for filename in os.listdir(settings.static_files_dir):
if re.match(settings.static_files_regexp, filename, match_flags):
available_files.append(filename)
return available_files
def get_files_from_image_list(settings):
# returns list of files in requested image list file
available_files = []
labels = []
with open(settings.static_files_input_file, 'r') as image_list_file:
lines = image_list_file.readlines()
# take first token from each line
available_files = [tsplit(line, True, ' ', ',', '\t')[0] for line in lines if line.strip() != ""]
labels = [tsplit(line, True, ' ', ',', '\t')[1].strip() for line in lines if line.strip() != ""]
return available_files, labels
def get_files_from_siamese_image_list(settings):
# returns list of pair files in requested siamese image list file
available_files = []
labels = []
with open(settings.static_files_input_file, 'r') as image_list_file:
lines = image_list_file.readlines()
# take first and second tokens from each line
available_files = [(tsplit(line, True, ' ', ',', '\t')[0], tsplit(line, True, ' ', ',', '\t')[1])
for line in lines if line.strip() != ""]
labels = [tsplit(line, True, ' ', ',', '\t')[2].strip() for line in lines if line.strip() != ""]
return available_files, labels
def get_files_list(settings):
print 'Getting image list...'
# available_files - local list of files
if settings.static_files_input_mode == "directory":
available_files = get_files_from_directory(settings)
labels = None
elif (settings.static_files_input_mode == "image_list") and (not settings.is_siamese):
available_files, labels = get_files_from_image_list(settings)
elif (settings.static_files_input_mode == "image_list") and (settings.is_siamese):
available_files, labels = get_files_from_siamese_image_list(settings)
else:
raise Exception(('Error: setting static_files_input_mode has invalid option (%s)' %
(settings.static_files_input_mode)))
print 'Getting image list... Done.'
return available_files, labels