{ "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": "S59WEX1lbXWW" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "lhabo1q_VXgc" }, "outputs": [], "source": [ "import os\n", "os.chdir('/content/drive/MyDrive/Colab Notebooks/IS_LR3')" ] }, { "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": "ZYpnLJOCaSFR" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "2 пункт\n" ], "metadata": { "id": "QTplfsEEbWtr" } }, { "cell_type": "code", "source": [ "# загрузка датасета\n", "from keras.datasets import mnist\n", "(X_train, y_train), (X_test, y_test) = mnist.load_data()" ], "metadata": { "id": "FmAqO707aR_5" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "3 пункт\n" ], "metadata": { "id": "VR6XttyDbpGS" } }, { "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 = 19)" ], "metadata": { "id": "idfAHcp9aR32" }, "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": "ZcpI4-Mfb8_M" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "4 пункт" ], "metadata": { "id": "MsUxxLu4dXsF" } }, { "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": "xIB0CdYqdWPv" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "5 пункт" ], "metadata": { "id": "1HQjX5z6dp3h" } }, { "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()\n", "\n" ], "metadata": { "id": "owMPTAvseQFB" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "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": "thNo1LXUepwN" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "6 пункт" ], "metadata": { "id": "8Vvr7f3ng2EI" } }, { "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": "JUg1WDEngza0" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "7 пункт" ], "metadata": { "id": "EYoMHxN_hPlv" } }, { "cell_type": "code", "source": [ "# вывод первого тестового изображения и результата распознавания\n", "n = 222\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": "ozvxCjFFhF0i" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# вывод второго тестового изображения и результата распознавания\n", "n = 111\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": "XrQQWslhjhxA" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "8 пункт" ], "metadata": { "id": "njvWDE6whDUz" } }, { "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": "HuPTHd_YkZ-V" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "9 пункт\n" ], "metadata": { "id": "uNi4E7gPl8rd" } }, { "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": "cQUHadWyl_d4" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# загрузка собственного изображения 2\n", "from PIL import Image\n", "file_data = Image.open('test_2.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": "D-LsJFTpmsCL" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "10 пункт" ], "metadata": { "id": "B7dQZmiTnFjk" } }, { "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()\n", "\n" ], "metadata": { "id": "JAFvXfzHnEyf" }, "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": "iSMKJsCznIKM" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "11 пункт" ], "metadata": { "id": "YE0Ne5Y5pUaZ" } }, { "cell_type": "code", "source": [], "metadata": { "id": "c22hf9CjpT6Z" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "S4SaPgPbnIAp" }, "execution_count": null, "outputs": [] } ] }