Image classification method and device, image classification model training method and device, electronic equipment and storage medium
By embedding image and text feature vectors in the same space to calculate the loss difference for model training, the problem of low image classification efficiency in new scenarios is solved and efficient image classification is achieved.
Patent Information
- Application Number
- CN202410846602.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-27
- Publication Date
- 2024-10-01
Smart Images

Figure CN118736304A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and specifically to an image classification and model training method, device, electronic device, and storage medium. Background Art
[0002] At present, when images are classified, after the image data is input, a mapping is made based on the image information and text information, and the required category can be mapped based on the mapping. The existing image classification method cannot realize the classification of new scene images when facing new scenes. It is necessary to add more category data sets under supervised training, label each data, and classify the data to which category the data belongs. According to the scene, many data sets need to be added to the model for model training, which will result in a lot of time to retrain the model every time facing a new scene.
[0003] It can be seen that the existing technology has the problem of requiring a large amount of data for supervised training when identifying new scenes, which will lead to the problem of low image classification efficiency. Summary of the invention
[0004] In view of the above problems, the present application provides an image classification and model training method, device, electronic device, and storage medium to solve the problem of low image classification efficiency in the prior art.
[0005] According to a first aspect of an embodiment of the present application, a method for training an image classification model is provided, the method comprising: obtaining a plurality of training images and a plurality of training texts; wherein each of the training images corresponds to and matches a training text; encoding each of the plurality of training images to obtain a plurality of training image feature vectors; encoding each of the plurality of training texts to obtain a plurality of training text feature vectors; embedding each of the training image feature vectors and each of the plurality of training text feature vectors into the same space to obtain a positive sample pair vector and a plurality of negative sample pair vectors; wherein the positive sample pair vector comprises a training image vector and a training text feature vector that matches the image feature vector; the negative sample pair vector comprises a training image vector and a training text feature vector that does not match the training image feature vector; respectively calculating the differences between the positive sample pair vector and the plurality of negative sample pair vectors to obtain a plurality of loss differences; and performing model training based on the plurality of loss differences to obtain the image classification model.
[0006] In an optional manner, the image classification model is obtained by performing model training based on the multiple loss differences, further comprising: shortening the vector distance of the positive sample pairs and increasing the vector distance of the negative sample pairs based on the multiple loss differences to optimize the parameters of the model to obtain the image classification model.
[0007] In an optional manner, encoding each of the multiple training images to obtain multiple training image feature vectors further includes: preprocessing each of the multiple training images to obtain a first training image of a target dimension; extracting deep features from the first training image of the target dimension using an image understanding convolutional network; extracting correlation features between different pixels from the first training image of the target dimension using a self-attention visual network; and fusing the deep features with the correlation features between different pixels to obtain the training image feature vector.
[0008] In an optional manner, the encoding of each training text in a plurality of training texts to obtain a plurality of training text feature vectors further includes: converting each training text in a plurality of training texts into a text tag; mapping the text tag to obtain a high-dimensional vector; determining a position vector of the text tag; fusing the high-dimensional vector and the position vector to obtain a first training text vector; inputting each of the first training text vectors into a plurality of bidirectional encoders for feature extraction to obtain a plurality of second text vectors; and fusing the plurality of the second text vectors to obtain the training text feature vector.
[0009] In an optional manner, the encoder includes a self-attention layer, a feedforward neural network layer, a residual connection layer and a normalization layer; the step of inputting each of the first training text vectors into multiple bidirectional encoders for feature extraction to obtain multiple second text vectors further includes: inputting each of the first training text vectors into a self-attention mechanism so that each text label is connected to obtain an attention matrix; wherein the first training text vector includes at least one text label; inputting the self-attention matrix into a feedforward neural network layer to extract a deep feature information vector; connecting the residual connection layer before the self-attention layer and after the feedforward neural network layer to make the gradient in the feedforward neural network disappear; inputting the deep feature information vector into a normalization layer for normalization to obtain the second text vector.
[0010] In an optional manner, the obtaining of multiple training images and multiple training texts further includes: obtaining continuous frame video data; extracting frames from the continuous frame video data to obtain multiple training images; and / or placing the multiple training images into a visual model to generate multiple training texts that match the multiple training images.
[0011] According to a second aspect of an embodiment of the present application, a method for image classification is provided, the method comprising: obtaining a target classification image and N category texts; wherein N is a positive integer; inputting the target classification image and the N category texts into the image classification model obtained by a training method of the image classification model; obtaining a target category text of the target classification model according to an output of the image classification model; wherein the N category texts include the target category text.
[0012] In an optional manner, the method of obtaining the target category text of the target classification model based on the output of the image classification model further includes: encoding the target classification image to obtain a target classification image feature vector; encoding each category text of the N category texts to obtain N category text feature vectors; embedding the target classification image feature vector and the N category text feature vectors into the same space and obtaining N image-text pair vectors; calculating N similarities of the N image-text pair vectors; sorting the N similarities to obtain the maximum similarity; and obtaining the target category text of the target classification image according to the maximum similarity mapping; wherein the N category texts include the target category text.
[0013] According to a third aspect of an embodiment of the present application, a training device for an image classification model is provided, the device comprising: an acquisition module for acquiring a plurality of training images and a plurality of training texts; wherein each of the training images corresponds to and matches a training text; an image encoding module for encoding each of the plurality of training images to obtain a plurality of training image feature vectors; a text encoding module for encoding each of the plurality of training texts to obtain a plurality of training text feature vectors; a parameter processing module for embedding each of the training image feature vectors and each of the plurality of training text feature vectors into the same space and obtaining a positive sample pair vector and a plurality of negative sample pair vectors; wherein the positive sample pair vector comprises a training image vector and a training text feature vector that matches the image feature vector; the negative sample pair vector comprises a training image vector and a training text feature vector that does not match the training image feature vector; a calculation module for respectively calculating the differences between the positive sample pair vector and the plurality of negative sample pair vectors to obtain a plurality of loss differences; and a training module for performing model training based on the plurality of loss differences to obtain the image classification model.
[0014] According to a fourth aspect of an embodiment of the present application, an image classification device is provided, comprising: an acquisition module for acquiring a target classification image and N category texts; an output module for outputting the target classification image and the N category texts to the image classification model obtained by a training method of the image classification model; obtaining a target category text of the target classification model according to the output of the image classification model; wherein the N category texts include the target category text.
[0015] According to the fifth aspect of the embodiments of the present application, an electronic device is provided, comprising: a controller; and a memory for storing one or more programs, wherein when the one or more programs are executed by the controller, the controller implements the steps in any one of the training methods for the image classification model provided in the present embodiment or the steps in any one of the image classification methods provided in the present embodiment.
[0016] According to a sixth aspect of an embodiment of the present application, a computer-readable storage medium is provided, in which a computer program is stored. The computer program includes at least one executable instruction. When the executable instruction is run on a training device for an image classification model, an image classification device / electronic device, the training device for the image classification model, the image classification device / electronic device executes the steps of any one of the training methods for the image classification model provided in this embodiment or the steps of any one of the image classification methods provided in this embodiment.
[0017] The embodiment of the present application obtains multiple training images and multiple training texts, encodes each training image in the multiple training images to obtain multiple training image feature vectors, encodes each training text in the multiple training texts to obtain multiple training text feature vectors, and embeds each training image feature vector with each training text feature vector in the multiple training text feature vectors in the same space to obtain a positive sample pair and multiple negative sample pairs, and calculates the differences between the one positive sample vector and the multiple negative sample pair vectors to obtain multiple loss differences, and performs model training based on the multiple instantaneous differences to obtain the image classification model. After the above training process, when the image classification model is used subsequently, the target classification image and multiple category texts are input to directly obtain the text category of the target classification image. Even if the target image is new scene data, it can be well classified without having to input new scene training texts for a large amount of training, which greatly reduces the cost and time of data preparation, thereby improving the efficiency of image classification.
[0018] The above description is only an overview of the technical solution of the embodiment of the present application. In order to more clearly understand the technical means of the embodiment of the present application, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the embodiment of the present application more obvious and easy to understand, the specific implementation methods of the present application are listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] The accompanying drawings are only used to illustrate the embodiments and are not to be considered as limiting the present application. In addition, the same reference symbols are used to represent the same components throughout the accompanying drawings. In the accompanying drawings:
[0020] Figure 1 A flowchart of a first embodiment of a training method for an image classification model provided by the present application is shown;
[0021] Figure 2 A flowchart of a second embodiment of a method for training an image classification model provided by the present application is shown;
[0022] Figure 3 A flowchart of a third embodiment of a training method for an image classification model provided by the present application is shown;
[0023] Figure 4 A flowchart of a fourth embodiment of a training method for an image classification model provided by the present application is shown;
[0024] Figure 5 A flowchart of a fifth embodiment of a training method for an image classification model provided by the present application is shown;
[0025] Figure 6 A flowchart of a first embodiment of an image classification method provided by the present application is shown;
[0026] Figure 7 A flowchart of a second embodiment of an image classification method provided by the present application is shown;
[0027] Figure 8 A schematic diagram of the structure of an embodiment of a training device for an image classification model provided by the present application is shown;
[0028] Fig. 9 A schematic diagram showing the structure of an embodiment of an image classification device provided by the present application is shown;
[0029] Fig.10 A schematic structural diagram of an embodiment of the electronic device of the present application is shown. DETAILED DESCRIPTION
[0030] Here, exemplary embodiments will be described in detail, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are only examples of devices and methods consistent with some aspects of the present application as detailed in the attached claims.
[0031] The block diagrams shown in the accompanying drawings are merely functional entities and do not necessarily correspond to physically independent entities. That is, these functional entities may be implemented in software form, or in one or more hardware modules or integrated circuits, or in different networks and / or processor devices and / or microcontroller devices.
[0032] The flowcharts shown in the accompanying drawings are only exemplary and do not necessarily include all the contents and operations / steps, nor must they be executed in the order described. For example, some operations / steps can be decomposed, and some operations / steps can be combined or partially combined, so the actual execution order may change according to actual conditions.
[0033] The term "multiple" as used in this application refers to two or more than two. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the related objects are in an "or" relationship.
[0034] Figure 1 The flowchart of the first embodiment of the training method of an image classification model provided by the present application is shown, and the method is executed by an electronic device. Figure 1 As shown, the method comprises the following steps:
[0035] Step S110: Acquire a plurality of training images and a plurality of training texts; wherein each of the training images corresponds to and matches one of the training texts;
[0036] In which, supervised training is performed on the image classification model by obtaining multiple training images and multiple training texts, wherein the number of training images is the same as the number of training texts, each training image corresponds to a matching training text, and the training text is used to describe the content of the training image, so that the image classification model can learn the correspondence between the training images and the training texts describing the content of the training images.
[0037] Specifically, Figure 2 The flowchart of the second embodiment of the training method of an image classification model provided by the present application is shown, and the method is executed by an electronic device. Figure 2As shown, obtaining multiple training images can be achieved by following the steps below:
[0038] Step S111: Acquire continuous frame video data;
[0039] Among them, the video data can be image materials taken by a driving recorder and a user's active use of a vehicle-mounted camera during the driving of the car. The video data is obtained by passing in the storage file path of the video data, for example, the ID number of the transmission camera and the storage path of the network video file. Specifically, the video capture (VideoCapture) function of the cross-platform computer vision library (opencv) can be used to read the storage path of the video data.
[0040] Step S112: extracting frames from the continuous frame video data to obtain a plurality of training images;
[0041] Wherein, a plurality of training images are obtained by extracting the continuous frame video data, so that a single image is input into the model for training.
[0042] It should be noted that multiple training images may also be directly acquired for training.
[0043] Specifically, obtaining multiple training texts can be achieved in the following ways:
[0044] The multiple training images are placed into a visual model to generate multiple training texts matching the multiple training images.
[0045] The training images are placed into the visual training model to generate corresponding training texts, and the training images are described in detail. For example, the scenes of the images are mainly landscape scenes, and the corresponding text descriptions are: forest scenes, mountain scenes, ocean scenes, flower scenes, and storm scenes, etc. After the corresponding training texts are generated, they are manually verified, and the unreasonable parts are modified, and the corresponding supplementary descriptions are made for the data with less description or no description. It should be noted that the training texts can also be directly described manually according to the content represented by the image.
[0046] Step S120: encoding each of the plurality of training images to obtain a plurality of training image feature vectors;
[0047] Among them, an image encoder is used to encode each training image to obtain a training image feature vector, so that the loss can be calculated between the training image and the training text.
[0048] Step S130: Encode each of the plurality of training texts to obtain a plurality of training text feature vectors;
[0049] Among them, a text encoder is used to encode each training text in multiple training texts to obtain a text feature vector, so that the loss can be calculated between the training image and the training text.
[0050] Step S140: embed each of the training image feature vectors and each of the training text feature vectors in the plurality of training text feature vectors into the same space to obtain a positive sample pair vector and a plurality of negative sample pair vectors;
[0051] Wherein, the positive sample pair vector includes one of the training image vectors and one of the training text feature vectors matching the image feature vector; the negative sample pair vector includes one of the training image vectors and one of the training text feature vectors not matching the training image feature vector;
[0052] Specifically, after multiple training images and multiple training texts are placed into the image classification model, the image classification model does not know which training image corresponds to the training text of the training image. After each training image is placed into the image classification model, a sample pair will be formed with each training text; by embedding the training image feature vector and the multiple training text feature vectors into the same space, a positive sample pair feature vector and multiple negative sample pair vectors are formed, that is, there is only one training text corresponding to the training image.
[0053] Step S150: respectively calculating the difference between the one positive sample pair vector and the multiple negative sample pair vectors to obtain multiple loss differences;
[0054] Among them, the loss difference between the positive sample pair and the negative sample pair is different. By calculating the difference between the positive sample pair vectors and the loss difference between the negative sample pairs, the image classification model can determine which is the positive sample pair and which is the negative sample pair.
[0055] Step S130: Performing model training based on the multiple loss differences to obtain the image classification model.
[0056] The image classification model is trained through multiple loss differences, and the parameters inside the model are adjusted to obtain the image classification model, so that after the image classification model inputs the target image and the category text, the category of the target image can be obtained.
[0057] By acquiring multiple training images and multiple training texts, encoding each training image in the multiple training images to obtain multiple training image feature vectors, encoding each training text in the multiple training texts to obtain multiple training text feature vectors, and embedding each training image feature vector and each training text feature vector in the multiple training text feature vectors into the same space to obtain a positive sample pair and multiple negative sample pairs, and respectively calculating the differences between the one positive sample vector and the multiple negative sample pair vectors to obtain multiple loss differences, and performing model training based on the multiple loss differences to obtain the image classification model. After the above training process, when the image classification model is used subsequently, the target classification image and multiple category texts are input, and the text category of the target classification image can be directly obtained. Even if the target image is new scene data, good classification can be performed, and there is no need to input new scene training texts for a large amount of training, which greatly reduces the cost and time of data preparation, thereby improving the efficiency of image classification.
[0058] In another embodiment of the present application, the image classification model is obtained by performing model training based on the multiple loss differences, further comprising: shortening the vector distance of the positive sample pairs and increasing the vector distance of the negative sample pairs based on the multiple loss differences to optimize the parameters of the model to obtain the image classification model.
[0059] In this embodiment, the process of shortening the distance between the positive sample pairs and increasing the distance between the negative sample pairs is implemented through multiple loss differences, so as to adjust the parameters of the image classification model, so that the subsequent image classification model can classify the target classification image based on the adjusted parameters when performing image classification. The loss difference NCE can be calculated using the following formula:
[0060]
[0061] in,
[0062] v represents the image sample;
[0063] t represents a text sample;
[0064] z v represents the training image vector;
[0065] z t represents the training text vector;
[0066] Represents the training text vector that forms a positive sample pair with the training image vector;
[0067] Represents the training text vector that forms a negative sample pair with the training image vector;
[0068] τ represents the smoothness constant that controls the similarity distribution, which is generally between 0.05 and 0.5;
[0069] The process of obtaining the image classification model by optimizing the parameters of the model based on the multiple loss differences by shortening the vector distance of the positive sample pair and extending the vector distance of the negative sample pair can be implemented by using the following loss function L:
[0070]
[0071] Among them, v,t belongs to the training sample data B.
[0072] Figure 3 The flowchart of the third embodiment of the training method of an image classification model provided by the present application is shown, and the method is executed by an electronic device. Figure 3 As shown, the encoding of each training image in the plurality of training images to obtain a plurality of training image feature vectors further comprises the following steps:
[0073] S210: Preprocess each of the multiple training images to obtain a first training image of a target dimension;
[0074] Among them, first, after obtaining multiple training images, the dimension of each training image is the first target dimension [B, T, FPS, H, W, C], and then the training image of the first target dimension is transformed by view to obtain the second dimension [B*T, FPS, H, W, C], and then the C feature in the training image of the second dimension is placed on dimension 1, and then the permute exchange dimension function is used to obtain the training image of the target dimension [B*T, C, FPS, H, W,].
[0075] S220: extracting deep features from the first training image of the target dimension using an image understanding convolutional network;
[0076] Among them, a deep layer is extracted from the first training image of the target dimension by adopting an image convolution network to obtain the key features of the training image, thereby reducing the amount of calculation; specifically, the convolution network can be an S3D convolutional image encoder; and the training image of the target dimension [B*T,C,FPS,H,W,] is converted into a third dimension [B1,8*C,T1 / / 2,H / / 2,W / / 2].
[0077] S230: extracting correlation features between different pixels from the first training image of the target dimension using a self-attention visual network;
[0078] Among them, a self-attention visual network is used to extract correlation features between different pixels from the first training image of the target dimension, so that the extracted features are more accurate; the self-attention visual network can be a VIT visual encoding network.
[0079] S240: Fusing the deep features and the correlation features between different pixels to obtain the training image feature vector.
[0080] By fusing the deep features with the correlation features between different pixels, the amount of calculation of the training image feature vector in the subsequent process is reduced, and the extracted training image feature vector is more accurate.
[0081] It should be noted that the fused features extracted by the S3D module and the ViT module are subjected to a global average pooling operation to obtain a fixed-size feature representation. Finally, these features are sent to the fully connected layer, where they are converted into training image vectors of dimension [B1, 1024], where 1024 is the dimension size of the video image feature, so that subsequent calculations can be performed between the training text vectors.
[0082] Figure 4 The flowchart of the fourth embodiment of the training method of an image classification model provided by the present application is shown, and the method is executed by an electronic device. Figure 4 As shown, the encoding of each training text in the plurality of training texts to obtain a plurality of training text feature vectors further comprises the following steps:
[0083] Step S310: converting each training text in the plurality of training texts into a text mark;
[0084] Among them, each text token represents a number, representing a word or phrase in the vocabulary, so that the words or phrases in the training text correspond to the numbers; each training text data is converted into a text token representation in vector form to facilitate subsequent calculations.
[0085] Step S320: Map the text mark to obtain a high-dimensional vector;
[0086] Among them, each token will be mapped to a high-dimensional vector (called word embedding), that is, all text tokens in the training text are mapped to a digital matrix.
[0087] Step S330: determining the position vector of the text mark;
[0088] In order to capture the order information of the sentence, a positional code is embedded in each text token to ensure the meaning expressed by all text tokens.
[0089] Step S340: fusing the high-dimensional vector and the position vector to obtain a first training text vector;
[0090] The high-dimensional vector and the position vector are fused to obtain a first training text vector, that is, a digital matrix representing the meaning of the training text, so that calculations can be performed between the first training text vector and the image feature vector.
[0091] Step S350: input each of the first training text vectors into a plurality of bidirectional encoders for feature extraction to obtain a plurality of second text vectors;
[0092] Among them, by inputting each of the first training text vectors into multiple bidirectional encoders for feature extraction to obtain multiple second text vectors, the amount of calculation of the text vectors in subsequent processes can be reduced to improve calculation efficiency.
[0093] Step S360: fusing the plurality of second text vectors to obtain the training text feature vector;
[0094] Among them, since the initial value of each bidirectional encoder is different, and each of the first training text feature vectors is input into multiple bidirectional encoders for feature extraction and then fused, the accuracy of feature extraction can be improved.
[0095] Figure 5 The flowchart of the fifth embodiment of the training method of an image classification model provided by the present application is shown, and the method is executed by an electronic device. Figure 5 As shown, the encoder includes a self-attention layer, a feedforward neural network layer, a residual connection layer and a normalization layer; each of the first training text vectors is input into multiple bidirectional encoders for feature extraction to obtain multiple second text vectors, further comprising the following steps:
[0096] Step S351: input each of the first training text vectors into the self-attention mechanism so that each text label is connected to obtain an attention matrix;
[0097] Wherein, the first training text vector includes at least one of the text labels;
[0098] Step S352: inputting the self-attention matrix into the feed-forward neural network layer to extract a deep feature information vector;
[0099] Step S353: Connect the residual connection layer before the self-attention layer and after the feedforward neural network layer to make the gradient in the feedforward neural network disappear;
[0100] Among them, the feedforward neural network may stagnate during the process of extracting deep feature information vectors. By establishing a residual connection layer before the self-attention layer and after the feedforward neural network layer, the gradient in the feedforward neural network disappears, making the feedforward neural network stagnate, and feature extraction can continue.
[0101] Step S354: input the deep feature information vector into a normalization layer for normalization to obtain the second text vector.
[0102] Among them, normalizing the deep feature information vector helps to speed up the training process and improve the generalization ability of the model.
[0103] Figure 6 The flowchart of the first embodiment of an image classification method provided by the present application is shown, and the method is executed by an electronic device. Figure 6 As shown, the method comprises the following steps:
[0104] Step S410: Obtain a target classification image and N category texts;
[0105] Wherein, N is a positive integer; the N category texts include the category text corresponding to the target classification image.
[0106] Step S420: inputting the target classification image and the N category texts into the image classification model obtained by the training method of the image classification model;
[0107] The target classification image and the N category texts are input into the image category model for image classification to obtain a category text image corresponding to the target classification image.
[0108] Step S430: obtaining the target category text of the target classification model according to the output of the image classification model;
[0109] Among them, the N category texts include the target category text.
[0110] By using the image classification model obtained through the above-mentioned image classification model training method, by inputting a target classification image and multiple category texts, the text category of the target classification image can be directly obtained. Even if the target image is new scene data, it can still be well classified. There is no need to input new scene training text for a large amount of training, which greatly reduces the cost and time of data preparation, thereby improving the efficiency of image classification.
[0111] Figure 7 FIG. 1 is a flowchart of a second embodiment of an image classification method provided by the present application, and the method is executed by an electronic device. Figure 7As shown, obtaining the target category text of the target classification model according to the output of the image classification model further includes the following steps:
[0112] Step S510: Encode the target classification image to obtain a target classification image feature vector;
[0113] The target classification image is encoded by using an image encoder to obtain the target classification image feature vector, so that the loss can be calculated between the target classification image pair and each category text.
[0114] Step S520: Encode each of the N category texts to obtain N category text feature vectors;
[0115] Among them, each category text is encoded by using a text encoder to obtain N category text feature vectors, so that the loss can be calculated between the target classification image pair and each category text.
[0116] Step S530: embedding the target classification image feature vector and N category text feature vectors into the same space to obtain N image-text pair vectors;
[0117] The target classification image feature vector and N category text feature vectors are embedded in the same space to obtain N image-text pair vectors, thereby calculating the similarity between the image vector and each category text feature vector.
[0118] Step S540: Calculating N similarities of the N image-text pair vectors;
[0119] Step S550: sorting the N similarities to obtain the maximum similarity;
[0120] Step S560: obtaining the target category text of the target classification image according to the maximum similarity mapping;
[0121] The N category texts include the target category text. The target category image and the category text in the image-text pair vector corresponding to the maximum similarity have the highest similarity, which means that the target category image belongs to the category text, thereby achieving the classification of the target category image.
[0122] Figure 8 The schematic diagram of the structure of an embodiment of a training device for an image classification model provided by the present application is shown. Figure 8 As shown, the device 600 includes: an acquisition module 610, an image encoding module 620, a text encoding module 630, a parameter processing module 640, a calculation module 650 and a training module 660;
[0123] The acquisition module 610 is used to acquire a plurality of training images and a plurality of training texts; wherein each of the training images corresponds to and matches one of the training texts;
[0124] An image encoding module 620, configured to encode each of the plurality of training images to obtain a plurality of training image feature vectors;
[0125] A text encoding module 630, configured to encode each of the plurality of training texts to obtain a plurality of training text feature vectors;
[0126] The parameter processing module 640 is used to embed each of the training image feature vectors and each of the training text feature vectors in the multiple training text feature vectors into the same space and obtain a positive sample pair vector and multiple negative sample pair vectors; wherein the positive sample pair vector includes one of the training image vectors and one of the training text feature vectors that matches the image feature vector; and the negative sample pair vector includes one of the training image vectors and one of the training text feature vectors that does not match the training image feature vector.
[0127] A calculation module 650 is used to calculate the difference between the one positive sample pair vector and the multiple negative sample pair vectors to obtain multiple loss differences;
[0128] The training module 660 is used to perform model training based on the multiple loss differences to obtain the image classification model.
[0129] It should be noted that the image classification model training device provided in the above embodiment and the image classification model training method provided in the above embodiment belong to the same concept, and the specific manner in which each module and unit performs operations has been described in detail in the method embodiment and will not be repeated here.
[0130] Fig. 9 FIG. 1 is a schematic diagram showing a structure of an embodiment of an image classification device provided by the present application. Fig. 9 As shown, the device 700 includes: an input module 710 and an output module 720;
[0131] An acquisition module 710 is used to acquire a target classification image and N categories of text;
[0132] The output module 720 is used to input the target classification image and the N category texts into the image classification model training method to obtain the image classification model; obtain the target category text of the target classification model according to the output of the image classification model; wherein the N category texts include the target category text.
[0133] Fig.10A schematic diagram of the structure of an embodiment of the electronic device of the present application is shown, which shows a schematic diagram of the structure of a computer system suitable for implementing the electronic device of the embodiment of the present application. The specific embodiment of the present application does not limit the specific implementation of the electronic device.
[0134] See also Fig.10 As shown, the electronic device includes: a controller; a memory for storing one or more programs, and when the one or more programs are executed by the controller, the above-mentioned image classification model training method or image classification method is executed.
[0135] Please continue reading Fig.10 As shown, the computer system 800 of the electronic device includes a central processing unit (CPU) 801, which can perform various appropriate actions and processes according to the program stored in the read-only memory (ROM) 802 or the program loaded from the storage part 808 to the random access memory (RAM) 803, such as executing the method in the above embodiment. In RAM 803, various programs and data required for system operation are also stored. CPU 801, ROM 802 and RAM 803 are connected to each other through bus 804. Input / output (I / O) interface 805 is also connected to bus 804.
[0136] The following components are connected to the I / O interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN (Local Area Network) card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed so that a computer program read therefrom is installed into the storage section 808 as needed.
[0137] In particular, according to an embodiment of the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present application includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a computer program for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication section 809, and / or installed from a removable medium 811. When the computer program is executed by a central processing unit (CPU) 801, various functions defined in the system of the present application are executed.
[0138] Another aspect of the present application further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the training method of the image classification model or the image classification method as described above. The computer-readable storage medium may be included in the electronic device described in the above embodiment, or may exist independently without being assembled into the electronic device.
[0139] Another aspect of the present application provides a computer program product or a computer program, which includes at least one executable instruction. When the executable instruction is run on a training device for an image classification model / image classification device / electronic device, the training device for an image classification model / image classification device / electronic device executes the training method for an image classification model or the image classification method as described above.
[0140] The executable instructions may be specifically used to enable the training device / electronic device of the image classification model to perform the following operations:
[0141] Acquire multiple training images and multiple training texts; wherein each of the training images corresponds to a matching training text; encode each of the multiple training images to obtain multiple training image feature vectors; encode each of the multiple training texts to obtain multiple training text feature vectors; embed each of the training image feature vectors and each of the multiple training text feature vectors into the same space to obtain a positive sample pair vector and multiple negative sample pair vectors; wherein the positive sample pair vector includes a training image vector and a training text feature vector that matches the image feature vector; the negative sample pair vector includes a training image vector and a training text feature vector that does not match the training image feature vector; respectively calculate the differences between the one positive sample pair vector and the multiple negative sample pair vectors to obtain multiple loss differences; perform model training based on the multiple loss differences to obtain the image classification model.
[0142] The executable instructions may be specifically used to enable the image classification device / electronic device to perform the following operations:
[0143] Obtain a target classification image and N category texts; wherein N is a positive integer; input the target classification image and the N category texts into the image classification model training method to obtain the image classification model; obtain the target category text of the target classification model according to the output of the image classification model; wherein the N category texts include the target category text.
[0144] It should be noted that the computer-readable medium shown in the embodiment of the present application can be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium can be, for example, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media can include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (ErasableProgrammable Read Only Memory, EPROM), a flash memory, an optical fiber, a portable compact disk read-only memory (Compact Disc Read-Only Memory, CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present application, a computer-readable storage medium can be any tangible medium containing or storing a program, which can be used by an instruction execution system, device or device or used in combination with it. In the present application, a computer-readable signal medium can include a data signal propagated in a baseband or as a part of a carrier wave, wherein a computer-readable computer program is carried. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate, or transmit programs for use by or in conjunction with an instruction execution system, apparatus, or device. The computer program contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wired, etc., or any suitable combination of the above.
[0145] The flowchart and block diagram in the accompanying drawings illustrate the possible architecture, functions and operations of the system, method and computer program product according to various embodiments of the present application. Wherein, each box in the flowchart or block diagram can represent a module, a program segment, or a part of the code, and the above-mentioned module, program segment, or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order than the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flowchart, and the combination of boxes in the block diagram or flowchart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0146] The units involved in the embodiments described in this application may be implemented by software or hardware, and the units described may also be set in a processor. The names of these units do not, in some cases, constitute limitations on the units themselves.
[0147] According to one aspect of an embodiment of the present application, a computer system is also provided, including a central processing unit (CPU), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) or a program loaded from a storage portion into a random access memory (RAM), such as executing the method in the above embodiment. In RAM, various programs and data required for system operation are also stored. CPU, ROM and RAM are connected to each other via a bus. An input / output (I / O) interface is also connected to the bus.
[0148] The following components are connected to the I / O interface: an input part including a keyboard, a mouse, etc.; an output part including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker; a storage part including a hard disk, etc.; and a communication part including a network interface card such as a LAN (Local Area Network) card, a modem, etc. The communication part performs communication processing via a network such as the Internet. A drive is also connected to the I / O interface as needed. Removable media, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., are installed on the drive as needed so that the computer program read therefrom is installed into the storage part as needed.
[0149] The above content is only a preferred exemplary embodiment of the present application and is not intended to limit the implementation scheme of the present application. A person skilled in the art can easily make corresponding changes or modifications based on the main concept and spirit of the present application. Therefore, the scope of protection of the present application shall be based on the scope of protection required by the claims.
Claims
1. A training method for an image classification model, characterized in that: The method comprises: Acquire multiple training images and multiple training texts; wherein each of the training images corresponds to and matches one of the training texts; Encoding each of the plurality of training images to obtain a plurality of training image feature vectors; Encoding each of the plurality of training texts to obtain a plurality of training text feature vectors; Each of the training image feature vectors is respectively embedded in the same space with each of the multiple training text feature vectors to obtain a positive sample pair vector and multiple negative sample pair vectors; wherein the positive sample pair vector includes one of the training image vectors and one of the training text feature vectors that matches the image feature vector; and the negative sample pair vector includes one of the training image vectors and one of the training text feature vectors that does not match the training image feature vector; Calculating the difference between the one positive sample pair vector and the multiple negative sample pair vectors respectively to obtain multiple loss differences; The image classification model is obtained by performing model training based on the multiple loss differences.
2. The method for training an image classification model according to claim 1, characterized in that: The method of performing model training based on the multiple loss differences to obtain the image classification model further includes: Based on the multiple loss differences, the vector distance of the positive sample pair is shortened and the vector distance of the negative sample pair is extended, so as to optimize the parameters of the model to obtain the image classification model.
3. The training method of the image classification model according to claim 1, characterized in that: The step of encoding each of the plurality of training images to obtain a plurality of training image feature vectors further comprises: Preprocessing each of the multiple training images to obtain a first training image of the target dimension; extracting deep features from a first training image of the target dimension using an image understanding convolutional network; Extracting correlation features between different pixels from a first training image of the target dimension using a self-attention visual network; The deep features and the correlation features between different pixels are fused to obtain the training image feature vector.
4. The method for training an image classification model according to claim 1, characterized in that: The step of encoding each training text in the plurality of training texts to obtain a plurality of training text feature vectors further comprises: Convert each training text of the plurality of training texts into a text token; Mapping the text mark to obtain a high-dimensional vector; Determine the position vector of the text mark; The high-dimensional vector and the position vector are fused to obtain a first training text vector; Input each of the first training text vectors into a plurality of bidirectional encoders for feature extraction to obtain a plurality of second text vectors; The plurality of second text vectors are fused to obtain the training text feature vector.
5. The method for training an image classification model according to claim 4, characterized in that: The encoder includes a self-attention layer, a feedforward neural network layer, a residual connection layer and a normalization layer; each of the first training text vectors is input into a plurality of bidirectional encoders for feature extraction to obtain a plurality of second text vectors, further comprising: Input each of the first training text vectors into a self-attention mechanism to establish a connection between each text label to obtain an attention matrix; wherein the first training text vector includes at least one of the text labels; Inputting the self-attention matrix into a feed-forward neural network layer to extract a deep feature information vector; Connecting the residual connection layer before the self-attention layer and after the feed-forward neural network layer to make the gradient in the feed-forward neural network disappear; The deep feature information vector is input into a normalization layer for normalization to obtain the second text vector.
6. The method for training an image classification model according to claim 1, characterized in that: The obtaining of a plurality of training images and a plurality of training texts further comprises: Get continuous frame video data; Extracting frames from the continuous frame video data to obtain a plurality of training images; And / or, putting the multiple training images into a visual model to generate multiple training texts matching the multiple training images.
7. A method for image classification, characterized in that: The method comprises: Get the target classification image and N category texts; where N is a positive integer; Inputting the target classification image and the N category texts into the image classification model obtained by the training method of the image classification model according to any one of claims 1 to 6; The target category text of the target classification model is obtained according to the output of the image classification model; wherein the N category texts include the target category text.
8. The image classification method according to claim 7, characterized in that: The step of obtaining the target category text of the target classification model according to the output of the image classification model further includes: Encoding the target classification image to obtain a target classification image feature vector; Encoding each of the N category texts to obtain N category text feature vectors; Embed the target classification image feature vector and N category text feature vectors into the same space to obtain N image-text pair vectors; Calculating N similarities of the N image-text pair vectors; Sort the N similarities to obtain the maximum similarity; Obtaining the target category text of the target classification image according to the maximum similarity mapping; Among them, the N category texts include the target category text.
9. A training device for an image classification model, characterized in that: The device comprises: An acquisition module, used to acquire a plurality of training images and a plurality of training texts; wherein each of the training images corresponds to and matches one of the training texts; An image encoding module, used for encoding each of the plurality of training images to obtain a plurality of training image feature vectors; A text encoding module, used for encoding each of the plurality of training texts to obtain a plurality of training text feature vectors; A parameter processing module, used for respectively embedding each of the training image feature vectors and each of the training text feature vectors in the multiple training text feature vectors into the same space and obtaining a positive sample pair vector and multiple negative sample pair vectors; wherein the positive sample pair vector includes one of the training image vectors and one of the training text feature vectors matching the image feature vector; and the negative sample pair vector includes one of the training image vectors and one of the training text feature vectors not matching the training image feature vector; A calculation module, used to calculate the difference between the one positive sample pair vector and the multiple negative sample pair vectors respectively, to obtain multiple loss differences; A training module is used to perform model training based on the multiple loss differences to obtain the image classification model.
10. An image classification device, characterized in that: The device comprises: The acquisition module is used to obtain the target classification image and N category texts; An output module is used to input the target classification image and the N category texts into the image classification model obtained by the training method of the image classification model; obtain the target category text of the target classification model according to the output of the image classification model; wherein the N category texts include the target category text.
11. An electronic device, characterized in that: include: Controller; A memory for storing one or more programs. When the one or more programs are executed by the controller, the controller implements the training method of the image classification model described in any one of claims 1-6 or the image classification method described in any one of claims 7-8.
12. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, which includes at least one executable instruction. When the executable instruction is run on the training device of the image classification model, the image classification device / electronic device, the training device of the image classification model, the image classification device / electronic device executes the training method of the image classification model as described in any one of claims 1-6 or the image classification method as described in any one of claims 7-8.
Citation Information
Cited By
Image geographic positioning method, device and equipment based on multi-modal large model
CN119206385A
