Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<define_variables>
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Activation, Dropout, Flatten, Dense, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from keras.optimizers import Adam
val_preds = val_preds > 0.5<data_type_conversions>
df = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 print(df.shape)
 print(test.shape)
 print(df.head() )
full_preds = val_preds_all[0].numpy()<compute_test_metric>
labels = df["label"]
X = df.drop('label', axis = 1)
 print(labels.value_counts())
 print("Baseline Accuracy: " + str(round(labels.value_counts().max() /labels.value_counts().sum() ,3)) )
vis_arr = cm(val_targ, val_preds )<filter>
def normalizeANDreshape(df, minimum, maximum):
diff = maximum - minimum
zero_min = df - minimum
adjusted = zero_min/diff
shaped = adjusted.values.reshape(-1,28,28,1)
 return shaped
print(np.max(normalizeANDreshape(X, 0, 255)))
 print(np.min(normalizeANDreshape(X, 0, 255)))
 print(type(normalizeANDreshape(X, 0, 255)))
 print(normalizeANDreshape(X, 0, 255 ).shape )
val = dfs[dfs.is_valid==True]
len(val[val['16'] == 1] )<compute_test_metric>
y = pd.get_dummies(labels)
 y.head()
average_precision = average_precision_score(val_targ, val_preds)
 average_precision<find_best_params>
X_train, X_val, y_train, y_val = train_test_split(normalizeANDreshape(X, 0, 255), y, test_size = 0.20 )
precision = dict()
recall = dict()
average_precision = dict()
for i in range(19):
precision[i], recall[i], _ = precision_recall_curve(val_targ[:, i], val_preds[:, i])
 average_precision[i] = average_precision_score(val_targ[:, i], val_preds[:, i])
 precision["micro"], recall["micro"], _ = precision_recall_curve(val_targ.ravel() , val_preds.ravel())
 average_precision["micro"] = average_precision_score(val_targ, val_preds, average="micro")
 print('Average precision score, micro-averaged over all classes: {0:0.2f}'.format(average_precision["micro"]))<define_variables>
augment = ImageDataGenerator(rotation_range = 15,
width_shift_range = 0.35,
height_shift_range = 0.35,
zoom_range = 0.2, 
)
 augment.fit(X_train )
path = Path('.. /input/hpa-cell-tiles-test-with-enc-dataset' )<load_from_csv>
def makeCNN(shape):
model = Sequential()
model.add(Conv2D(filters = 64, kernel_size =(4,4), padding = 'same', activation = 'relu', input_shape = shape))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'same', activation = 'relu'))
model.add(Conv2D(filters = 128, kernel_size =(2,2), padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Flatten())
 model.add(Dense(64, activation = 'relu'))
model.add(Dropout(0.25))
model.add(Dense(10, activation = 'softmax'))
return model
model = makeCNN(( 28, 28, 1))
model.summary()
df = pd.read_csv(path/'cell_df.csv' )<save_to_csv>
model.compile(optimizer = Adam() ,
loss = 'categorical_crossentropy',
metrics = 'accuracy' )
df.to_csv('cell_df.csv', index=False )<train_model>
epochs = 50
batch_size = 64
history = model.fit_generator(augment.flow(X_train,y_train, batch_size=batch_size),
epochs = epochs,
steps_per_epoch=len(X_train)// batch_size,
validation_data =(X_val,y_val),
verbose = 1,
use_multiprocessing = True,
workers = 2
)
test_dl = learn.dls.test_dl(df )<define_variables>
validation_predictions = model.predict_classes(X_val)
 confusion = confusion_matrix(validation_predictions,y_val.idxmax(axis=1))
print(confusion )
test_dl.show_batch()<predict_on_test>
test = normalizeANDreshape(test, 0, 255)
 predictions = model.predict_classes(test)
 print(predictions[0:5] )
preds, _ = learn.get_preds(dl=test_dl )<load_pretrained>
Id = []
for i in range(len(test)) :
Id.append(i+1)
 output = pd.DataFrame({'ImageID': Id, 'Label': predictions})
 output.to_csv('predictions.csv', index=False )