将tensorflow的ckpt模型存储为npy的实例

yipeiwu_com5年前Python基础

实例如下所示:

#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的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现的简单hangman游戏实例

本文实例讲述了Python实现的简单hangman游戏。分享给大家供大家参考。具体如下: #!/usr/bin/env python import random import cP...

tensorflow TFRecords文件的生成和读取的方法

TensorFlow提供了TFRecords的格式来统一存储数据,理论上,TFRecords可以存储任何形式的数据。 TFRecords文件中的数据都是通过tf.train.Examp...

Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程

Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程

使用Flask-SQLAlchemy管理数据库 Flask-SQLAlchemy是一个Flask扩展,它简化了在Flask应用程序中对SQLAlchemy的使用。SQLAlchemy是一...

Python有序查找算法之二分法实例分析

Python有序查找算法之二分法实例分析

本文实例讲述了Python有序查找算法之二分法。分享给大家供大家参考,具体如下: 二分法是一种快速查找的方法,时间复杂度低,逻辑简单易懂,总的来说就是不断的除以2除以2... 例如需要查...

Python selenium实现微博自动登录的示例代码

Python selenium实现微博自动登录的示例代码

(一)编程环境 操作系统:Win 10 编程语言:Python 3.6 (二)安装selenium 这里使用selenium实现。 如果没有安装过python的seleni...