Kaggle competition: Digit Recognizer
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )<train_model>
img_rows, img_cols = 28, 28
X = pd.read_csv(".. /input/digit-recognizer/train.csv")
 y = X['label']
X = X.drop(['label'],axis = 1)
 X = X.values.reshape(-1, 28, 28, 1 ).astype('float32')/255
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42,stratify=y)
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 sample = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 print(sample.shape )
x = train_df.iloc[:,1:].values
y = train_df.iloc[:,0].values
z = test_df.iloc[::].values
print("X shape : {}".format(x.shape))
print("Y shape : {}".format(y.shape))
print("Z shape : {}".format(z.shape))<categorify>
X_unseen = test.values.reshape(-1, 28, 28, 1 ).astype('float32')/255
class Dataset(Dataset):
def __init__(self, data):
self.data = data
self.n_samples = data.shape[0]
self.x_data = torch.tensor(data.iloc[::].values, dtype=torch.long)
 def __getitem__(self, index):
return self.x_data[index].reshape(28,28)
 def __len__(self):
return self.n_samples
def get_long_type(self, target):
target = target.type(torch.LongTensor)
 return target<create_dataframe>
IMAGE_HT_WID = 28
input_size = 784
hidden_size = 100
num_classes = 10
num_epochs = 2
batch_size = 100
learning_rate = 0.001
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transforms.Compose(
 [transforms.ToTensor() , transforms.Normalize(( 0.1307,),(0.3081,)) ]), download=True)
 test_dataset = Dataset(test_df)
 train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
 test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
<choose_model_class>
import tensorflow as tf
from tensorflow import keras
class CNNModel(nn.Module):
def __init__(self):
super(CNNModel, self ).__init__()
self.cnn1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=5, stride=1, padding=0)
 self.relu1 = nn.ReLU()
self.maxpool1 = nn.MaxPool2d(kernel_size=2)
 self.cnn2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=5, stride=1, padding=0)
 self.relu2 = nn.ReLU()
self.maxpool2 = nn.MaxPool2d(kernel_size=2)
 self.fc1 = nn.Linear(32 * 4 * 4, 10)
 def forward(self, x):
out = self.cnn1(x)
 out = self.relu1(out)
 out = self.maxpool1(out)
 out = self.cnn2(out)
 out = self.relu2(out)
 out = self.maxpool2(out)
 out = out.view(out.size(0), -1)
 out = self.fc1(out)
 return out
n_total_steps = len(train_loader)
 batch_size = 100
num_epochs = 10
model = CNNModel()
if torch.cuda.is_available() :
model.cuda()
criterion = nn.CrossEntropyLoss()
learning_rate = 0.1
optimizer = torch.optim.SGD(model.parameters() , lr=learning_rate )<find_best_params>
print(tf.__version__)
 IMG_SHAPE =(IMAGE_HT_WID, IMAGE_HT_WID, 1)
 model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32,(3, 3), activation=tf.nn.tanh, input_shape=IMG_SHAPE,padding='same'),
tf.keras.layers.GaussianNoise(0.1),
tf.keras.layers.Conv2D(32,(3, 3), activation=tf.nn.tanh),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout (.25),
tf.keras.layers.Conv2D(64,(3, 3), activation=tf.nn.tanh,padding='same'),
tf.keras.layers.Conv2D(64,(3, 3), activation=tf.nn.tanh),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout (.25),
tf.keras.layers.Conv2D(128,(3, 3), activation=tf.nn.tanh,padding='same'),
tf.keras.layers.Conv2D(128,(3, 3), activation=tf.nn.tanh),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout (.25),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout (.5),
tf.keras.layers.Dense(10, activation='softmax')
 ])
for epoch in range(num_epochs):
for i,(images, labels)in enumerate(train_loader):
images = images.to(device)
 train = Variable(images.view(100,1,28,28)).to(device)
 labels = labels.to(device)
 outputs = model(train)
 loss = criterion(outputs, labels)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
if(i + 1)% 100 == 0:
print(f'Epoch [{epoch + 1}/{num_epochs}], Step [{i + 1}/{n_total_steps}], Loss: {loss.item() :.4f}' )<predict_on_test>
sgd = tf.keras.optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
 model.compile(loss='sparse_categorical_crossentropy', optimizer=sgd, metrics=['sparse_categorical_accuracy'])
 model.summary()
def evalueate_model(model, test_loader):
predictions = []
model.eval()
for images in test_loader:
images = images.to(device)
 train = Variable(images.view(100,1,28,28 ).type(torch.float32)).to(device)
 with torch.no_grad() :
predicts = model(train)
 predicts = predicts.argmax(axis=1)
 predicts = predicts.cpu().numpy()
for pred in predicts:
predictions.append(pred)
 return(predictions )<prepare_output>
EPOCHS=32
pred = evalueate_model(model, test_loader )<load_from_csv>
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1,
patience=2, verbose=2, mode='auto',
min_lr=1e-6 )
submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 submission['Label'] = pred
submission.head(25 )<save_to_csv>
early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss',
patience=4, verbose=2, mode='auto',
baseline=None, restore_best_weights=True )
submission.to_csv('submission.csv', index=False )<save_to_csv>
history = model.fit(X, y, batch_size=50, epochs=EPOCHS, verbose = 2, callbacks = [reduce_lr,early_stop], validation_split=0.25,
shuffle=True )
submission.to_csv('submission.csv', index=False )<import_modules>
history.history
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from keras.utils.np_utils import to_categorical
from sklearn.model_selection import train_test_split<load_from_csv>
pred_X = model.predict(X, verbose = 2 )
train_df = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_df = pd.read_csv('.. /input/digit-recognizer/test.csv')
 train_df.head()
