将tensorflow的ckpt模型存储为npy的实例
2019/7/15 0:32:29
本文主要是介绍将tensorflow的ckpt模型存储为npy的实例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
实例如下所示:
#coding=gbk import numpy as np import tensorflow as tf from tensorflow.python import pywrap_tensorflow checkpoint_path='model.ckpt-5000'#your ckpt path reader=pywrap_tensorflow.NewCheckpointReader(checkpoint_path) var_to_shape_map=reader.get_variable_to_shape_map() alexnet={} alexnet_layer = ['conv1','conv2','conv3','conv4','conv5','fc6','fc7','fc8'] add_info = ['weights','biases'] alexnet={'conv1':[[],[]],'conv2':[[],[]],'conv3':[[],[]],'conv4':[[],[]],'conv5':[[],[]],'fc6':[[],[]],'fc7':[[],[]],'fc8':[[],[]]} for key in var_to_shape_map: #print ("tensor_name",key) str_name = key # 因为模型使用Adam算法优化的,在生成的ckpt中,有Adam后缀的tensor if str_name.find('Adam') > -1: continue print('tensor_name:' , str_name) if str_name.find('/') > -1: names = str_name.split('/') # first layer name and weight, bias layer_name = names[0] layer_add_info = names[1] else: layer_name = str_name layer_add_info = None if layer_add_info == 'weights': alexnet[layer_name][0]=reader.get_tensor(key) elif layer_add_info == 'biases': alexnet[layer_name][1] = reader.get_tensor(key) else: alexnet[layer_name] = reader.get_tensor(key) # save npy np.save('alexnet_pointing04.npy',alexnet) print('save npy over...') #print(alexnet['conv1'][0].shape) #print(alexnet['conv1'][1].shape)
以上这篇将tensorflow的ckpt模型存储为npy的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持找一找教程网。
这篇关于将tensorflow的ckpt模型存储为npy的实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程