Python Forum
Classifying images using trained model - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Classifying images using trained model (/thread-31307.html)



Classifying images using trained model - kiton - Dec-03-2020

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.


RE: Classifying images using trained model - kiton - Dec-08-2020

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")



RE: Classifying images using trained model - kiton - Dec-08-2020

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 :-)