예제 #1
0
class MaskedLMConfig(cfg.TaskConfig):
  """The model config."""
  network: bert.BertPretrainerConfig = bert.BertPretrainerConfig(cls_heads=[
      bert.ClsHeadConfig(
          inner_dim=768, num_classes=2, dropout_rate=0.1, name='next_sentence')
  ])
  train_data: cfg.DataConfig = cfg.DataConfig()
  validation_data: cfg.DataConfig = cfg.DataConfig()
예제 #2
0
class QuestionAnsweringConfig(cfg.TaskConfig):
  """The model config."""
  # At most one of `init_checkpoint` and `hub_module_url` can be specified.
  init_checkpoint: str = ''
  hub_module_url: str = ''
  network: encoders.TransformerEncoderConfig = (
      encoders.TransformerEncoderConfig())
  train_data: cfg.DataConfig = cfg.DataConfig()
  validation_data: cfg.DataConfig = cfg.DataConfig()
예제 #3
0
class DualEncoderConfig(cfg.TaskConfig):
  """The model config."""
  # At most one of `init_checkpoint` and `hub_module_url` can
  # be specified.
  init_checkpoint: str = ''
  hub_module_url: str = ''
  # Defines the concrete model config at instantiation time.
  model: ModelConfig = ModelConfig()
  train_data: cfg.DataConfig = cfg.DataConfig()
  validation_data: cfg.DataConfig = cfg.DataConfig()
예제 #4
0
class ElectraPretrainConfig(cfg.TaskConfig):
    """The model config."""
    model: electra.ElectraPretrainerConfig = electra.ElectraPretrainerConfig(
        cls_heads=[
            bert.ClsHeadConfig(inner_dim=768,
                               num_classes=2,
                               dropout_rate=0.1,
                               name='next_sentence')
        ])
    train_data: cfg.DataConfig = cfg.DataConfig()
    validation_data: cfg.DataConfig = cfg.DataConfig()
예제 #5
0
class QuestionAnsweringConfig(cfg.TaskConfig):
    """The model config."""
    # At most one of `init_checkpoint` and `hub_module_url` can be specified.
    init_checkpoint: str = ''
    hub_module_url: str = ''
    n_best_size: int = 20
    max_answer_length: int = 30
    null_score_diff_threshold: float = 0.0
    model: ModelConfig = ModelConfig()
    train_data: cfg.DataConfig = cfg.DataConfig()
    validation_data: cfg.DataConfig = cfg.DataConfig()
예제 #6
0
class SentencePredictionConfig(cfg.TaskConfig):
  """The model config."""
  # At most one of `init_checkpoint` and `hub_module_url` can
  # be specified.
  init_checkpoint: str = ''
  init_cls_pooler: bool = False
  hub_module_url: str = ''
  metric_type: str = 'accuracy'
  # Defines the concrete model config at instantiation time.
  model: ModelConfig = ModelConfig()
  train_data: cfg.DataConfig = cfg.DataConfig()
  validation_data: cfg.DataConfig = cfg.DataConfig()
예제 #7
0
class TaggingConfig(cfg.TaskConfig):
  """The model config."""
  # At most one of `init_checkpoint` and `hub_module_url` can be specified.
  init_checkpoint: str = ''
  hub_module_url: str = ''
  model: ModelConfig = ModelConfig()

  # The real class names, the order of which should match real label id.
  # Note that a word may be tokenized into multiple word_pieces tokens, and
  # we asssume the real label id (non-negative) is assigned to the first token
  # of the word, and a negative label id is assigned to the remaining tokens.
  # The negative label id will not contribute to loss and metrics.
  class_names: Optional[List[str]] = None
  train_data: cfg.DataConfig = cfg.DataConfig()
  validation_data: cfg.DataConfig = cfg.DataConfig()
