{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "gentle-selling",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com\n",
      "Processing /home/uint/work/altius/target/wheels/altius_py-0.1.0-cp38-cp38-manylinux_2_28_x86_64.whl\n",
      "Installing collected packages: altius-py\n",
      "  Attempting uninstall: altius-py\n",
      "    Found existing installation: altius-py 0.1.0\n",
      "    Uninstalling altius-py-0.1.0:\n",
      "      Successfully uninstalled altius-py-0.1.0\n",
      "Successfully installed altius-py-0.1.0\n"
     ]
    }
   ],
   "source": [
    "! pip install /home/uint/work/altius/target/wheels/altius_py-0.1.0-cp38-cp38-manylinux_2_28_x86_64.whl --force-reinstall\n",
    "import altius_py\n",
    "import numpy as np\n",
    "from PIL import Image\n",
    "from torchvision import transforms\n",
    "import os, random\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "defined-religion",
   "metadata": {},
   "outputs": [],
   "source": [
    "def random_image():\n",
    "    root = \"/home/uint/work/aoha/renesas/val_png2/\"\n",
    "    dir = random.choice(os.listdir(root))\n",
    "    file = root + dir + \"/\" + random.choice(os.listdir(root + dir))\n",
    "\n",
    "    image = Image.open(file)\n",
    "\n",
    "    preprocess = transforms.Compose(\n",
    "        [\n",
    "            transforms.Resize(256),\n",
    "            transforms.CenterCrop(224),\n",
    "            transforms.ToTensor(),\n",
    "            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n",
    "        ]\n",
    "    )\n",
    "    input = preprocess(image)\n",
    "    input = input.unsqueeze(0).numpy()\n",
    "\n",
    "    return image, input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "european-wings",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'altius_py' has no attribute 'load'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[0;32mIn [24]\u001b[0m, in \u001b[0;36m<cell line: 24>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     21\u001b[0m         output \u001b[38;5;241m=\u001b[39m [labels[i]\u001b[38;5;241m.\u001b[39mstrip() \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m output]\n\u001b[1;32m     22\u001b[0m         \u001b[38;5;28mprint\u001b[39m(output)\n\u001b[0;32m---> 24\u001b[0m \u001b[43mmain\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "Input \u001b[0;32mIn [24]\u001b[0m, in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmain\u001b[39m():\n\u001b[1;32m      2\u001b[0m     labels \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/imagenet_classes.txt\u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mreadlines()\n\u001b[0;32m----> 4\u001b[0m     model \u001b[38;5;241m=\u001b[39m \u001b[43maltius_py\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mload\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../models/mobilenetv3.onnx\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m      5\u001b[0m     sess \u001b[38;5;241m=\u001b[39m altius_py\u001b[38;5;241m.\u001b[39msession(model)\n\u001b[1;32m      7\u001b[0m     image, \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m random_image()\n",
      "\u001b[0;31mAttributeError\u001b[0m: module 'altius_py' has no attribute 'load'"
     ]
    }
   ],
   "source": [
    "def main():\n",
    "    labels = open(\"../models/imagenet_classes.txt\").readlines()\n",
    "\n",
    "    model = altius_py.load(\"../models/mobilenetv3.onnx\")\n",
    "    sess = altius_py.session(model)\n",
    "\n",
    "    image, input = random_image()\n",
    "    plt.ion()\n",
    "    imshow = plt.imshow(image, interpolation='nearest')\n",
    "    plt.show()\n",
    "\n",
    "    for i in range(100):\n",
    "        image, input = random_image()\n",
    "\n",
    "        imshow.set_data(image)\n",
    "        plt.draw()\n",
    "\n",
    "        inputs = {\"input\": input}\n",
    "        output = sess.run(inputs)[0][0]\n",
    "        output = np.argsort(output)[::-1][:5]\n",
    "        output = [labels[i].strip() for i in output]\n",
    "        print(output)\n",
    "\n",
    "main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "endangered-shift",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "international-salem",
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
