def load_program_text(model_filename): """load program from human-readable text file""" with open(model_filename, "r") as f: program_desc_text = f.read() prog_desc = framework_pb2.ProgramDesc() text_format.Merge(program_desc_text, prog_desc) return Program.parse_from_string(prog_desc.SerializeToString())
def save_inference_model(self, executor, dirname, feeded_var_names, target_vars, main_program=None, export_for_deployment=True): """ Prune the given `main_program` to build a new program especially for inference, and then save it and all related parameters to given `dirname` by the `executor`. """ if self._inner_mode == PSMode.PSLIB: raise NotImplementedError("add implement later") if isinstance(executor, ParallelExecutor): raise TypeError( "in fleet.save_inference_model() function, executor must be as Executor type, ParallelExecutor is not allowed" ) if not isinstance(executor, Executor): raise TypeError( "in fleet.save_inference_model() function, executor must be as Executor type" ) # Todo(MrChengmo): support recv&save GPU-Kernel for ps-gpu model save if not isinstance(executor.place, fluid.CPUPlace): save_executor = Executor(fluid.CPUPlace()) else: save_executor = executor if main_program is not None: if isinstance(main_program, CompiledProgram): raise TypeError( "in fleet.save_inference_model() function, main_program must be as Program type, CompiledProgram is not allowed" ) fluid.io.save_inference_model(dirname, feeded_var_names, target_vars, executor, main_program, None, None, export_for_deployment) else: fluid.io.save_inference_model(dirname, feeded_var_names, target_vars, executor, self._origin_main_program, None, None, export_for_deployment, True) model_basename = "__model__" model_filename = os.path.join(dirname, model_basename) with open(model_filename, "rb") as f: program_desc_str = f.read() program = Program.parse_from_string(program_desc_str) program._copy_dist_param_info_from(self.main_program) self.save_persistables(executor, dirname, program)
def _ps_inference_save_inference_model(self, executor, dirname, feeded_var_names, target_vars, main_program=None, export_for_deployment=True): """ Prune the given `main_program` to build a new program especially for inference, and then save it and all related parameters to given `dirname` by the `executor`. """ if isinstance(executor, ParallelExecutor): raise TypeError( "in fleet.save_inference_model() function, executor must be as Executor type, ParallelExecutor is not allowed" ) if not isinstance(executor, Executor): raise TypeError( "in fleet.save_inference_model() function, executor must be as Executor type" ) if main_program is not None: if isinstance(main_program, CompiledProgram): raise TypeError( "in fleet.save_inference_model() function, main_program must be as Program type, CompiledProgram is not allowed" ) fluid.io.save_inference_model(dirname, feeded_var_names, target_vars, executor, main_program, None, None, export_for_deployment) else: fluid.io.save_inference_model(dirname, feeded_var_names, target_vars, executor, self.origin_main_program, None, None, export_for_deployment, True) model_basename = "__model__" model_filename = os.path.join(dirname, model_basename) with open(model_filename, "rb") as f: program_desc_str = f.read() program = Program.parse_from_string(program_desc_str) program._copy_dist_param_info_from(fluid.default_main_program()) self._ps_inference_save_persistables(executor, dirname, program, mode=0)
def load_inference_model(dirname, executor, model_filename=None, params_filename=None): """ Load inference model from a directory :param dirname: directory path :param executor: executor that load inference model :param model_filename: The name of file to load inference program. If not specified, default filename `__model__` will be used. :param params_filename: The name of file to load parameters. It is used for the case that all parameters are saved in a single binary file. If not specified, parameters are considered saved in separate files. :return: [program, feed_target_names, fetch_targets] program: program especially for inference. feed_target_names: Names of variables that need to feed data fetch_targets: Variables from which we can get inference results. """ if not os.path.isdir(dirname): raise ValueError("There is no directory named '%s'", dirname) if model_filename is not None: model_filename = os.path.basename(model_filename) else: model_filename = "__model__" model_filename = os.path.join(dirname, model_filename) if params_filename is not None: params_filename = os.path.basename(params_filename) with open(model_filename, "rb") as f: program_desc_str = f.read() program = Program.parse_from_string(program_desc_str) load_persistables(executor, dirname, program, params_filename) feed_target_names = program.desc.get_feed_target_names() fetch_target_names = program.desc.get_fetch_target_names() fetch_targets = [ program.global_block().var(name) for name in fetch_target_names ] return [program, feed_target_names, fetch_targets]
def test_parse_program_from_string(self): prog = Program() x = prog.global_block().create_var( name='X', shape=[1000, 784], dtype='float32') y = prog.global_block().create_var( name='Y', shape=[784, 100], dtype='float32') out = prog.global_block().create_var(name='Out', dtype='float32') prog.global_block().append_op( type="mul", inputs={'X': [x], 'Y': [y]}, outputs={'Out': [out]}) binary_str = prog.desc.serialize_to_string() prog_restored = Program.parse_from_string(binary_str) print(prog) print(prog_restored)
def test_parse_program_from_string(self): prog = Program() x = prog.global_block().create_var(name='X', shape=[1000, 784], dtype='float32') y = prog.global_block().create_var(name='Y', shape=[784, 100], dtype='float32') out = prog.global_block().create_var(name='Out', dtype='float32') prog.global_block().append_op(type="mul", inputs={ 'X': [x], 'Y': [y] }, outputs={'Out': [out]}) binary_str = prog.desc.serialize_to_string() prog_restored = Program.parse_from_string(binary_str) print(prog) print(prog_restored)
def load(path, **configs): ''' Load an object can be used in paddle from specified path. .. note:: Now supports loading ``state_dict`` of Layer/Optimizer, Tensor and nested structure containing Tensor, Program. .. note:: In order to use the model parameters saved by paddle more efficiently, ``paddle.load`` supports loading ``state_dict`` of Layer from the result of other save APIs except ``paddle.save`` , but the argument ``path`` format is different: 1. loading from ``paddle.static.save`` or ``paddle.Model().save(training=True)`` , ``path`` needs to be a complete file name, such as ``model.pdparams`` or ``model.pdopt`` ; 2. loading from ``paddle.jit.save`` or ``paddle.static.save_inference_model`` or ``paddle.Model().save(training=False)`` , ``path`` need to be a file prefix, such as ``model/mnist``, and ``paddle.load`` will get information from ``mnist.pdmodel`` and ``mnist.pdiparams`` ; 3. loading from paddle 1.x APIs ``paddle.fluid.io.save_inference_model`` or ``paddle.fluid.io.save_params/save_persistables`` , ``path`` need to be a directory, such as ``model`` and model is a directory. .. note:: If you load ``state_dict`` from the saved result of static mode API such as ``paddle.static.save`` or ``paddle.static.save_inference_model`` , the structured variable name in dynamic mode will cannot be restored. You need to set the argument ``use_structured_name=False`` when using ``Layer.set_state_dict`` later. Args: path(str|BytesIO) : The path/buffer to load the target object. Generally, the path is the target file path. When loading state_dict from the saved result of the API used to save the inference model, the path may be a file prefix or directory. **configs (dict, optional): other load configuration options for compatibility. We do not recommend using these configurations, they may be removed in the future. If not necessary, DO NOT use them. Default None. The following options are currently supported: (1) model_filename (str): The inference model file name of the paddle 1.x ``save_inference_model`` save format. Default file name is :code:`__model__` . (2) params_filename (str): The persistable variables file name of the paddle 1.x ``save_inference_model`` save format. No default file name, save variables separately by default. (3) return_numpy(bool): If specified as True, return tensor as numpy.ndarray, otherwise return tensor as paddle.Tensor. Default False. Returns: Object(Object): a target object can be used in paddle Examples: .. code-block:: python # example 1: dynamic graph import paddle emb = paddle.nn.Embedding(10, 10) layer_state_dict = emb.state_dict() # save state_dict of emb paddle.save(layer_state_dict, "emb.pdparams") scheduler = paddle.optimizer.lr.NoamDecay( d_model=0.01, warmup_steps=100, verbose=True) adam = paddle.optimizer.Adam( learning_rate=scheduler, parameters=emb.parameters()) opt_state_dict = adam.state_dict() # save state_dict of optimizer paddle.save(opt_state_dict, "adam.pdopt") # save weight of emb paddle.save(emb.weight, "emb.weight.pdtensor") # load state_dict of emb load_layer_state_dict = paddle.load("emb.pdparams") # load state_dict of optimizer load_opt_state_dict = paddle.load("adam.pdopt") # load weight of emb load_weight = paddle.load("emb.weight.pdtensor") # example 2: Load multiple state_dict at the same time from paddle import nn from paddle.optimizer import Adam layer = paddle.nn.Linear(3, 4) adam = Adam(learning_rate=0.001, parameters=layer.parameters()) obj = {'model': layer.state_dict(), 'opt': adam.state_dict(), 'epoch': 100} path = 'example/model.pdparams' paddle.save(obj, path) obj_load = paddle.load(path) # example 3: static graph import paddle import paddle.static as static paddle.enable_static() # create network x = paddle.static.data(name="x", shape=[None, 224], dtype='float32') z = paddle.static.nn.fc(x, 10) place = paddle.CPUPlace() exe = paddle.static.Executor(place) exe.run(paddle.static.default_startup_program()) prog = paddle.static.default_main_program() for var in prog.list_vars(): if list(var.shape) == [224, 10]: tensor = var.get_value() break # save/load tensor path_tensor = 'temp/tensor.pdtensor' paddle.save(tensor, path_tensor) load_tensor = paddle.load(path_tensor) # save/load state_dict path_state_dict = 'temp/model.pdparams' paddle.save(prog.state_dict("param"), path_tensor) load_state_dict = paddle.load(path_tensor) # example 4: load program import paddle paddle.enable_static() data = paddle.static.data( name='x_static_save', shape=(None, 224), dtype='float32') y_static = z = paddle.static.nn.fc(data, 10) main_program = paddle.static.default_main_program() path = "example/main_program.pdmodel" paddle.save(main_program, path) load_main = paddle.load(path) print(load_main) # example 5: save object to memory from io import BytesIO import paddle from paddle.nn import Linear paddle.disable_static() linear = Linear(5, 10) state_dict = linear.state_dict() byio = BytesIO() paddle.save(state_dict, byio) tensor = paddle.randn([2, 3], dtype='float32') paddle.save(tensor, byio) byio.seek(0) # load state_dict dict_load = paddle.load(byio) ''' if _is_memory_buffer(path) or os.path.isfile(path): config = _parse_load_config(configs) exception_type = pickle.UnpicklingError try: with _open_file_buffer(path, 'rb') as f: # When value of dict is lager than 4GB ,there is a Bug on 'MAC python3' if _is_file_path( path ) and sys.platform == 'darwin' and sys.version_info.major == 3: load_result = _pickle_loads_mac(path, f) else: load_result = pickle.load(f, encoding='latin1') # TODO(weixin):If `obj` is any object, the judgment condition should be more precise. if isinstance(load_result, dict): load_result = _pack_loaded_dict(load_result) # paddle2.0: paddle.save/load if "StructuredToParameterName@@" in load_result: for key in load_result["StructuredToParameterName@@"]: if isinstance(load_result[key], np.ndarray): load_result[key] = _ndarray_to_tensor( load_result[key], config.return_numpy) if not config.keep_name_table and "StructuredToParameterName@@" in load_result: del load_result["StructuredToParameterName@@"] else: # paddle2.1 static.save/load load_result = _parse_load_result(load_result, config.return_numpy) else: load_result = _parse_load_result(load_result, config.return_numpy) except exception_type as msg_pickle: try: tensor, _ = _load_selected_rows(path) return tensor except: try: tensor, _ = _load_lod_tensor(path) if config.return_numpy: return np.array(tensor) else: if _non_static_mode(): return _lod_tensor2varbase(tensor) return tensor except: try: with _open_file_buffer(path, "rb") as f: program_desc_str = f.read() program = Program.parse_from_string( program_desc_str) return program except: raise ValueError( "`paddle.load` can not parse the file:{}.".format( path)) else: load_result = _legacy_load(path, **configs) return load_result
def load_program_binary(model_filename): """load program from binary string file""" with open(model_filename, "rb") as f: program_desc_str = f.read() return Program.parse_from_string(program_desc_str)
def load_inference_model(dirname, executor, model_filename=None, params_filename=None, pserver_endpoints=None): """ Load inference model from a directory Args: dirname(str): The directory path executor(Executor): The executor to run for loading inference model. model_filename(str|None): The name of file to load inference program. If it is None, the default filename '__model__' will be used. Default: None params_filename(str|None): The name of file to load all parameters. It is only used for the case that all parameters were saved in a single binary file. If parameters were saved in separate files, set it as 'None'. pserver_endpoints(list|None): This only need by distributed inference. When use distributed look up table in training, We also need it in inference.The parameter is a list of pserver endpoints. Returns: tuple: The return of this function is a tuple with three elements: (program, feed_target_names, fetch_targets). The `program` is a Program, it's the program for inference. The `feed_target_names` is a list of str, it contains Names of variables that need to feed data in the inference program. The `fetch_targets` is a list of Variable. It contains variables from which we can get inference results. Raises: ValueError: If `dirname` is not a existing directory. Examples: .. code-block:: python exe = fluid.Executor(fluid.CPUPlace()) path = "./infer_model" endpoints = ["127.0.0.1:2023","127.0.0.1:2024"] [inference_program, feed_target_names, fetch_targets] = fluid.io.load_inference_model(dirname=path, executor=exe) results = exe.run(inference_program, feed={feed_target_names[0]: tensor_img}, fetch_list=fetch_targets) # if we need lookup table, we will use: fluid.io.load_inference_model(dirname=path, executor=exe, pserver_endpoints=endpoints) # In this exsample, the inference program was saved in the # "./infer_model/__model__" and parameters were saved in # separate files in ""./infer_model". # After getting inference program, feed target names and # fetch targets, we can use an Executor to run the inference # program to get the inference result. """ if not os.path.isdir(dirname): raise ValueError("There is no directory named '%s'", dirname) if model_filename is not None: model_filename = os.path.basename(model_filename) else: model_filename = "__model__" model_filename = os.path.join(dirname, model_filename) if params_filename is not None: params_filename = os.path.basename(params_filename) with open(model_filename, "rb") as f: program_desc_str = f.read() program = Program.parse_from_string(program_desc_str) if not core._is_program_version_supported(program._version()): raise ValueError("Unsupported program version: %d\n" % program._version()) # Binary data also need versioning. load_persistables(executor, dirname, program, params_filename) if pserver_endpoints: program = _endpoints_replacement(program, pserver_endpoints) feed_target_names = program.desc.get_feed_target_names() fetch_target_names = program.desc.get_fetch_target_names() fetch_targets = [ program.global_block().var(name) for name in fetch_target_names ] return [program, feed_target_names, fetch_targets]