@@ -2,48 +2,198 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"name": "stdout",
"output_type": "stream",
"text": [
"C:\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
" from ._conv import register_converters as _register_converters\n",
"Using TensorFlow backend.\n"
]
},
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'yolo_utils'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-ac73fa7c69d1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlayers\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mInput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mLambda\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mConv2D\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mkeras\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmodels\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mload_model\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mModel\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0myolo_utils\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mread_classes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mread_anchors\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgenerate_colors\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpreprocess_image\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdraw_boxes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscale_boxes\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 15\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0myad2k\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmodels\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkeras_yolo\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0myolo_head\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0myolo_boxes_to_corners\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpreprocess_true_boxes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0myolo_loss\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0myolo_body\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'yolo_utils'"
"Imageio: 'ffmpeg-win32-v3.2.4.exe' was not found on your computer; downloading it now.\n",
"Try 1. Download from https://github.com/imageio/imageio-binaries/raw/master/ffmpeg/ffmpeg-win32-v3.2.4.exe (34.1 MB)\n",
"Downloading: 8192/35749888 bytes (0.057344/35749888 bytes (0.2%237568/35749888 bytes (0.7450560/35749888 bytes (1.3679936/35749888 bytes (1.9909312/35749888 bytes (2.51073152/35749888 bytes (3.0%1269760/35749888 bytes (3.6%1695744/35749888 bytes (4.7%1966080/35749888 bytes (5.5%2269184/35749888 bytes (6.3%2498560/35749888 bytes (7.0%2719744/35749888 bytes (7.6%2924544/35749888 bytes (8.2%3129344/35749888 bytes (8.8%3358720/35749888 bytes (9.4%3653632/35749888 bytes (10.23842048/35749888 bytes (10.74120576/35749888 bytes (11.54399104/35749888 bytes (12.34595712/35749888 bytes (12.94792320/35749888 bytes (13.45005312/35749888 bytes (14.05316608/35749888 bytes (14.95529600/35749888 bytes (15.55824512/35749888 bytes (16.36103040/35749888 bytes (17.16316032/35749888 bytes (17.76627328/35749888 bytes (18.56963200/35749888 bytes (19.57192576/35749888 bytes (20.17397376/35749888 bytes (20.77675904/35749888 bytes (21.57954432/35749888 bytes (22.38175616/35749888 bytes (22.98462336/35749888 bytes (23.78650752/35749888 bytes (24.28904704/35749888 bytes (24.99093120/35749888 bytes (25.49363456/35749888 bytes (26.29543680/35749888 bytes (26.79773056/35749888 bytes (27.39977856/35749888 bytes (27.910182656/35749888 bytes (28.5%10395648/35749888 bytes (29.1%10698752/35749888 bytes (29.9%10895360/35749888 bytes (30.5%11132928/35749888 bytes (31.1%11403264/35749888 bytes (31.9%11665408/35749888 bytes (32.6%11862016/35749888 bytes (33.2%12066816/35749888 bytes (33.8%12345344/35749888 bytes (34.5%12623872/35749888 bytes (35.3%12877824/35749888 bytes (36.0%13148160/35749888 bytes (36.8%13451264/35749888 bytes (37.6%13942784/35749888 bytes (39.0%14262272/35749888 bytes (39.9%14458880/35749888 bytes (40.4%14671872/35749888 bytes (41.0%14942208/35749888 bytes (41.8%15417344/35749888 bytes (43.1%15704064/35749888 bytes (43.9%15917056/35749888 bytes (44.5%16113664/35749888 bytes (45.1%16318464/35749888 bytes (45.6%16482304/35749888 bytes (46.1%16687104/35749888 bytes (46.7%16875520/35749888 bytes (47.2%17072128/35749888 bytes (47.8%17342464/35749888 bytes (48.5%17752064/35749888 bytes (49.7%17981440/35749888 bytes (50.3%18251776/35749888 bytes (51.1%18472960/35749888 bytes (51.7%18759680/35749888 bytes (52.5%18964480/35749888 bytes (53.0%19169280/35749888 bytes (53.6%19431424/35749888 bytes (54.4%19636224/35749888 bytes (54.9%19931136/35749888 bytes (55.8%20209664/35749888 bytes (56.5%20422656/35749888 bytes (57.1%20635648/35749888 bytes (57.7%21110784/35749888 bytes (59.1%21454848/35749888 bytes (60.0%21749760/35749888 bytes (60.8%21921792/35749888 bytes (61.3%22142976/35749888 bytes (61.9%22421504/35749888 bytes (62.7%22921216/35749888 bytes (64.1%23175168/35749888 bytes (64.8%23379968/35749888 bytes (65.4%23822336/35749888 bytes (66.6%24117248/35749888 bytes (67.5%24322048/35749888 bytes (68.0%24584192/35749888 bytes (68.8%24829952/35749888 bytes (69.5%25042944/35749888 bytes (70.1%25272320/35749888 bytes (70.7%25501696/35749888 bytes (71.3%25747456/35749888 bytes (72.0%25993216/35749888 bytes (72.7%26157056/35749888 bytes (73.2%26435584/35749888 bytes (73.9%26615808/35749888 bytes (74.5%26959872/35749888 bytes (75.4%27197440/35749888 bytes (76.1%27484160/35749888 bytes (76.9%27746304/35749888 bytes (77.6%28090368/35749888 bytes (78.6%28360704/35749888 bytes (79.3%28729344/35749888 bytes (80.4%29048832/35749888 bytes (81.3%29212672/35749888 bytes (81.7%29507584/35749888 bytes (82.5%29851648/35749888 bytes (83.5%30162944/35749888 bytes (84.4%30457856/35749888 bytes (85.2%30793728/35749888 bytes (86.1%31039488/35749888 bytes (86.8%31383552/35749888 bytes (87.8%31801344/35749888 bytes (89.0%31981568/35749888 bytes (89.5%32243712/35749888 bytes (90.2%32612352/35749888 bytes (91.2%32882688/35749888 bytes (92.0%33341440/35749888 bytes (93.3%33800192/35749888 bytes (94.5%34119680/35749888 bytes (95.4%34439168/35749888 bytes (96.3%34652160/35749888 bytes (96.9%34848768/35749888 bytes (97.5%35135488/35749888 bytes (98.3%35463168/35749888 bytes (99.2%35749888/35749888 bytes (100.0%)\n",
" Done\n",
"File saved as C:\\Users\\Kaustubh Chaudhuri\\AppData\\Local\\imageio\\ffmpeg\\ffmpeg-win32-v3.2.4.exe.\n"
]
}
],
"source": [
"import argparse\n",
"import os\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib.pyplot import imshow\n",
"import scipy.io\n",
"import scipy.misc\n",
"import numpy as np\n",
"import pandas as pd\n",
"import PIL\n",
"import tensorflow as tf\n",
"from keras import backend as K\n",
"from keras.layers import Input, Lambda, Conv2D\n",
"from keras.models import load_model, Model\n",
"from yolo_utils import read_classes, read_anchors, generate_colors, preprocess_image, draw_boxes, scale_boxes\n",
"from yad2k.models.keras_yolo import yolo_head, yolo_boxes_to_corners, preprocess_true_boxes, yolo_loss, yolo_body\n",
"import matplotlib.pyplot as plt\n",
"import cv2\n",
"import glob\n",
"from moviepy.editor import VideoFileClip\n",
"from IPython.display import HTML\n",
"%matplotlib inline\n",
"\n",
"%matplotlib inline"
"import keras # broken for keras >= 2.0, use 1.2.2\n",
"from keras.models import Sequential\n",
"from keras.layers.convolutional import Convolution2D, MaxPooling2D\n",
"from keras.layers.advanced_activations import LeakyReLU\n",
"from keras.layers.core import Flatten, Dense, Activation, Reshape\n",
"\n",
"from utils import load_weights, Box, yolo_net_out_to_car_boxes, draw_box"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"keras.backend.set_image_dim_ordering('th')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"model = Sequential()\n",
"model.add(Convolution2D(16, 3, 3,input_shape=(3,448,448),border_mode='same',subsample=(1,1)))\n",
"model.add(LeakyReLU(alpha=0.1))\n",
"model.add(MaxPooling2D(pool_size=(2, 2)))\n",
"model.add(Convolution2D(32,3,3 ,border_mode='same'))\n",
"model.add(LeakyReLU(alpha=0.1))\n",
"model.add(MaxPooling2D(pool_size=(2, 2),border_mode='valid'))\n",
"model.add(Convolution2D(64,3,3 ,border_mode='same'))\n",
"model.add(LeakyReLU(alpha=0.1))\n",
"model.add(MaxPooling2D(pool_size=(2, 2),border_mode='valid'))\n",
"model.add(Convolution2D(128,3,3 ,border_mode='same'))\n",
"model.add(LeakyReLU(alpha=0.1))\n",
"model.add(MaxPooling2D(pool_size=(2, 2),border_mode='valid'))\n",
"model.add(Convolution2D(256,3,3 ,border_mode='same'))\n",
"model.add(LeakyReLU(alpha=0.1))\n",
"model.add(MaxPooling2D(pool_size=(2, 2),border_mode='valid'))\n",
"model.add(Convolution2D(512,3,3 ,border_mode='same'))\n",
"model.add(LeakyReLU(alpha=0.1))\n",
"model.add(MaxPooling2D(pool_size=(2, 2),border_mode='valid'))\n",
"model.add(Convolution2D(1024,3,3 ,border_mode='same'))\n",
"model.add(LeakyReLU(alpha=0.1))\n",
"model.add(Convolution2D(1024,3,3 ,border_mode='same'))\n",
"model.add(LeakyReLU(alpha=0.1))\n",
"model.add(Convolution2D(1024,3,3 ,border_mode='same'))\n",
"model.add(LeakyReLU(alpha=0.1))\n",
"model.add(Flatten())\n",
"model.add(Dense(256))\n",
"model.add(Dense(4096))\n",
"model.add(LeakyReLU(alpha=0.1))\n",
"model.add(Dense(1470))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv2d_1 (Conv2D) (None, 16, 448, 448) 448 \n",
"_________________________________________________________________\n",
"leaky_re_lu_1 (LeakyReLU) (None, 16, 448, 448) 0 \n",
"_________________________________________________________________\n",
"max_pooling2d_1 (MaxPooling2 (None, 16, 224, 224) 0 \n",
"_________________________________________________________________\n",
"conv2d_2 (Conv2D) (None, 32, 224, 224) 4640 \n",
"_________________________________________________________________\n",
"leaky_re_lu_2 (LeakyReLU) (None, 32, 224, 224) 0 \n",
"_________________________________________________________________\n",
"max_pooling2d_2 (MaxPooling2 (None, 32, 112, 112) 0 \n",
"_________________________________________________________________\n",
"conv2d_3 (Conv2D) (None, 64, 112, 112) 18496 \n",
"_________________________________________________________________\n",
"leaky_re_lu_3 (LeakyReLU) (None, 64, 112, 112) 0 \n",
"_________________________________________________________________\n",
"max_pooling2d_3 (MaxPooling2 (None, 64, 56, 56) 0 \n",
"_________________________________________________________________\n",
"conv2d_4 (Conv2D) (None, 128, 56, 56) 73856 \n",
"_________________________________________________________________\n",
"leaky_re_lu_4 (LeakyReLU) (None, 128, 56, 56) 0 \n",
"_________________________________________________________________\n",
"max_pooling2d_4 (MaxPooling2 (None, 128, 28, 28) 0 \n",
"_________________________________________________________________\n",
"conv2d_5 (Conv2D) (None, 256, 28, 28) 295168 \n",
"_________________________________________________________________\n",
"leaky_re_lu_5 (LeakyReLU) (None, 256, 28, 28) 0 \n",
"_________________________________________________________________\n",
"max_pooling2d_5 (MaxPooling2 (None, 256, 14, 14) 0 \n",
"_________________________________________________________________\n",
"conv2d_6 (Conv2D) (None, 512, 14, 14) 1180160 \n",
"_________________________________________________________________\n",
"leaky_re_lu_6 (LeakyReLU) (None, 512, 14, 14) 0 \n",
"_________________________________________________________________\n",
"max_pooling2d_6 (MaxPooling2 (None, 512, 7, 7) 0 \n",
"_________________________________________________________________\n",
"conv2d_7 (Conv2D) (None, 1024, 7, 7) 4719616 \n",
"_________________________________________________________________\n",
"leaky_re_lu_7 (LeakyReLU) (None, 1024, 7, 7) 0 \n",
"_________________________________________________________________\n",
"conv2d_8 (Conv2D) (None, 1024, 7, 7) 9438208 \n",
"_________________________________________________________________\n",
"leaky_re_lu_8 (LeakyReLU) (None, 1024, 7, 7) 0 \n",
"_________________________________________________________________\n",
"conv2d_9 (Conv2D) (None, 1024, 7, 7) 9438208 \n",
"_________________________________________________________________\n",
"leaky_re_lu_9 (LeakyReLU) (None, 1024, 7, 7) 0 \n",
"_________________________________________________________________\n",
"flatten_1 (Flatten) (None, 50176) 0 \n",
"_________________________________________________________________\n",
"dense_1 (Dense) (None, 256) 12845312 \n",
"_________________________________________________________________\n",
"dense_2 (Dense) (None, 4096) 1052672 \n",
"_________________________________________________________________\n",
"leaky_re_lu_10 (LeakyReLU) (None, 4096) 0 \n",
"_________________________________________________________________\n",
"dense_3 (Dense) (None, 1470) 6022590 \n",
"=================================================================\n",
"Total params: 45,089,374\n",
"Trainable params: 45,089,374\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"load_weights(model,'./yolo-tiny.weights')"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"imagePath = './test_images/test1.jpg'\n",
"image = plt.imread(imagePath)\n",
"image_crop = image[300:650,500:,:]\n",
"resized = cv2.resize(image_crop,(448,448))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"batch = np.transpose(resized,(2,0,1))\n",
"batch = 2*(batch/255.) - 1\n",
"batch = np.expand_dims(batch, axis=0)\n",
"out = model.predict(batch)"
]
},
{