#coding:utf-8
import tensorflow as tf
from tensorflow.python.framework import ops
import numpy as np
tf.reset_default_graph()
#1.prelu def prelu(x, name = 'prelu'):
with tf.variable_scope(name):
alphas = tf.get_variable('alpha', x.get_shape()[-1], initializer=tf.constant_initializer(0.25), regularizer=l2_regularizer, dtype=
tf.float32)
pos = tf.nn.relu(x)
neg = tf.multiply(alphas, (x - abs(x)) * 0.5)
return pos + neg
#2.conv with strides
def first_conv(input,num_output,name):
with tf.variable_scope(name):
zero_init = tf.zeros_initializer()
network = tf.layers.conv2d(input, num_output, kernel_size=[3, 3], strides=(2, 2), padding = 'SAME',
kernel_initializer=xavier, bias_initializer=zero_init, kernel_regularizer=l2_regularizer,
bias_regularizer = l2_regularizer)
network = prelu(network, name=name)
return network
#3.conv with residual units
def block(input, name, num_output):
with tf.variable_scope(name):
network = tf.layers.conv2d(input, num_output, kernel_size=[3, 3], strides = [1, 1],padding='SAME',
kernel_initializer=tf.random_normal_initializer(stddev=0.01), use_bias=False, kernel_regularizer= l2_regularizer)
network = prelu(network, name = 'name' + '')
network = tf.layers.conv2d(network, num_output, kernel_size=[3, 3], strides = [1, 1],padding='SAME',
kernel_initializer=tf.random_normal_initializer(stddev=0.01), use_bias=False, kernel_regularizer= l2_regularizer)
network = prelu(network, name = 'name' + '')
network = tf.add(input, network)
return network
#4.infer
l2_regularizer = tf.contrib.layers.l2_regularizer(1.0)
xavier = tf.contrib.layers.xavier_initializer_conv2d() def get_shape(tensor):
static_shape = tensor.shape.as_list()
dynamic_shape = tf.unstack(tf.shape(tensor))
dims = [s[1] if s[0] is None else s[0] for s in zip(static_shape,dynamic_shape)]
return dims
def infer(input, embedding_size=512):
with tf.variable_scope('conv1_'):
network = first_conv(input, 64, name = 'conv1')
network = block(network, 'conv1_23', 64)
with tf.variable_scope('conv2_'):
network = first_conv(network, 128, name= 'conv2')
network = block(network, 'conv2_23', 128)
network = block(network,'conv2_45', 128)
with tf.variable_scope('conv3_'):
network = first_conv(network, 256, name='conv3')
network = block(network, 'conv3_23', 256)
network = block(network, 'conv3_45', 256)
network = block(network, 'conv3_67', 256)
network = block(network, 'conv3_89', 256)
with tf.variable_scope('conv4_'):
network = first_conv(network, 512, name='conv4')
network = block(network, 'conv4_23', 512)
with tf.variable_scope('feature'):
dims = get_shape(network)
print (dims)
feature = tf.layers.dense(tf.reshape(network, [dims[0], np.prod(dims[1:])]), embedding_size, kernel_regularizer= l2_regularizer, kernel_initializer= xavier)
return feature tf.reset_default_graph()
image = tf.constant(np.random.normal(size=[1, 112, 96, 3]),dtype=tf.float32)
feature = infer(image)
print feature.get_shape()
tf.summary.FileWriter('sphereface_network', tf.get_default_graph()) #测试网络是否正确搭建 给一张图片 看网络是否能够将结果向label拟合
pred = tf.layers.dense(feature, 1)
print pred.get_shape()
loss = tf.nn.l2_loss(pred - 1)
optimizer = tf.train.GradientDescentOptimizer(0.0001).minimize(loss) with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in xrange(500):
pred_np, loss_np, _ = sess.run([pred, loss, optimizer])
if i % 20 == 0:
print (pred_np, loss_np)

最新文章

  1. xcode 删除文件后编译会出现*** is missing from working copy
  2. [Mac]关闭所有打开finder
  3. ES(一): 架构及原理
  4. 01:Geoserver发布shapfile,中文字段乱码问题
  5. 如何通过XShell传输文件
  6. 基于流的自动化构建工具------gulp (简单配置)
  7. 《SQL Server企业级平台管理实践》读书笔记——SQL Server数据库文件分配方式
  8. Python 安装 httpie
  9. 浅谈 Underscore.js 中 _.throttle 和 _.debounce 的差异
  10. openjpa框架入门_Demo概览(一)
  11. readelf -s 命令‘symbol’名字显示不全
  12. jquery获取json对象中的key小技巧
  13. delphi xe 3的EhLib 9.0 Build 9.0.033 Full Source安装
  14. Numpy入门 - 线性代数运算
  15. IOS JavaScriptCore介绍
  16. 【原创】运维基础之Zabbix(1)简介、安装、使用
  17. 剑指offer(14)
  18. CodeCraft-19 and Codeforces Round #537 Div. 2
  19. c# automapper 使用(一)
  20. Java虚拟机 内存区域划分

热门文章

  1. ACM-牛喝水
  2. jedis哨兵模式的redis组(集群),连接池实现。(客户端分片)
  3. c++程序—浮点数
  4. BurpSuite详解
  5. 中国移动携手华为百度展示5G应用,实现8K视频传输
  6. HDU 5461:Largest Point
  7. StarUML 3.0.2安装激活
  8. 查看两个集合中有没有相同的元素的方法。Collections disjoint
  9. pycharm 的 使用 设置智能目录 Pycharm 断点跳转及 Step Over/Step Into/Step Out 等的区别
  10. nodejs(10)express路由