{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "from models.yolo import *\n",
    "PATH_WEIGHT = './models/best.pt'\n",
    "PATH = '/root/Public/pretrained/best.pt'\n",
    "\n",
    "net = Model('/root/helmet_det/yolov7-main/cfg/training/yolov7.yaml').to('cuda')\n",
    "state_dict = torch.load(PATH, map_location='cuda')['model'].state_dict()\n",
    "net.load_state_dict(state_dict, strict=False)\n",
    "\n",
    "a = torch.load(PATH_WEIGHT, map_location='cuda')['model']\n",
    "#print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[2, 0, 4],\n",
       "         [1, 3, 2],\n",
       "         [1, 2, 3]],\n",
       "\n",
       "        [[4, 4, 0],\n",
       "         [2, 2, 0],\n",
       "         [1, 2, 0]],\n",
       "\n",
       "        [[4, 1, 0],\n",
       "         [4, 0, 2],\n",
       "         [4, 4, 1]]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "from utils.torch_utils import select_device\n",
    "\n",
    "select_device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([3, 3, 3]) 4\n",
      "tensor([[[4, 1, 0],\n",
      "         [4, 0, 2],\n",
      "         [4, 4, 1]],\n",
      "\n",
      "        [[4, 4, 0],\n",
      "         [2, 2, 0],\n",
      "         [1, 2, 0]],\n",
      "\n",
      "        [[2, 0, 4],\n",
      "         [1, 3, 2],\n",
      "         [1, 2, 3]]])\n"
     ]
    }
   ],
   "source": [
    "a = a[[2, 1, 0], :, :]\n",
    "print(a.shape, 4)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>46</td>\n",
       "      <td>37</td>\n",
       "      <td>22</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>19</td>\n",
       "      <td>38</td>\n",
       "      <td>11</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>15</td>\n",
       "      <td>22</td>\n",
       "      <td>40</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5</td>\n",
       "      <td>31</td>\n",
       "      <td>34</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>46</td>\n",
       "      <td>47</td>\n",
       "      <td>2</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    a   b   c   d\n",
       "0  46  37  22   8\n",
       "1  19  38  11  10\n",
       "2  15  22  40  30\n",
       "3   5  31  34  45\n",
       "4  46  47   2  46"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "df = pd.DataFrame(np.random.randint(0, 50, size=(5000000, 4)), columns=('a','b','c','d'))\n",
    "df.shape\n",
    "# (5000000, 5)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "451.1593863964081\n"
     ]
    }
   ],
   "source": [
    "import time \n",
    "start = time.time() # iterrows for idx, row in df.iterrows() \n",
    "for idx, row in df.iterrows():\n",
    "    if row.a == 0 :         \n",
    "        df.at[idx, 'e' ] = row.d     \n",
    "    elif ( row.a <= 25 ) and (row.a > 0 ):         \n",
    "        df.at[idx, 'e' ] = (row.b)-(row.c)     \n",
    "    else :         \n",
    "        df.at[idx, 'e' ] = row.b + row.c \n",
    "end = time.time()\n",
    "print (end - start) ### 걸린 시간: 177초\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.16010785102844238\n"
     ]
    }
   ],
   "source": [
    "# using vectorization \n",
    "\n",
    "start = time.time()\n",
    "df['e'] = df['b'] + df['c']\n",
    "df.loc[df['a'] <= 25, 'e'] = df['b'] -df['c']\n",
    "df.loc[df['a']==0, 'e'] = df['d']\n",
    "end = time.time()\n",
    "print(end - start)\n",
    "## 0.28007707595825195 sec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "B\n",
      "C\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "def yield_abc():\n",
    "  for ch in \"ABC\":\n",
    "    time.sleep(1)\n",
    "    yield ch\n",
    "\n",
    "for ch in yield_abc():\n",
    "    print(ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False and device.type != 'cpu' \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False and True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.13 ('base')",
   "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.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:10) \n[GCC 10.3.0]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d4d1e4263499bec80672ea0156c357c1ee493ec2b1c70f0acce89fc37c4a6abe"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}