Search in sources :

Example 6 with SceneGraphImage

use of edu.stanford.nlp.scenegraph.image.SceneGraphImage in project CoreNLP by stanfordnlp.

the class SceneGraphSentenceMatcher method main.

public static void main(String[] args) throws IOException {
    String filename = args[0];
    BufferedReader reader = IOUtils.readerFromString(filename);
    List<SceneGraphImage> images = Generics.newLinkedList();
    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        SceneGraphImage img = SceneGraphImage.readFromJSON(line);
        if (img == null) {
            continue;
        }
        images.add(img);
    }
}
Also used : SceneGraphImage(edu.stanford.nlp.scenegraph.image.SceneGraphImage) BufferedReader(java.io.BufferedReader)

Example 7 with SceneGraphImage

use of edu.stanford.nlp.scenegraph.image.SceneGraphImage in project CoreNLP by stanfordnlp.

the class SceneGraphImageFilter method main.

public static void main(String[] args) throws IOException {
    String filename = args[0];
    int threshold = Integer.parseInt(args[1]);
    BufferedReader reader = IOUtils.readerFromString(filename);
    List<SceneGraphImage> images = Generics.newLinkedList();
    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        SceneGraphImage img = SceneGraphImage.readFromJSON(line);
        if (img == null) {
            continue;
        }
        images.add(img);
    }
    countAll(images);
    filterRegions(images, threshold);
    for (SceneGraphImage img : images) {
        if (!img.regions.isEmpty()) {
            System.out.println(img.toJSON());
        }
    }
}
Also used : SceneGraphImage(edu.stanford.nlp.scenegraph.image.SceneGraphImage) BufferedReader(java.io.BufferedReader)

Example 8 with SceneGraphImage

use of edu.stanford.nlp.scenegraph.image.SceneGraphImage in project CoreNLP by stanfordnlp.

the class SceneGraphImageFilter method filterRegions.

private static void filterRegions(List<SceneGraphImage> images, int threshold) {
    int regionCount = 0;
    int filterCount = 0;
    int imgCount = 0;
    int removedEntireImgCount = 0;
    int removedPartialImgCount = 0;
    for (SceneGraphImage img : images) {
        imgCount++;
        List<SceneGraphImageRegion> toDelete = Generics.newLinkedList();
        for (SceneGraphImageRegion region : img.regions) {
            regionCount++;
            boolean delete = false;
            for (SceneGraphImageAttribute attr : region.attributes) {
                if (attributeCounter.getCount(attr.attributeLemmaGloss()) < threshold || entityCounter.getCount(attr.subjectLemmaGloss()) < threshold) {
                    delete = true;
                    break;
                }
            }
            if (delete) {
                toDelete.add(region);
                continue;
            }
            for (SceneGraphImageRelationship reln : region.relationships) {
                if (entityCounter.getCount(reln.objectLemmaGloss()) < threshold || entityCounter.getCount(reln.subjectLemmaGloss()) < threshold || relationCounter.getCount(reln.predicateLemmaGloss()) < threshold) {
                    delete = true;
                    break;
                }
            }
            if (delete) {
                toDelete.add(region);
                continue;
            }
        }
        for (SceneGraphImageRegion region : toDelete) {
            img.removeRegion(region);
            filterCount++;
        }
        if (!toDelete.isEmpty()) {
            removedPartialImgCount++;
        }
        if (img.regions.isEmpty()) {
            removedEntireImgCount++;
        }
    }
    System.err.printf("%d\t%f\t%f\t%f %n", filterCount, filterCount * 100.0 / regionCount, removedPartialImgCount * 100.0 / imgCount, removedEntireImgCount * 100.0 / imgCount);
}
Also used : SceneGraphImageRelationship(edu.stanford.nlp.scenegraph.image.SceneGraphImageRelationship) SceneGraphImage(edu.stanford.nlp.scenegraph.image.SceneGraphImage) SceneGraphImageRegion(edu.stanford.nlp.scenegraph.image.SceneGraphImageRegion) SceneGraphImageAttribute(edu.stanford.nlp.scenegraph.image.SceneGraphImageAttribute)

Example 9 with SceneGraphImage

use of edu.stanford.nlp.scenegraph.image.SceneGraphImage in project CoreNLP by stanfordnlp.

