Training method of image classification model, image classification method and related device
By splicing hint vectors and mask vectors, and using mask prediction methods to train image classification models, it solves the problem in the existing technology that learning strategies need to be adjusted for different image classification tasks, achieves efficient and accurate image classification, and reduces costs.
Patent Information
- Application Number
- CN202310671260.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2023-03-10
- Filing Date
- 2023-06-07
- Publication Date
- 2024-09-10
- Estimated Expiration
- 2043-06-07
AI Technical Summary
In the existing technology, the learning strategy needs to be adjusted according to different tasks in image classification tasks, which consumes a lot of time and affects efficiency and cost.
By splicing the cue vector and the mask vector of the pre-trained model, the mask prediction method is used to train the image classification model, narrowing the gap between upstream and downstream tasks, achieving the stability of the same learning strategy, and being suitable for a variety of downstream tasks.
It improves the efficiency and accuracy of image classification tasks, reduces costs, and ensures the stability and consistency of learning strategies in different image classification tasks.
Smart Images

Figure CN118628784A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer vision, and in particular to a training method for an image classification model, an image classification method, and related devices. Background Art
[0002] In recent years, pre-trained models have been widely used in image classification tasks in the field of computer vision. At present, it is possible to connect an optimizable hint vector to the input of the first layer or each layer of the pre-trained model, add a corresponding classification head to the model based on the image classification task to be implemented, and then implement the relevant image classification task by fine-tuning the hint vector and the classification head parameters. However, for different image classification tasks, the above method needs to use different learning strategies (e.g., learning rate, weight decay factor) to adjust the hint vector and the classification head parameters, which requires a lot of time to search for the optimal learning strategy, affecting the efficiency and cost of the image classification task. Summary of the invention
[0003] The present application provides an image classification model training method, an image classification method and related devices, which can improve the efficiency of image classification tasks and reduce the cost of image classification tasks.
[0004] In a first aspect, the present application provides a method for training an image classification model. The method can be applied to a training device. The training device obtains a training data set, which includes multiple training images. Then, the training device concatenates a prompt vector and a mask vector of a pre-trained model for each training image to obtain a target input vector corresponding to each training image, where the pre-trained model is obtained by training the mask vector. Afterwards, the training device optimizes the above prompt vector based on the pre-trained model and the target input vector corresponding to each training image to obtain an image classification model that can be used to perform image classification tasks, wherein the image classification model includes the following parameters: parameters of the pre-trained model (e.g., the above mask vector) and the optimized prompt vector.
[0005] In the technical solution provided by the present application, the training device trains the image classification model by means of mask prediction, that is, the mask vector and the prompt vector are spliced for the training image to obtain the target input vector, and the image classification model is trained by predicting the target input vector. Since the pre-trained model is also trained by means of mask prediction, the image classification model is trained using the method provided by the present application, which can narrow the gap between upstream tasks and downstream tasks, so that the learning strategy required to implement downstream tasks has better stability and can be applied to different downstream tasks. Accordingly, when using the pre-trained model to implement different downstream tasks, the same learning strategy can be used, so that the pre-trained model can be efficiently applied to a variety of downstream tasks.
[0006] In a possible implementation of the first aspect, the training device concatenates the prompt vector and the mask vector of the pre-trained model for each training image, including: the training device concatenates the prompt vector and the mask vector of the pre-trained model with each training image. Concatenation refers to concatenation along the row direction of the vector.
[0007] It should be understood that in practical applications, in order to facilitate the processing of training images, the training images are usually processed into row vectors. Moreover, the hint vector and the mask vector are generally selected as row vectors, that is, if the training image, the hint vector and the mask vector are all row vectors, then it is more appropriate to select concatenation in the splicing method.
[0008] In a possible implementation of the first aspect, the training device optimizes the prompt vector based on the pre-trained model and the target input vector corresponding to each training image, including: the training device uses the pre-trained model to predict the mask vector in the target input vector corresponding to each training image to obtain the predicted visual words corresponding to each training image, and then maps the predicted visual words corresponding to each training image and the category labels corresponding to each training image to the same vector space, and then optimizes the prompt vector based on the similarity between the mapped predicted visual words and the category labels.
[0009] In the above implementation, the training device predicts the mask vector in the target input vector corresponding to the training image, thereby realizing the training of the image classification model in a mask prediction manner. In addition, by mapping the predicted visual words corresponding to the training image and the category label corresponding to the image to the same vector space, the distance between the two can be shortened, thereby more accurately determining the gap between the predicted visual words and the category label, and then more accurately adjusting the prompt vector, so that the trained image classification model has higher accuracy.
[0010] In a possible implementation of the first aspect, the training device maps the predicted visual words corresponding to each training image and the category label corresponding to each training image to the same vector space, including: the training device sets a corresponding category vector for the category label corresponding to each training image, and then performs a dimension transformation on the predicted visual words based on the dimension of the category vector. The transformed predicted visual words have the same dimension as the category vector, that is, the two are in the same vector space.
[0011] In a second aspect, the present application provides an image classification method. The method can be applied to an inference device. The inference device obtains an image to be processed, and then classifies the image to be processed based on an image classification model to obtain a classification result of the image. Among them, the image classification model includes the following parameters: parameters of a pre-trained model and an optimized prompt vector, and the parameters of the pre-trained model include a mask vector. The pre-trained model is obtained by training based on the mask vector. The optimized prompt vector is optimized based on the pre-trained model and the target input vector corresponding to multiple training images, and the target input vector corresponding to each training image is obtained by splicing the above-mentioned mask vector and the unoptimized prompt vector for each training image.
[0012] In the technical solution provided by the present application, the image classification model is trained by mask prediction, that is, the image classification task implemented by the image classification model is implemented by mask prediction. In this way, the gap between upstream tasks and downstream tasks is narrowed, so that the pre-training knowledge in the upstream tasks can be more fully applied to the downstream tasks, and the accuracy of the classification results is improved.
[0013] In a possible implementation of the second aspect, the target input vector corresponding to each training image is obtained by concatenating the mask vector and the unoptimized hint vector of the pre-trained model with each training image, wherein concatenation refers to concatenation along the row direction of the vector.
[0014] It should be understood that in practical applications, in order to facilitate the processing of training images, the training images are usually processed into row vectors. Moreover, the hint vector and the mask vector are generally selected as row vectors, that is, if the training image, the hint vector and the mask vector are all row vectors, then it is more appropriate to select concatenation in the splicing method.
[0015] In a possible implementation of the second aspect, the above-mentioned inference device classifies the image to be processed based on the image classification model, including: the inference device concatenates the mask vector and the optimized prompt vector of the pre-trained model with the image to obtain the target input vector corresponding to the image, and then classifies the image based on the target input vector corresponding to the image.
[0016] In a possible implementation manner of the second aspect, the above-mentioned image classification model also includes the following parameters: at least one category vector, and a category label corresponding to each category vector.
[0017] In a possible implementation of the second aspect, the inference device classifies the image to be processed based on a target input vector corresponding to the image to be processed, including: the inference device determines a predicted visual word corresponding to the image based on the target input vector corresponding to the image to be processed, and then performs a dimension transformation on the predicted visual word corresponding to the image based on the dimension of the at least one category vector, and then classifies the image based on the similarity between the transformed predicted visual word and the at least one category vector.
[0018] In the above implementation, the transformed predicted visual word and at least one category vector are in the same vector space. Vectors in the same vector space are easier to calculate distances, so as to more accurately determine which category vector the transformed predicted visual word is closer to. The category label corresponding to the closer category vector is the classification result of the image. Therefore, the above implementation can improve the accuracy of the image classification result.
[0019] In a third aspect, the present application provides a training device. The device includes an acquisition module, a splicing module and an optimization module. The acquisition module is used to acquire a training data set, and the training data set includes multiple training images. The splicing module is used to splice the prompt vector and the mask vector of the pre-trained model for each training image in the training data set to obtain a target input vector corresponding to each training image, and the pre-trained model here is obtained by training the mask vector. The optimization module is used to optimize the above prompt vector based on the pre-trained model and the target input vector corresponding to each training image to obtain an image classification model that can be used to perform image classification tasks. Among them, the image classification model includes the following parameters: parameters of the pre-trained model (for example, the above-mentioned mask vector) and the optimized prompt vector.
[0020] In a possible implementation of the third aspect, the concatenation module is used to concatenate the prompt vector and the mask vector of the pre-trained model with each training image, wherein concatenation refers to concatenation along the direction of the rows of the vectors.
[0021] In a possible implementation of the third aspect, the above-mentioned optimization module is used to use a pre-trained model to predict the mask vector in the target input vector corresponding to each training image, obtain the predicted visual words corresponding to each training image, map the predicted visual words corresponding to each training image and the category labels corresponding to each training image to the same vector space, and optimize the prompt vector based on the similarity between the mapped predicted visual words and the category labels.
[0022] In a possible implementation of the third aspect, the optimization module is used to set a corresponding category vector for the category label corresponding to each training image, and perform a dimension transformation on the predicted visual word based on the dimension of the category vector, and the transformed predicted visual word has the same dimension as the category vector.
[0023] In a fourth aspect, the present application provides an inference device. The device includes an acquisition module and a classification module. The acquisition module is used to acquire an image to be processed. The classification module is used to classify the image to be processed based on an image classification model to obtain a classification result of the image. Among them, the image classification model includes the following parameters: parameters of a pre-trained model and an optimized prompt vector, and the parameters of the pre-trained model include a mask vector. The pre-trained model is obtained by training based on the mask vector. The optimized prompt vector is optimized based on the pre-trained model and the target input vector corresponding to multiple training images, and the target input vector corresponding to each training image is obtained by splicing the above-mentioned mask vector and the unoptimized prompt vector for each training image.
[0024] In a possible implementation of the fourth aspect, the target input vector corresponding to each training image is obtained by concatenating the mask vector and the unoptimized hint vector of the pre-trained model with each training image, wherein concatenation refers to concatenation along the row direction of the vector.
[0025] In a possible implementation manner of the fourth aspect, the above-mentioned image classification model also includes the following parameters: at least one category vector, and a category label corresponding to each category vector.
[0026] In a possible implementation of the fourth aspect, the above-mentioned classification module is used to concatenate the mask vector and the optimized prompt vector of the pre-trained model with the image to obtain the target input vector corresponding to the image, and classify the image based on the target input vector corresponding to the image.
[0027] In a possible implementation of the fourth aspect, the classification module is used to determine the predicted visual word corresponding to the image to be processed based on the target input vector corresponding to the image, perform dimensional transformation on the predicted visual word corresponding to the image based on the dimension of the at least one category vector, and classify the image based on the similarity between the transformed predicted visual word and the at least one category vector.
[0028] In a fifth aspect, the present application provides a computing device comprising a processor and a memory, wherein the processor executes computer program code in the memory to implement part or all of the methods described in the aforementioned first aspect and any one of the implementations of the first aspect, and / or part or all of the methods described in the aforementioned second aspect and any one of the implementations of the second aspect.
[0029] In a sixth aspect, the present application provides a computing device cluster. The computing device cluster includes at least one computing device, and each computing device includes a processor and a memory. The processor of at least one computing device is used to execute instructions stored in the memory of at least one computing device, so that the computing device cluster performs part or all of the methods described in the aforementioned first aspect and any implementation of the first aspect, and / or, part or all of the methods described in the aforementioned second aspect and any implementation of the second aspect.
[0030] In a seventh aspect, the present application provides a computer program product. The computer program product may be software or a program product that includes instructions and can be run on a computing device or stored in any available medium. When the computer program product is run on a computing device or a computing device cluster, the computing device or the computing device cluster executes part or all of the methods described in the aforementioned first aspect and any one of the implementations of the first aspect, and / or part or all of the methods described in the aforementioned second aspect and any one of the implementations of the second aspect.
[0031] In an eighth aspect, the present application provides a computer-readable storage medium. The computer storage medium includes computer program instructions, and when the computer program instructions are executed by a computing device or a computing device cluster, the computing device or the computing device cluster executes part or all of the methods described in the aforementioned first aspect and any implementation of the first aspect, and / or part or all of the methods described in the aforementioned second aspect and any implementation of the second aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 It is a schematic diagram of an application scenario provided by an embodiment of the present application;
[0033] Figure 2 It is a flowchart of a training method for an image classification model provided in an embodiment of the present application;
[0034] Figure 3 is a schematic diagram of another application scenario provided by an embodiment of the present application;
[0035] Figure 4 It is a flowchart of an image classification method provided in an embodiment of the present application;
[0036] Figure 5 is a schematic diagram of an optimal learning strategy provided by an embodiment of the present application;
[0037] Figure 6 It is a schematic diagram of different optimal learning strategies corresponding to different data sets provided by an embodiment of the present application;
[0038] Figure 7is a schematic diagram of test results of different image classification models provided in an embodiment of the present application;
[0039] Figure 8 is a visualization diagram of a classification result provided in an embodiment of the present application;
[0040] Fig. 9 This is a schematic diagram of test results when different mapping rules are used, provided in an embodiment of the present application;
[0041] Fig.10 is a structural schematic diagram of a training device provided in an embodiment of the present application;
[0042] Fig.11 is a structural diagram of an inference device provided in an embodiment of the present application;
[0043] Fig.12 is a schematic diagram of the structure of a computing device provided in an embodiment of the present application;
[0044] Fig.13 It is a structural diagram of a computing device cluster provided in an embodiment of the present application. DETAILED DESCRIPTION
[0045] In order to facilitate understanding of the technical solution provided by this application, some terms involved in this application are first explained.
[0046] Pre-trained model: refers to a model with visual representation capabilities that is trained on a large-scale dataset. This process is also called an upstream task.
[0047] Masked visual token modeling (MVTM): is an implementation method of the upstream task. That is, mask vectors are used to randomly mask some image blocks of the input image of the model (for example, the model of the Transformer architecture), and the visual words of the masked image blocks are predicted in a self-supervised learning manner, so that the above model can learn visual representation capabilities, so that the pre-trained model obtained by training has visual representation capabilities.
[0048] Visual words: refer to the visual features of an image and are used to express the content of the image.
[0049] Self-supervised learning: also known as self-supervised training, refers to the process of automatically generating labels based on unlabeled data in a given training data set, and then determining the parameters of the model based on the generated labels. For example, when performing mask prediction, the pre-trained model uses a visual vocabulary to encode each image block of the input image to obtain the visual word label corresponding to each image block. After that, the pre-trained model adjusts the parameters of the pre-trained model according to the gap between the visual word label corresponding to the masked image block and the predicted visual word of the masked image block. It can be inferred that the level of semantic level of the visual vocabulary determines the level of visual representation ability that the pre-trained model can have. For example, if the visual vocabulary is low-level underlying information without semantic information, the pre-trained model is more inclined to learn the representation ability of the underlying visual information. If the visual vocabulary is high-level high-level information rich in semantic information, the pre-trained model is more inclined to learn the representation ability of high-level semantic information. For this reason, the pre-trained model involved in the embodiment of the present application is obtained by training using a visual vocabulary with high-level semantic information, and has a higher visual representation ability.
[0050] The pre-trained model can be applied to downstream tasks related to the upstream tasks. Downstream tasks refer to tasks related to the field of computer vision that are implemented using pre-trained models. The downstream tasks in the embodiments of the present application include image classification tasks. For example, the pre-trained model is a model that can detect specific targets (including vehicles, pedestrians, animals, etc.) in an image, which is obtained by training on a large number of surveillance videos (including surveillance videos from transportation hubs, streets, commercial areas, etc.). Then, the model can be used to detect vehicles passing through a certain intersection based on the surveillance video of the intersection.
[0051] When using a pre-trained model to implement downstream tasks, the pre-trained model needs to be trained based on the training data set of the downstream task. Compared with the large-scale data sets used in upstream tasks, the training data sets used in downstream tasks are smaller in scale. Therefore, when implementing downstream tasks, the pre-trained model is usually trained in a supervised manner. Supervised training (also known as supervised learning) refers to the process of determining the parameters of a model based on the data in a given training data set and the labels corresponding to each data in the training data set. The labels corresponding to the data in the training data set are usually manually annotated to identify the correct answer to the data for a specific task.
[0052] In the training phase of the model, in order to improve the training efficiency and performance of the model after training, it is necessary to set some reasonable hyperparameters (also called learning strategies) for training. Hyperparameters refer to a type of parameters that cannot be obtained by learning training data during training or cannot be changed by training data. It is a concept relative to the parameters in the model. The hyperparameters of the model are usually set manually based on experience or experiments. For example, hyperparameters include learning rate, weight decay, momentum, learning rate adjustment strategy (scheduler), total epochs, and warmup epochs.
[0053] Prompt learning: It refers to narrowing the difference between upstream and downstream tasks by designing downstream tasks to conform to the form of upstream tasks, so as to better apply pre-training knowledge to downstream tasks, especially in scenarios with few or zero samples.
[0054] Hint vector: refers to additional optimizable parameters that are added to assist the pre-trained model in achieving downstream tasks.
[0055] In the field of computer vision, there are many ways to implement upstream tasks, such as mask prediction, supervised discriminant, or contrast prediction mentioned above. Similarly, there are many downstream tasks, such as image classification tasks, target detection tasks, image segmentation tasks, etc. This makes it difficult for upstream and downstream tasks in the field of computer vision to reach a formal agreement. On the other hand, considering that the pre-trained model has the characteristics of large scale and large number of parameters, the VPT (visual prompt tuning) method is currently mainly used to fine-tune the pre-trained model to achieve downstream tasks (specifically image classification tasks). Specifically: the pre-trained model is specially designed for the image classification task to be implemented, for example, a learnable prompt vector is connected to the input of the first layer or each layer of the pre-trained model, and a corresponding classification head (for example, multi-layer perceptron (MLP)) is added after the pre-trained model based on the image classification task. Afterwards, the above-designed model is trained using the data set corresponding to the image classification task to obtain an image classification model that can be directly used to implement the image classification task. In the above method, only the hint vector and the classification head parameters need to be fine-tuned based on the data set corresponding to the image classification task, and a large number of parameters included in the pre-trained model do not need to be adjusted to train the image classification model. However, after research and testing, it was found that for different image classification tasks, the above method often needs to use different learning strategies to adjust the hint vector and the classification head parameters. In other words, it takes a lot of time to search for the optimal learning strategy through the above method to obtain a suitable image classification model, which will affect the efficiency of the image classification task and increase the cost of the image classification task.
[0056] For example, in a security scenario, image classification tasks may include detecting pedestrians, vehicles, and animals in surveillance videos. For the above three image classification tasks, if the above existing methods are used, it is necessary to determine the optimal learning strategy for each task separately, wherein the optimal learning strategies for different tasks are different, and the optimal learning strategy corresponding to one task usually results in extremely poor results for another task. In other words, three different optimal learning strategies need to be determined through the above existing methods to achieve the above three image classification tasks. Therefore, how to efficiently use pre-trained models to achieve downstream tasks is an urgent problem that needs to be solved.
[0057] In view of the above problems, an embodiment of the present application provides a training method for an image classification model. The method is based on the idea of prompt learning, and uses a pre-trained model to design the image classification task into a task in the form of mask prediction, thereby training the image classification model. Since the method achieves the formal consistency of the downstream task (i.e., the image classification task) and the upstream task (i.e., the training task of the pre-trained model) when training the image classification model, the learning strategy obtained by the method has good stability and can be uniformly deployed on different image classification tasks. In addition, when the downstream task and the upstream task are consistent in form, the pre-training knowledge can be fully applied to the downstream task, so the image classification model trained by the method provided by the embodiment of the present application has a high accuracy in the classification of images.
[0058] The training method of the image classification model provided in the embodiment of the present application can be performed by a training device. The training device can be a software device, a hardware device, or a combination of a software device and a hardware device. When the training device is a software device, the training device can be a virtual machine (VM), a container, or other software with a model training function.
[0059] The deployment of the training device is flexible. For example, the training device can be independently deployed in a data center of a cloud environment. The data center of the cloud environment includes a large number of basic resources (including computing resources, storage resources, and network resources). The computing resources in the data center of the cloud environment can be a large number of computing devices (for example, servers). In some embodiments, the training device can be independently deployed on a server or a virtual machine in a data center of a cloud environment, or distributedly deployed on multiple servers or multiple virtual machines in a data center of a cloud environment, or distributedly deployed on servers and virtual machines in a data center of a cloud environment. Figure 1 As shown, the training device is deployed by the cloud service provider in the data center of the cloud environment. Accordingly, the cloud service provider provides the functions of the training device as cloud services to tenants. Tenants can register an account on the cloud management platform, log in to the cloud management platform through the account, and purchase the cloud service on the cloud management platform (pre-charge and then settle according to the final resource usage, or purchase on an annual or monthly basis). After the purchase is successful, the tenant can upload the training data set corresponding to the image classification task to be implemented to the training device deployed in the data center. Accordingly, the training device receives the training data set uploaded by the tenant and performs the training operation of the image classification model according to the training data set. The trained image classification model can be used or downloaded by the tenant online (for example, the tenant can download the parameters of the trained image classification model) to complete specific image classification tasks.
[0060] For another example, the training device can also be deployed on a computing device in an edge environment (also called an edge computing device), where the edge environment is an environment that is geographically close to the user's terminal computing device, and the edge environment includes edge computing devices, such as edge servers, edge stations with computing capabilities, etc. Alternatively, the training device can also be deployed on a terminal computing device, which includes a terminal server, a smart phone, a laptop, a tablet computer, a personal desktop computer, etc. Alternatively, the training device can also be distributed and deployed in different environments (including any two or three of a data center in a cloud environment, a computing device in an edge environment, or a terminal computing device). For example, the training device can be logically divided into multiple parts, each of which has different functions. A part of the training device can be deployed on a computing device in a cloud environment, and another part can be deployed on a computing device in an edge environment. The various parts of the training device deployed in different environments collaborate to provide the function of training an image classification model. It should be understood that the embodiments of the present application do not limit the division of the various parts of the training device, nor do they limit the deployment methods of the various parts of the training device. In actual application, it can be adaptively deployed according to specific application requirements or the computing power of the hardware device.
[0061] Combine the following Figure 2 The training method of the image classification model provided in the embodiment of the present application is described in detail.
[0062] S101: The training device obtains a training data set.
[0063] The training data set is constructed for the image classification task to be implemented, and the training data set includes multiple training images and the category label corresponding to each training image in the multiple training images. The category label corresponding to the training image is the target for using the image to train a model (i.e., an image classification model) that can be used to implement the image classification task. For example, to train a model that can be used to identify different vehicles, the training data set may include images of multiple different vehicles, and each image may have a label to identify the type of vehicle contained therein, such as a car, a bus, etc. In this example, the type of vehicle in each image is the category label of the image. For another example, to train a model that can be used to identify different animals, the training data set may include images of multiple different animals, and each image may have a label to identify the type of animal contained therein, such as a cat, a dog, etc. In this example, the type of animal in each image is the category label of the image.
[0064] Optionally, the training images in the training data set may be preprocessed images, and the preprocessing may include one or more of the following operations: scaling or normalizing the image size; verifying the images to eliminate individual images that seriously affect the model training effect; verifying the category labels corresponding to the images to eliminate or correct individual images whose category labels do not match the images.
[0065] In some embodiments, the training device provides an interface, such as a graphical user interface (GUI), an application programming interface (API), a command line interface (CLI), etc. A user or other device storing a training data set can upload the training data set to the training device by calling the interface, and accordingly, the training device obtains the training data set.
[0066] S102: The training device processes each training image in the training data set to obtain a target input vector corresponding to each training image.
[0067] Specifically, the training device concatenates the prompt vector and the mask vector of the pre-trained model for each training image to obtain a target input vector corresponding to each training image.
[0068] The mask vector of the pre-trained model is a vector (or parameter) used to mask the training image in the pre-trained model. The pre-trained model can be a model based on the Transformer architecture, and the mask vector of the pre-trained model can be a vector used in the mask prediction stage of the pre-trained model (see the term explanation section above for details) to randomly mask part of the image blocks of the input image. The hint vector refers to an additionally added optimizable vector (or parameter), and "optimizable" means that the vector can be dynamically adjusted during the training process of the model. In some embodiments, the hint vector can be set according to the category in the image classification task to be implemented, or it can be other vectors used to assist the pre-trained model in implementing the image classification task, and it can also be, for example, an initialization vector, a vector randomly sampled in the vector space, or a vector after a linear change of the randomly sampled vector, etc.
[0069] The following describes how the training device in this step obtains the target input vector corresponding to each training image by taking any training image (assuming it is training image x) among the multiple training images as an example, which may specifically include the following steps:
[0070] 1. The training device processes the training image x into blocks to obtain multiple image blocks.
[0071] 2. The training device encodes each image block of the multiple image blocks to obtain an image block encoding sequence corresponding to the training image x.
[0072] In some embodiments, the training device may encode any image block among the multiple image blocks by the following formula to obtain the encoding vector corresponding to the image block: The image block encoding sequence corresponding to the training image x includes the encoding vectors corresponding to the multiple image blocks.
[0073] e i =Embed pre (x i )
[0074] In this formula, x i represents any image block of the training image x, 1≤i≤n, and i and n are both positive integers, and n represents the number of image blocks obtained after the training image x is segmented. i Represents x i The corresponding encoding vector. Embed pre () represents x i With e i It can be further concluded that the image block coding sequence is [e1,…,e n ].
[0075] 3. The training device concatenates the comprehensive classification vector of the pre-trained model with the image block coding sequence corresponding to the training image x to obtain the original input vector corresponding to the training image x.
[0076] In some embodiments, the training device can use the following formula to combine the comprehensive classification vector of the pre-trained model with the training image x i The corresponding picture block encoding sequences are concatenated to obtain the original input vector corresponding to the training image x.
[0077] H o =[e [CLs] ,e1,…,e n ]
[0078] In this formula, H o represents the original input vector corresponding to the training image x, e [CLS] Represents the comprehensive classification vector (class token) of the pre-trained model, e [CLS] and the image block coding sequence (i.e. [e1,…,e n ]) in any encoding vector (e.g., e i) has the same number of rows. This formula indicates that the original input vector corresponding to the training image x is obtained by concatenating the comprehensive classification vector of the pre-trained model before the picture block coding sequence corresponding to the training image x. It should be understood that the implementation method represented by this formula is only used as an example. In actual applications, the training device can splice the comprehensive classification vector of the pre-trained model with the picture block coding sequence corresponding to the training image x according to the specific situation. The splicing method (for example, concatenation or parallel connection) and position are not limited in this embodiment of the application. For example, the comprehensive classification vector of the pre-trained model can be concatenated after the picture block coding sequence corresponding to the training image x, that is, H o =[e1,…,e n ,e [CLS] ]. Alternatively, the comprehensive classification vector of the pre-trained model can be connected to the image block encoding sequence corresponding to the training image x, that is, H o =[e [CLS] ;e1;…;e n ], in this example, the image block encoding sequence corresponding to the training image x can be [e1; ...; e n ],e [CLS] and any encoding vector in the image block encoding sequence (e.g., e i ) have the same number of columns.
[0079] It is worth noting that the “concatenation” used in the embodiments of the present application refers to splicing along the row direction of the vector, and the “parallel connection” refers to splicing along the column direction of the vector.
[0080] 4. The training device concatenates the prompt vector and the mask vector of the pre-trained model with the original input vector corresponding to the training image x to obtain the target input vector corresponding to the training image x.
[0081] In some embodiments, the training device may concatenate the prompt vector and the mask vector of the pre-trained model with the original input vector corresponding to the training image x by the following formula, thereby obtaining the target input vector corresponding to the training image x.
[0082] H vp =[H o ,p1,…,p m ,e [MASK] ]
[0083] In this formula, H vp represents the target input vector corresponding to the training image x, p1,…,p m represents the prompt vector (m is a positive integer greater than 0), e [MASK] represents the mask vector of the pre-trained model, and H o , p1, …, p m 、e [MASK]The number of rows is the same. This formula indicates that the target input vector corresponding to the training image x is obtained by concatenating the prompt vector and the mask vector of the pre-trained model to the original input vector corresponding to the training image x. It should be understood that the implementation method represented by this formula is only used as an example. In actual applications, the training device can splice the prompt vector and the mask vector of the pre-trained model with the original input vector corresponding to the training image x according to the specific situation. The splicing method (for example, concatenation or parallel connection) and position are not limited in this embodiment of the application. For example, the prompt vector and the mask vector of the pre-trained model can be concatenated before the original input vector corresponding to the training image x, that is, H vp =[p1,…,p m ,e [MASK] ,H o ]. Alternatively, the hint vector can be concatenated before the original input vector corresponding to the training image x, and the mask vector of the pre-trained model can be concatenated after the original input vector corresponding to the training image x, that is, H vp =[p1,…,p m ,H o ,e [MASK] Alternatively, the hint vector can be concatenated to the original input vector corresponding to the training image x, and the mask vector of the pre-trained model can be concatenated between the original input vectors corresponding to the training image x, such as H vp =[e [CLs] ,e [MASK] ,e1,…,e n ,p1,…,p m ], or H vp =[e [CLs] ,e1,…,e [MASK] ,…,e n ,p1,…,p m Alternatively, the hint vector and the mask vector of the pre-trained model can be concatenated to the original input vector corresponding to the training image x, that is, H vp =[H o ;p1;…;p m ;e [MASK] ], in this example, the original input vector corresponding to the training image x can be H o =[e [CLS] ;e1;…;e n ], H o , p1, …, p m 、e [MASK] The same number of columns.
[0084] It should be understood that in order to retain more features of the training image, the training device adopts the above-mentioned splicing method, that is, splicing the mask vector with the training image, rather than directly using the mask vector to mask some image blocks in the training image. In this way, in the subsequent model training process, the complete features of the training image can be better learned, so that the trained image classification model is more accurate.
[0085] S103: The training device optimizes the prompt vector based on the pre-trained model and the target input vector corresponding to each training image to obtain an image classification model.
[0086] Specifically, the training device uses a pre-trained model to predict the mask vector in the target input vector corresponding to each training image to obtain the predicted visual words corresponding to each training image, and then optimizes the prompt vector based on the predicted visual words corresponding to each training image and the category label corresponding to each training image, thereby obtaining an image classification model.
[0087] In some embodiments, the training device optimizes the prompt vector based on the predicted visual words corresponding to each training image and its corresponding category label, including: the training device maps the predicted visual words corresponding to each training image and the category label corresponding to the image to the same vector space, and then optimizes the prompt vector based on the similarity between the mapped predicted visual words and the category label. It should be understood that the predicted visual words corresponding to the training images are non-displayed semantic information. Through the above mapping rules (i.e., mapping the predicted visual words corresponding to the training images and the category labels corresponding to the images to the same vector space), the distance between the predicted visual words corresponding to the training images and the category labels corresponding to the images can be shortened, so that the predicted visual words corresponding to the images can be better supervised by the category labels corresponding to the images, thereby improving the accuracy of the classification results.
[0088] The following still takes the training image x as an example to describe this step in detail, which may specifically include the following steps:
[0089] 1. The training device sets a category vector for the category label corresponding to the training image x.
[0090] The training device does not specifically limit the category vector when setting it. For example, the category vector can be a row vector or a column vector; it can be a 1*128-dimensional vector or a 1*256-dimensional vector; the data in the category vector can also be randomly set.
[0091] 2. The training device predicts the mask vector in the target input vector corresponding to the training image x to obtain a predicted visual word.
[0092] As can be seen from the above S102, the target input vector corresponding to the training image x can be divided into two parts, one part is the mask part (i.e., the mask vector), and the other part is the non-mask part (including the prompt vector and the original input vector). Therefore, in this step, the training device predicts the mask vector in the target input vector corresponding to the training image x, including: the training device predicts the mask part in the target input vector corresponding to the training image x based on the non-mask part in the target input vector corresponding to the training image x.
[0093] 3. The training device maps the predicted visual words to the vector space where the category vector corresponding to the category label of the training image x is located.
[0094] Specifically, the training device performs a dimension transformation on the predicted visual word based on the dimension of the category vector corresponding to the category label of the training image x, and the dimension of the predicted visual word after the transformation is the same as that of the category vector, that is, the two are in the same vector space.
[0095] In some embodiments, the training device may perform dimension transformation on the predicted visual words using the following formula.
[0096]
[0097] In this formula, h [MASK] represents the predicted visual word, u [MASK] Represents the transformed predicted visual word. Indicates h [MASK] The vector space where Indicates u [MASK] In one possible implementation, A is the mapping matrix used to map h [MASK] Transform the dimension so that it is mapped to the vector space where the category vector corresponding to the category label of the training image x is located. For example, h [MASK] is a 1*768-dimensional vector, and the category vector corresponding to the category label of the training image x is a 1*128-dimensional vector, then A can be a 768*128-dimensional matrix. Correspondingly, the transformed predicted visual word is a 1*128-dimensional vector, that is, the transformed predicted visual word and the category vector corresponding to the category label of the training image x are in the same vector space.
[0098] 4. The training device determines the similarity between the category vector corresponding to the category label of the training image x and the predicted visual word after the above transformation.
[0099] In some embodiments, the training device may determine the similarity between the category vector corresponding to the category label of the training image x and the predicted visual word after the above transformation by the following formula.
[0100] sim j =u [MASK] ·(c j ) T
[0101] In this formula, c j Represents the category vector corresponding to the category label of the training image x, (c j ) T Indicates c j The transposed vector, sim j Represents the similarity (specifically cosine similarity) between the category vector corresponding to the category label of the training image x and the predicted visual word after the above transformation. It should be understood that the implementation method represented by this formula is only used as an example. In actual applications, the training device can also determine the similarity between the category vector corresponding to the category label of the training image x and the predicted visual word after the above transformation by calculating the Euclidean distance, Manhattan distance or Hamming distance between the two.
[0102] 5. The training device optimizes the prompt vector in the target input vector corresponding to the training image x based on the similarity between the category vector corresponding to the category label of the training image x and the predicted visual word after the above transformation.
[0103] Specifically, the training device calculates the loss value (loss) of the above-mentioned predicted visual word and the category label corresponding to the training image x based on the similarity between the category vector corresponding to the category label of the training image x and the above-mentioned transformed predicted visual word using a loss function, and then adjusts the prompt vector in the target input vector corresponding to the training image x based on the loss value.
[0104] In some embodiments, the training device may calculate the loss value of the above-mentioned predicted visual word and the category label corresponding to the training image x by using the loss function shown in the following formula.
[0105]
[0106] In this formula, N C Represents the number of categories, that is, the number of category labels corresponding to the training images in the training data set is N C , N C is a positive integer greater than 0. c k Represents the category vector corresponding to any category label, k is a positive integer, 1≤k≤N C The loss function shown in this formula is a normalized exponential function (Softmax function). It should be understood that in practical applications, the training device can also use other loss functions (for example, exponential loss function) to calculate the loss value of the above-mentioned predicted visual word and the category label corresponding to the training image x.
[0107] The training device can iteratively adjust the prompt vector using each training image in the training data set through the above steps 1 to 5 until the image classification model can output an output value that is the same as the category label corresponding to the input training image with high accuracy. At this time, the image classification model can be used to perform image classification tasks with high accuracy.
[0108] It should be understood that in the training process of the above-mentioned image classification model, the training device designs the image classification task (i.e., the downstream task) to be a task with the same form as the upstream task. The reason is as follows: As can be seen from the foregoing, the upstream task is the training task of the pre-trained model, and the pre-trained model is obtained by mask prediction, that is, the mask vector is used to randomly mask part of the image blocks of the input image, and then the remaining image blocks in the input image (i.e., the unmasked image blocks) are used to predict the visual words of the masked image blocks, and then the parameters of the pre-trained model are continuously adjusted based on the visual word labels of the masked image blocks and the predicted visual words, so as to obtain the pre-trained model. This process is consistent with the training process of the image classification model described in the above embodiment in terms of ideas. Therefore, in the embodiment of the present application, the training device designs the image classification task to be a task with the same form as the upstream task, so as to train the image classification model. For this reason, the difference between the image classification task and the upstream task is narrowed, so that the learning strategy used in the training process has better stability and can be used for the training of image classification models for different image classification tasks. In addition, it can also make full use of the pre-trained knowledge obtained from upstream tasks, so that the trained image classification model can accurately extract the underlying information that can characterize the training image category (that is, the predicted visual words corresponding to the training image), thereby improving the accuracy of image classification.
[0109] Further, it can be seen from the above S103 that in the process of training the image classification model using the method provided by the embodiment of the present application, in addition to focusing on how to accurately extract the underlying information that can characterize the training image category (i.e., the predicted visual words corresponding to the training image), it is also focused on how to use the above-extracted underlying information for classification (i.e., by mapping the predicted visual words corresponding to the training image and the category label corresponding to the image to the same vector space to determine the classification result of the image). Since the predicted visual words and category labels after mapping are in the same vector space, the classification result of the image can be determined by calculating the distance (i.e., similarity) between the two, and then the prompt vector is adjusted in reverse. Compared with the prior art method of directly mapping the predicted visual words to the category label to achieve classification by adding a classification head (e.g., MLP), in the process of training the image classification model using the method provided by the embodiment of the present application, there is no need to add additional design (i.e., classification head), and there is no need to train the classification head parameters, and the classification result has higher accuracy because the distance between the predicted visual words and the category label is shortened during classification.
[0110] The image classification model obtained by executing the above S101-S103 can be used to perform corresponding image classification tasks. To this end, the embodiment of the present application also provides an image classification method, which uses the image classification model obtained by the above training to classify the image to be processed, thereby obtaining the classification result of the image.
[0111] The training method of the image classification model provided in the embodiment of the present application can be performed by an inference device. The inference device can be a software device, a hardware device, or a combination of a software device and a hardware device. When the inference device is a software device, the inference device can be a virtual machine, a container, or software with a model inference function.
[0112] The deployment of the inference device is flexible. For example, the inference device can be independently deployed in a data center of a cloud environment. In some embodiments, the inference device can be independently deployed on a server or virtual machine in a data center of a cloud environment, or distributedly deployed on multiple servers or multiple virtual machines in a data center of a cloud environment, or distributedly deployed on servers and virtual machines in a data center of a cloud environment. Figure 3As shown, the inference device is deployed by the cloud service provider in the data center of the cloud environment. Accordingly, the cloud service provider provides the functions of the inference device as cloud services to tenants. Tenants can register an account on the cloud management platform, log in to the cloud management platform through the account, and purchase the cloud service on the cloud management platform (pre-charge and then settle according to the final resource usage, or purchase on an annual or monthly basis). After the purchase is successful, the tenant can upload the image to be processed to the inference device deployed in the data center. Accordingly, the inference device receives the image to be processed uploaded by the tenant, inputs the image to be processed into the image classification model, and the inference device also returns the classification result of the image to be processed by the image classification model to the tenant. Tenants can further complete specific tasks based on the classification results, such as prevention and control warnings.
[0113] In other embodiments, the inference device and the training device described above can be deployed in the data center of the cloud environment. The tenant can first purchase only the part of the cloud service corresponding to the training device on the cloud management platform to obtain the image classification model. When the image classification model needs to be used to classify the image, the tenant purchases the part of the cloud service corresponding to the inference device to complete the classification of the image. Alternatively, the cloud service provider combines the training device and the inference device into one cloud service and provides it to the tenant. In this case, after purchasing the cloud service, the tenant can complete the training of the image classification model and the classification of the image in the data center of the cloud environment.
[0114] In addition to the data center of the cloud environment, the reasoning device can also be deployed on a computing device in the edge environment, or a terminal computing device, or distributedly deployed in different environments (including any two or three of the data center of the cloud environment, the computing device of the edge environment, or the terminal computing device). When the reasoning device is distributedly deployed in different environments, the reasoning device can be logically divided into multiple parts, each part has different functions, a part of the reasoning device can be deployed on a computing device in the cloud environment, and another part can be deployed on a computing device in the edge environment. The various parts of the reasoning device deployed in different environments cooperate to provide image classification functions. It should be understood that the embodiments of the present application do not limit the division of the various parts of the reasoning device, nor do they limit the deployment methods of the various parts of the reasoning device. In actual application, it can be adaptively deployed according to specific application requirements or the computing power of the hardware equipment.
[0115] Combine the following Figure 4 The image classification method provided in the embodiment of the present application is described in detail.
[0116] S201: The inference device obtains an image to be processed.
[0117] Optionally, the image to be processed may be a pre-processed image, for example, an image that has been scaled or normalized according to an image size specification.
[0118] In some embodiments, the reasoning device provides an interface, such as a GUI, API, CLI, etc. A user or other device storing an image to be processed can upload the image to the reasoning device by calling the interface, and accordingly, the reasoning device obtains the image.
[0119] S202: The inference device classifies the image to be processed based on the image classification model to obtain a classification result of the image.
[0120] Specifically, the inference device inputs the image to be processed into the image classification model, and accordingly, the image classification model obtains the image to be processed and processes the image to be processed, thereby obtaining a classification result of the image. The image classification model here is an image classification model obtained through the above-mentioned S101-S103 training, and it includes the following parameters: parameters of the pre-trained model (for example, mask vector) and optimized prompt vectors. As can be seen from the foregoing, the pre-trained model is obtained based on mask vector training. The optimized prompt vector is optimized based on the target input vector corresponding to multiple training images (see S103 above for details), and the target input vector corresponding to each training image is obtained by splicing the mask vector of the pre-trained model and the unoptimized prompt vector for each training image (see S102 above for details).
[0121] In some embodiments, the inference device classifies the image to be processed based on the image classification model, including: the inference device concatenates the mask vector and the optimized prompt vector of the pre-trained model with the image to be processed to obtain a target input vector corresponding to the image to be processed. Then, the inference device classifies the image to be processed based on the target input vector corresponding to the image to be processed.
[0122] In some embodiments, the inference device classifies the image to be processed based on the target input vector corresponding to the image, including: the inference device determines the predicted visual words corresponding to the image based on the target input vector corresponding to the image to be processed, and then maps the predicted visual words corresponding to the image and the category label to the same vector space, and classifies the image based on the mapped predicted visual words and category labels.
[0123] In some embodiments, the inference device maps the predicted visual words and the category labels corresponding to the image to be processed to the same vector space, and classifies the image based on the mapped predicted visual words and the category labels, including: the inference device transforms the dimension of the predicted visual words corresponding to the image based on the dimension of at least one category vector, and then classifies the image based on the similarity between the transformed predicted visual words and the at least one category vector. The at least one category vector is also a parameter of the image classification model, and each category vector in the at least one category vector corresponds to a category label (see step 1 in S103 above for details).
[0124] In some embodiments, the inference device classifies the image based on the similarity between the transformed predicted visual word and the at least one category vector, including: the inference device determines the similarity between the transformed predicted visual word and each category vector, and the category label corresponding to the maximum similarity is the classification result of the image to be processed.
[0125] For simplicity, this step does not describe how the inference device splices the mask vector of the pre-trained model and the optimized prompt vector with the image to be processed. It should be understood that the specific splicing method depends on the training process of the image classification model, and the details can be seen in S102 above. Similarly, this step does not describe how the inference device transforms the dimension of the predicted visual word based on the dimension of at least one category vector, and how to calculate the similarity between the two. It should be understood that the specific implementation process depends on the training process of the image classification model, and the details can be seen in steps 3-4 in S103 above.
[0126] The inference device can implement the image classification task by executing the above S201-S202. Since the image classification model used in performing the image classification task is trained by mask prediction, the image classification task and the upstream task are formally consistent, so the pre-training knowledge learned in the upstream task can be more fully used in the image classification process, and accordingly, the classification result obtained has higher accuracy.
[0127] Combine the following Figure 5-Figure 9 , further describes the image classification model trained by the method provided in the embodiment of the present application and the performance of the model in actual use through specific experimental data.
[0128] The following 10 public data sets were used for experiments: CIFAR100, CIFAR10, Flowers102, Food101, EuroSAT, SVHN, Pets, DTD, Resisc45, and PatchCame. The data in each data set includes a training data set and a test data set. The training data set in each data set is used to train the image classification model, and the test data set in each data set is used to test the image classification model obtained by the above training. In order to facilitate the understanding of the performance of the image classification model trained by the method provided in the embodiment of the present application, the following 7 baseline methods are also selected for comparison in this experiment:
[0129] 1. The pre-trained model is BEITv2. The image classification model is obtained by fine-tuning BEITv2 using the training dataset. This method is referred to as FT+BEITv2 below, where FT stands for fine tuning.
[0130] 2. The pre-trained model is BEITv2. BEITv2 is used to extract features from the training dataset, and then logistic regression is used to fit the category labels. This method is referred to as LP+BEITv2 below, where LP stands for linear probe.
[0131] 3. The pre-trained model is CLIP. The training dataset is used to fine-tune CLIP to obtain an image classification model. This method is referred to as FT+CLIP below.
[0132] 4. The pre-trained model is CLIP. CLIP is used to extract features from the training dataset, and then logistic regression is used to fit the category labels. This method is referred to as LP+CLIP in the following text.
[0133] 5. The pre-trained model is CLIP. The images in the training dataset and the texts corresponding to the images are used to train CLIP to obtain an image classification model. This method is referred to as TP+CLIP below, where TP stands for textual prompt.
[0134] 6. The pre-trained model is CLIP. Learnable perturbations are added to the images in the training dataset as visual prompts. The images with added visual prompts and the text corresponding to the images are used to train CLIP to obtain an image classification model. This method is referred to as VP+TP+CLIP below, where VP stands for visual prompt.
[0135] 7. The pre-trained model is BEITv2. VPT is used to fine-tune BEITv2 to obtain an image classification model. This method is referred to as VPT+BEITv2 below.
[0136] First see Figure 5 ,like Figure 5 As shown, when using the training method of the image classification model provided in the embodiment of the present application, the following learning strategies can be used on the above 10 data sets: the optimization algorithm (optimizer) is AdamW, total epochs = 50, warmup epochs = 5, learning rate = 0.001, weight decay = 0.01, momentum = 0.9, and the scheduler is the cos strategy. In other words, for different image classification tasks, the training method of the image classification model provided in the embodiment of the present application supports the use of a unified learning strategy. On the contrary, the baseline method VPT+BEITv2 listed above needs to search for different learning strategies for different image classification tasks. Taking the two data sets of Flowers102 and Pets as examples, Figure 6 As shown, (a) and (b) respectively show the corresponding loss value (shown as Loss) and accuracy (shown as Accuracy) when training the model on Flowers102 using different learning strategies (epoch and learning rate (shown as lr) in the figure). (c) and (d) respectively show the corresponding loss value (shown as Loss) and accuracy (shown as Accuracy) when training the model on Pets using different learning strategies (epoch and learning rate (shown as lr) in the figure). Figure 6 It can be seen that when the baseline method VPT+BEITv2 is used to train the image classification model, the optimal learning strategies corresponding to the Flowers102 and Pets datasets are different, that is, different learning strategies need to be used to train the model on these two datasets. Therefore, compared with the above baseline method, the training method of the image classification model provided in the embodiment of the present application does not need to spend a lot of time searching for the corresponding optimal learning strategy for different image classification tasks, so that different image classification tasks can be efficiently implemented.
[0137] Figure 7 Figure 2 shows the test results of image classification models trained using different methods. Figure 7 As shown in the figure, this test was conducted under the condition of epoch 50, that is, for each data set, all the data in the test data set were trained in the image classification model 50 times. In addition, since the performance of the VPT method at epoch 100 is better than its performance at epoch 50, this test also tested the VPT method and the method provided in the embodiment of the present application at epoch 100. Figure 7 The following conclusions can be drawn:
[0138] 1. On the above 10 data sets, the average accuracy of the image classification model trained using the method provided in the embodiment of the present application is about 5% higher than the average accuracy of the image classification model trained using LP+BEI Tv2.
[0139] 2. On the first 9 data sets, the average accuracy of the image classification model trained using the method provided in the embodiments of the present application (88.65%) is close to the average accuracy of the image classification model trained using FT+CLI P (89.4%).
[0140] 3. On the first 9 data sets, the average accuracy of the image classification models trained using LP+CLI P, TP+CLI P and VP+TP+CLI P were 83%, 56.69% and 79.67%, respectively. Therefore, on the first 9 data sets, the average accuracy of the image classification models trained using these three methods were lower than the average accuracy of the image classification models trained using the method provided in the embodiments of the present application (88.65%).
[0141] 4. On the above 10 data sets, regardless of epoch 50 or 100, the average accuracy of the image classification model trained by the method provided in the embodiment of the present application is higher than the average accuracy of the image classification model trained by VPT. Moreover, on most data sets (i.e., 8 data sets), the accuracy of the image classification model trained by the method provided in the embodiment of the present application is higher than the accuracy of the image classification model trained by VPT.
[0142] 5. On the above 10 data sets, when the epoch is 50, the average accuracy of the image classification model trained by the method provided in the embodiment of the present application is 87.91. The visualization diagram of the classification results can be seen in Figure 8 (The black circles in the figure represent the category vectors corresponding to the category labels, and the other discrete points represent the predicted visual words obtained by the image classification model predicting the test data set.) When the epoch is 100, the average accuracy of the image classification model trained using VPT is 87.91. In other words, the image classification model trained using the method provided in the embodiment of the present application can achieve the performance of the image classification model trained using VPT when the epoch is 100 when the epoch is 50. This shows that compared with VPT, the method provided in the embodiment of the present application can save the training time of the image classification model.
[0143] For further information, see Fig. 9 , Fig. 9The performance of the mapping rule used by the image classification model in the embodiment of the present application (i.e., determining the classification result of the image by mapping the predicted visual words and possible categories corresponding to the image to the same vector space) is shown. In order to facilitate understanding of the performance of the mapping rule provided in the embodiment of the present application, the following two mapping rules are also selected for comparison in this experiment:
[0144] 1. Directly map the predicted visual words to categories. This mapping rule is referred to as MLP-1 below.
[0145] 2. First, map the predicted visual word into a 128-dimensional vector, and then map the 128-dimensional vector to the category. This mapping rule is referred to as MLP-2 below.
[0146] On the training data sets among the above 10 data sets, the image classification model is trained using MLP-1, MLP-2 and the mapping rules provided in the embodiments of the present application. Then, the image classification model obtained by the above training is used to test the test data set among the above 10 data sets, and the obtained Fig. 9 The test results are shown. Fig. 9 It can be seen that:
[0147] 1. On the above 10 data sets, although the average proportion of fine-tuning parameters when using MLP-1 is lower than the average proportion of fine-tuning parameters when using the mapping rule provided in the embodiment of the present application, the average accuracy of image classification achieved by the mapping rule provided in the embodiment of the present application is 8.18% higher than the average accuracy of image classification achieved by MLP-1. The proportion of fine-tuning parameters is the proportion of fine-tuning parameters to all parameters in the model.
[0148] 2. On the above 10 data sets, the mean proportion of fine-tuning parameters when using MLP-2 is lower than the mean proportion of fine-tuning parameters when using the mapping rules provided in the embodiments of the present application, and the mean accuracy of image classification achieved by the mapping rules provided in the embodiments of the present application is higher than the mean accuracy of image classification achieved by MLP-2.
[0149] It is worth noting that the mapping rules provided by the above-mentioned MLP-1, MLP-2 and the embodiments of the present application are all based on determining the category of the image based on the predicted visual words corresponding to the image. Therefore, in order to more accurately determine the performance of each mapping rule, when using MLP-1, MLP-2 and the mapping rules provided by the embodiments of the present application to train and test the image classification model, it is necessary to ensure that the predicted visual words corresponding to the images used are consistent. Here, the method provided in the embodiments of the present application is used to obtain the predicted visual words corresponding to the image.
[0150] The previous article describes in detail the training method of the image classification model and the method of using the trained image classification model to classify images. Figure 10-13 , a training device for executing the training method of the above-mentioned image classification model, and an inference device for executing the above-mentioned image classification method are further described.
[0151] Fig.10 The structural diagram of the training device is shown as an example. Fig.10 As shown, the training device 100 includes an acquisition module 110, a splicing module 120 and an optimization module 130. The above modules work together to implement the steps performed by the training device in the above method embodiment. Specifically, the acquisition module 110 is used to perform the above S101; the splicing module 120 is used to perform the above S102; and the optimization module 130 is used to perform the above S103.
[0152] In the embodiment of the present application, the acquisition module 110, the splicing module 120 and the optimization module 130 can be implemented by software or by hardware. Exemplarily, the implementation of the optimization module 130 is described below by taking the optimization module 130 as an example. Similarly, the implementation of the acquisition module 110 and the splicing module 120 can refer to the implementation of the optimization module 130.
[0153] As an example of a software functional unit, the optimization module 130 may include code running on a computing instance. Among them, the computing instance may include at least one of a physical host (computing device), a virtual machine, and a container. Further, the above-mentioned computing instance may be one or more. For example, the optimization module 130 may include code running on multiple hosts / virtual machines / containers. It should be noted that the multiple hosts / virtual machines / containers used to run the code may be distributed in the same region (region) or in different regions. Furthermore, the multiple hosts / virtual machines / containers used to run the code may be distributed in the same availability zone (AZ) or in different AZs, each AZ including a data center or multiple data centers with similar geographical locations. Among them, usually a region may include multiple AZs.
[0154] Similarly, multiple hosts / virtual machines / containers used to run the code can be distributed in the same virtual private cloud (VPC) or in multiple VPCs. Usually, a VPC is set up in a region. For cross-region communication between two VPCs in the same region and between VPCs in different regions, a communication gateway needs to be set up in each VPC to achieve interconnection between VPCs through the communication gateway.
[0155] As an example of a hardware functional unit, the optimization module 130 may include at least one computing device, such as a server, etc. Alternatively, the optimization module 130 may also be a device implemented using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD), etc. The PLD may be a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.
[0156] The multiple computing devices included in the optimization module 130 can be distributed in the same region or in different regions. The multiple computing devices included in the optimization module 130 can be distributed in the same AZ or in different AZs. Similarly, the multiple computing devices included in the optimization module 130 can be distributed in the same VPC or in multiple VPCs. The multiple computing devices can be any combination of computing devices such as servers, ASICs, PLDs, CPLDs, FPGAs, and GALs.
[0157] It should be noted that, in other embodiments, the acquisition module 110 can be used to execute any of the steps in S101-S103 above; the splicing module 120 can be used to execute any of the steps in S101-S103 above; and the optimization module 130 can be used to execute any of the steps in S101-S103 above. The steps that the acquisition module 110, the splicing module 120, and the optimization module 130 are responsible for implementing can be specified as needed, and the acquisition module 110, the splicing module 120, and the optimization module 130 respectively implement different steps in the training method of the above-mentioned image classification model to realize all the functions of the training device 100.
[0158] Fig.11 The structural diagram of the inference device is shown as an example. Fig.11 As shown, the inference device 200 includes an acquisition module 210 and a classification module 220. The above modules work together to implement the steps performed by the inference device in the above method embodiment. Specifically, the acquisition module 210 is used to perform the above S201; the classification module 220 is used to perform the above S202.
[0159] In the embodiment of the present application, both the acquisition module 210 and the classification module 220 can be implemented by software, or can be implemented by hardware. Exemplarily, the implementation of the classification module 220 is introduced below by taking the classification module 220 as an example. Similarly, the implementation of the acquisition module 210 can refer to the implementation of the classification module 220.
[0160] As an example of a software functional unit, the classification module 220 may include code running on a computing instance. Among them, the computing instance may include at least one of a physical host (computing device), a virtual machine, and a container. Further, the above-mentioned computing instance may be one or more. For example, the classification module 220 may include code running on multiple hosts / virtual machines / containers. It should be noted that the multiple hosts / virtual machines / containers used to run the code may be distributed in the same region (region) or in different regions. Furthermore, the multiple hosts / virtual machines / containers used to run the code may be distributed in the same availability zone (AZ) or in different AZs, each AZ including a data center or multiple data centers with similar geographical locations. Among them, usually a region may include multiple AZs.
[0161] Similarly, multiple hosts / virtual machines / containers used to run the code can be distributed in the same virtual private cloud (VPC) or in multiple VPCs. Usually, a VPC is set up in a region. For cross-region communication between two VPCs in the same region and between VPCs in different regions, a communication gateway needs to be set up in each VPC to achieve interconnection between VPCs through the communication gateway.
[0162] As an example of a hardware functional unit, the classification module 220 may include at least one computing device, such as a server, etc. Alternatively, the classification module 220 may also be a device implemented using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD), etc. The PLD may be a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.
[0163] The multiple computing devices included in the classification module 220 can be distributed in the same region or in different regions. The multiple computing devices included in the classification module 220 can be distributed in the same AZ or in different AZs. Similarly, the multiple computing devices included in the classification module 220 can be distributed in the same VPC or in multiple VPCs. The multiple computing devices can be any combination of computing devices such as servers, ASICs, PLDs, CPLDs, FPGAs, and GALs.
[0164] It should be noted that, in other embodiments, the acquisition module 210 can be used to execute any of the steps in S201-S202 above; the classification module 220 can be used to execute any of the steps in S201-S202 above. The steps that the acquisition module 210 and the classification module 220 are responsible for implementing can be specified as needed, and the acquisition module 210 and the classification module 220 respectively implement different steps in the above image classification method to achieve all the functions of the reasoning device 200.
[0165] The present application also provides a computing device. Fig.12 As shown, the computing device 300 includes a bus 310, a processor 320, a memory 330, and a communication interface 340. The processor 320, the memory 330, and the communication interface 340 communicate with each other through the bus 310. The computing device 300 can be a server or a terminal device. It should be understood that the embodiment of the present application does not limit the number of processors and memories in the computing device 300.
[0166] The bus 310 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Fig.12 The bus 310 is represented by only one line, but it does not mean that there is only one bus or one type of bus. The bus 310 may include a path for transmitting information between various components of the computing device 300 (eg, memory 330, processor 320, communication interface 340).
[0167] The processor 320 may include any one or more processors such as a CPU, a GPU, a TPU, a microprocessor (MP) or a digital signal processor (DSP).
[0168] The memory 330 may include a volatile memory, such as a random access memory (RAM). The memory 330 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD), or a solid state drive (SSD).
[0169] The memory 330 stores executable program codes. The processor 320 executes the executable program codes stored in the memory 330 to implement the functions of the modules (including the acquisition module 110, the splicing module 120 and the optimization module 130) in the aforementioned training device 100, thereby implementing the training method of the image classification model described above. Alternatively, the processor 320 executes the executable program codes stored in the memory 330 to implement the functions of the modules (including the acquisition module 210 and the classification module 220) in the aforementioned reasoning device 200, thereby implementing the image classification method described above. That is, the memory 330 stores instructions for executing the training method of the image classification model and / or the image classification method described above.
[0170] The communication interface 340 uses a transceiver module such as but not limited to a network interface card or a transceiver to implement communication between the computing device 300 and other devices or a communication network. For example, the computing device 300 obtains a training data set and / or an image to be processed through the communication interface 340.
[0171] The embodiment of the present application also provides a computing device cluster. The computing device cluster includes one or more computing devices. The computing device can be a server, such as a central server, an edge server, or a local server in a local data center. In some embodiments, the computing device can also be a terminal device such as a desktop computer, a laptop computer, or a smart phone.
[0172] like Fig.13 As shown, the computing device cluster 400 includes one or more computing devices 300. The memory 330 in one or more computing devices 300 in the computing device cluster 400 may store the same instructions for executing the training method of the above-mentioned image classification model and / or the image classification method. Alternatively, the memory 330 of one or more computing devices 300 in the computing device cluster 400 may also respectively store partial instructions for executing the training method of the above-mentioned image classification model and / or the image classification method. In other words, the combination of one or more computing devices 300 can jointly execute the training method of the above-mentioned image classification model and / or the image classification method.
[0173] It should be noted that the memory 330 in different computing devices 300 in the computing device cluster 400 may store different instructions, which are respectively used to execute part of the functions of the training device 100 and / or the reasoning device 200. That is, the instructions stored in the memory 330 in different computing devices 300 may implement the functions of one or more of the aforementioned acquisition module 110, splicing module 120, optimization module 130, acquisition module 210, and classification module 220.
[0174] In some embodiments, one or more computing devices 300 in the computing device cluster 400 may be connected via a network, which may be a wide area network or a local area network.
[0175] The embodiment of the present application also provides a computer program product including instructions. The computer program product may be software or a program product including instructions that can be run on a computing device or stored in any available medium. When the computer program product is run on at least one computing device, the at least one computing device executes the computational graph compilation method described above.
[0176] The embodiment of the present application also provides a computer-readable storage medium. The computer-readable storage medium can be any available medium that can be stored by the computing device or a data storage device such as a data center containing one or more available media, wherein the available medium can be a magnetic medium (such as a floppy disk, a hard disk, a tape), an optical medium (such as a DVD), or a semiconductor medium (such as an SSD), etc. The computer-readable storage medium includes instructions, wherein the instructions instruct the computing device to execute the computational graph compilation method described above, or instruct the computing device to execute the computational graph compilation method described above.
[0177] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the protection scope of the technical solutions of the embodiments of the present application.
Claims
1. A training method for an image classification model, characterized in that: include: Acquire a training data set, wherein the training data set includes a plurality of training images; splicing a prompt vector and a mask vector of a pre-trained model for each training image to obtain a target input vector corresponding to each training image, wherein the pre-trained model is trained by the mask vector; The prompt vector is optimized based on the pre-trained model and the target input vector corresponding to each training image to obtain an image classification model, the parameters of the image classification model include the parameters of the pre-trained model and the optimized prompt vector, and the image classification model is used to perform image classification tasks.
2. The method according to claim 1, characterized in that The splicing hint vector and the mask vector of the pre-trained model for each training image comprises: The prompt vector and the mask vector of the pre-trained model are concatenated with each of the training images.
3. The method according to claim 1 or 2, characterized in that: The optimizing the prompt vector based on the pre-trained model and the target input vector corresponding to each training image includes: Using the pre-trained model, predicting the mask vector in the target input vector corresponding to each training image, to obtain the predicted visual word corresponding to each training image; Mapping the predicted visual words corresponding to each training image and the category labels corresponding to each training image to the same vector space; The prompt vector is optimized based on the similarity between the mapped predicted visual word and the category label.
4. The method according to claim 3, characterized in that Mapping the predicted visual words corresponding to each training image and the category labels corresponding to each training image to the same vector space includes: Setting a corresponding category vector for the category label corresponding to each training image; The predicted visual word is dimensionally transformed based on the dimension of the category vector, and the dimension of the predicted visual word after the transformation is the same as that of the category vector.
5. An image classification method, characterized in that: include: Get the image to be processed; The image to be processed is classified based on an image classification model to obtain a classification result of the image to be processed, wherein the image classification model includes the following parameters: parameters of a pre-trained model and an optimized prompt vector, the parameters of the pre-trained model include a mask vector, the pre-trained model is trained based on the mask vector, the optimized prompt vector is optimized based on the pre-trained model and target input vectors corresponding to multiple training images, and the target input vector corresponding to each training image is obtained by splicing the mask vector and the unoptimized prompt vector for each training image.
6. The method according to claim 5, characterized in that The target input vector corresponding to each training image is obtained by concatenating the mask vector and the unoptimized hint vector with each training image.
7. The method according to claim 5 or 6, characterized in that: The image classification model also includes the following parameters: at least one category vector, and a category label corresponding to each category vector.
8. The method according to claim 7, characterized in that The classifying the image to be processed based on the image classification model includes: Concatenating the mask vector and the optimized hint vector with the image to be processed to obtain a target input vector corresponding to the image to be processed; The image to be processed is classified based on a target input vector corresponding to the image to be processed.
9. The method according to claim 8, characterized in that The classifying the image to be processed based on the target input vector corresponding to the image to be processed includes: Determining a predicted visual word corresponding to the image to be processed based on a target input vector corresponding to the image to be processed; Transforming the dimension of the predicted visual word based on the dimension of the at least one category vector; The image to be processed is classified based on the similarity between the transformed predicted visual word and the at least one category vector.
10. A training device, characterized in that: include: An acquisition module, used to acquire a training data set, wherein the training data set includes a plurality of training images; A splicing module, used for splicing a prompt vector and a mask vector of a pre-trained model for each training image to obtain a target input vector corresponding to each training image, wherein the pre-trained model is obtained by training the mask vector; An optimization module is used to optimize the prompt vector based on the pre-trained model and the target input vector corresponding to each training image to obtain an image classification model, wherein the parameters of the image classification model include the parameters of the pre-trained model and the optimized prompt vector, and the image classification model is used to perform image classification tasks.
11. The device according to claim 10, characterized in that The splicing module is used to concatenate the prompt vector and the mask vector of the pre-trained model with each training image.
12. The device according to claim 10 or 11, characterized in that The optimization module is used to predict the mask vector in the target input vector corresponding to each training image using the pre-trained model to obtain the predicted visual words corresponding to each training image, map the predicted visual words corresponding to each training image and the category labels corresponding to each training image to the same vector space, and optimize the prompt vector based on the similarity between the mapped predicted visual words and the category labels.
13. The device according to claim 12, characterized in that The optimization module is used to set a corresponding category vector for the category label corresponding to each training image, and perform a dimension transformation on the predicted visual word based on the dimension of the category vector, so that the transformed predicted visual word has the same dimension as the category vector.
14. An inference device, characterized in that: include: An acquisition module, used for acquiring an image to be processed; A classification module is used to classify the image to be processed based on an image classification model to obtain a classification result of the image to be processed, wherein the image classification model includes the following parameters: parameters of a pre-trained model and an optimized prompt vector, the parameters of the pre-trained model include a mask vector, the pre-trained model is trained based on the mask vector, the optimized prompt vector is optimized based on the pre-trained model and target input vectors corresponding to multiple training images, and the target input vector corresponding to each training image is obtained by splicing the mask vector and the unoptimized prompt vector for each training image.
15. The device according to claim 14, characterized in that The target input vector corresponding to each training image is obtained by concatenating the mask vector and the unoptimized hint vector with each training image.
16. The device according to claim 14 or 15, characterized in that The image classification model also includes the following parameters: at least one category vector, and a category label corresponding to each category vector.
17. The device according to claim 16, characterized in that The classification module is used to concatenate the mask vector and the optimized hint vector with the image to be processed to obtain a target input vector corresponding to the image to be processed, and classify the image to be processed based on the target input vector corresponding to the image to be processed.
18. The device according to claim 17, characterized in that The classification module is used to determine the predicted visual word corresponding to the image to be processed based on the target input vector corresponding to the image to be processed, perform dimension transformation on the predicted visual word based on the dimension of the at least one category vector, and classify the image to be processed based on the similarity between the transformed predicted visual word and the at least one category vector.
19. A computing device, characterized in that The method comprises a processor and a memory, wherein the processor is used to execute instructions stored in the memory to implement the method according to any one of claims 1 to 9.
20. A computer program product comprising instructions, characterized in that When the instructions are executed by a computing device, the computing device is caused to perform the method according to any one of claims 1 to 9.
21. A computer-readable storage medium, characterized in that: The method comprises computer program instructions, which, when executed by a computing device, cause the computing device to perform the method according to any one of claims 1 to 9.
Citation Information
Patent Citations
A class prediction model training method and device and an interface element class recognition method and device
CN114092949A
Small sample stomach tumor diagnosis system
CN115171889A
Device and method for classification using classification model and computer readable storage medium
US20220101040A1
