Deep compression code

时间:2022-08-31 23:45:14

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. &lbrack;综述&rsqb;Deep Compression&sol;Acceleration深度压缩&sol;加速&sol;量化

    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&comma; Trained QuantizationAnd Huffman Coding

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

  4. A Full Hardware Guide to Deep Learning

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

  5. 网络压缩论文集&lpar;network compression&rpar;

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

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

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

  7. 深度学习网络压缩模型方法总结&lpar;model compression&rpar;

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

  8. &lpar;zhuan&rpar; Where can I start with Deep Learning&quest;

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

  9. 网络压缩论文整理&lpar;network compression&rpar;

    1. Parameter pruning and sharing 1.1 Quantization and Binarization Compressing deep convolutional ne ...

随机推荐

  1. 安装使用ubuntu问题汇总

    很早以前就安装了ubuntu系统,可是一直没怎么用,也没有深入研究.这两天重装了一下windows,顺带着也重新装了一遍最新的ubuntu14.04.期间碰到了不少问题,一个个解决也花费了不少时间.所 ...

  2. &lbrack;java基础&rsqb;分支结构(2)

    [java基础]分支结构2 switch case /** 文件路径:G:\JavaByHands\if-else\ 文件名称:switchcase.java 编写时间:2016/6/6 作 者:郑晨 ...

  3. python pickle 和 shelve模块

    pickle和shelve模块都可以把python对象存储到文件中,下面来看看它们的用法吧 1.pickle 写: 以写方式打开一个文件描述符,调用pickle.dump把对象写进去 dn = {'b ...

  4. Armitage初始化

    Kali2.0 Armitage初始化步骤如下 (1)点击页面的Armitage按钮 (2)提示Metasploit RPC server is not running,是否启动该服务,选择是 (3) ...

  5. Convert Sorted List to Binary Search Tree java

    public TreeNode sortedListToBST(ListNode head) { if(head==null) return new TreeNode(0); ArrayList&lt ...

  6. Py:数据挖掘之对个人微信朋友圈好友的性别、区域、昵称、签名信息进行情感分析——Jason niu

    #Py:数据挖掘之对微信朋友圈好友的性别.区域.昵称.签名信息进行情感分析——Jason niu import os import re import csv import time import j ...

  7. flex布局下overflow失效问题

    经常我们会使用flex布局,但是flex布局常常会遇到一些不可思议的麻烦,下面介绍一下overflow遇到的麻烦 我在工作中使用了左右两栏布局 .container { display: flex; ...

  8. PHP 开发者的 Docker 之旅

    用 PHP 作为我们「Docker 开发大礼包」开篇是带着一些朝圣的心情的.这是一门堪称「古老」的语言,这也是一门争议最多的语言,这更是一门不断涅槃的语言.「PHP 是最好的语言」这个流传已久的梗,或 ...

  9. 数据挖掘算法——Close算法

    说明奥:菜鸟的自我学习,可能有错. Close算法原理: 一个频繁闭合项目集的所有闭合子集一定是频繁的,一个非频繁闭合项目集的所有闭合超集一定是非频繁的. close算法是对Apriori算法的改进 ...

  10. 【模板】AC自动机(简单版)

    我:“woc...AC自动机?” 我:“可以自动AC???” 然鹅... 大佬:“傻...” 我:“(⊙_⊙)?” 大佬:“缺...” 我:“......” (大佬...卒 | 逃...) emm.. ...