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. android aapt 用法 -- ApkReader

    aapt 是android assert packaging tool的缩写,具体如下: 1. 列出apk包的内容 aapt l[ist] [-v] [-a] file.{zip,jar,apk} - ...

  2. jQuery中的观察者模式(Observer Pattern)

    在jQuery中,on方法可以为元素绑定事件,trigger方法可以手动触发事件,围绕这2个方法,我们来体验jQuery中的观察者模式(Observer Pattern). ■ on方法绑定内置事件, ...

  3. MVC文件上传04-使用客户端jQuery-File-Upload插件和服务端Backload组件实现多文件异步上传

    本篇使用客户端jQuery-File-Upload插件和服务端Badkload组件实现多文件异步上传.MVC文件上传相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小  ...

  4. MFC绘制直方图和饼图

    转载原文: Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNorma ...

  5. andriod 获得MP4时长

    //获得MP4时长 private int getTimeLong(String videoPath) { MediaMetadataRetriever retr = new MediaMetadat ...

  6. 【微信小程序】在微信开发工具上七牛云的图片可以看到,但是在真机上看不到的原因解决

    在开发微信小程序过程中,在微信开发者工具上,七牛云的图片都可以展示出来,但是在真机上,七牛云的图片却展示不出来,也没有报404找不到或者不能加载图片的问题, 必须保证: 1.图片是用image加载的: ...

  7. Deployment failure on Tomcat 7.x. Could not copy all resources to

    今天在网上部署项目的时候出现在了问题 tomcat一直部署不上 网上查了一下 原因记下来供大家查看 [plain] <span style="font-size:18px;" ...

  8. PHP命名空间学习笔记

    命名空间的支持版本:PHP 5 > 5.3.0,PHP 7 . 什么是命名空间 从广义上来说,命名空间是一种封装事物的方法.在很多地方都可以见到这种抽象概念.例如,在操作系统中目录用来将相关文件 ...

  9. Coursera课程《Python数据结构》中课件

    You can access the Google Drive containing all of the current and in-progress lecture slides for thi ...

  10. FPGA三段式状态机的思维陷阱

    用三段式描述状态机的好处,国内外各位大牛都已经说的很多了,大致可归为以下三点: 1.将组合逻辑和时序逻辑分开,利于综合器分析优化和程序维护; 2.更符合设计的思维习惯; 3.代码少,比一段式状态机更简 ...