my train and val scores are
training loss: 0.11447336673736572, iou score: 0.7451344195415179, dice score: 0.8161399212012007
validation loss: 3.3190221786499023, iou score: 0.09056307559778348, dice score: 0.11742234666033746
please help me why i get very less validation score. i am using same function to evaluate my train and test dataset. But validation scores is very less. Is this training problem?
my testing code is as follows:
model.eval()
with torch.no_grad():
for batch_idx, (data, labels) in enumerate(train_loader):
labels = torch.argmax(labels, dim=1) # ccel torch.Size([16, 256, 256]) torch.int64.... 4,4,4......21,21,21...
inputs1 = data.to(device=device)
labels1 = labels.to(device=device)
# Predicting segmentation for val inputs
outputs1 = model(inputs1)#32, 32, 256, 256
#print(outputs1.shape, labels1.shape)
# Compute CE loss and aggregate it
loss1 = loss_fn(outputs1, labels1)
train_running_loss += loss1
# Reshaping prediction segmentations and actual segmentations for iou and dice score
preds = torch.argmax(outputs1, dim=1).detach().cpu().numpy()
gt = labels1.detach().cpu().numpy()
print(preds.shape, gt.shape)#(32, 256, 256) b c h
# Compute confusion matrix
conf_mat = confusion_matrix(y_pred=preds.flatten(), y_true=gt.flatten(), labels=list(range(21)))
#conf_mat = confusion_matrix(y_pred=gt.flatten(), y_true=gt.flatten(), labels=list(range(21)))
# Computing iou and dice scores and aggregating them
iou_score = get_mean_iou(conf_mat=conf_mat)
iou_running_score += iou_score
dice_score = get_mean_iou(conf_mat=conf_mat, multiplier=2.0)
dice_running_score += dice_score
# Averaging loss and scores
avg_train_loss = float(train_running_loss)/(batch_idx+1)
avg_iou_score = float(iou_running_score)/(batch_idx+1)
avg_dice_score = float(dice_running_score)/(batch_idx+1)
# Visualizations for batch wise metrics
print('training loss: {}, iou score: {}, dice score: {}'.format(avg_train_loss, avg_iou_score, avg_dice_score))
best_loss = 1000000000
val_running_loss = 0
iou_running_score = 0
dice_running_score = 0
model.eval()
with torch.no_grad():
for batch_idx, (data, labels) in enumerate(test_loader):
labels = torch.argmax(labels, dim=1) # ccel torch.Size([16, 256, 256]) torch.int64.... 4,4,4......21,21,21...
inputs1 = data.to(device=device)
labels1 = labels.to(device=device)
# Predicting segmentation for val inputs
outputs1 = model(inputs1)
#print(outputs1.shape, labels1.shape)
# Compute CE loss and aggregate it
loss1 = loss_fn(outputs1, labels1)
val_running_loss += loss1
# Reshaping prediction segmentations and actual segmentations for iou and dice score
preds = torch.argmax(outputs1, dim=1).detach().cpu().numpy()
gt = labels1.detach().cpu().numpy()
#print(preds.shape, gt.shape)
# Compute confusion matrix
conf_mat = confusion_matrix(y_pred=preds.flatten(), y_true=gt.flatten(), labels=list(range(21)))
#conf_mat = confusion_matrix(y_pred=gt.flatten(), y_true=gt.flatten(), labels=list(range(21)))
# Computing iou and dice scores and aggregating them
iou_score = get_mean_iou(conf_mat=conf_mat)
iou_running_score += iou_score
dice_score = get_mean_iou(conf_mat=conf_mat, multiplier=2.0)
dice_running_score += dice_score
# Averaging loss and scores
avg_val_loss = float(val_running_loss)/(batch_idx+1)
avg_iou_score = float(iou_running_score)/(batch_idx+1)
avg_dice_score = float(dice_running_score)/(batch_idx+1)
# Visualizations for batch wise metrics
print('validation loss: {}, iou score: {}, dice score: {}'.format(avg_val_loss, avg_iou_score, avg_dice_score))