Ngoc Minh Pham
05/07/2024, 12:13 PMbentofile.yaml
service: 'service:InferenceService'
labels:
owner: minhpham
project: background-replacer
include:
- '*.py'
- 'models/*.py'
python:
packages:
- torch
- torchvision
- wheel
requirements_txt: './requirements.txt'
and this is my service.py script:
import bentoml
from upscaler_service import UpscalerService
from captioner_service import CaptionerService
from segmenter_service import SegmenterService
from depth_estimator_service import DepthEstimatorService
from sdxl_service import SDXLService
from PIL import Image, ImageFilter
from typing import List
from tqdm import tqdm
import torch
import numpy as np
from scipy.ndimage import binary_dilation
import os
import datetime
@bentoml.service(resources={"gpu": 2, "memory": "2Gi"}, traffic={"timeout": 600},)
class InferenceService:
developer_mode = os.getenv('DEV_MODE', False)
POSITIVE_PROMPT_SUFFIX = "commercial product photography, 24mm lens f/8"
NEGATIVE_PROMPT_SUFFIX = "cartoon, drawing, anime, semi-realistic, illustration, painting, art, text, greyscale, (black and white), lens flare, watermark, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, 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, floating, levitating"
MEGAPIXELS = 1.0
def __init__(self):
self.upscaler_service = bentoml.depends(UpscalerService)
self.captioner_service = bentoml.depends(CaptionerService)
self.segmenter_service = bentoml.depends(SegmenterService)
self.depth_estimator_service = bentoml.depends(DepthEstimatorService)
self.sdxl_service = bentoml.depends(SDXLService)
@bentoml.api
async def inference(self, image: Image.Image, positive_prompt, negative_prompt, options) -> List[Image.Image]:
torch.cuda.empty_cache()
print('Running inference...')
pbar = tqdm(total=7)
caption = await self.captioner_service.to_async.derive_caption(image)
pbar.update(1)
torch.cuda.empty_cache()
print('Original image size: ', image.size)
resized_image = self.upscaler_service.ensure_resolution(image, mega_pixels=self.MEGAPIXELS)
pbar.update(1)
torch.cuda.empty_cache()
[cropped, crop_mask]= self.segmenter_service.segment(resized_image)
pbar.update(1)
torch.cuda.empty_cache()
depth_map = self.depth_estimator_service.get_depth_map(resized_image)
pbar.update(1)
torch.cuda.empty_cache()
print("Feathering the depth map...")
crop_mask_np = np.array(crop_mask.convert('L'))
# Convert to binary and dilate (grow) the edges
# adjust threshold as needed
crop_mask_binary = crop_mask_np > options.get(
'depth_map_feather_threshold')
# adjust iterations as needed
dilated_mask = binary_dilation(
crop_mask_binary, iterations=options.get('depth_map_dilation_iterations'))
# Convert back to PIL Image
dilated_mask = Image.fromarray((dilated_mask * 255).astype(np.uint8))
# Apply Gaussian blur and normalize
dilated_mask_blurred = dilated_mask.filter(
ImageFilter.GaussianBlur(radius=options.get('depth_map_blur_radius')))
dilated_mask_blurred_np = np.array(dilated_mask_blurred) / 255.0
# Normalize depth map, apply blurred, dilated mask, and scale back
depth_map_np = np.array(depth_map.convert('L')) / 255.0
masked_depth_map_np = depth_map_np * dilated_mask_blurred_np
masked_depth_map_np = (masked_depth_map_np * 255).astype(np.uint8)
# Convert back to PIL Image
masked_depth_map = Image.fromarray(masked_depth_map_np).convert('RGB')
pbar.update(1)
final_positive_prompt = f"{caption}, {positive_prompt}, {self.POSITIVE_PROMPT_SUFFIX}"
final_negative_prompt = f"{negative_prompt}, {self.NEGATIVE_PROMPT_SUFFIX}"
print("Final positive prompt:", final_positive_prompt)
print("Final negative prompt:", final_negative_prompt)
print("Generating...")
torch.cuda.empty_cache()
generated_images = self.sdxl_service.run_pipeline(
num_inference_steps=options.get('num_inference_steps'),
num_images_per_prompt=int(options.get('num_images_per_prompt')),
controlnet_conditioning_scale=options.get('controlnet_conditioning_scale'),
guidance_scale=options.get('guidance_scale'),
num_images_to_generate=options.get('num_images_to_generate'),
positive_prompt=final_positive_prompt,
negative_prompt=final_negative_prompt,
image=[masked_depth_map],
seed=options.get('seed')
)
pbar.update(1)
torch.cuda.empty_cache()
print("Compositing...")
composited_images = [
Image.alpha_composite(
generated_image.convert('RGBA'),
self.upscaler_service.crop_centered(cropped, generated_image.size)
) for generated_image in generated_images
]
pbar.update(1)
pbar.close()
if not os.path.exists("outputs"):
os.makedirs("outputs")
for img in composited_images:
timestamp = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S-%f')
img.save(f"outputs/{timestamp}.png")
print("Done!")
if self.developer_mode:
pre_processing_images = [
[resized_image, "Resized"],
[crop_mask, "Crop mask"],
[cropped, "Cropped"],
[depth_map, "Depth map"],
[dilated_mask, "Dilated mask"],
[dilated_mask_blurred, "Dilated mask blurred"],
[masked_depth_map, "Masked depth map"]
]
return [
composited_images,
generated_images,
pre_processing_images,
caption,
]
else:
return [composited_images, None, None, None]Jian Shen Yap
05/07/2024, 2:02 PMNgoc Minh Pham
05/07/2024, 2:14 PMminhphamNgoc Minh Pham
05/07/2024, 2:14 PMJian Shen Yap
05/07/2024, 3:12 PMNgoc Minh Pham
05/07/2024, 3:38 PMNgoc Minh Pham
05/07/2024, 3:38 PMNgoc Minh Pham
05/07/2024, 9:19 PMError: [31m[serve] `serve` failed: Failed loading Bento from directory /home/bentoml/bento: Failed to import module "service": libgthread-2.0.so.0: cannot open shared object file: No such file or directory[0mJian Shen Yap
05/08/2024, 1:09 PMNgoc Minh Pham
05/08/2024, 2:11 PM