Skip to content

Commit

Permalink
[core] Fix loading of precomputed conditions and latents (#199)
Browse files Browse the repository at this point in the history
* support precomputation.

* fixes
  • Loading branch information
sayakpaul authored Jan 9, 2025
1 parent 6716ccf commit f311f16
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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

0 comments on commit f311f16

Please sign in to comment.