(
pipeline,
args,
accelerator,
pipeline_args,
epoch,
torch_dtype,
is_final_validation=False,
)
| 117 | |
| 118 | |
| 119 | def log_validation( |
| 120 | pipeline, |
| 121 | args, |
| 122 | accelerator, |
| 123 | pipeline_args, |
| 124 | epoch, |
| 125 | torch_dtype, |
| 126 | is_final_validation=False, |
| 127 | ): |
| 128 | logger.info( |
| 129 | f"Running validation... \n Generating {args.num_validation_images} images with prompt:" |
| 130 | f" {args.validation_prompt}." |
| 131 | ) |
| 132 | # We train on the simplified learning objective. If we were previously predicting a variance, we need the scheduler to ignore it |
| 133 | scheduler_args = {} |
| 134 | |
| 135 | if "variance_type" in pipeline.scheduler.config: |
| 136 | variance_type = pipeline.scheduler.config.variance_type |
| 137 | |
| 138 | if variance_type in ["learned", "learned_range"]: |
| 139 | variance_type = "fixed_small" |
| 140 | |
| 141 | scheduler_args["variance_type"] = variance_type |
| 142 | |
| 143 | pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config, **scheduler_args) |
| 144 | |
| 145 | pipeline = pipeline.to(accelerator.device, dtype=torch_dtype) |
| 146 | pipeline.set_progress_bar_config(disable=True) |
| 147 | |
| 148 | # run inference |
| 149 | generator = torch.Generator(device=accelerator.device).manual_seed(args.seed) if args.seed else None |
| 150 | |
| 151 | if args.validation_images is None: |
| 152 | images = [] |
| 153 | for _ in range(args.num_validation_images): |
| 154 | with torch.cuda.amp.autocast(): |
| 155 | image = pipeline(**pipeline_args, generator=generator).images[0] |
| 156 | images.append(image) |
| 157 | else: |
| 158 | images = [] |
| 159 | for image in args.validation_images: |
| 160 | image = Image.open(image) |
| 161 | with torch.cuda.amp.autocast(): |
| 162 | image = pipeline(**pipeline_args, image=image, generator=generator).images[0] |
| 163 | images.append(image) |
| 164 | |
| 165 | for tracker in accelerator.trackers: |
| 166 | phase_name = "test" if is_final_validation else "validation" |
| 167 | if tracker.name == "tensorboard": |
| 168 | np_images = np.stack([np.asarray(img) for img in images]) |
| 169 | tracker.writer.add_images(phase_name, np_images, epoch, dataformats="NHWC") |
| 170 | if tracker.name == "wandb": |
| 171 | tracker.log( |
| 172 | { |
| 173 | phase_name: [ |
| 174 | wandb.Image(image, caption=f"{i}: {args.validation_prompt}") for i, image in enumerate(images) |
| 175 | ] |
| 176 | } |
no test coverage detected