You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
2.5 KiB
Matlab
50 lines
2.5 KiB
Matlab
function classify_images_kNN()
|
|
%Apply PC/BC-DIM to classifying images
|
|
task='USPS';%'MNIST'; %'YALE'; %
|
|
onoff=0;
|
|
|
|
%load data
|
|
[data,class,inTrain,inTest,imDims,numClasses]=images_load_dataset(task);
|
|
|
|
if onoff
|
|
%subtract mean from each image
|
|
data=bsxfun(@minus,data,mean(data')');
|
|
%split into ON and OFF channels
|
|
on=data; on(on<0)=0;
|
|
off=-data; off(off<0)=0;
|
|
data=[on,off];
|
|
else
|
|
%scale each image to range between 0 and 1
|
|
data=bsxfun(@minus,data,min(data')');
|
|
data=bsxfun(@rdivide,data,max(data')');
|
|
end
|
|
data=bsxfun(@rdivide,data,max(1e-6,sum(abs(data),2)));
|
|
|
|
|
|
%present all test cases to network and calculate classification error
|
|
|
|
%distance='euclidean';% — Euclidean distance (default)
|
|
%distance='cityblock';% — Sum of absolute differences
|
|
distance='cosine';% — One minus the cosine of the included angle between points (treated as vectors)
|
|
|
|
for k=1:20
|
|
classPredicted=knnclassify(data(inTest,:), data(inTrain,:), class(inTrain), k,distance);
|
|
percent_error(k)=100*sum(classPredicted~=class(inTest)')./length(inTest);
|
|
end
|
|
percent_error
|
|
|
|
%YALEeuclidean=[10.7054,10.7054,10.4564,10.2905,10.5394,10.7884,10.5394, 9.9585,10.2905,10.7054,10.7884,10.8714,11.2863,10.6224,11.6183,12.0332,12.4481,13.0290,12.9461,13.2780];
|
|
%USPSeuclidean=[4.8829, 4.8829, 4.8331, 4.7833, 5.3812, 5.3313, 5.8296, 5.7798, 5.7798, 5.9292, 5.9791, 6.1784, 6.3777, 6.2780, 6.6268, 6.5272, 6.7763, 6.6766, 6.8261, 6.8759];
|
|
%k=4: 10.2905, 4.7833 (15.0738)
|
|
|
|
%YALEcosine=[9.7095, 9.7095, 9.3776, 9.3776, 9.0456, 9.4606, 9.6266, 9.3776, 9.3776, 9.7095, 9.5436,10.2075,10.2075,11.0373,11.8672,12.0332,11.8672,11.5353,11.7842,11.7842];
|
|
%USPScosine=[4.4345, 4.4345, 4.6338, 4.4345, 4.6338, 4.6836, 4.9327, 5.2317, 5.3812, 5.1819, 5.5306, 5.4310, 5.8794, 5.6801, 5.9292, 5.8794, 6.1286, 6.1286, 6.2780, 6.2780];
|
|
%k=5: 9.0456, 4.6338 (13.6794)
|
|
|
|
%YALEeuclideanONOFF=[9.2946,9.2946, 9.3776,9.3776, 9.6266, 9.8755, 9.4606, 9.2946, 9.5436, 9.8755,10.3734,10.5394,11.0373,11.3693,11.7842,12.1992,12.3651,12.3651,12.6141,12.7801];
|
|
%USPSeuclideanONOFF=[4.9327,4.9327, 5.0324, 4.7833, 5.1819, 4.9826, 5.3812, 5.3812, 5.5805,5.5306, 5.8296,5.8296, 6.1286, 5.8794, 6.0787, 6.1286, 6.2282, 6.2780, 6.2282, 6.2282];
|
|
%k=4: 9.3776, 4.7833 (14.1609)
|
|
|
|
%YALEcosineONOFF=[9.2946, 9.2946, 9.0456, 8.9627, 8.6307, 8.7137, 8.6307, 8.6307, 8.8797, 9.0456,10.1245,10.7884,11.1203,11.6183,11.6183,11.7842,11.6183,12.0332,12.0332,12.2822];
|
|
%USPScosineONOFF=[4.6836, 4.6836, 4.8331, 4.4345, 5.0324, 4.6836, 5.1819, 4.8829, 5.2815, 5.0822, 5.3812, 5.5805, 5.6303, 5.6801, 5.8296, 5.8794, 6.1286,6.0787,6.0289,6.1286];
|
|
%k=4: 8.9627, 4.4345 (13.3972) |