File size: 4,538 Bytes
57095bd 810d1d3 57095bd b1f9ac8 3a5d86c 57095bd 104edab 57095bd 918ace3 57095bd 918ace3 57095bd 918ace3 57095bd 918ace3 57095bd 918ace3 57095bd 918ace3 57095bd 918ace3 57095bd 918ace3 57095bd 918ace3 57095bd 918ace3 57095bd 918ace3 57095bd f2eef6c 814c81d 0883a29 104edab 3582193 712f902 918ace3 57095bd 918ace3 9cf45b7 |
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 |
import pandas as pd
import numpy as np
import onnxruntime as ort
import os
from tqdm import tqdm
import timm
import torchvision.transforms as T
from PIL import Image
import torch
import torch.nn as nn
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=number_of_categories, pretrained=False)
# if not torch.cuda.is_available():
# model_ckpt = torch.load(model_path, map_location=torch.device("cpu"))
# else:
# model_ckpt = torch.load(model_path)
model_ckpt = torch.load(model_path, map_location=self.device)
model.load_state_dict(model_ckpt, strict=False)
msg = model.load_state_dict(model_ckpt, strict=False)
print("load_state_dict: ", msg)
# num_features = model.get_classifier().in_features
# model.classifier = nn.Linear(num_features, number_of_categories)
return model.to(self.device).eval()
self.model = _load_model(model_name, model_path)
self.transforms = T.Compose([T.Resize((299, 299)),
T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
# T.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
def predict_image(self, image: np.ndarray) -> list():
"""Run inference using ONNX runtime.
:param image: Input image as numpy array.
:return: A list with logits and confidences.
"""
# logits = self.model(self.transforms(image).unsqueeze(0).to(self.device))
self.model.eval()
outputs = self.model(self.transforms(image).unsqueeze(0).to(self.device))
_, preds = torch.max(outputs, 1)
preds = preds.cpu() # Move tensor to CPU
# post process
# max_value = torch.max(outputs)
# if max_value < -20:
# preds[0]=1604
print("preds: ", preds)
return preds.tolist() # Convert tensor to list
def make_submission(test_metadata, model_path, model_name, output_csv_path="./submission.csv", images_root_path="/tmp/data/private_testset"):
"""Make submission with given """
model = PytorchWorker(model_path, model_name)
predictions = []
for _, row in tqdm(test_metadata.iterrows(), total=len(test_metadata)):
image_path = os.path.join(images_root_path, row.image_path)
test_image = Image.open(image_path).convert("RGB")
logits = model.predict_image(test_image)
pred_class_id = logits[0] if logits[0] !=1604 else -1
predictions.append(pred_class_id)
test_metadata["class_id"] = predictions
user_pred_df = test_metadata.drop_duplicates("observation_id", keep="first")
user_pred_df[["observation_id", "class_id"]].to_csv(output_csv_path, index=None)
if __name__ == "__main__":
import zipfile
with zipfile.ZipFile("/tmp/data/private_testset.zip", 'r') as zip_ref:
zip_ref.extractall("/tmp/data")
# MODEL_PATH = './efficientnet_b3_epoch_9_delete_pre.pth' # "./efficientnet_b3_epoch_9.pth"
# MODEL_PATH = './efficientnet_b3_epoch_24_trick1.2.3_0.6067.pth'
# MODEL_PATH = './efficientnet_b3_epoch_10_trick1.2.4_0.6016.pth'
# MODEL_PATH = './efficientnet_b3_epoch_3_trick1.2.3_a0.6067_l5.6311.pth'
# MODEL_PATH = './efficientnet_b3_epoch_21_trick1.2.5_a0.7237_l17.1662.pth'
# MODEL_PATH = './efficientnet_b3_epoch_21_trcik1.5.2.pth'
# MODEL_PATH = './efficientnet_b3_epoch_28_1.4.3.pth'
# MODEL_PATH = './efficientnet_b3_epoch_28_trick1.4.3.2.pth'
MODEL_PATH = './fused_model_soup.pth'
MODEL_NAME = 'tf_efficientnet_b3_ns' #"tf_efficientnet_b1.ap_in1k"
metadata_file_path = "./FungiCLEF2024_TestMetadata.csv"
test_metadata = pd.read_csv(metadata_file_path)
make_submission(
test_metadata=test_metadata,
model_path=MODEL_PATH,
model_name=MODEL_NAME
) |