Skip to content

class-preservation target loss for LoRA / LyCORIS #1031

@bghira

Description

@bghira

the idea is based on this pastebin entry: https://pastebin.com/3eRwcAJD

snippet:

                    if batch['prompt'][0] == "woman":
                        with torch.no_grad():
                            self.model.transformer_lora.remove_hook_from_module()
                            regmodel_output_data = self.model_setup.predict(self.model, batch, self.config, train_progress)
                            self.model.transformer_lora.hook_to_module()
 
                        model_output_data = self.model_setup.predict(self.model, batch, self.config, train_progress)
                        model_output_data['target']=regmodel_output_data['predicted']
                        loss = self.model_setup.calculate_loss(self.model, batch, model_output_data, self.config)
                        loss *= 1.0
                        print("\nregmodel loss:",loss)
                    else:
 
                        model_output_data = self.model_setup.predict(self.model, batch, self.config, train_progress)
 
                        loss = self.model_setup.calculate_loss(self.model, batch, model_output_data, self.config)

the idea is that we can set a flag inside the multidatabackend.json for a dataset that contains our regularisation data.

instead of training on this data as we currently do, we will instead;

  • temporarily disable the lora/lycoris adapter
  • run a prediction using the regularisation data on the (probably quantised) base model network
  • re-enable the lora/lycoris adapter
  • run the prediction on the adapter
  • update the loss target from the clean latent to the base model prediction

instead of checking for woman in the first element's caption, the batch will come with a flag to enable this behaviour, from multidatabackend.json somehow.

this will indeed run more slowly as it runs two forward passes during training from the regularisation dataset but it has the intended effect of maintaining the original model's outputs for the given inputs, which helps substantially prevent subject bleed.

note: i'm not aware of the author of the code snippet, but i would love to give credit to whoever did create it.

example that came with the snippet:

image

requested by a user on the terminus research discord.

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficult-featuredocumentationImprovements or additions to documentationwork-in-progressThis issue relates to some currently in-progress work.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions