| import os |
| import sys |
| import requests |
|
|
| BASE_URL = "http://35.192.205.84" |
| API_KEY = "YOUR_API_KEY_HERE" |
|
|
| TASK_ID = "11-duci" |
| FILE_PATH = "sample_submission.csv" |
|
|
|
|
| """ |
| MODEL_ID = model_{0i} ---> ResNet18 |
| MODEL_ID = model_{1i} ---> ResNet50 |
| MODEL_ID = model_{2i} ---> ResNet152 |
| """ |
|
|
| |
| import torch |
| import torchvision.models as models |
|
|
| NUM_CLASSES = 100 |
|
|
| ARCHITECTURE_MAP = { |
| "0": models.resnet18, |
| "1": models.resnet50, |
| "2": models.resnet152, |
| } |
|
|
| def build_model(model_id: str) -> torch.nn.Module: |
| """ |
| Build a model from a MODEL_ID string like 'model_00', 'model_12', etc. |
| |
| Naming convention: |
| model_{arch}{instance} |
| arch 0 β ResNet18 | 1 β ResNet50 | 2 β ResNet152 |
| instance 0, 1, 2 (three independently trained seeds per architecture) |
| |
| Examples: |
| model_00, model_01, model_02 β ResNet18 |
| model_10, model_11, model_12 β ResNet50 |
| model_20, model_21, model_22 β ResNet152 |
| """ |
| |
| key = model_id.removeprefix("model_") |
| arch_digit = key[0] |
|
|
| if arch_digit not in ARCHITECTURE_MAP: |
| raise ValueError( |
| f"Unknown architecture digit '{arch_digit}' in model_id '{model_id}'. " |
| f"Expected 0 (ResNet18), 1 (ResNet50), or 2 (ResNet152)." |
| ) |
|
|
| model_fn = ARCHITECTURE_MAP[arch_digit] |
| model = model_fn(weights=None, num_classes=NUM_CLASSES) |
| return model |
|
|
|
|
| def load_model(model_id: str, weights_path: str, |
| device: str = "cpu") -> torch.nn.Module: |
| """ |
| Instantiate the correct architecture for *model_id* and load weights |
| from *weights_path*. |
| |
| Args: |
| model_id: e.g. 'model_00', 'model_12', 'model_21' |
| weights_path: path to the saved state-dict (.pth / .pt file) |
| device: 'cpu', 'cuda', 'cuda:0', etc. |
| |
| Returns: |
| model in eval mode with weights loaded. |
| """ |
| model = build_model(model_id) |
| state_dict = torch.load(weights_path, map_location=device) |
| |
| if isinstance(state_dict, dict) and "state_dict" in state_dict: |
| state_dict = state_dict["state_dict"] |
| model.load_state_dict(state_dict) |
| model.to(device) |
| model.eval() |
| return model |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
|
|
| def die(msg): |
| print(f"{msg}", file=sys.stderr) |
| sys.exit(1) |
|
|
|
|
| if not os.path.isfile(FILE_PATH): |
| die(f"File not found: {FILE_PATH}") |
|
|
| try: |
| with open(FILE_PATH, "rb") as f: |
| files = { |
| "file": (os.path.basename(FILE_PATH), f, "csv"), |
| } |
| resp = requests.post( |
| f"{BASE_URL}/submit/{TASK_ID}", |
| headers={"X-API-Key": API_KEY}, |
| files=files, |
| timeout=(10, 120), |
| ) |
| try: |
| body = resp.json() |
| except Exception: |
| body = {"raw_text": resp.text} |
|
|
| if resp.status_code == 413: |
| die("Upload rejected: file too large (HTTP 413). Reduce size and try again.") |
|
|
| resp.raise_for_status() |
|
|
| submission_id = body.get("submission_id") |
| print("Successfully submitted.") |
| print("Server response:", body) |
| if submission_id: |
| print(f"Submission ID: {submission_id}") |
|
|
| except requests.exceptions.RequestException as e: |
| detail = getattr(e, "response", None) |
| print(f"Submission error: {e}") |
| if detail is not None: |
| try: |
| print("Server response:", detail.json()) |
| except Exception: |
| print("Server response (text):", detail.text) |
| sys.exit(1) |