Skip to content

Commit 550a64d

Browse files
Suppress training output warnings and remove intermediate epoch PNG visualizations in Focal Modulation Network tutorial
1 parent 2414199 commit 550a64d

9 files changed

+65
-94
lines changed

examples/vision/focal_modulation_network.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,26 @@
7878
import os
7979

8080
# Set backend before importing keras
81-
os.environ["KERAS_BACKEND"] = "tensorflow" # Or "torch" or "tensorflow"
81+
os.environ["KERAS_BACKEND"] = "tensorflow" # or "torch" or "jax"
82+
# Suppress TensorFlow C++ logging (XLA messages)
83+
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
8284

8385
import numpy as np
8486
import keras
8587
from keras import layers
8688
from keras import ops
8789
from matplotlib import pyplot as plt
8890
from random import randint
91+
import warnings
92+
import logging
93+
94+
# Suppress warnings
95+
warnings.filterwarnings("ignore", category=UserWarning)
96+
warnings.filterwarnings("ignore", message=".*build.*method.*")
97+
warnings.filterwarnings("ignore", message=".*tf.function.*retracing.*")
98+
99+
# Suppress TensorFlow logging
100+
logging.getLogger("tensorflow").setLevel(logging.ERROR)
89101

90102
# Set seed for reproducibility using Keras 3 utility.
91103
keras.utils.set_random_seed(42)
@@ -837,7 +849,7 @@ def __call__(self, step):
837849
train_ds,
838850
validation_data=val_ds,
839851
epochs=EPOCHS,
840-
callbacks=[TrainMonitor(epoch_interval=5)],
852+
callbacks=[],
841853
)
842854

