File size: 13,170 Bytes
8f2252f 77875f7 8f2252f 77875f7 8f2252f 77875f7 8f2252f 77875f7 8f2252f 77875f7 815a0c4 77875f7 3735d66 77875f7 3735d66 77875f7 d10e961 77875f7 3735d66 77875f7 3735d66 77875f7 8f2252f 77875f7 8f2252f 77875f7 8f2252f 77875f7 8f2252f 77875f7 8f2252f 77875f7 8f2252f 77875f7 8f2252f 894f232 8f2252f 894f232 8f2252f 894f232 8f2252f 77875f7 8f2252f 77875f7 8f2252f 815a0c4 8f2252f 77875f7 8f2252f d10e961 8f2252f d10e961 8f2252f d10e961 8f2252f 3735d66 8f2252f 3735d66 8f2252f 3735d66 8f2252f 3735d66 8f2252f d10e961 8f2252f |
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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
import io
import os
from typing import List
import cv2
import numpy as np
import pandas as pd
import timm
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as T
from albumentations import (CenterCrop, Compose, HorizontalFlip, Normalize,
PadIfNeeded, RandomBrightnessContrast, RandomCrop,
RandomResizedCrop, Resize, VerticalFlip)
from albumentations.pytorch import ToTensorV2
from PIL import Image
from timm.layers import LayerNorm2d, SelectAdaptivePool2d
from timm.models.metaformer import MlpHead
from torch.utils.data import DataLoader, Dataset
from tqdm import tqdm
DEFAULT_WIDTH = 518
DEFAULT_HEIGHT = 518
def get_transforms(*, data, model=None, width=None, height=None):
assert data in ("train", "valid")
width = width if width else DEFAULT_WIDTH
height = height if height else DEFAULT_HEIGHT
model_mean = list(model.default_cfg["mean"]) if model else (0.5, 0.5, 0.5)
model_std = list(model.default_cfg["std"]) if model else (0.5, 0.5, 0.5)
if data == "train":
return Compose(
[
RandomResizedCrop(width, height, scale=(0.6, 1.0)),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
RandomBrightnessContrast(p=0.2),
Normalize(mean=model_mean, std=model_std),
ToTensorV2(),
]
)
elif data == "valid":
return Compose(
[
Resize(width, height),
Normalize(mean=model_mean, std=model_std),
ToTensorV2(),
]
)
DIM = 518
BASE_PATH = "../data/DF_FULL"
def generate_embeddings(metadata_file_path, root_dir):
metadata_df = pd.read_csv(metadata_file_path)
transforms = get_transforms(data="valid", width=DIM, height=DIM)
test_dataset = ImageMetadataDataset(
metadata_df, local_filepath=root_dir, transform=transforms
)
loader = DataLoader(test_dataset, batch_size=3, shuffle=False, num_workers=4)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = timm.create_model("timm/vit_large_patch14_reg4_dinov2.lvd142m", pretrained=True)
model = model.to(device)
model.eval()
all_embs = []
for data in tqdm(loader):
img, _ = data
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
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 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
class ImageMetadataDataset(Dataset):
def __init__(self, df, transform=None, local_filepath=None):
self.df = df
self.transform = transform
self.local_filepath = local_filepath
self.filepaths = df["image_path"].apply(lambda x: x.replace("jpg", "JPG")).to_list()
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):
file_path = os.path.join(self.local_filepath, self.filepaths[idx])
try:
image = cv2.imread(file_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
except:
print(file_path)
if self.transform:
augmented = self.transform(image=image)
image = augmented["image"]
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 image, metadata
DATE_SIZE = 4
GEO_SIZE = 7
SUBSTRATE_SIZE = 73
NUM_CLASSES = 1717
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("cuda:0" if torch.cuda.is_available() else "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) #.unsqueeze(0)
# print(full_emb.shape)
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)
# Any preprocess happens here
return logits.argmax(1).tolist()
class FungiEnsembleModel(nn.Module):
def __init__(self, models, softmax=True) -> None:
super().__init__()
self.models = nn.ModuleList()
self.softmax = softmax
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "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() if self.softmax else logits.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 is_gpu_available():
"""Check if the python package `onnxruntime-gpu` is installed."""
return torch.cuda.is_available()
class PytorchWorker:
"""Run inference using ONNX runtime."""
def __init__(self, model_path: str, model_name: str, number_of_categories: int = 1605):
def _load_model(model_name, model_path):
print("Setting up Pytorch Model")
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(f"Using devide: {self.device}")
model = timm.create_model(model_name, num_classes=0, pretrained=False)
# weights = torch.load(model_path, map_location=self.device)
# model.load_state_dict({w.replace("model.", ""): v for w, v in weights.items()})
return model.to(self.device).eval()
self.model = _load_model(model_name, model_path)
self.transforms = T.Compose([T.Resize((518, 518)),
T.ToTensor(),
T.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
def predict_image(self, image: np.ndarray):
"""Run inference using ONNX runtime.
:param image: Input image as numpy array.
:return: A list with logits and confidences.
"""
self.model(self.transforms(image).unsqueeze(0).to(self.device))
return [-1]
def make_submission(metadata_df, model_names=None):
OUTPUT_CSV_PATH="./submission.csv"
"""Make submission with given """
BASE_CKPT_PATH = "./checkpoints"
model_names = model_names or os.listdir(BASE_CKPT_PATH)
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['state_dict']["model." + w] for w in model.state_dict().keys()})
model.eval()
model.cuda()
models.append(model)
ensemble_model = FungiEnsembleModel(models)
embedding_dataset = EmbeddingMetadataDataset(metadata_df)
loader = DataLoader(embedding_dataset, batch_size=128, shuffle=False)
preds = []
for data in tqdm(loader):
emb, metadata = data
pred = ensemble_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__":
MODEL_PATH = "metaformer-s-224.pth"
MODEL_NAME = "timm/vit_base_patch14_reg4_dinov2.lvd142m"
# # 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"
# # 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)
|