MCPcopy Index your code
hub / github.com/lazyprogrammer/machine_learning_examples / get_test_accuracy

Function get_test_accuracy

cnn_class2/siamese.py:356–418  ·  view source on GitHub ↗
(threshold=0.85)

Source from the content-addressed store, hash-verified

354
355
356def get_test_accuracy(threshold=0.85):
357 positive_distances = []
358 negative_distances = []
359
360 tp = 0
361 tn = 0
362 fp = 0
363 fn = 0
364
365 batch_size = 64
366 x_batch_1 = np.zeros([batch_size] + list(img.shape))
367 x_batch_2 = np.zeros([batch_size] + list(img.shape))
368 n_batches = int(np.ceil(len(test_positives) / batch_size))
369 for i in range(n_batches):
370 print(f"pos batch: {i+1}/{n_batches}")
371 pos_batch_indices = test_positives[i * batch_size: (i + 1) * batch_size]
372
373 # fill up x_batch and y_batch
374 j = 0
375 for idx1, idx2 in pos_batch_indices:
376 x_batch_1[j] = test_images[idx1]
377 x_batch_2[j] = test_images[idx2]
378 j += 1
379
380 x1 = x_batch_1[:j]
381 x2 = x_batch_2[:j]
382 distances = model.predict([x1, x2]).flatten()
383 positive_distances += distances.tolist()
384
385 # update tp, tn, fp, fn
386 tp += (distances < threshold).sum()
387 fn += (distances > threshold).sum()
388
389 n_batches = int(np.ceil(len(test_negatives) / batch_size))
390 for i in range(n_batches):
391 print(f"neg batch: {i+1}/{n_batches}")
392 neg_batch_indices = test_negatives[i * batch_size: (i + 1) * batch_size]
393
394 # fill up x_batch and y_batch
395 j = 0
396 for idx1, idx2 in neg_batch_indices:
397 x_batch_1[j] = test_images[idx1]
398 x_batch_2[j] = test_images[idx2]
399 j += 1
400
401 x1 = x_batch_1[:j]
402 x2 = x_batch_2[:j]
403 distances = model.predict([x1, x2]).flatten()
404 negative_distances += distances.tolist()
405
406 # update tp, tn, fp, fn
407 fp += (distances < threshold).sum()
408 tn += (distances > threshold).sum()
409
410
411 tpr = tp / (tp + fn)
412 tnr = tn / (tn + fp)
413 print(f"sensitivity (tpr): {tpr}, specificity (tnr): {tnr}")

Callers 1

siamese.pyFile · 0.85

Calls 1

predictMethod · 0.45

Tested by

no test coverage detected