def _convert_eval_steps_to_hooks(self, steps): if steps is None: return [] if steps <= 0: raise ValueError('Must specify steps > 0, given: {}'.format(steps)) return [evaluation._StopAfterNEvalsHook(num_evals=steps)] # pylint: disable=protected-access
def testEvalOpAndFinalOp(self): checkpoint_dir = os.path.join(self.get_temp_dir(), 'eval_ops_and_final_ops') # Train a model for a single step to get a checkpoint. self._train_model(checkpoint_dir, num_steps=1) checkpoint_path = saver.latest_checkpoint(checkpoint_dir) # Create the model so we have something to restore. inputs = constant_op.constant(self._inputs, dtype=dtypes.float32) logistic_classifier(inputs) num_evals = 5 final_increment = 9.0 my_var = local_variable(0.0, name='MyVar') eval_ops = state_ops.assign_add(my_var, 1.0) final_ops = array_ops.identity(my_var) + final_increment final_hooks = [ evaluation._StopAfterNEvalsHook(num_evals), ] initial_hooks = list(final_hooks) final_ops_values = evaluation._evaluate_once( checkpoint_path=checkpoint_path, eval_ops=eval_ops, final_ops={'value': final_ops}, hooks=final_hooks) self.assertEqual(final_ops_values['value'], num_evals + final_increment) self.assertEqual(initial_hooks, final_hooks)
def testMultiEvalStepIncrements(self): checkpoint_dir = os.path.join(self.get_temp_dir(), 'eval_ops_and_final_ops') # Train a model for a single step to get a checkpoint. self._train_model(checkpoint_dir, num_steps=1) checkpoint_path = saver.latest_checkpoint(checkpoint_dir) # Create the model so we have something to restore. inputs = constant_op.constant(self._inputs, dtype=dtypes.float32) logistic_classifier(inputs) num_evals = 6 my_var = local_variable(0.0, name='MyVar') # In eval ops, we also increase the eval step one more time. eval_ops = [ state_ops.assign_add(my_var, 1.0), state_ops.assign_add(evaluation._get_or_create_eval_step(), 1, use_locking=True) ] expect_eval_update_counts = num_evals // 2 final_ops = array_ops.identity(my_var) final_ops_values = evaluation._evaluate_once( checkpoint_path=checkpoint_path, eval_ops=eval_ops, final_ops={'value': final_ops}, hooks=[ evaluation._StopAfterNEvalsHook(num_evals), ]) self.assertEqual(final_ops_values['value'], expect_eval_update_counts)
def testMultiEvalStepIncrements(self): checkpoint_dir = os.path.join(self.get_temp_dir(), 'eval_ops_and_final_ops') # Train a model for a single step to get a checkpoint. self._train_model(checkpoint_dir, num_steps=1) checkpoint_path = saver.latest_checkpoint(checkpoint_dir) # Create the model so we have something to restore. inputs = constant_op.constant(self._inputs, dtype=dtypes.float32) logistic_classifier(inputs) num_evals = 6 my_var = local_variable(0.0, name='MyVar') # In eval ops, we also increase the eval step one more time. eval_ops = [state_ops.assign_add(my_var, 1.0), state_ops.assign_add( evaluation._get_or_create_eval_step(), 1, use_locking=True)] expect_eval_update_counts = num_evals // 2 final_ops = array_ops.identity(my_var) final_ops_values = evaluation._evaluate_once( checkpoint_path=checkpoint_path, eval_ops=eval_ops, final_ops={'value': final_ops}, hooks=[evaluation._StopAfterNEvalsHook(num_evals),]) self.assertEqual(final_ops_values['value'], expect_eval_update_counts)
def testEvaluatePerfectModel(self): checkpoint_dir = os.path.join(self.get_temp_dir(), 'evaluate_perfect_model_once') # Train a Model to completion: self._train_model(checkpoint_dir, num_steps=300) # Run inputs = constant_op.constant(self._inputs, dtype=dtypes.float32) labels = constant_op.constant(self._labels, dtype=dtypes.float32) logits = logistic_classifier(inputs) predictions = math_ops.round(logits) accuracy, update_op = metrics.accuracy(predictions=predictions, labels=labels) checkpoint_path = saver.latest_checkpoint(checkpoint_dir) final_ops_values = evaluation._evaluate_once( checkpoint_path=checkpoint_path, eval_ops=update_op, final_ops={'accuracy': accuracy}, hooks=[ evaluation._StopAfterNEvalsHook(1), ]) self.assertTrue(final_ops_values['accuracy'] > .99)
def testEvalOpAndFinalOp(self): checkpoint_dir = os.path.join(self.get_temp_dir(), 'eval_ops_and_final_ops') # Train a model for a single step to get a checkpoint. self._train_model(checkpoint_dir, num_steps=1) checkpoint_path = saver.latest_checkpoint(checkpoint_dir) # Create the model so we have something to restore. inputs = constant_op.constant(self._inputs, dtype=dtypes.float32) logistic_classifier(inputs) num_evals = 5 final_increment = 9.0 my_var = local_variable(0.0, name='MyVar') eval_ops = state_ops.assign_add(my_var, 1.0) final_ops = array_ops.identity(my_var) + final_increment final_hooks = [evaluation._StopAfterNEvalsHook(num_evals),] initial_hooks = list(final_hooks) final_ops_values = evaluation._evaluate_once( checkpoint_path=checkpoint_path, eval_ops=eval_ops, final_ops={'value': final_ops}, hooks=final_hooks) self.assertEqual(final_ops_values['value'], num_evals + final_increment) self.assertEqual(initial_hooks, final_hooks)
def testEvaluateWithFiniteInputs(self): checkpoint_dir = os.path.join(self.get_temp_dir(), 'evaluate_with_finite_inputs') # Train a Model to completion: self._train_model(checkpoint_dir, num_steps=300) # Run evaluation. Inputs are fed through input producer for one epoch. all_inputs = constant_op.constant(self._inputs, dtype=dtypes.float32) all_labels = constant_op.constant(self._labels, dtype=dtypes.float32) single_input, single_label = training.slice_input_producer( [all_inputs, all_labels], num_epochs=1) inputs, labels = training.batch([single_input, single_label], batch_size=6, allow_smaller_final_batch=True) logits = logistic_classifier(inputs) predictions = math_ops.round(logits) accuracy, update_op = metrics.accuracy( predictions=predictions, labels=labels) checkpoint_path = saver.latest_checkpoint(checkpoint_dir) final_ops_values = evaluation._evaluate_once( checkpoint_path=checkpoint_path, eval_ops=update_op, final_ops={'accuracy': accuracy, 'eval_steps': evaluation._get_or_create_eval_step()}, hooks=[evaluation._StopAfterNEvalsHook(None),]) self.assertTrue(final_ops_values['accuracy'] > .99) # Runs evaluation for 4 iterations. First 2 evaluate full batch of 6 inputs # each; the 3rd iter evaluates the remaining 4 inputs, and the last one # triggers an error which stops evaluation. self.assertEqual(final_ops_values['eval_steps'], 4)
def evaluate(self, input_fn, steps=None, hooks=None, checkpoint_path=None, name=None): """Evaluates the model given evaluation data input_fn. For each step, calls `input_fn`, which returns one batch of data. Evaluates until: - `steps` batches are processed, or - `input_fn` raises an end-of-input exception (`OutOfRangeError` or `StopIteration`). Args: input_fn: Input function returning a tuple of: features - Dictionary of string feature name to `Tensor` or `SparseTensor`. labels - `Tensor` or dictionary of `Tensor` with labels. steps: Number of steps for which to evaluate model. If `None`, evaluates until `input_fn` raises an end-of-input exception. hooks: List of `SessionRunHook` subclass instances. Used for callbacks inside the evaluation call. checkpoint_path: Path of a specific checkpoint to evaluate. If `None`, the latest checkpoint in `model_dir` is used. name: 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. Returns: A dict containing the evaluation metrics specified in `model_fn` keyed by name, as well as an entry `global_step` which contains the value of the global step for which this evaluation was performed. Raises: ValueError: If `steps <= 0`. ValueError: If no model has been trained, namely `model_dir`, or the given `checkpoint_path` is empty. """ # We need to copy the hook array as we modify it, thus [:]. hooks = hooks[:] if hooks else [] if steps is not None: if steps <= 0: raise ValueError( 'Must specify steps >= 0, given: {}'.format(steps)) hooks.append( evaluation._StopAfterNEvalsHook( # pylint: disable=protected-access num_evals=steps)) return self._evaluate_model(input_fn=input_fn, hooks=hooks, checkpoint_path=checkpoint_path, name=name)
def evaluate(self, input_fn=None, steps=None, hooks=None, checkpoint_path=None, name=None): """Evaluates given model with provided evaluation data. Stop conditions - we evaluate on the given input data until one of the following: - If `steps` is provided, and `steps` batches of size `batch_size` are processed. - If `input_fn` is provided, and it raises an end-of-input exception (`OutOfRangeError` or `StopIteration`). - If `x` is provided, and all items in `x` have been processed. The return value is a dict containing the metrics specified in `metrics`, as well as an entry `global_step` which contains the value of the global step for which this evaluation was performed. Args: input_fn: Input function returning a tuple of: features - Dictionary of string feature name to `Tensor` or `Tensor`. labels - `Tensor` or dictionary of `Tensor` with labels. If `steps` is not provided, this should raise `OutOfRangeError` or `StopIteration` after the desired amount of data (e.g., one epoch) has been provided. See "Stop conditions" above for specifics. steps: Number of steps for which to evaluate model. If `None`, evaluate until `x` is consumed or `input_fn` raises an end-of-input exception. See "Stop conditions" above for specifics. name: Name of the evaluation if user needs to run multiple evaluations on different data sets, such as on training data vs test data. checkpoint_path: Path of a specific checkpoint to evaluate. If `None`, the latest checkpoint in `model_dir` is used. hooks: List of `SessionRunHook` subclass instances. Used for callbacks inside the evaluation call. Raises: ValueError: If `metrics` is not `None` or `dict`. Returns: Returns `dict` with evaluation results. """ hooks = self._check_hooks(hooks) if steps is not None: if steps <= 0: raise ValueError( 'Must specify steps > 0, given: {}'.format(steps)) hooks.append(evaluation._StopAfterNEvalsHook(num_evals=steps)) return self._evaluate_model(input_fn=input_fn, name=name, checkpoint_path=checkpoint_path, hooks=hooks)
def evaluate(self, input_fn, steps=None, hooks=None, checkpoint_path=None, name=None): """Evaluates the model given evaluation data input_fn. For each step, calls `input_fn`, which returns one batch of data. Evaluates until: - `steps` batches are processed, or - `input_fn` raises an end-of-input exception (`OutOfRangeError` or `StopIteration`). Args: input_fn: Input function returning a tuple of: features - Dictionary of string feature name to `Tensor` or `SparseTensor`. labels - `Tensor` or dictionary of `Tensor` with labels. steps: Number of steps for which to evaluate model. If `None`, evaluates until `input_fn` raises an end-of-input exception. hooks: List of `SessionRunHook` subclass instances. Used for callbacks inside the evaluation call. checkpoint_path: Path of a specific checkpoint to evaluate. If `None`, the latest checkpoint in `model_dir` is used. name: 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. Returns: A dict containing the evaluation metrics specified in `model_fn` keyed by name, as well as an entry `global_step` which contains the value of the global step for which this evaluation was performed. Raises: ValueError: If `steps <= 0`. ValueError: If no model has been trained, namely `model_dir`, or the given `checkpoint_path` is empty. """ # We need to copy the hook array as we modify it, thus [:]. hooks = hooks[:] if hooks else [] if steps is not None: if steps <= 0: raise ValueError('Must specify steps >= 0, given: {}'.format(steps)) hooks.append(evaluation._StopAfterNEvalsHook( # pylint: disable=protected-access num_evals=steps)) return self._evaluate_model( input_fn=input_fn, hooks=hooks, checkpoint_path=checkpoint_path, name=name)
def testEvaluateWithFiniteInputs(self): checkpoint_dir = os.path.join(self.get_temp_dir(), 'evaluate_with_finite_inputs') # Train a Model to completion: self._train_model(checkpoint_dir, num_steps=300) # Run evaluation. Inputs are fed through input producer for one epoch. all_inputs = constant_op.constant(self._inputs, dtype=dtypes.float32) all_labels = constant_op.constant(self._labels, dtype=dtypes.float32) single_input, single_label = training.slice_input_producer( [all_inputs, all_labels], num_epochs=1) inputs, labels = training.batch([single_input, single_label], batch_size=6, allow_smaller_final_batch=True) logits = logistic_classifier(inputs) predictions = math_ops.round(logits) accuracy, update_op = metrics.accuracy(predictions=predictions, labels=labels) checkpoint_path = saver.latest_checkpoint(checkpoint_dir) final_ops_values = evaluation._evaluate_once( checkpoint_path=checkpoint_path, eval_ops=update_op, final_ops={ 'accuracy': accuracy, 'eval_steps': evaluation._get_or_create_eval_step() }, hooks=[ evaluation._StopAfterNEvalsHook(None), ]) self.assertTrue(final_ops_values['accuracy'] > .99) # Runs evaluation for 4 iterations. First 2 evaluate full batch of 6 inputs # each; the 3rd iter evaluates the remaining 4 inputs, and the last one # triggers an error which stops evaluation. self.assertEqual(final_ops_values['eval_steps'], 4)
def testEvaluatePerfectModel(self): checkpoint_dir = os.path.join(self.get_temp_dir(), 'evaluate_perfect_model_once') # Train a Model to completion: self._train_model(checkpoint_dir, num_steps=300) # Run inputs = constant_op.constant(self._inputs, dtype=dtypes.float32) labels = constant_op.constant(self._labels, dtype=dtypes.float32) logits = logistic_classifier(inputs) predictions = math_ops.round(logits) accuracy, update_op = metrics.accuracy( predictions=predictions, labels=labels) checkpoint_path = saver.latest_checkpoint(checkpoint_dir) final_ops_values = evaluation._evaluate_once( checkpoint_path=checkpoint_path, eval_ops=update_op, final_ops={'accuracy': accuracy}, hooks=[evaluation._StopAfterNEvalsHook(1),]) self.assertTrue(final_ops_values['accuracy'] > .99)