参考:

TensorFlow 自定义模型导出:将 .ckpt 格式转化为 .pb 格式

TensorFlow 模型保存与恢复

snpe

tensorflow 模型前向传播 保存ckpt  tensorbard查看 ckpt转pb  pb 转snpe dlc 实例

log文件

输入节点 图像高度 图像宽度 图像通道数

input0 6,6,3

输出节点

--out_node add

snpe-tensorflow-to-dlc --graph ./simple_snpe_log/model200.pb -i input0 6,6,3 --out_node add

#coding:utf-8
#http://blog.csdn.net/zhuiqiuk/article/details/53376283
#http://blog.csdn.net/gan_player/article/details/77586489
from __future__ import absolute_import, unicode_literals
import tensorflow as tf
import shutil
import os.path
from tensorflow.python.framework import graph_util
import mxnet as mx
import numpy as np
import random
import cv2
from time import sleep
from easydict import EasyDict as edict
import logging
import math
import tensorflow as tf
import numpy as np def FullyConnected(input, fc_weight, fc_bias, name):
fc = tf.matmul(input, fc_weight) + fc_bias
return fc def inference(body, name_class,outchannel):
wkernel = 3
inchannel = body.get_shape()[3].value
conv_weight = np.arange(wkernel * wkernel * inchannel * outchannel,dtype=np.float32).reshape((outchannel,inchannel,wkernel,wkernel))
conv_weight = conv_weight / (outchannel*inchannel*wkernel*wkernel)
print("conv_weight ", conv_weight)
conv_weight = conv_weight.transpose(2,3,1,0)
conv_weight = tf.Variable(conv_weight, dtype=np.float32, name = "conv_weight")
body = tf.nn.conv2d(body, conv_weight, strides=[1, 1, 1, 1], padding='SAME', name = "conv0")
conv = body
conv_shape = body.get_shape()
dim = conv_shape[1].value * conv_shape[2].value * conv_shape[3].value
body = tf.reshape(body, [1, dim], name = "fc0")
fc_weight = np.ones((dim, name_class))
fc_bias = np.zeros((1, name_class))
fc_weight = tf.Variable(fc_weight, dtype=np.float32, name="fc_weight")
fc_bias = tf.Variable(fc_bias, dtype=np.float32, name="fc_bias")
# tf.constant(100,dtype=np.float32, shape=(body.get_shape()[1] * body.get_shape()[2] * body.get_shape()[3], name_class])
# fc_bias = tf.constant(10, dtype=np.float32, shape=(1, name_class])
body = FullyConnected(body, fc_weight, fc_bias, "fc0")
return conv, body export_dir = "simple_snpe_log"
def saveckpt():
height = 6
width = 6
inchannel = 3
outchannel = 3
graph = tf.get_default_graph()
with tf.Graph().as_default():
input_image = tf.placeholder("float", [1, height, width, inchannel], name = "input0")
conv, logdit = inference(input_image,10,outchannel)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
img = np.arange(height * width * inchannel, dtype=np.float32).reshape((1,inchannel,height,width)) \
/ (1 * inchannel * height * width) * 255.0 - 127.5
print("img",img)
img = img.transpose(0,2,3,1)
import time
since = time.time()
fc = sess.run(logdit,{input_image:img})
conv = sess.run(conv, {input_image: img})
time_elapsed = time.time() - since
print("tf inference time ", str(time_elapsed))
print("conv", conv.transpose(0, 2, 3, 1))
print("fc", fc)
#np.savetxt("tfconv.txt",fc)
#print( "fc", fc.transpose(0,3,2,1))
#np.savetxt("tfrelu.txt",fc.transpose(0,3,2,1)[0][0]) # #save ckpt
export_dir = "simple_snpe_log"
saver = tf.train.Saver()
step = 200
# if os.path.exists(export_dir):
# os.system("rm -rf " + export_dir)
if not os.path.isdir(export_dir): # Create the log directory if it doesn't exist
os.makedirs(export_dir) checkpoint_file = os.path.join(export_dir, 'model.ckpt')
saver.save(sess, checkpoint_file, global_step=step) def LoadModelToTensorBoard():
graph = tf.get_default_graph()
checkpoint_file = os.path.join(export_dir, 'model.ckpt-200.meta')
saver = tf.train.import_meta_graph(checkpoint_file)
print(saver)
summary_write = tf.summary.FileWriter(export_dir , graph)
print(summary_write) def ckptToPb():
checkpoint_file = os.path.join(export_dir, 'model.ckpt-200.meta')
ckpt = tf.train.get_checkpoint_state(export_dir)
print("model ", ckpt.model_checkpoint_path)
saver = tf.train.import_meta_graph(ckpt.model_checkpoint_path +'.meta')
graph = tf.get_default_graph()
with tf.Session() as sess:
saver.restore(sess,ckpt.model_checkpoint_path)
height = 6
width = 6
input_image = tf.get_default_graph().get_tensor_by_name("input0:0")
fc0_output = tf.get_default_graph().get_tensor_by_name("add:0")
sess.run(tf.global_variables_initializer())
output_graph_def = tf.graph_util.convert_variables_to_constants(
sess, graph.as_graph_def(), ['add'])
model_name = os.path.join(export_dir, 'model200.pb')
with tf.gfile.GFile(model_name, "wb") as f:
f.write(output_graph_def.SerializeToString()) def PbTest():
with tf.Graph().as_default():
output_graph_def = tf.GraphDef()
output_graph_path = os.path.join(export_dir,'model200.pb')
with open(output_graph_path, "rb") as f:
output_graph_def.ParseFromString(f.read())
tf.import_graph_def(output_graph_def, name="") with tf.Session() as sess:
tf.initialize_all_variables().run()
height = 6
width = 6
inchannel = 3
outchannel = 3
input_image = tf.get_default_graph().get_tensor_by_name("input0:0")
fc0_output = tf.get_default_graph().get_tensor_by_name("add:0")
conv = tf.get_default_graph().get_tensor_by_name("conv0:0") img = np.arange(height * width * inchannel, dtype=np.float32).reshape((1,inchannel,height,width)) \
/ (1 * inchannel * height * width) * 255.0 - 127.5
print("img",img)
img = img.transpose(0,2,3,1)
import time
since = time.time()
fc0_output = sess.run(fc0_output,{input_image:img})
conv = sess.run(conv, {input_image: img})
time_elapsed = time.time() - since
print("tf inference time ", str(time_elapsed))
print("conv", conv.transpose(0, 2, 3, 1))
print("fc0_output", fc0_output) if __name__ == '__main__': saveckpt() #1
LoadModelToTensorBoard()#2
ckptToPb()#3
PbTest()#

tensorflow 模型前向传播 保存ckpt tensorbard查看 ckpt转pb pb 转snpe dlc 实例的更多相关文章

  1. Tensorflow模型加载与保存、Tensorboard简单使用

    先上代码: from __future__ import absolute_import from __future__ import division from __future__ import ...

  2. TensorFlow模型加载与保存

    我们经常遇到训练时间很长,使用起来就是Weight和Bias.那么如何将训练和测试分开操作呢? TF给出了模型的加载与保存操作,看了网上都是很简单的使用了一下,这里给出一个神经网络的小程序去测试. 本 ...

  3. 利用tensorflow实现前向传播

    import tensorflow as tf w1 = tf.Variable(tf.random_normal((2, 3), stddev=1, seed=1))w2 = tf.Variable ...

  4. Tensorflow笔记——神经网络图像识别(一)前反向传播,神经网络八股

      第一讲:人工智能概述       第三讲:Tensorflow框架         前向传播: 反向传播: 总的代码: #coding:utf-8 #1.导入模块,生成模拟数据集 import t ...

  5. tensorflow模型的保存与恢复,以及ckpt到pb的转化

    转自 https://www.cnblogs.com/zerotoinfinity/p/10242849.html 一.模型的保存 使用tensorflow训练模型的过程中,需要适时对模型进行保存,以 ...

  6. tensorflow模型持久化保存和加载

    模型文件的保存 tensorflow将模型保持到本地会生成4个文件: meta文件:保存了网络的图结构,包含变量.op.集合等信息 ckpt文件: 二进制文件,保存了网络中所有权重.偏置等变量数值,分 ...

  7. Tensorflow模型变量保存

    Tensorflow:模型变量保存 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献Tensorflow实战Google深度学习框架 实验平台: Tensorflow1.4.0 pyt ...

  8. tensorflow模型持久化保存和加载--深度学习-神经网络

    模型文件的保存 tensorflow将模型保持到本地会生成4个文件: meta文件:保存了网络的图结构,包含变量.op.集合等信息 ckpt文件: 二进制文件,保存了网络中所有权重.偏置等变量数值,分 ...

  9. 超详细的Tensorflow模型的保存和加载(理论与实战详解)

    1.Tensorflow的模型到底是什么样的? Tensorflow模型主要包含网络的设计(图)和训练好的各参数的值等.所以,Tensorflow模型有两个主要的文件: a) Meta graph: ...

随机推荐

  1. 深入理解Linux内核-信号

    信号:1.最初被引入作为用户态进程间通信2.内核也使用信号通知进程系统所发生的事件3.信号很短,发送给进程的唯一信息通常是一个数.4.名称通常以SIG为前缀5.信号时可消费资源,每个信号只能被传递一次 ...

  2. 深入理解Linux内核-内核同步

    内核基本的同步机制: 抢占内核的主要特点:一个在内核态运行的进程,可能在执行内核函数期间被另外一个进程取代. 内核抢占:Linux 2.6允许用户在编译内核的时候配置十分启用 进程临界区:每个进程中访 ...

  3. 了解HTTP协议栈(实践篇)

    关于http协议的理论知识,我在这里就不详细说明了,具体下面给出的链接有.接下来都是用具体的操作显示的,各位可以结合起来看. 一.使用nc打开端口,并使用浏览器进行访问 (对应文章中的HTTP协议详解 ...

  4. lua -- debug

    framework.debug 调试支持 ~~ echo 功能同 print. 格式: echo(值, [值, 值, ...]) ~~ printf 按照特定格式输出. 格式: printf(格式字符 ...

  5. c++并行计算库TBB和PPL的基本用法

    并行库充分利用多核的优势,通过并行运算提高程序效率,本文主要介绍c++中两个知名的并行库,一个是intel开发的TBB,一个是微软开发的PPL.本文只介绍其基本的常用用法:并行算法和任务. TBB(I ...

  6. Tips for Navigating Large Game Code Bases

    http://solid-angle.blogspot.com/2015/08/tips-for-navigating-large-game-code.html

  7. Tensorflow网址

    https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/1-1-why/ 莫烦视频学习 http://wiki.jikex ...

  8. Inner Functions - What Are They Good For?

    Referece: https://realpython.com/blog/python/inner-functions-what-are-they-good-for/ Let’s look at t ...

  9. jQuery之自定义datagrid控件

    sldatagrid 效果: sldatagrid.js (function($) { function loadColumns(sldatagrid, columns) { $(sldatagrid ...

  10. mysql性能优化(二)

    key_buffer_size 为了最小化磁盘的 I/O , MyISAM 存储引擎的表使用键高速缓存来缓存索引,这个键高速缓存的大小则通过 key-buffer-size 参数来设置.如果应用系统中 ...