File size: 8,398 Bytes
aaadf85
 
 
 
 
 
 
63c7686
aaadf85
 
63c7686
aaadf85
 
23d1d33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
---
license: apache-2.0
task_categories:
- text-generation
language:
- en
tags:
- art
- ascii-art
- visual
- SFT
size_categories:
- 100K<n<1M
---

# Data for LLM ASCII Art
This repo contains open-sourced SFT data for fine-tuning LLMs on ASCII Art Generation.

## Dataset Links

|                                                    Link                                                          |     Language      |  Size   |
|:----------------------------------------------------------------------------------------------------------------:|:-----------------:|:-------:|
|           [ascii_art_generation_140k](https://huggingface.co/datasets/mrzjy/ascii_art_generation_140k)           |      English      | 138,941 |
| [ascii_art_generation_140k_bilingual](https://huggingface.co/datasets/mrzjy/ascii_art_generation_140k_bilingual) | Chinese & English | 138,941 |

## Data Preparation

### Training data description

The training data consists of 138,941 ASCII arts instruction-response samples for LLMs to perform SFT.

The source images of these samples are either from [LAION-COCO-NLLB](https://huggingface.co/datasets/visheratin/laion-coco-nllb) (majority) or from [Imagenet-Sketch](https://github.com/HaohanWang/ImageNet-Sketch). 

**Data Processing**

- **1) ASCII Art Conversion from Image:** All images are simply converted to ASCII art by [ascii-image-converter](https://github.com/TheZoraiz/ascii-image-converter) through the following command:

```
ascii-image-converter path/to/image -m " .+#@/()" -H 20 --negative
```

- **2) Blank Space Cropping:** Crop horizontal and vertical blank spaces to remove redundant space tokens. See the example below:

```example
# Illustration of raw ascii art output
# by ascii-image-converter

# ============== Top corner ========================= 
                                                   \n
                                                   \n
        .               .#.                        \n
         ++       ..   +++++........               \n
          +#+.   .##+++++++++..............        \n
           .++##########++++......++...++++..      \n
            +##.+##++#++++...+++.++.....           \n
            +.  ++ .#+.     .+..                   \n
                                                   \n
                                                   \n
                                                   \n
# ============== Bottom corner ======================

# After Blank Space Cropping
# ============== Top corner ========================= 
.               .#.\n
 ++       ..   +++++........\n
  +#+.   .##+++++++++..............\n
   .++##########++++......++...++++..\n
    +##.+##++#++++...+++.++.....\n
    +.  ++ .#+.     .+..\n
# ============== Bottom corner ======================
```

- **3) Instruction-Response Sample Format:** We use a simple prompt template to format our training sample:

```prompt
# template user instruction
Draw {caption} in ASCII art{instruction}

# template for assistant response
\```
{ascii_txt}
\```
```

A real training sample:

```json
{
    "id": "6d8c10c8-df57-461d-8a88-2fe74f578f6f",
    "conversations": [
        {
            "role": "user",
            "content": "Draw The legs and heels are wearing orange shoes. in ASCII art in small size"
        },
        {
            "role": "assistant",
            "content": "```\n       .........    ..    ...\n       ........     ...   ...\n       ........     ...   ...\n       ........     ...   ..\n        .......     ...  ...\n        ......      ...  ...\n        ......      ... ...\n        ......      .......\n        .......     ......\n        ....+.+#.  .......\n       .......###..... ...+.\n       ......##@@........+##.\n       .....#@###... ....####\n      .....@@+ .+..  ...+###.\n     .+. .#@.  .... . .##+++\n.++######@#.   ....  .##. .+\n++##++##++...++++...+##.  .+\n         +##@#########.    .\n         ............\n```"
        }
    ],
    "meta": {
        "width": 29,
        "height": 19
    }
}
```

```
# Visualization of the above sample
Instruction: Draw The legs and heels are wearing orange shoes. in ASCII art in small size
Response:
       .........    ..    ...
       ........     ...   ...
       ........     ...   ...
       ........     ...   ..
        .......     ...  ...
        ......      ...  ...
        ......      ... ...
        ......      .......
        .......     ......
        ....+.+#.  .......
       .......###..... ...+.
       ......##@@........+##.
       .....#@###... ....####
      .....@@+ .+..  ...+###.
     .+. .#@.  .... . .##+++
     .++######@#.   ....  .##. .+
