Изменил(а) на 'labworks/LW1/report.md'

main
ChaginSA 1 месяц назад
Родитель 6e3643d715
Сommit ad93d23fc9

@ -7,7 +7,7 @@ import os
os.chdir('/content/drive/MyDrive/Colab Notebooks') os.chdir('/content/drive/MyDrive/Colab Notebooks')
``` ```
* 1.1 Импорт необходимых модулей. 1.1 Импорт необходимых модулей.
``` ```
from tensorflow import keras from tensorflow import keras
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
@ -25,17 +25,17 @@ from keras.datasets import mnist
``` ```
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
``` ```
* 3.1 Объединение в один набор. 3.1 Объединение в один набор.
``` ```
X = np.concatenate((X_train, X_test)) X = np.concatenate((X_train, X_test))
y = np.concatenate((y_train, y_test)) y = np.concatenate((y_train, y_test))
``` ```
* 3.2 Разбиение по вариантам. (5 бригада -> k=4*5-1) 3.2 Разбиение по вариантам. (5 бригада -> k=4*5-1)
``` ```
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size = 10000,train_size = 60000, random_state = 19) X_train, X_test, y_train, y_test = train_test_split(X, y,test_size = 10000,train_size = 60000, random_state = 19)
``` ```
* 3.3 Вывод размерностей. 3.3 Вывод размерностей.
``` ```
print('Shape of X train:', X_train.shape) print('Shape of X train:', X_train.shape)
print('Shape of y train:', y_train.shape) print('Shape of y train:', y_train.shape)
@ -45,7 +45,7 @@ print('Shape of y train:', y_train.shape)
> Shape of y train: (60000,) > Shape of y train: (60000,)
## 4. Вывод обучающих данных. ## 4. Вывод обучающих данных.
* 4.1 Выведем первые четыре элемента обучающих данных. 4.1 Выведем первые четыре элемента обучающих данных.
``` ```
plt.figure(figsize=(10, 3)) plt.figure(figsize=(10, 3))
for i in range(4): for i in range(4):
@ -60,7 +60,7 @@ plt.show()
![отображение элементов](1.png) ![отображение элементов](1.png)
## 5. Предобработка данных. ## 5. Предобработка данных.
* 5.1 Развернем каждое изображение в вектор. 5.1 Развернем каждое изображение в вектор.
``` ```
num_pixels = X_train.shape[1] * X_train.shape[2] num_pixels = X_train.shape[1] * X_train.shape[2]
X_train = X_train.reshape(X_train.shape[0], num_pixels) / 255 X_train = X_train.reshape(X_train.shape[0], num_pixels) / 255
@ -70,7 +70,7 @@ print('Shape of transformed X train:', X_train.shape)
> Shape of transformed X train: (60000, 784) > Shape of transformed X train: (60000, 784)
* 5.2 Переведем метки в one-hot. 5.2 Переведем метки в one-hot.
``` ```
from keras.utils import to_categorical from keras.utils import to_categorical
@ -89,12 +89,12 @@ from keras.models import Sequential
from keras.layers import Dense from keras.layers import Dense
``` ```
* 6.1. Создаем модель - объявляем ее объектом класса Sequential, добавляем выходной слой. 6.1. Создаем модель - объявляем ее объектом класса Sequential, добавляем выходной слой.
``` ```
model = Sequential() model = Sequential()
model.add(Dense(units=num_classes, activation='softmax')) model.add(Dense(units=num_classes, activation='softmax'))
``` ```
* 6.2. Компилируем модель. 6.2. Компилируем модель.
``` ```
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
print(model.summary()) print(model.summary())
@ -110,12 +110,12 @@ print(model.summary())
>Trainable params: 0 (0.00 B) >Trainable params: 0 (0.00 B)
>Non-trainable params: 0 (0.00 B) >Non-trainable params: 0 (0.00 B)
* 6.3 Обучаем модель. 6.3 Обучаем модель.
``` ```
H = model.fit(X_train, y_train, validation_split=0.1, epochs=50) H = model.fit(X_train, y_train, validation_split=0.1, epochs=50)
``` ```
* 6.4 Выводим график функции ошибки 6.4 Выводим график функции ошибки
``` ```
plt.plot(H.history['loss']) plt.plot(H.history['loss'])
plt.plot(H.history['val_loss']) plt.plot(H.history['val_loss'])
@ -141,7 +141,7 @@ print('Accuracy on test data:', scores[1])
>Accuracy on test data: 0.9225000143051147 >Accuracy on test data: 0.9225000143051147
## 8. Добавление одного скрытого слоя. ## 8. Добавление одного скрытого слоя.
* 8.1 При 100 нейронах в скрытом слое. 8.1 При 100 нейронах в скрытом слое.
``` ```
model100 = Sequential() model100 = Sequential()
model100.add(Dense(units=100,input_dim=num_pixels, activation='sigmoid')) model100.add(Dense(units=100,input_dim=num_pixels, activation='sigmoid'))
@ -164,12 +164,12 @@ print(model100.summary())
>Trainable params: 79,510 (310.59 KB) >Trainable params: 79,510 (310.59 KB)
>Non-trainable params: 0 (0.00 B) >Non-trainable params: 0 (0.00 B)
* 8.2 Обучение модели. 8.2 Обучение модели.
``` ```
H = model100.fit(X_train, y_train, validation_split=0.1, epochs=50) H = model100.fit(X_train, y_train, validation_split=0.1, epochs=50)
``` ```
* 8.3 График функции ошибки. 8.3 График функции ошибки.
``` ```
plt.plot(H.history['loss']) plt.plot(H.history['loss'])
plt.plot(H.history['val_loss']) plt.plot(H.history['val_loss'])
@ -193,7 +193,7 @@ print('Accuracy on test data:', scores[1])
>Loss on test data: 0.19745595753192902 >Loss on test data: 0.19745595753192902
>Accuracy on test data: 0.9442999958992004 >Accuracy on test data: 0.9442999958992004
* 8.4 При 300 нейронах в скрытом слое. 8.4 При 300 нейронах в скрытом слое.
``` ```
model300 = Sequential() model300 = Sequential()
model300.add(Dense(units=300,input_dim=num_pixels, activation='sigmoid')) model300.add(Dense(units=300,input_dim=num_pixels, activation='sigmoid'))
@ -216,12 +216,12 @@ print(model300.summary())
>Trainable params: 238,510 (931.68 KB) >Trainable params: 238,510 (931.68 KB)
>Non-trainable params: 0 (0.00 B) >Non-trainable params: 0 (0.00 B)
* 8.5 Обучение модели. 8.5 Обучение модели.
``` ```
H = model300.fit(X_train, y_train, validation_split=0.1, epochs=50) H = model300.fit(X_train, y_train, validation_split=0.1, epochs=50)
``` ```
* 8.6 Вывод графиков функции ошибки. 8.6 Вывод графиков функции ошибки.
``` ```
plt.plot(H.history['loss']) plt.plot(H.history['loss'])
plt.plot(H.history['val_loss']) plt.plot(H.history['val_loss'])
@ -245,7 +245,7 @@ print('Accuracy on test data:', scores[1])
>Loss on test data: 0.22660093009471893 >Loss on test data: 0.22660093009471893
>Accuracy on test data: 0.9348000288009644 >Accuracy on test data: 0.9348000288009644
* 8.7 При 500 нейронах в скрытом слое. 8.7 При 500 нейронах в скрытом слое.
``` ```
model500 = Sequential() model500 = Sequential()
model500.add(Dense(units=500,input_dim=num_pixels, activation='sigmoid')) model500.add(Dense(units=500,input_dim=num_pixels, activation='sigmoid'))
@ -268,12 +268,12 @@ print(model500.summary())
>Trainable params: 397,510 (1.52 MB) >Trainable params: 397,510 (1.52 MB)
>Non-trainable params: 0 (0.00 B) >Non-trainable params: 0 (0.00 B)
* 8.8 Обучение модели. 8.8 Обучение модели.
``` ```
H = model500.fit(X_train, y_train, validation_split=0.1, epochs=50) H = model500.fit(X_train, y_train, validation_split=0.1, epochs=50)
``` ```
* 8.9 Вывод графиков функции ошибки. 8.9 Вывод графиков функции ошибки.
``` ```
plt.plot(H.history['loss']) plt.plot(H.history['loss'])
plt.plot(H.history['val_loss']) plt.plot(H.history['val_loss'])
@ -302,7 +302,7 @@ print('Accuracy on test data:', scores[1])
Точность тестовых данных: 0.9442999958992004 Точность тестовых данных: 0.9442999958992004
## 9. Добавление второго скрытого слоя. ## 9. Добавление второго скрытого слоя.
* 9.1 При 50 нейронах во втором скрытом слое. 9.1 При 50 нейронах во втором скрытом слое.
``` ```
model10050 = Sequential() model10050 = Sequential()
model10050.add(Dense(units=100,input_dim=num_pixels, activation='sigmoid')) model10050.add(Dense(units=100,input_dim=num_pixels, activation='sigmoid'))
@ -328,12 +328,12 @@ print(model10050.summary())
>Trainable params: 84,060 (328.36 KB) >Trainable params: 84,060 (328.36 KB)
>Non-trainable params: 0 (0.00 B) >Non-trainable params: 0 (0.00 B)
* 9.2 Обучаем модель. 9.2 Обучаем модель.
``` ```
H = model10050.fit(X_train, y_train, validation_split=0.1, epochs=50) H = model10050.fit(X_train, y_train, validation_split=0.1, epochs=50)
``` ```
* 9.3 Выводим график функции ошибки. 9.3 Выводим график функции ошибки.
``` ```
plt.plot(H.history['loss']) plt.plot(H.history['loss'])
plt.plot(H.history['val_loss']) plt.plot(H.history['val_loss'])
@ -357,7 +357,7 @@ print('Accuracy on test data:', scores[1])
>Loss on test data: 0.1993969976902008 >Loss on test data: 0.1993969976902008
>Accuracy on test data: 0.9438999891281128 >Accuracy on test data: 0.9438999891281128
* 9.4 При 100 нейронах во втором скрытом слое. 9.4 При 100 нейронах во втором скрытом слое.
``` ```
model100100 = Sequential() model100100 = Sequential()
model100100.add(Dense(units=100,input_dim=num_pixels, activation='sigmoid')) model100100.add(Dense(units=100,input_dim=num_pixels, activation='sigmoid'))
@ -383,12 +383,12 @@ print(model100100.summary())
>Trainable params: 89,610 (350.04 KB) >Trainable params: 89,610 (350.04 KB)
>Non-trainable params: 0 (0.00 B) >Non-trainable params: 0 (0.00 B)
* 9.5 Обучаем модель. 9.5 Обучаем модель.
``` ```
H = model100100.fit(X_train, y_train, validation_split=0.1, epochs=50) H = model100100.fit(X_train, y_train, validation_split=0.1, epochs=50)
``` ```
* 9.6 Выводим график функции ошибки. 9.6 Выводим график функции ошибки.
``` ```
plt.plot(H.history['loss']) plt.plot(H.history['loss'])
plt.plot(H.history['val_loss']) plt.plot(H.history['val_loss'])
@ -430,7 +430,7 @@ print('Accuracy on test data:', scores[1])
model100.save('/content/drive/MyDrive/Colab Notebooks/best_model/model100.keras') model100.save('/content/drive/MyDrive/Colab Notebooks/best_model/model100.keras')
``` ```
* 11.1 Загрузка лучшей модели с диска. 11.1 Загрузка лучшей модели с диска.
``` ```
from keras.models import load_model from keras.models import load_model
model = load_model('/content/drive/MyDrive/Colab Notebooks/best_model/model100.keras') model = load_model('/content/drive/MyDrive/Colab Notebooks/best_model/model100.keras')
@ -470,7 +470,8 @@ print('NN answer: ', str(np.argmax(result)))
>NN answer: 9 >NN answer: 9
## 13. Тестирование на собственных изображениях. ## 13. Тестирование на собственных изображениях.
* 13.1 Загрузка 1 собственного изображения.
13.1 Загрузка 1 собственного изображения.
``` ```
from PIL import Image from PIL import Image
file_data = Image.open('test.png') file_data = Image.open('test.png')
@ -478,7 +479,7 @@ file_data = file_data.convert('L') # перевод в градации серо
test_img = np.array(file_data) test_img = np.array(file_data)
``` ```
* 13.2 Вывод собственного изображения. 13.2 Вывод собственного изображения.
``` ```
plt.imshow(test_img, cmap=plt.get_cmap('gray')) plt.imshow(test_img, cmap=plt.get_cmap('gray'))
plt.show() plt.show()
@ -486,20 +487,20 @@ plt.show()
![1 изображение](10.png) ![1 изображение](10.png)
* 13.3 Предобработка. 13.3 Предобработка.
``` ```
test_img = test_img / 255 test_img = test_img / 255
test_img = test_img.reshape(1, num_pixels) test_img = test_img.reshape(1, num_pixels)
``` ```
* 13.4 Распознавание. 13.4 Распознавание.
``` ```
result = model.predict(test_img) result = model.predict(test_img)
print('I think it\'s ', np.argmax(result)) print('I think it\'s ', np.argmax(result))
``` ```
>I think it's 5 >I think it's 5
* 13.5 Тест 2 изображения. 13.5 Тест 2 изображения.
``` ```
from PIL import Image from PIL import Image
file2_data = Image.open('test_2.png') file2_data = Image.open('test_2.png')

Загрузка…
Отмена
Сохранить