项目作者: Ermlab

项目描述 :
CIFAR-10 IMAGE CLASSIFICATION WITH KERAS CONVOLUTIONAL NEURAL NETWORK TUTORIAL
高级语言: Python
项目地址: git://github.com/Ermlab/cifar10keras.git
创建时间: 2017-07-21T20:41:57Z
项目社区:https://github.com/Ermlab/cifar10keras

开源协议:

下载


CIFAR-10 IMAGE CLASSIFICATION WITH KERAS CONVOLUTIONAL NEURAL NETWORK TUTORIAL

What is Keras?

“Keras is an open source neural network library written in Python and capable of running on top of either TensorFlow, CNTK or Theano.

Use Keras if you need a deep learning libraty that:

  • Allows for easy and fast prototyping
  • Supports both convolutional networks and recurrent networks, as well as combinations of the two
  • Runs seamlessly on CPU and GPU

Keras is compatible with Python 2.7-3.5”[1].

Since Semptember 2016, Keras is the second-fastest growing Deep Learning framework after Google’s Tensorflow, and the third largest after Tensorflow and Caffe[2].

What is Deep Learning?

“Deep Learning is the application to learning tasks of artificial neural networks(ANNs) that contain more than one hidden layer. Deep learning is part of Machine Learning methods based on learning data representations.
Learning can be supervised, parially supervised or unsupervised[3].”

Project desciption

Simple Youtube presentation what type of visualization is generated:

What will you learn?

You will learn:

  • What is Keras library and how to use it
  • What is Deep Learning
  • How to use ready datasets
  • What is Convolutional Neural Networks(CNN)
  • How to build step by step Convolutional Neural Networks(CNN)
  • What are differences in model results
  • What is supervised and unsupervised learning
  • Basics of Machine Learning
  • Introduction to Artificial Intelligence(AI)

Project structure

  • 4layerCNN.py - 4-layer Keras model
  • 6layerCNN.py - 6-layer Keras model
  • README.md - project description step by step

Convolutional neural network

6-layer neural network

Network Architecture

  1. OPERATION DATA DIMENSIONS WEIGHTS(N) WEIGHTS(%)
  2. Input ##### 3 32 32
  3. Conv2D \|/ ------------------- 896 0.0%
  4. relu ##### 32 32 32
  5. Dropout | || ------------------- 0 0.0%
  6. ##### 32 32 32
  7. Conv2D \|/ ------------------- 9248 0.4%
  8. relu ##### 32 32 32
  9. MaxPooling2D Y max ------------------- 0 0.0%
  10. ##### 32 16 16
  11. Conv2D \|/ ------------------- 18496 0.8%
  12. relu ##### 64 16 16
  13. Dropout | || ------------------- 0 0.0%
  14. ##### 64 16 16
  15. Conv2D \|/ ------------------- 36928 1.5%
  16. relu ##### 64 16 16
  17. MaxPooling2D Y max ------------------- 0 0.0%
  18. ##### 64 8 8
  19. Conv2D \|/ ------------------- 73856 3.1%
  20. relu ##### 128 8 8
  21. Dropout | || ------------------- 0 0.0%
  22. ##### 128 8 8
  23. Conv2D \|/ ------------------- 147584 6.2%
  24. relu ##### 128 8 8
  25. MaxPooling2D Y max ------------------- 0 0.0%
  26. ##### 128 4 4
  27. Flatten ||||| ------------------- 0 0.0%
  28. ##### 2048
  29. Dropout | || ------------------- 0 0.0%
  30. ##### 2048
  31. Dense XXXXX ------------------- 2098176 87.6%
  32. relu ##### 1024
  33. Dropout | || ------------------- 0 0.0%
  34. ##### 1024
  35. Dense XXXXX ------------------- 10250 0.4%
  36. softmax ##### 10

