예제 #1
0
def search():
    if not fluid.core.is_compiled_with_cuda():
        return

    space = LightNASSpace()

    startup_prog, train_prog, test_prog, train_metrics, test_metrics, train_reader, test_reader = space.create_net(
    )
    train_cost, train_acc1, train_acc5, global_lr = train_metrics
    test_cost, test_acc1, test_acc5 = test_metrics

    place = fluid.CUDAPlace(0)
    exe = fluid.Executor(place)
    exe.run(startup_prog)

    val_fetch_list = [('acc_top1', test_acc1.name),
                      ('acc_top5', test_acc5.name)]
    train_fetch_list = [('loss', train_cost.name)]

    com_pass = Compressor(place,
                          fluid.global_scope(),
                          train_prog,
                          train_reader=train_reader,
                          train_feed_list=None,
                          train_fetch_list=train_fetch_list,
                          eval_program=test_prog,
                          eval_reader=test_reader,
                          eval_feed_list=None,
                          eval_fetch_list=val_fetch_list,
                          train_optimizer=None,
                          search_space=space)
    com_pass.config('./compress.yaml')
    eval_graph = com_pass.run()
예제 #2
0
    def quan(self, config_file):
        if not fluid.core.is_compiled_with_cuda():
            return
        class_dim = 10
        image_shape = [1, 28, 28]

        train_program = fluid.Program()
        startup_program = fluid.Program()
        val_program = fluid.Program()

        with fluid.program_guard(train_program, startup_program):
            with fluid.unique_name.guard():
                image = fluid.layers.data(name='image',
                                          shape=image_shape,
                                          dtype='float32')
                image.stop_gradient = False
                label = fluid.layers.data(name='label',
                                          shape=[1],
                                          dtype='int64')
                out = MobileNet(name='quan').net(input=image,
                                                 class_dim=class_dim)
                print("out: {}".format(out.name))
                acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
                acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
                cost = fluid.layers.cross_entropy(input=out, label=label)
                avg_cost = fluid.layers.mean(x=cost)
        optimizer = fluid.optimizer.Momentum(
            momentum=0.9,
            learning_rate=0.01,
            regularization=fluid.regularizer.L2Decay(4e-5))

        val_program = train_program.clone(for_test=False)

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

        val_reader = self.set_val_reader(image, label, place)

        val_feed_list = self.set_feed_list(image, label)
        val_fetch_list = [('acc_top1', acc_top1.name),
                          ('acc_top5', acc_top5.name)]

        train_reader = self.set_train_reader(image, label, place)
        train_feed_list = self.set_feed_list(image, label)
        train_fetch_list = [('loss', avg_cost.name)]

        com_pass = Compressor(place,
                              fluid.global_scope(),
                              train_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,
                              train_optimizer=optimizer)
        com_pass.config(config_file)
        eval_graph = com_pass.run()
예제 #3
0
    def test_compression(self):
        """
        Model: mobilenet_v1
        data: mnist
        step1: Training one epoch
        step2: pruning flops
        step3: fine-tune one epoch
        step4: check top1_acc.
        """
        if not fluid.core.is_compiled_with_cuda():
            return
        class_dim = 10
        image_shape = [1, 28, 28]
        image = fluid.layers.data(name='image',
                                  shape=image_shape,
                                  dtype='float32')
        image.stop_gradient = False
        label = fluid.layers.data(name='label', shape=[1], dtype='int64')
        out = MobileNet("auto_pruning").net(input=image, class_dim=class_dim)
        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(for_test=False)

        cost = fluid.layers.cross_entropy(input=out, label=label)
        avg_cost = fluid.layers.mean(x=cost)

        optimizer = fluid.optimizer.Momentum(
            momentum=0.9,
            learning_rate=0.01,
            regularization=fluid.regularizer.L2Decay(4e-5))

        place = fluid.CUDAPlace(0)
        exe = fluid.Executor(place)
        exe.run(fluid.default_startup_program())

        val_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=128)

        val_feed_list = [('img', image.name), ('label', label.name)]
        val_fetch_list = [('acc_top1', acc_top1.name),
                          ('acc_top5', acc_top5.name)]

        train_reader = paddle.batch(paddle.dataset.mnist.train(),
                                    batch_size=128)
        train_feed_list = [('img', 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,
                              train_optimizer=optimizer)
        com_pass.config('./auto_pruning/compress.yaml')
        eval_graph = com_pass.run()
예제 #4
0
    def test_compression_with_target_latency(self):
        """
        Test LightNAS with target_latency.
        """
        # Update compress.yaml
        lines = list()
        fid = open('./light_nas/compress.yaml')
        for line in fid:
            if 'target_latency' in line:
                lines.append('        target_latency: 1\n')
            else:
                lines.append(line)
        fid.close()
        fid = open('./light_nas/compress.yaml', 'w')
        for line in lines:
            fid.write(line)
        fid.close()

        # Begin test
        if not fluid.core.is_compiled_with_cuda():
            return

        space = LightNASSpace()

        startup_prog, train_prog, test_prog, train_metrics, test_metrics, train_reader, test_reader = space.create_net(
        )
        train_cost, train_acc1, train_acc5, global_lr = train_metrics
        test_cost, test_acc1, test_acc5 = test_metrics

        place = fluid.CUDAPlace(0)
        exe = fluid.Executor(place)
        exe.run(startup_prog)

        val_fetch_list = [('acc_top1', test_acc1.name),
                          ('acc_top5', test_acc5.name)]
        train_fetch_list = [('loss', train_cost.name)]

        com_pass = Compressor(
            place,
            fluid.global_scope(),
            train_prog,
            train_reader=train_reader,
            train_feed_list=None,
            train_fetch_list=train_fetch_list,
            eval_program=test_prog,
            eval_reader=test_reader,
            eval_feed_list=None,
            eval_fetch_list=val_fetch_list,
            train_optimizer=None,
            search_space=space)
        com_pass.config('./light_nas/compress.yaml')
        eval_graph = com_pass.run()
예제 #5
0
 def _warmup(self, reader=None, config_path=''):
     com_pass = Compressor(place=None,
                           scope=None,
                           train_program=None,
                           train_reader=None,
                           train_feed_list=[],
                           train_fetch_list=[],
                           eval_program=None,
                           eval_reader=reader,
                           eval_feed_list=[],
                           eval_fetch_list=[],
                           teacher_programs=[],
                           checkpoint_path='',
                           train_optimizer=None,
                           distiller_optimizer=None)
     com_pass.config(config_path)
     com_pass.run()
예제 #6
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
    model = create(main_arch)
    _, train_feed_vars = create_feed(train_feed, True)
    train_fetches = model.train(train_feed_vars)
    loss = train_fetches['loss']
    lr = lr_builder()
    opt = optim_builder(lr)
    opt.minimize(loss)
    #for v in fluid.default_main_program().list_vars():
    #    if "py_reader" not in v.name and "double_buffer" not in v.name and "generated_var" not in v.name:
    #        print(v.name, v.shape)

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

    exe.run(fluid.default_startup_program())

    # 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))
    print("train_fetch_list: {}".format(train_fetch_list))

    eval_prog = fluid.Program()
    startup_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))
    print("eval_fetch_list: {}".format(eval_fetch_list))

    exe.run(startup_prog)
    checkpoint.load_params(exe,
                           fluid.default_main_program(), cfg.pretrain_weights)

    best_box_ap_list = []

    def eval_func(program, scope):
        results = eval_run(exe, program, eval_reader, eval_keys, eval_values,
                           eval_cls, test_data_feed)

        resolution = None
        is_bbox_normalized = False
        if 'mask' in results[0]:
            resolution = model.mask_head.resolution
        box_ap_stats = eval_results(results, eval_feed, cfg.metric,
                                    cfg.num_classes, resolution,
                                    is_bbox_normalized, 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)]

    teacher_cfg = load_config(FLAGS.teacher_config)
    teacher_arch = teacher_cfg.architecture
    teacher_programs = []
    teacher_program = fluid.Program()
    teacher_startup_program = fluid.Program()
    with fluid.program_guard(teacher_program, teacher_startup_program):
        with fluid.unique_name.guard('teacher_'):
            teacher_feed_vars = OrderedDict()
            for name, var in train_feed_vars.items():
                teacher_feed_vars[name] = teacher_program.global_block(
                )._clone_variable(
                    var, force_persistable=False)
            model = create(teacher_arch)
            train_fetches = model.train(teacher_feed_vars)
    #print("="*50+"teacher_model_params"+"="*50)
    #for v in teacher_program.list_vars():
    #    print(v.name, v.shape)
    #return

    exe.run(teacher_startup_program)
    assert FLAGS.teacher_pretrained and os.path.exists(
        FLAGS.teacher_pretrained
    ), "teacher_pretrained should be set when teacher_model is not None."

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

    fluid.io.load_vars(
        exe,
        FLAGS.teacher_pretrained,
        main_program=teacher_program,
        predicate=if_exist)

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

    com = Compressor(
        place,
        fluid.global_scope(),
        fluid.default_main_program(),
        train_reader=train_reader,
        train_feed_list=[(key, value.name)
                         for key, value in train_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:1],
        save_eval_model=True,
        prune_infer_model=[["image", "im_size"], ["multiclass_nms_0.tmp_0"]],
        teacher_programs=teacher_programs,
        train_optimizer=None,
        distiller_optimizer=opt,
        log_period=20)
    com.config(FLAGS.slim_file)
    com.run()