예제 #8
0
class TaggingConfig(cfg.TaskConfig):
    """The model config."""
    # At most one of `init_checkpoint` and `hub_module_url` can be specified.
    init_checkpoint: str = ''
    hub_module_url: str = ''
    model: encoders.TransformerEncoderConfig = (
        encoders.TransformerEncoderConfig())
    num_classes: int = 0
    # The ignored label id will not contribute to loss.
    # A word may be tokenized into multiple word_pieces tokens, and we usually
    # assign the real label id for the first token of the word, and
    # `ignore_label_id` for the remaining tokens.
    ignore_label_id: int = 0
    train_data: cfg.DataConfig = cfg.DataConfig()
    validation_data: cfg.DataConfig = cfg.DataConfig()
예제 #9
0
class SentencePredictionConfig(cfg.TaskConfig):
    """The model config."""
    # At most one of `pretrain_checkpoint_dir` and `hub_module_url` can
    # be specified.
    pretrain_checkpoint_dir: str = ''
    hub_module_url: str = ''
    network: bert.BertPretrainerConfig = bert.BertPretrainerConfig(
        num_masked_tokens=0,
        cls_heads=[
            bert.ClsHeadConfig(inner_dim=768,
                               num_classes=3,
                               dropout_rate=0.1,
                               name='sentence_prediction')
        ])
    train_data: cfg.DataConfig = cfg.DataConfig()
    validation_data: cfg.DataConfig = cfg.DataConfig()
예제 #10
0
    def test_parser(self, output_size, dtype, is_training):

        params = cfg.DataConfig(input_path='imagenet-2012-tfrecord/train*',
                                global_batch_size=2,
                                is_training=True,
                                examples_consume=4)

        decoder = classification_input.Decoder()
        parser = classification_input.Parser(output_size=output_size[:2],
                                             num_classes=1001,
                                             aug_rand_hflip=False,
                                             dtype=dtype)

        reader = input_reader.InputReader(params,
                                          dataset_fn=tf.data.TFRecordDataset,
                                          decoder_fn=decoder.decode,
                                          parser_fn=parser.parse_fn(
                                              params.is_training))

        dataset = reader.read()

        images, labels = next(iter(dataset))

        self.assertAllEqual(images.numpy().shape,
                            [params.global_batch_size] + output_size)
        self.assertAllEqual(labels.numpy().shape, [params.global_batch_size])

        if dtype == 'float32':
            self.assertAllEqual(images.dtype, tf.float32)
        elif dtype == 'float16':
            self.assertAllEqual(images.dtype, tf.float16)
        elif dtype == 'bfloat16':
            self.assertAllEqual(images.dtype, tf.bfloat16)
class TaggingConfig(cfg.TaskConfig):
    """The model config."""
    # At most one of `init_checkpoint` and `hub_module_url` can be specified.
    init_checkpoint: str = ''
    hub_module_url: str = ''
    model: encoders.TransformerEncoderConfig = (
        encoders.TransformerEncoderConfig())

    # The number of real labels. Note that a word may be tokenized into
    # multiple word_pieces tokens, and we asssume the real label id (non-negative)
    # is assigned to the first token of the word, and a negative label id is
    # assigned to the remaining tokens. The negative label id will not contribute
    # to loss and metrics.
    num_classes: int = 0
    train_data: cfg.DataConfig = cfg.DataConfig()
    validation_data: cfg.DataConfig = cfg.DataConfig()
예제 #12
0
class SentencePredictionConfig(cfg.TaskConfig):
    """The model config."""
    # At most one of `init_checkpoint` and `hub_module_url` can
    # be specified.
    init_checkpoint: str = ''
    hub_module_url: str = ''
    metric_type: str = 'accuracy'
    network: bert.BertPretrainerConfig = bert.BertPretrainerConfig(
        num_masked_tokens=0,  # No masked language modeling head.
        cls_heads=[
            bert.ClsHeadConfig(inner_dim=768,
                               num_classes=3,
                               dropout_rate=0.1,
                               name='sentence_prediction')
        ])
    train_data: cfg.DataConfig = cfg.DataConfig()
    validation_data: cfg.DataConfig = cfg.DataConfig()
예제 #13
0
파일: masked_lm.py 프로젝트: xjx0524/models
from official.nlp.configs import bert
from official.nlp.data import data_loader_factory


@dataclasses.dataclass
class MaskedLMConfig(cfg.TaskConfig):
  """The model config."""
<<<<<<< HEAD
  init_checkpoint: str = ''