the class BoWSceneGraphParser method loadImages.

/**
 * Loads scene graphs from JSON file.
 *
 * @param filename Path to JSON file.
 * @return List with images parsed to SceneGraphImage.
 * @throws IOException
 */
public static List<SceneGraphImage> loadImages(String filename) throws IOException {
    List<SceneGraphImage> images = Generics.newLinkedList();
    BufferedReader reader = IOUtils.readerFromString(filename);
    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        SceneGraphImage img = SceneGraphImage.readFromJSON(line);
        if (img == null) {
            continue;
        }
        images.add(img);
    }
    return images;
}
Also used : SceneGraphImage(edu.stanford.nlp.scenegraph.image.SceneGraphImage) BufferedReader(java.io.BufferedReader)

Example 10 with SceneGraphImage

use of edu.stanford.nlp.scenegraph.image.SceneGraphImage in project CoreNLP by stanfordnlp.

the class ObjectSceneGraphParser method main.

public static void main(String[] args) throws IOException {
    ObjectSceneGraphParser parser = new ObjectSceneGraphParser(args[1]);
    BufferedReader reader = IOUtils.readerFromString(args[0]);
    SceneGraphEvaluation eval = new SceneGraphEvaluation();
    String evalFilePrefix = args[2];
    String embeddingsPath = args[3];
    parser.embeddings = new Embedding(embeddingsPath);
    PrintWriter predWriter = null;
    PrintWriter goldWriter = null;
    if (evalFilePrefix != null) {
        String predEvalFilePath = evalFilePrefix + ".smatch";
        String goldEvalFilePath = evalFilePrefix + "_gold.smatch";
        predWriter = IOUtils.getPrintWriter(predEvalFilePath);
        goldWriter = IOUtils.getPrintWriter(goldEvalFilePath);
    }
    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        SceneGraphImage img = SceneGraphImage.readFromJSON(line);
        if (img == null) {
            continue;
        }
        for (SceneGraphImageRegion region : img.regions) {
            SemanticGraph sg = region.getEnhancedSemanticGraph();
            SceneGraph scene = parser.parse(sg);
            System.out.println(scene.toJSON(img.id, img.url, region.phrase));
            if (evalFilePrefix != null) {
                eval.toSmatchString(scene, region, predWriter, goldWriter);
            }
        }
    }
}
Also used : SceneGraphImage(edu.stanford.nlp.scenegraph.image.SceneGraphImage) BufferedReader(java.io.BufferedReader) Embedding(edu.stanford.nlp.neural.Embedding) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) SceneGraphImageRegion(edu.stanford.nlp.scenegraph.image.SceneGraphImageRegion) PrintWriter(java.io.PrintWriter)

Aggregations

SceneGraphImage (edu.stanford.nlp.scenegraph.image.SceneGraphImage)20 SceneGraphImageRegion (edu.stanford.nlp.scenegraph.image.SceneGraphImageRegion)13 BufferedReader (java.io.BufferedReader)13 SceneGraphImageAttribute (edu.stanford.nlp.scenegraph.image.SceneGraphImageAttribute)5 SceneGraphImageRelationship (edu.stanford.nlp.scenegraph.image.SceneGraphImageRelationship)5 PrintWriter (java.io.PrintWriter)5 CoreLabel (edu.stanford.nlp.ling.CoreLabel)4 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)4 Embedding (edu.stanford.nlp.neural.Embedding)3 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)2 IndexedWord (edu.stanford.nlp.ling.IndexedWord)2 RVFDatum (edu.stanford.nlp.ling.RVFDatum)2 ClassicCounter (edu.stanford.nlp.stats.ClassicCounter)2 Triple (edu.stanford.nlp.util.Triple)2 Dataset (edu.stanford.nlp.classify.Dataset)1 KNNClassifierFactory (edu.stanford.nlp.classify.KNNClassifierFactory)1 LinearClassifierFactory (edu.stanford.nlp.classify.LinearClassifierFactory)1 RVFDataset (edu.stanford.nlp.classify.RVFDataset)1 QNMinimizer (edu.stanford.nlp.optimization.QNMinimizer)1 LexicalizedParser (edu.stanford.nlp.parser.lexparser.LexicalizedParser)1