예제 #7
0
    def uniform_restore_from_checkpoint(self, config_file):

        class_dim = 10
        image_shape = [1, 28, 28]

        train_program = fluid.Program()
        startup_program = fluid.Program()
        train_program.random_seed = 10
        startup_program.random_seed = 10

        with fluid.program_guard(train_program, startup_program):
            with fluid.unique_name.guard():
                image = fluid.layers.data(
                    name='image', shape=image_shape, dtype='float32')
                image.stop_gradient = False
                label = fluid.layers.data(
                    name='label', shape=[1], dtype='int64')
                out = fluid.layers.conv2d(image, 4, 1)
                out = fluid.layers.fc(out, size=class_dim)
                out = fluid.layers.softmax(out)
                acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
                acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
                cost = fluid.layers.cross_entropy(input=out, label=label)
                avg_cost = fluid.layers.mean(x=cost)
        val_program = train_program.clone(for_test=False)

        optimizer = fluid.optimizer.Momentum(
            momentum=0.9,
            learning_rate=0.01,
            regularization=fluid.regularizer.L2Decay(4e-5))

        place = fluid.CPUPlace()
        scope = fluid.Scope()
        exe = fluid.Executor(place)
        exe.run(startup_program, scope=scope)

        val_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=128)

        val_feed_list = [('img', image.name), ('label', label.name)]
        val_fetch_list = [('acc_top1', acc_top1.name), ('acc_top5',
                                                        acc_top5.name)]

        train_reader = paddle.batch(
            paddle.dataset.mnist.train(), batch_size=128)
        train_feed_list = [('img', image.name), ('label', label.name)]
        train_fetch_list = [('loss', avg_cost.name)]

        com_pass = Compressor(
            place,
            scope,
            train_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,
            train_optimizer=optimizer)
        com_pass.config(config_file)
        eval_graph = com_pass.run()
        return com_pass.context.eval_results['acc_top1'][-1]
