This project focuses on enhancing visual reasoning capabilities of Large Language Models (LLMs) by training them to generate code. A key component of this work is the creation of a preference and SFT dataset derived from the GQA dataset.
The primary goal was to collect a substantial amount of data to train LLMs for visual reasoning tasks. Since existing approaches like viperGPT use few-shot learning via prompt engineering, they don't allow for direct adaptation of model weights to improve code reasoning. Therefore, this project aimed to create a dataset that encapsulates diverse forms of code-based reasoning grounded on visual questions.
To generate the dataset, six different LLMs were utilized: Llama-3.1-8b, CodeLlama-7b, Mixtral-87B, DeepSeek-Llama-8b, Qwen2.5-7b, and DeepSeek-Qwen-7b.
For all models, the recommended generation_config.json was adopted, with a key modification:
1. This was done to encourage diversity in reasoning strategies. Many GQA questions are structurally similar, and without access to the image, deterministic sampling often yields identical code. A temperature of 1, while slightly reducing per-model accuracy, provides more diverse reasoning paths, which is beneficial for modeling reasoning diversity.The generation was performed on 12,600 samples from the train_balanced_questions.json file of the balanced training split of the GQA dataset.
Once code was generated for each question, the programs were executed to obtain answers and evaluate code quality. Each outcome was categorized into one of four classes:
Note: Some edge cases, like semantically incorrect code yielding a correct result due to coincidental VLM failure, are not systematically addressed.
A system was developed to assign each evaluated program to one of these categories. These categories form a strict hierarchy, allowing for quick comparison of reasoning quality between different code pieces.
Table 3.1: Performance of each LLM on the balanced training-question set, over 12600 instances. GLIP-Large and BLIP2-Flan-T5-XL (32-bit) were used as the VLMs.
| LLM | Code Error | Runtime Error | Semantic/Inference Error | Correct |
|---|---|---|---|---|
| llama31-8b | 156 | 199 | 6018 | 6228 |
| codellama7b | 194 | 997 | 6476 | 4934 |
| mixtral87B | 296 | 1646 | 5802 | 4857 |
| deepSeekLlama8b | 1153 | 1007 | 5658 | 4783 |
| Qwen2.5-7b | 2090 | 1976 | 5057 | 3478 |
| deepSeekQwen7b | 3019 | 2026 | 4480 | 3076 |
An ideal preference hierarchy would have five distinct reasoning-quality levels:
Correct answer > Inference error > Semantic error > Runtime error > Syntax error
However, since an automated classifier cannot reliably distinguish inference errors from semantic errors, these two are merged in practice.
Practical hierarchy used by the automatic classifier:
Correct answer > Inference / semantic error > Runtime error > Syntax error
This strict ordering allows the construction of a preference system: for any two candidate codes for the same visual question, it's possible to decide which one demonstrates better reasoning. This procedure is similar to Reinforcement Learning from Human Feedback, but with automatic labeling. From these pairwise decisions, a dataset of preferences can be constructed, selecting pairs via a heuristic focusing on the most informative comparisons.
Table 3.2: Distribution of the 12600 visual question instances according to the classifications of the 6 generated codes per instance.
| Pattern | Compilation Er | Run-time Er | Sem./Inf. Er | Correct | # instances |
|---|---|---|---|---|---|
| A | ✓ | 443 | |||
| B | ✓ | ✓ | 409 | ||
| C | ✓ | ✓ | 447 | ||
| D | ✓ | ✓ | 375 | ||
| E | ✓ | ✓ | ✓ | 2209 | |
| F | ✓ | ✓ | ✓ | 1820 | |
| G | ✓ | ✓ | ✓ | 1973 | |
| H | ✓ | ✓ | ✓ | ✓ | 1198 |
| I | ✓ | 0 | |||
| J | ✓ | 0 | |||
| K | ✓ | 1 | |||
| L | ✓ | ✓ | 793 | ||
| M | ✓ | ✓ | 1008 | ||
| N | ✓ | ✓ | 1078 | ||
| O | ✓ | ✓ | ✓ | 846 | |
| Total | 12600 |
Interpretation of Table 3.2:
Across the dataset, 8,874 out of 12,600 visual questions have at least one correct answer, implying an upper-bound accuracy of approximately $8874/12600 \approx 0.7$ if all six models' knowledge could be consolidated. It's an open question whether this can be surpassed if the model generalizes transferred knowledge.
To build the preference dataset, only the 8,431 instances falling into patterns B–H were retained.
llama31-8b has the highest number of preferred codes and lowest rejected ones.llama31-8b's performance.llama31-8b generated incorrectly. The aim is to discourage past mistakes while integrating knowledge from other models.llama31-8b produced incorrect code.llama31-8b.llama31-8b.Python
54.1%
Jupyter Notebook
39.5%
HTML
3.5%
Shell
2.9%
This project focuses on enhancing visual reasoning capabilities of Large Language Models (LLMs) by training them to generate code. A key component of this work is the creation of a preference and SFT dataset derived from the GQA dataset.
The primary goal was to collect a substantial amount of data to train LLMs for visual reasoning tasks. Since existing approaches like viperGPT use few-shot learning via prompt engineering, they don't allow for direct adaptation of model weights to improve code reasoning. Therefore, this project aimed to create a dataset that encapsulates diverse forms of code-based reasoning grounded on visual questions.
To generate the dataset, six different LLMs were utilized: Llama-3.1-8b, CodeLlama-7b, Mixtral-87B, DeepSeek-Llama-8b, Qwen2.5-7b, and DeepSeek-Qwen-7b.
For all models, the recommended generation_config.json was adopted, with a key modification:
1. This was done to encourage diversity in reasoning strategies. Many GQA questions are structurally similar, and without access to the image, deterministic sampling often yields identical code. A temperature of 1, while slightly reducing per-model accuracy, provides more diverse reasoning paths, which is beneficial for modeling reasoning diversity.The generation was performed on 12,600 samples from the train_balanced_questions.json file of the balanced training split of the GQA dataset.
Once code was generated for each question, the programs were executed to obtain answers and evaluate code quality. Each outcome was categorized into one of four classes:
Note: Some edge cases, like semantically incorrect code yielding a correct result due to coincidental VLM failure, are not systematically addressed.
A system was developed to assign each evaluated program to one of these categories. These categories form a strict hierarchy, allowing for quick comparison of reasoning quality between different code pieces.
Table 3.1: Performance of each LLM on the balanced training-question set, over 12600 instances. GLIP-Large and BLIP2-Flan-T5-XL (32-bit) were used as the VLMs.
| LLM | Code Error | Runtime Error | Semantic/Inference Error | Correct |
|---|---|---|---|---|
| llama31-8b | 156 | 199 | 6018 | 6228 |
| codellama7b | 194 | 997 | 6476 | 4934 |
| mixtral87B | 296 | 1646 | 5802 | 4857 |
| deepSeekLlama8b | 1153 | 1007 | 5658 | 4783 |
| Qwen2.5-7b | 2090 | 1976 | 5057 | 3478 |
| deepSeekQwen7b | 3019 | 2026 | 4480 | 3076 |
An ideal preference hierarchy would have five distinct reasoning-quality levels:
Correct answer > Inference error > Semantic error > Runtime error > Syntax error
However, since an automated classifier cannot reliably distinguish inference errors from semantic errors, these two are merged in practice.
Practical hierarchy used by the automatic classifier:
Correct answer > Inference / semantic error > Runtime error > Syntax error
This strict ordering allows the construction of a preference system: for any two candidate codes for the same visual question, it's possible to decide which one demonstrates better reasoning. This procedure is similar to Reinforcement Learning from Human Feedback, but with automatic labeling. From these pairwise decisions, a dataset of preferences can be constructed, selecting pairs via a heuristic focusing on the most informative comparisons.
Table 3.2: Distribution of the 12600 visual question instances according to the classifications of the 6 generated codes per instance.
| Pattern | Compilation Er | Run-time Er | Sem./Inf. Er | Correct | # instances |
|---|---|---|---|---|---|
| A | ✓ | 443 | |||
| B | ✓ | ✓ | 409 | ||
| C | ✓ | ✓ | 447 | ||
| D | ✓ | ✓ | 375 | ||
| E | ✓ | ✓ | ✓ | 2209 | |
| F | ✓ | ✓ | ✓ | 1820 | |
| G | ✓ | ✓ | ✓ | 1973 | |
| H | ✓ | ✓ | ✓ | ✓ | 1198 |
| I | ✓ | 0 | |||
| J | ✓ | 0 | |||
| K | ✓ | 1 | |||
| L | ✓ | ✓ | 793 | ||
| M | ✓ | ✓ | 1008 | ||
| N | ✓ | ✓ | 1078 | ||
| O | ✓ | ✓ | ✓ | 846 | |
| Total | 12600 |
Interpretation of Table 3.2:
Across the dataset, 8,874 out of 12,600 visual questions have at least one correct answer, implying an upper-bound accuracy of approximately $8874/12600 \approx 0.7$ if all six models' knowledge could be consolidated. It's an open question whether this can be surpassed if the model generalizes transferred knowledge.
To build the preference dataset, only the 8,431 instances falling into patterns B–H were retained.
llama31-8b has the highest number of preferred codes and lowest rejected ones.llama31-8b's performance.llama31-8b generated incorrectly. The aim is to discourage past mistakes while integrating knowledge from other models.llama31-8b produced incorrect code.llama31-8b.llama31-8b.Python
54.1%
Jupyter Notebook
39.5%
HTML
3.5%
Shell
2.9%