++##++##++...++++...+##.  .+
         +##@#########.    .
         ............
```

**Data Filtering**

Not all processed ascii arts are of high quality. Here are several attempts to filter low-quality samples (~85% data samples are filtered, resulting our current training dataset).

- **1) Density:** Defined as 1 - the ratio of non-space character in all characters. We only keep samples with proper density (0.3 < density < 0.6)

```python
"""bad case to be filtered
@@@@@@@@///(/@#+......+#@((///////
///////////(@. .++++. ..#((///////
(((////////(#  .@@.##   #((///////
(((////////(@+. ....  ..#((///////
///////////(/@@#+++.+##@/((///////
//////((/////(((((((((((((///////(
(//((//((//(((((//((((//(/((((((((
(((((/@((//((((((/(((((((((@((((((
(((((((((((((((#@(((/((((((@/+/(((
(((((///(((((((##(((((((((((/@/(((
"""

def calculate_density(ascii_text):
    space_cnt = 0
    max_characters_per_line = max(len(l) for l in ascii_text.split("\n"))
    n_lines = len(ascii_text.split("\n"))
    for l in ascii_text.split("\n"):
        for i, c in enumerate(l):
            if c == " ":
                space_cnt += 1
        space_cnt += (max_characters_per_line - len(l))
    return 1 - space_cnt / (max_characters_per_line * n_lines)
```

- **2) Diversity:** Defined as 1 - the ratio of dot characters among all non-space characters. We filter samples of low diversity.

```python
"""bad case to be filtered
      .+.  ..  ... .. .++ .+  +.
.+.+...   ..  ..   .   .  .   .  .
....      ..  ... ..  .+  ..  .  .
.....     .+  ... ..   +  ..  .  .
....      ..  ... .    +  ..  .  .
....     ..  .+. .   ..  ..  .  .
.....     ..  ... ..  ..  ..  .  .
...       ..  ... ..  ..  ..  .  .
......   .++  .+. ..  .+  +.  .  .
"""

def calculate_diversity(ascii_text):
    dot_cnt, non_space_cnt = 0, 0
    for c in ascii_text:
        if c == ".":
            dot_cnt += 1
        if c != " ":
            non_space_cnt += 1
    return 1 - dot_cnt / non_space_cnt
```

- **3) No Isolation:** We filter samples having isolated lines (all blank space for previous 3 lines)

```python
"""bad case to be filtered
            ....
      .... ..        .
 ..+++++++........   .
 ..+++++++........   .
 ...++++++.... .     .
 ...++++.+...        .
 . ...........       .
  .  .    .......    .
 ...++........       .
 ................    .
           .++++ .. ..




+############.
#/@@@@@@@@@@@+
"""

def check_isolation(ascii_text):
    lines = ascii_text.split("\n")
    for i, l in enumerate(lines):
        has_character = False
        for c in l:
            if c != " ":
                has_character = True
                break

        if has_character and i > 3:
            # check whether there's prev and after 2 lines
            isolation_from_previous = True
            for prev_i in range(max(0, i-3), i):
                if not all(c == " " for c in lines[prev_i]):
                    isolation_from_previous = False
                    break
            if isolation_from_previous:
                return True
    return False
```

**Bilingual Version**

Apart from the English dataset, we add a Chinese-English bilingual version dataset, where 50% of the image captions are changed to Chinese (thanks to the translations from laion-coco-nllb).

```{'zh': 69777, 'en': 69164}```

Note that the total number of training samples are always 138,941.

## Limitations

- Color: Current implementation only supports black and white ascii art generation (Although there're inevitably color descriptions in training samples, we have no choice but ignore them for now.). Adding additional prediction head for RGB colors could be worth trying. You can find colored ascii art examples in [ascii-image-converter](https://github.com/TheZoraiz/ascii-image-converter).