{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "T4"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "**Пункт 1**"
      ],
      "metadata": {
        "id": "02oT0d1nrHsn"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "os.chdir('/content/drive/MyDrive/Colab Notebooks/IS_LR3')"
      ],
      "metadata": {
        "id": "tRydpSDQWJiB"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# импорт модулей\n",
        "from tensorflow import keras\n",
        "from tensorflow.keras import layers\n",
        "from tensorflow.keras.models import Sequential\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "from sklearn.metrics import classification_report, confusion_matrix\n",
        "from sklearn.metrics import ConfusionMatrixDisplay"
      ],
      "metadata": {
        "id": "lR2eVWcXa3G3"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Пункт** 2"
      ],
      "metadata": {
        "id": "FUlWVQ9WrUIG"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# загрузка датасета\n",
        "from keras.datasets import mnist\n",
        "(X_train, y_train), (X_test, y_test) = mnist.load_data()"
      ],
      "metadata": {
        "id": "4HaBhEvAqcIk"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Пункт 3**"
      ],
      "metadata": {
        "id": "OXQ1zw59rZC9"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# создание своего разбиения датасета\n",
        "from sklearn.model_selection import train_test_split\n",
        "\n",
        "# объединяем в один набор\n",
        "X = np.concatenate((X_train, X_test))\n",
        "y = np.concatenate((y_train, y_test))\n",
        "\n",
        "# разбиваем по вариантам\n",
        "X_train, X_test, y_train, y_test = train_test_split(X, y,\n",
        "                                                    test_size = 10000,\n",
        "                                                    train_size = 60000,\n",
        "                                                    random_state = 15)"
      ],
      "metadata": {
        "id": "IpFTFYx3bH2n"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# вывод размерностей\n",
        "print('Shape of X train:', X_train.shape)\n",
        "print('Shape of y train:', y_train.shape)\n",
        "\n",
        "print('Shape of X test:', X_test.shape)\n",
        "print('Shape of y test:', y_test.shape)"
      ],
      "metadata": {
        "id": "QVquJXQgqfLF"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Пункт 4**"
      ],
      "metadata": {
        "id": "FSolDisjriNY"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Зададим параметры данных и модели\n",
        "num_classes = 10\n",
        "input_shape = (28, 28, 1)\n",
        "\n",
        "# Приведение входных данных к диапазону [0, 1]\n",
        "X_train = X_train / 255\n",
        "X_test = X_test / 255\n",
        "\n",
        "# Расширяем размерность входных данных, чтобы каждое изображение имело\n",
        "# размерность (высота, ширина, количество каналов)\n",
        "\n",
        "\n",
        "X_train = np.expand_dims(X_train, -1)\n",
        "X_test = np.expand_dims(X_test, -1)\n",
        "print('Shape of transformed X train:', X_train.shape)\n",
        "print('Shape of transformed X test:', X_test.shape)\n",
        "\n",
        "# переведем метки в one-hot\n",
        "y_train = keras.utils.to_categorical(y_train, num_classes)\n",
        "y_test = keras.utils.to_categorical(y_test, num_classes)\n",
        "print('Shape of transformed y train:', y_train.shape)\n",
        "print('Shape of transformed y test:', y_test.shape)"
      ],
      "metadata": {
        "id": "tYOmNY6HqhgT"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Пункт 5**"
      ],
      "metadata": {
        "id": "J22FRrP6rp6H"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# создаем модель\n",
        "model = Sequential()\n",
        "model.add(layers.Conv2D(32, kernel_size=(3, 3), activation=\"relu\", input_shape=input_shape))\n",
        "model.add(layers.MaxPooling2D(pool_size=(2, 2)))\n",
        "model.add(layers.Conv2D(64, kernel_size=(3, 3), activation=\"relu\"))\n",
        "model.add(layers.MaxPooling2D(pool_size=(2, 2)))\n",
        "model.add(layers.Dropout(0.5))\n",
        "model.add(layers.Flatten())\n",
        "model.add(layers.Dense(num_classes, activation=\"softmax\"))\n",
        "\n",
        "model.summary()"
      ],
      "metadata": {
        "id": "CfKD5iNhqka1"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# компилируем и обучаем модель\n",
        "batch_size = 512\n",
        "epochs = 15\n",
        "model.compile(loss=\"categorical_crossentropy\", optimizer=\"adam\", metrics=[\"accuracy\"])\n",
        "model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)"
      ],
      "metadata": {
        "id": "XTdrbt_Vqmzn"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Пункт 6**"
      ],
      "metadata": {
        "id": "g0NNH4Tfrvap"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Оценка качества работы модели на тестовых данных\n",
        "scores = model.evaluate(X_test, y_test)\n",
        "print('Loss on test data:', scores[0])\n",
        "print('Accuracy on test data:', scores[1])"
      ],
      "metadata": {
        "id": "knUHNHoVqpLs"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Пункт 7**"
      ],
      "metadata": {
        "id": "yNmb5Ot3sKBx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# вывод первого тестового изображения и результата распознавания\n",
        "n = 123\n",
        "result = model.predict(X_test[n:n+1])\n",
        "print('NN output:', result)\n",
        "plt.show()\n",
        "plt.imshow(X_test[n].reshape(28,28), cmap=plt.get_cmap('gray'))\n",
        "print('Real mark: ', np.argmax(y_test[n]))\n",
        "print('NN answer: ', np.argmax(result))"
      ],
      "metadata": {
        "id": "ONaoSInWqtAV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# вывод первого тестового изображения и результата распознавания\n",
        "n = 110\n",
        "result = model.predict(X_test[n:n+1])\n",
        "print('NN output:', result)\n",
        "plt.show()\n",
        "plt.imshow(X_test[n].reshape(28,28), cmap=plt.get_cmap('gray'))\n",
        "print('Real mark: ', np.argmax(y_test[n]))\n",
        "print('NN answer: ', np.argmax(result))"
      ],
      "metadata": {
        "id": "Xem4kHY3qvnH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Пункт 8**"
      ],
      "metadata": {
        "id": "ciRgy9visOnv"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# истинные метки классов\n",
        "true_labels = np.argmax(y_test, axis=1)\n",
        "# предсказанные метки классов\n",
        "predicted_labels = np.argmax(model.predict(X_test), axis=1)\n",
        "\n",
        "# отчет о качестве классификации\n",
        "print(classification_report(true_labels, predicted_labels))\n",
        "# вычисление матрицы ошибок\n",
        "conf_matrix = confusion_matrix(true_labels, predicted_labels)\n",
        "# отрисовка матрицы ошибок в виде \"тепловой карты\"\n",
        "display = ConfusionMatrixDisplay(confusion_matrix=conf_matrix)\n",
        "display.plot()\n",
        "plt.show()"
      ],
      "metadata": {
        "id": "LMippjEhqyAQ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Пункт 9**"
      ],
      "metadata": {
        "id": "Sb3HfMLpsUsT"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# загрузка собственного изображения 1\n",
        "from PIL import Image\n",
        "file_data = Image.open('test.png')\n",
        "file_data = file_data.convert('L') # перевод в градации серого\n",
        "test_img = np.array(file_data)\n",
        "\n",
        "# вывод собственного изображения\n",
        "plt.imshow(test_img, cmap=plt.get_cmap('gray'))\n",
        "plt.show()\n",
        "\n",
        "# предобработка\n",
        "test_img = test_img / 255\n",
        "test_img = np.reshape(test_img, (1,28,28,1))\n",
        "\n",
        "# распознавание\n",
        "result = model.predict(test_img)\n",
        "print('I think it\\'s ', np.argmax(result))"
      ],
      "metadata": {
        "id": "EWSA9wnQq0oH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# загрузка собственного изображения 2\n",
        "from PIL import Image\n",
        "file_data = Image.open('test2.png')\n",
        "file_data = file_data.convert('L') # перевод в градации серого\n",
        "test_img = np.array(file_data)\n",
        "\n",
        "# вывод собственного изображения\n",
        "plt.imshow(test_img, cmap=plt.get_cmap('gray'))\n",
        "plt.show()\n",
        "\n",
        "# предобработка\n",
        "test_img = test_img / 255\n",
        "test_img = np.reshape(test_img, (1,28,28,1))\n",
        "\n",
        "# распознавание\n",
        "result = model.predict(test_img)\n",
        "print('I think it\\'s ', np.argmax(result))"
      ],
      "metadata": {
        "id": "HV_2ipNkq4PI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Пункт 10**"
      ],
      "metadata": {
        "id": "OsnJR4STsaCl"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# путь к сохранённой модели из ЛР1\n",
        "model_fc = keras.models.load_model('/content/drive/MyDrive/Colab Notebooks/best_model/model100.keras')\n",
        "\n",
        "# архитектура модели\n",
        "model_fc.summary()"
      ],
      "metadata": {
        "id": "w575Bu7Yq7W1"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# подготовка тестовых данных для полносвязной модели\n",
        "X_test_fc = X_test.reshape(X_test.shape[0], 28*28)  # (10000, 784)\n",
        "y_test_fc = y_test  # если в ЛР3 ты уже перевёл метки в one-hot\n",
        "\n",
        "# оценка качества, как в п. 6\n",
        "scores = model_fc.evaluate(X_test_fc, y_test_fc, verbose=0)\n",
        "print('Loss on test data (FC model):', scores[0])\n",
        "print('Accuracy on test data (FC model):', scores[1])"
      ],
      "metadata": {
        "id": "83S9Lr-Bq9gD"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}