You are on page 1of 6

Jacob Chandran

IM 6, English 12 AP
5/13/17
UsingAutoencoderstoclassifyAgeRelatedMacularDegeneration

Code:

from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D

from keras.models import Model

from PIL import Image

import glob

import numpy as np

#####Load and list images#####

img_list = []

for filename in glob.glob('/home/chandje1/Data/Control/sample/*.jpg'):

im = Image.open(filename)

im = im.crop((0,0,28,28))

img_list.append(im)

data = np.stack([np.array(im) for im in img_list],axis=3)

data = data.T

print('data array shape')

print(data.shape)

#####Build the convolutional neural network#####

input_img = Input(shape=(3, 28, 28))

x = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(input_img)

x = MaxPooling2D((2, 2), border_mode='same')(x)


Jacob Chandran
IM 6, English 12 AP
5/13/17
x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(x)

x = MaxPooling2D((2, 2), border_mode='same')(x)

x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(x)

encoded = MaxPooling2D((2, 2), border_mode='same')(x)

# at this point the representation is (8, 4, 4) i.e. 128-dimensional

x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(encoded)

x = UpSampling2D((2, 2))(x)

x = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(x)

x = UpSampling2D((2, 2))(x)

x = Convolution2D(64, 3, 3, activation='relu')(x)

x = UpSampling2D((2, 2))(x)

decoded = Convolution2D(3, 3, 3, activation='sigmoid', border_mode='same')(x)

autoencoder = Model(input_img, decoded)

autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

#####Define training and testing data#####

x_train = data[:(len(data)/2)]

x_test = data[len(data)/2:]

print 'split data'

print np.shape(x_train)

print np.shape(x_test)
Jacob Chandran
IM 6, English 12 AP
5/13/17

x_train = x_train.astype('float32') / 255.

x_test = x_test.astype('float32') / 255.

x_train = np.reshape(x_train, (len(x_train), 3, 28, 28))

x_test = np.reshape(x_test, (len(x_test), 3, 28, 28))

#####Fit model to network#####

autoencoder.fit(x_train, x_train,

nb_epoch=50,

batch_size=128,

shuffle=True,

validation_data=(x_test, x_test),

#callbacks=[TensorBoard(log_dir='/tmp/autoencoder')]

decoded_imgs = autoencoder.predict(x_test)

print 'shape of x_test'

print np.shape(x_test[1])

import matplotlib.pyplot as plt

#####Display images#####

n = 10

plt.figure(figsize=(20, 4))

for i in range(n):

# display original

ax = plt.subplot(2, n, i)
Jacob Chandran
IM 6, English 12 AP
5/13/17
#plt.imshow(x_test[i].reshape(28, 28))

plt.imshow(x_test[i].transpose(1,2,0))

plt.gray()

ax.get_xaxis().set_visible(False)

ax.get_yaxis().set_visible(False)

# display reconstruction

ax = plt.subplot(2, n, i + n)

#plt.imshow(decoded_imgs[i].reshape(28, 28))

plt.imshow(decoded_imgs[i].transpose(1,2,0))

plt.gray()

ax.get_xaxis().set_visible(False)

ax.get_yaxis().set_visible(False)

plt.show()

Explanation:

The code featured above is for what is essentially called an autoencoder.

Autoencoders can be thought of a means to compression, compressing data and

images into matrixes and then vectors which are useful for data analysis and image

adaption. A convolutional autoencoder, which I have been working on, basically

takes an image, turns it into its matrix representation, flattens the matrix into a

vector, and then converts the data back to image. Unfortunately, some data is always

lost in the translation between the original image and the reconstructed image,

however, the better the autoencoder, the less detail is lost.

Autoencoders have a large significance in the field of image analysis. On its


Jacob Chandran
IM 6, English 12 AP
5/13/17
surface, image analysis may seem like a very mundane field of research, however, its

power is revealed when we recognize that images of diseases can be analyzed. This

is precisely what my project is about.

There is a prevalent eye disease in older people called Age Related Macular

Degeneration (AMD) where white spots begin to form on the eye and limit the

ability of the eye to see. The issue is that only with early detection can this disease

be cured, however, there are not many detection systems for AMD. Using

autoencoders, at APL, we have build an autoencoder that, trained with the images of

the healthy eye, can recognize deviations from this norm. Basically, by compressing

images from their picture state down to a vector state, by mathematical formula,

the center of balance of all the healthy images can be calculated. After this, a

standard deviation can be determined so that any image vector outside of this

center of balance is deemed unhealthy. This is a simple sounding solution to the

problem of detection, however, this code is just the foundation of this recognition

technology, and will also need to be converted into a user friendly interface through

an app for example. There are still many more steps that need to be taken for this

project to be finished completely.

It is exciting that a detection system for AMD may be close to

implementation, however the real power of this experiment is the idea the this kind

of detection could be universalized to all diseases. There would be immense


Jacob Chandran
IM 6, English 12 AP
5/13/17
progress in the field of medicine and treatment if we could detect disease early and

therefore limit the amount of sickness in the United States.

Along with AMD, in the lab, this research is also being applied to breast

cancer research. We have found ways to create artificial tissue and cancerous cysts

called phantoms. Using an ultrasound machine, we can take pictures of the cancer

just like it would be seen in a real person, and then analyze these images.

Ultrasound images are actually very noisy in that they create unclear pictures with

a lot of unrelated picture images. This is where autoencoders can be used. Even

though autoencoders are very prone to losing data, if they lose the right data in this

scenario, basically, all of the noise in the ultrasound image can be eliminated so that

it is easier to detect cancer in images. This is one case study of how losing data can

actually be beneficial to classification. Along with these two examples, breast cancer

and AMD, hopefully there will be many places to implement autoencoders so that

computer science can be used as a side by side tool that aids early detection in the

medical field.

You might also like