package uk.ac.soton.ecs.zmk1g19; import org.openimaj.data.dataset.VFSGroupDataset; import org.openimaj.data.dataset.VFSListDataset; import org.openimaj.experiment.dataset.split.GroupedRandomSplitter; import org.openimaj.feature.DoubleFV; import org.openimaj.feature.FeatureExtractor; import org.openimaj.image.FImage; import org.openimaj.image.feature.dense.gradient.dsift.DenseSIFT; import org.openimaj.image.feature.dense.gradient.dsift.PyramidDenseSIFT; import org.openimaj.ml.annotation.Annotator; import org.openimaj.ml.annotation.bayes.NaiveBayesAnnotator; import org.openimaj.ml.annotation.linear.LiblinearAnnotator; import org.openimaj.ml.clustering.assignment.HardAssigner; import org.openimaj.ml.kernel.HomogeneousKernelMap; import org.openimaj.util.pair.IntFloatPair; import uk.ac.soton.ecs.zmk1g19.classifiers.LiblinearFactory; import uk.ac.soton.ecs.zmk1g19.classifiers.NaiveBayesFactory; import uk.ac.soton.ecs.zmk1g19.classifiers.VoteClassifier; import uk.ac.soton.ecs.zmk1g19.featureExtractors.GISTFeatureExtractor; import uk.ac.soton.ecs.zmk1g19.featureExtractors.HOGFeatureExtractor; import uk.ac.soton.ecs.zmk1g19.featureExtractors.PHOWExtractor; import uk.ac.soton.ecs.zmk1g19.utils.AccuracyTest; import uk.ac.soton.ecs.zmk1g19.utils.HardAssignerFactory; import java.io.IOException; import java.util.HashMap; /** * Run 3: Voting Ensemble using Naive Bayes and LiblinearAnnotator * with a combination of feature extractors * * @author Sofia Kisiala (zmk1g19) * @author Harry Nelson (hjn2g19) * @author Max Burgess (mwmb1g19) * @author Anan Venkatesh (av1u19) * @author Fergus Adams (fhwa1g19) */ public class Run3 { public static void run(VFSGroupDataset training, VFSListDataset testing) throws IOException { GroupedRandomSplitter split = new GroupedRandomSplitter<>(training, 80, 0, 20); // Setup HOG extractor HOGFeatureExtractor hogFeatureExtractor = HOGFeatureExtractor.getDefault(); // Setup GIST extractor GISTFeatureExtractor gistFeatureExtractor = GISTFeatureExtractor.getDefault(); // Setup PHOW extractor DenseSIFT dsift = new DenseSIFT(5, 7); PyramidDenseSIFT pdsift = new PyramidDenseSIFT(dsift, 6f, 7); HardAssigner hardAssigner = HardAssignerFactory.trainQuantiser(split.getTrainingDataset(), pdsift); PHOWExtractor phowExtractor = new PHOWExtractor(pdsift, hardAssigner); // Setup Kernel Map and wrapped extractors HomogeneousKernelMap homogeneousKernelMap = new HomogeneousKernelMap(HomogeneousKernelMap.KernelType.Intersection, 0.2, HomogeneousKernelMap.WindowType.Uniform); FeatureExtractor wrappedGistExtractor = homogeneousKernelMap.createWrappedExtractor(gistFeatureExtractor); FeatureExtractor wrappedHogExtractor = homogeneousKernelMap.createWrappedExtractor(hogFeatureExtractor); FeatureExtractor wrappedPHOWExtractor = homogeneousKernelMap.createWrappedExtractor(phowExtractor); // Make Liblinear Annotators LiblinearAnnotator liblinearGIST = LiblinearFactory.createLiblinear(wrappedGistExtractor, training); LiblinearAnnotator liblinearHOG = LiblinearFactory.createLiblinear(wrappedHogExtractor, training); LiblinearAnnotator liblinearPHOW = LiblinearFactory.createLiblinear(wrappedPHOWExtractor, training); // Make Naive Bayes Annotators NaiveBayesAnnotator bayesUnwrappedGIST = NaiveBayesFactory.createNaiveBayes(gistFeatureExtractor, training); NaiveBayesAnnotator bayesUnrwappedPHOW = NaiveBayesFactory.createNaiveBayes(phowExtractor, training); NaiveBayesAnnotator bayesPHOW = NaiveBayesFactory.createNaiveBayes(wrappedPHOWExtractor, training); // Add to HashMap with a weighting HashMap, Float> annotators = new HashMap<>(); annotators.put(liblinearGIST, 0.6f); annotators.put(liblinearHOG, 0.5f); annotators.put(liblinearPHOW, 0.7f); annotators.put(bayesUnwrappedGIST, 0.6f); annotators.put(bayesUnrwappedPHOW, 0.6f); annotators.put(bayesPHOW, 0.6f); // Create vote classifier VoteClassifier voteClassifier = VoteClassifier.createVoteClassifier(annotators); App.writeAnnotatorOutput(voteClassifier, "run3", testing); } }