예제 #8
0
    def test_compression(self):
        if not fluid.core.is_compiled_with_cuda():
            return
        class_dim = 10
        image_shape = [1, 28, 28]
        image = fluid.layers.data(name='image',
                                  shape=image_shape,
                                  dtype='float32')
        image.stop_gradient = False
        label = fluid.layers.data(name='label', shape=[1], dtype='int64')
        out = MobileNet(name="student").net(input=image, class_dim=class_dim)
        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(for_test=False)

        cost = fluid.layers.cross_entropy(input=out, label=label)
        avg_cost = fluid.layers.mean(x=cost)

        optimizer = fluid.optimizer.Momentum(
            momentum=0.9,
            learning_rate=fluid.layers.piecewise_decay(
                boundaries=[5, 10], values=[0.01, 0.001, 0.0001]),
            regularization=fluid.regularizer.L2Decay(4e-5))

        place = fluid.CUDAPlace(0)
        exe = fluid.Executor(place)
        exe.run(fluid.default_startup_program())

        val_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=128)

        val_feed_list = [('img', image.name), ('label', label.name)]
        val_fetch_list = [('acc_top1', acc_top1.name),
                          ('acc_top5', acc_top5.name)]

        train_reader = paddle.batch(paddle.dataset.mnist.train(),
                                    batch_size=128)
        train_feed_list = [('img', image.name), ('label', label.name)]
        train_fetch_list = [('loss', avg_cost.name)]

        # 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 = MobileNet(name="teacher").net(input=img,
                                                    class_dim=class_dim)

        exe.run(startup_program)

        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_program.clone(for_test=True)],
            train_optimizer=optimizer,
            distiller_optimizer=optimizer)
        com_pass.config('./distillation/compress.yaml')
        eval_graph = com_pass.run()
예제 #9
0
    def test_eval_func(self):
        class_dim = 10
        image_shape = [1, 28, 28]
        image = fluid.layers.data(name='image',
                                  shape=image_shape,
                                  dtype='float32')
        image.stop_gradient = False
        label = fluid.layers.data(name='label', shape=[1], dtype='int64')
        out = fluid.layers.fc(input=image, size=class_dim)
        out = fluid.layers.softmax(out)
        acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
        val_program = fluid.default_main_program().clone(for_test=False)

        cost = fluid.layers.cross_entropy(input=out, label=label)
        avg_cost = fluid.layers.mean(x=cost)

        optimizer = fluid.optimizer.Momentum(
            momentum=0.9,
            learning_rate=0.01,
            regularization=fluid.regularizer.L2Decay(4e-5))

        place = fluid.CPUPlace()
        exe = fluid.Executor(place)
        exe.run(fluid.default_startup_program())

        val_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=128)

        train_reader = paddle.batch(paddle.dataset.mnist.train(),
                                    batch_size=128)
        train_feed_list = [('img', image.name), ('label', label.name)]
        train_fetch_list = [('loss', avg_cost.name)]
        eval_feed_list = [('img', image.name), ('label', label.name)]
        eval_fetch_list = [('acc_top1', acc_top1.name)]

        def eval_func(program, scope):
            place = fluid.CPUPlace()
            exe = fluid.Executor(place)
            feeder = fluid.DataFeeder(feed_list=[image.name, label.name],
                                      place=place,
                                      program=program)
            results = []
            for data in val_reader():
                result = exe.run(program=program,
                                 scope=scope,
                                 fetch_list=[acc_top1.name],
                                 feed=feeder.feed(data))
                results.append(np.array(result))
            result = np.mean(results)
            return result

        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_feed_list=eval_feed_list,
                              eval_fetch_list=eval_fetch_list,
                              eval_func={"score": eval_func},
                              prune_infer_model=[[image.name], [out.name]],
                              train_optimizer=optimizer)
        com_pass.config('./configs/compress.yaml')
        com_pass.run()
        self.assertTrue('score' in com_pass.context.eval_results)
        self.assertTrue(float(com_pass.context.eval_results['score'][0]) > 0.9)
        self.assertTrue(os.path.exists("./checkpoints/0/eval_model/__model__"))
        self.assertTrue(
            os.path.exists("./checkpoints/0/eval_model/__model__.infer"))
        self.assertTrue(
            os.path.exists("./checkpoints/0/eval_model/__params__"))