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

Source: Internet
Author: User
Tags random shuffle shuffle 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.

#生成一个modeldef __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-8import Cv2import osimport numpy as np# import various module components #from Keras.preprocessing.image Imagedatageneratorfrom keras.models Import sequentialfrom keras.layers.core import dense, dropout, Activation, Flattenfrom keras.layers.advanced_activations Import prelufrom keras.layers.convolutional import convolution2d, Maxpooling2d, Zeropadding2dfrom keras.optimizers import SGD, Adadelta, adagradfrom keras.utils import np_utils, generic_ Utilsimport NumPy as Npfrom PIL import imagefrom keras Import backend as k# number of statistical samples Def __getnum__ (path): Fm=os.listdir (P ATH) 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= "U    Int8 ") 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,labe l############### #开始建立CNN模型 ############### #生成一个modeldef __cnn__ (testdata,testlabel,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 ')) mo    Del.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 (' sigmoid ')) Model.add (Maxpooling2d (pool_size= (2, 2))) #第三个卷积层, 16 convolution cores per convolutional core size 3*3 #激活函数用tanh #采用maxpooling, poolsize to (2,2 ) #model. Add (convolution2d (3, 3, border_mode= ' valid ')) #model. Add (Activation (' Tanh ')) #model. Add (Maxpoolin     G2d (Pool_size= (2, 2)) Model.add (Flatten ()) Model.add (Dense (init= ' normal ')) Model.add (Activation (' sigmoid ')) #Softmax分类, the output is 4 category Model.add (Dense (4, init= ' Normal)) Model.add (Activation (' Softmax ')) ############# #开始训练模型 ######### ##### #使用SGD + momentum Impulse SGD = SGD (lr=0.05, decay=1e-6, momentum=0.9, Nesterov=true) #model. Compile parameter loss is the loss letter. Number (target function) model.compile (loss= ' binary_crossentropy ', optimizer=sgd,metrics=[' accuracy ']) #开始训练, show_accuracy is displayed after each iteration  Correct rate.      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_accur acy=true) ############ #主模块 ########### #trainpath = '/home/emmons/carband_resize/train/' Testpath = '/home/emmons/ carband_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 required format binary class matrices, conversion, directly call Keras provides this function TestLabel = np_utils.to_categorical (TestLabel, 4) Trainlabel = Np_ Utils.to_categorical (Trainlabel, 4) __cnn__ (TestData, TestLabel, Traindata, Trainlabel)



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

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.