脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 将tensorflow的ckpt模型存储为npy的实例

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

2021-03-14 00:38机器学习的小学生 Python

今天小编就为大家分享一篇将tensorflow的ckpt模型存储为npy的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

实例如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/raby_gyl/article/details/79075716

延伸 · 阅读

精彩推荐