$$heading_1$$ Image Dataset Augmentation using Scikit-Image

$$heading_2$$ Part One: Introduction and Transformations

Imaging in deep learning for computer vision and classification problems have become a compelling way to impress friends and family, influence strangers and demonstrate the power and adaptability of artificial neural networks and convolutional networks. It can also assist researchers and systems in sorting, segmenting and identifying / classifying data, moving mundane tasks into the realm of automation and saving graduate students and TAs countless late nights sorting, indexing, checking and eventually spilling coffee all over the entirety of a project.

Image Classification relies on a sample set of images that have been ‘labeled’ with a desired output pictures of cats labeled with ‘cat’, and dogs with ‘dog’. The neural network that has been trained to classify these will learn features that images of cats possess, and will learn other features that identify dog images as dogs. But this requires a large quantity of labeled data; and can have complications what if all cat images have white backgrounds? the network may not even learn to identify features of a cat, but instead will learn that if the image has a white background, it’s a cat.

Using image transformations and some know-how, we can augment an existing dataset, expanding the effective size of the training data pool, and hopefully pushing the network to learn more meaningful features.

Let’s begin by looking at the basics of an imaging package, scikit-image

In $$2$$:
import numpy as np #numpy represents complex dimensional data as arrays

import matplotlib.pyplot as plt #some standard python plotting

import skimage #our imaging library

In $$3$$:
grump = skimage.io.imread$$extract_itex$$'turtle.jpg'$$/extract_itex$$


using scikit-image’s io module and imread function, we read in an image as a NumPy array of n-dimensions, filled with numbers that represent the pixel locations and color values.

In $$5$$:
print$$extract_itex$$type(grump$$/extract_itex$$)

print$$extract_itex$$'our image, as a numpy array, is shaped', grump.shape$$/extract_itex$$

plt.figure$$extract_itex$$figsize = (10, 8$$/extract_itex$$)
plt.imshow$$extract_itex$$grump$$/extract_itex$$
plt.show

<class 'imageio.core.util.Array'>
our image, as a numpy array, is shaped $$extract_itex$$667, 1000, 3$$/extract_itex$$


So Scikit-Image’s io.imread function reads in pixel values in the first two dimensions grump.shape$$0$$, grump.shape$$1$$, and RGB color channels in the third dimension, grump.shape$$2$$.

So to represent the image we have a set of three values for each pixel ‘location’

This means our image will have 667 values pixels – this first dimension is the number of rows, which might be counter-intuitive for you it is for me. The number of rows corresponds to the Y axis of the image.

The second dimension will be the X, and the final dimension, grump.shape$$2$$, contains values for the color channels, Red Green and Blue, or RGB. Scikit-Image has multiple color spaces, including RGBA, HSV, XYZ, CIE, and grayscale, and supports conversion between them easily.

So let’s just look at some of those specific pixel values

In $$17$$:
print$$extract_itex$$grump$$50, 50, :$$$$/extract_itex$$ #look at all RGB values for a specific array index, or one pixel's RGB value.
print$$extract_itex$$"these three numbers in brackets represent one pixel's values for a specific location in the image array."$$/extract_itex$$

'''and let's index into a specific section of the image array'''
print$$extract_itex$$"let's look at the first 5 pixels in the x and y axis"$$/extract_itex$$
plt.imshow$$extract_itex$$grump$$:5, :5, :$$$$/extract_itex$$
plt.show
print$$extract_itex$$grump$$:5, :5, :$$$$/extract_itex$$

$$81 76 72$$
these three numbers in brackets represent one pixel's values for a specific location in the image array.
let's look at the first 5 pixels in the x and y axis

$$[[72 64 62$$
$$75 67 65$$
$$76 68 65$$
$$79 71 68$$
$$82 73 68$$]

$$[68 59 60$$
$$71 62 63$$
$$69 60 61$$
$$72 64 62$$
$$72 62 61$$]

$$[60 55 52$$
$$61 56 53$$
$$64 59 56$$
$$63 55 53$$
$$64 56 54$$]

$$[54 51 46$$
$$53 50 45$$
$$54 51 46$$
$$55 50 46$$
$$56 51 47$$]

$$[46 45 41$$
$$51 47 46$$
$$49 45 42$$
$$48 44 41$$
$$48 44 41$$]]


$$heading_3$$ Let’s take a closer look at our shell-buddy

In $$10$$:
plt.imshow$$extract_itex$$grump$$/extract_itex$$
plt.title$$extract_itex$$"Grumpy's X and Y"$$/extract_itex$$
plt.xlabel$$extract_itex$$'x : image$$1$$'$$/extract_itex$$
plt.ylabel$$extract_itex$$'y : image$$0$$'$$/extract_itex$$
plt.show

#And now let's take a look at the individual channels of the image.
plt.figure$$extract_itex$$figsize = (20, 15$$/extract_itex$$)
plt.subplot$$extract_itex$$1,3,1$$/extract_itex$$
plt.imshow$$extract_itex$$grump$$:, :, 0$$, cmap = 'Reds'$$/extract_itex$$
plt.title$$extract_itex$$'First Color Channel'$$/extract_itex$$
plt.subplot$$extract_itex$$1,3,2$$/extract_itex$$
plt.imshow$$extract_itex$$grump$$:, :, 1$$, cmap = 'Greens'$$/extract_itex$$
plt.title$$extract_itex$$'Second Color Channel'$$/extract_itex$$
plt.subplot$$extract_itex$$1,3,3$$/extract_itex$$
plt.imshow$$extract_itex$$grump$$:, :, 2$$, cmap = 'Blues'$$/extract_itex$$
plt.title$$extract_itex$$'Third Color Channel'$$/extract_itex$$
plt.show

>
>

Ok, well, maybe the color channels would be better represented with a different image. But I do love this tortoise-friend. Let’s use a much more illustrative, albeit inferior, non-testudinidae-friendly image.

In $$14$$:
test_img = skimage.io.imread$$extract_itex$$'trial.jpg'$$/extract_itex$$

plt.figure$$extract_itex$$figsize = (18, 8$$/extract_itex$$)
plt.subplot$$extract_itex$$1,4,1$$/extract_itex$$
plt.imshow$$extract_itex$$test_img$$/extract_itex$$
plt.title$$extract_itex$$"Testing Image"$$/extract_itex$$
plt.subplot$$extract_itex$$1,4,2$$/extract_itex$$
plt.imshow$$extract_itex$$test_img$$:, :, 0$$ - (test_img$$:, :, 1$$ + test_img$$:, :, 2$$$$/extract_itex$$, cmap = 'Reds')
plt.title$$extract_itex$$"Red Channel"$$/extract_itex$$
plt.subplot$$extract_itex$$1,4,3$$/extract_itex$$
plt.imshow$$extract_itex$$test_img$$:, :, 1$$ - (test_img$$:, :, 0$$ + test_img$$:, :, 2$$$$/extract_itex$$, cmap = 'Greens')
plt.title$$extract_itex$$"Green Channel"$$/extract_itex$$
plt.subplot$$extract_itex$$1,4,4$$/extract_itex$$
plt.imshow$$extract_itex$$test_img$$:, :, 2$$ - (test_img$$:, :, 0$$ + test_img$$:, :, 1$$$$/extract_itex$$, cmap = 'Blues')
plt.title$$extract_itex$$"Blue Channel"$$/extract_itex$$
plt.show


Ok, another point worth delving into – why did I have to subtract some channels from the others to get something to display in the above example? Well, in this image we’re working in RGB – colors are represented by a set of integers or float values. Combining the three RGB channels – a red value of 0 to 255, Green value of 0 to 255, and Blue value of 0 to 255. A white pixel would be represented in the three channels by $$255, 255, 255$$, and black would be $$0, 0, 0$$

So when we go to plot, say, the red channel test_img$$:, :, 0$$, if we have a fully or very close to fully red object as we do in this test image, it would be represented by a single pixel value of 255, and a fully white pixel would be represented, in the red channel, by 255. So our red values would appear identical to our white pixels! Something to keep in mind if you hit some wierd display bumps in the road while dealing with color images!

$$heading_2$$ Back to Turtle, errr, Tortoise… Power!

Color is great and all, very nice, but for many purposes, we can get by with grayscale values – we’re eliminating a huge amount of the array’s data by converting – instead of holding 3 color values, it will only hold a single value per pixel.

Another aspect to note – before we get away from color channels – these don’t have to hold different colors. You could put different image processing techniques or different images entirely into different channels. Anything you can turn into an array, you can assign to a channel, since in NumPy it’s just another dimension.

Matplotlib’s Pyplot uses value maps called colormaps to display an image or data in different ways – these are worth examining when you’re doing any data or image representation. Default pyplotting of a black and white image will give you some crazy high-contrast colors, which can really be nice, but be sure to use the cmap argument if you want a more tame or interpretable representation.

In $$20$$:
grump_bw = skimage.color.rgb2gray$$extract_itex$$grump$$/extract_itex$$
grump_bw = skimage.util.img_as_float32$$extract_itex$$grump_bw$$/extract_itex$$

plt.figure$$extract_itex$$figsize = (20, 8$$/extract_itex$$)
plt.subplot$$extract_itex$$1,3,1$$/extract_itex$$
plt.imshow$$extract_itex$$grump$$/extract_itex$$
plt.xlabel$$extract_itex$$'Original RGB'$$/extract_itex$$
plt.subplot$$extract_itex$$1,3,2$$/extract_itex$$
plt.imshow$$extract_itex$$grump_bw$$/extract_itex$$
plt.xlabel$$extract_itex$$'Grayscale with default colormap'$$/extract_itex$$
plt.subplot$$extract_itex$$1,3,3$$/extract_itex$$
plt.imshow$$extract_itex$$grump_bw, cmap = 'gray'$$/extract_itex$$
plt.xlabel$$extract_itex$$'Grayscale in 32-bit'$$/extract_itex$$
plt.show


