{
  "id": 333432,
  "title": "Help! Confusion about dice coefficient",
  "url": "/competitions/hubmap-organ-segmentation/discussion/333432",
  "author_name": "Nishant Bhansali",
  "post_date": "2022-06-26T13:32:50.003000",
  "votes": 2,
  "comment_count": 0,
  "views": 0,
  "content": "<p>I am confused about the implementation of the dice coefficient as a loss function. Is the implementation if segmentation models pytorch library correct or am i missing something?</p>\n<pre><code>tl,vl = prepare_loaders(fold = 0,df = create_folds(cfg),cfg = cfg,debug = False)\nimgs,masks = next(iter(tl))\n\nimgs = imgs.to(cfg.device, dtype=torch.float)/255\nmasks  = masks.to(cfg.device, dtype=torch.float)\nimgs.shape,masks.shape\n ###  (torch.Size([32, 3, 256, 256]), torch.Size([32, 256, 256]))\n\nmodel = build_model(cfg)\nmodel.train()\ntype(model)\n### segmentation_models_pytorch.unet.model.Unet\n\ny_pred = model(imgs)\ny_pred = nn.Sigmoid()(y_pred)\ny_pred.shape\n### torch.Size([32, 1, 256, 256])\n</code></pre>\n<p><strong>segmentation-models-pytorch library</strong></p>\n<pre><code>loss_function = smp.losses.DiceLoss(mode='binary',from_logits =True,log_loss = False)\nt1 = torch.tensor([[0,1,1,0],\n                  [0,1,1,0],\n                  [0,1,1,0],\n                  [0,1,1,0]],dtype = torch.float32)\nloss_function(t1,t1)\n</code></pre>\n<p>why does the above give output as <code>tensor(0.3447)</code> whereas it should be 0?</p>",
  "messages": [
    {
      "id": 1833945,
      "postDate": "2022-06-26T13:32:50.003Z",
      "content": "<p>I am confused about the implementation of the dice coefficient as a loss function. Is the implementation if segmentation models pytorch library correct or am i missing something?</p>\n<pre><code>tl,vl = prepare_loaders(fold = 0,df = create_folds(cfg),cfg = cfg,debug = False)\nimgs,masks = next(iter(tl))\n\nimgs = imgs.to(cfg.device, dtype=torch.float)/255\nmasks  = masks.to(cfg.device, dtype=torch.float)\nimgs.shape,masks.shape\n ###  (torch.Size([32, 3, 256, 256]), torch.Size([32, 256, 256]))\n\nmodel = build_model(cfg)\nmodel.train()\ntype(model)\n### segmentation_models_pytorch.unet.model.Unet\n\ny_pred = model(imgs)\ny_pred = nn.Sigmoid()(y_pred)\ny_pred.shape\n### torch.Size([32, 1, 256, 256])\n</code></pre>\n<p><strong>segmentation-models-pytorch library</strong></p>\n<pre><code>loss_function = smp.losses.DiceLoss(mode='binary',from_logits =True,log_loss = False)\nt1 = torch.tensor([[0,1,1,0],\n                  [0,1,1,0],\n                  [0,1,1,0],\n                  [0,1,1,0]],dtype = torch.float32)\nloss_function(t1,t1)\n</code></pre>\n<p>why does the above give output as <code>tensor(0.3447)</code> whereas it should be 0?</p>",
      "rawMarkdown": "I am confused about the implementation of the dice coefficient as a loss function. Is the implementation if segmentation models pytorch library correct or am i missing something?\n\n```\ntl,vl = prepare_loaders(fold = 0,df = create_folds(cfg),cfg = cfg,debug = False)\nimgs,masks = next(iter(tl))\n\nimgs = imgs.to(cfg.device, dtype=torch.float)/255\nmasks  = masks.to(cfg.device, dtype=torch.float)\nimgs.shape,masks.shape\n ###  (torch.Size([32, 3, 256, 256]), torch.Size([32, 256, 256]))\n\nmodel = build_model(cfg)\nmodel.train()\ntype(model)\n### segmentation_models_pytorch.unet.model.Unet\n\ny_pred = model(imgs)\ny_pred = nn.Sigmoid()(y_pred)\ny_pred.shape\n### torch.Size([32, 1, 256, 256])\n\n```\n\n**segmentation-models-pytorch library**\n```\nloss_function = smp.losses.DiceLoss(mode='binary',from_logits =True,log_loss = False)\nt1 = torch.tensor([[0,1,1,0],\n                  [0,1,1,0],\n                  [0,1,1,0],\n                  [0,1,1,0]],dtype = torch.float32)\nloss_function(t1,t1)\n```\nwhy does the above give output as `tensor(0.3447)` whereas it should be 0?\n",
      "votes": 2
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "1833945": "I am confused about the implementation of the dice coefficient as a loss function. Is the implementation if segmentation models pytorch library correct or am i missing something?\n\n```\ntl,vl = prepare_loaders(fold = 0,df = create_folds(cfg),cfg = cfg,debug = False)\nimgs,masks = next(iter(tl))\n\nimgs = imgs.to(cfg.device, dtype=torch.float)/255\nmasks  = masks.to(cfg.device, dtype=torch.float)\nimgs.shape,masks.shape\n ###  (torch.Size([32, 3, 256, 256]), torch.Size([32, 256, 256]))\n\nmodel = build_model(cfg)\nmodel.train()\ntype(model)\n### segmentation_models_pytorch.unet.model.Unet\n\ny_pred = model(imgs)\ny_pred = nn.Sigmoid()(y_pred)\ny_pred.shape\n### torch.Size([32, 1, 256, 256])\n\n```\n\n**segmentation-models-pytorch library**\n```\nloss_function = smp.losses.DiceLoss(mode='binary',from_logits =True,log_loss = False)\nt1 = torch.tensor([[0,1,1,0],\n                  [0,1,1,0],\n                  [0,1,1,0],\n                  [0,1,1,0]],dtype = torch.float32)\nloss_function(t1,t1)\n```\nwhy does the above give output as `tensor(0.3447)` whereas it should be 0?\n"
  }
}