Text generation method and device, electronic equipment, program product and storage medium

By using the cross-attention mechanism in the multi-modal model to interact with image and text features, the problem of excessive computing resource usage of image representation vector dimensions is solved, the user experience is improved and the coherence of information extraction is maintained.

CN118690010AActive Publication Date: 2024-09-24LANGCHAO ELECTRONIC INFORMATION IND CO LTD

Patent Information

Application Number
CN202410867700.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-30
Publication Date
2024-09-24
Estimated Expiration
2044-06-30

AI Technical Summary

Technical Problem

When the multi-modal model extracts image and text features, the dimensionality of the image representation vector is too large, which causes serious occupation of computing resources and prolongs the processing time, affecting the user experience.

Method used

The cross-attention mechanism is used to perform cross-attention processing on image representation vectors and text representation vectors, and perform self-attention processing on the cross-attention processing results, replacing the traditional splicing vector self-attention processing and reducing the amount of calculation.

Benefits of technology

It effectively reduces the computing power requirements of the model, improves the user experience, avoids the increase in calculations caused by high-resolution images, and maintains the consistency of information extraction.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118690010A
    Figure CN118690010A
Patent Text Reader

Abstract

The invention provides a text generation method and device, electronic equipment, a program product and a storage medium, and relates to the field of artificial intelligence, the method comprises the following steps: receiving an input image and an input text, and respectively encoding the input image and the input text to obtain an image representation vector and a text representation vector; performing cross attention processing on the text representation vector by using the image representation vector, and performing self-attention processing on a cross attention processing result to obtain a feature extraction vector; performing logic calculation on the feature extraction vector to obtain an output text generated according to semantics of the input image and the input text; according to the technical scheme, by adjusting the feature interaction mode between the image and the text, the calculation increase of the quadratic generation of the length of the input sequence is avoided, then the calculation power requirement of the model can be reduced, and the use experience of a user can be effectively improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence, and in particular to a text generation method, device, electronic device, program product and storage medium. Background Art

[0002] A multimodal model is a machine learning model that involves multiple data sources and data types. It can extract and integrate information from different perceptual modalities (such as text, images, voice, video, etc.), perform comprehensive analysis and prediction, and generate results in the form required by the user (such as text, images, etc.). For example, a multimodal visual language question answering model can receive a question input containing image and text information, and jointly extract and analyze the features of the image and text information to generate the corresponding answer to the question.

[0003] In the related art, when extracting features from images and texts, multimodal models usually concatenate the image representation vector with the text representation vector and perform self-attention processing based on the concatenated vector to extract features from the image and text. However, since the dimension of the image representation vector is usually large, and the larger the image resolution, the larger the dimension, the larger the concatenated vector will be. In addition, since the time complexity of the self-attention mechanism is O(N 2 )(N can represent the dimension of the splicing vector), therefore, when the dimension of the splicing vector is too large, a large amount of processing will be generated, which will not only easily occupy computing resources seriously, but also easily prolong the processing time, thereby bringing a bad user experience. Summary of the invention

[0004] The purpose of the present invention is to provide a text generation method, device, electronic device, program product and storage medium, which can reduce the computing power requirements of the model by adjusting the feature interaction mode between image and text, thereby effectively improving the user experience.

[0005] In order to solve the above technical problems, the present invention provides a text generation method, comprising:

[0006] Receiving an input image and an input text, and encoding the input image and the input text respectively to obtain an image representation vector and a text representation vector;

[0007] Using the image representation vector to perform cross-attention processing on the text representation vector, and performing self-attention processing on the cross-attention processing result to obtain a feature extraction vector;

[0008] Logical calculation is performed on the feature extraction vector to obtain output text generated according to the semantics of both the input image and the input text.

[0009] Optionally, performing cross attention processing on the text representation vector using the image representation vector includes:

[0010] Performing root mean square normalization processing on both the text representation vector and the image representation vector;

[0011] The processed text representation vector is set as query data, and the processed image representation vector is set as key data and value data;

[0012] Cross-attention processing is performed on the query data, the key data, and the value data, and linear layer processing is performed on the processing result.

[0013] Optionally, the input image includes a high-resolution image and a low-resolution image, the image size of the high-resolution image is larger than a preset size, and the image size of the low-resolution image is not larger than the preset size, and the high-resolution image representation vector of the high-resolution image and the low-resolution image representation vector of the low-resolution image are cross-attention processed with the text representation vector in sequence.

[0014] Optionally, the using the image representation vector to perform cross attention processing on the text representation vector, and performing self attention processing on the cross attention processing result to obtain a feature extraction vector, includes:

[0015] Inputting both the high-resolution image representation vector and the text representation vector into a first cross-attention unit, so that the first cross-attention unit uses the high-resolution image representation vector to perform the cross-attention process on the text representation vector to obtain a first feature vector;

[0016] Inputting the first feature vector into a first self-attention unit, so that the first self-attention unit performs the self-attention processing on the first feature vector to obtain a second feature vector;

[0017] Inputting the low-resolution image representation vector and the second feature vector into a second cross-attention unit, so that the second cross-attention unit uses the low-resolution image representation vector to perform the cross-attention process on the second feature vector to obtain a third feature vector;

[0018] The third eigenvector is input into a second self-attention unit, so that the second self-attention unit performs the self-attention processing on the third eigenvector to obtain the feature extraction vector.

[0019] Optionally, receiving an input image includes:

[0020] Receive an original input image;

[0021] The resolution of the original input image is adjusted to obtain the high-resolution image and the low-resolution image.

[0022] Optionally, the first cross-attention unit and the second cross-attention unit both include a cross-attention layer and a first multi-layer perceptron layer, and the first multi-layer perceptron performs multi-layer perceptron processing on the output of the cross-attention layer; the first self-attention unit and the second self-attention unit both include a self-attention layer and a second multi-layer perceptron layer, and the second multi-layer perceptron performs multi-layer perceptron processing on the output of the sub-attention layer.

[0023] Optionally, the first cross-attention unit, the first self-attention unit, the second cross-attention unit, and the second self-attention unit form a feature extraction unit, and multiple feature extraction units are connected in series.

[0024] Optionally, encoding the input image comprises:

[0025] Converting the color mode of the input image into a preset color mode, and determining the length and width of the input image;

[0026] A background color is set according to the color mean of the input image, a maximum value of the length and width of the input image is used as the length and width of the background image, a background image is generated based on the background color and the length and width of the background image, and the input image is set in the background image to obtain a new input image;

[0027] Convert the new input image into a first tensor, and regularize the first tensor to obtain a second tensor;

[0028] Encoding the second tensor using an image encoder to obtain a third tensor;

[0029] Perform linear layer processing on the third tensor to obtain the image representation vector.

[0030] Optionally, encoding the input text includes:

[0031] Perform vector embedding processing on the input text to obtain the text representation vector.

[0032] Optionally, the using the image representation vector to perform cross attention processing on the text representation vector, and performing self attention processing on the cross attention processing result to obtain a feature extraction vector, includes:

[0033] The image feature vector and the text feature vector are sent to a heterogeneous computing device so that the heterogeneous computing device executes the step of performing cross-attention processing on the text representation vector using the image representation vector, and performing self-attention processing on the cross-attention processing result to obtain a feature extraction vector.

[0034] Optionally, the heterogeneous computing device is a graphics card device.

[0035] The present invention also provides a text generation device, comprising:

[0036] An encoding module, used for receiving an input image and an input text, and encoding the input image and the input text respectively to obtain an image representation vector and a text representation vector;

[0037] A feature extraction module, used to perform cross-attention processing on the text representation vector using the image representation vector, and perform self-attention processing on the cross-attention processing result to obtain a feature extraction vector;

[0038] The logic calculation module is used to perform logic calculation on the feature extraction vector to obtain output text generated according to the semantics of both the input image and the input text.

[0039] The present invention also provides an electronic device, comprising:

[0040] Memory for storing computer programs;

[0041] A processor is used to implement the text generation method as described above when executing the computer program.

[0042] Optionally, the electronic device further includes:

[0043] A heterogeneous computing device is used to receive the image representation vector and the text representation vector sent by the processor, use the image representation vector to perform cross-attention processing on the text representation vector, and perform self-attention processing on the cross-attention processing result to obtain a feature extraction vector.

[0044] The present invention also provides a computer program product, comprising a computer program / instruction, which implements the steps of the text generation method as described above when executed by a processor.

[0045] The present invention also provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are loaded and executed by a processor, the text generation method as described above is implemented.

[0046] The present invention provides a text generation method, comprising: receiving an input image and an input text, and encoding the input image and the input text respectively to obtain an image representation vector and a text representation vector; performing cross-attention processing on the text representation vector using the image representation vector, and performing self-attention processing on the cross-attention processing result to obtain a feature extraction vector; performing logical calculation on the feature extraction vector to obtain an output text generated according to the semantics of both the input image and the input text.

[0047] It can be seen that after receiving the input image and the input text, the present invention can first encode the input image and the input text respectively to obtain the image representation vector and the text representation vector. Subsequently, unlike the related art, the present invention does not splice the image representation vector and the text representation vector, and performs self-attention processing based on the spliced ​​vector, but can use the image representation vector to perform cross-attention processing on the text representation vector, and then perform self-attention processing on the cross-attention processing result to obtain a feature extraction vector, that is, the image representation vector can be used to perform feature interaction on the text representation vector alone, and then the interacted result corresponding to the text representation vector is used for self-attention interaction. In this way, since the image processing in the model adopts the cross-attention mechanism, the quadratic increase in the calculation of the length of the input sequence will not be generated; and since the dimension of the text representation vector is usually small, the computing power required for self-attention interaction can also be reduced, so the computing power requirement of the model can be reduced, thereby effectively improving the user experience. The present invention also provides a text generation device, an electronic device, a computer program product and a computer-readable storage medium, which have the above-mentioned beneficial effects. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.

[0049] Figure 1 A flowchart of a text generation method provided by an embodiment of the present invention;

[0050] Figure 2 A schematic diagram of a cross-attention processing provided by an embodiment of the present invention;

[0051] Figure 3 A schematic diagram of a multimodal model provided by an embodiment of the present invention;

[0052] Figure 4 A schematic diagram of another multimodal model provided by an embodiment of the present invention;

[0053] Figure 5 A structural block diagram of a text generation device provided by an embodiment of the present invention;

[0054] Figure 6 The present invention provides a structural block diagram of an electronic device. DETAILED DESCRIPTION

[0055] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0056] A multimodal model is a machine learning model that involves multiple data sources and data types. It can extract and integrate information from different perceptual modalities (such as text, images, voice, video, etc.), perform comprehensive analysis and prediction, and generate results in the form required by the user (such as text, images, etc.). For example, a multimodal visual language question answering model can receive a question input containing image and text information, and jointly extract and analyze the features of the image and text information to generate the corresponding answer to the question.

[0057] In the related art, when extracting features from images and texts, multimodal models usually concatenate the image representation vector with the text representation vector, and perform self-attention processing based on the concatenated representation vector to extract features from the image and text. However, since the dimension of the image representation vector is related to the image resolution, the larger the image resolution, the larger the image representation vector. Therefore, images with larger resolutions will directly lead to an excessively large dimension of the concatenated representation vector. In addition, the time complexity of the self-attention mechanism is O(N 2 ), where represents the size of the input sequence, i.e., the dimension of the concatenated representation vector. Therefore, images with too large a resolution will increase the amount of computation for self-attention processing by a factor of two, which will not only easily occupy computing resources, but also easily prolong processing time, thus bringing a bad user experience.

[0058] In the related art, in order to avoid images with too high resolution bringing too much computation to the model, the resolution of the image is usually limited, such as limiting it to 336*336 or 448*448 pixels, but this will directly affect the amount of information that the model can extract from the image. The related art can also cut the high-resolution image into several small blocks, and perform separate feature extraction on each small block. However, the cutting of the image will cause the image content to be incoherent, affect the model's extraction of image information, and then cause the model to produce wrong answers. This situation is particularly obvious when image document processing or charts involve OCR processing (Optical Character Recognition). In order to increase the resolution of the input image without significantly increasing the amount of computation and avoid cutting the image, the present invention can provide a text generation method, which can reduce the computing power requirements of the model by adjusting the feature interaction method between the image and the text, thereby effectively improving the user experience.

[0059] It should be noted that the embodiment of the present invention does not limit the type of hardware device for executing the method, and can be set according to actual application requirements, for example, it can be a personal computer, a server or other special equipment, which can be set according to actual application requirements. In addition, all steps in the method can be executed by a central processing unit (CPU); in order to improve the processing effect, some steps can also be sent from the central processing unit to other heterogeneous computing devices for execution, where heterogeneous computing devices refer to other computing devices other than the central processing unit, such as graphics card devices (GPU), FPGA (Field Programmable Gate Array), etc.

[0060] For easier understanding, please refer to Figure 1 , Figure 1 A flowchart of a text generation method provided by an embodiment of the present invention, the method may include:

[0061] S101 . Receive an input image and an input text, and encode the input image and the input text respectively to obtain an image representation vector and a text representation vector.

[0062] One possible form of image and text input is:

[0063] {

[0064] "index":"0",

[0065] "input":[

[0066] {

[0067] "image":" / image / path / namg.jpeg"

[0068] "text":"Please describe this picture"

[0069] } ]

[0071] }

[0072] Among them, index is the input sequence number, input represents the actual input content, image is the path of the input image, and text is the input text.

[0073] It should be noted that the embodiment of the present invention does not limit the resolution of the input image, nor does it limit the length of the input text, which can be set according to actual application requirements. It is worth pointing out that since the embodiment of the present invention improves the way in which the model extracts image features and text features, when the input image has a larger resolution (i.e., image size), the embodiment of the present invention can achieve a better effect of reducing the amount of calculation. In addition, to facilitate model processing, the resolution of the input image can be a plurality of preset resolutions of different sizes. The embodiment of the present invention does not limit the specific value of the preset resolution, which can be set according to actual application requirements.

[0074] Further, in order to extract features and interact features of input images and input texts, the embodiment of the present invention first needs to encode the input images and input texts to convert the images and texts into image representation vectors and text representation vectors that can be processed by the model. The embodiment of the present invention does not limit how to encode the input images, and can be set according to actual application requirements. For example, to facilitate model processing, the input image can be first converted into a square. Subsequently, the input image that has completed the shape conversion can be converted into a tensor (Tensor), and the tensor is regularized. Subsequently, the tensor can be encoded using an image encoder (such as the Clip-Vit-L14 model) to obtain the encoded tensor. Subsequently, the encoded tensor can also be processed by a linear layer (Linear) to further adjust the hidden layer dimension in the tensor to obtain the image representation vector. For the process of converting the input image into a square, the color mode of the input image is first converted into a preset color mode, and the preset color mode can be an RGB mode, a CMYK mode, an HSB mode, a Lab color mode, etc., so that the model performs feature extraction processing on the image based on the preset color mode. Subsequently, the length and width of the input image can be determined. Subsequently, the background color can be set according to the color mean of the input image, and the maximum value of the length and width of the input image is used as the length and width value of the background image. Then, a background image is generated based on the background color and the length and width value of the background image, and the input image is set in the background image (such as set in the center of the background image) to obtain the input image after shape transformation.

[0075] Based on this, encoding the input image may include:

[0076] Step 11: Convert the color mode of the input image into a preset color mode, and determine the length and width of the input image.

[0077] Step 12: setting the background color according to the color mean of the input image, taking the maximum value of the length and width of the input image as the length and width of the background image, generating a background image based on the background color and the length and width of the background image, and setting the input image in the background image to obtain a new input image;

[0078] Step 13: converting the new input image into a first tensor, and regularizing the first tensor to obtain a second tensor;

[0079] Step 14: Encode the second tensor using an image encoder to obtain a third tensor;

[0080] Step 15: Perform linear layer processing on the third tensor to obtain the image representation vector.

[0081] It should be noted that, since the resolutions of the input images may be different, the dimensions of the first tensor, the second tensor, and the third tensor corresponding to different images may be different.

[0082] Furthermore, the embodiment of the present invention does not limit how to encode the input text, and can be set according to actual application requirements, such as performing vector embedding processing (embedding) on ​​the input text to obtain the text representation vector.

[0083] Based on this, encoding the input text may include:

[0084] Step 21: Perform vector embedding processing on the input text to obtain the text representation vector.

[0085] S102, using the image representation vector to perform cross-attention processing on the text representation vector, and performing self-attention processing on the cross-attention processing result to obtain a feature extraction vector.

[0086] This step aims to extract features from the image representation vector and the text representation vector so that the model can obtain the semantic information in the input image and the input text, and the model can generate the corresponding answer result text according to the semantic information. Unlike the related art, the embodiment of the present invention does not splice the image representation vector with the text representation vector, and performs self-attention processing on the obtained spliced ​​representation vector, but can first use the image representation vector to perform cross-attention processing on the text representation vector, and then perform self-attention processing on the cross-attention processing result to obtain the feature extraction vector. The reason why the amount of calculation can be reduced in this way is that the amount of calculation generated by the self-attention processing of the spliced ​​representation vector is the square of the dimension of the spliced ​​representation vector, while the amount of calculation generated by the cross-attention processing of the text representation vector using the image representation vector is the product of the dimension of the image representation vector and the dimension of the text representation vector. Both the dimension of the image representation vector and the dimension of the text representation vector are smaller than the dimension of the spliced ​​representation vector, so the product of the dimension of the image representation vector and the dimension of the text representation vector is obviously smaller than the square of the dimension of the spliced ​​representation vector. In addition, the dimension of the cross-attention processing result obtained by performing cross-attention processing on the text representation vector using the image representation vector is actually the dimension of the text representation vector. Generally, since the input text is short, the dimension of the text representation vector is also small, so the amount of calculation generated by performing self-attention processing on the cross-attention processing result with the dimension of the text representation vector is also small. Therefore, the embodiment of the present invention can significantly reduce the amount of model calculation and improve processing efficiency by improving the way in which the multimodal model extracts features from images and texts.

[0087] Specifically, the present invention can set the text representation vector as query data (Query, Q), and set the image representation vector as key data (Key, K) and value data (Value, V), and perform cross-attention processing based on the query data, key data, and value data. Of course, in order to improve the processing efficiency of the model, the text representation vector and the image representation vector can first be subjected to root mean square normalization (RMSNorm, Root Mean Square Layer Normalization) before the cross-attention processing; and after the cross-attention processing is completed, the cross-attention processing can be subjected to linear layer processing (Linear).

[0088] Based on this, performing cross attention processing on the text representation vector using the image representation vector may include:

[0089] Step 31: Perform root mean square normalization processing on both the text representation vector and the image representation vector.

[0090] Step 32: Set the processed text representation vector as query data, and set the processed image representation vector as key data and value data.

[0091] Step 33: Perform cross-attention processing on the query data, the key data and the value data, and perform linear layer processing on the processing results.

[0092] Specifically, the query data, the key data and the value data are cross-attention processed according to the following formula:

[0093]

[0094] Among them, softmax() represents a normalized exponential function, Q represents the query data, K represents the key data, V represents the value data, T represents a transposition operation, scale represents the dimension corresponding to each attention head in a plurality of attention heads, and the attention head is used to split the query data, the key data and the value data.

[0095] To understand the process of cross attention processing, please refer to Figure 2 , Figure 2 A schematic diagram of a cross-attention processing provided by an embodiment of the present invention.

[0096] Further, step S102 can be performed multiple times. It should be noted that starting from the second round of execution, step S102 is transformed into: using the image representation vector to perform cross-attention processing on the feature extraction vector of the previous round, and performing self-attention processing on the result of the cross-attention processing to obtain the feature extraction vector of this round.

[0097] S103: Performing logical calculation on the feature extraction vector to obtain output text generated according to the semantics of both the input image and the input text.

[0098] In this step, after the feature extraction is completed, it is only necessary to perform a logical calculation (Logits) on the feature extraction vector to obtain an output text generated according to the semantics of the input image and the input text. For example, if the input image contains the content: a little girl flying a kite, and the input text is: please describe this picture, then the output text may be: the picture depicts a child flying a kite. It should be noted that the embodiment of the present invention does not limit the specific process of the logical calculation, and reference may be made to the relevant technology of machine learning.

[0099] Based on the above embodiments, after receiving the input image and the input text, the present invention can first encode the input image and the input text respectively to obtain the image representation vector and the text representation vector. Subsequently, unlike the related art, the present invention does not splice the image representation vector and the text representation vector, and performs self-attention processing based on the spliced ​​vector, but can use the image representation vector to perform cross-attention processing on the text representation vector, and then perform self-attention processing on the cross-attention processing result to obtain a feature extraction vector, that is, the image representation vector can be used to perform feature interaction on the text representation vector alone, and then the interacted result corresponding to the text representation vector is used for self-attention interaction. In this way, since the image processing in the model adopts the cross-attention mechanism, the quadratic increase in the calculation of the length of the input sequence will not be generated; and since the dimension of the text representation vector is usually small, it can also reduce the computing power required for self-attention interaction, so the computing power requirement of the model can be reduced, thereby effectively improving the user experience.

[0100] Based on the above embodiments, in order to further reduce the amount of calculation generated by the interaction between image features and text features, the embodiments of the present invention can input a high-resolution image and a low-resolution image, and use the high-resolution image features and the low-resolution image features in turn to perform cross-attention processing interaction with the text features, so as to use the low-resolution image to further reduce the amount of calculation. In other words, the input image includes a high-resolution image and a low-resolution image, the image size of the high-resolution image is larger than a preset size, the image size of the low-resolution image is not larger than the preset size, and the high-resolution image representation vector of the high-resolution image and the low-resolution image representation vector of the low-resolution image are cross-attention processed with the text representation vector in turn. In addition, the high-resolution image and the low-resolution image differ only in image size, and contain the same image content.

[0101] To facilitate model processing, the embodiment of the present invention can also set corresponding preset resolutions for high-resolution images and low-resolution images, that is, the resolutions of the input high-resolution images and low-resolution images must be the same as the preset resolutions. The electronic device can adjust the image resolution by itself for the convenience of users. For example, after receiving the original input image, the electronic device can adjust the resolution of the original input image (such as enlarging or reducing the resolution) to obtain the required high-resolution image and low-resolution image.

[0102] Based on this, receiving an input image may include:

[0103] Step 41: receiving an original input image;

[0104] Step 42: Adjust the resolution of the original input image to obtain the high-resolution image and the low-resolution image.

[0105] The following will introduce the process of using the high-resolution image representation vector and the low-resolution image representation vector to perform cross-attention processing on the text representation vector in turn. In one possible case, using the image representation vector to perform cross-attention processing on the text representation vector, and performing self-attention processing on the cross-attention processing result to obtain a feature extraction vector, may include:

[0106] S201. Input both the high-resolution image representation vector and the text representation vector into a first cross-attention unit, so that the first cross-attention unit uses the high-resolution image representation vector to perform the cross-attention processing on the text representation vector to obtain a first feature vector.

[0107] The high-resolution image representation vector is an image representation vector obtained by encoding a high-resolution image. The embodiment of the present invention may first use the high-resolution image representation vector to perform cross-attention processing on the text representation vector. Specifically, the embodiment of the present invention may set a first cross-attention unit, which is dedicated to performing cross-attention processing on the high-resolution image representation vector and the text representation vector. It should be noted that the relevant description of the cross-attention processing is the same as the above embodiment, so it will not be repeated here.

[0108] S202. Input the first feature vector into a first self-attention unit, so that the first self-attention unit performs the self-attention processing on the first feature vector to obtain a second feature vector.

[0109] After completing the first round of cross attention processing, the embodiment of the present invention will perform self-attention processing on the obtained first eigenvector. The embodiment of the present invention can set a first self-attention unit, which is dedicated to performing self-attention processing on the first eigenvector.

[0110] S203. Input the low-resolution image representation vector and the second feature vector into a second cross-attention unit, so that the second cross-attention unit uses the low-resolution image representation vector to perform the cross-attention processing on the second feature vector to obtain a third feature vector.

[0111] The low-resolution image representation vector is an image representation vector obtained by encoding the low-resolution image. The embodiment of the present invention can use the low-resolution image representation vector to continue to perform cross-attention processing on the second feature vector. Specifically, the embodiment of the present invention can set a second cross-attention unit, which is dedicated to performing cross-attention processing on the low-resolution image representation vector and the second feature vector.

[0112] S204. Input the third eigenvector into a second self-attention unit, so that the second self-attention unit performs the self-attention processing on the third eigenvector to obtain the feature extraction vector.

[0113] After completing the second round of cross attention processing, the embodiment of the present invention will perform self-attention processing on the obtained third eigenvector. The embodiment of the present invention can set a second self-attention unit, which is dedicated to performing self-attention processing on the third eigenvector.

[0114] It can be seen that the embodiment of the present invention can first use the high-resolution image representation vector to perform feature interaction on the text representation vector, and then use the low-resolution image representation vector to continue the feature interaction. In this way, in multiple rounds of feature interaction, the embodiment of the present invention can avoid using the high-resolution image representation vector for interaction all the time, and can use the high-resolution image representation vector and the low-resolution image representation vector for interaction in turn, thereby effectively reducing the amount of calculation.

[0115] Furthermore, in order to improve the processing performance of the above units, each unit may also include a multilayer perceptron (MLP). Specifically, the first cross-attention unit and the second cross-attention unit both include a cross-attention layer and a first multilayer perceptron layer, and the first multilayer perceptron performs multilayer perceptron processing on the output of the cross-attention layer; the first self-attention unit and the second self-attention unit both include a self-attention layer and a second multilayer perceptron layer, and the second multilayer perceptron performs multilayer perceptron processing on the output of the sub-attention layer. For ease of understanding, please refer to Figure 3 , Figure 3 A schematic diagram of a multimodal model provided by an embodiment of the present invention. Wherein, CrossAttention represents a cross attention layer, MLP represents a multi-layer perceptron, SelfAttention represents a self-attention layer, CrossAttention and its subsequent MLP form a cross attention unit, and SelfAttention and its subsequent MLP form a self-attention unit. Furthermore, the embodiment of the present invention can use the first cross attention unit, the first self-attention unit, the second cross attention unit, and the second self-attention unit to form a feature extraction unit. In order to improve the feature extraction capability, a plurality of the feature extraction units can also be used in series to perform multiple rounds of feature interaction on the high-resolution image representation vector, the low-resolution image representation vector, and the text representation vector.

[0116] Further, to understand the overall structure of the multimodal model, please refer to Figure 4 , Figure 4A schematic diagram of another multimodal model provided by an embodiment of the present invention, wherein the model may include a high-resolution image pre-processing and encoding module, a low-resolution image pre-processing and encoding module, an Embedding module (text embedding module), an image and text feature extraction module, and a Logits calculation module (logic calculation module). The following will focus on the high-resolution image pre-processing and encoding module, the low-resolution image pre-processing and encoding module, and the image and text feature extraction module.

[0117] 1. High-resolution image pre-processing and encoding module

[0118] like Figure 4 The dotted box in the upper left corner shows the high-resolution image pre-processing and encoding module in the multimodal model. In this module, the Clip-ViT-L14 model can be used for image encoding. The image pre-processing includes the following steps:

[0119] Step 1: Open the image and convert it to RGB mode, and calculate the length and width of the image.

[0120] Step 2: The fixed length is determined based on the larger value of the length and width of the original image and the normalized mean in the pre-processing of the image coding model, and the background image of the fixed length is calculated with the normalized mean as the background color.

[0121] Step 3: Copy the original image to the center of the background image to get a new input image.

[0122] Step 4: Reset the size of the new input image obtained in step 3 to 1120*1120 image resolution.

[0123] Step 5: Convert the 1120*1120 pixel image obtained in step 4 into a Tensor that expresses the image information.

[0124] Step 6: Regularize the Tensor expressing the image information obtained in step 5.

[0125] Step 7: Input the regularized Tensor in step 6 into the Clip-ViT-L14 model for image information encoding to obtain a Tensor of length [6400, 896].

[0126] Step 8: Pass the [6400, 896] length Tensor in step 7 through a Linear layer to change the hidden layer dimension from [6400, 896] to [6400, 2048].

[0127] 2. Low-resolution image pre-processing and encoding module

[0128] like Figure 4The dotted box in the upper right corner shows the low-resolution image pre-processing and encoding module in the multimodal model. In this module, the Clip-ViT-G14 model can be used for image encoding. The image pre-processing includes the following steps:

[0129] Step 1: Open the image and convert it to RGB mode, and calculate the length and width of the image.

[0130] Step 2: The fixed length is determined based on the larger value of the length and width of the original image and the normalized mean in the pre-processing of the image coding model, and the background image of the fixed length is calculated with the normalized mean as the background color.

[0131] Step 3: Copy the original image to the center of the background image to get a new input image.

[0132] Step 4: Reset the size of the new input image obtained in step 3 to 224*224 image resolution.

[0133] Step 5: Convert the 224*224 pixel image obtained in step 4 into a Tensor that expresses the image information.

[0134] Step 6: Regularize the Tensor expressing the image information obtained in step 5.

[0135] Step 7: Input the regularized Tensor in step 6 into the Clip-ViT-G14 model for image information encoding to obtain a Tensor of length [256, 1664].

[0136] Step 8: Pass the [256, 1664] length Tensor in step 7 through a Linear layer to change the hidden layer dimension from [256, 1664] to [256, 2048].

[0137] 3. Image and text feature extraction module

[0138] like Figure 4 The main structure is an N-layer image and text feature extraction module, which has three inputs: input text information, high-resolution image features, and low-resolution image features.

[0139] For text information, it will first pass through the Embedding layer to convert the text into a vector representation. A simple example is to input a sentence: "Please describe this picture". This sentence will be converted into a [9,2048] Tensor after the Embedding layer.

[0140] Figure 4The main body of the module is a feature extraction module consisting of N layers of cross-attention layers (CrossAttention), self-attention layers (SelfAttention) and multi-layer perceptrons (MLP). In this combined module, the high-resolution image features ([6400, 2048]) are first subjected to cross-attention processing with the output ([9, 2048]) from the text embedding module (Embedding). Specifically, the input text features are first subjected to root mean square normalization (RMSNorm), and then the query data Query (Q) is calculated through linear processing (Linear). The high-resolution image features are first subjected to root mean square normalization, and then the key data Key (K) and value data Value (V) are calculated through linear processing. Then the three values ​​QKV complete the Softmax (QKT) V / scale calculation, where scale represents the corresponding dimension of each attention head in the calculation. When the number of attention heads is 16, the scale is (2048 / 16) 0.5 After the above calculations, the data will pass through a linear layer (Linear) and get the text feature output after the image and text interact. After the cross-attention processing operation, the model will pass through a multi-layer perceptron layer, followed by a self-attention processing operation, and there will also be a multi-layer perceptron layer following the self-attention layer. After the self-attention processing and the multi-layer perceptron processing, the text features will interact with the low-resolution image features. In the low-resolution cross-attention processing, the interaction between the image and text features is similar to the above high-resolution calculation process, except that the number of image features expressed is different, which will not be repeated here.

[0141] After Logits calculation, the final model will map the text output features to the output text, and convert the text feature values ​​into output text. An output example is: "The picture depicts a child flying a kite."

[0142] From the above introduction, it can be seen that when the present invention introduces image features in the multimodal model, two image encoders of different sizes are first introduced. The interactive method adopts a cross-attention processing operation. While introducing high-resolution features, the computational complexity of the model is further reduced by respectively interacting high-resolution and low-resolution images with text features. This is different from the interactive method of splicing image features and text features and then performing self-attention processing. It limits the computational complexity of the model while improving the image resolution.

[0143] Based on the above embodiment, in order to give full play to the advantages of various computing devices, for feature extraction operations, the central processor of the electronic device can send the image representation vector and the text representation vector to the heterogeneous computing device, so as to use the heterogeneous computing device to perform feature extraction on the image representation vector and the text representation vector. Based on this, the use of the image representation vector to perform cross-attention processing on the text representation vector, and the self-attention processing on the cross-attention processing result to obtain the feature extraction vector, can include:

[0144] S301. Send the image representation vector and the text representation vector to a heterogeneous computing device, so that the heterogeneous computing device executes the step of performing cross-attention processing on the text representation vector using the image representation vector, and performing self-attention processing on the cross-attention processing result to obtain a feature extraction vector.

[0145] It should be noted that the embodiment of the present invention does not limit the type of heterogeneous computing devices, for example, it may be a graphics card device.

[0146] The text generation device provided in an embodiment of the present invention is introduced below. The intelligent gateway described below and the message information processing method of the home intelligent gateway described above can be referred to each other.

[0147] Please refer to Figure 5 , Figure 5 A structural block diagram of a text generation device provided by an embodiment of the present invention, the device may include:

[0148] The encoding module 501 is used to receive an input image and an input text, and encode the input image and the input text respectively to obtain an image representation vector and a text representation vector;

[0149] A feature extraction module 502 is used to perform cross-attention processing on the text representation vector using the image representation vector, and perform self-attention processing on the cross-attention processing result to obtain a feature extraction vector;

[0150] The logic calculation module 503 is used to perform logic calculation on the feature extraction vector to obtain output text generated according to the semantics of both the input image and the input text.

[0151] Optionally, the feature extraction module 502 includes:

[0152] A normalization processing submodule, used for performing root mean square normalization processing on both the text representation vector and the image representation vector;

[0153] A setting submodule is used to set the processed text representation vector as query data and the processed image representation vector as key data and value data;

[0154] The cross-attention processing submodule is used to perform cross-attention processing on the query data, the key data and the value data, and perform linear layer processing on the processing results.

[0155] Optionally, the cross attention processing submodule may be used to:

[0156] The query data, the key data and the value data are cross-attention processed according to the following formula:

