Training a model on MNIST Images

Hello Everyone,

I am trying to train a model to identify hand-written digits by using the MNIST dataset. (Not the tabular form, rather the jpg images themselves). All the jpg images used in the training/validation are in black and white.

I have been able to train a model that performs very well on the validation as well as the test data.

However, I tried to see if the model works on a different image by writing a digit on a post-it note with a marker and passing an image of it through the model. The model seems to be finding it difficult to recognise this since the marker as well as the post-it note are coloured (different colours obviously). When I convert this post-it note image into black and white, the model is again able to recognise it very easily.

Now here is my question: Is there some kind of batch transform in fastai that I can use to augment my training data by using different colour schemes, so that the eventual model can recognise the image irrespective of what the background/digit colour is? (without me having to convert it to B&W)

Thanks in advance for the help.

Nice explanation.
I think it is better to preprocess the images and put them on BW before making inference.
Anyway, if you want to augment your data this way, you could write a transform to do so, that replaces the background by random colors. (replace zeros by other colors).

1 Like

Ok. You are right. Preprocessing the inference image seems to be the simpler, more elegant solution.

I now need to figure out if/how I can bundle this pre-processing step into the .pkl file that I export (like an inference-pipline). Doing so would be convenient if I want to explore the possibility of building a web interface as a front-end to the model for inference.

Thank you for the suggestion.

For web inference, if you’re going to be using PIL, you could simply do img.convert('L') to turn your image black & white, no need to include it in the model itself.


Nice suggestion. I just implemented a slightly modified version of this suggestion and the web application works perfectly fine now.

Since I was already using OpenCV in my project, I went with cv2 instead of PIL.


1 Like

You’re welcome!

just be aware of this:

1 Like