0
$\begingroup$

I am using keras flow from directory for image segmentation. Following are my codes

import os
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator 
from tensorflow import keras
from sklearn.metrics import classification_report, confusion_matrix
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from skimage import data, img_as_float
from skimage import exposure
SEED = 909
BATCH_SIZE_TRAIN = 8
BATCH_SIZE_TEST = 8

IMAGE_HEIGHT = 200
IMAGE_WIDTH = 200
IMG_SIZE = (IMAGE_HEIGHT, IMAGE_WIDTH)

NUM_TRAIN = 1320
NUM_TEST = 440

NUM_OF_EPOCHS = 50
def create_segmentation_generator_train(img_path, msk_path, BATCH_SIZE):
    data_gen_args = dict(rescale=1./255,
                         featurewise_center=False,
                         featurewise_std_normalization=False,
                         rotation_range=0,
                         width_shift_range=0.0,
                         height_shift_range=0.0,
                         zoom_range=0.0
                         )
    
    datagen = ImageDataGenerator(**data_gen_args)
    datagen1 = ImageDataGenerator(rescale=1./255)      
    
    img_generator = datagen.flow_from_directory(img_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
    msk_generator = datagen1.flow_from_directory(msk_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
    return zip(img_generator, msk_generator)


def create_segmentation_generator_test(img_path, msk_path, BATCH_SIZE):
    data_gen_args = dict(rescale=1./255)
    
    datagen = ImageDataGenerator(**data_gen_args)
        
    img_generator = datagen.flow_from_directory(img_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
    msk_generator = datagen.flow_from_directory(msk_path, target_size=IMG_SIZE, class_mode=None, color_mode='grayscale', batch_size=BATCH_SIZE, seed=SEED)
    return zip(img_generator, msk_generator)
train_generator = create_segmentation_generator_train(data_dir_train_image,data_dir_train_mask,BATCH_SIZE_TRAIN)
test_generator  = create_segmentation_generator_test(data_dir_test_image,data_dir_test_mask, BATCH_SIZE_TEST)

On running deep learning model of U-net, I gave following codes to create a confusion matrix

Y_pred_test = model.predict_generator(test_generator, NUM_TEST // BATCH_SIZE_TEST+1)
y_pred_test = np.argmax(Y_pred_test, axis=1)
Y_actual_test = test_generator
y_actual_test = np.argmax(Y_pred_test,axis=1)
confusion_matrix(y_actual_test,y_pred_test)

I get a below error

ValueError                                Traceback (most recent call last)
<ipython-input-58-5316d65a4c5b> in <module>
----> 1 confusion_matrix(y_actual_test,y_pred_test)

~\Anaconda3\envs\tf-gpu1\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     61             extra_args = len(args) - len(all_args)
     62             if extra_args <= 0:
---> 63                 return f(*args, **kwargs)
     64 
     65             # extra_args > 0

~\Anaconda3\envs\tf-gpu1\lib\site-packages\sklearn\metrics\_classification.py in confusion_matrix(y_true, y_pred, labels, sample_weight, normalize)
    297 
    298     """
--> 299     y_type, y_true, y_pred = _check_targets(y_true, y_pred)
    300     if y_type not in ("binary", "multiclass"):
    301         raise ValueError("%s is not supported" % y_type)

~\Anaconda3\envs\tf-gpu1\lib\site-packages\sklearn\metrics\_classification.py in _check_targets(y_true, y_pred)
     98     # No metrics support "multiclass-multioutput" format
     99     if (y_type not in ["binary", "multiclass", "multilabel-indicator"]):
--> 100         raise ValueError("{0} is not supported".format(y_type))
    101 
    102     if y_type in ["binary", "multiclass"]:

ValueError: unknown is not supported

I am doing a single class image segmentation task and have used U-net architecture using keras library. I also checked type of y_actual_test and y_pred_test and see it as numpy.ndarray. What am I doing wrong?

Please help.

$\endgroup$

1 Answer 1

0
$\begingroup$

Semantic image segmentation is pixel level classification. If it is binary class(background and object) segmentation, output of the trained model will be a 2D array of size(width and height) same as input image.Each element in the array is confidence of prediction of each corresponding pixel in the input image. Pixels of detected object will have a high-confidence predictions.

You can create binary output mask by thresholding the confidence of prediction . for example :-

  1. confidence_th = 0.8
  2. Y_pred_test = seg_model.predict(test_image)
  3. Y_pred_test = Y_pred_test > confidence_th
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.