1#!/usr/bin/env python
2
3'''
4Digit recognition adjustment.
5Grid search is used to find the best parameters for SVM and KNearest classifiers.
6SVM adjustment follows the guidelines given in
7http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf
8
9Threading or cloud computing (with http://www.picloud.com/)) may be used
10to speedup the computation.
11
12Usage:
13  digits_adjust.py [--model {svm|knearest}] [--cloud] [--env <PiCloud environment>]
14
15  --model {svm|knearest}   - select the classifier (SVM is the default)
16  --cloud                  - use PiCloud computing platform
17  --env                    - cloud environment name
18
19'''
20# TODO cloud env setup tutorial
21
22import numpy as np
23import cv2
24from multiprocessing.pool import ThreadPool
25
26from digits import *
27
28try:
29    import cloud
30    have_cloud = True
31except ImportError:
32    have_cloud = False
33
34
35
36def cross_validate(model_class, params, samples, labels, kfold = 3, pool = None):
37    n = len(samples)
38    folds = np.array_split(np.arange(n), kfold)
39    def f(i):
40        model = model_class(**params)
41        test_idx = folds[i]
42        train_idx = list(folds)
43        train_idx.pop(i)
44        train_idx = np.hstack(train_idx)
45        train_samples, train_labels = samples[train_idx], labels[train_idx]
46        test_samples, test_labels = samples[test_idx], labels[test_idx]
47        model.train(train_samples, train_labels)
48        resp = model.predict(test_samples)
49        score = (resp != test_labels).mean()
50        print ".",
51        return score
52    if pool is None:
53        scores = map(f, xrange(kfold))
54    else:
55        scores = pool.map(f, xrange(kfold))
56    return np.mean(scores)
57
58
59class App(object):
60    def __init__(self, usecloud=False, cloud_env=''):
61        if usecloud and not have_cloud:
62            print 'warning: cloud module is not installed, running locally'
63            usecloud = False
64        self.usecloud = usecloud
65        self.cloud_env = cloud_env
66
67        if self.usecloud:
68            print 'uploading dataset to cloud...'
69            cloud.files.put(DIGITS_FN)
70            self.preprocess_job = cloud.call(self.preprocess, _env=self.cloud_env)
71        else:
72            self._samples, self._labels = self.preprocess()
73
74    def preprocess(self):
75        if self.usecloud:
76            cloud.files.get(DIGITS_FN)
77        digits, labels = load_digits(DIGITS_FN)
78        shuffle = np.random.permutation(len(digits))
79        digits, labels = digits[shuffle], labels[shuffle]
80        digits2 = map(deskew, digits)
81        samples = preprocess_hog(digits2)
82        return samples, labels
83
84    def get_dataset(self):
85        if self.usecloud:
86            return cloud.result(self.preprocess_job)
87        else:
88            return self._samples, self._labels
89
90    def run_jobs(self, f, jobs):
91        if self.usecloud:
92            jids = cloud.map(f, jobs, _env=self.cloud_env, _profile=True, _depends_on=self.preprocess_job)
93            ires = cloud.iresult(jids)
94        else:
95            pool = ThreadPool(processes=cv2.getNumberOfCPUs())
96            ires = pool.imap_unordered(f, jobs)
97        return ires
98
99    def adjust_SVM(self):
100        Cs = np.logspace(0, 10, 15, base=2)
101        gammas = np.logspace(-7, 4, 15, base=2)
102        scores = np.zeros((len(Cs), len(gammas)))
103        scores[:] = np.nan
104
105        print 'adjusting SVM (may take a long time) ...'
106        def f(job):
107            i, j = job
108            samples, labels = self.get_dataset()
109            params = dict(C = Cs[i], gamma=gammas[j])
110            score = cross_validate(SVM, params, samples, labels)
111            return i, j, score
112
113        ires = self.run_jobs(f, np.ndindex(*scores.shape))
114        for count, (i, j, score) in enumerate(ires):
115            scores[i, j] = score
116            print '%d / %d (best error: %.2f %%, last: %.2f %%)' % (count+1, scores.size, np.nanmin(scores)*100, score*100)
117        print scores
118
119        print 'writing score table to "svm_scores.npz"'
120        np.savez('svm_scores.npz', scores=scores, Cs=Cs, gammas=gammas)
121
122        i, j = np.unravel_index(scores.argmin(), scores.shape)
123        best_params = dict(C = Cs[i], gamma=gammas[j])
124        print 'best params:', best_params
125        print 'best error: %.2f %%' % (scores.min()*100)
126        return best_params
127
128    def adjust_KNearest(self):
129        print 'adjusting KNearest ...'
130        def f(k):
131            samples, labels = self.get_dataset()
132            err = cross_validate(KNearest, dict(k=k), samples, labels)
133            return k, err
134        best_err, best_k = np.inf, -1
135        for k, err in self.run_jobs(f, xrange(1, 9)):
136            if err < best_err:
137                best_err, best_k = err, k
138            print 'k = %d, error: %.2f %%' % (k, err*100)
139        best_params = dict(k=best_k)
140        print 'best params:', best_params, 'err: %.2f' % (best_err*100)
141        return best_params
142
143
144if __name__ == '__main__':
145    import getopt
146    import sys
147
148    print __doc__
149
150    args, _ = getopt.getopt(sys.argv[1:], '', ['model=', 'cloud', 'env='])
151    args = dict(args)
152    args.setdefault('--model', 'svm')
153    args.setdefault('--env', '')
154    if args['--model'] not in ['svm', 'knearest']:
155        print 'unknown model "%s"' % args['--model']
156        sys.exit(1)
157
158    t = clock()
159    app = App(usecloud='--cloud' in args, cloud_env = args['--env'])
160    if args['--model'] == 'knearest':
161        app.adjust_KNearest()
162    else:
163        app.adjust_SVM()
164    print 'work time: %f s' % (clock() - t)
165