Multi-constrained balanced sample image dynamic weight iterative sampling method
By adopting a dynamic weighted iterative sampling method for sample images with multiple constraints, the problem of balancing the uniformity of target categories and the proportion of image complexity in image databases is solved, achieving efficient and stable sampling of sample sets while taking into account the handling of interference from multiple coexisting categories.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- THE FIRST RES INST OF MIN OF PUBLIC SECURITY
- Filing Date
- 2026-06-03
- Publication Date
- 2026-07-24
AI Technical Summary
Existing sample sampling methods fail to effectively meet the requirements of uniform distribution of target categories and image complexity ratio in image databases, and fail to handle sampling interference from images with multiple coexisting categories, making it difficult for the sample set to simultaneously meet multiple constraints.
A dynamic weighted iterative sampling method for sample images with multiple constraints is adopted. By labeling the category and complexity of the images, the category priority and complexity ratio are calculated, the sampling weights are dynamically adjusted, the sample set is optimized in real time, and the coexistence penalty mechanism is used to avoid sampling too many categories, thus achieving a balance of dual constraints.
It achieves a dynamic balance between the uniformity of target category distribution and the proportion of image complexity, improves the rationality and stability of the sample set, avoids the disruption of global balance by single-category sampling, and improves sampling efficiency.
Smart Images

Figure CN122454318A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of data processing technology, and specifically to a dynamic weighted iterative sampling method for sample images with multiple constraints and balance. Background Technology
[0002] In applications such as machine learning model training and image data mining, it is necessary to extract sample sets from large-scale image databases for model training or data analysis. Current image databases have the following characteristics: the number of target categories is large and the distribution of samples in each category is uneven; a single image may contain a single target of a single category, targets of multiple categories, or multiple targets of the same category; and the distribution of images of different complexities (divided by the number of target categories contained) has no fixed pattern.
[0003] Existing sampling methods have the following main drawbacks: 1. Focusing only on the uniformity of the target category distribution and ignoring the proportional requirements of image complexity results in the extracted sample set failing to meet the requirements of image scene diversity for subsequent model training; 2. Considering only the proportion of image complexity sacrifices the uniformity of class distribution, resulting in insufficient representativeness of scarce classes; 3. The sampling interference of multi-class coexisting images is not handled. When sampling a certain class, it is easy to cause an overabundance of coexisting class samples, which will disrupt the global balance. 4. The sampling process lacks a dynamic adjustment mechanism and cannot optimize the sampling strategy in real time according to the sampling progress, making it difficult for the final sample set to simultaneously meet multiple constraints.
[0004] Therefore, there is an urgent need for an intelligent sampling method that can simultaneously satisfy the uniform distribution of target categories, the image complexity ratio constraint, and the ability to handle interference from multiple coexisting categories. Summary of the Invention
[0005] To address the shortcomings of existing technologies, this invention aims to provide a dynamic weighted iterative sampling method for sample images with multiple constraints and balance.
[0006] To achieve the above objectives, the present invention adopts the following technical solution: A multi-constraint balanced dynamic weighted iterative sampling method for sample images includes the following steps: S1. Data Preprocessing and Initialization: S1.1 Image labeling: Traverse all images in the database and label each image with two types of labels, including categories and complexity. The categories label is used to record the set of categories of the targets contained in the image, and the complexity label is used to record the image complexity according to the number of categories of targets contained in the image. S1.2, Category Information Statistics: Statistically analyze relevant information for each category, including total_images_of_C i and images_of_C i total_images_of_C i Indicates that it contains category C i The total number of images, images_of_C i Indicates that it contains category C i A list of all images; S1.3, Parameter Initialization: Initialize the selected_images collection, which is used to store the IDs of the selected images; Initialize the category_selection_count dictionary, which is used to record the number of times each category is included in the selected images; Initialize the complexity_selection_count dictionary, which records the number of images corresponding to each complexity label among the selected images; Set the target proportion of the image corresponding to each complexity tag within the selected image; S2, Iterative Sampling: S2.1, Category Priority Calculation: For each category C i Calculate the current number that needs to be extracted: target i =(N / m)-category_selection_count.get(C i , 0), where m is the total number of categories, N represents the preset total number of sampled images, and target i A positive value indicates the number of samples that still need to be added to this category, `category_selection_count.get(C` i ,0) represents category C i The number of times the selected image contains information, where 0 represents the 0th iteration, which is the initial state before the iteration begins; If there is category C i corresponding target i >0, select target i The largest category will be selected as the priority category C in this round. p ; S2.2, Global Complexity Ratio and Adjustment Factor Calculation: S2.2.1, Calculate the complexity ratio current_ratio of the currently selected sample set, that is, the proportion of the images corresponding to each complexity label in the current sample set; S2.2.2, For Complexity labels IC1, IC2, IC3, calculate their corresponding adjustment factors af j , where j takes values from the complexity labels: If current_ratio[j] < Desired_Ratio[j] - 0.05, set af j > 1, and reward the images labeled as j; If current_ratio[j] > Desired_Ratio[j] + 0.05, set af j < 1, and punish the images labeled as j; Otherwise, set af j = 1.0; S2.3, Candidate Image Dynamic Weight Calculation: Candidate Pool Determination: Exclude the images already in selected_images from the set images_of_C p containing images of category C p to form the candidate image pool for this round; S2.4, For each image img in the candidate image pool for this round, calculate the dynamic weight w according to the following formula: w = base_weight × complexity_bonus × co_occurrence_penalty where base_weight is the base weight, set to 1.0; complexity_bonus is the complexity reward, which is the adjustment factor af corresponding to the complexity label of image img j ; co_occurrence_penalty represents the co-occurrence penalty, and the specific calculation process is as follows: For each co-occurring category C p other than C in img, calculate the saturation: c Calculate the saturation: saturation_C c = current_count[C c / target_total[C c ; where current_count[C c[Category C] c The total number of times the image is included in the currently selected image, target_total[C c = N / m; The coexistence penalty factor is the product of the penalty terms for all coexistence categories, as shown in the following formula: co_occurrence_penalty=∏(1 / max(1.0, saturation_ C c ))(C c ∈img.categories-{C p}); Here, img.categories represents the collection of all categories contained in the image img; S2.5 Weighted Sampling and State Update: S2.5.1. Using a weighted random sampling method, select one image (chosen_img) from the candidate image pool based on the w value of each image; S2.5.2 Add the ID of chosen_img to the selected_images collection; S2.5.3 Update Status: In category_selection_count, for all categories c contained in chosen_img, increment category_selection_count[c] by 1; and increment complexity_selection_count[chosen_img.complexity] by 1; Increasing 1 in complexity_selection_count[chosen_img.complexity] means that in the complexity_selection_count dictionary, the value corresponding to the Complexity tag of the selected image is incremented by 1. S2.6 Determine whether the current selected_images set has reached the preset total number of sampled images N. If it has, output the current selected_images set as the final sample set; otherwise, return to step S2.1.
[0007] Furthermore, in step S1.1, the complexity label includes three categories: IC1, IC2, and IC3. Images containing only one type of target are labeled as IC1, images containing two types of targets are labeled as IC2, and images containing more than two types of targets are labeled as IC3.
[0008] Furthermore, in step S1.2, Desired_Ratio[IC1], Desired_Ratio[IC2], and Desired_Ratio[IC3] represent the target proportions of the images with complexity labels IC1, IC2, and IC3 in the selected images, respectively, and Desired_Ratio[IC1] = 0.6, Desired_Ratio[IC2] = 0.3, and Desired_Ratio[IC3] = 0.1.
[0009] Furthermore, in step S1.3, the allowable floating range k of the target proportion of the image corresponding to each complexity label in the selected image is also set.
[0010] Furthermore, in step S2, during the initial iteration, the adjustment factor is not calculated for the first m / 3 iterations; that is, when executing steps S2.1-S2.5 for the first m / 3 iterations, step S2.2.2 is not performed, and af is fixed. j =1.0.
[0011] Furthermore, in step S2.1, if all targets i If all values are ≤0 and the current selected_images set has not reached the preset total number of sampled images N, then for each category, extract images of each category in equal quantities according to steps S2.2-S2.5, so that the size of the selected_images set reaches the preset total number of sampled images N.
[0012] The present invention also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the above-described method.
[0013] The present invention also provides a computer device, including a processor and a memory, wherein the memory is used to store a computer program; and the processor is used to execute the computer program to implement the above-described method.
[0014] The beneficial effects of this invention are as follows: 1. Clear priorities, taking into account dual constraints: The primary goal is to ensure the uniformity of the target category distribution, and the representativeness of each category is guaranteed by prioritizing the extraction of scarce categories; at the same time, a complexity adjustment factor is introduced to ensure that the image complexity ratio meets the preset requirements, so as to achieve a dynamic balance of dual constraints. 2. Handling multi-class coexistence interference: By using a coexistence penalty mechanism, images containing an excess of classes are avoided, preventing single-class sampling from disrupting the global balance and improving the overall rationality of the sample set; 3. Strong dynamic adaptability: During the iteration process, the scarcity and complexity ratio of categories are calculated in real time, and the sampling weights are dynamically adjusted to adapt to different data distribution scenarios, resulting in high stability of sampling results; 4. Efficiency optimization: Utilize the automatic deduplication feature of sets to avoid invalid processing of duplicate images; simplify the calculation of adjustment factors in the early stage of iteration to reduce the amount of computation and improve sampling efficiency. Attached Figure Description
[0015] Figure 1 This is a flowchart of a method according to an embodiment of the present invention. Detailed Implementation
[0016] The present invention will be further described below with reference to the accompanying drawings. It should be noted that this embodiment is based on the present technical solution and provides detailed implementation methods and specific operation processes, but the protection scope of the present invention is not limited to this embodiment.
[0017] This embodiment provides a dynamic weighted iterative sampling method for sample images with multiple constraints and balance, such as... Figure 1 As shown, it includes the following steps: S1. Data Preprocessing and Initialization: S1.1 Image Labeling: Traverse all images in the database and label each image with two types of labels, including categories and complexity. The categories label is used to record the set of target categories contained in the image, and the complexity label is used to record the image complexity according to the number of target categories contained in the image. In this embodiment, the complexity label includes three types: IC1, IC2, and IC3. Images containing only one type of target are labeled as IC1, images containing two types of targets are labeled as IC2, and images containing more than two types of targets are labeled as IC3. S1.2, Category Information Statistics: Statistically analyze relevant information for each category, including total_images_of_C i and images_of_C i total_images_of_C i Indicates that it contains category C i The total number of images, images_of_C i Indicates that it contains category C i A list of all images.
[0018] S1.3, Parameter Initialization: Initialize the selected_images collection, which is used to store the IDs of selected images, aiming to achieve automatic deduplication by leveraging the characteristics of collections; Initialize the `category_selection_count` dictionary, which records the number of times each category appears in the selected images (at least once, but may appear multiple times). For example, for category C... i The selected image A contains two categories C. i The target, therefore, image A contains category C. i The number of times is 2.
[0019] Initialize the complexity_selection_count dictionary, which records the number of images corresponding to the complexity labels IC1, IC2, and IC3 among the selected images; Set the target image complexity ratios: Desired_Ratio[IC1], Desired_Ratio[IC2], and Desired_Ratio[IC3] represent the target proportions of images with complexity labels IC1, IC2, and IC3 among the selected images, respectively. In this embodiment, Desired_Ratio[IC1] = 0.6, Desired_Ratio[IC2] = 0.3, and Desired_Ratio[IC3] = 0.1, with an allowed fluctuation range of ±5%.
[0020] S2, Iterative Sampling: S2.1, Category Priority Calculation: For each category C i Calculate the current number that needs to be extracted: target i =(N / m) - category_selection_count.get(C i , 0), where m is the total number of categories, N represents the preset total number of sampled images, and target i A positive value indicates the number of samples that still need to be added to this category, `category_selection_count.get(C` i ,0) represents category C i The number of times a target of the same category is contained in the selected images (because there may be multiple targets of the same category in a single image, so the number of categories is not equal to the number of images). 0 represents the 0th iteration, which is the initial state before the iteration begins. If there is category C i corresponding target i >0, select targeti The largest category will be selected as the priority category C in this round. p ; S2.2 Calculation of Global Complexity Ratio and Adjustment Factor: S2.2.1 Calculate the complexity ratio current_ratio of the currently selected sample set, which is the proportion of the images corresponding to the Complexity labels IC1, IC2, and IC3 in the current sample set.
[0021] S2.2.2 For Complexity labels IC1, IC2, and IC3, calculate their corresponding adjustment factors af. j j = IC1, IC2, IC3: If current_ratio[j] < Desired_Ratio[j] - 0.05, let af j A reward is given for images labeled j if the value is greater than 1 (e.g., 1.5). If current_ratio[j] > Desired_Ratio[j] + 0.05, let af j If the value is less than 1 (e.g., 0.5), a penalty is applied to the image labeled j. Otherwise set af j = 1.0; It should be noted that in the initial stage of the iteration, the adjustment factor is not calculated in the first m / 3 iterations. That is, when executing steps S2.1-S2.5 in the first m / 3 iterations, step S2.2.2 is not performed, and af is fixed. j =1.0 to reduce the amount of computation.
[0022] S2.3, Calculation of dynamic weights for candidate images: Candidate pool determination: from the pool containing category C p The collection of images_of_C p Images already in selected_images are excluded from the candidate image pool for this round. S2.4. For each image (img) in the candidate image pool of this round, calculate the dynamic weight w according to the following formula: w=base_weight×complexity_bonus×co_occurrence_penalty Where base_weight is the base weight, set to 1.0 (because all images in the candidate image pool contain class C). p (The basic priority is the same); complexity_bonus is the complexity bonus, which is the adjustment factor af corresponding to the Complexity label of the image img.j ;co_occurrence_penalty represents the coexistence penalty, and the specific calculation process is as follows: Except for C in img p Each coexisting category C outside c (i.e., the image img contains elements other than C) p Other categories besides those mentioned above), calculate saturation: saturation_C c =current_count[C c ] / target_total[C c ], Where, current_count[C c [Category C] c The total number of times the image is included in the currently selected image, target_total[C c = N / m; The coexistence penalty factor is the product of the penalty terms for all coexistence categories, as shown in the following formula: co_occurrence_penalty=∏(1 / max(1.0, saturation_ C c ))(C c ∈img.categories-{C p}).
[0023] Here, img.categories represents the collection of all categories contained in the image img; S2.5 Weighted Sampling and State Update: S2.5.1. Using a weighted random sampling method, select one image (chosen_img) from the candidate image pool based on the w value of each image; S2.5.2 Add the ID of chosen_img to the selected_images collection; S2.5.3 Update Status: In category_selection_count, for all categories c contained in chosen_img, increment category_selection_count[c] by 1; and increment complexity_selection_count[chosen_img.complexity] by 1.
[0024] Increasing 1 in complexity_selection_count[chosen_img.complexity] means that in the complexity_selection_count dictionary, the value corresponding to the Complexity tag of the selected image is incremented by 1.
[0025] S2.6 Determine whether the current selected_images set has reached the preset total number of sampled images N. If it has, output the current selected_images set as the final sample set; otherwise, return to step S2.1.
[0026] Furthermore, in step S2.1, if all targets i If all values are ≤0 and the current selected_images set has not reached the preset total number of sampled images N, then for each category, extract images of each category in equal quantities according to steps S2.2-S2.5, so that the size of the selected_images set reaches the preset total number of sampled images N.
[0027] It should be noted that in step S2, the expected average number of sampled images N / m for each category is used as a threshold. When the number of times a category is included in the selected images (i.e., the current number of samples for that category) is less than this threshold, the strategy of prioritizing the category with the largest difference is adopted. When the number of times all categories are included in the selected images exceeds the threshold, but the number of sampled images has not yet reached the expected number N, a uniform sampling strategy is adopted. The ultimate goal is to achieve a total number of sampled images that meets the expected requirements.
[0028] For those skilled in the art, various corresponding changes and modifications can be made based on the above technical solutions and concepts, and all such changes and modifications should be included within the protection scope of the claims of this invention.
Claims
1. A multi-constraint balanced dynamic weighted iterative sampling method for sample images, characterized in that, Includes the following steps: S1. Data Preprocessing and Initialization: S1.1 Image labeling: Traverse all images in the database and label each image with two types of labels, including categories and complexity. The categories label is used to record the set of categories of the targets contained in the image, and the complexity label is used to record the image complexity according to the number of categories of targets contained in the image. S1.2, Category Information Statistics: Statistically analyze relevant information for each category, including total_images_of_C i and images_of_C i total_images_of_C i Indicates that it contains category C i The total number of images, images_of_C i Indicates that it contains category C i A list of all images; S1.3, Parameter Initialization: Initialize the selected_images collection, which is used to store the IDs of the selected images; Initialize the category_selection_count dictionary, which is used to record the number of times each category is included in the selected images; Initialize the complexity_selection_count dictionary, which records the number of images corresponding to each complexity label among the selected images; Set the target proportion of the image corresponding to each complexity tag within the selected image; S2, Iterative Sampling: S2.1, Category Priority Calculation: For each category C i Calculate the current number that needs to be extracted: target i = (N / m) - category_selection_count.get(C i , 0), where m is the total number of categories, N represents the preset total number of sampled images, and target i A positive value indicates the number of samples that still need to be added to this category, `category_selection_count.get(C` i ,0) represents category C i The number of times the selected image contains information, where 0 represents the 0th iteration, which is the initial state before the iteration begins; If there is category C i corresponding target i >0, select target i The largest category will be selected as the priority category C in this round. p ; S2.2 Calculation of Global Complexity Ratio and Adjustment Factor: S2.2.1 Calculate the complexity ratio current_ratio of the currently selected sample set, which is the proportion of the image corresponding to each complexity label in the current sample set; S2.2.2 For Complexity labels IC1, IC2, and IC3, calculate their corresponding adjustment factors af. j The value of j is the complexity tag: If current_ratio[j] < Desired_Ratio[j] - 0.05, let af j >1, reward the image labeled j; If current_ratio[j] > Desired_Ratio[j] + 0.05, let af j <1, penalize the image labeled j; Otherwise set af j = 1.0; S2.3, Calculation of dynamic weights for candidate images: Candidate pool determination: from the pool containing category C p The collection of images_of_C p Images already in selected_images are excluded from the candidate image pool for this round. S2.
4. For each image (img) in the candidate image pool of this round, calculate the dynamic weight w according to the following formula: w=base_weight×complexity_bonus×co_occurrence_penalty Where base_weight is the base weight, set to 1.0; complexity_bonus is the complexity bonus, which is the adjustment factor af corresponding to the complexity label of the image img. j ;co_occurrence_penalty represents the coexistence penalty, and the specific calculation process is as follows: Except for C in img p Each coexisting category C outside c Calculate saturation: saturation_C c =current_count[C c ] / target_total[C c ]; Where, current_count[C c [Category C] c The total number of times the image is included in the currently selected image, target_total[C c ] = N / m; The coexistence penalty factor is the product of the penalty terms for all coexistence categories, as shown in the following formula: co_occurrence_penalty=∏(1 / max(1.0, saturation_C c ))(C c ∈img.categories-{C p }); Here, img.categories represents the collection of all categories contained in the image img; S2.5 Weighted Sampling and State Update: S2.5.
1. Using a weighted random sampling method, select one image (chosen_img) from the candidate image pool based on the w value of each image; S2.5.2 Add the ID of chosen_img to the selected_images collection; S2.5.3 Update Status: In category_selection_count, for all categories c contained in chosen_img, increment category_selection_count[c] by 1; and increment complexity_selection_count[chosen_img.complexity] by 1; Increasing 1 in complexity_selection_count[chosen_img.complexity] means that in the complexity_selection_count dictionary, the value corresponding to the Complexity tag of the selected image is incremented by 1. S2.6 Determine whether the current selected_images set has reached the preset total number of sampled images N. If it has, output the current selected_images set as the final sample set; otherwise, return to step S2.
1.
2. The method according to claim 1, characterized in that, In step S1.1, the complexity label includes three categories: IC1, IC2, and IC3. Images containing only one type of target are labeled as IC1, images containing two types of targets are labeled as IC2, and images containing more than two types of targets are labeled as IC3.
3. The method according to claim 2, characterized in that, In step S1.2, Desired_Ratio[IC1], Desired_Ratio[IC2], and Desired_Ratio[IC3] represent the target proportions of the images with complexity labels IC1, IC2, and IC3 in the selected images, respectively, and Desired_Ratio[IC1] = 0.6, Desired_Ratio[IC2] = 0.3, and Desired_Ratio[IC3] = 0.
1.
4. The method according to claim 1 or 3, characterized in that, In step S1.3, the allowable floating range k of the target proportion of the image corresponding to each complexity label in the selected image is also set.
5. The method according to claim 1, characterized in that, In step S2, during the initial iteration, the adjustment factor is not calculated for the first m / 3 iterations. That is, when executing steps S2.1-S2.5 for the first m / 3 iterations, step S2.2.2 is not performed, and af is fixed. j =1.
0.
6. The method according to claim 1, characterized in that, In step S2.1, if all targets i If all values are ≤0 and the current selected_images set has not reached the preset total number of sampled images N, then for each category, extract images of each category in equal quantities according to steps S2.2-S2.5, so that the size of the selected_images set reaches the preset total number of sampled images N.
7. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the method described in any one of claims 1-6.
8. A computer device, characterized in that, It includes a processor and a memory, the memory being used to store a computer program; the processor being used to execute the computer program to implement the method of any one of claims 1-6.