I've got a problem about training the Pytorch models. I'm trying to train my Pytorch model using dicom data and nifti GT However, the size of the weight file is ridiculously small because model training is not performed normally.
I used network model Unet++
I think there is a problem with the data loader. But I can't fixe it...
I'd appreciate it if you could help me.
Raw image file format is dicom and GT image format is nifti
in my dataloder
def __getitem__(self, index):
image_path = self.image_paths[index]
image_GT_path = image_path[:8]+'_'+image_path[8:12]+'.nii'
GT_path = self.GT_paths + image_GT_path
ds = dcmread(self.root+image_path)
image = ds.pixel_array.astype(np.float32)
image = torch.from_numpy(image.transpose(0,1)/255)
image = image.unsqueeze(0)
GT = nib.load(GT_path)
GT = GT.get_fdata(dtype=np.float32)
print(GT.shape)
GT = torch.from_numpy(GT.transpose(0,1))
GT = GT.unsqueeze(0)
return image, GT, image_path
and Train Code is
for epoch in range(self.num_epochs):
self.unet.train(True)
epoch_loss = 0
for i, (images, GT,empty) in enumerate(tqdm(self.train_loader)):
# GT : Ground Truth
images = images.to(self.device)
GT = GT.to(self.device)
# SR : Segmentation Result
SR = self.unet(images)
SR_probs = torch.sigmoid(SR)
SR_flat = SR_probs.view(SR_probs.size(0),-1)
GT_flat = GT.view(GT.size(0),-1)
loss =self.criterion(SR_flat,GT_flat)
# self.criterion=DiceLoss() #BCE not use
# loss = self.criterion(GT,SR_probs)
epoch_loss += loss.item()
train_losses.append(loss.item())
# Backprop + optimize
self.reset_grad()
loss.backward()
self.optimizer.step()