diff --git a/8382/Ershov/lb/4/8.png b/8382/Ershov/lb/4/8.png new file mode 100644 index 00000000..4beea7f7 Binary files /dev/null and b/8382/Ershov/lb/4/8.png differ diff --git a/8382/Ershov/lb/4/main.py b/8382/Ershov/lb/4/main.py new file mode 100644 index 00000000..6a985268 --- /dev/null +++ b/8382/Ershov/lb/4/main.py @@ -0,0 +1,59 @@ +import tensorflow as tf +import matplotlib.pyplot as plt +from tensorflow.keras.utils import to_categorical +from tensorflow.keras.layers import Dense, Flatten +from tensorflow.keras.models import Sequential +import numpy as np +from PIL import Image +from tensorflow.keras import optimizers +from tensorflow.keras.preprocessing.image import load_img +from tensorflow.keras.preprocessing.image import img_to_array + + +mnist = tf.keras.datasets.mnist + +(train_images, train_labels), (test_images, test_labels) = mnist.load_data() + +train_images = train_images / 255.0 +test_images = test_images / 255.0 +train_labels = to_categorical(train_labels) +test_labels = to_categorical(test_labels) + +def build_model(): + model = Sequential() + model.add(Flatten()) + model.add(Dense(256, activation='relu')) + model.add(Dense(10, activation='softmax')) + + return model + +def get_image(filename): + img = load_img(filename, color_mode='grayscale', target_size=(28, 28)) + img = img_to_array(img) + img = img.reshape(1, 28, 28, 1) + img = img.astype('float32') / 255.0 + return img + +model = build_model() +model.compile(optimizer=optimizers.Adam(learning_rate=0.1),loss='categorical_crossentropy', metrics=['accuracy']) +history = model.fit(train_images, train_labels, epochs=5, batch_size=128, validation_data=(test_images, test_labels)) + +plt.title('Training and test accuracy Adam 0.1') +plt.plot(history.history['accuracy'], 'r', label='Training acc') +plt.plot(history.history['val_accuracy'], 'b', label='Validation acc') +plt.xlabel('Epochs') +plt.ylabel('Accuracy') +plt.legend() +#plt.show() + +plt.plot(history.history['loss'], 'r', label='Training loss') +plt.plot(history.history['val_loss'], 'b', label='Validation loss') +plt.title('Training and validation accuracy Adam 0.1') +plt.xlabel('Epochs') +plt.ylabel('Loss') +plt.legend() +#plt.show() + +img = get_image("8.png") +digit = np.argmax(model.predict(img), axis=-1) +print(digit[0]) \ No newline at end of file diff --git a/8382/Ershov/lb/4/report.pdf b/8382/Ershov/lb/4/report.pdf new file mode 100644 index 00000000..b8b45195 Binary files /dev/null and b/8382/Ershov/lb/4/report.pdf differ