| 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 = [ |
| |
| f"a {genre} in the style of {artist}", |
| |
| f"a {genre} in the style of {style}", |
| |
| f"a {style} painting by {artist}", |
| |
| f"a {genre} by {artist}", |
| ] |
|
|
| return {"text": captions, "image": image} |
|
|
|
|
| dataset = load_dataset("huggan/wikiart", split="train") |
|
|
| |
| 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"], |
| ) |
|
|
| |
| 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") |
|
|