| 194 | |
| 195 | # same thing as the train generator except no shuffling and it uses the test set |
| 196 | def test_generator(): |
| 197 | n_batches = int(np.ceil(len(test_positives) / batch_size)) |
| 198 | |
| 199 | while True: |
| 200 | n_samples = batch_size * 2 |
| 201 | shape = [n_samples] + list(img.shape) |
| 202 | x_batch_1 = np.zeros(shape) |
| 203 | x_batch_2 = np.zeros(shape) |
| 204 | y_batch = np.zeros(n_samples) |
| 205 | |
| 206 | for i in range(n_batches): |
| 207 | pos_batch_indices = test_positives[i * batch_size: (i + 1) * batch_size] |
| 208 | |
| 209 | # fill up x_batch and y_batch |
| 210 | j = 0 |
| 211 | for idx1, idx2 in pos_batch_indices: |
| 212 | x_batch_1[j] = test_images[idx1] |
| 213 | x_batch_2[j] = test_images[idx2] |
| 214 | y_batch[j] = 1 # match |
| 215 | j += 1 |
| 216 | |
| 217 | # get negative samples |
| 218 | neg_indices = np.random.choice(len(test_negatives), size=len(pos_batch_indices), replace=False) |
| 219 | for neg in neg_indices: |
| 220 | idx1, idx2 = test_negatives[neg] |
| 221 | x_batch_1[j] = test_images[idx1] |
| 222 | x_batch_2[j] = test_images[idx2] |
| 223 | y_batch[j] = 0 # non-match |
| 224 | j += 1 |
| 225 | |
| 226 | x1 = x_batch_1[:j] |
| 227 | x2 = x_batch_2[:j] |
| 228 | y = y_batch[:j] |
| 229 | yield [x1, x2], y |
| 230 | |
| 231 | |
| 232 | |