Image-text matching method, apparatus, device and storage medium
By employing self-attention mechanism and cross-attention fusion technology, the problem of inaccurate feature association in image-text matching is solved, thereby improving the matching accuracy.
Patent Information
- Application Number
- CN202310404231.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-06
- Publication Date
- 2025-12-02
- Estimated Expiration
- 2043-04-06
AI Technical Summary
In existing image-text matching methods, the fusion of image features and text features cannot accurately represent the degree of correlation, resulting in low matching accuracy.
A self-attention mechanism is used to perform weighted transformation on image and text feature vectors. A fused feature vector is obtained through cross-attention fusion. Then, a multilayer perceptron and a fully connected layer are used to perform feature concatenation and matching probability calculation.
It improves the accuracy of image-text matching by removing the influence of irrelevant features, thereby enhancing the accuracy of feature association.
Smart Images

Figure CN116541723B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to artificial intelligence technology, and more particularly to an image-text matching method, apparatus, electronic device, and storage medium. Background Technology
[0002] Image-text matching plays a crucial role in connecting vision and language. With the development of various vision and language technologies, image-text matching methods are attracting increasing attention.
[0003] However, existing image-text matching methods simply use artificial intelligence models to extract image and text features and then concatenate and fuse them. This results in the fused features failing to accurately represent the degree of association between the image and the text, leading to a low accuracy rate in image-text matching. Summary of the Invention
[0004] This invention provides an image-text matching method, apparatus, electronic device, and storage medium, the main purpose of which is to improve the accuracy of image-text matching.
[0005] Obtain the image to be matched, and extract image features from the image to be matched to obtain the image feature vector;
[0006] Obtain the text to be matched, extract text features from the text to be matched to obtain a text feature vector, and perform a weighted transformation on the text feature vector based on a self-attention mechanism to obtain a first weighted result;
[0007] The image feature vector is weighted and transformed based on a self-attention mechanism to obtain a second weighted result.
[0008] Based on the first weighting result and the second weighting result, cross-attention fusion of text features and image features is performed to obtain a fused text feature vector that incorporates image features and a fused image feature vector that incorporates text features.
[0009] The fused text feature vector and the fused image feature vector are used to perform vector concatenation processing to obtain a matching feature vector;
[0010] Based on the matching feature vector, the matching probability is calculated using a preset classification function to obtain the matching result.
[0011] Optionally, the step of extracting image features from the image to be matched to obtain an image feature vector includes:
[0012] The image to be matched is divided into blocks to obtain at least two image blocks;
[0013] The image blocks are converted into vectors to obtain image block vectors, and all the image block vectors are combined to obtain an image vector;
[0014] The image vector is weighted based on a multi-head attention mechanism to obtain a weighted image vector.
[0015] The weighted image vector is transformed using a preset multilayer perceptron to obtain an image feature vector.
[0016] Optionally, converting the image patch into a vector to obtain an image patch vector includes:
[0017] Obtain the feature value of each pixel in each RGB channel in the image block to obtain the channel feature value of the pixel in the corresponding channel;
[0018] The channel feature values of the same channel of all pixels in the image block are linearly connected to obtain the channel feature vector of each channel;
[0019] All the channel feature vectors are concatenated according to a preset channel order to obtain the image block vector.
[0020] Optionally, the weighted transformation of the text feature vector based on the self-attention mechanism to obtain the first weighted result includes:
[0021] Obtain the first attention network constructed based on the self-attention mechanism;
[0022] Obtain the first query weight matrix, the first key weight matrix, and the first value weight matrix in the first attention network;
[0023] The first query matrix is obtained by weighting the first query weight matrix with the text feature vector.
[0024] The first key matrix is obtained by weighting the first key weight matrix with the text feature vector.
[0025] The first value matrix is obtained by weighting the first value weight matrix with the text feature vector.
[0026] The first query matrix, the first key matrix, and the first value matrix are combined to obtain the first weighted result.
[0027] Optionally, the step of performing cross-attention fusion of text features and image features based on the first weighting result and the second weighting result to obtain a fused text feature vector that incorporates image features and a fused image feature vector that incorporates text features includes:
[0028] Attention is calculated using the first query matrix and the first key matrix in the first weighted result and the second value matrix in the second weighted result to obtain the fused image feature vector;
[0029] Attention is calculated using the second query matrix and the second key matrix in the second weighted result and the first value matrix in the first weighted result to obtain the fused text feature vector.
[0030] Optionally, the step of performing attention calculation using the first query matrix and the first key matrix in the first weighted result and the second value matrix in the second weighted result to obtain the fused image feature vector includes:
[0031] The first attention weight is obtained by calculating using the first query matrix and the first key matrix;
[0032] The first attention weight is normalized based on the vector dimension of the image feature vector to obtain the first fusion weight;
[0033] The fused image feature vector is obtained by weighting the first fusion weight with the second value matrix.
[0034] To address the above problems, the present invention also provides an image-text matching device, the device comprising:
[0035] The feature extraction module is used to acquire the image to be matched, extract image features from the image to be matched, and obtain an image feature vector.
[0036] The feature fusion module is used to acquire the text to be matched, extract text features from the text to obtain a text feature vector, and perform a weighted transformation on the text feature vector based on a self-attention mechanism to obtain a first weighted result; perform a weighted transformation on the image feature vector based on a self-attention mechanism to obtain a second weighted result; perform cross-attention fusion of text features and image features based on the first weighted result and the second weighted result to obtain a fused text feature vector that incorporates image features and a fused image feature vector that incorporates text features; and perform vector concatenation processing using the fused text feature vector and the fused image feature vector to obtain a matching feature vector.
[0037] The matching calculation module is used to calculate the matching probability based on the matching feature vector using a preset classification function to obtain the matching result.
[0038] Optionally, the step of extracting image features from the image to be matched to obtain an image feature vector includes:
[0039] The image to be matched is divided into blocks to obtain at least two image blocks;
[0040] The image blocks are converted into vectors to obtain image block vectors, and all the image block vectors are combined to obtain an image vector;
[0041] The image vector is weighted based on a multi-head attention mechanism to obtain a weighted image vector.
[0042] The weighted image vector is transformed using a preset multilayer perceptron to obtain an image feature vector.
[0043] To address the above problems, the present invention also provides an electronic device, the electronic device comprising:
[0044] Memory, storing at least one computer program; and
[0045] The processor executes the computer program stored in the memory to implement the image-text matching method described above.
[0046] To address the aforementioned problems, the present invention also provides a computer-readable storage medium storing at least one computer program, which is executed by a processor in an electronic device to implement the image-text matching method described above.
[0047] This invention employs a self-attention mechanism to perform a weighted transformation on the text feature vector, yielding a first weighted result; it also performs a weighted transformation on the image feature vector, yielding a second weighted result; based on the first and second weighted results, it performs cross-attention fusion of text and image features, resulting in a fused text feature vector and a fused image feature vector; it then performs vector concatenation on the fused text and image feature vectors to obtain a matching feature vector; by extracting effective features from the image and text features through a self-attention mechanism and removing the influence of irrelevant features, the extracted features are then fused through cross-attention, making the association between the fused feature image and text more accurate, thereby improving the accuracy of image-text matching based on the fused matching feature vector. Therefore, the image-text matching method, apparatus, electronic device, and readable storage medium proposed in this invention improve the accuracy of image-text matching. Attached Figure Description
[0048] Figure 1 This is a flowchart illustrating an image-text matching method according to an embodiment of the present invention.
[0049] Figure 2 This is a schematic diagram of a module of an image-text matching device provided in an embodiment of the present invention;
[0050] Figure 3 This is a schematic diagram of the internal structure of an electronic device that implements an image-text matching method according to an embodiment of the present invention;
[0051] The realization of the objective, functional features and advantages of the present invention will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation
[0052] It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
[0053] This invention provides an image-text matching method. The execution subject of the image-text matching method includes, but is not limited to, at least one of the following: a server, a terminal, or an electronic device that can be configured to execute the method provided in this application embodiment. In other words, the image-text matching method can be executed by software or hardware installed on a terminal device or a server device, and the software can be a blockchain platform. The server includes, but is not limited to: a single server, a server cluster, a cloud server, or a cloud server cluster, etc. The server can be an independent server or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks (CDNs), and big data and artificial intelligence platforms.
[0054] Reference Figure 1 The flowchart shown is a schematic diagram of an image-text matching method provided in an embodiment of the present invention. In this embodiment, the image-text matching method includes the following steps S1 to S6:
[0055] S1. Obtain the image to be matched, and extract image features from the image to be matched to obtain the image feature vector.
[0056] In this embodiment of the invention, the image to be matched is a preset-sized image that requires image-text matching.
[0057] Specifically, in this embodiment of the invention, image feature extraction is performed on the image to be matched to obtain an image feature vector, including:
[0058] The image to be matched is divided into blocks to obtain at least two image blocks;
[0059] The image blocks are converted into vectors to obtain image block vectors, and all the image block vectors are combined to obtain an image vector;
[0060] The image vector is weighted based on a multi-head attention mechanism to obtain a weighted image vector.
[0061] The weighted image vector is transformed using a preset multilayer perceptron to obtain an image feature vector.
[0062] Specifically, in order to better extract the image features of the image to be matched in this embodiment of the invention, the image to be matched is divided into blocks. Specifically, the image to be matched is divided into blocks according to a preset block size to obtain the image blocks.
[0063] For example, if the size of the image to be matched is 224x224 and the size of the block is 16x16, then the image to be matched can be divided into 196 16x16 image blocks.
[0064] Furthermore, in order to better extract features from the image blocks in this embodiment of the invention, the image blocks are converted into vectors to obtain the image block vectors.
[0065] Specifically, in this embodiment of the invention, converting the image patch into a vector to obtain an image patch vector includes:
[0066] Obtain the feature value of each pixel in each RGB channel in the image block to obtain the channel feature value of the pixel in the corresponding channel;
[0067] For example, if a pixel in the image block has RGB values of r: 30, g: 35, and b: 25, then the channel feature value of the r channel of that pixel is 30, the channel feature value of the g channel is 35, and the channel feature value of the b channel is 25. By linearly concatenating the channel feature values of the same channel for all pixels in the image block, a channel feature vector for each channel is obtained.
[0068] In detail, in this embodiment of the invention, the channel feature values of the same channel of all pixels in the image block are connected according to a preset pixel order to obtain the channel vector of each channel.
[0069] For example: An image patch contains two rows and two columns of pixels, arranged in the following order: first row, first column; first row, second column; second row, first column; second row, second column; second row, second column. The r-channel feature value of the first row, first column pixel is 3; the r-channel feature value of the first row, second column pixel is 1; the r-channel feature value of the second row, first column pixel is 5; and the r-channel feature value of the second row, second column pixel is 4. Therefore, the r-channel feature vector of this image patch is: .
[0070] All the channel feature vectors are concatenated according to a preset channel order to obtain the image block vector.
[0071] In this embodiment of the invention, the channel order is not restricted. For example, the channel feature vector of channel r is... The channel eigenvectors of channel g are The channel feature vectors of channel b are When the channel order is r channel, g channel, b channel, then the image block vector is: .
[0072] Furthermore, in this embodiment of the invention, each image block vector is used as a column in a preset blank matrix with the corresponding image block in the order of the image block in the image to be matched, to obtain the image vector.
[0073] In another embodiment of the present invention, the position information of the image block corresponding to the image block vector also needs to be considered. Therefore, the step of converting the image block into a vector to obtain the image block vector includes:
[0074] Obtain the position encoding vector of the image patch, wherein the dimension of the position encoding vector is the same as the dimension of the image patch;
[0075] Specifically, in this embodiment of the invention, the sequential number of the image block is obtained, and the position encoding vector of each image block is obtained by calculating the position encoding vector based on the sequential number using a position encoding function well known to those skilled in the art.
[0076] The image block feature vector is obtained by calculating the position encoding vector corresponding to the image block and the image block vector.
[0077] Specifically, in this embodiment of the invention, the position encoding vector and the image block vector corresponding to the same image block are added together to obtain the image block feature vector corresponding to that image block.
[0078] The image vector is obtained by taking the feature vector of each image block as a column of the same order in a preset blank matrix according to the order of the corresponding image blocks in the image to be matched.
[0079] Specifically, in this embodiment of the invention, the order of the image blocks in the image to be matched can be manually sorted according to different sorting rules and marked with corresponding sequence codes. Then, the image block feature vectors of the image blocks are filled into a blank matrix as the column corresponding to the sequence code in the blank matrix, thus obtaining the image vector. For example, if an image block is the first in the sorting order, then the corresponding sequence code is 1, and the image block feature vector of that image block is filled into a blank matrix as the first column of the blank matrix.
[0080] Furthermore, in this embodiment of the invention, the image vector is weighted based on a multi-head attention mechanism to obtain a weighted image vector, including:
[0081] A pre-defined number of attention mechanism networks constructed based on multi-head attention mechanisms;
[0082] In this embodiment of the invention, the preset number corresponds to the number of multi-head attention mechanisms. The types of attention mechanism networks are the same, and can be ordinary attention mechanism networks or self-attention mechanism networks. This embodiment of the invention does not limit this.
[0083] The image vector is divided into image sub-vectors according to the specified number;
[0084] For example: the image vector is If the preset quantity is 3, then the image sub-vectors are respectively , .
[0085] The image sub-vectors are input into each of the attention mechanism networks to obtain attention-weighted sub-vectors;
[0086] In this embodiment of the invention, each sub-vector corresponds to a unique attention mechanism network.
[0087] All the attention-weighted sub-vectors are concatenated according to the order of their corresponding image sub-vectors in the image vector to obtain the weighted image vector.
[0088] For example: the image vector is Then the image subvectors are respectively , Image subvectors The corresponding attention-weighted subvector is Image subvectors The corresponding attention-weighted subvector is Then the weighted image vector is .
[0089] In detail, in this embodiment of the invention, a preset multilayer perceptron is used to perform feature transformation on the weighted image vector to obtain an image feature vector, including:
[0090] The weighted image vector is input into the multilayer perceptron to obtain the image feature vector.
[0091] In this embodiment of the invention, the number of input nodes and output nodes of the multilayer perceptron are the same, so that the weighted feature vector and the image feature vector have the same dimension. The image feature vector is obtained by performing nonlinear feature transformation on the weighted image vector through the multilayer perceptron. The multilayer perceptron is a commonly used neural network structure in this field, and the specific transformation process will not be described in detail here.
[0092] In another embodiment of the present invention, the image feature vector can be stored in a blockchain node, thereby improving the data retrieval efficiency by utilizing the high throughput characteristics of the blockchain node.
[0093] S2. Obtain the text to be matched, extract text features from the text to be matched to obtain a text feature vector, and perform a weighted transformation on the text feature vector based on a self-attention mechanism to obtain a first weighted result.
[0094] In this embodiment of the invention, the text feature vector is the extracted text feature of the text to be matched. In this embodiment of the invention, a deep learning model can be used to extract the feature of the text to be matched. The deep learning model can be a RoBERTa model, a BERT model, etc. This embodiment of the invention does not limit the specific type of the deep learning model.
[0095] Furthermore, in this embodiment of the invention, the text feature vector is weighted and transformed based on a self-attention mechanism to obtain a first weighted result, including:
[0096] Obtain the first attention network constructed based on the self-attention mechanism;
[0097] Obtain the first query weight matrix, the first key weight matrix, and the first value weight matrix in the first attention network;
[0098] In this embodiment of the invention, the first query weight matrix, the first key weight matrix, and the first value weight matrix in the first attention network are weight parameters K, Q, and V that map text feature vectors to the attention mechanism.
[0099] The first query matrix is obtained by weighting the first query weight matrix with the text feature vector.
[0100] Specifically, in this embodiment of the invention, the first query weight matrix is multiplied by the text feature vector to obtain the first query matrix.
[0101] The first key matrix is obtained by weighting the first key weight matrix with the text feature vector.
[0102] In detail, in this embodiment of the invention, the first key weight matrix is multiplied by the text feature vector to obtain the first key matrix.
[0103] The first value matrix is obtained by weighting the first value weight matrix with the text feature vector.
[0104] In this embodiment of the invention, the first value weight matrix is multiplied with the text feature vector to obtain the first value matrix.
[0105] The first query matrix, the first key matrix, and the first value matrix are combined to obtain the first weighted result.
[0106] S3. The image feature vector is weighted and transformed based on the self-attention mechanism to obtain the second weighted result.
[0107] Specifically, S3 in this embodiment of the invention includes:
[0108] Obtain a second attention network constructed based on a self-attention mechanism;
[0109] Obtain the second query weight matrix, the second key weight matrix, and the second value weight matrix from the second attention network;
[0110] In this embodiment of the invention, the second query weight matrix, the second key weight matrix, and the second value weight matrix in the second attention network are weight parameters K, Q, and V that map text feature vectors to the attention mechanism.
[0111] The second query matrix is obtained by weighting the second query weight matrix with the image feature vector.
[0112] Specifically, in this embodiment of the invention, the second query weight matrix is multiplied by the image feature vector to obtain the second query matrix.
[0113] The second key matrix is obtained by weighting the second key weight matrix with the image feature vector.
[0114] In detail, in this embodiment of the invention, the second key weight matrix is multiplied with the image feature vector to obtain the second key matrix.
[0115] The second value matrix is obtained by weighting the second value weight matrix with the image feature vector.
[0116] In this embodiment of the invention, the second value weight matrix is multiplied with the image feature vector to obtain the second value matrix.
[0117] The second query matrix, the second key matrix, and the second value matrix are combined to obtain the second weighted result.
[0118] In this invention, the query matrix is equivalent to K in the attention mechanism, the key matrix is equivalent to Q in the attention mechanism, and the value matrix is equivalent to V in the attention mechanism.
[0119] S4. Based on the first weighted result and the second weighted result, perform cross-attention fusion of text features and image features to obtain a fused text feature vector that incorporates image features and a fused image feature vector that incorporates text features.
[0120] In detail, S4 in this embodiment of the invention includes:
[0121] Attention is calculated using the first query matrix and the first key matrix in the first weighted result and the second value matrix in the second weighted result to obtain the fused image feature vector;
[0122] Attention is calculated using the second query matrix and the second key matrix in the second weighted result and the first value matrix in the first weighted result to obtain the fused text feature vector.
[0123] Specifically, in this embodiment of the invention, attention is calculated using the first query matrix and the first key matrix in the first weighted result and the second value matrix in the second weighted result to obtain the fused image feature vector, including:
[0124] The first attention weight is obtained by calculating using the first query matrix and the first key matrix;
[0125] The first attention weight is normalized based on the vector dimension of the image feature vector to obtain the first fusion weight;
[0126] The fused image feature vector is obtained by weighting the first fusion weight with the second value matrix.
[0127] Specifically, in this embodiment of the invention, the transpose of the first query matrix and the first key matrix are multiplied to obtain the first attention weight; the first attention weight is divided by the arithmetic square root of the vector dimension of the image feature vector, and the result is used as a variable parameter of the softmax function to calculate the softmax function to obtain the first fusion weight; the first fusion weight and the second value matrix are multiplied to obtain the fused image feature vector.
[0128] In detail, in this embodiment of the invention, attention is calculated using the second query matrix and the second key matrix in the second weighted result and the first value matrix in the first weighted result to obtain the fused text feature vector.
[0129] The second attention weight is obtained by calculating using the second query matrix and the second key matrix;
[0130] The second attention weight is calculated by normalizing the vector dimension of the text feature vector to obtain the second fusion weight;
[0131] The fused text feature vector is obtained by weighting the second fusion weight with the first value matrix.
[0132] The method for obtaining the fused text feature vector in this embodiment of the invention is similar to the method for obtaining the fused image feature vector, and will not be described in detail here.
[0133] S5. Perform vector concatenation processing using the fused text feature vector and the fused image feature vector to obtain a matching feature vector.
[0134] S5 in this embodiment of the invention includes:
[0135] The fused text feature vector is pooled to obtain a pooled text vector;
[0136] The fused image feature vector is pooled to obtain a pooled image vector;
[0137] The specific pooling algorithm is limited in the embodiments of the present invention, such as the use of max pooling, average pooling and other pooling algorithms.
[0138] The pooled text vector is compressed in dimension using a pre-built first fully connected layer to obtain the target fused text vector.
[0139] Specifically, in this embodiment of the invention, the number of input nodes in the first fully connected layer is less than the number of output nodes.
[0140] The pooled image vector is compressed in dimension using a pre-constructed second fully connected layer to obtain the target fused image vector.
[0141] Specifically, in this embodiment of the invention, the number of input nodes in the first fully connected layer is less than the number of output nodes.
[0142] The target fused text vector and the target fused image vector are concatenated to obtain the matching feature vector.
[0143] In this embodiment of the invention, the target fused text vector and the target fused image vector are concatenated end to end to obtain the matching feature vector.
[0144] S6. Based on the matching feature vector, calculate the matching probability using a preset classification function to obtain the matching result.
[0145] S6 in this embodiment of the invention includes:
[0146] The matching feature vector is compressed using a pre-constructed fully connected layer to obtain compressed feature values;
[0147] Optionally, in this embodiment of the invention, the fully connected layer has only one node.
[0148] The compressed feature value is used as a function variable to calculate the classification function, thereby obtaining the probability of a preset matching category, and the probability is used as the matching result.
[0149] Specifically, in this embodiment of the invention, the classification function is the sigmoid function.
[0150] In another embodiment of the present invention, a preset matching threshold can be used to determine the probability to obtain the matching result, including:
[0151] When the probability is not less than the matching threshold, the matching result is a successful match.
[0152] When the probability is less than the matching threshold, the matching result is a matching failure.
[0153] In this embodiment of the invention, effective features in image and text features are extracted through a self-attention mechanism to remove the influence of irrelevant features. Then, the extracted features are fused by cross-attention, so that the correlation between the fused feature image and the text is more accurate, thereby improving the accuracy of image-text matching based on the fused matching feature vector.
[0154] like Figure 2 The diagram shown is a functional block diagram of the image-text matching device of the present invention.
[0155] The image-text matching device 100 of the present invention can be installed in an electronic device. Depending on the functions implemented, the image-text matching device may include a feature extraction module 101, a feature fusion module 102, and a matching calculation module 103. The module described in the present invention may also be referred to as a unit, which refers to a series of computer program segments that can be executed by the processor of an electronic device and can perform a fixed function, and are stored in the memory of the electronic device.
[0156] In this embodiment, the functions of each module / unit are as follows:
[0157] The feature extraction module 101 is used to acquire the image to be matched, and to extract image features from the image to be matched to obtain an image feature vector;
[0158] The feature fusion module 102 is used to acquire the text to be matched, extract text features from the text to obtain a text feature vector, and perform a weighted transformation on the text feature vector based on a self-attention mechanism to obtain a first weighted result; perform a weighted transformation on the image feature vector based on a self-attention mechanism to obtain a second weighted result; perform cross-attention fusion of text features and image features based on the first weighted result and the second weighted result to obtain a fused text feature vector that incorporates image features and a fused image feature vector that incorporates text features; and perform vector concatenation processing using the fused text feature vector and the fused image feature vector to obtain a matching feature vector.
[0159] The matching calculation module 103 is used to calculate the matching probability based on the matching feature vector using a preset classification function to obtain the matching result.
[0160] In detail, each module in the image-text matching device 100 described in this embodiment of the invention employs the same methods as described above. Figure 1 The image-text matching method described herein uses the same technical means and can produce the same technical effect, so it will not be repeated here.
[0161] like Figure 3 The diagram shown is a structural schematic of the electronic device that implements the image-text matching method of the present invention.
[0162] The electronic device may include a processor 10, a memory 11, a communication bus 12 and a communication interface 13, and may also include a computer program, such as an image-text matching program, stored in the memory 11 and capable of running on the processor 10.
[0163] The memory 11 includes at least one type of readable storage medium, such as flash memory, portable hard drive, multimedia card, card-type memory (e.g., SD or DX memory), magnetic memory, magnetic disk, optical disk, etc. In some embodiments, the memory 11 can be an internal storage unit of an electronic device, such as a portable hard drive. In other embodiments, the memory 11 can be an external storage device of the electronic device, such as a plug-in portable hard drive, Smart Media Card (SMC), Secure Digital (SD) card, Flash Card, etc. Furthermore, the memory 11 can include both internal and external storage units of the electronic device. The memory 11 can be used not only to store application software and various types of data installed on the electronic device, such as the code of an image-text matching program, but also to temporarily store data that has been output or will be output.
[0164] In some embodiments, the processor 10 may be composed of integrated circuits, such as a single packaged integrated circuit or multiple integrated circuits packaged with the same or different functions, including combinations of one or more central processing units (CPUs), microprocessors, digital processing chips, graphics processors, and various control chips. The processor 10 is the control unit of the electronic device, connecting various components of the entire electronic device through various interfaces and lines. It executes programs or modules (such as image-text matching programs) stored in the memory 11, and calls data stored in the memory 11 to perform various functions of the electronic device and process data.
[0165] The communication bus 12 can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This bus can be divided into an address bus, a data bus, a control bus, etc. The communication bus 12 is configured to enable communication between the memory 11 and at least one processor 10, etc. For ease of illustration, only one thick line is used in the figure, but this does not indicate that there is only one bus or one type of bus.
[0166] Figure 3 Only electronic devices with components are shown; it will be understood by those skilled in the art that... Figure 3 The structure shown does not constitute a limitation on the electronic device and may include fewer or more components than shown, or combine certain components, or have different component arrangements.
[0167] For example, although not shown, the electronic device may also include a power supply (such as a battery) to power the various components. Preferably, the power supply can be logically connected to the at least one processor 10 through a power management device, thereby enabling functions such as charging management, discharging management, and power consumption management. The power supply may also include one or more DC or AC power supplies, recharging devices, power fault classification circuits, power converters or inverters, power status indicators, and other arbitrary components. The electronic device may also include various sensors, Bluetooth modules, Wi-Fi modules, etc., which will not be described in detail here.
[0168] Optionally, the communication interface 13 may include a wired interface and / or a wireless interface (such as a Wi-Fi interface, a Bluetooth interface, etc.), which is typically used to establish communication connections between the electronic device and other electronic devices.
[0169] Optionally, the communication interface 13 may further include a user interface, which may be a display, an input unit (such as a keyboard), or a standard wired or wireless interface. Optionally, in some embodiments, the display may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, or an OLED (Organic Light-Emitting Diode) touchscreen, etc. The display may also be appropriately referred to as a screen or display unit, used to display information processed in the electronic device and to display a visual user interface.
[0170] It should be understood that the embodiments described are for illustrative purposes only and are not limited to this structure in the scope of the patent application.
[0171] The image-text matching program stored in the memory 11 of the electronic device is a combination of multiple computer programs, which, when run in the processor 10, can achieve the following:
[0172] Obtain the image to be matched, and extract image features from the image to be matched to obtain the image feature vector;
[0173] Obtain the text to be matched, extract text features from the text to be matched to obtain a text feature vector, and perform a weighted transformation on the text feature vector based on a self-attention mechanism to obtain a first weighted result;
[0174] The image feature vector is weighted and transformed based on a self-attention mechanism to obtain a second weighted result.
[0175] Based on the first weighting result and the second weighting result, cross-attention fusion of text features and image features is performed to obtain a fused text feature vector that incorporates image features and a fused image feature vector that incorporates text features.
[0176] The fused text feature vector and the fused image feature vector are used to perform vector concatenation processing to obtain a matching feature vector;
[0177] Based on the matching feature vector, the matching probability is calculated using a preset classification function to obtain the matching result.
[0178] Specifically, the processor 10's implementation method of the above-mentioned computer program can be found in [reference needed]. Figure 1 The descriptions of the relevant steps in the corresponding embodiments are not repeated here.
[0179] Furthermore, if the modules / units integrated into the electronic device are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. The computer-readable medium can be non-volatile or volatile. The computer-readable medium may include: any entity or device capable of carrying the computer program code, a recording medium, a USB flash drive, a portable hard drive, a magnetic disk, an optical disk, a computer memory, or a read-only memory (ROM).
[0180] Embodiments of the present invention may also provide a computer-readable storage medium storing a computer program, which, when executed by a processor of an electronic device, can perform the following:
[0181] Obtain the image to be matched, and extract image features from the image to be matched to obtain the image feature vector;
[0182] Obtain the text to be matched, extract text features from the text to be matched to obtain a text feature vector, and perform a weighted transformation on the text feature vector based on a self-attention mechanism to obtain a first weighted result;
[0183] The image feature vector is weighted and transformed based on a self-attention mechanism to obtain a second weighted result.
[0184] Based on the first weighting result and the second weighting result, cross-attention fusion of text features and image features is performed to obtain a fused text feature vector that incorporates image features and a fused image feature vector that incorporates text features.
[0185] The fused text feature vector and the fused image feature vector are used to perform vector concatenation processing to obtain a matching feature vector;
[0186] Based on the matching feature vector, the matching probability is calculated using a preset classification function to obtain the matching result.
[0187] Furthermore, the computer's usable storage medium may mainly include a program storage area and a data storage area, wherein the program storage area may store the operating system, applications required for at least one function, etc.; and the data storage area may store data created based on the use of blockchain nodes, etc.
[0188] In the several embodiments provided by this invention, it should be understood that the disclosed devices, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules is only a logical functional division, and other division methods may be used in actual implementation.
[0189] The modules described as separate components may or may not be physically separate. The components shown as modules may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0190] The embodiments of this application can acquire and process relevant data based on artificial intelligence technology. Artificial intelligence (AI) refers to the theories, methods, technologies, and application systems that use digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, perceive the environment, acquire knowledge, and use that knowledge to obtain optimal results.
[0191] Furthermore, the functional modules in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or in the form of hardware plus software functional modules.
[0192] It will be apparent to those skilled in the art that the present invention is not limited to the details of the exemplary embodiments described above, and that the present invention can be implemented in other specific forms without departing from the spirit or essential characteristics of the present invention.
[0193] Therefore, the embodiments should be considered exemplary and non-limiting in all respects, and the scope of the invention is defined by the appended claims rather than the foregoing description. Thus, all variations falling within the meaning and scope of equivalents of the claims are intended to be embraced within the invention. No appended diagram markings in the claims should be construed as limiting the scope of the claims.
[0194] The blockchain referred to in this invention is a novel application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanisms, and encryption algorithms. Essentially, a blockchain is a decentralized database, a chain of data blocks linked together using cryptographic methods. Each data block contains information about a batch of network transactions, used to verify the validity of the information (anti-counterfeiting) and generate the next block. A blockchain can include an underlying blockchain platform, a platform product service layer, and an application service layer.
[0195] Furthermore, it is clear that the word "comprising" does not exclude other units or steps, and the singular does not exclude the plural. Multiple units or devices recited in a system claim may also be implemented by a single unit or device through software or hardware. The term "second class" is used to indicate names and does not indicate any specific order.
[0196] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit it. Although the present invention has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical solutions of the present invention without departing from the spirit and scope of the technical solutions of the present invention.
Claims
1. An image-text matching method, characterized in that, The method includes: Obtain the image to be matched, and extract image features from the image to be matched to obtain the image feature vector; Obtain the text to be matched, extract text features from the text to be matched to obtain a text feature vector, and perform a weighted transformation on the text feature vector based on a self-attention mechanism to obtain a first weighted result; The image feature vector is weighted and transformed based on a self-attention mechanism to obtain a second weighted result. Based on the first weighting result and the second weighting result, cross-attention fusion of text features and image features is performed to obtain a fused text feature vector that incorporates image features and a fused image feature vector that incorporates text features. The fused text feature vector and the fused image feature vector are used to perform vector concatenation processing to obtain a matching feature vector; Based on the matching feature vector, the matching probability is calculated using a preset classification function to obtain the matching result.
2. The image-text matching method as described in claim 1, characterized in that, The step of extracting image features from the image to be matched to obtain an image feature vector includes: The image to be matched is divided into blocks to obtain at least two image blocks; The image blocks are converted into vectors to obtain image block vectors, and all the image block vectors are combined to obtain an image vector; The image vector is weighted based on a multi-head attention mechanism to obtain a weighted image vector. The weighted image vector is transformed using a preset multilayer perceptron to obtain an image feature vector.
3. The image-text matching method as described in claim 2, characterized in that, The step of converting the image patch into a vector to obtain an image patch vector includes: Obtain the feature value of each pixel in each RGB channel in the image block to obtain the channel feature value of the pixel in the corresponding channel; The channel feature values of the same channel of all pixels in the image block are linearly connected to obtain the channel feature vector of each channel; All the channel feature vectors are concatenated according to a preset channel order to obtain the image block vector.
4. The image-text matching method as described in claim 1, characterized in that, The weighted transformation of the text feature vector based on the self-attention mechanism to obtain the first weighted result includes: Obtain the first attention network constructed based on the self-attention mechanism; Obtain the first query weight matrix, the first key weight matrix, and the first value weight matrix in the first attention network; The first query matrix is obtained by weighting the first query weight matrix with the text feature vector. The first key matrix is obtained by weighting the first key weight matrix with the text feature vector. The first value matrix is obtained by weighting the first value weight matrix with the text feature vector. The first query matrix, the first key matrix, and the first value matrix are combined to obtain the first weighted result.
5. The image-text matching method as described in claim 4, characterized in that, The method of performing cross-attention fusion of text features and image features based on the first weighting result and the second weighting result to obtain a fused text feature vector that incorporates image features and a fused image feature vector that incorporates text features includes: Attention is calculated using the first query matrix and the first key matrix in the first weighted result and the second value matrix in the second weighted result to obtain the fused image feature vector; Attention is calculated using the second query matrix and the second key matrix in the second weighted result and the first value matrix in the first weighted result to obtain the fused text feature vector.
6. The image-text matching method as described in claim 5, characterized in that, The step of performing attention calculation using the first query matrix and the first key matrix in the first weighted result and the second value matrix in the second weighted result to obtain the fused image feature vector includes: The first attention weight is obtained by calculating using the first query matrix and the first key matrix; The first attention weight is normalized based on the vector dimension of the image feature vector to obtain the first fusion weight; The fused image feature vector is obtained by weighting the first fusion weight with the second value matrix.
7. An image-text matching device, characterized in that, include: The feature extraction module is used to acquire the image to be matched, extract image features from the image to be matched, and obtain an image feature vector. The feature fusion module is used to acquire the text to be matched, extract text features from the text to obtain a text feature vector, and perform a weighted transformation on the text feature vector based on a self-attention mechanism to obtain a first weighted result; perform a weighted transformation on the image feature vector based on a self-attention mechanism to obtain a second weighted result; perform cross-attention fusion of text features and image features based on the first weighted result and the second weighted result to obtain a fused text feature vector that incorporates image features and a fused image feature vector that incorporates text features; and perform vector concatenation processing using the fused text feature vector and the fused image feature vector to obtain a matching feature vector. The matching calculation module is used to calculate the matching probability based on the matching feature vector using a preset classification function to obtain the matching result.
8. The image-text matching device as described in claim 7, characterized in that, The step of extracting image features from the image to be matched to obtain an image feature vector includes: The image to be matched is divided into blocks to obtain at least two image blocks; The image blocks are converted into vectors to obtain image block vectors, and all the image block vectors are combined to obtain an image vector; The image vector is weighted based on a multi-head attention mechanism to obtain a weighted image vector. The weighted image vector is transformed using a preset multilayer perceptron to obtain an image feature vector.
9. An electronic device, characterized in that, The electronic device includes: At least one processor; and, A memory that is communicatively connected to the at least one processor; The memory stores a computer program that can be executed by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to perform the image-text matching method as described in any one of claims 1 to 6.
10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the image-text matching method as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Image and text matching method and device, electronic equipment and storage medium
CN115098644A
Image text matching method and device and readable storage medium
CN115761277A