Skip to content
Snippets Groups Projects
Commit aa2a4985 authored by ik1g19's avatar ik1g19
Browse files

fixed classification

parent 5fd8ed48
Branches
No related tags found
No related merge requests found
...@@ -12,29 +12,20 @@ import org.openimaj.experiment.evaluation.classification.ClassificationEvaluator ...@@ -12,29 +12,20 @@ import org.openimaj.experiment.evaluation.classification.ClassificationEvaluator
import org.openimaj.experiment.evaluation.classification.ClassificationResult; import org.openimaj.experiment.evaluation.classification.ClassificationResult;
import org.openimaj.experiment.evaluation.classification.analysers.confusionmatrix.CMAnalyser; import org.openimaj.experiment.evaluation.classification.analysers.confusionmatrix.CMAnalyser;
import org.openimaj.experiment.evaluation.classification.analysers.confusionmatrix.CMResult; import org.openimaj.experiment.evaluation.classification.analysers.confusionmatrix.CMResult;
import org.openimaj.feature.DoubleFV;
import org.openimaj.feature.FeatureExtractor; import org.openimaj.feature.FeatureExtractor;
import org.openimaj.feature.FloatFV;
import org.openimaj.feature.SparseIntFV; import org.openimaj.feature.SparseIntFV;
import org.openimaj.feature.local.LocalFeatureImpl;
import org.openimaj.feature.local.Location;
import org.openimaj.feature.local.data.LocalFeatureListDataSource; import org.openimaj.feature.local.data.LocalFeatureListDataSource;
import org.openimaj.feature.local.list.LocalFeatureList; import org.openimaj.feature.local.list.LocalFeatureList;
import org.openimaj.image.DisplayUtilities;
import org.openimaj.image.FImage; import org.openimaj.image.FImage;
import org.openimaj.image.ImageUtilities; import org.openimaj.image.ImageUtilities;
import org.openimaj.image.MBFImage;
import org.openimaj.image.annotation.evaluation.datasets.Caltech101;
import org.openimaj.image.colour.ColourSpace;
import org.openimaj.image.colour.RGBColour;
import org.openimaj.image.feature.dense.gradient.dsift.ByteDSIFTKeypoint;
import org.openimaj.image.feature.dense.gradient.dsift.DenseSIFT;
import org.openimaj.image.feature.dense.gradient.dsift.PyramidDenseSIFT;
import org.openimaj.image.feature.local.aggregate.BagOfVisualWords; import org.openimaj.image.feature.local.aggregate.BagOfVisualWords;
import org.openimaj.image.feature.local.aggregate.BlockSpatialAggregator;
import org.openimaj.image.processing.convolution.FGaussianConvolve;
import org.openimaj.image.typography.hershey.HersheyFont;
import org.openimaj.ml.annotation.linear.LiblinearAnnotator; import org.openimaj.ml.annotation.linear.LiblinearAnnotator;
import org.openimaj.ml.clustering.ByteCentroidsResult; import org.openimaj.ml.clustering.FloatCentroidsResult;
import org.openimaj.ml.clustering.assignment.HardAssigner; import org.openimaj.ml.clustering.assignment.HardAssigner;
import org.openimaj.ml.clustering.kmeans.ByteKMeans; import org.openimaj.ml.clustering.kmeans.FloatKMeans;
import org.openimaj.util.pair.IntFloatPair; import org.openimaj.util.pair.IntFloatPair;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -42,37 +33,45 @@ import java.util.List; ...@@ -42,37 +33,45 @@ import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* OpenIMAJ Hello world! * Run #2
* * Image classification using 15 one-vs-all classifiers
* Trained using densely sampled patches with k-means clustering and visual words
*/ */
public class App { public class App {
public static void main( String[] args ) { public static void main( String[] args ) {
//creating datasets
GroupedDataset<String, VFSListDataset<FImage>, FImage> trainingData; GroupedDataset<String, VFSListDataset<FImage>, FImage> trainingData;
GroupedDataset<String, VFSListDataset<FImage>, FImage> testingData; VFSListDataset<FImage> testingData;
try { try {
trainingData = new VFSGroupDataset<FImage>( trainingData = new VFSGroupDataset<FImage>(
"zip:C:\\Users\\isaac\\Documents\\repos\\visioncw3\\visioncw3\\data\\training", ImageUtilities.FIMAGE_READER); "zip:C:/Users/isaac/Documents/GitHub Repos/visioncw3/visioncw3/data/training.zip", ImageUtilities.FIMAGE_READER);
testingData = new VFSGroupDataset<FImage>( testingData = new VFSListDataset<FImage>(
"zip:C:\\Users\\isaac\\Documents\\repos\\visioncw3\\visioncw3\\data\\testing.zip", ImageUtilities.FIMAGE_READER); "zip:C:/Users/isaac/Documents/GitHub Repos/visioncw3/visioncw3/data/testing.zip", ImageUtilities.FIMAGE_READER);
} catch (Exception e) { } catch (Exception e) {
System.err.println("Training & testing data failed to load"); System.err.println("Training & testing data failed to load");
System.err.println(e); System.err.println(e);
return; return;
} }
trainingData.remove("training");
GroupedRandomSplitter<String, FImage> splits = GroupedRandomSplitter<String, FImage> splits =
new GroupedRandomSplitter<String, FImage>(trainingData, 15, 0, 15); new GroupedRandomSplitter<String, FImage>(trainingData, 80, 0, 20);
DenseSIFT dsift = new DenseSIFT(5, 7); //analyse patches of 8x8 sizes
PyramidDenseSIFT<FImage> pdsift = new PyramidDenseSIFT<FImage>(dsift, 6f, 7); PatchFeature patch = new PatchFeature(8);
HardAssigner<byte[], float[], IntFloatPair> assigner = //train quantiser on a sample of the data set
trainQuantiser(GroupedUniformRandomisedSampler.sample(splits.getTrainingDataset(), 30), pdsift); HardAssigner<float[], float[], IntFloatPair> assigner =
trainQuantiser(GroupedUniformRandomisedSampler.sample(splits.getTrainingDataset(), 30), patch);
FeatureExtractor<DoubleFV, FImage> extractor = new PHOWExtractor(pdsift, assigner); //create a feature extractor to densely sample patches
FeatureExtractor<SparseIntFV, FImage> extractor = new PatchExtractor(patch, assigner);
LiblinearAnnotator<FImage, String> ann = new LiblinearAnnotator<FImage, String>( LiblinearAnnotator<FImage, String> ann = new LiblinearAnnotator<FImage, String>(
...@@ -86,50 +85,50 @@ public class App { ...@@ -86,50 +85,50 @@ public class App {
Map<FImage, ClassificationResult<String>> guesses = eval.evaluate(); Map<FImage, ClassificationResult<String>> guesses = eval.evaluate();
CMResult<String> result = eval.analyse(guesses); CMResult<String> result = eval.analyse(guesses);
System.out.println(result.getDetailReport());
} }
static HardAssigner<byte[], float[], IntFloatPair> trainQuantiser(
Dataset<FImage> sample, PyramidDenseSIFT<FImage> pdsift) static HardAssigner<float[], float[], IntFloatPair> trainQuantiser(
Dataset<FImage> sample, PatchFeature<FImage> patch)
{ {
List<LocalFeatureList<ByteDSIFTKeypoint>> allkeys = new ArrayList<LocalFeatureList<ByteDSIFTKeypoint>>(); List<LocalFeatureList<LocalFeatureImpl<Location, FloatFV>>> allkeys = new ArrayList<>();
for (FImage rec : sample) { for (FImage rec : sample) {
FImage img = rec; FImage img = rec;
pdsift.analyseImage(img); patch.analyseImage(img);
allkeys.add(pdsift.getByteKeypoints(0.005f)); allkeys.add(patch.getPatchKeypoints());
} }
if (allkeys.size() > 10000) if (allkeys.size() > 10000)
allkeys = allkeys.subList(0, 10000); allkeys = allkeys.subList(0, 10000);
ByteKMeans km = ByteKMeans.createKDTreeEnsemble(300); FloatKMeans km = FloatKMeans.createExact(500);
DataSource<byte[]> datasource = new LocalFeatureListDataSource<ByteDSIFTKeypoint, byte[]>(allkeys); DataSource<float[]> datasource = new LocalFeatureListDataSource<LocalFeatureImpl<Location, FloatFV>, float[]>(allkeys);
ByteCentroidsResult result = km.cluster(datasource); FloatCentroidsResult result = km.cluster(datasource);
return result.defaultHardAssigner(); return result.defaultHardAssigner();
} }
} }
class PHOWExtractor implements FeatureExtractor<DoubleFV, FImage> { class PatchExtractor implements FeatureExtractor<SparseIntFV, FImage> {
PyramidDenseSIFT<FImage> pdsift; PatchFeature<FImage> patch;
HardAssigner<byte[], float[], IntFloatPair> assigner; HardAssigner<float[], float[], IntFloatPair> assigner;
public PHOWExtractor(PyramidDenseSIFT<FImage> pdsift, HardAssigner<byte[], float[], IntFloatPair> assigner) public PatchExtractor(PatchFeature<FImage> patch, HardAssigner<float[], float[], IntFloatPair> assigner)
{ {
this.pdsift = pdsift; this.patch = patch;
this.assigner = assigner; this.assigner = assigner;
} }
public DoubleFV extractFeature(FImage object) { public SparseIntFV extractFeature(FImage object) {
FImage image = object.getImage(); FImage image = object.getImage();
pdsift.analyseImage(image); patch.analyseImage(image);
BagOfVisualWords<byte[]> bovw = new BagOfVisualWords<byte[]>(assigner);
BlockSpatialAggregator<byte[], SparseIntFV> spatial = new BlockSpatialAggregator<byte[], SparseIntFV>( BagOfVisualWords<float[]> bovw = new BagOfVisualWords<>(assigner);
bovw, 2, 2);
return spatial.aggregate(pdsift.getByteKeypoints(0.015f), image.getBounds()).normaliseFV(); return bovw.aggregate(patch.getPatchKeypoints());
} }
} }
\ No newline at end of file
package uk.ac.soton.ecs.ik1g19;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.openimaj.feature.DoubleFV;
import org.openimaj.feature.FloatFV;
import org.openimaj.feature.local.LocalFeatureImpl;
import org.openimaj.feature.local.Location;
import org.openimaj.feature.local.SpatialLocation;
import org.openimaj.feature.local.list.LocalFeatureList;
import org.openimaj.feature.local.list.MemoryLocalFeatureList;
import org.openimaj.image.FImage;
import org.openimaj.image.Image;
import org.openimaj.image.analyser.ImageAnalyser;
import org.openimaj.image.feature.local.keypoints.Keypoint;
import org.openimaj.image.pixel.sampling.RectangleSampler;
import org.openimaj.image.processing.convolution.FGaussianConvolve;
import org.openimaj.image.processor.SinglebandImageProcessor.Processable;
import org.openimaj.math.geometry.shape.Rectangle;
import org.openimaj.util.array.ArrayUtils;
import org.openimaj.util.pair.IntObjectPair;
public class PatchFeature <IMAGE extends Image<Float, IMAGE> & Processable<Float, FImage, IMAGE>> implements ImageAnalyser<IMAGE> {
float size;
private LocalFeatureList<LocalFeatureImpl<Location, FloatFV>> patchList;
public PatchFeature(float size) {
this.size = size;
patchList = new MemoryLocalFeatureList<>();
}
public void analyseImage(IMAGE image) {
patchList.clear();
RectangleSampler sampler = new RectangleSampler(image, 4f, 4f, size, size);
Iterator<Rectangle> rects = sampler.allRectangles().iterator();
while (rects.hasNext()) {
Rectangle rect = rects.next();
IMAGE rImg = image.extractROI(rect).normalise();
Float[] pvF = new Float[rImg.getWidth()* rImg.getHeight()];
rImg.getPixelVector(pvF);
float[] pvf = new float[rImg.getWidth()* rImg.getHeight()];
for (int i = 0; i < pvF.length; i++) { pvf[i] = pvF[i].floatValue(); }
FloatFV fv = new FloatFV(pvf);
patchList.add(new LocalFeatureImpl<Location, FloatFV>(new SpatialLocation(rect.x, rect.y),fv));
}
}
public LocalFeatureList<LocalFeatureImpl<Location, FloatFV>> getPatchKeypoints() {
return patchList;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment