1

I am trying to classify cancers into benign and malignant using a pre-trained model Resnet50.

Here is the code for the model.

data_path = '/content/drive/My Drive/data'
data_dir_list = os.listdir(data_path)

img_data_list = []    

for dataclass in data_dir_list:
    img_list = os.listdir(data_path+"/"+dataclass)
    for img in img_list:
        img_path = data_path+"/"+dataclass+"/"+img
        img=image.load_img(img_path, target_size=(224, 224))
        x=image.img_to_array(img)
        x=np.expand_dims(x,axis=0)
        x=preprocess_input(x)
        img_data_list.append(x)
        img_data=np.array(img_data_list)

        img_data=np.rollaxis(img_data,1,0)
        img_data= img_data[0]

        num_classes=2
        num_of_sample=250
        labels=np.ones(250,dtype='int64')
        labels[0:100]=0
        labels[100:250]=1
        names=['benign', 'malignant']

        Y=np_utils.to_categorical(labels, num_classes)
        x,y = shuffle(img_data, Y, random_state=3)
        X_train,X_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=3)

        image_input = Input(shape=(224,224,3))

        model = ResNet50(input_tensor=image_input, include_top='True', weights='imagenet')
        last_layer = model.get_layer('avg_pool').output
        x=Flatten(name='flattern')(last_layer)
        out=Dense(2, activation='softmax', name='output_layer')(x)

        custom_resnet_model = Model(inputs = image_input, outputs=out)

        for layer in custom_resnet_model.layers[:-1]:
            layer.trainable = False

            custom_resnet_model.layers[-1].trainable

            custom_resnet_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

            hist=custom_resnet_model.fit(X_train,y_train, batch_size=32, epochs=100, steps_per_epoch=7,  
            validation_data=(X_test,y_test))

Then I tried to create a confusion matrix for this model using scikit learn. But I am getting an error.

Here is the code for the confusion matrix.

prediction = model.predict_generator(test_set, steps=2 )

rounded_prediction = np.argmax(prediction, axis=-1)

cm = confusion_matrix(y_true=y_test , y_pred=rounded_prediction)

Error:

ValueError                                Traceback (most recent call last)
<ipython-input-96-1965da59b466> in <module>()
----> 1 cm = confusion_matrix(y_true=y_test , y_pred=rounded_prediction)

1 frames
/usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py in _check_targets(y_true, y_pred)
     88     if len(y_type) > 1:
     89         raise ValueError("Classification metrics can't handle a mix of {0} "
---> 90                          "and {1} targets".format(type_true, type_pred))
     91 
     92     # We can't have more than one value on y_type => The set is no more needed

ValueError: Classification metrics can't handle a mix of multilabel-indicator and binary targets

As I think there is an issue with y_true or y_pred. what is the value for this parameters according to my model. Here I used rounded_prediction for selecting most probable prediction for each sample.

Data types:

type(y_test)
numpy.ndarray

type(rounded_prediction)
numpy.ndarray
Savin
  • 47
  • 11
  • What does `y_test` contain? Can you add few lines from it? – Frightera Feb 27 '21 at 08:38
  • What are you trying to do with this line `rounded_prediction = prediction.argmax(axis=1)`? – David M. Feb 27 '21 at 08:46
  • @Frightera `y_test` contain label for testing data set like `[[1. 0.] [0. 1.] [1. 0.].... ` – Savin Feb 27 '21 at 10:52
  • @DavidM. rounded_prediction for selecting **most probable prediction** for each sample. – Savin Feb 27 '21 at 10:54
  • 2
    You need also `argmax` your `y_test`. Please refer [this answer](https://stackoverflow.com/questions/66386561/keras-accuracy-is-different-between-model-and-classification-report-for-multi-cl/66389499#66389499) and upvote if it is useful for you. – Frightera Feb 27 '21 at 11:02

0 Answers0