|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stderr", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "Using TensorFlow backend.\n" |
| 13 | + ] |
| 14 | + } |
| 15 | + ], |
| 16 | + "source": [ |
| 17 | + "\"\"\"Some special pupropse layers for SSD.\"\"\"\n", |
| 18 | + "\n", |
| 19 | + "import keras.backend as K\n", |
| 20 | + "from keras.engine.topology import InputSpec\n", |
| 21 | + "from keras.engine.topology import Layer\n", |
| 22 | + "import numpy as np\n", |
| 23 | + "import tensorflow as tf" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + " Normalization layer as described in ParseNet paper.\n", |
| 31 | + "### Arguments\n", |
| 32 | + " scale: Default feature scale.\n", |
| 33 | + "### Input shape\n", |
| 34 | + " 4D tensor with shape:\n", |
| 35 | + " (samples, channels, rows, cols) if dim_ordering='th'\n", |
| 36 | + " or 4D tensor with shape:\n", |
| 37 | + " (samples, rows, cols, channels) if dim_ordering='tf'.\n", |
| 38 | + "### Output shape\n", |
| 39 | + " Same as input\n", |
| 40 | + "#### References\n", |
| 41 | + " http://cs.unc.edu/~wliu/papers/parsenet.pdf\n", |
| 42 | + "#### TODO\n", |
| 43 | + " Add possibility to have one scale for all features" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": 2, |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [], |
| 51 | + "source": [ |
| 52 | + "class Normalize(Layer):\n", |
| 53 | + " def __init__(self, scale, **kwargs):\n", |
| 54 | + " if K.image_dim_ordering() == 'tf':\n", |
| 55 | + " self.axis = 3\n", |
| 56 | + " else:\n", |
| 57 | + " self.axis = 1\n", |
| 58 | + " self.scale = scale\n", |
| 59 | + " super(Normalize, self).__init__(**kwargs)\n", |
| 60 | + "\n", |
| 61 | + " def build(self, input_shape):\n", |
| 62 | + " self.input_spec = [InputSpec(shape=input_shape)]\n", |
| 63 | + " shape = (input_shape[self.axis],)\n", |
| 64 | + " init_gamma = self.scale * np.ones(shape)\n", |
| 65 | + " self.gamma = K.variable(init_gamma, name='{}_gamma'.format(self.name))\n", |
| 66 | + " self.trainable_weights = [self.gamma]\n", |
| 67 | + "\n", |
| 68 | + " def call(self, x, mask=None):\n", |
| 69 | + " output = K.l2_normalize(x, self.axis)\n", |
| 70 | + " output *= self.gamma\n", |
| 71 | + " return output" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "markdown", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + " Generate the prior boxes of designated sizes and aspect ratios.\n", |
| 79 | + " ### Arguments\n", |
| 80 | + " img_size: Size of the input image as tuple (w, h).\n", |
| 81 | + " min_size: Minimum box size in pixels.\n", |
| 82 | + " max_size: Maximum box size in pixels.\n", |
| 83 | + " aspect_ratios: List of aspect ratios of boxes.\n", |
| 84 | + " flip: Whether to consider reverse aspect ratios.\n", |
| 85 | + " variances: List of variances for x, y, w, h.\n", |
| 86 | + " clip: Whether to clip the prior's coordinates\n", |
| 87 | + " such that they are within [0, 1].\n", |
| 88 | + "### Input shape\n", |
| 89 | + " 4D tensor with shape:\n", |
| 90 | + " (samples, channels, rows, cols) if dim_ordering='th'\n", |
| 91 | + " or 4D tensor with shape:\n", |
| 92 | + " (samples, rows, cols, channels) if dim_ordering='tf'\n", |
| 93 | + "### Output shape\n", |
| 94 | + " 3D tensor with shape:\n", |
| 95 | + " (samples, num_boxes, 8)\n", |
| 96 | + "### References\n", |
| 97 | + " https://arxiv.org/abs/1512.02325\n", |
| 98 | + "### TODO\n", |
| 99 | + " Add possibility not to have variances.\n", |
| 100 | + " Add Theano support" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": 3, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "class PriorBox(Layer):\n", |
| 110 | + " def __init__(self, img_size, min_size, max_size=None, aspect_ratios=None,\n", |
| 111 | + " flip=True, variances=[0.1], clip=True, **kwargs):\n", |
| 112 | + " if K.image_dim_ordering() == 'tf':\n", |
| 113 | + " self.waxis = 2\n", |
| 114 | + " self.haxis = 1\n", |
| 115 | + " else:\n", |
| 116 | + " self.waxis = 3\n", |
| 117 | + " self.haxis = 2\n", |
| 118 | + " self.img_size = img_size\n", |
| 119 | + " if min_size <= 0:\n", |
| 120 | + " raise Exception('min_size must be positive.')\n", |
| 121 | + " self.min_size = min_size\n", |
| 122 | + " self.max_size = max_size\n", |
| 123 | + " self.aspect_ratios = [1.0]\n", |
| 124 | + " if max_size:\n", |
| 125 | + " if max_size < min_size:\n", |
| 126 | + " raise Exception('max_size must be greater than min_size.')\n", |
| 127 | + " self.aspect_ratios.append(1.0)\n", |
| 128 | + " if aspect_ratios:\n", |
| 129 | + " for ar in aspect_ratios:\n", |
| 130 | + " if ar in self.aspect_ratios:\n", |
| 131 | + " continue\n", |
| 132 | + " self.aspect_ratios.append(ar)\n", |
| 133 | + " if flip:\n", |
| 134 | + " self.aspect_ratios.append(1.0 / ar)\n", |
| 135 | + " self.variances = np.array(variances)\n", |
| 136 | + " self.clip = True\n", |
| 137 | + " super(PriorBox, self).__init__(**kwargs)\n", |
| 138 | + "\n", |
| 139 | + " def get_output_shape_for(self, input_shape):\n", |
| 140 | + " num_priors_ = len(self.aspect_ratios)\n", |
| 141 | + " layer_width = input_shape[self.waxis]\n", |
| 142 | + " layer_height = input_shape[self.haxis]\n", |
| 143 | + " num_boxes = num_priors_ * layer_width * layer_height\n", |
| 144 | + " return input_shape[0], num_boxes, 8\n", |
| 145 | + "\n", |
| 146 | + " # support for Keras 2.0\n", |
| 147 | + " def compute_output_shape(self, input_shape):\n", |
| 148 | + " return self.get_output_shape_for(input_shape)\n", |
| 149 | + "\n", |
| 150 | + " def call(self, x, mask=None):\n", |
| 151 | + " if hasattr(x, '_keras_shape'):\n", |
| 152 | + " input_shape = x._keras_shape\n", |
| 153 | + " elif hasattr(K, 'int_shape'):\n", |
| 154 | + " input_shape = K.int_shape(x)\n", |
| 155 | + " layer_width = input_shape[self.waxis]\n", |
| 156 | + " layer_height = input_shape[self.haxis]\n", |
| 157 | + " img_width = self.img_size[0]\n", |
| 158 | + " img_height = self.img_size[1]\n", |
| 159 | + " # define prior boxes shapes\n", |
| 160 | + " box_widths = []\n", |
| 161 | + " box_heights = []\n", |
| 162 | + " for ar in self.aspect_ratios:\n", |
| 163 | + " if ar == 1 and len(box_widths) == 0:\n", |
| 164 | + " box_widths.append(self.min_size)\n", |
| 165 | + " box_heights.append(self.min_size)\n", |
| 166 | + " elif ar == 1 and len(box_widths) > 0:\n", |
| 167 | + " box_widths.append(np.sqrt(self.min_size * self.max_size))\n", |
| 168 | + " box_heights.append(np.sqrt(self.min_size * self.max_size))\n", |
| 169 | + " elif ar != 1:\n", |
| 170 | + " box_widths.append(self.min_size * np.sqrt(ar))\n", |
| 171 | + " box_heights.append(self.min_size / np.sqrt(ar))\n", |
| 172 | + " box_widths = 0.5 * np.array(box_widths)\n", |
| 173 | + " box_heights = 0.5 * np.array(box_heights)\n", |
| 174 | + " # define centers of prior boxes\n", |
| 175 | + " step_x = img_width / layer_width\n", |
| 176 | + " step_y = img_height / layer_height\n", |
| 177 | + " linx = np.linspace(0.5 * step_x, img_width - 0.5 * step_x,\n", |
| 178 | + " layer_width)\n", |
| 179 | + " liny = np.linspace(0.5 * step_y, img_height - 0.5 * step_y,\n", |
| 180 | + " layer_height)\n", |
| 181 | + " centers_x, centers_y = np.meshgrid(linx, liny)\n", |
| 182 | + " centers_x = centers_x.reshape(-1, 1)\n", |
| 183 | + " centers_y = centers_y.reshape(-1, 1)\n", |
| 184 | + " # define xmin, ymin, xmax, ymax of prior boxes\n", |
| 185 | + " num_priors_ = len(self.aspect_ratios)\n", |
| 186 | + " prior_boxes = np.concatenate((centers_x, centers_y), axis=1)\n", |
| 187 | + " prior_boxes = np.tile(prior_boxes, (1, 2 * num_priors_))\n", |
| 188 | + " prior_boxes[:, ::4] -= box_widths\n", |
| 189 | + " prior_boxes[:, 1::4] -= box_heights\n", |
| 190 | + " prior_boxes[:, 2::4] += box_widths\n", |
| 191 | + " prior_boxes[:, 3::4] += box_heights\n", |
| 192 | + " prior_boxes[:, ::2] /= img_width\n", |
| 193 | + " prior_boxes[:, 1::2] /= img_height\n", |
| 194 | + " prior_boxes = prior_boxes.reshape(-1, 4)\n", |
| 195 | + " if self.clip:\n", |
| 196 | + " prior_boxes = np.minimum(np.maximum(prior_boxes, 0.0), 1.0)\n", |
| 197 | + " # define variances\n", |
| 198 | + " num_boxes = len(prior_boxes)\n", |
| 199 | + " if len(self.variances) == 1:\n", |
| 200 | + " variances = np.ones((num_boxes, 4)) * self.variances[0]\n", |
| 201 | + " elif len(self.variances) == 4:\n", |
| 202 | + " variances = np.tile(self.variances, (num_boxes, 1))\n", |
| 203 | + " else:\n", |
| 204 | + " raise Exception('Must provide one or four variances.')\n", |
| 205 | + " prior_boxes = np.concatenate((prior_boxes, variances), axis=1)\n", |
| 206 | + " prior_boxes_tensor = K.expand_dims(K.variable(prior_boxes), 0)\n", |
| 207 | + " if K.backend() == 'tensorflow':\n", |
| 208 | + " pattern = [tf.shape(x)[0], 1, 1]\n", |
| 209 | + " prior_boxes_tensor = tf.tile(prior_boxes_tensor, pattern)\n", |
| 210 | + " elif K.backend() == 'theano':\n", |
| 211 | + " #TODO\n", |
| 212 | + " pass\n", |
| 213 | + " return prior_boxes_tensor" |
| 214 | + ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "code", |
| 218 | + "execution_count": null, |
| 219 | + "metadata": {}, |
| 220 | + "outputs": [], |
| 221 | + "source": [] |
| 222 | + } |
| 223 | + ], |
| 224 | + "metadata": { |
| 225 | + "anaconda-cloud": {}, |
| 226 | + "kernelspec": { |
| 227 | + "display_name": "Python [default]", |
| 228 | + "language": "python", |
| 229 | + "name": "python3" |
| 230 | + }, |
| 231 | + "language_info": { |
| 232 | + "codemirror_mode": { |
| 233 | + "name": "ipython", |
| 234 | + "version": 3 |
| 235 | + }, |
| 236 | + "file_extension": ".py", |
| 237 | + "mimetype": "text/x-python", |
| 238 | + "name": "python", |
| 239 | + "nbconvert_exporter": "python", |
| 240 | + "pygments_lexer": "ipython3", |
| 241 | + "version": "3.5.4" |
| 242 | + } |
| 243 | + }, |
| 244 | + "nbformat": 4, |
| 245 | + "nbformat_minor": 2 |
| 246 | +} |
0 commit comments