{"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)\n\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\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n        \nfor dirname, _, filenames in os.walk('/kaggle/working'):\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\nimport numpy as np\nimport h5py\nimport matplotlib.pyplot as plt\nimport scipy\nfrom PIL import Image\nfrom scipy import ndimage\nimport tensorflow as tf\nfrom tensorflow.python.framework import ops\n%matplotlib inline","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num=9\npixelsample=np.zeros((784,))\nfor i in range(783):\n    pixelsample[i]=df[\"pixel\"+str(i)][num]\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=pixelsample\na=np.reshape(a,(28,28))\nprint(\"y={}\".format(df[\"label\"][num]))\nplt.imshow(a)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m=28000\nX_data=np.zeros((m,28*28))\nfor i in range(m):\n    pixelarray=np.zeros((784,))\n    for j in range(783):\n       pixelarray[j]=df[\"pixel\"+str(j)][i] \n    X_data[i,:]=pixelarray","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=X_data[0,:]\na=np.reshape(a,(28,28))\nprint(\"y={}\".format(df[\"label\"][0]))\nplt.imshow(a)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_data=np.zeros((m,10))\nfor i in range(m):\n    y_label=np.zeros((10,))\n    val=df[\"label\"][i]\n    y_label[val]=1\n    Y_data[i,:]=y_label","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn\nfrom sklearn.model_selection import train_test_split\nprint(X_data.shape)\nprint(Y_data.shape)\nX_train, X_test, Y_train, Y_test = train_test_split(X_data,Y_data,test_size=0.05)\nX_train, X_cv, Y_train, Y_cv = train_test_split(X_train,Y_train,test_size=0.2)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Activation, Dropout, Dense, Flatten, BatchNormalization, Conv2D, MaxPooling2D\nfrom tensorflow.keras.optimizers import RMSprop,Adam\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.preprocessing import image\nfrom sklearn.metrics import accuracy_score, classification_report\n\nmodel = Sequential()\n\nmodel.add((Conv2D(60,(5,5),strides=(1,1),input_shape=(28, 28,1) ,padding = 'same' ,activation='relu')))\nmodel.add((Conv2D(60, (5,5),padding=\"same\",activation='relu')))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n#model.add(Dropout(0.25))\n\nmodel.add((Conv2D(30, (3,3),padding=\"same\", activation='relu')))\nmodel.add((Conv2D(30, (3,3), padding=\"same\", activation='relu')))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.5))\n\nmodel.add(Flatten())\nmodel.add(Dense(500,activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_cv[0].shape\nplt.imshow(X_cv[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_data=np.reshape(X_data,(m,28,28,1))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=np.reshape(X_train,(21280,28,28,1))\nX_cv=np.reshape(X_cv,(5320,28,28,1))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#optimizer = RMSprop(lr=0.001, rho=0.9, epsilon = 1e-08, decay=0.0)\nmodel.compile(optimizer=Adam(lr=2e-6),loss='categorical_crossentropy',metrics=['accuracy'])\n\n# checkpoint_path = 'training_1/cp.ckpt'\n# checkpoint_dir = os.path.dirname(checkpoint_path)\n\n# # create checkpoint callback\n# cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path,\n#                                                 save_weights_only=True,\n#                                                 verbose=1)\n\nhistory = model.fit(X_train, Y_train,\n                        \n                    validation_data = (X_cv, Y_cv),\n                        epochs=1,\n                        steps_per_epoch=200,batch_size=32)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('my_model.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model = tf.keras.models.load_model('../input/model11/anoth_model11.h5')\nnew_model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#X_test=np.reshape(X_test,(1400,28,28,1))\npred = new_model.predict(X_data)\n#print('Restored model, accuracy: {:5.2f}%'.format(100*acc))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_index_row = np.argmax(pred, axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_index_row","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result=[]\nfor i in range(m):\n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = df.index.values + 1\n\nmy_submission = pd.DataFrame({'ImageId': index, 'Label': max_index_row.astype(int).ravel()})\n# you could use any filename. We choose submission here\nmy_submission.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model.fit(X_train, Y_train,\n                        \n                    validation_data = (X_cv, Y_cv),\n                        epochs=1,\n                        steps_per_epoch=200,batch_size=32)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Y_train[4])\n\n\n\nplt.imshow(X_train[4])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model.fit(X_train, Y_train,\n                        \n                    validation_data = (X_cv, Y_cv),\n                        epochs=1,\n                        steps_per_epoch=200,batch_size=32)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model.save('anoth_model17.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model.save('anoth_model3.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_path = 'training_1/cp.ckpt'\ncheckpoint_dir = os.path.dirname(checkpoint_path)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test=np.reshape(X_test,(1400,28,28,1))\nloss,acc=model.evaluate(X_test,Y_test)\nprint(\"accuarcy=\",acc*100)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_model = tf.keras.models.load_model('./anoth_model10.h5')\n\nnum=0\na=X_test[num]\nplt.imshow(a.reshape(28,28))\nprint(my_model.predict(a.reshape(1,28,28,1)))\nprint(np.max(my_model.predict(a.reshape(1,28,28,1))))\nY_test[num]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num=18\n\n\nplt.imshow(X_test[num])\narr=X_test[num]\narr=np.reshape(arr,(1,28,28,1))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=model.predict(arr)\n#np.max(pred)\npred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"version-86percent\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}