-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetection.py
More file actions
239 lines (173 loc) · 7.13 KB
/
detection.py
File metadata and controls
239 lines (173 loc) · 7.13 KB
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
import numpy as np
import dataset_reader
import sys
import argparse
from cv2 import imread, imshow, waitKey
import random
FLAGS = None
class detection_cor_coeff:
def __init__(self, train_set, test_set, random):
"""
Class definition.
Parameters
----------
train_set (str):
Dictionary of file paths and annotations
test_set (str):
Dictionary of file paths and annotations
random (boolean):
flag to either define templates at random or
average them out
"""
if random:
self.templates = self.random_templates_gen(train_set)
else:
self.templates = self.average_templates_gen(train_set)
self.test_set = test_set
def random_templates_gen(self, train_set):
"""
Random template generator.
Parameters:
-----------
train_set (dict):
Dictionary
Returns:
--------
A templates dictionary with a random template each class
"""
train_images = train_set["train_images"]
train_annotations = train_set["train_annotations"]
templates = {}
for cla in np.unique(train_annotations):
cla_indices = np.where(train_annotations == cla)[0]
template_index = random.choice(cla_indices)
templates[cla] = imread(train_images[template_index])
return templates
def average_templates_gen(self, train_set):
"""
Averaged template generator.
@param: train_set dictionary
@return: A templates dictionary with an averaged out template of random
sample of size 10 for each class.
"""
train_images = train_set["train_images"]
train_annotations = train_set["train_annotations"]
templates = {}
for cla in np.unique(train_annotations):
cla_indices = np.where(train_annotations == cla)[0]
sample_indices = np.random.choice(cla_indices, 10)
images = []
for train_img in train_images[sample_indices]:
img = imread(train_img)
images.append(img)
images = np.stack(images)
templates[cla] = np.mean(images, axis=0, dtype=np.int16)
return templates
def template_detection_threshold(self, template, test_img):
"""
Takes in a template and test image of same annotation and
returns the pearson correlation coefficient
Parameters:
-----------
template (dict):
Numpy image arrays
test_img (dict):
Numpy image arrays
Returns:
---------
Boolean based on value of correlation. Returns
True is correlation is above a threshold
"""
template_flat = np.ravel(template)
image_flat = np.ravel(test_img)
#imshow("template img", template)
#imshow("test img", test_img)
corr_coef = np.corrcoef(template_flat, image_flat)
#print(corr_coef)
if corr_coef[0,1] > 0.40:
return True
return False
def one_to_one_template_detection(self, batch_size):
"""
Runs one to one template dectection on particular
batch size of test set
Parameters:
-----------
batch_size (int): A batch size
Return:
-------
An accuracy
"""
count = 0
for i in range(batch_size):
annotation = self.test_set["test_annotations"][i]
test_img = imread(self.test_set["test_images"][i])
template = self.templates[annotation]
if self.template_detection_threshold(template, test_img):
count += 1
return count/batch_size
def template_detection_best_match(self, batch_size):
"""
Runs one to one template dectection on particular
batch size of test set
Parameters:
-----------
batch_size (int): A batch size
Return:
-------
An accuracy
"""
count = 0
for i in range(batch_size):
test_annotation = self.test_set["test_annotations"][i]
test_img = imread(self.test_set["test_images"][i])
pred_annotation = -1
max_corr = -1
for annotation, template in self.templates.items():
template_flat = np.ravel(template)
image_flat = np.ravel(test_img)
corr_coef = np.corrcoef(template_flat, image_flat)
if corr_coef[0,1] > max_corr:
pred_annotation = annotation
max_corr = corr_coef[0,1]
if pred_annotation == test_annotation:
count += 1
return count/batch_size
def main():
data_set = dataset_reader.class_dataset_reader(FLAGS.data_dir, train_test=True)
# reads images in batches to aviod running out of memory
data_set.read_images()
test_set = data_set.test_set()
train_set = data_set.train_set()
simple_cor_coeff = detection_cor_coeff(train_set, test_set, random=FLAGS.randomized)
# maximum possible batch size
max_batch_size = len(test_set["test_annotations"])
print("Maximum batch size. ", max_batch_size)
if FLAGS.test_mode == "one_to_one":
accuracy = simple_cor_coeff.one_to_one_template_detection(batch_size=FLAGS.batch_size)
print("Accuracy of one to one method is {0} for batch size of {1}".format(accuracy, FLAGS.batch_size))
elif FLAGS.test_mode == "best_match":
accuracy = simple_cor_coeff.template_detection_best_match(batch_size=FLAGS.batch_size)
print("Accuracy of best match method is {0} for batch size of {1}.".format(accuracy, FLAGS.batch_size))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--data_dir",
type=str,
default="/home/abi-osler/Documents/CV_final_project/DeepScoresClassification",
help="Directory for storing input data")
parser.add_argument("--batch_size",
type=int,
default=100,
help= "Set the batch size of test data to evaluate on")
parser.add_argument("--test_mode",
type=str,
default="best_match",
choices=["best_match", "one_to_one"],
help="Pick the mode for this test")
parser.add_argument('--randomized',
default=False,
type=lambda x: (str(x).lower() == 'true'),
help="Setting randomized to True sets the template at random. \
Setting it true sets a template from a random batch of 10 from train set")
FLAGS, unparsed = parser.parse_known_args()
main()