quantize.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:Caffe-Python-Tutorial 作者: tostq 项目源码 文件源码
def kmeans_net(net, layers, num_c=16, initials=None):
    # net: ??
    # layers: ??????
    # num_c: ???????
    # initials: ??????
    codebook = {} # ????
    if type(num_c) == type(1):
        num_c = [num_c] * len(layers)
    else:
        assert len(num_c) == len(layers)

    # ?????????
    print "==============Perform K-means============="
    for idx, layer in enumerate(layers):
        print "Eval layer:", layer
        W = net.params[layer][0].data.flatten()
        W = W[np.where(W != 0)] # ????0???
        # ?????????????????
        if initials is None:  # Default: uniform sample
            min_W = np.min(W)
            max_W = np.max(W)
            initial_uni = np.linspace(min_W, max_W, num_c[idx] - 1)
            codebook[layer], _ = scv.kmeans(W, initial_uni)
        elif type(initials) == type(np.array([])):
            codebook[layer], _ = scv.kmeans(W, initials)
        elif initials == 'random':
            codebook[layer], _ = scv.kmeans(W, num_c[idx] - 1)
        else:
            raise Exception

        # ?0?????
        codebook[layer] = np.append(0.0, codebook[layer])
        print "codebook size:", len(codebook[layer])

    return codebook

# ???????
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号