def load(self, preprocessor_file, json_file, weights_file, custom_objects=None): """load ner application Args: preprocessor_file: path to load preprocessor json_file: path to load model architecture weights_file: path to load model weights custom_objects: Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization. Must provided when using custom layer. """ self.preprocessor = NERPreprocessor.load(preprocessor_file) logging.info('Load preprocessor from {}'.format(preprocessor_file)) custom_objects = custom_objects or {} custom_objects.update(get_custom_objects()) with open(json_file, 'r') as reader: self.model = model_from_json(reader.read(), custom_objects=custom_objects) logging.info('Load model architecture from {}'.format(json_file)) self.model.load_weights(weights_file) logging.info('Load model weight from {}'.format(weights_file)) self.trainer = NERTrainer(self.model, self.preprocessor) self.predictor = NERPredictor(self.model, self.preprocessor)
def load(self, preprocessor_file: str, json_file: str, weights_file: str, custom_objects: Optional[Dict[str, Any]] = None) -> None: """Load ner application from disk. There are 3 things in total that we need to load: 1) preprocessor, which stores the vocabulary and embedding matrix built during pre-processing and helps us prepare feature input for ner model; 2) model architecture, which describes the framework of our ner model; 3) model weights, which stores the value of ner model's parameters. Args: preprocessor_file: path to load preprocessor json_file: path to load model architecture weights_file: path to load model weights custom_objects: Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization. We will automatically add all the custom layers of this project to custom_objects. So you can ignore this argument in most cases unlesss you use your own custom layer. """ self.preprocessor = NERPreprocessor.load(preprocessor_file) logging.info('Load preprocessor from {}'.format(preprocessor_file)) custom_objects = custom_objects or {} custom_objects.update(get_custom_objects()) with open(json_file, 'r') as reader: self.model = tf.keras.models.model_from_json( reader.read(), custom_objects=custom_objects) logging.info('Load model architecture from {}'.format(json_file)) self.model.load_weights(weights_file) logging.info('Load model weight from {}'.format(weights_file)) self.trainer = NERTrainer(self.model, self.preprocessor) self.predictor = NERPredictor(self.model, self.preprocessor)