Tensorflow相关知识(一)MTCNN代码相关

以MTCNN为例,顺便学习下TensorFlow的底层用法。

目录

一、tf.device()

二、tf.Graph().as_default()

2.1 tf.Graph

2.2 tf.Graph().as_default()

2.3 MTCNN之中as_default()

三、tf.layers()

四、tf.ConfigProto指定显卡相关

自动选择运行设备 : tf.ConfigProto(allow_soft_placement=True)

记录设备指派情况 :  tf.ConfigProto(log_device_placement=True)

动态申请内存

限制显存资源使用

指定GPU

五、tf.Session()

5.1 通过python管理器使用session

5.2 MTCNN中的session

六、tf.train.Saver模型的保存与恢复

6.1 模型保存,先要创建一个Saver对象

6.2 max_to_keep 保存模型的个数

6.3 保存训练模型

6.4 MTCNN中的saver

七、global_variables获取全局变量


一、tf.device()

指定运行设备,例如with tf.device('/cpu'):或者with tf.device('\gpu:0‘):

https://blog.csdn.net/LoseInVain/article/details/78814091

如果需要切换成CPU运算,可以调用tf.device(device_name)函数,其中device_name格式如/cpu:0其中的0表示设备号,TF不区分CPU的设备号,设置为0即可。GPU区分设备号\gpu:0\gpu:1表示两张不同的显卡。

2018-10-17 20:03:23.128241: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2018-10-17 20:03:23.128270: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2018-10-17 20:03:23.128290: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-10-17 20:03:23.128296: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-10-17 20:03:23.128302: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2018-10-17 20:03:23.439851: I tensorflow/core/common_runtime/gpu/gpu_device.cc:955] Found device 0 with properties:
name: GeForce GTX 1080 Ti
major: 6 minor: 1 memoryClockRate (GHz) 1.582
pciBusID 0000:83:00.0
Total memory: 10.92GiB
Free memory: 9.22GiB
2018-10-17 20:03:23.439906: I tensorflow/core/common_runtime/gpu/gpu_device.cc:976] DMA: 0
2018-10-17 20:03:23.439916: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 0:   Y
2018-10-17 20:03:23.439945: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:83:00.0)

二、tf.Graph().as_default()

官方文档位置: https://tensorflow.google.cn/api_docs/python/tf/Graph

https://www.cnblogs.com/studylyn/p/9105818.html

https://blog.csdn.net/xierhacker/article/details/53860379

2.1 tf.Graph

dataflow graph是一系列TensorFlow 运算的集合。是一系列tf.Operation的实体。表示实例化了一个类,一个用于 tensorflow 计算和表示用的数据流图

默认的Graph可以通过tf.get_default_graph调用出来,如果想要给default graph加相应的operation,则简单的调用相应的function就可以。

c = tf.constant(4.0)
assert c.graph is tf.get_default_graph()

另一种方法是运用函数 tf.Graph.as_default, 可以将当前的默认graph进行覆盖

g = tf.Graph()
with g.as_default():
  # Define operations and tensors in `g`.
  c = tf.constant(30.0)
  assert c.graph is g

2.2 tf.Graph().as_default()

函数回归一个车context manager将当前Graph设置为default graph 

# 1. Using Graph.as_default():
g = tf.Graph()
with g.as_default():
  c = tf.constant(5.0)
  assert c.graph is g

# 2. Constructing and making default:
with tf.Graph().as_default() as g:
  c = tf.constant(5.0)
  assert c.graph is g

