How understand image_dataset_from_directory() and use it as X, y input?

Question:

I made a simple CNN and to allocate datasets I used image_dataset_from_directory() func

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

test_ds = tf.keras.preprocessing.image_dataset_from_directory(
  check_dir,
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

Now I was interested to create graphs like RocCurve and confusion_matrix, but I can’t understand what should I put as an input?

Asked By: Sanjar Tlepin

||

Answers:

Depends basically on what you want and what do you use to create it.

For example to create a confusion matrix you can use the confusion_matrix functions provided by sklearn (link here). In this case the function want as input the true label and the predicted label

Answered By: Alberto Zancanaro

I found the solution, if someone will face this problem

If you create cycle

for x, y in test_ds:
...
# Code
...

x – numpy array of images
y – numpy array of true labels

UPD:

predictions = np.array([]) 
labels =  np.array([]) 
scores = np.array([]) 
////////////
for x, y in test_ds:   
    predictions = np.concatenate([predictions, np.argmax(new_model.predict(x), axis=1)])   
    labels = np.concatenate([labels, y.numpy()])   
    scores = np.concatenate([scores, new_model.predict(x)[:, 1]])
Answered By: Sanjar Tlepin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.