-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathmodel.pyx
185 lines (156 loc) · 7.14 KB
/
model.pyx
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
import svm
import annotations
import features
import random
import logging
import numpy
import convolution
from math import ceil
logger = logging.getLogger("vision.model")
cimport numpy
from vision cimport annotations
class PathModel(object):
"""
A model that learns a linear SVM weight vector based off a path.
The model extracts positve examples from the given path and negative
examples from sliding windows that do not overlap with a given example.
We extract both HOG features and RGB features.
For fast scoring, use the corresponding convolution.pyx routine.
"""
def __init__(self, images, givens, dim = (40,40), hogbin = 8,
rgbbin = 8, bgskip = 2, bgsize = 5e4, c = 0.000001,
realprior = None, realpriorweight = 10):
"""
Constructs a path based model from the given path.
"""
self.dim = dim
self.hogbin = hogbin
self.rgbbin = rgbbin
self.c = c
logger.info("Extracting features from path")
positives, negatives = self.extractpath(images, givens, dim,
hogbin, rgbbin, bgskip,
bgsize)
# svm.sanity(positives, negatives) # uncomment when debugging
logger.info("Learning weights for path with {0} foregrounds and "
"{1} backgrounds".format(len(positives), len(negatives)))
svm.sanity(negatives, positives)
model = svm.train(negatives, positives, c = c)
self.weights, self.bias = model.weights, model.bias
logger.info("Weights learned with bias {0}".format(self.bias))
self.realprior = realprior
self.realpriorweight = realpriorweight
if self.realprior and not self.realprior.built:
self.realprior.build(givens, forcescore = 1)
def extractpath(self, images, givens, dim,
int hogbin, int rgbbin, int bgskip, int bgsize):
"""
Extracts features from a path.
"""
cdef int dimw = dim[0], dimh = dim[1]
cdef int imw, imh
cdef int i, j, xtl, ytl, xbr, ybr
cdef double wr, hr
cdef annotations.Box given
cdef numpy.ndarray[ndim=3, dtype=numpy.double_t] hogim
positives = []
negatives = []
negativesperframe = int(bgsize / len(givens))
for given in givens:
logger.debug("Extracting features from "
"frame {0}".format(given.frame))
wr = float(dim[0]) / given.width
hr = float(dim[1]) / given.height
im = images[given.frame]
im = im.resize((int(im.size[0]*wr), int(im.size[1]*hr)), 2)
imw, imh = im.size
mapped = given.transform(wr, hr)
mapped.xbr = mapped.xtl + dim[0]
mapped.ybr = mapped.ytl + dim[1]
# positives
xtl, ytl, xbr, ybr = mapped[0:4]
patch = im.crop((xtl-hogbin*2, ytl-hogbin*2,
xbr+hogbin*2, ybr+hogbin*2))
hogpatch = features.hog(patch, hogbin)[1:-1,1:-1,:]
hogpatch = hogpatch.flatten()
patch = im.crop((xtl, ytl, xbr, ybr))
rgbpatch = features.rgbmean(patch)
positives.append(numpy.append(hogpatch, rgbpatch))
# we shift the positives patch by a couple of pixels to have a
# larger training set
# xtl, ytl, xbr, ybr = mapped[0:4]
# for horzoffset in range(-hogbin+1, hogbin):
# for vertoffset in range(-hogbin+1, hogbin):
# patch = im.crop((xtl-hogbin+horzoffset,
# ytl-hogbin+vertoffset,
# xbr+hogbin+horzoffset,
# ybr+hogbin+vertoffset))
# hogpatch = features.hog(patch, hogbin)[1:-1,1:-1].flatten()
# rgbpatch = features.rgbhist(patch, self.rgbbin).flatten()
# positives.append(numpy.append(hogpatch, rgbpatch))
logger.debug("Extracting negatives")
# negatives
hogim = features.hog(im, hogbin)
hogim = features.hogpad(hogim)
framenegatives = []
for i from 0 <= i < imw-dimw by bgskip:
for j from 0 <= j < imh-dimh by bgskip:
if annotations.Box(i/wr, j/hr, (i + dimw)/wr,
(j + dimh)/hr).percentoverlap(given) < 0.5:
framenegatives.append((i, j))
logger.debug("Sampling negatives")
if len(framenegatives) > negativesperframe:
framenegatives = random.sample(framenegatives,
negativesperframe)
for i, j in framenegatives:
hogj = j/hogbin
hogjs = (j+dimh)/hogbin
hogi = i/hogbin
hogis = (i+dimw)/hogbin
hogpatch = hogim[hogj:hogjs, hogi:hogis, :]
hogpatch = hogpatch.flatten()
rgbpatchregion = (i, j, i+dimw, j+dimh)
rgbpatch = features.rgbmean(im.crop(rgbpatchregion))
rgbpatch = rgbpatch.flatten()
negatives.append(numpy.append(hogpatch, rgbpatch))
return positives, negatives
def hogweights(self):
"""
Returns the SVM weights just for the HOG features.
"""
x = self.dim[0]/self.hogbin
y = self.dim[1]/self.hogbin
return self.weights[0:x*y*13].reshape((y, x, 13))
def rgbweights(self):
"""
Returns the SVM weights just for the RGB features.
"""
x = self.dim[0]/self.hogbin
y = self.dim[1]/self.hogbin
return self.weights[x*y*13:]
def scoreframe(self, image, size, frame = None):
# resize image to so box has 'dim' in the resized space
cdef int i, j, width, height, dim0, dim1
cdef double rpw = self.realpriorweight
cdef double probsum = 0
cdef numpy.ndarray[ndim=2, dtype=numpy.double_t] proj
width, height = image.size
dim0, dim1 = self.dim
wr = self.dim[0] / <double>(size[0])
hr = self.dim[1] / <double>(size[1])
rimage = image.resize((int(ceil(width * wr)), int(ceil(height * hr))), 2)
cost = convolution.hogrgbmean(rimage, self.dim,
self.hogweights(),
self.rgbweights(),
hogbin = self.hogbin)
if self.realprior and frame and self.realprior.hasprojection(frame):
proj = self.realprior.scorelocations(frame)
proj = convolution.sumprob(proj, self.dim)
for i from 0 <= i < <int>(width * wr - dim0):
for j from 0 <= j < <int>(height * hr - dim1):
probsum = proj[<int>(i / wr), <int>(j / hr)]
probsum += proj[<int>((i + dim0) / wr), <int>((j + dim1) / hr)]
probsum -= proj[<int>(i / wr), <int>((j + dim1) / hr)]
probsum -= proj[<int>((i + dim0) / wr), <int>(j / hr)]
cost[i, j] += cost[i, j] - rpw * probsum
return cost