-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
275 lines (249 loc) · 14.5 KB
/
test.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
264
265
266
267
268
269
270
271
272
273
274
275
"""
Code for a test session.
"""
import csv
import os
from collections import namedtuple
import numpy as np
import torch
from torch.autograd import Variable
import torchvision
import scipy.misc
import cv2
import transforms
import settings as settings_
from crowd_dataset import CrowdDataset
from model3 import Discriminator1, load_trainer
from hardware import gpu, cpu
from matplotlib import pyplot as plt
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
font = ImageFont.truetype("/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf",25)
ExamplePatchWithMeta = namedtuple('ExamplePatchWithMeta', ['example', 'half_patch_size', 'x', 'y'])
def batches_of_examples_with_meta(full_example, batch_size=20):
"""
Generator for example patch batches with meta.
:param full_example: The original full size example.
:type full_example: CrowdExample
:param batch_size: The number of patches per batch to get.
:type batch_size: int
:return: A list of examples patches with meta.
:rtype: list[ExamplePatchWithMeta]
"""
patch_transform = transforms.ExtractPatchForPositionAndRescale()
test_transform = torchvision.transforms.Compose([transforms.NegativeOneToOneNormalizeImage(),
transforms.NumpyArraysToTorchTensors()])
sample_x = 0
sample_y = 0
half_patch_size = 0 # Don't move on the first patch.
while True:
batch = []
for _ in range(batch_size):
sample_x += half_patch_size
if sample_x >= full_example.label.shape[1]:
sample_x = 0
sample_y += half_patch_size
if sample_y >= full_example.label.shape[0]:
if batch:
yield batch
return
example_patch, original_patch_size = patch_transform(full_example, sample_y, sample_x)
example = test_transform(example_patch)
half_patch_size = int(original_patch_size // 2)
example_with_meta = ExamplePatchWithMeta(example, half_patch_size, sample_x, sample_y)
batch.append(example_with_meta)
yield batch
def test(settings=None):
"""Main script for testing a model."""
if not settings:
settings = settings_
# test_dataset = CrowdDataset(settings.test_dataset_path, 'test')
net = Discriminator1()
model_state_dict, _, _, _ = load_trainer(prefix='discriminator')
net.load_state_dict(model_state_dict)
gpu(net)
net.eval()
count_errors = []
density_errors = []
# print('Starting test...')
# scene_number = 1
# running_count = 0
# running_count_error = 0
# running_density_error = 0
# for full_example_index, full_example in enumerate(test_dataset):
# print('Processing example {}'.format(full_example_index), end='\r')
# sum_density_label = np.zeros_like(full_example.label, dtype=np.float32)
# sum_count_label = np.zeros_like(full_example.label, dtype=np.float32)
# hit_predicted_label = np.zeros_like(full_example.label, dtype=np.int32)
# for batch in batches_of_examples_with_meta(full_example):
# images = torch.stack([example_with_meta.example.image for example_with_meta in batch])
# rois = torch.stack([example_with_meta.example.roi for example_with_meta in batch])
# images = Variable(gpu(images))
# predicted_labels, predicted_counts = net(images)
# predicted_labels = predicted_labels * Variable(gpu(rois))
# predicted_labels = cpu(predicted_labels.data).numpy()
# predicted_counts = cpu(predicted_counts.data).numpy()
# for example_index, example_with_meta in enumerate(batch):
# predicted_label = predicted_labels[example_index]
# predicted_count = predicted_counts[example_index]
# x, y = example_with_meta.x, example_with_meta.y
# half_patch_size = example_with_meta.half_patch_size
# predicted_label_sum = np.sum(predicted_label)
# original_patch_dimensions = ((2 * half_patch_size) + 1, (2 * half_patch_size) + 1)
# predicted_label = scipy.misc.imresize(predicted_label, original_patch_dimensions, mode='F')
# unnormalized_predicted_label_sum = np.sum(predicted_label)
# if unnormalized_predicted_label_sum != 0:
# density_label = predicted_label * predicted_label_sum / unnormalized_predicted_label_sum
# count_label = predicted_label * predicted_count / unnormalized_predicted_label_sum
# else:
# density_label = predicted_label
# count_label = np.full(predicted_label.shape, predicted_count / predicted_label.size)
# y_start_offset = 0
# if y - half_patch_size < 0:
# y_start_offset = half_patch_size - y
# y_end_offset = 0
# if y + half_patch_size >= full_example.label.shape[0]:
# y_end_offset = y + half_patch_size + 1 - full_example.label.shape[0]
# x_start_offset = 0
# if x - half_patch_size < 0:
# x_start_offset = half_patch_size - x
# x_end_offset = 0
# if x + half_patch_size >= full_example.label.shape[1]:
# x_end_offset = x + half_patch_size + 1 - full_example.label.shape[1]
# sum_density_label[y - half_patch_size + y_start_offset:y + half_patch_size + 1 - y_end_offset,
# x - half_patch_size + x_start_offset:x + half_patch_size + 1 - x_end_offset
# ] += density_label[y_start_offset:density_label.shape[0] - y_end_offset,
# x_start_offset:density_label.shape[1] - x_end_offset]
# sum_count_label[y - half_patch_size + y_start_offset:y + half_patch_size + 1 - y_end_offset,
# x - half_patch_size + x_start_offset:x + half_patch_size + 1 - x_end_offset
# ] += count_label[y_start_offset:count_label.shape[0] - y_end_offset,
# x_start_offset:count_label.shape[1] - x_end_offset]
# hit_predicted_label[y - half_patch_size + y_start_offset:y + half_patch_size + 1 - y_end_offset,
# x - half_patch_size + x_start_offset:x + half_patch_size + 1 - x_end_offset
# ] += 1
# sum_density_label *= full_example.roi
# sum_count_label *= full_example.roi
# full_predicted_label = sum_density_label / hit_predicted_label.astype(np.float32)
# full_predicted_count = np.sum(sum_count_label / hit_predicted_label.astype(np.float32))
# label_in_roi = full_example.label * full_example.roi
# density_loss = np.abs(full_predicted_label - label_in_roi).sum()
# count_loss = np.abs(full_predicted_count - label_in_roi.sum())
# running_count += full_example.label.sum()
# running_count_error += count_loss
# running_density_error += density_loss
# if ((full_example_index + 1) % 120) == 0:
# print('Scene {}'.format(scene_number))
# print('Total count: {}'.format(running_count))
# count_error = running_count_error / 120
# print('Mean count error: {}'.format(count_error))
# density_error = running_density_error / 120
# print('Mean density error: {}'.format(density_error))
# count_errors.append(count_error)
# density_errors.append(density_error)
# running_count = 0
# running_count_error = 0
# running_density_error = 0
# scene_number += 1
validation_dataset_path = '/media/pankaj/D04BA26478DAA96B/PANKAJ/testdatabase/'
load_model_path = '/media/pankaj/D04BA26478DAA96B/PANKAJ/jointcnn/CNN 5 Cameras 5 Images lr 1e-5 y2018m05d04h11m41s21/model 1000000'
log_directory = '/media/pankaj/D04BA26478DAA96B/PANKAJ/logs'
validation_dataset = CrowdDataset(validation_dataset_path, '500717 Time Lapse Demo')
print('Starting test...')
running_count = 0
running_count_error = 0
running_density_error = 0
initial_label = validation_dataset[0].label
full_predicted_labels = np.zeros(shape=(len(validation_dataset), initial_label.shape[0], initial_label.shape[1]), dtype=np.float32)
for full_example_index, full_example in enumerate(validation_dataset):
print('Processing example {}'.format(full_example_index), end='\r')
sum_density_label = np.zeros_like(full_example.label, dtype=np.float32)
sum_count_label = np.zeros_like(full_example.label, dtype=np.float32)
hit_predicted_label = np.zeros_like(full_example.label, dtype=np.int32)
for batch in batches_of_examples_with_meta(full_example):
images = torch.stack([example_with_meta.example.image for example_with_meta in batch])
rois = torch.stack([example_with_meta.example.roi for example_with_meta in batch])
images = Variable(gpu(images))
predicted_labels, predicted_counts = net(images)
predicted_labels = predicted_labels * Variable(gpu(rois))
predicted_labels = cpu(predicted_labels.data).numpy()
predicted_counts = cpu(predicted_counts.data).numpy()
for example_index, example_with_meta in enumerate(batch):
predicted_label = predicted_labels[example_index]
predicted_count = predicted_counts[example_index]
x, y = example_with_meta.x, example_with_meta.y
half_patch_size = example_with_meta.half_patch_size
predicted_label_sum = np.sum(predicted_label)
original_patch_dimensions = ((2 * half_patch_size) + 1, (2 * half_patch_size) + 1)
predicted_label = scipy.misc.imresize(predicted_label, original_patch_dimensions, mode='F')
unnormalized_predicted_label_sum = np.sum(predicted_label)
if unnormalized_predicted_label_sum != 0:
density_label = predicted_label * predicted_label_sum / unnormalized_predicted_label_sum
count_label = predicted_label * predicted_count / unnormalized_predicted_label_sum
else:
density_label = predicted_label
count_label = np.full(predicted_label.shape, predicted_count / predicted_label.size)
y_start_offset = 0
if y - half_patch_size < 0:
y_start_offset = half_patch_size - y
y_end_offset = 0
if y + half_patch_size >= full_example.label.shape[0]:
y_end_offset = y + half_patch_size + 1 - full_example.label.shape[0]
x_start_offset = 0
if x - half_patch_size < 0:
x_start_offset = half_patch_size - x
x_end_offset = 0
if x + half_patch_size >= full_example.label.shape[1]:
x_end_offset = x + half_patch_size + 1 - full_example.label.shape[1]
sum_density_label[y - half_patch_size + y_start_offset:y + half_patch_size + 1 - y_end_offset,
x - half_patch_size + x_start_offset:x + half_patch_size + 1 - x_end_offset
] += density_label[y_start_offset:density_label.shape[0] - y_end_offset,
x_start_offset:density_label.shape[1] - x_end_offset]
sum_count_label[y - half_patch_size + y_start_offset:y + half_patch_size + 1 - y_end_offset,
x - half_patch_size + x_start_offset:x + half_patch_size + 1 - x_end_offset
] += count_label[y_start_offset:count_label.shape[0] - y_end_offset,
x_start_offset:count_label.shape[1] - x_end_offset]
hit_predicted_label[y - half_patch_size + y_start_offset:y + half_patch_size + 1 - y_end_offset,
x - half_patch_size + x_start_offset:x + half_patch_size + 1 - x_end_offset
] += 1
hit_predicted_label[hit_predicted_label == 0] = 1
sum_density_label *= full_example.roi
sum_count_label *= full_example.roi
full_predicted_label = sum_density_label / hit_predicted_label.astype(np.float32)
full_predicted_count = np.sum(sum_count_label / hit_predicted_label.astype(np.float32))
label_in_roi = full_example.label * full_example.roi
# density_counter = np.abs(full_predicted_label).sum()
# print('density =',density_counter)
density_loss = np.abs(full_predicted_label - label_in_roi).sum()
count_loss = np.abs(full_predicted_count - label_in_roi.sum())
running_count += full_example.label.sum()
running_count_error += count_loss
running_density_error += density_loss
full_predicted_labels[full_example_index] = full_predicted_label
# print('coutning error =',running_count_error)
# print('density error =',running_density_error)
#
#plt.text(500, 30,full_predicted_count*100, bbox=dict(facecolor='red', alpha=0.5))
# plt.imshow(full_predicted_label)
# plt.show()
validation_count_error = running_count_error / len(validation_dataset)
csv_file_path = os.path.join(log_directory, 'Test Results.csv')
if not os.path.isfile(csv_file_path):
with open(csv_file_path, 'w') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(['Run Name', 'Scene 1', 'Scene 2', 'Scene 3', 'Scene 4', 'Scene 5', 'Mean',
'Scene 1 Density', 'Scene 2 Density', 'Scene 3 Density', 'Scene 4 Density',
'Scene 5 Density', 'Mean Density', 'Mean Validation'])
with open(csv_file_path, 'a') as csv_file:
writer = csv.writer(csv_file)
path_list = os.path.normpath(load_model_path).split(os.sep)
model_name = os.path.join(*path_list[-2:])
test_results = [model_name, *count_errors, np.mean(count_errors),*density_errors, np.mean(density_errors), validation_count_error]
writer.writerow(test_results)
np.save(os.path.join(log_directory, 'predicted_labels_time_lapse.npy'), full_predicted_labels)
print('Finished test.')
settings.load_model_path = None
return np.mean(count_errors)
if __name__ == '__main__':
test()