Image Regression using fastai

Think I finally got it. The approach I took was to make a custom pytorch module as follows:

ymin = 0
ymax = 100

class scaledSigmoid(nn.Module):
    def forward(self, input):
        return torch.sigmoid(input) * (ymax - ymin) + ymin

Putting it together with my last post:

learn = cnn_learner(data,
                    models.densenet121, 
                    metrics=explained_variance)

learn.model[1].add_module("sSig", module= scaledSigmoid())

After talking to a colleague, however, he suggested a modified ReLU, since a sigmoid isn’t ideal for predicting at the the extrema. So this is the module that works best for me with my response data that are scaled 0 to 100:

ymin = 0
ymax = 100

class clampedReLU(nn.Module):
    def forward(self, input):
        bottomClamp = input < ymin
        topClamp = input > ymax
        input[bottomClamp,] = ymin
        input[topClamp,] = ymax
        return input

learn = cnn_learner(data,
                    models.densenet121, 
                    metrics=explained_variance)

learn.model[1].add_module("cReLU", module= clampedReLU())

Appears to behave as expected when added as a final layer. I’m no longer predicting above 100 or below 0.

3 Likes