python - Numpy array, indexing and convolving confusion -
i'm trying complete following function, have been running problems indexing, resulting in "valueerror: operands not broadcast shapes (0,9) (5)".
i think error might coming how i'm trying call values ssd_difference[], i'm not entirely sure.
also how go using convolve2d based on hint given below? understand numpy has function it, have no idea need put in make work.
additional information: binomialfilter5() returns 5x1 numpy array of dtype float representing binomial filter. i'm assuming "weights[]" ssd_difference[] values.
def transitiondifference(ssd_difference):
""" compute transition costs between frames, taking dynamics account.
instructions: 1. iterate through rows , columns of ssd difference, ignoring first 2 values , last 2 values.
1a. each value @ i, j, multiply binomial filter of length 5 (implemented later in code) weights starting 2 frames before until 2 frames after, , take sum of products.
i.e. weights frame are: [weight[i - 2, j - 2], weight[i - 1, j - 1], weight[i, j], weight[i + 1, j + 1], weight[i + 2, j + 2]]
multiply binomial filter weights @ each i, j output.
it may take little bit of understanding why computing this, simple explanation change frame 4 5, lets call ch(4, 5), , make weight:
ch(4, 5) = ch(2, 3) + ch(3, 4) + ch(4, 5) + ch(5, 6) + ch(6, 7)
this accounts weights in previous changes , future changes when considering current frame.
of course, weigh these sums binomial filter, weight ch(4, 5) still important one, gives better understanding.
args: ssd_difference (numpy.ndarray): difference matrix produced ssd function. returns: output (numpy.ndarray): difference matrix takes preceding , following frames account. output difference matrix should have same dtype input, 4 rows , columns smaller, corresponding frames have valid dynamics. hint: there efficient way 2d convolution. think coordinates using consider preceding , following frame pairings. """ output = np.zeros((ssd_difference.shape[0] - 4, ssd_difference.shape[1] - 4), dtype=ssd_difference.dtype) # write code here. in range(len(ssd_difference)): j in range(len(ssd_difference)): if == 0: if j > 1: output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialfilter5()) elif == ssd_difference.shape[0] - 1: if j < ssd_difference.shape[1] - 2: output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialfilter5()) else: output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialfilter5()) # end of function. return output
as commented, should tell line produced error message.
but can guess, since there couple of lines operation involves broadcasting. is:
output[i,j] = np.sum( ssd_difference[i-2:i+2]*binomialfilter5())
you write binomialfilter5()
produces (5,1)
array, error talks (5,)
. doesn't matter here, should keep number of dimensions straight. (5,1)
signficantly different (5,)
.
output
has shape (ssd_difference.shape[0] - 4, ssd_difference.shape[1] - 4)
. iterating i,j
both on range(len(ssd_difference))
. output[i,j]
result in index error
. when iterating on 2d array, better use correct shape
element, rather len()
.
but suspect immediate error results ssd_difference[i-2:i+2]
. when i==0
, ssd_difference[-2:2]
. producing (0,9)
array, since -2
index means second last, larger 2
.
i think intending pull 5 rows array, match 5 values in other array. correct iteration, think be:
for in range(output.shape[0]): j in range(output.shape[1]): .... output[i,j] = np.sum(ssd_difference[i:i+5, :] * binomialfilter5()) ...
you should test expressions individually in interactive shell, selected values of i
. ssd_difference[i:i+5, :]
should have shape (5,9)
, , binomialfilter5()
should (5,1)
.
Comments
Post a Comment