| 51 | |
| 52 | |
| 53 | class App(object): |
| 54 | def __init__(self): |
| 55 | self._samples, self._labels = self.preprocess() |
| 56 | |
| 57 | def preprocess(self): |
| 58 | digits, labels = load_digits(DIGITS_FN) |
| 59 | shuffle = np.random.permutation(len(digits)) |
| 60 | digits, labels = digits[shuffle], labels[shuffle] |
| 61 | digits2 = list(map(deskew, digits)) |
| 62 | samples = preprocess_hog(digits2) |
| 63 | return samples, labels |
| 64 | |
| 65 | def get_dataset(self): |
| 66 | return self._samples, self._labels |
| 67 | |
| 68 | def run_jobs(self, f, jobs): |
| 69 | pool = ThreadPool(processes=cv2.getNumberOfCPUs()) |
| 70 | ires = pool.imap_unordered(f, jobs) |
| 71 | return ires |
| 72 | |
| 73 | def adjust_SVM(self): |
| 74 | Cs = np.logspace(0, 10, 15, base=2) |
| 75 | gammas = np.logspace(-7, 4, 15, base=2) |
| 76 | scores = np.zeros((len(Cs), len(gammas))) |
| 77 | scores[:] = np.nan |
| 78 | |
| 79 | print('adjusting SVM (may take a long time) ...') |
| 80 | def f(job): |
| 81 | i, j = job |
| 82 | samples, labels = self.get_dataset() |
| 83 | params = dict(C = Cs[i], gamma=gammas[j]) |
| 84 | score = cross_validate(SVM, params, samples, labels) |
| 85 | return i, j, score |
| 86 | |
| 87 | ires = self.run_jobs(f, np.ndindex(*scores.shape)) |
| 88 | for count, (i, j, score) in enumerate(ires): |
| 89 | scores[i, j] = score |
| 90 | print('%d / %d (best error: %.2f %%, last: %.2f %%)' % |
| 91 | (count+1, scores.size, np.nanmin(scores)*100, score*100)) |
| 92 | print(scores) |
| 93 | |
| 94 | print('writing score table to "svm_scores.npz"') |
| 95 | np.savez('svm_scores.npz', scores=scores, Cs=Cs, gammas=gammas) |
| 96 | |
| 97 | i, j = np.unravel_index(scores.argmin(), scores.shape) |
| 98 | best_params = dict(C = Cs[i], gamma=gammas[j]) |
| 99 | print('best params:', best_params) |
| 100 | print('best error: %.2f %%' % (scores.min()*100)) |
| 101 | return best_params |
| 102 | |
| 103 | def adjust_KNearest(self): |
| 104 | print('adjusting KNearest ...') |
| 105 | def f(k): |
| 106 | samples, labels = self.get_dataset() |
| 107 | err = cross_validate(KNearest, dict(k=k), samples, labels) |
| 108 | return k, err |
| 109 | best_err, best_k = np.inf, -1 |
| 110 | for k, err in self.run_jobs(f, xrange(1, 9)): |