Plant Recognition Module Implementation: Using MobileNetV2 for Image Classification
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).
Code Implementation:
(1) Importing Libraries 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.
from keras.models import load_model:Loads the load_model function from Keras for loading and restoring saved models.from keras.applications import MobileNet:Imports the MobileNet model from Keras as the base model for the image classifier.from keras.models import Sequential:Imports the Sequential model from Keras as the container for building the model.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.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.from PIL import Image:Imports the Image module from the PIL library for image processing.import numpy as np:Imports the NumPy library for array operations and numerical calculations.import json:Imports the JSON library for processing JSON formatted data.
(2) ImageClassifier Class Definition 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:
__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 theload_model()method is called to load the model, theload_class_index_dict()method is called to load the category label mapping, and theload_classname()method is called to load the category name mapping.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.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 inclass_index_dict. The category label mapping dictionary is returned.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 inclassname. The category name mapping dictionary is returned.predict_image(self, image_path):Method for image classification prediction. Receives an image path as a parameter. Firstly, the image is opened usingImage.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.
原文地址: http://www.cveoy.top/t/topic/og1l 著作权归作者所有。请勿转载和采集!