{"cells":[{"metadata":{},"cell_type":"markdown","source":"I wanted to try Transfer Learning using the [model](https://github.com/kkweon/mnist-competition/tree/master/model) pretrained on original MNIST Dataset, I got more than 98% accuracy in 5-epochs."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"from keras.models import load_model\n\nimport matplotlib.pyplot as plt\nfrom keras.layers import Conv2D, Dropout, Dense, Flatten, BatchNormalization, MaxPooling2D, Activation\nfrom keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\nfrom keras.utils.np_utils import to_categorical\n\nfrom sklearn.model_selection import train_test_split\n\nimport keras.backend as K","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Dig_MNIST = pd.read_csv(\"../input/Kannada-MNIST/Dig-MNIST.csv\")\nsample_submission = pd.read_csv(\"../input/Kannada-MNIST/sample_submission.csv\")\ntest = pd.read_csv(\"../input/Kannada-MNIST/test.csv\")\ntrain = pd.read_csv(\"../input/Kannada-MNIST/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Pre-trained model** on original MNIST https://github.com/kkweon/mnist-competition/tree/master/model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = load_model('/kaggle/input/tmodelvgg5/vggnet5.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Split the data\nx = train.iloc[:,1:].values\ny = train.iloc[:,0].values\ny[:10]\nx_test = test.drop('id', axis=1).iloc[:,:].values\nx_dig = Dig_MNIST.drop('label', axis=1).iloc[:,:].values\ny_dig = Dig_MNIST.label\n\n# Reshape the data\nx = x.reshape(x.shape[0], 28, 28, 1)\ny = to_categorical(y, 10)\nx_test = x_test.reshape(x_test.shape[0], 28, 28, 1)\nx_dig = x_dig.reshape(x_dig.shape[0], 28, 28, 1)\n\n# Split the data between train and test\nx_train, x_valid, y_train, y_valid = train_test_split(x, y, test_size = 0.10, random_state=42) \n\n# Artificially increase training set\ntrain_datagen = ImageDataGenerator(rescale=1./255.,\n                                   rotation_range=10,\n                                   width_shift_range=0.25,\n                                   height_shift_range=0.25,\n                                   shear_range=0.1,\n                                   zoom_range=0.25,\n                                   horizontal_flip=False)\n\nvalid_datagen = ImageDataGenerator(rescale=1./255.,\n                                  rotation_range=10,\n                                   width_shift_range=0.25,\n                                   height_shift_range=0.25,\n                                   shear_range=0.1,\n                                   zoom_range=0.25,\n                                   horizontal_flip=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"I have tried with lesser Learning Rates, what I observed was increasing the lr imporved accuracy with less epochs. So I have used lr=1e-2"},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size =1024  # 64\nnum_classes = 10\nepochs = 10 # 50\nlearning_rate=1e-2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"optimizer = Adam(lr=learning_rate)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer=optimizer,\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.optimizer.lr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss', \n                                            patience=200,\n                                            verbose=1,\n                                            factor=0.2)\n\nes = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=200)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* Before I tried things like removing the last layer and adding a new layer and freezing the entire network except the last layer and then training I tried it for 20-50 epochs but it didn't work (val_loss and val_accuracy were bad).\n* Freeze the entire network except the last layer (without removing it) then trained it.\n* Train only the layers from Flatten till the last layer\n* Remove the layers from Flatten and add new layers and train\n* Remove up to last conv layers and add new layers and then train.\n* None of them worked, then I just trained the model without any change with lr=1e-3, it worked, then I tried increasing and decreasing the lr. I observed increasing lr trained faster with good accuracy, So I choose lr=1e-2 (for lr>1e-2 the performance was poor)"},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nhistory = model.fit_generator(train_datagen.flow(x_train, y_train, batch_size=batch_size, shuffle=True),\n                              steps_per_epoch=100,\n                              epochs=10,\n                              validation_data=valid_datagen.flow(x_valid, y_valid),\n                              validation_steps=50,\n                              callbacks=[learning_rate_reduction, es])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.optimizer.lr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save('Tmodelx.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot training & validation accuracy values\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()\n\n# Plot training & validation loss values\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = model.predict(x_test/255.)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = np.argmax(predictions, axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsubmission['label'] = preds\nsubmission.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"I have tried running it for more epochs (10-30) but the accuracy was oscillating so was the error. I tried tried to use lesser lr but it also didn't work. What do you think I should do to improve the accuracy and the score on the LB please, let me know in the comments."}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}