rigvedrs / YOLO-V8-CAM

Wanna know what your model sees? Here's a package for applying EigenCAM on the new YOLO V8 model
MIT License
100 stars 17 forks source link

AssertionError in the results.py (ultralytics) #3

Closed gycka closed 11 months ago

gycka commented 1 year ago

I am trying to work with your YOLOv8 CAM; however, I am getting an AssertionError in BaseTensor class in the results.py line 32.

Initially, the problem originates from this line: grayscale_cam = cam(rgb_img)[0, :, :]

It does predict the class correctly: 224x224 golden_retriever 0.41, Labrador_retriever 0.30, Great_Pyrenees 0.09, kuvasz 0.04, cocker_spaniel 0.03, 2.4ms Speed: 1.0ms preprocess, 2.4ms inference, 0.0ms postprocess per image at shape (1, 3, 224, 224)

that comes from here: ultralytics\yolo\engine\results.py", line 32, in __init__ assert isinstance(data, (torch.Tensor, np.ndarray))

info1

It seems that the model does predict the classes correctly and the data in this class is a tensor/numpy array the first 3 times, but then it becomes a single digit, so it no longer is a tensor or a numpy array. This is outside of your code, but I was wondering if you have encountered anything like this before.

I also tried to convert the image to tensor as it was introduced here:

transform = transforms.ToTensor()
tensor = transform(img).unsqueeze(0)
grayscale_cam = cam(tensor)[0, :, :]

but no success.

gycka commented 1 year ago

I have been playing around and I found a hacky solution that works (for the puppies image (target_layers =[model.model.model[-2]]))). I replaced one line in the base_cam.py:

# target_categories = [np.argmax(outputs[0].probs.cpu().numpy())] # original
target_categories = [0] # changed

I checked with the resnet just to see what are the target categories and I always got it to be a 0, so I tried with a 0. Obviously, a better solution is required. I was getting this error because my outputs was empty for whatever it was looking for (it was not empty). puppies_work

rigvedrs commented 1 year ago

Hey! Thanks for your inputs in this. It is a hacky solution indeed. I will see if there's a better way for this. Till then you can use the method you've suggested.

rigvedrs commented 1 year ago

This seems like an issue for the new version of ultralytics. I will try to fix it, but till then I recommend you to use ultralytics-8.0.107 or below if you want to use the package.