迁移学习 迁移参数_迁移学习简介

本文介绍了迁移学习的概念,重点讨论了如何在不同任务间迁移模型参数,以提升机器学习算法的性能。通过实例展示了如何在Python和Java中实现这一技术。
摘要由CSDN通过智能技术生成

迁移学习 迁移参数

介绍 (Introduction)

We as humans have the ability to transfer the knowledge gained in one task to use it another task, the easier is the task the easier is to utilize the knowledge. Some simple examples would be:

作为人类,我们有能力转移在一项任务中获得的知识,以将其用于另一项任务,任务越容易,知识的利用就越容易。 一些简单的例子是:

  • Know Math and Statistics → Learn Machine Learning

    知道数学和统计学→学习机器学习
  • Know how to ride a bicycle → Learn how to ride a Motor Bike

    知道如何骑自行车→了解如何骑摩托车

Most of the Machine Learning and deep learning algorithms so far are designed to focus on solving specific tasks. These algorithms are again rebuilt if any distribution changes and it would be hard to rebuilt and retrain it as it requires computational power and a lot of time.

到目前为止,大多数机器学习和深度学习算法都旨在解决特定任务。 如果分布发生变化,这些算法将再次被重建,并且由于需要计算能力和大量时间,将很难重建和重新训练它们。

Transfer learning is all about how to use a pre-trained network and apply it to our custom task, transferring what it learned from previous tasks.

转移学习就是关于如何使用预先训练的网络并将其应用于我们的自定义任务,以及转移从先前任务中学到的知识。

Transfer learning is where we take architecture like VGG 16 and ResNet which are the result of many architectures and extensive hyperparameter tuning based on what they have already learned we apply that knowledge to a new task/model instead of starting from scratch which is called Transfer learning.

迁移学习是我们采用VGG 16和ResNet之类的架构的地方,它们是许多架构和基于他们已经学到的内容进行大量超参数调整的结果,我们将该知识应用于新的任务/模型,而不是从头开始,这被称为迁移学习。 。

Some of the Transfer Learning models include:

一些转移学习模型包括:

  • Xception

    Xception
  • VGG16

    VGG16
  • VGG19

    VGG19
  • Resnet, ResnetV2

    Resnet,ResnetV2
  • InceptionV3

    盗梦空间V3
  • MobileNet

    移动网

使用转移学习实施医学应用 (Implementing Medical Application Using Transfer Learning)

In this application, we will detect whether the person is having pneumonia or not. We use a Kaggle dataset for this classification. The link to the dataset and code is given below.

在此应用程序中,我们将检测该人是否患有肺炎。 我们使用Kaggle数据集进行此分类。 到数据集和代码的链接如下。

Dataset Link:

数据集链接:

Code Link:

代码链接:

The Dataset consists of a Train set and Test set with subfolders as normal and pneumonia. Pneumonia has the chest x-ray images of people who are suffering from pneumonia and the normal folder has images who are normal i.e free from lung disease.

数据集由训练集和测试集组成,子集为正常和肺炎。 肺炎包含患有肺炎的人的胸部X射线照片,而正常文件夹则包含正常的图像,即没有肺部疾病。

安装Tensorflow (Installing Tensorflow)

You can either use Google Colab if your PC or laptop doesn’t have a GPU or else you can use Jupyter Notebook. If you use your system, upgrade pip and then install TensorFlow as follows

如果您的PC或笔记本电脑没有GPU,则可以使用Google Colab,也可以使用Jupyter Notebook。 如果您使用系统,请升级pip然后按以下方式安装TensorFlow

Image for post
tensorflow.org
tensorflow.org

导入库 (Import Libraries)

调整图像大小 (Re-Size Images)

Here, we will be resizing all the images to 224*224 because we use the VGG16 model and it accepts images of size 224*224.

在这里,我们将所有图像的大小调整为224 * 224,因为我们使用的是VGG16模型,它接受尺寸为224 * 224的图像。

训练和测试路径 (Train and Test path)

We will specify the train and test path fro training.

我们将指定培训的培训路径和测试路径。

导入VGG16 (Importing VGG16)

Here, we will import the VGG16 model weights for our application. We should declare an image size to the model, which we have done in the previous step, argument 3 represents that the image will accept RGB image i.e the color image. To train our model, we use imagenet weights and include_top = False means it will remove the last layers from the model.

在这里,我们将为我们的应用程序导入VGG16模型权重。 我们应该为模型声明一个图像大小 ,这是我们在上一步中完成的,参数3表示该图像将接受RGB图像,即彩色图像。 为了训练我们的模型,我们使用imagenet权重,并且include_top = False表示它将从模型中删除最后一层。

训练层 (Training Layers)

Models like VGG16, VGG19, Resnet, and others have been trained on thousands and thousands of images and these weights are used to classify thousands of classes so we use these models weights for classification of our model, so we do not need to train the model again.

VGG16,VGG19,Resnet等模型已经在成千上万张图像上进行了训练,并且这些权重用于对数千个类别进行分类,因此我们将这些模型权重用于模型的分类,因此我们不需要训练模型再次。

类数 (Number of Classes)

we use glob to find out the number of classes in our model. The number of subfolders in our train folder represents the number of classes in our model.

我们使用glob找出模型中的类数。 训练文件夹中子文件夹的数量代表模型中类的数量。

展平 (Flattening)

Whatever we got as output from VGG16, we are going to flatten it and we removed the last layers from VGG16 so that we can keep our own output layer. we replace the last layer with the number of categories we have in our problem statement. We consider softmax as our activation function and we are appending it to x.

无论从VGG16获得什么输出,我们都要对其进行展平,然后从VGG16中删除最后一层,以便保留自己的输出层。 我们用问题陈述中的类别数替换最后一层。 我们将softmax视为我们的激活函数,并将其附加到x上。

模型 (Model)

We will wrap it into a model where the input refers to what we have from VGG16 and output refers to the output layer that we created in the previous step.

我们将其包装到一个模型中,其中输入引用我们从VGG16中获得的内容,输出引用我们在上一步中创建的输出层。

summary of model
模型总结
Image for post

The above picture is the summary of our model and in the dense layer, we have two nodes because of two different categories we have pneumonia and normal.

上图是我们模型的摘要,在致密层中,由于有肺炎正常 肺炎两种不同的类别,我们有两个结节

编译 (Compile)

We compile our model using categoricaa_cross_entropy as loss, adam optimizer, and accuracy as metrics. If you are not aware of what these terms mean, I will mention the link to my blogs at the last of the article where I explain all these terms in clear.

我们使用categoricaa_cross_entropy作为损失,adam优化器和准确性作为度量标准来编译模型。 如果您不了解这些术语的含义,那么我将在文章的最后提到我的博客的链接,在其中我将对所有这些术语进行清楚的解释。

预处理 (Pre-Processing)

We will apply some transformations on the training images to avoid overfitting, if we do not perform we get a large difference between the accuracy of the training set and the test set.

我们将对训练图像进行一些变换以避免过度拟合,如果不执行,则会在训练集和测试集的准确性之间产生很大差异。

We perform some geometrical transformations like flipping the image horizontally, flipping vertically, zoom in, zoom out and many more can be performed, we apply it so that our model won’t over learn our training images. We perform the above methods using the ImageDataGenerator class.

我们执行一些几何变换,例如水平翻转图像,垂直翻转,放大,缩小,并且可以执行更多操作,我们应用它,以便我们的模型不会过度学习训练图像。 我们使用ImageDataGenerator类执行上述方法。

We don’t apply transformations for the test set because we only use them to evaluate, the only task to do with our test set is to rescale the images because in the training part we defined a target size for the image that could be fed into the network.

我们不对测试集应用变换,因为我们仅使用变换来进行评估,与测试集有关的唯一任务是重新缩放图像,因为在训练部分中,我们为图像定义了目标尺寸,可以将其输入网络。

flow_from_directory will connect the Image Augmentation process to our training set. We need to mention the path of our training set. Target size is the size of the image that should be fed into the Neural Network. Batch size is defined as the no of images in a batch, the class mode would be categorical as we only have two outputs.

flow_from_directory将把“图像增强”过程连接到我们的训练集中。 我们需要提及我们的培训课程。 目标尺寸是应馈入神经网络的图像尺寸。 批处理大小定义为批处理中没有图像, 分类模式将是分类的,因为我们只有两个输出。

Now we define the test set that imports our test images from the directory. We do define the batch size, target size, and class mode as same mentioned in the training set.

现在,我们定义了从目录导入测试图像的测试集。 我们确实定义了批次大小,目标大小和班级模式,与训练集中提到的相同。

拟合模型 (Fit the Model)

We will fit our model and declare the number of epochs as five, steps per epoch would be the length of the training set and validation steps would be the length of the test set.

我们将拟合模型并声明时期数为五个,每个时期的步长为训练集的长度,而验证步骤为测试集的长度。

Image for post

That's great, we achieved an accuracy of around 97.7% and validation accuracy of 91.5%, that’s the power of transfer learning. Hope you enjoyed this tutorial on Transfer Learning. If you would like to know about how Artificial Neural Networks and Convolutional Neural Networks work along with an application, check out my blogs below:

太好了,我们达到了约97.7%的准确度和91.5%的验证准确度,这就是迁移学习的力量。 希望您喜欢本教程有关转移学习。 如果您想了解人工神经网络和卷积神经网络如何与应用程序一起工作,请查看以下我的博客:

翻译自: https://towardsdatascience.com/introduction-to-transfer-learning-c59f6f27e3e

迁移学习 迁移参数

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值