您好,登錄后才能下訂單哦!
實例如下所示:
#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的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。