Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<find_best_params>
def set_seeds(offset):
np.random.seed(100+offset)
 return(100+offset)
 tempseed = set_seeds(0)
 image_width = 28
image_height = 28
batch_size = 256
no_epochs = 40
no_classes = 10
ensemble_size = 1
validation_split = 0.2
verbosity = 1
base_filters = 256
for fold in range(1,n_folds+1):
print(f"Running on fold {fold}")
 model=create_model()
model.load_state_dict(torch.load(f"fold{fold}Best.pt"))
model.to(device)
 model.eval()
for i,image in enumerate(tqdm.tqdm(TestSet)) :
image=torch.unsqueeze(image,0 ).to(device)
 outputs=model(image)
 model_outputs[i]+=outputs.detach() [0]<load_from_csv>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 finaltest = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train['label'].value_counts(normalize=False )
submission_df=pd.read_csv(".. /input/digit-recognizer/sample_submission.csv" )<feature_engineering>
datagen = ImageDataGenerator(
 rotation_range=35,
width_shift_range=0.3,
height_shift_range=0.2,
shear_range=0.3,
zoom_range=0.2,
validation_split=validation_split,
horizontal_flip=False, 
)
submission_df["Label"]=np.argmax(model_outputs.cpu().numpy() ,1 )<save_to_csv>
trainlabels = train['label']
trainimages = train.drop(['label'], axis = 1)
 trainimages = trainimages.values.reshape(-1, image_width, image_height, 1)
 trainimages = trainimages.astype('float32')
 trainimages = trainimages/255
testset = finaltest.values.reshape(-1, image_width, image_height, 1)
 testset = testset.astype('float32')
 testset = testset/255
submission_df.to_csv("submission.csv", index=False )<load_from_csv>
x_train, x_test, y_train, y_test = train_test_split(trainimages, trainlabels, test_size = validation_split )
pd.read_csv("submission.csv" )<import_modules>
train_generator = datagen.flow(x_train, y_train, batch_size=batch_size, shuffle=True, subset='training')
 val_generator = datagen.flow(x_test, y_test, batch_size=batch_size, subset='validation')
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
models = list()
for i in range(ensemble_size):
model = Sequential()
model.add(Conv2D(filters =(int(base_filters/8)) , kernel_size =(7,7), padding = 'Same', activation = 'relu', input_shape =(image_width, image_height, 1)))
 model.add(Conv2D(filters =(base_filters/8), kernel_size =(7,7), padding = 'Same', activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size =(2,2), strides =(2,2)))
 model.add(Dropout(0.25, seed=tempseed))
model.add(Conv2D(filters =(base_filters/4), kernel_size =(5,5), padding = 'Same', activation = 'relu'))
model.add(Conv2D(filters =(base_filters/4), kernel_size =(5,5), padding = 'Same', activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size =(2,2), strides =(2,2)))
 model.add(Dropout(0.25, seed=tempseed))
model.add(Conv2D(filters =(base_filters/2), kernel_size =(3,3), padding = 'Same', activation = 'relu'))
model.add(Conv2D(filters =(base_filters/2), kernel_size =(3,3), padding = 'Same', activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size =(2,2), strides =(2,2)))
 model.add(Dropout(0.25, seed=tempseed))
model.add(Conv2D(filters =(base_filters), kernel_size =(3,3), padding = 'Same', activation = 'relu'))
model.add(Conv2D(filters =(base_filters), kernel_size =(3,3), padding = 'Same', activation = 'relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size =(2,2), strides =(2,2)))
 model.add(Dropout(0.25, seed=tempseed))
model.add(Flatten())
 model.add(Dense(base_filters, activation = "relu"))
model.add(Dense(base_filters/2, activation = "relu"))
model.add(Dense(no_classes, activation = "softmax"))
models.insert(i, model)
 models[0].summary()
np.random.seed(1)
 df_train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 df_train = df_train.iloc[np.random.permutation(len(df_train)) ]<prepare_x_and_y>
optimizer = Adam(lr=0.0005, decay=1e-9 )
sample_size = df_train.shape[0]
validation_size = int(df_train.shape[0] * 0.1)
 train_x = np.asarray(df_train.iloc[:sample_size - validation_size:, 1:] ).reshape([sample_size - validation_size, 28, 28, 1])
 train_y = np.asarray(df_train.iloc[:sample_size - validation_size:, 0] ).reshape([sample_size - validation_size, 1])
 val_x = np.asarray(df_train.iloc[sample_size - validation_size:,1:] ).reshape([validation_size,28,28,1])
 val_y = np.asarray(df_train.iloc[sample_size - validation_size:, 0] ).reshape([validation_size, 1] )<load_from_csv>
for i in range(len(models)) :
models[i].compile(optimizer = optimizer, loss = "sparse_categorical_crossentropy", metrics = ["accuracy"] )
df_test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 test_x = np.asarray(df_test.iloc[:, :] ).reshape([-1, 28, 28, 1] )<feature_engineering>
learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_accuracy', patience = 3, verbose = 1, factor = 0.25, min_lr = 0.0000001 )
train_x = train_x/255
val_x = val_x/255
test_x = test_x/255<choose_model_class>
callbacks = [learning_rate_reduction]
model = models.Sequential()<choose_model_class>
histories = list()
for i in range(len(models)) :
tempseed = set_seeds(i)
 histories.insert(i, models[i].fit_generator(
 train_generator,
epochs = no_epochs,
verbose = 1,
validation_data = val_generator,
callbacks = [callbacks] 
))
<choose_model_class><EOS> 
predictions = np.zeros(( testset.shape[0],10))
for i in range(ensemble_size):
predictions = predictions + models[i].predict(testset)
 finalpredictions = np.argmax(predictions, axis = 1)
 submissions = {'ImageID':list(range(1, len(finalpredictions)+ 1)) ,'Label': finalpredictions}
submission_df = pd.DataFrame(submissions ).astype('int')
 submission_df.head()
submission_df.to_csv('submission.csv', index=False )