使用Turi Create训练核心ML模型以对犬种进行分类

In this tutorial, you’ll learn how to train a custom dog-breed classification Core ML model to use in your iOS and macOS apps. Your Core ML model will be able to distinguish between five different breeds by the end of this tutorial!

在本教程中,您将学习如何训练自定义的狗品种分类Core ML模型,以便在您的iOS和macOS应用中使用。 在本教程结束时,您的Core ML模型将能够区分五个不同的品种!

You may recall that Apple acquired the machine learning and artificial intelligence startup Turi a few years ago for upwards of $200M; it offers powerful tools to create advanced machine learning models in a short amount of time.

您可能还记得,几年前,苹果以超过2亿美元的价格收购了机器学习和人工智能初创公司Turi; 它提供了强大的工具,可在短时间内创建高级机器学习模型。

In this tutorial, you’ll be learning to install Turi Create on your Mac, create a Python script, and use that script to train a Core ML model that you can drag directly into your Xcode projects and quickly implement in your apps.

在本教程中,您将学习如何在Mac上安装Turi Create,创建Python脚本并使用该脚本来训练Core ML模型,您可以将其直接拖动到Xcode项目中并在应用中快速实现。

入门 (Getting Started)

Before we get started with the actual machine learning part of it, let’s get the installation of Turi and Python out of the way first — and, of course, you’ll need to make sure your hardware and software meet Turi’s requirements.

在开始其中的实际机器学习部分之前,让我们首先不进行Turi和Python的安装-当然,您需要确保您的硬件和软件满足Turi的要求。

要求 (Requirements)

As with any software you install, Turi Create has some specific requirements, which can be found on their official GitHub page.

与您安装的所有软件一样,Turi Create也有一些特定要求,可以在其官方GitHub页面上找到

Turi Create supports:

Turi Create支持:

  • macOS 10.12+

    macOS 10.12以上
  • Linux (with glibc 2.12+)

    Linux(含glibc 2.12+)
  • Windows 10 (via WSL)

    Windows 10(通过WSL)

Turi Create requires:

Turi Create要求:

  • Python 2.7, 3.5, 3.6

    Python 2.7、3.5、3.6
  • x86_64 architecture

    x86_64架构
  • At least 4 GB of RAM

    至少4 GB RAM

The bottom line is, as long as your Mac is reasonably new, you should be able to run Turi Create. If you’d like, you can follow along with another operating system; however, you may need to alter some steps for them to work.

底线是,只要您的Mac是相当新的,您就应该能够运行Turi Create。 如果愿意,您可以跟进其他操作系统。 但是,您可能需要更改一些步骤才能使其工作。

安装 (Installation)

Installing Turi Create is fairly simple, especially if you’re familiar with the command line. While you may choose to use a newer version of Python, I’ll be using Python 2.7 in this tutorial.

安装Turi Create非常简单,特别是如果您熟悉命令行的话。 尽管您可能选择使用更新版本的Python,但在本教程中我将使用Python 2.7。

In MacOS Mojave, Python 2.7 comes installed by default, so all you’d need to do is check the version. On your Mac, open Applications > Utilities > Terminal or simply search for it with they keyboard shortcut Command-Space.

在MacOS Mojave中,默认情况下安装了Python 2.7,因此您只需检查版本即可。 在Mac上,打开“ 应用程序”>“实用程序”>“终端”,或者使用键盘快捷键Command -Space来搜索它。

To check the version of Python on your Mac, enter:

要在Mac上检查Python版本,请输入:

$ python - version

This will tell you the version of Python, and your console should look something like this:

这将告诉您Python的版本,您的控制台应如下所示:

If your version is not Python 2.7, or if it isn’t installed on your computer for some reason, you should install it at this link. If your output looks similar to mine, you’re ready to proceed.

如果您的版本不是Python 2.7,或者由于某种原因未安装在计算机上,则应在此链接上安装它。 如果您的输出与我的输出相似,则可以继续进行。

Note: Some people prefer to use a virtual machine to install Turi Create since that’s what Apple recommends. But to keep things simple, we’ll just be installing it directly.

注意:有些人喜欢使用虚拟机来安装Turi Create,因为这是Apple的建议。 但是为了简单起见,我们将直接安装它。

To install Turi Create, just enter the following in your Terminal window:

要安装Turi Create,只需在“终端”窗口中输入以下内容:

$ pip install turicreate

That’s all! Turi Create is successfully installed on your Mac, and it’s ready to use. You can now build classification, detection, regression, and other types of models.

就这样! Turi Create已成功安装在Mac上,可以使用了。 现在,您可以构建分类,检测,回归和其他类型的模型。

数据集 (Dataset)

For any machine learning model, you need a dataset. In this tutorial, you’ll be learning how to train a simple dog-breed classification model, which requires image classification. The data that I’ll be using comes from Stanford University’s Dogs Dataset.

对于任何机器学习模型,您都需要一个数据集。 在本教程中,您将学习如何训练一个简单的狗品种分类模型,该模型需要图像分类。 我将使用的数据来自斯坦福大学的Dogs Dataset

In order for Turi to be able to recognize the pre-classified images, you’ll need to organize them based on what they represent. For example, all of the images of golden retrievers would be in one folder, while all pictures of labradoodles would be in another.

为了使Turi能够识别预分类的图像,您需要根据它们代表的内容对其进行组织。 例如,金毛猎犬的所有图像都放在一个文件夹中,而拉布拉多犬的所有图片都放在另一个文件夹中。

For the sake of simplicity, we’ll just be using five breeds out of the hundreds in Stanford’s dataset, but you can use as many as you’d like. I’ve gone ahead and organized it for you and created a repository for it. If you choose to add more dog breeds, simply add more folders and name them as you wish.

为了简单起见,我们将只使用斯坦福大学数据集中数百个中的五个品种,但您可以根据需要使用任意数量。 我已经为您整理好了,并为它创建了一个存储库 。 如果您选择添加更多的犬种,只需添加更多文件夹并根据需要命名。

资料夹结构 (Folder Structure)

By now, you may have figured out that the way you arrange your dataset is critical to being able to train the model correctly — there’s no other way that Turi Create knows about what goes where. Take a moment now to organize yourself.

到现在为止,您可能已经知道,布置数据集的方式对于正确训练模型至关重要,而Turi Create别无其他方法可以知道去哪里。 现在花点时间整理自己。

This hierarchy diagram should explain everything, and you’ll need to get your folders in this order before continuing with this tutorial. If you want to change names or arrange things differently, you’ll need to make sure you make a note of this.

该层次结构图应解释所有内容,在继续本教程之前,您需要按此顺序获取文件夹。 如果要更改名称或以其他方式安排其他事情,则需要确保对此进行记录。

训练分类器 (Training the Classifier)

After you’ve finished setting up, you’re ready to dive into the meat of this tutorial — actually training your classifier. We’ll be working mostly in Python, but if you’ve never used Python before, that’s okay. I’ll explain each step as we go along, and if you have any questions, don’t hesitate to leave a comment below.

设置完成后,您就可以开始学习本教程的内容了-实际上是在训练您的分类器。 我们将主要使用Python进行工作,但是如果您以前从未使用过Python,那就可以了。 在我们进行的过程中,我将解释每个步骤,如果您有任何疑问,请随时在下面发表评论。

Python文件 (Python File)

First, we’ll need to have a place to put down our thoughts (that is, of course, in Python). If you already have an editor that supports Python, such as Atom or an integrated development environment such as PyCharm, you can use them to create a blank file called dog_breeds.py.

首先,我们需要有一个地方放下思想(当然,在Python中)。 如果您已经拥有支持Python的编辑器(例如Atom)或集成开发环境(例如PyCharm) ,则可以使用它们来创建一个名为dog_breeds.py的空白文件。

