(
self,
step,
text_encoder,
height,
width,
guidance_scale,
eta,
num_inference_steps,
)
| 513 | |
| 514 | @torch.no_grad() |
| 515 | def save_samples( |
| 516 | self, |
| 517 | step, |
| 518 | text_encoder, |
| 519 | height, |
| 520 | width, |
| 521 | guidance_scale, |
| 522 | eta, |
| 523 | num_inference_steps, |
| 524 | ): |
| 525 | samples_path = f"{self.output_dir}/samples" |
| 526 | os.makedirs(samples_path, exist_ok=True) |
| 527 | checker = NoCheck() |
| 528 | |
| 529 | unwrapped = self.accelerator.unwrap_model(text_encoder) |
| 530 | # Save a sample image |
| 531 | pipeline = StableDiffusionPipeline( |
| 532 | text_encoder=unwrapped, |
| 533 | vae=self.vae, |
| 534 | unet=self.unet, |
| 535 | tokenizer=self.tokenizer, |
| 536 | scheduler=LMSDiscreteScheduler( |
| 537 | beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear" |
| 538 | ), |
| 539 | safety_checker=NoCheck(), |
| 540 | feature_extractor=CLIPFeatureExtractor.from_pretrained( |
| 541 | "openai/clip-vit-base-patch32" |
| 542 | ), |
| 543 | ).to("cuda") |
| 544 | pipeline.enable_attention_slicing() |
| 545 | |
| 546 | if self.stable_sample_batches > 0: |
| 547 | stable_latents = torch.randn( |
| 548 | ( |
| 549 | self.sample_batch_size, |
| 550 | pipeline.unet.in_channels, |
| 551 | height // 8, |
| 552 | width // 8, |
| 553 | ), |
| 554 | device=pipeline.device, |
| 555 | generator=torch.Generator(device=pipeline.device).manual_seed( |
| 556 | self.seed |
| 557 | ), |
| 558 | ) |
| 559 | |
| 560 | stable_prompts = [ |
| 561 | choice.format(self.placeholder_token) |
| 562 | for choice in (self.templates * self.sample_batch_size)[ |
| 563 | : self.sample_batch_size |
| 564 | ] |
| 565 | ] |
| 566 | |
| 567 | # Generate and save stable samples |
| 568 | for i in range(0, self.stable_sample_batches): |
| 569 | samples = pipeline( |
| 570 | prompt=stable_prompts, |
| 571 | height=384, |
| 572 | latents=stable_latents, |
no test coverage detected