使用OpenCV在Python中实现神经网络时的函数原型错误

时间:2013-04-16 19:00:30

标签: python opencv machine-learning neural-network backpropagation

我们正在尝试在Python中实现神经网络。这是我们的代码:

import cv
from opencv.cv import *
from opencv.highgui import *
from opencv.ml import *

def trainMachine():
    ann = CvANN_MLP()
    global ann
    fin=open("train.txt","r")
    train_sample_count=int(fin.readline())
    print "Found training file with ",train_sample_count," examples"
    trainData=cvCreateMat(train_sample_count,2,CV_32FC1)
    trainClasses=cvCreateMat(train_sample_count,1,CV_32FC1)
    sampleWts=cvCreateMat(train_sample_count,1,CV_32FC1)
    neuralLayers=cvCreateMat(4,1,CV_32SC1)
    trainData1=cvGetRows(trainData,0,train_sample_count)
        trainClasses1=cvGetRows(trainClasses,0,train_sample_count)
        sampleWts1=cvGetRows(sampleWts,0,train_sample_count)
        neuralLayers1=cvGetRows(neuralLayers,0,4)
    cvSet1D(neuralLayers1,0,cvScalar(2))
        cvSet1D(neuralLayers1,1,cvScalar(3))
        cvSet1D(neuralLayers1,2,cvScalar(3))
        cvSet1D(neuralLayers1,3,cvScalar(1))
    td=list()
    for i in range(train_sample_count):
        line = fin.readline().split()
        td.append(list())
        for num in line:
            td[i].append(float(num))
    for i in range(0,train_sample_count):
        cvSetReal2D(trainData1,i,0,td[i][0])
        cvSetReal2D(trainData1,i,1,td[i][1])
        cvSet1D(trainClasses1,i,cvScalar(td[i][2]))
        cvSet1D(sampleWts1,i,cvScalar(1))
    ann.create(neuralLayers)
    criter=cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,100000,0.001)
    params=CvANN_MLP_TrainParams(criter,0,0.01,0.05)
    ann.train(trainData,trainClasses,sampleWts,0,params)

trainmachine()

但我们收到了错误:

Traceback (most recent call last):
  File "ml.py", line 58, in <module>
    main()
  File "ml.py", line 52, in main
    trainMachine();
  File "ml.py", line 39, in trainMachine
    ann.train(trainData,trainClasses,sampleWts,0,params)
  File "/usr/lib/pymodules/python2.6/opencv/ml.py", line 1310, in train
    def train(self, *args): return _ml.CvANN_MLP_train(self, *args)
NotImplementedError: Wrong number of arguments for overloaded function 'CvANN_MLP_train'.
  Possible C/C++ prototypes are:
    train(CvANN_MLP *,CvMat const *,CvMat const *,CvMat const *,CvMat const *,CvANN_MLP_TrainParams,int)
    train(CvANN_MLP *,CvMat const *,CvMat const *,CvMat const *,CvMat const *,CvANN_MLP_TrainParams)
    train(CvANN_MLP *,CvMat const *,CvMat const *,CvMat const *,CvMat const *)
    train(CvANN_MLP *,CvMat const *,CvMat const *,CvMat const *)

我们尝试了所有给定的C ++原型。但仍有错误。

我们实际上正在尝试将C ++中神经网络的工作实现移植到Python。问题在于用于训练我们的神经网络的训练函数。

这是C ++源代码:neural.cpp

这有什么解决方案吗?如果要在Python中使用的函数原型或名称不同,请帮助我们。

请注意,该代码适用于Python v2.6和OpenCV v2.1。较新的版本不支持导入的模块。

0 个答案:

没有答案