Kaggle competition: Digit Recognizer
<feature_engineering>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
<import_modules>
print('Working on "%s"' % Path('.' ).absolute() )
import pandas as pd
import numpy as np<import_modules>
path = Path('.. /input' )
import pandas as pd
import numpy as np<define_variables>
train = pd.read_csv(path/'train.csv', header='infer' )
data_path_list = [
".. /input/indoor-gbm-postprocessing-xy-prediction/submission.csv",
".. /input/wifi-features-with-lightgbm-kfold/submission.csv",
".. /input/simple-99-accurate-floor-model/submission.csv"
]
position_weights = np.array([
0.9,
0.1,
0
])
 floor_weights = np.array([
0,
0,
1
])
 position_weights = position_weights / np.sum(position_weights)
 floor_weights = floor_weights / np.sum(floor_weights )<load_from_csv>
test = pd.read_csv(path/'test.csv', header='infer' )
df_list = [pd.read_csv(d)for d in data_path_list]<save_to_csv>
class CustomImageList(ImageList):
def open(self, fn):

 fn = fn.reshape(28,28)
 fn = np.stack(( fn,)*3, axis=-1)
 return Image(pil2tensor(fn,dtype=np.float32))
@classmethod
def from_df_custom(cls, df, path:PathOrStr, **kwargs)->'ItemList':
res = super().from_df(df, path=path, cols=0, **kwargs)
 if 'label' in df.columns:
df = df.drop('label', axis=1)
 df = np.array(df,dtype=np.float32)/255.
 df = df/255.
 mean = df.mean()
std = df.std()
res.items =(df-mean)/std
return res
sub = pd.read_csv(".. /input/indoor-location-navigation/sample_submission.csv")
 sub.floor = df_list[np.argmax(floor_weights)].floor
sub.x = 0
sub.y = 0
for df, w in zip(df_list, position_weights):
sub[["x", "y"]] += df[["x", "y"]] * w
sub.to_csv("submission.csv", index=False )<load_from_csv>
test_data = CustomImageList.from_df_custom(test,path=path)
 test_data
train_data = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_data = pd.read_csv('.. /input/digit-recognizer/test.csv' )<prepare_x_and_y>
tfms = get_transforms(do_flip=False)
 data =(CustomImageList.from_df_custom(train, path=path)
.split_by_rand_pct (.2, seed=2019)
.label_from_df(cols='label')
.add_test(test_data, label=0)
.transform(tfms)
.databunch(bs=128, num_workers=0)
.normalize(imagenet_stats))
data
X = train_data.drop(['label'], axis=1)
 y = train_data['label']
del train_data<categorify>
learner = cnn_learner(data, models.resnet50,metrics=accuracy,model_dir='/kaggle/working/models' )
X = X / 255.; test_data = test_data / 255.
 X = X.values.reshape(-1, 28, 28, 1)
 test_data = test_data.values.reshape(-1, 28, 28, 1 )<import_modules>
learner.lr_find()
import tensorflow as tf<choose_model_class>
learner.fit_one_cycle(7, 1e-2 )
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[28, 28, 1]),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
tf.keras.layers.Conv2D(filters=64, kernel_size=3, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPool2D(pool_size=2, strides=2),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(units=256, activation='relu'),
tf.keras.layers.Dropout (.15),
tf.keras.layers.Dense(units=128, activation='relu'),
tf.keras.layers.Dropout (.1),
tf.keras.layers.Dense(units=64, activation='relu'),
tf.keras.layers.Dense(units=10, activation='softmax')
 ])
 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
 learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(
 monitor='acc',
patience=5,
factor=0.5,
min_lr=0.00001
)<train_model>
learner.unfreeze()
y = tf.keras.utils.to_categorical(y, num_classes=10)
 datagen = tf.keras.preprocessing.image.ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X)
 Xtrain, Xvalid, ytrain, yvalid = train_test_split(X, y, test_size=.1, random_state=333 )<train_model>
learner.fit_one_cycle(8, slice(1e-5,1e-4))
history = model.fit(datagen.flow(Xtrain, ytrain, batch_size=100), validation_data=(Xvalid, yvalid), epochs = 100, callbacks=[learning_rate_reduction] )<predict_on_test>
learner.save('stage1' )
ypred = model.predict(test_data)
 for i in range(ypred.shape[0]):
pred = ypred[i, :]
ypred[i, 0] = list(pred ).index(pred.max() )<save_to_csv>
learner.unfreeze()
learner.fit_one_cycle(8, slice(1e-5,1e-4))
submission = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 submission.iloc[:, 1] = ypred[:, 0].astype(np.int)
 submission.to_csv('submission.csv', index=False )<install_modules>
learner.save('MnistStage2' )
!pip install efficientnet tensorflow_addons > /dev/null<set_options>
pred, y, losses = learner.get_preds(ds_type=DatasetType.Test, with_loss=True )
%matplotlib inline 
<load_from_csv>
labels = torch.argmax(pred, dim=1 )
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )<feature_engineering>
submission_df = pd.DataFrame({'ImageId': range(1, len(y)+ 1), 'Label': labels}, columns=['ImageId', 'Label'])
 submission_df.head()
<train_model><EOS> 
submission_df.to_csv('MnistSubmission.csv', index=False )