{"id":322012,"date":"2021-04-24T15:00:13","date_gmt":"2021-04-24T15:00:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=322012"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=322012","title":{"rendered":"\u0420\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u0435 \u0434\u043e\u0440\u043e\u0436\u043d\u044b\u0445 \u0437\u043d\u0430\u043a\u043e\u0432"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0435 \u0437\u0440\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u044b\u0445 \u043d\u0430\u0443\u043a, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0444\u043e\u043a\u0443\u0441\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0438 \u0447\u0430\u0441\u0442\u0435\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0437\u0440\u0435\u043d\u0438\u044f \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430\u043c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u0445 \u0438 \u0432\u0438\u0434\u0435\u043e, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043b\u044e\u0434\u0435\u0439. \u0420\u0430\u043d\u043d\u0438\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f \u043d\u0430\u0447\u0430\u043b\u0438\u0441\u044c \u0432 1950-\u0445 \u0433\u043e\u0434\u0430\u0445 \u0438 \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0431\u044b\u043b\u0438 \u043a\u043e\u043c\u043c\u0435\u0440\u0447\u0435\u0441\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u044b \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u0435\u0447\u0430\u0442\u043d\u043e\u0433\u043e \u0438 \u0440\u0443\u043a\u043e\u043f\u0438\u0441\u043d\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 1970-\u0445 \u0433\u043e\u0434\u0430\u0445. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f \u0432\u044b\u0440\u043e\u0441\u043b\u0438 \u0432 \u0433\u0435\u043e\u043c\u0435\u0442\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0438\u0438. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u044c \u0434\u043e\u0440\u043e\u0436\u043d\u044b\u0435 \u0437\u043d\u0430\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f.  <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/30a\/7da\/15d\/30a7da15d96e7f8ecce4fcb851d3625d.jpg\" width=\"1920\" height=\"1281\"><figcaption><\/figcaption><\/figure>\n<h2>\u041d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u0440\u043e\u0436\u043d\u044b\u0445 \u0437\u043d\u0430\u043a\u043e\u0432<\/h2>\n<p>\u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0431\u0449\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439 \u0432&nbsp;<a href=\"https:\/\/www.kaggle.com\/\" rel=\"noopener noreferrer nofollow\">Kaggle<\/a>:&nbsp;<a href=\"https:\/\/www.kaggle.com\/meowmeowmeowmeowmeow\/gtsrb-german-traffic-sign\" rel=\"noopener noreferrer nofollow\">GTSRB<\/a>&nbsp;\u2014\u200a \u044d\u0442\u043e \u043c\u0443\u043b\u044c\u0442\u0438\u043a\u043b\u0430\u0441\u0441\u043e\u0432\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u043d\u0430 \u041c\u0435\u0436\u0434\u0443\u043d\u0430\u0440\u043e\u0434\u043d\u043e\u0439 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0439 \u043a\u043e\u043d\u0444\u0435\u0440\u0435\u043d\u0446\u0438\u0438 \u043f\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u043c \u0441\u0435\u0442\u044f\u043c (IJCNN) 2011. \u041d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0431\u043e\u043b\u0435\u0435 50 000 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0434\u043e\u0440\u043e\u0436\u043d\u044b\u0445 \u0437\u043d\u0430\u043a\u043e\u0432 \u0438 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0430 43 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0430. \u041e\u043d \u0432\u0435\u0441\u044c\u043c\u0430 \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u0435\u043d: \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u043c\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b &#8212; \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439.<\/p>\n<h2>\u0418\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/h2>\n<p>\u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438.<\/p>\n<pre><code class=\"python\">import os import matplotlib import numpy as np from PIL import Image from tensorflow.keras.preprocessing.image import img_to_array from sklearn.model_selection import train_test_split from keras.utils import to_categorical from keras.models import Sequential, load_model from keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout from tensorflow.keras import backend as K import matplotlib.pyplot as plt from sklearn.metrics import accuracy_score<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 \u00ab<em>train<\/em>\u00bb, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 43 \u043f\u0430\u043f\u043a\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432. \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0434\u0432\u0430 \u0441\u043f\u0438\u0441\u043a\u0430:&nbsp;<em>data<\/em>&nbsp;\u0438 labels. \u042d\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u043d\u0435\u0441\u0442\u0438 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0437\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0438\u0445 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c, \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u043c\u0435\u0442\u043a\u0430\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432. <\/p>\n<pre><code class=\"python\">data = [] labels = []<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0443\u043b\u044f os \u043c\u044b \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438 \u0438\u0445 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043a\u0438 \u0432 \u0441\u043f\u0438\u0441\u043e\u043a&nbsp;<em>data<\/em>&nbsp;\u0438&nbsp;<em>labels<\/em>. \u0414\u043b\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430&nbsp;<em>PIL<\/em>. <\/p>\n<pre><code class=\"python\">for num in range(0, classes):     path = os.path.join('train',str(num))     imagePaths = os.listdir(path)     for img in imagePaths:       image = Image.open(path + '\/'+ img)       image = image.resize((30,30))       image = img_to_array(image)       data.append(image)       labels.append(num)<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u0446\u0438\u043a\u043b \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043e \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 30\u00d730 \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0432\u0441\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438 \u0438\u0445 \u043c\u0435\u0442\u043a\u0438 \u0432 \u0441\u043f\u0438\u0441\u043a\u0430\u0445&nbsp;<em>data<\/em>&nbsp;\u0438&nbsp;<em>labels<\/em>.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u044b&nbsp;<em>numpy<\/em>&nbsp;\u0434\u043b\u044f \u043f\u043e\u0434\u0430\u0447\u0438 \u0432 \u043c\u043e\u0434\u0435\u043b\u044c.  <\/p>\n<pre><code class=\"python\">data = np.array(data) labels = np.array(labels)<\/code><\/pre>\n<p>\u0424\u043e\u0440\u043c\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 &#8212; (39209, 30, 30, 3), \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0438\u043c\u0435\u0435\u0442\u0441\u044f 39 209 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c 30\u00d730 \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439, \u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 3 \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0446\u0432\u0435\u0442\u043d\u044b\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f (\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 RGB).  <\/p>\n<pre><code class=\"python\">print(data.shape, labels.shape) (39209, 30, 30, 3) (39209,)<\/code><\/pre>\n<p>\u0418\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 <code>sklearn<\/code> \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 <code>train_test_split()<\/code> \u0434\u043b\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f 80% \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0438 20% \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u042d\u0442\u043e \u0442\u0438\u043f\u0438\u0447\u043d\u043e\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043c\u0430 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"python\">X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42) print(X_train.shape, X_test.shape, y_train.shape, y_test.shape) (31367, 30, 30, 3) (7842, 30, 30, 3) (31367,) (7842,) <\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0438 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0432 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0435\u043c \u043d\u0430\u0431\u043e\u0440\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0443 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432.<\/p>\n<pre><code class=\"python\">def cnt_img_in_classes(labels):     count = {}     for i in labels:         if i in count:             count[i] += 1         else:             count[i] = 1     return count  samples_distribution = cnt_img_in_classes (y_train)  def diagram(count_classes):     plt.bar(range(len(dct)), sorted(list(count_classes.values())), align='center')     plt.xticks(range(len(dct)), sorted(list(count_classes.keys())), rotation=90, fontsize=7)     plt.show() diagram(samples_distribution)<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fab\/329\/894\/fab3298944b028257f49a2046349ac68.png\" alt=\"\u0414\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f\" title=\"\u0414\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f\" width=\"381\" height=\"247\"><figcaption>\u0414\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f<\/figcaption><\/figure>\n<p>\u0418\u0437 \u0433\u0440\u0430\u0444\u0438\u043a\u0430 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0438\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d, \u043d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u0441 \u044d\u0442\u0438\u043c \u0444\u0430\u043a\u0442\u043e\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0442\u043e\u0434 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"python\">def aug_images(images, p):     from imgaug import augmenters as iaa     augs =  iaa.SomeOf((2, 4),           [               iaa.Crop(px=(0, 4)),                iaa.Affine(scale={\"x\": (0.8, 1.2), \"y\": (0.8, 1.2)}),               iaa.Affine(translate_percent={\"x\": (-0.2, 0.2), \"y\": (-0.2, 0.2)}),               iaa.Affine(rotate=(-45, 45))               iaa.Affine(shear=(-10, 10)) ])          seq = iaa.Sequential([iaa.Sometimes(p, augs)])     res = seq.augment_images(images)     return res  def augmentation(images, labels):     min_imgs = 500     classes = cnt_img_in_classes(labels)     for i in range(len(classes)):         if (classes[i] &lt; min_imgs):             add_num = min_imgs - classes[i]             imgs_for_augm = []             lbls_for_augm = []             for j in range(add_num):                 im_index = random.choice(np.where(labels == i)[0])                 imgs_for_augm.append(images[im_index])                 lbls_for_augm.append(labels[im_index])             augmented_class = augment_imgs(imgs_for_augm, 1)             augmented_class_np = np.array(augmented_class)             augmented_lbls_np = np.array(lbls_for_augm)             imgs = np.concatenate((images, augmented_class_np), axis=0)             lbls = np.concatenate((labels, augmented_lbls_np), axis=0)     return (images, labels) X_train, y_train = augmentation(X_train, y_train)<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u0448 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0438\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0444\u043e\u0440\u043c\u0443.<\/p>\n<pre><code class=\"python\">print(X_train.shape, X_test.shape, y_train.shape, y_test.shape) (36256, 30, 30, 3) (7842, 30, 30, 3) (36256,) (7842,)<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0449\u0435 \u0440\u0430\u0437 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"python\">augmented_samples_distribution = cnt_img_in_classes(y_train) diagram(augmented_samples_distribution)<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/71c\/e48\/3d9\/71ce483d9b6a6aae14b10ce4c59c578a.png\" alt=\"\u0414\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435 \u0430\u0443\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438\" title=\"\u0414\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435 \u0430\u0443\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438\" width=\"381\" height=\"247\"><figcaption>\u0414\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435 \u0430\u0443\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/figcaption><\/figure>\n<p>\u041d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0430 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043d\u0430\u0448 \u043d\u0430\u0431\u043e\u0440 \u0441\u0442\u0430\u043b \u0431\u043e\u043b\u0435\u0435 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d. \u0414\u0430\u043b\u0435\u0435 \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 <code>keras.utils<\/code> \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 <code>to_categorical<\/code> \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u043a, \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0432<code>y_train<\/code>\u0438<code>t_test<\/code>, \u0432 one-hot encoding.<\/p>\n<pre><code>y_train = to_categorical(y_train, 43) y_test = to_categorical(y_test, 43)<\/code><\/pre>\n<h2>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438<\/h2>\n<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430&nbsp;<a href=\"https:\/\/keras.io\/\" rel=\"noopener noreferrer nofollow\">Keras<\/a>]. \u0427\u0442\u043e\u0431\u044b \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u044f\u043c, \u043c\u044b \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u043c\u043e\u0434\u0435\u043b\u044c CNN (\u0441\u0432\u0435\u0440\u0442\u043e\u0447\u043d\u0430\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0430\u044f \u0441\u0435\u0442\u044c). CNN \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<ul>\n<li>\n<p>2 Conv2D \u0441\u043b\u043e\u044f (filter=32, kernel_size=(5,5), activation=\u201drelu\u201d)<\/p>\n<\/li>\n<li>\n<p>MaxPool2D \u0441\u043b\u043e\u0439 ( pool_size=(2,2))<\/p>\n<\/li>\n<li>\n<p>Dropout \u0441\u043b\u043e\u0439 (rate=0.25)<\/p>\n<\/li>\n<li>\n<p>2 Conv2D \u0441\u043b\u043e\u044f (filter=64, kernel_size=(3,3), activation=\u201drelu\u201d)<\/p>\n<\/li>\n<li>\n<p>MaxPool2D \u0441\u043b\u043e\u0439 ( pool_size=(2,2))<\/p>\n<\/li>\n<li>\n<p>Dropout \u0441\u043b\u043e\u0439 (rate=0.25)<\/p>\n<\/li>\n<li>\n<p>Flatten \u0441\u043b\u043e\u0439, \u0447\u0442\u043e\u0431\u044b \u0441\u0436\u0430\u0442\u044c \u0441\u043b\u043e\u0438 \u0432 1 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0435<\/p>\n<\/li>\n<li>\n<p>Dense \u0441\u043b\u043e\u0439 (500, activation=\u201drelu\u201d)<\/p>\n<\/li>\n<li>\n<p>Dropout \u0441\u043b\u043e\u0439 (rate=0.5)<\/p>\n<\/li>\n<li>\n<p>Dense \u0441\u043b\u043e\u0439 (43, activation=\u201dsoftmax\u201d)<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">class Net:   @staticmethod   def build(width, height, depth, classes):     model = Sequential()     inputShape = (height, width, depth)     if K.image_data_format() == 'channels_first':       inputShape = (depth, heigth, width)     model = Sequential()     model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu', input_shape=inputShape))     model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu'))     model.add(MaxPool2D(pool_size=(2, 2)))     model.add(Dropout(rate=0.25))     model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))     model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))     model.add(MaxPool2D(pool_size=(2, 2)))     model.add(Dropout(rate=0.25))     model.add(Flatten())     model.add(Dense(500, activation='relu'))     model.add(Dropout(rate=0.5))     model.add(Dense(classes, activation='softmax'))     return model<\/code><\/pre>\n<h2>\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438<\/h2>\n<p>\u041c\u044b \u0441\u0442\u0440\u043e\u0438\u043c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u043e\u043c <code>Adam<\/code>, \u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0442\u0435\u0440\u044c \u2014 \u044d\u0442\u043e <code>categorical_crossentropy<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0434\u043b\u044f \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438. \u0417\u0430\u0442\u0435\u043c \u043e\u0431\u0443\u0447\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>model.fit()<\/code>.<\/p>\n<pre><code class=\"python\">epochs = 25 model = Net.build(width=30, height=30, depth=3, classes=43) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) history = model.fit(X_train, y_train, batch_size=64, validation_data=(X_test, y_test), epochs=epochs)<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/09a\/d1c\/088\/09ad1c088766cb6b3e79426a709319cc.png\" width=\"1065\" height=\"767\"><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u043a \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0438\u0434\u0435\u0442\u044c, \u043d\u0430\u0448\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u043e\u0431\u0443\u0447\u0430\u043b\u0430\u0441\u044c \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0438 25 \u044d\u043f\u043e\u0445 \u0438 \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0430 93% \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043d\u0430 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u043e\u043c \u043d\u0430\u0431\u043e\u0440\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>matplotlib<\/code> \u043c\u044b \u0441\u0442\u0440\u043e\u0438\u043c \u0433\u0440\u0430\u0444\u0438\u043a \u0434\u043b\u044f \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u043e\u0442\u0435\u0440\u044c.<\/p>\n<pre><code>plt.style.use(\"plot\") plt.figure() N = epochs plt.plot(np.arange(0, N), history.history[\"loss\"], label=\"train_loss\") plt.plot(np.arange(0, N), history.history[\"val_loss\"], label=\"val_loss\") plt.plot(np.arange(0, N), history.history[\"accuracy\"], label=\"train_acc\") plt.plot(np.arange(0, N), history.history[\"val_accuracy\"], label=\"val_acc\") plt.title(\"Training Loss and Accuracy\") plt.xlabel(\"Epoch\") plt.ylabel(\"Loss\/Accuracy\") plt.legend(loc=\"lower left\") plt.show()<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a18\/7ae\/ebe\/a187aeebe3a3ef1af21c9315562ae420.png\" alt=\"Training Loss and Accuracy\" title=\"Training Loss and Accuracy\" width=\"393\" height=\"282\"><figcaption>Training Loss and Accuracy<\/figcaption><\/figure>\n<h2>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043d\u0430\u0431\u043e\u0440\u0435<\/h2>\n<p>\u041d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u0430\u043f\u043a\u0443 \u00abTest\u00bb, \u0430 \u0432 \u0444\u0430\u0439\u043b\u0435 <code>Test.csv<\/code> \u0435\u0441\u0442\u044c \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u043f\u0443\u0442\u0435\u043c \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e \u0438 \u043c\u0435\u0442\u043a\u0430\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432. \u041c\u044b \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c \u043f\u0443\u0442\u044c \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e \u0438 \u043c\u0435\u0442\u043a\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 <code>Test.csv<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 <code>Pandas<\/code>. \u0417\u0430\u0442\u0435\u043c, \u043c\u044b \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043e 30\u00d730 \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439 \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 <code>numpy<\/code>, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>accuracy_score<\/code> \u0438\u0437 <code>sklearn metrics<\/code> \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439 \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438. \u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 96% \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043d\u0430 \u044d\u0442\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<pre><code>y_test = pd.read_csv('Test.csv') labels = y_test[\"ClassId\"].values imgs = y_test[\"Path\"].values  images=[]  for img in imgs:     image = Image.open(img)     image = image.resize((30,30))     images.append(img_to_array(image))  X_test=np.array(images) pred = model.predict_classes(X_test) print(accuracy_score(labels, pred)) 0.958590657165479<\/code><\/pre>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/554130\/\"> https:\/\/habr.com\/ru\/post\/554130\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0435 \u0437\u0440\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u044b\u0445 \u043d\u0430\u0443\u043a, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0444\u043e\u043a\u0443\u0441\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0438 \u0447\u0430\u0441\u0442\u0435\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0437\u0440\u0435\u043d\u0438\u044f \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430\u043c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u0445 \u0438 \u0432\u0438\u0434\u0435\u043e, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043b\u044e\u0434\u0435\u0439. \u0420\u0430\u043d\u043d\u0438\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f \u043d\u0430\u0447\u0430\u043b\u0438\u0441\u044c \u0432 1950-\u0445 \u0433\u043e\u0434\u0430\u0445 \u0438 \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0431\u044b\u043b\u0438 \u043a\u043e\u043c\u043c\u0435\u0440\u0447\u0435\u0441\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u044b \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u0435\u0447\u0430\u0442\u043d\u043e\u0433\u043e \u0438 \u0440\u0443\u043a\u043e\u043f\u0438\u0441\u043d\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 1970-\u0445 \u0433\u043e\u0434\u0430\u0445. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f \u0432\u044b\u0440\u043e\u0441\u043b\u0438 \u0432 \u0433\u0435\u043e\u043c\u0435\u0442\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0438\u0438. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u044c \u0434\u043e\u0440\u043e\u0436\u043d\u044b\u0435 \u0437\u043d\u0430\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f.  <\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>\u041d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u0440\u043e\u0436\u043d\u044b\u0445 \u0437\u043d\u0430\u043a\u043e\u0432<\/h2>\n<p>\u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0431\u0449\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439 \u0432&nbsp;<a href=\"https:\/\/www.kaggle.com\/\" rel=\"noopener noreferrer nofollow\">Kaggle<\/a>:&nbsp;<a href=\"https:\/\/www.kaggle.com\/meowmeowmeowmeowmeow\/gtsrb-german-traffic-sign\" rel=\"noopener noreferrer nofollow\">GTSRB<\/a>&nbsp;\u2014\u200a \u044d\u0442\u043e \u043c\u0443\u043b\u044c\u0442\u0438\u043a\u043b\u0430\u0441\u0441\u043e\u0432\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u043d\u0430 \u041c\u0435\u0436\u0434\u0443\u043d\u0430\u0440\u043e\u0434\u043d\u043e\u0439 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0439 \u043a\u043e\u043d\u0444\u0435\u0440\u0435\u043d\u0446\u0438\u0438 \u043f\u043e \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u043c \u0441\u0435\u0442\u044f\u043c (IJCNN) 2011. \u041d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0431\u043e\u043b\u0435\u0435 50 000 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0434\u043e\u0440\u043e\u0436\u043d\u044b\u0445 \u0437\u043d\u0430\u043a\u043e\u0432 \u0438 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0430 43 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0430. \u041e\u043d \u0432\u0435\u0441\u044c\u043c\u0430 \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u0435\u043d: \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u043c\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b &#8212; \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439.<\/p>\n<h2>\u0418\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/h2>\n<p>\u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438.<\/p>\n<pre><code class=\"python\">import os import matplotlib import numpy as np from PIL import Image from tensorflow.keras.preprocessing.image import img_to_array from sklearn.model_selection import train_test_split from keras.utils import to_categorical from keras.models import Sequential, load_model from keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout from tensorflow.keras import backend as K import matplotlib.pyplot as plt from sklearn.metrics import accuracy_score<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 \u00ab<em>train<\/em>\u00bb, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 43 \u043f\u0430\u043f\u043a\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432. \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0434\u0432\u0430 \u0441\u043f\u0438\u0441\u043a\u0430:&nbsp;<em>data<\/em>&nbsp;\u0438 labels. \u042d\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u043d\u0435\u0441\u0442\u0438 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0437\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043d\u0430\u0448\u0438\u0445 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c, \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u043c\u0435\u0442\u043a\u0430\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432. <\/p>\n<pre><code class=\"python\">data = [] labels = []<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0443\u043b\u044f os \u043c\u044b \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438 \u0438\u0445 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043a\u0438 \u0432 \u0441\u043f\u0438\u0441\u043e\u043a&nbsp;<em>data<\/em>&nbsp;\u0438&nbsp;<em>labels<\/em>. \u0414\u043b\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430&nbsp;<em>PIL<\/em>. <\/p>\n<pre><code class=\"python\">for num in range(0, classes):     path = os.path.join('train',str(num))     imagePaths = os.listdir(path)     for img in imagePaths:       image = Image.open(path + '\/'+ img)       image = image.resize((30,30))       image = img_to_array(image)       data.append(image)       labels.append(num)<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u0446\u0438\u043a\u043b \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043e \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 30\u00d730 \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0432\u0441\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438 \u0438\u0445 \u043c\u0435\u0442\u043a\u0438 \u0432 \u0441\u043f\u0438\u0441\u043a\u0430\u0445&nbsp;<em>data<\/em>&nbsp;\u0438&nbsp;<em>labels<\/em>.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u044b&nbsp;<em>numpy<\/em>&nbsp;\u0434\u043b\u044f \u043f\u043e\u0434\u0430\u0447\u0438 \u0432 \u043c\u043e\u0434\u0435\u043b\u044c.  <\/p>\n<pre><code class=\"python\">data = np.array(data) labels = np.array(labels)<\/code><\/pre>\n<p>\u0424\u043e\u0440\u043c\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 &#8212; (39209, 30, 30, 3), \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0438\u043c\u0435\u0435\u0442\u0441\u044f 39 209 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c 30\u00d730 \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439, \u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 3 \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0446\u0432\u0435\u0442\u043d\u044b\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f (\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 RGB).  <\/p>\n<pre><code class=\"python\">print(data.shape, labels.shape) (39209, 30, 30, 3) (39209,)<\/code><\/pre>\n<p>\u0418\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 <code>sklearn<\/code> \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 <code>train_test_split()<\/code> \u0434\u043b\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f 80% \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0438 20% \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u042d\u0442\u043e \u0442\u0438\u043f\u0438\u0447\u043d\u043e\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043c\u0430 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"python\">X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=42) print(X_train.shape, X_test.shape, y_train.shape, y_test.shape) (31367, 30, 30, 3) (7842, 30, 30, 3) (31367,) (7842,) <\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0438 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0432 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0435\u043c \u043d\u0430\u0431\u043e\u0440\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0443 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432.<\/p>\n<pre><code class=\"python\">def cnt_img_in_classes(labels):     count = {}     for i in labels:         if i in count:             count[i] += 1         else:             count[i] = 1     return count  samples_distribution = cnt_img_in_classes (y_train)  def diagram(count_classes):     plt.bar(range(len(dct)), sorted(list(count_classes.values())), align='center')     plt.xticks(range(len(dct)), sorted(list(count_classes.keys())), rotation=90, fontsize=7)     plt.show() diagram(samples_distribution)<\/code><\/pre>\n<figure class=\"\"><figcaption>\u0414\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f<\/figcaption><\/figure>\n<p>\u0418\u0437 \u0433\u0440\u0430\u0444\u0438\u043a\u0430 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0438\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d, \u043d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u0441 \u044d\u0442\u0438\u043c \u0444\u0430\u043a\u0442\u043e\u043c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0442\u043e\u0434 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"python\">def aug_images(images, p):     from imgaug import augmenters as iaa     augs =  iaa.SomeOf((2, 4),           [               iaa.Crop(px=(0, 4)),                iaa.Affine(scale={\"x\": (0.8, 1.2), \"y\": (0.8, 1.2)}),               iaa.Affine(translate_percent={\"x\": (-0.2, 0.2), \"y\": (-0.2, 0.2)}),               iaa.Affine(rotate=(-45, 45))               iaa.Affine(shear=(-10, 10)) ])          seq = iaa.Sequential([iaa.Sometimes(p, augs)])     res = seq.augment_images(images)     return res  def augmentation(images, labels):     min_imgs = 500     classes = cnt_img_in_classes(labels)     for i in range(len(classes)):         if (classes[i] &lt; min_imgs):             add_num = min_imgs - classes[i]             imgs_for_augm = []             lbls_for_augm = []             for j in range(add_num):                 im_index = random.choice(np.where(labels == i)[0])                 imgs_for_augm.append(images[im_index])                 lbls_for_augm.append(labels[im_index])             augmented_class = augment_imgs(imgs_for_augm, 1)             augmented_class_np = np.array(augmented_class)             augmented_lbls_np = np.array(lbls_for_augm)             imgs = np.concatenate((images, augmented_class_np), axis=0)             lbls = np.concatenate((labels, augmented_lbls_np), axis=0)     return (images, labels) X_train, y_train = augmentation(X_train, y_train)<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u0448 \u043e\u0431\u0443\u0447\u0430\u044e\u0449\u0438\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0444\u043e\u0440\u043c\u0443.<\/p>\n<pre><code class=\"python\">print(X_train.shape, X_test.shape, y_train.shape, y_test.shape) (36256, 30, 30, 3) (7842, 30, 30, 3) (36256,) (7842,)<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0449\u0435 \u0440\u0430\u0437 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"python\">augmented_samples_distribution = cnt_img_in_classes(y_train) diagram(augmented_samples_distribution)<\/code><\/pre>\n<figure class=\"\"><figcaption>\u0414\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435 \u0430\u0443\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/figcaption><\/figure>\n<p>\u041d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0430 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043d\u0430\u0448 \u043d\u0430\u0431\u043e\u0440 \u0441\u0442\u0430\u043b \u0431\u043e\u043b\u0435\u0435 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d. \u0414\u0430\u043b\u0435\u0435 \u0438\u0437 \u043f\u0430\u043a\u0435\u0442\u0430 <code>keras.utils<\/code> \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u0435\u0442\u043e\u0434 <code>to_categorical<\/code> \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u043a, \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0432<code>y_train<\/code>\u0438<code>t_test<\/code>, \u0432 one-hot encoding.<\/p>\n<pre><code>y_train = to_categorical(y_train, 43) y_test = to_categorical(y_test, 43)<\/code><\/pre>\n<h2>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438<\/h2>\n<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430&nbsp;<a href=\"https:\/\/keras.io\/\" rel=\"noopener noreferrer nofollow\">Keras<\/a>]. \u0427\u0442\u043e\u0431\u044b \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u044f\u043c, \u043c\u044b \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u043c\u043e\u0434\u0435\u043b\u044c CNN (\u0441\u0432\u0435\u0440\u0442\u043e\u0447\u043d\u0430\u044f \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u0430\u044f \u0441\u0435\u0442\u044c). CNN \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438:<\/p>\n<ul>\n<li>\n<p>2 Conv2D \u0441\u043b\u043e\u044f (filter=32, kernel_size=(5,5), activation=\u201drelu\u201d)<\/p>\n<\/li>\n<li>\n<p>MaxPool2D \u0441\u043b\u043e\u0439 ( pool_size=(2,2))<\/p>\n<\/li>\n<li>\n<p>Dropout \u0441\u043b\u043e\u0439 (rate=0.25)<\/p>\n<\/li>\n<li>\n<p>2 Conv2D \u0441\u043b\u043e\u044f (filter=64, kernel_size=(3,3), activation=\u201drelu\u201d)<\/p>\n<\/li>\n<li>\n<p>MaxPool2D \u0441\u043b\u043e\u0439 ( pool_size=(2,2))<\/p>\n<\/li>\n<li>\n<p>Dropout \u0441\u043b\u043e\u0439 (rate=0.25)<\/p>\n<\/li>\n<li>\n<p>Flatten \u0441\u043b\u043e\u0439, \u0447\u0442\u043e\u0431\u044b \u0441\u0436\u0430\u0442\u044c \u0441\u043b\u043e\u0438 \u0432 1 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0435<\/p>\n<\/li>\n<li>\n<p>Dense \u0441\u043b\u043e\u0439 (500, activation=\u201drelu\u201d)<\/p>\n<\/li>\n<li>\n<p>Dropout \u0441\u043b\u043e\u0439 (rate=0.5)<\/p>\n<\/li>\n<li>\n<p>Dense \u0441\u043b\u043e\u0439 (43, activation=\u201dsoftmax\u201d)<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">class Net:   @staticmethod   def build(width, height, depth, classes):     model = Sequential()     inputShape = (height, width, depth)     if K.image_data_format() == 'channels_first':       inputShape = (depth, heigth, width)     model = Sequential()     model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu', input_shape=inputShape))     model.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu'))     model.add(MaxPool2D(pool_size=(2, 2)))     model.add(Dropout(rate=0.25))     model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))     model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))     model.add(MaxPool2D(pool_size=(2, 2)))     model.add(Dropout(rate=0.25))     model.add(Flatten())     model.add(Dense(500, activation='relu'))     model.add(Dropout(rate=0.5))     model.add(Dense(classes, activation='softmax'))     return model<\/code><\/pre>\n<h2>\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438<\/h2>\n<p>\u041c\u044b \u0441\u0442\u0440\u043e\u0438\u043c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0442\u043e\u0440\u043e\u043c <code>Adam<\/code>, \u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0442\u0435\u0440\u044c \u2014 \u044d\u0442\u043e <code>categorical_crossentropy<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0434\u043b\u044f \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438. \u0417\u0430\u0442\u0435\u043c \u043e\u0431\u0443\u0447\u0430\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>model.fit()<\/code>.<\/p>\n<pre><code class=\"python\">epochs = 25 model = Net.build(width=30, height=30, depth=3, classes=43) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) history = model.fit(X_train, y_train, batch_size=64, validation_data=(X_test, y_test), epochs=epochs)<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u043a \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0438\u0434\u0435\u0442\u044c, \u043d\u0430\u0448\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u043e\u0431\u0443\u0447\u0430\u043b\u0430\u0441\u044c \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0438 25 \u044d\u043f\u043e\u0445 \u0438 \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0430 93% \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043d\u0430 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043e\u0447\u043d\u043e\u043c \u043d\u0430\u0431\u043e\u0440\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>matplotlib<\/code> \u043c\u044b \u0441\u0442\u0440\u043e\u0438\u043c \u0433\u0440\u0430\u0444\u0438\u043a \u0434\u043b\u044f \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u043e\u0442\u0435\u0440\u044c.<\/p>\n<pre><code>plt.style.use(\"plot\") plt.figure() N = epochs plt.plot(np.arange(0, N), history.history[\"loss\"], label=\"train_loss\") plt.plot(np.arange(0, N), history.history[\"val_loss\"], label=\"val_loss\") plt.plot(np.arange(0, N), history.history[\"accuracy\"], label=\"train_acc\") plt.plot(np.arange(0, N), history.history[\"val_accuracy\"], label=\"val_acc\") plt.title(\"Training Loss and Accuracy\") plt.xlabel(\"Epoch\") plt.ylabel(\"Loss\/Accuracy\") plt.legend(loc=\"lower left\") plt.show()<\/code><\/pre>\n<figure class=\"\"><figcaption>Training Loss and Accuracy<\/figcaption><\/figure>\n<h2>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c \u043d\u0430\u0431\u043e\u0440\u0435<\/h2>\n<p>\u041d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u0430\u043f\u043a\u0443 \u00abTest\u00bb, \u0430 \u0432 \u0444\u0430\u0439\u043b\u0435 <code>Test.csv<\/code> \u0435\u0441\u0442\u044c \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u043f\u0443\u0442\u0435\u043c \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e \u0438 \u043c\u0435\u0442\u043a\u0430\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432. \u041c\u044b \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c \u043f\u0443\u0442\u044c \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e \u0438 \u043c\u0435\u0442\u043a\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 <code>Test.csv<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 <code>Pandas<\/code>. \u0417\u0430\u0442\u0435\u043c, \u043c\u044b \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043e 30\u00d730 \u043f\u0438\u043a\u0441\u0435\u043b\u0435\u0439 \u0438 \u0434\u0435\u043b\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 <code>numpy<\/code>, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>accuracy_score<\/code> \u0438\u0437 <code>sklearn metrics<\/code> \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043d\u0438\u0439 \u043d\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438. \u041c\u044b \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 96% \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u043d\u0430 \u044d\u0442\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u0438.<\/p>\n<pre><code>y_test = pd.read_csv('Test.csv') labels = y_test[\"ClassId\"].values imgs = y_test[\"Path\"].values  images=[]  for img in imgs:     image = Image.open(img)     image = image.resize((30,30))     images.append(img_to_array(image))  X_test=np.array(images) pred = model.predict_classes(X_test) print(accuracy_score(labels, pred)) 0.958590657165479<\/code><\/pre>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/554130\/\"> https:\/\/habr.com\/ru\/post\/554130\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-322012","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/322012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=322012"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/322012\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=322012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=322012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=322012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}