[SOLVED] Help! What am I doing wrong with my regression?

Dear all,

I’m trying to build a conv neural net to guess the age of someone based on their picture, using the UTK faces dataset. I did it once, a while ago when I was getting into deep learning for the first time and I remember getting pretty decent results. I lost that file and I am attempting to replicate this result. Unfortunately, I’m getting garbage!

I’ve been trying for hours and hours, and my neural net ALWAYS spits a number between 29 years and 30 years for some reason (I’m guessing it’s the average age in the dataset). I have no idea why!

I’ve tried changing the arch to resnet 34 and resnet 50, adding more linear layers, adding dropout and batchnorm, changing image size to 192 and to 224, training only the last few layers vs training all layers, setting the loss to F.mse_loss and F.l1_loss.

I’m always getting numbers between 29 and 30.

The strange thing is that I remember doing this a while ago with the old fast.ai, and getting pretty good results very quickly. And then coming up with custom loss functions so that it’s not that bad guessing 90 when the age is 80, but it’s pretty bad guessing 11 when the age is 1, and doing pretty well on the test set.

Thank you!

1 Like

Just guessing, but could it be that the output is between - infinity to infinity, but your regression prediction needs to be between 0 to 90?
Maybe you need to squish the output

age = F.sigmoid(current_output) * (max_age_value - min_age_value) + min_age_value

Thank you for your answer. I tried again with your suggestion, same result :frowning:.

I have the same result while trying to predict the age of somebody based on their ches x rays. While it is hard even for a experienced radiologist it is not that difficult to predict whether someone is <20 or < 60 or >60 and still I get a more or less constant prediction every time.

The predicted age is nowhere near the mean or median age of the sample so that is much more interesting.

I’m very much interested what other people are suggesting here :slight_smile:

@alvisanovari has a working one using v1 here
seems like the label class has to be a Floatlist

1 Like

Excellent, I’ll try that, thank you. It seems I don’t have access to that post, but I’ll check out the Floatlist thing and report my results here.

Nope, didn’t work when adding label_cls=FloatList to label_from_func. Still getting that everyone is aged 29 years old :frowning:

I even tried dividing into 6 categories: baby (0-3), kid (4-13), teenager (14-20), young_adult (21-39), adult (40-69), elder_adult (70+) and got, after 3 epochs with resnet18, 84% accuracy! And the errors are mostly understandable: saying a 43 year-old is a young adult, etc.

But directly doing regression on the age… that’s too hard for the neural net somehow, so it just guesses 29 for everyone.

Okay, so I don’t know what’s wrong, BUT, I did trained a CNN the old-fashioned way with pure pytorch, and I’m getting much better results right away.

So I don’t know if this is a bug in the fast.ai library or in my code.

SOLVED!!! Finally, after so many hours, it was really dumb: My model was spitting things of shape (bs,1) and fast.ai was spitting things of shape (bs), which, together with broadcasting, was getting garbage.

So it’s solved now. I don’t know how to help others not to fall into the same trap. :slight_smile:

Thank you all.

1 Like

Did you achieve it in fast.ai now or did you stay at pure pytorch? Would be very interested in a code example in fastai :slight_smile:

Sure thing. I managed in fast.ai, and uploaded the new, fixed, working version here: https://github.com/mraggi/PlaygroundTestingSharing/blob/master/MinimalAgeFinder.ipynb

The only real change was that the model had to do a “squeeze” at the end :slight_smile:

Thanks for the link.

I implented your model but somehow I’m still stuck with numbers in a very narrow range. Now it actually predicts the same for every image, lol. I have a l1_loss of 7-8 which shouldnt be too bad.

I added a sigmoid function to the last layer to squeeze the output into the range of the actual output. somehow that doesnt improve things either.

Would you be able to share your xray data? I can try and see if I spot the error.

I’m currently experimenting in a kaggle kernel. I will share it here.

Here it is: https://www.kaggle.com/rasmus01610/nih-chest-x-ray-age

Hey @mraggi , thanks for your work here. It really helped me in my project.

I am very new to DL and have two questions on your work:

a. learn.split : If I understand it correctly, the choice you made is an arbitrary one and I can choose different splits and different number of splits as well.

b. Prediction on a single image: I am really struggling here. If I use factory method of cnn_learner, learn.predict is quite simply applied to image. But when I am using your method of “Learner”, I am getting some weird errors such as "Attribute Error: ’Tensor’ object has no attribute 'apply_tfms’

I will be really glad if you can help me here.

Best Regards