Model

  1. model = Sequential()
  2. model.add(Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=x_train.shape[1:]))
  3. model.add(Dropout(0.2))
  4. model.add(Conv2D(32,(3,3),padding='same', activation='relu'))
  5. model.add(MaxPooling2D(pool_size=(2,2)))
  6. model.add(Conv2D(64,(3,3),padding='same',activation='relu'))
  7. model.add(Dropout(0.2))
  8. model.add(Conv2D(64,(3,3),padding='same',activation='relu'))
  9. model.add(MaxPooling2D(pool_size=(2,2)))
  10. model.add(Conv2D(128,(3,3),padding='same',activation='relu'))
  11. model.add(Dropout(0.2))
  12. model.add(Conv2D(128,(3,3),padding='same',activation='relu'))
  13. model.add(MaxPooling2D(pool_size=(2,2)))
  14. model.add(Flatten())
  15. model.add(Dropout(0.2))
  16. model.add(Dense(1024,activation='relu',kernel_constraint=maxnorm(3)))
  17. model.add(Dropout(0.2))
  18. model.add(Dense(num_classes, activation='softmax'))
  19. sgd = SGD(lr=0.01, momentum=0.9, decay=1e-6, nesterov=False)

Train model:

  1. model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
  2. return model
  3. cnn_n = base_model()
  4. cnn_n.summary()

Fit model:

  1. cnn = cnn_n.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test,y_test),shuffle=True)

Results

All results are for 50k iteration, learning rate=0.01. Neural networks have been trained at 16 cores and 16GB RAM on plon.io

  • epochs = 10 accuracy=75.61%

Keras Training Accuracy vs Validation Accuracy
Keras Training Loss vs Validation Loss

Confusion matrix result:

  1. [[806 9 39 13 28 4 7 9 61 24]
  2. [ 14 870 4 10 3 4 7 0 28 60]
  3. [ 69 1 628 64 122 36 44 19 13 4]
  4. [ 19 5 52 582 109 99 76 29 14 15]
  5. [ 13 2 44 46 761 27 38 62 6 1]
  6. [ 15 1 50 189 69 588 31 48 7 2]
  7. [ 8 3 39 53 52 14 814 4 10 3]
  8. [ 15 3 31 45 63 29 5 795 2 12]
  9. [ 61 13 8 10 17 1 4 4 875 7]
  10. [ 23 52 11 10 7 7 5 12 31 842]]

Confusion matrix vizualizing

610

Time of learning process: 1h 45min

  • epochs = 20 accuracy=75.31%

Keras Training Accuracy vs Validation Accuracy
Keras Training Loss vs Validation Loss

Confusion matrix result:

  1. [[810 5 30 22 14 2 9 10 60 38]
  2. [ 13 862 7 8 3 6 4 7 20 70]
  3. [ 85 2 626 67 84 44 44 27 12 9]
  4. [ 39 6 47 581 73 137 50 38 17 12]
  5. [ 22 1 52 87 744 34 22 64 2 2]
  6. [ 20 3 40 178 44 639 21 48 2 5]
  7. [ 12 3 42 55 67 16 782 10 7 6]
  8. [ 15 2 24 38 59 37 3 810 5 7]
  9. [ 79 14 10 19 6 4 8 5 827 28]
  10. [ 25 60 8 9 8 5 2 12 21 850]]

Confusion matrix vizualizing

620

Time of learning process: 3h 40min

  • epochs = 50 accuracy=69.93%

Keras Training Accuracy vs Validation Accuracy
Keras Training Loss vs Validation Loss

Confusion matrix result:

  1. [[760 5 72 32 11 6 12 7 67 28]
  2. [ 12 862 10 16 3 2 18 4 30 43]
  3. [ 55 1 712 67 44 35 47 20 11 8]
  4. [ 37 7 126 554 63 81 69 45 11 7]
  5. [ 23 2 125 86 622 27 36 69 8 2]
  6. [ 20 2 121 201 48 488 56 50 7 7]
  7. [ 16 7 101 65 28 27 734 8 10 4]
  8. [ 16 4 59 60 57 36 9 749 5 5]
  9. [107 13 30 32 3 10 8 6 770 21]
  10. [ 42 100 8 26 8 7 4 21 42 742]]

