Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"%matplotlib inline\n",
"%load_ext autoreload\n",
"%autoreload 2\n",
"\n",
"from ipywidgets import IntProgress\n",
"from keras_tqdm import TQDMNotebookCallback\n",
"\n",
"import keras\n",
"import keras.preprocessing.image\n",
"from keras.applications.imagenet_utils import get_file\n",
"\n",
"import keras_resnet.models\n",
"import keras_retinanet.layers\n",
"import keras_retinanet.models\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import cv2\n",
"import numpy as np\n",
"import sys\n",
"import math\n",
"\n",
"import tensorflow as tf\n",
"\n",
"def get_session():\n",
" config = tf.ConfigProto()\n",
" config.gpu_options.allow_growth = True\n",
" return tf.Session(config=config)\n",
"\n",
"keras.backend.tensorflow_backend.set_session(get_session())\n",
"\n",
"WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/lib/python3.6/site-packages/ipykernel/__main__.py:15: UserWarning: Output \"classification_softmax\" missing from loss dictionary. We assume this was done on purpose, and we will not be expecting any data to be passed to \"classification_softmax\" during training.\n",
"/usr/lib/python3.6/site-packages/ipykernel/__main__.py:15: UserWarning: Output \"concatenate_8\" missing from loss dictionary. We assume this was done on purpose, and we will not be expecting any data to be passed to \"concatenate_8\" during training.\n",
"/usr/lib/python3.6/site-packages/ipykernel/__main__.py:15: UserWarning: Output \"focal_loss_1\" missing from loss dictionary. We assume this was done on purpose, and we will not be expecting any data to be passed to \"focal_loss_1\" during training.\n",
"/usr/lib/python3.6/site-packages/ipykernel/__main__.py:15: UserWarning: Output \"concatenate_7\" missing from loss dictionary. We assume this was done on purpose, and we will not be expecting any data to be passed to \"concatenate_7\" during training.\n"
]
}
],
"source": [
"def create_model(num_classes=2, *args, **kwargs):\n",
" image = keras.layers.Input((512, 512, 3))\n",
" im_info = keras.layers.Input((3,))\n",
" gt_boxes = keras.layers.Input((None, 5))\n",
" \n",
" return keras_retinanet.models.ResNet50RetinaNet([image, im_info, gt_boxes], num_classes=num_classes)\n",
"\n",
"model = create_model()\n",
"\n",
"# load imagenet weights\n",
"weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af')\n",
"model.load_weights(weights_path, by_name=True)\n",
"\n",
"# compile model\n",
"model.compile(loss=None, optimizer=keras.optimizers.sgd(lr=0.01, momentum=0.9, decay=0.0001))\n",
"#print(model.summary())"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAQYAAAD8CAYAAACVSwr3AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADU1JREFUeJzt3GuopdV9x/HvL46jaU0d7wwz047ivNAXrZHBTDAUa5Ki\nNmR8oWAIOMjAQC9gsJCOLbQE+qL2RRRpMB2qdCxJ1ObCDJLUymho33ibeM/UeCxWDzM4iJekCG2N\n/77Y6yTbWUfPdubsyzHfD2z2WutZ++z/uezfWc+zn2enqpCkYR+ZdgGSZo/BIKljMEjqGAySOgaD\npI7BIKkzlmBIclmS55LMJdk5jueQND5Z7vMYkhwH/AT4LDAPPAp8oap+vKxPJGlsxrFiuAiYq6r/\nrKr/Be4Cto7heSSNyaoxfM11wMtD/XngE+/3gCSefimN36tVdcYoE8cRDFlkrHvhJ9kB7BjD80ta\n3H+NOnEcwTAPbBjqrwcOHjmpqnYBu8AVgzRrxnGM4VFgU5Kzk6wGrgH2juF5JI3Jsq8YqurtJH8C\n3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n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7f530a2d5cf8>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 512, 512, 3) (1, 3) (1, 1, 5)\n"
]
}
],
"source": [
"# generate really simple image with one object\n",
"image = np.zeros((512, 512, 3), dtype=keras.backend.floatx())\n",
"image[100:300, 100:300, :] = 1.0\n",
"plt.imshow(image)\n",
"plt.show()\n",
"\n",
"# create input batch blobs\n",
"image_batch = np.expand_dims(image, axis=0)\n",
"im_info_batch = np.array([[512, 512, 1.0]])\n",
"gt_boxes_batch = np.array([[[100, 100, 300, 300, 1]]])\n",
"\n",
"inputs = [image_batch, im_info_batch, gt_boxes_batch]\n",
"\n",
"def simple_data_generator():\n",
" while True:\n",
" yield inputs, None\n",
"\n",
"print(image_batch.shape, im_info_batch.shape, gt_boxes_batch.shape)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/15\n",
"100/100 [==============================] - 16s - loss: 0.4958 \n",
"Epoch 2/15\n",
"100/100 [==============================] - 10s - loss: 6.1573e-05 \n",
"Epoch 3/15\n",
"100/100 [==============================] - 10s - loss: 3.1098e-05 \n",
"Epoch 4/15\n",
"100/100 [==============================] - 10s - loss: 2.0863e-05 \n",
"Epoch 5/15\n",
"100/100 [==============================] - 10s - loss: 1.5440e-05 \n",
"Epoch 6/15\n",
"100/100 [==============================] - 10s - loss: 1.2124e-05 \n",
"Epoch 7/15\n",
"100/100 [==============================] - 10s - loss: 9.8986e-06 \n",
"Epoch 8/15\n",
"100/100 [==============================] - 10s - loss: 8.3127e-06 \n",
"Epoch 9/15\n",
"100/100 [==============================] - 10s - loss: 7.1460e-06 \n",
"Epoch 10/15\n",
"100/100 [==============================] - 10s - loss: 6.2527e-06 \n",
"Epoch 11/15\n",
"100/100 [==============================] - 10s - loss: 5.5488e-06 \n",
"Epoch 12/15\n",
"100/100 [==============================] - 10s - loss: 4.9827e-06 \n",
"Epoch 13/15\n",
"100/100 [==============================] - 10s - loss: 4.5180e-06 \n",
"Epoch 14/15\n",
"100/100 [==============================] - 10s - loss: 4.1302e-06 \n",
"Epoch 15/15\n",
"100/100 [==============================] - 10s - loss: 3.8022e-06 \n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7f535b465a90>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# train for some iterations\n",
"model.fit_generator(\n",
" generator=simple_data_generator(),\n",
" steps_per_epoch=100,\n",
" epochs=15,\n",
" verbose=1\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# predict the same image again\n",
"classification, labels, cls_loss, anchors = model.predict_on_batch(inputs)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 12240) (1, 12240, 2) (1, 12240, 4)\n",
"1.0 [ 0.00666258 0.99333739]\n",
"1.0 [ 3.34702025e-04 9.99665260e-01]\n",
"1.0 [ 0.0110987 0.98890126]\n",
"1.0 [ 0.00323502 0.99676502]\n",
"1.0 [ 4.17040610e-05 9.99958277e-01]\n",
"1.0 [ 8.02129125e-06 9.99992013e-01]\n",
"1.0 [ 3.94867238e-04 9.99605119e-01]\n",
"1.0 [ 1.02005768e-04 9.99897957e-01]\n",
"1.0 [ 3.95044044e-04 9.99605000e-01]\n",
"1.0 [ 0.01033688 0.98966306]\n",
"1.0 [ 0.0094928 0.99050713]\n",
"1.0 [ 9.11216432e-07 9.99999046e-01]\n",
"1.0 [ 2.16826210e-11 1.00000000e+00]\n",
"1.0 [ 7.33204400e-11 1.00000000e+00]\n",
"1.0 [ 2.50683530e-16 1.00000000e+00]\n",
"1.0 [ 8.84166695e-08 9.99999881e-01]\n",
"1.0 [ 1.91244744e-08 1.00000000e+00]\n",
"1.0 [ 3.98018580e-15 1.00000000e+00]\n",
"1.0 [ 1.07011005e-13 1.00000000e+00]\n",
"1.0 [ 4.25768623e-24 1.00000000e+00]\n",
"1.0 [ 5.34723266e-10 1.00000000e+00]\n",
"1.0 [ 7.58171720e-11 1.00000000e+00]\n",
"1.0 [ 1.73536567e-11 1.00000000e+00]\n",
"1.0 [ 3.06059519e-04 9.99693990e-01]\n",
"1.0 [ 0.00720659 0.99279344]\n",
"1.0 [ 2.09298378e-06 9.99997854e-01]\n",
"1.0 [ 0.00124534 0.99875462]\n",
"1.0 [ 2.72659673e-10 1.00000000e+00]\n",
"1.0 [ 7.90482986e-08 9.99999881e-01]\n",
"1.0 [ 1.61399162e-22 1.00000000e+00]\n",
"1.0 [ 1.61693231e-06 9.99998331e-01]\n",
"1.0 [ 1.30369202e-10 1.00000000e+00]\n",
"1.0 [ 3.09675452e-05 9.99969006e-01]\n",
"1.0 [ 7.38852389e-29 1.00000000e+00]\n",
"1.0 [ 1.19105447e-04 9.99880910e-01]\n",
"1.0 [ 3.54203564e-08 1.00000000e+00]\n",
"1.0 [ 0.01634809 0.98365188]\n",
"1.0 [ 0.02008545 0.97991461]\n",
"1.0 [ 0.0170248 0.98297518]\n",
"1.0 [ 0.02075755 0.97924244]\n",
"1.0 [ 0.02280544 0.97719461]\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAksAAAJCCAYAAADQsoPKAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHtJJREFUeJzt3V+sdWldH/Dvzxn+GLUOoBAyMy00zAVeVKQTOgm9ULQG\n0DhcQIqxMiGTvDc2wWij6I1pUxO9EUNqSCZCHIyKBKVMjGmdDBh7A2VGKKCj5dVQ5s0QJhYYtSSa\n0acXZ71w5p1zfnvvs/+tvc/nk7w5e6+99tq//T57r/M9z3rWs2qMEQAAzvYN+y4AAGDOhCUAgIaw\nBADQEJYAABrCEgBAQ1gCAGhsJSxV1Wur6s+r6mpV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dV9YNJ\nnhhjPFJV33198Rmraqd5ePUY4/GqemGSB6vqz5p1N9ZW++5Zupbk9lP3b0vy+J5q4WxfvN5tOf18\nYlqu7faoqp6Vk6D0G2OM350Wa6uZGmN8Jckf5mSM2S1Vdf0P1dNt8bV2mh7/1jzzsDib9+okP1RV\nn8vJUJDX5KSnSTvN0Bjj8ennEzn5A+RV2cG+b99h6eNJ7pjOOnh2kjcneWDPNfF0DyS5Z7p9T5IP\nnVr+lulsg7uSPHm9G5TtmsZHvDvJo2OMXzr1kLaakar69qlHKVX1jUm+Lyfjyz6S5I3Taje20/X2\ne2OSDw8T4W3dGONnxhi3jTFekpPfQR8eY/xItNPsVNU3VdW3XL+d5PuTfCY72PftfVLKqnp9TlL8\nTUneM8b4+b0WdIlV1W8l+e6cXLn5i0l+Lsl/TfL+JP80yeeTvGmM8aXpF/Z/ycnZc19N8tYxxsP7\nqPuyqap/neR/JPl0vj7G4mdzMm5JW81EVf2LnAw2vSknf5i+f4zxn6rqn+ekB+P5ST6R5N+NMf6u\nqp6b5NdzMgbtS0nePMb4y/1UfzlNh+H+wxjjB7XT/Ext8sHp7s1JfnOM8fNV9YJsed+397AEADBn\n+z4MBwAwa8ISAEBDWAIAaAhLAAANYQkAoCEsAQA0hCUAgIawBADQ+P9wTnMqFaZ6CwAAAABJRU5E\nrkJggg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x7f52220b28d0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# visualize the positive anchors\n",
"indices = np.where(labels[0, :] > 0)[0]\n",
"im = image_batch[0, ...].copy()\n",
"print(labels.shape, classification.shape, anchors.shape)\n",
"for idx in indices:\n",
" # print the label and the classification scores\n",
" print(labels[0, idx], classification[0, idx, :])\n",
" b = anchors[0, idx, :].astype(int)\n",
" cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 1, 0), 3)\n",
" \n",
"plt.figure(figsize=(10, 10))\n",
"plt.imshow(im)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generate anchors for a certain image size"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import keras_retinanet.layers\n",
"\n",
"im_info = keras.layers.Input((3,))\n",
"gt_boxes = keras.layers.Input((None, 5))\n",
"\n",
"shapes = [(64, 64), (32, 32), (16, 16), (8, 8), (4, 4)]\n",
"strides = [8, 16, 32, 64, 128]\n",
"sizes = [32, 64, 128, 256, 512]\n",
"idx = 2\n",
"\n",
"labels, bbox_reg_targets, anchors = keras_retinanet.layers.AnchorTarget(shapes[idx], strides[idx], sizes[idx])([im_info, gt_boxes])\n",
"anchor_model = keras.models.Model(inputs=[im_info, gt_boxes], outputs=[labels, anchors])\n",
"\n",
"l, a = anchor_model.predict([im_info_batch, gt_boxes_batch])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# show labeled anchors\n",
"indices = np.where(l[0, :] > 0)[0]\n",
"im = image_batch[0, ...].copy()\n",
"for idx in indices:\n",
" b = a[0, idx, :].astype(int)\n",
" cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 1, 0), 3)\n",
" \n",
"plt.figure(figsize=(10, 10))\n",
"plt.imshow(im)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment