TensorFlow 调用预训练好的模型—— Python 实现
1. 准备预训练好的模型
- TensorFlow 预训练好的模型被保存为以下四个文件
- data 文件是训练好的参数值,meta 文件是定义的神经网络图,checkpoint 文件是所有模型的保存路径,如下所示,为简单起见只保留了一个模型。
model_checkpoint_path: "/home/senius/python/c_python/test/model-40"
all_model_checkpoint_paths: "/home/senius/python/c_python/test/model-40"
2. 导入模型图、参数值和相关变量
import tensorflow as tf
import numpy as np
sess = tf.Session()
X = None # input
yhat = None # output
def load_model():
"""
Loading the pre-trained model and parameters.
"""
global X, yhat
modelpath = r'/home/senius/python/c_python/test/'
saver = tf.train.import_meta_graph(modelpath + 'model-40.meta')
saver.restore(sess, tf.train.latest_checkpoint(modelpath))
graph = tf.get_default_graph()
X = graph.get_tensor_by_name("X:0")
yhat = graph.get_tensor_by_name("tanh:0")
print('Successfully load the pre-trained model!')
- 通过 saver.restore 我们可以得到预训练的所有参数值,然后再通过 graph.get_tensor_by_name 得到模型的输入张量和我们想要的输出张量。
3. 运行前向传播过程得到预测值
def predict(txtdata):
"""
Convert data to Numpy array which has a shape of (-1, 41, 41, 41 3).
Test a single example.
Arg:
txtdata: Array in C.
Returns:
Three coordinates of a face normal.
"""
global X, yhat
data = np.array(txtdata)
data = data.reshape(-1, 41, 41, 41, 3)
output = sess.run(yhat, feed_dict={X: data}) # (-1, 3)
output = output.reshape(-1, 1)
ret = output.tolist()
return ret
- 通过 feed_dict 喂入测试数据,然后 run 输出的张量我们就可以得到预测值。
4. 测试
load_model()
testdata = np.fromfile('/home/senius/python/c_python/test/04t30t00.npy', dtype=np.float32)
testdata = testdata.reshape(-1, 41, 41, 41, 3) # (150, 41, 41, 41, 3)
testdata = testdata[0:2, ...] # the first two examples
txtdata = testdata.tolist()
output = predict(txtdata)
print(output)
# [[-0.13345889747142792], [0.5858198404312134], [-0.7211828231811523],
# [-0.03778800368309021], [0.9978875517845154], [0.06522832065820694]]
- 本例输入是一个三维网格模型处理后的 [41, 41, 41, 3] 的数据,输出一个表面法向量坐标 (x, y, z)。
获取更多精彩,请关注「seniusen」!
TensorFlow 调用预训练好的模型—— Python 实现的更多相关文章
- tensorflow 使用预训练好的模型的一部分参数
vars = tf.global_variables() net_var = [var for var in vars if 'bi-lstm_secondLayer' not in var.name ...
- 学习TensorFlow,调用预训练好的网络(Alex, VGG, ResNet etc)
视觉问题引入深度神经网络后,针对端对端的训练和预测网络,可以看是特征的表达和任务的决策问题(分类,回归等).当我们自己的训练数据量过小时,往往借助牛人已经预训练好的网络进行特征的提取,然后在后面加上自 ...
- 在 C/C++ 中使用 TensorFlow 预训练好的模型—— 间接调用 Python 实现
现在的深度学习框架一般都是基于 Python 来实现,构建.训练.保存和调用模型都可以很容易地在 Python 下完成.但有时候,我们在实际应用这些模型的时候可能需要在其他编程语言下进行,本文将通过 ...
- 在 C/C++ 中使用 TensorFlow 预训练好的模型—— 直接调用 C++ 接口实现
现在的深度学习框架一般都是基于 Python 来实现,构建.训练.保存和调用模型都可以很容易地在 Python 下完成.但有时候,我们在实际应用这些模型的时候可能需要在其他编程语言下进行,本文将通过直 ...
- TensorFlow 同时调用多个预训练好的模型
在某些任务中,我们需要针对不同的情况训练多个不同的神经网络模型,这时候,在测试阶段,我们就需要调用多个预训练好的模型分别来进行预测. 调用单个预训练好的模型请点击此处 弄明白了如何调用单个模型,其实调 ...
- 【猫狗数据集】使用预训练的resnet18模型
数据集下载地址: 链接:https://pan.baidu.com/s/1l1AnBgkAAEhh0vI5_loWKw提取码:2xq4 创建数据集:https://www.cnblogs.com/xi ...
- ubuntu16.04 使用tensorflow object detection训练自己的模型
一.构建自己的数据集 1.格式必须为jpg.jpeg或png. 2.在models/research/object_detection文件夹下创建images文件夹,在images文件夹下创建trai ...
- 深度学习tensorflow实战笔记 用预训练好的VGG-16模型提取图像特征
1.首先就要下载模型结构 首先要做的就是下载训练好的模型结构和预训练好的模型,结构地址是:点击打开链接 模型结构如下: 文件test_vgg16.py可以用于提取特征.其中vgg16.npy是需要单独 ...
- Tensorflow加载预训练模型和保存模型(ckpt文件)以及迁移学习finetuning
转载自:https://blog.csdn.net/huachao1001/article/details/78501928 使用tensorflow过程中,训练结束后我们需要用到模型文件.有时候,我 ...
随机推荐
- Mac通过域名查询IP地址
Mac通过域名查询IP地址 方法一:使用Mac自带的"网络实用工具" 步骤: 搜索"网络使用工具",并打开: 点击LookUp,输入互联网地址,点击Lookup ...
- 常用的linux指令整理
ls 列出文件目录 -a全部文件,连同隐藏文件一起列出 -d仅列出目录本身,而不是列出目录内的文件数据 -l 连同权限一同列出 cd 切换文件目录的命令 pwd显示目前所在的目录 mkdir 创建新的 ...
- SpringBoot非官方教程 | 第十六篇:用restTemplate消费服务
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot11-restTemplate/ 本文出自方志朋的 ...
- js中FormData+XMLHttpRequest数据传输
前言: 首先我们需要了解,前后端进行数据传输依赖于浏览器的XMLHttpRequest对象 一.什么是XMLHttpRequest对象? XMLHttpRequest 是DOM对象,提供了对于http ...
- 【2018 CCPC网络赛 1004】Find Integer(勾股数+费马大定理)
Problem Description people in USSS love math very much, and there is a famous math problem . give yo ...
- POJ 1180 Batch Scheduling (dp,双端队列)
#include <iostream> using namespace std; + ; int S, N; int T[MAX_N], F[MAX_N]; int sum_F[MAX_N ...
- Keepalived 配置高可用
VRRP协议及Keepalived原理使用 VRRP 协议即 Virtual Router Redundancy Protocol,虚拟路由器冗余协议, 为了解决局域网内默认网关单点失效的问题. ...
- djangorestframework怎么这么好用!
一年前就已经用过restframework, 当时觉得这个只是给web框架打辅助的, 他能实现的我也都实现(可能没有那么好用, 嘿嘿) 但是我有一种东西叫做效率, 时间就是金钱, 别人造好的就直接用就 ...
- php GD图片四角圆形处理
<?php /** * blog:http://www.zhaokeli.com * 处理四角圆图片 * @param string $imgpath 源图片路径 * @param intege ...
- 【Linux】Nginx无法加载.woff .eot .svg .ttf问题解决
只需要修改Nginx的vhosts.ini,加上以下代码即可修复该问题 location ~ \.(eot|otf|ttf|woff|woff2|svg)$ { add_header Access-C ...