I am trying to plot misclassified images but I am facing an error saying: TypeError: Invalid shape () for image data
My validation is defined as follow:
validation = data.flow_from_directory(data_dir,
class_mode = "categorical",
target_size = (X, Y),
color_mode="rgb",
batch_size = BATCH_SIZE,
shuffle = False,
subset='validation',
seed = 42)
and my function to plot misclassified images is defined as follow:
predictions = model.predict(validation) # Vector of probabilities
pred_labels = np.argmax(predictions, axis = 1) # We take the highest probability
test_labels_vector = np.argmax(validation.classes)
def classification_evaluation(classification, predicted_labels, test_labels, test_images):
if classification== "correct":
indices_list = np.where(predicted_labels == validation.classes)[0]
else:
indices_list = np.where(predicted_labels!= validation.classes)[0]
test_images_filtered= [validation.classes[i] for i in indices_list]
images_labels_original= [validation.classes[i] for i in indices_list]
images_labels_predicted= [pred_labels[i] for i in indices_list]
print(f"{len(test_images_filtered)} images were classified {classification.upper()} out of a total of {len(test_images)} in the Test dataset")
unique, counts = np.unique(images_labels_original, return_counts=True)
for i in range(0,len(unique)):
print(f"for category {unique[i]} the number of {classification.upper()} classified images were: {counts[i]}")
# Plot some of the misclassified images
print("\n\n")
fig,ax=plt.subplots(5,2)
fig.suptitle(f"Sample of {classification.upper()} Classified Images", fontsize=20)
fig.set_size_inches(15,15)
for i in range(5):
for j in range (2):
l=random.randint(0,len(test_images_filtered))
ax[i,j].imshow(test_images_filtered[l])
ax[i,j].set_title("Predicted: "+str(images_labels_predicted[l])+"\n"+"Actual: "+str(images_labels_original[l]))
plt.tight_layout()
classification_evaluation("incorrect", pred_labels, test_labels_vector, validation.classes)
The track of the error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_17/2783498455.py in <module>
----> 1 classification_evaluation("incorrect", pred_labels, test_labels_vector, validation.classes)
/tmp/ipykernel_17/3357022360.py in classification_evaluation(classification, predicted_labels, test_labels, test_images)
21 for j in range (2):
22 l=random.randint(0,len(test_images_filtered))
---> 23 ax[i,j].imshow(test_images_filtered[l])
24 print(ax[i,j].imshow(test_images_filtered[l]))
25 ax[i,j].set_title("Predicted: "+str(images_labels_predicted[l])+"\n"+"Actual: "+str(images_labels_original[l]))
/opt/conda/lib/python3.7/site-packages/matplotlib/_api/deprecation.py in wrapper(*args, **kwargs)
457 "parameter will become keyword-only %(removal)s.",
458 name=name, obj_type=f"parameter of {func.__name__}()")
--> 459 return func(*args, **kwargs)
460
461 # Don't modify *func*'s signature, as boilerplate.py needs it.
/opt/conda/lib/python3.7/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
1412 def inner(ax, *args, data=None, **kwargs):
1413 if data is None:
-> 1414 return func(ax, *map(sanitize_sequence, args), **kwargs)
1415
1416 bound = new_sig.bind(ax, *args, **kwargs)
/opt/conda/lib/python3.7/site-packages/matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, interpolation_stage, filternorm, filterrad, resample, url, **kwargs)
5485 **kwargs)
5486
-> 5487 im.set_data(X)
5488 im.set_alpha(alpha)
5489 if im.get_clip_path() is None:
/opt/conda/lib/python3.7/site-packages/matplotlib/image.py in set_data(self, A)
714 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
715 raise TypeError("Invalid shape {} for image data"
--> 716 .format(self._A.shape))
717
718 if self._A.ndim == 3:
TypeError: Invalid shape () for image data
After changing, test_images_filtered= [validation.classes[i] for i in indices_list] to test_images_filtered= [validation.filepaths[i] for i in indices_list]
and:
ax[i,j].imshow(test_images_filtered[l])
to
ax[i,j].imshow(plt.imread(test_images_filtered[l]))
I have one image plotted but with an error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/tmp/ipykernel_17/2783498455.py in <module>
----> 1 classification_evaluation("incorrect", pred_labels, test_labels_vector, validation.classes)
/tmp/ipykernel_17/2305134943.py in classification_evaluation(classification, predicted_labels, test_labels, test_images)
23 l=random.randint(0,len(test_images_filtered))
24 #ax[i,j].imshow(test_images_filtered[l])
---> 25 ax[i,j].imshow(plt.imread(test_images_filtered[l]))
26 ax[i,j].set_title("Predicted: "+str(images_labels_predicted[l])+"\n"+"Actual: "+str(images_labels_original[l]))
27 plt.tight_layout()
IndexError: list index out of range