|
16 | 16 | from ldm.models.diffusion.ddim import DDIMSampler |
17 | 17 | from ldm.models.diffusion.plms import PLMSSampler |
18 | 18 |
|
| 19 | +from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker |
| 20 | +from transformers import AutoFeatureExtractor |
| 21 | + |
| 22 | +feature_extractor = AutoFeatureExtractor.from_pretrained("CompVis/stable-diffusion-v-1-3", use_auth_token=True) |
| 23 | +safety_checker = StableDiffusionSafetyChecker.from_pretrained("CompVis/stable-diffusion-v-1-3", use_auth_token=True) |
19 | 24 |
|
20 | 25 | def chunk(it, size): |
21 | 26 | it = iter(it) |
22 | 27 | return iter(lambda: tuple(islice(it, size)), ()) |
23 | 28 |
|
24 | 29 |
|
| 30 | +def numpy_to_pil(images): |
| 31 | + """ |
| 32 | + Convert a numpy image or a batch of images to a PIL image. |
| 33 | + """ |
| 34 | + if images.ndim == 3: |
| 35 | + images = images[None, ...] |
| 36 | + images = (images * 255).round().astype("uint8") |
| 37 | + pil_images = [Image.fromarray(image) for image in images] |
| 38 | + |
| 39 | + return pil_images |
| 40 | + |
| 41 | + |
25 | 42 | def load_model_from_config(config, ckpt, verbose=False): |
26 | 43 | print(f"Loading model from {ckpt}") |
27 | 44 | pl_sd = torch.load(ckpt, map_location="cpu") |
@@ -220,7 +237,9 @@ def main(): |
220 | 237 | if opt.fixed_code: |
221 | 238 | start_code = torch.randn([opt.n_samples, opt.C, opt.H // opt.f, opt.W // opt.f], device=device) |
222 | 239 |
|
| 240 | + print("start code", start_code.abs().sum()) |
223 | 241 | precision_scope = autocast if opt.precision=="autocast" else nullcontext |
| 242 | + precision_scope = nullcontext |
224 | 243 | with torch.no_grad(): |
225 | 244 | with precision_scope("cuda"): |
226 | 245 | with model.ema_scope(): |
@@ -269,7 +288,11 @@ def main(): |
269 | 288 | Image.fromarray(grid.astype(np.uint8)).save(os.path.join(outpath, f'grid-{grid_count:04}.png')) |
270 | 289 | grid_count += 1 |
271 | 290 |
|
272 | | - toc = time.time() |
| 291 | + image = x_samples_ddim.cpu().permute(0, 2, 3, 1).numpy() |
| 292 | + |
| 293 | + # run safety checker |
| 294 | + safety_checker_input = pipe.feature_extractor(numpy_to_pil(image), return_tensors="pt") |
| 295 | + image, has_nsfw_concept = pipe.safety_checker(images=image, clip_input=safety_checker_input.pixel_values) |
273 | 296 |
|
274 | 297 | print(f"Your samples are ready and waiting for you here: \n{outpath} \n" |
275 | 298 | f" \nEnjoy.") |
|
0 commit comments