[PYTHON] Tree disease determination by image recognition using CNTK and SVM

Overview

I am manually determining the disease of trees, but the beginning of the story is whether the work can be automated due to lack of manpower. Plant diseases appear in various forms, but I tried to create by machine learning whether it is possible to diagnose dead trees as a starting point. Since it is in the area of image recognition, I think that it is a common method to extract features by image processing and configure a classifier with logic like SVM. However, such a method depends on the method of feature extraction, and problems are likely to occur in the subsequent development. Under such circumstances, I decided to use Deep Learning. However, it takes a lot of time to learn from scratch. Therefore, I will try using a technique called transfer learning. Deep Learrning is roughly divided into the part where the feature is extracted and the part where it is classified. The last fully connected layer just before entering the classifier is believed to be the most characteristic. It is a method that can be used for recognition of arbitrary images by taking out the final fully connected layer and applying it to a classifier. neuro.png

ResNet There is ResNet developed by Microsoft, which is said to be the model with the highest recognition rate now. The final fully connected layer of ResNet has 512 parameters. If only the classifier is trained using the 512 parameters generated from the trained image, it is not necessary to train the model from the beginning, and it is possible to build an image recognition mechanism. The classifier does not have to be a Neural Network and can use machine learning classification techniques such as SVM and LightGBM. neuro2.png

Processing policy

  1. Extract the last fully connected layer from the ResNet model
  2. Learn the classifier from the extracted features and labels

For the first full bond extraction, the CNTK sample comes with a Feasture Extraction. Use it to extract features. This is FeatureExtraction.py in CNTK2.0-rc1 \ cntk \ Examples \ Image \ FeatureExtraction.

Ready to move

Explicitly use the GPU. Add the bottom two lines to the beginning of the file.

from cntk.io import MinibatchSource, ImageDeserializer, StreamDefs, StreamDef
from cntk.device import set_default_device, gpu

Also, it seems that the specification of the return type from some models has changed from Beta.

def eval_and_write(model_file, node_name, output_file, minibatch_source, num_objects):

Within the above definition out_values = output[0,0].flatten()

However, it does not work with this, so Modify it below.

out_values=np.array(output).flatten()

Also, add a device setting call to use the GPU in the following location.

if __name__ == '__main__':
    set_default_device(gpu(0))

It is now operational. After that, set the data reading First, download Resnet's Pretrained model. The data will also be downloaded, but the model will be downloaded by executing the following command.

cd .\CNTK2.0-rc1\Examples\Image\FeatureExtraction
Python install_data_and_model.py

There is a text.txt file in CNTK-2.0-rc1 \ cntk \ Examples \ Image \ DataSets \ Grocery, and write a list of files to extract features here. Just change the location and name of the file and you're good to go. When you read the data in this program, you put it in the data conversion stream and resize it.

You need to change from using ImageNet's Pretrained Model to 224x224. If the original data is too large, it may take a long time to read or an error may occur, so I think it is better to arrange it in HD size (1920 x 1024) in advance.

def create_mb_source(map_file, image_width, image_height, num_channels, num_classes, randomize=True):
   transforms = [xforms.scale(width=image_width, height=image_height, channels=num_channels, interpolations='linear')]
   return MinibatchSource(ImageDeserializer(map_file, StreamDefs(
           features =StreamDef(field='image', transforms=transforms),
           labels   =StreamDef(field='label', shape=num_classes))),
           randomize=randomize)

Learning process

A layerOut.txt file is generated, and 512 features for each data image are output in CSV format. Withered label for each data image Withered: 1 Not withered: 0 Create a CSV file by adding it to the first column of layerOut.txt The test data is as layerOut2.txt

Learning with SVM Python code is below.

from sklearn import svm
import numpy as np

d_tmp = np.loadtxt('layerOut.txt', delimiter=',')
train_data = [x[1:] for x in d_tmp]
label = [int(x[0]) for x in d_tmp]

d_tmp_t = np.loadtxt('layerOut2.txt', delimiter=',')
test_data =[x[1:] for x in d_tmp_t]

treehelth =svm.LinearSVC(C=1.0)
treehelth.fit(train_data, label)
prediction = treehelth.predict(test_data)
print(prediction)

You can also use LightGBM developed by Microsoft for the SVM part. Learning will be faster if the amount of data is large.

The original image seems to have a problem, but the recognition rate is just under 70%. You probably need to tune the original data side. The image is one of the training data.

Recommended Posts

Tree disease determination by image recognition using CNTK and SVM
Image recognition using CNN Horses and deer
Machine Learning: Image Recognition of MNIST by using PCA and Gaussian Native Bayes
Similar face image detection using face recognition and PCA and K-means clustering
OS determination by Makefile using Python
Image recognition environment construction and basics
Image recognition of fruits using VGG16
Python: Basics of image recognition using CNN
Category estimation using docomo's image recognition API
Python: Application of image recognition using CNN
Image recognition model using deep learning in 2016
Face image inference using Flask and TensorFlow
Image analysis was easy using the data and API provided by Microsoft COCO.