I have a network that returns two different outputs.
class Deeplab_v3plus(nn.Module): def __init__(self, cfg, *args, **kwargs): super(Deeplab_v3plus, self).__init__() self.backbone = Resnet101(stride=16) self.aspp = ASPP(in_chan=2048, out_chan=256, with_gp=cfg.aspp_global_feature) self.decoder = Decoder(cfg.n_classes, low_chan=256) # self.backbone = Darknet53(stride=16) # self.aspp = ASPP(in_chan=1024, out_chan=256, with_gp=False) # self.decoder = Decoder(cfg.n_classes, low_chan=128) self.init_weight() def forward(self, x): H, W = x.size()[2:] feat4, _, _, feat32 = self.backbone(x) feat_aspp,features = self.aspp(feat32) logits = self.decoder(feat4, feat_aspp) logits = F.interpolate(logits, (H, W), mode='bilinear', align_corners=True) for f in features: f.detach_() return logits, features
Now I only need the first one to participate in calculating our loss.
optim.zero_grad() lodits,_= net(im) loss = criteria(logits, lb) loss.backward() optim.step()
the second output(
features) will be used in another network that running simultaneously, so at this moment, it’s unnecessary to join in calculating my loss.
when launching our program, I always get such an error as follows:
Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that were not used in producing loss. You can enable unused parameter detection by (1) passing the keyword argument
torch.nn.parallel.DistributedDataParallel; (2) making sure all
forwardfunction outputs participate in calculating loss. If you already have done the above two steps, then the distributed data parallel module wasn’t able to locate the output tensors in the return value of your module’s
Then I tried to detach “features” from my computation graph, however, it’s failed. Any cues would be highly appreciated!!! thank you in advance.