Deep compression code

https://github.com/songhan/SqueezeNet-Deep-Compression

import sys
import os
import numpy as np
import pickle

help_ = ‘‘‘
Usage:
    decode.py <net.prototxt> <net.binary> <target.caffemodel>
    Set variable CAFFE_ROOT as root of caffe before run this demo!
‘‘‘

if len(sys.argv) != 4:
    print help_
    sys.exit()
else:
    prototxt = sys.argv[1]
    net_bin = sys.argv[2]
    target = sys.argv[3]

# os.system("cd $CAFFE_ROOT")
caffe_root = os.environ["CAFFE_ROOT"]
os.chdir(caffe_root)
print caffe_root
sys.path.insert(0, caffe_root + ‘python‘)
import caffe

caffe.set_mode_cpu()
net = caffe.Net(prototxt, caffe.TEST)
layers = filter(lambda x:‘conv‘ in x or ‘fc‘ in x or ‘ip‘ in x, net.params.keys())

fin = open(net_bin, ‘rb‘)

def binary_to_net(weights, spm_stream, ind_stream, codebook, num_nz):
    bits = np.log2(codebook.size)
    if bits == 4:
        slots = 2
    elif bits == 8:
        slots = 1
    else:
        print "Not impemented,", bits
        sys.exit()
    code = np.zeros(weights.size, np.uint8) 

    # Recover from binary stream
    spm = np.zeros(num_nz, np.uint8)
    ind = np.zeros(num_nz, np.uint8)
    if slots == 2:
        spm[np.arange(0, num_nz, 2)] = spm_stream % (2**4)
        spm[np.arange(1, num_nz, 2)] = spm_stream / (2**4)
    else:
        spm = spm_stream
    ind[np.arange(0, num_nz, 2)] = ind_stream% (2**4)
    ind[np.arange(1, num_nz, 2)] = ind_stream/ (2**4)

    # Recover the matrix
    ind = np.cumsum(ind+1)-1
    code[ind] = spm
    data = np.reshape(codebook[code], weights.shape)
    np.copyto(weights, data)

nz_num = np.fromfile(fin, dtype = np.uint32, count = len(layers))
for idx, layer in enumerate(layers):
    print "Reconstruct layer", layer
    print "Total Non-zero number:", nz_num[idx]    #eg . Reconstruct layer conv1    #Total Non-zero number: 13902
    if ‘conv‘ in layer:
        bits = 8  #卷积层使用8bit量化,全连接使用4bit
    else:
        bits = 4
    codebook_size = 2 ** bits #所有码字的总数
    codebook = np.fromfile(fin, dtype = np.float32, count = codebook_size)
    bias = np.fromfile(fin, dtype = np.float32, count = net.params[layer][1].data.size)
    np.copyto(net.params[layer][1].data, bias)   #把fin里的值拷贝进去,原先net.params[layer][1].data全部都是0

    spm_stream = np.fromfile(fin, dtype = np.uint8, count = (nz_num[idx]-1) / (8/bits) + 1)
    ind_stream = np.fromfile(fin, dtype = np.uint8, count = (nz_num[idx]-1) / 2+1)

    binary_to_net(net.params[layer][0].data, spm_stream, ind_stream, codebook, nz_num[idx])

net.save(target)
时间: 2024-10-17 20:54:15

Deep compression code的相关文章

[综述]Deep Compression/Acceleration深度压缩/加速/量化

Survey Recent Advances in Efficient Computation of Deep Convolutional Neural Networks, [arxiv '18] A Survey of Model Compression and Acceleration for Deep Neural Networks [arXiv '17] Quantization The ZipML Framework for Training Models with End-to-En

A Full Hardware Guide to Deep Learning

A Full Hardware Guide to Deep Learning Deep Learning is very computationally intensive, so you will need a fast CPU with many cores, right? Or is it maybe wasteful to buy a fast CPU? One of the worst things you can do when building a deep learning sy

[Fri 19 Jun 2015 ~ Thu 25 Jun 2015] Deep Learning in arxiv

A Neural Network Approach to Context-Sensitive Generation of Conversational Responses Leverage Financial News to Predict Stock Price Movements Using Word Embeddings and Deep Neural Networks MatchNet: Unifying Feature and Metric Learning for Patch-Bas

cs231n spring 2017 lecture15 Efficient Methods and Hardware for Deep Learning 听课笔记

1. 深度学习面临的问题: 1)模型越来越大,很难在移动端部署,也很难网络更新. 2)训练时间越来越长,限制了研究人员的产量. 3)耗能太多,硬件成本昂贵. 解决的方法:联合设计算法和硬件. 计算硬件可以分为通用和专用两大类.通用硬件又可以分为CPU和GPU.专用硬件可以分为(FPGA和ASIC,ASIC更高效,谷歌的TPU就是ASIC). 2. Algorithms for Efficient Inference 1)Pruning,修剪掉不那么重要的神经元和连接.第一步,用原始的网络训练:第

Awesome Machine Learning

Awesome Machine Learning  A curated list of awesome machine learning frameworks, libraries and software (by language). Inspired by awesome-php. If you want to contribute to this list (please do), send me a pull request or contact me @josephmisiti Als

從文本到視覺:各領域最前沿的論文集合

選自GitHub 作者:Simon Brugman 參與:吳攀 深度學習已經在語音識別.機器翻譯.圖像目標檢測和聊天機器人等許多領域百花齊放.近日,GitHub 用戶 Simon Brugman 發布了一個按任務分類的深度學習論文項目,其按照不同的任務類型列出了一些當前最佳的論文和對起步有用的論文. 目錄 1. 文本 1.1. 代碼生成(Code Generation) 1.2. 情感分析(Sentiment Analysis) 1.3. 翻譯(Translation) 1.4. 分類(Clas

机器学习框架、库

C General-Purpose Machine Learning Computer Vision C++ Computer Vision General-Purpose Machine Learning Natural Language Processing Sequence Analysis Common Lisp General-Purpose Machine Learning Clojure Natural Language Processing General-Purpose Mac

paper 15 :整理的CV代码合集

这篇blog,原来是西弗吉利亚大学的Li xin整理的,CV代码相当的全,不知道要经过多长时间的积累才会有这么丰富的资源,在此谢谢LI Xin .我现在分享给大家,希望可以共同进步!还有,我需要说一下,不管你的理论有多么漂亮,不管你有多聪明,如果没有实验来证明,那么都是错误的.  OK~本博文未经允许,禁止转载哦!  By  wei shen Reproducible Research in Computational Science “It doesn't matter how beautif

深度学习 vs. 概率图模型 vs. 逻辑学

深度学习 vs. 概率图模型 vs. 逻辑学 摘要:本文回顾过去50年人工智能(AI)领域形成的三大范式:逻辑学.概率方法和深度学习.文章按时间顺序展开,先回顾逻辑学和概率图方法,然后就人工智能和机器学习的未来走向做些预测. [编者按]在上个月发表博客文章<深度学习 vs. 机器学习 vs. 模式识别>之后,CMU博士.MIT博士后及vision.ai联合创始人Tomasz Malisiewicz这一次带领我们回顾50年来人工智能领域三大范式(逻辑学.概率方法和深度学习)的演变历程.通过本文我