[PYTHON] [Windows] Library Keras course where you can try Deep Learning immediately-Part 1

Introduction

This post is for people who want to try deep learning, which has become popular in recent years, without knowing the theory, or who just want to use it. I wrote it for my own memo, so please point out any confusing parts or mistakes. Keras is a Python wrapper for Tensorflow and Theano, a deep learning library. By using this library, you can experience deep learning very easily, and you can code many network structures without being aware of difficult theories. In this article, we will introduce Tensorflow and Keras and start by identifying rectangles with a multi-layer perceptron.

(Addition) Since it seems that many people have seen it, we also added identification by reading the learned parameters. I hope it will be helpful to everyone.

(Added on 2017/03/09) TensorFlow 1.0 was released by Google on February 15, 2017. Along with this, a new tf.keras module will be introduced in TensorFlow. (For now, it seems that it will be added in TensorFlow 1.2.) Therefore, you will be able to use Keras API directly from TensorFlow without running TensorFlow on the backend using Keras as before. I'm wondering where to write the next article, but I'm probably thinking of writing this series in pure Keras. If TensorFlow also supports Keras, I think that it will be uploaded to Gist by referring to the code of TensorFlow.

Execution environment

Execution environment installation procedure

Installation of Anaconda

I'm using Anaconda as the execution environment for python on Windows. If you already have an environment in which you are using it, you can skip it. To install Anaconda, go to the official Download Page (http://www.continuum.io/downloads#windows) and download the installer for your environment. After that, run the installer and click Next to complete. Along with the installation of Anaconda, the editors called Python (Python3 in my case) and jupyter notebook selected at the time of download are also included, so there is no need to install python separately. anaconda2.png

Install Tensorflow

In Keras, you can select the library to run on the backend with Tensorflow or Theano, but here we will use Tensorflow. Since pip is used for installation, first search for Anaconda from the start screen and start it. anaconda_pronpt2.png Immediately after installation, update pip to the latest version just in case. To get the latest version, run the following command in Anaconda Prompt.

pip_upgrade


$ pip install --upgrade pip

Once you have the latest version, install Tensorflow. With the Anaconda prompt open, run the following command: Please note that the commands at the time of installation are different between a PC with a GPU and a PC with only a CPU.

tensorflow_install


#For CPU only
$ pip install tensorflow
#If you have a GPU
$ pip install tensorflow-gpu

#How to check if it is installed
#Execute the following command, and if tensorflow is in the list, it's OK.
$ pip list |grep tensorflow

(Addition) (As of March 8, 2017) Tensorflow cannot be installed with the above command unless the Python version is 3.5.x. I wrote an article that summarizes how to avoid this, so if you can not install it, please install Tensorflow referring to it. The following is the article. How to install the deep learning framework Tensorflow 1.0 in Anaconda environment of Windows

Install Keras

Then, execute the following command to complete the installation of Keras.

Keras_install


#Keras installation
$ pip install keras

#Check if it is installed
#Similarly, if you can confirm Keras, it is OK.
$ pip list |grep Keras

Check if it can be installed

Before we get into the main subject, let's check if the installation is correct. I think that the Anaconda prompt is open, so start python there and check if it can be imported properly. It's okay if it looks like this:

introduction_confirmation


#Start python
$ python
#Import tensorflow
>>> import tensorflow 
#Keras import
>>> import keras
using TensorFlow backend

Multi-value classification using multi-layer perceptron (MLP)

Classification of quadrangle horizontally or vertically

Dataset download

Let's do a simple classification by inputting the following image. First of all, data is required for deep learning, so please download the data from Site .. rectangle2.png This dataset is the following rectangular image dataset. So, as a starting point, let's use Keras to classify into two classes, portrait or landscape.

rectangleImage.png

programming

For how to use Jupyter, please refer to here for easy understanding. Now, about the procedure of the program, it is roughly as follows.

  1. Read data for learning
  2. Formatting data into a learnable form (preprocessing)
  3. Building the network layer
  4. Learning
  5. Performance evaluation with test data
  6. Display a part of the result on the screen and check
  7. Export architecture and weights

Once you've learned it, you can read and use the exported architecture and weights from the second time, so you don't need 3 or 4. When implemented, the code looks like this: The execution results etc. are uploaded to Gist, so please check there if necessary.

rectangle_mlp.py


import numpy as np
from scipy.misc import toimage
from keras.utils import np_utils
import matplotlib.pyplot as plt

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation

#Rectangle data read function
def load_rectangles_data(filename):
    data = np.loadtxt(filename)
    #Extraction of image data
    x = data[:,0:784]
    #Extraction of correct label
    y = data[:,784]
    return [x,y]

#parameter settings
nb_classes = 2
epoch = 20
batch = 10

#label:"Horizontal":Horizontal,"Vertical":Vertical
label = ["Horizontal","Vertical"]

#Reading square training data
[X_train,y_train] = load_rectangles_data('rectangles_train.amat')

#Read square test data
[X_test,y_test] = load_rectangles_data('rectangles_test.amat')

#Change the label to an array corresponding to the number of classes
#Example: y_train:[0 1 0 0] -> Y_train:[[1 0],[0 1],[1 0],[1 0]]
Y_train = np_utils.to_categorical(y_train,nb_classes)
Y_test = np_utils.to_categorical(y_test,nb_classes)

#Networking of Multilayer Perceptron
#Input 784 dimensions(28x28)And set the final output to the number of classes
model = Sequential()
model.add(Dense(512, input_dim=784, init='uniform'))
model.add(Activation('sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(512, init='uniform'))
model.add(Activation('sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, input_dim=512, init='uniform'))
model.add(Activation('softmax'))

#Binary is selected because it is a binary classification, and RMSprop is selected as the optimization algorithm.
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.fit(X_train, Y_train,
          nb_epoch=epoch,
          batch_size=batch)

#Evaluation of created model and weight using test data
score = model.evaluate(X_test, Y_test, batch_size=batch)
#This time the correct answer rate is 92%
print(score)
#Predict some labels of test data
classified_labels = model.predict_classes(X_test[0:10,:].reshape(-1,784))

#Specify the size to display
plt.figure(figsize=(20,10))
for i in range(10):
    plt.subplot(2,5,i+1)
    #Convert to image data
    img = toimage(X_test[i].reshape(28,28))
    plt.imshow(img, cmap='gray')
    plt.title("Class {}".format(label[classified_labels[i]]),fontsize=20)
    plt.axis('off')
plt.show()

#Export model and weight parameters
model_json = model.to_json()
open('rectangles_architecture.json', 'w').write(model_json)
model.save_weights('rectangles_weights.h5', overwrite=True)

result

If you adjust the parameters such as the number of epochs, the number of batches, and the optimization algorithm appropriately, it seems that the correct answer rate is about 92% with this simple classification. The results of some test data are displayed. You can see that they are classified correctly. result.png

Classification using trained data

It still takes time to learn. Therefore, in practical use, the method is to save the learned weights in advance and use them. As for what to do specifically, I think that it is saved with the extension JSON and h5 at the end of the script, so just load it. it's simple. In the above, we classified the test data, but I can't trust that any data can be classified. Therefore, I prepared the following four pieces of 56-pixel x 56-pixel square data as appropriate for painting. Let's see if this can be classified. If you look at the results, you can see that they are classified correctly. result3.png The Python script used to load and classify is: The results of the Jupyter Notebook are also posted on Gist, so please check that if you need it.

rectangle_load_learned_parameter.py


import numpy as np
import matplotlib.pyplot as plt

from keras.utils import np_utils

from keras.models import Sequential,model_from_json
from keras.layers import Dense, Dropout, Activation
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input, decode_predictions

#A function that binarizes the array values to 0 and 1 by specifying a threshold
# array:Two-dimensional array, threshold:Threshold
def array2d_binarization(array,threshold):
    (row,column) = array.shape
    for i in range(row):
        for j in range(column):
            if(array[i,j] > threshold):
                array[i,j] = 1
            else:
                array[i,j] = 0
    return array

#Number of images prepared for the test
test_num = 4

#The meaning of the identified label
# "Horizontal":Horizontal,"Vertical":Vertical
label = ["Horizontal","Vertical"]

#Model loading
model = model_from_json(open('rectangles_architecture.json').read())
#Load model weights
model.load_weights('rectangles_weights.h5')

for s in range(test_num):
    #Specifying the file name
    img_path = "rectangle" + str(s)+".jpg "
    #28 pixels x 28 pixels, import images in grayscale
    img = image.load_img(img_path, grayscale=True, target_size=(28, 28))
    #Convert the imported image to an array
    x = image.img_to_array(img)
    #Checking the array size
    print(x.shape)
    
    #Binarize image data with threshold 50, x to give 28x28 as an argument[:,:,0]To
    x = array2d_binarization(x[:,:,0],50)
    #Format a 28x28 2D array into a vector of size 784 for input
    x = x.reshape(-1,784)

    #Class prediction
    classified_label = model.predict_classes(x)
    
    #Prediction result plot
    plt.subplot(1,test_num,s+1)
    plt.imshow(img, cmap='gray')
    plt.title("Class {0}".format(label[classified_label[0]]),fontsize=10)
    plt.axis('off')
plt.show()

in conclusion

I would like to continue to write articles on how to use Keras in several parts. Next time, I will talk about a little more complicated classification. If there are any mistakes, we will correct them, so please point them out.

reference

-Tensorflow Official Documentation-Installation -Keras Official Document-Installation -Keras Official Document-Sequential Model Guide -Rectangle Dataset -Jupyter Beginning -Announcement of TensorFlow 1.0

Recommended Posts

[Windows] Library Keras course where you can try Deep Learning immediately-Part 1
[Windows] A library where you can try Deep Learning immediately Keras course-Part 2
Build an environment on windows10 where you can try MXNet
Deep learning course that can be crushed on site
Sine wave prediction using RNN in deep learning library Keras
Site summary where you can learn machine learning for free
[Free to use] 7 learning sites where you can study Python
Try deep learning with TensorFlow
<Course> Deep Learning: Day2 CNN
<Course> Deep Learning: Day1 NN
Try Deep Learning with FPGA
With deep learning, you can exceed 100% recovery rate in horse racing
Deep Learning Model Lightening Library Distiller
Try Deep Learning with FPGA-Select Cucumbers
Try deep learning with TensorFlow Part 2
Microsoft's Deep Learning Library "CNTK" Tutorial
(python) Deep Learning Library Chainer Basics Basics
Coursera's TensorFlow introductory course to get you started with Deep Learning implementations
I tried using the trained model VGG16 of the deep learning library Keras
School service (free / paid) where you can learn programming language Python and artificial intelligence technology (machine learning / deep learning)