示例#1
0
def compress(args):
    # add ce
    if args.enable_ce:
        SEED = 1
        fluid.default_main_program().random_seed = SEED
        fluid.default_startup_program().random_seed = SEED

    class_dim = 1000
    image_shape = "3,224,224"
    image_shape = [int(m) for m in image_shape.split(",")]
    assert args.model in model_list, "{} is not in lists: {}".format(
        args.model, model_list)
    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')
    # model definition
    model = models.__dict__[args.model]()
    out = model.net(input=image, class_dim=class_dim)
    cost = fluid.layers.cross_entropy(input=out, label=label)
    avg_cost = fluid.layers.mean(x=cost)
    acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
    acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
    val_program = fluid.default_main_program().clone()
    opt = create_optimizer(args)
    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    if args.pretrained_model:

        def if_exist(var):
            return os.path.exists(os.path.join(args.pretrained_model,
                                               var.name))

        fluid.io.load_vars(exe, args.pretrained_model, predicate=if_exist)

    val_reader = paddle.batch(reader.val(), batch_size=args.batch_size)
    val_feed_list = [('image', image.name), ('label', label.name)]
    val_fetch_list = [('acc_top1', acc_top1.name), ('acc_top5', acc_top5.name)]

    train_reader = paddle.batch(reader.train(),
                                batch_size=args.batch_size,
                                drop_last=True)
    train_feed_list = [('image', image.name), ('label', label.name)]
    train_fetch_list = [('loss', avg_cost.name)]

    com_pass = Compressor(place,
                          fluid.global_scope(),
                          fluid.default_main_program(),
                          train_reader=train_reader,
                          train_feed_list=train_feed_list,
                          train_fetch_list=train_fetch_list,
                          eval_program=val_program,
                          eval_reader=val_reader,
                          eval_feed_list=val_feed_list,
                          eval_fetch_list=val_fetch_list,
                          save_eval_model=True,
                          prune_infer_model=[[image.name], [out.name]],
                          train_optimizer=opt)
    com_pass.config(args.config_file)
    com_pass.run()
