Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<drop_column>
%matplotlib inline
np.random.seed(2)
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
sns.set(style='white', context='notebook', palette='deep' )
del x_valid, y_valid,preds_valid,y_valid_ori,train_df
gc.collect()<load_from_csv>
dig_train_data = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 dig_test_data = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 Y_train= dig_train_data.label
X_train= dig_train_data.drop("label", axis=1)
 del dig_train_data
train_df = pd.read_csv(".. /input/tgs-salt-identification-challenge/train.csv", index_col="id", usecols=[0])
 depths_df = pd.read_csv(".. /input/tgs-salt-identification-challenge/depths.csv", index_col="id")
 train_df = train_df.join(depths_df)
 test_df = depths_df[~depths_df.index.isin(train_df.index)]<predict_on_test>
X_train = X_train/255.0
dig_test_data= dig_test_data/255.0
X_train = X_train.values.reshape(-1,28,28,1)
 dig_test_data = dig_test_data.values.reshape(-1,28,28,1)
 Y_train = to_categorical(Y_train,num_classes = 10 )
batch_size = 500
preds_test = []
i = 0
while i < test_df.shape[0]:
index_val = test_df.index[i:i+batch_size]
x_test = np.array([upsample(np.array(load_img("./test/images/{}.png".format(idx), grayscale=True)))/ 255 for idx in(index_val)] ).reshape(-1, img_size_target, img_size_target, 1)
 x_test = np.repeat(x_test,3,axis=3)
 preds_test_temp = predict_result(model,x_test,img_size_target,32)
 if i==0:
preds_test = preds_test_temp
else:
preds_test = np.concatenate([preds_test,preds_test_temp],axis=0)
 if i%2000==0:
print('Images Processed:',i)
 i += batch_size
print('Done!' )<categorify>
random_seed= 2
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size= 0.1, random_state = random_seed )
def rle_encode(im):

 pixels = im.flatten(order = 'F')
 pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs )<compute_test_metric>
model= Sequential()
model.add(Conv2D(filters=32, kernel_size =3, activation='relu',input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters=32, kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 32,kernel_size =5,strides=2, padding = 'Same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(filters=64, kernel_size =3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters=64, kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64,kernel_size =5,strides=2, padding = 'Same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(256, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(10,activation="softmax"))
t1 = time.time()
pred_dict = {idx: rle_encode(np.round(downsample(preds_test[i])> threshold_best)) for i, idx in enumerate(tqdm_notebook(test_df.index.values)) }
t2 = time.time()
print(f"Usedtime = {t2-t1} s" )<save_to_csv>
model.compile(optimizer = 'adam', loss="categorical_crossentropy", metrics= ["accuracy"] )
sub = pd.DataFrame.from_dict(pred_dict,orient='index')
 sub.index.names = ['id']
sub.columns = ['rle_mask']
sub.to_csv('swa_xce_submission.csv' )<import_modules>
learning_rate_reduction = ReduceLROnPlateau(moitor='val_acc',
patience=3 ,
verbose=1,
factor =0.5,
min_lr=0.00001 )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import keras
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers, models, utils, callbacks
import tensorflow as tf
import os<load_from_csv>
epochs= 40
batch_size = 128
nbr_of_clases = 10
validation_percentage = 0.2
resnet_path = '.. /input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
training_data = pd.read_csv('.. /input/digit-recognizer/train.csv' )<categorify>
datagen = 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_train )
def prepare_data(data_to_transform):
data = data_to_transform.copy()
data = data.reshape(-1, 28, 28, 1)/ 255
return data<split>
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 1, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )
y = training_data['label'].values
X = training_data.drop('label',axis = 1 ).values
y = keras.utils.to_categorical(y, nbr_of_clases)
 X_rgb = prepare_data(X)
 X_train, X_val, y_train, y_val = train_test_split(X_rgb, y, test_size=validation_percentage )<choose_model_class>
results = model.predict(dig_test_data)
 results = np.argmax(results, axis=1)
 results = pd.Series(results, name="label" )
model = models.Sequential([
layers.Conv2D(32, 3, activation='relu', input_shape=(128,128,1), padding='same'),
layers.Conv2D(32, 3, activation='relu', padding='same'),
layers.MaxPooling2D(padding='same'),
layers.Dropout(0.5),
layers.Conv2D(64, 3, activation='relu', padding='same'),
layers.Conv2D(64, 3, activation='relu', padding='same'),
layers.MaxPooling2D(padding='same'),
layers.Dropout(0.25),
layers.GlobalAveragePooling2D() ,
layers.Dense(nbr_of_clases, activation='softmax'),
])
 model.summary()
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'], )<init_hyperparams>
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )