Search in sources :

Example 1 with Converter

use of org.vitrivr.cineast.core.extraction.decode.general.Converter in project cineast by vitrivr.

the class EvaluationRuntime method call.

/**
 * Executes the evaluation and returns a Triple that contains the number of files that were processed, skipped due to errors and skipped deliberately. The actual evaluation results are written to files.
 *
 * @return computed result
 * @throws EvaluationException if unable to compute a result
 * @see EvaluationConfig
 */
@Override
public Triple<Integer, Integer, Integer> call() throws EvaluationException, IOException {
    /* Tries to instantiate the converter. */
    final Converter converter = this.config.getConverter();
    if (converter == null) {
        throw new EvaluationException("Failed to instantiate the converter class.");
    }
    /* Instantiates the groundtruth and checks if it contains classes. */
    final Groundtruth gt = this.config.getGroundtruth();
    if (gt.numberOfClasses() == 0) {
        throw new EvaluationException(String.format("The specified ground truth '%s' does not contain any classes.", this.config.getClassfile()));
    }
    /* Updates the retrieval configuration. */
    Config.sharedConfig().getRetriever().setMaxResults(this.config.getSize());
    Config.sharedConfig().getRetriever().setResultsPerModule(this.config.getSize());
    /* Prepares the iterator for the test files. */
    final Iterator<Path> testfilesIterator;
    try {
        testfilesIterator = Files.walk(this.config.getTestfiles()).filter(p -> {
            try {
                return Files.exists(p) && Files.isRegularFile(p) && !Files.isHidden(p) && Files.isReadable(p);
            } catch (IOException e) {
                LOGGER.error("An IO exception occurred while testing the media file at '{}'.", p.toString(), LogHelper.getStackTrace(e));
                return false;
            }
        }).iterator();
    } catch (IOException exception) {
        throw new EvaluationException(String.format("Could not obtain test files under the specified path '%s'.", this.config.getTestfiles()));
    }
    /* Prepare folder structure per category. */
    for (String category : this.config.getCategories()) {
        Files.createDirectories(this.config.getResults().resolve(category));
    }
    /* Prepare a placeholder query-config. */
    final ReadableQueryConfig queryConfig = new ReadableQueryConfig(null);
    /* Prepare a random number generator that decides if a file should be used for evaluation or not. */
    final Random random = new Random();
    /* Perform evaluation for every file. */
    Path path;
    while (testfilesIterator.hasNext()) {
        path = testfilesIterator.next();
        if (random.nextBoolean() && config.getMode() == EvaluationConfig.EvaluationMode.RANDOM) {
            LOGGER.info("Randomly skipping file {}.", path);
            this.skipped += 1;
            continue;
        }
        /* Try to create a QueryContainer. If this fails, the file is skipped. */
        final AbstractQueryTermContainer container = converter.convert(path);
        if (container == null) {
            LOGGER.warn("Failed to convert the file {}. File is being skipped...", path.getFileName());
            this.error += 1;
            continue;
        }
        LOGGER.info("Starting evaluation for {}", path);
        for (String category : this.config.getCategories()) {
            List<SegmentScoreElement> scores = this.retrievalLogic.retrieve(container, category, queryConfig);
            EvaluationResult result = this.performEvaluation(scores, path, gt);
            this.writeToFile(category, result);
        }
        this.processed += 1;
    }
    return new ImmutableTriple<>(this.processed, this.error, this.skipped);
}
Also used : Path(java.nio.file.Path) AbstractQueryTermContainer(org.vitrivr.cineast.core.data.query.containers.AbstractQueryTermContainer) ImmutableTriple(org.apache.commons.lang3.tuple.ImmutableTriple) IOException(java.io.IOException) ReadableQueryConfig(org.vitrivr.cineast.core.config.ReadableQueryConfig) Random(java.util.Random) SegmentScoreElement(org.vitrivr.cineast.core.data.score.SegmentScoreElement) Converter(org.vitrivr.cineast.core.extraction.decode.general.Converter)

Aggregations

IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Random (java.util.Random)1 ImmutableTriple (org.apache.commons.lang3.tuple.ImmutableTriple)1 ReadableQueryConfig (org.vitrivr.cineast.core.config.ReadableQueryConfig)1 AbstractQueryTermContainer (org.vitrivr.cineast.core.data.query.containers.AbstractQueryTermContainer)1 SegmentScoreElement (org.vitrivr.cineast.core.data.score.SegmentScoreElement)1 Converter (org.vitrivr.cineast.core.extraction.decode.general.Converter)1