diff --git a/totrans/prac-dl-cld_13.yaml b/totrans/prac-dl-cld_13.yaml index feec264..58ed22c 100644 --- a/totrans/prac-dl-cld_13.yaml +++ b/totrans/prac-dl-cld_13.yaml @@ -4,6 +4,7 @@ prefs: - PREF_H1 type: TYPE_NORMAL + zh: 第13章。食物的Shazam:使用TensorFlow Lite和ML Kit开发Android应用程序 - en: After developing the viral Not Hotdog app (that we looked at in [Chapter 12](part0014.html#DB7S3-13fa565533764549a6f0ab7f11eed62b)), Jian-Yang was originally supposed to build a classifier to recognize all food in existence. In fact, the app was originally supposed to be called SeeFood—an @@ -15,15 +16,19 @@ id: totrans-1 prefs: [] type: TYPE_NORMAL + zh: 在开发了病毒性的Not Hotdog应用程序(我们在[第12章](part0014.html#DB7S3-13fa565533764549a6f0ab7f11eed62b)中看到)之后,建阳最初应该构建一个能识别所有食物的分类器。事实上,该应用最初应该被称为SeeFood——一款可以“看到”食物并立即识别的应用程序([图13-1](part0015.html#not_hotdog_app_listing_on_the_apple_app))。换句话说,这是“食物的Shazam”。然而,该应用太成功了,以至于被Periscope收购。他的投资者Erlich + Bachman的最初愿景仍未实现。在本章中,我们的任务是实现这个梦想。 - en: '![Not Hotdog app listing on the Apple App Store](../images/00282.jpeg)' id: totrans-2 prefs: [] type: TYPE_IMG + zh: '![在苹果应用商店上的Not Hotdog应用列表](../images/00282.jpeg)' - en: Figure 13-1\. Not Hotdog app listing on the Apple App Store id: totrans-3 prefs: - PREF_H6 type: TYPE_NORMAL + zh: 图13-1。在苹果应用商店上的Not Hotdog应用列表 - en: Where would such a feature be useful? For health nuts, it could look at a dish and provide the nutritional information, including the number of calories. Or, it could scan a few ingredients, and recommend a recipe based on them. Or, it @@ -32,46 +37,56 @@ id: totrans-4 prefs: [] type: TYPE_NORMAL + zh: 这样的功能在哪里会有用呢?对于健康狂热者,它可以查看一道菜并提供营养信息,包括卡路里数。或者,它可以扫描几种成分,并根据它们推荐食谱。或者,它甚至可以查看市场上的产品,并检查它是否含有任何被列入黑名单的成分,如特定过敏原。 - en: 'This is an interesting problem to solve for several reasons because it represents several challenges:' id: totrans-5 prefs: [] type: TYPE_NORMAL + zh: 这是一个有趣的问题,有几个原因需要解决,因为它代表了几个挑战: - en: Data collection challenge id: totrans-6 prefs: [] type: TYPE_NORMAL + zh: 数据收集挑战 - en: There are more than a hundred cuisines around the world, each with hundreds if not thousands of dishes. id: totrans-7 prefs: [] type: TYPE_NORMAL + zh: 世界上有一百多种美食,每种美食都有数百甚至数千种菜肴。 - en: Accuracy challenge id: totrans-8 prefs: [] type: TYPE_NORMAL + zh: 准确性挑战 - en: It should be right most of the time. id: totrans-9 prefs: [] type: TYPE_NORMAL + zh: 它应该大部分时间都是正确的。 - en: Performance challenge id: totrans-10 prefs: [] type: TYPE_NORMAL + zh: 性能挑战 - en: It should run near instantly. id: totrans-11 prefs: [] type: TYPE_NORMAL + zh: 它应该几乎立即运行。 - en: Platform challenge id: totrans-12 prefs: [] type: TYPE_NORMAL + zh: 平台挑战 - en: An iPhone app alone would be insufficient. A lot of users in developing countries use less powerful smartphones, particularly Android devices. Cross-platform development is a must. id: totrans-13 prefs: [] type: TYPE_NORMAL + zh: 仅有iPhone应用程序是不够的。许多发展中国家的用户使用性能较弱的智能手机,特别是Android设备。跨平台开发是必须的。 - en: Making a food classifier app for one cuisine is tricky enough. Imagine having to do that for every food in existence—and doing it on two platforms! An individual or a small team will quickly run into scaling issues trying to tackle this problem. @@ -80,17 +95,20 @@ id: totrans-14 prefs: [] type: TYPE_NORMAL + zh: 为一个美食分类器应用程序制作一个美食已经够棘手了。想象一下要为存在的每种食物做到这一点——并且在两个平台上都要做到!一个个人或一个小团队将很快遇到尝试解决这个问题的扩展问题。在本章中,我们以此示例作为动力,探索我们在[第11章](part0013.html#CCNA3-13fa565533764549a6f0ab7f11eed62b)中探讨的移动AI开发生命周期的不同部分。 - en: The material we explore here does not need to be limited to smartphones, either. We can apply our learnings beyond mobile to edge devices such as Google Coral and Raspberry Pi, which we discuss later in the book. id: totrans-15 prefs: [] type: TYPE_NORMAL + zh: 我们在这里探讨的材料不需要局限于智能手机。我们可以将我们的学习应用到移动以外的边缘设备,如Google Coral和Raspberry Pi,这些我们在本书后面讨论。 - en: The Life Cycle of a Food Classifier App id: totrans-16 prefs: - PREF_H1 type: TYPE_NORMAL + zh: 食品分类器应用程序的生命周期 - en: 'So, we want to build a global multicuisine, multiplatform food classifier. It sounds like a daunting task, but we can break it down into manageable steps. As in life, we first need to crawl, then walk, and then run. The following is @@ -98,36 +116,43 @@ id: totrans-17 prefs: [] type: TYPE_NORMAL + zh: 因此,我们希望构建一个全球多美食、多平台的食品分类器。听起来是一项艰巨的任务,但我们可以将其分解为可管理的步骤。就像生活中一样,我们首先需要爬行,然后才能行走,最后才能奔跑。以下是一个可能要考虑的潜在方法: - en: Collect a small initial set images for a single cuisine (e.g., Italian). id: totrans-18 prefs: - PREF_OL type: TYPE_NORMAL + zh: 为单一美食(例如意大利美食)收集一小组初始图像。 - en: Label these images with their corresponding dish identifiers (e.g., `margherita_pizza`). id: totrans-19 prefs: - PREF_OL type: TYPE_NORMAL + zh: 用相应的菜品标识符(例如`margherita_pizza`)标记这些图像。 - en: Train a classifier model. id: totrans-20 prefs: - PREF_OL type: TYPE_NORMAL + zh: 训练分类器模型。 - en: Convert the model to a mobile framework-compatible format (e.g., *.tflite*). id: totrans-21 prefs: - PREF_OL type: TYPE_NORMAL + zh: 将模型转换为移动框架兼容格式(例如*.tflite*)。 - en: Build a mobile app by integrating the model with a great UX. id: totrans-22 prefs: - PREF_OL type: TYPE_NORMAL + zh: 通过将模型与出色的用户体验集成来构建移动应用程序。 - en: Recruit alpha users and share the app with them. id: totrans-23 prefs: - PREF_OL type: TYPE_NORMAL + zh: 招募α用户并与他们分享应用程序。 - en: Collect detailed usage metrics along with feedback from active users, including camera frames (which tend to reflect real-world usage) and corresponding proxy labels (indicating whether the classification was right or wrong). @@ -135,12 +160,14 @@ prefs: - PREF_OL type: TYPE_NORMAL + zh: 收集详细的使用指标以及来自活跃用户的反馈,包括相机帧(倾向于反映真实世界的使用)和相应的代理标签(指示分类是正确还是错误)。 - en: Improve the model using the newly collected images as additional training data. This process needs to be iterative. id: totrans-25 prefs: - PREF_OL type: TYPE_NORMAL + zh: 使用新收集的图像作为额外的训练数据来改进模型。这个过程需要迭代。 - en: When the quality of the model meets the minimum quality bar, ship the app/feature to more/all users. Continue to monitor and improve the quality of the model for that cuisine. @@ -148,16 +175,19 @@ prefs: - PREF_OL type: TYPE_NORMAL + zh: 当模型的质量达到最低质量标准时,将应用程序/功能发布给更多/所有用户。继续监控和改进该美食的模型质量。 - en: Repeat these steps for each cuisine. id: totrans-27 prefs: - PREF_OL type: TYPE_NORMAL + zh: 为每种美食重复这些步骤。 - en: Tip id: totrans-28 prefs: - PREF_H6 type: TYPE_NORMAL + zh: 提示 - en: For step 7, we could alternatively integrate the feedback within the UX itself. For example, the app could show a ranked list of predictions (by probability) that are likely candidates for a given picture. If our model is doing well, the @@ -169,6 +199,7 @@ id: totrans-29 prefs: [] type: TYPE_NORMAL + zh: 对于第7步,我们可以选择在用户体验中集成反馈。例如,应用程序可以显示一个按概率排名的预测列表,这些预测是给定图片的可能候选项。如果我们的模型表现良好,用户应该大部分时间选择第一个选项。选择排名较低的预测基本上被视为错误的预测。在最坏的情况下,如果没有一个选项是正确的,允许用户手动添加一个新标签。这张照片,连同标签(在所有三种情况下),可以作为训练数据纳入。 - en: We don’t need a whole lot of data to get underway. Although each of the aforementioned steps might sound involved, we can significantly automate the process. What’s cool about this approach is that the more the app is used, the better it becomes, @@ -177,11 +208,13 @@ id: totrans-30 prefs: [] type: TYPE_NORMAL + zh: 我们不需要大量数据就可以开始。尽管上述每个步骤听起来可能有些复杂,但我们可以显著地自动化这个过程。这种方法的酷之处在于,应用程序被使用得越多,它就变得越好,自动地。就好像它有自己的生命一样。我们在本章末尾探讨了这种自我进化的方法。 - en: Tip id: totrans-31 prefs: - PREF_H6 type: TYPE_NORMAL + zh: 提示 - en: A die-hard fan of your app/company would make for a good alpha user. Alpha users ideally are people who have a vested interest in the success of your product. For a food recognition app, a potential group of users could be fitness buffs @@ -195,6 +228,7 @@ id: totrans-32 prefs: [] type: TYPE_NORMAL + zh: 您的应用/公司的铁杆粉丝将成为一个好的α用户。α用户理想情况下是那些对您的产品成功有着切身利益的人。对于一个食物识别应用程序,潜在的用户群可能是那些关注每一卡路里和食材的健身爱好者。这些用户明白应用程序的质量在一开始可能不尽如人意,但他们也看到了通过持续、建设性的反馈来塑造它的角色。他们自愿同意签署一个自由的数据共享协议,以提供诸如使用度量和日常使用中的图像帧等数据。我们建议您的用户清楚地知道您将收集关于他们的哪些信息,并允许他们选择退出或删除。不要显得可怕! - en: In this chapter, we explore the different parts of the aforementioned life cycle and the tools that help us with each of those steps. In the end, we will take a holistic, end-to-end look at the entire mobile development life cycle, not just @@ -204,51 +238,62 @@ id: totrans-33 prefs: [] type: TYPE_NORMAL + zh: 在本章中,我们探讨了上述生命周期的不同部分以及帮助我们完成每个步骤的工具。最后,我们将全面、端到端地查看整个移动开发生命周期,不仅仅是从本章开始,还包括之前的章节,并结合它们看看我们如何有效地在构建一个生产质量的真实应用程序中使用它们。 - en: Our journey begins with understanding the following tools from the Google ecosystem. id: totrans-34 prefs: [] type: TYPE_NORMAL + zh: 我们的旅程始于理解谷歌生态系统中以下工具。 - en: TensorFlow Lite id: totrans-35 prefs: [] type: TYPE_NORMAL + zh: TensorFlow Lite - en: Model conversion and mobile inference engine. id: totrans-36 prefs: [] type: TYPE_NORMAL + zh: 模型转换和移动推理引擎。 - en: ML Kit id: totrans-37 prefs: [] type: TYPE_NORMAL + zh: ML Kit - en: High-level software development kit (SDK) with several built-in APIs, along with the ability to run custom TensorFlow Lite models as well as integration with Firebase on Google Cloud. id: totrans-38 prefs: [] type: TYPE_NORMAL + zh: 高级软件开发工具包(SDK),具有几个内置API,以及运行自定义TensorFlow Lite模型的能力,以及与Firebase在谷歌云上的集成。 - en: Firebase id: totrans-39 prefs: [] type: TYPE_NORMAL + zh: Firebase - en: A cloud-based framework that provides the necessary infrastructure for production-quality mobile applications, including analytics, crash reporting, A/B testing, push notifications, and more. id: totrans-40 prefs: [] type: TYPE_NORMAL + zh: 一个基于云的框架,为生产质量的移动应用程序提供必要的基础设施,包括分析、崩溃报告、A/B测试、推送通知等。 - en: TensorFlow Model Optimization Toolkit id: totrans-41 prefs: [] type: TYPE_NORMAL + zh: TensorFlow模型优化工具包 - en: A set of tools for optimizing the size and performance of models. id: totrans-42 prefs: [] type: TYPE_NORMAL + zh: 用于优化模型大小和性能的一组工具。 - en: An Overview of TensorFlow Lite id: totrans-43 prefs: - PREF_H1 type: TYPE_NORMAL + zh: TensorFlow Lite概述 - en: As mentioned in [Chapter 11](part0013.html#CCNA3-13fa565533764549a6f0ab7f11eed62b), Google released an on-device inference engine called TensorFlow Lite that expanded the reach of the TensorFlow ecosystem beyond the cloud and desktops. Prior to @@ -258,15 +303,20 @@ id: totrans-44 prefs: [] type: TYPE_NORMAL + zh: 正如[第11章](part0013.html#CCNA3-13fa565533764549a6f0ab7f11eed62b)中提到的,谷歌发布了一个名为TensorFlow + Lite的设备端推理引擎,将TensorFlow生态系统的覆盖范围扩展到了云端和桌面之外。在此之前,TensorFlow生态系统中的选项是将整个TensorFlow库移植到iOS(这样会很沉重和缓慢),后来是其略微简化的版本TensorFlow + Mobile(有所改进,但仍然相当庞大)。 - en: 'TensorFlow Lite is optimized from the ground up for mobile, with the following salient features:' id: totrans-45 prefs: [] type: TYPE_NORMAL + zh: TensorFlow Lite从头开始针对移动端进行了优化,具有以下显著特点: - en: Small id: totrans-46 prefs: [] type: TYPE_NORMAL + zh: 小 - en: TensorFlow Lite comes packaged with a much lighter interpreter. Even with all the operators included, the interpreter is less than 300 KB. In typical usage with common models like MobileNet, we can expect that footprint to be less than @@ -277,10 +327,13 @@ id: totrans-47 prefs: [] type: TYPE_NORMAL + zh: TensorFlow Lite附带了一个更轻量级的解释器。即使包含了所有操作符,解释器的大小也不到300KB。在使用常见模型如MobileNet时,我们可以预期该占用空间小于200KB。作为参考,上一代的TensorFlow + Mobile曾占用1.5MB。此外,TensorFlow Lite使用了选择性注册——它只打包模型将使用的操作,最小化不必要的开销。 - en: Fast id: totrans-48 prefs: [] type: TYPE_NORMAL + zh: 快速 - en: TensorFlow Lite provides a significant speedup because it is able to take advantage of on-device *hardware acceleration such as* GPUs and NPUs, where available. In the Android ecosystem, it uses the Android Neural Networks API for acceleration. @@ -412,6 +465,9 @@ id: totrans-65 prefs: [] type: TYPE_NORMAL + zh: 在物联网(IoT)世界中,微控制器(MCU)占主导地位。没有操作系统,没有处理器,内存很少(几KB),这些产品在大量生产中成本低廉,并且易于整合到各种应用程序中。使用TensorFlow + Lite for Microcontrollers,开发人员可以在这些裸机设备上运行人工智能,而无需互联网连接。针对MCU的TensorFlow Lite解释器的精简版本(大约20 + KB)称为TensorFlow Lite Micro Interpreter。 - en: So how does TensorFlow Lite interact with hardware? By using delegates that are platform-aware objects that expose a consistent platform-agnostic API. In other words, delegates shield the interpreter from needing to know anything about @@ -422,6 +478,8 @@ id: totrans-66 prefs: [] type: TYPE_NORMAL + zh: 那么TensorFlow Lite如何与硬件交互呢?通过使用代理,这些代理是平台感知对象,公开一致的平台无关API。换句话说,代理屏蔽了解释器对其运行的具体硬件的任何了解。它们承担了图执行的全部或部分责任,否则将在CPU上运行,而现在则在效率更高的GPU和NPU上运行。在Android上,GPU代理使用OpenGL加速性能,而在iOS上则使用Metal + API。 - en: Given that TensorFlow Lite by itself is platform agnostic, it needs to call into a platform-specific library that implements a known contract. This contract is the TensorFlow Lite Delegate API. Within Android, this contract is fulfilled @@ -432,15 +490,20 @@ id: totrans-67 prefs: [] type: TYPE_NORMAL + zh: 鉴于TensorFlow Lite本身是平台无关的,它需要调用一个实现已知合同的特定平台库。这个合同是TensorFlow Lite Delegate + API。在Android中,这个合同由Android神经网络API来实现(适用于运行Android 8.1及以上版本的设备)。神经网络API旨在为更高级别的机器学习框架提供基础功能层。在苹果世界中,相当于神经网络API的是Metal + Performance Shaders。 - en: With the information we have looked at so far, let’s get hands on. id: totrans-68 prefs: [] type: TYPE_NORMAL + zh: 根据我们目前所了解的信息,让我们动手操作。 - en: Model Conversion to TensorFlow Lite id: totrans-69 prefs: - PREF_H1 type: TYPE_NORMAL + zh: 模型转换为TensorFlow Lite - en: At this point in the book, we should already have a model at hand (either pretrained on ImageNet or custom trained in Keras). Before we can plug that model into an Android app, we need to convert it to the TensorFlow Lite format (a .*tflite* @@ -448,11 +511,14 @@ id: totrans-70 prefs: [] type: TYPE_NORMAL + zh: 在本书的这一部分,我们应该已经有一个模型(在ImageNet上预训练或在Keras中自定义训练)。在我们可以将该模型插入到Android应用程序之前,我们需要将其转换为TensorFlow + Lite格式(一个.*tflite*文件)。 - en: 'Let’s take a look at how to convert the model using the TensorFlow Lite Converter tool, the `tflite_convert` command that comes bundled with our TensorFlow installation:' id: totrans-71 prefs: [] type: TYPE_NORMAL + zh: 让我们看看如何使用TensorFlow Lite Converter工具转换模型,这个`tflite_convert`命令与我们的TensorFlow安装捆绑在一起: - en: '[PRE0]' id: totrans-72 prefs: [] @@ -466,22 +532,27 @@ id: totrans-73 prefs: [] type: TYPE_NORMAL + zh: 这个命令的输出是新的*my_model.tflite*文件,然后我们可以将其插入到下一节中的Android应用程序中。稍后,我们将看看如何使用`tflite_convert`工具使该模型更具性能。此外,TensorFlow + Lite团队已经创建了许多预训练模型,这些模型以TensorFlow Lite格式可用,省去了这一转换步骤。 - en: Building a Real-Time Object Recognition App id: totrans-74 prefs: - PREF_H1 type: TYPE_NORMAL + zh: 构建实时物体识别应用程序 - en: 'Running the sample app from the TensorFlow repository is an easy way to play with the TensorFlow Lite API. Note that we would need an Android phone or tablet to run the app. Following are steps to build and deploy the app:' id: totrans-75 prefs: [] type: TYPE_NORMAL + zh: 从TensorFlow存储库中运行示例应用程序是玩转TensorFlow Lite API的简单方法。请注意,我们需要一部Android手机或平板电脑来运行该应用程序。以下是构建和部署应用程序的步骤: - en: 'Clone the TensorFlow repository:' id: totrans-76 prefs: - PREF_OL type: TYPE_NORMAL + zh: 克隆TensorFlow存储库: - en: '[PRE1]' id: totrans-77 prefs: @@ -493,23 +564,28 @@ prefs: - PREF_OL type: TYPE_NORMAL + zh: 从[*https://developer.android.com/studio*](https://developer.android.com/studio)下载并安装Android + Studio。 - en: Open Android Studio and then select “Open an existing Android Studio project” ([Figure 13-3](part0015.html#start_screen_of_android_studio)). id: totrans-79 prefs: - PREF_OL type: TYPE_NORMAL + zh: 打开Android Studio,然后选择“打开现有的Android Studio项目”([图13-3](part0015.html#start_screen_of_android_studio))。 - en: '![Start screen of Android Studio](../images/00020.jpeg)' id: totrans-80 prefs: - PREF_IND type: TYPE_IMG + zh: '![Android Studio的启动界面](../images/00020.jpeg)' - en: Figure 13-3\. Start screen of Android Studio id: totrans-81 prefs: - PREF_IND - PREF_H6 type: TYPE_NORMAL + zh: 图13-3. Android Studio的启动界面 - en: Go to the location of the cloned TensorFlow repository and then navigate further to *tensorflow/tensorflow/contrib/lite/java/demo/* ([Figure 13-4](part0015.html#android_studio_quotation_markopen_existi)). Select Open. @@ -517,11 +593,13 @@ prefs: - PREF_OL type: TYPE_NORMAL + zh: 转到克隆的TensorFlow存储库的位置,然后进一步导航至*tensorflow/tensorflow/contrib/lite/java/demo/*([图13-4](part0015.html#android_studio_quotation_markopen_existi))。选择打开。 - en: '![Android Studio “Open Existing Project” screen in the TensorFlow repository](../images/00308.jpeg)' id: totrans-83 prefs: - PREF_IND type: TYPE_IMG + zh: '![TensorFlow存储库中Android Studio“打开现有项目”屏幕](../images/00308.jpeg)' - en: Figure 13-4\. Android Studio “Open Existing Project” screen in the TensorFlow repository id: totrans-84 @@ -529,6 +607,7 @@ - PREF_IND - PREF_H6 type: TYPE_NORMAL + zh: 图13-4. TensorFlow存储库中Android Studio“打开现有项目”屏幕 - en: On the Android device, enable Developer Options. (Note that we used a Pixel device here, which uses the stock Android OS. For other manufacturers, the instructions might be a little different.) @@ -536,12 +615,14 @@ prefs: - PREF_OL type: TYPE_NORMAL + zh: 在Android设备上,启用开发者选项。(请注意,我们在这里使用的是Pixel设备,它使用原生Android操作系统。对于其他制造商,说明可能会有所不同。) - en: Go to Settings. id: totrans-86 prefs: - PREF_IND - PREF_OL type: TYPE_NORMAL + zh: 转到设置。 - en: Scroll down to the About Phone or About Tablet option ([Figure 13-5](part0015.html#system_information_screen_on_an_android)) and select it. id: totrans-87 @@ -549,6 +630,7 @@ - PREF_IND - PREF_OL type: TYPE_NORMAL + zh: 向下滚动到“关于手机”或“关于平板电脑”选项([图13-5](part0015.html#system_information_screen_on_an_android)),然后选择它。 - en: '![System information screen on an Android phone; select the About Phone option here](../images/00253.jpeg)' id: totrans-88 @@ -797,11 +879,14 @@ id: totrans-122 prefs: [] type: TYPE_NORMAL + zh: 这对于刚开始很棒。然而,在现实世界中情况要复杂得多。拥有成千上万甚至数百万用户的严肃现实世界应用程序的开发者需要考虑超越推断的问题,比如更新和分发模型,在用户子集中测试不同版本,保持iOS和Android之间的一致性,最终降低每个工程成本。在内部完成所有这些工作可能会昂贵、耗时,而且实际上是不必要的。自然而然,提供这些功能的平台会很吸引人。这就是ML + Kit和Firebase的作用所在。 - en: ML Kit + Firebase id: totrans-123 prefs: - PREF_H1 type: TYPE_NORMAL + zh: ML Kit + Firebase - en: ML Kit is a mobile SDK launched during the 2018 Google I/O conference. It provides convenient APIs for novices as well as advanced ML developers to do a lot of common ML tasks. By default, ML Kit comes with a generic feature set in vision and language @@ -810,24 +895,30 @@ id: totrans-124 prefs: [] type: TYPE_NORMAL + zh: ML Kit是在2018年Google I/O大会上推出的移动SDK。它为初学者和高级ML开发人员提供了方便的API,用于执行许多常见的ML任务。默认情况下,ML + Kit具有视觉和语言智能的通用功能集。[表4-1](part0006.html#top_1percent_accuracy_and_feature_length)列出了我们可以在几行代码中完成的一些常见ML任务。 - en: Table 13-1\. ML Kit built-in features id: totrans-125 prefs: [] type: TYPE_NORMAL + zh: 表13-1\. ML Kit内置功能 - en: '| **Vision** | **Language** |' id: totrans-126 prefs: [] type: TYPE_TB + zh: '| **视觉** | **语言** |' - en: '| --- | --- |' id: totrans-127 prefs: [] type: TYPE_TB + zh: '| --- | --- |' - en: '| Object classificationObject detection and trackingPopular landmark detectionText recognitionFace detectionBarcode detection | Language identificationOn-device translationSmart replies |' id: totrans-128 prefs: [] type: TYPE_TB + zh: '| 对象分类对象检测和跟踪流行的地标检测文本识别人脸检测条形码检测 | 语言识别设备上翻译智能回复 |' - en: ML Kit also gives us the ability to use custom trained TensorFlow Lite models for inference. Let’s take a moment to appreciate why this is so great for developers. Imagine that we were building a business card scanner. We could bring in a custom @@ -842,6 +933,8 @@ id: totrans-129 prefs: [] type: TYPE_NORMAL + zh: ML Kit还使我们能够使用自定义训练的TensorFlow Lite模型进行推断。让我们花点时间来欣赏为什么这对开发者如此重要。想象一下,我们正在构建一个名片扫描器。我们可以引入一个自定义的名片检测模型,识别出名片可见时及其边界(以创建一个漂亮的视觉用户界面),运行内置的文本识别,并过滤掉边界之外的文本,以防止多余的字符。或者,考虑一个可以通过指向物体构建的语言学习游戏,运行一个物体分类器,然后使用设备上的翻译API来宣布标签的法语。使用ML + Kit完全可以相对快速地构建这些功能。虽然许多这些功能也在Core ML中可用,但ML Kit具有跨平台的额外优势。 - en: 'ML Kit, though, is only one piece of the puzzle. It integrates into Google’s Firebase, the mobile and web application development platform that is part of Google Cloud. Firebase offers an array of features that are necessary infrastructure @@ -849,46 +942,55 @@ id: totrans-130 prefs: [] type: TYPE_NORMAL + zh: 然而,ML Kit只是谜题的一部分。它集成到Google的Firebase中,这是Google Cloud的一部分,是移动和Web应用程序开发平台。Firebase提供了一系列功能,这些功能是生产质量应用程序所必需的基础设施,例如以下功能: - en: Push notifications id: totrans-131 prefs: - PREF_UL type: TYPE_NORMAL + zh: 推送通知 - en: Authentication id: totrans-132 prefs: - PREF_UL type: TYPE_NORMAL + zh: 认证 - en: Crash reporting id: totrans-133 prefs: - PREF_UL type: TYPE_NORMAL + zh: 崩溃报告 - en: Logging id: totrans-134 prefs: - PREF_UL type: TYPE_NORMAL + zh: 日志 - en: Performance monitoring id: totrans-135 prefs: - PREF_UL type: TYPE_NORMAL + zh: 性能监控 - en: Hosted device testing id: totrans-136 prefs: - PREF_UL type: TYPE_NORMAL + zh: 托管设备测试 - en: A/B testing id: totrans-137 prefs: - PREF_UL type: TYPE_NORMAL + zh: A/B测试 - en: Model management id: totrans-138 prefs: - PREF_UL type: TYPE_NORMAL + zh: 模型管理 - en: The last point is very pertinent to us. One of the biggest benefits that Firebase gives us is the ability to host our custom models on the cloud and download them within the app as needed. Simply copy the models over to Firebase on Google Cloud, @@ -898,11 +1000,14 @@ id: totrans-139 prefs: [] type: TYPE_NORMAL + zh: 最后一点对我们非常重要。Firebase给我们带来的最大好处之一是能够在云端托管我们的自定义模型,并根据需要在应用程序中下载它们。只需将模型复制到Google + Cloud上的Firebase,引用应用程序内的ML Kit模型,然后我们就可以开始了。A/B测试功能使我们能够向不同用户展示同一模型的不同版本,并测量不同模型之间的性能。 - en: Note id: totrans-140 prefs: - PREF_H6 type: TYPE_NORMAL + zh: 注意 - en: For a lot of the built-in functionality, ML Kit optionally provides a feature to process images in the cloud, where the models are much larger compared to on-device models. These larger models obviously require more powerful hardware to run them, @@ -913,23 +1018,27 @@ id: totrans-141 prefs: [] type: TYPE_NORMAL + zh: 对于许多内置功能,ML Kit还提供了一个在云端处理图像的功能,其中模型比设备上的模型要大得多。这些更大的模型显然需要更强大的硬件来运行它们,但可以提供一些准确性的改进,可能有更大的分类法(比如成千上万的对象类别而不是数百个)。事实上,一些功能,比如地标识别功能,只能在云端上运行。 - en: The cloud processing option is particularly useful when we need a little bit of extra accuracy and/or the user’s phone has low processing power that prevents it from running the on-device model well. id: totrans-142 prefs: [] type: TYPE_NORMAL + zh: 云处理选项在我们需要一点额外准确性和/或用户手机处理能力不足以很好地运行设备上模型时特别有用。 - en: Object Classification in ML Kit id: totrans-143 prefs: - PREF_H2 type: TYPE_NORMAL + zh: ML Kit中的对象分类 - en: 'For our previous task of object classification in real time, if we use ML Kit instead of vanilla TensorFlow Lite, we can simplify our code to just the following lines (in Kotlin):' id: totrans-144 prefs: [] type: TYPE_NORMAL + zh: 对于我们之前的实时对象分类任务,如果我们使用ML Kit而不是原始的TensorFlow Lite,我们可以简化我们的代码到以下几行(使用Kotlin): - en: '[PRE2]' id: totrans-145 prefs: [] @@ -1384,21 +1493,25 @@ - PREF_IND - PREF_H6 type: TYPE_NORMAL + zh: 图13-22。远程配置屏幕的变体部分 - en: Select Review and then select “Start experiment.” Over time, we can increase the distribution of users using the variant. id: totrans-215 prefs: - PREF_OL type: TYPE_NORMAL + zh: 选择“审查”,然后选择“开始实验”。随着时间的推移,我们可以增加使用变体的用户分布。 - en: Ta-da! Now we have our experiment up and running. id: totrans-216 prefs: [] type: TYPE_NORMAL + zh: 哒哒!现在我们的实验已经开始运行。 - en: Measuring an experiment id: totrans-217 prefs: - PREF_H3 type: TYPE_NORMAL + zh: 测量实验效果 - en: Where do we go from here? We want to give our experiment some time to see how it performs. Depending on the type of experiment, we might want to give it a few days to a few weeks. The success of the experiments can be determined by any number @@ -1407,15 +1520,18 @@ id: totrans-218 prefs: [] type: TYPE_NORMAL + zh: 接下来我们该怎么做?我们希望给我们的实验一些时间来看它的表现。根据实验的类型,我们可能需要给它几天到几周的时间。实验的成功可以通过任意数量的标准来确定。Google提供了一些开箱即用的指标,我们可以使用,如[图13-23](part0015.html#analytics_available_when_setting_up_an_a)所示。 - en: '![Analytics available when setting up an A/B testing experiment](../images/00198.jpeg)' id: totrans-219 prefs: [] type: TYPE_IMG + zh: '![设置A/B测试实验时可用的分析](../images/00198.jpeg)' - en: Figure 13-23\. Analytics available when setting up an A/B testing experiment id: totrans-220 prefs: - PREF_H6 type: TYPE_NORMAL + zh: 图13-23。设置A/B测试实验时可用的分析 - en: Suppose that we want to maximize our estimated total revenue—after all, we all want to become rich like Jian-Yang. We would measure our revenue from the users who have the experiment turned on against the baseline; that is, the users who @@ -1427,11 +1543,13 @@ id: totrans-221 prefs: [] type: TYPE_NORMAL + zh: 假设我们想要最大化我们估计的总收入——毕竟,我们都想像Jian-Yang一样变得富有。我们将衡量那些打开实验的用户与基线(即未打开实验的用户)的收入。如果我们的每位用户的收入相对于基线增加,我们将认为实验是成功的。相反,如果每位用户的收入没有增加/减少,我们将得出相反的结论。对于成功的实验,我们希望逐步将其推广到所有用户。在那一点上,它不再是一个实验,而是“毕业”成为核心产品。 - en: Using the Experiment in Code id: totrans-222 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 在代码中使用实验 - en: 'Now that we have set up an experiment, let’s see how to include it in our application using code. To use the appropriate model within our code, we simply need to access the remote configuration object (`remoteConfig`) and get the model name from it. @@ -1441,6 +1559,7 @@ id: totrans-223 prefs: [] type: TYPE_NORMAL + zh: 现在我们已经设置了一个实验,让我们看看如何在我们的应用程序中使用代码包含它。要在代码中使用适当的模型,我们只需访问远程配置对象(`remoteConfig`)并从中获取模型名称。我们从远程配置对象获取的模型名称将取决于用户是否包含在实验中。以下代码行实现了这一点: - en: '[PRE10]' id: totrans-224 prefs: [] @@ -1452,11 +1571,13 @@ id: totrans-225 prefs: [] type: TYPE_NORMAL + zh: 执行预测的其余代码与前几节完全相同。我们的应用现在已准备好使用我们实验规定的正确模型。 - en: TensorFlow Lite on iOS id: totrans-226 prefs: - PREF_H1 type: TYPE_NORMAL + zh: iOS上的TensorFlow Lite - en: The previous chapters demonstrate how easy it is to use Apple’s Core ML on iOS. We can simply drag and drop a model into Xcode and start making inferences with just a couple of lines of code. In contrast, even looking at basic iOS examples @@ -1471,22 +1592,29 @@ id: totrans-227 prefs: [] type: TYPE_NORMAL + zh: 前几章展示了在iOS上使用苹果的Core ML有多么容易。我们只需将模型拖放到Xcode中,然后只需几行代码就可以开始进行推理。相比之下,即使是查看TensorFlow + Lite存储库中的基本iOS示例,也可以明显看出,需要大量样板代码才能使最基本的应用程序运行。相比之下,与ML Kit一起使用的TensorFlow Lite是一种相当愉快的体验。除了能够使用干净简洁的API外,我们还可以获得本章前面详细介绍的所有功能——远程下载和更新模型、模型A/B测试以及云回退处理。所有这些都不需要做太多额外的工作。编写同时面向iOS和Android的深度学习应用程序的开发人员可能会考虑使用ML + Kit作为“一次构建,随处使用”的方式。 - en: Performance Optimizations id: totrans-228 prefs: - PREF_H1 type: TYPE_NORMAL + zh: 性能优化 - en: In [Chapter 6](part0008.html#7K4G3-13fa565533764549a6f0ab7f11eed62b), we explored quantization and pruning, mostly from a theoretical standpoint. Let’s see them up close from TensorFlow Lite’s perspective and the tools to achieve them. id: totrans-229 prefs: [] type: TYPE_NORMAL + zh: 在[第6章](part0008.html#7K4G3-13fa565533764549a6f0ab7f11eed62b)中,我们从理论角度探讨了量化和剪枝。让我们从TensorFlow + Lite的角度以及实现它们的工具中近距离地看看它们。 - en: Quantizing with TensorFlow Lite Converter id: totrans-230 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 使用TensorFlow Lite转换器进行量化 - en: For iOS, Apple provides `quantization_utils` in its Core ML Tools package. And for TensorFlow Lite, the equivalent is the already built-in `tflite_convert` tool, which we used earlier in this chapter. On the command line, we can specify the @@ -1497,6 +1625,7 @@ id: totrans-231 prefs: [] type: TYPE_NORMAL + zh: 对于iOS,苹果在其Core ML工具包中提供了`quantization_utils`。而对于TensorFlow Lite,等效的是已经内置的`tflite_convert`工具,我们在本章前面使用过。在命令行中,我们可以指定输入文件、模型图、要转换为的数据类型以及输入和输出的名称(可以使用Netron进行检查,如[第11章](part0013.html#CCNA3-13fa565533764549a6f0ab7f11eed62b)所示)。从32位转换为8位整数表示意味着模型大小减小四倍,而准确性损失相对较小。 - en: '[PRE11]' id: totrans-232 prefs: [] @@ -1507,11 +1636,13 @@ id: totrans-233 prefs: [] type: TYPE_NORMAL + zh: 完成后,此命令应该给我们提供`quantized-model.tflite`模型。 - en: TensorFlow Model Optimization Toolkit id: totrans-234 prefs: - PREF_H2 type: TYPE_NORMAL + zh: TensorFlow模型优化工具包 - en: The TensorFlow Lite converter is the simplest way to get our models quantized. It’s worth noting that the converter quantizes the models post-training. Due to the decrease in representation power, there can be a tiny but noticeable loss @@ -1521,11 +1652,13 @@ id: totrans-235 prefs: [] type: TYPE_NORMAL + zh: TensorFlow Lite转换器是获得我们的模型量化的最简单方法。值得注意的是,转换器在训练后对模型进行量化。由于表示能力的降低,可能会出现轻微但明显的准确率损失。我们能做得更好吗?*量化感知训练*,顾名思义,在训练期间考虑量化的影响,并试图补偿和减少后续训练中可能发生的损失。 - en: 'Although both forms of quantization offer 75% reduction in the size of the model, experiments have shown the following:' id: totrans-236 prefs: [] type: TYPE_NORMAL + zh: 尽管两种量化形式都使模型大小减少了75%,但实验结果显示如下: - en: In the case of MobileNetV2, compared to an eight-point loss in accuracy with post-training quantization, quantization-aware training yielded only a one-point loss. @@ -1533,23 +1666,27 @@ prefs: - PREF_UL type: TYPE_NORMAL + zh: 对于MobileNetV2,与训练后量化导致的准确率下降8个点相比,量化感知训练仅导致了1个点的下降。 - en: For InceptionV3, quantization-aware training yielded a whopping 52% reduction in latency, compared to 25% reduction with post-training quantization. id: totrans-238 prefs: - PREF_UL type: TYPE_NORMAL + zh: 对于InceptionV3,与训练后量化相比,量化感知训练使延迟减少了惊人的52%,而后者仅减少了25%。 - en: Note id: totrans-239 prefs: - PREF_H6 type: TYPE_NORMAL + zh: 注意 - en: It is worth noting that these accuracy metrics are on the 1,000 class ImageNet test set. Most problems have less complexity with a smaller number of classes. Post-training quantization should result in a smaller loss on such simpler problems. id: totrans-240 prefs: [] type: TYPE_NORMAL + zh: 值得注意的是,这些准确率指标是在1,000类ImageNet测试集上的。大多数问题在类别较少时具有较低的复杂性。对于这种更简单的问题,训练后量化应该导致更小的损失。 - en: Quantization-aware training can be implemented with the TensorFlow Model Optimization Toolkit. This toolkit additional offers an increasing array of tools for model compression, including pruning. Additionally, the TensorFlow Lite model repository @@ -1558,56 +1695,70 @@ id: totrans-241 prefs: [] type: TYPE_NORMAL + zh: 量化感知训练可以使用 TensorFlow Model Optimization Toolkit 实现。该工具包还提供了一系列用于模型压缩的工具,包括修剪。此外,TensorFlow + Lite 模型仓库已经提供了使用这种技术的预量化模型。[表13-2](part0015.html#effects_of_different_quantizatio-id00001)列出了各种量化策略的效果。 - en: 'Table 13-2\. Effects of different quantization strategies (8-bit) on models (source: TensorFlow Lite Model optimization documentation)' id: totrans-242 prefs: [] type: TYPE_NORMAL + zh: 表13-2\. 不同量化策略(8位)对模型的影响(来源:TensorFlow Lite模型优化文档) - en: '| Model | MobileNet | MobileNetV2 | InceptionV3 |' id: totrans-243 prefs: [] type: TYPE_TB + zh: '| 模型 | MobileNet | MobileNetV2 | InceptionV3 |' - en: '| --- | --- | --- | --- |' id: totrans-244 prefs: [] type: TYPE_TB + zh: '| --- | --- | --- | --- |' - en: '| **Top-1 accuracy** | **Original** | 0.709 | 0.719 | 0.78 |' id: totrans-245 prefs: [] type: TYPE_TB + zh: '| **Top-1准确率** | **原始** | 0.709 | 0.719 | 0.78 |' - en: '| **Post-training quantized** | 0.657 | 0.637 | 0.772 |' id: totrans-246 prefs: [] type: TYPE_TB + zh: '| **训练后量化** | 0.657 | 0.637 | 0.772 |' - en: '| **Quantization-aware training** | 0.7 | 0.709 | 0.775 |' id: totrans-247 prefs: [] type: TYPE_TB + zh: '| **量化感知训练** | 0.7 | 0.709 | 0.775 |' - en: '| **Latency (ms)** | **Original** | 124 | 89 | 1130 |' id: totrans-248 prefs: [] type: TYPE_TB + zh: '| **延迟(毫秒)** | **原始** | 124 | 89 | 1130 |' - en: '| **Post-training quantized** | 112 | 98 | 845 |' id: totrans-249 prefs: [] type: TYPE_TB + zh: '| **训练后量化** | 112 | 98 | 845 |' - en: '| **Quantization-aware training** | 64 | 54 | 543 |' id: totrans-250 prefs: [] type: TYPE_TB + zh: '| **量化感知训练** | 64 | 54 | 543 |' - en: '| **Size (MB)** | **Original** | 16.9 | 14 | 95.7 |' id: totrans-251 prefs: [] type: TYPE_TB + zh: '| **大小(MB)** | **原始** | 16.9 | 14 | 95.7 |' - en: '| **Optimized** | 4.3 | 3.6 | 23.9 |' id: totrans-252 prefs: [] type: TYPE_TB + zh: '| **优化后** | 4.3 | 3.6 | 23.9 |' - en: Fritz id: totrans-253 prefs: - PREF_H1 type: TYPE_NORMAL + zh: Fritz - en: As we have seen so far, the primary purpose of Core ML and TensorFlow Lite is to serve fast mobile inferences. Have a model, plug it into the app, and run inferences. Then came ML Kit, which apart from its built-in AI capabilities, made it easier @@ -1624,23 +1775,28 @@ id: totrans-254 prefs: [] type: TYPE_NORMAL + zh: 正如我们迄今所见,Core ML 和 TensorFlow Lite 的主要目的是为了提供快速的移动推断。有一个模型,将其插入应用程序中,并运行推断。然后出现了ML + Kit,除了具有内置的AI功能外,还使得部署模型和监视我们的自定义模型(使用Firebase)变得更加容易。回顾整个流程,我们有训练、转换为移动格式、优化速度、部署到用户、监视性能、跟踪模型版本。这些是分布在许多工具中的几个步骤。很可能没有一个人拥有整个流程。这是因为通常这些工具需要一定程度的熟悉度(例如,将模型部署给用户可能超出数据科学家的舒适区)。总部位于波士顿的初创公司Fritz正试图消除这些障碍,使数据科学家和移动开发人员更加简单地完成从模型开发到部署的整个周期。 - en: 'Fritz offers an end-to-end solution for mobile AI development that includes the following noteworthy features:' id: totrans-255 prefs: [] type: TYPE_NORMAL + zh: Fritz提供了一个移动AI开发的端到端解决方案,包括以下值得注意的功能: - en: Ability to deploy models directly to user devices from Keras after training completes using callbacks. id: totrans-256 prefs: - PREF_UL type: TYPE_NORMAL + zh: 能够在训练完成后使用回调函数直接将模型部署到用户设备上。 - en: 'Ability to benchmark a model directly from the computer without having to deploy it on a phone. The following code demonstrates this:' id: totrans-257 prefs: - PREF_UL type: TYPE_NORMAL + zh: 能够在计算机上直接对模型进行基准测试,而无需将其部署到手机上。以下代码演示了这一点: - en: '[PRE12]' id: totrans-258 prefs: @@ -1653,6 +1809,7 @@ prefs: - PREF_UL type: TYPE_NORMAL + zh: 能够加密模型,以便我们的知识产权不被恶意行为者从设备上窃取。 - en: Ability to implement many advanced computer-vision algorithms with just a few lines of code. The SDK comes with ready-to-use, mobile-friendly implementations of these algorithms, which run at high frame rates. For example, image segmentation, @@ -1662,6 +1819,7 @@ prefs: - PREF_UL type: TYPE_NORMAL + zh: 能够用几行代码实现许多先进的计算机视觉算法。SDK带有这些算法的移动友好实现,可以以高帧率运行。例如,图像分割、风格转移、对象检测和姿势估计。[图13-24](part0015.html#performance_of_fritz_sdkapostrophes_obje)显示了在各种iOS设备上运行的对象检测的基准。 - en: '[PRE13]' id: totrans-261 prefs: @@ -1674,6 +1832,7 @@ prefs: - PREF_IND type: TYPE_IMG + zh: '![Fritz SDK在不同移动设备上的对象检测功能性能,相对于iPhone X](../images/00163.jpeg)' - en: Figure 13-24\. Performance of Fritz SDK’s object detection functionality on different mobile devices, relative to the iPhone X id: totrans-263 @@ -1681,6 +1840,7 @@ - PREF_IND - PREF_H6 type: TYPE_NORMAL + zh: 图13-24。Fritz SDK在不同移动设备上的对象检测功能性能,相对于iPhone X - en: Ability to customize the prebuilt models to custom datasets by retraining using their Jupyter notebooks. It’s worth noting that this can be a difficult problem (even for professional data scientists) that is simplified significantly because @@ -1689,11 +1849,13 @@ prefs: - PREF_UL type: TYPE_NORMAL + zh: 能够通过重新训练使用他们的Jupyter笔记本自定义预构建模型到自定义数据集。值得注意的是,这可能是一个困难的问题(即使对于专业数据科学家来说),但因为开发人员只需确保数据格式正确,这个问题就得到了很大的简化。 - en: Ability to manage all model versions from the command line. id: totrans-265 prefs: - PREF_UL type: TYPE_NORMAL + zh: 能够从命令行管理所有模型版本。 - en: Ability to test out mobile readiness of our models using Fritz’ open source app called Heartbeat (also available on iOS/Android app stores). Assuming that we have a model ready without much mobile know how, we can clone the app, swap @@ -1702,17 +1864,20 @@ prefs: - PREF_UL type: TYPE_NORMAL + zh: 能够使用Fritz的开源应用Heartbeat测试我们模型的移动准备性(也可在iOS/Android应用商店中找到)。假设我们已经准备好一个模型,但对移动端不太了解,我们可以克隆该应用,用我们自己的模型替换现有模型,并在手机上运行。 - en: A vibrant community of contributors blogging about the latest in mobile AI on *heartbeat.fritz.ai*. id: totrans-267 prefs: - PREF_UL type: TYPE_NORMAL + zh: 一个充满活力的社区,关于移动AI最新信息的博客在*heartbeat.fritz.ai*上。 - en: A Holistic Look at the Mobile AI App Development Cycle id: totrans-268 prefs: - PREF_H1 type: TYPE_NORMAL + zh: 移动AI应用开发周期的整体视角 - en: Until this point in the book, we’ve looked at a bunch of techniques and technologies that enable us to perform individual tasks in the mobile AI development cycle. Here, we tie everything together by exploring the kind of questions that come @@ -1721,46 +1886,55 @@ id: totrans-269 prefs: [] type: TYPE_NORMAL + zh: 到目前为止,在本书中,我们已经看过许多技术和技术,使我们能够在移动AI开发周期中执行各种任务。在这里,我们通过探索贯穿整个生命周期的问题类型,将所有内容联系在一起。[图13-25](part0015.html#mobile_ai_app_development_life_cycle)提供了开发周期的广泛概述。 - en: '![Mobile AI app development life cycle](../images/00162.jpeg)' id: totrans-270 prefs: [] type: TYPE_IMG + zh: '![移动AI应用开发生命周期](../images/00162.jpeg)' - en: Figure 13-25\. Mobile AI app development life cycle id: totrans-271 prefs: - PREF_H6 type: TYPE_NORMAL + zh: 图13-25。移动AI应用开发生命周期 - en: How Do I Collect Initial Data? id: totrans-272 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 如何收集初始数据? - en: 'We can apply a few different strategies to accomplish this:' id: totrans-273 prefs: [] type: TYPE_NORMAL + zh: 我们可以采用几种不同的策略来实现这一点: - en: Find the object of interest and manually take photos with different angles, lighting, environments, framing, and so on. id: totrans-274 prefs: - PREF_UL type: TYPE_NORMAL + zh: 找到感兴趣的对象,并手动拍摄不同角度、光照、环境、构图等照片。 - en: Scrape from the internet using browser extensions like Fatkun ([Chapter 12](part0014.html#DB7S3-13fa565533764549a6f0ab7f11eed62b)). id: totrans-275 prefs: - PREF_UL type: TYPE_NORMAL + zh: 使用浏览器扩展程序(如Fatkun)从互联网上抓取数据([第12章](part0014.html#DB7S3-13fa565533764549a6f0ab7f11eed62b))。 - en: Find existing datasets ([Google Dataset Search](https://oreil.ly/NawZ6)). For example, Food-101 for dishes. id: totrans-276 prefs: - PREF_UL type: TYPE_NORMAL + zh: 查找现有数据集([Google数据集搜索](https://oreil.ly/NawZ6))。例如,Food-101用于菜肴。 - en: Synthesize your own dataset. id: totrans-277 prefs: - PREF_UL type: TYPE_NORMAL + zh: 合成自己的数据集。 - en: Place the object (foreground) in front of a green screen (background) and take a photograph of it. Replace the background with random images to synthesize a large dataset, while zooming, cropping, and rotating to create potentially hundreds @@ -1770,6 +1944,7 @@ - PREF_IND - PREF_OL type: TYPE_NORMAL + zh: 将对象(前景)放在绿幕(背景)前拍摄照片。将背景替换为随机图像以合成大型数据集,同时进行缩放、裁剪和旋转,以创建数百张图像。 - en: If a green screen is not possible, segment (i.e., cut) the object out of the background from existing real-world images and repeat the previous step in order to build a robust, diverse dataset. It’s important to note that, in points (a) @@ -1780,6 +1955,7 @@ - PREF_IND - PREF_OL type: TYPE_NORMAL + zh: 如果无法使用绿幕,从现有的真实世界图像中分割(即切割)出对象并重复上一步骤,以构建一个强大、多样化的数据集。需要注意的是,在(a)和(b)点中,前景需要有足够的多样性;否则,网络可能会过度学习一个例子,而不是理解对象。 - en: Find a realistic 3D model of the object of interest and place it in realistic environments using a 3D framework such as Unity. Adjust the lighting and camera position, zoom, and rotation to take snapshots of this object from many angles. @@ -1792,11 +1968,13 @@ - PREF_IND - PREF_OL type: TYPE_NORMAL + zh: 找到感兴趣对象的真实3D模型,并将其放置在使用Unity等3D框架的真实环境中。调整光照和相机位置、缩放和旋转,从多个角度拍摄这个对象的快照。我们在[第7章](part0009.html#8IL23-13fa565533764549a6f0ab7f11eed62b)中探讨了AI.Reverie和CVEDIA等在这一领域工作的公司。我们使用逼真的模拟器来为自动驾驶章节([第16章](part0019.html#I3QM3-13fa565533764549a6f0ab7f11eed62b)和[第17章](part0020.html#J2B83-13fa565533764549a6f0ab7f11eed62b))训练模型。 - en: How Do I Label My Data? id: totrans-281 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 如何为我的数据标记标签? - en: Most of the steps in the previous answer should have already given you labels for the data. For unlabeled collections, use labeling tools such as Supervisely, Labelbox, and Diffgram. For really large datasets in which annotating data by @@ -1806,20 +1984,25 @@ id: totrans-282 prefs: [] type: TYPE_NORMAL + zh: 在前一个答案中的大多数步骤应该已经为您提供了数据的标签。对于未标记的集合,请使用Supervisely、Labelbox和Diffgram等标记工具。对于真正大型的数据集,自己注释数据可能不可行,提供收入机会给弱势群体的社会责任标记服务,如Digital + Data Divide、iMerit和Samasource,可能是一个不错的选择。 - en: How Do I Train My Model? id: totrans-283 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 如何训练我的模型? - en: 'The following are the two broad approaches to train a model:' id: totrans-284 prefs: [] type: TYPE_NORMAL + zh: 训练模型有两种广泛的方法: - en: 'With code: Use Keras and TensorFlow ([Chapter 3](part0005.html#4OIQ3-13fa565533764549a6f0ab7f11eed62b)).' id: totrans-285 prefs: - PREF_UL type: TYPE_NORMAL + zh: 使用代码:使用Keras和TensorFlow。 - en: 'Without code: Use custom classifier services such as Google’s Auto ML, Microsoft’s CustomVision.ai, and Clarifai (benchmarked in [Chapter 8](part0010.html#9H5K3-13fa565533764549a6f0ab7f11eed62b)), or Apple’s ecosystem-only Create ML ([Chapter 12](part0014.html#DB7S3-13fa565533764549a6f0ab7f11eed62b)).' @@ -1827,46 +2010,56 @@ prefs: - PREF_UL type: TYPE_NORMAL + zh: 无需代码:使用自定义分类器服务,如Google的Auto ML、Microsoft的CustomVision.ai和Clarifai,或仅适用于Apple生态系统的Create + ML。 - en: How Do I Convert the Model to a Mobile-Friendly Format? id: totrans-287 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 如何将模型转换为移动友好格式? - en: 'The following are a few different ways to convert a model to a mobile-compatible format:' id: totrans-288 prefs: [] type: TYPE_NORMAL + zh: 以下是将模型转换为移动兼容格式的几种不同方法: - en: Use Core ML Tools (Apple only). id: totrans-289 prefs: - PREF_UL type: TYPE_NORMAL + zh: 使用Core ML Tools(仅适用于Apple)。 - en: Use TensorFlow Lite Converter for iOS and Android. id: totrans-290 prefs: - PREF_UL type: TYPE_NORMAL + zh: 对于iOS和Android,请使用TensorFlow Lite Converter。 - en: Alternatively, use Fritz for the end-to-end pipeline. id: totrans-291 prefs: - PREF_UL type: TYPE_NORMAL + zh: 或者,使用Fritz进行端到端的流程。 - en: How Do I Make my Model Performant? id: totrans-292 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 如何使我的模型性能良好? - en: 'Here are some techniques to make a model performant:' id: totrans-293 prefs: [] type: TYPE_NORMAL + zh: 以下是一些使模型性能良好的技术: - en: Start with an efficient model such as the MobileNet family, or even better, EfficientNet. id: totrans-294 prefs: - PREF_UL type: TYPE_NORMAL + zh: 从MobileNet系列等高效模型开始,甚至更好的是EfficientNet。 - en: Make the model smaller in size by quantizing and pruning it to improve loading and inference times, while keeping the model accuracy relatively intact ([Chapter 6](part0008.html#7K4G3-13fa565533764549a6f0ab7f11eed62b), [Chapter 11](part0013.html#CCNA3-13fa565533764549a6f0ab7f11eed62b), and [Chapter 13](part0015.html#E9OE3-13fa565533764549a6f0ab7f11eed62b)). @@ -1875,17 +2068,20 @@ prefs: - PREF_UL type: TYPE_NORMAL + zh: 通过量化和修剪模型来减小模型的大小,以改善加载和推断时间,同时保持模型准确性相对完好。预计大小会减小高达75%,准确性损失很小。 - en: Use Core ML Tools for the Apple ecosystem or TensorFlow Model Optimization Kit for both iOS and Android. id: totrans-296 prefs: - PREF_UL type: TYPE_NORMAL + zh: 对于Apple生态系统,请使用Core ML Tools,对于iOS和Android,请使用TensorFlow模型优化工具包。 - en: How Do I Build a Great UX for My Users? id: totrans-297 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 如何为用户构建出色的用户体验? - en: Obviously, that’s going to depend on the kind of problem you are trying to solve. But a general guideline is to strike the proper balance between interactivity, performance, and resource usage (memory, CPU, battery, etc.). And, of course, @@ -1894,6 +2090,7 @@ id: totrans-298 prefs: [] type: TYPE_NORMAL + zh: 显然,这将取决于您试图解决的问题的类型。但一个一般的指导原则是在互动性、性能和资源使用(内存、CPU、电池等)之间取得适当的平衡。当然,一个智能的反馈机制可以实现轻松的数据收集和反馈。将这种体验变成游戏化将使其达到一个全新的水平。 - en: In the case of the food classifier app, after the user takes a picture, our UI would show a list of top five candidate predictions (in decreasing order of confidence) for a given photo. For example, if the user takes a picture of a pizza, @@ -1908,62 +2105,75 @@ id: totrans-299 prefs: [] type: TYPE_NORMAL + zh: 在食品分类器应用程序的情况下,用户拍照后,我们的用户界面将显示给定照片的前五个候选预测列表(按置信度降序排列)。例如,如果用户拍摄了一张披萨的照片,屏幕上显示的候选预测可能是“馅饼 + - 75%”,“披萨 - 15%”,“烤饼 - 6%”,“面包 - 3%”,“砂锅 - 1%”。在这种情况下,用户将选择第二个预测。对于完美的模型,用户将始终选择第一个预测。由于我们的模型并不完美,用户选择的预测的排名将成为未来改进模型的信号。在最坏的情况下,如果没有一个预测是正确的,用户应该有一种手动标记数据的方法。在手动输入时提供自动建议功能将有助于保持数据集中的干净标签。 - en: An even better experience could be one in which the user never needs to click a picture. Rather, the predictions are available in real time. id: totrans-300 prefs: [] type: TYPE_NORMAL + zh: 一个更好的体验可能是用户永远不需要拍照。相反,预测是实时可用的。 - en: How Do I Make the Model Available to My Users? id: totrans-301 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 如何让模型对用户可用? - en: 'The following are some ways to deploy models to users:' id: totrans-302 prefs: [] type: TYPE_NORMAL + zh: 以下是将模型部署给用户的一些方法: - en: Bundle the model into the app binary and release it on the app stores. id: totrans-303 prefs: - PREF_UL type: TYPE_NORMAL + zh: 将模型捆绑到应用程序二进制文件中,并在应用商店发布。 - en: Alternatively, host the model in the cloud and have the app download the model, as necessary. id: totrans-304 prefs: - PREF_UL type: TYPE_NORMAL + zh: 或者,将模型托管在云中,并在需要时让应用程序下载模型。 - en: Use a model management service like Fritz or Firebase (integrated with ML Kit). id: totrans-305 prefs: - PREF_UL type: TYPE_NORMAL + zh: 使用Fritz或Firebase等模型管理服务(与ML Kit集成)。 - en: How Do I Measure the Success of My Model? id: totrans-306 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 如何衡量模型的成功? - en: 'The very first step is to determine the success criteria. Consider the following examples:' id: totrans-307 prefs: [] type: TYPE_NORMAL + zh: 第一步是确定成功标准。考虑以下示例: - en: “My model should run inferences in under 200 ms at the 90th percentile.” id: totrans-308 prefs: - PREF_UL type: TYPE_NORMAL + zh: 我的模型应该在第90百分位数以下的时间内运行推断。 - en: “Users who use this model open the app every day.” id: totrans-309 prefs: - PREF_UL type: TYPE_NORMAL + zh: “使用这个模型的用户每天都会打开应用程序。” - en: In the food classifier app, a success metric could be something along the lines of “80% of users selected the first prediction in the list of predictions.” id: totrans-310 prefs: - PREF_UL type: TYPE_NORMAL + zh: 在食品分类器应用程序中,一个成功的指标可能是“80%的用户选择了预测列表中的第一个预测”。 - en: These success criteria are not set in stone and should continue to evolve over time. It’s very essential to be data driven. When you have a new model version, run A/B tests on a subset of users and evaluate the success criteria against that @@ -1971,15 +2181,18 @@ id: totrans-311 prefs: [] type: TYPE_NORMAL + zh: 这些成功标准并非一成不变,应该随着时间的推移不断发展。数据驱动非常重要。当你有一个新的模型版本时,在一部分用户中运行A/B测试,并根据该版本评估成功标准,以确定它是否比上一个版本更好。 - en: How Do I Improve My Model? id: totrans-312 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 如何改进我的模型? - en: 'Here are some ways to improve the quality of our models:' id: totrans-313 prefs: [] type: TYPE_NORMAL + zh: 以下是改进我们模型质量的一些方法: - en: 'Collect feedback on individual predictions from users: what was correct, and, more important, what was incorrect. Feed these images along with the corresponding labels as input for the next model training cycle. [Figure 13-26](part0015.html#the_feedback_cycle_of_an_incorrect_predi) @@ -1988,6 +2201,7 @@ prefs: - PREF_UL type: TYPE_NORMAL + zh: 从用户那里收集关于个别预测的反馈:什么是正确的,更重要的是,什么是错误的。将这些图像与相应的标签一起作为下一个模型训练周期的输入。[图13-26](part0015.html#the_feedback_cycle_of_an_incorrect_predi)说明了这一点。 - en: For users who have explicitly opted in, collect frames automatically whenever the prediction confidence is low. Manually label those frames and feed them into the next training cycle. @@ -1995,53 +2209,63 @@ prefs: - PREF_UL type: TYPE_NORMAL + zh: 对于明确选择加入的用户,在预测置信度低时自动收集帧。手动标记这些帧并将其输入到下一个训练周期中。 - en: '![The feedback cycle of an incorrect prediction, generating more training data, leading to an improved model](../images/00073.jpeg)' id: totrans-316 prefs: [] type: TYPE_IMG + zh: '![错误预测的反馈周期,生成更多训练数据,导致改进的模型](../images/00073.jpeg)' - en: Figure 13-26\. The feedback cycle of an incorrect prediction, generating more training data, leading to an improved model id: totrans-317 prefs: - PREF_H6 type: TYPE_NORMAL + zh: 图13-26. 错误预测的反馈周期,生成更多训练数据,导致改进的模型 - en: How Do I Update the Model on My Users’ Phones? id: totrans-318 prefs: - PREF_H2 type: TYPE_NORMAL + zh: 如何在用户手机上更新模型? - en: 'Here are some ways to update the model on your users’ phones:' id: totrans-319 prefs: [] type: TYPE_NORMAL + zh: 以下是如何在用户手机上更新模型的一些方法: - en: Bundle the new model into the next app release. This is slow and inflexible. id: totrans-320 prefs: - PREF_UL type: TYPE_NORMAL + zh: 将新模型捆绑到下一个应用程序发布中。这是缓慢且不灵活的。 - en: Host the new model on the cloud and force the apps out in the world to download the new model. Prefer to do this when the user is on WiFi. id: totrans-321 prefs: - PREF_UL type: TYPE_NORMAL + zh: 将新模型托管到云端,并强制应用程序在世界范围内下载新模型。最好在用户连接WiFi时执行此操作。 - en: Use a model management system such as Firebase (in conjunction with ML Kit) or Fritz to automate a lot of the grunt work involved. id: totrans-322 prefs: - PREF_UL type: TYPE_NORMAL + zh: 使用模型管理系统,如Firebase(与ML Kit一起)或Fritz来自动化涉及的大部分繁重工作。 - en: With all of these questions answered, let’s appreciate the beauty of how this app improves on its own. id: totrans-323 prefs: [] type: TYPE_NORMAL + zh: 有了所有这些问题的答案,让我们欣赏这个应用程序如何自我改进的美丽。 - en: The Self-Evolving Model id: totrans-324 prefs: - PREF_H1 type: TYPE_NORMAL + zh: 自我进化模型 - en: For applications that need only mature pretrained models, our job is already done. Just plug the model into an app and we’re ready to go. For custom-trained models that rely especially on scarce training data, we can get the user involved @@ -2049,21 +2273,25 @@ id: totrans-325 prefs: [] type: TYPE_NORMAL + zh: 对于只需要成熟预训练模型的应用程序,我们的工作已经完成。只需将模型插入应用程序,我们就可以开始了。对于特别依赖稀缺训练数据的定制训练模型,我们可以让用户参与构建一个自我改进、不断发展的模型。 - en: At the most fundamental level, each time a user uses the app, they’re providing the necessary feedback (image + label) to improve the model further, as illustrated in [Figure 13-27](part0015.html#the_self-evolving_model_cycle). id: totrans-326 prefs: [] type: TYPE_NORMAL + zh: 在最基本的层面上,每当用户使用应用程序时,他们都会提供必要的反馈(图像+标签)以进一步改进模型,如[图13-27](part0015.html#the_self-evolving_model_cycle)所示。 - en: '![The self-evolving model cycle](../images/00033.jpeg)' id: totrans-327 prefs: [] type: TYPE_IMG + zh: '![自我进化模型周期](../images/00033.jpeg)' - en: Figure 13-27\. The self-evolving model cycle id: totrans-328 prefs: - PREF_H6 type: TYPE_NORMAL + zh: 图13-27. 自我进化模型周期 - en: Just like a university student must graduate multiple years before getting ready to step into the real world, a model needs to go through phases of development before it reaches its final users. The following is a popular model of phasing @@ -2071,20 +2299,24 @@ id: totrans-329 prefs: [] type: TYPE_NORMAL + zh: 就像大学生必须在步入现实世界之前经历多年的毕业准备一样,模型在到达最终用户之前必须经历开发阶段。以下是软件发布阶段的流行模型,也是发布AI模型的有用指南。 - en: 1\. Dev id: totrans-330 prefs: [] type: TYPE_NORMAL + zh: 1. Dev - en: This is the initial phase of development in which the developers of the app are the only users of it. Data accumulation is slow, the experience is very buggy, and model predictions can be quite unreliable. id: totrans-331 prefs: [] type: TYPE_NORMAL + zh: 这是开发的初始阶段,在这个阶段,应用程序的开发人员是唯一的用户。数据积累缓慢,体验非常不稳定,模型预测可能相当不可靠。 - en: 2\. Alpha id: totrans-332 prefs: [] type: TYPE_NORMAL + zh: 2. Alpha - en: After the app is ready to be tested by a few users beyond the developers, it is now considered to be in alpha. User feedback is extremely crucial here because it would serve to improve the app experience as well provide data at a greater @@ -2094,10 +2326,12 @@ id: totrans-333 prefs: [] type: TYPE_NORMAL + zh: 在应用程序准备好由开发人员之外的少数用户测试之后,现在被认为处于alpha阶段。用户反馈在这里非常关键,因为它将有助于改善应用体验,并提供更大规模的数据给管道。这里的体验不像以前那样有bug,模型预测也更可靠一些。在一些组织中,这个阶段也被称为*dogfooding*(即员工进行内部应用测试)。 - en: 3\. Beta id: totrans-334 prefs: [] type: TYPE_NORMAL + zh: 3. Beta - en: An app in beta has significantly more users than in alpha. User feedback is also quite important here. The data collection rate is at an even larger scale. Many different devices are out in the real world collecting data to help improve