コード例 #1
0
def _seresblock_proj(name,
                     input,
                     weight1,
                     weight2,
                     weight_proj,
                     filter_num,
                     ratio=4):
    with tf.variable_scope(name):
        fc1_w = tf.get_variable(name=name + '_fc1_w',
                                shape=[filter_num,
                                       int(filter_num / ratio)],
                                initializer=he())
        fc2_w = tf.get_variable(name=name + '_fc2_w',
                                shape=[int(filter_num / ratio), filter_num],
                                initializer=he())

        # residual block
        conv = tf.nn.conv2d(input,
                            weight1,
                            strides=(1, 1, 1, 1),
                            padding='SAME')
        bn = tf.layers.batch_normalization(conv)
        af = tf.nn.relu(bn)
        conv = tf.nn.conv2d(af, weight2, strides=(1, 1, 1, 1), padding='SAME')
        bn = tf.layers.batch_normalization(conv)
        proj = tf.nn.conv2d(input,
                            weight_proj,
                            strides=(1, 1, 1, 1),
                            padding='SAME')
        bn_proj = tf.layers.batch_normalization(proj)

        # squeeze & exitation block
        gap = tf.reduce_mean(bn_proj, axis=[1, 2])  # (B x 1 x C)
        fc1 = tf.reshape(gap, shape=[-1, filter_num])  # (B x C)
        fc1 = tf.matmul(fc1, fc1_w)  # (B x C/R)
        relu = tf.nn.relu(fc1)  # (B x C/R)
        fc2 = tf.matmul(relu, fc2_w)  # (B x C)
        sig = tf.nn.sigmoid(fc2)  # (B x C)
        descriptor = tf.reshape(sig, shape=[-1, 1, 1, filter_num])

        out = tf.nn.relu(bn + bn_proj * descriptor)

    return out
コード例 #2
0
def _seresblock_double(name, input, weight1, weight2, filter_num, ratio=4):
    with tf.variable_scope(name):
        fc1_w = tf.get_variable(name=name + '_fc1_w',
                                shape=[filter_num,
                                       int(filter_num / ratio)],
                                initializer=he())
        fc2_w = tf.get_variable(name=name + '_fc2_w',
                                shape=[int(filter_num / ratio), filter_num],
                                initializer=he())

        # residual block
        bn1 = tf.layers.batch_normalization(input)
        relu1 = tf.nn.leaky_relu(bn1)
        conv1 = tf.nn.conv2d(relu1,
                             weight1,
                             strides=(1, 1, 1, 1),
                             padding='SAME')
        bn2 = tf.layers.batch_normalization(conv1)
        relu2 = tf.nn.leaky_relu(bn2)
        conv2 = tf.nn.conv2d(relu2,
                             weight2,
                             strides=(1, 1, 1, 1),
                             padding='SAME')

        # squeeze & exitation block
        gap = tf.reduce_mean(conv2, axis=[1, 2])  # (B x 1 x C)
        fc1 = tf.reshape(gap, shape=[-1, filter_num])  # (B x C)
        fc1 = tf.matmul(fc1, fc1_w)  # (B x C/R)
        relu = tf.nn.relu(fc1)  # (B x C/R)
        fc2 = tf.matmul(relu, fc2_w)  # (B x C)
        sig = tf.nn.sigmoid(fc2)  # (B x C)
        descriptor = tf.reshape(sig, shape=[-1, 1, 1, filter_num])

        out = input + conv2 * descriptor

    return out
