   Python.      CNN  AlexNet
 


          :     KNN/SVM  HOG  SIFT  CNN, AlexNet, transfer learning, GPU  Google Colab.   ,  , , , ,        .  ,     ,         ,  ,  ,    .        Python    ,    .       .





 

   Python.      CNN  AlexNet





 1: 


            .    ,          .  ,       ,            ,    .

           :       .

            .          ,       .

            .        ,           .

          .

        Python 








       ,     (Guido van Rossum).   Python   1991 .       ;  ,    . Python       ,  -, ,          .

 Python  Anaconda     : https://www.anaconda.com/download (https://www.anaconda.com/download).        : 2.7  3.7.

 64- Windows      Anaconda2-2018.12-Windows-x86_64.exe  Anaconda3-2018.12-Windows-x86_64.exe.       .   Anaconda Prompt   ,     .  Python 2.7   pip install opencv-contrib-python==3.3.0.9.  Python 3.7  opencv-python  pip install opencv-python==3.4.3.18,  PyTorch;  ,   CPU,   pip install https://download.pytorch.org/whl/cpu/torch-1.0.0-cp37-cp37m-win_amd64.whl (https://download.pytorch.org/whl/cpu/torch-1.0.0-cp37-cp37m-win_amd64.whl). ,  torchvision  pip install torchvision.

     CPU,    ,   :    HP ZBook 15 G2;   Windows 7 Professional, 64- ;  Intel Core i7-4810MQ    2,80   8,0   .

   :

       :

 1:       (Tiny Images);

 2:       (Histogram of Oriented Gradients, HOG);

 3:  Bag of SIFT (Bag of Scale-Invariant Feature Transform)    ;

 4:    (Convolutional Neural Network, CNN),   ;

 5:        AlexNet;

 6:       AlexNet    .

  ,  ,        :  k   (k-Nearest Neighbors,  KNN)     (Support Vector Machines,  SVM).

             ( ,  ),      ( ,  ).        Jupyter Notebook.     ,           .

     .




 2:     


   








       .           .

          ,       ,       .    ,         ,          .           .

         .

#   # Python # 3.7.0

import cv2 # opencv-python 3.4.3.18

import numpy as np # 1.15.1

import matplotlib.pyplot as plt # 2.2.3

import math

import time

import copy

import random

import glob #  glob    

import pickle #  pickle          

import itertools

import skimage.exposure # scikit-image 0.13.1

from sklearn.neighbors import KNeighborsClassifier as KNC

from sklearn.svm import LinearSVC # scikit-learn 0.19.2

from sklearn.cluster import KMeans

from skimage.feature import hog

from sklearn.metrics import confusion_matrix

                   .

c_names = [name[11:] for name in glob.glob('data/Train/*')]

class_names = dict(zip(range(len(c_names)), c_names))

print(" {:d} :".format(len(class_names)))

print(class_names)

 Jupyter Notebook    :

 15 :

{0: 'Class01', 1: 'Class02', 2: 'Class03', 3: 'Class04', 4: 'Class05', 5: 'Class06', 6: 'Class07', 7: 'Class08', 8: 'Class09', 9: 'Class10', 10: 'Class11', 11: 'Class12', 12: 'Class13', 13: 'Class14', 14: 'Class15'}

              ,    ,      .

 Bag of SIFT         cv2.imread().      HOG-   ,   ,        ;  HOG     .      ,   - (minibatch).       .          :      .

     100     100  .  ,      3000 .

     HOG-   load_data().    : path, img_size, flatten, mean, norm, shuffle  is_hog.  path        . img_size     ;    (0, 0) ,    .  flatten, mean, norm, shuffle  is_hog       False.  flatten=True,     HOG-    .  mean=True     ,   norm=True    .      .  shuffle=True,    .  is_hog=True,    HOG-.         .

def load_data (path, img_size=(0,0), flatten=False, mean=False, norm=False, shuffle=False, is_hog=False):

start_time = time.time() #  

n = 0 #    

data = [] #    

labels = [] #    

if is_hog: #  HOG-

cv2.HOGDescriptor()

#     

for id, class_name in class_names.items():

img_path_class = glob.glob(path + '/' + class_name + '/*.bmp')

labels.extend([id]*len(img_path_class))

for filename in img_path_class:

img = cv2.imread(filename, 0) #  

if img_size[0] and img_size[1]: #   

img = cv2.resize(img, img_size, cv2.INTER_LINEAR)

if is_hog: #   HOG

_, img = hog(img, block_norm='L2-Hys', visualise=True)

if flatten: #  

img = img.flatten()

if mean: #      

img = np.subtract(img, np.mean(img))

if norm: #  

img = cv2.normalize(img, img)

data.append(img)

n = n + 1

#   (   )

if shuffle:

bundle = list(zip(data, labels))

random.shuffle(bundle)

data, labels = zip(*bundle)

print ("  {:d}   {:.2f}   {:s}ing.".format(n, time.time()-start_time, path[5:]))

return data, labels #    

      ,      .

#            

img_size_tiny = (16, 16)

train_d_tiny, train_l_tiny = load_data('data/Train', img_size_tiny, flatten=True, mean=True, norm=True, shuffle=True)

test_d_tiny, test_l_tiny = load_data('data/Test', img_size_tiny, flatten=True, mean=True, norm=True, shuffle=True)

  1500   1,34   .

 1500      1,35 .

  ,          .   :  k   (k-Nearest Neighbors,  KNN)     (Support Vector Machines,  SVM).

   (Nearest Neighbor) 














  :         ,          ,         .      :    ,       ,        .          .          k   (k-Nearest Neighbors,  KNN).        ,    k      ,        ,  ,        .

   (SVM)      ,   , n.

   (Support Vector Machines,  SVM) 




















     ,   ,    .     .  ,       ,     .  ,      ,           (  ). ,       ,         ,   .  ,       ,         .

     SVM    








      .      ,         ,      .         ,   KNN,      .

    KNN  SVM       .       (,   ),    get_best_parameter(),      n_neighbors  KNeighborsClassifier(),      lambda ( C,      )  sklearn.svm(),       .

   : train_data, train_label, test_data, test_label, model  n_max.      ,  ,      .  model  : KNN  SVM.   sklearn.neighbors KNN   KNeighborsClassifier(),      KNC.  n_max ,       ;   n_max=10.

      ,       .         ,    ,        . ,   ,   .       ,      .     .

#  n_neighbors  KNeighborsClassifier()  lambda  sklearn.svm()   

def get_best_parameter(train_data, train_label, test_data, test_label, model, n_max = 10):

start_time = time.time() #  

if model == "KNC":

param = "n_neighbors"

elif model == "SVM":

param = "lambda"

else:

return -1

nn_max = 0.0 #   

ac_max = 0.0 #      n_neighbors  lambda

nn_all = [] #     n_neighbors  lambda

ac_all = [] #    

for nn in range(1, n_max+1): #       n_neighbors  KNeighborsClassifier()

if model == "KNC": #  KNeighborsClassifier

mod = KNC(nn)

elif model == "SVM": #  LinearSVC

nn = nn * 0.1

mod = LinearSVC(C=nn, random_state=0)

mod.fit(train_data, train_label) #     

pred_label = mod.predict(test_data) #    

ac = 100.0 * np.sum(pred_label == test_label) / len(test_label) #  

ac_all.append(ac)

nn_all.append(nn)

print("   {:.2f}% ({:s} = {:.2f}).".format(ac, param, nn))

if (ac > ac_max): #      n_neighbors  lambda

ac_max = ac

nn_max = nn

print("\n    {:.2f}% ({:s} = {:2f}).".format(ac_max, param, nn_max))

print("  : {:7.2f} ." .format(time.time() - start_time))

plt.plot(nn_all, ac_all, '-ob')

plt.title("    {:s}" .format(param))

plt.xlabel("{:s}".format(param))

plt.ylabel(" (%)")

plt.grid(True)

plt.show()

return nn_max

   ,        KNN.

#  n_neighbors  KNeighborsClassifier()   

n_neighbors_tiny = get_best_parameter(train_d_ tiny, train_l_tiny, test_d_tiny, test_l_tiny, "KNC")

  Jupyter Notebook   .             KNN    1.

   55,20 % (n_neighbors = 1,00).

   50,60 % (n_neighbors = 2,00).

   50,87% (n_neighbors = 3,00).

   52,60 % (n_neighbors = 4,00).

   53,53% (n_neighbors = 5,00).

   51,80% (n_neighbors = 6,00).

   51,27% (n_neighbors = 7,00).

   51,07% (n_neighbors = 8,00).

   50,20 % (n_neighbors = 9,00).

   49,40 % (n_neighbors = 10,00).

    55,20 % (n_neighbors = 1,000000).

  : 9,73 .

   get_best_parameter()        SVM.    n_max    20:

