форкнуто от main/is_dnn
Родитель
ec7819bac7
Сommit
7177c7a232
@ -0,0 +1,868 @@
|
||||
{
|
||||
"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": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "CAumUvAGaImn"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"os.chdir('/content/drive/MyDrive/Colab Notebooks')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# импорт модулей\n",
|
||||
"from tensorflow import keras\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import numpy as np\n",
|
||||
"import sklearn"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "h5MSWSsQamWR"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# загрузка датасета\n",
|
||||
"from keras.datasets import mnist\n",
|
||||
"(X_train, y_train), (X_test, y_test) = mnist.load_data()"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "95AfnWl1aq9X"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# создание своего разбиения датасета\n",
|
||||
"from sklearn.model_selection import train_test_split\n",
|
||||
"# объединяем в один набор\n",
|
||||
"X = np.concatenate((X_train, X_test))\n",
|
||||
"y = np.concatenate((y_train, y_test))\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": "F2Fe8Fa6av1X"
|
||||
},
|
||||
"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)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "w5R3s-subD5z"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Вывод 4 изображений\n",
|
||||
"plt.figure(figsize=(10, 3))\n",
|
||||
"for i in range(4):\n",
|
||||
" plt.subplot(1, 4, i + 1)\n",
|
||||
" plt.imshow(X_train[i], cmap='gray')\n",
|
||||
" plt.title(f'Label: {y_train[i]}')\n",
|
||||
" plt.axis('off')\n",
|
||||
"plt.tight_layout()\n",
|
||||
"plt.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "YmYWjSeDbKFg"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# развернем каждое изображение 28*28 в вектор 784\n",
|
||||
"num_pixels = X_train.shape[1] * X_train.shape[2]\n",
|
||||
"X_train = X_train.reshape(X_train.shape[0], num_pixels) / 255\n",
|
||||
"X_test = X_test.reshape(X_test.shape[0], num_pixels) / 255\n",
|
||||
"print('Shape of transformed X train:', X_train.shape)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "NGKvRZ8fbypE"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# переведем метки в one-hot\n",
|
||||
"from keras.utils import to_categorical\n",
|
||||
"\n",
|
||||
"y_train = to_categorical(y_train)\n",
|
||||
"y_test = to_categorical(y_test)\n",
|
||||
"\n",
|
||||
"print('Shape of transformed y train:', y_train.shape)\n",
|
||||
"num_classes = y_train.shape[1]"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "dKZDth4wdMoi"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"from keras.models import Sequential\n",
|
||||
"from keras.layers import Dense"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "HdlasD8UdSFr"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# 1. создаем модель - объявляем ее объектом класса Sequential\n",
|
||||
"model = Sequential()\n",
|
||||
"# 2. добавляем выходной слой(скрытые слои отсутствуют)\n",
|
||||
"model.add(Dense(units=num_classes, activation='softmax'))\n",
|
||||
"# 3. компилируем модель\n",
|
||||
"model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "f7EFobe4dTjU"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод информации об архитектуре модели\n",
|
||||
"print(model.summary())"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "Fr_Lnir_eTUS"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# обучение модели\n",
|
||||
"H = model.fit(X_train, y_train, validation_split=0.1, epochs=50)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "P4jek-2sedhi"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод графика ошибки по эпохам\n",
|
||||
"plt.plot(H.history['loss'])\n",
|
||||
"plt.plot(H.history['val_loss'])\n",
|
||||
"plt.grid()\n",
|
||||
"plt.xlabel('Epochs')\n",
|
||||
"plt.ylabel('loss')\n",
|
||||
"plt.legend(['train_loss', 'val_loss'])\n",
|
||||
"plt.title('Loss by epochs')\n",
|
||||
"plt.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "JUeBjeS0ffg2"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"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": "9h5aG6MtfnjN"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# сохранение модели на диск\n",
|
||||
"model.save('/content/drive/MyDrive/Colab Notebooks/models/model_zero_hide.keras')"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "31ngORxnfsJb"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"model100 = Sequential()\n",
|
||||
"model100.add(Dense(units=100,input_dim=num_pixels, activation='sigmoid'))\n",
|
||||
"model100.add(Dense(units=num_classes, activation='softmax'))\n",
|
||||
"\n",
|
||||
"model100.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "GuUp0o_nf_Oq"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод информации об архитектуре модели\n",
|
||||
"print(model100.summary())"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "1RJG5PfSgSdz"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Обучаем модель\n",
|
||||
"H = model100.fit(X_train, y_train, validation_split=0.1, epochs=50)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "Ofd6o3nzgc8D"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод графика ошибки по эпохам\n",
|
||||
"plt.plot(H.history['loss'])\n",
|
||||
"plt.plot(H.history['val_loss'])\n",
|
||||
"plt.grid()\n",
|
||||
"plt.xlabel('Epochs')\n",
|
||||
"plt.ylabel('loss')\n",
|
||||
"plt.legend(['train_loss', 'val_loss'])\n",
|
||||
"plt.title('Loss by epochs')\n",
|
||||
"plt.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "On3RA9ZghcLj"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Оценка качества работы модели на тестовых данных\n",
|
||||
"scores = model100.evaluate(X_test, y_test)\n",
|
||||
"print('Loss on test data:', scores[0])\n",
|
||||
"print('Accuracy on test data:', scores[1])"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "d-2h4TVuhemj"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# сохранение модели на диск\n",
|
||||
"model100.save('/content/drive/MyDrive/Colab Notebooks/models/model100in_1hide.keras')"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "1mvHa_c8hjJx"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"model300 = Sequential()\n",
|
||||
"model300.add(Dense(units=300,input_dim=num_pixels, activation='sigmoid'))\n",
|
||||
"model300.add(Dense(units=num_classes, activation='softmax'))\n",
|
||||
"\n",
|
||||
"model300.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "WO3ZHI6xhlVt"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод информации об архитектуре модели\n",
|
||||
"print(model300.summary())"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "BqRtNfophpf3"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Обучаем модель\n",
|
||||
"H = model300.fit(X_train, y_train, validation_split=0.1, epochs=50)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "YrP4IANqhwjf"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод графика ошибки по эпохам\n",
|
||||
"plt.plot(H.history['loss'])\n",
|
||||
"plt.plot(H.history['val_loss'])\n",
|
||||
"plt.grid()\n",
|
||||
"plt.xlabel('Epochs')\n",
|
||||
"plt.ylabel('loss')\n",
|
||||
"plt.legend(['train_loss', 'val_loss'])\n",
|
||||
"plt.title('Loss by epochs')\n",
|
||||
"plt.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "M7D5NYCSiqzI"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Оценка качества работы модели на тестовых данных\n",
|
||||
"scores = model300.evaluate(X_test, y_test)\n",
|
||||
"print('Loss on test data:', scores[0])\n",
|
||||
"print('Accuracy on test data:', scores[1])"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "5dBUsxjVivJU"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# сохранение модели на диск\n",
|
||||
"model300.save('/content/drive/MyDrive/Colab Notebooks/models/model300in_1hide.keras')"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "0GB5tz5eizCo"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"model500 = Sequential()\n",
|
||||
"model500.add(Dense(units=500,input_dim=num_pixels, activation='sigmoid'))\n",
|
||||
"model500.add(Dense(units=num_classes, activation='softmax'))\n",
|
||||
"\n",
|
||||
"model500.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "9FlJqDcci26k"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод информации об архитектуре модели\n",
|
||||
"print(model500.summary())"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "TbPS-5fKi9mZ"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Обучаем модель\n",
|
||||
"H = model500.fit(X_train, y_train, validation_split=0.1, epochs=50)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "rODU_cugjBOX"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод графика ошибки по эпохам\n",
|
||||
"plt.plot(H.history['loss'])\n",
|
||||
"plt.plot(H.history['val_loss'])\n",
|
||||
"plt.grid()\n",
|
||||
"plt.xlabel('Epochs')\n",
|
||||
"plt.ylabel('loss')\n",
|
||||
"plt.legend(['train_loss', 'val_loss'])\n",
|
||||
"plt.title('Loss by epochs')\n",
|
||||
"plt.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "7uCJOOJGkTCc"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Оценка качества работы модели на тестовых данных\n",
|
||||
"scores = model500.evaluate(X_test, y_test)\n",
|
||||
"print('Loss on test data:', scores[0])\n",
|
||||
"print('Accuracy on test data:', scores[1])"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "H5BhhLZrkWFq"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# сохранение модели на диск\n",
|
||||
"model500.save('/content/drive/MyDrive/Colab Notebooks/models/model500in_1hide.keras')"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "Uyv2pf5FkYjc"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"model10050 = Sequential()\n",
|
||||
"model10050.add(Dense(units=100,input_dim=num_pixels, activation='sigmoid'))\n",
|
||||
"model10050.add(Dense(units=50,activation='sigmoid'))\n",
|
||||
"model10050.add(Dense(units=num_classes, activation='softmax'))\n",
|
||||
"\n",
|
||||
"model10050.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "0X6rM1m6klas"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод информации об архитектуре модели\n",
|
||||
"print(model10050.summary())"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "CJRW6vaKkm9o"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Обучаем модель\n",
|
||||
"H = model10050.fit(X_train, y_train, validation_split=0.1, epochs=50)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "wWbPA8j4k18a"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод графика ошибки по эпохам\n",
|
||||
"plt.plot(H.history['loss'])\n",
|
||||
"plt.plot(H.history['val_loss'])\n",
|
||||
"plt.grid()\n",
|
||||
"plt.xlabel('Epochs')\n",
|
||||
"plt.ylabel('loss')\n",
|
||||
"plt.legend(['train_loss', 'val_loss'])\n",
|
||||
"plt.title('Loss by epochs')\n",
|
||||
"plt.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "BnxtXX1kl33n"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Оценка качества работы модели на тестовых данных\n",
|
||||
"scores = model10050.evaluate(X_test, y_test)\n",
|
||||
"print('Loss on test data:', scores[0])\n",
|
||||
"print('Accuracy on test data:', scores[1])"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "c97Qx3pul98e"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# сохранение модели на диск\n",
|
||||
"model10050.save('/content/drive/MyDrive/Colab Notebooks/models/model100in_1hide_50in_2hide.keras')"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "Dn5qMhDAmBlZ"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"model100100 = Sequential()\n",
|
||||
"model100100.add(Dense(units=100,input_dim=num_pixels, activation='sigmoid'))\n",
|
||||
"model100100.add(Dense(units=100,activation='sigmoid'))\n",
|
||||
"model100100.add(Dense(units=num_classes, activation='softmax'))\n",
|
||||
"\n",
|
||||
"model100100.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "YIfzGZVzmCqT"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод информации об архитектуре модели\n",
|
||||
"print(model100100.summary())"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "aK8ffWILmIDg"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Обучаем модель\n",
|
||||
"H = model100100.fit(X_train, y_train, validation_split=0.1, epochs=50)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "Dz7X9T55mLCh"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод графика ошибки по эпохам\n",
|
||||
"plt.plot(H.history['loss'])\n",
|
||||
"plt.plot(H.history['val_loss'])\n",
|
||||
"plt.grid()\n",
|
||||
"plt.xlabel('Epochs')\n",
|
||||
"plt.ylabel('loss')\n",
|
||||
"plt.legend(['train_loss', 'val_loss'])\n",
|
||||
"plt.title('Loss by epochs')\n",
|
||||
"plt.show()"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "eF7B4wucnIPS"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Оценка качества работы модели на тестовых данных\n",
|
||||
"scores = model100100.evaluate(X_test, y_test)\n",
|
||||
"print('Loss on test data:', scores[0])\n",
|
||||
"print('Accuracy on test data:', scores[1])"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "yxdjaq6bnNXt"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# сохранение модели на диск\n",
|
||||
"model100100.save('/content/drive/MyDrive/Colab Notebooks/models/model100in_1hide_100in_2hide.keras')"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "Sr9bCq_KnP85"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# сохранение лучшей модели в папку best_model\n",
|
||||
"model100.save('/content/drive/MyDrive/Colab Notebooks/best_model/model100.keras')"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "BV7wEu2SoMaB"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# Загрузка модели с диска\n",
|
||||
"from keras.models import load_model\n",
|
||||
"model = load_model('/content/drive/MyDrive/Colab Notebooks/best_model/model100.keras')"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "hg2PYRgwoTiU"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод тестового изображения и результата распознавания\n",
|
||||
"n = 222\n",
|
||||
"result = model.predict(X_test[n:n+1])\n",
|
||||
"print('NN output:', result)\n",
|
||||
"plt.imshow(X_test[n].reshape(28,28), cmap=plt.get_cmap('gray'))\n",
|
||||
"plt.show()\n",
|
||||
"print('Real mark: ', str(np.argmax(y_test[n])))\n",
|
||||
"print('NN answer: ', str(np.argmax(result)))"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "A8O5K-_4oeK9"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод тестового изображения и результата распознавания\n",
|
||||
"n = 123\n",
|
||||
"result = model.predict(X_test[n:n+1])\n",
|
||||
"print('NN output:', result)\n",
|
||||
"plt.imshow(X_test[n].reshape(28,28), cmap=plt.get_cmap('gray'))\n",
|
||||
"plt.show()\n",
|
||||
"print('Real mark: ', str(np.argmax(y_test[n])))\n",
|
||||
"print('NN answer: ', str(np.argmax(result)))"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "pk03l3jdpUp5"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# загрузка собственного изображения\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)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "PkjvyImOpii6"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод собственного изображения\n",
|
||||
"plt.imshow(test_img, cmap=plt.get_cmap('gray'))\n",
|
||||
"plt.show()\n",
|
||||
"# предобработка\n",
|
||||
"test_img = test_img / 255\n",
|
||||
"test_img = test_img.reshape(1, num_pixels)\n",
|
||||
"# распознавание\n",
|
||||
"result = model.predict(test_img)\n",
|
||||
"print('I think it\\'s ', np.argmax(result))"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "wcbVyWwusUx6"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# загрузка собственного изображения\n",
|
||||
"from PIL import Image\n",
|
||||
"file2_data = Image.open('test2.png')\n",
|
||||
"file2_data = file2_data.convert('L') # перевод в градации серого\n",
|
||||
"test2_img = np.array(file2_data)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "JY7tkymctESN"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод собственного изображения\n",
|
||||
"plt.imshow(test2_img, cmap=plt.get_cmap('gray'))\n",
|
||||
"plt.show()\n",
|
||||
"# предобработка\n",
|
||||
"test2_img = test2_img / 255\n",
|
||||
"test2_img = test2_img.reshape(1, num_pixels)\n",
|
||||
"# распознавание\n",
|
||||
"result_2 = model.predict(test2_img)\n",
|
||||
"print('I think it\\'s ', np.argmax(result_2))"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "saUm4dytutDS"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# загрузка собственного изображения, повернутого на 90 градусов\n",
|
||||
"from PIL import Image\n",
|
||||
"file90_data = Image.open('test90.png')\n",
|
||||
"file90_data = file90_data.convert('L') # перевод в градации серого\n",
|
||||
"test90_img = np.array(file90_data)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "3DV_1KeKvo3S"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод собственного изображения\n",
|
||||
"plt.imshow(test90_img, cmap=plt.get_cmap('gray'))\n",
|
||||
"plt.show()\n",
|
||||
"# предобработка\n",
|
||||
"test90_img = test90_img / 255\n",
|
||||
"test90_img = test90_img.reshape(1, num_pixels)\n",
|
||||
"# распознавание\n",
|
||||
"result_3 = model.predict(test90_img)\n",
|
||||
"print('I think it\\'s ', np.argmax(result_3))"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "uBXsSP-iweMO"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# загрузка собственного изображения, повернутого на 90 градусов\n",
|
||||
"from PIL import Image\n",
|
||||
"file902_data = Image.open('test90_2.png')\n",
|
||||
"file902_data = file902_data.convert('L') # перевод в градации серого\n",
|
||||
"test902_img = np.array(file902_data)"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "s9FSbb99wh_9"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"source": [
|
||||
"# вывод собственного изображения\n",
|
||||
"plt.imshow(test902_img, cmap=plt.get_cmap('gray'))\n",
|
||||
"plt.show()\n",
|
||||
"# предобработка\n",
|
||||
"test902_img = test902_img / 255\n",
|
||||
"test902_img = test902_img.reshape(1, num_pixels)\n",
|
||||
"# распознавание\n",
|
||||
"result_4 = model.predict(test902_img)\n",
|
||||
"print('I think it\\'s ', np.argmax(result_4))"
|
||||
],
|
||||
"metadata": {
|
||||
"id": "ppK14r4-w0Av"
|
||||
},
|
||||
"execution_count": null,
|
||||
"outputs": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"source": [],
|
||||
"metadata": {
|
||||
"id": "ZaKbfAx8xaud"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Загрузка…
Ссылка в новой задаче