如何使用 TF Hub 进行 Tensorflow 迁移学习,下载图像网络分类器?

tensorflowserver side programmingprogramming

可以使用 TF Hub 进行 Tensorflow 迁移学习,使用"Sequential"模型下载图像网络分类器。分类器模型使用 google url 指定。此模型指定为"KerasLayer"方法的参数。

阅读更多: 什么是 TensorFlow,以及 Keras 如何与 TensorFlow 配合使用来创建神经网络?

包含至少一个层的神经网络称为卷积层。我们可以使用卷积神经网络构建学习模型。 

我们使用 Google Colaboratory 运行以下代码。Google Colab 或 Colaboratory 有助于在浏览器上运行 Python 代码,无需任何配置,并可免费访问 GPU(图形处理单元)。Colaboratory 建立在 Jupyter Notebook 之上。

图像分类迁移学习背后的直觉是,如果在大型通用数据集上训练模型,则该模型可有效地用作视觉世界的通用模型。它会学习特征图,这意味着用户不必从头开始在大型数据集上训练大型模型。

TensorFlow Hub 是一个包含预训练 TensorFlow 模型的存储库。TensorFlow 可用于微调学习模型。

我们将了解如何将 TensorFlow Hub 中的模型与 tf.keras 结合使用,使用 TensorFlow Hub 中的图像分类模型。完成此操作后,可以执行迁移学习以微调自定义图像类别的模型。这是通过使用预训练的分类器模型来获取图像并预测其内容来完成的。无需任何培训即可完成此操作。

示例

import numpy as np
import time
import PIL.Image as Image
import matplotlib.pylab as plt
import tensorflow as tf
import tensorflow_hub as hub
classifier_model ="https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4"IMAGE_SHAPE = (224, 224)
print("Creating a sequential layer")
classifier = tf.keras.Sequential([
   hub.KerasLayer(classifier_model, input_shape=IMAGE_SHAPE+(3,))
])

代码来源 −https://www.tensorflow.org/tutorials/images/transfer_learning_with_hub

输出

Creating a sequential layer

解释

  • 已导入所需包。
  • 已创建顺序层。

相关文章