Confusion matrix vizualizing

650

Time of learning process: 8h 10min

  • epochs = 100 accuracy=68.66%

Keras Training Accuracy vs Validation Accuracy
Keras Training Loss vs Validation Loss

Confusion matrix result:

  1. [[736 11 54 45 30 14 15 9 61 25]
  2. [ 10 839 6 38 3 13 7 5 22 57]
  3. [ 47 2 566 96 145 65 51 17 7 4]
  4. [ 23 6 56 570 97 140 57 29 12 10]
  5. [ 16 2 52 80 700 55 25 64 3 3]
  6. [ 10 1 64 211 59 582 24 39 6 4]
  7. [ 4 3 42 114 121 40 650 13 5 8]
  8. [ 14 1 40 57 69 68 11 723 3 14]
  9. [ 93 32 26 37 16 15 6 2 752 21]
  10. [ 34 83 8 42 12 21 6 21 25 748]]

Confusion matrix vizualizing

6100

Time of learning process: 17h 10min

4-Layer neural network

Network Architecture

  1. OPERATION DATA DIMENSIONS WEIGHTS(N) WEIGHTS(%)
  2. Input ##### 3 32 32
  3. Conv2D \|/ ------------------- 896 0.1%
  4. relu ##### 32 32 32
  5. Conv2D \|/ ------------------- 9248 0.7%
  6. relu ##### 32 30 30
  7. MaxPooling2D Y max ------------------- 0 0.0%
  8. ##### 32 15 15
  9. Dropout | || ------------------- 0 0.0%
  10. ##### 32 15 15
  11. Conv2D \|/ ------------------- 18496 1.5%
  12. relu ##### 64 15 15
  13. Conv2D \|/ ------------------- 36928 3.0%
  14. relu ##### 64 13 13
  15. MaxPooling2D Y max ------------------- 0 0.0%
  16. ##### 64 6 6
  17. Dropout | || ------------------- 0 0.0%
  18. ##### 64 6 6
  19. Flatten ||||| ------------------- 0 0.0%
  20. ##### 2304
  21. Dense XXXXX ------------------- 1180160 94.3%
  22. relu ##### 512
  23. Dropout | || ------------------- 0 0.0%
  24. ##### 512
  25. Dense XXXXX ------------------- 5130 0.4%
  26. softmax ##### 10

Model

  1. model = Sequential()
  2. model.add(Conv2D(32, (3, 3), padding='same', input_shape=x_train.shape[1:]))
  3. model.add(Activation('relu'))
  4. model.add(Conv2D(32,(3, 3)))
  5. model.add(Activation('relu'))
  6. model.add(MaxPooling2D(pool_size=(2, 2)))
  7. model.add(Dropout(0.25))
  8. model.add(Conv2D(64, (3, 3), padding='same'))
  9. model.add(Activation('relu'))
  10. model.add(Conv2D(64, (3,3)))
  11. model.add(Activation('relu'))
  12. model.add(MaxPooling2D(pool_size=(2, 2)))
  13. model.add(Dropout(0.25))
  14. model.add(Flatten())
  15. model.add(Dense(512))
  16. model.add(Activation('relu'))
  17. model.add(Dropout(0.5))
  18. model.add(Dense(num_classes))
  19. model.add(Activation('softmax'))
  20. sgd = SGD(lr = 0.1, decay=1e-6, nesterov=True)

Train model:

  1. model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
  2. return model
  3. cnn_n = base_model()
  4. cnn_n.summary()

Fit model:

  1. cnn = cnn_n.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test,y_test),shuffle=True)

Results

All results are for 50k iteration, learning rate=0.1. Neural networks have been trained at 16 cores and 16GB RAM on plon.io

  • epochs = 10 accuracy=71.29%

Keras Training Accuracy vs Validation Accuracy
Keras Training Loss vs Validation Loss

Confusion matrix result:

  1. [[772 5 24 19 17 6 18 10 66 63]
  2. [ 14 637 1 4 3 7 19 2 38 275]
  3. [ 81 0 538 50 88 86 93 27 19 18]
  4. [ 20 1 52 468 60 180 143 33 13 30]
  5. [ 19 1 51 59 662 33 91 66 16 2]
  6. [ 12 0 34 135 37 664 53 41 11 13]
  7. [ 7 0 23 29 26 13 885 2 10 5]
  8. [ 10 0 24 45 48 69 20 756 5 23]
  9. [ 74 4 3 9 4 6 8 4 854 34]
  10. [ 18 8 5 13 9 4 10 6 34 893]]

Confusion matrix vizualizing

410

Time of learning process: 1h 10min

  • epochs = 20 accuracy=74.57%

Keras Training Accuracy vs Validation Accuracy
Keras Training Loss vs Validation Loss

Confusion matrix result:

  1. [[729 11 56 36 52 5 12 11 58 30]
  2. [ 8 883 1 12 5 1 19 0 11 60]
  3. [ 40 3 545 88 152 54 71 35 7 5]
  4. [ 10 8 30 583 128 106 75 38 10 12]
  5. [ 7 1 15 37 806 9 43 77 5 0]
  6. [ 6 5 18 214 76 586 32 59 2 2]
  7. [ 3 3 23 58 65 7 825 11 4 1]
  8. [ 5 2 12 56 73 24 11 811 0 6]
  9. [ 39 30 11 18 18 5 11 5 847 16]
  10. [ 30 61 3 22 9 2 7 8 16 842]]

Confusion matrix vizualizing

420

Time of learning process: 2h 15min

  • epochs = 50 accuracy=75.32%

Keras Training Accuracy vs Validation Accuracy
Keras Training Loss vs Validation Loss

Confusion matrix result:

  1. [[727 8 53 25 39 5 4 13 82 44]
  2. [ 7 821 6 10 5 2 4 2 22 121]
  3. [ 46 0 652 66 107 43 40 30 9 7]
  4. [ 14 2 61 577 113 125 40 32 13 23]
  5. [ 7 0 46 39 812 15 20 49 6 6]
  6. [ 3 1 47 150 66 649 17 49 4 14]
  7. [ 1 3 49 69 80 27 751 2 9 9]
  8. [ 9 0 22 47 65 38 8 791 3 17]
  9. [ 32 19 8 21 15 1 6 8 859 31]
  10. [ 19 30 8 14 5 0 2 9 20 893]]

Confusion matrix vizualizing

450

Time of learning process: 5h 45min

  • epochs = 100 accuracy=67.06%

Keras Training Accuracy vs Validation Accuracy
Keras Training Loss vs Validation Loss

Time of learning process: 11h 10min

Confusion matrix result:

  1. [[599 5 74 98 55 14 12 9 117 17]
  2. [ 16 738 12 65 9 26 7 6 40 81]
  3. [ 31 0 523 168 136 86 33 14 9 0]
  4. [ 10 1 31 652 90 175 19 15 5 2]
  5. [ 6 0 34 132 717 55 16 31 9 0]
  6. [ 5 1 17 233 53 661 10 15 4 1]
  7. [ 2 1 39 157 105 48 637 3 7 1]
  8. [ 6 0 14 97 103 96 5 637 5 1]
  9. [ 41 7 28 84 19 18 6 4 783 10]
  10. [ 25 28 8 77 29 27 5 19 59 723]]

Confusion matrix vizualizing

4100

Resources

  1. Official Keras Documentation
  2. About Keras on Wikipedia
  3. About Deep Learning on Wikipedia
  4. Tutorial by Dr. Jason Brownlee
  5. Tutorial by Parneet Kaur
  6. Tutorial by Giuseppe Bonaccorso
  7. Open Source on GitHub

Grab the code or run project in online IDE