Gemini899 commited on
Commit
7555be9
·
verified ·
1 Parent(s): 46d2fcf

Update flux1_img2img.py

Browse files
Files changed (1) hide show
  1. flux1_img2img.py +12 -49
flux1_img2img.py CHANGED
@@ -1,66 +1,29 @@
1
- import os
2
  import torch
3
  from diffusers import FluxImg2ImgPipeline
 
4
  from PIL import Image
5
  import sys
6
  import spaces
7
 
8
- # Set memory optimization flags
9
- os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
10
-
11
- # Global pipe variable for lazy loading
12
- pipe = None
13
-
14
- def resize_image(image: Image.Image, max_dim: int = 512) -> Image.Image:
15
- """Resizes image to fit within max_dim while preserving aspect ratio"""
16
- w, h = image.size
17
- ratio = min(max_dim / w, max_dim / h)
18
- if ratio < 1.0:
19
- new_w = int(w * ratio)
20
- new_h = int(h * ratio)
21
- image = image.resize((new_w, new_h), Image.LANCZOS)
22
- return image
23
-
24
- def get_pipe(model_id="black-forest-labs/FLUX.1-schnell"):
25
- global pipe
26
- if pipe is None:
27
- pipe = FluxImg2ImgPipeline.from_pretrained(
28
- model_id,
29
- torch_dtype=torch.float16
30
- ).to("cuda")
31
- return pipe
32
 
33
  @spaces.GPU
34
- def process_image(image, mask_image, prompt="a person", model_id="black-forest-labs/FLUX.1-schnell", strength=0.75, seed=0, num_inference_steps=4):
35
  print("start process image process_image")
36
- if image is None:
37
  print("empty input image returned")
38
  return None
39
 
40
- # Resize image to reduce memory usage
41
- image = resize_image(image, max_dim=512)
42
-
43
- # Get model using lazy loading
44
- model = get_pipe(model_id)
45
 
46
  generators = []
47
  generator = torch.Generator("cuda").manual_seed(seed)
48
  generators.append(generator)
49
-
50
- # Use autocast for better memory efficiency
51
- with torch.cuda.amp.autocast(dtype=torch.float16):
52
- with torch.no_grad():
53
- # more parameter see https://huggingface.co/docs/diffusers/api/pipelines/flux#diffusers.FluxInpaintPipeline
54
- print(prompt)
55
- output = model(
56
- prompt=prompt,
57
- image=image,
58
- generator=generator,
59
- strength=strength,
60
- guidance_scale=0,
61
- num_inference_steps=num_inference_steps,
62
- max_sequence_length=256
63
- )
64
 
65
  # TODO support mask
66
  return output.images[0]
@@ -68,6 +31,6 @@ def process_image(image, mask_image, prompt="a person", model_id="black-forest-l
68
  if __name__ == "__main__":
69
  #args input-image input-mask output
70
  image = Image.open(sys.argv[1])
71
- mask = Image.open(sys.argv[2])
72
- output = process_image(image, mask)
73
  output.save(sys.argv[3])
 
 
1
  import torch
2
  from diffusers import FluxImg2ImgPipeline
3
+
4
  from PIL import Image
5
  import sys
6
  import spaces
7
 
8
+ # I only test with FLUX.1-schnell
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  @spaces.GPU
11
+ def process_image(image,mask_image,prompt="a person",model_id="black-forest-labs/FLUX.1-schnell",strength=0.75,seed=0,num_inference_steps=4):
12
  print("start process image process_image")
13
+ if image == None:
14
  print("empty input image returned")
15
  return None
16
 
17
+ pipe = FluxImg2ImgPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
18
+ pipe.to("cuda")
 
 
 
19
 
20
  generators = []
21
  generator = torch.Generator("cuda").manual_seed(seed)
22
  generators.append(generator)
23
+ # more parameter see https://huggingface.co/docs/diffusers/api/pipelines/flux#diffusers.FluxInpaintPipeline
24
+ print(prompt)
25
+ output = pipe(prompt=prompt, image=image,generator=generator,strength=strength
26
+ ,guidance_scale=0,num_inference_steps=num_inference_steps,max_sequence_length=256)
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  # TODO support mask
29
  return output.images[0]
 
31
  if __name__ == "__main__":
32
  #args input-image input-mask output
33
  image = Image.open(sys.argv[1])
34
+ mask = Image.open(sys.argv[2])
35
+ output = process_image(image,mask)
36
  output.save(sys.argv[3])