2.3 MTCNN之中as_default()

    with tf.device('/gpu:3'):
        with tf.Graph().as_default():
            config = tf.ConfigProto(allow_soft_placement=True)
            with tf.Session(config=config) as sess:
                print("\n"+"LOCATION!!!tf config done"+"\n")
                if len(file_paths) == 3:
                    print("\n"+"LOCATION!!!file_paths(model_dir)=3"+"\n")
                    image_pnet = tf.placeholder(
""" ........

三、tf.layers()

https://blog.csdn.net/xierhacker/article/details/82747919

用于实现神经网络中相应的层。

  • Input(…): 用于实例化一个输入 Tensor,作为神经网络的输入。
  • average_pooling1d(…): 一维平均池化层
  • average_pooling2d(…): 二维平均池化层
  • average_pooling3d(…): 三维平均池化层
  • batch_normalization(…): 批量标准化层
  • conv1d(…): 一维卷积层
  • conv2d(…): 二维卷积层
  • conv2d_transpose(…): 二维反卷积层
  • conv3d(…): 三维卷积层
  • conv3d_transpose(…): 三维反卷积层
  • dense(…): 全连接层
  • dropout(…): Dropout层
  • flatten(…): Flatten层,即把一个 Tensor 展平
  • max_pooling1d(…): 一维最大池化层
  • max_pooling2d(…): 二维最大池化层
  • max_pooling3d(…): 三维最大池化层
  • separable_conv2d(…): 二维深度可分离卷积层

例如 tf.layers.batch_normalization

tf.layers.batch_normalization(
    inputs,
    axis=-1,
    momentum=0.99,
    epsilon=0.001,
    center=True,
    scale=True,
    beta_initializer=tf.zeros_initializer(),
    gamma_initializer=tf.ones_initializer(),
    moving_mean_initializer=tf.zeros_initializer(),
    moving_variance_initializer=tf.ones_initializer(),
    beta_regularizer=None,
    gamma_regularizer=None,
    beta_constraint=None,
    gamma_constraint=None,
    training=False,
    trainable=True,
    name=None,
    reuse=None,
    renorm=False,
    renorm_clipping=None,
    renorm_momentum=0.99,
    fused=None,
    virtual_batch_size=None,
    adjustment=None
)

四、tf.ConfigProto指定显卡相关

https://blog.csdn.net/dcrmg/article/details/79091941

用于配置Session运行参数与GPU设备指定,创建session的时候,用来对session进行参数配置

config = tf.ConfigProto(allow_soft_placement=True, allow_soft_placement=True)
config.gpu_options.per_process_gpu_memory_fraction = 0.4  #占用40%显存
sess = tf.Session(config=config)

自动选择运行设备 : tf.ConfigProto(allow_soft_placement=True)

在tf中,通过命令 "with tf.device('/cpu:0'):",允许手动设置操作运行的设备。如果手动设置的设备不存在或者不可用,就会导致tf程序等待或异常,为了防止这种情况,可以设置tf.ConfigProto()中参数allow_soft_placement=True,允许tf自动选择一个存在并且可用的设备来运行操作。

记录设备指派情况 :  tf.ConfigProto(log_device_placement=True)

设置tf.ConfigProto()中参数log_device_placement = True ,可以获取到 operations 和 Tensor 被指派到哪个设备(几号CPU或几号GPU)上运行,会在终端打印出各项操作是在哪个设备上运行的。

动态申请内存

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4  #占用40%显存
session = tf.Session(config=config)

限制显存资源使用

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4  #占用40%显存
session = tf.Session(config=config)
#或者
gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.4)
config=tf.ConfigProto(gpu_options=gpu_options)
session = tf.Session(config=config)

指定GPU

程序中

os.environ['CUDA_VISIBLE_DEVICES'] = '0' #使用 GPU 0
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1' # 使用 GPU 0,1

命令行

CUDA_VISIBLE_DEVICES=0,1 python yourcode.py

五、tf.Session()

https://blog.csdn.net/qq_14839543/article/details/77822916?locationNum=1&fps=1

https://blog.csdn.net/xierhacker/article/details/53860379

tf.Session():需要在启动session之前构建整个计算图,然后启动该计算图。session包括了执行对象的环境

from __future__ import print_function,division 
import tensorflow as tf 
#build graph 
a=tf.constant(2.) 
b=tf.constant(5.) 
c=a*b 
#construct session 
sess=tf.Session() 
#Evaluate the tensor `c` 
print(sess.run(c)) 
#close session 
sess.close()

5.1 通过python管理器使用session

用法:

#创建一个会话,并通过Python中的上下文管理器来管理 
with tf.Session() as sess: 
#使用这个会话来计算关心的结果 
    sess.run(...) 
    #不再需要调用“Session.close()”函数来关闭会话,当上下文退出时会话关闭和资源释放也会自动完成。

例如:

import tensorflow as tf 
#使用张量记录中间结果 
a = tf.constant([1.0, 2.0], name="a") 
b = tf.constant([2.0, 3.0], name="b") 
result = a + b 
with tf.Session() as sess: 
    print(sess.run(result))

5.2 MTCNN中的session

            with tf.Session(config=config) as sess:
                print("\n"+"LOCATION!!!tf config done"+"\n")
                if len(file_paths) == 3:
                    print("\n"+"LOCATION!!!file_paths(model_dir)=3"+"\n")
                    image_pnet = tf.placeholder(
                        tf.float32, [None, None, None, 3])
                    pnet = PNet({'data': image_pnet}, mode='test')
                    out_tensor_pnet = pnet.get_all_output()

                    image_rnet = tf.placeholder(tf.float32, [None, 24, 24, 3])
                    rnet = RNet({'data': image_rnet}, mode='test')
                    out_tensor_rnet = rnet.get_all_output()

                    image_onet = tf.placeholder(tf.float32, [None, 48, 48, 3])
                    onet = ONet({'data': image_onet}, mode='test')
                    out_tensor_onet = onet.get_all_output()
                    
                    print("\n"+"LOCATION!!!placeholder and out_tensor done"+"\n")

                    saver_pnet = tf.train.Saver(
                                    [v for v in tf.global_variables()
                                     if v.name[0:5] == "pnet/"])
                    saver_rnet = tf.train.Saver(
                                    [v for v in tf.global_variables()
                                     if v.name[0:5] == "rnet/"])
                    saver_onet = tf.train.Saver(
                                    [v for v in tf.global_variables()
                                     if v.name[0:5] == "onet/"])

                    saver_pnet.restore(sess, file_paths[0])
                    
                    print("\n"+"LOCATION!!!saver done"+"\n")

                    def pnet_fun(img): return sess.run(
                        out_tensor_pnet, feed_dict={image_pnet: img})

                    saver_rnet.restore(sess, file_paths[1])

                    def rnet_fun(img): return sess.run(
                        out_tensor_rnet, feed_dict={image_rnet: img})

                    saver_onet.restore(sess, file_paths[2])

                    def onet_fun(img): return sess.run(
                        out_tensor_onet, feed_dict={image_onet: img})
                    print("\n"+"LOCATION!!!def net_fun done"+"\n")

 

六、tf.train.Saver模型的保存与恢复

https://blog.csdn.net/qiqiaiairen/article/details/53184216

https://www.cnblogs.com/denny402/p/6940134.html

https://blog.csdn.net/u011500062/article/details/51728830

将训练好的模型参数保存起来,以便以后进行验证或测试,这是我们经常要做的事情。tf里面提供模型保存的是tf.train.Saver()模块。

6.1 模型保存,先要创建一个Saver对象

saver=tf.train.Saver()

6.2 max_to_keep 保存模型的个数

在创建这个Saver对象的时候,有一个参数我们经常会用到,就是 max_to_keep 参数,这个是用来设置保存模型的个数,默认为5,即 max_to_keep=5,保存最近的5个模型。如果你想每训练一代(epoch)就想保存一次模型,则可以将 max_to_keep设置为None或者0,如:

saver=tf.train.Saver(max_to_keep=0)

但是这样做除了多占用硬盘,并没有实际多大的用处,因此不推荐。

当然,如果你只想保存最后一代的模型,则只需要将max_to_keep设置为1即可,即

saver=tf.train.Saver(max_to_keep=1)

6.3 保存训练模型

创建完saver对象后,就可以保存训练好的模型了,如:

saver.save(sess,'ckpt/mnist.ckpt',global_step=step)

第一个参数sess,这个就不用说了。第二个参数设定保存的路径和名字,第三个参数将训练的次数作为后缀加入到模型名字中。

saver.save(sess, 'my-model', global_step=0) ==>      filename: 'my-model-0'
...
saver.save(sess, 'my-model', global_step=1000) ==> filename: 'my-model-1000'

6.4 MTCNN中的saver

                    saver_pnet = tf.train.Saver(
                                    [v for v in tf.global_variables()
                                     if v.name[0:5] == "pnet/"])
                    saver_rnet = tf.train.Saver(
                                    [v for v in tf.global_variables()
                                     if v.name[0:5] == "rnet/"])
                    saver_onet = tf.train.Saver(
                                    [v for v in tf.global_variables()
                                     if v.name[0:5] == "onet/"])

                    saver_pnet.restore(sess, file_paths[0])
                    
                    print("\n"+"LOCATION!!!saver done"+"\n")

                    def pnet_fun(img): return sess.run(
                        out_tensor_pnet, feed_dict={image_pnet: img})

                    saver_rnet.restore(sess, file_paths[1])

                    def rnet_fun(img): return sess.run(
                        out_tensor_rnet, feed_dict={image_rnet: img})

                    saver_onet.restore(sess, file_paths[2])

                    def onet_fun(img): return sess.run(
                        out_tensor_onet, feed_dict={image_onet: img})

 

七、global_variables获取全局变量

tf.global_variables或者tf.all_variables都是获取程序中的变量

#例如:

import tensorflow as tf;  
import numpy as np;  
import matplotlib.pyplot as plt;  

v = tf.Variable(tf.constant(0.0, shape=[1], dtype=tf.float32), name='v')
v1 = tf.Variable(tf.constant(5, shape=[1], dtype=tf.float32), name='v1')

variables = tf.global_variables()

print variables[0].name
print variables[1].name

"""
输出:

v:0
v1:0
"""

你可能感兴趣的:(python,机器学习,tensorflow,MTCNN)