File size: 10,040 Bytes
77875f7 8f2252f 77875f7 8f2252f 77875f7 8f2252f e73e119 8f2252f 8f1fb11 c000764 8f2252f 8f1fb11 8f2252f 8f1fb11 c000764 e73e119 c000764 e73e119 c000764 f0be6d7 c000764 e73e119 c000764 b1be50b c000764 8f2252f 8f1fb11 8f2252f 8f1fb11 8f2252f 8f1fb11 8f2252f c000764 8f1fb11 b1be50b c000764 8f2252f c000764 8f2252f 0fc6977 8f2252f 2e47c02 c000764 b1be50b c000764 b1be50b 8f2252f c000764 8f2252f c000764 8f2252f c000764 8f2252f c000764 8f1fb11 c000764 8f2252f 8f1fb11 8f2252f 8f1fb11 8f2252f 2e47c02 8f2252f 8f1fb11 8f2252f 8f1fb11 8f2252f b1be50b 8f2252f 8f1fb11 8f2252f 8f1fb11 8f2252f 8f1fb11 8f2252f e73e119 8f2252f 2e47c02 8f2252f 8f1fb11 8f2252f 8f1fb11 8f2252f 8f1fb11 e73e119 8f2252f 8f1fb11 8f2252f 8f1fb11 8f2252f 8f1fb11 b1be50b c000764 8f1fb11 8f2252f 77875f7 4598f99 f0be6d7 4598f99 894f232 8f2252f 894f232 8f2252f e73e119 8f2252f 77875f7 8f2252f 77875f7 8f1fb11 815a0c4 8f2252f 77875f7 8f1fb11 8f2252f d10e961 96597c6 b1be50b d10e961 b1be50b 4d6ee04 3735d66 b1be50b 4d6ee04 3735d66 b791c77 b1be50b b791c77 b18eb82 3735d66 b18eb82 d10e961 b18eb82 f0be6d7 |
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
import os
import numpy as np
import pandas as pd
import timm
import torch
import torch.nn as nn
from PIL import Image
from timm.models.metaformer import MlpHead
from torch.utils.data import DataLoader, Dataset
from tqdm import tqdm
from albumentations import Compose, Normalize, Resize
from albumentations.pytorch import ToTensorV2
import cv2
DIM = 518
DATE_SIZE = 4
GEO_SIZE = 7
SUBSTRATE_SIZE = 73
NUM_CLASSES = 1717
TIME = ["m0", "m1", "d0", "d1"]
GEO = ["g0", "g1", "g2", "g3", "g4", "g5", "g_float"]
SUBSTRATE = [
"substrate_0",
"substrate_1",
"substrate_2",
"substrate_3",
"substrate_4",
"substrate_5",
"substrate_6",
"substrate_7",
"substrate_8",
"substrate_9",
"substrate_10",
"substrate_11",
"substrate_12",
"substrate_13",
"substrate_14",
"substrate_15",
"substrate_16",
"substrate_17",
"substrate_18",
"substrate_19",
"substrate_20",
"substrate_21",
"substrate_22",
"substrate_23",
"substrate_24",
"substrate_25",
"substrate_26",
"substrate_27",
"substrate_28",
"substrate_29",
"substrate_30",
"metasubstrate_0",
"metasubstrate_1",
"metasubstrate_2",
"metasubstrate_3",
"metasubstrate_4",
"metasubstrate_5",
"metasubstrate_6",
"metasubstrate_7",
"metasubstrate_8",
"metasubstrate_9",
"habitat_0",
"habitat_1",
"habitat_2",
"habitat_3",
"habitat_4",
"habitat_5",
"habitat_6",
"habitat_7",
"habitat_8",
"habitat_9",
"habitat_10",
"habitat_11",
"habitat_12",
"habitat_13",
"habitat_14",
"habitat_15",
"habitat_16",
"habitat_17",
"habitat_18",
"habitat_19",
"habitat_20",
"habitat_21",
"habitat_22",
"habitat_23",
"habitat_24",
"habitat_25",
"habitat_26",
"habitat_27",
"habitat_28",
"habitat_29",
"habitat_30",
"habitat_31",
]
class ImageDataset(Dataset):
def __init__(self, df, local_filepath):
self.df = df
self.transform = Compose(
[
Resize(DIM, DIM),
Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)),
ToTensorV2(),
]
)
self.local_filepath = local_filepath
self.filepaths = df["image_path"].to_list()
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
image_path = os.path.join(self.local_filepath, self.filepaths[idx])
# print("Reading from ", image_path)
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return self.transform(image=image)["image"]
class EmbeddingMetadataDataset(Dataset):
def __init__(self, df):
self.df = df
self.emb = df["embedding"]
self.metadata_date = df[TIME].to_numpy()
self.metadata_geo = df[GEO].to_numpy()
self.metadata_substrate = df[SUBSTRATE].to_numpy()
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
embedding = torch.Tensor(self.emb[idx].copy()).type(torch.float)
metadata = {
"date": torch.from_numpy(self.metadata_date[idx, :]).type(torch.float),
"geo": torch.from_numpy(self.metadata_geo[idx, :]).type(torch.float),
"substr": torch.from_numpy(self.metadata_substrate[idx, :]).type(
torch.float
),
}
return embedding, metadata
def generate_embeddings(metadata_file_path, root_dir):
DINOV2_CKPT = "./checkpoints/dinov2.bin"
metadata_df = pd.read_csv(metadata_file_path)
test_dataset = ImageDataset(metadata_df, local_filepath=root_dir)
loader = DataLoader(test_dataset, batch_size=3, shuffle=False)
device = torch.device('cpu')
model = timm.create_model(
"timm/vit_large_patch14_reg4_dinov2.lvd142m", pretrained=False
)
weights = torch.load(DINOV2_CKPT)
model.load_state_dict(weights)
model = model.to(device)
model.eval()
all_embs = []
for img in tqdm(loader):
img = img.to(device)
emb = model.forward(img)
all_embs.append(emb.detach().cpu().numpy())
all_embs = np.vstack(all_embs)
embs_list = [x for x in all_embs]
metadata_df["embedding"] = embs_list
return metadata_df
class StarReLU(nn.Module):
"""
StarReLU: s * relu(x) ** 2 + b
"""
def __init__(
self,
scale_value=1.0,
bias_value=0.0,
scale_learnable=True,
bias_learnable=True,
mode=None,
inplace=False,
):
super().__init__()
self.inplace = inplace
self.relu = nn.ReLU(inplace=inplace)
self.scale = nn.Parameter(
scale_value * torch.ones(1), requires_grad=scale_learnable
)
self.bias = nn.Parameter(
bias_value * torch.ones(1), requires_grad=bias_learnable
)
def forward(self, x):
return self.scale * self.relu(x) ** 2 + self.bias
class FungiMEEModel(nn.Module):
def __init__(
self,
num_classes=NUM_CLASSES,
dim=1024,
):
super().__init__()
print("Setting up Pytorch Model")
self.device = torch.device('cpu')
print(f"Using devide: {self.device}")
self.date_embedding = MlpHead(
dim=DATE_SIZE, num_classes=dim, mlp_ratio=128, act_layer=StarReLU
)
self.geo_embedding = MlpHead(
dim=GEO_SIZE, num_classes=dim, mlp_ratio=128, act_layer=StarReLU
)
self.substr_embedding = MlpHead(
dim=SUBSTRATE_SIZE,
num_classes=dim,
mlp_ratio=8,
act_layer=StarReLU,
)
self.encoder = nn.TransformerEncoder(
nn.TransformerEncoderLayer(d_model=dim, nhead=8, batch_first=True),
num_layers=4,
)
self.head = MlpHead(dim=dim, num_classes=num_classes, drop_rate=0)
for param in self.parameters():
if param.dim() > 1:
nn.init.kaiming_normal_(param)
def forward(self, img_emb, metadata):
img_emb = img_emb.to(self.device)
date_emb = self.date_embedding.forward(metadata["date"].to(self.device))
geo_emb = self.geo_embedding.forward(metadata["geo"].to(self.device))
substr_emb = self.substr_embedding.forward(metadata["substr"].to(self.device))
full_emb = torch.stack((img_emb, date_emb, geo_emb, substr_emb), dim=1)
cls_emb = self.encoder.forward(full_emb)[:, 0, :].squeeze(1)
return self.head.forward(cls_emb)
def predict(self, img_emb, metadata):
logits = self.forward(img_emb, metadata)
return logits.argmax(1).tolist()
class FungiEnsembleModel(nn.Module):
def __init__(self, models) -> None:
super().__init__()
self.models = nn.ModuleList()
self.device = torch.device('cpu')
for model in models:
model = model.to(self.device)
model.eval()
self.models.append(model)
def forward(self, img_emb, metadata):
img_emb = img_emb.to(self.device)
probs = []
for model in self.models:
logits = model.forward(img_emb, metadata)
p = logits.softmax(dim=1).detach().cpu()
probs.append(p)
return torch.stack(probs).mean(dim=0)
def predict(self, img_emb, metadata):
logits = self.forward(img_emb, metadata)
# Any preprocess happens here
return logits.argmax(1).tolist()
def make_submission(metadata_df):
OUTPUT_CSV_PATH = "./submission.csv"
BASE_CKPT_PATH = "./checkpoints"
model_names = [
"dino_2_optuna_05242231.ckpt",
"dino_optuna_05241449.ckpt",
"dino_optuna_05241257.ckpt",
"dino_optuna_05241222.ckpt",
"dino_2_optuna_05242055.ckpt",
"dino_2_optuna_05242156.ckpt",
"dino_2_optuna_05242344.ckpt",
]
models = []
for model_path in model_names:
print("loading ", model_path)
ckpt_path = os.path.join(BASE_CKPT_PATH, model_path)
ckpt = torch.load(ckpt_path)
model = FungiMEEModel()
model.load_state_dict(
{w: ckpt["model." + w] for w in model.state_dict().keys()}
)
model.eval()
models.append(model)
fungi_model = FungiEnsembleModel(models)
# ckpt_path = os.path.join(BASE_CKPT_PATH, "dino_2_optuna_05242055.ckpt")
# fungi_model = FungiMEEModel()
# ckpt = torch.load(ckpt_path)
# fungi_model.load_state_dict(
# {w: ckpt["model." + w] for w in fungi_model.state_dict().keys()}
# )
embedding_dataset = EmbeddingMetadataDataset(metadata_df)
loader = DataLoader(embedding_dataset, batch_size=128, shuffle=False)
preds = []
for data in tqdm(loader):
emb, metadata = data
pred = fungi_model.forward(emb, metadata)
preds.append(pred)
all_preds = torch.vstack(preds).numpy()
preds_df = metadata_df[["observation_id", "image_path"]]
preds_df["preds"] = [i for i in all_preds]
preds_df = (
preds_df[["observation_id", "preds"]]
.groupby("observation_id")
.mean()
.reset_index()
)
preds_df["class_id"] = preds_df["preds"].apply(
lambda x: x.argmax() if x.argmax() <= 1603 else -1
)
preds_df[["observation_id", "class_id"]].to_csv(OUTPUT_CSV_PATH, index=None)
print("Submission complete")
if __name__ == "__main__":
# # # # # # Real submission
import zipfile
with zipfile.ZipFile("/tmp/data/private_testset.zip", "r") as zip_ref:
zip_ref.extractall("/tmp/data/")
metadata_file_path = "./_test_preprocessed.csv"
root_dir = "/tmp/data/private_testset"
# Test submission
# metadata_file_path = "../trial_submission.csv"
# root_dir = "../data/DF_FULL"
##############
metadata_df = generate_embeddings(metadata_file_path, root_dir)
make_submission(metadata_df)
|