1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
from __future__ import division, print_function
import numpy as np import tensorflow as tf
slim = tf.contrib.slim
def conv2d(inputs, filters, kernel_size, strides=1): def _fixed_padding(inputs, kernel_size): pad_total = kernel_size - 1 pad_beg = pad_total // 2 pad_end = pad_total - pad_beg
padded_inputs = tf.pad(inputs, [[0, 0], [pad_beg, pad_end], [pad_beg, pad_end], [0, 0]], mode='CONSTANT') return padded_inputs
if strides > 1: inputs = _fixed_padding(inputs, kernel_size) inputs = slim.conv2d(inputs, filters, kernel_size, stride=strides, padding=('SAME' if strides == 1 else 'VALID')) return inputs
def darknet53_body(inputs): """ darknet的主体网络框架 :param inputs: :return: 三张不同尺度的特征图 """ def res_block(inputs, filters): shortcut = inputs net = conv2d(inputs, filters * 1, 1) net = conv2d(net, filters * 2, 3)
net = net + shortcut
return net
net = conv2d(inputs, 32, 3, strides=1) net = conv2d(net, 64, 3, strides=2)
net = res_block(net, 32)
net = conv2d(net, 128, 3, strides=2)
for i in range(2): net = res_block(net, 64)
net = conv2d(net, 256, 3, strides=2)
for i in range(8): net = res_block(net, 128)
route_1 = net net = conv2d(net, 512, 3, strides=2)
for i in range(8): net = res_block(net, 256)
route_2 = net net = conv2d(net, 1024, 3, strides=2)
for i in range(4): net = res_block(net, 512) route_3 = net
return route_1, route_2, route_3
def yolo_block(inputs, filters): """ 在darknet主体网络提取特征的基础上增加的若干卷积层,为了后面的特征融合做准备 :param inputs: :param filters: :return: """ net = conv2d(inputs, filters * 1, 1) net = conv2d(net, filters * 2, 3) net = conv2d(net, filters * 1, 1) net = conv2d(net, filters * 2, 3) net = conv2d(net, filters * 1, 1) route = net net = conv2d(net, filters * 2, 3) return route, net
def upsample_layer(inputs, out_shape): """ 这一部分主要是对特征图进行resize,默认使用最近邻方法 :param inputs: :param out_shape: :return: """ new_height, new_width = out_shape[1], out_shape[2] inputs = tf.image.resize_nearest_neighbor(inputs, (new_height, new_width), name='upsampled') return inputs
|