使用TensorFlow Hub迁移学习

    TensorFlow Hub是一种共享预训练模型组件的方法。参见TensorFlow Module Hub,获得预训练模型的可搜索列表。本教程演示了:

    1. 如何使用TensorFlow Hub Keras。
    2. 如何使用TensorFlow Hub进行图像分类。
    3. 如何做简单的迁移学习。

    设置

    图书馆(keras)图书馆(tfhub)

    一个ImageNet分类器

    下载分类器

    使用layer_hub加载一个移动网络,并将其包装为keras层。任何TensorFlow 2兼容的图像分类器URLtfhub.dev将在这里工作。

    解码预测

    我们有预测的类ID,获取ImageNet标签,并解码预测:

    img% > %as.array()% > %as.raster()% > %情节()标题粘贴预测:“, imagenet_labels predicted_class+1)))

    学习简单的转移

    使用TF Hub可以很容易地重新训练模型的顶层来识别数据集中的类。

    花< -::“https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz”“flower_photos”

    将这些数据加载到模型中的最简单方法是使用image_data_generator

    TensorFlow Hub的所有图像模块都期望在[0,1]范围内的浮点输入。使用image_data_generator参数来实现此功能。

    图像大小将在后面处理。

    image_generator < -image_data_generator重新调节=1/255image_data < -花(1% > %目录名()% > %目录名()% > %flow_images_from_directory(image_generatortarget_size =image_shape [-3.])
    ##找到了属于5类的3670张图片。

    结果对象是一个返回image_batch、label_batch对的迭代器。我们可以用iter_next网状

    str(网状::iter_next(image_data))
    ## $: num[1:32, 1:224, 1:224, 1:3] 0.145 0.431 0.431 0.863 1…## $: num[1:32, 1:5] 1 0 0 0 0 0 0 0 0 0 0…

    在一批图像上运行分类器

    现在在图像批处理上运行分类器。

    票面价值mfcol =c48),3月=代表14),oma =代表0.24))image_batch [[1]]% > %purrr::array_tree1% > %purrr::set_names(predicted_classnames)% > %purrr::地图(as.raster)% > %purrr::我走情节(方式);标题(.y)})

    看到LICENSE.txt图像属性文件。

    结果远非完美,但考虑到这些不是训练模型的类(除了“雏菊”),这是合理的。

    下载无头模型

    TensorFlow Hub还分发没有顶层分类层的模型。这些可以用来轻松地进行迁移学习。

    任何Tensorflow 2兼容图像特征向量来自tfhub.dev的URL将在这里工作。

    创建特征提取器。

    它为每张图像返回一个1280长度的向量:

    # #特遣部队。张量(##[[0.13427277 0.16856195 0.283491…0.00557477 0。0.8134863[0.00754159 0.49517953 0.18708485…]0.01621983 0。0.) # #(0。0.60116017 0。…0.0.05334444 0.00277256] ##… ## [0.6140208 1.3715637 0. ... 0.02907389 0.11318099 0.12228318] ## [1.2423071 1.0235544 0.170658 ... 0.51680547 0. 0. ] ## [0.5452022 0.2789958 0.16163555 ... 0.1076004 0.01267634 0. ]], shape=(32, 1280), dtype=float32)

    冻结特征提取器层中的变量,以便训练只修改新的分类器层。

    freeze_weights(feature_extractor_layer)

    附加分类标头

    现在,让我们使用特征提取层创建一个顺序模型,并添加一个新的分类层。

    模型< -keras_model_sequential列表feature_extractor_layer,layer_dense单位=image_datanum_classes,激活=“softmax”))总结(模型)
    # #模型:“顺序 " ## ___________________________________________________________________________ ## 输出层(类型)的形状参数  # ## =========================================================================== ## keras_layer_1 (KerasLayer)(没有,1280)2257984  ## ___________________________________________________________________________ ## 密度(密度)6405(没有,5)  ## =========================================================================== ## 总参数:2264389 # #可训练的参数:6405 # # Non-trainable params: 2257984  ## ___________________________________________________________________________

    火车模型

    使用compile来配置训练过程:

    现在使用的适合方法训练模型。

    为了使这个例子保持短小,只需2个epoch。

    ##纪元1/2 ## 114/114 - 255s -损耗:0.6656 -精度:0.7567 ##纪元2/2 ## 114/114 - 250s -损耗:0.3308 -精度:0.8931

    现在,在经过了一些训练迭代之后,我们已经可以看到模型在任务上取得了进展。

    然后我们可以验证这些预测:

    image_batch < -网状::iter_next(image_data)预测< -predict_classes(模型、image_batch [[1]])票面价值mfcol =c48),3月=代表14),oma =代表0.24))image_batch [[1]]% > %purrr::array_tree1% > %purrr::set_names的名字(image_dataclass_indices)[预测+1])% > %purrr::地图(as.raster)% > %purrr::我走情节(方式);标题(.y)})

    导出模型

    现在你已经训练了模型,将它导出为一个已保存的模型:

    save_model_tf(模型中,“mymodel /”include_optimizer =

    现在确认我们可以重新加载它,它仍然会给出相同的结果:

    model_等< -load_model_tf“mymodel /”
    # # [1]