tensorflow 笔记

1.tf.train.exponential_decay

tf.train.exponential_decay(
    learning_rate,
    global_step,
    decay_steps,
    decay_rate,
    staircase=False,
    name=None
)
decayed_learning_rate = learning_rate * decay_rate ^ (global_step / decay_steps)

If the argument staircase is True, then global_step / decay_steps is an integer division and the decayed learning rate follows a staircase function.

Example: decay every 100000 steps with a base of 0.96:

...
global_step = tf.Variable(0, trainable=False)
starter_learning_rate = 0.1
learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step,
                                           100000, 0.96, staircase=True)
# Passing global_step to minimize() will increment it at each step.
learning_step = (
    tf.train.GradientDescentOptimizer(learning_rate)
    .minimize(...my loss..., global_step=global_step)
)

2. save model

创建 SavedModel 的最简单方法是使用 tf.saved_model.simple_save 函数:

simple_save(session,
            export_dir,
            inputs={"x": x, "y": y},
            outputs={"z": z})

3. 输出tflite文件

The converter supports as input: SavedModels, frozen graphs (models generated byfreeze_graph.py), and tf.keras HDF5 models.

freeze_graph.py 方案

保存时生成 .pb(binary)或 .pbtxt(txt)格式的graph文件和checkpoint文件

# as_text 用于控制生成二进制文件 或 文本文件 
tf.train.write_graph(sess.graph_def, 'checkpoints', 'net_txt.pb', as_text=False)
saver.save(sess=sess, save_path=model_save_path, global_step=epoch)

将.pb(或.pbtxt)和 checkpoint合并为 freeze graph,注意node name间不能加空格。

freeze_graph --input_graph=/tmp/mobilenet_v1_224.pb \
  --input_checkpoint=/tmp/checkpoints/mobilenet-10202.ckpt \
  --input_binary=true \
  --output_graph=/tmp/frozen_mobilenet_v1_224.pb \
  --output_node_names=MobileNetV1/Predictions/Reshape_1,node_name2

然后通过tflite_convert 转换为 .tflite文件

tflite_convert \
  --output_file=/tmp/mobilenet_v1_1.0_224.tflite \
  --graph_def_file=/tmp/mobilenet_v1_0.50_128/frozen_graph.pb \
  --input_arrays=input \
  --output_arrays=MobilenetV1/Predictions/Reshape_1

当多输入时,注意,参数间不能加空格

tflite_convert \
  --graph_def_file=/tmp/inception_v1_2016_08_28_frozen.pb \
  --output_file=/tmp/foo.tflite \
  --input_shapes=1,28,28,96:1,28,28,16:1,28,28,192:1,28,28,64 \
  --input_arrays=InceptionV1/InceptionV1/Mixed_3b/Branch_1/Conv2d_0a_1x1/Relu,InceptionV1/InceptionV1/Mixed_3b/Branch_2/Conv2d_0a_1x1/Relu,InceptionV1/InceptionV1/Mixed_3b/Branch_3/MaxPool_0a_3x3/MaxPool,InceptionV1/InceptionV1/Mixed_3b/Branch_0/Conv2d_0a_1x1/Relu \
  --output_arrays=InceptionV1/Logits/Predictions/Reshape_1

当多输出时,注意参数间不能加空格

tflite_convert \
  --graph_def_file=/tmp/inception_v1_2016_08_28_frozen.pb \
  --output_file=/tmp/foo.tflite \
  --input_arrays=input \
  --output_arrays=InceptionV1/InceptionV1/Mixed_3b/Branch_1/Conv2d_0a_1x1/Relu,InceptionV1/InceptionV1/Mixed_3b/Branch_2/Conv2d_0a_1x1/Relu

4.加载ckpt文件获得训练权重

model_path = './checkpoints/net_2018-12-19-10-05-17.ckpt-99900'
# 读取ckpt文件
reader = tf.train.NewCheckpointReader(model_path)
# 获得所有variable
all_variables = reader.get_variable_to_shape_map()
{'conv1/biases': [16],
 'conv1/weights': [3, 3, 1, 16],
 'conv2/biases': [32],
 'conv2/weights': [3, 3, 16, 32],
 'conv3/biases': [32],
 'conv3/weights': [3, 3, 32, 32],
 'fc1/biases': [128],
 'fc1/weights': [512, 128],
 'fc2/biases': [256],
 'fc2/weights': [128, 256],
 'global_step': [],
 'logits/biases': [10],
 'logits/weights': [256, 10]}
# 通过名字获得该tensor的权重
conv1_weight = reader.get_tensor("conv1/weights")

5. 单个tensor单独加载权重

# 定义tensor 并初始化
inputs = tf.placeholder(dtype=tf.float32, shape=(1, 28, 28, 1), name='input')
conv1 = slim.layers.conv2d(inputs, 5, 3, padding="SAME", scope='conv1')
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# 获得图中所有可以训练的variable
trainable_tensors = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
[<tf.Variable 'conv1/weights:0' shape=(3, 3, 1, 5) dtype=float32_ref>,
 <tf.Variable 'conv1/biases:0' shape=(5,) dtype=float32_ref>]
# 给tensor加载权重 pure_weight 的 shape 需要和 该tensor 的 shape相同 
trainable_tensors[0].load(pure_weight, sess)

6.在Python代码中指定GPU

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

#设置定量的GPU使用量

config = tf.ConfigProto() 
config.gpu_options.per_process_gpu_memory_fraction = 0.9 # 占用GPU90%的显存 
session = tf.Session(config=config)

#设置最小的GPU使用量

config = tf.ConfigProto() 
config.gpu_options.allow_growth = True 
session = tf.Session(config=config)

7.部分层加载权重

#获得所有变量
vars = tf.global_variables()        
#筛选需要加载的变量
var_to_restore = [val for val in vars if 'conv' in val.name and 'rpn' not in val.name and 'Momentum' not in val.name] 
#获得部分变量的saver
vgg_saver = tf.train.Saver(var_to_restore)
#恢复权重
vgg_saver.restore(sess, pretrained_model)

8.冻结部分层

获得所有可训练变量

trainable_ops = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
def get_all_rpn_ops(self):
    output_vars = []
    for scope in self.rpn_scopes:
        output_vars += tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope)
    return output_vars

trainable_ops = self.get_all_rpn_ops()

将可训练变量列表传给优化器

starter_learning_rate = cfg.TRAIN.LEARNING_RATE
learning_rate = tf.train.exponential_decay(starter_learning_rate, self.global_step, cfg.TRAIN.LR_DECAY_STEPS,cfg.TRAIN.LR_DECAY_RATE, staircase=True)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

with tf.control_dependencies(update_ops):
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) \
                .minimize(loss=cost, global_step=self.global_step, var_list=trainable_ops)

9.tensorboard 一张图中绘制多个曲线

import tensorflow as tf
from numpy import random
 
writer_1 = tf.summary.FileWriter("./logs/plot_1")
writer_2 = tf.summary.FileWriter("./logs/plot_2")
 
log_var = tf.Variable(0.0)
tf.summary.scalar("loss", log_var)
 
write_op = tf.summary.merge_all()
 
session = tf.InteractiveSession()
session.run(tf.global_variables_initializer())
 
for i in range(100):
    # for writer 1
    summary = session.run(write_op, {log_var: random.rand()})
    writer_1.add_summary(summary, i)
    writer_1.flush()
 
    # for writer 2
    summary = session.run(write_op, {log_var: random.rand()})
    writer_2.add_summary(summary, i)
    writer_2.flush()
tensorboard --logdir=./logs

曲线的名字为文件夹的名字
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值