tgxs002 commited on
Commit
53b8210
1 Parent(s): 250f47a

initialize

Browse files
Files changed (3) hide show
  1. adapted_model.bin +3 -0
  2. app.py +106 -0
  3. normalization_only.bin +3 -0
adapted_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3bebe6307f062671f5a70a36341c27321c350111c3127b500d6ac90b13260628
3
+ size 25604993
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from diffusers.models.cross_attention import LoRACrossAttnProcessor
3
+ from diffusers import StableDiffusionPipeline
4
+ import hashlib
5
+
6
+ import gradio as gr
7
+
8
+ # two pipelines for normalization only and normalization + human preference
9
+
10
+ # load a pretrained sd model, and then load the LoRA weights
11
+ def create_model(weight_path):
12
+ model = StableDiffusionPipeline.from_pretrained(
13
+ "CompVis/stable-diffusion-v1-4",
14
+ torch_dtype=torch.float16,
15
+ ).to("cuda")
16
+ model_weight = torch.load(weight_path, map_location='cpu')
17
+ unet = model.unet
18
+ lora_attn_procs = {}
19
+ lora_rank = list(set([v.size(0) for k, v in model_weight.items() if k.endswith("down.weight")]))
20
+ assert len(lora_rank) == 1
21
+ lora_rank = lora_rank[0]
22
+ for name in unet.attn_processors.keys():
23
+ cross_attention_dim = None if name.endswith("attn1.processor") else unet.config.cross_attention_dim
24
+ if name.startswith("mid_block"):
25
+ hidden_size = unet.config.block_out_channels[-1]
26
+ elif name.startswith("up_blocks"):
27
+ block_id = int(name[len("up_blocks.")])
28
+ hidden_size = list(reversed(unet.config.block_out_channels))[block_id]
29
+ elif name.startswith("down_blocks"):
30
+ block_id = int(name[len("down_blocks.")])
31
+ hidden_size = unet.config.block_out_channels[block_id]
32
+
33
+ lora_attn_procs[name] = LoRACrossAttnProcessor(
34
+ hidden_size=hidden_size, cross_attention_dim=cross_attention_dim, rank=lora_rank
35
+ ).to("cuda")
36
+ unet.set_attn_processor(lora_attn_procs)
37
+ unet.load_state_dict(model_weight, strict=False)
38
+ return model
39
+
40
+ norm_only = create_model("normalization_only.bin")
41
+ adapted = create_model("adapted_model.bin")
42
+
43
+ def inference(prompt):
44
+ # create a hash of the prompt
45
+ hash_object = hashlib.sha256(prompt.encode())
46
+ hex_dig = hash_object.hexdigest()
47
+ integer_value = int(hex_dig, 16)
48
+ # create a pytorch generator based on the prompt
49
+ generator = torch.Generator(device='cuda').manual_seed(integer_value % 2147483647)
50
+ baseline_image = norm_only(prompt, num_inference_steps=50, generator=generator, negative_prompt="Weird image. ").images[0]
51
+ generator = torch.Generator(device='cuda').manual_seed(integer_value % 2147483647)
52
+ adapted_image = adapted(prompt, num_inference_steps=50, generator=generator, negative_prompt="Weird image. ").images[0]
53
+ return baseline_image, adapted_image
54
+
55
+ example_list = [
56
+ "a painting of a waterfall in the middle of a forest, concept art, inspired by andreas rocha, fantasy art, in a beautiful crystal caverine, portal into anotheer dimension, underground lake, sparkling cove, vortex river, plants inside cave, moonlight shafts, intricate sparkling atmosphere, enchanting",
57
+ "photoetch of prize - winning marble albino dog sculpture, intricate details, a turing pattern, aesthetic complexity, midjourney, black background, photorealistic, shpongle, glo - fi, psychedelic, first person view, realistic lighting, intricate, elite, contre - jour lighting",
58
+ "photoetch of marble cacti sculpture with intricate details, black background, midjourney, mathematical formulas, sacred geometry, photorealistic, shpongle, glo - fi, psychedelic, first person view, realistic lighting, retro, intricate, elite, contre - jour lighting",
59
+ "a female angel with blonde pigtails, pale skin and blue eyes, flying, pixar's style, detailed texture, hd, wings, cute, blushed, adorableness, smile, teeth braces",
60
+ "soviet constructivism style, extremly detailed epic painting of a glowing greek sun god apollo zeus in triumphant pose surrounded by thunder, glorious, dark background, masterpiece, trending on artstation, stark red and black and beige and gold, constructivism, by mike mignola and joseph leyendecker and edward hopper",
61
+ "an aquarium with a galaxy and fish inside floating in space with planet earth in the background, concept art, highly detailed photorealistic, dynamic hdr.",
62
+ "a masterpiece landscape, david coulter, mike barr, greg rutkowski, anton fadeev, caspar david friedrich, ferdinand knab, hdr, trending on artstation, cel - shaded, oil painting, professional photography, colorful, complex, epic, realistic colors, hyperdetailed, intricate",
63
+ "girl with fox ears, tired eyes, long wavy orange hair, light brown trenchcoat, forest background, focus on face, pretty, moody lighting, painterly, illustration by shigenori soejima ",
64
+ "a beautiful portrait of anne curtis smith as a shaman, by guweiz and artgerm, ominous, epic fantasy, octopath traveler, afternoon light, rim lighting, unreal engine highly rendered, octane render, volumetric fog, misty, ethereal, 8 k ",
65
+ "masterpiece portrait of an aesthetic mage woman, ice spell, 3 0 years old woman, ( katheryn winnick like ), black dynamic hair, wearing silver diadem with blue gems inlays, silver necklace, painting by joachim bergauer and magali villeneuve, atmospheric effects, chaotic blue sparks dynamics in the background, intricate, artstation, fantasy ",
66
+ "male character study of male tori spelling, clear faces, screenwriter, introvert, outsider, geek, disturbed, emotional, character sheet, fine details, concept design, contrast, kim jung gi, pixar and da vinci, trending on artstation, 8 k, full body and head, turnaround, front view, back view, ultra wide angle ",
67
+ "by yoji shinkawa, concept art of a beautiful woman with purple long hair wearing a large witch hat, ( highly detailed ), flying on broomstick, dynamic lighting, cinematic lighting, neon rim lighting, dreamy night sky background",
68
+ "allen williams, asymmetrical fantantasy cute caracter of mystical ser, cartoon, hight resolution, subrealism, accene, miracle, homiest, rustic estyle, ancestral, 8 k, realism, vértigo estyle pint",
69
+ "emma watson in lara croft costume, war scene, hyper realistic, dramatic cold light, very detailed face, 8 k resolution, photo realistic",
70
+ "a picture of a forest elf clothed in flowers and leaves standing on a stone in an enchanted forest, high fantasy, elegant, epic, detailed, intricate, digital painting, concept art, realistic detailed face, smooth, focus, rim light,",
71
+ "fairy princess, highly detailed, d & d, fantasy, highly detailed, digital painting, trending on artstation, concept art, sharp focus, illustration, art by artgerm and greg rutkowski and magali villeneuve",
72
+ "cardboard knight in game of thrones, artstation trent, portrait, classic paint, heartstone style, wlop style",
73
+ "pixel art digital lion art. wallpaper 3 d pixel art 8 k suoer detailed 3 2 bit. amazing pixel art details. flowers. pixel art. many flowers in the foreground",
74
+ "portrait art of hatsune miku 8 k ultra realistic, lens flare, atmosphere, glow, detailed, intricate, full of colour, cinematic lighting, trending on artstation, 4 k, matte, hyperrealistic, focused, extreme details, unreal engine 5, cinematic, masterpiece",
75
+ "ellyne, beautiful, queen of the unicorns, brown hair, crown, cinematic lighting, 8 k",
76
+ "cute anime supergirl, short blonde hair, concept art, detailed, dark light, digital painting, elegant,",
77
+ "lion king extreme ultra highly detailed full extreme detailed neon tech, hyperdetailed, distopic, by john blanche and greg rutkowski, trending on artstation, depth shading, volumetric light,, digital painting illustration, lighting tean and orange colors, super detailed colors, cinematic lighting colors",
78
+ "a wolf that is merged with ornate silver jewelry and armor, made of celtic knots and other ornatmental patterns, in a snowy forest with a pink orange yellow background, snow flurries, standing on a rock, soft focus, dreamy, realistic 3 d oil painting, unreal engine",
79
+ "fantasy bandit camp, realistic, highly detailed, intricate detailed, trending on artstation by ross tran",
80
+ "(( beautiful girl )), elegant, ultra realistic digital art, pencil drawing, grimdark vintage woodcut sepia, ultra - detailed, hyper detailed, crazy details, intricate details, unreal engine, 8 k, full resolution, super detailed, sharp focus, architectural, volume, by paolo eleuteri serpieri",
81
+ "keanu reeves wearing superman suit flying in the air like a god",
82
+ ]
83
+
84
+ with gr.Blocks() as demo:
85
+ with gr.Row():
86
+ prompt = gr.Textbox(lines=1, label="Prompt")
87
+ button = gr.Button("Generate")
88
+ with gr.Row():
89
+ with gr.Column():
90
+ gr.Markdown('''
91
+ ## Normalization only model
92
+ ''')
93
+ norm_output = gr.Image(label="Normalization only", type="pil")
94
+ with gr.Column():
95
+ gr.Markdown('''
96
+ ## Adaptated model
97
+ ''')
98
+ adapted_output = gr.Image(label="Adapted", type="pil")
99
+ gr.Markdown('''
100
+ ## Example inputs
101
+ ''')
102
+ examples = gr.Examples(examples=example_list, label="Examples", inputs=[prompt])
103
+ button.click(inference, inputs=prompt, outputs=[norm_output, adapted_output])
104
+
105
+ demo.queue(concurrency_count=1)
106
+ demo.launch(share=True)
normalization_only.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b29c79407d391943588a55d5dc7eaf23c4bd58d25b19f6ad2eac4fc340c88d36
3
+ size 25604993