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

[core] Fix loading of precomputed conditions and latents #199

Merged
merged 3 commits into from
Jan 9, 2025
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
11 changes: 8 additions & 3 deletions finetrainers/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,18 @@ def _find_nearest_resolution(self, height, width):


class PrecomputedDataset(Dataset):
def __init__(self, data_root: str) -> None:
def __init__(self, data_root: str, model_name: str = None, cleaned_model_id: str = None) -> None:
super().__init__()

self.data_root = Path(data_root)

self.latents_path = self.data_root / PRECOMPUTED_DIR_NAME / PRECOMPUTED_LATENTS_DIR_NAME
self.conditions_path = self.data_root / PRECOMPUTED_DIR_NAME / PRECOMPUTED_CONDITIONS_DIR_NAME
if model_name and cleaned_model_id:
precomputation_dir = self.data_root / f"{model_name}_{cleaned_model_id}_{PRECOMPUTED_DIR_NAME}"
self.latents_path = precomputation_dir / PRECOMPUTED_LATENTS_DIR_NAME
self.conditions_path = precomputation_dir / PRECOMPUTED_CONDITIONS_DIR_NAME
else:
self.latents_path = self.data_root / PRECOMPUTED_DIR_NAME / PRECOMPUTED_LATENTS_DIR_NAME
self.conditions_path = self.data_root / PRECOMPUTED_DIR_NAME / PRECOMPUTED_CONDITIONS_DIR_NAME

self.latent_conditions = sorted(os.listdir(self.latents_path))
self.text_conditions = sorted(os.listdir(self.conditions_path))
Expand Down
8 changes: 6 additions & 2 deletions finetrainers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ def collate_fn(batch):
if not should_precompute:
logger.info("Precomputed conditions and latents found. Loading precomputed data.")
self.dataloader = torch.utils.data.DataLoader(
PrecomputedDataset(self.args.data_root),
PrecomputedDataset(
data_root=self.args.data_root, model_name=self.args.model_name, cleaned_model_id=cleaned_model_id
),
batch_size=self.args.batch_size,
shuffle=True,
collate_fn=collate_fn,
Expand Down Expand Up @@ -353,7 +355,9 @@ def collate_fn(batch):

# Update dataloader to use precomputed conditions and latents
self.dataloader = torch.utils.data.DataLoader(
PrecomputedDataset(self.args.data_root),
PrecomputedDataset(
data_root=self.args.data_root, model_name=self.args.model_name, cleaned_model_id=cleaned_model_id
),
batch_size=self.args.batch_size,
shuffle=True,
collate_fn=collate_fn,
Expand Down