-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo.py
195 lines (155 loc) · 7.01 KB
/
demo.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
from __future__ import print_function, division
import torch
from torch.autograd import Variable
from torchvision.transforms import Normalize
from model.cnn_geometric_model import CNNGeometricPearson
from image.normalization import NormalizeImageDict, normalize_image
from util.torch_util import BatchTensorToVars, str_to_bool
# from util.checkboard import createCheckBoard
from geotnf.transformation import GeometricTnf
# from geotnf.point_tnf import *
import matplotlib.pyplot as plt
from skimage import io
import cv2
import numpy as np
import warnings
from collections import OrderedDict
import pickle
from functools import partial
import time
start_time = time.time()
warnings.filterwarnings('ignore')
# torch.cuda.set_device(1)
### Parameter
feature_extraction_cnn = 'resnet101'
if feature_extraction_cnn=='vgg':
model_homo_path = ''
elif feature_extraction_cnn=='resnet101':
model_aff_path = 'trained_models/resnet36_myproc_1_new_cor_fefr_4p5.pth.tar'
model_aff_path2 = 'trained_models/resnet101_epo81_lr4p4_rm11.pth.tar'
target_image_path='datasets/tgt15.jpg'
source_image_path='datasets/src15.jpg'
### Load models
use_cuda = torch.cuda.is_available()
do_aff = not model_aff_path2 == ''\
# Create model
print('Creating CNN model...')
if do_aff:
model_aff = CNNGeometricPearson(use_cuda=use_cuda, geometric_model='affine', feature_extraction_cnn=feature_extraction_cnn)\
pickle.load = partial(pickle.load, encoding="latin1")
pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
# Load trained weights
print('Loading trained model weights...')
if do_aff:
checkpoint = torch.load(model_aff_path, map_location=lambda storage, loc: storage)
checkpoint2 = torch.load(model_aff_path2, map_location=lambda storage, loc: storage)
model_dict = model_aff.FeatureExtraction.state_dict()
for name, param in model_dict.items():
model_dict[name].copy_(checkpoint['state_dict'][
'FeatureExtraction.' + name])
model_dict = model_aff.FeatureClassification.state_dict()
for name, param in model_dict.items():
model_dict[name].copy_(checkpoint['state_dict'][
'FeatureClassification.' + name])
model_dict = model_aff.FeatureExtraction2.state_dict()
for name, param in model_dict.items():
model_dict[name].copy_(checkpoint2['state_dict'][
'FeatureExtraction.' + name])
model_dict = model_aff.FeatureRegression.state_dict()
for name, param in model_dict.items():
model_dict[name].copy_(checkpoint2['state_dict'][
'FeatureRegression.' + name])
### Create image transformers
affTnf = GeometricTnf(geometric_model='affine', use_cuda=use_cuda)
### Load and preprocess images
resizeCNN = GeometricTnf(out_h=240, out_w=240, use_cuda=False)
affTnf_origin = GeometricTnf(out_h=1080, out_w=1080, use_cuda=False)
affTnf_Demo = GeometricTnf(out_h=540, out_w=540, use_cuda=False)
normalizeTnf = Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
def Im2Tensor(image):
image = np.expand_dims(image.transpose((2, 0, 1)), 0)
image = torch.Tensor(image.astype(np.float32) / 255.0)
image_var = Variable(image, requires_grad=False)
if use_cuda:
image_var = image_var.cuda()
return image_var
def preprocess_image(image):
# convert to torch Variable
image = np.expand_dims(image.transpose((2, 0, 1)), 0)
image = torch.Tensor(image.astype(np.float32) / 255.0)
image_var = Variable(image, requires_grad=False)
# Resize image using bilinear sampling with identity affine tnf
image_var = resizeCNN(image_var)
# Normalize image
image_var = normalize_image(image_var)
return image_var
def preprocess_image_Demo(image):
# convert to torch Variable
image = np.expand_dims(image.transpose((2, 0, 1)), 0)
image = torch.Tensor(image.astype(np.float32) / 255.0)
image_var = Variable(image, requires_grad=False)
# Resize image using bilinear sampling with identity affine tnf
image_var = affTnf_Demo(image_var)
# Normalize image
image_var = normalize_image(image_var)
return image_var
def preprocess_image_Origin(image):
# convert to torch Variable
image = np.expand_dims(image.transpose((2, 0, 1)), 0)
image = torch.Tensor(image.astype(np.float32) / 255.0)
image_var = Variable(image, requires_grad=False)
# Resize image using bilinear sampling with identity affine tnf
image_var = affTnf_origin(image_var)
# Normalize image
image_var = normalize_image(image_var)
return image_var
source_image = io.imread(source_image_path)
target_image = io.imread(target_image_path)
source_image_var = preprocess_image(source_image)
source_image_var_orgin = preprocess_image_Origin(source_image)
source_image_var_demo = preprocess_image_Demo(source_image)
target_image_var = preprocess_image(target_image)
target_image = np.float32(target_image/255.)
if use_cuda:
source_image_var = source_image_var.cuda()
source_image_var_demo = source_image_var_demo.cuda()
source_image_var_orgin = source_image_var_orgin.cuda()
target_image_var = target_image_var.cuda()
batch = {'source_image': source_image_var, 'target_image':target_image_var, 'source_image_demo':source_image_var_demo, 'origin_image':source_image_var_orgin}
resizeTgt = GeometricTnf(out_h=target_image.shape[0], out_w=target_image.shape[1], use_cuda = use_cuda)
resizeTgt_demo = GeometricTnf(out_h=540, out_w=540, use_cuda = use_cuda)
### Evaluate model
if do_aff:
model_aff.eval()
# Evaluate models
if do_aff:
theta_aff = model_aff(batch)
warped_image_aff = affTnf(batch['source_image'], theta_aff.view(-1, 2, 3))
### Process result
if do_aff:
result_aff = affTnf(Im2Tensor(source_image), theta_aff.view(-1,2,3))
warped_image_aff_np = resizeTgt(result_aff).squeeze(0).transpose(0,1).transpose(1,2).cpu().detach().numpy()
# io.imsave('results/aff.jpg', warped_image_aff_np)
result_aff_demo = affTnf_Demo(Im2Tensor(source_image), theta_aff.view(-1,2,3))
warped_image_aff_np_demo = resizeTgt_demo(result_aff_demo).squeeze(0).transpose(0,1).transpose(1,2).cpu().detach().numpy()
io.imsave('aff_demo.jpg', warped_image_aff_np_demo)
print()
print("# ====================================== #")
print("# <Execution Time> #")
print("# - %.4s seconds - #" %(time.time() - start_time))
print("# ====================================== #")
# Create checkboard
if do_aff:
aff_checkboard = createCheckBoard(warped_image_aff_np, target_image)
io.imsave('aff_checkboard.jpg', aff_checkboard)
N_subplots = 3
fig, axs = plt.subplots(1, N_subplots)
axs[0].imshow(source_image)
axs[0].set_title('src')
axs[1].imshow(target_image)
axs[1].set_title('tgt')
axs[2].imshow(warped_image_aff_np)
axs[2].set_title('aff')
for i in range(N_subplots):
axs[i].axis('off')
plt.show()