{"cells":[{"metadata":{},"cell_type":"markdown","source":"Here I am using the image generator given by Keras(tensorflow.keras.preprocessing.image.ImageDataGenerator) and I u**sed the Dig-MNIST as my validation data**. I started with training my model for 50 epochs and got an accuracy 0.98120. Then I started to increase the number of epochs to determine how my model performs if I train it for a greater number. I trained it for 100, 150 and 200 epochs and got accuracy 0.98280, 0.98320 and 0.98620  accordingly. As the training accuracy is increased by training more, I am training this for 220 epochs. After this 220 epoch, the accuracy does not improve that much. \nI have tried to keep my number of the parameter as low as I can with a good score."},{"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":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics\nimport time\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten, MaxPool2D, Dropout, BatchNormalization, LeakyReLU\nfrom tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras.callbacks import LearningRateScheduler\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, LearningRateScheduler\n\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest_data = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")\ndig_data = pd.read_csv(\"/kaggle/input/Kannada-MNIST/Dig-MNIST.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"Train set shape = \" +str(train_data.shape))\nprint(\"Test set shape = \" +str(test_data.shape))\nprint(\"Dif set shape = \" +str(dig_data.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"val_count=train_data.label.value_counts()\nsns.barplot(val_count.index, val_count)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Slicing and Reshaping training images\ndata_train = train_data.iloc[:,1:].values \nx_train = data_train.reshape(data_train.shape[0], 28, 28, 1)\n#Slicing training labels and applying one hot encoding\ntrain_label = train_data.iloc[:,0].values \ny_train = tf.keras.utils.to_categorical(train_label, 10)\nprint(x_train.shape, y_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Slicing and Reshaping validation images\ndata_val=dig_data.drop('label',axis=1).iloc[:,:].values\nx_val = data_val.reshape(data_val.shape[0], 28, 28,1)\n#Slicing validation labels and applying one hot encoding\nval_label=dig_data.label\ny_val = tf.keras.utils.to_categorical(val_label, 10) \nprint(x_val.shape, y_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#procesing  test data\nx_test=test_data.drop('id', axis=1).iloc[:,:].values\nx_test = x_test.reshape(x_test.shape[0], 28, 28,1)\nx_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (3,3), padding = 'Same', activation = 'relu', input_shape = (28,28,1)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(filters = 32, kernel_size = (3,3), padding = 'Same', activation = 'relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3), padding = 'Same', activation = 'relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3), padding = 'Same', activation = 'relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n\nmodel.add(Conv2D(filters = 128, kernel_size = (3,3), padding = 'Same', activation = 'relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(filters = 128, kernel_size = (3,3), padding = 'Same', activation = 'relu'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n\nmodel.add(Flatten())\nmodel.add(Dense(64, activation = 'relu'))\nmodel.add(Dense(32, activation = 'relu'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(10, activation = 'softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_shape = (28, 28, 1)\nlearning_rate=0.001\nbatch_size = 512\nepochs = 280\noptimizer = Adam(learning_rate=learning_rate, beta_1=0.9, beta_2=0.999)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def lr_decay(epoch):\n    return learning_rate * 0.99 ** epoch","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data_generator = ImageDataGenerator(rescale = 1./255.,\n                                        rotation_range = 20,\n                                        width_shift_range = 0.1,\n                                        height_shift_range = 0.1,\n                                        shear_range = 0.1,\n                                        zoom_range = [0.2, 1.2],\n                                        horizontal_flip = False)\nval_data_generator = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer=optimizer, loss=['categorical_crossentropy'], metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"** As there is a time limit for 2 hours, I am checking after every epoch whether I am running out of time or not.**"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Featching the start time and tracking it to check if the training hour passed more than 2 hours or not\nstart = time.time()\ntime_spent = 0\naccuracy = []\nval_accuracy = []\nfor epoch in range (220):\n    time_spent = time.time()-start\n    if time_spent >= 7150:  # as training time limit >=2 hours (2 * 60 * 60 = 7200s)\n        break\n    else:\n        epoch += 1\n        print('epoch:', epoch)\n        history = model.fit_generator(\n          train_data_generator.flow(x_train,y_train, batch_size=batch_size),\n          steps_per_epoch=100,\n          epochs=1,\n          callbacks=[LearningRateScheduler(lr_decay)],\n          validation_data=val_data_generator.flow(x_val,y_val),\n          validation_steps=50,  \n          verbose=1, shuffle = True)\n        accuracy.append(history.history['accuracy'])\n        val_accuracy.append(history.history['val_accuracy'])\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_accuracy(accuracy, val_accuracy):\n    epochs = range(len(accuracy))\n    plt.plot(epochs, accuracy, 'r')\n    plt.plot(epochs, val_accuracy, 'b')\n    plt.title('Training accuracy')\n    plt.legend(['train', 'val'], loc='lower right')\n    plt.grid()\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_accuracy(accuracy, val_accuracy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test = x_test/255\n\npredictions = model.predict_classes(x_test)\nsubmission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsubmission['label'] = predictions\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv(\"submission.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('end...')","execution_count":null,"outputs":[]}],"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}