{"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":"import numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow import keras as kr\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Dense, Activation, Flatten,BatchNormalization, Activation,MaxPool2D,Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.utils.np_utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nfrom keras.callbacks import ReduceLROnPlateau\n\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-16T11:25:05.203985Z","iopub.execute_input":"2021-10-16T11:25:05.204299Z","iopub.status.idle":"2021-10-16T11:25:05.217880Z","shell.execute_reply.started":"2021-10-16T11:25:05.204266Z","shell.execute_reply":"2021-10-16T11:25:05.217057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-16T11:25:05.219787Z","iopub.execute_input":"2021-10-16T11:25:05.219994Z","iopub.status.idle":"2021-10-16T11:25:05.234550Z","shell.execute_reply.started":"2021-10-16T11:25:05.219970Z","shell.execute_reply":"2021-10-16T11:25:05.233889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:05.235693Z","iopub.execute_input":"2021-10-16T11:25:05.236533Z","iopub.status.idle":"2021-10-16T11:25:09.816532Z","shell.execute_reply.started":"2021-10-16T11:25:05.236468Z","shell.execute_reply":"2021-10-16T11:25:09.815676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_data.drop([\"label\"],axis = 1).values\nY = train_data[\"label\"].values","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:09.817714Z","iopub.execute_input":"2021-10-16T11:25:09.818351Z","iopub.status.idle":"2021-10-16T11:25:09.902623Z","shell.execute_reply.started":"2021-10-16T11:25:09.818321Z","shell.execute_reply":"2021-10-16T11:25:09.901842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#преобразование в матрицу изображений\nX = X.reshape([42000,28,28,1])\nY = Y.reshape([42000,1])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:09.905823Z","iopub.execute_input":"2021-10-16T11:25:09.906537Z","iopub.status.idle":"2021-10-16T11:25:09.913165Z","shell.execute_reply.started":"2021-10-16T11:25:09.906486Z","shell.execute_reply":"2021-10-16T11:25:09.912505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#конвертирование в двочиную матрицу для 10 категорий (цифры 0-9)\nY = to_categorical(Y, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:09.916124Z","iopub.execute_input":"2021-10-16T11:25:09.916343Z","iopub.status.idle":"2021-10-16T11:25:09.926117Z","shell.execute_reply.started":"2021-10-16T11:25:09.916318Z","shell.execute_reply":"2021-10-16T11:25:09.925204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#разделение данных на обучающую выборку (x_train, x_train) и данные для проверки (x_test, Y_test). Размер набора данных для проверки = 10%\nx_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.1, random_state = 14)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:09.927700Z","iopub.execute_input":"2021-10-16T11:25:09.928371Z","iopub.status.idle":"2021-10-16T11:25:10.322628Z","shell.execute_reply.started":"2021-10-16T11:25:09.928324Z","shell.execute_reply":"2021-10-16T11:25:10.321852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#нормализация данных\nx_train = x_train/255\nx_test = x_test/255","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:10.324136Z","iopub.execute_input":"2021-10-16T11:25:10.324452Z","iopub.status.idle":"2021-10-16T11:25:10.427515Z","shell.execute_reply.started":"2021-10-16T11:25:10.324411Z","shell.execute_reply":"2021-10-16T11:25:10.426677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cоздание модели\nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(64, (3,3), padding = 'same', activation='relu', input_shape=(28, 28, 1)),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(64, (3,3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(128, (3,3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Conv2D(128, (3,3), padding = 'same', activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Dropout(0.25),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(256, activation='relu'),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:10.428757Z","iopub.execute_input":"2021-10-16T11:25:10.428978Z","iopub.status.idle":"2021-10-16T11:25:10.524171Z","shell.execute_reply.started":"2021-10-16T11:25:10.428951Z","shell.execute_reply":"2021-10-16T11:25:10.523339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#конфигурация обучения модели\noptimizer = Adam(learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999)\nmodel.compile(optimizer = optimizer, loss = 'categorical_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:10.525413Z","iopub.execute_input":"2021-10-16T11:25:10.525642Z","iopub.status.idle":"2021-10-16T11:25:10.535159Z","shell.execute_reply.started":"2021-10-16T11:25:10.525615Z","shell.execute_reply":"2021-10-16T11:25:10.534256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#настройка скорости обучения\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.6, \n                                            min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:10.536313Z","iopub.execute_input":"2021-10-16T11:25:10.536566Z","iopub.status.idle":"2021-10-16T11:25:10.543737Z","shell.execute_reply.started":"2021-10-16T11:25:10.536536Z","shell.execute_reply":"2021-10-16T11:25:10.542784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\nepochs = 30","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:10.545131Z","iopub.execute_input":"2021-10-16T11:25:10.545378Z","iopub.status.idle":"2021-10-16T11:25:10.558821Z","shell.execute_reply.started":"2021-10-16T11:25:10.545352Z","shell.execute_reply":"2021-10-16T11:25:10.558063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#обработка изображений\ntrain_datagen = ImageDataGenerator( \n        rotation_range=10,  \n        zoom_range = 0.1, \n        width_shift_range=0.1,  \n        height_shift_range=0.1,\n        shear_range = 0.1,\n        horizontal_flip=False,  \n        vertical_flip=False\n        )\ntrain_datagen.fit(x_train)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:10.560074Z","iopub.execute_input":"2021-10-16T11:25:10.560997Z","iopub.status.idle":"2021-10-16T11:25:10.666311Z","shell.execute_reply.started":"2021-10-16T11:25:10.560952Z","shell.execute_reply":"2021-10-16T11:25:10.665594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#обучение модели\nhistory = model.fit(\n            train_datagen.flow(x_train,y_train,batch_size = batch_size),\n            validation_data = (x_test,y_test),\n            batch_size = batch_size,\n            steps_per_epoch = x_train.shape[0]//batch_size,\n            epochs = epochs,\n            verbose = 1,\n            callbacks=[learning_rate_reduction]\n            )","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:25:10.668699Z","iopub.execute_input":"2021-10-16T11:25:10.669473Z","iopub.status.idle":"2021-10-16T11:52:40.542241Z","shell.execute_reply.started":"2021-10-16T11:25:10.669409Z","shell.execute_reply":"2021-10-16T11:52:40.541551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#оценка потерь и точности\nmodel.evaluate(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:52:40.543679Z","iopub.execute_input":"2021-10-16T11:52:40.544129Z","iopub.status.idle":"2021-10-16T11:52:42.019723Z","shell.execute_reply.started":"2021-10-16T11:52:40.544082Z","shell.execute_reply":"2021-10-16T11:52:42.019126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#преобразование в матрицу изображений и нормализация тестовых данных\ndata = test_data.values\ndata = data.reshape([28000,28,28,1])\nprint(data.shape)\ndata = data/255","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:52:42.020536Z","iopub.execute_input":"2021-10-16T11:52:42.020735Z","iopub.status.idle":"2021-10-16T11:52:42.144886Z","shell.execute_reply.started":"2021-10-16T11:52:42.020710Z","shell.execute_reply":"2021-10-16T11:52:42.144141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#предсказания\ntest_pred = model.predict(data)\ntest_pred = np.argmax(test_pred,axis=1)\nprint(test_pred.shape)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:52:42.146486Z","iopub.execute_input":"2021-10-16T11:52:42.146818Z","iopub.status.idle":"2021-10-16T11:52:51.899753Z","shell.execute_reply.started":"2021-10-16T11:52:42.146776Z","shell.execute_reply":"2021-10-16T11:52:51.899164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2021-10-16T11:52:51.900774Z","iopub.execute_input":"2021-10-16T11:52:51.901097Z","iopub.status.idle":"2021-10-16T11:52:51.930050Z","shell.execute_reply.started":"2021-10-16T11:52:51.901069Z","shell.execute_reply":"2021-10-16T11:52:51.929114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = sample_submission.ImageId\ndata = {'ImageId' : index,'Label': test_pred}\ndf = pd.DataFrame(data)\ndf.head","metadata":{"execution":{"iopub.status.busy":"2021-10-16T12:02:19.476885Z","iopub.execute_input":"2021-10-16T12:02:19.477588Z","iopub.status.idle":"2021-10-16T12:02:19.487410Z","shell.execute_reply.started":"2021-10-16T12:02:19.477545Z","shell.execute_reply":"2021-10-16T12:02:19.486542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T12:02:21.714603Z","iopub.execute_input":"2021-10-16T12:02:21.714870Z","iopub.status.idle":"2021-10-16T12:02:21.772734Z","shell.execute_reply.started":"2021-10-16T12:02:21.714842Z","shell.execute_reply":"2021-10-16T12:02:21.771830Z"},"trusted":true},"execution_count":null,"outputs":[]}]}