Search in sources :

Example 1 with MergeFeature

use of com.joliciel.jochre.boundaries.features.MergeFeature in project jochre by urieli.

the class Jochre method doCommandEvaluateFull.

/**
 * Evaluate a suite of split/merge models and letter guessing model.
 *  @param criteria
 *          for selecting the evaluation corpus
 * @param save
 *          whether or not the letter guesses should be saved
 */
public void doCommandEvaluateFull(CorpusSelectionCriteria criteria, boolean save, File outputDir, MostLikelyWordChooser wordChooser, String suffix, List<DocumentObserver> observers) throws IOException {
    String baseName = jochreSession.getLetterModelPath().substring(0, jochreSession.getLetterModelPath().indexOf("."));
    if (baseName.lastIndexOf("/") > 0)
        baseName = baseName.substring(baseName.lastIndexOf("/") + 1);
    ClassificationModel letterModel = jochreSession.getLetterModel();
    List<String> letterFeatureDescriptors = letterModel.getFeatureDescriptors();
    LetterFeatureParser letterFeatureParser = new LetterFeatureParser();
    Set<LetterFeature<?>> letterFeatures = letterFeatureParser.getLetterFeatureSet(letterFeatureDescriptors);
    LetterGuesser letterGuesser = new LetterGuesser(letterFeatures, letterModel.getDecisionMaker());
    ClassificationModel splitModel = jochreSession.getSplitModel();
    if (splitModel == null)
        throw new IllegalArgumentException("Missing parameter: jochre.image-analyser.split-model");
    List<String> splitFeatureDescriptors = splitModel.getFeatureDescriptors();
    SplitFeatureParser splitFeatureParser = new SplitFeatureParser();
    Set<SplitFeature<?>> splitFeatures = splitFeatureParser.getSplitFeatureSet(splitFeatureDescriptors);
    SplitCandidateFinder splitCandidateFinder = new SplitCandidateFinder(jochreSession);
    splitCandidateFinder.setMinDistanceBetweenSplits(5);
    ShapeSplitter shapeSplitter = new RecursiveShapeSplitter(splitCandidateFinder, splitFeatures, splitModel.getDecisionMaker(), jochreSession);
    ClassificationModel mergeModel = jochreSession.getMergeModel();
    if (mergeModel == null)
        throw new IllegalArgumentException("Missing parameter: jochre.image-analyser.merge-model");
    List<String> mergeFeatureDescriptors = mergeModel.getFeatureDescriptors();
    MergeFeatureParser mergeFeatureParser = new MergeFeatureParser();
    Set<MergeFeature<?>> mergeFeatures = mergeFeatureParser.getMergeFeatureSet(mergeFeatureDescriptors);
    ShapeMerger shapeMerger = new ShapeMerger(mergeFeatures, mergeModel.getDecisionMaker());
    BoundaryDetector boundaryDetector = null;
    String boundaryDetectorTypeName = jochreSession.getConfig().getConfig("jochre.boundaries").getString("boundary-detector-type");
    BoundaryDetectorType boundaryDetectorType = BoundaryDetectorType.valueOf(boundaryDetectorTypeName);
    switch(boundaryDetectorType) {
        case LetterByLetter:
            boundaryDetector = new LetterByLetterBoundaryDetector(shapeSplitter, shapeMerger, jochreSession);
            break;
        case Deterministic:
            boundaryDetector = new DeterministicBoundaryDetector(shapeSplitter, shapeMerger, jochreSession);
            break;
    }
    ImageAnalyser imageAnalyser = new BeamSearchImageAnalyser(boundaryDetector, letterGuesser, wordChooser, jochreSession);
    LetterValidator letterValidator = new ComponentCharacterValidator(jochreSession);
    OriginalShapeLetterAssigner shapeLetterAssigner = new OriginalShapeLetterAssigner();
    shapeLetterAssigner.setEvaluate(true);
    shapeLetterAssigner.setSave(save);
    shapeLetterAssigner.setLetterValidator(letterValidator);
    shapeLetterAssigner.setSingleLetterMethod(false);
    imageAnalyser.addObserver(shapeLetterAssigner);
    ErrorLogger errorLogger = new ErrorLogger(jochreSession);
    Writer errorWriter = null;
    File errorFile = new File(outputDir, baseName + suffix + "errors.txt");
    errorFile.delete();
    errorWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(errorFile, true), "UTF8"));
    errorLogger.setErrorWriter(errorWriter);
    imageAnalyser.addObserver(errorLogger);
    JochreCorpusImageProcessor imageProcessor = new JochreCorpusImageProcessor(criteria, jochreSession);
    imageProcessor.addObserver(imageAnalyser);
    for (DocumentObserver observer : observers) imageProcessor.addObserver(observer);
    imageProcessor.process();
    LOG.debug("F-score for " + jochreSession.getLetterModelPath() + ": " + shapeLetterAssigner.getFScoreCalculator().getTotalFScore());
    String modelFileName = baseName + suffix + "_full";
    File fscoreFile = new File(outputDir, modelFileName + "_fscores.csv");
    Writer fscoreWriter = errorWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fscoreFile, true), jochreSession.getCsvEncoding()));
    shapeLetterAssigner.getFScoreCalculator().writeScoresToCSV(fscoreWriter);
}
Also used : LetterByLetterBoundaryDetector(com.joliciel.jochre.boundaries.LetterByLetterBoundaryDetector) RecursiveShapeSplitter(com.joliciel.jochre.boundaries.RecursiveShapeSplitter) OriginalShapeLetterAssigner(com.joliciel.jochre.analyser.OriginalShapeLetterAssigner) BeamSearchImageAnalyser(com.joliciel.jochre.analyser.BeamSearchImageAnalyser) ImageAnalyser(com.joliciel.jochre.analyser.ImageAnalyser) DeterministicBoundaryDetector(com.joliciel.jochre.boundaries.DeterministicBoundaryDetector) SplitFeature(com.joliciel.jochre.boundaries.features.SplitFeature) BufferedWriter(java.io.BufferedWriter) MergeFeatureParser(com.joliciel.jochre.boundaries.features.MergeFeatureParser) LetterValidator(com.joliciel.jochre.letterGuesser.LetterValidator) JochreCorpusImageProcessor(com.joliciel.jochre.graphics.JochreCorpusImageProcessor) LetterFeature(com.joliciel.jochre.letterGuesser.features.LetterFeature) LetterFeatureParser(com.joliciel.jochre.letterGuesser.features.LetterFeatureParser) RecursiveShapeSplitter(com.joliciel.jochre.boundaries.RecursiveShapeSplitter) TrainingCorpusShapeSplitter(com.joliciel.jochre.boundaries.TrainingCorpusShapeSplitter) ShapeSplitter(com.joliciel.jochre.boundaries.ShapeSplitter) SplitCandidateFinder(com.joliciel.jochre.boundaries.SplitCandidateFinder) MergeFeature(com.joliciel.jochre.boundaries.features.MergeFeature) OriginalBoundaryDetector(com.joliciel.jochre.boundaries.OriginalBoundaryDetector) BoundaryDetector(com.joliciel.jochre.boundaries.BoundaryDetector) LetterByLetterBoundaryDetector(com.joliciel.jochre.boundaries.LetterByLetterBoundaryDetector) DeterministicBoundaryDetector(com.joliciel.jochre.boundaries.DeterministicBoundaryDetector) DocumentObserver(com.joliciel.jochre.doc.DocumentObserver) SplitFeatureParser(com.joliciel.jochre.boundaries.features.SplitFeatureParser) LetterGuesser(com.joliciel.jochre.letterGuesser.LetterGuesser) ErrorLogger(com.joliciel.jochre.analyser.ErrorLogger) TrainingCorpusShapeMerger(com.joliciel.jochre.boundaries.TrainingCorpusShapeMerger) ShapeMerger(com.joliciel.jochre.boundaries.ShapeMerger) FileOutputStream(java.io.FileOutputStream) BeamSearchImageAnalyser(com.joliciel.jochre.analyser.BeamSearchImageAnalyser) OutputStreamWriter(java.io.OutputStreamWriter) ComponentCharacterValidator(com.joliciel.jochre.letterGuesser.ComponentCharacterValidator) File(java.io.File) ClassificationModel(com.joliciel.talismane.machineLearning.ClassificationModel) UnknownWordListWriter(com.joliciel.jochre.lexicon.UnknownWordListWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) LexiconErrorWriter(com.joliciel.jochre.lexicon.LexiconErrorWriter) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with MergeFeature

use of com.joliciel.jochre.boundaries.features.MergeFeature in project jochre by urieli.

the class Jochre method doCommandEvaluateMerge.

/**
 * Evaluate the letter merging model on its own.
 *
 * @param criteria
 *          for selecting the portion of the corpus to evaluate
 */
public void doCommandEvaluateMerge(CorpusSelectionCriteria criteria) throws IOException {
    ClassificationModel mergeModel = jochreSession.getMergeModel();
    if (mergeModel == null)
        throw new IllegalArgumentException("Missing parameter: jochre.image-analyser.merge-model");
    List<String> mergeFeatureDescriptors = mergeModel.getFeatureDescriptors();
    MergeFeatureParser mergeFeatureParser = new MergeFeatureParser();
    Set<MergeFeature<?>> mergeFeatures = mergeFeatureParser.getMergeFeatureSet(mergeFeatureDescriptors);
    JochreCorpusGroupReader groupReader = new JochreCorpusGroupReader(jochreSession);
    groupReader.setSelectionCriteria(criteria);
    ShapeMerger merger = new ShapeMerger(mergeFeatures, mergeModel.getDecisionMaker());
    MergeEvaluator evaluator = new MergeEvaluator(jochreSession);
    FScoreCalculator<String> fScoreCalculator = evaluator.evaluate(groupReader, merger);
    LOG.debug("" + fScoreCalculator.getTotalFScore());
}
Also used : MergeFeatureParser(com.joliciel.jochre.boundaries.features.MergeFeatureParser) MergeEvaluator(com.joliciel.jochre.boundaries.MergeEvaluator) MergeFeature(com.joliciel.jochre.boundaries.features.MergeFeature) TrainingCorpusShapeMerger(com.joliciel.jochre.boundaries.TrainingCorpusShapeMerger) ShapeMerger(com.joliciel.jochre.boundaries.ShapeMerger) ClassificationModel(com.joliciel.talismane.machineLearning.ClassificationModel) JochreCorpusGroupReader(com.joliciel.jochre.graphics.JochreCorpusGroupReader)

Example 3 with MergeFeature

use of com.joliciel.jochre.boundaries.features.MergeFeature in project jochre by urieli.

the class Jochre method doCommandTrainMerge.

/**
 * Train the letter merging model.
 *
 * @param featureDescriptors
 *          feature descriptors for training
 * @param multiplier
 *          if &gt; 0, will be used to equalize the outcomes
 * @param criteria
 *          the criteria used to select the training corpus
 */
public void doCommandTrainMerge(List<String> featureDescriptors, int multiplier, CorpusSelectionCriteria criteria) {
    if (jochreSession.getMergeModelPath() == null)
        throw new RuntimeException("Missing argument: mergeModel");
    if (featureDescriptors == null)
        throw new JochreException("features is required");
    File mergeModelFile = new File(jochreSession.getMergeModelPath());
    mergeModelFile.getParentFile().mkdirs();
    MergeFeatureParser mergeFeatureParser = new MergeFeatureParser();
    Set<MergeFeature<?>> mergeFeatures = mergeFeatureParser.getMergeFeatureSet(featureDescriptors);
    ClassificationEventStream corpusEventStream = new JochreMergeEventStream(criteria, mergeFeatures, jochreSession);
    if (multiplier > 0) {
        corpusEventStream = new OutcomeEqualiserEventStream(corpusEventStream, multiplier);
    }
    ModelTrainerFactory modelTrainerFactory = new ModelTrainerFactory();
    ClassificationModelTrainer trainer = modelTrainerFactory.constructTrainer(jochreSession.getConfig());
    ClassificationModel mergeModel = trainer.trainModel(corpusEventStream, featureDescriptors);
    mergeModel.persist(mergeModelFile);
}
Also used : MergeFeatureParser(com.joliciel.jochre.boundaries.features.MergeFeatureParser) ClassificationEventStream(com.joliciel.talismane.machineLearning.ClassificationEventStream) ModelTrainerFactory(com.joliciel.talismane.machineLearning.ModelTrainerFactory) JochreException(com.joliciel.jochre.utils.JochreException) ClassificationModelTrainer(com.joliciel.talismane.machineLearning.ClassificationModelTrainer) MergeFeature(com.joliciel.jochre.boundaries.features.MergeFeature) File(java.io.File) JochreMergeEventStream(com.joliciel.jochre.boundaries.JochreMergeEventStream) OutcomeEqualiserEventStream(com.joliciel.talismane.machineLearning.OutcomeEqualiserEventStream) ClassificationModel(com.joliciel.talismane.machineLearning.ClassificationModel)

Aggregations

MergeFeature (com.joliciel.jochre.boundaries.features.MergeFeature)3 MergeFeatureParser (com.joliciel.jochre.boundaries.features.MergeFeatureParser)3 ClassificationModel (com.joliciel.talismane.machineLearning.ClassificationModel)3 ShapeMerger (com.joliciel.jochre.boundaries.ShapeMerger)2 TrainingCorpusShapeMerger (com.joliciel.jochre.boundaries.TrainingCorpusShapeMerger)2 File (java.io.File)2 BeamSearchImageAnalyser (com.joliciel.jochre.analyser.BeamSearchImageAnalyser)1 ErrorLogger (com.joliciel.jochre.analyser.ErrorLogger)1 ImageAnalyser (com.joliciel.jochre.analyser.ImageAnalyser)1 OriginalShapeLetterAssigner (com.joliciel.jochre.analyser.OriginalShapeLetterAssigner)1 BoundaryDetector (com.joliciel.jochre.boundaries.BoundaryDetector)1 DeterministicBoundaryDetector (com.joliciel.jochre.boundaries.DeterministicBoundaryDetector)1 JochreMergeEventStream (com.joliciel.jochre.boundaries.JochreMergeEventStream)1 LetterByLetterBoundaryDetector (com.joliciel.jochre.boundaries.LetterByLetterBoundaryDetector)1 MergeEvaluator (com.joliciel.jochre.boundaries.MergeEvaluator)1 OriginalBoundaryDetector (com.joliciel.jochre.boundaries.OriginalBoundaryDetector)1 RecursiveShapeSplitter (com.joliciel.jochre.boundaries.RecursiveShapeSplitter)1 ShapeSplitter (com.joliciel.jochre.boundaries.ShapeSplitter)1 SplitCandidateFinder (com.joliciel.jochre.boundaries.SplitCandidateFinder)1 TrainingCorpusShapeSplitter (com.joliciel.jochre.boundaries.TrainingCorpusShapeSplitter)1