Create new file
Browse files- create_wikiart.py +49 -0
create_wikiart.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
from torchvision.transforms import InterpolationMode
|
3 |
+
from torchvision.transforms import functional as F
|
4 |
+
|
5 |
+
|
6 |
+
def art2prompt(example):
|
7 |
+
image = example["image"]
|
8 |
+
image = F.resize(image, 512, InterpolationMode.LANCZOS)
|
9 |
+
|
10 |
+
artist = example["artist_str"].replace("-", " ").title()
|
11 |
+
if example["genre_str"] == "Unknown Genre":
|
12 |
+
genre = "painting"
|
13 |
+
else:
|
14 |
+
genre = example["genre_str"].replace("_", " ")
|
15 |
+
|
16 |
+
style = example["style_str"].replace("_", " ").lower()
|
17 |
+
|
18 |
+
captions = [
|
19 |
+
# a landscape in the style of Vincent Van Gogh
|
20 |
+
f"a {genre} in the style of {artist}",
|
21 |
+
# a landscape in the style of realism
|
22 |
+
f"a {genre} in the style of {style}",
|
23 |
+
# a realism painting by Vincent Van Gogh
|
24 |
+
f"a {style} painting by {artist}",
|
25 |
+
# a landscape by Vincent Van Gogh
|
26 |
+
f"a {genre} by {artist}",
|
27 |
+
]
|
28 |
+
|
29 |
+
return {"text": captions, "image": image}
|
30 |
+
|
31 |
+
|
32 |
+
dataset = load_dataset("huggan/wikiart", split="train")
|
33 |
+
|
34 |
+
# map the integer labels to their strings
|
35 |
+
dataset = dataset.map(
|
36 |
+
lambda ex: {
|
37 |
+
"artist_str": dataset.features["artist"].int2str(ex["artist"]),
|
38 |
+
"genre_str": dataset.features["genre"].int2str(ex["genre"]),
|
39 |
+
"style_str": dataset.features["style"].int2str(ex["style"]),
|
40 |
+
},
|
41 |
+
remove_columns=["artist", "genre", "style"],
|
42 |
+
)
|
43 |
+
|
44 |
+
# generate prompts from attributes
|
45 |
+
dataset = dataset.map(
|
46 |
+
art2prompt, remove_columns=["artist_str", "genre_str", "style_str"], num_proc=8, writer_batch_size=100
|
47 |
+
)
|
48 |
+
|
49 |
+
dataset.push_to_hub("fusing/wikiart_captions", split="train")
|