Monday, November 12, 2018

Building an image classifier quickly

Updated on 24th June 2020

Here are my steps:
1. Installed Ubuntu subsystem on my Windows 10 machine.
sudo -i
apt update
apt install python-pip
sudo rm -rf /etc/apt/apt.conf.d/20snapd.conf(had to run this due to an error)
apt install python-pip
apt install tensorflow 
apt update
apt install tensorflow
apt install python-pip
apt install tensorflow
apt install python3-pip
pip3 install --user --upgrade tensorflow
pip install numpy
pip install tensorflow
git clone https://github.com/googlecodelabs/tensorflow-for-poets-2 (HEAD: bc96088a4de86729920e120111f5b208f7f1cbb1)
cd tensorflow-for-poets-2
mkdir -p tf_files/images/useful
mkdir -p tf_files/images/useless
Put images in above folders

Training:
11. IMAGE_SIZE=224
12. ARCHITECTURE="mobilenet_0.50_${IMAGE_SIZE}"
13.  python -m scripts.retrain --bottleneck_dir=tf_files/bottlenecks --model_dir=tf_files/models/"${ARCHITECTURE}" --summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}"  --output_graph=tf_files/retrained_graph.pb  --output_labe
ls=tf_files/retrained_labels.txt --architecture="${ARCHITECTURE}"  --image_dir=tf_files/images

Testing:
12. python -m scripts.label_image  --graph=tf_files/retrained_graph.pb  --image=YOUR_PATH_TO_IMAGE_HERE


I built it for classifying whatsapp images into personal vs generic(good morning messages etc). It works quite well.

Shell script to test and move images into respective folders:

#!/bin/bash
for filename in /mnt/c/images/actual/*.jpg; do
        echo $filename
        dest=`python -m scripts.label_image  --graph=tf_files/retrained_graph.pb  --image=$filename | head -n4 | tail -n1 |grep -o '^\S*'`
        echo $dest
        mv $filename /mnt/c/images/out/$dest
done

No comments:

Blog Archive