Code Monkey home page Code Monkey logo

Comments (7)

JunMa11 avatar JunMa11 commented on July 18, 2024

Hi @yanghedada ,

Sorry for my late reply.

The direct answer is No.
We only use torch.no_grad() when something should not be used during BP, e.g., computing the distance transform maps.

from seglossodyssey.

EJShim avatar EJShim commented on July 18, 2024

I still don't get it.

Sorry I am not very familiar to pytorch gradients things,, but shouldn't every loss functions be differentiable? Is it really OK if there is "no_grad" thing included?

from seglossodyssey.

JunMa11 avatar JunMa11 commented on July 18, 2024

@EJShim Yes!

from seglossodyssey.

EJShim avatar EJShim commented on July 18, 2024

Thank you for your reply, then is it possible to use non-differentiable operations in Loss function when the function is wrapped in
torch.no_grad()?

from seglossodyssey.

EJShim avatar EJShim commented on July 18, 2024

I cannot understand how pred_dt can still have grad_fn after it passed through self.distance_field() function.

        pred_dt = torch.from_numpy(self.distance_field(pred.detach().cpu().numpy())).float()
        target_dt = torch.from_numpy(self.distance_field(target.cpu().numpy())).float()
        print(pred_dt) #grad_fn exists!

I made a simple test function that returns same np.ndarray() as input array:

    @torch.no_grad()
    def test(self, img : np.ndarray) -> np.ndarray:
        return img

and the results torch.tensor() have lost its grad_fn

        pred = torch.from_numpy(self.test( pred.detach().cpu().numpy() )).float()
        print(pred) # no grad

What is the difference between test() and distance_field() function? how distance_field doesn't lose its grad function?

from seglossodyssey.

JunMa11 avatar JunMa11 commented on July 18, 2024

Hi @EJShim,

I got your concerns. Actually, we (and also pytorch) do not need to compute the gradient of the distance transform. You can just regard them as a pre-computed constant.
Please check the equations in the original paper (HD loss, BD loss) or recent study to get more details of these loss functions.

Also, please feel free to raise questions and I'm happy to try my best to help you:)

Best,
Jun

from seglossodyssey.

EJShim avatar EJShim commented on July 18, 2024

@JunMa11 Thank you very much for your kind replay. At least now I can see that this works fine.

HDDT loss optimizes interestingly compared to the others : https://www.youtube.com/watch?v=oHLykKOqytI&ab_channel=EJShim

from seglossodyssey.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.