• 大小: 14.29MB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2023-06-29
  • 语言: Python
  • 标签: BP算法  人脸识别  

资源简介

利用Python实现的BP神经网络进行人脸识别,源码公开,打开就能使用,欢迎大家学习借鉴,进制使用于非法用途或者有损他人利益。

资源截图

代码片段和文件信息

#coding=utf8
from neuralnetworktool import *
import random
import traceback
class Neuron():
def __init__(selfnInput = 0studyspeed = 1thresholdFunc = sigmoidX):
self.nInput = nInput
#init the weight in range -1 to 1
randomweights = [random.uniform(-1 1) for i in range(nInput)]
self.weights = randomweights
self.thresholdFunc = thresholdFunc
self.studyspeed = studyspeed
self.inputs = []

def setInputs(selfinputs):
self.inputs = inputs

def getInputs(self):
return self.inputs

def getOutput(selfinputs):
self.inputs = inputs
outputs = np.squeeze(np.asarray(self.weights * inputs))
#for i in range(len(self.weights)):
# outputs += self.weights[i] * self.inputs[i]
return round(self.thresholdFunc(outputs)3)


def adjustWeights(selfinputsexceptOutputrealOutput = None):
if realOutput == None:
f = self.getOutput(inputs)
else:
f = realOutput
d = exceptOutput
r = (d - f) * f * (1 - f)
self.adjustWeightsWithR(r)
return r

#R反映出网络输出的平方差对相应的sigmoid函数的输入中的变化的敏感度
def adjustWeightsWithR(selfR):
self.weights += self.studyspeed * R *  np.transpose(self.inputs)

def getWeights(self):
return np.squeeze(np.asarray(self.weights))

def getWeightsMatrix(self):
return self.weights

def setWeights(selfweights):
self.weights = np.matrix(weights)

def setStudySpeed(selfstudyspeed):
self.studyspeed = studyspeed

def printInfo(self):
print self.weights


class NeuralNetWork():
def __init__(selfnInput=0nHide=0nOutput=0studyspeed=0):
self.nInput = nInput
self.nHide = nHide
self.nOutput = nOutput
self.studyspeed = studyspeed
self.inputNeurons = [0 for i in range(nInput)]
self.hideNeurons = [
Neuron(nInput = nInputstudyspeed = studyspeed) for i in range(nHide)]
self.outputNeurons = [
Neuron(nInput = nHidestudyspeed = studyspeed) for i in range(nOutput)]


def backpropagation(selftrainings=[]examples=[]):
for i in range(len(trainings)):
self.backpropagationSingle(
training = trainings[i]
example = examples[i]


def backpropagationSingle(selftrainingexample):
#d为训练集目标输出
d = example
#输入转化为单列矩阵
self.inputNeurons = np.transpose(np.matrix(training))
#用于保存隐藏层输出
hideoutputs = []
#对输入层作为输入,得到隐藏层输出结果
for hideNeuron in self.hideNeurons:
hideoutputs.append(hideNeuron.getOutput(self.inputNeurons))


#将隐藏层结果作为输出层的输入,得到输出层的结果
outputlayeroutputs = []
hideOutPutMatrixs =  np.transpose(np.matrix(hideoutputs))
for outputNeuron in self.outputNeurons:
outputlayeroutputs.append(outputNeuron.getOutput(hideOutPutMatrixs))
#输出层结果与期望结果做比较,并且反向传播调节
#保存调节前输出层权值
beforeweights = []

#对输出层进行调节
#保存输出层各个神经元的调节因子
outputRs = []
for i in range(self.nOutput):
#f为实际输出结果
f = outputlayeroutputs[i]
outputR = (d[i] - f) * f * (1 - f)
outputRs.append(outputR)
#保存第i个输出神经元的调节前权值
beforeweights.append(self.outputNeurons[i].getWeights())
#对每个输出层神经元进行调节
self.outputNeurons[i].adjustWeightsWithR(outputR)
#对隐藏层进行调节
for i in range(s

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-05-05 03:06  NeuralNetwork-master\
     文件         378  2016-05-05 03:06  NeuralNetwork-master\.gitattributes
     文件         655  2016-05-05 03:06  NeuralNetwork-master\.gitignore
     文件         744  2016-05-05 03:06  NeuralNetwork-master\README.md
     文件           0  2016-05-05 03:06  NeuralNetwork-master\TestConfig.txt
     目录           0  2016-05-05 03:06  NeuralNetwork-master\data\
     目录           0  2016-05-05 03:06  NeuralNetwork-master\data\faces\
     文件         131  2016-05-05 03:06  NeuralNetwork-master\data\faces\.anonr
     目录           0  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\
     文件       42648  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_angry_open.pgm
     文件        3853  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_angry_open_2.pgm
     文件         973  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_angry_open_4.pgm
     文件       42480  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_angry_sunglasses.pgm
     文件        3853  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_angry_sunglasses_2.pgm
     文件         973  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_angry_sunglasses_4.pgm
     文件       42802  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_happy_open.pgm
     文件        3853  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_happy_open_2.pgm
     文件         973  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_happy_open_4.pgm
     文件       42944  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_happy_sunglasses.pgm
     文件        3853  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_happy_sunglasses_2.pgm
     文件         973  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_happy_sunglasses_4.pgm
     文件       43292  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_neutral_open.pgm
     文件        3853  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_neutral_open_2.pgm
     文件         973  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_neutral_open_4.pgm
     文件       42974  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_neutral_sunglasses.pgm
     文件        3853  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_neutral_sunglasses_2.pgm
     文件         973  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_neutral_sunglasses_4.pgm
     文件       42693  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_sad_open.pgm
     文件        3853  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_sad_open_2.pgm
     文件         973  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_sad_open_4.pgm
     文件       42967  2016-05-05 03:06  NeuralNetwork-master\data\faces\an2i\an2i_left_sad_sunglasses.pgm
............此处省略1896个文件信息

评论

共有 条评论