Deep Neural Network Test Case Generation Method for Model State Difference
By generating feature heat maps and differential results, the use case transformation is guided, and the problem of low efficiency and quality in the deep neural network test case generation method is solved, and high-quality test cases are efficiently generated and more model defects are discovered.
Patent Information
- Application Number
- CN202210534831.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-17
- Publication Date
- 2025-07-04
- Estimated Expiration
- 2042-05-17
AI Technical Summary
The existing deep neural network test case generation methods have shortcomings in terms of operation efficiency and quality of generated test cases, making it difficult to comprehensively test the model and find potential defects.
By generating feature heat maps, clustering analysis is performed to select use case templates for new neuron states, use feature heat map differential results to guide use case transformation, generate new test cases with high coverage, and perform defect detection and coverage calculation, and update valuable use cases for iteration.
It improves the operation efficiency and quality of test case generation, can more effectively discover potential defects of the model, and improves the comprehensiveness of the test.
Smart Images

Figure CN115080383B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a method for generating test cases for deep neural network based on model state difference, belonging to the field of computer and information science and technology. Background Art
[0002] Deep neural networks have been widely used in safety-critical fields such as autonomous driving, malware detection, and face recognition. Quality problems in neural network models will lead to serious consequences. Therefore, it is necessary to comprehensively test deep neural network models before deployment to ensure the stable and secure operation of the system.
[0003] Different from traditional software system developers who manually write code to implement their internal decision-making logic, deep neural network systems follow a data-driven programming paradigm, and the completeness of their testing depends on the quantity, quality, and comprehensiveness of test cases. The current mainstream methods for generating test cases for deep neural networks are mainly divided into test case generation based on gradient optimization and test case generation based on fuzz testing.
[0004] 1. Test Case Generation Based on Gradient Optimization
[0005] The main idea of test case generation based on gradient optimization is to model the test case generation task as a joint optimization task, aiming to improve coverage and discover model defects. By selecting target neurons, perturbations are applied to existing test cases in the way of gradient descent to generate new test cases. The effect of such methods depends on the selection of target neurons, and the selection and trial-and-error process of target neurons will consume a lot of time and generate worthless test cases, with too low operating efficiency.
[0006] 2. Test Case Generation Based on Fuzz Testing
[0007] The method for generating test cases based on fuzz testing applies various changes to selected existing test cases through a certain mutation strategy, generates multiple cases in one iteration by mutation, and selects and saves the cases that are helpful to improve test coverage. Such methods lack guidance during the mutation process, it is difficult to determine effective mutation directions, and the generated test cases generally have low quality and are difficult to trigger model defects.
[0008] In summary, the existing methods for generating test cases for deep neural networks have obvious deficiencies in terms of operating efficiency and the quality of generated test cases. Therefore, the present invention proposes a method for generating test cases for deep neural network based on model state difference. Summary of the Invention
[0009] The purpose of the present invention is to overcome the problems of low operating efficiency and low quality of test cases in the method for generating test cases for deep neural networks, improve the comprehensiveness of testing, and discover more potential model defects.
[0010] The design principle of the present invention is as follows: First, generate a feature heat map to establish the connection between use case features and model states; second, perform clustering analysis on the feature heat maps of existing use cases, and select the use cases that can trigger new neuron states as templates for generating new test cases; then, use the differential results of the feature heat maps to determine the high-coverage transformation direction of use case features, and perform pixel-level and region-level transformations on the use case templates to generate new test cases; finally, use the generated test cases for defect detection and coverage calculation, save the test cases that trigger the model defect output, and update the use cases that contribute to coverage improvement to the use case set for a new round of iteration.
[0011] The technical solution of the present invention is implemented through the following steps:
[0012] Step 1: Extract the model state and update the use case attribute features.
[0013] Step 1.1: Extract the neuron output values triggered by each use case in the use case set, and calculate and generate the use case feature heat maps of each layer of neurons according to the neuron levels.
[0014] Step 1.2: Add the use case feature heat maps generated in Step 1.1 as use case attributes and save them to the use case set.
[0015] Step 2: Select use cases from the use case set as templates for generating new test cases.
[0016] Step 2.1: Perform clustering analysis on the feature heat maps of each use case, and calculate the distance and density information between use cases.
[0017] Step 2.2: Update the selection probability of each use case according to the clustering analysis results, and select use cases as templates for generating new test cases based on the probability.
[0018] Step 3: Calculate the model state differential results, transform the template use cases selected in Step 2, and generate new test cases.
[0019] Step 3.1: Differentiate the model state triggered by the selected template use case from the model state triggered by its parent use case to determine the high-coverage transformation direction.
[0020] Step 3.2: Perform pixel-level or region-level transformations on the template use cases in the high-coverage transformation direction determined in Step 3.1 to generate new test cases.
[0021] Step 4: Use the newly generated test cases for defect detection and calculate their coverage, and retain the use cases that contribute to coverage improvement for subsequent iteration
[0022] Step 4.1: Detect defects in the newly generated test cases and retain the new test cases that can trigger potential defects in the model.
[0023] Step 4.2: Calculate the coverage rate of the newly generated test cases and retain the new test cases that can improve the test coverage rate.
[0024] Beneficial effects
[0025] Compared with the test case generation method based on gradient optimization, the present invention eliminates the selection and trial - and - error process before generating effective test cases, reducing the time consumption in the test case generation process.
[0026] Compared with the existing test case generation method based on fuzz testing, the present invention uses the heatmap difference results between test cases with different coverage rates to guide the change of test cases in the direction of helping to discover model defects and improve test coverage rate, improving the quality of test cases generated in each round of iteration. Description of the drawings
[0027] Figure 1 Schematic diagram of the test case generation method for a deep neural network for model state difference. Detailed implementation manners
[0028] To better illustrate the purpose and advantages of the present invention, the implementation manners of the method of the present invention will be further described in detail below with reference to examples.
[0029] Step 1: Extract the model state and update the use - case attribute features.
[0030] Step 1.1: Extract the neuron output values triggered by each use case in the use - case set, and calculate and generate the use - case feature heatmaps of neurons at each layer according to the layer where the neurons are located. For a given test case, assume that f k (x,y) represents the output value of neuron k at the spatial position (x,y). Then, for the classification result, the input S of the output layer is shown in formula (1).
[0031]
[0032] where ω k represents the weight of neuron k for the classification result. Since the offset value has little effect on the classification result, it is set to 0 in this method.
[0033] Define H l as the use - case feature heatmap generated according to the output of the l - th layer of the model, where the value corresponding to each pixel point is shown in formula (2).
[0034]
[0035] The correlation between the current model state and the input test case features can be obtained from the heatmap, where the positive part represents that the model pays more attention to the information at the corresponding position, while the part close to 0 indicates that the model pays less attention to this part of the information.
[0036] Step 1.2, Add the test case feature heatmap generated in Step 1.1 as a test case attribute and save it to the test case set. The test case features extracted by the hidden layer closer to the output layer are more specific and are more helpful for guiding the distribution of mutant test case features. Therefore, the heatmaps H l and H l-1 generated by the last layer and the second-to-last hidden layer of the model are saved together with the test cases as the attributes of the test cases in the test case set.
[0037] Step 2, Select test cases from the test case set as templates for generating new test cases.
[0038] Step 2.1, Perform clustering analysis on the feature heatmaps of each test case to calculate the distance and density information between test cases. The local density ρ i of test case x i is defined as: the number of feature heatmaps whose distance from the feature heatmap H l (x i ) is less than the cut-off distance d c . The calculation formula is shown in Formula (3).
[0039]
[0040] In the formula, d ij is the Euclidean distance between H l (x i ) and H l (x j ); d c is the cut-off distance; χ(·) is a logical judgment function. If (·) < 0, then χ(·) = 1, otherwise, χ(·) = 0.
[0041] The feature distance δ i of a sample is defined as the shortest distance from each test case to the test cases with a local density greater than it. The calculation formula is shown in Formula (4).
[0042]
[0043] When test case x i is the point with the maximum local density, the calculation formula of δ i is shown in Formula (5).
[0044]
[0045] Step 2.2: Update the selection probability of each test case according to the clustering analysis results, and select test cases as templates for generating new test cases based on the probability. The calculation formula for the selection probability of test cases is shown in Formula (6).
[0046]
[0047] Among them, Rank ρ (x i ) and Rank d (x i ) are the rankings of the local density and characteristic distance of test case x i in the current set of test cases respectively; |Q| is the total number of test cases; t(x i ) represents the number of times the test case is selected; ε represents the decay rate of the selection probability of the test case with the number of selections. α, β, and γ are weight factors, and their value ranges are all (0, 1), and α + β + γ = 1. From the probability formula, it can be seen that test cases with low local density ρ i and large characteristic distance δ i and few selection times are more likely to be selected as templates.
[0048] Step 3: Transform the template test cases selected in Step 2 based on the model state difference results to generate new test cases.
[0049] Step 3.1: Differentiate the model state triggered by the selected template test case from the model state triggered by its parent test case to determine the high-coverage transformation direction. Assume that the selected template test case is x i , and its parent test case (i.e., the template test case used to generate x i ) is x j . In this round of iteration, differentiate H l (x i ) and H l (x j ), H l-1 (x i ) and H l-1 (x j ) respectively to obtain the difference results DH l , DH l-1 that can represent the relationship between the change in the model state and the test case characteristics when the coverage rate increases, and use this difference result to determine the high-coverage transformation direction of the test case characteristics.
[0050] DH l = H l (x i ) - H l (x j ) (7)
[0051] DH l-1 = Hl-1 (x i )-H l-1 (x j ) (8)
[0052] Step 3.2, perform pixel-level or region-level transformations on the template test cases respectively in the high-coverage transformation direction determined in Step 3.1 to generate new test cases. Pixel-level transformations include contrast transformation, brightness transformation, blur transformation, and noise points, aiming to explore the transformation routes in the high-coverage transformation direction through minute changes in pixel points. Region-level transformations include translation, scaling, shearing, and rotation, and explore other possible transformation directions that may improve the coverage rate by changing the high-heat distribution region of the differential result. During the test case generation process, perform the above transformations on the differential result in sequence. Assume that the result after one transformation is D′, then the new test case x′ i can be expressed as:
[0053] x′ i = x i + λD′ (9)
[0054] where λ is the enhancement parameter, which is randomly selected within a limited range and is used to control the strength of the transformation.
[0055] Step 4, use the newly generated test cases for defect detection and coverage rate calculation, and retain valuable test cases for subsequent iterations.
[0056] Step 4.1, perform defect detection on the newly generated test cases, and retain the new test cases that can trigger potential defects of the model. Input the newly generated test cases into the deep neural network model, and compare the data result of the model for the test cases with the test case labels to determine whether there is an incorrect output. Save and record the test cases that trigger incorrect classification of the model and the corresponding classification results.
[0057] Step 4.2, perform coverage rate calculation on the newly generated test cases, retain the new test cases that can improve the test coverage rate, extract their model state information and update it to the test case set, so as to generate new test cases in subsequent test iterations.
[0058] Test results: In the experiment, the gradient optimization method, the fuzz testing method, and the method of the present invention were respectively used to generate test cases for the same ResNet-20 model trained using the CIFAR-10 image recognition data set and evaluate the quality of the test cases. The experimental results after 5000 iterations are shown in Table 1. The present invention establishes the relationship between the test case features and the model state through the test case feature heat map, determines the mutation direction for improving the coverage rate using the heat map differential result between test cases with different coverage rates, generates test cases from pixel-level and region-level mutations respectively, and effectively improves the operation efficiency and the quality of the test cases for deep neural network test case generation.
[0059] ·Table 1 Test Experiment Results
[0060]
[0061] The specific descriptions above further elaborate on the purpose, technical solution, and beneficial effects of the invention. It should be understood that the above are only specific embodiments of the present invention and are not used to limit the protection scope of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present invention shall be included within the protection scope of the present invention.
Claims
1. A method for generating test cases for a deep neural network based on model state difference, characterized in that The method includes the following steps: Step 1: Extract the model states triggered by each use case in the use case set, and add and save the model states as use case attributes to the use case set; Step 2: Conduct a clustering analysis on the model states, and select the use cases that can trigger new neuron states as templates for generating new test cases; Step 3: Determine the high-coverage transformation direction by calculating the model state difference result, and transform the template use cases selected in Step 2 to generate new test cases; Step 4: Use the newly generated test cases for defect detection and coverage calculation, and retain the test cases that can trigger potential model defects or improve test coverage for subsequent iterations.
2. The method for generating a test case for a deep neural network based on model state difference according to claim 1, wherein: In Step 1, the use case feature heatmap of each layer of neurons is used to represent the model state, and its calculation method is as follows: Assume that f k (x, y) represents the output value of neuron k at spatial position (x, y), and ω k represents the weight of neuron k for the classification result. Define H l = ∑ k∈l ω k f k (x, y) as the use case feature heatmap generated based on the output of the l-th layer of the model.
3. The method for generating a test case of a deep neural network for model state difference according to claim 1, wherein: In step 2, the local density and characteristic distance between use cases are used as the basis for cluster analysis, and the local density ρ i = ∑ j≠i χ(d ij - d c ), where d ij is the Euclidean distance between H l (x i ) and H l (x j ), d c is the cut-off distance, χ(·) is a logical judgment function. If (·) < 0, then χ(·) = 1; otherwise, χ(·) = 0. The characteristic distance δ i is defined as the shortest distance from each use case to the use cases with a local density greater than it. When the use case x i is the point with the maximum local density, δ i is defined as the maximum distance from this use case to other use cases.
4. The method for generating a test case of a deep neural network for model state difference according to claim 1, wherein: In step 2, the probability formula for use case selection is: P(x i ) = α * (1 - Rank ρ (x i ) / |Q|) + β * (Rank d (x i ) / |Q|) + γ * (1 - t(x i ) / ε), where Rank ρ (x i ) and Rank d (x i ) are the rankings of the local density and characteristic distance of use case x i in the current use case set respectively; |Q| is the total number of use cases; t(x i ) represents the number of times the use case is selected; ε represents the decay rate of the use case selection probability with the number of selections; α, β, γ are weight factors, and their value ranges are all (0, 1), and α + β + γ = 1.
5. The method for generating a test case of a deep neural network for model state difference according to claim 1, characterized in that: In step 3, the model states triggered by the template use case are differentiated from those triggered by its parent use case to determine the high-coverage transformation direction D; during the test case generation process, pixel-level or region-level transformation is performed on D. Assuming that the result after one transformation is D′, then the template use case x i The newly generated test case x′ i Can be expressed as x′ i = x i + λD′, where λ is an enhancement parameter that is randomly valued within a finite range and is used to control the strength of the transformation.