Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<load_pretrained>
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from keras.utils.np_utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers import Conv2D
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Activation
from keras.layers.normalization import BatchNormalization
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import ModelCheckpoint
model.save_weights("weights.h5" )<train_model>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 y_train = train['label']
X_train = train.drop(labels = ["label"], axis = 1)
 print(X_train.shape)
 print(test.shape)
 train.head()
print(model )<load_pretrained>
print(np.where(pd.isnull(X_train)))
 print(np.where(pd.isnull(y_train)))
 print(np.where(pd.isnull(test)))
 counts = y_train.value_counts()
print(counts )
model.load_weights("weights.h5" )<set_options>
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )
<load_pretrained>
X_train.astype('float32')
 test.astype('float32')
 X_train = X_train / 255.0
test = test / 255.0
y_train = to_categorical(y_train)
 print(y_train.shape )
<train_model>
random_seed = 42
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train,
test_size = 0.10,
random_state = random_seed )
checkpoint = ModelCheckpoint(OUTPUT_PATH, monitor='history = model.fit() ', verbose=1, save_best_only=True, mode='max')
 print(checkpoint )<save_to_csv>
def baseline_cnn(num_classes=10):
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5), padding = 'same',input_shape =(28,28,1)))
 model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.20))
model.add(Flatten())
 model.add(Dense(256))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
 return model
sub1.rename(columns={'toxic':'toxic1'}, inplace=True)
 sub2.rename(columns={'toxic':'toxic2'}, inplace=True)
 sub3 = pd.merge(sub1, sub2, how='left', on='id')
 sub3['toxic'] =(sub3['toxic1'] * 0.1)+(sub3['toxic2'] * 0.9)
 sub3['toxic'] =(sub3['toxic2'] * 0.39)+(sub3['toxic'] * 0.61)
 sub3[['id', 'toxic']].to_csv('submission.csv', index=False )<import_modules>
def cnn(num_classes=10):
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5), padding = 'same',input_shape =(28,28,1)))
 model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(5,5), padding = 'same'))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.20))
model.add(Conv2D(filters = 64, kernel_size =(2,2), padding = 'same'))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(2,2), padding = 'same'))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2,2)))
 model.add(Dropout(0.20))
model.add(Flatten())
 model.add(Dense(256))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
 return model
import pandas as pd<import_modules>
learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_acc',
patience = 5,
verbose = 1,
factor = 0.8,
min_lr = 0.00001 )
import pandas as pd<load_from_csv>
datagen = ImageDataGenerator(rotation_range = 20,
zoom_range = 0.2,
width_shift_range = 0.1,
height_shift_range = 0.1,
fill_mode = 'constant',
cval = 0.0)
 datagen.fit(X_train)
 for X_batch, y_batch in datagen.flow(X_train, y_train, batch_size = 16):
for i in range(16):
plt.subplot(4, 4, i+1)
 plt.imshow(X_batch[i,:,:,0], cmap = plt.get_cmap('gray'))
plt.axis('off')
 plt.show()
break
df = pd.read_csv(".. /input/submission4/submission2.csv" )<save_to_csv>
batch_size = 86
epochs = 100
df.to_csv('submission.csv', index=False )<import_modules>
model = cnn(y_train.shape[1])
 history = model.fit_generator(datagen.flow(X_train, y_train, batch_size = batch_size),
validation_data=(X_val, y_val),
epochs = epochs,
steps_per_epoch = X_train.shape[0] // batch_size,
callbacks = [learning_rate_reduction],
verbose=2 )
import math
import sys
import random
import os
import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.pyplot as mpplot
import torch
from torch.optim import lr_scheduler
from torch.utils.data import Dataset, DataLoader
import torchvision
from PIL import Image 
<prepare_x_and_y>
prediction = model.predict(test)
 prediction = np.argmax(prediction, axis =1)
 dataframe = pd.read_csv(".. /input/sample_submission.csv")
 list_of_images = dataframe.ImageId.values
name = 'submission'
submission = pd.DataFrame({'ImageId':list_of_images})
 submission['Label'] = prediction
submission.to_csv(f'{name}.csv',index=False )