What You See is What it Means! Semantic Representation Learning of Code based on Visualization and Transfer Learning

Patrick Keller, Laura Plein, Tegawendé F. Bissyandé, Jacques Klein, Yves Le Traon

TL;DR

WySiWiM ("What You See Is What It Means") is a novel approach that treats source code as an image rather than text, bypassing traditional natural language processing (NLP) pipelines. By feeding visual renderings of code into pre-trained computer vision models (ResNets), it leverages transfer learning to identify semantic code clones, matching or even outperforming state-of-the-art syntax-tree-based methods.

What Problem Does This Paper Address?

Identifying "code clones"—fragments of code that perform identical functions—is vital for bug detection, software maintenance, and automated program repair. While textually similar clones are relatively easy to spot, Type-4 semantic clones (code fragments that look completely different but behave identically) remain a massive challenge.

Current state-of-the-art techniques rely heavily on NLP-inspired methods, treating code as text tokens or parsing it into complex Abstract Syntax Trees (ASTs). However, these approaches are often fragile: they require constant retraining to learn new vocabularies, can be easily fooled by minor lexical changes, and struggle with scale. The authors of this paper ask a creative question: Can we instead mimic how human programmers instantly recognize code functionality through visual perception and structural patterns?

Methods

The authors introduce WySiWiM, a pipeline that converts source code into a visual representation and processes it using deep learning models from the computer vision domain.

The approach operates in two primary stages:

  1. Visual Rendering: The source code is converted into a 2D PNG image. The authors experiment with four distinct rendering variations:
  2. Plain Text: A simple black-and-white image of the raw text.
  3. Color Syntax Highlighting: Mimicking a standard IDE view to emphasize language constructs.
  4. Geometric Syntax Highlighting: Replacing code keywords with distinct geometric icons.
  5. AST in Condensed Format: Converting the code's abstract syntax tree into a condensed graph layout.
  6. Deep Feature Extraction & Transfer Learning: Instead of training a massive neural network from scratch, the code images are fed into a ResNet (ResNet18 or ResNet50) pre-trained on the ImageNet dataset.
Figure 1: Deep feature extraction (a.k.a, visualization-based code embedding)

For Code Classification (predicting what a single piece of code does), the network is fine-tuned by adjusting its final output layer. For Clone Detection (determining if two pieces of code are functional matches), feature vectors are extracted from the network's penultimate layer. The absolute difference between the two vectors is calculated and passed to a traditional binary classifier (such as an SVM, k-NN, or a simple neural network).

Figure 1: Deep feature extraction (a.k.a, visualization-based code embedding)

Datasets

The approach was evaluated using two widely accepted benchmarks:

  • Open Judge (OJ): A dataset containing C programming solutions submitted by students across 104 distinct programming problems (500 solutions per problem). This was used to evaluate multi-class code classification.
  • BigCloneBench (BCB): A massive dataset of real-world Java code clones. The authors utilized a pre-filtered subset containing 20,000 Type-4 clone pairs and 20,000 non-clone pairs to ensure a direct, unbiased comparison with existing baselines.

Key Insights and Ideas

  • Vision Models Understand Code Structure: Remarkably, neural networks trained on everyday objects (like cats, cars, and dogs) can successfully transfer their structural pattern-recognition capabilities to abstract code layouts.
  • The Power of ASTs and Colors: Not all visual representations are created equal. The Condensed AST format and Color Syntax Highlighting yielded the best results. This confirms that visual cues helpful to humans—like IDE coloring and structural branching—are also highly informative to convolutional neural networks.
  • The Benchmark Duplicate Trap: During development, the authors discovered a major threat to validity in existing clone detection benchmarks: a high volume of "clone duplicates" (ranging from 30% to 60%). When these duplicates are strictly removed, performance drops across all models, proving that current datasets make the clone detection task appear easier than it actually is in the wild.

Key Results

  • Clone Detection Outperformance: On the BigCloneBench dataset, WySiWiM achieved an F1-score of 94.8% (95.4% Precision, 94.3% Recall), outperforming the state-of-the-art ASTNN model (93.7% F1-score). WySiWiM demonstrated a significantly better balance between precision and recall.
  • Competitiveness in Classification: On the Open Judge dataset, WySiWiM achieved 89.7% accuracy using a pre-trained ResNet50. While this slightly trailed specialized text-based models like TBCNN (94.0%) and ASTNN (98.2%), it proves that a pure vision approach can be highly competitive without any language-specific vocabulary training.

Limitations and Future Work

  • Resolution and Information Loss: Standard ResNets enforce a strict input size limitation of 224x224 pixels. Compressing large blocks of code into this small footprint causes a severe loss of fine-grained lexical details. The authors suggest exploring image-slicing techniques to preserve resolution.
  • Granularity Constraints: The current model is optimized for method-level code blocks. It struggles with full-program classification (as seen in the Open Judge results). Future work could leverage object localization techniques to detect multiple distinct functionalities within a single large file.
  • Color Underutilization: Most code renderings use color sparsely, failing to exploit the full three-channel (RGB) capability of modern CNNs. Infusing richer color-coding schemes directly into AST graphs is a promising avenue for future research.

Summary updated

BibTeX
@article{keller2020what,
  author = {Patrick Keller and Laura Plein and Tegawendé F. Bissyandé and Jacques Klein and Yves Le Traon},
  title = {{What You See is What it Means! Semantic Representation Learning of Code based on Visualization and Transfer Learning}},
  journal = {ACM Trans. Softw. Eng. Methodol.},
  year = {2020},
  doi = {10.1145/3485135},
}
Figure