Skip to main content
QUICK REVIEW

[Paper Review] Code2Image: Intelligent Code Analysis by Computer Vision Techniques and Application to Vulnerability Prediction

Zeki Bilgin|arXiv (Cornell University)|May 7, 2021
Software Engineering ResearchComputer Science23 references6 citations
TL;DR

This paper proposes Code2Image, a novel method that converts source code Abstract Syntax Trees (ASTs) into visual representations for direct input into deep learning models. By leveraging computer vision techniques on these code images, the approach enables automatic feature extraction and achieves superior performance in vulnerability prediction compared to state-of-the-art methods, particularly on imbalanced datasets.

ABSTRACT

Intelligent code analysis has received increasing attention in parallel with the remarkable advances in the field of machine learning (ML) in recent years. A major challenge in leveraging ML for this purpose is to represent source code in a useful form that ML algorithms can accept as input. In this study, we present a novel method to represent source code as image while preserving semantic and syntactic properties, which paves the way for leveraging computer vision techniques to use for code analysis. Indeed the method makes it possible to directly enter the resulting image representation of source codes into deep learning (DL) algorithms as input without requiring any further data pre-processing or feature extraction step. We demonstrate feasibility and effectiveness of our method by realizing a vulnerability prediction use case over a public dataset containing a large number of real-world source code samples with performance evaluation in comparison to the state-of-art solutions. Our implementation is publicly available.

Motivation & Objective

  • To address the challenge of representing source code in a form suitable for deep learning models without extensive feature engineering.
  • To preserve syntactic and semantic information from source code during representation by leveraging ASTs.
  • To enable direct application of computer vision-based deep learning models to source code analysis via image-based representation.
  • To evaluate the effectiveness of the proposed method in a real-world vulnerability prediction use case.
  • To compare the performance of Code2Image against state-of-the-art approaches on a public dataset of real-world code samples.

Proposed method

  • The method converts the Abstract Syntax Tree (AST) of source code into a visual image representation that preserves structural and semantic relationships.
  • The AST is rendered as a tree diagram with non-intersecting edges to maintain readability and structural fidelity, ensuring parent-child node relationships are clearly visualized.
  • The resulting image is optimized for size and compactness while retaining the topological properties of the AST to support efficient deep learning inference.
  • The image representation is directly fed into convolutional neural networks (CNNs) without requiring additional feature extraction or pre-processing steps.
  • A separate deep learning model is trained for each vulnerability type to minimize validation loss, enabling targeted optimization per class.
  • The approach leverages the success of computer vision models in image classification tasks to perform code analysis, particularly vulnerability detection.

Experimental results

Research questions

  • RQ1Can AST-based code representation be effectively transformed into a visual format that preserves semantic and syntactic information for deep learning?
  • RQ2To what extent can computer vision techniques be applied to source code analysis through image-based code representation?
  • RQ3How does the performance of the proposed Code2Image method compare to state-of-the-art approaches in vulnerability prediction?
  • RQ4Does the use of full ASTs in image form lead to better performance than partial AST representations in vulnerability detection tasks?
  • RQ5How does the method perform on highly imbalanced vulnerability categories, such as CWE-469 with only 32 positive samples in the test set?

Key findings

  • Code2Image achieved an F1 score of 0.528 for CWE-119, outperforming Ast2vec’s maximum F1 of 0.424 on the same vulnerability category.
  • For the most imbalanced category, CWE-469 (0.56% positive samples), Code2Image achieved an F1 score of 0.140, compared to Ast2vec’s 0.082, demonstrating superior performance on rare vulnerabilities.
  • On CWE-476, Code2Image achieved an F1 score of 0.617, slightly higher than Ast2vec’s 0.599, indicating strong performance even on moderately balanced categories.
  • For CWE-others, Code2Image achieved an F1 score of 0.335, significantly outperforming Ast2vec’s 0.283, highlighting consistent gains across diverse vulnerability types.
  • The average inference time per code sample was 20.8 milliseconds, indicating efficient deployment potential for real-world static analysis pipelines.
  • The method’s superiority is attributed to the use of full ASTs and per-vulnerability-type model specialization, reducing information loss and improving optimization.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.