Building Custom CNN For Regression

Hi All,

After the great lesson 7 I managed to build a custom CNN to do classification on my data. Now I’m trying to do roughly the same thing but for it to do regression and encounter several iissues.

First, my data’s shape (bs=64):

(torch.Size([64, 5, 3, 12]), torch.Size([64]))

Just imagine its an image but with 5 channels rather than the usual 3.

Second, my module is:

def conv2(ni,nf): return conv_layer(ni,nf,stride=2)
def conv_and_res(ni,nf): return nn.Sequential(conv_layer(ni,nf,stride=2), res_block(nf))

model = nn.Sequential(
    conv_and_res(5, 8),
    conv_and_res(8, 16),
    conv_and_res(16, 32),
    conv_and_res(32, 16),
    conv2(16, 1),
    Flatten()
)

For the classification I ended it with conv2(16,2) and for regression I just shifted nf to be 1.

Learner is:

learn = Learner(data, model, loss_func = nn.MSELoss(), metrics= [mse,r2_score],callback_fns=ShowGraph)

And right out the box when I’m trying to do lr_find I’m getting these:

UserWarning: Using a target size (torch.Size([64])) that is different to the input size (torch.Size([64, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size. return F.mse_loss(input, target, reduction=self.reduction)

And of course the training is going really bad.
I must say that if I do different transformation to my data and shape it like a regular imag (shape=[3,X,Y]) I manage to train a cnn_learner for regression with good results, but I wish to peruse the original path.

Any help would be appreciated, thank you

1 Like