ckpt2npy和npy2ckpt转换
1、ckpt2npy转换
import tensorflow as tf
import numpy as np
import sys
from model import AlexNetModel# Edit just these
FILE_PATH = '/Users/dgurkaynak/Projects/marvel-finetuning/training/alexnet_20171125_124517/checkpoint/model_epoch7.ckpt'
NUM_CLASSES = 26
OUTPUT_FILE = 'alexnet_20171125_124517_epoch7.npy'if __name__ == '__main__':x = tf.placeholder(tf.float32, [128, 227, 227, 3])model = AlexNetModel(num_classes=NUM_CLASSES)model.inference(x)saver = tf.train.Saver()layers = ['conv1', 'conv2', 'conv3', 'conv4', 'conv5', 'fc6', 'fc7', 'fc8']data = {'conv1': [],'conv2': [],'conv3': [],'conv4': [],'conv5': [],'fc6': [],'fc7': [],'fc8': []}with tf.Session() as sess:saver.restore(sess, FILE_PATH)for op_name in layers:with tf.variable_scope(op_name, reuse = True):biases_variable = tf.get_variable('biases')weights_variable = tf.get_variable('weights')data[op_name].append(sess.run(biases_variable))data[op_name].append(sess.run(weights_variable))np.save(OUTPUT_FILE, data)
2、npy2ckpt转换
"""Conversion of the .npy weights into the .ckpt ones.
This script converts the weights of the DeepLab-ResNet model
from the numpy format into the TensorFlow one.
"""from __future__ import print_functionimport argparse
import osimport tensorflow as tf
import numpy as npfrom deeplab_resnet import DeepLabResNetModelSAVE_DIR = './'def get_arguments():"""Parse all the arguments provided from the CLI.Returns:A list of parsed arguments."""parser = argparse.ArgumentParser(description="NPY to CKPT converter.")parser.add_argument("npy_path", type=str,help="Path to the .npy file, which contains the weights.")parser.add_argument("--save-dir", type=str, default=SAVE_DIR,help="Where to save the converted .ckpt file.")return parser.parse_args()def save(saver, sess, logdir):model_name = 'model.ckpt'checkpoint_path = os.path.join(logdir, model_name)if not os.path.exists(logdir):os.makedirs(logdir)saver.save(sess, checkpoint_path, write_meta_graph=False)print('The weights have been converted to {}.'.format(checkpoint_path))def main():"""Create the model and start the training."""args = get_arguments()# Default image.image_batch = tf.constant(0, tf.float32, shape=[1, 321, 321, 3]) # Create network.net = DeepLabResNetModel({'data': image_batch})var_list = tf.global_variables()# Set up tf session and initialize variables. config = tf.ConfigProto()config.gpu_options.allow_growth = Truewith tf.Session(config=config) as sess:init = tf.global_variables_initializer()sess.run(init)# Loading .npy weights.net.load(args.npy_path, sess)# Saver for converting the loaded weights into .ckpt.saver = tf.train.Saver(var_list=var_list, write_version=1)save(saver, sess, args.save_dir)if __name__ == '__main__':main()
源码来自:
https://github.com/SamXiaosheng/tensorflow-cnn-finetune/blob/master/alexnet/ckpt2npy.py
https://github.com/DrSleep/tensorflow-deeplab-resnet/blob/master/npy2ckpt.py
ckpt2npy和npy2ckpt转换相关推荐
- javabean实体类与实体类之间的快速转换
一.Dozer是什么? dozer是一个能把实体和实体之间进行转换的工具.只要建立好映射关系.就像是ORM的数据库和实体映射一样. 使用方法示例如下: // article(PO) -> art ...
- C++ 笔记(35)— std::to_string 转换整形数字为字符串
1. 函数原型 string to_string (int val); string to_string (long val); string to_string (long long val); s ...
- C++ OJ 中多行数据输入(大小写转换、通过移位运算实现2的n次方、多组输入,每行输入数量不一样)
1. 多组输入,输出每行最大值 while(cin>>a>>b) 主要解决的是两个为一组的多组数据输入,当一次只输入一个数据时就用 while(cin>>a) 输入 ...
- 数据结构(02)— 时间复杂度与空间复杂度转换
1. 时间复杂度转化为空间复杂度 常用的降低时间复杂度的方法有递归.二分法.排序算法.动态规划等,降低空间复杂度的核心思路就是,能用低复杂度的数据结构能解决问题,就千万不要用高复杂度的数据结构. ...
- NumPy — 创建全零、全1、空、arange 数组,array 对象类型,astype 转换数据类型,数组和标量以及数组之间的运算,NumPy 数组共享内存
NumPy 简介 一个用 python 实现的科学计算包.包括: 1.一个强大的 N 维数组对象 Array : 2.比较成熟的(广播)函数库: 3.用于整合 C/C++ 和 Fortran 代码的工 ...
- Python+OpenCV 图像处理系列(7)—— 图像色彩空间及转换
1. 色彩空间转换函数 cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 第一个参数为加载在内存的读片,第二个参数为转换的类型,其中包括: COLOR_BGR2GRAY = ...
- 【C#】类——里式转换
类是由面对对象程序设计中产生的,在面向结构的程序设计例如C语言中是没有类这个概念的!C语言中有传值调用和传址调用的两种方式!在c语言中,主方法调用方法,通过传递参数等完成一些操作,其中比较常用的的数据 ...
- shell 批量转换文件编码
相信大家在平时的跨平台编程中碰到过文件编码问题,比如在Windows代码字符编码方式是GB2312,然而转到Linux却只支持utf-8,虽然对代码部分没啥影响,但是很多中文注释部分,却一片乱码,很让 ...
- tensorflow2.0 基础一 常用数据类型及转换
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/weixin_43619065/arti ...
- Pytorch | BERT模型实现,提供转换脚本【横扫NLP】
<谷歌终于开源BERT代码:3 亿参数量,机器之心全面解读>,上周推送的这篇文章,全面解读基于TensorFlow实现的BERT代码.现在,PyTorch用户的福利来了:一个名为Huggi ...
最新文章
- oracle主备库sync模式,Oracle 探索DG备库undo工作模式
- 分布式:阿里云HSF转dubbo+zookeeper
- JavaScript是如何工作的:事件循环和异步编程的崛起+ 5种使用 async/await 更好地编码方式!...
- C#为什么要用到 try...catch... 呢?
- websocke 在线测试地址
- IntelliJ IDEA 安装使用 aiXcoder 智能编程助手
- 贝叶斯统计(Bayesian statistics) vs 频率统计(Frequentist statistics):marginal likelihood(边缘似然)
- UVM学习整理——附录(部分组件源码)
- C# 串口驱动封装成类库
- clickhouse 入门介绍和预演
- 数字图像处理与Python实现-图像降噪-指数型低通滤波
- 有趣的 Google command line shell
- 魅蓝s6手机sim卡不显示无服务器,科普OPPOA57怎么截图及魅蓝S6怎么插卡
- 鸿蒙系统一体机使用教程,华为视频会议系统TE30(华为新一体机)(示例代码)
- HTML、CSS实现手风琴效果
- xtu1395 字符频度
- Linux粘滞位(粘着位)
- 腾讯云tca认证是什么?含金量怎么样?证书有什么有事吗
- 利用这5个办法成为自由职业者,通过远程赚钱,开心旅行、轻松赚钱!
- ue4 飞机大战游戏开发日记
热门文章
- oracle 报错904,EXP-00008: 遇到 ORACLE 错误 904
- 【软件技巧】【截图】浏览器自带的全网页截图工具
- Chrome扩展程序应用商店方式一
- php用redis实现队列,PHP使用Redis实现队列
- “华为杯”山东理工大学第十一届 ACM程序设计竞赛 我不是股神
- Bulma CSS框架教程
- 基于易班开放平台接入研究与探索
- 元宇宙大杀器来了!小扎祭出4款VR头显,挑战视觉图灵测试
- 现代大学英语精读第二版(第一册)学习笔记(原文及全文翻译)——16A - Who Shall Dwell?(生的机会留给谁?)
- Novell NetWare 及其协议