def dark_net_conv_1(ipt, i, name='dark_net_conv_1', reuse=False, is_training=True, norm='batch'): c1s1k32 = ops.conv2d(ipt, 32, 1, 1, False, norm=norm, activation=ops.leaky_relu, name=name + str(i), reuse=reuse, is_training=is_training) c3s1k64 = ops.conv2d(c1s1k32, 64, 3, 1, True, norm=norm, activation=ops.leaky_relu, name=name + str(i + 1), reuse=reuse, is_training=is_training) return tf.add(c3s1k64, ipt, name='shortcut_0')
def dark_net_conv_5(ipt, i, j, name='dark_net_conv_5', reuse=False, is_training=True, norm='batch'): c1s1k512 = ops.conv2d(ipt, 512, 1, 1, False, norm=norm, activation=ops.leaky_relu, name=name + str(i), reuse=reuse, is_training=is_training) c3s1k1024 = ops.conv2d(c1s1k512, 1024, 3, 1, True, norm=norm, activation=ops.leaky_relu, name=name + str(i + 1), reuse=reuse, is_training=is_training) return tf.add(c3s1k1024, ipt, name='shortcut_' + str(j + 19))
def dark_net_conv_2(ipt, i, j, name='dark_net_conv_2', reuse=False, is_training=True, norm='batch'): c1s1k64 = ops.conv2d(ipt, 64, 1, 1, False, norm=norm, activation=ops.leaky_relu, name=name + str(i), reuse=reuse, is_training=is_training) c3s1k128 = ops.conv2d(c1s1k64, 128, 3, 1, True, norm=norm, activation=ops.leaky_relu, name=name + str(i + 1), reuse=reuse, is_training=is_training) if j == 0: return tf.add(c3s1k128, ipt, name='shortcut_1') else: return tf.add(c3s1k128, ipt, name='shortcut_2')
def dark_net_conv_3(ipt, i, j, name='dark_net_conv_3', reuse=False, is_training=True, norm='batch'): c1s1k128 = ops.conv2d(ipt, 128, 1, 1, False, norm=norm, activation=ops.leaky_relu, name=name + str(i), reuse=reuse, is_training=is_training) c3s1k256 = ops.conv2d(c1s1k128, 256, 3, 1, True, norm=norm, activation=ops.leaky_relu, name=name + str(i + 1), reuse=reuse, is_training=is_training) return tf.add(c3s1k256, ipt, name='shotcut_' + str(j + 3))
def inception_module_factorization(inputs, scope=""): """ inception module after factorization of n x n convolutions, as illustrated in figure 6 in paper :param inputs: input tensor with 784 channel :param scope: scope name :return: tensor """ with tf.variable_scope(scope): with tf.variable_scope("branch1x1"): branch1x1 = conv2d(inputs, 192, [1, 1]) with tf.variable_scope("branch7x7"): branch7x7 = conv2d(inputs, 160, [1, 1]) branch7x7 = conv2d(branch7x7, 160, [1, 7]) branch7x7 = conv2d(branch7x7, 192, [7, 1]) with tf.variable_scope("branch7x7db"): branch7x7db = conv2d(inputs, 160, [1, 1]) branch7x7db = conv2d(branch7x7db, 160, [7, 1]) branch7x7db = conv2d(branch7x7db, 160, [1, 7]) branch7x7db = conv2d(branch7x7db, 160, [7, 1]) branch7x7db = conv2d(branch7x7db, 192, [1, 7]) with tf.variable_scope("branch_pool"): branch_pool = tf.nn.avg_pool(inputs, [1, 3, 3, 1], [1, 1, 1, 1], padding="SAME") branch_pool = conv2d(branch_pool, 192, [1, 1]) net = tf.concat( axis=3, values=[branch1x1, branch7x7, branch7x7db, branch_pool]) return net
def inception_module_expanded(inputs, scope=""): with tf.variable_scope(scope): with tf.variable_scope('branch1x1'): branch1x1 = conv2d(inputs, 320, [1, 1]) with tf.variable_scope('branch3x3'): branch3x3 = conv2d(inputs, 384, [1, 1]) branch3x3 = tf.concat(axis=3, values=[ conv2d(branch3x3, 384, [1, 3]), conv2d(branch3x3, 384, [3, 1]) ]) with tf.variable_scope('branch3x3db'): branch3x3db = conv2d(inputs, 448, [1, 1]) branch3x3db = conv2d(branch3x3db, 384, [3, 3]) branch3x3db = tf.concat(axis=3, values=[ conv2d(branch3x3db, 384, [1, 3]), conv2d(branch3x3db, 384, [3, 1]) ]) with tf.variable_scope("branch_pool"): branch_pool = tf.nn.avg_pool(inputs, [1, 3, 3, 1], [1, 1, 1, 1], padding='SAME') branch_pool = conv2d(branch_pool, 192, [1, 1]) net = tf.concat( axis=3, values=[branch1x1, branch3x3, branch3x3db, branch_pool]) return net
def inception_module_origin(inputs, scope=""): """ The original inception module, change 5x5 to two 3x3 :param inputs: inputs with channel of 192 :param scope: scope name :return: tensor """ # 61 * 61 * 192 with tf.variable_scope(scope): with tf.variable_scope('branch1x1'): branch1x1 = conv2d(inputs, 64, [1, 1]) with tf.variable_scope('branch5x5'): branch5x5 = conv2d(inputs, 48, [1, 1]) branch5x5 = conv2d(branch5x5, 64, [5, 5]) with tf.variable_scope('branch3x3'): branch3x3 = conv2d(inputs, 64, [1, 1]) branch3x3 = conv2d(branch3x3, 96, [3, 3]) branch3x3 = conv2d(branch3x3, 96, [3, 3]) with tf.variable_scope('branch_pool'): branch_pool = tf.nn.avg_pool(inputs, [1, 3, 3, 1], [1, 1, 1, 1], padding='SAME') branch_pool = conv2d(branch_pool, 64, [1, 1]) net = tf.concat(axis=3, values=[branch1x1, branch5x5, branch3x3, branch_pool]) return net
def c3s2k64(ipt, name='c3s2k64', reuse=False, is_training=True, norm='batch'): return ops.conv2d(ipt, 64, 3, 2, True, norm=norm, activation=ops.leaky_relu, name=name, reuse=reuse, is_training=is_training)
def c3s1k32(ipt, name='c3s1k32', reuse=False, is_training=True, norm='batch'): return ops.conv2d(ipt, 32, 3, 1, True, norm=norm, activation=ops.leaky_relu, name=name, reuse=reuse, is_training=is_training)
def inception_reduction_v2(inputs, scope=""): """ module used for reduce dimension, with different input and output shape :param inputs: inputs tensor of 768 :param scope: scope name :return: output tensor of 1280 """ with tf.variable_scope(scope): with tf.variable_scope('branch3x3'): branch3x3 = conv2d(inputs, 192, [1, 1]) branch3x3 = conv2d(branch3x3, 320, [3, 3], stride=2, padding='VALID') with tf.variable_scope('branch7x7x3'): branch7x7x3 = conv2d(inputs, 192, [1, 1]) branch7x7x3 = conv2d(branch7x7x3, 192, [1, 7]) branch7x7x3 = conv2d(branch7x7x3, 192, [7, 1]) branch7x7x3 = conv2d(branch7x7x3, 192, [3, 3], stride=2, padding='VALID') with tf.variable_scope("branch_pool"): branch_pool = tf.nn.max_pool(inputs, [1, 3, 3, 1], [1, 2, 2, 1], padding='VALID') net = tf.concat(axis=3, values=[branch3x3, branch7x7x3, branch_pool]) return net
def discriminator(self, image, conditioning, training, reuse=False): with tf.variable_scope("discriminator", reuse=reuse): net = conv2d(image, self.d_dim, training=training) # (14 x 14 x 64) net = conv2d(net, self.d_dim * 2, training=training) # (7 x 7 x 128) net = conv2d(net, self.d_dim * 4, training=training) # (4 x 4 x 128) conditioning = tf.reshape(conditioning, [-1, 1, 1, conditioning.shape[1]]) conditioning = tf.tile(conditioning, [1, net.shape[1], net.shape[2], 1]) net = tf.concat([conditioning, net], axis=-1) # (4 x 4 x 601) net = conv2d(net, self.d_dim * 4, kernels=1, strides=1) # (4 x 4 x 256) net = layers.conv2d(net, 1, kernel_size=4, strides=1) # (1 x 1 x 1) net = tf.nn.sigmoid(net) net = tf.squeeze(net) return net
def inception_reduction_v1(inputs, scope=""): """ module used for reduce dimension, as illustrated in figure 10 in the paper :param inputs: tensor of shape 288 :param scope: scope name :return: tensor """ with tf.variable_scope(scope): with tf.variable_scope("branch3x3"): branch3x3 = conv2d(inputs, 384, [3, 3], stride=2, padding='VALID') with tf.variable_scope("branch3x3db"): branch3x3db = conv2d(inputs, 64, [1, 1]) branch3x3db = conv2d(branch3x3db, 96, [3, 3]) branch3x3db = conv2d(branch3x3db, 96, [3, 3], stride=2, padding='VALID') with tf.variable_scope("branch_pool"): branch_pool = tf.nn.max_pool(inputs, [1, 3, 3, 1], [1, 2, 2, 1], padding='VALID') net = tf.concat(axis=3, values=[branch3x3, branch3x3db, branch_pool]) return net
def yolo_3(ipt1, ipt2, num_classes, reuse=False, is_training=True, norm='batch', activation=ops.leaky_relu): c1s1k128 = ops.conv2d(ipt1, 128, 1, 1, True, norm=norm, activation=activation, name='conv_67', reuse=reuse, is_training=is_training) with tf.variable_scope('upsample_1'): upsample = tf.image.resize_nearest_neighbor( c1s1k128, (c1s1k128.shape[1] * 2, c1s1k128.shape[2] * 2)) with tf.variable_scope('route_1'): route = tf.concat([upsample, ipt2], axis=3) c1s1k128 = ops.conv2d(route, 128, 1, 1, False, norm=norm, activation=activation, name='conv_68', reuse=reuse, is_training=is_training) c3s1k256 = ops.conv2d(c1s1k128, 256, 3, 1, True, norm=norm, activation=activation, name='conv_69', reuse=reuse, is_training=is_training) c1s1k128 = ops.conv2d(c3s1k256, 128, 1, 1, False, norm=norm, activation=activation, name='conv_70', reuse=reuse, is_training=is_training) c3s1k256 = ops.conv2d(c1s1k128, 256, 3, 1, True, norm=norm, activation=activation, name='conv_71', reuse=reuse, is_training=is_training) c1s1k128 = ops.conv2d(c3s1k256, 128, 1, 1, False, norm=norm, activation=activation, name='conv_72', reuse=reuse, is_training=is_training) c3s1k256 = ops.conv2d(c1s1k128, 256, 3, 1, True, norm=norm, activation=activation, name='conv_73', reuse=reuse, is_training=is_training) small_obj_raw_detections = ops.conv2d(c3s1k256, 3 * (num_classes + 5), 1, 1, False, norm=None, activation=None, use_bias=True, name='conv_74', reuse=reuse, is_training=is_training) return small_obj_raw_detections
def yolo_2(ipt1, ipt2, num_classes, reuse=False, is_training=True, norm='batch', activation=ops.leaky_relu): c1s1k256 = ops.conv2d(ipt1, 256, 1, 1, False, norm=norm, activation=activation, name='conv_59', reuse=reuse, is_training=is_training) with tf.variable_scope('upsample_0'): upsample = tf.image.resize_nearest_neighbor( c1s1k256, (c1s1k256.shape[1] * 2, c1s1k256.shape[2] * 2)) with tf.variable_scope('route_0'): route = tf.concat([upsample, ipt2], axis=3) c1s1k256 = ops.conv2d(route, 256, 1, 1, False, norm=norm, activation=activation, name='conv_60', reuse=reuse, is_training=is_training) c3s1k512 = ops.conv2d(c1s1k256, 512, 3, 1, True, norm=norm, activation=activation, name='conv_61', reuse=reuse, is_training=is_training) c1s1k256 = ops.conv2d(c3s1k512, 256, 1, 1, False, norm=norm, activation=activation, name='conv_62', reuse=reuse, is_training=is_training) c3s1k512 = ops.conv2d(c1s1k256, 512, 3, 1, True, norm=norm, activation=activation, name='conv_63', reuse=reuse, is_training=is_training) c1s1k256 = ops.conv2d(c3s1k512, 256, 1, 1, False, norm=norm, activation=activation, name='conv_64', reuse=reuse, is_training=is_training) yolo_route = c1s1k256 c3s1k512 = ops.conv2d(c1s1k256, 512, 3, 1, True, norm=norm, activation=activation, name='conv_65', reuse=reuse, is_training=is_training) medium_obj_raw_detections = ops.conv2d(c3s1k512, 3 * (num_classes + 5), 1, 1, False, norm=None, activation=None, use_bias=True, name='conv_66', reuse=reuse, is_training=is_training) return medium_obj_raw_detections, yolo_route
def yolo_1(ipt, num_classes, reuse=False, is_training=True, norm='bath', activation=ops.leaky_relu): c1s1k512 = ops.conv2d(ipt, 512, 1, 1, False, norm=norm, activation=activation, name='conv_52', reuse=reuse, is_training=is_training) c3s1k1024 = ops.conv2d(c1s1k512, 1024, 3, 1, True, norm=norm, activation=activation, name='conv_53', reuse=reuse, is_training=is_training) c1s1k512 = ops.conv2d(c3s1k1024, 512, 1, 1, False, norm=norm, activation=activation, name='conv_54', reuse=reuse, is_training=is_training) c3s1k1024 = ops.conv2d(c1s1k512, 1024, 3, 1, True, norm=norm, activation=activation, name='conv_55', reuse=reuse, is_training=is_training) c1s1k512 = ops.conv2d(c3s1k1024, 512, 1, 1, False, norm=norm, activation=activation, name='conv_56', reuse=reuse, is_training=is_training) yolo_route = c1s1k512 c3s1k1024 = ops.conv2d(c1s1k512, 1024, 3, 1, True, norm=norm, activation=activation, name='conv_57', reuse=reuse, is_training=is_training) large_obj_raw_detections = ops.conv2d(c3s1k1024, 3 * (num_classes + 5), 1, 1, False, norm=None, activation=None, use_bias=True, name='conv_58', reuse=reuse, is_training=is_training) return large_obj_raw_detections, yolo_route
def inception_v3(inputs, dropout_keep_prob=0.8, num_classes=2, is_training=True, restore_logits=True, scope=""): with tf.name_scope(scope, "inception_v3", [inputs]): # 512 * 512 * 3 conv0 = conv2d(inputs, 32, [3, 3], stride=2, scope='conv0', padding='VALID') # 255 * 255 * 32 conv1 = conv2d(conv0, 32, [3, 3], scope='conv1', padding='VALID') # 253 * 253 * 32 conv2 = conv2d(conv1, 64, [3, 3], scope='conv2') # 253 * 253 * 64 with tf.name_scope('pool1') as scope: pool1 = tf.nn.max_pool(conv2, [1, 3, 3, 1], [1, 2, 2, 1], padding='VALID', name=scope) # 126 * 126 * 64 conv3 = conv2d(conv2, 80, [1, 1], scope='conv3', padding='VALID') # 126 * 126 * 80 conv4 = conv2d(conv3, 192, [3, 3], scope='conv4', padding='VALID') # 124 * 124 * 192 with tf.name_scope('pool2') as scope: pool2 = tf.nn.max_pool(conv4, [1, 3, 3, 1], [1, 2, 2, 1], padding='VALID', name=scope) net = pool2 for i in range(3): net = inception_module_origin(net, scope="inception_origin" + str(i + 1)) # 61 * 61 * 288 net = inception_reduction_v1(net, scope="reduction_v1") # 30 * 30 * 768 for i in range(4): net = inception_module_factorization( net, scope="inception_factorization" + str(i)) # Auxiliary Head logits, input 30 * 30 * 768 with tf.variable_scope("aux_logits"): aux_logits = tf.identity(net) aux_logits = tf.nn.avg_pool(aux_logits, [1, 5, 5, 1], [1, 3, 3, 1], padding='VALID') # 9 * 9 * 768 aux_logits = conv2d(aux_logits, 128, [1, 1], scope='proj') shape = aux_logits.get_shape() aux_logits = conv2d(aux_logits, 768, shape[1:3], stddev=0.01, padding='VALID') dims = aux_logits.get_shape()[1:] k = dims.num_elements() aux_logits = tf.reshape(aux_logits, [-1, k]) aux_logits = fc(aux_logits, num_classes, activation=None, stddev=0.001) net = inception_reduction_v2(net, scope="reduction_v2") # 14 * 14 * 1280 for i in range(2): net = inception_module_expanded(net, scope="inception_expanded" + str(i)) with tf.variable_scope("logits"): shape = net.get_shape() net = tf.nn.avg_pool(net, [1, shape[1], shape[2], 1], [1, 1, 1, 1], padding='VALID') net = tf.nn.dropout(net, dropout_keep_prob, name="dropout") dims = net.get_shape()[1:].num_elements() net = tf.reshape(net, [-1, dims]) logits = fc(net, num_classes, activation=None, scope="logits") return logits, aux_logits