So why convert into float 32? weren’t we working in 64? or int values for the org image? Yes. Yes we were. Some image operations and processes will require specific array value types, and some will output different types, so knowing what you’re working in is important.

Use numpy’s dtype property!

In $$8$$:
print$$extract_itex$$'grump.dtype = ', grump.dtype$$/extract_itex$$
print$$extract_itex$$'grump_bw.dtype = ', grump_bw.dtype$$/extract_itex$$
print$$extract_itex$$'grump_bw_scaled.dtype = ', grump_bw.dtype$$/extract_itex$$

grump.dtype =  uint8
grump_bw.dtype =  float32
grump_bw_scaled.dtype =  float32


Congratulations, we have some of the basics of the basics covered, we can move on to the cool stuff that Scikit-Image is capable of! Let’s expose that tortoise! No… no, that came out wrong. We’re going to maximize exposure… no… no, again. Scikit-Image’s exposure module contains a bunch of useful image processes that will really get us into fighting shape.

skimage.exposure.___

In $$22$$:
rescale = skimage.exposure.rescale_intensity$$extract_itex$$grump_bw$$/extract_itex$$
#stretches the values in the image, setting the highest values to maximum allowed by datatype, and lowest to minimum

eqhist = skimage.exposure.equalize_hist$$extract_itex$$grump_bw$$/extract_itex$$
#histogram equalization will generally 'squash' intensity values to bump up the contrast over the whole image

adapthist = skimage.exposure.equalize_adapthist$$extract_itex$$grump_bw, clip_limit = .5$$/extract_itex$$
#adaptive version of the eqhist - this will use the same 'squash and bump', but across sections of the image

sigmoid = skimage.exposure.adjust_sigmoid$$extract_itex$$grump_bw$$/extract_itex$$
#sigmoidal contrast adjustment to rescale intensity values

log = skimage.exposure.adjust_log$$extract_itex$$grump_bw$$/extract_itex$$
#Logarithmic contrast adjustment to rescale intensity values

plt.figure$$extract_itex$$figsize = (20, 8$$/extract_itex$$)
plt.subplot$$extract_itex$$2,3,1$$/extract_itex$$
plt.imshow$$extract_itex$$grump_bw, cmap = 'gray'$$/extract_itex$$
plt.title$$extract_itex$$'OG Grump in 32-bit glory'$$/extract_itex$$
plt.xlabel$$extract_itex$$str(grump_bw.dtype$$/extract_itex$$)

plt.subplot$$extract_itex$$2,3,2$$/extract_itex$$
plt.imshow$$extract_itex$$eqhist, cmap = 'gray'$$/extract_itex$$
plt.title$$extract_itex$$'Equalize Histogram'$$/extract_itex$$
plt.xlabel$$extract_itex$$str(eqhist.dtype$$/extract_itex$$)

plt.subplot$$extract_itex$$2,3,3$$/extract_itex$$
plt.imshow$$extract_itex$$adapthist, cmap = 'gray'$$/extract_itex$$
plt.title$$extract_itex$$'Equalize Adaptive Histogram'$$/extract_itex$$
plt.xlabel$$extract_itex$$str(adapthist.dtype$$/extract_itex$$)

plt.subplot$$extract_itex$$2,3,4$$/extract_itex$$
plt.imshow$$extract_itex$$rescale, cmap = 'gray'$$/extract_itex$$
plt.title$$extract_itex$$'Rescale Intensity'$$/extract_itex$$
plt.xlabel$$extract_itex$$str(rescale.dtype$$/extract_itex$$)

plt.subplot$$extract_itex$$2,3,5$$/extract_itex$$
plt.imshow$$extract_itex$$log, cmap = 'gray'$$/extract_itex$$
plt.title$$extract_itex$$'Logarithmic Gamma Adjustment'$$/extract_itex$$
plt.xlabel$$extract_itex$$str(log.dtype$$/extract_itex$$)

plt.subplot$$extract_itex$$2,3,6$$/extract_itex$$
plt.imshow$$extract_itex$$sigmoid, cmap = 'gray'$$/extract_itex$$
plt.title$$extract_itex$$'Sigmoid Gamma Adjustment'$$/extract_itex$$
plt.xlabel$$extract_itex$$str(sigmoid.dtype$$/extract_itex$$)
plt.tight_layout
plt.show


Each of these processes has a place in different applications, but for scientific purposes, rescaling intensities, histogram equalizing and the adaptive version of histogram equalizing tend to have a leg up in a lot of applications, including many Machine Learning processes, by maximizing / minimizing contrast across sections of the image, we have a much more obvious representation of potential features. I mean, just look at Grump’s beautiful, shimmering shell detail in the adaptive histogram equalization – top right above.