示例#2
0
def main():
    cfg = load_config(FLAGS.config)
    if 'architecture' in cfg:
        main_arch = cfg.architecture
    else:
        raise ValueError("'architecture' not specified in config file.")

    merge_config(FLAGS.opt)
    if 'log_iter' not in cfg:
        cfg.log_iter = 20

    # check if set use_gpu=True in paddlepaddle cpu version
    check_gpu(cfg.use_gpu)

    if cfg.use_gpu:
        devices_num = fluid.core.get_cuda_device_count()
    else:
        devices_num = int(
            os.environ.get('CPU_NUM', multiprocessing.cpu_count()))

    if 'train_feed' not in cfg:
        train_feed = create(main_arch + 'TrainFeed')
    else:
        train_feed = create(cfg.train_feed)

    if 'eval_feed' not in cfg:
        eval_feed = create(main_arch + 'EvalFeed')
    else:
        eval_feed = create(cfg.eval_feed)

    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)

    lr_builder = create('LearningRate')
    optim_builder = create('OptimizerBuilder')

    # build program
    startup_prog = fluid.Program()
    train_prog = fluid.Program()
    with fluid.program_guard(train_prog, startup_prog):
        with fluid.unique_name.guard():
            model = create(main_arch)
            _, feed_vars = create_feed(train_feed, True)
            train_fetches = model.train(feed_vars)
            loss = train_fetches['loss']
            lr = lr_builder()
            optimizer = optim_builder(lr)
            optimizer.minimize(loss)

    train_reader = create_reader(train_feed, cfg.max_iters, FLAGS.dataset_dir)

    # parse train fetches
    train_keys, train_values, _ = parse_fetches(train_fetches)
    train_keys.append("lr")
    train_values.append(lr.name)

    train_fetch_list = []
    for k, v in zip(train_keys, train_values):
        train_fetch_list.append((k, v))

    eval_prog = fluid.Program()
    with fluid.program_guard(eval_prog, startup_prog):
        with fluid.unique_name.guard():
            model = create(main_arch)
            _, test_feed_vars = create_feed(eval_feed, True)
            fetches = model.eval(test_feed_vars)

    eval_prog = eval_prog.clone(True)

    eval_reader = create_reader(eval_feed, args_path=FLAGS.dataset_dir)
    test_data_feed = fluid.DataFeeder(test_feed_vars.values(), place)

    # parse eval fetches
    extra_keys = []
    if cfg.metric == 'COCO':
        extra_keys = ['im_info', 'im_id', 'im_shape']
    if cfg.metric == 'VOC':
        extra_keys = ['gt_box', 'gt_label', 'is_difficult']
    eval_keys, eval_values, eval_cls = parse_fetches(fetches, eval_prog,
                                                     extra_keys)
    eval_fetch_list = []
    for k, v in zip(eval_keys, eval_values):
        eval_fetch_list.append((k, v))

    exe.run(startup_prog)
    checkpoint.load_params(exe, train_prog, cfg.pretrain_weights)

    best_box_ap_list = []

    def eval_func(program, scope):

        #place = fluid.CPUPlace()
        #exe = fluid.Executor(place)
        results = eval_run(exe, program, eval_reader, eval_keys, eval_values,
                           eval_cls, test_data_feed)

        resolution = None
        if 'mask' in results[0]:
            resolution = model.mask_head.resolution
        box_ap_stats = eval_results(results, eval_feed, cfg.metric,
                                    cfg.num_classes, resolution, False,
                                    FLAGS.output_eval)
        if len(best_box_ap_list) == 0:
            best_box_ap_list.append(box_ap_stats[0])
        elif box_ap_stats[0] > best_box_ap_list[0]:
            best_box_ap_list[0] = box_ap_stats[0]
        logger.info("Best test box ap: {}".format(best_box_ap_list[0]))
        return best_box_ap_list[0]

    test_feed = [('image', test_feed_vars['image'].name),
                 ('im_size', test_feed_vars['im_size'].name)]

    com = Compressor(
        place,
        fluid.global_scope(),
        train_prog,
        train_reader=train_reader,
        train_feed_list=[(key, value.name) for key, value in feed_vars.items()],
        train_fetch_list=train_fetch_list,
        eval_program=eval_prog,
        eval_reader=eval_reader,
        eval_feed_list=test_feed,
        eval_func={'map': eval_func},
        eval_fetch_list=[eval_fetch_list[0]],
        save_eval_model=True,
        prune_infer_model=[["image", "im_size"], ["multiclass_nms_0.tmp_0"]],
        train_optimizer=None)
    com.config(FLAGS.slim_file)
    com.run()
