Introduction to Image Classification with TensorFlow — Part 2

A beginner’s practical guide to Computer Vision in Python

Zolzaya Luvsandorj
Towards Data Science
10 min readOct 3, 2022

--

In the first part of the series, we built basic image classification models on MNIST dataset containing black and white images of handwritten digits. The data was readily available through TensorFlow. However, in practice, real life images are colourful and data is often not easily available like that. In this post, we will practice loading the image data ourselves and building models on colour images. We will also learn a little bit about Transfer Learning.

Photo by Mae Mu on Unsplash

📦 Data

We will use images of 15 kinds of vegetables that were collected from vegetable farm and market. The official paper on this dataset is available here. The dataset is available through CC BY-SA 4.0 licence.

If you want to follow along with the tutorial, please download the dataset from Vegetable Image Dataset | Kaggle and save the data in a folder called data that is located in the same directory as your notebook and rename the validation subdirectory to valid. Once complete, your working directory would be structured similar to below:

image_classification
├── *.ipynb
├── data
│ ├── train
│ │ ├── class 1
│ │ │ ├── image1.jpg
│ │ │ ├── ...
│ │ │ ├── imagen.jpg
│ │ ├── ...
│ │ ├── class n
│ │ │ ├── image1.jpg
│ │ │ ├── ...
│ │ │ ├── imagen.jpg
│ ├── valid
│ │ ├── class 1
│ │ │ ├── image1.jpg
│ │ │ ├── ...
│ │ │ ├── imagen.jpg
│ │ ├── ...
│ │ ├── class n
│ │ │ ├── image1.jpg
│ │ │ ├── ...
│ │ │ ├── imagen.jpg
│ ├── test
│ │ ├── class 1
│ │ │ ├── image1.jpg
│ │ │ ├── ...
│ │ │ ├── imagen.jpg
│ │ ├── ...
│ │ ├── class n
│ │ │ ├── image1.jpg
│ │ │ ├── ...
│ │ │ ├── imagen.jpg

This is a perfect format to organise the images. Each partitioned dataset has images of different classes saved in a separate subdirectory named after the class.

We will now load the libraries and check the total number of images:

import pathlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import seaborn as sns…

--

--