rkv1990 commited on
Commit
567a310
·
verified ·
1 Parent(s): f6ef36b

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +110 -1
README.md CHANGED
@@ -3,6 +3,115 @@ The idea is to unlock the full outpainting potential of Flux.1.Fill-dev model.
3
  The original model parameters have not been finetuned or modified.
4
  Rather, this simple hack unlocks the full potential of the Flux.1-Fill-dev model.
5
 
 
 
 
 
 
 
 
 
 
 
 
6
  Here is a code snippet to use the code.
7
 
8
- TODO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  The original model parameters have not been finetuned or modified.
4
  Rather, this simple hack unlocks the full potential of the Flux.1-Fill-dev model.
5
 
6
+ `FLUX.1 Fill [dev]` is a 12 billion parameter rectified flow transformer capable of filling areas in existing images based on a text description.
7
+
8
+ ## Diffusers
9
+
10
+ To use `FLUX.1 Fill [dev]` with the 🧨 diffusers python library, first install or upgrade diffusers
11
+
12
+ ```shell
13
+ pip install -U diffusers
14
+ ```
15
+
16
+ Then you can use `FluxFillPipeline` to run the model
17
  Here is a code snippet to use the code.
18
 
19
+
20
+ ```python
21
+ import torch
22
+ from diffusers import FluxFillPipeline
23
+ from diffusers.utils import load_image
24
+
25
+
26
+ def get_mask_and_image(self, original_image, model_w=1024, model_h=1024):
27
+ orig_h, orig_w = original_image.size[0:2]
28
+ pil_image = original_image
29
+ np_image = np.asarray(pil_image)
30
+ np_input_image = np_image[:, :, :3]
31
+ np_input_mask = np_image[:, :, 3]
32
+ pure_fg_image = np.uint8(np_input_image)
33
+ np_input_mask = 255 - np_input_mask
34
+
35
+ alpha = 1 - (np.array(np_input_mask) / 255)
36
+ alpha = np.stack([alpha, alpha, alpha], -1)
37
+ input_image = Image.fromarray(pure_fg_image)
38
+
39
+ kernel = np.ones((3,3))
40
+ np_input_mask = cv2.erode(np_input_mask, kernel, iterations=1)
41
+ input_mask = Image.fromarray(np_input_mask)
42
+ return input_mask, alpha, input_image
43
+
44
+ def prepare_masked_image(self, foreground, mask, alpha=0.001, blur=True):
45
+ # Creating kernel
46
+ kernel = np.ones((3, 3), np.uint8)
47
+ mask_np= np.array(mask)
48
+ h, w, c = np.shape(foreground)
49
+
50
+ #print(h,w,c)
51
+ # Add random Gaussian noise
52
+ noise = np.random.rand(h, w)*255
53
+ noise = np.array(noise, dtype=np.uint8)
54
+ if(blur):
55
+ noise = cv2.GaussianBlur(noise, (5,5), 0)
56
+ noise = np.stack([noise, noise, noise], -1)
57
+
58
+ if(isinstance(foreground,PIL.Image.Image)):
59
+ foreground = np.array(foreground)
60
+
61
+ black_image = Image.fromarray(np.zeros_like(foreground))
62
+ background = np.array(black_image)
63
+
64
+ dilated_mask = np.array(cv2.dilate(np.array(mask), kernel, iterations=10))
65
+ center = (np.shape(foreground)[1]//2,np.shape(foreground)[0]//2)
66
+
67
+ black_image = cv2.seamlessClone(foreground, background, dilated_mask, center, cv2.MIXED_CLONE)
68
+ #black_image = cv2.seamlessClone(foreground, background, dilated_mask, center, cv2.NORMAL_CLONE)
69
+
70
+ noisy_background = np.array(alpha*np.array(black_image) + (1-alpha)*noise, dtype=np.uint8)
71
+ if(np.max(mask_np)>1.0):
72
+ mask_np = mask_np/255.0
73
+ if(mask_np.shape[-1]!=3):
74
+ mask_np = np.stack([mask_np]*3,-1)
75
+
76
+ masked_image = Image.fromarray(np.array((1 - mask_np) * foreground + mask_np * noisy_background, dtype=np.uint8))
77
+
78
+ return masked_image
79
+
80
+
81
+ image = load_image("https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/cup.png")
82
+ mask = load_image("https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/cup_mask.png")
83
+
84
+ fg_mask, alpha, input_img = self.get_mask_and_image()
85
+
86
+ masked_image = self.prepare_masked_image(foreground=input_img_resized, mask=fg_mask_resized)
87
+
88
+ pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16).to("cuda")
89
+ image = pipe(
90
+ prompt="a white paper cup",
91
+ image=masked_image,
92
+ mask_image=mask,
93
+ height=1632,
94
+ width=1232,
95
+ guidance_scale=30,
96
+ num_inference_steps=50,
97
+ max_sequence_length=512,
98
+ generator=torch.Generator("cpu").manual_seed(0)
99
+ ).images[0]
100
+
101
+ image.save(f"flux-fill-dev.png")
102
+ ```
103
+
104
+ To learn more check out the [diffusers](https://huggingface.co/docs/diffusers/main/en/api/pipelines/flux) documentation
105
+
106
+ ---
107
+ language:
108
+ - en
109
+ license: other
110
+ license_name: flux-1-dev-non-commercial-license
111
+ license_link: LICENSE.md
112
+ tags:
113
+ - image-generation
114
+ - flux
115
+ - inpainting
116
+ - diffusion-single-file
117
+ ---