PyBrain网络给出了错误的结果

时间:2016-07-02 12:10:34

标签: python neural-network pybrain

我正在尝试训练网络以进行基本图像识别。我尝试用 if (mystring.contains(context.getResources().getString(R.string.testString))) { check = true; } if (mystring.equals(context.getResources().getString(R.string.testString))) { check = true; } 进行训练,但它给了我奇怪的结果。我有132个样本。每个图像都是基本的灰度图像。这是我的培训和分类部分代码

培训

ClassificationDataSet

分类

import os
import numpy as np

from pybrain.datasets import ClassificationDataSet
from pybrain.supervised import BackpropTrainer
from pybrain.tools.shortcuts import buildNetwork
from pybrain.tools.xml.networkwriter import NetworkWriter




#letter map
resdict = { '0':0, '1':1 ,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'a':10, 'b':11, 'c':12, 'd':13, 'f':14, 'g':15, 'h':16, 'j':17, 'k':18, 'm':19, 'n':20, 'q':21, 'r':22, 's':23, 't':24, 'v':25, 'w':26, 'x':27,'y':28, 'z':29}

#coordinates of letters
train_ds = ClassificationDataSet(14*24,1)
FOLDER_LOCATION = ''

#loop each box according to x position
for filename in os.listdir(FOLDER_LOCATION):
    if filename.endswith(".jpg"):
        index = 0

        #preprocess the image
        # im_grey Grayscale image
        # box coordinates of each letter's bounding box
        # ex : box = {0: (22, 15, 34, 39), 1: (7, 16, 19, 39), 2: (87, 16, 98, 38), 4: (54, 22, 67, 39), 7: (37, 23, 51, 38), 12: (71, 23, 82, 39)}
        (im_grey,box) = preprocess(FOLDER_LOCATION + filename)

        for boxId in sorted(box, key=lambda x: box[x][0]):
            (minX,minY,maxX,maxY) = box[boxId]
            if (maxY - minY) > 10:
                #this is a blob that we care
                region = im_grey.crop((box[boxId])).resize((14, 24))
                label = resdict[filename[index]]

                srcdata = np.ravel(np.array(region) / 255)
                print filename,filename[index],label,srcdata

                train_ds.addSample(srcdata,label)
                index = index + 1

net = buildNetwork(train_ds.indim, 98, train_ds.outdim, bias=True)
trainer = BackpropTrainer(net, train_ds)
print "start training"
trainer.trainUntilConvergence()


print "finish training"
file = "newNetwork.xml"
NetworkWriter.writeToFile(net, file)

当我尝试分类时,会打印错误的值,例如

import os
import numpy as np

from pybrain.tools.xml.networkreader import NetworkReader

FOLDER_LOCATION = ''
net  = NetworkReader.readFrom('newNetWork.xml')
resdict = { '0':0, '1':1 ,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'a':10, 'b':11, 'c':12, 'd':13, 'f':14, 'g':15, 'h':16, 'j':17, 'k':18, 'm':19, 'n':20, 'q':21, 'r':22, 's':23, 't':24, 'v':25, 'w':26, 'x':27,'y':28, 'z':29}

for filename in os.listdir(FOLDER_LOCATION):
    if filename.endswith(".jpg"):
        index = 0

        #preprocess the image
        # im_grey Grayscale image
        # box coordinates of each letter's bounding box
        # ex : box = {0: (22, 15, 34, 39), 1: (7, 16, 19, 39), 2: (87, 16, 98, 38), 4: (54, 22, 67, 39), 7: (37, 23, 51, 38), 12: (71, 23, 82, 39)}
        (im_grey,box) = preprocess(FOLDER_LOCATION + filename)

        for boxId in sorted(box, key=lambda x: box[x][0]):
            (minX,minY,maxX,maxY) = box[boxId]
            if (maxY - minY) > 10:
                #this is a blob that we care
                region = im_grey.crop((box[boxId])).resize((14, 24))
                label = resdict[filename[index]]
                srcdata = np.ravel(np.array(region) / 255)
                out = net.activate(srcdata)
                l = out.tolist()
                max_v = max(l)
                print "======START============"
                max_i = l.index(max_v)
                print filename,filename[index],label
                print "Max", max_v
                print "Guess:", max_i
                print "RAW:", out
                print "======END============"

                index = index + 1

有时数字部分似乎没问题,但有时它需要四舍五入,有时它完全错了。培训这种网络的正确方法是什么?

0 个答案:

没有答案