A Grid of Numbers
What an image is, and why a pixel is a poor feature
An image is a grid of numbers. Everything in computer vision follows from that one fact, and most of the difficulty follows from a second: the numbers describe light, not objects, and nothing in them says where one thing ends and another begins.
from sklearn.datasets import load_digits
digits = load_digits()
X, y = digits.images, digits.target
RAMP = ' .:-=+*#%@'
def show(img, scale=16.0):
for row in img:
print(''.join(RAMP[min(9, max(0, int(v / scale * 9.999)))]
for v in row))
print('%d images, each %d by %d' % X.shape)
print('values run from %.0f to %.0f' % (X.min(), X.max()))
print()
print('this one is labelled %d:' % y[0])
show(X[0])
print()
print('the top left corner as numbers:')
print(X[0][:4, :4])
values run from 0 to 16
this one is labelled 0:
-%+
%@*@-
.@. *=
:# ==
-= +=
:* #=
.%-*#
-%*
the top left corner as numbers:
[[ 0. 0. 5. 13.]
[ 0. 0. 13. 15.]
[ 0. 3. 15. 2.]
[ 0. 4. 12. 0.]]
Eight by eight, one number per pixel for brightness. A colour photograph is the same idea with three numbers per pixel, and a modern camera image is a few million pixels rather than sixty-four, but the structure does not change.
What makes this hard
from sklearn.datasets import load_digits
digits = load_digits()
X, y = digits.images, digits.target
RAMP = ' .:-=+*#%@'
def show(img, scale=16.0):
for row in img:
print(''.join(RAMP[min(9, max(0, int(v / scale * 9.999)))]
for v in row))
import numpy as np
a = X[0]
shifted = np.roll(a, 1, axis=1)
brighter = np.clip(a * 1.4, 0, 16)
print('%-28s %10s' % ('', 'pixels changed'))
for name, img in [('shifted one pixel right', shifted),
('40% brighter', brighter)]:
print('%-28s %10d' % (name, int((img != a).sum())))
print()
print('to a human both are obviously the same digit')
print('to a model comparing pixels they are largely different images')
shifted one pixel right 46
40% brighter 35
to a human both are obviously the same digit
to a model comparing pixels they are largely different images
Why a pixel is a poor feature
Moving an image one pixel changes most of its numbers while changing nothing about what it depicts. Brightness, contrast, rotation and scale all do the same. A model treating each pixel position as an independent feature has to learn every appearance of every object separately, which is why the whole field is about finding representations that survive these changes.