资源简介

VGG16实现人脸检测

资源截图

代码片段和文件信息

# coding: utf-8

import os
import tensorflow as tf
import numpy as np

# new params

num_classes = 2
VGG_MEAN = [103.062623801 115.902882574 123.151630838]


def load_graph():
    with tf.device(‘/cpu:0‘):
        g = tf.Graph()
        with g.as_default():
            x = tf.placeholder(tf.float32 shape=[1 112 112 3] name=‘x‘)
            prob = Net(x)
        return g x prob


def _get_variable(name shape initializer dtype=‘float‘ trainable=True):
    return tf.get_variable(name shape=shape initializer=initializer trainable=trainable dtype=dtype)


# 定义卷积 conv
def conv_layer(input
               num_input_features
               num_out_features
               filter_size
               stride
               padding=‘SAME‘
               name=None
               dtype=‘float‘
               trainable=True):
    with tf.variable_scope(name):
        # weights shape
        shape_weights = [filter_size filter_size num_input_features num_out_features]
        shape_biases = [num_out_features]

        initilizer_weights = tf.truncated_normal_initializer(stddev=0.02)
        initilizer_biases = tf.truncated_normal_initializer(stddev=0.02)

        weights = _get_variable(‘weights‘
                                shape=shape_weights
                                initializer=initilizer_weights
                                trainable=True)
        biases = _get_variable(‘biases‘
                               shape=shape_biases
                               initializer=initilizer_biases
                               trainable=True)

        conv = tf.nn.conv2d(input weights strides=[1 stride stride 1] padding=padding)
        return tf.nn.bias_add(conv biases)


# 定义非线性激励函数,relu
def action(input):
    return tf.nn.relu(input)


# 定义max pooling
def max_pool(input ksize stride pading name):
    return tf.nn.max_pool(input
                          ksize=[1 ksize ksize 1]
                          strides=[1 stride stride 1]
                          padding=pading
                          name=name)


# 展平
def flatten_layer(layer):
    layer_shape = layer.get_shape()

    # get input features
    num_input_features = layer_shape[1:4].num_elements()

    layer_flat = tf.reshape(layer [-1 num_input_features])

    return layer_flat num_input_features


# 定义全连接 FC层
def fc_layer(input num_output_features name):
    with tf.variable_scope(name):
        layer_flat num_input_features = flatten_layer(input)
        shape_fc_weights = [num_input_features num_output_features]
        shape_fc_biases = [num_output_features]

        initilizer_fc_weights = tf.truncated_normal_initializer(stddev=0.02)
        initilizer_fc_biases = tf.truncated_normal_initializer(stddev=0.02)

        fc_weights = _get_variable(‘weights‘ shape=shape_fc_weights initializer=initilizer_fc_weights trainable=True)
        f

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件           0  2020-09-25 07:11  vgg16瀹炵幇浜鸿劯妫€娴?
     文件        9201  2019-05-13 09:52  vgg16瀹炵幇浜鸿劯妫€娴?Vgg16.py
     文件         276  2019-05-13 09:52  __MACOSX\vgg16瀹炵幇浜鸿劯妫€娴?._Vgg16.py

评论

共有 条评论