MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / __call__

Method __call__

infer.py:30–72  ·  view source on GitHub ↗
(
        self,
        prompt: Union[torch.FloatTensor, PIL.Image.Image],
        glyph: Union[torch.FloatTensor, PIL.Image.Image],
        mask_image: Union[torch.FloatTensor, PIL.Image.Image],
        mask: Union[torch.FloatTensor, PIL.Image.Image],
        num_inference_steps: int = 50,
        device=None
    )

Source from the content-addressed store, hash-verified

28
29 @torch.no_grad()
30 def __call__(
31 self,
32 prompt: Union[torch.FloatTensor, PIL.Image.Image],
33 glyph: Union[torch.FloatTensor, PIL.Image.Image],
34 mask_image: Union[torch.FloatTensor, PIL.Image.Image],
35 mask: Union[torch.FloatTensor, PIL.Image.Image],
36 num_inference_steps: int = 50,
37 device=None
38 ):
39 if mask_image is None:
40 raise ValueError("`mask_image` input cannot be undefined.")
41
42 batch_size = prompt.shape[0]
43 vae.to(device)
44 unet.to(device)
45 self.scheduler.set_timesteps(num_inference_steps, device=device)
46 timesteps = self.scheduler.timesteps
47
48 # Preprocess mask and image
49 vae_scale_factor = self.vae_scale_factor
50 _, _, mask_height, mask_width = mask.size()
51 mask = torch.nn.functional.interpolate(mask, size=[mask_width // vae_scale_factor, mask_height // vae_scale_factor])
52
53 glyph_latents = vae.encode(glyph).latent_dist.sample() * vae.config.scaling_factor
54 masked_image_latents = vae.encode(mask_image).latent_dist.sample() * vae.config.scaling_factor
55
56 shape = (batch_size, vae.config.latent_channels, mask_height // vae_scale_factor, mask_width // vae_scale_factor)
57 latents = randn_tensor(shape, generator=torch.manual_seed(20), device=device) * self.scheduler.init_noise_sigma
58
59 with self.progress_bar(total=num_inference_steps) as progress_bar:
60 for t in timesteps:
61 latent_model_input = latents
62 latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
63 # glyph_latents
64 sample = torch.cat([latent_model_input, masked_image_latents, glyph_latents, mask], dim=1)
65 noise_pred = unet(sample=sample, timestep=t, encoder_hidden_states=prompt, ).sample
66 latents = self.scheduler.step(noise_pred, t, latents).prev_sample
67 progress_bar.update()
68
69 pred_latents = latents / vae.config.scaling_factor
70 image_vae = vae.decode(pred_latents).sample
71 image = (image_vae / 2 + 0.5) * 255.0
72 return image, image_vae
73
74# Initialize models
75vae = AutoencoderKL.from_pretrained("./model/vae_ft/checkpoint-150000/vae")

Callers

nothing calls this directly

Calls 12

randn_tensorFunction · 0.90
unetFunction · 0.85
toMethod · 0.45
set_timestepsMethod · 0.45
interpolateMethod · 0.45
sampleMethod · 0.45
encodeMethod · 0.45
progress_barMethod · 0.45
scale_model_inputMethod · 0.45
stepMethod · 0.45
updateMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected