<jupyter_start><jupyter_text>Классификация MNIST сверточной сетью<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import cv2
import numpy as np
from tensorflow import keras
train = np.loadtxt('train.csv', delimiter=',', skiprows=1)
test = np.loadtxt('test.csv', delimiter=',', skiprows=1)
# сохраняем разметку в отдельную переменную
train_label = train[:, 0]

# приводим размерность к удобному для обаботки виду
# добавляем размерность канала
train_img = np.resize(train[:, 1:], (train.shape[0], 28, 28, 1))
test_img = np.resize(test, (test.shape[0], 28, 28, 1))<jupyter_output><empty_output><jupyter_text>Визуализируем исходные данные<jupyter_code>fig = plt.figure(figsize=(20, 10))
for i, img in enumerate(train_img[0:5, :], 1):
    subplot = fig.add_subplot(1, 5, i)
    plt.imshow(img[:,:,0], cmap='gray');
    subplot.set_title('%s' % train_label[i - 1]);<jupyter_output><empty_output><jupyter_text>Разбиваем выборку на обучение и валидацию<jupyter_code>from sklearn.model_selection import train_test_split
y_train, y_val, x_train, x_val = train_test_split(
    train_label, train_img, test_size=0.2, random_state=42)<jupyter_output><empty_output><jupyter_text>Собираем сверточную сеть для обучения<jupyter_code>seed = 123457
kernek_initializer = keras.initializers.glorot_normal(seed=seed)
bias_initializer = keras.initializers.normal(stddev=1., seed=seed)

model = keras.models.Sequential()

model.add(keras.layers.Conv2D(6, 
                              kernel_size=(5, 5), 
                              padding='same', 
                              activation='relu', 
                              input_shape=x_train.shape[1:],
                              bias_initializer=bias_initializer,
                              kernel_initializer=kernek_initializer))

model.add(keras.layers.MaxPool2D(pool_size=(2, 2), padding='valid'))

model.add(keras.layers.Conv2D(16, 
                              kernel_size=(5, 5),
                              padding='valid',
                              activation='relu', 
                              bias_initializer=bias_initializer,
                              kernel_initializer=kernek_initializer))

model.add(keras.layers.MaxPool2D(pool_size=(2, 2), padding='valid'))

model.add(keras.layers.Flatten())

model.add(keras.layers.Dense(32, activation='relu',
                             bias_initializer=bias_initializer,
                             kernel_initializer=kernek_initializer))

model.add(keras.layers.Dense(10, activation='softmax',
                             bias_initializer=bias_initializer,
                             kernel_initializer=kernek_initializer))

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])<jupyter_output>WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/initializers.py:143: calling RandomNormal.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.<jupyter_text>Выводим информацию о модели<jupyter_code>model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 28, 28, 6)         156       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 6)         0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 10, 10, 16)        2416      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 16)          0         
_________________________________________________________________
flatten (Flatten)            (None, 400)               0         
_________________________________________________________________
dense (Dense)                (None, 32)                12832     
________________________________________________________[...]<jupyter_text>One hot encoding разметки<jupyter_code>y_train_labels = keras.utils.to_categorical(y_train)
y_train[:10]
y_train_labels[:10]<jupyter_output><empty_output><jupyter_text>Запускаем обучение<jupyter_code>model.fit(x_train, 
          y_train_labels,
          batch_size=32, 
          epochs=5,
          validation_split=0.2)<jupyter_output>Train on 26880 samples, validate on 6720 samples
Epoch 1/5
26880/26880 [==============================] - 19s 708us/sample - loss: 0.9753 - acc: 0.7766 - val_loss: 0.2560 - val_acc: 0.9298
Epoch 2/5
26880/26880 [==============================] - 21s 776us/sample - loss: 0.1949 - acc: 0.9466 - val_loss: 0.1530 - val_acc: 0.9589
Epoch 3/5
26880/26880 [==============================] - 19s 697us/sample - loss: 0.1254 - acc: 0.9631 - val_loss: 0.1349 - val_acc: 0.9637
Epoch 4/5
26880/26880 [==============================] - 18s 687us/sample - loss: 0.0924 - acc: 0.9727 - val_loss: 0.1286 - val_acc: 0.9664
Epoch 5/5
26880/26880 [==============================] - 19s 691us/sample - loss: 0.0727 - acc: 0.9779 - val_loss: 0.1151 - val_acc: 0.9677<jupyter_text>Предсказываем класс объекта<jupyter_code>pred_val = model.predict_classes(x_val)
pred_val[:10]<jupyter_output><empty_output><jupyter_text>Оцениваем качество решение на валидационной выборке<jupyter_code>from sklearn.metrics import accuracy_score
print('Accuracy: %s' % accuracy_score(y_val, pred_val))
from sklearn.metrics import classification_report
print(classification_report(y_val, pred_val))
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_val, pred_val))<jupyter_output>[[801   0   2   1   0   1   2   0   0   9]
 [  0 897   5   1   1   0   0   2   0   3]
 [  0   4 831   3   3   0   1   1   1   2]
 [  1   0   3 915   0   1   1   5   4   7]
 [  1   1   2   0 805   0   4   1   3  22]
 [  1   1   0  11   2 668   3   3   3  10]
 [  4   2   1   0   0   3 769   0   4   2]
 [  1   0  11   6   1   0   0 855   1  18]
 [  3   0   9   9   3   4   0   1 799   7]
 [  0   0   0   0   0   1   0   1   2 834]]<jupyter_text>Предсказания на тестовыйх данных<jupyter_code>pred_test = model.predict_classes(test_img)<jupyter_output><empty_output><jupyter_text>Визуализируем предсказания<jupyter_code>fig = plt.figure(figsize=(20, 10))
indices = np.random.choice(range(len(test_img)), 5)
img_prediction = zip(test_img[indices], pred_test[indices])
for i, (img, pred) in enumerate(img_prediction, 1):
    subplot = fig.add_subplot(1, 5, i)
    plt.imshow(img[...,0], cmap='gray');
    subplot.set_title('%d' % pred);<jupyter_output><empty_output><jupyter_text>Готовим файл для отправки<jupyter_code>with open('submit.txt', 'w') as dst:
    dst.write('ImageId,Label\n')
    for i, p in enumerate(pred_test, 1):
        dst.write('%s,%d\n' % (i, p))
# Your submission scored 0.9730952380952381<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1289717120257407, 'fraction_numerical': 0.06394959109800241, 'mean_word_length': 3.4063792085056113, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '667164', 'license': 'MIT', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2532, 'n_words': 509, 'path': 'Lectures notebooks/(Lectures notebooks) netology Machine learning/15. Convolutional Neural Network  (CNN)/005_cnn_mnist.ipynb', 'repo_name': 'Alex110117/data_analysis', 'size': '7459'}