|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| """Video ssl linear evaluation task definition."""
|
| from typing import Any, Optional, List, Tuple
|
| from absl import logging
|
| import tensorflow as tf, tf_keras
|
|
|
|
|
| from official.core import task_factory
|
| from official.projects.video_ssl.configs import video_ssl as exp_cfg
|
| from official.projects.video_ssl.modeling import video_ssl_model
|
| from official.vision.tasks import video_classification
|
|
|
|
|
| @task_factory.register_task_cls(exp_cfg.VideoSSLEvalTask)
|
| class VideoSSLEvalTask(video_classification.VideoClassificationTask):
|
| """A task for video ssl linear evaluation."""
|
|
|
| def initialize(self, model: tf_keras.Model):
|
| """Loading pretrained checkpoint."""
|
| if not self.task_config.init_checkpoint:
|
| return
|
|
|
| ckpt_dir_or_file = self.task_config.init_checkpoint
|
| if tf.io.gfile.isdir(ckpt_dir_or_file):
|
| ckpt_dir_or_file = tf.train.latest_checkpoint(ckpt_dir_or_file)
|
|
|
|
|
| if self.task_config.init_checkpoint_modules == 'backbone':
|
| ckpt = tf.train.Checkpoint(backbone=model.backbone)
|
| ckpt.read(ckpt_dir_or_file)
|
| else:
|
| raise NotImplementedError
|
|
|
| logging.info('Finished loading pretrained checkpoint from %s',
|
| ckpt_dir_or_file)
|
|
|
| def train_step(self,
|
| inputs: Tuple[Any, Any],
|
| model: tf_keras.Model,
|
| optimizer: tf_keras.optimizers.Optimizer,
|
| metrics: Optional[List[Any]] = None):
|
| """Does forward and backward.
|
|
|
| Args:
|
| inputs: a dictionary of input tensors.
|
| model: the model, forward pass definition.
|
| optimizer: the optimizer for this training step.
|
| metrics: a nested structure of metrics objects.
|
|
|
| Returns:
|
| A dictionary of logs.
|
| """
|
| model.backbone.trainable = False
|
| logging.info('Setting the backbone to non-trainable.')
|
|
|
| return super().train_step(inputs, model, optimizer, metrics)
|
|
|