EveryDream-trainer/ldm/data/ed_validate.py

54 lines
1.7 KiB
Python
Raw Normal View History

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
import ldm.data.dl_singleton as dls
2022-11-06 17:59:37 -07:00
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,
batch_size=1,
set='val',
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
if not dls.shared_dataloader:
print("Creating new dataloader singleton")
2022-11-06 17:59:37 -07:00
dls.shared_dataloader = dlma(data_root=data_root, debug_level=debug_level, batch_size=self.batch_size, flip_p=flip_p)
2022-11-06 17:59:37 -07:00
self.image_train_items = dls.shared_dataloader.get_all_images()
2022-11-02 20:23:09 -06:00
2022-11-06 17:59:37 -07:00
self.num_images = len(self.image_train_items)
2022-09-06 01:00:21 -06:00
self._length = max(math.trunc(self.num_images * repeats), batch_size) - self.num_images % self.batch_size
print()
2022-11-06 17:59:37 -07:00
print(f" ** Validation Set: {set}, steps: {self._length / batch_size:.0f}, repeats: {repeats} ")
print()
2022-09-06 01:00:21 -06:00
def __len__(self):
return self._length
def __getitem__(self, i):
2022-11-06 17:59:37 -07:00
idx = i % self.num_images
image_train_item = self.image_train_items[idx]
example = self.__get_image_for_trainer(image_train_item)
2022-11-03 17:47:54 -06:00
return example
2022-11-06 17:59:37 -07:00
@staticmethod
def __get_image_for_trainer(image_train_item):
2022-09-06 01:00:21 -06:00
example = {}
2022-11-06 17:59:37 -07:00
image_train_tmp = image_train_item.hydrate()
2022-09-06 01:00:21 -06:00
2022-11-06 17:59:37 -07:00
example["image"] = image_train_tmp.image
example["caption"] = image_train_tmp.caption
2022-11-02 20:23:09 -06:00
return example
2022-11-06 17:59:37 -07:00