forked from PruneTruong/DenseMatching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_selection.py
254 lines (223 loc) · 14.9 KB
/
model_selection.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import os.path as osp
import torch
import os
from models.GLUNet.GLU_Net import GLUNetModel
from models.PWCNet.pwc_net import PWCNetModel
from models.PDCNet.PDCNet import PDCNet_vgg16
from models.GLUNet.Semantic_GLUNet import SemanticGLUNetModel
from models.semantic_matching_models.SFNet import SFNet, SFNetWithBin
from models.semantic_matching_models.NCNet import NCNetWithBin, ImMatchNet
from models.semantic_matching_models.cats import CATs
def load_network(net, checkpoint_path=None, **kwargs):
"""Loads a network checkpoint file.
args:
net: network architecture
checkpoint_path
outputs:
net: loaded network
"""
if not os.path.isfile(checkpoint_path):
raise ValueError('The checkpoint that you chose does not exist, {}'.format(checkpoint_path))
# Load checkpoint
checkpoint_dict = torch.load(checkpoint_path, map_location='cpu')
if 'state_dict' in checkpoint_dict:
checkpoint_dict = checkpoint_dict['state_dict']
net.load_state_dict(checkpoint_dict, strict=False)
return net
model_type = ['GLUNet', 'GLUNet_interp',
'GLUNet_GOCor', 'PWCNet', 'PWCNet_GOCor',
'GLUNet_GOCor_star', 'PDCNet', 'PDCNet_plus',
'GLUNet_star', 'WarpCGLUNet', 'SemanticGLUNet', 'WarpCSemanticGLUNet', 'WarpCGLUNet_interp',
'UAWarpC',
'SFNet', 'PWarpCSFNet_WS', 'PWarpCSFNet_SS', 'NCNet', 'PWarpCNCNet_WS', 'PWarpCNCNet_SS',
'CATs', 'PWarpCCATs_SS', 'CATs_ft_features', 'PWarpCCATs_ft_features_SS',
]
pre_trained_model_types = ['static', 'dynamic', 'chairs_things', 'chairs_things_ft_sintel', 'megadepth',
'megadepth_stage1', 'pfpascal', 'spair']
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def select_model(model_name, pre_trained_model_type, arguments, global_optim_iter, local_optim_iter,
path_to_pre_trained_models='pre_trained_models/'):
"""
Select, construct and load model
args:
model_name
pre_trained_model_type
global_optim_iter
local_optim_iter
path_to_pre_trained_models
output:
network: constructed and loaded network
"""
print('Model: {}\nPre-trained-model: {}'.format(model_name, pre_trained_model_type))
if model_name not in model_type:
raise ValueError(
'The model that you chose does not exist, you chose {}'.format(model_name))
if 'GOCor' in model_name or 'PDCNet' in model_name:
print('GOCor: Local iter {}'.format(local_optim_iter))
print('GOCor: Global iter {}'.format(global_optim_iter))
'''
if pre_trained_model_type not in pre_trained_model_types:
raise ValueError(
'The pre trained model that you chose does not exist, you chose {}'.format(pre_trained_model_types))
'''
estimate_uncertainty = False
if model_name == 'GLUNet':
# GLU-Net uses a global feature correlation layer followed by a cyclic consistency post-processing.
# local cost volumes are computed by feature correlation layers
network = GLUNetModel(iterative_refinement=True, global_corr_type='feature_corr_layer',
normalize='relu_l2norm', cyclic_consistency=True,
local_corr_type='feature_corr_layer')
elif model_name == 'GLUNet_interp':
network = GLUNetModel(iterative_refinement=True, global_corr_type='feature_corr_layer',
normalize='relu_l2norm', cyclic_consistency=True,
local_corr_type='feature_corr_layer', use_interp_instead_of_deconv=True)
elif model_name == 'GLUNet_GOCor':
'''
Default for global and local gocor arguments:
global_gocor_arguments = {'optim_iter':3, 'num_features': 512, 'init_step_length': 1.0,
'init_filter_reg': 1e-2, 'min_filter_reg': 1e-5, 'steplength_reg': 0.0,
'num_dist_bins':10, 'bin_displacement': 0.5, 'init_gauss_sigma_DIMP':1.0,
'v_minus_act': 'sigmoid', 'v_minus_init_factor': 4.0
'apply_query_loss': False, 'reg_kernel_size': 3,
'reg_inter_dim': 1, 'reg_output_dim': 1.0}
local_gocor_arguments= {'optim_iter':3, 'num_features': 512, 'search_size': 9, 'init_step_length': 1.0,
'init_filter_reg': 1e-2, 'min_filter_reg': 1e-5, 'steplength_reg': 0.0,
'num_dist_bins':10, 'bin_displacement': 0.5, 'init_gauss_sigma_DIMP':1.0,
'v_minus_act': 'sigmoid', 'v_minus_init_factor': 4.0
'apply_query_loss': False, 'reg_kernel_size': 3,
'reg_inter_dim': 1, 'reg_output_dim': 1.0}
'''
# for global gocor, we apply L_r and L_q within the optimizer module
global_gocor_arguments = {'optim_iter': global_optim_iter, 'apply_query_loss': True,
'reg_kernel_size': 3, 'reg_inter_dim': 16, 'reg_output_dim': 16}
# for global gocor, we apply L_r only
local_gocor_arguments = {'optim_iter': local_optim_iter}
network = GLUNetModel(iterative_refinement=True, global_corr_type='GlobalGOCor',
global_gocor_arguments=global_gocor_arguments, normalize='leakyrelu',
local_corr_type='LocalGOCor', local_gocor_arguments=local_gocor_arguments,
same_local_corr_at_all_levels=True)
elif model_name == 'PWCNet':
# PWC-Net uses a feature correlation layer at each pyramid level
network = PWCNetModel(local_corr_type='feature_corr_layer')
elif model_name == 'PWCNet_GOCor':
local_gocor_arguments = {'optim_iter': local_optim_iter}
# We instead replace the feature correlation layers by Local GOCor modules
network = PWCNetModel(local_corr_type='LocalGOCor', local_gocor_arguments=local_gocor_arguments,
same_local_corr_at_all_levels=False)
elif model_name == 'GLUNet_GOCor_star':
# different mapping and flow decoders, features are also finetuned with two VGG copies
# for global gocor, we apply L_r and L_q within the optimizer module
global_gocor_arguments = {'optim_iter': global_optim_iter, 'steplength_reg': 0.1, 'apply_query_loss': True,
'reg_kernel_size': 3, 'reg_inter_dim': 16, 'reg_output_dim': 16}
# for global gocor, we apply L_r only
local_gocor_arguments = {'optim_iter': local_optim_iter, 'steplength_reg': 0.1}
network = GLUNetModel(iterative_refinement=True, cyclic_consistency=False, global_corr_type='GlobalGOCor',
global_gocor_arguments=global_gocor_arguments, normalize='leakyrelu',
local_corr_type='LocalGOCor', local_gocor_arguments=local_gocor_arguments,
same_local_corr_at_all_levels=True, give_flow_to_refinement_module=True,
local_decoder_type='OpticalFlowEstimatorResidualConnection',
global_decoder_type='CMDTopResidualConnection', make_two_feature_copies=True)
elif model_name == 'PDCNet' or model_name == 'PDCNet_plus':
estimate_uncertainty = True
# for global gocor, we apply L_r and L_q within the optimizer module
global_gocor_arguments = {'optim_iter': global_optim_iter, 'steplength_reg': 0.1, 'train_label_map': False,
'apply_query_loss': True,
'reg_kernel_size': 3, 'reg_inter_dim': 16, 'reg_output_dim': 16}
# for global gocor, we apply L_r only
local_gocor_arguments = {'optim_iter': local_optim_iter, 'steplength_reg': 0.1}
if model_name == 'PDCNet':
network = PDCNet_vgg16(global_corr_type='GlobalGOCor', global_gocor_arguments=global_gocor_arguments,
normalize='leakyrelu', same_local_corr_at_all_levels=True,
local_corr_type='LocalGOCor', local_gocor_arguments=local_gocor_arguments,
local_decoder_type='OpticalFlowEstimatorResidualConnection',
global_decoder_type='CMDTopResidualConnection',
corr_for_corr_uncertainty_decoder='corr',
give_layer_before_flow_to_uncertainty_decoder=True,
var_2_plus=520 ** 2, var_2_plus_256=256 ** 2, var_1_minus_plus=1.0, var_2_minus=2.0)
else:
network = PDCNet_vgg16(global_corr_type='GlobalGOCor', global_gocor_arguments=global_gocor_arguments,
normalize='leakyrelu', same_local_corr_at_all_levels=True,
local_corr_type='LocalGOCor', local_gocor_arguments=local_gocor_arguments,
local_decoder_type='OpticalFlowEstimatorResidualConnection',
global_decoder_type='CMDTopResidualConnection',
corr_for_corr_uncertainty_decoder='corr',
give_layer_before_flow_to_uncertainty_decoder=True,
var_2_plus=520 ** 2, var_2_plus_256=256 ** 2, var_1_minus_plus=1.0, var_2_minus=2.0,
make_two_feature_copies=True)
elif model_name == 'UAWarpC':
estimate_uncertainty = True
# probabilistic model with a single mode. Uncertainty predictors are based on PDCNet. Does not use GOCor.
# the predictions from the LNet (pyramid network taking 256x256 images) are scaled to original resolution
network = PDCNet_vgg16(global_corr_type='global_corr', normalize='relu_l2norm',
cyclic_consistency=True, local_decoder_type='OpticalFlowEstimatorResidualConnection',
global_decoder_type='CMDTopResidualConnection',
use_interp_instead_of_deconv=True, scale_low_resolution=True,
corr_for_corr_uncertainty_decoder='corr', estimate_one_mode=True,
laplace_distr=False, # Gaussian distribution
give_layer_before_flow_to_uncertainty_decoder=True)
elif model_name == 'GLUNet_star' or model_name == 'WarpCGLUNet':
# replaced the DenseNet connections in original network by residual connections to make the network lighter.
network = GLUNetModel(iterative_refinement=True, global_corr_type='feature_corr_layer',
normalize='relu_l2norm', cyclic_consistency=True,
local_corr_type='feature_corr_layer',
local_decoder_type='OpticalFlowEstimatorResidualConnection',
global_decoder_type='CMDTopResidualConnection')
elif model_name == 'WarpCGLUNet_interp':
network = GLUNetModel(iterative_refinement=True, global_corr_type='feature_corr_layer',
normalize='relu_l2norm', cyclic_consistency=True,
local_corr_type='feature_corr_layer',
local_decoder_type='OpticalFlowEstimatorResidualConnection',
global_decoder_type='CMDTopResidualConnection', use_interp_instead_of_deconv=True)
elif model_name == 'SemanticGLUNet' or model_name == 'WarpCSemanticGLUNet':
network = SemanticGLUNetModel(iterative_refinement=True)
# ########################## PWarpC Semantic networks #####################################################
# the architecture is the unchanged compared to original works, only the training is different (and the
# inference strategy in some cases).
elif model_name == 'SFNet':
network = SFNet(forward_pass_strategy='flow', inference_strategy='softargmax_padding')
elif model_name == 'PWarpCSFNet_SS':
network = SFNet(forward_pass_strategy='corr_prediction_no_kernel', inference_strategy='argmax')
elif model_name == 'PWarpCSFNet_WS':
network = SFNetWithBin(forward_pass_strategy='corr_prediction_no_kernel', inference_strategy='argmax')
elif model_name == 'NCNet' or model_name == 'PWarpCNCNet_SS':
network = ImMatchNet(inference_strategy='argmax')
elif model_name == 'PWarpCNCNet_WS':
network = NCNetWithBin(inference_strategy='argmax')
elif 'CATs' in model_name:
# similar to original work, we use softargmax as the inference_strategy. This is because the kp loss is the
# EPE after applying softargmax.
network = CATs(forward_pass_strategy='flow_prediction', inference_strategy='softargmax')
else:
raise NotImplementedError('the model that you chose does not exist: {}'.format(model_name))
if path_to_pre_trained_models.endswith('.pth') or path_to_pre_trained_models.endswith('.pth.tar') \
or path_to_pre_trained_models.endswith('.pt'):
# if the path already corresponds to a checkpoint path, we use it directly
checkpoint_fname = path_to_pre_trained_models
else:
# it is the path to the directory containing all checkpoints.
checkpoint_fname = osp.join(path_to_pre_trained_models, model_name + '_{}'.format(pre_trained_model_type)
+ '.pth')
if not os.path.exists(checkpoint_fname):
checkpoint_fname = checkpoint_fname + '.tar'
if not os.path.exists(checkpoint_fname):
raise ValueError('The checkpoint that you chose does not exist, {}'.format(checkpoint_fname))
network = load_network(network, checkpoint_path=checkpoint_fname)
network.eval()
network = network.to(device)
# define inference arguments
if arguments.network_type == 'PDCNet' or arguments.network_type == 'PDCNet_plus':
# define inference parameters for PDC-Net and particularly the ones needed for multi-stage alignment
network.set_inference_parameters(confidence_R=arguments.confidence_map_R,
ransac_thresh=arguments.ransac_thresh,
multi_stage_type=arguments.multi_stage_type,
mask_type_for_2_stage_alignment=arguments.mask_type,
homography_visibility_mask=arguments.homography_visibility_mask,
list_resizing_ratios=arguments.scaling_factors,
compute_cyclic_consistency_error=arguments.compute_cyclic_consistency_error)
'''
to plot GOCor weights
if model_name == 'GLUNet_GOCor':
network.corr.corr_module.filter_optimizer._plot_weights(save_dir='evaluation/')
network.local_corr.filter_optimizer._plot_weights(save_dir='evaluation/')
'''
return network, estimate_uncertainty