MamaPearl commited on
Commit
0dabfa6
·
verified ·
1 Parent(s): e20d75b

Update train_robust.py

Browse files
Files changed (1) hide show
  1. train_robust.py +16 -8
train_robust.py CHANGED
@@ -15,7 +15,7 @@ SAVE_EVERY = 10
15
  CHECKPOINT_DIR = "./checkpoints"
16
  BEST_MODEL_DIR = "./nula-best-model"
17
 
18
- def train_one_epoch(model, loader, optimizer, device, grad_clip=1.0):
19
  model.train()
20
 
21
  total_loss = 0.0
@@ -34,7 +34,7 @@ def train_one_epoch(model, loader, optimizer, device, grad_clip=1.0):
34
 
35
  if mask_aug.any():
36
  with pt.no_grad():
37
- x_image = x * STD + MEAN
38
  choices = pt.randint(0, 3, (B,), device=x.device)
39
  mask_resize = mask_aug & (choices == 0)
40
  if mask_resize.any():
@@ -58,11 +58,11 @@ def train_one_epoch(model, loader, optimizer, device, grad_clip=1.0):
58
  mask_blur = mask_aug & (choices == 2)
59
  if mask_blur.any():
60
  x_subset = x_image[mask_blur]
61
- x_down = GLOBAL_POOL_BLUR(x_subset)
62
  x_up = F.interpolate(x_down, size=x_subset.shape[-2:], mode="bilinear", align_corners=False)
63
  x_image[mask_blur] = x_up
64
 
65
- x = (x_image - MEAN) / STD
66
 
67
  out = model(pixel_values=x, labels=y)
68
  loss = out.loss
@@ -110,16 +110,24 @@ if __name__ == "__main__":
110
  cosine = CosineAnnealingLR(optimizer, T_max=45)
111
  scheduler = SequentialLR(optimizer, schedulers=[warmup, cosine], milestones=[5])
112
 
113
- MEAN = pt.tensor([0.5, 0.5, 0.5], device=DEVICE).view(1, 3, 1, 1)
114
- STD = pt.tensor([0.5, 0.5, 0.5], device=DEVICE).view(1, 3, 1, 1)
115
  GLOBAL_POOL_BLUR = BlurPool2d(channels=cfg.in_channels, stride=2).to(DEVICE)
116
  best_val_acc = 0.0
117
 
118
  os.makedirs(CHECKPOINT_DIR, exist_ok=True)
119
 
120
  for epoch in range(1, NUM_EPOCHS + 1):
121
- train_loss, train_acc = train_one_epoch(model, train_loader, optimizer, DEVICE)
122
- val_loss, val_acc = evaluate(model, test_loader, DEVICE)
 
 
 
 
 
 
 
 
123
  scheduler.step()
124
 
125
  if epoch % SAVE_EVERY == 0:
 
15
  CHECKPOINT_DIR = "./checkpoints"
16
  BEST_MODEL_DIR = "./nula-best-model"
17
 
18
+ def train_one_epoch(model, loader, optimizer, device, mean, std, blur, grad_clip=1.0):
19
  model.train()
20
 
21
  total_loss = 0.0
 
34
 
35
  if mask_aug.any():
36
  with pt.no_grad():
37
+ x_image = x * std + mean
38
  choices = pt.randint(0, 3, (B,), device=x.device)
39
  mask_resize = mask_aug & (choices == 0)
40
  if mask_resize.any():
 
58
  mask_blur = mask_aug & (choices == 2)
59
  if mask_blur.any():
60
  x_subset = x_image[mask_blur]
61
+ x_down = blur(x_subset)
62
  x_up = F.interpolate(x_down, size=x_subset.shape[-2:], mode="bilinear", align_corners=False)
63
  x_image[mask_blur] = x_up
64
 
65
+ x = (x_image - mean) / std
66
 
67
  out = model(pixel_values=x, labels=y)
68
  loss = out.loss
 
110
  cosine = CosineAnnealingLR(optimizer, T_max=45)
111
  scheduler = SequentialLR(optimizer, schedulers=[warmup, cosine], milestones=[5])
112
 
113
+ MEAN = pt.tensor([0.5, 0.5, 0.5], device=DEVICE).view(1, 3, 1, 1)
114
+ STD = pt.tensor([0.5, 0.5, 0.5], device=DEVICE).view(1, 3, 1, 1)
115
  GLOBAL_POOL_BLUR = BlurPool2d(channels=cfg.in_channels, stride=2).to(DEVICE)
116
  best_val_acc = 0.0
117
 
118
  os.makedirs(CHECKPOINT_DIR, exist_ok=True)
119
 
120
  for epoch in range(1, NUM_EPOCHS + 1):
121
+ train_loss, train_acc = train_one_epoch(
122
+ model,
123
+ train_loader,
124
+ optimizer,
125
+ DEVICE,
126
+ MEAN,
127
+ STD,
128
+ GLOBAL_POOL_BLUR
129
+ )
130
+ val_loss, val_acc = evaluate(model, test_loader, DEVICE)
131
  scheduler.step()
132
 
133
  if epoch % SAVE_EVERY == 0: