marcotcr / lime

Lime: Explaining the predictions of any machine learning classifier
BSD 2-Clause "Simplified" License
11.5k stars 1.79k forks source link

How to resolve the error!! #563

Closed akshay-gupta123 closed 3 years ago

akshay-gupta123 commented 3 years ago

Here is my code:

import face_detection
from lime import lime_image
detector1 = face_detection.build_detector('DSFDDetector',confidence_threshold=0.3,nms_iou_threshold=0.3)
def batch_img(images):
    pred = []
    for img in images:
      result = detector1.detect(img)
      results = [[i,1-i] for i in result[:,4]]
      pred.append(results)
    return np.array(pred)
image = cv2.imread('/content/image-mask-estimate/data/sample-images/2.jpg')[:,:,::-1]
explainer = lime_image.LimeImageExplainer()
x = np.array(image)
explanation = explainer.explain_instance(x, batch_img , top_labels=5, hide_color=0, num_samples=10)

I am getting this error:

/usr/local/lib/python3.6/dist-packages/lime/lime_base.py in explain_instance_with_data(self, neighborhood_data, neighborhood_labels, distances, label, num_features, feature_selection, model_regressor)
    180 
    181         weights = self.kernel_fn(distances)
--> 182         labels_column = neighborhood_labels[:, label]
    183         used_features = self.feature_selection(neighborhood_data,
    184                                                labels_column,

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
saraswat40 commented 3 years ago

@akshay-gupta123 did you find a solution to this? I am also running into this. Thanks

akshay-gupta123 commented 3 years ago

@saraswat40 LIME expects a fixed 2D output, but due to annotation model predicts different number of outputs. Try to fix the output size, it will work.

saraswat40 commented 3 years ago

@akshay-gupta123 thanks for responding. Yes, I think I got past this problem.

setthawut8 commented 2 years ago

@akshay-gupta123 @saraswat40 hi I got the same problem for multiclass text classification.

"IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed"

an example of the prediction based as a below image Screenshot 2022-07-27 150626

and codes for the lime as a below image. Screenshot 2022-07-27 150927

how to turn above prediction (or output) to be 2D output as you did or can I have your addressed codes.

Thank you for your helps.

Ra1491 commented 1 year ago

Did you solve this problem?

oussema9999 commented 1 year ago

@saraswat40 LIME expects a fixed 2D output, but due to annotation model predicts different number of outputs. Try to fix the output size, it will work.

can you please explain more ?