(self, num_samples=10)
| 68 | return predicted.item(), confidence.item(), top5_idx[0].cpu().numpy(), top5_prob[0].cpu().numpy() |
| 69 | |
| 70 | def test_random_samples(self, num_samples=10): |
| 71 | print(f"\nTesting {num_samples} random samples...") |
| 72 | print("-" * 60) |
| 73 | |
| 74 | correct = 0 |
| 75 | indices = np.random.choice(len(self.dataset), num_samples, replace=False) |
| 76 | |
| 77 | for i, idx in enumerate(indices, 1): |
| 78 | image, label = self.dataset[idx] |
| 79 | pred_class, confidence, top5_idx, top5_prob = self.predict_image(image) |
| 80 | |
| 81 | true_label = self.classes[label] |
| 82 | pred_label = self.classes[pred_class] |
| 83 | |
| 84 | is_correct = pred_class == label |
| 85 | correct += is_correct |
| 86 | |
| 87 | status = "✓" if is_correct else "✗" |
| 88 | print(f"{i:2d}. {status} True: {true_label:15s} | Pred: {pred_label:15s} | Conf: {confidence:.2%}") |
| 89 | |
| 90 | if not is_correct: |
| 91 | print(f" Top-5: ", end="") |
| 92 | for j, (idx, prob) in enumerate(zip(top5_idx, top5_prob)): |
| 93 | print(f"{self.classes[idx]}({prob:.1%})", end=" ") |
| 94 | print() |
| 95 | |
| 96 | accuracy = correct / num_samples |
| 97 | print("-" * 60) |
| 98 | print(f"Accuracy: {accuracy:.1%} ({correct}/{num_samples})") |
| 99 | |
| 100 | def test_specific_sample(self, index): |
| 101 | if index < 0 or index >= len(self.dataset): |
no test coverage detected