forked from milesial/Pytorch-UNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict_kp_all.py
executable file
·260 lines (213 loc) · 11 KB
/
predict_kp_all.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
255
256
257
258
259
260
import argparse
import logging
import os
import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image
from torchvision import transforms
from utils.data_loading import BasicDataset
from unet import UNet
from utils.utils import plot_img_and_mask
def predict_img(net,
full_img,
device,
scale_factor=1,
out_threshold=0.5):
net.eval()
img = torch.from_numpy(BasicDataset.preprocess(None, full_img, scale_factor, is_mask=False))
img = img.unsqueeze(0)
img = img.to(device=device, dtype=torch.float32)
with torch.no_grad():
output = net(img).cpu()
return output
# output = F.interpolate(output, (full_img.size[1], full_img.size[0]), mode='bilinear')
# if net.n_classes > 1:
# mask = output.argmax(dim=1)
# else:
# mask = torch.sigmoid(output) > out_threshold
# return mask[0].long().squeeze().numpy()
def get_args():
parser = argparse.ArgumentParser(description='Predict masks from input images')
parser.add_argument('--model', '-m', default='MODEL.pth', metavar='FILE',
help='Specify the file in which the model is stored')
parser.add_argument('--input', '-i', metavar='INPUT', nargs='+', help='Filenames of input images', required=True)
parser.add_argument('--output', '-o', metavar='OUTPUT', nargs='+', help='Filenames of output images')
parser.add_argument('--viz', '-v', action='store_true',
help='Visualize the images as they are processed')
parser.add_argument('--no-save', '-n', action='store_true', help='Do not save the output masks')
parser.add_argument('--mask-threshold', '-t', type=float, default=0.5,
help='Minimum probability value to consider a mask pixel white')
parser.add_argument('--scale', '-s', type=float, default=1.0,
help='Scale factor for the input images')
parser.add_argument('--bilinear', action='store_true', default=False, help='Use bilinear upsampling')
parser.add_argument('--classes', '-c', type=int, default=2, help='Number of classes')
return parser.parse_args()
def get_output_filenames(args):
def _generate_name(fn):
return f'{os.path.splitext(fn)[0]}_OUT.png'
return args.output or list(map(_generate_name, args.input))
def mask_to_image(mask: np.ndarray, mask_values):
if isinstance(mask_values[0], list):
out = np.zeros((mask.shape[-2], mask.shape[-1], len(mask_values[0])), dtype=np.uint8)
elif mask_values == [0, 1]:
out = np.zeros((mask.shape[-2], mask.shape[-1]), dtype=bool)
else:
out = np.zeros((mask.shape[-2], mask.shape[-1]), dtype=np.uint8)
if mask.ndim == 3:
mask = np.argmax(mask, axis=0)
for i, v in enumerate(mask_values):
out[mask == i] = v
return Image.fromarray(out)
# def find_largest_polytope(v):
# v1 = 0
# v2 = 1
# v3 = 2
# v4 = 3
# largest_area = -1
# for i in range(v.shape[0]-3):
# for j in range(i+1,v.shape[0]-2):
# for k in range(j+1, v.shape[0]-1):
# for l in range(k+1, v.shape[0]):
# area = 1/2*((v[i,0]*v[j,1]+v[j,0]*v[k,1]+v[k,0]*v[l,1]+v[l,0]*v[i,1])-\
# (v[j,0]*v[i,1]+v[k,0]*v[j,1]+v[l,0]*v[k,1]+v[i,0]*v[l,1]))
# if area > largest_area:
# largest_area = area
# v1 = i
# v2 = j
# v3 = k
# v4 = l
# return hull_vertices[[v1, v2, v3, v4],:]
if __name__ == '__main__':
args = get_args()
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
in_files = args.input
out_files = get_output_filenames(args)
net = UNet(n_channels=3, n_classes=14, bilinear=args.bilinear)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
logging.info(f'Loading model {args.model}')
logging.info(f'Using device {device}')
net.to(device=device)
state_dict = torch.load(args.model, map_location=device)
# mask_values = state_dict.pop('mask_values', [0, 1])
net.load_state_dict(state_dict)
logging.info('Model loaded!')
all_kps = []
for idx in range(205, 11095):
filename = f'/home/younger/work/Pytorch-UNet/data/imgs/img_{idx}.png'
# for i, filename in enumerate(in_files):
logging.info(f'Predicting image {filename} ...')
img = Image.open(filename)
output = predict_img(net=net,
full_img=img,
scale_factor=args.scale,
out_threshold=args.mask_threshold,
device=device)
p0 = (((output[0,0,:,:]==torch.max(output[0,0,:,:])).nonzero())/args.scale).tolist()
p0[0].reverse()
p1 = (((output[0,1,:,:]==torch.max(output[0,1,:,:])).nonzero())/args.scale).tolist()
p1[0].reverse()
p2 = (((output[0,2,:,:]==torch.max(output[0,2,:,:])).nonzero())/args.scale).tolist()
p2[0].reverse()
p3 = (((output[0,3,:,:]==torch.max(output[0,3,:,:])).nonzero())/args.scale).tolist()
p3[0].reverse()
p4 = (((output[0,4,:,:]==torch.max(output[0,4,:,:])).nonzero())/args.scale).tolist()
p4[0].reverse()
p5 = (((output[0,5,:,:]==torch.max(output[0,5,:,:])).nonzero())/args.scale).tolist()
p5[0].reverse()
p6 = (((output[0,6,:,:]==torch.max(output[0,6,:,:])).nonzero())/args.scale).tolist()
p6[0].reverse()
p7 = (((output[0,7,:,:]==torch.max(output[0,7,:,:])).nonzero())/args.scale).tolist()
p7[0].reverse()
p8 = (((output[0,8,:,:]==torch.max(output[0,8,:,:])).nonzero())/args.scale).tolist()
p8[0].reverse()
p9 = (((output[0,9,:,:]==torch.max(output[0,9,:,:])).nonzero())/args.scale).tolist()
p9[0].reverse()
p10 = (((output[0,2+8,:,:]==torch.max(output[0,2+8,:,:])).nonzero())/args.scale).tolist()
p10[0].reverse()
p11 = (((output[0,3+8,:,:]==torch.max(output[0,3+8,:,:])).nonzero())/args.scale).tolist()
p11[0].reverse()
p12 = (((output[0,4+8,:,:]==torch.max(output[0,4+8,:,:])).nonzero())/args.scale).tolist()
p12[0].reverse()
p13 = (((output[0,5+8,:,:]==torch.max(output[0,5+8,:,:])).nonzero())/args.scale).tolist()
p13[0].reverse()
# p14 = (((output[0,6+8,:,:]==torch.max(output[0,6+8,:,:])).nonzero())/args.scale).tolist()
# p14[0].reverse()
# p15 = (((output[0,7+8,:,:]==torch.max(output[0,7+8,:,:])).nonzero())/args.scale).tolist()
# p15[0].reverse()
# heat_map = torch.sum(output[:,:14,:,:], axis=1)
kps = p0+p1+p2+p3+p4+p5+p6+p7+p8+p9+p10+p11+p12+p13
all_kps.append(kps)
np.save('estimation', np.array(all_kps))
# with open(f"/home/younger/work/Pytorch-UNet/data/label_kp/",'r') as fgt:
# kpgt = fgt.readlines()
# kpgt = [elem.replace(' ','').split(',') for elem in kpgt]
# kpgt = [[float(val) for val in elem] for elem in kpgt]
# kpgt = kpgt[:14]
# # print(np.max(mask))
# # print(np.min(mask))
# pixels = np.where(mask>0)
# # print(pixels)
# pixels_array = np.vstack((pixels[1],pixels[0])).T
# # print(pixels_array.shape)
# import matplotlib.pyplot as plt
# # plt.figure()
# # plt.imshow(mask)
# for i in range(16):
# plt.figure()
# plt.imshow(output[0,i,:,:])
# plt.figure()
# plt.imshow(heat_map[0,:,:])
# plt.figure()
# plt.imshow(img)
# plt.plot(np.array(kps)[:,0],np.array(kps)[:,1],'r*')
# import scipy.spatial
# hull = scipy.spatial.ConvexHull(pixels_array)
# # plt.plot(pixels_array[hull.vertices,0],pixels_array[hull.vertices,1],'r*')
# hull_vertices = pixels_array[hull.vertices,:]
# center_point = np.mean(hull_vertices,axis=0)
# # plt.plot(center_point[0], center_point[1], 'b*')
# pts = find_largest_polytope(hull_vertices)
# upleft = np.where((pts[:,0]<center_point[0]) & (pts[:,1]<center_point[1]))
# upleft_vertices = pts[upleft[0],:]
# plt.plot(upleft_vertices[:,0],upleft_vertices[:,1],'r*')
# bottomleft = np.where((pts[:,0]<center_point[0]) & (pts[:,1]>center_point[1]))
# bottomleft_vertices = pts[bottomleft[0],:]
# plt.plot(bottomleft_vertices[:,0],bottomleft_vertices[:,1],'g*')
# bottomright = np.where((pts[:,0]>center_point[0]) & (pts[:,1]>center_point[1]))
# bottomright_vertices = pts[bottomright[0],:]
# plt.plot(bottomright_verti+p8ight[0],:]
# plt.plot(upright_vertices[:,0],upright_vertices[:,1],'y*')
# # plt.plot(pts[:,0], pts[:,1],'g*')
# # bottomleft = np.where((hull_vertices[:,0]<center_point[0]) & (hull_vertices[:,1]>center_point[1]))
# # bottomleft_vertices = hull_vertices[bottomleft[0],:]
# # diff = bottomleft_vertices - center_point
# # tmp = np.argmax(np.lina+p8vertices[upleft[0],:]
# # diff = upleft_vertices - center_point
# # tmp = np.argmax(np.linalg.norm(diff,axis=1)*np.cos(np.arctan2(diff[:,1], diff[:,0]))**2*np.sin(np.arctan2(diff[:,1], diff[:,0]))**2)
# # upleft_vertex = upleft_vertices[tmp,:]
# # # plt.plot(hull_vertices[upleft,0], hull_vertices[upleft,1],'g*')
# # plt.plot(upleft_vertex[0], upleft_vertex[1], 'g*')
# # bottomright = np.where((hull_vertices[:,0]>center_point[0]) & (hull_vertices[:,1]>center_point[1]))
# # bottomright_vertices = hull_vertices[bottomright[0],:]
# # diff = bottomright_vertices - center_point
# # tmp = np.argmax(np.linalg.norm(diff,axis=1)*np.cos(np.arctan2(diff[:,1], diff[:,0]))**2*np.sin(np.arctan2(diff[:,1], diff[:,0]))**2)
# # bottomright_vertex = bottomright_vertices[tmp,:]
# # # plt.plot(hull_vertices[bottomright,0], hull_vertices[bottomright,1],'g*')
# # plt.plot(bottomright_vertex[0], bottomright_vertex[1], 'g*')
# # upright = np.where((hull_vertices[:,0]>center_point[0]) & (hull_vertices[:,1]<center_point[1]))
# # upright_vertices = hull_vertices[upright[0],:]
# # diff = upright_vertices - center_point
# # tmp = np.argmax(np.linalg.norm(diff,axis=1)*np.cos(np.arctan2(diff[:,1], diff[:,0]))**2*np.sin(np.arctan2(diff[:,1], diff[:,0]))**2)
# # upright_vertex = upright_vertices[tmp,:]
# # # plt.plot(hull_vertices[upright,0], hull_vertices[upright,1],'g*')
# # plt.plot(upright_vertex[0], upright_vertex[1], 'g*')
# plt.show()
# if not args.no_save:
# out_filename = out_files[i]
# result = mask_to_image(mask, mask_values)
# result.save(out_filename)
# logging.info(f'Mask saved to {out_filename}')
# if args.viz:
# logging.info(f'Visualizing results for image {filename}, close to continue...')
# plot_img_and_mask(img, mask)