4.3 Implementation of Plant Recognition Module

Recognition Process: The plant recognition model used in the application program is based on the MobileNetV2 architecture and implemented using the Keras framework. Before training, the pre-trained parameters are loaded into the model. To facilitate the prediction of plant species, additional layers, including global pooling layers and fully connected layers, have been added. The activation function used is 'softmax'. The weight parameters of the entire model are provided in TABLE1. The MobileNet layer is the main component of the model, specifically designed for image classification tasks. It adopts the MobileNet network architecture, accepts input images of size (224, 224, 3), where the height and width are 224 pixels, and the number of channels is 3. The model contains 54 convolutional layers, and uses depth-wise separable convolution to reduce the number of parameters and computational complexity while maintaining high accuracy. This approach ensures improved efficiency without compromising performance. MobileNetV2 also introduces an optimization residual module structure called InvertedResidual, which can enhance efficiency and accuracy. In this structure, the channel number in the convolutional layer is compressed and expanded, reducing the computational complexity and number of model parameters. The GlobalAveragePooling2D layer converts the output of the MobileNet layer into a fixed-length feature vector. It converts the feature map of shape (None, 7, 7, 1024) into a feature vector of shape (None, 1024). The layer does not contain any trainable parameters. The final Dense layer has 102 output units for the ultimate classification task. It receives input of shape (None, 1024) from the GlobalAveragePooling layer and generates output of shape (None, 102).

(1) First, some Keras libraries and classes are imported, as well as other necessary third-party libraries, to implement the image classifier and load the pre-trained MobileNet model.

  1. from keras.models import load_model: Loads the load_model function from Keras for loading and restoring saved models.
  2. from keras.applications import MobileNet: Imports the MobileNet model from Keras as the base model for the image classifier.
  3. from keras.models import Sequential: Imports the Sequential model from Keras as the container for building the model.
  4. from keras.layers import Dense, GlobalAveragePooling2D: Imports the Dense layer and GlobalAveragePooling2D layer from Keras to add fully connected layers and global average pooling layers to the model.
  5. from keras.models import model_from_json: Imports the model_from_json function from Keras to load the configuration information of the model from a JSON file.
  6. from PIL import Image: Imports the Image module from the PIL library for image processing.
  7. import numpy as np: Imports the NumPy library for array operations and numerical calculations.
  8. import json: Imports the JSON library for processing JSON formatted data. (2) A class named ImageClassifier is defined which uses the pre-trained MobileNet model for image classification prediction. By loading the model, category label mapping, category name mapping, and implementing the image prediction method, the image classification function is realized.

ImageClassifier class implementation process:

  1. init(self,model_config_path,model_weights_path, cat_to_label_path, cat_to_name_path): Initialization method that receives four parameters: model configuration file path, model weight file path, category to label mapping file path, and category to name mapping file path. During initialization, the received paths are saved to class member variables, and the load_model() method is called to load the model, the load_class_index_dict() method is called to load the category label mapping, and the load_classname() method is called to load the category name mapping.
  2. load_model(self): Method to load the model. Firstly, a pre-trained MobileNet model is created as the base model, and a custom model is built on top of it by adding a global average pooling layer and a fully connected layer. Then, the model configuration information is loaded using the model configuration file path, and the model weights are loaded using the model weight file path. Finally, the model is compiled using the Adam optimizer and cross-entropy loss function, and the loaded and compiled model object is returned.
  3. load_class_index_dict(self): Method to load the category label mapping. The category label mapping is loaded using the category to label mapping file path and stored in class_index_dict. The category label mapping dictionary is returned.
  4. load_classname(self): Method to load the category name mapping. The category name mapping is loaded using the category to name mapping file path and stored in classname. The category name mapping dictionary is returned.
  5. predict_image(self, image_path): Method for image classification prediction. Receives an image path as a parameter. Firstly, the image is opened using Image.open() from the PIL library, and its size is adjusted to 224x224 pixels. Then, the image is converted to a NumPy array and normalized. Next, the image array is extended by one dimension to meet the input requirements of the model. The model is used for image classification prediction, and the probability distribution of the predicted results is returned. Then, the index of the category with the highest probability in the predicted results is obtained. Based on the category index, the corresponding category label is obtained from the category label mapping, and the corresponding category name is obtained from the category name mapping. Finally, the category label and category name of the predicted result are returned
将下面一段话翻译成英文;43 植物识别模块实现识别流程:应用程序中使用的植物识别模型基于MobileNetV2架构使用Keras框架实现。在训练之前预训练的参数会被加载到模型中。为了方便对植物物种进行预测还添加了额外的层包括全局池化层和全连接层。激活函数使用的是softmax。整个模型的权重参数在TABLE1中提供。MobileNet层是模型的主要组件专门设计用于图像分类任务。它采用MobileN

原文地址: http://www.cveoy.top/t/topic/fsDg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录