MCPcopy Create free account
hub / github.com/ali-vilab/ACE_plus / ACEPlusDiffuserInference

Class ACEPlusDiffuserInference

inference/ace_plus_diffusers.py:15–117  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13from .utils import ACEPlusImageProcessor
14
15class ACEPlusDiffuserInference():
16 def __init__(self, logger=None):
17 if logger is None:
18 logger = get_logger(name='ace_plus')
19 self.logger = logger
20 self.input = {}
21
22 def load_default(self, cfg):
23 if cfg is not None:
24 self.input_cfg = {k.lower(): v for k, v in cfg.INPUT.items()}
25 self.input = {k.lower(): dict(v).get('DEFAULT', None) if isinstance(v, (dict, OrderedDict, Config)) else v for k, v in cfg.INPUT.items()}
26 self.output = {k.lower(): v for k, v in cfg.OUTPUT.items()}
27
28 def init_from_cfg(self, cfg):
29 self.max_seq_len = cfg.get("MAX_SEQ_LEN", 4096)
30 self.image_processor = ACEPlusImageProcessor(max_seq_len=self.max_seq_len)
31
32 local_folder = FS.get_dir_to_local_dir(cfg.MODEL.PRETRAINED_MODEL)
33
34 self.pipe = FluxFillPipeline.from_pretrained(local_folder, torch_dtype=torch.bfloat16).to(we.device_id)
35
36 tokenizer_2 = T5TokenizerFast.from_pretrained(os.path.join(local_folder, "tokenizer_2"),
37 additional_special_tokens=["{image}"])
38 self.pipe.tokenizer_2 = tokenizer_2
39 self.load_default(cfg.DEFAULT_PARAS)
40
41 def prepare_input(self,
42 image,
43 mask,
44 batch_size=1,
45 dtype = torch.bfloat16,
46 num_images_per_prompt=1,
47 height=512,
48 width=512,
49 generator=None):
50 num_channels_latents = self.pipe.vae.config.latent_channels
51 # import pdb;pdb.set_trace()
52 mask, masked_image_latents = self.pipe.prepare_mask_latents(
53 mask.unsqueeze(0),
54 image.unsqueeze(0).to(we.device_id, dtype = dtype),
55 batch_size,
56 num_channels_latents,
57 num_images_per_prompt,
58 height,
59 width,
60 dtype,
61 we.device_id,
62 generator,
63 )
64 # import pdb;pdb.set_trace()
65 masked_image_latents = torch.cat((masked_image_latents, mask), dim=-1)
66 return masked_image_latents
67
68 @torch.no_grad()
69 def __call__(self,
70 reference_image=None,
71 edit_image=None,
72 edit_mask=None,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected