-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_annotation_method.py
191 lines (147 loc) · 7.34 KB
/
eval_annotation_method.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
180
181
182
183
184
185
186
187
188
189
190
191
import os
import argparse
import pandas as pd
import numpy as np
import re
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
from datasets import AnnotationDataset
from util.mypath import Path
# interact policies
from interactions.mask import qnet_mask, rand_mask, oracle_mask, l2_mask, upper_bound_mask
from interactions.mulitple_annotations import oracle_oracle, rand_type, rand_rand, eva_vos
# EVA-VOS
from models.qnet import QualityNet
from ppo.ppo_agent import PPOAgent
# models
from feature_extractors import DINOFeatureExtractor, ViTFeatureExtractor, ResnetFeatureExtractor
from annotator import Annotator
from mivos.model.propagation.prop_net import PropagationNetwork
from mivos.model.fusion_net import FusionNet
""" Arguments loading """
parser = argparse.ArgumentParser()
parser.add_argument('--rounds', type=int, default=60, help='Rounds of interactions')
parser.add_argument('--policy', default='eva_vos', help='Policy for rounds')
parser.add_argument('--db', type=str, default='MOSE')
parser.add_argument('--encoder', type=str, default='resnet50', help='Only used with l2_mask policy')
parser.add_argument('--min-idx', type=int, help='From min-idx until max-idx')
parser.add_argument('--max-idx', type=int, help='From min-idx until max-idx')
# multiple annotations arguments
parser.add_argument('--types', nargs='+', help='Annotation types', default=['3clicks', 'mask']) # , 'click', 'bbox', 'mask', '3clicks', '10clicks', etc.
args = parser.parse_args()
assert args.db in {'MOSE', 'DAVIS_17'}
assert args.rounds >= 1 , "At least one round is required"
torch.autograd.set_grad_enabled(False)
"""Paths"""
if args.db =='MOSE':
db_root_path = Path.db_root_path(args.db)
imset = os.path.join(db_root_path, 'ImageSets', f'test.txt')
else:
db_root_path = Path.db_root_path(args.db)
imset = os.path.join(db_root_path, 'ImageSets/2017', f'val.txt')
prop_path = './model_weights/mivos/stcn.pth'
fusion_path = './model_weights/mivos/fusion.pth'
"""Models"""
prop_saved = torch.load(prop_path)
prop_model = PropagationNetwork().cuda().eval()
prop_model.load_state_dict(prop_saved)
fusion_saved = torch.load(fusion_path)
fusion_model = FusionNet().cuda().eval()
fusion_model.load_state_dict(fusion_saved)
second_type_transform = None
policy_str = f'{args.policy}'
if args.policy == 'qnet_mask' or args.policy == 'eva_vos':
qnet = QualityNet().cuda().eval()
qnet.load_state_dict(torch.load('./model_weights/qnet/qnet.pth'))
if args.policy == 'eva_vos':
rl_agent = PPOAgent(2, 'resnet18', './model_weights/rl_agent/model.pth')
annotator = Annotator()
elif args.policy == 'l2_mask':
if args.encoder.__contains__('dino'):
arch = args.encoder.split('_')[1] # e.g. dino_large
encoder = DINOFeatureExtractor(arch)
elif args.encoder.__contains__('vit'):
arch = args.encoder.split('_')[1] # e.g. vit_large
encoder = ViTFeatureExtractor(arch)
elif args.encoder.__contains__('resnet'):
# e.g. resnet50
encoder = ResnetFeatureExtractor(args.encoder)
else :
raise AttributeError(f'{args.encoder} is invalid!')
second_type_transform = encoder.transforms
policy_str += f'_{args.encoder}'
elif args.policy in {'oracle_oracle', 'rand_type', 'rand_rand'}:
avail_types = ['click', 'bbox', 'mask']
annotation_types = args.types
for annot_type in sorted(annotation_types):
if annot_type not in avail_types:
pattern = r'^\d+clicks$'
if not re.match(pattern, annot_type):
raise AttributeError('Invalid annotation type')
policy_str += f'_{annot_type}'
annotator = Annotator()
if args.policy.__contains__('type'):
assert len(annotation_types) == 1, f'Only one annotation type for {args.policy}'
annotation_type = annotation_types[0]
"""Dataset and Dataloader"""
db = AnnotationDataset(db_root_path, imset=imset, \
second_type_transform=second_type_transform, min_idx=args.min_idx, max_idx=args.max_idx
)
dataloader = DataLoader(db , batch_size=1)
if args.min_idx is not None and args.max_idx is not None:
policy_str += f'from_{args.min_idx}_to_{args.max_idx}'
"""Inference"""
policy_results = {
'video': [],
'mu_metric': [],
'annotation_time': [],
'round':[]
}
if args.policy == 'eva_vos':
policy_results['rl_values'] = []
policy_results['round_metrics'] = []
policy_results['annotated_frames'] = []
elif args.policy == 'oracle_oracle':
policy_results['round_metrics'] = []
policy_results['annotated_frames'] = []
if args.policy in {'oracle_oracle', 'rand_type', 'rand_rand', 'eva_vos'}:
policy_results['annotation_actions'] = []
for data in tqdm(dataloader, desc=f'{policy_str} at {args.db} with {args.rounds} rounds'):
video_name = data['info']['name']
### mask only
if args.policy == 'qnet_mask':
mu_metrics, annotation_times = qnet_mask(qnet, args.rounds, prop_model, fusion_model, data, 'j_and_f')
elif args.policy == 'rand_mask':
mu_metrics, annotation_times = rand_mask(args.rounds, prop_model, fusion_model, data, 'j_and_f')
elif args.policy == 'oracle_mask':
mu_metrics, annotation_times = oracle_mask(args.rounds, prop_model, fusion_model, data, 'j_and_f')
elif args.policy == 'l2_mask':
mu_metrics, annotation_times = l2_mask(encoder, args.rounds, prop_model, fusion_model, data, 'j_and_f')
elif args.policy == 'upper_bound_mask':
mu_metrics, annotation_times = upper_bound_mask(args.rounds, prop_model, fusion_model, data, 'j_and_f')
### mulitple annotations
elif args.policy == 'oracle_oracle':
mu_metrics, annotation_times, annotations_actions, round_metrics, annotated_frames = oracle_oracle(args.rounds, prop_model, fusion_model, data, annotator, annotation_types, 'j_and_f')
policy_results['round_metrics'].extend(round_metrics)
policy_results['annotated_frames'].extend(annotated_frames)
elif args.policy == 'rand_type':
mu_metrics, annotation_times, annotations_actions = rand_type(args.rounds, prop_model, fusion_model, data, annotator, annotation_type, 'j_and_f')
elif args.policy == 'rand_rand':
mu_metrics, annotation_times, annotations_actions = rand_rand(args.rounds, prop_model, fusion_model, data, annotator, annotation_types, 'j_and_f')
elif args.policy == 'eva_vos':
mu_metrics, annotation_times, rl_values, annotations_actions, round_metrics, annotated_frames = eva_vos(qnet, rl_agent, args.rounds, prop_model, fusion_model, data, annotator, eval_metric='j_and_f')
policy_results['rl_values'].extend(rl_values)
policy_results['round_metrics'].extend(round_metrics)
policy_results['annotated_frames'].extend(annotated_frames)
else:
raise AttributeError(f'Policy: {args.policy} is invalid!')
policy_results['video'].extend(video_name*len(mu_metrics))
policy_results['mu_metric'].extend(mu_metrics)
policy_results['annotation_time'].extend(annotation_times)
policy_results['round'].extend(np.arange(len(mu_metrics)))
if args.policy in {'oracle_oracle', 'rand_type', 'rand_rand', 'eva_vos'}:
policy_results['annotation_actions'].extend(annotations_actions)
os.makedirs(f'./Experiments/{args.db}', exist_ok=True)
df = pd.DataFrame.from_dict(policy_results)
df.to_csv(os.path.join(f'./Experiments/{args.db}', f'{policy_str}.csv'), index=False)