def __init__(self, estimator, input_fn, steps=None, hooks=None, name=None, every_n_iter=100): """Initializes a `InMemoryEvaluatorHook`. Args: estimator: A `tf.estimator.Estimator` instance to call evaluate. input_fn: Equivalent to the `input_fn` arg to `estimator.evaluate`. A function that constructs the input data for evaluation. See @{$premade_estimators#create_input_functions} for more information. The function should construct and return one of the following: * A 'tf.data.Dataset' object: Outputs of `Dataset` object must be a tuple (features, labels) with same constraints as below. * A tuple (features, labels): Where `features` is a `Tensor` or a dictionary of string feature name to `Tensor` and `labels` is a `Tensor` or a dictionary of string label name to `Tensor`. Both `features` and `labels` are consumed by `model_fn`. They should satisfy the expectation of `model_fn` from inputs. steps: Equivalent to the `steps` arg to `estimator.evaluate`. Number of steps for which to evaluate model. If `None`, evaluates until `input_fn` raises an end-of-input exception. hooks: Equivalent to the `hooks` arg to `estimator.evaluate`. List of `SessionRunHook` subclass instances. Used for callbacks inside the evaluation call. name: Equivalent to the `name` arg to `estimator.evaluate`. Name of the evaluation if user needs to run multiple evaluations on different data sets, such as on training data vs test data. Metrics for different evaluations are saved in separate folders, and appear separately in tensorboard. every_n_iter: `int`, runs the evaluator once every N training iteration. Raises: ValueError: if `every_n_iter` is non-positive or it's not a single machine training """ if every_n_iter is None or every_n_iter <= 0: raise ValueError('invalid every_n_iter=%s.' % every_n_iter) if (estimator.config.num_ps_replicas > 0 or estimator.config.num_worker_replicas > 1): raise ValueError( 'InMemoryEvaluator supports only single machine (aka Local) setting.') self._estimator = estimator self._input_fn = input_fn self._steps = steps self._name = name self._every_n_iter = every_n_iter self._eval_dir = os.path.join(self._estimator.model_dir, 'eval' if not name else 'eval_' + name) self._graph = None self._hooks = estimator_lib._check_hooks_type(hooks) self._hooks.extend(self._estimator._convert_eval_steps_to_hooks(steps)) self._timer = training.SecondOrStepTimer(every_steps=every_n_iter)
def __prepare_interactive_model(self): """Create monitored session and generator that reads from the terminal and yields "interactive inputs". Due to temporary limitations in tf.learn, if we don't want to reload the whole graph, then we are stuck encoding all of the input as one fixed-size numpy array. We yield int32 arrays with shape [const_array_size]. The format is: [num_samples, decode_length, len(input ids), <input ids>, <padding>] Raises: ValueError: Could not find a trained model in model_dir. ValueError: if batch length of predictions are not same. """ word = self.__get_word() self.first_ex = True self.decode_word(word) self.first_ex = False prob_choice = np.array(0).astype(np.int32) def input_fn(): """Input function returning features which is a dictionary of string feature name to `Tensor` or `SparseTensor`. If it returns a tuple, first item is extracted as features. Prediction continues until `input_fn` raises an end-of-input exception (`OutOfRangeError` or `StopIteration`).""" gen_fn = make_input_fn(self.inputs, prob_choice) example = gen_fn() example = decoding._interactive_input_tensor_to_features_dict(example, self.estimator.params) return example self.input_fn = input_fn if os.path.exists(self.frozen_graph_filename): return with estimator_lib.ops.Graph().as_default() as g: self.features = self.estimator._get_features_from_input_fn( input_fn, estimator_lib.model_fn_lib.ModeKeys.PREDICT) # List of `SessionRunHook` subclass instances. Used for callbacks inside # the prediction call. hooks = estimator_lib._check_hooks_type(None) # Check that model has been trained. # Path of a specific checkpoint to predict. The latest checkpoint # in `model_dir` is used checkpoint_path = estimator_lib.saver.latest_checkpoint( self.params.model_dir) if not checkpoint_path: raise ValueError('Could not find trained model in model_dir: {}.' .format(self.params.model_dir)) estimator_lib.random_seed.set_random_seed( self.estimator._config.tf_random_seed) self.estimator._create_and_assert_global_step(g) self.estimator_spec = self.estimator._call_model_fn( self.features, None, estimator_lib.model_fn_lib.ModeKeys.PREDICT, self.estimator.config) self.mon_sess = estimator_lib.training.MonitoredSession( session_creator=estimator_lib.training.ChiefSessionCreator( checkpoint_filename_with_path=checkpoint_path, scaffold=self.estimator_spec.scaffold, config=self.estimator._session_config), hooks=hooks) pronunciations = self.decode_word(word) print("Pronunciations: {}".format(pronunciations))
def __prepare_interactive_model(self): """Create monitored session and generator that reads from the terminal and yields "interactive inputs". Due to temporary limitations in tf.learn, if we don't want to reload the whole graph, then we are stuck encoding all of the input as one fixed-size numpy array. We yield int32 arrays with shape [const_array_size]. The format is: [num_samples, decode_length, len(input ids), <input ids>, <padding>] Raises: ValueError: Could not find a trained model in model_dir. ValueError: if batch length of predictions are not same. """ def input_fn(): """Input function returning features which is a dictionary of string feature name to `Tensor` or `SparseTensor`. If it returns a tuple, first item is extracted as features. Prediction continues until `input_fn` raises an end-of-input exception (`OutOfRangeError` or `StopIteration`).""" gen_fn = decoding.make_input_fn_from_generator( self.__interactive_input_fn()) example = gen_fn() example = decoding._interactive_input_tensor_to_features_dict( example, self.hparams) return example self.res_iter = self.estimator.predict(input_fn) if os.path.exists(self.frozen_graph_filename): return # List of `SessionRunHook` subclass instances. Used for callbacks inside # the prediction call. hooks = estimator_lib._check_hooks_type(None) # Check that model has been trained. # Path of a specific checkpoint to predict. The latest checkpoint # in `model_dir` is used checkpoint_path = estimator_lib.saver.latest_checkpoint( self.params.model_dir) if not checkpoint_path: raise ValueError('Could not find trained model in model_dir: {}.' .format(self.params.model_dir)) with estimator_lib.ops.Graph().as_default() as graph: estimator_lib.random_seed.set_random_seed( self.estimator._config.tf_random_seed) self.estimator._create_and_assert_global_step(graph) self.features, input_hooks = self.estimator._get_features_from_input_fn( input_fn, estimator_lib.model_fn_lib.ModeKeys.PREDICT) self.estimator_spec = self.estimator._call_model_fn( self.features, None, estimator_lib.model_fn_lib.ModeKeys.PREDICT, self.estimator.config) try: self.mon_sess = estimator_lib.training.MonitoredSession( session_creator=estimator_lib.training.ChiefSessionCreator( checkpoint_filename_with_path=checkpoint_path, scaffold=self.estimator_spec.scaffold, config=self.estimator._session_config), hooks=hooks) except: raise StandardError("Invalid model in {}".format(self.params.model_dir))
def predict_with_feeddict(self, input_fn, predict_keys=None, hooks=None, checkpoint_path=None, export_checkpoint_path=None, feed=None): with estimator_lib.context.graph_mode(): hooks = estimator_lib._check_hooks_type(hooks) # Check that model has been trained. # Check that model has been trained. if not checkpoint_path: checkpoint_path = estimator_lib.saver.latest_checkpoint( self._model_dir) if not checkpoint_path: estimator_lib.logging.info( 'Could not find trained model in model_dir: {}, running ' 'initialization to predict.'.format(self._model_dir)) if export_checkpoint_path: export_checkpoint_path = estimator_lib.saver.latest_checkpoint( export_checkpoint_path) with estimator_lib.ops.Graph().as_default() as g: estimator_lib.random_seed.set_random_seed( self._config.tf_random_seed) # self._create_and_assert_global_step(g) features, input_hooks = self._get_features_from_input_fn( input_fn, estimator_lib.model_fn_lib.ModeKeys.PREDICT) estimator_spec = self._call_model_fn( features, None, estimator_lib.model_fn_lib.ModeKeys.PREDICT, self.config) # A Hack! feedd = { features['input_ids']: [feed['input_ids']], features['input_mask']: [feed['input_mask']], features['segment_ids']: [feed['segment_ids']] } # Call to warm_start has to be after model_fn is called. self._maybe_warm_start(checkpoint_path) predictions = self._extract_keys(estimator_spec.predictions, predict_keys) all_hooks = list(input_hooks) all_hooks.extend(hooks) all_hooks.extend(list(estimator_spec.prediction_hooks or [])) with estimator_lib.training.MonitoredSession( session_creator=estimator_lib.training. ChiefSessionCreator( checkpoint_filename_with_path=checkpoint_path, master=self._config.master, scaffold=estimator_spec.scaffold, config=self._session_config), hooks=all_hooks) as mon_sess: preds_evaluated = mon_sess.run(predictions, feed_dict=feedd) # return preds_evaluated input_graph_name = "input_graph.pb" output_graph_name = "output_graph.pb" export_dir = 'export' tf.train.write_graph(mon_sess.graph, export_dir, input_graph_name) tf.logging.info("Write graph at %s." % os.path.join(export_dir, input_graph_name)) export_graph = tf.Graph() with export_graph.as_default(): freeze_graph.freeze_graph( input_graph=os.path.join(export_dir, input_graph_name), input_saver="", input_binary=False, input_checkpoint=export_checkpoint_path, output_node_names='loss/Softmax', restore_op_name="", filename_tensor_name="", output_graph=os.path.join(export_dir, output_graph_name), clear_devices=True, initializer_nodes=None, variable_names_blacklist="") tf.logging.info( "Export model at %s." % os.path.join(export_dir, output_graph_name)) return preds_evaluated