示例#1
0
def transition_block(x,
                     filter_num,
                     training,
                     name,
                     keep_prob,
                     compression=1.0):
    """
    :param x:
    :param filter_num:
    :param training:
    :param name:
    :param compression:
    :param keep_prob:
    :return:
    """
    with tf.variable_scope(name):
        x = tf.layers.batch_normalization(x, training=training)
        x = tf.nn.relu(x)
        x = conv2d(x,
                   kernel_size=1,
                   stride=1,
                   filter_num=int(filter_num * compression),
                   padding="SAME",
                   name="conv")

        x = tf.nn.dropout(x, keep_prob=keep_prob)

        x = avg_pool(x,
                     kernel_size=2,
                     stride=2,
                     name="avg_pool",
                     padding="SAME")

        return x
    def __build_graph(self):
        """
        :return:
        """
        x = conv2d(self.__X, kernel_size=3, stride=1, filter_num=self.filter_num, padding="SAME", name="conv1")
        x = tf.layers.batch_normalization(x, training=self.__is_training)
        x = tf.nn.relu(x)

        for block_idx in range(self.dense_block_num - 1):
            dense_block_name = "dense_block" + str(block_idx + 1)
            transition_block_name = "transition_block" + str(block_idx + 1)
            x, filter_num = dense_block(x, self.layers_num[block_idx], self.filter_num,
                                        self.growth_rate, training=self.__is_training,
                                        name=dense_block_name, keep_prob=self.__keep_prob)
            x = transition_block(x, self.filter_num, training=self.__is_training,
                                 name=transition_block_name, keep_prob=self.__keep_prob,
                                 compression=self.compression)

            self.filter_num = int(self.filter_num * self.compression)
        dense_block_name = "dense_block" + str(self.dense_block_num)
        x = conv2d(x, kernel_size=3, stride=1, filter_num=self.filter_num, padding="VALID", name="conv2")
        x = tf.layers.batch_normalization(x, training=self.__is_training)
        x = tf.nn.relu(x)
        x, filter_num = dense_block(x, self.layers_num[-1], self.filter_num, self.growth_rate,
                                    training=self.__is_training, name=dense_block_name,
                                    keep_prob=self.__keep_prob)

        x = tf.layers.batch_normalization(x, training=self.__is_training)
        x = tf.nn.relu(x)
        x = conv2d(x, kernel_size=3, stride=1, filter_num=self.filter_num, padding="VALID", name="conv3")
        x = tf.layers.batch_normalization(x, training=self.__is_training)
        x = tf.nn.relu(x)
        self.deep_features = x
        pool_size = x.get_shape()[1]
        x = avg_pool(x, kernel_size=pool_size, stride=1, padding="VALID", name="avg_pool")

        input_dim = x.get_shape()[-1]
        x = tf.reshape(x, [-1, input_dim])

        x = fc_connect(x, self.class_num, name="fc_connect")
        x = tf.nn.softmax(x)
        return x