Image Classification
Transformers
Safetensors
nula
computer-vision
cnn
cifar10
adversarial-robustness
stress-test
downsampling
anti-aliasing
custom_code
Instructions to use MamaPearl/nula-cifar10-robust-v0 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use MamaPearl/nula-cifar10-robust-v0 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-classification", model="MamaPearl/nula-cifar10-robust-v0", trust_remote_code=True) pipe("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png")# Load model directly from transformers import AutoModelForImageClassification model = AutoModelForImageClassification.from_pretrained("MamaPearl/nula-cifar10-robust-v0", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update train_robust.py
Browse files- 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 *
|
| 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 =
|
| 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 -
|
| 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
|
| 114 |
-
STD
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|