-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClassifierHelperFunctions.py
More file actions
141 lines (109 loc) · 4.17 KB
/
ClassifierHelperFunctions.py
File metadata and controls
141 lines (109 loc) · 4.17 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
import numpy as np
import tensorflow as tf
import sys
def weight_var(shape):
return tf.Variable(tf.truncated_normal(shape, stddev=0.1))
def bias_var(shape):
return tf.Variable(tf.constant(0.1, shape=shape))
def conv(x, W):
# 2D convolution
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool(x):
# 2x2 max pooling
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
def sample_fractions(labels):
N, C = labels.shape
fractions = np.zeros(C)
if N==0:
return fractions
for i in range(C):
fractions[i] = np.sum(np.equal(labels[:, i], np.ones(N)))
fractions /= N
return fractions
def load_data(features_csv, labels_csv, newPerson=False):
# loads data as a dictionary of numpy arrays
data = {}
data_split = np.array([80.0,20.0]) # split sizes for train and test sets
if newPerson:
data_split = np.array([0.0, 100.0])
features = np.loadtxt(features_csv, dtype='float', delimiter=', ')
labels = np.loadtxt(labels_csv, dtype='int', delimiter=', ')
assert features.shape[0] == labels.shape[0] # sanity check
N, D = features.shape
_, C = labels.shape
index_test_start = np.floor((data_split[0] / np.sum(data_split)) * N).astype(int)
fractions_overall = sample_fractions(labels)
shuffled_order = None
shuffled_labels = None
while True:
shuffled_order = np.random.permutation(N)
shuffled_labels = labels[shuffled_order]
if newPerson:
break
else:
train_fractions = sample_fractions(shuffled_labels[:index_test_start])
test_fractions = sample_fractions(shuffled_labels[index_test_start:])
ssd_train = np.sum((train_fractions - fractions_overall) ** 2)
ssd_test = np.sum((test_fractions - fractions_overall) ** 2)
if ssd_train<0.0005 and ssd_test<0.0005:
break
assert shuffled_order is not None
assert shuffled_labels is not None
shuffled_features = features[shuffled_order]
data['train_features'] = shuffled_features[:index_test_start]
data['train_labels'] = shuffled_labels[:index_test_start]
data['test_features'] = shuffled_features[index_test_start:]
data['test_labels'] = shuffled_labels[index_test_start:]
return data
def get_data_folds(k, num_folds, features, labels):
# returns k-th fold for training and validation data split
N = features.shape[0]
samples_per_fold = int(np.ceil(float(N) / num_folds))
start_index = k*samples_per_fold
end_index = (k+1)*samples_per_fold
if end_index > N:
end_index = N
train_fold_features = np.delete(features, np.arange(start_index, end_index), axis=0)
train_fold_labels = np.delete(labels, np.arange(start_index, end_index), axis=0)
val_fold_features = features[start_index:end_index]
val_fold_labels = labels[start_index:end_index]
return train_fold_features, train_fold_labels, val_fold_features, val_fold_labels
class Logger(object):
"""
Logger class to print stdout messages into a log file while displaying them in stdout also.
Pass filename to which to save when instantiating.
"""
def __init__(self, f_log_name):
"""
Initialization.
:param f_log_name: file path to which to write the logs
"""
self.terminal = sys.stdout
self.log = open(f_log_name, 'w')
def write(self, message):
"""
Actual logging operations.
:param message: The message to log.
:return: Nothing
"""
self.terminal.write(message)
self.log.write(message)
def flush(self):
"""
Exists only to satisfy Python 3
:return: Nothing
"""
pass
def close_log(self):
"""
Closes the opened log file.
:return:
"""
self.log.close()
def __exit__(self, exc_type, exc_value, traceback):
"""
Exists to close the log file in case user terminated the script, etc., and close_log is not explicitly called.
"""
self.close_log()