-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathrun_predictions.py
157 lines (117 loc) · 4.06 KB
/
run_predictions.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
import os
import numpy as np
import json
from PIL import Image
def compute_convolution(I, T, stride=None):
'''
This function takes an image <I> and a template <T> (both numpy arrays)
and returns a heatmap where each grid represents the output produced by
convolution at each location. You can add optional parameters (e.g. stride,
window_size, padding) to create additional functionality.
'''
(n_rows,n_cols,n_channels) = np.shape(I)
'''
BEGIN YOUR CODE
'''
heatmap = np.random.random((n_rows, n_cols))
'''
END YOUR CODE
'''
return heatmap
def predict_boxes(heatmap):
'''
This function takes heatmap and returns the bounding boxes and associated
confidence scores.
'''
output = []
'''
BEGIN YOUR CODE
'''
'''
As an example, here's code that generates between 1 and 5 random boxes
of fixed size and returns the results in the proper format.
'''
box_height = 8
box_width = 6
num_boxes = np.random.randint(1,5)
for i in range(num_boxes):
(n_rows,n_cols,n_channels) = np.shape(I)
tl_row = np.random.randint(n_rows - box_height)
tl_col = np.random.randint(n_cols - box_width)
br_row = tl_row + box_height
br_col = tl_col + box_width
score = np.random.random()
output.append([tl_row,tl_col,br_row,br_col, score])
'''
END YOUR CODE
'''
return output
def detect_red_light_mf(I):
'''
This function takes a numpy array <I> and returns a list <output>.
The length of <output> is the number of bounding boxes predicted for <I>.
Each entry of <output> is a list <[row_TL,col_TL,row_BR,col_BR,score]>.
The first four entries are four integers specifying a bounding box
(the row and column index of the top left corner and the row and column
index of the bottom right corner).
<score> is a confidence score ranging from 0 to 1.
Note that PIL loads images in RGB order, so:
I[:,:,0] is the red channel
I[:,:,1] is the green channel
I[:,:,2] is the blue channel
'''
'''
BEGIN YOUR CODE
'''
template_height = 8
template_width = 6
# You may use multiple stages and combine the results
T = np.random.random((template_height, template_width))
heatmap = compute_convolution(I, T)
output = predict_boxes(heatmap)
'''
END YOUR CODE
'''
for i in range(len(output)):
assert len(output[i]) == 5
assert (output[i][4] >= 0.0) and (output[i][4] <= 1.0)
return output
# Note that you are not allowed to use test data for training.
# set the path to the downloaded data:
data_path = '../data/RedLights2011_Medium'
# load splits:
split_path = '../data/hw02_splits'
file_names_train = np.load(os.path.join(split_path,'file_names_train.npy'))
file_names_test = np.load(os.path.join(split_Path,'file_names_test.npy'))
# set a path for saving predictions:
preds_path = '../data/hw02_preds'
os.makedirs(preds_path, exist_ok=True) # create directory if needed
# Set this parameter to True when you're done with algorithm development:
done_tweaking = False
'''
Make predictions on the training set.
'''
preds_train = {}
for i in range(len(file_names_train)):
# read image using PIL:
I = Image.open(os.path.join(data_path,file_names_train[i]))
# convert to numpy array:
I = np.asarray(I)
preds_train[file_names_train[i]] = detect_red_light_mf(I)
# save preds (overwrites any previous predictions!)
with open(os.path.join(preds_path,'preds_train.json'),'w') as f:
json.dump(preds_train,f)
if done_tweaking:
'''
Make predictions on the test set.
'''
preds_test = {}
for i in range(len(file_names_test)):
# read image using PIL:
I = Image.open(os.path.join(data_path,file_names_test[i]))
# convert to numpy array:
I = np.asarray(I)
preds_test[file_names_test[i]] = detect_red_light_mf(I)
# save preds (overwrites any previous predictions!)
with open(os.path.join(preds_path,'preds_test.json'),'w') as f:
json.dump(preds_test,f)