Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the Segmentation Tutorial Colab to be compatible with Keras 3 #2300

Conversation

kinarr
Copy link
Contributor

@kinarr kinarr commented Apr 8, 2024

I've identified a couple of issues that occur under specific conditions when using TensorFlow 2.16 and Keras 3's plotting utilities, along with their respective solutions.

Issue 1: AttributeError with plot_model

  • Issue: When executing tf.keras.utils.plot_model(model, show_shapes=True), users encounter an AttributeError stating that a 'list' object has no attribute 'shape'.
  • Fix: Enhancing the plot_model function call by adding keyword arguments expand_nested=True and dpi=64. This not only resolves the AttributeError but also improves the visualization output (might be a little verbose but works without needing any changes to the plotting APIs). The adjusted function call looks like this:
tf.keras.utils.plot_model(model, show_shapes=True, expand_nested=True, dpi=64)

Issue 2: AttributeError with SparseCategoricalCrossentropy

  • Issue: The following code sequence leads to an AttributeError similar to Issue 1, indicating a 'list' object has no attribute 'shape':
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True, reduction=tf.keras.losses.Reduction.NONE)
loss(label, prediction, sample_weight).numpy()

This occurs when label and prediction are defined as lists, e.g., label = [0,0] and prediction = [[-3., 0], [-3, 0]].

  • Fix: Converting label and prediction objects into numpy arrays resolves the issue. The corrected definitions should be as follows:
label = np.array([0,0])
prediction = np.array([[-3., 0], [-3, 0]])

Summary of Changes:

  • Added expand_nested=True and dpi=64 keyword arguments to plot_model for enhanced visualization and error prevention.
  • Converted y_true and y_pred objects into numpy arrays before passing them to SparseCategoricalCrossentropy to fix the AttributeError.

- Added kwargs (expand_nested=True, dpi=64) to `plot_model`
- Convert the y_true, y_pred objects into numpy arrays
@kinarr kinarr requested a review from a team as a code owner April 8, 2024 14:09
Copy link

github-actions bot commented Apr 8, 2024

Preview

Preview and run these notebook edits with Google Colab: Rendered notebook diffs available on ReviewNB.com.

Format and style

Use the TensorFlow docs notebook tools to format for consistent source diffs and lint for style:
$ python3 -m pip install -U --user git+https://github.com/tensorflow/docs

$ python3 -m tensorflow_docs.tools.nbfmt notebook.ipynb
$ python3 -m tensorflow_docs.tools.nblint --arg=repo:tensorflow/docs notebook.ipynb
If commits are added to the pull request, synchronize your local branch: git pull origin image-segmentation-tutorial-keras-3-update

@8bitmp3 8bitmp3 self-assigned this Apr 9, 2024
@8bitmp3 8bitmp3 assigned markmcd and MarkDaoust and unassigned 8bitmp3 Apr 11, 2024
Copy link
Contributor

@8bitmp3 8bitmp3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@8bitmp3 8bitmp3 added the ready to pull Start merge process label Apr 11, 2024
@copybara-service copybara-service bot merged commit 94fd565 into tensorflow:master Apr 12, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready to pull Start merge process
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants