如何使用 Tensorflow 对一批图像运行分类器?

tensorflowserver side programmingprogramming

使用‘classifier’类和‘predict’方法,使用 TensorFlow 对一批图像运行分类器。

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

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

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

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

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

我们正在使用 Google Colaboratory 来运行以下代码。Google Colab 或 Colaboratory 有助于在浏览器上运行 Python 代码,并且不需要任何配置,并且可以免费访问 GPU(图形处理单元)。 Colaboratory 是在 Jupyter Notebook 基础上构建的。

示例

print("Classifier is run on batch of images")
result_batch = classifier.predict(train_ds)
predicted_class_names = imagenet_labels[np.argmax(result_batch, axis=-1)]
print("The class names are predicted")
print(predicted_class_names)

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

输出

Classifier is run on batch of images
The class names are predicted
["yellow lady's slipper" 'daisy' 'sea urchin' ... 'vault' 'bee'
'barn spider']

解释

  • 分类器在一批图像上运行。
  • 预测的类名显示在控制台上。

相关文章