If you prefer the more developer-y route, like I do, you can use the Terminal to do the same thing. You’ll need to create this file inside your ml_classifier folder, alongside the images folder so that your hierarchy looks like this:

如果您喜欢像我这样的更具开发性的路线,则可以使用终端执行相同的操作。 您需要在ml_classifier文件夹中以及images文件夹旁边创建此文件,以便您的层次结构如下所示:

To create a new file, first enter into the target directory:

要创建一个新文件,首先进入目标目录:

$ cd ml_classifier

Then, create a new file named dog_breeds.py.

然后,创建一个名为dog_breeds.py的新文件。

$ touch dog_breeds.py

Voilà! Your folders, files, and images are all where they need to be, and you’re ready to continue with the next step. We’ll be using Xcode to open our file, so make sure you have it installed and up-to-date.

瞧! 您的文件夹,文件和图像都在需要的位置,您已准备好继续下一步。 我们将使用Xcode打开我们的文件,因此请确保您已安装该文件并保持最新。

加载数据集图像 (Loading Dataset Images)

Finally, it’s time to begin telling Turi what it needs to do via the Python file we just created. If you double-click the file, it should open up by default in Xcode, if you have it installed. If not, you can also use another editor or a Python IDE.

最后,是时候开始通过我们刚刚创建的Python文件告诉Turi它需要做什么。 如果双击该文件,则默认情况下会在Xcode中打开它(如果已安装)。 如果没有,您也可以使用其他编辑器或Python IDE。

1.导入框架 (1. Import Frameworks)
import turicreate

At the top of the file, you’ll need to import the Turi Create framework. If you want, you can create a name for reference by adding as <your name>. For example, if you wanted to refer to it as tc in your code, you could write:

在文件的顶部,您需要导入Turi Create框架。 如果需要,可以通过添加as <your na的名字>来创建参考名称。 例如,如果您想在代码中将it称为tc,则可以编写:

import turicreate as tc

This would allow you to call it tc instead of writing out turicreate. In this tutorial, I’ll be using the full version, calling it turicreate to reduce ambiguity.

这将允许您将其称为tc而不是写出turicreate 。 在本教程中,我将使用完整版本,将其turicreate可以减少歧义。

You’ll also need to deal with folder names and other OS-related tasks in order to classify your images. This will require another Python library called os. To import it, simply add the following:

您还需要处理文件夹名称和其他与OS相关的任务,以便对图像进行分类。 这将需要另一个名为os Python库。 要导入它,只需添加以下内容:

import os
2.加载图像 (2. Loading Images)
data = turicreate.image_analysis.load_images("images/")

Here, we’re storing all of the images in our dataset into a variable called data. Since our dog_breeds.py file is in the same directory as the images folder, we can simply put “images/” as the path.

在这里,我们将数据集中的所有图像存储到名为data的变量中。 由于我们的dog_breeds.py文件与images文件夹位于同一目录中,因此我们可以简单地将“images/”作为路径。

3.定义标签 (3. Defining Labels)

Now that Turi Create has all of your images, you need to link the folder names to a label name. These label names are what will be returned in your Core ML model when it’s being used in an iOS or MacOS app.

现在,Turi Create已包含所有图像,您需要将文件夹名称链接到标签名称。 这些标签名称是在iOS或MacOS应用中使用Core ML模型时将返回的名称。

data["label"] = data["path"].apply(lambda path: os.path.basename(os.path.dirname(path)))

This allows you to map all of your folder names to a “label” name, which tells Turi Create that all of the images that are in the “cocker_spaniel” folder are indeed Cocker Spaniels, for example.

例如,这使您可以将所有文件夹名称映射到“标签”名称,这告诉Turi Create“ cocker_spaniel”文件夹中的所有图像确实都是可卡犬。

4.另存为SFrame (4. Save as SFrame)

In case you’re not familiar with an SFrame, in simple terms, it’s a dictionary of all of your data (in this case, an image) and all of the labels (in this case, the dog breed). Save your SFrame like this:

如果您不熟悉SFrame ,简单来说,它是所有数据(在这种情况下为图像)和所有标签(在这种情况下为狗的品种)的字典。 这样保存您的SFrame

data.save("dog_classifier.sframe")

This allows you to store your labeled images for use in the next step. This is a fairly standard data type in the machine learning industry.

这使您可以存储标记的图像以供下一步使用。 这是机器学习行业中相当标准的数据类型。

培训与测试 (Training and Testing)

After Turi Create has all of your labeled images in place, it’s time to enter the home stretch and finally train your model. We also need to split the data so that 80% is used for training, and 20% is saved for testing the model once it’s done training — we won’t have to test it manually.

在Turi Create将所有带标签的图像放置到位之后,是时候进入原始拉伸并最终训练模型了。 我们还需要拆分数据,以便将80%的数据用于训练,而将20%的数据用于模型训练后保存下来-我们不必手动对其进行测试。

1.加载SFrame (1. Loading SFrame)

Now, we need to load the SFrame we just created in the previous step. This is what we’ll use to split into testing and training data later.

现在,我们需要加载上一步中刚刚创建的SFrame。 这是我们稍后将用于拆分为测试和培训数据的内容。

data = turicreate.SFrame("dog_classifier.sframe")

This assigns the data variable, which is now of type SFrame to the SFrame that we saved in the previous step. Now, we’ll need to split up the data into testing and training data. As aforementioned, we’ll be doing an 80:20 split of testing to training data.

这会将data变量(现在是SFrame类型) SFrame给我们在上一步中保存的SFrame。 现在,我们需要将数据分为测试和培训数据。 如前所述,我们将对测试数据进行80:20的测试拆分。

2.分割数据 (2. Splitting Data)

It’s time to split the data. After your SFrame code, add the following:

现在该拆分数据了。 在您的SFrame代码之后,添加以下内容:

testing, training = data.random_split(0.8)

This code randomly splits the data 80–20 and assigns it to two variables, testing and training, respectively. Now, Turi will automatically test your model without you needing to manually supply test images and create an app — if you need to make adjustments, you won’t need to fully implement it first, and instead, you can do them right in your Python file.

此代码随机拆分数据80-20并将其分配给两个变量, testingtraining ,分别。 现在,Turi将自动测试您的模型,而无需手动提供测试图像并创建应用程序-如果需要进行调整,则无需先完全实现它,而可以在Python中直接进行操作文件。

3.培训,测试和导出 (3. Training, Testing, and Exporting)

Your hard work has finally paid off! In this line of Python code, you’ll just tell Turi Create to train your model, while specifying the architecture that you’d like to use.

您的辛苦终于得到回报! 在这行Python代码中,您只需告诉Turi Create训练模型,同时指定您要使用的架构。

classifier = turicreate.image_classifier.create(testing, target="label", model="resnet-50")

You’re simply telling Turi to use your testing data (specified earlier), and use them to predict the labels (based on the folder structure from before), while using resnet-50, which is one of the most accurate machine learning model architectures.

您只是告诉Turi使用testing数据(先前指定),并使用它们预测labels (基于之前的文件夹结构),同时使用resnet-50 ,这是最准确的机器学习模型架构之一。

To use your testing data and ensure that your model is accurate, add this:

要使用测试数据并确保模型正确,请添加以下内容:

testing = classifier.evaluate(training)print testing["accuracy"]

This uses the training data you specified and stores the results after testing in a variable called (you guessed it) testing. For your information, it prints out the accuracy, but you can print other things as well, given enough time on Turi Create’s APIs.

这将使用您指定的training数据,并将测试后的结果存储在一个名为(您猜对了) testing的变量中。 为了给您提供信息,它打印出了准确性,但是如果有足够的时间使用Turi Create的API,您也可以打印其他内容。

Last but not least, you can save your model right into your file system with this one-liner after you give it a useful name:

最后但并非最不重要的一点是,在为模型起了有用的名字后,您可以将模型直接保存到文件系统中:

classifier.save("dog_classifier.model")classifier.export_coreml("dog_classifier.mlmodel")

Of course, you can also save your model in other formats, but for this example, I’ve saved it as a Core ML model.

当然,您也可以将模型保存为其他格式,但是对于本示例,我将其保存为Core ML模型。

运行和输出 (Running and Output)

For all you iOS developers out there — no, this isn’t an Xcode project that keeps compiling automatically and complaining for errors. In order for the code you just wrote to execute, we’ll need to do it via the terminal.

对于所有iOS开发人员来说-不,这不是一个Xcode项目,它会一直自动编译并抱怨错误。 为了使您刚刚编写的代码得以执行,我们需要通过终端来执行。

运行Python文件 (Running the Python File)

Running the Python file is easy! Make sure you’re in the correct directory, and all you need to do is enter the following in your terminal window:

运行Python文件很容易! 确保您在正确的目录中,并且所需要做的就是在终端窗口中输入以下内容:

python dog_breeds.py
输出量 (Output)

After a couple of minutes of training, your images folder and dog_breeds.py file will be accompanied by an SFrame, a model folder, and a .mlmodel file, which is your Core ML model!

经过几分钟的训练,您的images文件夹和dog_breeds.py文件将伴随一个SFrame,一个模型文件夹和一个.mlmodel文件,这就是您的Core ML模型!

You’ll also be presented with output in your terminal window, which will look something like this:

您还将在终端窗口中看到输出,如下所示:

This gives you information about training and training accuracy, the amount of images processed, and other useful information, which you can use to analyze your model without ever even using it.

这为您提供了有关训练和训练准确性,已处理图像的数量以及其他有用信息的信息,您甚至可以不使用它们就可以分析模型。

结论 (Conclusion)

I hope you enjoyed reading this tutorial as much as I enjoyed making it! Here are some steps on where to go from here. If you want to learn how to use your Core ML model in an iOS app, check out another one of my tutorials:

希望您和我一样喜欢阅读本教程! 以下是从这里出发的一些步骤。 如果您想学习如何在iOS应用中使用Core ML模型,请查看我的另一本教程:

Get Started With Image Recognition in Core MLWith technological advances, we’re at the point where our devices can use their built-in cameras to accurately identify…code.tutsplus.com

核心ML中的图像识别入门随着 技术的进步,我们正处于设备可以使用其内置摄像头来准确识别……代码的地步 tutsplus.com

This tutorial will show you how to take your resulting dog_classifier.mlmodel model and implement it in a real-world iOS app. It will also teach you to parse a live video feed and take individual frames for image classification.

本教程将向您展示如何获取生成的dog_classifier.mlmodel模型并将其在真实的iOS应用中实现。 它还将教您解析实时视频供稿,并采用单独的帧进行图像分类。

If you have any questions or comments regarding this tutorial, don’t hesitate to ask them down in the comments section below! I’m always eager to hear feedback, questions, or how you used your knowledge from this tutorial.

如果您对本教程有任何疑问或意见,请随时在下面的评论部分中提问! 我一直很想听听反馈,问题或您如何使用本教程的知识。

支持我的工作很容易! (It’s easy to support my work!)

Be sure to smash that “clap” button as many times as you can, share this tutorial on social media, and follow me on Twitter.

请确保尽可能多地粉碎“拍手”按钮 ,在社交媒体上分享本教程 ,并在Twitter上关注我。

Vardhan Agrawal (@vhanagwal) | TwitterThe latest Tweets from Vardhan Agrawal (@vhanagwal). Completely self-taught #ios developer, #instructor, and human…

Vardhan Agrawal(@vhanagwal)| Twitter 来自Vardhan Agrawal(@vhanagwal)的最新推文。 完全自学成才的#ios开发人员,#instructor和人类…

翻译自: https://www.freecodecamp.org/news/how-to-train-a-core-ml-model-with-turi-create-to-classify-dog-breeds-bc1d0fa108b/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值