=======
>>>>>>> a811a3b7e640722318ad868c99feddf3f3063e36
  model: bert.BertPretrainerConfig = bert.BertPretrainerConfig(cls_heads=[
      bert.ClsHeadConfig(
          inner_dim=768, num_classes=2, dropout_rate=0.1, name='next_sentence')
  ])
  train_data: cfg.DataConfig = cfg.DataConfig()
  validation_data: cfg.DataConfig = cfg.DataConfig()


<<<<<<< HEAD
@base_task.register_task_cls(MaskedLMConfig)
=======
@task_factory.register_task_cls(MaskedLMConfig)
>>>>>>> a811a3b7e640722318ad868c99feddf3f3063e36
class MaskedLMTask(base_task.Task):
  """Mock task object for testing."""

  def build_model(self, params=None):
    params = params or self.task_config.model
    return bert.instantiate_pretrainer_from_cfg(params)
예제 #14
0
    def testRetinanetInputReader(self, output_size, skip_crowd_during_training,
                                 use_autoaugment, is_training):

        batch_size = 2
        min_level = 3
        max_level = 7
        num_scales = 3
        aspect_ratios = [0.5, 1.0, 2.0]
        anchor_size = 3
        max_num_instances = 100

        params = cfg.DataConfig(
            input_path='/placer/prod/home/snaggletooth/test/data/coco/val*',
            global_batch_size=batch_size,
            is_training=is_training)

        decoder = tf_example_decoder.TfExampleDecoder()
        parser = retinanet_input.Parser(
            output_size=output_size,
            min_level=min_level,
            max_level=max_level,
            num_scales=num_scales,
            aspect_ratios=aspect_ratios,
            anchor_size=anchor_size,
            skip_crowd_during_training=skip_crowd_during_training,
            use_autoaugment=use_autoaugment,
            max_num_instances=max_num_instances,
            dtype='bfloat16')

        reader = input_reader.InputReader(params,
                                          dataset_fn=tf.data.TFRecordDataset,
                                          decoder_fn=decoder.decode,
                                          parser_fn=parser.parse_fn(
                                              params.is_training))

        dataset = reader.read()

        iterator = iter(dataset)
        image, labels = next(iterator)
        np_image = image.numpy()
        np_labels = tf.nest.map_structure(lambda x: x.numpy(), labels)

        # Checks image shape.
        self.assertEqual(list(np_image.shape),
                         [batch_size, output_size[0], output_size[1], 3])
        # Checks keys in labels.
        if is_training:
            self.assertCountEqual(np_labels.keys(), [
                'cls_targets', 'box_targets', 'anchor_boxes', 'cls_weights',
                'box_weights', 'image_info'
            ])
        else:
            self.assertCountEqual(np_labels.keys(), [
                'cls_targets', 'box_targets', 'anchor_boxes', 'cls_weights',
                'box_weights', 'groundtruths', 'image_info'
            ])
        # Checks shapes of `image_info` and `anchor_boxes`.
        self.assertEqual(np_labels['image_info'].shape, (batch_size, 4, 2))
        n_anchors = 0
        for level in range(min_level, max_level + 1):
            stride = 2**level
            output_size_l = [output_size[0] / stride, output_size[1] / stride]
            anchors_per_location = num_scales * len(aspect_ratios)
            self.assertEqual(list(np_labels['anchor_boxes'][level].shape), [
                batch_size, output_size_l[0], output_size_l[1],
                4 * anchors_per_location
            ])
            n_anchors += output_size_l[0] * output_size_l[
                1] * anchors_per_location
        # Checks shapes of training objectives.
        self.assertEqual(np_labels['cls_weights'].shape,
                         (batch_size, n_anchors))
        for level in range(min_level, max_level + 1):
            stride = 2**level
            output_size_l = [output_size[0] / stride, output_size[1] / stride]
            anchors_per_location = num_scales * len(aspect_ratios)
            self.assertEqual(list(np_labels['cls_targets'][level].shape), [
                batch_size, output_size_l[0], output_size_l[1],
                anchors_per_location
            ])
            self.assertEqual(list(np_labels['box_targets'][level].shape), [
                batch_size, output_size_l[0], output_size_l[1],
                4 * anchors_per_location
            ])
        # Checks shape of groundtruths for eval.
        if not is_training:
            self.assertEqual(np_labels['groundtruths']['source_id'].shape,
                             (batch_size, ))
            self.assertEqual(np_labels['groundtruths']['classes'].shape,
                             (batch_size, max_num_instances))
            self.assertEqual(np_labels['groundtruths']['boxes'].shape,
                             (batch_size, max_num_instances, 4))
            self.assertEqual(np_labels['groundtruths']['areas'].shape,
                             (batch_size, max_num_instances))
            self.assertEqual(np_labels['groundtruths']['is_crowds'].shape,
                             (batch_size, max_num_instances))