[0157] softmsacxa(lQeK) T V;

[0158] Among them, softmax() represents a normalized exponential function, Q represents the query data, K represents the key data, V represents the value data, T represents a transposition operation, scale represents the dimension corresponding to each attention head in a plurality of attention heads, and the attention head is used to split the query data, the key data and the value data.

[0159] Optionally, the input image includes a high-resolution image and a low-resolution image, the image size of the high-resolution image is larger than a preset size, and the image size of the low-resolution image is not larger than the preset size;

[0160] The feature extraction module 502 may include:

[0161] A first input submodule, configured to input both the high-resolution image representation vector and the text representation vector into a first cross-attention unit, so that the first cross-attention unit performs the cross-attention processing on the text representation vector using the high-resolution image representation vector to obtain a first feature vector;

[0162] A second input submodule, configured to input the first feature vector into a first self-attention unit, so that the first self-attention unit performs the self-attention processing on the first feature vector to obtain a second feature vector;

[0163] A third input submodule, configured to input the low-resolution image representation vector and the second feature vector into a second cross-attention unit, so that the second cross-attention unit performs the cross-attention processing on the second feature vector using the low-resolution image representation vector to obtain a third feature vector;

[0164] The fourth input submodule is used to input the third feature vector into the second self-attention unit, so that the second self-attention unit performs the self-attention processing on the third feature vector to obtain the feature extraction vector.

[0165] Optionally, the encoding module 501 may include:

[0166] An image receiving submodule, used for receiving an original input image;

[0167] The image resolution adjustment submodule is used to adjust the resolution of the original input image to obtain the high-resolution image and the low-resolution image.

[0168] Optionally, the first cross-attention unit and the second cross-attention unit both include a cross-attention layer and a first multi-layer perceptron layer, and the first multi-layer perceptron performs multi-layer perceptron processing on the output of the cross-attention layer; the first self-attention unit and the second self-attention unit both include a self-attention layer and a second multi-layer perceptron layer, and the second multi-layer perceptron performs multi-layer perceptron processing on the output of the sub-attention layer.

[0169] Optionally, the first cross-attention unit, the first self-attention unit, the second cross-attention unit, and the second self-attention unit form a feature extraction unit, and multiple feature extraction units are connected in series.

[0170] Optionally, the encoding module 501 may include:

[0171] A first pre-processing submodule, used for converting the color mode of the input image into a preset color mode, and determining the length and width of the input image;

[0172] A second pre-processing submodule is used to set a background color according to a color mean value of the input image, take a maximum value of a length and a width of the input image as a length and a width value of the background image, generate a background image based on the background color and the length and width values ​​of the background image, and set the input image in the background image to obtain a new input image;

[0173] A tensor conversion submodule, used for converting the new input image into a first tensor, and regularizing the first tensor to obtain a second tensor;

[0174] An image encoding submodule, used for encoding the second tensor using an image encoder to obtain a third tensor;

[0175] The linear processing submodule is used to perform linear layer processing on the third tensor to obtain the image representation vector.

[0176] Optionally, the encoding module 501 may include:

[0177] The text encoding submodule is used to perform vector embedding processing on the input text to obtain the text representation vector.

[0178] Optionally, the feature extraction module 502 includes:

[0179] The task sending submodule is used to send the image representation vector and the text representation vector to a heterogeneous computing device, so that the heterogeneous computing device executes the steps of using the image representation vector to perform cross-attention processing on the text representation vector, and performing self-attention processing on the cross-attention processing result to obtain a feature extraction vector.

[0180] Please refer to Figure 6 , Figure 6 This is a structural block diagram of an electronic device provided in an embodiment of the present invention. The embodiment of the present invention provides an electronic device 60, including a processor 61 and a memory 62; wherein the memory 62 is used to store a computer program; and the processor 61 is used to execute the text generation method provided in the aforementioned embodiment when executing the computer program.

[0181] For the specific process of the above-mentioned text generation method, please refer to the corresponding content provided in the above-mentioned embodiment, which will not be repeated here.

[0182] Furthermore, the memory 62 as a carrier for storing resources may be a read-only memory, a random access memory, a magnetic disk or an optical disk, etc., and the storage method may be temporary storage or permanent storage.

[0183] In addition, the electronic device 60 also includes a power supply 63, a communication interface 64, an input / output interface 65 and a communication bus 66; wherein the power supply 63 is used to provide working voltage for each hardware device on the electronic device 60; the communication interface 64 can create a data transmission channel between the electronic device 60 and external devices, and the communication protocol it follows is any communication protocol that can be applied to the technical solution of the present invention, and is not specifically limited here; the input / output interface 65 is used to obtain external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs, and is not specifically limited here.

[0184] Optionally, the electronic device may also include: a heterogeneous computing device for receiving the image representation vector and the text representation vector sent by the processor, performing cross-attention processing on the text representation vector using the image representation vector, and performing self-attention processing on the cross-attention processing result to obtain a feature extraction vector.

[0185] Optionally, the heterogeneous computing device is a graphics card device.

[0186] An embodiment of the present invention further provides a computer program product, including a computer program / instruction, which implements the text generation method described in the above embodiment when executed by a processor.

[0187] Since the embodiments of the computer program product part correspond to the embodiments of the text generation method part, please refer to the description of the embodiments of the text generation method part for the embodiments of the computer program product part, and will not be repeated here.

[0188] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the text generation method described in the above embodiment is implemented.

[0189] Since the embodiments of the computer-readable storage medium part correspond to the embodiments of the text generation method part, the embodiments of the storage medium part refer to the description of the embodiments of the text generation method part, which will not be repeated here.

[0190] The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.

[0191] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0192] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.

[0193] The above is a detailed introduction to a text generation method, device, electronic device, program product and storage medium provided by the present invention. Specific examples are used herein to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present invention, the present invention can also be improved and modified, and these improvements and modifications also fall within the scope of protection of the present invention.

