File size: 1,840 Bytes
56238f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import json
import copy
from torch.utils.data import Dataset
import os
from PIL import Image

def geneval_save_fn(image, metadata, root_path):
    path = os.path.join(root_path, metadata['filename'])
    if not os.path.exists(path):
        os.makedirs(path, exist_ok=True)
    # save image
    image_path = os.path.join(path, "samples", f"{metadata['seed']}.png")
    if not os.path.exists(os.path.dirname(image_path)):
        os.makedirs(os.path.dirname(image_path), exist_ok=True)
    Image.fromarray(image).save(image_path)
    # metadata_path
    metadata_path = os.path.join(path, "metadata.jsonl")
    with open(metadata_path, "w") as fp:
        json.dump(metadata, fp)

class GenEvalDataset(Dataset):
    def __init__(self, meta_json_path, num_samples_per_instance, latent_shape):
        self.latent_shape = latent_shape
        self.meta_json_path = meta_json_path
        with open(meta_json_path) as fp:
            self.metadatas = [json.loads(line) for line in fp]
        self.num_instances = len(self.metadatas)
        self.num_samples_per_instance = num_samples_per_instance
        self.num_samples = self.num_instances * self.num_samples_per_instance

    def __len__(self):
        return self.num_samples

    def __getitem__(self, idx):
        instance_idx = idx // self.num_samples_per_instance
        sample_idx = idx % self.num_samples_per_instance
        metadata = copy.deepcopy(self.metadatas[instance_idx])
        generator = torch.Generator().manual_seed(sample_idx)
        condition = metadata["prompt"]
        latent = torch.randn(self.latent_shape, generator=generator, dtype=torch.float32)
        filename = f"{idx}"
        metadata["seed"] = sample_idx
        metadata["filename"] = filename
        metadata["save_fn"] = geneval_save_fn
        return latent, condition, metadata