opencv AttributError:'NeuralNetwork'对象没有属性'initialize'?

l2osamch  于 8个月前  发布在  其他
关注(0)|答案(2)|浏览(79)

我正在为我的学术项目学习Python编程和机器学习,我对车牌识别感兴趣。
通过执行下面的代码,我得到了错误,这是下面提到的代码后,

values=      
    ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','J','K','L','M','N','P','R','S','T','U','V','W','X','Z']
keys=range(32)
data_map=dict((keys, values))

def get_ann(data_map):
feature_mat=[]    
label_mat=[]
for keys in data_map:
    path_train="/home/sagar/Project data set/ANPR/ann/%s"%data_map[keys]
    filenames=get_imlist(path_train)
    perfeature_mat=[]
    perlabel_mat=[]

    for image in filenames[0]:
        raw_image=cv2.imread(image)
        raw_image=cv2.cvtColor(raw_image, cv2.COLOR_BGR2GRAY)

        #resize the image into 5 cols(width) and 10 rows(height)
        raw_image=cv2.resize(raw_image,(5,10),                            interpolation=cv2.INTER_AREA)
        #Do a hard thresholding.
        _,th2=cv2.threshold(raw_image, 70, 255, cv2.THRESH_BINARY)

        #generate features
        horz_hist=np.sum(th2==255, axis=0)
        vert_hist=np.sum(th2==255, axis=1)
        sample=th2.flatten()

        #concatenate these features together
        feature=np.concatenate([horz_hist, vert_hist, sample])

        # append these features together along with their respective labels
        perfeature_mat.append(feature)
        perlabel_mat.append(keys)

    feature_mat.append(perfeature_mat)
    label_mat.append(perlabel_mat)

# These are the final product.
bigfeature_mat=np.vstack(feature_mat)
biglabel_mat=np.hstack(label_mat)

# As usual. We need to convert them into double type for Shogun.
bigfeature_mat=np.array(bigfeature_mat, dtype='double')
biglabel_mat=np.array(biglabel_mat, dtype='double')

#shogun works in a way in which columns are samples and rows are features.
#Hence we need to transpose the observation matrix
obs_matrix=bigfeature_mat.T

#convert the observation matrix and the labels into Shogun RealFeatures and MulticlassLabels structures resp. .
sg_features=RealFeatures(obs_matrix)
sg_labels=MulticlassLabels(biglabel_mat)

#initialize a simple ANN in Shogun with one hidden layer.
layers=DynamicObjectArray()
layers.append_element(NeuralInputLayer(65))
layers.append_element(NeuralLogisticLayer(65))
layers.append_element(NeuralSoftmaxLayer(32))
net=NeuralNetwork(layers)
net.quick_connect()
net.initialize()

net.io.set_loglevel(MSG_INFO)
net.l1_coefficient=3e-4
net.epsilon = 1e-6
net.max_num_epochs = 600

net.set_labels(sg_labels)
net.train(sg_features) 
return net

错误:

AttributeError   Traceback (most recent call last)
 <ipython-input-28-30225c91fe73> in <module>()
 ----> 1 net=get_ann(data_map)

 <ipython-input-27-809f097ce563> in get_ann(data_map)
  59     net=NeuralNetwork(layers)
  60     net.quick_connect()
 ---> 61     net.initialize()
  62 
  63     net.io.set_loglevel(MSG_INFO)

 AttributeError: 'NeuralNetwork' object has no attribute 'initialize'

使用的平台:Ubuntu 14.04,Python 2.7,opencv-2.4.9,iPython notebook和shogun toolbox。
任何人都可以请帮助我解决这个错误?提前感谢.其他代码示例如下,已执行上述代码之前.

from modshogun import *
def get_vstacked_data(path):
    filenames=np.array(get_imlist(path))
    #read the image
    #convert the image into grayscale.
    #change its data-type to double.
    #flatten it
    vmat=[]
    for i in range(filenames[0].shape[0]):
    temp=cv2.imread(filenames[0][i])
    temp=cv2.cvtColor(temp, cv2.COLOR_BGR2GRAY)
    temp=cv2.equalizeHist(temp)
    temp=np.array(temp, dtype='double')
    temp=temp.flatten()
    vmat.append(temp)
    vmat=np.vstack(vmat)
    return vmat
    def get_svm():
#set path for positive training images
path_train='/home/sagar/resized/'
pos_trainmat=get_vstacked_data(path_train)

#set path for negative training images
path_train='/home/sagar/rezize/'
neg_trainmat=get_vstacked_data(path_train)

#form the observation matrix
obs_matrix=np.vstack([pos_trainmat, neg_trainmat])

#shogun works in a way in which columns are samples and rows are features.
#Hence we need to transpose the observation matrix
obs_matrix=obs_matrix.T

#get the labels. Positive training images are marked with +1 and negative with -1
labels=np.ones(obs_matrix.shape[1])
labels[pos_trainmat.shape[0]:obs_matrix.shape[1]]*=-1

#convert the observation matrix and the labels into Shogun RealFeatures and BinaryLabels structures resp. .
sg_features=RealFeatures(obs_matrix)
sg_labels=BinaryLabels(labels)

#Initialise a basic LibSVM in Shogun.
width=2
#kernel=GaussianKernel(sg_features, sg_features, width)
kernel=LinearKernel(sg_features, sg_features)
C=1.0
svm=LibSVM(C, kernel, sg_labels)
_=svm.train()

_=svm.apply(sg_features)
return svm

OCR分类

def validate_ann(cnt):
rect=cv2.minAreaRect(cnt)  
box=cv2.cv.BoxPoints(rect) 
box=np.int0(box) 
output=False
width=rect[1][0]
height=rect[1][1]
if ((width!=0) & (height!=0)):
    if (((height/width>1.12) & (height>width)) | ((width/height>1.12) & (width>height))):
        if((height*width<1700) & (height*width>100)):
            if((max(width, height)<64) & (max(width, height)>35)):
                output=True
return output
beq87vna

beq87vna1#

可能有一些方法弃用的问题与将军。
尝试替换:

net.initialize()

有:

net.initialize_neural_network()
q9rjltbz

q9rjltbz2#

请更改

from modshogun import *' to 'from shogun import *

请同时将ocr分类下的box=cv2.cv.BoxPoints(rect)更改为

box=cv2.boxPoints(rect)

相关问题