示例#3
0
def compress(args):
    image_shape = [int(m) for m in args.image_shape.split(",")]

    assert args.model in model_list, "{} is not in lists: {}".format(
        args.model, model_list)
    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')
    # model definition
    model = models.__dict__[args.model]()

    if args.model is "GoogleNet":
        out0, out1, out2 = model.net(input=image, class_dim=args.class_dim)
        cost0 = fluid.layers.cross_entropy(input=out0, label=label)
        cost1 = fluid.layers.cross_entropy(input=out1, label=label)
        cost2 = fluid.layers.cross_entropy(input=out2, label=label)
        avg_cost0 = fluid.layers.mean(x=cost0)
        avg_cost1 = fluid.layers.mean(x=cost1)
        avg_cost2 = fluid.layers.mean(x=cost2)
        avg_cost = avg_cost0 + 0.3 * avg_cost1 + 0.3 * avg_cost2
        acc_top1 = fluid.layers.accuracy(input=out0, label=label, k=1)
        acc_top5 = fluid.layers.accuracy(input=out0, label=label, k=5)
    else:
        out = model.net(input=image, class_dim=args.class_dim)
        cost = fluid.layers.cross_entropy(input=out, label=label)
        avg_cost = fluid.layers.mean(x=cost)
        acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
        acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
    val_program = fluid.default_main_program().clone()
    if args.quant_only:
        boundaries = [
            args.total_images / args.batch_size * 10,
            args.total_images / args.batch_size * 16
        ]
        values = [1e-4, 1e-5, 1e-6]
    else:
        boundaries = [
            args.total_images / args.batch_size * 30,
            args.total_images / args.batch_size * 60,
            args.total_images / args.batch_size * 90
        ]
        values = [0.1, 0.01, 0.001, 0.0001]
    opt = fluid.optimizer.Momentum(
        momentum=0.9,
        learning_rate=fluid.layers.piecewise_decay(boundaries=boundaries,
                                                   values=values),
        regularization=fluid.regularizer.L2Decay(4e-5))

    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    if args.pretrained_model:

        def if_exist(var):
            return os.path.exists(os.path.join(args.pretrained_model,
                                               var.name))

        fluid.io.load_vars(exe, args.pretrained_model, predicate=if_exist)

    val_reader = paddle.batch(reader.val(), batch_size=args.batch_size)
    val_feed_list = [('image', image.name), ('label', label.name)]
    val_fetch_list = [('acc_top1', acc_top1.name), ('acc_top5', acc_top5.name)]

    train_reader = paddle.batch(reader.train(),
                                batch_size=args.batch_size,
                                drop_last=True)
    train_feed_list = [('image', image.name), ('label', label.name)]
    train_fetch_list = [('loss', avg_cost.name)]

    teacher_programs = []
    distiller_optimizer = None
    if args.teacher_model:
        teacher_model = models.__dict__[args.teacher_model]()
        # define teacher program
        teacher_program = fluid.Program()
        startup_program = fluid.Program()
        with fluid.program_guard(teacher_program, startup_program):
            img = teacher_program.global_block()._clone_variable(
                image, force_persistable=False)
            predict = teacher_model.net(img,
                                        class_dim=args.class_dim,
                                        conv1_name='res_conv1',
                                        fc_name='res_fc')
        exe.run(startup_program)
        assert args.teacher_pretrained_model and os.path.exists(
            args.teacher_pretrained_model
        ), "teacher_pretrained_model should be set when teacher_model is not None."

        def if_exist(var):
            return os.path.exists(
                os.path.join(args.teacher_pretrained_model, var.name))

        fluid.io.load_vars(exe,
                           args.teacher_pretrained_model,
                           main_program=teacher_program,
                           predicate=if_exist)

        distiller_optimizer = opt
        teacher_programs.append(teacher_program.clone(for_test=True))

    com_pass = Compressor(place,
                          fluid.global_scope(),
                          fluid.default_main_program(),
                          train_reader=train_reader,
                          train_feed_list=train_feed_list,
                          train_fetch_list=train_fetch_list,
                          eval_program=val_program,
                          eval_reader=val_reader,
                          eval_feed_list=val_feed_list,
                          eval_fetch_list=val_fetch_list,
                          teacher_programs=teacher_programs,
                          train_optimizer=opt,
                          distiller_optimizer=distiller_optimizer)
    com_pass.config(args.compress_config)
    com_pass.run()
