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 plot methods in Make predictions section #898

Merged
merged 4 commits into from Aug 5, 2019

Conversation

@harshildarji
Copy link
Contributor

commented Aug 2, 2019

The current implementation of methods plot_image and plot_value_array works well when the size of predictions_array is greater than 1. But this implementation gives error while performing prediction on a single image which is not at 0th position in test_images.


For example, the current implementation of single image prediction uses the first image from the test_images,

# Grab an image from the test dataset
img = test_images[0]

print(img.shape)

which works well while plotting the value array graph (the first argument of the plot_value_array method is the index of the image being used for the test purpose, which in this case is 0),

plot_value_array(0, predictions_single, test_labels)
plt.xticks(range(10), class_names, rotation=45)
plt.show()

But, if we use an image other than the first one (let's say the second image from the test_image),

# Grab an image from the test dataset
img = test_images[1]

print(img.shape)

and try to plot the value array graph using,

plot_value_array(1, predictions_single, test_labels)
plt.xticks(range(10), class_names, rotation=45)
plt.show()

it will throw following error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-57-358696683377> in <module>
----> 1 plot_value_array(1, predictions_single, test_labels)
      2 plt.xticks(range(10), class_names, rotation=45)
      3 plt.show()

<ipython-input-48-290e8ffd30e7> in plot_value_array(i, predictions_array, true_label)
     19 
     20 def plot_value_array(i, predictions_array, true_label):
---> 21   predictions_array, true_label = predictions_array[i], true_label[i]
     22   plt.grid(False)
     23   plt.xticks([])

IndexError: index 1 is out of bounds for axis 0 with size 1

In this PR, I update both the methods and also its references to work perfectly even in case of using any test image to perform single image prediction.

Now, if you perform single image prediction (in this case, the second image from test_images), it will give you the following graph instead of throwing an error.

image

Let me know if further explanation (or proof) is required.
Thank you.

Best regards,
Harshil Darji

@harshildarji harshildarji requested review from lamberta and MarkDaoust as code owners Aug 2, 2019

@tfdocsbot

This comment has been minimized.

Copy link
Collaborator

commented Aug 2, 2019

Preview and run these notebook edits with Google Colab:

Notebook diffs available on ReviewNB.com.
@googlebot

This comment has been minimized.

Copy link

commented Aug 2, 2019

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added the cla: no label Aug 2, 2019

@harshildarji

This comment has been minimized.

Copy link
Contributor Author

commented Aug 2, 2019

@googlebot I signed it!

@googlebot

This comment has been minimized.

Copy link

commented Aug 2, 2019

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@googlebot googlebot added cla: yes and removed cla: no labels Aug 2, 2019

@lamberta lamberta requested a review from yashk2810 Aug 2, 2019

@lamberta

This comment has been minimized.

Copy link
Member

commented Aug 2, 2019

Hi Harshil,

With TF2 around the corner, we're no longer working TF1 docs. Can you see how this applies to here: https://github.com/tensorflow/docs/blob/master/site/en/r2/tutorials/keras/basic_classification.ipynb

Also, try downloading the ipynb notebook from Colab to make the diff easier to review.
Thanks!

@harshildarji harshildarji requested a review from brilee as a code owner Aug 2, 2019

@harshildarji

This comment has been minimized.

Copy link
Contributor Author

commented Aug 2, 2019

Hi @lamberta,
I checked the TF2 version of the basic_classification.ipynb and found that it also had the same problem. So I have also updated that file to work well with any image from the test_images to perform single image prediction without any errors.
Thank you.

harshildarji and others added some commits Aug 2, 2019

@lamberta lamberta added ready to pull and removed ready to pull labels Aug 5, 2019

@TensorFlow-Docs-Copybara TensorFlow-Docs-Copybara merged commit 5fe173b into tensorflow:master Aug 5, 2019

2 checks passed

cla/google All necessary CLAs are signed
import/copybara Change imported to the internal review system
Details

TensorFlow-Docs-Copybara pushed a commit that referenced this pull request Aug 5, 2019

Copybara-Service
Merge pull request #898 from harshildarji:master
PiperOrigin-RevId: 261763784
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.