MobileNet V1 代码

2020-03-04  本文已影响0人  晨光523152

上周看来MobileNet V1的文章,然后去找了找代码。

代码传送门:
https://github.com/calmisential/Basic_CNNs_TensorFlow2/blob/master/models/mobilenet_v1.py

用了这个代码之后我发现运行 model.summary()之后,看不见每一层 output_shape,所以稍微进行了下改变,

网络代码如下:

class MobileNetV1(tf.keras.Model):
    def __init__(self):
        super(MobileNetV1, self).__init__()
        self.conv1 = tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3),
                           strides=2,
                           padding="same")
        self.separable_conv_1 = tf.keras.layers.SeparableConv2D(filters=64,
                                    kernel_size=(3, 3),
                                    strides=1,
                                    padding="same")
        self.separable_conv_2 = tf.keras.layers.SeparableConv2D(filters=128,
                                    kernel_size=(3, 3),
                                    strides=2,
                                    padding="same")
        self.separable_conv_3 = tf.keras.layers.SeparableConv2D(filters=128,
                                    kernel_size=(3, 3),
                                    strides=1,
                                    padding="same")
        self.separable_conv_4 = tf.keras.layers.SeparableConv2D(filters=256,
                                    kernel_size=(3, 3),
                                    strides=2,
                                    padding="same")
        self.separable_conv_5 = tf.keras.layers.SeparableConv2D(filters=256,
                                    kernel_size=(3, 3),
                                    strides=1,
                                    padding="same")
        self.separable_conv_6 = tf.keras.layers.SeparableConv2D(filters=512,
                                    kernel_size=(3, 3),
                                    strides=2,
                                    padding="same")

        self.separable_conv_7 = tf.keras.layers.SeparableConv2D(filters=512,
                                    kernel_size=(3, 3),
                                    strides=1,
                                    padding="same")
        self.separable_conv_8 = tf.keras.layers.SeparableConv2D(filters=512,
                                    kernel_size=(3, 3),
                                    strides=1,
                                    padding="same")
        self.separable_conv_9 = tf.keras.layers.SeparableConv2D(filters=512,
                                    kernel_size=(3, 3),
                                    strides=1,
                                    padding="same")
        self.separable_conv_10 = tf.keras.layers.SeparableConv2D(filters=512,
                                    kernel_size=(3, 3),
                                    strides=1,
                                    padding="same")
        self.separable_conv_11 = tf.keras.layers.SeparableConv2D(filters=512,
                                    kernel_size=(3, 3),
                                    strides=1,
                                    padding="same")

        self.separable_conv_12 = tf.keras.layers.SeparableConv2D(filters=1024,
                                    kernel_size=(3, 3),
                                    strides=2,
                                    padding="same")
        self.separable_conv_13 = tf.keras.layers.SeparableConv2D(filters=1024,
                                    kernel_size=(3, 3),
                                    strides=1,
                                    padding="same")

        self.avg_pool = tf.keras.layers.AveragePooling2D(pool_size=(7, 7),
                                  strides=1)
        self.fc = tf.keras.layers.Dense(units=10,
                        activation=tf.keras.activations.softmax)

    def call(self, inputs, training=None, mask=None):
        x = self.conv1(inputs)
        x = self.separable_conv_1(x)
        x = self.separable_conv_2(x)
        x = self.separable_conv_3(x)
        x = self.separable_conv_4(x)
        x = self.separable_conv_5(x)
        x = self.separable_conv_6(x)
        x = self.separable_conv_7(x)
        x = self.separable_conv_8(x)
        x = self.separable_conv_9(x)
        x = self.separable_conv_10(x)
        x = self.separable_conv_11(x)
        x = self.separable_conv_12(x)
        x = self.separable_conv_13(x)

        x = self.avg_pool(x)
        x = self.fc(x)

        return x

    def model(self):
        x = tf.keras.layers.Input(shape=(224, 224, 3))
        return tf.keras.Model(inputs=[x], outputs=self.call(x))
sub = MobileNetV1()
sub.model().summary()
模型

参考资料:
https://github.com/calmisential/Basic_CNNs_TensorFlow2/blob/master/models/mobilenet_v1.py
https://stackoverflow.com/questions/55235212/model-summary-cant-print-output-shape-while-using-subclass-model

上一篇下一篇

猜你喜欢

热点阅读