python - Python OpenCV TypeError:找不到必需的参数 'layout'(pos 2)

标签 python opencv

当我尝试运行此Python脚本时,会发生以下情况:

$ python mlp_training.py
Loading training data...
(0, 38400)
(0, 4)
Loading image duration: 0.000199834
Training MLP ...
Traceback (most recent call last):
  File "mlp_training.py", line 49, in <module>
    num_iter = model.train((train, ( train_labels ), ( params )))
TypeError: Required argument 'layout' (pos 2) not found

mlp_training.py
__author__ = 'zhengwang'

import cv2
import numpy as np
import glob

print 'Loading training data...'
e0 = cv2.getTickCount()

# load training data
image_array = np.zeros((1, 38400))
label_array = np.zeros((1, 4), 'float')
training_data = glob.glob('training_data/*.npz')

for single_npz in training_data:
 with np.load(single_npz) as data:
    print data.files
    train_temp = data['train']
    train_labels_temp = data['train_labels']
    print train_temp.shape
    print train_labels_temp.shape
 image_array = np.vstack((image_array, train_temp))
 label_array = np.vstack((label_array, train_labels_temp))

train = image_array[1:, :]
train_labels = label_array[1:, :]
print train.shape
print train_labels.shape

e00 = cv2.getTickCount()
time0 = (e00 - e0)/ cv2.getTickFrequency()
print 'Loading image duration:', time0

# set start time
e1 = cv2.getTickCount()

# create MLP
layer_sizes = np.int32([38400, 32, 4])
model = cv2.ml.ANN_MLP_create()
model.setLayerSizes(layer_sizes)
criteria = (cv2.TERM_CRITERIA_COUNT | cv2.TERM_CRITERIA_EPS, 500, 0.0001)
criteria2 = (cv2.TERM_CRITERIA_COUNT, 100, 0.001)
params = dict(term_crit = criteria,
           train_method = cv2.ml.ANN_MLP_BACKPROP,
           bp_dw_scale = 0.001,
           bp_moment_scale = 0.0 )

print 'Training MLP ...'
num_iter = model.train((train, ( train_labels ), ( params )))

# set end time
e2 = cv2.getTickCount()
time = (e2 - e1)/cv2.getTickFrequency()
print 'Training duration:', time

# save param
model.save('mlp_xml/mlp.xml')

print 'Ran for %d iterations' % num_iter

ret, resp = model.predict(train)
prediction = resp.argmax(-1)
print 'Prediction:', prediction
true_labels = train_labels.argmax(-1)
print 'True labels:', true_labels

print 'Testing...'
train_rate = np.mean(prediction == true_labels)
print 'Train rate: %f:' % (train_rate*100)

我是从GitHub page获得的,然后复制了代码并在此处和此处对其进行了修改以满足我的需要。怎么了

最佳答案

查看mlp_training.py中的代码

num_iter = model.train(train, train_labels, None, params = params)

它使用4个参数运行train(),但您仅使用一个参数-元组
model.train(   (train, ( train_labels ), ( params ))   )

似乎您放置了太多的感觉障碍。

关于python - Python OpenCV TypeError:找不到必需的参数 'layout'(pos 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41601636/

相关文章:

python - 在 OSX 10.9 中安装 lxml

python - 多行 TQDM 彩色进度条打印

c++ - 如何获取属于轮廓特定一侧的点?

python - 为什么python会引发错误:AttributeError: 'numpy.ndarray'对象没有属性 'append'?

python - 使用logging.info而不是logger.info会破坏StreamHandler

python - 从 numpy datetime64 获取年、月或日

python - 在 matplotlib 中绘制方形等高线图

c++ - 在 OpenCV 中查找 SparseMat 的最大和最小位置

python - 为 Python 3.3 安装 opencv

python - Opencv 二进制项目检测