示例#4
0
def train(args, data_args, train_params, train_file_list, val_file_list):

    model_save_dir = args.model_save_dir
    pretrained_model = args.pretrained_model
    use_gpu = args.use_gpu
    parallel = args.parallel
    enable_ce = args.enable_ce
    is_shuffle = True

    if not use_gpu:
        devices_num = int(
            os.environ.get('CPU_NUM', multiprocessing.cpu_count()))
    else:
        devices_num = fluid.core.get_cuda_device_count()

    batch_size = train_params['batch_size']
    epoc_num = train_params['epoc_num']
    batch_size_per_device = batch_size // devices_num
    num_workers = 8

    startup_prog = fluid.Program()
    train_prog = fluid.Program()
    test_prog = fluid.Program()

    train_py_reader, loss = build_program(main_prog=train_prog,
                                          startup_prog=startup_prog,
                                          train_params=train_params,
                                          is_train=True)
    test_py_reader, map_var, _, _ = build_program(main_prog=test_prog,
                                                  startup_prog=startup_prog,
                                                  train_params=train_params,
                                                  is_train=False)

    test_prog = test_prog.clone(for_test=True)

    for param in train_prog.global_block().all_parameters():
        if 'conv' in param.name:
            print param.name, param.shape
    place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(startup_prog)

    if pretrained_model:

        def if_exist(var):
            return os.path.exists(os.path.join(pretrained_model, var.name))

        fluid.io.load_vars(exe,
                           pretrained_model,
                           main_program=train_prog,
                           predicate=if_exist)

    test_reader = reader.test(data_args, val_file_list, batch_size)
    test_py_reader.decorate_paddle_reader(test_reader)
    train_reader = reader.train(data_args,
                                train_file_list,
                                batch_size_per_device,
                                shuffle=is_shuffle,
                                use_multiprocess=args.use_multiprocess,
                                num_workers=num_workers,
                                enable_ce=enable_ce)
    train_py_reader.decorate_paddle_reader(train_reader)

    train_fetch_list = [("loss", loss.name)]
    val_fetch_list = [("map", map_var.name)]
    compressor = Compressor(place,
                            fluid.global_scope(),
                            train_prog,
                            train_reader=train_py_reader,
                            train_feed_list=None,
                            train_fetch_list=train_fetch_list,
                            eval_program=test_prog,
                            eval_reader=test_py_reader,
                            eval_feed_list=None,
                            eval_fetch_list=val_fetch_list,
                            train_optimizer=None)
    compressor.config('./compress.yaml')
    compressor.run()
示例#5
0
def do_compress(args):
    train_program = fluid.default_main_program()
    startup_program = fluid.default_startup_program()

    dataset = reader.Dataset(args)
    with fluid.program_guard(train_program, startup_program):
        train_program.random_seed = args.random_seed
        startup_program.random_seed = args.random_seed

        with fluid.unique_name.guard():
            train_ret = creator.create_model(args,
                                             dataset.vocab_size,
                                             dataset.num_labels,
                                             mode='train')

    test_program = train_program.clone()

    optimizer = fluid.optimizer.Adam(learning_rate=args.base_learning_rate)

    # init executor
    if args.use_cuda:
        place = fluid.CUDAPlace(int(os.getenv('FLAGS_selected_gpus', '0')))
        dev_count = fluid.core.get_cuda_device_count()
    else:
        dev_count = min(multiprocessing.cpu_count(), args.cpu_num)
        if (dev_count < args.cpu_num):
            print(
                "WARNING: The total CPU NUM in this machine is %d, which is less than cpu_num parameter you set. "
                "Change the cpu_num from %d to %d" %
                (dev_count, args.cpu_num, dev_count))
        os.environ['CPU_NUM'] = str(dev_count)
        place = fluid.CPUPlace()

    train_reader = paddle.batch(dataset.file_reader(args.train_data),
                                batch_size=args.batch_size)
    test_reader = paddle.batch(dataset.file_reader(args.test_data),
                               batch_size=args.batch_size)

    exe = fluid.Executor(place)
    exe.run(startup_program)

    if args.init_checkpoint:
        utils.init_checkpoint(exe, args.init_checkpoint + '.pdckpt',
                              train_program)

    train_feed_list = [('words', train_ret['words'].name),
                       ("targets", train_ret["targets"].name)]
    train_fetch_list = [('loss', train_ret['avg_cost'].name)]

    test_feed_list = [('words', train_ret['words'].name),
                      ("targets", train_ret["targets"].name)]
    test_fetch_list = [('f1_score', train_ret['f1_score'].name)]
    print(train_ret['crf_decode'].name)

    com_pass = Compressor(place,
                          fluid.global_scope(),
                          train_program=train_program,
                          train_reader=train_reader,
                          train_feed_list=train_feed_list,
                          train_fetch_list=train_fetch_list,
                          eval_program=test_program,
                          eval_reader=test_reader,
                          eval_feed_list=test_feed_list,
                          eval_fetch_list=test_fetch_list,
                          teacher_programs=[],
                          train_optimizer=optimizer,
                          distiller_optimizer=None)
    com_pass.config(args.compress_config)
    com_pass.run()
