在python中创建MLP显示错误 [英] creating MLP in python showing error

查看:255
本文介绍了在python中创建MLP显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是神经网络的新手,同时使用opencv 3.1.0错误弹出窗口在python中创建MLP.代码为 导入cv2 将numpy导入为np 导入glob

Iam a newbie in neural networks while creating MLP in python using opencv 3.1.0 error pops.The code is 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.create(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 = 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)

这是错误 AttributeError:'cv2.ml_ANN_MLP'对象没有属性'create'

this is the error AttributeError: 'cv2.ml_ANN_MLP' object has no attribute 'create'

推荐答案

基于此代码示例,我建议如下设置层数:

Based on this code sample, I would suggest setting the number of layers as follows:

model.setLayerSizes(layer_sizes)

您还可以查看学习OpenCV 3 书.

我似乎无法在一分钟内找到合适的完整文档,但是为了记录,这是dir(model)提供的内容:

I can't seem to be able to find a proper complete doc at the minute, but for the record here's what dir(model) gives:

  • calcError
  • 清除
  • getBackpropMomentumScale
  • getBackpropWeightScale
  • getDefaultName
  • getLayerSizes
  • getRpropDW0
  • getRpropDWMax
  • getRpropDWMin
  • getRpropDWMinus
  • getRpropDWPlus
  • getTermCriteria
  • getTrainMethod
  • getVarCount
  • getWeights
  • isClassifier
  • 已培训
  • 预测
  • 保存
  • setActivationFunction
  • setBackpropMomentumScale
  • setBackpropWeightScale
  • setLayerSizes
  • setRpropDW0
  • setRpropDWMax
  • setRpropDWMin
  • setRpropDWMinus
  • setRpropDWPlus
  • setTermCriteria
  • setTrainMethod
  • 火车
  • calcError
  • clear
  • empty
  • getBackpropMomentumScale
  • getBackpropWeightScale
  • getDefaultName
  • getLayerSizes
  • getRpropDW0
  • getRpropDWMax
  • getRpropDWMin
  • getRpropDWMinus
  • getRpropDWPlus
  • getTermCriteria
  • getTrainMethod
  • getVarCount
  • getWeights
  • isClassifier
  • isTrained
  • predict
  • save
  • setActivationFunction
  • setBackpropMomentumScale
  • setBackpropWeightScale
  • setLayerSizes
  • setRpropDW0
  • setRpropDWMax
  • setRpropDWMin
  • setRpropDWMinus
  • setRpropDWPlus
  • setTermCriteria
  • setTrainMethod
  • train

这篇关于在python中创建MLP显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