MCPcopy
hub / github.com/clovaai/donut / prepare_input

Method prepare_input

donut/model.py:104–133  ·  view source on GitHub ↗

Convert PIL Image to tensor according to specified input_size after following steps below: - resize - rotate (if align_long_axis is True and image is not aligned longer axis with canvas) - pad

(self, img: PIL.Image.Image, random_padding: bool = False)

Source from the content-addressed store, hash-verified

102 return x
103
104 def prepare_input(self, img: PIL.Image.Image, random_padding: bool = False) -> torch.Tensor:
105 """
106 Convert PIL Image to tensor according to specified input_size after following steps below:
107 - resize
108 - rotate (if align_long_axis is True and image is not aligned longer axis with canvas)
109 - pad
110 """
111 img = img.convert("RGB")
112 if self.align_long_axis and (
113 (self.input_size[0] > self.input_size[1] and img.width > img.height)
114 or (self.input_size[0] < self.input_size[1] and img.width < img.height)
115 ):
116 img = rotate(img, angle=-90, expand=True)
117 img = resize(img, min(self.input_size))
118 img.thumbnail((self.input_size[1], self.input_size[0]))
119 delta_width = self.input_size[1] - img.width
120 delta_height = self.input_size[0] - img.height
121 if random_padding:
122 pad_width = np.random.randint(low=0, high=delta_width + 1)
123 pad_height = np.random.randint(low=0, high=delta_height + 1)
124 else:
125 pad_width = delta_width // 2
126 pad_height = delta_height // 2
127 padding = (
128 pad_width,
129 pad_height,
130 delta_width - pad_width,
131 delta_height - pad_height,
132 )
133 return self.to_tensor(ImageOps.expand(img, padding))
134
135
136class BARTDecoder(nn.Module):

Callers 2

__getitem__Method · 0.80
inferenceMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected