pytorch-grad-cam
pytorch-grad-cam copied to clipboard
YOLOv8n auto train with 100 epochs starts when in try to use the EigenCAM method
Hi, I want to use the EigenCAM technique on my YOLOv8n model, preaddestred for a detection task.
Once i declare my YOLO .pt weights and I create my tensor image, I try to call the EigenCAM method to generate activation maps with my chosen target layers. Now here's the problem: a 100 epochs automatic training starts.
Even when i put my model in .eval() mode this train starts... can someone please help me?
Here it is my image in tensors
Here it's when my train starts
These are my chosen target layers
I followed step by step the tutorial with the pretrained YOLOv5 model (EigenCAM puppies) and all things works perfectly in there... @jacobgil
same problem
I am also facing the same problem.
same here
upon inspection it looks like this (no guarantees): YOLO is its own model, and not directly a pytorch model. thus, its training method does not follow pytorch train methods. eval() calls self.training(false), but in YOLO this will start training, since the 'false' is taken as an argument possibly for config.
thus, to remedy the problem one can either extract the underlying pytorch model like this:
model = YOLO("yolo11s-cls.yaml").load(f"runs/classify/train/weights/best.pt")
model = model.model.model
model.to(torch.device(args.device)).eval()
and then specify the layer one wants to apply cam to it:
target_layers = [model[8]]
or alternatively, one could try to provide a custom wrapper, something like
class CustomYOLO(YOLO):
def eval(self):
"""Set the module in evaluation mode and adjust to return self.model.model.eval()."""
#super().eval() # Call the original eval method to ensure any necessary setup
return self.model.model.train(False) # Change the return value
however, this is not complete, since the output is again not what a pytorch model would provide, but is a class, so there would be more code necessary to get this working.
disclaimers:
- while this now does not start training and yields some heatmap, i have no idea right now if there are other problems, i.e., if the heatmap is 'correct'.
- this is for yolo11. i did not check for other versions.
nonetheless, the problem is not with pytorch-grad-cam, the issue can be closed.