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://35.192.205.84"
|
| 6 |
+
API_KEY = "YOUR_API_KEY_HERE"
|
| 7 |
+
|
| 8 |
+
TASK_ID = "11-duci"
|
| 9 |
+
FILE_PATH = "sample_submission.csv"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
"""
|
| 13 |
+
MODEL_ID = model_{0i} ---> ResNet18
|
| 14 |
+
MODEL_ID = model_{1i} ---> ResNet50
|
| 15 |
+
MODEL_ID = model_{2i} ---> ResNet152
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
# ββ Model loading ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
+
import torch
|
| 20 |
+
import torchvision.models as models
|
| 21 |
+
|
| 22 |
+
NUM_CLASSES = 100
|
| 23 |
+
|
| 24 |
+
ARCHITECTURE_MAP = {
|
| 25 |
+
"0": models.resnet18,
|
| 26 |
+
"1": models.resnet50,
|
| 27 |
+
"2": models.resnet152,
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
def build_model(model_id: str) -> torch.nn.Module:
|
| 31 |
+
"""
|
| 32 |
+
Build a model from a MODEL_ID string like 'model_00', 'model_12', etc.
|
| 33 |
+
|
| 34 |
+
Naming convention:
|
| 35 |
+
model_{arch}{instance}
|
| 36 |
+
arch 0 β ResNet18 | 1 β ResNet50 | 2 β ResNet152
|
| 37 |
+
instance 0, 1, 2 (three independently trained seeds per architecture)
|
| 38 |
+
|
| 39 |
+
Examples:
|
| 40 |
+
model_00, model_01, model_02 β ResNet18
|
| 41 |
+
model_10, model_11, model_12 β ResNet50
|
| 42 |
+
model_20, model_21, model_22 β ResNet152
|
| 43 |
+
"""
|
| 44 |
+
# strip optional "model_" prefix
|
| 45 |
+
key = model_id.removeprefix("model_") # e.g. "12"
|
| 46 |
+
arch_digit = key[0] # first digit encodes architecture
|
| 47 |
+
|
| 48 |
+
if arch_digit not in ARCHITECTURE_MAP:
|
| 49 |
+
raise ValueError(
|
| 50 |
+
f"Unknown architecture digit '{arch_digit}' in model_id '{model_id}'. "
|
| 51 |
+
f"Expected 0 (ResNet18), 1 (ResNet50), or 2 (ResNet152)."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
model_fn = ARCHITECTURE_MAP[arch_digit]
|
| 55 |
+
model = model_fn(weights=None, num_classes=NUM_CLASSES)
|
| 56 |
+
return model
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def load_model(model_id: str, weights_path: str,
|
| 60 |
+
device: str = "cpu") -> torch.nn.Module:
|
| 61 |
+
"""
|
| 62 |
+
Instantiate the correct architecture for *model_id* and load weights
|
| 63 |
+
from *weights_path*.
|
| 64 |
+
|
| 65 |
+
Args:
|
| 66 |
+
model_id: e.g. 'model_00', 'model_12', 'model_21'
|
| 67 |
+
weights_path: path to the saved state-dict (.pth / .pt file)
|
| 68 |
+
device: 'cpu', 'cuda', 'cuda:0', etc.
|
| 69 |
+
|
| 70 |
+
Returns:
|
| 71 |
+
model in eval mode with weights loaded.
|
| 72 |
+
"""
|
| 73 |
+
model = build_model(model_id)
|
| 74 |
+
state_dict = torch.load(weights_path, map_location=device)
|
| 75 |
+
# support both raw state-dicts and checkpoint dicts
|
| 76 |
+
if isinstance(state_dict, dict) and "state_dict" in state_dict:
|
| 77 |
+
state_dict = state_dict["state_dict"]
|
| 78 |
+
model.load_state_dict(state_dict)
|
| 79 |
+
model.to(device)
|
| 80 |
+
model.eval()
|
| 81 |
+
return model
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# ββ Example usage ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 85 |
+
# Replace the paths below with the actual locations of your weight files.
|
| 86 |
+
#
|
| 87 |
+
# MODEL_WEIGHTS = {
|
| 88 |
+
# "model_00": "/path/to/weights/model_00.pth",
|
| 89 |
+
# "model_01": "/path/to/weights/model_01.pth",
|
| 90 |
+
# "model_02": "/path/to/weights/model_02.pth",
|
| 91 |
+
# "model_10": "/path/to/weights/model_10.pth",
|
| 92 |
+
# "model_11": "/path/to/weights/model_11.pth",
|
| 93 |
+
# "model_12": "/path/to/weights/model_12.pth",
|
| 94 |
+
# "model_20": "/path/to/weights/model_20.pth",
|
| 95 |
+
# "model_21": "/path/to/weights/model_21.pth",
|
| 96 |
+
# "model_22": "/path/to/weights/model_22.pth",
|
| 97 |
+
# }
|
| 98 |
+
#
|
| 99 |
+
# for model_id, path in MODEL_WEIGHTS.items():
|
| 100 |
+
# model = load_model(model_id, path, device="cpu")
|
| 101 |
+
# print(f"Loaded {model_id}: {model.__class__.__name__}")
|
| 102 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def die(msg):
|
| 107 |
+
print(f"{msg}", file=sys.stderr)
|
| 108 |
+
sys.exit(1)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
if not os.path.isfile(FILE_PATH):
|
| 112 |
+
die(f"File not found: {FILE_PATH}")
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
with open(FILE_PATH, "rb") as f:
|
| 116 |
+
files = {
|
| 117 |
+
"file": (os.path.basename(FILE_PATH), f, "csv"),
|
| 118 |
+
}
|
| 119 |
+
resp = requests.post(
|
| 120 |
+
f"{BASE_URL}/submit/{TASK_ID}",
|
| 121 |
+
headers={"X-API-Key": API_KEY},
|
| 122 |
+
files=files,
|
| 123 |
+
timeout=(10, 120),
|
| 124 |
+
)
|
| 125 |
+
try:
|
| 126 |
+
body = resp.json()
|
| 127 |
+
except Exception:
|
| 128 |
+
body = {"raw_text": resp.text}
|
| 129 |
+
|
| 130 |
+
if resp.status_code == 413:
|
| 131 |
+
die("Upload rejected: file too large (HTTP 413). Reduce size and try again.")
|
| 132 |
+
|
| 133 |
+
resp.raise_for_status()
|
| 134 |
+
|
| 135 |
+
submission_id = body.get("submission_id")
|
| 136 |
+
print("Successfully submitted.")
|
| 137 |
+
print("Server response:", body)
|
| 138 |
+
if submission_id:
|
| 139 |
+
print(f"Submission ID: {submission_id}")
|
| 140 |
+
|
| 141 |
+
except requests.exceptions.RequestException as e:
|
| 142 |
+
detail = getattr(e, "response", None)
|
| 143 |
+
print(f"Submission error: {e}")
|
| 144 |
+
if detail is not None:
|
| 145 |
+
try:
|
| 146 |
+
print("Server response:", detail.json())
|
| 147 |
+
except Exception:
|
| 148 |
+
print("Server response (text):", detail.text)
|
| 149 |
+
sys.exit(1)
|