{"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_minor":4,"nbformat":4,"cells":[{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport warnings\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nimport keras\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Dropout\nfrom keras.optimizers import Adam\nfrom keras.callbacks import TensorBoard\nnum_classes = 10\n# Input data files are available in the read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\ntrain_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv',sep=',')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv', sep = ',')\n\ntrain_data = np.array(train_df, dtype = 'float32')\ntest_data = np.array(test_df, dtype='float32')\n\nx_train = train_data[:,1:]/255\ny_train = train_data[:,0]\nx_test= test_data[:,:]/255\n\nx_train,x_validate,y_train,y_validate = train_test_split(x_train,y_train,test_size = 0.2,random_state = 12345)\n\nclass_names = ['T_shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', \n               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']\nplt.figure(figsize=(10, 10))\nfor i in range(36):\n    plt.subplot(6, 6, i + 1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(x_train[i].reshape((28,28)))\n    label_index = int(y_train[i])\n    plt.title(class_names[label_index])\nplt.show()\n\nW_grid = 15\nL_grid = 15\n\nfig, axes = plt.subplots(L_grid, W_grid, figsize = (16,16))\naxes = axes.ravel() # flaten the 15 x 15 matrix into 225 array\nn_train = len(train_data) # get the length of the train dataset\n\n# Select a random number from 0 to n_train\nfor i in np.arange(0, W_grid * L_grid): # create evenly spaces variables \n\n    # Select a random number\n    index = np.random.randint(0, n_train)\n    # read and display an image with the selected index    \n    axes[i].imshow( train_data[index,1:].reshape((28,28)) )\n    labelindex = int(train_data[index,0])\n    axes[i].set_title(class_names[labelindex], fontsize = 9)\n    axes[i].axis('off')\n\nplt.subplots_adjust(hspace=0.3)\n\nimage_rows = 28\nimage_cols = 28\nbatch_size = 4096\nimage_shape = (image_rows,image_cols,1) \n\nx_train = x_train.reshape(x_train.shape[0],*image_shape)\nx_test = x_test.reshape(x_test.shape[0],*image_shape)\nx_validate = x_validate.reshape(x_validate.shape[0],*image_shape)\n\ncallback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)\n\ncnn_model = Sequential([\n    Conv2D(filters=32,kernel_size=3,activation='relu',input_shape = image_shape),\n    MaxPooling2D(pool_size=2) ,# down sampling the output instead of 28*28 it is 14*14\n    Dropout(0.2),\n    Flatten(), # flatten out the layers\n    Dense(32,activation='relu'),\n    Dense(10,activation = 'softmax')\n    \n])\n\ncnn_model.compile(loss ='sparse_categorical_crossentropy', optimizer=Adam(lr=0.001),metrics =['accuracy'])\n\nhistory = cnn_model.fit(\n    x_train,\n    y_train,\n    batch_size=4096,\n    epochs=200,\n    verbose=1,\n    validation_data=(x_validate,y_validate),\n)\n\nfrom scipy import signal\n\ndef srch_ple(x, y, num_fit):\n    p1 = np.poly1d(np.polyfit(x, y, num_fit))\n    yvals = p1(x)\n    yyyd = np.polyder(p1, 1)\n    peak_ind = signal.argrelextrema(yvals,np.less)[0]\n    print('The polyfit function:', '\\n', p1, '\\n')\n    print('The 1st order of polyfit function:', '\\n', yyyd)\n    return peak_ind, yvals\n    \npeak_ind, yvals = srch_ple(history.epoch, history.history['val_loss'], 4)\nplt.figure(figsize=(15, 10))\nplt.plot(history.history['loss'], '.', color='darkorange', label='Train Loss')\nplt.plot(history.history['val_loss'], 'b.', label='Validation Loss')\nplt.plot(yvals, 'y-', label='Polyfit of Val Loss', color='red', linewidth=2)\nplt.plot(peak_ind[len(peak_ind)-1], yvals[peak_ind[len(peak_ind)-1]], 'ro', markersize=15)\nbbox = dict(boxstyle=\"round\", fc=\"1.0\")\narrowprops = dict(\n    arrowstyle=\"->\",\n    connectionstyle=\"angle,angleA=0,angleB=90,rad=100\")\nplt.annotate('Stop Training', xy=(peak_ind[len(peak_ind)-1], yvals[peak_ind[len(peak_ind)-1]]), xytext=(100, 1.00),\n    bbox=bbox, arrowprops=arrowprops, size=15\n             )\nplt.legend(fontsize=15)\nplt.grid(True)\nplt.title('The Loss during Training', fontsize=18)\nplt.savefig('loss_epoch=200', dpi=50)\n\npeak_ind_acc, yvals_acc = srch_ple(history.epoch, history.history['val_accuracy'],6)\n\nplt.figure(figsize=(15, 10))\nplt.plot(history.history['accuracy'], '.', color='darkorange', label='Train Accuracy')\nplt.plot(history.history['val_accuracy'], 'b.', label='Validation Accuracy')\nplt.plot(yvals_acc, 'y-', label='Polyfit of Val Accuracy', color='red', linewidth=2)\nplt.plot(peak_ind_acc, yvals_acc[peak_ind_acc], 'ro', markersize=15)\nplt.legend(fontsize=15)\nplt.grid(True)\nplt.title('The Accuracy during Training', fontsize=18)\nplt.savefig('acc_epoch=200', dpi=50)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(cnn_model.predict(x_test), axis=-1)\nsubmissions=pd.DataFrame({\"ImageId\": list(range(1,len(predictions)+1)),\n                         \"Label\": predictions})\nsubmissions.to_csv(\"DR.csv\", index=False, header=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}