コード例 #1
0
ファイル: nonlocal_utils.py プロジェクト: zhong110020/models
def load_weights_params_from_file(exe, prog, weights, place):
    """
    The params of the training process is stored in the file named weights.
    However, the network of the training and test process is slightly different due to the layer 
    named "pred" was fc in trainng but convolution in test. When loading weights of pred (pred_w), 
    from the pretrained file, shape mismatch error will be raised due to the check in fluid.io. 
    This check on params' shape is newly added in fluid.version==1.6.0. So it is recommendated to 
    treat pred_w specifically.
    The process is as following:
      1, load the parmas from weights file into a state_dict
      2, specifically treat the paramter named "pred_w" from the foramt of fc into convolution
      3, set the state_dict to prog
    """

    logger.info('Load test weights from {}'.format(weights))

    # get the param_list in prog
    prog_vars = list(filter(is_parameter, prog.list_vars()))

    if weights[-9:] == '.pdparams':
        weights = weights[:-9]

    state_dict = fluid.load_program_state(weights, var_list=prog_vars)
    pred_array = state_dict["pred_w"]
    pred_array = np.transpose(pred_array, (1, 0))
    pred_array = np.reshape(
        pred_array, [pred_array.shape[0], pred_array.shape[1], 1, 1, 1])
    state_dict["pred_w"] = pred_array
    fluid.set_program_state(prog, state_dict)
コード例 #2
0
    def load_pretrain_params(self, exe, pretrain, prog, place):
        """
        The pretrained params are ResNet50 pretrained on ImageNet.
        However, conv1_weights' shape of StNet is not the same as that in ResNet50 because the input are super-image
        concatanated by a series of images. So it is recommendated to treat conv1_weights specifically.
        The process is as following:
          1, load params from pretrain
          2, get the value of conv1_weights in the state_dict and transform it
          3, set the transformed value to conv1_weights in prog
        """

        logger.info(
            "Load pretrain weights from {}, exclude fc, batch_norm, xception, conv3d layers."
            .format(pretrain))

        state_dict = fluid.load_program_state(pretrain)
        dict_keys = list(state_dict.keys())
        for name in dict_keys:
            if ("batch_norm" in name) or ("fc_0" in name) or ("batch_norm" in name) \
                     or ("xception" in name) or ("conv3d" in name):
                del state_dict[name]
                logger.info(
                    'Delete {} from pretrained parameters. Do not load it'.
                    format(name))
        conv1_weights = state_dict["conv1_weights"]
        conv1_weights = np.mean(conv1_weights, axis=1,
                                keepdims=True) / self.seglen
        conv1_weights = np.repeat(conv1_weights, 3 * self.seglen, axis=1)
        logger.info(
            'conv1_weights is transformed from [Cout, 3, Kh, Kw] into [Cout, 3*seglen, Kh, Kw]'
        )
        state_dict["conv1_weights"] = conv1_weights
        fluid.set_program_state(prog, state_dict)
コード例 #3
0
    def load_pretrain_params(self, exe, pretrain, prog, place):
        logger.info("Load pretrain weights from {}, exclude fc layer.".format(pretrain))

        state_dict = fluid.load_program_state(pretrain)
        dict_keys = list(state_dict.keys())
        for name in dict_keys:
            if "fc_0" in name:
                del state_dict[name]
                logger.info('Delete {} from pretrained parameters. Do not load it'.format(name))
        fluid.set_program_state(prog, state_dict)
