QNNRepair: Quantized Neural Network Repair
We present QNNRepair, the first method in the literature for repairing
quantized neural networks (QNNs). QNNRepair aims to improve the accuracy of a
neural network model after quantization. It accepts the full-precision and
weight-quantized neural networks and a repair dataset of passing and failing
tests. At first, QNNRepair applies a software fault localization method to
identify the neurons that cause performance degradation during neural network
quantization. Then, it formulates the repair problem into a linear programming
problem of solving neuron weights parameters, which corrects the QNN's
performance on failing tests while not compromising its performance on passing
tests. We evaluate QNNRepair with widely used neural network architectures such
as MobileNetV2, ResNet, and VGGNet on popular datasets, including
high-resolution images. We also compare QNNRepair with the state-of-the-art
data-free quantization method SQuant. According to the experiment results, we
conclude that QNNRepair is effective in improving the quantized model's
performance in most cases. Its repaired models have 24% higher accuracy than
SQuant's in the independent validation set, especially for the ImageNet
dataset.
Xidan Song, Youcheng Sun, Mustafa A. Mustafa, Lucas C. Cordeiro
# QNNRepair: Quantized Neural Network Repair

###### Abstract

We present QNNRepair, the first method in the literature for repairing quantized neural networks (QNNs). QNNRepair aims to improve the accuracy of a neural network model after quantization. It accepts the full-precision and weight-quantized neural networks, together with a repair dataset of passing and failing tests. At first, QNNRepair applies a software fault localization method to identify the neurons that cause performance degradation during neural network quantization. Then, it formulates the repair problem into a linear programming problem of solving neuron weights parameters, which corrects the QNN's performance on failing tests while not compromising its performance on passing tests. We evaluate QNNRepair with widely used neural network architectures such as MobileNetV2, ResNet, and VGGNet on popular datasets, including high-resolution images. We also compare QNNRepair with the state-of-the-art data-free quantization method SQuant [20]. According to the experiment results, we conclude that QNNRepair is effective in improving the quantized model's performance in most cases. Its repaired models have 24% higher accuracy than SQuant's in the independent validation set especially for the ImageNet dataset.

Keywords:neural network repair, quantization, fault localization, constraints solving

## 1 Introduction

Nowadays, neural networks are often used in safety-critical applications, such as autonomous driving, medical diagnosis, and aerospace systems [47]. In such applications, often quantized (instead of full precision) neural network models are deployed due to the limited computational and memory resources of embedded devices [21]. Since the consequences of a malfunction/error in such applications can be catastrophic, it is crucial to ensure that the network behaves correctly and reliably [36].

Quantized neural networks [21] use low-precision data types, such as 8-bit integers, to represent the weights and activations of the network. While this reduces the memory and computation requirements of the network, it can also
lead to a loss of accuracy and the introduction of errors in the network's output. Therefore, it is important to verify that the quantization process has not introduced any significant errors that could affect the safety or reliability of the network.

To limit the inaccuracy in a specific range, various neural network model verification methods [13][28][27][42][23] have been proposed. Neural network verification [24, 34, 37] aims to provide formal guarantees about the behavior of a neural network, ensuring that it meets specific safety and performance requirements under all possible input conditions. They set constraints and properties of the network input and output to check whether the model satisfies the safety properties. However, neural network verification can be computationally expensive, especially for large, deep networks with millions of parameters. This can make it challenging to scale the verification process to more complex models. While the majority of the neural network verification work is on full precision models, many verification techniques focus on quantized models as well [23, 46, 17, 4].

Other researchers improve the performance and robustness of the trained neural network models by repair [45][18][40][39][6]. These methods can be divided into three categories: retraining/refining, direct weight modification, and attaching repairing units. There are also quantized aware training (QAT) techniques [30][19][7], a method to train neural networks with lower precision weights and activations, typically INT8 format. QAT emulates the effects of quantization during the training process. QAT requires additional steps, such as quantization-aware back-propagation and quantization-aware weight initialization, making the training process more complex and time-consuming. Quantized-aware training methods require datasets for retraining, which consume a lot of time and storage. However, for Data-free quantization like SQuant [20], which does not require datasets, the accuracy after quantization is relatively low.

In QNNRepair, we use the well-established software fault localization methods to identify suspicious neurons in a quantized model corresponding to the performance degradation after quantization. We then correct these most suspicious neurons' behavior by linear programming, in which the constraints are encoded by observing the difference between the quantized model and the original model when inputs are the same. The main contributions of this paper are three-fold:

* a new method for repairing QNNs. It converts quantized neural network repair into an LP (linear programming) problem. QNNRepair features direct weight modification and does not require the training dataset.
* Squant [20], and demonstrate that QNNRepair can achieve higher accuracy than Squant after repair. We also evaluate QNNRepair on multiple widely used neural network architectures to demonstrate its effectiveness.
* We have made QNNRepair and its benchmark publicly available at: [https://github.com/HymnOfLight/QNNRepair](https://github.com/HymnOfLight/QNNRepair)
Preliminaries

### Statistical Fault Localization

Statistical fault localization techniques (SFL) [31] have been widely used in software testing to aid in locating the causes of failures of programs. During the execution of each test case, data is collected indicating the executed statements. Additionally, each test case is classified as passed or failed.

This technique uses information about the program's execution traces and associated outcomes (pass/fail) to identify suspicious program statements. It calculates four suspiciousness scores for each statement based on the correlation between its execution and the observed failures. We use the notation \(C^{\mathrm{af}},C^{\mathrm{nf}},C^{\mathrm{as}},C^{\mathrm{ns}}\). The first part of the superscript indicates whether the statement was executed/"activated" (a) or not (n), and the second indicates whether the test is a passing/successful (s) or failing (f) one. For example, \(C^{\mathrm{as}}\) is the number of successful tests that execute a statement \(C\). Statements with higher suspiciousness scores are more likely to contain faults. There are many possible metrics that have been proposed in the literature. We use Tarantula [26], Ochiai [2], DStar [43], Jaccard [3], Ample [8], Euclid [15] and Wong3 [44], which are widely used and accepted in the application of Statistical fault localization, in our ranking procedure. We discuss the definition and application in our method of these metrics in Section 3.1.

### Neural Network and Quantization

A neural network consists of an input layer, an output layer, and one or more intermediate layers called hidden layers. Each layer is a collection of nodes, called neurons. Each neuron is connected to other neurons by one or more directed edges [14].

Let \(f:\mathcal{I}\rightarrow\mathcal{O}\) be the neural network \(N\) with \(m\) layers. In this paper, we focus on a neural network for image classification. For a given input \(\mathrm{x}\in\mathcal{I},f(x)\in\mathcal{O}\) calculates the output of the DNN, which is the classification label of the input image. Specifically, we have

\[f(x)=f_{N}\left(\ldots f_{2}\left(f_{1}\left(x;W_{1},b_{1}\right);W_{2},b_{2} \right)\ldots;W_{N},b_{N}\right) \tag{1}\]

In this equation, \(W_{i}\) and \(b_{i}\) for \(i=1,2,\ldots,N\) represent the weights and bias of the model, which are trainable parameters. \(f_{i}\left(z_{i-1};W_{i-1},b_{i-1}\right)\) is the layer function that maps the output of layer \((i-1)\), i.e., \(z_{i-1}\), to the input layer \(i\).

QuantizationAs one of the general neural network model optimization methods, model quantization can reduce the size and model inference time of DNN models and their application to most models and different hardware devices. Quantization is the process of approximating continuous values of a signal to a finite number of discrete values. It can be understood as a method of information compression. When considering this concept in the context of computer systems, it is generally referred to as "low bits." In the following formula, \(r\) is the true
floating point value, \(q\) is the quantized fixed point value, \(Z\) is the quantized fixed point value corresponding to the 0 floating point value, and \(S\) is the smallest scale that can be represented after quantization of the fixed point. The formula for quantization from floating point to fixed point is as follows:

\[\begin{array}{c}r=S(q-Z)\\ q=\mathrm{round}\left(\frac{r}{S}+Z\right)\end{array} \tag{2}\]

Currently, Google's TensorFlow Lite [9] and NVIDIA's TensorRT [41] support the INT8 engine framework.

## 3 QNNRepair Methodology

The overall workflow of QNNRepair is illustrated in Figure 1. It takes two neural networks, a floating-point model and its quantized version for repair, as inputs. There is also a repair dataset of successful (passing) and failing tests, signifying whether the two models would produce the same classification outcome when given the same test input.

Figure 1: The QNNRepair Architecture.
The passing/failing tests are used by QNNRepair to evaluate each neuron's importance and localize these neurons to repair for improving the quantized model's performance (Section 3.1). In QNNRepair, the neural network repair problem is encoded into a linear programming problem for solving the corrected neuron weights (Section 3.2). It then replaces the weights with corrected weights, after which QNNRepair evaluates the performance similarity between the floating model and the quantized model on an independent validation set (Section 3.3). If the quantized model's performance is good enough w.r.t. the floating point one after repair, the model is ready for deployment. Otherwise, QNNRepair continues by selecting other parameters to repair. More detailed information is presented in Algorithm 1 (Section 3.4).

### Ranking the Importance of the Neurons

QNNRepair starts with evaluating the importance of the neurons in the neural network for causing the output difference between the quantized model and the floating point one. When conducting an inference procedure on an image, the intermediate layer in the model has a series of outputs as the inputs for the next layer. The outputs go through activation functions, and we assume it is a ReLU function. For the output, if it is positive, we place it as one. If not, we place it as zero, naming it the activation output. Let \(f_{i}\) and \(q_{i}\) represent the activation output of a single neuron in full-precision and quantized models separately. If there is a testing image that makes \((f_{i},q_{i})\) not equal, we consider the neuron as "activated", and we set \(v_{mn}=1\), otherwise \(v_{mn}=0\). Then we define the activation function matrix to assemble the activation status of all neurons for the floating-point model:

\(\begin{pmatrix}f_{11}&\cdots&f_{1n}\\ \vdots&\ddots&\vdots\\ f_{m1}&\cdots&f_{mn}\end{pmatrix}=f_{i}\) and \(q_{i}\) for the quantized model.

We define the activation differential matrix to evaluate the activation difference between the floating point and the quantized model. Given an input image \(i\), we calculate \(\operatorname{diff}_{i}=f_{i}-q_{i}\) between the two models. We form a large matrix of these \(diff_{i}\) regarding the image \(i\). The element in this matrix should be 0 or 1, representing whether the floating and quantized neural networks' activation status is the same.

We borrow the concepts from traditional software engineering, just replacing the statements in traditional software with neurons in neural network models. We define the passing tests as the images in the repair set that the floating-point and quantized model have the same classification output, and failing tests as their classification results are different. For a set of repair images, we define \(<C_{n}^{\text{af}},C_{n}^{\text{nf}},C_{n}^{\text{as}},C_{n}^{\text{ns}}>\) as following:

* \(C_{n}^{\text{af}}\) is the number of "activated" neurons for failing tests.
* \(C_{n}^{\text{nf}}\) is the number of "not activated" neurons for failing tests.
* \(C_{n}^{\text{as}}\) is the number of "activated" neurons for passing tests.
* \(C_{n}^{\rm ns}\) is the number of "not activated" neurons for passing tests.

We borrow the concepts from traditional software fault localization: Tarantula [26], Ochiai [2], DStar [43], Jaccard [3], Ample [8], Euclid [15] and Wong3 [44] and defined the indicators of neuronal suspicion in Table 1. Note that in DStar, * represents the \(n\) square of \(C_{n}^{af}\).

We then rank the quantitative metrics of these neurons from largest to smallest based on certain weights, with higher metrics indicating more suspicious neurons and the ones we needed to target for repair.

### Constraints-solving based Repairing

After the neuron importance evaluation, for each layer, we obtain a vector of neuron importance. We rank this importance vector. The neuron with the highest importance is our target for repair and as it could have the greatest impact on the corrected error outcome.

The optimization problem for a single neuron can be described as follows:

\[\begin{split}\text{Minimize}:&\ M\\ \text{Subject to}:&\ M\geq 0;(\delta_{1},\delta_{2}, \ldots,\delta_{n})\in[-M,M]\\ &\forall x_{i}\ \text{in TestSet}\ X\text{:}\ \sum_{i=1}^{m}w_{i}x_{i}<0\ \ \text{and}\ \ \sum_{i=1}^{m}(w_{i}+\delta_{i})x_{i}>0\end{split} \tag{3}\]

In the formula, \(m\) represents the number of neurons connected to the previous layer of the selected neuron, and we number them from \(1\) to \(m\). We add incremental \(\delta\) to the weights to indicate the weights that need to be modified, all the way to \(m\). We assume that in the full-precision neural network, this neuron's activation function gives the result \(1\), and the quantized gives \(0\). The corrected neuron in the quantized model result needs to be greater than \(0\) for the output of the activation function to be \(1\). In this case, we make the distance of the repaired quantized neural network as close as possible to that of the original quantized neural network.

\begin{table}
\begin{tabular}{l l l l} \hline \hline Tarantula: & \(\frac{C_{n}^{af}/(C_{n}^{af}+C_{n}^{\rm nf})}{C_{n}^{\rm af}/(C_{n}^{af}+C_{n}^ {\rm nf})+C_{n}^{\rm ns}/(C_{n}^{\rm ns}+C_{n}^{\rm ns})}\) & Euclid: & \(\sqrt{C_{n}^{\rm af}+C_{n}^{\rm ns}}\) \\ Ochiai: & \(\frac{C_{n}^{af}}{\sqrt{(C_{n}^{af}+C_{n}^{\rm ns})}(C_{n}^{af}+C_{n}^{\rm nf })}\) & DStar: & \(\frac{C_{n}^{af}}{C_{n}^{\rm ns}+C_{n}^{\rm nf}}\) \\ Ample: & \(\left|\frac{C_{n}^{af}}{C_{n}^{af}+C_{n}^{\rm nf}}-\frac{C_{n}^{\rm ns}}{C_{n} ^{\rm ns}+C_{n}^{\rm ns}}\right|\) & Jaccard: & \(\frac{C_{n}^{af}}{C_{n}^{af}+C_{n}^{\rm nf}+C_{n}^{\rm ns}}\) \\ Wong3: & \(C_{n}^{\rm af}-h\ \ h=\left\{\begin{array}{ll}C_{n}^{as}&\ \
The inputs to our algorithm are a quantized neural network \(Q\) that needs to be repaired, a set of data sets \(X\) for testing, and the full-precision neuron network model \(F\) to be repaired. We use Gurobi [32] as the constraint solver to solve the constraint and then replace the original weights with the result obtained as the new weights. After the repair is complete, we re-verify the fidelity accuracy to confirm that our requirements are met. If so, we consider the repaired network to be ready to use. If not, we select the second most important neuron and perform the repair process again, until the accuracy satisfies our requirements.

### Evaluating the performance similarity between the two models

The goal of quantized neural network repair is to improve the model's accuracy to be closer (and even better) than its floating-point version. Hence, the fidelity accuracy [48] is chosen to quantify the performance similarity between two neural network models. Let \(X\) denote the test set, \(f_{F}(x)\) denotes the classification output of the floating-point model, and \(f_{Q}(x)\) - the classification output of the quantized model. The fidelity accuracy can be defined as the following equation:

\[\text{Fidelity }=1-\text{Prob}\left\{x\in X\mid f_{F}(x)\neq f_{Q}(x)\right\} \tag{4}\]

In the equation, \(Prob\) indicates the probability that a floating-point model and a quantized model produce different outputs for the same input image.

### QNNRepair Algorithm

Our repair method is formulated in Algorithm 1. The input to our algorithm is the full-precision model \(F\), the quantized model \(Q\), the repair set \(X\), the validation set \(V\), and the number of neurons that need to be repaired \(N\) (Line 1). Firstly, we initialize arrays to store the activation states of the floating and quantized model, the values of the neuron importance, and four arrays \(C^{as}[]\), \(C^{af}[]\), \(C^{ns}[]\) and \(C^{nf}[]\) mentioned in Section 3.1 (Line 1). For these six arrays, we set all elements to 0.

Next, in lines 3-4, for each input in the test set \(x\in\mathcal{X}_{n}\), we perform the inference process once and obtain the neurons' activation states in the corresponding model layers, and store them in the activation states of the floating and quantized model. In line 5, if \(x[i]\) is a failing test, then we add the difference of activation status between the float model and quantized model to \(C^{as}[i]\), and vice versa. In line 11 and 12, we calculate \(C^{ns}[]\) and \(C^{nf}[]\) according to the definition in Section 3.1. We calculate the importance (here we use DStar as an example) for each neuron regarding seven importance metrics and sort them in descending order then store them in set \(I_{n}[]\) in line 14.

Then, we pick the neuron in \(I_{n}[]\), according to the neuron's weights and the corresponding inputs from the previous layer, we create and solve the LP problem we discussed in Section 3.2, get the correction of each neuron, and update their weights. When it arrives at the maxim number of neurons to repair, the loop
breaks, and we have corrected all the neurons. These are implemented at lines 17-24 in Algorithm 1.

```
Input: Floating-point model \(F\), Quantized model \(Q\), Repair set \(X\), Validation set \(V\), Number of neurons to be repaired \(N\) Output: Repaired model \(Q^{\prime}\), Repaired model's accuracy \(Acc\)
1 Initialize \(F_{a}[][],Q_{a}[][],I_{n}[]\), \(C^{us}[],C^{af}[]\), \(C^{us}[],C^{nf}[]\)
2foreach\(X\)do
3\(F_{a}[][i]=\operatorname{getActStatus}(F,x_{i})\)
4\(Q_{a}[][i]=\operatorname{getActStatus}(Q,x_{i})\)
5if\(x[i]\) is a failing testthen
6\(C^{nf}[i]=C^{nf}[i]+|F_{a}[][i]-Q_{a}[][i]|\)
7else
8\(C^{us}[i]=C^{us}[i]+|F_{a}[][i]-Q_{a}[][i]|\)
9
10 end if
11
12 end for
13\(C^{nf}[]=C^{nf}[]-C^{nf}[]\)
14\(C^{ns}[]=C^{ns}[]-C^{ns}[]\)
15\(I_{n}[]=\operatorname{DStar}(C^{ns}_{n}[],C^{ns}_{n}[],C^{ns}_{n}[])\)
16\(I_{n}[]=\operatorname{sort}(I_{n}[])\)// In descending order
17 Initialize weight of neurons \(w[][]\) and the increment \(\delta[][]\)
18foreach\(neuron[i]\in I_{n}[]\)do
19foreach\(edge[][i]\in neuron[i]\)do
20\(w[j][i]=\operatorname{getWeight}(edge[j][i])\)
21 end if
22\(\delta[][i]=\operatorname{solve}(X,w[][i])\)// Solve LP problem 3
23foreach\(edge[j][i]\in neuron[i]\)do
24\(edge[j][i]=\operatorname{setWeight}(w[j][i]+\delta[j][i])\)
25\(Q^{\prime}=\operatorname{update}(Q,edge[j][i])\)
26
27 end if
28if\(i>=N\)then
29 break
30 end if
31
32\(Acc=\operatorname{calculateAcc}(Q^{\prime},V)\)
33\(FidelityAcc=\operatorname{calculateFidelityAcc}(Q,Q^{\prime})\)
34return\(Q^{\prime},FidelityAcc\)
```

**Algorithm 1**Repair algorithm

Finally, we evaluate the fidelity accuracy of the corrected model. If it satisfies our requirements, then the model is repaired. Otherwise, try other combinations of parameters like important metrics or the maxim number of neurons need to repair and repeat the LP solving and correction process. The output for this algorithm is the repaired model with updated weight.

## 4 Experiment

### Experimental Setup

We conduct experiments on a machine with Ubuntu 18.04.6 LTS OS Intel(R) Xeon(R) Gold 5217 CPU @ 3.00GHz and two Nvidia Quadro RTX 6000 GPUs. The experiments are run with TensorFlow2 + nVidia CUDA platform. We apply QNNRepair to repair a benchmark of five quantized neural network models, including MobileNetV2 [33] on ImageNet datasets [11], and ResNet-18 [22], VGGNet [35] and two simple convolutional models trained on CIFAR-10 dataset [29]. The details of these models are given in Table 2.
We obtain the full-precision MobileNetV2 directly from Keras library, whereas we trained the VGGNet and ResNet-18 models on the CIFAR-10 dataset. We also defined and trained two smaller convolutional neural networks on CIFAR-10 for comparison: Conv3, which contains three convolutional layers, and Conv5, which contains five convolutional layers. Both models have two dense layers at the end. The quantized models are generated by using TensorFlow Lite (TFlite) [1], from the floating point models. In TFLite, the quantized convolution operation is optimized for performance, and the calculations are done in the fixed-point arithmetic domain to avoid the overhead of de-quantizing and re-quantizing tensors.

For repairs of the quantized model's performance, we use a subset of ImageNet called ImageNet-mini [25], which contains 38,668 images in 1,000 classes. The dataset is divided into the repair set and the validation set. The repair set contains 34,745 images, and the validation set contains 3,923 images. The CIFAR-10 dataset contains 60,000 images in 10 classes in total. 50,000 of them are training image, and 10,000 of them are test set. We use 1,000 images as the repair set. We use the repair set to identify suspicious neurons, generate LP constraints, and apply corrections to the identified neurons, and use the validation set to evaluate the accuracy of the models. We repeat the same experiment ten times for random neuron selection and get the average to eliminate randomness in repair methods.

### Repair Results on Baselines

In this part, we apply QNNRepair to these baseline quantized models, except for MobilenetV2, in Table 2. For each model, we perform a layer-by-layer repair of its last dense layers. We name these dense layers as dense-3 (the third last layer), dense-2 (the second last layer), and dense-1 (the output layer).

The QNNRepair result are reported in Table 3. We ranked the neurons using important metrics and chose the best results among the seven metrics. We also run randomly picked repairing as a comparison. We have chosen Top-1, Top-5, Top-10, Top-100, and all neurons as the rep

\begin{table}
\begin{tabular}{l l c c c c} \hline \hline  & & & & \multicolumn{2}{c}{Accuracy} \\ \cline{3-6} Model & Dataset & \#Layers & \#Params & \multicolumn{1}{c}{floating point} & \multicolumn{1}{c}{quantized} \\ \hline Conv3 & CIFAR-10 & 6 & 1.0M & 66.48\% & 66.20\% \\ Conv5 & CIFAR-10 & 12 & 2.6M & 72.90\% & 72.64\% \\ VGGNet & CIFAR-10 & 45 & 9.0M & 78.67\% & 78.57\% \\ ResNet-18 & CIFAR-10 & 69 & 11.2M & 79.32\% & 79.16\% \\ MobileNetV2 & ImageNet & 156 & 3.5M & 71.80\% & 65.86\% \\ \hline \hline \end{tabular}
\end{table}
Table 2: The baseline models. Parameters include the trainable and non-trainable parameters in the models; the unit is million (M). The two accuracy values are for the original floating point model and its quantized version, respectively.
models, the repair improves the accuracy of the quantized network, and in some cases, even higher than the accuracy of the floating-point model.

The dense-2 layer only contains 64 neurons in the Conv3 model. Hence we selected 30 neurons as the repair targets. In the dense-1 layer of Conv3, the effect of repairing individual neurons is not ideal, but as the number of repaired neurons gradually increases, the more correct information the Conv3 quantization model obtains from the floating-point model, so the accuracy gradually improves until it reaches 66.46% (which does not exceed the accuracy of the floating-point Conv3 neural network, but it gets very close to it: 66.48%, see Table 2 ). This is because all the repair information in the last layer comes from the original floating-point neural network. Note that because of the simple structure of the Conv3 neural network, the floating-point version of Conv3 itself is inaccurate, and the quantized and repaired neural network does not exceed the accuracy. In the dense-2 layer of Conv5, applying importance metrics to repair this layer is slightly better than random selection, only 0.01% regarding randomly selecting 5 neurons compared with using fault localization to select Top-5 neurons. Compared to the quantized model before repair, whose accuracy is 72.64%, the repairing only gets an accuracy of 72.56%, which does not improve the model's accuracy. In the dense-1 layer of Conv5, the best result is using fault localization to pick the Top-1 neuron and repair, at 72.58% accuracy, and this is not better than the quantized model before repair.

For VGGNet and ResNet-18 neural networks, the dense-1 layer is a good comparison. Both VGGNet and ResNet-18 have relatively complex network structures, and the accuracy of the original floating-point model is close to 80%. In the dense-1 layer of ResNet-18, only some of the neurons were repaired with

\begin{table}
\begin{tabular}{l c c c c c c c c c} \hline \hline  & \multicolumn{4}{c}{Random} & \multicolumn{4}{c}{Fault Localization} & - \\ \cline{2-10} \#Neurons repaired & 1 & 5 & 10 & 100 & 1 & 5 & 10 & 100 & All \\ \hline Conv3\_dense-2 & 63.43\% & 64.74\% & 38.90\% & n/a & 66.26\% & **66.36**\% & 62.35\% & n/a & 57.00\% \\ Conv3\_dense-1 & 65.23\% & 66.31\% & - & n/a & 66.10\% & 66.39\% & - & n/a & **66.46**\% \\ Conv5\_dense-2 & 72.49\% & 72.55\% & 72.52\% & 72.52\% & **72.56**\% & 72.56\% & 72.56\% & 72.56\% & 72.54\% \\ Conv5\_dense-1 & 72.51\% & 72.52\% & - & n/a & **72.58**\% & 72.56\% & - & n/a & 72.56\% \\ VGGNet\_dense-3 & 78.13\% & 78.44\% & 78.20\% & 78.38\% & **78.83**\% & 78.82\% & 78.78\% & 78.66\% & 78.60\% \\ VGGNet\_dense-2 & 78.36\% & 78.59\% & 78.44\% & 78.22\% & 78.55\% & **78.83**\% & 78.83\% & 78.83\% & 78.83\% \\ VGGNet\_dense-1 & 78.94\% & 67.75\% & - & n/a & **79.29**\% & 69.04\% & - & n/a & 74.49\% \\ ResNet\_dense\_1 & 78.90\% & 78.92\% & - & n/a & 79.08\% & **79.20**\% & - & n/a & 78.17\% \\ \hline \hline \end{tabular}
\end{table}
Table 3: QNNRepair results on CIFAR-10 models. The best repair outcome for each model, w.r.t. the dense layer in that row, is in **bold**. We further highlight the best result in _blue_ if the repair result is even better than the floating point model and in red if the repair result is worse than the original quantized model. Random means that we randomly select neurons at the corresponding dense layer for the repair, whereas Fault Localization refers to the selection of neurons based on important metrics in QNNRepair. In All cases, all neurons in that layer are used for repair. ’n/a’ happens when the number of neurons in the repair is less than 100, and ’-’ is for repairing the last dense layer of 10 neurons, and the result is the same as the All case.
accuracy close to their original quantized version, but all of them did not exceed the exact value of the floating-point neural network after the repair. However, unlike ResNet-18, correcting a single neuron randomly in the dense-1 layer of VGGNet make it more accurate than the quantized version of VGGNet. Using the importance metric and correcting a single neuron make the accuracy even higher than the floating-point version of VGGNet. However, repairing dense-1 of VGGNet was unsatisfactory, especially when 5 neurons are selected for repair; it suffered a significant loss of accuracy, even below 70%, which was regained if all ten neurons in the last layer were repaired. In the dense-2 layer of VGGNet, the overall accuracy is higher than 78%. When the importance metric is applied, the accuracy reaches 78.83%, noting that this accuracy is also achieved if all neurons in this layer are repaired. For the dense-3 layer of VGGNet, repairing 5 or 10 neurons using importance metrics will achieve the highest accuracy at 78.83%, the same as repairing the dense-2 layer.

ImageNetWe also conducted repair on the last layer for MobileNetV2 trained on the ImageNet dataset of high-resolution images. Using Euclid as the importance metric and picking 10 neurons as the correct targets achieve the best results, at 70.77%, improving the accuracy the quantized model.

### Comparison with Data-free Quantization

We tested SQuant [20], a fast and accurate data-free quantization framework for convolutional neural networks, employing the constrained absolute sum of error (CASE) of weights as the rounding metric. We tested SQuant two quantized models, the same as our approach: MobileNetV2 trained on ImageNet and ResNet-18 on CIFAR-10. We made some modifications to the original code to support MobileNetV2, which is not reported in their experiments.

\begin{table}
\begin{tabular}{l c c c c c} \hline \hline  & \multicolumn{2}{c}{Random} & \multicolumn{2}{c}{Fault Localization} & - \\ \cline{2-5} \#Neurons repaired & 10 & 100 & 10 & 100 & All \\ \hline MobileNetV2.dense-1 & 70.75\% & 70.46\% & **70.77**\% & 70.00\% & 68.98\% \\ \hline \hline \end{tabular}
\end{table}
Table 4: QNNRepair results on ImageNet model.

\begin{table}
\begin{tabular}{l c c c c} \hline \hline  & \multicolumn{2}{c}{MobileNetV2} & \multicolumn{2}{c}{ResNet-18} \\ \cline{2-5}  & Accuracy & Time & Accuracy & Time \\ \hline SQuant [20] & 46.09\% & 1635.37ms & 70.70\% & 708.16ms \\ QNNRepair & **70.77\%** & \(\sim\)15h & **79.20\%** & \(\sim\)9h \\ \hline \hline \end{tabular}
\end{table}
Table 5: QNNRepair vs SQuant
In contrast, to complete data-free quantization, our constraint solver-based quantization does not require a complete dataset and but only some input images for repair. Despite taking much more time than SQuant because it uses Gurobi and a constrained solution approach, MobileNetV2 - a complex model trained on ImageNet - QNNRepair achieves much higher accuracy.

### Repair Efficiency

The constraints-solving part contributes to the major computation cost in QNNRepair. For other operations, such as importance evaluation, modification of weights, model formatting, etc., it takes only a few minutes to complete. Thereby, Table 6 measures the runtime cost when using the Gurobi to solve the values of the new weights for a neuron for our experiments on the VGGNet model. It is shown in Table 6 that 75% of the solutions were completed within 5 minutes, and less than 9% of the neurons could not be solved, resulting in a total solution time of 9 hours for a layer of 512 neurons.

### Comparison Between Fault Localization Metrics in QNNRepair

We let the model and the layer stay the same. We use MobileNetV2 and the last layer as our target. We compare seven representative important metrics mentioned in Section 4.5. In these experiments, we used 1000, 500, 100, and 10 jpeg images as the repair sets to assess the performance of different importance assessment methods.

Firstly we rank the neurons in the last layer using seven different representative important metrics, which are Tarantula [26], Ochiai [2], DStar [43], Jaccard [3], Ample [8], Euclid [15] and Wong3 [44]. As shown in Figure 2, for the last fully connected layer of MobileNetV2, the important neurons are mainly concentrated at the two ends, those neurons with the first and last numbers. The evaluation metrics results are relatively similar for different neurons.

We selected the 100 neurons (for Conv3 it is 30 neurons) with the highest importance and could be solved by linear programming solvers according to different importance measures. The deltas are obtained according to Equation 3, and we apply the deltas to the quantized model. After that, we use the validation sets from ImageNet, which contains 50,000 jpeg images, to test the MobileNetV2 model. We also use the validation sets from CIFAR-10, which contains 10000 png

\begin{table}
\begin{tabular}{l c c c c c} \hline \hline Duration & \(<\)=5mins & 5-10mins & 10-30mins & 30mins-1h & No solution \\ \hline Percentage & 75\% & 8.98\% & 5.27\% & 1.76\% & 8.98\% \\ \hline \hline \end{tabular}
\end{table}
Table 6: The Gurobi solving time for constraints of each neuron in the dense-2 layer of the VGGNet model. There are 512 neurons in total.
image files, to test VGGNet and Conv5 after the repair. As a comparison, we also randomly picked 100 neurons to apply to repair and tested their accuracy. We give the results of the top 100 important neurons after selection and repair, as shown in Table 7.

We pick Tarantula, the best result of using ten images to repair the MobileNetV2 last layer, and plot the scatter plots based on the importance distribution of the different neurons. We rank the importance of those neurons and draw line plots as illustrated in Figure 2.

The figures give scatter plots of neuron importance and ranked line plots for the last dense layer of MobileNetV2. The horizontal coordinates of these plots are the serial numbers of the neurons. For the last layer in the MobileNetV2 model, few neurons have the highest importance. More than 300 neurons had an importance measurement of 0, and another large proportion had an importance of 0.5 or less. Based on the ranking of the importance of neurons, all the evaluation metrics except Tarantula and Euclid considered 108, 984, 612, 972 to be the four most important neurons in this layer, and among the 5th-10th most important neurons, 550, 974, 816, 795 and 702, just in a different order. This is reflected in the importance distribution graphs as spikes at the ends and as spikes at the ends of the graphs. Hence Ochiai, Dstar, Jaccard, Ample, and Wong3 have similar performance regarding the accuracy evaluation, and Euclid and Tarantula achieve better accuracy on ImageNet validation sets. Table 7 shows that the Euclid importance assessment method is highly effective, achieving relatively good results from restoration with 500 images to restoration with ten images and achieving only weaker accuracy than the Tarantula method in a restoration scenario with 1000 images. Also, a random selection of neurons can achieve good restoration results, especially when we select 100 images as restoration images. Only the random selection method has a validation accuracy higher than 70%.

For VGGNet model, for the same reason as MobileNetV2, the Tarantula, Ochiai, DStar, Jaccard, Euclid, and Wong3 give the same results when selecting 100 top important neurons to repair. As a comparison, the accuracy of random selection in dense-2 layer and dense-3 has a slight drop, at 78.38% and 78.22%. For Conv3 model, the seven importance metrics give the same results, and randomly select 30 neurons suffered a great accuracy loss, at 32.42%. But compared

\begin{table}
\begin{tabular}{l l l l l l l l l l} \hline \hline Model+Repair Layer & \#Images & Tarantula & Ochiai & DStar & Jaccard & Ample & Euclid & Wong3 & Random \\ \hline MobileNetV2\_dense-1 & 1000 & 70.61\% & 69.76\% & 69.73\% & 69.73\% & 69.72\% & **70.70\%** & 69.73\% & 69.56\% \\  & 500 & 68.99\% & 69.01\% & 69.05\% & 69.05\% & 68.99\% & **69.46\%** & 69.06\% & 69.00\% \\  & 100 & 69.50\% & 69.42\% & 69.46\% & 69.46\% & 69.53\% & **69.98\%** & 69.46\% & 70.12\% \\  & 10 & **70.62**\% & 70.15\% & 70.12\% & 70.12\% & 70.17\% & 70.73\% & 70.12\% & 70.18\% \\ \hline VGGNet\_dense-3 & 1000 & 78.64\% & 78.64\% & 78.64\% & 78.64\% & 78.65\% & **78.66\%** & **78.66**\% & 78.22\% \\ VGGNet\_dense-2 & 1000 & 78.83\% & 78.83\% & 78.83\% & 78.83\% & 78.83\% & 78.83\% & 78.83\% & **78.38**\% \\ Conv3\_dense & 1000 & **59.50**\% & **59.50**\% & **59.50**\% & **59.50**\% & 59.27\% & 59.27\% & 59.27\% & 32.42\% \\ \hline \hline \end{tabular}
\end{table}
Table 7: The results regarding importance metrics, including 7 fault localization metrics and 1 random baseline. The number of images indicates how many inputs are in the repair set.
to the results in Table 3, repairing 30 top neurons also suffered accuracy drops. For the dense layer of conv3, the best repair is still to select one neuron for repair based on Tarantula sorting at 66.10%, and if random selection is taken into account, then selecting five neurons for repair would give the best result at 64.74%.

We also conducted a side-by-side comparison of the number of images required for the repair on MobileNetV2. It shows that the best results are obtained using 1000 images for repair and 10 images for the repair, but given the amount of time required to generate constraints for the repair using 1000 images and to solve the constraints using Gurobi, we recommend using a smaller set of repair images for the model.

Euclid demonstrates that it has the highest accuracy most of the time, and repairing with importance evaluation is more accurate than repairing randomly selected neurons.

### Limitations

For larger models, it takes a longer time to solve the linear programming problem. Also, selecting more repairing images for correction will have a greater likelihood of Gurobi being unable to solve the linear programming problem, reflected in the limitation of improving accuracy.

## 5 Related Work

### Neural Network Verification

The first applicable methods supporting the non-linear activation function for neural network verification can be traced back to 2017, R Ehlers et al. [13] proposed the first practicable neural network verification method based on SAT

Figure 2: Importance distribution regarding certain importance metrics on MobileNetV2.
solver (solve the Boolean satisfiability problem) [12]. They present an approach to verify neural networks with piece-wise linear activation functions. Guy Katz et al. [28] presented Marabou, an SMT(Satisfiability modulo theories) [10]-based tool that can answer queries about a network's properties by transforming these queries into constraint satisfaction problems. However, implementing SMT-based neural network verification tools is limited due to the search space and the scale of a large neural network model, which usually contains millions of parameters [16]. The SMT-based neural network verification has also been proved as NP-complete problem [27]. Shiqi Wang et al. develop \(\beta\)-CROWN [42], a new bound propagation-based method that can fully encode neuron splits via optimizable parameters \(\beta\) constructed from either primal or dual space. Their algorithm is empowered by the \(\alpha\),\(\beta\)-CROWN (alpha-beta-CROWN) verifier, the winning tool in VNN-COMP 2021 [5]. There are also some quantized neural network verification methods. TA Henzinger et al. [23] proposed a scalable quantized neural network verification method based on abstract interpretation. But due to the search-space explosion and it has been proved that SMT-based quantized neural network verification is a PSPACE-hard problem [23].

### Neural Network Repair

Many researchers have proposed their full-precision neural network repairing techniques. These can be divided into three categories: Retraining, direct weight modification, and attaching repairing units.

In the first category of repair methods, the idea is to retrain or fine-tune the model for the corrected output with the identified misclassified input. DeepRepair [45] implements transfer-based data augmentation to enlarge the training dataset before fine-tuning the models. The second category use solvers to get the corrected weights and modify the weight in the trained model directly. These types of methods, including [18] and [40], used SMT solvers for solving the weight modification needed at the output layer for the neural network to meet specific requirements without any retraining. The third category of methods repairs the models by introducing more weight parameters or repair units to facilitate more efficient repair. PRDNN [39] introduces a new DNN architecture that enables efficient and effective repair, while DeepCorrect [6] corrects the worst distortion-affected filter activations by appending correction units. AIRepair [38] aims to integrate multiple existing repair techniques into the same platform. However, these methods only support the full-precision models and cannot apply to quantized models.

### Quantized Aware Training

Some researchers use quantized-aware training to improve the performance of the quantized models. Yuhang Li et al. proposed a post-training quantization framework by analyzing the second-order error called BRECQ(Block Reconstruction Quantization) [30]. Ruihao Gong et al. proposed Differentiable Soft Quantization (DSQ) [19] to bridge the gap between the full-precision and low-bit
networks. It can automatically evolve during training to gradually approximate the standard quantization. J Choi et al. [7]proposed a novel quantization scheme PACT(PArameterized Clipping acTivation) for activations during training - that enables neural networks to work well with ultra-low precision weights and activations without any significant accuracy degradation. However, these methods require retraining and the whole dataset, which will consume lots of computing power and time to improve tiny accuracy in actual practices. In addition, there is a method called Data-free quantization, which quantizes the neural network model without any datasets. Cong Guo et al. proposed SQuant [20], which can quantize networks on inference-only devices with low computation and memory requirements.

## 6 Conclusion

In this paper, we presented QNNRepair, a novel method for repairing quantized neural networks. Our method is inspired by traditional software statistical fault localization. We evaluated the importance of the neural network models and used Gurobi to get the correction for these neurons. According to the experiment results, after correcting the model, accuracy increased compared with the quantized model. We also compared our method with state-of-the-art techniques; the experiment results show that our method can achieve much higher accuracy when repair models are trained on large datasets.

## Acknowledgements

This work is funded by the EPSRC grant EP/T026995/1 _"EnnCore: End-to-End Conceptual Guarding of Neural Architectures"_. The Dame Kathleen Ollerenshaw Fellowship of The University of Manchester supports M. A. Mustafa.
