Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Self-organizing map-based quantization

error from images


John M. Wandeto1
Birgitta Dresp-Langley2
1
Dedan Kimathi University of Technology, department of Information
Technology, Nyeri, KENYA
2
Centre National de la Recherche Scientifique (CNRS), UMR 7357
ICube Lab, University of Strasbourg, FRANCE

1 Introduction
Self-Organizing Map (SOM) is an unsupervised machine learning algorithm
(Kohonen, 1981) that is commonly used for pattern analysis in data. Given
some input data vector, SOM organises it in related categories based on
similarity and original location of each vector.
The difference between the data and SOM’s final weights is known as
the Quantization Error (QE).
This code (in Python programming language) demonstrates the deter-
mination of the QE values for images based on a trained SOM, hereafter
referred to as SOM-QE. On exploring the functional properties of SOM-QE
value on sets of image series, it is found to be a good detector of changes
between images, (Wandeto, Nyongesa, Rémond, & Dresp-Langley, 2017).

2 The code
from pylab import imread
from numpy import reshape
import time
from minisom import MiniSom

"""
see https://github.com/JustGlowing/minisom/blob/master/Readme.md

1
on more, including instructions on how to install the it.
"""
start_time = time.time() # capture the starting time

# read image to use in training the SOM


image = imread(‘train_image.jpg’)

#pre-process the data, if necessary


def normalize(image):
image_rng = image.max()-image.min()
image_min = image.min()
return (image-image_min)*255/image_rng

"""
re-structure the input data to be usable by SOM.
The input will be a 3D vector to cater for the RGB values per pixel"""

input_image = reshape(image,(image.shape[0]*image.shape[1],3))

# SOM initialization and training


print(‘Training...’)
som = MiniSom(4,4,3,sigma=1.2,learning_rate=0.2) # a 4 by 4 map.
som.random_weights_init(input_image) #pick initial values randomly from the
input data
som.train_batch(input_image,10000) # train the map for 10000 iterations
train_time = time.time() #capture the time at which training ends
print(‘Training took --- %s seconds ---’ % round(time.time() - start_time, 2))
print ‘done’

"""
Using the trained SOM, determine the quantization error of each
image in the image series

"""
print ‘.... QE for the training image’
print som.quantization_error(input_image)

# .. For the subsequent images, read an image, restructure it and find its QE

print ‘....example to find QE of image named first.jpg’

2
im1 = imread(‘first.jpg’)
im1 = reshape(im1,(im1.shape[0]*im1.shape[1],3))
print som.quantization_error(im1)
print ‘The end’

3 References:
Kohonen, T. (1981). Automatic formation of topological maps of patterns
in a self-organizing system. In 2nd Scand. Conf. on Image Analysis (pp.
214–220). Espoo, Finland.
Wandeto, J. M., Nyongesa, H., Rémond, Y., & Dresp-Langley, B. (2017).
Detection of small changes in medical and random-dot images compar-
ing self-organizing map performance to human detection. Informatics in
Medicine Unlocked, 7, 39–45. https://doi.org/10.1016/j.imu.2017.03.001

You might also like