<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Generating Counterfactual Images: Towards a C2C-VAE Approach</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Ziwei Zhao</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>David Leake</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Xiaomeng Ye</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>David Crandall</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Indiana University, Luddy School</institution>
          ,
          <addr-line>Bloomington, Indiana, 47408</addr-line>
          <country country="US">U.S.A</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>Generating semi-factual and counterfactual explanations from images requires methods for extracting and adjusting appropriate image features. This short paper presents initial research on a counterfactual generation method for images based on class-to-class variational autoencoders (C2C-VAEs). Initial experiments illustrate substantial speed increase in counterfactual generation while suggesting that the method achieves reasonable counterfactual quality compared to the state of the art. The paper closes by discussing tradeofs of the approach.</p>
      </abstract>
      <kwd-group>
        <kwd>eol&gt;Class-to-class</kwd>
        <kwd>Counterfactual</kwd>
        <kwd>Explanation</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>
        the diference patterns between two classes. Given a standard VAE that can extract a feature
vector from a case, a C2C-VAE can embed the diference between two feature vectors into a
diference embedding or reconstruct a feature diference from a diference embedding. Previously
used to generate creative samples from limited data [3], a C2C-VAE can modify a source case 
into a target case , where the line connecting  and  in the embedding space possesses desirable
characteristics for generating counterfactuals: (
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) Most, if not all, of the line lies within the VAE
embedding distribution (so cases on the line are valid), (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) The line follows a straightforward
modification between the two classes (modifications are sparse), and (
        <xref ref-type="bibr" rid="ref3">3</xref>
        ) The line also allows
perturbation of the source or the line itself (cases can be diverse). This paper describes CVC and
initial results illustrating tradeofs between CVC and PIECE, including a substantial speedup
using CVC. The paper closes with some future directions. This paper focuses on the generation
of counterfactuals, but semi-factuals could be similarly generated.
      </p>
    </sec>
    <sec id="sec-2">
      <title>2. Background</title>
      <p>Desiderata for Counterfactuals: For a given query  of class , a counterfactual is a case
 of a diferent class , and a semi-factual is a case  of the class . Keane and Smyth [4]
propose three criteria for good counterfactuals: A good counterfactual  avoids prolixity (it is
minimally diferent from the query ), achieves sparsity (it difers from the query in the fewest
features) and has plausibility (The counterfactual is realistic for the domain).
The PIECE Approach to Counterfactual Generation: Given a query Image , a GAN
generator  and a CNN classifier to be explained , PIECE first performs GAN inversion,
locating a latent vector  using gradient descent such that () = . PIECE then modifies the
penultimate layer output of CNN  = () to ′ by identifying exceptional features according
to the weight vector of the last layer. The counterfactual output (′) is generated by optimizing
′ such that the MSE loss between ′ and ((′)) is minimized.</p>
      <p>The C2C-VAE Approach: A C2C-VAE [3] is a type of variational autoencoder that learns an
embedding space of the diference pattern between features of two classes [ 3]. Because C2C-VAE
works with case features, it requires a feature extractor  for domains in which pre-extracted
features are not available (e.g. images) and, if new case generation is desired, a procedure  ′ to
reconstruct a case from a feature vector. Both the feature extractor and case constructor can be
implemented by the encoder and decoder of a standard variational autoencoder.</p>
      <p>Given a pair of cases  and , their features are  () and  (), and the feature diference
Δ(, ) =  () −  (). C2C-VAE encodes Δ using an encoder function  as (&lt; Δ, ,  &gt;)
and decodes this embedding using a decoder function ′ as  Δ′ = ′((&lt; Δ, ,  &gt;).
With its encoder and decoder, C2C-VAE can sample a new feature diference embedding (&lt;
Δ, ,  &gt;) from a normal distribution and construct the corresponding feature diference
Δ = ′((&lt; Δ, ,  &gt;). C2C-VAE can also synthesize a case  of class  by adapting a
source case  as  =  ′( () − Δ) (See Figure 1).</p>
      <p>VAE
encoder f
encoder f
case s
case t
reconstruction loss +
Regularizer (gaussian)
f(s)
f(t)
decoder f'
recovered
sample f'(f(t))
fΔ = f(s) - f(t)</p>
      <p>C2C-VAE
Conditional Parameter</p>
      <p>Cs + Ct
encoder g
feature
difference fΔ
reconstruction loss +
Regularizer (gaussian)
difference encoding
g(&lt;fΔ, Cs,Ct&gt;)</p>
      <p>decoder g'
recovered difference
g'(g(&lt;fΔ, Cs,Ct&gt;),&lt;Cs,Ct&gt;)
3. Using C2C-VAE to Generate Counterfactuals:
In previous work applying C2C-VAE to creative case generation [3], we noticed that the
generated case  often preserves the visual characteristics of . We hypothesized that this is due to
the encoder-decoder pair of C2C-VAE,  and ′, learning to recognize the feature diferences
in Δ(, ) =  () −  () more related to class change  to , therefore ignoring other less
related features. This relates the sparsity needed for a good counterfactual, as shown in Figure 2.</p>
      <p>Following the core design of C2C-VAE in Section 2, given a query  of the class , C2C-VAE
can be used to generate a guide  of another class . A counterfactual  of the class  can
be found on the interpolation between  () and  () such that:  is near the boundary of 
and  (avoiding prolixity);  is generated following an average diference Δ(, ) pattern
(sparsity, explained in the previous paragraph); And  is interpolated from  () and  ()
within the embedding space of a standard VAE, thus conforming to the training data distribution
(plausibility). Diversity of  can be introduced by perturbing either  or Δ(, ).
3.1. The CVC Algorithm to Generate Counterfactuals
The CVC counterfactual generation algorithm is based on C2C-VAE and the Native Guide
technique [5]. CVC has two steps: retrieval and adaptation.</p>
      <p>Retrieval: CVC first randomly samples  random vectors 1 . . .  from a normal
distribution with mean 0 and standard deviation  (e.g.  = 1) in the feature diference embedding
(a) Retrieval Process of CVC
(b) Adaptation Process of CVC
space of C2C-VAE. Given query , its class  and a target class , CVC uses C2C-VAE to
generate K feature diferences  Δ′1 . . .  Δ′ by decoding 1 . . .  using equation 1.</p>
      <p>Δ′ = ′(&lt; , ,  &gt;)</p>
      <p>CVC then generates K guide candidate cases 1, ...,  such that  =  ′( () −  Δ′), and
selects  as the guide case  that minimizes mean squared error  (, ) in pixel space.
Figure 3a shows an example of the retrieval step. In contrast to the guide feature ′ of PIECE,
the CVC guide  can be directly visualized, therefore providing more explanability.</p>
      <p>Adaptation: After selecting guide  and its corresponding VAE feature  () =  () −  Δ′,
CVC interpolates between  () and  () in the VAE’s latent space to construct counterfactuals
 (or semi-factuals  ), as shown in equation 2.</p>
      <p>
        =  ′((1 −  ) *  () +  *  ()), 0 ≤  ≤ 1
In equation 2,  is a variable that determines the relative weight of interpolation between query
 and guide . A small  value (&lt;0.5) means the output is more similar to , and a large  value
(&gt;0.5) means the output is more similar to . As shown in figure 3b, CVC is able to synthesize
meaningful results for diferent  values. The diferent values of  allow CVC to find  and 
that are closest to the decision boundary. For qualitative evaluation, we found that  = 0.5 was
suitable to visualize the diference between query, counterfactual and guide in our test domain.
(
        <xref ref-type="bibr" rid="ref1">1</xref>
        )
(
        <xref ref-type="bibr" rid="ref2">2</xref>
        )
(a) Counterfactual from class 3 to 5
(b) Counterfactual from class 8 to 6
      </p>
    </sec>
    <sec id="sec-3">
      <title>4. Evaluation</title>
      <p>
        We performed an ablation study comparing adaptation results using (
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) the CVC retrieved case
and (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) the average case of the target class as guide. Figure 4 illustrates the observed trend that
using the CVC-retrieved guide better preserves query characteristics.
      </p>
      <p>
        We compared CVC to PIECE for the incorrect classifications test-set from Kenny and Keane
[2]. We evaluated PIECE under two settings: (
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) GAN inversion is accurate and pre-calculated
before testing, and (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) GAN inversion is calculated at test time using gradient descent. For (
        <xref ref-type="bibr" rid="ref1">1</xref>
        ),
we used the oficial implementation of PIECE provided by its authors. For (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ), we implemented
gradient descent following the equation in their paper. Because more efective methods of GAN
inversion exist, processing time for an application using PIECE would be likely to fall between
(
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) and (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ).
      </p>
      <p>As observed by Kenny and Keane, it is dificult to quantitatively assess the counterfactual
desiderata for image data. We measured both eficiency and proximity:
• Inference time: Time used to generate each image.
• SSIM and PSNR: We calculate Structural Similarity Index (SSIM) and Peak Signal to
Noise Ratio (PSNR) between query and generated counterfactual image to measure their
proximity.</p>
    </sec>
    <sec id="sec-4">
      <title>5. Conclusion</title>
      <p>We proposed a novel counterfactual generation algorithm (CVC) that significantly reduces
computational time comparing to the current state-of-the-art. Initial qualitative results suggest
1https://drive.google.com/drive/folders/1bZ_oy7eFt7LubmVSXmrOIIenO2yKwlW9?usp=sharing
comparable quality counterfactuals. As a benefit inherited from C2C-VAE, CVC is applicable to
one-shot learning settings (however this is not illustrated in this study). In addition, CVC does
not require weight vectors of any layers of the CNN classifier to be explained, so is applicable
to any black-box classifier.</p>
      <p>This paper presents initial work. Additional evaluation is needed on other data sets and for
image quality. The limited image reconstruction quality of the “vanilla" VAE structure we are
currently using may prevent CVC from generating plausible results on higher resolution and
more realistic images, so our future work will also involve testing other image synthesis models
such as GANs. Another direction is to apply CVC to counterfactual generation for tabular data.</p>
    </sec>
    <sec id="sec-5">
      <title>6. Acknowledgment</title>
      <p>This work was funded by the Department of the Navy, Ofice of Naval Research (Award
N0001419-1-2655).</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>M. T.</given-names>
            <surname>Keane</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E. M.</given-names>
            <surname>Kenny</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E.</given-names>
            <surname>Delaney</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            <surname>Smyth</surname>
          </string-name>
          ,
          <article-title>If only we had better counterfactual explanations: Five key deficits to rectify in the evaluation of counterfactual XAI techniques</article-title>
          ,
          <source>in: Proceedings of the Thirtieth International Joint Conference on Artificial Intelligence, IJCAI-21</source>
          , ijcai.org,
          <year>2021</year>
          , pp.
          <fpage>4466</fpage>
          -
          <lpage>4474</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>E. M.</given-names>
            <surname>Kenny</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M. T.</given-names>
            <surname>Keane</surname>
          </string-name>
          ,
          <article-title>On generating plausible counterfactual and semi-factual explanations for deep learning</article-title>
          ,
          <source>in: Proceedings of the AAAI Conference on Artificial Intelligence</source>
          , AAAI Press,
          <year>2021</year>
          , pp.
          <fpage>11575</fpage>
          -
          <lpage>11585</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>X.</given-names>
            <surname>Ye</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Z.</given-names>
            <surname>Zhao</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Leake</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Crandall</surname>
          </string-name>
          ,
          <article-title>Generation and evaluation of creative images from limited data: A classs-to-class VAE approach</article-title>
          ,
          <source>in: Proceedings of the Thirteenth International Conference on Computational Creativity</source>
          ,
          <year>2022</year>
          . https://computationalcreativity.net/iccc22/ conference-program/.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>M. T.</given-names>
            <surname>Keane</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            <surname>Smyth</surname>
          </string-name>
          ,
          <article-title>Good counterfactuals and where to find them: A case-based technique for generating counterfactuals for explainable AI (XAI)</article-title>
          ,
          <source>in: Case-Based Reasoning Research and Development, ICCBR-21</source>
          , Springer, Cham,
          <year>2020</year>
          , pp.
          <fpage>163</fpage>
          -
          <lpage>178</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>E.</given-names>
            <surname>Delaney</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Greene</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M. T.</given-names>
            <surname>Keane</surname>
          </string-name>
          ,
          <article-title>Instance-based counterfactual explanations for time series classification</article-title>
          ,
          <source>in: Case-Based Reasoning Research and Development, ICCBR-21</source>
          , Springer,
          <year>2021</year>
          , pp.
          <fpage>32</fpage>
          -
          <lpage>47</lpage>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>