Impact of Low-bitwidth Quantization on the Adversarial Robustness for Embedded Neural Networks
Rémi Bernhard Pierre-Alain Moellic Jean-Max Dutertre
























Impact of Low-bitwidth Quantization on the Adversarial Robustness for Embedded Neural Networks
    Rémi Bernhard1, Pierre-Alain Moellic1, Jean-Max Dutertre2 
 

1CEA Tech, Systemes et Architectures Sécurisées (SAS),
Centre CMP, Equipe Commune CEA Tech - Mines Saint-Etienne
 Gardanne, France 
 
2Mines Saint-Etienne, CEA-Tech,
 Centre CMP,

Gardanne, France 
 

remi.bernhard@cea.fr, pierre-alain.moellic@cea.fr, dutertre@emse.fr

    
===============================================================================================================================================================================================================================================================================================================================================







§ ABSTRACT


As the will to deploy neural networks models on embedded systems grows, and considering the related memory footprint and energy consumption issues, finding lighter solutions to store neural networks such as weight quantization and more efficient inference methods become major research topics. 
Parallel to that, adversarial machine learning has risen recently with an impressive and significant attention, unveiling some critical flaws of machine learning models, especially neural networks. In particular, perturbed inputs called adversarial examples have been shown to fool a model into making incorrect predictions.

In this article, we investigate the adversarial robustness of quantized neural networks under different threat models for a classical supervised image classification task. We show that quantization does not offer any robust protection, results in severe form of gradient masking and advance some hypotheses to explain it. However, we experimentally observe poor transferability capacities which we explain by quantization value shift phenomenon and gradient misalignment and explore how these results can be exploited with an ensemble-based defense.




§ INTRODUCTION




 §.§ Context


    
Neural networks achieve state-of-the art performances in various domains such as speech translation or image recognition. These outstanding performances have been allowed – among others – by tremendous computation power (e.g., popularization of GPU) and the resulting trained architectures come with thousands or even millions parameters. 
As the desire to run pre-trained neural network based application (e.g image recognition) on embedded or mobile systems grows, one must investigate the ways to solve the practical issues involved. First of all, the memory footprint can quickly be a limiting factor for constrained devices. For example, a typical ARM Cortex-M4-based microcontroller such as STM32F4 has up to 384 KBytes RAM and a maximum of 2MBytes of Flash memory[https://www.st.com/en/microcontrollers-microprocessors/stm32f4-series.html]. Secondly, inference cost in terms of energy is critical for devices like mobile phones or a large variety of connected objects (e.g., industrial sensors). Thirdly, inference speed is necessary to avoid critical latency issues. 

Some APIs like the Android Neural Network API (NNAPI[https://developer.android.com/ndk/guides/neuralnetworks]) have been already developed to allow to run efficiently trained models with famous frameworks (TensorFlow[https://www.tensorflow.org/], etc.) on Android systems. Tensorflow Lite (TFLite[https://www.tensorflow.org/lite]) allows to transfer pre-trained model to mobile or embedded devices thanks to model compression techniques and 8-bit post-training weights quantization. ARM-NN[https://developer.arm.com/products/processors/machine-learning/arm-nn] is another SDK that does the link for applications between various machine learning frameworks and diverse Cortex CPUs or Mali GPUs types (note that CMSIS-NN[https://github.com/ARM-software/CMSIS_5] is dedicated to Cortex-M MCU). STMicroelectronics also proposes an AI expansion pack for the STM32CubeMX, called X.Cube.AI[https://www.st.com/en/embedded-software/x-cube-ai.html], to map pre-trained neural network models into different STM32 microcontroller series thanks to 8-bit post-training quantization and other optimization tricks related to the specificity of these platforms.

On a more theoretical side, research about reducing the number of parameters to directly impact the memory footprint of models <cit.>, or developing quantization schemes coupled with efficient computation methods to reduce inference time and energy consumption has arisen <cit.>.

At the same time, neural networks have been shown to be vulnerable to malicious tampering of inputs <cit.>. From a clean observation correctly classified by a model, an adversary optimally crafts a so-called adversarial example, which is very similar to the clean observation and fools the model. Many attack methods (<cit.> for some of the most famous) and defense methods (<cit.> for some of the most famous) have been developed and evaluated in benchmarks or competition tracks such as the NIPS Adversarial Vision Challenge[https://www.crowdai.org/challenges/adversarial-vision-challenge].



 §.§ Motivation and related works



In terms of security, as embedded systems with neural networks models become ubiquitous, it is a particularly interesting topic to evaluate the robustness of state-of-the-art quantization methods under different threat models. Moreover, studying the transferability of adversarial examples between original (i.e. full precision) and quantized neural networks may at the same time highlight weaknesses or strengths of future embedded systems, and allow to better understand if quantization in itself could be a relevant defense against adversarial examples or, on the contrary, exacerbates these flaws.

Some authors have already investigated the link between quantization and robustness. <cit.> claimed that neural networks trained with weights and activation values binarized to {-1,1} have an interesting robustness against adversarial examples. However, this robustness was demonstrated thanks to the only MNIST dataset 
and use stochastic quantization. This quantization scheme induces the stochastic gradient phenomenon <cit.>, which can mislead to the true efficiency of this defense by causing what <cit.> called obscurity. <cit.> tries to explain some weaknesses of quantization-based defense methods against adversarial examples. They show experimentally that these defense methods can, in fact, denoise an adversarial example or enlarge its perturbation, depending on the size of the perturbation in the input space and the number of bits used for quantization. Thus, quantization can participate in an error amplification or attenuation effect. However, they only apply the FGSM attack <cit.> in a white-box setting against simple activation quantization.
Although focused on model compression (pruning), <cit.> studied the robustness of quantized neural networks against adversarial examples with a fixed-point quantization scheme applies to both weight and activation values, no less than 4-bit model, and a restricted set of (gradient-based) attacks.
<cit.> proposes a defense method based on activation quantization coupled with adversarial training <cit.>, which has been shown by <cit.> to introduce gradient masking <cit.>.
Interestingly, <cit.> notes that the gradients obtained via the use of a Straight Through Estimator (hereafter STE, <cit.>) – a common technique to compute gradients when quantization operations lead to differentiability issues – may not be representative of the true gradient. This observation leads to questions about efficiency of gradient-based attacks against quantized neural networks, and strengthens up the motivation to study gradient masking issues and black-box attacks against such models. The authors propose a Mixed Integer Linear Programming (MILP) based attack, which shows good results on the MNIST data set but is not scalable to large neural networks, due to computation cost issues. 




 §.§ Contributions



In this work, we study the robustness of natural and quantized models and against adversarial examples under different threat models against various types of attacks. Our contributions are:

    
  – We show that quantization in itself offers poor protection against various well-known adversarial crafting methods and we explain why activation quantization can lead to severe gradient masking, a phenomenon which leads to non-useful gradients  to craft adversarial examples <cit.> and causes ineffective defense <cit.>.
    
  – We show very poor transferability capacities of adversarial examples between full-precision and quantized models and between quantized models with different bitwidths. We advance hypothesis to explain it, including a quantization shift phenomenon and gradient misalignment.
    
  – We investigate a defense based on an ensemble of models with different quantization levels. Results are promising and pave the way towards a possible perspective for future work.





§ BACKGROUND




 §.§ Quantization of neural networks



The purpose of this article is to study the impact of quantization techniques on the adversarial robustness, for embedded neural networks. However, other complementary approaches are extensively studied to compress as well as to speed up models at inference time. During inference, energy consumption grows with memory access, which itself grows with memory footprint. Thus, reducing the number of parameters has been logically investigated. For example, <cit.> show, for specific architectures and datasets, that some of the parameters are predictable from the others.
 <cit.> develop a three-step method (pruning, clustering, tuning) to efficiently compress a neural network achieving a reduction of AlexNet memory footprint by a factor of 35. <cit.> propose a method to reduce the memory size of a convolutional neural network by pruning connections based on a deterministic rule. This method is also coupled with weight binarization (<cit.>) and an efficient hardware architecture on a FPGA in order to reduce inference time.







Reducing the precision of the weights or developing efficient computation methods for some precise format of weight values is an important field of investigation. Quantization can be performed as a post-training process or during training. For the first case, as previously described in introduction, several tools have been recently proposed to map full precision pre-trained models for inference purpose (TFLite, ARM-NN, STMCubeMX A.I.[https://www.st.com/en/embedded-software/x-cube-ai.html]) by coarsely quantizing some weights into – usually – no more than 8-bit integers. More advanced methods propose clustering methods <cit.> or information theoretical vector quantization methods (inspired by <cit.>) such as <cit.> who achieved 
 about 20 times compression of the model with only 1% loss of
classification accuracy on the Imagenet benchmark. 

In this article, we focus our work on quantization techniques at training time since these approaches enable to reach state-of-the-art performance with lower bitwidth precision. Hereunder, we detail some of the most popular works on that field that we consider for our experimentations.


Binary Connect and Binary Net.

<cit.> presents a method to train neural networks with weights w binarized to w_b ∈{-1,1}. During training, weights are binarized for the forward pass, and as the binarization operation can be not differentiable or lead to the vanishing gradient problem, the STE given in Equation <ref> is used for the backward pass:

    ∂ C(w)/∂ w≈∂ C/∂ w|_w=w_b1_|w|≤1

Where C is the cost function and 1(.) the indicator function. <cit.> pursue this idea by training binary networks (BNN) with weight values w and activation function a^k values binarized to (w_b, a_b^k) ∈{-1,1}^2. During the backward pass, the authors used the same STE principle for activations as  in Equation <ref> above. Improvements have been proposed as in <cit.> by adding regularization and more complex approximation of the derivative on the backward pass.







Xnor Net.
<cit.> binarizes weigths and activation values no more to {-1,1} but to {-α , α} with α∈ℝ^*,+. They formalize the search of the best binarization approximation of the real-valued weights as the following optimization problem.:

    _B, α W - α B _2

Where W is the weight matrix and B is a matrix with only -1 and 1.
During the backward pass, a STE is used.


Ternarization.
In <cit.>, the authors propose a method to train a neural network with weight values ternarized to {-1,0,1} during the forward pass. <cit.> also propose a method to train a neural network with weight values ternarized to {-α_l,0,α_u} during the forward pass, where α_l, α_u ∈ℝ^2,*_+ and α_l and α_u are updated during training.


Low bitwidth quantization.

<cit.> successfully train networks with good precisions on MNIST and CIFAR10 data sets while limiting the bitwidth of the weights values to 16 bits and using stochastic rounding. <cit.> proposes a method to train neural networks with low-bitwidth weight values, gradients and activation function values.   
They claim that taking advantage of this technique during the forward pass could help speed up the training of neural network on resource-limited hardware, and naturally speed up the inference. 
For a real value x ∈[0,1 ] and a number of bits n, the function 

    Q(x,n) = round((2^n -1) x)/2^n -1

is the quantization function used for weights, activation values and gradients. The weight and activation values are quantized on the forward pass only. The authors also found that quantizing gradients on the backward pass (with a STE) was requisite. Q(x,n)  (2^n -1) results in 2^n values, each of them representable with a n bits integer. During the forward pass, one can take advantage of the bit convolution kernel method (see <cit.> for details) with respect to the Q(x,n)  (2^n -1) values, and then scale afterwards with the 2^n -1 value.

<cit.> proposes another weight quantization method with a constraint on the number of "1" in the binary representation of weights, along with some efficient computation method.

<cit.> proposes a method which involves distillation and quantization of the weight values to decrease the storage size of a model. For the quantization part, given some weight value w, it is first scaled to a value v in [ 0,1 ], then mapped with a function Q to the nearest mapping points among the s+1 points in [0,1] and then scaled back to the original scale. 


In this article, we use the Binary Net method and Binary Connect respectively from <cit.> and <cit.>, and the quantization method (Dorefa-Net) from <cit.>.


    

 §.§ Adversarial machine learning

    
    
Machine learning systems have been shown to be vulnerable against different types of attacks threatening their confidentiality, integrity or accessibility. We can distinguish three different types of attacks: 

	
  * Data/Model leakage occurs once the model has been trained. An adversary aims at stealing models parameters or architecture, or stealing confidential or private (training) data <cit.>.
	
  * With data poisoning, which steps in during the training phase, an attacker targets the integrity or availability of the system according to the level of the perturbation. In order to decrease the model accuracy, corrupted data are introduced in the training set when the data are collected in the physical world or directly in the model input domain <cit.>.
	
  * Adversaries may also alter the inputs at inference time, by crafting malicious observations looking like clean ones but designed such as to fool the model <cit.>, striking the model integrity.

Here we focus on the latter type of integrity-based attack, i.e adversarial examples crafting.




  §.§.§ Adversarial examples

     

Adversarial examples are highly worrying threats to machine learning. Roughly stated, considering a classifier model, an adversarial example is a slightly modified version of a correctly classified clean example in a way such that the classifier will output two different classes for those two examples.

The reason of existence of adversarial examples lead to various hypothesis.
<cit.> propose a first explanation to the existence of adversarial examples: they would be in fact located in low-probability pockets of the input space. On their side, for <cit.>, it is some local linearity assumption which eases the crafting of adversarial examples, and not the global non-linear nature of neural networks. In <cit.>, the authors give a more geometric explanation to the existence of adversarial examples, saying that the learned boundary "extends beyond the submanifold of sample data and can be – under certain circumstances – lying close to it" (boundary tilting effect), and argue that the linearity assumption is not sufficient to explain adversarial examples that are – for some of them – the result of overfitting issues. <cit.>, based on theoretical results linking the generalization error to the average distance to a misclassified point for a very particular type of dataset, include high dimensionality as a possible power factor of adversarial examples. Another common hypothesis, used among others to detect adversarial examples, is that adversarial examples are not on the data manifold <cit.>. Recently, <cit.> show that adversarial examples are the consequence of non-robust features derived from patterns with a big predictive power, yet these patterns are meaningless to humans and they can be adversarially modified to fool the target classifier.

More precisely, given a classifier model M learning a mapping function f: ℝ^m {1,...,K }, given an initial clean observation x ∈ℝ^m, given a target label t ∈{1,...,K },  a targeted adversarial example x ' ∈ℝ^m crafted from a correctly classified x is defined such as f(x) ≠ f(x')=t and d(x,x') < ϵ with d a distance function being often the distance derived from the l_2 or l_∞ norm.

Based on <cit.>, the search of such an adversarial example can be written as:



    ϵ     = _ϵϵ_p
    
        s.t    f(x + ϵ)     = t  (targeted attack)
    
        s.t    f(x + ϵ)    ≠ f(x)   (untargeted attack)


Usually, the adversary may also wants that x + ϵ be bounded (for example, x+ ϵ∈ [0,1] as in <cit.>).






  §.§.§ Attacks



In this article, we use five different adversarial crafting methods. These attacks are presented in their untargeted version, where adversarial examples are crafted from a clean observation x of label y, logit_j(x) designates the logit output for the j^th class, and f_j(x) designates the softmax output for the j^th class.


Fast Gradient Sign Method (FGSM).
Presented by <cit.>, this method, derives an adversarial example x maximizing J(θ, x + α,y) - J(θ,x,y) with respect to α ,given that α_∞ < ϵ,  by performing a linear approximation of the loss function J(θ,x,y) around x, one gets:

    x' = x + ϵ Sign( ∂ J/∂ x(θ,x,y))

x' is then clipped to respect a possible box constraint (for images for example one may want x' ∈ [0,1])


Basic Iterative Method (BIM) <cit.> presents the Basic Iterative Method, derived from the FGSM method, which allows to craft targeted adversarial perturbations. Given a maximum adversarial perturbation ϵ:

    x_0 = x, 
         x_n+1 = Clip_B_∞(x, ϵ)(x_n + α sign( ∂ J/∂ x(θ,x_n,y)))


where B_∞(x, ϵ) is the l_∞-ball of radius ϵ and center x, and we set α = ϵ/T with T the total number of iterations.
In fact, we just repeat the targeted FGSM method for K iterations, performing clipping at each iteration.
x' is then clipped to respect a possible box constraint (for images for example one must have x' ∈ [0,1]).


Carlini-Wagner l_2 (CWl2).
Presented by <cit.>, the Carlini-Wagner l_2 method consists of considering the following objective:


    min_ϵ     ϵ_2 + c F(x + ϵ,y),
        s.t.    x + ϵ∈ [0,1]

where:

    F(x + ϵ, y) = max(logit_y(x + ϵ) - max_j ≠ y logit_j(x + ϵ), -κ)

with κ≥ 0, c >0. We set κ=0 and thus we have F(x+ϵ,y) = 0   label(x) ≠ y.
c ∈ℝ^+ is a constant for which binary search is performed a decided amount of time. Then, the change of variable x=1/2(tanh(w)+1) is performed to get rid of the box constraint. The resulting optimization problem with respect to the new variable w can then be solved with classical optimization methods like Stochastic Gradient Descent (SGD) or Adam.


SPSA attack.
In <cit.>, the authors propose a very effective gradient-free attack to evaluate defense strategies. The authors propose the constrained optimization problem given in Equation <ref> that they solve by using the Adam update rule,approximating the gradients with finite difference estimates thanks to the SPSA (Simultaneous Perturbation Stochastic Approximation, <cit.>) technique which is suitable for noisy high dimensional optimization problems, and performing clipping at each iteration to respect the constraint α_∞ < ϵ: 

    min_ϵ      logit_y(x + α) - max_j ≠ y logit_j(x + α),
        s.t. α_∞ < ϵ


Zeroth Order Optimization (ZOO).
The ZOO attack <cit.> is based on the CWl2 attack with a discrete approximation of the gradients:

    g'(x)_i ≃g(x + h e_i)   - g(x - h e_i) /2 h

where e_i is the basis vector with only the i^th element equal to 1, the others equal 0 and h is a small constant. The ZOO attack does not consider the logits values as the CWl2 attack does but the logarithm of the softmax output values, i.e we have:

    F_ZOO(x + ϵ, y) = max(log(f_y(x + ϵ)) - max_j ≠ y log(f_j(x + ϵ)), -κ)





Characteristics.
We sum up the main characteristics of these attacks in table <ref>.





  §.§.§ Defenses



Many defenses have been investigated to counter adversarial examples. As the core of this article does not either aim at testing defense schemes or a specific attack, we refer to <cit.> for an overview of protections. The authors distinguish mainly the reactive defenses, which encompass pre-processing inputs and detection methods <cit.>, the proactive defenses, which encompass techniques to make a network in itself more robust to adversarial examples <cit.>, and provable defense methods <cit.>.



            






















 §.§ Threat model





  §.§.§ Main characteristics of threat models


The threat model encompasses assumptions about the adversary's goals, capabilities and knowledge.



  
Adversarial goal

            
Here we focus on an adversary that aims to fool a supervised model at inference time. From a clean observation x correctly labeled as y, the adversary wants to craft an adversarial example x' labeled as a precise class t ≠ y (targeted attacks) or any class y' ≠ y (untargeted attacks). Given some threat model, a defense method claiming robustness against untargeted attacks is stronger than a one claiming robustness against targeted attacks. Similarly, it is often more difficult to craft targeted adversarial examples than untargeted ones.



  
Adversarial capability


It is crucial to properly define how much an adversary can alter a process of the machine learning pipeline. In the scope of adversarial examples crafting, the adversary's ability is almost all the time defined as an upper bound ϵ of the distance D(x, x') between a clean observation x and the adversarial example x' crafted from x. The distance D is derived from an l_p norm, usually the l_0, l_1, l_2 or l_∞ norm.
        


  
Adversarial knowledge

        
Traditionally, two main different settings are used to describe the way an adversary can operate. Each setting contains its own nuances but for the sake of simplicity we only present those we will later consider in this article. 

In the white-box setting, the adversary is assumed to have a full access to the target model. This includes the type of model (SVM, architecture of a neural network, etc.), the parameters of the model (network's weights, etc.), any preprocessing component, etc. 

In a rigorous black-box setting, the adversary has no information about the model but can (only) query it (in a limited or unlimited way). However, this setting can be loosened (some talk about grey-box settings) according to the kind of information the adversary can get when querying the model (full prediction outputs – softmax or logits outputs – or just the predicted label) as well as a full or partial access to the training data. Note that in order to thwart a possible restriction concerning the access to the training set, <cit.> proposes a way to train a substitute model by synthetically generating data labeled by the neural network under attack. 



  §.§.§ Specificity induced by embedded models


For our experiments, we do not consider a strict black-box setting since we assume an attacker will try to transfer the adversarial examples from one full-precision model to a quantified one or one quantified model to another one with a different level of quantization. This means that we assume a worst-case scenario where an adversary knows the model architecture and can query it without limitation with a full access to the softmax output. Moreover, since we use classical image collections, we assume that the attacker has access to the same dataset. Considering the global context of embedded neural networks for inference, numerous popular and proven architectures (such as the ResNet networks) are directly applied for a large scope of applications. Then, a scenario where an adversary craft malicious inputs from a known full precision models to attack an optimized (i.e. quantized) model in, for example, a mobile device is a realistic scenario.

However, we must highlight an important characteristic of the threat models when dealing with an attacker who aims to target an embedded machine learning model. In that case, both the architecture of the model itself and its implementation are important. That means we need to consider a twofold white/black box paradigm: on one hand, the adversary can have – classically –  full or no knowledge of the model architecture (abstraction level), and, on the other hand, he may also have full or no knowledge of the model implementation within the target device (physical level).

As previously said, in this work and more particularly in the section dealing with the use of quantized networks as a defense mechanism, we mainly focus on a threat model where an attacker has a white-box access to the model architecture but not for its implementation in the target device. Then, the most natural scenario corresponds to an attacker that tries to directly transfer the adversarial examples crafted from a full precision model to the embedded system.

We are conscious of the limitation of such a scenario since, obviously, the attacker may guess – thanks to information about the hardware platform (i.e. memory, precision constraints, etc.) – relevant optimization methods applied to the model (weights and activations quantization, pruning...). That means an advanced adversary could try to craft adversarial examples from a quantized model of its own (without knowing the quantization method used for the target device neither if additional optimizations have been performed).


  




§ EXPERIMENTS


We start by performing adversarial robustness experiments for full-precision and quantized models with gradient-based and gradient-free attacks that may be used in black-box settings (unfeasible gradient computation).
In both case, we find that quantization does not provide reliable protection. We notice that quantization causes some gradient masking, which tampers some gradient-based attacks (FGSM and BIM) and may prevent gradient-free attacks relying on the approximation of the gradient of the output function (ZOO) to perform well. However, some gradient-based attacks using the STE to mount gradient-based attacks (CWl2) seem to avoid the gradient masking effect.
Secondly, we perform transferability experiences between full-precision and quantized models and show poor transferability capacities, which we explain with the quantization value shift phenomenon and gradient misalignment.




 §.§ Data


        
We conduct our experiments on the CIFAR10[https://www.cs.toronto.edu/ kriz/cifar.html] and SVHN[http://ufldl.stanford.edu/housenumbers/] (Street View House Numbers)  two classical natural scene image datasets. CIFAR10 is composed of
60,000 images, with 10 classes. We use a training set of size 50,000 and a testing set of 10,000. The SVHN dataset is composed of 99,289 images, with 10 classes. We use a training set of size 73,257 and a testing test of 26,032.

    

 §.§ Experience details

    

For each dataset, we trained a full-precision (32-bit floating point) neural network (hereafter called "float model" in tables), and various quantized neural networks. More precisely, for each data set, the neural network architecture is based on the one presented in <cit.>. It consists of convolutional blocks, each of them being the stack of a convolution layer, a batch-normalization layer and the relu activation function, followed by dense blocks being a stack of a dense layer, a batch-normalization layer and the ReLu activation function. At the top of the network, we chose a dense layer with the softmax activation function, contrary to <cit.>, where there is no activation function but a final batch normalization layer. Models architecture are detailed in Appendix <ref>.
Full-precision and quantized networks were trained with the cross-entropy loss, contrary to <cit.> where the hinge-loss is used, as we found it to converge faster. The optimization is done with Adam <cit.>, using a staircase decay for the learning rate.

Four different quantization bitwidth are considered: 1,2,3 and 4 bits. For each bitwidth, we consider quantization on the weights only (weight quantization) or the weights and the output of each convolutional or dense block (full quantization). For the weight binarization, the full binarization and the 2,3,4-bit quantization, we use respectively the Binary Connect method <cit.>, the Binary Net method <cit.> and the Dorefa Net method <cit.> described in <ref>. The input layer and the last dense layer are never quantized, to allow an efficient training <cit.>. 

The performance (accuracy) of each model on the test sets is presented in Table <ref>. Quantization does not affect significantly the accuracy, except for fully binarized models which achieve only 0.79 and 0.89 accuracy on CIFAR10 and SVHN respectively, which represents a non negligible drop of performance. For quantized models with more than 1 bit, the test set accuracy is comparable to the one obtained for full-precision models. These results are consistent with <cit.> and <cit.>. Note that in <cit.> the authors explain the performance of binarized networks with a regularization effect brought by quantization and <cit.> show that the architecture as well as the size of the data set can have an impact on the performance of quantized networks.




For each data set, we begin by evaluating the robustness of the full-precision and quantized models when the adversary uses three classical white-box gradient-based attacks: FGSM, BIM and CWl2. Then, we evaluate two gradient-free attacks, suitable for black-box settings: ZOO and SPSA. For FGSM, BIM, CWl2 and SPSA we use the Cleverhans library <cit.>, and for ZOO we use the original code provided by the authors[https://github.com/huanzhang12/ZOO-Attack]. The attacks parameters are detailed in Appendix <ref>. BIM, CWl2, SPSA and ZOO are performed on 1000 randomly samples from the test set. 


Over a second phase, we evaluate the transferability of attacks between full-precision and quantized models.


    

 §.§ Evaluation metrics

    

For x ∈ℝ^m, p ∈ℕ, p ≥ 1, we note l_p the p-norm of x:

    x _p = ( ∑_i=1^m | x_i |^p )^1/p


For each attack, we report two evaluation metrics (see <cit.> for an extended review of the adversarial robustness evaluation):

    
  * The adversarial accuracy, which is the accuracy of the model on adversarial examples (noted acc in the result tables). The crafting method generates an adversarial example x' from each input x of the test set X. Hereafter we note X' the adversarial test set on which is computed the adversarial accuracy. The higher the adversarial accuracy, the less the model is fooled by adversarial examples, i.e. the more the model is robust against the attack.
    
  * The average minimum-distance of the adversarial perturbation, i.e. in our case, the average l_2 norm and l_∞ norm of the difference between clean and adversarial examples which succeed to fool the target model (simply noted l_2 and l_∞ in the result tables). This quantifies the average distortion needed by the attacker to fool the model. 






§ RESULTS




 §.§ Robustness against gradient-based and gradient-free attacks

    
    
Results of direct attacks against fully quantized and weight-only quantized models are presented respectively in tables <ref> and <ref>. For these tables and the following ones dealing with quantized models, first row of the results is for 1-bit model (binarized model), second row for the 2-bit model, third row for the 3-bit model and fourth row for the 4-bit model. 

























 




    

  §.§.§ Robustness of binarized neural networks

    

A first observation from the comparison of table <ref> and <ref> is that the weight-only quantization has no impact on the robustness. Then, with
table <ref> we see that fully binarized models are far more robust to FGSM and BIM than their full-precision counterparts, as noted by <cit.>, but achieve only 0.79 and 0.89 accuracy on (respectively) the CIFAR10 and SVHN test datasets (see table <ref>) which represents a non negligible drop of performance. 

However, CWl2 – one of the most powerful crafting method – is almost as efficient against fully binarized neural networks as against full-precision models. Therefore, fully-binarized neural networks do not bring much robustness improvement compared to full-precison models against gradient-based attacks, as claimed in <cit.>.
We also note that fully binarized models are relatively more robust to SPSA as well compared to full-precision models. This combined with the slightly poorer performance of CWl2 on binarized models indicates that the loss surface for binarized models is difficult to optimize over.

For full quantization with more than 1 bit, the gradient-based attacks are almost as efficient as against a full-precision model, except for FGSM on SVHN only with a 10% gain of accuracy.


    

  §.§.§ Activation quantization causes gradient masking


Interestingly, we see that ZOO fails very often to produce adversarial examples when attacking fully quantized neural networks. More precisely, we note that when adversarial examples are crafted on a full-precision model, ZOO and CWl2 reach almost the same adversarial accuracy, with slightly higher l_2 distortion for ZOO. However, when adversarial examples are crafted on a model with quantized weights and activations, we note that the adversarial accuracy is higher with ZOO than with CWl2, but that successful adversarial examples crafted with ZOO have much lower l_2 distortion than the ones crafted with CWl2 as well as the one observed for the full-precision model (l_2=0.72).
We claim that these observations reveal some form of gradient masking caused by the quantization of activation values. Firstly, the almost equal performance of ZOO compared to CWl2 for a full-precision model is expected as gradient-free attacks are supposed to perform worse than their gradient-based counterparts when no gradient masking occurs. Secondly, we argue that the phenomenon observed on full quantization models is due to gradient masking and the STE technique involved in these models training. 

We explain this by distinguishing two cases:

    
  – ZOO fails to produce successful adversarial examples and CWl2 succeeds: (1) because of the activation quantization, a little change (h e_i) may switch the activation value from one quantization bucket to another, inducing a big change in the predicted softmax values, causing the discrete derivative F^d_ZOO(x) to explode; (2) on the contrary, this change can also have no impact (keep values in the same bucket), which results in F_ZOO(x - he_i, y) = F_ZOO(x + he_i, y), causing the discrete derivative F^d_ZOO(x) to be null. To sum up, F_ZOO presents some sharp curvatures or flatness around some points, caused by activation quantization, which prevents ZOO to build successful adversarial examples. The CWl2 attack avoids this problem as it computes gradients thanks to a STE, even if the gradient computed may not be exactly the same as the true gradient <cit.>.
    
  – Both ZOO and CWl2 succeed to produce successful adversarial examples: the l_2 distortion for the successful adversarial examples produced by ZOO is smaller than the one produced by CWl2. Around these points the surface of the objective function to optimize does not present any sharp curvature or flatness. Both ZOO and CWl2 do not suffer from the local minima problem, but as noted by <cit.>, the gradients computed by the CWl2 attack is not representative of the true gradient. The gradient being better estimated by the ZOO attack, it explains its success (lower l_2 distortion).


The gradient masking phenomenon hypothesis concerning the quantization of activation values is also verified with the fact that the SPSA attack (a gradient-free attack) performs quite better in terms of adversarial accuracy than the BIM attack, against fully binarized models. We also make the hypothesis that SPSA avoids the sharp curvatures or flatness observed around some points, where ZOO fails to produce adversarial examples, because of the more efficient gradient estimation method suited to noisy objective functions <cit.>. 

For the weight quantized models results presented in Table <ref>, we do not observe the same phenomena as for the full quantization models. No apparent robustness is noticeable for the weight quantized models. No sharp variation or flatness is induced for the objective function of the ZOO attack of the weight quantized models, as it originated from the quantization of activation values. It has to be noted that we also measured that the variance of the logits values between full-precision and weight-only quantized models is almost the same.

























    
    








 §.§ Transferability


We present results of transferability when the source network (i.e. the models the adversarial examples are crafted from) is full-precision, fully or weight-only quantized models, and the adversarial examples are transferred to (target models) full-precision, fully or weight-only quantized networks, in figures <ref> and <ref>. For CWl2, as advised in <cit.>, we consider κ > 0 to build strong adversarial examples on the source model, more likely to transfer. We tested κ = 5, 10, 15, 20, 25. For each source model, we report the best transferability results. The results for the cases where the source networks are 2-bit or 4-bit quantized models are not presented here for paper length purpose and as these results can be interpolated from the one we present. More complete tables can be found in Appendix <ref>.





 













    

  §.§.§ Weak transferability

 
A first observation is that transferability results are quite poor for FGSM, BIM and SPSA. CWl2, given tuning the parameter κ, suffers less from transferability issues, at the cost of increased l_2 and l_∞ distortion, except when the source or target network is a fully binarized network. Indeed, for the κ values tested (for fully binarized models, the results reported are for κ = 5), when the source network is a fully binarized model, 
CWl2 struggles to find adversarial examples having both F(x+ϵ,y) < 0 (see Equation <ref>) and a little l_2 distortion. This results in adversarial examples being missclassified but not imperceptible by a human. We hypothesize this comes from the hard to optimize loss function as noted in <ref>. 
We also note that, as already noticed by <cit.>, and contrary to what was initially found by <cit.>, that BIM – as it is the case here – may produce more transferable adversarial examples than FGSM . 

    

  §.§.§ Quantization shift phenomenon


These poor transferability results (mainly for FGSM and BIM) can be explained by the quantization value shift phenomenon which takes places when quantization ruins the adversarial effect by mapping two different values to the same quantization bucket. In case of activation quantization, two activation values can be mapped to the same value. In case of weight quantization, this levelling effect may also be observed and ruins the adversarial effect. The Figure <ref> shows a toy example of the impact of weight quantization on adversarial effect: in this example, the adversarial effect is canceled.



















Consequently, whatever the quantization level of the source model adversarial examples are crafted on, evaluating them on a target model with a different quantization level may hinder their efficiency because of this phenomenon. 










    

  §.§.§ Gradient misalignment


Regarding the transferability results, we may also hypothesize that the gradient direction between float models and quantized models and between models with different bitwidths is quite different. This gradient misalignment may be noticeable for the gradient computed with the Straight Through Estimator, as poor transferability is observed for the white-box attacks (FGSM, BIM, CWl2), and for the real gradient, as poor transferability is observed for SPSA. We measure the mean cosine similarity between the gradient of the loss function with respect to the input between models with different bitwidth and show the results in Figure <ref> for CIFAR10.
We remind that the cosine similarity CS(a,b) ∈ℝ between two vectors a,b ∈ℝ^m ×ℝ^m is defined as:


    CS(a,b) = ⟨a,b⟩/ a _2 · b _2

where ⟨·,·⟩ is the usual scalar product. CS(a,b)=0 indicates orthogonal vectors for the usual scalar product, CS(a,b)=1 indicates aligned vectors in the same direction and CS(a,b)=-1 indicates aligned vectors in opposite directions.




In Figure <ref>, we first observe that the cosine similarity values for gradients of the loss function between full-precision and quantized models and between quantized models with different bitwidths, are relatively close to 0, indicating nearly orthogonal gradient directions. We observe that the cosine similarity for the gradient of the loss function with respect to the input between fully-binarized models and others models is the closest to 0.  This is in accordance for example with results presented in Fig. <ref> where transferability capacities for FGSM, BIM, CWl2 and SPSA are the poorest when fully-binarized models are involved. Moreover, this may explain the fact that adversarial accuracy is much higher in tables <ref> and <ref> (Appendix <ref>), where adversarial examples are crafted on  fully-binarized models, than in the other tables
(see Appendix <ref>).




To conclude, transferability results show that quantization strongly alters chances of success for an adversary who has only access to a full-precision (or quantized) version of a model and wants to attack a quantized (respectively, a full-precision) version of a model, assuming this adversary can not use a black-box attack such as the SPSA one.






§ ENSEMBLE OF QUANTIZED MODELS













 §.§ Motivation


Regarding of the transferability results, a logical consequence and a natural assumption is to consider an ensemble of quantized models to filter out adversarial examples. In this section, we analyze the relevance of this defense strategy. We remind the reader about the important point we highlight in section <ref> about the threat models and their intrinsic limitations.


We consider an ensemble of quantized models, ℳ={M_i}, in our case: a full-precision model and four fully-quantized models (1,2,3 and 4 bits). We first analyze statistically how the models agree on clean and adversarial test set using different crafting methods (FGSM, BIM, CWl2 and SPSA). More precisely, we consider an adversarial example crafted on a source model M_s and we look at how the five models agree, given that this adversarial example is successful or not on M_s. Similarly, we look at how the five models agree on clean test set examples, given that this test set example is correctly classified in the true class or, on the contrary, misclassified  by M_s.


    
    



Our main observations are the following:

    
  * the models are more likely to agree on clean samples than on adversarial examples (successful or not);
    
  * the models are much more likely to agree on unsuccessful (on M_s) adversarial examples. 


In table <ref>, we show for CIFAR10 and SVHN, given the source model M_s, how the four other models agree on test set and adversarial examples crafted with FGSM. For example, for CIFAR10, with the 2-bitwidth model, 77% of test set examples well-recognized by M_s are also correctly classified by the other four models, compared to 33% for misclassified test set examples. Moreover, 37% of successful adversarial examples (i.e. examples that effectively fool M_s) also fool the other four models, and 76% of unsuccessful adversarial examples (on M_s) are also unsuccessful on the other four models.





We design the following ensemble-based defense method for the distant system: the prediction for an upcoming example is done only if m or more models agree, and the final label is the one predicted by these m models. 
We note valid_m, ℳ(X) the set of samples from the input data set X which respect this criterion:




    valid_m, ℳ(X) = { x ∈ X  | max_j ∈[ 1,…,C ] ∑_i1_M_i (x) = j≥ m . }

with C the number of labels and M_i( x ) the output prediction label of x by M_i. Then, the prediction rate (hereafter, PR) quantifying the proportion of examples from X for which prediction is performed is :

    PR_m, ℳ(X) = |valid_m, ℳ(X)|/|X|

Where |X| is the cardinality of X. Considering the remarks made above, this approach encourages prediction for clean examples rather than for adversarial examples, and when the prediction is performed on adversarial examples, it would be predominantly unsuccessful ones. 

When evaluating this defense on the adversarial test set (X'), an overall performance metric, hereafter called defense accuracy (d_acc) is simply defined as the proportion of adversarial examples which have been filtered out or which are unsuccessful. Practically, the defense accuracy could also be defined as:  


    d_acc_m, ℳ(X') = 1- |valid_m, ℳ(X')^s|/|X'|


Where valid_m, ℳ(X')^s denotes the set of successful adversarial examples which thwart the filtering process (i.e. the error rate of the defense). 

Thus, the number m has to be decided following a trade-off between the number of test set examples for which prediction is performed (which has to remain high) and the defense error rates (which has to be low) when facing adversarial examples. We experimentally set m=4 for CIFAR10 and m=5 for SVHN to reach a good trade-off. As presented in Table <ref>, the prediction is performed for more than 87% of the clean test set examples for both CIFAR10 and SVHN, with an accuracy of 90% and 98% respectively for CIFAR10 and SVHN on clean test set examples for which prediction is performed. 





 §.§ Results



We present the results of this defense on CIFAR10 and SVHN in table <ref>. For each source model and each attack method, we report the defense accuracy d_acc against four attacks (FGSM, BIM, CWl2 and SPSA), along with the prediction rate PR.



Considering the natural threat model discussed in <ref>, results are interesting for adversarial examples crafted from the full precision model especially for SVHN. If an attacker tries to directly transfer the adversarial examples crafted from the full precision model with, for example, the CWl2 attack, 60% of the adversarial examples are harmless (i.e. filtered out or unsuccessful) for CIFAR10 and this robustness is even stronger for SVHN with a defense accuracy superior to 80% for BIM, CWl2 or SPSA.

Moreover, coherently with the transferability results (see Figures <ref>, <ref> and additional tables in Appendix <ref>), the highest robustness is reached when adversarial examples are crafted from a fully binarized network with a defense accuracy superior to 0.8 – whatever the crafting method –  particularly for SVHN. However, there is no significant gain compare to the transferability results obtained by taking each quantized model separately (see figures <ref> and <ref>). But, except for this case of the fully binarized network, the ensemble of quantized models shows better robustness to transferred adversarial examples than all single models. The more relevant gain is reached with CWl2 attack with a mean (over the 2, 3 and 4-bitwidth networks) defense accuracy of 0.53 and 0.8 respectively for CIFAR10 and SVHN. 

Once again, if we do not claim to meet state-of-art detection based protection (such as <cit.>, <cit.>, <cit.> and <cit.>), we regard these results as significant ones, particularly since we are deeply convinced that an efficient defense strategy against adversarial examples will necessary be a composition of several protection schemes as it the case in other security domains such as efficient countermeasures against physical attacks for cryptographic systems which combine masking, hiding and redundancy principles.   





















 
 













§ CONCLUSION

In this article, we show experimentally on CIFAR10 and SVHN and state-of-the-art gradient-based and gradient-free attacks that quantization in itself offers very poor protection against adversarial examples crafted by adversaries having access to the model or able to query it. 

We find that activation quantization can lead to gradient masking. We verify experimentally that the efficiency of some gradient-based and gradient-free attacks can thus be tampered but other gradient-based or gradient-free attacks do not suffer from gradient masking, because of the usage of a STE to approximate gradients, or the optimization procedure begin well-suited for noisy functions.
Eventually, we demonstrate poor transferability capacities between classical models and quantized models, and between quantized models with different bitwidths. We explain this by the quantization shift phenomenon which ruins adversarial effects and gradients misalignment.

As an exploratory work and logical consequence of the transferability results, we analyze the impact of considering an ensemble of quantized models in order to filter out adversarial examples with a minimum impact on the natural accuracy. Such an ensemble method, like any other detection-based approach, suffers from a narrow threat model since the defense is useless with an attacker aware of the implementation details of the model in the target device <cit.>. However, for black-box paradigms, the use of quantized ensemble may have an interesting impact on the transferability when associated to other and complementary defense mechanisms. 

As an important outcome of these experiments, we believe that the characteristics of embedded models particularly induced by quantization approaches (weights or activation outputs) have to be taken into consideration in order to design suitable and efficient protection schemes. These defense strategies for embedded models will be the purpose of future works,

since robustness requirements will obviously become more and more compulsory as critical tasks (as well as processed data) will be performed thanks to a growing variety of devices. 











ieeetr
 






§ NETWORKS ARCHITECTURE











§ ATTACKS PARAMETERS



For ZOO and CWl2, we noticed that results between 100 and 1000 iterations were almost similar, the adversarial accuracy almost never decreased and the l_2 distortion for the two attacks decreased proportionally. For computation time issues we then chose to perform the attack with 100 iterations, as this does not change any interpretations of our results.


The value of κ for the CWl2 attack is set to 0 when considering an adversary in the white-box setting (see Section <ref>). Otherwise, in particular for transfer-based attacks, this parameter is tuned (see Section <ref> for details).



      











§ COMPLETE TRANSFERABILITY RESULTS
     
   

In the following tables, "–" denotes a value which can not be computed. For example, the l_2 distortion of successful adversarial examples for an attack can not be computed when the adversarial accuracy of the target model against this attack equals 1.

We summarize the reference of the transferability tables where w_ia_j designates a model with a i-bit quantization of the weights and a j-bit quantization of the activation values.


                          
               








  




 

               
               


 





               

 


       
               

 



  



 


                
               






  



 







 


 


 


 



           
               
               

       

 
 
               


