imgs, labels = imgs.to(device), labels.to(device)

            out = net(imgs)
            loss = loss_fn(out, labels)
            ctr += 1
            pred = out.data.max(1)[1]
            running_metrics_val.update(pred.cpu().numpy(),
                                       labels.cpu().numpy())
            val_loss += loss.item()
    score, class_iou = running_metrics_val.get_scores()
    for k, v in score.items():
        print(k, v)

    # for k, v in class_iou.items():
    #     logger.info('{}: {}'.format(k, v))

    running_metrics_val.reset()

    print("EPOCH %d: VAL loss=%f" % (EPOCHS, val_loss / (ctr)))

    scheduler.step(val_loss)

    if score["Mean IoU : \t"] >= best_iou:
        best_iou = score["Mean IoU : \t"]
        torch.save(net.state_dict(), 'best_val.wts')

    # if val_loss < best_val_loss:
    #     torch.save(net.state_dict(), 'best_val.wts')

    torch.save(net.state_dict(), 'trained.wts')
    running_metrics_val.reset()
    val_loss_meter.reset()


    #print("EPOCH %d: VAL loss=%f"%(EPOCHS, val_loss/(ctr)))
    
    # scheduler.step(val_loss)

    # if score["Mean IoU : \t"] >= best_iou:
    #     best_iou = score["Mean IoU : \t"]
    #     torch.save(net.state_dict(), 'best_val.wts')

    if score["Mean IoU : \t"] >= best_iou:
        best_iou = score["Mean IoU : \t"]
        state = {
            "epoch": EPOCHS + 1,
            "model_state": net.state_dict(),
            "optimizer_state": optimizer.state_dict(),
            "scheduler_state": scheduler.state_dict(),
            "best_iou": best_iou,
        }
        save_path = os.path.join(writer.file_writer.get_logdir(),
                                    "best_val_model.pkl")
        torch.save(state, save_path)

    # if val_loss < best_val_loss:
    #     torch.save(net.state_dict(), 'best_val.wts')

    #torch.save(net.state_dict(), 'trained.wts')