Bound label to Image

Question:

From the mnist dataset example I know that the dataset look something like this (60000,28,28) and the labels are (60000,). When, I print the first three examples of Mnist dataset first three examples of Mnist dataset

and I print the first three labels of those which are:

first three labels of mnist dataset

The images and labels are bounded.

I want to know how can I bound a folder with (1200 images) with size 64 and 64 with an excel with a column named "damage", with 5 different classes so I can train a neural network.

Like image of a car door and damage is class 3.

Asked By: Dude Rar

||

Answers:

Here’s a rough sketch of how you can approach this problem.

Loading each image

The first step is how you pre-process each image. You can use Python Imaging Library for this.

Example:

from PIL import Image

def load_image(path):
    image = Image.open(path)
    # Images can be in one of several different modes.
    # Convert to single consistent mode.
    image = image.convert("RGB")
    image = image.resize((64, 64))
    return image

Optional step: cropping

Cropping the images to focus on the feature you want the network to pay attention to can improve performance, but requires some work for each training example and for each inference.

Loading all images

I would load the images like this:

import glob
import pandas as pd

image_search_path = "image_directory/*.png"

def load_all_images():
    images = []
    for path in glob.glob(image_search_path):
        image = load_image(path)
        images.append({
            'path': path,
            'img': image,
        })
    return pd.DataFrame(images)

Loading the labels

I would use Pandas to load the labels. Suppose you have an excel file with the columns path and label, named labels.xlsx.

labels = pd.read_excel("labels.xlsx")

You then have the problem that the images that are loaded are probably not in the same order as your file full of labels. You can fix this by merging the two datasets.

images = load_all_images()
images_and_labels = images.merge(labels, on="path", validate="1:1")
# check that no rows were dropped or added, say by a missing label
assert len(images.index) == len(images_and_labels.index)
assert len(labels.index) == len(images_and_labels.index)

Converting images to numpy

Next, you need to convert both the images and labels into a numpy dataframe.

Example for images:

import numpy as np

images_processed = []
for image in images_and_labels['img'].tolist():
    image = np.array(image)
    # Does the image have expected shape?
    assert image.shape == (64, 64, 3)
    images_process.append(image)
images_numpy = np.array(images_processed)
# Check that this has the expected shape. You'll need
# to replace 1200 with the number of training examples.
assert images_numpy.shape == (1200, 64, 64, 3)

Converting labels to numpy

Assuming you’re setting up a classifier, like MNIST, you’ll first want to decide on an ordering of categories, and map each element of that list of categories to its position within that ordering.

The ordering of categories is arbitrary, but you’ll want to be consistent about it.

Example:

categories = {
    'damage high': 0,
    'damage low': 1,
    'damage none': 2,
}

categories_num = labels_and_images['label'].map(categories)
# Are there any labels that didn't get mapped to something?
assert categories_num.isna().sum() == 0
# Convert labels to numpy
labels_np = categories_num.values
# Check shape. You'll need to replace 1200 with the number of training examples
assert labels_np.shape == (1200,)

You should now have the variables images_np and labels_np set up as numpy arrays in the same style as the MNIST example.

Answered By: Nick ODell