【TensorFlow2.0实战】基于TnesorFlow实现MobileNet V1

目录

了解 MobileNet V1网络结构

基于TensorFlow实现 MobileNet V1

基于 CIFAR-10数据训练网络

使用训练好的模型进行预测


了解 MobileNet V1网络结构

  • 轻量级卷积神经网络
  • 更少的参数、更小的计算量,却拥有不俗的性能
  • 深度可分离卷积

深度可分离卷积

我们先来看看正常的卷积:

输入为:12 × 12 × 3,卷积核为:5 × 5 × 3,我们得到一个 8 × 8 × 1 的输出。

而在空间可分离卷积中,卷积分成两步骤:深度卷积 逐点卷积

 深度卷积:

逐点卷积:

不难发现,不管是正常的卷积,还是空间可分离卷积,最后得到的结果都是一个 8 × 8 × 1 的输出。

为什么使用深度可分离卷积可以减少参数计算量呢?

基于TensorFlow实现 MobileNet V1

复现的操作对着上图写即可。

import tensorflow as tf
import numpy as np
import cv2

#标准卷积块
def conv_block(
    inputs,
    filters,
    kernel_size=(3,3),
    strides=(1,1)
):
    x = tf.keras.layers.Conv2D(filters, kernel_size=kernel_size, strides=strides, padding='same', use_bias=False)(inputs)
    x = tf.keras.layers.BatchNormalization()(x)
    
    return tf.keras.layers.ReLU(6.0)(x)
##深度可分离卷积块
def depthwise_conv_block(
    inputs,
    pointwise_conv_filters,
    strides=(1,1)
):
    x = tf.keras.layers.DepthwiseConv2D((3, 3), padding='same', strides=strides, use_bias=False)(inputs)
    x = tf.keras.layers.BatchNormalization()(x)
    x = tf.keras.layers.ReLU(6.0)(x)
    
    ###深度卷积到此结束
    
    ###下面是逐点卷积
    x = tf.keras.layers.Conv2D(pointwise_conv_filters, kernel_size=(1,1), padding='same', use_bias=False)(x)
    x = tf.keras.layers.BatchNormalization()(x)
    
    return tf.keras.layers.ReLU(6.0)(x)

#mobile_net
def mobilenet_v1(
    inputs,
    classes
):
    ##特征提取层
    x = conv_block(inputs, 32, strides=(2,2))
    x = depthwise_conv_block(x, 64)
    x = depthwise_conv_block(x, 128, strides=(2,2))
    x = depthwise_conv_block(x, 128)
    x = depthwise_conv_block(x, 256, strides=(2,2))
    x = depthwise_conv_block(x, 256)
    x = depthwise_conv_block(x, 512, strides=(2,2))
    x = depthwise_conv_block(x, 512)
    x = depthwise_conv_block(x, 512)
    x = depthwise_conv_block(x, 512)
    x = depthwise_conv_block(x, 512)
    x = depthwise_conv_block(x, 512)
    x = depthwise_conv_block(x, 1024, strides=(2,2))
    x = depthwise_conv_block(x, 1024)
    
    ##全局池化
    x = tf.keras.layers.GlobalAveragePooling2D()(x)
    ##全连接层
    pred = tf.keras.layers.Dense(classes, activation='softmax')(x)
    
    return pred


##模型实例化
inputs = tf.keras.Input(shape=(32,32,3))
model = tf.keras.Model(inputs=inputs, outputs=mobilenet_v1(inputs, 10))

基于 CIFAR-10数据训练网络

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()

x_train.shape

##标签数据变成one_hot矩阵
x_train = x_train / 255.0
y_train = tf.keras.utils.to_categorical(y_train, 10)

model.compile(
    loss='categorical_crossentropy',
    optimizer='adam',
    metrics=['categorical_crossentropy','Recall', 'AUC']
)

#训练
model.fit(x_train, y_train, batch_size=10, epochs=1)

#保存
model.save('mobilenet_v1_cifar.h5')

使用训练好的模型进行预测

model = tf.keras.models.load_model('mobilenet_v1_cifar.h5')
model.summary()

##路径中要放入一张ship图像测试
img = cv2.imread('ship.png', 1) / 255.0
img = np.expand.dims(image, 0)

##预测
pred = model.predict(img)
pred 

 

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 很抱歉,TensorFlow 2.0 中已经删除了 `tensorflow.contrib` 模块,因此不能直接导入 `tensorflow.contrib.learn`。不过,您可以使用 `TensorFlow 2.0` 中内置的 `tf.keras` 模块,或者使用 `TensorFlow Hub` 中的预训练模型。 ### 回答2: 要导入tensorflow.contrib.learn,您需要使用tensorflow 2.0的兼容性模块tf.compat.v1。在TensorFlow 2.0中,tf.contrib模块已被移除。然而,通过tf.compat.v1模块,您仍然可以使用一些tensorflow.contrib模块中的功能。 您可以按照以下步骤来导入tensorflow.contrib.learn: 1. 导入所需的模块: ```python import tensorflow.compat.v1 as tf from tensorflow.compat.v1 import contrib ``` 2. 启用兼容性模式: ```python tf.disable_v2_behavior() ``` 3. 现在您可以使用tf.contrib.learn及其功能: ```python contrib.learn.Estimator(...) ``` 注意:虽然这种方法使您能够导入tensorflow.contrib.learn,但由于tf.compat.v1模块是为了向后兼容而设计的,因此它可能在将来的版本中被删除。因此,最好尽量使用tensorflow 2.0的原生API。如果您使用tensorflow.contrib.learn的功能非常重要,您可以考虑使用旧版本的tensorflow(如tensorflow 1.15)来支持它。 ### 回答3: 在TensorFlow 2.0中,已经不再支持`tensorflow.contrib.learn`这个模块。`tensorflow.contrib`是一个容纳实验性、不太稳定或较少使用的功能和功能组件的命名空间,而且在TensorFlow 1.X版本中是存在的。在TensorFlow 2.0中,TensorFlow团队已经将这些组件整合到了其他模块中,或者将它们作为独立的项目进行维护。因此,如果你想在TensorFlow 2.0中使用`tensorflow.contrib.learn`,你将无法直接导入它。 如果你仍然想使用类似于`tensorflow.contrib.learn`的某些功能,可以考虑以下方法: 1. 使用TensorFlow 2.0官方文档中提供的迁移指南,查找替代`tensorflow.contrib.learn`的功能或模块。官方文档通常会提供有关如何将旧版本的代码迁移到TensorFlow 2.0的详细说明。 2. 如果你只是需要用到一些机器学习算法,你可以考虑使用`scikit-learn`这个Python库。它是一个专门用于机器学习的库,提供了丰富的算法和工具,可以与TensorFlow 2.0进行结合使用。 总之,在TensorFlow 2.0中,将不再直接支持导入`tensorflow.contrib.learn`。如果你有特定的需求,需要找到替代的方法来实现你的目标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

见见大魔王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值