コード例 #4
0
ファイル: nonlocal_utils.py プロジェクト: zhong110020/models
def load_pretrain_params_from_file(exe, prog, pretrained_file, place):
    """
    The pretrined_file stores ResNet50/101 parameters pretrained on ImageNet.
    However, the conv_weights of Nonlocal model is not the same as that in ResNet50/101 because the 
    input shape is [N, C, T, H, W] and the convolution kernels' shape is [Cout, Cin, Kt, Kh, Kw]. It is
    different from the convolution kernels of ResNet whose shape is typically [Cout, Cin, Kh, Kw].
    So it is recommendated to treat conv_weights specifically.
    The process is as following:
      1, check the params that will be loaded, those with the same name in the target program and pretrained_file. 
         These params will be called common params in this function.
      2, load params from the pretrained_file into a state dict, named file_state_dict in this method.
      3, get the value of common params in the file_state_dict, treat the convolution parameters specifically.
      4, set the value to params in the target program
    """

    logger.info('load pretrained params from {}'.format(pretrained_file))
    if os.path.isdir(pretrained_file):
        # get params' list in prog
        param_list = filter(is_parameter, prog.list_vars())
        param_name_list = []
        param_shape_dict = {}
        for p in param_list:
            param_name_list.append(p.name)
            param_shape_dict[p.name] = p.shape

        # get all params' names in pretrained_file
        param_name_from_file = os.listdir(pretrained_file)
        # get common params of prog and pretrained_file
        # only those common params will be loaded from pretrained_file into prog
        common_names = get_common_names(param_name_list, param_name_from_file)

        file_state_dict = fluid.load_program_state(pretrained_file)
        pretrain_state_dict = {}
        for name in common_names:
            common_array = file_state_dict[name]
            param_shape = param_shape_dict[name]
            if len(param_shape) == 5:
                # transform the loaded conv weights into the format of [Cout, Cin, Kt, Kh, Kw]
                num_inflate = param_shape[2]
                pretrain_state_dict[name] = np.stack(
                    [common_array] * num_inflate, axis=2) / float(num_inflate)
                logger.info("load inflated({}) param {}".format(num_inflate,
                                                                name))
            else:
                pretrain_state_dict[name] = common_array
                logger.info("load param {}".format(name))

        fluid.set_program_state(prog, pretrain_state_dict)
    else:
        raise TypeError(
            "pretrained file {} is not in a directory, not suitable to load params".
            format(pretrained_file))
コード例 #5
0
 def _load_program(self, dir, predicate_fn=None):
     save_path = os.path.join(dir, 'ckpt')
     if not os.path.exists(save_path + '.pdparams'):
         try:
             log.warn('failed to load model, try old-styled saver')
             super(SaverV2, self)._load_program(dir,
                                                predicate_fn=predicate_fn)
         except F.core.EnforceNotMet as e:
             log.exception(e)
             raise RuntimeError(
                 'can not load model from %s, is this a textone checkpoint?'
                 % dir)
     else:
         sd = F.load_program_state(save_path)
         F.set_program_state(self._program.train_program, sd)
コード例 #6
0
ファイル: tsm.py プロジェクト: zhong110020/models
    def load_pretrain_params(self, exe, pretrain, prog, place):
        def is_parameter(var):
            return isinstance(
                var, fluid.framework.Parameter) and (not ("fc_0" in var.name))

        logger.info("Load pretrain weights from {}, exclude fc layer.".format(
            pretrain))

        state_dict = fluid.load_program_state(pretrain)
        dict_keys = list(state_dict.keys())
        for name in dict_keys:
            if "fc_0" in name:
                del state_dict[name]
                print('Delete {} from pretrained parameters. Do not load it'.
                      format(name))
        fluid.set_program_state(prog, state_dict)
コード例 #7
0
ファイル: tsn.py プロジェクト: zhong110020/models
    def load_pretrain_params(self, exe, pretrain, prog):
        def is_parameter(var):
            return isinstance(var, fluid.framework.Parameter)

        logger.info("Load pretrain weights from {}, exclude fc layer.".format(
            pretrain))

        print("===pretrain===", pretrain)
        state_dict = fluid.load_program_state(pretrain)
        dict_keys = list(state_dict.keys())
        # remove fc layer when pretrain, because the number of classes in final fc may not match
        for name in dict_keys:
            if "fc_0" in name:
                del state_dict[name]
                print('Delete {} from pretrained parameters. Do not load it'.
                      format(name))
        fluid.set_program_state(prog, state_dict)
コード例 #8
0
ファイル: model.py プロジェクト: zhong110020/models
 def load_pretrain_params(self, exe, pretrain, prog):
     logger.info("Load pretrain weights from {}".format(pretrain))
     state_dict = fluid.load_program_state(pretrain)
     fluid.set_program_state(prog, state_dict)
