Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
138 lines (114 loc) · 3.94 KB
/
Copy pathutils.py
File metadata and controls
138 lines (114 loc) · 3.94 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
import numpy as np
import os
import tensorflow as tf
import random
from tensorflow.python.client import device_lib
from sklearn.metrics import confusion_matrix
from matplotlib import pyplot as plt
def create_session(gpu_id='0', pp_mem_frac=None):
tf.reset_default_graph()
os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id # can multiple?
with tf.device('/gpu:' + gpu_id):
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
if pp_mem_frac is not None:
config.gpu_options.per_process_gpu_memory_fraction=pp_mem_frac
session = tf.Session(config = config)
return session
def close_session(session):
session.close()
def check_available_device():
print(device_lib.list_local_devices())
def set_rand_seed(seed=None): # TODO: keras, theano and so forth
if seed is None:
# seed = int(os.getenv("SEED", 12))
pass
tf.set_random_seed(seed)
np.random.seed(seed)
random.seed(seed)
def get_tensor_shape(tensor):
return tensor.get_shape().as_list(), tensor.get_shape().num_elements()
# plot and show sample # deprecated
def plot_mnist(X, y, y_soft=None, n_samples=6, round_show=4, random=False):
n_data = X.shape[0]
if random:
indices = np.random.choice(n_data,n_samples)
else:
indices = np.arange(n_samples)
for i in range(n_samples):
sample = X[[indices[i]]]
label = y[indices[i]]
if y_soft is not None:
label_soft = y_soft[indices[i]]
plt.imshow(sample.reshape((28,28)), cmap='gray')
plt.show()
# pred = student.predict(sample)
# pred_t = student.predict_softened(sample, temperature=temperature)
print(label)
print(label_soft)
# print(np.round(pred,round_show))
# print(np.round(pred_t,round_show))
def plot_trend(df, label_x, label_y, label_line=None):
if label_line is not None:
for cond in df[label_line].unique():
label_legend = label_line+'='+str(cond)
mask = df[label_line] == cond
df_plt = df[mask]
plt.plot(df_plt[label_x], df_plt[label_y], label=label_legend)
plt.xlabel(label_x)
plt.ylabel(label_y)
plt.legend()
plt.show()
def plot_confusion_matrix(y_true, y_pred):
# This is called from print_test_accuracy() below.
# cls_pred is an array of the predicted class-number for
# all images in the test-set.
# Get the number of classes
num_classes = y_true.shape[1]
# Get the confusion matrix using sklearn.
cm = confusion_matrix(y_true=y_true,
y_pred=y_pred)
# Print the confusion matrix as text.
print(cm)
# Plot the confusion matrix as an image.
plt.matshow(cm)
# Make various adjustments to the plot.
plt.colorbar()
tick_marks = np.arange(num_classes)
plt.xticks(tick_marks, range(num_classes))
plt.yticks(tick_marks, range(num_classes))
plt.xlabel('Predicted')
plt.ylabel('True')
# Ensure the plot is shown correctly with multiple plots
# in a single Notebook cell.
plt.show()
def print_obj(obj, obj_str): # support ndarray, Tensor, dict, iterable
# exec('global '+obj_str)
# exec('obj = '+obj_str)
obj_type = type(obj)
print(obj_str
, obj_type
, end = ' '
)
if obj_type == np.ndarray:
print(obj.shape)
elif tf.contrib.framework.is_tensor(obj):
obj_shape = obj.get_shape().as_list() # get_shape().num_elements() can get sum
print(obj_shape)
elif isinstance(obj, dict):
print()
for key, content in obj.items():
print(key, ':', content)
else:
try:
iterator = iter(obj)
except TypeError:
# not iterable
print(obj)
else:
# iterable
print(len(obj))
def unison_shuffled_copies(a, b):
assert len(a) == len(b)
p = np.random.permutation(len(a))
return a[p], b[p]