-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
63 lines (55 loc) · 2.04 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
(c) Copyright 2011, Francisco Caraballo La Riva.
This program is free software: you can redistribute it and/or modify
it in any way.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"""
from scipy.misc import imread
import numpy as np
from copy import deepcopy
import pca
import classifiers
from glob import glob
def load_images_bw(filenames):
result = []
for i in filenames:
result.append(imread(i, flatten = 1))
return result
# Each set contains 18 images tagged with
# the same label
images1 = load_images_bw(glob("set1/*"))
images2 = load_images_bw(glob("set2/*"))
images3 = load_images_bw(glob("set3/*"))
image_vectors = images1 + images2 + images3
# Images are represented as a vector of all the pixels.
# In this example, images were 400 x 400 so the resulting
# vectors are 1 x 160000
for v in image_vectors:
v.shape = (1,160000)
vt = tuple(image_vectors)
vectors = np.vstack(vt)
# Do Principal Component Analysis on the matrix of the
# images
eig_vectors,eig_vals = pca.pca(vectors, threshold=0.99)
transformed_vectors = (np.dot(eig_vectors,vectors.T)).T
# This is just a hack to get the tags vector
# I just happen to know that the tag changes every
# 18 images because of how I loaded the images.
tags = [ ]
for i in range(transformed_vectors.shape[0]):
tags.append(i/18)
tags = np.asarray(tags)
tags.shape = (54,1)
#Leave one out test of the classifier
successes = 0.0
for i in range(transformed_vectors.shape[0]):
vec = np.vstack((transformed_vectors[:i,:],transformed_vectors[i+1:,:]))
tags_2 = np.vstack((tags[:i,:],tags[i+1:,:]))
classifier = classifiers.knnclassifier(vec,tags_2,5)
classifier_output = classifier.classify(transformed_vectors[i,:])
if classifier_output == tags[i,0]:
successes += 1
print 'Classifier output: ', classifier_output, 'Real Tag: ', tags[i,0]
print 'Success %', successes/transformed_vectors.shape[0]*100