|
| 1 | +""" |
| 2 | +This file is temporary, for testing with 2.X. |
| 3 | +We'll need to integrate a more robust testing pipeline and make this part of pytest |
| 4 | +before pushing to master. |
| 5 | +
|
| 6 | +This was tested with TensorFlow 2.1, by running |
| 7 | +`python tests/tensorflow2/test_keras.py` from the main directory. |
| 8 | +""" |
| 9 | +# Standard Library |
| 10 | +import shutil |
| 11 | + |
| 12 | +# Third Party |
| 13 | +import pytest |
| 14 | +import tensorflow.compat.v2 as tf |
| 15 | + |
| 16 | +# First Party |
| 17 | +import smdebug.tensorflow as smd |
| 18 | +from smdebug.core.collection import CollectionKeys |
| 19 | +from smdebug.tensorflow import SaveConfig |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(scope="function") |
| 23 | +def out_dir(): |
| 24 | + """ Use this method to construct an out_dir. |
| 25 | +
|
| 26 | + Then it will be automatically cleaned up for you, passed into the test method, and we'll have |
| 27 | + fewer folders lying around. |
| 28 | + """ |
| 29 | + out_dir = "/tmp/test" |
| 30 | + shutil.rmtree(out_dir, ignore_errors=True) |
| 31 | + return out_dir |
| 32 | + |
| 33 | + |
| 34 | +def helper_keras_fit( |
| 35 | + trial_dir, |
| 36 | + save_all=False, |
| 37 | + include_collections=None, |
| 38 | + reduction_config=None, |
| 39 | + save_config=None, |
| 40 | + hook=None, |
| 41 | + steps=None, |
| 42 | + add_callbacks=None, |
| 43 | + run_eagerly=False, |
| 44 | +): |
| 45 | + |
| 46 | + mnist = tf.keras.datasets.mnist |
| 47 | + (x_train, y_train), (x_test, y_test) = mnist.load_data() |
| 48 | + x_train, x_test = x_train / 255, x_test / 255 |
| 49 | + |
| 50 | + model = tf.keras.models.Sequential( |
| 51 | + [ |
| 52 | + tf.keras.layers.Flatten(input_shape=(28, 28)), |
| 53 | + tf.keras.layers.Dense(128, activation="relu"), |
| 54 | + tf.keras.layers.Dropout(0.2), |
| 55 | + tf.keras.layers.Dense(10, activation="softmax"), |
| 56 | + ] |
| 57 | + ) |
| 58 | + |
| 59 | + if hook is None: |
| 60 | + if save_config is None: |
| 61 | + save_config = SaveConfig(save_interval=3) |
| 62 | + |
| 63 | + hook = smd.KerasHook( |
| 64 | + trial_dir, |
| 65 | + save_config=save_config, |
| 66 | + save_all=save_all, |
| 67 | + include_collections=include_collections, |
| 68 | + reduction_config=reduction_config, |
| 69 | + ) |
| 70 | + |
| 71 | + if not save_all and include_collections is not None: |
| 72 | + for cname in hook.include_collections: |
| 73 | + if cname not in include_collections: |
| 74 | + hook.get_collection(cname).save_config = SaveConfig(end_step=0) |
| 75 | + |
| 76 | + opt = tf.keras.optimizers.Adam() |
| 77 | + |
| 78 | + opt = hook.wrap_optimizer(opt) |
| 79 | + model.compile( |
| 80 | + optimizer=opt, |
| 81 | + loss="sparse_categorical_crossentropy", |
| 82 | + metrics=["accuracy"], |
| 83 | + run_eagerly=run_eagerly, |
| 84 | + ) |
| 85 | + hooks = [] |
| 86 | + if add_callbacks: |
| 87 | + if "tensorboard" in add_callbacks: |
| 88 | + hooks.append( |
| 89 | + tf.keras.callbacks.TensorBoard( |
| 90 | + log_dir="/tmp/logs", histogram_freq=1, write_grads=True, write_images=True |
| 91 | + ) |
| 92 | + ) |
| 93 | + hooks.append(hook) |
| 94 | + |
| 95 | + if steps is None: |
| 96 | + steps = ["train"] |
| 97 | + for step in steps: |
| 98 | + if step == "train": |
| 99 | + model.fit(x_train, y_train, epochs=1, steps_per_epoch=10, callbacks=hooks, verbose=0) |
| 100 | + elif step == "eval": |
| 101 | + model.evaluate(x_test, y_test, steps=10, callbacks=hooks, verbose=0) |
| 102 | + elif step == "predict": |
| 103 | + model.predict(x_test[:100], callbacks=hooks, verbose=0) |
| 104 | + |
| 105 | + hook.close() |
| 106 | + |
| 107 | + |
| 108 | +def test_keras_fit_eager(out_dir, tf_eager_mode=True): |
| 109 | + test_include_collections = [ |
| 110 | + CollectionKeys.LOSSES, |
| 111 | + CollectionKeys.METRICS, |
| 112 | + CollectionKeys.WEIGHTS, |
| 113 | + CollectionKeys.BIASES, |
| 114 | + CollectionKeys.GRADIENTS, |
| 115 | + CollectionKeys.INPUTS, |
| 116 | + CollectionKeys.OUTPUTS, |
| 117 | + CollectionKeys.LAYERS, |
| 118 | + CollectionKeys.OPTIMIZER_VARIABLES, |
| 119 | + ] |
| 120 | + hook = smd.KerasHook(out_dir=out_dir, include_collections=test_include_collections) |
| 121 | + helper_keras_fit( |
| 122 | + include_collections=test_include_collections, |
| 123 | + trial_dir=out_dir, |
| 124 | + hook=hook, |
| 125 | + run_eagerly=tf_eager_mode, |
| 126 | + steps=["train", "eval", "predict", "train"], |
| 127 | + ) |
| 128 | + trial = smd.create_trial(path=out_dir) |
| 129 | + |
| 130 | + # We first assert that none of the collections we requested for are empty |
| 131 | + assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1 |
| 132 | + assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == 2 |
| 133 | + assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2 |
| 134 | + assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2 |
| 135 | + assert len(trial.tensor_names(collection=CollectionKeys.GRADIENTS)) == 4 |
| 136 | + assert len(trial.tensor_names(collection=CollectionKeys.INPUTS)) == 1 # 1 Model Input |
| 137 | + assert len(trial.tensor_names(collection=CollectionKeys.OUTPUTS)) == 2 # 2 Model outputs |
| 138 | + assert len(trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5 |
| 139 | + |
| 140 | + # We assert that all the tensors saved have a valid value |
| 141 | + for tname in trial.tensor_names(): |
| 142 | + assert trial.tensor(tname).value(0) is not None |
| 143 | + |
| 144 | + # We then analyse Layer Inputs and Layer Outputs |
| 145 | + # Check that output of layer is equal to the input of the next |
| 146 | + boolean_matrix = trial.tensor("flatten/outputs").value(0) == trial.tensor("dense/inputs").value( |
| 147 | + 0 |
| 148 | + ) |
| 149 | + assert boolean_matrix.all() |
| 150 | + boolean_matrix = trial.tensor("dense/outputs").value(0) == trial.tensor("dropout/inputs").value( |
| 151 | + 0 |
| 152 | + ) |
| 153 | + assert boolean_matrix.all() |
| 154 | + boolean_matrix = trial.tensor("dropout/outputs").value(0) == trial.tensor( |
| 155 | + "dense_1/inputs" |
| 156 | + ).value(0) |
| 157 | + assert boolean_matrix.all() |
| 158 | + |
| 159 | + |
| 160 | +def test_keras_fit_false(out_dir, tf_eager_mode=False): |
| 161 | + test_include_collections = [ |
| 162 | + CollectionKeys.LOSSES, |
| 163 | + CollectionKeys.METRICS, |
| 164 | + CollectionKeys.WEIGHTS, |
| 165 | + CollectionKeys.BIASES, |
| 166 | + CollectionKeys.GRADIENTS, |
| 167 | + CollectionKeys.INPUTS, |
| 168 | + CollectionKeys.OUTPUTS, |
| 169 | + CollectionKeys.LAYERS, |
| 170 | + CollectionKeys.OPTIMIZER_VARIABLES, |
| 171 | + ] |
| 172 | + hook = smd.KerasHook(out_dir=out_dir, include_collections=test_include_collections) |
| 173 | + helper_keras_fit( |
| 174 | + include_collections=test_include_collections, |
| 175 | + trial_dir=out_dir, |
| 176 | + hook=hook, |
| 177 | + run_eagerly=tf_eager_mode, |
| 178 | + steps=["train", "eval", "predict", "train"], |
| 179 | + ) |
| 180 | + trial = smd.create_trial(path=out_dir) |
| 181 | + |
| 182 | + # We first assert that none of the collections we requested for are empty |
| 183 | + assert len(trial.tensor_names(collection=CollectionKeys.LOSSES)) == 1 |
| 184 | + assert len(trial.tensor_names(collection=CollectionKeys.METRICS)) == 2 |
| 185 | + assert len(trial.tensor_names(collection=CollectionKeys.WEIGHTS)) == 2 |
| 186 | + assert len(trial.tensor_names(collection=CollectionKeys.BIASES)) == 2 |
| 187 | + assert len(trial.tensor_names(collection=CollectionKeys.GRADIENTS)) == 4 |
| 188 | + assert len(trial.tensor_names(collection=CollectionKeys.INPUTS)) == 1 # 1 Model Input |
| 189 | + assert len(trial.tensor_names(collection=CollectionKeys.OUTPUTS)) == 2 # 2 Model outputs |
| 190 | + assert len(trial.tensor_names(collection=CollectionKeys.OPTIMIZER_VARIABLES)) == 5 |
| 191 | + |
| 192 | + # We assert that all the tensors saved have a valid value |
| 193 | + for tname in trial.tensor_names(): |
| 194 | + assert trial.tensor(tname).value(0) is not None |
| 195 | + |
| 196 | + # We then analyse Layer Inputs and Layer Outputs |
| 197 | + # Check that output of layer is equal to the input of the next |
| 198 | + boolean_matrix = trial.tensor("flatten_1/outputs").value(0) == trial.tensor( |
| 199 | + "dense_2/inputs" |
| 200 | + ).value(0) |
| 201 | + assert boolean_matrix.all() |
| 202 | + boolean_matrix = trial.tensor("dense_2/outputs").value(0) == trial.tensor( |
| 203 | + "dropout_1/inputs" |
| 204 | + ).value(0) |
| 205 | + assert boolean_matrix.all() |
| 206 | + boolean_matrix = trial.tensor("dropout_1/outputs").value(0) == trial.tensor( |
| 207 | + "dense_3/inputs" |
| 208 | + ).value(0) |
| 209 | + assert boolean_matrix.all() |
0 commit comments