# Interpreting and Steering Features in Images

*![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/576cc93ecd702379d32bed26bd1abd91abb484e33854e75a.png)We trained a SAE to find sparse features in image embeddings. We found many meaningful, interpretable, and steerable features. We find that steering image diffusion works surprisingly well and yields predictable and high-quality generations.*

*You can see the* [*feature library here*](https://www.featurelab.xyz/)*. We also have an* [*intervention playground*](https://www.featurelab.xyz/playground?token=friend_of_gytis) *you can try.*

Key Results
===========

*   We can extract interpretable features from CLIP image embeddings.
*   We observe a diverse set of features, e.g. golden retrievers, there being two of something, image borders, nudity, and stylistic effects.
*   Editing features allows for conceptual and semantic changes while maintaining generation quality and coherency.
*   We devise a way to preview the causal impact of a feature, and show that many features have an explanation that is consistent with what they activate for and what they cause.
*   Many feature edits can be stacked to perform task-relevant operations, like transferring a subject, mixing in a specific property of a style, or removing something.

Interactive demo
----------------

*   [Visit the feature library](https://www.featurelab.xyz/) of over ~50k features to explore the features we find. 
*   Our main result, the [intervention playground](https://www.featurelab.xyz/playground?token=friend_of_gytis), is now available for public use.
*   The weights are open source -- here's [a notebook to try an intervention](https://colab.research.google.com/drive/1m6IRqKaRUWRL-d2PC82nslb7kmAgjoKV?usp=sharing).

Introduction
============

We trained a [sparse autoencoder](https://transformer-circuits.pub/2023/monosemantic-features) on 1.4 million image embeddings to find monosemantic features. In our run, we found 35% (58k) of the total of 163k features were alive, which is that they have a non-zero activation for any of the images in our dataset.

We found that many features map to human interpretable concepts, like dog breeds, times of day, and emotions. Some express quantities, human relationships, and political activity. Others express more sophisticated relationships like organizations, groups of people, and pairs.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/af7f4c75442a6c343688b79cb0f4e9f99fe72758e747aeee.png)

Some features were also safety relevant.We found features for nudity, kink, and sickness and injury, which we won’t link here.

Steering Features
=================

Previous work found similarly interpretable features, [e.g. in CLIP-ViT](https://www.lesswrong.com/posts/bCtbuWraqYTDtuARg/towards-multimodal-interpretability-learning-sparse-2). We expand upon their work by training an SAE in a domain that allows for easily testing interventions.

To test an explanation derived from describing the top activating images for a particular feature, we can intervene on an embedding and see if the generation (the decoded image) matches our hypothesis. We do this by steering the features of the image embedding and re-adding the reconstruction loss. We then use an open source diffusion model, [Kadinsky 2.2](https://github.com/ai-forever/Kandinsky-2), to diffuse an image back out conditional on this embedding.

Even though an image typically has many active features that appear to encode a similar concept, intervening on one feature with a much higher activation value still works and yields an output without noticeable degradation of quality.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/48cb1db1be1156b2776208187bf4bbf006ac0695c160f6a2.png)

We built an intervention playground where users could adjust the features of an image to test hypotheses, and later found that the steering worked so well that users could perform many meaningful tasks while maintaining an output that is comparably as coherent and high quality as the original.

For instance, the subject of one photo could be transferred to another. We could adjust the time of day, and the quantity of the subject. We could add entirely new features to images to sculpt and finely control them. We could pick two photos that had a semantic difference, and precisely transfer over the difference by transferring the features. We could also stack hundreds of edits together.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/662b9d4ee94c19cd1f225ab7c1b69b42f444ad7b465df47f.png)

Qualitative tests with users showed that even relatively untrained users could learn to manipulate image features in meaningful directions. This was an exciting result, because it could suggest that feature space edits could be useful for setting inference time rules (e.g. banning some feature that the underlying model learned) or as user interface affordances (e.g. tweaking feature expression in output).

Discovering and Interpreting Features
=====================================

In order to learn what each feature actually controls, we developed a feature encyclopedia where we see the top activations, coactivating features, and features similar in direction.

To quickly compare features, we also found it useful to apply the feature to a standard template to generate a reference photo for human comparison. We call this the feature expression icon, or just the icon for short. We include it as part of the human-interpretable reference to this feature.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/c7b2deb502d3dff97b9ad91b9415c09aace74dbcb3fe743e.png)

*From left to right: the template, and then the feature expressions of — being held, the subject being the American Robin, and there being three of something.*

The icon helps clarify and isolate the feature intervention direction. We tested 200 randomly sampled icons, and found that 88% of the samples were rated by a human as clearly displaying the intervention in question. But there are some limitations to technique. For instance, the activation value we used (7.5x the max activation in the embeddings) did not express some features, which remained very close to the default template. These features did not tend to be meaningful to intervene on, and represented concepts like text or fine compression artifacts.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/405c9b0cb093636bda2007e2f79903cf8c2fe6e2b880a433.png)

Above, we’ve shown the dimension reduced map of the top 1000 features that we found, and how they cluster into meaningful semantic categories. Many of the top activating features were about image borders, compression artifacts, and minute differences in light or shadow. Further down the list we see more semantically meaningful features, like dogs, objects, background scenes, relationships between objects, and styles. We haven’t yet begun to explore the full depth of the 58k learned features, and so have released the feature library publically to encourage more interpretation.

Autointerpretation Labels
-------------------------

We created auto-interpreted labels by using Gemini to come up with a hypothesis given (a) some top activating samples, and (b) the standard template and the feature expression icon. The feature expression icon helped clarify otherwise confusing or ambiguous features. Of these labels, the human reviewer rated 63% (in a random sample of 200) as a good or great explanation.[^aa7pkpfy13p]

There is some low hanging fruit to improve this metric. We plan to improve label precision by sampling from features that have a similar direction and asking the model to discriminate between them. We can also perform simple accuracy tests by using a LLM to predict which images conform to the label, and measuring the rate of false positives.

Training Details
================

We used the open-source image encoder, `ViT-bigG-14-laion2B-39B-b160k`, to embed 1.4 million images from the ImageNet dataset. The resulting embeddings were 1280-dimensional.

We trained the SAE without using any neuron resampling and with untied decoder and encoder weights. We selected a SAE dictionary size of 163,840, which is a 128x expansion factor on the CLIP embedding dimension. However, our run resulted in approximately 105k dead features, suggesting that the model might be undertrained on the given number of image embeddings. We plan to conduct a hyperparameter search in future work to address this issue.

The SAE reconstruction explained 88% of the variance in the embeddings. We observed that the reconstruction process often led to a loss of some aesthetic information, resulting in decreased generation quality. To mitigate this, we found the residual information lost during reconstruction by finding the difference between the reconstructed and original embedding, and added this residual back in when performing an intervention.

For image generation (decoding), we utilized [Kadinsky 2.2](https://github.com/ai-forever/Kandinsky-2)[^p991b5c96t], a pretrained open-source diffusion model.

Future Work
-----------

We're planning to continue this work on interpreting CLIP by:

1.  Performing a hyperparameter sweep to discover the upper limits of reconstruction on image embeddings
2.  Trying various strategies to improve the autointerpretation, e.g. by adding a verification step to label generation, and finding improvements to feature expression icons.
3.  Train on significantly more data, including from LAION to extract more features about style and beauty.
4.  Additional work on user interface and steerability would be helpful to see if we can gain more knowledge about how to help users steer generations in ways meaningful to them. Logit lens approaches on CLIP[^4r2vixlk8r8] could help localize why a feature is firing and might suggest better ways to modify an image to, e.g. remove or change something.

*You can replicate* [*an image steering intervention in this notebook*](https://colab.research.google.com/drive/1m6IRqKaRUWRL-d2PC82nslb7kmAgjoKV?usp=sharing)*.*

*Thanks to* [*David M*](https://www.linkedin.com/in/david-mcsharry-208612202/?originalSubdomain=ie)*,* [*Matt S*](https://www.matthewsiu.com/)*,* [*Linus L*](https://thesephist.com/) *for great conversations on the research direction, and* [*Max K*](https://a9.io/)*,* [*Mehran J*](https://x.com/mehran__jalali)*, and many others, for design feedback and support.*

*Please cite this post as “Daujotas, G. (2024, June 20). Interpreting and Steering Features in Images. https://www.lesswrong.com/posts/Quqekpvx8BGMMcaem/interpreting-and-steering-features-in-images"*

[^aa7pkpfy13p]: We didn't see a large difference between various LLM providers, and so chose Gemini because it had greater uptime. 

[^p991b5c96t]: We chose Kadinsky as it was reasonably popular and open source. We expect this to transfer over to other CLIP guided diffusion models. https://github.com/ai-forever/Kandinsky-2 

[^4r2vixlk8r8]: See ViT-Prisma by Sonia Joseph. https://github.com/soniajoseph/ViT-Prisma