Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement CNN with Keras

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to achieve CNN in Keras. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

After installing Tensorflow, after installing Keras, TF is used as the back end by default. The code for Keras to implement convolution network is very simple, and the callback class in keras provides a method to detect variables in the process of model training, which can adjust the learning efficiency and some parameters of the model in time according to the detection of variables.

In the following example, MNIST data is used as a test

Import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.image as pimgimport seaborn as sb # A drawing module built on matplotlib Support numpy,pandas and other data structures such as% matplotlib inlinefrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import confusion_matrix # obfuscation matrix import itertools# kerasfrom keras.utils import to_categorical # numeric tags into one-hot encoding from keras.models import Sequentialfrom keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPool2Dfrom keras.optimizers import RMSpropfrom keras.preprocessing.image import ImageDataGeneratorfrom keras.callbacks import ReduceLROnPlateauUsing TensorFlow backend.# settings painting style sb.set (style='white', context='notebook' Palette='deep') # load data train_data = pd.read_csv ('data/train.csv') test_data = pd.read_csv (' data/test.csv') # train_x = train_data.drop (labels= ['label'], axis=1) # remove the label column train_x = train_data.iloc [:, 1:] train_y = train_data.iloc [: 0] del train_data # release memory # observe the distribution of training data g = sb.countplot (train_y) train_y.value_counts () 1 46847 44013 43519 41882 41776 41370 41324 40728 40635 3795Name: label, dtype: int64

Train_x.isnull (). Describe () # check whether there is a true value train_x.isnull (). Any (). Describe () count 784unique 1top Falsefreq 784dtype: objecttest_data.isnull (). Any (). Describe () count 784unique 1top Falsefreq 784dtype: object# normalized train _ x = train_x/255.0test_x = test_data/255.0del test_data

Convert the shape of the data

# reshape trian_x, test_x#train_x = train_x.values.reshape (- 1,28,28,1) # test_x = test_x.values.reshape (- 1,28,28,1) train_x = train_x.as_matrix (). Reshape (- 1,28,28,1) test_x = test_x.as_matrix (). Reshape (- 1,28,28,1) # convert the tag column to one-hot encoding format train_y = to_categorical (train_y) Num_classes = 10)

Separate the validation data from the data

# 1/10 of the training data is separated as verification data random_seed = 3train_x, val_x, train_y, val_y = train_test_split (train_x, train_y, test_size=0.1, random_state=random_seed)

A training sample

Plt.imshow (train_x [0] [:,:, 0])

Using Keras to build CNN

Model = Sequential () # first convolution layer, 32 convolution cores, size 5x5 Convolution mode SAME, activation function relu, the size of the input tensor model.add (Conv2D (filters= 32, kernel_size= (5Power5), padding='Same', activation='relu',input_shape= (28pc28) model.add (Conv2D (filters= 32, kernel_size= (5Power5), padding='Same', activation='relu') # pooling layer, pooled core size 2x2model.add (MaxPool2D (pool_size= (2) # randomly discards 1/4 of the network connections Prevent over-fitting model.add (Dropout (0.25)) model.add (Conv2D (filters= 64, kernel_size= (3mag3), padding='Same', activation='relu')) model.add (Conv2D (filters= 64, kernel_size= (3), padding='Same', activation='relu')) model.add (MaxPool2D (pool_size= (2)), strides= (2) model.add (Dropout (0.25) # full connection layer, start the operation Model.add (Flatten ()) # number of neurons in the hidden layer added and activation function model.add (Dense (256, activation='relu')) model.add (Dropout (0.25)) # output layer model.add (Dense (10, activation='softmax')) # set optimizer # lr: learning efficiency, decay: attenuation of lr optimizer = RMSprop (lr = 0.001, decay=0.0) # compilation model # loss: loss function Metrics: corresponding performance evaluation function model.compile (optimizer=optimizer, loss = 'categorical_crossentropy',metrics= [' accuracy'])

Create an instance of the callback class

# keras's callback class provides the ability to track target values and dynamically adjust learning efficiency # moitor: the amount to be monitored, here is the verification accuracy # matience: when after three rounds of iterations, the monitored target amount remains unchanged, the learning efficiency will be adjusted # verbose: information display mode, to zero or zero factor: a factor that reduces the learning rate each time, the learning rate will be reduced in the form of lr = lr*factor # mode:'auto' One of the min','max', in min mode, if the detected value triggers a reduction in the learning rate. In max mode, the reduction of learning rate is triggered when the detection value no longer increases. # epsilon: threshold, used to determine whether to enter the "plain" # cooldown: after the learning rate is reduced, it will take cooldown epoch to resume normal operation # min_lr: the lower limit of the learning rate learning_rate_reduction = ReduceLROnPlateau (monitor = 'val_acc', patience = 3, verbose = 1, factor=0.5, min_lr = 0.00001) epochs = 40batch_size = 100

Data enhancement processing

# enhance data processing to improve the generalization ability of the model It can also effectively avoid overfitting of the model # rotation_range: angle of rotation # zoom_range: randomly scaled image # width_shift_range: ratio of horizontal movement to image width # height_shift_range # horizontal_filp: horizontal reversal # vertical_filp: reversal in the vertical axis data_augment = ImageDataGenerator (rotation_range= 10 zoomorphic range = 0.1, width_shift_range = 0.1 Height_shift_range = 0.1, horizontal_flip = False, vertical_flip = False)

Training model

History = model.fit_generator (data_augment.flow (train_x, train_y, batch_size=batch_size), epochs= epochs, validation_data = (val_x,val_y), verbose = 2, steps_per_epoch=train_x.shape [0] / / batch_size Callbacks= [learning _ rate_reduction]) Epoch 1Acer 40359s-loss: 0.4529-acc: 0.8498-val_loss: 0.0658-val_acc: 0.9793Epoch 2 val_loss 40375s-loss: 0.1188-acc: 0.9637-val_loss: 0.0456-val_acc: 0.9848Epoch 3acc 40374s-loss: 0.0880-acc: 0.9734-val_loss: 0.0502-val_acc: 0.9845Epoch 4bat 40375s -loss: 0.0750-acc: 0.9767-val_loss: 0.0318-val_acc: 0.9902Epoch 50.9888Epoch 40374s-loss: 0.0680-acc: 0.9800-val_loss: 0.0379-val_acc: 0.9888Epoch 60.9888Epoch 40369s-loss: 0.0584-acc: 0.9823-val_loss: 0.0267-val_acc: 0.9910Epoch 7par 40381s-loss: 0.0556-acc: 0.9832-val_loss: 0.0505-val_acc: 0. 9824Epoch 8pex 40381s-loss: 0.0531-acc: 0.9842-val_loss: 0.0236-val_acc: 0.9912Epoch 9ape 40376s-loss: 0.0534-acc: 0.9839-val_loss: 0.0310-val_acc: 0.9910Epoch 10val_acc 40379s-loss: 0.0537-acc: 0.9848-val_loss: 0.0274-val_acc: 0.9917Epoch 11 Universe 40375s-loss: 0.0501-acc: 0.9856-val_loss: 0.0254-val_ Acc: 0.9931Epoch 12val_loss 40382s-loss: 0.0492-acc: 0.9860-val_loss: 0.0212-val_acc: 0.9924Epoch 13val_loss 40380s-loss: 0.0482-acc: 0.9864-val_loss: 0.0259-val_acc: 0.9919Epoch 14Charger 40373s-loss: 0.0488-acc: 0.9858-val_loss: 0.0305-val_acc: 0.9905Epoch 15/40Epoch 00014: reducing learning rate to 0.0005000023749.370s-loss: 0.0493-acc : 0.9853-val_loss: 0.0259-val_acc: 0.9919Epoch 16 val_loss 40367s-loss: 0.0382-acc: 0.9888-val_loss: 0.0176-val_acc: 0.9936Epoch 17 val_loss 40376s-loss: 0.0376-acc: 0.9891-val_loss: 0.0187-val_acc: 0.9945Epoch 18acc 40376s-loss: 0.0410-acc: 0.9885-val_loss: 0.0220 val_acc: 0.9926Epoch 1940371s-loss: 0 . 0385-acc: 0.9886-val_loss: 0.0194-val_acc: 0.9933Epoch 20 apprentice 40372s-loss: 0.0345-acc: 0.9894-val_loss: 0.0186-val_acc: 0.9938Epoch 21/40Epoch 00020: reducing learning rate to 0.000250000011874.375s-loss: 0.0395-acc: 0.988-val_loss: 0.0233-val_acc: 0.9945Epoch 22 Universe 40369s-loss: 0.0313-acc: 0.9907-val_loss: 0.0141 -val_acc: 0.9955Epoch 23 val_acc 40376s-loss: 0.0308-acc: 0.9910-val_loss: 0.0187-val_acc: 0.9945Epoch 24Charger 40374s-loss: 0.0331-acc: 0.9908-val_loss: 0.0170-val_acc: 0.9940Epoch 25Charger 40372s-loss: 0.0325-acc: 0.9904-val_loss: 0.0166-val_acc: 0.9948Epoch 26/40Epoch 00025: reducing learning rate to 0.000125000005937.373s-loss: 0. 0319-acc: 0.9904-val_loss: 0.0167-val_acc: 0.9943Epoch 27 acc 40372s-loss: 0.0285-acc: 0.9915-val_loss: 0.0138-val_acc: 0.9950Epoch 28val_loss 40375s-loss: 0.0280-acc: 0.9913-val_loss: 0.0150-val_acc: 0.9950Epoch 29/40Epoch 00028: reducing learning rate to 6.25000029686e-05.377s-loss: 0.0281-acc: 0.9924-val_loss: 0. 0158-val_acc: 0.9948Epoch 30val_loss 40374s-loss: 0.0265-acc: 0.9920-val_loss: 0.0134-val_acc: 0.9952Epoch 31 loss 40378s-loss: 0.0270-acc: 0.9922-val_loss: 0.0128-val_acc: 0.9957Epoch 32val_loss 40372s-loss: 0.0237-acc: 0.9930-val_loss: 0.0133-val_acc: 0.9957Epoch 33Unip 40375s-loss: 0.0237-acc: 0.9931-val_ Loss: 0.0138-val_acc: 0.9955Epoch 34gamer 40371s-loss: 0.0276-acc: 0.9920-val_loss: 0.0135-val_acc: 0.9962Epoch 35lem40373s-loss: 0.0259-acc: 0.9920-val_loss: 0.0136-val_acc: 0.9952Epoch 36gamble 40369s-loss: 0.0249-acc: 0.9924-val_loss: 0.0126-val_acc: 0.9952Epoch 37Unip 40370s-loss: 0.0257-acc: 0. 9923-val_loss: 0.0130-val_acc: 0.9960Epoch 38/40Epoch 00037: reducing learning rate to 3.12500014843e-05.374s-loss: 0.0252-acc: 0.9926-val_loss: 0.0136-val_acc: 0.9950Epoch 39Ma40372s-loss: 0.0246-acc: 0.9927-val_loss: 0.0134-val_acc: 0.9957Epoch 40371s-loss: 0.0247-acc: 0.9929-val_loss: 0.0139-val_acc: 0.9950

During the training process, the condition of learning efficiency decline is triggered several times. Whenever the val_acc does not grow for three consecutive rounds, the learning efficiency will be adjusted to half of the current one. After adjustment, the val_acc will increase obviously, but in the last few rounds, the model may have converged.

# learning curvesfig,ax = plt.subplots (2 history.history ['loss'], color='r', label='Training Loss') ax [0] .plot (history.history [' val_loss'], color='g', label='Validation Loss') ax [0] .plot (loc='best',shadow=True) ax [0] .grid (True) ax [1] .plot (history.history ['acc'], color='r') Label='Training Accuracy') ax [1] .plot (history.history ['val_acc'], color='g', label='Validation Accuracy') ax [1] .plot (loc='best',shadow=True) ax [1] .grid (True)

# confusion matrix def plot_sonfusion_matrix (cm, classes, normalize=False, title='Confusion matrix',cmap=plt.cm.Blues): 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) if normalize: cm = cm.astype ('float') / cm.sum (axis=1) [: Np.newaxis] thresh = cm.max () / 2.0for ithresh else j in itertools.product (range (cm.shape [0]), range (cm.shape [1])): plt.text (jpime iLECH cm [iLJ], horizontalalignment='center',color='white' if cm [iLJ] > thresh else 'black') plt.tight_layout () plt.ylabel (' True label') plt.xlabel ('Predict label')

Confusing proof of verification data

Pred_y = model.predict (val_x) pred_label = np.argmax (pred_y, axis=1) true_label = np.argmax (val_y, axis=1) confusion_mat = confusion_matrix (true_label, pred_label) plot_sonfusion_matrix (confusion_mat, classes = range (10))

About Keras how to achieve CNN to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report