从NN模型提取的权重大小变得比模型大

时间:2018-06-25 09:58:48

标签: machine-learning neural-network huffman-code

我尝试从.pb张量流模型中提取权重并将其存储在文本文件中。文本文件本身的大小比模型大。为什么会这样? 预先感谢

提取权重的代码:

import tensorflow as tf
from tensorflow.python.platform import gfile
from tensorflow.python.framework import tensor_util
import operator
from functools import reduce
import matplotlib.pyplot as plt
import zlib
import pickle

PB_PATH = 'quantized_graph_resnet.pb'
with tf.Session() as sess:
    with gfile.FastGFile(PB_PATH,'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        sess.graph.as_default()
        tf.import_graph_def(graph_def, name='')
        graph_nodes = [n for n in graph_def.node]
        wts = [n for n in graph_nodes if n.op=='Const']


def check(l):
    for item in l:
        if(type(item) is list):
            return True
    return False


weightsFreq = {}
f = open('weights.txt', 'w')
for n in wts:
    print("Name of the node - %s" % n.name)
    if(True):
            l = (tensor_util.MakeNdarray(n.attr['value'].tensor)).tolist()
            if(isinstance(l, int)):
                f.write('%d' % l)
                f.write(' ')
                if l in weightsFreq:
                    weightsFreq[l]+=1
                else:
                    weightsFreq[l]=1
                continue
            if(isinstance(l, float)):
                continue
            while(check(l)):
                l = reduce(operator.concat, l)
            for item in l :
                f.write('%d' % item)
                f.write(' ')
                # print(item)
                if item in weightsFreq:
                    weightsFreq[item]+=1
                else:
                    weightsFreq[item]=1
    # print("Value - ", tensor_util.MakeNdarray(n.attr['value'].tensor), type(tensor_util.MakeNdarray(n.attr['value'].tensor)), "\n")

1 个答案:

答案 0 :(得分:0)

文本文件是存储大量十进制数字的一种非常低效的方式,它为每个数字的每个数字使用一个字节,而二进制文件将使用固定大小的表示形式(每个数字4个字节,并且单精度浮点数点号)。

这就是为什么文本文件比二进制文件大得多的原因。