2022-09-06 01:00:21 -06:00
|
|
|
import numpy as np
|
|
|
|
from torch.utils.data import Dataset
|
|
|
|
from torchvision import transforms
|
2022-11-03 17:47:54 -06:00
|
|
|
from ldm.data.data_loader import DataLoaderMultiAspect as dlma
|
2022-11-02 20:23:09 -06:00
|
|
|
import math
|
2022-11-05 23:25:03 -06:00
|
|
|
import ldm.data.dl_singleton as dls
|
2022-11-02 20:23:09 -06:00
|
|
|
class EDValidateBatch(Dataset):
|
2022-09-06 01:00:21 -06:00
|
|
|
def __init__(self,
|
|
|
|
data_root,
|
2022-09-28 15:18:09 -06:00
|
|
|
flip_p=0.0,
|
2022-11-02 20:23:09 -06:00
|
|
|
repeats=1,
|
2022-11-05 09:41:48 -06:00
|
|
|
debug_level=0,
|
2022-11-05 23:25:03 -06:00
|
|
|
batch_size=1,
|
|
|
|
set='val',
|
2022-09-06 01:00:21 -06:00
|
|
|
):
|
2022-11-05 23:25:03 -06:00
|
|
|
|
2022-09-06 01:00:21 -06:00
|
|
|
self.data_root = data_root
|
2022-11-05 09:41:48 -06:00
|
|
|
self.batch_size = batch_size
|
|
|
|
|
2022-11-05 23:25:03 -06:00
|
|
|
if not dls.shared_dataloader:
|
|
|
|
print("Creating new dataloader singleton")
|
|
|
|
dls.shared_dataloader = dlma(data_root=data_root, debug_level=debug_level, batch_size=self.batch_size)
|
|
|
|
|
|
|
|
self.image_caption_pairs = dls.shared_dataloader.get_all_images()
|
2022-11-02 20:23:09 -06:00
|
|
|
|
2022-11-03 17:47:54 -06:00
|
|
|
self.num_images = len(self.image_caption_pairs)
|
2022-09-06 01:00:21 -06:00
|
|
|
|
2022-11-05 23:25:03 -06:00
|
|
|
self._length = max(math.trunc(self.num_images * repeats), batch_size) - self.num_images % self.batch_size
|
|
|
|
|
|
|
|
print()
|
|
|
|
print(f" ** Validation Set: {set}, num_images: {self.num_images}, length: {self._length}, repeats: {repeats}, batch_size: {self.batch_size}, ")
|
|
|
|
print(f" ** Validation steps: {self._length / batch_size:.0f}")
|
|
|
|
print()
|
2022-09-06 01:00:21 -06:00
|
|
|
|
|
|
|
self.flip = transforms.RandomHorizontalFlip(p=flip_p)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self._length
|
|
|
|
|
|
|
|
def __getitem__(self, i):
|
2022-11-03 17:47:54 -06:00
|
|
|
idx = i % len(self.image_caption_pairs)
|
|
|
|
example = self.get_image(self.image_caption_pairs[idx])
|
|
|
|
return example
|
|
|
|
|
|
|
|
def get_image(self, image_caption_pair):
|
2022-09-06 01:00:21 -06:00
|
|
|
example = {}
|
2022-09-30 21:56:27 -06:00
|
|
|
|
2022-11-03 17:47:54 -06:00
|
|
|
image = image_caption_pair[0]
|
2022-09-06 01:00:21 -06:00
|
|
|
|
|
|
|
if not image.mode == "RGB":
|
|
|
|
image = image.convert("RGB")
|
|
|
|
|
2022-11-03 17:47:54 -06:00
|
|
|
identifier = image_caption_pair[1]
|
2022-09-06 01:00:21 -06:00
|
|
|
|
|
|
|
image = self.flip(image)
|
|
|
|
image = np.array(image).astype(np.uint8)
|
|
|
|
example["image"] = (image / 127.5 - 1.0).astype(np.float32)
|
2022-11-02 20:23:09 -06:00
|
|
|
example["caption"] = identifier
|
|
|
|
|
2022-09-26 19:06:04 -06:00
|
|
|
return example
|