0

I have the following Dataloader that I am using for an autoencoder in Pytorch


class DataLoader(data.DataLoader):
    def __init__(self,  *args, **kwargs):
        super(DataLoader, self).__init__(*args, **kwargs)
        self.iterator = self.__iter__()

    def __next__(self):
        try:
            return next(self.iterator)
        except:
            self.iterator = self.__iter__()
            return next(self.iterator)

When running the loss function computation I want to display predictions as images for the first file in my dataset, but even if I only have one batch of 2 images for training, these images are being exchanged so my code:

class CrossEntropyLoss2d(nn.Module):
    def __init__(self, weight=None):
        super(CrossEntropyLoss2d, self).__init__()

    def forward(self, pred, target, weight=None):
        pred = pred.clamp(min=1e-16)

        plt.clf()
        predpic = (np.uint8(np.argmax(pred.data.cpu().numpy(), 1)))
        plt.imshow(predpic[0]) ### here I want only the first picture! 
        plt.title('Example prediction')
        plt.savefig(os.path.join(results_path, "exampleprediction.png"))
        plt.show()

        ##### COMPUTE LOSS
        loss = -(pred.log() * target)
        loss = loss.sum(1)
        if weight is not None:
            loss = loss * weight
        loss = loss.mean()
        return loss 

does not work.

How can I fix this? Thanks!

Maja
  • 151
  • 7
  • 1
    Need more information. The arguments to dataloader (specifically sampler or shuffle) and the dataset are probably the most likely culprits. Also as far as I can see, your dataloader class doesn't do anything more than the default so why are you defining a derived dataloader? – jodag Aug 11 '19 at 19:15
  • Thanks for your answer. The code is not mine and I am new to PyTorch so I just assumed this should be like this. Do you mean I can get rid of the data.loader code? Thanks a lot a I changed the shuffle argument! – Maja Aug 12 '19 at 07:46
  • Yes you should be able to just use data.DataLoader directly. – jodag Aug 12 '19 at 15:52

0 Answers0