-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_vg_text_graphs.py
101 lines (79 loc) · 2.7 KB
/
create_vg_text_graphs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright 2020 Keren Ye, University of Pittsburgh
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import cv2
import json
import zipfile
from absl import app
from absl import flags
from absl import logging
import numpy as np
import tensorflow as tf
from multiprocessing import Pool
import sng_parser
flags.DEFINE_string('caption_annotations_file', '',
'Path to the caption annotations.')
flags.DEFINE_string('scenegraph_annotations_file', '',
'Path to the scenegraphs annotations.')
flags.DEFINE_integer('number_of_processes', 30, 'Number of threads.')
FLAGS = flags.FLAGS
def _extract_scene_graphs(image_id, captions):
"""Starts worker. """
logging.info('Processing %s.', image_id)
paired_captions = []
scene_graphs = []
for caption in captions:
caption = caption.strip().replace('\n', '').lower()
if not caption: continue
if caption[-1] != '.':
caption += '.'
paired_captions.append(caption)
scene_graphs.append(sng_parser.parse(caption))
return {
'image_id': image_id,
'captions': paired_captions,
'scene_graphs': scene_graphs,
}
def _create_scene_graphs(input_file, output_file):
"""Extracts scene graphs.
Args:
zip_file: ZIP file containing the image files.
"""
with tf.io.gfile.GFile(input_file, 'r') as f:
annots = json.load(f)
res_list = []
with Pool(processes=FLAGS.number_of_processes) as pool:
for i, annot in enumerate(annots):
image_id = annot['id']
captions = [x['phrase'] for x in annot['regions']]
res_list.append(
pool.apply_async(_extract_scene_graphs, (image_id, captions)))
pool.close()
pool.join()
data = []
for res in res_list:
data.append(res.get())
with tf.io.gfile.GFile(output_file, 'w') as fid:
json.dump(data, fid)
logging.info('Done')
def main(_):
logging.set_verbosity(logging.INFO)
_create_scene_graphs(FLAGS.caption_annotations_file,
FLAGS.scenegraph_annotations_file)
if __name__ == '__main__':
app.run(main)