#   lambda  LinearSVC()   

lambda_tiny = get_best_parameter(train_d_tiny, train_l_tiny, test_d_tiny, test_l_tiny, "SVM", 20)

  Jupyter Notebook   .             SVM    2.

   41,20% (lambda = 0,10).

   41,67 % (lambda = 0,20).

   41,73 % (lambda = 0,30).

   41,47 % (lambda = 0,40).

   40,80 % (lambda = 0,50).

   40,47 % (lambda = 0,60).

   40,00% (lambda = 0,70).

   40,20 % (lambda = 0,80).

   40,27 % (lambda = 0,90).

   40,07 % (lambda = 1,00).

   40,27% (lambda = 1,10).

   40,00 % (lambda = 1,20).

   39,73% (lambda = 1,30).

   39,73% (lambda = 1,40).

   39,80 % (lambda = 1,50).

   39,73% (lambda = 1,60).

   39,47 % (lambda = 1,70).

   39,33 % (lambda = 1,80).

   39,07 % (lambda = 1,90).

   39,13 % (lambda = 2,00).

    41,73% (lambda = 0,300000).

  : 11,41 .

   . 1  . 2,     (16, 16)      49,40 %  55,20 %,     n_neighbors  KNN   1  10.    SVM,        (lambda C)    0,1  2,0   0,1       39,07 %  41,73 %.   ,       ,           : 10   n_neighbors,   1  10   1; 20   lambda,   0,1  2,0   0,1.

          print_stats().      : label_pred, label_true, time_train, time_test, model  n_class.      :   ,  ,     .  model ,      . n_class        ,    15.           ,        ,  n_class   .

def print_stats(label_pred, label_true, time_train, time_test, model, n_class = 15):

accuracy = 100.0 * np.sum(label_pred == label_true) / len(label_true)

print("  \"{:s}\"  {:.2f}%.".format(model, accuracy))

print(":\t : {:7.2f} .".format(time_train))

print(": \t : {:7.2f} .\n".format(time_test))

#     

if n_class:

class_correct = list(0. for i in range(n_class))

class_total = list(0. for i in range(n_class))

for i in range(len(label_true)):

label = label_true[i]

class_total[label] += 1

if label_pred[i] == label:

class_correct[label] += 1

for i in range(n_class):

acc = 100.0 * class_correct[i] / class_total[i]

print("  {:2.0f}%  {:s}.".format(acc, class_names[i]))

return accuracy

   ,     ,        ,        ,        ,    ,     print_stats(),      KNN  SVM,  .

#       KNN

time_start = time.time() #  

model_best_tiny_KNC = KNC(n_neighbors_tiny)

model_best_tiny_KNC.fit(train_d_tiny, train_l_tiny) #     

time_mid = time.time()

time_train = time_mid - time_start # ,   

label_pred_tiny_KNC = model_best_tiny_KNC.predict(test_d_tiny) #    

time_test = time.time() - time_mid # ,   

acc_tiny_KNC = print_stats(label_pred_tiny_KNC, test_l_tiny, time_train, time_test, "Tiny Images + Nearest Neighbor")

  Tiny Images + Nearest Neighbor  55,20%.

:  : 0,02 .

:  : 0,73 .

   Class01  21%.

   Class02  38%.

   Class03  76%.

   Class04  50%.

  Class05  93%.

   Class06  28%.

   Class07  74%.

   Class08  40%.

   09  53%.

   Class10  87%.

   Class11  29%.

   12  62%.

   Class13  40%.

   Class14  65%.

   15  72%.

#       SVM

time_start = time.time() #  

model_best_tiny_SVM = LinearSVC(C=lambda_tiny, random_state=0) #  LinearSVC

model_best_tiny_SVM.fit(train_d_tiny, train_l_tiny) #  SVM

time_mid = time.time()

time_train = time_mid - time_start #  

label_pred_tiny_SVM = model_best_tiny_SVM.predict(test_d_tiny) #  clf.predict

time_test = time.time() - time_mid #  

acc_tiny_SVM = print_stats(label_pred_tiny_SVM, test_l_tiny, time_train, time_test, "Tiny Images + Linear SVC")

  Tiny Images + Linear SVC  41,73%.

:  : 0,19 .

:  : 0,02 .

  Class01  26%.

  Class02  22%.

  Class03  75%.

  Class04  11%.

  78%  Class05.

   Class06  21%.

   Class07  63%.

   08  18%.

   Class09  44%.

   Class10  64%.

   Class11  26%.

   Class12  42%.

   Class13  22%.

   Class14  42%.

   15  72%.

      








   .     plot_confusion_matrix(),     .

#      

def plot_confusion_matrix(cm, classes, normalize=True, title=' ', cmap=plt.cm. Blues):

if normalize: # 

cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

plt.figure(figsize=(10, 8))

plt.imshow(cm, interpolation='nearest', cmap=cmap)

plt.title(' : ' + title)

plt.colorbar()

tick_marks = np.arange(len(classes))

plt.xticks(tick_marks, classes, rotation=45)

plt.yticks(tick_marks, classes)

fmt = '.2f',  normalize,  'd'

thresh = cm.max() / 2.

for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):

plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white",  cm[i, j] > thresh,  "black")

plt.ylabel (" ")

plt.xlabel ( )

plt.tight_layout()



#  1:   

#   (  )

cm11 = confusion_matrix(test_l_tiny, label_pred_tiny_KNC)

plot_confusion_matrix(cm11, c_names, title='  +   ')

#   ( SVC)

cm12 = confusion_matrix(test_l_tiny, label_pred_tiny_SVM)

plot_confusion_matrix(cm12, c_names, title='  +  SVC')

        3  4 .

       .              ,   03, 05, 07, 10  15   60 %   .

      (8, 8), (16, 16), (32, 32), (64, 64)  (128, 128).     1.           KNN,    SVM;             ,       .

    ,     KNN  SVM  59,07 %  57,13 % ;      KNN   ,  SVM.



. 1.        .



 

(8,8)

(16,16)

(32,32)

(64,64)

(128, 128)



  (%)

KNN

48,73

55,20

55,20

57,47

59,07



SVM

32,93

41,73

48,20

53,40

57,13



  (.)

KNN

0,25

0,97

3,88

14,97

58,05



SVM

0,19

0,57

1,66

6,10

25,76




 3:        


   (Histogram of Oriented Gradients,  HOG) 














   ,         .

HOG       ,          .       .

  HOG-   ,           .      HOG-,    ,  KNN  SVM,       .        HOG-  .

  load_data(),    2,       is_hog  True,    HOG-         ,   .

#         

img_size_hog = (64, 64)

train_d_hog, train_l_hog = load_data('data/Train', img_size_hog, flatten=True, shuffle=True, is_hog=True)

test_d_hog, test_l_hog = load_data('data/Test', img_size_hog, flatten=True, shuffle=True, is_hog=True)

  1500   17,24   .

  1500     17,21 .

                ,    ,   HOG-   .

    get_best_parameter() (.  2),     n_neighbors  KNN-    lambda  SVM-.    .               5  6.

#   n_neighbors  KNeighborsClassifier()   

n_neighbors_hog = get_best_parameter(train_d_hog, train_l_hog, test_d_hog, test_l_hog, "KNC")

   61,13% (n_neighbors = 1,00).

   56,73% (n_neighbors = 2,00).

   56,40% (n_neighbors = 3,00).

   56,33% (n_neighbors = 4,00).

   55,13% (n_ neighbors = 5,00).

   53,60 % (n_neighbors = 6,00).

   52,00 % (n_neighbors = 7,00).

   51,87% (n_neighbors = 8,00).

   51,33% (n_neighbors = 9,00).

   50,60 % (n_neighbors = 10,00).

    61,13% (n_neighbors = 1,000000).

  : 155,58 .

     (64, 64),   KNN     50,60 %  61,13 %;   SVM   47,80 %  48,73 %.    156   378  ,       15,6   18,9 .




  .


   .

   ,     (https://www.litres.ru/book/iton-bleyk/klassifikatsiia-izobrazhenii-na-python-ot-klassicheskogo-komp-i-74596859/)  .

      Visa, MasterCard, Maestro,    ,   ,     ,  PayPal, WebMoney, ., QIWI ,       .


