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

YOLOv8n auto train with 100 epochs starts when in try to use the EigenCAM method #507

Open
enricopierga opened this issue Jun 12, 2024 · 4 comments

Comments

@enricopierga
Copy link

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
image

Here it's when my train starts
image

These are my chosen target layers
image

I followed step by step the tutorial with the pretrained YOLOv5 model (EigenCAM puppies) and all things works perfectly in there...
@jacobgil

@Andreavisi1
Copy link

same problem

@atultiwari
Copy link

I am also facing the same problem.

@aydindemircioglu
Copy link

same here

@aydindemircioglu
Copy link

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.

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

4 participants