예제 #15
0
    def testMaskRCNNInputReader(self, output_size, skip_crowd_during_training,
                                include_mask, is_training):
        min_level = 3
        max_level = 7
        num_scales = 3
        aspect_ratios = [1.0, 2.0, 0.5]
        max_num_instances = 100
        batch_size = 2
        mask_crop_size = 112
        anchor_size = 4.0

        params = cfg.DataConfig(
            input_path='/placer/prod/home/snaggletooth/test/data/coco/val*',
            global_batch_size=batch_size,
            is_training=is_training)

        parser = maskrcnn_input.Parser(
            output_size=output_size,
            min_level=min_level,
            max_level=max_level,
            num_scales=num_scales,
            aspect_ratios=aspect_ratios,
            anchor_size=anchor_size,
            rpn_match_threshold=0.7,
            rpn_unmatched_threshold=0.3,
            rpn_batch_size_per_im=256,
            rpn_fg_fraction=0.5,
            aug_rand_hflip=True,
            aug_scale_min=0.8,
            aug_scale_max=1.2,
            skip_crowd_during_training=skip_crowd_during_training,
            max_num_instances=max_num_instances,
            include_mask=include_mask,
            mask_crop_size=mask_crop_size,
            dtype='bfloat16')

        decoder = tf_example_decoder.TfExampleDecoder(
            include_mask=include_mask)
        reader = input_reader.InputReader(params,
                                          dataset_fn=tf.data.TFRecordDataset,
                                          decoder_fn=decoder.decode,
                                          parser_fn=parser.parse_fn(
                                              params.is_training))

        dataset = reader.read()
        iterator = iter(dataset)

        images, labels = next(iterator)

        np_images = images.numpy()
        np_labels = tf.nest.map_structure(lambda x: x.numpy(), labels)

        if is_training:
            self.assertAllEqual(
                np_images.shape,
                [batch_size, output_size[0], output_size[1], 3])
            self.assertAllEqual(np_labels['image_info'].shape,
                                [batch_size, 4, 2])
            self.assertAllEqual(np_labels['gt_boxes'].shape,
                                [batch_size, max_num_instances, 4])
            self.assertAllEqual(np_labels['gt_classes'].shape,
                                [batch_size, max_num_instances])
            if include_mask:
                self.assertAllEqual(np_labels['gt_masks'].shape, [
                    batch_size, max_num_instances, mask_crop_size,
                    mask_crop_size
                ])
            for level in range(min_level, max_level + 1):
                stride = 2**level
                output_size_l = [
                    output_size[0] / stride, output_size[1] / stride
                ]
                anchors_per_location = num_scales * len(aspect_ratios)
                self.assertAllEqual(
                    np_labels['rpn_score_targets'][level].shape, [
                        batch_size, output_size_l[0], output_size_l[1],
                        anchors_per_location
                    ])
                self.assertAllEqual(
                    np_labels['rpn_box_targets'][level].shape, [
                        batch_size, output_size_l[0], output_size_l[1],
                        4 * anchors_per_location
                    ])
                self.assertAllEqual(np_labels['anchor_boxes'][level].shape, [
                    batch_size, output_size_l[0], output_size_l[1],
                    4 * anchors_per_location
                ])
        else:
            self.assertAllEqual(
                np_images.shape,
                [batch_size, output_size[0], output_size[1], 3])
            self.assertAllEqual(np_labels['image_info'].shape,
                                [batch_size, 4, 2])