Beispiel #1
0
    def resnet_model_fn_helper(self, mode, version, multi_gpu=False):
        """Tests that the EstimatorSpec is given the appropriate arguments."""
        tf.train.create_global_step()

        input_fn = imagenet_main.get_synth_input_fn()
        dataset = input_fn(True, '', _BATCH_SIZE)
        iterator = dataset.make_one_shot_iterator()
        features, labels = iterator.get_next()
        spec = imagenet_main.imagenet_model_fn(
            features, labels, mode, {
                'resnet_size': 50,
                'data_format': 'channels_last',
                'batch_size': _BATCH_SIZE,
                'version': version,
                'multi_gpu': multi_gpu,
            })

        predictions = spec.predictions
        self.assertAllEqual(predictions['probabilities'].shape,
                            (_BATCH_SIZE, _LABEL_CLASSES))
        self.assertEqual(predictions['probabilities'].dtype, tf.float32)
        self.assertAllEqual(predictions['classes'].shape, (_BATCH_SIZE, ))
        self.assertEqual(predictions['classes'].dtype, tf.int64)

        if mode != tf.estimator.ModeKeys.PREDICT:
            loss = spec.loss
            self.assertAllEqual(loss.shape, ())
            self.assertEqual(loss.dtype, tf.float32)

        if mode == tf.estimator.ModeKeys.EVAL:
            eval_metric_ops = spec.eval_metric_ops
            self.assertAllEqual(eval_metric_ops['accuracy'][0].shape, ())
            self.assertAllEqual(eval_metric_ops['accuracy'][1].shape, ())
            self.assertEqual(eval_metric_ops['accuracy'][0].dtype, tf.float32)
            self.assertEqual(eval_metric_ops['accuracy'][1].dtype, tf.float32)
Beispiel #2
0
  def resnet_model_fn_helper(self, mode, version, multi_gpu=False):
    """Tests that the EstimatorSpec is given the appropriate arguments."""
    tf.train.create_global_step()

    input_fn = imagenet_main.get_synth_input_fn()
    dataset = input_fn(True, '', _BATCH_SIZE)
    iterator = dataset.make_one_shot_iterator()
    features, labels = iterator.get_next()
    spec = imagenet_main.imagenet_model_fn(
        features, labels, mode, {
            'resnet_size': 50,
            'data_format': 'channels_last',
            'batch_size': _BATCH_SIZE,
            'version': version,
            'multi_gpu': multi_gpu,
        })

    predictions = spec.predictions
    self.assertAllEqual(predictions['probabilities'].shape,
                        (_BATCH_SIZE, _LABEL_CLASSES))
    self.assertEqual(predictions['probabilities'].dtype, tf.float32)
    self.assertAllEqual(predictions['classes'].shape, (_BATCH_SIZE,))
    self.assertEqual(predictions['classes'].dtype, tf.int64)

    if mode != tf.estimator.ModeKeys.PREDICT:
      loss = spec.loss
      self.assertAllEqual(loss.shape, ())
      self.assertEqual(loss.dtype, tf.float32)

    if mode == tf.estimator.ModeKeys.EVAL:
      eval_metric_ops = spec.eval_metric_ops
      self.assertAllEqual(eval_metric_ops['accuracy'][0].shape, ())
      self.assertAllEqual(eval_metric_ops['accuracy'][1].shape, ())
      self.assertEqual(eval_metric_ops['accuracy'][0].dtype, tf.float32)
      self.assertEqual(eval_metric_ops['accuracy'][1].dtype, tf.float32)
Beispiel #3
0
    def _resnet_model_fn_helper(self, mode, version, dtype, multi_gpu):
        """Tests that the EstimatorSpec is given the appropriate arguments."""
        with tf.Graph().as_default() as g:
            tf.train.create_global_step()

            input_fn = imagenet_main.get_synth_input_fn()
            dataset = input_fn(True, '', _BATCH_SIZE)
            iterator = dataset.make_one_shot_iterator()
            features, labels = iterator.get_next()
            spec = imagenet_main.imagenet_model_fn(
                features, labels, mode, {
                    'dtype': dtype,
                    'resnet_size': 50,
                    'data_format': 'channels_last',
                    'batch_size': _BATCH_SIZE,
                    'version': version,
                    'loss_scale': 128 if dtype == tf.float16 else 1,
                    'multi_gpu': multi_gpu,
                })

            predictions = spec.predictions
            self.assertAllEqual(predictions['probabilities'].shape,
                                (_BATCH_SIZE, _LABEL_CLASSES))
            self.assertEqual(predictions['probabilities'].dtype, tf.float32)
            self.assertAllEqual(predictions['classes'].shape, (_BATCH_SIZE, ))
            self.assertEqual(predictions['classes'].dtype, tf.int64)

            if mode != tf.estimator.ModeKeys.PREDICT:
                loss = spec.loss
                self.assertAllEqual(loss.shape, ())
                self.assertEqual(loss.dtype, tf.float32)

            if mode == tf.estimator.ModeKeys.EVAL:
                eval_metric_ops = spec.eval_metric_ops
                self.assertAllEqual(eval_metric_ops['accuracy'][0].shape, ())
                self.assertAllEqual(eval_metric_ops['accuracy'][1].shape, ())
                self.assertEqual(eval_metric_ops['accuracy'][0].dtype,
                                 tf.float32)
                self.assertEqual(eval_metric_ops['accuracy'][1].dtype,
                                 tf.float32)

                tensors_to_check = ('initial_conv:0', 'initial_max_pool:0',
                                    'block_layer1:0', 'block_layer2:0',
                                    'block_layer3:0', 'block_layer4:0',
                                    'final_reduce_mean:0', 'final_dense:0')

                for tensor_name in tensors_to_check:
                    tensor = g.get_tensor_by_name('resnet_model/' +
                                                  tensor_name)
                    self.assertEqual(
                        tensor.dtype, dtype,
                        'Tensor {} has dtype {}, while dtype {} was '
                        'expected'.format(tensor, tensor.dtype, dtype))
  def _resnet_model_fn_helper(self, mode, version, dtype, multi_gpu):
    """Tests that the EstimatorSpec is given the appropriate arguments."""
    with tf.Graph().as_default() as g:
      tf.train.create_global_step()

      input_fn = imagenet_main.get_synth_input_fn()
      dataset = input_fn(True, '', _BATCH_SIZE)
      iterator = dataset.make_one_shot_iterator()
      features, labels = iterator.get_next()
      spec = imagenet_main.imagenet_model_fn(
          features, labels, mode, {
              'dtype': dtype,
              'resnet_size': 50,
              'data_format': 'channels_last',
              'batch_size': _BATCH_SIZE,
              'version': version,
              'loss_scale': 128 if dtype == tf.float16 else 1,
              'multi_gpu': multi_gpu,
          })

      predictions = spec.predictions
      self.assertAllEqual(predictions['probabilities'].shape,
                          (_BATCH_SIZE, _LABEL_CLASSES))
      self.assertEqual(predictions['probabilities'].dtype, tf.float32)
      self.assertAllEqual(predictions['classes'].shape, (_BATCH_SIZE,))
      self.assertEqual(predictions['classes'].dtype, tf.int64)

      if mode != tf.estimator.ModeKeys.PREDICT:
        loss = spec.loss
        self.assertAllEqual(loss.shape, ())
        self.assertEqual(loss.dtype, tf.float32)

      if mode == tf.estimator.ModeKeys.EVAL:
        eval_metric_ops = spec.eval_metric_ops
        self.assertAllEqual(eval_metric_ops['accuracy'][0].shape, ())
        self.assertAllEqual(eval_metric_ops['accuracy'][1].shape, ())
        self.assertEqual(eval_metric_ops['accuracy'][0].dtype, tf.float32)
        self.assertEqual(eval_metric_ops['accuracy'][1].dtype, tf.float32)

        tensors_to_check = ('initial_conv:0', 'initial_max_pool:0',
                            'block_layer1:0', 'block_layer2:0',
                            'block_layer3:0', 'block_layer4:0',
                            'final_reduce_mean:0', 'final_dense:0')

        for tensor_name in tensors_to_check:
          tensor = g.get_tensor_by_name('resnet_model/' + tensor_name)
          self.assertEqual(tensor.dtype, dtype,
                           'Tensor {} has dtype {}, while dtype {} was '
                           'expected'.format(tensor, tensor.dtype,
                                             dtype))
Beispiel #5
0
    def resnet_model_fn_helper(self, mode, resnet_version, dtype):
        """Tests that the EstimatorSpec is given the appropriate arguments."""
        tf.compat.v1.train.create_global_step()

        input_fn = imagenet_main.get_synth_input_fn(dtype)
        dataset = input_fn(True, '', _BATCH_SIZE)
        iterator = tf.compat.v1.data.make_initializable_iterator(dataset)
        features, labels = iterator.get_next()
        spec = imagenet_main.imagenet_model_fn(
            features, labels, mode, {
                'dtype': dtype,
                'resnet_size': 50,
                'data_format': 'channels_last',
                'batch_size': _BATCH_SIZE,
                'resnet_version': resnet_version,
                'loss_scale': 128 if dtype == tf.float16 else 1,
                'fine_tune': False,
            })

        predictions = spec.predictions
        self.assertAllEqual(predictions['probabilities'].shape,
                            (_BATCH_SIZE, _LABEL_CLASSES))
        self.assertEqual(predictions['probabilities'].dtype, tf.float32)
        self.assertAllEqual(predictions['classes'].shape, (_BATCH_SIZE, ))
        self.assertEqual(predictions['classes'].dtype, tf.int64)

        if mode != tf.estimator.ModeKeys.PREDICT:
            loss = spec.loss
            self.assertAllEqual(loss.shape, ())
            self.assertEqual(loss.dtype, tf.float32)

        if mode == tf.estimator.ModeKeys.EVAL:
            eval_metric_ops = spec.eval_metric_ops
            self.assertAllEqual(eval_metric_ops['accuracy'][0].shape, ())
            self.assertAllEqual(eval_metric_ops['accuracy'][1].shape, ())
            self.assertEqual(eval_metric_ops['accuracy'][0].dtype, tf.float32)
            self.assertEqual(eval_metric_ops['accuracy'][1].dtype, tf.float32)
Beispiel #6
0
  def resnet_model_fn_helper(self, mode, resnet_version, dtype):
    """Tests that the EstimatorSpec is given the appropriate arguments."""
    tf.compat.v1.train.create_global_step()

    input_fn = imagenet_main.get_synth_input_fn(dtype)
    dataset = input_fn(True, '', _BATCH_SIZE)
    iterator = tf.compat.v1.data.make_initializable_iterator(dataset)
    features, labels = iterator.get_next()
    spec = imagenet_main.imagenet_model_fn(
        features, labels, mode, {
            'dtype': dtype,
            'resnet_size': 50,
            'data_format': 'channels_last',
            'batch_size': _BATCH_SIZE,
            'resnet_version': resnet_version,
            'loss_scale': 128 if dtype == tf.float16 else 1,
            'fine_tune': False,
        })

    predictions = spec.predictions
    self.assertAllEqual(predictions['probabilities'].shape,
                        (_BATCH_SIZE, _LABEL_CLASSES))
    self.assertEqual(predictions['probabilities'].dtype, tf.float32)
    self.assertAllEqual(predictions['classes'].shape, (_BATCH_SIZE,))
    self.assertEqual(predictions['classes'].dtype, tf.int64)

    if mode != tf.estimator.ModeKeys.PREDICT:
      loss = spec.loss
      self.assertAllEqual(loss.shape, ())
      self.assertEqual(loss.dtype, tf.float32)

    if mode == tf.estimator.ModeKeys.EVAL:
      eval_metric_ops = spec.eval_metric_ops
      self.assertAllEqual(eval_metric_ops['accuracy'][0].shape, ())
      self.assertAllEqual(eval_metric_ops['accuracy'][1].shape, ())
      self.assertEqual(eval_metric_ops['accuracy'][0].dtype, tf.float32)
      self.assertEqual(eval_metric_ops['accuracy'][1].dtype, tf.float32)