{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "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",
      "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": 2,
   "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": null,
   "id": "european-wings",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 154256 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 174716 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['tick', 'rock crab', 'king crab', 'Dungeness crab', 'fiddler crab']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 95076 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['banjo', 'cowboy hat', 'microphone', 'acoustic guitar', 'electric guitar']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 165448 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['squirrel monkey', 'spider monkey', 'titi', 'patas', 'indri']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 164412 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ping-pong ball', 'parallel bars', 'horizontal bar', 'volleyball', 'racket']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 150900 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['television', 'screen', 'oscilloscope', 'hand-held computer', 'monitor']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 114452 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['coil', 'space heater', 'maze', 'electric fan', 'plate rack']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 155124 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['studio couch', 'pajama', 'shower cap', 'quilt', 'Italian greyhound']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 178176 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Blenheim spaniel', 'cocker spaniel', 'Sussex spaniel', 'Japanese spaniel', 'Brittany spaniel']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 190828 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Australian terrier', 'silky terrier', 'Yorkshire terrier', 'Norfolk terrier', 'Norwich terrier']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 169988 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['cabbage butterfly', 'ringlet', 'sulphur butterfly', 'lycaenid', 'admiral']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 176520 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['buckeye', 'sea urchin', 'dung beetle', 'honeycomb', 'acorn']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 179096 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Norwegian elkhound', 'keeshond', 'German shepherd', 'Eskimo dog', 'malamute']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 154688 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['mousetrap', 'harvester', 'rocking chair', 'plow', 'shopping cart']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 198088 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['African elephant', 'tusker', 'Indian elephant', 'American black bear', 'water buffalo']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 206556 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Australian terrier', 'silky terrier', 'Yorkshire terrier', 'Norfolk terrier', 'Norwich terrier']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 163800 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['buckle', 'shield', 'hair slide', 'thimble', 'bottlecap']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 207456 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['timber wolf', 'red wolf', 'coyote', 'grey fox', 'wallaby']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 202164 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Eskimo dog', 'Siberian husky', 'malamute', 'Samoyed', 'Great Pyrenees']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 195172 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hippopotamus', 'otter', 'German short-haired pointer', 'warthog', 'wild boar']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 137056 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['mantis', 'walking stick', 'lacewing', 'leafhopper', 'cicada']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 91464 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['grand piano', 'upright', 'organ', 'violin', 'cello']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 169096 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['coucal', 'bulbul', 'prairie chicken', 'partridge', 'chickadee']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 196964 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['organ', 'vault', 'church', 'altar', 'coil']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 159908 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['fire screen', 'brass', 'isopod', 'wood rabbit', 'scorpion']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 166416 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['barn', 'boathouse', 'freight car', 'viaduct', 'worm fence']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 146584 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['plate rack', 'wok', 'washbasin', 'microwave', 'frying pan']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 196968 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ballplayer', 'baseball', 'torch', 'scoreboard', 'soccer ball']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 183736 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Bouvier des Flandres', 'toy poodle', 'Kerry blue terrier', 'miniature poodle', 'affenpinscher']\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 209556 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['barracouta', 'gar', 'tench', 'reel', 'sturgeon']\n"
     ]
    }
   ],
   "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",
    "        plt.imshow(image, interpolation='nearest')\n",
    "        plt.show()\n",
    "\n",
    "        # imshow.set_data(image)\n",
    "        # plt.draw()\n",
    "        # plt.show()\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
}
