Convolutional Neural Networks (CNN)
Table of Contents
%%html
<center><iframe src="https://www.youtube.com/embed/MHbgEOAbywA?rel=0"
width="560" height="315" frameborder="0" allowfullscreen></iframe></center>
%%html
<center><iframe src="https://www.youtube.com/embed/5zQgad2ukik?rel=0"
width="560" height="315" frameborder="0" allowfullscreen></iframe></center>
%%html
<center><iframe src="https://www.youtube.com/embed/YC-aHDmAe_g?rel=0"
width="560" height="315" frameborder="0" allowfullscreen></iframe></center>
Filter (or Kernel)
Filtering includes smoothing, sharpening and edge enhancement
Discrete convolution can be viewed as element-wise multiplication by a matrix
How to find the right Kernels
We learn many different kernels that make specific effect on images
Let’s apply an opposite approach
We are not designing the kernel, but are learning the kernel from data
Can learn feature extractor from data using a deep learning framework
The bird occupies a local area and looks the same in different parts of an image. We should construct neural networks which exploit these properties.
ANN structure for object detecion in image
Convolution of CNN
Typically have sparse interactions
Convolutional Neural Networks
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
mnist = tf.keras.datasets.mnist
(train_x, train_y), (test_x, test_y) = mnist.load_data()
train_x, test_x = train_x/255.0, test_x/255.0
train_x = train_x.reshape((train_x.shape[0], 28, 28, 1))
test_x = test_x.reshape((test_x.shape[0], 28, 28, 1))
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32,
(3,3),
activation = 'relu',
padding = 'SAME',
input_shape = (28, 28, 1)),
tf.keras.layers.MaxPool2D((2,2)),
tf.keras.layers.Conv2D(64,
(3,3),
activation = 'relu',
padding = 'SAME',
input_shape = (14, 14, 32)),
tf.keras.layers.MaxPool2D((2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation = 'relu'),
tf.keras.layers.Dense(10, activation = 'softmax')
])
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
model.fit(train_x, train_y)
test_loss, test_acc = model.evaluate(test_x, test_y)
print('loss = {}, Accuracy = {} %'.format(round(test_loss,2), round(test_acc*100)))
test_img = test_x[np.random.choice(test_x.shape[0], 1)]
predict = model.predict_on_batch(test_img)
mypred = np.argmax(predict, axis = 1)
plt.figure(figsize = (12,5))
plt.subplot(1,2,1)
plt.imshow(test_img.reshape(28, 28), 'gray')
plt.axis('off')
plt.subplot(1,2,2)
plt.stem(predict[0])
plt.show()
print('Prediction : {}'.format(mypred[0]))
from scipy.signal import spectrogram
from six.moves import cPickle
data = cPickle.load(open('./data_files/data.pkl','rb'))
# data saved as dictionary
print(data.keys())
# 2000 vibration signals
print(len(data['time']))
# 10000 points in each signal
print(len(data['time'][0]))
print(len(data['signal'][0]))
# binary classes
print(len(data['label'][0]))
# plot ramdonly selected three vibration signals
N = 2000
for _ in range(3):
idx = np.random.randint(N)
plt.plot(data['time'][idx], data['signal'][idx])
plt.title(np.argmax(data['label'][idx]))
plt.show()
# from vibration signal in 1D to STFT image in 2D
Fs = 12800
idx = np.random.randint(N)
x = data['signal'][idx]
f, t, Sxx = spectrogram(x,
Fs,
scaling = 'spectrum',
mode = 'magnitude')
plt.figure(figsize = (6, 6))
plt.pcolormesh(t, f, Sxx)
plt.title(np.argmax(data['label'][idx]))
plt.ylabel('Frequency [Hz]', fontsize = 15)
plt.xlabel('Time [sec]', fontsize = 15)
plt.show()
# Convert all data to STFT image in 2D
stft_data = []
for i in range(N):
f, t, Sxx = spectrogram(data['signal'][i],
Fs,
scaling = 'spectrum',
mode = 'magnitude')
stft_data.append(Sxx)
np.shape(stft_data)
from sklearn.model_selection import train_test_split
# train: test = 0.7: 0.3
train_x, test_x, train_y, test_y = train_test_split(stft_data, data['label'], test_size = 0.3)
# data type and shape conversion
train_x = np.asarray(train_x)
train_y = np.asarray(train_y)
train_x = np.expand_dims(train_x, 3)
train_y = np.argmax(train_y, axis = 1)
test_x = np.asarray(test_x)
test_y = np.asarray(test_y)
test_x = np.expand_dims(test_x, 3)
test_y = np.argmax(test_y, axis = 1)
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu',
padding = 'SAME',
input_shape = (129, 44, 1)),
tf.keras.layers.Conv2D(16, (3,3), activation = 'relu',
padding = 'SAME',
input_shape = (129, 44, 16)),
tf.keras.layers.MaxPool2D((2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation = 'relu'),
tf.keras.layers.Dense(2, activation = 'softmax')
])
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
model.fit(train_x, train_y, epochs = 20)
test_loss, test_acc = model.evaluate(test_x, test_y)
print('loss = {}, Accuracy = {} %'.format(round(test_loss, 8), round(test_acc * 100)))
test_img = test_x[np.random.choice(test_x.shape[0], 1)]
pred_ohe = model.predict_on_batch(test_img)
pred = np.argmax(pred_ohe, axis = 1)
plt.figure(figsize = (12,5))
plt.subplot(1,2,1)
plt.imshow(test_img.reshape(129, 44), 'jet', origin = 'lower')
plt.axis('off')
plt.subplot(1,2,2)
plt.stem(pred_ohe[0])
plt.xticks([0,1])
plt.show()
print('Prediction : {}'.format(pred[0]))
print('Probability : {}'.format(pred_ohe[0]))
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')