🔥알림🔥
① 테디노트 유튜브 -
구경하러 가기!
② LangChain 한국어 튜토리얼
바로가기 👀
③ 랭체인 노트 무료 전자책(wikidocs)
바로가기 🙌
④ RAG 비법노트 LangChain 강의오픈
바로가기 🙌
⑤ 서울대 PyTorch 딥러닝 강의
바로가기 🙌
Kaggle Digit Recognizer Competition - achieve over 99% accuracy!
This is a very simple tutorial for tensorflow 2.0 beginners!
This tutorial is based on Digit Recognizer presented by Kaggle
You may be able to submit with the output generated by your model
Goal
- using tensorflow 2.0 frameworks
- using Sequential Models
- applying Image Augmentation by using ImageDataGenerator
- Make submission to Kaggle
Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline
Load Data
train = pd.read_csv('data/train.csv')
test = pd.read_csv('data/test.csv')
x_train = train.drop('label', 1)
y_train = train['label']
Explore class distributions
y_train.value_counts()
x_train.shape, y_train.shape
Reshaping Model to 28 * 28
change shape to (28, 28, 1)
28 X 28 picture size with graysclae channel of 1
x_train = x_train.values.reshape(-1, 28, 28, 1)
y_train = y_train.values.reshape(-1, 1)
y_train.shape
x_test refers to test datasets that I need to predict
x_test = test.values.reshape(-1, 28, 28, 1)
x_test.shape
Using ImageDataGenerator to augment Images
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
rescale=1.0/255,
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=10, # randomly rotate images in the range (degrees, 0 to 180)
zoom_range = 0.1, # Randomly zoom image
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=False, # randomly flip images
vertical_flip=False) # randomly flip images
from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.2)
x_train.shape, x_val.shape
Modeling
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout
from tensorflow.keras.callbacks import ModelCheckpoint
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(5, 5), padding='same', activation='relu', input_shape=(28, 28, 1)))
model.add(Conv2D(filters=32, kernel_size=(5, 5), padding='same', activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPool2D(pool_size=(2, 2), strides=2))
model.add(Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2), strides=2))
model.add(Dropout(rate=0.25))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(rate=0.25))
model.add(Dense(10, activation='softmax'))
model.summary()
batch_size = 128
epochs = 20
model_path = 'model/tmp-best-model.h5'
checkpoint = ModelCheckpoint(model_path,
verbose=1,
monitor='val_loss',
save_best_only=True,
save_weight_only=True,
mode='auto')
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
history = model.fit(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=[checkpoint],
)
Visualize
plt.figure(figsize=(12, 6))
plt.plot(np.arange(20)+1, history.history['loss'], label='Loss')
plt.plot(np.arange(20)+1, history.history['val_loss'], label='Validation Loss')
plt.title('losses over training', fontsize=20)
plt.xlabel('epochs', fontsize=15)
plt.ylabel('loss', fontsize=15)
plt.legend()
plt.show()
plt.figure(figsize=(12, 6))
plt.plot(np.arange(20)+1, history.history['accuracy'], label='Accuracy')
plt.plot(np.arange(20)+1, history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Accuracy over training', fontsize=20)
plt.xlabel('epochs', fontsize=15)
plt.ylabel('Accuracy', fontsize=15)
plt.legend()
plt.show()
Load best model
model.load_weights(model_path)
Make Prediction
result = model.predict(x_test)
result = np.argmax(result, axis=1)
result = pd.Series(result, name='label')
Make Submission
submission = pd.read_csv('data/sample_submission.csv')
submission['Label'] = result
submission['Label'].value_counts()
submission.to_csv('submission/final_submission.csv', index=False)
댓글남기기