From 76fbc8d786c110ba660288eee3476f2566ffdc13 Mon Sep 17 00:00:00 2001 From: Thomas Coldwell <31568400+thomas-coldwell@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:05:43 +0100 Subject: [PATCH 1/2] fix: Display gallery with arbitrary number of images --- .gitignore | 3 +++ .python-version | 1 + .../preprocessing/bounding_box/demo_utils.py | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 .python-version diff --git a/.gitignore b/.gitignore index 566d9d109e..110279bcbe 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ wheels .devcontainer/ .coverage .history + +# Common env config file use by pyenv +.python_version diff --git a/.python-version b/.python-version new file mode 100644 index 0000000000..14348698da --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +keras diff --git a/examples/layers/preprocessing/bounding_box/demo_utils.py b/examples/layers/preprocessing/bounding_box/demo_utils.py index e0dad414bf..462b35d3d7 100644 --- a/examples/layers/preprocessing/bounding_box/demo_utils.py +++ b/examples/layers/preprocessing/bounding_box/demo_utils.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. """Utility functions for preprocessing demos.""" +import math + import matplotlib.pyplot as plt import numpy as np import tensorflow as tf @@ -84,9 +86,18 @@ def visualize_bounding_boxes(image, bounding_boxes, bounding_box_format): def gallery_show(images): images = images.astype(int) - for i in range(9): + image_count = len(images) + rows = 1 + cols = 1 + if image_count > 1: + cols = 2 + if image_count > 2: + cols = 3 + if image_count > 3: + rows = math.ceil(image_count / 3) + for i in range(len(images)): image = images[i] - plt.subplot(3, 3, i + 1) + plt.subplot(rows, cols, i + 1) plt.imshow(image.astype("uint8")) plt.axis("off") plt.show() From 263ec2f947df756081988ac6aaa5cabdb026e3fd Mon Sep 17 00:00:00 2001 From: Thomas Coldwell <31568400+thomas-coldwell@users.noreply.github.com> Date: Mon, 19 Aug 2024 20:13:05 +0100 Subject: [PATCH 2/2] fix: Reduce hardcoded if statements --- .../layers/preprocessing/bounding_box/demo_utils.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/examples/layers/preprocessing/bounding_box/demo_utils.py b/examples/layers/preprocessing/bounding_box/demo_utils.py index 462b35d3d7..2eb9cad71f 100644 --- a/examples/layers/preprocessing/bounding_box/demo_utils.py +++ b/examples/layers/preprocessing/bounding_box/demo_utils.py @@ -87,14 +87,9 @@ def visualize_bounding_boxes(image, bounding_boxes, bounding_box_format): def gallery_show(images): images = images.astype(int) image_count = len(images) - rows = 1 - cols = 1 - if image_count > 1: - cols = 2 - if image_count > 2: - cols = 3 - if image_count > 3: - rows = math.ceil(image_count / 3) + max_columns = 3 + cols = min(image_count, max_columns) + rows = math.ceil(image_count / max_columns) for i in range(len(images)): image = images[i] plt.subplot(rows, cols, i + 1)