Image generation model training method, image generation method, device and equipment
By fine-tuning the pre-trained diffusion model and adjusting the degree of difference, the problem of poor image quality generated by the diffusion model on rare categories is solved, and higher-quality rare category image generation is achieved.
Patent Information
- Application Number
- CN202410444192.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-12
- Publication Date
- 2024-08-27
- Estimated Expiration
- 2044-04-12
AI Technical Summary
Diffusion models have difficulty generating high-quality images on rare categories with sparse training data, and existing pre-training and fine-tuning training methods are ineffective.
By using the training set to perform fine-tuning training on the pre-trained diffusion model, the difference between the generative image and the training image is determined, and the amount of training data is adjusted according to the difference, and multiple rounds of fine-tuning training are performed to improve the image generation capabilities of rare categories.
The quality of image generation of rare categories by the diffusion model is improved, ensuring that the model can generate images of rare categories with high quality.
Smart Images

Figure CN118552633A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of machine learning, and in particular to an image generation model training method, an image generation method, a device and equipment. Background Art
[0002] The Diffusion Model is a machine learning model that can generate corresponding images based on input information. In related technologies, the diffusion model is generally trained with a large amount of data to obtain a pre-trained version, and then the pre-trained version is fine-tuned according to the training set in the actual business to obtain the final version of the training. However, for rare categories with very little training data, neither pre-training nor fine-tuning training can ensure that the diffusion model can generate high-quality images for rare categories.
[0003] Therefore, how to improve the training effect of the diffusion model on rare categories with scarce training data is a technical problem that technical personnel in this field urgently need to solve. Summary of the invention
[0004] The purpose of the present invention is to provide an image generation model training method, an image generation method, a device and an apparatus, which can improve the training effect of rare categories with scarce training data in a diffusion model.
[0005] In order to solve the above technical problems, the present invention provides an image generation model training method, comprising:
[0006] Fine-tune the pre-trained diffusion model using the training set to obtain a first diffusion model; wherein the training set includes training data of multiple categories, the training data includes category information and training images, and the diffusion model is used to generate an image according to the input information;
[0007] Inputting the category information of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model;
[0008] Determine the difference between each of the generated images and the training images of the same category to obtain the difference corresponding to each category;
[0009] The number of training images of each category in the training set is adjusted according to the difference between each category, and the first diffusion model is fine-tuned using the adjusted training set to obtain a trained second diffusion model.
[0010] Optionally, the category information includes a category description word and a category number;
[0011] Before fine-tuning the pre-trained diffusion model using the training set, it also includes:
[0012] Translating the category description words in each of the category information into Chinese to obtain the Chinese category description words;
[0013] Determine whether there are rare characters in the Chinese category description words that are not stored in the preset dictionary, and when it is determined that the rare characters exist, replace the rare characters with characters in the preset dictionary; wherein the preset dictionary stores characters that can be encoded by the diffusion model;
[0014] The Chinese category description words and category numbers of the same category are used to expand the text to obtain the category text of each category;
[0015] The fine-tuning training of the pre-trained diffusion model using the training set includes:
[0016] Fine-tune the pre-trained diffusion model using the category texts and training images of each category;
[0017] The step of inputting the category information of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model includes:
[0018] Inputting the category texts of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model;
[0019] The fine-tuning training of the first diffusion model using the adjusted training set includes:
[0020] The first diffusion model is fine-tuned using the category texts and training images of each category in the adjusted training set.
[0021] Optionally, adjusting the number of training images of each category in the training set according to the difference between the categories includes:
[0022] Determine a target difference interval in which the difference of each category lies among a plurality of continuous preset difference intervals; each of the preset difference intervals is set with a different weight;
[0023] Set the weight of the target similarity interval corresponding to each category for each category;
[0024] The number of training images in each category in the training set is adjusted according to the weight of each category.
[0025] Optionally, adjusting the number of training images of each category in the training set according to the weight of each category includes:
[0026] According to the weight value of each category, the training images of each category are copied the same number of times to obtain the adjusted training set.
[0027] Optionally, determining the difference between each of the generated images and a training image of the same category to obtain the difference corresponding to each category includes:
[0028] Determine the differences between multiple generated images and multiple training images of the same category, and obtain the differences corresponding to each category.
[0029] Optionally, determining the difference between the multiple generated images and the multiple training images of the same category includes:
[0030] Convert each of the generative images into a corresponding first eigenvector, add all the first eigenvectors to a set of generative eigenvectors, and determine a mean and a covariance matrix corresponding to the set of generative eigenvectors;
[0031] Convert each training image of the same category into a corresponding second eigenvector, add all the second eigenvectors to a set of true eigenvectors, and determine a mean and a covariance matrix corresponding to the set of true eigenvectors;
[0032] The difference between multiple generated images and multiple training images of the same category is determined according to the following formula:
[0033] ;
[0034] Wherein, P represents the real feature vector set, G represents the generated feature vector set, and are the means corresponding to the real feature vector set and the generated feature vector set respectively, and are the covariance matrices corresponding to the real eigenvector set and the generated eigenvector set respectively, Represents the square root of the trace of the covariance matrix.
[0035] Optionally, the fine-tuning training of the first diffusion model using the adjusted training set to obtain a trained second diffusion model includes:
[0036] Using the adjusted training set, perform a first preset iteration round of first fine-tuning training on the first diffusion model to obtain a second initial diffusion model;
[0037] Performing a second preset iteration round of second fine-tuning training on the second initial diffusion model using the adjusted training set, and deriving the second diffusion model of the current iteration version at the end of each round of the second fine-tuning training to obtain multiple iteration versions of the second diffusion model;
[0038] The difference degree corresponding to each category in each iterative version of the second diffusion model is determined, so as to select the optimal diffusion model in each iterative version of the second diffusion model according to the difference degree corresponding to each category in each iterative version of the second diffusion model.
[0039] The present invention also provides an image generation method, comprising:
[0040] Receiving input image generation information;
[0041] The image generation information is input into a trained diffusion model to obtain a generative image output by the diffusion model; the diffusion model is trained using the image generation model training method as described above.
[0042] Optionally, the image generation information includes text description information for the generative image.
[0043] The present invention also provides an image generation model training device, comprising:
[0044] A first training module is used to fine-tune the pre-trained diffusion model using a training set to obtain a first diffusion model; wherein the training set includes training data of multiple categories, the training data includes category information and training images, and the diffusion model is used to generate an image according to input information;
[0045] A testing module, used for inputting the category information of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model;
[0046] A difference calculation module is used to determine the difference between each of the generated images and the training images of the same category to obtain the difference corresponding to each category;
[0047] The second training module is used to adjust the number of training images of each category in the training set according to the difference between each category, and use the adjusted training set to fine-tune the first diffusion model to obtain a trained second diffusion model.
[0048] Optionally, the category information includes a category description word and a category number;
[0049] The image generation model training device further includes:
[0050] A translation module, used for translating the category description words in each category information into Chinese to obtain the Chinese category description words;
[0051] An uncommon character adjustment module, used to determine whether there are uncommon characters in the Chinese category description words that are not stored in the preset dictionary, and when it is determined that the uncommon characters exist, replace the uncommon characters with characters in the preset dictionary; wherein the preset dictionary stores characters that can be encoded by the diffusion model;
[0052] A text expansion module is used to expand the text of the Chinese category description words and category numbers of the same category to obtain category texts of each category;
[0053] The first training module is specifically used for:
[0054] Fine-tune the pre-trained diffusion model using the category texts and training images of each category;
[0055] The test module is specifically used for:
[0056] Inputting the category texts of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model;
[0057] The second training module is specifically used for:
[0058] The first diffusion model is fine-tuned using the category texts and training images of each category in the adjusted training set.
[0059] Optionally, the second training module includes:
[0060] An interval confirmation submodule, used to determine the target difference interval where the difference of each category is located in a plurality of continuous preset difference intervals; each of the preset difference intervals is set with a different weight;
[0061] The weight setting submodule is used to set the weight of the target similarity interval corresponding to each category;
[0062] The adjustment submodule is used to adjust the number of training images of each category in the training set according to the weight of each category.
[0063] The present invention also provides an image generating device, comprising:
[0064] A receiving module, used for receiving input image generation information;
[0065] The image generation module is used to input the image generation information into a trained diffusion model to obtain a generative image output by the diffusion model; the diffusion model is trained using the image generation model training method as described above.
[0066] The present invention also provides an electronic device, comprising:
[0067] Memory for storing computer programs;
[0068] A processor is used to implement the above-mentioned image generation model training method and / or the above-mentioned image generation method when executing the computer program.
[0069] The present invention also provides a computer-readable storage medium, which stores computer-executable instructions. When the computer-executable instructions are loaded and executed by a processor, the image generation model training method and / or the image generation method as described above are implemented.
[0070] The present invention provides an image generation model training method, comprising: using a training set to fine-tune a pre-trained diffusion model to obtain a first diffusion model; wherein the training set contains training data of multiple categories, the training data contains category information and training images, and the diffusion model is used to generate images according to the input information; inputting the category information of each category into the first diffusion model to obtain generative images of each category output by the first diffusion model; determining the difference between each generative image and a training image of the same category to obtain the difference corresponding to each category; adjusting the number of training images of each category in the training set according to the difference of each category, and using the adjusted training set to fine-tune the first diffusion model to obtain a second diffusion model that has completed training.
[0071] It can be seen that the present invention can firstly use the training set to fine-tune the pre-trained diffusion model to obtain the first diffusion model; wherein the training set contains training data of multiple categories, the training data contains category information and training images, and the diffusion model is used to generate images according to the input information. Subsequently, the present invention can input the category information of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model, and can determine the difference between each generative image and the training image of the same category to obtain the difference corresponding to each category; wherein the difference of each category reflects the image generation ability of the first diffusion model corresponding to each category, the lower the difference, the better the image generation ability, and the higher the difference, the worse the image generation ability. Furthermore, the present invention can adjust the number of training images of each category in the training set according to the difference of each category, and use the adjusted training set to fine-tune the first diffusion model to ensure that the categories with poor image generation quality are trained more, thereby improving the training effect of rare categories with scarce training data, and ensuring that the second diffusion model that has completed the training can generate images for rare categories with high quality. The present invention also provides an image generation method, an image generation model training device, an image generation device, an electronic device and a computer-readable storage medium, which have the above-mentioned beneficial effects. BRIEF DESCRIPTION OF THE DRAWINGS
[0072] 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.
[0073] Figure 1 A flowchart of an image generation model training method provided by an embodiment of the present invention;
[0074] Figure 2 A flowchart of an image generation method provided by an embodiment of the present invention;
[0075] Figure 3 A flowchart of another image generation model training method provided by an embodiment of the present invention;
[0076] Figure 4 A structural block diagram of an image generation model training device provided by an embodiment of the present invention;
[0077] Figure 5 A structural block diagram of an image generating device provided by an embodiment of the present invention;
[0078] Figure 6 The present invention provides a structural block diagram of an electronic device. DETAILED DESCRIPTION
[0079] 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.
[0080] The diffusion model is a machine learning model that can generate corresponding images based on input information. In the related art, the diffusion model is generally first trained with a large amount of data to obtain a pre-trained version, and then the pre-trained version is fine-tuned according to the training set in the actual business to obtain the final version of the completed training. However, for rare categories with very scarce training data, neither pre-training nor fine-tuning training can ensure that the diffusion model can generate high-quality images for rare categories. In view of this, the present invention can provide an image generation model training method, which can improve the training effect of rare categories with scarce training data in the diffusion model, so as to ensure the quality of images generated by the diffusion model for rare categories.
[0081] 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, etc.
[0082] For easier understanding, please refer to Figure 1 , Figure 1 A flowchart of an image generation model training method provided by an embodiment of the present invention, the method may include:
[0083] S101. Fine-tune a pre-trained diffusion model using a training set to obtain a first diffusion model; wherein the training set includes training data of multiple categories, the training data includes category information and training images, and the diffusion model is used to generate an image according to input information.
[0084] In an embodiment of the present invention, a diffusion model is used to generate a corresponding image based on input information. For example, when the input information indicates that an image containing an animal needs to be generated, the image output by the diffusion model may contain an animal; when the input information indicates that an image of a specific style needs to be generated, the image output by the diffusion model may have such a style. Among them, the input information can be either text information or image information, or it can include both text information and image information, that is, the diffusion model can analyze the user's intention based on the text and image, and then generate the corresponding image. Furthermore, a pre-trained diffusion model refers to a diffusion model that has been trained using a large amount of training data, which has basic image generation capabilities and can be tuned using other training sets. In other words, this step uses the training set to fine-tune the pre-trained diffusion model, with the aim of optimizing the parameters of the pre-trained diffusion model so that it adapts to the business scenario corresponding to the training set used in the present invention.
[0085] Furthermore, the training set used in the embodiment of the present invention includes training data of multiple categories, and each category of training data includes category information and training images. The category information can be input into the diffusion model as input information. It can be understood that since the input of the diffusion model can be text information, image information, or both, the above-mentioned category information can include text information, image information, or both. It should be noted that the text information in the category information of the training set is usually only category description words and category numbers, and does not include sentences; and the category description words are usually in English, not Chinese.
[0086] Furthermore, since the diffusion model has a stronger ability to understand complete sentences than single or a small number of words, and it has a higher recognition rate for Chinese, in order to improve the training effect, before performing fine-tuning training in the embodiments of the present invention, the category description words and category numbers in the training set category information can also be expanded into category texts. Specifically, the present invention can first translate the category description words into Chinese to obtain Chinese category description words. Subsequently, the present invention can determine whether the Chinese category description words contain rare words that the diffusion model cannot encode. If so, the rare words can be replaced with non-rare words that the diffusion model can encode, and this step can be completed based on a preset dictionary that stores the characters that the diffusion model can encode. Subsequently, the Chinese category description words and category numbers of the same category can be jointly text-expanded to obtain the category texts of each category.
[0087] Based on this, the category information includes category description words and category numbers; before using the training set to perform fine-tuning training on the pre-trained diffusion model, it may further include:
[0088] Step 11: Translate the category description words in each piece of category information into Chinese to obtain Chinese category description words;
[0089] Step 12: Determine whether there are rare characters in the Chinese category description words that are not stored in the preset dictionary, and when it is determined that there are such rare characters, replace the rare characters with the characters in the preset dictionary; wherein, the preset dictionary stores the characters that the diffusion model can encode;
[0090] Step 13: Jointly text-expand the Chinese category description words and category numbers of the same category to obtain the category texts of each category.
[0091] For example, for the category information "6:'stingray'", it can be expanded to "This is the sixth category of pictures about stingrays"; and for the rare character "鳐" that the diffusion model cannot encode, it can be replaced with the non-rare character "瑶" that the diffusion model can encode.
[0092] Furthermore, the present invention can use the category texts of each category and the training images to jointly perform fine-tuning training on the pre-trained diffusion model.
[0093] Based on this, the use of the training set to perform fine-tuning training on the pre-trained diffusion model may include:
[0094] Step 21: Use the category texts of each category and the training images to jointly perform fine-tuning training on the pre-trained diffusion model.
[0095] It should be noted that the embodiments of the present invention do not limit the number of various training images, which can be set according to actual application requirements.
[0096] S102: Inputting category information of each category into the first diffusion model to obtain generative images of each category output by the first diffusion model.
[0097] In an embodiment of the present invention, the training set may contain rare categories. For example, the ImageNet data set is a commonly used computer vision data set, which contains uncommon categories in daily life, such as stingray ray category, spotted blunt-mouthed giant salamander category, striped sheathed tiger category, etc. Since these categories of images are relatively rare on the Internet, in the relevant technology, it is difficult for the diffusion model to effectively learn these rare categories in both the pre-training stage and the fine-tuning stage, and thus it is also difficult to achieve a good image generation effect. Therefore, in the present invention, the fine-tuning training of the pre-trained diffusion model is divided into two stages. In the first stage, the pre-trained diffusion model can be directly fine-tuned using the training set to obtain the first diffusion model; in the second stage, the image generation capability corresponding to the first diffusion model when generating images of each category is first tested, and the number of training images of each category in the training set is adjusted based on the image generation capability to improve the training effect on rare categories, and then the first diffusion model is continued to be fine-tuned using the adjusted training set to obtain the second diffusion model that has completed the training. In this step and step S103, the image generation capability corresponding to each category of images generated by the first diffusion model is tested. Specifically, in this step, the category information of each category is first input into the first diffusion model to obtain the generative images of each category output by the first diffusion model. As described above, since the present invention can expand the category description words and category numbers in the category information into category texts, the category texts of each category can also be input into the first diffusion model to obtain the generative images of each category output by the first diffusion model.
[0098] Based on this, inputting the category information of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model may include:
[0099] Step 31: Input the category texts of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model.
[0100] It should be noted that the embodiment of the present invention does not limit how many generative images need to be generated for each category, and can be set according to actual application requirements, for example, it can be 50, or more or less.
[0101] S103, determining the difference between each of the generated images and the training images of the same category, and obtaining the difference corresponding to each category.
[0102] After obtaining the generative images, this step will determine the difference between each generative image and the training images of the same category, and obtain the difference corresponding to each category. The difference represents the strength of the image generation capability of the first diffusion model corresponding to a certain category of images. The higher the difference, the weaker the ability of the first diffusion model to generate images for the category; the lower the difference, the stronger the ability of the first diffusion model to generate images for the category. Therefore, the embodiment of the present invention will adjust the training set based on the difference to enhance the ability of the diffusion model to generate images for rare categories.
[0103] As described above, there may be multiple generative images corresponding to each category. Therefore, the embodiment of the present invention can determine the difference between multiple generative images and multiple training images of the same category, and obtain the difference corresponding to each category, thereby more comprehensively evaluating the model performance.
[0104] Based on this, determining the difference between each of the generated images and the training images of the same category to obtain the difference corresponding to each category may include:
[0105] Step 41: Determine the difference between the multiple generated images and the multiple training images of the same category, and obtain the difference corresponding to each category.
[0106] It should be noted that the embodiments of the present invention do not limit how to calculate the difference. For example, an image similarity algorithm can be used to calculate the similarity between images, and the difference can be determined based on the similarity. The difference can also be determined using an indicator that measures the image generation model. For example, the difference can be calculated using the FID indicator (Frechet Inception Distance) or the IS indicator (Inception Score). Since the FID indicator has better reliability, the difference in the embodiments of the present invention will be calculated based on the FID indicator. The larger the FID corresponding to a category, the worse the ability of the diffusion model to generate images for the category; the smaller the FID corresponding to a category, the stronger the ability of the diffusion model to generate images for the category.
[0107] The following is a specific introduction to a method for calculating the difference. Based on this, the difference between multiple generated images and multiple training images of the same category is determined, including:
[0108] Step 1101: convert each generative image into a corresponding first eigenvector, add all first eigenvectors to a generative eigenvector set, and determine a mean and a covariance matrix corresponding to the generative eigenvector set;
[0109] Step 1102: converting each training image of the same category into a corresponding second eigenvector, adding all the second eigenvectors to the real eigenvector set, and determining the mean and covariance matrix corresponding to the real eigenvector set;
[0110] Step 1103: Determine the difference between the multiple generated images and the multiple training images of the same category according to the following formula:
[0111] ;
[0112] Among them, P represents the set of real feature vectors, G represents the set of generated feature vectors, and are the means corresponding to the real eigenvector set and the generated eigenvector set, respectively. and are the covariance matrices corresponding to the real eigenvector set and the generated eigenvector set, respectively. Represents the square root of the trace of the covariance matrix.
[0113] It can be seen that in the above calculation method, the mean and covariance matrix corresponding to the eigenvectors of all generative images can be determined, and the mean and covariance matrix corresponding to the eigenvectors of all training images of the same category can be determined. Subsequently, the present invention can use the mean and covariance matrix corresponding to all generative images and the mean and covariance matrix corresponding to all training images to determine the difference between multiple generative images and multiple training images of the same category. This is because: if the eigenvector of the generative image and the eigenvector of the training image both satisfy the Gaussian distribution, then if the generative image is similar to the training image, the distribution corresponding to the eigenvector of the generative image and the distribution corresponding to the eigenvector of the training image should be similar. The Gaussian distribution can be determined by the mean and covariance, so the above calculation method can determine whether the generative image is similar to the training image based on the distribution of the eigenvector of the generative image and the distribution of the eigenvector of the training image.
[0114] S104 , adjusting the number of training images of each category in the training set according to the difference between the categories, and using the adjusted training set to perform fine-tuning training on the first diffusion model to obtain a trained second diffusion model.
[0115] After obtaining the difference degree corresponding to each category, this step will adjust the number of training images of each category in the training set according to the difference degree to improve the training effect of the diffusion model for rare types. It is understandable that the adjustment of the number of training images can be carried out by increasing or reducing the training images, and of course, it can also be carried out by combining the means of increasing and reducing the training images. For increasing the training images, the embodiment of the present invention can be implemented by copying the training images; for reducing the training images, the embodiment of the present invention can be implemented by removing the training images. Further, the embodiment of the present invention does not limit how to adjust the number of training images of each category in the training set according to the difference degree of each category. For example, the ratio of the number of training images of each category can be formed according to the difference degree of each category, and the number of training images of each category can be adjusted based on the ratio of the number of training images. It is also possible to set multiple continuous difference degree intervals, and set corresponding weights for each difference degree interval, and then determine the corresponding weights for each type according to the difference degree interval where the difference degree of each type is located, and then adjust the number of training images of each category in the training set according to the weights of each category, such as adjusting the number of training images according to the ratio of the number of training images formed by the weights, or adjusting the number of training images by using the weights as the number of copies of the images. In the embodiment of the present invention, in order to improve the adjustment flexibility, multiple difference intervals can be preset to set corresponding weights, and a corresponding weight can be set for each category according to the difference interval to which the difference of each category belongs, and then the number of training images for each category can be adjusted according to the weight of each category. It can be understood that the larger the value of the difference interval, the greater the weight.
[0116] Based on this, adjusting the number of training images of each category in the training set according to the difference between each category may include:
[0117] Step 51: determining a target difference interval in which the difference of each category lies in a plurality of continuous preset difference intervals; each of the preset difference intervals is set with a different weight;
[0118] Step 52: setting the weight of the target similarity interval corresponding to each category for each category;
[0119] Step 53: Adjust the number of training images for each category in the training set according to the weight of each category.
[0120] It should be noted that the embodiment of the present invention does not limit the specific preset difference intervals, which can be set according to actual application requirements, for example, there can be five intervals of less than 20, 20-30, 30-40, 40-50, and greater than 50. The embodiment of the present invention also does not limit the weights corresponding to the preset difference intervals, for example, the weight of less than 20 is 1, the weight of 20-30 is 2, the weight of 30-40 is 4, the weight of 40-50 is 8, and the weight of greater than 50 is 10.
[0121] Furthermore, considering that removing training images may have an impact on subsequent evaluations, the embodiment of the present invention adjusts the number of training images only by increasing the number of training images (i.e., copying training images), and then the weights can be used as the number of image copies to copy the training images of each category. In this way, when the diffusion model is trained once using all the data in the adjusted training set, the images in the category of copied images are equivalent to having been trained multiple times. For example, if a certain category of training images is copied 8 times, then the training images of this category are equivalent to being trained 8 times in one epoch (iteration).
[0122] Based on this, adjusting the number of training images of each category in the training set according to the weight of each category may include:
[0123] Step 61: Copy the training images of each category by the same number according to the weight value of each category to obtain an adjusted training set.
[0124] Furthermore, as described above, since the present invention can expand the category description words and category numbers in the category information into category text, the category texts and training images of each category in the adjusted training set are also used to fine-tune the first diffusion model to obtain the second diffusion model.
[0125] Based on this, the fine-tuning training of the first diffusion model using the adjusted training set includes:
[0126] Step 71: fine-tune the first diffusion model using the category texts and training images of each category in the adjusted training set.
[0127] Furthermore, considering that after expanding part of the category information into category text, the degree of understanding of the category by the diffusion model cannot be improved, which results in a large difference in the category. For example, when the category texts between categories are relatively similar, the diffusion model will not be able to distinguish the two categories well. In order to further deepen the model's ability to understand these categories and to effectively distinguish each category, when determining that the difference of a certain category is greater than a preset threshold, the present invention can further add a preset description text corresponding to the category to the category text of the category to further expand the category text of the category. In this way, the diffusion model's ability to understand the category can be further enhanced; at the same time, the preset description texts of each category are different, which can further improve the difference between the category texts and facilitate the diffusion model to distinguish each category. It should be noted that the embodiment of the present invention does not limit the specific preset description text, and the preset description text can be the definition of the category description word corresponding to the category. For example, when the category description word is an animal, the preset description text can be a more detailed description of the animal.
[0128] Based on this, after obtaining the difference degree corresponding to each category, it can also include:
[0129] Step 81: Determine whether the difference between the categories is greater than a preset threshold; if so, proceed to step 82; if not, ignore.
[0130] Step 82: adding a preset description text corresponding to the category to the category text of the category, so as to fine-tune the first diffusion model based on the added category text.
[0131] It should be noted that the embodiments of the present invention do not limit the specific preset threshold value, which can be set according to actual application requirements.
[0132] Furthermore, in order to select the second diffusion model with the best performance, the first diffusion model can be fine-tuned for a first preset iterative round to obtain a second initial diffusion model, and then the second initial diffusion model can be fine-tuned for a second preset iterative round. In the process of fine-tuning the second initial diffusion model, the model version of each iteration is saved as the second diffusion model of the iterative version, and then the FID is used to evaluate each version of the second diffusion model to select the one with the best effect.
[0133] Based on this, fine-tuning the first diffusion model using the adjusted training set to obtain a trained second diffusion model may include:
[0134] Step 91: performing a first preset iteration round of first fine-tuning training on the first diffusion model using the adjusted training set to obtain a second initial diffusion model;
[0135] Step 92: performing a second preset iteration round of second fine-tuning training on the second initial diffusion model using the adjusted training set, and deriving the second diffusion model of the current iteration version at the end of each round of second fine-tuning training to obtain multiple iteration versions of the second diffusion model;
[0136] Step 93: Determine the degree of difference corresponding to each category in each iterative version of the second diffusion model, so as to select the optimal diffusion model in each iterative version of the second diffusion model according to the degree of difference corresponding to each category in each iterative version of the second diffusion model.
[0137] It should be noted that the embodiments of the present invention do not limit the specific values of the first preset iteration round and the second preset iteration round, and can be set according to actual application requirements. For example, the first preset iteration round can be 10 rounds, and the second preset iteration round can be 5 to 10 rounds.
[0138] Based on the above embodiments, the present invention can firstly use the training set to fine-tune the pre-trained diffusion model to obtain the first diffusion model; wherein the training set includes training data of multiple categories, the training data includes category information and training images, and the diffusion model is used to generate images according to the input information. Subsequently, the present invention can input the category information of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model, and can determine the difference between each generative image and the training image of the same category to obtain the difference corresponding to each category; wherein the difference of each category reflects the image generation ability of the first diffusion model corresponding to each category, the lower the difference, the better the image generation ability, and the higher the difference, the worse the image generation ability. Furthermore, the present invention can adjust the number of training images of each category in the training set according to the difference of each category, and use the adjusted training set to fine-tune the first diffusion model to ensure that the categories with poor image generation quality are trained more, thereby improving the training effect of rare categories with scarce training data, and ensuring that the second diffusion model that has completed the training can generate images for rare categories with high quality.
[0139] Based on the above embodiment, the application of the diffusion model trained by the embodiment of the present invention in actual business scenarios is introduced below. Figure 2 , Figure 2 A flowchart of an image generation method provided by an embodiment of the present invention, the method may include:
[0140] S201, receiving input image generation information.
[0141] It should be pointed out that the image generation information here can be text information, image information, or a combination of text information and image information, which can be selected according to actual application requirements.
[0142] S202. Input the image generation information into the trained diffusion model to obtain the generated image output by the diffusion model; the diffusion model is trained by using the image generation model training method as described above.
[0143] In the embodiment of the present invention, since the diffusion model has been trained by using the image generation model training method provided by the present invention, high-quality images can be generated for each type, so as to better meet various image generation requirements of users.
[0144] Optionally, the above image generation information may include text description information for the generated image; of course, the above image generation information may further include an image, which can be set according to actual application requirements.
[0145] Based on the above embodiments, the above image generation model training method will be introduced in detail below based on specific schematic diagrams. Please refer to Figure 3 , Figure 3 is a flowchart of another image generation model training method provided by the embodiment of the present invention. In the embodiment of the present invention, taking the optimization of the diffusion model on ImageNet as an example, the following specific embodiments are described. The model is fine-tuned on the training set of ImageNet. The reference data set used for calculating FID is the training set of ImageNet. 50,000 pictures generated by the model will be compared with the training set, and the text guidance is the label processed for each category of ImageNet.
[0146] 1. For the expansion example of text guidance:
[0147] Original English text: stingray.
[0148] Translate it into as detailed Chinese words as possible: manta ray, stingray.
[0149] If it contains rare Chinese characters, for example, if the character "鳐" cannot be recognized by the text encoder, it can be replaced with a character in the vocabulary, and converted into: manta ray, stingray.
[0150] Expand the word into a sentence by adding a category number: This is a picture of the sixth category of manta ray, stingray.
[0151] 2. Example of the first fine-tuning with a mixed training set:
[0152] The training set of ImageNet has a total of 1,281,167 pictures, which are divided into 10,000 categories. In the first-step training, these pictures will be trained for 10 iterations.
[0153] 3. Example of generating pictures with model M1 and calculating the FID value:
[0154] Taking the example mentioned in step 1 as an example, the model will be guided by "this is the sixth category of pictures about stingrays and stingrays" to generate 50 pictures. These 50 pictures will be compared with about 1,000 pictures of the same category in the training set, and the FID value between the two will be calculated to get 45.97.
[0155] 4. Example of adjusting the weight of training data based on FID:
[0156] The present invention is provided with five intervals of less than 20, 20-30, 30-40, 40-50, and greater than 50. The smaller the value, the lower the group weight. In actual operation, the weights of different intervals are allocated as follows: the weight of less than 20 is 1, the weight of 20-30 is 2, the weight of 30-40 is 4, the weight of 40-50 is 8, and the weight of greater than 50 is 10.
[0157] Based on the FID value obtained in the previous step, the weight of the "stingray" class will be multiplied by 8, which means that this class of images will be repeated 8 times in each epoch of training.
[0158] 5. Perform the second step of fine-tuning the model:
[0159] In this step, the model is further fine-tuned using the weighted data. Generally, 10 iterations can be trained, and then the checkpoint results saved in each iteration are calculated (also evaluated using FID), and the one with the best effect is selected.
[0160] The image generation model training device, image generation device, electronic device and computer-readable storage medium provided in the embodiments of the present invention are introduced below. The image generation model training device, image generation device, electronic device and computer-readable storage medium described below can be referenced to each other with the image generation model training method and image generation method described above.
[0161] Please refer to Figure 4 , Figure 4 A structural block diagram of an image generation model training device provided by an embodiment of the present invention, the device may include:
[0162] A first training module 401 is used to fine-tune the pre-trained diffusion model using a training set to obtain a first diffusion model; wherein the training set includes training data of multiple categories, the training data includes category information and training images, and the diffusion model is used to generate an image according to input information;
[0163] A testing module 402, configured to input the category information of each category into the first diffusion model to obtain a generative image of each category output by the first diffusion model;
[0164] The difference calculation module 403 is used to determine the difference between each of the generated images and the training images of the same category, and obtain the difference corresponding to each category;
[0165] The second training module 404 is used to adjust the number of training images of each category in the training set according to the difference between each category, and use the adjusted training set to fine-tune the first diffusion model to obtain a trained second diffusion model.
[0166] Optionally, the category information includes a category description word and a category number; the device may also include:
[0167] A translation module, used for translating the category description words in each category information into Chinese to obtain the Chinese category description words;
[0168] An uncommon character adjustment module, used to determine whether there are uncommon characters in the Chinese category description words that are not stored in the preset dictionary, and when it is determined that the uncommon characters exist, replace the uncommon characters with characters in the preset dictionary; wherein the preset dictionary stores characters that can be encoded by the diffusion model;
[0169] A text expansion module is used to expand the text of the Chinese category description words and category numbers of the same category to obtain category texts of each category;
[0170] The first training module 401 may be used to: perform fine-tuning training on the pre-trained diffusion model using the category texts and training images of each category;
[0171] The testing module 402 may be used to: input the category texts of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model;
[0172] The second training module 404 may be used to: perform fine-tuning training on the first diffusion model by using the category texts and training images of each category in the adjusted training set.
[0173] Optionally, the device may further include:
[0174] The description text adding module is used to determine whether the difference of the category is greater than a preset threshold; if so, add the preset description text corresponding to the category to the category text of the category to fine-tune the first diffusion model based on the added category text.
[0175] Optionally, the second training module 404 may include:
[0176] An interval confirmation submodule, used to determine the target difference interval where the difference of each category is located in a plurality of continuous preset difference intervals; each of the preset difference intervals is set with a different weight;
[0177] The weight setting submodule is used to set the weight of the target similarity interval corresponding to each category;
[0178] The adjustment submodule is used to adjust the number of training images of each category in the training set according to the weight of each category.
[0179] Optionally, the adjustment submodule may be used to: copy the same number of training images of each category according to the weight value of each category to obtain an adjusted training set.
[0180] Optionally, the difference calculation module 403 may be used to:
[0181] Determine the differences between multiple generated images and multiple training images of the same category, and obtain the differences corresponding to each category.
[0182] Optionally, the difference calculation module 403 may include:
[0183] A first calculation submodule, used for converting each generative image into a corresponding first eigenvector, adding all the first eigenvectors to a generative eigenvector set, and determining a mean and a covariance matrix corresponding to the generative eigenvector set;
[0184] A second calculation submodule is used to convert each training image of the same category into a corresponding second eigenvector, add all the second eigenvectors to the real eigenvector set, and determine the mean and covariance matrix corresponding to the real eigenvector set;
[0185] The third calculation submodule is used to determine the difference between the multiple generated images and the multiple training images of the same category according to the following formula:
[0186] ;
[0187] Among them, P represents the set of real feature vectors, G represents the set of generated feature vectors, and are the means corresponding to the real eigenvector set and the generated eigenvector set, respectively. and are the covariance matrices corresponding to the real eigenvector set and the generated eigenvector set, respectively. Represents the square root of the trace of the covariance matrix.
[0188] Optionally, the second training module 404 may include:
[0189] A first training submodule, configured to perform a first preset iteration round of first fine-tuning training on the first diffusion model using the adjusted training set to obtain a second initial diffusion model;
[0190] A second training submodule is configured to perform a second fine-tuning training of a second preset iteration round on the second initial diffusion model using the adjusted training set, and derive the second diffusion model of the current iteration version at the end of each round of the second fine-tuning training to obtain multiple iteration versions of the second diffusion model;
[0191] The evaluation module is used to determine the difference degree corresponding to each category in each iterative version of the second diffusion model, so as to select the optimal diffusion model in each iterative version of the second diffusion model according to the difference degree corresponding to each category in each iterative version of the second diffusion model.
[0192] Please refer to Figure 5 , Figure 5 A structural block diagram of an image generating device provided by an embodiment of the present invention, the device may include:
[0193] A receiving module 501 is used to receive input image generation information;
[0194] The image generation module 502 is used to input the image generation information into a trained diffusion model to obtain a generative image output by the diffusion model; the diffusion model is trained using the image generation model training method described above.
[0195] 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 image generation model training method and the image generation method provided in the aforementioned embodiment when executing the computer program.
[0196] For the specific process of the above-mentioned image generation model training method and image generation method, please refer to the corresponding content provided in the aforementioned embodiments, which will not be repeated here.
[0197] 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.
[0198] 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.
[0199] An embodiment of the present invention also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the image generation model training method and the image generation method of any of the above embodiments are implemented.
[0200] Since the embodiments of the computer-readable storage medium part correspond to the embodiments of the image generation model training method and the image generation method part, the embodiments of the storage medium part please refer to the description of the embodiments of the image generation model training method and the image generation method part, and will not be repeated here.
[0201] 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.
[0202] 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.
[0203] 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.
[0204] The above is a detailed introduction to the image generation model training method, image generation method, device, equipment, and 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, several improvements and modifications can be made to the present invention, and these improvements and modifications also fall within the scope of protection of the claims of the present invention.
Claims
1. A method for training an image generation model, characterized in that: include: Fine-tune the pre-trained diffusion model using the training set to obtain a first diffusion model; wherein the training set includes training data of multiple categories, the training data includes category information and training images, and the diffusion model is used to generate an image according to the input information; Inputting the category information of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model; Determine the difference between each of the generated images and the training images of the same category to obtain the difference corresponding to each category; The number of training images of each category in the training set is adjusted according to the difference between each category, and the first diffusion model is fine-tuned using the adjusted training set to obtain a trained second diffusion model.
2. The image generation model training method according to claim 1, characterized in that: The category information includes a category description word and a category number; Before fine-tuning the pre-trained diffusion model using the training set, it also includes: Translating the category description words in each of the category information into Chinese to obtain the Chinese category description words; Determine whether there are rare characters in the Chinese category description words that are not stored in the preset dictionary, and when it is determined that the rare characters exist, replace the rare characters with characters in the preset dictionary; wherein the preset dictionary stores characters that can be encoded by the diffusion model; The Chinese category description words and category numbers of the same category are used to expand the text to obtain the category text of each category; The fine-tuning training of the pre-trained diffusion model using the training set includes: Fine-tune the pre-trained diffusion model using the category texts and training images of each category; The step of inputting the category information of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model includes: Inputting the category texts of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model; The fine-tuning training of the first diffusion model using the adjusted training set includes: The first diffusion model is fine-tuned using the category texts and training images of each category in the adjusted training set.
3. The image generation model training method according to claim 1, characterized in that: The step of adjusting the number of training images of each category in the training set according to the difference between the categories includes: Determine a target difference interval in which the difference of each category lies among a plurality of continuous preset difference intervals; each of the preset difference intervals is set with a different weight; Set the weight of the target similarity interval corresponding to each category for each category; The number of training images in each category in the training set is adjusted according to the weight of each category.
4. The image generation model training method according to claim 3, characterized in that: The step of adjusting the number of training images of each category in the training set according to the weight of each category comprises: According to the weight value of each category, the training images of each category are copied the same number of times to obtain the adjusted training set.
5. The image generation model training method according to claim 1, characterized in that: Determining the difference between each of the generated images and the training images of the same category to obtain the difference corresponding to each category includes: Determine the differences between multiple generated images and multiple training images of the same category, and obtain the differences corresponding to each category.
6. The image generation model training method according to claim 5, characterized in that: Determining the difference between the multiple generated images and the multiple training images of the same category includes: Convert each of the generative images into a corresponding first eigenvector, add all the first eigenvectors to a set of generative eigenvectors, and determine a mean and a covariance matrix corresponding to the set of generative eigenvectors; Convert each training image of the same category into a corresponding second eigenvector, add all the second eigenvectors to a set of true eigenvectors, and determine a mean and a covariance matrix corresponding to the set of true eigenvectors; The difference between multiple generated images and multiple training images of the same category is determined according to the following formula: ; Wherein, P represents the real feature vector set, G represents the generated feature vector set, and are the means corresponding to the real feature vector set and the generated feature vector set respectively, and are the covariance matrices corresponding to the real eigenvector set and the generated eigenvector set respectively, Represents the square root of the trace of the covariance matrix.
7. The image generation model training method according to any one of claims 1 to 6, characterized in that: The fine-tuning training of the first diffusion model using the adjusted training set to obtain a trained second diffusion model includes: Using the adjusted training set, perform a first preset iteration round of first fine-tuning training on the first diffusion model to obtain a second initial diffusion model; Performing a second preset iteration round of second fine-tuning training on the second initial diffusion model using the adjusted training set, and deriving the second diffusion model of the current iteration version at the end of each round of the second fine-tuning training to obtain multiple iteration versions of the second diffusion model; The difference degree corresponding to each category in each iterative version of the second diffusion model is determined, so as to select the optimal diffusion model in each iterative version of the second diffusion model according to the difference degree corresponding to each category in each iterative version of the second diffusion model.
8. An image generation method, characterized in that: include: Receiving input image generation information; Inputting the image generation information into a trained diffusion model to obtain a generative image output by the diffusion model; The diffusion model is trained using the image generation model training method according to any one of claims 1 to 7.
9. The image generation method according to claim 8, characterized in that: The image generation information includes text description information for the generative image.
10. An image generation model training device, characterized in that: include: A first training module is used to fine-tune the pre-trained diffusion model using a training set to obtain a first diffusion model; wherein the training set includes training data of multiple categories, the training data includes category information and training images, and the diffusion model is used to generate an image according to input information; A testing module, used for inputting the category information of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model; A difference calculation module is used to determine the difference between each of the generated images and the training images of the same category to obtain the difference corresponding to each category; The second training module is used to adjust the number of training images of each category in the training set according to the difference between each category, and use the adjusted training set to fine-tune the first diffusion model to obtain a trained second diffusion model.
11. The image generation model training device according to claim 10, characterized in that: The category information includes a category description word and a category number; The image generation model training device further includes: A translation module, used for translating the category description words in each category information into Chinese to obtain the Chinese category description words; An uncommon character adjustment module, used to determine whether there are uncommon characters in the Chinese category description words that are not stored in the preset dictionary, and when it is determined that the uncommon characters exist, replace the uncommon characters with characters in the preset dictionary; wherein the preset dictionary stores characters that can be encoded by the diffusion model; A text expansion module is used to expand the text of the Chinese category description words and category numbers of the same category to obtain category texts of each category; The first training module is specifically used for: Fine-tune the pre-trained diffusion model using the category texts and training images of each category; The test module is specifically used for: Inputting the category texts of each category into the first diffusion model to obtain the generative images of each category output by the first diffusion model; The second training module is specifically used for: The first diffusion model is fine-tuned using the category texts and training images of each category in the adjusted training set.
12. The image generation model training device according to claim 10, characterized in that: The second training module comprises: An interval confirmation submodule, used to determine the target difference interval where the difference of each category is located in a plurality of continuous preset difference intervals; each of the preset difference intervals is set with a different weight; The weight setting submodule is used to set the weight of the target similarity interval corresponding to each category; The adjustment submodule is used to adjust the number of training images of each category in the training set according to the weight of each category.
13. An image generating device, characterized in that: include: A receiving module, used for receiving input image generation information; An image generation module is used to input the image generation information into a trained diffusion model to obtain a generative image output by the diffusion model; the diffusion model is trained using the image generation model training method according to any one of claims 1 to 7.
14. An electronic device, characterized in that: include: Memory for storing computer programs; A processor, used to implement the image generation model training method as described in any one of claims 1 to 7 and / or the image generation method as described in any one of claims 8 to 9 when executing the computer program.
15. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, which, when loaded and executed by a processor, implement the image generation model training method as described in any one of claims 1 to 7 and / or the image generation method as described in any one of claims 8 to 9.
Citation Information
Patent Citations
Method, device and equipment for automatically generating product style based on diffusion model
CN117237479A
Image generation method and device, equipment and medium
CN117456033A
Portrait generation model training method and device, computer equipment and storage medium
CN117557688A
Multi-class cyclic generative adversarial method based on training set expansion technology
CN117671408A
Small sample surface defect image generation method and system based on feedback reinforcement learning
CN117710349A
