Unverified Commit 93f22151 authored by Vishnu Banna's avatar Vishnu Banna Committed by GitHub
Browse files

Merge pull request #1 from PurdueCAM2Project/linting

Linting
parents 9a1b8990 5dfe788d
...@@ -26,4 +26,7 @@ ...@@ -26,4 +26,7 @@
* [shapemask](vision/detection): An object detection and instance segmentation model using shape priors. See [Tensorboard.dev training metrics](https://tensorboard.dev/experiment/ZbXgVoc6Rf6mBRlPj0JpLA). * [shapemask](vision/detection): An object detection and instance segmentation model using shape priors. See [Tensorboard.dev training metrics](https://tensorboard.dev/experiment/ZbXgVoc6Rf6mBRlPj0JpLA).
## Recommendation ## Recommendation
* [dlrm](recommendation/ranking): [Deep Learning Recommendation Model for
Personalization and Recommendation Systems](https://arxiv.org/abs/1906.00091).
* [dcn v2](recommendation/ranking): [Improved Deep & Cross Network and Practical Lessons for Web-scale Learning to Rank Systems](https://arxiv.org/abs/2008.13535).
* [ncf](recommendation): Neural Collaborative Filtering. See [Tensorboard.dev training metrics](https://tensorboard.dev/experiment/0k3gKjZlR1ewkVTRyLB6IQ). * [ncf](recommendation): Neural Collaborative Filtering. See [Tensorboard.dev training metrics](https://tensorboard.dev/experiment/0k3gKjZlR1ewkVTRyLB6IQ).
...@@ -180,11 +180,15 @@ class EMAConfig(BaseOptimizerConfig): ...@@ -180,11 +180,15 @@ class EMAConfig(BaseOptimizerConfig):
Attributes: Attributes:
name: 'str', name of the optimizer. name: 'str', name of the optimizer.
trainable_weights_only: 'bool', if True, only model trainable weights will
be updated. Otherwise, all model weights will be updated. This mainly
affects batch normalization parameters.
average_decay: 'float', average decay value. average_decay: 'float', average decay value.
start_step: 'int', start step to apply moving average. start_step: 'int', start step to apply moving average.
dynamic_decay: 'bool', whether to apply dynamic decay or not. dynamic_decay: 'bool', whether to apply dynamic decay or not.
""" """
name: str = "ExponentialMovingAverage" name: str = "ExponentialMovingAverage"
trainable_weights_only: bool = True
average_decay: float = 0.99 average_decay: float = 0.99
start_step: int = 0 start_step: int = 0
dynamic_decay: bool = True dynamic_decay: bool = True
......
...@@ -48,6 +48,7 @@ class ExponentialMovingAverage(tf.keras.optimizers.Optimizer): ...@@ -48,6 +48,7 @@ class ExponentialMovingAverage(tf.keras.optimizers.Optimizer):
def __init__(self, def __init__(self,
optimizer: tf.keras.optimizers.Optimizer, optimizer: tf.keras.optimizers.Optimizer,
trainable_weights_only: bool = True,
average_decay: float = 0.99, average_decay: float = 0.99,
start_step: int = 0, start_step: int = 0,
dynamic_decay: bool = True, dynamic_decay: bool = True,
...@@ -58,6 +59,9 @@ class ExponentialMovingAverage(tf.keras.optimizers.Optimizer): ...@@ -58,6 +59,9 @@ class ExponentialMovingAverage(tf.keras.optimizers.Optimizer):
Args: Args:
optimizer: `tf.keras.optimizers.Optimizer` that will be optimizer: `tf.keras.optimizers.Optimizer` that will be
used to compute and apply gradients. used to compute and apply gradients.
trainable_weights_only: 'bool', if True, only model trainable weights will
be updated. Otherwise, all model weights will be updated. This mainly
affects batch normalization parameters.
average_decay: float. Decay to use to maintain the moving averages average_decay: float. Decay to use to maintain the moving averages
of trained variables. of trained variables.
start_step: int. What step to start the moving average. start_step: int. What step to start the moving average.
...@@ -72,6 +76,7 @@ class ExponentialMovingAverage(tf.keras.optimizers.Optimizer): ...@@ -72,6 +76,7 @@ class ExponentialMovingAverage(tf.keras.optimizers.Optimizer):
""" """
super().__init__(name, **kwargs) super().__init__(name, **kwargs)
self._average_decay = average_decay self._average_decay = average_decay
self._trainable_weights_only = trainable_weights_only
self._start_step = tf.constant(start_step, tf.float32) self._start_step = tf.constant(start_step, tf.float32)
self._dynamic_decay = dynamic_decay self._dynamic_decay = dynamic_decay
self._optimizer = optimizer self._optimizer = optimizer
...@@ -81,12 +86,17 @@ class ExponentialMovingAverage(tf.keras.optimizers.Optimizer): ...@@ -81,12 +86,17 @@ class ExponentialMovingAverage(tf.keras.optimizers.Optimizer):
def shadow_copy(self, model: tf.keras.Model): def shadow_copy(self, model: tf.keras.Model):
"""Creates shadow variables for the given model weights.""" """Creates shadow variables for the given model weights."""
for var in model.weights:
if self._trainable_weights_only:
self._model_weights = model.trainable_variables
else:
self._model_weights = model.variables
for var in self._model_weights:
self.add_slot(var, 'average', initializer='zeros') self.add_slot(var, 'average', initializer='zeros')
self._average_weights = [ self._average_weights = [
self.get_slot(var, 'average') for var in model.weights self.get_slot(var, 'average') for var in self._model_weights
] ]
self._model_weights = model.weights
@property @property
def has_shadow_copy(self): def has_shadow_copy(self):
......
...@@ -1316,8 +1316,8 @@ class AXgProcessor(DataProcessor): ...@@ -1316,8 +1316,8 @@ class AXgProcessor(DataProcessor):
return examples return examples
class SuperGLUERTEProcessor(DataProcessor): class SuperGLUEDataProcessor(DataProcessor):
"""Processor for the RTE dataset (SuperGLUE version).""" """Processor for the SuperGLUE dataset."""
def get_train_examples(self, data_dir): def get_train_examples(self, data_dir):
"""See base class.""" """See base class."""
...@@ -1334,6 +1334,70 @@ class SuperGLUERTEProcessor(DataProcessor): ...@@ -1334,6 +1334,70 @@ class SuperGLUERTEProcessor(DataProcessor):
return self._create_examples( return self._create_examples(
self._read_jsonl(os.path.join(data_dir, "test.jsonl")), "test") self._read_jsonl(os.path.join(data_dir, "test.jsonl")), "test")
def _create_examples(self, lines, set_type):
"""Creates examples for the training/dev/test sets."""
raise NotImplementedError()
class BoolQProcessor(SuperGLUEDataProcessor):
"""Processor for the BoolQ dataset (SuperGLUE diagnostics dataset)."""
def get_labels(self):
"""See base class."""
return ["True", "False"]
@staticmethod
def get_processor_name():
"""See base class."""
return "BoolQ"
def _create_examples(self, lines, set_type):
"""Creates examples for the training/dev/test sets."""
examples = []
for line in lines:
guid = "%s-%s" % (set_type, self.process_text_fn(str(line["idx"])))
text_a = self.process_text_fn(line["question"])
text_b = self.process_text_fn(line["passage"])
if set_type == "test":
label = "False"
else:
label = str(line["label"])
examples.append(
InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
return examples
class CBProcessor(SuperGLUEDataProcessor):
"""Processor for the CB dataset (SuperGLUE diagnostics dataset)."""
def get_labels(self):
"""See base class."""
return ["entailment", "neutral", "contradiction"]
@staticmethod
def get_processor_name():
"""See base class."""
return "CB"
def _create_examples(self, lines, set_type):
"""Creates examples for the training/dev/test sets."""
examples = []
for line in lines:
guid = "%s-%s" % (set_type, self.process_text_fn(str(line["idx"])))
text_a = self.process_text_fn(line["premise"])
text_b = self.process_text_fn(line["hypothesis"])
if set_type == "test":
label = "entailment"
else:
label = self.process_text_fn(line["label"])
examples.append(
InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
return examples
class SuperGLUERTEProcessor(SuperGLUEDataProcessor):
"""Processor for the RTE dataset (SuperGLUE version)."""
def get_labels(self): def get_labels(self):
"""See base class.""" """See base class."""
# All datasets are converted to 2-class split, where for 3-class datasets we # All datasets are converted to 2-class split, where for 3-class datasets we
......
...@@ -50,7 +50,7 @@ flags.DEFINE_enum( ...@@ -50,7 +50,7 @@ flags.DEFINE_enum(
"classification_task_name", "MNLI", [ "classification_task_name", "MNLI", [
"AX", "COLA", "IMDB", "MNLI", "MRPC", "PAWS-X", "QNLI", "QQP", "RTE", "AX", "COLA", "IMDB", "MNLI", "MRPC", "PAWS-X", "QNLI", "QQP", "RTE",
"SST-2", "STS-B", "WNLI", "XNLI", "XTREME-XNLI", "XTREME-PAWS-X", "SST-2", "STS-B", "WNLI", "XNLI", "XTREME-XNLI", "XTREME-PAWS-X",
"AX-g", "SUPERGLUE-RTE" "AX-g", "SUPERGLUE-RTE", "CB", "BoolQ"
], "The name of the task to train BERT classifier. The " ], "The name of the task to train BERT classifier. The "
"difference between XTREME-XNLI and XNLI is: 1. the format " "difference between XTREME-XNLI and XNLI is: 1. the format "
"of input tsv files; 2. the dev set for XTREME is english " "of input tsv files; 2. the dev set for XTREME is english "
...@@ -243,7 +243,11 @@ def generate_classifier_dataset(): ...@@ -243,7 +243,11 @@ def generate_classifier_dataset():
"ax-g": "ax-g":
classifier_data_lib.AXgProcessor, classifier_data_lib.AXgProcessor,
"superglue-rte": "superglue-rte":
classifier_data_lib.SuperGLUERTEProcessor classifier_data_lib.SuperGLUERTEProcessor,
"cb":
classifier_data_lib.CBProcessor,
"boolq":
classifier_data_lib.BoolQProcessor,
} }
task_name = FLAGS.classification_task_name.lower() task_name = FLAGS.classification_task_name.lower()
if task_name not in processors: if task_name not in processors:
......
...@@ -27,6 +27,7 @@ import tensorflow as tf ...@@ -27,6 +27,7 @@ import tensorflow as tf
from official.common import distribute_utils from official.common import distribute_utils
# Imports registered experiment configs. # Imports registered experiment configs.
from official.common import registry_imports # pylint: disable=unused-import
from official.core import exp_factory from official.core import exp_factory
from official.core import task_factory from official.core import task_factory
from official.core import train_lib from official.core import train_lib
...@@ -64,6 +65,8 @@ EVAL_METRIC_MAP = { ...@@ -64,6 +65,8 @@ EVAL_METRIC_MAP = {
AXG_CLASS_NAMES = ['entailment', 'not_entailment'] AXG_CLASS_NAMES = ['entailment', 'not_entailment']
RTE_CLASS_NAMES = ['entailment', 'not_entailment'] RTE_CLASS_NAMES = ['entailment', 'not_entailment']
CB_CLASS_NAMES = ['entailment', 'neutral', 'contradiction']
BOOLQ_CLASS_NAMES = ['True', 'False']
def _override_exp_config_by_file(exp_config, exp_config_files): def _override_exp_config_by_file(exp_config, exp_config_files):
...@@ -153,7 +156,9 @@ def _write_submission_file(task, seq_length): ...@@ -153,7 +156,9 @@ def _write_submission_file(task, seq_length):
write_fn = binary_helper.write_superglue_classification write_fn = binary_helper.write_superglue_classification
write_fn_map = { write_fn_map = {
'RTE': functools.partial(write_fn, class_names=RTE_CLASS_NAMES), 'RTE': functools.partial(write_fn, class_names=RTE_CLASS_NAMES),
'AX-g': functools.partial(write_fn, class_names=AXG_CLASS_NAMES) 'AX-g': functools.partial(write_fn, class_names=AXG_CLASS_NAMES),
'CB': functools.partial(write_fn, class_names=CB_CLASS_NAMES),
'BoolQ': functools.partial(write_fn, class_names=BOOLQ_CLASS_NAMES)
} }
logging.info('Predicting %s', FLAGS.test_input_path) logging.info('Predicting %s', FLAGS.test_input_path)
write_fn_map[FLAGS.task_name]( write_fn_map[FLAGS.task_name](
......
...@@ -16,8 +16,8 @@ When training on TPUs we use ...@@ -16,8 +16,8 @@ When training on TPUs we use
[TPUEmbedding layer](https://github.com/tensorflow/recommenders/blob/main/tensorflow_recommenders/layers/embedding/tpu_embedding_layer.py) [TPUEmbedding layer](https://github.com/tensorflow/recommenders/blob/main/tensorflow_recommenders/layers/embedding/tpu_embedding_layer.py)
for categorical features. TPU embedding supports large embedding tables with for categorical features. TPU embedding supports large embedding tables with
fast lookup, the size of embedding tables scales linearly with the size of TPU fast lookup, the size of embedding tables scales linearly with the size of TPU
pod. We can have up to 96 GB embedding tables for TPU v3-8 and 6.14 TB for pod. We can have up to 90 GB embedding tables for TPU v3-8 and 5.6 TB for
v3-512 and 24.6 TB for TPU Pod v3-2048. v3-512 and 22,4 TB for TPU Pod v3-2048.
The Model code is in The Model code is in
[TensorFlow Recommenders](https://github.com/tensorflow/recommenders/tree/main/tensorflow_recommenders/experimental/models) [TensorFlow Recommenders](https://github.com/tensorflow/recommenders/tree/main/tensorflow_recommenders/experimental/models)
...@@ -25,16 +25,30 @@ library, while input pipeline, configuration and training loop is here. ...@@ -25,16 +25,30 @@ library, while input pipeline, configuration and training loop is here.
## Prerequisites ## Prerequisites
To get started, download the code from TensorFlow models GitHub repository or To get started, download the code from TensorFlow models GitHub repository or
use the pre-installed Google Cloud VM. We also need to install [TensorFlow use the pre-installed Google Cloud VM.
Recommenders](https://www.tensorflow.org/recommenders) library.
```bash ```bash
git clone https://github.com/tensorflow/models.git git clone https://github.com/tensorflow/models.git
pip install -r models/official/requirements.txt
export PYTHONPATH=$PYTHONPATH:$(pwd)/models export PYTHONPATH=$PYTHONPATH:$(pwd)/models
``` ```
Make sure to use TensorFlow 2.4+. We also need to install
[TensorFlow Recommenders](https://www.tensorflow.org/recommenders) library.
If you are using [tf-nightly](https://pypi.org/project/tf-nightly/) make
sure to install
[tensorflow-recommenders](https://pypi.org/project/tensorflow-recommenders/)
without its dependancies by passing `--no-deps` argument.
For tf-nightly:
```bash
pip install tensorflow-recommenders --no-deps
```
For stable TensorFlow 2.4+ [releases](https://pypi.org/project/tensorflow/):
```bash
pip install tensorflow-recommenders
```
## Dataset ## Dataset
...@@ -98,10 +112,10 @@ export EXPERIMENT_NAME=my_experiment_name ...@@ -98,10 +112,10 @@ export EXPERIMENT_NAME=my_experiment_name
export BUCKET_NAME="gs://my_dlrm_bucket" export BUCKET_NAME="gs://my_dlrm_bucket"
export DATA_DIR="${BUCKET_NAME}/data" export DATA_DIR="${BUCKET_NAME}/data"
python3 official/recommendation/ranking/main.py --mode=train_and_eval \ python3 models/official/recommendation/ranking/train.py --mode=train_and_eval \
--model_dir=${BUCKET_NAME}/model_dirs/${EXPERIMENT_NAME} --params_override=" --model_dir=${BUCKET_NAME}/model_dirs/${EXPERIMENT_NAME} --params_override="
runtime: runtime:
distribution_strategy='tpu' distribution_strategy: 'tpu'
task: task:
use_synthetic_data: false use_synthetic_data: false
train_data: train_data:
...@@ -125,7 +139,7 @@ trainer: ...@@ -125,7 +139,7 @@ trainer:
checkpoint_interval: 100000 checkpoint_interval: 100000
validation_steps: 5440 validation_steps: 5440
train_steps: 256054 train_steps: 256054
steps_per_execution: 1000 steps_per_loop: 1000
" "
``` ```
......
...@@ -12,7 +12,6 @@ tensorflow-hub>=0.6.0 ...@@ -12,7 +12,6 @@ tensorflow-hub>=0.6.0
tensorflow-model-optimization>=0.4.1 tensorflow-model-optimization>=0.4.1
tensorflow-datasets tensorflow-datasets
tensorflow-addons tensorflow-addons
tensorflow-recommenders>=0.5.0
dataclasses;python_version<"3.7" dataclasses;python_version<"3.7"
gin-config gin-config
tf_slim>=1.1.0 tf_slim>=1.1.0
......
...@@ -31,8 +31,8 @@ py_test() { ...@@ -31,8 +31,8 @@ py_test() {
local exit_code=0 local exit_code=0
echo "===========Running Python test============" echo "===========Running Python test============"
# Skipping Ranking tests, TODO(b/189265753) remove it once the issue is fixed.
for test_file in `find official/ -name '*test.py' -print` for test_file in `find official/ -name '*test.py' -print | grep -v 'official/recommendation/ranking'`
do do
echo "####=======Testing ${test_file}=======####" echo "####=======Testing ${test_file}=======####"
${PY_BINARY} "${test_file}" ${PY_BINARY} "${test_file}"
......
...@@ -80,6 +80,7 @@ trainer: ...@@ -80,6 +80,7 @@ trainer:
optimizer_config: optimizer_config:
ema: ema:
average_decay: 0.9999 average_decay: 0.9999
trainable_weights_only: false
learning_rate: learning_rate:
cosine: cosine:
decay_steps: 73682 decay_steps: 73682
......
...@@ -227,7 +227,8 @@ def image_classification_imagenet_resnetrs() -> cfg.ExperimentConfig: ...@@ -227,7 +227,8 @@ def image_classification_imagenet_resnetrs() -> cfg.ExperimentConfig:
} }
}, },
'ema': { 'ema': {
'average_decay': 0.9999 'average_decay': 0.9999,
'trainable_weights_only': False,
}, },
'learning_rate': { 'learning_rate': {
'type': 'cosine', 'type': 'cosine',
......
...@@ -254,7 +254,12 @@ def video_classification_ucf101() -> cfg.ExperimentConfig: ...@@ -254,7 +254,12 @@ def video_classification_ucf101() -> cfg.ExperimentConfig:
'task.validation_data.is_training != None', 'task.validation_data.is_training != None',
'task.train_data.num_classes == task.validation_data.num_classes', 'task.train_data.num_classes == task.validation_data.num_classes',
]) ])
add_trainer(config, train_batch_size=64, eval_batch_size=16, train_epochs=100) add_trainer(
config,
train_batch_size=64,
eval_batch_size=16,
learning_rate=0.8,
train_epochs=100)
return config return config
......
...@@ -331,8 +331,8 @@ class YoloPAN(tf.keras.layers.Layer): ...@@ -331,8 +331,8 @@ class YoloPAN(tf.keras.layers.Layer):
Args: Args:
minimum_depth: `int` depth of the smallest branch of the FPN. minimum_depth: `int` depth of the smallest branch of the FPN.
inputs: `dict[str, tf.InputSpec]` of the shape of input args as a dictionary of inputs: `dict[str, tf.InputSpec]` of the shape of input args as a
lists. dictionary of lists.
Returns: Returns:
The unscaled depths of the FPN branches. The unscaled depths of the FPN branches.
...@@ -386,8 +386,8 @@ class YoloDecoder(tf.keras.Model): ...@@ -386,8 +386,8 @@ class YoloDecoder(tf.keras.Model):
kernel_regularizer=None, kernel_regularizer=None,
bias_regularizer=None, bias_regularizer=None,
**kwargs): **kwargs):
"""Yolo Decoder initialization function. A unified model that ties all decoder """Yolo Decoder initialization function. A unified model that ties all
components into a conditionally build YOLO decder. decoder components into a conditionally build YOLO decoder.
Args: Args:
input_specs: `dict[str, tf.InputSpec]`: input specs of each of the inputs input_specs: `dict[str, tf.InputSpec]`: input specs of each of the inputs
...@@ -409,7 +409,7 @@ class YoloDecoder(tf.keras.Model): ...@@ -409,7 +409,7 @@ class YoloDecoder(tf.keras.Model):
zero. zero.
kernel_initializer: kernel_initializer for convolutional layers. kernel_initializer: kernel_initializer for convolutional layers.
kernel_regularizer: tf.keras.regularizers.Regularizer object for Conv2D. kernel_regularizer: tf.keras.regularizers.Regularizer object for Conv2D.
bias_regularizer: tf.keras.regularizers.Regularizer object for Conv2d. bias_regularizer: tf.keras.regularizers.Regularizer object for Conv2D.
**kwargs: keyword arguments to be passed. **kwargs: keyword arguments to be passed.
""" """
......
...@@ -1152,8 +1152,8 @@ class SAM(tf.keras.layers.Layer): ...@@ -1152,8 +1152,8 @@ class SAM(tf.keras.layers.Layer):
def call(self, inputs, training=None): def call(self, inputs, training=None):
if self._use_pooling: if self._use_pooling:
depth_max = tf.reduce_max(inputs, axis=-1, keep_dims=True) depth_max = tf.reduce_max(inputs, axis=-1, keepdims=True)
depth_avg = tf.reduce_mean(inputs, axis=-1, keep_dims=True) depth_avg = tf.reduce_mean(inputs, axis=-1, keepdims=True)
input_maps = tf.concat([depth_avg, depth_max], axis=-1) input_maps = tf.concat([depth_avg, depth_max], axis=-1)
else: else:
input_maps = inputs input_maps = inputs
...@@ -1545,7 +1545,7 @@ class DarkRouteProcess(tf.keras.layers.Layer): ...@@ -1545,7 +1545,7 @@ class DarkRouteProcess(tf.keras.layers.Layer):
elif layer == 'spp': elif layer == 'spp':
self.layers.append(self._spp(self._filters, dark_conv_args)) self.layers.append(self._spp(self._filters, dark_conv_args))
elif layer == 'sam': elif layer == 'sam':
self.layers.append(self._sam(-1, _args)) self.layers.append(self._sam(-1, dark_conv_args))
self._lim = len(self.layers) self._lim = len(self.layers)
super().build(input_shape) super().build(input_shape)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment