|
| 1 | +# Copyright 2021 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Video classification configuration definition.""" |
| 16 | +from typing import Optional, Tuple |
| 17 | +from absl import flags |
| 18 | +import dataclasses |
| 19 | + |
| 20 | +from official.core import config_definitions as cfg |
| 21 | +from official.core import exp_factory |
| 22 | +from official.modeling import hyperparams |
| 23 | +from official.modeling import optimization |
| 24 | + |
| 25 | +FLAGS = flags.FLAGS |
| 26 | + |
| 27 | +YT8M_TRAIN_EXAMPLES = 3888919 |
| 28 | +YT8M_VAL_EXAMPLES = 1112356 |
| 29 | +# 2/frame -> frame level |
| 30 | +# 3/frame -> segment level |
| 31 | +YT8M_TRAIN_PATH = 'gs://youtube8m-ml/2/frame/train/train*.tfrecord' |
| 32 | +YT8M_VAL_PATH = 'gs://youtube8m-ml/3/frame/validate/validate*.tfrecord' |
| 33 | + |
| 34 | + |
| 35 | +@dataclasses.dataclass |
| 36 | +class DataConfig(cfg.DataConfig): |
| 37 | + """The base configuration for building datasets.""" |
| 38 | + name: Optional[str] = 'yt8m' |
| 39 | + split: Optional[str] = None |
| 40 | + feature_sizes: Tuple[int, ...] = (1024, 128) |
| 41 | + feature_names: Tuple[str, ...] = ('rgb', 'audio') |
| 42 | + segment_size: int = 1 |
| 43 | + segment_labels: bool = False |
| 44 | + temporal_stride: int = 1 |
| 45 | + max_frames: int = 300 |
| 46 | + num_frames: int = 300 # set smaller to allow random sample (Parser) |
| 47 | + num_classes: int = 3862 |
| 48 | + num_devices: int = 1 |
| 49 | + input_path: str = '' |
| 50 | + is_training: bool = True |
| 51 | + random_seed: int = 123 |
| 52 | + num_examples: int = -1 |
| 53 | + |
| 54 | + |
| 55 | +def yt8m(is_training): |
| 56 | + """YT8M dataset configs.""" |
| 57 | + return DataConfig( |
| 58 | + num_frames=30, |
| 59 | + temporal_stride=1, |
| 60 | + segment_labels=False, |
| 61 | + segment_size=5, |
| 62 | + is_training=is_training, |
| 63 | + split='train' if is_training else 'valid', |
| 64 | + num_examples=YT8M_TRAIN_EXAMPLES if is_training else YT8M_VAL_EXAMPLES, |
| 65 | + input_path=YT8M_TRAIN_PATH if is_training else YT8M_VAL_PATH) |
| 66 | + |
| 67 | + |
| 68 | +@dataclasses.dataclass |
| 69 | +class YT8MModel(hyperparams.Config): |
| 70 | + """The model config.""" |
| 71 | + cluster_size: int = 2048 |
| 72 | + hidden_size: int = 2048 |
| 73 | + add_batch_norm: bool = True |
| 74 | + sample_random_frames: bool = True |
| 75 | + is_training: bool = True |
| 76 | + activation: str = 'relu6' |
| 77 | + pooling_method: str = 'average' |
| 78 | + yt8m_agg_classifier_model: str = 'MoeModel' |
| 79 | + |
| 80 | + |
| 81 | +@dataclasses.dataclass |
| 82 | +class Losses(hyperparams.Config): |
| 83 | + name: str = 'binary_crossentropy' |
| 84 | + from_logits: bool = False |
| 85 | + label_smoothing: float = 0.0 |
| 86 | + |
| 87 | + |
| 88 | +@dataclasses.dataclass |
| 89 | +class YT8MTask(cfg.TaskConfig): |
| 90 | + """The task config.""" |
| 91 | + model: YT8MModel = YT8MModel() |
| 92 | + train_data: DataConfig = yt8m(is_training=True) |
| 93 | + validation_data: DataConfig = yt8m(is_training=False) |
| 94 | + losses: Losses = Losses() |
| 95 | + gradient_clip_norm: float = 1.0 |
| 96 | + num_readers: int = 8 |
| 97 | + top_k: int = 20 |
| 98 | + top_n: Optional[int] = None |
| 99 | + |
| 100 | + |
| 101 | +def add_trainer( |
| 102 | + experiment: cfg.ExperimentConfig, |
| 103 | + train_batch_size: int, |
| 104 | + eval_batch_size: int, |
| 105 | + learning_rate: float = 0.005, |
| 106 | + train_epochs: int = 44, |
| 107 | +): |
| 108 | + """Add and config a trainer to the experiment config.""" |
| 109 | + if YT8M_TRAIN_EXAMPLES <= 0: |
| 110 | + raise ValueError('Wrong train dataset size {!r}'.format( |
| 111 | + experiment.task.train_data)) |
| 112 | + if YT8M_VAL_EXAMPLES <= 0: |
| 113 | + raise ValueError('Wrong validation dataset size {!r}'.format( |
| 114 | + experiment.task.validation_data)) |
| 115 | + experiment.task.train_data.global_batch_size = train_batch_size |
| 116 | + experiment.task.validation_data.global_batch_size = eval_batch_size |
| 117 | + steps_per_epoch = YT8M_TRAIN_EXAMPLES // train_batch_size |
| 118 | + experiment.trainer = cfg.TrainerConfig( |
| 119 | + steps_per_loop=steps_per_epoch, |
| 120 | + summary_interval=steps_per_epoch, |
| 121 | + checkpoint_interval=steps_per_epoch, |
| 122 | + train_steps=train_epochs * steps_per_epoch, |
| 123 | + validation_steps=YT8M_VAL_EXAMPLES // eval_batch_size, |
| 124 | + validation_interval=steps_per_epoch, |
| 125 | + optimizer_config=optimization.OptimizationConfig({ |
| 126 | + 'optimizer': { |
| 127 | + 'type': 'adam', |
| 128 | + 'adam': {} |
| 129 | + }, |
| 130 | + 'learning_rate': { |
| 131 | + 'type': 'exponential', |
| 132 | + 'exponential': { |
| 133 | + 'initial_learning_rate': learning_rate, |
| 134 | + 'decay_rate': 0.95, |
| 135 | + 'decay_steps': 1500000, |
| 136 | + } |
| 137 | + }, |
| 138 | + })) |
| 139 | + return experiment |
| 140 | + |
| 141 | + |
| 142 | +@exp_factory.register_config_factory('yt8m_experiment') |
| 143 | +def yt8m_experiment() -> cfg.ExperimentConfig: |
| 144 | + """Video classification general.""" |
| 145 | + exp_config = cfg.ExperimentConfig( |
| 146 | + runtime=cfg.RuntimeConfig(mixed_precision_dtype='bfloat16'), |
| 147 | + task=YT8MTask(), |
| 148 | + trainer=cfg.TrainerConfig(), |
| 149 | + restrictions=[ |
| 150 | + 'task.train_data.is_training != None', |
| 151 | + 'task.validation_data.is_training != None', |
| 152 | + 'task.train_data.num_classes == task.validation_data.num_classes', |
| 153 | + 'task.train_data.feature_sizes != None', |
| 154 | + 'task.train_data.feature_names != None', |
| 155 | + ]) |
| 156 | + |
| 157 | + return add_trainer(exp_config, train_batch_size=512, eval_batch_size=512) |
0 commit comments