| 103 | |
| 104 | |
| 105 | def evaluate_model(model, digits, samples, labels): |
| 106 | resp = model.predict(samples) |
| 107 | err = (labels != resp).mean() |
| 108 | print('error: %.2f %%' % (err*100)) |
| 109 | |
| 110 | confusion = np.zeros((10, 10), np.int32) |
| 111 | for i, j in zip(labels, resp): |
| 112 | confusion[i, int(j)] += 1 |
| 113 | print('confusion matrix:') |
| 114 | print(confusion) |
| 115 | print() |
| 116 | |
| 117 | vis = [] |
| 118 | for img, flag in zip(digits, resp == labels): |
| 119 | img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) |
| 120 | if not flag: |
| 121 | img[...,:2] = 0 |
| 122 | vis.append(img) |
| 123 | return mosaic(25, vis) |
| 124 | |
| 125 | def preprocess_simple(digits): |
| 126 | return np.float32(digits).reshape(-1, SZ*SZ) / 255.0 |