|
| 1 | +# Copyright 2020 MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import torch.nn.functional as F |
| 13 | +import torch.nn as nn |
| 14 | +import torch.optim as optim |
| 15 | +import torch |
| 16 | +import unittest |
| 17 | +from monai.losses import FocalLoss |
| 18 | + |
| 19 | + |
| 20 | +class TestFocalLoss(unittest.TestCase): |
| 21 | + def test_consistency_with_cross_entropy_2d(self): |
| 22 | + # For gamma=0 the focal loss reduces to the cross entropy loss |
| 23 | + focal_loss = FocalLoss(gamma=0.0, reduction="mean") |
| 24 | + ce = nn.CrossEntropyLoss(reduction="mean") |
| 25 | + max_error = 0 |
| 26 | + class_num = 10 |
| 27 | + batch_size = 128 |
| 28 | + for _ in range(100): |
| 29 | + # Create a random tensor of shape (batch_size, class_num, 8, 4) |
| 30 | + x = torch.rand(batch_size, class_num, 8, 4, requires_grad=True) |
| 31 | + # Create a random batch of classes |
| 32 | + l = torch.randint(low=0, high=class_num, size=(batch_size, 8, 4)) |
| 33 | + l = l.long() |
| 34 | + if torch.cuda.is_available(): |
| 35 | + x = x.cuda() |
| 36 | + l = l.cuda() |
| 37 | + output0 = focal_loss.forward(x, l) |
| 38 | + output1 = ce.forward(x, l) |
| 39 | + a = float(output0.cpu().detach()) |
| 40 | + b = float(output1.cpu().detach()) |
| 41 | + if abs(a - b) > max_error: |
| 42 | + max_error = abs(a - b) |
| 43 | + self.assertAlmostEqual(max_error, 0.0, places=3) |
| 44 | + |
| 45 | + def test_consistency_with_cross_entropy_classification(self): |
| 46 | + # for gamma=0 the focal loss reduces to the cross entropy loss |
| 47 | + focal_loss = FocalLoss(gamma=0.0, reduction="mean") |
| 48 | + ce = nn.CrossEntropyLoss(reduction="mean") |
| 49 | + max_error = 0 |
| 50 | + class_num = 10 |
| 51 | + batch_size = 128 |
| 52 | + for _ in range(100): |
| 53 | + # Create a random scores tensor of shape (batch_size, class_num) |
| 54 | + x = torch.rand(batch_size, class_num, requires_grad=True) |
| 55 | + # Create a random batch of classes |
| 56 | + l = torch.randint(low=0, high=class_num, size=(batch_size,)) |
| 57 | + l = l.long() |
| 58 | + if torch.cuda.is_available(): |
| 59 | + x = x.cuda() |
| 60 | + l = l.cuda() |
| 61 | + output0 = focal_loss.forward(x, l) |
| 62 | + output1 = ce.forward(x, l) |
| 63 | + a = float(output0.cpu().detach()) |
| 64 | + b = float(output1.cpu().detach()) |
| 65 | + if abs(a - b) > max_error: |
| 66 | + max_error = abs(a - b) |
| 67 | + self.assertAlmostEqual(max_error, 0.0, places=3) |
| 68 | + |
| 69 | + def test_bin_seg_2d(self): |
| 70 | + # define 2d examples |
| 71 | + target = torch.tensor([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]) |
| 72 | + # add another dimension corresponding to the batch (batch size = 1 here) |
| 73 | + target = target.unsqueeze(0) # shape (1, H, W) |
| 74 | + pred_very_good = 1000 * F.one_hot(target, num_classes=2).permute(0, 3, 1, 2).float() |
| 75 | + |
| 76 | + # initialize the mean dice loss |
| 77 | + loss = FocalLoss() |
| 78 | + |
| 79 | + # focal loss for pred_very_good should be close to 0 |
| 80 | + focal_loss_good = float(loss.forward(pred_very_good, target).cpu()) |
| 81 | + self.assertAlmostEqual(focal_loss_good, 0.0, places=3) |
| 82 | + |
| 83 | + # Same test, but for target with a class dimension |
| 84 | + target = target.unsqueeze(1) # shape (1, 1, H, W) |
| 85 | + focal_loss_good = float(loss.forward(pred_very_good, target).cpu()) |
| 86 | + self.assertAlmostEqual(focal_loss_good, 0.0, places=3) |
| 87 | + |
| 88 | + def test_empty_class_2d(self): |
| 89 | + num_classes = 2 |
| 90 | + # define 2d examples |
| 91 | + target = torch.tensor([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) |
| 92 | + # add another dimension corresponding to the batch (batch size = 1 here) |
| 93 | + target = target.unsqueeze(0) # shape (1, H, W) |
| 94 | + pred_very_good = 1000 * F.one_hot(target, num_classes=num_classes).permute(0, 3, 1, 2).float() |
| 95 | + |
| 96 | + # initialize the mean dice loss |
| 97 | + loss = FocalLoss() |
| 98 | + |
| 99 | + # focal loss for pred_very_good should be close to 0 |
| 100 | + focal_loss_good = float(loss.forward(pred_very_good, target).cpu()) |
| 101 | + self.assertAlmostEqual(focal_loss_good, 0.0, places=3) |
| 102 | + |
| 103 | + def test_multi_class_seg_2d(self): |
| 104 | + num_classes = 6 # labels 0 to 5 |
| 105 | + # define 2d examples |
| 106 | + target = torch.tensor([[0, 0, 0, 0], [0, 1, 2, 0], [0, 3, 4, 0], [0, 0, 0, 0]]) |
| 107 | + # add another dimension corresponding to the batch (batch size = 1 here) |
| 108 | + target = target.unsqueeze(0) # shape (1, H, W) |
| 109 | + pred_very_good = 1000 * F.one_hot(target, num_classes=num_classes).permute(0, 3, 1, 2).float() |
| 110 | + |
| 111 | + # initialize the mean dice loss |
| 112 | + loss = FocalLoss() |
| 113 | + |
| 114 | + # focal loss for pred_very_good should be close to 0 |
| 115 | + focal_loss_good = float(loss.forward(pred_very_good, target).cpu()) |
| 116 | + self.assertAlmostEqual(focal_loss_good, 0.0, places=3) |
| 117 | + |
| 118 | + def test_bin_seg_3d(self): |
| 119 | + # define 2d examples |
| 120 | + target = torch.tensor( |
| 121 | + [ |
| 122 | + # raw 0 |
| 123 | + [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]], |
| 124 | + # raw 1 |
| 125 | + [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]], |
| 126 | + # raw 2 |
| 127 | + [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]], |
| 128 | + ] |
| 129 | + ) |
| 130 | + # add another dimension corresponding to the batch (batch size = 1 here) |
| 131 | + target = target.unsqueeze(0) # shape (1, H, W, D) |
| 132 | + pred_very_good = 1000 * F.one_hot(target, num_classes=2).permute(0, 4, 1, 2, 3).float() |
| 133 | + |
| 134 | + # initialize the mean dice loss |
| 135 | + loss = FocalLoss() |
| 136 | + |
| 137 | + # focal loss for pred_very_good should be close to 0 |
| 138 | + focal_loss_good = float(loss.forward(pred_very_good, target).cpu()) |
| 139 | + self.assertAlmostEqual(focal_loss_good, 0.0, places=3) |
| 140 | + |
| 141 | + def test_convergence(self): |
| 142 | + """ |
| 143 | + The goal of this test is to assess if the gradient of the loss function |
| 144 | + is correct by testing if we can train a one layer neural network |
| 145 | + to segment one image. |
| 146 | + We verify that the loss is decreasing in almost all SGD steps. |
| 147 | + """ |
| 148 | + learning_rate = 0.001 |
| 149 | + max_iter = 20 |
| 150 | + |
| 151 | + # define a simple 3d example |
| 152 | + target_seg = torch.tensor( |
| 153 | + [ |
| 154 | + # raw 0 |
| 155 | + [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]], |
| 156 | + # raw 1 |
| 157 | + [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]], |
| 158 | + # raw 2 |
| 159 | + [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]], |
| 160 | + ] |
| 161 | + ) |
| 162 | + target_seg = torch.unsqueeze(target_seg, dim=0) |
| 163 | + image = 12 * target_seg + 27 |
| 164 | + image = image.float() |
| 165 | + num_classes = 2 |
| 166 | + num_voxels = 3 * 4 * 4 |
| 167 | + |
| 168 | + # define a one layer model |
| 169 | + class OnelayerNet(nn.Module): |
| 170 | + def __init__(self): |
| 171 | + super(OnelayerNet, self).__init__() |
| 172 | + self.layer = nn.Linear(num_voxels, num_voxels * num_classes) |
| 173 | + |
| 174 | + def forward(self, x): |
| 175 | + x = x.view(-1, num_voxels) |
| 176 | + x = self.layer(x) |
| 177 | + x = x.view(-1, num_classes, 3, 4, 4) |
| 178 | + return x |
| 179 | + |
| 180 | + # initialise the network |
| 181 | + net = OnelayerNet() |
| 182 | + |
| 183 | + # initialize the loss |
| 184 | + loss = FocalLoss() |
| 185 | + |
| 186 | + # initialize an SGD |
| 187 | + optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9) |
| 188 | + |
| 189 | + loss_history = [] |
| 190 | + # train the network |
| 191 | + for _ in range(max_iter): |
| 192 | + # set the gradient to zero |
| 193 | + optimizer.zero_grad() |
| 194 | + |
| 195 | + # forward pass |
| 196 | + output = net(image) |
| 197 | + loss_val = loss(output, target_seg) |
| 198 | + |
| 199 | + # backward pass |
| 200 | + loss_val.backward() |
| 201 | + optimizer.step() |
| 202 | + |
| 203 | + # stats |
| 204 | + loss_history.append(loss_val.item()) |
| 205 | + |
| 206 | + # count the number of SGD steps in which the loss decreases |
| 207 | + num_decreasing_steps = 0 |
| 208 | + for i in range(len(loss_history) - 1): |
| 209 | + if loss_history[i] > loss_history[i + 1]: |
| 210 | + num_decreasing_steps += 1 |
| 211 | + decreasing_steps_ratio = float(num_decreasing_steps) / (len(loss_history) - 1) |
| 212 | + |
| 213 | + # verify that the loss is decreasing for sufficiently many SGD steps |
| 214 | + self.assertTrue(decreasing_steps_ratio > 0.9) |
| 215 | + |
| 216 | + |
| 217 | +if __name__ == "__main__": |
| 218 | + unittest.main() |
0 commit comments