Flower17 Species ClassificationΒΆ

This example will show you how to train and test a model on the FLOWER17 dataset. This dataset contains 17 classes of flower species, each having 80 images.

Download the dataset and verify with with sha384sum.

curl -LO http://www.robots.ox.ac.uk/~vgg/data/flowers/17/17flowers.tgz
sha384sum -c - << EOF
ad5c0d6272d52d2d2c27726fde18bcede3f8a2c6cd834891bf9819fac4e3f6047cfb8bd921851cb3048323df3ad06e3d 17flowers.tgz
EOF
17flowers.tgz: OK

Extract the dataset.

$ tar -xzf 17flowers.tgz

All the images are now in a folder called jpg. We split these images into flower_dataset/train and flower_dataset/test directories, each directory containing sub-directories corresponding to the 17 flower classes using split.py.

split.py

import shutil
import os

directory = "jpg"
target_train = "flower_dataset/train"
target_test = "flower_dataset/test"

if not os.path.isdir(target_train):
    os.makedirs(target_train)

if not os.path.isdir(target_test):
    os.makedirs(target_test)

classes = [
    "daffodil",
    "snowdrop",
    "lilyvalley",
    "bluebell",
    "crocus",
    "iris",
    "tigerlily",
    "tulip",
    "fritillary",
    "sunflower",
    "daisy",
    "coltsfoot",
    "dandelion",
    "cowslip",
    "buttercup",
    "windflower",
    "pansy",
]

j = 0
for i in range(1, 1361):
    if (i - 1) % 80 < 70:
        label_dir = os.path.join(target_train, classes[j])
    else:
        label_dir = os.path.join(target_test, classes[j])

    if not os.path.isdir(label_dir):
        os.makedirs(label_dir)

    filename = "image_" + str(i).zfill(4) + ".jpg"
    shutil.copy(
        os.path.join(directory, filename), os.path.join(label_dir, filename)
    )

    if i % 80 == 0:
        j += 1

We execute the split.py file. The train directory will contain 70 images for each class while the test directory will contain 10 images for each class.

$ python split.py

Now that we have our flower dataset ready, we can perform classification of the flower species. For this example, we will be going through 2 approaches: