
Going through a bunch of images in a folder trying to figure out which ones to keep can be troublesome. To solve this, I decided to automate the process by building an image classifier that classifies photos according to whether or not I want to keep them.
In this article, we will implement a simple binary classifier using Keras to classify photos as "Keep it" or "Don’t Keep it", to automate the process of filtering out your personal photo album.
If you prefer, you can check out my Youtube video on this topic here:
The Steps
The steps to do this will be:
- Set up and installation
- Creating the dataset
- Creating the train/test folders
- Training the model
- Running the app
If you want to skip this article and go straight to the code, you can find it here. Now, let’s go through each of these steps.
Set up and installation
First, we will create an environment with conda and install the necessary packages (keras
, matplotlib
and streamlit
):
conda create -n photo_album_sorter_ml
pip install -r requirements.txt
Creating the dataset
The first step we will take is to sort the images by creation time and move them to a folder named files_with_dates,
just to keep the images relatively organized:
In this script, we are simply looping over a folder that is provided by the user and moving the images to the files_with_dates
folder while adding the creation time to the names of the files.
To run this, type in the terminal:
python sort_creation_time.py - path ./path/to/your/images/folder
Now, we can create the folders for the dataset and we will label the images to train the classifier.
Here, we are simply creating the data folders for the dataset and setting up a simple loop using matplotlib to classify the images as "keep it" or "don’t keep it". To run this, do:
python create_dataset.py
Now we will split the files into train and test folders.
Here we are just splitting the images with 80% for training and 20% for testing. To run do:
python create_train_test.py
Training the model
We will train two types of classifiers, starting with a simple Keras implementation of a classic CNN borrowed from this blog post by François Chollet.
To run:
python train.py - epochs 10 - batch_size 16
Be careful with the size of your images, in this implementation we resize all images to 150×150 according to the example in the blog post for cats and dogs.
Even though the performance with this model was not great (around 58% on my example dataset), I used it as a baseline for the problem.
We also train a pre-trained inception v3 model using template code taken from the Keras documentation.
In here we are just following a basic setup for fine tunning an inception v3 model and adding a little CLI tool to make it easy to run:
python train_pretrained.py - epochs 10 - batch_size 16
This model yielded some pretty impressive results without overfitting and reaching up to 93% performance on the test set!
This is great news but since each person will use their own dataset, there should be different obtained performances, depending on a series of factors like the nature of the images or the type of photos you want to exclude from your album.
Running the app
Now, after training, to see the classifier in action, we will write a simple streamlit app to wrap our model in:
To run, type:
streamlit run app.py
You can load an image with streamlit’s file uploader and the classification will be written below the image.
Final Thoughts on Using ML for Filtering Personal Photos
Given the interpolative nature of Machine Learning models, we always run the risk the model will misclassify images which could culminate in us losing important photos we care about, so my idea is to use this as a kind of first approach to clear out the clutter images, but always taking a general look before deleting the images completely.
If you liked this post, join Medium, follow, subscribe to my newsletter. Also, subscribe to my youtube channel connect with me on Tiktok, Twitter, LinkedIn, Instagram! Thanks and see you next time! 🙂
If you are interested in machine learning gear, here is an affiliate link for a 3070 GPU from MSI:
MSI Gaming GeForce RTX 3070 LHR 8GB GDRR6 256-Bit HDMI/DP Nvlink Torx Fan 4 RGB Ampere Architecture…
This is an affiliate link, if you buy the product I get a small commission, cheers! 🙂