{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-21T11:17:53.697328Z","iopub.execute_input":"2023-11-21T11:17:53.697771Z","iopub.status.idle":"2023-11-21T11:17:54.080204Z","shell.execute_reply.started":"2023-11-21T11:17:53.697731Z","shell.execute_reply":"2023-11-21T11:17:54.079217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.datasets import mnist\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras import utils\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten, MaxPooling2D, Dropout\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:17:54.082181Z","iopub.execute_input":"2023-11-21T11:17:54.082703Z","iopub.status.idle":"2023-11-21T11:17:57.554640Z","shell.execute_reply.started":"2023-11-21T11:17:54.082670Z","shell.execute_reply":"2023-11-21T11:17:57.553473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = np.loadtxt('/kaggle/input/digit-recognizer/train.csv', skiprows=1, delimiter=',')","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:17:57.556281Z","iopub.execute_input":"2023-11-21T11:17:57.557250Z","iopub.status.idle":"2023-11-21T11:18:00.078319Z","shell.execute_reply.started":"2023-11-21T11:17:57.557203Z","shell.execute_reply":"2023-11-21T11:18:00.077103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset[453:455]","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:00.079719Z","iopub.execute_input":"2023-11-21T11:18:00.080160Z","iopub.status.idle":"2023-11-21T11:18:00.088823Z","shell.execute_reply.started":"2023-11-21T11:18:00.080128Z","shell.execute_reply":"2023-11-21T11:18:00.087502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train_dataset[:, 1:]","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:00.091653Z","iopub.execute_input":"2023-11-21T11:18:00.092031Z","iopub.status.idle":"2023-11-21T11:18:00.097589Z","shell.execute_reply.started":"2023-11-21T11:18:00.092001Z","shell.execute_reply":"2023-11-21T11:18:00.096756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train /= 255.0\n","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:00.098853Z","iopub.execute_input":"2023-11-21T11:18:00.099166Z","iopub.status.idle":"2023-11-21T11:18:00.178793Z","shell.execute_reply.started":"2023-11-21T11:18:00.099139Z","shell.execute_reply":"2023-11-21T11:18:00.177661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) # number of images, image size (28x28), number of color channels\ninput_shape = (28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:00.180817Z","iopub.execute_input":"2023-11-21T11:18:00.181535Z","iopub.status.idle":"2023-11-21T11:18:00.186975Z","shell.execute_reply.started":"2023-11-21T11:18:00.181496Z","shell.execute_reply":"2023-11-21T11:18:00.185849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:00.188170Z","iopub.execute_input":"2023-11-21T11:18:00.188483Z","iopub.status.idle":"2023-11-21T11:18:00.199085Z","shell.execute_reply.started":"2023-11-21T11:18:00.188455Z","shell.execute_reply":"2023-11-21T11:18:00.198221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = np.loadtxt('/kaggle/input/digit-recognizer/test.csv', skiprows=1, delimiter=\",\")","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:00.200788Z","iopub.execute_input":"2023-11-21T11:18:00.201086Z","iopub.status.idle":"2023-11-21T11:18:01.858735Z","shell.execute_reply.started":"2023-11-21T11:18:00.201061Z","shell.execute_reply":"2023-11-21T11:18:01.857560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test_dataset / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:01.860146Z","iopub.execute_input":"2023-11-21T11:18:01.860458Z","iopub.status.idle":"2023-11-21T11:18:01.917061Z","shell.execute_reply.started":"2023-11-21T11:18:01.860431Z","shell.execute_reply":"2023-11-21T11:18:01.916066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:01.918184Z","iopub.execute_input":"2023-11-21T11:18:01.918464Z","iopub.status.idle":"2023-11-21T11:18:01.923503Z","shell.execute_reply.started":"2023-11-21T11:18:01.918439Z","shell.execute_reply":"2023-11-21T11:18:01.922453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:01.924831Z","iopub.execute_input":"2023-11-21T11:18:01.925380Z","iopub.status.idle":"2023-11-21T11:18:01.934490Z","shell.execute_reply.started":"2023-11-21T11:18:01.925351Z","shell.execute_reply":"2023-11-21T11:18:01.933516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_dataset[:, 0]\ny_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:01.935559Z","iopub.execute_input":"2023-11-21T11:18:01.936089Z","iopub.status.idle":"2023-11-21T11:18:01.947791Z","shell.execute_reply.started":"2023-11-21T11:18:01.936057Z","shell.execute_reply":"2023-11-21T11:18:01.946677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = utils.to_categorical(y_train, 10)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:01.952716Z","iopub.execute_input":"2023-11-21T11:18:01.953085Z","iopub.status.idle":"2023-11-21T11:18:01.959551Z","shell.execute_reply.started":"2023-11-21T11:18:01.953052Z","shell.execute_reply":"2023-11-21T11:18:01.958645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:01.960852Z","iopub.execute_input":"2023-11-21T11:18:01.961154Z","iopub.status.idle":"2023-11-21T11:18:01.971507Z","shell.execute_reply.started":"2023-11-21T11:18:01.961122Z","shell.execute_reply":"2023-11-21T11:18:01.970809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = test_dataset[:, 0]\n","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:01.973157Z","iopub.execute_input":"2023-11-21T11:18:01.973534Z","iopub.status.idle":"2023-11-21T11:18:01.980560Z","shell.execute_reply.started":"2023-11-21T11:18:01.973498Z","shell.execute_reply":"2023-11-21T11:18:01.979614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = utils.to_categorical(y_test, 10)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:01.981674Z","iopub.execute_input":"2023-11-21T11:18:01.981993Z","iopub.status.idle":"2023-11-21T11:18:01.993420Z","shell.execute_reply.started":"2023-11-21T11:18:01.981954Z","shell.execute_reply":"2023-11-21T11:18:01.992414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:01.995117Z","iopub.execute_input":"2023-11-21T11:18:01.995767Z","iopub.status.idle":"2023-11-21T11:18:02.003099Z","shell.execute_reply.started":"2023-11-21T11:18:01.995736Z","shell.execute_reply":"2023-11-21T11:18:02.002136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:02.004507Z","iopub.execute_input":"2023-11-21T11:18:02.004790Z","iopub.status.idle":"2023-11-21T11:18:02.043394Z","shell.execute_reply.started":"2023-11-21T11:18:02.004765Z","shell.execute_reply":"2023-11-21T11:18:02.042404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Conv2D(filters = 32, kernel_size = (3,3),\n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),\n                 activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(filters = 128, kernel_size = (3,3),\n                 activation ='relu' ))\nmodel.add(Conv2D(filters = 256, kernel_size = (3,3),\n                 activation ='relu' ))\nmodel.add(Dropout(0.03))\n#model.add(MaxPooling2D(pool_size=(2,2)))\n#model.add(Conv2D(filters = 256, kernel_size = (3,3),\n#                 activation ='relu'))\n#model.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(200, activation='relu')) \nmodel.add(Dropout(0.2))\nmodel.add(Dense(100, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10, activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:02.044845Z","iopub.execute_input":"2023-11-21T11:18:02.045262Z","iopub.status.idle":"2023-11-21T11:18:02.216051Z","shell.execute_reply.started":"2023-11-21T11:18:02.045224Z","shell.execute_reply":"2023-11-21T11:18:02.215032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:02.217202Z","iopub.execute_input":"2023-11-21T11:18:02.217509Z","iopub.status.idle":"2023-11-21T11:18:02.261035Z","shell.execute_reply.started":"2023-11-21T11:18:02.217481Z","shell.execute_reply":"2023-11-21T11:18:02.259604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:02.264463Z","iopub.execute_input":"2023-11-21T11:18:02.264944Z","iopub.status.idle":"2023-11-21T11:18:02.285935Z","shell.execute_reply.started":"2023-11-21T11:18:02.264914Z","shell.execute_reply":"2023-11-21T11:18:02.285176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"utils.plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=False, show_layer_activations=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:02.287053Z","iopub.execute_input":"2023-11-21T11:18:02.287641Z","iopub.status.idle":"2023-11-21T11:18:02.411788Z","shell.execute_reply.started":"2023-11-21T11:18:02.287611Z","shell.execute_reply":"2023-11-21T11:18:02.411027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.python.keras.callbacks import ModelCheckpoint","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:02.412719Z","iopub.execute_input":"2023-11-21T11:18:02.412997Z","iopub.status.idle":"2023-11-21T11:18:02.416976Z","shell.execute_reply.started":"2023-11-21T11:18:02.412954Z","shell.execute_reply":"2023-11-21T11:18:02.416250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelCheckpoint = ModelCheckpoint(\"model_CNN.h5\",        # model_CNN.h5 model_Dense.h5\n                                  monitor='val_accuracy',\n                                  verbose=2,\n                                  save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:02.417951Z","iopub.execute_input":"2023-11-21T11:18:02.418273Z","iopub.status.idle":"2023-11-21T11:18:02.435950Z","shell.execute_reply.started":"2023-11-21T11:18:02.418245Z","shell.execute_reply":"2023-11-21T11:18:02.435038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [modelCheckpoint]","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:02.437489Z","iopub.execute_input":"2023-11-21T11:18:02.438220Z","iopub.status.idle":"2023-11-21T11:18:02.446366Z","shell.execute_reply.started":"2023-11-21T11:18:02.438179Z","shell.execute_reply":"2023-11-21T11:18:02.445250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train,\n                    y_train,\n                    batch_size=350,\n                    epochs=26,\n                    validation_split=0.15,\n                    callbacks = [modelCheckpoint],\n                    verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:18:02.448048Z","iopub.execute_input":"2023-11-21T11:18:02.448922Z","iopub.status.idle":"2023-11-21T11:32:45.903931Z","shell.execute_reply.started":"2023-11-21T11:18:02.448762Z","shell.execute_reply":"2023-11-21T11:32:45.903173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:45.905721Z","iopub.execute_input":"2023-11-21T11:32:45.906354Z","iopub.status.idle":"2023-11-21T11:32:45.915286Z","shell.execute_reply.started":"2023-11-21T11:32:45.906311Z","shell.execute_reply":"2023-11-21T11:32:45.914090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'],\n         label='Доля верных ответов на обучающем наборе')\nplt.plot(history.history['val_accuracy'],\n         label='Доля верных ответов на проверочном наборе')\nplt.xlabel('Эпоха обучения')\nplt.ylabel('Доля верных ответов')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:45.917217Z","iopub.execute_input":"2023-11-21T11:32:45.918022Z","iopub.status.idle":"2023-11-21T11:32:46.207505Z","shell.execute_reply.started":"2023-11-21T11:32:45.917955Z","shell.execute_reply":"2023-11-21T11:32:46.206488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('model_CNN.h5')","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:46.208765Z","iopub.execute_input":"2023-11-21T11:32:46.209093Z","iopub.status.idle":"2023-11-21T11:32:46.245311Z","shell.execute_reply.started":"2023-11-21T11:32:46.209064Z","shell.execute_reply":"2023-11-21T11:32:46.244458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = model.evaluate(x_test, y_test, verbose=0)\nprint('\\033[91m', \"The model recognized\", round(scores[1] * 100, 4), \"% images correctly\")","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:37:15.907727Z","iopub.execute_input":"2023-11-21T11:37:15.908180Z","iopub.status.idle":"2023-11-21T11:37:21.007380Z","shell.execute_reply.started":"2023-11-21T11:37:15.908141Z","shell.execute_reply":"2023-11-21T11:37:21.006242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:51.492001Z","iopub.execute_input":"2023-11-21T11:32:51.492708Z","iopub.status.idle":"2023-11-21T11:32:57.121514Z","shell.execute_reply.started":"2023-11-21T11:32:51.492665Z","shell.execute_reply":"2023-11-21T11:32:57.120411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[:3]","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:57.122870Z","iopub.execute_input":"2023-11-21T11:32:57.125123Z","iopub.status.idle":"2023-11-21T11:32:57.132721Z","shell.execute_reply.started":"2023-11-21T11:32:57.125063Z","shell.execute_reply":"2023-11-21T11:32:57.131580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:57.134202Z","iopub.execute_input":"2023-11-21T11:32:57.134685Z","iopub.status.idle":"2023-11-21T11:32:57.143502Z","shell.execute_reply.started":"2023-11-21T11:32:57.134637Z","shell.execute_reply":"2023-11-21T11:32:57.142358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[:3]","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:57.145121Z","iopub.execute_input":"2023-11-21T11:32:57.145543Z","iopub.status.idle":"2023-11-21T11:32:57.156074Z","shell.execute_reply.started":"2023-11-21T11:32:57.145504Z","shell.execute_reply":"2023-11-21T11:32:57.155028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:57.157132Z","iopub.execute_input":"2023-11-21T11:32:57.157411Z","iopub.status.idle":"2023-11-21T11:32:57.165186Z","shell.execute_reply.started":"2023-11-21T11:32:57.157386Z","shell.execute_reply":"2023-11-21T11:32:57.164497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:57.166119Z","iopub.execute_input":"2023-11-21T11:32:57.166765Z","iopub.status.idle":"2023-11-21T11:32:57.176839Z","shell.execute_reply.started":"2023-11-21T11:32:57.166735Z","shell.execute_reply":"2023-11-21T11:32:57.176154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out = np.column_stack((range(1, predictions.shape[0]+1), predictions))","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:57.178265Z","iopub.execute_input":"2023-11-21T11:32:57.178603Z","iopub.status.idle":"2023-11-21T11:32:57.190250Z","shell.execute_reply.started":"2023-11-21T11:32:57.178576Z","shell.execute_reply":"2023-11-21T11:32:57.189241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out[:5]","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:57.191247Z","iopub.execute_input":"2023-11-21T11:32:57.191799Z","iopub.status.idle":"2023-11-21T11:32:57.200601Z","shell.execute_reply.started":"2023-11-21T11:32:57.191772Z","shell.execute_reply":"2023-11-21T11:32:57.199727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.savetxt('submission.csv', out, header=\"ImageId,Label\",\n            comments=\"\", fmt=\"%d,%d\")","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:57.201619Z","iopub.execute_input":"2023-11-21T11:32:57.202995Z","iopub.status.idle":"2023-11-21T11:32:57.258082Z","shell.execute_reply.started":"2023-11-21T11:32:57.202949Z","shell.execute_reply":"2023-11-21T11:32:57.257113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head submission.csv","metadata":{"execution":{"iopub.status.busy":"2023-11-21T11:32:57.263797Z","iopub.execute_input":"2023-11-21T11:32:57.264291Z","iopub.status.idle":"2023-11-21T11:32:58.345449Z","shell.execute_reply.started":"2023-11-21T11:32:57.264255Z","shell.execute_reply":"2023-11-21T11:32:58.344327Z"},"trusted":true},"execution_count":null,"outputs":[]}]}