Nicolas Burrus commited on
Commit
dcf98e8
1 Parent(s): fcb1411

Fix image padding.

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -23,17 +23,19 @@ def denormalize_and_clip_as_numpy (im: Tensor) -> np.ndarray:
23
  im = im.squeeze(0)
24
  return np.ascontiguousarray(denormalize_and_clip_as_tensor(im).permute(1,2,0).detach().cpu().numpy())
25
 
 
 
 
26
  # pad image to a multiple of 64
27
  def pad_image(im, multiple=64):
28
  # B,C,H,W
29
  rows = im.shape[2]
30
  cols = im.shape[3]
31
- if rows % multiple == 0 and cols % multiple == 0:
 
 
32
  return im
33
- else:
34
- rows_to_pad = multiple - (rows % multiple)
35
- cols_to_pad = multiple - (cols % multiple)
36
- return transforms.Pad(padding=(0, 0, cols_to_pad, rows_to_pad), padding_mode='reflect')(im)
37
 
38
  def undo_antialiasing(im):
39
  im_torch = torch.from_numpy (im).permute(2,0,1).unsqueeze(0).float() / 255.0
 
23
  im = im.squeeze(0)
24
  return np.ascontiguousarray(denormalize_and_clip_as_tensor(im).permute(1,2,0).detach().cpu().numpy())
25
 
26
+ def pad_width (size: int, multiple: int):
27
+ return 0 if size % multiple == 0 else multiple - (size%multiple)
28
+
29
  # pad image to a multiple of 64
30
  def pad_image(im, multiple=64):
31
  # B,C,H,W
32
  rows = im.shape[2]
33
  cols = im.shape[3]
34
+ rows_to_pad = pad_width(rows, multiple)
35
+ cols_to_pad = pad_width(cols, multiple)
36
+ if rows_to_pad == 0 and cols_to_pad == 0:
37
  return im
38
+ return transforms.Pad(padding=(0, 0, cols_to_pad, rows_to_pad), padding_mode='reflect')(im)
 
 
 
39
 
40
  def undo_antialiasing(im):
41
  im_torch = torch.from_numpy (im).permute(2,0,1).unsqueeze(0).float() / 255.0