【发布时间】:2016-05-26 12:40:49
【问题描述】:
我正在尝试复制这篇论文:Global motion estimation from coarsely sampled motion vector field and the applications
我需要找到参数m0, m1, m2....., m7
给定图像 x1_1 和 x1_2 以及方程式在哪里
其中 x'{x1_2[1, :, :]} 和 y'{x1_2[0, :, :]} 是 x1_2 和 x, y x1_1 与 x1_1 风格相同。
我参考了this post 中的示例来进行此实现。
谁能帮我找到这些参数?
根据 cmets 和 example of leastsq function 进行编辑。
修改后的程序如下,输出如下
import os
import sys
import numpy as np
import scipy
from scipy.optimize import leastsq
def peval (inp_mat,p):
m0,m1,m2,m3,m4,m5,m6,m7 = p
out_mat = np.zeros(inp_mat.shape,dtype=np.float32)
mid = inp_mat.shape[0]/2
for xy in range(0,inp_mat.shape[0]):
if (xy<(inp_mat.shape[0]/2)):
out_mat[xy] = ( ( (inp_mat[xy+mid]*m0)+(inp_mat[xy]*m1)+ m2 ) /( (inp_mat[xy+mid]*m6)+(inp_mat[xy]*m7)+1 ) )
else:
out_mat[xy] = ( ( (inp_mat[xy]*m3)+(inp_mat[xy-mid]*m4)+ m5 ) /( (inp_mat[xy]*m6)+(inp_mat[xy-mid]*m7)+1 ) )
return out_mat
def residuals(p, out_mat, inp_mat):
m0,m1,m2,m3,m4,m5,m6,m7 = p
err=np.zeros(inp_mat.shape,dtype=np.float32)
if (out_mat.shape == inp_mat.shape):
for xy in range(0,inp_mat.shape[0]):
err[xy] = err[xy]+ (out_mat[xy] -inp_mat[xy])
return err
f = open('/media/anilil/Data/Datasets/repo/txt_op/vid.txt','r')
x = np.loadtxt(f,dtype=np.int16,comments='#',delimiter='\t')
nof = x.shape[0]/72 # Find the number of frames
x1 = x.reshape(-1,60,40)
x1_1= x1[0,:,:].flatten()
x1_2= x1[1,:,:].flatten()
x= []
y= []
for xy in range(1,50,1):
y.append(x1[xy,:,:].flatten())
x.append(x1[xy-1,:,:].flatten())
x=np.array(x,dtype=np.float32)
y=np.array(y,dtype=np.float32)
length = x1_1.shape#initail guess
p0 = np.array([1,1,1,1,1,1,1,1],dtype=np.float32)
abc=leastsq(residuals, p0,args=(y,x))
print ('Size of first matrix is '+str(x1_1.shape))
print ('Size of first matrix is '+str(x1_2.shape))
print ("Done with program")
输出
ValueError: object too deep for desired array
Traceback (most recent call last):
File "/media/anilil/Data/charm/mv_clean/.idea/nose_reduction_mpeg.py", line 49, in <module>
abc=leastsq(residuals, p0,args=(y,x))
File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 378, in leastsq
gtol, maxfev, epsfcn, factor, diag)
minpack.error: Result from function call is not a proper array of floats.
【问题讨论】:
-
你能分享你正在使用的数据吗?!
-
opt.leastsq接受一个函数句柄,但在将它传递给leastsq之前调用function()。删除()以传递函数本身,而不是先调用它:opt.leastsq(function, args=(), Dfun=...。这应该会使错误消息消失,但我不知道其他一切是否正确。 -
分享了示例数据并尝试了“function”而不是“function()”@kazemakase
-
啊,我错过了,首先。您还必须将
x0传递给函数:opt.leastsq(function, x0, args=(), ...。这些是您优化的参数m0...m7的起始值(起始猜测)。这是非常特定于领域的,我不知道什么是好的值。您可以尝试 0,或者论文可以提示您从哪里开始。此外,inp_mat听起来像是数据而不是优化参数。因此,我认为它不应该是函数参数。
标签: python numpy machine-learning scipy scikit-learn