コード例 #3
0
    def __init__(self, name, images, channel, classes):
        with tf.variable_scope(name):
            # 1 - Filters
            self.conv_w = tf.get_variable(name='cls_conv_w',
                                          shape=[3, 3, channel, 64],
                                          initializer=he())

            self.res_w1_1 = tf.get_variable(name='cls_res_w1_1',
                                            shape=[3, 3, 64, 64],
                                            initializer=he())
            self.res_w1_2 = tf.get_variable(name='cls_res_w1_2',
                                            shape=[3, 3, 64, 64],
                                            initializer=he())
            self.res_w1_3 = tf.get_variable(name='cls_res_w1_3',
                                            shape=[3, 3, 64, 64],
                                            initializer=he())
            self.res_w1_4 = tf.get_variable(name='cls_res_w1_4',
                                            shape=[3, 3, 64, 64],
                                            initializer=he())

            self.proj_w2 = tf.get_variable(name='cls_proj_w2',
                                           shape=[1, 1, 64, 128],
                                           initializer=he())
            self.res_w2_1 = tf.get_variable(name='cls_res_w2_1',
                                            shape=[3, 3, 64, 128],
                                            initializer=he())
            self.res_w2_2 = tf.get_variable(name='cls_res_w2_2',
                                            shape=[3, 3, 128, 128],
                                            initializer=he())
            self.res_w2_3 = tf.get_variable(name='cls_res_w2_3',
                                            shape=[3, 3, 128, 128],
                                            initializer=he())
            self.res_w2_4 = tf.get_variable(name='cls_res_w2_4',
                                            shape=[3, 3, 128, 128],
                                            initializer=he())

            self.proj_w3 = tf.get_variable(name='cls_proj_w3',
                                           shape=[1, 1, 128, 256],
                                           initializer=he())
            self.res_w3_1 = tf.get_variable(name='cls_res_w3_1',
                                            shape=[3, 3, 128, 256],
                                            initializer=he())
            self.res_w3_2 = tf.get_variable(name='cls_res_w3_2',
                                            shape=[3, 3, 256, 256],
                                            initializer=he())
            self.res_w3_3 = tf.get_variable(name='cls_res_w3_3',
                                            shape=[3, 3, 256, 256],
                                            initializer=he())
            self.res_w3_4 = tf.get_variable(name='cls_res_w3_4',
                                            shape=[3, 3, 256, 256],
                                            initializer=he())

            self.conv_last_w1 = tf.get_variable(name='conv_last_w1',
                                                shape=[3, 3, 256, 512],
                                                initializer=he())
            self.fc_w1 = tf.get_variable(name='fc_w1',
                                         shape=[2048, classes
                                                ])  # bounding box coords
            # self.concat_w = tf.get_variable(name='concat_w', shape=[3072, classes])

            # 2 - Graphs
            self.conv = tf.nn.relu(
                tf.nn.conv2d(images,
                             self.conv_w,
                             strides=(1, 1, 1, 1),
                             padding='SAME'))
            self.pool = tf.nn.max_pool(self.conv,
                                       ksize=(1, 2, 2, 1),
                                       strides=(1, 2, 2, 1),
                                       padding='SAME')  # (?,64,64,64)

            self.res1_1 = _resblock_double('cls_res1_1', self.pool,
                                           self.res_w1_1, self.res_w1_2)
            self.res1_2 = _resblock_double('cls_res1_2', self.res1_1,
                                           self.res_w1_3, self.res_w1_4)
            self.pool1 = tf.nn.max_pool(self.res1_2,
                                        ksize=(1, 2, 2, 1),
                                        strides=(1, 2, 2, 1),
                                        padding='SAME')  # (?,32,32,64)

            self.res2_1 = _resblock_proj('cls_res2_1', self.pool1,
                                         self.res_w2_1, self.res_w2_2,
                                         self.proj_w2)
            self.res2_2 = _resblock_double('cls_res2_2', self.res2_1,
                                           self.res_w2_3, self.res_w2_4)
            self.pool2 = tf.nn.max_pool(self.res2_2,
                                        ksize=(1, 2, 2, 1),
                                        strides=(1, 2, 2, 1),
                                        padding='SAME')  # (?,16,16,128)

            self.res3_1 = _resblock_proj('cls_res3_1', self.pool2,
                                         self.res_w3_1, self.res_w3_2,
                                         self.proj_w3)
            self.res3_2 = _resblock_double('cls_res3_2', self.res3_1,
                                           self.res_w3_3, self.res_w3_4)
            self.pool3 = tf.nn.max_pool(self.res3_2,
                                        ksize=(1, 2, 2, 1),
                                        strides=(1, 2, 2, 1),
                                        padding='SAME')  # (?,8,8,256)

            self.conv_last = tf.nn.relu(
                tf.nn.conv2d(self.pool3,
                             self.conv_last_w1,
                             strides=(1, 1, 1, 1),
                             padding='SAME'))
            self.pool_last = tf.nn.avg_pool(self.conv_last,
                                            ksize=(1, 4, 4, 1),
                                            strides=(1, 4, 4, 1),
                                            padding='SAME')

            self.flat = tf.reshape(self.pool_last, shape=[-1, 2048])
            self.logits = tf.matmul(self.flat, self.fc_w1)  # (?, classes * 4)
コード例 #4
0
    def __init__(self, name, img, channel, classes):
        with tf.variable_scope(name):
            # feature weights
            self.proj_w1 = tf.get_variable(name='proj_w1',
                                           shape=[1, 1, channel, 64],
                                           initializer=he())
            self.res_w1 = tf.get_variable(name='res_w1',
                                          shape=[3, 3, channel, 64],
                                          initializer=he())

            self.proj_w2 = tf.get_variable(name='proj_w2',
                                           shape=[1, 1, 64, 128],
                                           initializer=he())
            self.res_w2 = tf.get_variable(name='res_w2',
                                          shape=[3, 3, 64, 128],
                                          initializer=he())

            self.proj_w3 = tf.get_variable(name='proj_w3',
                                           shape=[1, 1, 128, 256],
                                           initializer=he())
            self.res_w3 = tf.get_variable(name='res_w3',
                                          shape=[3, 3, 128, 256],
                                          initializer=he())

            self.proj_w4 = tf.get_variable(name='proj_w4',
                                           shape=[1, 1, 256, 512],
                                           initializer=he())
            self.res_w4 = tf.get_variable(name='res_w4',
                                          shape=[3, 3, 256, 512],
                                          initializer=he())

            self.proj_w5 = tf.get_variable(name='proj_w5',
                                           shape=[1, 1, 512, 1024],
                                           initializer=he())
            self.res_w5 = tf.get_variable(name='res_w5',
                                          shape=[3, 3, 512, 1024],
                                          initializer=he())

            self.proj_w6 = tf.get_variable(name='proj_w6',
                                           shape=[1, 1, 1024, 2048],
                                           initializer=he())
            self.res_w6 = tf.get_variable(name='res_w6',
                                          shape=[3, 3, 1024, 2048],
                                          initializer=he())

            self.proj_w7 = tf.get_variable(name='proj_w7',
                                           shape=[1, 1, 2048, 4096],
                                           initializer=he())
            self.res_w7 = tf.get_variable(name='res_w7',
                                          shape=[3, 3, 2048, 4096],
                                          initializer=he())

            self.fc_w = tf.get_variable(name='fc_w',
                                        shape=[4096, classes],
                                        initializer=he())

            # common graphs
            self.res1_1 = ops.resblock_single('res1_1', img, self.res_w1,
                                              self.proj_w1)
            self.pool1 = tf.nn.max_pool(self.res1_1,
                                        ksize=(1, 2, 2, 1),
                                        strides=(1, 2, 2, 1),
                                        padding='SAME')  # 40,40,64
            self.res2_1 = ops.resblock_single('res2_1', self.pool1,
                                              self.res_w2, self.proj_w2)
            self.pool2 = tf.nn.max_pool(self.res2_1,
                                        ksize=(1, 2, 2, 1),
                                        strides=(1, 2, 2, 1),
                                        padding='SAME')  # 20,20,128
            self.res3_1 = ops.resblock_single('res3_1', self.pool2,
                                              self.res_w3, self.proj_w3)
            self.pool3 = tf.nn.max_pool(self.res3_1,
                                        ksize=(1, 2, 2, 1),
                                        strides=(1, 2, 2, 1),
                                        padding='SAME')  # 10,10,256
            self.res4_1 = ops.resblock_single('res4_1', self.pool3,
                                              self.res_w4, self.proj_w4)
            self.pool4 = tf.nn.max_pool(self.res4_1,
                                        ksize=(1, 2, 2, 1),
                                        strides=(1, 2, 2, 1),
                                        padding='SAME')  # 5,5,512
            self.res5_1 = ops.resblock_single('res5_1', self.pool4,
                                              self.res_w5, self.proj_w5)
            self.pool5 = tf.nn.max_pool(self.res5_1,
                                        ksize=(1, 2, 2, 1),
                                        strides=(1, 2, 2, 1),
                                        padding='SAME')  # 3,3,1024
            self.res6_1 = ops.resblock_single('res6_1', self.pool5,
                                              self.res_w6, self.proj_w6)
            self.pool6 = tf.nn.max_pool(self.res6_1,
                                        ksize=(1, 2, 2, 1),
                                        strides=(1, 2, 2, 1),
                                        padding='SAME')  # 2,2,2048
            self.res7_1 = ops.resblock_single('res7_1', self.pool6,
                                              self.res_w7, self.proj_w7)
            self.pool7 = tf.nn.avg_pool(self.res7_1,
                                        ksize=(1, 2, 2, 1),
                                        strides=(1, 2, 2, 1),
                                        padding='SAME')  # 1,1,4096
            self.logits = tf.nn.softmax(
                tf.matmul(tf.squeeze(self.pool7), self.fc_w))