Abstract: This article mainly through Tensorflow+Opencv to achieve CNN custom image classification case, it can solve the problem of image classification in our real paper or practice, and compared with machine learning image classification algorithm experiment.

This article is shared from Huawei cloud community “Tensorflow+Opencv To Achieve CNN custom image Classification and comparison with KNN Image Classification”, author: Eastmount.

I. Image classification

Image Classification is the problem of Image content Classification. It makes use of computer to make quantitative analysis of the Image and divide the Image or the area in the Image into several categories to replace human visual judgment. The traditional method of image classification is feature description and detection, which may be effective for some simple image classification, but due to the actual situation is very complicated, the traditional classification method is overwhelmed. Nowadays, machine learning and deep learning methods are widely used to deal with image classification. The main task is to assign a bunch of input images to a label in a known mixed category.

In the figure below, the image classification model will acquire a single image, and it will be 4 labels {cat, dog, hat, mug}, corresponding to the probability {0.6, 0.3, 0.05, 0.05} respectively, where 0.6 represents the probability that the image label is cat, and the rest are analogous. The image is represented as a three-dimensional array. In this example, the cat image is 248 pixels wide, 400 pixels high and has red, green and blue color channels (commonly referred to as RGB). Thus, the image consists of 248×400×3 numbers or a total of 297,600 numbers, each of which is an integer ranging from 0 (black) to 255 (white). The task of image classification is to turn these nearly 300,000 numbers into a single label, such as “cat.”

So, how do you write an algorithm for image classification? And how do you tell a cat from a multitude of images? The approach here is similar to teaching a child how to see a picture, presenting a lot of image data and having the model continually learn the characteristics of each class. Before the training, the images of the training set need to be classified and labeled, as shown in the figure, including cat, dog, mug and hat. In a real project, there might be thousands of categories of objects, and each category would have millions of images.

Image classification is to input an array of pixel values of a bunch of images, and then assign a classification label to it. An algorithm model is established through training and learning, and then the model is used for image classification prediction. The specific process is as follows:

  • Input: Input contains a set of N images, each image label is one of K classification labels, this set is called the training set.
  • Learning: The second task is to use the training set to learn the characteristics of each class and build a training classifier or classification model.
  • Evaluation: The classification label of the newly input image is predicted by the classifier, and the quality of the classifier is evaluated accordingly. The quality of the classification algorithm is evaluated by comparing the predicted labels with the real image classification labels. If the classification label predicted by the classifier is consistent with the real classification label of the image, the prediction is correct; otherwise, the prediction is wrong.

Common classification algorithms include naive Bayes classifier, decision tree, K-nearest neighbor classification algorithm, support vector machine, neural network and rule-based classification algorithm, and ensemble learning algorithms for combining single class methods, such as Bagging and Boosting.

2. Image classification based on KNN algorithm

1. The KNN algorithm

K-nearest Neighbor Classifier algorithm is an instance – based classification method, which is one of the simplest and most commonly used methods in data mining classification technology. The core idea of the algorithm from the training samples for all the training samples and testing samples in the distance X (Euclidean distance) before the recent K samples (as similarity), sample selection and classification for the smallest distance again as X K K sample most nearby, and detect the K most belong to which kind of sample, sample thought the test sample classification samples belong to this category.

Assuming that the circular pattern in the figure below belongs to the category of triangle or square, KNN algorithm is used for analysis as follows:

  • When K=3, the first circle in the figure contains three figures, including two triangles and one square. The classification result of the circle is triangle.
  • When K=5, the second circle contains 5 figures, including 2 triangles and 3 squares, and the circle is predicted to be the square class standard with the voting result of 3:2. Different K values may predict different results.

In short, a sample has the same category as most of the k contiguous samples in the dataset. It can be seen from its idea that KNN classifies by measuring the distance between different characteristic values, and only refers to the category of k “neighbor” samples around the sample when deciding the sample category. Therefore, it is suitable for processing scenarios with more overlapping sample sets, mainly used for predictive analysis, text classification, dimensionality reduction and other processing.

KNN achieve in Sklearn machine learning packages, classes are neighbors. KNeighborsClassifier, hereinafter referred to as KNN algorithm. The construction method is:

KNeighborsClassifier(algorithm='ball_tree', 
    leaf_size=30, 
    metric='minkowski',
    metric_params=None, 
    n_jobs=1, 
    n_neighbors=3, 
    p=2, 
    weights='uniform')
Copy the code

You can configure three algorithms for KNeighborsClassifier, such as BRUTE, kd_tree, and ball_tree. Set the K value to n_neighbors=3. The call method is as follows:

  • from sklearn.neighbors import KNeighborsClassifier
  • KNN = KNeighborsClassifier (n_neighbors = 3, algorithm = “ball_tree”)

It consists of two steps:

  • Training: NBRS.fit (data, Target)
  • Prediction: pre = CLF. Predict (data)

2. The data set

This section mainly uses the Scikit-learn package for Python image classification. The SciKit-Learn extension package, often abbreviated to Sklearn, is a classic, useful extension for Python data mining and data analysis. Machine learning models in SciKit-Learn are very rich, including linear regression, decision tree, SVM, KMeans, KNN, PCA, etc. Users can choose the appropriate model of the extension package according to the type of specific analysis problem, so as to conduct data analysis. PIP install scikit-learn is used to implement the installation.

The data set used in the experiment is Sort_1000pics data set, which contains 1000 pictures and is divided into 10 categories in total. They are people (category 0), beaches (category 1), buildings (category 2), trucks (category 3), dinosaurs (category 4), elephants (category 5), flowers (category 6), horses (category 7), mountains (category 8) and food (category 9). As shown in the figure.

Then divide all kinds of images into folders named “0” to “9” according to the corresponding class label, as shown in the figure, each folder contains 100 images corresponding to the same category.

For example, folder “6” contains 100 images of flowers, as shown below.

3.KNN image classification

The following is the complete code for image classification by calling KNN algorithm. It randomly divides 1000 images into 70% training set and 30% test set, then obtains the pixel histogram of each image, and conducts image classification analysis according to the feature distribution of pixels. The core code of KNeighborsClassifier() is as follows:

  • from sklearn.neighbors import KNeighborsClassifier
  • clf = KNeighborsClassifier(n_neighbors=11).fit(XX_train, y_train)
  • predictions_labels = clf.predict(XX_test)

The complete code and comments are as follows:

# -*- coding: utf-8 -*- import os import cv2 import numpy as np from sklearn.cross_validation import train_test_split from sklearn.metrics import confusion_matrix, Classification_report # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # first step Segmentation of training set and testing set # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- X = name Y = [] [] # define image For f in OS. Listdir ("photo/%s" % I): X.append("photo//" + STR (I) + "//" + STR (f)).append(I) X = Np.array (X) Y = Np.array (Y X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=1) print len(X_train), len(X_test), len(y_train), Len (y_test) # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the second step # image read and converted to pixel histogram # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # training set XX_train = [] for I X_train in: Img = cv2.resize(image, (256,256)) #print image = cv2.imread(I) Hist = cv2.calchist ([img], [0,1], None, [256,256], [0.0,255.0,0.0,255.0]) xx_train.appEnd (((hist/255).flatten())) # test set XX_test = [] for I in X_test: Img = cv2.resize(image, (256,256)) #print image = cv2.imread(I) Hist = cv2.calchist ([img], [0,1], None, [256,256], [0.0, 255.0, 0.0, 255.0]) XX_test. Append (((hist / 255). Flatten ())) # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the third step # image classification based on KNN processing #---------------------------------------------------------------------------------- from sklearn.neighbors import KNeighborsClassifier clf = KNeighborsClassifier(n_neighbors=11).fit(XX_train, Predictions_labels = clf.predict(XX_test) print U 'Print predictions_labels print U [classiFICation_report (y_test, predictionS_labels)] Print X_test[k] print predictions_labels[k] print predictions_labels[k] image) cv2.waitKey(0) cv2.destroyAllWindows() k = k + 1Copy the code

The first ten images of the prediction set are displayed in the code, among which “818.jpg” image is shown in the figure. The standard result of classification prediction is “8”, indicating the eighth peak, and the prediction result is correct.

The following figure shows the image “452.jpg”. The standard result of its classification prediction is “4”, indicating the fourth category dinosaur, and the prediction result is correct.

The image “507.jpg”, shown below, predicted a category of “7” and wrongly predicted a category of “7” dinosaurs, which should have turned out to be a category of “5” elephants.

KNN algorithm was used for image classification experiment, and the final accuracy, Recall and F1-score of algorithm evaluation were shown in the figure, where the average accuracy was 0.64, the average Recall rate was 0.55, and the average F value was 0.50. The results were not very ideal. Then, if CNN convolutional neural network is used for classification, can the accuracy be improved by constantly learning details?

Tensorflow+Opencv realize CNN image classification

First, we need to install the OpenCV extension package in TensorFlow. Secondly, CNN neural network needs to be built through TensorFlow environment. Finally, the image classification experiment is realized through continuous learning.

1. Install OpenCV library

First, open the Anaconda program and select the installed TensorFlow environment to run Spyder.

In the second step, we need to install the Opencv-Python extension package in the TensorFlow environment, otherwise the error “ModuleNotFoundError: No module named ‘cv2′” will be displayed. Call Anaconda Prompt to install, as shown below:

activate tensorflow
pip install opencv-python
Copy the code

The following figure shows the successful installation.

But because Anaconda’s.org server is abroad and downloading is slow, Anaconda An HTTP error occurred when trying to retrieve this URL.HTTP errors are often intermittent.

  • Solution 1: download from the mirror of tsinghua University in China
  • Solution 2: Download the corresponding OpencV-Python version from the PYPI website, and install the. WHL file downloaded from the local PC. Download: www.lfd.uci.edu/~gohlke/pyt…

Since the first method has always failed, we recommend you to try the second method and upload the “opencv_python-4.1.2-cp36-cp36m-win_amd64. WHL” file for direct use. (4.1.2 represents the version of opencv, cp36 represents python3.6, and is 64-bit).

Third, call PIP to install the local OpencV extension pack.

Activate Tensorflow PIP install C:\Users\xiuzhang\Desktop\ tensorflow \opencv_python-4.1.2-cp36-cp36m-win_amd64.whlCopy the code

This method is very quick and recommended. Once installed, start writing our code!

2. Read the folder image

The specific steps are as follows:

  • Define the function read_img() to read images “0” through “9” in the folder “photo”
  • Call the cv2.imread() function to loop through all the pixel values of each image and change them uniformly to 32*32 by cv2.resize()
  • Get image pixel, image class label and image path name in sequence: fpaths, data, label = read_img(path)
  • The order of images was randomly adjusted and the data set was divided on a 2-8 scale, with 80% of the data used for training and 20% for testing

# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the first step Read image -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- def read_img (path) : cate = [path + x for x in os.listdir(path) if os.path.isdir(path + x)] imgs = [] labels = [] fpath = [] for idx, For im in glob.glob(Folder + '/*.jpg'): #print('reading the images:%s' % (im)) img = cv2.imread(im) #print('reading the images:%s' % (im)) img = cv2.imread(im) #print('reading the images:%s' % (im)) img = cv2.imread(im) #print('reading the images:%s' % (im)) img = cv2.imread(im) Imgs.append (img) # Image datagrams. Append (IDx) # fpath.append(path+im) # image pathname #print(path+im, Idx) return np.asarray(fPATH, np.string_), nP.asarray (IMGS, NP.float32), nP.asarray (Labels, NP.int32) # data, label = read_img(path) print(data.shape) # (1000, 256, 256, Num_classes = len(set(label)) print(num_classes) num_example = data.shape[0] arr = Random.shuffle (arr) data = data[arr] label = label[arr] fpaths = fPaths [arr] # Split training set and test set Ratio = 0.8s = np.int(num_example * ratio) x_train = data[:s] y_train = label[:s] fpaths_train = fpaths[:s] x_val = data[s:] y_val = label[s:] fpaths_test = fpaths[s:] print(len(x_train),len(y_train),len(x_val),len(y_val)) #800 800 200 200 print(y_val)Copy the code

