-
Notifications
You must be signed in to change notification settings - Fork 3
/
f30k_eval.py
179 lines (150 loc) · 6.83 KB
/
f30k_eval.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# -*- coding: utf-8 -*-
''' Inherent Python '''
import os
''' Third Libs '''
import torch
import numpy as np
from absl import app
from absl import flags
''' Local Libs '''
from datasets import f30ke_base
from datasets import F30KE_provider
from preprocess import image_detection_processer
from models.language import LanguageModel
from models.anchors_op import AnchorsOperator
from models.base_graph_op import BatchGraphOperator
from models.node_transformer import CrossModalNodeEncoderLayer, CrossModalContextMultiHeadedAttention, TwoDTransformationHead
from models.multi_modal_graph import CMGATLayerImp
from models.MDGT_framework import MDGTFramework
from learning import evaluation
from common_flags import CURRENT_PROJECT_DIR
from visualization import grounding_visualizer
from visualization import graph_visualizer
FLAGS = flags.FLAGS
flags.DEFINE_string('visualizations_dir',
os.path.join(CURRENT_PROJECT_DIR, "visualization",
"flikerE_demo"),
help="the visualization dir")
flags.DEFINE_string('backbone_visual_net_name',
"resnset18",
help="the backbone network used for the image")
flags.DEFINE_string('backbone_text_net_name',
"bert",
help="the backbone network used for the text")
flags.DEFINE_float("min_size", 800,
"the minimum size of the image in the transformer")
flags.DEFINE_float("max_size", 1333,
"the maximum size of the image in the transformer")
flags.DEFINE_list("image_mean", [0.485, 0.456, 0.406],
"the mean size of the image in the transformer")
flags.DEFINE_list("image_std", [0.229, 0.224, 0.225],
"the std size of the image in the transformer")
flags.DEFINE_list("anchor_sizes", [(256, )],
"the base anchor size used to generate boxes")
flags.DEFINE_list("anchor_intervals", [(256, 256)],
"the distance between the generated anchors")
flags.DEFINE_list("aspect_ratios", [(1.0)],
"the ratio of h/w used to generate different boxes")
flags.DEFINE_integer(
"graphs_node_neighbors_circle_level", 2,
"the edges of the neighbot circle in the initialization graph")
flags.DEFINE_list('featmap_names', ["avg_pool"],
help="the names of the feature maps that obtain the ROIs")
flags.DEFINE_list("roi_output_size", [5], "output size of the roi alignment")
flags.DEFINE_float("roi_sampling_ratio", 2, "the sampling ratio")
flags.DEFINE_string(
"eval_log_dir", os.path.join(CURRENT_PROJECT_DIR, "experiments",
"FlikerE"),
"the number of batches for one epoch")
flags.DEFINE_string(
'visualization_dir',
os.path.join(CURRENT_PROJECT_DIR, "visualization", "FlikerE"),
"The path of the visualization")
flags.DEFINE_float("eps", 0.0, "epsilon of label smoothing")
def unpack_batch_data(batches_data):
images_name = [bt_data[0] for bt_data in batches_data]
original_images = [bt_data[1] for bt_data in batches_data] # uint8 array
processed_images = [bt_data[2]
for bt_data in batches_data] # uint8, tensor
images_caption = [bt_data[3].caption for bt_data in batches_data]
images_caption_phrases = [
bt_data[3].caption_phrases for bt_data in batches_data
]
images_caption_phrase_bboxs = [
bt_data[3].caption_phrase_bboxs for bt_data in batches_data
]
images_caption_phrases_cate = [
bt_data[3].caption_phrases_cate for bt_data in batches_data
]
images_caption_phrases_cate_id = [
bt_data[3].caption_phrases_cate_id for bt_data in batches_data
]
return images_name, original_images, processed_images, images_caption_phrases, \
images_caption_phrase_bboxs, images_caption, images_caption_phrases_cate, images_caption_phrases_cate_id
def _main(argv):
f30k_bs = f30ke_base.F30KEBase(
dataset_dir=FLAGS.Flickr30KEntities_source_path)
f30k_bs.split_F30KE_dataset()
f30k_bs.integrate_data(split_wise=True, globally=True)
img_dec_processor = image_detection_processer.ImageDetectionProcessor(
dataset_name='f30k')
image_dec_transform_func = img_dec_processor.create_image_dec_processor(
resize_shape=[None, None],
target_shape=[None, None],
phase="val",
randomization=False,
normalization=True)
f30ke_provider = F30KE_provider.F30KProvider(
base_data=f30k_bs,
batch_size=1,
epoch_size=1,
num_data_loading_workers=FLAGS.num_data_loading_workers,
transform_image_dec_func=image_dec_transform_func,
transform_text_func=None,
phase="val")
lang_module = LanguageModel(
language_model_name=FLAGS.backbone_text_net_name)
anchors_operator = AnchorsOperator()
batch_phy_graph_op = BatchGraphOperator()
cross_modal_node_encode_layer = CrossModalNodeEncoderLayer(
size=512, node_feed_forward=None, dropout=0.2)
cross_modal_attn_model = CrossModalContextMultiHeadedAttention(com_dim=512,
dropout=0.2)
mm_graph = CMGATLayerImp(
cross_modal_node_encode_layer=cross_modal_node_encode_layer,
cross_modal_attn_model=cross_modal_attn_model,
num_in_node_features=512,
num_in_phrase_features=512,
num_out_node_features=1024,
num_out_phrase_features=512,
num_of_heads=1) # the number of heads do not impact the performance
transformation_head = TwoDTransformationHead(input_dims=1024,
coefficients_dim=4)
dgf_model = MDGTFramework(
backbone_visual_net_name=FLAGS.backbone_visual_net_name,
language_module=lang_module,
anchor_operator=anchors_operator,
physical_batch_graph_operator=batch_phy_graph_op,
mm_graph=mm_graph,
transformation_head=transformation_head,
flags=FLAGS)
# 6. visualization
gds_visualizer = grounding_visualizer.GroundingVisualizer(
visualization_dir=FLAGS.visualizations_dir, is_unique_created=True)
gph_visualizer = graph_visualizer.GraphVisualizer(
visualization_dir=FLAGS.visualizations_dir, is_unique_created=False)
# 5. build trainer
model_evaler = evaluation.ModelStrategyEvaluator(
eval_FLAGS=FLAGS,
num_iters=9,
visualizer=gds_visualizer,
graph_visualizer=gph_visualizer)
dgf_model = model_evaler.load_model(defined_model=dgf_model,
model_path="cvpr22_f30k_20.ph")
model_evaler.eval_onece(epoch_num=1,
main_model=dgf_model,
data_loader=f30ke_provider,
unpack_batch_data=unpack_batch_data)
if __name__ == "__main__":
app.run(_main)