ChilNik commited on
Commit
a6c0b02
·
1 Parent(s): cc555fe
Files changed (1) hide show
  1. app.py.py +58 -0
app.py.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Untitled3.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1gWAA1NHcuSs1JrZSG9sQIrcozcWOaaZL
8
+ """
9
+
10
+
11
+
12
+ import tensorflow as tf
13
+ from tensorflow.keras.datasets import mnist #Загрузка датасета mnist:
14
+ (x_train, y_train), (x_test, y_test) = mnist.load_data()
15
+
16
+ x_train = x_train / 255
17
+ x_test = x_test / 255
18
+ y_train = tf.keras.utils.to_categorical(y_train, num_classes=10) # Преобразование меток в бинарные векторы
19
+ y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)
20
+
21
+ from tensorflow.keras.models import Sequential
22
+ from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
23
+ model = Sequential()
24
+ # Добавление слоев
25
+ model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
26
+ model.add(MaxPooling2D((2, 2)))
27
+ model.add(Flatten())
28
+ model.add(Dense(64, activation='relu'))
29
+ model.add(Dense(10, activation='softmax'))
30
+ model.summary()
31
+ # Размеры тренировочного, валидационного и тестового датасетов
32
+ train_size = x_train.shape[0]
33
+ val_size = int(train_size * 0.1)
34
+ test_size = x_test.shape[0]
35
+ print("Размер тренировочного датасета:", train_size)
36
+ print("Размер валидационного датасета:", val_size)
37
+ print("Размер тестового датасета:", test_size)
38
+ tf.keras.utils.plot_model(model, show_shapes= True, show_layer_names= True, show_layer_activations= True)
39
+
40
+ model.save('my_model')
41
+
42
+ from google.colab import drive
43
+ drive.mount('/content/drive')
44
+
45
+ model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
46
+ model.fit(x_train, y_train, batch_size=32, epochs=10, validation_data=(x_test, y_test)) # Обучение модели
47
+
48
+ loss, accuracy = model.evaluate(x_test, y_test)
49
+ print(f'Loss: {loss}, Accuracy: {accuracy}') # Оценка модели на тестовых данных
50
+
51
+ import numpy as np
52
+ index = np.random.randint(len(x_test)) # возьмем случайное изображение
53
+ image = x_test[index]
54
+ image = np.expand_dims(image, axis=0)
55
+ prediction = model.predict(image) # найдем метки
56
+ predicted_digit = np.argmax(prediction)
57
+ remainder = predicted_digit % 2 # Вычисление остатка на 2
58
+ print(f'Predicted Digit: {predicted_digit}, Remainder: {remainder}')