08.06.2015 Views

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Computer Vision – Pattern Recognition<br />

The number of words used does not usually have a big impact on<br />

the final performance of the algorithm. Naturally, if the number<br />

is extremely small (ten or twenty, when you have a few thousand<br />

images), then the overall system will not perform well. Similarly, if<br />

you have too many words (many more than the number of images<br />

for example), the system will not perform well. However, in between<br />

these two extremes, there is often a very large plateau where you can<br />

choose the number of words <strong>with</strong>out a big impact on the result. As a<br />

rule of thumb, using a value such as 256, 512, or 1024 if you have<br />

very many images, should give you a good result.<br />

We are going to start by computing the features:<br />

alldescriptors = []<br />

for im in images:<br />

im = mh.imread(im, as_grey=True)<br />

im = im.astype(np.uint8)<br />

alldescriptors.append(surf.surf(im, descriptors_only))<br />

This results in over 100,000 local descriptors. Now, we use k-means clustering to<br />

obtain the centroids. We could use all the descriptors, but we are going to use a<br />

smaller sample for extra speed:<br />

concatenated = np.concatenate(alldescriptors) # get all descriptors<br />

into a single array<br />

concatenated = concatenated[::32] # use only every 32nd vector<br />

from sklearn.cluster import Kmeans<br />

k = 256<br />

km = KMeans(k)<br />

km.fit(concatenated)<br />

After this is done (which will take a while), we have km containing information<br />

about the centroids. We now go back to the descriptors and build feature vectors:<br />

features = []<br />

for d in alldescriptors:<br />

c = km.predict(d)<br />

features.append(<br />

np.array([np.sum(c == ci) for ci in range(k)])<br />

)<br />

features = np.array(features)<br />

The end result of this loop is that features[fi] is a histogram corresponding<br />

to the image at position fi (the same could have been computed faster <strong>with</strong> the<br />

np.histogram function, but getting the arguments just right is a little tricky, and<br />

the rest of the code is, in any case, much slower than this simple step).<br />

[ 218 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!