diff --git a/labworks/LW1/notebook (1).ipynb b/labworks/LW1/notebook (1).ipynb new file mode 100644 index 0000000..0c81bc3 --- /dev/null +++ b/labworks/LW1/notebook (1).ipynb @@ -0,0 +1 @@ +{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"gpuType":"T4","mount_file_id":"1VmO4hQEAPfDizDdcqCwl1OYwcYnBO7VX","authorship_tag":"ABX9TyNQczjRBGL7zcJBULFsHj9b"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"accelerator":"GPU"},"cells":[{"cell_type":"code","source":["from google.colab import drive\n","drive.mount('/content/drive')"],"metadata":{"id":"3d-xZ2lZy7Rb"},"execution_count":null,"outputs":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sY7hrfuDep2o"},"outputs":[],"source":["import os\n","os.chdir('/content/drive/MyDrive/Colab Notebooks')"]},{"cell_type":"code","source":["from tensorflow import keras\n","import matplotlib.pyplot as plt\n","import numpy as np\n","import sklearn"],"metadata":{"id":"Tacr_fcxzVhL"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["from keras.datasets import mnist"],"metadata":{"id":"El-7TrohzlH4"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["(X_train,y_train),(X_test,y_test)=mnist.load_data()\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,test_size=10000,train_size=60000,random_state=11)\n"],"metadata":{"id":"61fC3tqdzqfp"},"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":"CRqr_1wv0Hv0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#вывод изображения\n","plt.imshow(X_train[1],cmap=plt.get_cmap('gray'))\n","plt.show()\n","print(y_train[1])\n","\n","plt.imshow(X_train[2],cmap=plt.get_cmap('gray'))\n","plt.show()\n","print(y_train[2])\n","\n","plt.imshow(X_train[3],cmap=plt.get_cmap('gray'))\n","plt.show()\n","print(y_train[3])\n","\n","plt.imshow(X_train[4],cmap=plt.get_cmap('gray'))\n","plt.show()\n","print(y_train[4])"],"metadata":{"id":"u5l41yUK0PkK"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#развернем каждое изображение 8*228 в вектор 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":"saLoJeVw0gM9"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#переведем метки в one-hot\n","import keras.utils\n","y_train=keras.utils.to_categorical(y_train)\n","y_test=keras.utils.to_categorical(y_test)\n","print('Shape of transformed y train:',y_train.shape)\n","num_classes=y_train.shape[1]"],"metadata":{"id":"Rqe4rjxW07Pc"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["from keras.models import Sequential\n","from keras.layers import Dense"],"metadata":{"id":"eoxg_hKM2hrk"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["model_1 = Sequential()\n","model_1.add(Dense(units=num_classes, input_dim=num_pixels, activation='softmax'))\n","model_1.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])"],"metadata":{"id":"XUtTRNgq3f_W"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# вывод информации об архитектуре модели\n","print(model_1.summary())"],"metadata":{"id":"P1mTGIIw9icA"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Обучаем модель\n","H = model_1.fit(X_train, y_train, validation_split=0.1, epochs=50)"],"metadata":{"id":"7R-bwn519mT-"},"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":"GRTe79ph9py0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Оценка качества работы модели на тестовых данных\n","scores = model_1.evaluate(X_test, y_test)\n","print('Loss on test data:', scores[0])\n","print('Accuracy on test data:', scores[1])"],"metadata":{"id":"UIYWFwAV_bdy"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["model_2 = Sequential()\n","model_2.add(Dense(units=100, input_dim=num_pixels, activation='sigmoid'))\n","model_2.add(Dense(units=num_classes, activation='softmax'))\n","model_2.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])\n","\n","print(model_2.summary())\n","\n","H_2 = model_2.fit(X_train, y_train, validation_split=0.1, epochs=50)\n","\n","# вывод графика ошибки по эпохам\n","plt.plot(H_2.history['loss'])\n","plt.plot(H_2.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()\n","\n","# Оценка качества работы модели на тестовых данных\n","scores = model_2.evaluate(X_test, y_test)\n","print('Loss on test data:', scores[0])\n","print('Accuracy on test data:', scores[1])"],"metadata":{"id":"3s9ndeb6AbQa"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["model_3 = Sequential()\n","model_3.add(Dense(units=300, input_dim=num_pixels, activation='sigmoid'))\n","model_3.add(Dense(units=num_classes, activation='softmax'))\n","model_3.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])\n","\n","print(model_3.summary())\n","\n","H_3 = model_3.fit(X_train, y_train, validation_split=0.1, epochs=50)\n","\n","# вывод графика ошибки по эпохам\n","plt.plot(H_3.history['loss'])\n","plt.plot(H_3.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()\n","\n","# Оценка качества работы модели на тестовых данных\n","scores = model_3.evaluate(X_test, y_test)\n","print('Loss on test data:', scores[0])\n","print('Accuracy on test data:', scores[1])"],"metadata":{"id":"mS7oysm8DKOp"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["model_4 = Sequential()\n","model_4.add(Dense(units=500, input_dim=num_pixels, activation='sigmoid'))\n","model_4.add(Dense(units=num_classes, activation='softmax'))\n","model_4.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])\n","\n","print(model_4.summary())\n","\n","H_4 = model_4.fit(X_train, y_train, validation_split=0.1, epochs=50)\n","\n","# вывод графика ошибки по эпохам\n","plt.plot(H_4.history['loss'])\n","plt.plot(H_4.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()\n","\n","# Оценка качества работы модели на тестовых данных\n","scores = model_4.evaluate(X_test, y_test)\n","print('Loss on test data:', scores[0])\n","print('Accuracy on test data:', scores[1])"],"metadata":{"id":"7A7oVRCpKUJS"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["model_5 = Sequential()\n","model_5.add(Dense(units=100, input_dim=num_pixels, activation='sigmoid'))\n","model_5.add(Dense(units=50, activation='sigmoid'))\n","model_5.add(Dense(units=num_classes, activation='softmax'))\n","model_5.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])\n","\n","print(model_5.summary())\n","\n","H_5 = model_5.fit(X_train, y_train, validation_split=0.1, epochs=50)\n","\n","# вывод графика ошибки по эпохам\n","plt.plot(H_5.history['loss'])\n","plt.plot(H_5.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()\n","\n","# Оценка качества работы модели на тестовых данных\n","scores = model_5.evaluate(X_test, y_test)\n","print('Loss on test data:', scores[0])\n","print('Accuracy on test data:', scores[1])"],"metadata":{"id":"M9Lb-RmTKd8k"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["model_5 = Sequential()\n","model_5.add(Dense(units=100, input_dim=num_pixels, activation='sigmoid'))\n","model_5.add(Dense(units=100, activation='sigmoid'))\n","model_5.add(Dense(units=num_classes, activation='softmax'))\n","model_5.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])\n","\n","print(model_5.summary())\n","\n","H_5 = model_5.fit(X_train, y_train, validation_split=0.1, epochs=50)\n","\n","# вывод графика ошибки по эпохам\n","plt.plot(H_5.history['loss'])\n","plt.plot(H_5.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()\n","\n","# Оценка качества работы модели на тестовых данных\n","scores = model_5.evaluate(X_test, y_test)\n","print('Loss on test data:', scores[0])\n","print('Accuracy on test data:', scores[1])"],"metadata":{"id":"F64kmzaCO5so"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["model_5.save('best_model.keras')"],"metadata":{"id":"B0GTrE3OSZyv"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# вывод тестового изображения и результата распознавания 1\n","n = 123\n","result = model_5.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)))\n"],"metadata":{"id":"LcMyQ0EoSz-e"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# вывод тестового изображения и результата распознавания 2\n","n = 111\n","result = model_5.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)))\n"],"metadata":{"id":"8xnFNh_fWkpW"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# загрузка собственного изображения\n","from PIL import Image\n","file_data = Image.open('5.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","test_img = test_img / 255\n","test_img = test_img.reshape(1, num_pixels)\n","# распознавание\n","result = model_5.predict(test_img)\n","print('I think it\\'s ', np.argmax(result))"],"metadata":{"id":"vLHbBjHFWqwp"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["file_data = Image.open('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","test_img = test_img / 255\n","test_img = test_img.reshape(1, num_pixels)\n","# распознавание\n","result = model_5.predict(test_img)\n","print('I think it\\'s ', np.argmax(result))"],"metadata":{"id":"2YSqzMkvdSDb"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["file_data = Image.open('2_1.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","test_img = test_img / 255\n","test_img = test_img.reshape(1, num_pixels)\n","# распознавание\n","result = model_5.predict(test_img)\n","print('I think it\\'s ', np.argmax(result))"],"metadata":{"id":"24nABeDGlVJ0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["file_data = Image.open('5_1.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","test_img = test_img / 255\n","test_img = test_img.reshape(1, num_pixels)\n","# распознавание\n","result = model_5.predict(test_img)\n","print('I think it\\'s ', np.argmax(result))"],"metadata":{"id":"24fY8TDOmLd5"},"execution_count":null,"outputs":[]}]} \ No newline at end of file