mrzjy's picture
Update README.md
23d1d33 verified
metadata
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 English 138,941
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 (majority) or from Imagenet-Sketch.

Data Processing

  • 1) ASCII Art Conversion from Image: All images are simply converted to ASCII art by 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:
# 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:
# template user instruction
Draw {caption} in ASCII art{instruction}

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

A real training sample:

{
    "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)
"""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.
"""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)
"""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.