Manza12/nnMorpho

In opening and closing only the latter operation gets gradients

Closed this issue · 6 comments

Also noticing that when doing opening and closing, only the latter operation/strel gets gradients

Originally posted by @s-rog in #3 (comment)

I have noticed that I only return gradients for the structural element when eroding and dilating.

@s-rog, what are your tensors that requiere grad? if the images requiere grad, it should refactor the backward functions to adapt.

In all the cases, I probably need to return both gradients (image and strel)

s-rog commented

I meant like when your doing an erosion then a dilation, the erosion op doesn't produce any gradients.

In my testing if you have two morph ops in series, each using their own strel parameter, the first morph strel does not move.

Can you post a Python code doing that such that I can reproduce the problem? It will be easier for me to debug :)

s-rog commented

A minimal snippet looks something like this:

import nnMorpho.functions as MF
import torch

class morph_module(torch.nn.Module):
    def __init__(self, sz=11):
        super(morph_module, self).__init__()
        self.strels = torch.nn.ParameterList([torch.rand(sz, sz), torch.rand(sz, sz)])

def forward(self, x):
    for strel in self.strels:
        origin = (strel.shape[0] // 2, strel.shape[1] // 2)
        x = MF.DilationFunction.apply(x, strel, origin, -1e4)
    return x

Thank you!
I am almost sure that the problem comes from the fact that the backward function of DilationFunction does not return gradients with respect to the input image.
I am fixing that.
I'll prevent here when a new release is ready

The new release has corrected this bug: the problem was that the functions did not return gradients with respect to the input image; now they do so then iterative applies of the functions work as expected :)