Back to the estimator

About

LeanAI

I'm a Computing and Mathematics (COMA) student at Queen's University. LeanAI was my first machine learning project built end to end: I collected the dataset, labelled it, trained and compared models, and deployed the result as the web app you just used.

Why I built it

I've lifted for a long time, and body fat percentage is the number that decides whether you should be building muscle or losing fat. I used to post progress photos to r/guessmybf for estimates. When I started learning deep learning, automating that judgement from a single photo seemed like a good first real problem: my own data, a regression target, and a result people could actually use.

Getting data

My first plan was to train on photos paired with medical-grade measurements such as DEXA or MRI scans. There isn't enough public data like that to train on, so I changed the goal: learn to reproduce the consensus estimate of experienced fitness communities instead.

I wrote an asynchronous scraper with PRAW (the Python Reddit API Wrapper) to collect posts and comment threads from r/guessmybf and r/bulkorcut, giving about 800 posts and roughly 1,600 labelled photos. I split these into 1,264 training and 316 validation images. All usernames and identifying information were removed. The data was used only for this non-commercial project and was never redistributed.

Labelling with an LLM

Reddit comments are noisy. Jokes and sarcasm made simple number extraction unreliable, so I used the OpenAI API as a filter. Each post's comments went through one prompt that returned only the serious estimates as a parseable list. The mean of those estimates became the training label.

You extract body fat % predictions from a Python array of Reddit comments.
Return an array of the predictions in integer form. Ignore arbitrary numbers.
If the user gives a range (like 12-15%), return the mean.
Output only a valid Python list of integers, like [12, 18, 22].

Training and iterating

I used fastai on top of PyTorch so I could prototype quickly. The table below shows the main iterations. The biggest improvement came from fixing the data, not from changing the model. Some photos showed a full body in a large room and others were tight torso shots, so I used MediaPipe pose landmarks to crop every image to the region between the shoulders and hips.

StepWhat changedResult
BaselineResNet-50, RMSE loss, basic augmentation, 20 epochsRMSE 9.9
Loss functionsCompared RMSE, root mean cubed error, Huber and Smooth L1 against MSEMSE kept
Pose-based croppingMediaPipe shoulder/hip landmarks crop every photo to the torsoMAE < 3
Ensemble11 architectures combined with per-model weightsDeployed model

I chose MSE partly because it gave the best results and partly because I could reason about it directly: its square root is the typical size of a prediction error in percentage points. That gave me a concrete target of about 2 points of average error.

The ensemble

Each architecture made different mistakes, so the final model combines eleven of them. I trained them with varying augmentation pipelines, combined their predictions with per-model weights, and exported the whole ensemble as one TorchScript graph so the server doesn't need fastai at runtime.

resnet50resnet50dresnet34densenet121efficientnet_b3efficientnet_b0regnety_040mobilenetv3_large_100ghostnet_100maxvit_tiny_rw_224efficientformerv2_s0

Deployment

The model is served by a small FastAPI app in a Docker container on Hugging Face Spaces. It decodes the uploaded image, resizes it to 224×224 and runs the ensemble. This page is a Next.js and TypeScript client that sends the photo to that API. I also published a Gradio version of the model on Hugging Face.

What I learned

  • Data quality set the ceiling. Cropping did more for accuracy than any architecture or loss change.
  • When ideal data doesn't exist, reframing the problem can make it solvable, as long as you're honest about what the labels mean.
  • LLMs are useful as a tool inside a data pipeline, not just as a product.
  • Ensembling and careful validation splits give steadier predictions than chasing a single best model.
  • A model isn't finished until someone else can use it: exporting, containerizing and serving were their own engineering problems.

Limitations

The labels are community estimates, not scans, so the model learns Reddit's judgement along with its biases. The source communities are mostly male, and lighting, posture and camera angle all move the estimate. Treat the result as an estimate, not a measurement.

Where it fits

  1. 2025

    fast.ai Practical Deep Learning

    Worked through the course notebooks: image classifiers, neural nets from scratch, random forests.

  2. Jun 2, 2025

    duck_or_desert

    First end-to-end model: a ResNet-18 image classifier on scraped images with basic augmentation.

  3. Jun 2025

    LeanAI

    This project. My own dataset, a regression task instead of classification, and a deployed ensemble.

  4. Jun 17, 2025

    lora-from-scratch

    Implemented the LoRA paper on top of a pre-trained Hugging Face language model.

  5. Jun–Jul 2025

    llm-from-scratch

    A transformer language model written in pure NumPy, with hand-written backpropagation and LoRA.