File size: 1,589 Bytes
2115186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datasets import load_dataset
from torchvision.transforms import InterpolationMode
from torchvision.transforms import functional as F


def art2prompt(example):
    image = example["image"]
    image = F.resize(image, 512, InterpolationMode.LANCZOS)

    artist = example["artist_str"].replace("-", " ").title()
    if example["genre_str"] == "Unknown Genre":
        genre = "painting"
    else:
        genre = example["genre_str"].replace("_", " ")

    style = example["style_str"].replace("_", " ").lower()

    captions = [
        # a landscape in the style of Vincent Van Gogh
        f"a {genre} in the style of {artist}",
        # a landscape in the style of realism
        f"a {genre} in the style of {style}",
        # a realism painting by Vincent Van Gogh
        f"a {style} painting by {artist}",
        # a landscape by Vincent Van Gogh
        f"a {genre} by {artist}",
    ]

    return {"text": captions, "image": image}


dataset = load_dataset("huggan/wikiart", split="train")

# map the integer labels to their strings
dataset = dataset.map(
    lambda ex: {
        "artist_str": dataset.features["artist"].int2str(ex["artist"]),
        "genre_str": dataset.features["genre"].int2str(ex["genre"]),
        "style_str": dataset.features["style"].int2str(ex["style"]),
    },
    remove_columns=["artist", "genre", "style"],
)

# generate prompts from attributes
dataset = dataset.map(
    art2prompt, remove_columns=["artist_str", "genre_str", "style_str"], num_proc=8, writer_batch_size=100
)

dataset.push_to_hub("fusing/wikiart_captions", split="train")