diff --git a/spp b/spp new file mode 100644 index 0000000000000000000000000000000000000000..6b6b83215329f3835dab8fc31671b146fc65bc59 --- /dev/null +++ b/spp @@ -0,0 +1,78 @@ +import tensorflow as tf +class SpatialPyramidPooling(tf.keras.layers.Layer): + def __init__(self, pyramid_levels): + super(SpatialPyramidPooling, self).__init__() + self.pyramid_levels = pyramid_levels + + def call(self, inputs): + # 获取输入特征图的尺寸 + _, h, w, c = inputs.get_shape().as_list() + # 初始化一个列表来存储不同金字塔级别的池化结果 + pooled_features = [] + for level in self.pyramid_levels: + # 计算每个金字塔级别的池化窗口大小 + ksize = [h // level, w // level] + # 对每个级别应用最大池化 + pooled = tf.nn.max_pool2d(inputs, ksize=ksize, strides=ksize, padding='VALID') + pooled_features.append(pooled) + # 沿着通道维度拼接所有池化结果 + return tf.concat(pooled_features, axis=-1) +class Model(): + def build_network(self, training_options, image, drop_rate, labels): + with tf.variable_scope('CNN'): + # 卷积卷积卷积卷积 + with tf.variable_scope('hidden1'): + hidden1 = self.cnn(image, 32, kernel_size=[3, 3]) + with tf.variable_scope('hidden2'): + hidden2 = self.cnn(hidden1, 64, kernel_size=[3, 3]) + with tf.variable_scope('hidden3'): + hidden3 = self.cnn(hidden2, 64, kernel_size=[3, 3]) + # with tf.variable_scope('hidden4'): + # hidden4 = self.cnn(hidden3, 100, kernel_size=[1, 1]) + # 更改一下形状,因为全连接神经网络需要2维数据的input + #flatten = tf.reshape(hidden3, [-1, 4 * 9 * 64]) # --> 5 * 2 * 160 = 1600 + # 添加SPP层 + with tf.variable_scope('spp_layer'): + spp = SpatialPyramidPooling(pyramid_levels=[1, 2, 4]) + spp_out = spp(hidden3) + + # SPP层的输出可能需要扁平化以匹配全连接层的输入要求 + flatten = tf.reshape(spp_out, [-1, c1 * c2 * c3]) # c1, c2, c3是SPP输出的尺寸 + + # 第一层全连接 + with tf.variable_scope('hidden5'): + dense = tf.layers.dense(flatten, 1024, activation=tf.nn.relu) + hidden5 = tf.layers.dropout(dense, rate=drop_rate) + # 第二层全连接 + with tf.variable_scope('hidden6'): + dense = tf.layers.dense(hidden5, 1024, activation=tf.nn.relu) + hidden6 = tf.layers.dropout(dense, rate=drop_rate) + # 第一个字符预测输出 + with tf.variable_scope('digit1'): + dense = tf.layers.dense(hidden6, units=36) + self.digit1 = dense + tf.summary.histogram('digit1', self.digit1) + # 第二个 + with tf.variable_scope('digit2'): + dense = tf.layers.dense(hidden6, units=36) + self.digit2 = dense + tf.summary.histogram('digit2', self.digit2) + # 第三个 + with tf.variable_scope('digit3'): + dense = tf.layers.dense(hidden6, units=36) + self.digit3 = dense + tf.summary.histogram('digit3', self.digit3) + # 第四个 + with tf.variable_scope('digit4'): + dense = tf.layers.dense(hidden6, units=36) + self.digit4 = dense + tf.summary.histogram('digit4', self.digit4) + + layer = dict( + digit1=self.digit1, + digit2=self.digit2, + digit3=self.digit3, + digit4=self.digit4 + ) + train = self.train(training_options, labels) + return layer, train \ No newline at end of file