使用X2paddle将tf转paddle模型后,根据敏感度剪枝的问题
Created by: down-to-earth-WYL
我现在有一个tf转成paddle的模型,如果我想对该paddle模型模型压缩,然后进行finetune,请问应该用什么载入模型? 我现在使用的代码如下: ` import numpy as np import paddle.fluid as fluid import paddleslim as slim import paddle import time import cv2 import os
data_path = r'E:\paddleslim\ILSVRC2012_img_val' model_path = r'E:\paddleslim\paddle_models\mobilenet_imagenet\inference_model' labels_path = 'labels.npy' labels = np.load(labels_path) images = os.listdir(data_path) num_sample = len(images) batch_size = 2 num_sample = 100
def reader_train(): for i in range(num_sample): image = images[i] image_path = os.path.join(data_path, image) img = cv2.imread(image_path) img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_LINEAR) img = img.astype(np.float32) / 255 img = img * 2 - 1 img = np.transpose(img, (2, 0, 1)) label = labels[i] + 1 yield img, label
def reader_test(): for i in range(num_sample, num_sample+num_sample): image = images[i] image_path = os.path.join(data_path, image) img = cv2.imread(image_path) img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_LINEAR) img = img.astype(np.float32) / 255 img = img * 2 - 1 img = np.transpose(img, (2, 0, 1)) label = labels[i] + 1 yield img, label
def test(program): acc_top1_ns = [] acc_top5_ns = [] for data in test_reader(): acc_top1_n, acc_top5_n, _ = exe.run(program, feed=data_feeder.feed(data), fetch_list=outputs) acc_top1_ns.append(np.mean(acc_top1_n)) acc_top5_ns.append(np.mean(acc_top5_n)) print("Final eva - acc_top1: {}; acc_top5: {}".format(np.mean(np.array(acc_top1_ns)), np.mean(np.array(acc_top5_ns)))) return np.mean(np.array(acc_top1_ns))
定义调试器
place = fluid.CPUPlace() exe = fluid.Executor(place)
载入模型
[inference_program, feed_target_names, outputs] = fluid.io.load_inference_model(model_path, exe)
加载数据
train_reader = fluid.io.batch(reader_train, batch_size=batch_size) test_reader = fluid.io.batch(reader_test, batch_size=batch_size) feed_list = [inference_program.global_block().var(var_name) for var_name in feed_target_names] # feed_vars_name 是一个由变量名组成的列表 data_feeder = fluid.DataFeeder(feed_list, place)
计算剪枝之前的FLOPs
FLOPs = slim.analysis.flops(inference_program) print("剪枝前的FLOPs: {}".format(FLOPs))
测试网络
test(inference_program)
获取待分析的卷积层参数
params = [] for param in inference_program.global_block().all_parameters(): if "_sep_weights" in param.name: params.append(param.name) print("-"*30 + "待分析的卷积参数" + "-"*30) print(params)
调用sensitivity接口对训练好的模型进行敏感度分析
print("-"*30 + "裁剪率为40%敏感度分析结果" + "-"*30) sens_1 = slim.prune.sensitivity(inference_program, place, params, test, sensitivities_file="sensitivities_1.data", pruned_ratios=[0.4]) print(sens_1)
加载多个进行产出的敏感度文件
s_1 = slim.prune.load_sensitivities("sensitivities_1.data") print("-"*30 + "加载裁减率为40%的敏感度" + "-"*30) print(s_1)
调用get_ratios_by_loss根据敏感度计算裁减率,通过调整参数loss大小获得合适的一组裁剪率
loss = 0.05 ratios = slim.prune.get_ratios_by_loss(s_1, loss) print("-"*30 + "获取的合适的一组裁剪率" + "-"*30) print(ratios) ` 任务描述: 使用x2paddle模型将tf转化为paddle模型,然后基于该模型使用paddleslim做剪枝,但是里面涉及到需要将剪枝后的网络进行finetune,所以在构建数据时需要image和label,但是从上面获得的inputs只能够获得image,所以会出现以下报错: 请问一下怎么获得label的输入接口。