├── .gitignore ├── README.md ├── VGG_ILSVRC_16_layers_deploy.prototxt ├── tf2caffe.py ├── tf2caffe_faster_rcnn.py └── vgg16Faster-rcnn.prototxt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tensorflow2caffemodel 2 | ## tensorflow 2 caffe model 3 | ## VGG16: 4 | ``` 5 | $ python tf2caffe.py 6 | ``` 7 | 8 | ## ONE STEP: 9 | ### Install caffe and tensroflow latest version 10 | 11 | 12 | ## SECOND STEP: 13 | ### Download vgg16 prototxt and tensorflow model vgg16.ckpt 14 | ### Changed the path of 'checkpoint_path', 'cf_prototxt' 15 | # vgg16 ckpt url:https://github.com/tensorflow/models/tree/master/slim 16 | 17 | ## VGG16 faster - rcnn: 18 | ``` 19 | $ python tf2caffe_faster_rcnn.py 20 | ``` 21 | # You can train vgg16 faster-rcnn https://github.com/smallcorgi/Faster-RCNN_TF 22 | -------------------------------------------------------------------------------- /VGG_ILSVRC_16_layers_deploy.prototxt: -------------------------------------------------------------------------------- 1 | name: "VGG_ILSVRC_16_layers" 2 | input: "data" 3 | input_dim: 10 4 | input_dim: 3 5 | input_dim: 224 6 | input_dim: 224 7 | layers { 8 | bottom: "data" 9 | top: "conv1_1" 10 | name: "conv1_1" 11 | type: CONVOLUTION 12 | convolution_param { 13 | num_output: 64 14 | pad: 1 15 | kernel_size: 3 16 | } 17 | } 18 | layers { 19 | bottom: "conv1_1" 20 | top: "conv1_1" 21 | name: "relu1_1" 22 | type: RELU 23 | } 24 | layers { 25 | bottom: "conv1_1" 26 | top: "conv1_2" 27 | name: "conv1_2" 28 | type: CONVOLUTION 29 | convolution_param { 30 | num_output: 64 31 | pad: 1 32 | kernel_size: 3 33 | } 34 | } 35 | layers { 36 | bottom: "conv1_2" 37 | top: "conv1_2" 38 | name: "relu1_2" 39 | type: RELU 40 | } 41 | layers { 42 | bottom: "conv1_2" 43 | top: "pool1" 44 | name: "pool1" 45 | type: POOLING 46 | pooling_param { 47 | pool: MAX 48 | kernel_size: 2 49 | stride: 2 50 | } 51 | } 52 | layers { 53 | bottom: "pool1" 54 | top: "conv2_1" 55 | name: "conv2_1" 56 | type: CONVOLUTION 57 | convolution_param { 58 | num_output: 128 59 | pad: 1 60 | kernel_size: 3 61 | } 62 | } 63 | layers { 64 | bottom: "conv2_1" 65 | top: "conv2_1" 66 | name: "relu2_1" 67 | type: RELU 68 | } 69 | layers { 70 | bottom: "conv2_1" 71 | top: "conv2_2" 72 | name: "conv2_2" 73 | type: CONVOLUTION 74 | convolution_param { 75 | num_output: 128 76 | pad: 1 77 | kernel_size: 3 78 | } 79 | } 80 | layers { 81 | bottom: "conv2_2" 82 | top: "conv2_2" 83 | name: "relu2_2" 84 | type: RELU 85 | } 86 | layers { 87 | bottom: "conv2_2" 88 | top: "pool2" 89 | name: "pool2" 90 | type: POOLING 91 | pooling_param { 92 | pool: MAX 93 | kernel_size: 2 94 | stride: 2 95 | } 96 | } 97 | layers { 98 | bottom: "pool2" 99 | top: "conv3_1" 100 | name: "conv3_1" 101 | type: CONVOLUTION 102 | convolution_param { 103 | num_output: 256 104 | pad: 1 105 | kernel_size: 3 106 | } 107 | } 108 | layers { 109 | bottom: "conv3_1" 110 | top: "conv3_1" 111 | name: "relu3_1" 112 | type: RELU 113 | } 114 | layers { 115 | bottom: "conv3_1" 116 | top: "conv3_2" 117 | name: "conv3_2" 118 | type: CONVOLUTION 119 | convolution_param { 120 | num_output: 256 121 | pad: 1 122 | kernel_size: 3 123 | } 124 | } 125 | layers { 126 | bottom: "conv3_2" 127 | top: "conv3_2" 128 | name: "relu3_2" 129 | type: RELU 130 | } 131 | layers { 132 | bottom: "conv3_2" 133 | top: "conv3_3" 134 | name: "conv3_3" 135 | type: CONVOLUTION 136 | convolution_param { 137 | num_output: 256 138 | pad: 1 139 | kernel_size: 3 140 | } 141 | } 142 | layers { 143 | bottom: "conv3_3" 144 | top: "conv3_3" 145 | name: "relu3_3" 146 | type: RELU 147 | } 148 | layers { 149 | bottom: "conv3_3" 150 | top: "pool3" 151 | name: "pool3" 152 | type: POOLING 153 | pooling_param { 154 | pool: MAX 155 | kernel_size: 2 156 | stride: 2 157 | } 158 | } 159 | layers { 160 | bottom: "pool3" 161 | top: "conv4_1" 162 | name: "conv4_1" 163 | type: CONVOLUTION 164 | convolution_param { 165 | num_output: 512 166 | pad: 1 167 | kernel_size: 3 168 | } 169 | } 170 | layers { 171 | bottom: "conv4_1" 172 | top: "conv4_1" 173 | name: "relu4_1" 174 | type: RELU 175 | } 176 | layers { 177 | bottom: "conv4_1" 178 | top: "conv4_2" 179 | name: "conv4_2" 180 | type: CONVOLUTION 181 | convolution_param { 182 | num_output: 512 183 | pad: 1 184 | kernel_size: 3 185 | } 186 | } 187 | layers { 188 | bottom: "conv4_2" 189 | top: "conv4_2" 190 | name: "relu4_2" 191 | type: RELU 192 | } 193 | layers { 194 | bottom: "conv4_2" 195 | top: "conv4_3" 196 | name: "conv4_3" 197 | type: CONVOLUTION 198 | convolution_param { 199 | num_output: 512 200 | pad: 1 201 | kernel_size: 3 202 | } 203 | } 204 | layers { 205 | bottom: "conv4_3" 206 | top: "conv4_3" 207 | name: "relu4_3" 208 | type: RELU 209 | } 210 | layers { 211 | bottom: "conv4_3" 212 | top: "pool4" 213 | name: "pool4" 214 | type: POOLING 215 | pooling_param { 216 | pool: MAX 217 | kernel_size: 2 218 | stride: 2 219 | } 220 | } 221 | layers { 222 | bottom: "pool4" 223 | top: "conv5_1" 224 | name: "conv5_1" 225 | type: CONVOLUTION 226 | convolution_param { 227 | num_output: 512 228 | pad: 1 229 | kernel_size: 3 230 | } 231 | } 232 | layers { 233 | bottom: "conv5_1" 234 | top: "conv5_1" 235 | name: "relu5_1" 236 | type: RELU 237 | } 238 | layers { 239 | bottom: "conv5_1" 240 | top: "conv5_2" 241 | name: "conv5_2" 242 | type: CONVOLUTION 243 | convolution_param { 244 | num_output: 512 245 | pad: 1 246 | kernel_size: 3 247 | } 248 | } 249 | layers { 250 | bottom: "conv5_2" 251 | top: "conv5_2" 252 | name: "relu5_2" 253 | type: RELU 254 | } 255 | layers { 256 | bottom: "conv5_2" 257 | top: "conv5_3" 258 | name: "conv5_3" 259 | type: CONVOLUTION 260 | convolution_param { 261 | num_output: 512 262 | pad: 1 263 | kernel_size: 3 264 | } 265 | } 266 | layers { 267 | bottom: "conv5_3" 268 | top: "conv5_3" 269 | name: "relu5_3" 270 | type: RELU 271 | } 272 | layers { 273 | bottom: "conv5_3" 274 | top: "pool5" 275 | name: "pool5" 276 | type: POOLING 277 | pooling_param { 278 | pool: MAX 279 | kernel_size: 2 280 | stride: 2 281 | } 282 | } 283 | layers { 284 | bottom: "pool5" 285 | top: "fc6" 286 | name: "fc6" 287 | type: INNER_PRODUCT 288 | inner_product_param { 289 | num_output: 4096 290 | } 291 | } 292 | layers { 293 | bottom: "fc6" 294 | top: "fc6" 295 | name: "relu6" 296 | type: RELU 297 | } 298 | layers { 299 | bottom: "fc6" 300 | top: "fc6" 301 | name: "drop6" 302 | type: DROPOUT 303 | dropout_param { 304 | dropout_ratio: 0.5 305 | } 306 | } 307 | layers { 308 | bottom: "fc6" 309 | top: "fc7" 310 | name: "fc7" 311 | type: INNER_PRODUCT 312 | inner_product_param { 313 | num_output: 4096 314 | } 315 | } 316 | layers { 317 | bottom: "fc7" 318 | top: "fc7" 319 | name: "relu7" 320 | type: RELU 321 | } 322 | layers { 323 | bottom: "fc7" 324 | top: "fc7" 325 | name: "drop7" 326 | type: DROPOUT 327 | dropout_param { 328 | dropout_ratio: 0.5 329 | } 330 | } 331 | layers { 332 | bottom: "fc7" 333 | top: "fc8" 334 | name: "fc8" 335 | type: INNER_PRODUCT 336 | inner_product_param { 337 | num_output: 1000 338 | } 339 | } 340 | layers { 341 | bottom: "fc8" 342 | top: "prob" 343 | name: "prob" 344 | type: SOFTMAX 345 | } 346 | -------------------------------------------------------------------------------- /tf2caffe.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import tensorflow as tf 3 | import caffe 4 | import numpy as np 5 | import cv2 6 | sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) 7 | 8 | from tensorflow.python import pywrap_tensorflow 9 | checkpoint_path = "./vgg_16.ckpt" 10 | reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path) 11 | var_to_shape_map = reader.get_variable_to_shape_map() 12 | 13 | 14 | cf_prototxt = "./VGG_ILSVRC_16_layers_deploy.prototxt" 15 | cf_model = "./vgg16.caffemodel" 16 | 17 | 18 | def tensor4d_transform(tensor): 19 | return tensor.transpose((3, 2, 0, 1)) 20 | def tensor2d_transform(tensor): 21 | return tensor.transpose((1, 0)) 22 | 23 | def tf2caffe(checkpoint_path,cf_prototxt,cf_model): 24 | net = caffe.Net(cf_prototxt, caffe.TRAIN) 25 | for key_i in var_to_shape_map: 26 | 27 | try: 28 | 29 | if 'data' in key_i: 30 | pass 31 | elif 'weights' in key_i: 32 | a = key_i.split('/') 33 | if (len(a) == 3): 34 | key_caffe = a[1] 35 | else: 36 | key_caffe = a[2] 37 | if (reader.get_tensor(key_i).ndim == 4): 38 | if (key_caffe == 'fc6'): 39 | weights = tensor4d_transform(reader.get_tensor(key_i).reshape([7, 7, 512, 4096])).reshape( 40 | [[7, 7, 512, 4096][3], -1]) 41 | elif key_caffe == 'fc7': 42 | a = np.squeeze(reader.get_tensor(key_i)[0][0]) 43 | weights = tensor2d_transform(a) # 2dim 44 | elif key_caffe == 'fc8': 45 | a = np.squeeze(reader.get_tensor(key_i)[0][0]) 46 | weights = tensor2d_transform(a) # 2dim 47 | else: 48 | weights = tensor4d_transform(reader.get_tensor(key_i)) 49 | net.params[key_caffe][0].data.flat = weights.flat 50 | elif 'biases' in key_i: 51 | a = key_i.split('/') 52 | if (len(a) == 3): 53 | key_caffe = a[1] 54 | else: 55 | key_caffe = a[2] 56 | net.params[key_caffe][1].data.flat = reader.get_tensor(key_i).flat 57 | elif 'mean_rgb' in key_i: 58 | pass 59 | elif 'global' in key_i: 60 | pass 61 | else: 62 | sys.exit("Warning! Unknown tf:{}".format(key_i)) 63 | except KeyError: 64 | print("\nWarning! key error tf:{}".format(key_i)) 65 | net.save(cf_model) 66 | print("\n- Finished.\n") 67 | -------------------------------------------------------------------------------- /tf2caffe_faster_rcnn.py: -------------------------------------------------------------------------------- 1 | import sys, argparse 2 | import tensorflow as tf 3 | sys.path.append('/home/root/py-faster-rcnn/caffe-fast-rcnn/python') 4 | sys.path.append('/home/root/py-faster-rcnn/lib') 5 | import caffe 6 | import numpy as np 7 | 8 | sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) 9 | import trans_tools as trans 10 | 11 | from tensorflow.python.training import checkpoint_utils as cp 12 | 13 | 14 | def tensor4d_transform(tensor): 15 | return tensor.transpose((3, 2, 0, 1)) 16 | def tensor2d_transform(tensor): 17 | return tensor.transpose((1, 0)) 18 | def tf2caffe(): 19 | checkpoint_path = "./VGGnet_fast_rcnn_iter_70000.ckpt" 20 | tensorName = cp.list_variables(checkpoint_path) 21 | 22 | cf_prototxt = "./vgg14faster-rcnn.prototxt" 23 | cf_model = "./vgg16faster0814.caffemodel" 24 | net = caffe.Net(cf_prototxt, caffe.TRAIN) 25 | 26 | for key_value in tensorName: 27 | key_i = key_value[0] 28 | nddary_data = cp.load_variable(checkpoint_path, key_i) 29 | try: 30 | 31 | if 'data' in key_i: 32 | pass 33 | elif 'weights' in key_i: 34 | a = key_i.split('/') 35 | if (len(a) == 2): 36 | key_caffe = a[0] 37 | if (len(a) == 3): 38 | key_caffe = "rpn_conv_3x3" 39 | if key_caffe == 'cls_score': 40 | weights = tensor2d_transform(nddary_data) # 2dim 41 | if key_caffe == 'bbox_pred': 42 | weights = tensor2d_transform(nddary_data) # 2dim 43 | if key_caffe == 'fc7': 44 | weights = tensor2d_transform(nddary_data) # 2dim 45 | if key_caffe == 'fc6': 46 | weights = tensor2d_transform(nddary_data) # 2dim 47 | 48 | if (nddary_data.ndim == 4): 49 | if key_caffe == 'rpn_cls_score': 50 | a = np.squeeze(nddary_data[0][0]) 51 | weights = tensor2d_transform(a) # 2dim 52 | elif key_caffe == 'rpn_bbox_pred': 53 | a = np.squeeze(nddary_data[0][0]) 54 | weights = tensor2d_transform(a) # 2dim 55 | else: 56 | weights = tensor4d_transform(nddary_data) 57 | net.params[key_caffe][0].data.flat = weights.flat 58 | elif 'biases' in key_i: 59 | a = key_i.split('/') 60 | if (len(a) == 2): 61 | key_caffe = a[0] 62 | if (len(a) == 3): 63 | key_caffe = "rpn_conv_3x3" 64 | net.params[key_caffe][1].data.flat = nddary_data.flat 65 | elif 'bn_gamma' in key_i: 66 | a = key_i.split('/') 67 | if (len(a) == 3): 68 | key_caffe = a[1] 69 | else: 70 | key_caffe = a[2] 71 | net.params[key_caffe][0].data.flat = nddary_data.flat 72 | elif '_gamma' in key_i: # for prelu 73 | a = key_i.split('/') 74 | if (len(a) == 3): 75 | key_caffe = a[1] 76 | else: 77 | key_caffe = a[2] 78 | assert (len(net.params[key_caffe]) == 1) 79 | net.params[key_caffe][0].data.flat = nddary_data.flat 80 | elif 'mean_rgb' in key_i: 81 | pass 82 | elif 'global' in key_i: 83 | pass 84 | else: 85 | sys.exit("Warning! Unknown tf:{}".format(key_i)) 86 | 87 | except KeyError: 88 | print("\nWarning! key error tf:{}".format(key_i)) 89 | 90 | net.save(cf_model) 91 | print("\n- Finished.\n") 92 | -------------------------------------------------------------------------------- /vgg16Faster-rcnn.prototxt: -------------------------------------------------------------------------------- 1 | name: "VGG_ILSVRC_16_layers" 2 | 3 | input: "data" 4 | input_shape { 5 | dim: 1 6 | dim: 3 7 | dim: 224 8 | dim: 224 9 | } 10 | 11 | input: "im_info" 12 | input_shape { 13 | dim: 1 14 | dim: 3 15 | } 16 | 17 | layer { 18 | name: "conv1_1" 19 | type: "Convolution" 20 | bottom: "data" 21 | top: "conv1_1" 22 | param { 23 | lr_mult: 0 24 | decay_mult: 0 25 | } 26 | param { 27 | lr_mult: 0 28 | decay_mult: 0 29 | } 30 | convolution_param { 31 | num_output: 64 32 | pad: 1 33 | kernel_size: 3 34 | } 35 | } 36 | layer { 37 | name: "relu1_1" 38 | type: "ReLU" 39 | bottom: "conv1_1" 40 | top: "conv1_1" 41 | } 42 | layer { 43 | name: "conv1_2" 44 | type: "Convolution" 45 | bottom: "conv1_1" 46 | top: "conv1_2" 47 | param { 48 | lr_mult: 0 49 | decay_mult: 0 50 | } 51 | param { 52 | lr_mult: 0 53 | decay_mult: 0 54 | } 55 | convolution_param { 56 | num_output: 64 57 | pad: 1 58 | kernel_size: 3 59 | } 60 | } 61 | layer { 62 | name: "relu1_2" 63 | type: "ReLU" 64 | bottom: "conv1_2" 65 | top: "conv1_2" 66 | } 67 | layer { 68 | name: "pool1" 69 | type: "Pooling" 70 | bottom: "conv1_2" 71 | top: "pool1" 72 | pooling_param { 73 | pool: MAX 74 | kernel_size: 2 75 | stride: 2 76 | } 77 | } 78 | layer { 79 | name: "conv2_1" 80 | type: "Convolution" 81 | bottom: "pool1" 82 | top: "conv2_1" 83 | param { 84 | lr_mult: 0 85 | decay_mult: 0 86 | } 87 | param { 88 | lr_mult: 0 89 | decay_mult: 0 90 | } 91 | convolution_param { 92 | num_output: 128 93 | pad: 1 94 | kernel_size: 3 95 | } 96 | } 97 | layer { 98 | name: "relu2_1" 99 | type: "ReLU" 100 | bottom: "conv2_1" 101 | top: "conv2_1" 102 | } 103 | layer { 104 | name: "conv2_2" 105 | type: "Convolution" 106 | bottom: "conv2_1" 107 | top: "conv2_2" 108 | param { 109 | lr_mult: 0 110 | decay_mult: 0 111 | } 112 | param { 113 | lr_mult: 0 114 | decay_mult: 0 115 | } 116 | convolution_param { 117 | num_output: 128 118 | pad: 1 119 | kernel_size: 3 120 | } 121 | } 122 | layer { 123 | name: "relu2_2" 124 | type: "ReLU" 125 | bottom: "conv2_2" 126 | top: "conv2_2" 127 | } 128 | layer { 129 | name: "pool2" 130 | type: "Pooling" 131 | bottom: "conv2_2" 132 | top: "pool2" 133 | pooling_param { 134 | pool: MAX 135 | kernel_size: 2 136 | stride: 2 137 | } 138 | } 139 | layer { 140 | name: "conv3_1" 141 | type: "Convolution" 142 | bottom: "pool2" 143 | top: "conv3_1" 144 | param { 145 | lr_mult: 1 146 | decay_mult: 1 147 | } 148 | param { 149 | lr_mult: 2 150 | decay_mult: 0 151 | } 152 | convolution_param { 153 | num_output: 256 154 | pad: 1 155 | kernel_size: 3 156 | } 157 | } 158 | layer { 159 | name: "relu3_1" 160 | type: "ReLU" 161 | bottom: "conv3_1" 162 | top: "conv3_1" 163 | } 164 | layer { 165 | name: "conv3_2" 166 | type: "Convolution" 167 | bottom: "conv3_1" 168 | top: "conv3_2" 169 | param { 170 | lr_mult: 1 171 | decay_mult: 1 172 | } 173 | param { 174 | lr_mult: 2 175 | decay_mult: 0 176 | } 177 | convolution_param { 178 | num_output: 256 179 | pad: 1 180 | kernel_size: 3 181 | } 182 | } 183 | layer { 184 | name: "relu3_2" 185 | type: "ReLU" 186 | bottom: "conv3_2" 187 | top: "conv3_2" 188 | } 189 | layer { 190 | name: "conv3_3" 191 | type: "Convolution" 192 | bottom: "conv3_2" 193 | top: "conv3_3" 194 | param { 195 | lr_mult: 1 196 | decay_mult: 1 197 | } 198 | param { 199 | lr_mult: 2 200 | decay_mult: 0 201 | } 202 | convolution_param { 203 | num_output: 256 204 | pad: 1 205 | kernel_size: 3 206 | } 207 | } 208 | layer { 209 | name: "relu3_3" 210 | type: "ReLU" 211 | bottom: "conv3_3" 212 | top: "conv3_3" 213 | } 214 | layer { 215 | name: "pool3" 216 | type: "Pooling" 217 | bottom: "conv3_3" 218 | top: "pool3" 219 | pooling_param { 220 | pool: MAX 221 | kernel_size: 2 222 | stride: 2 223 | } 224 | } 225 | layer { 226 | name: "conv4_1" 227 | type: "Convolution" 228 | bottom: "pool3" 229 | top: "conv4_1" 230 | param { 231 | lr_mult: 1 232 | decay_mult: 1 233 | } 234 | param { 235 | lr_mult: 2 236 | decay_mult: 0 237 | } 238 | convolution_param { 239 | num_output: 512 240 | pad: 1 241 | kernel_size: 3 242 | } 243 | } 244 | layer { 245 | name: "relu4_1" 246 | type: "ReLU" 247 | bottom: "conv4_1" 248 | top: "conv4_1" 249 | } 250 | layer { 251 | name: "conv4_2" 252 | type: "Convolution" 253 | bottom: "conv4_1" 254 | top: "conv4_2" 255 | param { 256 | lr_mult: 1 257 | decay_mult: 1 258 | } 259 | param { 260 | lr_mult: 2 261 | decay_mult: 0 262 | } 263 | convolution_param { 264 | num_output: 512 265 | pad: 1 266 | kernel_size: 3 267 | } 268 | } 269 | layer { 270 | name: "relu4_2" 271 | type: "ReLU" 272 | bottom: "conv4_2" 273 | top: "conv4_2" 274 | } 275 | layer { 276 | name: "conv4_3" 277 | type: "Convolution" 278 | bottom: "conv4_2" 279 | top: "conv4_3" 280 | param { 281 | lr_mult: 1 282 | decay_mult: 1 283 | } 284 | param { 285 | lr_mult: 2 286 | decay_mult: 0 287 | } 288 | convolution_param { 289 | num_output: 512 290 | pad: 1 291 | kernel_size: 3 292 | } 293 | } 294 | layer { 295 | name: "relu4_3" 296 | type: "ReLU" 297 | bottom: "conv4_3" 298 | top: "conv4_3" 299 | } 300 | layer { 301 | name: "pool4" 302 | type: "Pooling" 303 | bottom: "conv4_3" 304 | top: "pool4" 305 | pooling_param { 306 | pool: MAX 307 | kernel_size: 2 308 | stride: 2 309 | } 310 | } 311 | layer { 312 | name: "conv5_1" 313 | type: "Convolution" 314 | bottom: "pool4" 315 | top: "conv5_1" 316 | param { 317 | lr_mult: 1 318 | decay_mult: 1 319 | } 320 | param { 321 | lr_mult: 2 322 | decay_mult: 0 323 | } 324 | convolution_param { 325 | num_output: 512 326 | pad: 1 327 | kernel_size: 3 328 | } 329 | } 330 | layer { 331 | name: "relu5_1" 332 | type: "ReLU" 333 | bottom: "conv5_1" 334 | top: "conv5_1" 335 | } 336 | layer { 337 | name: "conv5_2" 338 | type: "Convolution" 339 | bottom: "conv5_1" 340 | top: "conv5_2" 341 | param { 342 | lr_mult: 1 343 | decay_mult: 1 344 | } 345 | param { 346 | lr_mult: 2 347 | decay_mult: 0 348 | } 349 | convolution_param { 350 | num_output: 512 351 | pad: 1 352 | kernel_size: 3 353 | } 354 | } 355 | layer { 356 | name: "relu5_2" 357 | type: "ReLU" 358 | bottom: "conv5_2" 359 | top: "conv5_2" 360 | } 361 | layer { 362 | name: "conv5_3" 363 | type: "Convolution" 364 | bottom: "conv5_2" 365 | top: "conv5_3" 366 | param { 367 | lr_mult: 1 368 | decay_mult: 1 369 | } 370 | param { 371 | lr_mult: 2 372 | decay_mult: 0 373 | } 374 | convolution_param { 375 | num_output: 512 376 | pad: 1 377 | kernel_size: 3 378 | } 379 | } 380 | layer { 381 | name: "relu5_3" 382 | type: "ReLU" 383 | bottom: "conv5_3" 384 | top: "conv5_3" 385 | } 386 | 387 | #========= RPN ============ 388 | 389 | layer { 390 | name: "rpn_conv_3x3" 391 | type: "Convolution" 392 | bottom: "conv5_3" 393 | top: "rpn/output" 394 | param { lr_mult: 1.0 decay_mult: 1.0 } 395 | param { lr_mult: 2.0 decay_mult: 0 } 396 | convolution_param { 397 | num_output: 512 398 | kernel_size: 3 pad: 1 stride: 1 399 | weight_filler { type: "gaussian" std: 0.01 } 400 | bias_filler { type: "constant" value: 0 } 401 | } 402 | } 403 | layer { 404 | name: "rpn_relu_3x3" 405 | type: "ReLU" 406 | bottom: "rpn/output" 407 | top: "rpn/output" 408 | } 409 | 410 | layer { 411 | name: "rpn_cls_score" 412 | type: "Convolution" 413 | bottom: "rpn/output" 414 | top: "rpn_cls_score" 415 | param { lr_mult: 1.0 decay_mult: 1.0 } 416 | param { lr_mult: 2.0 decay_mult: 0 } 417 | convolution_param { 418 | num_output: 18 # 2(bg/fg) * 9(anchors) 419 | kernel_size: 1 pad: 0 stride: 1 420 | weight_filler { type: "gaussian" std: 0.01 } 421 | bias_filler { type: "constant" value: 0 } 422 | } 423 | } 424 | layer { 425 | name: "rpn_bbox_pred" 426 | type: "Convolution" 427 | bottom: "rpn/output" 428 | top: "rpn_bbox_pred" 429 | param { lr_mult: 1.0 decay_mult: 1.0 } 430 | param { lr_mult: 2.0 decay_mult: 0 } 431 | convolution_param { 432 | num_output: 36 # 4 * 9(anchors) 433 | kernel_size: 1 pad: 0 stride: 1 434 | weight_filler { type: "gaussian" std: 0.01 } 435 | bias_filler { type: "constant" value: 0 } 436 | } 437 | } 438 | layer { 439 | bottom: "rpn_cls_score" 440 | top: "rpn_cls_score_reshape" 441 | name: "rpn_cls_score_reshape" 442 | type: "Reshape" 443 | reshape_param { shape { dim: 0 dim: 2 dim: -1 dim: 0 } } 444 | } 445 | 446 | #========= RoI Proposal ============ 447 | 448 | layer { 449 | name: "rpn_cls_prob" 450 | type: "Softmax" 451 | bottom: "rpn_cls_score_reshape" 452 | top: "rpn_cls_prob" 453 | } 454 | layer { 455 | name: 'rpn_cls_prob_reshape' 456 | type: 'Reshape' 457 | bottom: 'rpn_cls_prob' 458 | top: 'rpn_cls_prob_reshape' 459 | reshape_param { shape { dim: 0 dim: 18 dim: -1 dim: 0 } } 460 | } 461 | layer { 462 | name: 'proposal' 463 | type: 'Python' 464 | bottom: 'rpn_cls_prob_reshape' 465 | bottom: 'rpn_bbox_pred' 466 | bottom: 'im_info' 467 | top: 'rois' 468 | python_param { 469 | module: 'rpn.proposal_layer' 470 | layer: 'ProposalLayer' 471 | param_str: "'feat_stride': 16" 472 | } 473 | } 474 | 475 | #========= RCNN ============ 476 | 477 | layer { 478 | name: "roi_pool5" 479 | type: "ROIPooling" 480 | bottom: "conv5_3" 481 | bottom: "rois" 482 | top: "pool5" 483 | roi_pooling_param { 484 | pooled_w: 7 485 | pooled_h: 7 486 | spatial_scale: 0.0625 # 1/16 487 | } 488 | } 489 | layer { 490 | name: "fc6" 491 | type: "InnerProduct" 492 | bottom: "pool5" 493 | top: "fc6" 494 | param { 495 | lr_mult: 1 496 | decay_mult: 1 497 | } 498 | param { 499 | lr_mult: 2 500 | decay_mult: 0 501 | } 502 | inner_product_param { 503 | num_output: 4096 504 | } 505 | } 506 | layer { 507 | name: "relu6" 508 | type: "ReLU" 509 | bottom: "fc6" 510 | top: "fc6" 511 | } 512 | layer { 513 | name: "drop6" 514 | type: "Dropout" 515 | bottom: "fc6" 516 | top: "fc6" 517 | dropout_param { 518 | dropout_ratio: 0.5 519 | } 520 | } 521 | layer { 522 | name: "fc7" 523 | type: "InnerProduct" 524 | bottom: "fc6" 525 | top: "fc7" 526 | param { 527 | lr_mult: 1 528 | decay_mult: 1 529 | } 530 | param { 531 | lr_mult: 2 532 | decay_mult: 0 533 | } 534 | inner_product_param { 535 | num_output: 4096 536 | } 537 | } 538 | layer { 539 | name: "relu7" 540 | type: "ReLU" 541 | bottom: "fc7" 542 | top: "fc7" 543 | } 544 | layer { 545 | name: "drop7" 546 | type: "Dropout" 547 | bottom: "fc7" 548 | top: "fc7" 549 | dropout_param { 550 | dropout_ratio: 0.5 551 | } 552 | } 553 | layer { 554 | name: "cls_score" 555 | type: "InnerProduct" 556 | bottom: "fc7" 557 | top: "cls_score" 558 | param { 559 | lr_mult: 1 560 | decay_mult: 1 561 | } 562 | param { 563 | lr_mult: 2 564 | decay_mult: 0 565 | } 566 | inner_product_param { 567 | num_output: 21 568 | weight_filler { 569 | type: "gaussian" 570 | std: 0.01 571 | } 572 | bias_filler { 573 | type: "constant" 574 | value: 0 575 | } 576 | } 577 | } 578 | layer { 579 | name: "bbox_pred" 580 | type: "InnerProduct" 581 | bottom: "fc7" 582 | top: "bbox_pred" 583 | param { 584 | lr_mult: 1 585 | decay_mult: 1 586 | } 587 | param { 588 | lr_mult: 2 589 | decay_mult: 0 590 | } 591 | inner_product_param { 592 | num_output: 84 593 | weight_filler { 594 | type: "gaussian" 595 | std: 0.001 596 | } 597 | bias_filler { 598 | type: "constant" 599 | value: 0 600 | } 601 | } 602 | } 603 | layer { 604 | name: "cls_prob" 605 | type: "Softmax" 606 | bottom: "cls_score" 607 | top: "cls_prob" 608 | } 609 | --------------------------------------------------------------------------------