Claims

1. A text generation method, characterized in that: include: Receiving an input image and an input text, and encoding the input image and the input text respectively to obtain an image representation vector and a text representation vector; Using the image representation vector to perform cross-attention processing on the text representation vector, and performing self-attention processing on the cross-attention processing result to obtain a feature extraction vector; Logical calculation is performed on the feature extraction vector to obtain output text generated according to the semantics of both the input image and the input text.

2. The text generation method according to claim 1, characterized in that: The using the image representation vector to perform cross attention processing on the text representation vector includes: Performing root mean square normalization processing on both the text representation vector and the image representation vector; The processed text representation vector is set as query data, and the processed image representation vector is set as key data and value data; Cross-attention processing is performed on the query data, the key data, and the value data, and linear layer processing is performed on the processing result.

3. The text generation method according to claim 1, characterized in that: The input image includes a high-resolution image and a low-resolution image, the image size of the high-resolution image is larger than a preset size, the image size of the low-resolution image is not larger than the preset size, and the high-resolution image representation vector of the high-resolution image and the low-resolution image representation vector of the low-resolution image are cross-attention processed with the text representation vector in sequence.

4. The text generation method according to claim 3, characterized in that: The using the image representation vector to perform cross attention processing on the text representation vector, and performing self attention processing on the cross attention processing result to obtain a feature extraction vector, includes: Inputting both the high-resolution image representation vector and the text representation vector into a first cross-attention unit, so that the first cross-attention unit uses the high-resolution image representation vector to perform the cross-attention process on the text representation vector to obtain a first feature vector; Inputting the first feature vector into a first self-attention unit, so that the first self-attention unit performs the self-attention processing on the first feature vector to obtain a second feature vector; Inputting the low-resolution image representation vector and the second feature vector into a second cross-attention unit, so that the second cross-attention unit uses the low-resolution image representation vector to perform the cross-attention process on the second feature vector to obtain a third feature vector; The third eigenvector is input into a second self-attention unit, so that the second self-attention unit performs the self-attention processing on the third eigenvector to obtain the feature extraction vector.

5. The text generation method according to claim 4, characterized in that: The first cross-attention unit and the second cross-attention unit both include a cross-attention layer and a first multi-layer perceptron layer, and the first multi-layer perceptron performs multi-layer perceptron processing on the output of the cross-attention layer; the first self-attention unit and the second self-attention unit both include a self-attention layer and a second multi-layer perceptron layer, and the second multi-layer perceptron performs multi-layer perceptron processing on the output of the sub-attention layer.

6. The text generation method according to claim 4, characterized in that: The first cross-attention unit, the first self-attention unit, the second cross-attention unit, and the second self-attention unit constitute a feature extraction unit, and multiple feature extraction units are connected in series.

7. The text generation method according to claim 1, characterized in that: Encoding the input image, comprising: Converting the color mode of the input image into a preset color mode, and determining the length and width of the input image; A background color is set according to the color mean of the input image, a maximum value of the length and width of the input image is used as the length and width of the background image, a background image is generated based on the background color and the length and width of the background image, and the input image is set in the background image to obtain a new input image; Convert the new input image into a first tensor, and regularize the first tensor to obtain a second tensor; Encoding the second tensor using an image encoder to obtain a third tensor; Perform linear layer processing on the third tensor to obtain the image representation vector.

8. The text generation method according to claim 1, characterized in that: Encoding the input text includes: Perform vector embedding processing on the input text to obtain the text representation vector.

9. The text generation method according to claim 1, characterized in that: The using the image representation vector to perform cross attention processing on the text representation vector, and performing self attention processing on the cross attention processing result to obtain a feature extraction vector, includes: The image representation vector and the text representation vector are sent to a heterogeneous computing device so that the heterogeneous computing device executes the step of performing cross-attention processing on the text representation vector using the image representation vector, and performing self-attention processing on the cross-attention processing result to obtain a feature extraction vector.

10. The text generation method according to claim 9, characterized in that: The heterogeneous computing device is a graphics card device.

11. A text generation device, characterized in that: include: An encoding module, used for receiving an input image and an input text, and encoding the input image and the input text respectively to obtain an image representation vector and a text representation vector; A feature extraction module, used to perform cross-attention processing on the text representation vector using the image representation vector, and perform self-attention processing on the cross-attention processing result to obtain a feature extraction vector; The logic calculation module is used to perform logic calculation on the feature extraction vector to obtain output text generated according to the semantics of both the input image and the input text.

12. An electronic device, characterized in that: include: Memory for storing computer programs; A processor, configured to implement the text generation method according to any one of claims 1 to 10 when executing the computer program.

13. The electronic device according to claim 12, characterized in that: Also includes: A heterogeneous computing device is used to receive the image representation vector and the text representation vector sent by the processor, use the image representation vector to perform cross-attention processing on the text representation vector, and perform self-attention processing on the cross-attention processing result to obtain a feature extraction vector.

14. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the steps of the text generation method according to any one of claims 1 to 10 are implemented.

15. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are loaded and executed by a processor, the text generation method according to any one of claims 1 to 10 is implemented.

Citation Information

Patent Citations

  • Text generation method and device and electronic equipment

    CN111898338A

  • Text guidance image segmentation method based on cross-modal text retrieval attention mechanism

    CN113657400A

  • Data processing method and device, electronic equipment and storage medium

    CN115115913A

  • Text generation method and device, electronic equipment and storage medium

    CN115984874A

  • Multi-scale data fusion method and device, medium and electronic equipment

    CN117113268A

Cited By

  • Data processing method, device and system and computer readable storage medium

    CN120449947A

  • Sample processing method and device, equipment, medium and program product

    CN121614060A