{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/Tessellate-Imaging/Monk_Object_Detection/blob/master/application_model_zoo/Example%20-%20UCF101%20Action%20Recognition.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Table of contents\n",
    "\n",
    "## Installation\n",
    "\n",
    "\n",
    "## Run a classifier using pretrained weights\n",
    "\n",
    "\n",
    "## Training your own classifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Installation\n",
    "\n",
    " - Run these commands\n",
    "     \n",
    "     - git clone https://github.com/Tessellate-Imaging/Monk_Object_Detection.git\n",
    "     \n",
    "     - cd Monk_Object_Detection/18_mmaction/installation\n",
    "     \n",
    " - Select the right file and run\n",
    " \n",
    "     - chmod +x install.sh && ./install.sh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! git clone https://github.com/Tessellate-Imaging/Monk_Object_Detection.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! cd Monk_Object_Detection/18_mmaction/installation && chmod +x install.sh && ./install.sh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Running a classifier using pretrained weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.append(\"Monk_Object_Detection/18_mmaction/lib\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from infer_engine import Infer_Videos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtf = Infer_Videos();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download trained model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! wget --load-cookies /tmp/cookies.txt \"https://docs.google.com/uc?export=download&confirm=$(wget --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1-3cbejxIEhCu4oBqRt4fBg593kTeKpG0' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\\1\\n/p')&id=1-3cbejxIEhCu4oBqRt4fBg593kTeKpG0\" -O act_ucf101_trained.zip && rm -rf /tmp/cookies.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! unzip -qq act_ucf101_trained.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtf.Dataset_Params('work_dirs/classes.txt');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "config_file = \"work_dirs/config.py\"\n",
    "checkpoint_file = \"work_dirs/latest.pth\"\n",
    "\n",
    "gtf.Model_Params(config_file, checkpoint_file, use_gpu=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "def overlay_text_on_video(input_video_path, output_video_path, class_name, score):\n",
    "    cap = cv2.VideoCapture(input_video_path)\n",
    "\n",
    "    if (cap.isOpened() == False): \n",
    "        print(\"Unable to read camera feed\")\n",
    "\n",
    "    frame_width = int(cap.get(3))\n",
    "    frame_height = int(cap.get(4))\n",
    "\n",
    "    out = cv2.VideoWriter(output_video_path, \n",
    "                          cv2.VideoWriter_fourcc('M','J','P','G'), \n",
    "                          30, \n",
    "                          (frame_width,frame_height))\n",
    "\n",
    "    while(True):\n",
    "        ret, frame = cap.read()\n",
    "        \n",
    "        if ret == True: \n",
    "            cv2.putText(frame, \n",
    "                        class_name + \": \" + str(score), \n",
    "                        (10, frame_height - 50), cv2.FONT_HERSHEY_SIMPLEX, \n",
    "                        1, \n",
    "                        (0, 0, 255),\n",
    "                        3)\n",
    "            out.write(frame)\n",
    "        else:\n",
    "            break  \n",
    "\n",
    "    cap.release()\n",
    "    out.release()\n",
    "\n",
    "    cv2.destroyAllWindows() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Skiing:  0.6082520294189453\n",
      "SkateBoarding:  0.2202327346801758\n",
      "WalkingWithDog:  0.1683511734008789\n",
      "ParallelBars:  0.1427432441711426\n",
      "UnevenBars:  0.13866983413696288\n"
     ]
    }
   ],
   "source": [
    "video_path = \"work_dirs/test/vid8.avi\"\n",
    "classes, scores = gtf.Predict(video_path)\n",
    "overlay_text_on_video(video_path, \"out_vid8.avi\", classes[0], scores[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Basketball:  0.48030906677246094\n",
      "BasketballDunk:  0.30279836654663084\n",
      "CleanAndJerk:  0.12095466613769532\n",
      "Bowling:  0.11661135673522949\n",
      "HandstandWalking:  0.10883028030395508\n"
     ]
    }
   ],
   "source": [
    "video_path = \"work_dirs/test/vid7.avi\"\n",
    "classes, scores = gtf.Predict(video_path)\n",
    "overlay_text_on_video(video_path, \"out_vid7.avi\", classes[0], scores[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BoxingPunchingBag:  0.625430679321289\n",
      "Punch:  0.19515787124633788\n",
      "PlayingDhol:  0.11967282295227051\n",
      "CleanAndJerk:  0.10407372474670411\n",
      "BandMarching:  0.08694223403930663\n"
     ]
    }
   ],
   "source": [
    "video_path = \"work_dirs/test/vid6.avi\"\n",
    "classes, scores = gtf.Predict(video_path)\n",
    "overlay_text_on_video(video_path, \"out_vid6.avi\", classes[0], scores[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CricketShot:  0.3568499755859375\n",
      "HammerThrow:  0.09251106262207032\n",
      "CricketBowling:  0.07718360900878907\n",
      "Swing:  0.07637649059295654\n",
      "TennisSwing:  0.05253950119018555\n"
     ]
    }
   ],
   "source": [
    "video_path = \"work_dirs/test/vid5.avi\"\n",
    "classes, scores = gtf.Predict(video_path)\n",
    "overlay_text_on_video(video_path, \"out_vid5.avi\", classes[0], scores[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PullUps:  0.35908180236816406\n",
      "JumpingJack:  0.12416510581970215\n",
      "RockClimbingIndoor:  0.1184957504272461\n",
      "WritingOnBoard:  0.08797657012939453\n",
      "ParallelBars:  0.08728105545043946\n"
     ]
    }
   ],
   "source": [
    "video_path = \"work_dirs/test/vid4.avi\"\n",
    "classes, scores = gtf.Predict(video_path)\n",
    "overlay_text_on_video(video_path, \"out_vid4.avi\", classes[0], scores[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PlayingGuitar:  0.3193160057067871\n",
      "LongJump:  0.08718187332153321\n",
      "CleanAndJerk:  0.08348180770874024\n",
      "SumoWrestling:  0.07985279560089112\n",
      "PlayingCello:  0.0709849214553833\n"
     ]
    }
   ],
   "source": [
    "video_path = \"work_dirs/test/vid3.avi\"\n",
    "classes, scores = gtf.Predict(video_path)\n",
    "overlay_text_on_video(video_path, \"out_vid3.avi\", classes[0], scores[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Typing:  0.39598682403564456\n",
      "Billiards:  0.15178186416625977\n",
      "HighJump:  0.12249127388000489\n",
      "BabyCrawling:  0.12157865524291993\n",
      "SkateBoarding:  0.11141880035400391\n"
     ]
    }
   ],
   "source": [
    "video_path = \"work_dirs/test/vid2.avi\"\n",
    "classes, scores = gtf.Predict(video_path)\n",
    "overlay_text_on_video(video_path, \"out_vid2.avi\", classes[0], scores[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Drumming:  0.4466825866699219\n",
      "BandMarching:  0.1379043483734131\n",
      "PizzaTossing:  0.1151356601715088\n",
      "PlayingDhol:  0.11162675857543945\n",
      "JumpRope:  0.09683942794799805\n"
     ]
    }
   ],
   "source": [
    "video_path = \"work_dirs/test/vid1.avi\"\n",
    "classes, scores = gtf.Predict(video_path)\n",
    "overlay_text_on_video(video_path, \"out_vid1.avi\", classes[0], scores[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Download and reformat Dataset\n",
    "  - Dataset Credits: https://www.crcv.ucf.edu/data/UCF101.php"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2020-09-02 13:09:47--  https://www.crcv.ucf.edu/data/UCF101/UCF101.rar\n",
      "Resolving www.crcv.ucf.edu (www.crcv.ucf.edu)... 132.170.214.127\n",
      "Connecting to www.crcv.ucf.edu (www.crcv.ucf.edu)|132.170.214.127|:443... connected.\n",
      "WARNING: cannot verify www.crcv.ucf.edu's certificate, issued by ‘CN=InCommon RSA Server CA,OU=InCommon,O=Internet2,L=Ann Arbor,ST=MI,C=US’:\n",
      "  Unable to locally verify the issuer's authority.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 6932971618 (6.5G) [application/rar]\n",
      "Saving to: ‘UCF101.rar’\n",
      "\n",
      "UCF101.rar          100%[===================>]   6.46G  40.2MB/s    in 2m 47s  \n",
      "\n",
      "2020-09-02 13:12:35 (39.6 MB/s) - ‘UCF101.rar’ saved [6932971618/6932971618]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "! wget https://www.crcv.ucf.edu/data/UCF101/UCF101.rar --no-check-certificate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reading package lists... Done\n",
      "Building dependency tree       \n",
      "Reading state information... Done\n",
      "unrar is already the newest version (1:5.5.8-1).\n",
      "The following packages were automatically installed and are no longer required:\n",
      "  linux-aws-5.3-headers-5.3.0-1023 linux-aws-5.3-headers-5.3.0-1028\n",
      "  linux-aws-5.3-headers-5.3.0-1030 linux-aws-5.3-headers-5.3.0-1032\n",
      "Use 'sudo apt autoremove' to remove them.\n",
      "0 upgraded, 0 newly installed, 0 to remove and 114 not upgraded.\n"
     ]
    }
   ],
   "source": [
    "! sudo apt-get install unrar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! unrar x UCF101.rar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "! mkdir Dataset\n",
    "! mkdir Dataset/Videos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ApplyEyeMakeup\t   Drumming\t       MilitaryParade\t   Shotput\r\n",
      "ApplyLipstick\t   Fencing\t       Mixing\t\t   SkateBoarding\r\n",
      "Archery\t\t   FieldHockeyPenalty  MoppingFloor\t   Skiing\r\n",
      "BabyCrawling\t   FloorGymnastics     Nunchucks\t   Skijet\r\n",
      "BalanceBeam\t   FrisbeeCatch        ParallelBars\t   SkyDiving\r\n",
      "BandMarching\t   FrontCrawl\t       PizzaTossing\t   SoccerJuggling\r\n",
      "BaseballPitch\t   GolfSwing\t       PlayingCello\t   SoccerPenalty\r\n",
      "Basketball\t   Haircut\t       PlayingDaf\t   StillRings\r\n",
      "BasketballDunk\t   HammerThrow\t       PlayingDhol\t   SumoWrestling\r\n",
      "BenchPress\t   Hammering\t       PlayingFlute\t   Surfing\r\n",
      "Biking\t\t   HandstandPushups    PlayingGuitar\t   Swing\r\n",
      "Billiards\t   HandstandWalking    PlayingPiano\t   TableTennisShot\r\n",
      "BlowDryHair\t   HeadMassage\t       PlayingSitar\t   TaiChi\r\n",
      "BlowingCandles\t   HighJump\t       PlayingTabla\t   TennisSwing\r\n",
      "BodyWeightSquats   HorseRace\t       PlayingViolin\t   ThrowDiscus\r\n",
      "Bowling\t\t   HorseRiding\t       PoleVault\t   TrampolineJumping\r\n",
      "BoxingPunchingBag  HulaHoop\t       PommelHorse\t   Typing\r\n",
      "BoxingSpeedBag\t   IceDancing\t       PullUps\t\t   UnevenBars\r\n",
      "BreastStroke\t   JavelinThrow        Punch\t\t   VolleyballSpiking\r\n",
      "BrushingTeeth\t   JugglingBalls       PushUps\t\t   WalkingWithDog\r\n",
      "CleanAndJerk\t   JumpRope\t       Rafting\t\t   WallPushups\r\n",
      "CliffDiving\t   JumpingJack\t       RockClimbingIndoor  WritingOnBoard\r\n",
      "CricketBowling\t   Kayaking\t       RopeClimbing\t   YoYo\r\n",
      "CricketShot\t   Knitting\t       Rowing\r\n",
      "CuttingInKitchen   LongJump\t       SalsaSpin\r\n",
      "Diving\t\t   Lunges\t       ShavingBeard\r\n"
     ]
    }
   ],
   "source": [
    "! ls UCF-101"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "classes_list = sorted(os.listdir(\"UCF-101\"));\n",
    "\n",
    "f = open(\"Dataset/classes.txt\", 'w')\n",
    "for i in range(len(classes_list)):\n",
    "    f.write(classes_list[i] + \"\\n\");\n",
    "f.close();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 101/101 [00:50<00:00,  2.00it/s]\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from tqdm import tqdm\n",
    "\n",
    "complete_list = [];\n",
    "for i in tqdm(range(len(classes_list))):\n",
    "    folder_name = classes_list[i];\n",
    "    vid_label = str(classes_list.index(folder_name));\n",
    "    vid_list = os.listdir(\"UCF-101/\" + folder_name);\n",
    "    for j in range(len(vid_list)):\n",
    "        vid_name = vid_list[j];\n",
    "        wr = str(img_name) + \" \" + vid_label;\n",
    "        os.system(\"cp UCF-101/\" + folder_name + \"/\" + vid_name + \" Dataset/Videos/\")\n",
    "        complete_list.append(wr);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['v_ApplyEyeMakeup_g02_c01.avi 0',\n",
       "  'v_ApplyEyeMakeup_g21_c02.avi 0',\n",
       "  'v_ApplyEyeMakeup_g05_c01.avi 0',\n",
       "  'v_ApplyEyeMakeup_g10_c03.avi 0',\n",
       "  'v_ApplyEyeMakeup_g14_c04.avi 0'],\n",
       " 13320)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "complete_list[:5], len(complete_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "random.shuffle(complete_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['v_SoccerPenalty_g18_c01.avi 84',\n",
       "  'v_Basketball_g18_c05.avi 7',\n",
       "  'v_JumpRope_g11_c02.avi 46',\n",
       "  'v_Rowing_g07_c02.avi 75',\n",
       "  'v_Hammering_g10_c04.avi 35'],\n",
       " 13320)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "complete_list[:5], len(complete_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Splitting data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_list = complete_list[:10000];\n",
    "val_list = complete_list[10000:];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open(\"Dataset/train.txt\", 'w')\n",
    "for i in range(len(train_list)):\n",
    "    f.write(train_list[i] + \"\\n\");\n",
    "f.close();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open(\"Dataset/val.txt\", 'w')\n",
    "for i in range(len(val_list)):\n",
    "    f.write(val_list[i] + \"\\n\");\n",
    "f.close();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Train your own network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.append(\"Monk_Object_Detection/18_mmaction/lib\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from train_engine import Detector_Videos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtf = Detector_Videos();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "video_dir = 'Dataset/Videos/';\n",
    "anno_file = 'Dataset/train.txt';\n",
    "classes_list_file = 'Dataset/classes.txt';\n",
    "\n",
    "gtf.Train_Video_Dataset(video_dir, \n",
    "                          anno_file, \n",
    "                          classes_list_file);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "video_dir = 'Dataset/Videos/';\n",
    "anno_file = 'Dataset/val.txt';\n",
    "\n",
    "gtf.Val_Video_Dataset(video_dir, \n",
    "                          anno_file);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtf.Dataset_Params(batch_size=4, num_workers=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtf.List_Models();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtf.Model_Params(model_name=\"tsn_r50\", gpu_devices=[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtf.Hyper_Params(lr=0.002, momentum=0.9, weight_decay=0.0001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtf.Training_Params(num_epochs=100, val_interval=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtf.Train();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Inference on trained network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.append(\"Monk_Object_Detection/18_mmaction/lib\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from infer_engine import Infer_Videos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtf = Infer_Videos();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gtf.Dataset_Params('Dataset/classes.txt');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config_file = \"work_dirs/config.py\"\n",
    "checkpoint_file = \"work_dirs/latest.pth\"\n",
    "\n",
    "gtf.Model_Params(config_file, checkpoint_file, use_gpu=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "vid_list = os.listdir(\"Dataset/Videos\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "video_path = \"Dataset/Videos/\" + vid_list[0]\n",
    "results = gtf.Predict(video_path)\n",
    "from IPython.display import Video\n",
    "Video(video_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "video_path = \"Dataset/Videos/\" + vid_list[1]\n",
    "results = gtf.Predict(video_path)\n",
    "from IPython.display import Video\n",
    "Video(video_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
