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.ml.annotation; 031 032import java.util.List; 033import java.util.Set; 034 035import org.openimaj.data.dataset.cache.GroupedListCache; 036import org.openimaj.data.dataset.cache.InMemoryGroupedListCache; 037import org.openimaj.feature.FeatureExtractor; 038import org.openimaj.feature.IdentityFeatureExtractor; 039 040/** 041 * Adaptor that allows a {@link BatchAnnotator} to behave like a 042 * {@link IncrementalAnnotator} by caching extracted features and then 043 * performing training only when {@link #annotate(Object)} is called. 044 * <p> 045 * Because the features are cached, the internal annotator must rely on a 046 * {@link IdentityFeatureExtractor} or similar, and thus not perform any 047 * extraction itself. 048 * 049 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 050 * 051 * @param <OBJECT> 052 * Type of object 053 * @param <ANNOTATION> 054 * Type of annotation 055 * @param <FEATURE> 056 * Type of feature extracted and cached. 057 */ 058public class FeatureCachingIncrementalBatchAnnotator<OBJECT, ANNOTATION, FEATURE> 059extends IncrementalAnnotator<OBJECT, ANNOTATION> 060{ 061 BatchAnnotator<FEATURE, ANNOTATION> batchAnnotator; 062 GroupedListCache<ANNOTATION, FEATURE> featureCache; 063 private FeatureExtractor<FEATURE, OBJECT> extractor; 064 boolean isInvalid = true; 065 066 /** 067 * Construct with the given feature extractor and batch annotator, and use 068 * an in-memory cache. 069 * 070 * @param extractor 071 * the extractor 072 * @param batchAnnotator 073 * the annotator 074 */ 075 public FeatureCachingIncrementalBatchAnnotator(FeatureExtractor<FEATURE, OBJECT> extractor, 076 BatchAnnotator<FEATURE, ANNOTATION> batchAnnotator) 077 { 078 this.extractor = extractor; 079 this.featureCache = new InMemoryGroupedListCache<ANNOTATION, FEATURE>(); 080 this.batchAnnotator = batchAnnotator; 081 } 082 083 /** 084 * Construct with the given feature extractor and batch annotator, and use 085 * an in-memory cache. 086 * 087 * @param extractor 088 * the extractor 089 * @param batchAnnotator 090 * the annotator 091 * @param cache 092 * the cache 093 */ 094 public FeatureCachingIncrementalBatchAnnotator(FeatureExtractor<FEATURE, OBJECT> extractor, 095 BatchAnnotator<FEATURE, ANNOTATION> batchAnnotator, 096 GroupedListCache<ANNOTATION, FEATURE> cache) 097 { 098 this.extractor = extractor; 099 this.batchAnnotator = batchAnnotator; 100 this.featureCache = cache; 101 } 102 103 @Override 104 public void train(Annotated<OBJECT, ANNOTATION> annotated) { 105 final FEATURE fv = extractor.extractFeature(annotated.getObject()); 106 107 featureCache.add(annotated.getAnnotations(), fv); 108 isInvalid = true; 109 } 110 111 @Override 112 public void reset() { 113 featureCache.reset(); 114 isInvalid = true; 115 } 116 117 @Override 118 public Set<ANNOTATION> getAnnotations() { 119 return featureCache.getDataset().getGroups(); 120 } 121 122 @Override 123 public List<ScoredAnnotation<ANNOTATION>> annotate(OBJECT object) { 124 if (isInvalid) { 125 batchAnnotator.train(featureCache.getDataset()); 126 isInvalid = false; 127 } 128 129 return batchAnnotator.annotate(extractor.extractFeature(object)); 130 } 131}