Python Forum
Classifying images using trained model
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classifying images using trained model
#1
Hello!

I have followed this TensorFlow tutorial to classify images using transfer learning approach. Using almost 16,000 classified images (with about 40/60 split of 1/0) I as able to achieve 95% accuracy on the hold out testing set. I then saved this trained model using pickle.

Now I would like to use this trained model to classify new images. But I am having difficulty understanding how to do so. I am seeking you advice on how to:

(1) Appropriately load the new images, and
(2) Pass those images for classification.

Thanks a lot for your help.
Reply
#2
I think I figured out the solution:

new_dir = os.path.join(PATH, 'New') # make sure there is at least one class sub-folder

new_dataset = image_dataset_from_directory(new_dir, shuffle=True, batch_size=BATCH_SIZE, image_size=IMG_SIZE)

new_dataset = new_dataset.prefetch(buffer_size=AUTOTUNE)

#Retrieve a batch of images from the test set
image_batch, label_batch = new_dataset.as_numpy_iterator().next()
predictions = model.predict_on_batch(image_batch).flatten()

# Apply a sigmoid since our model returns logits
predictions = tf.nn.sigmoid(predictions)
predictions = tf.where(predictions < 0.5, 0, 1)

print('Predictions:\n', predictions.numpy()) # drop labels as they are meaningless anyway

plt.figure(figsize=(25, 25))
for i in range(25):
  ax = plt.subplot(5, 5, i + 1)
  plt.imshow(image_batch[i].astype("uint8"))
  plt.title(class_names[predictions[i]])
  plt.axis("off")
Reply
#3
Okay, I just realized that the aforementioned code only processes a single batch of 32 images. But how do I run multiple batches for large number of files?.. Back to the drawing board. Feedback is welcome :-)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiprocessing with loading well-trained model. (via Python3) Chris2013 0 2,195 Jun-06-2018, 06:48 AM
Last Post: Chris2013

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020