3. The structures, CNN

The specific steps are as follows:

  • Placeholder is Placeholder, which is used to pass in input values. Xs means 32*32 pixels and contains three RGB layers, so the size is set to 32*32 *3. Ys represents the final predicted class index value for each image.

  • Call tf.layers.conv2d() to define the convolution layer, including 20 convolution kernels, the size of the convolution kernel is 5, and the excitation function is Relu. Call tf.layers.max_pooling2d() to define the pooling process with a step of 2 and a doubling of the size.

  • Then define the second convolution layer and the pooling layer, now have conv0, pool0 and conv1, pool1.

  • The full connection layer is defined by the tF.layers.dense () function, which converts to a feature vector of length 400 and adds DropOut to prevent overfitting.

  • The output layer is logits, including 10 numbers, and the final prediction result is predicted_labels, that is, tF.arg_max (logits, 1).

    # — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — the second step Establish neural networks — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

    Define Placeholder

    Xs = tf.placeholder(tf.int32, [None, 32, 3]) # 1 output per sample

    A container for storing DropOut parameters

    Drop = tf.placeholder(tF.float32) # 0.25 for training and 0 for test

    Define convolution layer conv0

    Conv0 = tf.layers.conv2d(xs, 20, 5, activation=tf.nn.relu) #20 convolutional kernels the convolutional kernels size is 5 relu activation

    Define the max-pooling layer pool0

    Pool0 = tf. Layers. Max_pooling2d (conv0, [2, 2], [2, 2]) print(“Layer0: \n”,

    Define convolution layer conv1

    Conv1 = tf.layers.conv2d(pool0, 40, 4, activation=tf.nn.relu) #40 convolution kernels convolution kernels size 4 relu activation

    Define the max-pooling layer pool1

    Pool1 = tf. Layers. Max_pooling2d (conv1, [2, 2], [2, 2]) print(“Layer1: \n”, conv1, pool1)

    Convert 3-dimensional features to 1-dimensional vectors

    flatten = tf.layers.flatten(pool1)

    The full connection layer is converted into a feature vector of length 400

    Fc = tf.layers. Dense (flatten, 400, activation=tf.nn.relu) print(“Layer2: \n”, fc)

    DropOut is added to prevent overfitting

    dropout_fc = tf.layers.dropout(fc, drop)

    Inactive output layer

    Dense (dropout_fc, num_classes) print(“Output: \n”, logits)

    Defining the output

    predicted_labels = tf.arg_max(logits, 1)

4. Define loss functions and optimizers

Loss is defined by cross entropy and deep learning is carried out by AdamOptimizer. The core code is as follows.

One-hot data is also known as one-bit effective encoding, which mainly uses the N-bit status register to encode N states. Each state is independent of its register bits, and only one bit is effective at any time. For example, [0 0 0 1 0 0 0 0 0 0 0 0…] Represents “animal”.

Losses = tf.nn. Softmax_cross_entropy_with_logits (labels = tf.one_hot(ys, num_classes), Mean_loss = tf.reduce_mean(losses) # Define optimizer learning efficiency set to 0.0001 optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(losses)Copy the code

5. Model training and prediction

Define the token variable train, which performs the training operation and saves the training model when it is True; When it is False, the prediction is made, and 20% of the prediction set is used for image classification prediction experiment.

# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the fourth step training and prediction -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # model is used to save and load the saver = tf. Train. Saver () # model file path model_path = "model/image_model" with tf.session () as sess: if train: Print (" training mode ") # Train the initialization parameter sess.run(tf.global_variables_initializer()) # Define the input and Label to fill the container while training. Dropout is 0.25 train_feed_dict = {xs: x_train, ys: y_train, drop: 0.25} # for step in range(1000): _, mean_loss_val = sess.run([optimizer, mean_loss], feed_dict=train_feed_dict) if step % 50 == 0: Print ("step = {}\t mean loss = {}". Format (step, mean_loss_val)) # Save model saver.save(sess, Model_path) print(" training finished, save model to {}". Format (model_path)) else: Print (" test mode ") # test the load parameter saver.restore(sess, Format (model_path) print(" load model from {} ". Format (model_path)) label_name_dict = {0: "human ", 1:" beach ", 2: "building ", 3: } # Define input and Label to fill the container. Dropout = 0 test_feed_dict = {xs: x_val, ys: y_val, drop: Predicted_labels_val = sess.run(predicted_labels, feed_dict=test_feed_dict) for fpath, real_label, predicted_label in zip(fpaths_test, y_val, predicted_labels_val): Real_label_name = label_name_dict[real_label] predicted_label_name = label_name_dict[predicted_label] print("{}\t{} => {}".format(fpath, real_label_name, Sum (y_val==predicted_labels_val) print(sum(y_val==predicted_labels_val) print( 1.0 * sum (y_val = = predicted_labels_val)/len (y_val))Copy the code

6. Complete code and experimental results

The complete code is shown below. Here we refer to part of the code of Teacher Wang Shiye. We strongly recommend you to learn from his blog. Address: blog.csdn.net/wills798

""" Created on Sun Dec 29 19:21:08 2019 @author: Xiuzhang Eastmount CSDN "" import OS import glob import Cv2 import numpy as NP import tensorflow as TF # 'photo / # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the first step Read image -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- def read_img (path) : cate = [path + x for x in os.listdir(path) if os.path.isdir(path + x)] imgs = [] labels = [] fpath = [] for idx, For im in glob.glob(Folder + '/*.jpg'): #print('reading the images:%s' % (im)) img = cv2.imread(im) #print('reading the images:%s' % (im)) img = cv2.imread(im) #print('reading the images:%s' % (im)) img = cv2.imread(im) #print('reading the images:%s' % (im)) img = cv2.imread(im) Imgs.append (img) # Image datagrams. Append (IDx) # fpath.append(path+im) # image pathname #print(path+im, Idx) return np.asarray(fPATH, np.string_), nP.asarray (IMGS, NP.float32), nP.asarray (Labels, NP.int32) # data, label = read_img(path) print(data.shape) # (1000, 256, 256, Num_classes = len(set(label)) print(num_classes) num_example = data.shape[0] arr = Random.shuffle (arr) data = data[arr] label = label[arr] fpaths = fPaths [arr] # Split training set and test set Ratio = 0.8s = np.int(num_example * ratio) x_train = data[:s] y_train = label[:s] fpaths_train = fpaths[:s] x_val = data[s:] y_val = label[s:] fpaths_test = fpaths[s:] print(len(x_train),len(y_train),len(x_val),len(y_val)) #800 800 200 200 print(y_val) # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the second step Establish neural networks -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - # define Placeholder xs = Tf.placeholder (tF.int32, [None, 32, 32, 3]) [None]) # drop = tf.placeholder(tF.float32) # 0.25 at training time 0 at test time tf.layers.conv2d(xs, 20, 5, Pool0 pool0 = tf.layers.max_pooling2D (conv0, [2, 2], [2, 2]) #pooling window = 0 print("Layer0: Conv2 = tf. Conv2. Conv2d (pool0, 40, 4) Pool1 pool1 = tf.layers.max_pooling2D (conv1, [2, 2], [2, 2]) #pooling = 1 print("Layer1: \n", conv1, Flatten (pool1) # Convert the 3-dimensional feature to 1-dimensional vector flatten = tf. Layers. Flatten (pool1) # Convert the full-connected feature vector FC = tf. The activation = tf. Nn. Relu) print (" Layer2: \n", FC) # Add DropOut to prevent overfitting Dropout_fc = tf.layout.dropout (FC, drop) # DropOut = tf.layout.dense (dropout_fc, fc) # DropOut = tf.layout.dense (dropout_fc, FC) Num_classes) print (" the Output: \n", logits) # predicted_labels = tf.arg_max(logits, 1) # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the third step definition loss function and optimizer -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- losses = # using cross entropy definition loss tf.nn.softmax_cross_entropy_with_logits( labels = tf.one_hot(ys, num_classes), Mean_loss = tf.reduce_mean(losses) # Define optimizer learning efficiency set to 0.0001 optimizer = Tf. Train. AdamOptimizer (learning_rate = 1-4 e). Minimize (losses) # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the fourth step Training and prediction -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # model is used to save and load the saver = tf. The train. The saver () the "train" = False # # training or prediction model file path model_path = "model/image_model" with tf.Session() as sess: if train: Print (" training mode ") # Train the initialization parameter sess.run(tf.global_variables_initializer()) # Define the input and Label to fill the container while training. Dropout is 0.25 train_feed_dict = {xs: x_train, ys: y_train, drop: 0.25} # for step in range(1000): _, mean_loss_val = sess.run([optimizer, mean_loss], feed_dict=train_feed_dict) if step % 50 == 0: Print ("step = {}\t mean loss = {}". Format (step, mean_loss_val)) # Save model saver.save(sess, Model_path) print(" training finished, save model to {}". Format (model_path)) else: Print (" test mode ") # test the load parameter saver.restore(sess, Format (model_path) print(" load model from {} ". Format (model_path)) label_name_dict = {0: "human ", 1:" beach ", 2: "building ", 3: } # Define input and Label to fill the container. Dropout = 0 test_feed_dict = {xs: x_val, ys: y_val, drop: Predicted_labels_val = sess.run(predicted_labels, feed_dict=test_feed_dict) for fpath, real_label, predicted_label in zip(fpaths_test, y_val, predicted_labels_val): Real_label_name = label_name_dict[real_label] predicted_label_name = label_name_dict[predicted_label] print("{}\t{} => {}".format(fpath, real_label_name, Sum (y_val==predicted_labels_val) print(sum(y_val==predicted_labels_val) print( 1.0 * sum (y_val = = predicted_labels_val)/len (y_val))Copy the code

The training output is as follows:

(1000, 32, 32, 3) 10 800 800 200 200 [2 8 6 9 9 5 2 2 9 3 7 0 6 0 1 3 2 7 3 4 6 9 5 8 6 4 1 1 4 4 8 6 1 2 50 7 9 5 2 4 6 8 7 5 8 8 8 8 8 8 1 6 5 1 4 8 1 9 1 8 8 6 5 3 1 2 9 1 8 7 6 0 8 1 8 0 2 1 3 5 3 6 9 8 8 7 5 2 5 2 8 8 8 8 4 2 2 4 5 3 3 3 9 1 1 5 2 6 7 6 7 0 7 4 1 7 2 9 4 0 3 8 7 5 3 8 1 9 3 6 8 0 0 1 7 7 9 5 4 0 3 4 5 2 2 3 0 8 2 6 8 6 5 5 Layer0: Tensor("conv2d_1/Relu:0", shape=(? , 28, 28, 20), dtype=float32) Tensor("max_pooling2d_1/MaxPool:0", shape=(? Dtype =float32) Layer1: Tensor("conv2d_2/Relu:0", shape=(? , 11, 11, 40), dtype=float32) Tensor("max_pooling2d_2/MaxPool:0", shape=(? Dtype =float32) Layer2: Tensor("dense_1/Relu:0", shape=(? Tensor("dense_2/BiasAdd:0", shape=(? , 10), Dtype =float32) Training mode step = 0 mean loss = 66.93688201904297 step = 50 mean loss = 3.376957654953003 step = 100 mean loss = 0.5910811424255371 step = 150 mean loss = 0.061084795743227005 step = 200 mean loss = 0.013018212281167507 step = 250 Mean loss = 0.006795921362936497 step = 300 mean loss = 0.004505819175392389 step = 350 mean loss = 0.0032660639844834805 step = 400 mean loss = 0.0024683878291398287 step = 450 mean loss = 0.0019308131886646152 step = 500 mean loss = 0.001541870180517435 step = 550 mean loss = 0.0012695763725787401 step = 600 mean loss = 0.0010685999877750874 step = 650 mean loss = 0.0009132082923315465 step = 700 mean loss = 0.0007910516578704119 step = 750 mean loss = 0.0006900889566168189 step = 800 mean loss = 0.0006068988586775959 step = 850 mean loss = 0.0005381597438827157 Step = 900 mean loss = 0.0004809059901162982 Step = 950 mean loss = 0.0004320790758356452 Save the model to model/image_modelCopy the code

The prediction output results are shown in the figure below. Finally, 181 images were correctly predicted, with an accuracy of 0.905. Compared with the previous machine learning KNN of 0.500 is a very high improvement.

Test mode INFO: tensorflow: Restoring the parameters from the model/image_model load model from the model/image_model b 'photo/photo / 3 \ \ 335 JPG' bus = > Photo /photo/1\\129.jpg' beach => beach B 'photo/photo/7\\740. JPG 'wild horse => wild horse B 'photo/photo/5\\564.jpg' elephant => Elephant... JPG 'food => food B 'photo/photo/2\\220.jpg' building => bus B 'photo/photo/9\\912.jpg' food => food B 'photo/photo/4\\459.jpg' dinosaur => dinosaur B 'photo/photo/5\\525.jpg' elephant => elephant B 'photo/photo/0\\44.jpg' human => Number of correct predictions by human: 181 Accuracy is 0.905Copy the code

4. To summarize

More TensorFlow deep learning articles will continue to be shared. Meanwhile, experimental evaluation, RNN, LSTM and cases of various majors will be further explained. Finally, I hope this basic article will be helpful to you, if errors or deficiencies exist in the article, also please burke ~ as a rookie of artificial intelligence, I hope I can progress and thorough, the subsequent it was applied to image recognition, network security, counter sample, and other fields, to guide you to write a simple academic papers, together come on!

References:

[1] Gonzales. Digital Image Processing (3rd Edition) [M]. Beijing: Publishing House of Electronics Industry, 2013. [2] Yang Xiuzhang, YAN Na. Python Network Data crawling and analysis from beginner to Master (Analysis) [M]. Beijing: Beihang University Press, 2018. [3] Luo Zijiang et al. Image Processing in Python [M]. Science Press, 2020. [4] [Python Data Mining course] twentieth. [5] TensorFlow [minimal] CNN-Yellow_python [6] Sound source localization based on deep neural Network for directional Activation function development – Zhang Zi Ju Kevin [7] TensorFlow: Chapter 5 (CNN-3- Classical Convolutional Neural Network (GoogleNet) – DFann [8] github.com/siucaan/CNN… [9] Interpretation of image processing – Taking CNN image classification as an example – Bing Hui [10] Image defect classification based on CNN – BellaVita1 [12] TensorFlow (vi) Training to classify their own images (CNN Super detailed Introduction edition) – Missayaa [13] Tensorflow training its own dataset to achieve CNN image classification – Wang Shiye (Strong Push) [14] github.com/hujunxianli… [15] TensorFlow (三) : Image Classification using CNN – FlowRush [16] TensorFlow Image Recognition (Object Classification) Tutorial – Cococok2 [17] github.com/calssion/Fu… [18] [19] CNN Image Single tag Classification (Based on TensorFlow implementation basic VGG16 network) [20] github.com/siucaan/CNN… [21] Tensorflow implements CNN for MNIST recognition – Siucaan (Push) [22] Installs Tensorflow, OpencV, using Anaconda3 to enable it to run in Spyder

Click to follow, the first time to learn about Huawei cloud fresh technology ~