直接稀疏
对一些model可以直接做稀疏压缩也不掉点
相应的代码为
''' author :dezan zhao contact:1183489276 date :2018-02-23 func :prune caffemodel file directly ''' #-*-coding:utf-8-*- import pdb import caffe import re import numpy as np if __name__ == '__main__': #1.读取:读取caffemodel MODEL_FILE ='/data1/zhaodz/caffe-master/examples/mnist_cmp/lenet_train_test_compress_stage1_deploy.prototxt' PRETRAIN_FILE = '/data1/zhaodz/caffe-master/examples/mnist_cmp/models/lenet_finetune_stage1_iter_500.caffemodel' #PRETRAIN_FILE = '/data1/zhaodz/caffe-master/examples/mnist_cmp/models/lenet_iter_10000.caffemodel' net = caffe.Net(MODEL_FILE, PRETRAIN_FILE, caffe.TEST) sparse_ratio_vec = [0.33, 0.8, 0.9, 0.8] num = 0 for index in range(len(net.layers)): #if re.match('conv.',param_name) or re.match('fc.',param_name): if re.match('.*?Convolution',net.layers[index].type) or re.match('.*?InnerProduct',net.layers[index].type): param_name = net._layer_names[index] #层的名字 print param_name weight = net.params[param_name][0].data shape = weight.shape #2.处理:使权重按照近似公式进行运算 #weight.shape = (-1, 1) weight = weight.flatten() count = len(weight) sort_weight = np.zeros(count) #pdb.set_trace() for i in range(count): sort_weight[i] = abs(weight[i]) sort_weight.sort() ratio = sparse_ratio_vec[num]; index = int(count*ratio) mask_data = np.zeros(count) muweight = np.zeros(count) thr = sort_weight[index - 1] #pdb.set_trace() for i in range(count): if weight[i] > thr or weight[i] < -thr: mask_data[i] = 1 else: mask_data[i] = 0 muweight[i] = weight[i] * mask_data[i] #pdb.set_trace() muweight = muweight.reshape(shape) net.params[param_name][0].data[...] = muweight num = num + 1 net.save('/data1/zhaodz/caffe-master/examples/mnist_cmp/mnist_cmp.caffemodel')
depwise的直接稀疏为
#-*-coding:utf-8-*- import pdb import caffe import re import numpy as np if __name__ == '__main__': #1.读取:读取caffemodel MODEL_FILE ='deploy.prototxt' PRETRAIN_FILE = './models1/mobilenet_iter_1000000.caffemodel' #PRETRAIN_FILE = '/data1/zhaodz/caffe-master/examples/mnist_cmp/models/lenet_iter_10000.caffemodel' net = caffe.Net(MODEL_FILE, PRETRAIN_FILE, caffe.TEST) sparse_ratio_vec = [0.33, 0.8, 0.9, 0.8] num = 0 for index in range(len(net.layers)): print net.layers[index].type #if re.match('conv.',param_name) or re.match('fc.',param_name): if net.layers[index].type == 'Convolution' or net.layers[index].type=='InnerProduct': param_name = net._layer_names[index] #层的名字 print param_name weight = net.params[param_name][0].data shape = weight.shape #pdb.set_trace() #2.处理:使权重按照近似公式进行运算 #weight.shape = (-1, 1) weight = weight.flatten() count = len(weight) sort_weight = np.zeros(count) #pdb.set_trace() for i in range(count): sort_weight[i] = abs(weight[i]) sort_weight.sort() #ratio = sparse_ratio_vec[num]; ratio = 0.5 index_tmp = int(count*ratio) mask_data = np.zeros(count) muweight = np.zeros(count) thr = sort_weight[index_tmp - 1] #pdb.set_trace() for i in range(count): if weight[i] > thr or weight[i] < -thr: mask_data[i] = 1 else: mask_data[i] = 0 muweight[i] = weight[i] * mask_data[i] #pdb.set_trace() muweight = muweight.reshape(shape) net.params[param_name][0].data[...] = muweight num = num + 1 elif net.layers[index].type == 'ConvolutionDepthwise' or net.layers[index].type == 'InnerProductDepthwise': param_name = net._layer_names[index] #层的名字 print param_name weight = net.params[param_name][0].data for i in range(len(weight)): shape = weight[i][0].shape weight_k = weight[i][0].flatten() count = len(weight_k) #pdb.set_trace() sort_weight = np.zeros(count) for j in range(count): sort_weight[j] = abs(weight_k[j]) sort_weight.sort() ratio = 0.3 index_tmp = int(count*ratio) mask_data = np.zeros(count) muweight = np.zeros(count) thr = sort_weight[index_tmp - 1] #pdb.set_trace() for j in range(count): if weight_k[j] > thr or weight_k[j] < -thr: mask_data[j] = 1 else: mask_data[j] = 0 muweight[j] = weight_k[j] * mask_data[j] muweight = muweight.reshape(shape) net.params[param_name][0].data[i][0] = muweight net.save('cmp.caffemodel')