File size: 4,941 Bytes
2a51440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
"""

# ── Model loading ──────────────────────────────────────────────────────────────
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
    """
    # strip optional "model_" prefix
    key = model_id.removeprefix("model_")   # e.g. "12"
    arch_digit = key[0]                      # first digit encodes architecture

    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)
    # support both raw state-dicts and checkpoint dicts
    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


# ── Example usage ──────────────────────────────────────────────────────────────
# Replace the paths below with the actual locations of your weight files.
#
# MODEL_WEIGHTS = {
#     "model_00": "/path/to/weights/model_00.pth",
#     "model_01": "/path/to/weights/model_01.pth",
#     "model_02": "/path/to/weights/model_02.pth",
#     "model_10": "/path/to/weights/model_10.pth",
#     "model_11": "/path/to/weights/model_11.pth",
#     "model_12": "/path/to/weights/model_12.pth",
#     "model_20": "/path/to/weights/model_20.pth",
#     "model_21": "/path/to/weights/model_21.pth",
#     "model_22": "/path/to/weights/model_22.pth",
# }
#
# for model_id, path in MODEL_WEIGHTS.items():
#     model = load_model(model_id, path, device="cpu")
#     print(f"Loaded {model_id}: {model.__class__.__name__}")
# ──────────────────────────────────────────────────────────────────────────────



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)