コード例 #9
0
import paddle
import paddle.fluid as fluid

paddle.enable_static()

x = fluid.data(name="x", shape=[10, 10], dtype='float32')
y = fluid.layers.fc(x, 10)
z = fluid.layers.fc(y, 10)

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

fluid.save(prog, "./temp")
program_state = fluid.load_program_state("./temp")

fluid.set_program_state(prog, program_state)
コード例 #10
0
def eval():

    data_path = DatasetPath('val')
    test_list = data_path.get_file_list()

    image_shape = [3, cfg.TEST.max_size, cfg.TEST.max_size]
    class_nums = cfg.class_num
    devices = os.getenv("CUDA_VISIBLE_DEVICES") or ""
    devices_num = len(devices.split(","))
    total_batch_size = devices_num * cfg.TRAIN.im_per_batch
    cocoGt = COCO(test_list)
    num_id_to_cat_id_map = {i + 1: v for i, v in enumerate(cocoGt.getCatIds())}
    category_ids = cocoGt.getCatIds()
    label_list = {
        item['id']: item['name']
        for item in cocoGt.loadCats(category_ids)
    }
    label_list[0] = ['background']

    model = model_builder.RCNN(
        add_conv_body_func=resnet.add_ResNet50_conv4_body,
        add_roi_box_head_func=resnet.add_ResNet_roi_conv5_head,
        use_pyreader=False,
        mode='val')
    model.build_model(image_shape)
    pred_boxes = model.eval_bbox_out()
    if cfg.MASK_ON:
        masks = model.eval_mask_out()
    place = fluid.CUDAPlace(0) if cfg.use_gpu else fluid.CPUPlace()
    exe = fluid.Executor(place)
    prog = fluid.default_startup_program()
    exe.run(prog)
    # yapf: disable
    if cfg.pretrained_model:
        #def if_exist(var):
        #    return os.path.exists(os.path.join(cfg.pretrained_model, var.name))
        #fluid.io.load_vars(exe, cfg.pretrained_model, predicate=if_exist)
        para_state_dict, _ = fluid.load_dygraph( "model_final")
        new_dict = {}
        for k,v in para_state_dict.items():
            if "conv2d" in k:
                new_k = k.split('.')[1]
            elif 'linear' in k:
                new_k = k.split('.')[1]
            elif 'conv2dtranspose' in k:
                new_k = k.split('.')[1]
            else:
                new_k = k
            new_dict[new_k] = v.numpy()
        fluid.set_program_state(prog, new_dict)
    # yapf: enable
    test_reader = reader.test(total_batch_size)
    feeder = fluid.DataFeeder(place=place, feed_list=model.feeds())

    dts_res = []
    segms_res = []
    if cfg.MASK_ON:
        fetch_list = [pred_boxes, masks]
    else:
        fetch_list = [pred_boxes]
    eval_start = time.time()
    for batch_id, batch_data in enumerate(test_reader()):
        start = time.time()
        im_info = []
        for data in batch_data:
            im_info.append(data[1])
        results = exe.run(fetch_list=[v.name for v in fetch_list],
                          feed=feeder.feed(batch_data),
                          return_numpy=False)

        pred_boxes_v = results[0]
        if cfg.MASK_ON:
            masks_v = results[1]

        new_lod = pred_boxes_v.lod()
        nmsed_out = pred_boxes_v

        dts_res += get_dt_res(total_batch_size, new_lod[0], nmsed_out,
                              batch_data, num_id_to_cat_id_map)

        if cfg.MASK_ON and np.array(masks_v).shape != (1, 1):
            segms_out = segm_results(nmsed_out, masks_v, im_info)
            segms_res += get_segms_res(total_batch_size, new_lod[0], segms_out,
                                       batch_data, num_id_to_cat_id_map)
        end = time.time()
        print('batch id: {}, time: {}'.format(batch_id, end - start))
    eval_end = time.time()
    total_time = eval_end - eval_start
    print('average time of eval is: {}'.format(total_time / (batch_id + 1)))
    assert len(dts_res) > 0, "The number of valid bbox detected is zero.\n \
        Please use reasonable model and check input data."

    if cfg.MASK_ON:
        assert len(
            segms_res) > 0, "The number of valid mask detected is zero.\n \
            Please use reasonable model and check input data."

    with io.open("detection_bbox_result.json", 'w') as outfile:
        encode_func = unicode if six.PY2 else str
        outfile.write(encode_func(json.dumps(dts_res)))
    print("start evaluate bbox using coco api")
    cocoDt = cocoGt.loadRes("detection_bbox_result.json")
    cocoEval = COCOeval(cocoGt, cocoDt, 'bbox')
    cocoEval.evaluate()
    cocoEval.accumulate()
    cocoEval.summarize()

    if cfg.MASK_ON:
        with io.open("detection_segms_result.json", 'w') as outfile:
            encode_func = unicode if six.PY2 else str
            outfile.write(encode_func(json.dumps(segms_res)))
        print("start evaluate mask using coco api")
        cocoDt = cocoGt.loadRes("detection_segms_result.json")
        cocoEval = COCOeval(cocoGt, cocoDt, 'segm')
        cocoEval.evaluate()
        cocoEval.accumulate()
        cocoEval.summarize()
