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!