001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.image.annotation.evaluation.dataset;
031
032import java.io.BufferedReader;
033import java.io.File;
034import java.io.FileReader;
035import java.io.IOException;
036import java.util.HashMap;
037import java.util.List;
038import java.util.Map;
039import java.util.Scanner;
040
041import net.sf.jasperreports.engine.JRException;
042
043import org.openimaj.data.dataset.ListBackedDataset;
044import org.openimaj.data.dataset.ListDataset;
045import org.openimaj.experiment.dataset.util.DatasetAdaptors;
046import org.openimaj.experiment.evaluation.retrieval.RetrievalEvaluator;
047import org.openimaj.experiment.evaluation.retrieval.analysers.IREvalAnalyser;
048import org.openimaj.experiment.evaluation.retrieval.analysers.IREvalResult;
049import org.openimaj.feature.DoubleFV;
050import org.openimaj.feature.FeatureExtractor;
051import org.openimaj.ml.annotation.evaluation.AnnotationEvaluator;
052import org.openimaj.ml.annotation.linear.DenseLinearTransformAnnotator;
053
054public class Corel5kDataset extends ListBackedDataset<CorelAnnotatedImage> {
055        File baseDir = new File("/Users/jsh2/Data/corel-5k");
056        File imageDir = new File(baseDir, "images");
057        File metaDir = new File(baseDir, "metadata");
058
059        public Corel5kDataset() throws IOException {
060                for (final File f : imageDir.listFiles()) {
061                        if (f.getName().endsWith(".jpeg")) {
062                                final String id = f.getName().replace(".jpeg", "");
063
064                                data.add(new CorelAnnotatedImage(id, f, new File(metaDir, id + "_1.txt")));
065                        }
066                }
067        }
068
069        public static class HistogramExtractor implements FeatureExtractor<DoubleFV, ImageWrapper> {
070                Map<String, DoubleFV> data = new HashMap<String, DoubleFV>();
071
072                public HistogramExtractor() throws IOException {
073                        final BufferedReader br = new BufferedReader(new FileReader("/Users/jsh2/Data/corel-5k/BLOBS_data.txt"));
074                        String line;
075                        while ((line = br.readLine()) != null) {
076                                final Scanner sc = new Scanner(line);
077
078                                final String id = sc.nextInt() + "";
079                                final double[] vec = new double[500];
080
081                                while (sc.hasNext()) {
082                                        final String token = sc.next();
083                                        final double weight = Double.parseDouble(sc.next().replace(",", ""));
084
085                                        if (token.startsWith("blob")) {
086                                                final int blobId = Integer.parseInt(token.replace("blob[", "").replace("]", ""));
087                                                vec[blobId - 1] += weight;
088                                        }
089                                }
090                                sc.close();
091
092                                data.put(id, new DoubleFV(vec));
093                        }
094                        br.close();
095                }
096
097                @Override
098                public DoubleFV extractFeature(ImageWrapper object) {
099                        // HistogramModel hm = new HistogramModel(4,4,4);
100                        // hm.estimateModel(object.getImage());
101                        // return hm.histogram;
102                        return data.get(object.getID());
103                }
104        }
105
106        public static void main(String[] args) throws IOException, JRException {
107                final Corel5kDataset alldata = new Corel5kDataset();
108
109                final StandardCorel5kSplit split = new StandardCorel5kSplit();
110                split.split(alldata);
111
112                final ListDataset<CorelAnnotatedImage> training = split.getTrainingDataset();
113
114                // UniformRandomAnnotator<ImageWrapper, String> ann = new
115                // UniformRandomAnnotator<ImageWrapper, String>(new PriorChooser());
116                final DenseLinearTransformAnnotator<ImageWrapper, String> ann = new DenseLinearTransformAnnotator<ImageWrapper, String>(
117                                315, new HistogramExtractor());
118                ann.train(DatasetAdaptors.asList(training));
119
120                // for (CorelAnnotatedImage img : split.getTestDataset()) {
121                // List<AutoAnnotation<String>> anns = ann.annotate(img.getObject());
122                // MBFImage imgf = img.getObject();
123                // imgf.processInplace(new ResizeProcessor(400, 400));
124                // imgf.drawText(anns.get(0).toString(), 20, 20,
125                // HersheyFont.TIMES_BOLD,20);
126                // DisplayUtilities.display(imgf);
127                // }
128
129                final AnnotationEvaluator<ImageWrapper, String> eval = new AnnotationEvaluator<ImageWrapper, String>(ann,
130                                split.getTestDataset());
131
132                // ClassificationEvaluator<ROCAnalysisResult<String>, String,
133                // ImageWrapper> classEval = eval.newClassificationEvaluator(new
134                // ROCAnalyser<ImageWrapper, String>());
135                // Map<ImageWrapper, ClassificationResult<String>> classRes =
136                // classEval.evaluate();
137                // ROCAnalysisResult<String> classAnalysis =
138                // classEval.analyse(classRes);
139                // System.out.println(classAnalysis);
140
141                final RetrievalEvaluator<IREvalResult, ImageWrapper, String> retEval = eval
142                                .newRetrievalEvaluator(new IREvalAnalyser<String, ImageWrapper>());
143                final Map<String, List<ImageWrapper>> retRes = retEval.evaluate();
144                final IREvalResult retAnalysis = retEval.analyse(retRes);
145                System.out.println(retAnalysis);
146
147                // final InputStream inputStream =
148                // IREvalResult.class.getResourceAsStream("IREvalSummaryReport.jrxml");
149                // final ArrayList<IREvalResult> list = new ArrayList<IREvalResult>();
150                // list.add(retAnalysis);
151                // final JRBeanCollectionDataSource beanColDataSource = new
152                // JRBeanCollectionDataSource(list);
153                //
154                // final Map parameters = new HashMap();
155                //
156                // final JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
157                // final JasperReport jasperReport =
158                // JasperCompileManager.compileReport(jasperDesign);
159                // final JasperPrint jasperPrint =
160                // JasperFillManager.fillReport(jasperReport, parameters,
161                // beanColDataSource);
162                // JasperExportManager.exportReportToPdfFile(jasperPrint,
163                // "test_jasper.pdf");
164        }
165}