-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.py
263 lines (200 loc) · 7.48 KB
/
util.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
261
262
263
import torch
import torch.utils.data as data_utils
from PIL import Image
import numpy as np
imfile = 'images/rgb.png'
gtfile = 'images/gt.png'
mean_image_file = 'images/mean.npy'
pix_threshold = 0.5 # percentage of allowed blank pixels in patches (for training)
# Opens the image, apply zero padding so that it can be cropped into WxW patches
def load_data_as_np(W):
im = Image.open(imfile)
gt = Image.open(gtfile)
# calculate new size to evenly crop
w_orig = im.size[0]
h_orig = im.size[1]
w_new = (w_orig + (W - w_orig%W))
h_new = (h_orig + (W - h_orig%W))
# create new image (and gt) with zero padding
im_new = Image.new(im.mode, (w_new, h_new), (0,0,0,0))
im_new.paste(im, im.getbbox())
gt_new = Image.new(gt.mode, (w_new, h_new), (255,255,255))
gt_new.paste(gt, gt.getbbox())
# allocate np array
x = np.zeros((int(w_new/W * h_new/W), 3, W, W))
y = np.zeros((int(w_new/W * h_new/W), W, W))
imCounter = 0
# start cropping
for w in range(0, w_new, W):
for h in range(0, h_new, W):
box = (w, h, w+W, h+W) # a WxW box at (w,h)
cropped_im = im_new.crop(box).convert('RGB')
cropped_gt = gt_new.crop(box).convert('1')
# do cropping
xim = np.array(cropped_im)
xgt = np.array(cropped_gt)
# TODO double check the dim ordering
x[imCounter, :, :, :] = np.rollaxis(xim, 2, 0)
y[imCounter, :, :] = xgt
imCounter = imCounter+1
return x, y
# Wrapper function for load_data_as_np, return Dataset object
def load_data_simple(W):
x, y = load_data_as_np(W)
# preprocess data, convert it to tensor, then into TensorDataset
x, y = preprocess(x, y)
x = torch.from_numpy(x)
y = torch.from_numpy(y)
x = x.type(torch.FloatTensor)
y = y.type(torch.LongTensor)
dataset = data_utils.TensorDataset(x, y)
return dataset
# Loads WxW patches (via load_data_as_np(W)) and removes "some" for training purposes
# returns Dataset object
def load_data_analyzed(W):
x, y = load_data_as_np(W)
x_analyzed = np.zeros_like(x)
y_analyzed = np.zeros_like(y)
imCounter = 0
# go through patches to eliminate ones that not conform to the criteria
for i in range(x.shape[0]):
# analyze cropped image with following criteria:
xim = x[i, :, :, :]
xgt = y[i, :, :]
# more than 50% of the image is blank
isNotEnoughData = (np.count_nonzero(xim) / np.size(xim)) < pix_threshold
# no houses on the cropped image
isNoGt = np.count_nonzero(xgt) == np.size(xgt)
if (isNoGt and isNotEnoughData):
continue
# TODO double check the dim ordering (PIL has rows-cols ordering)
x_analyzed[imCounter, :, :, :] = xim
y_analyzed[imCounter, :, :] = xgt
imCounter = imCounter+1
# trim the array
x_analyzed = x_analyzed[0:imCounter, :, :, :]
y_analyzed = y_analyzed[0:imCounter, :, :]
# save it to use for zero centering
save_mean_image(x_analyzed)
x_analyzed, y_analyzed = preprocess(x_analyzed, y_analyzed)
x_analyzed = torch.from_numpy(x_analyzed)
y_analyzed = torch.from_numpy(y_analyzed)
x_analyzed = x_analyzed.type(torch.FloatTensor)
y_analyzed = y_analyzed.type(torch.LongTensor)
dataset = data_utils.TensorDataset(x_analyzed, y_analyzed)
return dataset
# Opens the image, apply zero padding so that it can be cropped into WxW patches,
# apply augmentation according to the params
# stride_ratio: amount of stride in terms of W (see nested loops below)
def load_data_augmented(W, stride_ratio=1.0):
im = Image.open(imfile)
gt = Image.open(gtfile)
# calculate new size to evenly crop
w_orig = im.size[0]
h_orig = im.size[1]
w_new = (w_orig + (W - w_orig%W))
h_new = (h_orig + (W - h_orig%W))
# create new image (and gt) with zero padding
im_new = Image.new(im.mode, (w_new, h_new), (0,0,0,0))
im_new.paste(im, im.getbbox())
gt_new = Image.new(gt.mode, (w_new, h_new), (255,255,255))
gt_new.paste(gt, gt.getbbox())
# allocate np array
x = np.zeros((int(w_new/W * 1/stride_ratio * h_new/W * 1/stride_ratio), 3, W, W))
y = np.zeros((int(w_new/W * 1/stride_ratio * h_new/W * 1/stride_ratio), W, W))
imCounter = 0
# start cropping
stride = int(W*stride_ratio)
for w in range(0, w_new-stride, stride):
for h in range(0, h_new-stride, stride):
box = (w, h, w+W, h+W) # a WxW box at (w,h)
cropped_im = im_new.crop(box).convert('RGB')
cropped_gt = gt_new.crop(box).convert('1')
# do cropping
xim = np.array(cropped_im)
xgt = np.array(cropped_gt)
# more than 50% of the image is blank
isNotEnoughData = (np.count_nonzero(xim) / np.size(xim)) < pix_threshold
# no houses on the cropped image
isNoGt = np.count_nonzero(xgt) == np.size(xgt)
if (isNoGt and isNotEnoughData):
continue
# TODO double check the dim ordering
x[imCounter, :, :, :] = np.rollaxis(xim, 2, 0)
y[imCounter, :, :] = xgt
imCounter = imCounter+1
# trim the array
x = x[0:imCounter, :, :, :]
y = y[0:imCounter, :, :]
# save it to use for zero centering
save_mean_image(x)
x, y = preprocess(x, y)
x = torch.from_numpy(x)
y = torch.from_numpy(y)
x = x.type(torch.FloatTensor)
y = y.type(torch.LongTensor)
dataset = data_utils.TensorDataset(x, y)
return dataset
# Saves the mean image of a given set
def save_mean_image(x):
mean_image = np.mean(x, axis=0)
np.save(mean_image_file, mean_image)
# Loads the mean image, zero center the data using it
def preprocess(x, y):
# use the mean image obtained from training set
try:
mean_image = np.load(mean_image_file)
except IOError:
print("File not found: ", mean_image_file)
print("pix4d_util.load_data_analyzed(W) generates this file during training.")
return
x -= mean_image
# x = x/255.0 # also scale between [-1, 1]
# invert y, so that "1" is house and 0 is background
y = np.logical_not(y).astype(int)
return x, y
# Given network output + ground truth, calculates precision/recall/F1 score
# Both predictions and gt assumed Tensor
def evaluate(predictions, gt):
acc = (predictions == gt)
accuracy = acc.sum() / acc.numel()
tp = np.logical_and(predictions, gt).sum() # True positives
precision = tp / predictions.sum() # tp over all house predictions
recall = tp / gt.sum() # tp over all houses from ground truth
F1score = 2*precision*recall / (precision + recall)
return accuracy, precision, recall, F1score
# Loads and returns the zero padded image
# (TODO redundant code with load_data_simple(W))
def get_zero_padded_img(W):
im = Image.open(imfile)
# calculate new size to evenly crop
w_orig = im.size[0]
h_orig = im.size[1]
w_new = (w_orig + (W - w_orig%W))
h_new = (h_orig + (W - h_orig%W))
# create new image (and gt) with zero padding
im_new = Image.new(im.mode, (w_new, h_new), (0,0,0,0))
im_new.paste(im, im.getbbox())
return im_new
# This function creates an overlaid image with predictions.
# predictions are assumed a numpy array
def save_predictions_image(predictions, imprefix):
W = predictions.shape[-1]
# prepare an overlaid image
zero_padded_im = get_zero_padded_img(W) # get zero padded image
full_w = zero_padded_im.size[0]
full_h = zero_padded_im.size[1]
predicted_fullsize = np.zeros((full_h, full_w))
# # rebuild a full-size prediction map
imCounter = 0
for w in range(0, full_w, W):
for h in range(0, full_h, W):
# xpred = np.logical_not(predicted[imCounter, :, :]) # 1 should be house predictions
xpred = predictions[imCounter, :, :]
predicted_fullsize[h:h+W, w:w+W] = xpred*255
imCounter = imCounter+1
mask = Image.fromarray(predicted_fullsize.astype('uint8'), mode='L')
annot_im = Image.new('RGB', (full_w, full_h), (255, 0, 0))
zero_padded_im.paste(annot_im, mask=mask)
# display the result
zero_padded_im.save("predicted_images/" + imprefix + '.png')