如何用DL4J构建起一个人脸识别系统( 三 )

接下来读取VGG16的参数,set到我们的新模型里 。为了代码方便,我们将LayerName设定的和vgg16里一样
String vggLayerNames = "conv1_1,conv1_2,conv2_1,conv2_2,conv3_1,conv3_2,conv3_3,conv4_1,conv4_2,conv4_3,conv5_1,conv5_2,conv5_3"; File vggfile = new File("F:/vgg16_dl4j_vggface_inference.v1.zip");ComputationGraph vggFace =ModelSerializer.restoreComputationGraph(vggfile);ComputationGraph model = buildModel();for (String name : vggLayerNames.split(",")) {model.getLayer(name).setParams(vggFace.getLayer(name).params().dup());}特征提取层构造完毕,提取特征之后,我们要计算距离了,这里就需要用DL4J实现自定义层,DL4J提供的自动微分可以非常方便的实现自定义层,这里我们选择 SameDiffLambdaVertex,原因是这一层不需要任何参数,仅仅计算cosine即可,代码如下:
public class CosineLambdaVertex extends SameDiffLambdaVertex { @Override public SDVariable defineVertex(SameDiff sameDiff, VertexInputs inputs) {SDVariable input1 = inputs.getInput(0);SDVariable input2 = inputs.getInput(1);return sameDiff.expandDims(sameDiff.math.cosineSimilarity(input1, input2, 1, 2, 3), 1); } @Override public InputType getOutputType(int layerIndex, InputType... vertexInputs) throws InvalidInputTypeException {return InputType.feedForward(1); }}说明:计算cosine之后这里用expandDims将一维张量拓宽成二维,是为了在LFW数据集中验证模型的准确性 。
DL4J也提供其他的自定层和自定义节点的实现,一共有如下五种:

  1. Layers: standard single input, single output layers defined using SameDiff. To implement, extend org.deeplearning4j.nn.conf.layers.samediff.SameDiffLayer
  2. Lambda layers: as above, but without any parameters. You only need to implement a single method for these! To implement, extend org.deeplearning4j.nn.conf.layers.samediff.SameDiffLambdaLayer
  3. Graph vertices: multiple inputs, single output layers usable only in ComputationGraph. To implement: extend org.deeplearning4j.nn.conf.layers.samediff.SameDiffVertex
  4. Lambda vertices: as above, but without any parameters. Again, you only need to implement a single method for these! To implement, extend org.deeplearning4j.nn.conf.layers.samediff.SameDiffLambdaVertex
  5. Output layers: An output layer, for calculating scores/losses. Used as the final layer in a network. To implement, extend org.deeplearning4j.nn.conf.layers.samediff.SameDiffOutputLayer
案例地址:https://github.com/eclipse/deeplearning4j-examples/tree/master/samediff-examples
说明文档:https://github.com/eclipse/deeplearning4j-examples/blob/master/samediff-examples/src/main/JAVA/org/nd4j/examples/samediff/customizingdl4j/README.md
接下来,还有最后一个问题,输出层怎么定义?输出层不需要任何参数和计算,仅仅将cosine结果输出即可,dl4j中提供LossLayer天然满足这种结构,没有参数,且激活函数为恒等函数IDENTITY 。那么到此为止模型构造完成,最终结构如下:
=========================================================================================================VertexName (VertexType)nIn,nOutTotalParamsParamsShapeVertex Inputs=========================================================================================================input1 (InputVertex)-,----input2 (InputVertex)-,----stack (StackVertex)-,---[input1, input2]conv1_1 (ConvolutionLayer)3,641,792W:{64,3,3,3}, b:{1,64}[stack]conv1_2 (ConvolutionLayer)64,6436,928W:{64,64,3,3}, b:{1,64}[conv1_1]pool1 (SubsamplingLayer)-,-0-[conv1_2]conv2_1 (ConvolutionLayer)64,12873,856W:{128,64,3,3}, b:{1,128}[pool1]conv2_2 (ConvolutionLayer)128,128147,584W:{128,128,3,3}, b:{1,128}[conv2_1]pool2 (SubsamplingLayer)-,-0-[conv2_2]conv3_1 (ConvolutionLayer)128,256295,168W:{256,128,3,3}, b:{1,256}[pool2]conv3_2 (ConvolutionLayer)256,256590,080W:{256,256,3,3}, b:{1,256}[conv3_1]conv3_3 (ConvolutionLayer)256,256590,080W:{256,256,3,3}, b:{1,256}[conv3_2]pool3 (SubsamplingLayer)-,-0-[conv3_3]conv4_1 (ConvolutionLayer)256,5121,180,160W:{512,256,3,3}, b:{1,512}[pool3]conv4_2 (ConvolutionLayer)512,5122,359,808W:{512,512,3,3}, b:{1,512}[conv4_1]conv4_3 (ConvolutionLayer)512,5122,359,808W:{512,512,3,3}, b:{1,512}[conv4_2]pool4 (SubsamplingLayer)-,-0-[conv4_3]conv5_1 (ConvolutionLayer)512,5122,359,808W:{512,512,3,3}, b:{1,512}[pool4]conv5_2 (ConvolutionLayer)512,5122,359,808W:{512,512,3,3}, b:{1,512}[conv5_1]conv5_3 (ConvolutionLayer)512,5122,359,808W:{512,512,3,3}, b:{1,512}[conv5_2]pool5 (SubsamplingLayer)-,-0-[conv5_3]unStack1 (UnstackVertex)-,---[pool5]unStack2 (UnstackVertex)-,---[pool5]cosine (SameDiffGraphVertex)-,---[unStack1, unStack2]out (LossLayer)-,-0-[cosine]---------------------------------------------------------------------------------------------------------Total Parameters:14,714,688Trainable Parameters:14,714,688Frozen Parameters:0=========================================================================================================


推荐阅读