{"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":"from tensorflow.keras.models                     import Sequential\nfrom tensorflow.keras.layers                     import Dense, Conv2D, MaxPooling2D, Dropout, Flatten \nfrom tensorflow.keras                            import utils\nfrom tensorflow.keras.preprocessing              import image\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks                  import ReduceLROnPlateau, ModelCheckpoint\nfrom sklearn.model_selection                     import train_test_split\nimport numpy as np\nimport os\nimport matplotlib.pyplot as plt","metadata":{"id":"sG1iPUF-4SrL","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install kaggle","metadata":{"id":"N277Isx34a8Z","outputId":"05bc3b55-7ce5-41d7-c2c1-30f0b26bbb71","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir ~/.kaggle\np = os.path.abspath('.kaggle')\nprint(p)\n!ls","metadata":{"id":"Res6vHg04dMB","outputId":"6f488528-6695-4cfe-fc72-b96c1ae8c00a","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mv kaggle.json ~/.kaggle\n!kaggle competitions download -c digit-recognizer","metadata":{"id":"nWkIz2dM4ho6","outputId":"b61e3b6a-fb37-44fd-af5c-0083707abeff","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"id":"IpFBy0U74inu","outputId":"5650568b-6f70-4d47-c543-35f15288326d","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip '/content/train.csv.zip'\n!unzip '/content/test.csv.zip'","metadata":{"id":"OrH_jQcfcRuT","outputId":"191d9ff5-a2dd-4220-b05c-df78ee98bca1","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = np.loadtxt('train.csv', skiprows=1, delimiter=',')","metadata":{"id":"OzWAgjRs4lst","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset[0:5]\nx_train = train_dataset[:, 1:]\n# Переформатируем данные в 2D, бэкенд TensorFlow\nx_train = x_train.reshape(x_train.shape[0], 28, 28, 1)\ninput_shape = (28, 28, 1) ","metadata":{"id":"Zp70SRLE4qfZ","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train /= 255.0\nx_train[1].shape\ny_train = train_dataset[:, 0]\ny_train[:5]\ny_train = utils.to_categorical(y_train)\n","metadata":{"id":"8ClnDQ8O4s3F","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_seed = 10\nX_train, X_val, Y_train, Y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=random_seed)","metadata":{"id":"8MKFaFSJ4wJm","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.1, # Randomly zoom image \n        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\n\ndatagen.fit(X_train)","metadata":{"id":"xRmj7T_b4yfW","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0\ndata = X_train[0]\ndata = np.expand_dims(data, axis=0)\nfor batch in datagen.flow(data, batch_size=1):\n    plt.figure(i)\n    imgplot = plt.imshow(batch[0][:,:,0])\n    i += 1\n    if i % 6 == 0:\n        break\nplt.show()","metadata":{"id":"LabmuZnc41JX","outputId":"554c0fb2-7c20-472d-a2da-03b849d64b26","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (28,28,1)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))","metadata":{"id":"VCGjlJKb43dk","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])\n\nprint(model.summary())","metadata":{"id":"dhzlkREG450r","outputId":"482265b1-d4fa-4702-8695-a556c26b670e","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"сheckpoint = ModelCheckpoint('content/mnist-cnn.h5', \n                              monitor='val_acc', \n                              save_best_only=True,\n                              verbose=1)","metadata":{"id":"6p557Rk648Ho","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","metadata":{"id":"OlwfX0Br4-v1","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 256\nhistory = model.fit(datagen.flow(X_train,Y_train, batch_size=batch_size), \n                    epochs=50,\n                    validation_data=(X_val, Y_val),\n                    steps_per_epoch=X_train.shape[0] // batch_size,\n                    verbose=1,\n                    callbacks=[сheckpoint, learning_rate_reduction])","metadata":{"id":"HUkwaUwi5Df9","outputId":"d3606c9b-a89c-4882-f032-1fbc556ef0f9","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], \n         label='Share of correct answers on educational set')\nplt.plot(history.history['val_accuracy'], \n         label='Share of correct answers on checking set')\nplt.xlabel('Epochs')\nplt.ylabel('share of correct answers')\nplt.legend()\nplt.show()","metadata":{"id":"29iqv6L2f9K1","outputId":"c202716d-2a62-4dba-a7b8-85f4d4a39b06","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = np.loadtxt('test.csv', skiprows=1, delimiter=\",\")\nx_test = test_dataset.reshape(test_dataset.shape[0], 28, 28, 1)\nx_test = x_test / 255.0","metadata":{"id":"8Fkcwz845GDd","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_test)\npredictions[:5]","metadata":{"id":"kqJPkaVo5ImN","outputId":"ffd91475-27a1-4a14-c323-86055cf23dae","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(predictions, axis=1)\npredictions[:5]","metadata":{"id":"zoD845GnfuGl","outputId":"3b1ace4b-6bad-4c42-9bfa-e796e4e6ccee","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out = np.column_stack((range(1, predictions.shape[0]+1), predictions))","metadata":{"id":"Rk3kK4il5K3w","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.savetxt('submission.csv', out, header=\"ImageId,Label\", \n            comments=\"\", fmt=\"%d,%d\")","metadata":{"id":"RDIU0RZK5NLW","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!kaggle competitions submit -c digit-recognizer -m \"Submition from Colab\" -f submission.csv","metadata":{"id":"RCOlNE9w5PEM","trusted":true},"execution_count":null,"outputs":[]}]}