VikramSingh178 commited on
Commit
a8d1f41
β€’
1 Parent(s): 4efd868

Former-commit-id: dfb71f8ff7b4354652740e6a622c2631ec6d5e41

logs/app_debug.log CHANGED
@@ -1326,3 +1326,48 @@ speed: {'preprocess': 1.9655227661132812, 'inference': 86.20810508728027, 'postp
1326
  2024-03-20 16:39:19,333 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
1327
  2024-03-20 16:39:50,404 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
1328
  2024-03-20 16:45:37,037 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1326
  2024-03-20 16:39:19,333 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
1327
  2024-03-20 16:39:50,404 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
1328
  2024-03-20 16:45:37,037 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
1329
+ 2024-03-22 03:55:40,678 [INFO] models - Inpainting Inference
1330
+ 2024-03-22 03:55:41,174 [INFO] clear_memory - Memory Cleared
1331
+ 2024-03-22 03:58:06,886 [INFO] models - Inpainting Inference
1332
+ 2024-03-22 03:58:07,066 [INFO] clear_memory - Memory Cleared
1333
+ 2024-03-22 04:01:27,172 [INFO] models - Inpainting Inference
1334
+ 2024-03-22 04:01:27,418 [INFO] clear_memory - Memory Cleared
1335
+ 2024-03-22 04:06:38,680 [INFO] models - Inpainting Inference
1336
+ 2024-03-22 04:06:38,889 [INFO] clear_memory - Memory Cleared
1337
+ 2024-03-22 04:11:41,673 [INFO] models - Inpainting Inference
1338
+ 2024-03-22 04:11:41,874 [INFO] clear_memory - Memory Cleared
1339
+ 2024-03-22 04:20:36,838 [INFO] models - Inpainting Inference Completed
1340
+ 2024-03-22 04:30:50,234 [INFO] models - Inpainting Inference
1341
+ 2024-03-22 04:30:50,522 [INFO] clear_memory - Memory Cleared
1342
+ 2024-03-22 04:34:30,860 [INFO] models - Inpainting Inference Completed
1343
+ 2024-03-22 04:37:41,028 [INFO] models - Inpainting Inference
1344
+ 2024-03-22 04:38:16,111 [INFO] models - Inpainting Inference
1345
+ 2024-03-22 04:38:16,367 [INFO] clear_memory - Memory Cleared
1346
+ 2024-03-22 04:40:29,119 [INFO] models - Inpainting Inference Completed
1347
+ 2024-03-22 05:00:56,937 [INFO] models - Kandinsky Inpainting Inference
1348
+ 2024-03-22 05:02:34,916 [INFO] models - Kandinsky Inpainting Inference
1349
+ 2024-03-22 05:13:44,547 [INFO] models - Kandinsky Inpainting Inference
1350
+ 2024-03-22 05:18:53,623 [INFO] models - Inpainting Inference Completed
1351
+ 2024-03-22 05:20:06,892 [INFO] models - Kandinsky Inpainting Inference
1352
+ 2024-03-22 05:20:26,852 [INFO] models - Inpainting Inference Completed
1353
+ 2024-03-22 05:25:49,956 [INFO] models - Kandinsky Inpainting Inference
1354
+ 2024-03-22 05:26:09,324 [INFO] models - Inpainting Inference Completed
1355
+ 2024-03-22 05:26:58,013 [INFO] models - Kandinsky Inpainting Inference
1356
+ 2024-03-22 05:27:17,153 [INFO] models - Inpainting Inference Completed
1357
+ 2024-03-22 05:32:55,633 [INFO] models - Kandinsky Inpainting Inference
1358
+ 2024-03-22 05:33:14,930 [INFO] models - Inpainting Inference Completed
1359
+ 2024-03-22 05:33:47,613 [INFO] models - Kandinsky Inpainting Inference
1360
+ 2024-03-22 05:34:06,803 [INFO] models - Inpainting Inference Completed
1361
+ 2024-03-22 05:34:56,622 [INFO] models - Kandinsky Inpainting Inference
1362
+ 2024-03-22 05:35:16,304 [INFO] models - Inpainting Inference Completed
1363
+ 2024-03-22 05:37:23,678 [INFO] models - Kandinsky Inpainting Inference
1364
+ 2024-03-22 05:37:46,172 [INFO] models - Inpainting Inference Completed
1365
+ 2024-03-22 06:04:47,683 [INFO] models - Kandinsky Inpainting Inference
1366
+ 2024-03-22 06:09:12,886 [INFO] models - Kandinsky Inpainting Inference
1367
+ 2024-03-22 06:12:29,146 [INFO] models - Kandinsky Inpainting Inference
1368
+ 2024-03-22 06:24:32,044 [INFO] models - Kandinsky Inpainting Inference
1369
+ 2024-03-22 06:27:52,332 [INFO] models - Kandinsky Inpainting Inference
1370
+ 2024-03-22 06:35:16,527 [INFO] models - Kandinsky Inpainting Inference
1371
+ 2024-03-22 06:39:26,709 [INFO] models - Kandinsky Inpainting Inference
1372
+ 2024-03-22 06:43:26,086 [INFO] models - Kandinsky Inpainting Inference
1373
+ 2024-03-22 06:53:54,562 [INFO] models - Kandinsky Inpainting Inference
logs/app_info.log CHANGED
@@ -1326,3 +1326,48 @@ speed: {'preprocess': 1.9655227661132812, 'inference': 86.20810508728027, 'postp
1326
  2024-03-20 16:39:19,333 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
1327
  2024-03-20 16:39:50,404 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
1328
  2024-03-20 16:45:37,037 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1326
  2024-03-20 16:39:19,333 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
1327
  2024-03-20 16:39:50,404 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
1328
  2024-03-20 16:45:37,037 [INFO] pipelineutils - Controlnet Pipeline initialized successfully
1329
+ 2024-03-22 03:55:40,678 [INFO] models - Inpainting Inference
1330
+ 2024-03-22 03:55:41,174 [INFO] clear_memory - Memory Cleared
1331
+ 2024-03-22 03:58:06,886 [INFO] models - Inpainting Inference
1332
+ 2024-03-22 03:58:07,066 [INFO] clear_memory - Memory Cleared
1333
+ 2024-03-22 04:01:27,172 [INFO] models - Inpainting Inference
1334
+ 2024-03-22 04:01:27,418 [INFO] clear_memory - Memory Cleared
1335
+ 2024-03-22 04:06:38,680 [INFO] models - Inpainting Inference
1336
+ 2024-03-22 04:06:38,889 [INFO] clear_memory - Memory Cleared
1337
+ 2024-03-22 04:11:41,673 [INFO] models - Inpainting Inference
1338
+ 2024-03-22 04:11:41,874 [INFO] clear_memory - Memory Cleared
1339
+ 2024-03-22 04:20:36,838 [INFO] models - Inpainting Inference Completed
1340
+ 2024-03-22 04:30:50,234 [INFO] models - Inpainting Inference
1341
+ 2024-03-22 04:30:50,522 [INFO] clear_memory - Memory Cleared
1342
+ 2024-03-22 04:34:30,860 [INFO] models - Inpainting Inference Completed
1343
+ 2024-03-22 04:37:41,028 [INFO] models - Inpainting Inference
1344
+ 2024-03-22 04:38:16,111 [INFO] models - Inpainting Inference
1345
+ 2024-03-22 04:38:16,367 [INFO] clear_memory - Memory Cleared
1346
+ 2024-03-22 04:40:29,119 [INFO] models - Inpainting Inference Completed
1347
+ 2024-03-22 05:00:56,937 [INFO] models - Kandinsky Inpainting Inference
1348
+ 2024-03-22 05:02:34,916 [INFO] models - Kandinsky Inpainting Inference
1349
+ 2024-03-22 05:13:44,547 [INFO] models - Kandinsky Inpainting Inference
1350
+ 2024-03-22 05:18:53,623 [INFO] models - Inpainting Inference Completed
1351
+ 2024-03-22 05:20:06,892 [INFO] models - Kandinsky Inpainting Inference
1352
+ 2024-03-22 05:20:26,852 [INFO] models - Inpainting Inference Completed
1353
+ 2024-03-22 05:25:49,956 [INFO] models - Kandinsky Inpainting Inference
1354
+ 2024-03-22 05:26:09,324 [INFO] models - Inpainting Inference Completed
1355
+ 2024-03-22 05:26:58,013 [INFO] models - Kandinsky Inpainting Inference
1356
+ 2024-03-22 05:27:17,153 [INFO] models - Inpainting Inference Completed
1357
+ 2024-03-22 05:32:55,633 [INFO] models - Kandinsky Inpainting Inference
1358
+ 2024-03-22 05:33:14,930 [INFO] models - Inpainting Inference Completed
1359
+ 2024-03-22 05:33:47,613 [INFO] models - Kandinsky Inpainting Inference
1360
+ 2024-03-22 05:34:06,803 [INFO] models - Inpainting Inference Completed
1361
+ 2024-03-22 05:34:56,622 [INFO] models - Kandinsky Inpainting Inference
1362
+ 2024-03-22 05:35:16,304 [INFO] models - Inpainting Inference Completed
1363
+ 2024-03-22 05:37:23,678 [INFO] models - Kandinsky Inpainting Inference
1364
+ 2024-03-22 05:37:46,172 [INFO] models - Inpainting Inference Completed
1365
+ 2024-03-22 06:04:47,683 [INFO] models - Kandinsky Inpainting Inference
1366
+ 2024-03-22 06:09:12,886 [INFO] models - Kandinsky Inpainting Inference
1367
+ 2024-03-22 06:12:29,146 [INFO] models - Kandinsky Inpainting Inference
1368
+ 2024-03-22 06:24:32,044 [INFO] models - Kandinsky Inpainting Inference
1369
+ 2024-03-22 06:27:52,332 [INFO] models - Kandinsky Inpainting Inference
1370
+ 2024-03-22 06:35:16,527 [INFO] models - Kandinsky Inpainting Inference
1371
+ 2024-03-22 06:39:26,709 [INFO] models - Kandinsky Inpainting Inference
1372
+ 2024-03-22 06:43:26,086 [INFO] models - Kandinsky Inpainting Inference
1373
+ 2024-03-22 06:53:54,562 [INFO] models - Kandinsky Inpainting Inference
scripts/__pycache__/config.cpython-310.pyc CHANGED
Binary files a/scripts/__pycache__/config.cpython-310.pyc and b/scripts/__pycache__/config.cpython-310.pyc differ
 
scripts/__pycache__/mask_generator.cpython-310.pyc CHANGED
Binary files a/scripts/__pycache__/mask_generator.cpython-310.pyc and b/scripts/__pycache__/mask_generator.cpython-310.pyc differ
 
scripts/__pycache__/pipeline.cpython-310.pyc ADDED
Binary file (2.6 kB). View file
 
scripts/config.py CHANGED
@@ -1,12 +1,13 @@
1
  LOGS_DIR = '../logs'
2
  Dataset_Name = "AlekseyKorshuk/product-photography-all"
3
  DATA_DIR = '../data'
4
- Project_Name = 'product_placement_diffusers'
5
  entity = 'vikramxd'
6
  image_dir = '../sample_data'
7
  mask_dir = '../masks'
8
  controlnet_adapter_model_name= 'lllyasviel/control_v11p_sd15_inpaint'
9
  controlnet_base_model_name = "runwayml/stable-diffusion-inpainting"
10
- stable_diffusion_inpainting_model_name = "stabilityai/stable-diffusion-2-inpainting"
11
  width = 512
12
  height = 512
 
 
1
  LOGS_DIR = '../logs'
2
  Dataset_Name = "AlekseyKorshuk/product-photography-all"
3
  DATA_DIR = '../data'
4
+ Project_Name = 'product_placement_api'
5
  entity = 'vikramxd'
6
  image_dir = '../sample_data'
7
  mask_dir = '../masks'
8
  controlnet_adapter_model_name= 'lllyasviel/control_v11p_sd15_inpaint'
9
  controlnet_base_model_name = "runwayml/stable-diffusion-inpainting"
10
+ kandinsky_model_name = 'kandinsky-community/kandinsky-2-2-decoder-inpaint'
11
  width = 512
12
  height = 512
13
+ yolo_model = 'yolov8s-seg.pt'
scripts/invert_mask.jpg ADDED
scripts/mask_generator.py CHANGED
@@ -1,24 +1,13 @@
1
-
2
- from typing import List, Tuple, Dict
3
- import torch
4
  from PIL import Image
5
  import numpy as np
6
  from logger import rich_logger as l
7
  from ultralytics import YOLO
8
  import cv2
 
9
 
10
 
11
 
12
 
13
- def convert_to_numpy_array(image: Image) -> np.ndarray:
14
- """Method to convert PIL image to numpy array
15
- Args:
16
- image (Image): input image
17
- Returns:
18
- np.ndarray: numpy array
19
- """
20
- return np.array(image)
21
-
22
 
23
 
24
 
@@ -30,7 +19,7 @@ def generate_mask(image_path: str) -> np.ndarray:
30
  Returns:
31
  Image: segmented image
32
  """
33
- model = YOLO(model='yolov8s-seg.pt',)
34
  results = model(image_path)
35
  for result in results:
36
  orig_img = result.orig_img
@@ -38,7 +27,6 @@ def generate_mask(image_path: str) -> np.ndarray:
38
  height, width = result.orig_img.shape[:2]
39
  background = np.ones((height, width, 3), dtype=np.uint8) * 255
40
 
41
-
42
  for mask in masks:
43
  mask = mask.astype(int)
44
  mask_img = np.zeros_like(orig_img)
@@ -48,12 +36,22 @@ def generate_mask(image_path: str) -> np.ndarray:
48
 
49
  return mask_img, orig_img
50
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  if __name__ == "__main__":
53
  image = Image.open("../sample_data/example1.jpg")
54
- image = image.resize((512, 512))
55
- image = convert_to_numpy_array(image)
56
- mask_image,orig_image = generate_mask(image_path='../sample_data/example1.jpg')
57
 
58
 
59
 
 
 
 
 
1
  from PIL import Image
2
  import numpy as np
3
  from logger import rich_logger as l
4
  from ultralytics import YOLO
5
  import cv2
6
+ from config import yolo_model
7
 
8
 
9
 
10
 
 
 
 
 
 
 
 
 
 
11
 
12
 
13
 
 
19
  Returns:
20
  Image: segmented image
21
  """
22
+ model = YOLO(model=yolo_model)
23
  results = model(image_path)
24
  for result in results:
25
  orig_img = result.orig_img
 
27
  height, width = result.orig_img.shape[:2]
28
  background = np.ones((height, width, 3), dtype=np.uint8) * 255
29
 
 
30
  for mask in masks:
31
  mask = mask.astype(int)
32
  mask_img = np.zeros_like(orig_img)
 
36
 
37
  return mask_img, orig_img
38
 
39
+ def invert_mask(mask_image: np.ndarray) -> np.ndarray:
40
+ """Method to invert mask
41
+ Args:
42
+ mask_image (np.ndarray): input mask image
43
+ Returns:
44
+ np.ndarray: inverted mask image
45
+ """
46
+ inverted_mask_image = cv2.bitwise_not(mask_image)
47
+ cv2.imwrite('invert_mask.jpg', inverted_mask_image)
48
+ return inverted_mask_image
49
+
50
 
51
  if __name__ == "__main__":
52
  image = Image.open("../sample_data/example1.jpg")
53
+ mask_img,orig_image = generate_mask(image_path='../sample_data/example1.jpg')
54
+ invert_mask(mask_image=mask_img)
 
55
 
56
 
57
 
scripts/models.py CHANGED
@@ -6,72 +6,178 @@ from typing import List
6
  import numpy as np
7
  import torch
8
  from PIL import Image
9
- from mask_generator import convert_to_numpy_array, generate_mask
10
  from diffusers.utils import load_image
11
- import cv2
12
- from config import controlnet_adapter_model_name,controlnet_base_model_name
13
- from diffusers import ControlNetModel,StableDiffusionControlNetInpaintPipeline
14
- autolog(init=dict(project=Project_Name))
 
15
 
16
 
17
 
 
18
 
19
 
20
 
21
-
22
 
23
 
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  def make_inpaint_condition(init_image, mask_image):
27
- # Prepare control image
28
- init_image = np.array(init_image.convert("RGB")).astype(np.float32) / 255.0
29
- mask_image = np.array(mask_image.convert("L")).astype(np.float32) / 255.0
30
-
31
- assert init_image.shape[0:1] == mask_image.shape[0:1], "image and image_mask must have the same image size"
32
- init_image[mask_image > 0.5] = -1.0 # set as masked pixel
33
- init_image = np.expand_dims(init_image, 0).transpose(0, 3, 1, 2)
34
- init_image = torch.from_numpy(init_image)
35
- return init_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
 
 
39
 
 
 
 
 
 
 
 
 
 
40
 
41
- def make_image_controlnet(image,
42
- mask_image,
43
- controlnet_conditioning_image,
44
- positive_prompt: str, negative_prompt: str,
45
- seed: int = 2356132) -> List[Image.Image]:
46
- """Method to make image using controlnet
47
  Args:
48
- image (np.ndarray): input image
49
- mask_image (np.ndarray): mask image
50
- controlnet_conditioning_image (np.ndarray): conditioning image
51
- positive_prompt (str): positive prompt string
52
- negative_prompt (str): negative prompt string
53
- seed (int, optional): seed. Defaults to 2356132.
54
  Returns:
55
- List[Image.Image]: list of generated images
56
  """
57
- controlnet = ControlNetModel.from_pretrained(controlnet_adapter_model_name, torch_dtype=torch.float32)
58
- pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
59
- controlnet_base_model_name, controlnet=controlnet, torch_dtype=torch.float32
60
- )
61
-
 
 
 
 
 
 
62
 
63
-
64
-
65
-
66
- image = pipe(prompt=positive_prompt,negative_prompt=negative_prompt, image=init_image, mask_image=mask_image, control_image=controlnet_conditioning_image).images[0]
 
 
67
 
 
 
68
 
 
 
 
 
 
 
69
  return image
70
 
71
- if __name__ == "__main__":
72
- init_image = load_image('/home/product_diffusion_api/sample_data/example1.jpg')
73
- mask_image = load_image('/home/product_diffusion_api/scripts/mask.jpg')
74
- controlnet_conditioning_image = make_inpaint_condition(init_image=init_image,mask_image=mask_image)
75
- result = make_image_controlnet(positive_prompt="Product used in kitchen 4k natural photography",negative_prompt="No artifcats",image=init_image,mask_image=mask_image,controlnet_conditioning_image=controlnet_conditioning_image)
76
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
 
6
  import numpy as np
7
  import torch
8
  from PIL import Image
9
+ from mask_generator import invert_mask
10
  from diffusers.utils import load_image
11
+ from pipeline import fetch_control_pipeline,fetch_kandinsky_pipeline,fetch_kandinsky_prior_pipeline,fetch_kandinsky_img2img_pipeline
12
+ from config import controlnet_adapter_model_name,controlnet_base_model_name,kandinsky_model_name
13
+ import cv2
14
+ import PIL.ImageOps
15
+ from transformers import pipeline
16
 
17
 
18
 
19
+ autolog(init=dict(project=Project_Name))
20
 
21
 
22
 
 
23
 
24
 
25
 
26
+ def make_controlnet_condition(image: Image.Image) -> Image.Image:
27
+ """
28
+ Applies image processing operations to create a controlnet condition image.
29
+
30
+ Args:
31
+ image (PIL.Image.Image): The input image.
32
+
33
+ Returns:
34
+ PIL.Image.Image: The controlnet condition image.
35
+ """
36
+ image = np.array(image)
37
+ image = cv2.Canny(image, 100, 200)
38
+ image = image[:, :, None]
39
+ image = np.concatenate([image, image, image], axis=2)
40
+ image = Image.fromarray(image)
41
+ return image
42
 
43
  def make_inpaint_condition(init_image, mask_image):
44
+ """
45
+ Prepare the initial image for inpainting by applying a mask.
46
+
47
+ Args:
48
+ init_image (PIL.Image.Image): The initial image.
49
+ mask_image (PIL.Image.Image): The mask image.
50
+
51
+ Returns:
52
+ torch.Tensor: The prepared initial image for inpainting.
53
+
54
+ Raises:
55
+ AssertionError: If the image and mask have different sizes.
56
+
57
+ """
58
+ # Prepare control image
59
+ init_image = np.array(init_image.convert("RGB")).astype(np.float32) / 255.0
60
+ mask_image = np.array(mask_image.convert("L")).astype(np.float32) / 255.0
61
+
62
+ assert init_image.shape[0:1] == mask_image.shape[0:1], "image and image_mask must have the same image size"
63
+ init_image[mask_image > 0.5] = -1.0 # set as masked pixel
64
+ init_image = np.expand_dims(init_image, 0).transpose(0, 3, 1, 2)
65
+ init_image = torch.from_numpy(init_image)
66
+ return init_image
67
 
68
 
69
+ def make_hint(image, depth_estimator):
70
+ image = depth_estimator(image)["depth"]
71
+ image = np.array(image)
72
+ image = image[:, :, None]
73
+ image = np.concatenate([image, image, image], axis=2)
74
+ detected_map = torch.from_numpy(image).float() / 255.0
75
+ hint = detected_map.permute(2, 0, 1)
76
+ return hint
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+ def controlnet_inpainting_inference(prompt,
86
+ image,
87
+ mask_image,
88
+ control_image,
89
+ num_inference_steps=200,
90
+ guidance_scale=1.2,
91
+ strength=5.0,
92
+ generator=torch.Generator(device="cpu").manual_seed(1)
93
+ ) -> List[Image.Image]:
94
+ """
95
+ Perform inpainting inference on an image using the given parameters.
96
+
97
+ Args:
98
+ prompt: The prompt for the inpainting inference.
99
+ image: The input image to be inpainted.
100
+ mask_image: The mask image indicating the regions to be inpainted.
101
+ controlnet_conditioning_image: The conditioning image for the controlnet.
102
+ num_inference_steps: The number of inference steps to perform (default: 200).
103
+ guidance_scale: The scale factor for the guidance loss (default: 1.2).
104
+ strength: The strength of the inpainting (default: 5.0).
105
+ generator: The random number generator for reproducibility (default: torch.Generator(device="cpu").manual_seed(1)).
106
 
107
+ Returns:
108
+ A list of inpainted images.
109
 
110
+ """
111
+ clear_memory()
112
+ pipe = fetch_control_pipeline(controlnet_adapter_model_name, controlnet_base_model_name,kandinsky_model_name, control_image)
113
+ image = pipe(prompt = prompt,num_inference_steps=num_inference_steps, generator=generator, eta=1.0, image=image, mask_image=mask_image,guidance_scale=guidance_scale,strenght=strength, control_image=control_image).images[0]
114
+ return image
115
+
116
+ def kandinsky_inpainting_inference(prompt, negative_prompt, image, mask_image):
117
+ """
118
+ Perform Kandinsky inpainting inference on the given image.
119
 
 
 
 
 
 
 
120
  Args:
121
+ prompt (str): The prompt for the inpainting process.
122
+ negative_prompt (str): The negative prompt for the inpainting process.
123
+ image (PIL.Image.Image): The input image to be inpainted.
124
+ mask_image (PIL.Image.Image): The mask image indicating the areas to be inpainted.
125
+
 
126
  Returns:
127
+ PIL.Image.Image: The output inpainted image.
128
  """
129
+ pipe = fetch_kandinsky_pipeline(controlnet_adapter_model_name, controlnet_base_model_name, kandinsky_model_name, image)
130
+ output_image = pipe(prompt=prompt, negative_prompt=negative_prompt, image=image, mask_image=mask_image).images[0]
131
+ return output_image
132
+ def kandinsky_inpainting_inference(prompt,negative_prompt,image,mask_image):
133
+ pipe = fetch_kandinsky_pipeline(controlnet_adapter_model_name, controlnet_base_model_name,kandinsky_model_name, image)
134
+ output_image = pipe(prompt=prompt,negative_prompt=negative_prompt,image=image,mask_image=mask_image).images[0]
135
+ return output_image
136
+
137
+ def kandinsky_controlnet_inpainting_inference(prompt, negative_prompt, image, hint, generator=torch.Generator(device="cuda").manual_seed(43)):
138
+ """
139
+ Perform inpainting inference using the Kandinsky ControlNet model.
140
 
141
+ Args:
142
+ prompt (str): The prompt for the inpainting process.
143
+ negative_prompt (str): The negative prompt for the inpainting process.
144
+ image (torch.Tensor): The input image for inpainting.
145
+ hint (torch.Tensor): The hint for guiding the inpainting process.
146
+ generator (torch.Generator, optional): The random number generator. Defaults to CUDA generator with seed 43.
147
 
148
+ Returns:
149
+ torch.Tensor: The inpainted image.
150
 
151
+ """
152
+ prior_pipe = fetch_kandinsky_prior_pipeline(controlnet_adapter_model_name, controlnet_base_model_name, kandinsky_model_name, image)
153
+ img_embed = prior_pipe(prompt=prompt, image=image, strength=0.85, generator=generator)
154
+ negative_embed = prior_pipe(prompt=negative_prompt, image=image, strength=1, generator=generator)
155
+ controlnet_pipe = fetch_kandinsky_img2img_pipeline(controlnet_adapter_model_name, controlnet_base_model_name, kandinsky_model_name, image)
156
+ image = controlnet_pipe(image=image, strength=0.5, image_embeds=img_embed.image_embeds, negative_image_embeds=negative_embed.image_embeds, hint=hint, num_inference_steps=50, generator=generator, height=768, width=768).images[0]
157
  return image
158
 
159
+
160
+
161
+ if __name__ == '__main__':
162
+ l.info("Kandinsky Inpainting Inference")
163
+ image = load_image('/home/product_diffusion_api/sample_data/example2.jpg')
164
+ image = image.resize((768, 768))
165
+ mask_image = load_image('/home/product_diffusion_api/scripts/invert_mask.jpg')
166
+ mask_image = mask_image.resize((768,768))
167
+ prompt = "Product in a GYM 8k ultrarealistic "
168
+ negative_prompt="lowres, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature"
169
+ output_image = kandinsky_inpainting_inference(prompt,negative_prompt,image,mask_image)
170
+ output_image=output_image.resize((768,768))
171
+ depth_estimator = pipeline("depth-estimation")
172
+ hint = make_hint(output_image, depth_estimator).unsqueeze(0).half().to("cuda")
173
+ final_output_image = kandinsky_controlnet_inpainting_inference(prompt,negative_prompt,image, hint)
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
 
scripts/output.jpg ADDED
scripts/pipeline.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import ControlNetModel,StableDiffusionControlNetInpaintPipeline,AutoPipelineForInpainting,KandinskyV22ControlnetImg2ImgPipeline,KandinskyV22PriorEmb2EmbPipeline
2
+ from diffusers.utils import load_image
3
+ import torch
4
+ from PIL import Image
5
+ import numpy as np
6
+ import cv2
7
+ import torch
8
+
9
+
10
+
11
+
12
+
13
+ class PipelineFetcher:
14
+ """
15
+ A class that fetches different pipelines for image processing.
16
+
17
+ Args:
18
+ controlnet_adapter_model_name (str): The name of the controlnet adapter model.
19
+ controlnet_base_model_name (str): The name of the controlnet base model.
20
+ kandinsky_model_name (str): The name of the Kandinsky model.
21
+ image (str): The image to be processed.
22
+
23
+ """
24
+
25
+ def __init__(self, controlnet_adapter_model_name, controlnet_base_model_name, kandinsky_model_name, image: str):
26
+ self.controlnet_adapter_model_name = controlnet_adapter_model_name
27
+ self.controlnet_base_model_name = controlnet_base_model_name
28
+ self.kandinsky_model_name = kandinsky_model_name
29
+ self.image = image
30
+
31
+ def ControlNetInpaintPipeline(self):
32
+ """
33
+ Fetches the ControlNet inpainting pipeline.
34
+
35
+ Returns:
36
+ pipe (StableDiffusionControlNetInpaintPipeline): The ControlNet inpainting pipeline.
37
+
38
+ """
39
+ controlnet = ControlNetModel.from_pretrained(self.controlnet_adapter_model_name, torch_dtype=torch.float16)
40
+ pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
41
+ self.controlnet_base_model_name, controlnet=controlnet, torch_dtype=torch.float16
42
+ )
43
+ pipe.to('cuda')
44
+
45
+ return pipe
46
+
47
+ def KandinskyPipeline(self):
48
+ """
49
+ Fetches the Kandinsky pipeline.
50
+
51
+ Returns:
52
+ pipe (AutoPipelineForInpainting): The Kandinsky pipeline.
53
+
54
+ """
55
+ pipe = AutoPipelineForInpainting.from_pretrained(self.kandinsky_model_name, torch_dtype=torch.float16)
56
+ pipe.to('cuda')
57
+ return pipe
58
+
59
+ def KandinskyPriorPipeline(self):
60
+ """
61
+ Fetches the Kandinsky prior pipeline.
62
+
63
+ Returns:
64
+ prior_pipeline (KandinskyV22PriorEmb2EmbPipeline): The Kandinsky prior pipeline.
65
+
66
+ """
67
+ prior_pipeline = KandinskyV22PriorEmb2EmbPipeline.from_pretrained(
68
+ "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16, use_safetensors=False
69
+ ).to("cuda")
70
+ return prior_pipeline
71
+
72
+ def KandinskyImg2ImgPipeline(self):
73
+ """
74
+ Fetches the Kandinsky img2img pipeline.
75
+
76
+ Returns:
77
+ img2img_pipeline (KandinskyV22ControlnetImg2ImgPipeline): The Kandinsky img2img pipeline.
78
+
79
+ """
80
+ img2img_pipeline = KandinskyV22ControlnetImg2ImgPipeline.from_pretrained(
81
+ "kandinsky-community/kandinsky-2-2-controlnet-depth", torch_dtype=torch.float16, use_safetensors=False
82
+ ).to("cuda")
83
+ return img2img_pipeline
84
+
85
+
86
+
87
+ def fetch_control_pipeline(controlnet_adapter_model_name,controlnet_base_model_name,kandinsky_model_name,image):
88
+ pipe_fetcher = PipelineFetcher(controlnet_adapter_model_name,controlnet_base_model_name,kandinsky_model_name,image)
89
+ pipe = pipe_fetcher.ControlNetInpaintPipeline()
90
+ return pipe
91
+
92
+ def fetch_kandinsky_pipeline(controlnet_adapter_model_name,controlnet_base_model_name,kandinsky_model_name,image):
93
+ pipe_fetcher = PipelineFetcher(controlnet_adapter_model_name,controlnet_base_model_name,kandinsky_model_name,image)
94
+ pipe = pipe_fetcher.KandinskyPipeline()
95
+ return pipe
96
+
97
+ def fetch_kandinsky_prior_pipeline(controlnet_adapter_model_name,controlnet_base_model_name,kandinsky_model_name,image):
98
+ pipe_fetcher = PipelineFetcher(controlnet_adapter_model_name,controlnet_base_model_name,kandinsky_model_name,image)
99
+ pipe = pipe_fetcher.KandinskyPriorPipeline()
100
+ return pipe
101
+
102
+ def fetch_kandinsky_img2img_pipeline(controlnet_adapter_model_name, controlnet_base_model_name, kandinsky_model_name, image):
103
+ """
104
+ Fetches the Kandinsky image-to-image pipeline.
105
+
106
+ Args:
107
+ controlnet_adapter_model_name (str): The name of the controlnet adapter model.
108
+ controlnet_base_model_name (str): The name of the controlnet base model.
109
+ kandinsky_model_name (str): The name of the Kandinsky model.
110
+ image: The input image.
111
+
112
+ Returns:
113
+ pipe: The Kandinsky image-to-image pipeline.
114
+ """
115
+ pipe_fetcher = PipelineFetcher(controlnet_adapter_model_name, controlnet_base_model_name, kandinsky_model_name, image)
116
+ pipe = pipe_fetcher.KandinskyImg2ImgPipeline()
117
+ return pipe
118
+ def fetch_kandinsky_img2img_pipeline(controlnet_adapter_model_name,controlnet_base_model_name,kandinsky_model_name,image):
119
+ pipe_fetcher = PipelineFetcher(controlnet_adapter_model_name,controlnet_base_model_name,kandinsky_model_name,image)
120
+ pipe = pipe_fetcher.KandinskyImg2ImgPipeline()
121
+ return pipe