資源簡介
將caffemodel中小于給定閾值的連接全部置0,如果用CSC等格式存儲,會降低存儲空間
代碼片段和文件信息
#?coding:utf-8
#?by?chen?yh
import?caffe
import?numpy?as?np
import?matplotlib.pyplot?as?plt
def?weight_0(prototxtmodellayerthreshold):
????caffe.set_mode_gpu()
????net=caffe.Net(prototxtmodelcaffe.TEST)
????weight?=?net.params[layer][0].data
????bias?=?net.params[layer][1].data
????sum_l1=[]
????for?i?in?range(weight.shape[0]):
????????for?j?in?range(weight.shape[1]):
????????????sum_l1.append((ijnp.sum(abs(weight[ij::]))))#i是核的順序j是每個卷積核與前面某個channel的連接順序求出每個連接的類似于L1范數的權重和加上ij是為了后續判斷weight的時候好直接處理到原weight
????display(sum_l1128)#從小到大排序后打印出前128個L1范數
????l1_plot(sum_l1)#畫出L1范數關于out*input的坐標圖以確定多少個需要修剪.
????weight_l1?=?[]
????for?i?in?sum_l1:
????????weight_l1.append(i[2])?#得到僅含有l1范數的列表
????for?iweight_sum?in?enumerate(weight_l1):
????????if?weight_sum?
評論
共有 條評論