In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use Keras to complete the CNN model building, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
For the task of image classification, convolution neural network (CNN) is the best network structure at present. CNN is widely used in facial recognition, autopilot, object detection and other fields, and has achieved the best performance. For the vast majority of deep learning novices, the digital handwriting recognition task may be the first project to get started, and the network is also full of all kinds of mature toolkits related code. novices can get good results immediately after running the program using the relevant toolbox, and there is only one feeling-deep learning is amazing, but they don't really understand the specific flow of the whole algorithm. The editor will use Keras and TensorFlow to design a simple two-dimensional convolution neural network (CNN) model, hand-in-hand to teach you to use code to complete the MNIST number recognition task, easy to understand the whole process of deep learning.
Prepare data
The model uses MNIST data set, which is the largest digital handwritten data set (0,9). It contains a total of 60000 training images and 10000 test images. The size of each image is 28x28, grayscale. The first step is to load the dataset, which can be done through Keras API:
# the source code cannot be downloaded directly. Make a slight modification here Specify the path after downloading the dataset # download link: https://pan.baidu.com/s/1jH6uFFC password: dw3dfrom _ _ future__ import print_functionimport kerasimport numpy as np path='./mnist.npz' f = np.load (path) X_train, y_train = f ['x _ perfect'], f ['y _ perfect'] X_test, y_test = f ['x _ blank test'], f ['y _ test']
In the above code, X_train represents the training data set, with a total of 60000 28x28-sized handwritten images, and y_train represents the corresponding label of the training image. Similarly, X_test represents the test data set, with a total of 10000 28x28-sized handwritten images, and y_test represents the label corresponding to the test image. The following is to visualize some of the data in the dataset in order to better understand the purpose of the built deep learning model.
Import matplotlib.pyplot as pltfig = plt.figure () for i in range (9): plt.subplot plt.tight_layout () plt.imshow (X_train [I], cmap='gray', interpolation='none') plt.title ("Digit: {}" .format (y _ blank [I]) plt.xticks ([]) plt.yticks ([]) fig
As you can see from the figure, the upper-left corner is the handwritten image stored in the training set X_train [0]. The corresponding label'5' is represented by the '5percent relegated yearly train [0]. The function of the whole deep learning model is to predict exactly what other people's handwritten numbers are after training.
For neural networks, it is generally necessary to preprocess the original data. The common preprocessing methods are adjusting image size, normalizing pixel values and so on. # let's print the actual data shape before we reshape and normalizeprint (X_train shape, X_train.shape) print ("y_train shape", y_train.shape) print ("X_test shape", X_test.shape) print ("y_test shape", y_test.shape) # input image size 28*28img_rows, img_cols = 28, 28#reshaping# "channels_first" assumes (channels, conv_dim1, conv_dim2 Conv_dim3). X_train = X_train.reshape (X_train.shape [0], img_rows, img_cols, 1) X_test = X_test.reshape (X_test.shape [0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1) # more reshapingX_train = X_train.astype ('float32') X_test = X_test.astype (' float32') X_train / = 255X_test / = 255print ('X_train shape:' X_train.shape) # X_train shape: (60000, 28, 28, 1)
After the necessary processing of the image information, the tag data y_train and y_test are converted into a classification format (in vector form), that is, the tag'3' is converted into a vector [0mem0mem0lmag0] for modeling, and the position where the label vector is non-zero (starting from 0) indicates the specific label of the image, that is, if the label vector of the image is not 0 at subscript 5, then the image represents the number'4'.
Import keras#set number of categoriesnum_category = 1percent convert class vectors to binary class matricesy_train = keras.utils.to_categorical (y_train, num_category) y_test = keras.utils.to_categorical (y_test, num_category) build and compile model
After the data is ready to be provided to the model, the architecture of the model needs to be defined and compiled using the necessary optimization functions, loss functions, and performance indicators.
The architecture of the model is classical convolution neural network, which contains two convolution layers respectively, and then connects the full connection layer and softmax classifier. If you are not familiar with the role of each floor, it is recommended to take the CS231 course.
After the maximum pool layer and the full connection layer, dropout is introduced into the model as regularization to reduce the over-fitting problem.
# Import the structure of related layers from _ _ future__ import print_functionimport kerasfrom keras.datasets import mnistfrom keras.models import Sequentialfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2Dfrom keras import backend as kimport matplotlib.pyplot as pltimport numpy as np## model buildingmodel = Sequential () # convolutional layer with rectified linear unit activationmodel.add (Conv2D (32, kernel_size= (3,3), activation='relu') Input_shape=input_shape)) # 32 convolution filters used each of size 3x3#againmodel.add (Conv2D (64, (3,3), activation='relu')) # 64 convolution filters used each of size 3x3#choose the best features via poolingmodel.add (MaxPooling2D (pool_size= (2,2) # randomly turn neurons on and off to improve convergencemodel.add (Dropout (0.25)) # flatten since too many dimensions, we only want a classification outputmodel.add (Flatten ()) # fully connected to get all relevant datamodel.add (Dense Activation='relu')) # one more dropout for convergence' sake:) model.add (Dropout (0.5)) # output a softmax to squash the matrix into output probabilitiesmodel.add (Dense (num_category, activation='softmax'))
After the model is built, it needs to be compiled. Categorical_crossentropy multi-classification loss function is used in this paper. Because all tags have similar weights, they are used as performance indicators, and AdaDelta gradient descent technique is used to optimize the model parameters.
# Adaptive learning rate (adaDelta) is a popular form of gradient descent rivaled only by adam and adagrad#categorical ce since we have multiple classes (10) model.compile (loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta (), metrics= ['accuracy']) training and evaluation model
After defining and compiling the model schema, you need to use training data to train the model so that you can recognize handwritten numbers. Even X_train and y_train are used to fit the model.
Batch_size= 128num_epoch = 10#model trainingmodel_log = model.fit (X_train, y_train, batch_size=batch_size, epochs=num_epoch, verbose=1, validation_data= (X_test, y_test))
Epoch represents a forward propagation process and a back propagation process for all training samples, and Batch_Size represents the number of training samples processed each time the forward process and reverse process are processed. The training output is as follows:
The performance of the model needs to be evaluated after training: score = model.evaluate (X_test, y_test, verbose=0) print ('Test loss:', score [0]) # Test loss: 0.0296396646054print (' Test accuracy:', score [1]) # Test accuracy: 0.9904
It can be seen that the accuracy of the test is as high as 99%, which means that the model is well trained for prediction. Visualize the whole process of training and testing, that is, draw the accurate curve and loss function curve of training and testing, as shown below. As can be seen from the figure, with the increase of the number of training iterations, the loss and accuracy of the training and test data of the model tend to be consistent, and the model finally tends to be stable.
Save model parameters
After the model is trained, the trained parameters need to be saved so that it can be called directly next time. The architecture or structure of the model will be stored in the json file, and the weights will be stored in the hdf5 file format.
# Save the model# serialize model to JSONmodel_digit_json = model.to_json () with open ("model_digit.json", "w") as json_file: json_file.write (model_digit_json) # serialize weights to HDF5model.save_weights ("model_digit.h6") print ("Saved model to disk")
Therefore, the saved model can be reused later or easily migrated to other application scenarios.
The above is how to use Keras to complete the CNN model building, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.