统计pytorch、caffe稀疏代码

pytorch

net = net_now()
state_dict = torch.load(model_path, map_location=lambda storage, loc: storage)
net.load_state_dict(state_dict, strict=True)
#net.cuda()
net.eval()
print("==========load success===!!")



T = 1e-8
total_weight = 0
total_weight_avail = 0
for name,parameters in net.named_parameters():
    if "bias" in name:
        continue
    print(name,':',parameters.size())
    weight = parameters.detach().numpy()
    weights_np = abs(weight)

    total_weight += weights_np.size
    tmp = weights_np > T
        # tmp = weights_np != 0
    total_weight_avail += tmp.sum()

    ratio_zero = (1 - (tmp.sum() * 1.0 / weights_np.size))
    print("name=", name, "    ratio_zero=", ratio_zero)

print("ratio_conv_avail_weight=", total_weight_avail * 1.0 / total_weight, "     ratio_conv_not_avail_weight=",
        1 - total_weight_avail * 1.0 / total_weight)

exit(0)

caffe

net.forward(**input_dict)

    ##################################
    #####bn
    print("==========>>bn"*5)
    T = 1e-5
    for layer_para_name, para in net.params.items():
        if "bn" in layer_para_name:
            weights_np = abs(para[3].data)  # para[3]是λ
            tmp = weights_np > T
            ratio_zero = (1 - (tmp.sum() * 1.0 / weights_np.size))
            print("layer_para_name=", layer_para_name, "    ratio_zero=", ratio_zero)

    ####conv
    print("==========>>conv" * 5, "            --->T=",T)
    total_weight = 0
    total_weight_avail = 0
    for layer_para_name, para in net.params.items():
        if "bn" in layer_para_name or "scale" in layer_para_name or "Scale" in layer_para_name or "bias" in layer_para_name:
            continue

        # if "conv1_1" == layer_para_name:
        #     # Statistics_weight(layer_para_name, abs(para[0].data))
        #
        #     Statistics_weight("/media/algo/data_1/project/oof/oof_sparse/caffe-jacinto/0000/deply/show_L2", layer_para_name, abs(para[0].data))
        #     a = 0



        # Statistics_weight("/media/algo/data_1/project/oof/oof_sparse/caffe-jacinto/0000/deply/show/0930/0930_L1+sprse", "L1+sparse", layer_para_name, abs(para[0].data))


        weights_np = abs(para[0].data)  # para[0]weight   para[1]bias   2  128  3  3
        weights_np_0 = weights_np[0]

        tmp_2 = weights_np <= 0.2
        ratio_123 = tmp_2.sum() * 1.0 / weights_np.size

        total_weight += weights_np.size
        tmp = weights_np > T
        # tmp = weights_np != 0
        total_weight_avail += tmp.sum()

        ratio_zero = (1 - (tmp.sum() * 1.0 / weights_np.size))
        print("layer_para_name=", layer_para_name, "    ratio_zero=", ratio_zero)

    print("ratio_conv_avail_weight=", total_weight_avail * 1.0 / total_weight, "     ratio_conv_not_avail_weight=",
          1 - total_weight_avail * 1.0 / total_weight)

    ##################################

你可能感兴趣的:(caffe,pytorch,深度学习,python,人工智能)