Skip to content

[Do not merge] Script to compare safety checkers#219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

patrickvonplaten
Copy link
Contributor

@patrickvonplatenpatrickvonplaten commented Aug 19, 2022

Open clip is taken from here: https://github.com/mlfoundations/open_clip

Script to compare the two:

fromdiffusersimportStableDiffusionPipeline, DDIMSchedulerfromtimeimporttimefromPILimportImagefromeinopsimportrearrangeimportnumpyasnpimporttorchfromtorchimportautocastfromtorchvision.utilsimportmake_gridtorch.manual_seed(42) prompts= ["a photograph of an astronaut riding a horse"] prompts.append("a photograph of the eiffel tower on the moon") prompts.append("an oil painting of a futuristic forest gives") #pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-3-diffusers", use_auth_token=True) # make sure you're logged in with `huggingface-cli login`pipe=StableDiffusionPipeline.from_pretrained("fusing/sd-v1-3", use_auth_token=True) # make sure you're logged in with `huggingface-cli login`all_images= [] num_rows=1num_columns=4forpromptinprompts: withautocast("cuda"): images=pipe(num_columns* [prompt], guidance_scale=7.5, output_type="np")["sample"] # image here is in [PIL format](https://pillow.readthedocs.io/en/stable/)all_images.append(torch.from_numpy(images)) # additionally, save as gridgrid=torch.stack(all_images, 0) grid=rearrange(grid, 'n b h w c -> (n b) h w c') grid=rearrange(grid, 'n h w c -> n c h w') grid=make_grid(grid, nrow=num_rows) # to imagegrid=255.*rearrange(grid, 'c h w -> h w c').cpu().numpy() image=Image.fromarray(grid.astype(np.uint8)) image.save(f"./images/diffusers/final_{'_'.join(prompt.split())}_{round(time())}.png")
concept_name = concept_list[j]
concept_cos = cos_dist[i][j]
concept_threshold = self.concepts_dict[concept_name]
result_img["concept_scores"][concept_name] = round(concept_cos - concept_threshold + adjustment,3)
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that if adjustment > concept_threshold -> the image will always be a bad image

Copy link

@yuimoyuimoOct 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the theory behind this code? why you calculate the similarity between the embedding of the image and a full-ones tensor below:
self.concept_embeds = nn.Parameter(torch.ones(17, config.projection_dim), requires_grad=False)
self.special_care_embeds = nn.Parameter(torch.ones(3, config.projection_dim), requires_grad=False)
thanks very much

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just means that if cosine similarity is above a certain threshold then images will be blocked

@patrickvonplaten
Copy link
ContributorAuthor

Safety Module is checked and works!

PhaneeshB pushed a commit to nod-ai/diffusers that referenced this pull request Mar 1, 2023
@sayakpaulsayakpaul deleted the compare_safety_checkers branch December 3, 2024 10:24
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
@patrickvonplaten@yuimo
close