MamaPearl commited on
Commit
a1b4ca9
·
verified ·
1 Parent(s): 1e61b20

Update augmentations.py

Browse files
Files changed (1) hide show
  1. augmentations.py +4 -0
augmentations.py CHANGED
@@ -1,7 +1,9 @@
1
  import torch as pt
2
  import torch.nn.functional as F
3
  import numbers
 
4
  def resize_down_up(x, scale=0.5, mode="bilinear"):
 
5
  if isinstance(scale, pt.Tensor):
6
  scale = scale.item()
7
  _val_invars(x, scale=scale, mode=mode, type="resize")
@@ -16,6 +18,7 @@ def resize_down_up(x, scale=0.5, mode="bilinear"):
16
  return F.interpolate(x_down, size=(H,W), mode=mode, **kwargs)
17
 
18
  def _val_invars(x, **kwargs):
 
19
  if x.ndim != 4:
20
  raise ValueError(f"x needs shape (B,C,H,W), got {x.shape}")
21
  op_type = kwargs.get("type")
@@ -55,6 +58,7 @@ def checkerboard_alias_attack(
55
  x: pt.Tensor,
56
  epsilon: float=0.5,
57
  ):
 
58
  _val_invars(x, epsilon=epsilon, type="attack")
59
  B, C, H, W = x.shape
60
  device = x.device
 
1
  import torch as pt
2
  import torch.nn.functional as F
3
  import numbers
4
+
5
  def resize_down_up(x, scale=0.5, mode="bilinear"):
6
+ # shrinks image then blows it back up to see what info was lost
7
  if isinstance(scale, pt.Tensor):
8
  scale = scale.item()
9
  _val_invars(x, scale=scale, mode=mode, type="resize")
 
18
  return F.interpolate(x_down, size=(H,W), mode=mode, **kwargs)
19
 
20
  def _val_invars(x, **kwargs):
21
+ # validate invariants
22
  if x.ndim != 4:
23
  raise ValueError(f"x needs shape (B,C,H,W), got {x.shape}")
24
  op_type = kwargs.get("type")
 
58
  x: pt.Tensor,
59
  epsilon: float=0.5,
60
  ):
61
+ # injects high-frequency noise that mimics sampling artifacts
62
  _val_invars(x, epsilon=epsilon, type="attack")
63
  B, C, H, W = x.shape
64
  device = x.device