"Deep learning" simply uses Keras to make car logos.

Source: Internet
Author: User
Tags random shuffle shuffle valid in python keras

The content of a simple experiment lesson.

First, the size of the given sample material is 32*32, which can be done in Python batch and OpenCV function resize (), where I do not list the code.


List some of the pictures that are well-shrunk.


Then in the use of Keras CV convolutional neural network model, before doing this experiment, the computer should be configured Python+theano+keras environment.

#生成一个model
def __cnn__ (Testdata,testlabel,traindata,trainlabel):
    model = sequential ()

    #第一个卷积层, 4 convolution cores, Each convolutional core size is 5*5. 1 represents the channel of the imported picture, and the grayscale image is 1 channels.
    Model.add (convolution2d (5, 5, border_mode= ' valid ', input_shape= (1,32,32))) 
    Model.add (Activation (' Sigmoid ')
    model.add (Maxpooling2d (pool_size= (2, 2)))

    #第二个卷积层, 30 convolution cores per convolutional core size 5*5.
    #采用maxpooling, Poolsize (2,2)
    Model.add (convolution2d (5, 5, border_mode= ' valid '))
    Model.add ( Activation (' sigmoid '))
    Model.add (Maxpooling2d (pool_size= (2, 2)))

    #第三个卷积层, 16 convolution cores per convolutional core size 3*3
    # The activation function
    is #采用maxpooling with Tanh, Poolsize (2,2)
    #model. Add (convolution2d (3, 3, border_mode= ' valid ')) 
    # Model.add (Activation (' Tanh '))
    #model. Add (Maxpooling2d (pool_size= (2, 2))

    Model.add (Flatten ()
    ) Model.add (Dense (init= ' normal '))
    Model.add (Activation (' sigmoid '))

    #Softmax分类, output is 4 category
    Model.add (Dense (4, init= ' normal '))
    Model.add (Activation (' Softmax '))

CNN tuning parameters is a very troublesome thing, the above layers of the parameters and the number of layers I refer to LENET-5 and then debug themselves out. I was not going to do this with Keras, it was done on Caffe, after several adjustments to the convolution kernel size, the number of model layers, the number of output neurons, and so on. Finally ran out on the caffe the result is 98% also good. The effect on the Keras is more ideal.


Another part of Keras's convolutional neural network is how to turn a picture sample into a qualified sample for training, specifically a two-column XY, where the x column is a four-dimensional array x (X1,X2,X3,X4). x1 represents the number of samples, X2, which represents the channel value of the picture, RGB or grayscale; x3,x4 represents the matrix of pixel points of the picture, generally uniform size (this is why the first reason to divide into 32*32, happy words divided into 64*64 line).

The y column is labeled with the image that corresponds to the X column. The function code is divided as follows:

#统计样本的数目
def __getnum__ (path):
    fm=os.listdir (path)
    i=0 for
    F in FM:
        ff= os.listdir (path+f+ '/')
        for N in FF:
            i+=1
    return i      

#生成X, y-column
def __data_label__ (path,count): 
    data = Np.empty ((count, 1,32,32), dtype= "float32")
    label = Np.empty ((count,), dtype= "Uint8")
    i=0;
    Filename= Os.listdir (path) for
    ff in filename:
        fi=os.listdir (path+ff+ "/') for
        F in fi:
            img = Cv2.imread (path+ff+ '/' +f,0)
            arr = Np.asarray (img,dtype= "float32")
            Data[i,:,:,:] = arr
            label[i]=int ( FF)
            i+=1
    return Data,label

The code is written well, each parameter is adjusted well, in the virtual machine or directly call terminal to run the program, you can get the results of training.


(In my view, in terms of speed, Keras is far better than Caffe.) The accuracy rate is also very high. I raised the number of parameters at Cafe The final accuracy rate is also 98%, but the same model placed under the Keras is only a few wrong or even good. The reason, I do not know, but also to continue to study hard AH Qaq)

Here is the source code for this experiment:

# coding=utf-8 Import cv2 import OS import numpy as NP #导入各种用到的模块组件 #from keras.preprocessing.image Import Imagedatagener Ator from keras.models import sequential from keras.layers.core import dense, dropout, Activation, Flatten from Keras.laye
Rs.advanced_activations Import Prelu from keras.layers.convolutional import convolution2d, maxpooling2d, zeropadding2d From keras.optimizers import SGD, Adadelta, Adagrad from keras.utils import np_utils, generic_utils import numpy as NP fro  M PIL import Image from Keras import backend as K #统计样本的数目 def __getnum__ (path): Fm=os.listdir (path) i=0 for F in fm:ff= Os.listdir (path+f+ '/') for N in Ff:i+=1 return i #生成X, y-column def __data_
    Label__ (path,count): data = Np.empty ((count,1,32,32), dtype= "float32") label = Np.empty ((count,), dtype= "Uint8")
    i=0; Filename= Os.listdir (path) for FF in Filename:fi=os.listdir (path+ff+ "/') for F in Fi:im g = Cv2.imread (Path+ff+ '/' +f,0) arr = Np.asarray (img,dtype= "float32") Data[i,:,:,:] = arr Label[i]=int (FF) i+=1 return Data,label ############### #开始建立CNN模型 ############### #生成一个model def __cnn__ (testdata,t Estlabel,traindata,trainlabel): Model = sequential () #第一个卷积层, 4 convolution cores, each convolution core size 5*5.
    1 represents the channel of the imported picture, and the grayscale image is 1 channels.
    Model.add (convolution2d (5, 5, border_mode= ' valid ', input_shape= (1,32,32))) Model.add (Activation (' sigmoid '))
    Model.add (Maxpooling2d (pool_size= (2, 2))) #第二个卷积层, 30 convolution cores, each convolutional core size 5*5. #采用maxpooling, Poolsize (2,2) Model.add (convolution2d (5, 5, border_mode= ' valid ')) Model.add (Activation (' Sigmo Id ')) model.add (Maxpooling2d (pool_size= (2, 2))) #第三个卷积层, 16 convolution cores per convolutional core size 3*3 #激活函数用tanh #采用maxpooling, Poolsi Ze (2,2) #model. Add (convolution2d (3, 3, border_mode= ' valid ')) #model. Add (Activation (' Tanh ')) #model. Add (Maxpooling2d (Pool_size= (2, 2)) Model.add (Flatten ()) Model.add (dense(init= ' normal ')) Model.add (Activation (' sigmoid ')) #Softmax分类, the output is 4 categories Model.add (Dense (4, init= ' normal ')) Model.add (Activation (' s Oftmax ')) ############# #开始训练模型 ############## #使用SGD + momentum Impulse SGD = SGD (lr=0.05, decay=1e-6, Mom entum=0.9, Nesterov=true) #model. The parameter loss in compile is the loss function (objective function) Model.compile (loss= ' binary_crossentropy ', optimizer  =sgd,metrics=[' accuracy ') #开始训练, the show_accuracy shows the correct rate after each iteration.  
    Batch_size is the number of samples that are brought into the training each time, Nb_epoch is the number of iterations, and shuffle is a random shuffle of the sample. Model.fit (Traindata, Trainlabel, Batch_size=16,nb_epoch=15,shuffle=true,verbose=1,show_accuracy=true,validation_ Data= (TestData, TestLabel)) #设置测试评估参数, with test set sample Model.evaluate (TestData, TestLabel, BATCH_SIZE=16,VERBOSE=1,SHOW_ACCU racy=true) ############ #主模块 ############ trainpath = '/home/emmons/carband_resize/train/' Testpath = '/home/emmons/c arband_resize/test/' testcount=__getnum__ (testpath) traincount=__getnum__ (trainpath) testdata,testlabel= __data_ label__ (tEstpath, Testcount) traindata,trainlabel= __data_label__ (Trainpath, Traincount) #label为0 to a total of 4 categories, Keras requires binary format Class matrices, convert it, call the function provided by Keras directly TestLabel = Np_utils.to_categorical (TestLabel, 4) Trainlabel = Np_utils.to_ Categorical (Trainlabel, 4) __cnn__ (TestData, TestLabel, Traindata, Trainlabel)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.