Image recognition method, device, electronic device and readable storage medium
By using spatial consistency confirmation of global and local features in the image recognition model, the problem of large changes in feature changes when the target object pose changes is solved, and fast and accurate similar image recognition is achieved.
Patent Information
- Application Number
- CN202210182070.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-25
- Publication Date
- 2024-11-29
- Estimated Expiration
- 2042-02-25
AI Technical Summary
The existing image recognition technology changes greatly when the pose or shooting viewing angle of the target object of the image to be recognized, resulting in low recognition efficiency and accuracy.
Similar images are determined by inputting the image to be identified to the pre-trained image recognition model, global features and local features are output, and local features of candidate images are confirmed using spatial consistency.
The rapid and accurate identification of similar images of the image to be identified is achieved, and the recognition efficiency and accuracy are improved, especially the robustness when the viewing angle of the target object changes.
Smart Images

Figure CN114595352B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of image processing technology, and in particular to an image recognition method, device, electronic equipment and readable storage medium. Background Art
[0002] With the continuous development of information technology and artificial intelligence, image recognition has been widely used, such as identifying the same or similar product images in online shopping. At present, similar image retrieval is usually performed by extracting image features of the image to be identified.
[0003] However, when the posture or shooting angle of the target object in the image to be identified changes, the image features will also change significantly, resulting in low recognition efficiency and accuracy. Summary of the invention
[0004] The purpose of the embodiments of the present invention is to provide an image recognition method, device, electronic device and readable storage medium, which can quickly and accurately determine similar images of the image to be recognized. The specific technical solution is as follows:
[0005] In a first aspect of the present invention, an image recognition method is provided, comprising:
[0006] Obtain an image to be recognized;
[0007] The image to be identified is input into a pre-trained image recognition model, and the global feature and the first local feature of the image to be identified are output; the image recognition model is trained according to the sample global features, sample local features and object category labels of multiple sample images; the object category label is used to describe the category to which the object in the sample image belongs;
[0008] The image recognition model is used to extract the second local feature of the candidate image, where the candidate image is screened from a preset image library according to the global feature;
[0009] The candidate image corresponding to the second local feature that meets the spatial consistency of the first local feature is determined as a similar image to the image to be recognized.
[0010] In a second aspect of the present invention, there is also provided an image recognition device, comprising:
[0011] An acquisition module, used for acquiring an image to be recognized;
[0012] An input module is used to input the image to be identified into a pre-trained image recognition model, and output the global features and the first local features of the image to be identified; the image recognition model is trained according to the sample global features, sample local features and object category labels of multiple sample images; the object category label is used to describe the category to which the object in the sample image belongs;
[0013] The extraction module is further used to extract the second local feature of the candidate image by using the image recognition model, and the candidate image is screened from a preset image library according to the global feature;
[0014] The determination module is used to determine the candidate image corresponding to the second local feature that meets the spatial consistency of the first local feature as a similar image to the image to be identified.
[0015] In another aspect of the present invention, a computer-readable storage medium is provided, wherein instructions are stored in the computer-readable storage medium, and when the computer-readable storage medium is run on a computer, the computer executes any of the above-mentioned image recognition methods.
[0016] In another aspect of the present invention, a computer program product comprising instructions is provided, which, when executed on a computer, enables the computer to execute any of the above-mentioned image recognition methods.
[0017] The image recognition method provided by the embodiment of the present invention outputs the global features and the first local features of the image to be recognized by inputting the image to be recognized into a pre-trained image recognition model. Since the image recognition model is trained based on the sample global features, sample local features and object category labels of multiple sample images, the global features and the first local features of the image to be recognized can be accurately extracted. Then, multiple candidate images screened from a preset image library according to the global features are input into the image recognition model, and the second local features of each candidate image are output. Here, the second local features of the candidate images can also be accurately extracted by the image recognition model. Since the first local feature and the second local feature can both effectively represent the image features of the image to be recognized and the candidate image, the spatial consistency of the first local feature and the second local feature can be confirmed. If the spatial consistency is satisfied, the spatial transformation can be performed on each other, indicating that the two local features represent the features of the same target object, that is, the target object in the candidate image and the image to be recognized is consistent. Therefore, the candidate image corresponding to the second local feature that meets the spatial consistency of the first local feature is determined as a similar image of the image to be recognized, and the similar image of the image to be recognized can be quickly and accurately determined. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art are briefly introduced below.
[0019] Figure 1 is a flow chart of an image recognition method provided by an embodiment of the present invention;
[0020] Figure 2is a flow chart of a method for training an image recognition model provided by an embodiment of the present invention;
[0021] Figure 3 is a schematic diagram of an image recognition model structure provided by an embodiment of the present invention;
[0022] Figure 4 It is a schematic diagram of a training process and an application process of an image recognition model provided by an embodiment of the present invention;
[0023] Figure 5 is a schematic diagram of the structure of an image recognition device provided by an embodiment of the present invention;
[0024] Figure 6 It is a schematic diagram of the hardware structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0025] The technical solutions in the embodiments of the present invention will be described below in conjunction with the accompanying drawings in the embodiments of the present invention.
[0026] The image recognition method provided by the embodiment of the present invention can be applied to at least the following application scenarios, which are described below.
[0027] As image recognition has been widely used, such as in identifying images of the same item in online shopping, currently, the method of extracting image features of the image to be identified is usually used to retrieve images of the same or similar items. For example, a close-up of the upper body and a full-body image of a person wearing the same dress may have significant changes in the image features of the clothing area, resulting in the inability to correctly identify similar items in the product library, resulting in low recognition efficiency and accuracy.
[0028] Based on the above application scenarios, the image recognition method provided by the embodiments of the present invention is described in detail below.
[0029] The image recognition method provided by the embodiment of the present invention is described below.
[0030] Figure 1 The present invention provides a flowchart of an image recognition method.
[0031] like Figure 1 As shown, the image recognition method may include steps 110 to 140, and the method is applied to an image recognition device, as shown below:
[0032] Step 110: Obtain an image to be recognized.
[0033] Step 120, input the image to be identified into a pre-trained image recognition model, and output the global features and the first local features of the image to be identified; the image recognition model is trained based on the sample global features, sample local features and object category labels of multiple sample images; the object category label is used to describe the category to which the object in the sample image belongs.
[0034] Step 130 , using an image recognition model to extract a second local feature of a candidate image, where the candidate image is screened from a preset image library based on the global feature.
[0035] Step 140: determine the candidate image corresponding to the second local feature that meets the spatial consistency of the first local feature as a similar image to the image to be identified.
[0036] In an embodiment of the present invention, the image to be identified is input into a pre-trained image recognition model, and the global features and the first local features of the image to be identified are output. Since the image recognition model is trained based on the sample global features, sample local features and object category labels of multiple sample images, the global features and the first local features of the image to be identified can be accurately extracted. Then, multiple candidate images screened from a preset image library according to the global features are input into the image recognition model, and the second local features of each candidate image are output. Here, the second local features of the candidate images can also be accurately extracted by the image recognition model. Since the first local feature and the second local feature can effectively represent the image features of the image to be identified and the candidate image, the spatial consistency of the first local feature and the second local feature can be confirmed. If the spatial consistency is satisfied, the spatial transformation can be performed on each other, indicating that the two local features represent the features of the same target object, that is, the target object in the candidate image and the image to be identified is consistent. Among them, the spatial consistency refers to the spatial coordinates of the local feature points corresponding to the subsets of the first local feature of the image to be identified and the second local feature of the candidate image that meet the feature consistency (for example, feature similarity>0.8) meet the constraints such as perspective change for affine transformation. Therefore, determining the candidate image corresponding to the second local feature that meets the spatial consistency of the first local feature as a similar image to the image to be identified can quickly and accurately determine the similar image to the image to be identified.
[0037] The specific implementation methods of the above steps are introduced below.
[0038] This involves step 110 .
[0039] Get the image to be recognized.
[0040] The image to be identified includes a target object.
[0041] This involves step 120 .
[0042] The image to be identified is input into a pre-trained image recognition model, and the global feature and the first local feature of the image to be identified are output. The global feature is used to characterize the overall feature of the image to be identified, and the first local feature is used to characterize the local feature of the image to be identified.
[0043] The image recognition model is trained based on the sample global features, sample local features and object category labels of multiple sample images; the object category labels are used to describe the category to which the objects in the sample images belong. For example, if the sample images are clothing images, the object category labels corresponding to the clothing images may include shirts, skirts, pants, etc.
[0044] This involves step 130 .
[0045] The image recognition model is used to extract the second local features of the candidate images, and the candidate images are screened from a preset image library based on the global features.
[0046] Wherein, step 130 may specifically include the following steps:
[0047] According to the global features, candidate images are screened from a preset image library; multiple candidate images are input into an image recognition model, and the second local features of each candidate image are output.
[0048] Here, the preset image library can be roughly screened based on the global features to obtain candidate images that are similar to the image to be identified to a certain extent.
[0049] This involves step 140 .
[0050] The spatial consistency of the first local feature and the second local feature is confirmed, and the candidate image corresponding to the second local feature that meets the spatial consistency condition is determined as a similar image to the image to be identified.
[0051] Spatial consistency refers to the subset of the first local feature of the image to be identified and the second local feature of the candidate image that meets the feature consistency (for example, feature similarity>0.8), and the corresponding local feature point spatial coordinates meet constraints such as perspective change and affine transformation.
[0052] In a possible embodiment, step 140 includes:
[0053] Compare the first local feature and the second local feature to determine feature similarity;
[0054] According to the feature similarity, a first similar feature is extracted from the first local feature, and a second similar feature is extracted from the second local feature, wherein the feature similarity between the first similar feature and the second similar feature is greater than a preset threshold;
[0055] Verify whether the spatial coordinates of the first similar feature in the first local feature and the spatial coordinates of the second similar feature in the second local feature meet spatial consistency;
[0056] The candidate image corresponding to the second similar feature that meets the spatial consistency is determined as a similar image to the image to be identified.
[0057] First, the random sample consensus (RANSAC) method can be used to confirm the spatial consistency of the first local features and the second local features of the image to be identified and the candidate image, and the candidate image that meets the spatial consistency is determined as a similar image to the image to be identified.
[0058] Among them, RANSAC is an algorithm that calculates the mathematical model parameters of the data based on a set of sample data sets containing abnormal data to obtain valid sample data.
[0059] Comparing the first local feature with the second local feature to determine the feature similarity may specifically include: extracting a first effective feature of the first local feature and extracting a second effective feature of the second local feature according to a random sampling consensus algorithm. Comparing the first effective feature with the second effective feature to determine the feature similarity.
[0060] Among them, the first effective feature of the first local feature is extracted, and the second effective feature of the second local feature is extracted. Specifically, the size of the local feature corresponding to an image can be HxWxD, wherein each spatial position (y, x) corresponds to a D-dimensional local feature, and the size of the local feature response map corresponding to an image is HxWx1, and the local feature of the spatial position whose response exceeds a preset threshold is considered to be a valid local feature; the preset threshold can be set to a typical value based on experience, such as 0.8 or 0.9.
[0061] Then, according to the feature similarity, the first similar feature is extracted from the first local feature, and the second similar feature is extracted from the second local feature, and the feature similarity between the first similar feature and the second similar feature is greater than a preset threshold. That is, some local features whose feature similarity is greater than a preset threshold are extracted from the local features of the image to be identified and the candidate image.
[0062] Therefore, the local features of the image to be identified and the candidate image can be compared to find a set of similar features whose feature similarity is greater than a threshold. These similar feature sets are used to verify whether the spatial coordinates corresponding to the similar features meet the spatial consistency constraints, that is, whether the coordinates of the local features meet constraints such as affine transformation or perspective transformation.
[0063] Finally, the candidate image corresponding to the second similar feature that meets the spatial consistency is determined as a similar image to the image to be identified.
[0064] In an embodiment of the present invention, the image to be identified is input into a pre-trained image recognition model, and the global features and the first local features of the image to be identified are output. Since the image recognition model is trained based on the sample global features, sample local features and object category labels of multiple sample images, the global features and the first local features of the image to be identified can be accurately extracted. Then, multiple candidate images screened from a preset image library according to the global features are input into the image recognition model, and the second local features of each candidate image are output. Here, the second local features of the candidate images can also be accurately extracted by the image recognition model. Since the first local feature and the second local feature can effectively represent the image features of the image to be identified and the candidate image, the spatial consistency of the first local feature and the second local feature can be confirmed. If the spatial consistency is satisfied, the spatial transformation can be performed on each other, indicating that the two local features represent the features of the same target object, that is, the target object in the candidate image and the image to be identified is consistent. Therefore, the candidate image corresponding to the second local feature that meets the spatial consistency of the first local feature is determined as a similar image of the image to be identified, and the similar image of the image to be identified can be quickly and accurately determined.
[0065] Next, the training process of the image recognition model provided by the embodiment of the present invention is described.
[0066] like Figure 2 As shown, the training process of the image recognition model may include steps 210 to 240, which are specifically as follows:
[0067] In a possible embodiment, before step 120, the method further includes:
[0068] Step 210: Acquire multiple training samples, where the training samples include sample images and object category labels corresponding to the sample images.
[0069] Step 220: input the sample image into a preset neural network, and output the image features and category features of the sample image.
[0070] Step 230, determining multiple loss values based on the image features, category features and object category labels.
[0071] Step 240, training the preset neural network according to the multiple loss values until the preset training conditions are met to obtain an image recognition model.
[0072] Through the embodiments of the present invention, multiple error metrics are determined based on the image features, category features and pre-marked object category labels of the sample image, and the preset neural network is trained according to the multiple error metrics until the preset training conditions are met to obtain an image recognition model. Here, the gap between the object category predicted by the preset neural network and the object category label can be continuously narrowed through multiple error metrics, and the accuracy of the image features extracted by the preset neural network can be continuously improved. As a result, the trained image recognition model can accurately extract the image features of the image to be recognized and can accurately recognize the category features of the image to be recognized.
[0073] This involves step 210 .
[0074] A plurality of training samples are obtained, where the training samples include corresponding sample images and object category labels.
[0075] Taking the sample image as a clothing image as an example, the object category labels corresponding to the clothing image may include: shirt, skirt, pants, etc.
[0076] This involves step 220 .
[0077] Specifically, the sample image can be input into the cascaded preset neural network to output the image features and category features of the sample image. The cascaded preset neural network may include: a convolutional neural network, a category feature mapping module, a feature pyramid module, a spatial attention module, and a local feature clustering module.
[0078] Wherein, step 220 includes:
[0079] Extracting sample global features and sample feature images of the sample image;
[0080] Extracting sample local features of the sample feature image;
[0081] Classify the global features of the samples to obtain global category features;
[0082] The local features of the samples are classified to obtain local category features.
[0083] Specifically, the preset neural network may include: a convolutional neural network, a category feature mapping module, a feature pyramid module, a spatial attention module and a local feature clustering module.
[0084] Accordingly, step 220 may specifically include: inputting the sample image into a convolutional neural network to extract a sample feature image and a sample global feature of the sample image; inputting the sample feature image into a feature pyramid module to extract a sample local feature of the sample feature image; classifying the sample global feature through the category feature mapping parameters in the category feature mapping module to obtain a global category feature; convolving and activating the sample local feature through the spatial attention module to obtain a local feature weighted map; weighting the sample local features through the local feature weighted map in the local feature clustering module to obtain aggregated sample local features; classifying and mapping the aggregated sample local features to obtain local category features.
[0085] In order to explain the training process clearly, Figure 3 Expand description.
[0086] First, the sample image is input into the convolutional neural network 310 to extract the sample feature image and sample global features of the sample image.
[0087] Among them, the convolutional neural network involved above can adopt the network structure of typical convolutional neural networks (CNN), such as vgg, resnet, resnext, etc.
[0088] Each convolutional layer may contain several cascaded network structures for performing convolution transformation, activation function, normalization operation, pooling operation and other operations, so as to reduce overfitting, speed up the training process, reduce memory usage and improve performance.
[0089] Here, using the hierarchical constraints of the convolutional neural network for training can improve the ability to express the global features of the sample images.
[0090] The above-mentioned steps of extracting sample global features and sample feature images of sample images may specifically include the following steps:
[0091] Extracting sample global features and multi-scale sub-sample feature images of the sample image;
[0092] The multi-scale sub-sample feature images are fused to obtain the sample feature image.
[0093] The sub-sample feature image is extracted from the sample image and is used to represent the features of the sample image. Its representation can be a vector. Figure 3 Features shown Figure 2 ,feature Figure 3 ,feature Figure 4 ,feature Figure 5 ) can be of different scales. Different scales refer to sub-sample feature images of different sizes output by each convolution layer. Correspondingly, the sample feature image is obtained by fusing sub-sample feature images of multiple scales, so it is also used to characterize the features of the sample image, and its expression form can also be a vector.
[0094] Since the multiple convolutional layers of the convolutional neural network output multi-scale sub-sample feature images, it is necessary to fuse the multi-scale sub-sample feature images to obtain the sample feature image. The process of the multiple convolutional layers of the convolutional neural network outputting multi-scale sample feature images can be achieved by upsampling, that is, increasing the size of the feature image by interpolation operation.
[0095] Here, by using the feature pyramid module 320 to fuse the multi-scale sample feature images to obtain a fused image to extract the sample local features, the expression capability of the local features at different scales can be improved.
[0096] Secondly, extracting sample local features of the sample feature image, specifically, inputting the sample feature image output by the convolutional neural network into the feature pyramid module 320 to extract sample local features of the sample feature image.
[0097] Among them, the mapping of multi-scale sample feature images can be realized by using 1*1 convolution kernel transformation, and the mapping of sample feature images of different scales can be realized by using convolution transformation and upsampling operation. Figure 2 ,feature Figure 3 ,feature Figure 4 ,feature Figure 5 ) After mapping, the features are obtained Figure 3 ',feature Figure 4 ',feature Figure 5 ', perform splicing, fusion and feature extraction on the obtained sample feature images to obtain the local features of the sample.
[0098] For example, the local features of the sample are composed of the features Figure 2 and Features Figure 3 'The features obtained by splicing and fusion Figure 3 ' is characterized by Figure 3 and Features Figure 4 'Obtained by splicing and fusion, ..., and so on.
[0099] Then, the global features of the samples are classified and processed to obtain global category features. Specifically, the global features of the samples are classified and mapped using category feature mapping parameters in a category feature mapping module to obtain global category features.
[0100] The sample global features output by the neural network are input to the category feature mapping module 330, and the sample global features are classified and mapped to obtain global category features for determining the global classification error metric based on the global category features and the category labels of the training images.
[0101] Among them, the category feature mapping module 330 can be composed of a BN (BatchNorm) layer and a fully connected layer (Fullyconnected layer, FC layer) to map the global features to the classifier dimension. In order to keep the input of each layer of the neural network in the same distribution during the training process, the distribution of the input can be smoothed by the BN layer to help the random gradient descent and alleviate the negative impact of the random gradient descent weight update on the subsequent layers. That is to prevent the gradient from disappearing. It can also prevent the gradient from exploding.
[0102] Among them, the global features of the samples are classified and mapped, specifically, the global features of the samples are mapped to the classifier dimension, wherein the purpose of the classifier is to realize the classification of objects in the image, the dimension is the total number of trained category labels, and the output needs to be mapped to the classifier dimension. 1 on each dimension can be used to express belonging to the category, and 0 can be used to express not belonging to the category, so as to perform model training.
[0103] The above-mentioned step of classifying the local features of the sample to obtain the local category features may specifically include the following steps:
[0104] Perform convolution and activation processing on the local features of the sample to obtain a local feature weighted graph;
[0105] The local features of the samples are weighted by the local feature weighted graph to obtain the aggregated local features of the samples;
[0106] The local features of the aggregated samples are classified to obtain local category features.
[0107] Next, the spatial attention module 340 performs convolution and activation processing on the local features of the samples output by the feature pyramid module 320 to obtain a local feature weighted graph.
[0108] Among them, local features are features that only appear locally, which refer to some feature points that can appear stably and have good distinguishability. These points still exist stably when the light / video changes. In this way, when the target object is not completely blocked, some local features still exist stably to represent the target object in the image, which is convenient for the subsequent analysis. It plays a role in reducing the amount of calculation.
[0109] On the one hand, using these stable points to replace the entire image can greatly reduce the large amount of information originally carried by the image, thus reducing the amount of calculation. On the other hand, when the object is disturbed, even if some redundant information is blocked, important information can still be restored from the unblocked feature points.
[0110] When people look at a picture, they first see a part of it rather than the whole picture, and this part is the key part of the image. This principle is applied to image semantic segmentation. Taking into account the different importance of spatial position information in the image, a spatial attention module is introduced to infer the local feature weighted graph of local features.
[0111] The spatial attention module 340 used in the present method can be implemented by superimposing a softplus activation function on two convolutional layers, wherein the softplus activation function is defined as: p=ln(1+exp(q)).
[0112] Among them, q is the input signal, which is used to represent the result of the sample local feature after convolution. That is to say, the input of the activation function is the output result of the sample local feature after passing through two convolution layers. p is the output signal, that is, the local feature weighted map.
[0113] Finally, in the local feature clustering module 350, the sample local features are weighted by using the local feature weighted graph to obtain aggregated sample local features.
[0114] Among them, each pixel point in the local feature weighted map represents a weight value, and each pixel point in the sample local feature represents a multi-dimensional vector, which is consistent with the classifier dimension involved in the above-mentioned category feature mapping module. The sample local feature is weighted by the local feature weighted map, that is, according to the weight value represented by each pixel point in the above-mentioned local feature weighted map, the multi-dimensional vector represented by each pixel point in the sample local feature is weighted and summed to obtain the aggregated sample local feature.
[0115] Here, the local features of the samples are weighted and summed using the local feature weighted graph to achieve the aggregation of the local features of the samples. The aggregated local features of the samples are classified and mapped to obtain local category features. By introducing the learning step of the local features of the samples, the robustness of the model to the change of the viewpoint of the target object can be enhanced.
[0116] Wherein, step 230 may specifically include the following steps:
[0117] Determine the global classification loss value based on the global category features and the object category label;
[0118] Determine the local classification loss value based on the local category features and the object category label;
[0119] Determine a feature similarity loss value according to a sample global feature of a ternary sample image; the ternary sample image includes a first image and a second image belonging to a first category, and a third image belonging to a second category, and the feature similarity loss value is a loss value of a difference between a first distance and a second distance and a preset difference value, the first distance is an error distance between the first image and the second image, and the second distance is an error distance between the first image and the third image;
[0120] The preset neural network is trained according to multiple loss values until the preset training conditions are met to obtain an image recognition model, including:
[0121] According to the global classification loss value, the local classification loss value and the feature similarity loss value, the preset neural network is trained until the preset training conditions are met to obtain an image recognition model.
[0122] Specifically, first, an error calculation is performed based on the global category features and category labels to determine the global classification error metric.
[0123] Specifically, classification error functions such as cross entropy or additive angular margin loss function can be used for calculation.
[0124] Cross entropy and Additive Angular Margin Loss (ArcFaceloss) are typical loss functions used in classification model learning. Cross entropy is used to measure the difference information between two probability distributions.
[0125] Secondly, the error is calculated based on the local category features and category labels to determine the local classification error metric.
[0126] Local classification error metrics can also be implemented using classification error functions such as cross entropy or additive angular margin.
[0127] Then, based on the triplet error (triplet loss) algorithm, the error calculation is performed on the sample global features of the ternary sample image to determine the feature similarity measure, that is, the feature similarity loss value is determined according to the sample global features of the ternary sample image; the ternary sample image includes a first image and a second image belonging to a first category, and a third image belonging to a second category, and the feature similarity loss value is a loss value of the difference between the first distance and the second distance and a preset difference value, the first distance is the error distance between the first image and the second image, and the second distance is the error distance between the first image and the third image.
[0128] Exemplarily, the ternary sample image includes three images: the first image is denoted as xai, the second image is denoted as xpi, and the third image is denoted as xni. In such a "triplet", xai and xpi correspond to images of the same object, that is, images of the first category, while xni is an image of another different object, that is, an image belonging to the second category.
[0129] The error distance between the first image xai and the second image xpi is the first distance, and the error distance between the first image xai and the third image xni is the second distance. Then, the difference between the first distance and the second distance is determined, and then the loss value between the difference and the preset difference is compared and used as the feature similarity loss value.
[0130] The triplet error algorithm is used to control the distance between the same objects to be at least smaller than the distance between different objects. In fact, it is equivalent to a loss function. The triplet error algorithm directly optimizes the distance, so it can solve the problem of feature representation of objects.
[0131] Among them, the above-mentioned step of training the preset neural network according to the global classification loss value, the local classification loss value and the feature similarity loss value until the preset training conditions are met to obtain the image recognition model may specifically include the following steps:
[0132] According to the global classification error metric and the feature similarity metric, the neural network parameters in the convolutional neural network and the category feature mapping parameters in the category feature mapping module are trained until the convolutional neural network and the category feature mapping module meet the preset training conditions;
[0133] According to the local classification error metric, the training parameters in the feature pyramid module and the spatial attention module are trained until the feature pyramid module and the spatial attention module meet the preset training conditions;
[0134] The preset neural network is trained by combining the global classification error metric, the local classification error metric, and the feature similarity metric until the preset training conditions are met to obtain an image recognition model.
[0135] When training the model, a multi-stage training method can be used. Among them, the neural network parameters of the convolutional neural network and the category feature mapping parameters in the category feature mapping module have initial values, and then the gradient descent method can be used for learning, and the typical method of convolutional neural network model parameter learning can be used.
[0136] After the convolutional neural network converges, the neural network parameters in the convolutional neural network and the category feature mapping parameters in the category feature mapping module are frozen, and the training parameters in the feature pyramid module and the spatial attention module are trained using the local classification error metric.
[0137] After the feature pyramid module and the spatial attention module converge, the global classification error metric, the local classification error metric, and the feature similarity metric are used to train the preset neural network, that is, the above five modules are trained until the preset training conditions are met to obtain an image recognition model.
[0138] In summary, the training method of the image recognition model provided by the embodiment of the present invention determines multiple error metrics based on the image features, category features and pre-marked object category labels of the sample image, and trains the preset neural network according to the multiple error metrics until the preset training conditions are met to obtain the image recognition model. Here, the gap between the object category predicted by the preset neural network and the object category label can be continuously narrowed through multiple error metrics, and the accuracy of the image features extracted by the preset neural network can be continuously improved. As a result, the trained image recognition model can accurately extract the image features of the image to be recognized and can accurately recognize the category features of the image to be recognized.
[0139] The following is an overall description of the training process and application process of the image recognition model provided by the embodiment of the present invention.
[0140] Figure 4 is a schematic diagram of the training process and application process of an image recognition model provided by an embodiment of the present invention, such as Figure 4 As shown, it is divided into a training process 410 and an application process 420.
[0141] In the training process 410, multiple error metrics 415 are determined based on the image features, category features, and object category labels 414 pre-marked in 413 of the sample image, and the preset neural network 412 is trained based on the multiple error metrics until the preset training conditions are met to obtain an image recognition model 422. Here, the gap between the object category predicted by the preset neural network and the object category label can be continuously narrowed through multiple error metrics, and the accuracy of the image features extracted by the preset neural network can be continuously improved. As a result, the trained image recognition model 422 can accurately extract the image features of the image to be recognized and can accurately recognize the category features of the image to be recognized.
[0142] In the application process 420, the image to be identified 421 is input into the pre-trained image recognition model 422, and the global features and the first local features 423 of the image to be identified are output. Then, multiple candidate images screened from the preset image library according to the global features are input into the image recognition model, and the second local features of each candidate image are output. The candidate image 424 corresponding to the second local feature that satisfies the spatial consistency is determined as a similar image of the image to be identified, and is output, so that the similar image of the image to be identified can be determined quickly and accurately.
[0143] Based on the above Figure 1 The image recognition method shown in the figure, the embodiment of the present invention also provides an image recognition device, such as Figure 5 As shown, the image recognition device 500 may include:
[0144] The acquisition module 510 is used to acquire the image to be recognized.
[0145] The input module 520 is used to input the image to be identified into a pre-trained image recognition model, and output the global features and the first local features of the image to be identified; the image recognition model is trained based on the sample global features, sample local features and object category labels of multiple sample images; the object category label is used to describe the category to which the object in the sample image belongs.
[0146] The extraction module 530 is further used to extract the second local features of the candidate images using the image recognition model, where the candidate images are screened from a preset image library based on the global features.
[0147] The determination module 540 is used to determine the candidate image corresponding to the second local feature that meets the spatial consistency of the first local feature as a similar image to the image to be identified.
[0148] In a possible embodiment, the determination module 540 is specifically configured to:
[0149] Compare the first local feature and the second local feature to determine feature similarity;
[0150] According to the feature similarity, a first similar feature is extracted from the first local feature, and a second similar feature is extracted from the second local feature, wherein the feature similarity between the first similar feature and the second similar feature is greater than a preset threshold;
[0151] Verify whether the spatial coordinates of the first similar feature in the first local feature and the spatial coordinates of the second similar feature in the second local feature meet spatial consistency;
[0152] The candidate image corresponding to the second similar feature that meets the spatial consistency is determined as a similar image to the image to be identified.
[0153] In a possible embodiment, the image recognition device 500 may further include:
[0154] The first acquisition module is used to acquire multiple training samples, where the training samples include sample images and object category labels corresponding to the sample images.
[0155] The first input module is used to input the sample image into a preset neural network and output the image features and category features of the sample image.
[0156] The first determination module is used to determine multiple loss values according to image features, category features and object category labels.
[0157] The training module is used to train the preset neural network according to multiple loss values until the preset training conditions are met to obtain an image recognition model.
[0158] In a possible embodiment, the first input module includes:
[0159] The extraction module is used to extract sample global features and sample feature images of the sample image.
[0160] The extraction module is also used to extract sample local features of the sample feature image.
[0161] The classification module is used to classify the global features of the samples to obtain global category features.
[0162] The classification module is used to classify the local features of the samples to obtain local category features.
[0163] Among them, the classification module is specifically used for:
[0164] Perform convolution and activation processing on the local features of the sample to obtain a local feature weighted graph;
[0165] The local features of the samples are weighted by the local feature weighted graph to obtain the aggregated local features of the samples;
[0166] The local features of the aggregated samples are classified to obtain local category features.
[0167] The extraction module is specifically used for:
[0168] Extracting sample global features and multi-scale sub-sample feature images of the sample image;
[0169] The multi-scale sub-sample feature images are fused to obtain the sample feature image.
[0170] Wherein, the first determination module is specifically used for:
[0171] Determine the global classification loss value based on the global category features and the object category label;
[0172] Determine the local classification loss value based on the local category features and the object category label;
[0173] According to the sample global features of the ternary sample image, a feature similarity loss value is determined; the ternary sample image includes a first image and a second image belonging to a first category, and a third image belonging to a second category, and the feature similarity loss value is a loss value of a difference between a first distance and a second distance and a preset difference value, the first distance is an error distance between the first image and the second image, and the second distance is an error distance between the first image and the third image.
[0174] Training modules are specifically used for:
[0175] According to the global classification loss value, the local classification loss value and the feature similarity loss value, the preset neural network is trained until the preset training conditions are met to obtain an image recognition model.
[0176] In summary, in an embodiment of the present invention, by inputting the image to be identified into a pre-trained image recognition model, the global features and the first local features of the image to be identified are output. Since the image recognition model is trained based on the sample global features, sample local features and object category labels of multiple sample images, the global features and the first local features of the image to be identified can be accurately extracted. Then, multiple candidate images screened from a preset image library according to the global features are input into the image recognition model, and the second local features of each candidate image are output. Here, the second local features of the candidate images can also be accurately extracted by the image recognition model. Since the first local feature and the second local feature can effectively represent the image features of the image to be identified and the candidate image, the spatial consistency of the first local feature and the second local feature can be confirmed. If the spatial consistency is satisfied, the spatial transformation can be performed on each other, indicating that the two local features represent the features of the same target object, that is, the target object in the candidate image and the image to be identified is consistent. Therefore, the candidate image corresponding to the second local feature that meets the spatial consistency of the first local feature is determined as a similar image of the image to be identified, and the similar image of the image to be identified can be quickly and accurately determined.
[0177] The embodiment of the present invention further provides an electronic device, such as Figure 6 As shown, it includes a processor 601, a communication interface 602, a memory 603 and a communication bus 604, wherein the processor 601, the communication interface 602, and the memory 603 communicate with each other through the communication bus 604, and the memory 603 is used to store computer programs;
[0178] The processor 601 is used to execute the program stored in the memory 603 to implement the following steps:
[0179] Obtain an image to be recognized;
[0180] The image to be identified is input into a pre-trained image recognition model, and the global feature and the first local feature of the image to be identified are output; the image recognition model is trained according to the sample global features, sample local features and object category labels of multiple sample images; the object category label is used to describe the category to which the object in the sample image belongs;
[0181] The image recognition model is used to extract the second local feature of the candidate image, where the candidate image is screened from a preset image library according to the global feature;
[0182] The candidate image corresponding to the second local feature that meets the spatial consistency of the first local feature is determined as a similar image to the image to be recognized.
[0183] The communication bus mentioned in the above terminal can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0184] The communication interface is used for communication between the above terminal and other devices.
[0185] The memory may include a random access memory (RAM) or a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0186] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0187] In another embodiment of the present invention, a computer-readable storage medium is provided. The computer-readable storage medium stores instructions, which, when executed on a computer, enable the computer to execute the image recognition method described in any one of the above embodiments.
[0188] In another embodiment of the present invention, a computer program product including instructions is provided. When the computer program product is run on a computer, the computer executes the image recognition method described in any one of the above embodiments.
[0189] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk Solid State Disk (SSD)), etc.
[0190] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0191] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0192] The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.
Claims
1. An image recognition method, It is characterized in that The method comprises: Obtain an image to be recognized; Input the image to be identified into a pre-trained image recognition model, and output the global features and the first local features of the image to be identified; the image recognition model is trained based on sample global features, sample local features and object category labels of multiple sample images; the object category label is used to describe the category to which the object in the sample image belongs; Extracting a second local feature of a candidate image using the image recognition model, wherein the candidate image is screened from a preset image library according to the global feature; Determine a candidate image corresponding to a second local feature that meets spatial consistency with the first local feature as a similar image to the image to be identified, wherein the spatial consistency refers to that the spatial coordinates of local feature points corresponding to the first local feature and a subset of the second local feature that meet feature consistency meet the constraints of perspective change affine transformation; Before inputting the image to be recognized into a pre-trained image recognition model and outputting the global feature and the first local feature of the image to be recognized, the method further includes: Acquire a plurality of training samples, wherein the training samples include sample images and the object category labels corresponding to the sample images; Inputting the sample image into a cascaded preset neural network, and outputting image features and category features of the sample image, wherein the cascaded preset neural network includes: a convolutional neural network, a category feature mapping module, a feature pyramid module, a spatial attention module, and a local feature clustering module; Determining a plurality of loss values according to the image features, the category features and the object category label; Training the preset neural network according to the multiple loss values until a preset training condition is met to obtain the image recognition model; Among them, the category features include global category features and local category features, the global category features are obtained through the feature pyramid module and the category feature mapping module, and the local category features are obtained through the spatial attention module and the local feature clustering module.
2. The method according to claim 1, wherein the candidate image corresponding to the second local feature that meets the spatial consistency of the first local feature is determined as a similar image to the image to be identified, include: comparing the first local feature and the second local feature to determine feature similarity; Extracting a first similar feature from the first local feature and extracting a second similar feature from the second local feature according to the feature similarity, wherein the feature similarity between the first similar feature and the second similar feature is greater than a preset threshold; Verifying whether the spatial coordinates of the first similar feature in the first local feature and the spatial coordinates of the second similar feature in the second local feature meet spatial consistency; The candidate image corresponding to the second similarity feature that meets the spatial consistency is determined as a similar image to the image to be identified.
3. The method according to claim 1, wherein the sample image is input into a cascaded preset neural network, and the image features and category features of the sample image are output. include: Extracting sample global features and sample feature images of the sample image; Extracting sample local features of the sample feature image; Classify the global features of the sample to obtain global category features; The local features of the samples are classified to obtain local category features.
4. The method according to claim 3, wherein the local features of the sample are classified to obtain local category features, include: Performing convolution and activation processing on the local features of the sample to obtain a local feature weighted graph; Performing weighted processing on the sample local features through the local feature weighted graph to obtain aggregated sample local features; The aggregated local features of the samples are classified to obtain the local category features.
5. The method according to claim 3, wherein extracting the sample global features and the sample feature image of the sample image, include: Extracting sample global features and multi-scale sub-sample feature images of the sample image; The multi-scale sub-sample feature images are fused to obtain the sample feature image.
6. The method according to claim 3, wherein a plurality of loss values are determined according to the image features, the category features and the object category label, include: Determining a global classification loss value according to the global category feature and the object category label; Determining a local classification loss value according to the local category feature and the object category label; Determine a feature similarity loss value according to a sample global feature of a ternary sample image; the ternary sample image includes a first image and a second image belonging to a first category, and a third image belonging to a second category, and the feature similarity loss value is a loss value of a difference between a first distance and a second distance and a preset difference value, the first distance is an error distance between the first image and the second image, and the second distance is an error distance between the first image and the third image; The step of training the preset neural network according to the multiple loss values until a preset training condition is met to obtain the image recognition model includes: The preset neural network is trained according to the global classification loss value, the local classification loss value and the feature similarity loss value until preset training conditions are met to obtain the image recognition model.
7. An image recognition device, It is characterized in that The device comprises: An acquisition module, used for acquiring an image to be recognized; An input module, used to input the image to be identified into a pre-trained image recognition model, and output the global features and the first local features of the image to be identified; the image recognition model is trained based on the sample global features, sample local features and object category labels of multiple sample images; the object category label is used to describe the category to which the object in the sample image belongs; The extraction module is further used to extract a second local feature of a candidate image using the image recognition model, wherein the candidate image is screened from a preset image library according to the global feature; a determination module, configured to determine a candidate image corresponding to a second local feature that meets spatial consistency with the first local feature as a similar image to the image to be identified, wherein the spatial consistency refers to that spatial coordinates of local feature points corresponding to a subset of the first local feature and the second local feature that meet feature consistency meet constraints of a perspective change affine transformation; The device also includes: A first acquisition module, configured to acquire a plurality of training samples, wherein the training samples include sample images and the object category labels corresponding to the sample images; A first input module, used to input the sample image into a cascaded preset neural network, and output image features and category features of the sample image, wherein the cascaded preset neural network includes: a convolutional neural network, a category feature mapping module, a feature pyramid module, a spatial attention module, and a local feature clustering module; A first determination module, configured to determine a plurality of loss values according to the image features, the category features and the object category label; A training module, used for training the preset neural network according to the multiple loss values until a preset training condition is met to obtain the image recognition model; Among them, the category features include global category features and local category features, the global category features are obtained through the feature pyramid module and the category feature mapping module, and the local category features are obtained through the spatial attention module and the local feature clustering module.
8. An electronic device, It is characterized in that It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, for implementing the method steps described in any one of claims 1 to 6 when executing a program stored in a memory.
9. A computer-readable storage medium having a computer program stored thereon, It is characterized in that When the program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Image retrieval method, device and system
CN102368237A
Image retrieval method and device, electronic equipment and storage medium
CN113849679A
Image processing method and device, retrieval method and device, training method and device, equipment and medium
CN113989593A
