Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError with AnchorExplainer and Yolov8 #1000

Open
knowitall12 opened this issue Feb 11, 2024 · 0 comments
Open

IndexError with AnchorExplainer and Yolov8 #1000

knowitall12 opened this issue Feb 11, 2024 · 0 comments

Comments

@knowitall12
Copy link

Hi,

I am trying to get anchor explainer working for fine-tuned yolov8 model.

Error:

image 1/1 /content/content/dataset/images/valid/HCL_0_idx3_xs130_xe250_res416.png: 416x416 1 morning_star, 3632.7ms
Speed: 1.5ms preprocess, 3632.7ms inference, 1.0ms postprocess per image at shape (1, 3, 416, 416)

image 1/1 /content/content/dataset/images/valid/HCL_0_idx3_xs130_xe250_res416.png: 416x416 1 morning_star, 1993.1ms
Speed: 1.4ms preprocess, 1993.1ms inference, 1.0ms postprocess per image at shape (1, 3, 416, 416)

image 1/1 /content/content/dataset/images/valid/HCL_0_idx3_xs130_xe250_res416.png: 416x416 1 morning_star, 3030.0ms
Speed: 1.3ms preprocess, 3030.0ms inference, 0.9ms postprocess per image at shape (1, 3, 416, 416)


IndexError Traceback (most recent call last)
in <cell line: 17>()
15 explainer = AnchorImage(predict_fn, image_shape, segmentation_fn=superpixel)
16
---> 17 explanation = explainer.explain(image, threshold=.95, p_sample=.8, seed=0)
18 plt.imshow(explanation.anchor[:,:,0]);

4 frames
/usr/local/lib/python3.10/dist-packages/alibi/explainers/anchors/anchor_image.py in call(self, anchor, num_samples, compute_labels)
137 raw_data, data = self.perturbation(anchor[1], num_samples)
138 labels = self.compare_labels(raw_data)
--> 139 print(raw_data,labels,self.n_covered_ex)
140 covered_true = raw_data[labels][: self.n_covered_ex]
141 covered_true = [scale_image(img) for img in covered_true]

IndexError: boolean index did not match indexed array along dimension 0; dimension is 100 but corresponding boolean dimension is 1

Code:

import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

model = YOLO("/content/final_model.pt")

image_path = "/content/content/dataset/images/valid/HCL_0_idx3_xs130_xe250_res416.png"
image = img = cv2.imread(image_path)
plt.imshow(image[:,:,0])
test_image = image.reshape(1, 416, 416, 3)
results = model([image_path])
print("Prediction ", results[0].boxes)

def superpixel(image, size=(16, 26)):
    segments = np.zeros([image.shape[0], image.shape[1]])
    row_idx, col_idx = np.where(segments == 0)
    for i, j in zip(row_idx, col_idx):
        segments[i, j] = int((image.shape[1]/size[1]) * (i//size[0]) + j//size[1])
    return segments

# trying with static probabilities
predict_fn = lambda x: np.array([0.01]*9+[0.82]+[0.01]*10).reshape(1,20)
image_shape = image.shape

explainer = AnchorImage(predict_fn, image_shape, segmentation_fn=superpixel)

explanation = explainer.explain(image, threshold=.95, p_sample=.8, seed=0)
plt.imshow(explanation.anchor[:,:,0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant