(
pipeline,
args,
accelerator,
pipeline_args,
epoch,
torch_dtype,
is_final_validation=False,
)
| 152 | |
| 153 | |
| 154 | def log_validation( |
| 155 | pipeline, |
| 156 | args, |
| 157 | accelerator, |
| 158 | pipeline_args, |
| 159 | epoch, |
| 160 | torch_dtype, |
| 161 | is_final_validation=False, |
| 162 | ): |
| 163 | logger.info( |
| 164 | f"Running validation... \n Generating {args.num_validation_images} images with prompt:" |
| 165 | f" {args.validation_prompt}." |
| 166 | ) |
| 167 | pipeline = pipeline.to(accelerator.device, dtype=torch_dtype) |
| 168 | pipeline.set_progress_bar_config(disable=True) |
| 169 | |
| 170 | # run inference |
| 171 | generator = torch.Generator(device=accelerator.device).manual_seed(args.seed) if args.seed else None |
| 172 | # autocast_ctx = torch.autocast(accelerator.device.type) if not is_final_validation else nullcontext() |
| 173 | autocast_ctx = nullcontext() |
| 174 | |
| 175 | with autocast_ctx: |
| 176 | images = [pipeline(**pipeline_args, generator=generator).images[0] for _ in range(args.num_validation_images)] |
| 177 | |
| 178 | for tracker in accelerator.trackers: |
| 179 | phase_name = "test" if is_final_validation else "validation" |
| 180 | if tracker.name == "tensorboard": |
| 181 | np_images = np.stack([np.asarray(img) for img in images]) |
| 182 | tracker.writer.add_images(phase_name, np_images, epoch, dataformats="NHWC") |
| 183 | if tracker.name == "wandb": |
| 184 | tracker.log( |
| 185 | { |
| 186 | phase_name: [ |
| 187 | wandb.Image(image, caption=f"{i}: {args.validation_prompt}") for i, image in enumerate(images) |
| 188 | ] |
| 189 | } |
| 190 | ) |
| 191 | |
| 192 | del pipeline |
| 193 | if torch.cuda.is_available(): |
| 194 | torch.cuda.empty_cache() |
| 195 | |
| 196 | return images |
| 197 | |
| 198 | |
| 199 | def import_model_class_from_model_name_or_path( |
no test coverage detected