ysharma HF staff commited on
Commit
61e50f7
·
1 Parent(s): f5594fe

update text encoder codes

Browse files
Files changed (1) hide show
  1. app.py +51 -32
app.py CHANGED
@@ -21,27 +21,38 @@ torch.manual_seed(1)
21
  counter = 0
22
 
23
  #Getting Lora fine-tuned weights
24
- def monkeypatching(alpha, in_prompt, wt): #, prompt, pipe): finetuned_lora_weights
25
  print("****** inside monkeypatching *******")
26
  print(f"in_prompt is - {str(in_prompt)}")
27
  global counter
 
 
 
 
 
28
  if counter == 0 :
29
  #if wt == "./lora_playgroundai_wt.pt" :
30
- monkeypatch_lora(pipe.unet, torch.load(wt)) #finetuned_lora_weights
31
- tune_lora_scale(pipe.unet, alpha) #1.00)
 
 
 
32
  counter +=1
33
  #else:
34
  #monkeypatch_lora(pipe.unet, torch.load("./output_example/lora_weight.pt")) #finetuned_lora_weights
35
  #tune_lora_scale(pipe.unet, alpha) #1.00)
36
  #counter +=1
37
  else :
38
- tune_lora_scale(pipe.unet, alpha) #1.00)
39
- prompt = "style of hclu, " + str(in_prompt) #"baby lion"
 
 
40
  image = pipe(prompt, num_inference_steps=50, guidance_scale=7).images[0]
41
  image.save("./illust_lora.jpg") #"./contents/illust_lora.jpg")
42
  return image
43
 
44
- def accelerate_train_lora(steps, images):
 
45
  print("*********** inside accelerate_train_lora ***********")
46
  print(f"images are -- {images}")
47
  # path can be retrieved by file_obj.name and original filename can be retrieved with file_obj.orig_name
@@ -54,21 +65,27 @@ def accelerate_train_lora(steps, images):
54
  --pretrained_model_name_or_path={MODEL_NAME} \
55
  --instance_data_dir={INSTANCE_DIR} \
56
  --output_dir={OUTPUT_DIR} \
57
- --instance_prompt="style of hclu" \
 
58
  --resolution=512 \
59
  --train_batch_size=1 \
60
  --gradient_accumulation_steps=1 \
61
  --learning_rate=1e-4 \
 
 
62
  --lr_scheduler="constant" \
63
  --lr_warmup_steps=0 \
64
- --max_train_steps={int(steps)}') #,shell=True) #30000
65
  print("*********** completing accelerate_train_lora ***********")
66
- print(f"inside output_dir -- {os.listdir(OUTPUT_DIR)}")
67
  #lora_trained_weights = "./output_example/lora_weight.pt"
68
- for file in os.listdir(f"{OUTPUT_DIR}"):
 
 
69
  if file.endswith(".pt"):
70
- print("weight files is -- ",os.path.join(f"{OUTPUT_DIR}", file))
71
- return file
 
72
  #return f"{OUTPUT_DIR}/*.pt"
73
 
74
  with gr.Blocks() as demo:
@@ -77,7 +94,7 @@ with gr.Blocks() as demo:
77
  gr.HTML("<p>You can skip the queue by duplicating this space and upgrading to gpu in settings: <a style='display:inline-block' href='https://huggingface.co/spaces/ysharma/Low-rank-Adaptation?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a></p>")
78
  gr.Markdown("""<b>NEW!!</b> : I have fine-tuned the SD model for 15,000 steps using 100 PlaygroundAI images and LORA. You can load this trained model using the example component. Load the weight and start using the Space with the Inference button. Feel free to toggle the Alpha value.""")
79
  gr.Markdown(
80
- """**Main Features**<br>- Fine-tune Stable diffusion models twice as faster as dreambooth method by Low-rank Adaptation.<br>- Get insanely small end results, easy to share and download.<br>- Easy to use, compatible with diffusers.<br>- Sometimes even better performance than full fine-tuning<br><br>Please refer to the GitHub repo this Space is based on, here - <a href = "https://github.com/cloneofsimo/lora">LORA</a>. You can also refer to this tweet by AK to quote/retweet/like here on <a href="https://twitter.com/_akhaliq/status/1601120767009513472">Twitter</a>.This Gradio Space is an attempt to explore this novel LORA approach to fine-tune Stable diffusion models, using the power and flexibility of Gradio! The higher number of steps results in longer training time and better fine-tuned SD models.<br><br><b>To use this Space well:</b><br>- First, upload your set of images (4-5), then enter the number of fine-tuning steps, and then press the 'Train LORA model' button. This will produce your fine-tuned model weights.<br>- Enter a prompt, set the alpha value using the Slider (nearer to 1 implies overfitting to the uploaded images), and then press the 'Inference' button. This will produce an image by the newly fine-tuned model.<br><b>Bonus:</b>You can download your fine-tuned model weights from the Gradio file component. The smaller size of LORA models (around 3-4 MB files) is the main highlight of this 'Low-rank Adaptation' approach of fine-tuning.""")
81
 
82
  with gr.Row():
83
  in_images = gr.File(label="Upload images to fine-tune for LORA", file_count="multiple")
@@ -91,26 +108,28 @@ with gr.Blocks() as demo:
91
  with gr.Column():
92
  with gr.Accordion("Advance settings for Training and Inference", open=False):
93
  gr.Markdown("Advance settings for a number of Training Steps and Alpha. Set alpha to 1.0 to fully add LORA. If the LORA seems to have too much effect (i.e., overfitting), set alpha to a lower value. If the LORA seems to have too little effect, set the alpha higher. You can tune these two values to your needs.")
94
- in_steps = gr.Number(label="Enter the number of training steps", value = 4000)
95
- in_alpha = gr.Slider(0.1,1.0, step=0.01, label="Set Alpha level", value=0.5)
96
- out_file = gr.File(label="Lora trained model weights" )
 
 
97
 
98
- gr.Examples(
99
- examples=[[0.65, "lion", "./lora_playgroundai_wt.pt" ]],
100
- inputs=[in_alpha, in_prompt, out_file],
101
- outputs=out_image,
102
- fn=monkeypatching,
103
- cache_examples=True,)
104
- gr.Examples(
105
- examples=[[4000, ['./simba1.jpg', './simba2.jpg', './simba3.jpg', './simba4.jpg']]],
106
- inputs=[in_steps, in_images],
107
- outputs=out_file,
108
- fn=accelerate_train_lora,
109
- cache_examples=False,
110
- run_on_click=False)
111
-
112
- b1.click(fn = accelerate_train_lora, inputs=[in_steps, in_images] , outputs=out_file)
113
- b2.click(fn = monkeypatching, inputs=[in_alpha, in_prompt, out_file], outputs=out_image)
114
 
115
  demo.queue(concurrency_count=3)
116
  demo.launch(debug=True, show_error=True)
 
21
  counter = 0
22
 
23
  #Getting Lora fine-tuned weights
24
+ def monkeypatching(unet_alpha,texten_alpha, in_prompt, wts): #, prompt, pipe): finetuned_lora_weights
25
  print("****** inside monkeypatching *******")
26
  print(f"in_prompt is - {str(in_prompt)}")
27
  global counter
28
+ #if model == 'Text-encoder':
29
+ unet_wt = wts[-2]
30
+ #else:
31
+ texten_wt = wts[-1]
32
+ print(f"UNET weight is = {unet_wt}, Text-encoder weight is = {texten_wt}")
33
  if counter == 0 :
34
  #if wt == "./lora_playgroundai_wt.pt" :
35
+ monkeypatch_lora(pipe.unet, torch.load(unet_wt)) #finetuned_lora_weights
36
+ monkeypatch_lora(pipe.text_encoder, torch.load(texten_wt), target_replace_module=["CLIPAttention"]) #text-encoder #"./lora/lora_kiriko.text_encoder.pt"
37
+ #tune_lora_scale(pipe.unet, alpha) #1.00)
38
+ tune_lora_scale(pipe.unet, unet_alpha)
39
+ tune_lora_scale(pipe.text_encoder, texten_alpha)
40
  counter +=1
41
  #else:
42
  #monkeypatch_lora(pipe.unet, torch.load("./output_example/lora_weight.pt")) #finetuned_lora_weights
43
  #tune_lora_scale(pipe.unet, alpha) #1.00)
44
  #counter +=1
45
  else :
46
+ tune_lora_scale(pipe.unet, unet_alpha)
47
+ tune_lora_scale(pipe.text_encoder, texten_alpha)
48
+ #tune_lora_scale(pipe.unet, alpha) #1.00)
49
+ prompt = str(in_prompt) #"style of hclu, " + str(in_prompt) #"baby lion"
50
  image = pipe(prompt, num_inference_steps=50, guidance_scale=7).images[0]
51
  image.save("./illust_lora.jpg") #"./contents/illust_lora.jpg")
52
  return image
53
 
54
+
55
+ def accelerate_train_lora(steps, images, in_prompt):
56
  print("*********** inside accelerate_train_lora ***********")
57
  print(f"images are -- {images}")
58
  # path can be retrieved by file_obj.name and original filename can be retrieved with file_obj.orig_name
 
65
  --pretrained_model_name_or_path={MODEL_NAME} \
66
  --instance_data_dir={INSTANCE_DIR} \
67
  --output_dir={OUTPUT_DIR} \
68
+ --instance_prompt="{in_prompt}" \
69
+ --train_text_encoder \
70
  --resolution=512 \
71
  --train_batch_size=1 \
72
  --gradient_accumulation_steps=1 \
73
  --learning_rate=1e-4 \
74
+ --learning_rate_text=5e-5 \
75
+ --color_jitter \
76
  --lr_scheduler="constant" \
77
  --lr_warmup_steps=0 \
78
+ --max_train_steps={int(steps)}') #10000
79
  print("*********** completing accelerate_train_lora ***********")