843855
"""
-701 Bytes
Loading
207 Bytes
Loading
-2.3 KB
Loading
751 Bytes
Loading
484 Bytes
Loading
3.8 KB
Loading

examples/vision/ipynb/focal_modulation_network.ipynb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,26 @@
102102
"import os\n",
103103
"\n",
104104
"# Set backend before importing keras\n",
105-
"os.environ[\"KERAS_BACKEND\"] = \"tensorflow\" # Or \"torch\" or \"tensorflow\"\n",
105+
"os.environ[\"KERAS_BACKEND\"] = \"tensorflow\" # or \"torch\" or \"jax\"\n",
106+
"# Suppress TensorFlow C++ logging (XLA messages)\n",
107+
"os.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"3\"\n",
106108
"\n",
107109
"import numpy as np\n",
108110
"import keras\n",
109111
"from keras import layers\n",
110112
"from keras import ops\n",
111113
"from matplotlib import pyplot as plt\n",
112114
"from random import randint\n",
115+
"import warnings\n",
116+
"import logging\n",
117+
"\n",
118+
"# Suppress warnings\n",
119+
"warnings.filterwarnings(\"ignore\", category=UserWarning)\n",
120+
"warnings.filterwarnings(\"ignore\", message=\".*build.*method.*\")\n",
121+
"warnings.filterwarnings(\"ignore\", message=\".*tf.function.*retracing.*\")\n",
122+
"\n",
123+
"# Suppress TensorFlow logging\n",
124+
"logging.getLogger(\"tensorflow\").setLevel(logging.ERROR)\n",
113125
"\n",
114126
"# Set seed for reproducibility using Keras 3 utility.\n",
115127
"keras.utils.set_random_seed(42)"
@@ -1059,7 +1071,7 @@
10591071
" train_ds,\n",
10601072
" validation_data=val_ds,\n",
10611073
" epochs=EPOCHS,\n",
1062-
" callbacks=[TrainMonitor(epoch_interval=5)],\n",
1074+
" callbacks=[],\n",
10631075
")"
10641076
]
10651077
},

examples/vision/md/focal_modulation_network.md

Lines changed: 37 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,26 @@ Keras 3 allows this model to run on JAX, PyTorch, or TensorFlow. We use keras.op
8080
import os
8181

8282
# Set backend before importing keras
83-
os.environ["KERAS_BACKEND"] = "tensorflow" # Or "torch" or "tensorflow"
83+
os.environ["KERAS_BACKEND"] = "tensorflow" # or "torch" or "jax"
84+
# Suppress TensorFlow C++ logging (XLA messages)
85+
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
8486

8587
import numpy as np
8688
import keras
8789
from keras import layers
8890
from keras import ops
8991
from matplotlib import pyplot as plt
9092
from random import randint
93+
import warnings
94+
import logging
95+
96+
# Suppress warnings
97+
warnings.filterwarnings("ignore", category=UserWarning)
98+
warnings.filterwarnings("ignore", message=".*build.*method.*")
99+
warnings.filterwarnings("ignore", message=".*tf.function.*retracing.*")
100+
101+
# Suppress TensorFlow logging
102+
logging.getLogger("tensorflow").setLevel(logging.ERROR)
91103

92104
# Set seed for reproducibility using Keras 3 utility.
93105
keras.utils.set_random_seed(42)
@@ -847,161 +859,96 @@ history = model.fit(
847859
train_ds,
848860
validation_data=val_ds,
849861
epochs=EPOCHS,
850-
callbacks=[TrainMonitor(epoch_interval=5)],
862+
callbacks=[],
851863
)
852864
```
853865

854866
<div class="k-default-codeblock">
855867
```
856868
Epoch 1/20
857869
858-
/Users/lakshmikala/node2vec_env/lib/python3.12/site-packages/keras/src/layers/layer.py:424: UserWarning: `build()` was called on layer 'focal_modulation_block', however the layer does not have a `build()` method implemented and it looks like it has unbuilt state. This will cause the layer to be marked as built, despite not being actually built, which may cause failures down the line. Make sure to implement a proper `build()` method.
859-
warnings.warn(
860-
/Users/lakshmikala/node2vec_env/lib/python3.12/site-packages/keras/src/layers/layer.py:424: UserWarning: `build()` was called on layer 'focal_modulation_block_1', however the layer does not have a `build()` method implemented and it looks like it has unbuilt state. This will cause the layer to be marked as built, despite not being actually built, which may cause failures down the line. Make sure to implement a proper `build()` method.
861-
warnings.warn(
862-
863-
/Users/lakshmikala/node2vec_env/lib/python3.12/site-packages/keras/src/layers/layer.py:424: UserWarning: `build()` was called on layer 'focal_modulation_block_2', however the layer does not have a `build()` method implemented and it looks like it has unbuilt state. This will cause the layer to be marked as built, despite not being actually built, which may cause failures down the line. Make sure to implement a proper `build()` method.
864-
warnings.warn(
865-
/Users/lakshmikala/node2vec_env/lib/python3.12/site-packages/keras/src/layers/layer.py:424: UserWarning: `build()` was called on layer 'focal_modulation_block_3', however the layer does not have a `build()` method implemented and it looks like it has unbuilt state. This will cause the layer to be marked as built, despite not being actually built, which may cause failures down the line. Make sure to implement a proper `build()` method.
866-
warnings.warn(
867-
868-
/Users/lakshmikala/node2vec_env/lib/python3.12/site-packages/keras/src/layers/layer.py:424: UserWarning: `build()` was called on layer 'focal_modulation_block_4', however the layer does not have a `build()` method implemented and it looks like it has unbuilt state. This will cause the layer to be marked as built, despite not being actually built, which may cause failures down the line. Make sure to implement a proper `build()` method.
869-
warnings.warn(
870-
871-
/Users/lakshmikala/node2vec_env/lib/python3.12/site-packages/keras/src/layers/layer.py:424: UserWarning: `build()` was called on layer 'focal_modulation_block_5', however the layer does not have a `build()` method implemented and it looks like it has unbuilt state. This will cause the layer to be marked as built, despite not being actually built, which may cause failures down the line. Make sure to implement a proper `build()` method.
872-
warnings.warn(
873-
/Users/lakshmikala/node2vec_env/lib/python3.12/site-packages/keras/src/layers/layer.py:424: UserWarning: `build()` was called on layer 'focal_modulation_block_6', however the layer does not have a `build()` method implemented and it looks like it has unbuilt state. This will cause the layer to be marked as built, despite not being actually built, which may cause failures down the line. Make sure to implement a proper `build()` method.
874-
warnings.warn(
875-
876-
/Users/lakshmikala/node2vec_env/lib/python3.12/site-packages/keras/src/layers/layer.py:424: UserWarning: `build()` was called on layer 'focal_modulation_network', however the layer does not have a `build()` method implemented and it looks like it has unbuilt state. This will cause the layer to be marked as built, despite not being actually built, which may cause failures down the line. Make sure to implement a proper `build()` method.
877-
warnings.warn(
878-
879870
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
880-
I0000 00:00:1770700186.220793 2002752 service.cc:152] XLA service 0x16cf639d0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
881-
I0000 00:00:1770700186.220808 2002752 service.cc:160] StreamExecutor device (0): Host, Default Version
882-
I0000 00:00:1770700186.251643 2002752 device_compiler.h:188] Compiled cluster using XLA! This line is logged at most once for the lifetime of the process.
871+
I0000 00:00:1772431619.408199 4985427 service.cc:152] XLA service 0x166d7d250 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
872+
I0000 00:00:1772431619.408213 4985427 service.cc:160] StreamExecutor device (0): Host, Default Version
873+
I0000 00:00:1772431619.566351 4985427 device_compiler.h:188] Compiled cluster using XLA! This line is logged at most once for the lifetime of the process.
883874
884-
313/313 ━━━━━━━━━━━━━━━━━━━━ 312s 964ms/step - accuracy: 0.1826 - loss: 2.1990 - val_accuracy: 0.2426 - val_loss: 2.0434
875+
313/313 ━━━━━━━━━━━━━━━━━━━━ 310s 958ms/step - accuracy: 0.1826 - loss: 2.1990 - val_accuracy: 0.2426 - val_loss: 2.0434
885876
886877
Epoch 2/20
887878
888-
313/313 ━━━━━━━━━━━━━━━━━━━━ 302s 964ms/step - accuracy: 0.2891 - loss: 1.8906 - val_accuracy: 0.3191 - val_loss: 1.8333
879+
313/313 ━━━━━━━━━━━━━━━━━━━━ 301s 963ms/step - accuracy: 0.2891 - loss: 1.8906 - val_accuracy: 0.3191 - val_loss: 1.8333
889880
890881
Epoch 3/20
891882
892-
313/313 ━━━━━━━━━━━━━━━━━━━━ 303s 968ms/step - accuracy: 0.3669 - loss: 1.7095 - val_accuracy: 0.3869 - val_loss: 1.6693
883+
313/313 ━━━━━━━━━━━━━━━━━━━━ 311s 994ms/step - accuracy: 0.3669 - loss: 1.7095 - val_accuracy: 0.3869 - val_loss: 1.6693
893884
894885
Epoch 4/20
895886
896-
313/313 ━━━━━━━━━━━━━━━━━━━━ 308s 984ms/step - accuracy: 0.4221 - loss: 1.5685 - val_accuracy: 0.4188 - val_loss: 1.5894
887+
313/313 ━━━━━━━━━━━━━━━━━━━━ 305s 975ms/step - accuracy: 0.4221 - loss: 1.5685 - val_accuracy: 0.4188 - val_loss: 1.5894
897888
898889
Epoch 5/20
899890
900-
313/313 ━━━━━━━━━━━━━━━━━━━━ 0s 905ms/step - accuracy: 0.4501 - loss: 1.5031
901-
902-
WARNING:tensorflow:5 out of the last 5 calls to <function conv.<locals>._conv_xla at 0x3190abc40> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.
903-
904-
WARNING:tensorflow:6 out of the last 6 calls to <function conv.<locals>._conv_xla at 0x3190abd80> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.
905-
```
906-
</div>
907-
908-
![png](/img/examples/vision/focal_modulation_network/focal_modulation_network_33_1582.png)
909-
910-
911-
912-
<div class="k-default-codeblock">
913-
```
914-
313/313 ━━━━━━━━━━━━━━━━━━━━ 313s 1s/step - accuracy: 0.4618 - loss: 1.4759 - val_accuracy: 0.4519 - val_loss: 1.5107
891+
313/313 ━━━━━━━━━━━━━━━━━━━━ 301s 961ms/step - accuracy: 0.4618 - loss: 1.4759 - val_accuracy: 0.4519 - val_loss: 1.5107
915892
916893
Epoch 6/20
917894
918-
313/313 ━━━━━━━━━━━━━━━━━━━━ 316s 1s/step - accuracy: 0.4919 - loss: 1.4076 - val_accuracy: 0.4692 - val_loss: 1.4941
895+
313/313 ━━━━━━━━━━━━━━━━━━━━ 308s 984ms/step - accuracy: 0.4943 - loss: 1.3977 - val_accuracy: 0.4743 - val_loss: 1.5007
919896
920897
Epoch 7/20
921898
922-
313/313 ━━━━━━━━━━━━━━━━━━━━ 312s 997ms/step - accuracy: 0.5189 - loss: 1.3461 - val_accuracy: 0.5032 - val_loss: 1.3940
899+
313/313 ━━━━━━━━━━━━━━━━━━━━ 304s 971ms/step - accuracy: 0.5185 - loss: 1.3445 - val_accuracy: 0.5042 - val_loss: 1.3920
923900
924901
Epoch 8/20
925902
926-
313/313 ━━━━━━━━━━━━━━━━━━━━ 307s 981ms/step - accuracy: 0.5356 - loss: 1.3025 - val_accuracy: 0.5182 - val_loss: 1.3580
903+
313/313 ━━━━━━━━━━━━━━━━━━━━ 308s 985ms/step - accuracy: 0.5350 - loss: 1.3001 - val_accuracy: 0.5192 - val_loss: 1.3544
927904
928905
Epoch 9/20
929906
930-
313/313 ━━━━━━━━━━━━━━━━━━━━ 299s 954ms/step - accuracy: 0.5440 - loss: 1.2654 - val_accuracy: 0.5273 - val_loss: 1.3291
907+
313/313 ━━━━━━━━━━━━━━━━━━━━ 308s 984ms/step - accuracy: 0.5451 - loss: 1.2637 - val_accuracy: 0.5247 - val_loss: 1.3289
931908
932909
Epoch 10/20
933910
934-
313/313 ━━━━━━━━━━━━━━━━━━━━ 0s 866ms/step - accuracy: 0.5588 - loss: 1.2346
935-
```
936-
</div>
937-
938-
![png](/img/examples/vision/focal_modulation_network/focal_modulation_network_33_3158.png)
939-
940-
941-
942-
<div class="k-default-codeblock">
943-
```
944-
313/313 ━━━━━━━━━━━━━━━━━━━━ 301s 961ms/step - accuracy: 0.5600 - loss: 1.2305 - val_accuracy: 0.5273 - val_loss: 1.3158
911+
313/313 ━━━━━━━━━━━━━━━━━━━━ 306s 976ms/step - accuracy: 0.5607 - loss: 1.2300 - val_accuracy: 0.5312 - val_loss: 1.3112
945912
946913
Epoch 11/20
947914
948-
313/313 ━━━━━━━━━━━━━━━━━━━━ 302s 965ms/step - accuracy: 0.5741 - loss: 1.1958 - val_accuracy: 0.5248 - val_loss: 1.3298
915+
313/313 ━━━━━━━━━━━━━━━━━━━━ 299s 956ms/step - accuracy: 0.5730 - loss: 1.1985 - val_accuracy: 0.5354 - val_loss: 1.3262
949916
950917
Epoch 12/20
951918
952-
313/313 ━━━━━━━━━━━━━━━━━━━━ 302s 965ms/step - accuracy: 0.5836 - loss: 1.1713 - val_accuracy: 0.5500 - val_loss: 1.2602
919+
313/313 ━━━━━━━━━━━━━━━━━━━━ 304s 970ms/step - accuracy: 0.5802 - loss: 1.1739 - val_accuracy: 0.5484 - val_loss: 1.2850
953920
954921
Epoch 13/20
955922
956-
313/313 ━━━━━━━━━━━━━━━━━━━━ 297s 947ms/step - accuracy: 0.5900 - loss: 1.1483 - val_accuracy: 0.5626 - val_loss: 1.2348
923+
313/313 ━━━━━━━━━━━━━━━━━━━━ 312s 996ms/step - accuracy: 0.5905 - loss: 1.1494 - val_accuracy: 0.5640 - val_loss: 1.2334
957924
958925
Epoch 14/20
959926
960-
313/313 ━━━━━━━━━━━━━━━━━━━━ 304s 970ms/step - accuracy: 0.5987 - loss: 1.1270 - val_accuracy: 0.5657 - val_loss: 1.2249
927+
313/313 ━━━━━━━━━━━━━━━━━━━━ 307s 982ms/step - accuracy: 0.5973 - loss: 1.1309 - val_accuracy: 0.5641 - val_loss: 1.2307
961928
962929
Epoch 15/20
963930
964-
313/313 ━━━━━━━━━━━━━━━━━━━━ 0s 884ms/step - accuracy: 0.6118 - loss: 1.1106
965-
```
966-
</div>
967-
968-
![png](/img/examples/vision/focal_modulation_network/focal_modulation_network_33_4734.png)
969-
970-
971-
972-
<div class="k-default-codeblock">
973-
```
974-
313/313 ━━━━━━━━━━━━━━━━━━━━ 308s 982ms/step - accuracy: 0.6081 - loss: 1.1134 - val_accuracy: 0.5671 - val_loss: 1.2246
931+
313/313 ━━━━━━━━━━━━━━━━━━━━ 313s 999ms/step - accuracy: 0.6072 - loss: 1.1127 - val_accuracy: 0.5740 - val_loss: 1.2129
975932
976933
Epoch 16/20
977934
978-
313/313 ━━━━━━━━━━━━━━━━━━━━ 298s 954ms/step - accuracy: 0.6105 - loss: 1.0981 - val_accuracy: 0.5708 - val_loss: 1.2035
935+
313/313 ━━━━━━━━━━━━━━━━━━━━ 311s 992ms/step - accuracy: 0.6120 - loss: 1.0956 - val_accuracy: 0.5734 - val_loss: 1.2059
979936
980937
Epoch 17/20
981938
982-
313/313 ━━━━━━━━━━━━━━━━━━━━ 302s 964ms/step - accuracy: 0.6144 - loss: 1.0838 - val_accuracy: 0.5770 - val_loss: 1.2002
939+
313/313 ━━━━━━━━━━━━━━━━━━━━ 5871s 19s/step - accuracy: 0.6145 - loss: 1.0874 - val_accuracy: 0.5788 - val_loss: 1.1996
983940
984941
Epoch 18/20
985942
986-
313/313 ━━━━━━━━━━━━━━━━━━━━ 308s 984ms/step - accuracy: 0.6209 - loss: 1.0799 - val_accuracy: 0.5764 - val_loss: 1.1978
943+
313/313 ━━━━━━━━━━━━━━━━━━━━ 300s 959ms/step - accuracy: 0.6164 - loss: 1.0800 - val_accuracy: 0.5797 - val_loss: 1.1991
987944
988945
Epoch 19/20
989946
990-
313/313 ━━━━━━━━━━━━━━━━━━━━ 315s 1s/step - accuracy: 0.6174 - loss: 1.0772 - val_accuracy: 0.5777 - val_loss: 1.1951
947+
313/313 ━━━━━━━━━━━━━━━━━━━━ 297s 950ms/step - accuracy: 0.6212 - loss: 1.0737 - val_accuracy: 0.5828 - val_loss: 1.1924
991948
992949
Epoch 20/20
993950
994-
313/313 ━━━━━━━━━━━━━━━━━━━━ 0s 896ms/step - accuracy: 0.6249 - loss: 1.0723
995-
```
996-
</div>
997-
998-
![png](/img/examples/vision/focal_modulation_network/focal_modulation_network_33_6310.png)
999-
1000-
1001-
1002-
<div class="k-default-codeblock">
1003-
```
1004-
313/313 ━━━━━━━━━━━━━━━━━━━━ 311s 993ms/step - accuracy: 0.6240 - loss: 1.0710 - val_accuracy: 0.5775 - val_loss: 1.1971
951+
313/313 ━━━━━━━━━━━━━━━━━━━━ 297s 950ms/step - accuracy: 0.6204 - loss: 1.0702 - val_accuracy: 0.5822 - val_loss: 1.1950
1005952
```
1006953
</div>
1007954

0 commit comments

Comments
 (0)