{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "ea626626-3ef4-47a0-b171-d600b965f6e0", "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'onnxruntime'", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mModuleNotFoundError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[1]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01monnxruntime\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mort\u001b[39;00m\n\u001b[32m 2\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mcv2\u001b[39;00m\n\u001b[32m 3\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mnumpy\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mnp\u001b[39;00m\n", "\u001b[31mModuleNotFoundError\u001b[39m: No module named 'onnxruntime'" ] } ], "source": [ "import onnxruntime as ort\n", "import cv2\n", "import numpy as np\n", "from scipy.special import softmax\n", "import pandas as pd\n", "from sklearn.preprocessing import LabelBinarizer\n", "\n", "# Load training metadata and LabelBinarizer\n", "metadata_path = r\"C:\\Users\\General\\OneDrive\\Desktop\\SNAD\\train_metadata.csv\"\n", "metadata = pd.read_csv(metadata_path)\n", "\n", "labels_species = metadata['binomial']\n", "lb_species = LabelBinarizer()\n", "lb_species.fit(np.asarray(labels_species))\n", "\n", "# Load ONNX model\n", "model_path = r\"C:\\Users\\General\\OneDrive\\Desktop\\SNAD\\ViT_PreProcessing-ops11-preprocessing-int-dynam_graph.onnx\"\n", "ort_session = ort.InferenceSession(model_path)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "fe22d4c8-e484-4e41-90f9-cc0d99f3f30b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Predicted class ID: 475\n", "Predicted species name: Nerodia erythrogaster\n" ] } ], "source": [ "# Prediction function\n", "def predict_image_int32(image_path, ort_session, lb_species, height=384, width=384):\n", " \"\"\"\n", " Predict species from a single image using ONNX ViT that expects int32 input.\n", " \"\"\"\n", " # Load image\n", " image = cv2.imread(image_path)\n", " if image is None:\n", " raise FileNotFoundError(f\"Image not found: {image_path}\")\n", " \n", " # Convert BGR to RGB\n", " image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n", " \n", " # Resize\n", " image = cv2.resize(image, (width, height))\n", " \n", " # Convert to int32 (required by your model)\n", " image = image.astype(np.int32)\n", " \n", " # Run ONNX inference\n", " outputs = ort_session.run(None, {ort_session.get_inputs()[0].name: image})\n", " # print(outputs)\n", " logits = outputs[0] # shape: (772,)\n", " \n", " # Softmax to get probabilities\n", " probs = softmax(logits)\n", " pred_class = np.argmax(probs)\n", " species_name = lb_species.classes_[pred_class]\n", " \n", " return pred_class, species_name\n", "\n", "# Test single image\n", "image_path = r\"C:\\Users\\General\\OneDrive\\Desktop\\SNAD\\test_image.png\"\n", "pred_class, species_name = predict_image_int32(image_path, ort_session, lb_species)\n", "\n", "print(\"Predicted class ID:\", pred_class)\n", "print(\"Predicted species name:\", species_name)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "76decffb-1fc3-49cd-ab11-d18c6ec1ea8e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2904\n" ] } ], "source": [ "count = metadata[metadata['country'] == 'India'].shape[0]\n", "print(count)" ] }, { "cell_type": "code", "execution_count": null, "id": "a191e34c-b4fb-49d1-b774-aa19421abdc5", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.10" } }, "nbformat": 4, "nbformat_minor": 5 }