File size: 1,976 Bytes
3451660 8bbcd1d 3451660 52edce7 | 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 | import os
import sys
import requests
from pathlib import Path
import torch
import torch.nn as nn
from torchvision import datasets, transforms
from torchvision.models import resnet18
from safetensors.torch import load_file
import pandas as pd
# --------------------------------
# LOADING A MODEL (EXAMPLE: TARGET MODEL)
# --------------------------------
def make_model():
model = resnet18(weights=None)
model.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
model.maxpool = nn.Identity()
model.fc = nn.Linear(model.fc.in_features, 100)
return model
checkpoint_path = "path/to/your/model_checkpoint.safetensors" # Replace with your model checkpoint path
state_dict = load_file(checkpoint_path, device="cpu")
model = make_model()
model.load_state_dict(state_dict, strict=True)
model.eval()
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408),
(0.2675, 0.2565, 0.2761)),
])
data_root = "path/to/cifar100" # Replace with your CIFAR-100 dataset path, or where it should be downloaded
dataset = datasets.CIFAR100(root=data_root, train=False, download=True, transform=transform)
x, y = dataset[0] # Example: get the first image and label
with torch.no_grad():
logits = model(x.unsqueeze(0))
print("True label:", y)
print("Logits shape:", logits.shape) # Should be [1, 100] for CIFAR-100
print("Logits:", logits)
# # --------------------------------
# # SUBMISSION FORMAT
# # --------------------------------
"""
The submission must be a .csv file with the following format:
-"id": ID of the subset (from 0 to 359)
-"score": Stealing confidence score for each model (float)
"""
# Example Submission:
subset_ids = list(range(360))
confidence_scores = torch.rand(len(subset_ids)).tolist()
submission_df = pd.DataFrame({
"id": subset_ids,
"score": confidence_scores
})
submission_df.to_csv("example_submission.csv", index=None) |