g = sns.countplot(train_df['label'])
 train_df['label'].value_counts()<prepare_x_and_y>
ohe = OneHotEncoder()
X_train = train_df.drop(['label'],1)
 Y_train = train_df['label']
X_train = X_train/255.0
test = test_df/255.0
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1)
 Y_train = to_categorical(Y_train,num_classes=10 )<split>
ohe_encoded_y = ohe.fit_transform(y.values.reshape(-1, 1))
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2 )<import_modules>
ohe_encoded_y[0].toarray()
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import BatchNormalization, Conv2D, Dense, Dropout, Flatten,MaxPooling2D
from keras.callbacks import ReduceLROnPlateau
from keras.applications import ResNet50<train_model>
fpr = dict()
tpr = dict()
roc_auc = dict()
fpr["micro"], tpr["micro"], thresholds = roc_curve(ohe_encoded_y.toarray().ravel() , pred_X.ravel())
 roc_auc["micro"] = auc(fpr["micro"], tpr["micro"] )
datagen = ImageDataGenerator(
 rotation_range= 10,
zoom_range= 0.2,
width_shift_range=0.1,
height_shift_range = 0.1,
horizontal_flip = False,
vertical_flip = False 
)
 datagen.fit(X_train )<choose_model_class>
np.argmax(pred_X,axis=1 )
model = Sequential([
Conv2D(64,(5,5),padding='same',input_shape=(28,28,1),activation='relu'),
Conv2D(64,(5,5),padding='same',activation='relu'),
MaxPooling2D(2,2),
Dropout(0.2),
Conv2D(64,(3,3),padding='same',activation='relu'),
Conv2D(64,(3,3),padding='same',activation='relu'),
MaxPooling2D(2,2),
Dropout(0.5),
Flatten() ,
Dense(128,activation='relu'),
Dense(128,activation='relu'),
Dropout(0.2),
Dense(10,activation='softmax')
 ])
 model.summary()<choose_model_class>
precision_recall_fscore_support(y, np.argmax(pred_X,axis=1), average='micro' )
rms = RMSprop(lr=0.001,rho=0.9,epsilon=1e-08,decay=0.0)
 adam = Adam(lr=0.001 )<choose_model_class>
precision_recall_fscore_support(y, np.argmax(pred_X,axis=1), average='weighted' )
model.compile(optimizer=adam,loss='categorical_crossentropy',metrics='acc' )<choose_model_class>
precision_recall_fscore_support(y, np.argmax(pred_X,axis=1), average=None )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<train_model>
precision_recall_fscore_support(y, np.argmax(pred_X,axis=1), average=None, beta=2 )
history = model.fit(
 datagen.flow(X_train,Y_train, batch_size=32),
epochs = 30,
validation_data =(X_val,Y_val),
verbose = 1,
steps_per_epoch=X_train.shape[0] // 32,
callbacks=[learning_rate_reduction]
)<predict_on_test>
precision_recall_fscore_support(y, np.argmax(pred_X,axis=1), average=None, beta=0.5 )
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
print(classification_report(y, np.argmax(pred_X,axis=1)) )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("new3.csv",index=False )<set_options>
pred = model.predict(X_unseen, verbose = 2 )
%matplotlib inline 
<load_from_csv>
results=pd.DataFrame({"ImageId":sample['ImageId'],
"Label":np.argmax(pred,axis=1)} )
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 subs = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv' )<categorify>
results['Label'].value_counts()
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 X_train = X_train / 255.0
X_test = test / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
 X_test = X_test.values.reshape(-1,28,28,1)
 Y_train = to_categorical(Y_train, num_classes = 10 )<choose_model_class>
results.to_csv("submission.csv",index=False )
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1 )<choose_model_class>
train_images = np.load('.. /input/kuzushiji/kmnist-train-imgs.npz')['arr_0']
test_images = np.load('.. /input/kuzushiji/kmnist-test-imgs.npz')['arr_0']
train_labels = np.load('.. /input/kuzushiji/kmnist-train-labels.npz')['arr_0']
test_labels = np.load('.. /input/kuzushiji/kmnist-test-labels.npz')['arr_0']
train_images = train_images / 255.0
test_images = test_images / 255.0
x_train = np.expand_dims(train_images, axis=-1)
 x_test = np.expand_dims(test_images, axis=-1)
 y_train = train_labels
y_test = test_labels
nets = 17
model = [0] *nets
for j in range(nets):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size = 3, activation='relu', input_shape =(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 3, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(128, kernel_size = 4, activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Flatten())
 model[j].add(Dropout(0.4))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )<choose_model_class>
hist = model.fit(x_train, y_train,
batch_size=50,
epochs=EPOCHS,
verbose=1,
validation_data=(x_test, y_test))
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x )<train_model>
score = model.evaluate(x_test, y_test, verbose=0)
history = [0] * nets
epochs = 55
for j in range(nets):
X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.1)
 history[j] = model[j].fit(datagen.flow(X_train2,Y_train2, batch_size=64),
epochs = epochs, steps_per_epoch = X_train2.shape[0]//64,
validation_data =(X_val2,Y_val2), callbacks=[annealer], verbose=0)
 print("CNN {0:d}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 j+1,epochs,max(history[j].history['accuracy']),max(history[j].history['val_accuracy'])) )<save_to_csv>
kpred_x_test = model.predict(x_test, verbose = 2 )
results = np.zeros(( X_test.shape[0],10))
for j in range(nets):
results = results + model[j].predict(X_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("Submission_Data_Aug.csv",index=False )<define_variables>
print(classification_report(y_test, np.argmax(kpred_x_test,axis=1)) )