# -*- coding: utf-8 -*-
"""
Created on Tue Apr 22 21:03:06 2014
@author: break
"""
import numpy as np
import theano
import theano.tensor as T
from theano.tensor.nnet import conv
from theano.tensor.signal import downsample
from dataset import loadFaceVerifyDataSet
import gzip
import cPickle
class MyConvPoolLayer(object):
"""Pool Layer of a convolutional network """
def __init__(self, rng, input1, input2, filter_shape, image_shape, poolsize=(2, 2)):
"""
Allocate a MyConvPoolLayer with shared variable internal parameters.
:type rng: numpy.random.RandomState
:param rng: a random number generator used to initialize weights
:type input1: theano.tensor.dtensor4
:param input1: symbolic image tensor, of shape image_shape, the one image of a pair
:type input2: theano.tensor.dtensor4
:param input2: symbolic image tensor, of shape image_shape, the other image of a pair
:type filter_shape: tuple or list of length 4
:param filter_shape: (number of filters, num input feature maps,
filter height,filter width)
:type image_shape: tuple or list of length 4
:param image_shape: (,num input feature maps,
image height, image width)
:type poolsize: tuple or list of length 2
:param poolsize: the downsampling (pooling) factor (#rows,#cols)
"""
assert image_shape[1] == filter_shape[1]
self.input1 = input1
self.input2 = input2
# there are "num input feature maps * filter height * filter width"
# inputs to each hidden unit
fan_in = np.prod(filter_shape[1:])
# each unit in the lower layer receives a gradient from:
# "num output feature maps * filter height * filter width" /
# pooling size
fan_out = (filter_shape[0] * np.prod(filter_shape[2:]) /
np.prod(poolsize))
# initialize weights with random weights
W_bound = np.sqrt(6. / (fan_in + fan_out))
self.W = theano.shared(np.asarray(
rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
dtype=theano.config.floatX),
borrow=True)
# the bias is a 1D tensor -- one bias per output feature map
b_values = np.zeros((filter_shape[0],), dtype=theano.config.floatX)
self.b = theano.shared(value=b_values, borrow=True)
# convolve input feature maps with filters
conv_out1 = conv.conv2d(input=input1, filters=self.W,
filter_shape=filter_shape, image_shape=image_shape)
conv_out2 = conv.conv2d(input=input2, filters=self.W,
filter_shape=filter_shape, image_shape=image_shape)
# downsample each feature map individually, using maxpooling
pooled_out1 = downsample.max_pool_2d(input=conv_out1,
ds=poolsize, ignore_border=True)
pooled_out2 = downsample.max_pool_2d(input=conv_out2,
ds=poolsize, ignore_border=True)
# add the bias term. Since the bias is a vector (1D array), we first
# reshape it to a tensor of shape (1,n_filters,1,1). Each bias will
# thus be broadcasted across mini-batches and feature map
# width & height
self.output1 = T.tanh(pooled_out1 + self.b.dimshuffle('x', 0, 'x', 'x'))
self.output2 = T.tanh(pooled_out2 + self.b.dimshuffle('x', 0, 'x', 'x'))
# store parameters of this layer
self.params = [self.W, self.b]
out_h = (image_shape[2] - filter_shape[2] + 1) / poolsize[0]
out_w = (image_shape[3] - filter_shape[3] + 1) / poolsize[1]
self.out_shape = (1,filter_shape[0], out_h, out_w)
class HiddenLayer(object):
def __init__(self, rng, input1, input2, n_in, n_out, W=None, b=None,
activation=T.tanh):
"""
Typical hidden layer of a MLP: units are fully-connected and have
sigmoidal activation function. Weight matrix W is of shape (n_in,n_out)
and the bias vector b is of shape (n_out,).
NOTE : The nonlinearity used here is tanh
Hidden unit activation is given by: tanh(dot(input,W) + b)
:type rng: numpy.random.RandomState
:param rng: a random number generator used to initialize weights
:type input1: theano.tensor.dmatrix
:param input1: a symbolic tensor of shape ( n_in)
:type input2: theano.tensor.dmatrix
:param input2: a symbolic tensor of shape ( n_in)
:type n_in: int
:param n_in: dimensionality of input
:type n_out: int
:param n_out: number of hidden units
:type activation: theano.Op or function
:param activation: Non linearity to be applied in the hidden
layer
"""
self.input1 = input1
self.input2 = input2
# `W` is initialized with `W_values` which is uniformely sampled
# from sqrt(-6./(n_in+n_hidden)) and sqrt(6./(n_in+n_hidden))
# for tanh activation function
# the output of uniform if converted using asarray to dtype
# theano.config.floatX so that the code is runable on GPU
# Note : optimal initialization of weights is dependent on the
# activation function used (among other things).
# For example, results presented in [Xavier10] suggest that you
# should use 4 times larger initial weights for sigmoid
# compared to tanh
# We have no info for other function, so we use the same as
# tanh.
if W is None:
W_values = np.asarray(rng.uniform(
low=-np.sqrt(6. / (n_in + n_out)),
high=np.sqrt(6. / (n_in + n_out)),
size=(n_in, n_out)), dtype=theano.config.floatX)
if activation == theano.tensor.nnet.sigmoid:
W_values *= 4
W = theano.shared(value=W_values, name='W', borrow=True)
if b is None:
b_values = np.zeros((n_out,), dtype=theano.config.floatX)
b = theano.shared(value=b_values, name='b', borrow=True)
self.W = W
self.b = b
lin_output1 = T.dot(input1, self.W) + self.b
self.output1 = (lin_output1 if activation is None
else activation(lin_output1))
lin_output2 = T.dot(input2, self.W) + self.b
self.output2 = (lin_output2 if activation is None
else activation(lin_output2))
# parameters of the model
self.params = [self.W, self.b]
def loss(self,delta):
#return T.log(1+T.exp(euclid(self.output1,self.output2)))
#return T.log(1+T.exp(T.sqrt(T.sum(T.sqr(self.output1-self.output2)))))
#return T.log(1+T.exp(T.sqrt(T.sum(T.sqr(self.output1)))))
return T.log(1+T.exp(delta*(T.sum(T.sqr(self.output1-self.output2)))))
def euclid(I_1, I_2):
"""
compute euclid distance of two vectors
"""
return T.sqrt(T.sum(T.sqr(I_1-I_2)))
def faceRecognition(learning_rate=0.1, n_epochs=10,
dataset='face_data_pcnn.pkl.gz',
nkerns=[10, 20], outDims=[324, 98]):
"""
Face recognition with Pyramid CNN architecture
"""
assert len(nkerns) == len(outDims)
levels = len(nkerns)
rng = np.random.RandomState(12345)
data_x, data_y = loadFaceVerifyDataSet(dataset)
data_length = len(data_x.get_value(borrow=True))
print "data_length:%d" % data_length
# allocate symbolic variables for the data
index_i = T.iscalar()
x1 = T.vector('x1') #
x2 = T.vector('x2') #image pair
delta = T.iscalar('delta') #label
######################
# BUILD ACTUAL MODEL #
######################
print('... building the mode
pcnn人脸识别matlab
需积分: 9 79 浏览量
2018-12-09
14:43:24
上传
评论 3
收藏 636KB ZIP 举报
小吕同学吖
- 粉丝: 81
- 资源: 5
最新资源
- yolov5,SSD 可能使用到的一些代码
- 介绍离散性制造行业的MES系统流程
- 基于IDEA-CCNL/Randeng-Pegasus-238M-Summary-Chines微调的中文文本摘要任务源码+数据集
- 微信小程序源码 车源宝 二手车交易平台 源码下载
- 微信小程序源码 实现 城市切换 demo 根据城市首字母排序城市 选择城市 源码下载
- 2024新版计算机编译原理期末速成全套视频教程(视频+配套资料)
- VMware7.0虚拟机硬盘无法编辑,无法连接到Profile-Driven Storage Service
- arm64内核的mongo镜像
- 基于stm32f103c单片机+MPU6050+0.96英寸OLED显示屏双柄遥控器硬件(原理图+PCB)工程文件.zip
- 整理的关于少儿编程的学习路径,以及如何在小升初,初升高和大学充分的利用起来编程经验的优势
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