반응형
In [1]:
import gc
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 교차검증 lib
from sklearn.model_selection import StratifiedKFold,train_test_split
from tqdm import tqdm_notebook
#모델 lib
from keras.datasets import mnist
from keras.utils.np_utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, Model
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from keras.layers import Dense, Dropout, Flatten, Activation, Conv2D, AveragePooling2D
from keras import layers
from keras.optimizers import Adam,RMSprop
#모델
from keras.applications import VGG16, VGG19, resnet50
#경고메세지 무시
import warnings
warnings.filterwarnings(action='ignore')
LeNet-5¶
- 최초로 산업에 성공적으로 적용된 CNN모델이다
data load¶
In [2]:
import os
os.listdir('../input/digit-recognizer')
datapath = '../input/digit-recognizer'
In [3]:
train =pd.read_csv(datapath+'/train.csv')
print(train.shape)
train.head()
Out[3]:
In [4]:
test =pd.read_csv(datapath+'/test.csv')
print(test.shape)
test.head()
Out[4]:
In [5]:
train_labels = train['label']
train = (train.iloc[:,1:].values).astype('float32')
test = test.values.astype('float32')
In [6]:
#Visualizing the data
sample = train[10, :].reshape(28,28)
plt.imshow(sample, cmap='gray')
plt.show()
print('label : ', train_labels[10])
Preprocessing¶
- LeNet-1 모델은 28x28의 이미지를 사용했습니다.
- LeNet-5에서는 MNIST의 28x28 테스트 영상을 32x32 이미지의 중심에 배치하여 처리하였습니다.큰사이즈의 이미지 사용으로 인해 작은부분의 고려가 작은사이즈 보다 훨씬 더 고려되어 성능이 더욱 향상되었습니다.
In [7]:
train = train.reshape(42000, 28, 28, 1)
test= test.reshape(28000, 28, 28, 1)
# change shape using pad
train = np.pad(train, ((0,0),(2,2),(2,2),(0,0)), 'constant')
test = np.pad(test, ((0,0),(2,2),(2,2),(0,0)), 'constant')
print('train shape : ', train.shape)
print('test shape : ', test.shape)
In [8]:
# int64 -> float32 , scaling
train = train.astype('float32')/255
test = test.astype('float32')/255
X_train, X_val, y_train, y_val = train_test_split(train, train_labels, test_size=0.20, random_state=42)
#One-hot encoding the labels
print('X_train shape : ', X_train.shape)
print('X_val shape : ', X_val.shape)
print('y_train : ', y_train.shape)
print('y_val : ', y_val.shape)
y_train = to_categorical(y_train)
y_val = to_categorical(y_val)
print('y_train_to_categorical : ', y_train.shape)
print('y_val_to_categorical : ', y_val.shape)
Model¶
- [32x32x1] INPUT
- [28x28x6] CONV1: 6 5x5 filters at stride 1, pad 0
- [6x14x14] Average POOL1: 2x2 filters at stride 2
- [16x10x10] CONV2: 256 5x5 filters at stride 1, pad 0
- [16x5x5] Average POOL2: 2x2 filters at stride 2
- [120] FC6: 120 neurons
- [84] FC7: 84 neurons
- [10] FC8: 10 neurons (class scores)
- LeNet-5모델은 Non-overlapping pooling을 사용했다.
In [9]:
#lenet-5 model
model = Sequential()
#Conv layer 1
model.add(layers.Conv2D(filters=6, kernel_size=(5, 5),strides=1, activation='relu', input_shape=(32,32,1)))
#Pooling layer 1
model.add(AveragePooling2D(pool_size = 2, strides = 2))
#Conv Layer2
model.add(layers.Conv2D(filters=16, kernel_size=(5, 5),strides=1, activation='relu'))
#Pooling layer 2
model.add(AveragePooling2D(pool_size = 2, strides = 2))
model.add(layers.Flatten())
#FC Layer 3
model.add(layers.Dense(120, activation='relu'))
#FC Layer 4
model.add(layers.Dense(84, activation='relu'))
#FC Layer 5
model.add(layers.Dense(10, activation = 'softmax'))
# compile
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.summary()
Train and predict¶
In [10]:
datagen = ImageDataGenerator(
rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1)
In [11]:
patient = 4
callbacks_list = [
ReduceLROnPlateau(
monitor = 'val_loss',
#학습률을 절반으로 줄입니다.
factor = 0.5,
#patience 만큼 val_loss가 감소하지 않으면 학습률을 줄입니다.
patience = patient / 2,
#min Reduces learning
min_lr=0.00001,
verbose=1,
mode='min'
)]
In [12]:
%%time
epochs =30
batch_size = 64
history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data = (X_val,y_val),
steps_per_epoch=X_train.shape[0] // batch_size
,callbacks=callbacks_list,verbose = 1)
In [13]:
#predict
submission =pd.read_csv(datapath+'/sample_submission.csv')
pred = model.predict(test)
pred = np.argmax(pred,axis = 1)
submission['Label'] = pred
submission.to_csv('submission.csv',index=False)
submission.head()
Out[13]:
Acc/Loss plot¶
In [14]:
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
epochs = range(len(acc))
plt.plot(epochs, acc, label='Training acc')
plt.plot(epochs, val_acc, label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.ylim(0.9,1)
plt.show()
In [15]:
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.plot(epochs, loss, label='Training loss')
plt.plot(epochs, val_loss, label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.ylim(0,0.5)
plt.show()
conclusion¶
- 간단한 Renet-5 모델로 정확도 99%이상을 달성했습니다.
- 교차검증을 통한 앙상블로 정확도를 더 높일 수 있을 것 같습니다
In [1]:
from IPython.core.display import display, HTML
display(HTML("<style>.container {width:90% !important;}</style>"))
반응형
'데이터분석 > vision' 카테고리의 다른 글
VGGNet using keras (0) | 2019.11.07 |
---|---|
LRN(Local Response Normalization) 이란 무엇인가?(feat. AlexNet) (0) | 2019.11.07 |
AlexNet using keras (0) | 2019.11.06 |
Lecture 9: CNN Architectures (0) | 2019.10.27 |
Lecture 7: Training Neural Networks, part I (0) | 2019.10.17 |