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)

Deep compression code的更多相关文章

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

    Survey Recent Advances in Efficient Computation of Deep Convolutional Neural Networks, [arxiv '18] A ...

  2. DEEP COMPRESSION小记

    2016ICLR最佳论文 Deep Compression: Compression Deep Neural Networks With Pruning, Trained Quantization A ...

  3. Deep Compression Compressing Deep Neural Networks With Pruning, Trained QuantizationAnd Huffman Coding

    转载请注明出处: http://www.cnblogs.com/sysuzyq/p/6200613.html by 少侠阿朱

  4. 论文翻译:2021_Towards model compression for deep learning based speech enhancement

    论文地址:面向基于深度学习的语音增强模型压缩 论文代码:没开源,鼓励大家去向作者要呀,作者是中国人,在语音增强领域 深耕多年 引用格式:Tan K, Wang D L. Towards model c ...

  5. A Full Hardware Guide to Deep Learning

    A Full Hardware Guide to Deep Learning Deep Learning is very computationally intensive, so you will ...

  6. 网络压缩论文集(network compression)

    Convolutional Neural Networks ImageNet Models Architecture Design Activation Functions Visualization ...

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

    1. 深度学习面临的问题: 1)模型越来越大,很难在移动端部署,也很难网络更新. 2)训练时间越来越长,限制了研究人员的产量. 3)耗能太多,硬件成本昂贵. 解决的方法:联合设计算法和硬件. 计算硬件 ...

  8. 深度学习网络压缩模型方法总结(model compression)

    两派 1. 新的卷机计算方法 这种是直接提出新的卷机计算方式,从而减少参数,达到压缩模型的效果,例如SqueezedNet,mobileNet SqueezeNet: AlexNet-level ac ...

  9. (zhuan) Where can I start with Deep Learning?

    Where can I start with Deep Learning? By Rotek Song, Deep Reinforcement Learning/Robotics/Computer V ...

随机推荐

  1. 使用Device IO Control 讀寫 USB Mass Storage

    http://www.ezblog.idv.tw/Download/USBStorage.rar 這是一個不透過檔案系統,去讀寫USB Mass Storage 任何位置(包含FAT)的方式 首先需安 ...

  2. PyQt5 各种菜单实现

    # -*- coding: utf-8 -*- # Created by PCITZDF on 2018/4/8 15:36. # FileName: menuandtools.py import s ...

  3. [asp.net web api] HttpStatusCode的使用

    摘要 在开放api的时,我们需要返回不同的状态给调用方,以告诉它们当前请求的结果,是成功了还是失败了.当然这种给调用方的反馈有很多种做法,这里就说是web api内置的对Http状态码.http状态码 ...

  4. C#程序集系列11,全局程序集缓存

    全局程序集缓存(GAC:Global Assembly Cache)用来存放可能被多次使用的强名称程序集.当主程序需要加载程序集的时候,优先选择到全局程序集缓存中去找寻需要的程序集. 为什么需要全局程 ...

  5. Oracle 快速插入1000万条数据的实现方式

    1.使用dual配合connect by level create table BigTable as select rownum as id from dual connect by level & ...

  6. Guava 源码分析之 Beta, GwtCompatible, GwtIncompatible, Charset, HashCode

    com.google.common.annotations.Beta /** * 表明一个公用API的未来版本是受不兼容变更或删除限制的 * 拥有这个注释标志的API不受任何兼容性保证 * */ @R ...

  7. 【BZOJ】【1941】【SDOI2010】Hide and Seek

    KD-Tree 一开始看错题了

  8. 【XJOI】【NOI考前模拟赛7】

    DP+卡常数+高精度/  计算几何+二分+判区间交/  凸包 首先感谢徐老师的慷慨,让蒟蒻有幸膜拜了学军的神题.祝NOI2015圆满成功 同时膜拜碾压了蒟蒻的众神QAQ 填填填 我的DP比较逗比……( ...

  9. 【BZOJ】【1251】序列终结者

    Splay 还是splay序列维护,这题我WA了的原因是:在Push_up的时候,当前子树的max我是直接取的L.R和v[x]的最大值,但是如果没有左/右儿子,默认是会访问0号结点的mx值,而这个值没 ...

  10. JBoss 系列一 O O:Maven jBPM 6 集成演示样例

    概述 jBPM 6 中底层架构基于 Maven,所以我们能够非常easy的进行 Maven jBPM 6 集成演示样例,本文分三个部分: 基本原理介绍 Maven jBPM 6 集成 jBPM 6 中 ...