示例#6
0
def compress(args):
    # add ce
    if args.enable_ce:
        SEED = 1
        fluid.default_main_program().random_seed = SEED
        fluid.default_startup_program().random_seed = SEED

    image_shape = [int(m) for m in args.image_shape.split(",")]

    assert args.model in model_list, "{} is not in lists: {}".format(
        args.model, model_list)
    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')
    # model definition
    model = models.__dict__[args.model]()

    if args.model == 'ResNet34':
        model.prefix_name = 'res34'
        out = model.net(input=image, class_dim=args.class_dim, fc_name='fc_0')
    else:
        out = model.net(input=image, class_dim=args.class_dim)
    cost = fluid.layers.cross_entropy(input=out, label=label)
    avg_cost = fluid.layers.mean(x=cost)
    acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
    acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
    #print("="*50+"student_model_params"+"="*50)
    #for v in fluid.default_main_program().list_vars():
    #    print(v.name, v.shape)

    val_program = fluid.default_main_program().clone()
    boundaries = [
        args.total_images / args.batch_size * 30,
        args.total_images / args.batch_size * 60,
        args.total_images / args.batch_size * 90
    ]
    values = [0.1, 0.01, 0.001, 0.0001]
    opt = fluid.optimizer.Momentum(
        momentum=0.9,
        learning_rate=fluid.layers.piecewise_decay(boundaries=boundaries,
                                                   values=values),
        regularization=fluid.regularizer.L2Decay(4e-5))

    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    if args.pretrained_model:

        def if_exist(var):
            return os.path.exists(os.path.join(args.pretrained_model,
                                               var.name))

        fluid.io.load_vars(exe, args.pretrained_model, predicate=if_exist)

    val_reader = paddle.batch(reader.val(), batch_size=args.batch_size)
    val_feed_list = [('image', image.name), ('label', label.name)]
    val_fetch_list = [('acc_top1', acc_top1.name), ('acc_top5', acc_top5.name)]

    train_reader = paddle.batch(reader.train(),
                                batch_size=args.batch_size,
                                drop_last=True)
    train_feed_list = [('image', image.name), ('label', label.name)]
    train_fetch_list = [('loss', avg_cost.name)]

    teacher_programs = []
    distiller_optimizer = None

    teacher_model = models.__dict__[args.teacher_model](prefix_name='res50')
    # define teacher program
    teacher_program = fluid.Program()
    startup_program = fluid.Program()
    with fluid.program_guard(teacher_program, startup_program):
        img = teacher_program.global_block()._clone_variable(
            image, force_persistable=False)
        predict = teacher_model.net(img,
                                    class_dim=args.class_dim,
                                    fc_name='fc_0')
    #print("="*50+"teacher_model_params"+"="*50)
    #for v in teacher_program.list_vars():
    #    print(v.name, v.shape)
    #return

    exe.run(startup_program)
    assert args.teacher_pretrained_model and os.path.exists(
        args.teacher_pretrained_model
    ), "teacher_pretrained_model should be set when teacher_model is not None."

    def if_exist(var):
        return os.path.exists(
            os.path.join(args.teacher_pretrained_model, var.name))

    fluid.io.load_vars(exe,
                       args.teacher_pretrained_model,
                       main_program=teacher_program,
                       predicate=if_exist)

    distiller_optimizer = opt
    teacher_programs.append(teacher_program.clone(for_test=True))

    com_pass = Compressor(place,
                          fluid.global_scope(),
                          fluid.default_main_program(),
                          train_reader=train_reader,
                          train_feed_list=train_feed_list,
                          train_fetch_list=train_fetch_list,
                          eval_program=val_program,
                          eval_reader=val_reader,
                          eval_feed_list=val_feed_list,
                          eval_fetch_list=val_fetch_list,
                          teacher_programs=teacher_programs,
                          save_eval_model=True,
                          prune_infer_model=[[image.name], [out.name]],
                          train_optimizer=opt,
                          distiller_optimizer=distiller_optimizer)
    com_pass.config(args.compress_config)
    com_pass.run()
