Hi
I am trying to build a (people and background) segmentation network.
I have a working network but for distance estimation. I plan to change this a little bit for the segmetation woor.
Here is my distance model:
class network(nn.Module):
distance estimation layers
...
distance estimation layers
self.scratch.output_conv = nn.Sequential(
nn.Conv2d(features, features//2, kernel_size=3, stride=1, padding=1, groups=self.groups),
Interpolate(scale_factor=2, mode="bilinear"),
nn.Conv2d(features//2, 32, kernel_size=3, stride=1, padding=1),
self.scratch.activation,
nn.Conv2d(32, 1, kernel_size=1, stride=1, padding=0),
nn.ReLU(True) if non_negative else nn.Identity(),
nn.Identity(),
)
def forward(self, x):
distance estimation layers
.....
output = self.scratch.output_conv(...)
return output
since the output is a dense map.
So to output either 0 or 1 for each pixel, I made the following change (Add Sigmoid and binary the output):
class network(nn.Module):
distance estimation layers
...
distance estimation layers
self.scratch.output_conv = nn.Sequential(
nn.Conv2d(features, features//2, kernel_size=3, stride=1, padding=1, groups=self.groups),
Interpolate(scale_factor=2, mode="bilinear"),
nn.Conv2d(features//2, 32, kernel_size=3, stride=1, padding=1),
self.scratch.activation,
nn.Conv2d(32, 1, kernel_size=1, stride=1, padding=0),
nn.ReLU(True) if non_negative else nn.Identity(),
nn.Identity(),
nn.Sigmoid()
)
def forward(self, x):
distance estimation layers
.....
output = self.scratch.output_conv(...)
pred = out > 0.75
pred = pred * 1.0
return pred
During training, I use the loss function:
loss_f= nn.BCELoss()
est= model(image)
loss = loss_f(est, target)
loss.backward()
When run I got
loss.backward()
File "/home/bigtree/miniconda3/envs/bigtree/lib/python3.7/site-packages/torch/tensor.py", line 245, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
File "/home/bigtree/miniconda3/envs/bigtree/lib/python3.7/site-packages/torch/autograd/__init__.py", line 147, in backward
allow_unreachable=True, accumulate_grad=True) # allow_unreachable flag
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
Please let me know if I did something wrong.
Thanks