import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
%matplotlib inline
import sklearn
from sklearn.model_selection import train_test_split
# import warnings
# import os
# from os.path import join
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
from google.colab import drive
drive.mount('/content/drive')
df = pd.read_pickle('/content/drive/MyDrive/tutorials/산학협동강좌/data/wafers.pkl')
# USB로 파일을 다운 받으신 경우
# df = pd.read_pickle('wafers.pkl')
df.head()
print('Number of Center Failures: {}'.format(len(np.where(df.encoded_labels == 0)[0])))
print('Number of Edge-Local Failures: {}'.format(len(np.where(df.encoded_labels == 1)[0])))
print('Number of Edge-Ring Failures: {}'.format(len(np.where(df.encoded_labels == 2)[0])))
print('Number of Local Failures: {}'.format(len(np.where(df.encoded_labels == 3)[0])))
Different Image Size → Image Resizing
random_indices = np.random.choice(range(1200), 20, replace=False)
plt.figure(figsize = (10, 10))
for i in range(20):
ax = plt.subplot(4, 5, i + 1)
ax.axis('off')
ax.imshow(df['waferMap'][random_indices[i]])
ax.set_title('Shape: {}'.format(df['waferMap'][random_indices[i]].shape))
Resizing for Same Size
resized_images = []
for i in range(len(df)):
img = df.iloc[i]['waferMap']
resized_img = cv2.resize(img, (32, 32))
resized_images.append(resized_img)
df['waferMap_resized'] = resized_images
plt.figure(figsize = (10, 10))
for i in range(20):
ax = plt.subplot(4, 5, i + 1)
ax.axis('off')
ax.imshow(df['waferMap_resized'][random_indices[i]])
ax.set_title('Shape: {}'.format(df['waferMap_resized'][random_indices[i]].shape))
Data Split: Train/Test
all_x = df['waferMap_resized'].values
all_y = df['encoded_labels'].values
train_x, test_x, train_y, test_y = train_test_split(all_x, all_y, test_size = 0.2, random_state = 42)
print(train_y.shape)
print(test_y.shape)
train_x = np.stack(train_x).reshape(-1, 32, 32, 1)
train_y = np.stack(train_y)
test_x = np.stack(test_x).reshape(-1, 32, 32, 1)
test_y = np.stack(test_y)
print('Train x : {}, y : {}'.format(train_x.shape, train_y.shape))
print('Test x: {}, y : {}'.format(test_x.shape, test_y.shape))
Build CNN Model
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3, 3),
activation = 'relu',
padding = 'SAME',
input_shape = (32, 32, 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(input_shape = (7, 7)),
tf.keras.layers.Dense(32, activation = 'relu'),
tf.keras.layers.Dense(4, activation = 'softmax')
])
model.summary()
Training
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
history = model.fit(train_x, train_y,
epochs = 10,
batch_size = 32)
history.history.keys()
Learning Curve
# accuracy plot
plt.plot(history.history['accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.show()
# loss plot
plt.plot(history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.show()
Test Result
test_loss, test_acc = model.evaluate(test_x, test_y, verbose = 2)
print('loss = {}, Accuracy = {} %'.format(test_loss, test_acc * 100))
pred_y = model.predict_on_batch(test_x[:4])
for i in range(4):
plt.figure(figsize = (8, 4))
plt.subplot(1, 2, 1)
plt.imshow(test_x[i].reshape(32, 32))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.stem(pred_y[i])
plt.xticks(np.arange(4), ['Center', 'Edge-Loc', 'Edge-Ring', 'Loc']) # Set text labels.
plt.show()
print('Prediction : {}'.format(np.argmax(pred_y[i])))
print('Probability : {}'.format(pred_y[i]))
Confusion Matrix
import seaborn as sns
from sklearn.metrics import confusion_matrix
#Predict
pred_y = model.predict(test_x)
pred_y = np.argmax(pred_y, axis = 1)
result = confusion_matrix(test_y, pred_y, normalize = 'pred')
print(result)
plt.figure(figsize = (6, 5))
heatmap = sns.heatmap(result, annot = True)
heatmap.yaxis.set_ticklabels(['Center', 'Edge-Loc', 'Edge-Ring', 'Loc'], fontsize = 10)
heatmap.xaxis.set_ticklabels(['Center', 'Edge-Loc', 'Edge-Ring', 'Loc'], fontsize = 10)
plt.ylabel('True label', fontsize = 13)
plt.xlabel('Predicted label', fontsize = 13)
plt.show()