示例#7
0
def compress(args):
    image_shape = "3,224,224"
    image_shape = [int(m) for m in image_shape.split(",")]

    image = fluid.data(name='image',
                       shape=[None] + image_shape,
                       dtype='float32')
    label = fluid.data(name='label', shape=[None, 1], dtype='int64')
    # model definition
    model = models.__dict__[args.model]()

    out = model.net(input=image, class_dim=1000)
    # print(out)
    cost = fluid.layers.cross_entropy(input=out, label=label)
    avg_cost = fluid.layers.mean(x=cost)
    acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
    acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)

    val_program = fluid.default_main_program().clone()

    # quantization usually use small learning rate
    values = [1e-4, 1e-5]
    opt = fluid.optimizer.Momentum(
        momentum=0.9,
        learning_rate=fluid.layers.piecewise_decay(boundaries=[5000 * 12],
                                                   values=values),
        regularization=fluid.regularizer.L2Decay(1e-4))

    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    if args.pretrained_model:
        assert os.path.exists(
            args.pretrained_model), "pretrained_model path doesn't exist"

        def if_exist(var):
            return os.path.exists(os.path.join(args.pretrained_model,
                                               var.name))

        fluid.io.load_vars(exe, args.pretrained_model, predicate=if_exist)

    val_reader = paddle.batch(reader.val(), batch_size=args.batch_size)
    val_feed_list = [('image', image.name), ('label', label.name)]
    val_fetch_list = [('acc_top1', acc_top1.name), ('acc_top5', acc_top5.name)]

    train_reader = paddle.batch(reader.train(),
                                batch_size=args.batch_size,
                                drop_last=True)
    train_feed_list = [('image', image.name), ('label', label.name)]
    train_fetch_list = [('loss', avg_cost.name)]

    com_pass = Compressor(place,
                          fluid.global_scope(),
                          fluid.default_main_program(),
                          train_reader=train_reader,
                          train_feed_list=train_feed_list,
                          train_fetch_list=train_fetch_list,
                          eval_program=val_program,
                          eval_reader=val_reader,
                          eval_feed_list=val_feed_list,
                          eval_fetch_list=val_fetch_list,
                          teacher_programs=[],
                          train_optimizer=opt,
                          prune_infer_model=[[image.name], [out.name]],
                          distiller_optimizer=None)
    com_pass.config(args.config_file)
    com_pass.run()

    conv_op_num = 0
    fake_quant_op_num = 0
    for op in com_pass.context.eval_graph.ops():
        if op._op.type == 'conv2d':
            conv_op_num += 1
        elif op._op.type.startswith('fake_quantize'):
            fake_quant_op_num += 1
    print('conv op num {}'.format(conv_op_num))
    print('fake quant op num {}'.format(fake_quant_op_num))
示例#8
0
def compress(args):
    assert args.batch_size > 0, 'batch size of input should be more than one'
    image_shape = [3, int(args.image_height), int(args.image_width)]
    assert args.model in model_list, "{} is not in lists: {}".format(
        args.model, model_list)
    assert args.strategy in strategy_list, "{} is not in lists: {}".format(
        args.strategy, strategy_list)
    image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')
    # model definition
    model = models.__dict__[args.model]()

    if args.model is "GoogleNet":
        out0, out1, out2 = model.net(input=image, class_dim=args.class_dim)
        cost0 = fluid.layers.cross_entropy(input=out0, label=label)
        cost1 = fluid.layers.cross_entropy(input=out1, label=label)
        cost2 = fluid.layers.cross_entropy(input=out2, label=label)
        avg_cost0 = fluid.layers.mean(x=cost0)
        avg_cost1 = fluid.layers.mean(x=cost1)
        avg_cost2 = fluid.layers.mean(x=cost2)
        avg_cost = avg_cost0 + 0.3 * avg_cost1 + 0.3 * avg_cost2
        acc_top1 = fluid.layers.accuracy(input=out0, label=label, k=1)
        acc_top5 = fluid.layers.accuracy(input=out0, label=label, k=5)
    else:
        out = model.net(input=image, class_dim=args.class_dim)
        cost = fluid.layers.cross_entropy(input=out, label=label)
        avg_cost = fluid.layers.mean(x=cost)
        acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
        acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
    val_program = fluid.default_main_program().clone()

    assert os.path.exists(
        args.data_dir), "data directory '{}' is not exist".format(
            args.data_dir)
    train_file_list = os.path.join(args.data_dir, 'train_list.txt')
    assert os.path.exists(
        train_file_list), "data directory '{}' is not exist".format(
            train_file_list)
    val_file_list = os.path.join(args.data_dir, 'val_list.txt')
    assert os.path.exists(
        val_file_list), "data directory '{}' is not exist".format(
            val_file_list)
    with open(train_file_list, 'r') as f:
        lines = f.readlines()
    total_images = len(lines)
    boundaries = [
        total_images / args.batch_size * 30,
        total_images / args.batch_size * 60,
        total_images / args.batch_size * 90
    ]
    values = [args.lr, args.lr * 0.1, args.lr * 0.01, args.lr * 0.001]
    opt = fluid.optimizer.Momentum(
        momentum=0.9,
        learning_rate=fluid.layers.piecewise_decay(boundaries=boundaries,
                                                   values=values),
        regularization=fluid.regularizer.L2Decay(4e-5))

    if args.use_gpu:
        os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
    place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    assert os.path.exists(
        args.pretrained_model
    ), "pretrained model directory '{}' is not exist".format(
        args.pretrained_model)
    if args.pretrained_model:

        def if_exist(var):
            return os.path.exists(os.path.join(args.pretrained_model,
                                               var.name))

        fluid.io.load_vars(exe, args.pretrained_model, predicate=if_exist)
    val_reader = paddle.batch(reader.val(settings=args),
                              batch_size=args.batch_size)
    val_feed_list = [('image', image.name), ('label', label.name)]
    val_fetch_list = [('acc_top1', acc_top1.name), ('acc_top5', acc_top5.name)]

    train_reader = paddle.batch(reader.train(settings=args),
                                batch_size=args.batch_size,
                                drop_last=True)
    train_feed_list = [('image', image.name), ('label', label.name)]
    train_fetch_list = [('loss', avg_cost.name)]

    teacher_programs = []
    distiller_optimizer = None

    com_pass = Compressor(place,
                          fluid.global_scope(),
                          fluid.default_main_program(),
                          train_reader=train_reader,
                          train_feed_list=train_feed_list,
                          train_fetch_list=train_fetch_list,
                          eval_program=val_program,
                          eval_reader=val_reader,
                          eval_feed_list=val_feed_list,
                          eval_fetch_list=val_fetch_list,
                          teacher_programs=teacher_programs,
                          train_optimizer=opt,
                          distiller_optimizer=distiller_optimizer)
    if args.strategy == 'Uniform':
        compress_config = "configs/filter_pruning_uniform.yaml"
    else:
        compress_config = "configs/filter_pruning_sen.yaml"
    com_pass.config(compress_config)
    assert args.target_ratio > 0 and args.target_ratio < 1, "prune ratio should be between 0 and 1"
    com_pass.strategies[0].target_ratio = args.target_ratio
    com_pass.checkpoint_path = args.checkpoint_path
    com_pass.run()
    pruned_prog = com_pass.eval_graph.program
    fluid.io.save_inference_model("./pruned_model/", [image.name], [out],
                                  exe,
                                  main_program=pruned_prog)