Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix] DreamBooth Dataset #17

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion diffengine/datasets/hf_dreambooth_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ def __getitem__(self, idx: int) -> dict:
class_image = Image.open(class_image)
result_class_image = dict(img=class_image, text=self.class_prompt)
result_class_image = self.pipeline(result_class_image)
result['result_class_image'] = result_class_image
assert 'inputs' in result
assert 'inputs' in result_class_image
result['inputs']['result_class_image'] = result_class_image[
'inputs']

return result
28 changes: 21 additions & 7 deletions tests/test_datasets/test_hf_dreambooth_datasets.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import shutil

from mmengine.registry import TRANSFORMS
from mmengine.testing import RunnerTestCase
from PIL import Image

from diffengine.datasets import HFDreamBoothDataset
from diffengine.datasets.transforms import PackInputs


class TestHFDreamBoothDataset(RunnerTestCase):

def setUp(self) -> None:
TRANSFORMS.register_module(
name='PackInputs', module=PackInputs, force=True)
return super().setUp()

def tearDown(self):
TRANSFORMS.module_dict.pop('PackInputs')
return super().tearDown()

def test_dataset(self):
dataset = HFDreamBoothDataset(
dataset='diffusers/dog-example',
Expand All @@ -30,18 +41,21 @@ def test_dataset_with_class_image(self):
num_images=1,
device='cpu',
),
)
pipeline=[
dict(type='PackInputs', skip_to_tensor_key=['img', 'text'])
])
assert len(dataset) == 5
assert len(dataset.class_images) == 1

data = dataset[0]
assert data['text'] == 'a photo of sks dog'
self.assertIsInstance(data['img'], Image.Image)
assert data['img'].width == 1815
assert data['inputs']['text'] == 'a photo of sks dog'
self.assertIsInstance(data['inputs']['img'], Image.Image)
assert data['inputs']['img'].width == 1815

assert data['result_class_image']['text'] == 'a photo of dog'
self.assertIsInstance(data['result_class_image']['img'], Image.Image)
assert data['result_class_image']['img'].width == 128
assert data['inputs']['result_class_image']['text'] == 'a photo of dog'
self.assertIsInstance(data['inputs']['result_class_image']['img'],
Image.Image)
assert data['inputs']['result_class_image']['img'].width == 128
shutil.rmtree('temp_dir')

def test_dataset_from_local(self):
Expand Down