コード例 #11
0
    def test_ptb_rnn_cpu_float32(self):
        seed = 90
        hidden_size = 10
        vocab_size = 1000
        num_layers = 1
        num_steps = 3
        init_scale = 0.1
        batch_size = 4
        batch_num = 200

        with new_program_scope():
            fluid.default_startup_program().random_seed = seed
            fluid.default_main_program().random_seed = seed
            ptb_model = PtbModel("ptb_model",
                                 hidden_size=hidden_size,
                                 vocab_size=vocab_size,
                                 num_layers=num_layers,
                                 num_steps=num_steps,
                                 init_scale=init_scale)

            place = fluid.CPUPlace(
            ) if not core.is_compiled_with_cuda() else fluid.CUDAPlace(0)
            exe = fluid.Executor(place)
            sgd = Adam(learning_rate=1e-3)
            x = fluid.layers.data(name="x",
                                  shape=[-1, num_steps, 1],
                                  dtype='int64')
            y = fluid.layers.data(name="y", shape=[-1, 1], dtype='float32')
            init_hidden = fluid.layers.data(name="init_hidden",
                                            shape=[1],
                                            dtype='float32')
            init_cell = fluid.layers.data(name="init_cell",
                                          shape=[1],
                                          dtype='float32')

            static_loss, static_last_hidden, static_last_cell = ptb_model(
                x, y, init_hidden, init_cell)

            test_program = fluid.default_main_program().clone(for_test=True)

            add_1 = fluid.layers.fc(static_last_hidden,
                                    size=hidden_size,
                                    num_flatten_dims=2,
                                    bias_attr=False)

            sgd.minimize(static_loss)
            static_param_updated = dict()
            static_param_init = dict()

            out = exe.run(framework.default_startup_program())

            static_loss_value = None
            static_last_cell_value = None
            static_last_hidden_value = None
            for i in range(batch_num):
                x_data = np.arange(12).reshape(4, 3).astype('int64')
                y_data = np.arange(1, 13).reshape(4, 3).astype('int64')
                x_data = x_data.reshape((-1, num_steps, 1))
                y_data = y_data.reshape((-1, 1))
                init_hidden_data = np.zeros(
                    (num_layers, batch_size, hidden_size), dtype='float32')
                init_cell_data = np.zeros(
                    (num_layers, batch_size, hidden_size), dtype='float32')
                fetch_list = [
                    static_loss, static_last_hidden, static_last_cell
                ]
                out = exe.run(fluid.default_main_program(),
                              feed={
                                  "x": x_data,
                                  "y": y_data,
                                  "init_hidden": init_hidden_data,
                                  "init_cell": init_cell_data
                              },
                              fetch_list=fetch_list)
                static_loss_value = out[0]
                static_last_hidden_value = out[1]
                static_last_cell_value = out[2]

            # get value before save
            main_program = framework.default_main_program()
            base_map = {}
            for var in main_program.list_vars():
                if isinstance(var, framework.Parameter) or var.persistable:
                    t = np.array(fluid.global_scope().find_var(
                        var.name).get_tensor())
                    # make sure all the paramerter or optimzier var have been update
                    self.assertTrue(np.sum(np.abs(t)) != 0)
                    base_map[var.name] = t

            fluid.save(main_program, "./test_1")

            # set var to zero
            for var in main_program.list_vars():
                if isinstance(var, framework.Parameter) or var.persistable:
                    ten = fluid.global_scope().find_var(var.name).get_tensor()
                    ten.set(np.zeros_like(np.array(ten)), place)

                    new_t = np.array(fluid.global_scope().find_var(
                        var.name).get_tensor())
                    # make sure all the paramerter or optimzier var have been set to zero
                    self.assertTrue(np.sum(np.abs(new_t)) == 0)

            #fluid.load(test_program, "./test_1", None )
            program_state = fluid.load_program_state("./test_1")
            fluid.set_program_state(test_program, program_state)

            for var in test_program.list_vars():
                if isinstance(var, framework.Parameter) or var.persistable:
                    print(var.name)
                    new_t = np.array(fluid.global_scope().find_var(
                        var.name).get_tensor())
                    base_t = base_map[var.name]
                    self.assertTrue(np.array_equal(new_t, base_t))