80
+ print(f"files in output_dir -- {os.listdir(OUTPUT_DIR)}")
81
  #lora_trained_weights = "./output_example/lora_weight.pt"
82
+ files = os.listdir(OUTPUT_DIR)
83
+ file_list = []
84
+ for file in files: #os.listdir(OUTPUT_DIR):
85
  if file.endswith(".pt"):
86
+ print("weight files are -- ",os.path.join(f"{OUTPUT_DIR}", file))
87
+ file_list.append(os.path.join(f"{OUTPUT_DIR}", file))
88
+ return file_list #files[1:]
89
  #return f"{OUTPUT_DIR}/*.pt"
90
 
91
  with gr.Blocks() as demo:
 
94
  gr.HTML("<p>You can skip the queue by duplicating this space and upgrading to gpu in settings: <a style='display:inline-block' href='https://huggingface.co/spaces/ysharma/Low-rank-Adaptation?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a></p>")
95
  gr.Markdown("""<b>NEW!!</b> : I have fine-tuned the SD model for 15,000 steps using 100 PlaygroundAI images and LORA. You can load this trained model using the example component. Load the weight and start using the Space with the Inference button. Feel free to toggle the Alpha value.""")
96
  gr.Markdown(
97
+ """**Main Features**<br>- Fine-tune Stable diffusion models twice as faster as dreambooth method by Low-rank Adaptation.<br>- Get insanely small end results, easy to share and download.<br>- Easy to use, compatible with diffusers.<br>- Sometimes even better performance than full fine-tuning<br><br>Please refer to the GitHub repo this Space is based on, here - <a href = "https://github.com/cloneofsimo/lora">LORA</a>. You can also refer to this tweet by AK to quote/retweet/like here on <a href="https://twitter.com/_akhaliq/status/1601120767009513472">Twitter</a>.This Gradio Space is an attempt to explore this novel LORA approach to fine-tune Stable diffusion models, using the power and flexibility of Gradio! The higher number of steps results in longer training time and better fine-tuned SD models.<br><br><b>To use this Space well:</b><br>- First, upload your set of images (suggested number of images is between 4-9), enter the prompt, enter the number of fine-tuning steps (suggested value is between 2000-4000), and then press the 'Train LORA model' button. This will produce your fine-tuned model weights.<br>- Modify the previous prompt by adding to it (suffix), set the alpha value using the Slider (nearer to 1 implies overfitting to the uploaded images), and then press the 'Inference' button. This will produce an image by the newly fine-tuned UNET and Text-Encoder LORA models.<br><b>Bonus:</b>You can download your fine-tuned model weights from the Gradio file component. The smaller size of LORA models (around 3-4 MB files) is the main highlight of this 'Low-rank Adaptation' approach of fine-tuning.""")
98
 
99
  with gr.Row():
100
  in_images = gr.File(label="Upload images to fine-tune for LORA", file_count="multiple")
 
108
  with gr.Column():
109
  with gr.Accordion("Advance settings for Training and Inference", open=False):
110
  gr.Markdown("Advance settings for a number of Training Steps and Alpha. Set alpha to 1.0 to fully add LORA. If the LORA seems to have too much effect (i.e., overfitting), set alpha to a lower value. If the LORA seems to have too little effect, set the alpha higher. You can tune these two values to your needs.")
111
+ in_steps = gr.Number(label="Enter the number of training steps", value = 2000)
112
+ in_alpha_unet = gr.Slider(0.1,1.0, step=0.01, label="Set UNET Alpha level", value=0.5)
113
+ in_alpha_texten = gr.Slider(0.1,1.0, step=0.01, label="Set Text-Encoder Alpha level", value=0.5)
114
+ #in_model = gr.Radio(["Text-encoder", "Unet"], label="Select the fine-tuned model for inference", value="Text-encoder", type="value")
115
+ out_file = gr.File(label="Lora trained model weights", file_count='multiple' )
116
 
117
+ #gr.Examples(
118
+ # examples=[[0.65, 0.6, "lion", ["./lora_playgroundai_wt.pt","./lora_playgroundai_wt.pt"], ],],
119
+ # inputs=[in_alpha_unet, in_alpha_texten, in_prompt, out_file ],
120
+ # outputs=out_image,
121
+ # fn=monkeypatching,
122
+ # cache_examples=True,)
123
+ #gr.Examples(
124
+ # examples=[[2500, ['./simba1.jpg', './simba2.jpg', './simba3.jpg', './simba4.jpg'], "baby lion in disney style"]],
125
+ # inputs=[in_steps, in_images, in_prompt],
126
+ # outputs=out_file,
127
+ # fn=accelerate_train_lora,
128
+ # cache_examples=False,
129
+ # run_on_click=False)
130
+
131
+ b1.click(fn = accelerate_train_lora, inputs=[in_steps, in_images, in_prompt] , outputs=out_file)
132
+ b2.click(fn = monkeypatching, inputs=[in_alpha_unet, in_alpha_texten, in_prompt, out_file,], outputs=out_image)
133
 
134
  demo.queue(concurrency_count=3)
135
  demo.launch(debug=True, show_error=True)