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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import requests
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# --------------------------------
|
| 8 |
+
# DATASET
|
| 9 |
+
# --------------------------------
|
| 10 |
+
|
| 11 |
+
"""
|
| 12 |
+
Dataset contents:
|
| 13 |
+
|
| 14 |
+
-"image_ids": Tensor containing the IDs of the 100 natural images, has shape (100)
|
| 15 |
+
-"images": Tensor containing the 100 natural images, has shape (100, 3, 28, 28)
|
| 16 |
+
-"labels": Tensor of true labels for the images, has shape (100)
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
# Load the dataset
|
| 20 |
+
dataset = torch.load("natural_images.pt", weights_only=False)
|
| 21 |
+
|
| 22 |
+
print("Dataset keys:", dataset.keys())
|
| 23 |
+
print("Image IDs shape:", dataset["image_ids"].shape)
|
| 24 |
+
print("Images shape:", dataset["images"].shape)
|
| 25 |
+
print("Labels shape:", dataset["labels"].shape)
|
| 26 |
+
print("First 10 image IDs:", dataset["image_ids"][:10])
|
| 27 |
+
print("First 10 labels:", dataset["labels"][:10])
|
| 28 |
+
print("First image tensor:", dataset["images"][:1])
|
| 29 |
+
|
| 30 |
+
# --------------------------------
|
| 31 |
+
# SUBMISSION FORMAT
|
| 32 |
+
# --------------------------------
|
| 33 |
+
|
| 34 |
+
"""
|
| 35 |
+
The submission must be a .npz file of the following format:
|
| 36 |
+
|
| 37 |
+
-"image_ids": Tensor containing the IDs of the images corresponding to your adversarial examples, has shape (100)
|
| 38 |
+
-"images": Tensor containing the generated adversarial examples in the same order as the corresponding
|
| 39 |
+
natural images, has shape (100, 3, 28, 28)
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
# Example Submission:
|
| 43 |
+
|
| 44 |
+
adversarial_examples = torch.randint(0, 256, size=(100, 3, 28, 28), dtype=torch.uint8)
|
| 45 |
+
|
| 46 |
+
adversarial_examples = adversarial_examples.float() / 255.0 # normalize to [0, 1] range
|
| 47 |
+
|
| 48 |
+
images_np = adversarial_examples.detach().cpu().numpy()
|
| 49 |
+
|
| 50 |
+
image_ids = np.arange(len(images_np))
|
| 51 |
+
|
| 52 |
+
np.savez_compressed("example_submission.npz", image_ids=image_ids, images=images_np)
|
| 53 |
+
|
| 54 |
+
# --------------------------------
|
| 55 |
+
# SUBMISSION PROCESS
|
| 56 |
+
# --------------------------------
|
| 57 |
+
|
| 58 |
+
"""
|
| 59 |
+
Adversarial Examples Task β Participant Submission Guide
|
| 60 |
+
========================================================
|
| 61 |
+
|
| 62 |
+
You will upload a single **.npz** file that contains ONLY an array named **'images'**.
|
| 63 |
+
The evaluator will load your file, run shape/dtype checks against the natural images,
|
| 64 |
+
and then score it by running a fixed classifier and measuring perturbations.
|
| 65 |
+
|
| 66 |
+
Follow these rules carefully to avoid automatic rejection.
|
| 67 |
+
|
| 68 |
+
1) File format
|
| 69 |
+
--------------
|
| 70 |
+
- **Extension:** `.npz` (NumPy compressed archive)
|
| 71 |
+
- **Content:** must contain exactly one required key: `'images'`
|
| 72 |
+
- **Max file size:** 200 MB (hard limit). Larger files are rejected.
|
| 73 |
+
|
| 74 |
+
2) Array requirements
|
| 75 |
+
---------------------
|
| 76 |
+
Let `G` be the ground-truth tensor loaded:
|
| 77 |
+
|
| 78 |
+
- **Shape:** `images.shape` must match `G["images"].shape` **exactly**.
|
| 79 |
+
- If `G["images"]` is `(N, 3, H, W)`, your array must also be `(N, 3, H, W)`.
|
| 80 |
+
- No extra samples; no fewer; no different dimensions.
|
| 81 |
+
- **Dtype:** `images.dtype` must match `G["images"].dtype` **exactly**.
|
| 82 |
+
- If the GT uses `float32`, you must submit `float32`.
|
| 83 |
+
- Safe cast example: `images = np.asarray(images, dtype=np.float32)`
|
| 84 |
+
- **Finite values only:** No NaN or Inf anywhere.
|
| 85 |
+
- The evaluator checks: `torch.isfinite(images).all()`.
|
| 86 |
+
- **Contiguity:** The server will convert to a contiguous Torch tensor; standard NumPy arrays are fine.
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
3) Typical failure messages & what they mean
|
| 90 |
+
--------------------------------------------
|
| 91 |
+
- "File must be .npz and contain an 'images' array."
|
| 92 |
+
β Wrong extension or missing `'images'` key.
|
| 93 |
+
- "File too large: X bytes (limit 209715200)."
|
| 94 |
+
β Your file exceeds 200 MB.
|
| 95 |
+
- "Failed to read .npz: ..."
|
| 96 |
+
β The file is corrupted or not a valid `.npz` created with `allow_pickle=False`.
|
| 97 |
+
- "Failed to convert 'images' to torch tensor: ..."
|
| 98 |
+
β Your `'images'` array has an unsupported dtype or structure (e.g., object array).
|
| 99 |
+
- "Submitted images must have shape (N, C, H, W), but got (...)."
|
| 100 |
+
β Shape mismatch with the ground-truth images.
|
| 101 |
+
- "Submitted images must be of type torch.float32, but got torch.float64."
|
| 102 |
+
β Dtype mismatch with the ground-truth images.
|
| 103 |
+
- "Images must not contain NaN or Inf values."
|
| 104 |
+
β Clean your array: `np.isfinite(images).all()` must be True.
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
BASE_URL = "http://34.122.51.94:80"
|
| 108 |
+
API_KEY = "YOUR_API_KEY_HERE"
|
| 109 |
+
|
| 110 |
+
TASK_ID = "10-adversarial-examples"
|
| 111 |
+
|
| 112 |
+
# Path to the .npz file containing the images you want to get logits for
|
| 113 |
+
|
| 114 |
+
QUERY_PATH = "PATH/TO/YOUR/QUERY_FILE.npz"
|
| 115 |
+
|
| 116 |
+
# Path to the .npz file you want to send
|
| 117 |
+
|
| 118 |
+
FILE_PATH = "PATH/TO/YOUR/SUBMISSION.npz"
|
| 119 |
+
|
| 120 |
+
GET_LOGITS = False # set True to get logits from the API
|
| 121 |
+
SUBMIT = False # set True to submit your solution
|
| 122 |
+
|
| 123 |
+
def die(msg):
|
| 124 |
+
print(f"{msg}", file=sys.stderr)
|
| 125 |
+
sys.exit(1)
|
| 126 |
+
|
| 127 |
+
if GET_LOGITS:
|
| 128 |
+
with open(QUERY_PATH, "rb") as f:
|
| 129 |
+
files = {"npz": (QUERY_PATH, f, "application/octet-stream")}
|
| 130 |
+
response = requests.post(
|
| 131 |
+
f"{BASE_URL}/{TASK_ID}/logits",
|
| 132 |
+
files=files,
|
| 133 |
+
headers={"X-API-Key": API_KEY},
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
if response.status_code == 200:
|
| 137 |
+
data = response.json()
|
| 138 |
+
print("Request successful")
|
| 139 |
+
print(data)
|
| 140 |
+
|
| 141 |
+
else:
|
| 142 |
+
print("Request failed")
|
| 143 |
+
print("Status code:", response.status_code)
|
| 144 |
+
print("Detail:", response.text)
|
| 145 |
+
|
| 146 |
+
if SUBMIT:
|
| 147 |
+
if not os.path.isfile(FILE_PATH):
|
| 148 |
+
die(f"File not found: {FILE_PATH}")
|
| 149 |
+
|
| 150 |
+
try:
|
| 151 |
+
with open(FILE_PATH, "rb") as f:
|
| 152 |
+
files = {
|
| 153 |
+
"file": (os.path.basename(FILE_PATH), f, "csv"),
|
| 154 |
+
}
|
| 155 |
+
resp = requests.post(
|
| 156 |
+
f"{BASE_URL}/submit/{TASK_ID}",
|
| 157 |
+
headers={"X-API-Key": API_KEY},
|
| 158 |
+
files=files,
|
| 159 |
+
timeout=(10, 120),
|
| 160 |
+
)
|
| 161 |
+
try:
|
| 162 |
+
body = resp.json()
|
| 163 |
+
except Exception:
|
| 164 |
+
body = {"raw_text": resp.text}
|
| 165 |
+
|
| 166 |
+
if resp.status_code == 413:
|
| 167 |
+
die("Upload rejected: file too large (HTTP 413). Reduce size and try again.")
|
| 168 |
+
|
| 169 |
+
resp.raise_for_status()
|
| 170 |
+
|
| 171 |
+
submission_id = body.get("submission_id")
|
| 172 |
+
print("Successfully submitted.")
|
| 173 |
+
print("Server response:", body)
|
| 174 |
+
if submission_id:
|
| 175 |
+
print(f"Submission ID: {submission_id}")
|
| 176 |
+
|
| 177 |
+
except requests.exceptions.RequestException as e:
|
| 178 |
+
detail = getattr(e, "response", None)
|
| 179 |
+
print(f"Submission error: {e}")
|
| 180 |
+
if detail is not None:
|
| 181 |
+
try:
|
| 182 |
+
print("Server response:", detail.json())
|
| 183 |
+
except Exception:
|
| 184 |
+
print("Server response (text):", detail.text)
|
| 185 |
+
sys.exit(1)
|