Keras如何与使用Python的预训练模型一起使用?

Tensorflow是Google提供的一种机器学习框架。它是一个开放源代码框架,与Python结合使用以实现算法,深度学习应用程序等等。它用于研究和生产目的。

Keras在希腊语中的意思是“号角”。Keras被开发为ONEIROS(开放式神经电子智能机器人操作系统)项目研究的一部分。Keras是使用Python编写的深度学习API。它是一个高级API,具有可帮助解决机器学习问题的高效接口。

它在Tensorflow框架之上运行。它旨在帮助快速进行实验。它提供了在开发和封装机器学习解决方案中必不可少的基本抽象和构建块。

它具有高度的可扩展性,并具有跨平台功能。这意味着Keras可以在TPU或GPU集群上运行。Keras模型也可以导出为在Web浏览器或手机中运行。

Keras已经存在于Tensorflow软件包中。可以使用下面的代码行进行访问。

import tensorflow
from tensorflow import keras

我们正在使用Google合作实验室来运行以下代码。Google Colab或Colaboratory可以帮助通过浏览器运行Python代码,并且需要零配置和对GPU(图形处理单元)的免费访问。合作已建立在Jupyter Notebook的基础上。以下是代码片段-

示例

print("A convolutional model with pre-trained weights is loaded")
base_model = keras.applications.Xception(
   weights='imagenet',
   include_top=False,
   pooling='avg')
print("This model is freezed")
base_model.trainable = False
print("A sequential model is used to add a trainable classifier on top of the base")
model = keras.Sequential([
   base_model,
   layers.Dense(1000),
])
print("Compile the model")
print("Fit the model to the test data")
model.compile(...)
model.fit(...)

代码信用-https://www.tensorflow.org/guide/keras/sequential_model

输出结果

A convolutional model with pre-trained weights is loaded
Downloading data from https://storage.googleapis.com/tensorflow/kerasapplications/xception/xception_weights_tf_dim_ordering_tf_kernels_notop.h583689472/83683744 [==============================] - 1s 0us/step
This model is freezed
A sequential model is used to add a trainable classifier on top of the base
Compile the model
Fit the model to the test data

解释

  • 可以使用顺序模型堆栈,以及预训练模型的帮助来初始化分类层。

  • 构建此模型后,将对其进行编译。

  • 一旦编译完成,该模型就可以适合训练数据。

猜你喜欢