Dice and CE not working together

Would really appreciate help, have been literally struggling on this for 8+ hrs

I am training a segmentation network on the Kaggle Salt challenge. My dice and ce decrease, but then suddenly dice increases and CE jumps up abit, this keeps happening to dice. I have been trying all day to fix this but can’t get my code to run. I am running on only 10 datapoint to overfit my data but it just is not happening. Any help would be greatly appreciated.

Plots of dice(top) and CE:

Here my dice:

The targets are [batch_size,100,100] so I make them [batch_size, 2,100,100] each channel being one class:

def dice(input, target,weights=torch.tensor([1,1]).float().cuda()):
    smooth=.001
    
    dummy=np.zeros([batch_size,2,100,100]) # create dummy to one hot encode target for weighted dice
    dummy[:,0,:,:][target==0]=1 # background class is 0
    dummy[:,1,:,:][target==1]=1 # salt class is 1 
    
    
    target=torch.tensor(dummy).float().cuda()
    
#     print(input.size(),input[:,0,:,:].size())
    input1=input[:,0,:,:].contiguous().view(-1) #flatten both classes seperately
    target1=target[:,0,:,:].contiguous().view(-1)
    
    input2=input[:,1,:,:].contiguous().view(-1)
    target2=target[:,1,:,:].contiguous().view(-1)
    
    score1=2*(input1*target1).sum()/(input1.sum()+target1.sum()+smooth) #back
    score2=2*(input2*target2).sum()/(input2.sum()+target2.sum()+smooth) #salt

    
    score=1-(weights[0]*score1+weights[1]*score2)/2
    if score<0:
        score=score-score
    
    return(score)

Heres the train:


def train(epoch):
    for idx, batch_data in enumerate(dataloader) : 
        x, target=batch_data['image'].float().cuda(),batch_data['label'].float().cuda()


        optimizer.zero_grad()
        output = net(x)
#         print(output.size())
        output.squeeze_(1)

#         print('out',output.size(),target.size())
        bce_loss = criterion(output, target.long())
        lc.append(bce_loss.item())

        dice_loss = dice((output), target)
        ld.append(dice_loss.item())
        loss =  dice_loss + bce_loss
        l.append(loss.item())

        loss.backward()
        optimizer.step()

        print('Epoch {}, loss {}, bce {}, dice {}'.format(
            epoch, sum(l)/len(l), sum(lc)/len(lc) , sum(ld)/len(ld) ))

Heres the rest of the code( I downed from gaggle kernel): https://github.com/bluesky314/Salt-Segmentation/blob/master/kernel-2.ipynb (the training showed here is when I ran that cell(14) a second time so the ups and downs don’t appear but can be seen in the plot)

dataset=DatasetSalt(limit_paths=10) just limits the dataset to any number by only taking the top paths to get the images from

Would really appreciate help, have been literally struggling on this for 8+ hrs