I wrote a program intended to classify an image by similarity:
for i in g:
fulFi = i
tiva = []
tivb = []
a = cv2.imread(i)
b = cv2.resize(a, (500, 500))
img2 = flatten_image(b)
tivb.append(img2)
cb = np.array(tivb)
iab = trueArray(cb)
print "Image: " + (str(i)).split("/")[-1]
print "Image Size " + str(len(iab))
print "Image Data: " + str(iab) + "\n"
pca = RandomizedPCA(n_components=2)
X = pca.fit_transform(iab)
Xy = pca.transform(X)
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X, Xy.ravel())
def aip(img):
a = cv2.imread(img)
b = cv2.resize(a, (500, 500))
tivb = []
r = flatten_image(b)
tivb.append(r)
o = np.array(tivb)
l = trueArray(o)
print "Test Image: " + (str(img)).split("/")[-1]
print "Test Image Size " + str(len(l))
print "Test Image Data: " + str(l) + "\n"
return l
testIm = aip(sys.argv[2])
b = pca.fit_transform(testIm)
print "KNN Prediction: " + str(knn.predict(b))
And while it functioned perfectly, it had an error: it gave me the exact same value regardless of the image used:
Image: 150119131035-green-bay-seattle-nfl-1024x576.jpg
Image Size 750000
Image Data: [255 242 242 ..., 148 204 191]
Test Image: agun.jpg
Test Image Size 750000
Test Image Data: [216 255 253 ..., 205 225 242]
KNN Prediction: [-255.]
and
Image: 150119131035-green-bay-seattle-nfl-1024x576.jpg
Image Size 750000
Image Data: [255 242 242 ..., 148 204 191]
Test Image: bliss.jpg
Test Image Size 750000
Test Image Data: [243 240 232 ..., 13 69 48]
KNN Prediction: [-255.]
The KNN prediction is always 255, no matter the image used. After investigation further, A found that the problem was my PCA: For some reason, it was taking an array with 750000 values and returning an array with only one:
pca = RandomizedPCA(n_components=2)
X = pca.fit_transform(iab)
Xy = pca.transform(X)
print "Iab: " + str(iab)
print "Iab Type: " + str(type(iab))
print "Iab length: " + str(len(iab))
print "X Type: " + str(type(X))
print "X length: " + str(len(X))
print "X: " + str(X)
print "Xy Type: " + str(type(Xy))
print "Xy Length: " + str(len(X))
print "Xy: " + str(Xy)
gives this:
Image: 150119131035-green-bay-seattle-nfl-1024x576.jpg
Image Size 750000
Image Data: [255 242 242 ..., 148 204 191]
Iab: [255 242 242 ..., 148 204 191]
Iab Type: <type 'numpy.ndarray'>
Iab length: 750000
X Type: <type 'numpy.ndarray'>
X length: 1
X: [[ 0.]]
Xy Type: <type 'numpy.ndarray'>
Xy Length: 1
Xy: [[-255.]]
My question is why? X and Xy should both have hundreds of values, not just one. The tutorial I followed didn't have an explanation, and the documentation only says that there needs to be the same array format for both the transform and the fit_transform. How should I be approaching this?