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
091                                data.put(id, new DoubleFV(vec));
092                        }
093                        br.close();
094                }
095
096                @Override
097                public DoubleFV extractFeature(ImageWrapper object) {
098                        // HistogramModel hm = new HistogramModel(4,4,4);
099                        // hm.estimateModel(object.getImage());
100                        // return hm.histogram;
101                        return data.get(object.getID());
102                }
103        }
104
105        public static void main(String[] args) throws IOException, JRException {
106                final Corel5kDataset alldata = new Corel5kDataset();
107
108                final StandardCorel5kSplit split = new StandardCorel5kSplit();
109                split.split(alldata);
110
111                final ListDataset<CorelAnnotatedImage> training = split.getTrainingDataset();
112
113                // UniformRandomAnnotator<ImageWrapper, String> ann = new
114                // UniformRandomAnnotator<ImageWrapper, String>(new PriorChooser());
115                final DenseLinearTransformAnnotator<ImageWrapper, String> ann = new DenseLinearTransformAnnotator<ImageWrapper, String>(
116                                315, new HistogramExtractor());
117                ann.train(DatasetAdaptors.asList(training));
118
119                // for (CorelAnnotatedImage img : split.getTestDataset()) {
120                // List<AutoAnnotation<String>> anns = ann.annotate(img.getObject());
121                // MBFImage imgf = img.getObject();
122                // imgf.processInplace(new ResizeProcessor(400, 400));
123                // imgf.drawText(anns.get(0).toString(), 20, 20,
124                // HersheyFont.TIMES_BOLD,20);
125                // DisplayUtilities.display(imgf);
126                // }
127
128                final AnnotationEvaluator<ImageWrapper, String> eval = new AnnotationEvaluator<ImageWrapper, String>(ann,
129                                split.getTestDataset());
130
131                // ClassificationEvaluator<ROCAnalysisResult<String>, String,
132                // ImageWrapper> classEval = eval.newClassificationEvaluator(new
133                // ROCAnalyser<ImageWrapper, String>());
134                // Map<ImageWrapper, ClassificationResult<String>> classRes =
135                // classEval.evaluate();
136                // ROCAnalysisResult<String> classAnalysis =
137                // classEval.analyse(classRes);
138                // System.out.println(classAnalysis);
139
140                final RetrievalEvaluator<IREvalResult, ImageWrapper, String> retEval = eval
141                                .newRetrievalEvaluator(new IREvalAnalyser<String, ImageWrapper>());
142                final Map<String, List<ImageWrapper>> retRes = retEval.evaluate();
143                final IREvalResult retAnalysis = retEval.analyse(retRes);
144                System.out.println(retAnalysis);
145
146                // final InputStream inputStream =
147                // IREvalResult.class.getResourceAsStream("IREvalSummaryReport.jrxml");
148                // final ArrayList<IREvalResult> list = new ArrayList<IREvalResult>();
149                // list.add(retAnalysis);
150                // final JRBeanCollectionDataSource beanColDataSource = new
151                // JRBeanCollectionDataSource(list);
152                //
153                // final Map parameters = new HashMap();
154                //
155                // final JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
156                // final JasperReport jasperReport =
157                // JasperCompileManager.compileReport(jasperDesign);
158                // final JasperPrint jasperPrint =
159                // JasperFillManager.fillReport(jasperReport, parameters,
160                // beanColDataSource);
161                // JasperExportManager.exportReportToPdfFile(jasperPrint,
162                // "test_jasper.pdf");
163        }
164}