Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<define_variables>
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )
files_path = '.. /input/cassava-leaf-disease-classification/test_images'
TEST_FILENAMES = tf.io.gfile.glob('.. /input/cassava-leaf-disease-classification/test_tfrecords/*')
 model_path_list = [
'.. /input/cassava-leaf-vit-models/ViTB16_best_fold_0__v3_.h5',
'.. /input/cassava-leaf-vit-models/ViTB16_best_fold_0_v4_.h5',
'.. /input/cassava-leaf-vit-models/ViTB16_best_fold_1_v4_.h5',
'.. /input/cassava-leaf-vit-models/ViTB16_best_fold_2_v4_.h5',
'.. /input/cassava-leaf-vit-models/ViTB16_best_fold_3_v4_.h5'
]
test_preds = np.zeros(( len(os.listdir(files_path)) , N_CLASSES))
for idx, model_path in enumerate(model_path_list):
print("Model: ", model_path)
 tf.keras.backend.clear_session()
model = get_vit_model('imagenet21k')
 model.load_weights(model_path)
 if TTA:
for step in range(N_TTA):
print(f"TTA step: {step+1}/{N_TTA}")
 test_ds = get_dataset(TEST_FILENAMES, labeled=False, ordered=True, augment = True)
 x_test = test_ds.map(lambda image, image_name: image)
 test_preds += model.predict(x_test)
 else:
test_ds = get_dataset(TEST_FILENAMES, labeled=False, ordered=True)
 x_test = test_ds.map(lambda image, image_name: image)
 test_preds += model.predict(x_test )<save_to_csv>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )
test_preds = np.argmax(test_preds, axis=-1)
 image_names = [img_name.numpy().decode('utf-8')for img, img_name in iter(test_ds.unbatch())]
submission = pd.DataFrame({'image_id': image_names, 'label': test_preds})
 submission.to_csv('submission.csv', index=False)
 display(submission.head() )<train_model>
X_train = X_train / 255.0
test = test / 255.0
print(f'Training path:{__training_path}
Test path:{__test_path}' )<install_modules>
Y_train = to_categorical(Y_train, num_classes = 10 )
!{sys.executable} -m pip install --upgrade scikit-learn=="0.24.2"<import_modules>
random_seed = 2
import sklearn; sklearn.show_versions()<load_from_csv>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed )
def __load__data(__training_path, __test_path, concat=False):
	
	
	
	__train_dataset = pd.read_csv(__training_path, delimiter=',')
 	__test_dataset = pd.read_csv(__test_path, delimiter=',')
 	return __train_dataset, __test_dataset
__train_dataset, __test_dataset = __load__data(__training_path, __test_path, concat=True)
 __train_dataset.head()<define_variables>
nets = 5
model = [0] *nets
for j in range(nets):
model[j] = Sequential()
model[j].add(Conv2D(32, kernel_size = 3, padding='same', activation='relu', input_shape =(28, 28, 1)))
 model[j].add(BatchNormalization())
 model[j].add(Conv2D(32, kernel_size = 3, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(MaxPooling2D(pool_size=(2, 2)))
 model[j].add(Dropout(0.4))
model[j].add(Conv2D(64, kernel_size = 3, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(Conv2D(64, kernel_size = 5, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(MaxPooling2D(pool_size=(2, 2)))
 model[j].add(Dropout(0.25))
model[j].add(Conv2D(128, kernel_size = 4, padding='same', activation='relu'))
model[j].add(BatchNormalization())
 model[j].add(MaxPooling2D(pool_size=(2, 2)))
 model[j].add(Flatten())
 model[j].add(Dense(128, activation='relu'))
model[j].add(Dropout(0.7))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )
__test_dataset_submission_columns = __test_dataset['Id']<drop_column>
__train_dataset.drop(['Descript', 'Resolution'], axis=1, inplace=True )<feature_engineering>
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.3,
shear_range = 0.2,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )
_DATE_COLUMNS = ['Dates']
for _col in _DATE_COLUMNS:
train_date_col = pd.to_datetime(__train_dataset[_col], errors='coerce')
 __train_dataset["year"] = train_date_col.dt.year
__train_dataset["month"] = train_date_col.dt.month
__train_dataset["day"] = train_date_col.dt.day
__train_dataset.drop(_col, axis=1, inplace=True)
 test_date_col = pd.to_datetime(__test_dataset[_col], errors='coerce')
 __test_dataset["year"] = test_date_col.dt.year
__test_dataset["month"] = test_date_col.dt.month
__test_dataset["day"] = test_date_col.dt.day
__test_dataset.drop(_col, axis=1, inplace=True )<categorify>
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 ** x)
 history = [0] * nets
epochs = 25
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_generator(datagen.flow(X_train2,Y_train2, batch_size=32),
epochs = epochs, steps_per_epoch = X_train2.shape[0]//32, verbose = 2,
validation_data =(X_val2,Y_val2), callbacks=[annealer])
 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'])) )
__feature_train = __train_dataset.drop(['Category'], axis=1)
 __target_train =__train_dataset['Category']
__feature_test = __test_dataset.drop(['Id'], axis=1)
 _CATEGORICAL_COLS = ['Address', 'PdDistrict', 'DayOfWeek']
__col_indices = [__feature_train.columns.get_loc(col)for col in _CATEGORICAL_COLS]
_ohe = OneHotEncoder(handle_unknown='ignore')
 __ct = ColumnTransformer([("ohe", _ohe, __col_indices)], remainder = 'passthrough')
 __feature_train = __ct.fit_transform(__feature_train)
 __feature_test = __ct.transform(__feature_test )<train_on_grid>
results = np.zeros(( test.shape[0],10))
for j in range(nets):
results = results + model[j].predict(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("MNIST-CNN-ENSEMBLE.csv",index=False )