{"cells":[{"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 matplotlib.pyplot as plt\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D, Dense, Dropout, Flatten\nimport tensorflow.keras as ks\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def Extract(file,labels=False):\n\tprint(\"Beginning the extraction process...\")\n\twith open(file,'r') as f:\n\t\tdata = f.read()\n\t\tdata = data.split('\\n')\n\t\n\tData = []\n\tlabel = []\n\t\n\tdataLen = len(data)\n\t\n\t\n\tprint(\"preparing data into {} samples..\".format(dataLen))\n\t\n\tfor i in range(1,dataLen):\n\t\tif data[i] != '':\n\t\t\tif labels == False:\n\t\t\t\tData.append(np.array(data[i].split(','), dtype=np.float32))\n\t\t\telse:\n\t\t\t\tData.append(np.array(data[i].split(',')[1:], dtype=np.float32)) \n\t\t\t\tlabel.append(np.array(data[i].split(',')[0], dtype=np.int8))\n\t\t\t\n\n\tData = np.array(Data)/255\n\tdel data\n\tif labels == False: \n\t\treturn Data\n\telse:\n\t\treturn (Data,label)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.datasets.mnist import load_data #req internet\n\n#training on original mnist dataset\n(x_train,y_train),(x_test,y_test) = load_data()\n\ndata = Extract('/kaggle/input/digit-recognizer/test.csv')\n(moreTrain,label) = Extract('/kaggle/input/digit-recognizer/train.csv',labels=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\ny_test = to_categorical(y_test) #one hot encoding\n\nx_train = x_train.astype(np.float32) / 255\nx_test = x_test.astype(np.float32) / 255\nx_test = x_test.reshape(len(x_test),28,28,1)\nmoreTrain /= 255\nprint(x_train.shape)\nprint(y_train.shape)\nx_train = np.concatenate((x_train,moreTrain.reshape(len(moreTrain),28,28)),axis=0)\ny_train = np.concatenate((y_train,label),axis=0)\n\nx_train = x_train.reshape(len(x_train),28,28,1)\ny_train = to_categorical(y_train)\nprint(x_train.shape)\nprint(y_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#lets display some data\nfor i in range(8):\n    plt.subplot(2,4,i+1)\n    plt.imshow(x_test[i].reshape(28,28),cmap='binary')\n    plt.title(np.argmax(y_test[i]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#designing the model\nmodel = Sequential()\nmodel.add(Conv2D(filters=16, kernel_size=5, strides=1, padding='same', activation='relu', input_shape=(28, 28, 1), name='conv1'))\nmodel.add(Conv2D(filters=16, kernel_size=5, strides=1, padding='same', activation='relu', input_shape=(28, 28, 1), name='conv2'))\nmodel.add(MaxPool2D(pool_size=(2, 2),name='pool1'))\nmodel.add(Conv2D(filters=32, kernel_size=5, strides=1,padding='same', activation='relu',name='conv3'))\nmodel.add(Conv2D(filters=32, kernel_size=3, strides=1,padding='same', activation='relu',name='conv4'))\nmodel.add(MaxPool2D(pool_size=(2, 2),name='pool2'))\nmodel.add(Conv2D(filters=64, kernel_size=3, strides=1,padding='same', activation='relu',name='conv5'))\nmodel.add(Conv2D(filters=64, kernel_size=3, strides=1,padding='valid', activation='relu',name='conv6'))\nmodel.add(Flatten(name='flat'))\nmodel.add(Dense(512,activation='relu',name='dense1'))\nmodel.add(Dropout(0.2,name='dropout'))\nmodel.add(Dense(10,activation='softmax',name='res'))\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class Callback(ks.callbacks.Callback):\n    def on_epoch_end(self,epoch,logs={}):\n        if(logs['val_acc'] >= 0.999):\n            print(\"\\nValication accuracy reached more than 99.9%, training stopped\")\n            self.model.stop_training=True","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"earlyStop = Callback()\nadam = ks.optimizers.Adam(lr=1e-3)\nmodel.compile(loss='categorical_crossentropy',optimizer=adam, metrics=['accuracy'])\ncheckpoint = ks.callbacks.ModelCheckpoint('model.h5',save_best_only=True, monitor='val_acc', mode='max')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(x_train, y_train, validation_data=(x_test,y_test),batch_size=1024, epochs=100, shuffle=True, verbose=1, callbacks=[checkpoint,earlyStop])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.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')\n\nmodelBest = ks.models.load_model('model.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(model.evaluate(x_test,y_test))\nprint(modelBest.evaluate(x_test,y_test)) #for me, modelBest gave better accuracy","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('result.csv','w') as f:\n    f.write('ImageId,Label\\n')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"i=0\nfor x in data:\n    with open('result.csv','a') as f:\n        pred = modelBest.predict(x.reshape(1,28,28,1))\n        val = int(np.argmax(pred,axis=1))\n        i += 1\n        res = str(i) + ',' + str(val) + '\\n'\n        f.write(res)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#predict\nfor i in range(8):\n    plt.subplot(2,4,i+1)\n    plt.imshow(data[-i].reshape(28,28),cmap='binary')\n    pred = model.predict(data[-i].reshape(1,28,28,1))\n    val = int(np.argmax(pred,axis=1))\n    plt.title(str(val))","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":4}