Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aamini
GitHub Repository: aamini/introtodeeplearning
Path: blob/master/mitdeeplearning/lab2.py
547 views
1
import cv2
2
import os
3
import matplotlib.pyplot as plt
4
import numpy as np
5
import tensorflow as tf
6
import time
7
import h5py
8
import sys
9
import glob
10
11
IM_SHAPE = (64, 64, 3)
12
13
14
def plot_image_prediction(i, predictions_array, true_label, img):
15
predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
16
plt.grid(False)
17
plt.xticks([])
18
plt.yticks([])
19
20
plt.imshow(np.squeeze(img), cmap=plt.cm.binary)
21
22
predicted_label = np.argmax(predictions_array)
23
if predicted_label == true_label:
24
color = "blue"
25
else:
26
color = "red"
27
28
plt.xlabel(
29
"{} {:2.0f}% ({})".format(
30
predicted_label, 100 * np.max(predictions_array), true_label
31
),
32
color=color,
33
)
34
35
36
def plot_value_prediction(i, predictions_array, true_label):
37
predictions_array, true_label = predictions_array[i], true_label[i]
38
plt.grid(False)
39
plt.xticks([])
40
plt.yticks([])
41
thisplot = plt.bar(range(10), predictions_array, color="#777777")
42
plt.ylim([0, 1])
43
predicted_label = np.argmax(predictions_array)
44
45
thisplot[predicted_label].set_color("red")
46
thisplot[true_label].set_color("blue")
47
48
49
class TrainingDatasetLoader(object):
50
def __init__(self, data_path, channels_last=True):
51
print("Opening {}".format(data_path))
52
sys.stdout.flush()
53
54
self.cache = h5py.File(data_path, "r")
55
56
print("Loading data into memory...")
57
sys.stdout.flush()
58
self.images = self.cache["images"][:]
59
self.channels_last = channels_last
60
self.labels = self.cache["labels"][:].astype(np.float32)
61
self.image_dims = self.images.shape
62
n_train_samples = self.image_dims[0]
63
64
self.train_inds = np.random.permutation(np.arange(n_train_samples))
65
66
self.pos_train_inds = self.train_inds[self.labels[self.train_inds, 0] == 1.0]
67
self.neg_train_inds = self.train_inds[self.labels[self.train_inds, 0] != 1.0]
68
69
def get_train_size(self):
70
return self.train_inds.shape[0]
71
72
def get_train_steps_per_epoch(self, batch_size, factor=10):
73
return self.get_train_size() // factor // batch_size
74
75
def get_batch(self, n, only_faces=False, p_pos=None, p_neg=None, return_inds=False):
76
if only_faces:
77
selected_inds = np.random.choice(
78
self.pos_train_inds, size=n, replace=False, p=p_pos
79
)
80
else:
81
selected_pos_inds = np.random.choice(
82
self.pos_train_inds, size=n // 2, replace=False, p=p_pos
83
)
84
selected_neg_inds = np.random.choice(
85
self.neg_train_inds, size=n // 2, replace=False, p=p_neg
86
)
87
selected_inds = np.concatenate((selected_pos_inds, selected_neg_inds))
88
89
sorted_inds = np.sort(selected_inds)
90
train_img = (self.images[sorted_inds, :, :, ::-1] / 255.0).astype(np.float32)
91
train_label = self.labels[sorted_inds, ...]
92
93
if not self.channels_last:
94
train_img = np.ascontiguousarray(
95
np.transpose(train_img, (0, 3, 1, 2))
96
) # [B, H, W, C] -> [B, C, H, W]
97
return (
98
(train_img, train_label, sorted_inds)
99
if return_inds
100
else (train_img, train_label)
101
)
102
103
def get_n_most_prob_faces(self, prob, n):
104
idx = np.argsort(prob)[::-1]
105
most_prob_inds = self.pos_train_inds[idx[: 10 * n : 10]]
106
return (self.images[most_prob_inds, ...] / 255.0).astype(np.float32)
107
108
def get_all_train_faces(self):
109
return self.images[self.pos_train_inds]
110
111
112
def get_test_faces(channels_last=True):
113
cwd = os.path.dirname(__file__)
114
images = {"LF": [], "LM": [], "DF": [], "DM": []}
115
for key in images.keys():
116
files = glob.glob(os.path.join(cwd, "data", "faces", key, "*.png"))
117
for file in sorted(files):
118
image = cv2.resize(cv2.imread(file), (64, 64))[:, :, ::-1] / 255.0
119
if not channels_last:
120
image = np.transpose(image, (2, 0, 1))
121
images[key].append(image)
122
123
return images["LF"], images["LM"], images["DF"], images["DM"]
124
125