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