コード例 #12
0
    def test_large_parameters_static_save(self):
        # enable static mode
        paddle.enable_static()
        with new_program_scope():
            # create network
            x = paddle.static.data(name="static_save_load_large_x",
                                   shape=[None, 10],
                                   dtype='float32')
            z = paddle.static.nn.fc(x, LARGE_PARAM, bias_attr=False)
            place = paddle.CPUPlace()
            exe = paddle.static.Executor(place)
            exe.run(paddle.static.default_startup_program())
            prog = paddle.static.default_main_program()

            base_map = {}
            for var in prog.list_vars():
                if isinstance(var, framework.Parameter) or var.persistable:
                    t = np.array(fluid.global_scope().find_var(
                        var.name).get_tensor())
                    # make sure all the paramerter or optimizer var have been update
                    self.assertTrue(np.sum(np.abs(t)) != 0)
                    base_map[var.name] = t

            path = os.path.join("test_static_save_load_large_param",
                                "static_save")
            protocol = 4
            paddle.fluid.save(prog, path, pickle_protocol=protocol)
            # set var to zero
            for var in prog.list_vars():
                if isinstance(var, framework.Parameter) or var.persistable:
                    ten = fluid.global_scope().find_var(var.name).get_tensor()
                    ten.set(np.zeros_like(np.array(ten)), place)

                    new_t = np.array(fluid.global_scope().find_var(
                        var.name).get_tensor())
                    self.assertTrue(np.sum(np.abs(new_t)) == 0)

            paddle.fluid.load(prog, path)

            for var in prog.list_vars():
                if isinstance(var, framework.Parameter) or var.persistable:
                    new_t = np.array(fluid.global_scope().find_var(
                        var.name).get_tensor())
                    base_t = base_map[var.name]
                    self.assertTrue(np.array_equal(new_t, base_t))

            # set var to zero
            for var in prog.list_vars():
                if isinstance(var, framework.Parameter) or var.persistable:
                    ten = fluid.global_scope().find_var(var.name).get_tensor()
                    ten.set(np.zeros_like(np.array(ten)), place)

                    new_t = np.array(fluid.global_scope().find_var(
                        var.name).get_tensor())
                    self.assertTrue(np.sum(np.abs(new_t)) == 0)

            program_state = fluid.load_program_state(path)
            fluid.set_program_state(prog, program_state)
            for var in prog.list_vars():
                if isinstance(var, framework.Parameter) or var.persistable:
                    new_t = np.array(fluid.global_scope().find_var(
                        var.name).get_tensor())
                    base_t = base_map[var.name]
                    self.assertTrue(np.array_equal(new_t, base_t))