Spaces:
Runtime error
Runtime error
Commit ·
2ce295b
1
Parent(s): b0cbfee
introduce control net from diffusers
Browse files5 unmodified lines67891011121312 unmodified lines262728293031323 unmodified lines3553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364371 unmodified line4394404414424434444454464474484494504514524534544554564574584594604614624634644654664672 unmodified lines4704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035041 unmodified line5065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805811 unmodified line5835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136142 unmodified lines6176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836842 unmodified lines6876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537542 unmodified lines7577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248252 unmodified lines8288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588595 unmodified linesfrom diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteSchedulerfrom diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepSchedulerfrom controlnet_aux import OpenposeDetector, MLSDdetector, HEDdetectorfrom transformers import AutoModelForCausalLM, AutoTokenizer, CLIPSegProcessor, CLIPSegForImageSegmentationfrom transformers import pipeline, BlipProcessor, BlipForConditionalGeneration, BlipForQuestionAnsweringfrom ldm.util import instantiate_from_configfrom ControlNet.cldm.model import create_model, load_state_dictfrom ControlNet.cldm.ddim_hacked import DDIMSampler12 unmodified linesimport cv2import randomdef HWC3(x):assert x.dtype == np.uint8if x.ndim == 2:323 unmodified linesreturn updated_image_pathclass image2line:def __init__(self):print("Direct detect straight line...")self.detector = MLSDdetector()self.value_thresh = 0.1self.dis_thresh = 0.1self.resolution = 512def inference(self, inputs):print("===>Starting image2hough Inference")image = Image.open(inputs)image = np.array(image)image = HWC3(image)hough = self.detector(resize_image(image, self.resolution), self.value_thresh, self.dis_thresh)updated_image_path = get_new_image_name(inputs, func_name="line-of")hough = 255 - cv2.dilate(hough, np.ones(shape=(3, 3), dtype=np.uint8), iterations=1)image = Image.fromarray(hough)image.save(updated_image_path)return updated_image_pathclass line2image:def __init__(self, device):print("Initialize the line2image model...")model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_mlsd.pth', location='cpu'))self.model = model.to(device)self.device = deviceself.ddim_sampler = DDIMSampler(self.model)self.ddim_steps = 20self.image_resolution = 512self.num_samples = 1self.save_memory = Falseself.strength = 1.0self.guess_mode = Falseself.scale = 9.0self.seed = -1self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'def inference(self, inputs):print("===>Starting line2image Inference")image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)image = 255 - imageprompt = instruct_textimg = resize_image(HWC3(image), self.image_resolution)H, W, C = img.shapeimg = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0control = torch.stack([control for _ in range(self.num_samples)], dim=0)control = einops.rearrange(control, 'b h w c -> b c h w').clone()self.seed = random.randint(0, 65535)seed_everything(self.seed)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}shape = (4, H // 8, W // 8)self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)x_samples = self.model.decode_first_stage(samples)x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).\cpu().numpy().clip(0,255).astype(np.uint8)updated_image_path = get_new_image_name(image_path, func_name="line2image")real_image = Image.fromarray(x_samples[0]) # default the index0 imagereal_image.save(updated_image_path)return updated_image_pathclass image2hed:def __init__(self):print("Direct detect soft HED boundary...")self.detector = HEDdetector()self.resolution = 512def inference(self, inputs):1 unmodified lineimage = Image.open(inputs)image = np.array(image)image = HWC3(image)hed = self.detector(resize_image(image, self.resolution))updated_image_path = get_new_image_name(inputs, func_name="hed-boundary")image = Image.fromarray(hed)image.save(updated_image_path)return updated_image_pathclass hed2image:def __init__(self, device):print("Initialize the hed2image model...")model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_hed.pth', location='cpu'))self.model = model.to(device)self.device = deviceself.ddim_sampler = DDIMSampler(self.model)self.ddim_steps = 20self.image_resolution = 512self.num_samples = 1self.save_memory = Falseself.strength = 1.0self.guess_mode = Falseself.scale = 9.0self.seed = -1self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'2 unmodified linesimage_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)prompt = instruct_textimg = resize_image(HWC3(image), self.image_resolution)H, W, C = img.shapeimg = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0control = torch.stack([control for _ in range(self.num_samples)], dim=0)control = einops.rearrange(control, 'b h w c -> b c h w').clone()self.seed = random.randint(0, 65535)seed_everything(self.seed)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}shape = (4, H // 8, W // 8)self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13)samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)x_samples = self.model.decode_first_stage(samples)x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)updated_image_path = get_new_image_name(image_path, func_name="hed2image")real_image = Image.fromarray(x_samples[0]) # default the index0 imagereal_image.save(updated_image_path)return updated_image_pathclass image2scribble:def __init__(self):print("Direct detect scribble.")self.detector = HEDdetector()self.resolution = 512def inference(self, inputs):1 unmodified lineimage = Image.open(inputs)image = np.array(image)image = HWC3(image)detected_map = self.detector(resize_image(image, self.resolution))detected_map = HWC3(detected_map)image = resize_image(image, self.resolution)H, W, C = image.shapedetected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)detected_map = nms(detected_map, 127, 3.0)detected_map = cv2.GaussianBlur(detected_map, (0, 0), 3.0)detected_map[detected_map > 4] = 255detected_map[detected_map < 255] = 0detected_map = 255 - detected_mapupdated_image_path = get_new_image_name(inputs, func_name="scribble")image = Image.fromarray(detected_map)image.save(updated_image_path)return updated_image_pathclass scribble2image:def __init__(self, device):print("Initialize the scribble2image model...")model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_scribble.pth', location='cpu'))self.model = model.to(device)self.device = deviceself.ddim_sampler = DDIMSampler(self.model)self.ddim_steps = 20self.image_resolution = 512self.num_samples = 1self.save_memory = Falseself.strength = 1.0self.guess_mode = Falseself.scale = 9.0self.seed = -1self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'def inference(self, inputs):print("===>Starting scribble2image Inference")print(f'sketch device {self.device}')image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)prompt = instruct_textimage = 255 - imageimg = resize_image(HWC3(image), self.image_resolution)H, W, C = img.shapeimg = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0control = torch.stack([control for _ in range(self.num_samples)], dim=0)control = einops.rearrange(control, 'b h w c -> b c h w').clone()self.seed = random.randint(0, 65535)seed_everything(self.seed)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}shape = (4, H // 8, W // 8)self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13)samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)x_samples = self.model.decode_first_stage(samples)x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)updated_image_path = get_new_image_name(image_path, func_name="scribble2image")real_image = Image.fromarray(x_samples[0]) # default the index0 imagereal_image.save(updated_image_path)return updated_image_pathclass image2pose:def __init__(self):print("Direct human pose.")self.detector = OpenposeDetector()self.resolution = 512def inference(self, inputs):1 unmodified lineimage = Image.open(inputs)image = np.array(image)image = HWC3(image)detected_map, _ = self.detector(resize_image(image, self.resolution))detected_map = HWC3(detected_map)image = resize_image(image, self.resolution)H, W, C = image.shapedetected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)updated_image_path = get_new_image_name(inputs, func_name="human-pose")image = Image.fromarray(detected_map)image.save(updated_image_path)return updated_image_pathclass pose2image:def __init__(self, device):print("Initialize the pose2image model...")model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_openpose.pth', location='cpu'))self.model = model.to(device)self.device = deviceself.ddim_sampler = DDIMSampler(self.model)self.ddim_steps = 20self.image_resolution = 512self.num_samples = 1self.save_memory = Falseself.strength = 1.0self.guess_mode = Falseself.scale = 9.0self.seed = -1self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'2 unmodified linesimage_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)prompt = instruct_textimg = resize_image(HWC3(image), self.image_resolution)H, W, C = img.shapeimg = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0control = torch.stack([control for _ in range(self.num_samples)], dim=0)control = einops.rearrange(control, 'b h w c -> b c h w').clone()self.seed = random.randint(0, 65535)seed_everything(self.seed)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)cond = {"c_concat": [control], "c_crossattn": [ self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}shape = (4, H // 8, W // 8)self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13)samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)x_samples = self.model.decode_first_stage(samples)x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)updated_image_path = get_new_image_name(image_path, func_name="pose2image")real_image = Image.fromarray(x_samples[0]) # default the index0 imagereal_image.save(updated_image_path)return updated_image_pathclass image2seg:def __init__(self):print("Direct segmentations.")self.detector = UniformerDetector()self.resolution = 512def inference(self, inputs):print("===>Starting image2seg Inference")image = Image.open(inputs)image = np.array(image)image = HWC3(image)detected_map = self.detector(resize_image(image, self.resolution))detected_map = HWC3(detected_map)image = resize_image(image, self.resolution)H, W, C = image.shapedetected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)updated_image_path = get_new_image_name(inputs, func_name="segmentation")image = Image.fromarray(detected_map)image.save(updated_image_path)return updated_image_pathclass seg2image:def __init__(self, device):print("Initialize the seg2image model...")model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_seg.pth', location='cpu'))self.model = model.to(device)self.device = deviceself.ddim_sampler = DDIMSampler(self.model)self.ddim_steps = 20self.image_resolution = 512self.num_samples = 1self.save_memory = Falseself.strength = 1.0self.guess_mode = Falseself.scale = 9.0self.seed = -1self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'2 unmodified linesimage_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)prompt = instruct_textimg = resize_image(HWC3(image), self.image_resolution)H, W, C = img.shapeimg = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0control = torch.stack([control for _ in range(self.num_samples)], dim=0)control = einops.rearrange(control, 'b h w c -> b c h w').clone()self.seed = random.randint(0, 65535)seed_everything(self.seed)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}shape = (4, H // 8, W // 8)self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13)samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)x_samples = self.model.decode_first_stage(samples)x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)updated_image_path = get_new_image_name(image_path, func_name="segment2image")real_image = Image.fromarray(x_samples[0]) # default the index0 imagereal_image.save(updated_image_path)return updated_image_pathclass image2depth:def __init__(self):print("Direct depth estimation.")self.detector = MidasDetector()self.resolution = 512def inference(self, inputs):print("===>Starting image2depth Inference")image = Image.open(inputs)image = np.array(image)image = HWC3(image)detected_map, _ = self.detector(resize_image(image, self.resolution))detected_map = HWC3(detected_map)image = resize_image(image, self.resolution)H, W, C = image.shapedetected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)updated_image_path = get_new_image_name(inputs, func_name="depth")image = Image.fromarray(detected_map)image.save(updated_image_path)return updated_image_pathclass depth2image:def __init__(self, device):print("Initialize depth2image model...")model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_depth.pth', location='cpu'))self.model = model.to(device)self.device = deviceself.ddim_sampler = DDIMSampler(self.model)self.ddim_steps = 20self.image_resolution = 512self.num_samples = 1self.save_memory = Falseself.strength = 1.0self.guess_mode = Falseself.scale = 9.0self.seed = -1self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'2 unmodified linesimage_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)prompt = instruct_textimg = resize_image(HWC3(image), self.image_resolution)H, W, C = img.shapeimg = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0control = torch.stack([control for _ in range(self.num_samples)], dim=0)control = einops.rearrange(control, 'b h w c -> b c h w').clone()self.seed = random.randint(0, 65535)seed_everything(self.seed)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)cond = {"c_concat": [control], "c_crossattn": [ self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}shape = (4, H // 8, W // 8)self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)x_samples = self.model.decode_first_stage(samples)x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)updated_image_path = get_new_image_name(image_path, func_name="depth2image")real_image = Image.fromarray(x_samples[0]) # default the index0 imagereal_image.save(updated_image_path)return updated_image_pathclass image2normal:def __init__(self):print("Direct normal estimation.")self.detector = MidasDetector()self.resolution = 512self.bg_threshold = 0.4def inference(self, inputs):print("===>Starting image2 normal Inference")image = Image.open(inputs)image = np.array(image)image = HWC3(image)_, detected_map = self.detector(resize_image(image, self.resolution), bg_th=self.bg_threshold)detected_map = HWC3(detected_map)image = resize_image(image, self.resolution)H, W, C = image.shapedetected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)updated_image_path = get_new_image_name(inputs, func_name="normal-map")image = Image.fromarray(detected_map)image.save(updated_image_path)return updated_image_pathclass normal2image:def __init__(self, device):print("Initialize normal2image model...")model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_normal.pth', location='cpu'))self.model = model.to(device)self.device = deviceself.ddim_sampler = DDIMSampler(self.model)self.ddim_steps = 20self.image_resolution = 512self.num_samples = 1self.save_memory = Falseself.strength = 1.0self.guess_mode = Falseself.scale = 9.0self.seed = -1self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'2 unmodified linesimage_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)prompt = instruct_textimg = image[:, :, ::-1].copy()img = resize_image(HWC3(img), self.image_resolution)H, W, C = img.shapeimg = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0control = torch.stack([control for _ in range(self.num_samples)], dim=0)control = einops.rearrange(control, 'b h w c -> b c h w').clone()self.seed = random.randint(0, 65535)seed_everything(self.seed)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}shape = (4, H // 8, W // 8)self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13)samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)if self.save_memory:self.model.low_vram_shift(is_diffusing=False)x_samples = self.model.decode_first_stage(samples)x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)updated_image_path = get_new_image_name(image_path, func_name="normal2image")real_image = Image.fromarray(x_samples[0]) # default the index0 imagereal_image.save(updated_image_path)return updated_image_pathclass BLIPVQA:def __init__(self, device):print("Initializing BLIP VQA to %s" % device)5 unmodified lines678910111213141512 unmodified lines28293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273323 unmodified lines3973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774781 unmodified line4804814824834844854864874884894904914924934944954964974984995005015025035045055065075085092 unmodified lines5125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016021 unmodified line6046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387391 unmodified line7417427437447457467477487497507517527537547557567577587597607617627637647657667677687697702 unmodified lines7737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129132 unmodified lines91691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410452 unmodified lines104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211932 unmodified lines1196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712885 unmodified linesfrom diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteSchedulerfrom diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepSchedulerfrom controlnet_aux import OpenposeDetector, MLSDdetector, HEDdetectorfrom transformers import AutoModelForCausalLM, AutoTokenizer, CLIPSegProcessor, CLIPSegForImageSegmentationfrom transformers import pipeline, BlipProcessor, BlipForConditionalGeneration, BlipForQuestionAnsweringfrom transformers import AutoImageProcessor, UperNetForSemanticSegmentationfrom ldm.util import instantiate_from_configfrom ControlNet.cldm.model import create_model, load_state_dictfrom ControlNet.cldm.ddim_hacked import DDIMSampler12 unmodified linesimport cv2import randomdef ade_palette():return [[120, 120, 120], [180, 120, 120], [6, 230, 230], [80, 50, 50],[4, 200, 3], [120, 120, 80], [140, 140, 140], [204, 5, 255],[230, 230, 230], [4, 250, 7], [224, 5, 255], [235, 255, 7],[150, 5, 61], [120, 120, 70], [8, 255, 51], [255, 6, 82],[143, 255, 140], [204, 255, 4], [255, 51, 7], [204, 70, 3],[0, 102, 200], [61, 230, 250], [255, 6, 51], [11, 102, 255],[255, 7, 71], [255, 9, 224], [9, 7, 230], [220, 220, 220],[255, 9, 92], [112, 9, 255], [8, 255, 214], [7, 255, 224],[255, 184, 6], [10, 255, 71], [255, 41, 10], [7, 255, 255],[224, 255, 8], [102, 8, 255], [255, 61, 6], [255, 194, 7],[255, 122, 8], [0, 255, 20], [255, 8, 41], [255, 5, 153],[6, 51, 255], [235, 12, 255], [160, 150, 20], [0, 163, 255],[140, 140, 140], [250, 10, 15], [20, 255, 0], [31, 255, 0],[255, 31, 0], [255, 224, 0], [153, 255, 0], [0, 0, 255],[255, 71, 0], [0, 235, 255], [0, 173, 255], [31, 0, 255],[11, 200, 200], [255, 82, 0], [0, 255, 245], [0, 61, 255],[0, 255, 112], [0, 255, 133], [255, 0, 0], [255, 163, 0],[255, 102, 0], [194, 255, 0], [0, 143, 255], [51, 255, 0],[0, 82, 255], [0, 255, 41], [0, 255, 173], [10, 0, 255],[173, 255, 0], [0, 255, 153], [255, 92, 0], [255, 0, 255],[255, 0, 245], [255, 0, 102], [255, 173, 0], [255, 0, 20],[255, 184, 184], [0, 31, 255], [0, 255, 61], [0, 71, 255],[255, 0, 204], [0, 255, 194], [0, 255, 82], [0, 10, 255],[0, 112, 255], [51, 0, 255], [0, 194, 255], [0, 122, 255],[0, 255, 163], [255, 153, 0], [0, 255, 10], [255, 112, 0],[143, 255, 0], [82, 0, 255], [163, 255, 0], [255, 235, 0],[8, 184, 170], [133, 0, 255], [0, 255, 92], [184, 0, 255],[255, 0, 31], [0, 184, 255], [0, 214, 255], [255, 0, 112],[92, 255, 0], [0, 224, 255], [112, 224, 255], [70, 184, 160],[163, 0, 255], [153, 0, 255], [71, 255, 0], [255, 0, 163],[255, 204, 0], [255, 0, 143], [0, 255, 235], [133, 255, 0],[255, 0, 235], [245, 0, 255], [255, 0, 122], [255, 245, 0],[10, 190, 212], [214, 255, 0], [0, 204, 255], [20, 0, 255],[255, 255, 0], [0, 153, 255], [0, 41, 255], [0, 255, 204],[41, 0, 255], [41, 255, 0], [173, 0, 255], [0, 245, 255],[71, 0, 255], [122, 0, 255], [0, 255, 184], [0, 92, 255],[184, 255, 0], [0, 133, 255], [255, 214, 0], [25, 194, 194],[102, 255, 0], [92, 0, 255]]def HWC3(x):assert x.dtype == np.uint8if x.ndim == 2:323 unmodified linesreturn updated_image_path# class image2line:# def __init__(self):# print("Direct detect straight line...")# self.detector = MLSDdetector()# self.value_thresh = 0.1# self.dis_thresh = 0.1# self.resolution = 512## def inference(self, inputs):# print("===>Starting image2hough Inference")# image = Image.open(inputs)# image = np.array(image)# image = HWC3(image)# hough = self.detector(resize_image(image, self.resolution), self.value_thresh, self.dis_thresh)# updated_image_path = get_new_image_name(inputs, func_name="line-of")# hough = 255 - cv2.dilate(hough, np.ones(shape=(3, 3), dtype=np.uint8), iterations=1)# image = Image.fromarray(hough)# image.save(updated_image_path)# return updated_image_path### class line2image:# def __init__(self, device):# print("Initialize the line2image model...")# model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)# model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_mlsd.pth', location='cpu'))# self.model = model.to(device)# self.device = device# self.ddim_sampler = DDIMSampler(self.model)# self.ddim_steps = 20# self.image_resolution = 512# self.num_samples = 1# self.save_memory = False# self.strength = 1.0# self.guess_mode = False# self.scale = 9.0# self.seed = -1# self.a_prompt = 'best quality, extremely detailed'# self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'## def inference(self, inputs):# print("===>Starting line2image Inference")# image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])# image = Image.open(image_path)# image = np.array(image)# image = 255 - image# prompt = instruct_text# img = resize_image(HWC3(image), self.image_resolution)# H, W, C = img.shape# img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)# control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0# control = torch.stack([control for _ in range(self.num_samples)], dim=0)# control = einops.rearrange(control, 'b h w c -> b c h w').clone()# self.seed = random.randint(0, 65535)# seed_everything(self.seed)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}# un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}# shape = (4, H // 8, W // 8)# self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01# samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# x_samples = self.model.decode_first_stage(samples)# x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).\# cpu().numpy().clip(0,255).astype(np.uint8)# updated_image_path = get_new_image_name(image_path, func_name="line2image")# real_image = Image.fromarray(x_samples[0]) # default the index0 image# real_image.save(updated_image_path)# return updated_image_pathclass image2hed_new:def __init__(self):print("Direct detect soft HED boundary...")self.detector = HEDdetector.from_pretrained('lllyasviel/ControlNet')self.resolution = 512def inference(self, inputs):1 unmodified lineimage = Image.open(inputs)image = np.array(image)image = HWC3(image)image = Image.fromarray(resize_image(image, self.resolution))hed = self.detector(image)updated_image_path = get_new_image_name(inputs, func_name="hed-boundary")hed.save(updated_image_path)return updated_image_pathclass hed2image_new:def __init__(self, device):print("Initialize the hed2image model...")self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-hed")self.pipe = StableDiffusionControlNetPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None)self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)self.pipe.to(device)self.image_resolution = 512self.num_inference_steps = 20self.seed = -1self.unconditional_guidance_scale = 9.0self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'2 unmodified linesimage_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)img = resize_image(HWC3(image), self.image_resolution)img = Image.fromarray(img)self.seed = random.randint(0, 65535)seed_everything(self.seed)prompt = instruct_textprompt = prompt + ', ' + self.a_promptimage = \self.pipe(prompt, img, num_inference_steps=self.num_inference_steps, eta=0.0, negative_prompt=self.n_prompt,guidance_scale=self.unconditional_guidance_scale).images[0]updated_image_path = get_new_image_name(image_path, func_name="hed2image")image.save(updated_image_path)return updated_image_path# class image2hed:# def __init__(self):# print("Direct detect soft HED boundary...")# self.detector = HEDdetector()# self.resolution = 512## def inference(self, inputs):# print("===>Starting image2hed Inference")# image = Image.open(inputs)# image = np.array(image)# image = HWC3(image)# hed = self.detector(resize_image(image, self.resolution))# updated_image_path = get_new_image_name(inputs, func_name="hed-boundary")# image = Image.fromarray(hed)# image.save(updated_image_path)# return updated_image_path### class hed2image:# def __init__(self, device):# print("Initialize the hed2image model...")# model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)# model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_hed.pth', location='cpu'))# self.model = model.to(device)# self.device = device# self.ddim_sampler = DDIMSampler(self.model)# self.ddim_steps = 20# self.image_resolution = 512# self.num_samples = 1# self.save_memory = False# self.strength = 1.0# self.guess_mode = False# self.scale = 9.0# self.seed = -1# self.a_prompt = 'best quality, extremely detailed'# self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'## def inference(self, inputs):# print("===>Starting hed2image Inference")# image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])# image = Image.open(image_path)# image = np.array(image)# prompt = instruct_text# img = resize_image(HWC3(image), self.image_resolution)# H, W, C = img.shape# img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)# control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0# control = torch.stack([control for _ in range(self.num_samples)], dim=0)# control = einops.rearrange(control, 'b h w c -> b c h w').clone()# self.seed = random.randint(0, 65535)# seed_everything(self.seed)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}# un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}# shape = (4, H // 8, W // 8)# self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13)# samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# x_samples = self.model.decode_first_stage(samples)# x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)# updated_image_path = get_new_image_name(image_path, func_name="hed2image")# real_image = Image.fromarray(x_samples[0]) # default the index0 image# real_image.save(updated_image_path)# return updated_image_pathclass image2scribble_new:def __init__(self):print("Direct detect scribble.")self.detector = HEDdetector.from_pretrained('lllyasviel/ControlNet')self.resolution = 512def inference(self, inputs):1 unmodified lineimage = Image.open(inputs)image = np.array(image)image = HWC3(image)image = resize_image(image, self.resolution)image = Image.fromarray(image)scribble = self.detector(image, scribble=True)scribble = np.array(scribble)scribble = 255 - scribblescribble = Image.fromarray(scribble)updated_image_path = get_new_image_name(inputs, func_name="scribble")scribble.save(updated_image_path)return updated_image_pathclass scribble2image_new:def __init__(self, device):self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-scribble")self.pipe = StableDiffusionControlNetPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None)self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)self.pipe.to(device)self.image_resolution = 512self.num_inference_steps = 20self.seed = -1self.unconditional_guidance_scale = 9.0self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'def inference(self, inputs):print("===>Starting scribble2image Inference")image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)image = 255 - imageimg = resize_image(HWC3(image), self.image_resolution)img = Image.fromarray(img)self.seed = random.randint(0, 65535)seed_everything(self.seed)prompt = instruct_textprompt = prompt + ', ' + self.a_promptimage = \self.pipe(prompt, img, num_inference_steps=self.num_inference_steps, eta=0.0, negative_prompt=self.n_prompt,guidance_scale=self.unconditional_guidance_scale).images[0]updated_image_path = get_new_image_name(image_path, func_name="scribble2image")image.save(updated_image_path)return updated_image_path# class image2scribble:# def __init__(self):# print("Direct detect scribble.")# self.detector = HEDdetector()# self.resolution = 512## def inference(self, inputs):# print("===>Starting image2scribble Inference")# image = Image.open(inputs)# image = np.array(image)# image = HWC3(image)# detected_map = self.detector(resize_image(image, self.resolution))# detected_map = HWC3(detected_map)# image = resize_image(image, self.resolution)# H, W, C = image.shape# detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)# detected_map = nms(detected_map, 127, 3.0)# detected_map = cv2.GaussianBlur(detected_map, (0, 0), 3.0)# detected_map[detected_map > 4] = 255# detected_map[detected_map < 255] = 0# detected_map = 255 - detected_map# updated_image_path = get_new_image_name(inputs, func_name="scribble")# image = Image.fromarray(detected_map)# image.save(updated_image_path)# return updated_image_path## class scribble2image:# def __init__(self, device):# print("Initialize the scribble2image model...")# model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)# model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_scribble.pth', location='cpu'))# self.model = model.to(device)# self.device = device# self.ddim_sampler = DDIMSampler(self.model)# self.ddim_steps = 20# self.image_resolution = 512# self.num_samples = 1# self.save_memory = False# self.strength = 1.0# self.guess_mode = False# self.scale = 9.0# self.seed = -1# self.a_prompt = 'best quality, extremely detailed'# self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'## def inference(self, inputs):# print("===>Starting scribble2image Inference")# print(f'sketch device {self.device}')# image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])# image = Image.open(image_path)# image = np.array(image)# prompt = instruct_text# image = 255 - image# img = resize_image(HWC3(image), self.image_resolution)# H, W, C = img.shape# img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)# control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0# control = torch.stack([control for _ in range(self.num_samples)], dim=0)# control = einops.rearrange(control, 'b h w c -> b c h w').clone()# self.seed = random.randint(0, 65535)# seed_everything(self.seed)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}# un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}# shape = (4, H // 8, W // 8)# self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13)# samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# x_samples = self.model.decode_first_stage(samples)# x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)# updated_image_path = get_new_image_name(image_path, func_name="scribble2image")# real_image = Image.fromarray(x_samples[0]) # default the index0 image# real_image.save(updated_image_path)# return updated_image_pathclass image2pose_new:def __init__(self):self.detector = OpenposeDetector.from_pretrained('lllyasviel/ControlNet')self.resolution = 512def inference(self, inputs):1 unmodified lineimage = Image.open(inputs)image = np.array(image)image = HWC3(image)image = resize_image(image, self.resolution)image = Image.fromarray(image)pose = self.detector(image)updated_image_path = get_new_image_name(inputs, func_name="human-pose")pose.save(updated_image_path)return updated_image_pathclass pose2image_new:def __init__(self, device):self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-openpose")self.pipe = StableDiffusionControlNetPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None)self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)self.pipe.to(device)self.image_resolution = 512self.num_inference_steps = 20self.seed = -1self.unconditional_guidance_scale = 9.0self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'2 unmodified linesimage_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)img = resize_image(HWC3(image), self.image_resolution)img = Image.fromarray(img)self.seed = random.randint(0, 65535)seed_everything(self.seed)prompt = instruct_textprompt = prompt + ', ' + self.a_promptimage = \self.pipe(prompt, img, num_inference_steps=self.num_inference_steps, eta=0.0, negative_prompt=self.n_prompt,guidance_scale=self.unconditional_guidance_scale).images[0]updated_image_path = get_new_image_name(image_path, func_name="pose2image")image.save(updated_image_path)return updated_image_path# class image2pose:# def __init__(self):# print("Direct human pose.")# self.detector = OpenposeDetector()# self.resolution = 512## def inference(self, inputs):# print("===>Starting image2pose Inference")# image = Image.open(inputs)# image = np.array(image)# image = HWC3(image)# detected_map, _ = self.detector(resize_image(image, self.resolution))# detected_map = HWC3(detected_map)# image = resize_image(image, self.resolution)# H, W, C = image.shape# detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)# updated_image_path = get_new_image_name(inputs, func_name="human-pose")# image = Image.fromarray(detected_map)# image.save(updated_image_path)# return updated_image_path## class pose2image:# def __init__(self, device):# print("Initialize the pose2image model...")# model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)# model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_openpose.pth', location='cpu'))# self.model = model.to(device)# self.device = device# self.ddim_sampler = DDIMSampler(self.model)# self.ddim_steps = 20# self.image_resolution = 512# self.num_samples = 1# self.save_memory = False# self.strength = 1.0# self.guess_mode = False# self.scale = 9.0# self.seed = -1# self.a_prompt = 'best quality, extremely detailed'# self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'## def inference(self, inputs):# print("===>Starting pose2image Inference")# image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])# image = Image.open(image_path)# image = np.array(image)# prompt = instruct_text# img = resize_image(HWC3(image), self.image_resolution)# H, W, C = img.shape# img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)# control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0# control = torch.stack([control for _ in range(self.num_samples)], dim=0)# control = einops.rearrange(control, 'b h w c -> b c h w').clone()# self.seed = random.randint(0, 65535)# seed_everything(self.seed)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# cond = {"c_concat": [control], "c_crossattn": [ self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}# un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}# shape = (4, H // 8, W // 8)# self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13)# samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# x_samples = self.model.decode_first_stage(samples)# x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)# updated_image_path = get_new_image_name(image_path, func_name="pose2image")# real_image = Image.fromarray(x_samples[0]) # default the index0 image# real_image.save(updated_image_path)# return updated_image_pathclass image2seg_new:def __init__(self):print("Initialize image2segmentation Inference")self.image_processor = AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-small")self.image_segmentor = UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-small")self.resolution = 512def inference(self, inputs):image = Image.open(inputs)image = np.array(image)image = HWC3(image)image = resize_image(image, self.resolution)image = Image.fromarray(image)pixel_values = self.image_processor(image, return_tensors="pt").pixel_valueswith torch.no_grad():outputs = self.image_segmentor(pixel_values)seg = self.image_processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3palette = np.array(ade_palette())for label, color in enumerate(palette):color_seg[seg == label, :] = colorcolor_seg = color_seg.astype(np.uint8)segmentation = Image.fromarray(color_seg)updated_image_path = get_new_image_name(inputs, func_name="segmentation")segmentation.save(updated_image_path)return updated_image_pathclass seg2image_new:def __init__(self, device):self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-seg")self.pipe = StableDiffusionControlNetPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None)self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)self.pipe.to(device)self.image_resolution = 512self.num_inference_steps = 20self.seed = -1self.unconditional_guidance_scale = 9.0self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'2 unmodified linesimage_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)img = resize_image(HWC3(image), self.image_resolution)img = Image.fromarray(img)self.seed = random.randint(0, 65535)seed_everything(self.seed)prompt = instruct_textprompt = prompt + ', ' + self.a_promptimage = \self.pipe(prompt, img, num_inference_steps=self.num_inference_steps, eta=0.0, negative_prompt=self.n_prompt,guidance_scale=self.unconditional_guidance_scale).images[0]updated_image_path = get_new_image_name(image_path, func_name="segment2image")image.save(updated_image_path)return updated_image_path# class image2seg:# def __init__(self):# print("===>Starting image2seg Inference")# print("Direct segmentations.")# self.detector = UniformerDetector()# self.resolution = 512## def inference(self, inputs):# print("===>Starting image2seg Inference")# image = Image.open(inputs)# image = np.array(image)# image = HWC3(image)# detected_map = self.detector(resize_image(image, self.resolution))# detected_map = HWC3(detected_map)# image = resize_image(image, self.resolution)# H, W, C = image.shape# detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)# updated_image_path = get_new_image_name(inputs, func_name="segmentation")# image = Image.fromarray(detected_map)# image.save(updated_image_path)# return updated_image_path## class seg2image:# def __init__(self, device):# print("Initialize the seg2image model...")# model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)# model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_seg.pth', location='cpu'))# self.model = model.to(device)# self.device = device# self.ddim_sampler = DDIMSampler(self.model)# self.ddim_steps = 20# self.image_resolution = 512# self.num_samples = 1# self.save_memory = False# self.strength = 1.0# self.guess_mode = False# self.scale = 9.0# self.seed = -1# self.a_prompt = 'best quality, extremely detailed'# self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'## def inference(self, inputs):# print("===>Starting seg2image Inference")# image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])# image = Image.open(image_path)# image = np.array(image)# prompt = instruct_text# img = resize_image(HWC3(image), self.image_resolution)# H, W, C = img.shape# img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)# control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0# control = torch.stack([control for _ in range(self.num_samples)], dim=0)# control = einops.rearrange(control, 'b h w c -> b c h w').clone()# self.seed = random.randint(0, 65535)# seed_everything(self.seed)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}# un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}# shape = (4, H // 8, W // 8)# self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13)# samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# x_samples = self.model.decode_first_stage(samples)# x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)# updated_image_path = get_new_image_name(image_path, func_name="segment2image")# real_image = Image.fromarray(x_samples[0]) # default the index0 image# real_image.save(updated_image_path)# return updated_image_pathclass image2depth_new:def __init__(self):print("initialize depth estimation")self.depth_estimator = pipeline('depth-estimation')self.resolution = 512def inference(self, inputs):image = Image.open(inputs)image = np.array(image)image = HWC3(image)image = resize_image(image, self.resolution)image = Image.fromarray(image)depth = self.depth_estimator(image)['depth']depth = np.array(depth)depth = depth[:, :, None]depth = np.concatenate([depth, depth, depth], axis=2)depth = Image.fromarray(depth)updated_image_path = get_new_image_name(inputs, func_name="depth")depth.save(updated_image_path)return updated_image_pathclass depth2image_new:def __init__(self, device):self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-depth")self.pipe = StableDiffusionControlNetPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None)self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)self.pipe.to(device)self.image_resolution = 512self.num_inference_steps = 20self.seed = -1self.unconditional_guidance_scale = 9.0self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'2 unmodified linesimage_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)img = resize_image(HWC3(image), self.image_resolution)img = Image.fromarray(img)self.seed = random.randint(0, 65535)seed_everything(self.seed)prompt = instruct_textprompt = prompt + ', ' + self.a_promptimage = \self.pipe(prompt, img, num_inference_steps=self.num_inference_steps, eta=0.0, negative_prompt=self.n_prompt,guidance_scale=self.unconditional_guidance_scale).images[0]updated_image_path = get_new_image_name(image_path, func_name="depth2image")image.save(updated_image_path)return updated_image_path# class image2depth:# def __init__(self):# print("Direct depth estimation.")# self.detector = MidasDetector()# self.resolution = 512## def inference(self, inputs):# print("===>Starting image2depth Inference")# image = Image.open(inputs)# image = np.array(image)# image = HWC3(image)# detected_map, _ = self.detector(resize_image(image, self.resolution))# detected_map = HWC3(detected_map)# image = resize_image(image, self.resolution)# H, W, C = image.shape# detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)# updated_image_path = get_new_image_name(inputs, func_name="depth")# image = Image.fromarray(detected_map)# image.save(updated_image_path)# return updated_image_path## class depth2image:# def __init__(self, device):# print("Initialize depth2image model...")# model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)# model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_depth.pth', location='cpu'))# self.model = model.to(device)# self.device = device# self.ddim_sampler = DDIMSampler(self.model)# self.ddim_steps = 20# self.image_resolution = 512# self.num_samples = 1# self.save_memory = False# self.strength = 1.0# self.guess_mode = False# self.scale = 9.0# self.seed = -1# self.a_prompt = 'best quality, extremely detailed'# self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'## def inference(self, inputs):# print("===>Starting depth2image Inference")# image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])# image = Image.open(image_path)# image = np.array(image)# prompt = instruct_text# img = resize_image(HWC3(image), self.image_resolution)# H, W, C = img.shape# img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)# control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0# control = torch.stack([control for _ in range(self.num_samples)], dim=0)# control = einops.rearrange(control, 'b h w c -> b c h w').clone()# self.seed = random.randint(0, 65535)# seed_everything(self.seed)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# cond = {"c_concat": [control], "c_crossattn": [ self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}# un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}# shape = (4, H // 8, W // 8)# self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01# samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# x_samples = self.model.decode_first_stage(samples)# x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)# updated_image_path = get_new_image_name(image_path, func_name="depth2image")# real_image = Image.fromarray(x_samples[0]) # default the index0 image# real_image.save(updated_image_path)# return updated_image_pathclass image2normal_new:def __init__(self):print("normal estimation")self.depth_estimator = pipeline("depth-estimation", model="Intel/dpt-hybrid-midas")self.resolution = 512self.bg_threhold = 0.4def inference(self, inputs):image = Image.open(inputs)image = np.array(image)image = HWC3(image)image = resize_image(image, self.resolution)image = Image.fromarray(image)image = self.depth_estimator(image)['predicted_depth'][0]image = image.numpy()image_depth = image.copy()image_depth -= np.min(image_depth)image_depth /= np.max(image_depth)bg_threhold = 0.4x = cv2.Sobel(image, cv2.CV_32F, 1, 0, ksize=3)x[image_depth < bg_threhold] = 0y = cv2.Sobel(image, cv2.CV_32F, 0, 1, ksize=3)y[image_depth < bg_threhold] = 0z = np.ones_like(x) * np.pi * 2.0image = np.stack([x, y, z], axis=2)image /= np.sum(image ** 2.0, axis=2, keepdims=True) ** 0.5image = (image * 127.5 + 127.5).clip(0, 255).astype(np.uint8)image = Image.fromarray(image)updated_image_path = get_new_image_name(inputs, func_name="normal-map")image.save(updated_image_path)return updated_image_pathclass normal2image_new:def __init__(self, device):self.controlnet = ControlNetModel.from_pretrained("fusing/stable-diffusion-v1-5-controlnet-normal")self.pipe = StableDiffusionControlNetPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", controlnet=self.controlnet, safety_checker=None)self.pipe.scheduler = UniPCMultistepScheduler.from_config(self.pipe.scheduler.config)self.pipe.to(device)self.image_resolution = 512self.num_inference_steps = 20self.seed = -1self.unconditional_guidance_scale = 9.0self.a_prompt = 'best quality, extremely detailed'self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'2 unmodified linesimage_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])image = Image.open(image_path)image = np.array(image)img = resize_image(HWC3(image), self.image_resolution)img = Image.fromarray(img)self.seed = random.randint(0, 65535)seed_everything(self.seed)prompt = instruct_textprompt = prompt + ', ' + self.a_promptimage = \self.pipe(prompt, img, num_inference_steps=self.num_inference_steps, eta=0.0, negative_prompt=self.n_prompt,guidance_scale=self.unconditional_guidance_scale).images[0]updated_image_path = get_new_image_name(image_path, func_name="normal2image")image.save(updated_image_path)return updated_image_path# class image2normal:# def __init__(self):# print("Direct normal estimation.")# self.detector = MidasDetector()# self.resolution = 512# self.bg_threshold = 0.4## def inference(self, inputs):# print("===>Starting image2 normal Inference")# image = Image.open(inputs)# image = np.array(image)# image = HWC3(image)# _, detected_map = self.detector(resize_image(image, self.resolution), bg_th=self.bg_threshold)# detected_map = HWC3(detected_map)# image = resize_image(image, self.resolution)# H, W, C = image.shape# detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)# updated_image_path = get_new_image_name(inputs, func_name="normal-map")# image = Image.fromarray(detected_map)# image.save(updated_image_path)# return updated_image_path## class normal2image:# def __init__(self, device):# print("Initialize normal2image model...")# model = create_model('ControlNet/models/cldm_v15.yaml', device=device).to(device)# model.load_state_dict(load_state_dict('ControlNet/models/control_sd15_normal.pth', location='cpu'))# self.model = model.to(device)# self.device = device# self.ddim_sampler = DDIMSampler(self.model)# self.ddim_steps = 20# self.image_resolution = 512# self.num_samples = 1# self.save_memory = False# self.strength = 1.0# self.guess_mode = False# self.scale = 9.0# self.seed = -1# self.a_prompt = 'best quality, extremely detailed'# self.n_prompt = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality'## def inference(self, inputs):# print("===>Starting normal2image Inference")# image_path, instruct_text = inputs.split(",")[0], ','.join(inputs.split(',')[1:])# image = Image.open(image_path)# image = np.array(image)# prompt = instruct_text# img = image[:, :, ::-1].copy()# img = resize_image(HWC3(img), self.image_resolution)# H, W, C = img.shape# img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)# control = torch.from_numpy(img.copy()).float().to(device=self.device) / 255.0# control = torch.stack([control for _ in range(self.num_samples)], dim=0)# control = einops.rearrange(control, 'b h w c -> b c h w').clone()# self.seed = random.randint(0, 65535)# seed_everything(self.seed)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# cond = {"c_concat": [control], "c_crossattn": [self.model.get_learned_conditioning([prompt + ', ' + self.a_prompt] * self.num_samples)]}# un_cond = {"c_concat": None if self.guess_mode else [control], "c_crossattn": [self.model.get_learned_conditioning([self.n_prompt] * self.num_samples)]}# shape = (4, H // 8, W // 8)# self.model.control_scales = [self.strength * (0.825 ** float(12 - i)) for i in range(13)] if self.guess_mode else ([self.strength] * 13)# samples, intermediates = self.ddim_sampler.sample(self.ddim_steps, self.num_samples, shape, cond, verbose=False, eta=0., unconditional_guidance_scale=self.scale, unconditional_conditioning=un_cond)# if self.save_memory:# self.model.low_vram_shift(is_diffusing=False)# x_samples = self.model.decode_first_stage(samples)# x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)# updated_image_path = get_new_image_name(image_path, func_name="normal2image")# real_image = Image.fromarray(x_samples[0]) # default the index0 image# real_image.save(updated_image_path)# return updated_image_pathclass BLIPVQA:def __init__(self, device):print("Initializing BLIP VQA to %s" % device)
visual_foundation_models.py+760-331