Training method for image classification model, image classification method and related apparatus
By splicing the cue vector and the mask vector, and using the mask prediction method to train the image classification model, it solves the problem that different image classification tasks require different learning strategies, improves efficiency and accuracy, and realizes the efficient application of the pre-trained model.
Patent Information
- Application Number
- PCT/CN2024/077437
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-06-07
- Filing Date
- 2024-02-18
- Publication Date
- 2024-09-19
AI Technical Summary
Existing technologies require different learning strategies in image classification tasks, resulting in a large amount of time spent searching for optimal strategies, affecting 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 enables unified deployment of different image classification tasks and efficient use of pre-trained models.
Smart Images

Figure CN2024077437_19092024_PF_FP_ABST
Abstract
Description
Image classification model training method, image classification method and related device
[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office of China on March 10, 2023, with application number 202310231532.5 and invention name “Visual classification method and device”, and the Chinese patent application filed with the State Intellectual Property Office of China on June 7, 2023, with application number 202310671260.0 and invention name “Training method of image classification model, image classification method and related device”, the entire contents of which are incorporated by reference into this application. Technical Field
[0002] 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
[0003] In recent years, pre-trained models have been widely used in image classification tasks in the field of computer vision. Currently, this can be achieved by concatenating an optimizable hint vector to the input of the first layer or each layer of the pre-trained model, adding a corresponding classification head to the model based on the image classification task to be implemented, and then fine-tuning the hint vector and classification head parameters to achieve the relevant image classification task. However, for different image classification tasks, the above method requires the use of different learning strategies (e.g., learning rate, weight decay factor) to adjust the hint vector and classification head parameters, resulting in a large amount of time required to search for the optimal learning strategy, affecting the efficiency and cost of the image classification task.
[0004] Summary of the Invention
[0005] 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.
[0006] In a first aspect, the present application provides a training method for 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-mentioned 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 (for example, the above-mentioned mask vector) and the optimized prompt vector.
[0007] 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.
[0008] In a possible implementation of the first aspect, the training device concatenates the hint vector and the mask vector of the pre-trained model for each training image, including concatenating the hint vector and the mask vector of the pre-trained model with each training image. Concatenation refers to concatenation along the rows of the vectors.
[0009] It should be understood that in practical applications, training images are usually processed as row vectors to facilitate processing. Furthermore, hint vectors and mask vectors are also typically row vectors. In other words, if the training image, hint vector, and mask vector are all row vectors, concatenation is more appropriate for the concatenation method.
[0010] In a possible implementation of the first aspect, the above-mentioned 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, obtains 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.
[0011] In this implementation, the training device predicts the mask vector in the target input vector corresponding to the training image, enabling training of the image classification model through mask prediction. Furthermore, by mapping the predicted visual word corresponding to the training image and the class label corresponding to the image into the same vector space, the distance between the two can be shortened, allowing for more accurate determination of the gap between the predicted visual word and the class label. This in turn allows for more precise adjustment of the cue vector, resulting in a more accurate trained image classification model.
[0012] 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 into the same vector space, including: the training device assigning a corresponding category vector to the category label corresponding to each training image, and then performing a dimensionality 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, i.e., the two are in the same vector space.
[0013] 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. 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 obtained by optimizing the target input vector corresponding to the pre-trained model and 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.
[0014] In the technical solution provided by this application, the image classification model is trained through mask prediction. In other words, the image classification task implemented using the image classification model is also achieved through mask prediction. This narrows the gap between upstream and downstream tasks, allowing pre-trained knowledge from upstream tasks to be more fully applied to downstream tasks, improving the accuracy of classification results.
[0015] In one 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. Concatenation refers to concatenation along the rows of the vectors.
[0016] It should be understood that in practical applications, training images are usually processed as row vectors to facilitate processing. Furthermore, hint vectors and mask vectors are also typically row vectors. In other words, if the training image, hint vector, and mask vector are all row vectors, concatenation is more appropriate for the concatenation method.
[0017] 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.
[0018] In a possible implementation manner of the second aspect, the above-mentioned image classification model further includes the following parameters: at least one category vector, and a category label corresponding to each category vector.
[0019] In a possible implementation of the second aspect, the inference device classifies the image to be processed based on the target input vector corresponding to the image to be processed, including: the inference device determines the 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.
[0020] In this implementation, the transformed predicted visual word and at least one category vector are in the same vector space. Distance calculations between vectors in the same vector space are easier, allowing for more accurate determination of which category vector the transformed predicted visual word is closest to. The category label corresponding to the closest category vector is the image classification result. Therefore, this implementation improves the accuracy of image classification results.
[0021] 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-mentioned 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.
[0022] In a possible implementation of the third aspect, the concatenation module is configured to concatenate the hint vector and the mask vector of the pre-trained model with each training image, wherein concatenation refers to concatenation along the row direction of the vector.
[0023] 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.
[0024] 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 words based on the dimension of the category vector, so that the transformed predicted visual words have the same dimension as the category vector.
[0025] 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. 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 obtained by optimizing the target input vector corresponding to the pre-trained model and 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.
[0026] In one 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. Concatenation refers to concatenation along the rows of the vectors.
[0027] In a possible implementation manner of the fourth aspect, the above-mentioned image classification model further includes the following parameters: at least one category vector, and a category label corresponding to each category vector.
[0028] 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.
[0029] 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 dimensionality 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.
[0030] 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.
[0031] In a sixth aspect, the present application provides a computing device cluster. The computing device cluster includes at least one computing device, each computing device including a processor and a memory. The processor of at least one computing device is configured 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 first aspect and any implementation of the first aspect, and / or part or all of the methods described in the second aspect and any implementation of the second aspect.
[0032] In a seventh aspect, the present application provides a computer program product. This computer program product may be software or a program product containing instructions that can be run on a computing device or stored in any available medium. When this computer program product is run on a computing device or a cluster of computing devices, it causes the computing device or cluster of computing devices to perform 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.
[0033] In an eighth aspect, the present application provides a computer-readable storage medium. The computer storage medium includes computer program instructions that, when executed by a computing device or a computing device cluster, cause the computing device or the computing device cluster to perform 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
[0034] FIG1 is a schematic diagram of an application scenario provided by an embodiment of the present application;
[0035] FIG2 is a flow chart of a method for training an image classification model according to an embodiment of the present application;
[0036] FIG3 is a schematic diagram of another application scenario provided by an embodiment of the present application;
[0037] FIG4 is a flow chart of an image classification method provided in an embodiment of the present application;
[0038] FIG5 is a schematic diagram of an optimal learning strategy provided in an embodiment of the present application;
[0039] FIG6 is a schematic diagram of different optimal learning strategies corresponding to different data sets provided by an embodiment of the present application;
[0040] FIG7 is a schematic diagram of test results of different image classification models provided in an embodiment of the present application;
[0041] FIG8 is a schematic diagram showing a visualization of a classification result provided in an embodiment of the present application;
[0042] FIG9 is a schematic diagram of test results when different mapping rules are used according to an embodiment of the present application;
[0043] FIG10 is a schematic structural diagram of a training device provided in an embodiment of the present application;
[0044] FIG11 is a schematic diagram of the structure of an inference device provided in an embodiment of the present application;
[0045] FIG12 is a schematic diagram of the structure of a computing device provided in an embodiment of the present application;
[0046] FIG13 is a schematic diagram of the structure of a computing device cluster provided in an embodiment of the present application. DETAILED DESCRIPTION
[0047] In order to facilitate understanding of the technical solution provided by this application, some of the terms involved in this application are first explained.
[0048] Pre-trained model: refers to a model with visual representation capabilities trained on a large-scale dataset. This process is also called an upstream task.
[0049] Masked visual token modeling (MVTM): This is an implementation of the upstream task. Specifically, mask vectors are used to randomly mask portions of the input image to a model (e.g., a Transformer-based model). The model then uses self-supervised learning to predict the visual words associated with the masked image patches. This allows the model to learn visual representation capabilities, thus imbuing the pre-trained model with visual representation capabilities.
[0050] Visual words: refer to the visual features of an image and are used to express the content of the image.
[0051] Self-supervised learning: also known as self-supervised training, refers to the process of automatically generating labels based on the 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. Afterwards, the pre-trained model adjusts the parameters of the pre-trained model based on 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 hierarchy 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 visual underlying 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.
[0052] The pre-trained model can be applied to downstream tasks related to 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 is trained on a large number of surveillance videos (including surveillance videos from transportation hubs, streets, commercial areas, etc.) and can detect specific targets (including vehicles, pedestrians, animals, etc.) in the image. Then, the model can be used to detect vehicles passing through a certain intersection based on the surveillance video of the intersection.
[0053] When using a pre-trained model to implement downstream tasks, the pre-trained model needs to be trained based on the training dataset of the downstream task. Compared with the large-scale datasets used in upstream tasks, the training datasets 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 the model based on the data in a given training dataset and the labels corresponding to each data in the training dataset. The labels corresponding to the data in the training dataset are usually manually annotated and are used to identify the correct answer to the data for a specific task.
[0054] During model training, to improve training efficiency and post-training performance, it's necessary to set appropriate hyperparameters (also known as learning strategies). Hyperparameters are parameters that can't be learned from the training data or changed by the training data during training. They are a concept relative to model parameters. Model hyperparameters are typically set manually based on experience or experimentation. Examples include the learning rate, weight decay, momentum, learning rate scheduler, total epochs, and warmup epochs.
[0055] Prompt learning: It refers to narrowing the gap 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.
[0056] Hint vector: refers to an additional optimizable parameter that is used to assist the pre-trained model in achieving downstream tasks.
[0057] In the field of computer vision, upstream tasks can be implemented in a variety of ways, such as the mask prediction mentioned above, supervised discriminant prediction, or contrastive prediction. Similarly, downstream tasks can also be diverse, such as image classification, object detection, and image segmentation. This makes it difficult to formally align the two tasks in computer vision. Furthermore, given the large size and high number of parameters of pre-trained models, the current approach of fine-tuning pre-trained models to achieve downstream tasks (specifically, image classification) is to use VPT (visual prompt tuning). Specifically, the pre-trained model is specifically designed for the image classification task. For example, a learnable prompt vector is concatenated to the input of the first layer or each layer of the pre-trained model, and a classification head (e.g., a multi-layer perceptron (MLP)) is added to the pre-trained model based on the image classification task. The pre-trained model is then trained using a dataset corresponding to the image classification task, resulting in an image classification model that can be directly used to implement the task. In the above method, only the cue vector and classification head parameters need to be fine-tuned based on the dataset corresponding to the image classification task, while the numerous parameters included in the pre-trained model do not need to be adjusted to train the image classification model. However, research and testing have found that for different image classification tasks, the above method often requires the use of different learning strategies to adjust the cue vector and classification head parameters. In other words, using the above method, it takes a considerable amount of time to search for the optimal learning strategy to obtain a suitable image classification model, which affects the efficiency and increases the cost of the image classification task.
[0058] For example, in security scenarios, image classification tasks can include detecting pedestrians, vehicles, and animals in surveillance videos. For these three image classification tasks, using existing methods requires determining the optimal learning strategy for each task. However, the optimal learning strategy varies between tasks, and applying the optimal learning strategy for one task to another often results in poor performance for that task. In other words, using existing methods requires determining three different optimal learning strategies to achieve the three image classification tasks. Therefore, efficiently leveraging pre-trained models to achieve downstream tasks is an urgent issue.
[0059] In response to the above problems, an embodiment of the present application provides a method for training 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 this method achieves formal consistency between downstream tasks (i.e., image classification tasks) and upstream tasks (i.e., training tasks of pre-trained models) when training image classification models, the learning strategy obtained by this method has good stability and can be uniformly deployed on different image classification tasks. In addition, when the downstream tasks and upstream tasks are consistent in form, the pre-trained knowledge can be fully applied to the downstream tasks. Therefore, the image classification model trained by the method provided in the embodiment of the present application has high accuracy in classifying images.
[0060] The training method for the image classification model provided in the embodiments 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 model training capabilities.
[0061] The training device can be deployed flexibly. 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 (e.g., servers). In some embodiments, the training device can be independently deployed on a server or virtual machine in the data center of the cloud environment, or distributedly deployed on multiple servers or multiple virtual machines in the data center of the cloud environment, or distributedly deployed on servers and virtual machines in the data center of the cloud environment. As shown in Figure 1, the training device is deployed in the data center of the cloud environment by the cloud service provider. Accordingly, the cloud service provider provides the functions of the training device as a cloud service to tenants. Tenants can register an account on the cloud management platform, log in to the cloud management platform using this account, and purchase the cloud service on the cloud management platform (pre-charge and then settle based on the final resource usage, or purchase on an annual or monthly basis). After the purchase is successful, the tenant can upload the training dataset 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 dataset uploaded by the tenant and performs the training operation of the image classification model based on the training dataset. The trained image classification model can be used online or downloaded by tenants (for example, tenants can download the parameters of the trained image classification model) to complete specific image classification tasks.
[0062] For another example, the training device can also be deployed on a computing device in an edge environment (also called an edge computing device). The edge environment is an environment that is geographically close to the user's terminal computing device. 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 with 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 devices deployed in different environments collaborate to provide the function of training the 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 method 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.
[0063] The training method of the image classification model provided in the embodiment of the present application is described in detail below in conjunction with Figure 2.
[0064] S101: The training device obtains a training data set.
[0065] The training dataset is constructed for the image classification task to be implemented, and the training dataset 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 that can be used to implement the image classification task (i.e., image classification model). For example, to train a model that can be used to identify different vehicles, the training dataset 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, 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 dataset 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, dog, etc. In this example, the type of animal in each image is the category label of the image.
[0066] Optionally, the training images in the training data set may be preprocessed images, and the preprocessing includes 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.
[0067] 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 dataset can upload the training dataset to the training device by calling the interface, and the training device obtains the training dataset accordingly.
[0068] S102: The training device processes each training image in the training data set to obtain a target input vector corresponding to each training image.
[0069] 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.
[0070] 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, or it can 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.
[0071] The following describes how the training device obtains the target input vector corresponding to each training image in this step, taking any training image (assuming it is training image x) from among multiple training images as an example. Specifically, the following steps may be included:
[0072] 1. The training device divides the training image x into blocks to obtain multiple image blocks.
[0073] 2. The training device encodes each of the multiple image blocks to obtain an image block encoding sequence corresponding to the training image x.
[0074] In some embodiments, the training device may encode any image block among the multiple image blocks using 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. i =Embed pre (x i )
[0075] 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 The mapping relationship between them. It can be further concluded that the image block coding sequence is [e1,…,e n ].
[0076] 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.
[0077] 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 image block coding sequences are spliced together to obtain the original input vector corresponding to the training image x. 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-training 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-training model with the picture block coding sequence corresponding to the training image x according to the specific circumstances. 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-training 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 coding vector in the image block coding 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 can concatenate the prompt vector and the mask vector of the pre-trained model with the original input vector corresponding to the training image x using the following formula to obtain the target input vector corresponding to the training image x. vp =[H o ,p1,…,p m ,e [MASK] ]
[0082] 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 circumstances. 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 to the original input vector 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.
[0083] It should be understood that to preserve more features of the training image, the training device employs the aforementioned splicing approach, concatenating the mask vector with the training image, rather than directly masking portions of the training image using the mask vector. This allows the subsequent model training process to better capture the complete features of the training image, resulting in a more accurate image classification model.
[0084] 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.
[0085] 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.
[0086] 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. The above-mentioned 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) can shorten the distance between the predicted visual words corresponding to the training images and the category labels corresponding to the images, 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.
[0087] The following is still taking the training image x as an example to describe this step in detail. Specifically, it may include the following steps:
[0088] 1. The training device sets a category vector for the category label corresponding to the training image x.
[0089] The training device does not impose any specific restrictions on the classification vector when setting it. For example, the classification 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 classification vector can also be randomly set.
[0090] 2. The training device predicts the mask vector in the target input vector corresponding to the training image x to obtain the predicted visual word.
[0091] 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 is the masked part (i.e., the mask vector) and the other is the unmasked part (including the hint vector and the original input vector). Therefore, in this step, the training device predicts the masked vector in the target input vector corresponding to the training image x, including: the training device predicts the masked part in the target input vector corresponding to the training image x based on the unmasked part in the target input vector corresponding to the training image x.
[0092] 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.
[0093] Specifically, the training device performs a dimension transformation on the predicted visual words based on the dimension of the category vector corresponding to the category label of the training image x. The transformed predicted visual words have the same dimension as the category vector, that is, the two are in the same vector space.
[0094] In some embodiments, the training device may perform dimension transformation on the predicted visual words using the following formula.
[0095] In this formula, h [MASK] Represents the above predicted visual word, u [MASK] Represents the predicted visual word after transformation. Indicates h [MASK] The vector space where Indicates u [MASK] In one possible implementation, A is the mapping matrix used to map h [MASK] Perform dimension transformation to map it 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.
[0096] 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.
[0097] In some embodiments, the training device can 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. j =u [MASK] ·(cj ) T
[0098] 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.
[0099] 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.
[0100] 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.
[0101] 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.
[0102] In this formula, N C Indicates the number of categories, that is, the number of category labels corresponding to the training images in the training dataset 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 actual applications, the training device can also use other loss functions (for example, an 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.
[0103] 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.
[0104] 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-training model, and the pre-training model is obtained through mask prediction, that is, a 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 based on the visual word labels of the masked image blocks and the predicted visual words, the parameters of the pre-training model are continuously adjusted to obtain the pre-training 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. It is also for this reason that 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-training 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.
[0105] Furthermore, it can be seen from the above S103 that in the process of training the image classification model using the method provided in 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 important to focus 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 to the prior art method of directly mapping the predicted visual words to the category labels by adding a classification head (e.g., MLP) to achieve classification, in the process of training the image classification model using the method provided in 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. Moreover, during classification, the distance between the predicted visual words and the category labels is shortened, so the classification results have higher accuracy.
[0106] The image classification model obtained by executing the above S101-S103 can be used to perform corresponding image classification tasks. To this end, an 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.
[0107] The image classification model training method provided in the embodiments 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 software and hardware devices. When the inference device is a software device, the inference device can be a virtual machine, a container, or software with model inference capabilities.
[0108] The inference device can be deployed flexibly. For example, the inference device can be independently deployed in a cloud environment's data center. In some embodiments, the inference device can be independently deployed on a server or virtual machine in the cloud environment's data center, distributed across multiple servers or virtual machines in the cloud environment's data center, or distributed across servers and virtual machines in the cloud environment's data center. As shown in Figure 3, the inference device is deployed in the cloud environment's data center by the cloud service provider. Accordingly, the cloud service provider provides the functions of the inference device as a cloud service to tenants. Tenants can register an account on the cloud management platform, log in to the cloud management platform using that account, and purchase the cloud service on the cloud management platform (pre-paid and then settled based on final resource usage, or purchase on an annual or monthly basis). After successful purchase, the tenant can upload the image to be processed to the inference device deployed in the data center. The inference device receives the uploaded image to be processed, inputs the image to be processed into an image classification model, and returns the classification result of the image to be processed to the tenant. The tenant can further complete specific tasks based on this classification result, such as epidemic prevention and control warnings.
[0109] In other embodiments, the inference device and the training device described above can both be deployed in a cloud environment's data center. Tenants can first purchase only the cloud services corresponding to the training device on the cloud management platform to obtain an image classification model. When the image classification model is needed to classify images, the tenant can then purchase the cloud services corresponding to the inference device to complete the image classification. Alternatively, the cloud service provider can combine the training device and the inference device into a single cloud service and provide it to the tenant. In this case, after purchasing the cloud service, the tenant can complete both the image classification model training and image classification in the cloud environment's data center.
[0110] In addition to the data center of the cloud environment, the inference device can also be deployed on a computing device in an 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 inference device is distributedly deployed in different environments, the inference device can be logically divided into multiple parts, each part has different functions, and one part of the inference 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 inference devices 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 inference device, nor do they limit the deployment method of the various parts of the inference device. In actual application, it can be adaptively deployed according to specific application requirements or the computing power of the hardware equipment.
[0111] The image classification method provided in the embodiment of the present application is described in detail below with reference to FIG4 .
[0112] S201: The inference device obtains an image to be processed.
[0113] 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.
[0114] In some embodiments, the inference 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 inference device by calling the interface, and the inference device obtains the image accordingly.
[0115] S202: The inference device classifies the image to be processed based on the image classification model to obtain a classification result of the image.
[0116] 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, which includes the following parameters: parameters of the pre-trained model (for example, mask vector) and optimized prompt vector. As can be seen from the foregoing, the pre-trained model is obtained based on mask vector training. The optimized prompt vector is obtained by optimizing the target input vectors corresponding to multiple training images (see the above S103 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 the above S102 for details).
[0117] In some embodiments, the inference device classifies the image to be processed based on the image classification model, including: the inference device concatenating the mask vector and the optimized hint 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. The inference device then classifies the image to be processed based on the target input vector corresponding to the image to be processed.
[0118] In some embodiments, the inference device classifies the image to be processed based on the target input vector corresponding to the image to be processed, 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 label.
[0119] In some embodiments, the inference device maps the predicted visual words and category labels corresponding to the image to be processed into the same vector space, and classifies the image based on the mapped predicted visual words and category labels, including: the inference device performs a dimensionality transformation on 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 (for details, see step 1 in S103 above).
[0120] 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 above-mentioned image to be processed.
[0121] For simplicity, this step does not describe how the inference device concatenates the pre-trained model's mask vector and optimized hint vector with the image to be processed. It should be understood that the specific concatenation method depends on the training process of the image classification model, as detailed in S102 above. Similarly, this step does not describe how the inference device transforms the dimensions of the predicted visual words based on the dimensions of at least one category vector, nor how the similarity between the two is calculated. It should be understood that the specific implementation process depends on the training process of the image classification model, as detailed in steps 3 and 4 in S103 above.
[0122] The inference device can implement the image classification task by executing S201-S202 above. Because the image classification model used in performing the image classification task is trained through mask prediction, the image classification task is formally consistent with the upstream task. Therefore, the pre-trained knowledge learned in the upstream task can be more fully utilized in the image classification process, and accordingly, the classification results obtained have higher accuracy.
[0123] 5-9 , the image classification model trained using the method provided in the embodiments of the present application and the performance of the model in actual use are further described below using specific experimental data.
[0124] The following 10 public datasets were used for experiments: CIFAR100, CIFAR10, Flowers102, Food101, EuroSAT, SVHN, Pets, DTD, Resisc45, and PatchCame. The data in each dataset includes a training dataset and a test dataset. The training dataset in each dataset is used to train the image classification model, and the test dataset in each dataset is used to test the image classification model obtained by the training. In order to facilitate the understanding of the performance of the image classification model trained using the method provided in the embodiment of the present application, this experiment also selected the following 7 baseline methods for comparison:
[0125] 1. The pre-trained model is BEITv2. BEITv2 is fine-tuned using the training dataset to obtain an image classification model. This method is referred to as FT+BEITv2 below, where FT stands for fine-tuning.
[0126] 2. The pre-trained model is BEITv2. BEITv2 is used to extract features from the training dataset, and then class labels are fitted using logistic regression. This method is referred to as LP+BEITv2 below, where LP stands for linear probe.
[0127] 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.
[0128] 4. The pre-trained model is CLIP, which extracts features from the training dataset and then uses logistic regression to fit the class labels. This method is referred to as LP+CLIP below.
[0129] 5. The pre-trained model is CLIP. The image classification model is trained using the images and the text corresponding to the images in the training dataset. This method is referred to as TP+CLIP below, where TP stands for textual prompt.
[0130] 6. The pre-trained model is CLIP. Learnable perturbations are added to the images in the training dataset as visual cues. The CLIP is trained using the images with added visual cues and the corresponding text to obtain an image classification model. This method is referred to as VP+TP+CLIP, where VP stands for visual prompt.
[0131] 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.
[0132] First, please refer to Figure 5. As shown in Figure 5, 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. In contrast, the baseline method VPT+BEITv2 listed above requires searching for different learning strategies for different image classification tasks. Taking the two data sets Flowers102 and Pets as examples, as shown in Figure 6, (a) and (b) respectively show the corresponding loss values (shown as Loss in the figure) and accuracy (shown as Accuracy in the figure) 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 in the figure) and accuracy (shown as Accuracy in the figure) when training the model using different learning strategies (epoch and learning rate (shown as lr in the figure)) on Pets. As can be seen from Figure 6, when using the baseline method VPT+BEITv2 to train the image classification model, the optimal learning strategies corresponding to the two data sets Flowers102 and Pets are different, that is, different learning strategies need to be used to train the model on these two data sets. Therefore, compared with the above-mentioned 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 achieved.
[0133] Figure 7 shows the test results of the image classification models trained using different methods. As shown in Figure 7, 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, because 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 application at epoch 100. The following conclusions can be drawn from Figure 7:
[0134] 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 approximately 5% higher than the average accuracy of the image classification model trained using LP+BEITv2.
[0135] 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+CLIP (89.4%).
[0136] 3. On the first 9 data sets, the average accuracy of the image classification models trained using LP+CLIP, TP+CLIP, and VP+TP+CLIP 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 was lower than the average accuracy of the image classification models trained using the method provided in the embodiments of the present application (88.65%).
[0137] 4. On the above 10 datasets, regardless of epoch number 50 or 100, the average accuracy of the image classification model trained using the method provided by the embodiment of the present application is higher than the average accuracy of the image classification model trained using VPT. Moreover, on most datasets (i.e., 8 datasets), the accuracy of the image classification model trained using the method provided by the embodiment of the present application is higher than the accuracy of the image classification model trained using VPT.
[0138] 5. On the above 10 data sets, when the epoch is 50, the average accuracy of the image classification model trained using the method provided in the embodiment of the present application is 87.91. The visual 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 for predicting the test data set). When the epoch is 100, the average accuracy of the image classification model trained using VPT is 87.91. That is to say, 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.
[0139] Further, please refer to Figure 9, which shows the 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 corresponding to the image and the possible categories into the same vector space). To facilitate understanding the performance of the mapping rule provided in the embodiment of the present application, this experiment also selected the following two mapping rules for comparison:
[0140] 1. Directly map the predicted visual words to categories. This mapping rule is referred to as MLP-1 below.
[0141] 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.
[0142] On the training datasets of the above 10 datasets, the image classification model was 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 was used to test the test datasets of the above 10 datasets, and the test results shown in Figure 9 were obtained. As can be seen from Figure 9:
[0143] 1. On the 10 datasets described above, although the mean fine-tuning parameter ratio when using MLP-1 was lower than the mean fine-tuning parameter ratio when using the mapping rules provided in the embodiments of this application, the mean image classification accuracy achieved using the mapping rules provided in the embodiments of this application was 8.18% higher than that achieved using MLP-1. The fine-tuning parameter ratio is the ratio of fine-tuning parameters to all parameters in the model.
[0144] 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.
[0145] 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 the predicted visual words corresponding to the image to determine the category of the image. Therefore, in order to more accurately determine the performance of each mapping rule, this experiment uses the mapping rules provided by MLP-1, MLP-2 and 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.
[0146] The previous article describes in detail the training method of the image classification model and the method of performing image classification using the trained image classification model. Below, in conjunction with Figures 10-13, the training device for executing the above-mentioned image classification model training method and the inference device for executing the above-mentioned image classification method are further described.
[0147] Figure 10 illustrates a schematic diagram of the structure of a training device. As shown in Figure 10 , training device 100 includes an acquisition module 110, a splicing module 120, and an optimization module 130. These modules work together to implement the steps performed by the training device in the above-described method embodiment. Specifically, acquisition module 110 is used to perform S101 above; splicing module 120 is used to perform S102 above; and optimization module 130 is used to perform S103 above.
[0148] In the embodiments of the present application, the acquisition module 110, the splicing module 120, and the optimization module 130 can all be implemented via software or hardware. For example, the implementation of the optimization module 130 will be described below using 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.
[0149] As an example of a software functional unit, the optimization module 130 may include code running on a computing instance. The computing instance may include at least one of a physical host (computing device), a virtual machine, and a container. Furthermore, the 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 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 one data center or multiple geographically close data centers. Typically, a region may include multiple AZs.
[0150] Similarly, the multiple hosts / virtual machines / containers used to run the code can be distributed within the same virtual private cloud (VPC) or across multiple VPCs. Typically, a VPC is set up within a region. Cross-region communication between two VPCs within the same region, or between VPCs in different regions, requires a communication gateway within each VPC to interconnect the VPCs.
[0151] As an example of a hardware functional unit, the optimization module 130 may include at least one computing device, such as a server. Alternatively, the optimization module 130 may be implemented using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD). 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.
[0152] 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 servers, ASICs, PLDs, CPLDs, FPGAs, GALs, and other computing devices.
[0153] 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 implemented by the acquisition module 110, the splicing module 120, and the optimization module 130 can be specified as needed. The full functionality of the training device 100 is achieved by having the acquisition module 110, the splicing module 120, and the optimization module 130 respectively implement different steps in the training method for the image classification model.
[0154] Figure 11 illustrates an exemplary structural diagram of an inference device. As shown in Figure 11, inference device 200 includes an acquisition module 210 and a classification module 220. These modules work together to implement the steps performed by the inference device in the above method embodiment. Specifically, acquisition module 210 is used to perform step S201 above, and classification module 220 is used to perform step S202 above.
[0155] In the embodiments of the present application, both acquisition module 210 and classification module 220 can be implemented via software or hardware. For example, the implementation of classification module 220 will be described below using classification module 220 as an example. Similarly, the implementation of acquisition module 210 can refer to the implementation of classification module 220.
[0156] As an example of a software functional unit, the classification module 220 may include code running on a computing instance. The computing instance may include at least one of a physical host (computing device), a virtual machine, and a container. Furthermore, the 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 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 one data center or multiple geographically close data centers. Typically, a region may include multiple AZs.
[0157] Similarly, the multiple hosts / virtual machines / containers used to run the code can be distributed within the same virtual private cloud (VPC) or across multiple VPCs. Typically, a VPC is set up within a region. Cross-region communication between two VPCs within the same region, or between VPCs in different regions, requires a communication gateway within each VPC to interconnect the VPCs.
[0158] As an example of a hardware functional unit, the classification module 220 may include at least one computing device, such as a server. Alternatively, the classification module 220 may be implemented using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD). 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.
[0159] 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 servers, ASICs, PLDs, CPLDs, FPGAs, GALs, and other computing devices.
[0160] It should be noted that, in other embodiments, the acquisition module 210 can be used to perform any of the steps in S201-S202 above; and the classification module 220 can be used to perform 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. By having the acquisition module 210 and the classification module 220 respectively implement different steps in the above-mentioned image classification method, the full functionality of the inference device 200 is realized.
[0161] The present application also provides a computing device. As shown in Figure 12, computing device 300 includes a bus 310, a processor 320, a memory 330, and a communication interface 340. Processor 320, memory 330, and communication interface 340 communicate with each other via bus 310. Computing device 300 can be a server or a terminal device. It should be understood that the present application does not limit the number of processors and memories in computing device 300.
[0162] Bus 310 may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, among others. Buses can be categorized as address buses, data buses, and control buses. For ease of illustration, FIG12 shows a single line, but this does not imply a single bus or type of bus. Bus 310 may include a pathway for transmitting information between various components of computing device 300 (e.g., memory 330, processor 320, and communication interface 340).
[0163] 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).
[0164] The memory 330 may include volatile memory, such as random access memory (RAM). The memory 330 may also include non-volatile memory, such as read-only memory (ROM), flash memory, hard disk drive (HDD), or solid state drive (SSD).
[0165] The memory 330 stores executable program code. The processor 320 executes the executable program code stored in the memory 330 to implement the functions of the various modules in the aforementioned training device 100 (including the acquisition module 110, the splicing module 120, and the optimization module 130), thereby implementing the training method of the image classification model described above. Alternatively, the processor 320 executes the executable program code stored in the memory 330 to implement the functions of the various modules in the aforementioned inference device 200 (including the acquisition module 210 and the classification module 220), 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.
[0166] 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.
[0167] Embodiments of the present application also provide 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 smartphone.
[0168] As shown in FIG13 , the computing device cluster 400 includes one or more computing devices 300. The memory 330 of one or more computing devices 300 in the computing device cluster 400 may store the same instructions for executing the above-mentioned training method for the 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 above-mentioned training method for the 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 above-mentioned training method for the image classification model and / or the image classification method.
[0169] It should be noted that the memories 330 in different computing devices 300 in the computing device cluster 400 may store different instructions, each for executing part of the functions of the training device 100 and / or the inference device 200. That is, the instructions stored in the memories 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.
[0170] 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.
[0171] The present application also provides a computer program product including instructions. This computer program product can be software or a program product including instructions that can be run on a computing device or stored in any available medium. When this computer program product is run on at least one computing device, it causes the at least one computing device to execute the computation graph compilation method described above.
[0172] 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 a 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). 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.
[0173] 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 them. 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 deviate the essence of the corresponding technical solutions 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
Image classification model training method and system and image classification method and system
CN113222066A
Image classification method and device, storage medium and terminal
CN114494718A
Pre-training image text model processing method and image-text retrieval system
CN114821223A
Device and method for classification using classification model and computer readable storage medium
US20220101040A1
Cited By
Intelligent screening system and method for peony damp-heat-resistant offspring based on data analysis
CN118861901A
Local perception point cloud representation learning pre-training method and device based on attention mechanism
CN119418143A
Zero sample anomaly detection method, device and equipment based on vision and language model
CN119941644A
Visual token compression method based on word segmentation device classification and applicability analysis
CN120876890A
A Visual Token Compression Method Based on Token Segmenter Classification and Applicability Analysis
CN120876890B
