AdversarialTrainer#
- class deepinv.training.AdversarialTrainer(model, physics, optimizer, train_dataloader, losses_d, D, step_ratio_D, ...)[source]#
Bases:
Trainer
Trainer class for training a reconstruction network using adversarial learning.
It overrides the
deepinv.Trainer
class to provide the same functionality, whilst supporting training using adversarial losses. Note that the forward pass remains the same.The usual reconstruction model corresponds to the generator model in an adversarial framework, which is trained using losses specified in the
losses
argument. Additionally, a discriminator modelD
is also jointly trained using the losses provided inlosses_d
. The adversarial losses themselves are defined in the Adversarial Learning module. Examples of discriminators are in Adversarial Networks.See Imaging inverse problems with adversarial networks for usage.
- Examples:
A very basic example:
>>> from deepinv.training import AdversarialTrainer, AdversarialOptimizer >>> from deepinv.loss.adversarial import SupAdversarialGeneratorLoss, SupAdversarialDiscriminatorLoss >>> from deepinv.models import UNet, PatchGANDiscriminator >>> from deepinv.physics import LinearPhysics >>> from deepinv.datasets.utils import PlaceholderDataset >>> >>> generator = UNet(scales=2) >>> discrimin = PatchGANDiscriminator(1, 2, 1) >>> >>> optimizer = AdversarialOptimizer( ... torch.optim.Adam(generator.parameters()), ... torch.optim.Adam(discrimin.parameters()), ... ) >>> >>> trainer = AdversarialTrainer( ... model = generator, ... D = discrimin, ... physics = LinearPhysics(), ... train_dataloader = torch.utils.data.DataLoader(PlaceholderDataset()), ... epochs = 1, ... losses = SupAdversarialGeneratorLoss(), ... losses_d = SupAdversarialDiscriminatorLoss(), ... optimizer = optimizer, ... verbose = False ... ) >>> >>> generator = trainer.train()
Note that this forward pass also computes
y_hat
ahead of time to avoid having to compute it multiple times, but this is completely optional.See
deepinv.Trainer
for additional parameters.- Parameters:
optimizer (AdversarialOptimizer) – optimizer encapsulating both generator and discriminator optimizers
losses_d (Loss, list) – losses to train the discriminator, e.g. adversarial losses
D (Module) – discriminator/critic/classification model, which must take in an image and return a scalar
step_ratio_D (int) – every iteration, train D this many times, allowing for imbalanced generator/discriminator training. Defaults to 1.
- check_clip_grad_D()[source]#
Check the discriminator’s gradient norm and perform gradient clipping if necessary.
Analogous to
check_clip_grad
for generator.
- compute_loss(physics, x, y, train=True, epoch: int | None = None)[source]#
Compute losses and perform backward passes for both generator and discriminator networks.
- Parameters:
physics (deepinv.physics.Physics) – Current physics operator.
x (torch.Tensor) – Ground truth.
y (torch.Tensor) – Measurement.
train (bool) – If
True
, the model is trained, otherwise it is evaluated.epoch (int) – current epoch.
- Returns:
(tuple) The network reconstruction x_net (for plotting and computing metrics) and the logs (for printing the training progress).
Examples using AdversarialTrainer
:#
Imaging inverse problems with adversarial networks