Eerik Sven Puudist
Eerik Sven Puudist

Reputation: 2346

TensorFlow: classify image

I am following the tutorial regarding image classification with TensorFlow 2.0: https://www.tensorflow.org/tutorials/images/classification

The tutorial shows how to build and train a model, but I do not understand, how to actually use the model.

What I am looking for is a way to pass an in an image (preferably just its path) and get back some kind of classification result. Something like this:

result = model.evaluate('path/to/image.jpg')
# result == {'cat': 0.92, 'dog': 0.08}

How to implement this? Also, where is the model saved and how to access it after the training is complete?

Upvotes: 2

Views: 145

Answers (1)

Enthus3d
Enthus3d

Reputation: 2165

For the specific case of printing out a result of the percentage chance of an image being either X% cat, %Y dog, this particular tensorflow tutorial might be more useful.

In it, they do go through how to plot out the percentage likelihoods, as well as most of the basics of using tensorflow.

After you train your model, you can use some more code to show the results in a graphical way, like the following code from the tutorial:

def plot_image(i, predictions_array, true_label, img):
  predictions_array, true_label, img = predictions_array, true_label[i], img[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img, cmap=plt.cm.binary)

  predicted_label = np.argmax(predictions_array)
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'

  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
  predictions_array, true_label = predictions_array, true_label[i]
  plt.grid(False)
  plt.xticks(range(10))
  plt.yticks([])
  thisplot = plt.bar(range(10), predictions_array, color="#777777")
  plt.ylim([0, 1])
  predicted_label = np.argmax(predictions_array)

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')

Then, with the following code, you can make some plots about the results: enter image description here

As for accessing your model and saving it, the following tensorflow tutorial may be usefull.

Hope that helps!

Upvotes: 2

Related Questions